blob: 429f8afd14f9ef683ae1e768a9d5fcc0fdb7ec65 [file] [log] [blame]
egdaniel3fe03272016-08-15 10:59:17 -07001/*
2 * Copyright 2016 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/*
9 * This is a straightforward test of using packed pixel configs (4444, 565).
10 * This test will make sure that these RGBA_4444 and RGB_565 are always supported
11 * as valid texturing configs.
12 */
13
14#include "Test.h"
15
16#if SK_SUPPORT_GPU
17#include "GrContext.h"
18#include "GrTexture.h"
19
20static const int DEV_W = 10, DEV_H = 10;
21static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
22static const uint8_t TOL = 0x4;
23
24static void check_component(skiatest::Reporter* reporter, uint8_t control, uint8_t test) {
25 uint8_t diff = 0;
26 if (control >= test) {
27 diff = control - test;
28 } else {
29 diff = test - control;
30 }
31 REPORTER_ASSERT(reporter, diff < TOL);
32}
33
34static uint8_t expand_value(uint8_t original, int sigBits) {
35 SkASSERT(sigBits >= 4);
36 uint8_t inSigBitShift = 8 - sigBits;
37 uint8_t duplBitShift = sigBits - inSigBitShift;
38 return (original << inSigBitShift) + (original >> duplBitShift);
39}
40
41static void check_4444(skiatest::Reporter* reporter,
42 const SkTDArray<uint16_t>& controlData,
43 const SkTDArray<uint32_t>& readBuffer) {
44 for (int j = 0; j < DEV_H; ++j) {
45 for (int i = 0; i < DEV_W; ++i) {
46 uint16_t control = controlData[i + j * DEV_H];
47 uint32_t test = readBuffer[i + j * DEV_H];
48
49 // Test alpha component
50 uint8_t ctrlComp = expand_value(control & 0xF, 4);
51 uint8_t testComp = GrColorUnpackA(test);
52 check_component(reporter, ctrlComp, testComp);
53
54 // Test blue component
55 ctrlComp = expand_value((control >> 4) & 0xF, 4);
56 testComp = GrColorUnpackB(test);
57 check_component(reporter, ctrlComp, testComp);
58
59 // Test green component
60 ctrlComp = expand_value((control >> 8) & 0xF, 4);
61 testComp = GrColorUnpackG(test);
62 check_component(reporter, ctrlComp, testComp);
63
64 // Test red component
65 ctrlComp = expand_value((control >> 12) & 0xF, 4);
66 testComp = GrColorUnpackR(test);
67 check_component(reporter, ctrlComp, testComp);
68 }
69 }
70}
71
72static void check_565(skiatest::Reporter* reporter,
73 const SkTDArray<uint16_t>& controlData,
74 const SkTDArray<GrColor>& readBuffer) {
75 for (int j = 0; j < DEV_H; ++j) {
76 for (int i = 0; i < DEV_W; ++i) {
77 uint16_t control = controlData[i + j * DEV_H];
78 GrColor test = readBuffer[i + j * DEV_H];
79 // Test blue component (5 bit control)
80 uint8_t ctrlComp = expand_value(control & 0x1F, 5);
81 uint8_t testComp = GrColorUnpackB(test);
82 check_component(reporter, ctrlComp, testComp);
83
84 // Test green component (6 bit control)
85 ctrlComp = expand_value((control >> 5) & 0x3F, 6);
86 testComp = GrColorUnpackG(test);
87 check_component(reporter, ctrlComp, testComp);
88
89 // Test red component (5 bit control)
90 ctrlComp = expand_value((control >> 11) & 0x1F, 5);
91 testComp = GrColorUnpackR(test);
92 check_component(reporter, ctrlComp, testComp);
93 }
94 }
95}
96
97template <typename T>
98void runTest(skiatest::Reporter* reporter, GrContext* context,
99 T val1, T val2, int arraySize, GrPixelConfig config) {
100 SkTDArray<T> controlPixelData;
101 // We will read back into an 8888 buffer since 565/4444 read backes aren't supported
102 SkTDArray<GrColor> readBuffer;
103 controlPixelData.setCount(arraySize);
104 readBuffer.setCount(arraySize);
105
106 for (int i = 0; i < arraySize; i += 2) {
107 controlPixelData[i] = val1;
108 controlPixelData[i + 1] = val2;
109 }
110
111 for (int origin = 0; origin < 2; ++origin) {
112 GrSurfaceDesc desc;
113 desc.fFlags = kNone_GrSurfaceFlags;
114 desc.fWidth = DEV_W;
115 desc.fHeight = DEV_H;
116 desc.fConfig = config;
117 desc.fOrigin = 0 == origin ?
118 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Hal Canary342b7ac2016-11-04 11:49:42 -0400119 sk_sp<GrTexture> fpTexture(context->textureProvider()->createTexture(
egdaniel3fe03272016-08-15 10:59:17 -0700120 desc, SkBudgeted::kNo, controlPixelData.begin(), 0));
121 SkASSERT(fpTexture);
122 fpTexture->readPixels(0, 0, DEV_W, DEV_H, kRGBA_8888_GrPixelConfig, readBuffer.begin(), 0);
123 if (kRGBA_4444_GrPixelConfig == config) {
124 check_4444(reporter, controlPixelData, readBuffer);
125 } else {
126 SkASSERT(kRGB_565_GrPixelConfig == config);
127 check_565(reporter, controlPixelData, readBuffer);
128 }
129 }
130}
131
132static const int CONTROL_ARRAY_SIZE = DEV_W * DEV_H;
133
134DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGBA4444TextureTest, reporter, ctxInfo) {
135 runTest<uint16_t>(reporter, ctxInfo.grContext(), 0xFF00, 0xFA62,
136 CONTROL_ARRAY_SIZE, kRGBA_4444_GrPixelConfig);
137}
138
139DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGB565TextureTest, reporter, ctxInfo) {
140 runTest<uint16_t>(reporter, ctxInfo.grContext(), 0xFF00, 0xFA62,
141 CONTROL_ARRAY_SIZE, kRGB_565_GrPixelConfig);
142}
143
144#endif