blob: d7ae53b48db5551ca3110e6a9d9d21d194b71b3f [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,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400288 GrRenderable::kYes,
289 GrProtected::kNo);
Mike Klein735c7ba2019-03-25 12:57:58 -0500290 surface = SkSurface::MakeFromBackendTexture(context,
291 backendTexture,
292 kTopLeft_GrSurfaceOrigin,
293 FLAGS_samples,
294 info.colorType(),
295 info.refColorSpace(),
296 &props);
297 break;
298
299 case SurfaceType::kBackendRenderTarget:
300 backendRT = context->priv().getGpu()
301 ->createTestingOnlyBackendRenderTarget(info.width(),
302 info.height(),
303 SkColorTypeToGrColorType(info.colorType()));
304 surface = SkSurface::MakeFromBackendRenderTarget(context,
305 backendRT,
306 kBottomLeft_GrSurfaceOrigin,
307 info.colorType(),
308 info.refColorSpace(),
309 &props);
310 break;
311 }
312
313 if (!surface) {
314 fprintf(stderr, "Could not create GPU surface.\n");
315 return nullptr;
316 }
317
318 if (FLAGS_preAbandonGpuContext) {
319 factory->abandonContexts();
320 }
321
Mike Kleina5c27172019-04-02 10:26:48 -0400322 sk_sp<SkImage> image;
323 if (draw(surface->getCanvas())) {
324 image = surface->makeImageSnapshot();
325 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500326
327 if (FLAGS_abandonGpuContext) {
328 factory->abandonContexts();
329 } else if (FLAGS_releaseAndAbandonGpuContext) {
330 factory->releaseResourcesAndAbandonContexts();
331 }
332
333 if (!context->abandoned()) {
334 surface.reset();
335 if (backendTexture.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400336 context->deleteBackendTexture(backendTexture);
Mike Klein735c7ba2019-03-25 12:57:58 -0500337 }
338 if (backendRT.isValid()) {
339 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
340 }
341 }
342
343 return image;
344}
345
346int main(int argc, char** argv) {
347 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500348 SetupCrashHandler();
Mike Klein735c7ba2019-03-25 12:57:58 -0500349
350 if (FLAGS_cpuDetect) {
351 SkGraphics::Init();
352 }
353 initializeEventTracingForTools();
354 ToolUtils::SetDefaultFontMgr();
355 SetAnalyticAAFromCommonFlags();
356
357 GrContextOptions baseOptions;
358 SetCtxOptionsFromCommonFlags(&baseOptions);
359
Brian Osman5aa11fb2019-04-08 16:40:36 -0400360 sk_gpu_test::MemoryCache memoryCache;
361 if (!FLAGS_writeShaders.isEmpty()) {
362 baseOptions.fPersistentCache = &memoryCache;
363 baseOptions.fDisallowGLSLBinaryCaching = true;
364 }
365
Mike Kleincbe93ee2019-04-02 17:00:54 -0400366 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500367 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400368 std::unique_ptr<skiagm::GM> gm{factory(nullptr)};
Mike Klein735c7ba2019-03-25 12:57:58 -0500369 if (FLAGS_sources.isEmpty()) {
370 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400371 } else {
372 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500373 }
374 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400375 if (FLAGS_sources.isEmpty()) {
376 return 0;
377 }
378
379 SkTArray<Source> sources;
Mike Kleina833cff2019-04-09 11:52:40 -0500380 for (const SkString& name : FLAGS_sources) {
381 Source* source = &sources.push_back();
382
383 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400384 std::shared_ptr<skiagm::GM> gm{(*factory)(nullptr)};
Mike Kleina833cff2019-04-09 11:52:40 -0500385 source->name = name;
386 init(source, gm);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400387 continue;
388 }
389
Mike Kleina833cff2019-04-09 11:52:40 -0500390 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
391 source->name = SkOSPath::Basename(name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500392
Mike Kleince90d6f2019-03-29 11:14:14 -0500393 if (name.endsWith(".skp")) {
394 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500395 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400396 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500397 }
398 } else if (name.endsWith(".svg")) {
399 SkMemoryStream stream{blob};
400 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500401 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400402 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500403 }
Brian Osmanca946562019-04-10 13:51:53 -0400404 }
405#if defined(SK_ENABLE_SKOTTIE)
406 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500407 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400408 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
409 .setResourceProvider(skottie_utils::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400410 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500411 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400412 continue;
Mike Klein22924262019-04-02 14:14:56 -0400413 }
Brian Osmanca946562019-04-10 13:51:53 -0400414 }
415#endif
416 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500417 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400418 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500419 }
420 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400421
Mike Kleina833cff2019-04-09 11:52:40 -0500422 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400423 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500424 }
425
Mike Klein9b462092019-03-27 13:52:35 -0500426 enum NonGpuBackends {
427 kCPU_Backend = -1,
428 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500429 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500430 };
431 const FlagOption<int> kBackends[] = {
432 { "cpu" , kCPU_Backend },
433 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500434 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500435 { "gl" , GrContextFactory::kGL_ContextType },
436 { "gles" , GrContextFactory::kGLES_ContextType },
437 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
438 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
439 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
440 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
441 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
442 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
443 { "vk" , GrContextFactory::kVulkan_ContextType },
444 { "mtl" , GrContextFactory::kMetal_ContextType },
445 { "mock" , GrContextFactory::kMock_ContextType },
446 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500447 const FlagOption<SkColorType> kColorTypes[] = {
448 { "a8", kAlpha_8_SkColorType },
449 { "g8", kGray_8_SkColorType },
450 { "565", kRGB_565_SkColorType },
451 { "4444", kARGB_4444_SkColorType },
452 { "8888", kN32_SkColorType },
453 { "888x", kRGB_888x_SkColorType },
454 { "1010102", kRGBA_1010102_SkColorType },
455 { "101010x", kRGB_101010x_SkColorType },
456 { "f16norm", kRGBA_F16Norm_SkColorType },
457 { "f16", kRGBA_F16_SkColorType },
458 { "f32", kRGBA_F32_SkColorType },
459 { "rgba", kRGBA_8888_SkColorType },
460 { "bgra", kBGRA_8888_SkColorType },
461 };
462 const FlagOption<SkAlphaType> kAlphaTypes[] = {
463 { "premul", kPremul_SkAlphaType },
464 { "unpremul", kUnpremul_SkAlphaType },
465 };
466 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
467 { "srgb", SkNamedGamut::kSRGB },
468 { "p3", SkNamedGamut::kDCIP3 },
469 { "rec2020", SkNamedGamut::kRec2020 },
470 { "adobe", SkNamedGamut::kAdobeRGB },
471 { "narrow", gNarrow_toXYZD50},
472 };
473 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
474 { "srgb" , SkNamedTransferFn::kSRGB },
Hal Canarybe67a172019-05-24 10:13:36 -0400475 { "rec2020", SkNamedTransferFn::kRec2020 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500476 { "2.2" , SkNamedTransferFn::k2Dot2 },
477 { "linear" , SkNamedTransferFn::kLinear },
478 };
479
Mike Klein735c7ba2019-03-25 12:57:58 -0500480
Mike Klein9b462092019-03-27 13:52:35 -0500481 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500482 SkColorType ct;
483 SkAlphaType at;
484 skcms_Matrix3x3 gamut;
485 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500486
Mike Klein9b462092019-03-27 13:52:35 -0500487 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
488 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500489 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
490 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500491 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500492 return 1;
493 }
494
Mike Klein4daf6372019-04-03 11:19:45 -0400495 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
496 : SkColorSpace::MakeRGB(tf,gamut);
497 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500498
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400499 AutoreleasePool pool;
Mike Klein735c7ba2019-03-25 12:57:58 -0500500 for (auto source : sources) {
501 const auto start = std::chrono::steady_clock::now();
Mike Klein832d3c52019-04-02 15:23:46 -0400502 fprintf(stdout, "%50s", source.name.c_str());
Mike Kleinbf15b662019-04-15 11:32:16 -0500503 fflush(stdout);
Mike Klein735c7ba2019-03-25 12:57:58 -0500504
505 const SkImageInfo info = unsized_info.makeWH(source.size.width(),
506 source.size.height());
507
Mike Kleina833cff2019-04-09 11:52:40 -0500508 auto draw = [&source](SkCanvas* canvas) {
509 Result result = source.draw(canvas);
510 switch (result.status) {
511 case Result::Ok: break;
512 case Result::Skip: return false;
513 case Result::Fail:
Mike Kleinbf15b662019-04-15 11:32:16 -0500514 SK_ABORT(result.failure.c_str());
Mike Kleina833cff2019-04-09 11:52:40 -0500515 }
516 return true;
517 };
518
Mike Klein735c7ba2019-03-25 12:57:58 -0500519 GrContextOptions options = baseOptions;
520 source.tweak(&options);
521 GrContextFactory factory(options); // N.B. factory must outlive image
522
523 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500524 sk_sp<SkData> blob;
525 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500526 switch (backend) {
527 case kCPU_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500528 image = draw_with_cpu(draw, info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500529 break;
Mike Klein9b462092019-03-27 13:52:35 -0500530 case kSKP_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500531 blob = draw_as_skp(draw, info);
Mike Klein9b462092019-03-27 13:52:35 -0500532 ext = ".skp";
533 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500534 case kPDF_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500535 blob = draw_as_pdf(draw, info, source.name);
Mike Kleinc245bd92019-03-27 14:31:09 -0500536 ext = ".pdf";
537 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500538 default:
Mike Kleina833cff2019-04-09 11:52:40 -0500539 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500540 break;
541 }
542
Mike Klein9b462092019-03-27 13:52:35 -0500543 if (!image && !blob) {
Mike Klein832d3c52019-04-02 15:23:46 -0400544 fprintf(stdout, "\tskipped\n");
Mike Kleina5c27172019-04-02 10:26:48 -0400545 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500546 }
547
548 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500549 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500550 SK_ABORT("SkImage::asLegacyBitmap() failed.");
Mike Klein735c7ba2019-03-25 12:57:58 -0500551 }
552
553 HashAndEncode hashAndEncode{bitmap};
554 SkString md5;
555 {
556 SkMD5 hash;
Mike Klein9b462092019-03-27 13:52:35 -0500557 if (image) {
558 hashAndEncode.write(&hash);
559 } else {
560 hash.write(blob->data(), blob->size());
561 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500562
Hal Canary0f2f5222019-04-03 10:13:45 -0400563 SkMD5::Digest digest = hash.finish();
Mike Klein735c7ba2019-03-25 12:57:58 -0500564 for (int i = 0; i < 16; i++) {
565 md5.appendf("%02x", digest.data[i]);
566 }
567 }
568
569 if (!FLAGS_writePath.isEmpty()) {
570 sk_mkdir(FLAGS_writePath[0]);
Mike Klein9b462092019-03-27 13:52:35 -0500571 SkString path = SkStringPrintf("%s/%s%s", FLAGS_writePath[0], source.name.c_str(), ext);
Mike Klein735c7ba2019-03-25 12:57:58 -0500572
Mike Klein9b462092019-03-27 13:52:35 -0500573 if (image) {
574 if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(),
Mike Klein7b8bc532019-04-09 11:19:47 -0500575 FLAGS_key, FLAGS_properties)) {
Mike Kleinbf15b662019-04-15 11:32:16 -0500576 SK_ABORT("Could not write .png.");
Mike Klein9b462092019-03-27 13:52:35 -0500577 }
578 } else {
579 SkFILEWStream file(path.c_str());
580 file.write(blob->data(), blob->size());
Mike Klein735c7ba2019-03-25 12:57:58 -0500581 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500582 }
583
Mike Klein832d3c52019-04-02 15:23:46 -0400584 const auto elapsed = std::chrono::steady_clock::now() - start;
585 fprintf(stdout, "\t%s\t%7dms\n",
586 md5.c_str(),
587 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Jim Van Verth8a9a3712019-05-31 10:49:12 -0400588 pool.drain();
Mike Klein735c7ba2019-03-25 12:57:58 -0500589 }
590
Brian Osman5aa11fb2019-04-08 16:40:36 -0400591 if (!FLAGS_writeShaders.isEmpty()) {
592 sk_mkdir(FLAGS_writeShaders[0]);
593 GrBackendApi api =
594 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
595 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
596
597 }
598
Mike Klein735c7ba2019-03-25 12:57:58 -0500599 return 0;
600}