commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +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 | |
commit-bot@chromium.org | 8400b23 | 2014-04-28 15:30:02 +0000 | [diff] [blame] | 8 | #include "BenchTimer.h" |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 9 | #include "SkCommandLineFlags.h" |
| 10 | #include "SkForceLinking.h" |
| 11 | #include "SkGraphics.h" |
| 12 | #include "SkOSFile.h" |
| 13 | #include "SkPicture.h" |
commit-bot@chromium.org | 8fe89d3 | 2014-05-09 15:00:10 +0000 | [diff] [blame] | 14 | #include "SkPictureRecorder.h" |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 15 | #include "SkRecording.h" |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 16 | #include "SkStream.h" |
| 17 | #include "SkString.h" |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 18 | |
| 19 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 20 | |
| 21 | DEFINE_string2(skps, r, "skps", "Directory containing SKPs to read and re-record."); |
| 22 | DEFINE_int32(loops, 10, "Number of times to play back each SKP."); |
| 23 | DEFINE_bool(skr, false, "Play via SkRecord instead of SkPicture."); |
| 24 | DEFINE_int32(tile, 1000000000, "Simulated tile size."); |
commit-bot@chromium.org | 5da5b59 | 2014-04-21 14:59:59 +0000 | [diff] [blame] | 25 | DEFINE_string(match, "", "The usual filters on file names of SKPs to bench."); |
commit-bot@chromium.org | 172eb1b | 2014-04-28 19:41:17 +0000 | [diff] [blame] | 26 | DEFINE_string(timescale, "ms", "Print times in ms, us, or ns"); |
| 27 | |
| 28 | static double scale_time(double ms) { |
| 29 | if (FLAGS_timescale.contains("us")) ms *= 1000; |
| 30 | if (FLAGS_timescale.contains("ns")) ms *= 1000000; |
| 31 | return ms; |
| 32 | } |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 33 | |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 34 | static SkPicture* rerecord_with_tilegrid(SkPicture& src) { |
| 35 | SkTileGridFactory::TileGridInfo info; |
| 36 | info.fTileInterval.set(FLAGS_tile, FLAGS_tile); |
| 37 | info.fMargin.setEmpty(); |
| 38 | info.fOffset.setZero(); |
| 39 | SkTileGridFactory factory(info); |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 40 | |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 41 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 42 | src.draw(recorder.beginRecording(src.width(), src.height(), &factory)); |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 43 | return recorder.endRecording(); |
| 44 | } |
| 45 | |
| 46 | static EXPERIMENTAL::SkPlayback* rerecord_with_skr(SkPicture& src) { |
| 47 | EXPERIMENTAL::SkRecording recording(src.width(), src.height()); |
| 48 | src.draw(recording.canvas()); |
| 49 | return recording.releasePlayback(); |
| 50 | } |
| 51 | |
| 52 | static void bench(SkPMColor* scratch, SkPicture& src, const char* name) { |
| 53 | SkAutoTUnref<SkPicture> picture(rerecord_with_tilegrid(src)); |
| 54 | SkAutoTDelete<EXPERIMENTAL::SkPlayback> record(rerecord_with_skr(src)); |
commit-bot@chromium.org | ad8ce57 | 2014-04-21 15:03:36 +0000 | [diff] [blame] | 55 | |
commit-bot@chromium.org | d6489c9 | 2014-04-11 19:06:46 +0000 | [diff] [blame] | 56 | SkAutoTDelete<SkCanvas> canvas(SkCanvas::NewRasterDirectN32(src.width(), |
| 57 | src.height(), |
| 58 | scratch, |
| 59 | src.width() * sizeof(SkPMColor))); |
commit-bot@chromium.org | 4bffdf2 | 2014-04-11 16:13:54 +0000 | [diff] [blame] | 60 | canvas->clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAGS_tile))); |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 61 | |
commit-bot@chromium.org | 8400b23 | 2014-04-28 15:30:02 +0000 | [diff] [blame] | 62 | BenchTimer timer; |
| 63 | timer.start(); |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 64 | for (int i = 0; i < FLAGS_loops; i++) { |
| 65 | if (FLAGS_skr) { |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 66 | record->draw(canvas.get()); |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 67 | } else { |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 68 | picture->draw(canvas.get()); |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
commit-bot@chromium.org | 8400b23 | 2014-04-28 15:30:02 +0000 | [diff] [blame] | 71 | timer.end(); |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 72 | |
commit-bot@chromium.org | 8400b23 | 2014-04-28 15:30:02 +0000 | [diff] [blame] | 73 | const double msPerLoop = timer.fCpu / (double)FLAGS_loops; |
commit-bot@chromium.org | 172eb1b | 2014-04-28 19:41:17 +0000 | [diff] [blame] | 74 | printf("%f\t%s\n", scale_time(msPerLoop), name); |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int tool_main(int argc, char** argv); |
| 78 | int tool_main(int argc, char** argv) { |
| 79 | SkCommandLineFlags::Parse(argc, argv); |
| 80 | SkAutoGraphics autoGraphics; |
| 81 | |
| 82 | // We share a single scratch bitmap among benches to reduce the profile noise from allocation. |
| 83 | static const int kMaxArea = 209825221; // tabl_mozilla is this big. |
| 84 | SkAutoTMalloc<SkPMColor> scratch(kMaxArea); |
| 85 | |
| 86 | SkOSFile::Iter it(FLAGS_skps[0], ".skp"); |
| 87 | SkString filename; |
| 88 | bool failed = false; |
| 89 | while (it.next(&filename)) { |
commit-bot@chromium.org | 5da5b59 | 2014-04-21 14:59:59 +0000 | [diff] [blame] | 90 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, filename.c_str())) { |
| 91 | continue; |
| 92 | } |
| 93 | |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 94 | const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str()); |
| 95 | |
| 96 | SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str())); |
| 97 | if (!stream) { |
| 98 | SkDebugf("Could not read %s.\n", path.c_str()); |
| 99 | failed = true; |
| 100 | continue; |
| 101 | } |
| 102 | SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream)); |
| 103 | if (!src) { |
| 104 | SkDebugf("Could not read %s as an SkPicture.\n", path.c_str()); |
| 105 | failed = true; |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | if (src->width() * src->height() > kMaxArea) { |
| 110 | SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n", |
| 111 | path.c_str(), src->width(), src->height(), kMaxArea); |
| 112 | failed = true; |
| 113 | continue; |
| 114 | } |
| 115 | |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 116 | bench(scratch.get(), *src, filename.c_str()); |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 117 | } |
| 118 | return failed ? 1 : 0; |
| 119 | } |
| 120 | |
| 121 | #if !defined SK_BUILD_FOR_IOS |
| 122 | int main(int argc, char * const argv[]) { |
| 123 | return tool_main(argc, (char**) argv); |
| 124 | } |
| 125 | #endif |