1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2012 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef GrContextFactory_DEFINED
9cb93a386Sopenharmony_ci#define GrContextFactory_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/gpu/GrContextOptions.h"
12cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci#include "include/private/SkTArray.h"
15cb93a386Sopenharmony_ci#include "tools/gpu/gl/GLTestContext.h"
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_cistruct GrVkBackendContext;
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_cinamespace sk_gpu_test {
20cb93a386Sopenharmony_ciclass ContextInfo;
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ci/**
23cb93a386Sopenharmony_ci * This is a simple class that is useful in test apps that use different
24cb93a386Sopenharmony_ci * GrContexts backed by different types of GL contexts. It manages creating the
25cb93a386Sopenharmony_ci * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
26cb93a386Sopenharmony_ci * factory is destroyed (though the caller can always grab a ref on the returned
27cb93a386Sopenharmony_ci * Gr and GL contexts to make them outlive the factory).
28cb93a386Sopenharmony_ci */
29cb93a386Sopenharmony_ciclass GrContextFactory : SkNoncopyable {
30cb93a386Sopenharmony_cipublic:
31cb93a386Sopenharmony_ci    // The availability of context types is subject to platform and build configuration
32cb93a386Sopenharmony_ci    // restrictions.
33cb93a386Sopenharmony_ci    enum ContextType {
34cb93a386Sopenharmony_ci        kGL_ContextType,                 //! OpenGL context.
35cb93a386Sopenharmony_ci        kGLES_ContextType,               //! OpenGL ES context.
36cb93a386Sopenharmony_ci        kANGLE_D3D9_ES2_ContextType,     //! ANGLE on Direct3D9 OpenGL ES 2 context.
37cb93a386Sopenharmony_ci        kANGLE_D3D11_ES2_ContextType,    //! ANGLE on Direct3D11 OpenGL ES 2 context.
38cb93a386Sopenharmony_ci        kANGLE_D3D11_ES3_ContextType,    //! ANGLE on Direct3D11 OpenGL ES 3 context.
39cb93a386Sopenharmony_ci        kANGLE_GL_ES2_ContextType,       //! ANGLE on OpenGL OpenGL ES 2 context.
40cb93a386Sopenharmony_ci        kANGLE_GL_ES3_ContextType,       //! ANGLE on OpenGL OpenGL ES 3 context.
41cb93a386Sopenharmony_ci        kCommandBuffer_ES2_ContextType,  //! Chromium command buffer OpenGL ES 2 context.
42cb93a386Sopenharmony_ci        kCommandBuffer_ES3_ContextType,  //! Chromium command buffer OpenGL ES 3 context.
43cb93a386Sopenharmony_ci        kVulkan_ContextType,             //! Vulkan
44cb93a386Sopenharmony_ci        kMetal_ContextType,              //! Metal
45cb93a386Sopenharmony_ci        kDirect3D_ContextType,           //! Direct3D 12
46cb93a386Sopenharmony_ci        kDawn_ContextType,               //! Dawn
47cb93a386Sopenharmony_ci        kMock_ContextType,               //! Mock context that does not draw.
48cb93a386Sopenharmony_ci        kLastContextType = kMock_ContextType
49cb93a386Sopenharmony_ci    };
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_ci    static const int kContextTypeCnt = kLastContextType + 1;
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci    /**
54cb93a386Sopenharmony_ci     * Overrides for the initial GrContextOptions provided at construction time, and required
55cb93a386Sopenharmony_ci     * features that will cause context creation to fail if not present.
56cb93a386Sopenharmony_ci     */
57cb93a386Sopenharmony_ci    enum class ContextOverrides {
58cb93a386Sopenharmony_ci        kNone                          = 0x0,
59cb93a386Sopenharmony_ci        kAvoidStencilBuffers           = 0x1,
60cb93a386Sopenharmony_ci        kFakeGLESVersionAs2            = 0x2,
61cb93a386Sopenharmony_ci        kReducedShaders                = 0x4,
62cb93a386Sopenharmony_ci    };
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci    static bool IsRenderingContext(ContextType type) {
65cb93a386Sopenharmony_ci        switch (type) {
66cb93a386Sopenharmony_ci            case kMock_ContextType:
67cb93a386Sopenharmony_ci                return false;
68cb93a386Sopenharmony_ci            default:
69cb93a386Sopenharmony_ci                return true;
70cb93a386Sopenharmony_ci        }
71cb93a386Sopenharmony_ci    }
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci    static GrBackendApi ContextTypeBackend(ContextType type) {
74cb93a386Sopenharmony_ci        switch (type) {
75cb93a386Sopenharmony_ci            case kVulkan_ContextType:
76cb93a386Sopenharmony_ci                return GrBackendApi::kVulkan;
77cb93a386Sopenharmony_ci            case kMetal_ContextType:
78cb93a386Sopenharmony_ci                return GrBackendApi::kMetal;
79cb93a386Sopenharmony_ci            case kDirect3D_ContextType:
80cb93a386Sopenharmony_ci                return GrBackendApi::kDirect3D;
81cb93a386Sopenharmony_ci            case kDawn_ContextType:
82cb93a386Sopenharmony_ci                return GrBackendApi::kDawn;
83cb93a386Sopenharmony_ci            case kMock_ContextType:
84cb93a386Sopenharmony_ci                return GrBackendApi::kMock;
85cb93a386Sopenharmony_ci            default:
86cb93a386Sopenharmony_ci                return GrBackendApi::kOpenGL;
87cb93a386Sopenharmony_ci        }
88cb93a386Sopenharmony_ci    }
89cb93a386Sopenharmony_ci
90cb93a386Sopenharmony_ci    static const char* ContextTypeName(ContextType contextType) {
91cb93a386Sopenharmony_ci        switch (contextType) {
92cb93a386Sopenharmony_ci            case kGL_ContextType:
93cb93a386Sopenharmony_ci                return "OpenGL";
94cb93a386Sopenharmony_ci            case kGLES_ContextType:
95cb93a386Sopenharmony_ci                return "OpenGLES";
96cb93a386Sopenharmony_ci            case kANGLE_D3D9_ES2_ContextType:
97cb93a386Sopenharmony_ci                return "ANGLE D3D9 ES2";
98cb93a386Sopenharmony_ci            case kANGLE_D3D11_ES2_ContextType:
99cb93a386Sopenharmony_ci                return "ANGLE D3D11 ES2";
100cb93a386Sopenharmony_ci            case kANGLE_D3D11_ES3_ContextType:
101cb93a386Sopenharmony_ci                return "ANGLE D3D11 ES3";
102cb93a386Sopenharmony_ci            case kANGLE_GL_ES2_ContextType:
103cb93a386Sopenharmony_ci                return "ANGLE GL ES2";
104cb93a386Sopenharmony_ci            case kANGLE_GL_ES3_ContextType:
105cb93a386Sopenharmony_ci                return "ANGLE GL ES3";
106cb93a386Sopenharmony_ci            case kCommandBuffer_ES2_ContextType:
107cb93a386Sopenharmony_ci                return "Command Buffer ES2";
108cb93a386Sopenharmony_ci            case kCommandBuffer_ES3_ContextType:
109cb93a386Sopenharmony_ci                return "Command Buffer ES3";
110cb93a386Sopenharmony_ci            case kVulkan_ContextType:
111cb93a386Sopenharmony_ci                return "Vulkan";
112cb93a386Sopenharmony_ci            case kMetal_ContextType:
113cb93a386Sopenharmony_ci                return "Metal";
114cb93a386Sopenharmony_ci            case kDirect3D_ContextType:
115cb93a386Sopenharmony_ci                return "Direct3D";
116cb93a386Sopenharmony_ci            case kDawn_ContextType:
117cb93a386Sopenharmony_ci                return "Dawn";
118cb93a386Sopenharmony_ci            case kMock_ContextType:
119cb93a386Sopenharmony_ci                return "Mock";
120cb93a386Sopenharmony_ci        }
121cb93a386Sopenharmony_ci        SK_ABORT("Unreachable");
122cb93a386Sopenharmony_ci    }
123cb93a386Sopenharmony_ci
124cb93a386Sopenharmony_ci    explicit GrContextFactory(const GrContextOptions& opts);
125cb93a386Sopenharmony_ci    GrContextFactory();
126cb93a386Sopenharmony_ci
127cb93a386Sopenharmony_ci    ~GrContextFactory();
128cb93a386Sopenharmony_ci
129cb93a386Sopenharmony_ci    void destroyContexts();
130cb93a386Sopenharmony_ci    void abandonContexts();
131cb93a386Sopenharmony_ci    void releaseResourcesAndAbandonContexts();
132cb93a386Sopenharmony_ci
133cb93a386Sopenharmony_ci    /**
134cb93a386Sopenharmony_ci     * Get a context initialized with a type of GL context. It also makes the GL context current.
135cb93a386Sopenharmony_ci     */
136cb93a386Sopenharmony_ci    ContextInfo getContextInfo(ContextType type, ContextOverrides = ContextOverrides::kNone);
137cb93a386Sopenharmony_ci
138cb93a386Sopenharmony_ci    /**
139cb93a386Sopenharmony_ci     * Get a context in the same share group as the passed in GrContext, with the same type and
140cb93a386Sopenharmony_ci     * overrides. To get multiple contexts in a single share group, pass the same shareContext,
141cb93a386Sopenharmony_ci     * with different values for shareIndex.
142cb93a386Sopenharmony_ci     */
143cb93a386Sopenharmony_ci    ContextInfo getSharedContextInfo(GrDirectContext* shareContext, uint32_t shareIndex = 0);
144cb93a386Sopenharmony_ci
145cb93a386Sopenharmony_ci    /**
146cb93a386Sopenharmony_ci     * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
147cb93a386Sopenharmony_ci     */
148cb93a386Sopenharmony_ci    GrDirectContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone);
149cb93a386Sopenharmony_ci    const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
150cb93a386Sopenharmony_ci
151cb93a386Sopenharmony_ciprivate:
152cb93a386Sopenharmony_ci    ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
153cb93a386Sopenharmony_ci                                       GrDirectContext* shareContext, uint32_t shareIndex);
154cb93a386Sopenharmony_ci
155cb93a386Sopenharmony_ci    struct Context {
156cb93a386Sopenharmony_ci        ContextType       fType;
157cb93a386Sopenharmony_ci        ContextOverrides  fOverrides;
158cb93a386Sopenharmony_ci        GrContextOptions  fOptions;
159cb93a386Sopenharmony_ci        GrBackendApi      fBackend;
160cb93a386Sopenharmony_ci        TestContext*      fTestContext;
161cb93a386Sopenharmony_ci        GrDirectContext*  fGrContext;
162cb93a386Sopenharmony_ci        GrDirectContext*  fShareContext;
163cb93a386Sopenharmony_ci        uint32_t          fShareIndex;
164cb93a386Sopenharmony_ci
165cb93a386Sopenharmony_ci        bool              fAbandoned;
166cb93a386Sopenharmony_ci    };
167cb93a386Sopenharmony_ci    SkTArray<Context, true>         fContexts;
168cb93a386Sopenharmony_ci    std::unique_ptr<GLTestContext>  fSentinelGLContext;
169cb93a386Sopenharmony_ci    const GrContextOptions          fGlobalOptions;
170cb93a386Sopenharmony_ci};
171cb93a386Sopenharmony_ci
172cb93a386Sopenharmony_ciclass ContextInfo {
173cb93a386Sopenharmony_cipublic:
174cb93a386Sopenharmony_ci    ContextInfo() = default;
175cb93a386Sopenharmony_ci    ContextInfo(const ContextInfo&) = default;
176cb93a386Sopenharmony_ci    ContextInfo& operator=(const ContextInfo&) = default;
177cb93a386Sopenharmony_ci
178cb93a386Sopenharmony_ci    GrContextFactory::ContextType type() const { return fType; }
179cb93a386Sopenharmony_ci    GrBackendApi backend() const { return GrContextFactory::ContextTypeBackend(fType); }
180cb93a386Sopenharmony_ci
181cb93a386Sopenharmony_ci    GrDirectContext* directContext() const { return fContext; }
182cb93a386Sopenharmony_ci    TestContext* testContext() const { return fTestContext; }
183cb93a386Sopenharmony_ci
184cb93a386Sopenharmony_ci#ifdef SK_GL
185cb93a386Sopenharmony_ci    GLTestContext* glContext() const {
186cb93a386Sopenharmony_ci        SkASSERT(GrBackendApi::kOpenGL == this->backend());
187cb93a386Sopenharmony_ci        return static_cast<GLTestContext*>(fTestContext);
188cb93a386Sopenharmony_ci    }
189cb93a386Sopenharmony_ci#endif
190cb93a386Sopenharmony_ci
191cb93a386Sopenharmony_ci    const GrContextOptions& options() const { return fOptions; }
192cb93a386Sopenharmony_ci
193cb93a386Sopenharmony_ciprivate:
194cb93a386Sopenharmony_ci    ContextInfo(GrContextFactory::ContextType type,
195cb93a386Sopenharmony_ci                TestContext* testContext,
196cb93a386Sopenharmony_ci                GrDirectContext* context,
197cb93a386Sopenharmony_ci                const GrContextOptions& options)
198cb93a386Sopenharmony_ci            : fType(type), fTestContext(testContext), fContext(context), fOptions(options) {}
199cb93a386Sopenharmony_ci
200cb93a386Sopenharmony_ci    GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType;
201cb93a386Sopenharmony_ci    // Valid until the factory destroys it via abandonContexts() or destroyContexts().
202cb93a386Sopenharmony_ci    TestContext* fTestContext = nullptr;
203cb93a386Sopenharmony_ci    GrDirectContext* fContext = nullptr;
204cb93a386Sopenharmony_ci    GrContextOptions fOptions;
205cb93a386Sopenharmony_ci
206cb93a386Sopenharmony_ci    friend class GrContextFactory;
207cb93a386Sopenharmony_ci};
208cb93a386Sopenharmony_ci
209cb93a386Sopenharmony_ci}  // namespace sk_gpu_test
210cb93a386Sopenharmony_ci
211cb93a386Sopenharmony_ciGR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides)
212cb93a386Sopenharmony_ci
213cb93a386Sopenharmony_ci#endif
214