gl_scaler_test_util.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_VIZ_TEST_GL_SCALER_TEST_UTIL_H_
  5. #define COMPONENTS_VIZ_TEST_GL_SCALER_TEST_UTIL_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "gpu/command_buffer/client/gles2_interface.h"
  11. #include "third_party/skia/include/core/SkBitmap.h"
  12. #include "third_party/skia/include/core/SkColor.h"
  13. #include "third_party/skia/include/core/SkRect.h"
  14. #include "ui/gfx/color_space.h"
  15. #include "ui/gfx/color_transform.h"
  16. #include "ui/gfx/geometry/size.h"
  17. namespace gfx {
  18. class Rect;
  19. }
  20. namespace viz {
  21. // A collection of utility functions used in pixel tests.
  22. class GLScalerTestUtil {
  23. public:
  24. struct ColorBar {
  25. SkIRect rect;
  26. SkColor4f color;
  27. };
  28. // The patterns that can be created by CreateCyclicalTestImage().
  29. enum CyclicalPattern {
  30. HORIZONTAL_STRIPES,
  31. VERTICAL_STRIPES,
  32. STAGGERED,
  33. };
  34. // Returns an SkBitmap with pixels allocated, having a RGBA format with
  35. // unpremultiplied alpha.
  36. static SkBitmap AllocateRGBABitmap(const gfx::Size& size);
  37. // Returns a |value| in the range [0.0,1.0] as an unsigned integer in the
  38. // range [0,255].
  39. static uint8_t ToClamped255(float value);
  40. // Return the SMPTE color bars that make up a test image, scaled to an image
  41. // of the given |size|.
  42. static std::vector<ColorBar> GetScaledSMPTEColorBars(const gfx::Size& size);
  43. // Create a SMPTE color bar test image, scaled to the given |size|.
  44. static SkBitmap CreateSMPTETestImage(const gfx::Size& size);
  45. // Returns true if the given |image| looks similar-enough to a scaled version
  46. // of a SMPTE color bar test image that was originally of |src_size| and
  47. // cropped to |src_rect|. |fuzzy_pixels| is used to ignore a border of pixels
  48. // surrounding each color bar, since the scaling algorithms may blend
  49. // in-between colors where the bars touch. |max_color_diff| controls what
  50. // "similar-enough" means: 0 for "exact," or otherwise some positive value
  51. // specifying the maximum color value difference; and is updated with the
  52. // the actual maximum.
  53. static bool LooksLikeSMPTETestImage(const SkBitmap& image,
  54. const gfx::Size& src_size,
  55. const gfx::Rect& src_rect,
  56. int fuzzy_pixels,
  57. float* max_color_diff);
  58. // Returns an image of the given |size| with the colors in |cycle| used to
  59. // generate a striped or staggered |pattern|. |rotation| specifies which index
  60. // in the |cycle| to start with.
  61. static SkBitmap CreateCyclicalTestImage(const gfx::Size& size,
  62. CyclicalPattern pattern,
  63. const std::vector<SkColor4f>& cycle,
  64. size_t rotation);
  65. // Returns the RGB/YUV color spaces used by default when color space
  66. // conversion is requested.
  67. static gfx::ColorSpace DefaultRGBColorSpace();
  68. static gfx::ColorSpace DefaultYUVColorSpace();
  69. // Performs an in-place transform of the given |image| from
  70. // DefaultRGBColorSpace() to DefaultYUVColorSpace(). The color channels (plus
  71. // one alpha) remain interleaved (i.e., no pixel blending or format transform
  72. // is being done).
  73. static void ConvertRGBABitmapToYUV(SkBitmap* image);
  74. static SkBitmap CopyAndConvertToRGBA(const SkBitmap& bitmap);
  75. // Performs an in-place swizzling of the red and blue color channels in the
  76. // given |image|.
  77. static void SwizzleBitmap(SkBitmap* image);
  78. // Returns a bitmap consisting of one color channel from every 4 pixels in a
  79. // |source| bitmap packed into a single quad. Thus, the resulting bitmap will
  80. // have 1/4 the width of the source bitmap. This is used to create the
  81. // expected output of the single-channel export shaders, and thus can be
  82. // considered a reference implementation of that.
  83. static SkBitmap CreatePackedPlanarBitmap(const SkBitmap& source, int channel);
  84. // Performs the inverse operation to CreatePackedPlanarBitmap(). This takes
  85. // all of the data in |plane| and uses it to populate a single color channel
  86. // of all the pixels of |out|. The |plane| can be a full-size or half-size
  87. // (subsampled) plane. The channels other than the one specified via |channel|
  88. // will be left unmodified.
  89. static void UnpackPlanarBitmap(const SkBitmap& plane,
  90. int channel,
  91. SkBitmap* out);
  92. // This takes all of the data in |plane| and uses it to populate a green and
  93. // blue color channels of all the pixels of |out|. The |plane| can be a
  94. // full-size or half-size (subsampled) plane. This leaves the R and A channels
  95. // of |out| unmodified.
  96. static void UnpackUVBitmap(const SkBitmap& plane, SkBitmap* out);
  97. // Returns the |source| bitmap, but with its content vertically flipped.
  98. static SkBitmap CreateVerticallyFlippedBitmap(const SkBitmap& source);
  99. // The area and color of the bars in a 1920x1080 HD SMPTE color bars test
  100. // image (https://commons.wikimedia.org/wiki/File:SMPTE_Color_Bars_16x9.svg).
  101. // The gray linear gradient bar is defined as half solid 0-level black and
  102. // half solid full-intensity white).
  103. static const ColorBar kSMPTEColorBars[30];
  104. static constexpr gfx::Size kSMPTEFullSize = gfx::Size(1920, 1080);
  105. #ifdef SK_CPU_BENDIAN
  106. // Bit shift offsets (within a uint32_t RGBA quad) to access each color
  107. // channel's byte.
  108. static constexpr int kRedShift = 24;
  109. static constexpr int kGreenShift = 16;
  110. static constexpr int kBlueShift = 8;
  111. static constexpr int kAlphaShift = 0;
  112. #else
  113. static constexpr int kRedShift = 0;
  114. static constexpr int kGreenShift = 8;
  115. static constexpr int kBlueShift = 16;
  116. static constexpr int kAlphaShift = 24;
  117. #endif
  118. };
  119. // A helper for tests to create textures, and download/upload RGBA textures
  120. // to/from SkBitmaps. All textures created by this helper will be deleted when
  121. // its destructor is invoked.
  122. class GLScalerTestTextureHelper {
  123. public:
  124. // |gl| context must outlive this instance.
  125. explicit GLScalerTestTextureHelper(gpu::gles2::GLES2Interface* gl);
  126. GLScalerTestTextureHelper(const GLScalerTestTextureHelper& other) = delete;
  127. GLScalerTestTextureHelper& operator=(const GLScalerTestTextureHelper& other) =
  128. delete;
  129. ~GLScalerTestTextureHelper();
  130. // Creates a fully-defined RGBA texture of the given |size|, returning its GL
  131. // name.
  132. GLuint CreateTexture(const gfx::Size& size);
  133. // Uploads the given RGBA |bitmap| to the GPU into a new texture, returning
  134. // its GL name.
  135. GLuint UploadTexture(const SkBitmap& bitmap);
  136. // Reads-back the |texture| which is of the given |size|, returning the result
  137. // as a RGBA SkBitmap.
  138. SkBitmap DownloadTexture(GLuint texture, const gfx::Size& size);
  139. private:
  140. const raw_ptr<gpu::gles2::GLES2Interface> gl_;
  141. std::vector<GLuint> textures_to_delete_;
  142. };
  143. } // namespace viz
  144. #endif // COMPONENTS_VIZ_TEST_GL_SCALER_TEST_UTIL_H_