blob: be33802f175eb8140c003f73aa950e3d5a77ab5c [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();
89 delete(context.fTestContext);
90 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070091 }
92 context.fGrContext->abandonContext();
93 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080094 }
kkinnunen34058002016-01-06 23:49:30 -080095 }
96}
97
bsalomon6e2aad42016-04-01 11:54:31 -070098void GrContextFactory::releaseResourcesAndAbandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050099 // We must abandon the test contexts in reverse order so that any child context is finished and
100 // abandoned before a parent context. This relies on the fact that when we make a new context we
101 // append it to the end of fContexts array.
102 // TODO: Look into keeping a dependency dag for contexts and deletion order
103 for (int i = fContexts.count() - 1; i >= 0; --i) {
104 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -0500105 SkScopeExit restore(nullptr);
bsalomondc0fcd42016-04-11 14:21:33 -0700106 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700107 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -0500108 restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomondc0fcd42016-04-11 14:21:33 -0700109 }
bsalomon6e2aad42016-04-01 11:54:31 -0700110 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700111 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -0700112 if (context.fTestContext) {
113 delete context.fTestContext;
114 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700115 }
bsalomon6e2aad42016-04-01 11:54:31 -0700116 }
117 }
118}
119
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400120GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
121 return this->getContextInfo(type, overrides).grContext();
122}
123
Brian Osman9eac2ea2017-02-24 14:51:44 -0500124ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
125 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500126 // (shareIndex != 0) -> (shareContext != nullptr)
127 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
128
djsollene4545212014-11-13 11:12:41 -0800129 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800130 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800131 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700132 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500133 context.fShareContext == shareContext &&
134 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700135 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700136 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400137 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
138 context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800139 }
140 }
Brian Osman60c774d2017-02-21 16:58:08 -0500141
142 // If we're trying to create a context in a share group, find the master context
143 Context* masterContext = nullptr;
144 if (shareContext) {
145 for (int i = 0; i < fContexts.count(); ++i) {
146 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
147 masterContext = &fContexts[i];
148 break;
149 }
150 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500151 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500152 }
153
Ben Wagner145dbcd2016-11-03 14:40:50 -0400154 std::unique_ptr<TestContext> testCtx;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400155 GrBackendApi backend = ContextTypeBackend(type);
bsalomondc0fcd42016-04-11 14:21:33 -0700156 switch (backend) {
John Rosascoa9b348f2019-11-08 13:18:15 -0800157#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400158 case GrBackendApi::kOpenGL: {
Brian Osman60c774d2017-02-21 16:58:08 -0500159 GLTestContext* glShareContext = masterContext
160 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700161 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700162 switch (type) {
163 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500164 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700165 break;
166 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500167 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700168 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000169#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700170 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500171 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
172 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700173 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700174 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500175 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
176 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700177 break;
178 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500179 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
180 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700181 break;
182 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500183 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
184 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700185 break;
186 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500187 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
188 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700189 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000190#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400191#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700192 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500193 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700194 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400195#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700196 default:
197 return ContextInfo();
198 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700199 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700200 return ContextInfo();
201 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700202 testCtx.reset(glCtx);
djsollene4545212014-11-13 11:12:41 -0800203 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700204 }
John Rosascoa9b348f2019-11-08 13:18:15 -0800205#endif // SK_GL
jvanvertha50e17a2015-08-12 12:19:36 -0700206#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400207 case GrBackendApi::kVulkan: {
Greg Daniel604b1972017-05-15 13:50:35 -0400208 VkTestContext* vkSharedContext = masterContext
209 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700210 SkASSERT(kVulkan_ContextType == type);
Greg Daniel604b1972017-05-15 13:50:35 -0400211 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700212 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700213 return ContextInfo();
214 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700215
John Rosascoa9b348f2019-11-08 13:18:15 -0800216#ifdef SK_GL
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000217 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
218 // destruction will hang occaisonally. For some reason having an existing GL
219 // context fixes this.
220 if (!fSentinelGLContext) {
221 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
222 if (!fSentinelGLContext) {
223 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
224 }
225 }
John Rosascoa9b348f2019-11-08 13:18:15 -0800226#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700227 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400228 }
jvanvertha50e17a2015-08-12 12:19:36 -0700229#endif
Greg Danielb76a72a2017-07-13 15:07:54 -0400230#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400231 case GrBackendApi::kMetal: {
Jim Van Vertha3407ab2019-03-15 15:22:39 -0400232 MtlTestContext* mtlSharedContext = masterContext
233 ? static_cast<MtlTestContext*>(masterContext->fTestContext) : nullptr;
234 SkASSERT(kMetal_ContextType == type);
235 testCtx.reset(CreatePlatformMtlTestContext(mtlSharedContext));
Greg Danielb76a72a2017-07-13 15:07:54 -0400236 if (!testCtx) {
237 return ContextInfo();
238 }
239 break;
240 }
241#endif
Stephen White985741a2019-07-18 11:43:45 -0400242#ifdef SK_DAWN
243 case GrBackendApi::kDawn: {
244 DawnTestContext* dawnSharedContext = masterContext
245 ? static_cast<DawnTestContext*>(masterContext->fTestContext) : nullptr;
246 testCtx.reset(CreatePlatformDawnTestContext(dawnSharedContext));
247 if (!testCtx) {
248 return ContextInfo();
249 }
250 break;
251 }
252#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400253 case GrBackendApi::kMock: {
Brian Salomoncfe910d2017-07-06 16:40:18 -0400254 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
255 SkASSERT(kMock_ContextType == type);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400256 testCtx.reset(CreateMockTestContext(sharedContext));
257 if (!testCtx) {
258 return ContextInfo();
259 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400260 break;
261 }
bsalomondc0fcd42016-04-11 14:21:33 -0700262 default:
263 return ContextInfo();
264 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500265
bsalomon18a2f9d2016-05-11 10:09:18 -0700266 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700267 GrContextOptions grOptions = fGlobalOptions;
Eric Karl5c779752017-05-08 12:02:07 -0700268 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
269 grOptions.fAvoidStencilBuffers = true;
270 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500271 sk_sp<GrContext> grCtx;
272 {
273 auto restore = testCtx->makeCurrentAndAutoRestore();
274 grCtx = testCtx->makeGrContext(grOptions);
275 }
djsollene4545212014-11-13 11:12:41 -0800276 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800277 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800278 }
kkinnunencfe62e32015-07-01 02:58:50 -0700279
Greg Danielf0fe02f2018-01-16 11:20:41 -0500280 // We must always add new contexts by pushing to the back so that when we delete them we delete
281 // them in reverse order in which they were made.
kkinnunen34058002016-01-06 23:49:30 -0800282 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700283 context.fBackend = backend;
284 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800285 context.fGrContext = SkRef(grCtx.get());
286 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700287 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700288 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500289 context.fShareContext = shareContext;
290 context.fShareIndex = shareIndex;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400291 context.fOptions = grOptions;
Brian Salomon55ad7742017-11-17 09:25:23 -0500292 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400293 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