Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Typedefs | Functions | Variables
SRGBReadWritePixelsTest.cpp File Reference
#include "include/core/SkAlphaType.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkColorType.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GpuTypes.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrTypes.h"
#include "include/private/gpu/ganesh/GrTypesPriv.h"
#include "src/gpu/SkBackingFit.h"
#include "src/gpu/ganesh/GrCaps.h"
#include "src/gpu/ganesh/GrDirectContextPriv.h"
#include "src/gpu/ganesh/GrImageInfo.h"
#include "src/gpu/ganesh/GrPixmap.h"
#include "src/gpu/ganesh/GrShaderCaps.h"
#include "src/gpu/ganesh/SurfaceContext.h"
#include "tests/CtsEnforcement.h"
#include "tests/Test.h"
#include "tests/TestUtils.h"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <initializer_list>
#include <memory>

Go to the source code of this file.

Typedefs

typedef bool(* CheckFn) (uint32_t orig, uint32_t actual, float error)
 

Functions

template<float(*)(float) CONVERT>
static bool check_conversion (uint32_t input, uint32_t output, float error)
 
template<float(*)(float) FORWARD, float(*)(float) BACKWARD>
static bool check_double_conversion (uint32_t input, uint32_t output, float error)
 
static bool check_srgb_to_linear_conversion (uint32_t srgb, uint32_t linear, float error)
 
static bool check_linear_to_srgb_conversion (uint32_t linear, uint32_t srgb, float error)
 
static bool check_linear_to_srgb_to_linear_conversion (uint32_t input, uint32_t output, float error)
 
static bool check_srgb_to_linear_to_srgb_conversion (uint32_t input, uint32_t output, float error)
 
static bool check_no_conversion (uint32_t input, uint32_t output, float error)
 
void read_and_check_pixels (skiatest::Reporter *reporter, GrDirectContext *dContext, skgpu::ganesh::SurfaceContext *sc, uint32_t *origData, const SkImageInfo &dstInfo, CheckFn checker, float error, const char *subtestName)
 
static sk_sp< SkColorSpaceencoding_as_color_space (Encoding encoding)
 
static const char * encoding_as_str (Encoding encoding)
 
static std::unique_ptr< uint32_t[]> make_data ()
 
static std::unique_ptr< skgpu::ganesh::SurfaceContextmake_surface_context (Encoding contextEncoding, GrRecordingContext *rContext, skiatest::Reporter *reporter)
 
