robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 9 | // This test is specific to the GPU backend. |
| 10 | #if SK_SUPPORT_GPU |
| 11 | |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 12 | #include "Test.h" |
| 13 | #include "SkGpuDevice.h" |
| 14 | |
| 15 | static const int X_SIZE = 12; |
| 16 | static const int Y_SIZE = 12; |
| 17 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 18 | static void ReadWriteAlphaTest(skiatest::Reporter* reporter, GrContext* context) { |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 19 | |
robertphillips@google.com | 38c3a30 | 2012-04-06 18:25:24 +0000 | [diff] [blame] | 20 | #if SK_SCALAR_IS_FIXED |
| 21 | // GPU device known not to work in the fixed pt build. |
| 22 | return; |
| 23 | #endif |
| 24 | |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 25 | unsigned char textureData[X_SIZE][Y_SIZE]; |
| 26 | |
| 27 | memset(textureData, 0, X_SIZE * Y_SIZE); |
| 28 | |
| 29 | GrTextureDesc desc; |
| 30 | |
| 31 | // let Skia know we will be using this texture as a render target |
| 32 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 33 | // it is a single channel texture |
| 34 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 35 | desc.fWidth = X_SIZE; |
| 36 | desc.fHeight = Y_SIZE; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 37 | |
| 38 | // We are initializing the texture with zeros here |
| 39 | GrTexture* texture = context->createUncachedTexture(desc, textureData, 0); |
| 40 | if (!texture) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | GrAutoUnref au(texture); |
| 45 | |
| 46 | // create a distinctive texture |
| 47 | for (int y = 0; y < Y_SIZE; ++y) { |
| 48 | for (int x = 0; x < X_SIZE; ++x) { |
| 49 | textureData[x][y] = x*Y_SIZE+y; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // upload the texture |
| 54 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 55 | textureData, 0); |
| 56 | |
| 57 | unsigned char readback[X_SIZE][Y_SIZE]; |
| 58 | |
| 59 | // clear readback to something non-zero so we can detect readback failures |
| 60 | memset(readback, 0x1, X_SIZE * Y_SIZE); |
| 61 | |
| 62 | // read the texture back |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 63 | texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 64 | readback, 0); |
| 65 | |
| 66 | // make sure the original & read back versions match |
| 67 | bool match = true; |
| 68 | |
| 69 | for (int y = 0; y < Y_SIZE; ++y) { |
| 70 | for (int x = 0; x < X_SIZE; ++x) { |
| 71 | if (textureData[x][y] != readback[x][y]) { |
| 72 | match = false; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | REPORTER_ASSERT(reporter, match); |
| 78 | |
| 79 | // Now try writing on the single channel texture |
reed@google.com | 44a42ea | 2012-10-01 17:54:05 +0000 | [diff] [blame] | 80 | SkAutoTUnref<SkDevice> device(new SkGpuDevice(context, texture->asRenderTarget())); |
| 81 | SkCanvas canvas(device); |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 82 | |
| 83 | SkPaint paint; |
| 84 | |
| 85 | const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10); |
| 86 | |
| 87 | paint.setColor(SK_ColorWHITE); |
| 88 | |
| 89 | canvas.drawRect(rect, paint); |
| 90 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 91 | texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 92 | readback, 0); |
| 93 | |
| 94 | match = true; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 95 | |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 96 | for (int y = 0; y < Y_SIZE; ++y) { |
| 97 | for (int x = 0; x < X_SIZE; ++x) { |
| 98 | if (0xFF != readback[x][y]) { |
| 99 | match = false; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | REPORTER_ASSERT(reporter, match); |
| 105 | } |
| 106 | |
borenet@google.com | 5a69ef7 | 2012-08-02 16:13:23 +0000 | [diff] [blame] | 107 | #ifndef SK_BUILD_FOR_ANDROID |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 108 | #include "TestClassDef.h" |
| 109 | DEFINE_GPUTESTCLASS("ReadWriteAlpha", ReadWriteAlphaTestClass, ReadWriteAlphaTest) |
| 110 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 111 | #endif |
borenet@google.com | 5a69ef7 | 2012-08-02 16:13:23 +0000 | [diff] [blame] | 112 | #endif |