blob: 1b34669f9780697809771bdceec1fdd8864950b8 [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 kFillIndex = 0x11;
15static const uint8_t kFillGray = 0x22;
16static const uint16_t kFill565 = 0x3344;
17static const uint32_t kFillColor = 0x55667788;
msarett438b2ad2015-04-09 12:43:10 -070018
19static void check_fill(skiatest::Reporter* r,
20 const SkImageInfo& imageInfo,
21 uint32_t startRow,
msarett3c309db2015-04-10 14:36:48 -070022 uint32_t endRow,
msarett438b2ad2015-04-09 12:43:10 -070023 size_t rowBytes,
24 uint32_t offset,
msarette6dd0042015-10-09 11:07:34 -070025 uint32_t colorOrIndex) {
msarett438b2ad2015-04-09 12:43:10 -070026
27 // Calculate the total size of the image in bytes. Use the smallest possible size.
28 // The offset value tells us to adjust the pointer from the memory we allocate in order
29 // to test on different memory alignments. If offset is nonzero, we need to increase the
30 // size of the memory we allocate in order to make sure that we have enough. We are
31 // still allocating the smallest possible size.
32 const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset;
33
34 // Create fake image data where every byte has a value of 0
halcanary385fe4d2015-08-26 13:07:48 -070035 SkAutoTDeleteArray<uint8_t> storage(new uint8_t[totalBytes]);
msarett438b2ad2015-04-09 12:43:10 -070036 memset(storage.get(), 0, totalBytes);
37 // Adjust the pointer in order to test on different memory alignments
38 uint8_t* imageData = storage.get() + offset;
msarett3c309db2015-04-10 14:36:48 -070039 uint8_t* imageStart = imageData + rowBytes * startRow;
msarette6dd0042015-10-09 11:07:34 -070040 const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1);
41 SkSampler::Fill(fillInfo, imageStart, rowBytes, colorOrIndex, SkCodec::kNo_ZeroInitialized);
msarett438b2ad2015-04-09 12:43:10 -070042
43 // Ensure that the pixels are filled properly
44 // The bots should catch any memory corruption
45 uint8_t* indexPtr = imageData + startRow * rowBytes;
msarette16b04a2015-04-15 07:32:19 -070046 uint8_t* grayPtr = indexPtr;
msarett438b2ad2015-04-09 12:43:10 -070047 uint32_t* colorPtr = (uint32_t*) indexPtr;
msarette6dd0042015-10-09 11:07:34 -070048 uint16_t* color565Ptr = (uint16_t*) indexPtr;
msarett3c309db2015-04-10 14:36:48 -070049 for (uint32_t y = startRow; y <= endRow; y++) {
msarett438b2ad2015-04-09 12:43:10 -070050 for (int32_t x = 0; x < imageInfo.width(); x++) {
msarette16b04a2015-04-15 07:32:19 -070051 switch (imageInfo.colorType()) {
52 case kIndex_8_SkColorType:
53 REPORTER_ASSERT(r, kFillIndex == indexPtr[x]);
54 break;
55 case kN32_SkColorType:
56 REPORTER_ASSERT(r, kFillColor == colorPtr[x]);
57 break;
58 case kGray_8_SkColorType:
msarette6dd0042015-10-09 11:07:34 -070059 REPORTER_ASSERT(r, kFillGray == grayPtr[x]);
60 break;
61 case kRGB_565_SkColorType:
62 REPORTER_ASSERT(r, kFill565 == color565Ptr[x]);
msarette16b04a2015-04-15 07:32:19 -070063 break;
64 default:
65 REPORTER_ASSERT(r, false);
66 break;
msarett438b2ad2015-04-09 12:43:10 -070067 }
68 }
69 indexPtr += rowBytes;
70 colorPtr = (uint32_t*) indexPtr;
71 }
72}
73
74// Test Fill() with different combinations of dimensions, alignment, and padding
75DEF_TEST(SwizzlerFill, r) {
msarett438b2ad2015-04-09 12:43:10 -070076 // Test on an invalid width and representative widths
77 const uint32_t widths[] = { 0, 10, 50 };
78
79 // In order to call Fill(), there must be at least one row to fill
80 // Test on the smallest possible height and representative heights
81 const uint32_t heights[] = { 1, 5, 10 };
82
83 // Test on interesting possibilities for row padding
msarette6dd0042015-10-09 11:07:34 -070084 const uint32_t paddings[] = { 0, 4 };
msarett438b2ad2015-04-09 12:43:10 -070085
86 // Iterate over test dimensions
87 for (uint32_t width : widths) {
88 for (uint32_t height : heights) {
89
90 // Create image info objects
msarette6dd0042015-10-09 11:07:34 -070091 const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kUnknown_SkAlphaType);
msarette16b04a2015-04-15 07:32:19 -070092 const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColorType);
msarette6dd0042015-10-09 11:07:34 -070093 const SkImageInfo indexInfo = colorInfo.makeColorType(kIndex_8_SkColorType);
94 const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_SkColorType);
msarett438b2ad2015-04-09 12:43:10 -070095
96 for (uint32_t padding : paddings) {
97
98 // Calculate row bytes
msarette6dd0042015-10-09 11:07:34 -070099 const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width
100 + padding;
101 const size_t indexRowBytes = width + padding;
102 const size_t grayRowBytes = indexRowBytes;
103 const size_t color565RowBytes =
104 SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding;
msarett438b2ad2015-04-09 12:43:10 -0700105
106 // If there is padding, we can invent an offset to change the memory alignment
msarette6dd0042015-10-09 11:07:34 -0700107 for (uint32_t offset = 0; offset <= padding; offset += 4) {
msarett438b2ad2015-04-09 12:43:10 -0700108
msarett3c309db2015-04-10 14:36:48 -0700109 // Test all possible start rows with all possible end rows
msarett438b2ad2015-04-09 12:43:10 -0700110 for (uint32_t startRow = 0; startRow < height; startRow++) {
msarett3c309db2015-04-10 14:36:48 -0700111 for (uint32_t endRow = startRow; endRow < height; endRow++) {
msarett438b2ad2015-04-09 12:43:10 -0700112
msarette6dd0042015-10-09 11:07:34 -0700113 // Test fill with each color type
msarett3c309db2015-04-10 14:36:48 -0700114 check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset,
msarette6dd0042015-10-09 11:07:34 -0700115 kFillColor);
msarett3c309db2015-04-10 14:36:48 -0700116 check_fill(r, indexInfo, startRow, endRow, indexRowBytes, offset,
msarette6dd0042015-10-09 11:07:34 -0700117 kFillIndex);
msarette16b04a2015-04-15 07:32:19 -0700118 check_fill(r, grayInfo, startRow, endRow, grayRowBytes, offset,
msarette6dd0042015-10-09 11:07:34 -0700119 kFillGray);
120 check_fill(r, color565Info, startRow, endRow, color565RowBytes, offset,
121 kFill565);
msarett3c309db2015-04-10 14:36:48 -0700122 }
msarett438b2ad2015-04-09 12:43:10 -0700123 }
124 }
125 }
126 }
127 }
128}
mtklein55c86ab2016-01-08 06:32:52 -0800129
130DEF_TEST(SwizzleOpts, r) {
131 uint32_t dst, src;
132
133 // forall c, c*255 == c, c*0 == 0
134 for (int c = 0; c <= 255; c++) {
135 src = (255<<24) | c;
mtklein8bf7b792016-01-22 07:42:53 -0800136 SkOpts::RGBA_to_rgbA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800137 REPORTER_ASSERT(r, dst == src);
mtklein8bf7b792016-01-22 07:42:53 -0800138 SkOpts::RGBA_to_bgrA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800139 REPORTER_ASSERT(r, dst == (uint32_t)((255<<24) | (c<<16)));
140
141 src = (0<<24) | c;
mtklein8bf7b792016-01-22 07:42:53 -0800142 SkOpts::RGBA_to_rgbA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800143 REPORTER_ASSERT(r, dst == 0);
mtklein8bf7b792016-01-22 07:42:53 -0800144 SkOpts::RGBA_to_bgrA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800145 REPORTER_ASSERT(r, dst == 0);
146 }
147
148 // check a totally arbitrary color
149 src = 0xFACEB004;
mtklein8bf7b792016-01-22 07:42:53 -0800150 SkOpts::RGBA_to_rgbA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800151 REPORTER_ASSERT(r, dst == 0xFACAAD04);
152
153 // swap red and blue
mtklein8bf7b792016-01-22 07:42:53 -0800154 SkOpts::RGBA_to_BGRA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800155 REPORTER_ASSERT(r, dst == 0xFA04B0CE);
156
157 // all together now
mtklein8bf7b792016-01-22 07:42:53 -0800158 SkOpts::RGBA_to_bgrA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800159 REPORTER_ASSERT(r, dst == 0xFA04ADCA);
160}
tomhudson734351d2016-03-23 10:51:20 -0700161
162DEF_TEST(PublicSwizzleOpts, r) {
163 uint32_t dst, src;
164
165 // check a totally arbitrary color
166 src = 0xFACEB004;
167 SkSwapRB(&dst, &src, 1);
168 REPORTER_ASSERT(r, dst == 0xFA04B0CE);
169}