static void test_write_read (Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding, float error, CheckFn check, GrDirectContext *dContext, skiatest::Reporter *reporter)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (SRGBReadWritePixels, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 

Variables

static constexpr int kW = 255
 
static constexpr int kH = 255
 

Typedef Documentation

◆ CheckFn

typedef bool(* CheckFn) (uint32_t orig, uint32_t actual, float error)

Definition at line 145 of file SRGBReadWritePixelsTest.cpp.

Function Documentation

◆ check_conversion()

template<float(*)(float) CONVERT>
static bool check_conversion ( uint32_t  input,
uint32_t  output,
float  error 
)
static

tests a conversion with an error tolerance

Definition at line 65 of file SRGBReadWritePixelsTest.cpp.

66 {
67 // alpha should always be exactly preserved.
68 if ((input & 0xff000000) != (output & 0xff000000)) {
69 return false;
70 }
71
72 for (int c = 0; c < 3; ++c) {
73 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
74 float lower = std::max(0.f, (float) inputComponent - error);
75 float upper = std::min(255.f, (float) inputComponent + error);
76 lower = CONVERT(lower / 255.f);
77 upper = CONVERT(upper / 255.f);
78 SkASSERT(lower >= 0.f && lower <= 255.f);
79 SkASSERT(upper >= 0.f && upper <= 255.f);
80 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
81 if (outputComponent < SkScalarFloorToInt(lower * 255.f) ||
82 outputComponent > SkScalarCeilToInt(upper * 255.f)) {
83 return false;
84 }
85 }
86 return true;
87}
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkScalarCeilToInt(x)
Definition SkScalar.h:36
#define SkScalarFloorToInt(x)
Definition SkScalar.h:35
const uint8_t uint32_t uint32_t GError ** error

◆ check_double_conversion()

template<float(*)(float) FORWARD, float(*)(float) BACKWARD>
static bool check_double_conversion ( uint32_t  input,
uint32_t  output,
float  error 
)
static

tests a forward and backward conversion with an error tolerance

Definition at line 91 of file SRGBReadWritePixelsTest.cpp.

91 {
92 // alpha should always be exactly preserved.
93 if ((input & 0xff000000) != (output & 0xff000000)) {
94 return false;
95 }
96
97 for (int c = 0; c < 3; ++c) {
98 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
99 float lower = std::max(0.f, (float) inputComponent - error);
100 float upper = std::min(255.f, (float) inputComponent + error);
101 lower = FORWARD(lower / 255.f);
102 upper = FORWARD(upper / 255.f);
103 SkASSERT(lower >= 0.f && lower <= 255.f);
104 SkASSERT(upper >= 0.f && upper <= 255.f);
105 uint8_t upperComponent = SkScalarCeilToInt(upper * 255.f);
106 uint8_t lowerComponent = SkScalarFloorToInt(lower * 255.f);
107 lower = std::max(0.f, (float) lowerComponent - error);
108 upper = std::min(255.f, (float) upperComponent + error);
109 lower = BACKWARD(lowerComponent / 255.f);
110 upper = BACKWARD(upperComponent / 255.f);
111 SkASSERT(lower >= 0.f && lower <= 255.f);
112 SkASSERT(upper >= 0.f && upper <= 255.f);
113 upperComponent = SkScalarCeilToInt(upper * 255.f);
114 lowerComponent = SkScalarFloorToInt(lower * 255.f);
115
116 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
117 if (outputComponent < lowerComponent || outputComponent > upperComponent) {
118 return false;
119 }
120 }
121 return true;
122}

◆ check_linear_to_srgb_conversion()

static bool check_linear_to_srgb_conversion ( uint32_t  linear,
uint32_t  srgb,
float  error 
)
static

Definition at line 128 of file SRGBReadWritePixelsTest.cpp.

128 {
129 return check_conversion<linear_to_srgb>(linear, srgb, error);
130}
static sk_sp< SkShader > linear(sk_sp< SkShader > shader)

◆ check_linear_to_srgb_to_linear_conversion()

static bool check_linear_to_srgb_to_linear_conversion ( uint32_t  input,
uint32_t  output,
float  error 
)
static

Definition at line 132 of file SRGBReadWritePixelsTest.cpp.

132 {
133 return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
134}

◆ check_no_conversion()

static bool check_no_conversion ( uint32_t  input,
uint32_t  output,
float  error 
)
static

Definition at line 140 of file SRGBReadWritePixelsTest.cpp.

140 {
141 // This is a bit of a hack to check identity transformations that may lose precision.
142 return check_srgb_to_linear_to_srgb_conversion(input, output, error);
143}
static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error)

◆ check_srgb_to_linear_conversion()

static bool check_srgb_to_linear_conversion ( uint32_t  srgb,
uint32_t  linear,
float  error 
)
static

Definition at line 124 of file SRGBReadWritePixelsTest.cpp.

124 {
125 return check_conversion<srgb_to_linear>(srgb, linear, error);
126}

◆ check_srgb_to_linear_to_srgb_conversion()

static bool check_srgb_to_linear_to_srgb_conversion ( uint32_t  input,
uint32_t  output,
float  error 
)
static

Definition at line 136 of file SRGBReadWritePixelsTest.cpp.

