Flutter Engine
The Flutter Engine
SRGBReadWritePixelsTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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
14#include "include/core/SkSize.h"
20#include "include/gpu/GrTypes.h"
30#include "tests/Test.h"
31#include "tests/TestUtils.h"
32
33#include <algorithm>
34#include <cmath>
35#include <cstdint>
36#include <cstring>
37#include <initializer_list>
38#include <memory>
39
41struct GrContextOptions;
42
43// using anonymous namespace because these functions are used as template params.
44namespace {
45/** convert 0..1 srgb value to 0..1 linear */
46float srgb_to_linear(float srgb) {
47 if (srgb <= 0.04045f) {
48 return srgb / 12.92f;
49 } else {
50 return powf((srgb + 0.055f) / 1.055f, 2.4f);
51 }
52}
53
54/** convert 0..1 linear value to 0..1 srgb */
55float linear_to_srgb(float linear) {
56 if (linear <= 0.0031308) {
57 return linear * 12.92f;
58 } else {
59 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
60 }
61}
62} // namespace
63
64/** tests a conversion with an error tolerance */
65template <float (*CONVERT)(float)> static bool check_conversion(uint32_t input, uint32_t output,
66 float error) {
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}
88
89/** tests a forward and backward conversion with an error tolerance */
90template <float (*FORWARD)(float), float (*BACKWARD)(float)>
91static bool check_double_conversion(uint32_t input, uint32_t output, float error) {
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}
123
124static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error) {
125 return check_conversion<srgb_to_linear>(srgb, linear, error);
126}
127
128static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error) {
129 return check_conversion<linear_to_srgb>(linear, srgb, error);
130}
131
132static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error) {
133 return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
134}
135
136static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error) {
137 return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
138}
139
140static bool check_no_conversion(uint32_t input, uint32_t output, float error) {
141 // This is a bit of a hack to check identity transformations that may lose precision.
143}
144
145typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
146
148 GrDirectContext* dContext,
150 uint32_t* origData,
151 const SkImageInfo& dstInfo,
152 CheckFn checker,
153 float error,
154 const char* subtestName) {
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}
177
178namespace {
179enum class Encoding {
180 kUntagged,
181 kLinear,
182 kSRGB,
183};
184} // namespace
185
187 switch (encoding) {
188 case Encoding::kUntagged: return nullptr;
190 case Encoding::kSRGB: return SkColorSpace::MakeSRGB();
191 }
192 return nullptr;
193}
194
195static const char* encoding_as_str(Encoding encoding) {
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}
203
204static constexpr int kW = 255;
205static constexpr int kH = 255;
206
207static std::unique_ptr<uint32_t[]> make_data() {
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}
216
217static std::unique_ptr<skgpu::ganesh::SurfaceContext> make_surface_context(
218 Encoding contextEncoding, GrRecordingContext* rContext, skiatest::Reporter* reporter) {
221 encoding_as_color_space(contextEncoding),
222 kW, kH);
223
224 auto sc = CreateSurfaceContext(rContext,
225 info,
229 if (!sc) {
230 ERRORF(reporter, "Could not create %s surface context.", encoding_as_str(contextEncoding));
231 }
232 return sc;
233}
234
235static void test_write_read(Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding,
236 float error, CheckFn check, GrDirectContext* dContext,
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}
260
261// Test all combinations of writePixels/readPixels where the surface context/write source/read dst
262// are sRGB, linear, or untagged RGBA_8888.
264 reporter,
265 ctxInfo,
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,
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.
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.
347 // Reading to untagged does no conversion.
349 check_no_conversion, context, reporter);
350 // Stays linear when read.
352 check_no_conversion, context, reporter);
353}
static float linear_to_srgb(float linear)
static float srgb_to_linear(float srgb)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
reporter
Definition: FontMgrTest.cpp:39
@ kBottomLeft_GrSurfaceOrigin
Definition: GrTypes.h:149
#define check(reporter, ref, unref, make, kill)
Definition: RefCntTest.cpp:85
static constexpr int kH
static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error)
static const char * encoding_as_str(Encoding encoding)
static std::unique_ptr< uint32_t[]> make_data()
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 bool check_double_conversion(uint32_t input, uint32_t output, float error)
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error)
bool(* CheckFn)(uint32_t orig, uint32_t actual, 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 bool check_conversion(uint32_t input, uint32_t output, float error)
static sk_sp< SkColorSpace > encoding_as_color_space(Encoding encoding)
static std::unique_ptr< skgpu::ganesh::SurfaceContext > make_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)
static constexpr int kW
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
static bool read(SkStream *stream, void *buffer, size_t amount)
#define SkScalarCeilToInt(x)
Definition: SkScalar.h:36
#define SkScalarFloorToInt(x)
Definition: SkScalar.h:35
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)
Definition: TestUtils.cpp:242
#define ERRORF(r,...)
Definition: Test.h:293
T * addr() const
Definition: GrPixmap.h:20
static GrPixmap Allocate(const GrImageInfo &info)
Definition: GrPixmap.h:101
static sk_sp< SkColorSpace > MakeSRGB()
static sk_sp< SkColorSpace > MakeSRGBLinear()
bool readPixels(GrDirectContext *dContext, GrPixmap dst, SkIPoint srcPt)
const uint8_t uint32_t uint32_t GError ** error
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
static constexpr skcms_TransferFunction kLinear
Definition: SkColorSpace.h:51
@ kSRGB
Definition: image.h:17
SkScalar w
SkScalar h
SkISize dimensions() const
Definition: SkImageInfo.h:421
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
static sk_sp< SkShader > linear(sk_sp< SkShader > shader)