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. |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 11 | #if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_ANDROID) |
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 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 20 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, context) { |
| 21 | unsigned char textureData[X_SIZE][Y_SIZE]; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 22 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 23 | memset(textureData, 0, X_SIZE * Y_SIZE); |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 24 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 25 | GrSurfaceDesc desc; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 26 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 27 | // let Skia know we will be using this texture as a render target |
| 28 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 29 | // it is a single channel texture |
| 30 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 31 | desc.fWidth = X_SIZE; |
| 32 | desc.fHeight = Y_SIZE; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 33 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 34 | // We are initializing the texture with zeros here |
| 35 | GrTexture* texture = context->textureProvider()->createTexture(desc, false, textureData, 0); |
| 36 | if (!texture) { |
| 37 | return; |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 38 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 39 | |
| 40 | SkAutoTUnref<GrTexture> au(texture); |
| 41 | |
| 42 | // create a distinctive texture |
| 43 | for (int y = 0; y < Y_SIZE; ++y) { |
| 44 | for (int x = 0; x < X_SIZE; ++x) { |
| 45 | textureData[x][y] = x*Y_SIZE+y; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // upload the texture |
| 50 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 51 | textureData, 0); |
| 52 | |
| 53 | unsigned char readback[X_SIZE][Y_SIZE]; |
| 54 | |
| 55 | // clear readback to something non-zero so we can detect readback failures |
| 56 | memset(readback, 0x1, X_SIZE * Y_SIZE); |
| 57 | |
| 58 | // read the texture back |
| 59 | texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 60 | readback, 0); |
| 61 | |
| 62 | // make sure the original & read back versions match |
| 63 | bool match = true; |
| 64 | |
| 65 | for (int y = 0; y < Y_SIZE; ++y) { |
| 66 | for (int x = 0; x < X_SIZE; ++x) { |
| 67 | if (textureData[x][y] != readback[x][y]) { |
| 68 | match = false; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | REPORTER_ASSERT(reporter, match); |
| 74 | |
| 75 | // Now try writing on the single channel texture |
| 76 | SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |
| 77 | SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarget(), &props, |
| 78 | SkGpuDevice::kUninit_InitContents)); |
| 79 | SkCanvas canvas(device); |
| 80 | |
| 81 | SkPaint paint; |
| 82 | |
| 83 | const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10); |
| 84 | |
| 85 | paint.setColor(SK_ColorWHITE); |
| 86 | |
| 87 | canvas.drawRect(rect, paint); |
| 88 | |
| 89 | texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 90 | readback, 0); |
| 91 | |
| 92 | match = true; |
| 93 | |
| 94 | for (int y = 0; y < Y_SIZE; ++y) { |
| 95 | for (int x = 0; x < X_SIZE; ++x) { |
| 96 | if (0xFF != readback[x][y]) { |
| 97 | match = false; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | REPORTER_ASSERT(reporter, match); |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 103 | } |
| 104 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 105 | #endif |