xref: /aosp_15_r20/external/skia/include/gpu/graphite/dawn/DawnBackendContext.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef skgpu_graphite_DawnBackendContext_DEFINED
9 #define skgpu_graphite_DawnBackendContext_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 #include "webgpu/webgpu_cpp.h"  // NO_G3_REWRITE
13 
14 namespace skgpu::graphite {
15 
16 /**
17  * WebGPU needs to allow the main thread loop to run to detect GPU progress. Dawn native has a
18  * function wgpu::Instance::ProcessEvents, not (currently) present in WebGPU, that can be used to
19  * detect GPU progress.
20  *
21  * When compiling using Emscripten/WASM the -s ASYNCIFY option can be used to yield. E.g.:
22  *
23  * EM_ASYNC_JS(void, asyncSleep, (), {
24  *                 await new Promise((resolve, _) = > {
25  *                     setTimeout(resolve, 0);
26  *                 })
27  *             });
28  *
29  * WebGPUTickFunction(wgpu::Device&) { asyncSleep(); }
30  *
31  * If no DawnTickFunction is provided then the graphite::Context will be "non-yielding". This
32  * implies the following restrictions on the Context:
33  *
34  * 1) SyncToCpu::kYes is disallowed as a parameter to Context::submit.
35  * 2) The client must guarantee that GPU work has completed before destroying Context as Context
36  *    cannot await the work completion in its destructor. Context reports whether it is awaiting
37  *    GPU work completion via Context::hasUnfinishedGpuWork().
38  *
39  * Using a non-yielding Context makes it possible to build and run Graphite/Dawn on WebGPU without
40  * -s ASYNCIFY.
41  */
42 using DawnTickFunction = void(const wgpu::Instance& device);
43 
44 #if !defined(__EMSCRIPTEN__)
DawnNativeProcessEventsFunction(const wgpu::Instance & instance)45 SK_API inline void DawnNativeProcessEventsFunction(const wgpu::Instance& instance) {
46     instance.ProcessEvents();
47 }
48 #endif
49 
50 // The DawnBackendContext contains all of the base Dawn objects needed by the graphite Dawn
51 // backend. The client will create this object and pass it into the Context::MakeDawn factory call
52 // when setting up Skia.
53 struct SK_API DawnBackendContext {
54     wgpu::Instance fInstance;
55     wgpu::Device fDevice;
56     wgpu::Queue fQueue;
57     // See comment on DawnTickFunction.
58     DawnTickFunction* fTick =
59 #if defined(__EMSCRIPTEN__)
60             nullptr;
61 #else
62             DawnNativeProcessEventsFunction;
63 #endif
64 };
65 
66 } // namespace skgpu::graphite
67 
68 #endif // skgpu_graphite_DawnBackendContext_DEFINED
69