jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 10 | #include "GMSlide.h" |
| 11 | #include "SKPSlide.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 12 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 13 | #include "SkCanvas.h" |
| 14 | #include "SkCommonFlags.h" |
| 15 | #include "SkOSFile.h" |
| 16 | #include "SkRandom.h" |
| 17 | #include "SkStream.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 18 | |
| 19 | Application* Application::Create(int argc, char** argv, void* platformData) { |
| 20 | return new VulkanViewer(argc, argv, platformData); |
| 21 | } |
| 22 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 23 | static bool on_key_handler(Window::Key key, Window::InputState state, uint32_t modifiers, |
| 24 | void* userData) { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 25 | VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData); |
| 26 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 27 | return vv->onKey(key, state, modifiers); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 28 | } |
| 29 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 30 | static 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 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 36 | static void on_paint_handler(SkCanvas* canvas, void* userData) { |
| 37 | VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData); |
| 38 | |
| 39 | return vv->onPaint(canvas); |
| 40 | } |
| 41 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 42 | DEFINE_bool2(fullscreen, f, true, "Run fullscreen."); |
| 43 | DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifying this builder."); |
| 44 | DEFINE_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 ~"); |
| 53 | DEFINE_string(skps, "skps", "Directory to read skps from."); |
| 54 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 55 | VulkanViewer::VulkanViewer(int argc, char** argv, void* platformData) |
| 56 | : fCurrentMeasurement(0) |
| 57 | , fDisplayStats(false) |
egdaniel | 2a0bb0a | 2016-04-11 08:30:40 -0700 | [diff] [blame] | 58 | , fZoomCenterX(0.0f) |
| 59 | , fZoomCenterY(0.0f) |
| 60 | , fZoomLevel(0.0f) |
| 61 | , fZoomScale(SK_Scalar1) |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 62 | { |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 63 | memset(fMeasurements, 0, sizeof(fMeasurements)); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 64 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 65 | 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 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 73 | fWindow = Window::CreateNativeWindow(platformData); |
| 74 | fWindow->attach(Window::kVulkan_BackendType, 0, nullptr); |
| 75 | |
| 76 | // register callbacks |
| 77 | fWindow->registerKeyFunc(on_key_handler, this); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 78 | fWindow->registerCharFunc(on_char_handler, this); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 79 | fWindow->registerPaintFunc(on_paint_handler, this); |
| 80 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 81 | // set up slides |
| 82 | this->initSlides(); |
| 83 | |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 84 | fAnimTimer.run(); |
| 85 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 86 | // set up first frame |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 87 | fCurrentSlide = 0; |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 88 | setupCurrentSlide(-1); |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 89 | updateMatrix(); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 90 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 91 | fWindow->show(); |
| 92 | } |
| 93 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 94 | void 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")) { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 117 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) { |
| 118 | continue; |
| 119 | } |
| 120 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 121 | SkString path(FLAGS_skps[i]); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 122 | sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path)); |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 123 | if (slide) { |
| 124 | fSlides.push_back(slide); |
| 125 | } |
| 126 | } else { |
| 127 | SkOSFile::Iter it(FLAGS_skps[i], ".skp"); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 128 | 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)); |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 136 | if (slide) { |
| 137 | fSlides.push_back(slide); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 145 | VulkanViewer::~VulkanViewer() { |
| 146 | fWindow->detach(); |
| 147 | delete fWindow; |
| 148 | } |
| 149 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 150 | void 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 | |
| 164 | void 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 | |
| 178 | void 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, ¢er); |
| 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 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 202 | bool VulkanViewer::onKey(Window::Key key, Window::InputState state, uint32_t modifiers) { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 203 | 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; |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 213 | } |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 214 | |
| 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; |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 226 | } |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 227 | |
| 228 | case Window::kUp_Key: { |
| 229 | this->changeZoomLevel(1.f / 32.f); |
egdaniel | 2a0bb0a | 2016-04-11 08:30:40 -0700 | [diff] [blame] | 230 | fWindow->inval(); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
| 234 | case Window::kDown_Key: { |
| 235 | this->changeZoomLevel(-1.f / 32.f); |
egdaniel | 2a0bb0a | 2016-04-11 08:30:40 -0700 | [diff] [blame] | 236 | fWindow->inval(); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 237 | return true; |
| 238 | } |
| 239 | |
| 240 | default: |
| 241 | break; |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 242 | } |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 243 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 244 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | |
| 248 | bool VulkanViewer::onChar(SkUnichar c, uint32_t modifiers) { |
| 249 | if ('s' == c) { |
| 250 | fDisplayStats = !fDisplayStats; |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | return false; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void VulkanViewer::onPaint(SkCanvas* canvas) { |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 258 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 259 | int count = canvas->save(); |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 260 | |
| 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); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 277 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 278 | fSlides[fCurrentSlide]->draw(canvas); |
| 279 | canvas->restoreToCount(count); |
| 280 | |
| 281 | if (fDisplayStats) { |
| 282 | drawStats(canvas); |
| 283 | } |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void 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 | |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 301 | if (fWindow->supportsContentRect()) { |
| 302 | SkRect contentRect = fWindow->getContentRect(); |
| 303 | canvas->clipRect(contentRect); |
| 304 | canvas->translate(contentRect.fLeft, contentRect.fTop); |
| 305 | } |
| 306 | |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 307 | 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); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 330 | |
| 331 | canvas->restore(); |
| 332 | } |
| 333 | |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 334 | void VulkanViewer::onIdle(double ms) { |
| 335 | // Record measurements |
| 336 | fMeasurements[fCurrentMeasurement++] = ms; |
| 337 | fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod |
| 338 | SkASSERT(fCurrentMeasurement < kMeasurementCount); |
| 339 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 340 | fAnimTimer.updateTime(); |
jvanverth | 9d5e47f | 2016-04-26 08:01:33 -0700 | [diff] [blame^] | 341 | if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 342 | fWindow->inval(); |
| 343 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 344 | } |