blob: 36502c744efd73d3c2d44392828898a5386a67cf [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
bsalomon9d02b262016-02-01 12:49:30 -080036DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, context) {
37 unsigned char alphaData[X_SIZE * Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000038
bsalomone9573312016-01-25 14:33:25 -080039 bool match;
Brian Salomonbe0fcb12016-02-01 18:41:58 -050040#ifdef SK_BUILD_FOR_WIN // TODO: Figure out why this breaks on Windows.
41 static const size_t kRowBytes[] = {0, X_SIZE};
42#else
bsalomon9d02b262016-02-01 12:49:30 -080043 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1};
Brian Salomonbe0fcb12016-02-01 18:41:58 -050044#endif
bsalomone9573312016-01-25 14:33:25 -080045 for (int rt = 0; rt < 2; ++rt) {
46 GrSurfaceDesc desc;
47 // let Skia know we will be using this texture as a render target
48 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
49 // it is a single channel texture
50 desc.fConfig = kAlpha_8_GrPixelConfig;
51 desc.fWidth = X_SIZE;
52 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080053
bsalomone9573312016-01-25 14:33:25 -080054 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080055 memset(alphaData, 0, X_SIZE * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080056 SkAutoTUnref<GrTexture> texture(
57 context->textureProvider()->createTexture(desc, false, alphaData, 0));
58 if (!texture) {
59 if (!rt) {
60 ERRORF(reporter, "Could not create alpha texture.");
61 }
62 continue;
63 }
kkinnunen15302832015-12-01 04:35:26 -080064
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).
74 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
75 alphaData, 0);
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
80 memset(readback.get(), 0x1, nonZeroRowBytes * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080081
bsalomon9d02b262016-02-01 12:49:30 -080082 // read the texture back
83 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
84 readback.get(), rowBytes);
bsalomone9573312016-01-25 14:33:25 -080085
bsalomon9d02b262016-02-01 12:49:30 -080086 // make sure the original & read back versions match
87 SkString msg;
88 msg.printf("rt:%d, rb:%zd", rt, rowBytes);
89 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
90 alphaData, msg);
bsalomone9573312016-01-25 14:33:25 -080091
bsalomon9d02b262016-02-01 12:49:30 -080092 // Now try writing on the single channel texture (if we could create as a RT).
93 if (texture->asRenderTarget()) {
94 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
95 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(
96 texture->asRenderTarget(), &props, SkGpuDevice::kUninit_InitContents));
97 SkCanvas canvas(device);
bsalomone9573312016-01-25 14:33:25 -080098
bsalomon9d02b262016-02-01 12:49:30 -080099 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -0800100
bsalomon9d02b262016-02-01 12:49:30 -0800101 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -0800102
bsalomon9d02b262016-02-01 12:49:30 -0800103 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800104
bsalomon9d02b262016-02-01 12:49:30 -0800105 canvas.drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800106
bsalomon9d02b262016-02-01 12:49:30 -0800107 memset(readback.get(), 0x1, nonZeroRowBytes * Y_SIZE);
108 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, readback.get(),
109 rowBytes);
bsalomone9573312016-01-25 14:33:25 -0800110
bsalomon9d02b262016-02-01 12:49:30 -0800111 match = true;
112 for (int y = 0; y < Y_SIZE && match; ++y) {
113 for (int x = 0; x < X_SIZE && match; ++x) {
114 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
115 if (0xFF != rbValue) {
116 ERRORF(reporter,
117 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
118 " at (%d,%d), rb:%zd", rbValue, x, y, rowBytes);
119 match = false;
120 }
bsalomone9573312016-01-25 14:33:25 -0800121 }
122 }
kkinnunen15302832015-12-01 04:35:26 -0800123 }
124 }
125 }
126
bsalomone9573312016-01-25 14:33:25 -0800127 static const GrPixelConfig kRGBAConfigs[] {
128 kRGBA_8888_GrPixelConfig,
129 kBGRA_8888_GrPixelConfig,
130 kSRGBA_8888_GrPixelConfig
131 };
kkinnunen15302832015-12-01 04:35:26 -0800132
bsalomon9d02b262016-02-01 12:49:30 -0800133 for (int y = 0; y < Y_SIZE; ++y) {
134 for (int x = 0; x < X_SIZE; ++x) {
135 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
136 }
137 }
138
bsalomone9573312016-01-25 14:33:25 -0800139 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
140 // once with a render target.
141 for (auto cfg : kRGBAConfigs) {
142 for (int rt = 0; rt < 2; ++rt) {
143 GrSurfaceDesc desc;
144 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
145 desc.fConfig = cfg;
146 desc.fWidth = X_SIZE;
147 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800148
bsalomon9d02b262016-02-01 12:49:30 -0800149 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800150 // Make the alpha channel of the rgba texture come from alphaData.
151 for (int y = 0; y < Y_SIZE; ++y) {
152 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800153 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800154 }
155 }
156 SkAutoTUnref<GrTexture> texture(
157 context->textureProvider()->createTexture(desc, false, rgbaData, 0));
158 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.
171 memset(readback.get(), 0x0, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800172
bsalomon9d02b262016-02-01 12:49:30 -0800173 // read the texture back
174 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixelConfig,
175 readback.get(), rowBytes);
bsalomon23e56662016-01-14 07:19:47 -0800176
bsalomon9d02b262016-02-01 12:49:30 -0800177 // make sure the original & read back versions match
178 SkString msg;
179 msg.printf("rt:%d, rb:%zd", rt, rowBytes);
180 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
181 alphaData, msg);
kkinnunen15302832015-12-01 04:35:26 -0800182 }
183 }
184 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000185}
186
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000187#endif