blob: 68a3ee098370aa069cf14dd15ce7e54cffbb7af3 [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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000012
kkinnunen15302832015-12-01 04:35:26 -080013#include "GrContext.h"
Robert Phillipsc949ce92017-01-19 16:59:04 -050014#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050015#include "GrProxyProvider.h"
Brian Osman32342f02017-03-04 08:12:46 -050016#include "GrResourceProvider.h"
Robert Phillipsc949ce92017-01-19 16:59:04 -050017#include "GrSurfaceContext.h"
18#include "GrSurfaceProxy.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040019#include "GrTextureProxy.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,
28 size_t actualRowBytes, const uint8_t* expected, SkString extraMsg) {
29 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];
33 if (e != a) {
34 ERRORF(reporter,
35 "Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d), %s",
36 e, a, x, y, extraMsg.c_str());
37 return;
38 }
39 }
40 }
41}
robertphillips@google.com69950682012-04-06 18:06:10 +000042
egdanielab527a52016-06-28 08:07:26 -070043DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050044 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050045 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
46
bsalomon9d02b262016-02-01 12:49:30 -080047 unsigned char alphaData[X_SIZE * Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000048
robertphillipsdf082c52016-04-19 08:32:40 -070049 static const int kClearValue = 0x2;
50
bsalomone9573312016-01-25 14:33:25 -080051 bool match;
bsalomon9d02b262016-02-01 12:49:30 -080052 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1};
robertphillips7e922762016-07-26 11:38:17 -070053 {
bsalomone9573312016-01-25 14:33:25 -080054 GrSurfaceDesc desc;
robertphillips7e922762016-07-26 11:38:17 -070055 desc.fFlags = kNone_GrSurfaceFlags;
Robert Phillipse44ef102017-07-21 15:37:19 -040056 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
robertphillips7e922762016-07-26 11:38:17 -070057 desc.fConfig = kAlpha_8_GrPixelConfig; // it is a single channel texture
bsalomone9573312016-01-25 14:33:25 -080058 desc.fWidth = X_SIZE;
59 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080060
bsalomone9573312016-01-25 14:33:25 -080061 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080062 memset(alphaData, 0, X_SIZE * Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050063
Robert Phillips0bd24dc2018-01-16 08:06:32 -050064 sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(desc, SkBudgeted::kNo,
65 alphaData, 0);
Robert Phillips2f493142017-03-02 18:18:38 -050066 if (!proxy) {
robertphillips7e922762016-07-26 11:38:17 -070067 ERRORF(reporter, "Could not create alpha texture.");
68 return;
bsalomone9573312016-01-25 14:33:25 -080069 }
Robert Phillipsc949ce92017-01-19 16:59:04 -050070 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050071 std::move(proxy)));
kkinnunen15302832015-12-01 04:35:26 -080072
robertphillips7e922762016-07-26 11:38:17 -070073 const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050074 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
robertphillips7e922762016-07-26 11:38:17 -070075
bsalomone9573312016-01-25 14:33:25 -080076 // create a distinctive texture
77 for (int y = 0; y < Y_SIZE; ++y) {
78 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080079 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080080 }
81 }
kkinnunen15302832015-12-01 04:35:26 -080082
bsalomon9d02b262016-02-01 12:49:30 -080083 for (auto rowBytes : kRowBytes) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050084
bsalomon9d02b262016-02-01 12:49:30 -080085 // upload the texture (do per-rowbytes iteration because we may overwrite below).
Robert Phillipsc949ce92017-01-19 16:59:04 -050086 bool result = sContext->writePixels(ii, alphaData, 0, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -050087 REPORTER_ASSERT(reporter, result, "Initial A8 writePixels failed");
bsalomone9573312016-01-25 14:33:25 -080088
bsalomon9d02b262016-02-01 12:49:30 -080089 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
Ben Wagner7ecc5962016-11-02 17:07:33 -040090 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -080091 // clear readback to something non-zero so we can detect readback failures
robertphillipsdf082c52016-04-19 08:32:40 -070092 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080093
bsalomon9d02b262016-02-01 12:49:30 -080094 // read the texture back
Robert Phillipsc949ce92017-01-19 16:59:04 -050095 result = sContext->readPixels(ii, readback.get(), rowBytes, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -050096 REPORTER_ASSERT(reporter, result, "Initial A8 readPixels failed");
bsalomone9573312016-01-25 14:33:25 -080097
bsalomon9d02b262016-02-01 12:49:30 -080098 // make sure the original & read back versions match
99 SkString msg;
robertphillips7e922762016-07-26 11:38:17 -0700100 msg.printf("rb:%d A8", SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800101 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
102 alphaData, msg);
bsalomone9573312016-01-25 14:33:25 -0800103
robertphillips7e922762016-07-26 11:38:17 -0700104 // Now try writing to a single channel surface (if we could create one).
105 if (surf) {
robertphillips7715e062016-04-22 10:57:16 -0700106 SkCanvas* canvas = surf->getCanvas();
bsalomone9573312016-01-25 14:33:25 -0800107
bsalomon9d02b262016-02-01 12:49:30 -0800108 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -0800109
bsalomon9d02b262016-02-01 12:49:30 -0800110 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800111
bsalomon9d02b262016-02-01 12:49:30 -0800112 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800113
robertphillips7715e062016-04-22 10:57:16 -0700114 canvas->drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800115
robertphillipsdf082c52016-04-19 08:32:40 -0700116 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
robertphillips7e922762016-07-26 11:38:17 -0700117 result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -0500118 REPORTER_ASSERT(reporter, result, "A8 readPixels after clear failed");
bsalomone9573312016-01-25 14:33:25 -0800119
bsalomon9d02b262016-02-01 12:49:30 -0800120 match = true;
121 for (int y = 0; y < Y_SIZE && match; ++y) {
122 for (int x = 0; x < X_SIZE && match; ++x) {
123 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
124 if (0xFF != rbValue) {
125 ERRORF(reporter,
126 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500127 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800128 match = false;
129 }
bsalomone9573312016-01-25 14:33:25 -0800130 }
131 }
kkinnunen15302832015-12-01 04:35:26 -0800132 }
133 }
134 }
135
bsalomone9573312016-01-25 14:33:25 -0800136 static const GrPixelConfig kRGBAConfigs[] {
137 kRGBA_8888_GrPixelConfig,
138 kBGRA_8888_GrPixelConfig,
139 kSRGBA_8888_GrPixelConfig
140 };
kkinnunen15302832015-12-01 04:35:26 -0800141
bsalomon9d02b262016-02-01 12:49:30 -0800142 for (int y = 0; y < Y_SIZE; ++y) {
143 for (int x = 0; x < X_SIZE; ++x) {
144 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
145 }
146 }
147
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400148 const SkImageInfo dstInfo = SkImageInfo::Make(X_SIZE, Y_SIZE,
149 kAlpha_8_SkColorType,
150 kPremul_SkAlphaType);
151
bsalomone9573312016-01-25 14:33:25 -0800152 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
153 // once with a render target.
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400154 for (auto config : kRGBAConfigs) {
bsalomone9573312016-01-25 14:33:25 -0800155 for (int rt = 0; rt < 2; ++rt) {
156 GrSurfaceDesc desc;
157 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Robert Phillipse44ef102017-07-21 15:37:19 -0400158 desc.fOrigin = rt ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400159 desc.fConfig = config;
bsalomone9573312016-01-25 14:33:25 -0800160 desc.fWidth = X_SIZE;
161 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800162
bsalomon9d02b262016-02-01 12:49:30 -0800163 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800164 // Make the alpha channel of the rgba texture come from alphaData.
165 for (int y = 0; y < Y_SIZE; ++y) {
166 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800167 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800168 }
169 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500170
171 sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(desc, SkBudgeted::kNo,
172 rgbaData, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400173 if (!proxy) {
bsalomone9573312016-01-25 14:33:25 -0800174 // We always expect to be able to create a RGBA texture
175 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) {
176 ERRORF(reporter, "Failed to create RGBA texture.");
177 }
178 continue;
179 }
kkinnunen15302832015-12-01 04:35:26 -0800180
Brian Salomon366093f2018-02-13 09:25:22 -0500181 sk_sp<SkColorSpace> colorSpace;
182 if (GrPixelConfigIsSRGB(proxy->config())) {
183 colorSpace = SkColorSpace::MakeSRGB();
184 }
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400185 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
Brian Salomon366093f2018-02-13 09:25:22 -0500186 std::move(proxy), std::move(colorSpace));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400187
bsalomon9d02b262016-02-01 12:49:30 -0800188 for (auto rowBytes : kRowBytes) {
189 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800190
Ben Wagner7ecc5962016-11-02 17:07:33 -0400191 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -0800192 // Clear so we don't accidentally see values from previous iteration.
robertphillipsdf082c52016-04-19 08:32:40 -0700193 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800194
bsalomon9d02b262016-02-01 12:49:30 -0800195 // read the texture back
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400196 bool result = sContext->readPixels(dstInfo, readback.get(), rowBytes, 0, 0);
Brian Salomon1c80e992018-01-29 09:50:47 -0500197 REPORTER_ASSERT(reporter, result, "8888 readPixels failed");
bsalomon23e56662016-01-14 07:19:47 -0800198
bsalomon9d02b262016-02-01 12:49:30 -0800199 // make sure the original & read back versions match
200 SkString msg;
robertphillipsdf082c52016-04-19 08:32:40 -0700201 msg.printf("rt:%d, rb:%d 8888", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800202 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
203 alphaData, msg);
kkinnunen15302832015-12-01 04:35:26 -0800204 }
205 }
206 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000207}
208
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000209#endif