blob: 4e8a49e52e1feaa69037ee9151194429fcedc8a9 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tests/Test.h"
egdaniel3fe03272016-08-15 10:59:17 -070015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/gpu/GrContext.h"
17#include "include/private/GrTextureProxy.h"
18#include "src/gpu/GrContextPriv.h"
19#include "src/gpu/GrProxyProvider.h"
20#include "tools/gpu/ProxyUtils.h"
egdaniel3fe03272016-08-15 10:59:17 -070021
22static const int DEV_W = 10, DEV_H = 10;
egdaniel3fe03272016-08-15 10:59:17 -070023static const uint8_t TOL = 0x4;
24
25static void check_component(skiatest::Reporter* reporter, uint8_t control, uint8_t test) {
26 uint8_t diff = 0;
27 if (control >= test) {
28 diff = control - test;
29 } else {
30 diff = test - control;
31 }
32 REPORTER_ASSERT(reporter, diff < TOL);
33}
34
35static uint8_t expand_value(uint8_t original, int sigBits) {
36 SkASSERT(sigBits >= 4);
37 uint8_t inSigBitShift = 8 - sigBits;
38 uint8_t duplBitShift = sigBits - inSigBitShift;
39 return (original << inSigBitShift) + (original >> duplBitShift);
40}
41
42static void check_4444(skiatest::Reporter* reporter,
43 const SkTDArray<uint16_t>& controlData,
44 const SkTDArray<uint32_t>& readBuffer) {
45 for (int j = 0; j < DEV_H; ++j) {
46 for (int i = 0; i < DEV_W; ++i) {
47 uint16_t control = controlData[i + j * DEV_H];
48 uint32_t test = readBuffer[i + j * DEV_H];
49
50 // Test alpha component
51 uint8_t ctrlComp = expand_value(control & 0xF, 4);
52 uint8_t testComp = GrColorUnpackA(test);
53 check_component(reporter, ctrlComp, testComp);
54
55 // Test blue component
56 ctrlComp = expand_value((control >> 4) & 0xF, 4);
57 testComp = GrColorUnpackB(test);
58 check_component(reporter, ctrlComp, testComp);
59
60 // Test green component
61 ctrlComp = expand_value((control >> 8) & 0xF, 4);
62 testComp = GrColorUnpackG(test);
63 check_component(reporter, ctrlComp, testComp);
64
65 // Test red component
66 ctrlComp = expand_value((control >> 12) & 0xF, 4);
67 testComp = GrColorUnpackR(test);
68 check_component(reporter, ctrlComp, testComp);
69 }
70 }
71}
72
73static void check_565(skiatest::Reporter* reporter,
74 const SkTDArray<uint16_t>& controlData,
75 const SkTDArray<GrColor>& readBuffer) {
76 for (int j = 0; j < DEV_H; ++j) {
77 for (int i = 0; i < DEV_W; ++i) {
78 uint16_t control = controlData[i + j * DEV_H];
79 GrColor test = readBuffer[i + j * DEV_H];
80 // Test blue component (5 bit control)
81 uint8_t ctrlComp = expand_value(control & 0x1F, 5);
82 uint8_t testComp = GrColorUnpackB(test);
83 check_component(reporter, ctrlComp, testComp);
84
85 // Test green component (6 bit control)
86 ctrlComp = expand_value((control >> 5) & 0x3F, 6);
87 testComp = GrColorUnpackG(test);
88 check_component(reporter, ctrlComp, testComp);
89
90 // Test red component (5 bit control)
91 ctrlComp = expand_value((control >> 11) & 0x1F, 5);
92 testComp = GrColorUnpackR(test);
93 check_component(reporter, ctrlComp, testComp);
94 }
95 }
96}
97
Brian Salomon58389b92018-03-07 13:01:25 -050098static void run_test(skiatest::Reporter* reporter, GrContext* context, int arraySize,
Brian Salomon19eaf2d2018-03-19 16:06:44 -040099 SkColorType colorType) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400100 SkTDArray<uint16_t> controlPixelData;
Robert Phillips301431d2017-03-29 12:08:49 -0400101 // We will read back into an 8888 buffer since 565/4444 read backs aren't supported
egdaniel3fe03272016-08-15 10:59:17 -0700102 SkTDArray<GrColor> readBuffer;
103 controlPixelData.setCount(arraySize);
104 readBuffer.setCount(arraySize);
105
106 for (int i = 0; i < arraySize; i += 2) {
Brian Osman91b5a762018-07-12 09:35:37 -0400107 controlPixelData[i] = 0xF00F;
108 controlPixelData[i + 1] = 0xA62F;
egdaniel3fe03272016-08-15 10:59:17 -0700109 }
110
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400111 const SkImageInfo dstInfo =
112 SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400113
114 for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400115 auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, GrRenderable::kNo,
116 DEV_W, DEV_H, colorType,
Brian Salomon58389b92018-03-07 13:01:25 -0500117 origin, controlPixelData.begin(), 0);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400118 SkASSERT(proxy);
119
Robert Phillips9da87e02019-02-04 13:26:26 -0500120 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500121 std::move(proxy));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400122
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400123 if (!sContext->readPixels(dstInfo, readBuffer.begin(), 0, 0, 0)) {
124 // We only require this to succeed if the format is renderable.
125 REPORTER_ASSERT(reporter, !context->colorTypeSupportedAsSurface(colorType));
126 return;
127 }
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400128
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400129 if (kARGB_4444_SkColorType == colorType) {
egdaniel3fe03272016-08-15 10:59:17 -0700130 check_4444(reporter, controlPixelData, readBuffer);
131 } else {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400132 SkASSERT(kRGB_565_SkColorType == colorType);
egdaniel3fe03272016-08-15 10:59:17 -0700133 check_565(reporter, controlPixelData, readBuffer);
134 }
135 }
136}
137
138static const int CONTROL_ARRAY_SIZE = DEV_W * DEV_H;
139
140DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGBA4444TextureTest, reporter, ctxInfo) {
Jim Van Vertha3407ab2019-03-15 15:22:39 -0400141 if (ctxInfo.grContext()->colorTypeSupportedAsImage(kARGB_4444_SkColorType)) {
142 run_test(reporter, ctxInfo.grContext(), CONTROL_ARRAY_SIZE, kARGB_4444_SkColorType);
143 }
egdaniel3fe03272016-08-15 10:59:17 -0700144}
145
146DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGB565TextureTest, reporter, ctxInfo) {
Jim Van Vertha3407ab2019-03-15 15:22:39 -0400147 if (ctxInfo.grContext()->colorTypeSupportedAsImage(kRGB_565_SkColorType)) {
148 run_test(reporter, ctxInfo.grContext(), CONTROL_ARRAY_SIZE, kRGB_565_SkColorType);
149 }
egdaniel3fe03272016-08-15 10:59:17 -0700150}