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