blob: 3f77b0962cd5e64653a066cb87302e3b870c8667 [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"
Mike Kleinbc4a36a2021-03-01 10:57:01 -060019#include "src/core/SkTaskGroup.h"
Adlai Hollera0693042020-10-14 11:23:11 -040020#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/utils/SkOSPath.h"
Mike Kleinfee00792019-11-21 16:18:47 -060023#include "tests/Test.h"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040024#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/CrashHandler.h"
26#include "tools/HashAndEncode.h"
27#include "tools/ToolUtils.h"
28#include "tools/flags/CommandLineFlags.h"
29#include "tools/flags/CommonFlags.h"
Brian Salomon72c7b982020-10-06 10:07:38 -040030#include "tools/gpu/BackendSurfaceFactory.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "tools/gpu/GrContextFactory.h"
32#include "tools/gpu/MemoryCache.h"
33#include "tools/trace/EventTracingPriv.h"
Brian Salomon72c7b982020-10-06 10:07:38 -040034
Mike Klein735c7ba2019-03-25 12:57:58 -050035#include <chrono>
36#include <functional>
Mike Kleina9e62e82021-03-02 10:44:32 -060037#include <limits.h>
Mike Klein735c7ba2019-03-25 12:57:58 -050038#include <stdio.h>
39#include <stdlib.h>
40
Brian Osmanca946562019-04-10 13:51:53 -040041#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042 #include "modules/skottie/include/Skottie.h"
Brian Osman849f4d62019-11-26 08:58:26 -050043 #include "modules/skresources/include/SkResources.h"
Brian Osmanca946562019-04-10 13:51:53 -040044#endif
45
Mike Klein735c7ba2019-03-25 12:57:58 -050046using sk_gpu_test::GrContextFactory;
47
Mike Kleinfee00792019-11-21 16:18:47 -060048static DEFINE_bool(listGMs , false, "Print GM names and exit.");
49static DEFINE_bool(listTests, false, "Print unit test names and exit.");
50
Mike Klein735c7ba2019-03-25 12:57:58 -050051static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw.");
52static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into.");
53
Mike Klein4daf6372019-04-03 11:19:45 -040054static DEFINE_string(ct , "8888", "The color type for any raster backend.");
55static DEFINE_string(at , "premul", "The alpha type for any raster backend.");
56static DEFINE_string(gamut , "srgb", "The color gamut for any raster backend.");
57static DEFINE_string(tf , "srgb", "The transfer function for any raster backend.");
58static DEFINE_bool (legacy, false, "Use a null SkColorSpace instead of --gamut and --tf?");
Mike Klein735c7ba2019-03-25 12:57:58 -050059
Brian Salomon5276ba22021-04-30 10:58:34 -040060static DEFINE_bool (skvm , false, "Use SkVMBlitter when supported?");
61static DEFINE_bool (jit , true, "JIT SkVM?");
62static DEFINE_bool (dylib, false, "JIT SkVM via dylib?");
63
64static DEFINE_bool (reducedshaders, false, "Use reduced shader set for any GPU backend.");
65static DEFINE_int (samples , 0, "Samples per pixel in GPU backends.");
66static DEFINE_bool (stencils , true, "If false, avoid stencil buffers in GPU backends.");
67static DEFINE_bool (dit , false, "Use device-independent text in GPU backends.");
68static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces.");
Mike Klein735c7ba2019-03-25 12:57:58 -050069
70static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing.");
71static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing.");
72static DEFINE_bool(releaseAndAbandonGpuContext, false,
73 "Release all GPU resources and abandon the GrContext after drawing.");
74
75static DEFINE_bool(decodeToDst, false,
76 "Decode images to destination format rather than suggested natural format.");
77
Mike Kleinc245bd92019-03-27 14:31:09 -050078static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI,
79 "DPI for rasterized content in vector backends like --backend pdf.");
80static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?");
81
Mike Kleina9e62e82021-03-02 10:44:32 -060082static DEFINE_int(clipW, INT_MAX, "Limit source width.");
83static DEFINE_int(clipH, INT_MAX, "Limit source height.");
84
Mike Klein735c7ba2019-03-25 12:57:58 -050085static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?");
86static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set.");
Mike Kleined2cec42019-11-01 10:51:01 -050087static DEFINE_bool (quick, false, "Skip image hashing and encoding?");
Mike Kleinbc4a36a2021-03-01 10:57:01 -060088static DEFINE_int (race, 0, "If >0, use threads to induce race conditions?");
Mike Klein735c7ba2019-03-25 12:57:58 -050089
Brian Osman5aa11fb2019-04-08 16:40:36 -040090static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set.");
91
Mike Klein735c7ba2019-03-25 12:57:58 -050092static DEFINE_string(key, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein7b8bc532019-04-09 11:19:47 -050093static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein735c7ba2019-03-25 12:57:58 -050094
95template <typename T>
96struct FlagOption {
97 const char* label;
98 T value;
99};
100
101template <typename T, int N>
102static bool parse_flag(const CommandLineFlags::StringArray& flag,
103 const char* flag_name,
104 const FlagOption<T> (&array)[N],
105 T* value) {
106 for (auto entry : array) {
107 if (flag.contains(entry.label)) {
108 *value = entry.value;
109 return true;
110 }
111 }
112 fprintf(stderr, "Known values for --%s:\n", flag_name);
113 for (auto entry : array) {
114 fprintf(stderr, " --%s %s\n", flag_name, entry.label);
115 }
116 return false;
117}
118
Mike Kleina833cff2019-04-09 11:52:40 -0500119struct Result {
120 enum { Ok, Skip, Fail} status;
121 SkString failure;
122};
123static const Result ok = {Result::Ok, {}},
124 skip = {Result::Skip, {}};
125
Adlai Holler684838f2020-05-12 10:41:04 -0400126static Result fail(const char* why) {
127 return { Result::Fail, SkString(why) };
128}
Mike Kleina833cff2019-04-09 11:52:40 -0500129template <typename... Args>
Adlai Holler684838f2020-05-12 10:41:04 -0400130static Result fail(const char* whyFmt, Args... args) {
131 return { Result::Fail, SkStringPrintf(whyFmt, args...) };
Mike Kleina833cff2019-04-09 11:52:40 -0500132}
133
134
Mike Klein735c7ba2019-03-25 12:57:58 -0500135struct Source {
136 SkString name;
137 SkISize size;
Mike Kleina833cff2019-04-09 11:52:40 -0500138 std::function<Result(SkCanvas*)> draw;
139 std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
Mike Klein735c7ba2019-03-25 12:57:58 -0500140};
141
Mike Kleina833cff2019-04-09 11:52:40 -0500142static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
143 source->size = gm->getISize();
144 source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
145 source->draw = [gm](SkCanvas* canvas) {
Adlai Hollere3ad5272020-07-07 10:27:55 -0400146 auto direct = GrAsDirectContext(canvas->recordingContext());
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400147
Mike Kleina833cff2019-04-09 11:52:40 -0500148 SkString err;
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400149 switch (gm->gpuSetup(direct, canvas, &err)) {
Robert Phillips900630c2020-06-24 10:35:00 -0400150 case skiagm::DrawResult::kOk : break;
151 case skiagm::DrawResult::kSkip: return skip;
152 case skiagm::DrawResult::kFail: return fail(err.c_str());
153 }
154
Mike Kleina833cff2019-04-09 11:52:40 -0500155 switch (gm->draw(canvas, &err)) {
156 case skiagm::DrawResult::kOk: break;
157 case skiagm::DrawResult::kSkip: return skip;
158 case skiagm::DrawResult::kFail: return fail(err.c_str());
159 }
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, sk_sp<SkPicture> pic) {
165 source->size = pic->cullRect().roundOut().size();
166 source->draw = [pic](SkCanvas* canvas) {
167 canvas->drawPicture(pic);
168 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500169 };
170}
171
Mike Kleina833cff2019-04-09 11:52:40 -0500172static void init(Source* source, std::shared_ptr<SkCodec> codec) {
173 source->size = codec->dimensions();
174 source->draw = [codec](SkCanvas* canvas) {
175 SkImageInfo info = codec->getInfo();
176 if (FLAGS_decodeToDst) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400177 info = canvas->imageInfo().makeDimensions(info.dimensions());
Mike Kleina833cff2019-04-09 11:52:40 -0500178 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500179
Mike Reed844beb52021-01-25 15:36:09 -0500180 auto [image, result] = codec->getImage(info);
181 if (image) {
182 canvas->drawImage(image, 0,0);
183 return ok;
Mike Kleina833cff2019-04-09 11:52:40 -0500184 }
Mike Reed844beb52021-01-25 15:36:09 -0500185 return fail("codec->getPixels() failed: %d\n", result);
Mike Klein735c7ba2019-03-25 12:57:58 -0500186 };
187}
188
Mike Kleina833cff2019-04-09 11:52:40 -0500189static void init(Source* source, sk_sp<SkSVGDOM> svg) {
Mike Kleind1131b62021-02-02 17:48:29 -0600190 if (svg->containerSize().isEmpty()) {
191 svg->setContainerSize({1000,1000});
192 }
193 source->size = svg->containerSize().toCeil();
Mike Kleina833cff2019-04-09 11:52:40 -0500194 source->draw = [svg](SkCanvas* canvas) {
195 svg->render(canvas);
196 return ok;
Mike Klein22924262019-04-02 14:14:56 -0400197 };
198}
199
Brian Osmanca946562019-04-10 13:51:53 -0400200#if defined(SK_ENABLE_SKOTTIE)
Mike Kleina833cff2019-04-09 11:52:40 -0500201static void init(Source* source, sk_sp<skottie::Animation> animation) {
202 source->size = {1000,1000};
203 source->draw = [animation](SkCanvas* canvas) {
204 canvas->clear(SK_ColorWHITE);
Mike Klein22924262019-04-02 14:14:56 -0400205
Mike Kleina833cff2019-04-09 11:52:40 -0500206 // Draw frames in a shuffled order to exercise nonlinear frame progression.
207 // The film strip will still be in time order, just drawn out of order.
208 const int order[] = { 4, 0, 3, 1, 2 };
209 const int tiles = SK_ARRAY_COUNT(order);
210 const float dim = 1000.0f / tiles;
Mike Klein22924262019-04-02 14:14:56 -0400211
Mike Kleina833cff2019-04-09 11:52:40 -0500212 const float dt = 1.0f / (tiles*tiles - 1);
Mike Klein22924262019-04-02 14:14:56 -0400213
Mike Kleina833cff2019-04-09 11:52:40 -0500214 for (int y : order)
215 for (int x : order) {
216 SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
Mike Klein22924262019-04-02 14:14:56 -0400217
John Stiles8cc118d2020-07-30 11:22:35 -0400218 SkAutoCanvasRestore _(canvas, /*doSave=*/true);
219 canvas->clipRect(dst, /*doAntiAlias=*/true);
Mike Reed2ac6ce82021-01-15 12:26:22 -0500220 canvas->concat(SkMatrix::RectToRect(SkRect::MakeSize(animation->size()), dst,
221 SkMatrix::kCenter_ScaleToFit));
Mike Kleina833cff2019-04-09 11:52:40 -0500222 float t = (y*tiles + x) * dt;
223 animation->seek(t);
224 animation->render(canvas);
225 }
226 return ok;
Mike Klein6253d902019-03-27 15:09:12 -0500227 };
228}
Brian Osmanca946562019-04-10 13:51:53 -0400229#endif
Mike Klein6253d902019-03-27 15:09:12 -0500230
Mike Kleinfee00792019-11-21 16:18:47 -0600231static void init(Source* source, const skiatest::Test& test) {
232 source->size = {1,1};
233 source->draw = [test](SkCanvas* canvas) {
234 struct Reporter : public skiatest::Reporter {
235 SkString msg;
236
237 void reportFailed(const skiatest::Failure& failure) override {
Mike Klein484b63b2020-04-15 12:43:12 -0500238 msg += failure.toString();
239 msg += "\n";
Mike Kleinfee00792019-11-21 16:18:47 -0600240 }
241 } reporter;
242
243 test.run(&reporter, GrContextOptions{});
244
245 if (reporter.msg.isEmpty()) {
246 canvas->clear(SK_ColorGREEN);
247 return ok;
248 }
249
250 canvas->clear(SK_ColorRED);
251 return fail(reporter.msg.c_str());
252 };
253}
254
Mike Kleina5c27172019-04-02 10:26:48 -0400255static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500256 SkImageInfo info) {
257 if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400258 if (draw(surface->getCanvas())) {
259 return surface->makeImageSnapshot();
260 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500261 }
262 return nullptr;
263}
264
Mike Kleina5c27172019-04-02 10:26:48 -0400265static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
Mike Klein9b462092019-03-27 13:52:35 -0500266 SkImageInfo info) {
267 SkPictureRecorder recorder;
Mike Kleina5c27172019-04-02 10:26:48 -0400268 if (draw(recorder.beginRecording(info.width(), info.height()))) {
269 return recorder.finishRecordingAsPicture()->serialize();
270 }
271 return nullptr;
Mike Klein9b462092019-03-27 13:52:35 -0500272}
273
Mike Kleina5c27172019-04-02 10:26:48 -0400274static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
Mike Kleinc245bd92019-03-27 14:31:09 -0500275 SkImageInfo info,
276 SkString name) {
277 SkPDF::Metadata metadata;
278 metadata.fTitle = name;
279 metadata.fCreator = "Skia/FM";
280 metadata.fRasterDPI = FLAGS_rasterDPI;
281 metadata.fPDFA = FLAGS_PDFA;
282
283 SkDynamicMemoryWStream stream;
284 if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400285 if (draw(doc->beginPage(info.width(), info.height()))) {
286 doc->endPage();
287 doc->close();
288 return stream.detachAsData();
289 }
Mike Kleinc245bd92019-03-27 14:31:09 -0500290 }
291 return nullptr;
292}
293
Mike Kleina5c27172019-04-02 10:26:48 -0400294static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500295 SkImageInfo info,
296 GrContextFactory::ContextType api,
297 GrContextFactory* factory) {
298 enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
299 const FlagOption<SurfaceType> kSurfaceTypes[] = {
300 { "default", SurfaceType::kDefault },
301 { "betex" , SurfaceType::kBackendTexture },
302 { "bert" , SurfaceType::kBackendRenderTarget },
303 };
304 SurfaceType surfaceType;
305 if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
306 return nullptr;
307 }
308
Chris Daltonb3c97452019-06-25 20:07:56 -0600309 auto overrides = GrContextFactory::ContextOverrides::kNone;
Mike Klein735c7ba2019-03-25 12:57:58 -0500310 if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
311
Robert Phillips7b4e43c2020-07-01 16:59:17 -0400312 auto context = factory->getContextInfo(api, overrides).directContext();
Mike Klein735c7ba2019-03-25 12:57:58 -0500313
314 uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
315 : 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -0400316 SkSurfaceProps props(flags, kRGB_H_SkPixelGeometry);
Mike Klein735c7ba2019-03-25 12:57:58 -0500317
318 sk_sp<SkSurface> surface;
Mike Klein735c7ba2019-03-25 12:57:58 -0500319
320 switch (surfaceType) {
321 case SurfaceType::kDefault:
322 surface = SkSurface::MakeRenderTarget(context,
323 SkBudgeted::kNo,
324 info,
325 FLAGS_samples,
326 &props);
327 break;
328
329 case SurfaceType::kBackendTexture:
Brian Salomonf9b00422020-10-08 16:00:14 -0400330 surface = sk_gpu_test::MakeBackendTextureSurface(context,
Brian Salomon72050802020-10-12 20:45:06 -0400331 info,
Brian Salomonf9b00422020-10-08 16:00:14 -0400332 kTopLeft_GrSurfaceOrigin,
333 FLAGS_samples,
Brian Salomonf9b00422020-10-08 16:00:14 -0400334 GrMipmapped::kNo,
335 GrProtected::kNo,
336 &props);
Mike Klein735c7ba2019-03-25 12:57:58 -0500337 break;
338
339 case SurfaceType::kBackendRenderTarget:
Brian Salomonf9b00422020-10-08 16:00:14 -0400340 surface = sk_gpu_test::MakeBackendRenderTargetSurface(context,
Brian Salomon72050802020-10-12 20:45:06 -0400341 info,
Brian Salomonf9b00422020-10-08 16:00:14 -0400342 kBottomLeft_GrSurfaceOrigin,
343 FLAGS_samples,
Brian Salomonf9b00422020-10-08 16:00:14 -0400344 GrProtected::kNo,
345 &props);
Mike Klein735c7ba2019-03-25 12:57:58 -0500346 break;
347 }
348
349 if (!surface) {
350 fprintf(stderr, "Could not create GPU surface.\n");
351 return nullptr;
352 }
353
354 if (FLAGS_preAbandonGpuContext) {
355 factory->abandonContexts();
356 }
357
Mike Kleina5c27172019-04-02 10:26:48 -0400358 sk_sp<SkImage> image;
359 if (draw(surface->getCanvas())) {
360 image = surface->makeImageSnapshot();
361 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500362
363 if (FLAGS_abandonGpuContext) {
364 factory->abandonContexts();
365 } else if (FLAGS_releaseAndAbandonGpuContext) {
366 factory->releaseResourcesAndAbandonContexts();
367 }
368
Mike Klein735c7ba2019-03-25 12:57:58 -0500369 return image;
370}
371
Mike Klein2a57e792020-01-14 12:47:52 -0600372extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500373extern bool gSkVMAllowJIT;
Mike Klein31bfa832020-01-22 11:36:40 -0600374extern bool gSkVMJITViaDylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600375
Mike Klein735c7ba2019-03-25 12:57:58 -0500376int main(int argc, char** argv) {
377 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500378 SetupCrashHandler();
Mike Kleinbc4a36a2021-03-01 10:57:01 -0600379 SkTaskGroup::Enabler enabled(FLAGS_race);
Mike Klein735c7ba2019-03-25 12:57:58 -0500380
381 if (FLAGS_cpuDetect) {
382 SkGraphics::Init();
383 }
Mike Klein31bfa832020-01-22 11:36:40 -0600384 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500385 gSkVMAllowJIT = FLAGS_jit;
Mike Klein31bfa832020-01-22 11:36:40 -0600386 gSkVMJITViaDylib = FLAGS_dylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600387
Mike Klein735c7ba2019-03-25 12:57:58 -0500388 initializeEventTracingForTools();
389 ToolUtils::SetDefaultFontMgr();
390 SetAnalyticAAFromCommonFlags();
391
392 GrContextOptions baseOptions;
393 SetCtxOptionsFromCommonFlags(&baseOptions);
Brian Salomon5276ba22021-04-30 10:58:34 -0400394 baseOptions.fReducedShaderVariations = FLAGS_reducedshaders;
Mike Klein735c7ba2019-03-25 12:57:58 -0500395
Brian Osman5aa11fb2019-04-08 16:40:36 -0400396 sk_gpu_test::MemoryCache memoryCache;
397 if (!FLAGS_writeShaders.isEmpty()) {
398 baseOptions.fPersistentCache = &memoryCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400399 baseOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5aa11fb2019-04-08 16:40:36 -0400400 }
401
Mike Kleincbe93ee2019-04-02 17:00:54 -0400402 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500403 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Hal Canaryedda5652019-08-05 10:28:09 -0400404 std::unique_ptr<skiagm::GM> gm{factory()};
Mike Kleinfee00792019-11-21 16:18:47 -0600405 if (FLAGS_listGMs) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500406 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400407 } else {
408 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500409 }
410 }
Mike Kleinfee00792019-11-21 16:18:47 -0600411
412 SkTHashMap<SkString, const skiatest::Test*> tests;
413 for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
414 if (test.needsGpu) {
415 continue; // TODO
416 }
417 if (FLAGS_listTests) {
418 fprintf(stdout, "%s\n", test.name);
419 } else {
420 tests.set(SkString{test.name}, &test);
421 }
422 }
423
424 if (FLAGS_listGMs || FLAGS_listTests) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400425 return 0;
426 }
Mike Kleinfee00792019-11-21 16:18:47 -0600427 if (FLAGS_sources.isEmpty()) {
428 fprintf(stderr, "Please give me something to run using -s/--sources!\n");
429 return 1;
430 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400431
Mike Kleinbc4a36a2021-03-01 10:57:01 -0600432 const int replicas = std::max(1, FLAGS_race);
433
Mike Kleincbe93ee2019-04-02 17:00:54 -0400434 SkTArray<Source> sources;
Mike Kleinbc4a36a2021-03-01 10:57:01 -0600435 for (const SkString& name : FLAGS_sources)
436 for (int replica = 0; replica < replicas; replica++) {
Mike Kleina833cff2019-04-09 11:52:40 -0500437 Source* source = &sources.push_back();
Mike Kleinfa962f52021-02-03 17:21:34 -0600438 source->name = name;
Mike Kleina833cff2019-04-09 11:52:40 -0500439
440 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Hal Canaryedda5652019-08-05 10:28:09 -0400441 std::shared_ptr<skiagm::GM> gm{(*factory)()};
Ben Wagner406ff502019-08-12 16:39:24 -0400442 init(source, std::move(gm));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400443 continue;
444 }
445
Mike Kleinfee00792019-11-21 16:18:47 -0600446 if (const skiatest::Test** test = tests.find(name)) {
Mike Kleinfee00792019-11-21 16:18:47 -0600447 init(source, **test);
448 continue;
449 }
450
Mike Kleina833cff2019-04-09 11:52:40 -0500451 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
Mike Kleince90d6f2019-03-29 11:14:14 -0500452 if (name.endsWith(".skp")) {
453 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500454 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400455 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500456 }
457 } else if (name.endsWith(".svg")) {
458 SkMemoryStream stream{blob};
459 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500460 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400461 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500462 }
Brian Osmanca946562019-04-10 13:51:53 -0400463 }
464#if defined(SK_ENABLE_SKOTTIE)
465 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500466 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400467 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
Brian Osman849f4d62019-11-26 08:58:26 -0500468 .setResourceProvider(skresources::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400469 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500470 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400471 continue;
Mike Klein22924262019-04-02 14:14:56 -0400472 }
Brian Osmanca946562019-04-10 13:51:53 -0400473 }
474#endif
475 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500476 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400477 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500478 }
479 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400480
Mike Kleina833cff2019-04-09 11:52:40 -0500481 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400482 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500483 }
484
Mike Klein9b462092019-03-27 13:52:35 -0500485 enum NonGpuBackends {
486 kCPU_Backend = -1,
487 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500488 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500489 };
490 const FlagOption<int> kBackends[] = {
491 { "cpu" , kCPU_Backend },
492 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500493 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500494 { "gl" , GrContextFactory::kGL_ContextType },
495 { "gles" , GrContextFactory::kGLES_ContextType },
496 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
497 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
498 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
499 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
500 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
Chris Daltone0f4de62021-06-28 08:43:08 -0600501 { "cmdbuffer_es2" , GrContextFactory::kCommandBuffer_ES2_ContextType },
502 { "cmdbuffer_es3" , GrContextFactory::kCommandBuffer_ES3_ContextType },
Mike Klein9b462092019-03-27 13:52:35 -0500503 { "vk" , GrContextFactory::kVulkan_ContextType },
504 { "mtl" , GrContextFactory::kMetal_ContextType },
505 { "mock" , GrContextFactory::kMock_ContextType },
506 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500507 const FlagOption<SkColorType> kColorTypes[] = {
Mike Kleind58e01b62020-07-17 13:52:41 -0500508 { "a8", kAlpha_8_SkColorType },
509 { "g8", kGray_8_SkColorType },
510 { "565", kRGB_565_SkColorType },
511 { "4444", kARGB_4444_SkColorType },
512 { "8888", kN32_SkColorType },
513 { "888x", kRGB_888x_SkColorType },
514 { "1010102", kRGBA_1010102_SkColorType },
515 { "101010x", kRGB_101010x_SkColorType },
516 { "bgra1010102", kBGRA_1010102_SkColorType },
517 { "bgr101010x", kBGR_101010x_SkColorType },
518 { "f16norm", kRGBA_F16Norm_SkColorType },
519 { "f16", kRGBA_F16_SkColorType },
520 { "f32", kRGBA_F32_SkColorType },
521 { "rgba", kRGBA_8888_SkColorType },
522 { "bgra", kBGRA_8888_SkColorType },
523 { "16161616", kR16G16B16A16_unorm_SkColorType },
Mike Klein735c7ba2019-03-25 12:57:58 -0500524 };
525 const FlagOption<SkAlphaType> kAlphaTypes[] = {
526 { "premul", kPremul_SkAlphaType },
527 { "unpremul", kUnpremul_SkAlphaType },
528 };
529 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
530 { "srgb", SkNamedGamut::kSRGB },
Mike Kleinb147ace2020-01-16 11:11:06 -0600531 { "p3", SkNamedGamut::kDisplayP3 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500532 { "rec2020", SkNamedGamut::kRec2020 },
533 { "adobe", SkNamedGamut::kAdobeRGB },
534 { "narrow", gNarrow_toXYZD50},
535 };
536 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
537 { "srgb" , SkNamedTransferFn::kSRGB },
Hal Canarybe67a172019-05-24 10:13:36 -0400538 { "rec2020", SkNamedTransferFn::kRec2020 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500539 { "2.2" , SkNamedTransferFn::k2Dot2 },
540 { "linear" , SkNamedTransferFn::kLinear },
541 };
542
Mike Klein735c7ba2019-03-25 12:57:58 -0500543
Mike Klein9b462092019-03-27 13:52:35 -0500544 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500545 SkColorType ct;
546 SkAlphaType at;
547 skcms_Matrix3x3 gamut;
548 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500549
Mike Klein9b462092019-03-27 13:52:35 -0500550 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
551 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500552 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
553 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500554 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500555 return 1;
556 }
557
Mike Klein4daf6372019-04-03 11:19:45 -0400558 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
559 : SkColorSpace::MakeRGB(tf,gamut);
Mike Klein6706c9f2021-02-26 12:22:07 -0600560 const SkColorInfo color_info{ct,at,cs};
Mike Klein735c7ba2019-03-25 12:57:58 -0500561
Mike Kleinbc4a36a2021-03-01 10:57:01 -0600562 for (int i = 0; i < sources.count(); i += replicas)
563 SkTaskGroup{}.batch(replicas, [=](int replica) {
564 Source source = sources[i+replica];
565
Mike Kleindb8340f2021-03-01 10:44:32 -0600566 AutoreleasePool pool;
Mike Klein735c7ba2019-03-25 12:57:58 -0500567 const auto start = std::chrono::steady_clock::now();
Mike Klein735c7ba2019-03-25 12:57:58 -0500568
Mike Kleina9e62e82021-03-02 10:44:32 -0600569 auto [w,h] = source.size;
570 w = std::min(w, FLAGS_clipW);
571 h = std::min(h, FLAGS_clipH);
572 const SkImageInfo info = SkImageInfo::Make({w,h}, color_info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500573
Mike Kleina833cff2019-04-09 11:52:40 -0500574 auto draw = [&source](SkCanvas* canvas) {
575 Result result = source.draw(canvas);
576 switch (result.status) {
577 case Result::Ok: break;
578 case Result::Skip: return false;
579 case Result::Fail:
John Stiles616da102020-06-12 14:07:41 -0400580 SK_ABORT("%s", result.failure.c_str());
Mike Kleina833cff2019-04-09 11:52:40 -0500581 }
582 return true;
583 };
584
Mike Klein735c7ba2019-03-25 12:57:58 -0500585 GrContextOptions options = baseOptions;
586 source.tweak(&options);
587 GrContextFactory factory(options); // N.B. factory must outlive image
588
589 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500590 sk_sp<SkData> blob;
591 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500592 switch (backend) {
593 case kCPU_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500594 image = draw_with_cpu(draw, info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500595 break;
Mike Klein9b462092019-03-27 13:52:35 -0500596 case kSKP_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500597 blob = draw_as_skp(draw, info);
Mike Klein9b462092019-03-27 13:52:35 -0500598 ext = ".skp";
599 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500600 case kPDF_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500601 blob = draw_as_pdf(draw, info, source.name);
Mike Kleinc245bd92019-03-27 14:31:09 -0500602 ext = ".pdf";
603 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500604 default:
Mike Kleina833cff2019-04-09 11:52:40 -0500605 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500606 break;
607 }
608
Mike Kleined2cec42019-11-01 10:51:01 -0500609 // We read back a bitmap even when --quick is set and we won't use it,
610 // to keep us honest about deferred work, flushing pipelines, etc.
Mike Klein735c7ba2019-03-25 12:57:58 -0500611 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500612 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500613 SK_ABORT("SkImage::asLegacyBitmap() failed.");
Mike Klein735c7ba2019-03-25 12:57:58 -0500614 }
615
Mike Kleinbc4a36a2021-03-01 10:57:01 -0600616 // Our --race replicas have done their job by now if they're going to catch anything.
617 if (replica != 0) {
618 return;
619 }
620
621 if (!image && !blob) {
622 fprintf(stdout, "%50s skipped\n", source.name.c_str());
623 fflush(stdout);
624 return;
625 }
626
Mike Klein735c7ba2019-03-25 12:57:58 -0500627 SkString md5;
Mike Kleined2cec42019-11-01 10:51:01 -0500628 if (!FLAGS_quick) {
629 HashAndEncode hashAndEncode{bitmap};
630 {
631 SkMD5 hash;
632 if (image) {
Mike Klein989f5bf2020-10-07 10:11:50 -0500633 hashAndEncode.feedHash(&hash);
Mike Kleined2cec42019-11-01 10:51:01 -0500634 } else {
635 hash.write(blob->data(), blob->size());
Mike Klein9b462092019-03-27 13:52:35 -0500636 }
Mike Kleined2cec42019-11-01 10:51:01 -0500637
638 SkMD5::Digest digest = hash.finish();
639 for (int i = 0; i < 16; i++) {
640 md5.appendf("%02x", digest.data[i]);
641 }
642 }
643
644 if (!FLAGS_writePath.isEmpty()) {
Mike Kleined2cec42019-11-01 10:51:01 -0500645 SkString path = SkStringPrintf("%s/%s%s",
646 FLAGS_writePath[0], source.name.c_str(), ext);
Mike Klein3144a1e2021-04-02 14:05:59 -0500647 for (char* it = path.writable_str(); *it != '\0'; it++) {
648 if (*it == '/' || *it == '\\') {
649 char prev = std::exchange(*it, '\0');
650 sk_mkdir(path.c_str());
651 *it = prev;
652 }
653 }
Mike Kleined2cec42019-11-01 10:51:01 -0500654
Mike Klein989f5bf2020-10-07 10:11:50 -0500655 SkFILEWStream file(path.c_str());
Mike Kleined2cec42019-11-01 10:51:01 -0500656 if (image) {
Mike Klein989f5bf2020-10-07 10:11:50 -0500657 if (!hashAndEncode.encodePNG(&file, md5.c_str(),
658 FLAGS_key, FLAGS_properties)) {
Mike Kleined2cec42019-11-01 10:51:01 -0500659 SK_ABORT("Could not write .png.");
660 }
661 } else {
Mike Kleined2cec42019-11-01 10:51:01 -0500662 file.write(blob->data(), blob->size());
663 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500664 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500665 }
666
Mike Klein832d3c52019-04-02 15:23:46 -0400667 const auto elapsed = std::chrono::steady_clock::now() - start;
Mike Klein4636ac62021-03-01 11:09:22 -0600668 fprintf(stdout, "%50s %s %7dms\n",
669 source.name.c_str(),
Mike Klein832d3c52019-04-02 15:23:46 -0400670 md5.c_str(),
671 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Mike Klein4636ac62021-03-01 11:09:22 -0600672 fflush(stdout);
Mike Kleinbc4a36a2021-03-01 10:57:01 -0600673 });
674
Mike Klein735c7ba2019-03-25 12:57:58 -0500675
Brian Osman5aa11fb2019-04-08 16:40:36 -0400676 if (!FLAGS_writeShaders.isEmpty()) {
677 sk_mkdir(FLAGS_writeShaders[0]);
678 GrBackendApi api =
679 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
680 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
681
682 }
683
Mike Klein735c7ba2019-03-25 12:57:58 -0500684 return 0;
685}