blob: 840575a1c4e79b0981a125d31ad5076216028161 [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"
12#include "SkCanvas.h"
13#include "SkSurface.h"
bsalomon16921ec2015-07-30 15:34:56 -070014
15// using anonymous namespace because these functions are used as template params.
16namespace {
17/** convert 0..1 srgb value to 0..1 linear */
18float srgb_to_linear(float srgb) {
19 if (srgb <= 0.04045f) {
20 return srgb / 12.92f;
21 } else {
22 return powf((srgb + 0.055f) / 1.055f, 2.4f);
23 }
24}
25
26/** convert 0..1 linear value to 0..1 srgb */
27float linear_to_srgb(float linear) {
28 if (linear <= 0.0031308) {
29 return linear * 12.92f;
30 } else {
31 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
32 }
33}
34}
35
36/** tests a conversion with an error tolerance */
37template <float (*CONVERT)(float)> static bool check_conversion(uint32_t input, uint32_t output,
38 float error) {
39 // alpha should always be exactly preserved.
40 if ((input & 0xff000000) != (output & 0xff000000)) {
41 return false;
42 }
43
44 for (int c = 0; c < 3; ++c) {
45 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
46 float lower = SkTMax(0.f, (float) inputComponent - error);
47 float upper = SkTMin(255.f, (float) inputComponent + error);
48 lower = CONVERT(lower / 255.f);
49 upper = CONVERT(upper / 255.f);
50 SkASSERT(lower >= 0.f && lower <= 255.f);
51 SkASSERT(upper >= 0.f && upper <= 255.f);
52 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
53 if (outputComponent < SkScalarFloorToInt(lower * 255.f) ||
54 outputComponent > SkScalarCeilToInt(upper * 255.f)) {
55 return false;
56 }
57 }
58 return true;
59}
60
61/** tests a forward and backward conversion with an error tolerance */
62template <float (*FORWARD)(float), float (*BACKWARD)(float)>
63static bool check_double_conversion(uint32_t input, uint32_t output, float error) {
64 // alpha should always be exactly preserved.
65 if ((input & 0xff000000) != (output & 0xff000000)) {
66 return false;
67 }
68
69 for (int c = 0; c < 3; ++c) {
70 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
71 float lower = SkTMax(0.f, (float) inputComponent - error);
72 float upper = SkTMin(255.f, (float) inputComponent + error);
73 lower = FORWARD(lower / 255.f);
74 upper = FORWARD(upper / 255.f);
75 SkASSERT(lower >= 0.f && lower <= 255.f);
76 SkASSERT(upper >= 0.f && upper <= 255.f);
77 uint8_t upperComponent = SkScalarCeilToInt(upper * 255.f);
78 uint8_t lowerComponent = SkScalarFloorToInt(lower * 255.f);
79 lower = SkTMax(0.f, (float) lowerComponent - error);
80 upper = SkTMin(255.f, (float) upperComponent + error);
81 lower = BACKWARD(lowerComponent / 255.f);
82 upper = BACKWARD(upperComponent / 255.f);
83 SkASSERT(lower >= 0.f && lower <= 255.f);
84 SkASSERT(upper >= 0.f && upper <= 255.f);
85 upperComponent = SkScalarCeilToInt(upper * 255.f);
86 lowerComponent = SkScalarFloorToInt(lower * 255.f);
87
88 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
89 if (outputComponent < lowerComponent || outputComponent > upperComponent) {
90 return false;
91 }
92 }
93 return true;
94}
95
96static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error) {
97 return check_conversion<srgb_to_linear>(srgb, linear, error);
98}
99
100static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error) {
101 return check_conversion<linear_to_srgb>(linear, srgb, error);
102}
103
104static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error) {
105 return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
106}
107
108static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error) {
109 return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
110}
111
112typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
113
114void read_and_check_pixels(skiatest::Reporter* reporter, GrTexture* texture, uint32_t* origData,
115 GrPixelConfig readConfig, CheckFn checker, float error,
116 const char* subtestName) {
117 int w = texture->width();
118 int h = texture->height();
119 SkAutoTMalloc<uint32_t> readData(w * h);
120 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
121 if (!texture->readPixels(0, 0, w, h, readConfig, readData.get())) {
122 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
123 return;
124 }
125 for (int j = 0; j < h; ++j) {
126 for (int i = 0; i < w; ++i) {
127 uint32_t orig = origData[j * w + i];
128 uint32_t read = readData[j * w + i];
129
130 if (!checker(orig, read, error)) {
131 ERRORF(reporter, "Expected 0x%08x, read back as 0x%08x in %s at %d, %d).",
132 orig, read, subtestName, i, j);
133 return;
134 }
135 }
136 }
137}
138
139// TODO: Add tests for copySurface between srgb/linear textures. Add tests for unpremul/premul
140// conversion during read/write along with srgb/linear conversions.
egdanielab527a52016-06-28 08:07:26 -0700141DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700142 GrContext* context = ctxInfo.grContext();
benjaminwagner7d974f52015-10-19 13:55:55 -0700143#if defined(GOOGLE3)
144 // Stack frame size is limited in GOOGLE3.
145 static const int kW = 63;
146 static const int kH = 63;
147#else
bsalomon16921ec2015-07-30 15:34:56 -0700148 static const int kW = 255;
149 static const int kH = 255;
benjaminwagner7d974f52015-10-19 13:55:55 -0700150#endif
bsalomon16921ec2015-07-30 15:34:56 -0700151 uint32_t origData[kW * kH];
152 for (int j = 0; j < kH; ++j) {
153 for (int i = 0; i < kW; ++i) {
154 origData[j * kW + i] = (j << 24) | (i << 16) | (i << 8) | i;
155 }
156 }
157
kkinnunen15302832015-12-01 04:35:26 -0800158 GrSurfaceDesc desc;
159 desc.fFlags = kRenderTarget_GrSurfaceFlag;
160 desc.fWidth = kW;
161 desc.fHeight = kH;
162 desc.fConfig = kSRGBA_8888_GrPixelConfig;
163 if (context->caps()->isConfigRenderable(desc.fConfig, false) &&
164 context->caps()->isConfigTexturable(desc.fConfig)) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800165 SkAutoTUnref<GrTexture> tex(context->textureProvider()->createTexture(
166 desc, SkBudgeted::kNo));
kkinnunen15302832015-12-01 04:35:26 -0800167 if (!tex) {
168 ERRORF(reporter, "Could not create SRGBA texture.");
169 return;
bsalomon16921ec2015-07-30 15:34:56 -0700170 }
171
kkinnunen15302832015-12-01 04:35:26 -0800172 float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f;
bsalomon16921ec2015-07-30 15:34:56 -0700173
kkinnunen15302832015-12-01 04:35:26 -0800174 // Write srgba data and read as srgba and then as rgba
175 if (tex->writePixels(0, 0, kW, kH, kSRGBA_8888_GrPixelConfig, origData)) {
176 // For the all-srgba case, we allow a small error only for devices that have
177 // precision variation because the srgba data gets converted to linear and back in
178 // the shader.
179 float smallError = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.f :
180 0.0f;
181 read_and_check_pixels(reporter, tex, origData, kSRGBA_8888_GrPixelConfig,
182 check_srgb_to_linear_to_srgb_conversion, smallError,
183 "write/read srgba to srgba texture");
184 read_and_check_pixels(reporter, tex, origData, kRGBA_8888_GrPixelConfig,
185 check_srgb_to_linear_conversion, error,
186 "write srgba/read rgba with srgba texture");
187 } else {
188 ERRORF(reporter, "Could not write srgba data to srgba texture.");
189 }
bsalomon16921ec2015-07-30 15:34:56 -0700190
kkinnunen15302832015-12-01 04:35:26 -0800191 // Now verify that we can write linear data
192 if (tex->writePixels(0, 0, kW, kH, kRGBA_8888_GrPixelConfig, origData)) {
193 // We allow more error on GPUs with lower precision shader variables.
194 read_and_check_pixels(reporter, tex, origData, kSRGBA_8888_GrPixelConfig,
195 check_linear_to_srgb_conversion, error,
196 "write rgba/read srgba with srgba texture");
197 read_and_check_pixels(reporter, tex, origData, kRGBA_8888_GrPixelConfig,
198 check_linear_to_srgb_to_linear_conversion, error,
199 "write/read rgba with srgba texture");
200 } else {
201 ERRORF(reporter, "Could not write rgba data to srgba texture.");
202 }
bsalomon16921ec2015-07-30 15:34:56 -0700203
kkinnunen15302832015-12-01 04:35:26 -0800204 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon5ec26ae2016-02-25 08:33:02 -0800205 tex.reset(context->textureProvider()->createTexture(desc, SkBudgeted::kNo));
kkinnunen15302832015-12-01 04:35:26 -0800206 if (!tex) {
207 ERRORF(reporter, "Could not create RGBA texture.");
208 return;
209 }
bsalomon16921ec2015-07-30 15:34:56 -0700210
kkinnunen15302832015-12-01 04:35:26 -0800211 // Write srgba data to a rgba texture and read back as srgba and rgba
212 if (tex->writePixels(0, 0, kW, kH, kSRGBA_8888_GrPixelConfig, origData)) {
213 read_and_check_pixels(reporter, tex, origData, kSRGBA_8888_GrPixelConfig,
214 check_srgb_to_linear_to_srgb_conversion, error,
215 "write/read srgba to rgba texture");
216 read_and_check_pixels(reporter, tex, origData, kRGBA_8888_GrPixelConfig,
217 check_srgb_to_linear_conversion, error,
218 "write srgba/read rgba to rgba texture");
219 } else {
220 ERRORF(reporter, "Could not write srgba data to rgba texture.");
221 }
bsalomon16921ec2015-07-30 15:34:56 -0700222
kkinnunen15302832015-12-01 04:35:26 -0800223 // Write rgba data to a rgba texture and read back as srgba
224 if (tex->writePixels(0, 0, kW, kH, kRGBA_8888_GrPixelConfig, origData)) {
225 read_and_check_pixels(reporter, tex, origData, kSRGBA_8888_GrPixelConfig,
226 check_linear_to_srgb_conversion, 1.2f,
227 "write rgba/read srgba to rgba texture");
228 } else {
229 ERRORF(reporter, "Could not write rgba data to rgba texture.");
230 }
bsalomon16921ec2015-07-30 15:34:56 -0700231 }
232}
233#endif