commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkCommandLineFlags.h" |
| 9 | #include "SkForceLinking.h" |
| 10 | #include "SkGraphics.h" |
| 11 | #include "SkOSFile.h" |
| 12 | #include "SkPicture.h" |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 13 | #include "SkQuadTreePicture.h" |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 14 | #include "SkRecorder.h" |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | #include "SkString.h" |
commit-bot@chromium.org | e892615 | 2014-02-28 19:25:11 +0000 | [diff] [blame] | 17 | #include "SkTileGridPicture.h" |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 18 | #include "SkTime.h" |
commit-bot@chromium.org | 16182f7 | 2014-03-28 16:08:18 +0000 | [diff] [blame] | 19 | #include "LazyDecodeBitmap.h" |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 20 | |
| 21 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 22 | |
| 23 | // Just reading all the SKPs takes about 2 seconds for me, which is the same as about 100 loops of |
| 24 | // rerecording all the SKPs. So we default to --loops=900, which makes ~90% of our time spent in |
| 25 | // recording, and this should take ~20 seconds to run. |
| 26 | |
| 27 | DEFINE_string2(skps, r, "skps", "Directory containing SKPs to read and re-record."); |
| 28 | DEFINE_int32(loops, 900, "Number of times to re-record each SKP."); |
| 29 | DEFINE_int32(flags, SkPicture::kUsePathBoundsForClip_RecordingFlag, "RecordingFlags to use."); |
| 30 | DEFINE_bool(endRecording, true, "If false, don't time SkPicture::endRecording()"); |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 31 | DEFINE_int32(nullSize, 1000, "Pretend dimension of null source picture."); |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 32 | DEFINE_int32(tileGridSize, 512, "Set the tile grid size. Has no effect if bbh is not set to tilegrid."); |
| 33 | DEFINE_string(bbh, "", "Turn on the bbh and select the type, one of rtree, tilegrid, quadtree"); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 34 | DEFINE_bool(skr, false, "Record SKR instead of SKP."); |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 36 | typedef SkPicture* (*PictureFactory)(const int width, const int height, int* recordingFlags); |
| 37 | |
| 38 | static SkPicture* vanilla_factory(const int width, const int height, int* recordingFlags) { |
| 39 | return SkNEW(SkPicture); |
| 40 | } |
| 41 | |
| 42 | static SkPicture* rtree_factory(const int width, const int height, int* recordingFlags) { |
| 43 | *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag; |
| 44 | return SkNEW(SkPicture); |
| 45 | } |
| 46 | |
| 47 | static SkPicture* tilegrid_factory(const int width, const int height, int* recordingFlags) { |
| 48 | *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag; |
| 49 | SkTileGridPicture::TileGridInfo info; |
| 50 | info.fTileInterval.set(FLAGS_tileGridSize, FLAGS_tileGridSize); |
| 51 | info.fMargin.setEmpty(); |
| 52 | info.fOffset.setZero(); |
| 53 | return SkNEW_ARGS(SkTileGridPicture, (width, height, info)); |
| 54 | } |
| 55 | |
| 56 | static SkPicture* quadtree_factory(const int width, const int height, int* recordingFlags) { |
| 57 | *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag; |
| 58 | return SkNEW_ARGS(SkQuadTreePicture, (SkIRect::MakeWH(width, height))); |
| 59 | } |
| 60 | |
| 61 | static PictureFactory parse_FLAGS_bbh() { |
| 62 | if (FLAGS_bbh.isEmpty()) { return &vanilla_factory; } |
| 63 | if (FLAGS_bbh.count() != 1) { |
| 64 | SkDebugf("Multiple bbh arguments supplied.\n"); |
| 65 | return NULL; |
| 66 | } |
| 67 | if (FLAGS_bbh.contains("rtree")) { return rtree_factory; } |
| 68 | if (FLAGS_bbh.contains("tilegrid")) { return tilegrid_factory; } |
| 69 | if (FLAGS_bbh.contains("quadtree")) { return quadtree_factory; } |
| 70 | SkDebugf("Invalid bbh type %s, must be one of rtree, tilegrid, quadtree.\n", FLAGS_bbh[0]); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | static void bench_record(SkPicture* src, const char* name, PictureFactory pictureFactory) { |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 75 | const SkMSec start = SkTime::GetMSecs(); |
| 76 | const int width = src ? src->width() : FLAGS_nullSize; |
| 77 | const int height = src ? src->height() : FLAGS_nullSize; |
| 78 | |
| 79 | for (int i = 0; i < FLAGS_loops; i++) { |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 80 | if (FLAGS_skr) { |
| 81 | SkRecord record; |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame^] | 82 | SkRecorder canvas(SkRecorder::kWriteOnly_Mode, &record, width, height); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 83 | if (NULL != src) { |
| 84 | src->draw(&canvas); |
| 85 | } |
| 86 | } else { |
| 87 | int recordingFlags = FLAGS_flags; |
| 88 | SkAutoTUnref<SkPicture> dst(pictureFactory(width, height, &recordingFlags)); |
| 89 | SkCanvas* canvas = dst->beginRecording(width, height, recordingFlags); |
| 90 | if (NULL != src) { |
| 91 | src->draw(canvas); |
| 92 | } |
| 93 | if (FLAGS_endRecording) { |
| 94 | dst->endRecording(); |
| 95 | } |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 96 | } |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | const SkMSec elapsed = SkTime::GetMSecs() - start; |
| 100 | const double msPerLoop = elapsed / (double)FLAGS_loops; |
| 101 | printf("%.2g\t%s\n", msPerLoop, name); |
| 102 | } |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 103 | |
| 104 | int tool_main(int argc, char** argv); |
| 105 | int tool_main(int argc, char** argv) { |
| 106 | SkCommandLineFlags::Parse(argc, argv); |
| 107 | SkAutoGraphics autoGraphics; |
| 108 | |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 109 | PictureFactory pictureFactory = parse_FLAGS_bbh(); |
| 110 | if (pictureFactory == NULL) { |
| 111 | return 1; |
| 112 | } |
| 113 | bench_record(NULL, "NULL", pictureFactory); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 114 | if (FLAGS_skps.isEmpty()) { |
| 115 | return 0; |
| 116 | } |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 117 | |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 118 | SkOSFile::Iter it(FLAGS_skps[0], ".skp"); |
| 119 | SkString filename; |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 120 | bool failed = false; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 121 | while (it.next(&filename)) { |
| 122 | const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str()); |
| 123 | |
| 124 | SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str())); |
| 125 | if (!stream) { |
| 126 | SkDebugf("Could not read %s.\n", path.c_str()); |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 127 | failed = true; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 128 | continue; |
| 129 | } |
commit-bot@chromium.org | 16182f7 | 2014-03-28 16:08:18 +0000 | [diff] [blame] | 130 | SkAutoTUnref<SkPicture> src( |
| 131 | SkPicture::CreateFromStream(stream, sk_tools::LazyDecodeBitmap)); |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 132 | if (!src) { |
| 133 | SkDebugf("Could not read %s as an SkPicture.\n", path.c_str()); |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 134 | failed = true; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 135 | continue; |
| 136 | } |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 137 | bench_record(src, filename.c_str(), pictureFactory); |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 138 | } |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 139 | return failed ? 1 : 0; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | #if !defined SK_BUILD_FOR_IOS |
| 143 | int main(int argc, char * const argv[]) { |
| 144 | return tool_main(argc, (char**) argv); |
| 145 | } |
| 146 | #endif |