blob: ac34b9dc327c14b4bb44efebedc4417c2f996171 [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"
robertphillips7715e062016-04-22 10:57:16 -070018#include "SkCanvas.h"
19#include "SkSurface.h"
robertphillips@google.com69950682012-04-06 18:06:10 +000020
bsalomonf46a1242015-12-15 12:37:38 -080021// This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT properly.
22static const int X_SIZE = 13;
23static const int Y_SIZE = 13;
robertphillips@google.com69950682012-04-06 18:06:10 +000024
bsalomon9d02b262016-02-01 12:49:30 -080025static void validate_alpha_data(skiatest::Reporter* reporter, int w, int h, const uint8_t* actual,
26 size_t actualRowBytes, const uint8_t* expected, SkString extraMsg) {
27 for (int y = 0; y < h; ++y) {
28 for (int x = 0; x < w; ++x) {
29 uint8_t a = actual[y * actualRowBytes + x];
30 uint8_t e = expected[y * w + x];
31 if (e != a) {
32 ERRORF(reporter,
33 "Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d), %s",
34 e, a, x, y, extraMsg.c_str());
35 return;
36 }
37 }
38 }
39}
robertphillips@google.com69950682012-04-06 18:06:10 +000040
egdanielab527a52016-06-28 08:07:26 -070041DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050042 GrContext* context = ctxInfo.grContext();
bsalomon9d02b262016-02-01 12:49:30 -080043 unsigned char alphaData[X_SIZE * Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000044
robertphillipsdf082c52016-04-19 08:32:40 -070045 static const int kClearValue = 0x2;
46
bsalomone9573312016-01-25 14:33:25 -080047 bool match;
bsalomon9d02b262016-02-01 12:49:30 -080048 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1};
robertphillips7e922762016-07-26 11:38:17 -070049 {
bsalomone9573312016-01-25 14:33:25 -080050 GrSurfaceDesc desc;
robertphillips7e922762016-07-26 11:38:17 -070051 desc.fFlags = kNone_GrSurfaceFlags;
52 desc.fConfig = kAlpha_8_GrPixelConfig; // it is a single channel texture
bsalomone9573312016-01-25 14:33:25 -080053 desc.fWidth = X_SIZE;
54 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080055
bsalomone9573312016-01-25 14:33:25 -080056 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080057 memset(alphaData, 0, X_SIZE * Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050058
Robert Phillips26c90e02017-03-14 14:39:29 -040059 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -050060 desc,
61 SkBudgeted::kNo,
62 alphaData, 0));
63 if (!proxy) {
robertphillips7e922762016-07-26 11:38:17 -070064 ERRORF(reporter, "Could not create alpha texture.");
65 return;
bsalomone9573312016-01-25 14:33:25 -080066 }
Robert Phillipsc949ce92017-01-19 16:59:04 -050067 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(
Robert Phillips2f493142017-03-02 18:18:38 -050068 std::move(proxy), nullptr));
kkinnunen15302832015-12-01 04:35:26 -080069
robertphillips7e922762016-07-26 11:38:17 -070070 const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE);
Robert Phillipsc949ce92017-01-19 16:59:04 -050071 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
robertphillips7e922762016-07-26 11:38:17 -070072
bsalomone9573312016-01-25 14:33:25 -080073 // create a distinctive texture
74 for (int y = 0; y < Y_SIZE; ++y) {
75 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080076 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080077 }
78 }
kkinnunen15302832015-12-01 04:35:26 -080079
bsalomon9d02b262016-02-01 12:49:30 -080080 for (auto rowBytes : kRowBytes) {
Robert Phillipsc949ce92017-01-19 16:59:04 -050081
bsalomon9d02b262016-02-01 12:49:30 -080082 // upload the texture (do per-rowbytes iteration because we may overwrite below).
Robert Phillipsc949ce92017-01-19 16:59:04 -050083 bool result = sContext->writePixels(ii, alphaData, 0, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -070084 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 writePixels failed");
bsalomone9573312016-01-25 14:33:25 -080085
bsalomon9d02b262016-02-01 12:49:30 -080086 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
Ben Wagner7ecc5962016-11-02 17:07:33 -040087 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -080088 // clear readback to something non-zero so we can detect readback failures
robertphillipsdf082c52016-04-19 08:32:40 -070089 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080090
bsalomon9d02b262016-02-01 12:49:30 -080091 // read the texture back
Robert Phillipsc949ce92017-01-19 16:59:04 -050092 result = sContext->readPixels(ii, readback.get(), rowBytes, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -070093 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 readPixels failed");
bsalomone9573312016-01-25 14:33:25 -080094
bsalomon9d02b262016-02-01 12:49:30 -080095 // make sure the original & read back versions match
96 SkString msg;
robertphillips7e922762016-07-26 11:38:17 -070097 msg.printf("rb:%d A8", SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -080098 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
99 alphaData, msg);
bsalomone9573312016-01-25 14:33:25 -0800100
robertphillips7e922762016-07-26 11:38:17 -0700101 // Now try writing to a single channel surface (if we could create one).
102 if (surf) {
robertphillips7715e062016-04-22 10:57:16 -0700103 SkCanvas* canvas = surf->getCanvas();
bsalomone9573312016-01-25 14:33:25 -0800104
bsalomon9d02b262016-02-01 12:49:30 -0800105 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -0800106
bsalomon9d02b262016-02-01 12:49:30 -0800107 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800108
bsalomon9d02b262016-02-01 12:49:30 -0800109 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800110
robertphillips7715e062016-04-22 10:57:16 -0700111 canvas->drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800112
robertphillipsdf082c52016-04-19 08:32:40 -0700113 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
robertphillips7e922762016-07-26 11:38:17 -0700114 result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -0700115 REPORTER_ASSERT_MESSAGE(reporter, result, "A8 readPixels after clear failed");
bsalomone9573312016-01-25 14:33:25 -0800116
bsalomon9d02b262016-02-01 12:49:30 -0800117 match = true;
118 for (int y = 0; y < Y_SIZE && match; ++y) {
119 for (int x = 0; x < X_SIZE && match; ++x) {
120 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
121 if (0xFF != rbValue) {
122 ERRORF(reporter,
123 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500124 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800125 match = false;
126 }
bsalomone9573312016-01-25 14:33:25 -0800127 }
128 }
kkinnunen15302832015-12-01 04:35:26 -0800129 }
130 }
131 }
132
bsalomone9573312016-01-25 14:33:25 -0800133 static const GrPixelConfig kRGBAConfigs[] {
134 kRGBA_8888_GrPixelConfig,
135 kBGRA_8888_GrPixelConfig,
136 kSRGBA_8888_GrPixelConfig
137 };
kkinnunen15302832015-12-01 04:35:26 -0800138
bsalomon9d02b262016-02-01 12:49:30 -0800139 for (int y = 0; y < Y_SIZE; ++y) {
140 for (int x = 0; x < X_SIZE; ++x) {
141 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
142 }
143 }
144
bsalomone9573312016-01-25 14:33:25 -0800145 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
146 // once with a render target.
147 for (auto cfg : kRGBAConfigs) {
148 for (int rt = 0; rt < 2; ++rt) {
149 GrSurfaceDesc desc;
150 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
151 desc.fConfig = cfg;
152 desc.fWidth = X_SIZE;
153 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800154
bsalomon9d02b262016-02-01 12:49:30 -0800155 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800156 // Make the alpha channel of the rgba texture come from alphaData.
157 for (int y = 0; y < Y_SIZE; ++y) {
158 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800159 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800160 }
161 }
Hal Canary342b7ac2016-11-04 11:49:42 -0400162 sk_sp<GrTexture> texture(
Brian Osman32342f02017-03-04 08:12:46 -0500163 context->resourceProvider()->createTexture(desc, SkBudgeted::kNo, rgbaData, 0));
bsalomone9573312016-01-25 14:33:25 -0800164 if (!texture) {
165 // We always expect to be able to create a RGBA texture
166 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) {
167 ERRORF(reporter, "Failed to create RGBA texture.");
168 }
169 continue;
170 }
kkinnunen15302832015-12-01 04:35:26 -0800171
bsalomon9d02b262016-02-01 12:49:30 -0800172 for (auto rowBytes : kRowBytes) {
173 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800174
Ben Wagner7ecc5962016-11-02 17:07:33 -0400175 std::unique_ptr<uint8_t[]> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
bsalomon9d02b262016-02-01 12:49:30 -0800176 // Clear so we don't accidentally see values from previous iteration.
robertphillipsdf082c52016-04-19 08:32:40 -0700177 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800178
bsalomon9d02b262016-02-01 12:49:30 -0800179 // read the texture back
robertphillipsdf082c52016-04-19 08:32:40 -0700180 bool result = texture->readPixels(0, 0, desc.fWidth, desc.fHeight,
181 kAlpha_8_GrPixelConfig,
182 readback.get(), rowBytes);
183 REPORTER_ASSERT_MESSAGE(reporter, result, "8888 readPixels failed");
bsalomon23e56662016-01-14 07:19:47 -0800184
bsalomon9d02b262016-02-01 12:49:30 -0800185 // make sure the original & read back versions match
186 SkString msg;
robertphillipsdf082c52016-04-19 08:32:40 -0700187 msg.printf("rt:%d, rb:%d 8888", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800188 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
189 alphaData, msg);
kkinnunen15302832015-12-01 04:35:26 -0800190 }
191 }
192 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000193}
194
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000195#endif