blob: a52e96104f8b186d3f43f545ca58d73c134d2822 [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
115typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
116
Robert Phillips301431d2017-03-29 12:08:49 -0400117void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
118 uint32_t* origData,
119 const SkImageInfo& dstInfo, CheckFn checker, float error,
bsalomon16921ec2015-07-30 15:34:56 -0700120 const char* subtestName) {
Robert Phillips301431d2017-03-29 12:08:49 -0400121 int w = dstInfo.width();
122 int h = dstInfo.height();
bsalomon16921ec2015-07-30 15:34:56 -0700123 SkAutoTMalloc<uint32_t> readData(w * h);
124 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -0400125
126 if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
bsalomon16921ec2015-07-30 15:34:56 -0700127 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
128 return;
129 }
Robert Phillips301431d2017-03-29 12:08:49 -0400130
bsalomon16921ec2015-07-30 15:34:56 -0700131 for (int j = 0; j < h; ++j) {
132 for (int i = 0; i < w; ++i) {
133 uint32_t orig = origData[j * w + i];
134 uint32_t read = readData[j * w + i];
135
136 if (!checker(orig, read, error)) {
137 ERRORF(reporter, "Expected 0x%08x, read back as 0x%08x in %s at %d, %d).",
138 orig, read, subtestName, i, j);
139 return;
140 }
141 }
142 }
143}
144
145// TODO: Add tests for copySurface between srgb/linear textures. Add tests for unpremul/premul
146// conversion during read/write along with srgb/linear conversions.
egdanielab527a52016-06-28 08:07:26 -0700147DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700148 GrContext* context = ctxInfo.grContext();
benjaminwagner7d974f52015-10-19 13:55:55 -0700149#if defined(GOOGLE3)
150 // Stack frame size is limited in GOOGLE3.
151 static const int kW = 63;
152 static const int kH = 63;
153#else
bsalomon16921ec2015-07-30 15:34:56 -0700154 static const int kW = 255;
155 static const int kH = 255;
benjaminwagner7d974f52015-10-19 13:55:55 -0700156#endif
bsalomon16921ec2015-07-30 15:34:56 -0700157 uint32_t origData[kW * kH];
158 for (int j = 0; j < kH; ++j) {
159 for (int i = 0; i < kW; ++i) {
160 origData[j * kW + i] = (j << 24) | (i << 16) | (i << 8) | i;
161 }
162 }
163
Robert Phillips301431d2017-03-29 12:08:49 -0400164 const SkImageInfo iiSRGBA = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType,
165 kPremul_SkAlphaType,
166 SkColorSpace::MakeSRGB());
167 const SkImageInfo iiRGBA = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType,
168 kPremul_SkAlphaType);
kkinnunen15302832015-12-01 04:35:26 -0800169 GrSurfaceDesc desc;
170 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipse78b7252017-04-06 07:59:41 -0400171 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
kkinnunen15302832015-12-01 04:35:26 -0800172 desc.fWidth = kW;
173 desc.fHeight = kH;
174 desc.fConfig = kSRGBA_8888_GrPixelConfig;
175 if (context->caps()->isConfigRenderable(desc.fConfig, false) &&
176 context->caps()->isConfigTexturable(desc.fConfig)) {
Robert Phillips301431d2017-03-29 12:08:49 -0400177
178 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
179 desc, SkBackingFit::kExact,
180 SkBudgeted::kNo);
181 if (!sContext) {
182 ERRORF(reporter, "Could not create SRGBA surface context.");
kkinnunen15302832015-12-01 04:35:26 -0800183 return;
bsalomon16921ec2015-07-30 15:34:56 -0700184 }
185
kkinnunen15302832015-12-01 04:35:26 -0800186 float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f;
bsalomon16921ec2015-07-30 15:34:56 -0700187
kkinnunen15302832015-12-01 04:35:26 -0800188 // Write srgba data and read as srgba and then as rgba
Robert Phillips301431d2017-03-29 12:08:49 -0400189 if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) {
kkinnunen15302832015-12-01 04:35:26 -0800190 // For the all-srgba case, we allow a small error only for devices that have
191 // precision variation because the srgba data gets converted to linear and back in
192 // the shader.
Robert Phillips301431d2017-03-29 12:08:49 -0400193 float smallError = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.f : 0.0f;
194 read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800195 check_srgb_to_linear_to_srgb_conversion, smallError,
196 "write/read srgba to srgba texture");
Robert Phillips301431d2017-03-29 12:08:49 -0400197 read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800198 check_srgb_to_linear_conversion, error,
199 "write srgba/read rgba with srgba texture");
200 } else {
201 ERRORF(reporter, "Could not write srgba data to srgba texture.");
202 }
bsalomon16921ec2015-07-30 15:34:56 -0700203
kkinnunen15302832015-12-01 04:35:26 -0800204 // Now verify that we can write linear data
Robert Phillips301431d2017-03-29 12:08:49 -0400205 if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) {
kkinnunen15302832015-12-01 04:35:26 -0800206 // We allow more error on GPUs with lower precision shader variables.
Robert Phillips301431d2017-03-29 12:08:49 -0400207 read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800208 check_linear_to_srgb_conversion, error,
209 "write rgba/read srgba with srgba texture");
Robert Phillips301431d2017-03-29 12:08:49 -0400210 read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800211 check_linear_to_srgb_to_linear_conversion, error,
212 "write/read rgba with srgba texture");
213 } else {
214 ERRORF(reporter, "Could not write rgba data to srgba texture.");
215 }
bsalomon16921ec2015-07-30 15:34:56 -0700216
kkinnunen15302832015-12-01 04:35:26 -0800217 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips301431d2017-03-29 12:08:49 -0400218 sContext = context->contextPriv().makeDeferredSurfaceContext(desc, SkBackingFit::kExact,
219 SkBudgeted::kNo);
220 if (!sContext) {
221 ERRORF(reporter, "Could not create RGBA surface context.");
kkinnunen15302832015-12-01 04:35:26 -0800222 return;
223 }
bsalomon16921ec2015-07-30 15:34:56 -0700224
kkinnunen15302832015-12-01 04:35:26 -0800225 // Write srgba data to a rgba texture and read back as srgba and rgba
Robert Phillips301431d2017-03-29 12:08:49 -0400226 if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) {
Brian Osman7ab6a7f2017-04-25 15:01:46 -0400227#if 0
228 // We don't support this conversion (read from untagged source into tagged destination.
229 // If we decide there is a meaningful way to implement this, restore this test.
Robert Phillips301431d2017-03-29 12:08:49 -0400230 read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800231 check_srgb_to_linear_to_srgb_conversion, error,
232 "write/read srgba to rgba texture");
Brian Osman7ab6a7f2017-04-25 15:01:46 -0400233#endif
234 // We expect the sRGB -> linear write to do no sRGB conversion (to match the behavior of
235 // drawing tagged sources). skbug.com/6547. So the data we read should still contain
236 // sRGB encoded values.
237 //
238 // srgb_to_linear_to_srgb is a proxy for the expected identity transform.
Robert Phillips301431d2017-03-29 12:08:49 -0400239 read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
Brian Osman7ab6a7f2017-04-25 15:01:46 -0400240 check_srgb_to_linear_to_srgb_conversion, error,
kkinnunen15302832015-12-01 04:35:26 -0800241 "write srgba/read rgba to rgba texture");
242 } else {
243 ERRORF(reporter, "Could not write srgba data to rgba texture.");
244 }
bsalomon16921ec2015-07-30 15:34:56 -0700245
kkinnunen15302832015-12-01 04:35:26 -0800246 // Write rgba data to a rgba texture and read back as srgba
Robert Phillips301431d2017-03-29 12:08:49 -0400247 if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) {
248 read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800249 check_linear_to_srgb_conversion, 1.2f,
250 "write rgba/read srgba to rgba texture");
251 } else {
252 ERRORF(reporter, "Could not write rgba data to rgba texture.");
253 }
bsalomon16921ec2015-07-30 15:34:56 -0700254 }
255}
256#endif