SkYUVAIndex.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2018 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 SkYUVAIndex_DEFINED
  8. #define SkYUVAIndex_DEFINED
  9. #include "include/core/SkTypes.h"
  10. /** \enum SkColorChannel
  11. Describes different color channels one can manipulate
  12. */
  13. enum class SkColorChannel {
  14. kR, // the red channel
  15. kG, // the green channel
  16. kB, // the blue channel
  17. kA, // the alpha channel
  18. kLastEnum = kA,
  19. };
  20. /** \struct SkYUVAIndex
  21. Describes from which image source and which channel to read each individual YUVA plane.
  22. SkYUVAIndex contains a index for which image source to read from and a enum for which channel
  23. to read from.
  24. */
  25. struct SK_API SkYUVAIndex {
  26. bool operator==(const SkYUVAIndex& that) const {
  27. return this->fIndex == that.fIndex && this->fChannel == that.fChannel;
  28. }
  29. bool operator!=(const SkYUVAIndex& that) const {
  30. return !(*this == that);
  31. }
  32. // Index in the array of SkYUVAIndex
  33. // TODO: rename as Component
  34. enum Index {
  35. kY_Index = 0,
  36. kU_Index = 1,
  37. kV_Index = 2,
  38. kA_Index = 3,
  39. kLast_Index = kA_Index
  40. };
  41. static constexpr int kIndexCount = kLast_Index + 1;
  42. /** The index is a number between -1..3 which defines which image source to read from, where -1
  43. * means the image source doesn't exist. The assumption is we will always have image sources for
  44. * each of YUV planes, but optionally have image source for A plane. */
  45. int fIndex;
  46. /** The channel describes from which channel to read the info from. Currently we only deal with
  47. * YUV and NV12 and channel info is ignored. */
  48. SkColorChannel fChannel;
  49. static bool AreValidIndices(const SkYUVAIndex yuvaIndices[4], int* numPlanes) {
  50. // Note that 'numPlanes' is always filled in even if the indices are not valid.
  51. // This means it can always be used to process the backing resources (but be careful
  52. // of empty intervening slots).
  53. int maxSlotUsed = -1;
  54. bool used[4] = { false, false, false, false };
  55. bool valid = true;
  56. for (int i = 0; i < 4; ++i) {
  57. if (yuvaIndices[i].fIndex < 0) {
  58. if (SkYUVAIndex::kA_Index != i) {
  59. valid = false; // only the 'A' plane can be omitted
  60. }
  61. } else if (yuvaIndices[i].fIndex > 3) {
  62. valid = false; // A maximum of four input textures is allowed
  63. } else {
  64. maxSlotUsed = SkTMax(yuvaIndices[i].fIndex, maxSlotUsed);
  65. used[i] = true;
  66. }
  67. }
  68. // All the used slots should be packed starting at 0 with no gaps
  69. for (int i = 0; i <= maxSlotUsed; ++i) {
  70. if (!used[i]) {
  71. valid = false;
  72. }
  73. }
  74. *numPlanes = maxSlotUsed + 1;
  75. return valid;
  76. }
  77. };
  78. #endif