blob: d4b290d06bd36f28eb4109d67857fd1ad96a3237 [file] [log] [blame]
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +00001/*
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
mtklein9ac68ee2014-06-20 11:29:20 -07008#include "Timer.h"
tfarinaf168b862014-06-19 12:32:29 -07009#include "Benchmark.h"
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000010#include "LazyDecodeBitmap.h"
11#include "PictureBenchmark.h"
12#include "PictureRenderer.h"
tfarinaf168b862014-06-19 12:32:29 -070013#include "SkCommandLineFlags.h"
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000014#include "SkForceLinking.h"
15#include "SkGraphics.h"
16#include "SkStream.h"
17#include "SkString.h"
commit-bot@chromium.org49a07ad2013-07-22 19:28:40 +000018#include "SkTArray.h"
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000019
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000020typedef sk_tools::PictureRenderer::BBoxHierarchyType BBoxType;
21static const int kBBoxTypeCount = sk_tools::PictureRenderer::kLast_BBoxHierarchyType + 1;
commit-bot@chromium.orgdb34f332014-03-11 14:23:50 +000022
23
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000024DEFINE_string2(skps, r, "", "The list of SKPs to benchmark.");
commit-bot@chromium.orgf16d7972014-03-11 19:12:44 +000025DEFINE_string(bb_types, "", "The set of bbox types to test. If empty, all are tested. "
mtklein2a65a232014-08-26 14:07:04 -070026 "Should be one or more of none, rtree, tilegrid.");
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000027DEFINE_int32(record, 100, "Number of times to record each SKP.");
28DEFINE_int32(playback, 1, "Number of times to playback each SKP.");
29DEFINE_int32(tilesize, 256, "The size of a tile.");
commit-bot@chromium.orgdb34f332014-03-11 14:23:50 +000030
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000031struct Measurement {
commit-bot@chromium.orgdb34f332014-03-11 14:23:50 +000032 SkString fName;
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000033 double fRecordAverage[kBBoxTypeCount];
34 double fPlaybackAverage[kBBoxTypeCount];
commit-bot@chromium.orgdb34f332014-03-11 14:23:50 +000035};
36
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000037const char* kBBoxHierarchyTypeNames[kBBoxTypeCount] = {
38 "none", // kNone_BBoxHierarchyType
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000039 "rtree", // kRTree_BBoxHierarchyType
40 "tilegrid", // kTileGrid_BBoxHierarchyType
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000041};
42
43static 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.orgcdd0f922014-03-11 17:27:07 +000054 * @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.org6645cde2013-07-19 18:54:04 +000061 */
62static void do_benchmark_work(sk_tools::PictureRenderer* renderer,
robertphillips78c71272014-10-09 04:59:19 -070063 BBoxType bBoxType,
64 SkPicture* pic,
65 const int numRepeats,
66 Timer* timer) {
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000067 renderer->setBBoxHierarchyType(bBoxType);
68 renderer->setGridSize(FLAGS_tilesize, FLAGS_tilesize);
robertphillips78c71272014-10-09 04:59:19 -070069 renderer->init(pic, NULL, NULL, NULL, false, false);
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000070
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000071 SkDebugf("%s %d times...\n", renderer->getConfigName().c_str(), numRepeats);
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000072 for (int i = 0; i < numRepeats; ++i) {
73 renderer->setup();
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000074 // Render once to fill caches
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000075 renderer->render();
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000076 // Render again to measure
77 timer->start();
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000078 renderer->render();
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000079 timer->end();
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000080 }
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000081}
82
sglez@google.com1d38ae92013-07-19 20:03:57 +000083int tool_main(int argc, char** argv);
84int tool_main(int argc, char** argv) {
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000085 SkCommandLineFlags::Parse(argc, argv);
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +000086 SkAutoGraphics ag;
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000087 bool includeBBoxType[kBBoxTypeCount];
88 for (int bBoxType = 0; bBoxType < kBBoxTypeCount; ++bBoxType) {
commit-bot@chromium.orgf16d7972014-03-11 19:12:44 +000089 includeBBoxType[bBoxType] = (FLAGS_bb_types.count() == 0) ||
90 FLAGS_bb_types.contains(kBBoxHierarchyTypeNames[bBoxType]);
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000091 }
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) {
krajcevskib1aded82014-08-18 07:52:17 -0700107#if SK_SUPPORT_GPU
108 GrContext::Options grContextOpts;
109 sk_tools::TiledPictureRenderer playbackRenderer(grContextOpts);
110#else
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +0000111 sk_tools::TiledPictureRenderer playbackRenderer;
krajcevskib1aded82014-08-18 07:52:17 -0700112#endif
mtklein9ac68ee2014-06-20 11:29:20 -0700113 Timer playbackTimer;
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +0000114 do_benchmark_work(&playbackRenderer, (BBoxType)bBoxType,
115 picture, FLAGS_playback, &playbackTimer);
116 measurement.fPlaybackAverage[bBoxType] = playbackTimer.fCpu;
117 }
118 if (FLAGS_record > 0) {
krajcevskib1aded82014-08-18 07:52:17 -0700119#if SK_SUPPORT_GPU
120 GrContext::Options grContextOpts;
121 sk_tools::RecordPictureRenderer recordRenderer(grContextOpts);
122#else
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +0000123 sk_tools::RecordPictureRenderer recordRenderer;
krajcevskib1aded82014-08-18 07:52:17 -0700124#endif
mtklein9ac68ee2014-06-20 11:29:20 -0700125 Timer recordTimer;
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +0000126 do_benchmark_work(&recordRenderer, (BBoxType)bBoxType,
127 picture, FLAGS_record, &recordTimer);
128 measurement.fRecordAverage[bBoxType] = recordTimer.fCpu;
129 }
130 }
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +0000131 }
132
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +0000133 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.org6645cde2013-07-19 18:54:04 +0000147 }
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.orgcdd0f922014-03-11 17:27:07 +0000156 SkDebugf("---\n");
157 for (int bBoxType = 0; bBoxType < kBBoxTypeCount; ++bBoxType) {
158 if (!includeBBoxType[bBoxType]) { continue; }
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +0000159 SkString out;
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +0000160 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.org6645cde2013-07-19 18:54:04 +0000168 }
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +0000169 if (FLAGS_playback > 0) {
170 SkDebugf("Average %s playback time: %.3fms\n",
171 kBBoxHierarchyTypeNames[bBoxType],
172 globalMeasurement.fPlaybackAverage[bBoxType]);
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +0000173 }
174 }
175 recordOut.writeText("\n");
176 playbackOut.writeText("\n");
commit-bot@chromium.org2e915b32013-08-20 12:23:18 +0000177 // Write to file, and save recording averages.
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +0000178 for (int index = 0; index < measurements.count(); ++index) {
179 const Measurement& measurement = measurements[index];
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +0000180 SkString pbLine;
181 SkString recLine;
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +0000182
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +0000183 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.org6645cde2013-07-19 18:54:04 +0000189 }
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +0000190 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.org6645cde2013-07-19 18:54:04 +0000196 return 0;
197}
198
sglez@google.com5f3f6812013-07-19 20:21:05 +0000199#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
commit-bot@chromium.org6645cde2013-07-19 18:54:04 +0000200int main(int argc, char** argv) {
201 return tool_main(argc, argv);
202}
sglez@google.com5f3f6812013-07-19 20:21:05 +0000203#endif