blob: d0ee2187d1dab28d5dbb6f85006ee4f79edf1618 [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
9#include "GrContextFactory.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040010#include "GrContextPriv.h"
bsalomon273c0f52016-03-31 10:59:06 -070011#include "gl/GLTestContext.h"
djsollene4545212014-11-13 11:12:41 -080012
Mike Kleinc168a3a2016-11-14 14:53:13 +000013#if SK_ANGLE
14 #include "gl/angle/GLTestContext_angle.h"
15#endif
mtklein605d9522016-09-21 14:01:32 -070016#include "gl/command_buffer/GLTestContext_command_buffer.h"
djsollen7e731082016-06-09 13:07:13 -070017#ifdef SK_VULKAN
bsalomon18a2f9d2016-05-11 10:09:18 -070018#include "vk/VkTestContext.h"
bsalomon7c62b472016-04-01 07:42:05 -070019#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040020#ifdef SK_METAL
21#include "mtl/MtlTestContext.h"
22#endif
bsalomon273c0f52016-03-31 10:59:06 -070023#include "gl/null/NullGLTestContext.h"
kkinnunencfe62e32015-07-01 02:58:50 -070024#include "gl/GrGLGpu.h"
Brian Salomoncfe910d2017-07-06 16:40:18 -040025#include "mock/MockTestContext.h"
kkinnunencfe62e32015-07-01 02:58:50 -070026#include "GrCaps.h"
djsollene4545212014-11-13 11:12:41 -080027
Mike Klein8f11d4d2018-01-24 12:42:55 -050028#if defined(SK_BUILD_FOR_WIN) && defined(SK_ENABLE_DISCRETE_GPU)
Brian Osman3f375d02016-12-28 11:19:22 -050029extern "C" {
30 // NVIDIA documents that the presence and value of this symbol programmatically enable the high
31 // performance GPU in laptops with switchable graphics.
32 // https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm
33 // From testing, including this symbol, even if it is set to 0, we still get the NVIDIA GPU.
34 _declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
35
36 // AMD has a similar mechanism, although I don't have an AMD laptop, so this is untested.
37 // https://community.amd.com/thread/169965
38 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
39}
40#endif
41
bsalomon3724e572016-03-30 18:56:19 -070042namespace sk_gpu_test {
kkinnunen34058002016-01-06 23:49:30 -080043GrContextFactory::GrContextFactory() { }
44
45GrContextFactory::GrContextFactory(const GrContextOptions& opts)
46 : fGlobalOptions(opts) {
47}
48
49GrContextFactory::~GrContextFactory() {
50 this->destroyContexts();
51}
52
53void GrContextFactory::destroyContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050054 // We must delete the test contexts in reverse order so that any child context is finished and
55 // deleted before a parent context. This relies on the fact that when we make a new context we
56 // append it to the end of fContexts array.
57 // TODO: Look into keeping a dependency dag for contexts and deletion order
58 for (int i = fContexts.count() - 1; i >= 0; --i) {
59 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -050060 SkScopeExit restore(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -070061 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050062 restore = context.fTestContext->makeCurrentAndAutoRestore();
kkinnunen34058002016-01-06 23:49:30 -080063 }
64 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070065 context.fGrContext->releaseResourcesAndAbandonContext();
66 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080067 }
68 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070069 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080070 }
71 fContexts.reset();
72}
73
74void GrContextFactory::abandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050075 // We must abandon the test contexts in reverse order so that any child context is finished and
76 // abandoned before a parent context. This relies on the fact that when we make a new context we
77 // append it to the end of fContexts array.
78 // TODO: Look into keeping a dependency dag for contexts and deletion order
79 for (int i = fContexts.count() - 1; i >= 0; --i) {
80 Context& context = fContexts[i];
bsalomondc0fcd42016-04-11 14:21:33 -070081 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070082 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -050083 auto restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomon18a2f9d2016-05-11 10:09:18 -070084 context.fTestContext->testAbandon();
85 delete(context.fTestContext);
86 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070087 }
88 context.fGrContext->abandonContext();
89 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080090 }
kkinnunen34058002016-01-06 23:49:30 -080091 }
92}
93
bsalomon6e2aad42016-04-01 11:54:31 -070094void GrContextFactory::releaseResourcesAndAbandonContexts() {
Greg Danielf0fe02f2018-01-16 11:20:41 -050095 // We must abandon the test contexts in reverse order so that any child context is finished and
96 // abandoned before a parent context. This relies on the fact that when we make a new context we
97 // append it to the end of fContexts array.
98 // TODO: Look into keeping a dependency dag for contexts and deletion order
99 for (int i = fContexts.count() - 1; i >= 0; --i) {
100 Context& context = fContexts[i];
Brian Salomon55ad7742017-11-17 09:25:23 -0500101 SkScopeExit restore(nullptr);
bsalomondc0fcd42016-04-11 14:21:33 -0700102 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700103 if (context.fTestContext) {
Brian Salomon55ad7742017-11-17 09:25:23 -0500104 restore = context.fTestContext->makeCurrentAndAutoRestore();
bsalomondc0fcd42016-04-11 14:21:33 -0700105 }
bsalomon6e2aad42016-04-01 11:54:31 -0700106 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700107 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -0700108 if (context.fTestContext) {
109 delete context.fTestContext;
110 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700111 }
bsalomon6e2aad42016-04-01 11:54:31 -0700112 }
113 }
114}
115
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400116GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
117 return this->getContextInfo(type, overrides).grContext();
118}
119
Brian Osman9eac2ea2017-02-24 14:51:44 -0500120ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
121 GrContext* shareContext, uint32_t shareIndex) {
Brian Osman60c774d2017-02-21 16:58:08 -0500122 // (shareIndex != 0) -> (shareContext != nullptr)
123 SkASSERT((shareIndex == 0) || (shareContext != nullptr));
124
djsollene4545212014-11-13 11:12:41 -0800125 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -0800126 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -0800127 if (context.fType == type &&
csmartdaltone812d492017-02-21 12:36:05 -0700128 context.fOverrides == overrides &&
Brian Osman60c774d2017-02-21 16:58:08 -0500129 context.fShareContext == shareContext &&
130 context.fShareIndex == shareIndex &&
bsalomondc0fcd42016-04-11 14:21:33 -0700131 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700132 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400133 return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
134 context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800135 }
136 }
Brian Osman60c774d2017-02-21 16:58:08 -0500137
138 // If we're trying to create a context in a share group, find the master context
139 Context* masterContext = nullptr;
140 if (shareContext) {
141 for (int i = 0; i < fContexts.count(); ++i) {
142 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
143 masterContext = &fContexts[i];
144 break;
145 }
146 }
Brian Osman9eac2ea2017-02-24 14:51:44 -0500147 SkASSERT(masterContext && masterContext->fType == type);
Brian Osman60c774d2017-02-21 16:58:08 -0500148 }
149
Ben Wagner145dbcd2016-11-03 14:40:50 -0400150 std::unique_ptr<TestContext> testCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700151 GrBackend backend = ContextTypeBackend(type);
152 switch (backend) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700153 case kOpenGL_GrBackend: {
Brian Osman60c774d2017-02-21 16:58:08 -0500154 GLTestContext* glShareContext = masterContext
155 ? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
bsalomon18a2f9d2016-05-11 10:09:18 -0700156 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700157 switch (type) {
158 case kGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500159 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700160 break;
161 case kGLES_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500162 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700163 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000164#if SK_ANGLE
bsalomon11abd8d2016-10-14 08:13:48 -0700165 case kANGLE_D3D9_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500166 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
167 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700168 break;
bsalomon11abd8d2016-10-14 08:13:48 -0700169 case kANGLE_D3D11_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500170 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
171 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700172 break;
173 case kANGLE_D3D11_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500174 glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
175 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700176 break;
177 case kANGLE_GL_ES2_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500178 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
179 glShareContext).release();
bsalomon11abd8d2016-10-14 08:13:48 -0700180 break;
181 case kANGLE_GL_ES3_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500182 glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
183 glShareContext).release();
bsalomondc0fcd42016-04-11 14:21:33 -0700184 break;
Mike Kleinc168a3a2016-11-14 14:53:13 +0000185#endif
Kevin Lubickffce0792017-05-24 15:30:35 -0400186#ifndef SK_NO_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700187 case kCommandBuffer_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500188 glCtx = CommandBufferGLTestContext::Create(glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700189 break;
Kevin Lubickffce0792017-05-24 15:30:35 -0400190#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700191 case kNullGL_ContextType:
Brian Osman60c774d2017-02-21 16:58:08 -0500192 glCtx = CreateNullGLTestContext(
193 ContextOverrides::kRequireNVPRSupport & overrides, glShareContext);
bsalomondc0fcd42016-04-11 14:21:33 -0700194 break;
bsalomondc0fcd42016-04-11 14:21:33 -0700195 default:
196 return ContextInfo();
197 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700198 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700199 return ContextInfo();
200 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700201 testCtx.reset(glCtx);
djsollene4545212014-11-13 11:12:41 -0800202 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700203 }
jvanvertha50e17a2015-08-12 12:19:36 -0700204#ifdef SK_VULKAN
Greg Daniel604b1972017-05-15 13:50:35 -0400205 case kVulkan_GrBackend: {
206 VkTestContext* vkSharedContext = masterContext
207 ? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -0700208 SkASSERT(kVulkan_ContextType == type);
csmartdaltone812d492017-02-21 12:36:05 -0700209 if (ContextOverrides::kRequireNVPRSupport & overrides) {
bsalomondc0fcd42016-04-11 14:21:33 -0700210 return ContextInfo();
211 }
Greg Daniel604b1972017-05-15 13:50:35 -0400212 testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
bsalomon18a2f9d2016-05-11 10:09:18 -0700213 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700214 return ContextInfo();
215 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700216
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 }
bsalomondc0fcd42016-04-11 14:21:33 -0700226 break;
Greg Daniel604b1972017-05-15 13:50:35 -0400227 }
jvanvertha50e17a2015-08-12 12:19:36 -0700228#endif
Greg Danielb76a72a2017-07-13 15:07:54 -0400229#ifdef SK_METAL
230 case kMetal_GrBackend: {
231 SkASSERT(!masterContext);
232 testCtx.reset(CreatePlatformMtlTestContext(nullptr));
233 if (!testCtx) {
234 return ContextInfo();
235 }
236 break;
237 }
238#endif
Brian Salomoncfe910d2017-07-06 16:40:18 -0400239 case kMock_GrBackend: {
240 TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
241 SkASSERT(kMock_ContextType == type);
242 if (ContextOverrides::kRequireNVPRSupport & overrides) {
243 return ContextInfo();
244 }
245 testCtx.reset(CreateMockTestContext(sharedContext));
246 if (!testCtx) {
247 return ContextInfo();
248 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400249 break;
250 }
bsalomondc0fcd42016-04-11 14:21:33 -0700251 default:
252 return ContextInfo();
253 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500254
bsalomon18a2f9d2016-05-11 10:09:18 -0700255 SkASSERT(testCtx && testCtx->backend() == backend);
csmartdaltone0d36292016-07-29 08:14:20 -0700256 GrContextOptions grOptions = fGlobalOptions;
csmartdalton008b9d82017-02-22 12:00:42 -0700257 if (ContextOverrides::kDisableNVPR & overrides) {
258 grOptions.fSuppressPathRendering = true;
259 }
Eric Karl5c779752017-05-08 12:02:07 -0700260 if (ContextOverrides::kAvoidStencilBuffers & overrides) {
261 grOptions.fAvoidStencilBuffers = true;
262 }
Brian Salomon55ad7742017-11-17 09:25:23 -0500263 sk_sp<GrContext> grCtx;
264 {
265 auto restore = testCtx->makeCurrentAndAutoRestore();
266 grCtx = testCtx->makeGrContext(grOptions);
267 }
djsollene4545212014-11-13 11:12:41 -0800268 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800269 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800270 }
csmartdaltone812d492017-02-21 12:36:05 -0700271 if (ContextOverrides::kRequireNVPRSupport & overrides) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400272 if (!grCtx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800273 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700274 }
275 }
276
Greg Danielf0fe02f2018-01-16 11:20:41 -0500277 // We must always add new contexts by pushing to the back so that when we delete them we delete
278 // them in reverse order in which they were made.
kkinnunen34058002016-01-06 23:49:30 -0800279 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700280 context.fBackend = backend;
281 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800282 context.fGrContext = SkRef(grCtx.get());
283 context.fType = type;
csmartdaltone812d492017-02-21 12:36:05 -0700284 context.fOverrides = overrides;
bsalomondc0fcd42016-04-11 14:21:33 -0700285 context.fAbandoned = false;
Brian Osman60c774d2017-02-21 16:58:08 -0500286 context.fShareContext = shareContext;
287 context.fShareIndex = shareIndex;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400288 context.fOptions = grOptions;
Brian Salomon55ad7742017-11-17 09:25:23 -0500289 context.fTestContext->makeCurrent();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400290 return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
djsollene4545212014-11-13 11:12:41 -0800291}
Brian Osman60c774d2017-02-21 16:58:08 -0500292
Brian Osman9eac2ea2017-02-24 14:51:44 -0500293ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
294 return this->getContextInfoInternal(type, overrides, nullptr, 0);
295}
296
297ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
298 SkASSERT(shareContext);
299 for (int i = 0; i < fContexts.count(); ++i) {
300 if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
301 return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
302 shareContext, shareIndex);
303 }
304 }
305
306 return ContextInfo();
307}
308
bsalomon3724e572016-03-30 18:56:19 -0700309} // namespace sk_gpu_test