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 | |
| 8 | #include "SkCommandLineFlags.h" |
| 9 | #include "SkForceLinking.h" |
| 10 | #include "SkGraphics.h" |
| 11 | #include "SkOSFile.h" |
| 12 | #include "SkPicture.h" |
| 13 | #include "SkRecordDraw.h" |
| 14 | #include "SkRecorder.h" |
| 15 | #include "SkStream.h" |
| 16 | #include "SkString.h" |
| 17 | #include "SkTime.h" |
| 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."); |
| 25 | |
| 26 | static void bench(SkPMColor* scratch, SkPicture& src, const char* name) { |
commit-bot@chromium.org | b17a24f | 2014-04-14 20:33:05 +0000 | [diff] [blame^] | 27 | // We don't use the public SkRecording interface here because we need kWriteOnly_Mode. |
| 28 | // (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] | 29 | SkRecord record; |
| 30 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, src.width(), src.height()); |
| 31 | src.draw(&recorder); |
| 32 | |
commit-bot@chromium.org | d6489c9 | 2014-04-11 19:06:46 +0000 | [diff] [blame] | 33 | SkAutoTDelete<SkCanvas> canvas(SkCanvas::NewRasterDirectN32(src.width(), |
| 34 | src.height(), |
| 35 | scratch, |
| 36 | src.width() * sizeof(SkPMColor))); |
commit-bot@chromium.org | 4bffdf2 | 2014-04-11 16:13:54 +0000 | [diff] [blame] | 37 | canvas->clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAGS_tile))); |
commit-bot@chromium.org | ba73d28 | 2014-04-11 15:53:39 +0000 | [diff] [blame] | 38 | |
| 39 | const SkMSec start = SkTime::GetMSecs(); |
| 40 | for (int i = 0; i < FLAGS_loops; i++) { |
| 41 | if (FLAGS_skr) { |
| 42 | SkRecordDraw(record, canvas.get()); |
| 43 | } else { |
| 44 | src.draw(canvas.get()); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | const SkMSec elapsed = SkTime::GetMSecs() - start; |
| 49 | const double msPerLoop = elapsed / (double)FLAGS_loops; |
| 50 | printf("%5.1f\t%s\n", msPerLoop, name); |
| 51 | } |
| 52 | |
| 53 | int tool_main(int argc, char** argv); |
| 54 | int tool_main(int argc, char** argv) { |
| 55 | SkCommandLineFlags::Parse(argc, argv); |
| 56 | SkAutoGraphics autoGraphics; |
| 57 | |
| 58 | // We share a single scratch bitmap among benches to reduce the profile noise from allocation. |
| 59 | static const int kMaxArea = 209825221; // tabl_mozilla is this big. |
| 60 | SkAutoTMalloc<SkPMColor> scratch(kMaxArea); |
| 61 | |
| 62 | SkOSFile::Iter it(FLAGS_skps[0], ".skp"); |
| 63 | SkString filename; |
| 64 | bool failed = false; |
| 65 | while (it.next(&filename)) { |
| 66 | const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str()); |
| 67 | |
| 68 | SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str())); |
| 69 | if (!stream) { |
| 70 | SkDebugf("Could not read %s.\n", path.c_str()); |
| 71 | failed = true; |
| 72 | continue; |
| 73 | } |
| 74 | SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream)); |
| 75 | if (!src) { |
| 76 | SkDebugf("Could not read %s as an SkPicture.\n", path.c_str()); |
| 77 | failed = true; |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | if (src->width() * src->height() > kMaxArea) { |
| 82 | SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n", |
| 83 | path.c_str(), src->width(), src->height(), kMaxArea); |
| 84 | failed = true; |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | bench(scratch.get(), *src, filename.c_str()); |
| 89 | } |
| 90 | return failed ? 1 : 0; |
| 91 | } |
| 92 | |
| 93 | #if !defined SK_BUILD_FOR_IOS |
| 94 | int main(int argc, char * const argv[]) { |
| 95 | return tool_main(argc, (char**) argv); |
| 96 | } |
| 97 | #endif |