Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Macros | Typedefs | Functions
SwizzlerTest.cpp File Reference
#include "include/codec/SkCodec.h"
#include "include/core/SkAlphaType.h"
#include "include/core/SkColorType.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkSwizzle.h"
#include "src/codec/SkSampler.h"
#include "src/core/SkSwizzlePriv.h"
#include "tests/Test.h"
#include <cstdint>
#include <cstring>
#include <memory>
#include "src/opts/SkOpts_SetTarget.h"
#include "src/opts/SkSwizzler_opts.inc"
#include "src/opts/SkOpts_RestoreTarget.h"

Go to the source code of this file.

Macros

#define SK_OPTS_NS   test
 
#define SK_OPTS_TARGET   SK_OPTS_TARGET_DEFAULT
 

Typedefs

using fn_reciprocal = float(*)(float)
 

Functions

static void check_fill (skiatest::Reporter *r, const SkImageInfo &imageInfo, uint32_t startRow, uint32_t endRow, size_t rowBytes, uint32_t offset)
 
 DEF_TEST (SwizzlerFill, r)
 
 DEF_TEST (SwizzleOpts, r)
 
 DEF_TEST (PublicSwizzleOpts, r)
 
static void test_reciprocal_alpha (skiatest::Reporter *reporter, fn_reciprocal test255, fn_reciprocal test1)
 
 DEF_TEST (ReciprocalAlphaOptimized, reporter)
 
 DEF_TEST (ReciprocalAlphaPortable, reporter)
 
uint32_t calcExpected (float alpha, float comp)
 
 DEF_TEST (UnpremulSimulatingRP, reporter)
 

Macro Definition Documentation

◆ SK_OPTS_NS

#define SK_OPTS_NS   test

Definition at line 183 of file SwizzlerTest.cpp.

◆ SK_OPTS_TARGET

#define SK_OPTS_TARGET   SK_OPTS_TARGET_DEFAULT

Definition at line 184 of file SwizzlerTest.cpp.

Typedef Documentation

◆ fn_reciprocal

using fn_reciprocal = float (*)(float)

Definition at line 163 of file SwizzlerTest.cpp.

Function Documentation

◆ calcExpected()

uint32_t calcExpected ( float  alpha,
float  comp 
)

Definition at line 222 of file SwizzlerTest.cpp.

222 {
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};

◆ check_fill()

static void check_fill ( skiatest::Reporter r,
const SkImageInfo imageInfo,
uint32_t  startRow,
uint32_t  endRow,
size_t  rowBytes,
uint32_t  offset 
)
static

Definition at line 21 of file SwizzlerTest.cpp.

26 {
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}
@ 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
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
@ kNo_ZeroInitialized
Definition SkCodec.h:315
static void Fill(const SkImageInfo &info, void *dst, size_t rowBytes, SkCodec::ZeroInitialized zeroInit)
Definition SkSampler.cpp:20
double y
double x
Point offset
SkImageInfo makeWH(int newWidth, int newHeight) const
size_t computeByteSize(size_t rowBytes) const
int width() const
SkColorType colorType() const

◆ DEF_TEST() [1/6]

DEF_TEST ( PublicSwizzleOpts  ,
 
)

Definition at line 154 of file SwizzlerTest.cpp.

154 {
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}
SK_API void SkSwapRB(uint32_t *dest, const uint32_t *src, int count)
Definition SkSwizzle.cpp:12
dst
Definition cp.py:12

◆ DEF_TEST() [2/6]

DEF_TEST ( ReciprocalAlphaOptimized  ,
reporter   
)

Definition at line 187 of file SwizzlerTest.cpp.

187 {
189 SK_OPTS_NS::reciprocal_alpha_times_255,
190 SK_OPTS_NS::reciprocal_alpha);
191}
reporter
static void test_reciprocal_alpha(skiatest::Reporter *reporter, fn_reciprocal test255, fn_reciprocal test1)

◆ DEF_TEST() [3/6]

DEF_TEST ( ReciprocalAlphaPortable  ,
reporter   
)

Definition at line 193 of file SwizzlerTest.cpp.

193 {
195 SK_OPTS_NS::reciprocal_alpha_times_255_portable,
196 SK_OPTS_NS::reciprocal_alpha_portable);
197}

◆ DEF_TEST() [4/6]

DEF_TEST ( SwizzleOpts  ,
 
)

Definition at line 122 of file SwizzlerTest.cpp.

122 {
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}
Swizzle_8888_u32 RGBA_to_rgbA
Swizzle_8888_u32 RGBA_to_BGRA
Swizzle_8888_u32 RGBA_to_bgrA

◆ DEF_TEST() [5/6]

DEF_TEST ( SwizzlerFill  ,
 
)

Definition at line 73 of file SwizzlerTest.cpp.

73 {
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}
@ kUnknown_SkAlphaType
uninitialized
Definition SkAlphaType.h:27
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
const SkScalar widths[]
static void check_fill(skiatest::Reporter *r, const SkImageInfo &imageInfo, uint32_t startRow, uint32_t endRow, size_t rowBytes, uint32_t offset)
int32_t height
int32_t width
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)
SkImageInfo makeColorType(SkColorType newColorType) const

◆ DEF_TEST() [6/6]

DEF_TEST ( UnpremulSimulatingRP  ,
reporter   
)

Definition at line 234 of file SwizzlerTest.cpp.

234 {
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}
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
uint32_t calcExpected(float alpha, float comp)
struct MyStruct a[10]

◆ test_reciprocal_alpha()

static void test_reciprocal_alpha ( skiatest::Reporter reporter,
fn_reciprocal  test255,
fn_reciprocal  test1 
)
static

Definition at line 164 of file SwizzlerTest.cpp.

166 {
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
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}
static void test1(skiatest::Reporter *reporter, SkWriter32 *writer)