OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "GrContextFactory.h" | 9 #include "GrContextFactory.h" |
10 | 10 |
11 #if SK_ANGLE | 11 #if SK_ANGLE |
12 #include "gl/angle/SkANGLEGLContext.h" | 12 #include "gl/angle/SkANGLEGLContext.h" |
13 #endif | 13 #endif |
14 #if SK_COMMAND_BUFFER | 14 #if SK_COMMAND_BUFFER |
15 #include "gl/command_buffer/SkCommandBufferGLContext.h" | 15 #include "gl/command_buffer/SkCommandBufferGLContext.h" |
16 #endif | 16 #endif |
17 #include "gl/debug/SkDebugGLContext.h" | 17 #include "gl/debug/SkDebugGLContext.h" |
18 #if SK_MESA | 18 #if SK_MESA |
19 #include "gl/mesa/SkMesaGLContext.h" | 19 #include "gl/mesa/SkMesaGLContext.h" |
20 #endif | 20 #endif |
21 #include "gl/SkGLContext.h" | 21 #include "gl/SkGLContext.h" |
22 #include "gl/SkNullGLContext.h" | 22 #include "gl/SkNullGLContext.h" |
23 #include "gl/GrGLGpu.h" | 23 #include "gl/GrGLGpu.h" |
24 #include "GrCaps.h" | 24 #include "GrCaps.h" |
25 | 25 |
26 GrContextFactory::ContextInfo* GrContextFactory::getContextInfo(GLContextType ty
pe, | 26 GrContextFactory::GrContextFactory() { } |
27 GLContextOptions
options) { | 27 |
| 28 GrContextFactory::GrContextFactory(const GrContextOptions& opts) |
| 29 : fGlobalOptions(opts) { |
| 30 } |
| 31 |
| 32 GrContextFactory::~GrContextFactory() { |
| 33 this->destroyContexts(); |
| 34 } |
| 35 |
| 36 void 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 |
| 50 void 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 |
| 62 GrContextFactory::ContextInfo GrContextFactory::getContextInfo(GLContextType typ
e, |
| 63 GLContextOptions
options) { |
28 for (int i = 0; i < fContexts.count(); ++i) { | 64 for (int i = 0; i < fContexts.count(); ++i) { |
29 if (fContexts[i]->fType == type && | 65 Context& context = fContexts[i]; |
30 fContexts[i]->fOptions == options) { | 66 if (!context.fGLContext) { |
31 fContexts[i]->fGLContext->makeCurrent(); | 67 continue; |
32 return fContexts[i]; | 68 } |
| 69 if (context.fType == type && |
| 70 context.fOptions == options) { |
| 71 context.fGLContext->makeCurrent(); |
| 72 return ContextInfo(context.fGrContext, context.fGLContext); |
33 } | 73 } |
34 } | 74 } |
35 SkAutoTUnref<SkGLContext> glCtx; | 75 SkAutoTDelete<SkGLContext> glCtx; |
36 SkAutoTUnref<GrContext> grCtx; | 76 SkAutoTUnref<GrContext> grCtx; |
37 switch (type) { | 77 switch (type) { |
38 case kNative_GLContextType: | 78 case kNative_GLContextType: |
39 glCtx.reset(SkCreatePlatformGLContext(kNone_GrGLStandard)); | 79 glCtx.reset(SkCreatePlatformGLContext(kNone_GrGLStandard)); |
40 break; | 80 break; |
41 case kGL_GLContextType: | 81 case kGL_GLContextType: |
42 glCtx.reset(SkCreatePlatformGLContext(kGL_GrGLStandard)); | 82 glCtx.reset(SkCreatePlatformGLContext(kGL_GrGLStandard)); |
43 break; | 83 break; |
44 case kGLES_GLContextType: | 84 case kGLES_GLContextType: |
45 glCtx.reset(SkCreatePlatformGLContext(kGLES_GrGLStandard)); | 85 glCtx.reset(SkCreatePlatformGLContext(kGLES_GrGLStandard)); |
(...skipping 19 matching lines...) Expand all Loading... |
65 break; | 105 break; |
66 #endif | 106 #endif |
67 case kNull_GLContextType: | 107 case kNull_GLContextType: |
68 glCtx.reset(SkNullGLContext::Create()); | 108 glCtx.reset(SkNullGLContext::Create()); |
69 break; | 109 break; |
70 case kDebug_GLContextType: | 110 case kDebug_GLContextType: |
71 glCtx.reset(SkDebugGLContext::Create()); | 111 glCtx.reset(SkDebugGLContext::Create()); |
72 break; | 112 break; |
73 } | 113 } |
74 if (nullptr == glCtx.get()) { | 114 if (nullptr == glCtx.get()) { |
75 return nullptr; | 115 return ContextInfo(); |
76 } | 116 } |
77 | 117 |
78 SkASSERT(glCtx->isValid()); | 118 SkASSERT(glCtx->isValid()); |
79 | 119 |
80 // Block NVPR from non-NVPR types. | 120 // Block NVPR from non-NVPR types. |
81 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl())); | 121 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl())); |
82 if (!(kEnableNVPR_GLContextOptions & options)) { | 122 if (!(kEnableNVPR_GLContextOptions & options)) { |
83 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface)); | 123 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface)); |
84 if (!glInterface) { | 124 if (!glInterface) { |
85 return nullptr; | 125 return ContextInfo(); |
86 } | 126 } |
87 } | 127 } |
88 | 128 |
89 glCtx->makeCurrent(); | 129 glCtx->makeCurrent(); |
90 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get
()); | 130 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get
()); |
91 #ifdef SK_VULKAN | 131 #ifdef SK_VULKAN |
92 grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions)); | 132 grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions)); |
93 #else | 133 #else |
94 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions)); | 134 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions)); |
95 #endif | 135 #endif |
96 if (!grCtx.get()) { | 136 if (!grCtx.get()) { |
97 return nullptr; | 137 return ContextInfo(); |
98 } | 138 } |
99 if (kEnableNVPR_GLContextOptions & options) { | 139 if (kEnableNVPR_GLContextOptions & options) { |
100 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { | 140 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
101 return nullptr; | 141 return ContextInfo(); |
102 } | 142 } |
103 } | 143 } |
104 | 144 |
105 ContextInfo* ctx = fContexts.emplace_back(new ContextInfo); | 145 Context& context = fContexts.push_back(); |
106 ctx->fGLContext = SkRef(glCtx.get()); | 146 context.fGLContext = glCtx.detach(); |
107 ctx->fGrContext = SkRef(grCtx.get()); | 147 context.fGrContext = SkRef(grCtx.get()); |
108 ctx->fType = type; | 148 context.fType = type; |
109 ctx->fOptions = options; | 149 context.fOptions = options; |
110 return ctx; | 150 return ContextInfo(context.fGrContext, context.fGLContext); |
111 } | 151 } |
OLD | NEW |