robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 10 | // This test is specific to the GPU backend. |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 11 | #if SK_SUPPORT_GPU |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 12 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 13 | #include "GrContext.h" |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 14 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 15 | #include "GrProxyProvider.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 16 | #include "GrResourceProvider.h" |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 17 | #include "GrSurfaceContext.h" |
| 18 | #include "GrSurfaceProxy.h" |
Robert Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 19 | #include "GrTextureProxy.h" |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 20 | #include "SkCanvas.h" |
| 21 | #include "SkSurface.h" |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 22 | |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 23 | // This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT properly. |
| 24 | static const int X_SIZE = 13; |
| 25 | static const int Y_SIZE = 13; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 26 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 27 | static void validate_alpha_data(skiatest::Reporter* reporter, int w, int h, const uint8_t* actual, |
| 28 | size_t actualRowBytes, const uint8_t* expected, SkString extraMsg) { |
| 29 | for (int y = 0; y < h; ++y) { |
| 30 | for (int x = 0; x < w; ++x) { |
| 31 | uint8_t a = actual[y * actualRowBytes + x]; |
| 32 | uint8_t e = expected[y * w + x]; |
| 33 | if (e != a) { |
| 34 | ERRORF(reporter, |
| 35 | "Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d), %s", |
| 36 | e, a, x, y, extraMsg.c_str()); |
| 37 | return; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | } |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 42 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 43 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) { |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 44 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 45 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 46 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 47 | unsigned char alphaData[X_SIZE * Y_SIZE]; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 48 | |
robertphillips | df082c5 | 2016-04-19 08:32:40 -0700 | [diff] [blame] | 49 | static const int kClearValue = 0x2; |
| 50 | |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 51 | bool match; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 52 | static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1}; |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 53 | { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 54 | GrSurfaceDesc desc; |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 55 | desc.fFlags = kNone_GrSurfaceFlags; |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 56 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 57 | desc.fConfig = kAlpha_8_GrPixelConfig; // it is a single channel texture |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 58 | desc.fWidth = X_SIZE; |
| 59 | desc.fHeight = Y_SIZE; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 60 | |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 61 | // We are initializing the texture with zeros here |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 62 | memset(alphaData, 0, X_SIZE * Y_SIZE); |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 63 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 64 | sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(desc, SkBudgeted::kNo, |
| 65 | alphaData, 0); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 66 | if (!proxy) { |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 67 | ERRORF(reporter, "Could not create alpha texture."); |
| 68 | return; |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 69 | } |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 70 | sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext( |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 71 | std::move(proxy))); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 72 | |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 73 | const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE); |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 74 | sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii)); |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 75 | |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 76 | // create a distinctive texture |
| 77 | for (int y = 0; y < Y_SIZE; ++y) { |
| 78 | for (int x = 0; x < X_SIZE; ++x) { |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 79 | alphaData[y * X_SIZE + x] = y*X_SIZE+x; |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 82 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 83 | for (auto rowBytes : kRowBytes) { |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 84 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 85 | // upload the texture (do per-rowbytes iteration because we may overwrite below). |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 86 | bool result = sContext->writePixels(ii, alphaData, 0, 0, 0); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 87 | REPORTER_ASSERT(reporter, result, "Initial A8 writePixels failed"); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 88 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 89 | size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE; |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 90 | std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]); |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 91 | // clear readback to something non-zero so we can detect readback failures |
robertphillips | df082c5 | 2016-04-19 08:32:40 -0700 | [diff] [blame] | 92 | memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 93 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 94 | // read the texture back |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 95 | result = sContext->readPixels(ii, readback.get(), rowBytes, 0, 0); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 96 | REPORTER_ASSERT(reporter, result, "Initial A8 readPixels failed"); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 97 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 98 | // make sure the original & read back versions match |
| 99 | SkString msg; |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 100 | msg.printf("rb:%d A8", SkToU32(rowBytes)); |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 101 | validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes, |
| 102 | alphaData, msg); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 103 | |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 104 | // Now try writing to a single channel surface (if we could create one). |
| 105 | if (surf) { |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 106 | SkCanvas* canvas = surf->getCanvas(); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 107 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 108 | SkPaint paint; |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 109 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 110 | const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 111 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 112 | paint.setColor(SK_ColorWHITE); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 113 | |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 114 | canvas->drawRect(rect, paint); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 115 | |
robertphillips | df082c5 | 2016-04-19 08:32:40 -0700 | [diff] [blame] | 116 | memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE); |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 117 | result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 118 | REPORTER_ASSERT(reporter, result, "A8 readPixels after clear failed"); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 119 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 120 | match = true; |
| 121 | for (int y = 0; y < Y_SIZE && match; ++y) { |
| 122 | for (int x = 0; x < X_SIZE && match; ++x) { |
| 123 | uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x]; |
| 124 | if (0xFF != rbValue) { |
| 125 | ERRORF(reporter, |
| 126 | "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x" |
Brian Salomon | 2ea6ff7 | 2016-02-02 11:34:46 -0500 | [diff] [blame] | 127 | " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes)); |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 128 | match = false; |
| 129 | } |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 136 | static const GrPixelConfig kRGBAConfigs[] { |
| 137 | kRGBA_8888_GrPixelConfig, |
| 138 | kBGRA_8888_GrPixelConfig, |
| 139 | kSRGBA_8888_GrPixelConfig |
| 140 | }; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 141 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 142 | for (int y = 0; y < Y_SIZE; ++y) { |
| 143 | for (int x = 0; x < X_SIZE; ++x) { |
| 144 | alphaData[y * X_SIZE + x] = y*X_SIZE+x; |
| 145 | } |
| 146 | } |
| 147 | |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 148 | const SkImageInfo dstInfo = SkImageInfo::Make(X_SIZE, Y_SIZE, |
| 149 | kAlpha_8_SkColorType, |
| 150 | kPremul_SkAlphaType); |
| 151 | |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 152 | // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and |
| 153 | // once with a render target. |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 154 | for (auto config : kRGBAConfigs) { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 155 | for (int rt = 0; rt < 2; ++rt) { |
| 156 | GrSurfaceDesc desc; |
| 157 | desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 158 | desc.fOrigin = rt ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 159 | desc.fConfig = config; |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 160 | desc.fWidth = X_SIZE; |
| 161 | desc.fHeight = Y_SIZE; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 162 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 163 | uint32_t rgbaData[X_SIZE * Y_SIZE]; |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 164 | // Make the alpha channel of the rgba texture come from alphaData. |
| 165 | for (int y = 0; y < Y_SIZE; ++y) { |
| 166 | for (int x = 0; x < X_SIZE; ++x) { |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 167 | rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 170 | |
| 171 | sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(desc, SkBudgeted::kNo, |
| 172 | rgbaData, 0); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 173 | if (!proxy) { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 174 | // We always expect to be able to create a RGBA texture |
| 175 | if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) { |
| 176 | ERRORF(reporter, "Failed to create RGBA texture."); |
| 177 | } |
| 178 | continue; |
| 179 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 180 | |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 181 | sk_sp<SkColorSpace> colorSpace; |
| 182 | if (GrPixelConfigIsSRGB(proxy->config())) { |
| 183 | colorSpace = SkColorSpace::MakeSRGB(); |
| 184 | } |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 185 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext( |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 186 | std::move(proxy), std::move(colorSpace)); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 187 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 188 | for (auto rowBytes : kRowBytes) { |
| 189 | size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 190 | |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 191 | std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]); |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 192 | // Clear so we don't accidentally see values from previous iteration. |
robertphillips | df082c5 | 2016-04-19 08:32:40 -0700 | [diff] [blame] | 193 | memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 194 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 195 | // read the texture back |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 196 | bool result = sContext->readPixels(dstInfo, readback.get(), rowBytes, 0, 0); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 197 | REPORTER_ASSERT(reporter, result, "8888 readPixels failed"); |
bsalomon | 23e5666 | 2016-01-14 07:19:47 -0800 | [diff] [blame] | 198 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 199 | // make sure the original & read back versions match |
| 200 | SkString msg; |
robertphillips | df082c5 | 2016-04-19 08:32:40 -0700 | [diff] [blame] | 201 | msg.printf("rt:%d, rb:%d 8888", rt, SkToU32(rowBytes)); |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 202 | validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes, |
| 203 | alphaData, msg); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 207 | } |
| 208 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 209 | #endif |