blob: 3bcbbf5d13d54ae2208dc1a8ec8eaa67e60dc806 [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.");
51static DEFINE_bool (nvpr , false, "Use NV_path_rendering in GPU backends?");
52static DEFINE_bool (stencils, true, "If false, avoid stencil buffers in GPU backends.");
53static DEFINE_bool (dit , false, "Use device-independent text in GPU backends.");
54static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces.");
55
56static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing.");
57static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing.");
58static DEFINE_bool(releaseAndAbandonGpuContext, false,
59 "Release all GPU resources and abandon the GrContext after drawing.");
60
61static DEFINE_bool(decodeToDst, false,
62 "Decode images to destination format rather than suggested natural format.");
63
Mike Kleinc245bd92019-03-27 14:31:09 -050064static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI,
65 "DPI for rasterized content in vector backends like --backend pdf.");
66static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?");
67
Mike Klein735c7ba2019-03-25 12:57:58 -050068static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?");
69static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set.");
Mike Klein735c7ba2019-03-25 12:57:58 -050070
Brian Osman5aa11fb2019-04-08 16:40:36 -040071static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set.");
72
Mike Klein735c7ba2019-03-25 12:57:58 -050073static DEFINE_string(key, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein7b8bc532019-04-09 11:19:47 -050074static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein735c7ba2019-03-25 12:57:58 -050075
76template <typename T>
77struct FlagOption {
78 const char* label;
79 T value;
80};
81
82template <typename T, int N>
83static bool parse_flag(const CommandLineFlags::StringArray& flag,
84 const char* flag_name,
85 const FlagOption<T> (&array)[N],
86 T* value) {
87 for (auto entry : array) {
88 if (flag.contains(entry.label)) {
89 *value = entry.value;
90 return true;
91 }
92 }
93 fprintf(stderr, "Known values for --%s:\n", flag_name);
94 for (auto entry : array) {
95 fprintf(stderr, " --%s %s\n", flag_name, entry.label);
96 }
97 return false;
98}
99
Mike Kleina833cff2019-04-09 11:52:40 -0500100struct Result {
101 enum { Ok, Skip, Fail} status;
102 SkString failure;
103};
104static const Result ok = {Result::Ok, {}},
105 skip = {Result::Skip, {}};
106
107template <typename... Args>
108static Result fail(const char* why, Args... args) {
109 return { Result::Fail, SkStringPrintf(why, args...) };
110}
111
112
Mike Klein735c7ba2019-03-25 12:57:58 -0500113struct Source {
114 SkString name;
115 SkISize size;
Mike Kleina833cff2019-04-09 11:52:40 -0500116 std::function<Result(SkCanvas*)> draw;
117 std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
Mike Klein735c7ba2019-03-25 12:57:58 -0500118};
119
Mike Kleina833cff2019-04-09 11:52:40 -0500120static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
121 source->size = gm->getISize();
122 source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
123 source->draw = [gm](SkCanvas* canvas) {
124 SkString err;
125 switch (gm->draw(canvas, &err)) {
126 case skiagm::DrawResult::kOk: break;
127 case skiagm::DrawResult::kSkip: return skip;
128 case skiagm::DrawResult::kFail: return fail(err.c_str());
129 }
130 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500131 };
132}
133
Mike Kleina833cff2019-04-09 11:52:40 -0500134static void init(Source* source, sk_sp<SkPicture> pic) {
135 source->size = pic->cullRect().roundOut().size();
136 source->draw = [pic](SkCanvas* canvas) {
137 canvas->drawPicture(pic);
138 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500139 };
140}
141
Mike Kleina833cff2019-04-09 11:52:40 -0500142static void init(Source* source, std::shared_ptr<SkCodec> codec) {
143 source->size = codec->dimensions();
144 source->draw = [codec](SkCanvas* canvas) {
145 SkImageInfo info = codec->getInfo();
146 if (FLAGS_decodeToDst) {
147 info = canvas->imageInfo().makeWH(info.width(),
148 info.height());
149 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500150
Mike Kleina833cff2019-04-09 11:52:40 -0500151 SkBitmap bm;
152 bm.allocPixels(info);
153 switch (SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) {
154 case SkCodec::kSuccess:
155 case SkCodec::kErrorInInput:
156 case SkCodec::kIncompleteInput: canvas->drawBitmap(bm, 0,0);
157 break;
158 default: return fail("codec->getPixels() failed: %d\n", result);
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<SkSVGDOM> svg) {
165 source->size = svg->containerSize().isEmpty() ? SkISize{1000,1000}
166 : svg->containerSize().toCeil();
167 source->draw = [svg](SkCanvas* canvas) {
168 svg->render(canvas);
169 return ok;
Mike Klein22924262019-04-02 14:14:56 -0400170 };
171}
172
Brian Osmanca946562019-04-10 13:51:53 -0400173#if defined(SK_ENABLE_SKOTTIE)
Mike Kleina833cff2019-04-09 11:52:40 -0500174static void init(Source* source, sk_sp<skottie::Animation> animation) {
175 source->size = {1000,1000};
176 source->draw = [animation](SkCanvas* canvas) {
177 canvas->clear(SK_ColorWHITE);
Mike Klein22924262019-04-02 14:14:56 -0400178
Mike Kleina833cff2019-04-09 11:52:40 -0500179 // Draw frames in a shuffled order to exercise nonlinear frame progression.
180 // The film strip will still be in time order, just drawn out of order.
181 const int order[] = { 4, 0, 3, 1, 2 };
182 const int tiles = SK_ARRAY_COUNT(order);
183 const float dim = 1000.0f / tiles;
Mike Klein22924262019-04-02 14:14:56 -0400184
Mike Kleina833cff2019-04-09 11:52:40 -0500185 const float dt = 1.0f / (tiles*tiles - 1);
Mike Klein22924262019-04-02 14:14:56 -0400186
Mike Kleina833cff2019-04-09 11:52:40 -0500187 for (int y : order)
188 for (int x : order) {
189 SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
Mike Klein22924262019-04-02 14:14:56 -0400190
Mike Kleina833cff2019-04-09 11:52:40 -0500191 SkAutoCanvasRestore _(canvas, true/*save now*/);
192 canvas->clipRect(dst, /*aa=*/true);
193 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
194 dst,
195 SkMatrix::kCenter_ScaleToFit));
196 float t = (y*tiles + x) * dt;
197 animation->seek(t);
198 animation->render(canvas);
199 }
200 return ok;
Mike Klein6253d902019-03-27 15:09:12 -0500201 };
202}
Brian Osmanca946562019-04-10 13:51:53 -0400203#endif
Mike Klein6253d902019-03-27 15:09:12 -0500204
Mike Kleina5c27172019-04-02 10:26:48 -0400205static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500206 SkImageInfo info) {
207 if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400208 if (draw(surface->getCanvas())) {
209 return surface->makeImageSnapshot();
210 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500211 }
212 return nullptr;
213}
214
Mike Kleina5c27172019-04-02 10:26:48 -0400215static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
Mike Klein9b462092019-03-27 13:52:35 -0500216 SkImageInfo info) {
217 SkPictureRecorder recorder;
Mike Kleina5c27172019-04-02 10:26:48 -0400218 if (draw(recorder.beginRecording(info.width(), info.height()))) {
219 return recorder.finishRecordingAsPicture()->serialize();
220 }
221 return nullptr;
Mike Klein9b462092019-03-27 13:52:35 -0500222}
223
Mike Kleina5c27172019-04-02 10:26:48 -0400224static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
Mike Kleinc245bd92019-03-27 14:31:09 -0500225 SkImageInfo info,
226 SkString name) {
227 SkPDF::Metadata metadata;
228 metadata.fTitle = name;
229 metadata.fCreator = "Skia/FM";
230 metadata.fRasterDPI = FLAGS_rasterDPI;
231 metadata.fPDFA = FLAGS_PDFA;
232
233 SkDynamicMemoryWStream stream;
234 if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400235 if (draw(doc->beginPage(info.width(), info.height()))) {
236 doc->endPage();
237 doc->close();
238 return stream.detachAsData();
239 }
Mike Kleinc245bd92019-03-27 14:31:09 -0500240 }
241 return nullptr;
242}
243
Mike Kleina5c27172019-04-02 10:26:48 -0400244static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500245 SkImageInfo info,
246 GrContextFactory::ContextType api,
247 GrContextFactory* factory) {
248 enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
249 const FlagOption<SurfaceType> kSurfaceTypes[] = {
250 { "default", SurfaceType::kDefault },
251 { "betex" , SurfaceType::kBackendTexture },
252 { "bert" , SurfaceType::kBackendRenderTarget },
253 };
254 SurfaceType surfaceType;
255 if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
256 return nullptr;
257 }
258
259 auto overrides = FLAGS_nvpr ? GrContextFactory::ContextOverrides::kRequireNVPRSupport
260 : GrContextFactory::ContextOverrides::kDisableNVPR;
261 if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
262
263 GrContext* context = factory->getContextInfo(api, overrides)
264 .grContext();
265
266 uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
267 : 0;
268 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
269
270 sk_sp<SkSurface> surface;
271 GrBackendTexture backendTexture;
272 GrBackendRenderTarget backendRT;
273
274 switch (surfaceType) {
275 case SurfaceType::kDefault:
276 surface = SkSurface::MakeRenderTarget(context,
277 SkBudgeted::kNo,
278 info,
279 FLAGS_samples,
280 &props);
281 break;
282
283 case SurfaceType::kBackendTexture:
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400284 backendTexture = context->createBackendTexture(info.width(),
285 info.height(),
286 info.colorType(),
287 GrMipMapped::kNo,
288 GrRenderable::kYes);
Mike Klein735c7ba2019-03-25 12:57:58 -0500289 surface = SkSurface::MakeFromBackendTexture(context,
290 backendTexture,
291 kTopLeft_GrSurfaceOrigin,
292 FLAGS_samples,
293 info.colorType(),
294 info.refColorSpace(),
295 &props);
296 break;
297
298 case SurfaceType::kBackendRenderTarget:
299 backendRT = context->priv().getGpu()
300 ->createTestingOnlyBackendRenderTarget(info.width(),
301 info.height(),
302 SkColorTypeToGrColorType(info.colorType()));
303 surface = SkSurface::MakeFromBackendRenderTarget(context,
304 backendRT,
305 kBottomLeft_GrSurfaceOrigin,
306 info.colorType(),
307 info.refColorSpace(),
308 &props);
309 break;
310 }
311
312 if (!surface) {
313 fprintf(stderr, "Could not create GPU surface.\n");
314 return nullptr;
315 }
316
317 if (FLAGS_preAbandonGpuContext) {
318 factory->abandonContexts();
319 }
320
Mike Kleina5c27172019-04-02 10:26:48 -0400321 sk_sp<SkImage> image;
322 if (draw(surface->getCanvas())) {
323 image = surface->makeImageSnapshot();
324 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500325
326 if (FLAGS_abandonGpuContext) {
327 factory->abandonContexts();
328 } else if (FLAGS_releaseAndAbandonGpuContext) {
329 factory->releaseResourcesAndAbandonContexts();
330 }
331
332 if (!context->abandoned()) {
333 surface.reset();
334 if (backendTexture.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400335 context->deleteBackendTexture(backendTexture);
Mike Klein735c7ba2019-03-25 12:57:58 -0500336 }
337 if (backendRT.isValid()) {
338 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
339 }
340 }
341
342 return image;
343}
344
345int main(int argc, char** argv) {
346 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500347 SetupCrashHandler();
Mike Klein735c7ba2019-03-25 12:57:58 -0500348
349 if (FLAGS_cpuDetect) {
350 SkGraphics::Init();
351 }
352 initializeEventTracingForTools();
353 ToolUtils::SetDefaultFontMgr();
354 SetAnalyticAAFromCommonFlags();
355
356 GrContextOptions baseOptions;
357 SetCtxOptionsFromCommonFlags(&baseOptions);
358
Brian Osman5aa11fb2019-04-08 16:40:36 -0400359 sk_gpu_test::MemoryCache memoryCache;
360 if (!FLAGS_writeShaders.isEmpty()) {
361 baseOptions.fPersistentCache = &memoryCache;
362 baseOptions.fDisallowGLSLBinaryCaching = true;
363 }
364
Mike Kleincbe93ee2019-04-02 17:00:54 -0400365 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500366 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400367 std::unique_ptr<skiagm::GM> gm{factory(nullptr)};
Mike Klein735c7ba2019-03-25 12:57:58 -0500368 if (FLAGS_sources.isEmpty()) {
369 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400370 } else {
371 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500372 }
373 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400374 if (FLAGS_sources.isEmpty()) {
375 return 0;
376 }
377
378 SkTArray<Source> sources;
Mike Kleina833cff2019-04-09 11:52:40 -0500379 for (const SkString& name : FLAGS_sources) {
380 Source* source = &sources.push_back();
381
382 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400383 std::shared_ptr<skiagm::GM> gm{(*factory)(nullptr)};
Mike Kleina833cff2019-04-09 11:52:40 -0500384 source->name = name;
385 init(source, gm);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400386 continue;
387 }
388
Mike Kleina833cff2019-04-09 11:52:40 -0500389 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
390 source->name = SkOSPath::Basename(name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500391
Mike Kleince90d6f2019-03-29 11:14:14 -0500392 if (name.endsWith(".skp")) {
393 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500394 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400395 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500396 }
397 } else if (name.endsWith(".svg")) {
398 SkMemoryStream stream{blob};
399 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500400 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400401 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500402 }
Brian Osmanca946562019-04-10 13:51:53 -0400403 }
404#if defined(SK_ENABLE_SKOTTIE)
405 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500406 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400407 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
408 .setResourceProvider(skottie_utils::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400409 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500410 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400411 continue;
Mike Klein22924262019-04-02 14:14:56 -0400412 }
Brian Osmanca946562019-04-10 13:51:53 -0400413 }
414#endif
415 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500416 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400417 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500418 }
419 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400420
Mike Kleina833cff2019-04-09 11:52:40 -0500421 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400422 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500423 }
424
Mike Klein9b462092019-03-27 13:52:35 -0500425 enum NonGpuBackends {
426 kCPU_Backend = -1,
427 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500428 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500429 };
430 const FlagOption<int> kBackends[] = {
431 { "cpu" , kCPU_Backend },
432 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500433 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500434 { "gl" , GrContextFactory::kGL_ContextType },
435 { "gles" , GrContextFactory::kGLES_ContextType },
436 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
437 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
438 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
439 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
440 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
441 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
442 { "vk" , GrContextFactory::kVulkan_ContextType },
443 { "mtl" , GrContextFactory::kMetal_ContextType },
444 { "mock" , GrContextFactory::kMock_ContextType },
445 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500446 const FlagOption<SkColorType> kColorTypes[] = {
447 { "a8", kAlpha_8_SkColorType },
448 { "g8", kGray_8_SkColorType },
449 { "565", kRGB_565_SkColorType },
450 { "4444", kARGB_4444_SkColorType },
451 { "8888", kN32_SkColorType },
452 { "888x", kRGB_888x_SkColorType },
453 { "1010102", kRGBA_1010102_SkColorType },
454 { "101010x", kRGB_101010x_SkColorType },
455 { "f16norm", kRGBA_F16Norm_SkColorType },
456 { "f16", kRGBA_F16_SkColorType },
457 { "f32", kRGBA_F32_SkColorType },
458 { "rgba", kRGBA_8888_SkColorType },
459 { "bgra", kBGRA_8888_SkColorType },
460 };
461 const FlagOption<SkAlphaType> kAlphaTypes[] = {
462 { "premul", kPremul_SkAlphaType },
463 { "unpremul", kUnpremul_SkAlphaType },
464 };
465 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
466 { "srgb", SkNamedGamut::kSRGB },
467 { "p3", SkNamedGamut::kDCIP3 },
468 { "rec2020", SkNamedGamut::kRec2020 },
469 { "adobe", SkNamedGamut::kAdobeRGB },
470 { "narrow", gNarrow_toXYZD50},
471 };
472 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
473 { "srgb" , SkNamedTransferFn::kSRGB },
Hal Canarybe67a172019-05-24 10:13:36 -0400474 { "rec2020", SkNamedTransferFn::kRec2020 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500475 { "2.2" , SkNamedTransferFn::k2Dot2 },
476 { "linear" , SkNamedTransferFn::kLinear },
477 };
478
Mike Klein735c7ba2019-03-25 12:57:58 -0500479
Mike Klein9b462092019-03-27 13:52:35 -0500480 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500481 SkColorType ct;
482 SkAlphaType at;
483 skcms_Matrix3x3 gamut;
484 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500485
Mike Klein9b462092019-03-27 13:52:35 -0500486 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
487 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500488 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
489 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500490 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500491 return 1;
492 }
493
Mike Klein4daf6372019-04-03 11:19:45 -0400494 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
495 : SkColorSpace::MakeRGB(tf,gamut);
496 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500497
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400498 AutoreleasePool pool;
Mike Klein735c7ba2019-03-25 12:57:58 -0500499 for (auto source : sources) {
500 const auto start = std::chrono::steady_clock::now();
Mike Klein832d3c52019-04-02 15:23:46 -0400501 fprintf(stdout, "%50s", source.name.c_str());
Mike Kleinbf15b662019-04-15 11:32:16 -0500502 fflush(stdout);
Mike Klein735c7ba2019-03-25 12:57:58 -0500503
504 const SkImageInfo info = unsized_info.makeWH(source.size.width(),
505 source.size.height());
506
Mike Kleina833cff2019-04-09 11:52:40 -0500507 auto draw = [&source](SkCanvas* canvas) {
508 Result result = source.draw(canvas);
509 switch (result.status) {
510 case Result::Ok: break;
511 case Result::Skip: return false;
512 case Result::Fail:
Mike Kleinbf15b662019-04-15 11:32:16 -0500513 SK_ABORT(result.failure.c_str());
Mike Kleina833cff2019-04-09 11:52:40 -0500514 }
515 return true;
516 };
517
Mike Klein735c7ba2019-03-25 12:57:58 -0500518 GrContextOptions options = baseOptions;
519 source.tweak(&options);
520 GrContextFactory factory(options); // N.B. factory must outlive image
521
522 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500523 sk_sp<SkData> blob;
524 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500525 switch (backend) {
526 case kCPU_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500527 image = draw_with_cpu(draw, info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500528 break;
Mike Klein9b462092019-03-27 13:52:35 -0500529 case kSKP_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500530 blob = draw_as_skp(draw, info);
Mike Klein9b462092019-03-27 13:52:35 -0500531 ext = ".skp";
532 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500533 case kPDF_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500534 blob = draw_as_pdf(draw, info, source.name);
Mike Kleinc245bd92019-03-27 14:31:09 -0500535 ext = ".pdf";
536 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500537 default:
Mike Kleina833cff2019-04-09 11:52:40 -0500538 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500539 break;
540 }
541
Mike Klein9b462092019-03-27 13:52:35 -0500542 if (!image && !blob) {
Mike Klein832d3c52019-04-02 15:23:46 -0400543 fprintf(stdout, "\tskipped\n");
Mike Kleina5c27172019-04-02 10:26:48 -0400544 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500545 }
546
547 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500548 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500549 SK_ABORT("SkImage::asLegacyBitmap() failed.");
Mike Klein735c7ba2019-03-25 12:57:58 -0500550 }
551
552 HashAndEncode hashAndEncode{bitmap};
553 SkString md5;
554 {
555 SkMD5 hash;
Mike Klein9b462092019-03-27 13:52:35 -0500556 if (image) {
557 hashAndEncode.write(&hash);
558 } else {
559 hash.write(blob->data(), blob->size());
560 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500561
Hal Canary0f2f5222019-04-03 10:13:45 -0400562 SkMD5::Digest digest = hash.finish();
Mike Klein735c7ba2019-03-25 12:57:58 -0500563 for (int i = 0; i < 16; i++) {
564 md5.appendf("%02x", digest.data[i]);
565 }
566 }
567
568 if (!FLAGS_writePath.isEmpty()) {
569 sk_mkdir(FLAGS_writePath[0]);
Mike Klein9b462092019-03-27 13:52:35 -0500570 SkString path = SkStringPrintf("%s/%s%s", FLAGS_writePath[0], source.name.c_str(), ext);
Mike Klein735c7ba2019-03-25 12:57:58 -0500571
Mike Klein9b462092019-03-27 13:52:35 -0500572 if (image) {
573 if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(),
Mike Klein7b8bc532019-04-09 11:19:47 -0500574 FLAGS_key, FLAGS_properties)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500575 SK_ABORT("Could not write .png.");
Mike Klein9b462092019-03-27 13:52:35 -0500576 }
577 } else {
578 SkFILEWStream file(path.c_str());
579 file.write(blob->data(), blob->size());
Mike Klein735c7ba2019-03-25 12:57:58 -0500580 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500581 }
582
Mike Klein832d3c52019-04-02 15:23:46 -0400583 const auto elapsed = std::chrono::steady_clock::now() - start;
584 fprintf(stdout, "\t%s\t%7dms\n",
585 md5.c_str(),
586 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400587 pool.drain();
Mike Klein735c7ba2019-03-25 12:57:58 -0500588 }
589
Brian Osman5aa11fb2019-04-08 16:40:36 -0400590 if (!FLAGS_writeShaders.isEmpty()) {
591 sk_mkdir(FLAGS_writeShaders[0]);
592 GrBackendApi api =
593 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
594 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
595
596 }
597
Mike Klein735c7ba2019-03-25 12:57:58 -0500598 return 0;
599}