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
|
|
|
*/
|
|
|
|
|
2018-09-07 17:32:47 +00:00
|
|
|
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#ifndef GrColor_DEFINED
|
|
|
|
#define GrColor_DEFINED
|
|
|
|
|
2014-12-01 20:30:50 +00:00
|
|
|
#include "SkColor.h"
|
2018-11-16 16:12:37 +00:00
|
|
|
#include "SkColorData.h"
|
2014-12-01 20:30:50 +00:00
|
|
|
#include "SkColorPriv.h"
|
2018-11-19 16:56:57 +00:00
|
|
|
#include "SkHalf.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-28 13:26:28 +00:00
|
|
|
* GrColor is 4 bytes for R, G, B, A, in a specific order defined below. Whether the color is
|
|
|
|
* premultiplied or not depends on the context in which it is being used.
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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;
|
|
|
|
}
|
|
|
|
|
2018-11-16 16:12:37 +00:00
|
|
|
/** Returns true if all channels are in [0, 1]. Used to pick vertex attribute types. */
|
|
|
|
static inline bool SkPMColor4fFitsInBytes(const SkPMColor4f& color) {
|
|
|
|
SkASSERT(color.fA >= 0.0f && color.fA <= 1.0f);
|
|
|
|
return color.fR >= 0.0f && color.fR <= 1.0f &&
|
|
|
|
color.fG >= 0.0f && color.fG <= 1.0f &&
|
|
|
|
color.fB >= 0.0f && color.fB <= 1.0f;
|
|
|
|
}
|
|
|
|
|
2019-01-09 21:30:12 +00:00
|
|
|
static inline uint64_t SkPMColor4f_toFP16(const SkPMColor4f& color) {
|
|
|
|
uint64_t halfColor;
|
|
|
|
SkFloatToHalf_finite_ftz(Sk4f::Load(color.vec())).store(&halfColor);
|
|
|
|
return halfColor;
|
|
|
|
}
|
|
|
|
|
2018-11-19 16:56:57 +00:00
|
|
|
/**
|
|
|
|
* GrVertexColor is a helper for writing colors to a vertex attribute. It stores either GrColor
|
|
|
|
* or four half-float channels, depending on the wideColor parameter. GrVertexWriter will write
|
|
|
|
* the correct amount of data. Note that the GP needs to have been constructed with the correct
|
|
|
|
* attribute type for colors, to match the usage here.
|
|
|
|
*/
|
|
|
|
class GrVertexColor {
|
|
|
|
public:
|
2018-11-20 16:39:15 +00:00
|
|
|
explicit GrVertexColor(const SkPMColor4f& color, bool wideColor)
|
2018-11-19 16:56:57 +00:00
|
|
|
: fWideColor(wideColor) {
|
|
|
|
if (wideColor) {
|
|
|
|
SkFloatToHalf_finite_ftz(Sk4f::Load(color.vec())).store(&fColor);
|
|
|
|
} else {
|
|
|
|
fColor[0] = color.toBytes_RGBA();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 13:55:10 +00:00
|
|
|
size_t size() const { return fWideColor ? 8 : 4; }
|
|
|
|
|
2018-11-19 16:56:57 +00:00
|
|
|
private:
|
|
|
|
friend struct GrVertexWriter;
|
|
|
|
|
|
|
|
uint32_t fColor[2];
|
|
|
|
bool fWideColor;
|
|
|
|
};
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#endif
|