blob: b88fc0d1e38adafc6e9ecca5f2bac9f42b2fa89a [file] [log] [blame]
Brian Salomonf84dfd62020-12-29 15:09:33 -05001/*
2 * Copyright 2020 Google LLC.
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
8#include "include/core/SkCanvas.h"
9#include "include/core/SkImage.h"
10#include "include/core/SkSurface.h"
11#include "include/effects/SkGradientShader.h"
12#include "include/gpu/GrDirectContext.h"
13#include "src/core/SkAutoPixmapStorage.h"
14#include "src/core/SkConvertPixels.h"
15#include "src/gpu/GrDirectContextPriv.h"
16#include "src/gpu/GrImageInfo.h"
17#include "src/gpu/GrSurfaceContext.h"
18#include "tests/Test.h"
19#include "tests/TestUtils.h"
20#include "tools/ToolUtils.h"
Brian Salomonbacbb922021-01-21 19:48:00 -050021#include "tools/gpu/BackendSurfaceFactory.h"
Brian Salomonf84dfd62020-12-29 15:09:33 -050022#include "tools/gpu/BackendTextureImageFactory.h"
23#include "tools/gpu/GrContextFactory.h"
24#include "tools/gpu/ProxyUtils.h"
25
26#include <initializer_list>
27
28static constexpr int min_rgb_channel_bits(SkColorType ct) {
29 switch (ct) {
30 case kUnknown_SkColorType: return 0;
31 case kAlpha_8_SkColorType: return 0;
32 case kA16_unorm_SkColorType: return 0;
33 case kA16_float_SkColorType: return 0;
34 case kRGB_565_SkColorType: return 5;
35 case kARGB_4444_SkColorType: return 4;
36 case kR8G8_unorm_SkColorType: return 8;
37 case kR16G16_unorm_SkColorType: return 16;
38 case kR16G16_float_SkColorType: return 16;
39 case kRGBA_8888_SkColorType: return 8;
40 case kRGB_888x_SkColorType: return 8;
41 case kBGRA_8888_SkColorType: return 8;
42 case kRGBA_1010102_SkColorType: return 10;
43 case kRGB_101010x_SkColorType: return 10;
44 case kBGRA_1010102_SkColorType: return 10;
45 case kBGR_101010x_SkColorType: return 10;
46 case kGray_8_SkColorType: return 8; // counting gray as "rgb"
47 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
48 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
49 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
50 case kR16G16B16A16_unorm_SkColorType: return 16;
51 }
52 SkUNREACHABLE;
53}
54
55static constexpr int alpha_channel_bits(SkColorType ct) {
56 switch (ct) {
57 case kUnknown_SkColorType: return 0;
58 case kAlpha_8_SkColorType: return 8;
59 case kA16_unorm_SkColorType: return 16;
60 case kA16_float_SkColorType: return 16;
61 case kRGB_565_SkColorType: return 0;
62 case kARGB_4444_SkColorType: return 4;
63 case kR8G8_unorm_SkColorType: return 0;
64 case kR16G16_unorm_SkColorType: return 0;
65 case kR16G16_float_SkColorType: return 0;
66 case kRGBA_8888_SkColorType: return 8;
67 case kRGB_888x_SkColorType: return 0;
68 case kBGRA_8888_SkColorType: return 8;
69 case kRGBA_1010102_SkColorType: return 2;
70 case kRGB_101010x_SkColorType: return 0;
71 case kBGRA_1010102_SkColorType: return 2;
72 case kBGR_101010x_SkColorType: return 0;
73 case kGray_8_SkColorType: return 0;
74 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
75 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
76 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
77 case kR16G16B16A16_unorm_SkColorType: return 16;
78 }
79 SkUNREACHABLE;
80}
81
82namespace {
83
84struct GpuReadPixelTestRules {
85 // Test unpremul sources? We could omit this and detect that creating the source of the read
86 // failed but having it lets us skip generating reference color data.
87 bool fAllowUnpremulSrc = true;
Brian Salomonf84dfd62020-12-29 15:09:33 -050088 // Are reads that are overlapping but not contained by the src bounds expected to succeed?
89 bool fUncontainedRectSucceeds = true;
90};
91
92// Makes a src populated with the pixmap. The src should get its image info (or equivalent) from
93// the pixmap.
94template <typename T> using GpuSrcFactory = T(SkPixmap&);
95
Brian Salomon674d2162021-01-06 14:23:51 +000096enum class GpuReadResult {
Brian Salomonf84dfd62020-12-29 15:09:33 -050097 kFail,
98 kSuccess,
99 kExcusedFailure,
100};
101
102// Does a read from the T into the pixmap.
103template <typename T>
Brian Salomon674d2162021-01-06 14:23:51 +0000104using GpuReadSrcFn = GpuReadResult(const T&, const SkIVector& offset, const SkPixmap&);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500105
106} // anonymous namespace
107
108template <typename T>
109static void gpu_read_pixels_test_driver(skiatest::Reporter* reporter,
110 const GpuReadPixelTestRules& rules,
111 const std::function<GpuSrcFactory<T>>& srcFactory,
Brian Salomonbacbb922021-01-21 19:48:00 -0500112 const std::function<GpuReadSrcFn<T>>& read,
113 SkString label) {
114 if (!label.isEmpty()) {
115 // Add space for printing.
116 label.append(" ");
117 }
Brian Salomonf84dfd62020-12-29 15:09:33 -0500118 // Separate this out just to give it some line width to breathe. Note 'srcPixels' should have
119 // the same image info as src. We will do a converting readPixels() on it to get the data
120 // to compare with the results of 'read'.
121 auto runTest = [&](const T& src,
122 const SkPixmap& srcPixels,
123 const SkImageInfo& readInfo,
Brian Salomon674d2162021-01-06 14:23:51 +0000124 const SkIVector& offset) {
Brian Salomonf84dfd62020-12-29 15:09:33 -0500125 const bool csConversion =
126 !SkColorSpace::Equals(readInfo.colorSpace(), srcPixels.info().colorSpace());
127 const auto readCT = readInfo.colorType();
128 const auto readAT = readInfo.alphaType();
129 const auto srcCT = srcPixels.info().colorType();
130 const auto srcAT = srcPixels.info().alphaType();
131 const auto rect = SkIRect::MakeWH(readInfo.width(), readInfo.height()).makeOffset(offset);
132 const auto surfBounds = SkIRect::MakeWH(srcPixels.width(), srcPixels.height());
133 const size_t readBpp = SkColorTypeBytesPerPixel(readCT);
134
135 // Make the row bytes in the dst be loose for extra stress.
136 const size_t dstRB = readBpp * readInfo.width() + 10 * readBpp;
137 // This will make the last row tight.
138 const size_t dstSize = readInfo.computeByteSize(dstRB);
139 std::unique_ptr<char[]> dstData(new char[dstSize]);
140 SkPixmap dstPixels(readInfo, dstData.get(), dstRB);
141 // Initialize with an arbitrary value for each byte. Later we will check that only the
142 // correct part of the destination gets overwritten by 'read'.
143 static constexpr auto kInitialByte = static_cast<char>(0x1B);
144 std::fill_n(static_cast<char*>(dstPixels.writable_addr()),
145 dstPixels.computeByteSize(),
146 kInitialByte);
147
Brian Salomon674d2162021-01-06 14:23:51 +0000148 const GpuReadResult result = read(src, offset, dstPixels);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500149
150 if (!SkIRect::Intersects(rect, surfBounds)) {
Brian Salomon674d2162021-01-06 14:23:51 +0000151 REPORTER_ASSERT(reporter, result != GpuReadResult::kSuccess);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500152 } else if (readCT == kUnknown_SkColorType) {
Brian Salomon674d2162021-01-06 14:23:51 +0000153 REPORTER_ASSERT(reporter, result != GpuReadResult::kSuccess);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500154 } else if ((readAT == kUnknown_SkAlphaType) != (srcAT == kUnknown_SkAlphaType)) {
Brian Salomon674d2162021-01-06 14:23:51 +0000155 REPORTER_ASSERT(reporter, result != GpuReadResult::kSuccess);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500156 } else if (!rules.fUncontainedRectSucceeds && !surfBounds.contains(rect)) {
Brian Salomon674d2162021-01-06 14:23:51 +0000157 REPORTER_ASSERT(reporter, result != GpuReadResult::kSuccess);
Brian Salomon674d2162021-01-06 14:23:51 +0000158 } else if (result == GpuReadResult::kFail) {
Brian Salomonf84dfd62020-12-29 15:09:33 -0500159 // TODO: Support RGB/BGR 101010x, BGRA 1010102 on the GPU.
160 if (SkColorTypeToGrColorType(readCT) != GrColorType::kUnknown) {
161 ERRORF(reporter,
Brian Salomonbacbb922021-01-21 19:48:00 -0500162 "Read failed. %sSrc CT: %s, Src AT: %s Read CT: %s, Read AT: %s, "
Brian Salomonf84dfd62020-12-29 15:09:33 -0500163 "Rect [%d, %d, %d, %d], CS conversion: %d\n",
Brian Salomonbacbb922021-01-21 19:48:00 -0500164 label.c_str(),
Brian Salomonf84dfd62020-12-29 15:09:33 -0500165 ToolUtils::colortype_name(srcCT), ToolUtils::alphatype_name(srcAT),
166 ToolUtils::colortype_name(readCT), ToolUtils::alphatype_name(readAT),
167 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion);
168 }
169 return result;
170 }
171
172 bool guardOk = true;
173 auto guardCheck = [](char x) { return x == kInitialByte; };
174
175 // Considering the rect we tried to read and the surface bounds figure out which pixels in
176 // both src and dst space should actually have been read and written.
177 SkIRect srcReadRect;
Brian Salomon674d2162021-01-06 14:23:51 +0000178 if (result == GpuReadResult::kSuccess && srcReadRect.intersect(surfBounds, rect)) {
Brian Salomonf84dfd62020-12-29 15:09:33 -0500179 SkIRect dstWriteRect = srcReadRect.makeOffset(-rect.fLeft, -rect.fTop);
180
181 const bool lumConversion =
182 !(SkColorTypeChannelFlags(srcCT) & kGray_SkColorChannelFlag) &&
183 (SkColorTypeChannelFlags(readCT) & kGray_SkColorChannelFlag);
184 // A CS or luminance conversion allows a 3 value difference and otherwise a 2 value
185 // difference. Note that sometimes read back on GPU can be lossy even when there no
186 // conversion at all because GPU->CPU read may go to a lower bit depth format and then
187 // be promoted back to the original type. For example, GL ES cannot read to 1010102, so
188 // we go through 8888.
189 float numer = (lumConversion || csConversion) ? 3.f : 2.f;
190 // Allow some extra tolerance if unpremuling.
191 if (srcAT == kPremul_SkAlphaType && readAT == kUnpremul_SkAlphaType) {
192 numer += 1;
193 }
194 int rgbBits = std::min({min_rgb_channel_bits(readCT), min_rgb_channel_bits(srcCT), 8});
195 float tol = numer / (1 << rgbBits);
196 float alphaTol = 0;
197 if (readAT != kOpaque_SkAlphaType && srcAT != kOpaque_SkAlphaType) {
198 // Alpha can also get squashed down to 8 bits going through an intermediate
199 // color format.
200 const int alphaBits = std::min({alpha_channel_bits(readCT),
201 alpha_channel_bits(srcCT),
202 8});
203 alphaTol = 2.f / (1 << alphaBits);
204 }
205
206 const float tols[4] = {tol, tol, tol, alphaTol};
207 auto error = std::function<ComparePixmapsErrorReporter>([&](int x, int y,
208 const float diffs[4]) {
209 SkASSERT(x >= 0 && y >= 0);
210 ERRORF(reporter,
Brian Salomonbacbb922021-01-21 19:48:00 -0500211 "%sSrc CT: %s, Src AT: %s, Read CT: %s, Read AT: %s, Rect [%d, %d, %d, %d]"
Brian Salomonf84dfd62020-12-29 15:09:33 -0500212 ", CS conversion: %d\n"
213 "Error at %d, %d. Diff in floats: (%f, %f, %f %f)",
Brian Salomonbacbb922021-01-21 19:48:00 -0500214 label.c_str(),
Brian Salomonf84dfd62020-12-29 15:09:33 -0500215 ToolUtils::colortype_name(srcCT), ToolUtils::alphatype_name(srcAT),
216 ToolUtils::colortype_name(readCT), ToolUtils::alphatype_name(readAT),
217 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion, x, y,
218 diffs[0], diffs[1], diffs[2], diffs[3]);
219 });
220 SkAutoPixmapStorage ref;
221 SkImageInfo refInfo = readInfo.makeDimensions(dstWriteRect.size());
222 ref.alloc(refInfo);
223 if (readAT == kUnknown_SkAlphaType) {
224 // Do a spoofed read where src and dst alpha type are both kUnpremul. This will
225 // allow SkPixmap readPixels to succeed and won't do any alpha type conversion.
226 SkPixmap unpremulRef(refInfo.makeAlphaType(kUnpremul_SkAlphaType),
227 ref.addr(),
228 ref.rowBytes());
229 SkPixmap unpremulSRc(srcPixels.info().makeAlphaType(kUnpremul_SkAlphaType),
230 srcPixels.addr(),
231 srcPixels.rowBytes());
232
233 unpremulSRc.readPixels(unpremulRef, srcReadRect.x(), srcReadRect.y());
234 } else {
235 srcPixels.readPixels(ref, srcReadRect.x(), srcReadRect.y());
236 }
237 // This is the part of dstPixels that should have been updated.
238 SkPixmap actual;
239 SkAssertResult(dstPixels.extractSubset(&actual, dstWriteRect));
240 ComparePixels(ref, actual, tols, error);
241
242 const auto* v = dstData.get();
243 const auto* end = dstData.get() + dstSize;
244 guardOk = std::all_of(v, v + dstWriteRect.top() * dstPixels.rowBytes(), guardCheck);
245 v += dstWriteRect.top() * dstPixels.rowBytes();
246 for (int y = dstWriteRect.top(); y < dstWriteRect.bottom(); ++y) {
247 guardOk |= std::all_of(v, v + dstWriteRect.left() * readBpp, guardCheck);
248 auto pad = v + dstWriteRect.right() * readBpp;
249 auto rowEnd = std::min(end, v + dstPixels.rowBytes());
250 // min protects against reading past the end of the tight last row.
251 guardOk |= std::all_of(pad, rowEnd, guardCheck);
252 v = rowEnd;
253 }
254 guardOk |= std::all_of(v, end, guardCheck);
255 } else {
256 guardOk = std::all_of(dstData.get(), dstData.get() + dstSize, guardCheck);
257 }
258 if (!guardOk) {
259 ERRORF(reporter,
260 "Result pixels modified result outside read rect [%d, %d, %d, %d]. "
Brian Salomonbacbb922021-01-21 19:48:00 -0500261 "%sSrc CT: %s, Read CT: %s, CS conversion: %d",
262 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, label.c_str(),
Brian Salomonf84dfd62020-12-29 15:09:33 -0500263 ToolUtils::colortype_name(srcCT), ToolUtils::colortype_name(readCT),
264 csConversion);
265 }
266 return result;
267 };
268
269 static constexpr int kW = 16;
270 static constexpr int kH = 16;
271
Brian Salomon674d2162021-01-06 14:23:51 +0000272 // Makes the reference data that is used to populate the src. Always F32 regardless of srcCT.
273 auto make_ref_f32_data = [](SkAlphaType srcAT, SkColorType srcCT) -> SkAutoPixmapStorage {
274 // Make src data in F32 with srcAT. We will convert it to each color type we test to
275 // initialize the src.
276 auto surfInfo = SkImageInfo::Make(kW, kH,
277 kRGBA_F32_SkColorType,
278 srcAT,
279 SkColorSpace::MakeSRGB());
280 // Can't make a kUnknown_SkAlphaType surface.
281 if (srcAT == kUnknown_SkAlphaType) {
282 surfInfo = surfInfo.makeAlphaType(kUnpremul_SkAlphaType);
283 }
284 auto refSurf = SkSurface::MakeRaster(surfInfo);
285 static constexpr SkPoint kPts1[] = {{0, 0}, {kW, kH}};
286 static constexpr SkColor kColors1[] = {SK_ColorGREEN, SK_ColorRED};
287 SkPaint paint;
288 paint.setShader(
289 SkGradientShader::MakeLinear(kPts1, kColors1, nullptr, 2, SkTileMode::kClamp));
290 refSurf->getCanvas()->drawPaint(paint);
291 static constexpr SkPoint kPts2[] = {{kW, 0}, {0, kH}};
292 static constexpr SkColor kColors2[] = {SK_ColorBLUE, SK_ColorBLACK};
293 paint.setShader(
294 SkGradientShader::MakeLinear(kPts2, kColors2, nullptr, 2, SkTileMode::kClamp));
295 paint.setBlendMode(SkBlendMode::kPlus);
296 refSurf->getCanvas()->drawPaint(paint);
297 // Keep everything opaque if the src alpha type is opaque. Also, there is an issue with
298 // 1010102 (the only color type where the number of alpha bits is non-zero and not the
299 // same as r, g, and b). Because of the different precisions the draw below can create
300 // data that isn't strictly premul (e.g. alpha is 1/3 but green is .4). SW will clamp
301 // r, g, b to a if the dst is premul and a different color type. GPU doesn't do this.
302 // We could but 1010102 premul is kind of dubious anyway. So for now just keep the data
303 // opaque.
304 if (srcAT != kOpaque_SkAlphaType &&
305 (srcAT == kPremul_SkAlphaType && srcCT != kRGBA_1010102_SkColorType &&
306 srcCT != kBGRA_1010102_SkColorType)) {
307 static constexpr SkColor kColors3[] = {SK_ColorWHITE,
308 SK_ColorWHITE,
309 0x60FFFFFF,
310 SK_ColorWHITE,
311 SK_ColorWHITE};
312 static constexpr SkScalar kPos3[] = {0.f, 0.15f, 0.5f, 0.85f, 1.f};
313 paint.setShader(SkGradientShader::MakeRadial({kW / 2.f, kH / 2.f}, (kW + kH) / 10.f,
314 kColors3, kPos3, 5, SkTileMode::kMirror));
315 paint.setBlendMode(SkBlendMode::kDstIn);
316 refSurf->getCanvas()->drawPaint(paint);
317 }
Brian Salomonf84dfd62020-12-29 15:09:33 -0500318
Brian Salomon674d2162021-01-06 14:23:51 +0000319 const auto srcInfo = SkImageInfo::Make(kW, kH, srcCT, srcAT, SkColorSpace::MakeSRGB());
320 SkAutoPixmapStorage srcPixels;
321 srcPixels.alloc(srcInfo);
322 SkPixmap readPixmap = srcPixels;
323 // Spoof the alpha type to kUnpremul so the read will succeed without doing any conversion
324 // (because we made our surface also be kUnpremul).
325 if (srcAT == kUnknown_SkAlphaType) {
326 readPixmap.reset(srcPixels.info().makeAlphaType(kUnpremul_SkAlphaType),
327 srcPixels.addr(),
328 srcPixels.rowBytes());
329 }
330 refSurf->readPixels(readPixmap, 0, 0);
331 return srcPixels;
332 };
333 const std::vector<SkIRect> longRectArray = {
334 // entire thing
335 SkIRect::MakeWH(kW, kH),
336 // larger on all sides
337 SkIRect::MakeLTRB(-10, -10, kW + 10, kH + 10),
338 // fully contained
339 SkIRect::MakeLTRB(kW / 4, kH / 4, 3 * kW / 4, 3 * kH / 4),
340 // outside top left
341 SkIRect::MakeLTRB(-10, -10, -1, -1),
342 // touching top left corner
343 SkIRect::MakeLTRB(-10, -10, 0, 0),
344 // overlapping top left corner
345 SkIRect::MakeLTRB(-10, -10, kW / 4, kH / 4),
346 // overlapping top left and top right corners
347 SkIRect::MakeLTRB(-10, -10, kW + 10, kH / 4),
348 // touching entire top edge
349 SkIRect::MakeLTRB(-10, -10, kW + 10, 0),
350 // overlapping top right corner
351 SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH / 4),
352 // contained in x, overlapping top edge
353 SkIRect::MakeLTRB(kW / 4, -10, 3 * kW / 4, kH / 4),
354 // outside top right corner
355 SkIRect::MakeLTRB(kW + 1, -10, kW + 10, -1),
356 // touching top right corner
357 SkIRect::MakeLTRB(kW, -10, kW + 10, 0),
358 // overlapping top left and bottom left corners
359 SkIRect::MakeLTRB(-10, -10, kW / 4, kH + 10),
360 // touching entire left edge
361 SkIRect::MakeLTRB(-10, -10, 0, kH + 10),
362 // overlapping bottom left corner
363 SkIRect::MakeLTRB(-10, 3 * kH / 4, kW / 4, kH + 10),
364 // contained in y, overlapping left edge
365 SkIRect::MakeLTRB(-10, kH / 4, kW / 4, 3 * kH / 4),
366 // outside bottom left corner
367 SkIRect::MakeLTRB(-10, kH + 1, -1, kH + 10),
368 // touching bottom left corner
369 SkIRect::MakeLTRB(-10, kH, 0, kH + 10),
370 // overlapping bottom left and bottom right corners
371 SkIRect::MakeLTRB(-10, 3 * kH / 4, kW + 10, kH + 10),
372 // touching entire left edge
373 SkIRect::MakeLTRB(0, kH, kW, kH + 10),
374 // overlapping bottom right corner
375 SkIRect::MakeLTRB(3 * kW / 4, 3 * kH / 4, kW + 10, kH + 10),
376 // overlapping top right and bottom right corners
377 SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH + 10),
378 };
379 const std::vector<SkIRect> shortRectArray = {
380 // entire thing
381 SkIRect::MakeWH(kW, kH),
382 // fully contained
383 SkIRect::MakeLTRB(kW / 4, kH / 4, 3 * kW / 4, 3 * kH / 4),
384 // overlapping top right corner
385 SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH / 4),
386 };
Brian Salomonf84dfd62020-12-29 15:09:33 -0500387 // We ensure we use the long array once per src and read color type and otherwise use the
388 // short array to improve test run time.
389 // Also, some color types have no alpha values and thus Opaque Premul and Unpremul are
390 // equivalent. Just ensure each redundant AT is tested once with each CT (src and read).
391 // Similarly, alpha-only color types behave the same for all alpha types so just test premul
392 // after one iter.
Brian Salomon674d2162021-01-06 14:23:51 +0000393 // We consider a src or read CT thoroughly tested once it has run through the short rect array
Brian Salomonf84dfd62020-12-29 15:09:33 -0500394 // and full complement of alpha types with one successful read in the loop.
395 std::array<bool, kLastEnum_SkColorType + 1> srcCTTestedThoroughly = {},
396 readCTTestedThoroughly = {};
397 for (int sat = 0; sat < kLastEnum_SkAlphaType; ++sat) {
398 const auto srcAT = static_cast<SkAlphaType>(sat);
399 if (srcAT == kUnpremul_SkAlphaType && !rules.fAllowUnpremulSrc) {
400 continue;
401 }
402 for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) {
403 const auto srcCT = static_cast<SkColorType>(sct);
Brian Salomon674d2162021-01-06 14:23:51 +0000404 // Note that we only currently use srcCT for a 1010102 workaround. If we remove this we
405 // can also put the ref data setup above the srcCT loop.
406 SkAutoPixmapStorage srcPixels = make_ref_f32_data(srcAT, srcCT);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500407 auto src = srcFactory(srcPixels);
408 if (!src) {
409 continue;
410 }
411 if (SkColorTypeIsAlwaysOpaque(srcCT) && srcCTTestedThoroughly[srcCT] &&
412 (kPremul_SkAlphaType == srcAT || kUnpremul_SkAlphaType == srcAT)) {
413 continue;
414 }
415 if (SkColorTypeIsAlphaOnly(srcCT) && srcCTTestedThoroughly[srcCT] &&
416 (kUnpremul_SkAlphaType == srcAT ||
417 kOpaque_SkAlphaType == srcAT ||
418 kUnknown_SkAlphaType == srcAT)) {
419 continue;
420 }
421 for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) {
422 const auto readCT = static_cast<SkColorType>(rct);
423 for (const sk_sp<SkColorSpace>& readCS :
424 {SkColorSpace::MakeSRGB(), SkColorSpace::MakeSRGBLinear()}) {
425 for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) {
426 const auto readAT = static_cast<SkAlphaType>(at);
427 if (srcAT != kOpaque_SkAlphaType && readAT == kOpaque_SkAlphaType) {
428 // This doesn't make sense.
429 continue;
430 }
431 if (SkColorTypeIsAlwaysOpaque(readCT) && readCTTestedThoroughly[readCT] &&
432 (kPremul_SkAlphaType == readAT || kUnpremul_SkAlphaType == readAT)) {
433 continue;
434 }
435 if (SkColorTypeIsAlphaOnly(readCT) && readCTTestedThoroughly[readCT] &&
436 (kUnpremul_SkAlphaType == readAT ||
437 kOpaque_SkAlphaType == readAT ||
438 kUnknown_SkAlphaType == readAT)) {
439 continue;
440 }
441 const auto& rects =
442 srcCTTestedThoroughly[sct] && readCTTestedThoroughly[rct]
443 ? shortRectArray
444 : longRectArray;
445 for (const auto& rect : rects) {
446 const auto readInfo = SkImageInfo::Make(rect.width(), rect.height(),
447 readCT, readAT, readCS);
Brian Salomon674d2162021-01-06 14:23:51 +0000448 const SkIVector offset = rect.topLeft();
449 GpuReadResult r = runTest(src, srcPixels, readInfo, offset);
450 if (r == GpuReadResult::kSuccess) {
Brian Salomonf84dfd62020-12-29 15:09:33 -0500451 srcCTTestedThoroughly[sct] = true;
452 readCTTestedThoroughly[rct] = true;
453 }
454 }
455 }
456 }
457 }
458 }
459 }
460}
461
462DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceContextReadPixels, reporter, ctxInfo) {
463 using Surface = std::unique_ptr<GrSurfaceContext>;
464 GrDirectContext* direct = ctxInfo.directContext();
465 auto reader = std::function<GpuReadSrcFn<Surface>>(
Brian Salomon674d2162021-01-06 14:23:51 +0000466 [direct](const Surface& surface, const SkIVector& offset, const SkPixmap& pixels) {
467 if (surface->readPixels(direct, pixels, {offset.fX, offset.fY})) {
468 return GpuReadResult::kSuccess;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500469 } else {
470 // Reading from a non-renderable format is not guaranteed to work on GL.
471 // We'd have to be able to force a copy or draw draw to a renderable format.
472 const auto& caps = *direct->priv().caps();
473 if (direct->backend() == GrBackendApi::kOpenGL &&
474 !caps.isFormatRenderable(surface->asSurfaceProxy()->backendFormat(), 1)) {
Brian Salomon674d2162021-01-06 14:23:51 +0000475 return GpuReadResult::kExcusedFailure;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500476 }
Brian Salomon674d2162021-01-06 14:23:51 +0000477 return GpuReadResult::kFail;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500478 }
479 });
480 GpuReadPixelTestRules rules;
481 rules.fAllowUnpremulSrc = true;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500482 rules.fUncontainedRectSucceeds = true;
483
484 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
485 for (GrSurfaceOrigin origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
486 auto factory = std::function<GpuSrcFactory<Surface>>(
487 [direct, origin, renderable](const SkPixmap& src) {
488 if (src.colorType() == kRGB_888x_SkColorType) {
489 return Surface();
490 }
491 auto surfContext = GrSurfaceContext::Make(
492 direct, src.info(), SkBackingFit::kExact, origin, renderable);
493 if (surfContext) {
494 surfContext->writePixels(direct, src, {0, 0});
495 }
496 return surfContext;
497 });
Brian Salomonbacbb922021-01-21 19:48:00 -0500498 auto label = SkStringPrintf("Renderable: %d, Origin: %d", (int)renderable, origin);
499 gpu_read_pixels_test_driver(reporter, rules, factory, reader, label);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500500 }
501 }
502}
503
504namespace {
505struct AsyncContext {
506 bool fCalled = false;
507 std::unique_ptr<const SkImage::AsyncReadResult> fResult;
508};
509} // anonymous namespace
510
511// Making this a lambda in the test functions caused:
512// "error: cannot compile this forwarded non-trivially copyable parameter yet"
513// on x86/Win/Clang bot, referring to 'result'.
514static void async_callback(void* c, std::unique_ptr<const SkImage::AsyncReadResult> result) {
515 auto context = static_cast<AsyncContext*>(c);
516 context->fResult = std::move(result);
517 context->fCalled = true;
518};
519
520DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceAsyncReadPixels, reporter, ctxInfo) {
521 using Surface = sk_sp<SkSurface>;
522 auto reader = std::function<GpuReadSrcFn<Surface>>(
Brian Salomon674d2162021-01-06 14:23:51 +0000523 [](const Surface& surface, const SkIVector& offset, const SkPixmap& pixels) {
Brian Salomonf84dfd62020-12-29 15:09:33 -0500524 auto direct = surface->recordingContext()->asDirectContext();
525 SkASSERT(direct);
526
527 AsyncContext context;
528 auto rect = SkIRect::MakeSize(pixels.dimensions()).makeOffset(offset);
529
530 // Rescale quality and linearity don't matter since we're doing a non-scaling
531 // readback.
Mike Reed1efa14d2021-01-02 21:44:59 -0500532 surface->asyncRescaleAndReadPixels(pixels.info(), rect,
533 SkImage::RescaleGamma::kSrc,
534 SkImage::RescaleMode::kNearest,
535 async_callback, &context);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500536 direct->submit();
537 while (!context.fCalled) {
538 direct->checkAsyncWorkCompletion();
539 }
540 if (!context.fResult) {
Brian Salomon674d2162021-01-06 14:23:51 +0000541 return GpuReadResult::kFail;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500542 }
543 SkRectMemcpy(pixels.writable_addr(), pixels.rowBytes(), context.fResult->data(0),
544 context.fResult->rowBytes(0), pixels.info().minRowBytes(),
545 pixels.height());
Brian Salomon674d2162021-01-06 14:23:51 +0000546 return GpuReadResult::kSuccess;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500547 });
548 GpuReadPixelTestRules rules;
549 rules.fAllowUnpremulSrc = false;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500550 rules.fUncontainedRectSucceeds = false;
551
552 for (GrSurfaceOrigin origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
553 auto factory = std::function<GpuSrcFactory<Surface>>(
554 [context = ctxInfo.directContext(), origin](const SkPixmap& src) {
555 if (src.colorType() == kRGB_888x_SkColorType) {
556 return Surface();
557 }
558 auto surf = SkSurface::MakeRenderTarget(context,
559 SkBudgeted::kYes,
560 src.info(),
Brian Salomonbacbb922021-01-21 19:48:00 -0500561 1,
Brian Salomonf84dfd62020-12-29 15:09:33 -0500562 origin,
563 nullptr);
564 if (surf) {
565 surf->writePixels(src, 0, 0);
566 }
567 return surf;
568 });
Brian Salomonbacbb922021-01-21 19:48:00 -0500569 auto label = SkStringPrintf("Origin: %d", origin);
570 gpu_read_pixels_test_driver(reporter, rules, factory, reader, label);
571 auto backendRTFactory = std::function<GpuSrcFactory<Surface>>(
572 [context = ctxInfo.directContext(), origin](const SkPixmap& src) {
573 if (src.colorType() == kRGB_888x_SkColorType) {
574 return Surface();
575 }
576 // Dawn backend implementation of backend render targets doesn't support reading.
577 if (context->backend() == GrBackendApi::kDawn) {
578 return Surface();
579 }
580 auto surf = sk_gpu_test::MakeBackendRenderTargetSurface(context,
581 src.info(),
582 origin,
583 1);
584 if (surf) {
585 surf->writePixels(src, 0, 0);
586 }
587 return surf;
588 });
589 label = SkStringPrintf("BERT Origin: %d", origin);
590 gpu_read_pixels_test_driver(reporter, rules, backendRTFactory, reader, label);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500591 }
592}
593
594DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageAsyncReadPixels, reporter, ctxInfo) {
595 using Image = sk_sp<SkImage>;
596 auto context = ctxInfo.directContext();
597 auto reader = std::function<GpuReadSrcFn<Image>>([context](const Image& image,
Brian Salomon674d2162021-01-06 14:23:51 +0000598 const SkIVector& offset,
Brian Salomonf84dfd62020-12-29 15:09:33 -0500599 const SkPixmap& pixels) {
600 AsyncContext asyncContext;
601 auto rect = SkIRect::MakeSize(pixels.dimensions()).makeOffset(offset);
602 // The GPU implementation is based on rendering and will fail for non-renderable color
603 // types.
604 auto ct = SkColorTypeToGrColorType(image->colorType());
605 auto format = context->priv().caps()->getDefaultBackendFormat(ct, GrRenderable::kYes);
606 if (!context->priv().caps()->isFormatAsColorTypeRenderable(ct, format)) {
Brian Salomon674d2162021-01-06 14:23:51 +0000607 return GpuReadResult::kExcusedFailure;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500608 }
609
610 // Rescale quality and linearity don't matter since we're doing a non-scaling readback.
Mike Reed1efa14d2021-01-02 21:44:59 -0500611 image->asyncRescaleAndReadPixels(pixels.info(), rect,
612 SkImage::RescaleGamma::kSrc,
613 SkImage::RescaleMode::kNearest,
614 async_callback, &asyncContext);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500615 context->submit();
616 while (!asyncContext.fCalled) {
617 context->checkAsyncWorkCompletion();
618 }
619 if (!asyncContext.fResult) {
Brian Salomon674d2162021-01-06 14:23:51 +0000620 return GpuReadResult::kFail;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500621 }
622 SkRectMemcpy(pixels.writable_addr(), pixels.rowBytes(), asyncContext.fResult->data(0),
623 asyncContext.fResult->rowBytes(0), pixels.info().minRowBytes(),
624 pixels.height());
Brian Salomon674d2162021-01-06 14:23:51 +0000625 return GpuReadResult::kSuccess;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500626 });
627
628 GpuReadPixelTestRules rules;
629 rules.fAllowUnpremulSrc = true;
Brian Salomonf84dfd62020-12-29 15:09:33 -0500630 rules.fUncontainedRectSucceeds = false;
631
632 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
633 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
634 auto factory = std::function<GpuSrcFactory<Image>>([&](const SkPixmap& src) {
635 if (src.colorType() == kRGB_888x_SkColorType) {
636 return Image();
637 }
638 return sk_gpu_test::MakeBackendTextureImage(ctxInfo.directContext(), src,
639 renderable, origin);
640 });
Brian Salomonbacbb922021-01-21 19:48:00 -0500641 auto label = SkStringPrintf("Renderable: %d, Origin: %d", (int)renderable, origin);
642 gpu_read_pixels_test_driver(reporter, rules, factory, reader, label);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500643 }
644 }
645}
646
647DEF_GPUTEST(AsyncReadPixelsContextShutdown, reporter, options) {
648 const auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
649 SkColorSpace::MakeSRGB());
650 enum class ShutdownSequence {
651 kFreeResult_DestroyContext,
652 kDestroyContext_FreeResult,
653 kFreeResult_ReleaseAndAbandon_DestroyContext,
654 kFreeResult_Abandon_DestroyContext,
655 kReleaseAndAbandon_FreeResult_DestroyContext,
656 kAbandon_FreeResult_DestroyContext,
657 kReleaseAndAbandon_DestroyContext_FreeResult,
658 kAbandon_DestroyContext_FreeResult,
659 };
660 for (int t = 0; t < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++t) {
661 auto type = static_cast<sk_gpu_test::GrContextFactory::ContextType>(t);
662 for (auto sequence : {ShutdownSequence::kFreeResult_DestroyContext,
663 ShutdownSequence::kDestroyContext_FreeResult,
664 ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext,
665 ShutdownSequence::kFreeResult_Abandon_DestroyContext,
666 ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext,
667 ShutdownSequence::kAbandon_FreeResult_DestroyContext,
668 ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult,
669 ShutdownSequence::kAbandon_DestroyContext_FreeResult}) {
670 // Vulkan context abandoning without resource release has issues outside of the scope of
671 // this test.
672 if (type == sk_gpu_test::GrContextFactory::kVulkan_ContextType &&
673 (sequence == ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext ||
674 sequence == ShutdownSequence::kFreeResult_Abandon_DestroyContext ||
675 sequence == ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext ||
676 sequence == ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult ||
677 sequence == ShutdownSequence::kAbandon_FreeResult_DestroyContext ||
678 sequence == ShutdownSequence::kAbandon_DestroyContext_FreeResult)) {
679 continue;
680 }
681 for (bool yuv : {false, true}) {
682 sk_gpu_test::GrContextFactory factory(options);
683 auto direct = factory.get(type);
684 if (!direct) {
685 continue;
686 }
687 // This test is only meaningful for contexts that support transfer buffers for
688 // reads.
689 if (!direct->priv().caps()->transferFromSurfaceToBufferSupport()) {
690 continue;
691 }
692 auto surf = SkSurface::MakeRenderTarget(direct, SkBudgeted::kYes, ii, 1, nullptr);
693 if (!surf) {
694 continue;
695 }
696 AsyncContext cbContext;
697 if (yuv) {
698 surf->asyncRescaleAndReadPixelsYUV420(
699 kIdentity_SkYUVColorSpace, SkColorSpace::MakeSRGB(), ii.bounds(),
Mike Reed1efa14d2021-01-02 21:44:59 -0500700 ii.dimensions(), SkImage::RescaleGamma::kSrc,
701 SkImage::RescaleMode::kNearest, &async_callback, &cbContext);
Brian Salomonf84dfd62020-12-29 15:09:33 -0500702 } else {
703 surf->asyncRescaleAndReadPixels(ii, ii.bounds(), SkImage::RescaleGamma::kSrc,
Mike Reed1efa14d2021-01-02 21:44:59 -0500704 SkImage::RescaleMode::kNearest, &async_callback,
Brian Salomonf84dfd62020-12-29 15:09:33 -0500705 &cbContext);
706 }
707 direct->submit();
708 while (!cbContext.fCalled) {
709 direct->checkAsyncWorkCompletion();
710 }
711 if (!cbContext.fResult) {
712 ERRORF(reporter, "Callback failed on %s. is YUV: %d",
713 sk_gpu_test::GrContextFactory::ContextTypeName(type), yuv);
714 continue;
715 }
716 // For vulkan we need to release all refs to the GrDirectContext before trying to
717 // destroy the test context. The surface here is holding a ref.
718 surf.reset();
719
720 // The real test is that we don't crash, get Vulkan validation errors, etc, during
721 // this shutdown sequence.
722 switch (sequence) {
723 case ShutdownSequence::kFreeResult_DestroyContext:
724 case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
725 case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
726 break;
727 case ShutdownSequence::kDestroyContext_FreeResult:
728 factory.destroyContexts();
729 break;
730 case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
731 factory.releaseResourcesAndAbandonContexts();
732 break;
733 case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
734 factory.abandonContexts();
735 break;
736 case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
737 factory.releaseResourcesAndAbandonContexts();
738 factory.destroyContexts();
739 break;
740 case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
741 factory.abandonContexts();
742 factory.destroyContexts();
743 break;
744 }
745 cbContext.fResult.reset();
746 switch (sequence) {
747 case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
748 factory.releaseResourcesAndAbandonContexts();
749 break;
750 case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
751 factory.abandonContexts();
752 break;
753 case ShutdownSequence::kFreeResult_DestroyContext:
754 case ShutdownSequence::kDestroyContext_FreeResult:
755 case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
756 case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
757 case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
758 case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
759 break;
760 }
761 }
762 }
763 }
764}