| OLD | NEW |
| (Empty) |
| 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 | |
| 10 #include "GMSlide.h" | |
| 11 #include "SKPSlide.h" | |
| 12 | |
| 13 #include "SkCanvas.h" | |
| 14 #include "SkCommonFlags.h" | |
| 15 #include "SkOSFile.h" | |
| 16 #include "SkRandom.h" | |
| 17 #include "SkStream.h" | |
| 18 | |
| 19 Application* Application::Create(int argc, char** argv, void* platformData) { | |
| 20 return new VulkanViewer(argc, argv, platformData); | |
| 21 } | |
| 22 | |
| 23 static bool on_key_handler(Window::Key key, Window::InputState state, uint32_t m
odifiers, | |
| 24 void* userData) { | |
| 25 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData); | |
| 26 | |
| 27 return vv->onKey(key, state, modifiers); | |
| 28 } | |
| 29 | |
| 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 | |
| 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 | |
| 42 DEFINE_bool2(fullscreen, f, true, "Run fullscreen."); | |
| 43 DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifyi
ng 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 | |
| 55 VulkanViewer::VulkanViewer(int argc, char** argv, void* platformData) | |
| 56 : fCurrentMeasurement(0) | |
| 57 , fDisplayStats(false) | |
| 58 , fZoomCenterX(0.0f) | |
| 59 , fZoomCenterY(0.0f) | |
| 60 , fZoomLevel(0.0f) | |
| 61 , fZoomScale(SK_Scalar1) | |
| 62 { | |
| 63 memset(fMeasurements, 0, sizeof(fMeasurements)); | |
| 64 | |
| 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 | |
| 73 fWindow = Window::CreateNativeWindow(platformData); | |
| 74 fWindow->attach(Window::kVulkan_BackendType, 0); | |
| 75 | |
| 76 // register callbacks | |
| 77 fWindow->registerKeyFunc(on_key_handler, this); | |
| 78 fWindow->registerCharFunc(on_char_handler, this); | |
| 79 fWindow->registerPaintFunc(on_paint_handler, this); | |
| 80 | |
| 81 // set up slides | |
| 82 this->initSlides(); | |
| 83 | |
| 84 fAnimTimer.run(); | |
| 85 | |
| 86 // set up first frame | |
| 87 fCurrentSlide = 0; | |
| 88 setupCurrentSlide(-1); | |
| 89 updateMatrix(); | |
| 90 | |
| 91 fWindow->show(); | |
| 92 } | |
| 93 | |
| 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")) { | |
| 117 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) { | |
| 118 continue; | |
| 119 } | |
| 120 | |
| 121 SkString path(FLAGS_skps[i]); | |
| 122 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()),
path)); | |
| 123 if (slide) { | |
| 124 fSlides.push_back(slide); | |
| 125 } | |
| 126 } else { | |
| 127 SkOSFile::Iter it(FLAGS_skps[i], ".skp"); | |
| 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)); | |
| 136 if (slide) { | |
| 137 fSlides.push_back(slide); | |
| 138 } | |
| 139 } | |
| 140 } | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 | |
| 145 VulkanViewer::~VulkanViewer() { | |
| 146 fWindow->detach(); | |
| 147 delete fWindow; | |
| 148 } | |
| 149 | |
| 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 | |
| 202 bool VulkanViewer::onKey(Window::Key key, Window::InputState state, uint32_t mod
ifiers) { | |
| 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; | |
| 213 } | |
| 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; | |
| 226 } | |
| 227 | |
| 228 case Window::kUp_Key: { | |
| 229 this->changeZoomLevel(1.f / 32.f); | |
| 230 fWindow->inval(); | |
| 231 return true; | |
| 232 } | |
| 233 | |
| 234 case Window::kDown_Key: { | |
| 235 this->changeZoomLevel(-1.f / 32.f); | |
| 236 fWindow->inval(); | |
| 237 return true; | |
| 238 } | |
| 239 | |
| 240 default: | |
| 241 break; | |
| 242 } | |
| 243 } | |
| 244 | |
| 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; | |
| 255 } | |
| 256 | |
| 257 void VulkanViewer::onPaint(SkCanvas* canvas) { | |
| 258 | |
| 259 int count = canvas->save(); | |
| 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_ScaleTo
Fit); | |
| 274 canvas->concat(matrix); | |
| 275 } | |
| 276 canvas->concat(fLocalMatrix); | |
| 277 | |
| 278 fSlides[fCurrentSlide]->draw(canvas); | |
| 279 canvas->restoreToCount(count); | |
| 280 | |
| 281 if (fDisplayStats) { | |
| 282 drawStats(canvas); | |
| 283 } | |
| 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(k
DisplayHeight)); | |
| 298 SkPaint paint; | |
| 299 canvas->save(); | |
| 300 | |
| 301 if (fWindow->supportsContentRect()) { | |
| 302 SkRect contentRect = fWindow->getContentRect(); | |
| 303 canvas->clipRect(contentRect); | |
| 304 canvas->translate(contentRect.fLeft, contentRect.fTop); | |
| 305 } | |
| 306 | |
| 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); // rou
nd 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); | |
| 330 | |
| 331 canvas->restore(); | |
| 332 } | |
| 333 | |
| 334 void VulkanViewer::onIdle(double ms) { | |
| 335 // Record measurements | |
| 336 fMeasurements[fCurrentMeasurement++] = ms; | |
| 337 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod | |
| 338 SkASSERT(fCurrentMeasurement < kMeasurementCount); | |
| 339 | |
| 340 fAnimTimer.updateTime(); | |
| 341 if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) { | |
| 342 fWindow->inval(); | |
| 343 } | |
| 344 } | |
| OLD | NEW |