xref: /aosp_15_r20/external/skia/src/gpu/graphite/dawn/DawnSharedContext.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_DawnSharedContext_DEFINED
9 #define skgpu_graphite_DawnSharedContext_DEFINED
10 
11 #include "webgpu/webgpu_cpp.h"  // NO_G3_REWRITE
12 
13 #include "include/gpu/graphite/dawn/DawnBackendContext.h"
14 #include "src/gpu/graphite/SharedContext.h"
15 #include "src/gpu/graphite/dawn/DawnCaps.h"
16 
17 namespace skgpu::graphite {
18 
19 struct DawnBackendContext;
20 struct ContextOptions;
21 
22 class DawnSharedContext final : public SharedContext {
23 public:
24     static sk_sp<SharedContext> Make(const DawnBackendContext&, const ContextOptions&);
25     ~DawnSharedContext() override;
26 
27     std::unique_ptr<ResourceProvider> makeResourceProvider(SingleOwner*,
28                                                            uint32_t recorderID,
29                                                            size_t resourceBudget,
30                                                            bool avoidBufferAlloc) override;
31 
dawnCaps()32     const DawnCaps* dawnCaps() const { return static_cast<const DawnCaps*>(this->caps()); }
device()33     const wgpu::Device& device() const { return fDevice; }
queue()34     const wgpu::Queue& queue() const { return fQueue; }
noopFragment()35     const wgpu::ShaderModule& noopFragment() const { return fNoopFragment; }
36 
hasTick()37     bool hasTick() const { return fTick; }
38 
tick()39     void tick() const {
40         SkASSERT(this->hasTick());
41         fTick(fInstance);
42     }
43 
44     void deviceTick(Context*) override;
45 
46 private:
47     DawnSharedContext(const DawnBackendContext&,
48                       std::unique_ptr<const DawnCaps> caps,
49                       wgpu::ShaderModule noopFragment);
50 
51     wgpu::Instance     fInstance;
52     wgpu::Device       fDevice;
53     wgpu::Queue        fQueue;
54     DawnTickFunction*  fTick;
55     // A noop fragment shader, it is used to workaround dawn a validation error(dawn doesn't allow
56     // a pipeline with a color attachment but without a fragment shader).
57     wgpu::ShaderModule fNoopFragment;
58 };
59 
60 } // namespace skgpu::graphite
61 
62 #endif // skgpu_graphite_DawnSharedContext_DEFINED
63