blob: 147dfaa83d2ee637d7f23f191a5d6520cad05661 [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,
msarett3c309db2015-04-10 14:36:48 -070018 uint32_t endRow,
msarett438b2ad2015-04-09 12:43:10 -070019 size_t rowBytes,
20 uint32_t offset,
21 uint32_t colorOrIndex,
22 SkPMColor* colorTable) {
23
24 // Calculate the total size of the image in bytes. Use the smallest possible size.
25 // The offset value tells us to adjust the pointer from the memory we allocate in order
26 // to test on different memory alignments. If offset is nonzero, we need to increase the
27 // size of the memory we allocate in order to make sure that we have enough. We are
28 // still allocating the smallest possible size.
29 const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset;
30
31 // Create fake image data where every byte has a value of 0
msarett29be7952015-04-09 13:14:40 -070032 SkAutoTDeleteArray<uint8_t> storage(SkNEW_ARRAY(uint8_t, totalBytes));
msarett438b2ad2015-04-09 12:43:10 -070033 memset(storage.get(), 0, totalBytes);
34 // Adjust the pointer in order to test on different memory alignments
35 uint8_t* imageData = storage.get() + offset;
msarett3c309db2015-04-10 14:36:48 -070036 uint8_t* imageStart = imageData + rowBytes * startRow;
msarett438b2ad2015-04-09 12:43:10 -070037
38 // Fill image with the fill value starting at the indicated row
msarett3c309db2015-04-10 14:36:48 -070039 SkSwizzler::Fill(imageStart, imageInfo, rowBytes, endRow - startRow + 1, colorOrIndex,
40 colorTable);
msarett438b2ad2015-04-09 12:43:10 -070041
42 // Ensure that the pixels are filled properly
43 // The bots should catch any memory corruption
44 uint8_t* indexPtr = imageData + startRow * rowBytes;
45 uint32_t* colorPtr = (uint32_t*) indexPtr;
msarett3c309db2015-04-10 14:36:48 -070046 for (uint32_t y = startRow; y <= endRow; y++) {
msarett438b2ad2015-04-09 12:43:10 -070047 for (int32_t x = 0; x < imageInfo.width(); x++) {
48 if (kIndex_8_SkColorType == imageInfo.colorType()) {
49 REPORTER_ASSERT(r, kFillIndex == indexPtr[x]);
50 } else {
51 REPORTER_ASSERT(r, kFillColor == colorPtr[x]);
52 }
53 }
54 indexPtr += rowBytes;
55 colorPtr = (uint32_t*) indexPtr;
56 }
57}
58
59// Test Fill() with different combinations of dimensions, alignment, and padding
60DEF_TEST(SwizzlerFill, r) {
61 // Set up a color table
62 SkPMColor colorTable[kFillIndex + 1];
63 colorTable[kFillIndex] = kFillColor;
64 // Apart from the fill index, we will leave the other colors in the color table uninitialized.
65 // If we incorrectly try to fill with this uninitialized memory, the bots will catch it.
66
67 // Test on an invalid width and representative widths
68 const uint32_t widths[] = { 0, 10, 50 };
69
70 // In order to call Fill(), there must be at least one row to fill
71 // Test on the smallest possible height and representative heights
72 const uint32_t heights[] = { 1, 5, 10 };
73
74 // Test on interesting possibilities for row padding
75 const uint32_t paddings[] = { 0, 1, 2, 3, 4 };
76
77 // Iterate over test dimensions
78 for (uint32_t width : widths) {
79 for (uint32_t height : heights) {
80
81 // Create image info objects
82 const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height,
83 kUnknown_SkAlphaType);
84 const SkImageInfo indexInfo = colorInfo.makeColorType(kIndex_8_SkColorType);
85
86 for (uint32_t padding : paddings) {
87
88 // Calculate row bytes
89 size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width +
90 padding;
91 size_t indexRowBytes = width + padding;
92
93 // If there is padding, we can invent an offset to change the memory alignment
94 for (uint32_t offset = 0; offset <= padding; offset++) {
95
msarett3c309db2015-04-10 14:36:48 -070096 // Test all possible start rows with all possible end rows
msarett438b2ad2015-04-09 12:43:10 -070097 for (uint32_t startRow = 0; startRow < height; startRow++) {
msarett3c309db2015-04-10 14:36:48 -070098 for (uint32_t endRow = startRow; endRow < height; endRow++) {
msarett438b2ad2015-04-09 12:43:10 -070099
msarett3c309db2015-04-10 14:36:48 -0700100 // Fill with an index that we use to look up a color
101 check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset,
102 kFillIndex, colorTable);
msarett438b2ad2015-04-09 12:43:10 -0700103
msarett3c309db2015-04-10 14:36:48 -0700104 // Fill with a color
105 check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset,
106 kFillColor, NULL);
msarett438b2ad2015-04-09 12:43:10 -0700107
msarett3c309db2015-04-10 14:36:48 -0700108 // Fill with an index
109 check_fill(r, indexInfo, startRow, endRow, indexRowBytes, offset,
110 kFillIndex, NULL);
111 }
msarett438b2ad2015-04-09 12:43:10 -0700112 }
113 }
114 }
115 }
116 }
117}