Debian Codegraph @

tools/ganesh/GrContextFactory.cpp

raw
1
2 /*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9 #include "tools/ganesh/GrContextFactory.h"
10
11 #include "include/gpu/ganesh/GrDirectContext.h"
12 #include "src/gpu/ganesh/GrDirectContextPriv.h"
13 #if defined(SK_GL)
14 #include "tools/ganesh/gl/GLTestContext.h"
15 #endif
16
17 #if defined(SK_ANGLE)
18 #include "tools/ganesh/gl/angle/GLTestContext_angle.h"
19 #endif
20 #if defined(SK_VULKAN)
21 #include "tools/ganesh/vk/VkTestContext.h"
22 #endif
23 #if defined(SK_METAL)
24 #include "tools/ganesh/mtl/MtlTestContext.h"
25 #endif
26 #if defined(SK_DIRECT3D)
27 #include "tools/ganesh/d3d/D3DTestContext.h"
28 #endif
29 #include "src/gpu/ganesh/GrCaps.h"
30 #include "tools/ganesh/mock/MockTestContext.h"
31
32 #if defined(SK_BUILD_FOR_WIN) && defined(SK_ENABLE_DISCRETE_GPU)
33 extern "C" {
34 // NVIDIA documents that the presence and value of this symbol programmatically enable the high
35 // performance GPU in laptops with switchable graphics.
36 // https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm
37 // From testing, including this symbol, even if it is set to 0, we still get the NVIDIA GPU.
38 _declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
39
40 // AMD has a similar mechanism, although I don't have an AMD laptop, so this is untested.
41 // https://community.amd.com/thread/169965
42 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
43 }
44 #endif
45
46 bool gCreateProtectedContext = false;
47
48 namespace sk_gpu_test {
49 GrContextFactory::GrContextFactory() {}
50
51 GrContextFactory::GrContextFactory(const GrContextOptions& opts)
52 : fGlobalOptions(opts) {}
53
54 GrContextFactory::~GrContextFactory() {
55 this->destroyContexts();
56 }
57
58 void GrContextFactory::destroyContexts() {
59 // We must delete the test contexts in reverse order so that any child context is finished and
60 // deleted before a parent context. This relies on the fact that when we make a new context we
61 // append it to the end of fContexts array.
62 // TODO: Look into keeping a dependency dag for contexts and deletion order
63 for (int i = fContexts.size() - 1; i >= 0; --i) {
64 Context& context = fContexts[i];
65 SkScopeExit restore(nullptr);
66 if (context.fTestContext) {
67 restore = context.fTestContext->makeCurrentAndAutoRestore();
68 }
69 if (!context.fGrContext->unique()) {
70 context.fGrContext->releaseResourcesAndAbandonContext();
71 context.fAbandoned = true;
72 }
73 context.fGrContext->unref();
74 delete context.fTestContext;
75 }
76 fContexts.clear();
77 }
78
79 void GrContextFactory::abandonContexts() {
80 // We must abandon the test contexts in reverse order so that any child context is finished and
81 // abandoned before a parent context. This relies on the fact that when we make a new context we
82 // append it to the end of fContexts array.
83 // TODO: Look into keeping a dependency dag for contexts and deletion order
84 for (int i = fContexts.size() - 1; i >= 0; --i) {
85 Context& context = fContexts[i];
86 if (!context.fAbandoned) {
87 if (context.fTestContext) {
88 auto restore = context.fTestContext->makeCurrentAndAutoRestore();
89 context.fTestContext->testAbandon();
90 }
91 GrBackendApi api = context.fGrContext->backend();
92 bool requiresEarlyAbandon = api == GrBackendApi::kVulkan;
93 if (requiresEarlyAbandon) {
94 context.fGrContext->abandonContext();
95 }
96 if (context.fTestContext) {
97 delete(context.fTestContext);
98 context.fTestContext = nullptr;
99 }
100 if (!requiresEarlyAbandon) {
101 context.fGrContext->abandonContext();
102 }
103 context.fAbandoned = true;
104 }
105 }
106 }
107
108 void GrContextFactory::releaseResourcesAndAbandonContexts() {
109 // We must abandon the test contexts in reverse order so that any child context is finished and
110 // abandoned before a parent context. This relies on the fact that when we make a new context we
111 // append it to the end of fContexts array.
112 // TODO: Look into keeping a dependency dag for contexts and deletion order
113 for (int i = fContexts.size() - 1; i >= 0; --i) {
114 Context& context = fContexts[i];
115 SkScopeExit restore(nullptr);
116 if (!context.fAbandoned) {
117 if (context.fTestContext) {
118 restore = context.fTestContext->makeCurrentAndAutoRestore();
119 }
120 context.fGrContext->releaseResourcesAndAbandonContext();
121 if (context.fTestContext) {
122 delete context.fTestContext;
123 context.fTestContext = nullptr;
124 }
125 context.fAbandoned = true;
126 }
127 }
128 }
129
130 GrDirectContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
131 return this->getContextInfo(type, overrides).directContext();
132 }
133
134 ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
135 GrDirectContext* shareContext,
136 uint32_t shareIndex) {
137 // (shareIndex != 0) -> (shareContext != nullptr)
138 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
139
140 for (int i = 0; i < fContexts.size(); ++i) {
141 Context& context = fContexts[i];
142 if (context.fType == type &&
143 context.fOverrides == overrides &&
144 context.fShareContext == shareContext &&
145 context.fShareIndex == shareIndex &&
146 !context.fAbandoned) {
147 context.fTestContext->makeCurrent();
148 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
149 context.fOptions);
150 }
151 }
152
153 // If we're trying to create a context in a share group, find the primary context
154 Context* primaryContext = nullptr;
155 if (shareContext) {
156 for (int i = 0; i < fContexts.size(); ++i) {
157 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
158 primaryContext = &fContexts[i];
159 break;
160 }
161 }
162 SkASSERT(primaryContext && primaryContext->fType == type);
163 }
164
165 std::unique_ptr<TestContext> testCtx;
166 GrBackendApi backend = skgpu::ganesh::ContextTypeBackend(type);
167 switch (backend) {
168 #ifdef SK_GL
169 case GrBackendApi::kOpenGL: {
170 GLTestContext* glShareContext = primaryContext
171 ? static_cast<GLTestContext*>(primaryContext->fTestContext) : nullptr;
172 GLTestContext* glCtx;
173 switch (type) {
174 case ContextType::kGL:
175 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
176 break;
177 case ContextType::kGLES:
178 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
179 break;
180 #if SK_ANGLE
181 case ContextType::kANGLE_D3D9_ES2:
182 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
183 glShareContext).release();
184 // Chrome will only run on D3D9 with NVIDIA for 2012 and earlier drivers.
185 // (<= 269.73). We get shader link failures when testing on recent drivers
186 // using this backend.
187 if (glCtx) {
188 GrGLDriverInfo info = GrGLGetDriverInfo(glCtx->gl());
189 if (info.fANGLEVendor == GrGLVendor::kNVIDIA) {
190 delete glCtx;
191 return ContextInfo();
192 }
193 }
194 break;
195 case ContextType::kANGLE_D3D11_ES2:
196 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
197 glShareContext).release();
198 break;
199 case ContextType::kANGLE_D3D11_ES3:
200 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
201 glShareContext).release();
202 break;
203 case ContextType::kANGLE_GL_ES2:
204 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
205 glShareContext).release();
206 break;
207 case ContextType::kANGLE_GL_ES3:
208 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
209 glShareContext).release();
210 break;
211 case ContextType::kANGLE_Metal_ES2:
212 glCtx = MakeANGLETestContext(ANGLEBackend::kMetal, ANGLEContextVersion::kES2,
213 glShareContext).release();
214 break;
215 case ContextType::kANGLE_Metal_ES3:
216 glCtx = MakeANGLETestContext(ANGLEBackend::kMetal, ANGLEContextVersion::kES3,
217 glShareContext).release();
218 break;
219 #endif
220 default:
221 return ContextInfo();
222 }
223 if (!glCtx) {
224 return ContextInfo();
225 }
226 if (glCtx->gl()->fStandard == kGLES_GrGLStandard &&
227 (overrides & ContextOverrides::kFakeGLESVersionAs2)) {
228 glCtx->overrideVersion("OpenGL ES 2.0", "OpenGL ES GLSL ES 1.00");
229 }
230 testCtx.reset(glCtx);
231 break;
232 }
233 #endif // SK_GL
234 #ifdef SK_VULKAN
235 case GrBackendApi::kVulkan: {
236 VkTestContext* vkSharedContext = primaryContext
237 ? static_cast<VkTestContext*>(primaryContext->fTestContext) : nullptr;
238 SkASSERT(ContextType::kVulkan == type);
239 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
240 if (!testCtx) {
241 return ContextInfo();
242 }
243 #ifdef SK_GL
244 // We previously had an issue where the VkDevice destruction would occasionally hang
245 // on systems with NVIDIA GPUs and having an existing GL context fixed it. Now (Feb
246 // 2022) we still need the GL context to keep Vulkan/TSAN bots from running incredibly
247 // slow. Perhaps this prevents repeated driver loading/unloading? Note that keeping
248 // a persistent VkTestContext around instead was tried and did not work.
249 if (!fSentinelGLContext) {
250 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
251 if (!fSentinelGLContext) {
252 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
253 }
254 }
255 #endif
256 break;
257 }
258 #endif
259 #ifdef SK_METAL
260 case GrBackendApi::kMetal: {
261 MtlTestContext* mtlSharedContext = primaryContext
262 ? static_cast<MtlTestContext*>(primaryContext->fTestContext) : nullptr;
263 SkASSERT(ContextType::kMetal == type);
264 testCtx.reset(CreatePlatformMtlTestContext(mtlSharedContext));
265 if (!testCtx) {
266 return ContextInfo();
267 }
268 break;
269 }
270 #endif
271 #ifdef SK_DIRECT3D
272 case GrBackendApi::kDirect3D: {
273 D3DTestContext* d3dSharedContext = primaryContext
274 ? static_cast<D3DTestContext*>(primaryContext->fTestContext) : nullptr;
275 SkASSERT(ContextType::kDirect3D == type);
276 testCtx.reset(CreatePlatformD3DTestContext(d3dSharedContext));
277 if (!testCtx) {
278 return ContextInfo();
279 }
280 break;
281 }
282 #endif
283 case GrBackendApi::kMock: {
284 TestContext* sharedContext = primaryContext ? primaryContext->fTestContext : nullptr;
285 SkASSERT(ContextType::kMock == type);
286 testCtx.reset(CreateMockTestContext(sharedContext));
287 if (!testCtx) {
288 return ContextInfo();
289 }
290 break;
291 }
292 default:
293 return ContextInfo();
294 }
295
296 SkASSERT(testCtx && testCtx->backend() == backend);
297 GrContextOptions grOptions = fGlobalOptions;
298 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
299 grOptions.fAvoidStencilBuffers = true;
300 }
301 if (ContextOverrides::kReducedShaders & overrides) {
302 grOptions.fReducedShaderVariations = true;
303 }
304 sk_sp<GrDirectContext> grCtx;
305 {
306 auto restore = testCtx->makeCurrentAndAutoRestore();
307 grCtx = testCtx->makeContext(grOptions);
308 }
309 if (!grCtx) {
310 return ContextInfo();
311 }
312
313 if (shareContext) {
314 SkASSERT(grCtx->directContextID() != shareContext->directContextID());
315 }
316
317 // We must always add new contexts by pushing to the back so that when we delete them we delete
318 // them in reverse order in which they were made.
319 Context& context = fContexts.push_back();
320 context.fBackend = backend;
321 context.fTestContext = testCtx.release();
322 context.fGrContext = SkRef(grCtx.get());
323 context.fType = type;
324 context.fOverrides = overrides;
325 context.fAbandoned = false;
326 context.fShareContext = shareContext;
327 context.fShareIndex = shareIndex;
328 context.fOptions = grOptions;
329 context.fTestContext->makeCurrent();
330 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
331 }
332
333 ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
334 return this->getContextInfoInternal(type, overrides, nullptr, 0);
335 }
336
337 ContextInfo GrContextFactory::getSharedContextInfo(GrDirectContext* shareContext,
338 uint32_t shareIndex) {
339 SkASSERT(shareContext);
340 for (int i = 0; i < fContexts.size(); ++i) {
341 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
342 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
343 shareContext, shareIndex);
344 }
345 }
346
347 return ContextInfo();
348 }
349
350 } // namespace sk_gpu_test