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 | |
| 14 | #include "Test.h" |
| 15 | |
| 16 | #if SK_SUPPORT_GPU |
| 17 | #include "GrContext.h" |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 18 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 19 | #include "GrProxyProvider.h" |
Robert Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 20 | #include "GrTextureProxy.h" |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 21 | |
| 22 | static const int DEV_W = 10, DEV_H = 10; |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 23 | static const uint8_t TOL = 0x4; |
| 24 | |
| 25 | static 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 | |
| 35 | static 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 | |
| 42 | static 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 | |
| 73 | static 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 | |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 98 | static void run_test(skiatest::Reporter* reporter, GrContext* context, |
| 99 | int arraySize, GrPixelConfig config) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 100 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
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) { |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 108 | controlPixelData[i] = 0xFF00; |
| 109 | controlPixelData[i + 1] = 0xFA62; |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 112 | const SkImageInfo dstInfo = SkImageInfo::Make(DEV_W, DEV_H, |
| 113 | kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 114 | |
| 115 | for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 116 | GrSurfaceDesc desc; |
| 117 | desc.fFlags = kNone_GrSurfaceFlags; |
| 118 | desc.fWidth = DEV_W; |
| 119 | desc.fHeight = DEV_H; |
| 120 | desc.fConfig = config; |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 121 | desc.fOrigin = origin; |
| 122 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 123 | sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy( |
| 124 | desc, SkBudgeted::kNo, controlPixelData.begin(), 0); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 125 | SkASSERT(proxy); |
| 126 | |
| 127 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext( |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 128 | std::move(proxy)); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 129 | |
| 130 | SkAssertResult(sContext->readPixels(dstInfo, readBuffer.begin(), 0, 0, 0)); |
| 131 | |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 132 | if (kRGBA_4444_GrPixelConfig == config) { |
| 133 | check_4444(reporter, controlPixelData, readBuffer); |
| 134 | } else { |
| 135 | SkASSERT(kRGB_565_GrPixelConfig == config); |
| 136 | check_565(reporter, controlPixelData, readBuffer); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static const int CONTROL_ARRAY_SIZE = DEV_W * DEV_H; |
| 142 | |
| 143 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGBA4444TextureTest, reporter, ctxInfo) { |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 144 | run_test(reporter, ctxInfo.grContext(), CONTROL_ARRAY_SIZE, kRGBA_4444_GrPixelConfig); |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGB565TextureTest, reporter, ctxInfo) { |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 148 | run_test(reporter, ctxInfo.grContext(), CONTROL_ARRAY_SIZE, kRGB_565_GrPixelConfig); |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | #endif |