GrColor.h 3.3 KB

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