commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame] | 8 | #include "Timer.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 9 | #include "Benchmark.h" |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 10 | #include "LazyDecodeBitmap.h" |
| 11 | #include "PictureBenchmark.h" |
| 12 | #include "PictureRenderer.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 13 | #include "SkCommandLineFlags.h" |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 14 | #include "SkForceLinking.h" |
| 15 | #include "SkGraphics.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkString.h" |
commit-bot@chromium.org | 49a07ad | 2013-07-22 19:28:40 +0000 | [diff] [blame] | 18 | #include "SkTArray.h" |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 19 | |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 20 | typedef sk_tools::PictureRenderer::BBoxHierarchyType BBoxType; |
| 21 | static const int kBBoxTypeCount = sk_tools::PictureRenderer::kLast_BBoxHierarchyType + 1; |
commit-bot@chromium.org | db34f33 | 2014-03-11 14:23:50 +0000 | [diff] [blame] | 22 | |
| 23 | |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 24 | DEFINE_string2(skps, r, "", "The list of SKPs to benchmark."); |
commit-bot@chromium.org | f16d797 | 2014-03-11 19:12:44 +0000 | [diff] [blame] | 25 | DEFINE_string(bb_types, "", "The set of bbox types to test. If empty, all are tested. " |
mtklein | 2a65a23 | 2014-08-26 14:07:04 -0700 | [diff] [blame] | 26 | "Should be one or more of none, rtree, tilegrid."); |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 27 | DEFINE_int32(record, 100, "Number of times to record each SKP."); |
| 28 | DEFINE_int32(playback, 1, "Number of times to playback each SKP."); |
| 29 | DEFINE_int32(tilesize, 256, "The size of a tile."); |
commit-bot@chromium.org | db34f33 | 2014-03-11 14:23:50 +0000 | [diff] [blame] | 30 | |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 31 | struct Measurement { |
commit-bot@chromium.org | db34f33 | 2014-03-11 14:23:50 +0000 | [diff] [blame] | 32 | SkString fName; |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 33 | double fRecordAverage[kBBoxTypeCount]; |
| 34 | double fPlaybackAverage[kBBoxTypeCount]; |
commit-bot@chromium.org | db34f33 | 2014-03-11 14:23:50 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 37 | const char* kBBoxHierarchyTypeNames[kBBoxTypeCount] = { |
| 38 | "none", // kNone_BBoxHierarchyType |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 39 | "rtree", // kRTree_BBoxHierarchyType |
| 40 | "tilegrid", // kTileGrid_BBoxHierarchyType |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static SkPicture* pic_from_path(const char path[]) { |
| 44 | SkFILEStream stream(path); |
| 45 | if (!stream.isValid()) { |
| 46 | SkDebugf("-- Can't open '%s'\n", path); |
| 47 | return NULL; |
| 48 | } |
| 49 | return SkPicture::CreateFromStream(&stream, &sk_tools::LazyDecodeBitmap); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * This function is the sink to which all work ends up going. |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 54 | * @param renderer The renderer to use to perform the work. |
| 55 | * To measure rendering, use a TiledPictureRenderer. |
| 56 | * To measure recording, use a RecordPictureRenderer. |
| 57 | * @param bBoxType The bounding box hierarchy type to use. |
| 58 | * @param pic The picture to draw to the renderer. |
| 59 | * @param numRepeats The number of times to repeat the draw. |
| 60 | * @param timer The timer used to benchmark the work. |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 61 | */ |
| 62 | static void do_benchmark_work(sk_tools::PictureRenderer* renderer, |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 63 | BBoxType bBoxType, |
| 64 | SkPicture* pic, |
| 65 | const int numRepeats, |
| 66 | Timer* timer) { |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 67 | renderer->setBBoxHierarchyType(bBoxType); |
| 68 | renderer->setGridSize(FLAGS_tilesize, FLAGS_tilesize); |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 69 | renderer->init(pic, NULL, NULL, NULL, false, false); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 70 | |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 71 | SkDebugf("%s %d times...\n", renderer->getConfigName().c_str(), numRepeats); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 72 | for (int i = 0; i < numRepeats; ++i) { |
| 73 | renderer->setup(); |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 74 | // Render once to fill caches |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 75 | renderer->render(); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 76 | // Render again to measure |
| 77 | timer->start(); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 78 | renderer->render(); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 79 | timer->end(); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 80 | } |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 81 | } |
| 82 | |
sglez@google.com | 1d38ae9 | 2013-07-19 20:03:57 +0000 | [diff] [blame] | 83 | int tool_main(int argc, char** argv); |
| 84 | int tool_main(int argc, char** argv) { |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 85 | SkCommandLineFlags::Parse(argc, argv); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 86 | SkAutoGraphics ag; |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 87 | bool includeBBoxType[kBBoxTypeCount]; |
| 88 | for (int bBoxType = 0; bBoxType < kBBoxTypeCount; ++bBoxType) { |
commit-bot@chromium.org | f16d797 | 2014-03-11 19:12:44 +0000 | [diff] [blame] | 89 | includeBBoxType[bBoxType] = (FLAGS_bb_types.count() == 0) || |
| 90 | FLAGS_bb_types.contains(kBBoxHierarchyTypeNames[bBoxType]); |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 91 | } |
| 92 | // go through all the pictures |
| 93 | SkTArray<Measurement> measurements; |
| 94 | for (int index = 0; index < FLAGS_skps.count(); ++index) { |
| 95 | const char* path = FLAGS_skps[index]; |
| 96 | SkPicture* picture = pic_from_path(path); |
| 97 | if (NULL == picture) { |
| 98 | SkDebugf("Couldn't create picture. Ignoring path: %s\n", path); |
| 99 | continue; |
| 100 | } |
| 101 | SkDebugf("Benchmarking path: %s\n", path); |
| 102 | Measurement& measurement = measurements.push_back(); |
| 103 | measurement.fName = path; |
| 104 | for (int bBoxType = 0; bBoxType < kBBoxTypeCount; ++bBoxType) { |
| 105 | if (!includeBBoxType[bBoxType]) { continue; } |
| 106 | if (FLAGS_playback > 0) { |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 107 | #if SK_SUPPORT_GPU |
| 108 | GrContext::Options grContextOpts; |
| 109 | sk_tools::TiledPictureRenderer playbackRenderer(grContextOpts); |
| 110 | #else |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 111 | sk_tools::TiledPictureRenderer playbackRenderer; |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 112 | #endif |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame] | 113 | Timer playbackTimer; |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 114 | do_benchmark_work(&playbackRenderer, (BBoxType)bBoxType, |
| 115 | picture, FLAGS_playback, &playbackTimer); |
| 116 | measurement.fPlaybackAverage[bBoxType] = playbackTimer.fCpu; |
| 117 | } |
| 118 | if (FLAGS_record > 0) { |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 119 | #if SK_SUPPORT_GPU |
| 120 | GrContext::Options grContextOpts; |
| 121 | sk_tools::RecordPictureRenderer recordRenderer(grContextOpts); |
| 122 | #else |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 123 | sk_tools::RecordPictureRenderer recordRenderer; |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 124 | #endif |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame] | 125 | Timer recordTimer; |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 126 | do_benchmark_work(&recordRenderer, (BBoxType)bBoxType, |
| 127 | picture, FLAGS_record, &recordTimer); |
| 128 | measurement.fRecordAverage[bBoxType] = recordTimer.fCpu; |
| 129 | } |
| 130 | } |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 131 | } |
| 132 | |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 133 | Measurement globalMeasurement; |
| 134 | for (int bBoxType = 0; bBoxType < kBBoxTypeCount; ++bBoxType) { |
| 135 | if (!includeBBoxType[bBoxType]) { continue; } |
| 136 | globalMeasurement.fPlaybackAverage[bBoxType] = 0; |
| 137 | globalMeasurement.fRecordAverage[bBoxType] = 0; |
| 138 | for (int index = 0; index < measurements.count(); ++index) { |
| 139 | const Measurement& measurement = measurements[index]; |
| 140 | globalMeasurement.fPlaybackAverage[bBoxType] += |
| 141 | measurement.fPlaybackAverage[bBoxType]; |
| 142 | globalMeasurement.fRecordAverage[bBoxType] += |
| 143 | measurement.fRecordAverage[bBoxType]; |
| 144 | } |
| 145 | globalMeasurement.fPlaybackAverage[bBoxType] /= measurements.count(); |
| 146 | globalMeasurement.fRecordAverage[bBoxType] /= measurements.count(); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Output gnuplot readable histogram data.. |
| 150 | const char* pbTitle = "bbh_shootout_playback.dat"; |
| 151 | const char* recTitle = "bbh_shootout_record.dat"; |
| 152 | SkFILEWStream playbackOut(pbTitle); |
| 153 | SkFILEWStream recordOut(recTitle); |
| 154 | recordOut.writeText("# "); |
| 155 | playbackOut.writeText("# "); |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 156 | SkDebugf("---\n"); |
| 157 | for (int bBoxType = 0; bBoxType < kBBoxTypeCount; ++bBoxType) { |
| 158 | if (!includeBBoxType[bBoxType]) { continue; } |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 159 | SkString out; |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 160 | out.printf("%s ", kBBoxHierarchyTypeNames[bBoxType]); |
| 161 | recordOut.writeText(out.c_str()); |
| 162 | playbackOut.writeText(out.c_str()); |
| 163 | |
| 164 | if (FLAGS_record > 0) { |
| 165 | SkDebugf("Average %s recording time: %.3fms\n", |
| 166 | kBBoxHierarchyTypeNames[bBoxType], |
| 167 | globalMeasurement.fRecordAverage[bBoxType]); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 168 | } |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 169 | if (FLAGS_playback > 0) { |
| 170 | SkDebugf("Average %s playback time: %.3fms\n", |
| 171 | kBBoxHierarchyTypeNames[bBoxType], |
| 172 | globalMeasurement.fPlaybackAverage[bBoxType]); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | recordOut.writeText("\n"); |
| 176 | playbackOut.writeText("\n"); |
commit-bot@chromium.org | 2e915b3 | 2013-08-20 12:23:18 +0000 | [diff] [blame] | 177 | // Write to file, and save recording averages. |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 178 | for (int index = 0; index < measurements.count(); ++index) { |
| 179 | const Measurement& measurement = measurements[index]; |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 180 | SkString pbLine; |
| 181 | SkString recLine; |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 182 | |
commit-bot@chromium.org | cdd0f92 | 2014-03-11 17:27:07 +0000 | [diff] [blame] | 183 | pbLine.printf("%d", index); |
| 184 | recLine.printf("%d", index); |
| 185 | for (int bBoxType = 0; bBoxType < kBBoxTypeCount; ++bBoxType) { |
| 186 | if (!includeBBoxType[bBoxType]) { continue; } |
| 187 | pbLine.appendf(" %f", measurement.fPlaybackAverage[bBoxType]); |
| 188 | recLine.appendf(" %f", measurement.fRecordAverage[bBoxType]); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 189 | } |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 190 | pbLine.appendf("\n"); |
| 191 | recLine.appendf("\n"); |
| 192 | playbackOut.writeText(pbLine.c_str()); |
| 193 | recordOut.writeText(recLine.c_str()); |
| 194 | } |
| 195 | SkDebugf("\nWrote data to gnuplot-readable files: %s %s\n", pbTitle, recTitle); |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
sglez@google.com | 5f3f681 | 2013-07-19 20:21:05 +0000 | [diff] [blame] | 199 | #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) |
commit-bot@chromium.org | 6645cde | 2013-07-19 18:54:04 +0000 | [diff] [blame] | 200 | int main(int argc, char** argv) { |
| 201 | return tool_main(argc, argv); |
| 202 | } |
sglez@google.com | 5f3f681 | 2013-07-19 20:21:05 +0000 | [diff] [blame] | 203 | #endif |