blob: eefa28ae57026b9fef636b669b982c5e9dcae064 [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"
Robert Phillips6d344c32020-07-06 10:56:46 -040012#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkTo.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050014#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040016#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#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];
Robert Phillipscbc96672020-04-28 15:03:34 -040034 if (GrColorType::kRGBA_1010102 == colorType ||
35 GrColorType::kBGRA_1010102 == colorType) {
36 // These configs only preserves two bits of alpha
Brian Osman10fc6fd2018-03-02 11:01:10 -050037 a >>= 6;
38 e >>= 6;
39 }
bsalomon9d02b262016-02-01 12:49:30 -080040 if (e != a) {
41 ERRORF(reporter,
42 "Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d), %s",
43 e, a, x, y, extraMsg.c_str());
44 return;
45 }
46 }
47 }
48}
robertphillips@google.com69950682012-04-06 18:06:10 +000049
egdanielab527a52016-06-28 08:07:26 -070050DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040051 auto direct = ctxInfo.directContext();
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 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080061 memset(alphaData, 0, X_SIZE * Y_SIZE);
Greg Daniel6f5441a2020-01-28 17:02:49 -050062 unsigned char alphaDataCopy[X_SIZE * Y_SIZE];
63 memcpy(alphaDataCopy, alphaData, X_SIZE * Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050064
Brian Osman2700abc2018-09-12 10:19:41 -040065 const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE);
66
Greg Daniel6f5441a2020-01-28 17:02:49 -050067 SkBitmap bitmap;
68 bitmap.installPixels(ii, alphaDataCopy, ii.minRowBytes());
69 bitmap.setImmutable();
Robert Phillips6d344c32020-07-06 10:56:46 -040070 GrBitmapTextureMaker maker(direct, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -040071 auto view = maker.view(GrMipmapped::kNo);
Greg Danielcc104db2020-02-03 14:17:08 -050072 if (!view.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
Robert Phillips6d344c32020-07-06 10:56:46 -040077 auto sContext = GrSurfaceContext::Make(direct, std::move(view), maker.colorType(),
Brian Salomonecbb0fb2020-02-28 18:07:32 -050078 kPremul_SkAlphaType, nullptr);
kkinnunen15302832015-12-01 04:35:26 -080079
Robert Phillips6d344c32020-07-06 10:56:46 -040080 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(direct, SkBudgeted::kNo, ii));
robertphillips7e922762016-07-26 11:38:17 -070081
bsalomone9573312016-01-25 14:33:25 -080082 // create a distinctive texture
83 for (int y = 0; y < Y_SIZE; ++y) {
84 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080085 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080086 }
87 }
kkinnunen15302832015-12-01 04:35:26 -080088
bsalomon9d02b262016-02-01 12:49:30 -080089 for (auto rowBytes : kRowBytes) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050090
bsalomon9d02b262016-02-01 12:49:30 -080091 // upload the texture (do per-rowbytes iteration because we may overwrite below).
Brian Salomon1d435302019-07-01 13:05:28 -040092 bool result = sContext->writePixels(ii, alphaData, 0, {0, 0});
Brian Salomon1c80e992018-01-29 09:50:47 -050093 REPORTER_ASSERT(reporter, result, "Initial A8 writePixels failed");
bsalomone9573312016-01-25 14:33:25 -080094
bsalomon9d02b262016-02-01 12:49:30 -080095 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
Brian Osman10fc6fd2018-03-02 11:01:10 -050096 size_t bufLen = nonZeroRowBytes * Y_SIZE;
97 std::unique_ptr<uint8_t[]> readback(new uint8_t[bufLen]);
bsalomon9d02b262016-02-01 12:49:30 -080098 // clear readback to something non-zero so we can detect readback failures
Brian Osman10fc6fd2018-03-02 11:01:10 -050099 memset(readback.get(), kClearValue, bufLen);
bsalomone9573312016-01-25 14:33:25 -0800100
bsalomon9d02b262016-02-01 12:49:30 -0800101 // read the texture back
Brian Salomon1d435302019-07-01 13:05:28 -0400102 result = sContext->readPixels(ii, readback.get(), rowBytes, {0, 0});
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400103 // We don't require reading from kAlpha_8 to be supported. TODO: At least make this work
104 // when kAlpha_8 is renderable.
105 if (!result) {
106 continue;
107 }
Brian Salomon1c80e992018-01-29 09:50:47 -0500108 REPORTER_ASSERT(reporter, result, "Initial A8 readPixels failed");
bsalomone9573312016-01-25 14:33:25 -0800109
bsalomon9d02b262016-02-01 12:49:30 -0800110 // make sure the original & read back versions match
111 SkString msg;
robertphillips7e922762016-07-26 11:38:17 -0700112 msg.printf("rb:%d A8", SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800113 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
Brian Salomon58389b92018-03-07 13:01:25 -0500114 alphaData, msg, GrColorType::kAlpha_8);
bsalomone9573312016-01-25 14:33:25 -0800115
robertphillips7e922762016-07-26 11:38:17 -0700116 // Now try writing to a single channel surface (if we could create one).
117 if (surf) {
robertphillips7715e062016-04-22 10:57:16 -0700118 SkCanvas* canvas = surf->getCanvas();
bsalomone9573312016-01-25 14:33:25 -0800119
bsalomon9d02b262016-02-01 12:49:30 -0800120 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -0800121
bsalomon9d02b262016-02-01 12:49:30 -0800122 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800123
bsalomon9d02b262016-02-01 12:49:30 -0800124 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800125
robertphillips7715e062016-04-22 10:57:16 -0700126 canvas->drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800127
Brian Osman10fc6fd2018-03-02 11:01:10 -0500128 // Workaround for a bug in old GCC/glibc used in our Chromecast toolchain:
129 // error: call to '__warn_memset_zero_len' declared with attribute warning:
130 // memset used with constant zero length parameter; this could be due
131 // to transposed parameters
132 // See also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61294
133 if (bufLen > 0) {
134 memset(readback.get(), kClearValue, bufLen);
135 }
robertphillips7e922762016-07-26 11:38:17 -0700136 result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -0500137 REPORTER_ASSERT(reporter, result, "A8 readPixels after clear failed");
bsalomone9573312016-01-25 14:33:25 -0800138
bsalomon9d02b262016-02-01 12:49:30 -0800139 match = true;
140 for (int y = 0; y < Y_SIZE && match; ++y) {
141 for (int x = 0; x < X_SIZE && match; ++x) {
142 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
143 if (0xFF != rbValue) {
144 ERRORF(reporter,
145 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500146 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800147 match = false;
148 }
bsalomone9573312016-01-25 14:33:25 -0800149 }
150 }
kkinnunen15302832015-12-01 04:35:26 -0800151 }
152 }
153 }
154
Brian Salomon58389b92018-03-07 13:01:25 -0500155 static constexpr struct {
156 GrColorType fColorType;
Brian Salomone7499c72019-06-24 12:12:36 -0400157 SkAlphaType fAlphaType;
Brian Salomon58389b92018-03-07 13:01:25 -0500158 } kInfos[] = {
Greg Daniele877dce2019-07-11 10:52:43 -0400159 {GrColorType::kRGBA_8888, kPremul_SkAlphaType},
160 {GrColorType::kBGRA_8888, kPremul_SkAlphaType},
161 {GrColorType::kRGBA_8888_SRGB, kPremul_SkAlphaType},
162 {GrColorType::kRGBA_1010102, kPremul_SkAlphaType},
bsalomone9573312016-01-25 14:33:25 -0800163 };
kkinnunen15302832015-12-01 04:35:26 -0800164
bsalomon9d02b262016-02-01 12:49:30 -0800165 for (int y = 0; y < Y_SIZE; ++y) {
166 for (int x = 0; x < X_SIZE; ++x) {
167 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
168 }
169 }
170
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400171 const SkImageInfo dstInfo = SkImageInfo::Make(X_SIZE, Y_SIZE,
172 kAlpha_8_SkColorType,
173 kPremul_SkAlphaType);
174
bsalomone9573312016-01-25 14:33:25 -0800175 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
176 // once with a render target.
Brian Salomon58389b92018-03-07 13:01:25 -0500177 for (auto info : kInfos) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400178 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
bsalomon9d02b262016-02-01 12:49:30 -0800179 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800180 // Make the alpha channel of the rgba texture come from alphaData.
181 for (int y = 0; y < Y_SIZE; ++y) {
182 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800183 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800184 }
185 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500186
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400187 auto origin = GrRenderable::kYes == renderable ? kBottomLeft_GrSurfaceOrigin
188 : kTopLeft_GrSurfaceOrigin;
Brian Salomone7499c72019-06-24 12:12:36 -0400189 auto proxy = sk_gpu_test::MakeTextureProxyFromData(
Robert Phillips6d344c32020-07-06 10:56:46 -0400190 direct, renderable, origin,
Brian Salomon4eda7102019-10-21 15:04:52 -0400191 {info.fColorType, info.fAlphaType, nullptr, X_SIZE, Y_SIZE}, rgbaData, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400192 if (!proxy) {
bsalomone9573312016-01-25 14:33:25 -0800193 continue;
194 }
kkinnunen15302832015-12-01 04:35:26 -0800195
Robert Phillips6d344c32020-07-06 10:56:46 -0400196 GrSwizzle swizzle = direct->priv().caps()->getReadSwizzle(proxy->backendFormat(),
197 info.fColorType);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500198 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
Robert Phillips6d344c32020-07-06 10:56:46 -0400199 auto sContext = GrSurfaceContext::Make(direct, std::move(view), info.fColorType,
Greg Danielbfa19c42019-12-19 16:41:40 -0500200 kPremul_SkAlphaType, nullptr);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400201
bsalomon9d02b262016-02-01 12:49:30 -0800202 for (auto rowBytes : kRowBytes) {
203 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800204
Ben Wagner7ecc5962016-11-02 17:07:33 -0400205 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -0800206 // Clear so we don't accidentally see values from previous iteration.
robertphillipsdf082c52016-04-19 08:32:40 -0700207 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800208
bsalomon9d02b262016-02-01 12:49:30 -0800209 // read the texture back
Brian Salomon1d435302019-07-01 13:05:28 -0400210 bool result = sContext->readPixels(dstInfo, readback.get(), rowBytes, {0, 0});
Brian Salomon1c80e992018-01-29 09:50:47 -0500211 REPORTER_ASSERT(reporter, result, "8888 readPixels failed");
bsalomon23e56662016-01-14 07:19:47 -0800212
bsalomon9d02b262016-02-01 12:49:30 -0800213 // make sure the original & read back versions match
214 SkString msg;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400215 msg.printf("rt:%d, rb:%d 8888", GrRenderable::kYes == renderable,
216 SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800217 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
Brian Salomon58389b92018-03-07 13:01:25 -0500218 alphaData, msg, info.fColorType);
kkinnunen15302832015-12-01 04:35:26 -0800219 }
220 }
221 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000222}