blob: e2a726f1a51a9cce6a0965e0fc39891927a03b06 [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()
18: fRepeats(1)
scroggo@google.com5239c322012-09-11 19:15:32 +000019, fRenderer(NULL)
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000020, fTimerResult(TimerData::kAvg_Result)
21, fTimerTypes(0)
scroggo@google.comcbcef702012-12-13 22:09:28 +000022, fTimeIndividualTiles(false)
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000023, fPurgeDecodedTex(false)
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +000024, fPreprocess(false)
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +000025, fWriter(NULL)
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
mtklein9ac68ee2014-06-20 11:29:20 -070045Timer* 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()) {
mtklein9ac68ee2014-06-20 11:29:20 -070048 return SkNEW_ARGS(Timer, (fRenderer->getGLContext()));
keyar@chromium.org77a55222012-08-20 15:03:47 +000049 }
keyar@chromium.org77a55222012-08-20 15:03:47 +000050#endif
mtklein9ac68ee2014-06-20 11:29:20 -070051 return SkNEW_ARGS(Timer, (NULL));
keyar@chromium.org77a55222012-08-20 15:03:47 +000052}
53
scroggo@google.com5239c322012-09-11 19:15:32 +000054PictureRenderer* PictureBenchmark::setRenderer(sk_tools::PictureRenderer* renderer) {
55 SkRefCnt_SafeAssign(fRenderer, renderer);
56 return renderer;
57}
58
scroggo@google.com9a412522012-09-07 15:21:18 +000059void PictureBenchmark::run(SkPicture* pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000060 SkASSERT(pict);
keyar@chromium.org78a35c52012-08-20 15:03:44 +000061 if (NULL == pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000062 return;
63 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000064
scroggo@google.com5239c322012-09-11 19:15:32 +000065 SkASSERT(fRenderer != NULL);
66 if (NULL == fRenderer) {
scroggo@google.com9a412522012-09-07 15:21:18 +000067 return;
68 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000069
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000070 fRenderer->init(pict, NULL, NULL, NULL, false);
scroggo@google.com5239c322012-09-11 19:15:32 +000071
72 // We throw this away to remove first time effects (such as paging in this program)
73 fRenderer->setup();
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +000074
75 if (fPreprocess) {
76 if (NULL != fRenderer->getCanvas()) {
77 fRenderer->getCanvas()->EXPERIMENTAL_optimize(pict);
78 }
79 }
80
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000081 fRenderer->render(NULL);
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +000082 fRenderer->resetState(true); // flush, swapBuffers and Finish
keyar@chromium.org163b5672012-08-01 17:53:29 +000083
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000084 if (fPurgeDecodedTex) {
85 fRenderer->purgeTextures();
86 }
87
scroggo@google.com9a412522012-09-07 15:21:18 +000088 bool usingGpu = false;
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +000089#if SK_SUPPORT_GPU
scroggo@google.com5239c322012-09-11 19:15:32 +000090 usingGpu = fRenderer->isUsingGpuDevice();
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +000091#endif
keyar@chromium.org77a55222012-08-20 15:03:47 +000092
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000093 uint32_t timerTypes = fTimerTypes;
94 if (!usingGpu) {
95 timerTypes &= ~TimerData::kGpu_Flag;
96 }
97
98 SkString timeFormat;
99 if (TimerData::kPerIter_Result == fTimerResult) {
100 timeFormat = fRenderer->getPerIterTimeFormat();
101 } else {
102 timeFormat = fRenderer->getNormalTimeFormat();
103 }
104
commit-bot@chromium.org359e4f02014-04-30 00:46:29 +0000105 static const int kNumInnerLoops = 10;
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000106 int numOuterLoops = 1;
107 int numInnerLoops = fRepeats;
108
109 if (TimerData::kPerIter_Result == fTimerResult && fRepeats > 1) {
110 // interpret this flag combination to mean: generate 'fRepeats'
111 // numbers by averaging each rendering 'kNumInnerLoops' times
112 numOuterLoops = fRepeats;
113 numInnerLoops = kNumInnerLoops;
114 }
115
scroggo@google.comcbcef702012-12-13 22:09:28 +0000116 if (fTimeIndividualTiles) {
117 TiledPictureRenderer* tiledRenderer = fRenderer->getTiledRenderer();
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000118 SkASSERT(tiledRenderer && tiledRenderer->supportsTimingIndividualTiles());
119 if (NULL == tiledRenderer || !tiledRenderer->supportsTimingIndividualTiles()) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000120 return;
121 }
122 int xTiles, yTiles;
123 if (!tiledRenderer->tileDimensions(xTiles, yTiles)) {
124 return;
125 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000126
scroggo@google.comcbcef702012-12-13 22:09:28 +0000127 int x, y;
128 while (tiledRenderer->nextTile(x, y)) {
scroggo@google.com08085f82013-01-28 20:40:24 +0000129 // There are two timers, which will behave slightly differently:
130 // 1) longRunningTimer, along with perTileTimerData, will time how long it takes to draw
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000131 // one tile fRepeats times, and take the average. As such, it will not respect the
scroggo@google.com08085f82013-01-28 20:40:24 +0000132 // logPerIter or printMin options, since it does not know the time per iteration. It
133 // will also be unable to call flush() for each tile.
134 // The goal of this timer is to make up for a system timer that is not precise enough to
135 // measure the small amount of time it takes to draw one tile once.
136 //
137 // 2) perTileTimer, along with perTileTimerData, will record each run separately, and
138 // then take the average. As such, it supports logPerIter and printMin options.
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000139 //
140 // Although "legal", having two gpu timers running at the same time
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000141 // seems to cause problems (i.e., INVALID_OPERATIONs) on several
142 // platforms. To work around this, we disable the gpu timer on the
robertphillips@google.com9b63c502013-02-07 20:20:27 +0000143 // long running timer.
mtklein9ac68ee2014-06-20 11:29:20 -0700144 SkAutoTDelete<Timer> longRunningTimer(this->setupTimer());
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000145 TimerData longRunningTimerData(numOuterLoops);
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000146
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000147 for (int outer = 0; outer < numOuterLoops; ++outer) {
mtklein9ac68ee2014-06-20 11:29:20 -0700148 SkAutoTDelete<Timer> perTileTimer(this->setupTimer(false));
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000149 TimerData perTileTimerData(numInnerLoops);
150
151 longRunningTimer->start();
152 for (int inner = 0; inner < numInnerLoops; ++inner) {
153 perTileTimer->start();
154 tiledRenderer->drawCurrentTile();
155 perTileTimer->truncatedEnd();
156 tiledRenderer->resetState(false); // flush & swapBuffers, but don't Finish
157 perTileTimer->end();
158 SkAssertResult(perTileTimerData.appendTimes(perTileTimer.get()));
159
160 if (fPurgeDecodedTex) {
161 fRenderer->purgeTextures();
162 }
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000163 }
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000164 longRunningTimer->truncatedEnd();
165 tiledRenderer->resetState(true); // flush, swapBuffers and Finish
166 longRunningTimer->end();
167 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000168 }
scroggo@google.com08085f82013-01-28 20:40:24 +0000169
kelvinly4d1a3642014-06-26 11:26:40 -0700170 fWriter->logRenderer(tiledRenderer);
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000171 fWriter->tileMeta(x, y, xTiles, yTiles);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000172
skia.committer@gmail.com0b708162014-03-12 03:02:18 +0000173 // TODO(borenet): Turn off per-iteration tile time reporting for now.
174 // Avoiding logging the time for every iteration for each tile cuts
175 // down on data file size by a significant amount. Re-enable this once
176 // we're loading the bench data directly into a data store and are no
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000177 // longer generating SVG graphs.
178#if 0
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000179 fWriter->tileData(
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000180 &perTileTimerData,
181 timeFormat.c_str(),
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000182 fTimerResult,
183 timerTypes);
borenet@google.comb15e6062013-02-11 18:42:43 +0000184#endif
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000185
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000186 if (fPurgeDecodedTex) {
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000187 fWriter->addTileFlag(PictureResultsWriter::kPurging);
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000188 }
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000189 fWriter->addTileFlag(PictureResultsWriter::kAvg);
190 fWriter->tileData(
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000191 &longRunningTimerData,
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000192 tiledRenderer->getNormalTimeFormat().c_str(),
193 TimerData::kAvg_Result,
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000194 timerTypes,
195 numInnerLoops);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000196 }
197 } else {
mtklein9ac68ee2014-06-20 11:29:20 -0700198 SkAutoTDelete<Timer> longRunningTimer(this->setupTimer());
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000199 TimerData longRunningTimerData(numOuterLoops);
robertphillips@google.com090601c2013-12-17 13:40:20 +0000200
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000201 for (int outer = 0; outer < numOuterLoops; ++outer) {
mtklein9ac68ee2014-06-20 11:29:20 -0700202 SkAutoTDelete<Timer> perRunTimer(this->setupTimer(false));
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000203 TimerData perRunTimerData(numInnerLoops);
keyar@chromium.org163b5672012-08-01 17:53:29 +0000204
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000205 longRunningTimer->start();
206 for (int inner = 0; inner < numInnerLoops; ++inner) {
207 fRenderer->setup();
scroggo@google.comcbcef702012-12-13 22:09:28 +0000208
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000209 perRunTimer->start();
210 fRenderer->render(NULL);
211 perRunTimer->truncatedEnd();
212 fRenderer->resetState(false); // flush & swapBuffers, but don't Finish
213 perRunTimer->end();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000214
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000215 SkAssertResult(perRunTimerData.appendTimes(perRunTimer.get()));
216
217 if (fPurgeDecodedTex) {
218 fRenderer->purgeTextures();
219 }
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000220 }
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000221 longRunningTimer->truncatedEnd();
222 fRenderer->resetState(true); // flush, swapBuffers and Finish
223 longRunningTimer->end();
224 SkAssertResult(longRunningTimerData.appendTimes(longRunningTimer.get()));
scroggo@google.comcbcef702012-12-13 22:09:28 +0000225 }
226
kelvinly4d1a3642014-06-26 11:26:40 -0700227 fWriter->logRenderer(fRenderer);
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000228 if (fPurgeDecodedTex) {
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000229 fWriter->addTileFlag(PictureResultsWriter::kPurging);
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000230 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000231
robertphillips@google.com090601c2013-12-17 13:40:20 +0000232 // Beware - since the per-run-timer doesn't ever include a glFinish it can
233 // report a lower time then the long-running-timer
234#if 0
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000235 fWriter->tileData(
236 &perRunTimerData,
237 timeFormat.c_str(),
238 fTimerResult,
239 timerTypes);
robertphillips@google.com090601c2013-12-17 13:40:20 +0000240#else
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000241 fWriter->tileData(
242 &longRunningTimerData,
243 timeFormat.c_str(),
244 fTimerResult,
245 timerTypes,
246 numInnerLoops);
robertphillips@google.com090601c2013-12-17 13:40:20 +0000247#endif
keyar@chromium.org163b5672012-08-01 17:53:29 +0000248 }
249
scroggo@google.com5239c322012-09-11 19:15:32 +0000250 fRenderer->end();
keyar@chromium.org163b5672012-08-01 17:53:29 +0000251}
252
253}