blob: 1bc46555585a20f10eea63a2593ac2306b6bbdfa [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
Greg Danielb76a72a2017-07-13 15:07:54 -040023#ifdef SK_METAL
24#include "mtl/MtlTestContext.h"
25#endif
bsalomon273c0f52016-03-31 10:59:06 -070026#include "gl/null/NullGLTestContext.h"
kkinnunencfe62e32015-07-01 02:58:50 -070027#include "gl/GrGLGpu.h"
Brian Salomoncfe910d2017-07-06 16:40:18 -040028#include "mock/MockTestContext.h"
kkinnunencfe62e32015-07-01 02:58:50 -070029#include "GrCaps.h"
djsollene4545212014-11-13 11:12:41 -080030
Brian Osman3f375d02016-12-28 11:19:22 -050031#if defined(SK_BUILD_FOR_WIN32) && defined(SK_ENABLE_DISCRETE_GPU)
32extern "C" {
33 // NVIDIA documents that the presence and value of this symbol programmatically enable the high
34 // performance GPU in laptops with switchable graphics.
35 // https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm
36 // From testing, including this symbol, even if it is set to 0, we still get the NVIDIA GPU.
37 _declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
38
39 // AMD has a similar mechanism, although I don't have an AMD laptop, so this is untested.
40 // https://community.amd.com/thread/169965
41 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
42}
43#endif
44
bsalomon3724e572016-03-30 18:56:19 -070045namespace sk_gpu_test {
kkinnunen34058002016-01-06 23:49:30 -080046GrContextFactory::GrContextFactory() { }
47
48GrContextFactory::GrContextFactory(const GrContextOptions& opts)
49 : fGlobalOptions(opts) {
50}
51
52GrContextFactory::~GrContextFactory() {
53 this->destroyContexts();
54}
55
56void GrContextFactory::destroyContexts() {
57 for (Context& context : fContexts) {
bsalomon18a2f9d2016-05-11 10:09:18 -070058 if (context.fTestContext) {
59 context.fTestContext->makeCurrent();
kkinnunen34058002016-01-06 23:49:30 -080060 }
61 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070062 context.fGrContext->releaseResourcesAndAbandonContext();
63 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080064 }
65 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070066 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080067 }
68 fContexts.reset();
69}
70
71void GrContextFactory::abandonContexts() {
72 for (Context& context : fContexts) {
bsalomondc0fcd42016-04-11 14:21:33 -070073 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070074 if (context.fTestContext) {
75 context.fTestContext->makeCurrent();
76 context.fTestContext->testAbandon();
77 delete(context.fTestContext);
78 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070079 }
80 context.fGrContext->abandonContext();
81 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080082 }
kkinnunen34058002016-01-06 23:49:30 -080083 }
84}
85
bsalomon6e2aad42016-04-01 11:54:31 -070086void GrContextFactory::releaseResourcesAndAbandonContexts() {
87 for (Context& context : fContexts) {
bsalomondc0fcd42016-04-11 14:21:33 -070088 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070089 if (context.fTestContext) {
90 context.fTestContext->makeCurrent();
bsalomondc0fcd42016-04-11 14:21:33 -070091 }
bsalomon6e2aad42016-04-01 11:54:31 -070092 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -070093 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -070094 if (context.fTestContext) {
95 delete context.fTestContext;
96 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070097 }
bsalomon6e2aad42016-04-01 11:54:31 -070098 }
99 }
100}
101
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400102GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
103 return this->getContextInfo(type, overrides).grContext();
104}
105
Brian Osman9eac2ea2017-02-24 14:51:44 -0500106ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
107 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500108 // (shareIndex != 0) -> (shareContext != nullptr)
109 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
110
djsollene4545212014-11-13 11:12:41 -0800111 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800112 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800113 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700114 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500115 context.fShareContext == shareContext &&
116 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700117 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700118 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400119 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
120 context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800121 }
122 }
Brian Osman60c774d2017-02-21 16:58:08 -0500123
124 // If we're trying to create a context in a share group, find the master context
125 Context* masterContext = nullptr;
126 if (shareContext) {
127 for (int i = 0; i < fContexts.count(); ++i) {
128 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
129 masterContext = &fContexts[i];
130 break;
131 }
132 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500133 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500134 }
135
Ben Wagner145dbcd2016-11-03 14:40:50 -0400136 std::unique_ptr<TestContext> testCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700137 GrBackend backend = ContextTypeBackend(type);
138 switch (backend) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700139 case kOpenGL_GrBackend: {
Brian Osman60c774d2017-02-21 16:58:08 -0500140 GLTestContext* glShareContext = masterContext
141 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700142 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700143 switch (type) {
144 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500145 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700146 break;
147 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500148 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700149 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000150#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700151 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500152 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
153 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700154 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700155 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500156 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
157 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700158 break;
159 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500160 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
161 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700162 break;
163 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500164 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
165 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700166 break;
167 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500168 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
169 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700170 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000171#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400172#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700173 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500174 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700175 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400176#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800177#if SK_MESA
bsalomondc0fcd42016-04-11 14:21:33 -0700178 case kMESA_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500179 glCtx = CreateMesaGLTestContext(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700180 break;
djsollene4545212014-11-13 11:12:41 -0800181#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700182 case kNullGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500183 glCtx = CreateNullGLTestContext(
184 ContextOverrides::kRequireNVPRSupport & overrides, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700185 break;
186 case kDebugGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500187 glCtx = CreateDebugGLTestContext(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700188 break;
189 default:
190 return ContextInfo();
191 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700192 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700193 return ContextInfo();
194 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700195 testCtx.reset(glCtx);
djsollene4545212014-11-13 11:12:41 -0800196 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700197 }
jvanvertha50e17a2015-08-12 12:19:36 -0700198#ifdef SK_VULKAN
Greg Daniel604b1972017-05-15 13:50:35 -0400199 case kVulkan_GrBackend: {
200 VkTestContext* vkSharedContext = masterContext
201 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700202 SkASSERT(kVulkan_ContextType == type);
csmartdaltone812d492017-02-21 12:36:05 -0700203 if (ContextOverrides::kRequireNVPRSupport & overrides) {
bsalomondc0fcd42016-04-11 14:21:33 -0700204 return ContextInfo();
205 }
Greg Daniel604b1972017-05-15 13:50:35 -0400206 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700207 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700208 return ContextInfo();
209 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700210
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000211 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
212 // destruction will hang occaisonally. For some reason having an existing GL
213 // context fixes this.
214 if (!fSentinelGLContext) {
215 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
216 if (!fSentinelGLContext) {
217 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
218 }
219 }
bsalomondc0fcd42016-04-11 14:21:33 -0700220 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400221 }
jvanvertha50e17a2015-08-12 12:19:36 -0700222#endif
Greg Danielb76a72a2017-07-13 15:07:54 -0400223#ifdef SK_METAL
224 case kMetal_GrBackend: {
225 SkASSERT(!masterContext);
226 testCtx.reset(CreatePlatformMtlTestContext(nullptr));
227 if (!testCtx) {
228 return ContextInfo();
229 }
230 break;
231 }
232#endif
Brian Salomoncfe910d2017-07-06 16:40:18 -0400233 case kMock_GrBackend: {
234 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
235 SkASSERT(kMock_ContextType == type);
236 if (ContextOverrides::kRequireNVPRSupport & overrides) {
237 return ContextInfo();
238 }
239 testCtx.reset(CreateMockTestContext(sharedContext));
240 if (!testCtx) {
241 return ContextInfo();
242 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400243 break;
244 }
bsalomondc0fcd42016-04-11 14:21:33 -0700245 default:
246 return ContextInfo();
247 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700248 testCtx->makeCurrent();
249 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700250 GrContextOptions grOptions = fGlobalOptions;
csmartdalton008b9d82017-02-22 12:00:42 -0700251 if (ContextOverrides::kDisableNVPR & overrides) {
252 grOptions.fSuppressPathRendering = true;
253 }
csmartdaltone812d492017-02-21 12:36:05 -0700254 if (ContextOverrides::kUseInstanced & overrides) {
csmartdaltone0d36292016-07-29 08:14:20 -0700255 grOptions.fEnableInstancedRendering = true;
256 }
csmartdaltone812d492017-02-21 12:36:05 -0700257 if (ContextOverrides::kAllowSRGBWithoutDecodeControl & overrides) {
258 grOptions.fRequireDecodeDisableForSRGB = false;
259 }
Eric Karl5c779752017-05-08 12:02:07 -0700260 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
261 grOptions.fAvoidStencilBuffers = true;
262 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400263 sk_sp<GrContext> grCtx = testCtx->makeGrContext(grOptions);
djsollene4545212014-11-13 11:12:41 -0800264 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800265 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800266 }
csmartdaltone812d492017-02-21 12:36:05 -0700267 if (ContextOverrides::kRequireNVPRSupport & overrides) {
kkinnunencfe62e32015-07-01 02:58:50 -0700268 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800269 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700270 }
271 }
csmartdaltone812d492017-02-21 12:36:05 -0700272 if (ContextOverrides::kUseInstanced & overrides) {
csmartdaltone0d36292016-07-29 08:14:20 -0700273 if (GrCaps::InstancedSupport::kNone == grCtx->caps()->instancedSupport()) {
274 return ContextInfo();
275 }
276 }
csmartdaltone812d492017-02-21 12:36:05 -0700277 if (ContextOverrides::kRequireSRGBSupport & overrides) {
brianosman61d3b082016-03-30 11:19:36 -0700278 if (!grCtx->caps()->srgbSupport()) {
279 return ContextInfo();
280 }
281 }
kkinnunencfe62e32015-07-01 02:58:50 -0700282
kkinnunen34058002016-01-06 23:49:30 -0800283 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700284 context.fBackend = backend;
285 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800286 context.fGrContext = SkRef(grCtx.get());
287 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700288 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700289 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500290 context.fShareContext = shareContext;
291 context.fShareIndex = shareIndex;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400292 context.fOptions = grOptions;
293 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800294}
Brian Osman60c774d2017-02-21 16:58:08 -0500295
Brian Osman9eac2ea2017-02-24 14:51:44 -0500296ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
297 return this->getContextInfoInternal(type, overrides, nullptr, 0);
298}
299
300ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
301 SkASSERT(shareContext);
302 for (int i = 0; i < fContexts.count(); ++i) {
303 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
304 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
305 shareContext, shareIndex);
306 }
307 }
308
309 return ContextInfo();
310}
311
bsalomon3724e572016-03-30 18:56:19 -0700312} // namespace sk_gpu_test