blob: 8a96b56f059fc22599c66851ce07de2e110faf21 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkSwizzle.h"
9#include "include/private/SkImageInfoPriv.h"
10#include "src/codec/SkSwizzler.h"
11#include "src/core/SkOpts.h"
12#include "tests/Test.h"
msarett438b2ad2015-04-09 12:43:10 -070013
msarett438b2ad2015-04-09 12:43:10 -070014static void check_fill(skiatest::Reporter* r,
15 const SkImageInfo& imageInfo,
16 uint32_t startRow,
msarett3c309db2015-04-10 14:36:48 -070017 uint32_t endRow,
msarett438b2ad2015-04-09 12:43:10 -070018 size_t rowBytes,
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -040019 uint32_t offset) {
msarett438b2ad2015-04-09 12:43:10 -070020
21 // Calculate the total size of the image in bytes. Use the smallest possible size.
22 // The offset value tells us to adjust the pointer from the memory we allocate in order
23 // to test on different memory alignments. If offset is nonzero, we need to increase the
24 // size of the memory we allocate in order to make sure that we have enough. We are
25 // still allocating the smallest possible size.
Mike Reedf0ffb892017-10-03 14:47:21 -040026 const size_t totalBytes = imageInfo.computeByteSize(rowBytes) + offset;
msarett438b2ad2015-04-09 12:43:10 -070027
28 // Create fake image data where every byte has a value of 0
Ben Wagner7ecc5962016-11-02 17:07:33 -040029 std::unique_ptr<uint8_t[]> storage(new uint8_t[totalBytes]);
msarett438b2ad2015-04-09 12:43:10 -070030 memset(storage.get(), 0, totalBytes);
31 // Adjust the pointer in order to test on different memory alignments
32 uint8_t* imageData = storage.get() + offset;
msarett3c309db2015-04-10 14:36:48 -070033 uint8_t* imageStart = imageData + rowBytes * startRow;
msarette6dd0042015-10-09 11:07:34 -070034 const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -040035 SkSampler::Fill(fillInfo, imageStart, rowBytes, SkCodec::kNo_ZeroInitialized);
msarett438b2ad2015-04-09 12:43:10 -070036
37 // Ensure that the pixels are filled properly
38 // The bots should catch any memory corruption
39 uint8_t* indexPtr = imageData + startRow * rowBytes;
msarette16b04a2015-04-15 07:32:19 -070040 uint8_t* grayPtr = indexPtr;
msarett438b2ad2015-04-09 12:43:10 -070041 uint32_t* colorPtr = (uint32_t*) indexPtr;
msarette6dd0042015-10-09 11:07:34 -070042 uint16_t* color565Ptr = (uint16_t*) indexPtr;
msarett3c309db2015-04-10 14:36:48 -070043 for (uint32_t y = startRow; y <= endRow; y++) {
msarett438b2ad2015-04-09 12:43:10 -070044 for (int32_t x = 0; x < imageInfo.width(); x++) {
msarette16b04a2015-04-15 07:32:19 -070045 switch (imageInfo.colorType()) {
msarette16b04a2015-04-15 07:32:19 -070046 case kN32_SkColorType:
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -040047 REPORTER_ASSERT(r, 0 == colorPtr[x]);
msarette16b04a2015-04-15 07:32:19 -070048 break;
49 case kGray_8_SkColorType:
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -040050 REPORTER_ASSERT(r, 0 == grayPtr[x]);
msarette6dd0042015-10-09 11:07:34 -070051 break;
52 case kRGB_565_SkColorType:
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -040053 REPORTER_ASSERT(r, 0 == color565Ptr[x]);
msarette16b04a2015-04-15 07:32:19 -070054 break;
55 default:
56 REPORTER_ASSERT(r, false);
57 break;
msarett438b2ad2015-04-09 12:43:10 -070058 }
59 }
60 indexPtr += rowBytes;
61 colorPtr = (uint32_t*) indexPtr;
62 }
63}
64
65// Test Fill() with different combinations of dimensions, alignment, and padding
66DEF_TEST(SwizzlerFill, r) {
msarett438b2ad2015-04-09 12:43:10 -070067 // 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
msarette6dd0042015-10-09 11:07:34 -070075 const uint32_t paddings[] = { 0, 4 };
msarett438b2ad2015-04-09 12:43:10 -070076
77 // Iterate over test dimensions
78 for (uint32_t width : widths) {
79 for (uint32_t height : heights) {
80
81 // Create image info objects
msarette6dd0042015-10-09 11:07:34 -070082 const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kUnknown_SkAlphaType);
msarette16b04a2015-04-15 07:32:19 -070083 const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColorType);
msarette6dd0042015-10-09 11:07:34 -070084 const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_SkColorType);
msarett438b2ad2015-04-09 12:43:10 -070085
86 for (uint32_t padding : paddings) {
87
88 // Calculate row bytes
msarette6dd0042015-10-09 11:07:34 -070089 const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width
90 + padding;
91 const size_t indexRowBytes = width + padding;
92 const size_t grayRowBytes = indexRowBytes;
93 const size_t color565RowBytes =
94 SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding;
msarett438b2ad2015-04-09 12:43:10 -070095
96 // If there is padding, we can invent an offset to change the memory alignment
msarette6dd0042015-10-09 11:07:34 -070097 for (uint32_t offset = 0; offset <= padding; offset += 4) {
msarett438b2ad2015-04-09 12:43:10 -070098
msarett3c309db2015-04-10 14:36:48 -070099 // Test all possible start rows with all possible end rows
msarett438b2ad2015-04-09 12:43:10 -0700100 for (uint32_t startRow = 0; startRow < height; startRow++) {
msarett3c309db2015-04-10 14:36:48 -0700101 for (uint32_t endRow = startRow; endRow < height; endRow++) {
msarett438b2ad2015-04-09 12:43:10 -0700102
msarette6dd0042015-10-09 11:07:34 -0700103 // Test fill with each color type
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400104 check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset);
105 check_fill(r, grayInfo, startRow, endRow, grayRowBytes, offset);
106 check_fill(r, color565Info, startRow, endRow, color565RowBytes, offset);
msarett3c309db2015-04-10 14:36:48 -0700107 }
msarett438b2ad2015-04-09 12:43:10 -0700108 }
109 }
110 }
111 }
112 }
113}
mtklein55c86ab2016-01-08 06:32:52 -0800114
115DEF_TEST(SwizzleOpts, r) {
116 uint32_t dst, src;
117
118 // forall c, c*255 == c, c*0 == 0
119 for (int c = 0; c <= 255; c++) {
120 src = (255<<24) | c;
mtklein8bf7b792016-01-22 07:42:53 -0800121 SkOpts::RGBA_to_rgbA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800122 REPORTER_ASSERT(r, dst == src);
mtklein8bf7b792016-01-22 07:42:53 -0800123 SkOpts::RGBA_to_bgrA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800124 REPORTER_ASSERT(r, dst == (uint32_t)((255<<24) | (c<<16)));
125
126 src = (0<<24) | c;
mtklein8bf7b792016-01-22 07:42:53 -0800127 SkOpts::RGBA_to_rgbA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800128 REPORTER_ASSERT(r, dst == 0);
mtklein8bf7b792016-01-22 07:42:53 -0800129 SkOpts::RGBA_to_bgrA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800130 REPORTER_ASSERT(r, dst == 0);
131 }
132
133 // check a totally arbitrary color
134 src = 0xFACEB004;
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 == 0xFACAAD04);
137
138 // swap red and blue
mtklein8bf7b792016-01-22 07:42:53 -0800139 SkOpts::RGBA_to_BGRA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800140 REPORTER_ASSERT(r, dst == 0xFA04B0CE);
141
142 // all together now
mtklein8bf7b792016-01-22 07:42:53 -0800143 SkOpts::RGBA_to_bgrA(&dst, &src, 1);
mtklein55c86ab2016-01-08 06:32:52 -0800144 REPORTER_ASSERT(r, dst == 0xFA04ADCA);
145}
tomhudson734351d2016-03-23 10:51:20 -0700146
147DEF_TEST(PublicSwizzleOpts, r) {
148 uint32_t dst, src;
149
150 // check a totally arbitrary color
151 src = 0xFACEB004;
152 SkSwapRB(&dst, &src, 1);
153 REPORTER_ASSERT(r, dst == 0xFA04B0CE);
154}