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 |
| 63 | texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 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 |
| 80 | SkCanvas canvas; |
| 81 | |
| 82 | canvas.setDevice(new SkGpuDevice(context, texture->asRenderTarget()))->unref(); |
| 83 | |
| 84 | SkPaint paint; |
| 85 | |
| 86 | const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10); |
| 87 | |
| 88 | paint.setColor(SK_ColorWHITE); |
| 89 | |
| 90 | canvas.drawRect(rect, paint); |
| 91 | |
| 92 | texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 93 | readback, 0); |
| 94 | |
| 95 | match = true; |
| 96 | |
| 97 | for (int y = 0; y < Y_SIZE; ++y) { |
| 98 | for (int x = 0; x < X_SIZE; ++x) { |
| 99 | if (0xFF != readback[x][y]) { |
| 100 | match = false; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | REPORTER_ASSERT(reporter, match); |
| 106 | } |
| 107 | |
| 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 |