Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2010 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 GrTypes_DEFINED
      9 #define GrTypes_DEFINED
     10 
     11 #include "SkMath.h"
     12 #include "SkTypes.h"
     13 #include "GrConfig.h"
     14 
     15 ////////////////////////////////////////////////////////////////////////////////
     16 
     17 /**
     18  * Defines overloaded bitwise operators to make it easier to use an enum as a
     19  * bitfield.
     20  */
     21 #define GR_MAKE_BITFIELD_OPS(X) \
     22     inline X operator |(X a, X b) { \
     23         return (X) (+a | +b); \
     24     } \
     25     inline X& operator |=(X& a, X b) { \
     26         return (a = a | b); \
     27     } \
     28     inline X operator &(X a, X b) { \
     29         return (X) (+a & +b); \
     30     } \
     31     inline X& operator &=(X& a, X b) { \
     32         return (a = a & b); \
     33     } \
     34     template <typename T> \
     35     inline X operator &(T a, X b) { \
     36         return (X) (+a & +b); \
     37     } \
     38     template <typename T> \
     39     inline X operator &(X a, T b) { \
     40         return (X) (+a & +b); \
     41     } \
     42 
     43 #define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
     44     friend X operator |(X a, X b); \
     45     friend X& operator |=(X& a, X b); \
     46     \
     47     friend X operator &(X a, X b); \
     48     friend X& operator &=(X& a, X b); \
     49     \
     50     template <typename T> \
     51     friend X operator &(T a, X b); \
     52     \
     53     template <typename T> \
     54     friend X operator &(X a, T b); \
     55 
     56 /**
     57  * Wraps a C++11 enum that we use as a bitfield, and enables a limited amount of
     58  * masking with type safety. Instantiated with the ~ operator.
     59  */
     60 template<typename TFlags> class GrTFlagsMask {
     61 public:
     62     constexpr explicit GrTFlagsMask(TFlags value) : GrTFlagsMask(static_cast<int>(value)) {}
     63     constexpr explicit GrTFlagsMask(int value) : fValue(value) {}
     64     constexpr int value() const { return fValue; }
     65 private:
     66     const int fValue;
     67 };
     68 
     69 // Or-ing a mask always returns another mask.
     70 template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator|(GrTFlagsMask<TFlags> a,
     71                                                                    GrTFlagsMask<TFlags> b) {
     72     return GrTFlagsMask<TFlags>(a.value() | b.value());
     73 }
     74 template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator|(GrTFlagsMask<TFlags> a,
     75                                                                    TFlags b) {
     76     return GrTFlagsMask<TFlags>(a.value() | static_cast<int>(b));
     77 }
     78 template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator|(TFlags a,
     79                                                                    GrTFlagsMask<TFlags> b) {
     80     return GrTFlagsMask<TFlags>(static_cast<int>(a) | b.value());
     81 }
     82 template<typename TFlags> inline GrTFlagsMask<TFlags>& operator|=(GrTFlagsMask<TFlags>& a,
     83                                                                   GrTFlagsMask<TFlags> b) {
     84     return (a = a | b);
     85 }
     86 
     87 // And-ing two masks returns another mask; and-ing one with regular flags returns flags.
     88 template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator&(GrTFlagsMask<TFlags> a,
     89                                                                    GrTFlagsMask<TFlags> b) {
     90     return GrTFlagsMask<TFlags>(a.value() & b.value());
     91 }
     92 template<typename TFlags> constexpr TFlags operator&(GrTFlagsMask<TFlags> a, TFlags b) {
     93     return static_cast<TFlags>(a.value() & static_cast<int>(b));
     94 }
     95 template<typename TFlags> constexpr TFlags operator&(TFlags a, GrTFlagsMask<TFlags> b) {
     96     return static_cast<TFlags>(static_cast<int>(a) & b.value());
     97 }
     98 template<typename TFlags> inline TFlags& operator&=(TFlags& a, GrTFlagsMask<TFlags> b) {
     99     return (a = a & b);
    100 }
    101 
    102 /**
    103  * Defines bitwise operators that make it possible to use an enum class as a
    104  * basic bitfield.
    105  */
    106 #define GR_MAKE_BITFIELD_CLASS_OPS(X) \
    107     constexpr GrTFlagsMask<X> operator~(X a) { \
    108         return GrTFlagsMask<X>(~static_cast<int>(a)); \
    109     } \
    110     constexpr X operator|(X a, X b) { \
    111         return static_cast<X>(static_cast<int>(a) | static_cast<int>(b)); \
    112     } \
    113     inline X& operator|=(X& a, X b) { \
    114         return (a = a | b); \
    115     } \
    116     constexpr bool operator&(X a, X b) { \
    117         return SkToBool(static_cast<int>(a) & static_cast<int>(b)); \
    118     } \
    119 
    120 #define GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(X) \
    121     friend constexpr GrTFlagsMask<X> operator ~(X); \
    122     friend constexpr X operator |(X, X); \
    123     friend X& operator |=(X&, X); \
    124     friend constexpr bool operator &(X, X)
    125 
    126 ////////////////////////////////////////////////////////////////////////////////
    127 
    128 // compile time versions of min/max
    129 #define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
    130 #define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
    131 
    132 /**
    133  *  divide, rounding up
    134  */
    135 static inline int32_t GrIDivRoundUp(int x, int y) {
    136     SkASSERT(y > 0);
    137     return (x + (y-1)) / y;
    138 }
    139 static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
    140     return (x + (y-1)) / y;
    141 }
    142 static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
    143     return (x + (y-1)) / y;
    144 }
    145 
    146 // compile time, evaluates Y multiple times
    147 #define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
    148 
    149 /**
    150  *  align up
    151  */
    152 static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
    153     return GrUIDivRoundUp(x, alignment) * alignment;
    154 }
    155 static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
    156     return GrSizeDivRoundUp(x, alignment) * alignment;
    157 }
    158 
    159 // compile time, evaluates A multiple times
    160 #define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
    161 
    162 /**
    163  * amount of pad needed to align up
    164  */
    165 static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
    166     return (alignment - x % alignment) % alignment;
    167 }
    168 static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
    169     return (alignment - x % alignment) % alignment;
    170 }
    171 
    172 /**
    173  *  align down
    174  */
    175 static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
    176     return (x / alignment) * alignment;
    177 }
    178 static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
    179     return (x / alignment) * alignment;
    180 }
    181 
    182 ///////////////////////////////////////////////////////////////////////////////
    183 
    184 /**
    185  * Possible 3D APIs that may be used by Ganesh.
    186  */
    187 enum class GrBackendApi : unsigned {
    188     kMetal,
    189     kOpenGL,
    190     kVulkan,
    191     /**
    192      * Mock is a backend that does not draw anything. It is used for unit tests
    193      * and to measure CPU overhead.
    194      */
    195     kMock,
    196 
    197     /**
    198      * Added here to support the legacy GrBackend enum value and clients who referenced it using
    199      * GrBackend::kOpenGL_GrBackend.
    200      */
    201     kOpenGL_GrBackend = kOpenGL,
    202 };
    203 
    204 /**
    205  * Previously the above enum was not an enum class but a normal enum. To support the legacy use of
    206  * the enum values we define them below so that no clients break.
    207  */
    208 typedef GrBackendApi GrBackend;
    209 
    210 static constexpr GrBackendApi kMetal_GrBackend = GrBackendApi::kMetal;
    211 static constexpr GrBackendApi kVulkan_GrBackend = GrBackendApi::kVulkan;
    212 static constexpr GrBackendApi kMock_GrBackend = GrBackendApi::kMock;
    213 
    214 ///////////////////////////////////////////////////////////////////////////////
    215 
    216 /**
    217  * Used to say whether a texture has mip levels allocated or not.
    218  */
    219 enum class GrMipMapped : bool {
    220     kNo = false,
    221     kYes = true
    222 };
    223 
    224 ///////////////////////////////////////////////////////////////////////////////
    225 
    226 /**
    227  * GPU SkImage and SkSurfaces can be stored such that (0, 0) in texture space may correspond to
    228  * either the top-left or bottom-left content pixel.
    229  */
    230 enum GrSurfaceOrigin : int {
    231     kTopLeft_GrSurfaceOrigin,
    232     kBottomLeft_GrSurfaceOrigin,
    233 };
    234 
    235 /**
    236  * A GrContext's cache of backend context state can be partially invalidated.
    237  * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
    238  */
    239 enum GrGLBackendState {
    240     kRenderTarget_GrGLBackendState     = 1 << 0,
    241     // Also includes samplers bound to texture units.
    242     kTextureBinding_GrGLBackendState   = 1 << 1,
    243     // View state stands for scissor and viewport
    244     kView_GrGLBackendState             = 1 << 2,
    245     kBlend_GrGLBackendState            = 1 << 3,
    246     kMSAAEnable_GrGLBackendState       = 1 << 4,
    247     kVertex_GrGLBackendState           = 1 << 5,
    248     kStencil_GrGLBackendState          = 1 << 6,
    249     kPixelStore_GrGLBackendState       = 1 << 7,
    250     kProgram_GrGLBackendState          = 1 << 8,
    251     kFixedFunction_GrGLBackendState    = 1 << 9,
    252     kMisc_GrGLBackendState             = 1 << 10,
    253     kPathRendering_GrGLBackendState    = 1 << 11,
    254     kALL_GrGLBackendState              = 0xffff
    255 };
    256 
    257 /**
    258  * This value translates to reseting all the context state for any backend.
    259  */
    260 static const uint32_t kAll_GrBackendState = 0xffffffff;
    261 
    262 enum GrFlushFlags {
    263     kNone_GrFlushFlags = 0,
    264     // flush will wait till all submitted GPU work is finished before returning.
    265     kSyncCpu_GrFlushFlag = 0x1,
    266 };
    267 
    268 typedef void* GrGpuFinishedContext;
    269 typedef void (*GrGpuFinishedProc)(GrGpuFinishedContext finishedContext);
    270 
    271 /**
    272  * Enum used as return value when flush with semaphores so the client knows whether the semaphores
    273  * were submitted to GPU or not.
    274  */
    275 enum class GrSemaphoresSubmitted : bool {
    276     kNo = false,
    277     kYes = true
    278 };
    279 
    280 #endif
    281