diff options
author | bsalomon <bsalomon@google.com> | 2016-01-12 13:29:26 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-12 13:29:26 -0800 |
commit | 7f9b2e4a45775e8cdd3f98260a66c0c6e1840550 (patch) | |
tree | 2634c3fe68d98ece6e9a30a4db7a251303c587ca /src/gpu/GrSwizzle.h | |
parent | 87a721b2465c9ccfa191ce9f5012f92be7731fbc (diff) |
Swizzle shader output and blend when using GL_RED to implement kAlpha_8_GrPixelConfig
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1584473002
Review URL: https://codereview.chromium.org/1584473002
Diffstat (limited to 'src/gpu/GrSwizzle.h')
-rw-r--r-- | src/gpu/GrSwizzle.h | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/src/gpu/GrSwizzle.h b/src/gpu/GrSwizzle.h index 87ea9534b9..48748803dd 100644 --- a/src/gpu/GrSwizzle.h +++ b/src/gpu/GrSwizzle.h @@ -9,6 +9,7 @@ #define GrSwizzle_DEFINED #include "GrTypes.h" +#include "GrColor.h" /** Represents a rgba swizzle. It can be converted either into a string or a eight bit int. Currently there is no way to specify an arbitrary swizzle, just some static swizzles and an @@ -17,11 +18,23 @@ class GrSwizzle { public: GrSwizzle() { *this = RGBA(); } + GrSwizzle(const GrSwizzle& that) { *this = that; } + GrSwizzle& operator=(const GrSwizzle& that) { memcpy(this, &that, sizeof(GrSwizzle)); return *this; } + /** Recreates a GrSwizzle from the output of asKey() */ + void setFromKey(uint8_t key) { + fKey = key; + for (int i = 0; i < 4; ++i) { + fSwiz[i] = IdxToChar(key & 3); + key >>= 2; + } + SkASSERT(fSwiz[4] == 0); + } + bool operator==(const GrSwizzle& that) const { return this->asUInt() == that.asUInt(); } bool operator!=(const GrSwizzle& that) const { return !(*this == that); } @@ -32,6 +45,25 @@ public: /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a'. */ const char* c_str() const { return fSwiz; } + /** Applies this swizzle to the input color and returns the swizzled color. */ + GrColor applyTo(GrColor color) const { + int idx; + uint32_t key = fKey; + // Index of the input color that should be mapped to output r. + idx = (key & 3); + uint32_t outR = (color >> idx * 8) & 0xFF; + key >>= 2; + idx = (key & 3); + uint32_t outG = (color >> idx * 8) & 0xFF; + key >>= 2; + idx = (key & 3); + uint32_t outB = (color >> idx * 8) & 0xFF; + key >>= 2; + idx = (key & 3); + uint32_t outA = (color >> idx * 8) & 0xFF; + return GrColorPackRGBA(outR, outG, outB, outA); + } + static const GrSwizzle& RGBA() { static GrSwizzle gRGBA("rgba"); return gRGBA; @@ -59,19 +91,31 @@ private: static int CharToIdx(char c) { switch (c) { case 'r': - return 0; + return (GrColor_SHIFT_R / 8); case 'g': - return 1; + return (GrColor_SHIFT_G / 8); case 'b': - return 2; + return (GrColor_SHIFT_B / 8); case 'a': - return 3; + return (GrColor_SHIFT_A / 8); default: SkFAIL("Invalid swizzle char"); return 0; } } + static /* constexpr */ char IToC(int idx) { + return (8*idx) == GrColor_SHIFT_R ? 'r' : + (8*idx) == GrColor_SHIFT_G ? 'g' : + (8*idx) == GrColor_SHIFT_B ? 'b' : 'a'; + } + + static char IdxToChar(int c) { + // Hopefully this array gets computed at compile time. + static const char gStr[4] = { IToC(0), IToC(1), IToC(2), IToC(3) }; + return gStr[c]; + } + explicit GrSwizzle(const char* str) { SkASSERT(strlen(str) == 4); fSwiz[0] = str[0]; |