blob: f0e747a40065e19fadda8a7def94b1869d68ca47 [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) {
143 static const int kW = 255;
144 static const int kH = 255;
145 uint32_t origData[kW * kH];
146 for (int j = 0; j < kH; ++j) {
147 for (int i = 0; i < kW; ++i) {
148 origData[j * kW + i] = (j << 24) | (i << 16) | (i << 8) | i;
149 }
150 }
151
152 for (int t = 0; t < GrContextFactory::kGLContextTypeCnt; ++t) {
153 GrContextFactory::GLContextType glType = (GrContextFactory::GLContextType) t;
154 GrContext* context;
155 // We allow more error on GPUs with lower precision shader variables.
156 if (!GrContextFactory::IsRenderingGLContext(glType) || !(context = factory->get(glType))) {
157 continue;
158 }
159
160 GrSurfaceDesc desc;
161 desc.fFlags = kRenderTarget_GrSurfaceFlag;
162 desc.fWidth = kW;
163 desc.fHeight = kH;
164 desc.fConfig = kSRGBA_8888_GrPixelConfig;
165 if (context->caps()->isConfigRenderable(desc.fConfig, false) &&
166 context->caps()->isConfigTexturable(desc.fConfig)) {
167 SkAutoTUnref<GrTexture> tex(context->textureProvider()->createTexture(desc, false));
168 if (!tex) {
169 ERRORF(reporter, "Could not create SRGBA texture.");
170 continue;
171 }
172
173 float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f;
174
175 // Write srgba data and read as srgba and then as rgba
176 if (tex->writePixels(0, 0, kW, kH, kSRGBA_8888_GrPixelConfig, origData)) {
177 // For the all-srgba case, we allow a small error only for devices that have
178 // precision variation because the srgba data gets converted to linear and back in
179 // the shader.
180 float smallError = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.f :
181 0.0f;
182 read_and_check_pixels(reporter, tex, origData, kSRGBA_8888_GrPixelConfig,
183 check_srgb_to_linear_to_srgb_conversion, smallError,
184 "write/read srgba to srgba texture");
185 read_and_check_pixels(reporter, tex, origData, kRGBA_8888_GrPixelConfig,
186 check_srgb_to_linear_conversion, error,
187 "write srgba/read rgba with srgba texture");
188 } else {
189 ERRORF(reporter, "Could not write srgba data to srgba texture.");
190 }
191
192 // Now verify that we can write linear data
193 if (tex->writePixels(0, 0, kW, kH, kRGBA_8888_GrPixelConfig, origData)) {
194 // We allow more error on GPUs with lower precision shader variables.
195 read_and_check_pixels(reporter, tex, origData, kSRGBA_8888_GrPixelConfig,
196 check_linear_to_srgb_conversion, error,
197 "write rgba/read srgba with srgba texture");
198 read_and_check_pixels(reporter, tex, origData, kRGBA_8888_GrPixelConfig,
199 check_linear_to_srgb_to_linear_conversion, error,
200 "write/read rgba with srgba texture");
201 } else {
202 ERRORF(reporter, "Could not write rgba data to srgba texture.");
203 }
204
205 desc.fConfig = kRGBA_8888_GrPixelConfig;
206 tex.reset(context->textureProvider()->createTexture(desc, false));
207 if (!tex) {
208 ERRORF(reporter, "Could not create RGBA texture.");
209 continue;
210 }
211
212 // Write srgba data to a rgba texture and read back as srgba and rgba
213 if (tex->writePixels(0, 0, kW, kH, kSRGBA_8888_GrPixelConfig, origData)) {
214 read_and_check_pixels(reporter, tex, origData, kSRGBA_8888_GrPixelConfig,
215 check_srgb_to_linear_to_srgb_conversion, error,
216 "write/read srgba to rgba texture");
217 read_and_check_pixels(reporter, tex, origData, kRGBA_8888_GrPixelConfig,
218 check_srgb_to_linear_conversion, error,
219 "write srgba/read rgba to rgba texture");
220 } else {
221 ERRORF(reporter, "Could not write srgba data to rgba texture.");
222 }
223
224 // Write rgba data to a rgba texture and read back as srgba
225 if (tex->writePixels(0, 0, kW, kH, kRGBA_8888_GrPixelConfig, origData)) {
226 read_and_check_pixels(reporter, tex, origData, kSRGBA_8888_GrPixelConfig,
227 check_linear_to_srgb_conversion, 1.2f,
228 "write rgba/read srgba to rgba texture");
229 } else {
230 ERRORF(reporter, "Could not write rgba data to rgba texture.");
231 }
232}
233 }
234}
235#endif