blob: 1355f3be8ca1045bb9ce18fb62474e93cbb5d71b [file] [log] [blame]
msarett438b2ad2015-04-09 12:43:10 -07001/*
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
11// These are the values that we will look for to indicate that the fill was successful
12static const uint8_t kFillIndex = 0x1;
13static const uint32_t kFillColor = 0x22334455;
14
15static void check_fill(skiatest::Reporter* r,
16 const SkImageInfo& imageInfo,
17 uint32_t startRow,
18 size_t rowBytes,
19 uint32_t offset,
20 uint32_t colorOrIndex,
21 SkPMColor* colorTable) {
22
23 // Calculate the total size of the image in bytes. Use the smallest possible size.
24 // The offset value tells us to adjust the pointer from the memory we allocate in order
25 // to test on different memory alignments. If offset is nonzero, we need to increase the
26 // size of the memory we allocate in order to make sure that we have enough. We are
27 // still allocating the smallest possible size.
28 const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset;
29
30 // Create fake image data where every byte has a value of 0
31 SkAutoTDelete<uint8_t> storage((uint8_t*) sk_malloc_throw(totalBytes));
32 memset(storage.get(), 0, totalBytes);
33 // Adjust the pointer in order to test on different memory alignments
34 uint8_t* imageData = storage.get() + offset;
35
36 // Fill image with the fill value starting at the indicated row
37 SkSwizzler::Fill(imageData, imageInfo, rowBytes, startRow, colorOrIndex, colorTable);
38
39 // Ensure that the pixels are filled properly
40 // The bots should catch any memory corruption
41 uint8_t* indexPtr = imageData + startRow * rowBytes;
42 uint32_t* colorPtr = (uint32_t*) indexPtr;
43 for (int32_t y = startRow; y < imageInfo.height(); y++) {
44 for (int32_t x = 0; x < imageInfo.width(); x++) {
45 if (kIndex_8_SkColorType == imageInfo.colorType()) {
46 REPORTER_ASSERT(r, kFillIndex == indexPtr[x]);
47 } else {
48 REPORTER_ASSERT(r, kFillColor == colorPtr[x]);
49 }
50 }
51 indexPtr += rowBytes;
52 colorPtr = (uint32_t*) indexPtr;
53 }
54}
55
56// Test Fill() with different combinations of dimensions, alignment, and padding
57DEF_TEST(SwizzlerFill, r) {
58 // Set up a color table
59 SkPMColor colorTable[kFillIndex + 1];
60 colorTable[kFillIndex] = kFillColor;
61 // Apart from the fill index, we will leave the other colors in the color table uninitialized.
62 // If we incorrectly try to fill with this uninitialized memory, the bots will catch it.
63
64 // Test on an invalid width and representative widths
65 const uint32_t widths[] = { 0, 10, 50 };
66
67 // In order to call Fill(), there must be at least one row to fill
68 // Test on the smallest possible height and representative heights
69 const uint32_t heights[] = { 1, 5, 10 };
70
71 // Test on interesting possibilities for row padding
72 const uint32_t paddings[] = { 0, 1, 2, 3, 4 };
73
74 // Iterate over test dimensions
75 for (uint32_t width : widths) {
76 for (uint32_t height : heights) {
77
78 // Create image info objects
79 const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height,
80 kUnknown_SkAlphaType);
81 const SkImageInfo indexInfo = colorInfo.makeColorType(kIndex_8_SkColorType);
82
83 for (uint32_t padding : paddings) {
84
85 // Calculate row bytes
86 size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width +
87 padding;
88 size_t indexRowBytes = width + padding;
89
90 // If there is padding, we can invent an offset to change the memory alignment
91 for (uint32_t offset = 0; offset <= padding; offset++) {
92
93 // Test all possible start rows
94 for (uint32_t startRow = 0; startRow < height; startRow++) {
95
96 // Fill with an index that we use to look up a color
97 check_fill(r, colorInfo, startRow, colorRowBytes, offset, kFillIndex,
98 colorTable);
99
100 // Fill with a color
101 check_fill(r, colorInfo, startRow, colorRowBytes, offset, kFillColor,
102 NULL);
103
104 // Fill with an index
105 check_fill(r, indexInfo, startRow, indexRowBytes, offset, kFillIndex,
106 NULL);
107 }
108 }
109 }
110 }
111 }
112}