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-11-07 13:14:52 +00:00
|
|
|
#include "../dm/DMFontMgr.h"
|
2017-09-21 20:01:07 +00:00
|
|
|
#include "ProcStats.h"
|
2017-09-29 19:43:28 +00:00
|
|
|
#include "SkColorFilter.h"
|
2017-07-24 18:27:18 +00:00
|
|
|
#include "SkEventTracingPriv.h"
|
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-07-24 19:21:31 +00:00
|
|
|
#include "SkTraceEvent.h"
|
2017-09-21 20:01:07 +00:00
|
|
|
#include "SkTypeface.h"
|
2017-05-09 20:03:07 +00:00
|
|
|
#include "Timer.h"
|
2017-03-29 16:41:13 +00:00
|
|
|
#include "ok.h"
|
2017-09-21 20:01:07 +00:00
|
|
|
#include "sk_tool_utils.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-07-24 19:21:31 +00:00
|
|
|
TRACE_EVENT0("ok", TRACE_FUNC);
|
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-09-21 20:01:07 +00:00
|
|
|
for (Status status = src->draw(rec.getRecordingCanvas()); status != Status::OK; ) {
|
2017-03-29 16:41:13 +00:00
|
|
|
return status;
|
2017-03-25 19:53:14 +00:00
|
|
|
}
|
2017-09-21 20:01:07 +00:00
|
|
|
sk_sp<SkPicture> 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};
|
|
|
|
|
2017-09-21 20:01:07 +00:00
|
|
|
// When deserializing, we need to hook this to intercept "Toy Liberation ..."
|
|
|
|
// typefaces and return our portable test typeface.
|
|
|
|
extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char[], SkFontStyle);
|
|
|
|
|
|
|
|
struct ViaSkp : Dst {
|
|
|
|
std::unique_ptr<Dst> target;
|
|
|
|
bool rtree = false;
|
|
|
|
|
|
|
|
static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) {
|
|
|
|
gCreateTypefaceDelegate = [](const char name[], SkFontStyle style) -> sk_sp<SkTypeface> {
|
|
|
|
if (name == strstr(name, "Toy Liberation")) {
|
|
|
|
return sk_tool_utils::create_portable_typeface(name, style);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
ViaSkp via;
|
|
|
|
via.target = std::move(dst);
|
|
|
|
if (options("bbh") == "rtree") { via.rtree = true; }
|
|
|
|
return move_unique(via);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status draw(Src* src) override {
|
|
|
|
TRACE_EVENT0("ok", TRACE_FUNC);
|
|
|
|
SkRTreeFactory factory;
|
|
|
|
SkPictureRecorder rec;
|
|
|
|
rec.beginRecording(SkRect::MakeSize(SkSize::Make(src->size())),
|
|
|
|
rtree ? &factory : nullptr);
|
|
|
|
|
|
|
|
for (Status status = src->draw(rec.getRecordingCanvas()); status != Status::OK; ) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
sk_sp<SkPicture> pic = rec.finishRecordingAsPicture();
|
|
|
|
|
|
|
|
// Serialize and deserialize.
|
|
|
|
pic = SkPicture::MakeFromData(pic->serialize().get());
|
|
|
|
|
|
|
|
return target->draw(proxy(src, [=](SkCanvas* canvas) {
|
|
|
|
pic->playback(canvas);
|
|
|
|
return Status::OK;
|
|
|
|
}).get());
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> image() override {
|
|
|
|
return target->image();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static Register via_skp{"via_skp", "serialize and deserialize an .skp", ViaSkp::Create};
|
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
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 {
|
2017-07-24 19:21:31 +00:00
|
|
|
TRACE_EVENT0("ok", TRACE_FUNC);
|
2017-03-29 16:41:13 +00:00
|
|
|
for (auto status = target->draw(src); status != Status::OK; ) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-05-07 14:52:30 +00:00
|
|
|
SkBitmap bm;
|
2017-09-29 19:43:28 +00:00
|
|
|
if (!target->image()->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode)) {
|
|
|
|
return Status::Failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SkPngEncoder can't encode A8 .pngs, and even if it could, they'd be a pain to look at.
|
|
|
|
if (bm.colorType() == kAlpha_8_SkColorType) {
|
|
|
|
SkPaint paint;
|
|
|
|
SkScalar alpha_to_opaque_gray[20] = {
|
|
|
|
0,0,0,1, 0, // red = alpha
|
|
|
|
0,0,0,1, 0, // green = alpha
|
|
|
|
0,0,0,1, 0, // blue = alpha
|
|
|
|
0,0,0,0,255, // alpha = 255
|
|
|
|
};
|
|
|
|
paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(alpha_to_opaque_gray));
|
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
|
|
|
|
|
|
|
SkBitmap dst;
|
|
|
|
dst.allocN32Pixels(bm.width(), bm.height(), /*isOpaque=*/true);
|
|
|
|
SkCanvas canvas(dst);
|
|
|
|
canvas.drawBitmap(bm, 0,0, &paint);
|
|
|
|
|
|
|
|
bm = dst;
|
|
|
|
}
|
|
|
|
|
2017-05-07 14:52:30 +00:00
|
|
|
SkPixmap pm;
|
2017-09-29 19:43:28 +00:00
|
|
|
if (!bm.peekPixels(&pm)) {
|
2017-05-07 14:52:30 +00:00
|
|
|
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;
|
|
|
|
|
2017-07-24 17:20:30 +00:00
|
|
|
if (status != Status::Skipped) {
|
|
|
|
auto msg = HumanizeMs(elapsed.count());
|
|
|
|
ok_log(msg.c_str());
|
|
|
|
}
|
2017-05-09 20:03:07 +00:00
|
|
|
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);
|
|
|
|
|
2017-07-24 17:20:30 +00:00
|
|
|
if (status != Status::Skipped) {
|
|
|
|
auto msg = SkStringPrintf("%dMB", sk_tools::getMaxResidentSetSizeMB());
|
|
|
|
ok_log(msg.c_str());
|
|
|
|
}
|
2017-06-19 19:05:31 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> image() override {
|
|
|
|
return target->image();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static Register memory{"memory", "print process maximum memory usage", Memory::Create};
|
2017-07-24 18:27:18 +00:00
|
|
|
|
|
|
|
struct Trace : Dst {
|
|
|
|
std::unique_ptr<Dst> target;
|
|
|
|
std::string trace_mode;
|
|
|
|
|
|
|
|
static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) {
|
|
|
|
Trace via;
|
|
|
|
via.target = std::move(dst);
|
|
|
|
via.trace_mode = options("mode", "trace.json");
|
|
|
|
return move_unique(via);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status draw(Src* src) override {
|
2017-11-07 13:14:52 +00:00
|
|
|
static SkOnce once;
|
|
|
|
once([&] { initializeEventTracingForTools(trace_mode.c_str()); });
|
2017-07-24 18:27:18 +00:00
|
|
|
return target->draw(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> image() override {
|
|
|
|
return target->image();
|
|
|
|
}
|
|
|
|
};
|
2017-11-07 13:14:52 +00:00
|
|
|
static Register trace{"trace",
|
|
|
|
"enable tracing in mode=atrace, mode=debugf, or mode=trace.json",
|
|
|
|
Trace::Create};
|
|
|
|
|
|
|
|
extern sk_sp<SkFontMgr> (*gSkFontMgr_DefaultFactory)();
|
|
|
|
|
|
|
|
struct PortableFonts : Dst {
|
|
|
|
std::unique_ptr<Dst> target;
|
|
|
|
|
|
|
|
static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) {
|
|
|
|
PortableFonts via;
|
|
|
|
via.target = std::move(dst);
|
|
|
|
return move_unique(via);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status draw(Src* src) override {
|
|
|
|
static SkOnce once;
|
2017-11-14 15:34:48 +00:00
|
|
|
once([]{ gSkFontMgr_DefaultFactory = &DM::MakeFontMgr; });
|
2017-11-07 13:14:52 +00:00
|
|
|
return target->draw(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> image() override {
|
|
|
|
return target->image();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static Register portable_fonts{"portable_fonts",
|
|
|
|
"use DM::FontMgr to make fonts more portable",
|
|
|
|
PortableFonts::Create};
|