blob: 1f235fd97a5ec740826857e26068c902c28568ac [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"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000014#include "SkGpuDevice.h"
robertphillips@google.com69950682012-04-06 18:06:10 +000015
bsalomonf46a1242015-12-15 12:37:38 -080016// This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT properly.
17static const int X_SIZE = 13;
18static const int Y_SIZE = 13;
robertphillips@google.com69950682012-04-06 18:06:10 +000019
bsalomon9d02b262016-02-01 12:49:30 -080020static void validate_alpha_data(skiatest::Reporter* reporter, int w, int h, const uint8_t* actual,
21 size_t actualRowBytes, const uint8_t* expected, SkString extraMsg) {
22 for (int y = 0; y < h; ++y) {
23 for (int x = 0; x < w; ++x) {
24 uint8_t a = actual[y * actualRowBytes + x];
25 uint8_t e = expected[y * w + x];
26 if (e != a) {
27 ERRORF(reporter,
28 "Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d), %s",
29 e, a, x, y, extraMsg.c_str());
30 return;
31 }
32 }
33 }
34}
robertphillips@google.com69950682012-04-06 18:06:10 +000035
bsalomon758586c2016-04-06 14:02:39 -070036DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) {
bsalomon9d02b262016-02-01 12:49:30 -080037 unsigned char alphaData[X_SIZE * Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000038
robertphillipsdf082c52016-04-19 08:32:40 -070039 static const int kClearValue = 0x2;
40
bsalomone9573312016-01-25 14:33:25 -080041 bool match;
bsalomon9d02b262016-02-01 12:49:30 -080042 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1};
bsalomone9573312016-01-25 14:33:25 -080043 for (int rt = 0; rt < 2; ++rt) {
44 GrSurfaceDesc desc;
45 // let Skia know we will be using this texture as a render target
46 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
47 // it is a single channel texture
48 desc.fConfig = kAlpha_8_GrPixelConfig;
49 desc.fWidth = X_SIZE;
50 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080051
bsalomone9573312016-01-25 14:33:25 -080052 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080053 memset(alphaData, 0, X_SIZE * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080054 SkAutoTUnref<GrTexture> texture(
bsalomonf2f1c172016-04-05 12:59:06 -070055 ctxInfo.fGrContext->textureProvider()->createTexture(desc, SkBudgeted::kNo , alphaData,
56 0));
bsalomone9573312016-01-25 14:33:25 -080057 if (!texture) {
58 if (!rt) {
59 ERRORF(reporter, "Could not create alpha texture.");
60 }
61 continue;
62 }
kkinnunen15302832015-12-01 04:35:26 -080063
bsalomone9573312016-01-25 14:33:25 -080064 // create a distinctive texture
65 for (int y = 0; y < Y_SIZE; ++y) {
66 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080067 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080068 }
69 }
kkinnunen15302832015-12-01 04:35:26 -080070
bsalomon9d02b262016-02-01 12:49:30 -080071 for (auto rowBytes : kRowBytes) {
72 // upload the texture (do per-rowbytes iteration because we may overwrite below).
robertphillipsdf082c52016-04-19 08:32:40 -070073 bool result = texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
74 alphaData, 0);
75 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 writePixels failed");
bsalomone9573312016-01-25 14:33:25 -080076
bsalomon9d02b262016-02-01 12:49:30 -080077 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
78 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
79 // clear readback to something non-zero so we can detect readback failures
robertphillipsdf082c52016-04-19 08:32:40 -070080 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080081
bsalomon9d02b262016-02-01 12:49:30 -080082 // read the texture back
robertphillipsdf082c52016-04-19 08:32:40 -070083 result = texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
84 readback.get(), rowBytes);
85 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 readPixels failed");
bsalomone9573312016-01-25 14:33:25 -080086
bsalomon9d02b262016-02-01 12:49:30 -080087 // make sure the original & read back versions match
88 SkString msg;
robertphillipsdf082c52016-04-19 08:32:40 -070089 msg.printf("rt:%d, rb:%d A8", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -080090 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
91 alphaData, msg);
bsalomone9573312016-01-25 14:33:25 -080092
bsalomon9d02b262016-02-01 12:49:30 -080093 // Now try writing on the single channel texture (if we could create as a RT).
94 if (texture->asRenderTarget()) {
95 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
96 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(
97 texture->asRenderTarget(), &props, SkGpuDevice::kUninit_InitContents));
98 SkCanvas canvas(device);
bsalomone9573312016-01-25 14:33:25 -080099
bsalomon9d02b262016-02-01 12:49:30 -0800100 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -0800101
bsalomon9d02b262016-02-01 12:49:30 -0800102 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800103
bsalomon9d02b262016-02-01 12:49:30 -0800104 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800105
bsalomon9d02b262016-02-01 12:49:30 -0800106 canvas.drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800107
robertphillipsdf082c52016-04-19 08:32:40 -0700108 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
109 result = texture->readPixels(0, 0, desc.fWidth, desc.fHeight,
110 desc.fConfig, readback.get(), rowBytes);
111 REPORTER_ASSERT_MESSAGE(reporter, result, "A8 readPixels after clear failed");
bsalomone9573312016-01-25 14:33:25 -0800112
bsalomon9d02b262016-02-01 12:49:30 -0800113 match = true;
114 for (int y = 0; y < Y_SIZE && match; ++y) {
115 for (int x = 0; x < X_SIZE && match; ++x) {
116 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
117 if (0xFF != rbValue) {
118 ERRORF(reporter,
119 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500120 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800121 match = false;
122 }
bsalomone9573312016-01-25 14:33:25 -0800123 }
124 }
kkinnunen15302832015-12-01 04:35:26 -0800125 }
126 }
127 }
128
bsalomone9573312016-01-25 14:33:25 -0800129 static const GrPixelConfig kRGBAConfigs[] {
130 kRGBA_8888_GrPixelConfig,
131 kBGRA_8888_GrPixelConfig,
132 kSRGBA_8888_GrPixelConfig
133 };
kkinnunen15302832015-12-01 04:35:26 -0800134
bsalomon9d02b262016-02-01 12:49:30 -0800135 for (int y = 0; y < Y_SIZE; ++y) {
136 for (int x = 0; x < X_SIZE; ++x) {
137 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
138 }
139 }
140
bsalomone9573312016-01-25 14:33:25 -0800141 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
142 // once with a render target.
143 for (auto cfg : kRGBAConfigs) {
144 for (int rt = 0; rt < 2; ++rt) {
145 GrSurfaceDesc desc;
146 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
147 desc.fConfig = cfg;
148 desc.fWidth = X_SIZE;
149 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800150
bsalomon9d02b262016-02-01 12:49:30 -0800151 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800152 // Make the alpha channel of the rgba texture come from alphaData.
153 for (int y = 0; y < Y_SIZE; ++y) {
154 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800155 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800156 }
157 }
158 SkAutoTUnref<GrTexture> texture(
bsalomonf2f1c172016-04-05 12:59:06 -0700159 ctxInfo.fGrContext->textureProvider()->createTexture(desc, SkBudgeted::kNo,
160 rgbaData, 0));
bsalomone9573312016-01-25 14:33:25 -0800161 if (!texture) {
162 // We always expect to be able to create a RGBA texture
163 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) {
164 ERRORF(reporter, "Failed to create RGBA texture.");
165 }
166 continue;
167 }
kkinnunen15302832015-12-01 04:35:26 -0800168
bsalomon9d02b262016-02-01 12:49:30 -0800169 for (auto rowBytes : kRowBytes) {
170 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800171
bsalomon9d02b262016-02-01 12:49:30 -0800172 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
173 // Clear so we don't accidentally see values from previous iteration.
robertphillipsdf082c52016-04-19 08:32:40 -0700174 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800175
bsalomon9d02b262016-02-01 12:49:30 -0800176 // read the texture back
robertphillipsdf082c52016-04-19 08:32:40 -0700177 bool result = texture->readPixels(0, 0, desc.fWidth, desc.fHeight,
178 kAlpha_8_GrPixelConfig,
179 readback.get(), rowBytes);
180 REPORTER_ASSERT_MESSAGE(reporter, result, "8888 readPixels failed");
bsalomon23e56662016-01-14 07:19:47 -0800181
bsalomon9d02b262016-02-01 12:49:30 -0800182 // make sure the original & read back versions match
183 SkString msg;
robertphillipsdf082c52016-04-19 08:32:40 -0700184 msg.printf("rt:%d, rb:%d 8888", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800185 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
186 alphaData, msg);
kkinnunen15302832015-12-01 04:35:26 -0800187 }
188 }
189 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000190}
191
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000192#endif