blob: c6cceb484307f871497c4b56b183ddd5ca2506e4 [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
9#include "GrContextFactory.h"
bsalomon273c0f52016-03-31 10:59:06 -070010#include "gl/GLTestContext.h"
djsollene4545212014-11-13 11:12:41 -080011
Mike Kleinc168a3a2016-11-14 14:53:13 +000012#if SK_ANGLE
13 #include "gl/angle/GLTestContext_angle.h"
14#endif
mtklein605d9522016-09-21 14:01:32 -070015#include "gl/command_buffer/GLTestContext_command_buffer.h"
bsalomon273c0f52016-03-31 10:59:06 -070016#include "gl/debug/DebugGLTestContext.h"
djsollen7e731082016-06-09 13:07:13 -070017#ifdef SK_VULKAN
bsalomon18a2f9d2016-05-11 10:09:18 -070018#include "vk/VkTestContext.h"
bsalomon7c62b472016-04-01 07:42:05 -070019#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040020#ifdef SK_METAL
21#include "mtl/MtlTestContext.h"
22#endif
bsalomon273c0f52016-03-31 10:59:06 -070023#include "gl/null/NullGLTestContext.h"
kkinnunencfe62e32015-07-01 02:58:50 -070024#include "gl/GrGLGpu.h"
Brian Salomoncfe910d2017-07-06 16:40:18 -040025#include "mock/MockTestContext.h"
kkinnunencfe62e32015-07-01 02:58:50 -070026#include "GrCaps.h"
djsollene4545212014-11-13 11:12:41 -080027
Brian Osman3f375d02016-12-28 11:19:22 -050028#if defined(SK_BUILD_FOR_WIN32) && defined(SK_ENABLE_DISCRETE_GPU)
29extern "C" {
30 // NVIDIA documents that the presence and value of this symbol programmatically enable the high
31 // performance GPU in laptops with switchable graphics.
32 // https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm
33 // From testing, including this symbol, even if it is set to 0, we still get the NVIDIA GPU.
34 _declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
35
36 // AMD has a similar mechanism, although I don't have an AMD laptop, so this is untested.
37 // https://community.amd.com/thread/169965
38 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
39}
40#endif
41
bsalomon3724e572016-03-30 18:56:19 -070042namespace sk_gpu_test {
kkinnunen34058002016-01-06 23:49:30 -080043GrContextFactory::GrContextFactory() { }
44
45GrContextFactory::GrContextFactory(const GrContextOptions& opts)
46 : fGlobalOptions(opts) {
47}
48
49GrContextFactory::~GrContextFactory() {
50 this->destroyContexts();
51}
52
53void GrContextFactory::destroyContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050054 // We must delete the test contexts in reverse order so that any child context is finished and
55 // deleted before a parent context. This relies on the fact that when we make a new context we
56 // append it to the end of fContexts array.
57 // TODO: Look into keeping a dependency dag for contexts and deletion order
58 for (int i = fContexts.count() - 1; i >= 0; --i) {
59 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -050060 SkScopeExit restore(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -070061 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050062 restore = context.fTestContext->makeCurrentAndAutoRestore();
kkinnunen34058002016-01-06 23:49:30 -080063 }
64 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070065 context.fGrContext->releaseResourcesAndAbandonContext();
66 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080067 }
68 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070069 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080070 }
71 fContexts.reset();
72}
73
74void GrContextFactory::abandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050075 // We must abandon the test contexts in reverse order so that any child context is finished and
76 // abandoned before a parent context. This relies on the fact that when we make a new context we
77 // append it to the end of fContexts array.
78 // TODO: Look into keeping a dependency dag for contexts and deletion order
79 for (int i = fContexts.count() - 1; i >= 0; --i) {
80 Context& context = fContexts[i];
bsalomondc0fcd42016-04-11 14:21:33 -070081 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070082 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050083 auto restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomon18a2f9d2016-05-11 10:09:18 -070084 context.fTestContext->testAbandon();
85 delete(context.fTestContext);
86 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070087 }
88 context.fGrContext->abandonContext();
89 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080090 }
kkinnunen34058002016-01-06 23:49:30 -080091 }
92}
93
bsalomon6e2aad42016-04-01 11:54:31 -070094void GrContextFactory::releaseResourcesAndAbandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050095 // We must abandon the test contexts in reverse order so that any child context is finished and
96 // abandoned before a parent context. This relies on the fact that when we make a new context we
97 // append it to the end of fContexts array.
98 // TODO: Look into keeping a dependency dag for contexts and deletion order
99 for (int i = fContexts.count() - 1; i >= 0; --i) {
100 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -0500101 SkScopeExit restore(nullptr);
bsalomondc0fcd42016-04-11 14:21:33 -0700102 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700103 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -0500104 restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomondc0fcd42016-04-11 14:21:33 -0700105 }
bsalomon6e2aad42016-04-01 11:54:31 -0700106 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700107 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -0700108 if (context.fTestContext) {
109 delete context.fTestContext;
110 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700111 }
bsalomon6e2aad42016-04-01 11:54:31 -0700112 }
113 }
114}
115
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400116GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
117 return this->getContextInfo(type, overrides).grContext();
118}
119
Brian Osman9eac2ea2017-02-24 14:51:44 -0500120ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
121 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500122 // (shareIndex != 0) -> (shareContext != nullptr)
123 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
124
djsollene4545212014-11-13 11:12:41 -0800125 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800126 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800127 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700128 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500129 context.fShareContext == shareContext &&
130 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700131 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700132 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400133 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
134 context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800135 }
136 }
Brian Osman60c774d2017-02-21 16:58:08 -0500137
138 // If we're trying to create a context in a share group, find the master context
139 Context* masterContext = nullptr;
140 if (shareContext) {
141 for (int i = 0; i < fContexts.count(); ++i) {
142 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
143 masterContext = &fContexts[i];
144 break;
145 }
146 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500147 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500148 }
149
Ben Wagner145dbcd2016-11-03 14:40:50 -0400150 std::unique_ptr<TestContext> testCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700151 GrBackend backend = ContextTypeBackend(type);
152 switch (backend) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700153 case kOpenGL_GrBackend: {
Brian Osman60c774d2017-02-21 16:58:08 -0500154 GLTestContext* glShareContext = masterContext
155 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700156 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700157 switch (type) {
158 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500159 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700160 break;
161 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500162 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700163 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000164#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700165 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500166 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
167 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700168 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700169 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500170 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
171 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700172 break;
173 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500174 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
175 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700176 break;
177 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500178 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
179 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700180 break;
181 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500182 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
183 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700184 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000185#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400186#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700187 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500188 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700189 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400190#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700191 case kNullGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500192 glCtx = CreateNullGLTestContext(
193 ContextOverrides::kRequireNVPRSupport & overrides, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700194 break;
195 case kDebugGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500196 glCtx = CreateDebugGLTestContext(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700197 break;
198 default:
199 return ContextInfo();
200 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700201 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700202 return ContextInfo();
203 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700204 testCtx.reset(glCtx);
djsollene4545212014-11-13 11:12:41 -0800205 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700206 }
jvanvertha50e17a2015-08-12 12:19:36 -0700207#ifdef SK_VULKAN
Greg Daniel604b1972017-05-15 13:50:35 -0400208 case kVulkan_GrBackend: {
209 VkTestContext* vkSharedContext = masterContext
210 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700211 SkASSERT(kVulkan_ContextType == type);
csmartdaltone812d492017-02-21 12:36:05 -0700212 if (ContextOverrides::kRequireNVPRSupport & overrides) {
bsalomondc0fcd42016-04-11 14:21:33 -0700213 return ContextInfo();
214 }
Greg Daniel604b1972017-05-15 13:50:35 -0400215 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700216 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700217 return ContextInfo();
218 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700219
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000220 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
221 // destruction will hang occaisonally. For some reason having an existing GL
222 // context fixes this.
223 if (!fSentinelGLContext) {
224 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
225 if (!fSentinelGLContext) {
226 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
227 }
228 }
bsalomondc0fcd42016-04-11 14:21:33 -0700229 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400230 }
jvanvertha50e17a2015-08-12 12:19:36 -0700231#endif
Greg Danielb76a72a2017-07-13 15:07:54 -0400232#ifdef SK_METAL
233 case kMetal_GrBackend: {
234 SkASSERT(!masterContext);
235 testCtx.reset(CreatePlatformMtlTestContext(nullptr));
236 if (!testCtx) {
237 return ContextInfo();
238 }
239 break;
240 }
241#endif
Brian Salomoncfe910d2017-07-06 16:40:18 -0400242 case kMock_GrBackend: {
243 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
244 SkASSERT(kMock_ContextType == type);
245 if (ContextOverrides::kRequireNVPRSupport & overrides) {
246 return ContextInfo();
247 }
248 testCtx.reset(CreateMockTestContext(sharedContext));
249 if (!testCtx) {
250 return ContextInfo();
251 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400252 break;
253 }
bsalomondc0fcd42016-04-11 14:21:33 -0700254 default:
255 return ContextInfo();
256 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500257
bsalomon18a2f9d2016-05-11 10:09:18 -0700258 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700259 GrContextOptions grOptions = fGlobalOptions;
csmartdalton008b9d82017-02-22 12:00:42 -0700260 if (ContextOverrides::kDisableNVPR & overrides) {
261 grOptions.fSuppressPathRendering = true;
262 }
csmartdaltone812d492017-02-21 12:36:05 -0700263 if (ContextOverrides::kAllowSRGBWithoutDecodeControl & overrides) {
264 grOptions.fRequireDecodeDisableForSRGB = false;
265 }
Eric Karl5c779752017-05-08 12:02:07 -0700266 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
267 grOptions.fAvoidStencilBuffers = true;
268 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500269 sk_sp<GrContext> grCtx;
270 {
271 auto restore = testCtx->makeCurrentAndAutoRestore();
272 grCtx = testCtx->makeGrContext(grOptions);
273 }
djsollene4545212014-11-13 11:12:41 -0800274 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800275 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800276 }
csmartdaltone812d492017-02-21 12:36:05 -0700277 if (ContextOverrides::kRequireNVPRSupport & overrides) {
kkinnunencfe62e32015-07-01 02:58:50 -0700278 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800279 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700280 }
281 }
csmartdaltone812d492017-02-21 12:36:05 -0700282 if (ContextOverrides::kRequireSRGBSupport & overrides) {
brianosman61d3b082016-03-30 11:19:36 -0700283 if (!grCtx->caps()->srgbSupport()) {
284 return ContextInfo();
285 }
286 }
kkinnunencfe62e32015-07-01 02:58:50 -0700287
Greg Danielf0fe02f2018-01-16 11:20:41 -0500288 // We must always add new contexts by pushing to the back so that when we delete them we delete
289 // them in reverse order in which they were made.
kkinnunen34058002016-01-06 23:49:30 -0800290 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700291 context.fBackend = backend;
292 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800293 context.fGrContext = SkRef(grCtx.get());
294 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700295 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700296 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500297 context.fShareContext = shareContext;
298 context.fShareIndex = shareIndex;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400299 context.fOptions = grOptions;
Brian Salomon55ad7742017-11-17 09:25:23 -0500300 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400301 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800302}
Brian Osman60c774d2017-02-21 16:58:08 -0500303
Brian Osman9eac2ea2017-02-24 14:51:44 -0500304ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
305 return this->getContextInfoInternal(type, overrides, nullptr, 0);
306}
307
308ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
309 SkASSERT(shareContext);
310 for (int i = 0; i < fContexts.count(); ++i) {
311 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
312 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
313 shareContext, shareIndex);
314 }
315 }
316
317 return ContextInfo();
318}
319
bsalomon3724e572016-03-30 18:56:19 -0700320} // namespace sk_gpu_test