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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 4 | #include "experimental/svg/model/SkSVGDOM.h" |
| 5 | #include "gm/gm.h" |
| 6 | #include "include/codec/SkCodec.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame^] | 7 | #include "include/core/SkCanvas.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkColorSpace.h" |
| 9 | #include "include/core/SkGraphics.h" |
| 10 | #include "include/core/SkPicture.h" |
| 11 | #include "include/core/SkPictureRecorder.h" |
| 12 | #include "include/docs/SkPDFDocument.h" |
| 13 | #include "include/gpu/GrContextOptions.h" |
| 14 | #include "include/private/SkTHash.h" |
| 15 | #include "src/core/SkColorSpacePriv.h" |
| 16 | #include "src/core/SkMD5.h" |
| 17 | #include "src/core/SkOSFile.h" |
| 18 | #include "src/gpu/GrContextPriv.h" |
| 19 | #include "src/gpu/GrGpu.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/utils/SkOSPath.h" |
Mike Klein | fee0079 | 2019-11-21 16:18:47 -0600 | [diff] [blame] | 21 | #include "tests/Test.h" |
Jim Van Verth | 8a9a371 | 2019-05-31 10:49:12 -0400 | [diff] [blame] | 22 | #include "tools/AutoreleasePool.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tools/CrashHandler.h" |
| 24 | #include "tools/HashAndEncode.h" |
| 25 | #include "tools/ToolUtils.h" |
| 26 | #include "tools/flags/CommandLineFlags.h" |
| 27 | #include "tools/flags/CommonFlags.h" |
| 28 | #include "tools/gpu/GrContextFactory.h" |
| 29 | #include "tools/gpu/MemoryCache.h" |
| 30 | #include "tools/trace/EventTracingPriv.h" |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 31 | #include <chrono> |
| 32 | #include <functional> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | |
Brian Osman | ca94656 | 2019-04-10 13:51:53 -0400 | [diff] [blame] | 36 | #if defined(SK_ENABLE_SKOTTIE) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 37 | #include "modules/skottie/include/Skottie.h" |
Brian Osman | 849f4d6 | 2019-11-26 08:58:26 -0500 | [diff] [blame] | 38 | #include "modules/skresources/include/SkResources.h" |
Brian Osman | ca94656 | 2019-04-10 13:51:53 -0400 | [diff] [blame] | 39 | #endif |
| 40 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 41 | using sk_gpu_test::GrContextFactory; |
| 42 | |
Mike Klein | fee0079 | 2019-11-21 16:18:47 -0600 | [diff] [blame] | 43 | static DEFINE_bool(listGMs , false, "Print GM names and exit."); |
| 44 | static DEFINE_bool(listTests, false, "Print unit test names and exit."); |
| 45 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 46 | static DEFINE_string2(sources, s, "", "Which GMs, .skps, or images to draw."); |
| 47 | static DEFINE_string2(backend, b, "", "Backend used to create a canvas to draw into."); |
| 48 | |
Mike Klein | 4daf637 | 2019-04-03 11:19:45 -0400 | [diff] [blame] | 49 | static DEFINE_string(ct , "8888", "The color type for any raster backend."); |
| 50 | static DEFINE_string(at , "premul", "The alpha type for any raster backend."); |
| 51 | static DEFINE_string(gamut , "srgb", "The color gamut for any raster backend."); |
| 52 | static DEFINE_string(tf , "srgb", "The transfer function for any raster backend."); |
| 53 | static DEFINE_bool (legacy, false, "Use a null SkColorSpace instead of --gamut and --tf?"); |
Mike Klein | 2a57e79 | 2020-01-14 12:47:52 -0600 | [diff] [blame] | 54 | static DEFINE_bool (skvm , false, "Use SkVMBlitter when supported?"); |
Mike Klein | 31bfa83 | 2020-01-22 11:36:40 -0600 | [diff] [blame] | 55 | static DEFINE_bool (dylib , false, "Use SkVM via dylib?"); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 56 | |
| 57 | static DEFINE_int (samples , 0, "Samples per pixel in GPU backends."); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 58 | static DEFINE_bool (stencils, true, "If false, avoid stencil buffers in GPU backends."); |
| 59 | static DEFINE_bool (dit , false, "Use device-independent text in GPU backends."); |
| 60 | static DEFINE_string(surf , "default", "Backing store for GPU backend surfaces."); |
| 61 | |
| 62 | static DEFINE_bool( preAbandonGpuContext, false, "Abandon the GrContext before drawing."); |
| 63 | static DEFINE_bool( abandonGpuContext, false, "Abandon the GrContext after drawing."); |
| 64 | static DEFINE_bool(releaseAndAbandonGpuContext, false, |
| 65 | "Release all GPU resources and abandon the GrContext after drawing."); |
| 66 | |
| 67 | static DEFINE_bool(decodeToDst, false, |
| 68 | "Decode images to destination format rather than suggested natural format."); |
| 69 | |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 70 | static DEFINE_double(rasterDPI, SK_ScalarDefaultRasterDPI, |
| 71 | "DPI for rasterized content in vector backends like --backend pdf."); |
| 72 | static DEFINE_bool(PDFA, false, "Create PDF/A with --backend pdf?"); |
| 73 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 74 | static DEFINE_bool (cpuDetect, true, "Detect CPU features for runtime optimizations?"); |
| 75 | static DEFINE_string2(writePath, w, "", "Write .pngs to this directory if set."); |
Mike Klein | ed2cec4 | 2019-11-01 10:51:01 -0500 | [diff] [blame] | 76 | static DEFINE_bool (quick, false, "Skip image hashing and encoding?"); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 77 | |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 78 | static DEFINE_string(writeShaders, "", "Write GLSL shaders to this directory if set."); |
| 79 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 80 | 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] | 81 | 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] | 82 | |
| 83 | template <typename T> |
| 84 | struct FlagOption { |
| 85 | const char* label; |
| 86 | T value; |
| 87 | }; |
| 88 | |
| 89 | template <typename T, int N> |
| 90 | static bool parse_flag(const CommandLineFlags::StringArray& flag, |
| 91 | const char* flag_name, |
| 92 | const FlagOption<T> (&array)[N], |
| 93 | T* value) { |
| 94 | for (auto entry : array) { |
| 95 | if (flag.contains(entry.label)) { |
| 96 | *value = entry.value; |
| 97 | return true; |
| 98 | } |
| 99 | } |
| 100 | fprintf(stderr, "Known values for --%s:\n", flag_name); |
| 101 | for (auto entry : array) { |
| 102 | fprintf(stderr, " --%s %s\n", flag_name, entry.label); |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 107 | struct Result { |
| 108 | enum { Ok, Skip, Fail} status; |
| 109 | SkString failure; |
| 110 | }; |
| 111 | static const Result ok = {Result::Ok, {}}, |
| 112 | skip = {Result::Skip, {}}; |
| 113 | |
| 114 | template <typename... Args> |
| 115 | static Result fail(const char* why, Args... args) { |
| 116 | return { Result::Fail, SkStringPrintf(why, args...) }; |
| 117 | } |
| 118 | |
| 119 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 120 | struct Source { |
| 121 | SkString name; |
| 122 | SkISize size; |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 123 | std::function<Result(SkCanvas*)> draw; |
| 124 | std::function<void(GrContextOptions*)> tweak = [](GrContextOptions*){}; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 125 | }; |
| 126 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 127 | static void init(Source* source, std::shared_ptr<skiagm::GM> gm) { |
| 128 | source->size = gm->getISize(); |
| 129 | source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); }; |
| 130 | source->draw = [gm](SkCanvas* canvas) { |
| 131 | SkString err; |
| 132 | switch (gm->draw(canvas, &err)) { |
| 133 | case skiagm::DrawResult::kOk: break; |
| 134 | case skiagm::DrawResult::kSkip: return skip; |
| 135 | case skiagm::DrawResult::kFail: return fail(err.c_str()); |
| 136 | } |
| 137 | return ok; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 138 | }; |
| 139 | } |
| 140 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 141 | static void init(Source* source, sk_sp<SkPicture> pic) { |
| 142 | source->size = pic->cullRect().roundOut().size(); |
| 143 | source->draw = [pic](SkCanvas* canvas) { |
| 144 | canvas->drawPicture(pic); |
| 145 | return ok; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 146 | }; |
| 147 | } |
| 148 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 149 | static void init(Source* source, std::shared_ptr<SkCodec> codec) { |
| 150 | source->size = codec->dimensions(); |
| 151 | source->draw = [codec](SkCanvas* canvas) { |
| 152 | SkImageInfo info = codec->getInfo(); |
| 153 | if (FLAGS_decodeToDst) { |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 154 | info = canvas->imageInfo().makeDimensions(info.dimensions()); |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 155 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 156 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 157 | SkBitmap bm; |
| 158 | bm.allocPixels(info); |
| 159 | switch (SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) { |
| 160 | case SkCodec::kSuccess: |
| 161 | case SkCodec::kErrorInInput: |
| 162 | case SkCodec::kIncompleteInput: canvas->drawBitmap(bm, 0,0); |
| 163 | break; |
| 164 | default: return fail("codec->getPixels() failed: %d\n", result); |
| 165 | } |
| 166 | return ok; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 167 | }; |
| 168 | } |
| 169 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 170 | static void init(Source* source, sk_sp<SkSVGDOM> svg) { |
| 171 | source->size = svg->containerSize().isEmpty() ? SkISize{1000,1000} |
| 172 | : svg->containerSize().toCeil(); |
| 173 | source->draw = [svg](SkCanvas* canvas) { |
| 174 | svg->render(canvas); |
| 175 | return ok; |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 176 | }; |
| 177 | } |
| 178 | |
Brian Osman | ca94656 | 2019-04-10 13:51:53 -0400 | [diff] [blame] | 179 | #if defined(SK_ENABLE_SKOTTIE) |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 180 | static void init(Source* source, sk_sp<skottie::Animation> animation) { |
| 181 | source->size = {1000,1000}; |
| 182 | source->draw = [animation](SkCanvas* canvas) { |
| 183 | canvas->clear(SK_ColorWHITE); |
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 | // Draw frames in a shuffled order to exercise nonlinear frame progression. |
| 186 | // The film strip will still be in time order, just drawn out of order. |
| 187 | const int order[] = { 4, 0, 3, 1, 2 }; |
| 188 | const int tiles = SK_ARRAY_COUNT(order); |
| 189 | const float dim = 1000.0f / tiles; |
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 | const float dt = 1.0f / (tiles*tiles - 1); |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 192 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 193 | for (int y : order) |
| 194 | for (int x : order) { |
| 195 | SkRect dst = {x*dim, y*dim, (x+1)*dim, (y+1)*dim}; |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 196 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 197 | SkAutoCanvasRestore _(canvas, true/*save now*/); |
| 198 | canvas->clipRect(dst, /*aa=*/true); |
| 199 | canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(animation->size()), |
| 200 | dst, |
| 201 | SkMatrix::kCenter_ScaleToFit)); |
| 202 | float t = (y*tiles + x) * dt; |
| 203 | animation->seek(t); |
| 204 | animation->render(canvas); |
| 205 | } |
| 206 | return ok; |
Mike Klein | 6253d90 | 2019-03-27 15:09:12 -0500 | [diff] [blame] | 207 | }; |
| 208 | } |
Brian Osman | ca94656 | 2019-04-10 13:51:53 -0400 | [diff] [blame] | 209 | #endif |
Mike Klein | 6253d90 | 2019-03-27 15:09:12 -0500 | [diff] [blame] | 210 | |
Mike Klein | fee0079 | 2019-11-21 16:18:47 -0600 | [diff] [blame] | 211 | static void init(Source* source, const skiatest::Test& test) { |
| 212 | source->size = {1,1}; |
| 213 | source->draw = [test](SkCanvas* canvas) { |
| 214 | struct Reporter : public skiatest::Reporter { |
| 215 | SkString msg; |
| 216 | |
| 217 | void reportFailed(const skiatest::Failure& failure) override { |
| 218 | msg = failure.toString(); |
| 219 | } |
| 220 | } reporter; |
| 221 | |
| 222 | test.run(&reporter, GrContextOptions{}); |
| 223 | |
| 224 | if (reporter.msg.isEmpty()) { |
| 225 | canvas->clear(SK_ColorGREEN); |
| 226 | return ok; |
| 227 | } |
| 228 | |
| 229 | canvas->clear(SK_ColorRED); |
| 230 | return fail(reporter.msg.c_str()); |
| 231 | }; |
| 232 | } |
| 233 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 234 | static sk_sp<SkImage> draw_with_cpu(std::function<bool(SkCanvas*)> draw, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 235 | SkImageInfo info) { |
| 236 | if (sk_sp<SkSurface> surface = SkSurface::MakeRaster(info)) { |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 237 | if (draw(surface->getCanvas())) { |
| 238 | return surface->makeImageSnapshot(); |
| 239 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -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<SkData> draw_as_skp(std::function<bool(SkCanvas*)> draw, |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 245 | SkImageInfo info) { |
| 246 | SkPictureRecorder recorder; |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 247 | if (draw(recorder.beginRecording(info.width(), info.height()))) { |
| 248 | return recorder.finishRecordingAsPicture()->serialize(); |
| 249 | } |
| 250 | return nullptr; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 251 | } |
| 252 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 253 | static sk_sp<SkData> draw_as_pdf(std::function<bool(SkCanvas*)> draw, |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 254 | SkImageInfo info, |
| 255 | SkString name) { |
| 256 | SkPDF::Metadata metadata; |
| 257 | metadata.fTitle = name; |
| 258 | metadata.fCreator = "Skia/FM"; |
| 259 | metadata.fRasterDPI = FLAGS_rasterDPI; |
| 260 | metadata.fPDFA = FLAGS_PDFA; |
| 261 | |
| 262 | SkDynamicMemoryWStream stream; |
| 263 | if (sk_sp<SkDocument> doc = SkPDF::MakeDocument(&stream, metadata)) { |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 264 | if (draw(doc->beginPage(info.width(), info.height()))) { |
| 265 | doc->endPage(); |
| 266 | doc->close(); |
| 267 | return stream.detachAsData(); |
| 268 | } |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 269 | } |
| 270 | return nullptr; |
| 271 | } |
| 272 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 273 | static sk_sp<SkImage> draw_with_gpu(std::function<bool(SkCanvas*)> draw, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 274 | SkImageInfo info, |
| 275 | GrContextFactory::ContextType api, |
| 276 | GrContextFactory* factory) { |
| 277 | enum class SurfaceType { kDefault, kBackendTexture, kBackendRenderTarget }; |
| 278 | const FlagOption<SurfaceType> kSurfaceTypes[] = { |
| 279 | { "default", SurfaceType::kDefault }, |
| 280 | { "betex" , SurfaceType::kBackendTexture }, |
| 281 | { "bert" , SurfaceType::kBackendRenderTarget }, |
| 282 | }; |
| 283 | SurfaceType surfaceType; |
| 284 | if (!parse_flag(FLAGS_surf, "surf", kSurfaceTypes, &surfaceType)) { |
| 285 | return nullptr; |
| 286 | } |
| 287 | |
Chris Dalton | b3c9745 | 2019-06-25 20:07:56 -0600 | [diff] [blame] | 288 | auto overrides = GrContextFactory::ContextOverrides::kNone; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 289 | if (!FLAGS_stencils) { overrides |= GrContextFactory::ContextOverrides::kAvoidStencilBuffers; } |
| 290 | |
| 291 | GrContext* context = factory->getContextInfo(api, overrides) |
| 292 | .grContext(); |
| 293 | |
| 294 | uint32_t flags = FLAGS_dit ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag |
| 295 | : 0; |
| 296 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 297 | |
| 298 | sk_sp<SkSurface> surface; |
| 299 | GrBackendTexture backendTexture; |
| 300 | GrBackendRenderTarget backendRT; |
| 301 | |
| 302 | switch (surfaceType) { |
| 303 | case SurfaceType::kDefault: |
| 304 | surface = SkSurface::MakeRenderTarget(context, |
| 305 | SkBudgeted::kNo, |
| 306 | info, |
| 307 | FLAGS_samples, |
| 308 | &props); |
| 309 | break; |
| 310 | |
| 311 | case SurfaceType::kBackendTexture: |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 312 | backendTexture = context->createBackendTexture(info.width(), |
| 313 | info.height(), |
| 314 | info.colorType(), |
| 315 | GrMipMapped::kNo, |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 316 | GrRenderable::kYes, |
| 317 | GrProtected::kNo); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 318 | surface = SkSurface::MakeFromBackendTexture(context, |
| 319 | backendTexture, |
| 320 | kTopLeft_GrSurfaceOrigin, |
| 321 | FLAGS_samples, |
| 322 | info.colorType(), |
| 323 | info.refColorSpace(), |
| 324 | &props); |
| 325 | break; |
| 326 | |
| 327 | case SurfaceType::kBackendRenderTarget: |
| 328 | backendRT = context->priv().getGpu() |
| 329 | ->createTestingOnlyBackendRenderTarget(info.width(), |
| 330 | info.height(), |
| 331 | SkColorTypeToGrColorType(info.colorType())); |
| 332 | surface = SkSurface::MakeFromBackendRenderTarget(context, |
| 333 | backendRT, |
| 334 | kBottomLeft_GrSurfaceOrigin, |
| 335 | info.colorType(), |
| 336 | info.refColorSpace(), |
| 337 | &props); |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | if (!surface) { |
| 342 | fprintf(stderr, "Could not create GPU surface.\n"); |
| 343 | return nullptr; |
| 344 | } |
| 345 | |
| 346 | if (FLAGS_preAbandonGpuContext) { |
| 347 | factory->abandonContexts(); |
| 348 | } |
| 349 | |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 350 | sk_sp<SkImage> image; |
| 351 | if (draw(surface->getCanvas())) { |
| 352 | image = surface->makeImageSnapshot(); |
| 353 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 354 | |
| 355 | if (FLAGS_abandonGpuContext) { |
| 356 | factory->abandonContexts(); |
| 357 | } else if (FLAGS_releaseAndAbandonGpuContext) { |
| 358 | factory->releaseResourcesAndAbandonContexts(); |
| 359 | } |
| 360 | |
| 361 | if (!context->abandoned()) { |
| 362 | surface.reset(); |
| 363 | if (backendTexture.isValid()) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 364 | context->deleteBackendTexture(backendTexture); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 365 | } |
| 366 | if (backendRT.isValid()) { |
| 367 | context->priv().getGpu()->deleteTestingOnlyBackendRenderTarget(backendRT); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | return image; |
| 372 | } |
| 373 | |
Mike Klein | 2a57e79 | 2020-01-14 12:47:52 -0600 | [diff] [blame] | 374 | extern bool gUseSkVMBlitter; |
Mike Klein | 31bfa83 | 2020-01-22 11:36:40 -0600 | [diff] [blame] | 375 | extern bool gSkVMJITViaDylib; |
Mike Klein | 2a57e79 | 2020-01-14 12:47:52 -0600 | [diff] [blame] | 376 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 377 | int main(int argc, char** argv) { |
| 378 | CommandLineFlags::Parse(argc, argv); |
Mike Klein | bf15b66 | 2019-04-15 11:32:16 -0500 | [diff] [blame] | 379 | SetupCrashHandler(); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 380 | |
| 381 | if (FLAGS_cpuDetect) { |
| 382 | SkGraphics::Init(); |
| 383 | } |
Mike Klein | 31bfa83 | 2020-01-22 11:36:40 -0600 | [diff] [blame] | 384 | gUseSkVMBlitter = FLAGS_skvm; |
| 385 | gSkVMJITViaDylib = FLAGS_dylib; |
Mike Klein | 2a57e79 | 2020-01-14 12:47:52 -0600 | [diff] [blame] | 386 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 387 | initializeEventTracingForTools(); |
| 388 | ToolUtils::SetDefaultFontMgr(); |
| 389 | SetAnalyticAAFromCommonFlags(); |
| 390 | |
| 391 | GrContextOptions baseOptions; |
| 392 | SetCtxOptionsFromCommonFlags(&baseOptions); |
| 393 | |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 394 | sk_gpu_test::MemoryCache memoryCache; |
| 395 | if (!FLAGS_writeShaders.isEmpty()) { |
| 396 | baseOptions.fPersistentCache = &memoryCache; |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 397 | baseOptions.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kBackendSource; |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 398 | } |
| 399 | |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 400 | SkTHashMap<SkString, skiagm::GMFactory> gm_factories; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 401 | for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) { |
Hal Canary | edda565 | 2019-08-05 10:28:09 -0400 | [diff] [blame] | 402 | std::unique_ptr<skiagm::GM> gm{factory()}; |
Mike Klein | fee0079 | 2019-11-21 16:18:47 -0600 | [diff] [blame] | 403 | if (FLAGS_listGMs) { |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 404 | fprintf(stdout, "%s\n", gm->getName()); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 405 | } else { |
| 406 | gm_factories.set(SkString{gm->getName()}, factory); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 407 | } |
| 408 | } |
Mike Klein | fee0079 | 2019-11-21 16:18:47 -0600 | [diff] [blame] | 409 | |
| 410 | SkTHashMap<SkString, const skiatest::Test*> tests; |
| 411 | for (const skiatest::Test& test : skiatest::TestRegistry::Range()) { |
| 412 | if (test.needsGpu) { |
| 413 | continue; // TODO |
| 414 | } |
| 415 | if (FLAGS_listTests) { |
| 416 | fprintf(stdout, "%s\n", test.name); |
| 417 | } else { |
| 418 | tests.set(SkString{test.name}, &test); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if (FLAGS_listGMs || FLAGS_listTests) { |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 423 | return 0; |
| 424 | } |
Mike Klein | fee0079 | 2019-11-21 16:18:47 -0600 | [diff] [blame] | 425 | if (FLAGS_sources.isEmpty()) { |
| 426 | fprintf(stderr, "Please give me something to run using -s/--sources!\n"); |
| 427 | return 1; |
| 428 | } |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 429 | |
| 430 | SkTArray<Source> sources; |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 431 | for (const SkString& name : FLAGS_sources) { |
| 432 | Source* source = &sources.push_back(); |
| 433 | |
| 434 | if (skiagm::GMFactory* factory = gm_factories.find(name)) { |
Hal Canary | edda565 | 2019-08-05 10:28:09 -0400 | [diff] [blame] | 435 | std::shared_ptr<skiagm::GM> gm{(*factory)()}; |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 436 | source->name = name; |
Ben Wagner | 406ff50 | 2019-08-12 16:39:24 -0400 | [diff] [blame] | 437 | init(source, std::move(gm)); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 438 | continue; |
| 439 | } |
| 440 | |
Mike Klein | fee0079 | 2019-11-21 16:18:47 -0600 | [diff] [blame] | 441 | if (const skiatest::Test** test = tests.find(name)) { |
| 442 | source->name = name; |
| 443 | init(source, **test); |
| 444 | continue; |
| 445 | } |
| 446 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 447 | if (sk_sp<SkData> blob = SkData::MakeFromFileName(name.c_str())) { |
| 448 | source->name = SkOSPath::Basename(name.c_str()); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 449 | |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 450 | if (name.endsWith(".skp")) { |
| 451 | if (sk_sp<SkPicture> pic = SkPicture::MakeFromData(blob.get())) { |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 452 | init(source, pic); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 453 | continue; |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 454 | } |
| 455 | } else if (name.endsWith(".svg")) { |
| 456 | SkMemoryStream stream{blob}; |
| 457 | if (sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(stream)) { |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 458 | init(source, svg); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 459 | continue; |
Mike Klein | ce90d6f | 2019-03-29 11:14:14 -0500 | [diff] [blame] | 460 | } |
Brian Osman | ca94656 | 2019-04-10 13:51:53 -0400 | [diff] [blame] | 461 | } |
| 462 | #if defined(SK_ENABLE_SKOTTIE) |
| 463 | else if (name.endsWith(".json")) { |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 464 | const SkString dir = SkOSPath::Dirname(name.c_str()); |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 465 | if (sk_sp<skottie::Animation> animation = skottie::Animation::Builder() |
Brian Osman | 849f4d6 | 2019-11-26 08:58:26 -0500 | [diff] [blame] | 466 | .setResourceProvider(skresources::FileResourceProvider::Make(dir)) |
Mike Klein | 176da0b | 2019-04-03 07:44:24 -0400 | [diff] [blame] | 467 | .make((const char*)blob->data(), blob->size())) { |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 468 | init(source, animation); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 469 | continue; |
Mike Klein | 2292426 | 2019-04-02 14:14:56 -0400 | [diff] [blame] | 470 | } |
Brian Osman | ca94656 | 2019-04-10 13:51:53 -0400 | [diff] [blame] | 471 | } |
| 472 | #endif |
| 473 | else if (std::shared_ptr<SkCodec> codec = SkCodec::MakeFromData(blob)) { |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 474 | init(source, codec); |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 475 | continue; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 476 | } |
| 477 | } |
Mike Klein | cbe93ee | 2019-04-02 17:00:54 -0400 | [diff] [blame] | 478 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 479 | 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] | 480 | return 1; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 481 | } |
| 482 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 483 | enum NonGpuBackends { |
| 484 | kCPU_Backend = -1, |
| 485 | kSKP_Backend = -2, |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 486 | kPDF_Backend = -3, |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 487 | }; |
| 488 | const FlagOption<int> kBackends[] = { |
| 489 | { "cpu" , kCPU_Backend }, |
| 490 | { "skp" , kSKP_Backend }, |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 491 | { "pdf" , kPDF_Backend }, |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 492 | { "gl" , GrContextFactory::kGL_ContextType }, |
| 493 | { "gles" , GrContextFactory::kGLES_ContextType }, |
| 494 | { "angle_d3d9_es2" , GrContextFactory::kANGLE_D3D9_ES2_ContextType }, |
| 495 | { "angle_d3d11_es2", GrContextFactory::kANGLE_D3D11_ES2_ContextType }, |
| 496 | { "angle_d3d11_es3", GrContextFactory::kANGLE_D3D11_ES3_ContextType }, |
| 497 | { "angle_gl_es2" , GrContextFactory::kANGLE_GL_ES2_ContextType }, |
| 498 | { "angle_gl_es3" , GrContextFactory::kANGLE_GL_ES3_ContextType }, |
| 499 | { "commandbuffer" , GrContextFactory::kCommandBuffer_ContextType }, |
| 500 | { "vk" , GrContextFactory::kVulkan_ContextType }, |
| 501 | { "mtl" , GrContextFactory::kMetal_ContextType }, |
| 502 | { "mock" , GrContextFactory::kMock_ContextType }, |
| 503 | }; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 504 | const FlagOption<SkColorType> kColorTypes[] = { |
Mike Klein | f7eb054 | 2020-02-11 12:19:08 -0600 | [diff] [blame] | 505 | { "a8", kAlpha_8_SkColorType }, |
| 506 | { "g8", kGray_8_SkColorType }, |
| 507 | { "565", kRGB_565_SkColorType }, |
| 508 | { "4444", kARGB_4444_SkColorType }, |
| 509 | { "8888", kN32_SkColorType }, |
| 510 | { "888x", kRGB_888x_SkColorType }, |
| 511 | { "1010102", kRGBA_1010102_SkColorType }, |
| 512 | { "101010x", kRGB_101010x_SkColorType }, |
| 513 | { "bgra1010102", kBGRA_1010102_SkColorType }, |
| 514 | { "bgr101010x", kBGR_101010x_SkColorType }, |
| 515 | { "f16norm", kRGBA_F16Norm_SkColorType }, |
| 516 | { "f16", kRGBA_F16_SkColorType }, |
| 517 | { "f32", kRGBA_F32_SkColorType }, |
| 518 | { "rgba", kRGBA_8888_SkColorType }, |
| 519 | { "bgra", kBGRA_8888_SkColorType }, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 520 | }; |
| 521 | const FlagOption<SkAlphaType> kAlphaTypes[] = { |
| 522 | { "premul", kPremul_SkAlphaType }, |
| 523 | { "unpremul", kUnpremul_SkAlphaType }, |
| 524 | }; |
| 525 | const FlagOption<skcms_Matrix3x3> kGamuts[] = { |
| 526 | { "srgb", SkNamedGamut::kSRGB }, |
Mike Klein | b147ace | 2020-01-16 11:11:06 -0600 | [diff] [blame] | 527 | { "p3", SkNamedGamut::kDisplayP3 }, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 528 | { "rec2020", SkNamedGamut::kRec2020 }, |
| 529 | { "adobe", SkNamedGamut::kAdobeRGB }, |
| 530 | { "narrow", gNarrow_toXYZD50}, |
| 531 | }; |
| 532 | const FlagOption<skcms_TransferFunction> kTransferFunctions[] = { |
| 533 | { "srgb" , SkNamedTransferFn::kSRGB }, |
Hal Canary | be67a17 | 2019-05-24 10:13:36 -0400 | [diff] [blame] | 534 | { "rec2020", SkNamedTransferFn::kRec2020 }, |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 535 | { "2.2" , SkNamedTransferFn::k2Dot2 }, |
| 536 | { "linear" , SkNamedTransferFn::kLinear }, |
| 537 | }; |
| 538 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 539 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 540 | int backend; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 541 | SkColorType ct; |
| 542 | SkAlphaType at; |
| 543 | skcms_Matrix3x3 gamut; |
| 544 | skcms_TransferFunction tf; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 545 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 546 | if (!parse_flag(FLAGS_backend, "backend", kBackends , &backend) || |
| 547 | !parse_flag(FLAGS_ct , "ct" , kColorTypes , &ct) || |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 548 | !parse_flag(FLAGS_at , "at" , kAlphaTypes , &at) || |
| 549 | !parse_flag(FLAGS_gamut , "gamut" , kGamuts , &gamut) || |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 550 | !parse_flag(FLAGS_tf , "tf" , kTransferFunctions, &tf)) { |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 551 | return 1; |
| 552 | } |
| 553 | |
Mike Klein | 4daf637 | 2019-04-03 11:19:45 -0400 | [diff] [blame] | 554 | sk_sp<SkColorSpace> cs = FLAGS_legacy ? nullptr |
| 555 | : SkColorSpace::MakeRGB(tf,gamut); |
| 556 | const SkImageInfo unsized_info = SkImageInfo::Make(0,0, ct,at,cs); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 557 | |
Jim Van Verth | 8a9a371 | 2019-05-31 10:49:12 -0400 | [diff] [blame] | 558 | AutoreleasePool pool; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 559 | for (auto source : sources) { |
| 560 | const auto start = std::chrono::steady_clock::now(); |
Mike Klein | 832d3c5 | 2019-04-02 15:23:46 -0400 | [diff] [blame] | 561 | fprintf(stdout, "%50s", source.name.c_str()); |
Mike Klein | bf15b66 | 2019-04-15 11:32:16 -0500 | [diff] [blame] | 562 | fflush(stdout); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 563 | |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 564 | const SkImageInfo info = unsized_info.makeDimensions(source.size); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 565 | |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 566 | auto draw = [&source](SkCanvas* canvas) { |
| 567 | Result result = source.draw(canvas); |
| 568 | switch (result.status) { |
| 569 | case Result::Ok: break; |
| 570 | case Result::Skip: return false; |
| 571 | case Result::Fail: |
Mike Klein | bf15b66 | 2019-04-15 11:32:16 -0500 | [diff] [blame] | 572 | SK_ABORT(result.failure.c_str()); |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 573 | } |
| 574 | return true; |
| 575 | }; |
| 576 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 577 | GrContextOptions options = baseOptions; |
| 578 | source.tweak(&options); |
| 579 | GrContextFactory factory(options); // N.B. factory must outlive image |
| 580 | |
| 581 | sk_sp<SkImage> image; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 582 | sk_sp<SkData> blob; |
| 583 | const char* ext = ".png"; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 584 | switch (backend) { |
| 585 | case kCPU_Backend: |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 586 | image = draw_with_cpu(draw, info); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 587 | break; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 588 | case kSKP_Backend: |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 589 | blob = draw_as_skp(draw, info); |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 590 | ext = ".skp"; |
| 591 | break; |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 592 | case kPDF_Backend: |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 593 | blob = draw_as_pdf(draw, info, source.name); |
Mike Klein | c245bd9 | 2019-03-27 14:31:09 -0500 | [diff] [blame] | 594 | ext = ".pdf"; |
| 595 | break; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 596 | default: |
Mike Klein | a833cff | 2019-04-09 11:52:40 -0500 | [diff] [blame] | 597 | image = draw_with_gpu(draw, info, (GrContextFactory::ContextType)backend, &factory); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 598 | break; |
| 599 | } |
| 600 | |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 601 | if (!image && !blob) { |
Mike Klein | 832d3c5 | 2019-04-02 15:23:46 -0400 | [diff] [blame] | 602 | fprintf(stdout, "\tskipped\n"); |
Mike Klein | a5c2717 | 2019-04-02 10:26:48 -0400 | [diff] [blame] | 603 | continue; |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 604 | } |
| 605 | |
Mike Klein | ed2cec4 | 2019-11-01 10:51:01 -0500 | [diff] [blame] | 606 | // We read back a bitmap even when --quick is set and we won't use it, |
| 607 | // to keep us honest about deferred work, flushing pipelines, etc. |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 608 | SkBitmap bitmap; |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 609 | if (image && !image->asLegacyBitmap(&bitmap)) { |
Mike Klein | bf15b66 | 2019-04-15 11:32:16 -0500 | [diff] [blame] | 610 | SK_ABORT("SkImage::asLegacyBitmap() failed."); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 611 | } |
| 612 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 613 | SkString md5; |
Mike Klein | ed2cec4 | 2019-11-01 10:51:01 -0500 | [diff] [blame] | 614 | if (!FLAGS_quick) { |
| 615 | HashAndEncode hashAndEncode{bitmap}; |
| 616 | { |
| 617 | SkMD5 hash; |
| 618 | if (image) { |
| 619 | hashAndEncode.write(&hash); |
| 620 | } else { |
| 621 | hash.write(blob->data(), blob->size()); |
Mike Klein | 9b46209 | 2019-03-27 13:52:35 -0500 | [diff] [blame] | 622 | } |
Mike Klein | ed2cec4 | 2019-11-01 10:51:01 -0500 | [diff] [blame] | 623 | |
| 624 | SkMD5::Digest digest = hash.finish(); |
| 625 | for (int i = 0; i < 16; i++) { |
| 626 | md5.appendf("%02x", digest.data[i]); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if (!FLAGS_writePath.isEmpty()) { |
| 631 | sk_mkdir(FLAGS_writePath[0]); |
| 632 | SkString path = SkStringPrintf("%s/%s%s", |
| 633 | FLAGS_writePath[0], source.name.c_str(), ext); |
| 634 | |
| 635 | if (image) { |
| 636 | if (!hashAndEncode.writePngTo(path.c_str(), md5.c_str(), |
| 637 | FLAGS_key, FLAGS_properties)) { |
| 638 | SK_ABORT("Could not write .png."); |
| 639 | } |
| 640 | } else { |
| 641 | SkFILEWStream file(path.c_str()); |
| 642 | file.write(blob->data(), blob->size()); |
| 643 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 644 | } |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 645 | } |
| 646 | |
Mike Klein | 832d3c5 | 2019-04-02 15:23:46 -0400 | [diff] [blame] | 647 | const auto elapsed = std::chrono::steady_clock::now() - start; |
| 648 | fprintf(stdout, "\t%s\t%7dms\n", |
| 649 | md5.c_str(), |
| 650 | (int)std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count()); |
Jim Van Verth | 8a9a371 | 2019-05-31 10:49:12 -0400 | [diff] [blame] | 651 | pool.drain(); |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 652 | } |
| 653 | |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 654 | if (!FLAGS_writeShaders.isEmpty()) { |
| 655 | sk_mkdir(FLAGS_writeShaders[0]); |
| 656 | GrBackendApi api = |
| 657 | GrContextFactory::ContextTypeBackend((GrContextFactory::ContextType)backend); |
| 658 | memoryCache.writeShadersToDisk(FLAGS_writeShaders[0], api); |
| 659 | |
| 660 | } |
| 661 | |
Mike Klein | 735c7ba | 2019-03-25 12:57:58 -0500 | [diff] [blame] | 662 | return 0; |
| 663 | } |