blob: 155d097760bd8847ddfcb50879fa2dc0b9bb0fbf [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)
scroggo@google.com5239c322012-09-11 19:15:32 +000025{}
26
27PictureBenchmark::~PictureBenchmark() {
28 SkSafeUnref(fRenderer);
29}
30
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000031void PictureBenchmark::setTimersToShow(bool wall,
32 bool truncatedWall,
33 bool cpu,
34 bool truncatedCpu,
35 bool gpu) {
36 fTimerTypes = 0;
37 fTimerTypes |= wall ? TimerData::kWall_Flag : 0;
38 fTimerTypes |= truncatedWall ? TimerData::kTruncatedWall_Flag : 0;
39 fTimerTypes |= cpu ? TimerData::kCpu_Flag : 0;
40 fTimerTypes |= truncatedCpu ? TimerData::kTruncatedCpu_Flag : 0;
41 fTimerTypes |= gpu ? TimerData::kGpu_Flag : 0;
42}
43
robertphillips@google.comf9d0c952013-02-07 16:21:22 +000044BenchTimer* PictureBenchmark::setupTimer(bool useGLTimer) {
keyar@chromium.org77a55222012-08-20 15:03:47 +000045#if SK_SUPPORT_GPU
robertphillips@google.comf9d0c952013-02-07 16:21:22 +000046 if (useGLTimer && fRenderer != NULL && fRenderer->isUsingGpuDevice()) {
scroggo@google.com5239c322012-09-11 19:15:32 +000047 return SkNEW_ARGS(BenchTimer, (fRenderer->getGLContext()));
keyar@chromium.org77a55222012-08-20 15:03:47 +000048 }
keyar@chromium.org77a55222012-08-20 15:03:47 +000049#endif
scroggo@google.comcbcef702012-12-13 22:09:28 +000050 return SkNEW_ARGS(BenchTimer, (NULL));
keyar@chromium.org77a55222012-08-20 15:03:47 +000051}
52
scroggo@google.com9a412522012-09-07 15:21:18 +000053void PictureBenchmark::logProgress(const char msg[]) {
54 if (fLogger != NULL) {
55 fLogger->logProgress(msg);
56 }
57}
58
scroggo@google.com5239c322012-09-11 19:15:32 +000059PictureRenderer* PictureBenchmark::setRenderer(sk_tools::PictureRenderer* renderer) {
60 SkRefCnt_SafeAssign(fRenderer, renderer);
61 return renderer;
62}
63
scroggo@google.com9a412522012-09-07 15:21:18 +000064void PictureBenchmark::run(SkPicture* pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000065 SkASSERT(pict);
keyar@chromium.org78a35c52012-08-20 15:03:44 +000066 if (NULL == pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000067 return;
68 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000069
scroggo@google.com5239c322012-09-11 19:15:32 +000070 SkASSERT(fRenderer != NULL);
71 if (NULL == fRenderer) {
scroggo@google.com9a412522012-09-07 15:21:18 +000072 return;
73 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000074
scroggo@google.com5239c322012-09-11 19:15:32 +000075 fRenderer->init(pict);
76
77 // We throw this away to remove first time effects (such as paging in this program)
78 fRenderer->setup();
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000079 fRenderer->render(NULL);
scroggo@google.com08085f82013-01-28 20:40:24 +000080 fRenderer->resetState(true);
keyar@chromium.org163b5672012-08-01 17:53:29 +000081
scroggo@google.com9a412522012-09-07 15:21:18 +000082 bool usingGpu = false;
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +000083#if SK_SUPPORT_GPU
scroggo@google.com5239c322012-09-11 19:15:32 +000084 usingGpu = fRenderer->isUsingGpuDevice();
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +000085#endif
keyar@chromium.org77a55222012-08-20 15:03:47 +000086
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000087 uint32_t timerTypes = fTimerTypes;
88 if (!usingGpu) {
89 timerTypes &= ~TimerData::kGpu_Flag;
90 }
91
92 SkString timeFormat;
93 if (TimerData::kPerIter_Result == fTimerResult) {
94 timeFormat = fRenderer->getPerIterTimeFormat();
95 } else {
96 timeFormat = fRenderer->getNormalTimeFormat();
97 }
98
scroggo@google.comcbcef702012-12-13 22:09:28 +000099 if (fTimeIndividualTiles) {
100 TiledPictureRenderer* tiledRenderer = fRenderer->getTiledRenderer();
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000101 SkASSERT(tiledRenderer && tiledRenderer->supportsTimingIndividualTiles());
102 if (NULL == tiledRenderer || !tiledRenderer->supportsTimingIndividualTiles()) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000103 return;
104 }
105 int xTiles, yTiles;
106 if (!tiledRenderer->tileDimensions(xTiles, yTiles)) {
107 return;
108 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000109
scroggo@google.comcbcef702012-12-13 22:09:28 +0000110 // Insert a newline so that each tile is reported on its own line (separate from the line
111 // that describes the skp being run).
112 this->logProgress("\n");
scroggo@google.com9a412522012-09-07 15:21:18 +0000113
scroggo@google.comcbcef702012-12-13 22:09:28 +0000114 int x, y;
115 while (tiledRenderer->nextTile(x, y)) {
scroggo@google.com08085f82013-01-28 20:40:24 +0000116 // There are two timers, which will behave slightly differently:
117 // 1) longRunningTimer, along with perTileTimerData, will time how long it takes to draw
robertphillips@google.comfe1b5362013-02-07 19:45:46 +0000118 // one tile fRepeats times, and take the average. As such, it will not respect thea
scroggo@google.com08085f82013-01-28 20:40:24 +0000119 // logPerIter or printMin options, since it does not know the time per iteration. It
120 // will also be unable to call flush() for each tile.
121 // The goal of this timer is to make up for a system timer that is not precise enough to
122 // measure the small amount of time it takes to draw one tile once.
123 //
124 // 2) perTileTimer, along with perTileTimerData, will record each run separately, and
125 // then take the average. As such, it supports logPerIter and printMin options.
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000126 //
127 // Although "legal", having two gpu timers running at the same time
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000128 // seems to cause problems (i.e., INVALID_OPERATIONs) on several
129 // platforms. To work around this, we disable the gpu timer on the
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000130 // long running timer.
borenet@google.comb15e6062013-02-11 18:42:43 +0000131 SkAutoTDelete<BenchTimer> longRunningTimer(this->setupTimer());
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000132 TimerData longRunningTimerData(1);
borenet@google.comb15e6062013-02-11 18:42:43 +0000133 SkAutoTDelete<BenchTimer> perTileTimer(this->setupTimer(false));
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000134 TimerData perTileTimerData(fRepeats);
scroggo@google.com08085f82013-01-28 20:40:24 +0000135 longRunningTimer->start();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000136 for (int i = 0; i < fRepeats; ++i) {
scroggo@google.com08085f82013-01-28 20:40:24 +0000137 perTileTimer->start();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000138 tiledRenderer->drawCurrentTile();
scroggo@google.com08085f82013-01-28 20:40:24 +0000139 perTileTimer->truncatedEnd();
140 tiledRenderer->resetState(false);
141 perTileTimer->end();
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000142 SkAssertResult(perTileTimerData.appendTimes(perTileTimer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000143 }
scroggo@google.com08085f82013-01-28 20:40:24 +0000144 longRunningTimer->truncatedEnd();
145 tiledRenderer->resetState(true);
146 longRunningTimer->end();
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000147 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.com08085f82013-01-28 20:40:24 +0000148
scroggo@google.comcbcef702012-12-13 22:09:28 +0000149 SkString configName = tiledRenderer->getConfigName();
150 configName.appendf(": tile [%i,%i] out of [%i,%i]", x, y, xTiles, yTiles);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000151
152 SkString result = perTileTimerData.getResult(timeFormat.c_str(), fTimerResult,
153 configName.c_str(), timerTypes);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000154 result.append("\n");
borenet@google.comb15e6062013-02-11 18:42:43 +0000155
156// TODO(borenet): Turn off per-iteration tile time reporting for now. Avoiding logging the time
157// for every iteration for each tile cuts down on data file size by a significant amount. Re-enable
158// this once we're loading the bench data directly into a data store and are no longer generating
159// SVG graphs.
160#if 0
scroggo@google.comcbcef702012-12-13 22:09:28 +0000161 this->logProgress(result.c_str());
borenet@google.comb15e6062013-02-11 18:42:43 +0000162#endif
scroggo@google.com08085f82013-01-28 20:40:24 +0000163
164 configName.append(" <averaged>");
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000165 SkString longRunningResult = longRunningTimerData.getResult(
166 tiledRenderer->getNormalTimeFormat().c_str(),
167 TimerData::kAvg_Result,
168 configName.c_str(), timerTypes, fRepeats);
scroggo@google.com08085f82013-01-28 20:40:24 +0000169 longRunningResult.append("\n");
170 this->logProgress(longRunningResult.c_str());
scroggo@google.comcbcef702012-12-13 22:09:28 +0000171 }
172 } else {
scroggo@google.com08085f82013-01-28 20:40:24 +0000173 SkAutoTDelete<BenchTimer> timer(this->setupTimer());
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000174 TimerData timerData(fRepeats);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000175 for (int i = 0; i < fRepeats; ++i) {
176 fRenderer->setup();
keyar@chromium.org163b5672012-08-01 17:53:29 +0000177
scroggo@google.comcbcef702012-12-13 22:09:28 +0000178 timer->start();
179 fRenderer->render(NULL);
180 timer->truncatedEnd();
181
182 // Finishes gl context
scroggo@google.com08085f82013-01-28 20:40:24 +0000183 fRenderer->resetState(true);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000184 timer->end();
185
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000186 SkAssertResult(timerData.appendTimes(timer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000187 }
188
189 SkString configName = fRenderer->getConfigName();
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000190
191 SkString result = timerData.getResult(timeFormat.c_str(),
192 fTimerResult,
193 configName.c_str(),
194 timerTypes);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000195 result.append("\n");
196 this->logProgress(result.c_str());
keyar@chromium.org163b5672012-08-01 17:53:29 +0000197 }
198
scroggo@google.com5239c322012-09-11 19:15:32 +0000199 fRenderer->end();
keyar@chromium.org163b5672012-08-01 17:53:29 +0000200}
201
202}