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