File: gl_scaler_test_util.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (177 lines) | stat: -rw-r--r-- 7,129 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_VIZ_TEST_GL_SCALER_TEST_UTIL_H_
#define COMPONENTS_VIZ_TEST_GL_SCALER_TEST_UTIL_H_

#include <stdint.h>

#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkRect.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/color_transform.h"
#include "ui/gfx/geometry/size.h"

namespace gfx {
class Rect;
}

namespace viz {

// A collection of utility functions used in pixel tests.
class GLScalerTestUtil {
 public:
  struct ColorBar {
    SkIRect rect;
    SkColor4f color;
  };

  // The patterns that can be created by CreateCyclicalTestImage().
  enum CyclicalPattern {
    HORIZONTAL_STRIPES,
    VERTICAL_STRIPES,
    STAGGERED,
  };

  // Returns an SkBitmap with pixels allocated, having a RGBA format with
  // unpremultiplied alpha.
  static SkBitmap AllocateRGBABitmap(const gfx::Size& size);

  // Returns a |value| in the range [0.0,1.0] as an unsigned integer in the
  // range [0,255].
  static uint8_t ToClamped255(float value);

  // Return the SMPTE color bars that make up a test image, scaled to an image
  // of the given |size|.
  static std::vector<ColorBar> GetScaledSMPTEColorBars(const gfx::Size& size);

  // Create a SMPTE color bar test image, scaled to the given |size|.
  static SkBitmap CreateSMPTETestImage(const gfx::Size& size);

  // Returns true if the given |image| looks similar-enough to a scaled version
  // of a SMPTE color bar test image that was originally of |src_size| and
  // cropped to |src_rect|. |fuzzy_pixels| is used to ignore a border of pixels
  // surrounding each color bar, since the scaling algorithms may blend
  // in-between colors where the bars touch. |max_color_diff| controls what
  // "similar-enough" means: 0 for "exact," or otherwise some positive value
  // specifying the maximum color value difference; and is updated with the
  // the actual maximum.
  static bool LooksLikeSMPTETestImage(const SkBitmap& image,
                                      const gfx::Size& src_size,
                                      const gfx::Rect& src_rect,
                                      int fuzzy_pixels,
                                      float* max_color_diff);

  // Returns an image of the given |size| with the colors in |cycle| used to
  // generate a striped or staggered |pattern|. |rotation| specifies which index
  // in the |cycle| to start with.
  static SkBitmap CreateCyclicalTestImage(const gfx::Size& size,
                                          CyclicalPattern pattern,
                                          const std::vector<SkColor4f>& cycle,
                                          size_t rotation);

  // Returns the RGB/YUV color spaces used by default when color space
  // conversion is requested.
  static gfx::ColorSpace DefaultRGBColorSpace();
  static gfx::ColorSpace DefaultYUVColorSpace();

  // Performs an in-place transform of the given |image| from
  // DefaultRGBColorSpace() to DefaultYUVColorSpace(). The color channels (plus
  // one alpha) remain interleaved (i.e., no pixel blending or format transform
  // is being done).
  static void ConvertRGBABitmapToYUV(SkBitmap* image);

  static SkBitmap CopyAndConvertToRGBA(const SkBitmap& bitmap);

  // Performs an in-place swizzling of the red and blue color channels in the
  // given |image|.
  static void SwizzleBitmap(SkBitmap* image);

  // Returns a bitmap consisting of one color channel from every 4 pixels in a
  // |source| bitmap packed into a single quad. Thus, the resulting bitmap will
  // have 1/4 the width of the source bitmap. This is used to create the
  // expected output of the single-channel export shaders, and thus can be
  // considered a reference implementation of that.
  static SkBitmap CreatePackedPlanarBitmap(const SkBitmap& source, int channel);

  // Performs the inverse operation to CreatePackedPlanarBitmap(). This takes
  // all of the data in |plane| and uses it to populate a single color channel
  // of all the pixels of |out|. The |plane| can be a full-size or half-size
  // (subsampled) plane. The channels other than the one specified via |channel|
  // will be left unmodified.
  static void UnpackPlanarBitmap(const SkBitmap& plane,
                                 int channel,
                                 SkBitmap* out);

  // This takes all of the data in |plane| and uses it to populate a green and
  // blue color channels of all the pixels of |out|. The |plane| can be a
  // full-size or half-size (subsampled) plane. This leaves the R and A channels
  // of |out| unmodified.
  static void UnpackUVBitmap(const SkBitmap& plane, SkBitmap* out);

  // Returns the |source| bitmap, but with its content vertically flipped.
  static SkBitmap CreateVerticallyFlippedBitmap(const SkBitmap& source);

  // The area and color of the bars in a 1920x1080 HD SMPTE color bars test
  // image (https://commons.wikimedia.org/wiki/File:SMPTE_Color_Bars_16x9.svg).
  // The gray linear gradient bar is defined as half solid 0-level black and
  // half solid full-intensity white).
  static const ColorBar kSMPTEColorBars[30];
  static constexpr gfx::Size kSMPTEFullSize = gfx::Size(1920, 1080);

#ifdef SK_CPU_BENDIAN
  // Bit shift offsets (within a uint32_t RGBA quad) to access each color
  // channel's byte.
  static constexpr int kRedShift = 24;
  static constexpr int kGreenShift = 16;
  static constexpr int kBlueShift = 8;
  static constexpr int kAlphaShift = 0;
#else
  static constexpr int kRedShift = 0;
  static constexpr int kGreenShift = 8;
  static constexpr int kBlueShift = 16;
  static constexpr int kAlphaShift = 24;
#endif
};

// A helper for tests to create textures, and download/upload RGBA textures
// to/from SkBitmaps. All textures created by this helper will be deleted when
// its destructor is invoked.
class GLScalerTestTextureHelper {
 public:
  // |gl| context must outlive this instance.
  explicit GLScalerTestTextureHelper(gpu::gles2::GLES2Interface* gl);

  GLScalerTestTextureHelper(const GLScalerTestTextureHelper& other) = delete;
  GLScalerTestTextureHelper& operator=(const GLScalerTestTextureHelper& other) =
      delete;

  ~GLScalerTestTextureHelper();

  // Creates a fully-defined RGBA texture of the given |size|, returning its GL
  // name.
  GLuint CreateTexture(const gfx::Size& size);

  // Uploads the given RGBA |bitmap| to the GPU into a new texture, returning
  // its GL name.
  GLuint UploadTexture(const SkBitmap& bitmap);

  // Reads-back the |texture| which is of the given |size|, returning the result
  // as a RGBA SkBitmap.
  SkBitmap DownloadTexture(GLuint texture, const gfx::Size& size);

 private:
  const raw_ptr<gpu::gles2::GLES2Interface> gl_;
  std::vector<GLuint> textures_to_delete_;
};

}  // namespace viz

#endif  // COMPONENTS_VIZ_TEST_GL_SCALER_TEST_UTIL_H_