blob: bdf13061019e542ffaaa82ca6a2505ed8a38fad5 [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"
scroggo@google.com9a412522012-09-07 15:21:18 +000015#include "TimerData.h"
keyar@chromium.org163b5672012-08-01 17:53:29 +000016
17namespace sk_tools {
18
scroggo@google.com5239c322012-09-11 19:15:32 +000019PictureBenchmark::PictureBenchmark()
20: fRepeats(1)
21, fLogger(NULL)
22, fRenderer(NULL)
23, fLogPerIter(false)
24, fPrintMin(false)
25, fShowWallTime(false)
26, fShowTruncatedWallTime(false)
27, fShowCpuTime(true)
28, fShowTruncatedCpuTime(false)
29, fShowGpuTime(false)
scroggo@google.comcbcef702012-12-13 22:09:28 +000030, fTimeIndividualTiles(false)
scroggo@google.com5239c322012-09-11 19:15:32 +000031{}
32
33PictureBenchmark::~PictureBenchmark() {
34 SkSafeUnref(fRenderer);
35}
36
keyar@chromium.org77a55222012-08-20 15:03:47 +000037BenchTimer* PictureBenchmark::setupTimer() {
38#if SK_SUPPORT_GPU
scroggo@google.com5239c322012-09-11 19:15:32 +000039 if (fRenderer != NULL && fRenderer->isUsingGpuDevice()) {
40 return SkNEW_ARGS(BenchTimer, (fRenderer->getGLContext()));
keyar@chromium.org77a55222012-08-20 15:03:47 +000041 }
keyar@chromium.org77a55222012-08-20 15:03:47 +000042#endif
scroggo@google.comcbcef702012-12-13 22:09:28 +000043 return SkNEW_ARGS(BenchTimer, (NULL));
keyar@chromium.org77a55222012-08-20 15:03:47 +000044}
45
scroggo@google.com9a412522012-09-07 15:21:18 +000046void PictureBenchmark::logProgress(const char msg[]) {
47 if (fLogger != NULL) {
48 fLogger->logProgress(msg);
49 }
50}
51
scroggo@google.com5239c322012-09-11 19:15:32 +000052PictureRenderer* PictureBenchmark::setRenderer(sk_tools::PictureRenderer* renderer) {
53 SkRefCnt_SafeAssign(fRenderer, renderer);
54 return renderer;
55}
56
scroggo@google.com9a412522012-09-07 15:21:18 +000057void PictureBenchmark::run(SkPicture* pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000058 SkASSERT(pict);
keyar@chromium.org78a35c52012-08-20 15:03:44 +000059 if (NULL == pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000060 return;
61 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000062
scroggo@google.com5239c322012-09-11 19:15:32 +000063 SkASSERT(fRenderer != NULL);
64 if (NULL == fRenderer) {
scroggo@google.com9a412522012-09-07 15:21:18 +000065 return;
66 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000067
scroggo@google.com5239c322012-09-11 19:15:32 +000068 fRenderer->init(pict);
69
70 // We throw this away to remove first time effects (such as paging in this program)
71 fRenderer->setup();
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000072 fRenderer->render(NULL);
scroggo@google.com5239c322012-09-11 19:15:32 +000073 fRenderer->resetState();
keyar@chromium.org163b5672012-08-01 17:53:29 +000074
keyar@chromium.org77a55222012-08-20 15:03:47 +000075 BenchTimer* timer = this->setupTimer();
scroggo@google.com9a412522012-09-07 15:21:18 +000076 bool usingGpu = false;
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +000077#if SK_SUPPORT_GPU
scroggo@google.com5239c322012-09-11 19:15:32 +000078 usingGpu = fRenderer->isUsingGpuDevice();
keyar@chromium.orgf8a513f2012-08-20 15:03:52 +000079#endif
keyar@chromium.org77a55222012-08-20 15:03:47 +000080
scroggo@google.comcbcef702012-12-13 22:09:28 +000081 if (fTimeIndividualTiles) {
82 TiledPictureRenderer* tiledRenderer = fRenderer->getTiledRenderer();
83 SkASSERT(tiledRenderer);
84 if (NULL == tiledRenderer) {
85 return;
86 }
87 int xTiles, yTiles;
88 if (!tiledRenderer->tileDimensions(xTiles, yTiles)) {
89 return;
90 }
keyar@chromium.org163b5672012-08-01 17:53:29 +000091
scroggo@google.comcbcef702012-12-13 22:09:28 +000092 // Insert a newline so that each tile is reported on its own line (separate from the line
93 // that describes the skp being run).
94 this->logProgress("\n");
scroggo@google.com9a412522012-09-07 15:21:18 +000095
scroggo@google.comcbcef702012-12-13 22:09:28 +000096 int x, y;
97 while (tiledRenderer->nextTile(x, y)) {
98 TimerData timerData(tiledRenderer->getPerIterTimeFormat(),
99 tiledRenderer->getNormalTimeFormat());
100 for (int i = 0; i < fRepeats; ++i) {
101 timer->start();
102 tiledRenderer->drawCurrentTile();
103 timer->truncatedEnd();
104 tiledRenderer->resetState();
105 timer->end();
106 timerData.appendTimes(timer, fRepeats - 1 == i);
107 }
108 SkString configName = tiledRenderer->getConfigName();
109 configName.appendf(": tile [%i,%i] out of [%i,%i]", x, y, xTiles, yTiles);
110 SkString result = timerData.getResult(fLogPerIter, fPrintMin, fRepeats,
111 configName.c_str(), fShowWallTime,
112 fShowTruncatedWallTime, fShowCpuTime,
113 fShowTruncatedCpuTime, usingGpu && fShowGpuTime);
114 result.append("\n");
115 this->logProgress(result.c_str());
116 }
117 } else {
118 TimerData timerData(fRenderer->getPerIterTimeFormat(), fRenderer->getNormalTimeFormat());
119 for (int i = 0; i < fRepeats; ++i) {
120 fRenderer->setup();
keyar@chromium.org163b5672012-08-01 17:53:29 +0000121
scroggo@google.comcbcef702012-12-13 22:09:28 +0000122 timer->start();
123 fRenderer->render(NULL);
124 timer->truncatedEnd();
125
126 // Finishes gl context
127 fRenderer->resetState();
128 timer->end();
129
130 timerData.appendTimes(timer, fRepeats - 1 == i);
131 }
132
133 SkString configName = fRenderer->getConfigName();
134 SkString result = timerData.getResult(fLogPerIter, fPrintMin, fRepeats,
135 configName.c_str(), fShowWallTime,
136 fShowTruncatedWallTime, fShowCpuTime,
137 fShowTruncatedCpuTime, usingGpu && fShowGpuTime);
138 result.append("\n");
139 this->logProgress(result.c_str());
keyar@chromium.org163b5672012-08-01 17:53:29 +0000140 }
141
scroggo@google.com5239c322012-09-11 19:15:32 +0000142 fRenderer->end();
keyar@chromium.org378c97f2012-08-20 15:04:09 +0000143 SkDELETE(timer);
keyar@chromium.org163b5672012-08-01 17:53:29 +0000144}
145
146}