blob: 65f36604c283129931551bf2a5d007d66bde6561 [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);
scroggo@google.com08085f82013-01-28 20:40:24 +000081 fRenderer->resetState(true);
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
scroggo@google.comcbcef702012-12-13 22:09:28 +0000104 if (fTimeIndividualTiles) {
105 TiledPictureRenderer* tiledRenderer = fRenderer->getTiledRenderer();
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000106 SkASSERT(tiledRenderer && tiledRenderer->supportsTimingIndividualTiles());
107 if (NULL == tiledRenderer || !tiledRenderer->supportsTimingIndividualTiles()) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000108 return;
109 }
110 int xTiles, yTiles;
111 if (!tiledRenderer->tileDimensions(xTiles, yTiles)) {
112 return;
113 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000114
scroggo@google.comcbcef702012-12-13 22:09:28 +0000115 // Insert a newline so that each tile is reported on its own line (separate from the line
116 // that describes the skp being run).
117 this->logProgress("\n");
scroggo@google.com9a412522012-09-07 15:21:18 +0000118
scroggo@google.comcbcef702012-12-13 22:09:28 +0000119 int x, y;
120 while (tiledRenderer->nextTile(x, y)) {
scroggo@google.com08085f82013-01-28 20:40:24 +0000121 // There are two timers, which will behave slightly differently:
122 // 1) longRunningTimer, along with perTileTimerData, will time how long it takes to draw
robertphillips@google.comfe1b5362013-02-07 19:45:46 +0000123 // one tile fRepeats times, and take the average. As such, it will not respect thea
scroggo@google.com08085f82013-01-28 20:40:24 +0000124 // logPerIter or printMin options, since it does not know the time per iteration. It
125 // will also be unable to call flush() for each tile.
126 // The goal of this timer is to make up for a system timer that is not precise enough to
127 // measure the small amount of time it takes to draw one tile once.
128 //
129 // 2) perTileTimer, along with perTileTimerData, will record each run separately, and
130 // then take the average. As such, it supports logPerIter and printMin options.
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000131 //
132 // Although "legal", having two gpu timers running at the same time
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000133 // seems to cause problems (i.e., INVALID_OPERATIONs) on several
134 // platforms. To work around this, we disable the gpu timer on the
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000135 // long running timer.
borenet@google.comb15e6062013-02-11 18:42:43 +0000136 SkAutoTDelete<BenchTimer> longRunningTimer(this->setupTimer());
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000137 TimerData longRunningTimerData(1);
borenet@google.comb15e6062013-02-11 18:42:43 +0000138 SkAutoTDelete<BenchTimer> perTileTimer(this->setupTimer(false));
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000139 TimerData perTileTimerData(fRepeats);
scroggo@google.com08085f82013-01-28 20:40:24 +0000140 longRunningTimer->start();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000141 for (int i = 0; i < fRepeats; ++i) {
scroggo@google.com08085f82013-01-28 20:40:24 +0000142 perTileTimer->start();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000143 tiledRenderer->drawCurrentTile();
scroggo@google.com08085f82013-01-28 20:40:24 +0000144 perTileTimer->truncatedEnd();
145 tiledRenderer->resetState(false);
146 perTileTimer->end();
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000147 SkAssertResult(perTileTimerData.appendTimes(perTileTimer.get()));
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000148
149 if (fPurgeDecodedTex) {
150 fRenderer->purgeTextures();
151 }
scroggo@google.comcbcef702012-12-13 22:09:28 +0000152 }
scroggo@google.com08085f82013-01-28 20:40:24 +0000153 longRunningTimer->truncatedEnd();
154 tiledRenderer->resetState(true);
155 longRunningTimer->end();
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000156 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.com08085f82013-01-28 20:40:24 +0000157
scroggo@google.comcbcef702012-12-13 22:09:28 +0000158 SkString configName = tiledRenderer->getConfigName();
159 configName.appendf(": tile [%i,%i] out of [%i,%i]", x, y, xTiles, yTiles);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000160
161 SkString result = perTileTimerData.getResult(timeFormat.c_str(), fTimerResult,
162 configName.c_str(), timerTypes);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000163 result.append("\n");
borenet@google.comb15e6062013-02-11 18:42:43 +0000164
165// TODO(borenet): Turn off per-iteration tile time reporting for now. Avoiding logging the time
166// for every iteration for each tile cuts down on data file size by a significant amount. Re-enable
167// this once we're loading the bench data directly into a data store and are no longer generating
168// SVG graphs.
169#if 0
scroggo@google.comcbcef702012-12-13 22:09:28 +0000170 this->logProgress(result.c_str());
borenet@google.comb15e6062013-02-11 18:42:43 +0000171#endif
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000172 if (fPurgeDecodedTex) {
173 configName.append(" <withPurging>");
174 }
scroggo@google.com08085f82013-01-28 20:40:24 +0000175 configName.append(" <averaged>");
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000176 SkString longRunningResult = longRunningTimerData.getResult(
177 tiledRenderer->getNormalTimeFormat().c_str(),
178 TimerData::kAvg_Result,
179 configName.c_str(), timerTypes, fRepeats);
scroggo@google.com08085f82013-01-28 20:40:24 +0000180 longRunningResult.append("\n");
181 this->logProgress(longRunningResult.c_str());
scroggo@google.comcbcef702012-12-13 22:09:28 +0000182 }
183 } else {
robertphillips@google.com090601c2013-12-17 13:40:20 +0000184 SkAutoTDelete<BenchTimer> longRunningTimer(this->setupTimer());
185 TimerData longRunningTimerData(1);
186 SkAutoTDelete<BenchTimer> perRunTimer(this->setupTimer(false));
187 TimerData perRunTimerData(fRepeats);
188
189 longRunningTimer->start();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000190 for (int i = 0; i < fRepeats; ++i) {
191 fRenderer->setup();
keyar@chromium.org163b5672012-08-01 17:53:29 +0000192
robertphillips@google.com090601c2013-12-17 13:40:20 +0000193 perRunTimer->start();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000194 fRenderer->render(NULL);
robertphillips@google.com090601c2013-12-17 13:40:20 +0000195 perRunTimer->truncatedEnd();
196 fRenderer->resetState(false);
197 perRunTimer->end();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000198
robertphillips@google.com090601c2013-12-17 13:40:20 +0000199 SkAssertResult(perRunTimerData.appendTimes(perRunTimer.get()));
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000200
201 if (fPurgeDecodedTex) {
202 fRenderer->purgeTextures();
203 }
scroggo@google.comcbcef702012-12-13 22:09:28 +0000204 }
robertphillips@google.com090601c2013-12-17 13:40:20 +0000205 longRunningTimer->truncatedEnd();
206 fRenderer->resetState(true);
207 longRunningTimer->end();
208 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000209
210 SkString configName = fRenderer->getConfigName();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000211 if (fPurgeDecodedTex) {
212 configName.append(" <withPurging>");
213 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000214
robertphillips@google.com090601c2013-12-17 13:40:20 +0000215 // Beware - since the per-run-timer doesn't ever include a glFinish it can
216 // report a lower time then the long-running-timer
217#if 0
218 SkString result = perRunTimerData.getResult(timeFormat.c_str(),
219 fTimerResult,
220 configName.c_str(),
221 timerTypes);
222 result.append("\n");
223
224 this->logProgress(result.c_str());
225#else
skia.committer@gmail.com3b85deb2013-12-18 07:01:56 +0000226 SkString result = longRunningTimerData.getResult(timeFormat.c_str(),
227 fTimerResult,
228 configName.c_str(),
229 timerTypes,
robertphillips@google.com090601c2013-12-17 13:40:20 +0000230 fRepeats);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000231 result.append("\n");
232 this->logProgress(result.c_str());
robertphillips@google.com090601c2013-12-17 13:40:20 +0000233#endif
keyar@chromium.org163b5672012-08-01 17:53:29 +0000234 }
235
scroggo@google.com5239c322012-09-11 19:15:32 +0000236 fRenderer->end();
keyar@chromium.org163b5672012-08-01 17:53:29 +0000237}
238
239}