blob: 389c398c7c5720166c493503879d9e34ce687b93 [file] [log] [blame]
bsalomon@google.com7361f542012-04-19 19:15:35 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrContextFactory_DEFINED
9#define GrContextFactory_DEFINED
10
11#if SK_ANGLE
12 #include "gl/SkANGLEGLContext.h"
13#endif
14#include "gl/SkDebugGLContext.h"
15#if SK_MESA
16 #include "gl/SkMesaGLContext.h"
17#endif
18#include "gl/SkNativeGLContext.h"
19#include "gl/SkNullGLContext.h"
20
21#include "GrContext.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000022#include "SkTArray.h"
bsalomon@google.com7361f542012-04-19 19:15:35 +000023
24/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000025 * This is a simple class that is useful in test apps that use different
bsalomon@google.com7361f542012-04-19 19:15:35 +000026 * GrContexts backed by different types of GL contexts. It manages creating the
27 * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
28 * factory is destroyed (though the caller can always grab a ref on the returned
29 * GrContext to make it outlive the factory).
30 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000031class GrContextFactory : public SkNoncopyable {
bsalomon@google.com7361f542012-04-19 19:15:35 +000032public:
33 /**
34 * Types of GL contexts supported.
35 */
36 enum GLContextType {
37 kNative_GLContextType,
38#if SK_ANGLE
39 kANGLE_GLContextType,
40#endif
41#if SK_MESA
42 kMESA_GLContextType,
43#endif
44 kNull_GLContextType,
45 kDebug_GLContextType,
bsalomon@google.com67b915d2013-02-04 16:13:32 +000046
47 kLastGLContextType = kDebug_GLContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000048 };
49
bsalomon@google.com67b915d2013-02-04 16:13:32 +000050 static const int kGLContextTypeCnt = kLastGLContextType + 1;
51
52 static bool IsRenderingGLContext(GLContextType type) {
53 switch (type) {
54 case kNull_GLContextType:
55 case kDebug_GLContextType:
56 return false;
57 default:
58 return true;
59 }
60 }
61
bsalomon@google.comcb265352013-02-22 16:13:16 +000062 static const char* GLContextTypeName(GLContextType type) {
63 switch (type) {
64 case kNative_GLContextType:
65 return "native";
66 case kNull_GLContextType:
67 return "null";
68#if SK_ANGLE
69 case kANGLE_GLContextType:
70 return "angle";
71#endif
72#if SK_MESA
73 case kMESA_GLContextType:
74 return "mesa";
75#endif
76 case kDebug_GLContextType:
77 return "debug";
78 default:
79 GrCrash("Unknown GL Context type.");
80 }
81 }
82
bsalomon@google.com7361f542012-04-19 19:15:35 +000083 GrContextFactory() {
84 }
85
bsalomon@google.com67b915d2013-02-04 16:13:32 +000086 ~GrContextFactory() { this->destroyContexts(); }
87
88 void destroyContexts() {
bsalomon@google.com7361f542012-04-19 19:15:35 +000089 for (int i = 0; i < fContexts.count(); ++i) {
90 fContexts[i].fGrContext->unref();
91 fContexts[i].fGLContext->unref();
92 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +000093 fContexts.reset();
bsalomon@google.com7361f542012-04-19 19:15:35 +000094 }
95
96 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +000097 * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
bsalomon@google.com7361f542012-04-19 19:15:35 +000098 */
99 GrContext* get(GLContextType type) {
100
101 for (int i = 0; i < fContexts.count(); ++i) {
102 if (fContexts[i].fType == type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000103 fContexts[i].fGLContext->makeCurrent();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000104 return fContexts[i].fGrContext;
105 }
106 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000107 SkAutoTUnref<SkGLContextHelper> glCtx;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000108 SkAutoTUnref<GrContext> grCtx;
109 switch (type) {
110 case kNative_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000111 glCtx.reset(SkNEW(SkNativeGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000112 break;
113#ifdef SK_ANGLE
114 case kANGLE_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000115 glCtx.reset(SkNEW(SkANGLEGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000116 break;
117#endif
118#ifdef SK_MESA
119 case kMESA_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000120 glCtx.reset(SkNEW(SkMesaGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000121 break;
122#endif
123 case kNull_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000124 glCtx.reset(SkNEW(SkNullGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000125 break;
126 case kDebug_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000127 glCtx.reset(SkNEW(SkDebugGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000128 break;
129 }
130 static const int kBogusSize = 1;
131 if (!glCtx.get()) {
132 return NULL;
133 }
134 if (!glCtx.get()->init(kBogusSize, kBogusSize)) {
135 return NULL;
136 }
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000137 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glCtx.get()->gl());
138 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000139 if (!grCtx.get()) {
140 return NULL;
141 }
142 GPUContext& ctx = fContexts.push_back();
143 ctx.fGLContext = glCtx.get();
144 ctx.fGLContext->ref();
145 ctx.fGrContext = grCtx.get();
146 ctx.fGrContext->ref();
147 ctx.fType = type;
148 return ctx.fGrContext;
149 }
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000150
151 // Returns the GLContext of the given type. If it has not been created yet,
152 // NULL is returned instead.
robertphillips@google.com6177e692013-02-28 20:16:25 +0000153 SkGLContextHelper* getGLContext(GLContextType type) {
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000154 for (int i = 0; i < fContexts.count(); ++i) {
155 if (fContexts[i].fType == type) {
156 return fContexts[i].fGLContext;
157 }
158 }
159
160 return NULL;
161 }
162
bsalomon@google.com7361f542012-04-19 19:15:35 +0000163private:
164 struct GPUContext {
165 GLContextType fType;
robertphillips@google.com6177e692013-02-28 20:16:25 +0000166 SkGLContextHelper* fGLContext;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000167 GrContext* fGrContext;
168 };
169 SkTArray<GPUContext, true> fContexts;
170};
171
172#endif