| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Viewer.h" | 8 #include "Viewer.h" |
| 9 | 9 |
| 10 #include "GMSlide.h" | 10 #include "GMSlide.h" |
| 11 #include "SKPSlide.h" | 11 #include "SKPSlide.h" |
| 12 | 12 |
| 13 #include "SkCanvas.h" | 13 #include "SkCanvas.h" |
| 14 #include "SkCommonFlags.h" | 14 #include "SkCommonFlags.h" |
| 15 #include "SkOSFile.h" | 15 #include "SkOSFile.h" |
| 16 #include "SkRandom.h" | 16 #include "SkRandom.h" |
| 17 #include "SkStream.h" | 17 #include "SkStream.h" |
| 18 | 18 |
| 19 using namespace sk_app; | 19 using namespace sk_app; |
| 20 | 20 |
| 21 Application* Application::Create(int argc, char** argv, void* platformData) { | 21 Application* Application::Create(int argc, char** argv, void* platformData) { |
| 22 return new Viewer(argc, argv, platformData); | 22 return new Viewer(argc, argv, platformData); |
| 23 } | 23 } |
| 24 | 24 |
| 25 static bool on_key_handler(Window::Key key, Window::InputState state, uint32_t m
odifiers, | |
| 26 void* userData) { | |
| 27 Viewer* vv = reinterpret_cast<Viewer*>(userData); | |
| 28 | |
| 29 return vv->onKey(key, state, modifiers); | |
| 30 } | |
| 31 | |
| 32 static bool on_char_handler(SkUnichar c, uint32_t modifiers, void* userData) { | |
| 33 Viewer* vv = reinterpret_cast<Viewer*>(userData); | |
| 34 | |
| 35 return vv->onChar(c, modifiers); | |
| 36 } | |
| 37 | |
| 38 static void on_paint_handler(SkCanvas* canvas, void* userData) { | 25 static void on_paint_handler(SkCanvas* canvas, void* userData) { |
| 39 Viewer* vv = reinterpret_cast<Viewer*>(userData); | 26 Viewer* vv = reinterpret_cast<Viewer*>(userData); |
| 40 | 27 |
| 41 return vv->onPaint(canvas); | 28 return vv->onPaint(canvas); |
| 42 } | 29 } |
| 43 | 30 |
| 44 DEFINE_bool2(fullscreen, f, true, "Run fullscreen."); | 31 DEFINE_bool2(fullscreen, f, true, "Run fullscreen."); |
| 45 DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifyi
ng this builder."); | 32 DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifyi
ng this builder."); |
| 46 DEFINE_string2(match, m, nullptr, | 33 DEFINE_string2(match, m, nullptr, |
| 47 "[~][^]substring[$] [...] of bench name to run.\n" | 34 "[~][^]substring[$] [...] of bench name to run.\n" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 69 SkDebugf("%s ", argv[i]); | 56 SkDebugf("%s ", argv[i]); |
| 70 } | 57 } |
| 71 SkDebugf("\n"); | 58 SkDebugf("\n"); |
| 72 | 59 |
| 73 SkCommandLineFlags::Parse(argc, argv); | 60 SkCommandLineFlags::Parse(argc, argv); |
| 74 | 61 |
| 75 fWindow = Window::CreateNativeWindow(platformData); | 62 fWindow = Window::CreateNativeWindow(platformData); |
| 76 fWindow->attach(Window::kVulkan_BackendType, DisplayParams()); | 63 fWindow->attach(Window::kVulkan_BackendType, DisplayParams()); |
| 77 | 64 |
| 78 // register callbacks | 65 // register callbacks |
| 79 fWindow->registerKeyFunc(on_key_handler, this); | 66 fCommands.attach(fWindow); |
| 80 fWindow->registerCharFunc(on_char_handler, this); | |
| 81 fWindow->registerPaintFunc(on_paint_handler, this); | 67 fWindow->registerPaintFunc(on_paint_handler, this); |
| 82 | 68 |
| 69 // add key-bindings |
| 70 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() { |
| 71 this->fDisplayStats = !this->fDisplayStats; |
| 72 fWindow->inval(); |
| 73 }); |
| 74 fCommands.addCommand('c', "Modes", "Toggle sRGB color mode", [this]() { |
| 75 DisplayParams params = fWindow->getDisplayParams(); |
| 76 params.fProfileType = (kLinear_SkColorProfileType == params.fProfileType
) |
| 77 ? kSRGB_SkColorProfileType : kLinear_SkColorProfileType; |
| 78 fWindow->setDisplayParams(params); |
| 79 this->updateTitle(); |
| 80 fWindow->inval(); |
| 81 }); |
| 82 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide
", [this]() { |
| 83 int previousSlide = fCurrentSlide; |
| 84 fCurrentSlide++; |
| 85 if (fCurrentSlide >= fSlides.count()) { |
| 86 fCurrentSlide = 0; |
| 87 } |
| 88 this->setupCurrentSlide(previousSlide); |
| 89 }); |
| 90 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous sli
de", [this]() { |
| 91 int previousSlide = fCurrentSlide; |
| 92 fCurrentSlide--; |
| 93 if (fCurrentSlide < 0) { |
| 94 fCurrentSlide = fSlides.count() - 1; |
| 95 } |
| 96 this->setupCurrentSlide(previousSlide); |
| 97 }); |
| 98 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this](
) { |
| 99 this->changeZoomLevel(1.f / 32.f); |
| 100 fWindow->inval(); |
| 101 }); |
| 102 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [t
his]() { |
| 103 this->changeZoomLevel(-1.f / 32.f); |
| 104 fWindow->inval(); |
| 105 }); |
| 106 |
| 83 // set up slides | 107 // set up slides |
| 84 this->initSlides(); | 108 this->initSlides(); |
| 85 | 109 |
| 86 fAnimTimer.run(); | 110 fAnimTimer.run(); |
| 87 | 111 |
| 88 // set up first frame | 112 // set up first frame |
| 89 fCurrentSlide = 0; | 113 fCurrentSlide = 0; |
| 90 setupCurrentSlide(-1); | 114 setupCurrentSlide(-1); |
| 91 updateMatrix(); | 115 updateMatrix(); |
| 92 | 116 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 } | 225 } |
| 202 | 226 |
| 203 // TODO: add gesture support | 227 // TODO: add gesture support |
| 204 // Apply any gesture matrix | 228 // Apply any gesture matrix |
| 205 //m.preConcat(fGesture.localM()); | 229 //m.preConcat(fGesture.localM()); |
| 206 //m.preConcat(fGesture.globalM()); | 230 //m.preConcat(fGesture.globalM()); |
| 207 | 231 |
| 208 fLocalMatrix = m; | 232 fLocalMatrix = m; |
| 209 } | 233 } |
| 210 | 234 |
| 211 bool Viewer::onKey(Window::Key key, Window::InputState state, uint32_t modifiers
) { | |
| 212 if (Window::kDown_InputState == state) { | |
| 213 switch (key) { | |
| 214 case Window::kRight_Key: { | |
| 215 int previousSlide = fCurrentSlide; | |
| 216 fCurrentSlide++; | |
| 217 if (fCurrentSlide >= fSlides.count()) { | |
| 218 fCurrentSlide = 0; | |
| 219 } | |
| 220 setupCurrentSlide(previousSlide); | |
| 221 return true; | |
| 222 } | |
| 223 | |
| 224 case Window::kLeft_Key: { | |
| 225 int previousSlide = fCurrentSlide; | |
| 226 fCurrentSlide--; | |
| 227 if (fCurrentSlide < 0) { | |
| 228 fCurrentSlide = fSlides.count() - 1; | |
| 229 } | |
| 230 setupCurrentSlide(previousSlide); | |
| 231 return true; | |
| 232 } | |
| 233 | |
| 234 case Window::kUp_Key: { | |
| 235 this->changeZoomLevel(1.f / 32.f); | |
| 236 fWindow->inval(); | |
| 237 return true; | |
| 238 } | |
| 239 | |
| 240 case Window::kDown_Key: { | |
| 241 this->changeZoomLevel(-1.f / 32.f); | |
| 242 fWindow->inval(); | |
| 243 return true; | |
| 244 } | |
| 245 | |
| 246 default: | |
| 247 break; | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 return false; | |
| 252 } | |
| 253 | |
| 254 bool Viewer::onChar(SkUnichar c, uint32_t modifiers) { | |
| 255 switch (c) { | |
| 256 case 's': | |
| 257 fDisplayStats = !fDisplayStats; | |
| 258 return true; | |
| 259 case 'c': | |
| 260 DisplayParams params = fWindow->getDisplayParams(); | |
| 261 params.fProfileType = (kLinear_SkColorProfileType == params.fProfile
Type) | |
| 262 ? kSRGB_SkColorProfileType : kLinear_SkColorProfileType; | |
| 263 fWindow->setDisplayParams(params); | |
| 264 this->updateTitle(); | |
| 265 fWindow->inval(); | |
| 266 return true; | |
| 267 } | |
| 268 | |
| 269 return false; | |
| 270 } | |
| 271 | |
| 272 void Viewer::onPaint(SkCanvas* canvas) { | 235 void Viewer::onPaint(SkCanvas* canvas) { |
| 273 | 236 |
| 274 int count = canvas->save(); | 237 int count = canvas->save(); |
| 275 | 238 |
| 276 if (fWindow->supportsContentRect()) { | 239 if (fWindow->supportsContentRect()) { |
| 277 SkRect contentRect = fWindow->getContentRect(); | 240 SkRect contentRect = fWindow->getContentRect(); |
| 278 canvas->clipRect(contentRect); | 241 canvas->clipRect(contentRect); |
| 279 canvas->translate(contentRect.fLeft, contentRect.fTop); | 242 canvas->translate(contentRect.fLeft, contentRect.fTop); |
| 280 } | 243 } |
| 281 | 244 |
| 282 canvas->clear(SK_ColorWHITE); | 245 canvas->clear(SK_ColorWHITE); |
| 283 if (fWindow->supportsContentRect() && fWindow->scaleContentToFit()) { | 246 if (fWindow->supportsContentRect() && fWindow->scaleContentToFit()) { |
| 284 const SkRect contentRect = fWindow->getContentRect(); | 247 const SkRect contentRect = fWindow->getContentRect(); |
| 285 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); | 248 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); |
| 286 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.
height()); | 249 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.
height()); |
| 287 SkMatrix matrix; | 250 SkMatrix matrix; |
| 288 matrix.setRectToRect(slideBounds, contentRect, SkMatrix::kCenter_ScaleTo
Fit); | 251 matrix.setRectToRect(slideBounds, contentRect, SkMatrix::kCenter_ScaleTo
Fit); |
| 289 canvas->concat(matrix); | 252 canvas->concat(matrix); |
| 290 } | 253 } |
| 291 canvas->concat(fLocalMatrix); | 254 canvas->concat(fLocalMatrix); |
| 292 | 255 |
| 293 fSlides[fCurrentSlide]->draw(canvas); | 256 fSlides[fCurrentSlide]->draw(canvas); |
| 294 canvas->restoreToCount(count); | 257 canvas->restoreToCount(count); |
| 295 | 258 |
| 296 if (fDisplayStats) { | 259 if (fDisplayStats) { |
| 297 drawStats(canvas); | 260 drawStats(canvas); |
| 298 } | 261 } |
| 262 fCommands.drawHelp(canvas); |
| 299 } | 263 } |
| 300 | 264 |
| 301 void Viewer::drawStats(SkCanvas* canvas) { | 265 void Viewer::drawStats(SkCanvas* canvas) { |
| 302 static const float kPixelPerMS = 2.0f; | 266 static const float kPixelPerMS = 2.0f; |
| 303 static const int kDisplayWidth = 130; | 267 static const int kDisplayWidth = 130; |
| 304 static const int kDisplayHeight = 100; | 268 static const int kDisplayHeight = 100; |
| 305 static const int kDisplayPadding = 10; | 269 static const int kDisplayPadding = 10; |
| 306 static const int kGraphPadding = 3; | 270 static const int kGraphPadding = 3; |
| 307 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps | 271 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps |
| 308 | 272 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 // Record measurements | 314 // Record measurements |
| 351 fMeasurements[fCurrentMeasurement++] = ms; | 315 fMeasurements[fCurrentMeasurement++] = ms; |
| 352 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod | 316 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod |
| 353 SkASSERT(fCurrentMeasurement < kMeasurementCount); | 317 SkASSERT(fCurrentMeasurement < kMeasurementCount); |
| 354 | 318 |
| 355 fAnimTimer.updateTime(); | 319 fAnimTimer.updateTime(); |
| 356 if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) { | 320 if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) { |
| 357 fWindow->inval(); | 321 fWindow->inval(); |
| 358 } | 322 } |
| 359 } | 323 } |
| OLD | NEW |