blob: faa7629e7b86779f99352d6b625813abc1cffa36 [file] [log] [blame]
robertphillips@google.com69950682012-04-06 18:06:10 +00001
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.comcf8fb1f2012-08-02 14:03:32 +00009// This test is specific to the GPU backend.
10#if SK_SUPPORT_GPU
11
robertphillips@google.com69950682012-04-06 18:06:10 +000012#include "Test.h"
13#include "SkGpuDevice.h"
14
15static const int X_SIZE = 12;
16static const int Y_SIZE = 12;
17
caryclark@google.com42639cd2012-06-06 12:03:39 +000018static void ReadWriteAlphaTest(skiatest::Reporter* reporter, GrContext* context) {
robertphillips@google.com69950682012-04-06 18:06:10 +000019
robertphillips@google.com38c3a302012-04-06 18:25:24 +000020#if SK_SCALAR_IS_FIXED
21 // GPU device known not to work in the fixed pt build.
22 return;
23#endif
24
robertphillips@google.com69950682012-04-06 18:06:10 +000025 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.com69950682012-04-06 18:06:10 +000037
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
borenet@google.com5a69ef72012-08-02 16:13:23 +0000108#ifndef SK_BUILD_FOR_ANDROID
robertphillips@google.com69950682012-04-06 18:06:10 +0000109#include "TestClassDef.h"
110DEFINE_GPUTESTCLASS("ReadWriteAlpha", ReadWriteAlphaTestClass, ReadWriteAlphaTest)
111
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000112#endif
borenet@google.com5a69ef72012-08-02 16:13:23 +0000113#endif