1 // Copyright 2019 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 
4 #include "gm/gm.h"
5 #include "include/codec/SkCodec.h"
6 #include "include/core/SkCanvas.h"
7 #include "include/core/SkColorSpace.h"
8 #include "include/core/SkGraphics.h"
9 #include "include/core/SkPicture.h"
10 #include "include/core/SkPictureRecorder.h"
11 #include "include/docs/SkPDFDocument.h"
12 #include "include/gpu/GrContextOptions.h"
13 #include "include/gpu/GrDirectContext.h"
14 #include "include/private/SkTHash.h"
15 #include "modules/svg/include/SkSVGDOM.h"
16 #include "src/core/SkColorSpacePriv.h"
17 #include "src/core/SkMD5.h"
18 #include "src/core/SkOSFile.h"
19 #include "src/gpu/GrDirectContextPriv.h"
20 #include "src/gpu/GrGpu.h"
21 #include "src/utils/SkOSPath.h"
22 #include "tests/Test.h"
23 #include "tools/AutoreleasePool.h"
24 #include "tools/CrashHandler.h"
25 #include "tools/HashAndEncode.h"
26 #include "tools/ToolUtils.h"
27 #include "tools/flags/CommandLineFlags.h"
28 #include "tools/flags/CommonFlags.h"
29 #include "tools/gpu/BackendSurfaceFactory.h"
30 #include "tools/gpu/GrContextFactory.h"
31 #include "tools/gpu/MemoryCache.h"
32 #include "tools/trace/EventTracingPriv.h"
33 
34 #include <chrono>
35 #include <functional>
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 #if defined(SK_ENABLE_SKOTTIE)
40     #include "modules/skottie/include/Skottie.h"
41     #include "modules/skresources/include/SkResources.h"
42 #endif
43 
44 using sk_gpu_test::GrContextFactory;
45 
46 static DEFINE_bool(listGMs  , false, "Print GM names and exit.");
47 static DEFINE_bool(listTests, false, "Print unit test names and exit.");
48 
49 static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw.");
50 static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into.");
51 
52 static DEFINE_string(ct    ,   "8888", "The color type for any raster backend.");
53 static DEFINE_string(at    , "premul", "The alpha type for any raster backend.");
54 static DEFINE_string(gamut ,   "srgb", "The color gamut for any raster backend.");
55 static DEFINE_string(tf    ,   "srgb", "The transfer function for any raster backend.");
56 static DEFINE_bool  (legacy,    false, "Use a null SkColorSpace instead of --gamut and --tf?");
57 static DEFINE_bool  (skvm  ,    false, "Use SkVMBlitter when supported?");
58 static DEFINE_bool  (jit   ,     true, "JIT SkVM?");
59 static DEFINE_bool  (dylib ,    false, "JIT SkVM via dylib?");
60 
61 static DEFINE_int   (samples ,         0, "Samples per pixel in GPU backends.");
62 static DEFINE_bool  (stencils,      true, "If false, avoid stencil buffers in GPU backends.");
63 static DEFINE_bool  (dit     ,     false, "Use device-independent text in GPU backends.");
64 static DEFINE_string(surf    , "default", "Backing store for GPU backend surfaces.");
65 
66 static DEFINE_bool(       preAbandonGpuContext, false, "Abandon the GrContext before drawing.");
67 static DEFINE_bool(          abandonGpuContext, false, "Abandon the GrContext after drawing.");
68 static DEFINE_bool(releaseAndAbandonGpuContext, false,
69                    "Release all GPU resources and abandon the GrContext after drawing.");
70 
71 static DEFINE_bool(decodeToDst, false,
72                    "Decode images to destination format rather than suggested natural format.");
73 
74 static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI,
75                      "DPI for rasterized content in vector backends like --backend pdf.");
76 static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?");
77 
78 static DEFINE_bool   (cpuDetect, true, "Detect CPU features for runtime optimizations?");
79 static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set.");
80 static DEFINE_bool   (quick, false, "Skip image hashing and encoding?");
81 
82 static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set.");
83 
84 static DEFINE_string(key,        "", "Metadata passed through to .png encoder and .json output.");
85 static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output.");
86 
87 template <typename T>
88 struct FlagOption {
89     const char* label;
90     T           value;
91 };
92 
93 template <typename T, int N>
parse_flag(const CommandLineFlags::StringArray & flag,const char * flag_name,const FlagOption<T> (& array)[N],T * value)94 static bool parse_flag(const CommandLineFlags::StringArray& flag,
95                        const char* flag_name,
96                        const FlagOption<T> (&array)[N],
97                        T* value) {
98     for (auto entry : array) {
99         if (flag.contains(entry.label)) {
100             *value = entry.value;
101             return true;
102         }
103     }
104     fprintf(stderr, "Known values for --%s:\n", flag_name);
105     for (auto entry : array) {
106         fprintf(stderr, "    --%s %s\n", flag_name, entry.label);
107     }
108     return false;
109 }
110 
111 struct Result {
112     enum { Ok, Skip, Fail} status;
113     SkString               failure;
114 };
115 static const Result ok = {Result::Ok,   {}},
116                   skip = {Result::Skip, {}};
117 
fail(const char * why)118 static Result fail(const char* why) {
119     return { Result::Fail, SkString(why) };
120 }
121 template <typename... Args>
fail(const char * whyFmt,Args...args)122 static Result fail(const char* whyFmt, Args... args) {
123     return { Result::Fail, SkStringPrintf(whyFmt, args...) };
124 }
125 
126 
127 struct Source {
128     SkString                               name;
129     SkISize                                size;
130     std::function<Result(SkCanvas*)>       draw;
__anon9a2d9d5b0202Source131     std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
132 };
133 
init(Source * source,std::shared_ptr<skiagm::GM> gm)134 static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
135     source->size  = gm->getISize();
136     source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
137     source->draw  = [gm](SkCanvas* canvas) {
138         auto direct = GrAsDirectContext(canvas->recordingContext());
139 
140         SkString err;
141         switch (gm->gpuSetup(direct, canvas, &err)) {
142             case skiagm::DrawResult::kOk  : break;
143             case skiagm::DrawResult::kSkip: return skip;
144             case skiagm::DrawResult::kFail: return fail(err.c_str());
145         }
146 
147         switch (gm->draw(canvas, &err)) {
148             case skiagm::DrawResult::kOk:   break;
149             case skiagm::DrawResult::kSkip: return skip;
150             case skiagm::DrawResult::kFail: return fail(err.c_str());
151         }
152         return ok;
153     };
154 }
155 
init(Source * source,sk_sp<SkPicture> pic)156 static void init(Source* source, sk_sp<SkPicture> pic) {
157     source->size = pic->cullRect().roundOut().size();
158     source->draw = [pic](SkCanvas* canvas) {
159         canvas->drawPicture(pic);
160         return ok;
161     };
162 }
163 
init(Source * source,std::shared_ptr<SkCodec> codec)164 static void init(Source* source, std::shared_ptr<SkCodec> codec) {
165     source->size = codec->dimensions();
166     source->draw = [codec](SkCanvas* canvas) {
167         SkImageInfo info = codec->getInfo();
168         if (FLAGS_decodeToDst) {
169             info = canvas->imageInfo().makeDimensions(info.dimensions());
170         }
171 
172         SkBitmap bm;
173         bm.allocPixels(info);
174         switch (SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) {
175             case SkCodec::kSuccess:
176             case SkCodec::kErrorInInput:
177             case SkCodec::kIncompleteInput: canvas->drawBitmap(bm, 0,0);
178                                             break;
179             default: return fail("codec->getPixels() failed: %d\n", result);
180         }
181         return ok;
182     };
183 }
184 
init(Source * source,sk_sp<SkSVGDOM> svg)185 static void init(Source* source, sk_sp<SkSVGDOM> svg) {
186     source->size = svg->containerSize().isEmpty() ? SkISize{1000,1000}
187                                                   : svg->containerSize().toCeil();
188     source->draw = [svg](SkCanvas* canvas) {
189         svg->render(canvas);
190         return ok;
191     };
192 }
193 
194 #if defined(SK_ENABLE_SKOTTIE)
init(Source * source,sk_sp<skottie::Animation> animation)195 static void init(Source* source, sk_sp<skottie::Animation> animation) {
196     source->size = {1000,1000};
197     source->draw = [animation](SkCanvas* canvas) {
198         canvas->clear(SK_ColorWHITE);
199 
200         // Draw frames in a shuffled order to exercise nonlinear frame progression.
201         // The film strip will still be in time order, just drawn out of order.
202         const int order[] = { 4, 0, 3, 1, 2 };
203         const int tiles = SK_ARRAY_COUNT(order);
204         const float dim = 1000.0f / tiles;
205 
206         const float dt = 1.0f / (tiles*tiles - 1);
207 
208         for (int y : order)
209         for (int x : order) {
210             SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
211 
212             SkAutoCanvasRestore _(canvas, /*doSave=*/true);
213             canvas->clipRect(dst, /*doAntiAlias=*/true);
214             canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
215                                                     dst,
216                                                     SkMatrix::kCenter_ScaleToFit));
217             float t = (y*tiles + x) * dt;
218             animation->seek(t);
219             animation->render(canvas);
220         }
221         return ok;
222     };
223 }
224 #endif
225 
init(Source * source,const skiatest::Test & test)226 static void init(Source* source, const skiatest::Test& test) {
227     source->size  = {1,1};
228     source->draw  = [test](SkCanvas* canvas) {
229         struct Reporter : public skiatest::Reporter {
230             SkString msg;
231 
232             void reportFailed(const skiatest::Failure& failure) override {
233                 msg += failure.toString();
234                 msg += "\n";
235             }
236         } reporter;
237 
238         test.run(&reporter, GrContextOptions{});
239 
240         if (reporter.msg.isEmpty()) {
241             canvas->clear(SK_ColorGREEN);
242             return ok;
243         }
244 
245         canvas->clear(SK_ColorRED);
246         return fail(reporter.msg.c_str());
247     };
248 }
249 
draw_with_cpu(std::function<bool (SkCanvas *)> draw,SkImageInfo info)250 static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
251                                     SkImageInfo info) {
252     if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
253         if (draw(surface->getCanvas())) {
254             return surface->makeImageSnapshot();
255         }
256     }
257     return nullptr;
258 }
259 
draw_as_skp(std::function<bool (SkCanvas *)> draw,SkImageInfo info)260 static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
261                                  SkImageInfo info) {
262     SkPictureRecorder recorder;
263     if (draw(recorder.beginRecording(info.width(), info.height()))) {
264         return recorder.finishRecordingAsPicture()->serialize();
265     }
266     return nullptr;
267 }
268 
draw_as_pdf(std::function<bool (SkCanvas *)> draw,SkImageInfo info,SkString name)269 static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
270                                  SkImageInfo info,
271                                  SkString name) {
272     SkPDF::Metadata metadata;
273     metadata.fTitle     = name;
274     metadata.fCreator   = "Skia/FM";
275     metadata.fRasterDPI = FLAGS_rasterDPI;
276     metadata.fPDFA      = FLAGS_PDFA;
277 
278     SkDynamicMemoryWStream stream;
279     if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
280         if (draw(doc->beginPage(info.width(), info.height()))) {
281             doc->endPage();
282             doc->close();
283             return stream.detachAsData();
284         }
285     }
286     return nullptr;
287 }
288 
draw_with_gpu(std::function<bool (SkCanvas *)> draw,SkImageInfo info,GrContextFactory::ContextType api,GrContextFactory * factory)289 static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
290                                     SkImageInfo info,
291                                     GrContextFactory::ContextType api,
292                                     GrContextFactory* factory) {
293     enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
294     const FlagOption<SurfaceType> kSurfaceTypes[] = {
295         { "default", SurfaceType::kDefault },
296         { "betex"  , SurfaceType::kBackendTexture },
297         { "bert"   , SurfaceType::kBackendRenderTarget },
298     };
299     SurfaceType surfaceType;
300     if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
301         return nullptr;
302     }
303 
304     auto overrides = GrContextFactory::ContextOverrides::kNone;
305     if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
306 
307     auto context = factory->getContextInfo(api, overrides).directContext();
308 
309     uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
310                                : 0;
311     SkSurfaceProps props(flags, kRGB_H_SkPixelGeometry);
312 
313     sk_sp<SkSurface> surface;
314 
315     switch (surfaceType) {
316         case SurfaceType::kDefault:
317             surface = SkSurface::MakeRenderTarget(context,
318                                                   SkBudgeted::kNo,
319                                                   info,
320                                                   FLAGS_samples,
321                                                   &props);
322             break;
323 
324         case SurfaceType::kBackendTexture:
325             surface = sk_gpu_test::MakeBackendTextureSurface(context,
326                                                              info,
327                                                              kTopLeft_GrSurfaceOrigin,
328                                                              FLAGS_samples,
329                                                              GrMipmapped::kNo,
330                                                              GrProtected::kNo,
331                                                              &props);
332             break;
333 
334         case SurfaceType::kBackendRenderTarget:
335             surface = sk_gpu_test::MakeBackendRenderTargetSurface(context,
336                                                                   info,
337                                                                   kBottomLeft_GrSurfaceOrigin,
338                                                                   FLAGS_samples,
339                                                                   GrProtected::kNo,
340                                                                   &props);
341             break;
342     }
343 
344     if (!surface) {
345         fprintf(stderr, "Could not create GPU surface.\n");
346         return nullptr;
347     }
348 
349     if (FLAGS_preAbandonGpuContext) {
350         factory->abandonContexts();
351     }
352 
353     sk_sp<SkImage> image;
354     if (draw(surface->getCanvas())) {
355         image = surface->makeImageSnapshot();
356     }
357 
358     if (FLAGS_abandonGpuContext) {
359         factory->abandonContexts();
360     } else if (FLAGS_releaseAndAbandonGpuContext) {
361         factory->releaseResourcesAndAbandonContexts();
362     }
363 
364     return image;
365 }
366 
367 extern bool gUseSkVMBlitter;
368 extern bool gSkVMAllowJIT;
369 extern bool gSkVMJITViaDylib;
370 
main(int argc,char ** argv)371 int main(int argc, char** argv) {
372     CommandLineFlags::Parse(argc, argv);
373     SetupCrashHandler();
374 
375     if (FLAGS_cpuDetect) {
376         SkGraphics::Init();
377     }
378     gUseSkVMBlitter  = FLAGS_skvm;
379     gSkVMAllowJIT    = FLAGS_jit;
380     gSkVMJITViaDylib = FLAGS_dylib;
381 
382     initializeEventTracingForTools();
383     ToolUtils::SetDefaultFontMgr();
384     SetAnalyticAAFromCommonFlags();
385 
386     GrContextOptions baseOptions;
387     SetCtxOptionsFromCommonFlags(&baseOptions);
388 
389     sk_gpu_test::MemoryCache memoryCache;
390     if (!FLAGS_writeShaders.isEmpty()) {
391         baseOptions.fPersistentCache = &memoryCache;
392         baseOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
393     }
394 
395     SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
396     for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
397         std::unique_ptr<skiagm::GM> gm{factory()};
398         if (FLAGS_listGMs) {
399             fprintf(stdout, "%s\n", gm->getName());
400         } else {
401             gm_factories.set(SkString{gm->getName()}, factory);
402         }
403     }
404 
405     SkTHashMap<SkString, const skiatest::Test*> tests;
406     for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
407         if (test.needsGpu) {
408             continue;  // TODO
409         }
410         if (FLAGS_listTests) {
411             fprintf(stdout, "%s\n", test.name);
412         } else {
413             tests.set(SkString{test.name}, &test);
414         }
415     }
416 
417     if (FLAGS_listGMs || FLAGS_listTests) {
418         return 0;
419     }
420     if (FLAGS_sources.isEmpty()) {
421         fprintf(stderr, "Please give me something to run using -s/--sources!\n");
422         return 1;
423     }
424 
425     SkTArray<Source> sources;
426     for (const SkString& name : FLAGS_sources) {
427         Source* source = &sources.push_back();
428 
429         if (skiagm::GMFactory* factory = gm_factories.find(name)) {
430             std::shared_ptr<skiagm::GM> gm{(*factory)()};
431             source->name = name;
432             init(source, std::move(gm));
433             continue;
434         }
435 
436         if (const skiatest::Test** test = tests.find(name)) {
437             source->name = name;
438             init(source, **test);
439             continue;
440         }
441 
442         if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
443             source->name = SkOSPath::Basename(name.c_str());
444 
445             if (name.endsWith(".skp")) {
446                 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
447                     init(source, pic);
448                     continue;
449                 }
450             } else if (name.endsWith(".svg")) {
451                 SkMemoryStream stream{blob};
452                 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
453                     init(source, svg);
454                     continue;
455                 }
456             }
457 #if defined(SK_ENABLE_SKOTTIE)
458             else if (name.endsWith(".json")) {
459                 const SkString dir  = SkOSPath::Dirname(name.c_str());
460                 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
461                         .setResourceProvider(skresources::FileResourceProvider::Make(dir))
462                         .make((const char*)blob->data(), blob->size())) {
463                     init(source, animation);
464                     continue;
465                 }
466             }
467 #endif
468             else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
469                 init(source, codec);
470                 continue;
471             }
472         }
473 
474         fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
475         return 1;
476     }
477 
478     enum NonGpuBackends {
479         kCPU_Backend = -1,
480         kSKP_Backend = -2,
481         kPDF_Backend = -3,
482     };
483     const FlagOption<int> kBackends[] = {
484         { "cpu"            , kCPU_Backend },
485         { "skp"            , kSKP_Backend },
486         { "pdf"            , kPDF_Backend },
487         { "gl"             , GrContextFactory::kGL_ContextType },
488         { "gles"           , GrContextFactory::kGLES_ContextType },
489         { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
490         { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
491         { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
492         { "angle_gl_es2"   , GrContextFactory::kANGLE_GL_ES2_ContextType },
493         { "angle_gl_es3"   , GrContextFactory::kANGLE_GL_ES3_ContextType },
494         { "commandbuffer"  , GrContextFactory::kCommandBuffer_ContextType },
495         { "vk"             , GrContextFactory::kVulkan_ContextType },
496         { "mtl"            , GrContextFactory::kMetal_ContextType },
497         { "mock"           , GrContextFactory::kMock_ContextType },
498     };
499     const FlagOption<SkColorType> kColorTypes[] = {
500         { "a8",                  kAlpha_8_SkColorType },
501         { "g8",                   kGray_8_SkColorType },
502         { "565",                 kRGB_565_SkColorType },
503         { "4444",              kARGB_4444_SkColorType },
504         { "8888",                    kN32_SkColorType },
505         { "888x",               kRGB_888x_SkColorType },
506         { "1010102",        kRGBA_1010102_SkColorType },
507         { "101010x",         kRGB_101010x_SkColorType },
508         { "bgra1010102",    kBGRA_1010102_SkColorType },
509         { "bgr101010x",      kBGR_101010x_SkColorType },
510         { "f16norm",        kRGBA_F16Norm_SkColorType },
511         { "f16",                kRGBA_F16_SkColorType },
512         { "f32",                kRGBA_F32_SkColorType },
513         { "rgba",              kRGBA_8888_SkColorType },
514         { "bgra",              kBGRA_8888_SkColorType },
515         { "16161616", kR16G16B16A16_unorm_SkColorType },
516     };
517     const FlagOption<SkAlphaType> kAlphaTypes[] = {
518         {   "premul",   kPremul_SkAlphaType },
519         { "unpremul", kUnpremul_SkAlphaType },
520     };
521     const FlagOption<skcms_Matrix3x3> kGamuts[] = {
522         { "srgb",    SkNamedGamut::kSRGB },
523         { "p3",      SkNamedGamut::kDisplayP3 },
524         { "rec2020", SkNamedGamut::kRec2020 },
525         { "adobe",   SkNamedGamut::kAdobeRGB },
526         { "narrow",  gNarrow_toXYZD50},
527     };
528     const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
529         { "srgb"   , SkNamedTransferFn::kSRGB },
530         { "rec2020", SkNamedTransferFn::kRec2020 },
531         { "2.2"    , SkNamedTransferFn::k2Dot2 },
532         { "linear" , SkNamedTransferFn::kLinear },
533     };
534 
535 
536     int                      backend;
537     SkColorType              ct;
538     SkAlphaType              at;
539     skcms_Matrix3x3          gamut;
540     skcms_TransferFunction   tf;
541 
542     if (!parse_flag(FLAGS_backend, "backend", kBackends         , &backend) ||
543         !parse_flag(FLAGS_ct     , "ct"     , kColorTypes       , &ct)      ||
544         !parse_flag(FLAGS_at     , "at"     , kAlphaTypes       , &at)      ||
545         !parse_flag(FLAGS_gamut  , "gamut"  , kGamuts           , &gamut)   ||
546         !parse_flag(FLAGS_tf     , "tf"     , kTransferFunctions, &tf)) {
547         return 1;
548     }
549 
550     sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
551                                           : SkColorSpace::MakeRGB(tf,gamut);
552     const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
553 
554     AutoreleasePool pool;
555     for (auto source : sources) {
556         const auto start = std::chrono::steady_clock::now();
557         fprintf(stdout, "%50s", source.name.c_str());
558         fflush(stdout);
559 
560         const SkImageInfo info = unsized_info.makeDimensions(source.size);
561 
562         auto draw = [&source](SkCanvas* canvas) {
563             Result result = source.draw(canvas);
564             switch (result.status) {
565                 case Result::Ok:   break;
566                 case Result::Skip: return false;
567                 case Result::Fail:
568                     SK_ABORT("%s", result.failure.c_str());
569             }
570             return true;
571         };
572 
573         GrContextOptions options = baseOptions;
574         source.tweak(&options);
575         GrContextFactory factory(options);  // N.B. factory must outlive image
576 
577         sk_sp<SkImage> image;
578         sk_sp<SkData>  blob;
579         const char*    ext = ".png";
580         switch (backend) {
581             case kCPU_Backend:
582                 image = draw_with_cpu(draw, info);
583                 break;
584             case kSKP_Backend:
585                 blob = draw_as_skp(draw, info);
586                 ext  = ".skp";
587                 break;
588             case kPDF_Backend:
589                 blob = draw_as_pdf(draw, info, source.name);
590                 ext  = ".pdf";
591                 break;
592             default:
593                 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
594                 break;
595         }
596 
597         if (!image && !blob) {
598             fprintf(stdout, "\tskipped\n");
599             continue;
600         }
601 
602         // We read back a bitmap even when --quick is set and we won't use it,
603         // to keep us honest about deferred work, flushing pipelines, etc.
604         SkBitmap bitmap;
605         if (image && !image->asLegacyBitmap(&bitmap)) {
606             SK_ABORT("SkImage::asLegacyBitmap() failed.");
607         }
608 
609         SkString md5;
610         if (!FLAGS_quick) {
611             HashAndEncode hashAndEncode{bitmap};
612             {
613                 SkMD5 hash;
614                 if (image) {
615                     hashAndEncode.feedHash(&hash);
616                 } else {
617                     hash.write(blob->data(), blob->size());
618                 }
619 
620                 SkMD5::Digest digest = hash.finish();
621                 for (int i = 0; i < 16; i++) {
622                     md5.appendf("%02x", digest.data[i]);
623                 }
624             }
625 
626             if (!FLAGS_writePath.isEmpty()) {
627                 sk_mkdir(FLAGS_writePath[0]);
628                 SkString path = SkStringPrintf("%s/%s%s",
629                                                FLAGS_writePath[0], source.name.c_str(), ext);
630 
631                 SkFILEWStream file(path.c_str());
632                 if (image) {
633                     if (!hashAndEncode.encodePNG(&file, md5.c_str(),
634                                                  FLAGS_key, FLAGS_properties)) {
635                         SK_ABORT("Could not write .png.");
636                     }
637                 } else {
638                     file.write(blob->data(), blob->size());
639                 }
640             }
641         }
642 
643         const auto elapsed = std::chrono::steady_clock::now() - start;
644         fprintf(stdout, "\t%s\t%7dms\n",
645                 md5.c_str(),
646                 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
647         pool.drain();
648     }
649 
650     if (!FLAGS_writeShaders.isEmpty()) {
651         sk_mkdir(FLAGS_writeShaders[0]);
652         GrBackendApi api =
653                 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
654         memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
655 
656     }
657 
658     return 0;
659 }
660