2010-12-22 21:39:39 +00:00
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#ifndef GrColor_DEFINED
|
|
|
|
#define GrColor_DEFINED
|
|
|
|
|
|
|
|
#include "GrTypes.h"
|
2014-12-01 20:30:50 +00:00
|
|
|
#include "SkColor.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkUnPreMultiply.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-28 16:08:05 +00:00
|
|
|
* GrColor is 4 bytes for R, G, B, A, in a specific order defined below. The components are stored
|
|
|
|
* premultiplied.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
|
|
|
typedef uint32_t GrColor;
|
|
|
|
|
2011-11-07 15:54:49 +00:00
|
|
|
// shift amount to assign a component to a GrColor int
|
|
|
|
// These shift values are chosen for compatibility with GL attrib arrays
|
|
|
|
// ES doesn't allow BGRA vertex attrib order so if they were not in this order
|
2014-03-28 16:08:05 +00:00
|
|
|
// we'd have to swizzle in shaders.
|
|
|
|
#ifdef SK_CPU_BENDIAN
|
|
|
|
#define GrColor_SHIFT_R 24
|
|
|
|
#define GrColor_SHIFT_G 16
|
|
|
|
#define GrColor_SHIFT_B 8
|
|
|
|
#define GrColor_SHIFT_A 0
|
|
|
|
#else
|
|
|
|
#define GrColor_SHIFT_R 0
|
|
|
|
#define GrColor_SHIFT_G 8
|
|
|
|
#define GrColor_SHIFT_B 16
|
|
|
|
#define GrColor_SHIFT_A 24
|
|
|
|
#endif
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack 4 components (RGBA) into a GrColor int
|
|
|
|
*/
|
2014-12-29 15:05:27 +00:00
|
|
|
static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a) {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT((uint8_t)r == r);
|
|
|
|
SkASSERT((uint8_t)g == g);
|
|
|
|
SkASSERT((uint8_t)b == b);
|
|
|
|
SkASSERT((uint8_t)a == a);
|
2010-12-22 21:39:39 +00:00
|
|
|
return (r << GrColor_SHIFT_R) |
|
|
|
|
(g << GrColor_SHIFT_G) |
|
|
|
|
(b << GrColor_SHIFT_B) |
|
|
|
|
(a << GrColor_SHIFT_A);
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:05:27 +00:00
|
|
|
/**
|
|
|
|
* Packs a color with an alpha channel replicated across all four channels.
|
|
|
|
*/
|
|
|
|
static inline GrColor GrColorPackA4(unsigned a) {
|
|
|
|
SkASSERT((uint8_t)a == a);
|
|
|
|
return (a << GrColor_SHIFT_R) |
|
|
|
|
(a << GrColor_SHIFT_G) |
|
|
|
|
(a << GrColor_SHIFT_B) |
|
|
|
|
(a << GrColor_SHIFT_A);
|
|
|
|
}
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
// extract a component (byte) from a GrColor int
|
|
|
|
|
|
|
|
#define GrColorUnpackR(color) (((color) >> GrColor_SHIFT_R) & 0xFF)
|
|
|
|
#define GrColorUnpackG(color) (((color) >> GrColor_SHIFT_G) & 0xFF)
|
|
|
|
#define GrColorUnpackB(color) (((color) >> GrColor_SHIFT_B) & 0xFF)
|
|
|
|
#define GrColorUnpackA(color) (((color) >> GrColor_SHIFT_A) & 0xFF)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Since premultiplied means that alpha >= color, we construct a color with
|
|
|
|
* each component==255 and alpha == 0 to be "illegal"
|
|
|
|
*/
|
|
|
|
#define GrColor_ILLEGAL (~(0xFF << GrColor_SHIFT_A))
|
|
|
|
|
2014-12-09 21:31:14 +00:00
|
|
|
#define GrColor_WHITE 0xFFFFFFFF
|
|
|
|
#define GrColor_TRANS_BLACK 0x0
|
|
|
|
|
2014-03-28 16:08:05 +00:00
|
|
|
/**
|
|
|
|
* Assert in debug builds that a GrColor is premultiplied.
|
|
|
|
*/
|
2014-11-14 19:11:46 +00:00
|
|
|
static inline void GrColorIsPMAssert(GrColor SkDEBUGCODE(c)) {
|
2014-03-28 16:08:05 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
unsigned a = GrColorUnpackA(c);
|
|
|
|
unsigned r = GrColorUnpackR(c);
|
|
|
|
unsigned g = GrColorUnpackG(c);
|
|
|
|
unsigned b = GrColorUnpackB(c);
|
|
|
|
|
|
|
|
SkASSERT(r <= a);
|
|
|
|
SkASSERT(g <= a);
|
|
|
|
SkASSERT(b <= a);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-17 17:23:21 +00:00
|
|
|
/** Converts a GrColor to an rgba array of GrGLfloat */
|
|
|
|
static inline void GrColorToRGBAFloat(GrColor color, float rgba[4]) {
|
|
|
|
static const float ONE_OVER_255 = 1.f / 255.f;
|
|
|
|
rgba[0] = GrColorUnpackR(color) * ONE_OVER_255;
|
|
|
|
rgba[1] = GrColorUnpackG(color) * ONE_OVER_255;
|
|
|
|
rgba[2] = GrColorUnpackB(color) * ONE_OVER_255;
|
|
|
|
rgba[3] = GrColorUnpackA(color) * ONE_OVER_255;
|
|
|
|
}
|
|
|
|
|
2014-11-06 16:00:48 +00:00
|
|
|
/** Normalizes and coverts an uint8_t to a float. [0, 255] -> [0.0, 1.0] */
|
|
|
|
static inline float GrNormalizeByteToFloat(uint8_t value) {
|
|
|
|
static const float ONE_OVER_255 = 1.f / 255.f;
|
|
|
|
return value * ONE_OVER_255;
|
|
|
|
}
|
|
|
|
|
2014-08-11 18:07:48 +00:00
|
|
|
/** Determines whether the color is opaque or not. */
|
|
|
|
static inline bool GrColorIsOpaque(GrColor color) {
|
|
|
|
return (color & (0xFFU << GrColor_SHIFT_A)) == (0xFFU << GrColor_SHIFT_A);
|
|
|
|
}
|
|
|
|
|
2014-12-01 20:30:50 +00:00
|
|
|
/** Returns an unpremuled version of the GrColor. */
|
|
|
|
static inline GrColor GrUnPreMulColor(GrColor color) {
|
|
|
|
unsigned r = GrColorUnpackR(color);
|
|
|
|
unsigned g = GrColorUnpackG(color);
|
|
|
|
unsigned b = GrColorUnpackB(color);
|
|
|
|
unsigned a = GrColorUnpackA(color);
|
|
|
|
SkPMColor colorPM = SkPackARGB32(a, r, g, b);
|
|
|
|
SkColor colorUPM = SkUnPreMultiply::PMColorToColor(colorPM);
|
|
|
|
|
2014-12-08 20:35:16 +00:00
|
|
|
r = SkColorGetR(colorUPM);
|
|
|
|
g = SkColorGetG(colorUPM);
|
|
|
|
b = SkColorGetB(colorUPM);
|
|
|
|
a = SkColorGetA(colorUPM);
|
2014-12-01 20:30:50 +00:00
|
|
|
|
|
|
|
return GrColorPackRGBA(r, g, b, a);
|
|
|
|
}
|
|
|
|
|
2013-03-28 13:46:42 +00:00
|
|
|
/**
|
|
|
|
* Flags used for bitfields of color components. They are defined so that the bit order reflects the
|
|
|
|
* GrColor shift order.
|
|
|
|
*/
|
|
|
|
enum GrColorComponentFlags {
|
|
|
|
kR_GrColorComponentFlag = 1 << (GrColor_SHIFT_R / 8),
|
|
|
|
kG_GrColorComponentFlag = 1 << (GrColor_SHIFT_G / 8),
|
|
|
|
kB_GrColorComponentFlag = 1 << (GrColor_SHIFT_B / 8),
|
|
|
|
kA_GrColorComponentFlag = 1 << (GrColor_SHIFT_A / 8),
|
|
|
|
|
2015-05-27 22:08:33 +00:00
|
|
|
kNone_GrColorComponentFlags = 0,
|
|
|
|
|
2013-03-28 13:46:42 +00:00
|
|
|
kRGB_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
|
|
|
|
kB_GrColorComponentFlag),
|
|
|
|
|
|
|
|
kRGBA_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
|
|
|
|
kB_GrColorComponentFlag | kA_GrColorComponentFlag)
|
|
|
|
};
|
|
|
|
|
2015-05-27 22:08:33 +00:00
|
|
|
GR_MAKE_BITFIELD_OPS(GrColorComponentFlags)
|
|
|
|
|
2013-04-18 19:36:09 +00:00
|
|
|
static inline char GrColorComponentFlagToChar(GrColorComponentFlags component) {
|
2014-06-06 13:35:28 +00:00
|
|
|
SkASSERT(SkIsPow2(component));
|
2013-04-18 19:36:09 +00:00
|
|
|
switch (component) {
|
|
|
|
case kR_GrColorComponentFlag:
|
|
|
|
return 'r';
|
|
|
|
case kG_GrColorComponentFlag:
|
|
|
|
return 'g';
|
|
|
|
case kB_GrColorComponentFlag:
|
|
|
|
return 'b';
|
|
|
|
case kA_GrColorComponentFlag:
|
|
|
|
return 'a';
|
|
|
|
default:
|
2014-04-30 14:17:00 +00:00
|
|
|
SkFAIL("Invalid color component flag.");
|
2013-04-18 19:36:09 +00:00
|
|
|
return '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 13:46:42 +00:00
|
|
|
static inline uint32_t GrPixelConfigComponentMask(GrPixelConfig config) {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(config >= 0 && config < kGrPixelConfigCnt);
|
2013-03-28 13:46:42 +00:00
|
|
|
static const uint32_t kFlags[] = {
|
|
|
|
0, // kUnknown_GrPixelConfig
|
|
|
|
kA_GrColorComponentFlag, // kAlpha_8_GrPixelConfig
|
|
|
|
kRGBA_GrColorComponentFlags, // kIndex_8_GrPixelConfig
|
|
|
|
kRGB_GrColorComponentFlags, // kRGB_565_GrPixelConfig
|
|
|
|
kRGBA_GrColorComponentFlags, // kRGBA_4444_GrPixelConfig
|
|
|
|
kRGBA_GrColorComponentFlags, // kRGBA_8888_GrPixelConfig
|
|
|
|
kRGBA_GrColorComponentFlags, // kBGRA_8888_GrPixelConfig
|
2014-12-22 16:31:49 +00:00
|
|
|
kRGBA_GrColorComponentFlags, // kSRGBA_8888_GrPixelConfig
|
2014-05-30 13:55:58 +00:00
|
|
|
kRGB_GrColorComponentFlags, // kETC1_GrPixelConfig
|
|
|
|
kA_GrColorComponentFlag, // kLATC_GrPixelConfig
|
2014-06-30 16:09:22 +00:00
|
|
|
kA_GrColorComponentFlag, // kR11_EAC_GrPixelConfig
|
2014-07-16 22:21:13 +00:00
|
|
|
kRGBA_GrColorComponentFlags, // kASTC_12x12_GrPixelConfig
|
2014-07-16 20:32:56 +00:00
|
|
|
kRGBA_GrColorComponentFlags, // kRGBA_float_GrPixelConfig
|
2014-12-05 21:06:35 +00:00
|
|
|
kA_GrColorComponentFlag, // kAlpha_16_GrPixelConfig
|
2015-05-21 15:12:27 +00:00
|
|
|
kRGBA_GrColorComponentFlags, // kRGBA_half_GrPixelConfig
|
2013-03-28 13:46:42 +00:00
|
|
|
};
|
|
|
|
return kFlags[config];
|
|
|
|
|
2014-07-16 22:21:13 +00:00
|
|
|
GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
|
2014-12-22 16:31:49 +00:00
|
|
|
GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(8 == kETC1_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(9 == kLATC_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(10 == kR11_EAC_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(11 == kASTC_12x12_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(12 == kRGBA_float_GrPixelConfig);
|
|
|
|
GR_STATIC_ASSERT(13 == kAlpha_half_GrPixelConfig);
|
2015-05-21 15:12:27 +00:00
|
|
|
GR_STATIC_ASSERT(14 == kRGBA_half_GrPixelConfig);
|
2013-03-28 13:46:42 +00:00
|
|
|
GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFlags) == kGrPixelConfigCnt);
|
|
|
|
}
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#endif
|