blob: f708f53613232e006d9ef73f16f3ef41dd372380 [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
mtklein9ac68ee2014-06-20 11:29:20 -07008#include "Timer.h"
keyar@chromium.org163b5672012-08-01 17:53:29 +00009#include "PictureBenchmark.h"
10#include "SkCanvas.h"
11#include "SkPicture.h"
12#include "SkString.h"
13#include "picture_utils.h"
14
15namespace sk_tools {
16
scroggo@google.com5239c322012-09-11 19:15:32 +000017PictureBenchmark::PictureBenchmark()
robertphillips78c71272014-10-09 04:59:19 -070018 : fRepeats(1)
19 , fRenderer(NULL)
20 , fTimerResult(TimerData::kAvg_Result)
21 , fTimerTypes(0)
22 , fTimeIndividualTiles(false)
23 , fPurgeDecodedTex(false)
24 , fWriter(NULL) {
25}
scroggo@google.com5239c322012-09-11 19:15:32 +000026
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
mtklein9ac68ee2014-06-20 11:29:20 -070044Timer* 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()) {
mtklein9ac68ee2014-06-20 11:29:20 -070047 return SkNEW_ARGS(Timer, (fRenderer->getGLContext()));
keyar@chromium.org77a55222012-08-20 15:03:47 +000048 }
keyar@chromium.org77a55222012-08-20 15:03:47 +000049#endif
mtklein9ac68ee2014-06-20 11:29:20 -070050 return SkNEW_ARGS(Timer, (NULL));
keyar@chromium.org77a55222012-08-20 15:03:47 +000051}
52
scroggo@google.com5239c322012-09-11 19:15:32 +000053PictureRenderer* PictureBenchmark::setRenderer(sk_tools::PictureRenderer* renderer) {
54 SkRefCnt_SafeAssign(fRenderer, renderer);
55 return renderer;
56}
57
robertphillips78c71272014-10-09 04:59:19 -070058void PictureBenchmark::run(SkPicture* pict, bool useMultiPictureDraw) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000059 SkASSERT(pict);
keyar@chromium.org78a35c52012-08-20 15:03:44 +000060 if (NULL == pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000061 return;
62 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000063
scroggo@google.com5239c322012-09-11 19:15:32 +000064 SkASSERT(fRenderer != NULL);
65 if (NULL == fRenderer) {
scroggo@google.com9a412522012-09-07 15:21:18 +000066 return;
67 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000068
robertphillips78c71272014-10-09 04:59:19 -070069 fRenderer->init(pict, NULL, NULL, NULL, false, useMultiPictureDraw);
scroggo@google.com5239c322012-09-11 19:15:32 +000070
71 // We throw this away to remove first time effects (such as paging in this program)
72 fRenderer->setup();
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +000073
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000074 fRenderer->render(NULL);
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +000075 fRenderer->resetState(true); // flush, swapBuffers and Finish
keyar@chromium.org163b5672012-08-01 17:53:29 +000076
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000077 if (fPurgeDecodedTex) {
78 fRenderer->purgeTextures();
79 }
80
scroggo@google.com9a412522012-09-07 15:21:18 +000081 bool usingGpu = false;
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +000082#if SK_SUPPORT_GPU
scroggo@google.com5239c322012-09-11 19:15:32 +000083 usingGpu = fRenderer->isUsingGpuDevice();
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +000084#endif
keyar@chromium.org77a55222012-08-20 15:03:47 +000085
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000086 uint32_t timerTypes = fTimerTypes;
87 if (!usingGpu) {
88 timerTypes &= ~TimerData::kGpu_Flag;
89 }
90
91 SkString timeFormat;
92 if (TimerData::kPerIter_Result == fTimerResult) {
93 timeFormat = fRenderer->getPerIterTimeFormat();
94 } else {
95 timeFormat = fRenderer->getNormalTimeFormat();
96 }
97
commit-bot@chromium.org359e4f02014-04-30 00:46:29 +000098 static const int kNumInnerLoops = 10;
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +000099 int numOuterLoops = 1;
100 int numInnerLoops = fRepeats;
101
102 if (TimerData::kPerIter_Result == fTimerResult && fRepeats > 1) {
103 // interpret this flag combination to mean: generate 'fRepeats'
104 // numbers by averaging each rendering 'kNumInnerLoops' times
105 numOuterLoops = fRepeats;
106 numInnerLoops = kNumInnerLoops;
107 }
108
scroggo@google.comcbcef702012-12-13 22:09:28 +0000109 if (fTimeIndividualTiles) {
110 TiledPictureRenderer* tiledRenderer = fRenderer->getTiledRenderer();
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000111 SkASSERT(tiledRenderer && tiledRenderer->supportsTimingIndividualTiles());
112 if (NULL == tiledRenderer || !tiledRenderer->supportsTimingIndividualTiles()) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000113 return;
114 }
115 int xTiles, yTiles;
116 if (!tiledRenderer->tileDimensions(xTiles, yTiles)) {
117 return;
118 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000119
scroggo@google.comcbcef702012-12-13 22:09:28 +0000120 int x, y;
121 while (tiledRenderer->nextTile(x, y)) {
scroggo@google.com08085f82013-01-28 20:40:24 +0000122 // There are two timers, which will behave slightly differently:
123 // 1) longRunningTimer, along with perTileTimerData, will time how long it takes to draw
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000124 // one tile fRepeats times, and take the average. As such, it will not respect the
scroggo@google.com08085f82013-01-28 20:40:24 +0000125 // logPerIter or printMin options, since it does not know the time per iteration. It
126 // will also be unable to call flush() for each tile.
127 // The goal of this timer is to make up for a system timer that is not precise enough to
128 // measure the small amount of time it takes to draw one tile once.
129 //
130 // 2) perTileTimer, along with perTileTimerData, will record each run separately, and
131 // then take the average. As such, it supports logPerIter and printMin options.
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000132 //
133 // Although "legal", having two gpu timers running at the same time
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000134 // seems to cause problems (i.e., INVALID_OPERATIONs) on several
135 // platforms. To work around this, we disable the gpu timer on the
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000136 // long running timer.
mtklein9ac68ee2014-06-20 11:29:20 -0700137 SkAutoTDelete<Timer> longRunningTimer(this->setupTimer());
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000138 TimerData longRunningTimerData(numOuterLoops);
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000139
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000140 for (int outer = 0; outer < numOuterLoops; ++outer) {
mtklein9ac68ee2014-06-20 11:29:20 -0700141 SkAutoTDelete<Timer> perTileTimer(this->setupTimer(false));
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000142 TimerData perTileTimerData(numInnerLoops);
143
144 longRunningTimer->start();
145 for (int inner = 0; inner < numInnerLoops; ++inner) {
146 perTileTimer->start();
147 tiledRenderer->drawCurrentTile();
148 perTileTimer->truncatedEnd();
149 tiledRenderer->resetState(false); // flush & swapBuffers, but don't Finish
150 perTileTimer->end();
151 SkAssertResult(perTileTimerData.appendTimes(perTileTimer.get()));
152
153 if (fPurgeDecodedTex) {
154 fRenderer->purgeTextures();
155 }
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000156 }
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000157 longRunningTimer->truncatedEnd();
158 tiledRenderer->resetState(true); // flush, swapBuffers and Finish
159 longRunningTimer->end();
160 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000161 }
scroggo@google.com08085f82013-01-28 20:40:24 +0000162
kelvinly4d1a3642014-06-26 11:26:40 -0700163 fWriter->logRenderer(tiledRenderer);
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000164 fWriter->tileMeta(x, y, xTiles, yTiles);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000165
skia.committer@gmail.com0b708162014-03-12 03:02:18 +0000166 // TODO(borenet): Turn off per-iteration tile time reporting for now.
167 // Avoiding logging the time for every iteration for each tile cuts
168 // down on data file size by a significant amount. Re-enable this once
169 // we're loading the bench data directly into a data store and are no
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000170 // longer generating SVG graphs.
171#if 0
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000172 fWriter->tileData(
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000173 &perTileTimerData,
174 timeFormat.c_str(),
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000175 fTimerResult,
176 timerTypes);
borenet@google.comb15e6062013-02-11 18:42:43 +0000177#endif
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000178
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000179 if (fPurgeDecodedTex) {
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000180 fWriter->addTileFlag(PictureResultsWriter::kPurging);
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000181 }
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000182 fWriter->addTileFlag(PictureResultsWriter::kAvg);
183 fWriter->tileData(
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000184 &longRunningTimerData,
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000185 tiledRenderer->getNormalTimeFormat().c_str(),
186 TimerData::kAvg_Result,
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000187 timerTypes,
188 numInnerLoops);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000189 }
190 } else {
mtklein9ac68ee2014-06-20 11:29:20 -0700191 SkAutoTDelete<Timer> longRunningTimer(this->setupTimer());
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000192 TimerData longRunningTimerData(numOuterLoops);
robertphillips@google.com090601c2013-12-17 13:40:20 +0000193
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000194 for (int outer = 0; outer < numOuterLoops; ++outer) {
mtklein9ac68ee2014-06-20 11:29:20 -0700195 SkAutoTDelete<Timer> perRunTimer(this->setupTimer(false));
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000196 TimerData perRunTimerData(numInnerLoops);
keyar@chromium.org163b5672012-08-01 17:53:29 +0000197
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000198 longRunningTimer->start();
199 for (int inner = 0; inner < numInnerLoops; ++inner) {
200 fRenderer->setup();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000201
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000202 perRunTimer->start();
203 fRenderer->render(NULL);
204 perRunTimer->truncatedEnd();
205 fRenderer->resetState(false); // flush & swapBuffers, but don't Finish
206 perRunTimer->end();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000207
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000208 SkAssertResult(perRunTimerData.appendTimes(perRunTimer.get()));
209
210 if (fPurgeDecodedTex) {
211 fRenderer->purgeTextures();
212 }
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000213 }
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000214 longRunningTimer->truncatedEnd();
215 fRenderer->resetState(true); // flush, swapBuffers and Finish
216 longRunningTimer->end();
217 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000218 }
219
kelvinly4d1a3642014-06-26 11:26:40 -0700220 fWriter->logRenderer(fRenderer);
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000221 if (fPurgeDecodedTex) {
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000222 fWriter->addTileFlag(PictureResultsWriter::kPurging);
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000223 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000224
robertphillips@google.com090601c2013-12-17 13:40:20 +0000225 // Beware - since the per-run-timer doesn't ever include a glFinish it can
226 // report a lower time then the long-running-timer
227#if 0
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000228 fWriter->tileData(
229 &perRunTimerData,
230 timeFormat.c_str(),
231 fTimerResult,
232 timerTypes);
robertphillips@google.com090601c2013-12-17 13:40:20 +0000233#else
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000234 fWriter->tileData(
235 &longRunningTimerData,
236 timeFormat.c_str(),
237 fTimerResult,
238 timerTypes,
239 numInnerLoops);
robertphillips@google.com090601c2013-12-17 13:40:20 +0000240#endif
keyar@chromium.org163b5672012-08-01 17:53:29 +0000241 }
242
scroggo@google.com5239c322012-09-11 19:15:32 +0000243 fRenderer->end();
keyar@chromium.org163b5672012-08-01 17:53:29 +0000244}
245
246}