blob: 2e5a1dc12d8cb2a1b355a4be5368c193de24268b [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"
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"
robertphillips7715e062016-04-22 10:57:16 -070019#include "SkCanvas.h"
20#include "SkSurface.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,
27 size_t actualRowBytes, const uint8_t* expected, SkString extraMsg) {
28 for (int y = 0; y < h; ++y) {
29 for (int x = 0; x < w; ++x) {
30 uint8_t a = actual[y * actualRowBytes + x];
31 uint8_t e = expected[y * w + x];
32 if (e != a) {
33 ERRORF(reporter,
34 "Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d), %s",
35 e, a, x, y, extraMsg.c_str());
36 return;
37 }
38 }
39 }
40}
robertphillips@google.com69950682012-04-06 18:06:10 +000041
egdanielab527a52016-06-28 08:07:26 -070042DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050043 GrContext* context = ctxInfo.grContext();
bsalomon9d02b262016-02-01 12:49:30 -080044 unsigned char alphaData[X_SIZE * Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000045
robertphillipsdf082c52016-04-19 08:32:40 -070046 static const int kClearValue = 0x2;
47
bsalomone9573312016-01-25 14:33:25 -080048 bool match;
bsalomon9d02b262016-02-01 12:49:30 -080049 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1};
robertphillips7e922762016-07-26 11:38:17 -070050 {
bsalomone9573312016-01-25 14:33:25 -080051 GrSurfaceDesc desc;
robertphillips7e922762016-07-26 11:38:17 -070052 desc.fFlags = kNone_GrSurfaceFlags;
Robert Phillipse44ef102017-07-21 15:37:19 -040053 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
robertphillips7e922762016-07-26 11:38:17 -070054 desc.fConfig = kAlpha_8_GrPixelConfig; // it is a single channel texture
bsalomone9573312016-01-25 14:33:25 -080055 desc.fWidth = X_SIZE;
56 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080057
bsalomone9573312016-01-25 14:33:25 -080058 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080059 memset(alphaData, 0, X_SIZE * Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050060
Robert Phillips26c90e02017-03-14 14:39:29 -040061 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -050062 desc,
63 SkBudgeted::kNo,
64 alphaData, 0));
65 if (!proxy) {
robertphillips7e922762016-07-26 11:38:17 -070066 ERRORF(reporter, "Could not create alpha texture.");
67 return;
bsalomone9573312016-01-25 14:33:25 -080068 }
Robert Phillipsc949ce92017-01-19 16:59:04 -050069 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(
Robert Phillips2f493142017-03-02 18:18:38 -050070 std::move(proxy), nullptr));
kkinnunen15302832015-12-01 04:35:26 -080071
robertphillips7e922762016-07-26 11:38:17 -070072 const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050073 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
robertphillips7e922762016-07-26 11:38:17 -070074
bsalomone9573312016-01-25 14:33:25 -080075 // create a distinctive texture
76 for (int y = 0; y < Y_SIZE; ++y) {
77 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080078 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080079 }
80 }
kkinnunen15302832015-12-01 04:35:26 -080081
bsalomon9d02b262016-02-01 12:49:30 -080082 for (auto rowBytes : kRowBytes) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050083
bsalomon9d02b262016-02-01 12:49:30 -080084 // upload the texture (do per-rowbytes iteration because we may overwrite below).
Robert Phillipsc949ce92017-01-19 16:59:04 -050085 bool result = sContext->writePixels(ii, alphaData, 0, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -070086 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 writePixels failed");
bsalomone9573312016-01-25 14:33:25 -080087
bsalomon9d02b262016-02-01 12:49:30 -080088 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
Ben Wagner7ecc5962016-11-02 17:07:33 -040089 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -080090 // clear readback to something non-zero so we can detect readback failures
robertphillipsdf082c52016-04-19 08:32:40 -070091 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080092
bsalomon9d02b262016-02-01 12:49:30 -080093 // read the texture back
Robert Phillipsc949ce92017-01-19 16:59:04 -050094 result = sContext->readPixels(ii, readback.get(), rowBytes, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -070095 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 readPixels failed");
bsalomone9573312016-01-25 14:33:25 -080096
bsalomon9d02b262016-02-01 12:49:30 -080097 // make sure the original & read back versions match
98 SkString msg;
robertphillips7e922762016-07-26 11:38:17 -070099 msg.printf("rb:%d A8", SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800100 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
101 alphaData, msg);
bsalomone9573312016-01-25 14:33:25 -0800102
robertphillips7e922762016-07-26 11:38:17 -0700103 // Now try writing to a single channel surface (if we could create one).
104 if (surf) {
robertphillips7715e062016-04-22 10:57:16 -0700105 SkCanvas* canvas = surf->getCanvas();
bsalomone9573312016-01-25 14:33:25 -0800106
bsalomon9d02b262016-02-01 12:49:30 -0800107 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -0800108
bsalomon9d02b262016-02-01 12:49:30 -0800109 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800110
bsalomon9d02b262016-02-01 12:49:30 -0800111 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800112
robertphillips7715e062016-04-22 10:57:16 -0700113 canvas->drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800114
robertphillipsdf082c52016-04-19 08:32:40 -0700115 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
robertphillips7e922762016-07-26 11:38:17 -0700116 result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -0700117 REPORTER_ASSERT_MESSAGE(reporter, result, "A8 readPixels after clear failed");
bsalomone9573312016-01-25 14:33:25 -0800118
bsalomon9d02b262016-02-01 12:49:30 -0800119 match = true;
120 for (int y = 0; y < Y_SIZE && match; ++y) {
121 for (int x = 0; x < X_SIZE && match; ++x) {
122 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
123 if (0xFF != rbValue) {
124 ERRORF(reporter,
125 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500126 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800127 match = false;
128 }
bsalomone9573312016-01-25 14:33:25 -0800129 }
130 }
kkinnunen15302832015-12-01 04:35:26 -0800131 }
132 }
133 }
134
bsalomone9573312016-01-25 14:33:25 -0800135 static const GrPixelConfig kRGBAConfigs[] {
136 kRGBA_8888_GrPixelConfig,
137 kBGRA_8888_GrPixelConfig,
138 kSRGBA_8888_GrPixelConfig
139 };
kkinnunen15302832015-12-01 04:35:26 -0800140
bsalomon9d02b262016-02-01 12:49:30 -0800141 for (int y = 0; y < Y_SIZE; ++y) {
142 for (int x = 0; x < X_SIZE; ++x) {
143 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
144 }
145 }
146
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400147 const SkImageInfo dstInfo = SkImageInfo::Make(X_SIZE, Y_SIZE,
148 kAlpha_8_SkColorType,
149 kPremul_SkAlphaType);
150
bsalomone9573312016-01-25 14:33:25 -0800151 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
152 // once with a render target.
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400153 for (auto config : kRGBAConfigs) {
bsalomone9573312016-01-25 14:33:25 -0800154 for (int rt = 0; rt < 2; ++rt) {
155 GrSurfaceDesc desc;
156 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Robert Phillipse44ef102017-07-21 15:37:19 -0400157 desc.fOrigin = rt ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400158 desc.fConfig = config;
bsalomone9573312016-01-25 14:33:25 -0800159 desc.fWidth = X_SIZE;
160 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800161
bsalomon9d02b262016-02-01 12:49:30 -0800162 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800163 // Make the alpha channel of the rgba texture come from alphaData.
164 for (int y = 0; y < Y_SIZE; ++y) {
165 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800166 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800167 }
168 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400169 sk_sp<GrTextureProxy> proxy =
170 GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kNo,
171 rgbaData, 0);
172 if (!proxy) {
bsalomone9573312016-01-25 14:33:25 -0800173 // We always expect to be able to create a RGBA texture
174 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) {
175 ERRORF(reporter, "Failed to create RGBA texture.");
176 }
177 continue;
178 }
kkinnunen15302832015-12-01 04:35:26 -0800179
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400180 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
181 std::move(proxy), nullptr);
182
bsalomon9d02b262016-02-01 12:49:30 -0800183 for (auto rowBytes : kRowBytes) {
184 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800185
Ben Wagner7ecc5962016-11-02 17:07:33 -0400186 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -0800187 // Clear so we don't accidentally see values from previous iteration.
robertphillipsdf082c52016-04-19 08:32:40 -0700188 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800189
bsalomon9d02b262016-02-01 12:49:30 -0800190 // read the texture back
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400191 bool result = sContext->readPixels(dstInfo, readback.get(), rowBytes, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -0700192 REPORTER_ASSERT_MESSAGE(reporter, result, "8888 readPixels failed");
bsalomon23e56662016-01-14 07:19:47 -0800193
bsalomon9d02b262016-02-01 12:49:30 -0800194 // make sure the original & read back versions match
195 SkString msg;
robertphillipsdf082c52016-04-19 08:32:40 -0700196 msg.printf("rt:%d, rb:%d 8888", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800197 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
198 alphaData, msg);
kkinnunen15302832015-12-01 04:35:26 -0800199 }
200 }
201 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000202}
203
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000204#endif