From e602f395813aab8242afad356008b8e79911adbb Mon Sep 17 00:00:00 2001 From: Mike Reed Date: Tue, 6 Feb 2018 10:17:08 -0500 Subject: move GrColor.h to private, IWYU Bug: skia: Change-Id: I0f0dabd7cc54cb7786f53bd6da0c0c012375037e Reviewed-on: https://skia-review.googlesource.com/104160 Commit-Queue: Mike Reed Reviewed-by: Brian Salomon --- gm/aaxfermodes.cpp | 3 +- gm/composeshader.cpp | 1 + gm/filterbug.cpp | 1 + gm/image.cpp | 1 + gm/jpg_color_cube.cpp | 1 + gm/shapes_as_paths.cpp | 1 + gm/showmiplevels.cpp | 1 + gm/simple_magnification.cpp | 1 + gn/gpu.gni | 2 +- include/core/SkImage.h | 1 + include/core/SkShader.h | 2 +- include/gpu/GrBlend.h | 2 +- include/gpu/GrColor.h | 294 ------------------------------------- include/gpu/GrContext.h | 1 - include/private/GrColor.h | 294 +++++++++++++++++++++++++++++++++++++ src/core/SkPixmap.cpp | 1 + src/core/SkUnPreMultiplyPriv.h | 1 + src/effects/SkBlurMask.cpp | 1 + src/effects/SkEmbossMaskFilter.cpp | 1 + src/gpu/GrBlend.cpp | 1 + tests/EncodeTest.cpp | 1 + 21 files changed, 313 insertions(+), 299 deletions(-) delete mode 100644 include/gpu/GrColor.h create mode 100644 include/private/GrColor.h diff --git a/gm/aaxfermodes.cpp b/gm/aaxfermodes.cpp index 1420e3d579..43b441c6cc 100644 --- a/gm/aaxfermodes.cpp +++ b/gm/aaxfermodes.cpp @@ -6,8 +6,9 @@ */ #include "gm.h" +#include "GrColor.h" #include "sk_tool_utils.h" -#include "SkBlendModePriv.h" +#include "SkColorPriv.h" #include "SkPath.h" #include "SkShader.h" diff --git a/gm/composeshader.cpp b/gm/composeshader.cpp index ac5457faf4..8033ff739d 100644 --- a/gm/composeshader.cpp +++ b/gm/composeshader.cpp @@ -7,6 +7,7 @@ #include "gm.h" +#include "SkColorPriv.h" #include "SkCanvas.h" #include "SkGradientShader.h" #include "SkGraphics.h" diff --git a/gm/filterbug.cpp b/gm/filterbug.cpp index 181ba7c4e2..258374f73a 100644 --- a/gm/filterbug.cpp +++ b/gm/filterbug.cpp @@ -6,6 +6,7 @@ */ #include "gm.h" +#include "SkColorPriv.h" #include "SkImageShader.h" static const sk_sp make_image(int firstBlackRow, int lastBlackRow) { diff --git a/gm/image.cpp b/gm/image.cpp index e98daef42d..fc91d6b7f1 100644 --- a/gm/image.cpp +++ b/gm/image.cpp @@ -9,6 +9,7 @@ #include "gm.h" #include "sk_tool_utils.h" #include "SkAutoPixmapStorage.h" +#include "SkColorPriv.h" #include "SkData.h" #include "SkCanvas.h" #include "SkRandom.h" diff --git a/gm/jpg_color_cube.cpp b/gm/jpg_color_cube.cpp index 408cc90ea2..b083428345 100644 --- a/gm/jpg_color_cube.cpp +++ b/gm/jpg_color_cube.cpp @@ -7,6 +7,7 @@ #include "gm.h" #include "sk_tool_utils.h" #include "SkCanvas.h" +#include "SkColorPriv.h" #include "SkData.h" #include "SkImage.h" #include "SkImageEncoder.h" diff --git a/gm/shapes_as_paths.cpp b/gm/shapes_as_paths.cpp index 774ceed31d..4ad760dcc7 100644 --- a/gm/shapes_as_paths.cpp +++ b/gm/shapes_as_paths.cpp @@ -8,6 +8,7 @@ #include "gm.h" #include "SkAutoPixmapStorage.h" +#include "SkColorPriv.h" #include "SkImage.h" #include "SkPath.h" #include "SkSurface.h" diff --git a/gm/showmiplevels.cpp b/gm/showmiplevels.cpp index 4f27365788..6f0975da7e 100644 --- a/gm/showmiplevels.cpp +++ b/gm/showmiplevels.cpp @@ -9,6 +9,7 @@ #include "sk_tool_utils.h" #include "Resources.h" +#include "SkColorPriv.h" #include "SkGradientShader.h" #include "SkTypeface.h" #include "SkStream.h" diff --git a/gm/simple_magnification.cpp b/gm/simple_magnification.cpp index 14dade1307..ef5f9c6dd8 100644 --- a/gm/simple_magnification.cpp +++ b/gm/simple_magnification.cpp @@ -7,6 +7,7 @@ #include "gm.h" #include "sk_tool_utils.h" +#include "SkColorPriv.h" #include "SkImageSource.h" #include "SkMagnifierImageFilter.h" #include "SkSurface.h" diff --git a/gn/gpu.gni b/gn/gpu.gni index 4d8f3d8973..8e6f76b653 100644 --- a/gn/gpu.gni +++ b/gn/gpu.gni @@ -12,7 +12,6 @@ skia_gpu_sources = [ "$_include/gpu/GrBackendSurface.h", "$_include/gpu/GrBlend.h", "$_include/gpu/GrCaps.h", - "$_include/gpu/GrColor.h", "$_include/gpu/GrConfig.h", "$_include/gpu/GrContextOptions.h", "$_include/gpu/GrContext.h", @@ -34,6 +33,7 @@ skia_gpu_sources = [ # Private includes "$_include/private/GrAuditTrail.h", + "$_include/private/GrColor.h", "$_include/private/GrGLSL.h", "$_include/private/GrOpList.h", "$_include/private/GrSingleOwner.h", diff --git a/include/core/SkImage.h b/include/core/SkImage.h index deb0313faf..9e4e237db4 100644 --- a/include/core/SkImage.h +++ b/include/core/SkImage.h @@ -8,6 +8,7 @@ #ifndef SkImage_DEFINED #define SkImage_DEFINED +#include "GrTypes.h" #include "SkFilterQuality.h" #include "SkImageInfo.h" #include "SkImageEncoder.h" diff --git a/include/core/SkShader.h b/include/core/SkShader.h index 290d09fc87..a5ba12ec2f 100644 --- a/include/core/SkShader.h +++ b/include/core/SkShader.h @@ -9,11 +9,11 @@ #define SkShader_DEFINED #include "SkBlendMode.h" +#include "SkColor.h" #include "SkFilterQuality.h" #include "SkFlattenable.h" #include "SkImageInfo.h" #include "SkMatrix.h" -#include "../gpu/GrColor.h" class SkArenaAlloc; class SkBitmap; diff --git a/include/gpu/GrBlend.h b/include/gpu/GrBlend.h index 30b0b9b6e0..8c968f2d2e 100644 --- a/include/gpu/GrBlend.h +++ b/include/gpu/GrBlend.h @@ -9,7 +9,7 @@ #ifndef GrBlend_DEFINED #define GrBlend_DEFINED -#include "GrColor.h" +#include "GrTypes.h" #include "../private/SkTLogic.h" /** diff --git a/include/gpu/GrColor.h b/include/gpu/GrColor.h deleted file mode 100644 index b4a461d738..0000000000 --- a/include/gpu/GrColor.h +++ /dev/null @@ -1,294 +0,0 @@ - -/* - * Copyright 2010 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - - - -#ifndef GrColor_DEFINED -#define GrColor_DEFINED - -#include "GrTypes.h" -#include "SkColor.h" -#include "SkColorPriv.h" -#include "SkUnPreMultiply.h" - -/** - * 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. - */ -typedef uint32_t GrColor; - -// 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 -// 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 - -/** - * Pack 4 components (RGBA) into a GrColor int - */ -static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a) { - SkASSERT((uint8_t)r == r); - SkASSERT((uint8_t)g == g); - SkASSERT((uint8_t)b == b); - SkASSERT((uint8_t)a == a); - return (r << GrColor_SHIFT_R) | - (g << GrColor_SHIFT_G) | - (b << GrColor_SHIFT_B) | - (a << GrColor_SHIFT_A); -} - -/** - * 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); -} - -// 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)) - -#define GrColor_WHITE 0xFFFFFFFF -#define GrColor_TRANSPARENT_BLACK 0x0 - -/** - * Assert in debug builds that a GrColor is premultiplied. - */ -static inline void GrColorIsPMAssert(GrColor SkDEBUGCODE(c)) { -#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 -} - -/** Inverts each color channel. */ -static inline GrColor GrInvertColor(GrColor c) { - U8CPU a = GrColorUnpackA(c); - U8CPU r = GrColorUnpackR(c); - U8CPU g = GrColorUnpackG(c); - U8CPU b = GrColorUnpackB(c); - return GrColorPackRGBA(0xff - r, 0xff - g, 0xff - b, 0xff - a); -} - -static inline GrColor GrColorMul(GrColor c0, GrColor c1) { - U8CPU r = SkMulDiv255Round(GrColorUnpackR(c0), GrColorUnpackR(c1)); - U8CPU g = SkMulDiv255Round(GrColorUnpackG(c0), GrColorUnpackG(c1)); - U8CPU b = SkMulDiv255Round(GrColorUnpackB(c0), GrColorUnpackB(c1)); - U8CPU a = SkMulDiv255Round(GrColorUnpackA(c0), GrColorUnpackA(c1)); - return GrColorPackRGBA(r, g, b, a); -} - -static inline GrColor GrColorSatAdd(GrColor c0, GrColor c1) { - unsigned r = SkTMin(GrColorUnpackR(c0) + GrColorUnpackR(c1), 0xff); - unsigned g = SkTMin(GrColorUnpackG(c0) + GrColorUnpackG(c1), 0xff); - unsigned b = SkTMin(GrColorUnpackB(c0) + GrColorUnpackB(c1), 0xff); - unsigned a = SkTMin(GrColorUnpackA(c0) + GrColorUnpackA(c1), 0xff); - return GrColorPackRGBA(r, g, b, a); -} - -/** 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; -} - -/** 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; -} - -/** Determines whether the color is opaque or not. */ -static inline bool GrColorIsOpaque(GrColor color) { - return (color & (0xFFU << GrColor_SHIFT_A)) == (0xFFU << GrColor_SHIFT_A); -} - -static inline GrColor GrPremulColor(GrColor color) { - unsigned r = GrColorUnpackR(color); - unsigned g = GrColorUnpackG(color); - unsigned b = GrColorUnpackB(color); - unsigned a = GrColorUnpackA(color); - return GrColorPackRGBA(SkMulDiv255Round(r, a), - SkMulDiv255Round(g, a), - SkMulDiv255Round(b, a), - a); -} - -/** Returns an unpremuled version of the GrColor. */ -static inline GrColor GrUnpremulColor(GrColor color) { - GrColorIsPMAssert(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); - - r = SkColorGetR(colorUPM); - g = SkColorGetG(colorUPM); - b = SkColorGetB(colorUPM); - a = SkColorGetA(colorUPM); - - return GrColorPackRGBA(r, g, b, a); -} - - -/** -* Similarly, GrColor4f is 4 floats for R, G, B, A, in that order. And like GrColor, whether -* the color is premultiplied or not depends on the context. -*/ -struct GrColor4f { - float fRGBA[4]; - - GrColor4f() {} - GrColor4f(float r, float g, float b, float a) { - fRGBA[0] = r; - fRGBA[1] = g; - fRGBA[2] = b; - fRGBA[3] = a; - } - - enum Illegal_Constructor { - kIllegalConstructor - }; - GrColor4f(Illegal_Constructor) { - fRGBA[0] = SK_FloatNaN; - fRGBA[1] = SK_FloatNaN; - fRGBA[2] = SK_FloatNaN; - fRGBA[3] = SK_FloatNaN; - } - - static GrColor4f OpaqueWhite() { - return GrColor4f(1.0f, 1.0f, 1.0f, 1.0f); - } - - static GrColor4f TransparentBlack() { - return GrColor4f(0.0f, 0.0f, 0.0f, 0.0f); - } - - static GrColor4f FromGrColor(GrColor color) { - GrColor4f result; - GrColorToRGBAFloat(color, result.fRGBA); - return result; - } - - static GrColor4f FromSkColor4f(const SkColor4f& color) { - return GrColor4f(color.fR, color.fG, color.fB, color.fA); - } - - GrColor4f modulate(const GrColor4f& x) const { - return GrColor4f(fRGBA[0] * x.fRGBA[0], - fRGBA[1] * x.fRGBA[1], - fRGBA[2] * x.fRGBA[2], - fRGBA[3] * x.fRGBA[3]); - } - - GrColor4f mulByScalar(float x) const { - return GrColor4f(fRGBA[0] * x, fRGBA[1] * x, fRGBA[2] * x, fRGBA[3] * x); - } - - bool operator==(const GrColor4f& other) const { - return - fRGBA[0] == other.fRGBA[0] && - fRGBA[1] == other.fRGBA[1] && - fRGBA[2] == other.fRGBA[2] && - fRGBA[3] == other.fRGBA[3]; - } - bool operator!=(const GrColor4f& other) const { - return !(*this == other); - } - - GrColor toGrColor() const { - return GrColorPackRGBA( - SkTPin(static_cast(fRGBA[0] * 255.0f + 0.5f), 0, 255), - SkTPin(static_cast(fRGBA[1] * 255.0f + 0.5f), 0, 255), - SkTPin(static_cast(fRGBA[2] * 255.0f + 0.5f), 0, 255), - SkTPin(static_cast(fRGBA[3] * 255.0f + 0.5f), 0, 255)); - } - - SkColor4f toSkColor4f() const { - return SkColor4f { fRGBA[0], fRGBA[1], fRGBA[2], fRGBA[3] }; - } - - GrColor4f opaque() const { - return GrColor4f(fRGBA[0], fRGBA[1], fRGBA[2], 1.0f); - } - - bool isOpaque() const { - return fRGBA[3] >= 1.f; // just in case precision causes a superopaque value. - } - - GrColor4f premul() const { - float a = fRGBA[3]; - return GrColor4f(fRGBA[0] * a, fRGBA[1] * a, fRGBA[2] * a, a); - } - - GrColor4f unpremul() const { - float a = fRGBA[3]; - if (a <= 0.0f) { - return GrColor4f(0.0f, 0.0f, 0.0f, 0.0f); - } - float invAlpha = 1.0f / a; - return GrColor4f(fRGBA[0] * invAlpha, fRGBA[1] * invAlpha, fRGBA[2] * invAlpha, a); - } -}; - -/** - * 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), - - kNone_GrColorComponentFlags = 0, - - kRGB_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag | - kB_GrColorComponentFlag), - - kRGBA_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag | - kB_GrColorComponentFlag | kA_GrColorComponentFlag) -}; - -GR_MAKE_BITFIELD_OPS(GrColorComponentFlags) - -#endif diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h index d0ca90e0f0..df9b85bacd 100644 --- a/include/gpu/GrContext.h +++ b/include/gpu/GrContext.h @@ -9,7 +9,6 @@ #define GrContext_DEFINED #include "GrCaps.h" -#include "GrColor.h" #include "SkMatrix.h" #include "SkPathEffect.h" #include "SkTypes.h" diff --git a/include/private/GrColor.h b/include/private/GrColor.h new file mode 100644 index 0000000000..b4a461d738 --- /dev/null +++ b/include/private/GrColor.h @@ -0,0 +1,294 @@ + +/* + * Copyright 2010 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + + + +#ifndef GrColor_DEFINED +#define GrColor_DEFINED + +#include "GrTypes.h" +#include "SkColor.h" +#include "SkColorPriv.h" +#include "SkUnPreMultiply.h" + +/** + * 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. + */ +typedef uint32_t GrColor; + +// 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 +// 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 + +/** + * Pack 4 components (RGBA) into a GrColor int + */ +static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a) { + SkASSERT((uint8_t)r == r); + SkASSERT((uint8_t)g == g); + SkASSERT((uint8_t)b == b); + SkASSERT((uint8_t)a == a); + return (r << GrColor_SHIFT_R) | + (g << GrColor_SHIFT_G) | + (b << GrColor_SHIFT_B) | + (a << GrColor_SHIFT_A); +} + +/** + * 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); +} + +// 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)) + +#define GrColor_WHITE 0xFFFFFFFF +#define GrColor_TRANSPARENT_BLACK 0x0 + +/** + * Assert in debug builds that a GrColor is premultiplied. + */ +static inline void GrColorIsPMAssert(GrColor SkDEBUGCODE(c)) { +#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 +} + +/** Inverts each color channel. */ +static inline GrColor GrInvertColor(GrColor c) { + U8CPU a = GrColorUnpackA(c); + U8CPU r = GrColorUnpackR(c); + U8CPU g = GrColorUnpackG(c); + U8CPU b = GrColorUnpackB(c); + return GrColorPackRGBA(0xff - r, 0xff - g, 0xff - b, 0xff - a); +} + +static inline GrColor GrColorMul(GrColor c0, GrColor c1) { + U8CPU r = SkMulDiv255Round(GrColorUnpackR(c0), GrColorUnpackR(c1)); + U8CPU g = SkMulDiv255Round(GrColorUnpackG(c0), GrColorUnpackG(c1)); + U8CPU b = SkMulDiv255Round(GrColorUnpackB(c0), GrColorUnpackB(c1)); + U8CPU a = SkMulDiv255Round(GrColorUnpackA(c0), GrColorUnpackA(c1)); + return GrColorPackRGBA(r, g, b, a); +} + +static inline GrColor GrColorSatAdd(GrColor c0, GrColor c1) { + unsigned r = SkTMin(GrColorUnpackR(c0) + GrColorUnpackR(c1), 0xff); + unsigned g = SkTMin(GrColorUnpackG(c0) + GrColorUnpackG(c1), 0xff); + unsigned b = SkTMin(GrColorUnpackB(c0) + GrColorUnpackB(c1), 0xff); + unsigned a = SkTMin(GrColorUnpackA(c0) + GrColorUnpackA(c1), 0xff); + return GrColorPackRGBA(r, g, b, a); +} + +/** 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; +} + +/** 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; +} + +/** Determines whether the color is opaque or not. */ +static inline bool GrColorIsOpaque(GrColor color) { + return (color & (0xFFU << GrColor_SHIFT_A)) == (0xFFU << GrColor_SHIFT_A); +} + +static inline GrColor GrPremulColor(GrColor color) { + unsigned r = GrColorUnpackR(color); + unsigned g = GrColorUnpackG(color); + unsigned b = GrColorUnpackB(color); + unsigned a = GrColorUnpackA(color); + return GrColorPackRGBA(SkMulDiv255Round(r, a), + SkMulDiv255Round(g, a), + SkMulDiv255Round(b, a), + a); +} + +/** Returns an unpremuled version of the GrColor. */ +static inline GrColor GrUnpremulColor(GrColor color) { + GrColorIsPMAssert(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); + + r = SkColorGetR(colorUPM); + g = SkColorGetG(colorUPM); + b = SkColorGetB(colorUPM); + a = SkColorGetA(colorUPM); + + return GrColorPackRGBA(r, g, b, a); +} + + +/** +* Similarly, GrColor4f is 4 floats for R, G, B, A, in that order. And like GrColor, whether +* the color is premultiplied or not depends on the context. +*/ +struct GrColor4f { + float fRGBA[4]; + + GrColor4f() {} + GrColor4f(float r, float g, float b, float a) { + fRGBA[0] = r; + fRGBA[1] = g; + fRGBA[2] = b; + fRGBA[3] = a; + } + + enum Illegal_Constructor { + kIllegalConstructor + }; + GrColor4f(Illegal_Constructor) { + fRGBA[0] = SK_FloatNaN; + fRGBA[1] = SK_FloatNaN; + fRGBA[2] = SK_FloatNaN; + fRGBA[3] = SK_FloatNaN; + } + + static GrColor4f OpaqueWhite() { + return GrColor4f(1.0f, 1.0f, 1.0f, 1.0f); + } + + static GrColor4f TransparentBlack() { + return GrColor4f(0.0f, 0.0f, 0.0f, 0.0f); + } + + static GrColor4f FromGrColor(GrColor color) { + GrColor4f result; + GrColorToRGBAFloat(color, result.fRGBA); + return result; + } + + static GrColor4f FromSkColor4f(const SkColor4f& color) { + return GrColor4f(color.fR, color.fG, color.fB, color.fA); + } + + GrColor4f modulate(const GrColor4f& x) const { + return GrColor4f(fRGBA[0] * x.fRGBA[0], + fRGBA[1] * x.fRGBA[1], + fRGBA[2] * x.fRGBA[2], + fRGBA[3] * x.fRGBA[3]); + } + + GrColor4f mulByScalar(float x) const { + return GrColor4f(fRGBA[0] * x, fRGBA[1] * x, fRGBA[2] * x, fRGBA[3] * x); + } + + bool operator==(const GrColor4f& other) const { + return + fRGBA[0] == other.fRGBA[0] && + fRGBA[1] == other.fRGBA[1] && + fRGBA[2] == other.fRGBA[2] && + fRGBA[3] == other.fRGBA[3]; + } + bool operator!=(const GrColor4f& other) const { + return !(*this == other); + } + + GrColor toGrColor() const { + return GrColorPackRGBA( + SkTPin(static_cast(fRGBA[0] * 255.0f + 0.5f), 0, 255), + SkTPin(static_cast(fRGBA[1] * 255.0f + 0.5f), 0, 255), + SkTPin(static_cast(fRGBA[2] * 255.0f + 0.5f), 0, 255), + SkTPin(static_cast(fRGBA[3] * 255.0f + 0.5f), 0, 255)); + } + + SkColor4f toSkColor4f() const { + return SkColor4f { fRGBA[0], fRGBA[1], fRGBA[2], fRGBA[3] }; + } + + GrColor4f opaque() const { + return GrColor4f(fRGBA[0], fRGBA[1], fRGBA[2], 1.0f); + } + + bool isOpaque() const { + return fRGBA[3] >= 1.f; // just in case precision causes a superopaque value. + } + + GrColor4f premul() const { + float a = fRGBA[3]; + return GrColor4f(fRGBA[0] * a, fRGBA[1] * a, fRGBA[2] * a, a); + } + + GrColor4f unpremul() const { + float a = fRGBA[3]; + if (a <= 0.0f) { + return GrColor4f(0.0f, 0.0f, 0.0f, 0.0f); + } + float invAlpha = 1.0f / a; + return GrColor4f(fRGBA[0] * invAlpha, fRGBA[1] * invAlpha, fRGBA[2] * invAlpha, a); + } +}; + +/** + * 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), + + kNone_GrColorComponentFlags = 0, + + kRGB_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag | + kB_GrColorComponentFlag), + + kRGBA_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag | + kB_GrColorComponentFlag | kA_GrColorComponentFlag) +}; + +GR_MAKE_BITFIELD_OPS(GrColorComponentFlags) + +#endif diff --git a/src/core/SkPixmap.cpp b/src/core/SkPixmap.cpp index 7873a9b8ce..42e2c66029 100644 --- a/src/core/SkPixmap.cpp +++ b/src/core/SkPixmap.cpp @@ -19,6 +19,7 @@ #include "SkReadPixelsRec.h" #include "SkSurface.h" #include "SkTemplates.h" +#include "SkUnPreMultiply.h" #include "SkUtils.h" ///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/SkUnPreMultiplyPriv.h b/src/core/SkUnPreMultiplyPriv.h index 073e239ed7..f25e78f96a 100644 --- a/src/core/SkUnPreMultiplyPriv.h +++ b/src/core/SkUnPreMultiplyPriv.h @@ -9,6 +9,7 @@ #define SkUnPreMultiplyPriv_DEFINED #include "SkColor.h" +#include "SkUnPreMultiply.h" template void SkUnpremultiplyRow(uint32_t* dst, const uint32_t* src, int count) { diff --git a/src/effects/SkBlurMask.cpp b/src/effects/SkBlurMask.cpp index f06e2b9f1e..59d70205c9 100644 --- a/src/effects/SkBlurMask.cpp +++ b/src/effects/SkBlurMask.cpp @@ -7,6 +7,7 @@ #include "SkBlurMask.h" +#include "SkColorPriv.h" #include "SkMaskBlurFilter.h" #include "SkMath.h" #include "SkTemplates.h" diff --git a/src/effects/SkEmbossMaskFilter.cpp b/src/effects/SkEmbossMaskFilter.cpp index 4caef9ef0b..9fa26c5731 100644 --- a/src/effects/SkEmbossMaskFilter.cpp +++ b/src/effects/SkEmbossMaskFilter.cpp @@ -8,6 +8,7 @@ #include "SkEmbossMaskFilter.h" #include "SkBlurMaskFilter.h" #include "SkBlurMask.h" +#include "SkColorPriv.h" #include "SkEmbossMask.h" #include "SkReadBuffer.h" #include "SkWriteBuffer.h" diff --git a/src/gpu/GrBlend.cpp b/src/gpu/GrBlend.cpp index 1bb4dd3c9c..436829ce63 100644 --- a/src/gpu/GrBlend.cpp +++ b/src/gpu/GrBlend.cpp @@ -6,6 +6,7 @@ */ #include "GrBlend.h" +#include "../private/GrColor.h" /** * MaskedColor is used to evaluate the color and valid color component flags through the diff --git a/tests/EncodeTest.cpp b/tests/EncodeTest.cpp index 40e99565cb..8d5db7ddcd 100644 --- a/tests/EncodeTest.cpp +++ b/tests/EncodeTest.cpp @@ -9,6 +9,7 @@ #include "Test.h" #include "SkBitmap.h" +#include "SkColorPriv.h" #include "SkEncodedImageFormat.h" #include "SkImage.h" #include "SkJpegEncoder.h" -- cgit v1.2.3