File: gpu.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 (474 lines) | stat: -rw-r--r-- 19,552 bytes parent folder | download | duplicates (2)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright 2018 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.h"

#include <utility>

#include "base/feature_list.h"
#include "base/notreached.h"
#include "base/synchronization/waitable_event.h"
#include "gpu/command_buffer/client/webgpu_interface.h"
#include "gpu/config/gpu_finch_features.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/common/privacy_budget/identifiability_metric_builder.h"
#include "third_party/blink/public/common/privacy_budget/identifiability_study_settings.h"
#include "third_party/blink/public/common/privacy_budget/identifiable_token_builder.h"
#include "third_party/blink/public/common/thread_safe_browser_interface_broker_proxy.h"
#include "third_party/blink/public/mojom/gpu/gpu.mojom-blink.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-shared.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_graphics_context_3d_provider.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_request_adapter_options.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_texture_format.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/agent.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/execution_context/navigator_base.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_enum_conversions.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_adapter.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_buffer.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_supported_features.h"
#include "third_party/blink/renderer/modules/webgpu/string_utils.h"
#include "third_party/blink/renderer/modules/webgpu/wgsl_language_features.h"
#include "third_party/blink/renderer/platform/graphics/gpu/dawn_control_client_holder.h"
#include "third_party/blink/renderer/platform/graphics/gpu/webgpu_callback.h"
#include "third_party/blink/renderer/platform/graphics/web_graphics_context_3d_provider_util.h"
#include "third_party/blink/renderer/platform/heap/cross_thread_handle.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/thread_state.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/privacy_budget/identifiability_digest_helpers.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"

