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 
     16 /**
     17  *  GrColor is 4 bytes for R, G, B, A, in a compile-time specific order. The
     18  *  components are stored premultiplied.
     19  */
     20 typedef uint32_t GrColor;
     21 
     22 
     23 // shift amount to assign a component to a GrColor int
     24 // These shift values are chosen for compatibility with GL attrib arrays
     25 // ES doesn't allow BGRA vertex attrib order so if they were not in this order
     26 // we'd have to swizzle in shaders. Note the assumption that the cpu is little
     27 // endian.
     28 #define GrColor_SHIFT_R     0
     29 #define GrColor_SHIFT_G     8
     30 #define GrColor_SHIFT_B     16
     31 #define GrColor_SHIFT_A     24
     32 
     33 /**
     34  *  Pack 4 components (RGBA) into a GrColor int
     35  */
     36 static inline GrColor GrColorPackRGBA(unsigned r, unsigned g,
     37                                       unsigned b, unsigned a) {
     38     GrAssert((uint8_t)r == r);
     39     GrAssert((uint8_t)g == g);
     40     GrAssert((uint8_t)b == b);
     41     GrAssert((uint8_t)a == a);
     42     return  (r << GrColor_SHIFT_R) |
     43             (g << GrColor_SHIFT_G) |
     44             (b << GrColor_SHIFT_B) |
     45             (a << GrColor_SHIFT_A);
     46 }
     47 
     48 // extract a component (byte) from a GrColor int
     49 
     50 #define GrColorUnpackR(color)   (((color) >> GrColor_SHIFT_R) & 0xFF)
     51 #define GrColorUnpackG(color)   (((color) >> GrColor_SHIFT_G) & 0xFF)
     52 #define GrColorUnpackB(color)   (((color) >> GrColor_SHIFT_B) & 0xFF)
     53 #define GrColorUnpackA(color)   (((color) >> GrColor_SHIFT_A) & 0xFF)
     54 
     55 /**
     56  *  Since premultiplied means that alpha >= color, we construct a color with
     57  *  each component==255 and alpha == 0 to be "illegal"
     58  */
     59 #define GrColor_ILLEGAL     (~(0xFF << GrColor_SHIFT_A))
     60 
     61 /** Converts a GrColor to an rgba array of GrGLfloat */
     62 static inline void GrColorToRGBAFloat(GrColor color, float rgba[4]) {
     63     static const float ONE_OVER_255 = 1.f / 255.f;
     64     rgba[0] = GrColorUnpackR(color) * ONE_OVER_255;
     65     rgba[1] = GrColorUnpackG(color) * ONE_OVER_255;
     66     rgba[2] = GrColorUnpackB(color) * ONE_OVER_255;
     67     rgba[3] = GrColorUnpackA(color) * ONE_OVER_255;
     68 }
     69 
     70 #endif
     71