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