blob: e5240666aaeb747a0c20374471b98d31f270c10a [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"
robertphillips7715e062016-04-22 10:57:16 -070014#include "SkCanvas.h"
15#include "SkSurface.h"
robertphillips@google.com69950682012-04-06 18:06:10 +000016
bsalomonf46a1242015-12-15 12:37:38 -080017// This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT properly.
18static const int X_SIZE = 13;
19static const int Y_SIZE = 13;
robertphillips@google.com69950682012-04-06 18:06:10 +000020
bsalomon9d02b262016-02-01 12:49:30 -080021static void validate_alpha_data(skiatest::Reporter* reporter, int w, int h, const uint8_t* actual,
22 size_t actualRowBytes, const uint8_t* expected, SkString extraMsg) {
23 for (int y = 0; y < h; ++y) {
24 for (int x = 0; x < w; ++x) {
25 uint8_t a = actual[y * actualRowBytes + x];
26 uint8_t e = expected[y * w + x];
27 if (e != a) {
28 ERRORF(reporter,
29 "Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d), %s",
30 e, a, x, y, extraMsg.c_str());
31 return;
32 }
33 }
34 }
35}
robertphillips@google.com69950682012-04-06 18:06:10 +000036
egdanielab527a52016-06-28 08:07:26 -070037DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) {
bsalomon9d02b262016-02-01 12:49:30 -080038 unsigned char alphaData[X_SIZE * Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000039
robertphillipsdf082c52016-04-19 08:32:40 -070040 static const int kClearValue = 0x2;
41
bsalomone9573312016-01-25 14:33:25 -080042 bool match;
bsalomon9d02b262016-02-01 12:49:30 -080043 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1};
robertphillips7e922762016-07-26 11:38:17 -070044 {
bsalomone9573312016-01-25 14:33:25 -080045 GrSurfaceDesc desc;
robertphillips7e922762016-07-26 11:38:17 -070046 desc.fFlags = kNone_GrSurfaceFlags;
47 desc.fConfig = kAlpha_8_GrPixelConfig; // it is a single channel texture
bsalomone9573312016-01-25 14:33:25 -080048 desc.fWidth = X_SIZE;
49 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080050
bsalomone9573312016-01-25 14:33:25 -080051 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080052 memset(alphaData, 0, X_SIZE * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080053 SkAutoTUnref<GrTexture> texture(
bsalomon8b7451a2016-05-11 06:33:06 -070054 ctxInfo.grContext()->textureProvider()->createTexture(desc, SkBudgeted::kNo, alphaData,
robertphillips7e922762016-07-26 11:38:17 -070055 0));
bsalomone9573312016-01-25 14:33:25 -080056 if (!texture) {
robertphillips7e922762016-07-26 11:38:17 -070057 ERRORF(reporter, "Could not create alpha texture.");
58 return;
bsalomone9573312016-01-25 14:33:25 -080059 }
kkinnunen15302832015-12-01 04:35:26 -080060
robertphillips7e922762016-07-26 11:38:17 -070061 const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE);
62 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(ctxInfo.grContext(),
63 SkBudgeted::kNo, ii));
64
bsalomone9573312016-01-25 14:33:25 -080065 // create a distinctive texture
66 for (int y = 0; y < Y_SIZE; ++y) {
67 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080068 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080069 }
70 }
kkinnunen15302832015-12-01 04:35:26 -080071
bsalomon9d02b262016-02-01 12:49:30 -080072 for (auto rowBytes : kRowBytes) {
73 // upload the texture (do per-rowbytes iteration because we may overwrite below).
robertphillipsdf082c52016-04-19 08:32:40 -070074 bool result = texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
75 alphaData, 0);
76 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 writePixels failed");
bsalomone9573312016-01-25 14:33:25 -080077
bsalomon9d02b262016-02-01 12:49:30 -080078 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
79 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
80 // clear readback to something non-zero so we can detect readback failures
robertphillipsdf082c52016-04-19 08:32:40 -070081 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080082
bsalomon9d02b262016-02-01 12:49:30 -080083 // read the texture back
robertphillipsdf082c52016-04-19 08:32:40 -070084 result = texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
85 readback.get(), rowBytes);
86 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 readPixels failed");
bsalomone9573312016-01-25 14:33:25 -080087
bsalomon9d02b262016-02-01 12:49:30 -080088 // make sure the original & read back versions match
89 SkString msg;
robertphillips7e922762016-07-26 11:38:17 -070090 msg.printf("rb:%d A8", SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -080091 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
92 alphaData, msg);
bsalomone9573312016-01-25 14:33:25 -080093
robertphillips7e922762016-07-26 11:38:17 -070094 // Now try writing to a single channel surface (if we could create one).
95 if (surf) {
robertphillips7715e062016-04-22 10:57:16 -070096 SkCanvas* canvas = surf->getCanvas();
bsalomone9573312016-01-25 14:33:25 -080097
bsalomon9d02b262016-02-01 12:49:30 -080098 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -080099
bsalomon9d02b262016-02-01 12:49:30 -0800100 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800101
bsalomon9d02b262016-02-01 12:49:30 -0800102 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800103
robertphillips7715e062016-04-22 10:57:16 -0700104 canvas->drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800105
robertphillipsdf082c52016-04-19 08:32:40 -0700106 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
robertphillips7e922762016-07-26 11:38:17 -0700107 result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0, 0);
robertphillipsdf082c52016-04-19 08:32:40 -0700108 REPORTER_ASSERT_MESSAGE(reporter, result, "A8 readPixels after clear failed");
bsalomone9573312016-01-25 14:33:25 -0800109
bsalomon9d02b262016-02-01 12:49:30 -0800110 match = true;
111 for (int y = 0; y < Y_SIZE && match; ++y) {
112 for (int x = 0; x < X_SIZE && match; ++x) {
113 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
114 if (0xFF != rbValue) {
115 ERRORF(reporter,
116 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500117 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800118 match = false;
119 }
bsalomone9573312016-01-25 14:33:25 -0800120 }
121 }
kkinnunen15302832015-12-01 04:35:26 -0800122 }
123 }
124 }
125
bsalomone9573312016-01-25 14:33:25 -0800126 static const GrPixelConfig kRGBAConfigs[] {
127 kRGBA_8888_GrPixelConfig,
128 kBGRA_8888_GrPixelConfig,
129 kSRGBA_8888_GrPixelConfig
130 };
kkinnunen15302832015-12-01 04:35:26 -0800131
bsalomon9d02b262016-02-01 12:49:30 -0800132 for (int y = 0; y < Y_SIZE; ++y) {
133 for (int x = 0; x < X_SIZE; ++x) {
134 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
135 }
136 }
137
bsalomone9573312016-01-25 14:33:25 -0800138 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
139 // once with a render target.
140 for (auto cfg : kRGBAConfigs) {
141 for (int rt = 0; rt < 2; ++rt) {
142 GrSurfaceDesc desc;
143 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
144 desc.fConfig = cfg;
145 desc.fWidth = X_SIZE;
146 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800147
bsalomon9d02b262016-02-01 12:49:30 -0800148 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800149 // Make the alpha channel of the rgba texture come from alphaData.
150 for (int y = 0; y < Y_SIZE; ++y) {
151 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800152 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800153 }
154 }
155 SkAutoTUnref<GrTexture> texture(
bsalomon8b7451a2016-05-11 06:33:06 -0700156 ctxInfo.grContext()->textureProvider()->createTexture(desc, SkBudgeted::kNo,
157 rgbaData, 0));
bsalomone9573312016-01-25 14:33:25 -0800158 if (!texture) {
159 // We always expect to be able to create a RGBA texture
160 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) {
161 ERRORF(reporter, "Failed to create RGBA texture.");
162 }
163 continue;
164 }
kkinnunen15302832015-12-01 04:35:26 -0800165
bsalomon9d02b262016-02-01 12:49:30 -0800166 for (auto rowBytes : kRowBytes) {
167 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800168
bsalomon9d02b262016-02-01 12:49:30 -0800169 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
170 // Clear so we don't accidentally see values from previous iteration.
robertphillipsdf082c52016-04-19 08:32:40 -0700171 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800172
bsalomon9d02b262016-02-01 12:49:30 -0800173 // read the texture back
robertphillipsdf082c52016-04-19 08:32:40 -0700174 bool result = texture->readPixels(0, 0, desc.fWidth, desc.fHeight,
175 kAlpha_8_GrPixelConfig,
176 readback.get(), rowBytes);
177 REPORTER_ASSERT_MESSAGE(reporter, result, "8888 readPixels failed");
bsalomon23e56662016-01-14 07:19:47 -0800178
bsalomon9d02b262016-02-01 12:49:30 -0800179 // make sure the original & read back versions match
180 SkString msg;
robertphillipsdf082c52016-04-19 08:32:40 -0700181 msg.printf("rt:%d, rb:%d 8888", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800182 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
183 alphaData, msg);
kkinnunen15302832015-12-01 04:35:26 -0800184 }
185 }
186 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000187}
188
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000189#endif