blob: eaad411d5ae7f05aea6c2fd822e4dc998ba3ab90 [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
reedf037e0b2014-10-30 11:34:15 -07008#include "Test.h"
9
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010// This test is specific to the GPU backend.
bsalomone9573312016-01-25 14:33:25 -080011#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080012#include "GrContext.h"
Robert Phillipsc949ce92017-01-19 16:59:04 -050013#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050014#include "GrProxyProvider.h"
Brian Osman32342f02017-03-04 08:12:46 -050015#include "GrResourceProvider.h"
Robert Phillipsc949ce92017-01-19 16:59:04 -050016#include "GrSurfaceContext.h"
17#include "GrSurfaceProxy.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040018#include "GrTextureProxy.h"
Brian Salomon58389b92018-03-07 13:01:25 -050019#include "ProxyUtils.h"
robertphillips7715e062016-04-22 10:57:16 -070020#include "SkCanvas.h"
21#include "SkSurface.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 Phillips1afd4cd2018-01-08 13:40:32 -050051 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
52
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.fFlags = kNone_GrSurfaceFlags;
62 desc.fConfig = kAlpha_8_GrPixelConfig; // it is a single channel texture
bsalomone9573312016-01-25 14:33:25 -080063 desc.fWidth = X_SIZE;
64 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080065
bsalomone9573312016-01-25 14:33:25 -080066 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080067 memset(alphaData, 0, X_SIZE * Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050068
Brian Salomon58389b92018-03-07 13:01:25 -050069 sk_sp<GrTextureProxy> proxy =
70 proxyProvider->createTextureProxy(desc, SkBudgeted::kNo, alphaData, 0);
Robert Phillips2f493142017-03-02 18:18:38 -050071 if (!proxy) {
robertphillips7e922762016-07-26 11:38:17 -070072 ERRORF(reporter, "Could not create alpha texture.");
73 return;
bsalomone9573312016-01-25 14:33:25 -080074 }
Robert Phillipsc949ce92017-01-19 16:59:04 -050075 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050076 std::move(proxy)));
kkinnunen15302832015-12-01 04:35:26 -080077
robertphillips7e922762016-07-26 11:38:17 -070078 const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050079 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
robertphillips7e922762016-07-26 11:38:17 -070080
bsalomone9573312016-01-25 14:33:25 -080081 // create a distinctive texture
82 for (int y = 0; y < Y_SIZE; ++y) {
83 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080084 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080085 }
86 }
kkinnunen15302832015-12-01 04:35:26 -080087
bsalomon9d02b262016-02-01 12:49:30 -080088 for (auto rowBytes : kRowBytes) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050089
bsalomon9d02b262016-02-01 12:49:30 -080090 // upload the texture (do per-rowbytes iteration because we may overwrite below).
Robert Phillipsc949ce92017-01-19 16:59:04 -050091 bool result = sContext->writePixels(ii, alphaData, 0, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -050092 REPORTER_ASSERT(reporter, result, "Initial A8 writePixels failed");
bsalomone9573312016-01-25 14:33:25 -080093
bsalomon9d02b262016-02-01 12:49:30 -080094 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
Brian Osman10fc6fd2018-03-02 11:01:10 -050095 size_t bufLen = nonZeroRowBytes * Y_SIZE;
96 std::unique_ptr<uint8_t[]> readback(new uint8_t[bufLen]);
bsalomon9d02b262016-02-01 12:49:30 -080097 // clear readback to something non-zero so we can detect readback failures
Brian Osman10fc6fd2018-03-02 11:01:10 -050098 memset(readback.get(), kClearValue, bufLen);
bsalomone9573312016-01-25 14:33:25 -080099
bsalomon9d02b262016-02-01 12:49:30 -0800100 // read the texture back
Robert Phillipsc949ce92017-01-19 16:59:04 -0500101 result = sContext->readPixels(ii, readback.get(), rowBytes, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -0500102 REPORTER_ASSERT(reporter, result, "Initial A8 readPixels failed");
bsalomone9573312016-01-25 14:33:25 -0800103
bsalomon9d02b262016-02-01 12:49:30 -0800104 // make sure the original & read back versions match
105 SkString msg;
robertphillips7e922762016-07-26 11:38:17 -0700106 msg.printf("rb:%d A8", SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800107 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
Brian Salomon58389b92018-03-07 13:01:25 -0500108 alphaData, msg, GrColorType::kAlpha_8);
bsalomone9573312016-01-25 14:33:25 -0800109
robertphillips7e922762016-07-26 11:38:17 -0700110 // Now try writing to a single channel surface (if we could create one).
111 if (surf) {
robertphillips7715e062016-04-22 10:57:16 -0700112 SkCanvas* canvas = surf->getCanvas();
bsalomone9573312016-01-25 14:33:25 -0800113
bsalomon9d02b262016-02-01 12:49:30 -0800114 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -0800115
bsalomon9d02b262016-02-01 12:49:30 -0800116 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800117
bsalomon9d02b262016-02-01 12:49:30 -0800118 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800119
robertphillips7715e062016-04-22 10:57:16 -0700120 canvas->drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800121
Brian Osman10fc6fd2018-03-02 11:01:10 -0500122 // Workaround for a bug in old GCC/glibc used in our Chromecast toolchain:
123 // error: call to '__warn_memset_zero_len' declared with attribute warning:
124 // memset used with constant zero length parameter; this could be due
125 // to transposed parameters
126 // See also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61294
127 if (bufLen > 0) {
128 memset(readback.get(), kClearValue, bufLen);
129 }
robertphillips7e922762016-07-26 11:38:17 -0700130 result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -0500131 REPORTER_ASSERT(reporter, result, "A8 readPixels after clear failed");
bsalomone9573312016-01-25 14:33:25 -0800132
bsalomon9d02b262016-02-01 12:49:30 -0800133 match = true;
134 for (int y = 0; y < Y_SIZE && match; ++y) {
135 for (int x = 0; x < X_SIZE && match; ++x) {
136 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
137 if (0xFF != rbValue) {
138 ERRORF(reporter,
139 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500140 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800141 match = false;
142 }
bsalomone9573312016-01-25 14:33:25 -0800143 }
144 }
kkinnunen15302832015-12-01 04:35:26 -0800145 }
146 }
147 }
148
Brian Salomon58389b92018-03-07 13:01:25 -0500149 static constexpr struct {
150 GrColorType fColorType;
151 GrSRGBEncoded fSRGBEncoded;
152 } kInfos[] = {
153 {GrColorType::kRGBA_8888, GrSRGBEncoded::kNo},
154 {GrColorType::kBGRA_8888, GrSRGBEncoded::kNo},
155 {GrColorType::kRGBA_8888, GrSRGBEncoded::kYes},
156 {GrColorType::kRGBA_1010102, GrSRGBEncoded::kNo},
bsalomone9573312016-01-25 14:33:25 -0800157 };
kkinnunen15302832015-12-01 04:35:26 -0800158
bsalomon9d02b262016-02-01 12:49:30 -0800159 for (int y = 0; y < Y_SIZE; ++y) {
160 for (int x = 0; x < X_SIZE; ++x) {
161 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
162 }
163 }
164
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400165 const SkImageInfo dstInfo = SkImageInfo::Make(X_SIZE, Y_SIZE,
166 kAlpha_8_SkColorType,
167 kPremul_SkAlphaType);
168
bsalomone9573312016-01-25 14:33:25 -0800169 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
170 // once with a render target.
Brian Salomon58389b92018-03-07 13:01:25 -0500171 for (auto info : kInfos) {
bsalomone9573312016-01-25 14:33:25 -0800172 for (int rt = 0; rt < 2; ++rt) {
bsalomon9d02b262016-02-01 12:49:30 -0800173 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800174 // Make the alpha channel of the rgba texture come from alphaData.
175 for (int y = 0; y < Y_SIZE; ++y) {
176 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800177 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800178 }
179 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500180
Brian Salomon2a4f9832018-03-03 22:43:43 -0500181 auto origin = rt ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
Brian Salomon58389b92018-03-07 13:01:25 -0500182 auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, rt, X_SIZE, Y_SIZE,
183 info.fColorType, info.fSRGBEncoded,
184 origin, rgbaData, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400185 if (!proxy) {
bsalomone9573312016-01-25 14:33:25 -0800186 continue;
187 }
kkinnunen15302832015-12-01 04:35:26 -0800188
Brian Salomon366093f2018-02-13 09:25:22 -0500189 sk_sp<SkColorSpace> colorSpace;
190 if (GrPixelConfigIsSRGB(proxy->config())) {
191 colorSpace = SkColorSpace::MakeSRGB();
192 }
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400193 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
Brian Salomon366093f2018-02-13 09:25:22 -0500194 std::move(proxy), std::move(colorSpace));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400195
bsalomon9d02b262016-02-01 12:49:30 -0800196 for (auto rowBytes : kRowBytes) {
197 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800198
Ben Wagner7ecc5962016-11-02 17:07:33 -0400199 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -0800200 // Clear so we don't accidentally see values from previous iteration.
robertphillipsdf082c52016-04-19 08:32:40 -0700201 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800202
bsalomon9d02b262016-02-01 12:49:30 -0800203 // read the texture back
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400204 bool result = sContext->readPixels(dstInfo, readback.get(), rowBytes, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -0500205 REPORTER_ASSERT(reporter, result, "8888 readPixels failed");
bsalomon23e56662016-01-14 07:19:47 -0800206
bsalomon9d02b262016-02-01 12:49:30 -0800207 // make sure the original & read back versions match
208 SkString msg;
robertphillipsdf082c52016-04-19 08:32:40 -0700209 msg.printf("rt:%d, rb:%d 8888", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800210 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
Brian Salomon58389b92018-03-07 13:01:25 -0500211 alphaData, msg, info.fColorType);
kkinnunen15302832015-12-01 04:35:26 -0800212 }
213 }
214 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000215}
216
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000217#endif