blob: fd0662a008fe7b9b2ca1001b27eefa5cd9f8b7b0 [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)) {
59 skps.push_back() = SkOSPath::Join(FLAGS_skps[0], 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() {
129 fRenderTarget.reset(this->renderTarget(fAttachmentInfo, fInterface, fContext));
130}
131
joshualitt7fe8ee42015-06-01 10:03:54 -0700132inline void VisualBench::renderFrame(SkCanvas* canvas) {
133 canvas->drawPicture(fPictures[fCurrentPicture]);
joshualittda7b8432015-05-27 09:19:03 -0700134 fContext->flush();
135 INHERITED::present();
joshualitt7fe8ee42015-06-01 10:03:54 -0700136}
joshualittda7b8432015-05-27 09:19:03 -0700137
joshualitt7fe8ee42015-06-01 10:03:54 -0700138void VisualBench::printStats() {
139 const SkTArray<double>& measurements = fTimings[fCurrentPicture].fMeasurements;
140 if (FLAGS_verbose) {
141 for (int i = 0; i < measurements.count(); i++) {
142 SkDebugf("%s ", HUMANIZE(measurements[i]));
143 }
144 SkDebugf("%s\n", fTimings[fCurrentPicture].fName.c_str());
145 } else {
146 SkASSERT(measurements.count());
147 Stats stats(measurements.begin(), measurements.count());
148 const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean;
149 SkDebugf("%4d/%-4dMB\t%s\t%s\t%s\t%s\t%.0f%%\t%s\n",
150 sk_tools::getCurrResidentSetSizeMB(),
151 sk_tools::getMaxResidentSetSizeMB(),
152 HUMANIZE(stats.min),
153 HUMANIZE(stats.median),
154 HUMANIZE(stats.mean),
155 HUMANIZE(stats.max),
156 stdDevPercent,
157 fTimings[fCurrentPicture].fName.c_str());
158 }
159}
160
161void VisualBench::timePicture(SkCanvas* canvas) {
162 this->renderFrame(canvas);
163 switch (fState) {
164 case kPreWarm_State: {
165 if (fCurrentSample >= FLAGS_gpuFrameLag) {
166 // TODO we currently time across all frames to make sure we capture all GPU work
167 // We should also rendering an empty SKP to get a baseline to subtract from
168 // our timing
169 fState = kTiming_State;
170 fCurrentSample -= FLAGS_gpuFrameLag;
171 fTimer.start();
172 } else {
173 fCurrentSample++;
174 }
175 break;
176 }
177 case kTiming_State: {
178 if (fCurrentSample >= FLAGS_samples) {
179 fTimer.end();
180 fTimings[fCurrentPicture].fMeasurements.push_back(fTimer.fWall / FLAGS_samples);
181 this->resetContext();
182 fTimer = WallTimer();
183 fState = kPreWarm_State;
184 fCurrentSample = 0;
185 if (fLoop++ > FLAGS_loops) {
186 this->printStats();
187 fCurrentPicture++;
188 fLoop = 0;
189 }
190 } else {
191 fCurrentSample++;
192 }
193 break;
194 }
195 }
196}
197
198void VisualBench::draw(SkCanvas* canvas) {
199 if (fCurrentPicture < fPictures.count()) {
200 this->timePicture(canvas);
201 } else {
202 this->closeWindow();
203 }
joshualittda7b8432015-05-27 09:19:03 -0700204
205 // Invalidate the window to force a redraw. Poor man's animation mechanism.
206 this->inval(NULL);
207}
208
209void VisualBench::onSizeChange() {
210 this->setupRenderTarget();
211}
212
213bool VisualBench::onHandleChar(SkUnichar unichar) {
214 return true;
215}
216
217// Externally declared entry points
218void application_init() {
219 SkGraphics::Init();
220 SkEvent::Init();
221}
222
223void application_term() {
224 SkEvent::Term();
225 SkGraphics::Term();
226}
227
228SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
229 return new VisualBench(hwnd, argc, argv);
230}
231