blob: 05db3c08ccdec109d59a435855fe2801070c8965 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkColorData.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrColor.h"
bsalomoncdee0092016-01-08 13:20:12 -080013
Brian Salomon217522c2019-06-11 15:55:30 -040014class SkRasterPipeline;
15
16/** Represents a rgba swizzle. It can be converted either into a string or a eight bit int. */
bsalomoncdee0092016-01-08 13:20:12 -080017class GrSwizzle {
18public:
Brian Salomon21511652018-10-17 08:30:51 -040019 constexpr GrSwizzle() : GrSwizzle("rgba") {}
Brian Salomon217522c2019-06-11 15:55:30 -040020 explicit constexpr GrSwizzle(const char c[4]);
bsalomoncdee0092016-01-08 13:20:12 -080021
Brian Salomon217522c2019-06-11 15:55:30 -040022 constexpr GrSwizzle(const GrSwizzle&);
23 constexpr GrSwizzle& operator=(const GrSwizzle& that);
bsalomoncdee0092016-01-08 13:20:12 -080024
Brian Salomonf30b1c12019-06-20 12:25:02 -040025 static constexpr GrSwizzle Concat(const GrSwizzle& a, const GrSwizzle& b);
26
bsalomon7f9b2e42016-01-12 13:29:26 -080027 /** Recreates a GrSwizzle from the output of asKey() */
Brian Salomon217522c2019-06-11 15:55:30 -040028 constexpr void setFromKey(uint16_t key);
Brian Salomonf30b1c12019-06-20 12:25:02 -040029
Brian Salomon21511652018-10-17 08:30:51 -040030 constexpr bool operator==(const GrSwizzle& that) const { return fKey == that.fKey; }
31 constexpr bool operator!=(const GrSwizzle& that) const { return !(*this == that); }
bsalomoncdee0092016-01-08 13:20:12 -080032
33 /** Compact representation of the swizzle suitable for a key. */
Greg Danielf259b8b2019-02-14 09:03:43 -050034 constexpr uint16_t asKey() const { return fKey; }
bsalomoncdee0092016-01-08 13:20:12 -080035
Brian Salomonf30b1c12019-06-20 12:25:02 -040036 /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a', '0', and '1'. */
Brian Salomon217522c2019-06-11 15:55:30 -040037 constexpr const char* c_str() const { return fSwiz; }
bsalomoncdee0092016-01-08 13:20:12 -080038
Brian Salomon217522c2019-06-11 15:55:30 -040039 constexpr char operator[](int i) const {
Brian Salomondc829942018-10-23 16:07:24 -040040 SkASSERT(i >= 0 && i < 4);
41 return fSwiz[i];
42 }
43
bsalomon7f9b2e42016-01-12 13:29:26 -080044 /** Applies this swizzle to the input color and returns the swizzled color. */
Brian Salomonb450f3b2019-07-09 09:36:51 -040045 template <SkAlphaType AlphaType>
46 constexpr SkRGBA4f<AlphaType> applyTo(const SkRGBA4f<AlphaType>& color) const;
Brian Salomon217522c2019-06-11 15:55:30 -040047
48 void apply(SkRasterPipeline*) const;
Brian Osmance425512017-03-22 14:37:50 -040049
Brian Salomon21511652018-10-17 08:30:51 -040050 static constexpr GrSwizzle RGBA() { return GrSwizzle("rgba"); }
51 static constexpr GrSwizzle AAAA() { return GrSwizzle("aaaa"); }
52 static constexpr GrSwizzle RRRR() { return GrSwizzle("rrrr"); }
53 static constexpr GrSwizzle RRRA() { return GrSwizzle("rrra"); }
54 static constexpr GrSwizzle BGRA() { return GrSwizzle("bgra"); }
Greg Danielf259b8b2019-02-14 09:03:43 -050055 static constexpr GrSwizzle RGB1() { return GrSwizzle("rgb1"); }
bsalomoncdee0092016-01-08 13:20:12 -080056
Brian Salomon21511652018-10-17 08:30:51 -040057private:
Brian Salomonb450f3b2019-07-09 09:36:51 -040058 template <SkAlphaType AlphaType>
59 static constexpr float ComponentIndexToFloat(const SkRGBA4f<AlphaType>& color, int idx);
Brian Salomon217522c2019-06-11 15:55:30 -040060 static constexpr int CToI(char c);
61 static constexpr char IToC(int idx);
62
Brian Salomon21511652018-10-17 08:30:51 -040063 char fSwiz[5];
Greg Danielf259b8b2019-02-14 09:03:43 -050064 uint16_t fKey;
bsalomoncdee0092016-01-08 13:20:12 -080065};
66
Brian Salomon217522c2019-06-11 15:55:30 -040067constexpr GrSwizzle::GrSwizzle(const char c[4])
68 : fSwiz{c[0], c[1], c[2], c[3], '\0'}
69 , fKey((CToI(c[0]) << 0) | (CToI(c[1]) << 4) | (CToI(c[2]) << 8) | (CToI(c[3]) << 12)) {}
70
71constexpr GrSwizzle::GrSwizzle(const GrSwizzle& that)
72 : fSwiz{that.fSwiz[0], that.fSwiz[1], that.fSwiz[2], that.fSwiz[3], '\0'}
73 , fKey(that.fKey) {}
74
75constexpr GrSwizzle& GrSwizzle::operator=(const GrSwizzle& that) {
76 fSwiz[0] = that.fSwiz[0];
77 fSwiz[1] = that.fSwiz[1];
78 fSwiz[2] = that.fSwiz[2];
79 fSwiz[3] = that.fSwiz[3];
80 SkASSERT(fSwiz[4] == '\0');
81 fKey = that.fKey;
82 return *this;
83}
84
Brian Salomonb450f3b2019-07-09 09:36:51 -040085template <SkAlphaType AlphaType>
86constexpr SkRGBA4f<AlphaType> GrSwizzle::applyTo(const SkRGBA4f<AlphaType>& color) const {
Brian Salomon217522c2019-06-11 15:55:30 -040087 uint32_t key = fKey;
88 // Index of the input color that should be mapped to output r.
89 int idx = (key & 15);
90 float outR = ComponentIndexToFloat(color, idx);
91 key >>= 4;
92 idx = (key & 15);
93 float outG = ComponentIndexToFloat(color, idx);
94 key >>= 4;
95 idx = (key & 15);
96 float outB = ComponentIndexToFloat(color, idx);
97 key >>= 4;
98 idx = (key & 15);
99 float outA = ComponentIndexToFloat(color, idx);
100 return { outR, outG, outB, outA };
101}
102
103/** Recreates a GrSwizzle from the output of asKey() */
104constexpr void GrSwizzle::setFromKey(uint16_t key) {
105 fKey = key;
106 for (int i = 0; i < 4; ++i) {
107 fSwiz[i] = IToC(key & 15);
108 key >>= 4;
109 }
Brian Salomon24fc4eb2019-06-26 14:15:36 -0400110 SkASSERT(fSwiz[4] == '\0');
Brian Salomon217522c2019-06-11 15:55:30 -0400111}
112
Brian Salomonb450f3b2019-07-09 09:36:51 -0400113template <SkAlphaType AlphaType>
114constexpr float GrSwizzle::ComponentIndexToFloat(const SkRGBA4f<AlphaType>& color, int idx) {
Brian Salomon217522c2019-06-11 15:55:30 -0400115 if (idx <= 3) {
116 return color[idx];
117 }
Brian Salomonf30b1c12019-06-20 12:25:02 -0400118 if (idx == CToI('1')) {
Brian Salomon217522c2019-06-11 15:55:30 -0400119 return 1.0f;
120 }
Brian Salomonf30b1c12019-06-20 12:25:02 -0400121 if (idx == CToI('0')) {
122 return 1.0f;
123 }
Brian Salomon24fc4eb2019-06-26 14:15:36 -0400124 SkUNREACHABLE;
Brian Salomon217522c2019-06-11 15:55:30 -0400125}
126
127constexpr int GrSwizzle::CToI(char c) {
128 switch (c) {
Brian Salomonf30b1c12019-06-20 12:25:02 -0400129 // r...a must map to 0...3 because other methods use them as indices into fSwiz.
130 case 'r': return 0;
131 case 'g': return 1;
132 case 'b': return 2;
133 case 'a': return 3;
134 case '0': return 4;
135 case '1': return 5;
Brian Salomon24fc4eb2019-06-26 14:15:36 -0400136 default: SkUNREACHABLE;
Brian Salomon217522c2019-06-11 15:55:30 -0400137 }
138}
139
140constexpr char GrSwizzle::IToC(int idx) {
Brian Salomonf30b1c12019-06-20 12:25:02 -0400141 switch (idx) {
142 case CToI('r'): return 'r';
143 case CToI('g'): return 'g';
144 case CToI('b'): return 'b';
145 case CToI('a'): return 'a';
146 case CToI('0'): return '0';
147 case CToI('1'): return '1';
Brian Salomon24fc4eb2019-06-26 14:15:36 -0400148 default: SkUNREACHABLE;
Brian Salomon217522c2019-06-11 15:55:30 -0400149 }
150}
151
Brian Salomonf30b1c12019-06-20 12:25:02 -0400152constexpr GrSwizzle GrSwizzle::Concat(const GrSwizzle& a, const GrSwizzle& b) {
153 char swiz[4]{};
154 for (int i = 0; i < 4; ++i) {
155 int idx = (b.fKey >> (4U * i)) & 0xfU;
156 switch (idx) {
157 case CToI('0'): swiz[i] = '0'; break;
158 case CToI('1'): swiz[i] = '1'; break;
159 default: swiz[i] = a.fSwiz[idx]; break;
160 }
161 }
162 return GrSwizzle(swiz);
163}
bsalomoncdee0092016-01-08 13:20:12 -0800164#endif