blob: f7356f57fb87324e4f479d469dee0df09407ed77 [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
Jim Van Verthb01e12b2020-02-18 14:34:38 -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
Jim Van Verthb01e12b2020-02-18 14:34:38 -050025#ifdef SK_DIRECT3D
26#include "tools/gpu/d3d/D3DTestContext.h"
27#endif
Stephen White985741a2019-07-18 11:43:45 -040028#ifdef SK_DAWN
29#include "tools/gpu/dawn/DawnTestContext.h"
30#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrCaps.h"
Robert Phillips08ba0852019-05-22 20:23:43 +000032#include "src/gpu/gl/GrGLGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "tools/gpu/mock/MockTestContext.h"
djsollene4545212014-11-13 11:12:41 -080034
Mike Klein8f11d4d2018-01-24 12:42:55 -050035#if defined(SK_BUILD_FOR_WIN) && defined(SK_ENABLE_DISCRETE_GPU)
Brian Osman3f375d02016-12-28 11:19:22 -050036extern "C" {
37 // NVIDIA documents that the presence and value of this symbol programmatically enable the high
38 // performance GPU in laptops with switchable graphics.
39 // https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm
40 // From testing, including this symbol, even if it is set to 0, we still get the NVIDIA GPU.
41 _declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
42
43 // AMD has a similar mechanism, although I don't have an AMD laptop, so this is untested.
44 // https://community.amd.com/thread/169965
45 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
46}
47#endif
48
bsalomon3724e572016-03-30 18:56:19 -070049namespace sk_gpu_test {
kkinnunen34058002016-01-06 23:49:30 -080050GrContextFactory::GrContextFactory() { }
51
52GrContextFactory::GrContextFactory(const GrContextOptions& opts)
53 : fGlobalOptions(opts) {
54}
55
56GrContextFactory::~GrContextFactory() {
57 this->destroyContexts();
58}
59
60void GrContextFactory::destroyContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050061 // We must delete the test contexts in reverse order so that any child context is finished and
62 // deleted before a parent context. This relies on the fact that when we make a new context we
63 // append it to the end of fContexts array.
64 // TODO: Look into keeping a dependency dag for contexts and deletion order
65 for (int i = fContexts.count() - 1; i >= 0; --i) {
66 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -050067 SkScopeExit restore(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -070068 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050069 restore = context.fTestContext->makeCurrentAndAutoRestore();
kkinnunen34058002016-01-06 23:49:30 -080070 }
71 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070072 context.fGrContext->releaseResourcesAndAbandonContext();
73 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080074 }
75 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070076 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080077 }
78 fContexts.reset();
79}
80
81void GrContextFactory::abandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050082 // We must abandon the test contexts in reverse order so that any child context is finished and
83 // abandoned before a parent context. This relies on the fact that when we make a new context we
84 // append it to the end of fContexts array.
85 // TODO: Look into keeping a dependency dag for contexts and deletion order
86 for (int i = fContexts.count() - 1; i >= 0; --i) {
87 Context& context = fContexts[i];
bsalomondc0fcd42016-04-11 14:21:33 -070088 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070089 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050090 auto restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomon18a2f9d2016-05-11 10:09:18 -070091 context.fTestContext->testAbandon();
Greg Danielf0e04f02019-12-04 15:17:54 -050092 }
Stephen White37a46d22020-06-05 11:42:39 -040093 GrBackendApi api = context.fGrContext->backend();
94 bool requiresEarlyAbandon = api == GrBackendApi::kVulkan || api == GrBackendApi::kDawn;
Greg Danielf0e04f02019-12-04 15:17:54 -050095 if (requiresEarlyAbandon) {
96 context.fGrContext->abandonContext();
97 }
98 if (context.fTestContext) {
bsalomon18a2f9d2016-05-11 10:09:18 -070099 delete(context.fTestContext);
100 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700101 }
Greg Danielf0e04f02019-12-04 15:17:54 -0500102 if (!requiresEarlyAbandon) {
103 context.fGrContext->abandonContext();
104 }
bsalomondc0fcd42016-04-11 14:21:33 -0700105 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -0800106 }
kkinnunen34058002016-01-06 23:49:30 -0800107 }
108}
109
bsalomon6e2aad42016-04-01 11:54:31 -0700110void GrContextFactory::releaseResourcesAndAbandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -0500111 // We must abandon the test contexts in reverse order so that any child context is finished and
112 // abandoned before a parent context. This relies on the fact that when we make a new context we
113 // append it to the end of fContexts array.
114 // TODO: Look into keeping a dependency dag for contexts and deletion order
115 for (int i = fContexts.count() - 1; i >= 0; --i) {
116 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -0500117 SkScopeExit restore(nullptr);
bsalomondc0fcd42016-04-11 14:21:33 -0700118 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700119 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -0500120 restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomondc0fcd42016-04-11 14:21:33 -0700121 }
bsalomon6e2aad42016-04-01 11:54:31 -0700122 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomon18a2f9d2016-05-11 10:09:18 -0700123 if (context.fTestContext) {
124 delete context.fTestContext;
125 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700126 }
Greg Danielf0e04f02019-12-04 15:17:54 -0500127 context.fAbandoned = true;
bsalomon6e2aad42016-04-01 11:54:31 -0700128 }
129 }
130}
131
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400132GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
133 return this->getContextInfo(type, overrides).grContext();
134}
135
Brian Osman9eac2ea2017-02-24 14:51:44 -0500136ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
137 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500138 // (shareIndex != 0) -> (shareContext != nullptr)
139 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
140
djsollene4545212014-11-13 11:12:41 -0800141 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800142 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800143 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700144 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500145 context.fShareContext == shareContext &&
146 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700147 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700148 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400149 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
150 context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800151 }
152 }
Brian Osman60c774d2017-02-21 16:58:08 -0500153
154 // If we're trying to create a context in a share group, find the master context
155 Context* masterContext = nullptr;
156 if (shareContext) {
157 for (int i = 0; i < fContexts.count(); ++i) {
158 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
159 masterContext = &fContexts[i];
160 break;
161 }
162 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500163 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500164 }
165
Ben Wagner145dbcd2016-11-03 14:40:50 -0400166 std::unique_ptr<TestContext> testCtx;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400167 GrBackendApi backend = ContextTypeBackend(type);
bsalomondc0fcd42016-04-11 14:21:33 -0700168 switch (backend) {
John Rosascoa9b348f2019-11-08 13:18:15 -0800169#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400170 case GrBackendApi::kOpenGL: {
Brian Osman60c774d2017-02-21 16:58:08 -0500171 GLTestContext* glShareContext = masterContext
172 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700173 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700174 switch (type) {
175 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500176 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700177 break;
178 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500179 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700180 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000181#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700182 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500183 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
184 glShareContext).release();
Brian Salomon414782d2020-04-17 09:34:17 -0400185 // Chrome will only run on D3D9 with NVIDIA for 2012 and earlier drivers.
186 // (<= 269.73). We get shader link failures when testing on recent drivers
187 // using this backend.
188 if (glCtx) {
189 auto [backend, vendor, renderer] = GrGLGetANGLEInfo(glCtx->gl());
190 if (vendor == GrGLANGLEVendor::kNVIDIA) {
191 delete glCtx;
192 return ContextInfo();
193 }
194 }
bsalomondc0fcd42016-04-11 14:21:33 -0700195 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700196 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500197 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
198 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700199 break;
200 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500201 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
202 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700203 break;
204 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500205 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
206 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700207 break;
208 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500209 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
210 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700211 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000212#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400213#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700214 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500215 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700216 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400217#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700218 default:
219 return ContextInfo();
220 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700221 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700222 return ContextInfo();
223 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700224 testCtx.reset(glCtx);
djsollene4545212014-11-13 11:12:41 -0800225 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700226 }
John Rosascoa9b348f2019-11-08 13:18:15 -0800227#endif // SK_GL
jvanvertha50e17a2015-08-12 12:19:36 -0700228#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400229 case GrBackendApi::kVulkan: {
Greg Daniel604b1972017-05-15 13:50:35 -0400230 VkTestContext* vkSharedContext = masterContext
231 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700232 SkASSERT(kVulkan_ContextType == type);
Greg Daniel604b1972017-05-15 13:50:35 -0400233 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700234 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700235 return ContextInfo();
236 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700237
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400238 // We previously had an issue where the VkDevice destruction would occasionally hang
239 // on systems with NVIDIA GPUs and having an existing GL context fixed it. Now (March
240 // 2020) we still need the GL context to keep Vulkan/TSAN bots from running incredibly
241 // slow. Perhaps this prevents repeated driver loading/unloading? Note that keeping
242 // a persistent VkTestContext around instead was tried and did not work.
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000243 if (!fSentinelGLContext) {
244 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
245 if (!fSentinelGLContext) {
246 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
247 }
248 }
bsalomondc0fcd42016-04-11 14:21:33 -0700249 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400250 }
jvanvertha50e17a2015-08-12 12:19:36 -0700251#endif
Greg Danielb76a72a2017-07-13 15:07:54 -0400252#ifdef SK_METAL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400253 case GrBackendApi::kMetal: {
Jim Van Vertha3407ab2019-03-15 15:22:39 -0400254 MtlTestContext* mtlSharedContext = masterContext
255 ? static_cast<MtlTestContext*>(masterContext->fTestContext) : nullptr;
256 SkASSERT(kMetal_ContextType == type);
257 testCtx.reset(CreatePlatformMtlTestContext(mtlSharedContext));
Greg Danielb76a72a2017-07-13 15:07:54 -0400258 if (!testCtx) {
259 return ContextInfo();
260 }
261 break;
262 }
263#endif
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500264#ifdef SK_DIRECT3D
265 case GrBackendApi::kDirect3D: {
266 D3DTestContext* d3dSharedContext = masterContext
267 ? static_cast<D3DTestContext*>(masterContext->fTestContext) : nullptr;
268 SkASSERT(kDirect3D_ContextType == type);
269 testCtx.reset(CreatePlatformD3DTestContext(d3dSharedContext));
270 if (!testCtx) {
271 return ContextInfo();
272 }
273 break;
274 }
275#endif
Stephen White985741a2019-07-18 11:43:45 -0400276#ifdef SK_DAWN
277 case GrBackendApi::kDawn: {
278 DawnTestContext* dawnSharedContext = masterContext
279 ? static_cast<DawnTestContext*>(masterContext->fTestContext) : nullptr;
280 testCtx.reset(CreatePlatformDawnTestContext(dawnSharedContext));
281 if (!testCtx) {
282 return ContextInfo();
283 }
284 break;
285 }
286#endif
Greg Danielbdf12ad2018-10-12 09:31:11 -0400287 case GrBackendApi::kMock: {
Brian Salomoncfe910d2017-07-06 16:40:18 -0400288 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
289 SkASSERT(kMock_ContextType == type);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400290 testCtx.reset(CreateMockTestContext(sharedContext));
291 if (!testCtx) {
292 return ContextInfo();
293 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400294 break;
295 }
bsalomondc0fcd42016-04-11 14:21:33 -0700296 default:
297 return ContextInfo();
298 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500299
bsalomon18a2f9d2016-05-11 10:09:18 -0700300 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700301 GrContextOptions grOptions = fGlobalOptions;
Eric Karl5c779752017-05-08 12:02:07 -0700302 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
303 grOptions.fAvoidStencilBuffers = true;
304 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500305 sk_sp<GrContext> grCtx;
306 {
307 auto restore = testCtx->makeCurrentAndAutoRestore();
308 grCtx = testCtx->makeGrContext(grOptions);
309 }
djsollene4545212014-11-13 11:12:41 -0800310 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800311 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800312 }
kkinnunencfe62e32015-07-01 02:58:50 -0700313
Greg Danielf0fe02f2018-01-16 11:20:41 -0500314 // We must always add new contexts by pushing to the back so that when we delete them we delete
315 // them in reverse order in which they were made.
kkinnunen34058002016-01-06 23:49:30 -0800316 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700317 context.fBackend = backend;
318 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800319 context.fGrContext = SkRef(grCtx.get());
320 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700321 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700322 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500323 context.fShareContext = shareContext;
324 context.fShareIndex = shareIndex;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400325 context.fOptions = grOptions;
Brian Salomon55ad7742017-11-17 09:25:23 -0500326 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400327 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800328}
Brian Osman60c774d2017-02-21 16:58:08 -0500329
Brian Osman9eac2ea2017-02-24 14:51:44 -0500330ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
331 return this->getContextInfoInternal(type, overrides, nullptr, 0);
332}
333
334ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
335 SkASSERT(shareContext);
336 for (int i = 0; i < fContexts.count(); ++i) {
337 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
338 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
339 shareContext, shareIndex);
340 }
341 }
342
343 return ContextInfo();
344}
345
bsalomon3724e572016-03-30 18:56:19 -0700346} // namespace sk_gpu_test