diff --git a/BUILD.gn b/BUILD.gn index 2f1e2cb80b..2a167c7c7a 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -2180,10 +2180,7 @@ if (skia_enable_tools) { } test_app("dump_record") { - sources = [ - "tools/DumpRecord.cpp", - "tools/dump_record.cpp", - ] + sources = [ "tools/dump_record.cpp" ] deps = [ ":flags", ":skia", diff --git a/tools/DumpRecord.cpp b/tools/DumpRecord.cpp deleted file mode 100644 index fb2fbb0eb4..0000000000 --- a/tools/DumpRecord.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright 2014 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include - -#include "src/core/SkPicturePriv.h" -#include "src/core/SkRecord.h" -#include "src/core/SkRecordDraw.h" - -#include "include/core/SkTime.h" -#include "tools/DumpRecord.h" - -namespace { - -class Dumper { -public: - explicit Dumper(SkCanvas* canvas, int count, bool timeWithCommand) - : fDigits(0) - , fIndent(0) - , fIndex(0) - , fDraw(canvas, nullptr, nullptr, 0, nullptr) - , fTimeWithCommand(timeWithCommand) { - while (count > 0) { - count /= 10; - fDigits++; - } - } - - template - void operator()(const T& command) { - auto start = SkTime::GetNSecs(); - fDraw(command); - this->print(command, SkTime::GetNSecs() - start); - } - - void operator()(const SkRecords::NoOp&) { - // Move on without printing anything. - } - - template - 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; - } - } - -#if 1 - void print(const SkRecords::DrawAnnotation& command, double ns) { - int us = (int)(ns * 1e-3); - if (!fTimeWithCommand) { - printf("%6dus ", us); - } - printf("%*d ", fDigits, fIndex++); - for (int i = 0; i < fIndent; i++) { - printf(" "); - } - if (fTimeWithCommand) { - 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()); - } -#endif - -private: - template - void printNameAndTime(const T& command, double ns) { - int us = (int)(ns * 1e-3); - if (!fTimeWithCommand) { - printf("%6dus ", us); - } - printf("%*d ", fDigits, fIndex++); - for (int i = 0; i < fIndent; i++) { - printf(" "); - } - if (fTimeWithCommand) { - printf("%6dus ", us); - } - puts(NameOf(command)); - } - - template - 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; - const bool fTimeWithCommand; -}; - -} // namespace - -void DumpRecord(const SkRecord& record, - SkCanvas* canvas, - bool timeWithCommand) { - Dumper dumper(canvas, record.count(), timeWithCommand); - for (int i = 0; i < record.count(); i++) { - record.visit(i, dumper); - } -} diff --git a/tools/DumpRecord.h b/tools/DumpRecord.h deleted file mode 100644 index 47097472b1..0000000000 --- a/tools/DumpRecord.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2014 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ -#ifndef DumpRecord_DEFINED -#define DumpRecord_DEFINED - -class SkRecord; -class SkCanvas; - -/** - * Draw the record to the supplied canvas via SkRecords::Draw, while - * printing each draw command and run time in microseconds to stdout. - * - * @param timeWithCommand If true, print time next to command, else in - * first column. - */ -void DumpRecord(const SkRecord& record, - SkCanvas* canvas, - bool timeWithCommand); - -#endif // DumpRecord_DEFINED diff --git a/tools/dump_record.cpp b/tools/dump_record.cpp index abfb12f645..9f168a788d 100644 --- a/tools/dump_record.cpp +++ b/tools/dump_record.cpp @@ -9,12 +9,13 @@ #include "include/core/SkPicture.h" #include "include/core/SkPictureRecorder.h" #include "include/core/SkStream.h" +#include "include/core/SkTime.h" +#include "src/core/SkPicturePriv.h" +#include "src/core/SkRecord.h" #include "src/core/SkRecordDraw.h" #include "src/core/SkRecordOpts.h" #include "src/core/SkRecorder.h" -#include "tools/DumpRecord.h" #include "tools/flags/CommandLineFlags.h" - #include static DEFINE_string2(skps, r, "", ".SKPs to dump."); @@ -26,17 +27,118 @@ 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."); -static void dump(const char* name, int w, int h, const SkRecord& record) { - SkBitmap bitmap; - bitmap.allocN32Pixels(w, h); - SkCanvas canvas(bitmap); - canvas.clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), - SkIntToScalar(FLAGS_tile))); +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++; + } + } - printf("%s %s\n", FLAGS_optimize ? "optimized" : "not-optimized", name); + template + void operator()(const T& command) { + auto start = SkTime::GetNSecs(); + fDraw(command); + this->print(command, SkTime::GetNSecs() - start); + } - DumpRecord(record, &canvas, FLAGS_timeWithCommand); -} + void operator()(const SkRecords::NoOp&) { + // Move on without printing anything. + } + + template + 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 + 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 + 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; +}; int main(int argc, char** argv) { CommandLineFlags::Parse(argc, argv); @@ -60,8 +162,8 @@ int main(int argc, char** argv) { const int h = SkScalarCeilToInt(src->cullRect().height()); SkRecord record; - SkRecorder canvas(&record, w, h); - src->playback(&canvas); + SkRecorder rec(&record, w, h); + src->playback(&rec); if (FLAGS_optimize) { SkRecordOptimize(&record); @@ -70,7 +172,18 @@ int main(int argc, char** argv) { SkRecordOptimize2(&record); } - dump(FLAGS_skps[i], w, h, record); + 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); + } if (FLAGS_write.count() > 0) { SkPictureRecorder r;