blob: 401189a6a3faf3e4b6b1b69cbdd9866751a70d4c [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
bsalomon85b4b532016-04-05 11:06:27 -070098#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
99const GrContextFactory::ContextType GrContextFactory::kNativeGL_ContextType =
100 GrContextFactory::kGL_ContextType;
101#else
102const GrContextFactory::ContextType GrContextFactory::kNativeGL_ContextType =
103 GrContextFactory::kGLES_ContextType;
104#endif
105
csmartdaltone812d492017-02-21 12:36:05 -0700106ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
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 &&
bsalomondc0fcd42016-04-11 14:21:33 -0700111 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700112 context.fTestContext->makeCurrent();
113 return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext);
djsollene4545212014-11-13 11:12:41 -0800114 }
115 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400116 std::unique_ptr<TestContext> testCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700117 sk_sp<GrContext> grCtx;
118 GrBackendContext backendContext = 0;
119 sk_sp<const GrGLInterface> glInterface;
bsalomondc0fcd42016-04-11 14:21:33 -0700120 GrBackend backend = ContextTypeBackend(type);
121 switch (backend) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700122 case kOpenGL_GrBackend: {
123 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700124 switch (type) {
125 case kGL_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700126 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard);
bsalomondc0fcd42016-04-11 14:21:33 -0700127 break;
128 case kGLES_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700129 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard);
bsalomondc0fcd42016-04-11 14:21:33 -0700130 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000131#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700132 case kANGLE_D3D9_ES2_ContextType:
Ben Wagner12e8dc22016-11-03 15:43:44 -0400133 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700134 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700135 case kANGLE_D3D11_ES2_ContextType:
Ben Wagner12e8dc22016-11-03 15:43:44 -0400136 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700137 break;
138 case kANGLE_D3D11_ES3_ContextType:
Ben Wagner12e8dc22016-11-03 15:43:44 -0400139 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700140 break;
141 case kANGLE_GL_ES2_ContextType:
Ben Wagner12e8dc22016-11-03 15:43:44 -0400142 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700143 break;
144 case kANGLE_GL_ES3_ContextType:
Ben Wagner12e8dc22016-11-03 15:43:44 -0400145 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700146 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000147#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700148 case kCommandBuffer_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700149 glCtx = CommandBufferGLTestContext::Create();
bsalomondc0fcd42016-04-11 14:21:33 -0700150 break;
kkinnunen3e980c32015-12-23 01:33:00 -0800151#if SK_MESA
bsalomondc0fcd42016-04-11 14:21:33 -0700152 case kMESA_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700153 glCtx = CreateMesaGLTestContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700154 break;
djsollene4545212014-11-13 11:12:41 -0800155#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700156 case kNullGL_ContextType:
csmartdaltone812d492017-02-21 12:36:05 -0700157 glCtx = CreateNullGLTestContext(ContextOverrides::kRequireNVPRSupport & overrides);
bsalomondc0fcd42016-04-11 14:21:33 -0700158 break;
159 case kDebugGL_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700160 glCtx = CreateDebugGLTestContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700161 break;
162 default:
163 return ContextInfo();
164 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700165 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700166 return ContextInfo();
167 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700168 testCtx.reset(glCtx);
bsalomondc0fcd42016-04-11 14:21:33 -0700169 glInterface.reset(SkRef(glCtx->gl()));
csmartdaltone812d492017-02-21 12:36:05 -0700170 if (ContextOverrides::kDisableNVPR & overrides) {
bsalomondc0fcd42016-04-11 14:21:33 -0700171 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface.get()));
172 if (!glInterface) {
173 return ContextInfo();
174 }
175 }
176 backendContext = reinterpret_cast<GrBackendContext>(glInterface.get());
djsollene4545212014-11-13 11:12:41 -0800177 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700178 }
jvanvertha50e17a2015-08-12 12:19:36 -0700179#ifdef SK_VULKAN
bsalomondc0fcd42016-04-11 14:21:33 -0700180 case kVulkan_GrBackend:
181 SkASSERT(kVulkan_ContextType == type);
csmartdaltone812d492017-02-21 12:36:05 -0700182 if (ContextOverrides::kRequireNVPRSupport & overrides) {
bsalomondc0fcd42016-04-11 14:21:33 -0700183 return ContextInfo();
184 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700185 testCtx.reset(CreatePlatformVkTestContext());
186 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700187 return ContextInfo();
188 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700189
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000190 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
191 // destruction will hang occaisonally. For some reason having an existing GL
192 // context fixes this.
193 if (!fSentinelGLContext) {
194 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
195 if (!fSentinelGLContext) {
196 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
197 }
198 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700199 backendContext = testCtx->backendContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700200 break;
jvanvertha50e17a2015-08-12 12:19:36 -0700201#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700202 default:
203 return ContextInfo();
204 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700205 testCtx->makeCurrent();
206 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700207 GrContextOptions grOptions = fGlobalOptions;
csmartdaltone812d492017-02-21 12:36:05 -0700208 if (ContextOverrides::kUseInstanced & overrides) {
csmartdaltone0d36292016-07-29 08:14:20 -0700209 grOptions.fEnableInstancedRendering = true;
210 }
csmartdaltone812d492017-02-21 12:36:05 -0700211 if (ContextOverrides::kAllowSRGBWithoutDecodeControl & overrides) {
212 grOptions.fRequireDecodeDisableForSRGB = false;
213 }
csmartdaltone0d36292016-07-29 08:14:20 -0700214 grCtx.reset(GrContext::Create(backend, backendContext, grOptions));
djsollene4545212014-11-13 11:12:41 -0800215 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800216 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800217 }
csmartdaltone812d492017-02-21 12:36:05 -0700218 if (ContextOverrides::kRequireNVPRSupport & overrides) {
kkinnunencfe62e32015-07-01 02:58:50 -0700219 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800220 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700221 }
222 }
csmartdaltone812d492017-02-21 12:36:05 -0700223 if (ContextOverrides::kUseInstanced & overrides) {
csmartdaltone0d36292016-07-29 08:14:20 -0700224 if (GrCaps::InstancedSupport::kNone == grCtx->caps()->instancedSupport()) {
225 return ContextInfo();
226 }
227 }
csmartdaltone812d492017-02-21 12:36:05 -0700228 if (ContextOverrides::kRequireSRGBSupport & overrides) {
brianosman61d3b082016-03-30 11:19:36 -0700229 if (!grCtx->caps()->srgbSupport()) {
230 return ContextInfo();
231 }
232 }
kkinnunencfe62e32015-07-01 02:58:50 -0700233
kkinnunen34058002016-01-06 23:49:30 -0800234 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700235 context.fBackend = backend;
236 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800237 context.fGrContext = SkRef(grCtx.get());
238 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700239 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700240 context.fAbandoned = false;
bsalomon18a2f9d2016-05-11 10:09:18 -0700241 return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext);
djsollene4545212014-11-13 11:12:41 -0800242}
bsalomon3724e572016-03-30 18:56:19 -0700243} // namespace sk_gpu_test