File: dawn_shared_texture_cache.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (171 lines) | stat: -rw-r--r-- 5,180 bytes parent folder | download | duplicates (5)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 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_cache.h"

#include "base/check_op.h"

namespace gpu {

DawnSharedTextureCache::TextureMetadata::TextureMetadata(
    wgpu::TextureUsage usage,
    wgpu::TextureUsage internal_usage,
    const std::vector<wgpu::TextureFormat>& view_formats)
    : usage(usage),
      internal_usage(internal_usage),
      view_formats(view_formats) {}

DawnSharedTextureCache::TextureMetadata::~TextureMetadata() = default;

DawnSharedTextureCache::TextureMetadata::TextureMetadata(TextureMetadata&&) =
    default;
DawnSharedTextureCache::TextureMetadata&
DawnSharedTextureCache::TextureMetadata::operator=(TextureMetadata&&) = default;

DawnSharedTextureCache::SharedTextureData::SharedTextureData() = default;

DawnSharedTextureCache::SharedTextureData::~SharedTextureData() {
  for (auto& [_, texture] : texture_cache) {
    texture.Destroy();
  }
}

DawnSharedTextureCache::SharedTextureData::SharedTextureData(
    SharedTextureData&&) = default;
DawnSharedTextureCache::SharedTextureData&
DawnSharedTextureCache::SharedTextureData::operator=(SharedTextureData&&) =
    default;

DawnSharedTextureCache::DawnSharedTextureCache() = default;

DawnSharedTextureCache::~DawnSharedTextureCache() = default;

DawnSharedTextureCache::TextureCache* DawnSharedTextureCache::GetTextureCache(
    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 DawnSharedTextureCache::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.shared_texture_memory;
}

void DawnSharedTextureCache::MaybeCacheSharedTextureMemory(
    const wgpu::Device& device,
    const wgpu::SharedTextureMemory& shared_texture_memory) {
  if (!shared_texture_memory) {
    return;
  }

  // Return early if the STM is already cached.
  if (auto cached = GetSharedTextureMemory(device)) {
    CHECK_EQ(cached.Get(), shared_texture_memory.Get());
    return;
  }

  SharedTextureData shared_texture_data;
  shared_texture_data.shared_texture_memory = shared_texture_memory;
  shared_texture_data_cache_.emplace(device.Get(),
                                     std::move(shared_texture_data));
}

wgpu::Texture DawnSharedTextureCache::GetCachedWGPUTexture(
    const wgpu::Device& device,
    wgpu::TextureUsage usage,
    wgpu::TextureUsage internal_usage,
    const std::vector<wgpu::TextureFormat>& view_formats) {
  auto* texture_cache = GetTextureCache(device);
  if (!texture_cache) {
    return nullptr;
  }

  auto iter = texture_cache->find({usage, internal_usage, view_formats});
  if (iter == texture_cache->end()) {
    return nullptr;
  }

  return iter->second;
}

void DawnSharedTextureCache::RemoveWGPUTextureFromCache(
    const wgpu::Device& device,
    const wgpu::Texture& texture) {
  auto* texture_cache = GetTextureCache(device);
  if (!texture_cache) {
    return;
  }

  base::EraseIf(*texture_cache, [&](const auto& kv_pair) {
    return kv_pair.second.Get() == texture.Get();
  });
}

void DawnSharedTextureCache::MaybeCacheWGPUTexture(
    const wgpu::Device& device,
    const wgpu::Texture& texture,
    wgpu::TextureUsage usage,
    wgpu::TextureUsage internal_usage,
    const std::vector<wgpu::TextureFormat>& view_formats) {
  if (!texture) {
    return;
  }

  auto* texture_cache = GetTextureCache(device);
  if (!texture_cache) {
    return;
  }

  auto [iter, _] = texture_cache->try_emplace(
      TextureMetadata{usage, internal_usage, view_formats}, texture);
  CHECK_EQ(texture.Get(), iter->second.Get());
}

void DawnSharedTextureCache::DestroyWGPUTextureIfNotCached(
    const wgpu::Device& device,
    const wgpu::Texture& texture) {
  if (!texture) {
    return;
  }

  auto* texture_cache = GetTextureCache(device);
  if (texture_cache) {
    auto iter = std::ranges::find_if(*texture_cache, [&](const auto& kv_pair) {
      return kv_pair.second.Get() == texture.Get();
    });
    if (iter != texture_cache->end()) {
      return;
    }
  }

  texture.Destroy();
}

void DawnSharedTextureCache::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.shared_texture_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