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."); |
Mike Klein | 7b8bc53 | 2019-04-09 11:19:47 -0500 | [diff] [blame] | 70 | static DEFINE_string(properties, "", "Metadata passed through to .png encoder and .json output."); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 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 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 101 | struct Result { |
| 102 | enum { Ok, Skip, Fail} status; |
| 103 | SkString failure; |
| 104 | }; |
| 105 | static const Result ok = {Result::Ok, {}}, |
| 106 | skip = {Result::Skip, {}}; |
| 107 | |
| 108 | template <typename... Args> |
| 109 | static Result fail(const char* why, Args... args) { |
| 110 | return { Result::Fail, SkStringPrintf(why, args...) }; |
| 111 | } |
| 112 | |
| 113 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 114 | struct Source { |
| 115 | SkString name; |
| 116 | SkISize size; |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 117 | std::function<Result(SkCanvas*)> draw; |
| 118 | std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){}; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 119 | }; |
| 120 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 121 | static 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 132 | }; |
| 133 | } |
| 134 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 135 | static 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 140 | }; |
| 141 | } |
| 142 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 143 | static 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 151 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 152 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 162 | }; |
| 163 | } |
| 164 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 165 | static 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 Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 171 | }; |
| 172 | } |
| 173 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 174 | static 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 Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 178 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 179 | // 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 Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 184 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 185 | const float dt = 1.0f / (tiles*tiles - 1); |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 186 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 187 | for (int y : order) |
| 188 | for (int x : order) { |
| 189 | SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim}; |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 190 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 191 | 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 Klein | 6253d90 | 2019-03-27 15:09:12 -0500 | [diff] [blame] | 201 | }; |
| 202 | } |
| 203 | |
| 204 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 205 | static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 206 | SkImageInfo info) { |
| 207 | if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) { |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 208 | if (draw(surface->getCanvas())) { |
| 209 | return surface->makeImageSnapshot(); |
| 210 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 211 | } |
| 212 | return nullptr; |
| 213 | } |
| 214 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 215 | static sk_sp<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw, |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 216 | SkImageInfo info) { |
| 217 | SkPictureRecorder recorder; |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 218 | if (draw(recorder.beginRecording(info.width(), info.height()))) { |
| 219 | return recorder.finishRecordingAsPicture()->serialize(); |
| 220 | } |
| 221 | return nullptr; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 224 | static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw, |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 225 | 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 Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 235 | if (draw(doc->beginPage(info.width(), info.height()))) { |
| 236 | doc->endPage(); |
| 237 | doc->close(); |
| 238 | return stream.detachAsData(); |
| 239 | } |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 240 | } |
| 241 | return nullptr; |
| 242 | } |
| 243 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 244 | static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 245 | 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 Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 323 | sk_sp<SkImage> image; |
| 324 | if (draw(surface->getCanvas())) { |
| 325 | image = surface->makeImageSnapshot(); |
| 326 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 327 | |
| 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 | |
| 347 | int 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 Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 360 | sk_gpu_test::MemoryCache memoryCache; |
| 361 | if (!FLAGS_writeShaders.isEmpty()) { |
| 362 | baseOptions.fPersistentCache = &memoryCache; |
| 363 | baseOptions.fDisallowGLSLBinaryCaching = true; |
| 364 | } |
| 365 | |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 366 | SkTHashMap<SkString, skiagm::GMFactory> gm_factories; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 367 | for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) { |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 368 | std::unique_ptr<skiagm::GM> gm{factory(nullptr)}; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 369 | if (FLAGS_sources.isEmpty()) { |
| 370 | fprintf(stdout, "%s\n", gm->getName()); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 371 | } else { |
| 372 | gm_factories.set(SkString{gm->getName()}, factory); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 373 | } |
| 374 | } |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 375 | if (FLAGS_sources.isEmpty()) { |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | SkTArray<Source> sources; |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 380 | for (const SkString& name : FLAGS_sources) { |
| 381 | Source* source = &sources.push_back(); |
| 382 | |
| 383 | if (skiagm::GMFactory* factory = gm_factories.find(name)) { |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 384 | std::shared_ptr<skiagm::GM> gm{(*factory)(nullptr)}; |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 385 | source->name = name; |
| 386 | init(source, gm); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 387 | continue; |
| 388 | } |
| 389 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 390 | if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) { |
| 391 | source->name = SkOSPath::Basename(name.c_str()); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 392 | |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 393 | if (name.endsWith(".skp")) { |
| 394 | if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) { |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 395 | init(source, pic); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 396 | continue; |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 397 | } |
| 398 | } else if (name.endsWith(".svg")) { |
| 399 | SkMemoryStream stream{blob}; |
| 400 | if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) { |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 401 | init(source, svg); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 402 | continue; |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 403 | } |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 404 | } else if (name.endsWith(".json")) { |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 405 | const SkString dir = SkOSPath::Dirname(name.c_str()); |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 406 | if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder() |
| 407 | .setResourceProvider(skottie_utils::FileResourceProvider::Make(dir)) |
Mike Klein | 176da0b | 2019-04-03 07:44:24 -0400 | [diff] [blame] | 408 | .make((const char*)blob->data(), blob->size())) { |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 409 | init(source, animation); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 410 | continue; |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 411 | } |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 412 | } else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) { |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 413 | init(source, codec); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 414 | continue; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 415 | } |
| 416 | } |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 417 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 418 | fprintf(stderr, "Don't understand source '%s'... bailing out.\n", name.c_str()); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 419 | return 1; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 420 | } |
| 421 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 422 | enum NonGpuBackends { |
| 423 | kCPU_Backend = -1, |
| 424 | kSKP_Backend = -2, |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 425 | kPDF_Backend = -3, |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 426 | }; |
| 427 | const FlagOption<int> kBackends[] = { |
| 428 | { "cpu" , kCPU_Backend }, |
| 429 | { "skp" , kSKP_Backend }, |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 430 | { "pdf" , kPDF_Backend }, |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 431 | { "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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 443 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 476 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 477 | int backend; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 478 | SkColorType ct; |
| 479 | SkAlphaType at; |
| 480 | skcms_Matrix3x3 gamut; |
| 481 | skcms_TransferFunction tf; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 482 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 483 | if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) || |
| 484 | !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) || |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 485 | !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) || |
| 486 | !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) || |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 487 | !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) { |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 488 | return 1; |
| 489 | } |
| 490 | |
Mike Klein | 4daf637 | 2019-04-03 11:19:45 -0400 | [diff] [blame] | 491 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 494 | |
| 495 | for (auto source : sources) { |
| 496 | const auto start = std::chrono::steady_clock::now(); |
Mike Klein | 832d3c5 | 2019-04-02 15:23:46 -0400 | [diff] [blame] | 497 | fprintf(stdout, "%50s", source.name.c_str()); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 498 | |
| 499 | const SkImageInfo info = unsized_info.makeWH(source.size.width(), |
| 500 | source.size.height()); |
| 501 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 502 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 514 | GrContextOptions options = baseOptions; |
| 515 | source.tweak(&options); |
| 516 | GrContextFactory factory(options); // N.B. factory must outlive image |
| 517 | |
| 518 | sk_sp<SkImage> image; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 519 | sk_sp<SkData> blob; |
| 520 | const char* ext = ".png"; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 521 | switch (backend) { |
| 522 | case kCPU_Backend: |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 523 | image = draw_with_cpu(draw, info); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 524 | break; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 525 | case kSKP_Backend: |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 526 | blob = draw_as_skp(draw, info); |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 527 | ext = ".skp"; |
| 528 | break; |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 529 | case kPDF_Backend: |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 530 | blob = draw_as_pdf(draw, info, source.name); |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 531 | ext = ".pdf"; |
| 532 | break; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 533 | default: |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 534 | image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 535 | break; |
| 536 | } |
| 537 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 538 | if (!image && !blob) { |
Mike Klein | 832d3c5 | 2019-04-02 15:23:46 -0400 | [diff] [blame] | 539 | fprintf(stdout, "\tskipped\n"); |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 540 | continue; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | SkBitmap bitmap; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 544 | if (image && !image->asLegacyBitmap(&bitmap)) { |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 545 | fprintf(stderr, "SkImage::asLegacyBitmap() failed.\n"); |
| 546 | exit_with_failure(); |
| 547 | } |
| 548 | |
| 549 | HashAndEncode hashAndEncode{bitmap}; |
| 550 | SkString md5; |
| 551 | { |
| 552 | SkMD5 hash; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 553 | if (image) { |
| 554 | hashAndEncode.write(&hash); |
| 555 | } else { |
| 556 | hash.write(blob->data(), blob->size()); |
| 557 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 558 | |
Hal Canary | 0f2f522 | 2019-04-03 10:13:45 -0400 | [diff] [blame] | 559 | SkMD5::Digest digest = hash.finish(); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 560 | 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 Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 567 | 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] | 568 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 569 | if (image) { |
| 570 | if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(), |
Mike Klein | 7b8bc53 | 2019-04-09 11:19:47 -0500 | [diff] [blame] | 571 | FLAGS_key, FLAGS_properties)) { |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 572 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 578 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 579 | } |
| 580 | |
Mike Klein | 832d3c5 | 2019-04-02 15:23:46 -0400 | [diff] [blame] | 581 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 585 | } |
| 586 | |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 587 | 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 Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 595 | return 0; |
| 596 | } |