blob: 18448702e2f16d40059279a5832bf815728094c1 [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"
Robert Phillips6d344c32020-07-06 10:56:46 -040011#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040014#include "src/gpu/GrImageInfo.h"
Brian Salomon947efe22019-07-16 15:36:11 -040015#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrSurfaceContext.h"
17#include "src/gpu/SkGr.h"
18#include "tests/Test.h"
bsalomon16921ec2015-07-30 15:34:56 -070019
20// using anonymous namespace because these functions are used as template params.
21namespace {
22/** convert 0..1 srgb value to 0..1 linear */
23float srgb_to_linear(float srgb) {
24 if (srgb <= 0.04045f) {
25 return srgb / 12.92f;
26 } else {
27 return powf((srgb + 0.055f) / 1.055f, 2.4f);
28 }
29}
30
31/** convert 0..1 linear value to 0..1 srgb */
32float linear_to_srgb(float linear) {
33 if (linear <= 0.0031308) {
34 return linear * 12.92f;
35 } else {
36 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
37 }
38}
39}
40
41/** tests a conversion with an error tolerance */
42template <float (*CONVERT)(float)> static bool check_conversion(uint32_t input, uint32_t output,
43 float error) {
44 // alpha should always be exactly preserved.
45 if ((input & 0xff000000) != (output & 0xff000000)) {
46 return false;
47 }
48
49 for (int c = 0; c < 3; ++c) {
50 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
Brian Osman788b9162020-02-07 10:36:46 -050051 float lower = std::max(0.f, (float) inputComponent - error);
52 float upper = std::min(255.f, (float) inputComponent + error);
bsalomon16921ec2015-07-30 15:34:56 -070053 lower = CONVERT(lower / 255.f);
54 upper = CONVERT(upper / 255.f);
55 SkASSERT(lower >= 0.f && lower <= 255.f);
56 SkASSERT(upper >= 0.f && upper <= 255.f);
57 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
58 if (outputComponent < SkScalarFloorToInt(lower * 255.f) ||
59 outputComponent > SkScalarCeilToInt(upper * 255.f)) {
60 return false;
61 }
62 }
63 return true;
64}
65
66/** tests a forward and backward conversion with an error tolerance */
67template <float (*FORWARD)(float), float (*BACKWARD)(float)>
68static bool check_double_conversion(uint32_t input, uint32_t output, float error) {
69 // alpha should always be exactly preserved.
70 if ((input & 0xff000000) != (output & 0xff000000)) {
71 return false;
72 }
73
74 for (int c = 0; c < 3; ++c) {
75 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
Brian Osman788b9162020-02-07 10:36:46 -050076 float lower = std::max(0.f, (float) inputComponent - error);
77 float upper = std::min(255.f, (float) inputComponent + error);
bsalomon16921ec2015-07-30 15:34:56 -070078 lower = FORWARD(lower / 255.f);
79 upper = FORWARD(upper / 255.f);
80 SkASSERT(lower >= 0.f && lower <= 255.f);
81 SkASSERT(upper >= 0.f && upper <= 255.f);
82 uint8_t upperComponent = SkScalarCeilToInt(upper * 255.f);
83 uint8_t lowerComponent = SkScalarFloorToInt(lower * 255.f);
Brian Osman788b9162020-02-07 10:36:46 -050084 lower = std::max(0.f, (float) lowerComponent - error);
85 upper = std::min(255.f, (float) upperComponent + error);
bsalomon16921ec2015-07-30 15:34:56 -070086 lower = BACKWARD(lowerComponent / 255.f);
87 upper = BACKWARD(upperComponent / 255.f);
88 SkASSERT(lower >= 0.f && lower <= 255.f);
89 SkASSERT(upper >= 0.f && upper <= 255.f);
90 upperComponent = SkScalarCeilToInt(upper * 255.f);
91 lowerComponent = SkScalarFloorToInt(lower * 255.f);
92
93 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
94 if (outputComponent < lowerComponent || outputComponent > upperComponent) {
95 return false;
96 }
97 }
98 return true;
99}
100
101static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error) {
102 return check_conversion<srgb_to_linear>(srgb, linear, error);
103}
104
105static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error) {
106 return check_conversion<linear_to_srgb>(linear, srgb, error);
107}
108
109static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error) {
110 return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
111}
112
113static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error) {
114 return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
115}
116
Brian Salomonf802e752018-02-13 17:13:31 -0500117static bool check_no_conversion(uint32_t input, uint32_t output, float error) {
118 // This is a bit of a hack to check identity transformations that may lose precision.
119 return check_srgb_to_linear_to_srgb_conversion(input, output, error);
120}
121
bsalomon16921ec2015-07-30 15:34:56 -0700122typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
123
Robert Phillips301431d2017-03-29 12:08:49 -0400124void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
125 uint32_t* origData,
126 const SkImageInfo& dstInfo, CheckFn checker, float error,
bsalomon16921ec2015-07-30 15:34:56 -0700127 const char* subtestName) {
Robert Phillips301431d2017-03-29 12:08:49 -0400128 int w = dstInfo.width();
129 int h = dstInfo.height();
bsalomon16921ec2015-07-30 15:34:56 -0700130 SkAutoTMalloc<uint32_t> readData(w * h);
131 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -0400132
Brian Salomon1d435302019-07-01 13:05:28 -0400133 if (!context->readPixels(dstInfo, readData.get(), 0, {0, 0})) {
bsalomon16921ec2015-07-30 15:34:56 -0700134 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
135 return;
136 }
Robert Phillips301431d2017-03-29 12:08:49 -0400137
bsalomon16921ec2015-07-30 15:34:56 -0700138 for (int j = 0; j < h; ++j) {
139 for (int i = 0; i < w; ++i) {
140 uint32_t orig = origData[j * w + i];
141 uint32_t read = readData[j * w + i];
142
143 if (!checker(orig, read, error)) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400144 ERRORF(reporter, "Original 0x%08x, read back as 0x%08x in %s at %d, %d).", orig,
145 read, subtestName, i, j);
bsalomon16921ec2015-07-30 15:34:56 -0700146 return;
147 }
148 }
149 }
150}
151
Brian Salomonf802e752018-02-13 17:13:31 -0500152namespace {
153enum class Encoding {
154 kUntagged,
155 kLinear,
156 kSRGB,
157};
158}
159
160static sk_sp<SkColorSpace> encoding_as_color_space(Encoding encoding) {
161 switch (encoding) {
162 case Encoding::kUntagged: return nullptr;
163 case Encoding::kLinear: return SkColorSpace::MakeSRGBLinear();
164 case Encoding::kSRGB: return SkColorSpace::MakeSRGB();
165 }
166 return nullptr;
167}
168
Brian Salomonf802e752018-02-13 17:13:31 -0500169static const char* encoding_as_str(Encoding encoding) {
170 switch (encoding) {
171 case Encoding::kUntagged: return "untagged";
172 case Encoding::kLinear: return "linear";
173 case Encoding::kSRGB: return "sRGB";
174 }
175 return nullptr;
176}
177
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500178static constexpr int kW = 255;
179static constexpr int kH = 255;
180
181static std::unique_ptr<uint32_t[]> make_data() {
182 std::unique_ptr<uint32_t[]> data(new uint32_t[kW * kH]);
bsalomon16921ec2015-07-30 15:34:56 -0700183 for (int j = 0; j < kH; ++j) {
184 for (int i = 0; i < kW; ++i) {
Brian Osmanf494bd22018-07-12 10:47:01 -0400185 data[j * kW + i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
bsalomon16921ec2015-07-30 15:34:56 -0700186 }
187 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500188 return data;
189}
bsalomon16921ec2015-07-30 15:34:56 -0700190
Brian Salomonbf6b9792019-08-21 09:38:10 -0400191static std::unique_ptr<GrSurfaceContext> make_surface_context(Encoding contextEncoding,
192 GrContext* context,
193 skiatest::Reporter* reporter) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500194 auto surfaceContext = GrRenderTargetContext::Make(
195 context, GrColorType::kRGBA_8888, encoding_as_color_space(contextEncoding),
196 SkBackingFit::kExact, {kW, kH}, 1, GrMipMapped::kNo, GrProtected::kNo,
197 kBottomLeft_GrSurfaceOrigin, SkBudgeted::kNo);
Brian Salomonf802e752018-02-13 17:13:31 -0500198 if (!surfaceContext) {
199 ERRORF(reporter, "Could not create %s surface context.", encoding_as_str(contextEncoding));
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500200 }
Mike Kleina9609ea2020-02-18 13:33:23 -0600201 return std::move(surfaceContext);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500202}
203
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500204static void test_write_read(Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding,
205 float error, CheckFn check, GrContext* context,
206 skiatest::Reporter* reporter) {
207 auto surfaceContext = make_surface_context(contextEncoding, context, reporter);
208 if (!surfaceContext) {
209 return;
210 }
211 auto writeII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
212 encoding_as_color_space(writeEncoding));
213 auto data = make_data();
Brian Salomon1d435302019-07-01 13:05:28 -0400214 if (!surfaceContext->writePixels(writeII, data.get(), 0, {0, 0})) {
Brian Salomonf802e752018-02-13 17:13:31 -0500215 ERRORF(reporter, "Could not write %s to %s surface context.",
216 encoding_as_str(writeEncoding), encoding_as_str(contextEncoding));
217 return;
218 }
219
220 auto readII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
221 encoding_as_color_space(readEncoding));
222 SkString testName;
223 testName.printf("write %s data to a %s context and read as %s.", encoding_as_str(writeEncoding),
224 encoding_as_str(contextEncoding), encoding_as_str(readEncoding));
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500225 read_and_check_pixels(reporter, surfaceContext.get(), data.get(), readII, check, error,
Brian Salomonf802e752018-02-13 17:13:31 -0500226 testName.c_str());
227}
228
229// Test all combinations of writePixels/readPixels where the surface context/write source/read dst
230// are sRGB, linear, or untagged RGBA_8888.
231DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400232 auto context = ctxInfo.directContext();
Greg Daniel0258c902019-08-01 13:08:33 -0400233 if (!context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888_SRGB,
234 GrRenderable::kNo).isValid()) {
Brian Salomonf802e752018-02-13 17:13:31 -0500235 return;
236 }
237 // We allow more error on GPUs with lower precision shader variables.
Robert Phillips9da87e02019-02-04 13:26:26 -0500238 float error = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
Brian Salomonf802e752018-02-13 17:13:31 -0500239 // For the all-sRGB case, we allow a small error only for devices that have
240 // precision variation because the sRGB data gets converted to linear and back in
241 // the shader.
Robert Phillips9da87e02019-02-04 13:26:26 -0500242 float smallError = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.0f : 1.f;
Brian Salomonf802e752018-02-13 17:13:31 -0500243
244 ///////////////////////////////////////////////////////////////////////////////////////////////
245 // Write sRGB data to a sRGB context - no conversion on the write.
246
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400247 // back to sRGB - no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500248 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kSRGB, smallError,
249 check_no_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400250 // Reading back to untagged should be a pass through with no conversion.
251 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kUntagged, error,
252 check_no_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400253
Brian Salomonf802e752018-02-13 17:13:31 -0500254 // Converts back to linear
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500255 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kLinear, error,
256 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500257
Mike Klein12d4b6e2018-08-17 14:09:55 -0400258 // Untagged source data should be interpreted as sRGB.
259 test_write_read(Encoding::kSRGB, Encoding::kUntagged, Encoding::kSRGB, smallError,
260 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500261
262 ///////////////////////////////////////////////////////////////////////////////////////////////
263 // Write linear data to a sRGB context. It gets converted to sRGB on write. The reads
264 // are all the same as the above cases where the original data was untagged.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500265 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kSRGB, error,
266 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400267 // When the dst buffer is untagged there should be no conversion on the read.
268 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kUntagged, error,
269 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500270 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kLinear, error,
271 check_linear_to_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500272
273 ///////////////////////////////////////////////////////////////////////////////////////////////
274 // Write data to an untagged context. The write does no conversion no matter what encoding the
275 // src data has.
276 for (auto writeEncoding : {Encoding::kSRGB, Encoding::kUntagged, Encoding::kLinear}) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400277 // The read from untagged to sRGB also does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500278 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kSRGB, error,
279 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500280 // Reading untagged back as untagged should do no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500281 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kUntagged, error,
282 check_no_conversion, context, reporter);
Brian Osman4f98dbe2019-03-28 11:49:01 -0400283 // Reading untagged back as linear does convert (context is source, so treated as sRGB),
284 // dst is tagged.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500285 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kLinear, error,
Brian Osman4f98dbe2019-03-28 11:49:01 -0400286 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500287 }
288
289 ///////////////////////////////////////////////////////////////////////////////////////////////
290 // Write sRGB data to a linear context - converts to sRGB on the write.
291
292 // converts back to sRGB on read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500293 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kSRGB, error,
294 check_srgb_to_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400295 // Reading untagged data from linear currently does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500296 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kUntagged, error,
297 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500298 // Stays linear when read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500299 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kLinear, error,
300 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500301
Mike Klein12d4b6e2018-08-17 14:09:55 -0400302 // Untagged source data should be interpreted as sRGB.
303 test_write_read(Encoding::kLinear, Encoding::kUntagged, Encoding::kSRGB, error,
304 check_srgb_to_linear_to_srgb_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500305
306 ///////////////////////////////////////////////////////////////////////////////////////////////
307 // Write linear data to a linear context. Does no conversion.
308
309 // Reading to sRGB does a conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500310 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kSRGB, error,
311 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400312 // Reading to untagged does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500313 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kUntagged, error,
314 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500315 // Stays linear when read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500316 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kLinear, error,
317 check_no_conversion, context, reporter);
bsalomon16921ec2015-07-30 15:34:56 -0700318}