blob: dd758f1d9dc03634d76452346734dd2c13b4731e [file] [log] [blame]
robertphillips@google.com69950682012-04-06 18:06:10 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
reedf037e0b2014-10-30 11:34:15 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkSurface.h"
12#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkTo.h"
14#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040015#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrSurfaceProxy.h"
19#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "tools/gpu/ProxyUtils.h"
robertphillips@google.com69950682012-04-06 18:06:10 +000021
bsalomonf46a1242015-12-15 12:37:38 -080022// This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT properly.
23static const int X_SIZE = 13;
24static const int Y_SIZE = 13;
robertphillips@google.com69950682012-04-06 18:06:10 +000025
bsalomon9d02b262016-02-01 12:49:30 -080026static void validate_alpha_data(skiatest::Reporter* reporter, int w, int h, const uint8_t* actual,
Brian Osman10fc6fd2018-03-02 11:01:10 -050027 size_t actualRowBytes, const uint8_t* expected, SkString extraMsg,
Brian Salomon58389b92018-03-07 13:01:25 -050028 GrColorType colorType) {
bsalomon9d02b262016-02-01 12:49:30 -080029 for (int y = 0; y < h; ++y) {
30 for (int x = 0; x < w; ++x) {
31 uint8_t a = actual[y * actualRowBytes + x];
32 uint8_t e = expected[y * w + x];
Brian Salomon58389b92018-03-07 13:01:25 -050033 if (GrColorType::kRGBA_1010102 == colorType) {
Brian Osman10fc6fd2018-03-02 11:01:10 -050034 // This config only preserves two bits of alpha
35 a >>= 6;
36 e >>= 6;
37 }
bsalomon9d02b262016-02-01 12:49:30 -080038 if (e != a) {
39 ERRORF(reporter,
40 "Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d), %s",
41 e, a, x, y, extraMsg.c_str());
42 return;
43 }
44 }
45 }
46}
robertphillips@google.com69950682012-04-06 18:06:10 +000047
egdanielab527a52016-06-28 08:07:26 -070048DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050049 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050050 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050051
bsalomon9d02b262016-02-01 12:49:30 -080052 unsigned char alphaData[X_SIZE * Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000053
robertphillipsdf082c52016-04-19 08:32:40 -070054 static const int kClearValue = 0x2;
55
bsalomone9573312016-01-25 14:33:25 -080056 bool match;
bsalomon9d02b262016-02-01 12:49:30 -080057 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1};
robertphillips7e922762016-07-26 11:38:17 -070058 {
bsalomone9573312016-01-25 14:33:25 -080059 GrSurfaceDesc desc;
bsalomone9573312016-01-25 14:33:25 -080060 desc.fWidth = X_SIZE;
61 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080062
bsalomone9573312016-01-25 14:33:25 -080063 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080064 memset(alphaData, 0, X_SIZE * Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050065
Brian Osman2700abc2018-09-12 10:19:41 -040066 const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE);
67
68 SkPixmap pixmap(ii, alphaData, ii.minRowBytes());
69 sk_sp<SkImage> alphaImg = SkImage::MakeRasterCopy(pixmap);
Brian Salomonf2c2ba92019-07-17 09:59:59 -040070 sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(
Brian Salomon96b383a2019-08-13 16:55:41 -040071 alphaImg, 1, SkBudgeted::kNo, SkBackingFit::kExact);
Robert Phillips2f493142017-03-02 18:18:38 -050072 if (!proxy) {
robertphillips7e922762016-07-26 11:38:17 -070073 ERRORF(reporter, "Could not create alpha texture.");
74 return;
bsalomone9573312016-01-25 14:33:25 -080075 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050076
77 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
78 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
79 GrColorType::kAlpha_8);
80 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
81 auto sContext = GrSurfaceContext::Make(context, std::move(view), GrColorType::kAlpha_8,
Greg Danielbfa19c42019-12-19 16:41:40 -050082 kPremul_SkAlphaType, nullptr);
kkinnunen15302832015-12-01 04:35:26 -080083
Robert Phillipsc949ce92017-01-19 16:59:04 -050084 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
robertphillips7e922762016-07-26 11:38:17 -070085
bsalomone9573312016-01-25 14:33:25 -080086 // create a distinctive texture
87 for (int y = 0; y < Y_SIZE; ++y) {
88 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080089 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080090 }
91 }
kkinnunen15302832015-12-01 04:35:26 -080092
bsalomon9d02b262016-02-01 12:49:30 -080093 for (auto rowBytes : kRowBytes) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050094
bsalomon9d02b262016-02-01 12:49:30 -080095 // upload the texture (do per-rowbytes iteration because we may overwrite below).
Brian Salomon1d435302019-07-01 13:05:28 -040096 bool result = sContext->writePixels(ii, alphaData, 0, {0, 0});
Brian Salomon1c80e992018-01-29 09:50:47 -050097 REPORTER_ASSERT(reporter, result, "Initial A8 writePixels failed");
bsalomone9573312016-01-25 14:33:25 -080098
bsalomon9d02b262016-02-01 12:49:30 -080099 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500100 size_t bufLen = nonZeroRowBytes * Y_SIZE;
101 std::unique_ptr<uint8_t[]> readback(new uint8_t[bufLen]);
bsalomon9d02b262016-02-01 12:49:30 -0800102 // clear readback to something non-zero so we can detect readback failures
Brian Osman10fc6fd2018-03-02 11:01:10 -0500103 memset(readback.get(), kClearValue, bufLen);
bsalomone9573312016-01-25 14:33:25 -0800104
bsalomon9d02b262016-02-01 12:49:30 -0800105 // read the texture back
Brian Salomon1d435302019-07-01 13:05:28 -0400106 result = sContext->readPixels(ii, readback.get(), rowBytes, {0, 0});
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400107 // We don't require reading from kAlpha_8 to be supported. TODO: At least make this work
108 // when kAlpha_8 is renderable.
109 if (!result) {
110 continue;
111 }
Brian Salomon1c80e992018-01-29 09:50:47 -0500112 REPORTER_ASSERT(reporter, result, "Initial A8 readPixels failed");
bsalomone9573312016-01-25 14:33:25 -0800113
bsalomon9d02b262016-02-01 12:49:30 -0800114 // make sure the original & read back versions match
115 SkString msg;
robertphillips7e922762016-07-26 11:38:17 -0700116 msg.printf("rb:%d A8", SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800117 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
Brian Salomon58389b92018-03-07 13:01:25 -0500118 alphaData, msg, GrColorType::kAlpha_8);
bsalomone9573312016-01-25 14:33:25 -0800119
robertphillips7e922762016-07-26 11:38:17 -0700120 // Now try writing to a single channel surface (if we could create one).
121 if (surf) {
robertphillips7715e062016-04-22 10:57:16 -0700122 SkCanvas* canvas = surf->getCanvas();
bsalomone9573312016-01-25 14:33:25 -0800123
bsalomon9d02b262016-02-01 12:49:30 -0800124 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -0800125
bsalomon9d02b262016-02-01 12:49:30 -0800126 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800127
bsalomon9d02b262016-02-01 12:49:30 -0800128 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800129
robertphillips7715e062016-04-22 10:57:16 -0700130 canvas->drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800131
Brian Osman10fc6fd2018-03-02 11:01:10 -0500132 // Workaround for a bug in old GCC/glibc used in our Chromecast toolchain:
133 // error: call to '__warn_memset_zero_len' declared with attribute warning:
134 // memset used with constant zero length parameter; this could be due
135 // to transposed parameters
136 // See also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61294
137 if (bufLen > 0) {
138 memset(readback.get(), kClearValue, bufLen);
139 }
robertphillips7e922762016-07-26 11:38:17 -0700140 result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -0500141 REPORTER_ASSERT(reporter, result, "A8 readPixels after clear failed");
bsalomone9573312016-01-25 14:33:25 -0800142
bsalomon9d02b262016-02-01 12:49:30 -0800143 match = true;
144 for (int y = 0; y < Y_SIZE && match; ++y) {
145 for (int x = 0; x < X_SIZE && match; ++x) {
146 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
147 if (0xFF != rbValue) {
148 ERRORF(reporter,
149 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500150 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800151 match = false;
152 }
bsalomone9573312016-01-25 14:33:25 -0800153 }
154 }
kkinnunen15302832015-12-01 04:35:26 -0800155 }
156 }
157 }
158
Brian Salomon58389b92018-03-07 13:01:25 -0500159 static constexpr struct {
160 GrColorType fColorType;
Brian Salomone7499c72019-06-24 12:12:36 -0400161 SkAlphaType fAlphaType;
Brian Salomon58389b92018-03-07 13:01:25 -0500162 } kInfos[] = {
Greg Daniele877dce2019-07-11 10:52:43 -0400163 {GrColorType::kRGBA_8888, kPremul_SkAlphaType},
164 {GrColorType::kBGRA_8888, kPremul_SkAlphaType},
165 {GrColorType::kRGBA_8888_SRGB, kPremul_SkAlphaType},
166 {GrColorType::kRGBA_1010102, kPremul_SkAlphaType},
bsalomone9573312016-01-25 14:33:25 -0800167 };
kkinnunen15302832015-12-01 04:35:26 -0800168
bsalomon9d02b262016-02-01 12:49:30 -0800169 for (int y = 0; y < Y_SIZE; ++y) {
170 for (int x = 0; x < X_SIZE; ++x) {
171 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
172 }
173 }
174
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400175 const SkImageInfo dstInfo = SkImageInfo::Make(X_SIZE, Y_SIZE,
176 kAlpha_8_SkColorType,
177 kPremul_SkAlphaType);
178
bsalomone9573312016-01-25 14:33:25 -0800179 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
180 // once with a render target.
Brian Salomon58389b92018-03-07 13:01:25 -0500181 for (auto info : kInfos) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400182 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
bsalomon9d02b262016-02-01 12:49:30 -0800183 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800184 // Make the alpha channel of the rgba texture come from alphaData.
185 for (int y = 0; y < Y_SIZE; ++y) {
186 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800187 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800188 }
189 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500190
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400191 auto origin = GrRenderable::kYes == renderable ? kBottomLeft_GrSurfaceOrigin
192 : kTopLeft_GrSurfaceOrigin;
Brian Salomone7499c72019-06-24 12:12:36 -0400193 auto proxy = sk_gpu_test::MakeTextureProxyFromData(
Brian Salomon4eda7102019-10-21 15:04:52 -0400194 context, renderable, origin,
195 {info.fColorType, info.fAlphaType, nullptr, X_SIZE, Y_SIZE}, rgbaData, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400196 if (!proxy) {
bsalomone9573312016-01-25 14:33:25 -0800197 continue;
198 }
kkinnunen15302832015-12-01 04:35:26 -0800199
Greg Daniel3912a4b2020-01-14 09:56:04 -0500200 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
201 info.fColorType);
202 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
203 auto sContext = GrSurfaceContext::Make(context, std::move(view), info.fColorType,
Greg Danielbfa19c42019-12-19 16:41:40 -0500204 kPremul_SkAlphaType, nullptr);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400205
bsalomon9d02b262016-02-01 12:49:30 -0800206 for (auto rowBytes : kRowBytes) {
207 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800208
Ben Wagner7ecc5962016-11-02 17:07:33 -0400209 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -0800210 // Clear so we don't accidentally see values from previous iteration.
robertphillipsdf082c52016-04-19 08:32:40 -0700211 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800212
bsalomon9d02b262016-02-01 12:49:30 -0800213 // read the texture back
Brian Salomon1d435302019-07-01 13:05:28 -0400214 bool result = sContext->readPixels(dstInfo, readback.get(), rowBytes, {0, 0});
Brian Salomon1c80e992018-01-29 09:50:47 -0500215 REPORTER_ASSERT(reporter, result, "8888 readPixels failed");
bsalomon23e56662016-01-14 07:19:47 -0800216
bsalomon9d02b262016-02-01 12:49:30 -0800217 // make sure the original & read back versions match
218 SkString msg;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400219 msg.printf("rt:%d, rb:%d 8888", GrRenderable::kYes == renderable,
220 SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800221 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
Brian Salomon58389b92018-03-07 13:01:25 -0500222 alphaData, msg, info.fColorType);
kkinnunen15302832015-12-01 04:35:26 -0800223 }
224 }
225 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000226}