blob: 851cf4e5018074b9b2f3c4b2f91701458a98ac14 [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
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(
bsalomonf2f1c172016-04-05 12:59:06 -070053 ctxInfo.fGrContext->textureProvider()->createTexture(desc, SkBudgeted::kNo , alphaData,
54 0));
bsalomone9573312016-01-25 14:33:25 -080055 if (!texture) {
56 if (!rt) {
57 ERRORF(reporter, "Could not create alpha texture.");
58 }
59 continue;
60 }
kkinnunen15302832015-12-01 04:35:26 -080061
bsalomone9573312016-01-25 14:33:25 -080062 // create a distinctive texture
63 for (int y = 0; y < Y_SIZE; ++y) {
64 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -080065 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
bsalomone9573312016-01-25 14:33:25 -080066 }
67 }
kkinnunen15302832015-12-01 04:35:26 -080068
bsalomon9d02b262016-02-01 12:49:30 -080069 for (auto rowBytes : kRowBytes) {
70 // upload the texture (do per-rowbytes iteration because we may overwrite below).
71 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
72 alphaData, 0);
bsalomone9573312016-01-25 14:33:25 -080073
bsalomon9d02b262016-02-01 12:49:30 -080074 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
75 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
76 // clear readback to something non-zero so we can detect readback failures
77 memset(readback.get(), 0x1, nonZeroRowBytes * Y_SIZE);
bsalomone9573312016-01-25 14:33:25 -080078
bsalomon9d02b262016-02-01 12:49:30 -080079 // read the texture back
80 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
81 readback.get(), rowBytes);
bsalomone9573312016-01-25 14:33:25 -080082
bsalomon9d02b262016-02-01 12:49:30 -080083 // make sure the original & read back versions match
84 SkString msg;
Brian Salomon2ea6ff72016-02-02 11:34:46 -050085 msg.printf("rt:%d, rb:%d", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -080086 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
87 alphaData, msg);
bsalomone9573312016-01-25 14:33:25 -080088
bsalomon9d02b262016-02-01 12:49:30 -080089 // Now try writing on the single channel texture (if we could create as a RT).
90 if (texture->asRenderTarget()) {
91 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
92 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(
93 texture->asRenderTarget(), &props, SkGpuDevice::kUninit_InitContents));
94 SkCanvas canvas(device);
bsalomone9573312016-01-25 14:33:25 -080095
bsalomon9d02b262016-02-01 12:49:30 -080096 SkPaint paint;
bsalomone9573312016-01-25 14:33:25 -080097
bsalomon9d02b262016-02-01 12:49:30 -080098 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
bsalomone9573312016-01-25 14:33:25 -080099
bsalomon9d02b262016-02-01 12:49:30 -0800100 paint.setColor(SK_ColorWHITE);
bsalomone9573312016-01-25 14:33:25 -0800101
bsalomon9d02b262016-02-01 12:49:30 -0800102 canvas.drawRect(rect, paint);
bsalomone9573312016-01-25 14:33:25 -0800103
bsalomon9d02b262016-02-01 12:49:30 -0800104 memset(readback.get(), 0x1, nonZeroRowBytes * Y_SIZE);
105 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, readback.get(),
106 rowBytes);
bsalomone9573312016-01-25 14:33:25 -0800107
bsalomon9d02b262016-02-01 12:49:30 -0800108 match = true;
109 for (int y = 0; y < Y_SIZE && match; ++y) {
110 for (int x = 0; x < X_SIZE && match; ++x) {
111 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x];
112 if (0xFF != rbValue) {
113 ERRORF(reporter,
114 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x"
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500115 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800116 match = false;
117 }
bsalomone9573312016-01-25 14:33:25 -0800118 }
119 }
kkinnunen15302832015-12-01 04:35:26 -0800120 }
121 }
122 }
123
bsalomone9573312016-01-25 14:33:25 -0800124 static const GrPixelConfig kRGBAConfigs[] {
125 kRGBA_8888_GrPixelConfig,
126 kBGRA_8888_GrPixelConfig,
127 kSRGBA_8888_GrPixelConfig
128 };
kkinnunen15302832015-12-01 04:35:26 -0800129
bsalomon9d02b262016-02-01 12:49:30 -0800130 for (int y = 0; y < Y_SIZE; ++y) {
131 for (int x = 0; x < X_SIZE; ++x) {
132 alphaData[y * X_SIZE + x] = y*X_SIZE+x;
133 }
134 }
135
bsalomone9573312016-01-25 14:33:25 -0800136 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
137 // once with a render target.
138 for (auto cfg : kRGBAConfigs) {
139 for (int rt = 0; rt < 2; ++rt) {
140 GrSurfaceDesc desc;
141 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
142 desc.fConfig = cfg;
143 desc.fWidth = X_SIZE;
144 desc.fHeight = Y_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800145
bsalomon9d02b262016-02-01 12:49:30 -0800146 uint32_t rgbaData[X_SIZE * Y_SIZE];
bsalomone9573312016-01-25 14:33:25 -0800147 // Make the alpha channel of the rgba texture come from alphaData.
148 for (int y = 0; y < Y_SIZE; ++y) {
149 for (int x = 0; x < X_SIZE; ++x) {
bsalomon9d02b262016-02-01 12:49:30 -0800150 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaData[y * X_SIZE + x]);
bsalomone9573312016-01-25 14:33:25 -0800151 }
152 }
153 SkAutoTUnref<GrTexture> texture(
bsalomonf2f1c172016-04-05 12:59:06 -0700154 ctxInfo.fGrContext->textureProvider()->createTexture(desc, SkBudgeted::kNo,
155 rgbaData, 0));
bsalomone9573312016-01-25 14:33:25 -0800156 if (!texture) {
157 // We always expect to be able to create a RGBA texture
158 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) {
159 ERRORF(reporter, "Failed to create RGBA texture.");
160 }
161 continue;
162 }
kkinnunen15302832015-12-01 04:35:26 -0800163
bsalomon9d02b262016-02-01 12:49:30 -0800164 for (auto rowBytes : kRowBytes) {
165 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE;
kkinnunen15302832015-12-01 04:35:26 -0800166
bsalomon9d02b262016-02-01 12:49:30 -0800167 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]);
168 // Clear so we don't accidentally see values from previous iteration.
169 memset(readback.get(), 0x0, nonZeroRowBytes * Y_SIZE);
kkinnunen15302832015-12-01 04:35:26 -0800170
bsalomon9d02b262016-02-01 12:49:30 -0800171 // read the texture back
172 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixelConfig,
173 readback.get(), rowBytes);
bsalomon23e56662016-01-14 07:19:47 -0800174
bsalomon9d02b262016-02-01 12:49:30 -0800175 // make sure the original & read back versions match
176 SkString msg;
Brian Salomon2ea6ff72016-02-02 11:34:46 -0500177 msg.printf("rt:%d, rb:%d", rt, SkToU32(rowBytes));
bsalomon9d02b262016-02-01 12:49:30 -0800178 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZeroRowBytes,
179 alphaData, msg);
kkinnunen15302832015-12-01 04:35:26 -0800180 }
181 }
182 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000183}
184
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000185#endif