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