LCOV - code coverage report
Current view: top level - gfx/skia/skia/include/gpu - GrColor.h (source / functions) Hit Total Coverage
Test: output.info Lines: 0 124 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 30 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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           0 : static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a) {
      45           0 :     SkASSERT((uint8_t)r == r);
      46           0 :     SkASSERT((uint8_t)g == g);
      47           0 :     SkASSERT((uint8_t)b == b);
      48           0 :     SkASSERT((uint8_t)a == a);
      49           0 :     return  (r << GrColor_SHIFT_R) |
      50           0 :             (g << GrColor_SHIFT_G) |
      51           0 :             (b << GrColor_SHIFT_B) |
      52           0 :             (a << GrColor_SHIFT_A);
      53             : }
      54             : 
      55             : /**
      56             :  *  Packs a color with an alpha channel replicated across all four channels.
      57             :  */
      58           0 : static inline GrColor GrColorPackA4(unsigned a) {
      59           0 :     SkASSERT((uint8_t)a == a);
      60           0 :     return  (a << GrColor_SHIFT_R) |
      61           0 :             (a << GrColor_SHIFT_G) |
      62           0 :             (a << GrColor_SHIFT_B) |
      63           0 :             (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           0 : static inline void GrColorIsPMAssert(GrColor SkDEBUGCODE(c)) {
      86             : #ifdef SK_DEBUG
      87           0 :     unsigned a = GrColorUnpackA(c);
      88           0 :     unsigned r = GrColorUnpackR(c);
      89           0 :     unsigned g = GrColorUnpackG(c);
      90           0 :     unsigned b = GrColorUnpackB(c);
      91             : 
      92           0 :     SkASSERT(r <= a);
      93           0 :     SkASSERT(g <= a);
      94           0 :     SkASSERT(b <= a);
      95             : #endif
      96           0 : }
      97             : 
      98             : /** Inverts each color channel. */
      99           0 : static inline GrColor GrInvertColor(GrColor c) {
     100           0 :     U8CPU a = GrColorUnpackA(c);
     101           0 :     U8CPU r = GrColorUnpackR(c);
     102           0 :     U8CPU g = GrColorUnpackG(c);
     103           0 :     U8CPU b = GrColorUnpackB(c);
     104           0 :     return GrColorPackRGBA(0xff - r, 0xff - g, 0xff - b, 0xff - a);
     105             : }
     106             : 
     107           0 : static inline GrColor GrColorMul(GrColor c0, GrColor c1) {
     108           0 :     U8CPU r = SkMulDiv255Round(GrColorUnpackR(c0), GrColorUnpackR(c1));
     109           0 :     U8CPU g = SkMulDiv255Round(GrColorUnpackG(c0), GrColorUnpackG(c1));
     110           0 :     U8CPU b = SkMulDiv255Round(GrColorUnpackB(c0), GrColorUnpackB(c1));
     111           0 :     U8CPU a = SkMulDiv255Round(GrColorUnpackA(c0), GrColorUnpackA(c1));
     112           0 :     return GrColorPackRGBA(r, g, b, a);
     113             : }
     114             : 
     115           0 : static inline GrColor GrColorSatAdd(GrColor c0, GrColor c1) {
     116           0 :     unsigned r = SkTMin<unsigned>(GrColorUnpackR(c0) + GrColorUnpackR(c1), 0xff);
     117           0 :     unsigned g = SkTMin<unsigned>(GrColorUnpackG(c0) + GrColorUnpackG(c1), 0xff);
     118           0 :     unsigned b = SkTMin<unsigned>(GrColorUnpackB(c0) + GrColorUnpackB(c1), 0xff);
     119           0 :     unsigned a = SkTMin<unsigned>(GrColorUnpackA(c0) + GrColorUnpackA(c1), 0xff);
     120           0 :     return GrColorPackRGBA(r, g, b, a);
     121             : }
     122             : 
     123             : /** Converts a GrColor to an rgba array of GrGLfloat */
     124           0 : static inline void GrColorToRGBAFloat(GrColor color, float rgba[4]) {
     125             :     static const float ONE_OVER_255 = 1.f / 255.f;
     126           0 :     rgba[0] = GrColorUnpackR(color) * ONE_OVER_255;
     127           0 :     rgba[1] = GrColorUnpackG(color) * ONE_OVER_255;
     128           0 :     rgba[2] = GrColorUnpackB(color) * ONE_OVER_255;
     129           0 :     rgba[3] = GrColorUnpackA(color) * ONE_OVER_255;
     130           0 : }
     131             : 
     132             : /** Normalizes and coverts an uint8_t to a float. [0, 255] -> [0.0, 1.0] */
     133           0 : static inline float GrNormalizeByteToFloat(uint8_t value) {
     134             :     static const float ONE_OVER_255 = 1.f / 255.f;
     135           0 :     return value * ONE_OVER_255;
     136             : }
     137             : 
     138             : /** Determines whether the color is opaque or not. */
     139           0 : static inline bool GrColorIsOpaque(GrColor color) {
     140           0 :     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           0 : static inline GrColor GrUnpremulColor(GrColor color) {
     156           0 :     GrColorIsPMAssert(color);
     157           0 :     unsigned r = GrColorUnpackR(color);
     158           0 :     unsigned g = GrColorUnpackG(color); 
     159           0 :     unsigned b = GrColorUnpackB(color);
     160           0 :     unsigned a = GrColorUnpackA(color);
     161           0 :     SkPMColor colorPM = SkPackARGB32(a, r, g, b);
     162           0 :     SkColor colorUPM = SkUnPreMultiply::PMColorToColor(colorPM);
     163             : 
     164           0 :     r = SkColorGetR(colorUPM);
     165           0 :     g = SkColorGetG(colorUPM);
     166           0 :     b = SkColorGetB(colorUPM);
     167           0 :     a = SkColorGetA(colorUPM);
     168             : 
     169           0 :     return GrColorPackRGBA(r, g, b, a);
     170             : }
     171             : 
     172             : 
     173             : /**
     174             : * Similarly, GrColor4f is 4 floats for R, G, B, A, in that order. And like GrColor, whether
     175             : * the color is premultiplied or not depends on the context.
     176             : */
     177             : struct GrColor4f {
     178             :     float fRGBA[4];
     179             : 
     180           0 :     GrColor4f() {}
     181           0 :     GrColor4f(float r, float g, float b, float a) {
     182           0 :         fRGBA[0] = r;
     183           0 :         fRGBA[1] = g;
     184           0 :         fRGBA[2] = b;
     185           0 :         fRGBA[3] = a;
     186           0 :     }
     187             : 
     188             :     enum Illegal_Constructor {
     189             :         kIllegalConstructor
     190             :     };
     191           0 :     GrColor4f(Illegal_Constructor) {
     192           0 :         fRGBA[0] = SK_FloatNaN;
     193           0 :         fRGBA[1] = SK_FloatNaN;
     194           0 :         fRGBA[2] = SK_FloatNaN;
     195           0 :         fRGBA[3] = SK_FloatNaN;
     196           0 :     }
     197             : 
     198           0 :     static GrColor4f OpaqueWhite() {
     199           0 :         return GrColor4f(1.0f, 1.0f, 1.0f, 1.0f);
     200             :     }
     201             : 
     202           0 :     static GrColor4f TransparentBlack() {
     203           0 :         return GrColor4f(0.0f, 0.0f, 0.0f, 0.0f);
     204             :     }
     205             : 
     206           0 :     static GrColor4f FromGrColor(GrColor color) {
     207           0 :         GrColor4f result;
     208           0 :         GrColorToRGBAFloat(color, result.fRGBA);
     209           0 :         return result;
     210             :     }
     211             : 
     212           0 :     static GrColor4f FromSkColor4f(const SkColor4f& color) {
     213           0 :         return GrColor4f(color.fR, color.fG, color.fB, color.fA);
     214             :     }
     215             : 
     216           0 :     GrColor4f modulate(const GrColor4f& x) const {
     217           0 :         return GrColor4f(fRGBA[0] * x.fRGBA[0],
     218           0 :                          fRGBA[1] * x.fRGBA[1],
     219           0 :                          fRGBA[2] * x.fRGBA[2],
     220           0 :                          fRGBA[3] * x.fRGBA[3]);
     221             :     }
     222             : 
     223           0 :     GrColor4f mulByScalar(float x) const {
     224           0 :         return GrColor4f(fRGBA[0] * x, fRGBA[1] * x, fRGBA[2] * x, fRGBA[3] * x);
     225             :     }
     226             : 
     227           0 :     bool operator==(const GrColor4f& other) const {
     228             :         return
     229           0 :             fRGBA[0] == other.fRGBA[0] &&
     230           0 :             fRGBA[1] == other.fRGBA[1] &&
     231           0 :             fRGBA[2] == other.fRGBA[2] &&
     232           0 :             fRGBA[3] == other.fRGBA[3];
     233             :     }
     234           0 :     bool operator!=(const GrColor4f& other) const {
     235           0 :         return !(*this == other);
     236             :     }
     237             : 
     238           0 :     GrColor toGrColor() const {
     239           0 :         return GrColorPackRGBA(
     240           0 :             SkTPin<unsigned>(static_cast<unsigned>(fRGBA[0] * 255.0f + 0.5f), 0, 255),
     241           0 :             SkTPin<unsigned>(static_cast<unsigned>(fRGBA[1] * 255.0f + 0.5f), 0, 255),
     242           0 :             SkTPin<unsigned>(static_cast<unsigned>(fRGBA[2] * 255.0f + 0.5f), 0, 255),
     243           0 :             SkTPin<unsigned>(static_cast<unsigned>(fRGBA[3] * 255.0f + 0.5f), 0, 255));
     244             :     }
     245             : 
     246           0 :     SkColor4f toSkColor4f() const {
     247           0 :         return SkColor4f { fRGBA[0], fRGBA[1], fRGBA[2], fRGBA[3] };
     248             :     }
     249             : 
     250           0 :     GrColor4f opaque() const {
     251           0 :         return GrColor4f(fRGBA[0], fRGBA[1], fRGBA[2], 1.0f);
     252             :     }
     253             : 
     254           0 :     bool isOpaque() const {
     255           0 :         return fRGBA[3] >= 1.f;  // just in case precision causes a superopaque value.
     256             :     }
     257             : 
     258           0 :     GrColor4f premul() const {
     259           0 :         float a = fRGBA[3];
     260           0 :         return GrColor4f(fRGBA[0] * a, fRGBA[1] * a, fRGBA[2] * a, a);
     261             :     }
     262             : 
     263           0 :     GrColor4f unpremul() const {
     264           0 :         float a = fRGBA[3];
     265           0 :         if (a <= 0.0f) {
     266           0 :             return GrColor4f(0.0f, 0.0f, 0.0f, 0.0f);
     267             :         }
     268           0 :         float invAlpha = 1.0f / a;
     269           0 :         return GrColor4f(fRGBA[0] * invAlpha, fRGBA[1] * invAlpha, fRGBA[2] * invAlpha, a);
     270             :     }
     271             : };
     272             : 
     273             : /**
     274             :  * Flags used for bitfields of color components. They are defined so that the bit order reflects the
     275             :  * GrColor shift order.
     276             :  */
     277             : enum GrColorComponentFlags {
     278             :     kR_GrColorComponentFlag = 1 << (GrColor_SHIFT_R / 8),
     279             :     kG_GrColorComponentFlag = 1 << (GrColor_SHIFT_G / 8),
     280             :     kB_GrColorComponentFlag = 1 << (GrColor_SHIFT_B / 8),
     281             :     kA_GrColorComponentFlag = 1 << (GrColor_SHIFT_A / 8),
     282             : 
     283             :     kNone_GrColorComponentFlags = 0,
     284             : 
     285             :     kRGB_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
     286             :                                   kB_GrColorComponentFlag),
     287             : 
     288             :     kRGBA_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
     289             :                                    kB_GrColorComponentFlag | kA_GrColorComponentFlag)
     290             : };
     291             : 
     292           0 : GR_MAKE_BITFIELD_OPS(GrColorComponentFlags)
     293             : 
     294             : #endif

Generated by: LCOV version 1.13