OLD | NEW |
(Empty) | |
| 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 "GrTypes.h" |
| 12 |
| 13 /** 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 assignment operator. That could be relaxed. */ |
| 16 class GrSwizzle { |
| 17 public: |
| 18 GrSwizzle() { *this = RGBA(); } |
| 19 |
| 20 GrSwizzle& operator=(const GrSwizzle& that) { |
| 21 memcpy(this, &that, sizeof(GrSwizzle)); |
| 22 return *this; |
| 23 } |
| 24 |
| 25 bool operator==(const GrSwizzle& that) const { return this->asUInt() == that
.asUInt(); } |
| 26 |
| 27 bool operator!=(const GrSwizzle& that) const { return !(*this == that); } |
| 28 |
| 29 /** Compact representation of the swizzle suitable for a key. */ |
| 30 uint8_t asKey() const { return fKey; } |
| 31 |
| 32 /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a
'. */ |
| 33 const char* c_str() const { return fSwiz; } |
| 34 |
| 35 static const GrSwizzle& RGBA() { |
| 36 static GrSwizzle gRGBA("rgba"); |
| 37 return gRGBA; |
| 38 } |
| 39 |
| 40 static const GrSwizzle& AAAA() { |
| 41 static GrSwizzle gAAAA("aaaa"); |
| 42 return gAAAA; |
| 43 } |
| 44 |
| 45 static const GrSwizzle& RRRR() { |
| 46 static GrSwizzle gRRRR("rrrr"); |
| 47 return gRRRR; |
| 48 } |
| 49 |
| 50 static const GrSwizzle& BGRA() { |
| 51 static GrSwizzle gBGRA("bgra"); |
| 52 return gBGRA; |
| 53 } |
| 54 |
| 55 private: |
| 56 char fSwiz[5]; |
| 57 uint8_t fKey; |
| 58 |
| 59 static int CharToIdx(char c) { |
| 60 switch (c) { |
| 61 case 'r': |
| 62 return 0; |
| 63 case 'g': |
| 64 return 1; |
| 65 case 'b': |
| 66 return 2; |
| 67 case 'a': |
| 68 return 3; |
| 69 default: |
| 70 SkFAIL("Invalid swizzle char"); |
| 71 return 0; |
| 72 } |
| 73 } |
| 74 |
| 75 explicit GrSwizzle(const char* str) { |
| 76 SkASSERT(strlen(str) == 4); |
| 77 fSwiz[0] = str[0]; |
| 78 fSwiz[1] = str[1]; |
| 79 fSwiz[2] = str[2]; |
| 80 fSwiz[3] = str[3]; |
| 81 fSwiz[4] = 0; |
| 82 fKey = SkToU8(CharToIdx(fSwiz[0]) | (CharToIdx(fSwiz[1]) << 2) | |
| 83 (CharToIdx(fSwiz[2]) << 4) | (CharToIdx(fSwiz[3]) << 6)); |
| 84 } |
| 85 |
| 86 uint32_t* asUIntPtr() { return SkTCast<uint32_t*>(fSwiz); } |
| 87 uint32_t asUInt() const { return *SkTCast<const uint32_t*>(fSwiz); } |
| 88 |
| 89 GR_STATIC_ASSERT(sizeof(char[4]) == sizeof(uint32_t)); |
| 90 }; |
| 91 |
| 92 #endif |
OLD | NEW |