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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 return vv->onPaint(canvas); | 28 return vv->onPaint(canvas); |
29 } | 29 } |
30 | 30 |
31 static bool on_touch_handler(int owner, Window::InputState state, float x, float
y, void* userData) | 31 static bool on_touch_handler(int owner, Window::InputState state, float x, float
y, void* userData) |
32 { | 32 { |
33 Viewer* viewer = reinterpret_cast<Viewer*>(userData); | 33 Viewer* viewer = reinterpret_cast<Viewer*>(userData); |
34 | 34 |
35 return viewer->onTouch(owner, state, x, y); | 35 return viewer->onTouch(owner, state, x, y); |
36 } | 36 } |
37 | 37 |
| 38 static void on_set_handler(const SkString& stateName, const SkString& stateValue
, void* userData) { |
| 39 Viewer* viewer = reinterpret_cast<Viewer*>(userData); |
| 40 |
| 41 return viewer->onSet(stateName, stateValue); |
| 42 } |
| 43 |
38 DEFINE_bool2(fullscreen, f, true, "Run fullscreen."); | 44 DEFINE_bool2(fullscreen, f, true, "Run fullscreen."); |
39 DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifyi
ng this builder."); | 45 DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifyi
ng this builder."); |
40 DEFINE_string2(match, m, nullptr, | 46 DEFINE_string2(match, m, nullptr, |
41 "[~][^]substring[$] [...] of bench name to run.\n" | 47 "[~][^]substring[$] [...] of bench name to run.\n" |
42 "Multiple matches may be separated by spaces.\n" | 48 "Multiple matches may be separated by spaces.\n" |
43 "~ causes a matching bench to always be skipped\n" | 49 "~ causes a matching bench to always be skipped\n" |
44 "^ requires the start of the bench to match\n" | 50 "^ requires the start of the bench to match\n" |
45 "$ requires the end of the bench to match\n" | 51 "$ requires the end of the bench to match\n" |
46 "^ and $ requires an exact match\n" | 52 "^ and $ requires an exact match\n" |
47 "If a bench does not match any list entry,\n" | 53 "If a bench does not match any list entry,\n" |
48 "it is skipped unless some list entry starts with ~"); | 54 "it is skipped unless some list entry starts with ~"); |
49 DEFINE_string(skps, "skps", "Directory to read skps from."); | 55 DEFINE_string(skps, "skps", "Directory to read skps from."); |
50 | 56 |
51 const char *kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = { | 57 const char *kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = { |
52 " [OpenGL]", | 58 " [OpenGL]", |
53 " [Vulkan]" | 59 " [Vulkan]" |
54 }; | 60 }; |
55 | 61 |
| 62 const char* kName = "name"; |
| 63 const char* kValue = "value"; |
| 64 const char* kOptions = "options"; |
| 65 const char* kSlideStateName = "Slide"; |
| 66 const char* kBackendStateName = "Backend"; |
| 67 |
56 Viewer::Viewer(int argc, char** argv, void* platformData) | 68 Viewer::Viewer(int argc, char** argv, void* platformData) |
57 : fCurrentMeasurement(0) | 69 : fCurrentMeasurement(0) |
58 , fDisplayStats(false) | 70 , fDisplayStats(false) |
59 , fBackendType(sk_app::Window::kVulkan_BackendType) | 71 , fBackendType(sk_app::Window::kVulkan_BackendType) |
60 , fZoomCenterX(0.0f) | 72 , fZoomCenterX(0.0f) |
61 , fZoomCenterY(0.0f) | 73 , fZoomCenterY(0.0f) |
62 , fZoomLevel(0.0f) | 74 , fZoomLevel(0.0f) |
63 , fZoomScale(SK_Scalar1) | 75 , fZoomScale(SK_Scalar1) |
64 { | 76 { |
65 memset(fMeasurements, 0, sizeof(fMeasurements)); | 77 memset(fMeasurements, 0, sizeof(fMeasurements)); |
66 | 78 |
67 SkDebugf("Command line arguments: "); | 79 SkDebugf("Command line arguments: "); |
68 for (int i = 1; i < argc; ++i) { | 80 for (int i = 1; i < argc; ++i) { |
69 SkDebugf("%s ", argv[i]); | 81 SkDebugf("%s ", argv[i]); |
70 } | 82 } |
71 SkDebugf("\n"); | 83 SkDebugf("\n"); |
72 | 84 |
73 SkCommandLineFlags::Parse(argc, argv); | 85 SkCommandLineFlags::Parse(argc, argv); |
74 | 86 |
75 fWindow = Window::CreateNativeWindow(platformData); | 87 fWindow = Window::CreateNativeWindow(platformData); |
76 fWindow->attach(fBackendType, DisplayParams()); | 88 fWindow->attach(fBackendType, DisplayParams()); |
77 | 89 |
78 // register callbacks | 90 // register callbacks |
79 fCommands.attach(fWindow); | 91 fCommands.attach(fWindow); |
80 fWindow->registerPaintFunc(on_paint_handler, this); | 92 fWindow->registerPaintFunc(on_paint_handler, this); |
81 fWindow->registerTouchFunc(on_touch_handler, this); | 93 fWindow->registerTouchFunc(on_touch_handler, this); |
| 94 fWindow->registerSetFunc(on_set_handler, this); |
82 | 95 |
83 // add key-bindings | 96 // add key-bindings |
84 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() { | 97 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() { |
85 this->fDisplayStats = !this->fDisplayStats; | 98 this->fDisplayStats = !this->fDisplayStats; |
86 fWindow->inval(); | 99 fWindow->inval(); |
87 }); | 100 }); |
88 fCommands.addCommand('c', "Modes", "Toggle sRGB color mode", [this]() { | 101 fCommands.addCommand('c', "Modes", "Toggle sRGB color mode", [this]() { |
89 DisplayParams params = fWindow->getDisplayParams(); | 102 DisplayParams params = fWindow->getDisplayParams(); |
90 params.fProfileType = (kLinear_SkColorProfileType == params.fProfileType
) | 103 params.fProfileType = (kLinear_SkColorProfileType == params.fProfileType
) |
91 ? kSRGB_SkColorProfileType : kLinear_SkColorProfileType; | 104 ? kSRGB_SkColorProfileType : kLinear_SkColorProfileType; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 SkString title("Viewer: "); | 219 SkString title("Viewer: "); |
207 title.append(fSlides[fCurrentSlide]->getName()); | 220 title.append(fSlides[fCurrentSlide]->getName()); |
208 if (kSRGB_SkColorProfileType == fWindow->getDisplayParams().fProfileType) { | 221 if (kSRGB_SkColorProfileType == fWindow->getDisplayParams().fProfileType) { |
209 title.append(" sRGB"); | 222 title.append(" sRGB"); |
210 } | 223 } |
211 title.append(kBackendTypeStrings[fBackendType]); | 224 title.append(kBackendTypeStrings[fBackendType]); |
212 fWindow->setTitle(title.c_str()); | 225 fWindow->setTitle(title.c_str()); |
213 } | 226 } |
214 | 227 |
215 void Viewer::setupCurrentSlide(int previousSlide) { | 228 void Viewer::setupCurrentSlide(int previousSlide) { |
| 229 if (fCurrentSlide == previousSlide) { |
| 230 return; // no change; do nothing |
| 231 } |
| 232 |
216 fGesture.reset(); | 233 fGesture.reset(); |
217 fDefaultMatrix.reset(); | 234 fDefaultMatrix.reset(); |
218 fDefaultMatrixInv.reset(); | 235 fDefaultMatrixInv.reset(); |
219 | 236 |
220 if (fWindow->supportsContentRect() && fWindow->scaleContentToFit()) { | 237 if (fWindow->supportsContentRect() && fWindow->scaleContentToFit()) { |
221 const SkRect contentRect = fWindow->getContentRect(); | 238 const SkRect contentRect = fWindow->getContentRect(); |
222 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); | 239 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); |
223 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.
height()); | 240 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.
height()); |
224 if (contentRect.width() > 0 && contentRect.height() > 0) { | 241 if (contentRect.width() > 0 && contentRect.height() > 0) { |
225 fDefaultMatrix.setRectToRect(slideBounds, contentRect, SkMatrix::kSt
art_ScaleToFit); | 242 fDefaultMatrix.setRectToRect(slideBounds, contentRect, SkMatrix::kSt
art_ScaleToFit); |
226 SkAssertResult(fDefaultMatrix.invert(&fDefaultMatrixInv)); | 243 SkAssertResult(fDefaultMatrix.invert(&fDefaultMatrixInv)); |
227 } | 244 } |
228 } | 245 } |
229 | 246 |
230 if (fWindow->supportsContentRect()) { | 247 if (fWindow->supportsContentRect()) { |
231 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); | 248 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); |
232 SkRect windowRect = fWindow->getContentRect(); | 249 SkRect windowRect = fWindow->getContentRect(); |
233 fDefaultMatrixInv.mapRect(&windowRect); | 250 fDefaultMatrixInv.mapRect(&windowRect); |
234 fGesture.setTransLimit(SkRect::MakeWH(slideSize.width(), slideSize.heigh
t()), windowRect); | 251 fGesture.setTransLimit(SkRect::MakeWH(slideSize.width(), slideSize.heigh
t()), windowRect); |
235 } | 252 } |
236 | 253 |
237 this->updateTitle(); | 254 this->updateTitle(); |
| 255 this->fWindow->onStateUpdated(getStateJson().toStyledString().c_str()); |
238 fSlides[fCurrentSlide]->load(); | 256 fSlides[fCurrentSlide]->load(); |
239 if (previousSlide >= 0) { | 257 if (previousSlide >= 0) { |
240 fSlides[previousSlide]->unload(); | 258 fSlides[previousSlide]->unload(); |
241 } | 259 } |
242 fWindow->inval(); | 260 fWindow->inval(); |
243 } | 261 } |
244 | 262 |
245 #define MAX_ZOOM_LEVEL 8 | 263 #define MAX_ZOOM_LEVEL 8 |
246 #define MIN_ZOOM_LEVEL -8 | 264 #define MIN_ZOOM_LEVEL -8 |
247 | 265 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 // Record measurements | 393 // Record measurements |
376 fMeasurements[fCurrentMeasurement++] = ms; | 394 fMeasurements[fCurrentMeasurement++] = ms; |
377 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod | 395 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod |
378 SkASSERT(fCurrentMeasurement < kMeasurementCount); | 396 SkASSERT(fCurrentMeasurement < kMeasurementCount); |
379 | 397 |
380 fAnimTimer.updateTime(); | 398 fAnimTimer.updateTime(); |
381 if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) { | 399 if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) { |
382 fWindow->inval(); | 400 fWindow->inval(); |
383 } | 401 } |
384 } | 402 } |
| 403 |
| 404 Json::Value Viewer::getStateJson() const { |
| 405 Json::Value slideState(Json::objectValue); |
| 406 slideState[kName] = kSlideStateName; |
| 407 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str(); |
| 408 Json::Value allSlideNames(Json::arrayValue); |
| 409 for(auto slide : fSlides) { |
| 410 allSlideNames.append(Json::Value(slide->getName().c_str())); |
| 411 } |
| 412 slideState[kOptions] = allSlideNames; |
| 413 |
| 414 // This state is currently a demo for the one without options. |
| 415 // We will be able to change the backend too. |
| 416 Json::Value backendState(Json::objectValue); |
| 417 backendState[kName] = kBackendStateName; |
| 418 backendState[kValue] = fBackendType == sk_app::Window::kVulkan_BackendType ? |
| 419 "Vulkan" : "Other than Vulkan"; |
| 420 backendState[kOptions] = Json::Value(Json::arrayValue); |
| 421 |
| 422 Json::Value state(Json::arrayValue); |
| 423 state.append(slideState); |
| 424 state.append(backendState); |
| 425 return state; |
| 426 } |
| 427 |
| 428 void Viewer::onSet(const SkString& stateName, const SkString& stateValue) { |
| 429 // Currently, we only recognize the Slide state |
| 430 if (stateName.equals(kSlideStateName)) { |
| 431 int previousSlide = fCurrentSlide; |
| 432 fCurrentSlide = 0; |
| 433 for(auto slide : fSlides) { |
| 434 if (slide->getName().equals(stateValue)) { |
| 435 setupCurrentSlide(previousSlide); |
| 436 break; |
| 437 } |
| 438 fCurrentSlide++; |
| 439 } |
| 440 if (fCurrentSlide >= fSlides.count()) { |
| 441 fCurrentSlide = previousSlide; |
| 442 SkDebugf("Slide not found: %s", stateValue.c_str()); |
| 443 } |
| 444 } else { |
| 445 SK_ABORT(SkString("Unknown stateName: ") + stateName); |
| 446 } |
| 447 } |
OLD | NEW |