Home | History | Annotate | Download | only in gpu
      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/GLTestContext.h"
     15 #include "vk/VkTestContext.h"
     16 #include "SkTArray.h"
     17 
     18 struct GrVkBackendContext;
     19 
     20 namespace sk_gpu_test {
     21 
     22 class ContextInfo {
     23 public:
     24     ContextInfo() = default;
     25     ContextInfo& operator=(const ContextInfo&) = default;
     26 
     27     GrBackend backend() const { return fBackend; }
     28 
     29     GrContext* grContext() const { return fGrContext; }
     30 
     31     TestContext* testContext() const { return fTestContext; }
     32 
     33     GLTestContext* glContext() const {
     34         SkASSERT(kOpenGL_GrBackend == fBackend);
     35         return static_cast<GLTestContext*>(fTestContext);
     36     }
     37 
     38 #ifdef SK_VULKAN
     39     VkTestContext* vkContext() const {
     40         SkASSERT(kVulkan_GrBackend == fBackend);
     41         return static_cast<VkTestContext*>(fTestContext);
     42     }
     43 #endif
     44 
     45 private:
     46     ContextInfo(GrBackend backend, TestContext* testContext, GrContext* grContext)
     47             : fBackend(backend)
     48             , fTestContext(testContext)
     49             , fGrContext(grContext) {}
     50 
     51     GrBackend       fBackend = kOpenGL_GrBackend;
     52     // Valid until the factory destroys it via abandonContexts() or destroyContexts().
     53     TestContext*    fTestContext = nullptr;
     54     GrContext*      fGrContext = nullptr;
     55 
     56     friend class GrContextFactory;
     57 };
     58 
     59 /**
     60  * This is a simple class that is useful in test apps that use different
     61  * GrContexts backed by different types of GL contexts. It manages creating the
     62  * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
     63  * factory is destroyed (though the caller can always grab a ref on the returned
     64  * Gr and GL contexts to make them outlive the factory).
     65  */
     66 class GrContextFactory : SkNoncopyable {
     67 public:
     68     // The availability of context types is subject to platform and build configuration
     69     // restrictions.
     70     enum ContextType {
     71         kGL_ContextType,             //! OpenGL context.
     72         kGLES_ContextType,           //! OpenGL ES context.
     73         kANGLE_D3D9_ES2_ContextType, //! ANGLE on Direct3D9 OpenGL ES 2 context.
     74         kANGLE_D3D11_ES2_ContextType,//! ANGLE on Direct3D11 OpenGL ES 2 context.
     75         kANGLE_D3D11_ES3_ContextType,//! ANGLE on Direct3D11 OpenGL ES 3 context.
     76         kANGLE_GL_ES2_ContextType,   //! ANGLE on OpenGL OpenGL ES 2 context.
     77         kANGLE_GL_ES3_ContextType,   //! ANGLE on OpenGL OpenGL ES 3 context.
     78         kCommandBuffer_ContextType,  //! Chromium command buffer OpenGL ES context.
     79         kMESA_ContextType,           //! MESA OpenGL context
     80         kNullGL_ContextType,         //! Non-rendering OpenGL mock context.
     81         kDebugGL_ContextType,        //! Non-rendering, state verifying OpenGL context.
     82         kVulkan_ContextType,         //! Vulkan
     83         kLastContextType = kVulkan_ContextType
     84     };
     85 
     86     static const int kContextTypeCnt = kLastContextType + 1;
     87 
     88     /**
     89      * Overrides for the initial GrContextOptions provided at construction time, and required
     90      * features that will cause context creation to fail if not present.
     91      */
     92     enum class ContextOverrides {
     93         kNone                          = 0x0,
     94         kDisableNVPR                   = 0x1,
     95         kUseInstanced                  = 0x2,
     96         kAllowSRGBWithoutDecodeControl = 0x4,
     97 
     98         kRequireNVPRSupport            = 0x8,
     99         kRequireSRGBSupport            = 0x10
    100     };
    101 
    102     static bool IsRenderingContext(ContextType type) {
    103         switch (type) {
    104             case kNullGL_ContextType:
    105             case kDebugGL_ContextType:
    106                 return false;
    107             default:
    108                 return true;
    109         }
    110     }
    111 
    112     static GrBackend ContextTypeBackend(ContextType type) {
    113         switch (type) {
    114             case kVulkan_ContextType:
    115                 return kVulkan_GrBackend;
    116             default:
    117                 return kOpenGL_GrBackend;
    118         }
    119     }
    120 
    121     explicit GrContextFactory(const GrContextOptions& opts);
    122     GrContextFactory();
    123 
    124     ~GrContextFactory();
    125 
    126     void destroyContexts();
    127     void abandonContexts();
    128     void releaseResourcesAndAbandonContexts();
    129 
    130     /**
    131      * Get a context initialized with a type of GL context. It also makes the GL context current.
    132      */
    133     ContextInfo getContextInfo(ContextType type,
    134                                ContextOverrides overrides = ContextOverrides::kNone);
    135 
    136     /**
    137      * Get a context in the same share group as the passed in GrContext, with the same type and
    138      * overrides. To get multiple contexts in a single share group, pass the same shareContext,
    139      * with different values for shareIndex.
    140      */
    141     ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0);
    142 
    143     /**
    144      * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
    145      */
    146     GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone) {
    147         return this->getContextInfo(type, overrides).grContext();
    148     }
    149     const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
    150 
    151 private:
    152     ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
    153                                        GrContext* shareContext, uint32_t shareIndex);
    154 
    155     struct Context {
    156         ContextType       fType;
    157         ContextOverrides  fOverrides;
    158         GrBackend         fBackend;
    159         TestContext*      fTestContext;
    160         GrContext*        fGrContext;
    161         GrContext*        fShareContext;
    162         uint32_t          fShareIndex;
    163 
    164         bool            fAbandoned;
    165     };
    166     SkTArray<Context, true>         fContexts;
    167     std::unique_ptr<GLTestContext>  fSentinelGLContext;
    168     const GrContextOptions          fGlobalOptions;
    169 };
    170 }  // namespace sk_gpu_test
    171 
    172 GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides);
    173 
    174 #endif
    175