• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkOpts.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