1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
gpu / command_buffer / service / shared_image / dawn_shared_texture_holder.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/shared_image/dawn_shared_texture_holder.h"
#include "base/check_op.h"
namespace gpu {
DawnSharedTextureHolder::SharedTextureData::SharedTextureData() = default;
DawnSharedTextureHolder::SharedTextureData::~SharedTextureData() {
for (auto [texture_usage, texture] : texture_cache) {
texture.Destroy();
}
}
DawnSharedTextureHolder::SharedTextureData::SharedTextureData(
SharedTextureData&&) = default;
DawnSharedTextureHolder::SharedTextureData&
DawnSharedTextureHolder::SharedTextureData::operator=(SharedTextureData&&) =
default;
DawnSharedTextureHolder::DawnSharedTextureHolder() = default;
DawnSharedTextureHolder::~DawnSharedTextureHolder() {}
DawnSharedTextureHolder::DawnSharedTextureHolder(DawnSharedTextureHolder&&) =
default;
DawnSharedTextureHolder& DawnSharedTextureHolder::operator=(
DawnSharedTextureHolder&&) = default;
DawnSharedTextureHolder::WGPUTextureCache*
DawnSharedTextureHolder::GetWGPUTextureCache(const wgpu::Device& device) {
auto iter = shared_texture_data_cache_.find(device.Get());
if (iter == shared_texture_data_cache_.end()) {
return nullptr;
}
return &iter->second.texture_cache;
}
wgpu::SharedTextureMemory DawnSharedTextureHolder::GetSharedTextureMemory(
const wgpu::Device& device) {
auto iter = shared_texture_data_cache_.find(device.Get());
if (iter == shared_texture_data_cache_.end()) {
return nullptr;
}
return iter->second.memory;
}
void DawnSharedTextureHolder::MaybeCacheSharedTextureMemory(
const wgpu::Device& device,
const wgpu::SharedTextureMemory& memory) {
if (!memory) {
return;
}
// Return early if the STM is already cached.
if (auto cached = GetSharedTextureMemory(device)) {
CHECK_EQ(cached.Get(), memory.Get());
return;
}
SharedTextureData shared_texture_data;
shared_texture_data.memory = memory;
shared_texture_data_cache_.emplace(device.Get(),
std::move(shared_texture_data));
}
wgpu::Texture DawnSharedTextureHolder::GetCachedWGPUTexture(
const wgpu::Device& device,
wgpu::TextureUsage texture_usage) {
auto* texture_cache = GetWGPUTextureCache(device);
if (!texture_cache) {
return nullptr;
}
auto iter = texture_cache->find(texture_usage);
if (iter == texture_cache->end()) {
return nullptr;
}
return iter->second;
}
void DawnSharedTextureHolder::RemoveWGPUTextureFromCache(
const wgpu::Device& device,
const wgpu::Texture& texture) {
auto* texture_cache = GetWGPUTextureCache(device);
if (!texture_cache) {
return;
}
texture_cache->erase(texture.GetUsage());
}
void DawnSharedTextureHolder::MaybeCacheWGPUTexture(
const wgpu::Device& device,
const wgpu::Texture& texture) {
if (!texture) {
return;
}
auto* texture_cache = GetWGPUTextureCache(device);
if (!texture_cache) {
return;
}
// Determine whether `texture` needs to be cached.
auto texture_usage = texture.GetUsage();
auto [iter, _] = texture_cache->emplace(texture_usage, texture);
CHECK_EQ(texture.Get(), iter->second.Get());
}
void DawnSharedTextureHolder::DestroyWGPUTextureIfNotCached(
const wgpu::Device& device,
const wgpu::Texture& texture) {
if (auto cached_texture = GetCachedWGPUTexture(device, texture.GetUsage())) {
CHECK_EQ(cached_texture.Get(), texture.Get());
return;
}
texture.Destroy();
}
void DawnSharedTextureHolder::EraseDataIfDeviceLost() {
// Clear out any cached SharedTextureMemory instances for which the
// associated Device has been lost. This both saves memory and more
// importantly ensures that a new SharedTextureMemory instance will be
// created if another Device occupies the same memory as a previously-used,
// now-lost Device.
std::vector<WGPUDevice> devices_to_erase;
for (auto& [wgpu_device, shared_texture_data] : shared_texture_data_cache_) {
if (!shared_texture_data.memory.IsDeviceLost()) {
continue;
}
devices_to_erase.push_back(wgpu_device);
}
for (const auto& wgpu_device : devices_to_erase) {
shared_texture_data_cache_.erase(wgpu_device);
}
}
} // namespace gpu