blob: 67e2e6e8cd42272b525a038f2937bfbb63fca8cf [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
4#include "CommandLineFlags.h"
5#include "CommonFlags.h"
Mike Kleinbf15b662019-04-15 11:32:16 -05006#include "CrashHandler.h"
Mike Klein735c7ba2019-03-25 12:57:58 -05007#include "EventTracingPriv.h"
8#include "GrContextFactory.h"
9#include "GrContextOptions.h"
10#include "GrContextPriv.h"
11#include "GrGpu.h"
Brian Osman5aa11fb2019-04-08 16:40:36 -040012#include "GrPersistentCacheUtils.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050013#include "HashAndEncode.h"
Brian Osman5aa11fb2019-04-08 16:40:36 -040014#include "MemoryCache.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050015#include "SkCodec.h"
16#include "SkColorSpace.h"
17#include "SkColorSpacePriv.h"
18#include "SkGraphics.h"
19#include "SkMD5.h"
20#include "SkOSFile.h"
21#include "SkOSPath.h"
Mike Kleinc245bd92019-03-27 14:31:09 -050022#include "SkPDFDocument.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050023#include "SkPicture.h"
Mike Klein9b462092019-03-27 13:52:35 -050024#include "SkPictureRecorder.h"
Mike Klein6253d902019-03-27 15:09:12 -050025#include "SkSVGDOM.h"
Mike Kleincbe93ee2019-04-02 17:00:54 -040026#include "SkTHash.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050027#include "ToolUtils.h"
28#include "gm.h"
29#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)
35 #include "Skottie.h"
36 #include "SkottieUtils.h"
37#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:
284 backendTexture = context->priv().getGpu()
285 ->createTestingOnlyBackendTexture(nullptr,
286 info.width(),
287 info.height(),
288 info.colorType(),
289 true,
290 GrMipMapped::kNo);
291 surface = SkSurface::MakeFromBackendTexture(context,
292 backendTexture,
293 kTopLeft_GrSurfaceOrigin,
294 FLAGS_samples,
295 info.colorType(),
296 info.refColorSpace(),
297 &props);
298 break;
299
300 case SurfaceType::kBackendRenderTarget:
301 backendRT = context->priv().getGpu()
302 ->createTestingOnlyBackendRenderTarget(info.width(),
303 info.height(),
304 SkColorTypeToGrColorType(info.colorType()));
305 surface = SkSurface::MakeFromBackendRenderTarget(context,
306 backendRT,
307 kBottomLeft_GrSurfaceOrigin,
308 info.colorType(),
309 info.refColorSpace(),
310 &props);
311 break;
312 }
313
314 if (!surface) {
315 fprintf(stderr, "Could not create GPU surface.\n");
316 return nullptr;
317 }
318
319 if (FLAGS_preAbandonGpuContext) {
320 factory->abandonContexts();
321 }
322
Mike Kleina5c27172019-04-02 10:26:48 -0400323 sk_sp<SkImage> image;
324 if (draw(surface->getCanvas())) {
325 image = surface->makeImageSnapshot();
326 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500327
328 if (FLAGS_abandonGpuContext) {
329 factory->abandonContexts();
330 } else if (FLAGS_releaseAndAbandonGpuContext) {
331 factory->releaseResourcesAndAbandonContexts();
332 }
333
334 if (!context->abandoned()) {
335 surface.reset();
336 if (backendTexture.isValid()) {
337 context->priv().getGpu()->deleteTestingOnlyBackendTexture(backendTexture);
338 }
339 if (backendRT.isValid()) {
340 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
341 }
342 }
343
344 return image;
345}
346
347int main(int argc, char** argv) {
348 CommandLineFlags::Parse(argc, argv);
Mike Kleinbf15b662019-04-15 11:32:16 -0500349 SetupCrashHandler();
Mike Klein735c7ba2019-03-25 12:57:58 -0500350
351 if (FLAGS_cpuDetect) {
352 SkGraphics::Init();
353 }
354 initializeEventTracingForTools();
355 ToolUtils::SetDefaultFontMgr();
356 SetAnalyticAAFromCommonFlags();
357
358 GrContextOptions baseOptions;
359 SetCtxOptionsFromCommonFlags(&baseOptions);
360
Brian Osman5aa11fb2019-04-08 16:40:36 -0400361 sk_gpu_test::MemoryCache memoryCache;
362 if (!FLAGS_writeShaders.isEmpty()) {
363 baseOptions.fPersistentCache = &memoryCache;
364 baseOptions.fDisallowGLSLBinaryCaching = true;
365 }
366
Mike Kleincbe93ee2019-04-02 17:00:54 -0400367 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500368 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400369 std::unique_ptr<skiagm::GM> gm{factory(nullptr)};
Mike Klein735c7ba2019-03-25 12:57:58 -0500370 if (FLAGS_sources.isEmpty()) {
371 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400372 } else {
373 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500374 }
375 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400376 if (FLAGS_sources.isEmpty()) {
377 return 0;
378 }
379
380 SkTArray<Source> sources;
Mike Kleina833cff2019-04-09 11:52:40 -0500381 for (const SkString& name : FLAGS_sources) {
382 Source* source = &sources.push_back();
383
384 if (skiagm::GMFactory* factory = gm_factories.find(name)) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400385 std::shared_ptr<skiagm::GM> gm{(*factory)(nullptr)};
Mike Kleina833cff2019-04-09 11:52:40 -0500386 source->name = name;
387 init(source, gm);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400388 continue;
389 }
390
Mike Kleina833cff2019-04-09 11:52:40 -0500391 if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) {
392 source->name = SkOSPath::Basename(name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500393
Mike Kleince90d6f2019-03-29 11:14:14 -0500394 if (name.endsWith(".skp")) {
395 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500396 init(source, pic);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400397 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500398 }
399 } else if (name.endsWith(".svg")) {
400 SkMemoryStream stream{blob};
401 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500402 init(source, svg);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400403 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500404 }
Brian Osmanca946562019-04-10 13:51:53 -0400405 }
406#if defined(SK_ENABLE_SKOTTIE)
407 else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500408 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400409 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
410 .setResourceProvider(skottie_utils::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400411 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500412 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400413 continue;
Mike Klein22924262019-04-02 14:14:56 -0400414 }
Brian Osmanca946562019-04-10 13:51:53 -0400415 }
416#endif
417 else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500418 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400419 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500420 }
421 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400422
Mike Kleina833cff2019-04-09 11:52:40 -0500423 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400424 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500425 }
426
Mike Klein9b462092019-03-27 13:52:35 -0500427 enum NonGpuBackends {
428 kCPU_Backend = -1,
429 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500430 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500431 };
432 const FlagOption<int> kBackends[] = {
433 { "cpu" , kCPU_Backend },
434 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500435 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500436 { "gl" , GrContextFactory::kGL_ContextType },
437 { "gles" , GrContextFactory::kGLES_ContextType },
438 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
439 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
440 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
441 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
442 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
443 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
444 { "vk" , GrContextFactory::kVulkan_ContextType },
445 { "mtl" , GrContextFactory::kMetal_ContextType },
446 { "mock" , GrContextFactory::kMock_ContextType },
447 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500448 const FlagOption<SkColorType> kColorTypes[] = {
449 { "a8", kAlpha_8_SkColorType },
450 { "g8", kGray_8_SkColorType },
451 { "565", kRGB_565_SkColorType },
452 { "4444", kARGB_4444_SkColorType },
453 { "8888", kN32_SkColorType },
454 { "888x", kRGB_888x_SkColorType },
455 { "1010102", kRGBA_1010102_SkColorType },
456 { "101010x", kRGB_101010x_SkColorType },
457 { "f16norm", kRGBA_F16Norm_SkColorType },
458 { "f16", kRGBA_F16_SkColorType },
459 { "f32", kRGBA_F32_SkColorType },
460 { "rgba", kRGBA_8888_SkColorType },
461 { "bgra", kBGRA_8888_SkColorType },
462 };
463 const FlagOption<SkAlphaType> kAlphaTypes[] = {
464 { "premul", kPremul_SkAlphaType },
465 { "unpremul", kUnpremul_SkAlphaType },
466 };
467 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
468 { "srgb", SkNamedGamut::kSRGB },
469 { "p3", SkNamedGamut::kDCIP3 },
470 { "rec2020", SkNamedGamut::kRec2020 },
471 { "adobe", SkNamedGamut::kAdobeRGB },
472 { "narrow", gNarrow_toXYZD50},
473 };
474 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
475 { "srgb" , SkNamedTransferFn::kSRGB },
476 { "rec2020", {2.22222f, 0.909672f, 0.0903276f, 0.222222f, 0.0812429f, 0, 0} },
477 { "2.2" , SkNamedTransferFn::k2Dot2 },
478 { "linear" , SkNamedTransferFn::kLinear },
479 };
480
Mike Klein735c7ba2019-03-25 12:57:58 -0500481
Mike Klein9b462092019-03-27 13:52:35 -0500482 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500483 SkColorType ct;
484 SkAlphaType at;
485 skcms_Matrix3x3 gamut;
486 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500487
Mike Klein9b462092019-03-27 13:52:35 -0500488 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
489 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500490 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
491 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500492 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500493 return 1;
494 }
495
Mike Klein4daf6372019-04-03 11:19:45 -0400496 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
497 : SkColorSpace::MakeRGB(tf,gamut);
498 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500499
500 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());
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}