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