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