2017-03-25 16:32:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2017-05-03 19:16:58 +00:00
|
|
|
#include "SkImage.h"
|
2017-03-29 16:41:13 +00:00
|
|
|
#include "SkOSFile.h"
|
2017-03-25 16:32:22 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2017-05-07 14:52:30 +00:00
|
|
|
#include "SkPngEncoder.h"
|
2017-06-19 19:05:31 +00:00
|
|
|
#include "ProcStats.h"
|
2017-05-09 20:03:07 +00:00
|
|
|
#include "Timer.h"
|
2017-03-29 16:41:13 +00:00
|
|
|
#include "ok.h"
|
2017-05-09 20:03:07 +00:00
|
|
|
#include <chrono>
|
2017-03-29 16:41:13 +00:00
|
|
|
#include <regex>
|
2017-03-25 16:32:22 +00:00
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
static std::unique_ptr<Src> proxy(Src* original, std::function<Status(SkCanvas*)> fn) {
|
2017-03-25 19:53:14 +00:00
|
|
|
struct : Src {
|
2017-03-29 16:41:13 +00:00
|
|
|
Src* original;
|
|
|
|
std::function<Status(SkCanvas*)> fn;
|
2017-03-25 19:53:14 +00:00
|
|
|
|
|
|
|
std::string name() override { return original->name(); }
|
|
|
|
SkISize size() override { return original->size(); }
|
2017-03-29 16:41:13 +00:00
|
|
|
Status draw(SkCanvas* canvas) override { return fn(canvas); }
|
2017-03-25 19:53:14 +00:00
|
|
|
} src;
|
|
|
|
src.original = original;
|
|
|
|
src.fn = fn;
|
|
|
|
return move_unique(src);
|
|
|
|
}
|
|
|
|
|
2017-03-25 16:32:22 +00:00
|
|
|
struct ViaPic : Dst {
|
|
|
|
std::unique_ptr<Dst> target;
|
2017-03-25 19:53:14 +00:00
|
|
|
bool rtree = false;
|
2017-03-25 16:32:22 +00:00
|
|
|
|
2017-03-25 19:53:14 +00:00
|
|
|
static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) {
|
|
|
|
ViaPic via;
|
|
|
|
via.target = std::move(dst);
|
|
|
|
if (options("bbh") == "rtree") { via.rtree = true; }
|
|
|
|
return move_unique(via);
|
|
|
|
}
|
2017-03-25 16:32:22 +00:00
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
Status draw(Src* src) override {
|
2017-03-25 19:53:14 +00:00
|
|
|
SkRTreeFactory factory;
|
|
|
|
SkPictureRecorder rec;
|
|
|
|
rec.beginRecording(SkRect::MakeSize(SkSize::Make(src->size())),
|
|
|
|
rtree ? &factory : nullptr);
|
2017-03-25 16:32:22 +00:00
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
for (auto status = src->draw(rec.getRecordingCanvas()); status != Status::OK; ) {
|
|
|
|
return status;
|
2017-03-25 19:53:14 +00:00
|
|
|
}
|
|
|
|
auto pic = rec.finishRecordingAsPicture();
|
2017-03-25 16:32:22 +00:00
|
|
|
|
2017-03-25 19:53:14 +00:00
|
|
|
return target->draw(proxy(src, [=](SkCanvas* canvas) {
|
|
|
|
pic->playback(canvas);
|
2017-03-29 16:41:13 +00:00
|
|
|
return Status::OK;
|
2017-03-25 19:53:14 +00:00
|
|
|
}).get());
|
2017-03-25 16:32:22 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 19:53:14 +00:00
|
|
|
sk_sp<SkImage> image() override {
|
|
|
|
return target->image();
|
2017-03-25 16:32:22 +00:00
|
|
|
}
|
|
|
|
};
|
2017-03-29 16:41:13 +00:00
|
|
|
static Register via_pic{"via_pic", "record then play back an SkPicture", ViaPic::Create};
|
|
|
|
|
|
|
|
struct Png : Dst {
|
|
|
|
std::unique_ptr<Dst> target;
|
|
|
|
std::string dir;
|
|
|
|
|
|
|
|
static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) {
|
|
|
|
Png via;
|
|
|
|
via.target = std::move(dst);
|
|
|
|
via.dir = options("dir", "ok");
|
|
|
|
return move_unique(via);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status draw(Src* src) override {
|
|
|
|
for (auto status = target->draw(src); status != Status::OK; ) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-05-07 14:52:30 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
SkPixmap pm;
|
|
|
|
if (!target->image()->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode) ||
|
|
|
|
!bm.peekPixels(&pm)) {
|
|
|
|
return Status::Failed;
|
|
|
|
}
|
2017-03-29 16:41:13 +00:00
|
|
|
|
|
|
|
sk_mkdir(dir.c_str());
|
2017-05-07 14:52:30 +00:00
|
|
|
SkFILEWStream dst{(dir + "/" + src->name() + ".png").c_str()};
|
|
|
|
|
|
|
|
SkPngEncoder::Options options;
|
|
|
|
options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
|
|
|
|
options.fZLibLevel = 1;
|
|
|
|
options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
|
|
|
|
: SkTransferFunctionBehavior::kIgnore;
|
|
|
|
return SkPngEncoder::Encode(&dst, pm, options) ? Status::OK
|
|
|
|
: Status::Failed;
|
2017-03-29 16:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> image() override {
|
|
|
|
return target->image();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static Register png{"png", "dump PNGs to dir=ok" , Png::Create};
|
|
|
|
|
|
|
|
struct Filter : Dst {
|
|
|
|
std::unique_ptr<Dst> target;
|
|
|
|
std::regex match, search;
|
|
|
|
|
|
|
|
static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) {
|
|
|
|
Filter via;
|
|
|
|
via.target = std::move(dst);
|
|
|
|
via.match = options("match", ".*");
|
|
|
|
via.search = options("search", ".*");
|
|
|
|
return move_unique(via);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status draw(Src* src) override {
|
|
|
|
auto name = src->name();
|
|
|
|
if (!std::regex_match (name, match) ||
|
|
|
|
!std::regex_search(name, search)) {
|
|
|
|
return Status::Skipped;
|
|
|
|
}
|
|
|
|
return target->draw(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> image() override {
|
|
|
|
return target->image();
|
|
|
|
}
|
|
|
|
};
|
2017-05-09 22:04:04 +00:00
|
|
|
static Register filter{"filter",
|
2017-03-29 16:41:13 +00:00
|
|
|
"run only srcs matching match=.* exactly and search=.* somewhere",
|
|
|
|
Filter::Create};
|
2017-05-09 20:03:07 +00:00
|
|
|
|
|
|
|
struct Time : Dst {
|
|
|
|
std::unique_ptr<Dst> target;
|
|
|
|
|
|
|
|
static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) {
|
|
|
|
Time via;
|
|
|
|
via.target = std::move(dst);
|
|
|
|
return move_unique(via);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status draw(Src* src) override {
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
Status status = target->draw(src);
|
|
|
|
std::chrono::duration<double, std::milli> elapsed = std::chrono::steady_clock::now()
|
|
|
|
- start;
|
|
|
|
|
|
|
|
auto msg = HumanizeMs(elapsed.count());
|
|
|
|
ok_log(msg.c_str());
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> image() override {
|
|
|
|
return target->image();
|
|
|
|
}
|
|
|
|
};
|
2017-06-19 19:05:31 +00:00
|
|
|
static Register _time{"time", "print wall run time", Time::Create};
|
|
|
|
|
|
|
|
struct Memory : Dst {
|
|
|
|
std::unique_ptr<Dst> target;
|
|
|
|
|
|
|
|
static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) {
|
|
|
|
Memory via;
|
|
|
|
via.target = std::move(dst);
|
|
|
|
return move_unique(via);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status draw(Src* src) override {
|
|
|
|
Status status = target->draw(src);
|
|
|
|
|
|
|
|
auto msg = SkStringPrintf("%dMB", sk_tools::getMaxResidentSetSizeMB());
|
|
|
|
ok_log(msg.c_str());
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> image() override {
|
|
|
|
return target->image();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static Register memory{"memory", "print process maximum memory usage", Memory::Create};
|