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 "src/gpu/GrContextPriv.h"
10 #include "tools/gpu/GrContextFactory.h"
11 #ifdef SK_GL
12 #include "tools/gpu/gl/GLTestContext.h"
13 #endif
14
15 #if SK_ANGLE
16 #include "tools/gpu/gl/angle/GLTestContext_angle.h"
17 #endif
18 #include "tools/gpu/gl/command_buffer/GLTestContext_command_buffer.h"
19 #ifdef SK_VULKAN
20 #include "tools/gpu/vk/VkTestContext.h"
21 #endif
22 #ifdef SK_METAL
23 #include "tools/gpu/mtl/MtlTestContext.h"
24 #endif
25 #ifdef SK_DIRECT3D
26 #include "tools/gpu/d3d/D3DTestContext.h"
27 #endif
28 #ifdef SK_DAWN
29 #include "tools/gpu/dawn/DawnTestContext.h"
30 #endif
31 #include "src/gpu/GrCaps.h"
32 #include "src/gpu/gl/GrGLGpu.h"
33 #include "tools/gpu/mock/MockTestContext.h"
34
35 #if defined(SK_BUILD_FOR_WIN) && defined(SK_ENABLE_DISCRETE_GPU)
36 extern "C" {
37 // NVIDIA documents that the presence and value of this symbol programmatically enable the high
38 // performance GPU in laptops with switchable graphics.
39 // https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm
40 // From testing, including this symbol, even if it is set to 0, we still get the NVIDIA GPU.
41 _declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
42
43 // AMD has a similar mechanism, although I don't have an AMD laptop, so this is untested.
44 // https://community.amd.com/thread/169965
45 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
46 }
47 #endif
48
49 namespace sk_gpu_test {
GrContextFactory()50 GrContextFactory::GrContextFactory() { }
51
GrContextFactory(const GrContextOptions & opts)52 GrContextFactory::GrContextFactory(const GrContextOptions& opts)
53 : fGlobalOptions(opts) {
54 }
55
~GrContextFactory()56 GrContextFactory::~GrContextFactory() {
57 this->destroyContexts();
58 }
59
destroyContexts()60 void GrContextFactory::destroyContexts() {
61 // We must delete the test contexts in reverse order so that any child context is finished and
62 // deleted before a parent context. This relies on the fact that when we make a new context we
63 // append it to the end of fContexts array.
64 // TODO: Look into keeping a dependency dag for contexts and deletion order
65 for (int i = fContexts.count() - 1; i >= 0; --i) {
66 Context& context = fContexts[i];
67 SkScopeExit restore(nullptr);
68 if (context.fTestContext) {
69 restore = context.fTestContext->makeCurrentAndAutoRestore();
70 }
71 if (!context.fGrContext->unique()) {
72 context.fGrContext->releaseResourcesAndAbandonContext();
73 context.fAbandoned = true;
74 }
75 context.fGrContext->unref();
76 delete context.fTestContext;
77 }
78 fContexts.reset();
79 }
80
abandonContexts()81 void GrContextFactory::abandonContexts() {
82 // We must abandon the test contexts in reverse order so that any child context is finished and
83 // abandoned before a parent context. This relies on the fact that when we make a new context we
84 // append it to the end of fContexts array.
85 // TODO: Look into keeping a dependency dag for contexts and deletion order
86 for (int i = fContexts.count() - 1; i >= 0; --i) {
87 Context& context = fContexts[i];
88 if (!context.fAbandoned) {
89 if (context.fTestContext) {
90 auto restore = context.fTestContext->makeCurrentAndAutoRestore();
91 context.fTestContext->testAbandon();
92 }
93 bool requiresEarlyAbandon = (context.fGrContext->backend() == GrBackendApi::kVulkan);
94 if (requiresEarlyAbandon) {
95 context.fGrContext->abandonContext();
96 }
97 if (context.fTestContext) {
98 delete(context.fTestContext);
99 context.fTestContext = nullptr;
100 }
101 if (!requiresEarlyAbandon) {
102 context.fGrContext->abandonContext();
103 }
104 context.fAbandoned = true;
105 }
106 }
107 }
108
releaseResourcesAndAbandonContexts()109 void GrContextFactory::releaseResourcesAndAbandonContexts() {
110 // We must abandon the test contexts in reverse order so that any child context is finished and
111 // abandoned before a parent context. This relies on the fact that when we make a new context we
112 // append it to the end of fContexts array.
113 // TODO: Look into keeping a dependency dag for contexts and deletion order
114 for (int i = fContexts.count() - 1; i >= 0; --i) {
115 Context& context = fContexts[i];
116 SkScopeExit restore(nullptr);
117 if (!context.fAbandoned) {
118 if (context.fTestContext) {
119 restore = context.fTestContext->makeCurrentAndAutoRestore();
120 }
121 context.fGrContext->releaseResourcesAndAbandonContext();
122 if (context.fTestContext) {
123 delete context.fTestContext;
124 context.fTestContext = nullptr;
125 }
126 context.fAbandoned = true;
127 }
128 }
129 }
130
get(ContextType type,ContextOverrides overrides)131 GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
132 return this->getContextInfo(type, overrides).grContext();
133 }
134
getContextInfoInternal(ContextType type,ContextOverrides overrides,GrContext * shareContext,uint32_t shareIndex)135 ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
136 GrContext* shareContext, uint32_t shareIndex) {
137 // (shareIndex != 0) -> (shareContext != nullptr)
138 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
139
140 for (int i = 0; i < fContexts.count(); ++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 master context
154 Context* masterContext = nullptr;
155 if (shareContext) {
156 for (int i = 0; i < fContexts.count(); ++i) {
157 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
158 masterContext = &fContexts[i];
159 break;
160 }
161 }
162 SkASSERT(masterContext && masterContext->fType == type);
163 }
164
165 std::unique_ptr<TestContext> testCtx;
166 GrBackendApi backend = ContextTypeBackend(type);
167 switch (backend) {
168 #ifdef SK_GL
169 case GrBackendApi::kOpenGL: {
170 GLTestContext* glShareContext = masterContext
171 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
172 GLTestContext* glCtx;
173 switch (type) {
174 case kGL_ContextType:
175 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
176 break;
177 case kGLES_ContextType:
178 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
179 break;
180 #if SK_ANGLE
181 case kANGLE_D3D9_ES2_ContextType:
182 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
183 glShareContext).release();
184 break;
185 case kANGLE_D3D11_ES2_ContextType:
186 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
187 glShareContext).release();
188 break;
189 case kANGLE_D3D11_ES3_ContextType:
190 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
191 glShareContext).release();
192 break;
193 case kANGLE_GL_ES2_ContextType:
194 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
195 glShareContext).release();
196 break;
197 case kANGLE_GL_ES3_ContextType:
198 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
199 glShareContext).release();
200 break;
201 #endif
202 #ifndef SK_NO_COMMAND_BUFFER
203 case kCommandBuffer_ContextType:
204 glCtx = CommandBufferGLTestContext::Create(glShareContext);
205 break;
206 #endif
207 default:
208 return ContextInfo();
209 }
210 if (!glCtx) {
211 return ContextInfo();
212 }
213 testCtx.reset(glCtx);
214 break;
215 }
216 #endif // SK_GL
217 #ifdef SK_VULKAN
218 case GrBackendApi::kVulkan: {
219 VkTestContext* vkSharedContext = masterContext
220 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
221 SkASSERT(kVulkan_ContextType == type);
222 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
223 if (!testCtx) {
224 return ContextInfo();
225 }
226
227 #ifdef SK_GL
228 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
229 // destruction will hang occaisonally. For some reason having an existing GL
230 // context fixes this.
231 if (!fSentinelGLContext) {
232 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
233 if (!fSentinelGLContext) {
234 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
235 }
236 }
237 #endif
238 break;
239 }
240 #endif
241 #ifdef SK_METAL
242 case GrBackendApi::kMetal: {
243 MtlTestContext* mtlSharedContext = masterContext
244 ? static_cast<MtlTestContext*>(masterContext->fTestContext) : nullptr;
245 SkASSERT(kMetal_ContextType == type);
246 testCtx.reset(CreatePlatformMtlTestContext(mtlSharedContext));
247 if (!testCtx) {
248 return ContextInfo();
249 }
250 break;
251 }
252 #endif
253 #ifdef SK_DIRECT3D
254 case GrBackendApi::kDirect3D: {
255 D3DTestContext* d3dSharedContext = masterContext
256 ? static_cast<D3DTestContext*>(masterContext->fTestContext) : nullptr;
257 SkASSERT(kDirect3D_ContextType == type);
258 testCtx.reset(CreatePlatformD3DTestContext(d3dSharedContext));
259 if (!testCtx) {
260 return ContextInfo();
261 }
262 break;
263 }
264 #endif
265 #ifdef SK_DAWN
266 case GrBackendApi::kDawn: {
267 DawnTestContext* dawnSharedContext = masterContext
268 ? static_cast<DawnTestContext*>(masterContext->fTestContext) : nullptr;
269 testCtx.reset(CreatePlatformDawnTestContext(dawnSharedContext));
270 if (!testCtx) {
271 return ContextInfo();
272 }
273 break;
274 }
275 #endif
276 case GrBackendApi::kMock: {
277 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
278 SkASSERT(kMock_ContextType == type);
279 testCtx.reset(CreateMockTestContext(sharedContext));
280 if (!testCtx) {
281 return ContextInfo();
282 }
283 break;
284 }
285 default:
286 return ContextInfo();
287 }
288
289 SkASSERT(testCtx && testCtx->backend() == backend);
290 GrContextOptions grOptions = fGlobalOptions;
291 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
292 grOptions.fAvoidStencilBuffers = true;
293 }
294 sk_sp<GrContext> grCtx;
295 {
296 auto restore = testCtx->makeCurrentAndAutoRestore();
297 grCtx = testCtx->makeGrContext(grOptions);
298 }
299 if (!grCtx.get()) {
300 return ContextInfo();
301 }
302
303 // We must always add new contexts by pushing to the back so that when we delete them we delete
304 // them in reverse order in which they were made.
305 Context& context = fContexts.push_back();
306 context.fBackend = backend;
307 context.fTestContext = testCtx.release();
308 context.fGrContext = SkRef(grCtx.get());
309 context.fType = type;
310 context.fOverrides = overrides;
311 context.fAbandoned = false;
312 context.fShareContext = shareContext;
313 context.fShareIndex = shareIndex;
314 context.fOptions = grOptions;
315 context.fTestContext->makeCurrent();
316 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
317 }
318
getContextInfo(ContextType type,ContextOverrides overrides)319 ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
320 return this->getContextInfoInternal(type, overrides, nullptr, 0);
321 }
322
getSharedContextInfo(GrContext * shareContext,uint32_t shareIndex)323 ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
324 SkASSERT(shareContext);
325 for (int i = 0; i < fContexts.count(); ++i) {
326 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
327 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
328 shareContext, shareIndex);
329 }
330 }
331
332 return ContextInfo();
333 }
334
335 } // namespace sk_gpu_test
336