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