Debian Codegraph @

tools/window/GraphiteDawnWindowContext.cpp

raw
1 /*
2 * Copyright 2022 Google Inc.
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 "tools/window/GraphiteDawnWindowContext.h"
9
10 #include "include/core/SkSurface.h"
11 #include "include/gpu/graphite/BackendTexture.h"
12 #include "include/gpu/graphite/Context.h"
13 #include "include/gpu/graphite/ContextOptions.h"
14 #include "include/gpu/graphite/GraphiteTypes.h"
15 #include "include/gpu/graphite/Recorder.h"
16 #include "include/gpu/graphite/Recording.h"
17 #include "include/gpu/graphite/Surface.h"
18 #include "include/gpu/graphite/dawn/DawnBackendContext.h"
19 #include "include/gpu/graphite/dawn/DawnGraphiteTypes.h"
20 #include "src/gpu/graphite/ContextOptionsPriv.h"
21 #include "tools/ToolUtils.h"
22 #include "tools/graphite/GraphiteToolUtils.h"
23 #include "tools/graphite/TestOptions.h"
24 #include "tools/graphite/dawn/GraphiteDawnToggles.h"
25 #include "tools/window/GraphiteDisplayParams.h"
26
27 #include "dawn/dawn_proc.h"
28
29 namespace skwindow::internal {
30
31 GraphiteDawnWindowContext::GraphiteDawnWindowContext(std::unique_ptr<const DisplayParams> params,
32 wgpu::TextureFormat surfaceFormat)
33 : WindowContext(std::move(params)), fSurfaceFormat(surfaceFormat) {
34 wgpu::InstanceDescriptor desc{};
35 // need for WaitAny with timeout > 0
36 static const auto kTimedWaitAny = wgpu::InstanceFeatureName::TimedWaitAny;
37 desc.requiredFeatureCount = 1;
38 desc.requiredFeatures = &kTimedWaitAny;
39 wgpu::DawnTogglesDescriptor togglesDesc = skiatest::graphite::GetInstanceToggles();
40 desc.nextInChain = &togglesDesc;
41 fInstance = std::make_unique<dawn::native::Instance>(&desc);
42 }
43
44 void GraphiteDawnWindowContext::initializeContext(int width, int height) {
45 #if defined(SK_GANESH)
46 SkASSERT(!fContext);
47 #endif
48
49 fWidth = width;
50 fHeight = height;
51
52 if (!this->onInitializeContext())
53 return;
54
55 SkASSERT(fDevice);
56 SkASSERT(fSurface);
57
58 skgpu::graphite::DawnBackendContext backendContext;
59 backendContext.fInstance = wgpu::Instance(fInstance->Get());
60 backendContext.fDevice = fDevice;
61 backendContext.fQueue = fDevice.GetQueue();
62
63 SkASSERT(fDisplayParams->graphiteTestOptions());
64 skwindow::GraphiteTestOptions opts = *fDisplayParams->graphiteTestOptions();
65
66 // Needed to make synchronous readPixels work:
67 opts.fPriv.fStoreContextRefInRecorder = true;
68 fDisplayParams =
69 GraphiteDisplayParamsBuilder(fDisplayParams.get()).graphiteTestOptions(opts).detach();
70
71 fGraphiteContext = skgpu::graphite::ContextFactory::MakeDawn(backendContext,
72 opts.fTestOptions.fContextOptions);
73 if (!fGraphiteContext) {
74 SkASSERT(false);
75 return;
76 }
77
78 fGraphiteRecorder = fGraphiteContext->makeRecorder(ToolUtils::CreateTestingRecorderOptions());
79 SkASSERT(fGraphiteRecorder);
80 }
81
82 GraphiteDawnWindowContext::~GraphiteDawnWindowContext() = default;
83
84 void GraphiteDawnWindowContext::destroyContext() {
85 if (!fDevice.Get()) {
86 return;
87 }
88
89 this->onDestroyContext();
90
91 fGraphiteRecorder = nullptr;
92 fGraphiteContext = nullptr;
93 fSurface = nullptr;
94 fDevice = nullptr;
95 }
96
97 sk_sp<SkSurface> GraphiteDawnWindowContext::getBackbufferSurface() {
98 wgpu::SurfaceTexture surfaceTexture;
99 fSurface.GetCurrentTexture(&surfaceTexture);
100 SkASSERT(surfaceTexture.texture);
101 auto texture = surfaceTexture.texture;
102
103 skgpu::graphite::DawnTextureInfo info(skgpu::graphite::SampleCount::k1,
104 skgpu::Mipmapped::kNo,
105 fSurfaceFormat,
106 texture.GetUsage(),
107 wgpu::TextureAspect::All);
108 auto backendTex = skgpu::graphite::BackendTextures::MakeDawn(texture.Get());
109 SkASSERT(this->graphiteRecorder());
110 auto surface = SkSurfaces::WrapBackendTexture(this->graphiteRecorder(),
111 backendTex,
112 fDisplayParams->colorSpace(),
113 &fDisplayParams->surfaceProps());
114 SkASSERT(surface);
115 return surface;
116 }
117
118 void GraphiteDawnWindowContext::onSwapBuffers() {
119 this->submitToGpu();
120 fSurface.Present();
121 }
122
123 void GraphiteDawnWindowContext::setDisplayParams(std::unique_ptr<const DisplayParams> params) {
124 this->destroyContext();
125 fDisplayParams = std::move(params);
126 this->initializeContext(fWidth, fHeight);
127 }
128
129 wgpu::Device GraphiteDawnWindowContext::createDevice(wgpu::BackendType type) {
130 DawnProcTable backendProcs = dawn::native::GetProcs();
131 dawnProcSetProcs(&backendProcs);
132
133 wgpu::RequestAdapterOptions adapterOptions;
134 adapterOptions.backendType = type;
135 adapterOptions.featureLevel =
136 type == wgpu::BackendType::OpenGL || type == wgpu::BackendType::OpenGLES
137 ? wgpu::FeatureLevel::Compatibility
138 : wgpu::FeatureLevel::Core;
139
140 wgpu::DawnTogglesDescriptor togglesDesc = skiatest::graphite::GetAdapterToggles();
141 adapterOptions.nextInChain = &togglesDesc;
142
143 std::vector<dawn::native::Adapter> adapters = fInstance->EnumerateAdapters(&adapterOptions);
144 if (adapters.empty()) {
145 return nullptr;
146 }
147
148 wgpu::Adapter adapter = adapters[0].Get();
149
150 std::vector<wgpu::FeatureName> features;
151 skiatest::graphite::AddPreferredFeatures(adapter, features);
152
153 wgpu::DeviceDescriptor deviceDescriptor;
154 deviceDescriptor.requiredFeatures = features.data();
155 deviceDescriptor.requiredFeatureCount = features.size();
156
157 wgpu::Limits limits = {};
158 adapter.GetLimits(&limits);
159 deviceDescriptor.requiredLimits = &limits;
160
161 deviceDescriptor.nextInChain = &togglesDesc;
162 deviceDescriptor.SetDeviceLostCallback(
163 wgpu::CallbackMode::AllowSpontaneous,
164 [](const wgpu::Device&, wgpu::DeviceLostReason reason, wgpu::StringView message) {
165 if (reason == wgpu::DeviceLostReason::Unknown ||
166 reason == wgpu::DeviceLostReason::FailedCreation) {
167 SK_ABORT("Device lost: %.*s\n", static_cast<int>(message.length), message.data);
168 }
169 });
170 deviceDescriptor.SetUncapturedErrorCallback(
171 [](const wgpu::Device&, wgpu::ErrorType, wgpu::StringView message) {
172 SkDebugf("Device error: %.*s\n", static_cast<int>(message.length), message.data);
173 SkASSERT(false);
174 });
175
176 wgpu::DawnTogglesDescriptor deviceTogglesDesc;
177
178 if (fDisplayParams->graphiteTestOptions()->fTestOptions.fDisableTintSymbolRenaming) {
179 static constexpr const char* kOptionalDeviceToggles[] = {
180 "disable_symbol_renaming",
181 };
182 deviceTogglesDesc.enabledToggleCount = std::size(kOptionalDeviceToggles);
183 deviceTogglesDesc.enabledToggles = kOptionalDeviceToggles;
184
185 // Insert the toggles descriptor ahead of any existing entries in the chain that might have
186 // been added above.
187 deviceTogglesDesc.nextInChain = deviceDescriptor.nextInChain;
188 deviceDescriptor.nextInChain = &deviceTogglesDesc;
189 }
190
191 auto device = adapter.CreateDevice(&deviceDescriptor);
192 if (!device) {
193 return nullptr;
194 }
195
196 return device;
197 }
198
199 void GraphiteDawnWindowContext::configureSurface() {
200 SkASSERT(fDevice);
201 SkASSERT(fSurface);
202
203 wgpu::SurfaceConfiguration surfaceConfig;
204 surfaceConfig.device = fDevice;
205 surfaceConfig.format = fSurfaceFormat;
206 surfaceConfig.usage = wgpu::TextureUsage::RenderAttachment |
207 wgpu::TextureUsage::TextureBinding |
208 wgpu::TextureUsage::CopySrc |
209 wgpu::TextureUsage::CopyDst;
210 surfaceConfig.width = fWidth;
211 surfaceConfig.height = fHeight;
212 surfaceConfig.presentMode =
213 fDisplayParams->disableVsync() ? wgpu::PresentMode::Immediate : wgpu::PresentMode::Fifo;
214 fSurface.Configure(&surfaceConfig);
215 }
216
217 } //namespace skwindow::internal