blob: eea78ce6c06627e51906b976564dae294ea4141a [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
rmistry@google.comd6176b02012-08-23 18:14:13 +000063 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
robertphillips@google.com69950682012-04-06 18:06:10 +000064 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
reed@google.com44a42ea2012-10-01 17:54:05 +000080 SkAutoTUnref<SkDevice> device(new SkGpuDevice(context, texture->asRenderTarget()));
81 SkCanvas canvas(device);
robertphillips@google.com69950682012-04-06 18:06:10 +000082
83 SkPaint paint;
84
85 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
86
87 paint.setColor(SK_ColorWHITE);
88
89 canvas.drawRect(rect, paint);
90
rmistry@google.comd6176b02012-08-23 18:14:13 +000091 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
robertphillips@google.com69950682012-04-06 18:06:10 +000092 readback, 0);
93
94 match = true;
rmistry@google.comd6176b02012-08-23 18:14:13 +000095
robertphillips@google.com69950682012-04-06 18:06:10 +000096 for (int y = 0; y < Y_SIZE; ++y) {
97 for (int x = 0; x < X_SIZE; ++x) {
98 if (0xFF != readback[x][y]) {
99 match = false;
100 }
101 }
102 }
103
104 REPORTER_ASSERT(reporter, match);
105}
106
borenet@google.com5a69ef72012-08-02 16:13:23 +0000107#ifndef SK_BUILD_FOR_ANDROID
robertphillips@google.com69950682012-04-06 18:06:10 +0000108#include "TestClassDef.h"
109DEFINE_GPUTESTCLASS("ReadWriteAlpha", ReadWriteAlphaTestClass, ReadWriteAlphaTest)
110
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000111#endif
borenet@google.com5a69ef72012-08-02 16:13:23 +0000112#endif