blob: 4ad1b8f6116dfa4ab1e3269f3885995c80ff1bf9 [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"
6#include "EventTracingPriv.h"
7#include "GrContextFactory.h"
8#include "GrContextOptions.h"
9#include "GrContextPriv.h"
10#include "GrGpu.h"
Brian Osman5aa11fb2019-04-08 16:40:36 -040011#include "GrPersistentCacheUtils.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050012#include "HashAndEncode.h"
Brian Osman5aa11fb2019-04-08 16:40:36 -040013#include "MemoryCache.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050014#include "SkCodec.h"
15#include "SkColorSpace.h"
16#include "SkColorSpacePriv.h"
17#include "SkGraphics.h"
18#include "SkMD5.h"
19#include "SkOSFile.h"
20#include "SkOSPath.h"
Mike Kleinc245bd92019-03-27 14:31:09 -050021#include "SkPDFDocument.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050022#include "SkPicture.h"
Mike Klein9b462092019-03-27 13:52:35 -050023#include "SkPictureRecorder.h"
Mike Klein6253d902019-03-27 15:09:12 -050024#include "SkSVGDOM.h"
Mike Kleincbe93ee2019-04-02 17:00:54 -040025#include "SkTHash.h"
Mike Klein22924262019-04-02 14:14:56 -040026#include "Skottie.h"
27#include "SkottieUtils.h"
Mike Klein735c7ba2019-03-25 12:57:58 -050028#include "ToolUtils.h"
29#include "gm.h"
30#include <chrono>
31#include <functional>
32#include <stdio.h>
33#include <stdlib.h>
34
35using sk_gpu_test::GrContextFactory;
36
37static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw.");
38static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into.");
39
Mike Klein4daf6372019-04-03 11:19:45 -040040static DEFINE_string(ct , "8888", "The color type for any raster backend.");
41static DEFINE_string(at , "premul", "The alpha type for any raster backend.");
42static DEFINE_string(gamut , "srgb", "The color gamut for any raster backend.");
43static DEFINE_string(tf , "srgb", "The transfer function for any raster backend.");
44static DEFINE_bool (legacy, false, "Use a null SkColorSpace instead of --gamut and --tf?");
Mike Klein735c7ba2019-03-25 12:57:58 -050045
46static DEFINE_int (samples , 0, "Samples per pixel in GPU backends.");
47static DEFINE_bool (nvpr , false, "Use NV_path_rendering in GPU backends?");
48static DEFINE_bool (stencils, true, "If false, avoid stencil buffers in GPU backends.");
49static DEFINE_bool (dit , false, "Use device-independent text in GPU backends.");
50static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces.");
51
52static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing.");
53static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing.");
54static DEFINE_bool(releaseAndAbandonGpuContext, false,
55 "Release all GPU resources and abandon the GrContext after drawing.");
56
57static DEFINE_bool(decodeToDst, false,
58 "Decode images to destination format rather than suggested natural format.");
59
Mike Kleinc245bd92019-03-27 14:31:09 -050060static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI,
61 "DPI for rasterized content in vector backends like --backend pdf.");
62static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?");
63
Mike Klein735c7ba2019-03-25 12:57:58 -050064static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?");
65static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set.");
Mike Klein735c7ba2019-03-25 12:57:58 -050066
Brian Osman5aa11fb2019-04-08 16:40:36 -040067static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set.");
68
Mike Klein735c7ba2019-03-25 12:57:58 -050069static DEFINE_string(key, "", "Metadata passed through to .png encoder and .json output.");
70static DEFINE_string(parameters, "", "Metadata passed through to .png encoder and .json output.");
71
72template <typename T>
73struct FlagOption {
74 const char* label;
75 T value;
76};
77
78template <typename T, int N>
79static bool parse_flag(const CommandLineFlags::StringArray& flag,
80 const char* flag_name,
81 const FlagOption<T> (&array)[N],
82 T* value) {
83 for (auto entry : array) {
84 if (flag.contains(entry.label)) {
85 *value = entry.value;
86 return true;
87 }
88 }
89 fprintf(stderr, "Known values for --%s:\n", flag_name);
90 for (auto entry : array) {
91 fprintf(stderr, " --%s %s\n", flag_name, entry.label);
92 }
93 return false;
94}
95
96static void exit_with_failure() {
97 // TODO: dump stack trace, debug trap, print currently running job, etc?
98 exit(1);
99}
100
101struct Source {
102 SkString name;
103 SkISize size;
Mike Klein735c7ba2019-03-25 12:57:58 -0500104 std::function<void(GrContextOptions*)> tweak;
Mike Klein22924262019-04-02 14:14:56 -0400105 std::function<bool(SkCanvas*)> draw; // true -> ok, false -> skip;
106 // failures should exit_with_failure()
Mike Klein735c7ba2019-03-25 12:57:58 -0500107};
108
109static Source gm_source(std::shared_ptr<skiagm::GM> gm) {
110 return {
111 SkString{gm->getName()},
112 gm->getISize(),
Mike Klein22924262019-04-02 14:14:56 -0400113 [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); },
Mike Klein735c7ba2019-03-25 12:57:58 -0500114 [gm](SkCanvas* canvas) {
115 SkString err;
Mike Kleina5c27172019-04-02 10:26:48 -0400116 switch (gm->draw(canvas, &err)) {
117 case skiagm::DrawResult::kOk: return true;
118 case skiagm::DrawResult::kSkip: break;
119 case skiagm::DrawResult::kFail:
120 fprintf(stderr, "Drawing GM %s failed: %s\n", gm->getName(), err.c_str());
121 exit_with_failure();
Mike Klein735c7ba2019-03-25 12:57:58 -0500122 }
Mike Kleina5c27172019-04-02 10:26:48 -0400123 return false;
Mike Klein735c7ba2019-03-25 12:57:58 -0500124 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500125 };
126}
127
128static Source picture_source(SkString name, sk_sp<SkPicture> pic) {
129 return {
130 name,
131 pic->cullRect().roundOut().size(),
Mike Klein22924262019-04-02 14:14:56 -0400132 [](GrContextOptions*) {},
Mike Klein735c7ba2019-03-25 12:57:58 -0500133 [pic](SkCanvas* canvas) {
134 canvas->drawPicture(pic);
Mike Kleina5c27172019-04-02 10:26:48 -0400135 return true;
Mike Klein735c7ba2019-03-25 12:57:58 -0500136 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500137 };
138}
139
140static Source codec_source(SkString name, std::shared_ptr<SkCodec> codec) {
141 return {
142 name,
143 codec->dimensions(),
Mike Klein22924262019-04-02 14:14:56 -0400144 [](GrContextOptions*) {},
Mike Klein735c7ba2019-03-25 12:57:58 -0500145 [codec](SkCanvas* canvas) {
146 SkImageInfo info = codec->getInfo();
147 if (FLAGS_decodeToDst) {
148 info = canvas->imageInfo().makeWH(info.width(),
149 info.height());
150 }
151
152 SkBitmap bm;
153 bm.allocPixels(info);
154
155 switch (auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) {
156 case SkCodec::kSuccess:
157 case SkCodec::kErrorInInput:
158 case SkCodec::kIncompleteInput:
159 canvas->drawBitmap(bm, 0,0);
Mike Kleina5c27172019-04-02 10:26:48 -0400160 return true;
Mike Klein735c7ba2019-03-25 12:57:58 -0500161 default:
162 fprintf(stderr, "SkCodec::getPixels failed: %d.", result);
163 exit_with_failure();
164 }
Mike Kleina5c27172019-04-02 10:26:48 -0400165 return false;
Mike Klein735c7ba2019-03-25 12:57:58 -0500166 },
Mike Klein735c7ba2019-03-25 12:57:58 -0500167 };
168}
169
Mike Klein6253d902019-03-27 15:09:12 -0500170static Source svg_source(SkString name, sk_sp<SkSVGDOM> svg) {
171 return {
172 name,
173 svg->containerSize().isEmpty() ? SkISize{1000,1000}
174 : svg->containerSize().toCeil(),
Mike Klein22924262019-04-02 14:14:56 -0400175 [](GrContextOptions*) {},
Mike Kleina5c27172019-04-02 10:26:48 -0400176 [svg](SkCanvas* canvas) {
177 svg->render(canvas);
178 return true;
179 },
Mike Klein22924262019-04-02 14:14:56 -0400180 };
181}
182
183static Source skottie_source(SkString name, sk_sp<skottie::Animation> animation) {
184 return {
185 name,
186 {1000,1000},
Mike Klein6253d902019-03-27 15:09:12 -0500187 [](GrContextOptions*) {},
Mike Klein22924262019-04-02 14:14:56 -0400188 [animation](SkCanvas* canvas) {
189 canvas->clear(SK_ColorWHITE);
190
191 // Draw frames in a shuffled order to exercise nonlinear frame progression.
192 // The film strip will still be in time order, just drawn out of order.
193 const int order[] = { 4, 0, 3, 1, 2 };
194 const int tiles = SK_ARRAY_COUNT(order);
195 const float dim = 1000.0f / tiles;
196
197 const float dt = 1.0f / (tiles*tiles - 1);
198
199 for (int y : order)
200 for (int x : order) {
201 SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim};
202
203 SkAutoCanvasRestore _(canvas, true/*save now*/);
204 canvas->clipRect(dst, /*aa=*/true);
205 canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()),
206 dst,
207 SkMatrix::kCenter_ScaleToFit));
208 float t = (y*tiles + x) * dt;
209 animation->seek(t);
210 animation->render(canvas);
211 }
212 return true;
213 },
Mike Klein6253d902019-03-27 15:09:12 -0500214 };
215}
216
217
Mike Kleina5c27172019-04-02 10:26:48 -0400218static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500219 SkImageInfo info) {
220 if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400221 if (draw(surface->getCanvas())) {
222 return surface->makeImageSnapshot();
223 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500224 }
225 return nullptr;
226}
227
Mike Kleina5c27172019-04-02 10:26:48 -0400228static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw,
Mike Klein9b462092019-03-27 13:52:35 -0500229 SkImageInfo info) {
230 SkPictureRecorder recorder;
Mike Kleina5c27172019-04-02 10:26:48 -0400231 if (draw(recorder.beginRecording(info.width(), info.height()))) {
232 return recorder.finishRecordingAsPicture()->serialize();
233 }
234 return nullptr;
Mike Klein9b462092019-03-27 13:52:35 -0500235}
236
Mike Kleina5c27172019-04-02 10:26:48 -0400237static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw,
Mike Kleinc245bd92019-03-27 14:31:09 -0500238 SkImageInfo info,
239 SkString name) {
240 SkPDF::Metadata metadata;
241 metadata.fTitle = name;
242 metadata.fCreator = "Skia/FM";
243 metadata.fRasterDPI = FLAGS_rasterDPI;
244 metadata.fPDFA = FLAGS_PDFA;
245
246 SkDynamicMemoryWStream stream;
247 if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) {
Mike Kleina5c27172019-04-02 10:26:48 -0400248 if (draw(doc->beginPage(info.width(), info.height()))) {
249 doc->endPage();
250 doc->close();
251 return stream.detachAsData();
252 }
Mike Kleinc245bd92019-03-27 14:31:09 -0500253 }
254 return nullptr;
255}
256
Mike Kleina5c27172019-04-02 10:26:48 -0400257static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw,
Mike Klein735c7ba2019-03-25 12:57:58 -0500258 SkImageInfo info,
259 GrContextFactory::ContextType api,
260 GrContextFactory* factory) {
261 enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget };
262 const FlagOption<SurfaceType> kSurfaceTypes[] = {
263 { "default", SurfaceType::kDefault },
264 { "betex" , SurfaceType::kBackendTexture },
265 { "bert" , SurfaceType::kBackendRenderTarget },
266 };
267 SurfaceType surfaceType;
268 if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) {
269 return nullptr;
270 }
271
272 auto overrides = FLAGS_nvpr ? GrContextFactory::ContextOverrides::kRequireNVPRSupport
273 : GrContextFactory::ContextOverrides::kDisableNVPR;
274 if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; }
275
276 GrContext* context = factory->getContextInfo(api, overrides)
277 .grContext();
278
279 uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag
280 : 0;
281 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
282
283 sk_sp<SkSurface> surface;
284 GrBackendTexture backendTexture;
285 GrBackendRenderTarget backendRT;
286
287 switch (surfaceType) {
288 case SurfaceType::kDefault:
289 surface = SkSurface::MakeRenderTarget(context,
290 SkBudgeted::kNo,
291 info,
292 FLAGS_samples,
293 &props);
294 break;
295
296 case SurfaceType::kBackendTexture:
297 backendTexture = context->priv().getGpu()
298 ->createTestingOnlyBackendTexture(nullptr,
299 info.width(),
300 info.height(),
301 info.colorType(),
302 true,
303 GrMipMapped::kNo);
304 surface = SkSurface::MakeFromBackendTexture(context,
305 backendTexture,
306 kTopLeft_GrSurfaceOrigin,
307 FLAGS_samples,
308 info.colorType(),
309 info.refColorSpace(),
310 &props);
311 break;
312
313 case SurfaceType::kBackendRenderTarget:
314 backendRT = context->priv().getGpu()
315 ->createTestingOnlyBackendRenderTarget(info.width(),
316 info.height(),
317 SkColorTypeToGrColorType(info.colorType()));
318 surface = SkSurface::MakeFromBackendRenderTarget(context,
319 backendRT,
320 kBottomLeft_GrSurfaceOrigin,
321 info.colorType(),
322 info.refColorSpace(),
323 &props);
324 break;
325 }
326
327 if (!surface) {
328 fprintf(stderr, "Could not create GPU surface.\n");
329 return nullptr;
330 }
331
332 if (FLAGS_preAbandonGpuContext) {
333 factory->abandonContexts();
334 }
335
Mike Kleina5c27172019-04-02 10:26:48 -0400336 sk_sp<SkImage> image;
337 if (draw(surface->getCanvas())) {
338 image = surface->makeImageSnapshot();
339 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500340
341 if (FLAGS_abandonGpuContext) {
342 factory->abandonContexts();
343 } else if (FLAGS_releaseAndAbandonGpuContext) {
344 factory->releaseResourcesAndAbandonContexts();
345 }
346
347 if (!context->abandoned()) {
348 surface.reset();
349 if (backendTexture.isValid()) {
350 context->priv().getGpu()->deleteTestingOnlyBackendTexture(backendTexture);
351 }
352 if (backendRT.isValid()) {
353 context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT);
354 }
355 }
356
357 return image;
358}
359
360int main(int argc, char** argv) {
361 CommandLineFlags::Parse(argc, argv);
362
363 if (FLAGS_cpuDetect) {
364 SkGraphics::Init();
365 }
366 initializeEventTracingForTools();
367 ToolUtils::SetDefaultFontMgr();
368 SetAnalyticAAFromCommonFlags();
369
370 GrContextOptions baseOptions;
371 SetCtxOptionsFromCommonFlags(&baseOptions);
372
Brian Osman5aa11fb2019-04-08 16:40:36 -0400373 sk_gpu_test::MemoryCache memoryCache;
374 if (!FLAGS_writeShaders.isEmpty()) {
375 baseOptions.fPersistentCache = &memoryCache;
376 baseOptions.fDisallowGLSLBinaryCaching = true;
377 }
378
Mike Kleincbe93ee2019-04-02 17:00:54 -0400379 SkTHashMap<SkString, skiagm::GMFactory> gm_factories;
Mike Klein735c7ba2019-03-25 12:57:58 -0500380 for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400381 std::unique_ptr<skiagm::GM> gm{factory(nullptr)};
Mike Klein735c7ba2019-03-25 12:57:58 -0500382 if (FLAGS_sources.isEmpty()) {
383 fprintf(stdout, "%s\n", gm->getName());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400384 } else {
385 gm_factories.set(SkString{gm->getName()}, factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500386 }
387 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400388 if (FLAGS_sources.isEmpty()) {
389 return 0;
390 }
391
392 SkTArray<Source> sources;
Mike Klein735c7ba2019-03-25 12:57:58 -0500393 for (const SkString& source : FLAGS_sources) {
Mike Kleincbe93ee2019-04-02 17:00:54 -0400394 if (skiagm::GMFactory* factory = gm_factories.find(source)) {
395 std::shared_ptr<skiagm::GM> gm{(*factory)(nullptr)};
396 sources.push_back(gm_source(gm));
397 continue;
398 }
399
Mike Klein735c7ba2019-03-25 12:57:58 -0500400 if (sk_sp<SkData> blob = SkData::MakeFromFileName(source.c_str())) {
Mike Klein22924262019-04-02 14:14:56 -0400401 const SkString dir = SkOSPath::Dirname (source.c_str()),
402 name = SkOSPath::Basename(source.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500403
Mike Kleince90d6f2019-03-29 11:14:14 -0500404 if (name.endsWith(".skp")) {
405 if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) {
406 sources.push_back(picture_source(name, pic));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400407 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500408 }
409 } else if (name.endsWith(".svg")) {
410 SkMemoryStream stream{blob};
411 if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) {
412 sources.push_back(svg_source(name, svg));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400413 continue;
Mike Kleince90d6f2019-03-29 11:14:14 -0500414 }
Mike Klein22924262019-04-02 14:14:56 -0400415 } else if (name.endsWith(".json")) {
416 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
417 .setResourceProvider(skottie_utils::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400418 .make((const char*)blob->data(), blob->size())) {
Mike Klein22924262019-04-02 14:14:56 -0400419 sources.push_back(skottie_source(name, animation));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400420 continue;
Mike Klein22924262019-04-02 14:14:56 -0400421 }
Mike Kleince90d6f2019-03-29 11:14:14 -0500422 } else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500423 sources.push_back(codec_source(name, codec));
Mike Kleincbe93ee2019-04-02 17:00:54 -0400424 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500425 }
426 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400427
Mike Klein408c3ea2019-04-03 10:31:25 -0400428 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", source.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400429 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500430 }
431
Mike Klein9b462092019-03-27 13:52:35 -0500432 enum NonGpuBackends {
433 kCPU_Backend = -1,
434 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500435 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500436 };
437 const FlagOption<int> kBackends[] = {
438 { "cpu" , kCPU_Backend },
439 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500440 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500441 { "gl" , GrContextFactory::kGL_ContextType },
442 { "gles" , GrContextFactory::kGLES_ContextType },
443 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
444 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
445 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
446 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
447 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
448 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
449 { "vk" , GrContextFactory::kVulkan_ContextType },
450 { "mtl" , GrContextFactory::kMetal_ContextType },
451 { "mock" , GrContextFactory::kMock_ContextType },
452 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500453 const FlagOption<SkColorType> kColorTypes[] = {
454 { "a8", kAlpha_8_SkColorType },
455 { "g8", kGray_8_SkColorType },
456 { "565", kRGB_565_SkColorType },
457 { "4444", kARGB_4444_SkColorType },
458 { "8888", kN32_SkColorType },
459 { "888x", kRGB_888x_SkColorType },
460 { "1010102", kRGBA_1010102_SkColorType },
461 { "101010x", kRGB_101010x_SkColorType },
462 { "f16norm", kRGBA_F16Norm_SkColorType },
463 { "f16", kRGBA_F16_SkColorType },
464 { "f32", kRGBA_F32_SkColorType },
465 { "rgba", kRGBA_8888_SkColorType },
466 { "bgra", kBGRA_8888_SkColorType },
467 };
468 const FlagOption<SkAlphaType> kAlphaTypes[] = {
469 { "premul", kPremul_SkAlphaType },
470 { "unpremul", kUnpremul_SkAlphaType },
471 };
472 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
473 { "srgb", SkNamedGamut::kSRGB },
474 { "p3", SkNamedGamut::kDCIP3 },
475 { "rec2020", SkNamedGamut::kRec2020 },
476 { "adobe", SkNamedGamut::kAdobeRGB },
477 { "narrow", gNarrow_toXYZD50},
478 };
479 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
480 { "srgb" , SkNamedTransferFn::kSRGB },
481 { "rec2020", {2.22222f, 0.909672f, 0.0903276f, 0.222222f, 0.0812429f, 0, 0} },
482 { "2.2" , SkNamedTransferFn::k2Dot2 },
483 { "linear" , SkNamedTransferFn::kLinear },
484 };
485
Mike Klein735c7ba2019-03-25 12:57:58 -0500486
Mike Klein9b462092019-03-27 13:52:35 -0500487 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500488 SkColorType ct;
489 SkAlphaType at;
490 skcms_Matrix3x3 gamut;
491 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500492
Mike Klein9b462092019-03-27 13:52:35 -0500493 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
494 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500495 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
496 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500497 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500498 return 1;
499 }
500
Mike Klein4daf6372019-04-03 11:19:45 -0400501 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
502 : SkColorSpace::MakeRGB(tf,gamut);
503 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500504
505 for (auto source : sources) {
506 const auto start = std::chrono::steady_clock::now();
Mike Klein832d3c52019-04-02 15:23:46 -0400507 fprintf(stdout, "%50s", source.name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500508
509 const SkImageInfo info = unsized_info.makeWH(source.size.width(),
510 source.size.height());
511
512 GrContextOptions options = baseOptions;
513 source.tweak(&options);
514 GrContextFactory factory(options); // N.B. factory must outlive image
515
516 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500517 sk_sp<SkData> blob;
518 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500519 switch (backend) {
520 case kCPU_Backend:
521 image = draw_with_cpu(source.draw, info);
522 break;
Mike Klein9b462092019-03-27 13:52:35 -0500523 case kSKP_Backend:
524 blob = draw_as_skp(source.draw, info);
525 ext = ".skp";
526 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500527 case kPDF_Backend:
528 blob = draw_as_pdf(source.draw, info, source.name);
529 ext = ".pdf";
530 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500531 default:
532 image = draw_with_gpu(source.draw, info,
533 (GrContextFactory::ContextType)backend, &factory);
534 break;
535 }
536
Mike Klein9b462092019-03-27 13:52:35 -0500537 if (!image && !blob) {
Mike Klein832d3c52019-04-02 15:23:46 -0400538 fprintf(stdout, "\tskipped\n");
Mike Kleina5c27172019-04-02 10:26:48 -0400539 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500540 }
541
542 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500543 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500544 fprintf(stderr, "SkImage::asLegacyBitmap() failed.\n");
545 exit_with_failure();
546 }
547
548 HashAndEncode hashAndEncode{bitmap};
549 SkString md5;
550 {
551 SkMD5 hash;
Mike Klein9b462092019-03-27 13:52:35 -0500552 if (image) {
553 hashAndEncode.write(&hash);
554 } else {
555 hash.write(blob->data(), blob->size());
556 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500557
Hal Canary0f2f5222019-04-03 10:13:45 -0400558 SkMD5::Digest digest = hash.finish();
Mike Klein735c7ba2019-03-25 12:57:58 -0500559 for (int i = 0; i < 16; i++) {
560 md5.appendf("%02x", digest.data[i]);
561 }
562 }
563
564 if (!FLAGS_writePath.isEmpty()) {
565 sk_mkdir(FLAGS_writePath[0]);
Mike Klein9b462092019-03-27 13:52:35 -0500566 SkString path = SkStringPrintf("%s/%s%s", FLAGS_writePath[0], source.name.c_str(), ext);
Mike Klein735c7ba2019-03-25 12:57:58 -0500567
Mike Klein9b462092019-03-27 13:52:35 -0500568 if (image) {
569 if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(),
570 FLAGS_key, FLAGS_parameters)) {
571 fprintf(stderr, "Could not write to %s.\n", path.c_str());
572 exit_with_failure();
573 }
574 } else {
575 SkFILEWStream file(path.c_str());
576 file.write(blob->data(), blob->size());
Mike Klein735c7ba2019-03-25 12:57:58 -0500577 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500578 }
579
Mike Klein832d3c52019-04-02 15:23:46 -0400580 const auto elapsed = std::chrono::steady_clock::now() - start;
581 fprintf(stdout, "\t%s\t%7dms\n",
582 md5.c_str(),
583 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Mike Klein735c7ba2019-03-25 12:57:58 -0500584 }
585
Brian Osman5aa11fb2019-04-08 16:40:36 -0400586 if (!FLAGS_writeShaders.isEmpty()) {
587 sk_mkdir(FLAGS_writeShaders[0]);
588 GrBackendApi api =
589 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
590 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
591
592 }
593
Mike Klein735c7ba2019-03-25 12:57:58 -0500594 return 0;
595}