blob: e5bea06002f4d840021adf7474f408bca108e8d9 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrContextPriv.h"
10#include "tools/gpu/GrContextFactory.h"
11#include "tools/gpu/gl/GLTestContext.h"
djsollene4545212014-11-13 11:12:41 -080012
Mike Kleinc168a3a2016-11-14 14:53:13 +000013#if SK_ANGLE
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014 #include "tools/gpu/gl/angle/GLTestContext_angle.h"
Mike Kleinc168a3a2016-11-14 14:53:13 +000015#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "tools/gpu/gl/command_buffer/GLTestContext_command_buffer.h"
djsollen7e731082016-06-09 13:07:13 -070017#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "tools/gpu/vk/VkTestContext.h"
bsalomon7c62b472016-04-01 07:42:05 -070019#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040020#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/gpu/mtl/MtlTestContext.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040022#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrCaps.h"
Robert Phillips08ba0852019-05-22 20:23:43 +000024#include "src/gpu/gl/GrGLGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/gpu/mock/MockTestContext.h"
djsollene4545212014-11-13 11:12:41 -080026
Mike Klein8f11d4d2018-01-24 12:42:55 -050027#if defined(SK_BUILD_FOR_WIN) && defined(SK_ENABLE_DISCRETE_GPU)
Brian Osman3f375d02016-12-28 11:19:22 -050028extern "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() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050053 // We must delete the test contexts in reverse order so that any child context is finished and
54 // deleted before a parent context. This relies on the fact that when we make a new context we
55 // append it to the end of fContexts array.
56 // TODO: Look into keeping a dependency dag for contexts and deletion order
57 for (int i = fContexts.count() - 1; i >= 0; --i) {
58 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -050059 SkScopeExit restore(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -070060 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050061 restore = context.fTestContext->makeCurrentAndAutoRestore();
kkinnunen34058002016-01-06 23:49:30 -080062 }
63 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070064 context.fGrContext->releaseResourcesAndAbandonContext();
65 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080066 }
67 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070068 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080069 }
70 fContexts.reset();
71}
72
73void GrContextFactory::abandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050074 // We must abandon the test contexts in reverse order so that any child context is finished and
75 // abandoned before a parent context. This relies on the fact that when we make a new context we
76 // append it to the end of fContexts array.
77 // TODO: Look into keeping a dependency dag for contexts and deletion order
78 for (int i = fContexts.count() - 1; i >= 0; --i) {
79 Context& context = fContexts[i];
bsalomondc0fcd42016-04-11 14:21:33 -070080 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070081 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050082 auto restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomon18a2f9d2016-05-11 10:09:18 -070083 context.fTestContext->testAbandon();
84 delete(context.fTestContext);
85 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070086 }
87 context.fGrContext->abandonContext();
88 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080089 }
kkinnunen34058002016-01-06 23:49:30 -080090 }
91}
92
bsalomon6e2aad42016-04-01 11:54:31 -070093void GrContextFactory::releaseResourcesAndAbandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050094 // We must abandon the test contexts in reverse order so that any child context is finished and
95 // abandoned before a parent context. This relies on the fact that when we make a new context we
96 // append it to the end of fContexts array.
97 // TODO: Look into keeping a dependency dag for contexts and deletion order
98 for (int i = fContexts.count() - 1; i >= 0; --i) {
99 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -0500100 SkScopeExit restore(nullptr);
bsalomondc0fcd42016-04-11 14:21:33 -0700101 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700102 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -0500103 restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomondc0fcd42016-04-11 14:21:33 -0700104 }
bsalomon6e2aad42016-04-01 11:54:31 -0700105 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700106 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -0700107 if (context.fTestContext) {
108 delete context.fTestContext;
109 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700110 }
bsalomon6e2aad42016-04-01 11:54:31 -0700111 }
112 }
113}
114
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400115GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
116 return this->getContextInfo(type, overrides).grContext();
117}
118
Brian Osman9eac2ea2017-02-24 14:51:44 -0500119ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
120 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500121 // (shareIndex != 0) -> (shareContext != nullptr)
122 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
123
djsollene4545212014-11-13 11:12:41 -0800124 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800125 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800126 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700127 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500128 context.fShareContext == shareContext &&
129 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700130 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700131 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400132 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
133 context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800134 }
135 }
Brian Osman60c774d2017-02-21 16:58:08 -0500136
137 // If we're trying to create a context in a share group, find the master context
138 Context* masterContext = nullptr;
139 if (shareContext) {
140 for (int i = 0; i < fContexts.count(); ++i) {
141 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
142 masterContext = &fContexts[i];
143 break;
144 }
145 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500146 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500147 }
148
Ben Wagner145dbcd2016-11-03 14:40:50 -0400149 std::unique_ptr<TestContext> testCtx;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400150 GrBackendApi backend = ContextTypeBackend(type);
bsalomondc0fcd42016-04-11 14:21:33 -0700151 switch (backend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400152 case GrBackendApi::kOpenGL: {
Brian Osman60c774d2017-02-21 16:58:08 -0500153 GLTestContext* glShareContext = masterContext
154 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700155 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700156 switch (type) {
157 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500158 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700159 break;
160 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500161 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700162 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000163#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700164 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500165 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
166 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700167 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700168 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500169 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
170 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700171 break;
172 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500173 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
174 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700175 break;
176 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500177 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
178 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700179 break;
180 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500181 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
182 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700183 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000184#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400185#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700186 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500187 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700188 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400189#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700190 default:
191 return ContextInfo();
192 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700193 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700194 return ContextInfo();
195 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700196 testCtx.reset(glCtx);
djsollene4545212014-11-13 11:12:41 -0800197 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700198 }
jvanvertha50e17a2015-08-12 12:19:36 -0700199#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400200 case GrBackendApi::kVulkan: {
Greg Daniel604b1972017-05-15 13:50:35 -0400201 VkTestContext* vkSharedContext = masterContext
202 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700203 SkASSERT(kVulkan_ContextType == type);
Greg Daniel604b1972017-05-15 13:50:35 -0400204 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700205 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700206 return ContextInfo();
207 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700208
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000209 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
210 // destruction will hang occaisonally. For some reason having an existing GL
211 // context fixes this.
212 if (!fSentinelGLContext) {
213 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
214 if (!fSentinelGLContext) {
215 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
216 }
217 }
bsalomondc0fcd42016-04-11 14:21:33 -0700218 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400219 }
jvanvertha50e17a2015-08-12 12:19:36 -0700220#endif
Greg Danielb76a72a2017-07-13 15:07:54 -0400221#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400222 case GrBackendApi::kMetal: {
Jim Van Vertha3407ab2019-03-15 15:22:39 -0400223 MtlTestContext* mtlSharedContext = masterContext
224 ? static_cast<MtlTestContext*>(masterContext->fTestContext) : nullptr;
225 SkASSERT(kMetal_ContextType == type);
226 testCtx.reset(CreatePlatformMtlTestContext(mtlSharedContext));
Greg Danielb76a72a2017-07-13 15:07:54 -0400227 if (!testCtx) {
228 return ContextInfo();
229 }
230 break;
231 }
232#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400233 case GrBackendApi::kMock: {
Brian Salomoncfe910d2017-07-06 16:40:18 -0400234 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
235 SkASSERT(kMock_ContextType == type);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400236 testCtx.reset(CreateMockTestContext(sharedContext));
237 if (!testCtx) {
238 return ContextInfo();
239 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400240 break;
241 }
bsalomondc0fcd42016-04-11 14:21:33 -0700242 default:
243 return ContextInfo();
244 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500245
bsalomon18a2f9d2016-05-11 10:09:18 -0700246 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700247 GrContextOptions grOptions = fGlobalOptions;
Eric Karl5c779752017-05-08 12:02:07 -0700248 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
249 grOptions.fAvoidStencilBuffers = true;
250 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500251 sk_sp<GrContext> grCtx;
252 {
253 auto restore = testCtx->makeCurrentAndAutoRestore();
254 grCtx = testCtx->makeGrContext(grOptions);
255 }
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 }
kkinnunencfe62e32015-07-01 02:58:50 -0700259
Greg Danielf0fe02f2018-01-16 11:20:41 -0500260 // We must always add new contexts by pushing to the back so that when we delete them we delete
261 // them in reverse order in which they were made.
kkinnunen34058002016-01-06 23:49:30 -0800262 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700263 context.fBackend = backend;
264 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800265 context.fGrContext = SkRef(grCtx.get());
266 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700267 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700268 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500269 context.fShareContext = shareContext;
270 context.fShareIndex = shareIndex;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400271 context.fOptions = grOptions;
Brian Salomon55ad7742017-11-17 09:25:23 -0500272 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400273 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800274}
Brian Osman60c774d2017-02-21 16:58:08 -0500275
Brian Osman9eac2ea2017-02-24 14:51:44 -0500276ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
277 return this->getContextInfoInternal(type, overrides, nullptr, 0);
278}
279
280ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
281 SkASSERT(shareContext);
282 for (int i = 0; i < fContexts.count(); ++i) {
283 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
284 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
285 shareContext, shareIndex);
286 }
287 }
288
289 return ContextInfo();
290}
291
bsalomon3724e572016-03-30 18:56:19 -0700292} // namespace sk_gpu_test