blob: 5be62611bd64115cc082b7dbbb97d0b92e470ff3 [file] [log] [blame]
bsalomon16921ec2015-07-30 15:34:56 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040010#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040012#include "src/gpu/GrDirectContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040013#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrSurfaceContext.h"
15#include "src/gpu/SkGr.h"
16#include "tests/Test.h"
bsalomon16921ec2015-07-30 15:34:56 -070017
18// using anonymous namespace because these functions are used as template params.
19namespace {
20/** convert 0..1 srgb value to 0..1 linear */
21float srgb_to_linear(float srgb) {
22 if (srgb <= 0.04045f) {
23 return srgb / 12.92f;
24 } else {
25 return powf((srgb + 0.055f) / 1.055f, 2.4f);
26 }
27}
28
29/** convert 0..1 linear value to 0..1 srgb */
30float linear_to_srgb(float linear) {
31 if (linear <= 0.0031308) {
32 return linear * 12.92f;
33 } else {
34 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
35 }
36}
John Stilesa6841be2020-08-06 14:11:56 -040037} // namespace
bsalomon16921ec2015-07-30 15:34:56 -070038
39/** tests a conversion with an error tolerance */
40template <float (*CONVERT)(float)> static bool check_conversion(uint32_t input, uint32_t output,
41 float error) {
42 // alpha should always be exactly preserved.
43 if ((input & 0xff000000) != (output & 0xff000000)) {
44 return false;
45 }
46
47 for (int c = 0; c < 3; ++c) {
48 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
Brian Osman788b9162020-02-07 10:36:46 -050049 float lower = std::max(0.f, (float) inputComponent - error);
50 float upper = std::min(255.f, (float) inputComponent + error);
bsalomon16921ec2015-07-30 15:34:56 -070051 lower = CONVERT(lower / 255.f);
52 upper = CONVERT(upper / 255.f);
53 SkASSERT(lower >= 0.f && lower <= 255.f);
54 SkASSERT(upper >= 0.f && upper <= 255.f);
55 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
56 if (outputComponent < SkScalarFloorToInt(lower * 255.f) ||
57 outputComponent > SkScalarCeilToInt(upper * 255.f)) {
58 return false;
59 }
60 }
61 return true;
62}
63
64/** tests a forward and backward conversion with an error tolerance */
65template <float (*FORWARD)(float), float (*BACKWARD)(float)>
66static bool check_double_conversion(uint32_t input, uint32_t output, 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));
Brian Osman788b9162020-02-07 10:36:46 -050074 float lower = std::max(0.f, (float) inputComponent - error);
75 float upper = std::min(255.f, (float) inputComponent + error);
bsalomon16921ec2015-07-30 15:34:56 -070076 lower = FORWARD(lower / 255.f);
77 upper = FORWARD(upper / 255.f);
78 SkASSERT(lower >= 0.f && lower <= 255.f);
79 SkASSERT(upper >= 0.f && upper <= 255.f);
80 uint8_t upperComponent = SkScalarCeilToInt(upper * 255.f);
81 uint8_t lowerComponent = SkScalarFloorToInt(lower * 255.f);
Brian Osman788b9162020-02-07 10:36:46 -050082 lower = std::max(0.f, (float) lowerComponent - error);
83 upper = std::min(255.f, (float) upperComponent + error);
bsalomon16921ec2015-07-30 15:34:56 -070084 lower = BACKWARD(lowerComponent / 255.f);
85 upper = BACKWARD(upperComponent / 255.f);
86 SkASSERT(lower >= 0.f && lower <= 255.f);
87 SkASSERT(upper >= 0.f && upper <= 255.f);
88 upperComponent = SkScalarCeilToInt(upper * 255.f);
89 lowerComponent = SkScalarFloorToInt(lower * 255.f);
90
91 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
92 if (outputComponent < lowerComponent || outputComponent > upperComponent) {
93 return false;
94 }
95 }
96 return true;
97}
98
99static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error) {
100 return check_conversion<srgb_to_linear>(srgb, linear, error);
101}
102
103static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error) {
104 return check_conversion<linear_to_srgb>(linear, srgb, error);
105}
106
107static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error) {
108 return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
109}
110
111static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error) {
112 return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
113}
114
Brian Salomonf802e752018-02-13 17:13:31 -0500115static bool check_no_conversion(uint32_t input, uint32_t output, float error) {
116 // This is a bit of a hack to check identity transformations that may lose precision.
117 return check_srgb_to_linear_to_srgb_conversion(input, output, error);
118}
119
bsalomon16921ec2015-07-30 15:34:56 -0700120typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
121
Adlai Hollerc95b5892020-08-11 12:02:22 -0400122void read_and_check_pixels(skiatest::Reporter* reporter,
123 GrDirectContext* dContext,
124 GrSurfaceContext* sContext,
Robert Phillips301431d2017-03-29 12:08:49 -0400125 uint32_t* origData,
126 const SkImageInfo& dstInfo, CheckFn checker, float error,
bsalomon16921ec2015-07-30 15:34:56 -0700127 const char* subtestName) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500128 auto [w, h] = dstInfo.dimensions();
Brian Salomonbe1084b2021-01-26 13:29:30 -0500129 GrPixmap readPM = GrPixmap::Allocate(dstInfo);
Brian Salomondd4087d2020-12-23 20:36:44 -0500130 memset(readPM.addr(), 0, sizeof(uint32_t)*w*h);
Robert Phillips301431d2017-03-29 12:08:49 -0400131
Brian Salomondd4087d2020-12-23 20:36:44 -0500132 if (!sContext->readPixels(dContext, readPM, {0, 0})) {
bsalomon16921ec2015-07-30 15:34:56 -0700133 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
134 return;
135 }
Robert Phillips301431d2017-03-29 12:08:49 -0400136
bsalomon16921ec2015-07-30 15:34:56 -0700137 for (int j = 0; j < h; ++j) {
138 for (int i = 0; i < w; ++i) {
139 uint32_t orig = origData[j * w + i];
Brian Salomondd4087d2020-12-23 20:36:44 -0500140 uint32_t read = static_cast<uint32_t*>(readPM.addr())[j * w + i];
bsalomon16921ec2015-07-30 15:34:56 -0700141
142 if (!checker(orig, read, error)) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400143 ERRORF(reporter, "Original 0x%08x, read back as 0x%08x in %s at %d, %d).", orig,
144 read, subtestName, i, j);
bsalomon16921ec2015-07-30 15:34:56 -0700145 return;
146 }
147 }
148 }
149}
150
Brian Salomonf802e752018-02-13 17:13:31 -0500151namespace {
152enum class Encoding {
153 kUntagged,
154 kLinear,
155 kSRGB,
156};
John Stilesa6841be2020-08-06 14:11:56 -0400157} // namespace
Brian Salomonf802e752018-02-13 17:13:31 -0500158
159static sk_sp<SkColorSpace> encoding_as_color_space(Encoding encoding) {
160 switch (encoding) {
161 case Encoding::kUntagged: return nullptr;
162 case Encoding::kLinear: return SkColorSpace::MakeSRGBLinear();
163 case Encoding::kSRGB: return SkColorSpace::MakeSRGB();
164 }
165 return nullptr;
166}
167
Brian Salomonf802e752018-02-13 17:13:31 -0500168static const char* encoding_as_str(Encoding encoding) {
169 switch (encoding) {
170 case Encoding::kUntagged: return "untagged";
171 case Encoding::kLinear: return "linear";
172 case Encoding::kSRGB: return "sRGB";
173 }
174 return nullptr;
175}
176
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500177static constexpr int kW = 255;
178static constexpr int kH = 255;
179
180static std::unique_ptr<uint32_t[]> make_data() {
181 std::unique_ptr<uint32_t[]> data(new uint32_t[kW * kH]);
bsalomon16921ec2015-07-30 15:34:56 -0700182 for (int j = 0; j < kH; ++j) {
183 for (int i = 0; i < kW; ++i) {
Brian Osmanf494bd22018-07-12 10:47:01 -0400184 data[j * kW + i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
bsalomon16921ec2015-07-30 15:34:56 -0700185 }
186 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500187 return data;
188}
bsalomon16921ec2015-07-30 15:34:56 -0700189
Brian Salomonbf6b9792019-08-21 09:38:10 -0400190static std::unique_ptr<GrSurfaceContext> make_surface_context(Encoding contextEncoding,
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400191 GrRecordingContext* rContext,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400192 skiatest::Reporter* reporter) {
Robert Phillips1a2e7de2021-07-29 11:23:48 -0400193 GrImageInfo info(GrColorType::kRGBA_8888,
194 kPremul_SkAlphaType,
195 encoding_as_color_space(contextEncoding),
196 kW, kH);
197
198 auto sc = GrSurfaceContext::Make(rContext, info,
199 SkBackingFit::kExact,
200 kBottomLeft_GrSurfaceOrigin,
201 GrRenderable::kYes);
202 if (!sc) {
Brian Salomonf802e752018-02-13 17:13:31 -0500203 ERRORF(reporter, "Could not create %s surface context.", encoding_as_str(contextEncoding));
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500204 }
Robert Phillips1a2e7de2021-07-29 11:23:48 -0400205 return sc;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500206}
207
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500208static void test_write_read(Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400209 float error, CheckFn check, GrDirectContext* dContext,
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500210 skiatest::Reporter* reporter) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400211 auto surfaceContext = make_surface_context(contextEncoding, dContext, reporter);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500212 if (!surfaceContext) {
213 return;
214 }
215 auto writeII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
216 encoding_as_color_space(writeEncoding));
217 auto data = make_data();
Brian Salomon5392c942021-03-30 16:14:37 -0400218 GrCPixmap dataPM(writeII, data.get(), kW*sizeof(uint32_t));
Brian Salomondd4087d2020-12-23 20:36:44 -0500219 if (!surfaceContext->writePixels(dContext, dataPM, {0, 0})) {
Brian Salomonf802e752018-02-13 17:13:31 -0500220 ERRORF(reporter, "Could not write %s to %s surface context.",
221 encoding_as_str(writeEncoding), encoding_as_str(contextEncoding));
222 return;
223 }
224
225 auto readII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
226 encoding_as_color_space(readEncoding));
227 SkString testName;
228 testName.printf("write %s data to a %s context and read as %s.", encoding_as_str(writeEncoding),
229 encoding_as_str(contextEncoding), encoding_as_str(readEncoding));
Adlai Hollerc95b5892020-08-11 12:02:22 -0400230 read_and_check_pixels(reporter, dContext, surfaceContext.get(), data.get(), readII, check,
231 error, testName.c_str());
Brian Salomonf802e752018-02-13 17:13:31 -0500232}
233
234// Test all combinations of writePixels/readPixels where the surface context/write source/read dst
235// are sRGB, linear, or untagged RGBA_8888.
236DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400237 auto context = ctxInfo.directContext();
Greg Daniel0258c902019-08-01 13:08:33 -0400238 if (!context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888_SRGB,
239 GrRenderable::kNo).isValid()) {
Brian Salomonf802e752018-02-13 17:13:31 -0500240 return;
241 }
242 // We allow more error on GPUs with lower precision shader variables.
Robert Phillips9da87e02019-02-04 13:26:26 -0500243 float error = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
Brian Salomonf802e752018-02-13 17:13:31 -0500244 // For the all-sRGB case, we allow a small error only for devices that have
245 // precision variation because the sRGB data gets converted to linear and back in
246 // the shader.
Robert Phillips9da87e02019-02-04 13:26:26 -0500247 float smallError = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.0f : 1.f;
Brian Salomonf802e752018-02-13 17:13:31 -0500248
249 ///////////////////////////////////////////////////////////////////////////////////////////////
250 // Write sRGB data to a sRGB context - no conversion on the write.
251
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400252 // back to sRGB - no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500253 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kSRGB, smallError,
254 check_no_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400255 // Reading back to untagged should be a pass through with no conversion.
256 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kUntagged, error,
257 check_no_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400258
Brian Salomonf802e752018-02-13 17:13:31 -0500259 // Converts back to linear
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500260 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kLinear, error,
261 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500262
Mike Klein12d4b6e2018-08-17 14:09:55 -0400263 // Untagged source data should be interpreted as sRGB.
264 test_write_read(Encoding::kSRGB, Encoding::kUntagged, Encoding::kSRGB, smallError,
265 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500266
267 ///////////////////////////////////////////////////////////////////////////////////////////////
268 // Write linear data to a sRGB context. It gets converted to sRGB on write. The reads
269 // are all the same as the above cases where the original data was untagged.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500270 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kSRGB, error,
271 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400272 // When the dst buffer is untagged there should be no conversion on the read.
273 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kUntagged, error,
274 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500275 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kLinear, error,
276 check_linear_to_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500277
278 ///////////////////////////////////////////////////////////////////////////////////////////////
279 // Write data to an untagged context. The write does no conversion no matter what encoding the
280 // src data has.
281 for (auto writeEncoding : {Encoding::kSRGB, Encoding::kUntagged, Encoding::kLinear}) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400282 // The read from untagged to sRGB also does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500283 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kSRGB, error,
284 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500285 // Reading untagged back as untagged should do no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500286 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kUntagged, error,
287 check_no_conversion, context, reporter);
Brian Osman4f98dbe2019-03-28 11:49:01 -0400288 // Reading untagged back as linear does convert (context is source, so treated as sRGB),
289 // dst is tagged.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500290 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kLinear, error,
Brian Osman4f98dbe2019-03-28 11:49:01 -0400291 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500292 }
293
294 ///////////////////////////////////////////////////////////////////////////////////////////////
295 // Write sRGB data to a linear context - converts to sRGB on the write.
296
297 // converts back to sRGB on read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500298 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kSRGB, error,
299 check_srgb_to_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400300 // Reading untagged data from linear currently does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500301 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kUntagged, error,
302 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500303 // Stays linear when read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500304 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kLinear, error,
305 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500306
Mike Klein12d4b6e2018-08-17 14:09:55 -0400307 // Untagged source data should be interpreted as sRGB.
308 test_write_read(Encoding::kLinear, Encoding::kUntagged, Encoding::kSRGB, error,
309 check_srgb_to_linear_to_srgb_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500310
311 ///////////////////////////////////////////////////////////////////////////////////////////////
312 // Write linear data to a linear context. Does no conversion.
313
314 // Reading to sRGB does a conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500315 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kSRGB, error,
316 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400317 // Reading to untagged does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500318 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kUntagged, error,
319 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500320 // Stays linear when read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500321 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kLinear, error,
322 check_no_conversion, context, reporter);
bsalomon16921ec2015-07-30 15:34:56 -0700323}