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 #include "src/gpu/graphite/dawn/DawnSharedContext.h"
9
10 #include "include/gpu/graphite/ContextOptions.h"
11 #include "include/gpu/graphite/dawn/DawnBackendContext.h"
12 #include "src/gpu/graphite/Log.h"
13 #include "src/gpu/graphite/dawn/DawnResourceProvider.h"
14
15 namespace skgpu::graphite {
16 namespace {
17
CreateNoopFragment(const wgpu::Device & device)18 wgpu::ShaderModule CreateNoopFragment(const wgpu::Device& device) {
19 wgpu::ShaderModuleWGSLDescriptor wgslDesc;
20 wgslDesc.code =
21 "@fragment\n"
22 "fn main() {}\n";
23 wgpu::ShaderModuleDescriptor smDesc;
24 smDesc.nextInChain = &wgslDesc;
25 smDesc.label = "no-op";
26 auto fsModule = device.CreateShaderModule(&smDesc);
27 return fsModule;
28 }
29
30 }
31
Make(const DawnBackendContext & backendContext,const ContextOptions & options)32 sk_sp<SharedContext> DawnSharedContext::Make(const DawnBackendContext& backendContext,
33 const ContextOptions& options) {
34 if (!backendContext.fDevice || !backendContext.fQueue) {
35 return {};
36 }
37
38 auto noopFragment = CreateNoopFragment(backendContext.fDevice);
39 if (!noopFragment) {
40 return {};
41 }
42
43 auto caps = std::make_unique<const DawnCaps>(backendContext, options);
44
45 return sk_sp<SharedContext>(new DawnSharedContext(backendContext,
46 std::move(caps),
47 std::move(noopFragment)));
48 }
49
DawnSharedContext(const DawnBackendContext & backendContext,std::unique_ptr<const DawnCaps> caps,wgpu::ShaderModule noopFragment)50 DawnSharedContext::DawnSharedContext(const DawnBackendContext& backendContext,
51 std::unique_ptr<const DawnCaps> caps,
52 wgpu::ShaderModule noopFragment)
53 : skgpu::graphite::SharedContext(std::move(caps), BackendApi::kDawn)
54 , fInstance(backendContext.fInstance)
55 , fDevice(backendContext.fDevice)
56 , fQueue(backendContext.fQueue)
57 , fTick(backendContext.fTick)
58 , fNoopFragment(std::move(noopFragment)) {}
59
~DawnSharedContext()60 DawnSharedContext::~DawnSharedContext() {
61 // need to clear out resources before any allocator is removed
62 this->globalCache()->deleteResources();
63 }
64
makeResourceProvider(SingleOwner * singleOwner,uint32_t recorderID,size_t resourceBudget)65 std::unique_ptr<ResourceProvider> DawnSharedContext::makeResourceProvider(
66 SingleOwner* singleOwner,
67 uint32_t recorderID,
68 size_t resourceBudget) {
69 return std::unique_ptr<ResourceProvider>(new DawnResourceProvider(this,
70 singleOwner,
71 recorderID,
72 resourceBudget));
73 }
74
75 } // namespace skgpu::graphite
76