blob: 2779059dfc83581cdcf6992dafd88befda2bd790 [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 "gm/gm.h"
5#include "include/codec/SkCodec.h"
Robert Phillipse19babf2020-04-06 13:57:30 -04006#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#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"
Robert Phillips7b4e43c2020-07-01 16:59:17 -040013#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkTHash.h"
Florin Malitab3418102020-10-15 18:10:29 -040015#include "modules/svg/include/SkSVGDOM.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkColorSpacePriv.h"
17#include "src/core/SkMD5.h"
18#include "src/core/SkOSFile.h"
Adlai Hollera0693042020-10-14 11:23:11 -040019#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#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"
Brian Salomon72c7b982020-10-06 10:07:38 -040029#include "tools/gpu/BackendSurfaceFactory.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "tools/gpu/GrContextFactory.h"
31#include "tools/gpu/MemoryCache.h"
32#include "tools/trace/EventTracingPriv.h"
Brian Salomon72c7b982020-10-06 10:07:38 -040033
Mike Klein735c7ba2019-03-25 12:57:58 -050034#include <chrono>
35#include <functional>
36#include <stdio.h>
37#include <stdlib.h>
38
Brian Osmanca946562019-04-10 13:51:53 -040039#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040 #include "modules/skottie/include/Skottie.h"
Brian Osman849f4d62019-11-26 08:58:26 -050041 #include "modules/skresources/include/SkResources.h"
Brian Osmanca946562019-04-10 13:51:53 -040042#endif
43
Mike Klein735c7ba2019-03-25 12:57:58 -050044using sk_gpu_test::GrContextFactory;
45
Mike Kleinfee00792019-11-21 16:18:47 -060046static DEFINE_bool(listGMs , false, "Print GM names and exit.");
47static DEFINE_bool(listTests, false, "Print unit test names and exit.");
48
Mike Klein735c7ba2019-03-25 12:57:58 -050049static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw.");
50static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into.");
51
Mike Klein4daf6372019-04-03 11:19:45 -040052static DEFINE_string(ct , "8888", "The color type for any raster backend.");
53static DEFINE_string(at , "premul", "The alpha type for any raster backend.");
54static DEFINE_string(gamut , "srgb", "The color gamut for any raster backend.");
55static DEFINE_string(tf , "srgb", "The transfer function for any raster backend.");
56static DEFINE_bool (legacy, false, "Use a null SkColorSpace instead of --gamut and --tf?");
Mike Klein2a57e792020-01-14 12:47:52 -060057static DEFINE_bool (skvm , false, "Use SkVMBlitter when supported?");
Mike Klein813e8cc2020-08-05 09:33:38 -050058static DEFINE_bool (jit , true, "JIT SkVM?");
59static DEFINE_bool (dylib , false, "JIT SkVM via dylib?");
Mike Klein735c7ba2019-03-25 12:57:58 -050060
61static DEFINE_int (samples , 0, "Samples per pixel in GPU backends.");
Mike Klein735c7ba2019-03-25 12:57:58 -050062static DEFINE_bool (stencils, true, "If false, avoid stencil buffers in GPU backends.");
63static DEFINE_bool (dit , false, "Use device-independent text in GPU backends.");
64static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces.");
65
66static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing.");
67static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing.");
68static DEFINE_bool(releaseAndAbandonGpuContext, false,
69 "Release all GPU resources and abandon the GrContext after drawing.");
70
71static DEFINE_bool(decodeToDst, false,
72 "Decode images to destination format rather than suggested natural format.");
73
Mike Kleinc245bd92019-03-27 14:31:09 -050074static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI,
75 "DPI for rasterized content in vector backends like --backend pdf.");
76static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?");
77
Mike Klein735c7ba2019-03-25 12:57:58 -050078static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?");
79static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set.");
Mike Kleined2cec42019-11-01 10:51:01 -050080static DEFINE_bool (quick, false, "Skip image hashing and encoding?");
Mike Klein735c7ba2019-03-25 12:57:58 -050081
Brian Osman5aa11fb2019-04-08 16:40:36 -040082static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set.");
83
Mike Klein735c7ba2019-03-25 12:57:58 -050084static DEFINE_string(key, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein7b8bc532019-04-09 11:19:47 -050085static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein735c7ba2019-03-25 12:57:58 -050086
87template <typename T>
88struct FlagOption {
89 const char* label;
90 T value;
91};
92
93template <typename T, int N>
94static 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
Mike Kleina833cff2019-04-09 11:52:40 -0500111struct Result {
112 enum { Ok, Skip, Fail} status;
113 SkString failure;
114};
115static const Result ok = {Result::Ok, {}},
116 skip = {Result::Skip, {}};
117
Adlai Holler684838f2020-05-12 10:41:04 -0400118static Result fail(const char* why) {
119 return { Result::Fail, SkString(why) };
120}
Mike Kleina833cff2019-04-09 11:52:40 -0500121template <typename... Args>
Adlai Holler684838f2020-05-12 10:41:04 -0400122static Result fail(const char* whyFmt, Args... args) {
123 return { Result::Fail, SkStringPrintf(whyFmt, args...) };
Mike Kleina833cff2019-04-09 11:52:40 -0500124}
125
126
Mike Klein735c7ba2019-03-25 12:57:58 -0500127struct Source {
128 SkString name;
129 SkISize size;
Mike Kleina833cff2019-04-09 11:52:40 -0500130 std::function<Result(SkCanvas*)> draw;
131 std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
Mike Klein735c7ba2019-03-25 12:57:58 -0500132};
133
Mike Kleina833cff2019-04-09 11:52:40 -0500134static 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) {
Adlai Hollere3ad5272020-07-07 10:27:55 -0400138 auto direct = GrAsDirectContext(canvas->recordingContext());
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400139
Mike Kleina833cff2019-04-09 11:52:40 -0500140 SkString err;
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400141 switch (gm->gpuSetup(direct, canvas, &err)) {
Robert Phillips900630c2020-06-24 10:35:00 -0400142 case skiagm::DrawResult::kOk : break;
143 case skiagm::DrawResult::kSkip: return skip;
144 case skiagm::DrawResult::kFail: return fail(err.c_str());
145 }
146
Mike Kleina833cff2019-04-09 11:52:40 -0500147 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;
Mike Klein735c7ba2019-03-25 12:57:58 -0500153 };
154}
155
Mike Kleina833cff2019-04-09 11:52:40 -0500156static 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;
Mike Klein735c7ba2019-03-25 12:57:58 -0500161 };
162}
163
Mike Kleina833cff2019-04-09 11:52:40 -0500164static 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) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400169 info = canvas->imageInfo().makeDimensions(info.dimensions());
Mike Kleina833cff2019-04-09 11:52:40 -0500170 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500171
Mike Kleina833cff2019-04-09 11:52:40 -0500172 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;
Mike Klein735c7ba2019-03-25 12:57:58 -0500182 };
183}
184
Mike Kleina833cff2019-04-09 11:52:40 -0500185static 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;
Mike Klein22924262019-04-02 14:14:56 -0400191 };
192}
193
Brian Osmanca946562019-04-10 13:51:53 -0400194#if defined(SK_ENABLE_SKOTTIE)
Mike Kleina833cff2019-04-09 11:52:40 -0500195static 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);
Mike Klein22924262019-04-02 14:14:56 -0400199
Mike Kleina833cff2019-04-09 11:52:40 -0500200 // 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;
Mike Klein22924262019-04-02 14:14:56 -0400205
Mike Kleina833cff2019-04-09 11:52:40 -0500206 const float dt = 1.0f / (tiles*tiles - 1);
Mike Klein22924262019-04-02 14:14:56 -0400207
Mike Kleina833cff2019-04-09 11:52:40 -0500208 for (int y : order)
209 for (int x : order) {
210 SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
Mike Klein22924262019-04-02 14:14:56 -0400211
John Stiles8cc118d2020-07-30 11:22:35 -0400212 SkAutoCanvasRestore _(canvas, /*doSave=*/true);
213 canvas->clipRect(dst, /*doAntiAlias=*/true);
Mike Reed2ac6ce82021-01-15 12:26:22 -0500214 canvas->concat(SkMatrix::RectToRect(SkRect::MakeSize(animation->size()), dst,
215 SkMatrix::kCenter_ScaleToFit));
Mike Kleina833cff2019-04-09 11:52:40 -0500216 float t = (y*tiles + x) * dt;
217 animation->seek(t);
218 animation->render(canvas);
219 }
220 return ok;
Mike Klein6253d902019-03-27 15:09:12 -0500221 };
222}
Brian Osmanca946562019-04-10 13:51:53 -0400223#endif
Mike Klein6253d902019-03-27 15:09:12 -0500224
Mike Kleinfee00792019-11-21 16:18:47 -0600225static void init(Source* source, const skiatest::Test& test) {
226 source->size = {1,1};
227 source->draw = [test](SkCanvas* canvas) {
228 struct Reporter : public skiatest::Reporter {
229 SkString msg;
230
231 void reportFailed(const skiatest::Failure& failure) override {
Mike Klein484b63b2020-04-15 12:43:12 -0500232 msg += failure.toString();
233 msg += "\n";
Mike Kleinfee00792019-11-21 16:18:47 -0600234 }
235 } reporter;
236
237 test.run(&reporter, GrContextOptions{});
238
239 if (reporter.msg.isEmpty()) {
240 canvas->clear(SK_ColorGREEN);
241 return ok;
242 }
243
244 canvas->clear(SK_ColorRED);
245 return fail(reporter.msg.c_str());
246 };
247}
248
Mike Kleina5c27172019-04-02 10:26:48 -0400249static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500250 SkImageInfo info) {
251 if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400252 if (draw(surface->getCanvas())) {
253 return surface->makeImageSnapshot();
254 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500255 }
256 return nullptr;
257}
258
Mike Kleina5c27172019-04-02 10:26:48 -0400259static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
Mike Klein9b462092019-03-27 13:52:35 -0500260 SkImageInfo info) {
261 SkPictureRecorder recorder;
Mike Kleina5c27172019-04-02 10:26:48 -0400262 if (draw(recorder.beginRecording(info.width(), info.height()))) {
263 return recorder.finishRecordingAsPicture()->serialize();
264 }
265 return nullptr;
Mike Klein9b462092019-03-27 13:52:35 -0500266}
267
Mike Kleina5c27172019-04-02 10:26:48 -0400268static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
Mike Kleinc245bd92019-03-27 14:31:09 -0500269 SkImageInfo info,
270 SkString name) {
271 SkPDF::Metadata metadata;
272 metadata.fTitle = name;
273 metadata.fCreator = "Skia/FM";
274 metadata.fRasterDPI = FLAGS_rasterDPI;
275 metadata.fPDFA = FLAGS_PDFA;
276
277 SkDynamicMemoryWStream stream;
278 if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400279 if (draw(doc->beginPage(info.width(), info.height()))) {
280 doc->endPage();
281 doc->close();
282 return stream.detachAsData();
283 }
Mike Kleinc245bd92019-03-27 14:31:09 -0500284 }
285 return nullptr;
286}
287
Mike Kleina5c27172019-04-02 10:26:48 -0400288static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500289 SkImageInfo info,
290 GrContextFactory::ContextType api,
291 GrContextFactory* factory) {
292 enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
293 const FlagOption<SurfaceType> kSurfaceTypes[] = {
294 { "default", SurfaceType::kDefault },
295 { "betex" , SurfaceType::kBackendTexture },
296 { "bert" , SurfaceType::kBackendRenderTarget },
297 };
298 SurfaceType surfaceType;
299 if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
300 return nullptr;
301 }
302
Chris Daltonb3c97452019-06-25 20:07:56 -0600303 auto overrides = GrContextFactory::ContextOverrides::kNone;
Mike Klein735c7ba2019-03-25 12:57:58 -0500304 if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
305
Robert Phillips7b4e43c2020-07-01 16:59:17 -0400306 auto context = factory->getContextInfo(api, overrides).directContext();
Mike Klein735c7ba2019-03-25 12:57:58 -0500307
308 uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
309 : 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -0400310 SkSurfaceProps props(flags, kRGB_H_SkPixelGeometry);
Mike Klein735c7ba2019-03-25 12:57:58 -0500311
312 sk_sp<SkSurface> surface;
Mike Klein735c7ba2019-03-25 12:57:58 -0500313
314 switch (surfaceType) {
315 case SurfaceType::kDefault:
316 surface = SkSurface::MakeRenderTarget(context,
317 SkBudgeted::kNo,
318 info,
319 FLAGS_samples,
320 &props);
321 break;
322
323 case SurfaceType::kBackendTexture:
Brian Salomonf9b00422020-10-08 16:00:14 -0400324 surface = sk_gpu_test::MakeBackendTextureSurface(context,
Brian Salomon72050802020-10-12 20:45:06 -0400325 info,
Brian Salomonf9b00422020-10-08 16:00:14 -0400326 kTopLeft_GrSurfaceOrigin,
327 FLAGS_samples,
Brian Salomonf9b00422020-10-08 16:00:14 -0400328 GrMipmapped::kNo,
329 GrProtected::kNo,
330 &props);
Mike Klein735c7ba2019-03-25 12:57:58 -0500331 break;
332
333 case SurfaceType::kBackendRenderTarget:
Brian Salomonf9b00422020-10-08 16:00:14 -0400334 surface = sk_gpu_test::MakeBackendRenderTargetSurface(context,
Brian Salomon72050802020-10-12 20:45:06 -0400335 info,
Brian Salomonf9b00422020-10-08 16:00:14 -0400336 kBottomLeft_GrSurfaceOrigin,
337 FLAGS_samples,
Brian Salomonf9b00422020-10-08 16:00:14 -0400338 GrProtected::kNo,
339 &props);
Mike Klein735c7ba2019-03-25 12:57:58 -0500340 break;
341 }
342
343 if (!surface) {
344 fprintf(stderr, "Could not create GPU surface.\n");
345 return nullptr;
346 }
347
348 if (FLAGS_preAbandonGpuContext) {
349 factory->abandonContexts();
350 }
351
Mike Kleina5c27172019-04-02 10:26:48 -0400352 sk_sp<SkImage> image;
353 if (draw(surface->getCanvas())) {
354 image = surface->makeImageSnapshot();
355 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500356
357 if (FLAGS_abandonGpuContext) {
358 factory->abandonContexts();
359 } else if (FLAGS_releaseAndAbandonGpuContext) {
360 factory->releaseResourcesAndAbandonContexts();
361 }
362
Mike Klein735c7ba2019-03-25 12:57:58 -0500363 return image;
364}
365
Mike Klein2a57e792020-01-14 12:47:52 -0600366extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500367extern bool gSkVMAllowJIT;
Mike Klein31bfa832020-01-22 11:36:40 -0600368extern bool gSkVMJITViaDylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600369
Mike Klein735c7ba2019-03-25 12:57:58 -0500370int main(int argc, char** argv) {
371 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500372 SetupCrashHandler();
Mike Klein735c7ba2019-03-25 12:57:58 -0500373
374 if (FLAGS_cpuDetect) {
375 SkGraphics::Init();
376 }
Mike Klein31bfa832020-01-22 11:36:40 -0600377 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500378 gSkVMAllowJIT = FLAGS_jit;
Mike Klein31bfa832020-01-22 11:36:40 -0600379 gSkVMJITViaDylib = FLAGS_dylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600380
Mike Klein735c7ba2019-03-25 12:57:58 -0500381 initializeEventTracingForTools();
382 ToolUtils::SetDefaultFontMgr();
383 SetAnalyticAAFromCommonFlags();
384
385 GrContextOptions baseOptions;
386 SetCtxOptionsFromCommonFlags(&baseOptions);
387
Brian Osman5aa11fb2019-04-08 16:40:36 -0400388 sk_gpu_test::MemoryCache memoryCache;
389 if (!FLAGS_writeShaders.isEmpty()) {
390 baseOptions.fPersistentCache = &memoryCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400391 baseOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5aa11fb2019-04-08 16:40:36 -0400392 }
393
Mike Kleincbe93ee2019-04-02 17:00:54 -0400394 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500395 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Hal Canaryedda5652019-08-05 10:28:09 -0400396 std::unique_ptr<skiagm::GM> gm{factory()};
Mike Kleinfee00792019-11-21 16:18:47 -0600397 if (FLAGS_listGMs) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500398 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400399 } else {
400 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500401 }
402 }
Mike Kleinfee00792019-11-21 16:18:47 -0600403
404 SkTHashMap<SkString, const skiatest::Test*> tests;
405 for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
406 if (test.needsGpu) {
407 continue; // TODO
408 }
409 if (FLAGS_listTests) {
410 fprintf(stdout, "%s\n", test.name);
411 } else {
412 tests.set(SkString{test.name}, &test);
413 }
414 }
415
416 if (FLAGS_listGMs || FLAGS_listTests) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400417 return 0;
418 }
Mike Kleinfee00792019-11-21 16:18:47 -0600419 if (FLAGS_sources.isEmpty()) {
420 fprintf(stderr, "Please give me something to run using -s/--sources!\n");
421 return 1;
422 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400423
424 SkTArray<Source> sources;
Mike Kleina833cff2019-04-09 11:52:40 -0500425 for (const SkString& name : FLAGS_sources) {
426 Source* source = &sources.push_back();
427
428 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Hal Canaryedda5652019-08-05 10:28:09 -0400429 std::shared_ptr<skiagm::GM> gm{(*factory)()};
Mike Kleina833cff2019-04-09 11:52:40 -0500430 source->name = name;
Ben Wagner406ff502019-08-12 16:39:24 -0400431 init(source, std::move(gm));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400432 continue;
433 }
434
Mike Kleinfee00792019-11-21 16:18:47 -0600435 if (const skiatest::Test** test = tests.find(name)) {
436 source->name = name;
437 init(source, **test);
438 continue;
439 }
440
Mike Kleina833cff2019-04-09 11:52:40 -0500441 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
442 source->name = SkOSPath::Basename(name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500443
Mike Kleince90d6f2019-03-29 11:14:14 -0500444 if (name.endsWith(".skp")) {
445 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500446 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400447 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500448 }
449 } else if (name.endsWith(".svg")) {
450 SkMemoryStream stream{blob};
451 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500452 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400453 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500454 }
Brian Osmanca946562019-04-10 13:51:53 -0400455 }
456#if defined(SK_ENABLE_SKOTTIE)
457 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500458 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400459 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
Brian Osman849f4d62019-11-26 08:58:26 -0500460 .setResourceProvider(skresources::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400461 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500462 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400463 continue;
Mike Klein22924262019-04-02 14:14:56 -0400464 }
Brian Osmanca946562019-04-10 13:51:53 -0400465 }
466#endif
467 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500468 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400469 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500470 }
471 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400472
Mike Kleina833cff2019-04-09 11:52:40 -0500473 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400474 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500475 }
476
Mike Klein9b462092019-03-27 13:52:35 -0500477 enum NonGpuBackends {
478 kCPU_Backend = -1,
479 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500480 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500481 };
482 const FlagOption<int> kBackends[] = {
483 { "cpu" , kCPU_Backend },
484 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500485 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500486 { "gl" , GrContextFactory::kGL_ContextType },
487 { "gles" , GrContextFactory::kGLES_ContextType },
488 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
489 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
490 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
491 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
492 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
493 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
494 { "vk" , GrContextFactory::kVulkan_ContextType },
495 { "mtl" , GrContextFactory::kMetal_ContextType },
496 { "mock" , GrContextFactory::kMock_ContextType },
497 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500498 const FlagOption<SkColorType> kColorTypes[] = {
Mike Kleind58e01b62020-07-17 13:52:41 -0500499 { "a8", kAlpha_8_SkColorType },
500 { "g8", kGray_8_SkColorType },
501 { "565", kRGB_565_SkColorType },
502 { "4444", kARGB_4444_SkColorType },
503 { "8888", kN32_SkColorType },
504 { "888x", kRGB_888x_SkColorType },
505 { "1010102", kRGBA_1010102_SkColorType },
506 { "101010x", kRGB_101010x_SkColorType },
507 { "bgra1010102", kBGRA_1010102_SkColorType },
508 { "bgr101010x", kBGR_101010x_SkColorType },
509 { "f16norm", kRGBA_F16Norm_SkColorType },
510 { "f16", kRGBA_F16_SkColorType },
511 { "f32", kRGBA_F32_SkColorType },
512 { "rgba", kRGBA_8888_SkColorType },
513 { "bgra", kBGRA_8888_SkColorType },
514 { "16161616", kR16G16B16A16_unorm_SkColorType },
Mike Klein735c7ba2019-03-25 12:57:58 -0500515 };
516 const FlagOption<SkAlphaType> kAlphaTypes[] = {
517 { "premul", kPremul_SkAlphaType },
518 { "unpremul", kUnpremul_SkAlphaType },
519 };
520 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
521 { "srgb", SkNamedGamut::kSRGB },
Mike Kleinb147ace2020-01-16 11:11:06 -0600522 { "p3", SkNamedGamut::kDisplayP3 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500523 { "rec2020", SkNamedGamut::kRec2020 },
524 { "adobe", SkNamedGamut::kAdobeRGB },
525 { "narrow", gNarrow_toXYZD50},
526 };
527 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
528 { "srgb" , SkNamedTransferFn::kSRGB },
Hal Canarybe67a172019-05-24 10:13:36 -0400529 { "rec2020", SkNamedTransferFn::kRec2020 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500530 { "2.2" , SkNamedTransferFn::k2Dot2 },
531 { "linear" , SkNamedTransferFn::kLinear },
532 };
533
Mike Klein735c7ba2019-03-25 12:57:58 -0500534
Mike Klein9b462092019-03-27 13:52:35 -0500535 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500536 SkColorType ct;
537 SkAlphaType at;
538 skcms_Matrix3x3 gamut;
539 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500540
Mike Klein9b462092019-03-27 13:52:35 -0500541 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
542 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500543 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
544 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500545 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500546 return 1;
547 }
548
Mike Klein4daf6372019-04-03 11:19:45 -0400549 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
550 : SkColorSpace::MakeRGB(tf,gamut);
551 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500552
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400553 AutoreleasePool pool;
Mike Klein735c7ba2019-03-25 12:57:58 -0500554 for (auto source : sources) {
555 const auto start = std::chrono::steady_clock::now();
Mike Klein832d3c52019-04-02 15:23:46 -0400556 fprintf(stdout, "%50s", source.name.c_str());
Mike Kleinbf15b662019-04-15 11:32:16 -0500557 fflush(stdout);
Mike Klein735c7ba2019-03-25 12:57:58 -0500558
Brian Salomon9241a6d2019-10-03 13:26:54 -0400559 const SkImageInfo info = unsized_info.makeDimensions(source.size);
Mike Klein735c7ba2019-03-25 12:57:58 -0500560
Mike Kleina833cff2019-04-09 11:52:40 -0500561 auto draw = [&source](SkCanvas* canvas) {
562 Result result = source.draw(canvas);
563 switch (result.status) {
564 case Result::Ok: break;
565 case Result::Skip: return false;
566 case Result::Fail:
John Stiles616da102020-06-12 14:07:41 -0400567 SK_ABORT("%s", result.failure.c_str());
Mike Kleina833cff2019-04-09 11:52:40 -0500568 }
569 return true;
570 };
571
Mike Klein735c7ba2019-03-25 12:57:58 -0500572 GrContextOptions options = baseOptions;
573 source.tweak(&options);
574 GrContextFactory factory(options); // N.B. factory must outlive image
575
576 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500577 sk_sp<SkData> blob;
578 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500579 switch (backend) {
580 case kCPU_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500581 image = draw_with_cpu(draw, info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500582 break;
Mike Klein9b462092019-03-27 13:52:35 -0500583 case kSKP_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500584 blob = draw_as_skp(draw, info);
Mike Klein9b462092019-03-27 13:52:35 -0500585 ext = ".skp";
586 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500587 case kPDF_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500588 blob = draw_as_pdf(draw, info, source.name);
Mike Kleinc245bd92019-03-27 14:31:09 -0500589 ext = ".pdf";
590 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500591 default:
Mike Kleina833cff2019-04-09 11:52:40 -0500592 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500593 break;
594 }
595
Mike Klein9b462092019-03-27 13:52:35 -0500596 if (!image && !blob) {
Mike Klein832d3c52019-04-02 15:23:46 -0400597 fprintf(stdout, "\tskipped\n");
Mike Kleina5c27172019-04-02 10:26:48 -0400598 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500599 }
600
Mike Kleined2cec42019-11-01 10:51:01 -0500601 // We read back a bitmap even when --quick is set and we won't use it,
602 // to keep us honest about deferred work, flushing pipelines, etc.
Mike Klein735c7ba2019-03-25 12:57:58 -0500603 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500604 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500605 SK_ABORT("SkImage::asLegacyBitmap() failed.");
Mike Klein735c7ba2019-03-25 12:57:58 -0500606 }
607
Mike Klein735c7ba2019-03-25 12:57:58 -0500608 SkString md5;
Mike Kleined2cec42019-11-01 10:51:01 -0500609 if (!FLAGS_quick) {
610 HashAndEncode hashAndEncode{bitmap};
611 {
612 SkMD5 hash;
613 if (image) {
Mike Klein989f5bf2020-10-07 10:11:50 -0500614 hashAndEncode.feedHash(&hash);
Mike Kleined2cec42019-11-01 10:51:01 -0500615 } else {
616 hash.write(blob->data(), blob->size());
Mike Klein9b462092019-03-27 13:52:35 -0500617 }
Mike Kleined2cec42019-11-01 10:51:01 -0500618
619 SkMD5::Digest digest = hash.finish();
620 for (int i = 0; i < 16; i++) {
621 md5.appendf("%02x", digest.data[i]);
622 }
623 }
624
625 if (!FLAGS_writePath.isEmpty()) {
626 sk_mkdir(FLAGS_writePath[0]);
627 SkString path = SkStringPrintf("%s/%s%s",
628 FLAGS_writePath[0], source.name.c_str(), ext);
629
Mike Klein989f5bf2020-10-07 10:11:50 -0500630 SkFILEWStream file(path.c_str());
Mike Kleined2cec42019-11-01 10:51:01 -0500631 if (image) {
Mike Klein989f5bf2020-10-07 10:11:50 -0500632 if (!hashAndEncode.encodePNG(&file, md5.c_str(),
633 FLAGS_key, FLAGS_properties)) {
Mike Kleined2cec42019-11-01 10:51:01 -0500634 SK_ABORT("Could not write .png.");
635 }
636 } else {
Mike Kleined2cec42019-11-01 10:51:01 -0500637 file.write(blob->data(), blob->size());
638 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500639 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500640 }
641
Mike Klein832d3c52019-04-02 15:23:46 -0400642 const auto elapsed = std::chrono::steady_clock::now() - start;
643 fprintf(stdout, "\t%s\t%7dms\n",
644 md5.c_str(),
645 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400646 pool.drain();
Mike Klein735c7ba2019-03-25 12:57:58 -0500647 }
648
Brian Osman5aa11fb2019-04-08 16:40:36 -0400649 if (!FLAGS_writeShaders.isEmpty()) {
650 sk_mkdir(FLAGS_writeShaders[0]);
651 GrBackendApi api =
652 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
653 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
654
655 }
656
Mike Klein735c7ba2019-03-25 12:57:58 -0500657 return 0;
658}