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