blob: a14425c07beef7906005853aeb9979bfe7639144 [file] [log] [blame]
Mike Klein735c7ba2019-03-25 12:57:58 -05001// Copyright 2019 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004#include "experimental/svg/model/SkSVGDOM.h"
5#include "gm/gm.h"
6#include "include/codec/SkCodec.h"
Robert Phillipse19babf2020-04-06 13:57:30 -04007#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkColorSpace.h"
9#include "include/core/SkGraphics.h"
10#include "include/core/SkPicture.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/docs/SkPDFDocument.h"
13#include "include/gpu/GrContextOptions.h"
14#include "include/private/SkTHash.h"
15#include "src/core/SkColorSpacePriv.h"
16#include "src/core/SkMD5.h"
17#include "src/core/SkOSFile.h"
18#include "src/gpu/GrContextPriv.h"
19#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/utils/SkOSPath.h"
Mike Kleinfee00792019-11-21 16:18:47 -060021#include "tests/Test.h"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040022#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/CrashHandler.h"
24#include "tools/HashAndEncode.h"
25#include "tools/ToolUtils.h"
26#include "tools/flags/CommandLineFlags.h"
27#include "tools/flags/CommonFlags.h"
28#include "tools/gpu/GrContextFactory.h"
29#include "tools/gpu/MemoryCache.h"
30#include "tools/trace/EventTracingPriv.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050031#include <chrono>
32#include <functional>
33#include <stdio.h>
34#include <stdlib.h>
35
Brian Osmanca946562019-04-10 13:51:53 -040036#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037 #include "modules/skottie/include/Skottie.h"
Brian Osman849f4d62019-11-26 08:58:26 -050038 #include "modules/skresources/include/SkResources.h"
Brian Osmanca946562019-04-10 13:51:53 -040039#endif
40
Mike Klein735c7ba2019-03-25 12:57:58 -050041using sk_gpu_test::GrContextFactory;
42
Mike Kleinfee00792019-11-21 16:18:47 -060043static DEFINE_bool(listGMs , false, "Print GM names and exit.");
44static DEFINE_bool(listTests, false, "Print unit test names and exit.");
45
Mike Klein735c7ba2019-03-25 12:57:58 -050046static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw.");
47static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into.");
48
Mike Klein4daf6372019-04-03 11:19:45 -040049static DEFINE_string(ct , "8888", "The color type for any raster backend.");
50static DEFINE_string(at , "premul", "The alpha type for any raster backend.");
51static DEFINE_string(gamut , "srgb", "The color gamut for any raster backend.");
52static DEFINE_string(tf , "srgb", "The transfer function for any raster backend.");
53static DEFINE_bool (legacy, false, "Use a null SkColorSpace instead of --gamut and --tf?");
Mike Klein2a57e792020-01-14 12:47:52 -060054static DEFINE_bool (skvm , false, "Use SkVMBlitter when supported?");
Mike Klein31bfa832020-01-22 11:36:40 -060055static DEFINE_bool (dylib , false, "Use SkVM via dylib?");
Mike Klein735c7ba2019-03-25 12:57:58 -050056
57static DEFINE_int (samples , 0, "Samples per pixel in GPU backends.");
Mike Klein735c7ba2019-03-25 12:57:58 -050058static DEFINE_bool (stencils, true, "If false, avoid stencil buffers in GPU backends.");
59static DEFINE_bool (dit , false, "Use device-independent text in GPU backends.");
60static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces.");
61
62static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing.");
63static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing.");
64static DEFINE_bool(releaseAndAbandonGpuContext, false,
65 "Release all GPU resources and abandon the GrContext after drawing.");
66
67static DEFINE_bool(decodeToDst, false,
68 "Decode images to destination format rather than suggested natural format.");
69
Mike Kleinc245bd92019-03-27 14:31:09 -050070static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI,
71 "DPI for rasterized content in vector backends like --backend pdf.");
72static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?");
73
Mike Klein735c7ba2019-03-25 12:57:58 -050074static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?");
75static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set.");
Mike Kleined2cec42019-11-01 10:51:01 -050076static DEFINE_bool (quick, false, "Skip image hashing and encoding?");
Mike Klein735c7ba2019-03-25 12:57:58 -050077
Brian Osman5aa11fb2019-04-08 16:40:36 -040078static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set.");
79
Mike Klein735c7ba2019-03-25 12:57:58 -050080static DEFINE_string(key, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein7b8bc532019-04-09 11:19:47 -050081static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein735c7ba2019-03-25 12:57:58 -050082
83template <typename T>
84struct FlagOption {
85 const char* label;
86 T value;
87};
88
89template <typename T, int N>
90static bool parse_flag(const CommandLineFlags::StringArray& flag,
91 const char* flag_name,
92 const FlagOption<T> (&array)[N],
93 T* value) {
94 for (auto entry : array) {
95 if (flag.contains(entry.label)) {
96 *value = entry.value;
97 return true;
98 }
99 }
100 fprintf(stderr, "Known values for --%s:\n", flag_name);
101 for (auto entry : array) {
102 fprintf(stderr, " --%s %s\n", flag_name, entry.label);
103 }
104 return false;
105}
106
Mike Kleina833cff2019-04-09 11:52:40 -0500107struct Result {
108 enum { Ok, Skip, Fail} status;
109 SkString failure;
110};
111static const Result ok = {Result::Ok, {}},
112 skip = {Result::Skip, {}};
113
Adlai Holler684838f2020-05-12 10:41:04 -0400114static Result fail(const char* why) {
115 return { Result::Fail, SkString(why) };
116}
Mike Kleina833cff2019-04-09 11:52:40 -0500117template <typename... Args>
Adlai Holler684838f2020-05-12 10:41:04 -0400118static Result fail(const char* whyFmt, Args... args) {
119 return { Result::Fail, SkStringPrintf(whyFmt, args...) };
Mike Kleina833cff2019-04-09 11:52:40 -0500120}
121
122
Mike Klein735c7ba2019-03-25 12:57:58 -0500123struct Source {
124 SkString name;
125 SkISize size;
Mike Kleina833cff2019-04-09 11:52:40 -0500126 std::function<Result(SkCanvas*)> draw;
127 std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
Mike Klein735c7ba2019-03-25 12:57:58 -0500128};
129
Mike Kleina833cff2019-04-09 11:52:40 -0500130static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
131 source->size = gm->getISize();
132 source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
133 source->draw = [gm](SkCanvas* canvas) {
134 SkString err;
Robert Phillipsd26d25e2020-06-25 13:26:22 -0400135 switch (gm->gpuSetup(canvas->getGrContext(), canvas, &err)) {
Robert Phillips900630c2020-06-24 10:35:00 -0400136 case skiagm::DrawResult::kOk : break;
137 case skiagm::DrawResult::kSkip: return skip;
138 case skiagm::DrawResult::kFail: return fail(err.c_str());
139 }
140
Mike Kleina833cff2019-04-09 11:52:40 -0500141 switch (gm->draw(canvas, &err)) {
142 case skiagm::DrawResult::kOk: break;
143 case skiagm::DrawResult::kSkip: return skip;
144 case skiagm::DrawResult::kFail: return fail(err.c_str());
145 }
146 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500147 };
148}
149
Mike Kleina833cff2019-04-09 11:52:40 -0500150static void init(Source* source, sk_sp<SkPicture> pic) {
151 source->size = pic->cullRect().roundOut().size();
152 source->draw = [pic](SkCanvas* canvas) {
153 canvas->drawPicture(pic);
154 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500155 };
156}
157
Mike Kleina833cff2019-04-09 11:52:40 -0500158static void init(Source* source, std::shared_ptr<SkCodec> codec) {
159 source->size = codec->dimensions();
160 source->draw = [codec](SkCanvas* canvas) {
161 SkImageInfo info = codec->getInfo();
162 if (FLAGS_decodeToDst) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400163 info = canvas->imageInfo().makeDimensions(info.dimensions());
Mike Kleina833cff2019-04-09 11:52:40 -0500164 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500165
Mike Kleina833cff2019-04-09 11:52:40 -0500166 SkBitmap bm;
167 bm.allocPixels(info);
168 switch (SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) {
169 case SkCodec::kSuccess:
170 case SkCodec::kErrorInInput:
171 case SkCodec::kIncompleteInput: canvas->drawBitmap(bm, 0,0);
172 break;
173 default: return fail("codec->getPixels() failed: %d\n", result);
174 }
175 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500176 };
177}
178
Mike Kleina833cff2019-04-09 11:52:40 -0500179static void init(Source* source, sk_sp<SkSVGDOM> svg) {
180 source->size = svg->containerSize().isEmpty() ? SkISize{1000,1000}
181 : svg->containerSize().toCeil();
182 source->draw = [svg](SkCanvas* canvas) {
183 svg->render(canvas);
184 return ok;
Mike Klein22924262019-04-02 14:14:56 -0400185 };
186}
187
Brian Osmanca946562019-04-10 13:51:53 -0400188#if defined(SK_ENABLE_SKOTTIE)
Mike Kleina833cff2019-04-09 11:52:40 -0500189static void init(Source* source, sk_sp<skottie::Animation> animation) {
190 source->size = {1000,1000};
191 source->draw = [animation](SkCanvas* canvas) {
192 canvas->clear(SK_ColorWHITE);
Mike Klein22924262019-04-02 14:14:56 -0400193
Mike Kleina833cff2019-04-09 11:52:40 -0500194 // Draw frames in a shuffled order to exercise nonlinear frame progression.
195 // The film strip will still be in time order, just drawn out of order.
196 const int order[] = { 4, 0, 3, 1, 2 };
197 const int tiles = SK_ARRAY_COUNT(order);
198 const float dim = 1000.0f / tiles;
Mike Klein22924262019-04-02 14:14:56 -0400199
Mike Kleina833cff2019-04-09 11:52:40 -0500200 const float dt = 1.0f / (tiles*tiles - 1);
Mike Klein22924262019-04-02 14:14:56 -0400201
Mike Kleina833cff2019-04-09 11:52:40 -0500202 for (int y : order)
203 for (int x : order) {
204 SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
Mike Klein22924262019-04-02 14:14:56 -0400205
Mike Kleina833cff2019-04-09 11:52:40 -0500206 SkAutoCanvasRestore _(canvas, true/*save now*/);
207 canvas->clipRect(dst, /*aa=*/true);
208 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
209 dst,
210 SkMatrix::kCenter_ScaleToFit));
211 float t = (y*tiles + x) * dt;
212 animation->seek(t);
213 animation->render(canvas);
214 }
215 return ok;
Mike Klein6253d902019-03-27 15:09:12 -0500216 };
217}
Brian Osmanca946562019-04-10 13:51:53 -0400218#endif
Mike Klein6253d902019-03-27 15:09:12 -0500219
Mike Kleinfee00792019-11-21 16:18:47 -0600220static void init(Source* source, const skiatest::Test& test) {
221 source->size = {1,1};
222 source->draw = [test](SkCanvas* canvas) {
223 struct Reporter : public skiatest::Reporter {
224 SkString msg;
225
226 void reportFailed(const skiatest::Failure& failure) override {
Mike Klein484b63b2020-04-15 12:43:12 -0500227 msg += failure.toString();
228 msg += "\n";
Mike Kleinfee00792019-11-21 16:18:47 -0600229 }
230 } reporter;
231
232 test.run(&reporter, GrContextOptions{});
233
234 if (reporter.msg.isEmpty()) {
235 canvas->clear(SK_ColorGREEN);
236 return ok;
237 }
238
239 canvas->clear(SK_ColorRED);
240 return fail(reporter.msg.c_str());
241 };
242}
243
Mike Kleina5c27172019-04-02 10:26:48 -0400244static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500245 SkImageInfo info) {
246 if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400247 if (draw(surface->getCanvas())) {
248 return surface->makeImageSnapshot();
249 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500250 }
251 return nullptr;
252}
253
Mike Kleina5c27172019-04-02 10:26:48 -0400254static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
Mike Klein9b462092019-03-27 13:52:35 -0500255 SkImageInfo info) {
256 SkPictureRecorder recorder;
Mike Kleina5c27172019-04-02 10:26:48 -0400257 if (draw(recorder.beginRecording(info.width(), info.height()))) {
258 return recorder.finishRecordingAsPicture()->serialize();
259 }
260 return nullptr;
Mike Klein9b462092019-03-27 13:52:35 -0500261}
262
Mike Kleina5c27172019-04-02 10:26:48 -0400263static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
Mike Kleinc245bd92019-03-27 14:31:09 -0500264 SkImageInfo info,
265 SkString name) {
266 SkPDF::Metadata metadata;
267 metadata.fTitle = name;
268 metadata.fCreator = "Skia/FM";
269 metadata.fRasterDPI = FLAGS_rasterDPI;
270 metadata.fPDFA = FLAGS_PDFA;
271
272 SkDynamicMemoryWStream stream;
273 if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400274 if (draw(doc->beginPage(info.width(), info.height()))) {
275 doc->endPage();
276 doc->close();
277 return stream.detachAsData();
278 }
Mike Kleinc245bd92019-03-27 14:31:09 -0500279 }
280 return nullptr;
281}
282
Mike Kleina5c27172019-04-02 10:26:48 -0400283static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500284 SkImageInfo info,
285 GrContextFactory::ContextType api,
286 GrContextFactory* factory) {
287 enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
288 const FlagOption<SurfaceType> kSurfaceTypes[] = {
289 { "default", SurfaceType::kDefault },
290 { "betex" , SurfaceType::kBackendTexture },
291 { "bert" , SurfaceType::kBackendRenderTarget },
292 };
293 SurfaceType surfaceType;
294 if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
295 return nullptr;
296 }
297
Chris Daltonb3c97452019-06-25 20:07:56 -0600298 auto overrides = GrContextFactory::ContextOverrides::kNone;
Mike Klein735c7ba2019-03-25 12:57:58 -0500299 if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
300
301 GrContext* context = factory->getContextInfo(api, overrides)
302 .grContext();
303
304 uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
305 : 0;
306 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
307
308 sk_sp<SkSurface> surface;
309 GrBackendTexture backendTexture;
310 GrBackendRenderTarget backendRT;
311
312 switch (surfaceType) {
313 case SurfaceType::kDefault:
314 surface = SkSurface::MakeRenderTarget(context,
315 SkBudgeted::kNo,
316 info,
317 FLAGS_samples,
318 &props);
319 break;
320
321 case SurfaceType::kBackendTexture:
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400322 backendTexture = context->createBackendTexture(info.width(),
323 info.height(),
324 info.colorType(),
325 GrMipMapped::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400326 GrRenderable::kYes,
327 GrProtected::kNo);
Mike Klein735c7ba2019-03-25 12:57:58 -0500328 surface = SkSurface::MakeFromBackendTexture(context,
329 backendTexture,
330 kTopLeft_GrSurfaceOrigin,
331 FLAGS_samples,
332 info.colorType(),
333 info.refColorSpace(),
334 &props);
335 break;
336
337 case SurfaceType::kBackendRenderTarget:
338 backendRT = context->priv().getGpu()
339 ->createTestingOnlyBackendRenderTarget(info.width(),
340 info.height(),
341 SkColorTypeToGrColorType(info.colorType()));
342 surface = SkSurface::MakeFromBackendRenderTarget(context,
343 backendRT,
344 kBottomLeft_GrSurfaceOrigin,
345 info.colorType(),
346 info.refColorSpace(),
347 &props);
348 break;
349 }
350
351 if (!surface) {
352 fprintf(stderr, "Could not create GPU surface.\n");
353 return nullptr;
354 }
355
356 if (FLAGS_preAbandonGpuContext) {
357 factory->abandonContexts();
358 }
359
Mike Kleina5c27172019-04-02 10:26:48 -0400360 sk_sp<SkImage> image;
361 if (draw(surface->getCanvas())) {
362 image = surface->makeImageSnapshot();
363 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500364
365 if (FLAGS_abandonGpuContext) {
366 factory->abandonContexts();
367 } else if (FLAGS_releaseAndAbandonGpuContext) {
368 factory->releaseResourcesAndAbandonContexts();
369 }
370
371 if (!context->abandoned()) {
372 surface.reset();
373 if (backendTexture.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400374 context->deleteBackendTexture(backendTexture);
Mike Klein735c7ba2019-03-25 12:57:58 -0500375 }
376 if (backendRT.isValid()) {
377 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
378 }
379 }
380
381 return image;
382}
383
Mike Klein2a57e792020-01-14 12:47:52 -0600384extern bool gUseSkVMBlitter;
Mike Klein31bfa832020-01-22 11:36:40 -0600385extern bool gSkVMJITViaDylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600386
Mike Klein735c7ba2019-03-25 12:57:58 -0500387int main(int argc, char** argv) {
388 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500389 SetupCrashHandler();
Mike Klein735c7ba2019-03-25 12:57:58 -0500390
391 if (FLAGS_cpuDetect) {
392 SkGraphics::Init();
393 }
Mike Klein31bfa832020-01-22 11:36:40 -0600394 gUseSkVMBlitter = FLAGS_skvm;
395 gSkVMJITViaDylib = FLAGS_dylib;
Mike Klein2a57e792020-01-14 12:47:52 -0600396
Mike Klein735c7ba2019-03-25 12:57:58 -0500397 initializeEventTracingForTools();
398 ToolUtils::SetDefaultFontMgr();
399 SetAnalyticAAFromCommonFlags();
400
401 GrContextOptions baseOptions;
402 SetCtxOptionsFromCommonFlags(&baseOptions);
403
Brian Osman5aa11fb2019-04-08 16:40:36 -0400404 sk_gpu_test::MemoryCache memoryCache;
405 if (!FLAGS_writeShaders.isEmpty()) {
406 baseOptions.fPersistentCache = &memoryCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400407 baseOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5aa11fb2019-04-08 16:40:36 -0400408 }
409
Mike Kleincbe93ee2019-04-02 17:00:54 -0400410 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500411 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Hal Canaryedda5652019-08-05 10:28:09 -0400412 std::unique_ptr<skiagm::GM> gm{factory()};
Mike Kleinfee00792019-11-21 16:18:47 -0600413 if (FLAGS_listGMs) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500414 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400415 } else {
416 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500417 }
418 }
Mike Kleinfee00792019-11-21 16:18:47 -0600419
420 SkTHashMap<SkString, const skiatest::Test*> tests;
421 for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
422 if (test.needsGpu) {
423 continue; // TODO
424 }
425 if (FLAGS_listTests) {
426 fprintf(stdout, "%s\n", test.name);
427 } else {
428 tests.set(SkString{test.name}, &test);
429 }
430 }
431
432 if (FLAGS_listGMs || FLAGS_listTests) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400433 return 0;
434 }
Mike Kleinfee00792019-11-21 16:18:47 -0600435 if (FLAGS_sources.isEmpty()) {
436 fprintf(stderr, "Please give me something to run using -s/--sources!\n");
437 return 1;
438 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400439
440 SkTArray<Source> sources;
Mike Kleina833cff2019-04-09 11:52:40 -0500441 for (const SkString& name : FLAGS_sources) {
442 Source* source = &sources.push_back();
443
444 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Hal Canaryedda5652019-08-05 10:28:09 -0400445 std::shared_ptr<skiagm::GM> gm{(*factory)()};
Mike Kleina833cff2019-04-09 11:52:40 -0500446 source->name = name;
Ben Wagner406ff502019-08-12 16:39:24 -0400447 init(source, std::move(gm));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400448 continue;
449 }
450
Mike Kleinfee00792019-11-21 16:18:47 -0600451 if (const skiatest::Test** test = tests.find(name)) {
452 source->name = name;
453 init(source, **test);
454 continue;
455 }
456
Mike Kleina833cff2019-04-09 11:52:40 -0500457 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
458 source->name = SkOSPath::Basename(name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500459
Mike Kleince90d6f2019-03-29 11:14:14 -0500460 if (name.endsWith(".skp")) {
461 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500462 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400463 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500464 }
465 } else if (name.endsWith(".svg")) {
466 SkMemoryStream stream{blob};
467 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500468 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400469 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500470 }
Brian Osmanca946562019-04-10 13:51:53 -0400471 }
472#if defined(SK_ENABLE_SKOTTIE)
473 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500474 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400475 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
Brian Osman849f4d62019-11-26 08:58:26 -0500476 .setResourceProvider(skresources::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400477 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500478 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400479 continue;
Mike Klein22924262019-04-02 14:14:56 -0400480 }
Brian Osmanca946562019-04-10 13:51:53 -0400481 }
482#endif
483 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500484 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400485 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500486 }
487 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400488
Mike Kleina833cff2019-04-09 11:52:40 -0500489 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400490 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500491 }
492
Mike Klein9b462092019-03-27 13:52:35 -0500493 enum NonGpuBackends {
494 kCPU_Backend = -1,
495 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500496 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500497 };
498 const FlagOption<int> kBackends[] = {
499 { "cpu" , kCPU_Backend },
500 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500501 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500502 { "gl" , GrContextFactory::kGL_ContextType },
503 { "gles" , GrContextFactory::kGLES_ContextType },
504 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
505 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
506 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
507 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
508 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
509 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
510 { "vk" , GrContextFactory::kVulkan_ContextType },
511 { "mtl" , GrContextFactory::kMetal_ContextType },
512 { "mock" , GrContextFactory::kMock_ContextType },
513 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500514 const FlagOption<SkColorType> kColorTypes[] = {
Mike Kleinf7eb0542020-02-11 12:19:08 -0600515 { "a8", kAlpha_8_SkColorType },
516 { "g8", kGray_8_SkColorType },
517 { "565", kRGB_565_SkColorType },
518 { "4444", kARGB_4444_SkColorType },
519 { "8888", kN32_SkColorType },
520 { "888x", kRGB_888x_SkColorType },
521 { "1010102", kRGBA_1010102_SkColorType },
522 { "101010x", kRGB_101010x_SkColorType },
523 { "bgra1010102", kBGRA_1010102_SkColorType },
524 { "bgr101010x", kBGR_101010x_SkColorType },
525 { "f16norm", kRGBA_F16Norm_SkColorType },
526 { "f16", kRGBA_F16_SkColorType },
527 { "f32", kRGBA_F32_SkColorType },
528 { "rgba", kRGBA_8888_SkColorType },
529 { "bgra", kBGRA_8888_SkColorType },
Mike Klein735c7ba2019-03-25 12:57:58 -0500530 };
531 const FlagOption<SkAlphaType> kAlphaTypes[] = {
532 { "premul", kPremul_SkAlphaType },
533 { "unpremul", kUnpremul_SkAlphaType },
534 };
535 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
536 { "srgb", SkNamedGamut::kSRGB },
Mike Kleinb147ace2020-01-16 11:11:06 -0600537 { "p3", SkNamedGamut::kDisplayP3 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500538 { "rec2020", SkNamedGamut::kRec2020 },
539 { "adobe", SkNamedGamut::kAdobeRGB },
540 { "narrow", gNarrow_toXYZD50},
541 };
542 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
543 { "srgb" , SkNamedTransferFn::kSRGB },
Hal Canarybe67a172019-05-24 10:13:36 -0400544 { "rec2020", SkNamedTransferFn::kRec2020 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500545 { "2.2" , SkNamedTransferFn::k2Dot2 },
546 { "linear" , SkNamedTransferFn::kLinear },
547 };
548
Mike Klein735c7ba2019-03-25 12:57:58 -0500549
Mike Klein9b462092019-03-27 13:52:35 -0500550 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500551 SkColorType ct;
552 SkAlphaType at;
553 skcms_Matrix3x3 gamut;
554 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500555
Mike Klein9b462092019-03-27 13:52:35 -0500556 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
557 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500558 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
559 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500560 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500561 return 1;
562 }
563
Mike Klein4daf6372019-04-03 11:19:45 -0400564 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
565 : SkColorSpace::MakeRGB(tf,gamut);
566 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500567
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400568 AutoreleasePool pool;
Mike Klein735c7ba2019-03-25 12:57:58 -0500569 for (auto source : sources) {
570 const auto start = std::chrono::steady_clock::now();
Mike Klein832d3c52019-04-02 15:23:46 -0400571 fprintf(stdout, "%50s", source.name.c_str());
Mike Kleinbf15b662019-04-15 11:32:16 -0500572 fflush(stdout);
Mike Klein735c7ba2019-03-25 12:57:58 -0500573
Brian Salomon9241a6d2019-10-03 13:26:54 -0400574 const SkImageInfo info = unsized_info.makeDimensions(source.size);
Mike Klein735c7ba2019-03-25 12:57:58 -0500575
Mike Kleina833cff2019-04-09 11:52:40 -0500576 auto draw = [&source](SkCanvas* canvas) {
577 Result result = source.draw(canvas);
578 switch (result.status) {
579 case Result::Ok: break;
580 case Result::Skip: return false;
581 case Result::Fail:
John Stiles616da102020-06-12 14:07:41 -0400582 SK_ABORT("%s", result.failure.c_str());
Mike Kleina833cff2019-04-09 11:52:40 -0500583 }
584 return true;
585 };
586
Mike Klein735c7ba2019-03-25 12:57:58 -0500587 GrContextOptions options = baseOptions;
588 source.tweak(&options);
589 GrContextFactory factory(options); // N.B. factory must outlive image
590
591 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500592 sk_sp<SkData> blob;
593 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500594 switch (backend) {
595 case kCPU_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500596 image = draw_with_cpu(draw, info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500597 break;
Mike Klein9b462092019-03-27 13:52:35 -0500598 case kSKP_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500599 blob = draw_as_skp(draw, info);
Mike Klein9b462092019-03-27 13:52:35 -0500600 ext = ".skp";
601 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500602 case kPDF_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500603 blob = draw_as_pdf(draw, info, source.name);
Mike Kleinc245bd92019-03-27 14:31:09 -0500604 ext = ".pdf";
605 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500606 default:
Mike Kleina833cff2019-04-09 11:52:40 -0500607 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500608 break;
609 }
610
Mike Klein9b462092019-03-27 13:52:35 -0500611 if (!image && !blob) {
Mike Klein832d3c52019-04-02 15:23:46 -0400612 fprintf(stdout, "\tskipped\n");
Mike Kleina5c27172019-04-02 10:26:48 -0400613 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500614 }
615
Mike Kleined2cec42019-11-01 10:51:01 -0500616 // We read back a bitmap even when --quick is set and we won't use it,
617 // to keep us honest about deferred work, flushing pipelines, etc.
Mike Klein735c7ba2019-03-25 12:57:58 -0500618 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500619 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500620 SK_ABORT("SkImage::asLegacyBitmap() failed.");
Mike Klein735c7ba2019-03-25 12:57:58 -0500621 }
622
Mike Klein735c7ba2019-03-25 12:57:58 -0500623 SkString md5;
Mike Kleined2cec42019-11-01 10:51:01 -0500624 if (!FLAGS_quick) {
625 HashAndEncode hashAndEncode{bitmap};
626 {
627 SkMD5 hash;
628 if (image) {
629 hashAndEncode.write(&hash);
630 } else {
631 hash.write(blob->data(), blob->size());
Mike Klein9b462092019-03-27 13:52:35 -0500632 }
Mike Kleined2cec42019-11-01 10:51:01 -0500633
634 SkMD5::Digest digest = hash.finish();
635 for (int i = 0; i < 16; i++) {
636 md5.appendf("%02x", digest.data[i]);
637 }
638 }
639
640 if (!FLAGS_writePath.isEmpty()) {
641 sk_mkdir(FLAGS_writePath[0]);
642 SkString path = SkStringPrintf("%s/%s%s",
643 FLAGS_writePath[0], source.name.c_str(), ext);
644
645 if (image) {
646 if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(),
647 FLAGS_key, FLAGS_properties)) {
648 SK_ABORT("Could not write .png.");
649 }
650 } else {
651 SkFILEWStream file(path.c_str());
652 file.write(blob->data(), blob->size());
653 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500654 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500655 }
656
Mike Klein832d3c52019-04-02 15:23:46 -0400657 const auto elapsed = std::chrono::steady_clock::now() - start;
658 fprintf(stdout, "\t%s\t%7dms\n",
659 md5.c_str(),
660 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400661 pool.drain();
Mike Klein735c7ba2019-03-25 12:57:58 -0500662 }
663
Brian Osman5aa11fb2019-04-08 16:40:36 -0400664 if (!FLAGS_writeShaders.isEmpty()) {
665 sk_mkdir(FLAGS_writeShaders[0]);
666 GrBackendApi api =
667 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
668 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
669
670 }
671
Mike Klein735c7ba2019-03-25 12:57:58 -0500672 return 0;
673}