| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 | 9 |
| 10 #include "BenchTimer.h" |
| 10 #include "LazyDecodeBitmap.h" | 11 #include "LazyDecodeBitmap.h" |
| 11 #include "SkCommandLineFlags.h" | 12 #include "SkCommandLineFlags.h" |
| 12 #include "SkGraphics.h" | 13 #include "SkGraphics.h" |
| 13 #include "SkOSFile.h" | 14 #include "SkOSFile.h" |
| 14 #include "SkPicture.h" | 15 #include "SkPicture.h" |
| 15 #include "SkRecord.h" | 16 #include "SkRecord.h" |
| 17 #include "SkRecordDraw.h" |
| 16 #include "SkRecordOpts.h" | 18 #include "SkRecordOpts.h" |
| 17 #include "SkRecorder.h" | 19 #include "SkRecorder.h" |
| 18 #include "SkStream.h" | 20 #include "SkStream.h" |
| 19 | 21 |
| 20 DEFINE_string2(skps, r, "", ".SKPs to dump."); | 22 DEFINE_string2(skps, r, "", ".SKPs to dump."); |
| 21 DEFINE_string(match, "", "The usual filters on file names to dump."); | 23 DEFINE_string(match, "", "The usual filters on file names to dump."); |
| 22 DEFINE_bool2(optimize, O, false, "Run SkRecordOptimize before dumping."); | 24 DEFINE_bool2(optimize, O, false, "Run SkRecordOptimize before dumping."); |
| 25 DEFINE_int32(tile, 1000000000, "Simulated tile size."); |
| 26 DEFINE_bool(timeWithCommand, false, "If true, print time next to command, else i
n first column."); |
| 23 | 27 |
| 24 class Dumper { | 28 class Dumper { |
| 25 public: | 29 public: |
| 26 Dumper() : fIndent(0) {} | 30 explicit Dumper(SkCanvas* canvas, int count) : fDigits(0), fIndent(0), fDraw
(canvas) { |
| 31 while (count > 0) { |
| 32 count /= 10; |
| 33 fDigits++; |
| 34 } |
| 35 } |
| 36 |
| 37 unsigned index() const { return fDraw.index(); } |
| 38 void next() { fDraw.next(); } |
| 27 | 39 |
| 28 template <typename T> | 40 template <typename T> |
| 29 void operator()(const T& command) { | 41 void operator()(const T& command) { |
| 30 this->printIndentedName(command); | 42 BenchTimer timer; |
| 43 timer.start(); |
| 44 fDraw(command); |
| 45 timer.end(); |
| 46 |
| 47 this->print(command, timer.fCpu); |
| 31 } | 48 } |
| 32 | 49 |
| 33 void operator()(const SkRecords::Restore& command) { | 50 void operator()(const SkRecords::NoOp&) { |
| 34 --fIndent; | 51 // Move on without printing anything. |
| 35 this->printIndentedName(command); | |
| 36 } | 52 } |
| 37 | 53 |
| 38 void operator()(const SkRecords::Save& command) { | 54 template <typename T> |
| 39 this->printIndentedName(command); | 55 void print(const T& command, double time) { |
| 56 this->printNameAndTime(command, time); |
| 57 } |
| 58 |
| 59 void print(const SkRecords::Restore& command, double time) { |
| 60 --fIndent; |
| 61 this->printNameAndTime(command, time); |
| 62 } |
| 63 |
| 64 void print(const SkRecords::Save& command, double time) { |
| 65 this->printNameAndTime(command, time); |
| 40 ++fIndent; | 66 ++fIndent; |
| 41 } | 67 } |
| 42 | 68 |
| 43 void operator()(const SkRecords::SaveLayer& command) { | 69 void print(const SkRecords::SaveLayer& command, double time) { |
| 44 this->printIndentedName(command); | 70 this->printNameAndTime(command, time); |
| 45 ++fIndent; | 71 ++fIndent; |
| 46 } | 72 } |
| 47 | 73 |
| 48 private: | 74 private: |
| 49 template <typename T> | 75 template <typename T> |
| 50 void printIndentedName(const T& command) { | 76 void printNameAndTime(const T& command, double time) { |
| 77 if (!FLAGS_timeWithCommand) { |
| 78 printf("%6.1f ", time * 1000); |
| 79 } |
| 80 printf("%*d ", fDigits, fDraw.index()); |
| 51 for (int i = 0; i < fIndent; i++) { | 81 for (int i = 0; i < fIndent; i++) { |
| 52 putchar('\t'); | 82 putchar('\t'); |
| 53 } | 83 } |
| 84 if (FLAGS_timeWithCommand) { |
| 85 printf("%6.1f ", time * 1000); |
| 86 } |
| 54 puts(NameOf(command)); | 87 puts(NameOf(command)); |
| 55 } | 88 } |
| 56 | 89 |
| 57 template <typename T> | 90 template <typename T> |
| 58 static const char* NameOf(const T&) { | 91 static const char* NameOf(const T&) { |
| 59 #define CASE(U) case SkRecords::U##_Type: return #U; | 92 #define CASE(U) case SkRecords::U##_Type: return #U; |
| 60 switch(T::kType) { SK_RECORD_TYPES(CASE); } | 93 switch(T::kType) { SK_RECORD_TYPES(CASE); } |
| 61 #undef CASE | 94 #undef CASE |
| 62 SkDEBUGFAIL("Unknown T"); | 95 SkDEBUGFAIL("Unknown T"); |
| 63 return "Unknown T"; | 96 return "Unknown T"; |
| 64 } | 97 } |
| 65 | 98 |
| 66 static const char* NameOf(const SkRecords::SaveLayer&) { | 99 static const char* NameOf(const SkRecords::SaveLayer&) { |
| 67 return "\x1b[31;1mSaveLayer\x1b[0m"; // Bold red. | 100 return "\x1b[31;1mSaveLayer\x1b[0m"; // Bold red. |
| 68 } | 101 } |
| 69 | 102 |
| 103 int fDigits; |
| 70 int fIndent; | 104 int fIndent; |
| 105 SkRecords::Draw fDraw; |
| 71 }; | 106 }; |
| 72 | 107 |
| 73 | 108 |
| 74 static void dump(const char* name, const SkRecord& record) { | 109 static void dump(const char* name, int w, int h, const SkRecord& record) { |
| 75 Dumper dumper; | 110 SkBitmap bitmap; |
| 76 | 111 bitmap.allocN32Pixels(w, h); |
| 77 unsigned count = record.count(); | 112 SkCanvas canvas(bitmap); |
| 78 int digits = 0; | 113 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAG
S_tile))); |
| 79 while (count > 0) { | |
| 80 count /= 10; | |
| 81 digits++; | |
| 82 } | |
| 83 | 114 |
| 84 printf("%s %s\n", FLAGS_optimize ? "optimized" : "not-optimized", name); | 115 printf("%s %s\n", FLAGS_optimize ? "optimized" : "not-optimized", name); |
| 85 for (unsigned i = 0; i < record.count(); i++) { | 116 |
| 86 printf("%*d ", digits, i); | 117 for (Dumper dumper(&canvas, record.count()); dumper.index() < record.count()
; dumper.next()) { |
| 87 record.visit<void>(i, dumper); | 118 record.visit<void>(dumper.index(), dumper); |
| 88 } | 119 } |
| 89 } | 120 } |
| 90 | 121 |
| 91 int tool_main(int argc, char** argv); | 122 int tool_main(int argc, char** argv); |
| 92 int tool_main(int argc, char** argv) { | 123 int tool_main(int argc, char** argv) { |
| 93 SkCommandLineFlags::Parse(argc, argv); | 124 SkCommandLineFlags::Parse(argc, argv); |
| 94 SkAutoGraphics ag; | 125 SkAutoGraphics ag; |
| 95 | 126 |
| 96 for (int i = 0; i < FLAGS_skps.count(); i++) { | 127 for (int i = 0; i < FLAGS_skps.count(); i++) { |
| 97 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) { | 128 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) { |
| 98 continue; | 129 continue; |
| 99 } | 130 } |
| 100 | 131 |
| 101 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(FLAGS_skps[i])); | 132 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(FLAGS_skps[i])); |
| 102 if (!stream) { | 133 if (!stream) { |
| 103 SkDebugf("Could not read %s.\n", FLAGS_skps[i]); | 134 SkDebugf("Could not read %s.\n", FLAGS_skps[i]); |
| 104 exit(1); | 135 exit(1); |
| 105 } | 136 } |
| 106 SkAutoTUnref<SkPicture> src( | 137 SkAutoTUnref<SkPicture> src( |
| 107 SkPicture::CreateFromStream(stream, sk_tools::LazyDecodeBitmap))
; | 138 SkPicture::CreateFromStream(stream, sk_tools::LazyDecodeBitmap))
; |
| 108 if (!src) { | 139 if (!src) { |
| 109 SkDebugf("Could not read %s as an SkPicture.\n", FLAGS_skps[i]); | 140 SkDebugf("Could not read %s as an SkPicture.\n", FLAGS_skps[i]); |
| 110 exit(1); | 141 exit(1); |
| 111 } | 142 } |
| 143 const int w = src->width(), h = src->height(); |
| 112 | 144 |
| 113 SkRecord record; | 145 SkRecord record; |
| 114 SkRecorder canvas(SkRecorder::kWriteOnly_Mode, &record, src->width(), sr
c->height()); | 146 SkRecorder canvas(SkRecorder::kWriteOnly_Mode, &record, w, h); |
| 115 src->draw(&canvas); | 147 src->draw(&canvas); |
| 116 | 148 |
| 149 |
| 117 if (FLAGS_optimize) { | 150 if (FLAGS_optimize) { |
| 118 SkRecordOptimize(&record); | 151 SkRecordOptimize(&record); |
| 119 } | 152 } |
| 120 | 153 |
| 121 dump(FLAGS_skps[i], record); | 154 dump(FLAGS_skps[i], w, h, record); |
| 122 } | 155 } |
| 123 | 156 |
| 124 return 0; | 157 return 0; |
| 125 } | 158 } |
| 126 | 159 |
| 127 #if !defined SK_BUILD_FOR_IOS | 160 #if !defined SK_BUILD_FOR_IOS |
| 128 int main(int argc, char * const argv[]) { | 161 int main(int argc, char * const argv[]) { |
| 129 return tool_main(argc, (char**) argv); | 162 return tool_main(argc, (char**) argv); |
| 130 } | 163 } |
| 131 #endif | 164 #endif |
| OLD | NEW |