blob: b7e48254c881af6835b8c6e195b3a8c2ee1a69a7 [file] [log] [blame]
djsollene4545212014-11-13 11:12:41 -08001
2/*
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"
10
11#if SK_ANGLE
12 #include "gl/angle/SkANGLEGLContext.h"
13#endif
hendrikw885bf092015-08-27 10:38:39 -070014#if SK_COMMAND_BUFFER
15 #include "gl/command_buffer/SkCommandBufferGLContext.h"
16#endif
djsollene4545212014-11-13 11:12:41 -080017#include "gl/debug/SkDebugGLContext.h"
18#if SK_MESA
19 #include "gl/mesa/SkMesaGLContext.h"
20#endif
21#include "gl/SkGLContext.h"
22#include "gl/SkNullGLContext.h"
kkinnunencfe62e32015-07-01 02:58:50 -070023#include "gl/GrGLGpu.h"
24#include "GrCaps.h"
djsollene4545212014-11-13 11:12:41 -080025
kkinnunen34058002016-01-06 23:49:30 -080026GrContextFactory::GrContextFactory() { }
27
28GrContextFactory::GrContextFactory(const GrContextOptions& opts)
29 : fGlobalOptions(opts) {
30}
31
32GrContextFactory::~GrContextFactory() {
33 this->destroyContexts();
34}
35
36void GrContextFactory::destroyContexts() {
37 for (Context& context : fContexts) {
38 if (context.fGLContext) {
39 context.fGLContext->makeCurrent();
40 }
41 if (!context.fGrContext->unique()) {
42 context.fGrContext->abandonContext();
43 }
44 context.fGrContext->unref();
45 delete(context.fGLContext);
46 }
47 fContexts.reset();
48}
49
50void GrContextFactory::abandonContexts() {
51 for (Context& context : fContexts) {
52 if (context.fGLContext) {
53 context.fGLContext->makeCurrent();
54 context.fGLContext->testAbandon();
55 delete(context.fGLContext);
56 context.fGLContext = nullptr;
57 }
58 context.fGrContext->abandonContext();
59 }
60}
61
62GrContextFactory::ContextInfo GrContextFactory::getContextInfo(GLContextType type,
63 GLContextOptions options) {
djsollene4545212014-11-13 11:12:41 -080064 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen34058002016-01-06 23:49:30 -080065 Context& context = fContexts[i];
66 if (!context.fGLContext) {
67 continue;
68 }
69 if (context.fType == type &&
70 context.fOptions == options) {
71 context.fGLContext->makeCurrent();
72 return ContextInfo(context.fGrContext, context.fGLContext);
djsollene4545212014-11-13 11:12:41 -080073 }
74 }
kkinnunen34058002016-01-06 23:49:30 -080075 SkAutoTDelete<SkGLContext> glCtx;
djsollene4545212014-11-13 11:12:41 -080076 SkAutoTUnref<GrContext> grCtx;
77 switch (type) {
djsollene4545212014-11-13 11:12:41 -080078 case kNative_GLContextType:
kkinnunen3e980c32015-12-23 01:33:00 -080079 glCtx.reset(SkCreatePlatformGLContext(kNone_GrGLStandard));
djsollene4545212014-11-13 11:12:41 -080080 break;
kkinnunen3e980c32015-12-23 01:33:00 -080081 case kGL_GLContextType:
82 glCtx.reset(SkCreatePlatformGLContext(kGL_GrGLStandard));
83 break;
84 case kGLES_GLContextType:
85 glCtx.reset(SkCreatePlatformGLContext(kGLES_GrGLStandard));
86 break;
87#if SK_ANGLE
88#ifdef SK_BUILD_FOR_WIN
djsollene4545212014-11-13 11:12:41 -080089 case kANGLE_GLContextType:
kkinnunen3e980c32015-12-23 01:33:00 -080090 glCtx.reset(SkANGLEGLContext::CreateDirectX());
hendrikweddbefb2015-09-11 13:07:29 -070091 break;
kkinnunen3e980c32015-12-23 01:33:00 -080092#endif
hendrikweddbefb2015-09-11 13:07:29 -070093 case kANGLE_GL_GLContextType:
kkinnunen3e980c32015-12-23 01:33:00 -080094 glCtx.reset(SkANGLEGLContext::CreateOpenGL());
djsollene4545212014-11-13 11:12:41 -080095 break;
96#endif
kkinnunen3e980c32015-12-23 01:33:00 -080097#if SK_COMMAND_BUFFER
hendrikw885bf092015-08-27 10:38:39 -070098 case kCommandBuffer_GLContextType:
kkinnunen3e980c32015-12-23 01:33:00 -080099 glCtx.reset(SkCommandBufferGLContext::Create());
hendrikw885bf092015-08-27 10:38:39 -0700100 break;
101#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800102#if SK_MESA
djsollene4545212014-11-13 11:12:41 -0800103 case kMESA_GLContextType:
kkinnunen3e980c32015-12-23 01:33:00 -0800104 glCtx.reset(SkMesaGLContext::Create());
djsollene4545212014-11-13 11:12:41 -0800105 break;
106#endif
107 case kNull_GLContextType:
kkinnunen3e980c32015-12-23 01:33:00 -0800108 glCtx.reset(SkNullGLContext::Create());
djsollene4545212014-11-13 11:12:41 -0800109 break;
110 case kDebug_GLContextType:
kkinnunen3e980c32015-12-23 01:33:00 -0800111 glCtx.reset(SkDebugGLContext::Create());
djsollene4545212014-11-13 11:12:41 -0800112 break;
113 }
halcanary96fcdcc2015-08-27 07:41:13 -0700114 if (nullptr == glCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800115 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800116 }
117
118 SkASSERT(glCtx->isValid());
119
kkinnunencfe62e32015-07-01 02:58:50 -0700120 // Block NVPR from non-NVPR types.
djsollene4545212014-11-13 11:12:41 -0800121 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl()));
kkinnunen5219fd92015-12-10 06:28:13 -0800122 if (!(kEnableNVPR_GLContextOptions & options)) {
djsollene4545212014-11-13 11:12:41 -0800123 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
124 if (!glInterface) {
kkinnunen34058002016-01-06 23:49:30 -0800125 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800126 }
127 }
128
129 glCtx->makeCurrent();
130 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
jvanvertha50e17a2015-08-12 12:19:36 -0700131#ifdef SK_VULKAN
132 grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions));
133#else
bsalomon682c2692015-05-22 14:01:46 -0700134 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions));
jvanvertha50e17a2015-08-12 12:19:36 -0700135#endif
djsollene4545212014-11-13 11:12:41 -0800136 if (!grCtx.get()) {
kkinnunen34058002016-01-06 23:49:30 -0800137 return ContextInfo();
djsollene4545212014-11-13 11:12:41 -0800138 }
kkinnunen5219fd92015-12-10 06:28:13 -0800139 if (kEnableNVPR_GLContextOptions & options) {
kkinnunencfe62e32015-07-01 02:58:50 -0700140 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunen34058002016-01-06 23:49:30 -0800141 return ContextInfo();
kkinnunencfe62e32015-07-01 02:58:50 -0700142 }
143 }
144
kkinnunen34058002016-01-06 23:49:30 -0800145 Context& context = fContexts.push_back();
146 context.fGLContext = glCtx.detach();
147 context.fGrContext = SkRef(grCtx.get());
148 context.fType = type;
149 context.fOptions = options;
150 return ContextInfo(context.fGrContext, context.fGLContext);
djsollene4545212014-11-13 11:12:41 -0800151}