Mike Klein | f5d1a55 | 2017-03-25 12:32:22 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 8 | #include "SkImage.h" |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 9 | #include "SkOSFile.h" |
Mike Klein | f5d1a55 | 2017-03-25 12:32:22 -0400 | [diff] [blame] | 10 | #include "SkPictureRecorder.h" |
Mike Klein | 39748b2 | 2017-05-07 10:52:30 -0400 | [diff] [blame] | 11 | #include "SkPngEncoder.h" |
Mike Klein | 62669ad | 2017-05-09 16:03:07 -0400 | [diff] [blame] | 12 | #include "Timer.h" |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 13 | #include "ok.h" |
Mike Klein | 62669ad | 2017-05-09 16:03:07 -0400 | [diff] [blame] | 14 | #include <chrono> |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 15 | #include <regex> |
Mike Klein | f5d1a55 | 2017-03-25 12:32:22 -0400 | [diff] [blame] | 16 | |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 17 | static std::unique_ptr<Src> proxy(Src* original, std::function<Status(SkCanvas*)> fn) { |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 18 | struct : Src { |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 19 | Src* original; |
| 20 | std::function<Status(SkCanvas*)> fn; |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 21 | |
| 22 | std::string name() override { return original->name(); } |
| 23 | SkISize size() override { return original->size(); } |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 24 | Status draw(SkCanvas* canvas) override { return fn(canvas); } |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 25 | } src; |
| 26 | src.original = original; |
| 27 | src.fn = fn; |
| 28 | return move_unique(src); |
| 29 | } |
| 30 | |
Mike Klein | f5d1a55 | 2017-03-25 12:32:22 -0400 | [diff] [blame] | 31 | struct ViaPic : Dst { |
| 32 | std::unique_ptr<Dst> target; |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 33 | bool rtree = false; |
Mike Klein | f5d1a55 | 2017-03-25 12:32:22 -0400 | [diff] [blame] | 34 | |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 35 | static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) { |
| 36 | ViaPic via; |
| 37 | via.target = std::move(dst); |
| 38 | if (options("bbh") == "rtree") { via.rtree = true; } |
| 39 | return move_unique(via); |
Mike Klein | f5d1a55 | 2017-03-25 12:32:22 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 42 | Status draw(Src* src) override { |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 43 | SkRTreeFactory factory; |
| 44 | SkPictureRecorder rec; |
| 45 | rec.beginRecording(SkRect::MakeSize(SkSize::Make(src->size())), |
| 46 | rtree ? &factory : nullptr); |
Mike Klein | f5d1a55 | 2017-03-25 12:32:22 -0400 | [diff] [blame] | 47 | |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 48 | for (auto status = src->draw(rec.getRecordingCanvas()); status != Status::OK; ) { |
| 49 | return status; |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 50 | } |
Mike Klein | f5d1a55 | 2017-03-25 12:32:22 -0400 | [diff] [blame] | 51 | auto pic = rec.finishRecordingAsPicture(); |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 52 | |
| 53 | return target->draw(proxy(src, [=](SkCanvas* canvas) { |
| 54 | pic->playback(canvas); |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 55 | return Status::OK; |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 56 | }).get()); |
| 57 | } |
| 58 | |
| 59 | sk_sp<SkImage> image() override { |
| 60 | return target->image(); |
Mike Klein | f5d1a55 | 2017-03-25 12:32:22 -0400 | [diff] [blame] | 61 | } |
| 62 | }; |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 63 | static Register via_pic{"via_pic", "record then play back an SkPicture", ViaPic::Create}; |
| 64 | |
| 65 | struct Png : Dst { |
| 66 | std::unique_ptr<Dst> target; |
| 67 | std::string dir; |
| 68 | |
| 69 | static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) { |
| 70 | Png via; |
| 71 | via.target = std::move(dst); |
| 72 | via.dir = options("dir", "ok"); |
| 73 | return move_unique(via); |
| 74 | } |
| 75 | |
| 76 | Status draw(Src* src) override { |
| 77 | for (auto status = target->draw(src); status != Status::OK; ) { |
| 78 | return status; |
| 79 | } |
| 80 | |
Mike Klein | 39748b2 | 2017-05-07 10:52:30 -0400 | [diff] [blame] | 81 | SkBitmap bm; |
| 82 | SkPixmap pm; |
| 83 | if (!target->image()->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode) || |
| 84 | !bm.peekPixels(&pm)) { |
| 85 | return Status::Failed; |
| 86 | } |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 87 | |
| 88 | sk_mkdir(dir.c_str()); |
Mike Klein | 39748b2 | 2017-05-07 10:52:30 -0400 | [diff] [blame] | 89 | SkFILEWStream dst{(dir + "/" + src->name() + ".png").c_str()}; |
| 90 | |
| 91 | SkPngEncoder::Options options; |
| 92 | options.fFilterFlags = SkPngEncoder::FilterFlag::kNone; |
| 93 | options.fZLibLevel = 1; |
| 94 | options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect |
| 95 | : SkTransferFunctionBehavior::kIgnore; |
| 96 | return SkPngEncoder::Encode(&dst, pm, options) ? Status::OK |
| 97 | : Status::Failed; |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | sk_sp<SkImage> image() override { |
| 101 | return target->image(); |
| 102 | } |
| 103 | }; |
| 104 | static Register png{"png", "dump PNGs to dir=ok" , Png::Create}; |
| 105 | |
| 106 | struct Filter : Dst { |
| 107 | std::unique_ptr<Dst> target; |
| 108 | std::regex match, search; |
| 109 | |
| 110 | static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) { |
| 111 | Filter via; |
| 112 | via.target = std::move(dst); |
| 113 | via.match = options("match", ".*"); |
| 114 | via.search = options("search", ".*"); |
| 115 | return move_unique(via); |
| 116 | } |
| 117 | |
| 118 | Status draw(Src* src) override { |
| 119 | auto name = src->name(); |
| 120 | if (!std::regex_match (name, match) || |
| 121 | !std::regex_search(name, search)) { |
| 122 | return Status::Skipped; |
| 123 | } |
| 124 | return target->draw(src); |
| 125 | } |
| 126 | |
| 127 | sk_sp<SkImage> image() override { |
| 128 | return target->image(); |
| 129 | } |
| 130 | }; |
Mike Klein | 452a7a2 | 2017-05-09 18:04:04 -0400 | [diff] [blame] | 131 | static Register filter{"filter", |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 132 | "run only srcs matching match=.* exactly and search=.* somewhere", |
| 133 | Filter::Create}; |
Mike Klein | 62669ad | 2017-05-09 16:03:07 -0400 | [diff] [blame] | 134 | |
| 135 | struct Time : Dst { |
| 136 | std::unique_ptr<Dst> target; |
| 137 | |
| 138 | static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) { |
| 139 | Time via; |
| 140 | via.target = std::move(dst); |
| 141 | return move_unique(via); |
| 142 | } |
| 143 | |
| 144 | Status draw(Src* src) override { |
| 145 | auto start = std::chrono::steady_clock::now(); |
| 146 | Status status = target->draw(src); |
| 147 | std::chrono::duration<double, std::milli> elapsed = std::chrono::steady_clock::now() |
| 148 | - start; |
| 149 | |
| 150 | auto msg = HumanizeMs(elapsed.count()); |
| 151 | ok_log(msg.c_str()); |
| 152 | return status; |
| 153 | } |
| 154 | |
| 155 | sk_sp<SkImage> image() override { |
| 156 | return target->image(); |
| 157 | } |
| 158 | }; |
Mike Klein | 452a7a2 | 2017-05-09 18:04:04 -0400 | [diff] [blame] | 159 | static Register _time{"time", |
Mike Klein | 62669ad | 2017-05-09 16:03:07 -0400 | [diff] [blame] | 160 | "print wall run time", |
| 161 | Time::Create}; |