blob: 95aaf386392b87be3bfa677e337bed4778926e7c [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
msarette6dd0042015-10-09 11:07:34 -070012static const uint8_t kFillIndex = 0x11;
13static const uint8_t kFillGray = 0x22;
14static const uint16_t kFill565 = 0x3344;
15static const uint32_t kFillColor = 0x55667788;
msarett438b2ad2015-04-09 12:43:10 -070016
17static void check_fill(skiatest::Reporter* r,
18 const SkImageInfo& imageInfo,
19 uint32_t startRow,
msarett3c309db2015-04-10 14:36:48 -070020 uint32_t endRow,
msarett438b2ad2015-04-09 12:43:10 -070021 size_t rowBytes,
22 uint32_t offset,
msarette6dd0042015-10-09 11:07:34 -070023 uint32_t colorOrIndex) {
msarett438b2ad2015-04-09 12:43:10 -070024
25 // Calculate the total size of the image in bytes. Use the smallest possible size.
26 // The offset value tells us to adjust the pointer from the memory we allocate in order
27 // to test on different memory alignments. If offset is nonzero, we need to increase the
28 // size of the memory we allocate in order to make sure that we have enough. We are
29 // still allocating the smallest possible size.
30 const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset;
31
32 // Create fake image data where every byte has a value of 0
halcanary385fe4d2015-08-26 13:07:48 -070033 SkAutoTDeleteArray<uint8_t> storage(new uint8_t[totalBytes]);
msarett438b2ad2015-04-09 12:43:10 -070034 memset(storage.get(), 0, totalBytes);
35 // Adjust the pointer in order to test on different memory alignments
36 uint8_t* imageData = storage.get() + offset;
msarett3c309db2015-04-10 14:36:48 -070037 uint8_t* imageStart = imageData + rowBytes * startRow;
msarette6dd0042015-10-09 11:07:34 -070038 const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1);
39 SkSampler::Fill(fillInfo, imageStart, rowBytes, colorOrIndex, SkCodec::kNo_ZeroInitialized);
msarett438b2ad2015-04-09 12:43:10 -070040
41 // Ensure that the pixels are filled properly
42 // The bots should catch any memory corruption
43 uint8_t* indexPtr = imageData + startRow * rowBytes;
msarette16b04a2015-04-15 07:32:19 -070044 uint8_t* grayPtr = indexPtr;
msarett438b2ad2015-04-09 12:43:10 -070045 uint32_t* colorPtr = (uint32_t*) indexPtr;
msarette6dd0042015-10-09 11:07:34 -070046 uint16_t* color565Ptr = (uint16_t*) indexPtr;
msarett3c309db2015-04-10 14:36:48 -070047 for (uint32_t y = startRow; y <= endRow; y++) {
msarett438b2ad2015-04-09 12:43:10 -070048 for (int32_t x = 0; x < imageInfo.width(); x++) {
msarette16b04a2015-04-15 07:32:19 -070049 switch (imageInfo.colorType()) {
50 case kIndex_8_SkColorType:
51 REPORTER_ASSERT(r, kFillIndex == indexPtr[x]);
52 break;
53 case kN32_SkColorType:
54 REPORTER_ASSERT(r, kFillColor == colorPtr[x]);
55 break;
56 case kGray_8_SkColorType:
msarette6dd0042015-10-09 11:07:34 -070057 REPORTER_ASSERT(r, kFillGray == grayPtr[x]);
58 break;
59 case kRGB_565_SkColorType:
60 REPORTER_ASSERT(r, kFill565 == color565Ptr[x]);
msarette16b04a2015-04-15 07:32:19 -070061 break;
62 default:
63 REPORTER_ASSERT(r, false);
64 break;
msarett438b2ad2015-04-09 12:43:10 -070065 }
66 }
67 indexPtr += rowBytes;
68 colorPtr = (uint32_t*) indexPtr;
69 }
70}
71
72// Test Fill() with different combinations of dimensions, alignment, and padding
73DEF_TEST(SwizzlerFill, r) {
msarett438b2ad2015-04-09 12:43:10 -070074 // 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
msarette6dd0042015-10-09 11:07:34 -070082 const uint32_t paddings[] = { 0, 4 };
msarett438b2ad2015-04-09 12:43:10 -070083
84 // Iterate over test dimensions
85 for (uint32_t width : widths) {
86 for (uint32_t height : heights) {
87
88 // Create image info objects
msarette6dd0042015-10-09 11:07:34 -070089 const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kUnknown_SkAlphaType);
msarette16b04a2015-04-15 07:32:19 -070090 const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColorType);
msarette6dd0042015-10-09 11:07:34 -070091 const SkImageInfo indexInfo = colorInfo.makeColorType(kIndex_8_SkColorType);
92 const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_SkColorType);
msarett438b2ad2015-04-09 12:43:10 -070093
94 for (uint32_t padding : paddings) {
95
96 // Calculate row bytes
msarette6dd0042015-10-09 11:07:34 -070097 const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width
98 + padding;
99 const size_t indexRowBytes = width + padding;
100 const size_t grayRowBytes = indexRowBytes;
101 const size_t color565RowBytes =
102 SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding;
msarett438b2ad2015-04-09 12:43:10 -0700103
104 // If there is padding, we can invent an offset to change the memory alignment
msarette6dd0042015-10-09 11:07:34 -0700105 for (uint32_t offset = 0; offset <= padding; offset += 4) {
msarett438b2ad2015-04-09 12:43:10 -0700106
msarett3c309db2015-04-10 14:36:48 -0700107 // Test all possible start rows with all possible end rows
msarett438b2ad2015-04-09 12:43:10 -0700108 for (uint32_t startRow = 0; startRow < height; startRow++) {
msarett3c309db2015-04-10 14:36:48 -0700109 for (uint32_t endRow = startRow; endRow < height; endRow++) {
msarett438b2ad2015-04-09 12:43:10 -0700110
msarette6dd0042015-10-09 11:07:34 -0700111 // Test fill with each color type
msarett3c309db2015-04-10 14:36:48 -0700112 check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset,
msarette6dd0042015-10-09 11:07:34 -0700113 kFillColor);
msarett3c309db2015-04-10 14:36:48 -0700114 check_fill(r, indexInfo, startRow, endRow, indexRowBytes, offset,
msarette6dd0042015-10-09 11:07:34 -0700115 kFillIndex);
msarette16b04a2015-04-15 07:32:19 -0700116 check_fill(r, grayInfo, startRow, endRow, grayRowBytes, offset,
msarette6dd0042015-10-09 11:07:34 -0700117 kFillGray);
118 check_fill(r, color565Info, startRow, endRow, color565RowBytes, offset,
119 kFill565);
msarett3c309db2015-04-10 14:36:48 -0700120 }
msarett438b2ad2015-04-09 12:43:10 -0700121 }
122 }
123 }
124 }
125 }
126}