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 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 36 | typedef SkPictureFactory* (*PictureFactory)(int* recordingFlags); |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 37 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 38 | static SkPictureFactory* vanilla_factory(int* recordingFlags) { |
| 39 | return NULL; |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 40 | } |
| 41 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 42 | static SkPictureFactory* rtree_factory(int* recordingFlags) { |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 43 | *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 44 | return NULL; |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 45 | } |
| 46 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 47 | static SkPictureFactory* tilegrid_factory(int* recordingFlags) { |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 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(); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 53 | return SkNEW_ARGS(SkTileGridPictureFactory, (info)); |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 54 | } |
| 55 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 56 | static SkPictureFactory* quadtree_factory(int* recordingFlags) { |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 57 | *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 58 | return SkNEW(SkQuadTreePictureFactory); |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static PictureFactory parse_FLAGS_bbh() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 62 | if (FLAGS_bbh.isEmpty()) { |
| 63 | return &vanilla_factory; |
| 64 | } |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 65 | if (FLAGS_bbh.count() != 1) { |
| 66 | SkDebugf("Multiple bbh arguments supplied.\n"); |
| 67 | return NULL; |
| 68 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 69 | if (FLAGS_bbh.contains("rtree")) { |
| 70 | return rtree_factory; |
| 71 | } |
| 72 | if (FLAGS_bbh.contains("tilegrid")) { |
| 73 | return tilegrid_factory; |
| 74 | } |
| 75 | if (FLAGS_bbh.contains("quadtree")) { |
| 76 | return quadtree_factory; |
| 77 | } |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 78 | SkDebugf("Invalid bbh type %s, must be one of rtree, tilegrid, quadtree.\n", FLAGS_bbh[0]); |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | 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] | 83 | const SkMSec start = SkTime::GetMSecs(); |
| 84 | const int width = src ? src->width() : FLAGS_nullSize; |
| 85 | const int height = src ? src->height() : FLAGS_nullSize; |
| 86 | |
| 87 | for (int i = 0; i < FLAGS_loops; i++) { |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 88 | if (FLAGS_skr) { |
| 89 | SkRecord record; |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 90 | SkRecorder canvas(SkRecorder::kWriteOnly_Mode, &record, width, height); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 91 | if (NULL != src) { |
| 92 | src->draw(&canvas); |
| 93 | } |
| 94 | } else { |
| 95 | int recordingFlags = FLAGS_flags; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 96 | SkAutoTUnref<SkPictureFactory> factory(pictureFactory(&recordingFlags)); |
| 97 | SkPictureRecorder recorder(factory); |
| 98 | SkCanvas* canvas = recorder.beginRecording(width, height, recordingFlags); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 99 | if (NULL != src) { |
| 100 | src->draw(canvas); |
| 101 | } |
| 102 | if (FLAGS_endRecording) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 103 | SkAutoTUnref<SkPicture> dst(recorder.endRecording()); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 104 | } |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 105 | } |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | const SkMSec elapsed = SkTime::GetMSecs() - start; |
| 109 | const double msPerLoop = elapsed / (double)FLAGS_loops; |
| 110 | printf("%.2g\t%s\n", msPerLoop, name); |
| 111 | } |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 112 | |
| 113 | int tool_main(int argc, char** argv); |
| 114 | int tool_main(int argc, char** argv) { |
| 115 | SkCommandLineFlags::Parse(argc, argv); |
| 116 | SkAutoGraphics autoGraphics; |
| 117 | |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 118 | PictureFactory pictureFactory = parse_FLAGS_bbh(); |
| 119 | if (pictureFactory == NULL) { |
| 120 | return 1; |
| 121 | } |
| 122 | bench_record(NULL, "NULL", pictureFactory); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 123 | if (FLAGS_skps.isEmpty()) { |
| 124 | return 0; |
| 125 | } |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 126 | |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 127 | SkOSFile::Iter it(FLAGS_skps[0], ".skp"); |
| 128 | SkString filename; |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 129 | bool failed = false; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 130 | while (it.next(&filename)) { |
| 131 | const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str()); |
| 132 | |
| 133 | SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str())); |
| 134 | if (!stream) { |
| 135 | SkDebugf("Could not read %s.\n", path.c_str()); |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 136 | failed = true; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 137 | continue; |
| 138 | } |
commit-bot@chromium.org | 16182f7 | 2014-03-28 16:08:18 +0000 | [diff] [blame] | 139 | SkAutoTUnref<SkPicture> src( |
| 140 | SkPicture::CreateFromStream(stream, sk_tools::LazyDecodeBitmap)); |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 141 | if (!src) { |
| 142 | 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] | 143 | failed = true; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 144 | continue; |
| 145 | } |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 146 | bench_record(src, filename.c_str(), pictureFactory); |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 147 | } |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 148 | return failed ? 1 : 0; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | #if !defined SK_BUILD_FOR_IOS |
| 152 | int main(int argc, char * const argv[]) { |
| 153 | return tool_main(argc, (char**) argv); |
| 154 | } |
| 155 | #endif |