1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2015 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/core/SkSwizzle.h"
9cb93a386Sopenharmony_ci#include "include/private/SkImageInfoPriv.h"
10cb93a386Sopenharmony_ci#include "src/codec/SkSwizzler.h"
11cb93a386Sopenharmony_ci#include "src/core/SkOpts.h"
12cb93a386Sopenharmony_ci#include "tests/Test.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_cistatic void check_fill(skiatest::Reporter* r,
15cb93a386Sopenharmony_ci                       const SkImageInfo& imageInfo,
16cb93a386Sopenharmony_ci                       uint32_t startRow,
17cb93a386Sopenharmony_ci                       uint32_t endRow,
18cb93a386Sopenharmony_ci                       size_t rowBytes,
19cb93a386Sopenharmony_ci                       uint32_t offset) {
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ci    // Calculate the total size of the image in bytes.  Use the smallest possible size.
22cb93a386Sopenharmony_ci    // The offset value tells us to adjust the pointer from the memory we allocate in order
23cb93a386Sopenharmony_ci    // to test on different memory alignments.  If offset is nonzero, we need to increase the
24cb93a386Sopenharmony_ci    // size of the memory we allocate in order to make sure that we have enough.  We are
25cb93a386Sopenharmony_ci    // still allocating the smallest possible size.
26cb93a386Sopenharmony_ci    const size_t totalBytes = imageInfo.computeByteSize(rowBytes) + offset;
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ci    // Create fake image data where every byte has a value of 0
29cb93a386Sopenharmony_ci    std::unique_ptr<uint8_t[]> storage(new uint8_t[totalBytes]);
30cb93a386Sopenharmony_ci    memset(storage.get(), 0, totalBytes);
31cb93a386Sopenharmony_ci    // Adjust the pointer in order to test on different memory alignments
32cb93a386Sopenharmony_ci    uint8_t* imageData = storage.get() + offset;
33cb93a386Sopenharmony_ci    uint8_t* imageStart = imageData + rowBytes * startRow;
34cb93a386Sopenharmony_ci    const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1);
35cb93a386Sopenharmony_ci    SkSampler::Fill(fillInfo, imageStart, rowBytes, SkCodec::kNo_ZeroInitialized);
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci    // Ensure that the pixels are filled properly
38cb93a386Sopenharmony_ci    // The bots should catch any memory corruption
39cb93a386Sopenharmony_ci    uint8_t* indexPtr = imageData + startRow * rowBytes;
40cb93a386Sopenharmony_ci    uint8_t* grayPtr = indexPtr;
41cb93a386Sopenharmony_ci    uint32_t* colorPtr = (uint32_t*) indexPtr;
42cb93a386Sopenharmony_ci    uint16_t* color565Ptr = (uint16_t*) indexPtr;
43cb93a386Sopenharmony_ci    for (uint32_t y = startRow; y <= endRow; y++) {
44cb93a386Sopenharmony_ci        for (int32_t x = 0; x < imageInfo.width(); x++) {
45cb93a386Sopenharmony_ci            switch (imageInfo.colorType()) {
46cb93a386Sopenharmony_ci                case kN32_SkColorType:
47cb93a386Sopenharmony_ci                    REPORTER_ASSERT(r, 0 == colorPtr[x]);
48cb93a386Sopenharmony_ci                    break;
49cb93a386Sopenharmony_ci                case kGray_8_SkColorType:
50cb93a386Sopenharmony_ci                    REPORTER_ASSERT(r, 0 == grayPtr[x]);
51cb93a386Sopenharmony_ci                    break;
52cb93a386Sopenharmony_ci                case kRGB_565_SkColorType:
53cb93a386Sopenharmony_ci                    REPORTER_ASSERT(r, 0 == color565Ptr[x]);
54cb93a386Sopenharmony_ci                    break;
55cb93a386Sopenharmony_ci                default:
56cb93a386Sopenharmony_ci                    REPORTER_ASSERT(r, false);
57cb93a386Sopenharmony_ci                    break;
58cb93a386Sopenharmony_ci            }
59cb93a386Sopenharmony_ci        }
60cb93a386Sopenharmony_ci        indexPtr += rowBytes;
61cb93a386Sopenharmony_ci        colorPtr = (uint32_t*) indexPtr;
62cb93a386Sopenharmony_ci    }
63cb93a386Sopenharmony_ci}
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_ci// Test Fill() with different combinations of dimensions, alignment, and padding
66cb93a386Sopenharmony_ciDEF_TEST(SwizzlerFill, r) {
67cb93a386Sopenharmony_ci    // Test on an invalid width and representative widths
68cb93a386Sopenharmony_ci    const uint32_t widths[] = { 0, 10, 50 };
69cb93a386Sopenharmony_ci
70cb93a386Sopenharmony_ci    // In order to call Fill(), there must be at least one row to fill
71cb93a386Sopenharmony_ci    // Test on the smallest possible height and representative heights
72cb93a386Sopenharmony_ci    const uint32_t heights[] = { 1, 5, 10 };
73cb93a386Sopenharmony_ci
74cb93a386Sopenharmony_ci    // Test on interesting possibilities for row padding
75cb93a386Sopenharmony_ci    const uint32_t paddings[] = { 0, 4 };
76cb93a386Sopenharmony_ci
77cb93a386Sopenharmony_ci    // Iterate over test dimensions
78cb93a386Sopenharmony_ci    for (uint32_t width : widths) {
79cb93a386Sopenharmony_ci        for (uint32_t height : heights) {
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci            // Create image info objects
82cb93a386Sopenharmony_ci            const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kUnknown_SkAlphaType);
83cb93a386Sopenharmony_ci            const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColorType);
84cb93a386Sopenharmony_ci            const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_SkColorType);
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci            for (uint32_t padding : paddings) {
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci                // Calculate row bytes
89cb93a386Sopenharmony_ci                const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width
90cb93a386Sopenharmony_ci                        + padding;
91cb93a386Sopenharmony_ci                const size_t indexRowBytes = width + padding;
92cb93a386Sopenharmony_ci                const size_t grayRowBytes = indexRowBytes;
93cb93a386Sopenharmony_ci                const size_t color565RowBytes =
94cb93a386Sopenharmony_ci                        SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding;
95cb93a386Sopenharmony_ci
96cb93a386Sopenharmony_ci                // If there is padding, we can invent an offset to change the memory alignment
97cb93a386Sopenharmony_ci                for (uint32_t offset = 0; offset <= padding; offset += 4) {
98cb93a386Sopenharmony_ci
99cb93a386Sopenharmony_ci                    // Test all possible start rows with all possible end rows
100cb93a386Sopenharmony_ci                    for (uint32_t startRow = 0; startRow < height; startRow++) {
101cb93a386Sopenharmony_ci                        for (uint32_t endRow = startRow; endRow < height; endRow++) {
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci                            // Test fill with each color type
104cb93a386Sopenharmony_ci                            check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset);
105cb93a386Sopenharmony_ci                            check_fill(r, grayInfo, startRow, endRow, grayRowBytes, offset);
106cb93a386Sopenharmony_ci                            check_fill(r, color565Info, startRow, endRow, color565RowBytes, offset);
107cb93a386Sopenharmony_ci                        }
108cb93a386Sopenharmony_ci                    }
109cb93a386Sopenharmony_ci                }
110cb93a386Sopenharmony_ci            }
111cb93a386Sopenharmony_ci        }
112cb93a386Sopenharmony_ci    }
113cb93a386Sopenharmony_ci}
114cb93a386Sopenharmony_ci
115cb93a386Sopenharmony_ciDEF_TEST(SwizzleOpts, r) {
116cb93a386Sopenharmony_ci    uint32_t dst, src;
117cb93a386Sopenharmony_ci
118cb93a386Sopenharmony_ci    // forall c, c*255 == c, c*0 == 0
119cb93a386Sopenharmony_ci    for (int c = 0; c <= 255; c++) {
120cb93a386Sopenharmony_ci        src = (255<<24) | c;
121cb93a386Sopenharmony_ci        SkOpts::RGBA_to_rgbA(&dst, &src, 1);
122cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, dst == src);
123cb93a386Sopenharmony_ci        SkOpts::RGBA_to_bgrA(&dst, &src, 1);
124cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, dst == (uint32_t)((255<<24) | (c<<16)));
125cb93a386Sopenharmony_ci
126cb93a386Sopenharmony_ci        src = (0<<24) | c;
127cb93a386Sopenharmony_ci        SkOpts::RGBA_to_rgbA(&dst, &src, 1);
128cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, dst == 0);
129cb93a386Sopenharmony_ci        SkOpts::RGBA_to_bgrA(&dst, &src, 1);
130cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, dst == 0);
131cb93a386Sopenharmony_ci    }
132cb93a386Sopenharmony_ci
133cb93a386Sopenharmony_ci    // check a totally arbitrary color
134cb93a386Sopenharmony_ci    src = 0xFACEB004;
135cb93a386Sopenharmony_ci    SkOpts::RGBA_to_rgbA(&dst, &src, 1);
136cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, dst == 0xFACAAD04);
137cb93a386Sopenharmony_ci
138cb93a386Sopenharmony_ci    // swap red and blue
139cb93a386Sopenharmony_ci    SkOpts::RGBA_to_BGRA(&dst, &src, 1);
140cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, dst == 0xFA04B0CE);
141cb93a386Sopenharmony_ci
142cb93a386Sopenharmony_ci    // all together now
143cb93a386Sopenharmony_ci    SkOpts::RGBA_to_bgrA(&dst, &src, 1);
144cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, dst == 0xFA04ADCA);
145cb93a386Sopenharmony_ci}
146cb93a386Sopenharmony_ci
147cb93a386Sopenharmony_ciDEF_TEST(PublicSwizzleOpts, r) {
148cb93a386Sopenharmony_ci    uint32_t dst, src;
149cb93a386Sopenharmony_ci
150cb93a386Sopenharmony_ci    // check a totally arbitrary color
151cb93a386Sopenharmony_ci    src = 0xFACEB004;
152cb93a386Sopenharmony_ci    SkSwapRB(&dst, &src, 1);
153cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, dst == 0xFA04B0CE);
154cb93a386Sopenharmony_ci}
155