blob: b47c5f9ad62b93336f74ea6d38a6f4e470eec0db [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"
Robert Phillips7b4e43c2020-07-01 16:59:17 -040014#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/private/SkTHash.h"
16#include "src/core/SkColorSpacePriv.h"
17#include "src/core/SkMD5.h"
18#include "src/core/SkOSFile.h"
19#include "src/gpu/GrContextPriv.h"
20#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/utils/SkOSPath.h"
Mike Kleinfee00792019-11-21 16:18:47 -060022#include "tests/Test.h"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040023#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#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/GrContextFactory.h"
30#include "tools/gpu/MemoryCache.h"
31#include "tools/trace/EventTracingPriv.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050032#include <chrono>
33#include <functional>
34#include <stdio.h>
35#include <stdlib.h>
36
Brian Osmanca946562019-04-10 13:51:53 -040037#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038 #include "modules/skottie/include/Skottie.h"
Brian Osman849f4d62019-11-26 08:58:26 -050039 #include "modules/skresources/include/SkResources.h"
Brian Osmanca946562019-04-10 13:51:53 -040040#endif
41
Mike Klein735c7ba2019-03-25 12:57:58 -050042using sk_gpu_test::GrContextFactory;
43
Mike Kleinfee00792019-11-21 16:18:47 -060044static DEFINE_bool(listGMs , false, "Print GM names and exit.");
45static DEFINE_bool(listTests, false, "Print unit test names and exit.");
46
Mike Klein735c7ba2019-03-25 12:57:58 -050047static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw.");
48static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into.");
49
Mike Klein4daf6372019-04-03 11:19:45 -040050static DEFINE_string(ct , "8888", "The color type for any raster backend.");
51static DEFINE_string(at , "premul", "The alpha type for any raster backend.");
52static DEFINE_string(gamut , "srgb", "The color gamut for any raster backend.");
53static DEFINE_string(tf , "srgb", "The transfer function for any raster backend.");
54static DEFINE_bool (legacy, false, "Use a null SkColorSpace instead of --gamut and --tf?");
Mike Klein2a57e792020-01-14 12:47:52 -060055static DEFINE_bool (skvm , false, "Use SkVMBlitter when supported?");
Mike Klein813e8cc2020-08-05 09:33:38 -050056static DEFINE_bool (jit , true, "JIT SkVM?");
57static DEFINE_bool (dylib , false, "JIT SkVM via dylib?");
Mike Klein735c7ba2019-03-25 12:57:58 -050058
59static DEFINE_int (samples , 0, "Samples per pixel in GPU backends.");
Mike Klein735c7ba2019-03-25 12:57:58 -050060static DEFINE_bool (stencils, true, "If false, avoid stencil buffers in GPU backends.");
61static DEFINE_bool (dit , false, "Use device-independent text in GPU backends.");
62static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces.");
63
64static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing.");
65static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing.");
66static DEFINE_bool(releaseAndAbandonGpuContext, false,
67 "Release all GPU resources and abandon the GrContext after drawing.");
68
69static DEFINE_bool(decodeToDst, false,
70 "Decode images to destination format rather than suggested natural format.");
71
Mike Kleinc245bd92019-03-27 14:31:09 -050072static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI,
73 "DPI for rasterized content in vector backends like --backend pdf.");
74static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?");
75
Mike Klein735c7ba2019-03-25 12:57:58 -050076static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?");
77static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set.");
Mike Kleined2cec42019-11-01 10:51:01 -050078static DEFINE_bool (quick, false, "Skip image hashing and encoding?");
Mike Klein735c7ba2019-03-25 12:57:58 -050079
Brian Osman5aa11fb2019-04-08 16:40:36 -040080static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set.");
81
Mike Klein735c7ba2019-03-25 12:57:58 -050082static DEFINE_string(key, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein7b8bc532019-04-09 11:19:47 -050083static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein735c7ba2019-03-25 12:57:58 -050084
85template <typename T>
86struct FlagOption {
87 const char* label;
88 T value;
89};
90
91template <typename T, int N>
92static bool parse_flag(const CommandLineFlags::StringArray& flag,
93 const char* flag_name,
94 const FlagOption<T> (&array)[N],
95 T* value) {
96 for (auto entry : array) {
97 if (flag.contains(entry.label)) {
98 *value = entry.value;
99 return true;
100 }
101 }
102 fprintf(stderr, "Known values for --%s:\n", flag_name);
103 for (auto entry : array) {
104 fprintf(stderr, " --%s %s\n", flag_name, entry.label);
105 }
106 return false;
107}
108
Mike Kleina833cff2019-04-09 11:52:40 -0500109struct Result {
110 enum { Ok, Skip, Fail} status;
111 SkString failure;
112};
113static const Result ok = {Result::Ok, {}},
114 skip = {Result::Skip, {}};
115
Adlai Holler684838f2020-05-12 10:41:04 -0400116static Result fail(const char* why) {
117 return { Result::Fail, SkString(why) };
118}
Mike Kleina833cff2019-04-09 11:52:40 -0500119template <typename... Args>
Adlai Holler684838f2020-05-12 10:41:04 -0400120static Result fail(const char* whyFmt, Args... args) {
121 return { Result::Fail, SkStringPrintf(whyFmt, args...) };
Mike Kleina833cff2019-04-09 11:52:40 -0500122}
123
124
Mike Klein735c7ba2019-03-25 12:57:58 -0500125struct Source {
126 SkString name;
127 SkISize size;
Mike Kleina833cff2019-04-09 11:52:40 -0500128 std::function<Result(SkCanvas*)> draw;
129 std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
Mike Klein735c7ba2019-03-25 12:57:58 -0500130};
131
Mike Kleina833cff2019-04-09 11:52:40 -0500132static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
133 source->size = gm->getISize();
134 source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
135 source->draw = [gm](SkCanvas* canvas) {
Adlai Hollere3ad5272020-07-07 10:27:55 -0400136 auto direct = GrAsDirectContext(canvas->recordingContext());
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400137
Mike Kleina833cff2019-04-09 11:52:40 -0500138 SkString err;
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400139 switch (gm->gpuSetup(direct, canvas, &err)) {
Robert Phillips900630c2020-06-24 10:35:00 -0400140 case skiagm::DrawResult::kOk : break;
141 case skiagm::DrawResult::kSkip: return skip;
142 case skiagm::DrawResult::kFail: return fail(err.c_str());
143 }
144
Mike Kleina833cff2019-04-09 11:52:40 -0500145 switch (gm->draw(canvas, &err)) {
146 case skiagm::DrawResult::kOk: break;
147 case skiagm::DrawResult::kSkip: return skip;
148 case skiagm::DrawResult::kFail: return fail(err.c_str());
149 }
150 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500151 };
152}
153
Mike Kleina833cff2019-04-09 11:52:40 -0500154static void init(Source* source, sk_sp<SkPicture> pic) {
155 source->size = pic->cullRect().roundOut().size();
156 source->draw = [pic](SkCanvas* canvas) {
157 canvas->drawPicture(pic);
158 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500159 };
160}
161
Mike Kleina833cff2019-04-09 11:52:40 -0500162static void init(Source* source, std::shared_ptr<SkCodec> codec) {
163 source->size = codec->dimensions();
164 source->draw = [codec](SkCanvas* canvas) {
165 SkImageInfo info = codec->getInfo();
166 if (FLAGS_decodeToDst) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400167 info = canvas->imageInfo().makeDimensions(info.dimensions());
Mike Kleina833cff2019-04-09 11:52:40 -0500168 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500169
Mike Kleina833cff2019-04-09 11:52:40 -0500170 SkBitmap bm;
171 bm.allocPixels(info);
172 switch (SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) {
173 case SkCodec::kSuccess:
174 case SkCodec::kErrorInInput:
175 case SkCodec::kIncompleteInput: canvas->drawBitmap(bm, 0,0);
176 break;
177 default: return fail("codec->getPixels() failed: %d\n", result);
178 }
179 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500180 };
181}
182
Mike Kleina833cff2019-04-09 11:52:40 -0500183static void init(Source* source, sk_sp<SkSVGDOM> svg) {
184 source->size = svg->containerSize().isEmpty() ? SkISize{1000,1000}
185 : svg->containerSize().toCeil();
186 source->draw = [svg](SkCanvas* canvas) {
187 svg->render(canvas);
188 return ok;
Mike Klein22924262019-04-02 14:14:56 -0400189 };
190}
191
Brian Osmanca946562019-04-10 13:51:53 -0400192#if defined(SK_ENABLE_SKOTTIE)
Mike Kleina833cff2019-04-09 11:52:40 -0500193static void init(Source* source, sk_sp<skottie::Animation> animation) {
194 source->size = {1000,1000};
195 source->draw = [animation](SkCanvas* canvas) {
196 canvas->clear(SK_ColorWHITE);
Mike Klein22924262019-04-02 14:14:56 -0400197
Mike Kleina833cff2019-04-09 11:52:40 -0500198 // Draw frames in a shuffled order to exercise nonlinear frame progression.
199 // The film strip will still be in time order, just drawn out of order.
200 const int order[] = { 4, 0, 3, 1, 2 };
201 const int tiles = SK_ARRAY_COUNT(order);
202 const float dim = 1000.0f / tiles;
Mike Klein22924262019-04-02 14:14:56 -0400203
Mike Kleina833cff2019-04-09 11:52:40 -0500204 const float dt = 1.0f / (tiles*tiles - 1);
Mike Klein22924262019-04-02 14:14:56 -0400205
Mike Kleina833cff2019-04-09 11:52:40 -0500206 for (int y : order)
207 for (int x : order) {
208 SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
Mike Klein22924262019-04-02 14:14:56 -0400209
John Stiles8cc118d2020-07-30 11:22:35 -0400210 SkAutoCanvasRestore _(canvas, /*doSave=*/true);
211 canvas->clipRect(dst, /*doAntiAlias=*/true);
Mike Kleina833cff2019-04-09 11:52:40 -0500212 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
213 dst,
214 SkMatrix::kCenter_ScaleToFit));
215 float t = (y*tiles + x) * dt;
216 animation->seek(t);
217 animation->render(canvas);
218 }
219 return ok;
Mike Klein6253d902019-03-27 15:09:12 -0500220 };
221}
Brian Osmanca946562019-04-10 13:51:53 -0400222#endif
Mike Klein6253d902019-03-27 15:09:12 -0500223
Mike Kleinfee00792019-11-21 16:18:47 -0600224static void init(Source* source, const skiatest::Test& test) {
225 source->size = {1,1};
226 source->draw = [test](SkCanvas* canvas) {
227 struct Reporter : public skiatest::Reporter {
228 SkString msg;
229
230 void reportFailed(const skiatest::Failure& failure) override {
Mike Klein484b63b2020-04-15 12:43:12 -0500231 msg += failure.toString();
232 msg += "\n";
Mike Kleinfee00792019-11-21 16:18:47 -0600233 }
234 } reporter;
235
236 test.run(&reporter, GrContextOptions{});
237
238 if (reporter.msg.isEmpty()) {
239 canvas->clear(SK_ColorGREEN);
240 return ok;
241 }
242
243 canvas->clear(SK_ColorRED);
244 return fail(reporter.msg.c_str());
245 };
246}
247
Mike Kleina5c27172019-04-02 10:26:48 -0400248static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500249 SkImageInfo info) {
250 if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400251 if (draw(surface->getCanvas())) {
252 return surface->makeImageSnapshot();
253 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500254 }
255 return nullptr;
256}
257
Mike Kleina5c27172019-04-02 10:26:48 -0400258static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
Mike Klein9b462092019-03-27 13:52:35 -0500259 SkImageInfo info) {
260 SkPictureRecorder recorder;
Mike Kleina5c27172019-04-02 10:26:48 -0400261 if (draw(recorder.beginRecording(info.width(), info.height()))) {
262 return recorder.finishRecordingAsPicture()->serialize();
263 }
264 return nullptr;
Mike Klein9b462092019-03-27 13:52:35 -0500265}
266
Mike Kleina5c27172019-04-02 10:26:48 -0400267static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
Mike Kleinc245bd92019-03-27 14:31:09 -0500268 SkImageInfo info,
269 SkString name) {
270 SkPDF::Metadata metadata;
271 metadata.fTitle = name;
272 metadata.fCreator = "Skia/FM";
273 metadata.fRasterDPI = FLAGS_rasterDPI;
274 metadata.fPDFA = FLAGS_PDFA;
275
276 SkDynamicMemoryWStream stream;
277 if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400278 if (draw(doc->beginPage(info.width(), info.height()))) {
279 doc->endPage();
280 doc->close();
281 return stream.detachAsData();
282 }
Mike Kleinc245bd92019-03-27 14:31:09 -0500283 }
284 return nullptr;
285}
286
Mike Kleina5c27172019-04-02 10:26:48 -0400287static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500288 SkImageInfo info,
289 GrContextFactory::ContextType api,
290 GrContextFactory* factory) {
291 enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
292 const FlagOption<SurfaceType> kSurfaceTypes[] = {
293 { "default", SurfaceType::kDefault },
294 { "betex" , SurfaceType::kBackendTexture },
295 { "bert" , SurfaceType::kBackendRenderTarget },
296 };
297 SurfaceType surfaceType;
298 if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
299 return nullptr;
300 }
301
Chris Daltonb3c97452019-06-25 20:07:56 -0600302 auto overrides = GrContextFactory::ContextOverrides::kNone;
Mike Klein735c7ba2019-03-25 12:57:58 -0500303 if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
304
Robert Phillips7b4e43c2020-07-01 16:59:17 -0400305 auto context = factory->getContextInfo(api, overrides).directContext();
Mike Klein735c7ba2019-03-25 12:57:58 -0500306
307 uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
308 : 0;
309 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
310
311 sk_sp<SkSurface> surface;
312 GrBackendTexture backendTexture;
313 GrBackendRenderTarget backendRT;
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:
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400325 backendTexture = context->createBackendTexture(info.width(),
326 info.height(),
327 info.colorType(),
Brian Salomon7e67dca2020-07-21 09:27:25 -0400328 GrMipmapped::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400329 GrRenderable::kYes,
330 GrProtected::kNo);
Mike Klein735c7ba2019-03-25 12:57:58 -0500331 surface = SkSurface::MakeFromBackendTexture(context,
332 backendTexture,
333 kTopLeft_GrSurfaceOrigin,
334 FLAGS_samples,
335 info.colorType(),
336 info.refColorSpace(),
337 &props);
338 break;
339
340 case SurfaceType::kBackendRenderTarget:
341 backendRT = context->priv().getGpu()
342 ->createTestingOnlyBackendRenderTarget(info.width(),
343 info.height(),
344 SkColorTypeToGrColorType(info.colorType()));
345 surface = SkSurface::MakeFromBackendRenderTarget(context,
346 backendRT,
347 kBottomLeft_GrSurfaceOrigin,
348 info.colorType(),
349 info.refColorSpace(),
350 &props);
351 break;
352 }
353
354 if (!surface) {
355 fprintf(stderr, "Could not create GPU surface.\n");
356 return nullptr;
357 }
358
359 if (FLAGS_preAbandonGpuContext) {
360 factory->abandonContexts();
361 }
362
Mike Kleina5c27172019-04-02 10:26:48 -0400363 sk_sp<SkImage> image;
364 if (draw(surface->getCanvas())) {
365 image = surface->makeImageSnapshot();
366 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500367
368 if (FLAGS_abandonGpuContext) {
369 factory->abandonContexts();
370 } else if (FLAGS_releaseAndAbandonGpuContext) {
371 factory->releaseResourcesAndAbandonContexts();
372 }
373
374 if (!context->abandoned()) {
375 surface.reset();
376 if (backendTexture.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400377 context->deleteBackendTexture(backendTexture);
Mike Klein735c7ba2019-03-25 12:57:58 -0500378 }
379 if (backendRT.isValid()) {
380 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
381 }
382 }
383
384 return image;
385}
386
Mike Klein2a57e792020-01-14 12:47:52 -0600387extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500388extern bool gSkVMAllowJIT;
Mike Klein31bfa832020-01-22 11:36:40 -0600389extern bool gSkVMJITViaDylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600390
Mike Klein735c7ba2019-03-25 12:57:58 -0500391int main(int argc, char** argv) {
392 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500393 SetupCrashHandler();
Mike Klein735c7ba2019-03-25 12:57:58 -0500394
395 if (FLAGS_cpuDetect) {
396 SkGraphics::Init();
397 }
Mike Klein31bfa832020-01-22 11:36:40 -0600398 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500399 gSkVMAllowJIT = FLAGS_jit;
Mike Klein31bfa832020-01-22 11:36:40 -0600400 gSkVMJITViaDylib = FLAGS_dylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600401
Mike Klein735c7ba2019-03-25 12:57:58 -0500402 initializeEventTracingForTools();
403 ToolUtils::SetDefaultFontMgr();
404 SetAnalyticAAFromCommonFlags();
405
406 GrContextOptions baseOptions;
407 SetCtxOptionsFromCommonFlags(&baseOptions);
408
Brian Osman5aa11fb2019-04-08 16:40:36 -0400409 sk_gpu_test::MemoryCache memoryCache;
410 if (!FLAGS_writeShaders.isEmpty()) {
411 baseOptions.fPersistentCache = &memoryCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400412 baseOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5aa11fb2019-04-08 16:40:36 -0400413 }
414
Mike Kleincbe93ee2019-04-02 17:00:54 -0400415 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500416 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Hal Canaryedda5652019-08-05 10:28:09 -0400417 std::unique_ptr<skiagm::GM> gm{factory()};
Mike Kleinfee00792019-11-21 16:18:47 -0600418 if (FLAGS_listGMs) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500419 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400420 } else {
421 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500422 }
423 }
Mike Kleinfee00792019-11-21 16:18:47 -0600424
425 SkTHashMap<SkString, const skiatest::Test*> tests;
426 for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
427 if (test.needsGpu) {
428 continue; // TODO
429 }
430 if (FLAGS_listTests) {
431 fprintf(stdout, "%s\n", test.name);
432 } else {
433 tests.set(SkString{test.name}, &test);
434 }
435 }
436
437 if (FLAGS_listGMs || FLAGS_listTests) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400438 return 0;
439 }
Mike Kleinfee00792019-11-21 16:18:47 -0600440 if (FLAGS_sources.isEmpty()) {
441 fprintf(stderr, "Please give me something to run using -s/--sources!\n");
442 return 1;
443 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400444
445 SkTArray<Source> sources;
Mike Kleina833cff2019-04-09 11:52:40 -0500446 for (const SkString& name : FLAGS_sources) {
447 Source* source = &sources.push_back();
448
449 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Hal Canaryedda5652019-08-05 10:28:09 -0400450 std::shared_ptr<skiagm::GM> gm{(*factory)()};
Mike Kleina833cff2019-04-09 11:52:40 -0500451 source->name = name;
Ben Wagner406ff502019-08-12 16:39:24 -0400452 init(source, std::move(gm));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400453 continue;
454 }
455
Mike Kleinfee00792019-11-21 16:18:47 -0600456 if (const skiatest::Test** test = tests.find(name)) {
457 source->name = name;
458 init(source, **test);
459 continue;
460 }
461
Mike Kleina833cff2019-04-09 11:52:40 -0500462 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
463 source->name = SkOSPath::Basename(name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500464
Mike Kleince90d6f2019-03-29 11:14:14 -0500465 if (name.endsWith(".skp")) {
466 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500467 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400468 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500469 }
470 } else if (name.endsWith(".svg")) {
471 SkMemoryStream stream{blob};
472 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500473 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400474 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500475 }
Brian Osmanca946562019-04-10 13:51:53 -0400476 }
477#if defined(SK_ENABLE_SKOTTIE)
478 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500479 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400480 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
Brian Osman849f4d62019-11-26 08:58:26 -0500481 .setResourceProvider(skresources::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400482 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500483 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400484 continue;
Mike Klein22924262019-04-02 14:14:56 -0400485 }
Brian Osmanca946562019-04-10 13:51:53 -0400486 }
487#endif
488 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500489 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400490 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500491 }
492 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400493
Mike Kleina833cff2019-04-09 11:52:40 -0500494 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400495 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500496 }
497
Mike Klein9b462092019-03-27 13:52:35 -0500498 enum NonGpuBackends {
499 kCPU_Backend = -1,
500 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500501 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500502 };
503 const FlagOption<int> kBackends[] = {
504 { "cpu" , kCPU_Backend },
505 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500506 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500507 { "gl" , GrContextFactory::kGL_ContextType },
508 { "gles" , GrContextFactory::kGLES_ContextType },
509 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
510 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
511 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
512 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
513 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
514 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
515 { "vk" , GrContextFactory::kVulkan_ContextType },
516 { "mtl" , GrContextFactory::kMetal_ContextType },
517 { "mock" , GrContextFactory::kMock_ContextType },
518 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500519 const FlagOption<SkColorType> kColorTypes[] = {
Mike Kleind58e01b62020-07-17 13:52:41 -0500520 { "a8", kAlpha_8_SkColorType },
521 { "g8", kGray_8_SkColorType },
522 { "565", kRGB_565_SkColorType },
523 { "4444", kARGB_4444_SkColorType },
524 { "8888", kN32_SkColorType },
525 { "888x", kRGB_888x_SkColorType },
526 { "1010102", kRGBA_1010102_SkColorType },
527 { "101010x", kRGB_101010x_SkColorType },
528 { "bgra1010102", kBGRA_1010102_SkColorType },
529 { "bgr101010x", kBGR_101010x_SkColorType },
530 { "f16norm", kRGBA_F16Norm_SkColorType },
531 { "f16", kRGBA_F16_SkColorType },
532 { "f32", kRGBA_F32_SkColorType },
533 { "rgba", kRGBA_8888_SkColorType },
534 { "bgra", kBGRA_8888_SkColorType },
535 { "16161616", kR16G16B16A16_unorm_SkColorType },
Mike Klein735c7ba2019-03-25 12:57:58 -0500536 };
537 const FlagOption<SkAlphaType> kAlphaTypes[] = {
538 { "premul", kPremul_SkAlphaType },
539 { "unpremul", kUnpremul_SkAlphaType },
540 };
541 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
542 { "srgb", SkNamedGamut::kSRGB },
Mike Kleinb147ace2020-01-16 11:11:06 -0600543 { "p3", SkNamedGamut::kDisplayP3 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500544 { "rec2020", SkNamedGamut::kRec2020 },
545 { "adobe", SkNamedGamut::kAdobeRGB },
546 { "narrow", gNarrow_toXYZD50},
547 };
548 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
549 { "srgb" , SkNamedTransferFn::kSRGB },
Hal Canarybe67a172019-05-24 10:13:36 -0400550 { "rec2020", SkNamedTransferFn::kRec2020 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500551 { "2.2" , SkNamedTransferFn::k2Dot2 },
552 { "linear" , SkNamedTransferFn::kLinear },
553 };
554
Mike Klein735c7ba2019-03-25 12:57:58 -0500555
Mike Klein9b462092019-03-27 13:52:35 -0500556 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500557 SkColorType ct;
558 SkAlphaType at;
559 skcms_Matrix3x3 gamut;
560 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500561
Mike Klein9b462092019-03-27 13:52:35 -0500562 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
563 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500564 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
565 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500566 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500567 return 1;
568 }
569
Mike Klein4daf6372019-04-03 11:19:45 -0400570 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
571 : SkColorSpace::MakeRGB(tf,gamut);
572 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500573
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400574 AutoreleasePool pool;
Mike Klein735c7ba2019-03-25 12:57:58 -0500575 for (auto source : sources) {
576 const auto start = std::chrono::steady_clock::now();
Mike Klein832d3c52019-04-02 15:23:46 -0400577 fprintf(stdout, "%50s", source.name.c_str());
Mike Kleinbf15b662019-04-15 11:32:16 -0500578 fflush(stdout);
Mike Klein735c7ba2019-03-25 12:57:58 -0500579
Brian Salomon9241a6d2019-10-03 13:26:54 -0400580 const SkImageInfo info = unsized_info.makeDimensions(source.size);
Mike Klein735c7ba2019-03-25 12:57:58 -0500581
Mike Kleina833cff2019-04-09 11:52:40 -0500582 auto draw = [&source](SkCanvas* canvas) {
583 Result result = source.draw(canvas);
584 switch (result.status) {
585 case Result::Ok: break;
586 case Result::Skip: return false;
587 case Result::Fail:
John Stiles616da102020-06-12 14:07:41 -0400588 SK_ABORT("%s", result.failure.c_str());
Mike Kleina833cff2019-04-09 11:52:40 -0500589 }
590 return true;
591 };
592
Mike Klein735c7ba2019-03-25 12:57:58 -0500593 GrContextOptions options = baseOptions;
594 source.tweak(&options);
595 GrContextFactory factory(options); // N.B. factory must outlive image
596
597 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500598 sk_sp<SkData> blob;
599 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500600 switch (backend) {
601 case kCPU_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500602 image = draw_with_cpu(draw, info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500603 break;
Mike Klein9b462092019-03-27 13:52:35 -0500604 case kSKP_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500605 blob = draw_as_skp(draw, info);
Mike Klein9b462092019-03-27 13:52:35 -0500606 ext = ".skp";
607 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500608 case kPDF_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500609 blob = draw_as_pdf(draw, info, source.name);
Mike Kleinc245bd92019-03-27 14:31:09 -0500610 ext = ".pdf";
611 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500612 default:
Mike Kleina833cff2019-04-09 11:52:40 -0500613 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500614 break;
615 }
616
Mike Klein9b462092019-03-27 13:52:35 -0500617 if (!image && !blob) {
Mike Klein832d3c52019-04-02 15:23:46 -0400618 fprintf(stdout, "\tskipped\n");
Mike Kleina5c27172019-04-02 10:26:48 -0400619 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500620 }
621
Mike Kleined2cec42019-11-01 10:51:01 -0500622 // We read back a bitmap even when --quick is set and we won't use it,
623 // to keep us honest about deferred work, flushing pipelines, etc.
Mike Klein735c7ba2019-03-25 12:57:58 -0500624 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500625 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500626 SK_ABORT("SkImage::asLegacyBitmap() failed.");
Mike Klein735c7ba2019-03-25 12:57:58 -0500627 }
628
Mike Klein735c7ba2019-03-25 12:57:58 -0500629 SkString md5;
Mike Kleined2cec42019-11-01 10:51:01 -0500630 if (!FLAGS_quick) {
631 HashAndEncode hashAndEncode{bitmap};
632 {
633 SkMD5 hash;
634 if (image) {
635 hashAndEncode.write(&hash);
636 } else {
637 hash.write(blob->data(), blob->size());
Mike Klein9b462092019-03-27 13:52:35 -0500638 }
Mike Kleined2cec42019-11-01 10:51:01 -0500639
640 SkMD5::Digest digest = hash.finish();
641 for (int i = 0; i < 16; i++) {
642 md5.appendf("%02x", digest.data[i]);
643 }
644 }
645
646 if (!FLAGS_writePath.isEmpty()) {
647 sk_mkdir(FLAGS_writePath[0]);
648 SkString path = SkStringPrintf("%s/%s%s",
649 FLAGS_writePath[0], source.name.c_str(), ext);
650
651 if (image) {
652 if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(),
653 FLAGS_key, FLAGS_properties)) {
654 SK_ABORT("Could not write .png.");
655 }
656 } else {
657 SkFILEWStream file(path.c_str());
658 file.write(blob->data(), blob->size());
659 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500660 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500661 }
662
Mike Klein832d3c52019-04-02 15:23:46 -0400663 const auto elapsed = std::chrono::steady_clock::now() - start;
664 fprintf(stdout, "\t%s\t%7dms\n",
665 md5.c_str(),
666 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400667 pool.drain();
Mike Klein735c7ba2019-03-25 12:57:58 -0500668 }
669
Brian Osman5aa11fb2019-04-08 16:40:36 -0400670 if (!FLAGS_writeShaders.isEmpty()) {
671 sk_mkdir(FLAGS_writeShaders[0]);
672 GrBackendApi api =
673 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
674 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
675
676 }
677
Mike Klein735c7ba2019-03-25 12:57:58 -0500678 return 0;
679}