blob: bd745bcfcd3a3b19fe570f4ea14a360573ffdb9d [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) {
csmartdalton6270e552016-09-13 10:41:49 -070046 // In this factory, instanced rendering is specified with ContextOptions::kUseInstanced.
csmartdaltone0d36292016-07-29 08:14:20 -070047 SkASSERT(!fGlobalOptions.fEnableInstancedRendering);
kkinnunen34058002016-01-06 23:49:30 -080048}
49
50GrContextFactory::~GrContextFactory() {
51 this->destroyContexts();
52}
53
54void GrContextFactory::destroyContexts() {
55 for (Context& context : fContexts) {
bsalomon18a2f9d2016-05-11 10:09:18 -070056 if (context.fTestContext) {
57 context.fTestContext->makeCurrent();
kkinnunen34058002016-01-06 23:49:30 -080058 }
59 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070060 context.fGrContext->releaseResourcesAndAbandonContext();
61 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080062 }
63 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070064 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080065 }
66 fContexts.reset();
67}
68
69void GrContextFactory::abandonContexts() {
70 for (Context& context : fContexts) {
bsalomondc0fcd42016-04-11 14:21:33 -070071 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070072 if (context.fTestContext) {
73 context.fTestContext->makeCurrent();
74 context.fTestContext->testAbandon();
75 delete(context.fTestContext);
76 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070077 }
78 context.fGrContext->abandonContext();
79 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080080 }
kkinnunen34058002016-01-06 23:49:30 -080081 }
82}
83
bsalomon6e2aad42016-04-01 11:54:31 -070084void GrContextFactory::releaseResourcesAndAbandonContexts() {
85 for (Context& context : fContexts) {
bsalomondc0fcd42016-04-11 14:21:33 -070086 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070087 if (context.fTestContext) {
88 context.fTestContext->makeCurrent();
bsalomondc0fcd42016-04-11 14:21:33 -070089 }
bsalomon6e2aad42016-04-01 11:54:31 -070090 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -070091 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -070092 if (context.fTestContext) {
93 delete context.fTestContext;
94 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070095 }
bsalomon6e2aad42016-04-01 11:54:31 -070096 }
97 }
98}
99
bsalomon85b4b532016-04-05 11:06:27 -0700100#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
101const GrContextFactory::ContextType GrContextFactory::kNativeGL_ContextType =
102 GrContextFactory::kGL_ContextType;
103#else
104const GrContextFactory::ContextType GrContextFactory::kNativeGL_ContextType =
105 GrContextFactory::kGLES_ContextType;
106#endif
107
bsalomonf2f1c172016-04-05 12:59:06 -0700108ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOptions options) {
djsollene4545212014-11-13 11:12:41 -0800109 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800110 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800111 if (context.fType == type &&
bsalomondc0fcd42016-04-11 14:21:33 -0700112 context.fOptions == options &&
113 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700114 context.fTestContext->makeCurrent();
115 return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext);
djsollene4545212014-11-13 11:12:41 -0800116 }
117 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400118 std::unique_ptr<TestContext> testCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700119 sk_sp<GrContext> grCtx;
120 GrBackendContext backendContext = 0;
121 sk_sp<const GrGLInterface> glInterface;
bsalomondc0fcd42016-04-11 14:21:33 -0700122 GrBackend backend = ContextTypeBackend(type);
123 switch (backend) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700124 case kOpenGL_GrBackend: {
125 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700126 switch (type) {
127 case kGL_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700128 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard);
bsalomondc0fcd42016-04-11 14:21:33 -0700129 break;
130 case kGLES_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700131 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard);
bsalomondc0fcd42016-04-11 14:21:33 -0700132 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000133#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700134 case kANGLE_D3D9_ES2_ContextType:
Ben Wagner12e8dc22016-11-03 15:43:44 -0400135 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700136 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700137 case kANGLE_D3D11_ES2_ContextType:
Ben Wagner12e8dc22016-11-03 15:43:44 -0400138 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700139 break;
140 case kANGLE_D3D11_ES3_ContextType:
Ben Wagner12e8dc22016-11-03 15:43:44 -0400141 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700142 break;
143 case kANGLE_GL_ES2_ContextType:
Ben Wagner12e8dc22016-11-03 15:43:44 -0400144 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700145 break;
146 case kANGLE_GL_ES3_ContextType:
Ben Wagner12e8dc22016-11-03 15:43:44 -0400147 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700148 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000149#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700150 case kCommandBuffer_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700151 glCtx = CommandBufferGLTestContext::Create();
bsalomondc0fcd42016-04-11 14:21:33 -0700152 break;
kkinnunen3e980c32015-12-23 01:33:00 -0800153#if SK_MESA
bsalomondc0fcd42016-04-11 14:21:33 -0700154 case kMESA_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700155 glCtx = CreateMesaGLTestContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700156 break;
djsollene4545212014-11-13 11:12:41 -0800157#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700158 case kNullGL_ContextType:
csmartdalton6270e552016-09-13 10:41:49 -0700159 glCtx = CreateNullGLTestContext(ContextOptions::kEnableNVPR & options);
bsalomondc0fcd42016-04-11 14:21:33 -0700160 break;
161 case kDebugGL_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700162 glCtx = CreateDebugGLTestContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700163 break;
164 default:
165 return ContextInfo();
166 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700167 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700168 return ContextInfo();
169 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700170 testCtx.reset(glCtx);
bsalomondc0fcd42016-04-11 14:21:33 -0700171 glInterface.reset(SkRef(glCtx->gl()));
csmartdaltone0d36292016-07-29 08:14:20 -0700172 // Block NVPR from non-NVPR types. We don't block NVPR from contexts that will use
173 // instanced rendering because that would prevent us from testing mixed samples.
csmartdalton6270e552016-09-13 10:41:49 -0700174 if (!((ContextOptions::kEnableNVPR | ContextOptions::kUseInstanced) & options)) {
bsalomondc0fcd42016-04-11 14:21:33 -0700175 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface.get()));
176 if (!glInterface) {
177 return ContextInfo();
178 }
179 }
180 backendContext = reinterpret_cast<GrBackendContext>(glInterface.get());
djsollene4545212014-11-13 11:12:41 -0800181 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700182 }
jvanvertha50e17a2015-08-12 12:19:36 -0700183#ifdef SK_VULKAN
bsalomondc0fcd42016-04-11 14:21:33 -0700184 case kVulkan_GrBackend:
185 SkASSERT(kVulkan_ContextType == type);
csmartdalton6270e552016-09-13 10:41:49 -0700186 if (ContextOptions::kEnableNVPR & options) {
bsalomondc0fcd42016-04-11 14:21:33 -0700187 return ContextInfo();
188 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700189 testCtx.reset(CreatePlatformVkTestContext());
190 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700191 return ContextInfo();
192 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700193
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000194 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
195 // destruction will hang occaisonally. For some reason having an existing GL
196 // context fixes this.
197 if (!fSentinelGLContext) {
198 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
199 if (!fSentinelGLContext) {
200 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
201 }
202 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700203 backendContext = testCtx->backendContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700204 break;
jvanvertha50e17a2015-08-12 12:19:36 -0700205#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700206 default:
207 return ContextInfo();
208 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700209 testCtx->makeCurrent();
210 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700211 GrContextOptions grOptions = fGlobalOptions;
csmartdalton6270e552016-09-13 10:41:49 -0700212 if (ContextOptions::kUseInstanced & options) {
csmartdaltone0d36292016-07-29 08:14:20 -0700213 grOptions.fEnableInstancedRendering = true;
214 }
brianosman20471892016-12-02 06:43:32 -0800215 grOptions.fRequireDecodeDisableForSRGB =
216 SkToBool(ContextOptions::kRequireSRGBDecodeDisableSupport & options);
csmartdaltone0d36292016-07-29 08:14:20 -0700217 grCtx.reset(GrContext::Create(backend, backendContext, grOptions));
djsollene4545212014-11-13 11:12:41 -0800218 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800219 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800220 }
csmartdalton6270e552016-09-13 10:41:49 -0700221 if (ContextOptions::kEnableNVPR & options) {
kkinnunencfe62e32015-07-01 02:58:50 -0700222 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800223 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700224 }
225 }
csmartdalton6270e552016-09-13 10:41:49 -0700226 if (ContextOptions::kUseInstanced & options) {
csmartdaltone0d36292016-07-29 08:14:20 -0700227 if (GrCaps::InstancedSupport::kNone == grCtx->caps()->instancedSupport()) {
228 return ContextInfo();
229 }
230 }
csmartdalton6270e552016-09-13 10:41:49 -0700231 if (ContextOptions::kRequireSRGBSupport & options) {
brianosman61d3b082016-03-30 11:19:36 -0700232 if (!grCtx->caps()->srgbSupport()) {
233 return ContextInfo();
234 }
235 }
kkinnunencfe62e32015-07-01 02:58:50 -0700236
kkinnunen34058002016-01-06 23:49:30 -0800237 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700238 context.fBackend = backend;
239 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800240 context.fGrContext = SkRef(grCtx.get());
241 context.fType = type;
242 context.fOptions = options;
bsalomondc0fcd42016-04-11 14:21:33 -0700243 context.fAbandoned = false;
bsalomon18a2f9d2016-05-11 10:09:18 -0700244 return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext);
djsollene4545212014-11-13 11:12:41 -0800245}
bsalomon3724e572016-03-30 18:56:19 -0700246} // namespace sk_gpu_test