1 /*
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 #include "GrCaps.h"
10 #include "GrContext.h"
11 #include "GrContextPriv.h"
12 #include "GrSurfaceContext.h"
13 #include "SkCanvas.h"
14 #include "SkGr.h"
15 #include "SkSurface.h"
16
17 // using anonymous namespace because these functions are used as template params.
18 namespace {
19 /** convert 0..1 srgb value to 0..1 linear */
srgb_to_linear(float srgb)20 float 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 */
linear_to_srgb(float linear)29 float 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 */
check_conversion(uint32_t input,uint32_t output,float error)39 template <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 */
64 template <float (*FORWARD)(float), float (*BACKWARD)(float)>
check_double_conversion(uint32_t input,uint32_t output,float error)65 static 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
check_srgb_to_linear_conversion(uint32_t srgb,uint32_t linear,float error)98 static 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
check_linear_to_srgb_conversion(uint32_t linear,uint32_t srgb,float error)102 static 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
check_linear_to_srgb_to_linear_conversion(uint32_t input,uint32_t output,float error)106 static 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
check_srgb_to_linear_to_srgb_conversion(uint32_t input,uint32_t output,float error)110 static 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
check_no_conversion(uint32_t input,uint32_t output,float error)114 static 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
119 typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
120
read_and_check_pixels(skiatest::Reporter * reporter,GrSurfaceContext * context,uint32_t * origData,const SkImageInfo & dstInfo,CheckFn checker,float error,const char * subtestName)121 void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
122 uint32_t* origData,
123 const SkImageInfo& dstInfo, CheckFn checker, float error,
124 const char* subtestName) {
125 int w = dstInfo.width();
126 int h = dstInfo.height();
127 SkAutoTMalloc<uint32_t> readData(w * h);
128 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
129
130 if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
131 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
132 return;
133 }
134
135 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)) {
141 ERRORF(reporter, "Original 0x%08x, read back as 0x%08x in %s at %d, %d).", orig,
142 read, subtestName, i, j);
143 return;
144 }
145 }
146 }
147 }
148
149 namespace {
150 enum class Encoding {
151 kUntagged,
152 kLinear,
153 kSRGB,
154 };
155 }
156
encoding_as_color_space(Encoding encoding)157 static 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
encoding_as_pixel_config(Encoding encoding)166 static 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
encoding_as_str(Encoding encoding)175 static 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
184 static constexpr int kW = 255;
185 static constexpr int kH = 255;
186
make_data()187 static std::unique_ptr<uint32_t[]> make_data() {
188 std::unique_ptr<uint32_t[]> data(new uint32_t[kW * kH]);
189 for (int j = 0; j < kH; ++j) {
190 for (int i = 0; i < kW; ++i) {
191 data[j * kW + i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
192 }
193 }
194 return data;
195 }
196
make_surface_context(Encoding contextEncoding,GrContext * context,skiatest::Reporter * reporter)197 static sk_sp<GrSurfaceContext> make_surface_context(Encoding contextEncoding, GrContext* context,
198 skiatest::Reporter* reporter) {
199 GrSurfaceDesc desc;
200 desc.fFlags = kRenderTarget_GrSurfaceFlag;
201 desc.fWidth = kW;
202 desc.fHeight = kH;
203 desc.fConfig = encoding_as_pixel_config(contextEncoding);
204
205 GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo;
206 GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(desc.fConfig, &srgbEncoded);
207 const GrBackendFormat format =
208 context->contextPriv().caps()->getBackendFormatFromGrColorType(colorType, srgbEncoded);
209
210 auto surfaceContext = context->contextPriv().makeDeferredSurfaceContext(
211 format, desc, kBottomLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
212 SkBudgeted::kNo, encoding_as_color_space(contextEncoding));
213 if (!surfaceContext) {
214 ERRORF(reporter, "Could not create %s surface context.", encoding_as_str(contextEncoding));
215 }
216 return surfaceContext;
217 }
218
test_write_read(Encoding contextEncoding,Encoding writeEncoding,Encoding readEncoding,float error,CheckFn check,GrContext * context,skiatest::Reporter * reporter)219 static void test_write_read(Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding,
220 float error, CheckFn check, GrContext* context,
221 skiatest::Reporter* reporter) {
222 auto surfaceContext = make_surface_context(contextEncoding, context, reporter);
223 if (!surfaceContext) {
224 return;
225 }
226 auto writeII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
227 encoding_as_color_space(writeEncoding));
228 auto data = make_data();
229 if (!surfaceContext->writePixels(writeII, data.get(), 0, 0, 0)) {
230 ERRORF(reporter, "Could not write %s to %s surface context.",
231 encoding_as_str(writeEncoding), encoding_as_str(contextEncoding));
232 return;
233 }
234
235 auto readII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
236 encoding_as_color_space(readEncoding));
237 SkString testName;
238 testName.printf("write %s data to a %s context and read as %s.", encoding_as_str(writeEncoding),
239 encoding_as_str(contextEncoding), encoding_as_str(readEncoding));
240 read_and_check_pixels(reporter, surfaceContext.get(), data.get(), readII, check, error,
241 testName.c_str());
242 }
243
244 // Test all combinations of writePixels/readPixels where the surface context/write source/read dst
245 // are sRGB, linear, or untagged RGBA_8888.
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels,reporter,ctxInfo)246 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
247 GrContext* context = ctxInfo.grContext();
248 if (!context->contextPriv().caps()->isConfigRenderable(kSRGBA_8888_GrPixelConfig) &&
249 !context->contextPriv().caps()->isConfigTexturable(kSRGBA_8888_GrPixelConfig)) {
250 return;
251 }
252 // We allow more error on GPUs with lower precision shader variables.
253 float error = context->contextPriv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
254 // For the all-sRGB case, we allow a small error only for devices that have
255 // precision variation because the sRGB data gets converted to linear and back in
256 // the shader.
257 float smallError = context->contextPriv().caps()->shaderCaps()->halfIs32Bits() ? 0.0f : 1.f;
258
259 ///////////////////////////////////////////////////////////////////////////////////////////////
260 // Write sRGB data to a sRGB context - no conversion on the write.
261
262 // back to sRGB - no conversion.
263 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kSRGB, smallError,
264 check_no_conversion, context, reporter);
265 // Reading back to untagged should be a pass through with no conversion.
266 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kUntagged, error,
267 check_no_conversion, context, reporter);
268
269 // Converts back to linear
270 test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kLinear, error,
271 check_srgb_to_linear_conversion, context, reporter);
272
273 // Untagged source data should be interpreted as sRGB.
274 test_write_read(Encoding::kSRGB, Encoding::kUntagged, Encoding::kSRGB, smallError,
275 check_no_conversion, context, reporter);
276
277 ///////////////////////////////////////////////////////////////////////////////////////////////
278 // Write linear data to a sRGB context. It gets converted to sRGB on write. The reads
279 // are all the same as the above cases where the original data was untagged.
280 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kSRGB, error,
281 check_linear_to_srgb_conversion, context, reporter);
282 // When the dst buffer is untagged there should be no conversion on the read.
283 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kUntagged, error,
284 check_linear_to_srgb_conversion, context, reporter);
285 test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kLinear, error,
286 check_linear_to_srgb_to_linear_conversion, context, reporter);
287
288 ///////////////////////////////////////////////////////////////////////////////////////////////
289 // Write data to an untagged context. The write does no conversion no matter what encoding the
290 // src data has.
291 for (auto writeEncoding : {Encoding::kSRGB, Encoding::kUntagged, Encoding::kLinear}) {
292 // The read from untagged to sRGB also does no conversion.
293 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kSRGB, error,
294 check_no_conversion, context, reporter);
295 // Reading untagged back as untagged should do no conversion.
296 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kUntagged, error,
297 check_no_conversion, context, reporter);
298 // Reading untagged back as linear does no conversion.
299 test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kLinear, error,
300 check_no_conversion, context, reporter);
301 }
302
303 ///////////////////////////////////////////////////////////////////////////////////////////////
304 // Write sRGB data to a linear context - converts to sRGB on the write.
305
306 // converts back to sRGB on read.
307 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kSRGB, error,
308 check_srgb_to_linear_to_srgb_conversion, context, reporter);
309 // Reading untagged data from linear currently does no conversion.
310 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kUntagged, error,
311 check_srgb_to_linear_conversion, context, reporter);
312 // Stays linear when read.
313 test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kLinear, error,
314 check_srgb_to_linear_conversion, context, reporter);
315
316 // Untagged source data should be interpreted as sRGB.
317 test_write_read(Encoding::kLinear, Encoding::kUntagged, Encoding::kSRGB, error,
318 check_srgb_to_linear_to_srgb_conversion, context, reporter);
319
320 ///////////////////////////////////////////////////////////////////////////////////////////////
321 // Write linear data to a linear context. Does no conversion.
322
323 // Reading to sRGB does a conversion.
324 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kSRGB, error,
325 check_linear_to_srgb_conversion, context, reporter);
326 // Reading to untagged does no conversion.
327 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kUntagged, error,
328 check_no_conversion, context, reporter);
329 // Stays linear when read.
330 test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kLinear, error,
331 check_no_conversion, context, reporter);
332 }
333