blob: c0343d715f4f3614d7b1607a24995d8fa4aa1872 [file] [log] [blame]
joshualittda7b8432015-05-27 09:19:03 -07001/*
2 * Copyright 2015 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 */
8
9#include "VisualBench.h"
10
joshualitt7fe8ee42015-06-01 10:03:54 -070011#include "ProcStats.h"
joshualittda7b8432015-05-27 09:19:03 -070012#include "SkApplication.h"
13#include "SkCanvas.h"
14#include "SkCommandLineFlags.h"
15#include "SkCommonFlags.h"
16#include "SkForceLinking.h"
17#include "SkGraphics.h"
18#include "SkGr.h"
19#include "SkImageDecoder.h"
20#include "SkOSFile.h"
21#include "SkStream.h"
joshualitt7fe8ee42015-06-01 10:03:54 -070022#include "Stats.h"
joshualittda7b8432015-05-27 09:19:03 -070023#include "gl/GrGLInterface.h"
24
25__SK_FORCE_IMAGE_DECODER_LINKING;
26
joshualitt7fe8ee42015-06-01 10:03:54 -070027DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
28DEFINE_int32(samples, 10, "Number of times to render each skp.");
29DEFINE_int32(loops, 5, "Number of times to time.");
30DEFINE_int32(msaa, 0, "Number of msaa samples.");
31
32static SkString humanize(double ms) {
33 if (FLAGS_verbose) {
34 return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
35 }
36 return HumanizeMs(ms);
37}
38
39#define HUMANIZE(time) humanize(time).c_str()
40
joshualittda7b8432015-05-27 09:19:03 -070041VisualBench::VisualBench(void* hwnd, int argc, char** argv)
42 : INHERITED(hwnd)
joshualitt7fe8ee42015-06-01 10:03:54 -070043 , fLoop(0)
joshualittda7b8432015-05-27 09:19:03 -070044 , fCurrentPicture(0)
joshualitt7fe8ee42015-06-01 10:03:54 -070045 , fCurrentSample(0)
46 , fState(kPreWarm_State) {
joshualittda7b8432015-05-27 09:19:03 -070047 SkCommandLineFlags::Parse(argc, argv);
48
joshualitt7fe8ee42015-06-01 10:03:54 -070049 // load all SKPs
joshualittda7b8432015-05-27 09:19:03 -070050 SkTArray<SkString> skps;
51 for (int i = 0; i < FLAGS_skps.count(); i++) {
52 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
53 skps.push_back() = FLAGS_skps[i];
joshualitt7fe8ee42015-06-01 10:03:54 -070054 fTimings.push_back().fName = FLAGS_skps[i];
joshualittda7b8432015-05-27 09:19:03 -070055 } else {
56 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
57 SkString path;
58 while (it.next(&path)) {
joshualitt030dc842015-06-12 12:51:44 -070059 skps.push_back() = SkOSPath::Join(FLAGS_skps[i], path.c_str());
joshualitt7fe8ee42015-06-01 10:03:54 -070060 fTimings.push_back().fName = path.c_str();
joshualittda7b8432015-05-27 09:19:03 -070061 }
62 }
63 }
64
joshualittda7b8432015-05-27 09:19:03 -070065 for (int i = 0; i < skps.count(); i++) {
66 SkFILEStream stream(skps[i].c_str());
67 if (stream.isValid()) {
68 fPictures.push_back(SkPicture::CreateFromStream(&stream));
69 } else {
70 SkDebugf("couldn't load picture at \"path\"\n", skps[i].c_str());
71 }
72 }
joshualitt7fe8ee42015-06-01 10:03:54 -070073
74 if (fPictures.empty()) {
75 SkDebugf("no valid skps found\n");
76 }
77
78 this->setTitle();
79 this->setupBackend();
joshualittda7b8432015-05-27 09:19:03 -070080}
81
82VisualBench::~VisualBench() {
83 for (int i = 0; i < fPictures.count(); i++) {
84 fPictures[i]->~SkPicture();
85 }
86 INHERITED::detach();
87}
88
89void VisualBench::setTitle() {
90 SkString title("VisualBench");
91 INHERITED::setTitle(title.c_str());
92}
93
94SkSurface* VisualBench::createSurface() {
95 SkSurfaceProps props(INHERITED::getSurfaceProps());
96 return SkSurface::NewRenderTargetDirect(fRenderTarget, &props);
97}
98
99bool VisualBench::setupBackend() {
100 this->setColorType(kRGBA_8888_SkColorType);
101 this->setVisibleP(true);
102 this->setClipToBounds(false);
103
joshualitt7fe8ee42015-06-01 10:03:54 -0700104 if (!this->attach(kNativeGL_BackEndType, FLAGS_msaa, &fAttachmentInfo)) {
joshualittda7b8432015-05-27 09:19:03 -0700105 SkDebugf("Not possible to create backend.\n");
106 INHERITED::detach();
107 return false;
108 }
109
110 this->setFullscreen(true);
111 this->setVsync(false);
joshualitt7fe8ee42015-06-01 10:03:54 -0700112 this->resetContext();
113 return true;
114}
joshualittda7b8432015-05-27 09:19:03 -0700115
joshualitt7fe8ee42015-06-01 10:03:54 -0700116void VisualBench::resetContext() {
joshualittda7b8432015-05-27 09:19:03 -0700117 fInterface.reset(GrGLCreateNativeInterface());
118 SkASSERT(fInterface);
119
120 // setup contexts
121 fContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface.get()));
122 SkASSERT(fContext);
123
124 // setup rendertargets
125 this->setupRenderTarget();
joshualittda7b8432015-05-27 09:19:03 -0700126}
127
128void VisualBench::setupRenderTarget() {
joshualitt030dc842015-06-12 12:51:44 -0700129 if (fContext) {
130 fRenderTarget.reset(this->renderTarget(fAttachmentInfo, fInterface, fContext));
131 }
joshualittda7b8432015-05-27 09:19:03 -0700132}
133
joshualitt7fe8ee42015-06-01 10:03:54 -0700134inline void VisualBench::renderFrame(SkCanvas* canvas) {
135 canvas->drawPicture(fPictures[fCurrentPicture]);
joshualittda7b8432015-05-27 09:19:03 -0700136 fContext->flush();
137 INHERITED::present();
joshualitt7fe8ee42015-06-01 10:03:54 -0700138}
joshualittda7b8432015-05-27 09:19:03 -0700139
joshualitt7fe8ee42015-06-01 10:03:54 -0700140void VisualBench::printStats() {
141 const SkTArray<double>& measurements = fTimings[fCurrentPicture].fMeasurements;
142 if (FLAGS_verbose) {
143 for (int i = 0; i < measurements.count(); i++) {
144 SkDebugf("%s ", HUMANIZE(measurements[i]));
145 }
146 SkDebugf("%s\n", fTimings[fCurrentPicture].fName.c_str());
147 } else {
148 SkASSERT(measurements.count());
149 Stats stats(measurements.begin(), measurements.count());
150 const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean;
151 SkDebugf("%4d/%-4dMB\t%s\t%s\t%s\t%s\t%.0f%%\t%s\n",
152 sk_tools::getCurrResidentSetSizeMB(),
153 sk_tools::getMaxResidentSetSizeMB(),
154 HUMANIZE(stats.min),
155 HUMANIZE(stats.median),
156 HUMANIZE(stats.mean),
157 HUMANIZE(stats.max),
158 stdDevPercent,
159 fTimings[fCurrentPicture].fName.c_str());
160 }
161}
162
163void VisualBench::timePicture(SkCanvas* canvas) {
164 this->renderFrame(canvas);
165 switch (fState) {
166 case kPreWarm_State: {
167 if (fCurrentSample >= FLAGS_gpuFrameLag) {
168 // TODO we currently time across all frames to make sure we capture all GPU work
169 // We should also rendering an empty SKP to get a baseline to subtract from
170 // our timing
171 fState = kTiming_State;
172 fCurrentSample -= FLAGS_gpuFrameLag;
173 fTimer.start();
174 } else {
175 fCurrentSample++;
176 }
177 break;
178 }
179 case kTiming_State: {
180 if (fCurrentSample >= FLAGS_samples) {
181 fTimer.end();
182 fTimings[fCurrentPicture].fMeasurements.push_back(fTimer.fWall / FLAGS_samples);
183 this->resetContext();
184 fTimer = WallTimer();
185 fState = kPreWarm_State;
186 fCurrentSample = 0;
187 if (fLoop++ > FLAGS_loops) {
188 this->printStats();
189 fCurrentPicture++;
190 fLoop = 0;
191 }
192 } else {
193 fCurrentSample++;
194 }
195 break;
196 }
197 }
198}
199
200void VisualBench::draw(SkCanvas* canvas) {
201 if (fCurrentPicture < fPictures.count()) {
202 this->timePicture(canvas);
203 } else {
204 this->closeWindow();
205 }
joshualittda7b8432015-05-27 09:19:03 -0700206
207 // Invalidate the window to force a redraw. Poor man's animation mechanism.
208 this->inval(NULL);
209}
210
211void VisualBench::onSizeChange() {
212 this->setupRenderTarget();
213}
214
215bool VisualBench::onHandleChar(SkUnichar unichar) {
216 return true;
217}
218
219// Externally declared entry points
220void application_init() {
221 SkGraphics::Init();
222 SkEvent::Init();
223}
224
225void application_term() {
226 SkEvent::Term();
227 SkGraphics::Term();
228}
229
230SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
231 return new VisualBench(hwnd, argc, argv);
232}
233