1 /*
2 * Copyright 2015 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
8 #include "dm/DMSrcSink.h"
9 #include "gm/verifiers/gmverifier.h"
10 #include "include/codec/SkAndroidCodec.h"
11 #include "include/codec/SkCodec.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkDeferredDisplayListRecorder.h"
15 #include "include/core/SkDocument.h"
16 #include "include/core/SkExecutor.h"
17 #include "include/core/SkImageGenerator.h"
18 #include "include/core/SkMallocPixelRef.h"
19 #include "include/core/SkPictureRecorder.h"
20 #include "include/core/SkStream.h"
21 #include "include/core/SkSurface.h"
22 #include "include/core/SkSurfaceCharacterization.h"
23 #include "include/docs/SkPDFDocument.h"
24 #include "include/gpu/GrBackendSurface.h"
25 #include "include/gpu/GrDirectContext.h"
26 #include "include/ports/SkImageGeneratorCG.h"
27 #include "include/ports/SkImageGeneratorNDK.h"
28 #include "include/ports/SkImageGeneratorWIC.h"
29 #include "include/private/SkImageInfoPriv.h"
30 #include "include/private/SkTLogic.h"
31 #include "include/third_party/skcms/skcms.h"
32 #include "include/utils/SkNullCanvas.h"
33 #include "include/utils/SkRandom.h"
34 #include "modules/skottie/utils/SkottieUtils.h"
35 #include "src/codec/SkCodecImageGenerator.h"
36 #include "src/codec/SkSwizzler.h"
37 #include "src/core/SkAutoMalloc.h"
38 #include "src/core/SkAutoPixmapStorage.h"
39 #include "src/core/SkOSFile.h"
40 #include "src/core/SkOpts.h"
41 #include "src/core/SkPictureCommon.h"
42 #include "src/core/SkPictureData.h"
43 #include "src/core/SkRecordDraw.h"
44 #include "src/core/SkRecorder.h"
45 #include "src/core/SkTaskGroup.h"
46 #include "src/gpu/GrDirectContextPriv.h"
47 #include "src/gpu/GrGpu.h"
48 #include "src/utils/SkMultiPictureDocumentPriv.h"
49 #include "src/utils/SkOSPath.h"
50 #include "tools/DDLPromiseImageHelper.h"
51 #include "tools/DDLTileHelper.h"
52 #include "tools/Resources.h"
53 #include "tools/debugger/DebugCanvas.h"
54 #include "tools/gpu/BackendSurfaceFactory.h"
55 #include "tools/gpu/MemoryCache.h"
56 #if defined(SK_BUILD_FOR_WIN)
57 #include "include/docs/SkXPSDocument.h"
58 #include "src/utils/win/SkAutoCoInitialize.h"
59 #include "src/utils/win/SkHRESULT.h"
60 #include "src/utils/win/SkTScopedComPtr.h"
61 #include <XpsObjectModel.h>
62 #endif
63
64 #if defined(SK_ENABLE_SKOTTIE)
65 #include "modules/skottie/include/Skottie.h"
66 #include "modules/skresources/include/SkResources.h"
67 #endif
68
69 #if defined(SK_ENABLE_SKRIVE)
70 #include "experimental/skrive/include/SkRive.h"
71 #endif
72
73 #if defined(SK_XML)
74 #include "include/svg/SkSVGCanvas.h"
75 #include "modules/svg/include/SkSVGDOM.h"
76 #include "src/xml/SkXMLWriter.h"
77 #endif
78
79 #if defined(SK_ENABLE_ANDROID_UTILS)
80 #include "client_utils/android/BitmapRegionDecoder.h"
81 #endif
82 #include "tests/TestUtils.h"
83
84 #include <cmath>
85 #include <functional>
86
87 static DEFINE_bool(multiPage, false,
88 "For document-type backends, render the source into multiple pages");
89 static DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
90
91 DECLARE_int(gpuThreads);
92
93 using sk_gpu_test::GrContextFactory;
94 using sk_gpu_test::ContextInfo;
95
96 namespace DM {
97
GMSrc(skiagm::GMFactory factory)98 GMSrc::GMSrc(skiagm::GMFactory factory) : fFactory(factory) {}
99
draw(GrDirectContext * context,SkCanvas * canvas) const100 Result GMSrc::draw(GrDirectContext* context, SkCanvas* canvas) const {
101 std::unique_ptr<skiagm::GM> gm(fFactory());
102 SkString msg;
103
104 skiagm::DrawResult gpuSetupResult = gm->gpuSetup(context, canvas, &msg);
105 switch (gpuSetupResult) {
106 case skiagm::DrawResult::kOk : break;
107 case skiagm::DrawResult::kFail: return Result(Result::Status::Fatal, msg);
108 case skiagm::DrawResult::kSkip: return Result(Result::Status::Skip, msg);
109 default: SK_ABORT("");
110 }
111
112 skiagm::DrawResult drawResult = gm->draw(canvas, &msg);
113 switch (drawResult) {
114 case skiagm::DrawResult::kOk : return Result(Result::Status::Ok, msg);
115 case skiagm::DrawResult::kFail: return Result(Result::Status::Fatal, msg);
116 case skiagm::DrawResult::kSkip: return Result(Result::Status::Skip, msg);
117 default: SK_ABORT("");
118 }
119
120 // Note: we don't call "gpuTeardown" here because, when testing DDL recording, we want
121 // the gpu-backed images to live past the lifetime of the GM.
122 }
123
size() const124 SkISize GMSrc::size() const {
125 std::unique_ptr<skiagm::GM> gm(fFactory());
126 return gm->getISize();
127 }
128
name() const129 Name GMSrc::name() const {
130 std::unique_ptr<skiagm::GM> gm(fFactory());
131 return gm->getName();
132 }
133
modifyGrContextOptions(GrContextOptions * options) const134 void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
135 std::unique_ptr<skiagm::GM> gm(fFactory());
136 gm->modifyGrContextOptions(options);
137 }
138
getVerifiers() const139 std::unique_ptr<skiagm::verifiers::VerifierList> GMSrc::getVerifiers() const {
140 std::unique_ptr<skiagm::GM> gm(fFactory());
141 return gm->getVerifiers();
142 }
143
144 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
145
get_scaled_name(const Path & path,float scale)146 static SkString get_scaled_name(const Path& path, float scale) {
147 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), scale);
148 }
149
150 #ifdef SK_ENABLE_ANDROID_UTILS
BRDSrc(Path path,Mode mode,CodecSrc::DstColorType dstColorType,uint32_t sampleSize)151 BRDSrc::BRDSrc(Path path, Mode mode, CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
152 : fPath(path)
153 , fMode(mode)
154 , fDstColorType(dstColorType)
155 , fSampleSize(sampleSize)
156 {}
157
veto(SinkFlags flags) const158 bool BRDSrc::veto(SinkFlags flags) const {
159 // No need to test to non-raster or indirect backends.
160 return flags.type != SinkFlags::kRaster
161 || flags.approach != SinkFlags::kDirect;
162 }
163
create_brd(Path path)164 static std::unique_ptr<android::skia::BitmapRegionDecoder> create_brd(Path path) {
165 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
166 return android::skia::BitmapRegionDecoder::Make(encoded);
167 }
168
alpha8_to_gray8(SkBitmap * bitmap)169 static inline void alpha8_to_gray8(SkBitmap* bitmap) {
170 // Android requires kGray8 bitmaps to be tagged as kAlpha8. Here we convert
171 // them back to kGray8 so our test framework can draw them correctly.
172 if (kAlpha_8_SkColorType == bitmap->info().colorType()) {
173 SkImageInfo newInfo = bitmap->info().makeColorType(kGray_8_SkColorType)
174 .makeAlphaType(kOpaque_SkAlphaType);
175 *const_cast<SkImageInfo*>(&bitmap->info()) = newInfo;
176 }
177 }
178
draw(GrDirectContext *,SkCanvas * canvas) const179 Result BRDSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
180 SkColorType colorType = canvas->imageInfo().colorType();
181 if (kRGB_565_SkColorType == colorType &&
182 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType)
183 {
184 return Result::Skip("Testing non-565 to 565 is uninteresting.");
185 }
186 switch (fDstColorType) {
187 case CodecSrc::kGetFromCanvas_DstColorType:
188 break;
189 case CodecSrc::kGrayscale_Always_DstColorType:
190 colorType = kGray_8_SkColorType;
191 break;
192 default:
193 SkASSERT(false);
194 break;
195 }
196
197 auto brd = create_brd(fPath);
198 if (nullptr == brd) {
199 return Result::Skip("Could not create brd for %s.", fPath.c_str());
200 }
201
202 auto recommendedCT = brd->computeOutputColorType(colorType);
203 if (kRGB_565_SkColorType == colorType && recommendedCT != colorType) {
204 return Result::Skip("Skip decoding non-opaque to 565.");
205 }
206 colorType = recommendedCT;
207
208 auto colorSpace = brd->computeOutputColorSpace(colorType, nullptr);
209
210 const uint32_t width = brd->width();
211 const uint32_t height = brd->height();
212 // Visually inspecting very small output images is not necessary.
213 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampleSize) {
214 return Result::Skip("Scaling very small images is uninteresting.");
215 }
216 switch (fMode) {
217 case kFullImage_Mode: {
218 SkBitmap bitmap;
219 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, width, height),
220 fSampleSize, colorType, false, colorSpace)) {
221 return Result::Fatal("Cannot decode (full) region.");
222 }
223 alpha8_to_gray8(&bitmap);
224
225 canvas->drawImage(bitmap.asImage(), 0, 0);
226 return Result::Ok();
227 }
228 case kDivisor_Mode: {
229 const uint32_t divisor = 2;
230 if (width < divisor || height < divisor) {
231 return Result::Skip("Divisor is larger than image dimension.");
232 }
233
234 // Use a border to test subsets that extend outside the image.
235 // We will not allow the border to be larger than the image dimensions. Allowing
236 // these large borders causes off by one errors that indicate a problem with the
237 // test suite, not a problem with the implementation.
238 const uint32_t maxBorder = std::min(width, height) / (fSampleSize * divisor);
239 const uint32_t scaledBorder = std::min(5u, maxBorder);
240 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
241
242 // We may need to clear the canvas to avoid uninitialized memory.
243 // Assume we are scaling a 780x780 image with sampleSize = 8.
244 // The output image should be 97x97.
245 // Each subset will be 390x390.
246 // Each scaled subset be 48x48.
247 // Four scaled subsets will only fill a 96x96 image.
248 // The bottom row and last column will not be touched.
249 // This is an unfortunate result of our rounding rules when scaling.
250 // Maybe we need to consider testing scaled subsets without trying to
251 // combine them to match the full scaled image? Or maybe this is the
252 // best we can do?
253 canvas->clear(0);
254
255 for (uint32_t x = 0; x < divisor; x++) {
256 for (uint32_t y = 0; y < divisor; y++) {
257 // Calculate the subset dimensions
258 uint32_t subsetWidth = width / divisor;
259 uint32_t subsetHeight = height / divisor;
260 const int left = x * subsetWidth;
261 const int top = y * subsetHeight;
262
263 // Increase the size of the last subset in each row or column, when the
264 // divisor does not divide evenly into the image dimensions
265 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
266 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
267
268 // Increase the size of the subset in order to have a border on each side
269 const int decodeLeft = left - unscaledBorder;
270 const int decodeTop = top - unscaledBorder;
271 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
272 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
273 SkBitmap bitmap;
274 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(decodeLeft,
275 decodeTop, decodeWidth, decodeHeight), fSampleSize, colorType, false,
276 colorSpace)) {
277 return Result::Fatal("Cannot decode region.");
278 }
279
280 alpha8_to_gray8(&bitmap);
281 canvas->drawImageRect(bitmap.asImage().get(),
282 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar) scaledBorder,
283 (SkScalar) (subsetWidth / fSampleSize),
284 (SkScalar) (subsetHeight / fSampleSize)),
285 SkRect::MakeXYWH((SkScalar) (left / fSampleSize),
286 (SkScalar) (top / fSampleSize),
287 (SkScalar) (subsetWidth / fSampleSize),
288 (SkScalar) (subsetHeight / fSampleSize)),
289 SkSamplingOptions(), nullptr,
290 SkCanvas::kStrict_SrcRectConstraint);
291 }
292 }
293 return Result::Ok();
294 }
295 default:
296 SkASSERT(false);
297 return Result::Fatal("Error: Should not be reached.");
298 }
299 }
300
size() const301 SkISize BRDSrc::size() const {
302 auto brd = create_brd(fPath);
303 if (brd) {
304 return {std::max(1, brd->width() / (int)fSampleSize),
305 std::max(1, brd->height() / (int)fSampleSize)};
306 }
307 return {0, 0};
308 }
309
name() const310 Name BRDSrc::name() const {
311 // We will replicate the names used by CodecSrc so that images can
312 // be compared in Gold.
313 if (1 == fSampleSize) {
314 return SkOSPath::Basename(fPath.c_str());
315 }
316 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
317 }
318
319 #endif // SK_ENABLE_ANDROID_UTILS
320
321 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
322
serial_from_path_name(const SkString & path)323 static bool serial_from_path_name(const SkString& path) {
324 if (!FLAGS_RAW_threading) {
325 static const char* const exts[] = {
326 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
327 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
328 };
329 const char* actualExt = strrchr(path.c_str(), '.');
330 if (actualExt) {
331 actualExt++;
332 for (auto* ext : exts) {
333 if (0 == strcmp(ext, actualExt)) {
334 return true;
335 }
336 }
337 }
338 }
339 return false;
340 }
341
CodecSrc(Path path,Mode mode,DstColorType dstColorType,SkAlphaType dstAlphaType,float scale)342 CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, SkAlphaType dstAlphaType,
343 float scale)
344 : fPath(path)
345 , fMode(mode)
346 , fDstColorType(dstColorType)
347 , fDstAlphaType(dstAlphaType)
348 , fScale(scale)
349 , fRunSerially(serial_from_path_name(path))
350 {}
351
veto(SinkFlags flags) const352 bool CodecSrc::veto(SinkFlags flags) const {
353 // Test to direct raster backends (8888 and 565).
354 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
355 }
356
357 // Allows us to test decodes to non-native 8888.
swap_rb_if_necessary(SkBitmap & bitmap,CodecSrc::DstColorType dstColorType)358 static void swap_rb_if_necessary(SkBitmap& bitmap, CodecSrc::DstColorType dstColorType) {
359 if (CodecSrc::kNonNative8888_Always_DstColorType != dstColorType) {
360 return;
361 }
362
363 for (int y = 0; y < bitmap.height(); y++) {
364 uint32_t* row = (uint32_t*) bitmap.getAddr(0, y);
365 SkOpts::RGBA_to_BGRA(row, row, bitmap.width());
366 }
367 }
368
get_decode_info(SkImageInfo * decodeInfo,SkColorType canvasColorType,CodecSrc::DstColorType dstColorType,SkAlphaType dstAlphaType)369 static bool get_decode_info(SkImageInfo* decodeInfo, SkColorType canvasColorType,
370 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType) {
371 switch (dstColorType) {
372 case CodecSrc::kGrayscale_Always_DstColorType:
373 if (kRGB_565_SkColorType == canvasColorType) {
374 return false;
375 }
376 *decodeInfo = decodeInfo->makeColorType(kGray_8_SkColorType);
377 break;
378 case CodecSrc::kNonNative8888_Always_DstColorType:
379 if (kRGB_565_SkColorType == canvasColorType
380 || kRGBA_F16_SkColorType == canvasColorType) {
381 return false;
382 }
383 #ifdef SK_PMCOLOR_IS_RGBA
384 *decodeInfo = decodeInfo->makeColorType(kBGRA_8888_SkColorType);
385 #else
386 *decodeInfo = decodeInfo->makeColorType(kRGBA_8888_SkColorType);
387 #endif
388 break;
389 default:
390 if (kRGB_565_SkColorType == canvasColorType &&
391 kOpaque_SkAlphaType != decodeInfo->alphaType()) {
392 return false;
393 }
394
395 *decodeInfo = decodeInfo->makeColorType(canvasColorType);
396 break;
397 }
398
399 *decodeInfo = decodeInfo->makeAlphaType(dstAlphaType);
400 return true;
401 }
402
draw_to_canvas(SkCanvas * canvas,const SkImageInfo & info,void * pixels,size_t rowBytes,CodecSrc::DstColorType dstColorType,SkScalar left=0,SkScalar top=0)403 static void draw_to_canvas(SkCanvas* canvas, const SkImageInfo& info, void* pixels, size_t rowBytes,
404 CodecSrc::DstColorType dstColorType,
405 SkScalar left = 0, SkScalar top = 0) {
406 SkBitmap bitmap;
407 bitmap.installPixels(info, pixels, rowBytes);
408 swap_rb_if_necessary(bitmap, dstColorType);
409 canvas->drawImage(bitmap.asImage(), left, top);
410 }
411
412 // For codec srcs, we want the "draw" step to be a memcpy. Any interesting color space or
413 // color format conversions should be performed by the codec. Sometimes the output of the
414 // decode will be in an interesting color space. On our srgb and f16 backends, we need to
415 // "pretend" that the color space is standard sRGB to avoid triggering color conversion
416 // at draw time.
set_bitmap_color_space(SkImageInfo * info)417 static void set_bitmap_color_space(SkImageInfo* info) {
418 *info = info->makeColorSpace(SkColorSpace::MakeSRGB());
419 }
420
draw(GrDirectContext *,SkCanvas * canvas) const421 Result CodecSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
422 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
423 if (!encoded) {
424 return Result::Fatal("Couldn't read %s.", fPath.c_str());
425 }
426
427 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
428 if (nullptr == codec) {
429 return Result::Fatal("Couldn't create codec for %s.", fPath.c_str());
430 }
431
432 SkImageInfo decodeInfo = codec->getInfo();
433 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
434 fDstAlphaType)) {
435 return Result::Skip("Skipping uninteresting test.");
436 }
437
438 // Try to scale the image if it is desired
439 SkISize size = codec->getScaledDimensions(fScale);
440
441 std::unique_ptr<SkAndroidCodec> androidCodec;
442 if (1.0f != fScale && fMode == kAnimated_Mode) {
443 androidCodec = SkAndroidCodec::MakeFromData(encoded);
444 size = androidCodec->getSampledDimensions(1 / fScale);
445 }
446
447 if (size == decodeInfo.dimensions() && 1.0f != fScale) {
448 return Result::Skip("Test without scaling is uninteresting.");
449 }
450
451 // Visually inspecting very small output images is not necessary. We will
452 // cover these cases in unit testing.
453 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) {
454 return Result::Skip("Scaling very small images is uninteresting.");
455 }
456 decodeInfo = decodeInfo.makeDimensions(size);
457
458 const int bpp = decodeInfo.bytesPerPixel();
459 const size_t rowBytes = size.width() * bpp;
460 const size_t safeSize = decodeInfo.computeByteSize(rowBytes);
461 SkAutoMalloc pixels(safeSize);
462
463 SkCodec::Options options;
464 if (kCodecZeroInit_Mode == fMode) {
465 memset(pixels.get(), 0, size.height() * rowBytes);
466 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
467 }
468
469 SkImageInfo bitmapInfo = decodeInfo;
470 set_bitmap_color_space(&bitmapInfo);
471 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
472 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
473 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
474 }
475
476 switch (fMode) {
477 case kAnimated_Mode: {
478 SkAndroidCodec::AndroidOptions androidOptions;
479 if (fScale != 1.0f) {
480 SkASSERT(androidCodec);
481 androidOptions.fSampleSize = 1 / fScale;
482 auto dims = androidCodec->getSampledDimensions(androidOptions.fSampleSize);
483 decodeInfo = decodeInfo.makeDimensions(dims);
484 }
485
486 std::vector<SkCodec::FrameInfo> frameInfos = androidCodec
487 ? androidCodec->codec()->getFrameInfo() : codec->getFrameInfo();
488 if (frameInfos.size() <= 1) {
489 return Result::Fatal("%s is not an animated image.", fPath.c_str());
490 }
491
492 // As in CodecSrc::size(), compute a roughly square grid to draw the frames
493 // into. "factor" is the number of frames to draw on one row. There will be
494 // up to "factor" rows as well.
495 const float root = sqrt((float) frameInfos.size());
496 const int factor = sk_float_ceil2int(root);
497
498 // Used to cache a frame that future frames will depend on.
499 SkAutoMalloc priorFramePixels;
500 int cachedFrame = SkCodec::kNoFrame;
501 for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) {
502 androidOptions.fFrameIndex = i;
503 // Check for a prior frame
504 const int reqFrame = frameInfos[i].fRequiredFrame;
505 if (reqFrame != SkCodec::kNoFrame && reqFrame == cachedFrame
506 && priorFramePixels.get()) {
507 // Copy into pixels
508 memcpy(pixels.get(), priorFramePixels.get(), safeSize);
509 androidOptions.fPriorFrame = reqFrame;
510 } else {
511 androidOptions.fPriorFrame = SkCodec::kNoFrame;
512 }
513 SkCodec::Result result = androidCodec
514 ? androidCodec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes,
515 &androidOptions)
516 : codec->getPixels(decodeInfo, pixels.get(), rowBytes, &androidOptions);
517 if (SkCodec::kInvalidInput == result && i > 0) {
518 // Some of our test images have truncated later frames. Treat that
519 // the same as incomplete.
520 result = SkCodec::kIncompleteInput;
521 }
522 switch (result) {
523 case SkCodec::kSuccess:
524 case SkCodec::kErrorInInput:
525 case SkCodec::kIncompleteInput: {
526 // If the next frame depends on this one, store it in priorFrame.
527 // It is possible that we may discard a frame that future frames depend on,
528 // but the codec will simply redecode the discarded frame.
529 // Do this before calling draw_to_canvas, which premultiplies in place. If
530 // we're decoding to unpremul, we want to pass the unmodified frame to the
531 // codec for decoding the next frame.
532 if (static_cast<size_t>(i+1) < frameInfos.size()
533 && frameInfos[i+1].fRequiredFrame == i) {
534 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeSize);
535 cachedFrame = i;
536 }
537
538 SkAutoCanvasRestore acr(canvas, true);
539 const int xTranslate = (i % factor) * decodeInfo.width();
540 const int yTranslate = (i / factor) * decodeInfo.height();
541 canvas->translate(SkIntToScalar(xTranslate), SkIntToScalar(yTranslate));
542 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
543 if (result != SkCodec::kSuccess) {
544 return Result::Ok();
545 }
546 break;
547 }
548 case SkCodec::kInvalidConversion:
549 if (i > 0 && (decodeInfo.colorType() == kRGB_565_SkColorType)) {
550 return Result::Skip(
551 "Cannot decode frame %i to 565 (%s).", i, fPath.c_str());
552 }
553 [[fallthrough]];
554 default:
555 return Result::Fatal(
556 "Couldn't getPixels for frame %i in %s.", i, fPath.c_str());
557 }
558 }
559 break;
560 }
561 case kCodecZeroInit_Mode:
562 case kCodec_Mode: {
563 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
564 case SkCodec::kSuccess:
565 // We consider these to be valid, since we should still decode what is
566 // available.
567 case SkCodec::kErrorInInput:
568 case SkCodec::kIncompleteInput:
569 break;
570 default:
571 // Everything else is considered a failure.
572 return Result::Fatal("Couldn't getPixels %s.", fPath.c_str());
573 }
574
575 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
576 break;
577 }
578 case kScanline_Mode: {
579 void* dst = pixels.get();
580 uint32_t height = decodeInfo.height();
581 const bool useIncremental = [this]() {
582 auto exts = { "png", "PNG", "gif", "GIF" };
583 for (auto ext : exts) {
584 if (fPath.endsWith(ext)) {
585 return true;
586 }
587 }
588 return false;
589 }();
590 // ico may use the old scanline method or the new one, depending on whether it
591 // internally holds a bmp or a png.
592 const bool ico = fPath.endsWith("ico");
593 bool useOldScanlineMethod = !useIncremental && !ico;
594 if (useIncremental || ico) {
595 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInfo, dst,
596 rowBytes, &options)) {
597 int rowsDecoded;
598 auto result = codec->incrementalDecode(&rowsDecoded);
599 if (SkCodec::kIncompleteInput == result || SkCodec::kErrorInInput == result) {
600 codec->fillIncompleteImage(decodeInfo, dst, rowBytes,
601 SkCodec::kNo_ZeroInitialized, height,
602 rowsDecoded);
603 }
604 } else {
605 if (useIncremental) {
606 // Error: These should support incremental decode.
607 return Result::Fatal("Could not start incremental decode");
608 }
609 // Otherwise, this is an ICO. Since incremental failed, it must contain a BMP,
610 // which should work via startScanlineDecode
611 useOldScanlineMethod = true;
612 }
613 }
614
615 if (useOldScanlineMethod) {
616 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
617 return Result::Fatal("Could not start scanline decoder");
618 }
619
620 // We do not need to check the return value. On an incomplete
621 // image, memory will be filled with a default value.
622 codec->getScanlines(dst, height, rowBytes);
623 }
624
625 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
626 break;
627 }
628 case kStripe_Mode: {
629 const int height = decodeInfo.height();
630 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
631 // does not align with image blocks.
632 const int stripeHeight = 37;
633 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
634 void* dst = pixels.get();
635
636 // Decode odd stripes
637 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
638 return Result::Fatal("Could not start scanline decoder");
639 }
640
641 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
642 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
643 // to run this test for image types that do not have this scanline ordering.
644 // We only run this on Jpeg, which is always kTopDown.
645 SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder());
646
647 for (int i = 0; i < numStripes; i += 2) {
648 // Skip a stripe
649 const int linesToSkip = std::min(stripeHeight, height - i * stripeHeight);
650 codec->skipScanlines(linesToSkip);
651
652 // Read a stripe
653 const int startY = (i + 1) * stripeHeight;
654 const int linesToRead = std::min(stripeHeight, height - startY);
655 if (linesToRead > 0) {
656 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
657 rowBytes);
658 }
659 }
660
661 // Decode even stripes
662 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
663 if (SkCodec::kSuccess != startResult) {
664 return Result::Fatal("Failed to restart scanline decoder with same parameters.");
665 }
666 for (int i = 0; i < numStripes; i += 2) {
667 // Read a stripe
668 const int startY = i * stripeHeight;
669 const int linesToRead = std::min(stripeHeight, height - startY);
670 codec->getScanlines(SkTAddOffset<void>(dst, rowBytes * startY), linesToRead,
671 rowBytes);
672
673 // Skip a stripe
674 const int linesToSkip = std::min(stripeHeight, height - (i + 1) * stripeHeight);
675 if (linesToSkip > 0) {
676 codec->skipScanlines(linesToSkip);
677 }
678 }
679
680 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, fDstColorType);
681 break;
682 }
683 case kCroppedScanline_Mode: {
684 const int width = decodeInfo.width();
685 const int height = decodeInfo.height();
686 // This value is chosen because, as we move across the image, it will sometimes
687 // align with the jpeg block sizes and it will sometimes not. This allows us
688 // to test interestingly different code paths in the implementation.
689 const int tileSize = 36;
690 SkIRect subset;
691 for (int x = 0; x < width; x += tileSize) {
692 subset = SkIRect::MakeXYWH(x, 0, std::min(tileSize, width - x), height);
693 options.fSubset = ⊂
694 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, &options)) {
695 return Result::Fatal("Could not start scanline decoder.");
696 }
697
698 codec->getScanlines(SkTAddOffset<void>(pixels.get(), x * bpp), height, rowBytes);
699 }
700
701 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
702 break;
703 }
704 case kSubset_Mode: {
705 // Arbitrarily choose a divisor.
706 int divisor = 2;
707 // Total width/height of the image.
708 const int W = codec->getInfo().width();
709 const int H = codec->getInfo().height();
710 if (divisor > W || divisor > H) {
711 return Result::Skip("Cannot codec subset: divisor %d is too big "
712 "for %s with dimensions (%d x %d)", divisor,
713 fPath.c_str(), W, H);
714 }
715 // subset dimensions
716 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
717 const int w = SkAlign2(W / divisor);
718 const int h = SkAlign2(H / divisor);
719 SkIRect subset;
720 options.fSubset = ⊂
721 SkBitmap subsetBm;
722 // We will reuse pixel memory from bitmap.
723 void* dst = pixels.get();
724 // Keep track of left and top (for drawing subsetBm into canvas). We could use
725 // fScale * x and fScale * y, but we want integers such that the next subset will start
726 // where the last one ended. So we'll add decodeInfo.width() and height().
727 int left = 0;
728 for (int x = 0; x < W; x += w) {
729 int top = 0;
730 for (int y = 0; y < H; y+= h) {
731 // Do not make the subset go off the edge of the image.
732 const int preScaleW = std::min(w, W - x);
733 const int preScaleH = std::min(h, H - y);
734 subset.setXYWH(x, y, preScaleW, preScaleH);
735 // And scale
736 // FIXME: Should we have a version of getScaledDimensions that takes a subset
737 // into account?
738 const int scaledW = std::max(1, SkScalarRoundToInt(preScaleW * fScale));
739 const int scaledH = std::max(1, SkScalarRoundToInt(preScaleH * fScale));
740 decodeInfo = decodeInfo.makeWH(scaledW, scaledH);
741 SkImageInfo subsetBitmapInfo = bitmapInfo.makeWH(scaledW, scaledH);
742 size_t subsetRowBytes = subsetBitmapInfo.minRowBytes();
743 const SkCodec::Result result = codec->getPixels(decodeInfo, dst, subsetRowBytes,
744 &options);
745 switch (result) {
746 case SkCodec::kSuccess:
747 case SkCodec::kErrorInInput:
748 case SkCodec::kIncompleteInput:
749 break;
750 default:
751 return Result::Fatal("subset codec failed to decode (%d, %d, %d, %d) "
752 "from %s with dimensions (%d x %d)\t error %d",
753 x, y, decodeInfo.width(), decodeInfo.height(),
754 fPath.c_str(), W, H, result);
755 }
756 draw_to_canvas(canvas, subsetBitmapInfo, dst, subsetRowBytes, fDstColorType,
757 SkIntToScalar(left), SkIntToScalar(top));
758
759 // translate by the scaled height.
760 top += decodeInfo.height();
761 }
762 // translate by the scaled width.
763 left += decodeInfo.width();
764 }
765 return Result::Ok();
766 }
767 default:
768 SkASSERT(false);
769 return Result::Fatal("Invalid fMode");
770 }
771 return Result::Ok();
772 }
773
size() const774 SkISize CodecSrc::size() const {
775 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
776 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
777 if (nullptr == codec) {
778 return {0, 0};
779 }
780
781 if (fMode != kAnimated_Mode) {
782 return codec->getScaledDimensions(fScale);
783 }
784
785 // We'll draw one of each frame, so make it big enough to hold them all
786 // in a grid. The grid will be roughly square, with "factor" frames per
787 // row and up to "factor" rows.
788 const size_t count = codec->getFrameInfo().size();
789 const float root = sqrt((float) count);
790 const int factor = sk_float_ceil2int(root);
791
792 auto androidCodec = SkAndroidCodec::MakeFromCodec(std::move(codec));
793 auto imageSize = androidCodec->getSampledDimensions(1 / fScale);
794 imageSize.fWidth = imageSize.fWidth * factor;
795 imageSize.fHeight = imageSize.fHeight * sk_float_ceil2int((float) count / (float) factor);
796 return imageSize;
797 }
798
name() const799 Name CodecSrc::name() const {
800 Name name = SkOSPath::Basename(fPath.c_str());
801 if (fMode == kAnimated_Mode) {
802 name.append("_animated");
803 }
804 if (1.0f == fScale) {
805 return name;
806 }
807 return get_scaled_name(name.c_str(), fScale);
808 }
809
810 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
811
AndroidCodecSrc(Path path,CodecSrc::DstColorType dstColorType,SkAlphaType dstAlphaType,int sampleSize)812 AndroidCodecSrc::AndroidCodecSrc(Path path, CodecSrc::DstColorType dstColorType,
813 SkAlphaType dstAlphaType, int sampleSize)
814 : fPath(path)
815 , fDstColorType(dstColorType)
816 , fDstAlphaType(dstAlphaType)
817 , fSampleSize(sampleSize)
818 , fRunSerially(serial_from_path_name(path))
819 {}
820
veto(SinkFlags flags) const821 bool AndroidCodecSrc::veto(SinkFlags flags) const {
822 // No need to test decoding to non-raster or indirect backend.
823 return flags.type != SinkFlags::kRaster
824 || flags.approach != SinkFlags::kDirect;
825 }
826
draw(GrDirectContext *,SkCanvas * canvas) const827 Result AndroidCodecSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
828 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
829 if (!encoded) {
830 return Result::Fatal("Couldn't read %s.", fPath.c_str());
831 }
832 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
833 if (nullptr == codec) {
834 return Result::Fatal("Couldn't create android codec for %s.", fPath.c_str());
835 }
836
837 SkImageInfo decodeInfo = codec->getInfo();
838 if (!get_decode_info(&decodeInfo, canvas->imageInfo().colorType(), fDstColorType,
839 fDstAlphaType)) {
840 return Result::Skip("Skipping uninteresting test.");
841 }
842
843 // Scale the image if it is desired.
844 SkISize size = codec->getSampledDimensions(fSampleSize);
845
846 // Visually inspecting very small output images is not necessary. We will
847 // cover these cases in unit testing.
848 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) {
849 return Result::Skip("Scaling very small images is uninteresting.");
850 }
851 decodeInfo = decodeInfo.makeDimensions(size);
852
853 int bpp = decodeInfo.bytesPerPixel();
854 size_t rowBytes = size.width() * bpp;
855 SkAutoMalloc pixels(size.height() * rowBytes);
856
857 SkBitmap bitmap;
858 SkImageInfo bitmapInfo = decodeInfo;
859 set_bitmap_color_space(&bitmapInfo);
860 if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
861 kBGRA_8888_SkColorType == decodeInfo.colorType()) {
862 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType);
863 }
864
865 // Create options for the codec.
866 SkAndroidCodec::AndroidOptions options;
867 options.fSampleSize = fSampleSize;
868
869 switch (codec->getAndroidPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
870 case SkCodec::kSuccess:
871 case SkCodec::kErrorInInput:
872 case SkCodec::kIncompleteInput:
873 break;
874 default:
875 return Result::Fatal("Couldn't getPixels %s.", fPath.c_str());
876 }
877 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, fDstColorType);
878 return Result::Ok();
879 }
880
size() const881 SkISize AndroidCodecSrc::size() const {
882 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
883 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
884 if (nullptr == codec) {
885 return {0, 0};
886 }
887 return codec->getSampledDimensions(fSampleSize);
888 }
889
name() const890 Name AndroidCodecSrc::name() const {
891 // We will replicate the names used by CodecSrc so that images can
892 // be compared in Gold.
893 if (1 == fSampleSize) {
894 return SkOSPath::Basename(fPath.c_str());
895 }
896 return get_scaled_name(fPath, 1.0f / (float) fSampleSize);
897 }
898
899 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
900
ImageGenSrc(Path path,Mode mode,SkAlphaType alphaType,bool isGpu)901 ImageGenSrc::ImageGenSrc(Path path, Mode mode, SkAlphaType alphaType, bool isGpu)
902 : fPath(path)
903 , fMode(mode)
904 , fDstAlphaType(alphaType)
905 , fIsGpu(isGpu)
906 , fRunSerially(serial_from_path_name(path))
907 {}
908
veto(SinkFlags flags) const909 bool ImageGenSrc::veto(SinkFlags flags) const {
910 if (fIsGpu) {
911 // MSAA runs tend to run out of memory and tests the same code paths as regular gpu configs.
912 return flags.type != SinkFlags::kGPU || flags.approach != SinkFlags::kDirect ||
913 flags.multisampled == SinkFlags::kMultisampled;
914 }
915
916 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
917 }
918
draw(GrDirectContext *,SkCanvas * canvas) const919 Result ImageGenSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
920 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
921 return Result::Skip("Uninteresting to test image generator to 565.");
922 }
923
924 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
925 if (!encoded) {
926 return Result::Fatal("Couldn't read %s.", fPath.c_str());
927 }
928
929 #if defined(SK_BUILD_FOR_WIN)
930 // Initialize COM in order to test with WIC.
931 SkAutoCoInitialize com;
932 if (!com.succeeded()) {
933 return Result::Fatal("Could not initialize COM.");
934 }
935 #endif
936
937 std::unique_ptr<SkImageGenerator> gen(nullptr);
938 switch (fMode) {
939 case kCodec_Mode:
940 gen = SkCodecImageGenerator::MakeFromEncodedCodec(encoded);
941 if (!gen) {
942 return Result::Fatal("Could not create codec image generator.");
943 }
944 break;
945 case kPlatform_Mode: {
946 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
947 gen = SkImageGeneratorCG::MakeFromEncodedCG(encoded);
948 #elif defined(SK_BUILD_FOR_WIN)
949 gen = SkImageGeneratorWIC::MakeFromEncodedWIC(encoded);
950 #elif defined(SK_ENABLE_NDK_IMAGES)
951 gen = SkImageGeneratorNDK::MakeFromEncodedNDK(encoded);
952 #endif
953 if (!gen) {
954 return Result::Fatal("Could not create platform image generator.");
955 }
956 break;
957 }
958 default:
959 SkASSERT(false);
960 return Result::Fatal("Invalid image generator mode");
961 }
962
963 // Test deferred decoding path on GPU
964 if (fIsGpu) {
965 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
966 if (!image) {
967 return Result::Fatal("Could not create image from codec image generator.");
968 }
969 canvas->drawImage(image, 0, 0);
970 return Result::Ok();
971 }
972
973 // Test various color and alpha types on CPU
974 SkImageInfo decodeInfo = gen->getInfo().makeAlphaType(fDstAlphaType);
975
976 int bpp = decodeInfo.bytesPerPixel();
977 size_t rowBytes = decodeInfo.width() * bpp;
978 SkAutoMalloc pixels(decodeInfo.height() * rowBytes);
979 if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes)) {
980 Result::Status status = Result::Status::Fatal;
981 #if defined(SK_BUILD_FOR_WIN)
982 if (kPlatform_Mode == fMode) {
983 // Do not issue a fatal error for WIC flakiness.
984 status = Result::Status::Skip;
985 }
986 #endif
987 return Result(status, "Image generator could not getPixels() for %s\n", fPath.c_str());
988 }
989
990 set_bitmap_color_space(&decodeInfo);
991 draw_to_canvas(canvas, decodeInfo, pixels.get(), rowBytes,
992 CodecSrc::kGetFromCanvas_DstColorType);
993 return Result::Ok();
994 }
995
size() const996 SkISize ImageGenSrc::size() const {
997 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
998 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
999 if (nullptr == codec) {
1000 return {0, 0};
1001 }
1002 return codec->getInfo().dimensions();
1003 }
1004
name() const1005 Name ImageGenSrc::name() const {
1006 return SkOSPath::Basename(fPath.c_str());
1007 }
1008
1009 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1010
ColorCodecSrc(Path path,bool decode_to_dst)1011 ColorCodecSrc::ColorCodecSrc(Path path, bool decode_to_dst) : fPath(path)
1012 , fDecodeToDst(decode_to_dst) {}
1013
veto(SinkFlags flags) const1014 bool ColorCodecSrc::veto(SinkFlags flags) const {
1015 // Test to direct raster backends (8888 and 565).
1016 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
1017 }
1018
draw(GrDirectContext *,SkCanvas * canvas) const1019 Result ColorCodecSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
1020 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
1021 if (!encoded) {
1022 return Result::Fatal("Couldn't read %s.", fPath.c_str());
1023 }
1024
1025 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
1026 if (nullptr == codec) {
1027 return Result::Fatal("Couldn't create codec for %s.", fPath.c_str());
1028 }
1029
1030 SkImageInfo info = codec->getInfo();
1031 if (fDecodeToDst) {
1032 SkImageInfo canvasInfo = canvas->imageInfo();
1033 if (!canvasInfo.colorSpace()) {
1034 // This will skip color conversion, and the resulting images will
1035 // look different from images they are compared against in Gold, but
1036 // that doesn't mean they are wrong. We have a test verifying that
1037 // passing a null SkColorSpace skips conversion, so skip this
1038 // misleading test.
1039 return Result::Skip("Skipping decoding without color transform.");
1040 }
1041 info = canvasInfo.makeDimensions(info.dimensions());
1042 }
1043
1044 auto [image, result] = codec->getImage(info);
1045 switch (result) {
1046 case SkCodec::kSuccess:
1047 case SkCodec::kErrorInInput:
1048 case SkCodec::kIncompleteInput:
1049 canvas->drawImage(image, 0,0);
1050 return Result::Ok();
1051 case SkCodec::kInvalidConversion:
1052 // TODO(mtklein): why are there formats we can't decode to?
1053 return Result::Skip("SkCodec can't decode to this format.");
1054 default:
1055 return Result::Fatal("Couldn't getPixels %s. Error code %d", fPath.c_str(), result);
1056 }
1057 }
1058
size() const1059 SkISize ColorCodecSrc::size() const {
1060 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
1061 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
1062 if (nullptr == codec) {
1063 return {0, 0};
1064 }
1065 return {codec->getInfo().width(), codec->getInfo().height()};
1066 }
1067
name() const1068 Name ColorCodecSrc::name() const {
1069 return SkOSPath::Basename(fPath.c_str());
1070 }
1071
1072 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1073
1074 static DEFINE_int(skpViewportSize, 1000,
1075 "Width & height of the viewport used to crop skp rendering.");
1076
SKPSrc(Path path)1077 SKPSrc::SKPSrc(Path path) : fPath(path) { }
1078
draw(GrDirectContext *,SkCanvas * canvas) const1079 Result SKPSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
1080 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str());
1081 if (!stream) {
1082 return Result::Fatal("Couldn't read %s.", fPath.c_str());
1083 }
1084 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
1085 if (!pic) {
1086 return Result::Fatal("Couldn't parse file %s.", fPath.c_str());
1087 }
1088 stream = nullptr; // Might as well drop this when we're done with it.
1089 canvas->clipRect(SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize));
1090 canvas->drawPicture(pic);
1091 return Result::Ok();
1092 }
1093
get_cull_rect_for_skp(const char * path)1094 static SkRect get_cull_rect_for_skp(const char* path) {
1095 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
1096 if (!stream) {
1097 return SkRect::MakeEmpty();
1098 }
1099 SkPictInfo info;
1100 if (!SkPicture_StreamIsSKP(stream.get(), &info)) {
1101 return SkRect::MakeEmpty();
1102 }
1103
1104 return info.fCullRect;
1105 }
1106
size() const1107 SkISize SKPSrc::size() const {
1108 SkRect viewport = get_cull_rect_for_skp(fPath.c_str());
1109 if (!viewport.intersect((SkRect::MakeWH(FLAGS_skpViewportSize, FLAGS_skpViewportSize)))) {
1110 return {0, 0};
1111 }
1112 return viewport.roundOut().size();
1113 }
1114
name() const1115 Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1116
1117 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1118
BisectSrc(Path path,const char * trail)1119 BisectSrc::BisectSrc(Path path, const char* trail) : INHERITED(path), fTrail(trail) {}
1120
draw(GrDirectContext * context,SkCanvas * canvas) const1121 Result BisectSrc::draw(GrDirectContext* context, SkCanvas* canvas) const {
1122 struct FoundPath {
1123 SkPath fPath;
1124 SkPaint fPaint;
1125 SkMatrix fViewMatrix;
1126 };
1127
1128 // This subclass of SkCanvas just extracts all the SkPaths (drawn via drawPath) from an SKP.
1129 class PathFindingCanvas : public SkCanvas {
1130 public:
1131 PathFindingCanvas(int width, int height) : SkCanvas(width, height, nullptr) {}
1132 const SkTArray<FoundPath>& foundPaths() const { return fFoundPaths; }
1133
1134 private:
1135 void onDrawPath(const SkPath& path, const SkPaint& paint) override {
1136 fFoundPaths.push_back() = {path, paint, this->getTotalMatrix()};
1137 }
1138
1139 SkTArray<FoundPath> fFoundPaths;
1140 };
1141
1142 PathFindingCanvas pathFinder(canvas->getBaseLayerSize().width(),
1143 canvas->getBaseLayerSize().height());
1144 Result result = this->INHERITED::draw(context, &pathFinder);
1145 if (!result.isOk()) {
1146 return result;
1147 }
1148
1149 int start = 0, end = pathFinder.foundPaths().count();
1150 for (const char* ch = fTrail.c_str(); *ch; ++ch) {
1151 int midpt = (start + end) / 2;
1152 if ('l' == *ch) {
1153 start = midpt;
1154 } else if ('r' == *ch) {
1155 end = midpt;
1156 }
1157 }
1158
1159 for (int i = start; i < end; ++i) {
1160 const FoundPath& path = pathFinder.foundPaths()[i];
1161 SkAutoCanvasRestore acr(canvas, true);
1162 canvas->concat(path.fViewMatrix);
1163 canvas->drawPath(path.fPath, path.fPaint);
1164 }
1165
1166 return Result::Ok();
1167 }
1168
1169 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1170
1171 #if defined(SK_ENABLE_SKOTTIE)
1172 static DEFINE_bool(useLottieGlyphPaths, false,
1173 "Prioritize embedded glyph paths over native fonts.");
1174
SkottieSrc(Path path)1175 SkottieSrc::SkottieSrc(Path path) : fPath(std::move(path)) {}
1176
draw(GrDirectContext *,SkCanvas * canvas) const1177 Result SkottieSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
1178 auto resource_provider =
1179 skresources::DataURIResourceProviderProxy::Make(
1180 skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
1181 /*predecode=*/true),
1182 /*predecode=*/true);
1183
1184 static constexpr char kInterceptPrefix[] = "__";
1185 auto precomp_interceptor =
1186 sk_make_sp<skottie_utils::ExternalAnimationPrecompInterceptor>(resource_provider,
1187 kInterceptPrefix);
1188 uint32_t flags = 0;
1189 if (FLAGS_useLottieGlyphPaths) {
1190 flags |= skottie::Animation::Builder::kPreferEmbeddedFonts;
1191 }
1192
1193 auto animation = skottie::Animation::Builder(flags)
1194 .setResourceProvider(std::move(resource_provider))
1195 .setPrecompInterceptor(std::move(precomp_interceptor))
1196 .makeFromFile(fPath.c_str());
1197 if (!animation) {
1198 return Result::Fatal("Unable to parse file: %s", fPath.c_str());
1199 }
1200
1201 canvas->drawColor(SK_ColorWHITE);
1202
1203 const auto t_rate = 1.0f / (kTileCount * kTileCount - 1);
1204
1205 // Draw the frames in a shuffled order to exercise non-linear
1206 // frame progression. The film strip will still be in order left-to-right,
1207 // top-down, just not drawn in that order.
1208 static constexpr int frameOrder[] = { 4, 0, 3, 1, 2 };
1209 static_assert(SK_ARRAY_COUNT(frameOrder) == kTileCount, "");
1210
1211 for (int i = 0; i < kTileCount; ++i) {
1212 const SkScalar y = frameOrder[i] * kTileSize;
1213
1214 for (int j = 0; j < kTileCount; ++j) {
1215 const SkScalar x = frameOrder[j] * kTileSize;
1216 SkRect dest = SkRect::MakeXYWH(x, y, kTileSize, kTileSize);
1217
1218 const auto t = t_rate * (frameOrder[i] * kTileCount + frameOrder[j]);
1219 {
1220 SkAutoCanvasRestore acr(canvas, true);
1221 canvas->clipRect(dest, true);
1222 canvas->concat(SkMatrix::RectToRect(SkRect::MakeSize(animation->size()), dest,
1223 SkMatrix::kCenter_ScaleToFit));
1224 animation->seek(t);
1225 animation->render(canvas);
1226 }
1227 }
1228 }
1229
1230 return Result::Ok();
1231 }
1232
size() const1233 SkISize SkottieSrc::size() const {
1234 return SkISize::Make(kTargetSize, kTargetSize);
1235 }
1236
name() const1237 Name SkottieSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1238
veto(SinkFlags flags) const1239 bool SkottieSrc::veto(SinkFlags flags) const {
1240 // No need to test to non-(raster||gpu||vector) or indirect backends.
1241 bool type_ok = flags.type == SinkFlags::kRaster
1242 || flags.type == SinkFlags::kGPU
1243 || flags.type == SinkFlags::kVector;
1244
1245 return !type_ok || flags.approach != SinkFlags::kDirect;
1246 }
1247 #endif
1248
1249 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1250 #if defined(SK_ENABLE_SKRIVE)
SkRiveSrc(Path path)1251 SkRiveSrc::SkRiveSrc(Path path) : fPath(std::move(path)) {}
1252
draw(GrDirectContext *,SkCanvas * canvas) const1253 Result SkRiveSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
1254 auto fileStream = SkFILEStream::Make(fPath.c_str());
1255 if (!fileStream) {
1256 return Result::Fatal("Unable to open file: %s", fPath.c_str());
1257 }
1258
1259 const auto skrive = skrive::SkRive::Builder().make(std::move(fileStream));
1260 if (!skrive) {
1261 return Result::Fatal("Unable to parse file: %s", fPath.c_str());
1262 }
1263
1264 auto bounds = SkRect::MakeEmpty();
1265
1266 for (const auto& ab : skrive->artboards()) {
1267 const auto& pos = ab->getTranslation();
1268 const auto& size = ab->getSize();
1269
1270 bounds.join(SkRect::MakeXYWH(pos.x, pos.y, size.x, size.y));
1271 }
1272
1273 canvas->drawColor(SK_ColorWHITE);
1274
1275 if (!bounds.isEmpty()) {
1276 // TODO: tiled frames when we add animation support
1277 SkAutoCanvasRestore acr(canvas, true);
1278 canvas->concat(SkMatrix::RectToRect(bounds, SkRect::MakeWH(kTargetSize, kTargetSize),
1279 SkMatrix::kCenter_ScaleToFit));
1280 for (const auto& ab : skrive->artboards()) {
1281 ab->render(canvas);
1282 }
1283 }
1284
1285 return Result::Ok();
1286 }
1287
size() const1288 SkISize SkRiveSrc::size() const {
1289 return SkISize::Make(kTargetSize, kTargetSize);
1290 }
1291
name() const1292 Name SkRiveSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1293
veto(SinkFlags flags) const1294 bool SkRiveSrc::veto(SinkFlags flags) const {
1295 // No need to test to non-(raster||gpu||vector) or indirect backends.
1296 bool type_ok = flags.type == SinkFlags::kRaster
1297 || flags.type == SinkFlags::kGPU
1298 || flags.type == SinkFlags::kVector;
1299
1300 return !type_ok || flags.approach != SinkFlags::kDirect;
1301 }
1302 #endif
1303
1304 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1305 #if defined(SK_XML)
1306 // Used when the image doesn't have an intrinsic size.
1307 static const SkSize kDefaultSVGSize = {1000, 1000};
1308
1309 // Used to force-scale tiny fixed-size images.
1310 static const SkSize kMinimumSVGSize = {128, 128};
1311
SVGSrc(Path path)1312 SVGSrc::SVGSrc(Path path)
1313 : fName(SkOSPath::Basename(path.c_str()))
1314 , fScale(1) {
1315
1316 auto stream = SkStream::MakeFromFile(path.c_str());
1317 if (!stream) {
1318 return;
1319 }
1320
1321 auto rp = skresources::DataURIResourceProviderProxy::Make(
1322 skresources::FileResourceProvider::Make(SkOSPath::Dirname(path.c_str()),
1323 /*predecode=*/true),
1324 /*predecode=*/true);
1325 fDom = SkSVGDOM::Builder().setResourceProvider(std::move(rp))
1326 .make(*stream);
1327 if (!fDom) {
1328 return;
1329 }
1330
1331 const SkSize& sz = fDom->containerSize();
1332 if (sz.isEmpty()) {
1333 // no intrinsic size
1334 fDom->setContainerSize(kDefaultSVGSize);
1335 } else {
1336 fScale = std::max(1.f, std::max(kMinimumSVGSize.width() / sz.width(),
1337 kMinimumSVGSize.height() / sz.height()));
1338 }
1339 }
1340
draw(GrDirectContext *,SkCanvas * canvas) const1341 Result SVGSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
1342 if (!fDom) {
1343 return Result::Fatal("Unable to parse file: %s", fName.c_str());
1344 }
1345
1346 SkAutoCanvasRestore acr(canvas, true);
1347 canvas->scale(fScale, fScale);
1348 canvas->drawColor(SK_ColorWHITE);
1349 fDom->render(canvas);
1350
1351 return Result::Ok();
1352 }
1353
size() const1354 SkISize SVGSrc::size() const {
1355 if (!fDom) {
1356 return {0, 0};
1357 }
1358
1359 return SkSize{fDom->containerSize().width() * fScale, fDom->containerSize().height() * fScale}
1360 .toRound();
1361 }
1362
name() const1363 Name SVGSrc::name() const { return fName; }
1364
veto(SinkFlags flags) const1365 bool SVGSrc::veto(SinkFlags flags) const {
1366 // No need to test to non-(raster||gpu||vector) or indirect backends.
1367 bool type_ok = flags.type == SinkFlags::kRaster
1368 || flags.type == SinkFlags::kGPU
1369 || flags.type == SinkFlags::kVector;
1370
1371 return !type_ok || flags.approach != SinkFlags::kDirect;
1372 }
1373
1374 #endif // defined(SK_XML)
1375 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1376
MSKPSrc(Path path)1377 MSKPSrc::MSKPSrc(Path path) : fPath(path) {
1378 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
1379 int count = SkMultiPictureDocumentReadPageCount(stream.get());
1380 if (count > 0) {
1381 fPages.reset(count);
1382 (void)SkMultiPictureDocumentReadPageSizes(stream.get(), &fPages[0], fPages.count());
1383 }
1384 }
1385
pageCount() const1386 int MSKPSrc::pageCount() const { return fPages.count(); }
1387
size() const1388 SkISize MSKPSrc::size() const { return this->size(0); }
size(int i) const1389 SkISize MSKPSrc::size(int i) const {
1390 return i >= 0 && i < fPages.count() ? fPages[i].fSize.toCeil() : SkISize{0, 0};
1391 }
1392
draw(GrDirectContext * context,SkCanvas * c) const1393 Result MSKPSrc::draw(GrDirectContext* context, SkCanvas* c) const {
1394 return this->draw(0, context, c);
1395 }
draw(int i,GrDirectContext *,SkCanvas * canvas) const1396 Result MSKPSrc::draw(int i, GrDirectContext*, SkCanvas* canvas) const {
1397 if (this->pageCount() == 0) {
1398 return Result::Fatal("Unable to parse MultiPictureDocument file: %s", fPath.c_str());
1399 }
1400 if (i >= fPages.count() || i < 0) {
1401 return Result::Fatal("MultiPictureDocument page number out of range: %d", i);
1402 }
1403 SkPicture* page = fPages[i].fPicture.get();
1404 if (!page) {
1405 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
1406 if (!stream) {
1407 return Result::Fatal("Unable to open file: %s", fPath.c_str());
1408 }
1409 if (!SkMultiPictureDocumentRead(stream.get(), &fPages[0], fPages.count())) {
1410 return Result::Fatal("SkMultiPictureDocument reader failed on page %d: %s", i,
1411 fPath.c_str());
1412 }
1413 page = fPages[i].fPicture.get();
1414 }
1415 canvas->drawPicture(page);
1416 return Result::Ok();
1417 }
1418
name() const1419 Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
1420
1421 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1422
draw(const Src & src,SkBitmap *,SkWStream *,SkString *) const1423 Result NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
1424 return src.draw(nullptr, SkMakeNullCanvas().get());
1425 }
1426
1427 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1428
compare_bitmaps(const SkBitmap & reference,const SkBitmap & bitmap)1429 static Result compare_bitmaps(const SkBitmap& reference, const SkBitmap& bitmap) {
1430 // The dimensions are a property of the Src only, and so should be identical.
1431 SkASSERT(reference.computeByteSize() == bitmap.computeByteSize());
1432 if (reference.computeByteSize() != bitmap.computeByteSize()) {
1433 return Result::Fatal("Dimensions don't match reference");
1434 }
1435 // All SkBitmaps in DM are tight, so this comparison is easy.
1436 if (0 != memcmp(reference.getPixels(), bitmap.getPixels(), reference.computeByteSize())) {
1437 SkString encoded;
1438 SkString errString("Pixels don't match reference");
1439 if (BipmapToBase64DataURI(reference, &encoded)) {
1440 errString.append("\nExpected: ");
1441 errString.append(encoded);
1442 } else {
1443 errString.append("\nExpected image failed to encode: ");
1444 errString.append(encoded);
1445 }
1446 if (BipmapToBase64DataURI(bitmap, &encoded)) {
1447 errString.append("\nActual: ");
1448 errString.append(encoded);
1449 } else {
1450 errString.append("\nActual image failed to encode: ");
1451 errString.append(encoded);
1452 }
1453 return Result::Fatal(errString);
1454 }
1455 return Result::Ok();
1456 }
1457
1458 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1459
1460 static DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
1461 static DEFINE_bool(preAbandonGpuContext, false,
1462 "Test abandoning the GrContext before running the test.");
1463 static DEFINE_bool(abandonGpuContext, false,
1464 "Test abandoning the GrContext after running each test.");
1465 static DEFINE_bool(releaseAndAbandonGpuContext, false,
1466 "Test releasing all gpu resources and abandoning the GrContext "
1467 "after running each test");
1468 static DEFINE_bool(drawOpClip, false, "Clip each GrDrawOp to its device bounds for testing.");
1469 static DEFINE_bool(programBinaryCache, true, "Use in-memory program binary cache");
1470
GPUSink(const SkCommandLineConfigGpu * config,const GrContextOptions & grCtxOptions)1471 GPUSink::GPUSink(const SkCommandLineConfigGpu* config,
1472 const GrContextOptions& grCtxOptions)
1473 : fContextType(config->getContextType())
1474 , fContextOverrides(config->getContextOverrides())
1475 , fSurfType(config->getSurfType())
1476 , fSampleCount(config->getSamples())
1477 , fSurfaceFlags(config->getSurfaceFlags())
1478 , fColorType(config->getColorType())
1479 , fAlphaType(config->getAlphaType())
1480 , fColorSpace(sk_ref_sp(config->getColorSpace()))
1481 , fBaseContextOptions(grCtxOptions) {
1482 if (FLAGS_programBinaryCache) {
1483 fBaseContextOptions.fPersistentCache = &fMemoryCache;
1484 }
1485 }
1486
draw(const Src & src,SkBitmap * dst,SkWStream * dstStream,SkString * log) const1487 Result GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream* dstStream, SkString* log) const {
1488 return this->onDraw(src, dst, dstStream, log, fBaseContextOptions);
1489 }
1490
createDstSurface(GrDirectContext * context,SkISize size) const1491 sk_sp<SkSurface> GPUSink::createDstSurface(GrDirectContext* context, SkISize size) const {
1492 sk_sp<SkSurface> surface;
1493
1494 SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
1495 SkSurfaceProps props(fSurfaceFlags, kRGB_H_SkPixelGeometry);
1496
1497 switch (fSurfType) {
1498 case SkCommandLineConfigGpu::SurfType::kDefault:
1499 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount,
1500 &props);
1501 break;
1502 case SkCommandLineConfigGpu::SurfType::kBackendTexture:
1503 surface = sk_gpu_test::MakeBackendTextureSurface(context,
1504 info,
1505 kTopLeft_GrSurfaceOrigin,
1506 fSampleCount,
1507 GrMipmapped::kNo,
1508 GrProtected::kNo,
1509 &props);
1510 break;
1511 case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget:
1512 surface = sk_gpu_test::MakeBackendRenderTargetSurface(context,
1513 info,
1514 kBottomLeft_GrSurfaceOrigin,
1515 fSampleCount,
1516 GrProtected::kNo,
1517 &props);
1518 break;
1519 }
1520
1521 return surface;
1522 }
1523
readBack(SkSurface * surface,SkBitmap * dst) const1524 bool GPUSink::readBack(SkSurface* surface, SkBitmap* dst) const {
1525 SkCanvas* canvas = surface->getCanvas();
1526 SkISize size = surface->imageInfo().dimensions();
1527
1528 SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
1529 dst->allocPixels(info);
1530 return canvas->readPixels(*dst, 0, 0);
1531 }
1532
onDraw(const Src & src,SkBitmap * dst,SkWStream *,SkString * log,const GrContextOptions & baseOptions,std::function<void (GrDirectContext *)> initContext) const1533 Result GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
1534 const GrContextOptions& baseOptions,
1535 std::function<void(GrDirectContext*)> initContext) const {
1536 GrContextOptions grOptions = baseOptions;
1537
1538 // We don't expect the src to mess with the persistent cache or the executor.
1539 SkDEBUGCODE(auto cache = grOptions.fPersistentCache);
1540 SkDEBUGCODE(auto exec = grOptions.fExecutor);
1541 src.modifyGrContextOptions(&grOptions);
1542 SkASSERT(cache == grOptions.fPersistentCache);
1543 SkASSERT(exec == grOptions.fExecutor);
1544
1545 GrContextFactory factory(grOptions);
1546 auto direct = factory.getContextInfo(fContextType, fContextOverrides).directContext();
1547 if (initContext) {
1548 initContext(direct);
1549 }
1550
1551 const int maxDimension = direct->priv().caps()->maxTextureSize();
1552 if (maxDimension < std::max(src.size().width(), src.size().height())) {
1553 return Result::Skip("Src too large to create a texture.\n");
1554 }
1555
1556 sk_sp<SkSurface> surface = this->createDstSurface(direct, src.size());
1557 if (!surface) {
1558 return Result::Fatal("Could not create a surface.");
1559 }
1560 if (FLAGS_preAbandonGpuContext) {
1561 factory.abandonContexts();
1562 }
1563 SkCanvas* canvas = surface->getCanvas();
1564 Result result = src.draw(direct, canvas);
1565 if (!result.isOk()) {
1566 return result;
1567 }
1568 surface->flushAndSubmit();
1569 if (FLAGS_gpuStats) {
1570 direct->priv().dumpCacheStats(log);
1571 direct->priv().dumpGpuStats(log);
1572 direct->priv().dumpContextStats(log);
1573 }
1574
1575 this->readBack(surface.get(), dst);
1576
1577 if (FLAGS_abandonGpuContext) {
1578 factory.abandonContexts();
1579 } else if (FLAGS_releaseAndAbandonGpuContext) {
1580 factory.releaseResourcesAndAbandonContexts();
1581 }
1582
1583 if (grOptions.fPersistentCache) {
1584 direct->storeVkPipelineCacheData();
1585 }
1586 return Result::Ok();
1587 }
1588
1589 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1590
GPUThreadTestingSink(const SkCommandLineConfigGpu * config,const GrContextOptions & grCtxOptions)1591 GPUThreadTestingSink::GPUThreadTestingSink(const SkCommandLineConfigGpu* config,
1592 const GrContextOptions& grCtxOptions)
1593 : INHERITED(config, grCtxOptions)
1594 , fExecutor(SkExecutor::MakeFIFOThreadPool(FLAGS_gpuThreads)) {
1595 SkASSERT(fExecutor);
1596 }
1597
draw(const Src & src,SkBitmap * dst,SkWStream * wStream,SkString * log) const1598 Result GPUThreadTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1599 SkString* log) const {
1600 // Draw twice, once with worker threads, and once without. Verify that we get the same result.
1601 // Also, force us to only use the software path renderer, so we really stress-test the threaded
1602 // version of that code.
1603 GrContextOptions contextOptions = this->baseContextOptions();
1604 contextOptions.fGpuPathRenderers = GpuPathRenderers::kNone;
1605 contextOptions.fExecutor = fExecutor.get();
1606
1607 Result result = this->onDraw(src, dst, wStream, log, contextOptions);
1608 if (!result.isOk() || !dst) {
1609 return result;
1610 }
1611
1612 SkBitmap reference;
1613 SkString refLog;
1614 SkDynamicMemoryWStream refStream;
1615 contextOptions.fExecutor = nullptr;
1616 Result refResult = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1617 if (!refResult.isOk()) {
1618 return refResult;
1619 }
1620
1621 return compare_bitmaps(reference, *dst);
1622 }
1623
1624 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1625
GPUPersistentCacheTestingSink(const SkCommandLineConfigGpu * config,const GrContextOptions & grCtxOptions)1626 GPUPersistentCacheTestingSink::GPUPersistentCacheTestingSink(const SkCommandLineConfigGpu* config,
1627 const GrContextOptions& grCtxOptions)
1628 : INHERITED(config, grCtxOptions)
1629 , fCacheType(config->getTestPersistentCache()) {}
1630
draw(const Src & src,SkBitmap * dst,SkWStream * wStream,SkString * log) const1631 Result GPUPersistentCacheTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1632 SkString* log) const {
1633 // Draw twice, once with a cold cache, and again with a warm cache. Verify that we get the same
1634 // result.
1635 sk_gpu_test::MemoryCache memoryCache;
1636 GrContextOptions contextOptions = this->baseContextOptions();
1637 contextOptions.fPersistentCache = &memoryCache;
1638 if (fCacheType == 2) {
1639 contextOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
1640 }
1641
1642 Result result = this->onDraw(src, dst, wStream, log, contextOptions);
1643 if (!result.isOk() || !dst) {
1644 return result;
1645 }
1646
1647 SkBitmap reference;
1648 SkString refLog;
1649 SkDynamicMemoryWStream refStream;
1650 memoryCache.resetCacheStats();
1651 Result refResult = this->onDraw(src, &reference, &refStream, &refLog, contextOptions);
1652 if (!refResult.isOk()) {
1653 return refResult;
1654 }
1655 SkASSERT(!memoryCache.numCacheMisses());
1656 SkASSERT(!memoryCache.numCacheStores());
1657
1658 return compare_bitmaps(reference, *dst);
1659 }
1660
1661
1662 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1663
GPUPrecompileTestingSink(const SkCommandLineConfigGpu * config,const GrContextOptions & grCtxOptions)1664 GPUPrecompileTestingSink::GPUPrecompileTestingSink(const SkCommandLineConfigGpu* config,
1665 const GrContextOptions& grCtxOptions)
1666 : INHERITED(config, grCtxOptions) {}
1667
draw(const Src & src,SkBitmap * dst,SkWStream * wStream,SkString * log) const1668 Result GPUPrecompileTestingSink::draw(const Src& src, SkBitmap* dst, SkWStream* wStream,
1669 SkString* log) const {
1670 // Three step process:
1671 // 1) Draw once with an SkSL cache, and store off the shader blobs.
1672 // 2) For the second context, pre-compile the shaders to warm the cache.
1673 // 3) Draw with the second context, ensuring that we get the same result, and no cache misses.
1674 sk_gpu_test::MemoryCache memoryCache;
1675 GrContextOptions contextOptions = this->baseContextOptions();
1676 contextOptions.fPersistentCache = &memoryCache;
1677 contextOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL;
1678
1679 Result result = this->onDraw(src, dst, wStream, log, contextOptions);
1680 if (!result.isOk() || !dst) {
1681 return result;
1682 }
1683
1684 auto precompileShaders = [&memoryCache](GrDirectContext* dContext) {
1685 memoryCache.foreach([dContext](sk_sp<const SkData> key,
1686 sk_sp<SkData> data,
1687 const SkString& /*description*/,
1688 int /*count*/) {
1689 SkAssertResult(dContext->precompileShader(*key, *data));
1690 });
1691 };
1692
1693 sk_gpu_test::MemoryCache replayCache;
1694 GrContextOptions replayOptions = this->baseContextOptions();
1695 // Ensure that the runtime cache is large enough to hold all of the shaders we pre-compile
1696 replayOptions.fRuntimeProgramCacheSize = memoryCache.numCacheMisses();
1697 replayOptions.fPersistentCache = &replayCache;
1698
1699 SkBitmap reference;
1700 SkString refLog;
1701 SkDynamicMemoryWStream refStream;
1702 Result refResult = this->onDraw(src, &reference, &refStream, &refLog, replayOptions,
1703 precompileShaders);
1704 if (!refResult.isOk()) {
1705 return refResult;
1706 }
1707 SkASSERT(!replayCache.numCacheMisses());
1708
1709 return compare_bitmaps(reference, *dst);
1710 }
1711
1712 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
GPUOOPRSink(const SkCommandLineConfigGpu * config,const GrContextOptions & ctxOptions)1713 GPUOOPRSink::GPUOOPRSink(const SkCommandLineConfigGpu* config, const GrContextOptions& ctxOptions)
1714 : INHERITED(config, ctxOptions) {
1715 }
1716
ooprDraw(const Src & src,sk_sp<SkSurface> dstSurface,GrDirectContext * context) const1717 Result GPUOOPRSink::ooprDraw(const Src& src,
1718 sk_sp<SkSurface> dstSurface,
1719 GrDirectContext* context) const {
1720 SkSurfaceCharacterization dstCharacterization;
1721 SkAssertResult(dstSurface->characterize(&dstCharacterization));
1722
1723 SkDeferredDisplayListRecorder recorder(dstCharacterization);
1724
1725 Result result = src.draw(context, recorder.getCanvas());
1726 if (!result.isOk()) {
1727 return result;
1728 }
1729
1730 auto ddl = recorder.detach();
1731
1732 SkDeferredDisplayList::ProgramIterator iter(context, ddl.get());
1733 for (; !iter.done(); iter.next()) {
1734 iter.compile();
1735 }
1736
1737 SkAssertResult(dstSurface->draw(std::move(ddl)));
1738
1739 return Result::Ok();
1740 }
1741
draw(const Src & src,SkBitmap * dst,SkWStream *,SkString * log) const1742 Result GPUOOPRSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
1743 GrContextOptions contextOptions = this->baseContextOptions();
1744 src.modifyGrContextOptions(&contextOptions);
1745 contextOptions.fPersistentCache = nullptr;
1746 contextOptions.fExecutor = nullptr;
1747
1748 GrContextFactory factory(contextOptions);
1749
1750 ContextInfo ctxInfo = factory.getContextInfo(this->contextType(), this->contextOverrides());
1751 auto context = ctxInfo.directContext();
1752 if (!context) {
1753 return Result::Fatal("Could not create context.");
1754 }
1755
1756 SkASSERT(context->priv().getGpu());
1757
1758 sk_sp<SkSurface> surface = this->createDstSurface(context, src.size());
1759 if (!surface) {
1760 return Result::Fatal("Could not create a surface.");
1761 }
1762
1763 Result result = this->ooprDraw(src, surface, context);
1764 if (!result.isOk()) {
1765 return result;
1766 }
1767
1768 if (FLAGS_gpuStats) {
1769 context->priv().dumpCacheStats(log);
1770 context->priv().dumpGpuStats(log);
1771 context->priv().dumpContextStats(log);
1772 }
1773
1774 if (!this->readBack(surface.get(), dst)) {
1775 return Result::Fatal("Could not readback from surface.");
1776 }
1777
1778 return Result::Ok();
1779 }
1780
1781 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
GPUDDLSink(const SkCommandLineConfigGpu * config,const GrContextOptions & ctxOptions)1782 GPUDDLSink::GPUDDLSink(const SkCommandLineConfigGpu* config, const GrContextOptions& ctxOptions)
1783 : INHERITED(config, ctxOptions)
1784 , fRecordingExecutor(SkExecutor::MakeLIFOThreadPool(1))
1785 , fGPUExecutor(SkExecutor::MakeFIFOThreadPool(1, false)) {
1786 }
1787
ddlDraw(const Src & src,sk_sp<SkSurface> dstSurface,SkTaskGroup * recordingTaskGroup,SkTaskGroup * gpuTaskGroup,sk_gpu_test::TestContext * gpuTestCtx,GrDirectContext * dContext) const1788 Result GPUDDLSink::ddlDraw(const Src& src,
1789 sk_sp<SkSurface> dstSurface,
1790 SkTaskGroup* recordingTaskGroup,
1791 SkTaskGroup* gpuTaskGroup,
1792 sk_gpu_test::TestContext* gpuTestCtx,
1793 GrDirectContext* dContext) const {
1794
1795 // We have to do this here bc characterization can hit the SkGpuDevice's thread guard (i.e.,
1796 // leaving it until the DDLTileHelper ctor will result in multiple threads trying to use the
1797 // same context (this thread and the gpuThread - which will be uploading textures)).
1798 SkSurfaceCharacterization dstCharacterization;
1799 SkAssertResult(dstSurface->characterize(&dstCharacterization));
1800
1801 auto size = src.size();
1802 SkPictureRecorder recorder;
1803 Result result = src.draw(dContext, recorder.beginRecording(SkIntToScalar(size.width()),
1804 SkIntToScalar(size.height())));
1805 if (!result.isOk()) {
1806 return result;
1807 }
1808 sk_sp<SkPicture> inputPicture(recorder.finishRecordingAsPicture());
1809
1810 // this is our ultimate final drawing area/rect
1811 SkIRect viewport = SkIRect::MakeWH(size.fWidth, size.fHeight);
1812
1813 SkYUVAPixmapInfo::SupportedDataTypes supportedYUVADataTypes(*dContext);
1814 DDLPromiseImageHelper promiseImageHelper(supportedYUVADataTypes);
1815 sk_sp<SkPicture> newSKP = promiseImageHelper.recreateSKP(dContext, inputPicture.get());
1816 if (!newSKP) {
1817 return Result::Fatal("GPUDDLSink: Couldn't recreate the SKP");
1818 }
1819
1820 // 'gpuTestCtx/gpuThreadCtx' is being shifted to the gpuThread. Leave the main (this)
1821 // thread w/o a context.
1822 gpuTestCtx->makeNotCurrent();
1823
1824 // Job one for the GPU thread is to make 'gpuTestCtx' current!
1825 gpuTaskGroup->add([gpuTestCtx] { gpuTestCtx->makeCurrent(); });
1826
1827 // TODO: move the image upload to the utility thread
1828 promiseImageHelper.uploadAllToGPU(gpuTaskGroup, dContext);
1829
1830 // Care must be taken when using 'gpuThreadCtx' bc it moves between the gpu-thread and this
1831 // one. About all it can be consistently used for is GrCaps access and 'defaultBackendFormat'
1832 // calls.
1833 constexpr int kNumDivisions = 3;
1834 DDLTileHelper tiles(dContext, dstCharacterization, viewport,
1835 kNumDivisions, kNumDivisions,
1836 /* addRandomPaddingToDst */ false);
1837
1838 tiles.createBackendTextures(gpuTaskGroup, dContext);
1839
1840 tiles.kickOffThreadedWork(recordingTaskGroup, gpuTaskGroup, dContext, newSKP.get());
1841
1842 // We have to wait for the recording threads to schedule all their work on the gpu thread
1843 // before we can schedule the composition draw and the flush. Note that the gpu thread
1844 // is not blocked at this point and this thread is borrowing recording work.
1845 recordingTaskGroup->wait();
1846
1847 // Note: at this point the recording thread(s) are stalled out w/ nothing to do.
1848
1849 // The recording threads have already scheduled the drawing of each tile's DDL on the gpu
1850 // thread. The composition DDL must be scheduled last bc it relies on the result of all
1851 // the tiles' rendering. Additionally, bc we're aliasing the tiles' backend textures,
1852 // there is nothing in the DAG to automatically force the required order.
1853 gpuTaskGroup->add([dstSurface, ddl = tiles.composeDDL()]() {
1854 dstSurface->draw(ddl);
1855 });
1856
1857 // This should be the only explicit flush for the entire DDL draw.
1858 gpuTaskGroup->add([dContext]() {
1859 // We need to ensure all the GPU work is finished so
1860 // the following 'deleteAllFromGPU' call will work
1861 // on Vulkan.
1862 // TODO: switch over to using the promiseImage callbacks
1863 // to free the backendTextures. This is complicated a
1864 // bit by which thread possesses the direct context.
1865 dContext->flush();
1866 dContext->submit(true);
1867 });
1868
1869 // The backend textures are created on the gpuThread by the 'uploadAllToGPU' call.
1870 // It is simpler to also delete them at this point on the gpuThread.
1871 promiseImageHelper.deleteAllFromGPU(gpuTaskGroup, dContext);
1872
1873 tiles.deleteBackendTextures(gpuTaskGroup, dContext);
1874
1875 // A flush has already been scheduled on the gpu thread along with the clean up of the backend
1876 // textures so it is safe to schedule making 'gpuTestCtx' not current on the gpuThread.
1877 gpuTaskGroup->add([gpuTestCtx] { gpuTestCtx->makeNotCurrent(); });
1878
1879 // All the work is scheduled on the gpu thread, we just need to wait
1880 gpuTaskGroup->wait();
1881
1882 return Result::Ok();
1883 }
1884
draw(const Src & src,SkBitmap * dst,SkWStream *,SkString * log) const1885 Result GPUDDLSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
1886 GrContextOptions contextOptions = this->baseContextOptions();
1887 src.modifyGrContextOptions(&contextOptions);
1888 contextOptions.fPersistentCache = nullptr;
1889 contextOptions.fExecutor = nullptr;
1890
1891 GrContextFactory factory(contextOptions);
1892
1893 // This captures the context destined to be the main gpu context
1894 ContextInfo mainCtxInfo = factory.getContextInfo(this->contextType(), this->contextOverrides());
1895 sk_gpu_test::TestContext* mainTestCtx = mainCtxInfo.testContext();
1896 auto mainCtx = mainCtxInfo.directContext();
1897 if (!mainCtx) {
1898 return Result::Fatal("Could not create context.");
1899 }
1900
1901 SkASSERT(mainCtx->priv().getGpu());
1902
1903 // TODO: make use of 'otherCtx' for uploads & compilation
1904 #if 0
1905 // This captures the context destined to be the utility context. It is in a share group
1906 // with the main context
1907 ContextInfo otherCtxInfo = factory.getSharedContextInfo(mainCtx);
1908 sk_gpu_test::TestContext* otherTestCtx = otherCtxInfo.testContext();
1909 auto otherCtx = otherCtxInfo.directContext();
1910 if (!otherCtx) {
1911 return Result::Fatal("Cound not create shared context.");
1912 }
1913
1914 SkASSERT(otherCtx->priv().getGpu());
1915 #endif
1916
1917 SkTaskGroup recordingTaskGroup(*fRecordingExecutor);
1918 SkTaskGroup gpuTaskGroup(*fGPUExecutor);
1919
1920 // Make sure 'mainCtx' is current
1921 mainTestCtx->makeCurrent();
1922
1923 sk_sp<SkSurface> surface = this->createDstSurface(mainCtx, src.size());
1924 if (!surface) {
1925 return Result::Fatal("Could not create a surface.");
1926 }
1927
1928 Result result = this->ddlDraw(src, surface, &recordingTaskGroup, &gpuTaskGroup,
1929 mainTestCtx, mainCtx);
1930 if (!result.isOk()) {
1931 return result;
1932 }
1933
1934 // 'ddlDraw' will have made 'mainCtx' not current on the gpuThread
1935 mainTestCtx->makeCurrent();
1936
1937 if (FLAGS_gpuStats) {
1938 mainCtx->priv().dumpCacheStats(log);
1939 mainCtx->priv().dumpGpuStats(log);
1940 mainCtx->priv().dumpContextStats(log);
1941
1942 #if 0
1943 otherCtx->priv().dumpCacheStats(log);
1944 otherCtx->priv().dumpGpuStats(log);
1945 otherCtx->priv().dumpContextStats(log);
1946 #endif
1947 }
1948
1949 if (!this->readBack(surface.get(), dst)) {
1950 return Result::Fatal("Could not readback from surface.");
1951 }
1952
1953 return Result::Ok();
1954 }
1955
1956 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
draw_skdocument(const Src & src,SkDocument * doc,SkWStream * dst)1957 static Result draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
1958 if (src.size().isEmpty()) {
1959 return Result::Fatal("Source has empty dimensions");
1960 }
1961 SkASSERT(doc);
1962 int pageCount = src.pageCount();
1963 for (int i = 0; i < pageCount; ++i) {
1964 int width = src.size(i).width(), height = src.size(i).height();
1965 SkCanvas* canvas =
1966 doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
1967 if (!canvas) {
1968 return Result::Fatal("SkDocument::beginPage(w,h) returned nullptr");
1969 }
1970 Result result = src.draw(i, nullptr, canvas);
1971 if (!result.isOk()) {
1972 return result;
1973 }
1974 doc->endPage();
1975 }
1976 doc->close();
1977 dst->flush();
1978 return Result::Ok();
1979 }
1980
draw(const Src & src,SkBitmap *,SkWStream * dst,SkString *) const1981 Result PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
1982 SkPDF::Metadata metadata;
1983 metadata.fTitle = src.name();
1984 metadata.fSubject = "rendering correctness test";
1985 metadata.fCreator = "Skia/DM";
1986 metadata.fRasterDPI = fRasterDpi;
1987 metadata.fPDFA = fPDFA;
1988 #if SK_PDF_TEST_EXECUTOR
1989 std::unique_ptr<SkExecutor> executor = SkExecutor::MakeFIFOThreadPool();
1990 metadata.fExecutor = executor.get();
1991 #endif
1992 auto doc = SkPDF::MakeDocument(dst, metadata);
1993 if (!doc) {
1994 return Result::Fatal("SkPDF::MakeDocument() returned nullptr");
1995 }
1996 return draw_skdocument(src, doc.get(), dst);
1997 }
1998
1999 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2000
XPSSink()2001 XPSSink::XPSSink() {}
2002
2003 #if defined(SK_SUPPORT_XPS)
make_xps_factory()2004 static SkTScopedComPtr<IXpsOMObjectFactory> make_xps_factory() {
2005 IXpsOMObjectFactory* factory;
2006 HRN(CoCreateInstance(CLSID_XpsOMObjectFactory,
2007 nullptr,
2008 CLSCTX_INPROC_SERVER,
2009 IID_PPV_ARGS(&factory)));
2010 return SkTScopedComPtr<IXpsOMObjectFactory>(factory);
2011 }
2012
draw(const Src & src,SkBitmap *,SkWStream * dst,SkString *) const2013 Result XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
2014 SkAutoCoInitialize com;
2015 if (!com.succeeded()) {
2016 return Result::Fatal("Could not initialize COM.");
2017 }
2018 SkTScopedComPtr<IXpsOMObjectFactory> factory = make_xps_factory();
2019 if (!factory) {
2020 return Result::Fatal("Failed to create XPS Factory.");
2021 }
2022 auto doc = SkXPS::MakeDocument(dst, factory.get());
2023 if (!doc) {
2024 return Result::Fatal("SkXPS::MakeDocument() returned nullptr");
2025 }
2026 return draw_skdocument(src, doc.get(), dst);
2027 }
2028 #else
draw(const Src & src,SkBitmap *,SkWStream * dst,SkString *) const2029 Result XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
2030 return Result::Fatal("XPS not supported on this platform.");
2031 }
2032 #endif
2033
2034 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2035
SKPSink()2036 SKPSink::SKPSink() {}
2037
draw(const Src & src,SkBitmap *,SkWStream * dst,SkString *) const2038 Result SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
2039 auto size = SkSize::Make(src.size());
2040 SkPictureRecorder recorder;
2041 Result result = src.draw(nullptr, recorder.beginRecording(size.width(), size.height()));
2042 if (!result.isOk()) {
2043 return result;
2044 }
2045 recorder.finishRecordingAsPicture()->serialize(dst);
2046 return Result::Ok();
2047 }
2048
2049 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2050
draw(const Src & src,SkBitmap *,SkWStream * dst,SkString *) const2051 Result DebugSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
2052 DebugCanvas debugCanvas(src.size().width(), src.size().height());
2053 Result result = src.draw(nullptr, &debugCanvas);
2054 if (!result.isOk()) {
2055 return result;
2056 }
2057 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
2058 UrlDataManager dataManager(SkString("data"));
2059 SkJSONWriter writer(dst, SkJSONWriter::Mode::kPretty);
2060 writer.beginObject(); // root
2061 debugCanvas.toJSON(writer, dataManager, nullCanvas.get());
2062 writer.endObject(); // root
2063 writer.flush();
2064 return Result::Ok();
2065 }
2066
2067 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2068
SVGSink(int pageIndex)2069 SVGSink::SVGSink(int pageIndex) : fPageIndex(pageIndex) {}
2070
draw(const Src & src,SkBitmap *,SkWStream * dst,SkString *) const2071 Result SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
2072 #if defined(SK_XML)
2073 if (src.pageCount() > 1) {
2074 int pageCount = src.pageCount();
2075 if (fPageIndex > pageCount - 1) {
2076 return Result::Fatal("Page index %d too high for document with only %d pages.",
2077 fPageIndex, pageCount);
2078 }
2079 }
2080 return src.draw(fPageIndex, nullptr,
2081 SkSVGCanvas::Make(SkRect::MakeWH(SkIntToScalar(src.size().width()),
2082 SkIntToScalar(src.size().height())),
2083 dst)
2084 .get());
2085 #else
2086 (void)fPageIndex;
2087 return Result::Fatal("SVG sink is disabled.");
2088 #endif // SK_XML
2089 }
2090
2091 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2092
RasterSink(SkColorType colorType,sk_sp<SkColorSpace> colorSpace)2093 RasterSink::RasterSink(SkColorType colorType, sk_sp<SkColorSpace> colorSpace)
2094 : fColorType(colorType)
2095 , fColorSpace(std::move(colorSpace)) {}
2096
draw(const Src & src,SkBitmap * dst,SkWStream *,SkString *) const2097 Result RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
2098 const SkISize size = src.size();
2099 // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
2100 SkAlphaType alphaType = kPremul_SkAlphaType;
2101 (void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
2102
2103 dst->allocPixelsFlags(SkImageInfo::Make(size, fColorType, alphaType, fColorSpace),
2104 SkBitmap::kZeroPixels_AllocFlag);
2105
2106 SkCanvas canvas(*dst, SkSurfaceProps(0, kRGB_H_SkPixelGeometry));
2107 return src.draw(nullptr, &canvas);
2108 }
2109
2110 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2111
2112 // Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
2113 // passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
2114 // Several examples below.
2115
2116 template <typename Fn>
draw_to_canvas(Sink * sink,SkBitmap * bitmap,SkWStream * stream,SkString * log,SkISize size,const Fn & draw)2117 static Result draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
2118 SkISize size, const Fn& draw) {
2119 class ProxySrc : public Src {
2120 public:
2121 ProxySrc(SkISize size, const Fn& draw) : fSize(size), fDraw(draw) {}
2122 Result draw(GrDirectContext*, SkCanvas* canvas) const override { return fDraw(canvas); }
2123 Name name() const override { return "ProxySrc"; }
2124 SkISize size() const override { return fSize; }
2125 private:
2126 SkISize fSize;
2127 const Fn& fDraw;
2128 };
2129 return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
2130 }
2131
2132 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2133
2134 static DEFINE_bool(check, true, "If true, have most Via- modes fail if they affect the output.");
2135
2136 // Is *bitmap identical to what you get drawing src into sink?
check_against_reference(const SkBitmap * bitmap,const Src & src,Sink * sink)2137 static Result check_against_reference(const SkBitmap* bitmap, const Src& src, Sink* sink) {
2138 // We can only check raster outputs.
2139 // (Non-raster outputs like .pdf, .skp, .svg may differ but still draw identically.)
2140 if (FLAGS_check && bitmap) {
2141 SkBitmap reference;
2142 SkString log;
2143 SkDynamicMemoryWStream wStream;
2144 Result result = sink->draw(src, &reference, &wStream, &log);
2145 // If we can draw into this Sink via some pipeline, we should be able to draw directly.
2146 SkASSERT(result.isOk());
2147 if (!result.isOk()) {
2148 return result;
2149 }
2150 return compare_bitmaps(reference, *bitmap);
2151 }
2152 return Result::Ok();
2153 }
2154
2155 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2156
auto_compute_translate(SkMatrix * matrix,int srcW,int srcH)2157 static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
2158 SkRect bounds = SkRect::MakeIWH(srcW, srcH);
2159 matrix->mapRect(&bounds);
2160 matrix->postTranslate(-bounds.x(), -bounds.y());
2161 return {SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height())};
2162 }
2163
ViaMatrix(SkMatrix matrix,Sink * sink)2164 ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
2165
draw(const Src & src,SkBitmap * bitmap,SkWStream * stream,SkString * log) const2166 Result ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2167 SkMatrix matrix = fMatrix;
2168 SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
2169 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
2170 canvas->concat(matrix);
2171 return src.draw(nullptr, canvas);
2172 });
2173 }
2174
2175 // Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
2176 // This should be pixel-preserving.
ViaUpright(SkMatrix matrix,Sink * sink)2177 ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
2178
draw(const Src & src,SkBitmap * bitmap,SkWStream * stream,SkString * log) const2179 Result ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2180 Result result = fSink->draw(src, bitmap, stream, log);
2181 if (!result.isOk()) {
2182 return result;
2183 }
2184
2185 SkMatrix inverse;
2186 if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
2187 return Result::Fatal("Cannot upright --matrix.");
2188 }
2189 SkMatrix upright = SkMatrix::I();
2190 upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
2191 upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
2192 upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
2193 upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
2194
2195 SkBitmap uprighted;
2196 SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
2197 uprighted.allocPixels(bitmap->info().makeDimensions(size));
2198
2199 SkCanvas canvas(uprighted);
2200 canvas.concat(upright);
2201 SkPaint paint;
2202 paint.setBlendMode(SkBlendMode::kSrc);
2203 canvas.drawImage(bitmap->asImage(), 0, 0, SkSamplingOptions(), &paint);
2204
2205 *bitmap = uprighted;
2206 return Result::Ok();
2207 }
2208
2209 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2210
draw(const Src & src,SkBitmap * bitmap,SkWStream * stream,SkString * log) const2211 Result ViaSerialization::draw(
2212 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2213 // Record our Src into a picture.
2214 auto size = src.size();
2215 SkPictureRecorder recorder;
2216 Result result = src.draw(nullptr, recorder.beginRecording(SkIntToScalar(size.width()),
2217 SkIntToScalar(size.height())));
2218 if (!result.isOk()) {
2219 return result;
2220 }
2221 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
2222
2223 // Serialize it and then deserialize it.
2224 sk_sp<SkPicture> deserialized(SkPicture::MakeFromData(pic->serialize().get()));
2225
2226 result = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
2227 canvas->drawPicture(deserialized);
2228 return Result::Ok();
2229 });
2230 if (!result.isOk()) {
2231 return result;
2232 }
2233
2234 return check_against_reference(bitmap, src, fSink.get());
2235 }
2236
2237 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2238
draw(const Src & src,SkBitmap * bitmap,SkWStream * stream,SkString * log) const2239 Result ViaPicture::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2240 auto size = src.size();
2241 Result result = draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) {
2242 SkPictureRecorder recorder;
2243 sk_sp<SkPicture> pic;
2244 Result result = src.draw(nullptr, recorder.beginRecording(SkIntToScalar(size.width()),
2245 SkIntToScalar(size.height())));
2246 if (!result.isOk()) {
2247 return result;
2248 }
2249 pic = recorder.finishRecordingAsPicture();
2250 canvas->drawPicture(pic);
2251 return result;
2252 });
2253 if (!result.isOk()) {
2254 return result;
2255 }
2256
2257 return check_against_reference(bitmap, src, fSink.get());
2258 }
2259
2260 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
2261
2262 #ifdef TEST_VIA_SVG
2263 #include "include/svg/SkSVGCanvas.h"
2264 #include "modules/svg/include/SkSVGDOM.h"
2265 #include "src/xml/SkXMLWriter.h"
2266
draw(const Src & src,SkBitmap * bitmap,SkWStream * stream,SkString * log) const2267 Result ViaSVG::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
2268 auto size = src.size();
2269 return draw_to_canvas(fSink.get(), bitmap, stream, log, size, [&](SkCanvas* canvas) -> Result {
2270 SkDynamicMemoryWStream wstream;
2271 SkXMLStreamWriter writer(&wstream);
2272 Result result = src.draw(SkSVGCanvas::Make(SkRect::Make(size), &writer).get());
2273 if (!result.isOk()) {
2274 return result;
2275 }
2276 std::unique_ptr<SkStream> rstream(wstream.detachAsStream());
2277 auto dom = SkSVGDOM::MakeFromStream(*rstream);
2278 if (dom) {
2279 dom->setContainerSize(SkSize::Make(size));
2280 dom->render(canvas);
2281 }
2282 return Result::Ok();
2283 });
2284 }
2285 #endif
2286
2287 } // namespace DM
2288