blob: 1be775f8f3b892c6a93d0103b5590f57805fe0fa [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;
bsalomon9d02b262016-02-01 12:49:30 -080040 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1};
bsalomone9573312016-01-25 14:33:25 -080041 for (int rt = 0; rt < 2; ++rt) {
42 GrSurfaceDesc desc;
43 // let Skia know we will be using this texture as a render target
44 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
45 // it is a single channel texture
46 desc.fConfig = kAlpha_8_GrPixelConfig;
47 desc.fWidth = X_SIZE;
48 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -080049
bsalomone9573312016-01-25 14:33:25 -080050 // We are initializing the texture with zeros here
bsalomon9d02b262016-02-01 12:49:30 -080051 memset(alphaData, 0, X_SIZE * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080052 SkAutoTUnref<GrTexture> texture(
bsalomon5ec26ae2016-02-25 08:33:02 -080053 context->textureProvider()->createTexture(desc, SkBudgeted::kNo , alphaData, 0));
bsalomone9573312016-01-25 14:33:25 -080054 if (!texture) {
55 if (!rt) {
56 ERRORF(reporter, "Could not create alpha texture.");
57 }
58 continue;
59 }
kkinnunen15302832015-12-01 04:35:26 -080060
bsalomone9573312016-01-25 14:33:25 -080061 // create a distinctive texture
62 for (int y = 0; y < Y_SIZE; ++y) {
63 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080064 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080065 }
66 }
kkinnunen15302832015-12-01 04:35:26 -080067
bsalomon9d02b262016-02-01 12:49:30 -080068 for (auto rowBytes : kRowBytes) {
69 // upload the texture (do per-rowbytes iteration because we may overwrite below).
70 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
71 alphaData, 0);
bsalomone9573312016-01-25 14:33:25 -080072
bsalomon9d02b262016-02-01 12:49:30 -080073 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
74 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
75 // clear readback to something non-zero so we can detect readback failures
76 memset(readback.get(), 0x1, nonZeroRowBytes * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080077
bsalomon9d02b262016-02-01 12:49:30 -080078 // read the texture back
79 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
80 readback.get(), rowBytes);
bsalomone9573312016-01-25 14:33:25 -080081
bsalomon9d02b262016-02-01 12:49:30 -080082 // make sure the original & read back versions match
83 SkString msg;
Brian Salomon2ea6ff72016-02-02 11:34:46 -050084 msg.printf("rt:%d, rb:%d", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -080085 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
86 alphaData, msg);
bsalomone9573312016-01-25 14:33:25 -080087
bsalomon9d02b262016-02-01 12:49:30 -080088 // Now try writing on the single channel texture (if we could create as a RT).
89 if (texture->asRenderTarget()) {
90 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
91 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(
92 texture->asRenderTarget(), &props, SkGpuDevice::kUninit_InitContents));
93 SkCanvas canvas(device);
bsalomone9573312016-01-25 14:33:25 -080094
bsalomon9d02b262016-02-01 12:49:30 -080095 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -080096
bsalomon9d02b262016-02-01 12:49:30 -080097 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -080098
bsalomon9d02b262016-02-01 12:49:30 -080099 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800100
bsalomon9d02b262016-02-01 12:49:30 -0800101 canvas.drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800102
bsalomon9d02b262016-02-01 12:49:30 -0800103 memset(readback.get(), 0x1, nonZeroRowBytes * Y_SIZE);
104 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, readback.get(),
105 rowBytes);
bsalomone9573312016-01-25 14:33:25 -0800106
bsalomon9d02b262016-02-01 12:49:30 -0800107 match = true;
108 for (int y = 0; y < Y_SIZE && match; ++y) {
109 for (int x = 0; x < X_SIZE && match; ++x) {
110 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
111 if (0xFF != rbValue) {
112 ERRORF(reporter,
113 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500114 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800115 match = false;
116 }
bsalomone9573312016-01-25 14:33:25 -0800117 }
118 }
kkinnunen15302832015-12-01 04:35:26 -0800119 }
120 }
121 }
122
bsalomone9573312016-01-25 14:33:25 -0800123 static const GrPixelConfig kRGBAConfigs[] {
124 kRGBA_8888_GrPixelConfig,
125 kBGRA_8888_GrPixelConfig,
126 kSRGBA_8888_GrPixelConfig
127 };
kkinnunen15302832015-12-01 04:35:26 -0800128
bsalomon9d02b262016-02-01 12:49:30 -0800129 for (int y = 0; y < Y_SIZE; ++y) {
130 for (int x = 0; x < X_SIZE; ++x) {
131 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
132 }
133 }
134
bsalomone9573312016-01-25 14:33:25 -0800135 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
136 // once with a render target.
137 for (auto cfg : kRGBAConfigs) {
138 for (int rt = 0; rt < 2; ++rt) {
139 GrSurfaceDesc desc;
140 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
141 desc.fConfig = cfg;
142 desc.fWidth = X_SIZE;
143 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800144
bsalomon9d02b262016-02-01 12:49:30 -0800145 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800146 // Make the alpha channel of the rgba texture come from alphaData.
147 for (int y = 0; y < Y_SIZE; ++y) {
148 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800149 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800150 }
151 }
152 SkAutoTUnref<GrTexture> texture(
bsalomon5ec26ae2016-02-25 08:33:02 -0800153 context->textureProvider()->createTexture(desc, SkBudgeted::kNo, rgbaData, 0));
bsalomone9573312016-01-25 14:33:25 -0800154 if (!texture) {
155 // We always expect to be able to create a RGBA texture
156 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) {
157 ERRORF(reporter, "Failed to create RGBA texture.");
158 }
159 continue;
160 }
kkinnunen15302832015-12-01 04:35:26 -0800161
bsalomon9d02b262016-02-01 12:49:30 -0800162 for (auto rowBytes : kRowBytes) {
163 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800164
bsalomon9d02b262016-02-01 12:49:30 -0800165 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
166 // Clear so we don't accidentally see values from previous iteration.
167 memset(readback.get(), 0x0, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800168
bsalomon9d02b262016-02-01 12:49:30 -0800169 // read the texture back
170 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixelConfig,
171 readback.get(), rowBytes);
bsalomon23e56662016-01-14 07:19:47 -0800172
bsalomon9d02b262016-02-01 12:49:30 -0800173 // make sure the original & read back versions match
174 SkString msg;
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500175 msg.printf("rt:%d, rb:%d", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800176 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
177 alphaData, msg);
kkinnunen15302832015-12-01 04:35:26 -0800178 }
179 }
180 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000181}
182
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000183#endif