blob: da5d3e0f16d674a9428a6633003e6f26a1c39456 [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));
Mike Kleine72e7732018-06-14 14:41:22 -040041 uint32_t* asUIntPtr() { return reinterpret_cast<uint32_t*>(fSwiz); }
42 uint32_t asUInt() const {
43 uint32_t v;
44 memcpy(&v, fSwiz, 4);
45 return v;
46 }
Brian Salomon1aaccc72016-11-18 15:08:06 -050047
bsalomoncdee0092016-01-08 13:20:12 -080048public:
49 GrSwizzle() { *this = RGBA(); }
50
bsalomon7f9b2e42016-01-12 13:29:26 -080051 GrSwizzle(const GrSwizzle& that) { *this = that; }
52
bsalomoncdee0092016-01-08 13:20:12 -080053 GrSwizzle& operator=(const GrSwizzle& that) {
54 memcpy(this, &that, sizeof(GrSwizzle));
55 return *this;
56 }
57
bsalomon7f9b2e42016-01-12 13:29:26 -080058 /** Recreates a GrSwizzle from the output of asKey() */
59 void setFromKey(uint8_t key) {
60 fKey = key;
61 for (int i = 0; i < 4; ++i) {
Brian Salomon1aaccc72016-11-18 15:08:06 -050062 fSwiz[i] = IToC(key & 3);
bsalomon7f9b2e42016-01-12 13:29:26 -080063 key >>= 2;
64 }
65 SkASSERT(fSwiz[4] == 0);
66 }
67
bsalomoncdee0092016-01-08 13:20:12 -080068 bool operator==(const GrSwizzle& that) const { return this->asUInt() == that.asUInt(); }
69
70 bool operator!=(const GrSwizzle& that) const { return !(*this == that); }
71
72 /** Compact representation of the swizzle suitable for a key. */
73 uint8_t asKey() const { return fKey; }
74
75 /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a'. */
76 const char* c_str() const { return fSwiz; }
77
bsalomon7f9b2e42016-01-12 13:29:26 -080078 /** Applies this swizzle to the input color and returns the swizzled color. */
79 GrColor applyTo(GrColor color) const {
80 int idx;
81 uint32_t key = fKey;
82 // Index of the input color that should be mapped to output r.
83 idx = (key & 3);
84 uint32_t outR = (color >> idx * 8) & 0xFF;
85 key >>= 2;
86 idx = (key & 3);
87 uint32_t outG = (color >> idx * 8) & 0xFF;
88 key >>= 2;
89 idx = (key & 3);
90 uint32_t outB = (color >> idx * 8) & 0xFF;
91 key >>= 2;
92 idx = (key & 3);
93 uint32_t outA = (color >> idx * 8) & 0xFF;
94 return GrColorPackRGBA(outR, outG, outB, outA);
95 }
96
Brian Osmance425512017-03-22 14:37:50 -040097 /** Applies this swizzle to the input color and returns the swizzled color. */
98 GrColor4f applyTo(const GrColor4f& color) const {
99 int idx;
100 uint32_t key = fKey;
101 // Index of the input color that should be mapped to output r.
102 idx = (key & 3);
103 float outR = color.fRGBA[idx];
104 key >>= 2;
105 idx = (key & 3);
106 float outG = color.fRGBA[idx];
107 key >>= 2;
108 idx = (key & 3);
109 float outB = color.fRGBA[idx];
110 key >>= 2;
111 idx = (key & 3);
112 float outA = color.fRGBA[idx];
113 return GrColor4f(outR, outG, outB, outA);
114 }
115
Brian Salomon1aaccc72016-11-18 15:08:06 -0500116 static GrSwizzle RGBA() { return GrSwizzle("rgba"); }
117 static GrSwizzle AAAA() { return GrSwizzle("aaaa"); }
118 static GrSwizzle RRRR() { return GrSwizzle("rrrr"); }
Brian Osman986563b2017-01-10 14:20:02 -0500119 static GrSwizzle RRRA() { return GrSwizzle("rrra"); }
Brian Salomon1aaccc72016-11-18 15:08:06 -0500120 static GrSwizzle BGRA() { return GrSwizzle("bgra"); }
bsalomoncdee0092016-01-08 13:20:12 -0800121
Brian Salomon1aaccc72016-11-18 15:08:06 -0500122 static GrSwizzle CreateRandom(SkRandom* random) {
bsalomon6c9cd552016-01-22 07:17:34 -0800123 switch (random->nextU() % 4) {
124 case 0:
125 return RGBA();
126 case 1:
127 return BGRA();
128 case 2:
129 return RRRR();
130 case 3:
131 return AAAA();
132 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400133 SK_ABORT("Mod is broken?!?");
bsalomon6c9cd552016-01-22 07:17:34 -0800134 return RGBA();
135 }
136 }
bsalomoncdee0092016-01-08 13:20:12 -0800137};
138
139#endif