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 | |
| 9 | #include "Test.h" |
| 10 | #include "SkGpuDevice.h" |
| 11 | |
| 12 | static const int X_SIZE = 12; |
| 13 | static const int Y_SIZE = 12; |
| 14 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 15 | static void ReadWriteAlphaTest(skiatest::Reporter* reporter, GrContext* context) { |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 16 | |
robertphillips@google.com | 38c3a30 | 2012-04-06 18:25:24 +0000 | [diff] [blame] | 17 | #if SK_SCALAR_IS_FIXED |
| 18 | // GPU device known not to work in the fixed pt build. |
| 19 | return; |
| 20 | #endif |
| 21 | |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 22 | unsigned char textureData[X_SIZE][Y_SIZE]; |
| 23 | |
| 24 | memset(textureData, 0, X_SIZE * Y_SIZE); |
| 25 | |
| 26 | GrTextureDesc desc; |
| 27 | |
| 28 | // let Skia know we will be using this texture as a render target |
| 29 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 30 | // it is a single channel texture |
| 31 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 32 | desc.fWidth = X_SIZE; |
| 33 | desc.fHeight = Y_SIZE; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 34 | |
| 35 | // We are initializing the texture with zeros here |
| 36 | GrTexture* texture = context->createUncachedTexture(desc, textureData, 0); |
| 37 | if (!texture) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | GrAutoUnref au(texture); |
| 42 | |
| 43 | // create a distinctive texture |
| 44 | for (int y = 0; y < Y_SIZE; ++y) { |
| 45 | for (int x = 0; x < X_SIZE; ++x) { |
| 46 | textureData[x][y] = x*Y_SIZE+y; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // upload the texture |
| 51 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 52 | textureData, 0); |
| 53 | |
| 54 | unsigned char readback[X_SIZE][Y_SIZE]; |
| 55 | |
| 56 | // clear readback to something non-zero so we can detect readback failures |
| 57 | memset(readback, 0x1, X_SIZE * Y_SIZE); |
| 58 | |
| 59 | // read the texture back |
| 60 | texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 61 | readback, 0); |
| 62 | |
| 63 | // make sure the original & read back versions match |
| 64 | bool match = true; |
| 65 | |
| 66 | for (int y = 0; y < Y_SIZE; ++y) { |
| 67 | for (int x = 0; x < X_SIZE; ++x) { |
| 68 | if (textureData[x][y] != readback[x][y]) { |
| 69 | match = false; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | REPORTER_ASSERT(reporter, match); |
| 75 | |
| 76 | // Now try writing on the single channel texture |
| 77 | SkCanvas canvas; |
| 78 | |
| 79 | canvas.setDevice(new SkGpuDevice(context, texture->asRenderTarget()))->unref(); |
| 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); |
| 103 | } |
| 104 | |
| 105 | #include "TestClassDef.h" |
| 106 | DEFINE_GPUTESTCLASS("ReadWriteAlpha", ReadWriteAlphaTestClass, ReadWriteAlphaTest) |
| 107 | |