xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrColor.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #ifndef GrColor_DEFINED
9 #define GrColor_DEFINED
10 
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorPriv.h"
13 #include "include/gpu/ganesh/GrTypes.h"
14 #include "include/private/SkColorData.h"
15 #include "src/base/SkHalf.h"
16 #include "src/gpu/BufferWriter.h"
17 
18 /**
19  * GrColor is 4 bytes for R, G, B, A, in a specific order defined below. Whether the color is
20  * premultiplied or not depends on the context in which it is being used.
21  */
22 typedef uint32_t GrColor;
23 
24 // shift amount to assign a component to a GrColor int
25 // These shift values are chosen for compatibility with GL attrib arrays
26 // ES doesn't allow BGRA vertex attrib order so if they were not in this order
27 // we'd have to swizzle in shaders.
28 #ifdef SK_CPU_BENDIAN
29     #define GrColor_SHIFT_R     24
30     #define GrColor_SHIFT_G     16
31     #define GrColor_SHIFT_B     8
32     #define GrColor_SHIFT_A     0
33 #else
34     #define GrColor_SHIFT_R     0
35     #define GrColor_SHIFT_G     8
36     #define GrColor_SHIFT_B     16
37     #define GrColor_SHIFT_A     24
38 #endif
39 
40 /**
41  *  Pack 4 components (RGBA) into a GrColor int
42  */
GrColorPackRGBA(unsigned r,unsigned g,unsigned b,unsigned a)43 static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a) {
44     SkASSERT((uint8_t)r == r);
45     SkASSERT((uint8_t)g == g);
46     SkASSERT((uint8_t)b == b);
47     SkASSERT((uint8_t)a == a);
48     return  (r << GrColor_SHIFT_R) |
49             (g << GrColor_SHIFT_G) |
50             (b << GrColor_SHIFT_B) |
51             (a << GrColor_SHIFT_A);
52 }
53 
54 // extract a component (byte) from a GrColor int
55 
56 #define GrColorUnpackR(color)   (((color) >> GrColor_SHIFT_R) & 0xFF)
57 #define GrColorUnpackG(color)   (((color) >> GrColor_SHIFT_G) & 0xFF)
58 #define GrColorUnpackB(color)   (((color) >> GrColor_SHIFT_B) & 0xFF)
59 #define GrColorUnpackA(color)   (((color) >> GrColor_SHIFT_A) & 0xFF)
60 
61 /**
62  *  Since premultiplied means that alpha >= color, we construct a color with
63  *  each component==255 and alpha == 0 to be "illegal"
64  */
65 #define GrColor_ILLEGAL     (~(0xFF << GrColor_SHIFT_A))
66 
67 /** Normalizes and coverts an uint8_t to a float. [0, 255] -> [0.0, 1.0] */
GrNormalizeByteToFloat(uint8_t value)68 static inline float GrNormalizeByteToFloat(uint8_t value) {
69     static const float ONE_OVER_255 = 1.f / 255.f;
70     return value * ONE_OVER_255;
71 }
72 
73 /** Used to pick vertex attribute types. */
SkPMColor4fFitsInBytes(const SkPMColor4f & color)74 static inline bool SkPMColor4fFitsInBytes(const SkPMColor4f& color) {
75     // Might want to instead check that the components are [0...a] instead of [0...1]?
76     return color.fitsInBytes();
77 }
78 
SkPMColor4f_toFP16(const SkPMColor4f & color)79 static inline uint64_t SkPMColor4f_toFP16(const SkPMColor4f& color) {
80     uint64_t halfColor;
81     to_half(skvx::float4::Load(color.vec())).store(&halfColor);
82     return halfColor;
83 }
84 
85 #endif
86