blob: 5df1ab395fe89c69b88c14435ad112e808e43022 [file] [log] [blame]
robertphillips@google.com69950682012-04-06 18:06:10 +00001/*
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.comcf8fb1f2012-08-02 14:03:32 +00008// This test is specific to the GPU backend.
djsollen@google.comefbe8e92013-02-07 18:58:35 +00009#if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_ANDROID)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010
bsalomon@google.com67b915d2013-02-04 16:13:32 +000011#include "GrContextFactory.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012#include "SkGpuDevice.h"
13#include "Test.h"
robertphillips@google.com69950682012-04-06 18:06:10 +000014
15static const int X_SIZE = 12;
16static const int Y_SIZE = 12;
17
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000018DEF_GPUTEST(ReadWriteAlpha, reporter, factory) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000019 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.com69950682012-04-06 18:06:10 +000023 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +000024 GrContext* context = factory->get(glType);
25 if (NULL == context) {
26 continue;
27 }
robertphillips@google.com69950682012-04-06 18:06:10 +000028
bsalomon@google.com67b915d2013-02-04 16:13:32 +000029 unsigned char textureData[X_SIZE][Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000030
bsalomon@google.com67b915d2013-02-04 16:13:32 +000031 memset(textureData, 0, X_SIZE * Y_SIZE);
robertphillips@google.com69950682012-04-06 18:06:10 +000032
bsalomon@google.com67b915d2013-02-04 16:13:32 +000033 GrTextureDesc desc;
robertphillips@google.com69950682012-04-06 18:06:10 +000034
bsalomon@google.com67b915d2013-02-04 16:13:32 +000035 // 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.com69950682012-04-06 18:06:10 +000041
bsalomon@google.com67b915d2013-02-04 16:13:32 +000042 // We are initializing the texture with zeros here
43 GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
44 if (!texture) {
45 return;
46 }
robertphillips@google.com69950682012-04-06 18:06:10 +000047
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000048 SkAutoUnref au(texture);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000049
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.com69950682012-04-06 18:06:10 +000054 }
55 }
robertphillips@google.com69950682012-04-06 18:06:10 +000056
bsalomon@google.com67b915d2013-02-04 16:13:32 +000057 // upload the texture
58 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
59 textureData, 0);
robertphillips@google.com69950682012-04-06 18:06:10 +000060
bsalomon@google.com67b915d2013-02-04 16:13:32 +000061 unsigned char readback[X_SIZE][Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000062
bsalomon@google.com67b915d2013-02-04 16:13:32 +000063 // clear readback to something non-zero so we can detect readback failures
64 memset(readback, 0x1, X_SIZE * Y_SIZE);
robertphillips@google.com69950682012-04-06 18:06:10 +000065
bsalomon@google.com67b915d2013-02-04 16:13:32 +000066 // read the texture back
67 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
68 readback, 0);
robertphillips@google.com69950682012-04-06 18:06:10 +000069
bsalomon@google.com67b915d2013-02-04 16:13:32 +000070 // make sure the original & read back versions match
71 bool match = true;
robertphillips@google.com69950682012-04-06 18:06:10 +000072
bsalomon@google.com67b915d2013-02-04 16:13:32 +000073 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.com69950682012-04-06 18:06:10 +000078 }
79 }
robertphillips@google.com69950682012-04-06 18:06:10 +000080
bsalomon@google.com67b915d2013-02-04 16:13:32 +000081 REPORTER_ASSERT(reporter, match);
82
83 // Now try writing on the single channel texture
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000084 SkAutoTUnref<SkBaseDevice> device(new SkGpuDevice(context, texture->asRenderTarget()));
bsalomon@google.com67b915d2013-02-04 16:13:32 +000085 SkCanvas canvas(device);
86
87 SkPaint paint;
88
89 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
90
91 paint.setColor(SK_ColorWHITE);
92
93 canvas.drawRect(rect, paint);
94
95 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
96 readback, 0);
97
98 match = true;
99
100 for (int y = 0; y < Y_SIZE; ++y) {
101 for (int x = 0; x < X_SIZE; ++x) {
102 if (0xFF != readback[x][y]) {
103 match = false;
104 }
105 }
106 }
107
108 REPORTER_ASSERT(reporter, match);
109 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000110}
111
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000112#endif