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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2014-10-30 18:34:15 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkTo.h"
|
2020-01-28 22:02:49 +00:00
|
|
|
#include "src/gpu/GrBitmapTextureMaker.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrContextPriv.h"
|
2019-09-30 16:15:30 +00:00
|
|
|
#include "src/gpu/GrImageInfo.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrProxyProvider.h"
|
|
|
|
#include "src/gpu/GrSurfaceContext.h"
|
2019-06-18 13:58:02 +00:00
|
|
|
#include "src/gpu/GrSurfaceProxy.h"
|
|
|
|
#include "src/gpu/GrTextureProxy.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/ProxyUtils.h"
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2015-12-15 20:37:38 +00:00
|
|
|
// This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT properly.
|
|
|
|
static const int X_SIZE = 13;
|
|
|
|
static const int Y_SIZE = 13;
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2016-02-01 20:49:30 +00:00
|
|
|
static void validate_alpha_data(skiatest::Reporter* reporter, int w, int h, const uint8_t* actual,
|
2018-03-02 16:01:10 +00:00
|
|
|
size_t actualRowBytes, const uint8_t* expected, SkString extraMsg,
|
2018-03-07 18:01:25 +00:00
|
|
|
GrColorType colorType) {
|
2016-02-01 20:49:30 +00:00
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
uint8_t a = actual[y * actualRowBytes + x];
|
|
|
|
uint8_t e = expected[y * w + x];
|
2020-04-28 19:03:34 +00:00
|
|
|
if (GrColorType::kRGBA_1010102 == colorType ||
|
|
|
|
GrColorType::kBGRA_1010102 == colorType) {
|
|
|
|
// These configs only preserves two bits of alpha
|
2018-03-02 16:01:10 +00:00
|
|
|
a >>= 6;
|
|
|
|
e >>= 6;
|
|
|
|
}
|
2016-02-01 20:49:30 +00:00
|
|
|
if (e != a) {
|
|
|
|
ERRORF(reporter,
|
|
|
|
"Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d), %s",
|
|
|
|
e, a, x, y, extraMsg.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2016-06-28 15:07:26 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) {
|
2020-07-06 14:56:46 +00:00
|
|
|
auto direct = ctxInfo.directContext();
|
2018-01-08 18:40:32 +00:00
|
|
|
|
2016-02-01 20:49:30 +00:00
|
|
|
unsigned char alphaData[X_SIZE * Y_SIZE];
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2016-04-19 15:32:40 +00:00
|
|
|
static const int kClearValue = 0x2;
|
|
|
|
|
2016-01-25 22:33:25 +00:00
|
|
|
bool match;
|
2016-02-01 20:49:30 +00:00
|
|
|
static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1};
|
2016-07-26 18:38:17 +00:00
|
|
|
{
|
2016-01-25 22:33:25 +00:00
|
|
|
// We are initializing the texture with zeros here
|
2016-02-01 20:49:30 +00:00
|
|
|
memset(alphaData, 0, X_SIZE * Y_SIZE);
|
2020-01-28 22:02:49 +00:00
|
|
|
unsigned char alphaDataCopy[X_SIZE * Y_SIZE];
|
|
|
|
memcpy(alphaDataCopy, alphaData, X_SIZE * Y_SIZE);
|
2017-01-19 21:59:04 +00:00
|
|
|
|
2018-09-12 14:19:41 +00:00
|
|
|
const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE);
|
|
|
|
|
2020-01-28 22:02:49 +00:00
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.installPixels(ii, alphaDataCopy, ii.minRowBytes());
|
|
|
|
bitmap.setImmutable();
|
2020-07-06 14:56:46 +00:00
|
|
|
GrBitmapTextureMaker maker(direct, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
|
2020-07-21 13:27:25 +00:00
|
|
|
auto view = maker.view(GrMipmapped::kNo);
|
2020-02-03 19:17:08 +00:00
|
|
|
if (!view.proxy()) {
|
2016-07-26 18:38:17 +00:00
|
|
|
ERRORF(reporter, "Could not create alpha texture.");
|
|
|
|
return;
|
2016-01-13 20:19:15 +00:00
|
|
|
}
|
2020-01-14 14:56:04 +00:00
|
|
|
|
2020-07-06 14:56:46 +00:00
|
|
|
auto sContext = GrSurfaceContext::Make(direct, std::move(view), maker.colorType(),
|
2020-02-28 23:07:32 +00:00
|
|
|
kPremul_SkAlphaType, nullptr);
|
2016-01-14 15:19:47 +00:00
|
|
|
|
2020-07-06 14:56:46 +00:00
|
|
|
sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(direct, SkBudgeted::kNo, ii));
|
2016-07-26 18:38:17 +00:00
|
|
|
|
2016-01-25 22:33:25 +00:00
|
|
|
// create a distinctive texture
|
|
|
|
for (int y = 0; y < Y_SIZE; ++y) {
|
|
|
|
for (int x = 0; x < X_SIZE; ++x) {
|
2016-02-01 20:49:30 +00:00
|
|
|
alphaData[y * X_SIZE + x] = y*X_SIZE+x;
|
2012-04-06 18:06:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:49:30 +00:00
|
|
|
for (auto rowBytes : kRowBytes) {
|
2017-01-19 21:59:04 +00:00
|
|
|
|
2016-02-01 20:49:30 +00:00
|
|
|
// upload the texture (do per-rowbytes iteration because we may overwrite below).
|
2019-07-01 17:05:28 +00:00
|
|
|
bool result = sContext->writePixels(ii, alphaData, 0, {0, 0});
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, result, "Initial A8 writePixels failed");
|
2012-04-06 18:06:10 +00:00
|
|
|
|
2016-02-01 20:49:30 +00:00
|
|
|
size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
|
2018-03-02 16:01:10 +00:00
|
|
|
size_t bufLen = nonZeroRowBytes * Y_SIZE;
|
|
|
|
std::unique_ptr<uint8_t[]> readback(new uint8_t[bufLen]);
|
2016-02-01 20:49:30 +00:00
|
|
|
// clear readback to something non-zero so we can detect readback failures
|
2018-03-02 16:01:10 +00:00
|
|
|
memset(readback.get(), kClearValue, bufLen);
|
2015-12-01 12:35:26 +00:00
|
|
|
|
2016-02-01 20:49:30 +00:00
|
|
|
// read the texture back
|
2019-07-01 17:05:28 +00:00
|
|
|
result = sContext->readPixels(ii, readback.get(), rowBytes, {0, 0});
|
2018-03-19 20:06:44 +00:00
|
|
|
// We don't require reading from kAlpha_8 to be supported. TODO: At least make this work
|
|
|
|
// when kAlpha_8 is renderable.
|
|
|
|
if (!result) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, result, "Initial A8 readPixels failed");
|
2016-02-01 20:49:30 +00:00
|
|
|
|
|
|
|
// make sure the original & read back versions match
|
|
|
|
SkString msg;
|
2016-07-26 18:38:17 +00:00
|
|
|
msg.printf("rb:%d A8", SkToU32(rowBytes));
|
2016-02-01 20:49:30 +00:00
|
|
|
validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
|
2018-03-07 18:01:25 +00:00
|
|
|
alphaData, msg, GrColorType::kAlpha_8);
|
2016-02-01 20:49:30 +00:00
|
|
|
|
2016-07-26 18:38:17 +00:00
|
|
|
// Now try writing to a single channel surface (if we could create one).
|
|
|
|
if (surf) {
|
2016-04-22 17:57:16 +00:00
|
|
|
SkCanvas* canvas = surf->getCanvas();
|
2016-02-01 20:49:30 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
|
2016-04-22 17:57:16 +00:00
|
|
|
canvas->drawRect(rect, paint);
|
2016-02-01 20:49:30 +00:00
|
|
|
|
2018-03-02 16:01:10 +00:00
|
|
|
// Workaround for a bug in old GCC/glibc used in our Chromecast toolchain:
|
|
|
|
// error: call to '__warn_memset_zero_len' declared with attribute warning:
|
|
|
|
// memset used with constant zero length parameter; this could be due
|
|
|
|
// to transposed parameters
|
|
|
|
// See also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61294
|
|
|
|
if (bufLen > 0) {
|
|
|
|
memset(readback.get(), kClearValue, bufLen);
|
|
|
|
}
|
2016-07-26 18:38:17 +00:00
|
|
|
result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0);
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, result, "A8 readPixels after clear failed");
|
2016-02-01 20:49:30 +00:00
|
|
|
|
|
|
|
match = true;
|
|
|
|
for (int y = 0; y < Y_SIZE && match; ++y) {
|
|
|
|
for (int x = 0; x < X_SIZE && match; ++x) {
|
|
|
|
uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
|
|
|
|
if (0xFF != rbValue) {
|
|
|
|
ERRORF(reporter,
|
|
|
|
"Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
|
2016-02-02 16:34:46 +00:00
|
|
|
" at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
|
2016-02-01 20:49:30 +00:00
|
|
|
match = false;
|
|
|
|
}
|
2016-01-25 22:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-13 20:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 15:19:47 +00:00
|
|
|
|
2018-03-07 18:01:25 +00:00
|
|
|
static constexpr struct {
|
|
|
|
GrColorType fColorType;
|
2019-06-24 16:12:36 +00:00
|
|
|
SkAlphaType fAlphaType;
|
2018-03-07 18:01:25 +00:00
|
|
|
} kInfos[] = {
|
2019-07-11 14:52:43 +00:00
|
|
|
{GrColorType::kRGBA_8888, kPremul_SkAlphaType},
|
|
|
|
{GrColorType::kBGRA_8888, kPremul_SkAlphaType},
|
|
|
|
{GrColorType::kRGBA_8888_SRGB, kPremul_SkAlphaType},
|
|
|
|
{GrColorType::kRGBA_1010102, kPremul_SkAlphaType},
|
2016-01-25 22:33:25 +00:00
|
|
|
};
|
|
|
|
|
2016-02-01 20:49:30 +00:00
|
|
|
for (int y = 0; y < Y_SIZE; ++y) {
|
|
|
|
for (int x = 0; x < X_SIZE; ++x) {
|
|
|
|
alphaData[y * X_SIZE + x] = y*X_SIZE+x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 11:43:27 +00:00
|
|
|
const SkImageInfo dstInfo = SkImageInfo::Make(X_SIZE, Y_SIZE,
|
|
|
|
kAlpha_8_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
|
|
|
|
2016-01-25 22:33:25 +00:00
|
|
|
// Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
|
|
|
|
// once with a render target.
|
2018-03-07 18:01:25 +00:00
|
|
|
for (auto info : kInfos) {
|
2019-05-13 14:40:06 +00:00
|
|
|
for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
|
2016-02-01 20:49:30 +00:00
|
|
|
uint32_t rgbaData[X_SIZE * Y_SIZE];
|
2016-01-25 22:33:25 +00:00
|
|
|
// Make the alpha channel of the rgba texture come from alphaData.
|
|
|
|
for (int y = 0; y < Y_SIZE; ++y) {
|
|
|
|
for (int x = 0; x < X_SIZE; ++x) {
|
2016-02-01 20:49:30 +00:00
|
|
|
rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
|
2016-01-25 22:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-16 13:06:32 +00:00
|
|
|
|
2019-05-13 14:40:06 +00:00
|
|
|
auto origin = GrRenderable::kYes == renderable ? kBottomLeft_GrSurfaceOrigin
|
|
|
|
: kTopLeft_GrSurfaceOrigin;
|
2019-06-24 16:12:36 +00:00
|
|
|
auto proxy = sk_gpu_test::MakeTextureProxyFromData(
|
2020-07-06 14:56:46 +00:00
|
|
|
direct, renderable, origin,
|
2019-10-21 19:04:52 +00:00
|
|
|
{info.fColorType, info.fAlphaType, nullptr, X_SIZE, Y_SIZE}, rgbaData, 0);
|
2017-04-06 11:59:41 +00:00
|
|
|
if (!proxy) {
|
2016-01-25 22:33:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-06 14:56:46 +00:00
|
|
|
GrSwizzle swizzle = direct->priv().caps()->getReadSwizzle(proxy->backendFormat(),
|
|
|
|
info.fColorType);
|
2020-01-14 14:56:04 +00:00
|
|
|
GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
|
2020-07-06 14:56:46 +00:00
|
|
|
auto sContext = GrSurfaceContext::Make(direct, std::move(view), info.fColorType,
|
2019-12-19 21:41:40 +00:00
|
|
|
kPremul_SkAlphaType, nullptr);
|
2017-04-17 11:43:27 +00:00
|
|
|
|
2016-02-01 20:49:30 +00:00
|
|
|
for (auto rowBytes : kRowBytes) {
|
|
|
|
size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
|
2016-01-25 22:33:25 +00:00
|
|
|
|
2016-11-02 21:07:33 +00:00
|
|
|
std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
|
2016-02-01 20:49:30 +00:00
|
|
|
// Clear so we don't accidentally see values from previous iteration.
|
2016-04-19 15:32:40 +00:00
|
|
|
memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
|
2016-02-01 20:49:30 +00:00
|
|
|
|
|
|
|
// read the texture back
|
2019-07-01 17:05:28 +00:00
|
|
|
bool result = sContext->readPixels(dstInfo, readback.get(), rowBytes, {0, 0});
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, result, "8888 readPixels failed");
|
2016-02-01 20:49:30 +00:00
|
|
|
|
|
|
|
// make sure the original & read back versions match
|
|
|
|
SkString msg;
|
2019-05-13 14:40:06 +00:00
|
|
|
msg.printf("rt:%d, rb:%d 8888", GrRenderable::kYes == renderable,
|
|
|
|
SkToU32(rowBytes));
|
2016-02-01 20:49:30 +00:00
|
|
|
validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
|
2018-03-07 18:01:25 +00:00
|
|
|
alphaData, msg, info.fColorType);
|
2016-01-25 22:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-06 18:06:10 +00:00
|
|
|
}
|