blob: 391740be721f62bef4246641211de33bb00d5911 [file] [log] [blame]
bsalomoncdee0092016-01-08 13:20:12 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrSwizzle_DEFINED
9#define GrSwizzle_DEFINED
10
bsalomon7f9b2e42016-01-12 13:29:26 -080011#include "GrColor.h"
bsalomon6c9cd552016-01-22 07:17:34 -080012#include "SkRandom.h"
bsalomoncdee0092016-01-08 13:20:12 -080013
14/** Represents a rgba swizzle. It can be converted either into a string or a eight bit int.
15 Currently there is no way to specify an arbitrary swizzle, just some static swizzles and an
16 assignment operator. That could be relaxed. */
17class GrSwizzle {
Brian Salomon1aaccc72016-11-18 15:08:06 -050018private:
19 char fSwiz[5];
20 uint8_t fKey;
21
22 static constexpr int CToI(char c) {
23 return ('r' == c) ? (GrColor_SHIFT_R / 8) :
24 ('g' == c) ? (GrColor_SHIFT_G / 8) :
25 ('b' == c) ? (GrColor_SHIFT_B / 8) :
26 ('a' == c) ? (GrColor_SHIFT_A / 8) : -1;
27 }
28
29 static constexpr char IToC(int idx) {
30 return (8 * idx) == GrColor_SHIFT_R ? 'r' :
31 (8 * idx) == GrColor_SHIFT_G ? 'g' :
32 (8 * idx) == GrColor_SHIFT_B ? 'b' :
33 (8 * idx) == GrColor_SHIFT_A ? 'a' : 'x';
34 }
35
36 constexpr GrSwizzle(const char c[4])
37 : fSwiz{c[0], c[1], c[2], c[3], 0}
38 , fKey((CToI(c[0]) << 0) | (CToI(c[1]) << 2) | (CToI(c[2]) << 4) | (CToI(c[3]) << 6)) {}
39
40 GR_STATIC_ASSERT(sizeof(char[4]) == sizeof(uint32_t));
41 uint32_t* asUIntPtr() { return SkTCast<uint32_t*>(fSwiz); }
42 uint32_t asUInt() const { return *SkTCast<const uint32_t*>(fSwiz); }
43
bsalomoncdee0092016-01-08 13:20:12 -080044public:
45 GrSwizzle() { *this = RGBA(); }
46
bsalomon7f9b2e42016-01-12 13:29:26 -080047 GrSwizzle(const GrSwizzle& that) { *this = that; }
48
bsalomoncdee0092016-01-08 13:20:12 -080049 GrSwizzle& operator=(const GrSwizzle& that) {
50 memcpy(this, &that, sizeof(GrSwizzle));
51 return *this;
52 }
53
bsalomon7f9b2e42016-01-12 13:29:26 -080054 /** Recreates a GrSwizzle from the output of asKey() */
55 void setFromKey(uint8_t key) {
56 fKey = key;
57 for (int i = 0; i < 4; ++i) {
Brian Salomon1aaccc72016-11-18 15:08:06 -050058 fSwiz[i] = IToC(key & 3);
bsalomon7f9b2e42016-01-12 13:29:26 -080059 key >>= 2;
60 }
61 SkASSERT(fSwiz[4] == 0);
62 }
63
bsalomoncdee0092016-01-08 13:20:12 -080064 bool operator==(const GrSwizzle& that) const { return this->asUInt() == that.asUInt(); }
65
66 bool operator!=(const GrSwizzle& that) const { return !(*this == that); }
67
68 /** Compact representation of the swizzle suitable for a key. */
69 uint8_t asKey() const { return fKey; }
70
71 /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a'. */
72 const char* c_str() const { return fSwiz; }
73
bsalomon7f9b2e42016-01-12 13:29:26 -080074 /** Applies this swizzle to the input color and returns the swizzled color. */
75 GrColor applyTo(GrColor color) const {
76 int idx;
77 uint32_t key = fKey;
78 // Index of the input color that should be mapped to output r.
79 idx = (key & 3);
80 uint32_t outR = (color >> idx * 8) & 0xFF;
81 key >>= 2;
82 idx = (key & 3);
83 uint32_t outG = (color >> idx * 8) & 0xFF;
84 key >>= 2;
85 idx = (key & 3);
86 uint32_t outB = (color >> idx * 8) & 0xFF;
87 key >>= 2;
88 idx = (key & 3);
89 uint32_t outA = (color >> idx * 8) & 0xFF;
90 return GrColorPackRGBA(outR, outG, outB, outA);
91 }
92
Brian Osmance425512017-03-22 14:37:50 -040093 /** Applies this swizzle to the input color and returns the swizzled color. */
94 GrColor4f applyTo(const GrColor4f& color) const {
95 int idx;
96 uint32_t key = fKey;
97 // Index of the input color that should be mapped to output r.
98 idx = (key & 3);
99 float outR = color.fRGBA[idx];
100 key >>= 2;
101 idx = (key & 3);
102 float outG = color.fRGBA[idx];
103 key >>= 2;
104 idx = (key & 3);
105 float outB = color.fRGBA[idx];
106 key >>= 2;
107 idx = (key & 3);
108 float outA = color.fRGBA[idx];
109 return GrColor4f(outR, outG, outB, outA);
110 }
111
Brian Salomon1aaccc72016-11-18 15:08:06 -0500112 static GrSwizzle RGBA() { return GrSwizzle("rgba"); }
113 static GrSwizzle AAAA() { return GrSwizzle("aaaa"); }
114 static GrSwizzle RRRR() { return GrSwizzle("rrrr"); }
Brian Osman986563b2017-01-10 14:20:02 -0500115 static GrSwizzle RRRA() { return GrSwizzle("rrra"); }
Brian Salomon1aaccc72016-11-18 15:08:06 -0500116 static GrSwizzle BGRA() { return GrSwizzle("bgra"); }
bsalomoncdee0092016-01-08 13:20:12 -0800117
Brian Salomon1aaccc72016-11-18 15:08:06 -0500118 static GrSwizzle CreateRandom(SkRandom* random) {
bsalomon6c9cd552016-01-22 07:17:34 -0800119 switch (random->nextU() % 4) {
120 case 0:
121 return RGBA();
122 case 1:
123 return BGRA();
124 case 2:
125 return RRRR();
126 case 3:
127 return AAAA();
128 default:
129 SkFAIL("Mod is broken?!?");
130 return RGBA();
131 }
132 }
bsalomoncdee0092016-01-08 13:20:12 -0800133};
134
135#endif