2015-11-18 19:06:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
|
|
|
#include "include/core/SkPictureRecorder.h"
|
|
|
|
#include "include/core/SkStream.h"
|
2020-08-18 21:02:52 +00:00
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "src/core/SkPicturePriv.h"
|
|
|
|
#include "src/core/SkRecord.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkRecordDraw.h"
|
|
|
|
#include "src/core/SkRecordOpts.h"
|
|
|
|
#include "src/core/SkRecorder.h"
|
|
|
|
#include "tools/flags/CommandLineFlags.h"
|
2015-11-18 19:06:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-03-21 16:31:36 +00:00
|
|
|
static DEFINE_string2(skps, r, "", ".SKPs to dump.");
|
|
|
|
static DEFINE_string(match, "", "The usual filters on file names to dump.");
|
|
|
|
static DEFINE_bool2(optimize, O, false, "Run SkRecordOptimize before dumping.");
|
|
|
|
static DEFINE_bool(optimize2, false, "Run SkRecordOptimize2 before dumping.");
|
2019-03-21 16:42:21 +00:00
|
|
|
static DEFINE_int(tile, 1000000000, "Simulated tile size.");
|
2019-03-21 16:31:36 +00:00
|
|
|
static DEFINE_bool(timeWithCommand, false,
|
|
|
|
"If true, print time next to command, else in first column.");
|
|
|
|
static DEFINE_string2(write, w, "", "Write the (optimized) picture to the named file.");
|
2015-11-18 19:06:37 +00:00
|
|
|
|
2020-08-18 21:02:52 +00:00
|
|
|
class Dumper {
|
|
|
|
public:
|
|
|
|
explicit Dumper(SkCanvas* canvas, int count)
|
|
|
|
: fDigits(0)
|
|
|
|
, fIndent(0)
|
|
|
|
, fIndex(0)
|
|
|
|
, fDraw(canvas, nullptr, nullptr, 0, nullptr)
|
|
|
|
{
|
|
|
|
while (count > 0) {
|
|
|
|
count /= 10;
|
|
|
|
fDigits++;
|
|
|
|
}
|
|
|
|
}
|
2015-11-18 19:06:37 +00:00
|
|
|
|
2020-08-18 21:02:52 +00:00
|
|
|
template <typename T>
|
|
|
|
void operator()(const T& command) {
|
|
|
|
auto start = SkTime::GetNSecs();
|
|
|
|
fDraw(command);
|
|
|
|
this->print(command, SkTime::GetNSecs() - start);
|
|
|
|
}
|
2015-11-18 19:06:37 +00:00
|
|
|
|
2020-08-18 21:02:52 +00:00
|
|
|
void operator()(const SkRecords::NoOp&) {
|
|
|
|
// Move on without printing anything.
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void print(const T& command, double ns) {
|
|
|
|
this->printNameAndTime(command, ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(const SkRecords::Restore& command, double ns) {
|
|
|
|
--fIndent;
|
|
|
|
this->printNameAndTime(command, ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(const SkRecords::Save& command, double ns) {
|
|
|
|
this->printNameAndTime(command, ns);
|
|
|
|
++fIndent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(const SkRecords::SaveLayer& command, double ns) {
|
|
|
|
this->printNameAndTime(command, ns);
|
|
|
|
++fIndent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(const SkRecords::DrawPicture& command, double ns) {
|
|
|
|
this->printNameAndTime(command, ns);
|
|
|
|
|
|
|
|
if (auto bp = SkPicturePriv::AsSkBigPicture(command.picture)) {
|
|
|
|
++fIndent;
|
|
|
|
|
|
|
|
const SkRecord& record = *bp->record();
|
|
|
|
for (int i = 0; i < record.count(); i++) {
|
|
|
|
record.visit(i, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
--fIndent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(const SkRecords::DrawAnnotation& command, double ns) {
|
|
|
|
int us = (int)(ns * 1e-3);
|
|
|
|
if (!FLAGS_timeWithCommand) {
|
|
|
|
printf("%6dus ", us);
|
|
|
|
}
|
|
|
|
printf("%*d ", fDigits, fIndex++);
|
|
|
|
for (int i = 0; i < fIndent; i++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
if (FLAGS_timeWithCommand) {
|
|
|
|
printf("%6dus ", us);
|
|
|
|
}
|
|
|
|
printf("DrawAnnotation [%g %g %g %g] %s\n",
|
|
|
|
command.rect.left(), command.rect.top(), command.rect.right(), command.rect.bottom(),
|
|
|
|
command.key.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
|
|
|
void printNameAndTime(const T& command, double ns) {
|
|
|
|
int us = (int)(ns * 1e-3);
|
|
|
|
if (!FLAGS_timeWithCommand) {
|
|
|
|
printf("%6dus ", us);
|
|
|
|
}
|
|
|
|
printf("%*d ", fDigits, fIndex++);
|
|
|
|
for (int i = 0; i < fIndent; i++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
if (FLAGS_timeWithCommand) {
|
|
|
|
printf("%6dus ", us);
|
|
|
|
}
|
|
|
|
puts(NameOf(command));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static const char* NameOf(const T&) {
|
|
|
|
#define CASE(U) case SkRecords::U##_Type: return #U;
|
|
|
|
switch (T::kType) { SK_RECORD_TYPES(CASE) }
|
|
|
|
#undef CASE
|
|
|
|
SkDEBUGFAIL("Unknown T");
|
|
|
|
return "Unknown T";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* NameOf(const SkRecords::SaveLayer&) {
|
|
|
|
return "\x1b[31;1mSaveLayer\x1b[0m"; // Bold red.
|
|
|
|
}
|
|
|
|
|
|
|
|
int fDigits;
|
|
|
|
int fIndent;
|
|
|
|
int fIndex;
|
|
|
|
SkRecords::Draw fDraw;
|
|
|
|
};
|
2015-11-18 19:06:37 +00:00
|
|
|
|
2017-02-06 17:46:20 +00:00
|
|
|
int main(int argc, char** argv) {
|
2019-03-20 15:50:33 +00:00
|
|
|
CommandLineFlags::Parse(argc, argv);
|
2015-11-18 19:06:37 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < FLAGS_skps.count(); i++) {
|
2019-03-20 15:50:33 +00:00
|
|
|
if (CommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
|
2015-11-18 19:06:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-16 13:24:20 +00:00
|
|
|
std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(FLAGS_skps[i]);
|
2015-11-18 19:06:37 +00:00
|
|
|
if (!stream) {
|
|
|
|
SkDebugf("Could not read %s.\n", FLAGS_skps[i]);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-09-16 13:24:20 +00:00
|
|
|
sk_sp<SkPicture> src(SkPicture::MakeFromStream(stream.get()));
|
2015-11-18 19:06:37 +00:00
|
|
|
if (!src) {
|
|
|
|
SkDebugf("Could not read %s as an SkPicture.\n", FLAGS_skps[i]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const int w = SkScalarCeilToInt(src->cullRect().width());
|
|
|
|
const int h = SkScalarCeilToInt(src->cullRect().height());
|
|
|
|
|
|
|
|
SkRecord record;
|
2020-08-18 21:02:52 +00:00
|
|
|
SkRecorder rec(&record, w, h);
|
|
|
|
src->playback(&rec);
|
2015-11-18 19:06:37 +00:00
|
|
|
|
|
|
|
if (FLAGS_optimize) {
|
|
|
|
SkRecordOptimize(&record);
|
|
|
|
}
|
2015-11-19 18:31:08 +00:00
|
|
|
if (FLAGS_optimize2) {
|
|
|
|
SkRecordOptimize2(&record);
|
|
|
|
}
|
2015-11-18 19:06:37 +00:00
|
|
|
|
2020-08-18 21:02:52 +00:00
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocN32Pixels(w, h);
|
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
canvas.clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile),
|
|
|
|
SkIntToScalar(FLAGS_tile)));
|
|
|
|
|
|
|
|
printf("%s %s\n", FLAGS_optimize ? "optimized" : "not-optimized", FLAGS_skps[i]);
|
|
|
|
|
|
|
|
Dumper dumper(&canvas, record.count());
|
|
|
|
for (int i = 0; i < record.count(); i++) {
|
|
|
|
record.visit(i, dumper);
|
|
|
|
}
|
2015-11-19 16:18:04 +00:00
|
|
|
|
|
|
|
if (FLAGS_write.count() > 0) {
|
|
|
|
SkPictureRecorder r;
|
|
|
|
SkRecordDraw(record,
|
|
|
|
r.beginRecording(SkRect::MakeIWH(w, h)),
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
nullptr);
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> dst(r.finishRecordingAsPicture());
|
2015-11-19 16:18:04 +00:00
|
|
|
SkFILEWStream ostream(FLAGS_write[0]);
|
|
|
|
dst->serialize(&ostream);
|
|
|
|
}
|
2015-11-18 19:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|