File: gpu_texture.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (320 lines) | stat: -rw-r--r-- 10,441 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/webgpu/gpu_texture.h"

#include "base/containers/heap_array.h"
#include "gpu/command_buffer/client/webgpu_interface.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_texture_descriptor.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_texture_view_descriptor.h"
#include "third_party/blink/renderer/core/html/canvas/canvas_rendering_context.h"
#include "third_party/blink/renderer/core/html/canvas/html_canvas_element.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_conversions.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_device.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_texture_usage.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_texture_view.h"
#include "third_party/blink/renderer/platform/graphics/accelerated_static_bitmap_image.h"
#include "third_party/blink/renderer/platform/graphics/canvas_resource_provider.h"
#include "third_party/blink/renderer/platform/graphics/gpu/shared_gpu_context.h"
#include "third_party/blink/renderer/platform/graphics/gpu/webgpu_mailbox_texture.h"
#include "third_party/blink/renderer/platform/graphics/gpu/webgpu_resource_provider_cache.h"

namespace blink {

namespace {

bool ConvertToDawn(const GPUTextureDescriptor* in,
                   wgpu::TextureDescriptor* out,
                   wgpu::TextureBindingViewDimensionDescriptor*
                       out_texture_binding_view_dimension,
                   std::string* label,
                   base::HeapArray<wgpu::TextureFormat>* view_formats,
                   GPUDevice* device,
                   ExceptionState& exception_state) {
  DCHECK(in);
  DCHECK(out);
  DCHECK(out_texture_binding_view_dimension);
  DCHECK(label);
  DCHECK(view_formats);
  DCHECK(device);

  *out = {};
  out->usage = static_cast<wgpu::TextureUsage>(in->usage());
  out->dimension = AsDawnEnum(in->dimension());
  out->format = AsDawnEnum(in->format());
  out->mipLevelCount = in->mipLevelCount();
  out->sampleCount = in->sampleCount();

  if (in->hasTextureBindingViewDimension()) {
    wgpu::TextureViewDimension texture_binding_view_dimension =
        AsDawnEnum(in->textureBindingViewDimension());
    if (texture_binding_view_dimension !=
        wgpu::TextureViewDimension::Undefined) {
      *out_texture_binding_view_dimension = {};
      out_texture_binding_view_dimension->textureBindingViewDimension =
          texture_binding_view_dimension;
      out->nextInChain = out_texture_binding_view_dimension;
    }
  }

  *label = in->label().Utf8();
  if (!label->empty()) {
    out->label = label->c_str();
  }

  *view_formats = AsDawnEnum<wgpu::TextureFormat>(in->viewFormats());
  out->viewFormatCount = in->viewFormats().size();
  out->viewFormats = view_formats->data();

  return ConvertToDawn(in->size(), &out->size, device, exception_state);
}

wgpu::TextureViewDescriptor AsDawnType(
    const GPUTextureViewDescriptor* webgpu_desc,
    std::string* label) {
  DCHECK(webgpu_desc);
  DCHECK(label);

  wgpu::TextureViewDescriptor dawn_desc = {};
  if (webgpu_desc->hasFormat()) {
    dawn_desc.format = AsDawnEnum(webgpu_desc->format());
  }
  if (webgpu_desc->hasDimension()) {
    dawn_desc.dimension = AsDawnEnum(webgpu_desc->dimension());
  }
  dawn_desc.baseMipLevel = webgpu_desc->baseMipLevel();
  if (webgpu_desc->hasMipLevelCount()) {
    dawn_desc.mipLevelCount = webgpu_desc->mipLevelCount();
  }
  dawn_desc.baseArrayLayer = webgpu_desc->baseArrayLayer();
  if (webgpu_desc->hasArrayLayerCount()) {
    dawn_desc.arrayLayerCount = webgpu_desc->arrayLayerCount();
  }
  dawn_desc.aspect = AsDawnEnum(webgpu_desc->aspect());
  *label = webgpu_desc->label().Utf8();
  if (!label->empty()) {
    dawn_desc.label = label->c_str();
  }
  if (webgpu_desc->hasUsage()) {
    dawn_desc.usage = static_cast<wgpu::TextureUsage>(webgpu_desc->usage());
  }

  return dawn_desc;
}

// Dawn represents `undefined` as the special uint32_t value (0xFFFF'FFFF).
// Blink must make sure that an actual value of 0xFFFF'FFFF coming in from JS
// is not treated as the special `undefined` value, so it injects an error in
// that case.
std::string ValidateTextureMipLevelAndArrayLayerCounts(
    const GPUTextureViewDescriptor* webgpu_desc) {
  DCHECK(webgpu_desc);

  if (webgpu_desc->hasMipLevelCount() &&
      webgpu_desc->mipLevelCount() == wgpu::kMipLevelCountUndefined) {
    std::ostringstream error;
    error << "mipLevelCount (" << webgpu_desc->mipLevelCount()
          << ") is too large when validating [GPUTextureViewDescriptor";
    if (!webgpu_desc->label().empty()) {
      error << " '" << webgpu_desc->label().Utf8() << "'";
    }
    error << "].";
    return error.str();
  }

  if (webgpu_desc->hasArrayLayerCount() &&
      webgpu_desc->arrayLayerCount() == wgpu::kArrayLayerCountUndefined) {
    std::ostringstream error;
    error << "arrayLayerCount (" << webgpu_desc->arrayLayerCount()
          << ") is too large when validating [GPUTextureViewDescriptor";
    if (!webgpu_desc->label().empty()) {
      error << " '" << webgpu_desc->label().Utf8() << "'";
    }
    error << "].";
    return error.str();
  }

  return std::string();
}

}  // anonymous namespace

// static
GPUTexture* GPUTexture::Create(GPUDevice* device,
                               const GPUTextureDescriptor* webgpu_desc,
                               ExceptionState& exception_state) {
  DCHECK(device);
  DCHECK(webgpu_desc);

  wgpu::TextureDescriptor dawn_desc;
  wgpu::TextureBindingViewDimensionDescriptor
      texture_binding_view_dimension_desc;

  std::string label;
  base::HeapArray<wgpu::TextureFormat> view_formats;
  if (!ConvertToDawn(webgpu_desc, &dawn_desc,
                     &texture_binding_view_dimension_desc, &label,
                     &view_formats, device, exception_state)) {
    return nullptr;
  }

  if (!device->ValidateTextureFormatUsage(webgpu_desc->format(),
                                          exception_state)) {
    return nullptr;
  }

  for (auto view_format : webgpu_desc->viewFormats()) {
    if (!device->ValidateTextureFormatUsage(view_format, exception_state)) {
      return nullptr;
    }
  }

  GPUTexture* texture = MakeGarbageCollected<GPUTexture>(
      device, device->GetHandle().CreateTexture(&dawn_desc),
      webgpu_desc->label());
  return texture;
}

GPUTexture* GPUTexture::Create(GPUDevice* device,
                               const wgpu::TextureDescriptor* desc) {
  DCHECK(device);
  DCHECK(desc);

  return MakeGarbageCollected<GPUTexture>(
      device, device->GetHandle().CreateTexture(desc),
      String::FromUTF8(desc->label));
}

// static
GPUTexture* GPUTexture::CreateError(GPUDevice* device,
                                    const wgpu::TextureDescriptor* desc) {
  DCHECK(device);
  DCHECK(desc);
  return MakeGarbageCollected<GPUTexture>(
      device, device->GetHandle().CreateErrorTexture(desc),
      String::FromUTF8(desc->label));
}

GPUTexture::GPUTexture(GPUDevice* device,
                       wgpu::Texture texture,
                       const String& label)
    : DawnObject<wgpu::Texture>(device, std::move(texture), label),
      dimension_(GetHandle().GetDimension()),
      format_(GetHandle().GetFormat()),
      usage_(GetHandle().GetUsage()) {}

GPUTexture::GPUTexture(GPUDevice* device,
                       wgpu::TextureFormat format,
                       wgpu::TextureUsage usage,
                       scoped_refptr<WebGPUMailboxTexture> mailbox_texture,
                       const String& label)
    : DawnObject<wgpu::Texture>(device, mailbox_texture->GetTexture(), label),
      format_(format),
      usage_(usage),
      mailbox_texture_(std::move(mailbox_texture)) {
  if (mailbox_texture_) {
    device_->TrackTextureWithMailbox(this);
  }

  // Mailbox textures are all 2d texture.
  dimension_ = wgpu::TextureDimension::e2D;
}

GPUTextureView* GPUTexture::createView(
    const GPUTextureViewDescriptor* webgpu_desc,
    ExceptionState& exception_state) {
  DCHECK(webgpu_desc);

  if (webgpu_desc->hasFormat() && !device()->ValidateTextureFormatUsage(
                                      webgpu_desc->format(), exception_state)) {
    return nullptr;
  }

  std::string error = ValidateTextureMipLevelAndArrayLayerCounts(webgpu_desc);
  if (!error.empty()) {
    device()->InjectError(wgpu::ErrorType::Validation, error.c_str());
    return MakeGarbageCollected<GPUTextureView>(
        device(), GetHandle().CreateErrorView(nullptr), String());
  }

  std::string label;
  wgpu::TextureViewDescriptor dawn_desc = AsDawnType(webgpu_desc, &label);
  GPUTextureView* view = MakeGarbageCollected<GPUTextureView>(
      device_, GetHandle().CreateView(&dawn_desc), webgpu_desc->label());
  return view;
}

GPUTexture::~GPUTexture() {
  DissociateMailbox();
}

void GPUTexture::destroy() {
  if (destroyed_) {
    return;
  }

  if (destroy_callback_) {
    std::move(destroy_callback_).Run();
  }

  if (mailbox_texture_) {
    DissociateMailbox();
    device_->UntrackTextureWithMailbox(this);
  }
  GetHandle().Destroy();
  destroyed_ = true;
}

uint32_t GPUTexture::width() const {
  return GetHandle().GetWidth();
}

uint32_t GPUTexture::height() const {
  return GetHandle().GetHeight();
}

uint32_t GPUTexture::depthOrArrayLayers() const {
  return GetHandle().GetDepthOrArrayLayers();
}

uint32_t GPUTexture::mipLevelCount() const {
  return GetHandle().GetMipLevelCount();
}

uint32_t GPUTexture::sampleCount() const {
  return GetHandle().GetSampleCount();
}

V8GPUTextureDimension GPUTexture::dimension() const {
  return FromDawnEnum(GetHandle().GetDimension());
}

V8GPUTextureFormat GPUTexture::format() const {
  return FromDawnEnum(GetHandle().GetFormat());
}

uint32_t GPUTexture::usage() const {
  return static_cast<uint32_t>(GetHandle().GetUsage());
}

void GPUTexture::DissociateMailbox() {
  if (mailbox_texture_) {
    mailbox_texture_->Dissociate();
    mailbox_texture_ = nullptr;
  }
}

scoped_refptr<WebGPUMailboxTexture> GPUTexture::GetMailboxTexture() {
  return mailbox_texture_;
}

void GPUTexture::SetBeforeDestroyCallback(base::OnceClosure callback) {
  destroy_callback_ = std::move(callback);
}

void GPUTexture::ClearBeforeDestroyCallback() {
  destroy_callback_.Reset();
}

}  // namespace blink