GrColor.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2010 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 GrColor_DEFINED
  8. #define GrColor_DEFINED
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkColorPriv.h"
  11. #include "include/gpu/GrTypes.h"
  12. #include "include/private/SkColorData.h"
  13. #include "include/private/SkHalf.h"
  14. /**
  15. * GrColor is 4 bytes for R, G, B, A, in a specific order defined below. Whether the color is
  16. * premultiplied or not depends on the context in which it is being used.
  17. */
  18. typedef uint32_t GrColor;
  19. // shift amount to assign a component to a GrColor int
  20. // These shift values are chosen for compatibility with GL attrib arrays
  21. // ES doesn't allow BGRA vertex attrib order so if they were not in this order
  22. // we'd have to swizzle in shaders.
  23. #ifdef SK_CPU_BENDIAN
  24. #define GrColor_SHIFT_R 24
  25. #define GrColor_SHIFT_G 16
  26. #define GrColor_SHIFT_B 8
  27. #define GrColor_SHIFT_A 0
  28. #else
  29. #define GrColor_SHIFT_R 0
  30. #define GrColor_SHIFT_G 8
  31. #define GrColor_SHIFT_B 16
  32. #define GrColor_SHIFT_A 24
  33. #endif
  34. /**
  35. * Pack 4 components (RGBA) into a GrColor int
  36. */
  37. static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a) {
  38. SkASSERT((uint8_t)r == r);
  39. SkASSERT((uint8_t)g == g);
  40. SkASSERT((uint8_t)b == b);
  41. SkASSERT((uint8_t)a == a);
  42. return (r << GrColor_SHIFT_R) |
  43. (g << GrColor_SHIFT_G) |
  44. (b << GrColor_SHIFT_B) |
  45. (a << GrColor_SHIFT_A);
  46. }
  47. // extract a component (byte) from a GrColor int
  48. #define GrColorUnpackR(color) (((color) >> GrColor_SHIFT_R) & 0xFF)
  49. #define GrColorUnpackG(color) (((color) >> GrColor_SHIFT_G) & 0xFF)
  50. #define GrColorUnpackB(color) (((color) >> GrColor_SHIFT_B) & 0xFF)
  51. #define GrColorUnpackA(color) (((color) >> GrColor_SHIFT_A) & 0xFF)
  52. /**
  53. * Since premultiplied means that alpha >= color, we construct a color with
  54. * each component==255 and alpha == 0 to be "illegal"
  55. */
  56. #define GrColor_ILLEGAL (~(0xFF << GrColor_SHIFT_A))
  57. /** Normalizes and coverts an uint8_t to a float. [0, 255] -> [0.0, 1.0] */
  58. static inline float GrNormalizeByteToFloat(uint8_t value) {
  59. static const float ONE_OVER_255 = 1.f / 255.f;
  60. return value * ONE_OVER_255;
  61. }
  62. /** Used to pick vertex attribute types. */
  63. static inline bool SkPMColor4fFitsInBytes(const SkPMColor4f& color) {
  64. // Might want to instead check that the components are [0...a] instead of [0...1]?
  65. return color.fitsInBytes();
  66. }
  67. static inline uint64_t SkPMColor4f_toFP16(const SkPMColor4f& color) {
  68. uint64_t halfColor;
  69. SkFloatToHalf_finite_ftz(Sk4f::Load(color.vec())).store(&halfColor);
  70. return halfColor;
  71. }
  72. /**
  73. * GrVertexColor is a helper for writing colors to a vertex attribute. It stores either GrColor
  74. * or four half-float channels, depending on the wideColor parameter. GrVertexWriter will write
  75. * the correct amount of data. Note that the GP needs to have been constructed with the correct
  76. * attribute type for colors, to match the usage here.
  77. */
  78. class GrVertexColor {
  79. public:
  80. explicit GrVertexColor(const SkPMColor4f& color, bool wideColor)
  81. : fWideColor(wideColor) {
  82. if (wideColor) {
  83. SkFloatToHalf_finite_ftz(Sk4f::Load(color.vec())).store(&fColor);
  84. } else {
  85. fColor[0] = color.toBytes_RGBA();
  86. }
  87. }
  88. size_t size() const { return fWideColor ? 8 : 4; }
  89. private:
  90. friend struct GrVertexWriter;
  91. uint32_t fColor[2];
  92. bool fWideColor;
  93. };
  94. #endif