Home | History | Annotate | Download | only in gpu
      1 
      2 /*
      3  * Copyright 2010 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 
     11 #ifndef GrColor_DEFINED
     12 #define GrColor_DEFINED
     13 
     14 #include "GrTypes.h"
     15 #include "SkColor.h"
     16 #include "SkColorPriv.h"
     17 #include "SkUnPreMultiply.h"
     18 
     19 /**
     20  * GrColor is 4 bytes for R, G, B, A, in a specific order defined below. Whether the color is
     21  * premultiplied or not depends on the context in which it is being used.
     22  */
     23 typedef uint32_t GrColor;
     24 
     25 // shift amount to assign a component to a GrColor int
     26 // These shift values are chosen for compatibility with GL attrib arrays
     27 // ES doesn't allow BGRA vertex attrib order so if they were not in this order
     28 // we'd have to swizzle in shaders.
     29 #ifdef SK_CPU_BENDIAN
     30     #define GrColor_SHIFT_R     24
     31     #define GrColor_SHIFT_G     16
     32     #define GrColor_SHIFT_B     8
     33     #define GrColor_SHIFT_A     0
     34 #else
     35     #define GrColor_SHIFT_R     0
     36     #define GrColor_SHIFT_G     8
     37     #define GrColor_SHIFT_B     16
     38     #define GrColor_SHIFT_A     24
     39 #endif
     40 
     41 /**
     42  *  Pack 4 components (RGBA) into a GrColor int
     43  */
     44 static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a) {
     45     SkASSERT((uint8_t)r == r);
     46     SkASSERT((uint8_t)g == g);
     47     SkASSERT((uint8_t)b == b);
     48     SkASSERT((uint8_t)a == a);
     49     return  (r << GrColor_SHIFT_R) |
     50             (g << GrColor_SHIFT_G) |
     51             (b << GrColor_SHIFT_B) |
     52             (a << GrColor_SHIFT_A);
     53 }
     54 
     55 /**
     56  *  Packs a color with an alpha channel replicated across all four channels.
     57  */
     58 static inline GrColor GrColorPackA4(unsigned a) {
     59     SkASSERT((uint8_t)a == a);
     60     return  (a << GrColor_SHIFT_R) |
     61             (a << GrColor_SHIFT_G) |
     62             (a << GrColor_SHIFT_B) |
     63             (a << GrColor_SHIFT_A);
     64 }
     65 
     66 // extract a component (byte) from a GrColor int
     67 
     68 #define GrColorUnpackR(color)   (((color) >> GrColor_SHIFT_R) & 0xFF)
     69 #define GrColorUnpackG(color)   (((color) >> GrColor_SHIFT_G) & 0xFF)
     70 #define GrColorUnpackB(color)   (((color) >> GrColor_SHIFT_B) & 0xFF)
     71 #define GrColorUnpackA(color)   (((color) >> GrColor_SHIFT_A) & 0xFF)
     72 
     73 /**
     74  *  Since premultiplied means that alpha >= color, we construct a color with
     75  *  each component==255 and alpha == 0 to be "illegal"
     76  */
     77 #define GrColor_ILLEGAL     (~(0xFF << GrColor_SHIFT_A))
     78 
     79 #define GrColor_WHITE 0xFFFFFFFF
     80 #define GrColor_TRANSPARENT_BLACK 0x0
     81 
     82 /**
     83  * Assert in debug builds that a GrColor is premultiplied.
     84  */
     85 static inline void GrColorIsPMAssert(GrColor SkDEBUGCODE(c)) {
     86 #ifdef SK_DEBUG
     87     unsigned a = GrColorUnpackA(c);
     88     unsigned r = GrColorUnpackR(c);
     89     unsigned g = GrColorUnpackG(c);
     90     unsigned b = GrColorUnpackB(c);
     91 
     92     SkASSERT(r <= a);
     93     SkASSERT(g <= a);
     94     SkASSERT(b <= a);
     95 #endif
     96 }
     97 
     98 /** Inverts each color channel. */
     99 static inline GrColor GrInvertColor(GrColor c) {
    100     U8CPU a = GrColorUnpackA(c);
    101     U8CPU r = GrColorUnpackR(c);
    102     U8CPU g = GrColorUnpackG(c);
    103     U8CPU b = GrColorUnpackB(c);
    104     return GrColorPackRGBA(0xff - r, 0xff - g, 0xff - b, 0xff - a);
    105 }
    106 
    107 static inline GrColor GrColorMul(GrColor c0, GrColor c1) {
    108     U8CPU r = SkMulDiv255Round(GrColorUnpackR(c0), GrColorUnpackR(c1));
    109     U8CPU g = SkMulDiv255Round(GrColorUnpackG(c0), GrColorUnpackG(c1));
    110     U8CPU b = SkMulDiv255Round(GrColorUnpackB(c0), GrColorUnpackB(c1));
    111     U8CPU a = SkMulDiv255Round(GrColorUnpackA(c0), GrColorUnpackA(c1));
    112     return GrColorPackRGBA(r, g, b, a);
    113 }
    114 
    115 static inline GrColor GrColorSatAdd(GrColor c0, GrColor c1) {
    116     unsigned r = SkTMin<unsigned>(GrColorUnpackR(c0) + GrColorUnpackR(c1), 0xff);
    117     unsigned g = SkTMin<unsigned>(GrColorUnpackG(c0) + GrColorUnpackG(c1), 0xff);
    118     unsigned b = SkTMin<unsigned>(GrColorUnpackB(c0) + GrColorUnpackB(c1), 0xff);
    119     unsigned a = SkTMin<unsigned>(GrColorUnpackA(c0) + GrColorUnpackA(c1), 0xff);
    120     return GrColorPackRGBA(r, g, b, a);
    121 }
    122 
    123 /** Converts a GrColor to an rgba array of GrGLfloat */
    124 static inline void GrColorToRGBAFloat(GrColor color, float rgba[4]) {
    125     static const float ONE_OVER_255 = 1.f / 255.f;
    126     rgba[0] = GrColorUnpackR(color) * ONE_OVER_255;
    127     rgba[1] = GrColorUnpackG(color) * ONE_OVER_255;
    128     rgba[2] = GrColorUnpackB(color) * ONE_OVER_255;
    129     rgba[3] = GrColorUnpackA(color) * ONE_OVER_255;
    130 }
    131 
    132 /** Normalizes and coverts an uint8_t to a float. [0, 255] -> [0.0, 1.0] */
    133 static inline float GrNormalizeByteToFloat(uint8_t value) {
    134     static const float ONE_OVER_255 = 1.f / 255.f;
    135     return value * ONE_OVER_255;
    136 }
    137 
    138 /** Determines whether the color is opaque or not. */
    139 static inline bool GrColorIsOpaque(GrColor color) {
    140     return (color & (0xFFU << GrColor_SHIFT_A)) == (0xFFU << GrColor_SHIFT_A);
    141 }
    142 
    143 static inline GrColor GrPremulColor(GrColor color) {
    144     unsigned r = GrColorUnpackR(color);
    145     unsigned g = GrColorUnpackG(color);
    146     unsigned b = GrColorUnpackB(color);
    147     unsigned a = GrColorUnpackA(color);
    148     return GrColorPackRGBA(SkMulDiv255Round(r, a),
    149                            SkMulDiv255Round(g, a),
    150                            SkMulDiv255Round(b, a),
    151                            a);
    152 }
    153 
    154 /** Returns an unpremuled version of the GrColor. */
    155 static inline GrColor GrUnpremulColor(GrColor color) {
    156     GrColorIsPMAssert(color);
    157     unsigned r = GrColorUnpackR(color);
    158     unsigned g = GrColorUnpackG(color);
    159     unsigned b = GrColorUnpackB(color);
    160     unsigned a = GrColorUnpackA(color);
    161     SkPMColor colorPM = SkPackARGB32(a, r, g, b);
    162     SkColor colorUPM = SkUnPreMultiply::PMColorToColor(colorPM);
    163 
    164     r = SkColorGetR(colorUPM);
    165     g = SkColorGetG(colorUPM);
    166     b = SkColorGetB(colorUPM);
    167     a = SkColorGetA(colorUPM);
    168 
    169     return GrColorPackRGBA(r, g, b, a);
    170 }
    171 
    172 /**
    173  * Flags used for bitfields of color components. They are defined so that the bit order reflects the
    174  * GrColor shift order.
    175  */
    176 enum GrColorComponentFlags {
    177     kR_GrColorComponentFlag = 1 << (GrColor_SHIFT_R / 8),
    178     kG_GrColorComponentFlag = 1 << (GrColor_SHIFT_G / 8),
    179     kB_GrColorComponentFlag = 1 << (GrColor_SHIFT_B / 8),
    180     kA_GrColorComponentFlag = 1 << (GrColor_SHIFT_A / 8),
    181 
    182     kNone_GrColorComponentFlags = 0,
    183 
    184     kRGB_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
    185                                   kB_GrColorComponentFlag),
    186 
    187     kRGBA_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
    188                                    kB_GrColorComponentFlag | kA_GrColorComponentFlag)
    189 };
    190 
    191 GR_MAKE_BITFIELD_OPS(GrColorComponentFlags)
    192 
    193 static inline char GrColorComponentFlagToChar(GrColorComponentFlags component) {
    194     SkASSERT(SkIsPow2(component));
    195     switch (component) {
    196         case kR_GrColorComponentFlag:
    197             return 'r';
    198         case kG_GrColorComponentFlag:
    199             return 'g';
    200         case kB_GrColorComponentFlag:
    201             return 'b';
    202         case kA_GrColorComponentFlag:
    203             return 'a';
    204         default:
    205             SkFAIL("Invalid color component flag.");
    206             return '\0';
    207     }
    208 }
    209 
    210 static inline uint32_t GrPixelConfigComponentMask(GrPixelConfig config) {
    211     SkASSERT(config >= 0 && config < kGrPixelConfigCnt);
    212     static const uint32_t kFlags[] = {
    213         0,                              // kUnknown_GrPixelConfig
    214         kA_GrColorComponentFlag,        // kAlpha_8_GrPixelConfig
    215         kRGBA_GrColorComponentFlags,    // kIndex_8_GrPixelConfig
    216         kRGB_GrColorComponentFlags,     // kRGB_565_GrPixelConfig
    217         kRGBA_GrColorComponentFlags,    // kRGBA_4444_GrPixelConfig
    218         kRGBA_GrColorComponentFlags,    // kRGBA_8888_GrPixelConfig
    219         kRGBA_GrColorComponentFlags,    // kBGRA_8888_GrPixelConfig
    220         kRGBA_GrColorComponentFlags,    // kSRGBA_8888_GrPixelConfig
    221         kRGB_GrColorComponentFlags,     // kETC1_GrPixelConfig
    222         kA_GrColorComponentFlag,        // kLATC_GrPixelConfig
    223         kA_GrColorComponentFlag,        // kR11_EAC_GrPixelConfig
    224         kRGBA_GrColorComponentFlags,    // kASTC_12x12_GrPixelConfig
    225         kRGBA_GrColorComponentFlags,    // kRGBA_float_GrPixelConfig
    226         kA_GrColorComponentFlag,        // kAlpha_16_GrPixelConfig
    227         kRGBA_GrColorComponentFlags,    // kRGBA_half_GrPixelConfig
    228     };
    229     return kFlags[config];
    230 
    231     GR_STATIC_ASSERT(0  == kUnknown_GrPixelConfig);
    232     GR_STATIC_ASSERT(1  == kAlpha_8_GrPixelConfig);
    233     GR_STATIC_ASSERT(2  == kIndex_8_GrPixelConfig);
    234     GR_STATIC_ASSERT(3  == kRGB_565_GrPixelConfig);
    235     GR_STATIC_ASSERT(4  == kRGBA_4444_GrPixelConfig);
    236     GR_STATIC_ASSERT(5  == kRGBA_8888_GrPixelConfig);
    237     GR_STATIC_ASSERT(6  == kBGRA_8888_GrPixelConfig);
    238     GR_STATIC_ASSERT(7  == kSRGBA_8888_GrPixelConfig);
    239     GR_STATIC_ASSERT(8  == kETC1_GrPixelConfig);
    240     GR_STATIC_ASSERT(9  == kLATC_GrPixelConfig);
    241     GR_STATIC_ASSERT(10  == kR11_EAC_GrPixelConfig);
    242     GR_STATIC_ASSERT(11 == kASTC_12x12_GrPixelConfig);
    243     GR_STATIC_ASSERT(12 == kRGBA_float_GrPixelConfig);
    244     GR_STATIC_ASSERT(13 == kAlpha_half_GrPixelConfig);
    245     GR_STATIC_ASSERT(14 == kRGBA_half_GrPixelConfig);
    246     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFlags) == kGrPixelConfigCnt);
    247 }
    248 
    249 #endif
    250