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