egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tests/Test.h" |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/gpu/GrContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrProxyProvider.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "tools/gpu/ProxyUtils.h" |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 22 | |
| 23 | static const int DEV_W = 10, DEV_H = 10; |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 24 | static const uint8_t TOL = 0x4; |
| 25 | |
| 26 | static 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 | |
| 36 | static 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 | |
| 43 | static 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 | |
| 74 | static 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 Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 99 | static void run_test(skiatest::Reporter* reporter, GrContext* context, int arraySize, |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 100 | SkColorType colorType) { |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 101 | SkTDArray<uint16_t> controlPixelData; |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 102 | // We will read back into an 8888 buffer since 565/4444 read backs aren't supported |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 103 | SkTDArray<GrColor> readBuffer; |
| 104 | controlPixelData.setCount(arraySize); |
| 105 | readBuffer.setCount(arraySize); |
| 106 | |
| 107 | for (int i = 0; i < arraySize; i += 2) { |
Brian Osman | 91b5a76 | 2018-07-12 09:35:37 -0400 | [diff] [blame] | 108 | controlPixelData[i] = 0xF00F; |
| 109 | controlPixelData[i + 1] = 0xA62F; |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 112 | const SkImageInfo dstInfo = |
| 113 | SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 114 | |
| 115 | for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 116 | auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, GrRenderable::kNo, DEV_W, DEV_H, |
| 117 | colorType, kPremul_SkAlphaType, origin, |
| 118 | controlPixelData.begin(), 0); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 119 | SkASSERT(proxy); |
| 120 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 121 | auto sContext = context->priv().makeWrappedSurfaceContext( |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 122 | std::move(proxy), SkColorTypeToGrColorType(colorType), kPremul_SkAlphaType); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 123 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 124 | if (!sContext->readPixels(dstInfo, readBuffer.begin(), 0, {0, 0})) { |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 125 | // We only require this to succeed if the format is renderable. |
| 126 | REPORTER_ASSERT(reporter, !context->colorTypeSupportedAsSurface(colorType)); |
| 127 | return; |
| 128 | } |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 129 | |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 130 | if (kARGB_4444_SkColorType == colorType) { |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 131 | check_4444(reporter, controlPixelData, readBuffer); |
| 132 | } else { |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 133 | SkASSERT(kRGB_565_SkColorType == colorType); |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 134 | check_565(reporter, controlPixelData, readBuffer); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static const int CONTROL_ARRAY_SIZE = DEV_W * DEV_H; |
| 140 | |
| 141 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGBA4444TextureTest, reporter, ctxInfo) { |
Jim Van Verth | a3407ab | 2019-03-15 15:22:39 -0400 | [diff] [blame] | 142 | if (ctxInfo.grContext()->colorTypeSupportedAsImage(kARGB_4444_SkColorType)) { |
| 143 | run_test(reporter, ctxInfo.grContext(), CONTROL_ARRAY_SIZE, kARGB_4444_SkColorType); |
| 144 | } |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGB565TextureTest, reporter, ctxInfo) { |
Jim Van Verth | a3407ab | 2019-03-15 15:22:39 -0400 | [diff] [blame] | 148 | if (ctxInfo.grContext()->colorTypeSupportedAsImage(kRGB_565_SkColorType)) { |
| 149 | run_test(reporter, ctxInfo.grContext(), CONTROL_ARRAY_SIZE, kRGB_565_SkColorType); |
| 150 | } |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 151 | } |