GrSwizzle.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef GrSwizzle_DEFINED
  8. #define GrSwizzle_DEFINED
  9. #include "include/private/SkColorData.h"
  10. #include "src/gpu/GrColor.h"
  11. class SkRasterPipeline;
  12. /** Represents a rgba swizzle. It can be converted either into a string or a eight bit int. */
  13. class GrSwizzle {
  14. public:
  15. constexpr GrSwizzle() : GrSwizzle("rgba") {}
  16. explicit constexpr GrSwizzle(const char c[4]);
  17. constexpr GrSwizzle(const GrSwizzle&);
  18. constexpr GrSwizzle& operator=(const GrSwizzle& that);
  19. static constexpr GrSwizzle Concat(const GrSwizzle& a, const GrSwizzle& b);
  20. /** Recreates a GrSwizzle from the output of asKey() */
  21. constexpr void setFromKey(uint16_t key);
  22. constexpr bool operator==(const GrSwizzle& that) const { return fKey == that.fKey; }
  23. constexpr bool operator!=(const GrSwizzle& that) const { return !(*this == that); }
  24. /** Compact representation of the swizzle suitable for a key. */
  25. constexpr uint16_t asKey() const { return fKey; }
  26. /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a', '0', and '1'. */
  27. constexpr const char* c_str() const { return fSwiz; }
  28. constexpr char operator[](int i) const {
  29. SkASSERT(i >= 0 && i < 4);
  30. return fSwiz[i];
  31. }
  32. /** Applies this swizzle to the input color and returns the swizzled color. */
  33. template <SkAlphaType AlphaType>
  34. constexpr SkRGBA4f<AlphaType> applyTo(const SkRGBA4f<AlphaType>& color) const;
  35. void apply(SkRasterPipeline*) const;
  36. static constexpr GrSwizzle RGBA() { return GrSwizzle("rgba"); }
  37. static constexpr GrSwizzle AAAA() { return GrSwizzle("aaaa"); }
  38. static constexpr GrSwizzle RRRR() { return GrSwizzle("rrrr"); }
  39. static constexpr GrSwizzle RRRA() { return GrSwizzle("rrra"); }
  40. static constexpr GrSwizzle BGRA() { return GrSwizzle("bgra"); }
  41. static constexpr GrSwizzle RGB1() { return GrSwizzle("rgb1"); }
  42. private:
  43. template <SkAlphaType AlphaType>
  44. static constexpr float ComponentIndexToFloat(const SkRGBA4f<AlphaType>& color, int idx);
  45. static constexpr int CToI(char c);
  46. static constexpr char IToC(int idx);
  47. char fSwiz[5];
  48. uint16_t fKey;
  49. };
  50. constexpr GrSwizzle::GrSwizzle(const char c[4])
  51. : fSwiz{c[0], c[1], c[2], c[3], '\0'}
  52. , fKey((CToI(c[0]) << 0) | (CToI(c[1]) << 4) | (CToI(c[2]) << 8) | (CToI(c[3]) << 12)) {}
  53. constexpr GrSwizzle::GrSwizzle(const GrSwizzle& that)
  54. : fSwiz{that.fSwiz[0], that.fSwiz[1], that.fSwiz[2], that.fSwiz[3], '\0'}
  55. , fKey(that.fKey) {}
  56. constexpr GrSwizzle& GrSwizzle::operator=(const GrSwizzle& that) {
  57. fSwiz[0] = that.fSwiz[0];
  58. fSwiz[1] = that.fSwiz[1];
  59. fSwiz[2] = that.fSwiz[2];
  60. fSwiz[3] = that.fSwiz[3];
  61. SkASSERT(fSwiz[4] == '\0');
  62. fKey = that.fKey;
  63. return *this;
  64. }
  65. template <SkAlphaType AlphaType>
  66. constexpr SkRGBA4f<AlphaType> GrSwizzle::applyTo(const SkRGBA4f<AlphaType>& color) const {
  67. uint32_t key = fKey;
  68. // Index of the input color that should be mapped to output r.
  69. int idx = (key & 15);
  70. float outR = ComponentIndexToFloat(color, idx);
  71. key >>= 4;
  72. idx = (key & 15);
  73. float outG = ComponentIndexToFloat(color, idx);
  74. key >>= 4;
  75. idx = (key & 15);
  76. float outB = ComponentIndexToFloat(color, idx);
  77. key >>= 4;
  78. idx = (key & 15);
  79. float outA = ComponentIndexToFloat(color, idx);
  80. return { outR, outG, outB, outA };
  81. }
  82. /** Recreates a GrSwizzle from the output of asKey() */
  83. constexpr void GrSwizzle::setFromKey(uint16_t key) {
  84. fKey = key;
  85. for (int i = 0; i < 4; ++i) {
  86. fSwiz[i] = IToC(key & 15);
  87. key >>= 4;
  88. }
  89. SkASSERT(fSwiz[4] == '\0');
  90. }
  91. template <SkAlphaType AlphaType>
  92. constexpr float GrSwizzle::ComponentIndexToFloat(const SkRGBA4f<AlphaType>& color, int idx) {
  93. if (idx <= 3) {
  94. return color[idx];
  95. }
  96. if (idx == CToI('1')) {
  97. return 1.0f;
  98. }
  99. if (idx == CToI('0')) {
  100. return 1.0f;
  101. }
  102. SkUNREACHABLE;
  103. }
  104. constexpr int GrSwizzle::CToI(char c) {
  105. switch (c) {
  106. // r...a must map to 0...3 because other methods use them as indices into fSwiz.
  107. case 'r': return 0;
  108. case 'g': return 1;
  109. case 'b': return 2;
  110. case 'a': return 3;
  111. case '0': return 4;
  112. case '1': return 5;
  113. default: SkUNREACHABLE;
  114. }
  115. }
  116. constexpr char GrSwizzle::IToC(int idx) {
  117. switch (idx) {
  118. case CToI('r'): return 'r';
  119. case CToI('g'): return 'g';
  120. case CToI('b'): return 'b';
  121. case CToI('a'): return 'a';
  122. case CToI('0'): return '0';
  123. case CToI('1'): return '1';
  124. default: SkUNREACHABLE;
  125. }
  126. }
  127. constexpr GrSwizzle GrSwizzle::Concat(const GrSwizzle& a, const GrSwizzle& b) {
  128. char swiz[4]{};
  129. for (int i = 0; i < 4; ++i) {
  130. int idx = (b.fKey >> (4U * i)) & 0xfU;
  131. switch (idx) {
  132. case CToI('0'): swiz[i] = '0'; break;
  133. case CToI('1'): swiz[i] = '1'; break;
  134. default: swiz[i] = a.fSwiz[idx]; break;
  135. }
  136. }
  137. return GrSwizzle(swiz);
  138. }
  139. #endif