136 {
137 return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
138}

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS()

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( SRGBReadWritePixels  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 263 of file SRGBReadWritePixelsTest.cpp.

266 {
267 auto context = ctxInfo.directContext();
268 if (!context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888_SRGB,
269 GrRenderable::kNo).isValid()) {
270 return;
271 }
272 // We allow more error on GPUs with lower precision shader variables.
273 float error = context->priv().caps()->shaderCaps()->fHalfIs32Bits ? 0.5f : 1.2f;
274 // For the all-sRGB case, we allow a small error only for devices that have
275 // precision variation because the sRGB data gets converted to linear and back in
276 // the shader.
277 float smallError = context->priv().caps()->shaderCaps()->fHalfIs32Bits ? 0.0f : 1.f;
278
279 ///////////////////////////////////////////////////////////////////////////////////////////////
280 // Write sRGB data to a sRGB context - no conversion on the write.
281
282 // back to sRGB - no conversion.
283 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kSRGB, smallError,
284 check_no_conversion, context, reporter);
285 // Reading back to untagged should be a pass through with no conversion.
286 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kUntagged, error,
287 check_no_conversion, context, reporter);
288
289 // Converts back to linear
290 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kLinear, error,
292
293 // Untagged source data should be interpreted as sRGB.
294 test_write_read(Encoding::kSRGB, Encoding::kUntagged, Encoding::kSRGB, smallError,
295 check_no_conversion, context, reporter);
296
297 ///////////////////////////////////////////////////////////////////////////////////////////////
298 // Write linear data to a sRGB context. It gets converted to sRGB on write. The reads
299 // are all the same as the above cases where the original data was untagged.
300 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kSRGB, error,
302 // When the dst buffer is untagged there should be no conversion on the read.
303 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kUntagged, error,
305 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kLinear, error,
307
308 ///////////////////////////////////////////////////////////////////////////////////////////////
309 // Write data to an untagged context. The write does no conversion no matter what encoding the
310 // src data has.
311 for (auto writeEncoding : {Encoding::kSRGB, Encoding::kUntagged, Encoding::kLinear}) {
312 // The read from untagged to sRGB also does no conversion.
313 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kSRGB, error,
314 check_no_conversion, context, reporter);
315 // Reading untagged back as untagged should do no conversion.
316 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kUntagged, error,
317 check_no_conversion, context, reporter);
318 // Reading untagged back as linear does convert (context is source, so treated as sRGB),
319 // dst is tagged.
320 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kLinear, error,
322 }
323
324 ///////////////////////////////////////////////////////////////////////////////////////////////
325 // Write sRGB data to a linear context - converts to sRGB on the write.
326
327 // converts back to sRGB on read.
328 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kSRGB, error,
330 // Reading untagged data from linear currently does no conversion.
331 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kUntagged, error,
333 // Stays linear when read.
334 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kLinear, error,
336
337 // Untagged source data should be interpreted as sRGB.
338 test_write_read(Encoding::kLinear, Encoding::kUntagged, Encoding::kSRGB, error,
340
341 ///////////////////////////////////////////////////////////////////////////////////////////////
342 // Write linear data to a linear context. Does no conversion.
343
344 // Reading to sRGB does a conversion.
345 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kSRGB, error,
347 // Reading to untagged does no conversion.
348 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kUntagged, error,
349 check_no_conversion, context, reporter);
350 // Stays linear when read.
351 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kLinear, error,
352 check_no_conversion, context, reporter);
353}
reporter
static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error)
static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error)
static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error)
static bool check_no_conversion(uint32_t input, uint32_t output, float error)
static void test_write_read(Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding, float error, CheckFn check, GrDirectContext *dContext, skiatest::Reporter *reporter)

◆ encoding_as_color_space()

static sk_sp< SkColorSpace > encoding_as_color_space ( Encoding  encoding)
static

Definition at line 186 of file SRGBReadWritePixelsTest.cpp.

186 {
187 switch (encoding) {
188 case Encoding::kUntagged: return nullptr;
189 case Encoding::kLinear: return SkColorSpace::MakeSRGBLinear();
190 case Encoding::kSRGB: return SkColorSpace::MakeSRGB();
191 }
192 return nullptr;
193}
static sk_sp< SkColorSpace > MakeSRGB()
static sk_sp< SkColorSpace > MakeSRGBLinear()

◆ encoding_as_str()

static const char * encoding_as_str ( Encoding  encoding)
static

Definition at line 195 of file SRGBReadWritePixelsTest.cpp.

195 {
196 switch (encoding) {
197 case Encoding::kUntagged: return "untagged";
198 case Encoding::kLinear: return "linear";
199 case Encoding::kSRGB: return "sRGB";
200 }
201 return nullptr;
202}

◆ make_data()

static std::unique_ptr< uint32_t[]> make_data ( )
static

Definition at line 207 of file SRGBReadWritePixelsTest.cpp.

207 {
208 std::unique_ptr<uint32_t[]> data(new uint32_t[kW * kH]);
209 for (int j = 0; j < kH; ++j) {
210 for (int i = 0; i < kW; ++i) {
211 data[j * kW + i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
212 }
213 }
214 return data;
215}
static constexpr int kH
static constexpr int kW
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41

◆ make_surface_context()

static std::unique_ptr< skgpu::ganesh::SurfaceContext > make_surface_context ( Encoding  contextEncoding,
GrRecordingContext rContext,
skiatest::Reporter reporter 
)
static

Definition at line 217 of file SRGBReadWritePixelsTest.cpp.

218 {
221 encoding_as_color_space(contextEncoding),
222 kW, kH);
223
224 auto sc = CreateSurfaceContext(rContext,
225 info,
228 GrRenderable::kYes);
229 if (!sc) {
230 ERRORF(reporter, "Could not create %s surface context.", encoding_as_str(contextEncoding));
231 }
232 return sc;
233}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kBottomLeft_GrSurfaceOrigin
Definition GrTypes.h:149
static const char * encoding_as_str(Encoding encoding)
static sk_sp< SkColorSpace > encoding_as_color_space(Encoding encoding)
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
std::unique_ptr< skgpu::ganesh::SurfaceContext > CreateSurfaceContext(GrRecordingContext *rContext, const GrImageInfo &info, SkBackingFit fit, GrSurfaceOrigin origin, GrRenderable renderable, int sampleCount, skgpu::Mipmapped mipmapped, GrProtected isProtected, skgpu::Budgeted budgeted)
#define ERRORF(r,...)
Definition Test.h:293