namespace blink {

namespace {

[[maybe_unused]] void AddConsoleWarning(ExecutionContext* execution_context,
                                        const char* message) {
  if (execution_context) {
    auto* console_message = MakeGarbageCollected<ConsoleMessage>(
        mojom::blink::ConsoleMessageSource::kRendering,
        mojom::blink::ConsoleMessageLevel::kWarning,
        StringFromASCIIAndUTF8(message));
    execution_context->AddConsoleMessage(console_message);
  }
}

wgpu::PowerPreference AsDawnType(V8GPUPowerPreference power_preference) {
  switch (power_preference.AsEnum()) {
    case V8GPUPowerPreference::Enum::kLowPower:
      return wgpu::PowerPreference::LowPower;
    case V8GPUPowerPreference::Enum::kHighPerformance:
      return wgpu::PowerPreference::HighPerformance;
  }
}

wgpu::FeatureLevel AsDawnFeatureLevel(const String& feature_level) {
  CHECK(feature_level == "core" || feature_level == "compatibility");

  if (feature_level == "compatibility") {
    return wgpu::FeatureLevel::Compatibility;
  }

  return wgpu::FeatureLevel::Core;
}

wgpu::RequestAdapterOptions AsDawnType(
    const GPURequestAdapterOptions* webgpu_options) {
  DCHECK(webgpu_options);

  wgpu::RequestAdapterOptions dawn_options;
  dawn_options.forceFallbackAdapter = webgpu_options->forceFallbackAdapter();

  if (RuntimeEnabledFeatures::WebGPUFeatureLevelEnabled()) {
    dawn_options.featureLevel =
        AsDawnFeatureLevel(webgpu_options->featureLevel());
  }

  if (webgpu_options->hasPowerPreference()) {
    dawn_options.powerPreference =
        AsDawnType(webgpu_options->powerPreference());
  }

  return dawn_options;
}

// Returns the execution context token given the context. Currently returning
// the WebGPU specific execution context token.
// TODO(dawn:549) Might be able to use ExecutionContextToken instead of WebGPU
//     specific execution context token if/when DocumentToken becomes a part of
//     ExecutionContextToken.
WebGPUExecutionContextToken GetExecutionContextToken(
    const ExecutionContext* execution_context) {
  // WebGPU only supports the following types of context tokens: DocumentTokens,
  // DedicatedWorkerTokens, SharedWorkerTokens, and ServiceWorkerTokens. The
  // token is sent to the GPU process so that it can be cross-referenced against
  // the browser process to get an isolation key for caching purposes.
  if (execution_context->IsDedicatedWorkerGlobalScope()) {
    return execution_context->GetExecutionContextToken()
        .GetAs<DedicatedWorkerToken>();
  }
  if (execution_context->IsSharedWorkerGlobalScope()) {
    return execution_context->GetExecutionContextToken()
        .GetAs<SharedWorkerToken>();
  }
  if (execution_context->IsServiceWorkerGlobalScope()) {
    return execution_context->GetExecutionContextToken()
        .GetAs<ServiceWorkerToken>();
  }
  if (execution_context->IsWindow()) {
    return To<LocalDOMWindow>(execution_context)->document()->Token();
  }
  NOTREACHED();
}

}  // anonymous namespace

// static
const char GPU::kSupplementName[] = "GPU";

// static
GPU* GPU::gpu(NavigatorBase& navigator) {
  GPU* gpu = Supplement<NavigatorBase>::From<GPU>(navigator);
  if (!gpu) {
    gpu = MakeGarbageCollected<GPU>(navigator);
    ProvideTo(navigator, gpu);
  }
  return gpu;
}

GPU::GPU(NavigatorBase& navigator)
    : Supplement<NavigatorBase>(navigator),
      ExecutionContextLifecycleObserver(navigator.GetExecutionContext()),
      wgsl_language_features_(MakeGarbageCollected<WGSLLanguageFeatures>(
          GatherWGSLLanguageFeatures())),
      mappable_buffer_handles_(
          base::MakeRefCounted<BoxedMappableWGPUBufferHandles>()) {}

GPU::~GPU() = default;

WGSLLanguageFeatures* GPU::wgslLanguageFeatures() const {
  return wgsl_language_features_.Get();
}

void GPU::Trace(Visitor* visitor) const {
  ScriptWrappable::Trace(visitor);
  Supplement<NavigatorBase>::Trace(visitor);
  ExecutionContextLifecycleObserver::Trace(visitor);
  visitor->Trace(mappable_buffers_);
  visitor->Trace(wgsl_language_features_);
}

void GPU::ContextDestroyed() {
  if (!dawn_control_client_) {
    return;
  }
  // Ensure all DOMArrayBuffers backed by shared memory are detached before
  // the WebGPU command buffer and transfer buffers are destroyed.
  // This is necessary because we will free the shmem backings, and some
  // short amount of JS can still execute after the ContextDestroyed event
  // is received.
  if (!mappable_buffers_.empty()) {
    v8::Isolate* isolate = GetExecutionContext()->GetIsolate();
    v8::HandleScope scope(isolate);
    for (GPUBuffer* buffer : mappable_buffers_) {
      buffer->DetachMappedArrayBuffers(isolate);
    }
  }
  // GPUBuffer::~GPUBuffer and GPUBuffer::destroy will remove wgpu::Buffers from
  // |mappable_buffer_handles_|.
  // However, there may be GPUBuffers that were removed from mappable_buffers_
  // for which ~GPUBuffer has not run yet. These GPUBuffers and their
  // DOMArrayBuffer mappings are no longer reachable from JS, so we don't need
  // to detach them, but we do need to eagerly destroy the wgpu::Buffer so that
  // its shared memory is freed before the context is completely destroyed.
  mappable_buffer_handles_->ClearAndDestroyAll();
  dawn_control_client_->Destroy();
}

void GPU::OnRequestAdapterCallback(
    ScriptState* script_state,
    const GPURequestAdapterOptions* options,
    ScriptPromiseResolver<IDLNullable<GPUAdapter>>* resolver,
    wgpu::RequestAdapterStatus status,
    wgpu::Adapter adapter,
    wgpu::StringView error_message) {
  GPUAdapter* gpu_adapter = nullptr;
  switch (status) {
    case wgpu::RequestAdapterStatus::Success:
      gpu_adapter = MakeGarbageCollected<GPUAdapter>(
          this, std::move(adapter), dawn_control_client_, options);
      break;

    // Note: requestAdapter never rejects, but we print a console warning if
    // there are error messages.
    case wgpu::RequestAdapterStatus::Unavailable:
    case wgpu::RequestAdapterStatus::Error:
    case wgpu::RequestAdapterStatus::CallbackCancelled:
      break;
  }
  if (error_message.length != 0) {
    ExecutionContext* execution_context = ExecutionContext::From(script_state);
    auto* console_message = MakeGarbageCollected<ConsoleMessage>(
        mojom::blink::ConsoleMessageSource::kRendering,
        mojom::blink::ConsoleMessageLevel::kWarning,
        StringFromASCIIAndUTF8(error_message));
    execution_context->AddConsoleMessage(console_message);
  }
  RecordAdapterForIdentifiability(script_state, options, gpu_adapter);
  resolver->Resolve(gpu_adapter);
}

void GPU::RecordAdapterForIdentifiability(
    ScriptState* script_state,
    const GPURequestAdapterOptions* options,
    GPUAdapter* adapter) const {
  constexpr IdentifiableSurface::Type type =
      IdentifiableSurface::Type::kGPU_RequestAdapter;
  if (!IdentifiabilityStudySettings::Get()->ShouldSampleType(type))
    return;
  ExecutionContext* context = GetExecutionContext();
  if (!context)
    return;

  IdentifiableTokenBuilder input_builder;
  if (options && options->hasPowerPreference()) {
    input_builder.AddToken(IdentifiabilityBenignStringToken(
        options->powerPreference().AsString()));
  }
  const auto surface =
      IdentifiableSurface::FromTypeAndToken(type, input_builder.GetToken());

  IdentifiableTokenBuilder output_builder;
  if (adapter) {
    for (const auto& feature : adapter->features()->FeatureNameSet()) {
      output_builder.AddToken(IdentifiabilityBenignStringToken(feature));
    }
  }

  IdentifiabilityMetricBuilder(context->UkmSourceID())
      .Add(surface, output_builder.GetToken())
      .Record(context->UkmRecorder());
}

std::unique_ptr<WebGraphicsContext3DProvider> CheckContextProvider(
    const KURL& url,
    std::unique_ptr<WebGraphicsContext3DProvider> context_provider) {
  // Note that we check for API blocking *after* creating the context. This is
  // because context creation synchronizes against GpuProcessHost lifetime in
  // the browser process, and GpuProcessHost destruction is what updates API
  // blocking state on a GPU process crash. See https://crbug.com/1215907#c10
  // for more details.
  bool blocked = true;
  mojo::Remote<mojom::blink::GpuDataManager> gpu_data_manager;
  Platform::Current()->GetBrowserInterfaceBroker()->GetInterface(
      gpu_data_manager.BindNewPipeAndPassReceiver());
  gpu_data_manager->Are3DAPIsBlockedForUrl(url, &blocked);
  if (blocked) {
    return nullptr;
  }

  // TODO(kainino): we will need a better way of accessing the GPU interface
  // from multiple threads than BindToCurrentSequence et al.
  if (context_provider && !context_provider->BindToCurrentSequence()) {
    // TODO(crbug.com/973017): Collect GPU info and surface context creation
    // error.
    return nullptr;
  }
  return context_provider;
}

void GPU::RequestAdapterImpl(
    ScriptState* script_state,
    const GPURequestAdapterOptions* options,
    ScriptPromiseResolver<IDLNullable<GPUAdapter>>* resolver) {
  ExecutionContext* execution_context = ExecutionContext::From(script_state);

  // Validate that the featureLevel is an allowed feature level string value. If
  // not return a null adapter. This logic will evolve as feature levels are
  // added in the future.
  if (options->featureLevel() != "core" &&
      options->featureLevel() != "compatibility") {
    OnRequestAdapterCallback(script_state, options, resolver,
                             wgpu::RequestAdapterStatus::Error, nullptr,
                             "Unknown feature level");
    return;
  }

  if (!dawn_control_client_ || dawn_control_client_->IsContextLost()) {
    dawn_control_client_initialized_callbacks_.push_back(WTF::BindOnce(
        [](GPU* gpu, ScriptState* script_state,
           const GPURequestAdapterOptions* options,
           ScriptPromiseResolver<IDLNullable<GPUAdapter>>* resolver) {
          if (gpu->dawn_control_client_ &&
              !gpu->dawn_control_client_->IsContextLost()) {
            gpu->RequestAdapterImpl(script_state, options, resolver);
          } else {
            // Failed to create context provider, won't be able to request
            // adapter
            // TODO(crbug.com/973017): Collect GPU info and surface context
            // creation error.
            gpu->OnRequestAdapterCallback(
                script_state, options, resolver,
                wgpu::RequestAdapterStatus::Error, nullptr,
                "Failed to create WebGPU Context Provider");
          }
        },
        WrapPersistent(this), WrapPersistent(script_state),
        WrapPersistent(options), WrapPersistent(resolver)));

    // Returning since the task to create the control client from a previous
    // call to EnsureDawnControlClientInitialized should be already running
    if (dawn_control_client_initialized_callbacks_.size() > 1) {
      return;
    }

    CreateWebGPUGraphicsContext3DProviderAsync(
        execution_context->Url(),
        execution_context->GetTaskRunner(TaskType::kWebGPU),
        CrossThreadBindOnce(
            [](CrossThreadHandle<GPU> gpu_handle,
               CrossThreadHandle<ExecutionContext> execution_context_handle,
               std::unique_ptr<WebGraphicsContext3DProvider> context_provider) {
              auto unwrap_gpu = MakeUnwrappingCrossThreadHandle(gpu_handle);
              auto unwrap_execution_context =
                  MakeUnwrappingCrossThreadHandle(execution_context_handle);
              if (!unwrap_gpu || !unwrap_execution_context) {
                return;
              }
              auto* gpu = unwrap_gpu.GetOnCreationThread();
              auto* execution_context =
                  unwrap_execution_context.GetOnCreationThread();
              const KURL& url = execution_context->Url();
              context_provider =
                  CheckContextProvider(url, std::move(context_provider));
              if (context_provider) {
                context_provider->WebGPUInterface()
                    ->SetWebGPUExecutionContextToken(
                        GetExecutionContextToken(execution_context));

                // Make a new DawnControlClientHolder with the context provider
                // we just made and set the lost context callback
                gpu->dawn_control_client_ = DawnControlClientHolder::Create(
                    std::move(context_provider),
                    execution_context->GetTaskRunner(TaskType::kWebGPU));
              }

              WTF::Vector<base::OnceCallback<void()>> callbacks =
                  std::move(gpu->dawn_control_client_initialized_callbacks_);
              for (auto& callback : callbacks) {
                std::move(callback).Run();
              }
            },
            MakeCrossThreadHandle(this),
            MakeCrossThreadHandle(execution_context)));
    return;
  }

  DCHECK_NE(dawn_control_client_, nullptr);

#if BUILDFLAG(IS_WIN)
  // TODO(crbug.com/369219127): Chrome always uses the same GPU adapter that's
  // been allocated for other Chrome workloads on Windows, which for laptops is
  // generally the integrated graphics card, due to the power usage aspect (ie:
  // power saving).
  if (options->hasPowerPreference()) {
    AddConsoleWarning(
        execution_context,
        "The powerPreference option is currently ignored when calling "
        "requestAdapter() on Windows. See https://crbug.com/369219127");
  }
#endif

  if (options->featureLevel() == "compatibility" &&
      !RuntimeEnabledFeatures::WebGPUCompatibilityModeEnabled()) {
    AddConsoleWarning(
        execution_context,
        "Beware! featureLevel was set to \"compatibility\", but this request "
        "is being ignored. Compatibility restrictions will start being "
        "enforced as soon as Chromium ships Compatibility Mode, potentially "
        "breaking this webpage. See "
        "https://github.com/gpuweb/gpuweb/issues/4266");
  }

  wgpu::RequestAdapterOptions dawn_options = AsDawnType(options);
  auto* callback = MakeWGPUOnceCallback(resolver->WrapCallbackInScriptScope(
      WTF::BindOnce(&GPU::OnRequestAdapterCallback, WrapPersistent(this),
                    WrapPersistent(script_state), WrapPersistent(options))));

  dawn_control_client_->GetWGPUInstance().RequestAdapter(
      &dawn_options, wgpu::CallbackMode::AllowSpontaneous,
      callback->UnboundCallback(), callback->AsUserdata());
  dawn_control_client_->EnsureFlush(
      *execution_context->GetAgent()->event_loop());

  UseCounter::Count(execution_context, WebFeature::kWebGPURequestAdapter);
}

ScriptPromise<IDLNullable<GPUAdapter>> GPU::requestAdapter(
    ScriptState* script_state,
    const GPURequestAdapterOptions* options) {
  // Remind developers when they are using WebGPU on unsupported platforms.
  ExecutionContext* execution_context = GetExecutionContext();
  if (execution_context &&
      !base::FeatureList::IsEnabled(features::kWebGPUService)) {
    execution_context->AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>(
        mojom::blink::ConsoleMessageSource::kJavaScript,
        mojom::blink::ConsoleMessageLevel::kInfo,
        "WebGPU is experimental on this platform. See "
        "https://github.com/gpuweb/gpuweb/wiki/"
        "Implementation-Status#implementation-status"));
  }

  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<IDLNullable<GPUAdapter>>>(
          script_state);
  auto promise = resolver->Promise();
  RequestAdapterImpl(script_state, options, resolver);
  return promise;
}

V8GPUTextureFormat GPU::getPreferredCanvasFormat() {
  return FromDawnEnum(GetPreferredCanvasFormat());
}

wgpu::TextureFormat GPU::GetPreferredCanvasFormat() {
#if BUILDFLAG(IS_ANDROID)
  return wgpu::TextureFormat::RGBA8Unorm;
#else
  return wgpu::TextureFormat::BGRA8Unorm;
#endif
}

void GPU::TrackMappableBuffer(GPUBuffer* buffer) {
  mappable_buffers_.insert(buffer);
  mappable_buffer_handles_->insert(buffer->GetHandle());
}

void GPU::UntrackMappableBuffer(GPUBuffer* buffer) {
  mappable_buffers_.erase(buffer);
  mappable_buffer_handles_->erase(buffer->GetHandle());
}

void BoxedMappableWGPUBufferHandles::ClearAndDestroyAll() {
  for (const wgpu::Buffer& b : contents_) {
    b.Destroy();
  }
  contents_.clear();
}

void GPU::SetDawnControlClientHolderForTesting(
    scoped_refptr<DawnControlClientHolder> dawn_control_client) {
  dawn_control_client_ = std::move(dawn_control_client);
}

}  // namespace blink