blob: 75f1389eaa83a93bf24e6ba5a7f7f1dcd578aa1a [file] [log] [blame]
Mike Klein735c7ba2019-03-25 12:57:58 -05001// 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004#include "experimental/svg/model/SkSVGDOM.h"
5#include "gm/gm.h"
6#include "include/codec/SkCodec.h"
Robert Phillipse19babf2020-04-06 13:57:30 -04007#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkColorSpace.h"
9#include "include/core/SkGraphics.h"
10#include "include/core/SkPicture.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/docs/SkPDFDocument.h"
13#include "include/gpu/GrContextOptions.h"
14#include "include/private/SkTHash.h"
15#include "src/core/SkColorSpacePriv.h"
16#include "src/core/SkMD5.h"
17#include "src/core/SkOSFile.h"
18#include "src/gpu/GrContextPriv.h"
19#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/utils/SkOSPath.h"
Mike Kleinfee00792019-11-21 16:18:47 -060021#include "tests/Test.h"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040022#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/CrashHandler.h"
24#include "tools/HashAndEncode.h"
25#include "tools/ToolUtils.h"
26#include "tools/flags/CommandLineFlags.h"
27#include "tools/flags/CommonFlags.h"
28#include "tools/gpu/GrContextFactory.h"
29#include "tools/gpu/MemoryCache.h"
30#include "tools/trace/EventTracingPriv.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050031#include <chrono>
32#include <functional>
33#include <stdio.h>
34#include <stdlib.h>
35
Brian Osmanca946562019-04-10 13:51:53 -040036#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037 #include "modules/skottie/include/Skottie.h"
Brian Osman849f4d62019-11-26 08:58:26 -050038 #include "modules/skresources/include/SkResources.h"
Brian Osmanca946562019-04-10 13:51:53 -040039#endif
40
Mike Klein735c7ba2019-03-25 12:57:58 -050041using sk_gpu_test::GrContextFactory;
42
Mike Kleinfee00792019-11-21 16:18:47 -060043static DEFINE_bool(listGMs , false, "Print GM names and exit.");
44static DEFINE_bool(listTests, false, "Print unit test names and exit.");
45
Mike Klein735c7ba2019-03-25 12:57:58 -050046static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw.");
47static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into.");
48
Mike Klein4daf6372019-04-03 11:19:45 -040049static DEFINE_string(ct , "8888", "The color type for any raster backend.");
50static DEFINE_string(at , "premul", "The alpha type for any raster backend.");
51static DEFINE_string(gamut , "srgb", "The color gamut for any raster backend.");
52static DEFINE_string(tf , "srgb", "The transfer function for any raster backend.");
53static DEFINE_bool (legacy, false, "Use a null SkColorSpace instead of --gamut and --tf?");
Mike Klein2a57e792020-01-14 12:47:52 -060054static DEFINE_bool (skvm , false, "Use SkVMBlitter when supported?");
Mike Klein31bfa832020-01-22 11:36:40 -060055static DEFINE_bool (dylib , false, "Use SkVM via dylib?");
Mike Klein735c7ba2019-03-25 12:57:58 -050056
57static DEFINE_int (samples , 0, "Samples per pixel in GPU backends.");
Mike Klein735c7ba2019-03-25 12:57:58 -050058static DEFINE_bool (stencils, true, "If false, avoid stencil buffers in GPU backends.");
59static DEFINE_bool (dit , false, "Use device-independent text in GPU backends.");
60static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces.");
61
62static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing.");
63static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing.");
64static DEFINE_bool(releaseAndAbandonGpuContext, false,
65 "Release all GPU resources and abandon the GrContext after drawing.");
66
67static DEFINE_bool(decodeToDst, false,
68 "Decode images to destination format rather than suggested natural format.");
69
Mike Kleinc245bd92019-03-27 14:31:09 -050070static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI,
71 "DPI for rasterized content in vector backends like --backend pdf.");
72static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?");
73
Mike Klein735c7ba2019-03-25 12:57:58 -050074static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?");
75static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set.");
Mike Kleined2cec42019-11-01 10:51:01 -050076static DEFINE_bool (quick, false, "Skip image hashing and encoding?");
Mike Klein735c7ba2019-03-25 12:57:58 -050077
Brian Osman5aa11fb2019-04-08 16:40:36 -040078static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set.");
79
Mike Klein735c7ba2019-03-25 12:57:58 -050080static DEFINE_string(key, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein7b8bc532019-04-09 11:19:47 -050081static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein735c7ba2019-03-25 12:57:58 -050082
83template <typename T>
84struct FlagOption {
85 const char* label;
86 T value;
87};
88
89template <typename T, int N>
90static bool parse_flag(const CommandLineFlags::StringArray& flag,
91 const char* flag_name,
92 const FlagOption<T> (&array)[N],
93 T* value) {
94 for (auto entry : array) {
95 if (flag.contains(entry.label)) {
96 *value = entry.value;
97 return true;
98 }
99 }
100 fprintf(stderr, "Known values for --%s:\n", flag_name);
101 for (auto entry : array) {
102 fprintf(stderr, " --%s %s\n", flag_name, entry.label);
103 }
104 return false;
105}
106
Mike Kleina833cff2019-04-09 11:52:40 -0500107struct Result {
108 enum { Ok, Skip, Fail} status;
109 SkString failure;
110};
111static const Result ok = {Result::Ok, {}},
112 skip = {Result::Skip, {}};
113
Adlai Holler684838f2020-05-12 10:41:04 -0400114static Result fail(const char* why) {
115 return { Result::Fail, SkString(why) };
116}
Mike Kleina833cff2019-04-09 11:52:40 -0500117template <typename... Args>
Adlai Holler684838f2020-05-12 10:41:04 -0400118static Result fail(const char* whyFmt, Args... args) {
119 return { Result::Fail, SkStringPrintf(whyFmt, args...) };
Mike Kleina833cff2019-04-09 11:52:40 -0500120}
121
122
Mike Klein735c7ba2019-03-25 12:57:58 -0500123struct Source {
124 SkString name;
125 SkISize size;
Mike Kleina833cff2019-04-09 11:52:40 -0500126 std::function<Result(SkCanvas*)> draw;
127 std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
Mike Klein735c7ba2019-03-25 12:57:58 -0500128};
129
Mike Kleina833cff2019-04-09 11:52:40 -0500130static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
131 source->size = gm->getISize();
132 source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
133 source->draw = [gm](SkCanvas* canvas) {
134 SkString err;
135 switch (gm->draw(canvas, &err)) {
136 case skiagm::DrawResult::kOk: break;
137 case skiagm::DrawResult::kSkip: return skip;
138 case skiagm::DrawResult::kFail: return fail(err.c_str());
139 }
140 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500141 };
142}
143
Mike Kleina833cff2019-04-09 11:52:40 -0500144static void init(Source* source, sk_sp<SkPicture> pic) {
145 source->size = pic->cullRect().roundOut().size();
146 source->draw = [pic](SkCanvas* canvas) {
147 canvas->drawPicture(pic);
148 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500149 };
150}
151
Mike Kleina833cff2019-04-09 11:52:40 -0500152static void init(Source* source, std::shared_ptr<SkCodec> codec) {
153 source->size = codec->dimensions();
154 source->draw = [codec](SkCanvas* canvas) {
155 SkImageInfo info = codec->getInfo();
156 if (FLAGS_decodeToDst) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400157 info = canvas->imageInfo().makeDimensions(info.dimensions());
Mike Kleina833cff2019-04-09 11:52:40 -0500158 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500159
Mike Kleina833cff2019-04-09 11:52:40 -0500160 SkBitmap bm;
161 bm.allocPixels(info);
162 switch (SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) {
163 case SkCodec::kSuccess:
164 case SkCodec::kErrorInInput:
165 case SkCodec::kIncompleteInput: canvas->drawBitmap(bm, 0,0);
166 break;
167 default: return fail("codec->getPixels() failed: %d\n", result);
168 }
169 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500170 };
171}
172
Mike Kleina833cff2019-04-09 11:52:40 -0500173static void init(Source* source, sk_sp<SkSVGDOM> svg) {
174 source->size = svg->containerSize().isEmpty() ? SkISize{1000,1000}
175 : svg->containerSize().toCeil();
176 source->draw = [svg](SkCanvas* canvas) {
177 svg->render(canvas);
178 return ok;
Mike Klein22924262019-04-02 14:14:56 -0400179 };
180}
181
Brian Osmanca946562019-04-10 13:51:53 -0400182#if defined(SK_ENABLE_SKOTTIE)
Mike Kleina833cff2019-04-09 11:52:40 -0500183static void init(Source* source, sk_sp<skottie::Animation> animation) {
184 source->size = {1000,1000};
185 source->draw = [animation](SkCanvas* canvas) {
186 canvas->clear(SK_ColorWHITE);
Mike Klein22924262019-04-02 14:14:56 -0400187
Mike Kleina833cff2019-04-09 11:52:40 -0500188 // Draw frames in a shuffled order to exercise nonlinear frame progression.
189 // The film strip will still be in time order, just drawn out of order.
190 const int order[] = { 4, 0, 3, 1, 2 };
191 const int tiles = SK_ARRAY_COUNT(order);
192 const float dim = 1000.0f / tiles;
Mike Klein22924262019-04-02 14:14:56 -0400193
Mike Kleina833cff2019-04-09 11:52:40 -0500194 const float dt = 1.0f / (tiles*tiles - 1);
Mike Klein22924262019-04-02 14:14:56 -0400195
Mike Kleina833cff2019-04-09 11:52:40 -0500196 for (int y : order)
197 for (int x : order) {
198 SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
Mike Klein22924262019-04-02 14:14:56 -0400199
Mike Kleina833cff2019-04-09 11:52:40 -0500200 SkAutoCanvasRestore _(canvas, true/*save now*/);
201 canvas->clipRect(dst, /*aa=*/true);
202 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
203 dst,
204 SkMatrix::kCenter_ScaleToFit));
205 float t = (y*tiles + x) * dt;
206 animation->seek(t);
207 animation->render(canvas);
208 }
209 return ok;
Mike Klein6253d902019-03-27 15:09:12 -0500210 };
211}
Brian Osmanca946562019-04-10 13:51:53 -0400212#endif
Mike Klein6253d902019-03-27 15:09:12 -0500213
Mike Kleinfee00792019-11-21 16:18:47 -0600214static void init(Source* source, const skiatest::Test& test) {
215 source->size = {1,1};
216 source->draw = [test](SkCanvas* canvas) {
217 struct Reporter : public skiatest::Reporter {
218 SkString msg;
219
220 void reportFailed(const skiatest::Failure& failure) override {
Mike Klein484b63b2020-04-15 12:43:12 -0500221 msg += failure.toString();
222 msg += "\n";
Mike Kleinfee00792019-11-21 16:18:47 -0600223 }
224 } reporter;
225
226 test.run(&reporter, GrContextOptions{});
227
228 if (reporter.msg.isEmpty()) {
229 canvas->clear(SK_ColorGREEN);
230 return ok;
231 }
232
233 canvas->clear(SK_ColorRED);
234 return fail(reporter.msg.c_str());
235 };
236}
237
Mike Kleina5c27172019-04-02 10:26:48 -0400238static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500239 SkImageInfo info) {
240 if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400241 if (draw(surface->getCanvas())) {
242 return surface->makeImageSnapshot();
243 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500244 }
245 return nullptr;
246}
247
Mike Kleina5c27172019-04-02 10:26:48 -0400248static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
Mike Klein9b462092019-03-27 13:52:35 -0500249 SkImageInfo info) {
250 SkPictureRecorder recorder;
Mike Kleina5c27172019-04-02 10:26:48 -0400251 if (draw(recorder.beginRecording(info.width(), info.height()))) {
252 return recorder.finishRecordingAsPicture()->serialize();
253 }
254 return nullptr;
Mike Klein9b462092019-03-27 13:52:35 -0500255}
256
Mike Kleina5c27172019-04-02 10:26:48 -0400257static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
Mike Kleinc245bd92019-03-27 14:31:09 -0500258 SkImageInfo info,
259 SkString name) {
260 SkPDF::Metadata metadata;
261 metadata.fTitle = name;
262 metadata.fCreator = "Skia/FM";
263 metadata.fRasterDPI = FLAGS_rasterDPI;
264 metadata.fPDFA = FLAGS_PDFA;
265
266 SkDynamicMemoryWStream stream;
267 if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400268 if (draw(doc->beginPage(info.width(), info.height()))) {
269 doc->endPage();
270 doc->close();
271 return stream.detachAsData();
272 }
Mike Kleinc245bd92019-03-27 14:31:09 -0500273 }
274 return nullptr;
275}
276
Mike Kleina5c27172019-04-02 10:26:48 -0400277static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500278 SkImageInfo info,
279 GrContextFactory::ContextType api,
280 GrContextFactory* factory) {
281 enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
282 const FlagOption<SurfaceType> kSurfaceTypes[] = {
283 { "default", SurfaceType::kDefault },
284 { "betex" , SurfaceType::kBackendTexture },
285 { "bert" , SurfaceType::kBackendRenderTarget },
286 };
287 SurfaceType surfaceType;
288 if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
289 return nullptr;
290 }
291
Chris Daltonb3c97452019-06-25 20:07:56 -0600292 auto overrides = GrContextFactory::ContextOverrides::kNone;
Mike Klein735c7ba2019-03-25 12:57:58 -0500293 if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
294
295 GrContext* context = factory->getContextInfo(api, overrides)
296 .grContext();
297
298 uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
299 : 0;
300 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
301
302 sk_sp<SkSurface> surface;
303 GrBackendTexture backendTexture;
304 GrBackendRenderTarget backendRT;
305
306 switch (surfaceType) {
307 case SurfaceType::kDefault:
308 surface = SkSurface::MakeRenderTarget(context,
309 SkBudgeted::kNo,
310 info,
311 FLAGS_samples,
312 &props);
313 break;
314
315 case SurfaceType::kBackendTexture:
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400316 backendTexture = context->createBackendTexture(info.width(),
317 info.height(),
318 info.colorType(),
319 GrMipMapped::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400320 GrRenderable::kYes,
321 GrProtected::kNo);
Mike Klein735c7ba2019-03-25 12:57:58 -0500322 surface = SkSurface::MakeFromBackendTexture(context,
323 backendTexture,
324 kTopLeft_GrSurfaceOrigin,
325 FLAGS_samples,
326 info.colorType(),
327 info.refColorSpace(),
328 &props);
329 break;
330
331 case SurfaceType::kBackendRenderTarget:
332 backendRT = context->priv().getGpu()
333 ->createTestingOnlyBackendRenderTarget(info.width(),
334 info.height(),
335 SkColorTypeToGrColorType(info.colorType()));
336 surface = SkSurface::MakeFromBackendRenderTarget(context,
337 backendRT,
338 kBottomLeft_GrSurfaceOrigin,
339 info.colorType(),
340 info.refColorSpace(),
341 &props);
342 break;
343 }
344
345 if (!surface) {
346 fprintf(stderr, "Could not create GPU surface.\n");
347 return nullptr;
348 }
349
350 if (FLAGS_preAbandonGpuContext) {
351 factory->abandonContexts();
352 }
353
Mike Kleina5c27172019-04-02 10:26:48 -0400354 sk_sp<SkImage> image;
355 if (draw(surface->getCanvas())) {
356 image = surface->makeImageSnapshot();
357 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500358
359 if (FLAGS_abandonGpuContext) {
360 factory->abandonContexts();
361 } else if (FLAGS_releaseAndAbandonGpuContext) {
362 factory->releaseResourcesAndAbandonContexts();
363 }
364
365 if (!context->abandoned()) {
366 surface.reset();
367 if (backendTexture.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400368 context->deleteBackendTexture(backendTexture);
Mike Klein735c7ba2019-03-25 12:57:58 -0500369 }
370 if (backendRT.isValid()) {
371 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
372 }
373 }
374
375 return image;
376}
377
Mike Klein2a57e792020-01-14 12:47:52 -0600378extern bool gUseSkVMBlitter;
Mike Klein31bfa832020-01-22 11:36:40 -0600379extern bool gSkVMJITViaDylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600380
Mike Klein735c7ba2019-03-25 12:57:58 -0500381int main(int argc, char** argv) {
382 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500383 SetupCrashHandler();
Mike Klein735c7ba2019-03-25 12:57:58 -0500384
385 if (FLAGS_cpuDetect) {
386 SkGraphics::Init();
387 }
Mike Klein31bfa832020-01-22 11:36:40 -0600388 gUseSkVMBlitter = FLAGS_skvm;
389 gSkVMJITViaDylib = FLAGS_dylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600390
Mike Klein735c7ba2019-03-25 12:57:58 -0500391 initializeEventTracingForTools();
392 ToolUtils::SetDefaultFontMgr();
393 SetAnalyticAAFromCommonFlags();
394
395 GrContextOptions baseOptions;
396 SetCtxOptionsFromCommonFlags(&baseOptions);
397
Brian Osman5aa11fb2019-04-08 16:40:36 -0400398 sk_gpu_test::MemoryCache memoryCache;
399 if (!FLAGS_writeShaders.isEmpty()) {
400 baseOptions.fPersistentCache = &memoryCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400401 baseOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5aa11fb2019-04-08 16:40:36 -0400402 }
403
Mike Kleincbe93ee2019-04-02 17:00:54 -0400404 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500405 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Hal Canaryedda5652019-08-05 10:28:09 -0400406 std::unique_ptr<skiagm::GM> gm{factory()};
Mike Kleinfee00792019-11-21 16:18:47 -0600407 if (FLAGS_listGMs) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500408 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400409 } else {
410 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500411 }
412 }
Mike Kleinfee00792019-11-21 16:18:47 -0600413
414 SkTHashMap<SkString, const skiatest::Test*> tests;
415 for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
416 if (test.needsGpu) {
417 continue; // TODO
418 }
419 if (FLAGS_listTests) {
420 fprintf(stdout, "%s\n", test.name);
421 } else {
422 tests.set(SkString{test.name}, &test);
423 }
424 }
425
426 if (FLAGS_listGMs || FLAGS_listTests) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400427 return 0;
428 }
Mike Kleinfee00792019-11-21 16:18:47 -0600429 if (FLAGS_sources.isEmpty()) {
430 fprintf(stderr, "Please give me something to run using -s/--sources!\n");
431 return 1;
432 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400433
434 SkTArray<Source> sources;
Mike Kleina833cff2019-04-09 11:52:40 -0500435 for (const SkString& name : FLAGS_sources) {
436 Source* source = &sources.push_back();
437
438 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Hal Canaryedda5652019-08-05 10:28:09 -0400439 std::shared_ptr<skiagm::GM> gm{(*factory)()};
Mike Kleina833cff2019-04-09 11:52:40 -0500440 source->name = name;
Ben Wagner406ff502019-08-12 16:39:24 -0400441 init(source, std::move(gm));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400442 continue;
443 }
444
Mike Kleinfee00792019-11-21 16:18:47 -0600445 if (const skiatest::Test** test = tests.find(name)) {
446 source->name = name;
447 init(source, **test);
448 continue;
449 }
450
Mike Kleina833cff2019-04-09 11:52:40 -0500451 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
452 source->name = SkOSPath::Basename(name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500453
Mike Kleince90d6f2019-03-29 11:14:14 -0500454 if (name.endsWith(".skp")) {
455 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500456 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400457 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500458 }
459 } else if (name.endsWith(".svg")) {
460 SkMemoryStream stream{blob};
461 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500462 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400463 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500464 }
Brian Osmanca946562019-04-10 13:51:53 -0400465 }
466#if defined(SK_ENABLE_SKOTTIE)
467 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500468 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400469 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
Brian Osman849f4d62019-11-26 08:58:26 -0500470 .setResourceProvider(skresources::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400471 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500472 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400473 continue;
Mike Klein22924262019-04-02 14:14:56 -0400474 }
Brian Osmanca946562019-04-10 13:51:53 -0400475 }
476#endif
477 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500478 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400479 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500480 }
481 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400482
Mike Kleina833cff2019-04-09 11:52:40 -0500483 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400484 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500485 }
486
Mike Klein9b462092019-03-27 13:52:35 -0500487 enum NonGpuBackends {
488 kCPU_Backend = -1,
489 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500490 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500491 };
492 const FlagOption<int> kBackends[] = {
493 { "cpu" , kCPU_Backend },
494 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500495 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500496 { "gl" , GrContextFactory::kGL_ContextType },
497 { "gles" , GrContextFactory::kGLES_ContextType },
498 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
499 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
500 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
501 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
502 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
503 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
504 { "vk" , GrContextFactory::kVulkan_ContextType },
505 { "mtl" , GrContextFactory::kMetal_ContextType },
506 { "mock" , GrContextFactory::kMock_ContextType },
507 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500508 const FlagOption<SkColorType> kColorTypes[] = {
Mike Kleinf7eb0542020-02-11 12:19:08 -0600509 { "a8", kAlpha_8_SkColorType },
510 { "g8", kGray_8_SkColorType },
511 { "565", kRGB_565_SkColorType },
512 { "4444", kARGB_4444_SkColorType },
513 { "8888", kN32_SkColorType },
514 { "888x", kRGB_888x_SkColorType },
515 { "1010102", kRGBA_1010102_SkColorType },
516 { "101010x", kRGB_101010x_SkColorType },
517 { "bgra1010102", kBGRA_1010102_SkColorType },
518 { "bgr101010x", kBGR_101010x_SkColorType },
519 { "f16norm", kRGBA_F16Norm_SkColorType },
520 { "f16", kRGBA_F16_SkColorType },
521 { "f32", kRGBA_F32_SkColorType },
522 { "rgba", kRGBA_8888_SkColorType },
523 { "bgra", kBGRA_8888_SkColorType },
Mike Klein735c7ba2019-03-25 12:57:58 -0500524 };
525 const FlagOption<SkAlphaType> kAlphaTypes[] = {
526 { "premul", kPremul_SkAlphaType },
527 { "unpremul", kUnpremul_SkAlphaType },
528 };
529 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
530 { "srgb", SkNamedGamut::kSRGB },
Mike Kleinb147ace2020-01-16 11:11:06 -0600531 { "p3", SkNamedGamut::kDisplayP3 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500532 { "rec2020", SkNamedGamut::kRec2020 },
533 { "adobe", SkNamedGamut::kAdobeRGB },
534 { "narrow", gNarrow_toXYZD50},
535 };
536 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
537 { "srgb" , SkNamedTransferFn::kSRGB },
Hal Canarybe67a172019-05-24 10:13:36 -0400538 { "rec2020", SkNamedTransferFn::kRec2020 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500539 { "2.2" , SkNamedTransferFn::k2Dot2 },
540 { "linear" , SkNamedTransferFn::kLinear },
541 };
542
Mike Klein735c7ba2019-03-25 12:57:58 -0500543
Mike Klein9b462092019-03-27 13:52:35 -0500544 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500545 SkColorType ct;
546 SkAlphaType at;
547 skcms_Matrix3x3 gamut;
548 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500549
Mike Klein9b462092019-03-27 13:52:35 -0500550 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
551 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500552 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
553 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500554 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500555 return 1;
556 }
557
Mike Klein4daf6372019-04-03 11:19:45 -0400558 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
559 : SkColorSpace::MakeRGB(tf,gamut);
560 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500561
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400562 AutoreleasePool pool;
Mike Klein735c7ba2019-03-25 12:57:58 -0500563 for (auto source : sources) {
564 const auto start = std::chrono::steady_clock::now();
Mike Klein832d3c52019-04-02 15:23:46 -0400565 fprintf(stdout, "%50s", source.name.c_str());
Mike Kleinbf15b662019-04-15 11:32:16 -0500566 fflush(stdout);
Mike Klein735c7ba2019-03-25 12:57:58 -0500567
Brian Salomon9241a6d2019-10-03 13:26:54 -0400568 const SkImageInfo info = unsized_info.makeDimensions(source.size);
Mike Klein735c7ba2019-03-25 12:57:58 -0500569
Mike Kleina833cff2019-04-09 11:52:40 -0500570 auto draw = [&source](SkCanvas* canvas) {
571 Result result = source.draw(canvas);
572 switch (result.status) {
573 case Result::Ok: break;
574 case Result::Skip: return false;
575 case Result::Fail:
Mike Kleinbf15b662019-04-15 11:32:16 -0500576 SK_ABORT(result.failure.c_str());
Mike Kleina833cff2019-04-09 11:52:40 -0500577 }
578 return true;
579 };
580
Mike Klein735c7ba2019-03-25 12:57:58 -0500581 GrContextOptions options = baseOptions;
582 source.tweak(&options);
583 GrContextFactory factory(options); // N.B. factory must outlive image
584
585 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500586 sk_sp<SkData> blob;
587 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500588 switch (backend) {
589 case kCPU_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500590 image = draw_with_cpu(draw, info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500591 break;
Mike Klein9b462092019-03-27 13:52:35 -0500592 case kSKP_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500593 blob = draw_as_skp(draw, info);
Mike Klein9b462092019-03-27 13:52:35 -0500594 ext = ".skp";
595 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500596 case kPDF_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500597 blob = draw_as_pdf(draw, info, source.name);
Mike Kleinc245bd92019-03-27 14:31:09 -0500598 ext = ".pdf";
599 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500600 default:
Mike Kleina833cff2019-04-09 11:52:40 -0500601 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500602 break;
603 }
604
Mike Klein9b462092019-03-27 13:52:35 -0500605 if (!image && !blob) {
Mike Klein832d3c52019-04-02 15:23:46 -0400606 fprintf(stdout, "\tskipped\n");
Mike Kleina5c27172019-04-02 10:26:48 -0400607 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500608 }
609
Mike Kleined2cec42019-11-01 10:51:01 -0500610 // We read back a bitmap even when --quick is set and we won't use it,
611 // to keep us honest about deferred work, flushing pipelines, etc.
Mike Klein735c7ba2019-03-25 12:57:58 -0500612 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500613 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500614 SK_ABORT("SkImage::asLegacyBitmap() failed.");
Mike Klein735c7ba2019-03-25 12:57:58 -0500615 }
616
Mike Klein735c7ba2019-03-25 12:57:58 -0500617 SkString md5;
Mike Kleined2cec42019-11-01 10:51:01 -0500618 if (!FLAGS_quick) {
619 HashAndEncode hashAndEncode{bitmap};
620 {
621 SkMD5 hash;
622 if (image) {
623 hashAndEncode.write(&hash);
624 } else {
625 hash.write(blob->data(), blob->size());
Mike Klein9b462092019-03-27 13:52:35 -0500626 }
Mike Kleined2cec42019-11-01 10:51:01 -0500627
628 SkMD5::Digest digest = hash.finish();
629 for (int i = 0; i < 16; i++) {
630 md5.appendf("%02x", digest.data[i]);
631 }
632 }
633
634 if (!FLAGS_writePath.isEmpty()) {
635 sk_mkdir(FLAGS_writePath[0]);
636 SkString path = SkStringPrintf("%s/%s%s",
637 FLAGS_writePath[0], source.name.c_str(), ext);
638
639 if (image) {
640 if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(),
641 FLAGS_key, FLAGS_properties)) {
642 SK_ABORT("Could not write .png.");
643 }
644 } else {
645 SkFILEWStream file(path.c_str());
646 file.write(blob->data(), blob->size());
647 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500648 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500649 }
650
Mike Klein832d3c52019-04-02 15:23:46 -0400651 const auto elapsed = std::chrono::steady_clock::now() - start;
652 fprintf(stdout, "\t%s\t%7dms\n",
653 md5.c_str(),
654 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400655 pool.drain();
Mike Klein735c7ba2019-03-25 12:57:58 -0500656 }
657
Brian Osman5aa11fb2019-04-08 16:40:36 -0400658 if (!FLAGS_writeShaders.isEmpty()) {
659 sk_mkdir(FLAGS_writeShaders[0]);
660 GrBackendApi api =
661 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
662 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
663
664 }
665
Mike Klein735c7ba2019-03-25 12:57:58 -0500666 return 0;
667}