blob: 0bb7694679708cafe9c969300112e895dc91f4ec [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"
bsalomon16921ec2015-07-30 15:34:56 -07009#include "GrCaps.h"
kkinnunen15302832015-12-01 04:35:26 -080010#include "GrContext.h"
Robert Phillips301431d2017-03-29 12:08:49 -040011#include "GrContextPriv.h"
Robert Phillips301431d2017-03-29 12:08:49 -040012#include "GrSurfaceContext.h"
kkinnunen15302832015-12-01 04:35:26 -080013#include "SkCanvas.h"
Robert Phillips301431d2017-03-29 12:08:49 -040014#include "SkGr.h"
kkinnunen15302832015-12-01 04:35:26 -080015#include "SkSurface.h"
bsalomon16921ec2015-07-30 15:34:56 -070016
17// using anonymous namespace because these functions are used as template params.
18namespace {
19/** convert 0..1 srgb value to 0..1 linear */
20float srgb_to_linear(float srgb) {
21 if (srgb <= 0.04045f) {
22 return srgb / 12.92f;
23 } else {
24 return powf((srgb + 0.055f) / 1.055f, 2.4f);
25 }
26}
27
28/** convert 0..1 linear value to 0..1 srgb */
29float linear_to_srgb(float linear) {
30 if (linear <= 0.0031308) {
31 return linear * 12.92f;
32 } else {
33 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
34 }
35}
36}
37
38/** tests a conversion with an error tolerance */
39template <float (*CONVERT)(float)> static bool check_conversion(uint32_t input, uint32_t output,
40 float error) {
41 // alpha should always be exactly preserved.
42 if ((input & 0xff000000) != (output & 0xff000000)) {
43 return false;
44 }
45
46 for (int c = 0; c < 3; ++c) {
47 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
48 float lower = SkTMax(0.f, (float) inputComponent - error);
49 float upper = SkTMin(255.f, (float) inputComponent + error);
50 lower = CONVERT(lower / 255.f);
51 upper = CONVERT(upper / 255.f);
52 SkASSERT(lower >= 0.f && lower <= 255.f);
53 SkASSERT(upper >= 0.f && upper <= 255.f);
54 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
55 if (outputComponent < SkScalarFloorToInt(lower * 255.f) ||
56 outputComponent > SkScalarCeilToInt(upper * 255.f)) {
57 return false;
58 }
59 }
60 return true;
61}
62
63/** tests a forward and backward conversion with an error tolerance */
64template <float (*FORWARD)(float), float (*BACKWARD)(float)>
65static bool check_double_conversion(uint32_t input, uint32_t output, float error) {
66 // alpha should always be exactly preserved.
67 if ((input & 0xff000000) != (output & 0xff000000)) {
68 return false;
69 }
70
71 for (int c = 0; c < 3; ++c) {
72 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
73 float lower = SkTMax(0.f, (float) inputComponent - error);
74 float upper = SkTMin(255.f, (float) inputComponent + error);
75 lower = FORWARD(lower / 255.f);
76 upper = FORWARD(upper / 255.f);
77 SkASSERT(lower >= 0.f && lower <= 255.f);
78 SkASSERT(upper >= 0.f && upper <= 255.f);
79 uint8_t upperComponent = SkScalarCeilToInt(upper * 255.f);
80 uint8_t lowerComponent = SkScalarFloorToInt(lower * 255.f);
81 lower = SkTMax(0.f, (float) lowerComponent - error);
82 upper = SkTMin(255.f, (float) upperComponent + error);
83 lower = BACKWARD(lowerComponent / 255.f);
84 upper = BACKWARD(upperComponent / 255.f);
85 SkASSERT(lower >= 0.f && lower <= 255.f);
86 SkASSERT(upper >= 0.f && upper <= 255.f);
87 upperComponent = SkScalarCeilToInt(upper * 255.f);
88 lowerComponent = SkScalarFloorToInt(lower * 255.f);
89
90 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
91 if (outputComponent < lowerComponent || outputComponent > upperComponent) {
92 return false;
93 }
94 }
95 return true;
96}
97
98static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error) {
99 return check_conversion<srgb_to_linear>(srgb, linear, error);
100}
101
102static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error) {
103 return check_conversion<linear_to_srgb>(linear, srgb, error);
104}
105
106static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error) {
107 return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
108}
109
110static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error) {
111 return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
112}
113
Brian Salomonf802e752018-02-13 17:13:31 -0500114static bool check_no_conversion(uint32_t input, uint32_t output, float error) {
115 // This is a bit of a hack to check identity transformations that may lose precision.
116 return check_srgb_to_linear_to_srgb_conversion(input, output, error);
117}
118
bsalomon16921ec2015-07-30 15:34:56 -0700119typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
120
Robert Phillips301431d2017-03-29 12:08:49 -0400121void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
122 uint32_t* origData,
123 const SkImageInfo& dstInfo, CheckFn checker, float error,
bsalomon16921ec2015-07-30 15:34:56 -0700124 const char* subtestName) {
Robert Phillips301431d2017-03-29 12:08:49 -0400125 int w = dstInfo.width();
126 int h = dstInfo.height();
bsalomon16921ec2015-07-30 15:34:56 -0700127 SkAutoTMalloc<uint32_t> readData(w * h);
128 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -0400129
130 if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
bsalomon16921ec2015-07-30 15:34:56 -0700131 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
132 return;
133 }
Robert Phillips301431d2017-03-29 12:08:49 -0400134
bsalomon16921ec2015-07-30 15:34:56 -0700135 for (int j = 0; j < h; ++j) {
136 for (int i = 0; i < w; ++i) {
137 uint32_t orig = origData[j * w + i];
138 uint32_t read = readData[j * w + i];
139
140 if (!checker(orig, read, error)) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400141 ERRORF(reporter, "Original 0x%08x, read back as 0x%08x in %s at %d, %d).", orig,
142 read, subtestName, i, j);
bsalomon16921ec2015-07-30 15:34:56 -0700143 return;
144 }
145 }
146 }
147}
148
Brian Salomonf802e752018-02-13 17:13:31 -0500149namespace {
150enum class Encoding {
151 kUntagged,
152 kLinear,
153 kSRGB,
154};
155}
156
157static sk_sp<SkColorSpace> encoding_as_color_space(Encoding encoding) {
158 switch (encoding) {
159 case Encoding::kUntagged: return nullptr;
160 case Encoding::kLinear: return SkColorSpace::MakeSRGBLinear();
161 case Encoding::kSRGB: return SkColorSpace::MakeSRGB();
162 }
163 return nullptr;
164}
165
166static GrPixelConfig encoding_as_pixel_config(Encoding encoding) {
167 switch (encoding) {
168 case Encoding::kUntagged: return kRGBA_8888_GrPixelConfig;
169 case Encoding::kLinear: return kRGBA_8888_GrPixelConfig;
170 case Encoding::kSRGB: return kSRGBA_8888_GrPixelConfig;
171 }
172 return kUnknown_GrPixelConfig;
173}
174
175static const char* encoding_as_str(Encoding encoding) {
176 switch (encoding) {
177 case Encoding::kUntagged: return "untagged";
178 case Encoding::kLinear: return "linear";
179 case Encoding::kSRGB: return "sRGB";
180 }
181 return nullptr;
182}
183
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500184static constexpr int kW = 255;
185static constexpr int kH = 255;
186
187static std::unique_ptr<uint32_t[]> make_data() {
188 std::unique_ptr<uint32_t[]> data(new uint32_t[kW * kH]);
bsalomon16921ec2015-07-30 15:34:56 -0700189 for (int j = 0; j < kH; ++j) {
190 for (int i = 0; i < kW; ++i) {
Brian Osmanf494bd22018-07-12 10:47:01 -0400191 data[j * kW + i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
bsalomon16921ec2015-07-30 15:34:56 -0700192 }
193 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500194 return data;
195}
bsalomon16921ec2015-07-30 15:34:56 -0700196
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500197static sk_sp<GrSurfaceContext> make_surface_context(Encoding contextEncoding, GrContext* context,
198 skiatest::Reporter* reporter) {
kkinnunen15302832015-12-01 04:35:26 -0800199 GrSurfaceDesc desc;
200 desc.fFlags = kRenderTarget_GrSurfaceFlag;
201 desc.fWidth = kW;
202 desc.fHeight = kH;
Brian Salomonf802e752018-02-13 17:13:31 -0500203 desc.fConfig = encoding_as_pixel_config(contextEncoding);
bsalomon16921ec2015-07-30 15:34:56 -0700204
Brian Salomonf802e752018-02-13 17:13:31 -0500205 auto surfaceContext = context->contextPriv().makeDeferredSurfaceContext(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500206 desc, kBottomLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
207 SkBudgeted::kNo, encoding_as_color_space(contextEncoding));
Brian Salomonf802e752018-02-13 17:13:31 -0500208 if (!surfaceContext) {
209 ERRORF(reporter, "Could not create %s surface context.", encoding_as_str(contextEncoding));
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500210 }
211 return surfaceContext;
212}
213
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500214static void test_write_read(Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding,
215 float error, CheckFn check, GrContext* context,
216 skiatest::Reporter* reporter) {
217 auto surfaceContext = make_surface_context(contextEncoding, context, reporter);
218 if (!surfaceContext) {
219 return;
220 }
221 auto writeII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
222 encoding_as_color_space(writeEncoding));
223 auto data = make_data();
224 if (!surfaceContext->writePixels(writeII, data.get(), 0, 0, 0)) {
Brian Salomonf802e752018-02-13 17:13:31 -0500225 ERRORF(reporter, "Could not write %s to %s surface context.",
226 encoding_as_str(writeEncoding), encoding_as_str(contextEncoding));
227 return;
228 }
229
230 auto readII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
231 encoding_as_color_space(readEncoding));
232 SkString testName;
233 testName.printf("write %s data to a %s context and read as %s.", encoding_as_str(writeEncoding),
234 encoding_as_str(contextEncoding), encoding_as_str(readEncoding));
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500235 read_and_check_pixels(reporter, surfaceContext.get(), data.get(), readII, check, error,
Brian Salomonf802e752018-02-13 17:13:31 -0500236 testName.c_str());
237}
238
239// Test all combinations of writePixels/readPixels where the surface context/write source/read dst
240// are sRGB, linear, or untagged RGBA_8888.
241DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
242 GrContext* context = ctxInfo.grContext();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400243 if (!context->contextPriv().caps()->isConfigRenderable(kSRGBA_8888_GrPixelConfig) &&
244 !context->contextPriv().caps()->isConfigTexturable(kSRGBA_8888_GrPixelConfig)) {
Brian Salomonf802e752018-02-13 17:13:31 -0500245 return;
246 }
247 // We allow more error on GPUs with lower precision shader variables.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400248 float error = context->contextPriv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
Brian Salomonf802e752018-02-13 17:13:31 -0500249 // For the all-sRGB case, we allow a small error only for devices that have
250 // precision variation because the sRGB data gets converted to linear and back in
251 // the shader.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400252 float smallError = context->contextPriv().caps()->shaderCaps()->halfIs32Bits() ? 0.0f : 1.f;
Brian Salomonf802e752018-02-13 17:13:31 -0500253
254 ///////////////////////////////////////////////////////////////////////////////////////////////
255 // Write sRGB data to a sRGB context - no conversion on the write.
256
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400257 // back to sRGB - no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500258 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kSRGB, smallError,
259 check_no_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400260 // Reading back to untagged should be a pass through with no conversion.
261 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kUntagged, error,
262 check_no_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400263
Brian Salomonf802e752018-02-13 17:13:31 -0500264 // Converts back to linear
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500265 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kLinear, error,
266 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500267
Mike Klein12d4b6e2018-08-17 14:09:55 -0400268 // Untagged source data should be interpreted as sRGB.
269 test_write_read(Encoding::kSRGB, Encoding::kUntagged, Encoding::kSRGB, smallError,
270 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500271
272 ///////////////////////////////////////////////////////////////////////////////////////////////
273 // Write linear data to a sRGB context. It gets converted to sRGB on write. The reads
274 // are all the same as the above cases where the original data was untagged.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500275 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kSRGB, error,
276 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400277 // When the dst buffer is untagged there should be no conversion on the read.
278 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kUntagged, error,
279 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500280 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kLinear, error,
281 check_linear_to_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500282
283 ///////////////////////////////////////////////////////////////////////////////////////////////
284 // Write data to an untagged context. The write does no conversion no matter what encoding the
285 // src data has.
286 for (auto writeEncoding : {Encoding::kSRGB, Encoding::kUntagged, Encoding::kLinear}) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400287 // The read from untagged to sRGB also does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500288 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kSRGB, error,
289 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500290 // Reading untagged back as untagged should do no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500291 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kUntagged, error,
292 check_no_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400293 // Reading untagged back as linear does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500294 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kLinear, error,
295 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500296 }
297
298 ///////////////////////////////////////////////////////////////////////////////////////////////
299 // Write sRGB data to a linear context - converts to sRGB on the write.
300
301 // converts back to sRGB on read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500302 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kSRGB, error,
303 check_srgb_to_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400304 // Reading untagged data from linear currently does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500305 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kUntagged, error,
306 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500307 // Stays linear when read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500308 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kLinear, error,
309 check_srgb_to_linear_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500310
Mike Klein12d4b6e2018-08-17 14:09:55 -0400311 // Untagged source data should be interpreted as sRGB.
312 test_write_read(Encoding::kLinear, Encoding::kUntagged, Encoding::kSRGB, error,
313 check_srgb_to_linear_to_srgb_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500314
315 ///////////////////////////////////////////////////////////////////////////////////////////////
316 // Write linear data to a linear context. Does no conversion.
317
318 // Reading to sRGB does a conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500319 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kSRGB, error,
320 check_linear_to_srgb_conversion, context, reporter);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400321 // Reading to untagged does no conversion.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500322 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kUntagged, error,
323 check_no_conversion, context, reporter);
Brian Salomonf802e752018-02-13 17:13:31 -0500324 // Stays linear when read.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500325 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kLinear, error,
326 check_no_conversion, context, reporter);
bsalomon16921ec2015-07-30 15:34:56 -0700327}