| 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/gpu/ganesh/GrTypes.h" |
| 13 |
#include "src/base/SkHalf.h" |
| 14 |
#include "src/base/SkVx.h" |
| 15 |
#include "src/core/SkColorData.h" |
| 16 |
#include "src/core/SkColorPriv.h" |
| 17 |
#include "src/gpu/BufferWriter.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 |
// extract a component (byte) from a GrColor int |
| 56 |
|
| 57 |
#define GrColorUnpackR(color) (((color) >> GrColor_SHIFT_R) & 0xFF) |
| 58 |
#define GrColorUnpackG(color) (((color) >> GrColor_SHIFT_G) & 0xFF) |
| 59 |
#define GrColorUnpackB(color) (((color) >> GrColor_SHIFT_B) & 0xFF) |
| 60 |
#define GrColorUnpackA(color) (((color) >> GrColor_SHIFT_A) & 0xFF) |
| 61 |
|
| 62 |
/** |
| 63 |
* Since premultiplied means that alpha >= color, we construct a color with |
| 64 |
* each component==255 and alpha == 0 to be "illegal" |
| 65 |
*/ |
| 66 |
#define GrColor_ILLEGAL (~(0xFF << GrColor_SHIFT_A)) |
| 67 |
|
| 68 |
/** Normalizes and coverts an uint8_t to a float. [0, 255] -> [0.0, 1.0] */ |
| 69 |
static inline float GrNormalizeByteToFloat(uint8_t value) { |
| 70 |
static const float ONE_OVER_255 = 1.f / 255.f; |
| 71 |
return value * ONE_OVER_255; |
| 72 |
} |
| 73 |
|
| 74 |
/** Used to pick vertex attribute types. */ |
| 75 |
static inline bool SkPMColor4fFitsInBytes(const SkPMColor4f& color) { |
| 76 |
// Might want to instead check that the components are [0...a] instead of [0...1]? |
| 77 |
return color.fitsInBytes(); |
| 78 |
} |
| 79 |
|
| 80 |
static inline uint64_t SkPMColor4f_toFP16(const SkPMColor4f& color) { |
| 81 |
uint64_t halfColor; |
| 82 |
to_half(skvx::float4::Load(color.vec())).store(&halfColor); |
| 83 |
return halfColor; |
| 84 |
} |
| 85 |
|
| 86 |
#endif |