2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 // This test is specific to the GPU backend.
9 #if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_ANDROID)
11 #include "GrContextFactory.h"
12 #include "SkGpuDevice.h"
15 static const int X_SIZE = 12;
16 static const int Y_SIZE = 12;
18 DEF_GPUTEST(ReadWriteAlpha, reporter, factory) {
19 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
20 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
21 if (!GrContextFactory::IsRenderingGLContext(glType)) {
24 GrContext* context = factory->get(glType);
25 if (NULL == context) {
29 unsigned char textureData[X_SIZE][Y_SIZE];
31 memset(textureData, 0, X_SIZE * Y_SIZE);
35 // let Skia know we will be using this texture as a render target
36 desc.fFlags = kRenderTarget_GrTextureFlagBit;
37 // it is a single channel texture
38 desc.fConfig = kAlpha_8_GrPixelConfig;
40 desc.fHeight = Y_SIZE;
42 // We are initializing the texture with zeros here
43 GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
48 SkAutoTUnref<GrTexture> au(texture);
50 // create a distinctive texture
51 for (int y = 0; y < Y_SIZE; ++y) {
52 for (int x = 0; x < X_SIZE; ++x) {
53 textureData[x][y] = x*Y_SIZE+y;
58 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
61 unsigned char readback[X_SIZE][Y_SIZE];
63 // clear readback to something non-zero so we can detect readback failures
64 memset(readback, 0x1, X_SIZE * Y_SIZE);
66 // read the texture back
67 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
70 // make sure the original & read back versions match
73 for (int y = 0; y < Y_SIZE; ++y) {
74 for (int x = 0; x < X_SIZE; ++x) {
75 if (textureData[x][y] != readback[x][y]) {
81 REPORTER_ASSERT(reporter, match);
83 // Now try writing on the single channel texture
84 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarget(),
85 SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)));
86 SkCanvas canvas(device);
90 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
92 paint.setColor(SK_ColorWHITE);
94 canvas.drawRect(rect, paint);
96 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
101 for (int y = 0; y < Y_SIZE; ++y) {
102 for (int x = 0; x < X_SIZE; ++x) {
103 if (0xFF != readback[x][y]) {
109 REPORTER_ASSERT(reporter, match);