blob: fb818b355aa95fecc04e8131486ac4de21775136 [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;
robertphillips@google.com69950682012-04-06 18:06:10 +000034
35 // We are initializing the texture with zeros here
36 GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
37 if (!texture) {
38 return;
39 }
40
41 GrAutoUnref au(texture);
42
43 // create a distinctive texture
44 for (int y = 0; y < Y_SIZE; ++y) {
45 for (int x = 0; x < X_SIZE; ++x) {
46 textureData[x][y] = x*Y_SIZE+y;
47 }
48 }
49
50 // upload the texture
51 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
52 textureData, 0);
53
54 unsigned char readback[X_SIZE][Y_SIZE];
55
56 // clear readback to something non-zero so we can detect readback failures
57 memset(readback, 0x1, X_SIZE * Y_SIZE);
58
59 // read the texture back
60 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
61 readback, 0);
62
63 // make sure the original & read back versions match
64 bool match = true;
65
66 for (int y = 0; y < Y_SIZE; ++y) {
67 for (int x = 0; x < X_SIZE; ++x) {
68 if (textureData[x][y] != readback[x][y]) {
69 match = false;
70 }
71 }
72 }
73
74 REPORTER_ASSERT(reporter, match);
75
76 // Now try writing on the single channel texture
77 SkCanvas canvas;
78
79 canvas.setDevice(new SkGpuDevice(context, texture->asRenderTarget()))->unref();
80
81 SkPaint paint;
82
83 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
84
85 paint.setColor(SK_ColorWHITE);
86
87 canvas.drawRect(rect, paint);
88
89 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
90 readback, 0);
91
92 match = true;
93
94 for (int y = 0; y < Y_SIZE; ++y) {
95 for (int x = 0; x < X_SIZE; ++x) {
96 if (0xFF != readback[x][y]) {
97 match = false;
98 }
99 }
100 }
101
102 REPORTER_ASSERT(reporter, match);
103}
104
105#include "TestClassDef.h"
106DEFINE_GPUTESTCLASS("ReadWriteAlpha", ReadWriteAlphaTestClass, ReadWriteAlphaTest)
107