blob: f5520d2a2dbc1fde0f567030b996ae8f68920c3b [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"
11#include "tools/gpu/gl/GLTestContext.h"
djsollene4545212014-11-13 11:12:41 -080012
Mike Kleinc168a3a2016-11-14 14:53:13 +000013#if SK_ANGLE
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014 #include "tools/gpu/gl/angle/GLTestContext_angle.h"
Mike Kleinc168a3a2016-11-14 14:53:13 +000015#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "tools/gpu/gl/command_buffer/GLTestContext_command_buffer.h"
djsollen7e731082016-06-09 13:07:13 -070017#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "tools/gpu/vk/VkTestContext.h"
bsalomon7c62b472016-04-01 07:42:05 -070019#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040020#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/gpu/mtl/MtlTestContext.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040022#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrCaps.h"
24#include "src/gpu/gl/GrGLGpu.h"
25#include "tools/gpu/mock/MockTestContext.h"
djsollene4545212014-11-13 11:12:41 -080026
Mike Klein8f11d4d2018-01-24 12:42:55 -050027#if defined(SK_BUILD_FOR_WIN) && defined(SK_ENABLE_DISCRETE_GPU)
Brian Osman3f375d02016-12-28 11:19:22 -050028extern "C" {
29 // NVIDIA documents that the presence and value of this symbol programmatically enable the high
30 // performance GPU in laptops with switchable graphics.
31 // https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm
32 // From testing, including this symbol, even if it is set to 0, we still get the NVIDIA GPU.
33 _declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
34
35 // AMD has a similar mechanism, although I don't have an AMD laptop, so this is untested.
36 // https://community.amd.com/thread/169965
37 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
38}
39#endif
40
bsalomon3724e572016-03-30 18:56:19 -070041namespace sk_gpu_test {
kkinnunen34058002016-01-06 23:49:30 -080042GrContextFactory::GrContextFactory() { }
43
44GrContextFactory::GrContextFactory(const GrContextOptions& opts)
45 : fGlobalOptions(opts) {
46}
47
48GrContextFactory::~GrContextFactory() {
49 this->destroyContexts();
50}
51
52void GrContextFactory::destroyContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050053 // We must delete the test contexts in reverse order so that any child context is finished and
54 // deleted before a parent context. This relies on the fact that when we make a new context we
55 // append it to the end of fContexts array.
56 // TODO: Look into keeping a dependency dag for contexts and deletion order
57 for (int i = fContexts.count() - 1; i >= 0; --i) {
58 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -050059 SkScopeExit restore(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -070060 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050061 restore = context.fTestContext->makeCurrentAndAutoRestore();
kkinnunen34058002016-01-06 23:49:30 -080062 }
63 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070064 context.fGrContext->releaseResourcesAndAbandonContext();
65 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080066 }
67 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070068 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080069 }
70 fContexts.reset();
71}
72
73void GrContextFactory::abandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050074 // We must abandon the test contexts in reverse order so that any child context is finished and
75 // abandoned before a parent context. This relies on the fact that when we make a new context we
76 // append it to the end of fContexts array.
77 // TODO: Look into keeping a dependency dag for contexts and deletion order
78 for (int i = fContexts.count() - 1; i >= 0; --i) {
79 Context& context = fContexts[i];
bsalomondc0fcd42016-04-11 14:21:33 -070080 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070081 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050082 auto restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomon18a2f9d2016-05-11 10:09:18 -070083 context.fTestContext->testAbandon();
84 delete(context.fTestContext);
85 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070086 }
87 context.fGrContext->abandonContext();
88 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080089 }
kkinnunen34058002016-01-06 23:49:30 -080090 }
91}
92
bsalomon6e2aad42016-04-01 11:54:31 -070093void GrContextFactory::releaseResourcesAndAbandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050094 // We must abandon the test contexts in reverse order so that any child context is finished and
95 // abandoned before a parent context. This relies on the fact that when we make a new context we
96 // append it to the end of fContexts array.
97 // TODO: Look into keeping a dependency dag for contexts and deletion order
98 for (int i = fContexts.count() - 1; i >= 0; --i) {
99 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -0500100 SkScopeExit restore(nullptr);
bsalomondc0fcd42016-04-11 14:21:33 -0700101 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700102 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -0500103 restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomondc0fcd42016-04-11 14:21:33 -0700104 }
bsalomon6e2aad42016-04-01 11:54:31 -0700105 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700106 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -0700107 if (context.fTestContext) {
108 delete context.fTestContext;
109 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700110 }
bsalomon6e2aad42016-04-01 11:54:31 -0700111 }
112 }
113}
114
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400115GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
116 return this->getContextInfo(type, overrides).grContext();
117}
118
Brian Osman9eac2ea2017-02-24 14:51:44 -0500119ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
120 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500121 // (shareIndex != 0) -> (shareContext != nullptr)
122 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
123
djsollene4545212014-11-13 11:12:41 -0800124 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800125 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800126 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700127 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500128 context.fShareContext == shareContext &&
129 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700130 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700131 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400132 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
133 context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800134 }
135 }
Brian Osman60c774d2017-02-21 16:58:08 -0500136
137 // If we're trying to create a context in a share group, find the master context
138 Context* masterContext = nullptr;
139 if (shareContext) {
140 for (int i = 0; i < fContexts.count(); ++i) {
141 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
142 masterContext = &fContexts[i];
143 break;
144 }
145 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500146 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500147 }
148
Ben Wagner145dbcd2016-11-03 14:40:50 -0400149 std::unique_ptr<TestContext> testCtx;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400150 GrBackendApi backend = ContextTypeBackend(type);
bsalomondc0fcd42016-04-11 14:21:33 -0700151 switch (backend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400152 case GrBackendApi::kOpenGL: {
Brian Osman60c774d2017-02-21 16:58:08 -0500153 GLTestContext* glShareContext = masterContext
154 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700155 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700156 switch (type) {
157 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500158 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700159 break;
160 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500161 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700162 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000163#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700164 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500165 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
166 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700167 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700168 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500169 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
170 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700171 break;
172 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500173 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
174 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700175 break;
176 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500177 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
178 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700179 break;
180 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500181 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
182 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700183 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000184#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400185#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700186 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500187 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700188 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400189#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700190 default:
191 return ContextInfo();
192 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700193 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700194 return ContextInfo();
195 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700196 testCtx.reset(glCtx);
djsollene4545212014-11-13 11:12:41 -0800197 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700198 }
jvanvertha50e17a2015-08-12 12:19:36 -0700199#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400200 case GrBackendApi::kVulkan: {
Greg Daniel604b1972017-05-15 13:50:35 -0400201 VkTestContext* vkSharedContext = masterContext
202 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700203 SkASSERT(kVulkan_ContextType == type);
csmartdaltone812d492017-02-21 12:36:05 -0700204 if (ContextOverrides::kRequireNVPRSupport & overrides) {
bsalomondc0fcd42016-04-11 14:21:33 -0700205 return ContextInfo();
206 }
Greg Daniel604b1972017-05-15 13:50:35 -0400207 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700208 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700209 return ContextInfo();
210 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700211
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000212 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
213 // destruction will hang occaisonally. For some reason having an existing GL
214 // context fixes this.
215 if (!fSentinelGLContext) {
216 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
217 if (!fSentinelGLContext) {
218 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
219 }
220 }
bsalomondc0fcd42016-04-11 14:21:33 -0700221 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400222 }
jvanvertha50e17a2015-08-12 12:19:36 -0700223#endif
Greg Danielb76a72a2017-07-13 15:07:54 -0400224#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400225 case GrBackendApi::kMetal: {
Jim Van Vertha3407ab2019-03-15 15:22:39 -0400226 MtlTestContext* mtlSharedContext = masterContext
227 ? static_cast<MtlTestContext*>(masterContext->fTestContext) : nullptr;
228 SkASSERT(kMetal_ContextType == type);
229 testCtx.reset(CreatePlatformMtlTestContext(mtlSharedContext));
Greg Danielb76a72a2017-07-13 15:07:54 -0400230 if (!testCtx) {
231 return ContextInfo();
232 }
233 break;
234 }
235#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400236 case GrBackendApi::kMock: {
Brian Salomoncfe910d2017-07-06 16:40:18 -0400237 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
238 SkASSERT(kMock_ContextType == type);
239 if (ContextOverrides::kRequireNVPRSupport & overrides) {
240 return ContextInfo();
241 }
242 testCtx.reset(CreateMockTestContext(sharedContext));
243 if (!testCtx) {
244 return ContextInfo();
245 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400246 break;
247 }
bsalomondc0fcd42016-04-11 14:21:33 -0700248 default:
249 return ContextInfo();
250 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500251
bsalomon18a2f9d2016-05-11 10:09:18 -0700252 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700253 GrContextOptions grOptions = fGlobalOptions;
csmartdalton008b9d82017-02-22 12:00:42 -0700254 if (ContextOverrides::kDisableNVPR & overrides) {
255 grOptions.fSuppressPathRendering = true;
256 }
Eric Karl5c779752017-05-08 12:02:07 -0700257 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
258 grOptions.fAvoidStencilBuffers = true;
259 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500260 sk_sp<GrContext> grCtx;
261 {
262 auto restore = testCtx->makeCurrentAndAutoRestore();
263 grCtx = testCtx->makeGrContext(grOptions);
264 }
djsollene4545212014-11-13 11:12:41 -0800265 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800266 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800267 }
csmartdaltone812d492017-02-21 12:36:05 -0700268 if (ContextOverrides::kRequireNVPRSupport & overrides) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500269 if (!grCtx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800270 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700271 }
272 }
273
Greg Danielf0fe02f2018-01-16 11:20:41 -0500274 // We must always add new contexts by pushing to the back so that when we delete them we delete
275 // them in reverse order in which they were made.
kkinnunen34058002016-01-06 23:49:30 -0800276 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700277 context.fBackend = backend;
278 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800279 context.fGrContext = SkRef(grCtx.get());
280 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700281 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700282 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500283 context.fShareContext = shareContext;
284 context.fShareIndex = shareIndex;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400285 context.fOptions = grOptions;
Brian Salomon55ad7742017-11-17 09:25:23 -0500286 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400287 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800288}
Brian Osman60c774d2017-02-21 16:58:08 -0500289
Brian Osman9eac2ea2017-02-24 14:51:44 -0500290ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
291 return this->getContextInfoInternal(type, overrides, nullptr, 0);
292}
293
294ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
295 SkASSERT(shareContext);
296 for (int i = 0; i < fContexts.count(); ++i) {
297 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
298 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
299 shareContext, shareIndex);
300 }
301 }
302
303 return ContextInfo();
304}
305
bsalomon3724e572016-03-30 18:56:19 -0700306} // namespace sk_gpu_test