1 | /* |
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 | |
11 | #include "include/core/SkString.h" |
12 | #include "include/private/SkColorData.h" |
13 | #include "src/gpu/GrColor.h" |
14 | |
15 | class SkRasterPipeline; |
16 | |
17 | /** Represents a rgba swizzle. It can be converted either into a string or a eight bit int. */ |
18 | class GrSwizzle { |
19 | public: |
20 | constexpr GrSwizzle() : GrSwizzle("rgba" ) {} |
21 | explicit constexpr GrSwizzle(const char c[4]); |
22 | |
23 | constexpr GrSwizzle(const GrSwizzle&); |
24 | constexpr GrSwizzle& operator=(const GrSwizzle& that); |
25 | |
26 | static constexpr GrSwizzle Concat(const GrSwizzle& a, const GrSwizzle& b); |
27 | |
28 | constexpr bool operator==(const GrSwizzle& that) const { return fKey == that.fKey; } |
29 | constexpr bool operator!=(const GrSwizzle& that) const { return !(*this == that); } |
30 | |
31 | /** Compact representation of the swizzle suitable for a key. */ |
32 | constexpr uint16_t asKey() const { return fKey; } |
33 | |
34 | /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a', '0', and '1'. */ |
35 | SkString asString() const; |
36 | |
37 | constexpr char operator[](int i) const { |
38 | SkASSERT(i >= 0 && i < 4); |
39 | int idx = (fKey >> (4U * i)) & 0xfU; |
40 | return IToC(idx); |
41 | } |
42 | |
43 | /** Applies this swizzle to the input color and returns the swizzled color. */ |
44 | template <SkAlphaType AlphaType> |
45 | constexpr SkRGBA4f<AlphaType> applyTo(const SkRGBA4f<AlphaType>& color) const; |
46 | |
47 | void apply(SkRasterPipeline*) const; |
48 | |
49 | static constexpr GrSwizzle RGBA() { return GrSwizzle("rgba" ); } |
50 | static constexpr GrSwizzle AAAA() { return GrSwizzle("aaaa" ); } |
51 | static constexpr GrSwizzle RRRR() { return GrSwizzle("rrrr" ); } |
52 | static constexpr GrSwizzle RRRA() { return GrSwizzle("rrra" ); } |
53 | static constexpr GrSwizzle BGRA() { return GrSwizzle("bgra" ); } |
54 | static constexpr GrSwizzle RGB1() { return GrSwizzle("rgb1" ); } |
55 | |
56 | private: |
57 | explicit constexpr GrSwizzle(uint16_t key) : fKey(key) {} |
58 | |
59 | template <SkAlphaType AlphaType> |
60 | static constexpr float ComponentIndexToFloat(const SkRGBA4f<AlphaType>& color, int idx); |
61 | static constexpr int CToI(char c); |
62 | static constexpr char IToC(int idx); |
63 | |
64 | uint16_t fKey; |
65 | }; |
66 | |
67 | constexpr GrSwizzle::GrSwizzle(const char c[4]) |
68 | : fKey((CToI(c[0]) << 0) | (CToI(c[1]) << 4) | (CToI(c[2]) << 8) | (CToI(c[3]) << 12)) {} |
69 | |
70 | constexpr GrSwizzle::GrSwizzle(const GrSwizzle& that) |
71 | : fKey(that.fKey) {} |
72 | |
73 | constexpr GrSwizzle& GrSwizzle::operator=(const GrSwizzle& that) { |
74 | fKey = that.fKey; |
75 | return *this; |
76 | } |
77 | |
78 | template <SkAlphaType AlphaType> |
79 | constexpr SkRGBA4f<AlphaType> GrSwizzle::applyTo(const SkRGBA4f<AlphaType>& color) const { |
80 | uint32_t key = fKey; |
81 | // Index of the input color that should be mapped to output r. |
82 | int idx = (key & 15); |
83 | float outR = ComponentIndexToFloat(color, idx); |
84 | key >>= 4; |
85 | idx = (key & 15); |
86 | float outG = ComponentIndexToFloat(color, idx); |
87 | key >>= 4; |
88 | idx = (key & 15); |
89 | float outB = ComponentIndexToFloat(color, idx); |
90 | key >>= 4; |
91 | idx = (key & 15); |
92 | float outA = ComponentIndexToFloat(color, idx); |
93 | return { outR, outG, outB, outA }; |
94 | } |
95 | |
96 | template <SkAlphaType AlphaType> |
97 | constexpr float GrSwizzle::ComponentIndexToFloat(const SkRGBA4f<AlphaType>& color, int idx) { |
98 | if (idx <= 3) { |
99 | return color[idx]; |
100 | } |
101 | if (idx == CToI('1')) { |
102 | return 1.0f; |
103 | } |
104 | if (idx == CToI('0')) { |
105 | return 1.0f; |
106 | } |
107 | SkUNREACHABLE; |
108 | } |
109 | |
110 | constexpr int GrSwizzle::CToI(char c) { |
111 | switch (c) { |
112 | // r...a must map to 0...3 because other methods use them as indices into fSwiz. |
113 | case 'r': return 0; |
114 | case 'g': return 1; |
115 | case 'b': return 2; |
116 | case 'a': return 3; |
117 | case '0': return 4; |
118 | case '1': return 5; |
119 | default: SkUNREACHABLE; |
120 | } |
121 | } |
122 | |
123 | constexpr char GrSwizzle::IToC(int idx) { |
124 | switch (idx) { |
125 | case CToI('r'): return 'r'; |
126 | case CToI('g'): return 'g'; |
127 | case CToI('b'): return 'b'; |
128 | case CToI('a'): return 'a'; |
129 | case CToI('0'): return '0'; |
130 | case CToI('1'): return '1'; |
131 | default: SkUNREACHABLE; |
132 | } |
133 | } |
134 | |
135 | constexpr GrSwizzle GrSwizzle::Concat(const GrSwizzle& a, const GrSwizzle& b) { |
136 | uint16_t key = 0; |
137 | for (int i = 0; i < 4; ++i) { |
138 | int idx = (b.fKey >> (4U * i)) & 0xfU; |
139 | if (idx != CToI('0') && idx != CToI('1')) { |
140 | SkASSERT(idx >= 0 && idx < 4); |
141 | // Get the index value stored in a at location idx. |
142 | idx = ((a.fKey >> (4U * idx)) & 0xfU); |
143 | } |
144 | key |= (idx << (4U * i)); |
145 | } |
146 | return GrSwizzle(key); |
147 | } |
148 | #endif |
149 | |