blob: c9fd99662a40ac1eabf1b065b2e3a47e1f15ebe2 [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
8#include "Test.h"
9#if SK_SUPPORT_GPU
bsalomon16921ec2015-07-30 15:34:56 -070010#include "GrCaps.h"
kkinnunen15302832015-12-01 04:35:26 -080011#include "GrContext.h"
Robert Phillips301431d2017-03-29 12:08:49 -040012#include "GrContextPriv.h"
Robert Phillips301431d2017-03-29 12:08:49 -040013#include "GrSurfaceContext.h"
kkinnunen15302832015-12-01 04:35:26 -080014#include "SkCanvas.h"
Robert Phillips301431d2017-03-29 12:08:49 -040015#include "SkGr.h"
kkinnunen15302832015-12-01 04:35:26 -080016#include "SkSurface.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}
37}
38
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));
49 float lower = SkTMax(0.f, (float) inputComponent - error);
50 float upper = SkTMin(255.f, (float) inputComponent + error);
51 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));
74 float lower = SkTMax(0.f, (float) inputComponent - error);
75 float upper = SkTMin(255.f, (float) inputComponent + error);
76 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);
82 lower = SkTMax(0.f, (float) lowerComponent - error);
83 upper = SkTMin(255.f, (float) upperComponent + error);
84 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
Robert Phillips301431d2017-03-29 12:08:49 -0400122void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
123 uint32_t* origData,
124 const SkImageInfo& dstInfo, CheckFn checker, float error,
bsalomon16921ec2015-07-30 15:34:56 -0700125 const char* subtestName) {
Robert Phillips301431d2017-03-29 12:08:49 -0400126 int w = dstInfo.width();
127 int h = dstInfo.height();
bsalomon16921ec2015-07-30 15:34:56 -0700128 SkAutoTMalloc<uint32_t> readData(w * h);
129 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -0400130
131 if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
bsalomon16921ec2015-07-30 15:34:56 -0700132 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
133 return;
134 }
Robert Phillips301431d2017-03-29 12:08:49 -0400135
bsalomon16921ec2015-07-30 15:34:56 -0700136 for (int j = 0; j < h; ++j) {
137 for (int i = 0; i < w; ++i) {
138 uint32_t orig = origData[j * w + i];
139 uint32_t read = readData[j * w + i];
140
141 if (!checker(orig, read, error)) {
142 ERRORF(reporter, "Expected 0x%08x, read back as 0x%08x in %s at %d, %d).",
143 orig, read, subtestName, i, j);
144 return;
145 }
146 }
147 }
148}
149
Brian Salomonf802e752018-02-13 17:13:31 -0500150namespace {
151enum class Encoding {
152 kUntagged,
153 kLinear,
154 kSRGB,
155};
156}
157
158static sk_sp<SkColorSpace> encoding_as_color_space(Encoding encoding) {
159 switch (encoding) {
160 case Encoding::kUntagged: return nullptr;
161 case Encoding::kLinear: return SkColorSpace::MakeSRGBLinear();
162 case Encoding::kSRGB: return SkColorSpace::MakeSRGB();
163 }
164 return nullptr;
165}
166
167static GrPixelConfig encoding_as_pixel_config(Encoding encoding) {
168 switch (encoding) {
169 case Encoding::kUntagged: return kRGBA_8888_GrPixelConfig;
170 case Encoding::kLinear: return kRGBA_8888_GrPixelConfig;
171 case Encoding::kSRGB: return kSRGBA_8888_GrPixelConfig;
172 }
173 return kUnknown_GrPixelConfig;
174}
175
176static const char* encoding_as_str(Encoding encoding) {
177 switch (encoding) {
178 case Encoding::kUntagged: return "untagged";
179 case Encoding::kLinear: return "linear";
180 case Encoding::kSRGB: return "sRGB";
181 }
182 return nullptr;
183}
184
185static void do_test(Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding,
186 float error, CheckFn check, GrContext* context, skiatest::Reporter* reporter) {
Mike Klein6613cc52017-12-19 09:09:33 -0500187#if defined(SK_BUILD_FOR_GOOGLE3)
188 // Stack frame size is limited in SK_BUILD_FOR_GOOGLE3.
benjaminwagner7d974f52015-10-19 13:55:55 -0700189 static const int kW = 63;
190 static const int kH = 63;
191#else
bsalomon16921ec2015-07-30 15:34:56 -0700192 static const int kW = 255;
193 static const int kH = 255;
benjaminwagner7d974f52015-10-19 13:55:55 -0700194#endif
bsalomon16921ec2015-07-30 15:34:56 -0700195 uint32_t origData[kW * kH];
196 for (int j = 0; j < kH; ++j) {
197 for (int i = 0; i < kW; ++i) {
198 origData[j * kW + i] = (j << 24) | (i << 16) | (i << 8) | i;
199 }
200 }
201
kkinnunen15302832015-12-01 04:35:26 -0800202 GrSurfaceDesc desc;
203 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipse78b7252017-04-06 07:59:41 -0400204 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
kkinnunen15302832015-12-01 04:35:26 -0800205 desc.fWidth = kW;
206 desc.fHeight = kH;
Brian Salomonf802e752018-02-13 17:13:31 -0500207 desc.fConfig = encoding_as_pixel_config(contextEncoding);
bsalomon16921ec2015-07-30 15:34:56 -0700208
Brian Salomonf802e752018-02-13 17:13:31 -0500209 auto surfaceContext = context->contextPriv().makeDeferredSurfaceContext(
210 desc, GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kNo,
211 encoding_as_color_space(contextEncoding));
212 if (!surfaceContext) {
213 ERRORF(reporter, "Could not create %s surface context.", encoding_as_str(contextEncoding));
214 return;
bsalomon16921ec2015-07-30 15:34:56 -0700215 }
Brian Salomonf802e752018-02-13 17:13:31 -0500216 auto writeII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
217 encoding_as_color_space(writeEncoding));
218
219 if (!surfaceContext->writePixels(writeII, origData, 0, 0, 0)) {
220 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));
230 read_and_check_pixels(reporter, surfaceContext.get(), origData, readII, check, error,
231 testName.c_str());
232}
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) {
237 GrContext* context = ctxInfo.grContext();
238 if (!context->caps()->isConfigRenderable(kSRGBA_8888_GrPixelConfig) &&
239 !context->caps()->isConfigTexturable(kSRGBA_8888_GrPixelConfig)) {
240 return;
241 }
242 // We allow more error on GPUs with lower precision shader variables.
243 float error = context->caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
244 // 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.
247 float smallError = context->caps()->shaderCaps()->halfIs32Bits() ? 0.0f : 1.f;
248
249 ///////////////////////////////////////////////////////////////////////////////////////////////
250 // Write sRGB data to a sRGB context - no conversion on the write.
251
252 // back to sRGB no conversion
253 do_test(Encoding::kSRGB, Encoding::kSRGB, Encoding::kSRGB, smallError, check_no_conversion,
254 context, reporter);
255 // Untagged read from sRGB is treated as a conversion back to linear. TODO: Fail or don't
256 // convert?
257 do_test(Encoding::kSRGB, Encoding::kSRGB, Encoding::kUntagged, error,
258 check_srgb_to_linear_conversion, context, reporter);
259 // Converts back to linear
260 do_test(Encoding::kSRGB, Encoding::kSRGB, Encoding::kLinear, error,
261 check_srgb_to_linear_conversion, context, reporter);
262
263 ///////////////////////////////////////////////////////////////////////////////////////////////
264 // Write untagged data to a sRGB context - Currently this treats the untagged data as
265 // linear and converts to sRGB during the write. TODO: Fail or passthrough?
266
267 // read back to srgb, no additional conversion
268 do_test(Encoding::kSRGB, Encoding::kUntagged, Encoding::kSRGB, error,
269 check_linear_to_srgb_conversion, context, reporter);
270 // read back to untagged. Currently converts back to linear. TODO: Fail or don't convert?
271 do_test(Encoding::kSRGB, Encoding::kUntagged, Encoding::kUntagged, error,
272 check_linear_to_srgb_to_linear_conversion, context, reporter);
273 // Converts back to linear.
274 do_test(Encoding::kSRGB, Encoding::kUntagged, Encoding::kLinear, error,
275 check_linear_to_srgb_to_linear_conversion, context, reporter);
276
277 ///////////////////////////////////////////////////////////////////////////////////////////////
278 // Write linear data to a sRGB context. It gets converted to sRGB on write. The reads
279 // are all the same as the above cases where the original data was untagged.
280 do_test(Encoding::kSRGB, Encoding::kLinear, Encoding::kSRGB, error,
281 check_linear_to_srgb_conversion, context, reporter);
282 // TODO: Fail or don't convert?
283 do_test(Encoding::kSRGB, Encoding::kLinear, Encoding::kUntagged, error,
284 check_linear_to_srgb_to_linear_conversion, context, reporter);
285 do_test(Encoding::kSRGB, Encoding::kLinear, Encoding::kLinear, error,
286 check_linear_to_srgb_to_linear_conversion, context, reporter);
287
288 ///////////////////////////////////////////////////////////////////////////////////////////////
289 // Write data to an untagged context. The write does no conversion no matter what encoding the
290 // src data has.
291 for (auto writeEncoding : {Encoding::kSRGB, Encoding::kUntagged, Encoding::kLinear}) {
292 // Currently this converts to sRGB when we read. TODO: Should reading from an untagged
293 // context to sRGB fail or do no conversion?
294 do_test(Encoding::kUntagged, writeEncoding, Encoding::kSRGB, error,
295 check_linear_to_srgb_conversion, context, reporter);
296 // Reading untagged back as untagged should do no conversion.
297 do_test(Encoding::kUntagged, writeEncoding, Encoding::kUntagged, error, check_no_conversion,
298 context, reporter);
299 // Reading untagged back as linear does no conversion. TODO: Should it just fail?
300 do_test(Encoding::kUntagged, writeEncoding, Encoding::kLinear, error, check_no_conversion,
301 context, reporter);
302 }
303
304 ///////////////////////////////////////////////////////////////////////////////////////////////
305 // Write sRGB data to a linear context - converts to sRGB on the write.
306
307 // converts back to sRGB on read.
308 do_test(Encoding::kLinear, Encoding::kSRGB, Encoding::kSRGB, error,
309 check_srgb_to_linear_to_srgb_conversion, context, reporter);
310 // Reading untagged data from linear currently does no conversion. TODO: Should it fail?
311 do_test(Encoding::kLinear, Encoding::kSRGB, Encoding::kUntagged, error,
312 check_srgb_to_linear_conversion, context, reporter);
313 // Stays linear when read.
314 do_test(Encoding::kLinear, Encoding::kSRGB, Encoding::kLinear, error,
315 check_srgb_to_linear_conversion, context, reporter);
316
317 ///////////////////////////////////////////////////////////////////////////////////////////////
318 // Write untagged data to a linear context. Currently does no conversion. TODO: Should this
319 // fail?
320
321 // Reading to sRGB does a conversion.
322 do_test(Encoding::kLinear, Encoding::kUntagged, Encoding::kSRGB, error,
323 check_linear_to_srgb_conversion, context, reporter);
324 // Reading to untagged does no conversion. TODO: Should it fail?
325 do_test(Encoding::kLinear, Encoding::kUntagged, Encoding::kUntagged, error, check_no_conversion,
326 context, reporter);
327 // Stays linear when read.
328 do_test(Encoding::kLinear, Encoding::kUntagged, Encoding::kLinear, error, check_no_conversion,
329 context, reporter);
330
331 ///////////////////////////////////////////////////////////////////////////////////////////////
332 // Write linear data to a linear context. Does no conversion.
333
334 // Reading to sRGB does a conversion.
335 do_test(Encoding::kLinear, Encoding::kLinear, Encoding::kSRGB, error,
336 check_linear_to_srgb_conversion, context, reporter);
337 // Reading to untagged does no conversion. TODO: Should it fail?
338 do_test(Encoding::kLinear, Encoding::kLinear, Encoding::kUntagged, error, check_no_conversion,
339 context, reporter);
340 // Stays linear when read.
341 do_test(Encoding::kLinear, Encoding::kLinear, Encoding::kLinear, error, check_no_conversion,
342 context, reporter);
bsalomon16921ec2015-07-30 15:34:56 -0700343}
344#endif