Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: tests/SwizzlerTest.cpp

Issue 1332053002: Fill incomplete images in SkCodec parent class (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Forgot to add SkSampler.cpp Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkSwizzler.h" 8 #include "SkSwizzler.h"
9 #include "Test.h" 9 #include "Test.h"
10 10
11 // These are the values that we will look for to indicate that the fill was succ essful 11 // These are the values that we will look for to indicate that the fill was succ essful
12 static const uint8_t kFillIndex = 0x1; 12 static const uint8_t kFillIndex = 0x11;
13 static const uint32_t kFillColor = 0x22334455; 13 static const uint8_t kFillGray = 0x22;
14 static const uint16_t kFill565 = 0x3344;
15 static const uint32_t kFillColor = 0x55667788;
16
17 // We just need this function to return any valid SrcConfig, so we can create a swizzler.
18 // The SrcConfig is not actually relevant to testing the fill function.
19 static SkSwizzler::SrcConfig get_valid_src_config(SkColorType colorType) {
20 switch (colorType) {
21 case kGray_8_SkColorType:
22 return SkSwizzler::kGray;
23 default:
24 return SkSwizzler::kIndex;
25 }
26 }
14 27
15 static void check_fill(skiatest::Reporter* r, 28 static void check_fill(skiatest::Reporter* r,
16 const SkImageInfo& imageInfo, 29 const SkImageInfo& imageInfo,
17 uint32_t startRow, 30 uint32_t startRow,
18 uint32_t endRow, 31 uint32_t endRow,
19 size_t rowBytes, 32 size_t rowBytes,
20 uint32_t offset, 33 uint32_t offset,
21 uint32_t colorOrIndex, 34 uint32_t colorOrIndex) {
22 SkPMColor* colorTable) {
23 35
24 // Calculate the total size of the image in bytes. Use the smallest possibl e size. 36 // Calculate the total size of the image in bytes. Use the smallest possibl e size.
25 // The offset value tells us to adjust the pointer from the memory we alloca te in order 37 // The offset value tells us to adjust the pointer from the memory we alloca te in order
26 // to test on different memory alignments. If offset is nonzero, we need to increase the 38 // 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 39 // size of the memory we allocate in order to make sure that we have enough. We are
28 // still allocating the smallest possible size. 40 // still allocating the smallest possible size.
29 const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset; 41 const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset;
30 42
31 // Create fake image data where every byte has a value of 0 43 // Create fake image data where every byte has a value of 0
32 SkAutoTDeleteArray<uint8_t> storage(new uint8_t[totalBytes]); 44 SkAutoTDeleteArray<uint8_t> storage(new uint8_t[totalBytes]);
33 memset(storage.get(), 0, totalBytes); 45 memset(storage.get(), 0, totalBytes);
34 // Adjust the pointer in order to test on different memory alignments 46 // Adjust the pointer in order to test on different memory alignments
35 uint8_t* imageData = storage.get() + offset; 47 uint8_t* imageData = storage.get() + offset;
36 uint8_t* imageStart = imageData + rowBytes * startRow; 48 uint8_t* imageStart = imageData + rowBytes * startRow;
37 49
38 // Fill image with the fill value starting at the indicated row 50 // The SrcConfig for the swizzler does not matter here. We just need to cho ose one that is
39 SkSwizzler::Fill(imageStart, imageInfo, rowBytes, endRow - startRow + 1, col orOrIndex, 51 // supported so the swizzler is created successfully.
40 colorTable, SkCodec::kNo_ZeroInitialized); 52 SkSwizzler::SrcConfig config = get_valid_src_config(imageInfo.colorType());
53 SkPMColor colorTable[256];
54 SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler(config, colorT able, imageInfo,
55 SkCodec::kNo_ZeroInitialized));
56 SkASSERT(nullptr != swizzler);
57 swizzler->fill(imageStart, imageInfo.colorType(), endRow - startRow + 1, row Bytes, colorOrIndex,
58 SkCodec::kNo_ZeroInitialized);
41 59
42 // Ensure that the pixels are filled properly 60 // Ensure that the pixels are filled properly
43 // The bots should catch any memory corruption 61 // The bots should catch any memory corruption
44 uint8_t* indexPtr = imageData + startRow * rowBytes; 62 uint8_t* indexPtr = imageData + startRow * rowBytes;
45 uint8_t* grayPtr = indexPtr; 63 uint8_t* grayPtr = indexPtr;
46 uint32_t* colorPtr = (uint32_t*) indexPtr; 64 uint32_t* colorPtr = (uint32_t*) indexPtr;
65 uint16_t* color565Ptr = (uint16_t*) indexPtr;
47 for (uint32_t y = startRow; y <= endRow; y++) { 66 for (uint32_t y = startRow; y <= endRow; y++) {
48 for (int32_t x = 0; x < imageInfo.width(); x++) { 67 for (int32_t x = 0; x < imageInfo.width(); x++) {
49 switch (imageInfo.colorType()) { 68 switch (imageInfo.colorType()) {
50 case kIndex_8_SkColorType: 69 case kIndex_8_SkColorType:
51 REPORTER_ASSERT(r, kFillIndex == indexPtr[x]); 70 REPORTER_ASSERT(r, kFillIndex == indexPtr[x]);
52 break; 71 break;
53 case kN32_SkColorType: 72 case kN32_SkColorType:
54 REPORTER_ASSERT(r, kFillColor == colorPtr[x]); 73 REPORTER_ASSERT(r, kFillColor == colorPtr[x]);
55 break; 74 break;
56 case kGray_8_SkColorType: 75 case kGray_8_SkColorType:
57 // We always fill kGray with black 76 REPORTER_ASSERT(r, kFillGray == grayPtr[x]);
58 REPORTER_ASSERT(r, (uint8_t) kFillColor == grayPtr[x]); 77 break;
78 case kRGB_565_SkColorType:
79 REPORTER_ASSERT(r, kFill565 == color565Ptr[x]);
59 break; 80 break;
60 default: 81 default:
61 REPORTER_ASSERT(r, false); 82 REPORTER_ASSERT(r, false);
62 break; 83 break;
63 } 84 }
64 } 85 }
65 indexPtr += rowBytes; 86 indexPtr += rowBytes;
66 colorPtr = (uint32_t*) indexPtr; 87 colorPtr = (uint32_t*) indexPtr;
67 } 88 }
68 } 89 }
69 90
70 // Test Fill() with different combinations of dimensions, alignment, and padding 91 // Test Fill() with different combinations of dimensions, alignment, and padding
71 DEF_TEST(SwizzlerFill, r) { 92 DEF_TEST(SwizzlerFill, r) {
72 // Set up a color table
73 SkPMColor colorTable[kFillIndex + 1];
74 colorTable[kFillIndex] = kFillColor;
75 // Apart from the fill index, we will leave the other colors in the color ta ble uninitialized.
76 // If we incorrectly try to fill with this uninitialized memory, the bots wi ll catch it.
77
78 // Test on an invalid width and representative widths 93 // Test on an invalid width and representative widths
79 const uint32_t widths[] = { 0, 10, 50 }; 94 const uint32_t widths[] = { 0, 10, 50 };
80 95
81 // In order to call Fill(), there must be at least one row to fill 96 // In order to call Fill(), there must be at least one row to fill
82 // Test on the smallest possible height and representative heights 97 // Test on the smallest possible height and representative heights
83 const uint32_t heights[] = { 1, 5, 10 }; 98 const uint32_t heights[] = { 1, 5, 10 };
84 99
85 // Test on interesting possibilities for row padding 100 // Test on interesting possibilities for row padding
86 const uint32_t paddings[] = { 0, 1, 2, 3, 4 }; 101 const uint32_t paddings[] = { 0, 1, 2, 3, 4 };
87 102
88 // Iterate over test dimensions 103 // Iterate over test dimensions
89 for (uint32_t width : widths) { 104 for (uint32_t width : widths) {
90 for (uint32_t height : heights) { 105 for (uint32_t height : heights) {
91 106
92 // Create image info objects 107 // Create image info objects
93 const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, 108 const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kU nknown_SkAlphaType);
94 kUnknown_SkAlphaType); 109 const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColor Type);
95 const SkImageInfo indexInfo = colorInfo.makeColorType(kIndex_8_SkCol orType); 110 const SkImageInfo indexInfo = colorInfo.makeColorType(kIndex_8_SkCol orType);
96 const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColor Type); 111 const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_Sk ColorType);
97 112
98 for (uint32_t padding : paddings) { 113 for (uint32_t padding : paddings) {
99 114
100 // Calculate row bytes 115 // Calculate row bytes
101 size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType ) * width + 116 const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkCol orType) * width
102 padding; 117 + padding;
103 size_t indexRowBytes = width + padding; 118 const size_t indexRowBytes = width + padding;
104 size_t grayRowBytes = indexRowBytes; 119 const size_t grayRowBytes = indexRowBytes;
120 const size_t color565RowBytes =
121 SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding;
105 122
106 // If there is padding, we can invent an offset to change the me mory alignment 123 // If there is padding, we can invent an offset to change the me mory alignment
107 for (uint32_t offset = 0; offset <= padding; offset++) { 124 for (uint32_t offset = 0; offset <= padding; offset++) {
108 125
109 // Test all possible start rows with all possible end rows 126 // Test all possible start rows with all possible end rows
110 for (uint32_t startRow = 0; startRow < height; startRow++) { 127 for (uint32_t startRow = 0; startRow < height; startRow++) {
111 for (uint32_t endRow = startRow; endRow < height; endRow ++) { 128 for (uint32_t endRow = startRow; endRow < height; endRow ++) {
112 129
113 // Fill with an index that we use to look up a color 130 // Test fill with each color type
114 check_fill(r, colorInfo, startRow, endRow, colorRowB ytes, offset, 131 check_fill(r, colorInfo, startRow, endRow, colorRowB ytes, offset,
115 kFillIndex, colorTable); 132 kFillColor);
116
117 // Fill with a color
118 check_fill(r, colorInfo, startRow, endRow, colorRowB ytes, offset,
119 kFillColor, nullptr);
120
121 // Fill with an index
122 check_fill(r, indexInfo, startRow, endRow, indexRowB ytes, offset, 133 check_fill(r, indexInfo, startRow, endRow, indexRowB ytes, offset,
123 kFillIndex, nullptr); 134 kFillIndex);
124
125 // Fill a grayscale image
126 check_fill(r, grayInfo, startRow, endRow, grayRowByt es, offset, 135 check_fill(r, grayInfo, startRow, endRow, grayRowByt es, offset,
127 kFillColor, nullptr); 136 kFillGray);
137 check_fill(r, color565Info, startRow, endRow, color5 65RowBytes, offset,
138 kFill565);
128 } 139 }
129 } 140 }
130 } 141 }
131 } 142 }
132 } 143 }
133 } 144 }
134 } 145 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698