Flutter Engine
The Flutter Engine
SwizzlerTest.cpp
Go to the documentation of this file.
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
13#include "src/codec/SkSampler.h"
15#include "tests/Test.h"
16
17#include <cstdint>
18#include <cstring>
19#include <memory>
20
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;
57 REPORTER_ASSERT(r, 0 == grayPtr[x]);
58 break;
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
73DEF_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
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 =
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
122DEF_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;
129 REPORTER_ASSERT(r, dst == src);
131 REPORTER_ASSERT(r, dst == (uint32_t)((255<<24) | (c<<16)));
132
133 src = (0<<24) | c;
135 REPORTER_ASSERT(r, dst == 0);
137 REPORTER_ASSERT(r, dst == 0);
138 }
139
140 // check a totally arbitrary color
141 src = 0xFACEB004;
143 REPORTER_ASSERT(r, dst == 0xFACAAD04);
144
145 // swap red and blue
147 REPORTER_ASSERT(r, dst == 0xFA04B0CE);
148
149 // all together now
151 REPORTER_ASSERT(r, dst == 0xFA04ADCA);
152}
153
154DEF_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
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);
172 }
173
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);
180 }
181}
182
183#define SK_OPTS_NS test
184#define SK_OPTS_TARGET SK_OPTS_TARGET_DEFAULT
187DEF_TEST(ReciprocalAlphaOptimized, reporter) {
189 SK_OPTS_NS::reciprocal_alpha_times_255,
190 SK_OPTS_NS::reciprocal_alpha);
191}
192
193DEF_TEST(ReciprocalAlphaPortable, 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// }
222uint32_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
234DEF_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
reporter
Definition: FontMgrTest.cpp:39
@ kUnknown_SkAlphaType
uninitialized
Definition: SkAlphaType.h:27
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
Definition: SkImageInfo.cpp:16
SK_API void SkSwapRB(uint32_t *dest, const uint32_t *src, int count)
Definition: SkSwizzle.cpp:12
const SkScalar widths[]
Definition: StrokerTest.cpp:39
static void test_reciprocal_alpha(skiatest::Reporter *reporter, fn_reciprocal test255, fn_reciprocal test1)
DEF_TEST(SwizzlerFill, r)
static void check_fill(skiatest::Reporter *r, const SkImageInfo &imageInfo, uint32_t startRow, uint32_t endRow, size_t rowBytes, uint32_t offset)
uint32_t calcExpected(float alpha, float comp)
float(*)(float) fn_reciprocal
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
static void test1(skiatest::Reporter *reporter, SkWriter32 *writer)
@ kNo_ZeroInitialized
Definition: SkCodec.h:315
static void Fill(const SkImageInfo &info, void *dst, size_t rowBytes, SkCodec::ZeroInitialized zeroInit)
Definition: SkSampler.cpp:20
struct MyStruct a[10]
static float min(float r, float g, float b)
Definition: hsl.cpp:48
double y
double x
Swizzle_8888_u32 RGBA_to_rgbA
Definition: SkSwizzlePriv.h:20
Swizzle_8888_u32 RGBA_to_BGRA
Swizzle_8888_u32 RGBA_to_bgrA
Definition: SkSwizzlePriv.h:21
dst
Definition: cp.py:12
int32_t height
int32_t width
SeparatedVector2 offset
SkImageInfo makeWH(int newWidth, int newHeight) const
Definition: SkImageInfo.h:444
size_t computeByteSize(size_t rowBytes) const
int width() const
Definition: SkImageInfo.h:365
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)
SkColorType colorType() const
Definition: SkImageInfo.h:373
SkImageInfo makeColorType(SkColorType newColorType) const
Definition: SkImageInfo.h:475