blob: a27f5b67ad26668a60d16ac94b989b05c3809bf4 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 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#include "VulkanViewer.h"
9
jvanverth2bb3b6d2016-04-08 07:24:09 -070010#include "GMSlide.h"
11#include "SKPSlide.h"
jvanverth9f372462016-04-06 06:08:59 -070012
jvanverth2bb3b6d2016-04-08 07:24:09 -070013#include "SkCanvas.h"
14#include "SkCommonFlags.h"
15#include "SkOSFile.h"
16#include "SkRandom.h"
17#include "SkStream.h"
jvanverth9f372462016-04-06 06:08:59 -070018
19Application* Application::Create(int argc, char** argv, void* platformData) {
20 return new VulkanViewer(argc, argv, platformData);
21}
22
jvanverth9fab59d2016-04-06 12:08:51 -070023static bool on_key_handler(Window::Key key, Window::InputState state, uint32_t modifiers,
24 void* userData) {
jvanverth9f372462016-04-06 06:08:59 -070025 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData);
26
jvanverth9fab59d2016-04-06 12:08:51 -070027 return vv->onKey(key, state, modifiers);
jvanverth9f372462016-04-06 06:08:59 -070028}
29
jvanverthc265a922016-04-08 12:51:45 -070030static bool on_char_handler(SkUnichar c, uint32_t modifiers, void* userData) {
31 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData);
32
33 return vv->onChar(c, modifiers);
34}
35
jvanverth9f372462016-04-06 06:08:59 -070036static void on_paint_handler(SkCanvas* canvas, void* userData) {
37 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData);
38
39 return vv->onPaint(canvas);
40}
41
jvanverth2bb3b6d2016-04-08 07:24:09 -070042DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
43DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifying this builder.");
44DEFINE_string2(match, m, nullptr,
45 "[~][^]substring[$] [...] of bench name to run.\n"
46 "Multiple matches may be separated by spaces.\n"
47 "~ causes a matching bench to always be skipped\n"
48 "^ requires the start of the bench to match\n"
49 "$ requires the end of the bench to match\n"
50 "^ and $ requires an exact match\n"
51 "If a bench does not match any list entry,\n"
52 "it is skipped unless some list entry starts with ~");
53DEFINE_string(skps, "skps", "Directory to read skps from.");
54
jvanverthc265a922016-04-08 12:51:45 -070055VulkanViewer::VulkanViewer(int argc, char** argv, void* platformData)
56 : fCurrentMeasurement(0)
57 , fDisplayStats(false)
egdaniel2a0bb0a2016-04-11 08:30:40 -070058 , fZoomCenterX(0.0f)
59 , fZoomCenterY(0.0f)
60 , fZoomLevel(0.0f)
61 , fZoomScale(SK_Scalar1)
jvanverthc265a922016-04-08 12:51:45 -070062{
jvanverth3d6ed3a2016-04-07 11:09:51 -070063 memset(fMeasurements, 0, sizeof(fMeasurements));
jvanverth9f372462016-04-06 06:08:59 -070064
jvanverth2bb3b6d2016-04-08 07:24:09 -070065 SkDebugf("Command line arguments: ");
66 for (int i = 1; i < argc; ++i) {
67 SkDebugf("%s ", argv[i]);
68 }
69 SkDebugf("\n");
70
71 SkCommandLineFlags::Parse(argc, argv);
72
jvanverth9f372462016-04-06 06:08:59 -070073 fWindow = Window::CreateNativeWindow(platformData);
74 fWindow->attach(Window::kVulkan_BackendType, 0, nullptr);
75
76 // register callbacks
77 fWindow->registerKeyFunc(on_key_handler, this);
jvanverthc265a922016-04-08 12:51:45 -070078 fWindow->registerCharFunc(on_char_handler, this);
jvanverth9f372462016-04-06 06:08:59 -070079 fWindow->registerPaintFunc(on_paint_handler, this);
80
jvanverth2bb3b6d2016-04-08 07:24:09 -070081 // set up slides
82 this->initSlides();
83
djsollen12d62a72016-04-21 07:59:44 -070084 fAnimTimer.run();
85
jvanverth2bb3b6d2016-04-08 07:24:09 -070086 // set up first frame
jvanverth2bb3b6d2016-04-08 07:24:09 -070087 fCurrentSlide = 0;
jvanverthc265a922016-04-08 12:51:45 -070088 setupCurrentSlide(-1);
djsollen12d62a72016-04-21 07:59:44 -070089 updateMatrix();
jvanverthc265a922016-04-08 12:51:45 -070090
jvanverth9f372462016-04-06 06:08:59 -070091 fWindow->show();
92}
93
jvanverth2bb3b6d2016-04-08 07:24:09 -070094void VulkanViewer::initSlides() {
95 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
96 while (gms) {
97 SkAutoTDelete<skiagm::GM> gm(gms->factory()(nullptr));
98
99 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
100 sk_sp<Slide> slide(new GMSlide(gm.release()));
101 fSlides.push_back(slide);
102 }
103
104 gms = gms->next();
105 }
106
107 // reverse array
108 for (int i = 0; i < fSlides.count()/2; ++i) {
109 sk_sp<Slide> temp = fSlides[i];
110 fSlides[i] = fSlides[fSlides.count() - i - 1];
111 fSlides[fSlides.count() - i - 1] = temp;
112 }
113
114 // SKPs
115 for (int i = 0; i < FLAGS_skps.count(); i++) {
116 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700117 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
118 continue;
119 }
120
jvanverth2bb3b6d2016-04-08 07:24:09 -0700121 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700122 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700123 if (slide) {
124 fSlides.push_back(slide);
125 }
126 } else {
127 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700128 SkString skpName;
129 while (it.next(&skpName)) {
130 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
131 continue;
132 }
133
134 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
135 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700136 if (slide) {
137 fSlides.push_back(slide);
138 }
139 }
140 }
141 }
142}
143
144
jvanverth9f372462016-04-06 06:08:59 -0700145VulkanViewer::~VulkanViewer() {
146 fWindow->detach();
147 delete fWindow;
148}
149
jvanverthc265a922016-04-08 12:51:45 -0700150void VulkanViewer::setupCurrentSlide(int previousSlide) {
151 SkString title("VulkanViewer: ");
152 title.append(fSlides[fCurrentSlide]->getName());
153 fSlides[fCurrentSlide]->load();
154 if (previousSlide >= 0) {
155 fSlides[previousSlide]->unload();
156 }
157 fWindow->setTitle(title.c_str());
158 fWindow->inval();
159}
160
161#define MAX_ZOOM_LEVEL 8
162#define MIN_ZOOM_LEVEL -8
163
164void VulkanViewer::changeZoomLevel(float delta) {
165 fZoomLevel += delta;
166 if (fZoomLevel > 0) {
167 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
168 fZoomScale = fZoomLevel + SK_Scalar1;
169 } else if (fZoomLevel < 0) {
170 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
171 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
172 } else {
173 fZoomScale = SK_Scalar1;
174 }
175 this->updateMatrix();
176}
177
178void VulkanViewer::updateMatrix(){
179 SkMatrix m;
180 m.reset();
181
182 if (fZoomLevel) {
183 SkPoint center;
184 //m = this->getLocalMatrix();//.invert(&m);
185 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
186 SkScalar cx = center.fX;
187 SkScalar cy = center.fY;
188
189 m.setTranslate(-cx, -cy);
190 m.postScale(fZoomScale, fZoomScale);
191 m.postTranslate(cx, cy);
192 }
193
194 // TODO: add gesture support
195 // Apply any gesture matrix
196 //m.preConcat(fGesture.localM());
197 //m.preConcat(fGesture.globalM());
198
199 fLocalMatrix = m;
200}
201
jvanverth9fab59d2016-04-06 12:08:51 -0700202bool VulkanViewer::onKey(Window::Key key, Window::InputState state, uint32_t modifiers) {
jvanverthc265a922016-04-08 12:51:45 -0700203 if (Window::kDown_InputState == state) {
204 switch (key) {
205 case Window::kRight_Key: {
206 int previousSlide = fCurrentSlide;
207 fCurrentSlide++;
208 if (fCurrentSlide >= fSlides.count()) {
209 fCurrentSlide = 0;
210 }
211 setupCurrentSlide(previousSlide);
212 return true;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700213 }
jvanverthc265a922016-04-08 12:51:45 -0700214
215 case Window::kLeft_Key: {
216 int previousSlide = fCurrentSlide;
217 fCurrentSlide--;
218 if (fCurrentSlide < 0) {
219 fCurrentSlide = fSlides.count() - 1;
220 }
221 SkString title("VulkanViewer: ");
222 title.append(fSlides[fCurrentSlide]->getName());
223 fWindow->setTitle(title.c_str());
224 setupCurrentSlide(previousSlide);
225 return true;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700226 }
jvanverthc265a922016-04-08 12:51:45 -0700227
228 case Window::kUp_Key: {
229 this->changeZoomLevel(1.f / 32.f);
egdaniel2a0bb0a2016-04-11 08:30:40 -0700230 fWindow->inval();
jvanverthc265a922016-04-08 12:51:45 -0700231 return true;
232 }
233
234 case Window::kDown_Key: {
235 this->changeZoomLevel(-1.f / 32.f);
egdaniel2a0bb0a2016-04-11 08:30:40 -0700236 fWindow->inval();
jvanverthc265a922016-04-08 12:51:45 -0700237 return true;
238 }
239
240 default:
241 break;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700242 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700243 }
jvanverth9f372462016-04-06 06:08:59 -0700244
jvanverthc265a922016-04-08 12:51:45 -0700245 return false;
246}
247
248bool VulkanViewer::onChar(SkUnichar c, uint32_t modifiers) {
249 if ('s' == c) {
250 fDisplayStats = !fDisplayStats;
251 return true;
252 }
253
254 return false;
jvanverth9f372462016-04-06 06:08:59 -0700255}
256
257void VulkanViewer::onPaint(SkCanvas* canvas) {
jvanverth2bb3b6d2016-04-08 07:24:09 -0700258
jvanverthc265a922016-04-08 12:51:45 -0700259 int count = canvas->save();
djsollen12d62a72016-04-21 07:59:44 -0700260
261 if (fWindow->supportsContentRect()) {
262 SkRect contentRect = fWindow->getContentRect();
263 canvas->clipRect(contentRect);
264 canvas->translate(contentRect.fLeft, contentRect.fTop);
265 }
266
267 canvas->clear(SK_ColorWHITE);
268 if (fWindow->supportsContentRect() && fWindow->scaleContentToFit()) {
269 const SkRect contentRect = fWindow->getContentRect();
270 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
271 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
272 SkMatrix matrix;
273 matrix.setRectToRect(slideBounds, contentRect, SkMatrix::kCenter_ScaleToFit);
274 canvas->concat(matrix);
275 }
276 canvas->concat(fLocalMatrix);
jvanverth3d6ed3a2016-04-07 11:09:51 -0700277
jvanverthc265a922016-04-08 12:51:45 -0700278 fSlides[fCurrentSlide]->draw(canvas);
279 canvas->restoreToCount(count);
280
281 if (fDisplayStats) {
282 drawStats(canvas);
283 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700284}
285
286void VulkanViewer::drawStats(SkCanvas* canvas) {
287 static const float kPixelPerMS = 2.0f;
288 static const int kDisplayWidth = 130;
289 static const int kDisplayHeight = 100;
290 static const int kDisplayPadding = 10;
291 static const int kGraphPadding = 3;
292 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
293
294 SkISize canvasSize = canvas->getDeviceSize();
295 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
296 SkIntToScalar(kDisplayPadding),
297 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
298 SkPaint paint;
299 canvas->save();
300
djsollen12d62a72016-04-21 07:59:44 -0700301 if (fWindow->supportsContentRect()) {
302 SkRect contentRect = fWindow->getContentRect();
303 canvas->clipRect(contentRect);
304 canvas->translate(contentRect.fLeft, contentRect.fTop);
305 }
306
jvanverth3d6ed3a2016-04-07 11:09:51 -0700307 canvas->clipRect(rect);
308 paint.setColor(SK_ColorBLACK);
309 canvas->drawRect(rect, paint);
310 // draw the 16ms line
311 paint.setColor(SK_ColorLTGRAY);
312 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
313 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
314 paint.setColor(SK_ColorRED);
315 paint.setStyle(SkPaint::kStroke_Style);
316 canvas->drawRect(rect, paint);
317
318 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
319 const int xStep = 2;
320 const int startY = SkScalarTruncToInt(rect.fBottom);
321 int i = fCurrentMeasurement;
322 do {
323 int endY = startY - (int)(fMeasurements[i] * kPixelPerMS + 0.5); // round to nearest value
324 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
325 SkIntToScalar(x), SkIntToScalar(endY), paint);
326 i++;
327 i &= (kMeasurementCount - 1); // fast mod
328 x += xStep;
329 } while (i != fCurrentMeasurement);
jvanverth9f372462016-04-06 06:08:59 -0700330
331 canvas->restore();
332}
333
jvanverth3d6ed3a2016-04-07 11:09:51 -0700334void VulkanViewer::onIdle(double ms) {
335 // Record measurements
336 fMeasurements[fCurrentMeasurement++] = ms;
337 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod
338 SkASSERT(fCurrentMeasurement < kMeasurementCount);
339
jvanverthc265a922016-04-08 12:51:45 -0700340 fAnimTimer.updateTime();
jvanverth9d5e47f2016-04-26 08:01:33 -0700341 if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) {
jvanverthc265a922016-04-08 12:51:45 -0700342 fWindow->inval();
343 }
jvanverth9f372462016-04-06 06:08:59 -0700344}