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