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