2012-04-06 18:06:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2012-08-02 14:03:32 +00:00
|
|
|
// This test is specific to the GPU backend.
|
2013-02-07 18:58:35 +00:00
|
|
|
#if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_ANDROID)
|
2012-08-02 14:03:32 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
#include "GrContextFactory.h"
|
2014-01-10 22:08:27 +00:00
|
|
|
#include "SkGpuDevice.h"
|
|
|
|
#include "Test.h"
|
2012-04-06 18:06:10 +00:00
|
|
|
|
|
|
|
static const int X_SIZE = 12;
|
|
|
|
static const int Y_SIZE = 12;
|
|
|
|
|
2014-01-10 22:08:27 +00:00
|
|
|
DEF_GPUTEST(ReadWriteAlpha, reporter, factory) {
|
2013-02-04 16:13:32 +00:00
|
|
|
for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
|
|
|
|
GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
|
|
|
|
if (!GrContextFactory::IsRenderingGLContext(glType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
GrContext* context = factory->get(glType);
|
|
|
|
if (NULL == context) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-06 18:25:24 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
unsigned char textureData[X_SIZE][Y_SIZE];
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
memset(textureData, 0, X_SIZE * Y_SIZE);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
GrTextureDesc desc;
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
// let Skia know we will be using this texture as a render target
|
|
|
|
desc.fFlags = kRenderTarget_GrTextureFlagBit;
|
|
|
|
// it is a single channel texture
|
|
|
|
desc.fConfig = kAlpha_8_GrPixelConfig;
|
|
|
|
desc.fWidth = X_SIZE;
|
|
|
|
desc.fHeight = Y_SIZE;
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
// We are initializing the texture with zeros here
|
|
|
|
GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
|
|
|
|
if (!texture) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2014-07-21 21:24:01 +00:00
|
|
|
SkAutoTUnref<GrTexture> au(texture);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
// create a distinctive texture
|
|
|
|
for (int y = 0; y < Y_SIZE; ++y) {
|
|
|
|
for (int x = 0; x < X_SIZE; ++x) {
|
|
|
|
textureData[x][y] = x*Y_SIZE+y;
|
|
|
|
}
|
2012-04-06 18:06:10 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
// upload the texture
|
|
|
|
texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
|
|
|
|
textureData, 0);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
unsigned char readback[X_SIZE][Y_SIZE];
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
// clear readback to something non-zero so we can detect readback failures
|
|
|
|
memset(readback, 0x1, X_SIZE * Y_SIZE);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
// read the texture back
|
|
|
|
texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
|
|
|
|
readback, 0);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
// make sure the original & read back versions match
|
|
|
|
bool match = true;
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
for (int y = 0; y < Y_SIZE; ++y) {
|
|
|
|
for (int x = 0; x < X_SIZE; ++x) {
|
|
|
|
if (textureData[x][y] != readback[x][y]) {
|
|
|
|
match = false;
|
|
|
|
}
|
2012-04-06 18:06:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
REPORTER_ASSERT(reporter, match);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
// Now try writing on the single channel texture
|
2014-08-28 21:06:42 +00:00
|
|
|
SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarget()));
|
2013-02-04 16:13:32 +00:00
|
|
|
SkCanvas canvas(device);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
SkPaint paint;
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
paint.setColor(SK_ColorWHITE);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
canvas.drawRect(rect, paint);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
|
|
|
|
readback, 0);
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
match = true;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
for (int y = 0; y < Y_SIZE; ++y) {
|
|
|
|
for (int x = 0; x < X_SIZE; ++x) {
|
|
|
|
if (0xFF != readback[x][y]) {
|
|
|
|
match = false;
|
|
|
|
}
|
2012-04-06 18:06:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
REPORTER_ASSERT(reporter, match);
|
|
|
|
}
|
2012-04-06 18:06:10 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 14:03:32 +00:00
|
|
|
#endif
|