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 "include/core/SkCanvas.h"
9 #include "include/core/SkSurface.h"
10 #include "include/gpu/GrDirectContext.h"
11 #include "src/gpu/GrCaps.h"
12 #include "src/gpu/GrDirectContextPriv.h"
13 #include "src/gpu/GrImageInfo.h"
14 #include "src/gpu/GrSurfaceContext.h"
15 #include "src/gpu/GrSurfaceDrawContext.h"
16 #include "src/gpu/SkGr.h"
17 #include "tests/Test.h"
18 
19 // using anonymous namespace because these functions are used as template params.
20 namespace {
21 /** convert 0..1 srgb value to 0..1 linear */
srgb_to_linear(float srgb)22 float srgb_to_linear(float srgb) {
23     if (srgb <= 0.04045f) {
24         return srgb / 12.92f;
25     } else {
26         return powf((srgb + 0.055f) / 1.055f, 2.4f);
27     }
28 }
29 
30 /** convert 0..1 linear value to 0..1 srgb */
linear_to_srgb(float linear)31 float linear_to_srgb(float linear) {
32     if (linear <= 0.0031308) {
33         return linear * 12.92f;
34     } else {
35         return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
36     }
37 }
38 }  // namespace
39 
40 /** tests a conversion with an error tolerance */
check_conversion(uint32_t input,uint32_t output,float error)41 template <float (*CONVERT)(float)> static bool check_conversion(uint32_t input, uint32_t output,
42                                                                 float error) {
43     // alpha should always be exactly preserved.
44     if ((input & 0xff000000) != (output & 0xff000000)) {
45         return false;
46     }
47 
48     for (int c = 0; c < 3; ++c) {
49         uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
50         float lower = std::max(0.f, (float) inputComponent - error);
51         float upper = std::min(255.f, (float) inputComponent + error);
52         lower = CONVERT(lower / 255.f);
53         upper = CONVERT(upper / 255.f);
54         SkASSERT(lower >= 0.f && lower <= 255.f);
55         SkASSERT(upper >= 0.f && upper <= 255.f);
56         uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
57         if (outputComponent < SkScalarFloorToInt(lower * 255.f) ||
58             outputComponent > SkScalarCeilToInt(upper * 255.f)) {
59             return false;
60         }
61     }
62     return true;
63 }
64 
65 /** tests a forward and backward conversion with an error tolerance */
66 template <float (*FORWARD)(float), float (*BACKWARD)(float)>
check_double_conversion(uint32_t input,uint32_t output,float error)67 static bool check_double_conversion(uint32_t input, uint32_t output, float error) {
68     // alpha should always be exactly preserved.
69     if ((input & 0xff000000) != (output & 0xff000000)) {
70         return false;
71     }
72 
73     for (int c = 0; c < 3; ++c) {
74         uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
75         float lower = std::max(0.f, (float) inputComponent - error);
76         float upper = std::min(255.f, (float) inputComponent + error);
77         lower = FORWARD(lower / 255.f);
78         upper = FORWARD(upper / 255.f);
79         SkASSERT(lower >= 0.f && lower <= 255.f);
80         SkASSERT(upper >= 0.f && upper <= 255.f);
81         uint8_t upperComponent = SkScalarCeilToInt(upper * 255.f);
82         uint8_t lowerComponent = SkScalarFloorToInt(lower * 255.f);
83         lower = std::max(0.f, (float) lowerComponent - error);
84         upper = std::min(255.f, (float) upperComponent + error);
85         lower = BACKWARD(lowerComponent / 255.f);
86         upper = BACKWARD(upperComponent / 255.f);
87         SkASSERT(lower >= 0.f && lower <= 255.f);
88         SkASSERT(upper >= 0.f && upper <= 255.f);
89         upperComponent = SkScalarCeilToInt(upper * 255.f);
90         lowerComponent = SkScalarFloorToInt(lower * 255.f);
91 
92         uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
93         if (outputComponent < lowerComponent || outputComponent > upperComponent) {
94             return false;
95         }
96     }
97     return true;
98 }
99 
check_srgb_to_linear_conversion(uint32_t srgb,uint32_t linear,float error)100 static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error) {
101     return check_conversion<srgb_to_linear>(srgb, linear, error);
102 }
103 
check_linear_to_srgb_conversion(uint32_t linear,uint32_t srgb,float error)104 static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error) {
105     return check_conversion<linear_to_srgb>(linear, srgb, error);
106 }
107 
check_linear_to_srgb_to_linear_conversion(uint32_t input,uint32_t output,float error)108 static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error) {
109     return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
110 }
111 
check_srgb_to_linear_to_srgb_conversion(uint32_t input,uint32_t output,float error)112 static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error) {
113     return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
114 }
115 
check_no_conversion(uint32_t input,uint32_t output,float error)116 static bool check_no_conversion(uint32_t input, uint32_t output, float error) {
117     // This is a bit of a hack to check identity transformations that may lose precision.
118     return check_srgb_to_linear_to_srgb_conversion(input, output, error);
119 }
120 
121 typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
122 
read_and_check_pixels(skiatest::Reporter * reporter,GrDirectContext * dContext,GrSurfaceContext * sContext,uint32_t * origData,const SkImageInfo & dstInfo,CheckFn checker,float error,const char * subtestName)123 void read_and_check_pixels(skiatest::Reporter* reporter,
124                            GrDirectContext* dContext,
125                            GrSurfaceContext* sContext,
126                            uint32_t* origData,
127                            const SkImageInfo& dstInfo, CheckFn checker, float error,
128                            const char* subtestName) {
129     auto [w, h] = dstInfo.dimensions();
130     GrPixmap readPM = GrPixmap::Allocate(dstInfo);
131     memset(readPM.addr(), 0, sizeof(uint32_t)*w*h);
132 
133     if (!sContext->readPixels(dContext, readPM, {0, 0})) {
134         ERRORF(reporter, "Could not read pixels for %s.", subtestName);
135         return;
136     }
137 
138     for (int j = 0; j < h; ++j) {
139         for (int i = 0; i < w; ++i) {
140             uint32_t orig = origData[j * w + i];
141             uint32_t read = static_cast<uint32_t*>(readPM.addr())[j * w + i];
142 
143             if (!checker(orig, read, error)) {
144                 ERRORF(reporter, "Original 0x%08x, read back as 0x%08x in %s at %d, %d).", orig,
145                        read, subtestName, i, j);
146                 return;
147             }
148         }
149     }
150 }
151 
152 namespace {
153 enum class Encoding {
154     kUntagged,
155     kLinear,
156     kSRGB,
157 };
158 }  // namespace
159 
encoding_as_color_space(Encoding encoding)160 static sk_sp<SkColorSpace> encoding_as_color_space(Encoding encoding) {
161     switch (encoding) {
162         case Encoding::kUntagged: return nullptr;
163         case Encoding::kLinear:   return SkColorSpace::MakeSRGBLinear();
164         case Encoding::kSRGB:     return SkColorSpace::MakeSRGB();
165     }
166     return nullptr;
167 }
168 
encoding_as_str(Encoding encoding)169 static const char* encoding_as_str(Encoding encoding) {
170     switch (encoding) {
171         case Encoding::kUntagged: return "untagged";
172         case Encoding::kLinear:   return "linear";
173         case Encoding::kSRGB:     return "sRGB";
174     }
175     return nullptr;
176 }
177 
178 static constexpr int kW = 255;
179 static constexpr int kH = 255;
180 
make_data()181 static std::unique_ptr<uint32_t[]> make_data() {
182     std::unique_ptr<uint32_t[]> data(new uint32_t[kW * kH]);
183     for (int j = 0; j < kH; ++j) {
184         for (int i = 0; i < kW; ++i) {
185             data[j * kW + i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
186         }
187     }
188     return data;
189 }
190 
make_surface_context(Encoding contextEncoding,GrRecordingContext * rContext,skiatest::Reporter * reporter)191 static std::unique_ptr<GrSurfaceContext> make_surface_context(Encoding contextEncoding,
192                                                               GrRecordingContext* rContext,
193                                                               skiatest::Reporter* reporter) {
194     auto surfaceContext = GrSurfaceDrawContext::Make(
195             rContext, GrColorType::kRGBA_8888, encoding_as_color_space(contextEncoding),
196             SkBackingFit::kExact, {kW, kH}, SkSurfaceProps(), 1, GrMipmapped::kNo, GrProtected::kNo,
197             kBottomLeft_GrSurfaceOrigin, SkBudgeted::kNo);
198     if (!surfaceContext) {
199         ERRORF(reporter, "Could not create %s surface context.", encoding_as_str(contextEncoding));
200     }
201     return std::move(surfaceContext);
202 }
203 
test_write_read(Encoding contextEncoding,Encoding writeEncoding,Encoding readEncoding,float error,CheckFn check,GrDirectContext * dContext,skiatest::Reporter * reporter)204 static void test_write_read(Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding,
205                             float error, CheckFn check, GrDirectContext* dContext,
206                             skiatest::Reporter* reporter) {
207     auto surfaceContext = make_surface_context(contextEncoding, dContext, reporter);
208     if (!surfaceContext) {
209         return;
210     }
211     auto writeII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
212                                      encoding_as_color_space(writeEncoding));
213     auto data = make_data();
214     GrCPixmap dataPM(writeII, data.get(), kW*sizeof(uint32_t));
215     if (!surfaceContext->writePixels(dContext, dataPM, {0, 0})) {
216         ERRORF(reporter, "Could not write %s to %s surface context.",
217                encoding_as_str(writeEncoding), encoding_as_str(contextEncoding));
218         return;
219     }
220 
221     auto readII = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
222                                     encoding_as_color_space(readEncoding));
223     SkString testName;
224     testName.printf("write %s data to a %s context and read as %s.", encoding_as_str(writeEncoding),
225                     encoding_as_str(contextEncoding), encoding_as_str(readEncoding));
226     read_and_check_pixels(reporter, dContext, surfaceContext.get(), data.get(), readII, check,
227                           error, testName.c_str());
228 }
229 
230 // Test all combinations of writePixels/readPixels where the surface context/write source/read dst
231 // are sRGB, linear, or untagged RGBA_8888.
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels,reporter,ctxInfo)232 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
233     auto context = ctxInfo.directContext();
234     if (!context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888_SRGB,
235                                                          GrRenderable::kNo).isValid()) {
236         return;
237     }
238     // We allow more error on GPUs with lower precision shader variables.
239     float error = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
240     // For the all-sRGB case, we allow a small error only for devices that have
241     // precision variation because the sRGB data gets converted to linear and back in
242     // the shader.
243     float smallError = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.0f : 1.f;
244 
245     ///////////////////////////////////////////////////////////////////////////////////////////////
246     // Write sRGB data to a sRGB context - no conversion on the write.
247 
248     // back to sRGB - no conversion.
249     test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kSRGB, smallError,
250                     check_no_conversion, context, reporter);
251     // Reading back to untagged should be a pass through with no conversion.
252     test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kUntagged, error,
253                     check_no_conversion, context, reporter);
254 
255     // Converts back to linear
256     test_write_read(Encoding::kSRGB, Encoding::kSRGB, Encoding::kLinear, error,
257                     check_srgb_to_linear_conversion, context, reporter);
258 
259     // Untagged source data should be interpreted as sRGB.
260     test_write_read(Encoding::kSRGB, Encoding::kUntagged, Encoding::kSRGB, smallError,
261                     check_no_conversion, context, reporter);
262 
263     ///////////////////////////////////////////////////////////////////////////////////////////////
264     // Write linear data to a sRGB context. It gets converted to sRGB on write. The reads
265     // are all the same as the above cases where the original data was untagged.
266     test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kSRGB, error,
267                     check_linear_to_srgb_conversion, context, reporter);
268     // When the dst buffer is untagged there should be no conversion on the read.
269     test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kUntagged, error,
270                     check_linear_to_srgb_conversion, context, reporter);
271     test_write_read(Encoding::kSRGB, Encoding::kLinear, Encoding::kLinear, error,
272                     check_linear_to_srgb_to_linear_conversion, context, reporter);
273 
274     ///////////////////////////////////////////////////////////////////////////////////////////////
275     // Write data to an untagged context. The write does no conversion no matter what encoding the
276     // src data has.
277     for (auto writeEncoding : {Encoding::kSRGB, Encoding::kUntagged, Encoding::kLinear}) {
278         // The read from untagged to sRGB also does no conversion.
279         test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kSRGB, error,
280                         check_no_conversion, context, reporter);
281         // Reading untagged back as untagged should do no conversion.
282         test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kUntagged, error,
283                         check_no_conversion, context, reporter);
284         // Reading untagged back as linear does convert (context is source, so treated as sRGB),
285         // dst is tagged.
286         test_write_read(Encoding::kUntagged, writeEncoding, Encoding::kLinear, error,
287                         check_srgb_to_linear_conversion, context, reporter);
288     }
289 
290     ///////////////////////////////////////////////////////////////////////////////////////////////
291     // Write sRGB data to a linear context - converts to sRGB on the write.
292 
293     // converts back to sRGB on read.
294     test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kSRGB, error,
295                     check_srgb_to_linear_to_srgb_conversion, context, reporter);
296     // Reading untagged data from linear currently does no conversion.
297     test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kUntagged, error,
298                     check_srgb_to_linear_conversion, context, reporter);
299     // Stays linear when read.
300     test_write_read(Encoding::kLinear, Encoding::kSRGB, Encoding::kLinear, error,
301                     check_srgb_to_linear_conversion, context, reporter);
302 
303     // Untagged source data should be interpreted as sRGB.
304     test_write_read(Encoding::kLinear, Encoding::kUntagged, Encoding::kSRGB, error,
305                     check_srgb_to_linear_to_srgb_conversion, context, reporter);
306 
307     ///////////////////////////////////////////////////////////////////////////////////////////////
308     // Write linear data to a linear context. Does no conversion.
309 
310     // Reading to sRGB does a conversion.
311     test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kSRGB, error,
312                     check_linear_to_srgb_conversion, context, reporter);
313     // Reading to untagged does no conversion.
314     test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kUntagged, error,
315                     check_no_conversion, context, reporter);
316     // Stays linear when read.
317     test_write_read(Encoding::kLinear, Encoding::kLinear, Encoding::kLinear, error,
318                     check_no_conversion, context, reporter);
319 }
320