blob: c2321213326aed4bcd902532afd2e138dcc155e0 [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
bsalomon7c62b472016-04-01 07:42:05 -070022#if SK_VULKAN
23#include "vk/GrVkBackendContext.h"
24#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) {
42 if (context.fGLContext) {
43 context.fGLContext->makeCurrent();
44 }
45 if (!context.fGrContext->unique()) {
46 context.fGrContext->abandonContext();
47 }
48 context.fGrContext->unref();
49 delete(context.fGLContext);
50 }
51 fContexts.reset();
52}
53
54void GrContextFactory::abandonContexts() {
55 for (Context& context : fContexts) {
56 if (context.fGLContext) {
57 context.fGLContext->makeCurrent();
58 context.fGLContext->testAbandon();
59 delete(context.fGLContext);
60 context.fGLContext = nullptr;
61 }
62 context.fGrContext->abandonContext();
63 }
64}
65
bsalomon6e2aad42016-04-01 11:54:31 -070066void GrContextFactory::releaseResourcesAndAbandonContexts() {
67 for (Context& context : fContexts) {
68 if (context.fGLContext) {
69 context.fGLContext->makeCurrent();
70 context.fGrContext->releaseResourcesAndAbandonContext();
71 delete(context.fGLContext);
72 context.fGLContext = nullptr;
73 }
74 }
75}
76
kkinnunen34058002016-01-06 23:49:30 -080077GrContextFactory::ContextInfo GrContextFactory::getContextInfo(GLContextType type,
78 GLContextOptions options) {
djsollene4545212014-11-13 11:12:41 -080079 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -080080 Context& context = fContexts[i];
81 if (!context.fGLContext) {
82 continue;
83 }
84 if (context.fType == type &&
85 context.fOptions == options) {
86 context.fGLContext->makeCurrent();
87 return ContextInfo(context.fGrContext, context.fGLContext);
djsollene4545212014-11-13 11:12:41 -080088 }
89 }
bsalomon273c0f52016-03-31 10:59:06 -070090 SkAutoTDelete<GLTestContext> glCtx;
djsollene4545212014-11-13 11:12:41 -080091 SkAutoTUnref<GrContext> grCtx;
92 switch (type) {
djsollene4545212014-11-13 11:12:41 -080093 case kNative_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -070094 glCtx.reset(CreatePlatformGLTestContext(kNone_GrGLStandard));
djsollene4545212014-11-13 11:12:41 -080095 break;
kkinnunen3e980c32015-12-23 01:33:00 -080096 case kGL_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -070097 glCtx.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
kkinnunen3e980c32015-12-23 01:33:00 -080098 break;
99 case kGLES_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700100 glCtx.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
kkinnunen3e980c32015-12-23 01:33:00 -0800101 break;
102#if SK_ANGLE
103#ifdef SK_BUILD_FOR_WIN
djsollene4545212014-11-13 11:12:41 -0800104 case kANGLE_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700105 glCtx.reset(CreateANGLEDirect3DGLTestContext());
hendrikweddbefb2015-09-11 13:07:29 -0700106 break;
kkinnunen3e980c32015-12-23 01:33:00 -0800107#endif
hendrikweddbefb2015-09-11 13:07:29 -0700108 case kANGLE_GL_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700109 glCtx.reset(CreateANGLEOpenGLGLTestContext());
djsollene4545212014-11-13 11:12:41 -0800110 break;
111#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800112#if SK_COMMAND_BUFFER
kkinnunenf655e932016-03-03 07:39:48 -0800113 case kCommandBuffer_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700114 glCtx.reset(CommandBufferGLTestContext::Create());
hendrikw885bf092015-08-27 10:38:39 -0700115 break;
116#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800117#if SK_MESA
djsollene4545212014-11-13 11:12:41 -0800118 case kMESA_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700119 glCtx.reset(CreateMesaGLTestContext());
djsollene4545212014-11-13 11:12:41 -0800120 break;
121#endif
122 case kNull_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700123 glCtx.reset(CreateNullGLTestContext());
djsollene4545212014-11-13 11:12:41 -0800124 break;
125 case kDebug_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700126 glCtx.reset(CreateDebugGLTestContext());
djsollene4545212014-11-13 11:12:41 -0800127 break;
128 }
halcanary96fcdcc2015-08-27 07:41:13 -0700129 if (nullptr == glCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800130 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800131 }
132
133 SkASSERT(glCtx->isValid());
134
kkinnunencfe62e32015-07-01 02:58:50 -0700135 // Block NVPR from non-NVPR types.
djsollene4545212014-11-13 11:12:41 -0800136 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl()));
kkinnunen5219fd92015-12-10 06:28:13 -0800137 if (!(kEnableNVPR_GLContextOptions & options)) {
djsollene4545212014-11-13 11:12:41 -0800138 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
139 if (!glInterface) {
kkinnunen34058002016-01-06 23:49:30 -0800140 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800141 }
142 }
143
144 glCtx->makeCurrent();
jvanvertha50e17a2015-08-12 12:19:36 -0700145#ifdef SK_VULKAN
jvanverthddf98352016-03-21 11:46:00 -0700146 if (kEnableNVPR_GLContextOptions & options) {
147 return ContextInfo();
148 } else {
bsalomon7c62b472016-04-01 07:42:05 -0700149 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(GrVkBackendContext::Create());
jvanverthddf98352016-03-21 11:46:00 -0700150 grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions));
151 }
jvanvertha50e17a2015-08-12 12:19:36 -0700152#else
bsalomon7c62b472016-04-01 07:42:05 -0700153 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
bsalomon682c2692015-05-22 14:01:46 -0700154 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions));
jvanvertha50e17a2015-08-12 12:19:36 -0700155#endif
djsollene4545212014-11-13 11:12:41 -0800156 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800157 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800158 }
kkinnunen5219fd92015-12-10 06:28:13 -0800159 if (kEnableNVPR_GLContextOptions & options) {
kkinnunencfe62e32015-07-01 02:58:50 -0700160 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800161 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700162 }
163 }
brianosman61d3b082016-03-30 11:19:36 -0700164 if (kRequireSRGBSupport_GLContextOptions & options) {
165 if (!grCtx->caps()->srgbSupport()) {
166 return ContextInfo();
167 }
168 }
kkinnunencfe62e32015-07-01 02:58:50 -0700169
kkinnunen34058002016-01-06 23:49:30 -0800170 Context& context = fContexts.push_back();
mtklein18300a32016-03-16 13:53:35 -0700171 context.fGLContext = glCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800172 context.fGrContext = SkRef(grCtx.get());
173 context.fType = type;
174 context.fOptions = options;
175 return ContextInfo(context.fGrContext, context.fGLContext);
djsollene4545212014-11-13 11:12:41 -0800176}
bsalomon3724e572016-03-30 18:56:19 -0700177} // namespace sk_gpu_test