blob: f05f80565d3e8408aa0d022b8c8b2c88c422bae7 [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)
scroggo@google.com5239c322012-09-11 19:15:32 +000026{}
27
28PictureBenchmark::~PictureBenchmark() {
29 SkSafeUnref(fRenderer);
30}
31
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000032void PictureBenchmark::setTimersToShow(bool wall,
33 bool truncatedWall,
34 bool cpu,
35 bool truncatedCpu,
36 bool gpu) {
37 fTimerTypes = 0;
38 fTimerTypes |= wall ? TimerData::kWall_Flag : 0;
39 fTimerTypes |= truncatedWall ? TimerData::kTruncatedWall_Flag : 0;
40 fTimerTypes |= cpu ? TimerData::kCpu_Flag : 0;
41 fTimerTypes |= truncatedCpu ? TimerData::kTruncatedCpu_Flag : 0;
42 fTimerTypes |= gpu ? TimerData::kGpu_Flag : 0;
43}
44
robertphillips@google.comf9d0c952013-02-07 16:21:22 +000045BenchTimer* PictureBenchmark::setupTimer(bool useGLTimer) {
keyar@chromium.org77a55222012-08-20 15:03:47 +000046#if SK_SUPPORT_GPU
robertphillips@google.comf9d0c952013-02-07 16:21:22 +000047 if (useGLTimer && fRenderer != NULL && fRenderer->isUsingGpuDevice()) {
scroggo@google.com5239c322012-09-11 19:15:32 +000048 return SkNEW_ARGS(BenchTimer, (fRenderer->getGLContext()));
keyar@chromium.org77a55222012-08-20 15:03:47 +000049 }
keyar@chromium.org77a55222012-08-20 15:03:47 +000050#endif
scroggo@google.comcbcef702012-12-13 22:09:28 +000051 return SkNEW_ARGS(BenchTimer, (NULL));
keyar@chromium.org77a55222012-08-20 15:03:47 +000052}
53
scroggo@google.com9a412522012-09-07 15:21:18 +000054void PictureBenchmark::logProgress(const char msg[]) {
55 if (fLogger != NULL) {
56 fLogger->logProgress(msg);
57 }
58}
59
scroggo@google.com5239c322012-09-11 19:15:32 +000060PictureRenderer* PictureBenchmark::setRenderer(sk_tools::PictureRenderer* renderer) {
61 SkRefCnt_SafeAssign(fRenderer, renderer);
62 return renderer;
63}
64
scroggo@google.com9a412522012-09-07 15:21:18 +000065void PictureBenchmark::run(SkPicture* pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000066 SkASSERT(pict);
keyar@chromium.org78a35c52012-08-20 15:03:44 +000067 if (NULL == pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000068 return;
69 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000070
scroggo@google.com5239c322012-09-11 19:15:32 +000071 SkASSERT(fRenderer != NULL);
72 if (NULL == fRenderer) {
scroggo@google.com9a412522012-09-07 15:21:18 +000073 return;
74 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000075
scroggo@google.com5239c322012-09-11 19:15:32 +000076 fRenderer->init(pict);
77
78 // We throw this away to remove first time effects (such as paging in this program)
79 fRenderer->setup();
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000080 fRenderer->render(NULL);
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +000081 fRenderer->resetState(true); // flush, swapBuffers and Finish
keyar@chromium.org163b5672012-08-01 17:53:29 +000082
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000083 if (fPurgeDecodedTex) {
84 fRenderer->purgeTextures();
85 }
86
scroggo@google.com9a412522012-09-07 15:21:18 +000087 bool usingGpu = false;
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +000088#if SK_SUPPORT_GPU
scroggo@google.com5239c322012-09-11 19:15:32 +000089 usingGpu = fRenderer->isUsingGpuDevice();
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +000090#endif
keyar@chromium.org77a55222012-08-20 15:03:47 +000091
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000092 uint32_t timerTypes = fTimerTypes;
93 if (!usingGpu) {
94 timerTypes &= ~TimerData::kGpu_Flag;
95 }
96
97 SkString timeFormat;
98 if (TimerData::kPerIter_Result == fTimerResult) {
99 timeFormat = fRenderer->getPerIterTimeFormat();
100 } else {
101 timeFormat = fRenderer->getNormalTimeFormat();
102 }
103
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000104 static const int kNumInnerLoops = 5;
105 int numOuterLoops = 1;
106 int numInnerLoops = fRepeats;
107
108 if (TimerData::kPerIter_Result == fTimerResult && fRepeats > 1) {
109 // interpret this flag combination to mean: generate 'fRepeats'
110 // numbers by averaging each rendering 'kNumInnerLoops' times
111 numOuterLoops = fRepeats;
112 numInnerLoops = kNumInnerLoops;
113 }
114
scroggo@google.comcbcef702012-12-13 22:09:28 +0000115 if (fTimeIndividualTiles) {
116 TiledPictureRenderer* tiledRenderer = fRenderer->getTiledRenderer();
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000117 SkASSERT(tiledRenderer && tiledRenderer->supportsTimingIndividualTiles());
118 if (NULL == tiledRenderer || !tiledRenderer->supportsTimingIndividualTiles()) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000119 return;
120 }
121 int xTiles, yTiles;
122 if (!tiledRenderer->tileDimensions(xTiles, yTiles)) {
123 return;
124 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000125
scroggo@google.comcbcef702012-12-13 22:09:28 +0000126 // Insert a newline so that each tile is reported on its own line (separate from the line
127 // that describes the skp being run).
128 this->logProgress("\n");
scroggo@google.com9a412522012-09-07 15:21:18 +0000129
scroggo@google.comcbcef702012-12-13 22:09:28 +0000130 int x, y;
131 while (tiledRenderer->nextTile(x, y)) {
scroggo@google.com08085f82013-01-28 20:40:24 +0000132 // There are two timers, which will behave slightly differently:
133 // 1) longRunningTimer, along with perTileTimerData, will time how long it takes to draw
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000134 // one tile fRepeats times, and take the average. As such, it will not respect the
scroggo@google.com08085f82013-01-28 20:40:24 +0000135 // logPerIter or printMin options, since it does not know the time per iteration. It
136 // will also be unable to call flush() for each tile.
137 // The goal of this timer is to make up for a system timer that is not precise enough to
138 // measure the small amount of time it takes to draw one tile once.
139 //
140 // 2) perTileTimer, along with perTileTimerData, will record each run separately, and
141 // then take the average. As such, it supports logPerIter and printMin options.
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000142 //
143 // Although "legal", having two gpu timers running at the same time
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000144 // seems to cause problems (i.e., INVALID_OPERATIONs) on several
145 // platforms. To work around this, we disable the gpu timer on the
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000146 // long running timer.
borenet@google.comb15e6062013-02-11 18:42:43 +0000147 SkAutoTDelete<BenchTimer> longRunningTimer(this->setupTimer());
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000148 TimerData longRunningTimerData(numOuterLoops);
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000149
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000150 for (int outer = 0; outer < numOuterLoops; ++outer) {
151 SkAutoTDelete<BenchTimer> perTileTimer(this->setupTimer(false));
152 TimerData perTileTimerData(numInnerLoops);
153
154 longRunningTimer->start();
155 for (int inner = 0; inner < numInnerLoops; ++inner) {
156 perTileTimer->start();
157 tiledRenderer->drawCurrentTile();
158 perTileTimer->truncatedEnd();
159 tiledRenderer->resetState(false); // flush & swapBuffers, but don't Finish
160 perTileTimer->end();
161 SkAssertResult(perTileTimerData.appendTimes(perTileTimer.get()));
162
163 if (fPurgeDecodedTex) {
164 fRenderer->purgeTextures();
165 }
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000166 }
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000167 longRunningTimer->truncatedEnd();
168 tiledRenderer->resetState(true); // flush, swapBuffers and Finish
169 longRunningTimer->end();
170 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000171 }
scroggo@google.com08085f82013-01-28 20:40:24 +0000172
scroggo@google.comcbcef702012-12-13 22:09:28 +0000173 SkString configName = tiledRenderer->getConfigName();
174 configName.appendf(": tile [%i,%i] out of [%i,%i]", x, y, xTiles, yTiles);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000175
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000176 // TODO(borenet): Turn off per-iteration tile time reporting for now.
177 // Avoiding logging the time for every iteration for each tile cuts
178 // down on data file size by a significant amount. Re-enable this once
179 // we're loading the bench data directly into a data store and are no
180 // longer generating SVG graphs.
181#if 0
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000182 SkString result = perTileTimerData.getResult(timeFormat.c_str(), fTimerResult,
183 configName.c_str(), timerTypes);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000184 result.append("\n");
185 this->logProgress(result.c_str());
borenet@google.comb15e6062013-02-11 18:42:43 +0000186#endif
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000187
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000188 if (fPurgeDecodedTex) {
189 configName.append(" <withPurging>");
190 }
scroggo@google.com08085f82013-01-28 20:40:24 +0000191 configName.append(" <averaged>");
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000192 SkString longRunningResult = longRunningTimerData.getResult(
193 tiledRenderer->getNormalTimeFormat().c_str(),
194 TimerData::kAvg_Result,
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000195 configName.c_str(), timerTypes, numInnerLoops);
scroggo@google.com08085f82013-01-28 20:40:24 +0000196 longRunningResult.append("\n");
197 this->logProgress(longRunningResult.c_str());
scroggo@google.comcbcef702012-12-13 22:09:28 +0000198 }
199 } else {
robertphillips@google.com090601c2013-12-17 13:40:20 +0000200 SkAutoTDelete<BenchTimer> longRunningTimer(this->setupTimer());
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000201 TimerData longRunningTimerData(numOuterLoops);
robertphillips@google.com090601c2013-12-17 13:40:20 +0000202
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000203 for (int outer = 0; outer < numOuterLoops; ++outer) {
204 SkAutoTDelete<BenchTimer> perRunTimer(this->setupTimer(false));
205 TimerData perRunTimerData(numInnerLoops);
keyar@chromium.org163b5672012-08-01 17:53:29 +0000206
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000207 longRunningTimer->start();
208 for (int inner = 0; inner < numInnerLoops; ++inner) {
209 fRenderer->setup();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000210
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000211 perRunTimer->start();
212 fRenderer->render(NULL);
213 perRunTimer->truncatedEnd();
214 fRenderer->resetState(false); // flush & swapBuffers, but don't Finish
215 perRunTimer->end();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000216
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000217 SkAssertResult(perRunTimerData.appendTimes(perRunTimer.get()));
218
219 if (fPurgeDecodedTex) {
220 fRenderer->purgeTextures();
221 }
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000222 }
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000223 longRunningTimer->truncatedEnd();
224 fRenderer->resetState(true); // flush, swapBuffers and Finish
225 longRunningTimer->end();
226 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000227 }
228
229 SkString configName = fRenderer->getConfigName();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000230 if (fPurgeDecodedTex) {
231 configName.append(" <withPurging>");
232 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000233
robertphillips@google.com090601c2013-12-17 13:40:20 +0000234 // Beware - since the per-run-timer doesn't ever include a glFinish it can
235 // report a lower time then the long-running-timer
236#if 0
237 SkString result = perRunTimerData.getResult(timeFormat.c_str(),
238 fTimerResult,
239 configName.c_str(),
240 timerTypes);
241 result.append("\n");
242
243 this->logProgress(result.c_str());
244#else
skia.committer@gmail.com3b85deb2013-12-18 07:01:56 +0000245 SkString result = longRunningTimerData.getResult(timeFormat.c_str(),
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000246 fTimerResult,
247 configName.c_str(),
248 timerTypes,
249 numInnerLoops);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000250 result.append("\n");
251 this->logProgress(result.c_str());
robertphillips@google.com090601c2013-12-17 13:40:20 +0000252#endif
keyar@chromium.org163b5672012-08-01 17:53:29 +0000253 }
254
scroggo@google.com5239c322012-09-11 19:15:32 +0000255 fRenderer->end();
keyar@chromium.org163b5672012-08-01 17:53:29 +0000256}
257
258}