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