blob: d89314e18b6954e9d58ac93f175d8dc5e079a34c [file] [log] [blame]
bsalomon3724e572016-03-30 18:56:19 -07001
djsollene4545212014-11-13 11:12:41 -08002/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrContextPriv.h"
10#include "tools/gpu/GrContextFactory.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080011#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "tools/gpu/gl/GLTestContext.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080013#endif
djsollene4545212014-11-13 11:12:41 -080014
Mike Kleinc168a3a2016-11-14 14:53:13 +000015#if SK_ANGLE
Jim Van Verthb01e12b2020-02-18 14:34:38 -050016#include "tools/gpu/gl/angle/GLTestContext_angle.h"
Mike Kleinc168a3a2016-11-14 14:53:13 +000017#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "tools/gpu/gl/command_buffer/GLTestContext_command_buffer.h"
djsollen7e731082016-06-09 13:07:13 -070019#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "tools/gpu/vk/VkTestContext.h"
bsalomon7c62b472016-04-01 07:42:05 -070021#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040022#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/gpu/mtl/MtlTestContext.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040024#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -050025#ifdef SK_DIRECT3D
26#include "tools/gpu/d3d/D3DTestContext.h"
27#endif
Stephen White985741a2019-07-18 11:43:45 -040028#ifdef SK_DAWN
29#include "tools/gpu/dawn/DawnTestContext.h"
30#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrCaps.h"
Robert Phillips08ba0852019-05-22 20:23:43 +000032#include "src/gpu/gl/GrGLGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "tools/gpu/mock/MockTestContext.h"
djsollene4545212014-11-13 11:12:41 -080034
Mike Klein8f11d4d2018-01-24 12:42:55 -050035#if defined(SK_BUILD_FOR_WIN) && defined(SK_ENABLE_DISCRETE_GPU)
Brian Osman3f375d02016-12-28 11:19:22 -050036extern "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
bsalomon3724e572016-03-30 18:56:19 -070049namespace sk_gpu_test {
kkinnunen34058002016-01-06 23:49:30 -080050GrContextFactory::GrContextFactory() { }
51
52GrContextFactory::GrContextFactory(const GrContextOptions& opts)
53 : fGlobalOptions(opts) {
54}
55
56GrContextFactory::~GrContextFactory() {
57 this->destroyContexts();
58}
59
60void GrContextFactory::destroyContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050061 // 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];
Brian Salomon55ad7742017-11-17 09:25:23 -050067 SkScopeExit restore(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -070068 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050069 restore = context.fTestContext->makeCurrentAndAutoRestore();
kkinnunen34058002016-01-06 23:49:30 -080070 }
71 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070072 context.fGrContext->releaseResourcesAndAbandonContext();
73 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080074 }
75 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070076 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080077 }
78 fContexts.reset();
79}
80
81void GrContextFactory::abandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050082 // 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];
bsalomondc0fcd42016-04-11 14:21:33 -070088 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070089 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050090 auto restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomon18a2f9d2016-05-11 10:09:18 -070091 context.fTestContext->testAbandon();
Greg Danielf0e04f02019-12-04 15:17:54 -050092 }
93 bool requiresEarlyAbandon = (context.fGrContext->backend() == GrBackendApi::kVulkan);
94 if (requiresEarlyAbandon) {
95 context.fGrContext->abandonContext();
96 }
97 if (context.fTestContext) {
bsalomon18a2f9d2016-05-11 10:09:18 -070098 delete(context.fTestContext);
99 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700100 }
Greg Danielf0e04f02019-12-04 15:17:54 -0500101 if (!requiresEarlyAbandon) {
102 context.fGrContext->abandonContext();
103 }
bsalomondc0fcd42016-04-11 14:21:33 -0700104 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -0800105 }
kkinnunen34058002016-01-06 23:49:30 -0800106 }
107}
108
bsalomon6e2aad42016-04-01 11:54:31 -0700109void GrContextFactory::releaseResourcesAndAbandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -0500110 // 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];
Brian Salomon55ad7742017-11-17 09:25:23 -0500116 SkScopeExit restore(nullptr);
bsalomondc0fcd42016-04-11 14:21:33 -0700117 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700118 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -0500119 restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomondc0fcd42016-04-11 14:21:33 -0700120 }
bsalomon6e2aad42016-04-01 11:54:31 -0700121 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomon18a2f9d2016-05-11 10:09:18 -0700122 if (context.fTestContext) {
123 delete context.fTestContext;
124 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700125 }
Greg Danielf0e04f02019-12-04 15:17:54 -0500126 context.fAbandoned = true;
bsalomon6e2aad42016-04-01 11:54:31 -0700127 }
128 }
129}
130
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400131GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
132 return this->getContextInfo(type, overrides).grContext();
133}
134
Brian Osman9eac2ea2017-02-24 14:51:44 -0500135ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
136 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500137 // (shareIndex != 0) -> (shareContext != nullptr)
138 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
139
djsollene4545212014-11-13 11:12:41 -0800140 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800141 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800142 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700143 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500144 context.fShareContext == shareContext &&
145 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700146 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700147 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400148 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
149 context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800150 }
151 }
Brian Osman60c774d2017-02-21 16:58:08 -0500152
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 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500162 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500163 }
164
Ben Wagner145dbcd2016-11-03 14:40:50 -0400165 std::unique_ptr<TestContext> testCtx;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400166 GrBackendApi backend = ContextTypeBackend(type);
bsalomondc0fcd42016-04-11 14:21:33 -0700167 switch (backend) {
John Rosascoa9b348f2019-11-08 13:18:15 -0800168#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400169 case GrBackendApi::kOpenGL: {
Brian Osman60c774d2017-02-21 16:58:08 -0500170 GLTestContext* glShareContext = masterContext
171 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700172 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700173 switch (type) {
174 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500175 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700176 break;
177 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500178 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700179 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000180#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700181 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500182 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
183 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700184 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700185 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500186 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
187 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700188 break;
189 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500190 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
191 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700192 break;
193 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500194 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
195 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700196 break;
197 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500198 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
199 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700200 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000201#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400202#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700203 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500204 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700205 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400206#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700207 default:
208 return ContextInfo();
209 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700210 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700211 return ContextInfo();
212 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700213 testCtx.reset(glCtx);
djsollene4545212014-11-13 11:12:41 -0800214 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700215 }
John Rosascoa9b348f2019-11-08 13:18:15 -0800216#endif // SK_GL
jvanvertha50e17a2015-08-12 12:19:36 -0700217#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400218 case GrBackendApi::kVulkan: {
Greg Daniel604b1972017-05-15 13:50:35 -0400219 VkTestContext* vkSharedContext = masterContext
220 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700221 SkASSERT(kVulkan_ContextType == type);
Greg Daniel604b1972017-05-15 13:50:35 -0400222 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700223 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700224 return ContextInfo();
225 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700226
John Rosascoa9b348f2019-11-08 13:18:15 -0800227#ifdef SK_GL
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000228 // 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 }
John Rosascoa9b348f2019-11-08 13:18:15 -0800237#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700238 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400239 }
jvanvertha50e17a2015-08-12 12:19:36 -0700240#endif
Greg Danielb76a72a2017-07-13 15:07:54 -0400241#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400242 case GrBackendApi::kMetal: {
Jim Van Vertha3407ab2019-03-15 15:22:39 -0400243 MtlTestContext* mtlSharedContext = masterContext
244 ? static_cast<MtlTestContext*>(masterContext->fTestContext) : nullptr;
245 SkASSERT(kMetal_ContextType == type);
246 testCtx.reset(CreatePlatformMtlTestContext(mtlSharedContext));
Greg Danielb76a72a2017-07-13 15:07:54 -0400247 if (!testCtx) {
248 return ContextInfo();
249 }
250 break;
251 }
252#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500253#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
Stephen White985741a2019-07-18 11:43:45 -0400265#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
Greg Danielbdf12ad2018-10-12 09:31:11 -0400276 case GrBackendApi::kMock: {
Brian Salomoncfe910d2017-07-06 16:40:18 -0400277 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
278 SkASSERT(kMock_ContextType == type);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400279 testCtx.reset(CreateMockTestContext(sharedContext));
280 if (!testCtx) {
281 return ContextInfo();
282 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400283 break;
284 }
bsalomondc0fcd42016-04-11 14:21:33 -0700285 default:
286 return ContextInfo();
287 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500288
bsalomon18a2f9d2016-05-11 10:09:18 -0700289 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700290 GrContextOptions grOptions = fGlobalOptions;
Eric Karl5c779752017-05-08 12:02:07 -0700291 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
292 grOptions.fAvoidStencilBuffers = true;
293 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500294 sk_sp<GrContext> grCtx;
295 {
296 auto restore = testCtx->makeCurrentAndAutoRestore();
297 grCtx = testCtx->makeGrContext(grOptions);
298 }
djsollene4545212014-11-13 11:12:41 -0800299 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800300 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800301 }
kkinnunencfe62e32015-07-01 02:58:50 -0700302
Greg Danielf0fe02f2018-01-16 11:20:41 -0500303 // 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.
kkinnunen34058002016-01-06 23:49:30 -0800305 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700306 context.fBackend = backend;
307 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800308 context.fGrContext = SkRef(grCtx.get());
309 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700310 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700311 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500312 context.fShareContext = shareContext;
313 context.fShareIndex = shareIndex;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400314 context.fOptions = grOptions;
Brian Salomon55ad7742017-11-17 09:25:23 -0500315 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400316 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800317}
Brian Osman60c774d2017-02-21 16:58:08 -0500318
Brian Osman9eac2ea2017-02-24 14:51:44 -0500319ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
320 return this->getContextInfoInternal(type, overrides, nullptr, 0);
321}
322
323ContextInfo 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
bsalomon3724e572016-03-30 18:56:19 -0700335} // namespace sk_gpu_test