Home | History | Annotate | Download | only in private
      1 /*
      2  * Copyright 2013 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 GrTypesPriv_DEFINED
      9 #define GrTypesPriv_DEFINED
     10 
     11 #include <chrono>
     12 #include "GrTypes.h"
     13 #include "SkRefCnt.h"
     14 
     15 class GrCaps;
     16 
     17 // The old libstdc++ uses the draft name "monotonic_clock" rather than "steady_clock". This might
     18 // not actually be monotonic, depending on how libstdc++ was built. However, this is only currently
     19 // used for idle resource purging so it shouldn't cause a correctness problem.
     20 #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000)
     21 using GrStdSteadyClock = std::chrono::monotonic_clock;
     22 #else
     23 using GrStdSteadyClock = std::chrono::steady_clock;
     24 #endif
     25 
     26 /** This enum indicates the type of antialiasing to be performed. */
     27 enum class GrAAType : unsigned {
     28     /** No antialiasing */
     29     kNone,
     30     /** Use fragment shader code to compute a fractional pixel coverage. */
     31     kCoverage,
     32     /** Use normal MSAA. */
     33     kMSAA,
     34     /**
     35      * Use "mixed samples" MSAA such that the stencil buffer is multisampled but the color buffer is
     36      * not.
     37      */
     38     kMixedSamples
     39 };
     40 
     41 static inline bool GrAATypeIsHW(GrAAType type) {
     42     switch (type) {
     43         case GrAAType::kNone:
     44             return false;
     45         case GrAAType::kCoverage:
     46             return false;
     47         case GrAAType::kMSAA:
     48             return true;
     49         case GrAAType::kMixedSamples:
     50             return true;
     51     }
     52     SkFAIL("Unknown AA Type");
     53     return false;
     54 }
     55 
     56 /** The type of full scene antialiasing supported by a render target. */
     57 enum class GrFSAAType {
     58     /** No FSAA */
     59     kNone,
     60     /** Regular MSAA where each attachment has the same sample count. */
     61     kUnifiedMSAA,
     62     /** One color sample, N stencil samples. */
     63     kMixedSamples,
     64 };
     65 
     66 /**
     67  * Not all drawing code paths support using mixed samples when available and instead use
     68  * coverage-based aa.
     69  */
     70 enum class GrAllowMixedSamples { kNo, kYes };
     71 
     72 GrAAType GrChooseAAType(GrAA, GrFSAAType, GrAllowMixedSamples, const GrCaps&);
     73 
     74 /**
     75  * Types of shader-language-specific boxed variables we can create. (Currently only GrGLShaderVars,
     76  * but should be applicable to other shader languages.)
     77  */
     78 enum GrSLType {
     79     kVoid_GrSLType,
     80     kBool_GrSLType,
     81     kInt_GrSLType,
     82     kUint_GrSLType,
     83     kFloat_GrSLType,
     84     kVec2f_GrSLType,
     85     kVec3f_GrSLType,
     86     kVec4f_GrSLType,
     87     kVec2i_GrSLType,
     88     kVec3i_GrSLType,
     89     kVec4i_GrSLType,
     90     kMat22f_GrSLType,
     91     kMat33f_GrSLType,
     92     kMat44f_GrSLType,
     93     kTexture2DSampler_GrSLType,
     94     kITexture2DSampler_GrSLType,
     95     kTextureExternalSampler_GrSLType,
     96     kTexture2DRectSampler_GrSLType,
     97     kBufferSampler_GrSLType,
     98     kTexture2D_GrSLType,
     99     kSampler_GrSLType,
    100     kImageStorage2D_GrSLType,
    101     kIImageStorage2D_GrSLType,
    102 };
    103 
    104 enum GrShaderType {
    105     kVertex_GrShaderType,
    106     kGeometry_GrShaderType,
    107     kFragment_GrShaderType,
    108 
    109     kLastkFragment_GrShaderType = kFragment_GrShaderType
    110 };
    111 static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
    112 
    113 enum GrShaderFlags {
    114     kNone_GrShaderFlags = 0,
    115     kVertex_GrShaderFlag = 1 << kVertex_GrShaderType,
    116     kGeometry_GrShaderFlag = 1 << kGeometry_GrShaderType,
    117     kFragment_GrShaderFlag = 1 << kFragment_GrShaderType
    118 };
    119 GR_MAKE_BITFIELD_OPS(GrShaderFlags);
    120 
    121 /**
    122  * Precisions of shader language variables. Not all shading languages support precisions or actually
    123  * vary the internal precision based on the qualifiers. These currently only apply to float types (
    124  * including float vectors and matrices).
    125  */
    126 enum GrSLPrecision {
    127     kLow_GrSLPrecision,
    128     kMedium_GrSLPrecision,
    129     kHigh_GrSLPrecision,
    130 
    131     // Default precision is a special tag that means "whatever the default for the program/type
    132     // combination is". In other words, it maps to the empty string in shader code. There are some
    133     // scenarios where kDefault is not allowed (as the default precision for a program, or for
    134     // varyings, for example).
    135     kDefault_GrSLPrecision,
    136 
    137     // We only consider the "real" precisions here
    138     kLast_GrSLPrecision = kHigh_GrSLPrecision,
    139 };
    140 
    141 static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
    142 
    143 /** Is the shading language type float (including vectors/matrices)? */
    144 static inline bool GrSLTypeIsFloatType(GrSLType type) {
    145     switch (type) {
    146         case kFloat_GrSLType:
    147         case kVec2f_GrSLType:
    148         case kVec3f_GrSLType:
    149         case kVec4f_GrSLType:
    150         case kMat22f_GrSLType:
    151         case kMat33f_GrSLType:
    152         case kMat44f_GrSLType:
    153             return true;
    154 
    155         case kVoid_GrSLType:
    156         case kTexture2DSampler_GrSLType:
    157         case kITexture2DSampler_GrSLType:
    158         case kTextureExternalSampler_GrSLType:
    159         case kTexture2DRectSampler_GrSLType:
    160         case kBufferSampler_GrSLType:
    161         case kBool_GrSLType:
    162         case kInt_GrSLType:
    163         case kUint_GrSLType:
    164         case kVec2i_GrSLType:
    165         case kVec3i_GrSLType:
    166         case kVec4i_GrSLType:
    167         case kTexture2D_GrSLType:
    168         case kSampler_GrSLType:
    169         case kImageStorage2D_GrSLType:
    170         case kIImageStorage2D_GrSLType:
    171             return false;
    172     }
    173     SkFAIL("Unexpected type");
    174     return false;
    175 }
    176 
    177 static inline bool GrSLTypeIs2DCombinedSamplerType(GrSLType type) {
    178     switch (type) {
    179         case kTexture2DSampler_GrSLType:
    180         case kITexture2DSampler_GrSLType:
    181         case kTextureExternalSampler_GrSLType:
    182         case kTexture2DRectSampler_GrSLType:
    183             return true;
    184 
    185         case kVoid_GrSLType:
    186         case kFloat_GrSLType:
    187         case kVec2f_GrSLType:
    188         case kVec3f_GrSLType:
    189         case kVec4f_GrSLType:
    190         case kVec2i_GrSLType:
    191         case kVec3i_GrSLType:
    192         case kVec4i_GrSLType:
    193         case kMat22f_GrSLType:
    194         case kMat33f_GrSLType:
    195         case kMat44f_GrSLType:
    196         case kBufferSampler_GrSLType:
    197         case kInt_GrSLType:
    198         case kUint_GrSLType:
    199         case kBool_GrSLType:
    200         case kTexture2D_GrSLType:
    201         case kSampler_GrSLType:
    202         case kImageStorage2D_GrSLType:
    203         case kIImageStorage2D_GrSLType:
    204             return false;
    205     }
    206     SkFAIL("Unexpected type");
    207     return false;
    208 }
    209 
    210 static inline bool GrSLTypeIsCombinedSamplerType(GrSLType type) {
    211     switch (type) {
    212         case kTexture2DSampler_GrSLType:
    213         case kITexture2DSampler_GrSLType:
    214         case kTextureExternalSampler_GrSLType:
    215         case kTexture2DRectSampler_GrSLType:
    216         case kBufferSampler_GrSLType:
    217             return true;
    218 
    219         case kVoid_GrSLType:
    220         case kFloat_GrSLType:
    221         case kVec2f_GrSLType:
    222         case kVec3f_GrSLType:
    223         case kVec4f_GrSLType:
    224         case kVec2i_GrSLType:
    225         case kVec3i_GrSLType:
    226         case kVec4i_GrSLType:
    227         case kMat22f_GrSLType:
    228         case kMat33f_GrSLType:
    229         case kMat44f_GrSLType:
    230         case kInt_GrSLType:
    231         case kUint_GrSLType:
    232         case kBool_GrSLType:
    233         case kTexture2D_GrSLType:
    234         case kSampler_GrSLType:
    235         case kImageStorage2D_GrSLType:
    236         case kIImageStorage2D_GrSLType:
    237             return false;
    238     }
    239     SkFAIL("Unexpected type");
    240     return false;
    241 }
    242 
    243 static inline bool GrSLTypeIsImageStorage(GrSLType type) {
    244     switch (type) {
    245         case kImageStorage2D_GrSLType:
    246         case kIImageStorage2D_GrSLType:
    247             return true;
    248 
    249         case kVoid_GrSLType:
    250         case kFloat_GrSLType:
    251         case kVec2f_GrSLType:
    252         case kVec3f_GrSLType:
    253         case kVec4f_GrSLType:
    254         case kVec2i_GrSLType:
    255         case kVec3i_GrSLType:
    256         case kVec4i_GrSLType:
    257         case kMat22f_GrSLType:
    258         case kMat33f_GrSLType:
    259         case kMat44f_GrSLType:
    260         case kInt_GrSLType:
    261         case kUint_GrSLType:
    262         case kBool_GrSLType:
    263         case kTexture2D_GrSLType:
    264         case kSampler_GrSLType:
    265         case kTexture2DSampler_GrSLType:
    266         case kITexture2DSampler_GrSLType:
    267         case kTextureExternalSampler_GrSLType:
    268         case kTexture2DRectSampler_GrSLType:
    269         case kBufferSampler_GrSLType:
    270             return false;
    271     }
    272     SkFAIL("Unexpected type");
    273     return false;
    274 }
    275 
    276 static inline bool GrSLTypeAcceptsPrecision(GrSLType type) {
    277     switch (type) {
    278         case kInt_GrSLType:
    279         case kUint_GrSLType:
    280         case kFloat_GrSLType:
    281         case kVec2f_GrSLType:
    282         case kVec3f_GrSLType:
    283         case kVec4f_GrSLType:
    284         case kVec2i_GrSLType:
    285         case kVec3i_GrSLType:
    286         case kVec4i_GrSLType:
    287         case kMat22f_GrSLType:
    288         case kMat33f_GrSLType:
    289         case kMat44f_GrSLType:
    290         case kTexture2DSampler_GrSLType:
    291         case kITexture2DSampler_GrSLType:
    292         case kTextureExternalSampler_GrSLType:
    293         case kTexture2DRectSampler_GrSLType:
    294         case kBufferSampler_GrSLType:
    295         case kTexture2D_GrSLType:
    296         case kSampler_GrSLType:
    297         case kImageStorage2D_GrSLType:
    298         case kIImageStorage2D_GrSLType:
    299             return true;
    300 
    301         case kVoid_GrSLType:
    302         case kBool_GrSLType:
    303             return false;
    304     }
    305     SkFAIL("Unexpected type");
    306     return false;
    307 }
    308 
    309 //////////////////////////////////////////////////////////////////////////////
    310 
    311 /**
    312  * Types used to describe format of vertices in arrays.
    313  */
    314 enum GrVertexAttribType {
    315     kFloat_GrVertexAttribType = 0,
    316     kVec2f_GrVertexAttribType,
    317     kVec3f_GrVertexAttribType,
    318     kVec4f_GrVertexAttribType,
    319 
    320     kVec2i_GrVertexAttribType,   // vector of 2 32-bit ints
    321     kVec3i_GrVertexAttribType,   // vector of 3 32-bit ints
    322     kVec4i_GrVertexAttribType,   // vector of 4 32-bit ints
    323 
    324     kUByte_GrVertexAttribType,   // unsigned byte, e.g. coverage
    325     kVec4ub_GrVertexAttribType,  // vector of 4 unsigned bytes, e.g. colors
    326 
    327     kVec2us_GrVertexAttribType,  // vector of 2 shorts, e.g. texture coordinates
    328 
    329     kInt_GrVertexAttribType,
    330     kUint_GrVertexAttribType,
    331 
    332     kLast_GrVertexAttribType = kUint_GrVertexAttribType
    333 };
    334 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
    335 
    336 /**
    337  * Returns the size of the attrib type in bytes.
    338  */
    339 static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
    340     switch (type) {
    341         case kFloat_GrVertexAttribType:
    342             return sizeof(float);
    343         case kVec2f_GrVertexAttribType:
    344             return 2 * sizeof(float);
    345         case kVec3f_GrVertexAttribType:
    346             return 3 * sizeof(float);
    347         case kVec4f_GrVertexAttribType:
    348             return 4 * sizeof(float);
    349         case kVec2i_GrVertexAttribType:
    350             return 2 * sizeof(int32_t);
    351         case kVec3i_GrVertexAttribType:
    352             return 3 * sizeof(int32_t);
    353         case kVec4i_GrVertexAttribType:
    354             return 4 * sizeof(int32_t);
    355         case kUByte_GrVertexAttribType:
    356             return 1 * sizeof(char);
    357         case kVec4ub_GrVertexAttribType:
    358             return 4 * sizeof(char);
    359         case kVec2us_GrVertexAttribType:
    360             return 2 * sizeof(int16_t);
    361         case kInt_GrVertexAttribType:
    362             return sizeof(int32_t);
    363         case kUint_GrVertexAttribType:
    364             return sizeof(uint32_t);
    365     }
    366     SkFAIL("Unexpected attribute type");
    367     return 0;
    368 }
    369 
    370 /**
    371  * Is the attrib type integral?
    372  */
    373 static inline bool GrVertexAttribTypeIsIntType(GrVertexAttribType type) {
    374     switch (type) {
    375         case kFloat_GrVertexAttribType:
    376             return false;
    377         case kVec2f_GrVertexAttribType:
    378             return false;
    379         case kVec3f_GrVertexAttribType:
    380             return false;
    381         case kVec4f_GrVertexAttribType:
    382             return false;
    383         case kVec2i_GrVertexAttribType:
    384             return true;
    385         case kVec3i_GrVertexAttribType:
    386             return true;
    387         case kVec4i_GrVertexAttribType:
    388             return true;
    389         case kUByte_GrVertexAttribType:
    390             return false;
    391         case kVec4ub_GrVertexAttribType:
    392             return false;
    393         case kVec2us_GrVertexAttribType:
    394             return false;
    395         case kInt_GrVertexAttribType:
    396             return true;
    397         case kUint_GrVertexAttribType:
    398             return true;
    399     }
    400     SkFAIL("Unexpected attribute type");
    401     return false;
    402 }
    403 
    404 /**
    405  * converts a GrVertexAttribType to a GrSLType
    406  */
    407 static inline GrSLType GrVertexAttribTypeToSLType(GrVertexAttribType type) {
    408     switch (type) {
    409         case kUByte_GrVertexAttribType:
    410         case kFloat_GrVertexAttribType:
    411             return kFloat_GrSLType;
    412         case kVec2us_GrVertexAttribType:
    413         case kVec2f_GrVertexAttribType:
    414             return kVec2f_GrSLType;
    415         case kVec3f_GrVertexAttribType:
    416             return kVec3f_GrSLType;
    417         case kVec4ub_GrVertexAttribType:
    418         case kVec4f_GrVertexAttribType:
    419             return kVec4f_GrSLType;
    420         case kVec2i_GrVertexAttribType:
    421             return kVec2i_GrSLType;
    422         case kVec3i_GrVertexAttribType:
    423             return kVec3i_GrSLType;
    424         case kVec4i_GrVertexAttribType:
    425             return kVec4i_GrSLType;
    426         case kInt_GrVertexAttribType:
    427             return kInt_GrSLType;
    428         case kUint_GrVertexAttribType:
    429             return kUint_GrSLType;
    430     }
    431     SkFAIL("Unsupported type conversion");
    432     return kVoid_GrSLType;
    433 }
    434 
    435 //////////////////////////////////////////////////////////////////////////////
    436 
    437 enum class GrImageStorageFormat {
    438     kRGBA8,
    439     kRGBA8i,
    440     kRGBA16f,
    441     kRGBA32f,
    442 };
    443 
    444 /**
    445  * Describes types of caching and compiler optimizations allowed for certain variable types
    446  * (currently only image storages).
    447  **/
    448 enum class GrSLMemoryModel {
    449     /** No special restrctions on memory accesses or compiler optimizations */
    450     kNone,
    451     /** Cache coherent across shader invocations */
    452     kCoherent,
    453     /**
    454      * Disallows compiler from eliding loads or stores that appear redundant in a single
    455      * invocation. Implies coherent.
    456      */
    457     kVolatile
    458 };
    459 
    460 /**
    461  * If kYes then the memory backing the varialble is only accessed via the variable. This is
    462  * currently only used with image storages.
    463  */
    464 enum class GrSLRestrict {
    465     kYes,
    466     kNo,
    467 };
    468 
    469 //////////////////////////////////////////////////////////////////////////////
    470 
    471 /**
    472  * We have coverage effects that clip rendering to the edge of some geometric primitive.
    473  * This enum specifies how that clipping is performed. Not all factories that take a
    474  * GrProcessorEdgeType will succeed with all values and it is up to the caller to check for
    475  * a NULL return.
    476  */
    477 enum GrPrimitiveEdgeType {
    478     kFillBW_GrProcessorEdgeType,
    479     kFillAA_GrProcessorEdgeType,
    480     kInverseFillBW_GrProcessorEdgeType,
    481     kInverseFillAA_GrProcessorEdgeType,
    482     kHairlineAA_GrProcessorEdgeType,
    483 
    484     kLast_GrProcessorEdgeType = kHairlineAA_GrProcessorEdgeType
    485 };
    486 
    487 static const int kGrProcessorEdgeTypeCnt = kLast_GrProcessorEdgeType + 1;
    488 
    489 static inline bool GrProcessorEdgeTypeIsFill(const GrPrimitiveEdgeType edgeType) {
    490     return (kFillAA_GrProcessorEdgeType == edgeType || kFillBW_GrProcessorEdgeType == edgeType);
    491 }
    492 
    493 static inline bool GrProcessorEdgeTypeIsInverseFill(const GrPrimitiveEdgeType edgeType) {
    494     return (kInverseFillAA_GrProcessorEdgeType == edgeType ||
    495             kInverseFillBW_GrProcessorEdgeType == edgeType);
    496 }
    497 
    498 static inline bool GrProcessorEdgeTypeIsAA(const GrPrimitiveEdgeType edgeType) {
    499     return (kFillBW_GrProcessorEdgeType != edgeType &&
    500             kInverseFillBW_GrProcessorEdgeType != edgeType);
    501 }
    502 
    503 static inline GrPrimitiveEdgeType GrInvertProcessorEdgeType(const GrPrimitiveEdgeType edgeType) {
    504     switch (edgeType) {
    505         case kFillBW_GrProcessorEdgeType:
    506             return kInverseFillBW_GrProcessorEdgeType;
    507         case kFillAA_GrProcessorEdgeType:
    508             return kInverseFillAA_GrProcessorEdgeType;
    509         case kInverseFillBW_GrProcessorEdgeType:
    510             return kFillBW_GrProcessorEdgeType;
    511         case kInverseFillAA_GrProcessorEdgeType:
    512             return kFillAA_GrProcessorEdgeType;
    513         case kHairlineAA_GrProcessorEdgeType:
    514             SkFAIL("Hairline fill isn't invertible.");
    515     }
    516     return kFillAA_GrProcessorEdgeType;  // suppress warning.
    517 }
    518 
    519 /**
    520  * Indicates the type of pending IO operations that can be recorded for gpu resources.
    521  */
    522 enum GrIOType {
    523     kRead_GrIOType,
    524     kWrite_GrIOType,
    525     kRW_GrIOType
    526 };
    527 
    528 /**
    529  * Indicates the type of data that a GPU buffer will be used for.
    530  */
    531 enum GrBufferType {
    532     kVertex_GrBufferType,
    533     kIndex_GrBufferType,
    534     kTexel_GrBufferType,
    535     kDrawIndirect_GrBufferType,
    536     kXferCpuToGpu_GrBufferType,
    537     kXferGpuToCpu_GrBufferType,
    538 
    539     kLast_GrBufferType = kXferGpuToCpu_GrBufferType
    540 };
    541 static const int kGrBufferTypeCount = kLast_GrBufferType + 1;
    542 
    543 static inline bool GrBufferTypeIsVertexOrIndex(GrBufferType type) {
    544     SkASSERT(type >= 0 && type < kGrBufferTypeCount);
    545     return type <= kIndex_GrBufferType;
    546 
    547     GR_STATIC_ASSERT(0 == kVertex_GrBufferType);
    548     GR_STATIC_ASSERT(1 == kIndex_GrBufferType);
    549 }
    550 
    551 /**
    552  * Provides a performance hint regarding the frequency at which a data store will be accessed.
    553  */
    554 enum GrAccessPattern {
    555     /** Data store will be respecified repeatedly and used many times. */
    556     kDynamic_GrAccessPattern,
    557     /** Data store will be specified once and used many times. (Thus disqualified from caching.) */
    558     kStatic_GrAccessPattern,
    559     /** Data store will be specified once and used at most a few times. (Also can't be cached.) */
    560     kStream_GrAccessPattern,
    561 
    562     kLast_GrAccessPattern = kStream_GrAccessPattern
    563 };
    564 
    565 // Flags shared between GrRenderTarget and GrRenderTargetProxy
    566 enum class GrRenderTargetFlags {
    567     kNone               = 0,
    568 
    569     // For internal resources:
    570     //    this is enabled whenever MSAA is enabled and GrCaps reports mixed samples are supported
    571     // For wrapped resources:
    572     //    this is disabled for FBO0
    573     //    but, otherwise, is enabled whenever MSAA is enabled and GrCaps reports mixed samples
    574     //        are supported
    575     kMixedSampled       = 1 << 0,
    576 
    577     // For internal resources:
    578     //    this is enabled whenever GrCaps reports window rect support
    579     // For wrapped resources1
    580     //    this is disabled for FBO0
    581     //    but, otherwise, is enabled whenever GrCaps reports window rect support
    582     kWindowRectsSupport = 1 << 1
    583 };
    584 GR_MAKE_BITFIELD_CLASS_OPS(GrRenderTargetFlags)
    585 
    586 #ifdef SK_DEBUG
    587 // Takes a pointer to a GrCaps, and will suppress prints if required
    588 #define GrCapsDebugf(caps, ...)      \
    589     if (!(caps)->suppressPrints()) { \
    590         SkDebugf(__VA_ARGS__);       \
    591     }
    592 #else
    593 #define GrCapsDebugf(caps, ...)
    594 #endif
    595 
    596 /**
    597  * Specifies if the holder owns the backend, OpenGL or Vulkan, object.
    598  */
    599 enum class GrBackendObjectOwnership : bool {
    600     /** Holder does not destroy the backend object. */
    601     kBorrowed = false,
    602     /** Holder destroys the backend object. */
    603     kOwned = true
    604 };
    605 
    606 template <typename T> T * const * sk_sp_address_as_pointer_address(sk_sp<T> const * sp) {
    607     static_assert(sizeof(T*) == sizeof(sk_sp<T>), "sk_sp not expected size.");
    608     return reinterpret_cast<T * const *>(sp);
    609 }
    610 
    611 /*
    612  * Object for CPU-GPU synchronization
    613  */
    614 typedef uint64_t GrFence;
    615 
    616 #endif
    617