Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(443)

Side by Side Diff: src/gpu/GrSwizzle.h

Issue 1576023002: Make readback of alpha channel work for RGBA. (Closed) Base URL: https://skia.googlesource.com/skia.git@swiz
Patch Set: more Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/gpu/GrProgramDesc.h ('k') | src/gpu/gl/GrGLCaps.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef GrSwizzle_DEFINED 8 #ifndef GrSwizzle_DEFINED
9 #define GrSwizzle_DEFINED 9 #define GrSwizzle_DEFINED
10 10
11 #include "GrTypes.h" 11 #include "GrTypes.h"
12 #include "GrColor.h"
12 13
13 /** Represents a rgba swizzle. It can be converted either into a string or a eig ht bit int. 14 /** Represents a rgba swizzle. It can be converted either into a string or a eig ht bit int.
14 Currently there is no way to specify an arbitrary swizzle, just some static swizzles and an 15 Currently there is no way to specify an arbitrary swizzle, just some static swizzles and an
15 assignment operator. That could be relaxed. */ 16 assignment operator. That could be relaxed. */
16 class GrSwizzle { 17 class GrSwizzle {
17 public: 18 public:
18 GrSwizzle() { *this = RGBA(); } 19 GrSwizzle() { *this = RGBA(); }
19 20
21 GrSwizzle(const GrSwizzle& that) { *this = that; }
22
23 void setFromKey(uint8_t key) {
24 fKey = key;
25 for (int i = 0; i < 4; ++i) {
26 fSwiz[i] = IdxToChar(key & 3);
27 key >>= 2;
28 }
29 SkASSERT(fSwiz[4] == 0);
30 }
31
20 GrSwizzle& operator=(const GrSwizzle& that) { 32 GrSwizzle& operator=(const GrSwizzle& that) {
21 memcpy(this, &that, sizeof(GrSwizzle)); 33 memcpy(this, &that, sizeof(GrSwizzle));
22 return *this; 34 return *this;
23 } 35 }
24 36
25 bool operator==(const GrSwizzle& that) const { return this->asUInt() == that .asUInt(); } 37 bool operator==(const GrSwizzle& that) const { return this->asUInt() == that .asUInt(); }
26 38
27 bool operator!=(const GrSwizzle& that) const { return !(*this == that); } 39 bool operator!=(const GrSwizzle& that) const { return !(*this == that); }
28 40
29 /** Compact representation of the swizzle suitable for a key. */ 41 /** Compact representation of the swizzle suitable for a key. */
30 uint8_t asKey() const { return fKey; } 42 uint8_t asKey() const { return fKey; }
31 43
32 /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a '. */ 44 /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a '. */
33 const char* c_str() const { return fSwiz; } 45 const char* c_str() const { return fSwiz; }
34 46
47 GrColor applyTo(GrColor color) const {
48 int idx;
49 uint32_t key = fKey;
50
51 // Index of the input color that should be mapped to output r.
52 idx = (key & 3);
53 uint32_t outR = (color >> idx * 8) & 0xFF;
54 key >>= 2;
55
56 idx = (key & 3);
57 uint32_t outG = (color >> idx * 8) & 0xFF;
58 key >>= 2;
59
60 idx = (key & 3);
61 uint32_t outB = (color >> idx * 8) & 0xFF;
62 key >>= 2;
63
64 idx = (key & 3);
65 uint32_t outA = (color >> idx * 8) & 0xFF;
66
67 return GrColorPackRGBA(outR, outG, outB, outA);
68 }
69
35 static const GrSwizzle& RGBA() { 70 static const GrSwizzle& RGBA() {
36 static GrSwizzle gRGBA("rgba"); 71 static GrSwizzle gRGBA("rgba");
37 return gRGBA; 72 return gRGBA;
38 } 73 }
39 74
40 static const GrSwizzle& AAAA() { 75 static const GrSwizzle& AAAA() {
41 static GrSwizzle gAAAA("aaaa"); 76 static GrSwizzle gAAAA("aaaa");
42 return gAAAA; 77 return gAAAA;
43 } 78 }
44 79
45 static const GrSwizzle& RRRR() { 80 static const GrSwizzle& RRRR() {
46 static GrSwizzle gRRRR("rrrr"); 81 static GrSwizzle gRRRR("rrrr");
47 return gRRRR; 82 return gRRRR;
48 } 83 }
49 84
50 static const GrSwizzle& BGRA() { 85 static const GrSwizzle& BGRA() {
51 static GrSwizzle gBGRA("bgra"); 86 static GrSwizzle gBGRA("bgra");
52 return gBGRA; 87 return gBGRA;
53 } 88 }
54 89
55 private: 90 private:
56 char fSwiz[5]; 91 char fSwiz[5];
57 uint8_t fKey; 92 uint8_t fKey;
58 93
59 static int CharToIdx(char c) { 94 static int CharToIdx(char c) {
60 switch (c) { 95 switch (c) {
61 case 'r': 96 case 'r':
62 return 0; 97 return (GrColor_SHIFT_R / 8);
63 case 'g': 98 case 'g':
64 return 1; 99 return (GrColor_SHIFT_G / 8);
65 case 'b': 100 case 'b':
66 return 2; 101 return (GrColor_SHIFT_B/ 8);
67 case 'a': 102 case 'a':
68 return 3; 103 return (GrColor_SHIFT_A / 8);
69 default: 104 default:
70 SkFAIL("Invalid swizzle char"); 105 SkFAIL("Invalid swizzle char");
71 return 0; 106 return 0;
72 } 107 }
73 } 108 }
74 109
110 static /* constexpr */ char IToC(int idx) {
111 return (8*idx) == GrColor_SHIFT_R ? 'r' :
112 (8*idx) == GrColor_SHIFT_G ? 'g' :
113 (8*idx) == GrColor_SHIFT_B ? 'b' :
114 'a';
115 }
116
117 static char IdxToChar(int c) {
118 // Hopefully this array gets computed at compile time.
119 static const char gStr[4] = { IToC(0), IToC(1), IToC(2), IToC(3) };
120 return gStr[c];
121 }
122
75 explicit GrSwizzle(const char* str) { 123 explicit GrSwizzle(const char* str) {
76 SkASSERT(strlen(str) == 4); 124 SkASSERT(strlen(str) == 4);
77 fSwiz[0] = str[0]; 125 fSwiz[0] = str[0];
78 fSwiz[1] = str[1]; 126 fSwiz[1] = str[1];
79 fSwiz[2] = str[2]; 127 fSwiz[2] = str[2];
80 fSwiz[3] = str[3]; 128 fSwiz[3] = str[3];
81 fSwiz[4] = 0; 129 fSwiz[4] = 0;
82 fKey = SkToU8(CharToIdx(fSwiz[0]) | (CharToIdx(fSwiz[1]) << 2) | 130 fKey = SkToU8(CharToIdx(fSwiz[0]) | (CharToIdx(fSwiz[1]) << 2) |
83 (CharToIdx(fSwiz[2]) << 4) | (CharToIdx(fSwiz[3]) << 6)); 131 (CharToIdx(fSwiz[2]) << 4) | (CharToIdx(fSwiz[3]) << 6));
84 } 132 }
85 133
86 uint32_t* asUIntPtr() { return SkTCast<uint32_t*>(fSwiz); }
87 uint32_t asUInt() const { return *SkTCast<const uint32_t*>(fSwiz); } 134 uint32_t asUInt() const { return *SkTCast<const uint32_t*>(fSwiz); }
88 135
89 GR_STATIC_ASSERT(sizeof(char[4]) == sizeof(uint32_t)); 136 GR_STATIC_ASSERT(sizeof(char[4]) == sizeof(uint32_t));
90 }; 137 };
91 138
92 #endif 139 #endif
OLDNEW
« no previous file with comments | « src/gpu/GrProgramDesc.h ('k') | src/gpu/gl/GrGLCaps.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698