• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkColor.h"
15 #include "include/core/SkColorPriv.h"
16 #include "include/gpu/GrTypes.h"
17 #include "include/private/SkColorData.h"
18 #include "include/private/SkHalf.h"
19 
20 /**
21  * GrColor is 4 bytes for R, G, B, A, in a specific order defined below. Whether the color is
22  * premultiplied or not depends on the context in which it is being used.
23  */
24 typedef uint32_t GrColor;
25 
26 // shift amount to assign a component to a GrColor int
27 // These shift values are chosen for compatibility with GL attrib arrays
28 // ES doesn't allow BGRA vertex attrib order so if they were not in this order
29 // we'd have to swizzle in shaders.
30 #ifdef SK_CPU_BENDIAN
31     #define GrColor_SHIFT_R     24
32     #define GrColor_SHIFT_G     16
33     #define GrColor_SHIFT_B     8
34     #define GrColor_SHIFT_A     0
35 #else
36     #define GrColor_SHIFT_R     0
37     #define GrColor_SHIFT_G     8
38     #define GrColor_SHIFT_B     16
39     #define GrColor_SHIFT_A     24
40 #endif
41 
42 /**
43  *  Pack 4 components (RGBA) into a GrColor int
44  */
GrColorPackRGBA(unsigned r,unsigned g,unsigned b,unsigned a)45 static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a) {
46     SkASSERT((uint8_t)r == r);
47     SkASSERT((uint8_t)g == g);
48     SkASSERT((uint8_t)b == b);
49     SkASSERT((uint8_t)a == a);
50     return  (r << GrColor_SHIFT_R) |
51             (g << GrColor_SHIFT_G) |
52             (b << GrColor_SHIFT_B) |
53             (a << GrColor_SHIFT_A);
54 }
55 
56 // extract a component (byte) from a GrColor int
57 
58 #define GrColorUnpackR(color)   (((color) >> GrColor_SHIFT_R) & 0xFF)
59 #define GrColorUnpackG(color)   (((color) >> GrColor_SHIFT_G) & 0xFF)
60 #define GrColorUnpackB(color)   (((color) >> GrColor_SHIFT_B) & 0xFF)
61 #define GrColorUnpackA(color)   (((color) >> GrColor_SHIFT_A) & 0xFF)
62 
63 /**
64  *  Since premultiplied means that alpha >= color, we construct a color with
65  *  each component==255 and alpha == 0 to be "illegal"
66  */
67 #define GrColor_ILLEGAL     (~(0xFF << GrColor_SHIFT_A))
68 
69 /** Normalizes and coverts an uint8_t to a float. [0, 255] -> [0.0, 1.0] */
GrNormalizeByteToFloat(uint8_t value)70 static inline float GrNormalizeByteToFloat(uint8_t value) {
71     static const float ONE_OVER_255 = 1.f / 255.f;
72     return value * ONE_OVER_255;
73 }
74 
75 /** Used to pick vertex attribute types. */
SkPMColor4fFitsInBytes(const SkPMColor4f & color)76 static inline bool SkPMColor4fFitsInBytes(const SkPMColor4f& color) {
77     // Might want to instead check that the components are [0...a] instead of [0...1]?
78     return color.fitsInBytes();
79 }
80 
SkPMColor4f_toFP16(const SkPMColor4f & color)81 static inline uint64_t SkPMColor4f_toFP16(const SkPMColor4f& color) {
82     uint64_t halfColor;
83     SkFloatToHalf_finite_ftz(Sk4f::Load(color.vec())).store(&halfColor);
84     return halfColor;
85 }
86 
87 /**
88  * GrVertexColor is a helper for writing colors to a vertex attribute. It stores either GrColor
89  * or four half-float channels, depending on the wideColor parameter. GrVertexWriter will write
90  * the correct amount of data. Note that the GP needs to have been constructed with the correct
91  * attribute type for colors, to match the usage here.
92  */
93 class GrVertexColor {
94 public:
95     GrVertexColor() = default;
96 
GrVertexColor(const SkPMColor4f & color,bool wideColor)97     explicit GrVertexColor(const SkPMColor4f& color, bool wideColor) {
98         this->set(color, wideColor);
99     }
100 
set(const SkPMColor4f & color,bool wideColor)101     void set(const SkPMColor4f& color, bool wideColor) {
102         if (wideColor) {
103             memcpy(fColor, color.vec(), sizeof(fColor));
104         } else {
105             fColor[0] = color.toBytes_RGBA();
106         }
107         fWideColor = wideColor;
108     }
109 
size()110     size_t size() const { return fWideColor ? 16 : 4; }
111 
112 private:
113     friend struct GrVertexWriter;
114 
115     uint32_t fColor[4];
116     bool     fWideColor;
117 };
118 
119 #endif
120