ReadWriteAlphaTest.cpp revision 23e566664b85472766c921cd2f5615c846919934
16995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com/*
26995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com * Copyright 2012 Google Inc.
36995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com *
46995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com * Use of this source code is governed by a BSD-style license that can be
56995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com * found in the LICENSE file.
66995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com */
76995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
8f037e0bf138a4e842f39e19864d05010a54950c9reed#include "Test.h"
9f037e0bf138a4e842f39e19864d05010a54950c9reed
10cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com// This test is specific to the GPU backend.
1123e566664b85472766c921cd2f5615c846919934bsalomon#if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_ANDROID)
12cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com
131530283c483cb88aa725bce50a6d193dd00ee570kkinnunen#include "GrContext.h"
144ee16bfaedb14aff8cf102f1f0722ff2529a9699tfarina@chromium.org#include "SkGpuDevice.h"
156995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
16f46a124ddd31ca4053c1719d720a74eb4a3cad6dbsalomon// This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT properly.
17f46a124ddd31ca4053c1719d720a74eb4a3cad6dbsalomonstatic const int X_SIZE = 13;
18f46a124ddd31ca4053c1719d720a74eb4a3cad6dbsalomonstatic const int Y_SIZE = 13;
196995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
201530283c483cb88aa725bce50a6d193dd00ee570kkinnunenDEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, context) {
2123e566664b85472766c921cd2f5615c846919934bsalomon    unsigned char textureData[X_SIZE][Y_SIZE];
226995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
2323e566664b85472766c921cd2f5615c846919934bsalomon    memset(textureData, 0, X_SIZE * Y_SIZE);
246995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
2523e566664b85472766c921cd2f5615c846919934bsalomon    GrSurfaceDesc desc;
266995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
2723e566664b85472766c921cd2f5615c846919934bsalomon    // let Skia know we will be using this texture as a render target
2823e566664b85472766c921cd2f5615c846919934bsalomon    desc.fFlags     = kRenderTarget_GrSurfaceFlag;
2923e566664b85472766c921cd2f5615c846919934bsalomon    // it is a single channel texture
3023e566664b85472766c921cd2f5615c846919934bsalomon    desc.fConfig    = kAlpha_8_GrPixelConfig;
3123e566664b85472766c921cd2f5615c846919934bsalomon    desc.fWidth     = X_SIZE;
3223e566664b85472766c921cd2f5615c846919934bsalomon    desc.fHeight    = Y_SIZE;
336995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
3423e566664b85472766c921cd2f5615c846919934bsalomon    // We are initializing the texture with zeros here
3523e566664b85472766c921cd2f5615c846919934bsalomon    GrTexture* texture = context->textureProvider()->createTexture(desc, false, textureData, 0);
3623e566664b85472766c921cd2f5615c846919934bsalomon    if (!texture) {
3723e566664b85472766c921cd2f5615c846919934bsalomon        return;
3823e566664b85472766c921cd2f5615c846919934bsalomon    }
3923e566664b85472766c921cd2f5615c846919934bsalomon
4023e566664b85472766c921cd2f5615c846919934bsalomon    SkAutoTUnref<GrTexture> au(texture);
4123e566664b85472766c921cd2f5615c846919934bsalomon
4223e566664b85472766c921cd2f5615c846919934bsalomon    // create a distinctive texture
4323e566664b85472766c921cd2f5615c846919934bsalomon    for (int y = 0; y < Y_SIZE; ++y) {
4423e566664b85472766c921cd2f5615c846919934bsalomon        for (int x = 0; x < X_SIZE; ++x) {
4523e566664b85472766c921cd2f5615c846919934bsalomon            textureData[x][y] = x*Y_SIZE+y;
46b76afedf11c7fe933954d030048c3222860249e1bsalomon        }
4723e566664b85472766c921cd2f5615c846919934bsalomon    }
4823e566664b85472766c921cd2f5615c846919934bsalomon
4923e566664b85472766c921cd2f5615c846919934bsalomon    // upload the texture
5023e566664b85472766c921cd2f5615c846919934bsalomon    texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
5123e566664b85472766c921cd2f5615c846919934bsalomon                         textureData, 0);
526995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
5323e566664b85472766c921cd2f5615c846919934bsalomon    unsigned char readback[X_SIZE][Y_SIZE];
546995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
5523e566664b85472766c921cd2f5615c846919934bsalomon    // clear readback to something non-zero so we can detect readback failures
5623e566664b85472766c921cd2f5615c846919934bsalomon    memset(readback, 0x1, X_SIZE * Y_SIZE);
576995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
5823e566664b85472766c921cd2f5615c846919934bsalomon    // read the texture back
5923e566664b85472766c921cd2f5615c846919934bsalomon    texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
6023e566664b85472766c921cd2f5615c846919934bsalomon                        readback, 0);
616995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
6223e566664b85472766c921cd2f5615c846919934bsalomon    // make sure the original & read back versions match
6323e566664b85472766c921cd2f5615c846919934bsalomon    bool match = true;
646995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
6523e566664b85472766c921cd2f5615c846919934bsalomon    for (int y = 0; y < Y_SIZE; ++y) {
6623e566664b85472766c921cd2f5615c846919934bsalomon        for (int x = 0; x < X_SIZE; ++x) {
6723e566664b85472766c921cd2f5615c846919934bsalomon            if (textureData[x][y] != readback[x][y]) {
6823e566664b85472766c921cd2f5615c846919934bsalomon                match = false;
696995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com            }
706995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com        }
7123e566664b85472766c921cd2f5615c846919934bsalomon    }
726995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
7323e566664b85472766c921cd2f5615c846919934bsalomon    REPORTER_ASSERT(reporter, match);
746995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
7523e566664b85472766c921cd2f5615c846919934bsalomon    // Now try writing on the single channel texture
7623e566664b85472766c921cd2f5615c846919934bsalomon    SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
7723e566664b85472766c921cd2f5615c846919934bsalomon    SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarget(), &props,
7823e566664b85472766c921cd2f5615c846919934bsalomon                                                          SkGpuDevice::kUninit_InitContents));
7923e566664b85472766c921cd2f5615c846919934bsalomon    SkCanvas canvas(device);
806995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
8123e566664b85472766c921cd2f5615c846919934bsalomon    SkPaint paint;
826995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
8323e566664b85472766c921cd2f5615c846919934bsalomon    const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
846995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
8523e566664b85472766c921cd2f5615c846919934bsalomon    paint.setColor(SK_ColorWHITE);
866995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
8723e566664b85472766c921cd2f5615c846919934bsalomon    canvas.drawRect(rect, paint);
886995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
8923e566664b85472766c921cd2f5615c846919934bsalomon    texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
9023e566664b85472766c921cd2f5615c846919934bsalomon                        readback, 0);
91d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
9223e566664b85472766c921cd2f5615c846919934bsalomon    match = true;
931530283c483cb88aa725bce50a6d193dd00ee570kkinnunen
9423e566664b85472766c921cd2f5615c846919934bsalomon    for (int y = 0; y < Y_SIZE; ++y) {
9523e566664b85472766c921cd2f5615c846919934bsalomon        for (int x = 0; x < X_SIZE; ++x) {
9623e566664b85472766c921cd2f5615c846919934bsalomon            if (0xFF != readback[x][y]) {
9723e566664b85472766c921cd2f5615c846919934bsalomon                match = false;
98b76afedf11c7fe933954d030048c3222860249e1bsalomon            }
99b76afedf11c7fe933954d030048c3222860249e1bsalomon        }
100b76afedf11c7fe933954d030048c3222860249e1bsalomon    }
10123e566664b85472766c921cd2f5615c846919934bsalomon
10223e566664b85472766c921cd2f5615c846919934bsalomon    REPORTER_ASSERT(reporter, match);
1036995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com}
1046995068c5ade6e179d2af82caddb0c1cd6f433b6robertphillips@google.com
105cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
106