◆ read_and_check_pixels()

void read_and_check_pixels ( skiatest::Reporter reporter,
GrDirectContext dContext,
skgpu::ganesh::SurfaceContext sc,
uint32_t *  origData,
const SkImageInfo dstInfo,
CheckFn  checker,
float  error,
const char *  subtestName 
)

Definition at line 147 of file SRGBReadWritePixelsTest.cpp.

154 {
155 auto [w, h] = dstInfo.dimensions();
156 GrPixmap readPM = GrPixmap::Allocate(dstInfo);
157 memset(readPM.addr(), 0, sizeof(uint32_t)*w*h);
158
159 if (!sc->readPixels(dContext, readPM, {0, 0})) {
160 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
161 return;
162 }
163
164 for (int j = 0; j < h; ++j) {
165 for (int i = 0; i < w; ++i) {
166 uint32_t orig = origData[j * w + i];
167 uint32_t read = static_cast<uint32_t*>(readPM.addr())[j * w + i];
168
169 if (!checker(orig, read, error)) {
170 ERRORF(reporter, "Original 0x%08x, read back as 0x%08x in %s at %d, %d).", orig,
171 read, subtestName, i, j);
172 return;
173 }
174 }
175 }
176}
static bool read(SkStream *stream, void *buffer, size_t amount)
T * addr() const
Definition GrPixmap.h:20
static GrPixmap Allocate(const GrImageInfo &info)
Definition GrPixmap.h:101
bool readPixels(GrDirectContext *dContext, GrPixmap dst, SkIPoint srcPt)
SkScalar w
SkScalar h
SkISize dimensions() const

◆ test_write_read()

static void test_write_read ( Encoding  contextEncoding,
Encoding  writeEncoding,
Encoding  readEncoding,
float  error,
CheckFn  check,
GrDirectContext dContext,
skiatest::Reporter reporter 
)
static

Definition at line 235 of file SRGBReadWritePixelsTest.cpp.

237 {
238 auto surfaceContext = make_surface_context(contextEncoding, dContext, reporter);
239 if (!surfaceContext) {
240 return;
241 }
243 encoding_as_color_space(writeEncoding));
244 auto data = make_data();
245 GrCPixmap dataPM(writeII, data.get(), kW*sizeof(uint32_t));
246 if (!surfaceContext->writePixels(dContext, dataPM, {0, 0})) {
247 ERRORF(reporter, "Could not write %s to %s surface context.",
248 encoding_as_str(writeEncoding), encoding_as_str(contextEncoding));
249 return;
250 }
251
253 encoding_as_color_space(readEncoding));
255 testName.printf("write %s data to a %s context and read as %s.", encoding_as_str(writeEncoding),
256 encoding_as_str(contextEncoding), encoding_as_str(readEncoding));
257 read_and_check_pixels(reporter, dContext, surfaceContext.get(), data.get(), readII, check,
258 error, testName.c_str());
259}
#define check(reporter, ref, unref, make, kill)
static std::unique_ptr< uint32_t[]> make_data()
void read_and_check_pixels(skiatest::Reporter *reporter, GrDirectContext *dContext, skgpu::ganesh::SurfaceContext *sc, uint32_t *origData, const SkImageInfo &dstInfo, CheckFn checker, float error, const char *subtestName)
static std::unique_ptr< skgpu::ganesh::SurfaceContext > make_surface_context(Encoding contextEncoding, GrRecordingContext *rContext, skiatest::Reporter *reporter)
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

Variable Documentation

◆ kH

constexpr int kH = 255
staticconstexpr

Definition at line 205 of file SRGBReadWritePixelsTest.cpp.

◆ kW

constexpr int kW = 255
staticconstexpr

Definition at line 204 of file SRGBReadWritePixelsTest.cpp.