blob: 6c0325e2ee115c76c3c021c50101b30ed61053d3 [file] [log] [blame]
keyar@chromium.org163b5672012-08-01 17:53:29 +00001/*
2 * Copyright 2012 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
scroggo@google.com9a412522012-09-07 15:21:18 +00008#include "SkBenchLogger.h"
keyar@chromium.org163b5672012-08-01 17:53:29 +00009#include "BenchTimer.h"
10#include "PictureBenchmark.h"
11#include "SkCanvas.h"
12#include "SkPicture.h"
13#include "SkString.h"
14#include "picture_utils.h"
15
16namespace sk_tools {
17
scroggo@google.com5239c322012-09-11 19:15:32 +000018PictureBenchmark::PictureBenchmark()
19: fRepeats(1)
20, fLogger(NULL)
21, fRenderer(NULL)
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000022, fTimerResult(TimerData::kAvg_Result)
23, fTimerTypes(0)
scroggo@google.comcbcef702012-12-13 22:09:28 +000024, fTimeIndividualTiles(false)
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000025, fPurgeDecodedTex(false)
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +000026, fPreprocess(false)
scroggo@google.com5239c322012-09-11 19:15:32 +000027{}
28
29PictureBenchmark::~PictureBenchmark() {
30 SkSafeUnref(fRenderer);
31}
32
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000033void PictureBenchmark::setTimersToShow(bool wall,
34 bool truncatedWall,
35 bool cpu,
36 bool truncatedCpu,
37 bool gpu) {
38 fTimerTypes = 0;
39 fTimerTypes |= wall ? TimerData::kWall_Flag : 0;
40 fTimerTypes |= truncatedWall ? TimerData::kTruncatedWall_Flag : 0;
41 fTimerTypes |= cpu ? TimerData::kCpu_Flag : 0;
42 fTimerTypes |= truncatedCpu ? TimerData::kTruncatedCpu_Flag : 0;
43 fTimerTypes |= gpu ? TimerData::kGpu_Flag : 0;
44}
45
robertphillips@google.comf9d0c952013-02-07 16:21:22 +000046BenchTimer* PictureBenchmark::setupTimer(bool useGLTimer) {
keyar@chromium.org77a55222012-08-20 15:03:47 +000047#if SK_SUPPORT_GPU
robertphillips@google.comf9d0c952013-02-07 16:21:22 +000048 if (useGLTimer && fRenderer != NULL && fRenderer->isUsingGpuDevice()) {
scroggo@google.com5239c322012-09-11 19:15:32 +000049 return SkNEW_ARGS(BenchTimer, (fRenderer->getGLContext()));
keyar@chromium.org77a55222012-08-20 15:03:47 +000050 }
keyar@chromium.org77a55222012-08-20 15:03:47 +000051#endif
scroggo@google.comcbcef702012-12-13 22:09:28 +000052 return SkNEW_ARGS(BenchTimer, (NULL));
keyar@chromium.org77a55222012-08-20 15:03:47 +000053}
54
scroggo@google.com9a412522012-09-07 15:21:18 +000055void PictureBenchmark::logProgress(const char msg[]) {
56 if (fLogger != NULL) {
57 fLogger->logProgress(msg);
58 }
59}
60
scroggo@google.com5239c322012-09-11 19:15:32 +000061PictureRenderer* PictureBenchmark::setRenderer(sk_tools::PictureRenderer* renderer) {
62 SkRefCnt_SafeAssign(fRenderer, renderer);
63 return renderer;
64}
65
scroggo@google.com9a412522012-09-07 15:21:18 +000066void PictureBenchmark::run(SkPicture* pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000067 SkASSERT(pict);
keyar@chromium.org78a35c52012-08-20 15:03:44 +000068 if (NULL == pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000069 return;
70 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000071
scroggo@google.com5239c322012-09-11 19:15:32 +000072 SkASSERT(fRenderer != NULL);
73 if (NULL == fRenderer) {
scroggo@google.com9a412522012-09-07 15:21:18 +000074 return;
75 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000076
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000077 fRenderer->init(pict, NULL, NULL, NULL, false);
scroggo@google.com5239c322012-09-11 19:15:32 +000078
79 // We throw this away to remove first time effects (such as paging in this program)
80 fRenderer->setup();
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +000081
82 if (fPreprocess) {
83 if (NULL != fRenderer->getCanvas()) {
84 fRenderer->getCanvas()->EXPERIMENTAL_optimize(pict);
85 }
86 }
87
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000088 fRenderer->render(NULL);
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +000089 fRenderer->resetState(true); // flush, swapBuffers and Finish
keyar@chromium.org163b5672012-08-01 17:53:29 +000090
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +000091 if (fPreprocess) {
92 if (NULL != fRenderer->getCanvas()) {
93 fRenderer->getCanvas()->EXPERIMENTAL_purge(pict);
94 }
95 }
96
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000097 if (fPurgeDecodedTex) {
98 fRenderer->purgeTextures();
99 }
100
scroggo@google.com9a412522012-09-07 15:21:18 +0000101 bool usingGpu = false;
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +0000102#if SK_SUPPORT_GPU
scroggo@google.com5239c322012-09-11 19:15:32 +0000103 usingGpu = fRenderer->isUsingGpuDevice();
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +0000104#endif
keyar@chromium.org77a55222012-08-20 15:03:47 +0000105
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000106 uint32_t timerTypes = fTimerTypes;
107 if (!usingGpu) {
108 timerTypes &= ~TimerData::kGpu_Flag;
109 }
110
111 SkString timeFormat;
112 if (TimerData::kPerIter_Result == fTimerResult) {
113 timeFormat = fRenderer->getPerIterTimeFormat();
114 } else {
115 timeFormat = fRenderer->getNormalTimeFormat();
116 }
117
commit-bot@chromium.org359e4f02014-04-30 00:46:29 +0000118 static const int kNumInnerLoops = 10;
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000119 int numOuterLoops = 1;
120 int numInnerLoops = fRepeats;
121
122 if (TimerData::kPerIter_Result == fTimerResult && fRepeats > 1) {
123 // interpret this flag combination to mean: generate 'fRepeats'
124 // numbers by averaging each rendering 'kNumInnerLoops' times
125 numOuterLoops = fRepeats;
126 numInnerLoops = kNumInnerLoops;
127 }
128
scroggo@google.comcbcef702012-12-13 22:09:28 +0000129 if (fTimeIndividualTiles) {
130 TiledPictureRenderer* tiledRenderer = fRenderer->getTiledRenderer();
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000131 SkASSERT(tiledRenderer && tiledRenderer->supportsTimingIndividualTiles());
132 if (NULL == tiledRenderer || !tiledRenderer->supportsTimingIndividualTiles()) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000133 return;
134 }
135 int xTiles, yTiles;
136 if (!tiledRenderer->tileDimensions(xTiles, yTiles)) {
137 return;
138 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000139
scroggo@google.comcbcef702012-12-13 22:09:28 +0000140 // Insert a newline so that each tile is reported on its own line (separate from the line
141 // that describes the skp being run).
142 this->logProgress("\n");
scroggo@google.com9a412522012-09-07 15:21:18 +0000143
scroggo@google.comcbcef702012-12-13 22:09:28 +0000144 int x, y;
145 while (tiledRenderer->nextTile(x, y)) {
scroggo@google.com08085f82013-01-28 20:40:24 +0000146 // There are two timers, which will behave slightly differently:
147 // 1) longRunningTimer, along with perTileTimerData, will time how long it takes to draw
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000148 // one tile fRepeats times, and take the average. As such, it will not respect the
scroggo@google.com08085f82013-01-28 20:40:24 +0000149 // logPerIter or printMin options, since it does not know the time per iteration. It
150 // will also be unable to call flush() for each tile.
151 // The goal of this timer is to make up for a system timer that is not precise enough to
152 // measure the small amount of time it takes to draw one tile once.
153 //
154 // 2) perTileTimer, along with perTileTimerData, will record each run separately, and
155 // then take the average. As such, it supports logPerIter and printMin options.
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000156 //
157 // Although "legal", having two gpu timers running at the same time
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000158 // seems to cause problems (i.e., INVALID_OPERATIONs) on several
159 // platforms. To work around this, we disable the gpu timer on the
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000160 // long running timer.
borenet@google.comb15e6062013-02-11 18:42:43 +0000161 SkAutoTDelete<BenchTimer> longRunningTimer(this->setupTimer());
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000162 TimerData longRunningTimerData(numOuterLoops);
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000163
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000164 for (int outer = 0; outer < numOuterLoops; ++outer) {
165 SkAutoTDelete<BenchTimer> perTileTimer(this->setupTimer(false));
166 TimerData perTileTimerData(numInnerLoops);
167
168 longRunningTimer->start();
169 for (int inner = 0; inner < numInnerLoops; ++inner) {
170 perTileTimer->start();
171 tiledRenderer->drawCurrentTile();
172 perTileTimer->truncatedEnd();
173 tiledRenderer->resetState(false); // flush & swapBuffers, but don't Finish
174 perTileTimer->end();
175 SkAssertResult(perTileTimerData.appendTimes(perTileTimer.get()));
176
177 if (fPurgeDecodedTex) {
178 fRenderer->purgeTextures();
179 }
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000180 }
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000181 longRunningTimer->truncatedEnd();
182 tiledRenderer->resetState(true); // flush, swapBuffers and Finish
183 longRunningTimer->end();
184 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000185 }
scroggo@google.com08085f82013-01-28 20:40:24 +0000186
scroggo@google.comcbcef702012-12-13 22:09:28 +0000187 SkString configName = tiledRenderer->getConfigName();
188 configName.appendf(": tile [%i,%i] out of [%i,%i]", x, y, xTiles, yTiles);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000189
skia.committer@gmail.com0b708162014-03-12 03:02:18 +0000190 // TODO(borenet): Turn off per-iteration tile time reporting for now.
191 // Avoiding logging the time for every iteration for each tile cuts
192 // down on data file size by a significant amount. Re-enable this once
193 // we're loading the bench data directly into a data store and are no
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000194 // longer generating SVG graphs.
195#if 0
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000196 SkString result = perTileTimerData.getResult(timeFormat.c_str(), fTimerResult,
197 configName.c_str(), timerTypes);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000198 result.append("\n");
199 this->logProgress(result.c_str());
borenet@google.comb15e6062013-02-11 18:42:43 +0000200#endif
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000201
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000202 if (fPurgeDecodedTex) {
203 configName.append(" <withPurging>");
204 }
scroggo@google.com08085f82013-01-28 20:40:24 +0000205 configName.append(" <averaged>");
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000206 SkString longRunningResult = longRunningTimerData.getResult(
207 tiledRenderer->getNormalTimeFormat().c_str(),
208 TimerData::kAvg_Result,
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000209 configName.c_str(), timerTypes, numInnerLoops);
scroggo@google.com08085f82013-01-28 20:40:24 +0000210 longRunningResult.append("\n");
211 this->logProgress(longRunningResult.c_str());
scroggo@google.comcbcef702012-12-13 22:09:28 +0000212 }
213 } else {
robertphillips@google.com090601c2013-12-17 13:40:20 +0000214 SkAutoTDelete<BenchTimer> longRunningTimer(this->setupTimer());
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000215 TimerData longRunningTimerData(numOuterLoops);
robertphillips@google.com090601c2013-12-17 13:40:20 +0000216
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000217 for (int outer = 0; outer < numOuterLoops; ++outer) {
218 SkAutoTDelete<BenchTimer> perRunTimer(this->setupTimer(false));
219 TimerData perRunTimerData(numInnerLoops);
keyar@chromium.org163b5672012-08-01 17:53:29 +0000220
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000221 longRunningTimer->start();
222 for (int inner = 0; inner < numInnerLoops; ++inner) {
223 fRenderer->setup();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000224
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000225 perRunTimer->start();
226 fRenderer->render(NULL);
227 perRunTimer->truncatedEnd();
228 fRenderer->resetState(false); // flush & swapBuffers, but don't Finish
229 perRunTimer->end();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000230
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000231 SkAssertResult(perRunTimerData.appendTimes(perRunTimer.get()));
232
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +0000233 if (fPreprocess) {
234 if (NULL != fRenderer->getCanvas()) {
235 fRenderer->getCanvas()->EXPERIMENTAL_purge(pict);
236 }
237 }
238
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000239 if (fPurgeDecodedTex) {
240 fRenderer->purgeTextures();
241 }
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000242 }
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000243 longRunningTimer->truncatedEnd();
244 fRenderer->resetState(true); // flush, swapBuffers and Finish
245 longRunningTimer->end();
246 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000247 }
248
249 SkString configName = fRenderer->getConfigName();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000250 if (fPurgeDecodedTex) {
251 configName.append(" <withPurging>");
252 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000253
robertphillips@google.com090601c2013-12-17 13:40:20 +0000254 // Beware - since the per-run-timer doesn't ever include a glFinish it can
255 // report a lower time then the long-running-timer
256#if 0
257 SkString result = perRunTimerData.getResult(timeFormat.c_str(),
258 fTimerResult,
259 configName.c_str(),
260 timerTypes);
261 result.append("\n");
262
263 this->logProgress(result.c_str());
264#else
skia.committer@gmail.com3b85deb2013-12-18 07:01:56 +0000265 SkString result = longRunningTimerData.getResult(timeFormat.c_str(),
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000266 fTimerResult,
267 configName.c_str(),
268 timerTypes,
269 numInnerLoops);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000270 result.append("\n");
271 this->logProgress(result.c_str());
robertphillips@google.com090601c2013-12-17 13:40:20 +0000272#endif
keyar@chromium.org163b5672012-08-01 17:53:29 +0000273 }
274
scroggo@google.com5239c322012-09-11 19:15:32 +0000275 fRenderer->end();
keyar@chromium.org163b5672012-08-01 17:53:29 +0000276}
277
278}