blob: b1fbc779773ed58f544d4f2c77e403aafa75a9da [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"
Robert Phillipse78b7252017-04-06 07:59:41 -040018#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050019#include "GrProxyProvider.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040020#include "GrTextureProxy.h"
Brian Salomon58389b92018-03-07 13:01:25 -050021#include "ProxyUtils.h"
egdaniel3fe03272016-08-15 10:59:17 -070022
23static const int DEV_W = 10, DEV_H = 10;
egdaniel3fe03272016-08-15 10:59:17 -070024static const uint8_t TOL = 0x4;
25
26static void check_component(skiatest::Reporter* reporter, uint8_t control, uint8_t test) {
27 uint8_t diff = 0;
28 if (control >= test) {
29 diff = control - test;
30 } else {
31 diff = test - control;
32 }
33 REPORTER_ASSERT(reporter, diff < TOL);
34}
35
36static uint8_t expand_value(uint8_t original, int sigBits) {
37 SkASSERT(sigBits >= 4);
38 uint8_t inSigBitShift = 8 - sigBits;
39 uint8_t duplBitShift = sigBits - inSigBitShift;
40 return (original << inSigBitShift) + (original >> duplBitShift);
41}
42
43static void check_4444(skiatest::Reporter* reporter,
44 const SkTDArray<uint16_t>& controlData,
45 const SkTDArray<uint32_t>& readBuffer) {
46 for (int j = 0; j < DEV_H; ++j) {
47 for (int i = 0; i < DEV_W; ++i) {
48 uint16_t control = controlData[i + j * DEV_H];
49 uint32_t test = readBuffer[i + j * DEV_H];
50
51 // Test alpha component
52 uint8_t ctrlComp = expand_value(control & 0xF, 4);
53 uint8_t testComp = GrColorUnpackA(test);
54 check_component(reporter, ctrlComp, testComp);
55
56 // Test blue component
57 ctrlComp = expand_value((control >> 4) & 0xF, 4);
58 testComp = GrColorUnpackB(test);
59 check_component(reporter, ctrlComp, testComp);
60
61 // Test green component
62 ctrlComp = expand_value((control >> 8) & 0xF, 4);
63 testComp = GrColorUnpackG(test);
64 check_component(reporter, ctrlComp, testComp);
65
66 // Test red component
67 ctrlComp = expand_value((control >> 12) & 0xF, 4);
68 testComp = GrColorUnpackR(test);
69 check_component(reporter, ctrlComp, testComp);
70 }
71 }
72}
73
74static void check_565(skiatest::Reporter* reporter,
75 const SkTDArray<uint16_t>& controlData,
76 const SkTDArray<GrColor>& readBuffer) {
77 for (int j = 0; j < DEV_H; ++j) {
78 for (int i = 0; i < DEV_W; ++i) {
79 uint16_t control = controlData[i + j * DEV_H];
80 GrColor test = readBuffer[i + j * DEV_H];
81 // Test blue component (5 bit control)
82 uint8_t ctrlComp = expand_value(control & 0x1F, 5);
83 uint8_t testComp = GrColorUnpackB(test);
84 check_component(reporter, ctrlComp, testComp);
85
86 // Test green component (6 bit control)
87 ctrlComp = expand_value((control >> 5) & 0x3F, 6);
88 testComp = GrColorUnpackG(test);
89 check_component(reporter, ctrlComp, testComp);
90
91 // Test red component (5 bit control)
92 ctrlComp = expand_value((control >> 11) & 0x1F, 5);
93 testComp = GrColorUnpackR(test);
94 check_component(reporter, ctrlComp, testComp);
95 }
96 }
97}
98
Brian Salomon58389b92018-03-07 13:01:25 -050099static void run_test(skiatest::Reporter* reporter, GrContext* context, int arraySize,
100 GrColorType colorType) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400101 SkTDArray<uint16_t> controlPixelData;
Robert Phillips301431d2017-03-29 12:08:49 -0400102 // We will read back into an 8888 buffer since 565/4444 read backs aren't supported
egdaniel3fe03272016-08-15 10:59:17 -0700103 SkTDArray<GrColor> readBuffer;
104 controlPixelData.setCount(arraySize);
105 readBuffer.setCount(arraySize);
106
107 for (int i = 0; i < arraySize; i += 2) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400108 controlPixelData[i] = 0xFF00;
109 controlPixelData[i + 1] = 0xFA62;
egdaniel3fe03272016-08-15 10:59:17 -0700110 }
111
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400112 const SkImageInfo dstInfo = SkImageInfo::Make(DEV_W, DEV_H,
113 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
114
115 for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
Brian Salomon58389b92018-03-07 13:01:25 -0500116 auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, false, DEV_W, DEV_H, colorType,
117 origin, controlPixelData.begin(), 0);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400118 SkASSERT(proxy);
119
120 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500121 std::move(proxy));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400122
123 SkAssertResult(sContext->readPixels(dstInfo, readBuffer.begin(), 0, 0, 0));
124
Brian Salomon58389b92018-03-07 13:01:25 -0500125 if (GrColorType::kABGR_4444 == colorType) {
egdaniel3fe03272016-08-15 10:59:17 -0700126 check_4444(reporter, controlPixelData, readBuffer);
127 } else {
Brian Salomon58389b92018-03-07 13:01:25 -0500128 SkASSERT(GrColorType::kRGB_565 == colorType);
egdaniel3fe03272016-08-15 10:59:17 -0700129 check_565(reporter, controlPixelData, readBuffer);
130 }
131 }
132}
133
134static const int CONTROL_ARRAY_SIZE = DEV_W * DEV_H;
135
136DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGBA4444TextureTest, reporter, ctxInfo) {
Brian Salomon58389b92018-03-07 13:01:25 -0500137 run_test(reporter, ctxInfo.grContext(), CONTROL_ARRAY_SIZE, GrColorType::kABGR_4444);
egdaniel3fe03272016-08-15 10:59:17 -0700138}
139
140DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGB565TextureTest, reporter, ctxInfo) {
Brian Salomon58389b92018-03-07 13:01:25 -0500141 run_test(reporter, ctxInfo.grContext(), CONTROL_ARRAY_SIZE, GrColorType::kRGB_565);
egdaniel3fe03272016-08-15 10:59:17 -0700142}
143
144#endif