Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 1 | // 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 Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame^] | 11 | #include "GrPersistentCacheUtils.h" |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 12 | #include "HashAndEncode.h" |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame^] | 13 | #include "MemoryCache.h" |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 14 | #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 Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 21 | #include "SkPDFDocument.h" |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 22 | #include "SkPicture.h" |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 23 | #include "SkPictureRecorder.h" |
Mike Klein | 6253d90 | 2019-03-27 15:09:12 -0500 | [diff] [blame] | 24 | #include "SkSVGDOM.h" |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 25 | #include "SkTHash.h" |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 26 | #include "Skottie.h" |
| 27 | #include "SkottieUtils.h" |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 28 | #include "ToolUtils.h" |
| 29 | #include "gm.h" |
| 30 | #include <chrono> |
| 31 | #include <functional> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | |
| 35 | using sk_gpu_test::GrContextFactory; |
| 36 | |
| 37 | static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw."); |
| 38 | static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into."); |
| 39 | |
Mike Klein | 4daf637 | 2019-04-03 11:19:45 -0400 | [diff] [blame] | 40 | static DEFINE_string(ct , "8888", "The color type for any raster backend."); |
| 41 | static DEFINE_string(at , "premul", "The alpha type for any raster backend."); |
| 42 | static DEFINE_string(gamut , "srgb", "The color gamut for any raster backend."); |
| 43 | static DEFINE_string(tf , "srgb", "The transfer function for any raster backend."); |
| 44 | static DEFINE_bool (legacy, false, "Use a null SkColorSpace instead of --gamut and --tf?"); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 45 | |
| 46 | static DEFINE_int (samples , 0, "Samples per pixel in GPU backends."); |
| 47 | static DEFINE_bool (nvpr , false, "Use NV_path_rendering in GPU backends?"); |
| 48 | static DEFINE_bool (stencils, true, "If false, avoid stencil buffers in GPU backends."); |
| 49 | static DEFINE_bool (dit , false, "Use device-independent text in GPU backends."); |
| 50 | static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces."); |
| 51 | |
| 52 | static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing."); |
| 53 | static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing."); |
| 54 | static DEFINE_bool(releaseAndAbandonGpuContext, false, |
| 55 | "Release all GPU resources and abandon the GrContext after drawing."); |
| 56 | |
| 57 | static DEFINE_bool(decodeToDst, false, |
| 58 | "Decode images to destination format rather than suggested natural format."); |
| 59 | |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 60 | static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI, |
| 61 | "DPI for rasterized content in vector backends like --backend pdf."); |
| 62 | static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?"); |
| 63 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 64 | static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?"); |
| 65 | static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set."); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 66 | |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame^] | 67 | static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set."); |
| 68 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 69 | static DEFINE_string(key, "", "Metadata passed through to .png encoder and .json output."); |
| 70 | static DEFINE_string(parameters, "", "Metadata passed through to .png encoder and .json output."); |
| 71 | |
| 72 | template <typename T> |
| 73 | struct FlagOption { |
| 74 | const char* label; |
| 75 | T value; |
| 76 | }; |
| 77 | |
| 78 | template <typename T, int N> |
| 79 | static 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 | |
| 96 | static void exit_with_failure() { |
| 97 | // TODO: dump stack trace, debug trap, print currently running job, etc? |
| 98 | exit(1); |
| 99 | } |
| 100 | |
| 101 | struct Source { |
| 102 | SkString name; |
| 103 | SkISize size; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 104 | std::function<void(GrContextOptions*)> tweak; |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 105 | std::function<bool(SkCanvas*)> draw; // true -> ok, false -> skip; |
| 106 | // failures should exit_with_failure() |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | static Source gm_source(std::shared_ptr<skiagm::GM> gm) { |
| 110 | return { |
| 111 | SkString{gm->getName()}, |
| 112 | gm->getISize(), |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 113 | [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); }, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 114 | [gm](SkCanvas* canvas) { |
| 115 | SkString err; |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 116 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 122 | } |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 123 | return false; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 124 | }, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 125 | }; |
| 126 | } |
| 127 | |
| 128 | static Source picture_source(SkString name, sk_sp<SkPicture> pic) { |
| 129 | return { |
| 130 | name, |
| 131 | pic->cullRect().roundOut().size(), |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 132 | [](GrContextOptions*) {}, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 133 | [pic](SkCanvas* canvas) { |
| 134 | canvas->drawPicture(pic); |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 135 | return true; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 136 | }, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 137 | }; |
| 138 | } |
| 139 | |
| 140 | static Source codec_source(SkString name, std::shared_ptr<SkCodec> codec) { |
| 141 | return { |
| 142 | name, |
| 143 | codec->dimensions(), |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 144 | [](GrContextOptions*) {}, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 145 | [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 Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 160 | return true; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 161 | default: |
| 162 | fprintf(stderr, "SkCodec::getPixels failed: %d.", result); |
| 163 | exit_with_failure(); |
| 164 | } |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 165 | return false; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 166 | }, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 167 | }; |
| 168 | } |
| 169 | |
Mike Klein | 6253d90 | 2019-03-27 15:09:12 -0500 | [diff] [blame] | 170 | static 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 Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 175 | [](GrContextOptions*) {}, |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 176 | [svg](SkCanvas* canvas) { |
| 177 | svg->render(canvas); |
| 178 | return true; |
| 179 | }, |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 180 | }; |
| 181 | } |
| 182 | |
| 183 | static Source skottie_source(SkString name, sk_sp<skottie::Animation> animation) { |
| 184 | return { |
| 185 | name, |
| 186 | {1000,1000}, |
Mike Klein | 6253d90 | 2019-03-27 15:09:12 -0500 | [diff] [blame] | 187 | [](GrContextOptions*) {}, |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 188 | [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 Klein | 6253d90 | 2019-03-27 15:09:12 -0500 | [diff] [blame] | 214 | }; |
| 215 | } |
| 216 | |
| 217 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 218 | static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 219 | SkImageInfo info) { |
| 220 | if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) { |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 221 | if (draw(surface->getCanvas())) { |
| 222 | return surface->makeImageSnapshot(); |
| 223 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 224 | } |
| 225 | return nullptr; |
| 226 | } |
| 227 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 228 | static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw, |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 229 | SkImageInfo info) { |
| 230 | SkPictureRecorder recorder; |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 231 | if (draw(recorder.beginRecording(info.width(), info.height()))) { |
| 232 | return recorder.finishRecordingAsPicture()->serialize(); |
| 233 | } |
| 234 | return nullptr; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 237 | static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw, |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 238 | 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 Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 248 | if (draw(doc->beginPage(info.width(), info.height()))) { |
| 249 | doc->endPage(); |
| 250 | doc->close(); |
| 251 | return stream.detachAsData(); |
| 252 | } |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 253 | } |
| 254 | return nullptr; |
| 255 | } |
| 256 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 257 | static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 258 | 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 Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 336 | sk_sp<SkImage> image; |
| 337 | if (draw(surface->getCanvas())) { |
| 338 | image = surface->makeImageSnapshot(); |
| 339 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 340 | |
| 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 | |
| 360 | int 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 Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame^] | 373 | sk_gpu_test::MemoryCache memoryCache; |
| 374 | if (!FLAGS_writeShaders.isEmpty()) { |
| 375 | baseOptions.fPersistentCache = &memoryCache; |
| 376 | baseOptions.fDisallowGLSLBinaryCaching = true; |
| 377 | } |
| 378 | |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 379 | SkTHashMap<SkString, skiagm::GMFactory> gm_factories; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 380 | for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) { |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 381 | std::unique_ptr<skiagm::GM> gm{factory(nullptr)}; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 382 | if (FLAGS_sources.isEmpty()) { |
| 383 | fprintf(stdout, "%s\n", gm->getName()); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 384 | } else { |
| 385 | gm_factories.set(SkString{gm->getName()}, factory); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 386 | } |
| 387 | } |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 388 | if (FLAGS_sources.isEmpty()) { |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | SkTArray<Source> sources; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 393 | for (const SkString& source : FLAGS_sources) { |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 394 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 400 | if (sk_sp<SkData> blob = SkData::MakeFromFileName(source.c_str())) { |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 401 | const SkString dir = SkOSPath::Dirname (source.c_str()), |
| 402 | name = SkOSPath::Basename(source.c_str()); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 403 | |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 404 | if (name.endsWith(".skp")) { |
| 405 | if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) { |
| 406 | sources.push_back(picture_source(name, pic)); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 407 | continue; |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 408 | } |
| 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 Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 413 | continue; |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 414 | } |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 415 | } else if (name.endsWith(".json")) { |
| 416 | if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder() |
| 417 | .setResourceProvider(skottie_utils::FileResourceProvider::Make(dir)) |
Mike Klein | 176da0b | 2019-04-03 07:44:24 -0400 | [diff] [blame] | 418 | .make((const char*)blob->data(), blob->size())) { |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 419 | sources.push_back(skottie_source(name, animation)); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 420 | continue; |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 421 | } |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 422 | } else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) { |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 423 | sources.push_back(codec_source(name, codec)); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 424 | continue; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 425 | } |
| 426 | } |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 427 | |
Mike Klein | 408c3ea | 2019-04-03 10:31:25 -0400 | [diff] [blame] | 428 | fprintf(stderr, "Don't understand source '%s'... bailing out.\n", source.c_str()); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 429 | return 1; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 430 | } |
| 431 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 432 | enum NonGpuBackends { |
| 433 | kCPU_Backend = -1, |
| 434 | kSKP_Backend = -2, |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 435 | kPDF_Backend = -3, |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 436 | }; |
| 437 | const FlagOption<int> kBackends[] = { |
| 438 | { "cpu" , kCPU_Backend }, |
| 439 | { "skp" , kSKP_Backend }, |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 440 | { "pdf" , kPDF_Backend }, |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 441 | { "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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 453 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 486 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 487 | int backend; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 488 | SkColorType ct; |
| 489 | SkAlphaType at; |
| 490 | skcms_Matrix3x3 gamut; |
| 491 | skcms_TransferFunction tf; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 492 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 493 | if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) || |
| 494 | !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) || |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 495 | !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) || |
| 496 | !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) || |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 497 | !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) { |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 498 | return 1; |
| 499 | } |
| 500 | |
Mike Klein | 4daf637 | 2019-04-03 11:19:45 -0400 | [diff] [blame] | 501 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 504 | |
| 505 | for (auto source : sources) { |
| 506 | const auto start = std::chrono::steady_clock::now(); |
Mike Klein | 832d3c5 | 2019-04-02 15:23:46 -0400 | [diff] [blame] | 507 | fprintf(stdout, "%50s", source.name.c_str()); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 508 | |
| 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 Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 517 | sk_sp<SkData> blob; |
| 518 | const char* ext = ".png"; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 519 | switch (backend) { |
| 520 | case kCPU_Backend: |
| 521 | image = draw_with_cpu(source.draw, info); |
| 522 | break; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 523 | case kSKP_Backend: |
| 524 | blob = draw_as_skp(source.draw, info); |
| 525 | ext = ".skp"; |
| 526 | break; |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 527 | case kPDF_Backend: |
| 528 | blob = draw_as_pdf(source.draw, info, source.name); |
| 529 | ext = ".pdf"; |
| 530 | break; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 531 | default: |
| 532 | image = draw_with_gpu(source.draw, info, |
| 533 | (GrContextFactory::ContextType)backend, &factory); |
| 534 | break; |
| 535 | } |
| 536 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 537 | if (!image && !blob) { |
Mike Klein | 832d3c5 | 2019-04-02 15:23:46 -0400 | [diff] [blame] | 538 | fprintf(stdout, "\tskipped\n"); |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 539 | continue; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | SkBitmap bitmap; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 543 | if (image && !image->asLegacyBitmap(&bitmap)) { |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 544 | fprintf(stderr, "SkImage::asLegacyBitmap() failed.\n"); |
| 545 | exit_with_failure(); |
| 546 | } |
| 547 | |
| 548 | HashAndEncode hashAndEncode{bitmap}; |
| 549 | SkString md5; |
| 550 | { |
| 551 | SkMD5 hash; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 552 | if (image) { |
| 553 | hashAndEncode.write(&hash); |
| 554 | } else { |
| 555 | hash.write(blob->data(), blob->size()); |
| 556 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 557 | |
Hal Canary | 0f2f522 | 2019-04-03 10:13:45 -0400 | [diff] [blame] | 558 | SkMD5::Digest digest = hash.finish(); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 559 | 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 Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 566 | SkString path = SkStringPrintf("%s/%s%s", FLAGS_writePath[0], source.name.c_str(), ext); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 567 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 568 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 577 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 578 | } |
| 579 | |
Mike Klein | 832d3c5 | 2019-04-02 15:23:46 -0400 | [diff] [blame] | 580 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 584 | } |
| 585 | |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame^] | 586 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 594 | return 0; |
| 595 | } |