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 "SkSwizzler.h"
9 #include "Test.h"
10 #include "SkOpts.h"
11 
12 // These are the values that we will look for to indicate that the fill was successful
13 static const uint8_t kFillIndex = 0x11;
14 static const uint8_t kFillGray = 0x22;
15 static const uint16_t kFill565 = 0x3344;
16 static const uint32_t kFillColor = 0x55667788;
17 
check_fill(skiatest::Reporter * r,const SkImageInfo & imageInfo,uint32_t startRow,uint32_t endRow,size_t rowBytes,uint32_t offset,uint32_t colorOrIndex)18 static void check_fill(skiatest::Reporter* r,
19                        const SkImageInfo& imageInfo,
20                        uint32_t startRow,
21                        uint32_t endRow,
22                        size_t rowBytes,
23                        uint32_t offset,
24                        uint32_t colorOrIndex) {
25 
26     // Calculate the total size of the image in bytes.  Use the smallest possible size.
27     // The offset value tells us to adjust the pointer from the memory we allocate in order
28     // to test on different memory alignments.  If offset is nonzero, we need to increase the
29     // size of the memory we allocate in order to make sure that we have enough.  We are
30     // still allocating the smallest possible size.
31     const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset;
32 
33     // Create fake image data where every byte has a value of 0
34     SkAutoTDeleteArray<uint8_t> storage(new uint8_t[totalBytes]);
35     memset(storage.get(), 0, totalBytes);
36     // Adjust the pointer in order to test on different memory alignments
37     uint8_t* imageData = storage.get() + offset;
38     uint8_t* imageStart = imageData + rowBytes * startRow;
39     const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1);
40     SkSampler::Fill(fillInfo, imageStart, rowBytes, colorOrIndex, SkCodec::kNo_ZeroInitialized);
41 
42     // Ensure that the pixels are filled properly
43     // The bots should catch any memory corruption
44     uint8_t* indexPtr = imageData + startRow * rowBytes;
45     uint8_t* grayPtr = indexPtr;
46     uint32_t* colorPtr = (uint32_t*) indexPtr;
47     uint16_t* color565Ptr = (uint16_t*) indexPtr;
48     for (uint32_t y = startRow; y <= endRow; y++) {
49         for (int32_t x = 0; x < imageInfo.width(); x++) {
50             switch (imageInfo.colorType()) {
51                 case kIndex_8_SkColorType:
52                     REPORTER_ASSERT(r, kFillIndex == indexPtr[x]);
53                     break;
54                 case kN32_SkColorType:
55                     REPORTER_ASSERT(r, kFillColor == colorPtr[x]);
56                     break;
57                 case kGray_8_SkColorType:
58                     REPORTER_ASSERT(r, kFillGray == grayPtr[x]);
59                     break;
60                 case kRGB_565_SkColorType:
61                     REPORTER_ASSERT(r, kFill565 == color565Ptr[x]);
62                     break;
63                 default:
64                     REPORTER_ASSERT(r, false);
65                     break;
66             }
67         }
68         indexPtr += rowBytes;
69         colorPtr = (uint32_t*) indexPtr;
70     }
71 }
72 
73 // Test Fill() with different combinations of dimensions, alignment, and padding
DEF_TEST(SwizzlerFill,r)74 DEF_TEST(SwizzlerFill, r) {
75     // Test on an invalid width and representative widths
76     const uint32_t widths[] = { 0, 10, 50 };
77 
78     // In order to call Fill(), there must be at least one row to fill
79     // Test on the smallest possible height and representative heights
80     const uint32_t heights[] = { 1, 5, 10 };
81 
82     // Test on interesting possibilities for row padding
83     const uint32_t paddings[] = { 0, 4 };
84 
85     // Iterate over test dimensions
86     for (uint32_t width : widths) {
87         for (uint32_t height : heights) {
88 
89             // Create image info objects
90             const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kUnknown_SkAlphaType);
91             const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColorType);
92             const SkImageInfo indexInfo = colorInfo.makeColorType(kIndex_8_SkColorType);
93             const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_SkColorType);
94 
95             for (uint32_t padding : paddings) {
96 
97                 // Calculate row bytes
98                 const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width
99                         + padding;
100                 const size_t indexRowBytes = width + padding;
101                 const size_t grayRowBytes = indexRowBytes;
102                 const size_t color565RowBytes =
103                         SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding;
104 
105                 // If there is padding, we can invent an offset to change the memory alignment
106                 for (uint32_t offset = 0; offset <= padding; offset += 4) {
107 
108                     // Test all possible start rows with all possible end rows
109                     for (uint32_t startRow = 0; startRow < height; startRow++) {
110                         for (uint32_t endRow = startRow; endRow < height; endRow++) {
111 
112                             // Test fill with each color type
113                             check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset,
114                                     kFillColor);
115                             check_fill(r, indexInfo, startRow, endRow, indexRowBytes, offset,
116                                     kFillIndex);
117                             check_fill(r, grayInfo, startRow, endRow, grayRowBytes, offset,
118                                     kFillGray);
119                             check_fill(r, color565Info, startRow, endRow, color565RowBytes, offset,
120                                     kFill565);
121                         }
122                     }
123                 }
124             }
125         }
126     }
127 }
128 
DEF_TEST(SwizzleOpts,r)129 DEF_TEST(SwizzleOpts, r) {
130     uint32_t dst, src;
131 
132     // forall c, c*255 == c, c*0 == 0
133     for (int c = 0; c <= 255; c++) {
134         src = (255<<24) | c;
135         SkOpts::RGBA_to_rgbA(&dst, &src, 1);
136         REPORTER_ASSERT(r, dst == src);
137         SkOpts::RGBA_to_bgrA(&dst, &src, 1);
138         REPORTER_ASSERT(r, dst == (uint32_t)((255<<24) | (c<<16)));
139 
140         src = (0<<24) | c;
141         SkOpts::RGBA_to_rgbA(&dst, &src, 1);
142         REPORTER_ASSERT(r, dst == 0);
143         SkOpts::RGBA_to_bgrA(&dst, &src, 1);
144         REPORTER_ASSERT(r, dst == 0);
145     }
146 
147     // check a totally arbitrary color
148     src = 0xFACEB004;
149     SkOpts::RGBA_to_rgbA(&dst, &src, 1);
150     REPORTER_ASSERT(r, dst == 0xFACAAD04);
151 
152     // swap red and blue
153     SkOpts::RGBA_to_BGRA(&dst, &src, 1);
154     REPORTER_ASSERT(r, dst == 0xFA04B0CE);
155 
156     // all together now
157     SkOpts::RGBA_to_bgrA(&dst, &src, 1);
158     REPORTER_ASSERT(r, dst == 0xFA04ADCA);
159 }
160