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