blob: d614b0643e1a2fc4ebaaccc8fd069e97ba7ad50e [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"
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() {
54 for (Context& context : fContexts) {
bsalomon18a2f9d2016-05-11 10:09:18 -070055 if (context.fTestContext) {
56 context.fTestContext->makeCurrent();
kkinnunen34058002016-01-06 23:49:30 -080057 }
58 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070059 context.fGrContext->releaseResourcesAndAbandonContext();
60 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080061 }
62 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070063 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080064 }
65 fContexts.reset();
66}
67
68void GrContextFactory::abandonContexts() {
69 for (Context& context : fContexts) {
bsalomondc0fcd42016-04-11 14:21:33 -070070 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070071 if (context.fTestContext) {
72 context.fTestContext->makeCurrent();
73 context.fTestContext->testAbandon();
74 delete(context.fTestContext);
75 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070076 }
77 context.fGrContext->abandonContext();
78 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080079 }
kkinnunen34058002016-01-06 23:49:30 -080080 }
81}
82
bsalomon6e2aad42016-04-01 11:54:31 -070083void GrContextFactory::releaseResourcesAndAbandonContexts() {
84 for (Context& context : fContexts) {
bsalomondc0fcd42016-04-11 14:21:33 -070085 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070086 if (context.fTestContext) {
87 context.fTestContext->makeCurrent();
bsalomondc0fcd42016-04-11 14:21:33 -070088 }
bsalomon6e2aad42016-04-01 11:54:31 -070089 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -070090 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -070091 if (context.fTestContext) {
92 delete context.fTestContext;
93 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070094 }
bsalomon6e2aad42016-04-01 11:54:31 -070095 }
96 }
97}
98
Robert Phillipscdabbcc2017-06-08 16:03:17 -040099GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
100 return this->getContextInfo(type, overrides).grContext();
101}
102
Brian Osman9eac2ea2017-02-24 14:51:44 -0500103ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
104 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500105 // (shareIndex != 0) -> (shareContext != nullptr)
106 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
107
djsollene4545212014-11-13 11:12:41 -0800108 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800109 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800110 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700111 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500112 context.fShareContext == shareContext &&
113 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700114 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700115 context.fTestContext->makeCurrent();
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400116 return ContextInfo(context.fType, context.fTestContext, context.fGrContext);
djsollene4545212014-11-13 11:12:41 -0800117 }
118 }
Brian Osman60c774d2017-02-21 16:58:08 -0500119
120 // If we're trying to create a context in a share group, find the master context
121 Context* masterContext = nullptr;
122 if (shareContext) {
123 for (int i = 0; i < fContexts.count(); ++i) {
124 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
125 masterContext = &fContexts[i];
126 break;
127 }
128 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500129 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500130 }
131
Ben Wagner145dbcd2016-11-03 14:40:50 -0400132 std::unique_ptr<TestContext> testCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700133 GrBackendContext backendContext = 0;
134 sk_sp<const GrGLInterface> glInterface;
bsalomondc0fcd42016-04-11 14:21:33 -0700135 GrBackend backend = ContextTypeBackend(type);
136 switch (backend) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700137 case kOpenGL_GrBackend: {
Brian Osman60c774d2017-02-21 16:58:08 -0500138 GLTestContext* glShareContext = masterContext
139 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700140 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700141 switch (type) {
142 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500143 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700144 break;
145 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500146 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700147 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000148#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700149 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500150 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
151 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700152 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700153 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500154 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
155 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700156 break;
157 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500158 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
159 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700160 break;
161 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500162 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
163 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700164 break;
165 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500166 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
167 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700168 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000169#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400170#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700171 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500172 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700173 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400174#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800175#if SK_MESA
bsalomondc0fcd42016-04-11 14:21:33 -0700176 case kMESA_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500177 glCtx = CreateMesaGLTestContext(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700178 break;
djsollene4545212014-11-13 11:12:41 -0800179#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700180 case kNullGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500181 glCtx = CreateNullGLTestContext(
182 ContextOverrides::kRequireNVPRSupport & overrides, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700183 break;
184 case kDebugGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500185 glCtx = CreateDebugGLTestContext(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700186 break;
187 default:
188 return ContextInfo();
189 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700190 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700191 return ContextInfo();
192 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700193 testCtx.reset(glCtx);
bsalomondc0fcd42016-04-11 14:21:33 -0700194 glInterface.reset(SkRef(glCtx->gl()));
bsalomondc0fcd42016-04-11 14:21:33 -0700195 backendContext = reinterpret_cast<GrBackendContext>(glInterface.get());
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 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700220 backendContext = testCtx->backendContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700221 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400222 }
jvanvertha50e17a2015-08-12 12:19:36 -0700223#endif
Brian Salomoncfe910d2017-07-06 16:40:18 -0400224 case kMock_GrBackend: {
225 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
226 SkASSERT(kMock_ContextType == type);
227 if (ContextOverrides::kRequireNVPRSupport & overrides) {
228 return ContextInfo();
229 }
230 testCtx.reset(CreateMockTestContext(sharedContext));
231 if (!testCtx) {
232 return ContextInfo();
233 }
234 backendContext = testCtx->backendContext();
235 break;
236 }
bsalomondc0fcd42016-04-11 14:21:33 -0700237 default:
238 return ContextInfo();
239 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700240 testCtx->makeCurrent();
241 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700242 GrContextOptions grOptions = fGlobalOptions;
csmartdalton008b9d82017-02-22 12:00:42 -0700243 if (ContextOverrides::kDisableNVPR & overrides) {
244 grOptions.fSuppressPathRendering = true;
245 }
csmartdaltone812d492017-02-21 12:36:05 -0700246 if (ContextOverrides::kUseInstanced & overrides) {
csmartdaltone0d36292016-07-29 08:14:20 -0700247 grOptions.fEnableInstancedRendering = true;
248 }
csmartdaltone812d492017-02-21 12:36:05 -0700249 if (ContextOverrides::kAllowSRGBWithoutDecodeControl & overrides) {
250 grOptions.fRequireDecodeDisableForSRGB = false;
251 }
Eric Karl5c779752017-05-08 12:02:07 -0700252 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
253 grOptions.fAvoidStencilBuffers = true;
254 }
Brian Osman60c774d2017-02-21 16:58:08 -0500255 sk_sp<GrContext> grCtx(GrContext::Create(backend, backendContext, grOptions));
djsollene4545212014-11-13 11:12:41 -0800256 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800257 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800258 }
csmartdaltone812d492017-02-21 12:36:05 -0700259 if (ContextOverrides::kRequireNVPRSupport & overrides) {
kkinnunencfe62e32015-07-01 02:58:50 -0700260 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800261 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700262 }
263 }
csmartdaltone812d492017-02-21 12:36:05 -0700264 if (ContextOverrides::kUseInstanced & overrides) {
csmartdaltone0d36292016-07-29 08:14:20 -0700265 if (GrCaps::InstancedSupport::kNone == grCtx->caps()->instancedSupport()) {
266 return ContextInfo();
267 }
268 }
csmartdaltone812d492017-02-21 12:36:05 -0700269 if (ContextOverrides::kRequireSRGBSupport & overrides) {
brianosman61d3b082016-03-30 11:19:36 -0700270 if (!grCtx->caps()->srgbSupport()) {
271 return ContextInfo();
272 }
273 }
kkinnunencfe62e32015-07-01 02:58:50 -0700274
kkinnunen34058002016-01-06 23:49:30 -0800275 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700276 context.fBackend = backend;
277 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800278 context.fGrContext = SkRef(grCtx.get());
279 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700280 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700281 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500282 context.fShareContext = shareContext;
283 context.fShareIndex = shareIndex;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400284 return ContextInfo(context.fType, context.fTestContext, context.fGrContext);
djsollene4545212014-11-13 11:12:41 -0800285}
Brian Osman60c774d2017-02-21 16:58:08 -0500286
Brian Osman9eac2ea2017-02-24 14:51:44 -0500287ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
288 return this->getContextInfoInternal(type, overrides, nullptr, 0);
289}
290
291ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
292 SkASSERT(shareContext);
293 for (int i = 0; i < fContexts.count(); ++i) {
294 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
295 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
296 shareContext, shareIndex);
297 }
298 }
299
300 return ContextInfo();
301}
302
bsalomon3724e572016-03-30 18:56:19 -0700303} // namespace sk_gpu_test