blob: 7dc93c3233380b83171b70877280cfb1c72b89ad [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
114template <typename... Args>
115static Result fail(const char* why, Args... args) {
116 return { Result::Fail, SkStringPrintf(why, args...) };
117}
118
119
Mike Klein735c7ba2019-03-25 12:57:58 -0500120struct Source {
121 SkString name;
122 SkISize size;
Mike Kleina833cff2019-04-09 11:52:40 -0500123 std::function<Result(SkCanvas*)> draw;
124 std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
Mike Klein735c7ba2019-03-25 12:57:58 -0500125};
126
Mike Kleina833cff2019-04-09 11:52:40 -0500127static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
128 source->size = gm->getISize();
129 source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
130 source->draw = [gm](SkCanvas* canvas) {
131 SkString err;
132 switch (gm->draw(canvas, &err)) {
133 case skiagm::DrawResult::kOk: break;
134 case skiagm::DrawResult::kSkip: return skip;
135 case skiagm::DrawResult::kFail: return fail(err.c_str());
136 }
137 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500138 };
139}
140
Mike Kleina833cff2019-04-09 11:52:40 -0500141static void init(Source* source, sk_sp<SkPicture> pic) {
142 source->size = pic->cullRect().roundOut().size();
143 source->draw = [pic](SkCanvas* canvas) {
144 canvas->drawPicture(pic);
145 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500146 };
147}
148
Mike Kleina833cff2019-04-09 11:52:40 -0500149static void init(Source* source, std::shared_ptr<SkCodec> codec) {
150 source->size = codec->dimensions();
151 source->draw = [codec](SkCanvas* canvas) {
152 SkImageInfo info = codec->getInfo();
153 if (FLAGS_decodeToDst) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400154 info = canvas->imageInfo().makeDimensions(info.dimensions());
Mike Kleina833cff2019-04-09 11:52:40 -0500155 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500156
Mike Kleina833cff2019-04-09 11:52:40 -0500157 SkBitmap bm;
158 bm.allocPixels(info);
159 switch (SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) {
160 case SkCodec::kSuccess:
161 case SkCodec::kErrorInInput:
162 case SkCodec::kIncompleteInput: canvas->drawBitmap(bm, 0,0);
163 break;
164 default: return fail("codec->getPixels() failed: %d\n", result);
165 }
166 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500167 };
168}
169
Mike Kleina833cff2019-04-09 11:52:40 -0500170static void init(Source* source, sk_sp<SkSVGDOM> svg) {
171 source->size = svg->containerSize().isEmpty() ? SkISize{1000,1000}
172 : svg->containerSize().toCeil();
173 source->draw = [svg](SkCanvas* canvas) {
174 svg->render(canvas);
175 return ok;
Mike Klein22924262019-04-02 14:14:56 -0400176 };
177}
178
Brian Osmanca946562019-04-10 13:51:53 -0400179#if defined(SK_ENABLE_SKOTTIE)
Mike Kleina833cff2019-04-09 11:52:40 -0500180static void init(Source* source, sk_sp<skottie::Animation> animation) {
181 source->size = {1000,1000};
182 source->draw = [animation](SkCanvas* canvas) {
183 canvas->clear(SK_ColorWHITE);
Mike Klein22924262019-04-02 14:14:56 -0400184
Mike Kleina833cff2019-04-09 11:52:40 -0500185 // Draw frames in a shuffled order to exercise nonlinear frame progression.
186 // The film strip will still be in time order, just drawn out of order.
187 const int order[] = { 4, 0, 3, 1, 2 };
188 const int tiles = SK_ARRAY_COUNT(order);
189 const float dim = 1000.0f / tiles;
Mike Klein22924262019-04-02 14:14:56 -0400190
Mike Kleina833cff2019-04-09 11:52:40 -0500191 const float dt = 1.0f / (tiles*tiles - 1);
Mike Klein22924262019-04-02 14:14:56 -0400192
Mike Kleina833cff2019-04-09 11:52:40 -0500193 for (int y : order)
194 for (int x : order) {
195 SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
Mike Klein22924262019-04-02 14:14:56 -0400196
Mike Kleina833cff2019-04-09 11:52:40 -0500197 SkAutoCanvasRestore _(canvas, true/*save now*/);
198 canvas->clipRect(dst, /*aa=*/true);
199 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
200 dst,
201 SkMatrix::kCenter_ScaleToFit));
202 float t = (y*tiles + x) * dt;
203 animation->seek(t);
204 animation->render(canvas);
205 }
206 return ok;
Mike Klein6253d902019-03-27 15:09:12 -0500207 };
208}
Brian Osmanca946562019-04-10 13:51:53 -0400209#endif
Mike Klein6253d902019-03-27 15:09:12 -0500210
Mike Kleinfee00792019-11-21 16:18:47 -0600211static void init(Source* source, const skiatest::Test& test) {
212 source->size = {1,1};
213 source->draw = [test](SkCanvas* canvas) {
214 struct Reporter : public skiatest::Reporter {
215 SkString msg;
216
217 void reportFailed(const skiatest::Failure& failure) override {
218 msg = failure.toString();
219 }
220 } reporter;
221
222 test.run(&reporter, GrContextOptions{});
223
224 if (reporter.msg.isEmpty()) {
225 canvas->clear(SK_ColorGREEN);
226 return ok;
227 }
228
229 canvas->clear(SK_ColorRED);
230 return fail(reporter.msg.c_str());
231 };
232}
233
Mike Kleina5c27172019-04-02 10:26:48 -0400234static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500235 SkImageInfo info) {
236 if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400237 if (draw(surface->getCanvas())) {
238 return surface->makeImageSnapshot();
239 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500240 }
241 return nullptr;
242}
243
Mike Kleina5c27172019-04-02 10:26:48 -0400244static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
Mike Klein9b462092019-03-27 13:52:35 -0500245 SkImageInfo info) {
246 SkPictureRecorder recorder;
Mike Kleina5c27172019-04-02 10:26:48 -0400247 if (draw(recorder.beginRecording(info.width(), info.height()))) {
248 return recorder.finishRecordingAsPicture()->serialize();
249 }
250 return nullptr;
Mike Klein9b462092019-03-27 13:52:35 -0500251}
252
Mike Kleina5c27172019-04-02 10:26:48 -0400253static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
Mike Kleinc245bd92019-03-27 14:31:09 -0500254 SkImageInfo info,
255 SkString name) {
256 SkPDF::Metadata metadata;
257 metadata.fTitle = name;
258 metadata.fCreator = "Skia/FM";
259 metadata.fRasterDPI = FLAGS_rasterDPI;
260 metadata.fPDFA = FLAGS_PDFA;
261
262 SkDynamicMemoryWStream stream;
263 if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400264 if (draw(doc->beginPage(info.width(), info.height()))) {
265 doc->endPage();
266 doc->close();
267 return stream.detachAsData();
268 }
Mike Kleinc245bd92019-03-27 14:31:09 -0500269 }
270 return nullptr;
271}
272
Mike Kleina5c27172019-04-02 10:26:48 -0400273static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500274 SkImageInfo info,
275 GrContextFactory::ContextType api,
276 GrContextFactory* factory) {
277 enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
278 const FlagOption<SurfaceType> kSurfaceTypes[] = {
279 { "default", SurfaceType::kDefault },
280 { "betex" , SurfaceType::kBackendTexture },
281 { "bert" , SurfaceType::kBackendRenderTarget },
282 };
283 SurfaceType surfaceType;
284 if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
285 return nullptr;
286 }
287
Chris Daltonb3c97452019-06-25 20:07:56 -0600288 auto overrides = GrContextFactory::ContextOverrides::kNone;
Mike Klein735c7ba2019-03-25 12:57:58 -0500289 if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
290
291 GrContext* context = factory->getContextInfo(api, overrides)
292 .grContext();
293
294 uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
295 : 0;
296 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
297
298 sk_sp<SkSurface> surface;
299 GrBackendTexture backendTexture;
300 GrBackendRenderTarget backendRT;
301
302 switch (surfaceType) {
303 case SurfaceType::kDefault:
304 surface = SkSurface::MakeRenderTarget(context,
305 SkBudgeted::kNo,
306 info,
307 FLAGS_samples,
308 &props);
309 break;
310
311 case SurfaceType::kBackendTexture:
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400312 backendTexture = context->createBackendTexture(info.width(),
313 info.height(),
314 info.colorType(),
315 GrMipMapped::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400316 GrRenderable::kYes,
317 GrProtected::kNo);
Mike Klein735c7ba2019-03-25 12:57:58 -0500318 surface = SkSurface::MakeFromBackendTexture(context,
319 backendTexture,
320 kTopLeft_GrSurfaceOrigin,
321 FLAGS_samples,
322 info.colorType(),
323 info.refColorSpace(),
324 &props);
325 break;
326
327 case SurfaceType::kBackendRenderTarget:
328 backendRT = context->priv().getGpu()
329 ->createTestingOnlyBackendRenderTarget(info.width(),
330 info.height(),
331 SkColorTypeToGrColorType(info.colorType()));
332 surface = SkSurface::MakeFromBackendRenderTarget(context,
333 backendRT,
334 kBottomLeft_GrSurfaceOrigin,
335 info.colorType(),
336 info.refColorSpace(),
337 &props);
338 break;
339 }
340
341 if (!surface) {
342 fprintf(stderr, "Could not create GPU surface.\n");
343 return nullptr;
344 }
345
346 if (FLAGS_preAbandonGpuContext) {
347 factory->abandonContexts();
348 }
349
Mike Kleina5c27172019-04-02 10:26:48 -0400350 sk_sp<SkImage> image;
351 if (draw(surface->getCanvas())) {
352 image = surface->makeImageSnapshot();
353 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500354
355 if (FLAGS_abandonGpuContext) {
356 factory->abandonContexts();
357 } else if (FLAGS_releaseAndAbandonGpuContext) {
358 factory->releaseResourcesAndAbandonContexts();
359 }
360
361 if (!context->abandoned()) {
362 surface.reset();
363 if (backendTexture.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400364 context->deleteBackendTexture(backendTexture);
Mike Klein735c7ba2019-03-25 12:57:58 -0500365 }
366 if (backendRT.isValid()) {
367 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
368 }
369 }
370
371 return image;
372}
373
Mike Klein2a57e792020-01-14 12:47:52 -0600374extern bool gUseSkVMBlitter;
Mike Klein31bfa832020-01-22 11:36:40 -0600375extern bool gSkVMJITViaDylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600376
Mike Klein735c7ba2019-03-25 12:57:58 -0500377int main(int argc, char** argv) {
378 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500379 SetupCrashHandler();
Mike Klein735c7ba2019-03-25 12:57:58 -0500380
381 if (FLAGS_cpuDetect) {
382 SkGraphics::Init();
383 }
Mike Klein31bfa832020-01-22 11:36:40 -0600384 gUseSkVMBlitter = FLAGS_skvm;
385 gSkVMJITViaDylib = FLAGS_dylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600386
Mike Klein735c7ba2019-03-25 12:57:58 -0500387 initializeEventTracingForTools();
388 ToolUtils::SetDefaultFontMgr();
389 SetAnalyticAAFromCommonFlags();
390
391 GrContextOptions baseOptions;
392 SetCtxOptionsFromCommonFlags(&baseOptions);
393
Brian Osman5aa11fb2019-04-08 16:40:36 -0400394 sk_gpu_test::MemoryCache memoryCache;
395 if (!FLAGS_writeShaders.isEmpty()) {
396 baseOptions.fPersistentCache = &memoryCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400397 baseOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5aa11fb2019-04-08 16:40:36 -0400398 }
399
Mike Kleincbe93ee2019-04-02 17:00:54 -0400400 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500401 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Hal Canaryedda5652019-08-05 10:28:09 -0400402 std::unique_ptr<skiagm::GM> gm{factory()};
Mike Kleinfee00792019-11-21 16:18:47 -0600403 if (FLAGS_listGMs) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500404 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400405 } else {
406 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500407 }
408 }
Mike Kleinfee00792019-11-21 16:18:47 -0600409
410 SkTHashMap<SkString, const skiatest::Test*> tests;
411 for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
412 if (test.needsGpu) {
413 continue; // TODO
414 }
415 if (FLAGS_listTests) {
416 fprintf(stdout, "%s\n", test.name);
417 } else {
418 tests.set(SkString{test.name}, &test);
419 }
420 }
421
422 if (FLAGS_listGMs || FLAGS_listTests) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400423 return 0;
424 }
Mike Kleinfee00792019-11-21 16:18:47 -0600425 if (FLAGS_sources.isEmpty()) {
426 fprintf(stderr, "Please give me something to run using -s/--sources!\n");
427 return 1;
428 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400429
430 SkTArray<Source> sources;
Mike Kleina833cff2019-04-09 11:52:40 -0500431 for (const SkString& name : FLAGS_sources) {
432 Source* source = &sources.push_back();
433
434 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Hal Canaryedda5652019-08-05 10:28:09 -0400435 std::shared_ptr<skiagm::GM> gm{(*factory)()};
Mike Kleina833cff2019-04-09 11:52:40 -0500436 source->name = name;
Ben Wagner406ff502019-08-12 16:39:24 -0400437 init(source, std::move(gm));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400438 continue;
439 }
440
Mike Kleinfee00792019-11-21 16:18:47 -0600441 if (const skiatest::Test** test = tests.find(name)) {
442 source->name = name;
443 init(source, **test);
444 continue;
445 }
446
Mike Kleina833cff2019-04-09 11:52:40 -0500447 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
448 source->name = SkOSPath::Basename(name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500449
Mike Kleince90d6f2019-03-29 11:14:14 -0500450 if (name.endsWith(".skp")) {
451 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500452 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400453 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500454 }
455 } else if (name.endsWith(".svg")) {
456 SkMemoryStream stream{blob};
457 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500458 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400459 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500460 }
Brian Osmanca946562019-04-10 13:51:53 -0400461 }
462#if defined(SK_ENABLE_SKOTTIE)
463 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500464 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400465 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
Brian Osman849f4d62019-11-26 08:58:26 -0500466 .setResourceProvider(skresources::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400467 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500468 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400469 continue;
Mike Klein22924262019-04-02 14:14:56 -0400470 }
Brian Osmanca946562019-04-10 13:51:53 -0400471 }
472#endif
473 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500474 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400475 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500476 }
477 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400478
Mike Kleina833cff2019-04-09 11:52:40 -0500479 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400480 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500481 }
482
Mike Klein9b462092019-03-27 13:52:35 -0500483 enum NonGpuBackends {
484 kCPU_Backend = -1,
485 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500486 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500487 };
488 const FlagOption<int> kBackends[] = {
489 { "cpu" , kCPU_Backend },
490 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500491 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500492 { "gl" , GrContextFactory::kGL_ContextType },
493 { "gles" , GrContextFactory::kGLES_ContextType },
494 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
495 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
496 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
497 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
498 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
499 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
500 { "vk" , GrContextFactory::kVulkan_ContextType },
501 { "mtl" , GrContextFactory::kMetal_ContextType },
502 { "mock" , GrContextFactory::kMock_ContextType },
503 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500504 const FlagOption<SkColorType> kColorTypes[] = {
Mike Kleinf7eb0542020-02-11 12:19:08 -0600505 { "a8", kAlpha_8_SkColorType },
506 { "g8", kGray_8_SkColorType },
507 { "565", kRGB_565_SkColorType },
508 { "4444", kARGB_4444_SkColorType },
509 { "8888", kN32_SkColorType },
510 { "888x", kRGB_888x_SkColorType },
511 { "1010102", kRGBA_1010102_SkColorType },
512 { "101010x", kRGB_101010x_SkColorType },
513 { "bgra1010102", kBGRA_1010102_SkColorType },
514 { "bgr101010x", kBGR_101010x_SkColorType },
515 { "f16norm", kRGBA_F16Norm_SkColorType },
516 { "f16", kRGBA_F16_SkColorType },
517 { "f32", kRGBA_F32_SkColorType },
518 { "rgba", kRGBA_8888_SkColorType },
519 { "bgra", kBGRA_8888_SkColorType },
Mike Klein735c7ba2019-03-25 12:57:58 -0500520 };
521 const FlagOption<SkAlphaType> kAlphaTypes[] = {
522 { "premul", kPremul_SkAlphaType },
523 { "unpremul", kUnpremul_SkAlphaType },
524 };
525 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
526 { "srgb", SkNamedGamut::kSRGB },
Mike Kleinb147ace2020-01-16 11:11:06 -0600527 { "p3", SkNamedGamut::kDisplayP3 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500528 { "rec2020", SkNamedGamut::kRec2020 },
529 { "adobe", SkNamedGamut::kAdobeRGB },
530 { "narrow", gNarrow_toXYZD50},
531 };
532 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
533 { "srgb" , SkNamedTransferFn::kSRGB },
Hal Canarybe67a172019-05-24 10:13:36 -0400534 { "rec2020", SkNamedTransferFn::kRec2020 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500535 { "2.2" , SkNamedTransferFn::k2Dot2 },
536 { "linear" , SkNamedTransferFn::kLinear },
537 };
538
Mike Klein735c7ba2019-03-25 12:57:58 -0500539
Mike Klein9b462092019-03-27 13:52:35 -0500540 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500541 SkColorType ct;
542 SkAlphaType at;
543 skcms_Matrix3x3 gamut;
544 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500545
Mike Klein9b462092019-03-27 13:52:35 -0500546 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
547 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500548 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
549 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500550 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500551 return 1;
552 }
553
Mike Klein4daf6372019-04-03 11:19:45 -0400554 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
555 : SkColorSpace::MakeRGB(tf,gamut);
556 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500557
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400558 AutoreleasePool pool;
Mike Klein735c7ba2019-03-25 12:57:58 -0500559 for (auto source : sources) {
560 const auto start = std::chrono::steady_clock::now();
Mike Klein832d3c52019-04-02 15:23:46 -0400561 fprintf(stdout, "%50s", source.name.c_str());
Mike Kleinbf15b662019-04-15 11:32:16 -0500562 fflush(stdout);
Mike Klein735c7ba2019-03-25 12:57:58 -0500563
Brian Salomon9241a6d2019-10-03 13:26:54 -0400564 const SkImageInfo info = unsized_info.makeDimensions(source.size);
Mike Klein735c7ba2019-03-25 12:57:58 -0500565
Mike Kleina833cff2019-04-09 11:52:40 -0500566 auto draw = [&source](SkCanvas* canvas) {
567 Result result = source.draw(canvas);
568 switch (result.status) {
569 case Result::Ok: break;
570 case Result::Skip: return false;
571 case Result::Fail:
Mike Kleinbf15b662019-04-15 11:32:16 -0500572 SK_ABORT(result.failure.c_str());
Mike Kleina833cff2019-04-09 11:52:40 -0500573 }
574 return true;
575 };
576
Mike Klein735c7ba2019-03-25 12:57:58 -0500577 GrContextOptions options = baseOptions;
578 source.tweak(&options);
579 GrContextFactory factory(options); // N.B. factory must outlive image
580
581 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500582 sk_sp<SkData> blob;
583 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500584 switch (backend) {
585 case kCPU_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500586 image = draw_with_cpu(draw, info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500587 break;
Mike Klein9b462092019-03-27 13:52:35 -0500588 case kSKP_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500589 blob = draw_as_skp(draw, info);
Mike Klein9b462092019-03-27 13:52:35 -0500590 ext = ".skp";
591 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500592 case kPDF_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500593 blob = draw_as_pdf(draw, info, source.name);
Mike Kleinc245bd92019-03-27 14:31:09 -0500594 ext = ".pdf";
595 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500596 default:
Mike Kleina833cff2019-04-09 11:52:40 -0500597 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500598 break;
599 }
600
Mike Klein9b462092019-03-27 13:52:35 -0500601 if (!image && !blob) {
Mike Klein832d3c52019-04-02 15:23:46 -0400602 fprintf(stdout, "\tskipped\n");
Mike Kleina5c27172019-04-02 10:26:48 -0400603 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500604 }
605
Mike Kleined2cec42019-11-01 10:51:01 -0500606 // We read back a bitmap even when --quick is set and we won't use it,
607 // to keep us honest about deferred work, flushing pipelines, etc.
Mike Klein735c7ba2019-03-25 12:57:58 -0500608 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500609 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500610 SK_ABORT("SkImage::asLegacyBitmap() failed.");
Mike Klein735c7ba2019-03-25 12:57:58 -0500611 }
612
Mike Klein735c7ba2019-03-25 12:57:58 -0500613 SkString md5;
Mike Kleined2cec42019-11-01 10:51:01 -0500614 if (!FLAGS_quick) {
615 HashAndEncode hashAndEncode{bitmap};
616 {
617 SkMD5 hash;
618 if (image) {
619 hashAndEncode.write(&hash);
620 } else {
621 hash.write(blob->data(), blob->size());
Mike Klein9b462092019-03-27 13:52:35 -0500622 }
Mike Kleined2cec42019-11-01 10:51:01 -0500623
624 SkMD5::Digest digest = hash.finish();
625 for (int i = 0; i < 16; i++) {
626 md5.appendf("%02x", digest.data[i]);
627 }
628 }
629
630 if (!FLAGS_writePath.isEmpty()) {
631 sk_mkdir(FLAGS_writePath[0]);
632 SkString path = SkStringPrintf("%s/%s%s",
633 FLAGS_writePath[0], source.name.c_str(), ext);
634
635 if (image) {
636 if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(),
637 FLAGS_key, FLAGS_properties)) {
638 SK_ABORT("Could not write .png.");
639 }
640 } else {
641 SkFILEWStream file(path.c_str());
642 file.write(blob->data(), blob->size());
643 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500644 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500645 }
646
Mike Klein832d3c52019-04-02 15:23:46 -0400647 const auto elapsed = std::chrono::steady_clock::now() - start;
648 fprintf(stdout, "\t%s\t%7dms\n",
649 md5.c_str(),
650 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400651 pool.drain();
Mike Klein735c7ba2019-03-25 12:57:58 -0500652 }
653
Brian Osman5aa11fb2019-04-08 16:40:36 -0400654 if (!FLAGS_writeShaders.isEmpty()) {
655 sk_mkdir(FLAGS_writeShaders[0]);
656 GrBackendApi api =
657 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
658 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
659
660 }
661
Mike Klein735c7ba2019-03-25 12:57:58 -0500662 return 0;
663}