blob: d0defdc6e932866c1aedce1575efd4801f5732eb [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
Stephen White985741a2019-07-18 11:43:45 -040023#ifdef SK_DAWN
24#include "tools/gpu/dawn/DawnTestContext.h"
25#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrCaps.h"
Robert Phillips08ba0852019-05-22 20:23:43 +000027#include "src/gpu/gl/GrGLGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "tools/gpu/mock/MockTestContext.h"
djsollene4545212014-11-13 11:12:41 -080029
Mike Klein8f11d4d2018-01-24 12:42:55 -050030#if defined(SK_BUILD_FOR_WIN) && defined(SK_ENABLE_DISCRETE_GPU)
Brian Osman3f375d02016-12-28 11:19:22 -050031extern "C" {
32 // NVIDIA documents that the presence and value of this symbol programmatically enable the high
33 // performance GPU in laptops with switchable graphics.
34 // https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm
35 // From testing, including this symbol, even if it is set to 0, we still get the NVIDIA GPU.
36 _declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
37
38 // AMD has a similar mechanism, although I don't have an AMD laptop, so this is untested.
39 // https://community.amd.com/thread/169965
40 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
41}
42#endif
43
bsalomon3724e572016-03-30 18:56:19 -070044namespace sk_gpu_test {
kkinnunen34058002016-01-06 23:49:30 -080045GrContextFactory::GrContextFactory() { }
46
47GrContextFactory::GrContextFactory(const GrContextOptions& opts)
48 : fGlobalOptions(opts) {
49}
50
51GrContextFactory::~GrContextFactory() {
52 this->destroyContexts();
53}
54
55void GrContextFactory::destroyContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050056 // We must delete the test contexts in reverse order so that any child context is finished and
57 // deleted before a parent context. This relies on the fact that when we make a new context we
58 // append it to the end of fContexts array.
59 // TODO: Look into keeping a dependency dag for contexts and deletion order
60 for (int i = fContexts.count() - 1; i >= 0; --i) {
61 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -050062 SkScopeExit restore(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -070063 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050064 restore = context.fTestContext->makeCurrentAndAutoRestore();
kkinnunen34058002016-01-06 23:49:30 -080065 }
66 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070067 context.fGrContext->releaseResourcesAndAbandonContext();
68 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080069 }
70 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070071 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080072 }
73 fContexts.reset();
74}
75
76void GrContextFactory::abandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050077 // We must abandon the test contexts in reverse order so that any child context is finished and
78 // abandoned before a parent context. This relies on the fact that when we make a new context we
79 // append it to the end of fContexts array.
80 // TODO: Look into keeping a dependency dag for contexts and deletion order
81 for (int i = fContexts.count() - 1; i >= 0; --i) {
82 Context& context = fContexts[i];
bsalomondc0fcd42016-04-11 14:21:33 -070083 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070084 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050085 auto restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomon18a2f9d2016-05-11 10:09:18 -070086 context.fTestContext->testAbandon();
87 delete(context.fTestContext);
88 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070089 }
90 context.fGrContext->abandonContext();
91 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080092 }
kkinnunen34058002016-01-06 23:49:30 -080093 }
94}
95
bsalomon6e2aad42016-04-01 11:54:31 -070096void GrContextFactory::releaseResourcesAndAbandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050097 // We must abandon the test contexts in reverse order so that any child context is finished and
98 // abandoned before a parent context. This relies on the fact that when we make a new context we
99 // append it to the end of fContexts array.
100 // TODO: Look into keeping a dependency dag for contexts and deletion order
101 for (int i = fContexts.count() - 1; i >= 0; --i) {
102 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -0500103 SkScopeExit restore(nullptr);
bsalomondc0fcd42016-04-11 14:21:33 -0700104 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700105 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -0500106 restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomondc0fcd42016-04-11 14:21:33 -0700107 }
bsalomon6e2aad42016-04-01 11:54:31 -0700108 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700109 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -0700110 if (context.fTestContext) {
111 delete context.fTestContext;
112 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700113 }
bsalomon6e2aad42016-04-01 11:54:31 -0700114 }
115 }
116}
117
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400118GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
119 return this->getContextInfo(type, overrides).grContext();
120}
121
Brian Osman9eac2ea2017-02-24 14:51:44 -0500122ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
123 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500124 // (shareIndex != 0) -> (shareContext != nullptr)
125 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
126
djsollene4545212014-11-13 11:12:41 -0800127 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800128 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800129 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700130 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500131 context.fShareContext == shareContext &&
132 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700133 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700134 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400135 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
136 context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800137 }
138 }
Brian Osman60c774d2017-02-21 16:58:08 -0500139
140 // If we're trying to create a context in a share group, find the master context
141 Context* masterContext = nullptr;
142 if (shareContext) {
143 for (int i = 0; i < fContexts.count(); ++i) {
144 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
145 masterContext = &fContexts[i];
146 break;
147 }
148 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500149 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500150 }
151
Ben Wagner145dbcd2016-11-03 14:40:50 -0400152 std::unique_ptr<TestContext> testCtx;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400153 GrBackendApi backend = ContextTypeBackend(type);
bsalomondc0fcd42016-04-11 14:21:33 -0700154 switch (backend) {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400155 case GrBackendApi::kOpenGL: {
Brian Osman60c774d2017-02-21 16:58:08 -0500156 GLTestContext* glShareContext = masterContext
157 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700158 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700159 switch (type) {
160 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500161 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700162 break;
163 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500164 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700165 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000166#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700167 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500168 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
169 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700170 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700171 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500172 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
173 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700174 break;
175 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500176 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
177 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700178 break;
179 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500180 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
181 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700182 break;
183 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500184 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
185 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700186 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000187#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400188#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700189 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500190 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700191 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400192#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700193 default:
194 return ContextInfo();
195 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700196 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700197 return ContextInfo();
198 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700199 testCtx.reset(glCtx);
djsollene4545212014-11-13 11:12:41 -0800200 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700201 }
jvanvertha50e17a2015-08-12 12:19:36 -0700202#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400203 case GrBackendApi::kVulkan: {
Greg Daniel604b1972017-05-15 13:50:35 -0400204 VkTestContext* vkSharedContext = masterContext
205 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700206 SkASSERT(kVulkan_ContextType == type);
Greg Daniel604b1972017-05-15 13:50:35 -0400207 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700208 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700209 return ContextInfo();
210 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700211
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000212 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
213 // destruction will hang occaisonally. For some reason having an existing GL
214 // context fixes this.
215 if (!fSentinelGLContext) {
216 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
217 if (!fSentinelGLContext) {
218 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
219 }
220 }
bsalomondc0fcd42016-04-11 14:21:33 -0700221 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400222 }
jvanvertha50e17a2015-08-12 12:19:36 -0700223#endif
Greg Danielb76a72a2017-07-13 15:07:54 -0400224#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400225 case GrBackendApi::kMetal: {
Jim Van Vertha3407ab2019-03-15 15:22:39 -0400226 MtlTestContext* mtlSharedContext = masterContext
227 ? static_cast<MtlTestContext*>(masterContext->fTestContext) : nullptr;
228 SkASSERT(kMetal_ContextType == type);
229 testCtx.reset(CreatePlatformMtlTestContext(mtlSharedContext));
Greg Danielb76a72a2017-07-13 15:07:54 -0400230 if (!testCtx) {
231 return ContextInfo();
232 }
233 break;
234 }
235#endif
Stephen White985741a2019-07-18 11:43:45 -0400236#ifdef SK_DAWN
237 case GrBackendApi::kDawn: {
238 DawnTestContext* dawnSharedContext = masterContext
239 ? static_cast<DawnTestContext*>(masterContext->fTestContext) : nullptr;
240 testCtx.reset(CreatePlatformDawnTestContext(dawnSharedContext));
241 if (!testCtx) {
242 return ContextInfo();
243 }
244 break;
245 }
246#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400247 case GrBackendApi::kMock: {
Brian Salomoncfe910d2017-07-06 16:40:18 -0400248 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
249 SkASSERT(kMock_ContextType == type);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400250 testCtx.reset(CreateMockTestContext(sharedContext));
251 if (!testCtx) {
252 return ContextInfo();
253 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400254 break;
255 }
bsalomondc0fcd42016-04-11 14:21:33 -0700256 default:
257 return ContextInfo();
258 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500259
bsalomon18a2f9d2016-05-11 10:09:18 -0700260 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700261 GrContextOptions grOptions = fGlobalOptions;
Eric Karl5c779752017-05-08 12:02:07 -0700262 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
263 grOptions.fAvoidStencilBuffers = true;
264 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500265 sk_sp<GrContext> grCtx;
266 {
267 auto restore = testCtx->makeCurrentAndAutoRestore();
268 grCtx = testCtx->makeGrContext(grOptions);
269 }
djsollene4545212014-11-13 11:12:41 -0800270 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800271 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800272 }
kkinnunencfe62e32015-07-01 02:58:50 -0700273
Greg Danielf0fe02f2018-01-16 11:20:41 -0500274 // We must always add new contexts by pushing to the back so that when we delete them we delete
275 // them in reverse order in which they were made.
kkinnunen34058002016-01-06 23:49:30 -0800276 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700277 context.fBackend = backend;
278 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800279 context.fGrContext = SkRef(grCtx.get());
280 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700281 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700282 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500283 context.fShareContext = shareContext;
284 context.fShareIndex = shareIndex;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400285 context.fOptions = grOptions;
Brian Salomon55ad7742017-11-17 09:25:23 -0500286 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400287 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800288}
Brian Osman60c774d2017-02-21 16:58:08 -0500289
Brian Osman9eac2ea2017-02-24 14:51:44 -0500290ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
291 return this->getContextInfoInternal(type, overrides, nullptr, 0);
292}
293
294ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
295 SkASSERT(shareContext);
296 for (int i = 0; i < fContexts.count(); ++i) {
297 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
298 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
299 shareContext, shareIndex);
300 }
301 }
302
303 return ContextInfo();
304}
305
bsalomon3724e572016-03-30 18:56:19 -0700306} // namespace sk_gpu_test