blob: 2d6da96d79b9356bfbfb28abecb5b9df9d391228 [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
9#include "Test.h"
10#include "SkGpuDevice.h"
11
12static const int X_SIZE = 12;
13static const int Y_SIZE = 12;
14
15void ReadWriteAlphaTest(skiatest::Reporter* reporter, GrContext* context) {
16
17 unsigned char textureData[X_SIZE][Y_SIZE];
18
19 memset(textureData, 0, X_SIZE * Y_SIZE);
20
21 GrTextureDesc desc;
22
23 // let Skia know we will be using this texture as a render target
24 desc.fFlags = kRenderTarget_GrTextureFlagBit;
25 // it is a single channel texture
26 desc.fConfig = kAlpha_8_GrPixelConfig;
27 desc.fWidth = X_SIZE;
28 desc.fHeight = Y_SIZE;
29 desc.fSampleCnt = 0;
30
31 // We are initializing the texture with zeros here
32 GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
33 if (!texture) {
34 return;
35 }
36
37 GrAutoUnref au(texture);
38
39 // create a distinctive texture
40 for (int y = 0; y < Y_SIZE; ++y) {
41 for (int x = 0; x < X_SIZE; ++x) {
42 textureData[x][y] = x*Y_SIZE+y;
43 }
44 }
45
46 // upload the texture
47 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
48 textureData, 0);
49
50 unsigned char readback[X_SIZE][Y_SIZE];
51
52 // clear readback to something non-zero so we can detect readback failures
53 memset(readback, 0x1, X_SIZE * Y_SIZE);
54
55 // read the texture back
56 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
57 readback, 0);
58
59 // make sure the original & read back versions match
60 bool match = true;
61
62 for (int y = 0; y < Y_SIZE; ++y) {
63 for (int x = 0; x < X_SIZE; ++x) {
64 if (textureData[x][y] != readback[x][y]) {
65 match = false;
66 }
67 }
68 }
69
70 REPORTER_ASSERT(reporter, match);
71
72 // Now try writing on the single channel texture
73 SkCanvas canvas;
74
75 canvas.setDevice(new SkGpuDevice(context, texture->asRenderTarget()))->unref();
76
77 SkPaint paint;
78
79 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
80
81 paint.setColor(SK_ColorWHITE);
82
83 canvas.drawRect(rect, paint);
84
85 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
86 readback, 0);
87
88 match = true;
89
90 for (int y = 0; y < Y_SIZE; ++y) {
91 for (int x = 0; x < X_SIZE; ++x) {
92 if (0xFF != readback[x][y]) {
93 match = false;
94 }
95 }
96 }
97
98 REPORTER_ASSERT(reporter, match);
99}
100
101#include "TestClassDef.h"
102DEFINE_GPUTESTCLASS("ReadWriteAlpha", ReadWriteAlphaTestClass, ReadWriteAlphaTest)
103