blob: 14a90f1372b5d550db69470f3242e06148cc6d16 [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"
10#include "include/gpu/GrContext.h"
11#include "src/gpu/GrCaps.h"
12#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040013#include "src/gpu/GrImageInfo.h"
Brian Salomon947efe22019-07-16 15:36:11 -040014#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrSurfaceContext.h"
16#include "src/gpu/SkGr.h"
17#include "tests/Test.h"
bsalomon16921ec2015-07-30 15:34:56 -070018
19// using anonymous namespace because these functions are used as template params.
20namespace {
21/** convert 0..1 srgb value to 0..1 linear */
22float srgb_to_linear(float srgb) {
23 if (srgb <= 0.04045f) {
24 return srgb / 12.92f;
25 } else {
26 return powf((srgb + 0.055f) / 1.055f, 2.4f);
27 }
28}
29
30/** convert 0..1 linear value to 0..1 srgb */
31float linear_to_srgb(float linear) {
32 if (linear <= 0.0031308) {
33 return linear * 12.92f;
34 } else {
35 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
36 }
37}
38}
39
40/** tests a conversion with an error tolerance */
41template <float (*CONVERT)(float)> static bool check_conversion(uint32_t input, uint32_t output,
42 float error) {
43 // alpha should always be exactly preserved.
44 if ((input & 0xff000000) != (output & 0xff000000)) {
45 return false;
46 }
47
48 for (int c = 0; c < 3; ++c) {
49 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
Brian Osman788b9162020-02-07 10:36:46 -050050 float lower = std::max(0.f, (float) inputComponent - error);
51 float upper = std::min(255.f, (float) inputComponent + error);
bsalomon16921ec2015-07-30 15:34:56 -070052 lower = CONVERT(lower / 255.f);
53 upper = CONVERT(upper / 255.f);
54 SkASSERT(lower >= 0.f && lower <= 255.f);
55 SkASSERT(upper >= 0.f && upper <= 255.f);
56 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
57 if (outputComponent < SkScalarFloorToInt(lower * 255.f) ||
58 outputComponent > SkScalarCeilToInt(upper * 255.f)) {
59 return false;
60 }
61 }
62 return true;
63}
64
65/** tests a forward and backward conversion with an error tolerance */
66template <float (*FORWARD)(float), float (*BACKWARD)(float)>
67static bool check_double_conversion(uint32_t input, uint32_t output, float error) {
68 // alpha should always be exactly preserved.
69 if ((input & 0xff000000) != (output & 0xff000000)) {
70 return false;
71 }
72
73 for (int c = 0; c < 3; ++c) {
74 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
Brian Osman788b9162020-02-07 10:36:46 -050075 float lower = std::max(0.f, (float) inputComponent - error);
76 float upper = std::min(255.f, (float) inputComponent + error);
bsalomon16921ec2015-07-30 15:34:56 -070077 lower = FORWARD(lower / 255.f);
78 upper = FORWARD(upper / 255.f);
79 SkASSERT(lower >= 0.f && lower <= 255.f);
80 SkASSERT(upper >= 0.f && upper <= 255.f);
81 uint8_t upperComponent = SkScalarCeilToInt(upper * 255.f);
82 uint8_t lowerComponent = SkScalarFloorToInt(lower * 255.f);
Brian Osman788b9162020-02-07 10:36:46 -050083 lower = std::max(0.f, (float) lowerComponent - error);
84 upper = std::min(255.f, (float) upperComponent + error);
bsalomon16921ec2015-07-30 15:34:56 -070085 lower = BACKWARD(lowerComponent / 255.f);
86 upper = BACKWARD(upperComponent / 255.f);
87 SkASSERT(lower >= 0.f && lower <= 255.f);
88 SkASSERT(upper >= 0.f && upper <= 255.f);
89 upperComponent = SkScalarCeilToInt(upper * 255.f);
90 lowerComponent = SkScalarFloorToInt(lower * 255.f);
91
92 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
93 if (outputComponent < lowerComponent || outputComponent > upperComponent) {
94 return false;
95 }
96 }
97 return true;
98}
99
100static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error) {
101 return check_conversion<srgb_to_linear>(srgb, linear, error);
102}
103
104static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error) {
105 return check_conversion<linear_to_srgb>(linear, srgb, error);
106}
107
108static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error) {
109 return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
110}
111
112static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error) {
113 return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
114}
115
Brian Salomonf802e752018-02-13 17:13:31 -0500116static bool check_no_conversion(uint32_t input, uint32_t output, float error) {
117 // This is a bit of a hack to check identity transformations that may lose precision.
118 return check_srgb_to_linear_to_srgb_conversion(input, output, error);
119}
120
bsalomon16921ec2015-07-30 15:34:56 -0700121typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
122
Robert Phillips301431d2017-03-29 12:08:49 -0400123void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
124 uint32_t* origData,
125 const SkImageInfo& dstInfo, CheckFn checker, float error,
bsalomon16921ec2015-07-30 15:34:56 -0700126 const char* subtestName) {
Robert Phillips301431d2017-03-29 12:08:49 -0400127 int w = dstInfo.width();
128 int h = dstInfo.height();
bsalomon16921ec2015-07-30 15:34:56 -0700129 SkAutoTMalloc<uint32_t> readData(w * h);
130 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -0400131
Brian Salomon1d435302019-07-01 13:05:28 -0400132 if (!context->readPixels(dstInfo, readData.get(), 0, {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];
140 uint32_t read = readData[j * w + i];
141
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};
157}
158
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,
191 GrContext* context,
192 skiatest::Reporter* reporter) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500193 auto surfaceContext = GrRenderTargetContext::Make(
194 context, GrColorType::kRGBA_8888, encoding_as_color_space(contextEncoding),
195 SkBackingFit::kExact, {kW, kH}, 1, GrMipMapped::kNo, GrProtected::kNo,
196 kBottomLeft_GrSurfaceOrigin, SkBudgeted::kNo);
Brian Salomonf802e752018-02-13 17:13:31 -0500197 if (!surfaceContext) {
198 ERRORF(reporter, "Could not create %s surface context.", encoding_as_str(contextEncoding));
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500199 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400200 return surfaceContext;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500201}
202
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500203static void test_write_read(Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding,
204 float error, CheckFn check, GrContext* context,
205 skiatest::Reporter* reporter) {
206 auto surfaceContext = make_surface_context(contextEncoding, context, reporter);
207 if (!surfaceContext) {
208 return;
209 }
210 auto writeII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
211 encoding_as_color_space(writeEncoding));
212 auto data = make_data();
Brian Salomon1d435302019-07-01 13:05:28 -0400213 if (!surfaceContext->writePixels(writeII, data.get(), 0, {0, 0})) {
Brian Salomonf802e752018-02-13 17:13:31 -0500214 ERRORF(reporter, "Could not write %s to %s surface context.",
215 encoding_as_str(writeEncoding), encoding_as_str(contextEncoding));
216 return;
217 }
218
219 auto readII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
220 encoding_as_color_space(readEncoding));
221 SkString testName;
222 testName.printf("write %s data to a %s context and read as %s.", encoding_as_str(writeEncoding),
223 encoding_as_str(contextEncoding), encoding_as_str(readEncoding));
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500224 read_and_check_pixels(reporter, surfaceContext.get(), data.get(), readII, check, error,
Brian Salomonf802e752018-02-13 17:13:31 -0500225 testName.c_str());
226}
227
228// Test all combinations of writePixels/readPixels where the surface context/write source/read dst
229// are sRGB, linear, or untagged RGBA_8888.
230DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
231 GrContext* context = ctxInfo.grContext();
Greg Daniel0258c902019-08-01 13:08:33 -0400232 if (!context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888_SRGB,
233 GrRenderable::kNo).isValid()) {
Brian Salomonf802e752018-02-13 17:13:31 -0500234 return;
235 }
236 // We allow more error on GPUs with lower precision shader variables.
Robert Phillips9da87e02019-02-04 13:26:26 -0500237 float error = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
Brian Salomonf802e752018-02-13 17:13:31 -0500238 // For the all-sRGB case, we allow a small error only for devices that have
239 // precision variation because the sRGB data gets converted to linear and back in
240 // the shader.
Robert Phillips9da87e02019-02-04 13:26:26 -0500241 float smallError = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.0f : 1.f;
Brian Salomonf802e752018-02-13 17:13:31 -0500242
243 ///////////////////////////////////////////////////////////////////////////////////////////////
244 // Write sRGB data to a sRGB context - no conversion on the write.
245
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400246 // back to sRGB - no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500247 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kSRGB, smallError,
248 check_no_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400249 // Reading back to untagged should be a pass through with no conversion.
250 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kUntagged, error,
251 check_no_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400252
Brian Salomonf802e752018-02-13 17:13:31 -0500253 // Converts back to linear
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500254 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kLinear, error,
255 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500256
Mike Klein12d4b6e2018-08-17 14:09:55 -0400257 // Untagged source data should be interpreted as sRGB.
258 test_write_read(Encoding::kSRGB, Encoding::kUntagged, Encoding::kSRGB, smallError,
259 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500260
261 ///////////////////////////////////////////////////////////////////////////////////////////////
262 // Write linear data to a sRGB context. It gets converted to sRGB on write. The reads
263 // are all the same as the above cases where the original data was untagged.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500264 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kSRGB, error,
265 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400266 // When the dst buffer is untagged there should be no conversion on the read.
267 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kUntagged, error,
268 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500269 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kLinear, error,
270 check_linear_to_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500271
272 ///////////////////////////////////////////////////////////////////////////////////////////////
273 // Write data to an untagged context. The write does no conversion no matter what encoding the
274 // src data has.
275 for (auto writeEncoding : {Encoding::kSRGB, Encoding::kUntagged, Encoding::kLinear}) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400276 // The read from untagged to sRGB also does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500277 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kSRGB, error,
278 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500279 // Reading untagged back as untagged should do no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500280 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kUntagged, error,
281 check_no_conversion, context, reporter);
Brian Osman4f98dbe2019-03-28 11:49:01 -0400282 // Reading untagged back as linear does convert (context is source, so treated as sRGB),
283 // dst is tagged.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500284 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kLinear, error,
Brian Osman4f98dbe2019-03-28 11:49:01 -0400285 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500286 }
287
288 ///////////////////////////////////////////////////////////////////////////////////////////////
289 // Write sRGB data to a linear context - converts to sRGB on the write.
290
291 // converts back to sRGB on read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500292 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kSRGB, error,
293 check_srgb_to_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400294 // Reading untagged data from linear currently does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500295 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kUntagged, error,
296 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500297 // Stays linear when read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500298 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kLinear, error,
299 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500300
Mike Klein12d4b6e2018-08-17 14:09:55 -0400301 // Untagged source data should be interpreted as sRGB.
302 test_write_read(Encoding::kLinear, Encoding::kUntagged, Encoding::kSRGB, error,
303 check_srgb_to_linear_to_srgb_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500304
305 ///////////////////////////////////////////////////////////////////////////////////////////////
306 // Write linear data to a linear context. Does no conversion.
307
308 // Reading to sRGB does a conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500309 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kSRGB, error,
310 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400311 // Reading to untagged does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500312 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kUntagged, error,
313 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500314 // Stays linear when read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500315 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kLinear, error,
316 check_no_conversion, context, reporter);
bsalomon16921ec2015-07-30 15:34:56 -0700317}