blob: 23ffad155f0469286e37739841bc50ca2fc0838a [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"
djsollene4545212014-11-13 11:12:41 -080017#if SK_MESA
bsalomon273c0f52016-03-31 10:59:06 -070018 #include "gl/mesa/GLTestContext_mesa.h"
djsollene4545212014-11-13 11:12:41 -080019#endif
djsollen7e731082016-06-09 13:07:13 -070020#ifdef SK_VULKAN
bsalomon18a2f9d2016-05-11 10:09:18 -070021#include "vk/VkTestContext.h"
bsalomon7c62b472016-04-01 07:42:05 -070022#endif
bsalomon273c0f52016-03-31 10:59:06 -070023#include "gl/null/NullGLTestContext.h"
kkinnunencfe62e32015-07-01 02:58:50 -070024#include "gl/GrGLGpu.h"
25#include "GrCaps.h"
djsollene4545212014-11-13 11:12:41 -080026
Brian Osman3f375d02016-12-28 11:19:22 -050027#if defined(SK_BUILD_FOR_WIN32) && defined(SK_ENABLE_DISCRETE_GPU)
28extern "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() {
53 for (Context& context : fContexts) {
bsalomon18a2f9d2016-05-11 10:09:18 -070054 if (context.fTestContext) {
55 context.fTestContext->makeCurrent();
kkinnunen34058002016-01-06 23:49:30 -080056 }
57 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070058 context.fGrContext->releaseResourcesAndAbandonContext();
59 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080060 }
61 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070062 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080063 }
64 fContexts.reset();
65}
66
67void GrContextFactory::abandonContexts() {
68 for (Context& context : fContexts) {
bsalomondc0fcd42016-04-11 14:21:33 -070069 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070070 if (context.fTestContext) {
71 context.fTestContext->makeCurrent();
72 context.fTestContext->testAbandon();
73 delete(context.fTestContext);
74 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070075 }
76 context.fGrContext->abandonContext();
77 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080078 }
kkinnunen34058002016-01-06 23:49:30 -080079 }
80}
81
bsalomon6e2aad42016-04-01 11:54:31 -070082void GrContextFactory::releaseResourcesAndAbandonContexts() {
83 for (Context& context : fContexts) {
bsalomondc0fcd42016-04-11 14:21:33 -070084 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070085 if (context.fTestContext) {
86 context.fTestContext->makeCurrent();
bsalomondc0fcd42016-04-11 14:21:33 -070087 }
bsalomon6e2aad42016-04-01 11:54:31 -070088 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -070089 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -070090 if (context.fTestContext) {
91 delete context.fTestContext;
92 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070093 }
bsalomon6e2aad42016-04-01 11:54:31 -070094 }
95 }
96}
97
Robert Phillipscdabbcc2017-06-08 16:03:17 -040098GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
99 return this->getContextInfo(type, overrides).grContext();
100}
101
Brian Osman9eac2ea2017-02-24 14:51:44 -0500102ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
103 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500104 // (shareIndex != 0) -> (shareContext != nullptr)
105 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
106
djsollene4545212014-11-13 11:12:41 -0800107 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800108 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800109 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700110 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500111 context.fShareContext == shareContext &&
112 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700113 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700114 context.fTestContext->makeCurrent();
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400115 return ContextInfo(context.fType, context.fTestContext, context.fGrContext);
djsollene4545212014-11-13 11:12:41 -0800116 }
117 }
Brian Osman60c774d2017-02-21 16:58:08 -0500118
119 // If we're trying to create a context in a share group, find the master context
120 Context* masterContext = nullptr;
121 if (shareContext) {
122 for (int i = 0; i < fContexts.count(); ++i) {
123 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
124 masterContext = &fContexts[i];
125 break;
126 }
127 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500128 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500129 }
130
Ben Wagner145dbcd2016-11-03 14:40:50 -0400131 std::unique_ptr<TestContext> testCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700132 GrBackendContext backendContext = 0;
133 sk_sp<const GrGLInterface> glInterface;
bsalomondc0fcd42016-04-11 14:21:33 -0700134 GrBackend backend = ContextTypeBackend(type);
135 switch (backend) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700136 case kOpenGL_GrBackend: {
Brian Osman60c774d2017-02-21 16:58:08 -0500137 GLTestContext* glShareContext = masterContext
138 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700139 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700140 switch (type) {
141 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500142 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700143 break;
144 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500145 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700146 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000147#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700148 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500149 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
150 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700151 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700152 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500153 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
154 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700155 break;
156 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500157 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
158 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700159 break;
160 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500161 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
162 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700163 break;
164 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500165 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
166 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700167 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000168#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400169#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700170 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500171 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700172 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400173#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800174#if SK_MESA
bsalomondc0fcd42016-04-11 14:21:33 -0700175 case kMESA_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500176 glCtx = CreateMesaGLTestContext(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700177 break;
djsollene4545212014-11-13 11:12:41 -0800178#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700179 case kNullGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500180 glCtx = CreateNullGLTestContext(
181 ContextOverrides::kRequireNVPRSupport & overrides, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700182 break;
183 case kDebugGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500184 glCtx = CreateDebugGLTestContext(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700185 break;
186 default:
187 return ContextInfo();
188 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700189 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700190 return ContextInfo();
191 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700192 testCtx.reset(glCtx);
bsalomondc0fcd42016-04-11 14:21:33 -0700193 glInterface.reset(SkRef(glCtx->gl()));
bsalomondc0fcd42016-04-11 14:21:33 -0700194 backendContext = reinterpret_cast<GrBackendContext>(glInterface.get());
djsollene4545212014-11-13 11:12:41 -0800195 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700196 }
jvanvertha50e17a2015-08-12 12:19:36 -0700197#ifdef SK_VULKAN
Greg Daniel604b1972017-05-15 13:50:35 -0400198 case kVulkan_GrBackend: {
199 VkTestContext* vkSharedContext = masterContext
200 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700201 SkASSERT(kVulkan_ContextType == type);
csmartdaltone812d492017-02-21 12:36:05 -0700202 if (ContextOverrides::kRequireNVPRSupport & overrides) {
bsalomondc0fcd42016-04-11 14:21:33 -0700203 return ContextInfo();
204 }
Greg Daniel604b1972017-05-15 13:50:35 -0400205 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700206 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700207 return ContextInfo();
208 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700209
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000210 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
211 // destruction will hang occaisonally. For some reason having an existing GL
212 // context fixes this.
213 if (!fSentinelGLContext) {
214 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
215 if (!fSentinelGLContext) {
216 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
217 }
218 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700219 backendContext = testCtx->backendContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700220 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400221 }
jvanvertha50e17a2015-08-12 12:19:36 -0700222#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700223 default:
224 return ContextInfo();
225 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700226 testCtx->makeCurrent();
227 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700228 GrContextOptions grOptions = fGlobalOptions;
csmartdalton008b9d82017-02-22 12:00:42 -0700229 if (ContextOverrides::kDisableNVPR & overrides) {
230 grOptions.fSuppressPathRendering = true;
231 }
csmartdaltone812d492017-02-21 12:36:05 -0700232 if (ContextOverrides::kUseInstanced & overrides) {
csmartdaltone0d36292016-07-29 08:14:20 -0700233 grOptions.fEnableInstancedRendering = true;
234 }
csmartdaltone812d492017-02-21 12:36:05 -0700235 if (ContextOverrides::kAllowSRGBWithoutDecodeControl & overrides) {
236 grOptions.fRequireDecodeDisableForSRGB = false;
237 }
Eric Karl5c779752017-05-08 12:02:07 -0700238 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
239 grOptions.fAvoidStencilBuffers = true;
240 }
Brian Osman60c774d2017-02-21 16:58:08 -0500241 sk_sp<GrContext> grCtx(GrContext::Create(backend, backendContext, grOptions));
djsollene4545212014-11-13 11:12:41 -0800242 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800243 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800244 }
csmartdaltone812d492017-02-21 12:36:05 -0700245 if (ContextOverrides::kRequireNVPRSupport & overrides) {
kkinnunencfe62e32015-07-01 02:58:50 -0700246 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800247 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700248 }
249 }
csmartdaltone812d492017-02-21 12:36:05 -0700250 if (ContextOverrides::kUseInstanced & overrides) {
csmartdaltone0d36292016-07-29 08:14:20 -0700251 if (GrCaps::InstancedSupport::kNone == grCtx->caps()->instancedSupport()) {
252 return ContextInfo();
253 }
254 }
csmartdaltone812d492017-02-21 12:36:05 -0700255 if (ContextOverrides::kRequireSRGBSupport & overrides) {
brianosman61d3b082016-03-30 11:19:36 -0700256 if (!grCtx->caps()->srgbSupport()) {
257 return ContextInfo();
258 }
259 }
kkinnunencfe62e32015-07-01 02:58:50 -0700260
kkinnunen34058002016-01-06 23:49:30 -0800261 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700262 context.fBackend = backend;
263 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800264 context.fGrContext = SkRef(grCtx.get());
265 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700266 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700267 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500268 context.fShareContext = shareContext;
269 context.fShareIndex = shareIndex;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400270 return ContextInfo(context.fType, context.fTestContext, context.fGrContext);
djsollene4545212014-11-13 11:12:41 -0800271}
Brian Osman60c774d2017-02-21 16:58:08 -0500272
Brian Osman9eac2ea2017-02-24 14:51:44 -0500273ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
274 return this->getContextInfoInternal(type, overrides, nullptr, 0);
275}
276
277ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
278 SkASSERT(shareContext);
279 for (int i = 0; i < fContexts.count(); ++i) {
280 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
281 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
282 shareContext, shareIndex);
283 }
284 }
285
286 return ContextInfo();
287}
288
bsalomon3724e572016-03-30 18:56:19 -0700289} // namespace sk_gpu_test