blob: 65a9c9339e66597af04d724435f97579c6e81990 [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"
7#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"
13#include "include/private/SkTHash.h"
14#include "src/core/SkColorSpacePriv.h"
15#include "src/core/SkMD5.h"
16#include "src/core/SkOSFile.h"
17#include "src/gpu/GrContextPriv.h"
18#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/utils/SkOSPath.h"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040020#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/CrashHandler.h"
22#include "tools/HashAndEncode.h"
23#include "tools/ToolUtils.h"
24#include "tools/flags/CommandLineFlags.h"
25#include "tools/flags/CommonFlags.h"
26#include "tools/gpu/GrContextFactory.h"
27#include "tools/gpu/MemoryCache.h"
28#include "tools/trace/EventTracingPriv.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050029#include <chrono>
30#include <functional>
31#include <stdio.h>
32#include <stdlib.h>
33
Brian Osmanca946562019-04-10 13:51:53 -040034#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035 #include "modules/skottie/include/Skottie.h"
36 #include "modules/skottie/utils/SkottieUtils.h"
Brian Osmanca946562019-04-10 13:51:53 -040037#endif
38
Mike Klein735c7ba2019-03-25 12:57:58 -050039using sk_gpu_test::GrContextFactory;
40
41static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw.");
42static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into.");
43
Mike Klein4daf6372019-04-03 11:19:45 -040044static DEFINE_string(ct , "8888", "The color type for any raster backend.");
45static DEFINE_string(at , "premul", "The alpha type for any raster backend.");
46static DEFINE_string(gamut , "srgb", "The color gamut for any raster backend.");
47static DEFINE_string(tf , "srgb", "The transfer function for any raster backend.");
48static DEFINE_bool (legacy, false, "Use a null SkColorSpace instead of --gamut and --tf?");
Mike Klein735c7ba2019-03-25 12:57:58 -050049
50static DEFINE_int (samples , 0, "Samples per pixel in GPU backends.");
Mike Klein735c7ba2019-03-25 12:57:58 -050051static DEFINE_bool (stencils, true, "If false, avoid stencil buffers in GPU backends.");
52static DEFINE_bool (dit , false, "Use device-independent text in GPU backends.");
53static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces.");
54
55static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing.");
56static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing.");
57static DEFINE_bool(releaseAndAbandonGpuContext, false,
58 "Release all GPU resources and abandon the GrContext after drawing.");
59
60static DEFINE_bool(decodeToDst, false,
61 "Decode images to destination format rather than suggested natural format.");
62
Mike Kleinc245bd92019-03-27 14:31:09 -050063static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI,
64 "DPI for rasterized content in vector backends like --backend pdf.");
65static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?");
66
Mike Klein735c7ba2019-03-25 12:57:58 -050067static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?");
68static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set.");
Mike Klein735c7ba2019-03-25 12:57:58 -050069
Brian Osman5aa11fb2019-04-08 16:40:36 -040070static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set.");
71
Mike Klein735c7ba2019-03-25 12:57:58 -050072static DEFINE_string(key, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein7b8bc532019-04-09 11:19:47 -050073static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein735c7ba2019-03-25 12:57:58 -050074
75template <typename T>
76struct FlagOption {
77 const char* label;
78 T value;
79};
80
81template <typename T, int N>
82static bool parse_flag(const CommandLineFlags::StringArray& flag,
83 const char* flag_name,
84 const FlagOption<T> (&array)[N],
85 T* value) {
86 for (auto entry : array) {
87 if (flag.contains(entry.label)) {
88 *value = entry.value;
89 return true;
90 }
91 }
92 fprintf(stderr, "Known values for --%s:\n", flag_name);
93 for (auto entry : array) {
94 fprintf(stderr, " --%s %s\n", flag_name, entry.label);
95 }
96 return false;
97}
98
Mike Kleina833cff2019-04-09 11:52:40 -050099struct Result {
100 enum { Ok, Skip, Fail} status;
101 SkString failure;
102};
103static const Result ok = {Result::Ok, {}},
104 skip = {Result::Skip, {}};
105
106template <typename... Args>
107static Result fail(const char* why, Args... args) {
108 return { Result::Fail, SkStringPrintf(why, args...) };
109}
110
111
Mike Klein735c7ba2019-03-25 12:57:58 -0500112struct Source {
113 SkString name;
114 SkISize size;
Mike Kleina833cff2019-04-09 11:52:40 -0500115 std::function<Result(SkCanvas*)> draw;
116 std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
Mike Klein735c7ba2019-03-25 12:57:58 -0500117};
118
Mike Kleina833cff2019-04-09 11:52:40 -0500119static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
120 source->size = gm->getISize();
121 source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
122 source->draw = [gm](SkCanvas* canvas) {
123 SkString err;
124 switch (gm->draw(canvas, &err)) {
125 case skiagm::DrawResult::kOk: break;
126 case skiagm::DrawResult::kSkip: return skip;
127 case skiagm::DrawResult::kFail: return fail(err.c_str());
128 }
129 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500130 };
131}
132
Mike Kleina833cff2019-04-09 11:52:40 -0500133static void init(Source* source, sk_sp<SkPicture> pic) {
134 source->size = pic->cullRect().roundOut().size();
135 source->draw = [pic](SkCanvas* canvas) {
136 canvas->drawPicture(pic);
137 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500138 };
139}
140
Mike Kleina833cff2019-04-09 11:52:40 -0500141static void init(Source* source, std::shared_ptr<SkCodec> codec) {
142 source->size = codec->dimensions();
143 source->draw = [codec](SkCanvas* canvas) {
144 SkImageInfo info = codec->getInfo();
145 if (FLAGS_decodeToDst) {
146 info = canvas->imageInfo().makeWH(info.width(),
147 info.height());
148 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500149
Mike Kleina833cff2019-04-09 11:52:40 -0500150 SkBitmap bm;
151 bm.allocPixels(info);
152 switch (SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) {
153 case SkCodec::kSuccess:
154 case SkCodec::kErrorInInput:
155 case SkCodec::kIncompleteInput: canvas->drawBitmap(bm, 0,0);
156 break;
157 default: return fail("codec->getPixels() failed: %d\n", result);
158 }
159 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500160 };
161}
162
Mike Kleina833cff2019-04-09 11:52:40 -0500163static void init(Source* source, sk_sp<SkSVGDOM> svg) {
164 source->size = svg->containerSize().isEmpty() ? SkISize{1000,1000}
165 : svg->containerSize().toCeil();
166 source->draw = [svg](SkCanvas* canvas) {
167 svg->render(canvas);
168 return ok;
Mike Klein22924262019-04-02 14:14:56 -0400169 };
170}
171
Brian Osmanca946562019-04-10 13:51:53 -0400172#if defined(SK_ENABLE_SKOTTIE)
Mike Kleina833cff2019-04-09 11:52:40 -0500173static void init(Source* source, sk_sp<skottie::Animation> animation) {
174 source->size = {1000,1000};
175 source->draw = [animation](SkCanvas* canvas) {
176 canvas->clear(SK_ColorWHITE);
Mike Klein22924262019-04-02 14:14:56 -0400177
Mike Kleina833cff2019-04-09 11:52:40 -0500178 // Draw frames in a shuffled order to exercise nonlinear frame progression.
179 // The film strip will still be in time order, just drawn out of order.
180 const int order[] = { 4, 0, 3, 1, 2 };
181 const int tiles = SK_ARRAY_COUNT(order);
182 const float dim = 1000.0f / tiles;
Mike Klein22924262019-04-02 14:14:56 -0400183
Mike Kleina833cff2019-04-09 11:52:40 -0500184 const float dt = 1.0f / (tiles*tiles - 1);
Mike Klein22924262019-04-02 14:14:56 -0400185
Mike Kleina833cff2019-04-09 11:52:40 -0500186 for (int y : order)
187 for (int x : order) {
188 SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
Mike Klein22924262019-04-02 14:14:56 -0400189
Mike Kleina833cff2019-04-09 11:52:40 -0500190 SkAutoCanvasRestore _(canvas, true/*save now*/);
191 canvas->clipRect(dst, /*aa=*/true);
192 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
193 dst,
194 SkMatrix::kCenter_ScaleToFit));
195 float t = (y*tiles + x) * dt;
196 animation->seek(t);
197 animation->render(canvas);
198 }
199 return ok;
Mike Klein6253d902019-03-27 15:09:12 -0500200 };
201}
Brian Osmanca946562019-04-10 13:51:53 -0400202#endif
Mike Klein6253d902019-03-27 15:09:12 -0500203
Mike Kleina5c27172019-04-02 10:26:48 -0400204static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500205 SkImageInfo info) {
206 if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400207 if (draw(surface->getCanvas())) {
208 return surface->makeImageSnapshot();
209 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500210 }
211 return nullptr;
212}
213
Mike Kleina5c27172019-04-02 10:26:48 -0400214static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
Mike Klein9b462092019-03-27 13:52:35 -0500215 SkImageInfo info) {
216 SkPictureRecorder recorder;
Mike Kleina5c27172019-04-02 10:26:48 -0400217 if (draw(recorder.beginRecording(info.width(), info.height()))) {
218 return recorder.finishRecordingAsPicture()->serialize();
219 }
220 return nullptr;
Mike Klein9b462092019-03-27 13:52:35 -0500221}
222
Mike Kleina5c27172019-04-02 10:26:48 -0400223static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
Mike Kleinc245bd92019-03-27 14:31:09 -0500224 SkImageInfo info,
225 SkString name) {
226 SkPDF::Metadata metadata;
227 metadata.fTitle = name;
228 metadata.fCreator = "Skia/FM";
229 metadata.fRasterDPI = FLAGS_rasterDPI;
230 metadata.fPDFA = FLAGS_PDFA;
231
232 SkDynamicMemoryWStream stream;
233 if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400234 if (draw(doc->beginPage(info.width(), info.height()))) {
235 doc->endPage();
236 doc->close();
237 return stream.detachAsData();
238 }
Mike Kleinc245bd92019-03-27 14:31:09 -0500239 }
240 return nullptr;
241}
242
Mike Kleina5c27172019-04-02 10:26:48 -0400243static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500244 SkImageInfo info,
245 GrContextFactory::ContextType api,
246 GrContextFactory* factory) {
247 enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
248 const FlagOption<SurfaceType> kSurfaceTypes[] = {
249 { "default", SurfaceType::kDefault },
250 { "betex" , SurfaceType::kBackendTexture },
251 { "bert" , SurfaceType::kBackendRenderTarget },
252 };
253 SurfaceType surfaceType;
254 if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
255 return nullptr;
256 }
257
Chris Daltonb3c97452019-06-25 20:07:56 -0600258 auto overrides = GrContextFactory::ContextOverrides::kNone;
Mike Klein735c7ba2019-03-25 12:57:58 -0500259 if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
260
261 GrContext* context = factory->getContextInfo(api, overrides)
262 .grContext();
263
264 uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
265 : 0;
266 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
267
268 sk_sp<SkSurface> surface;
269 GrBackendTexture backendTexture;
270 GrBackendRenderTarget backendRT;
271
272 switch (surfaceType) {
273 case SurfaceType::kDefault:
274 surface = SkSurface::MakeRenderTarget(context,
275 SkBudgeted::kNo,
276 info,
277 FLAGS_samples,
278 &props);
279 break;
280
281 case SurfaceType::kBackendTexture:
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400282 backendTexture = context->createBackendTexture(info.width(),
283 info.height(),
284 info.colorType(),
285 GrMipMapped::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400286 GrRenderable::kYes,
287 GrProtected::kNo);
Mike Klein735c7ba2019-03-25 12:57:58 -0500288 surface = SkSurface::MakeFromBackendTexture(context,
289 backendTexture,
290 kTopLeft_GrSurfaceOrigin,
291 FLAGS_samples,
292 info.colorType(),
293 info.refColorSpace(),
294 &props);
295 break;
296
297 case SurfaceType::kBackendRenderTarget:
298 backendRT = context->priv().getGpu()
299 ->createTestingOnlyBackendRenderTarget(info.width(),
300 info.height(),
301 SkColorTypeToGrColorType(info.colorType()));
302 surface = SkSurface::MakeFromBackendRenderTarget(context,
303 backendRT,
304 kBottomLeft_GrSurfaceOrigin,
305 info.colorType(),
306 info.refColorSpace(),
307 &props);
308 break;
309 }
310
311 if (!surface) {
312 fprintf(stderr, "Could not create GPU surface.\n");
313 return nullptr;
314 }
315
316 if (FLAGS_preAbandonGpuContext) {
317 factory->abandonContexts();
318 }
319
Mike Kleina5c27172019-04-02 10:26:48 -0400320 sk_sp<SkImage> image;
321 if (draw(surface->getCanvas())) {
322 image = surface->makeImageSnapshot();
323 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500324
325 if (FLAGS_abandonGpuContext) {
326 factory->abandonContexts();
327 } else if (FLAGS_releaseAndAbandonGpuContext) {
328 factory->releaseResourcesAndAbandonContexts();
329 }
330
331 if (!context->abandoned()) {
332 surface.reset();
333 if (backendTexture.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400334 context->deleteBackendTexture(backendTexture);
Mike Klein735c7ba2019-03-25 12:57:58 -0500335 }
336 if (backendRT.isValid()) {
337 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
338 }
339 }
340
341 return image;
342}
343
344int main(int argc, char** argv) {
345 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500346 SetupCrashHandler();
Mike Klein735c7ba2019-03-25 12:57:58 -0500347
348 if (FLAGS_cpuDetect) {
349 SkGraphics::Init();
350 }
351 initializeEventTracingForTools();
352 ToolUtils::SetDefaultFontMgr();
353 SetAnalyticAAFromCommonFlags();
354
355 GrContextOptions baseOptions;
356 SetCtxOptionsFromCommonFlags(&baseOptions);
357
Brian Osman5aa11fb2019-04-08 16:40:36 -0400358 sk_gpu_test::MemoryCache memoryCache;
359 if (!FLAGS_writeShaders.isEmpty()) {
360 baseOptions.fPersistentCache = &memoryCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400361 baseOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5aa11fb2019-04-08 16:40:36 -0400362 }
363
Mike Kleincbe93ee2019-04-02 17:00:54 -0400364 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500365 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Hal Canaryedda5652019-08-05 10:28:09 -0400366 std::unique_ptr<skiagm::GM> gm{factory()};
Mike Klein735c7ba2019-03-25 12:57:58 -0500367 if (FLAGS_sources.isEmpty()) {
368 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400369 } else {
370 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500371 }
372 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400373 if (FLAGS_sources.isEmpty()) {
374 return 0;
375 }
376
377 SkTArray<Source> sources;
Mike Kleina833cff2019-04-09 11:52:40 -0500378 for (const SkString& name : FLAGS_sources) {
379 Source* source = &sources.push_back();
380
381 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Hal Canaryedda5652019-08-05 10:28:09 -0400382 std::shared_ptr<skiagm::GM> gm{(*factory)()};
Mike Kleina833cff2019-04-09 11:52:40 -0500383 source->name = name;
Ben Wagner406ff502019-08-12 16:39:24 -0400384 init(source, std::move(gm));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400385 continue;
386 }
387
Mike Kleina833cff2019-04-09 11:52:40 -0500388 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
389 source->name = SkOSPath::Basename(name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500390
Mike Kleince90d6f2019-03-29 11:14:14 -0500391 if (name.endsWith(".skp")) {
392 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500393 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400394 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500395 }
396 } else if (name.endsWith(".svg")) {
397 SkMemoryStream stream{blob};
398 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500399 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400400 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500401 }
Brian Osmanca946562019-04-10 13:51:53 -0400402 }
403#if defined(SK_ENABLE_SKOTTIE)
404 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500405 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400406 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
407 .setResourceProvider(skottie_utils::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400408 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500409 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400410 continue;
Mike Klein22924262019-04-02 14:14:56 -0400411 }
Brian Osmanca946562019-04-10 13:51:53 -0400412 }
413#endif
414 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500415 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400416 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500417 }
418 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400419
Mike Kleina833cff2019-04-09 11:52:40 -0500420 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400421 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500422 }
423
Mike Klein9b462092019-03-27 13:52:35 -0500424 enum NonGpuBackends {
425 kCPU_Backend = -1,
426 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500427 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500428 };
429 const FlagOption<int> kBackends[] = {
430 { "cpu" , kCPU_Backend },
431 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500432 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500433 { "gl" , GrContextFactory::kGL_ContextType },
434 { "gles" , GrContextFactory::kGLES_ContextType },
435 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
436 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
437 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
438 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
439 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
440 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
441 { "vk" , GrContextFactory::kVulkan_ContextType },
442 { "mtl" , GrContextFactory::kMetal_ContextType },
443 { "mock" , GrContextFactory::kMock_ContextType },
444 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500445 const FlagOption<SkColorType> kColorTypes[] = {
446 { "a8", kAlpha_8_SkColorType },
447 { "g8", kGray_8_SkColorType },
448 { "565", kRGB_565_SkColorType },
449 { "4444", kARGB_4444_SkColorType },
450 { "8888", kN32_SkColorType },
451 { "888x", kRGB_888x_SkColorType },
452 { "1010102", kRGBA_1010102_SkColorType },
453 { "101010x", kRGB_101010x_SkColorType },
454 { "f16norm", kRGBA_F16Norm_SkColorType },
455 { "f16", kRGBA_F16_SkColorType },
456 { "f32", kRGBA_F32_SkColorType },
457 { "rgba", kRGBA_8888_SkColorType },
458 { "bgra", kBGRA_8888_SkColorType },
459 };
460 const FlagOption<SkAlphaType> kAlphaTypes[] = {
461 { "premul", kPremul_SkAlphaType },
462 { "unpremul", kUnpremul_SkAlphaType },
463 };
464 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
465 { "srgb", SkNamedGamut::kSRGB },
466 { "p3", SkNamedGamut::kDCIP3 },
467 { "rec2020", SkNamedGamut::kRec2020 },
468 { "adobe", SkNamedGamut::kAdobeRGB },
469 { "narrow", gNarrow_toXYZD50},
470 };
471 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
472 { "srgb" , SkNamedTransferFn::kSRGB },
Hal Canarybe67a172019-05-24 10:13:36 -0400473 { "rec2020", SkNamedTransferFn::kRec2020 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500474 { "2.2" , SkNamedTransferFn::k2Dot2 },
475 { "linear" , SkNamedTransferFn::kLinear },
476 };
477
Mike Klein735c7ba2019-03-25 12:57:58 -0500478
Mike Klein9b462092019-03-27 13:52:35 -0500479 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500480 SkColorType ct;
481 SkAlphaType at;
482 skcms_Matrix3x3 gamut;
483 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500484
Mike Klein9b462092019-03-27 13:52:35 -0500485 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
486 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500487 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
488 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500489 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500490 return 1;
491 }
492
Mike Klein4daf6372019-04-03 11:19:45 -0400493 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
494 : SkColorSpace::MakeRGB(tf,gamut);
495 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500496
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400497 AutoreleasePool pool;
Mike Klein735c7ba2019-03-25 12:57:58 -0500498 for (auto source : sources) {
499 const auto start = std::chrono::steady_clock::now();
Mike Klein832d3c52019-04-02 15:23:46 -0400500 fprintf(stdout, "%50s", source.name.c_str());
Mike Kleinbf15b662019-04-15 11:32:16 -0500501 fflush(stdout);
Mike Klein735c7ba2019-03-25 12:57:58 -0500502
503 const SkImageInfo info = unsized_info.makeWH(source.size.width(),
504 source.size.height());
505
Mike Kleina833cff2019-04-09 11:52:40 -0500506 auto draw = [&source](SkCanvas* canvas) {
507 Result result = source.draw(canvas);
508 switch (result.status) {
509 case Result::Ok: break;
510 case Result::Skip: return false;
511 case Result::Fail:
Mike Kleinbf15b662019-04-15 11:32:16 -0500512 SK_ABORT(result.failure.c_str());
Mike Kleina833cff2019-04-09 11:52:40 -0500513 }
514 return true;
515 };
516
Mike Klein735c7ba2019-03-25 12:57:58 -0500517 GrContextOptions options = baseOptions;
518 source.tweak(&options);
519 GrContextFactory factory(options); // N.B. factory must outlive image
520
521 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500522 sk_sp<SkData> blob;
523 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500524 switch (backend) {
525 case kCPU_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500526 image = draw_with_cpu(draw, info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500527 break;
Mike Klein9b462092019-03-27 13:52:35 -0500528 case kSKP_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500529 blob = draw_as_skp(draw, info);
Mike Klein9b462092019-03-27 13:52:35 -0500530 ext = ".skp";
531 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500532 case kPDF_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500533 blob = draw_as_pdf(draw, info, source.name);
Mike Kleinc245bd92019-03-27 14:31:09 -0500534 ext = ".pdf";
535 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500536 default:
Mike Kleina833cff2019-04-09 11:52:40 -0500537 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500538 break;
539 }
540
Mike Klein9b462092019-03-27 13:52:35 -0500541 if (!image && !blob) {
Mike Klein832d3c52019-04-02 15:23:46 -0400542 fprintf(stdout, "\tskipped\n");
Mike Kleina5c27172019-04-02 10:26:48 -0400543 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500544 }
545
546 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500547 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500548 SK_ABORT("SkImage::asLegacyBitmap() failed.");
Mike Klein735c7ba2019-03-25 12:57:58 -0500549 }
550
551 HashAndEncode hashAndEncode{bitmap};
552 SkString md5;
553 {
554 SkMD5 hash;
Mike Klein9b462092019-03-27 13:52:35 -0500555 if (image) {
556 hashAndEncode.write(&hash);
557 } else {
558 hash.write(blob->data(), blob->size());
559 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500560
Hal Canary0f2f5222019-04-03 10:13:45 -0400561 SkMD5::Digest digest = hash.finish();
Mike Klein735c7ba2019-03-25 12:57:58 -0500562 for (int i = 0; i < 16; i++) {
563 md5.appendf("%02x", digest.data[i]);
564 }
565 }
566
567 if (!FLAGS_writePath.isEmpty()) {
568 sk_mkdir(FLAGS_writePath[0]);
Mike Klein9b462092019-03-27 13:52:35 -0500569 SkString path = SkStringPrintf("%s/%s%s", FLAGS_writePath[0], source.name.c_str(), ext);
Mike Klein735c7ba2019-03-25 12:57:58 -0500570
Mike Klein9b462092019-03-27 13:52:35 -0500571 if (image) {
572 if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(),
Mike Klein7b8bc532019-04-09 11:19:47 -0500573 FLAGS_key, FLAGS_properties)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500574 SK_ABORT("Could not write .png.");
Mike Klein9b462092019-03-27 13:52:35 -0500575 }
576 } else {
577 SkFILEWStream file(path.c_str());
578 file.write(blob->data(), blob->size());
Mike Klein735c7ba2019-03-25 12:57:58 -0500579 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500580 }
581
Mike Klein832d3c52019-04-02 15:23:46 -0400582 const auto elapsed = std::chrono::steady_clock::now() - start;
583 fprintf(stdout, "\t%s\t%7dms\n",
584 md5.c_str(),
585 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400586 pool.drain();
Mike Klein735c7ba2019-03-25 12:57:58 -0500587 }
588
Brian Osman5aa11fb2019-04-08 16:40:36 -0400589 if (!FLAGS_writeShaders.isEmpty()) {
590 sk_mkdir(FLAGS_writeShaders[0]);
591 GrBackendApi api =
592 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
593 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
594
595 }
596
Mike Klein735c7ba2019-03-25 12:57:58 -0500597 return 0;
598}