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 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 8 | // This test is specific to the GPU backend. |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 9 | #if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_ANDROID) |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 11 | #include "GrContextFactory.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 12 | #include "SkGpuDevice.h" |
| 13 | #include "Test.h" |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 14 | |
| 15 | static const int X_SIZE = 12; |
| 16 | static const int Y_SIZE = 12; |
| 17 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 18 | DEF_GPUTEST(ReadWriteAlpha, reporter, factory) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 19 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 20 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 21 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 22 | continue; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 23 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 24 | GrContext* context = factory->get(glType); |
| 25 | if (NULL == context) { |
| 26 | continue; |
| 27 | } |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 28 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 29 | unsigned char textureData[X_SIZE][Y_SIZE]; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 31 | memset(textureData, 0, X_SIZE * Y_SIZE); |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 32 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 33 | GrTextureDesc desc; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 35 | // let Skia know we will be using this texture as a render target |
| 36 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 37 | // it is a single channel texture |
| 38 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 39 | desc.fWidth = X_SIZE; |
| 40 | desc.fHeight = Y_SIZE; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 41 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 42 | // We are initializing the texture with zeros here |
| 43 | GrTexture* texture = context->createUncachedTexture(desc, textureData, 0); |
| 44 | if (!texture) { |
| 45 | return; |
| 46 | } |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 47 | |
bsalomon | dcabb05 | 2014-07-21 14:24:01 -0700 | [diff] [blame] | 48 | SkAutoTUnref<GrTexture> au(texture); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 49 | |
| 50 | // create a distinctive texture |
| 51 | for (int y = 0; y < Y_SIZE; ++y) { |
| 52 | for (int x = 0; x < X_SIZE; ++x) { |
| 53 | textureData[x][y] = x*Y_SIZE+y; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 57 | // upload the texture |
| 58 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 59 | textureData, 0); |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 60 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 61 | unsigned char readback[X_SIZE][Y_SIZE]; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 62 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 63 | // clear readback to something non-zero so we can detect readback failures |
| 64 | memset(readback, 0x1, X_SIZE * Y_SIZE); |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 65 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 66 | // read the texture back |
| 67 | texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 68 | readback, 0); |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 69 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 70 | // make sure the original & read back versions match |
| 71 | bool match = true; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 72 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 73 | for (int y = 0; y < Y_SIZE; ++y) { |
| 74 | for (int x = 0; x < X_SIZE; ++x) { |
| 75 | if (textureData[x][y] != readback[x][y]) { |
| 76 | match = false; |
| 77 | } |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 80 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 81 | REPORTER_ASSERT(reporter, match); |
| 82 | |
| 83 | // Now try writing on the single channel texture |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 84 | SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarget(), |
| 85 | SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType))); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 86 | SkCanvas canvas(device); |
| 87 | |
| 88 | SkPaint paint; |
| 89 | |
| 90 | const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10); |
| 91 | |
| 92 | paint.setColor(SK_ColorWHITE); |
| 93 | |
| 94 | canvas.drawRect(rect, paint); |
| 95 | |
| 96 | texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 97 | readback, 0); |
| 98 | |
| 99 | match = true; |
| 100 | |
| 101 | for (int y = 0; y < Y_SIZE; ++y) { |
| 102 | for (int x = 0; x < X_SIZE; ++x) { |
| 103 | if (0xFF != readback[x][y]) { |
| 104 | match = false; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | REPORTER_ASSERT(reporter, match); |
| 110 | } |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 111 | } |
| 112 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 113 | #endif |