blob: bae73a4e1986a725de8f8afe7007708e7eee3777 [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
66GrContextFactory::ContextInfo GrContextFactory::getContextInfo(GLContextType type,
67 GLContextOptions options) {
djsollene4545212014-11-13 11:12:41 -080068 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -080069 Context& context = fContexts[i];
70 if (!context.fGLContext) {
71 continue;
72 }
73 if (context.fType == type &&
74 context.fOptions == options) {
75 context.fGLContext->makeCurrent();
76 return ContextInfo(context.fGrContext, context.fGLContext);
djsollene4545212014-11-13 11:12:41 -080077 }
78 }
bsalomon273c0f52016-03-31 10:59:06 -070079 SkAutoTDelete<GLTestContext> glCtx;
djsollene4545212014-11-13 11:12:41 -080080 SkAutoTUnref<GrContext> grCtx;
81 switch (type) {
djsollene4545212014-11-13 11:12:41 -080082 case kNative_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -070083 glCtx.reset(CreatePlatformGLTestContext(kNone_GrGLStandard));
djsollene4545212014-11-13 11:12:41 -080084 break;
kkinnunen3e980c32015-12-23 01:33:00 -080085 case kGL_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -070086 glCtx.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
kkinnunen3e980c32015-12-23 01:33:00 -080087 break;
88 case kGLES_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -070089 glCtx.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
kkinnunen3e980c32015-12-23 01:33:00 -080090 break;
91#if SK_ANGLE
92#ifdef SK_BUILD_FOR_WIN
djsollene4545212014-11-13 11:12:41 -080093 case kANGLE_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -070094 glCtx.reset(CreateANGLEDirect3DGLTestContext());
hendrikweddbefb2015-09-11 13:07:29 -070095 break;
kkinnunen3e980c32015-12-23 01:33:00 -080096#endif
hendrikweddbefb2015-09-11 13:07:29 -070097 case kANGLE_GL_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -070098 glCtx.reset(CreateANGLEOpenGLGLTestContext());
djsollene4545212014-11-13 11:12:41 -080099 break;
100#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800101#if SK_COMMAND_BUFFER
kkinnunenf655e932016-03-03 07:39:48 -0800102 case kCommandBuffer_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700103 glCtx.reset(CommandBufferGLTestContext::Create());
hendrikw885bf092015-08-27 10:38:39 -0700104 break;
105#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800106#if SK_MESA
djsollene4545212014-11-13 11:12:41 -0800107 case kMESA_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700108 glCtx.reset(CreateMesaGLTestContext());
djsollene4545212014-11-13 11:12:41 -0800109 break;
110#endif
111 case kNull_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700112 glCtx.reset(CreateNullGLTestContext());
djsollene4545212014-11-13 11:12:41 -0800113 break;
114 case kDebug_GLContextType:
bsalomon273c0f52016-03-31 10:59:06 -0700115 glCtx.reset(CreateDebugGLTestContext());
djsollene4545212014-11-13 11:12:41 -0800116 break;
117 }
halcanary96fcdcc2015-08-27 07:41:13 -0700118 if (nullptr == glCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800119 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800120 }
121
122 SkASSERT(glCtx->isValid());
123
kkinnunencfe62e32015-07-01 02:58:50 -0700124 // Block NVPR from non-NVPR types.
djsollene4545212014-11-13 11:12:41 -0800125 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl()));
kkinnunen5219fd92015-12-10 06:28:13 -0800126 if (!(kEnableNVPR_GLContextOptions & options)) {
djsollene4545212014-11-13 11:12:41 -0800127 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
128 if (!glInterface) {
kkinnunen34058002016-01-06 23:49:30 -0800129 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800130 }
131 }
132
133 glCtx->makeCurrent();
jvanvertha50e17a2015-08-12 12:19:36 -0700134#ifdef SK_VULKAN
jvanverthddf98352016-03-21 11:46:00 -0700135 if (kEnableNVPR_GLContextOptions & options) {
136 return ContextInfo();
137 } else {
bsalomon7c62b472016-04-01 07:42:05 -0700138 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(GrVkBackendContext::Create());
jvanverthddf98352016-03-21 11:46:00 -0700139 grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions));
140 }
jvanvertha50e17a2015-08-12 12:19:36 -0700141#else
bsalomon7c62b472016-04-01 07:42:05 -0700142 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
bsalomon682c2692015-05-22 14:01:46 -0700143 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions));
jvanvertha50e17a2015-08-12 12:19:36 -0700144#endif
djsollene4545212014-11-13 11:12:41 -0800145 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800146 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800147 }
kkinnunen5219fd92015-12-10 06:28:13 -0800148 if (kEnableNVPR_GLContextOptions & options) {
kkinnunencfe62e32015-07-01 02:58:50 -0700149 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800150 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700151 }
152 }
brianosman61d3b082016-03-30 11:19:36 -0700153 if (kRequireSRGBSupport_GLContextOptions & options) {
154 if (!grCtx->caps()->srgbSupport()) {
155 return ContextInfo();
156 }
157 }
kkinnunencfe62e32015-07-01 02:58:50 -0700158
kkinnunen34058002016-01-06 23:49:30 -0800159 Context& context = fContexts.push_back();
mtklein18300a32016-03-16 13:53:35 -0700160 context.fGLContext = glCtx.release();
kkinnunen34058002016-01-06 23:49:30 -0800161 context.fGrContext = SkRef(grCtx.get());
162 context.fType = type;
163 context.fOptions = options;
164 return ContextInfo(context.fGrContext, context.fGLContext);
djsollene4545212014-11-13 11:12:41 -0800165}
bsalomon3724e572016-03-30 18:56:19 -0700166} // namespace sk_gpu_test