blob: 344400cca761c2ca222f2da7d8a8a8a33ec91d4f [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;
53 desc.fConfig = kAlpha_8_GrPixelConfig; // it is a single channel texture
bsalomone9573312016-01-25 14:33:25 -080054 desc.fWidth = X_SIZE;
55 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080056
bsalomone9573312016-01-25 14:33:25 -080057 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080058 memset(alphaData, 0, X_SIZE * Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050059
Robert Phillips26c90e02017-03-14 14:39:29 -040060 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -050061 desc,
62 SkBudgeted::kNo,
63 alphaData, 0));
64 if (!proxy) {
robertphillips7e922762016-07-26 11:38:17 -070065 ERRORF(reporter, "Could not create alpha texture.");
66 return;
bsalomone9573312016-01-25 14:33:25 -080067 }
Robert Phillipsc949ce92017-01-19 16:59:04 -050068 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(
Robert Phillips2f493142017-03-02 18:18:38 -050069 std::move(proxy), nullptr));
kkinnunen15302832015-12-01 04:35:26 -080070
robertphillips7e922762016-07-26 11:38:17 -070071 const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050072 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
robertphillips7e922762016-07-26 11:38:17 -070073
bsalomone9573312016-01-25 14:33:25 -080074 // create a distinctive texture
75 for (int y = 0; y < Y_SIZE; ++y) {
76 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080077 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080078 }
79 }
kkinnunen15302832015-12-01 04:35:26 -080080
bsalomon9d02b262016-02-01 12:49:30 -080081 for (auto rowBytes : kRowBytes) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050082
bsalomon9d02b262016-02-01 12:49:30 -080083 // upload the texture (do per-rowbytes iteration because we may overwrite below).
Robert Phillipsc949ce92017-01-19 16:59:04 -050084 bool result = sContext->writePixels(ii, alphaData, 0, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -070085 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 writePixels failed");
bsalomone9573312016-01-25 14:33:25 -080086
bsalomon9d02b262016-02-01 12:49:30 -080087 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
Ben Wagner7ecc5962016-11-02 17:07:33 -040088 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -080089 // clear readback to something non-zero so we can detect readback failures
robertphillipsdf082c52016-04-19 08:32:40 -070090 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080091
bsalomon9d02b262016-02-01 12:49:30 -080092 // read the texture back
Robert Phillipsc949ce92017-01-19 16:59:04 -050093 result = sContext->readPixels(ii, readback.get(), rowBytes, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -070094 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 readPixels failed");
bsalomone9573312016-01-25 14:33:25 -080095
bsalomon9d02b262016-02-01 12:49:30 -080096 // make sure the original & read back versions match
97 SkString msg;
robertphillips7e922762016-07-26 11:38:17 -070098 msg.printf("rb:%d A8", SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -080099 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
100 alphaData, msg);
bsalomone9573312016-01-25 14:33:25 -0800101
robertphillips7e922762016-07-26 11:38:17 -0700102 // Now try writing to a single channel surface (if we could create one).
103 if (surf) {
robertphillips7715e062016-04-22 10:57:16 -0700104 SkCanvas* canvas = surf->getCanvas();
bsalomone9573312016-01-25 14:33:25 -0800105
bsalomon9d02b262016-02-01 12:49:30 -0800106 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -0800107
bsalomon9d02b262016-02-01 12:49:30 -0800108 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800109
bsalomon9d02b262016-02-01 12:49:30 -0800110 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800111
robertphillips7715e062016-04-22 10:57:16 -0700112 canvas->drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800113
robertphillipsdf082c52016-04-19 08:32:40 -0700114 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
robertphillips7e922762016-07-26 11:38:17 -0700115 result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -0700116 REPORTER_ASSERT_MESSAGE(reporter, result, "A8 readPixels after clear failed");
bsalomone9573312016-01-25 14:33:25 -0800117
bsalomon9d02b262016-02-01 12:49:30 -0800118 match = true;
119 for (int y = 0; y < Y_SIZE && match; ++y) {
120 for (int x = 0; x < X_SIZE && match; ++x) {
121 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
122 if (0xFF != rbValue) {
123 ERRORF(reporter,
124 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500125 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800126 match = false;
127 }
bsalomone9573312016-01-25 14:33:25 -0800128 }
129 }
kkinnunen15302832015-12-01 04:35:26 -0800130 }
131 }
132 }
133
bsalomone9573312016-01-25 14:33:25 -0800134 static const GrPixelConfig kRGBAConfigs[] {
135 kRGBA_8888_GrPixelConfig,
136 kBGRA_8888_GrPixelConfig,
137 kSRGBA_8888_GrPixelConfig
138 };
kkinnunen15302832015-12-01 04:35:26 -0800139
bsalomon9d02b262016-02-01 12:49:30 -0800140 for (int y = 0; y < Y_SIZE; ++y) {
141 for (int x = 0; x < X_SIZE; ++x) {
142 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
143 }
144 }
145
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400146 const SkImageInfo dstInfo = SkImageInfo::Make(X_SIZE, Y_SIZE,
147 kAlpha_8_SkColorType,
148 kPremul_SkAlphaType);
149
bsalomone9573312016-01-25 14:33:25 -0800150 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
151 // once with a render target.
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400152 for (auto config : kRGBAConfigs) {
bsalomone9573312016-01-25 14:33:25 -0800153 for (int rt = 0; rt < 2; ++rt) {
154 GrSurfaceDesc desc;
155 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400156 desc.fConfig = config;
bsalomone9573312016-01-25 14:33:25 -0800157 desc.fWidth = X_SIZE;
158 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800159
bsalomon9d02b262016-02-01 12:49:30 -0800160 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800161 // Make the alpha channel of the rgba texture come from alphaData.
162 for (int y = 0; y < Y_SIZE; ++y) {
163 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800164 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800165 }
166 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400167 sk_sp<GrTextureProxy> proxy =
168 GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kNo,
169 rgbaData, 0);
170 if (!proxy) {
bsalomone9573312016-01-25 14:33:25 -0800171 // We always expect to be able to create a RGBA texture
172 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) {
173 ERRORF(reporter, "Failed to create RGBA texture.");
174 }
175 continue;
176 }
kkinnunen15302832015-12-01 04:35:26 -0800177
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400178 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
179 std::move(proxy), nullptr);
180
bsalomon9d02b262016-02-01 12:49:30 -0800181 for (auto rowBytes : kRowBytes) {
182 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800183
Ben Wagner7ecc5962016-11-02 17:07:33 -0400184 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -0800185 // Clear so we don't accidentally see values from previous iteration.
robertphillipsdf082c52016-04-19 08:32:40 -0700186 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800187
bsalomon9d02b262016-02-01 12:49:30 -0800188 // read the texture back
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400189 bool result = sContext->readPixels(dstInfo, readback.get(), rowBytes, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -0700190 REPORTER_ASSERT_MESSAGE(reporter, result, "8888 readPixels failed");
bsalomon23e56662016-01-14 07:19:47 -0800191
bsalomon9d02b262016-02-01 12:49:30 -0800192 // make sure the original & read back versions match
193 SkString msg;
robertphillipsdf082c52016-04-19 08:32:40 -0700194 msg.printf("rt:%d, rb:%d 8888", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800195 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
196 alphaData, msg);
kkinnunen15302832015-12-01 04:35:26 -0800197 }
198 }
199 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000200}
201
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000202#endif