blob: 4acf7e44c724561f3845ecf4ad8a07e4f2ab5c44 [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 Salomon1aaccc72016-11-18 15:08:06 -050093 static GrSwizzle RGBA() { return GrSwizzle("rgba"); }
94 static GrSwizzle AAAA() { return GrSwizzle("aaaa"); }
95 static GrSwizzle RRRR() { return GrSwizzle("rrrr"); }
Brian Osman986563b2017-01-10 14:20:02 -050096 static GrSwizzle RRRA() { return GrSwizzle("rrra"); }
Brian Salomon1aaccc72016-11-18 15:08:06 -050097 static GrSwizzle BGRA() { return GrSwizzle("bgra"); }
bsalomoncdee0092016-01-08 13:20:12 -080098
Brian Salomon1aaccc72016-11-18 15:08:06 -050099 static GrSwizzle CreateRandom(SkRandom* random) {
bsalomon6c9cd552016-01-22 07:17:34 -0800100 switch (random->nextU() % 4) {
101 case 0:
102 return RGBA();
103 case 1:
104 return BGRA();
105 case 2:
106 return RRRR();
107 case 3:
108 return AAAA();
109 default:
110 SkFAIL("Mod is broken?!?");
111 return RGBA();
112 }
113 }
bsalomoncdee0092016-01-08 13:20:12 -0800114};
115
116#endif