blob: 4432c87fbe07dccceb5b8f332f079d59b9e99870 [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"
John Rosascoa9b348f2019-11-08 13:18:15 -080011#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "tools/gpu/gl/GLTestContext.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080013#endif
djsollene4545212014-11-13 11:12:41 -080014
Mike Kleinc168a3a2016-11-14 14:53:13 +000015#if SK_ANGLE
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016 #include "tools/gpu/gl/angle/GLTestContext_angle.h"
Mike Kleinc168a3a2016-11-14 14:53:13 +000017#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "tools/gpu/gl/command_buffer/GLTestContext_command_buffer.h"
djsollen7e731082016-06-09 13:07:13 -070019#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "tools/gpu/vk/VkTestContext.h"
bsalomon7c62b472016-04-01 07:42:05 -070021#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040022#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/gpu/mtl/MtlTestContext.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040024#endif
Stephen White985741a2019-07-18 11:43:45 -040025#ifdef SK_DAWN
26#include "tools/gpu/dawn/DawnTestContext.h"
27#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrCaps.h"
Robert Phillips08ba0852019-05-22 20:23:43 +000029#include "src/gpu/gl/GrGLGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "tools/gpu/mock/MockTestContext.h"
djsollene4545212014-11-13 11:12:41 -080031
Mike Klein8f11d4d2018-01-24 12:42:55 -050032#if defined(SK_BUILD_FOR_WIN) && defined(SK_ENABLE_DISCRETE_GPU)
Brian Osman3f375d02016-12-28 11:19:22 -050033extern "C" {
34 // NVIDIA documents that the presence and value of this symbol programmatically enable the high
35 // performance GPU in laptops with switchable graphics.
36 // https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm
37 // From testing, including this symbol, even if it is set to 0, we still get the NVIDIA GPU.
38 _declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
39
40 // AMD has a similar mechanism, although I don't have an AMD laptop, so this is untested.
41 // https://community.amd.com/thread/169965
42 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
43}
44#endif
45
bsalomon3724e572016-03-30 18:56:19 -070046namespace sk_gpu_test {
kkinnunen34058002016-01-06 23:49:30 -080047GrContextFactory::GrContextFactory() { }
48
49GrContextFactory::GrContextFactory(const GrContextOptions& opts)
50 : fGlobalOptions(opts) {
51}
52
53GrContextFactory::~GrContextFactory() {
54 this->destroyContexts();
55}
56
57void GrContextFactory::destroyContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050058 // We must delete the test contexts in reverse order so that any child context is finished and
59 // deleted before a parent context. This relies on the fact that when we make a new context we
60 // append it to the end of fContexts array.
61 // TODO: Look into keeping a dependency dag for contexts and deletion order
62 for (int i = fContexts.count() - 1; i >= 0; --i) {
63 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -050064 SkScopeExit restore(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -070065 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050066 restore = context.fTestContext->makeCurrentAndAutoRestore();
kkinnunen34058002016-01-06 23:49:30 -080067 }
68 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070069 context.fGrContext->releaseResourcesAndAbandonContext();
70 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080071 }
72 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070073 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080074 }
75 fContexts.reset();
76}
77
78void GrContextFactory::abandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050079 // We must abandon the test contexts in reverse order so that any child context is finished and
80 // abandoned before a parent context. This relies on the fact that when we make a new context we
81 // append it to the end of fContexts array.
82 // TODO: Look into keeping a dependency dag for contexts and deletion order
83 for (int i = fContexts.count() - 1; i >= 0; --i) {
84 Context& context = fContexts[i];
bsalomondc0fcd42016-04-11 14:21:33 -070085 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070086 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050087 auto restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomon18a2f9d2016-05-11 10:09:18 -070088 context.fTestContext->testAbandon();
Greg Danielf0e04f02019-12-04 15:17:54 -050089 }
90 bool requiresEarlyAbandon = (context.fGrContext->backend() == GrBackendApi::kVulkan);
91 if (requiresEarlyAbandon) {
92 context.fGrContext->abandonContext();
93 }
94 if (context.fTestContext) {
bsalomon18a2f9d2016-05-11 10:09:18 -070095 delete(context.fTestContext);
96 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070097 }
Greg Danielf0e04f02019-12-04 15:17:54 -050098 if (!requiresEarlyAbandon) {
99 context.fGrContext->abandonContext();
100 }
bsalomondc0fcd42016-04-11 14:21:33 -0700101 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -0800102 }
kkinnunen34058002016-01-06 23:49:30 -0800103 }
104}
105
bsalomon6e2aad42016-04-01 11:54:31 -0700106void GrContextFactory::releaseResourcesAndAbandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -0500107 // We must abandon the test contexts in reverse order so that any child context is finished and
108 // abandoned before a parent context. This relies on the fact that when we make a new context we
109 // append it to the end of fContexts array.
110 // TODO: Look into keeping a dependency dag for contexts and deletion order
111 for (int i = fContexts.count() - 1; i >= 0; --i) {
112 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -0500113 SkScopeExit restore(nullptr);
bsalomondc0fcd42016-04-11 14:21:33 -0700114 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700115 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -0500116 restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomondc0fcd42016-04-11 14:21:33 -0700117 }
bsalomon6e2aad42016-04-01 11:54:31 -0700118 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomon18a2f9d2016-05-11 10:09:18 -0700119 if (context.fTestContext) {
120 delete context.fTestContext;
121 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700122 }
Greg Danielf0e04f02019-12-04 15:17:54 -0500123 context.fAbandoned = true;
bsalomon6e2aad42016-04-01 11:54:31 -0700124 }
125 }
126}
127
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400128GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
129 return this->getContextInfo(type, overrides).grContext();
130}
131
Brian Osman9eac2ea2017-02-24 14:51:44 -0500132ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
133 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500134 // (shareIndex != 0) -> (shareContext != nullptr)
135 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
136
djsollene4545212014-11-13 11:12:41 -0800137 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800138 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800139 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700140 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500141 context.fShareContext == shareContext &&
142 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700143 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700144 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400145 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
146 context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800147 }
148 }
Brian Osman60c774d2017-02-21 16:58:08 -0500149
150 // If we're trying to create a context in a share group, find the master context
151 Context* masterContext = nullptr;
152 if (shareContext) {
153 for (int i = 0; i < fContexts.count(); ++i) {
154 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
155 masterContext = &fContexts[i];
156 break;
157 }
158 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500159 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500160 }
161
Ben Wagner145dbcd2016-11-03 14:40:50 -0400162 std::unique_ptr<TestContext> testCtx;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400163 GrBackendApi backend = ContextTypeBackend(type);
bsalomondc0fcd42016-04-11 14:21:33 -0700164 switch (backend) {
John Rosascoa9b348f2019-11-08 13:18:15 -0800165#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400166 case GrBackendApi::kOpenGL: {
Brian Osman60c774d2017-02-21 16:58:08 -0500167 GLTestContext* glShareContext = masterContext
168 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700169 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700170 switch (type) {
171 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500172 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700173 break;
174 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500175 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700176 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000177#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700178 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500179 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
180 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700181 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700182 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500183 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
184 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700185 break;
186 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500187 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
188 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700189 break;
190 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500191 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
192 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700193 break;
194 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500195 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
196 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700197 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000198#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400199#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700200 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500201 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700202 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400203#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700204 default:
205 return ContextInfo();
206 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700207 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700208 return ContextInfo();
209 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700210 testCtx.reset(glCtx);
djsollene4545212014-11-13 11:12:41 -0800211 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700212 }
John Rosascoa9b348f2019-11-08 13:18:15 -0800213#endif // SK_GL
jvanvertha50e17a2015-08-12 12:19:36 -0700214#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400215 case GrBackendApi::kVulkan: {
Greg Daniel604b1972017-05-15 13:50:35 -0400216 VkTestContext* vkSharedContext = masterContext
217 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700218 SkASSERT(kVulkan_ContextType == type);
Greg Daniel604b1972017-05-15 13:50:35 -0400219 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700220 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700221 return ContextInfo();
222 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700223
John Rosascoa9b348f2019-11-08 13:18:15 -0800224#ifdef SK_GL
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000225 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
226 // destruction will hang occaisonally. For some reason having an existing GL
227 // context fixes this.
228 if (!fSentinelGLContext) {
229 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
230 if (!fSentinelGLContext) {
231 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
232 }
233 }
John Rosascoa9b348f2019-11-08 13:18:15 -0800234#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700235 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400236 }
jvanvertha50e17a2015-08-12 12:19:36 -0700237#endif
Greg Danielb76a72a2017-07-13 15:07:54 -0400238#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400239 case GrBackendApi::kMetal: {
Jim Van Vertha3407ab2019-03-15 15:22:39 -0400240 MtlTestContext* mtlSharedContext = masterContext
241 ? static_cast<MtlTestContext*>(masterContext->fTestContext) : nullptr;
242 SkASSERT(kMetal_ContextType == type);
243 testCtx.reset(CreatePlatformMtlTestContext(mtlSharedContext));
Greg Danielb76a72a2017-07-13 15:07:54 -0400244 if (!testCtx) {
245 return ContextInfo();
246 }
247 break;
248 }
249#endif
Stephen White985741a2019-07-18 11:43:45 -0400250#ifdef SK_DAWN
251 case GrBackendApi::kDawn: {
252 DawnTestContext* dawnSharedContext = masterContext
253 ? static_cast<DawnTestContext*>(masterContext->fTestContext) : nullptr;
254 testCtx.reset(CreatePlatformDawnTestContext(dawnSharedContext));
255 if (!testCtx) {
256 return ContextInfo();
257 }
258 break;
259 }
260#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400261 case GrBackendApi::kMock: {
Brian Salomoncfe910d2017-07-06 16:40:18 -0400262 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
263 SkASSERT(kMock_ContextType == type);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400264 testCtx.reset(CreateMockTestContext(sharedContext));
265 if (!testCtx) {
266 return ContextInfo();
267 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400268 break;
269 }
bsalomondc0fcd42016-04-11 14:21:33 -0700270 default:
271 return ContextInfo();
272 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500273
bsalomon18a2f9d2016-05-11 10:09:18 -0700274 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700275 GrContextOptions grOptions = fGlobalOptions;
Eric Karl5c779752017-05-08 12:02:07 -0700276 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
277 grOptions.fAvoidStencilBuffers = true;
278 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500279 sk_sp<GrContext> grCtx;
280 {
281 auto restore = testCtx->makeCurrentAndAutoRestore();
282 grCtx = testCtx->makeGrContext(grOptions);
283 }
djsollene4545212014-11-13 11:12:41 -0800284 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800285 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800286 }
kkinnunencfe62e32015-07-01 02:58:50 -0700287
Greg Danielf0fe02f2018-01-16 11:20:41 -0500288 // We must always add new contexts by pushing to the back so that when we delete them we delete
289 // them in reverse order in which they were made.
kkinnunen34058002016-01-06 23:49:30 -0800290 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700291 context.fBackend = backend;
292 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800293 context.fGrContext = SkRef(grCtx.get());
294 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700295 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700296 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500297 context.fShareContext = shareContext;
298 context.fShareIndex = shareIndex;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400299 context.fOptions = grOptions;
Brian Salomon55ad7742017-11-17 09:25:23 -0500300 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400301 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800302}
Brian Osman60c774d2017-02-21 16:58:08 -0500303
Brian Osman9eac2ea2017-02-24 14:51:44 -0500304ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
305 return this->getContextInfoInternal(type, overrides, nullptr, 0);
306}
307
308ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
309 SkASSERT(shareContext);
310 for (int i = 0; i < fContexts.count(); ++i) {
311 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
312 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
313 shareContext, shareIndex);
314 }
315 }
316
317 return ContextInfo();
318}
319
bsalomon3724e572016-03-30 18:56:19 -0700320} // namespace sk_gpu_test