1 /*
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 #include "GrContext.h"
12 #include "GrContextOptions.h"
13 
14 #include "gl/SkGLContext.h"
15 #include "SkTArray.h"
16 
17 /**
18  * This is a simple class that is useful in test apps that use different
19  * GrContexts backed by different types of GL contexts. It manages creating the
20  * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
21  * factory is destroyed (though the caller can always grab a ref on the returned
22  * Gr and GL contexts to make them outlive the factory).
23  */
24 class GrContextFactory : SkNoncopyable {
25 public:
26     enum GLContextType {
27         kNative_GLContextType,  //! OpenGL or OpenGL ES context.
28         kGL_GLContextType,      //! OpenGL context.
29         kGLES_GLContextType,    //! OpenGL ES context.
30 #if SK_ANGLE
31 #ifdef SK_BUILD_FOR_WIN
32         kANGLE_GLContextType,    //! ANGLE on DirectX OpenGL ES context.
33 #endif
34         kANGLE_GL_GLContextType, //! ANGLE on OpenGL OpenGL ES context.
35 #endif
36 #if SK_COMMAND_BUFFER
37         kCommandBufferES2_GLContextType, //! Chromium command buffer OpenGL ES 2.0 context.
38         kCommandBufferES3_GLContextType, //! Chromium command buffer OpenGL ES 3.0 context.
39                                          //! Not ready for production.
40 #endif
41 #if SK_MESA
42         kMESA_GLContextType,  //! MESA OpenGL context
43 #endif
44         kNull_GLContextType,  //! Non-rendering OpenGL mock context.
45         kDebug_GLContextType, //! Non-rendering, state verifying OpenGL context.
46         kLastGLContextType = kDebug_GLContextType
47     };
48 
49     static const int kGLContextTypeCnt = kLastGLContextType + 1;
50 
51     /**
52      * Options for GL context creation. For historical and testing reasons the options will default
53      * to not using GL_NV_path_rendering extension  even when the driver supports it.
54      */
55     enum GLContextOptions {
56         kNone_GLContextOptions = 0,
57         kEnableNVPR_GLContextOptions = 0x1,
58     };
59 
IsRenderingGLContext(GLContextType type)60     static bool IsRenderingGLContext(GLContextType type) {
61         switch (type) {
62             case kNull_GLContextType:
63             case kDebug_GLContextType:
64                 return false;
65             default:
66                 return true;
67         }
68     }
69 
GLContextTypeName(GLContextType type)70     static const char* GLContextTypeName(GLContextType type) {
71         switch (type) {
72             case kNative_GLContextType:
73                 return "native";
74             case kGL_GLContextType:
75                 return "gl";
76             case kGLES_GLContextType:
77                 return "gles";
78 #if SK_ANGLE
79 #ifdef SK_BUILD_FOR_WIN
80             case kANGLE_GLContextType:
81                 return "angle";
82 #endif
83             case kANGLE_GL_GLContextType:
84                 return "angle-gl";
85 #endif
86 #if SK_COMMAND_BUFFER
87             case kCommandBufferES2_GLContextType:
88                 return "commandbuffer";
89             case kCommandBufferES3_GLContextType:
90                 return "commandbuffer3";
91 #endif
92 #if SK_MESA
93             case kMESA_GLContextType:
94                 return "mesa";
95 #endif
96             case kNull_GLContextType:
97                 return "null";
98             case kDebug_GLContextType:
99                 return "debug";
100             default:
101                 SkFAIL("Unknown GL Context type.");
102         }
103     }
104 
105     explicit GrContextFactory(const GrContextOptions& opts);
106     GrContextFactory();
107 
108     ~GrContextFactory();
109 
110     void destroyContexts();
111     void abandonContexts();
112 
113     struct ContextInfo {
ContextInfoContextInfo114         ContextInfo()
115             : fGrContext(nullptr), fGLContext(nullptr) { }
ContextInfoContextInfo116         ContextInfo(GrContext* grContext, SkGLContext* glContext)
117             : fGrContext(grContext), fGLContext(glContext) { }
118         GrContext* fGrContext;
119         SkGLContext* fGLContext; //! Valid until the factory destroys it via abandonContexts() or
120                                  //! destroyContexts().
121     };
122 
123     /**
124      * Get a context initialized with a type of GL context. It also makes the GL context current.
125      */
126     ContextInfo getContextInfo(GLContextType type,
127                                GLContextOptions options = kNone_GLContextOptions);
128     /**
129      * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
130      */
131     GrContext* get(GLContextType type,
132                    GLContextOptions options = kNone_GLContextOptions) {
133         return this->getContextInfo(type, options).fGrContext;
134     }
getGlobalOptions()135     const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
136 
137 private:
138     struct Context {
139         GLContextType fType;
140         GLContextOptions fOptions;
141         SkGLContext*  fGLContext;
142         GrContext*    fGrContext;
143     };
144     SkTArray<Context, true> fContexts;
145     const GrContextOptions  fGlobalOptions;
146 };
147 
148 #endif
149