blob: 053625558ffaaecad11c4d4c10376a7340acbe11 [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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010// This test is specific to the GPU backend.
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
12#include "include/core/SkSurface.h"
13#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkTo.h"
15#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrProxyProvider.h"
17#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrSurfaceProxy.h"
20#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/gpu/ProxyUtils.h"
robertphillips@google.com69950682012-04-06 18:06:10 +000022
bsalomonf46a1242015-12-15 12:37:38 -080023// This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT properly.
24static const int X_SIZE = 13;
25static const int Y_SIZE = 13;
robertphillips@google.com69950682012-04-06 18:06:10 +000026
bsalomon9d02b262016-02-01 12:49:30 -080027static void validate_alpha_data(skiatest::Reporter* reporter, int w, int h, const uint8_t* actual,
Brian Osman10fc6fd2018-03-02 11:01:10 -050028 size_t actualRowBytes, const uint8_t* expected, SkString extraMsg,
Brian Salomon58389b92018-03-07 13:01:25 -050029 GrColorType colorType) {
bsalomon9d02b262016-02-01 12:49:30 -080030 for (int y = 0; y < h; ++y) {
31 for (int x = 0; x < w; ++x) {
32 uint8_t a = actual[y * actualRowBytes + x];
33 uint8_t e = expected[y * w + x];
Brian Salomon58389b92018-03-07 13:01:25 -050034 if (GrColorType::kRGBA_1010102 == colorType) {
Brian Osman10fc6fd2018-03-02 11:01:10 -050035 // This config only preserves two bits of alpha
36 a >>= 6;
37 e >>= 6;
38 }
bsalomon9d02b262016-02-01 12:49:30 -080039 if (e != a) {
40 ERRORF(reporter,
41 "Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d), %s",
42 e, a, x, y, extraMsg.c_str());
43 return;
44 }
45 }
46 }
47}
robertphillips@google.com69950682012-04-06 18:06:10 +000048
egdanielab527a52016-06-28 08:07:26 -070049DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050050 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050051 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050052
bsalomon9d02b262016-02-01 12:49:30 -080053 unsigned char alphaData[X_SIZE * Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000054
robertphillipsdf082c52016-04-19 08:32:40 -070055 static const int kClearValue = 0x2;
56
bsalomone9573312016-01-25 14:33:25 -080057 bool match;
bsalomon9d02b262016-02-01 12:49:30 -080058 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1};
robertphillips7e922762016-07-26 11:38:17 -070059 {
bsalomone9573312016-01-25 14:33:25 -080060 GrSurfaceDesc desc;
robertphillips7e922762016-07-26 11:38:17 -070061 desc.fConfig = kAlpha_8_GrPixelConfig; // it is a single channel texture
bsalomone9573312016-01-25 14:33:25 -080062 desc.fWidth = X_SIZE;
63 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080064
bsalomone9573312016-01-25 14:33:25 -080065 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080066 memset(alphaData, 0, X_SIZE * Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050067
Brian Osman2700abc2018-09-12 10:19:41 -040068 const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE);
69
70 SkPixmap pixmap(ii, alphaData, ii.minRowBytes());
71 sk_sp<SkImage> alphaImg = SkImage::MakeRasterCopy(pixmap);
Brian Salomonf2c2ba92019-07-17 09:59:59 -040072 sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(
73 alphaImg, GrRenderable::kNo, 1, SkBudgeted::kNo, SkBackingFit::kExact);
Robert Phillips2f493142017-03-02 18:18:38 -050074 if (!proxy) {
robertphillips7e922762016-07-26 11:38:17 -070075 ERRORF(reporter, "Could not create alpha texture.");
76 return;
bsalomone9573312016-01-25 14:33:25 -080077 }
Brian Salomond6287472019-06-24 15:50:07 -040078 sk_sp<GrSurfaceContext> sContext(context->priv().makeWrappedSurfaceContext(
79 std::move(proxy), GrColorType::kAlpha_8, kPremul_SkAlphaType));
kkinnunen15302832015-12-01 04:35:26 -080080
Robert Phillipsc949ce92017-01-19 16:59:04 -050081 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
robertphillips7e922762016-07-26 11:38:17 -070082
bsalomone9573312016-01-25 14:33:25 -080083 // create a distinctive texture
84 for (int y = 0; y < Y_SIZE; ++y) {
85 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080086 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080087 }
88 }
kkinnunen15302832015-12-01 04:35:26 -080089
bsalomon9d02b262016-02-01 12:49:30 -080090 for (auto rowBytes : kRowBytes) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050091
bsalomon9d02b262016-02-01 12:49:30 -080092 // upload the texture (do per-rowbytes iteration because we may overwrite below).
Brian Salomon1d435302019-07-01 13:05:28 -040093 bool result = sContext->writePixels(ii, alphaData, 0, {0, 0});
Brian Salomon1c80e992018-01-29 09:50:47 -050094 REPORTER_ASSERT(reporter, result, "Initial A8 writePixels failed");
bsalomone9573312016-01-25 14:33:25 -080095
bsalomon9d02b262016-02-01 12:49:30 -080096 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
Brian Osman10fc6fd2018-03-02 11:01:10 -050097 size_t bufLen = nonZeroRowBytes * Y_SIZE;
98 std::unique_ptr<uint8_t[]> readback(new uint8_t[bufLen]);
bsalomon9d02b262016-02-01 12:49:30 -080099 // clear readback to something non-zero so we can detect readback failures
Brian Osman10fc6fd2018-03-02 11:01:10 -0500100 memset(readback.get(), kClearValue, bufLen);
bsalomone9573312016-01-25 14:33:25 -0800101
bsalomon9d02b262016-02-01 12:49:30 -0800102 // read the texture back
Brian Salomon1d435302019-07-01 13:05:28 -0400103 result = sContext->readPixels(ii, readback.get(), rowBytes, {0, 0});
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400104 // We don't require reading from kAlpha_8 to be supported. TODO: At least make this work
105 // when kAlpha_8 is renderable.
106 if (!result) {
107 continue;
108 }
Brian Salomon1c80e992018-01-29 09:50:47 -0500109 REPORTER_ASSERT(reporter, result, "Initial A8 readPixels failed");
bsalomone9573312016-01-25 14:33:25 -0800110
bsalomon9d02b262016-02-01 12:49:30 -0800111 // make sure the original & read back versions match
112 SkString msg;
robertphillips7e922762016-07-26 11:38:17 -0700113 msg.printf("rb:%d A8", SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800114 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
Brian Salomon58389b92018-03-07 13:01:25 -0500115 alphaData, msg, GrColorType::kAlpha_8);
bsalomone9573312016-01-25 14:33:25 -0800116
robertphillips7e922762016-07-26 11:38:17 -0700117 // Now try writing to a single channel surface (if we could create one).
118 if (surf) {
robertphillips7715e062016-04-22 10:57:16 -0700119 SkCanvas* canvas = surf->getCanvas();
bsalomone9573312016-01-25 14:33:25 -0800120
bsalomon9d02b262016-02-01 12:49:30 -0800121 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -0800122
bsalomon9d02b262016-02-01 12:49:30 -0800123 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800124
bsalomon9d02b262016-02-01 12:49:30 -0800125 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800126
robertphillips7715e062016-04-22 10:57:16 -0700127 canvas->drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800128
Brian Osman10fc6fd2018-03-02 11:01:10 -0500129 // Workaround for a bug in old GCC/glibc used in our Chromecast toolchain:
130 // error: call to '__warn_memset_zero_len' declared with attribute warning:
131 // memset used with constant zero length parameter; this could be due
132 // to transposed parameters
133 // See also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61294
134 if (bufLen > 0) {
135 memset(readback.get(), kClearValue, bufLen);
136 }
robertphillips7e922762016-07-26 11:38:17 -0700137 result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -0500138 REPORTER_ASSERT(reporter, result, "A8 readPixels after clear failed");
bsalomone9573312016-01-25 14:33:25 -0800139
bsalomon9d02b262016-02-01 12:49:30 -0800140 match = true;
141 for (int y = 0; y < Y_SIZE && match; ++y) {
142 for (int x = 0; x < X_SIZE && match; ++x) {
143 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
144 if (0xFF != rbValue) {
145 ERRORF(reporter,
146 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500147 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800148 match = false;
149 }
bsalomone9573312016-01-25 14:33:25 -0800150 }
151 }
kkinnunen15302832015-12-01 04:35:26 -0800152 }
153 }
154 }
155
Brian Salomon58389b92018-03-07 13:01:25 -0500156 static constexpr struct {
157 GrColorType fColorType;
Brian Salomone7499c72019-06-24 12:12:36 -0400158 SkAlphaType fAlphaType;
Brian Salomon58389b92018-03-07 13:01:25 -0500159 } kInfos[] = {
Greg Daniele877dce2019-07-11 10:52:43 -0400160 {GrColorType::kRGBA_8888, kPremul_SkAlphaType},
161 {GrColorType::kBGRA_8888, kPremul_SkAlphaType},
162 {GrColorType::kRGBA_8888_SRGB, kPremul_SkAlphaType},
163 {GrColorType::kRGBA_1010102, kPremul_SkAlphaType},
bsalomone9573312016-01-25 14:33:25 -0800164 };
kkinnunen15302832015-12-01 04:35:26 -0800165
bsalomon9d02b262016-02-01 12:49:30 -0800166 for (int y = 0; y < Y_SIZE; ++y) {
167 for (int x = 0; x < X_SIZE; ++x) {
168 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
169 }
170 }
171
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400172 const SkImageInfo dstInfo = SkImageInfo::Make(X_SIZE, Y_SIZE,
173 kAlpha_8_SkColorType,
174 kPremul_SkAlphaType);
175
bsalomone9573312016-01-25 14:33:25 -0800176 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
177 // once with a render target.
Brian Salomon58389b92018-03-07 13:01:25 -0500178 for (auto info : kInfos) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400179 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
bsalomon9d02b262016-02-01 12:49:30 -0800180 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800181 // Make the alpha channel of the rgba texture come from alphaData.
182 for (int y = 0; y < Y_SIZE; ++y) {
183 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800184 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800185 }
186 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500187
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400188 auto origin = GrRenderable::kYes == renderable ? kBottomLeft_GrSurfaceOrigin
189 : kTopLeft_GrSurfaceOrigin;
Brian Salomone7499c72019-06-24 12:12:36 -0400190 auto proxy = sk_gpu_test::MakeTextureProxyFromData(
Greg Daniele877dce2019-07-11 10:52:43 -0400191 context, renderable, X_SIZE, Y_SIZE, info.fColorType, info.fAlphaType, origin,
192 rgbaData, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400193 if (!proxy) {
bsalomone9573312016-01-25 14:33:25 -0800194 continue;
195 }
kkinnunen15302832015-12-01 04:35:26 -0800196
Robert Phillips9da87e02019-02-04 13:26:26 -0500197 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -0400198 std::move(proxy), info.fColorType, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400199
bsalomon9d02b262016-02-01 12:49:30 -0800200 for (auto rowBytes : kRowBytes) {
201 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800202
Ben Wagner7ecc5962016-11-02 17:07:33 -0400203 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -0800204 // Clear so we don't accidentally see values from previous iteration.
robertphillipsdf082c52016-04-19 08:32:40 -0700205 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800206
bsalomon9d02b262016-02-01 12:49:30 -0800207 // read the texture back
Brian Salomon1d435302019-07-01 13:05:28 -0400208 bool result = sContext->readPixels(dstInfo, readback.get(), rowBytes, {0, 0});
Brian Salomon1c80e992018-01-29 09:50:47 -0500209 REPORTER_ASSERT(reporter, result, "8888 readPixels failed");
bsalomon23e56662016-01-14 07:19:47 -0800210
bsalomon9d02b262016-02-01 12:49:30 -0800211 // make sure the original & read back versions match
212 SkString msg;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400213 msg.printf("rt:%d, rb:%d 8888", GrRenderable::kYes == renderable,
214 SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800215 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
Brian Salomon58389b92018-03-07 13:01:25 -0500216 alphaData, msg, info.fColorType);
kkinnunen15302832015-12-01 04:35:26 -0800217 }
218 }
219 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000220}