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