blob: 655c97eaddd413a163425cf115971ddf5ea78653 [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
tomhudson734351d2016-03-23 10:51:20 -07008#include "SkSwizzle.h"
msarett438b2ad2015-04-09 12:43:10 -07009#include "SkSwizzler.h"
10#include "Test.h"
mtklein55c86ab2016-01-08 06:32:52 -080011#include "SkOpts.h"
msarett438b2ad2015-04-09 12:43:10 -070012
13// These are the values that we will look for to indicate that the fill was successful
msarette6dd0042015-10-09 11:07:34 -070014static const uint8_t kFillGray = 0x22;
15static const uint16_t kFill565 = 0x3344;
16static const uint32_t kFillColor = 0x55667788;
msarett438b2ad2015-04-09 12:43:10 -070017
18static void check_fill(skiatest::Reporter* r,
19 const SkImageInfo& imageInfo,
20 uint32_t startRow,
msarett3c309db2015-04-10 14:36:48 -070021 uint32_t endRow,
msarett438b2ad2015-04-09 12:43:10 -070022 size_t rowBytes,
23 uint32_t offset,
msarette6dd0042015-10-09 11:07:34 -070024 uint32_t colorOrIndex) {
msarett438b2ad2015-04-09 12:43:10 -070025
26 // Calculate the total size of the image in bytes. Use the smallest possible size.
27 // The offset value tells us to adjust the pointer from the memory we allocate in order
28 // to test on different memory alignments. If offset is nonzero, we need to increase the
29 // size of the memory we allocate in order to make sure that we have enough. We are
30 // still allocating the smallest possible size.
31 const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset;
32
33 // Create fake image data where every byte has a value of 0
Ben Wagner7ecc5962016-11-02 17:07:33 -040034 std::unique_ptr<uint8_t[]> storage(new uint8_t[totalBytes]);
msarett438b2ad2015-04-09 12:43:10 -070035 memset(storage.get(), 0, totalBytes);
36 // Adjust the pointer in order to test on different memory alignments
37 uint8_t* imageData = storage.get() + offset;
msarett3c309db2015-04-10 14:36:48 -070038 uint8_t* imageStart = imageData + rowBytes * startRow;
msarette6dd0042015-10-09 11:07:34 -070039 const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1);
40 SkSampler::Fill(fillInfo, imageStart, rowBytes, colorOrIndex, SkCodec::kNo_ZeroInitialized);
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;
msarette16b04a2015-04-15 07:32:19 -070045 uint8_t* grayPtr = indexPtr;
msarett438b2ad2015-04-09 12:43:10 -070046 uint32_t* colorPtr = (uint32_t*) indexPtr;
msarette6dd0042015-10-09 11:07:34 -070047 uint16_t* color565Ptr = (uint16_t*) indexPtr;
msarett3c309db2015-04-10 14:36:48 -070048 for (uint32_t y = startRow; y <= endRow; y++) {
msarett438b2ad2015-04-09 12:43:10 -070049 for (int32_t x = 0; x < imageInfo.width(); x++) {
msarette16b04a2015-04-15 07:32:19 -070050 switch (imageInfo.colorType()) {
msarette16b04a2015-04-15 07:32:19 -070051 case kN32_SkColorType:
52 REPORTER_ASSERT(r, kFillColor == colorPtr[x]);
53 break;
54 case kGray_8_SkColorType:
msarette6dd0042015-10-09 11:07:34 -070055 REPORTER_ASSERT(r, kFillGray == grayPtr[x]);
56 break;
57 case kRGB_565_SkColorType:
58 REPORTER_ASSERT(r, kFill565 == color565Ptr[x]);
msarette16b04a2015-04-15 07:32:19 -070059 break;
60 default:
61 REPORTER_ASSERT(r, false);
62 break;
msarett438b2ad2015-04-09 12:43:10 -070063 }
64 }
65 indexPtr += rowBytes;
66 colorPtr = (uint32_t*) indexPtr;
67 }
68}
69
70// Test Fill() with different combinations of dimensions, alignment, and padding
71DEF_TEST(SwizzlerFill, r) {
msarett438b2ad2015-04-09 12:43:10 -070072 // Test on an invalid width and representative widths
73 const uint32_t widths[] = { 0, 10, 50 };
74
75 // In order to call Fill(), there must be at least one row to fill
76 // Test on the smallest possible height and representative heights
77 const uint32_t heights[] = { 1, 5, 10 };
78
79 // Test on interesting possibilities for row padding
msarette6dd0042015-10-09 11:07:34 -070080 const uint32_t paddings[] = { 0, 4 };
msarett438b2ad2015-04-09 12:43:10 -070081
82 // Iterate over test dimensions
83 for (uint32_t width : widths) {
84 for (uint32_t height : heights) {
85
86 // Create image info objects
msarette6dd0042015-10-09 11:07:34 -070087 const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kUnknown_SkAlphaType);
msarette16b04a2015-04-15 07:32:19 -070088 const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColorType);
msarette6dd0042015-10-09 11:07:34 -070089 const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_SkColorType);
msarett438b2ad2015-04-09 12:43:10 -070090
91 for (uint32_t padding : paddings) {
92
93 // Calculate row bytes
msarette6dd0042015-10-09 11:07:34 -070094 const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width
95 + padding;
96 const size_t indexRowBytes = width + padding;
97 const size_t grayRowBytes = indexRowBytes;
98 const size_t color565RowBytes =
99 SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding;
msarett438b2ad2015-04-09 12:43:10 -0700100
101 // If there is padding, we can invent an offset to change the memory alignment
msarette6dd0042015-10-09 11:07:34 -0700102 for (uint32_t offset = 0; offset <= padding; offset += 4) {
msarett438b2ad2015-04-09 12:43:10 -0700103
msarett3c309db2015-04-10 14:36:48 -0700104 // Test all possible start rows with all possible end rows
msarett438b2ad2015-04-09 12:43:10 -0700105 for (uint32_t startRow = 0; startRow < height; startRow++) {
msarett3c309db2015-04-10 14:36:48 -0700106 for (uint32_t endRow = startRow; endRow < height; endRow++) {
msarett438b2ad2015-04-09 12:43:10 -0700107
msarette6dd0042015-10-09 11:07:34 -0700108 // Test fill with each color type
msarett3c309db2015-04-10 14:36:48 -0700109 check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset,
msarette6dd0042015-10-09 11:07:34 -0700110 kFillColor);
msarette16b04a2015-04-15 07:32:19 -0700111 check_fill(r, grayInfo, startRow, endRow, grayRowBytes, offset,
msarette6dd0042015-10-09 11:07:34 -0700112 kFillGray);
113 check_fill(r, color565Info, startRow, endRow, color565RowBytes, offset,
114 kFill565);
msarett3c309db2015-04-10 14:36:48 -0700115 }
msarett438b2ad2015-04-09 12:43:10 -0700116 }
117 }
118 }
119 }
120 }
121}
mtklein55c86ab2016-01-08 06:32:52 -0800122
123DEF_TEST(SwizzleOpts, r) {
124 uint32_t dst, src;
125
126 // forall c, c*255 == c, c*0 == 0
127 for (int c = 0; c <= 255; c++) {
128 src = (255<<24) | c;
mtklein8bf7b792016-01-22 07:42:53 -0800129 SkOpts::RGBA_to_rgbA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800130 REPORTER_ASSERT(r, dst == src);
mtklein8bf7b792016-01-22 07:42:53 -0800131 SkOpts::RGBA_to_bgrA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800132 REPORTER_ASSERT(r, dst == (uint32_t)((255<<24) | (c<<16)));
133
134 src = (0<<24) | c;
mtklein8bf7b792016-01-22 07:42:53 -0800135 SkOpts::RGBA_to_rgbA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800136 REPORTER_ASSERT(r, dst == 0);
mtklein8bf7b792016-01-22 07:42:53 -0800137 SkOpts::RGBA_to_bgrA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800138 REPORTER_ASSERT(r, dst == 0);
139 }
140
141 // check a totally arbitrary color
142 src = 0xFACEB004;
mtklein8bf7b792016-01-22 07:42:53 -0800143 SkOpts::RGBA_to_rgbA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800144 REPORTER_ASSERT(r, dst == 0xFACAAD04);
145
146 // swap red and blue
mtklein8bf7b792016-01-22 07:42:53 -0800147 SkOpts::RGBA_to_BGRA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800148 REPORTER_ASSERT(r, dst == 0xFA04B0CE);
149
150 // all together now
mtklein8bf7b792016-01-22 07:42:53 -0800151 SkOpts::RGBA_to_bgrA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800152 REPORTER_ASSERT(r, dst == 0xFA04ADCA);
153}
tomhudson734351d2016-03-23 10:51:20 -0700154
155DEF_TEST(PublicSwizzleOpts, r) {
156 uint32_t dst, src;
157
158 // check a totally arbitrary color
159 src = 0xFACEB004;
160 SkSwapRB(&dst, &src, 1);
161 REPORTER_ASSERT(r, dst == 0xFA04B0CE);
162}