blob: 6e385d427d7eda4ab72f9dedc2f5a0d376fdeff0 [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.");
Mike Klein7b8bc532019-04-09 11:19:47 -050070static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output.");
Mike Klein735c7ba2019-03-25 12:57:58 -050071
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
Mike Kleina833cff2019-04-09 11:52:40 -0500101struct Result {
102 enum { Ok, Skip, Fail} status;
103 SkString failure;
104};
105static const Result ok = {Result::Ok, {}},
106 skip = {Result::Skip, {}};
107
108template <typename... Args>
109static Result fail(const char* why, Args... args) {
110 return { Result::Fail, SkStringPrintf(why, args...) };
111}
112
113
Mike Klein735c7ba2019-03-25 12:57:58 -0500114struct Source {
115 SkString name;
116 SkISize size;
Mike Kleina833cff2019-04-09 11:52:40 -0500117 std::function<Result(SkCanvas*)> draw;
118 std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){};
Mike Klein735c7ba2019-03-25 12:57:58 -0500119};
120
Mike Kleina833cff2019-04-09 11:52:40 -0500121static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
122 source->size = gm->getISize();
123 source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
124 source->draw = [gm](SkCanvas* canvas) {
125 SkString err;
126 switch (gm->draw(canvas, &err)) {
127 case skiagm::DrawResult::kOk: break;
128 case skiagm::DrawResult::kSkip: return skip;
129 case skiagm::DrawResult::kFail: return fail(err.c_str());
130 }
131 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500132 };
133}
134
Mike Kleina833cff2019-04-09 11:52:40 -0500135static void init(Source* source, sk_sp<SkPicture> pic) {
136 source->size = pic->cullRect().roundOut().size();
137 source->draw = [pic](SkCanvas* canvas) {
138 canvas->drawPicture(pic);
139 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500140 };
141}
142
Mike Kleina833cff2019-04-09 11:52:40 -0500143static void init(Source* source, std::shared_ptr<SkCodec> codec) {
144 source->size = codec->dimensions();
145 source->draw = [codec](SkCanvas* canvas) {
146 SkImageInfo info = codec->getInfo();
147 if (FLAGS_decodeToDst) {
148 info = canvas->imageInfo().makeWH(info.width(),
149 info.height());
150 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500151
Mike Kleina833cff2019-04-09 11:52:40 -0500152 SkBitmap bm;
153 bm.allocPixels(info);
154 switch (SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) {
155 case SkCodec::kSuccess:
156 case SkCodec::kErrorInInput:
157 case SkCodec::kIncompleteInput: canvas->drawBitmap(bm, 0,0);
158 break;
159 default: return fail("codec->getPixels() failed: %d\n", result);
160 }
161 return ok;
Mike Klein735c7ba2019-03-25 12:57:58 -0500162 };
163}
164
Mike Kleina833cff2019-04-09 11:52:40 -0500165static void init(Source* source, sk_sp<SkSVGDOM> svg) {
166 source->size = svg->containerSize().isEmpty() ? SkISize{1000,1000}
167 : svg->containerSize().toCeil();
168 source->draw = [svg](SkCanvas* canvas) {
169 svg->render(canvas);
170 return ok;
Mike Klein22924262019-04-02 14:14:56 -0400171 };
172}
173
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}
203
204
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);
349
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 }
Mike Klein22924262019-04-02 14:14:56 -0400404 } else if (name.endsWith(".json")) {
Mike Kleina833cff2019-04-09 11:52:40 -0500405 const SkString dir = SkOSPath::Dirname(name.c_str());
Mike Klein22924262019-04-02 14:14:56 -0400406 if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder()
407 .setResourceProvider(skottie_utils::FileResourceProvider::Make(dir))
Mike Klein176da0b2019-04-03 07:44:24 -0400408 .make((const char*)blob->data(), blob->size())) {
Mike Kleina833cff2019-04-09 11:52:40 -0500409 init(source, animation);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400410 continue;
Mike Klein22924262019-04-02 14:14:56 -0400411 }
Mike Kleince90d6f2019-03-29 11:14:14 -0500412 } else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) {
Mike Kleina833cff2019-04-09 11:52:40 -0500413 init(source, codec);
Mike Kleincbe93ee2019-04-02 17:00:54 -0400414 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500415 }
416 }
Mike Kleincbe93ee2019-04-02 17:00:54 -0400417
Mike Kleina833cff2019-04-09 11:52:40 -0500418 fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str());
Mike Kleincbe93ee2019-04-02 17:00:54 -0400419 return 1;
Mike Klein735c7ba2019-03-25 12:57:58 -0500420 }
421
Mike Klein9b462092019-03-27 13:52:35 -0500422 enum NonGpuBackends {
423 kCPU_Backend = -1,
424 kSKP_Backend = -2,
Mike Kleinc245bd92019-03-27 14:31:09 -0500425 kPDF_Backend = -3,
Mike Klein9b462092019-03-27 13:52:35 -0500426 };
427 const FlagOption<int> kBackends[] = {
428 { "cpu" , kCPU_Backend },
429 { "skp" , kSKP_Backend },
Mike Kleinc245bd92019-03-27 14:31:09 -0500430 { "pdf" , kPDF_Backend },
Mike Klein9b462092019-03-27 13:52:35 -0500431 { "gl" , GrContextFactory::kGL_ContextType },
432 { "gles" , GrContextFactory::kGLES_ContextType },
433 { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType },
434 { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType },
435 { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType },
436 { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType },
437 { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType },
438 { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType },
439 { "vk" , GrContextFactory::kVulkan_ContextType },
440 { "mtl" , GrContextFactory::kMetal_ContextType },
441 { "mock" , GrContextFactory::kMock_ContextType },
442 };
Mike Klein735c7ba2019-03-25 12:57:58 -0500443 const FlagOption<SkColorType> kColorTypes[] = {
444 { "a8", kAlpha_8_SkColorType },
445 { "g8", kGray_8_SkColorType },
446 { "565", kRGB_565_SkColorType },
447 { "4444", kARGB_4444_SkColorType },
448 { "8888", kN32_SkColorType },
449 { "888x", kRGB_888x_SkColorType },
450 { "1010102", kRGBA_1010102_SkColorType },
451 { "101010x", kRGB_101010x_SkColorType },
452 { "f16norm", kRGBA_F16Norm_SkColorType },
453 { "f16", kRGBA_F16_SkColorType },
454 { "f32", kRGBA_F32_SkColorType },
455 { "rgba", kRGBA_8888_SkColorType },
456 { "bgra", kBGRA_8888_SkColorType },
457 };
458 const FlagOption<SkAlphaType> kAlphaTypes[] = {
459 { "premul", kPremul_SkAlphaType },
460 { "unpremul", kUnpremul_SkAlphaType },
461 };
462 const FlagOption<skcms_Matrix3x3> kGamuts[] = {
463 { "srgb", SkNamedGamut::kSRGB },
464 { "p3", SkNamedGamut::kDCIP3 },
465 { "rec2020", SkNamedGamut::kRec2020 },
466 { "adobe", SkNamedGamut::kAdobeRGB },
467 { "narrow", gNarrow_toXYZD50},
468 };
469 const FlagOption<skcms_TransferFunction> kTransferFunctions[] = {
470 { "srgb" , SkNamedTransferFn::kSRGB },
471 { "rec2020", {2.22222f, 0.909672f, 0.0903276f, 0.222222f, 0.0812429f, 0, 0} },
472 { "2.2" , SkNamedTransferFn::k2Dot2 },
473 { "linear" , SkNamedTransferFn::kLinear },
474 };
475
Mike Klein735c7ba2019-03-25 12:57:58 -0500476
Mike Klein9b462092019-03-27 13:52:35 -0500477 int backend;
Mike Klein735c7ba2019-03-25 12:57:58 -0500478 SkColorType ct;
479 SkAlphaType at;
480 skcms_Matrix3x3 gamut;
481 skcms_TransferFunction tf;
Mike Klein735c7ba2019-03-25 12:57:58 -0500482
Mike Klein9b462092019-03-27 13:52:35 -0500483 if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) ||
484 !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) ||
Mike Klein735c7ba2019-03-25 12:57:58 -0500485 !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) ||
486 !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) ||
Mike Klein9b462092019-03-27 13:52:35 -0500487 !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500488 return 1;
489 }
490
Mike Klein4daf6372019-04-03 11:19:45 -0400491 sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr
492 : SkColorSpace::MakeRGB(tf,gamut);
493 const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs);
Mike Klein735c7ba2019-03-25 12:57:58 -0500494
495 for (auto source : sources) {
496 const auto start = std::chrono::steady_clock::now();
Mike Klein832d3c52019-04-02 15:23:46 -0400497 fprintf(stdout, "%50s", source.name.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500498
499 const SkImageInfo info = unsized_info.makeWH(source.size.width(),
500 source.size.height());
501
Mike Kleina833cff2019-04-09 11:52:40 -0500502 auto draw = [&source](SkCanvas* canvas) {
503 Result result = source.draw(canvas);
504 switch (result.status) {
505 case Result::Ok: break;
506 case Result::Skip: return false;
507 case Result::Fail:
508 fprintf(stderr, "%s failed: %s\n", source.name.c_str(), result.failure.c_str());
509 exit_with_failure();
510 }
511 return true;
512 };
513
Mike Klein735c7ba2019-03-25 12:57:58 -0500514 GrContextOptions options = baseOptions;
515 source.tweak(&options);
516 GrContextFactory factory(options); // N.B. factory must outlive image
517
518 sk_sp<SkImage> image;
Mike Klein9b462092019-03-27 13:52:35 -0500519 sk_sp<SkData> blob;
520 const char* ext = ".png";
Mike Klein735c7ba2019-03-25 12:57:58 -0500521 switch (backend) {
522 case kCPU_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500523 image = draw_with_cpu(draw, info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500524 break;
Mike Klein9b462092019-03-27 13:52:35 -0500525 case kSKP_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500526 blob = draw_as_skp(draw, info);
Mike Klein9b462092019-03-27 13:52:35 -0500527 ext = ".skp";
528 break;
Mike Kleinc245bd92019-03-27 14:31:09 -0500529 case kPDF_Backend:
Mike Kleina833cff2019-04-09 11:52:40 -0500530 blob = draw_as_pdf(draw, info, source.name);
Mike Kleinc245bd92019-03-27 14:31:09 -0500531 ext = ".pdf";
532 break;
Mike Klein735c7ba2019-03-25 12:57:58 -0500533 default:
Mike Kleina833cff2019-04-09 11:52:40 -0500534 image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory);
Mike Klein735c7ba2019-03-25 12:57:58 -0500535 break;
536 }
537
Mike Klein9b462092019-03-27 13:52:35 -0500538 if (!image && !blob) {
Mike Klein832d3c52019-04-02 15:23:46 -0400539 fprintf(stdout, "\tskipped\n");
Mike Kleina5c27172019-04-02 10:26:48 -0400540 continue;
Mike Klein735c7ba2019-03-25 12:57:58 -0500541 }
542
543 SkBitmap bitmap;
Mike Klein9b462092019-03-27 13:52:35 -0500544 if (image && !image->asLegacyBitmap(&bitmap)) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500545 fprintf(stderr, "SkImage::asLegacyBitmap() failed.\n");
546 exit_with_failure();
547 }
548
549 HashAndEncode hashAndEncode{bitmap};
550 SkString md5;
551 {
552 SkMD5 hash;
Mike Klein9b462092019-03-27 13:52:35 -0500553 if (image) {
554 hashAndEncode.write(&hash);
555 } else {
556 hash.write(blob->data(), blob->size());
557 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500558
Hal Canary0f2f5222019-04-03 10:13:45 -0400559 SkMD5::Digest digest = hash.finish();
Mike Klein735c7ba2019-03-25 12:57:58 -0500560 for (int i = 0; i < 16; i++) {
561 md5.appendf("%02x", digest.data[i]);
562 }
563 }
564
565 if (!FLAGS_writePath.isEmpty()) {
566 sk_mkdir(FLAGS_writePath[0]);
Mike Klein9b462092019-03-27 13:52:35 -0500567 SkString path = SkStringPrintf("%s/%s%s", FLAGS_writePath[0], source.name.c_str(), ext);
Mike Klein735c7ba2019-03-25 12:57:58 -0500568
Mike Klein9b462092019-03-27 13:52:35 -0500569 if (image) {
570 if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(),
Mike Klein7b8bc532019-04-09 11:19:47 -0500571 FLAGS_key, FLAGS_properties)) {
Mike Klein9b462092019-03-27 13:52:35 -0500572 fprintf(stderr, "Could not write to %s.\n", path.c_str());
573 exit_with_failure();
574 }
575 } else {
576 SkFILEWStream file(path.c_str());
577 file.write(blob->data(), blob->size());
Mike Klein735c7ba2019-03-25 12:57:58 -0500578 }
Mike Klein735c7ba2019-03-25 12:57:58 -0500579 }
580
Mike Klein832d3c52019-04-02 15:23:46 -0400581 const auto elapsed = std::chrono::steady_clock::now() - start;
582 fprintf(stdout, "\t%s\t%7dms\n",
583 md5.c_str(),
584 (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
Mike Klein735c7ba2019-03-25 12:57:58 -0500585 }
586
Brian Osman5aa11fb2019-04-08 16:40:36 -0400587 if (!FLAGS_writeShaders.isEmpty()) {
588 sk_mkdir(FLAGS_writeShaders[0]);
589 GrBackendApi api =
590 GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend);
591 memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api);
592
593 }
594
Mike Klein735c7ba2019-03-25 12:57:58 -0500595 return 0;
596}