blob: 5b5f31415d2096007d58c1cfc655c4c03f8e8b26 [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"
bsalomon273c0f52016-03-31 10:59:06 -070010#include "gl/GLTestContext.h"
djsollene4545212014-11-13 11:12:41 -080011
12#if SK_ANGLE
bsalomon273c0f52016-03-31 10:59:06 -070013 #include "gl/angle/GLTestContext_angle.h"
djsollene4545212014-11-13 11:12:41 -080014#endif
hendrikw885bf092015-08-27 10:38:39 -070015#if SK_COMMAND_BUFFER
bsalomon273c0f52016-03-31 10:59:06 -070016 #include "gl/command_buffer/GLTestContext_command_buffer.h"
hendrikw885bf092015-08-27 10:38:39 -070017#endif
bsalomon273c0f52016-03-31 10:59:06 -070018#include "gl/debug/DebugGLTestContext.h"
djsollene4545212014-11-13 11:12:41 -080019#if SK_MESA
bsalomon273c0f52016-03-31 10:59:06 -070020 #include "gl/mesa/GLTestContext_mesa.h"
djsollene4545212014-11-13 11:12:41 -080021#endif
djsollen7e731082016-06-09 13:07:13 -070022#ifdef SK_VULKAN
bsalomon18a2f9d2016-05-11 10:09:18 -070023#include "vk/VkTestContext.h"
bsalomon7c62b472016-04-01 07:42:05 -070024#endif
bsalomon273c0f52016-03-31 10:59:06 -070025#include "gl/null/NullGLTestContext.h"
kkinnunencfe62e32015-07-01 02:58:50 -070026#include "gl/GrGLGpu.h"
27#include "GrCaps.h"
djsollene4545212014-11-13 11:12:41 -080028
bsalomon3724e572016-03-30 18:56:19 -070029namespace sk_gpu_test {
kkinnunen34058002016-01-06 23:49:30 -080030GrContextFactory::GrContextFactory() { }
31
32GrContextFactory::GrContextFactory(const GrContextOptions& opts)
33 : fGlobalOptions(opts) {
34}
35
36GrContextFactory::~GrContextFactory() {
37 this->destroyContexts();
38}
39
40void GrContextFactory::destroyContexts() {
41 for (Context& context : fContexts) {
bsalomon18a2f9d2016-05-11 10:09:18 -070042 if (context.fTestContext) {
43 context.fTestContext->makeCurrent();
kkinnunen34058002016-01-06 23:49:30 -080044 }
45 if (!context.fGrContext->unique()) {
bsalomondc0fcd42016-04-11 14:21:33 -070046 context.fGrContext->releaseResourcesAndAbandonContext();
47 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080048 }
49 context.fGrContext->unref();
bsalomon18a2f9d2016-05-11 10:09:18 -070050 delete context.fTestContext;
kkinnunen34058002016-01-06 23:49:30 -080051 }
52 fContexts.reset();
53}
54
55void GrContextFactory::abandonContexts() {
56 for (Context& context : fContexts) {
bsalomondc0fcd42016-04-11 14:21:33 -070057 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070058 if (context.fTestContext) {
59 context.fTestContext->makeCurrent();
60 context.fTestContext->testAbandon();
61 delete(context.fTestContext);
62 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070063 }
64 context.fGrContext->abandonContext();
65 context.fAbandoned = true;
kkinnunen34058002016-01-06 23:49:30 -080066 }
kkinnunen34058002016-01-06 23:49:30 -080067 }
68}
69
bsalomon6e2aad42016-04-01 11:54:31 -070070void GrContextFactory::releaseResourcesAndAbandonContexts() {
71 for (Context& context : fContexts) {
bsalomondc0fcd42016-04-11 14:21:33 -070072 if (!context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -070073 if (context.fTestContext) {
74 context.fTestContext->makeCurrent();
bsalomondc0fcd42016-04-11 14:21:33 -070075 }
bsalomon6e2aad42016-04-01 11:54:31 -070076 context.fGrContext->releaseResourcesAndAbandonContext();
bsalomondc0fcd42016-04-11 14:21:33 -070077 context.fAbandoned = true;
bsalomon18a2f9d2016-05-11 10:09:18 -070078 if (context.fTestContext) {
79 delete context.fTestContext;
80 context.fTestContext = nullptr;
bsalomondc0fcd42016-04-11 14:21:33 -070081 }
bsalomon6e2aad42016-04-01 11:54:31 -070082 }
83 }
84}
85
bsalomon85b4b532016-04-05 11:06:27 -070086#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
87const GrContextFactory::ContextType GrContextFactory::kNativeGL_ContextType =
88 GrContextFactory::kGL_ContextType;
89#else
90const GrContextFactory::ContextType GrContextFactory::kNativeGL_ContextType =
91 GrContextFactory::kGLES_ContextType;
92#endif
93
bsalomonf2f1c172016-04-05 12:59:06 -070094ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOptions options) {
djsollene4545212014-11-13 11:12:41 -080095 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -080096 Context& context = fContexts[i];
kkinnunen34058002016-01-06 23:49:30 -080097 if (context.fType == type &&
bsalomondc0fcd42016-04-11 14:21:33 -070098 context.fOptions == options &&
99 !context.fAbandoned) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700100 context.fTestContext->makeCurrent();
101 return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext);
djsollene4545212014-11-13 11:12:41 -0800102 }
103 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700104 SkAutoTDelete<TestContext> testCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700105 sk_sp<GrContext> grCtx;
106 GrBackendContext backendContext = 0;
107 sk_sp<const GrGLInterface> glInterface;
bsalomondc0fcd42016-04-11 14:21:33 -0700108 GrBackend backend = ContextTypeBackend(type);
109 switch (backend) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700110 case kOpenGL_GrBackend: {
111 GLTestContext* glCtx;
bsalomondc0fcd42016-04-11 14:21:33 -0700112 switch (type) {
113 case kGL_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700114 glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard);
bsalomondc0fcd42016-04-11 14:21:33 -0700115 break;
116 case kGLES_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700117 glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard);
bsalomondc0fcd42016-04-11 14:21:33 -0700118 break;
119#if SK_ANGLE
120# ifdef SK_BUILD_FOR_WIN
121 case kANGLE_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700122 glCtx = CreateANGLEDirect3DGLTestContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700123 break;
124# endif
125 case kANGLE_GL_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700126 glCtx = CreateANGLEOpenGLGLTestContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700127 break;
djsollene4545212014-11-13 11:12:41 -0800128#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800129#if SK_COMMAND_BUFFER
bsalomondc0fcd42016-04-11 14:21:33 -0700130 case kCommandBuffer_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700131 glCtx = CommandBufferGLTestContext::Create();
bsalomondc0fcd42016-04-11 14:21:33 -0700132 break;
hendrikw885bf092015-08-27 10:38:39 -0700133#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800134#if SK_MESA
bsalomondc0fcd42016-04-11 14:21:33 -0700135 case kMESA_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700136 glCtx = CreateMesaGLTestContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700137 break;
djsollene4545212014-11-13 11:12:41 -0800138#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700139 case kNullGL_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700140 glCtx = CreateNullGLTestContext(kEnableNVPR_ContextOptions & options);
bsalomondc0fcd42016-04-11 14:21:33 -0700141 break;
142 case kDebugGL_ContextType:
bsalomon18a2f9d2016-05-11 10:09:18 -0700143 glCtx = CreateDebugGLTestContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700144 break;
145 default:
146 return ContextInfo();
147 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700148 if (!glCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700149 return ContextInfo();
150 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700151 testCtx.reset(glCtx);
bsalomondc0fcd42016-04-11 14:21:33 -0700152 glInterface.reset(SkRef(glCtx->gl()));
153 // Block NVPR from non-NVPR types.
154 if (!(kEnableNVPR_ContextOptions & options)) {
155 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface.get()));
156 if (!glInterface) {
157 return ContextInfo();
158 }
159 }
160 backendContext = reinterpret_cast<GrBackendContext>(glInterface.get());
djsollene4545212014-11-13 11:12:41 -0800161 break;
bsalomon18a2f9d2016-05-11 10:09:18 -0700162 }
jvanvertha50e17a2015-08-12 12:19:36 -0700163#ifdef SK_VULKAN
bsalomondc0fcd42016-04-11 14:21:33 -0700164 case kVulkan_GrBackend:
165 SkASSERT(kVulkan_ContextType == type);
brianosmana56800a2016-05-23 10:11:07 -0700166 if (kEnableNVPR_ContextOptions & options) {
bsalomondc0fcd42016-04-11 14:21:33 -0700167 return ContextInfo();
168 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700169 testCtx.reset(CreatePlatformVkTestContext());
170 if (!testCtx) {
bsalomondc0fcd42016-04-11 14:21:33 -0700171 return ContextInfo();
172 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700173
bsalomondc0fcd42016-04-11 14:21:33 -0700174 // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
175 // destruction will hang occaisonally. For some reason having an existing GL
176 // context fixes this.
177 if (!fSentinelGLContext) {
178 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
179 if (!fSentinelGLContext) {
180 fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
181 }
182 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700183 backendContext = testCtx->backendContext();
bsalomondc0fcd42016-04-11 14:21:33 -0700184 break;
jvanvertha50e17a2015-08-12 12:19:36 -0700185#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700186 default:
187 return ContextInfo();
188 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700189 testCtx->makeCurrent();
190 SkASSERT(testCtx && testCtx->backend() == backend);
bsalomondc0fcd42016-04-11 14:21:33 -0700191 grCtx.reset(GrContext::Create(backend, backendContext, fGlobalOptions));
djsollene4545212014-11-13 11:12:41 -0800192 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800193 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800194 }
bsalomon85b4b532016-04-05 11:06:27 -0700195 if (kEnableNVPR_ContextOptions & options) {
kkinnunencfe62e32015-07-01 02:58:50 -0700196 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800197 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700198 }
199 }
bsalomon85b4b532016-04-05 11:06:27 -0700200 if (kRequireSRGBSupport_ContextOptions & options) {
brianosman61d3b082016-03-30 11:19:36 -0700201 if (!grCtx->caps()->srgbSupport()) {
202 return ContextInfo();
203 }
204 }
kkinnunencfe62e32015-07-01 02:58:50 -0700205
kkinnunen34058002016-01-06 23:49:30 -0800206 Context& context = fContexts.push_back();
bsalomon18a2f9d2016-05-11 10:09:18 -0700207 context.fBackend = backend;
208 context.fTestContext = testCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800209 context.fGrContext = SkRef(grCtx.get());
210 context.fType = type;
211 context.fOptions = options;
bsalomondc0fcd42016-04-11 14:21:33 -0700212 context.fAbandoned = false;
bsalomon18a2f9d2016-05-11 10:09:18 -0700213 return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext);
djsollene4545212014-11-13 11:12:41 -0800214}
bsalomon3724e572016-03-30 18:56:19 -0700215} // namespace sk_gpu_test