blob: e91c0344ae89ba857afd2b639423a83396ed9d5b [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>
Mike Kleina9e62e82021-03-02 10:44:32 -060036#include <limits.h>
Mike Klein735c7ba2019-03-25 12:57:58 -050037#include <stdio.h>
38#include <stdlib.h>
39
Brian Osmanca946562019-04-10 13:51:53 -040040#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041 #include "modules/skottie/include/Skottie.h"
Brian Osman849f4d62019-11-26 08:58:26 -050042 #include "modules/skresources/include/SkResources.h"
Brian Osmanca946562019-04-10 13:51:53 -040043#endif
44
Mike Klein735c7ba2019-03-25 12:57:58 -050045using sk_gpu_test::GrContextFactory;
46
Mike Kleinfee00792019-11-21 16:18:47 -060047static DEFINE_bool(listGMs , false, "Print GM names and exit.");
48static DEFINE_bool(listTests, false, "Print unit test names and exit.");
49
Mike Klein735c7ba2019-03-25 12:57:58 -050050static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw.");
51static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into.");
52
Mike Klein4daf6372019-04-03 11:19:45 -040053static DEFINE_string(ct , "8888", "The color type for any raster backend.");
54static DEFINE_string(at , "premul", "The alpha type for any raster backend.");
55static DEFINE_string(gamut , "srgb", "The color gamut for any raster backend.");
56static DEFINE_string(tf , "srgb", "The transfer function for any raster backend.");
57static DEFINE_bool (legacy, false, "Use a null SkColorSpace instead of --gamut and --tf?");
Mike Klein2a57e792020-01-14 12:47:52 -060058static DEFINE_bool (skvm , false, "Use SkVMBlitter when supported?");
Mike Klein813e8cc2020-08-05 09:33:38 -050059static DEFINE_bool (jit , true, "JIT SkVM?");
60static DEFINE_bool (dylib , false, "JIT SkVM via dylib?");
Mike Klein735c7ba2019-03-25 12:57:58 -050061
62static DEFINE_int (samples , 0, "Samples per pixel in GPU backends.");
Mike Klein735c7ba2019-03-25 12:57:58 -050063static DEFINE_bool (stencils, true, "If false, avoid stencil buffers in GPU backends.");
64static DEFINE_bool (dit , false, "Use device-independent text in GPU backends.");
65static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces.");
66
67static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing.");
68static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing.");
69static DEFINE_bool(releaseAndAbandonGpuContext, false,
70 "Release all GPU resources and abandon the GrContext after drawing.");
71
72static DEFINE_bool(decodeToDst, false,
73 "Decode images to destination format rather than suggested natural format.");
74
Mike Kleinc245bd92019-03-27 14:31:09 -050075static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI,
76 "DPI for rasterized content in vector backends like --backend pdf.");
77static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?");
78
Mike Kleina9e62e82021-03-02 10:44:32 -060079static DEFINE_int(clipW, INT_MAX, "Limit source width.");
80static DEFINE_int(clipH, INT_MAX, "Limit source height.");
81
Mike Klein735c7ba2019-03-25 12:57:58 -050082static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?");
83static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set.");
Mike Kleined2cec42019-11-01 10:51:01 -050084static DEFINE_bool (quick, false, "Skip image hashing and encoding?");
Mike Klein735c7ba2019-03-25 12:57:58 -050085
Brian Osman5aa11fb2019-04-08 16:40:36 -040086static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set.");
87
Mike Klein735c7ba2019-03-25 12:57:58 -050088static DEFINE_string(key, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein7b8bc532019-04-09 11:19:47 -050089static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein735c7ba2019-03-25 12:57:58 -050090
91template <typename T>
92struct FlagOption {
93 const char* label;
94 T value;
95};
96
97template <typename T, int N>
98static bool parse_flag(const CommandLineFlags::StringArray& flag,
99 const char* flag_name,
100 const FlagOption<T> (&array)[N],
101 T* value) {
102 for (auto entry : array) {
103 if (flag.contains(entry.label)) {
104 *value = entry.value;
105 return true;
106 }
107 }
108 fprintf(stderr, "Known values for --%s:\n", flag_name);
109 for (auto entry : array) {
110 fprintf(stderr, " --%s %s\n", flag_name, entry.label);
111 }
112 return false;
113}
114
Mike Kleina833cff2019-04-09 11:52:40 -0500115struct Result {
116 enum { Ok, Skip, Fail} status;
117 SkString failure;
118};
119static const Result ok = {Result::Ok, {}},
120 skip = {Result::Skip, {}};
121
Adlai Holler684838f2020-05-12 10:41:04 -0400122static Result fail(const char* why) {
123 return { Result::Fail, SkString(why) };
124}
Mike Kleina833cff2019-04-09 11:52:40 -0500125template <typename... Args>
Adlai Holler684838f2020-05-12 10:41:04 -0400126static Result fail(const char* whyFmt, Args... args) {
127 return { Result::Fail, SkStringPrintf(whyFmt, args...) };
Mike Kleina833cff2019-04-09 11:52:40 -0500128}
129
130
Mike Klein735c7ba2019-03-25 12:57:58 -0500131struct Source {
132 SkString name;
133 SkISize size;
Mike Kleina833cff2019-04-09 11:52:40 -0500134 std::function<Result(SkCanvas*)> draw;
135 std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
Mike Klein735c7ba2019-03-25 12:57:58 -0500136};
137
Mike Kleina833cff2019-04-09 11:52:40 -0500138static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
139 source->size = gm->getISize();
140 source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
141 source->draw = [gm](SkCanvas* canvas) {
Adlai Hollere3ad5272020-07-07 10:27:55 -0400142 auto direct = GrAsDirectContext(canvas->recordingContext());
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400143
Mike Kleina833cff2019-04-09 11:52:40 -0500144 SkString err;
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400145 switch (gm->gpuSetup(direct, canvas, &err)) {
Robert Phillips900630c2020-06-24 10:35:00 -0400146 case skiagm::DrawResult::kOk : break;
147 case skiagm::DrawResult::kSkip: return skip;
148 case skiagm::DrawResult::kFail: return fail(err.c_str());
149 }
150
Mike Kleina833cff2019-04-09 11:52:40 -0500151 switch (gm->draw(canvas, &err)) {
152 case skiagm::DrawResult::kOk: break;
153 case skiagm::DrawResult::kSkip: return skip;
154 case skiagm::DrawResult::kFail: return fail(err.c_str());
155 }
156 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500157 };
158}
159
Mike Kleina833cff2019-04-09 11:52:40 -0500160static void init(Source* source, sk_sp<SkPicture> pic) {
161 source->size = pic->cullRect().roundOut().size();
162 source->draw = [pic](SkCanvas* canvas) {
163 canvas->drawPicture(pic);
164 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500165 };
166}
167
Mike Kleina833cff2019-04-09 11:52:40 -0500168static void init(Source* source, std::shared_ptr<SkCodec> codec) {
169 source->size = codec->dimensions();
170 source->draw = [codec](SkCanvas* canvas) {
171 SkImageInfo info = codec->getInfo();
172 if (FLAGS_decodeToDst) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400173 info = canvas->imageInfo().makeDimensions(info.dimensions());
Mike Kleina833cff2019-04-09 11:52:40 -0500174 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500175
Mike Reed844beb52021-01-25 15:36:09 -0500176 auto [image, result] = codec->getImage(info);
177 if (image) {
178 canvas->drawImage(image, 0,0);
179 return ok;
Mike Kleina833cff2019-04-09 11:52:40 -0500180 }
Mike Reed844beb52021-01-25 15:36:09 -0500181 return fail("codec->getPixels() failed: %d\n", result);
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) {
Mike Kleind1131b62021-02-02 17:48:29 -0600186 if (svg->containerSize().isEmpty()) {
187 svg->setContainerSize({1000,1000});
188 }
189 source->size = svg->containerSize().toCeil();
Mike Kleina833cff2019-04-09 11:52:40 -0500190 source->draw = [svg](SkCanvas* canvas) {
191 svg->render(canvas);
192 return ok;
Mike Klein22924262019-04-02 14:14:56 -0400193 };
194}
195
Brian Osmanca946562019-04-10 13:51:53 -0400196#if defined(SK_ENABLE_SKOTTIE)
Mike Kleina833cff2019-04-09 11:52:40 -0500197static void init(Source* source, sk_sp<skottie::Animation> animation) {
198 source->size = {1000,1000};
199 source->draw = [animation](SkCanvas* canvas) {
200 canvas->clear(SK_ColorWHITE);
Mike Klein22924262019-04-02 14:14:56 -0400201
Mike Kleina833cff2019-04-09 11:52:40 -0500202 // Draw frames in a shuffled order to exercise nonlinear frame progression.
203 // The film strip will still be in time order, just drawn out of order.
204 const int order[] = { 4, 0, 3, 1, 2 };
205 const int tiles = SK_ARRAY_COUNT(order);
206 const float dim = 1000.0f / tiles;
Mike Klein22924262019-04-02 14:14:56 -0400207
Mike Kleina833cff2019-04-09 11:52:40 -0500208 const float dt = 1.0f / (tiles*tiles - 1);
Mike Klein22924262019-04-02 14:14:56 -0400209
Mike Kleina833cff2019-04-09 11:52:40 -0500210 for (int y : order)
211 for (int x : order) {
212 SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
Mike Klein22924262019-04-02 14:14:56 -0400213
John Stiles8cc118d2020-07-30 11:22:35 -0400214 SkAutoCanvasRestore _(canvas, /*doSave=*/true);
215 canvas->clipRect(dst, /*doAntiAlias=*/true);
Mike Reed2ac6ce82021-01-15 12:26:22 -0500216 canvas->concat(SkMatrix::RectToRect(SkRect::MakeSize(animation->size()), dst,
217 SkMatrix::kCenter_ScaleToFit));
Mike Kleina833cff2019-04-09 11:52:40 -0500218 float t = (y*tiles + x) * dt;
219 animation->seek(t);
220 animation->render(canvas);
221 }
222 return ok;
Mike Klein6253d902019-03-27 15:09:12 -0500223 };
224}
Brian Osmanca946562019-04-10 13:51:53 -0400225#endif
Mike Klein6253d902019-03-27 15:09:12 -0500226
Mike Kleinfee00792019-11-21 16:18:47 -0600227static void init(Source* source, const skiatest::Test& test) {
228 source->size = {1,1};
229 source->draw = [test](SkCanvas* canvas) {
230 struct Reporter : public skiatest::Reporter {
231 SkString msg;
232
233 void reportFailed(const skiatest::Failure& failure) override {
Mike Klein484b63b2020-04-15 12:43:12 -0500234 msg += failure.toString();
235 msg += "\n";
Mike Kleinfee00792019-11-21 16:18:47 -0600236 }
237 } reporter;
238
239 test.run(&reporter, GrContextOptions{});
240
241 if (reporter.msg.isEmpty()) {
242 canvas->clear(SK_ColorGREEN);
243 return ok;
244 }
245
246 canvas->clear(SK_ColorRED);
247 return fail(reporter.msg.c_str());
248 };
249}
250
Mike Kleina5c27172019-04-02 10:26:48 -0400251static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500252 SkImageInfo info) {
253 if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400254 if (draw(surface->getCanvas())) {
255 return surface->makeImageSnapshot();
256 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500257 }
258 return nullptr;
259}
260
Mike Kleina5c27172019-04-02 10:26:48 -0400261static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
Mike Klein9b462092019-03-27 13:52:35 -0500262 SkImageInfo info) {
263 SkPictureRecorder recorder;
Mike Kleina5c27172019-04-02 10:26:48 -0400264 if (draw(recorder.beginRecording(info.width(), info.height()))) {
265 return recorder.finishRecordingAsPicture()->serialize();
266 }
267 return nullptr;
Mike Klein9b462092019-03-27 13:52:35 -0500268}
269
Mike Kleina5c27172019-04-02 10:26:48 -0400270static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
Mike Kleinc245bd92019-03-27 14:31:09 -0500271 SkImageInfo info,
272 SkString name) {
273 SkPDF::Metadata metadata;
274 metadata.fTitle = name;
275 metadata.fCreator = "Skia/FM";
276 metadata.fRasterDPI = FLAGS_rasterDPI;
277 metadata.fPDFA = FLAGS_PDFA;
278
279 SkDynamicMemoryWStream stream;
280 if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400281 if (draw(doc->beginPage(info.width(), info.height()))) {
282 doc->endPage();
283 doc->close();
284 return stream.detachAsData();
285 }
Mike Kleinc245bd92019-03-27 14:31:09 -0500286 }
287 return nullptr;
288}
289
Mike Kleina5c27172019-04-02 10:26:48 -0400290static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500291 SkImageInfo info,
292 GrContextFactory::ContextType api,
293 GrContextFactory* factory) {
294 enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
295 const FlagOption<SurfaceType> kSurfaceTypes[] = {
296 { "default", SurfaceType::kDefault },
297 { "betex" , SurfaceType::kBackendTexture },
298 { "bert" , SurfaceType::kBackendRenderTarget },
299 };
300 SurfaceType surfaceType;
301 if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
302 return nullptr;
303 }
304
Chris Daltonb3c97452019-06-25 20:07:56 -0600305 auto overrides = GrContextFactory::ContextOverrides::kNone;
Mike Klein735c7ba2019-03-25 12:57:58 -0500306 if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
307
Robert Phillips7b4e43c2020-07-01 16:59:17 -0400308 auto context = factory->getContextInfo(api, overrides).directContext();
Mike Klein735c7ba2019-03-25 12:57:58 -0500309
310 uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
311 : 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -0400312 SkSurfaceProps props(flags, kRGB_H_SkPixelGeometry);
Mike Klein735c7ba2019-03-25 12:57:58 -0500313
314 sk_sp<SkSurface> surface;
Mike Klein735c7ba2019-03-25 12:57:58 -0500315
316 switch (surfaceType) {
317 case SurfaceType::kDefault:
318 surface = SkSurface::MakeRenderTarget(context,
319 SkBudgeted::kNo,
320 info,
321 FLAGS_samples,
322 &props);
323 break;
324
325 case SurfaceType::kBackendTexture:
Brian Salomonf9b00422020-10-08 16:00:14 -0400326 surface = sk_gpu_test::MakeBackendTextureSurface(context,
Brian Salomon72050802020-10-12 20:45:06 -0400327 info,
Brian Salomonf9b00422020-10-08 16:00:14 -0400328 kTopLeft_GrSurfaceOrigin,
329 FLAGS_samples,
Brian Salomonf9b00422020-10-08 16:00:14 -0400330 GrMipmapped::kNo,
331 GrProtected::kNo,
332 &props);
Mike Klein735c7ba2019-03-25 12:57:58 -0500333 break;
334
335 case SurfaceType::kBackendRenderTarget:
Brian Salomonf9b00422020-10-08 16:00:14 -0400336 surface = sk_gpu_test::MakeBackendRenderTargetSurface(context,
Brian Salomon72050802020-10-12 20:45:06 -0400337 info,
Brian Salomonf9b00422020-10-08 16:00:14 -0400338 kBottomLeft_GrSurfaceOrigin,
339 FLAGS_samples,
Brian Salomonf9b00422020-10-08 16:00:14 -0400340 GrProtected::kNo,
341 &props);
Mike Klein735c7ba2019-03-25 12:57:58 -0500342 break;
343 }
344
345 if (!surface) {
346 fprintf(stderr, "Could not create GPU surface.\n");
347 return nullptr;
348 }
349
350 if (FLAGS_preAbandonGpuContext) {
351 factory->abandonContexts();
352 }
353
Mike Kleina5c27172019-04-02 10:26:48 -0400354 sk_sp<SkImage> image;
355 if (draw(surface->getCanvas())) {
356 image = surface->makeImageSnapshot();
357 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500358
359 if (FLAGS_abandonGpuContext) {
360 factory->abandonContexts();
361 } else if (FLAGS_releaseAndAbandonGpuContext) {
362 factory->releaseResourcesAndAbandonContexts();
363 }
364
Mike Klein735c7ba2019-03-25 12:57:58 -0500365 return image;
366}
367
Mike Klein2a57e792020-01-14 12:47:52 -0600368extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500369extern bool gSkVMAllowJIT;
Mike Klein31bfa832020-01-22 11:36:40 -0600370extern bool gSkVMJITViaDylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600371
Mike Klein735c7ba2019-03-25 12:57:58 -0500372int main(int argc, char** argv) {
373 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500374 SetupCrashHandler();
Mike Klein735c7ba2019-03-25 12:57:58 -0500375
376 if (FLAGS_cpuDetect) {
377 SkGraphics::Init();
378 }
Mike Klein31bfa832020-01-22 11:36:40 -0600379 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500380 gSkVMAllowJIT = FLAGS_jit;
Mike Klein31bfa832020-01-22 11:36:40 -0600381 gSkVMJITViaDylib = FLAGS_dylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600382
Mike Klein735c7ba2019-03-25 12:57:58 -0500383 initializeEventTracingForTools();
384 ToolUtils::SetDefaultFontMgr();
385 SetAnalyticAAFromCommonFlags();
386
387 GrContextOptions baseOptions;
388 SetCtxOptionsFromCommonFlags(&baseOptions);
389
Brian Osman5aa11fb2019-04-08 16:40:36 -0400390 sk_gpu_test::MemoryCache memoryCache;
391 if (!FLAGS_writeShaders.isEmpty()) {
392 baseOptions.fPersistentCache = &memoryCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400393 baseOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5aa11fb2019-04-08 16:40:36 -0400394 }
395
Mike Kleincbe93ee2019-04-02 17:00:54 -0400396 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500397 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Hal Canaryedda5652019-08-05 10:28:09 -0400398 std::unique_ptr<skiagm::GM> gm{factory()};
Mike Kleinfee00792019-11-21 16:18:47 -0600399 if (FLAGS_listGMs) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500400 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400401 } else {
402 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500403 }
404 }
Mike Kleinfee00792019-11-21 16:18:47 -0600405
406 SkTHashMap<SkString, const skiatest::Test*> tests;
407 for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
408 if (test.needsGpu) {
409 continue; // TODO
410 }
411 if (FLAGS_listTests) {
412 fprintf(stdout, "%s\n", test.name);
413 } else {
414 tests.set(SkString{test.name}, &test);
415 }
416 }
417
418 if (FLAGS_listGMs || FLAGS_listTests) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400419 return 0;
420 }
Mike Kleinfee00792019-11-21 16:18:47 -0600421 if (FLAGS_sources.isEmpty()) {
422 fprintf(stderr, "Please give me something to run using -s/--sources!\n");
423 return 1;
424 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400425
426 SkTArray<Source> sources;
Mike Kleina833cff2019-04-09 11:52:40 -0500427 for (const SkString& name : FLAGS_sources) {
428 Source* source = &sources.push_back();
Mike Kleinfa962f52021-02-03 17:21:34 -0600429 source->name = name;
Mike Kleina833cff2019-04-09 11:52:40 -0500430
431 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Hal Canaryedda5652019-08-05 10:28:09 -0400432 std::shared_ptr<skiagm::GM> gm{(*factory)()};
Ben Wagner406ff502019-08-12 16:39:24 -0400433 init(source, std::move(gm));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400434 continue;
435 }
436
Mike Kleinfee00792019-11-21 16:18:47 -0600437 if (const skiatest::Test** test = tests.find(name)) {
Mike Kleinfee00792019-11-21 16:18:47 -0600438 init(source, **test);
439 continue;
440 }
441
Mike Kleina833cff2019-04-09 11:52:40 -0500442 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
Mike Kleince90d6f2019-03-29 11:14:14 -0500443 if (name.endsWith(".skp")) {
444 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500445 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400446 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500447 }
448 } else if (name.endsWith(".svg")) {
449 SkMemoryStream stream{blob};
450 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500451 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400452 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500453 }
Brian Osmanca946562019-04-10 13:51:53 -0400454 }
455#if defined(SK_ENABLE_SKOTTIE)
456 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500457 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400458 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
Brian Osman849f4d62019-11-26 08:58:26 -0500459 .setResourceProvider(skresources::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400460 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500461 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400462 continue;
Mike Klein22924262019-04-02 14:14:56 -0400463 }
Brian Osmanca946562019-04-10 13:51:53 -0400464 }
465#endif
466 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500467 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400468 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500469 }
470 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400471
Mike Kleina833cff2019-04-09 11:52:40 -0500472 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400473 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500474 }
475
Mike Klein9b462092019-03-27 13:52:35 -0500476 enum NonGpuBackends {
477 kCPU_Backend = -1,
478 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500479 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500480 };
481 const FlagOption<int> kBackends[] = {
482 { "cpu" , kCPU_Backend },
483 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500484 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500485 { "gl" , GrContextFactory::kGL_ContextType },
486 { "gles" , GrContextFactory::kGLES_ContextType },
487 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
488 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
489 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
490 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
491 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
492 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
493 { "vk" , GrContextFactory::kVulkan_ContextType },
494 { "mtl" , GrContextFactory::kMetal_ContextType },
495 { "mock" , GrContextFactory::kMock_ContextType },
496 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500497 const FlagOption<SkColorType> kColorTypes[] = {
Mike Kleind58e01b62020-07-17 13:52:41 -0500498 { "a8", kAlpha_8_SkColorType },
499 { "g8", kGray_8_SkColorType },
500 { "565", kRGB_565_SkColorType },
501 { "4444", kARGB_4444_SkColorType },
502 { "8888", kN32_SkColorType },
503 { "888x", kRGB_888x_SkColorType },
504 { "1010102", kRGBA_1010102_SkColorType },
505 { "101010x", kRGB_101010x_SkColorType },
506 { "bgra1010102", kBGRA_1010102_SkColorType },
507 { "bgr101010x", kBGR_101010x_SkColorType },
508 { "f16norm", kRGBA_F16Norm_SkColorType },
509 { "f16", kRGBA_F16_SkColorType },
510 { "f32", kRGBA_F32_SkColorType },
511 { "rgba", kRGBA_8888_SkColorType },
512 { "bgra", kBGRA_8888_SkColorType },
513 { "16161616", kR16G16B16A16_unorm_SkColorType },
Mike Klein735c7ba2019-03-25 12:57:58 -0500514 };
515 const FlagOption<SkAlphaType> kAlphaTypes[] = {
516 { "premul", kPremul_SkAlphaType },
517 { "unpremul", kUnpremul_SkAlphaType },
518 };
519 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
520 { "srgb", SkNamedGamut::kSRGB },
Mike Kleinb147ace2020-01-16 11:11:06 -0600521 { "p3", SkNamedGamut::kDisplayP3 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500522 { "rec2020", SkNamedGamut::kRec2020 },
523 { "adobe", SkNamedGamut::kAdobeRGB },
524 { "narrow", gNarrow_toXYZD50},
525 };
526 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
527 { "srgb" , SkNamedTransferFn::kSRGB },
Hal Canarybe67a172019-05-24 10:13:36 -0400528 { "rec2020", SkNamedTransferFn::kRec2020 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500529 { "2.2" , SkNamedTransferFn::k2Dot2 },
530 { "linear" , SkNamedTransferFn::kLinear },
531 };
532
Mike Klein735c7ba2019-03-25 12:57:58 -0500533
Mike Klein9b462092019-03-27 13:52:35 -0500534 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500535 SkColorType ct;
536 SkAlphaType at;
537 skcms_Matrix3x3 gamut;
538 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500539
Mike Klein9b462092019-03-27 13:52:35 -0500540 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
541 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500542 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
543 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500544 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500545 return 1;
546 }
547
Mike Klein4daf6372019-04-03 11:19:45 -0400548 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
549 : SkColorSpace::MakeRGB(tf,gamut);
Mike Klein6706c9f2021-02-26 12:22:07 -0600550 const SkColorInfo color_info{ct,at,cs};
Mike Klein735c7ba2019-03-25 12:57:58 -0500551
552 for (auto source : sources) {
Mike Kleindb8340f2021-03-01 10:44:32 -0600553 AutoreleasePool pool;
Mike Klein735c7ba2019-03-25 12:57:58 -0500554 const auto start = std::chrono::steady_clock::now();
Mike Klein735c7ba2019-03-25 12:57:58 -0500555
Mike Kleina9e62e82021-03-02 10:44:32 -0600556 auto [w,h] = source.size;
557 w = std::min(w, FLAGS_clipW);
558 h = std::min(h, FLAGS_clipH);
559 const SkImageInfo info = SkImageInfo::Make({w,h}, color_info);
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 Klein4636ac62021-03-01 11:09:22 -0600597 fprintf(stdout, "%50s skipped\n", source.name.c_str());
598 fflush(stdout);
Mike Kleina5c27172019-04-02 10:26:48 -0400599 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500600 }
601
Mike Kleined2cec42019-11-01 10:51:01 -0500602 // We read back a bitmap even when --quick is set and we won't use it,
603 // to keep us honest about deferred work, flushing pipelines, etc.
Mike Klein735c7ba2019-03-25 12:57:58 -0500604 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500605 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500606 SK_ABORT("SkImage::asLegacyBitmap() failed.");
Mike Klein735c7ba2019-03-25 12:57:58 -0500607 }
608
Mike Klein735c7ba2019-03-25 12:57:58 -0500609 SkString md5;
Mike Kleined2cec42019-11-01 10:51:01 -0500610 if (!FLAGS_quick) {
611 HashAndEncode hashAndEncode{bitmap};
612 {
613 SkMD5 hash;
614 if (image) {
Mike Klein989f5bf2020-10-07 10:11:50 -0500615 hashAndEncode.feedHash(&hash);
Mike Kleined2cec42019-11-01 10:51:01 -0500616 } else {
617 hash.write(blob->data(), blob->size());
Mike Klein9b462092019-03-27 13:52:35 -0500618 }
Mike Kleined2cec42019-11-01 10:51:01 -0500619
620 SkMD5::Digest digest = hash.finish();
621 for (int i = 0; i < 16; i++) {
622 md5.appendf("%02x", digest.data[i]);
623 }
624 }
625
626 if (!FLAGS_writePath.isEmpty()) {
627 sk_mkdir(FLAGS_writePath[0]);
628 SkString path = SkStringPrintf("%s/%s%s",
629 FLAGS_writePath[0], source.name.c_str(), ext);
630
Mike Klein989f5bf2020-10-07 10:11:50 -0500631 SkFILEWStream file(path.c_str());
Mike Kleined2cec42019-11-01 10:51:01 -0500632 if (image) {
Mike Klein989f5bf2020-10-07 10:11:50 -0500633 if (!hashAndEncode.encodePNG(&file, md5.c_str(),
634 FLAGS_key, FLAGS_properties)) {
Mike Kleined2cec42019-11-01 10:51:01 -0500635 SK_ABORT("Could not write .png.");
636 }
637 } else {
Mike Kleined2cec42019-11-01 10:51:01 -0500638 file.write(blob->data(), blob->size());
639 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500640 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500641 }
642
Mike Klein832d3c52019-04-02 15:23:46 -0400643 const auto elapsed = std::chrono::steady_clock::now() - start;
Mike Klein4636ac62021-03-01 11:09:22 -0600644 fprintf(stdout, "%50s %s %7dms\n",
645 source.name.c_str(),
Mike Klein832d3c52019-04-02 15:23:46 -0400646 md5.c_str(),
647 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Mike Klein4636ac62021-03-01 11:09:22 -0600648 fflush(stdout);
Mike Klein735c7ba2019-03-25 12:57:58 -0500649 }
650
Brian Osman5aa11fb2019-04-08 16:40:36 -0400651 if (!FLAGS_writeShaders.isEmpty()) {
652 sk_mkdir(FLAGS_writeShaders[0]);
653 GrBackendApi api =
654 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
655 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
656
657 }
658
Mike Klein735c7ba2019-03-25 12:57:58 -0500659 return 0;
660}