xref: /aosp_15_r20/external/skia/tests/SwizzlerTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/codec/SkCodec.h"
9 #include "include/core/SkAlphaType.h"
10 #include "include/core/SkColorType.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkSwizzle.h"
13 #include "src/codec/SkSampler.h"
14 #include "src/core/SkSwizzlePriv.h"
15 #include "tests/Test.h"
16 
17 #include <cstdint>
18 #include <cstring>
19 #include <memory>
20 
check_fill(skiatest::Reporter * r,const SkImageInfo & imageInfo,uint32_t startRow,uint32_t endRow,size_t rowBytes,uint32_t offset)21 static void check_fill(skiatest::Reporter* r,
22                        const SkImageInfo& imageInfo,
23                        uint32_t startRow,
24                        uint32_t endRow,
25                        size_t rowBytes,
26                        uint32_t offset) {
27 
28     // Calculate the total size of the image in bytes.  Use the smallest possible size.
29     // The offset value tells us to adjust the pointer from the memory we allocate in order
30     // to test on different memory alignments.  If offset is nonzero, we need to increase the
31     // size of the memory we allocate in order to make sure that we have enough.  We are
32     // still allocating the smallest possible size.
33     const size_t totalBytes = imageInfo.computeByteSize(rowBytes) + offset;
34 
35     // Create fake image data where every byte has a value of 0
36     std::unique_ptr<uint8_t[]> storage(new uint8_t[totalBytes]);
37     memset(storage.get(), 0, totalBytes);
38     // Adjust the pointer in order to test on different memory alignments
39     uint8_t* imageData = storage.get() + offset;
40     uint8_t* imageStart = imageData + rowBytes * startRow;
41     const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1);
42     SkSampler::Fill(fillInfo, imageStart, rowBytes, SkCodec::kNo_ZeroInitialized);
43 
44     // Ensure that the pixels are filled properly
45     // The bots should catch any memory corruption
46     uint8_t* indexPtr = imageData + startRow * rowBytes;
47     uint8_t* grayPtr = indexPtr;
48     uint32_t* colorPtr = (uint32_t*) indexPtr;
49     uint16_t* color565Ptr = (uint16_t*) indexPtr;
50     for (uint32_t y = startRow; y <= endRow; y++) {
51         for (int32_t x = 0; x < imageInfo.width(); x++) {
52             switch (imageInfo.colorType()) {
53                 case kN32_SkColorType:
54                     REPORTER_ASSERT(r, 0 == colorPtr[x]);
55                     break;
56                 case kGray_8_SkColorType:
57                     REPORTER_ASSERT(r, 0 == grayPtr[x]);
58                     break;
59                 case kRGB_565_SkColorType:
60                     REPORTER_ASSERT(r, 0 == color565Ptr[x]);
61                     break;
62                 default:
63                     REPORTER_ASSERT(r, false);
64                     break;
65             }
66         }
67         indexPtr += rowBytes;
68         colorPtr = (uint32_t*) indexPtr;
69     }
70 }
71 
72 // Test Fill() with different combinations of dimensions, alignment, and padding
DEF_TEST(SwizzlerFill,r)73 DEF_TEST(SwizzlerFill, r) {
74     // Test on an invalid width and representative widths
75     const uint32_t widths[] = { 0, 10, 50 };
76 
77     // In order to call Fill(), there must be at least one row to fill
78     // Test on the smallest possible height and representative heights
79     const uint32_t heights[] = { 1, 5, 10 };
80 
81     // Test on interesting possibilities for row padding
82     const uint32_t paddings[] = { 0, 4 };
83 
84     // Iterate over test dimensions
85     for (uint32_t width : widths) {
86         for (uint32_t height : heights) {
87 
88             // Create image info objects
89             const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kUnknown_SkAlphaType);
90             const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColorType);
91             const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_SkColorType);
92 
93             for (uint32_t padding : paddings) {
94 
95                 // Calculate row bytes
96                 const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width
97                         + padding;
98                 const size_t indexRowBytes = width + padding;
99                 const size_t grayRowBytes = indexRowBytes;
100                 const size_t color565RowBytes =
101                         SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding;
102 
103                 // If there is padding, we can invent an offset to change the memory alignment
104                 for (uint32_t offset = 0; offset <= padding; offset += 4) {
105 
106                     // Test all possible start rows with all possible end rows
107                     for (uint32_t startRow = 0; startRow < height; startRow++) {
108                         for (uint32_t endRow = startRow; endRow < height; endRow++) {
109 
110                             // Test fill with each color type
111                             check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset);
112                             check_fill(r, grayInfo, startRow, endRow, grayRowBytes, offset);
113                             check_fill(r, color565Info, startRow, endRow, color565RowBytes, offset);
114                         }
115                     }
116                 }
117             }
118         }
119     }
120 }
121 
DEF_TEST(SwizzleOpts,r)122 DEF_TEST(SwizzleOpts, r) {
123     uint32_t dst, src;
124 
125     // forall c, c*255 == c, c*0 == 0
126     for (int c = 0; c <= 255; c++) {
127         src = (255<<24) | c;
128         SkOpts::RGBA_to_rgbA(&dst, &src, 1);
129         REPORTER_ASSERT(r, dst == src);
130         SkOpts::RGBA_to_bgrA(&dst, &src, 1);
131         REPORTER_ASSERT(r, dst == (uint32_t)((255<<24) | (c<<16)));
132 
133         src = (0<<24) | c;
134         SkOpts::RGBA_to_rgbA(&dst, &src, 1);
135         REPORTER_ASSERT(r, dst == 0);
136         SkOpts::RGBA_to_bgrA(&dst, &src, 1);
137         REPORTER_ASSERT(r, dst == 0);
138     }
139 
140     // check a totally arbitrary color
141     src = 0xFACEB004;
142     SkOpts::RGBA_to_rgbA(&dst, &src, 1);
143     REPORTER_ASSERT(r, dst == 0xFACAAD04);
144 
145     // swap red and blue
146     SkOpts::RGBA_to_BGRA(&dst, &src, 1);
147     REPORTER_ASSERT(r, dst == 0xFA04B0CE);
148 
149     // all together now
150     SkOpts::RGBA_to_bgrA(&dst, &src, 1);
151     REPORTER_ASSERT(r, dst == 0xFA04ADCA);
152 }
153 
DEF_TEST(PublicSwizzleOpts,r)154 DEF_TEST(PublicSwizzleOpts, r) {
155     uint32_t dst, src;
156 
157     // check a totally arbitrary color
158     src = 0xFACEB004;
159     SkSwapRB(&dst, &src, 1);
160     REPORTER_ASSERT(r, dst == 0xFA04B0CE);
161 }
162 
163 using fn_reciprocal = float (*)(float);
test_reciprocal_alpha(skiatest::Reporter * reporter,fn_reciprocal test255,fn_reciprocal test1)164 static void test_reciprocal_alpha(
165         skiatest::Reporter* reporter,
166         fn_reciprocal test255, fn_reciprocal test1) {
167     REPORTER_ASSERT(reporter, test255(0) == 0);
168     for (uint32_t i = 1; i < 256; ++i) {
169         const float r = test255(i);
170         const float e = (255.0f / i);
171         REPORTER_ASSERT(reporter, r == e);
172     }
173 
174     REPORTER_ASSERT(reporter, test1(0) == 0);
175     for (uint32_t i = 1; i < 256; ++i) {
176         const float normalized = i / 255.0f;
177         const float r = test1(normalized);
178         const float e = (1.0f / normalized);
179         REPORTER_ASSERT(reporter, r == e);
180     }
181 }
182 
183 #define SK_OPTS_NS test
184 #define SK_OPTS_TARGET SK_OPTS_TARGET_DEFAULT
185 #include "src/opts/SkOpts_SetTarget.h"
186 #include "src/opts/SkSwizzler_opts.inc"
DEF_TEST(ReciprocalAlphaOptimized,reporter)187 DEF_TEST(ReciprocalAlphaOptimized, reporter) {
188     test_reciprocal_alpha(reporter,
189                           SK_OPTS_NS::reciprocal_alpha_times_255,
190                           SK_OPTS_NS::reciprocal_alpha);
191 }
192 
DEF_TEST(ReciprocalAlphaPortable,reporter)193 DEF_TEST(ReciprocalAlphaPortable, reporter) {
194     test_reciprocal_alpha(reporter,
195                           SK_OPTS_NS::reciprocal_alpha_times_255_portable,
196                           SK_OPTS_NS::reciprocal_alpha_portable);
197 }
198 
199 // The stages of RasterPipeline unpremul calcExpected needs to simulate.
200 // SI void from_8888(U32 _8888, F* r, F* g, F* b, F* a) {
201 //     *r = cast((_8888      ) & 0xff) * (1/255.0f);
202 //     *g = cast((_8888 >>  8) & 0xff) * (1/255.0f);
203 //     *b = cast((_8888 >> 16) & 0xff) * (1/255.0f);
204 //     *a = cast((_8888 >> 24)       ) * (1/255.0f);
205 // }
206 // STAGE(unpremul, NoCtx) {
207 //     float inf = sk_bit_cast<float>(0x7f800000);
208 //     auto scale = if_then_else(1.0f/a < inf, 1.0f/a, 0.0f);
209 //     r *= scale;
210 //     g *= scale;
211 //     b *= scale;
212 // }
213 // STAGE(store_8888, const SkRasterPipeline_MemoryCtx* ctx) {
214 //     auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
215 //
216 //     U32 px = to_unorm(r, 255)
217 //            | to_unorm(g, 255) <<  8
218 //            | to_unorm(b, 255) << 16
219 //            | to_unorm(a, 255) << 24;
220 //     store(ptr, px);
221 // }
calcExpected(float alpha,float comp)222 uint32_t calcExpected(float alpha, float comp) {
223     if (alpha == 0) {
224         return 0;
225     }
226     const float normalized = comp * (1.0f / 255.0f);
227     const float normalizedA = alpha * (1.0f / 255.0f);
228     const float inverseAlpha = 1.0f / normalizedA;
229     const float unpremul = normalized * inverseAlpha;
230     const float scaledAndPinned = std::min(255.0f, unpremul * 255.0f);
231     return SK_OPTS_NS::pixel_round_as_RP(scaledAndPinned);
232 };
233 
DEF_TEST(UnpremulSimulatingRP,reporter)234 DEF_TEST(UnpremulSimulatingRP, reporter) {
235     for (uint32_t a = 0; a < 256; ++a) {
236         for (uint32_t c = 0; c < 256; ++c) {
237             const uint32_t expected = calcExpected(a, c);
238             const float normalizedA = a * (1.0f / 255.0f);
239             const float invA = SK_OPTS_NS::reciprocal_alpha(normalizedA);
240             const uint32_t actual = SK_OPTS_NS::unpremul_simulating_RP(invA, c);
241             if (actual != expected) {
242                 SkDebugf("a: %u c: %u expected: %u actual: %u\n", a, c, expected, actual);
243             }
244             REPORTER_ASSERT(reporter, actual == expected);
245         }
246     }
247 }
248 
249 #include "src/opts/SkOpts_RestoreTarget.h"
250