blob: 499673572b4d296541768aafcd1c08e84a3815e8 [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
jvanverth34524262016-05-04 13:49:13 -07008#include "Viewer.h"
jvanverth9f372462016-04-06 06:08:59 -07009
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
jvanverth34524262016-05-04 13:49:13 -070019using namespace sk_app;
20
jvanverth9f372462016-04-06 06:08:59 -070021Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070022 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070023}
24
jvanverth9f372462016-04-06 06:08:59 -070025static void on_paint_handler(SkCanvas* canvas, void* userData) {
jvanverth34524262016-05-04 13:49:13 -070026 Viewer* vv = reinterpret_cast<Viewer*>(userData);
jvanverth9f372462016-04-06 06:08:59 -070027
28 return vv->onPaint(canvas);
29}
30
jvanverth814e38d2016-06-06 08:48:47 -070031static bool on_touch_handler(intptr_t owner, Window::InputState state, float x, float y, void* userData)
liyuqiand3cdbca2016-05-17 12:44:20 -070032{
33 Viewer* viewer = reinterpret_cast<Viewer*>(userData);
34
35 return viewer->onTouch(owner, state, x, y);
36}
37
liyuqiane5a6cd92016-05-27 08:52:52 -070038static void on_ui_state_changed_handler(const SkString& stateName, const SkString& stateValue, void* userData) {
39 Viewer* viewer = reinterpret_cast<Viewer*>(userData);
40
41 return viewer->onUIStateChanged(stateName, stateValue);
42}
43
44DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
egdanielf533f112016-06-13 11:30:10 -070045DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifying this builder.");
jvanverth2bb3b6d2016-04-08 07:24:09 -070046DEFINE_string2(match, m, nullptr,
47 "[~][^]substring[$] [...] of bench name to run.\n"
48 "Multiple matches may be separated by spaces.\n"
49 "~ causes a matching bench to always be skipped\n"
50 "^ requires the start of the bench to match\n"
51 "$ requires the end of the bench to match\n"
52 "^ and $ requires an exact match\n"
53 "If a bench does not match any list entry,\n"
54 "it is skipped unless some list entry starts with ~");
55DEFINE_string(skps, "skps", "Directory to read skps from.");
liyuqian71491dc2016-06-09 12:02:34 -070056#ifdef SK_BUILD_FOR_ANDROID
57DEFINE_bool(vulkan, false, "Run with Vulkan.");
58#else
jvanverth85f758c2016-05-27 06:47:08 -070059DEFINE_bool(vulkan, true, "Run with Vulkan.");
liyuqian71491dc2016-06-09 12:02:34 -070060#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -070061
jvanverthaf236b52016-05-20 06:01:06 -070062const char *kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
63 " [OpenGL]",
liyuqiand94ad582016-06-07 14:22:37 -070064 " [Vulkan]",
65 " [Raster]"
jvanverthaf236b52016-05-20 06:01:06 -070066};
67
liyuqiane5a6cd92016-05-27 08:52:52 -070068const char* kName = "name";
69const char* kValue = "value";
70const char* kOptions = "options";
71const char* kSlideStateName = "Slide";
72const char* kBackendStateName = "Backend";
liyuqianb73c24b2016-06-03 08:47:23 -070073const char* kSoftkeyStateName = "Softkey";
74const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -070075const char* kFpsStateName = "FPS";
liyuqiane5a6cd92016-05-27 08:52:52 -070076
jvanverth34524262016-05-04 13:49:13 -070077Viewer::Viewer(int argc, char** argv, void* platformData)
jvanverthc265a922016-04-08 12:51:45 -070078 : fCurrentMeasurement(0)
79 , fDisplayStats(false)
jvanverthaf236b52016-05-20 06:01:06 -070080 , fBackendType(sk_app::Window::kVulkan_BackendType)
egdaniel2a0bb0a2016-04-11 08:30:40 -070081 , fZoomCenterX(0.0f)
82 , fZoomCenterY(0.0f)
83 , fZoomLevel(0.0f)
84 , fZoomScale(SK_Scalar1)
jvanverthc265a922016-04-08 12:51:45 -070085{
jvanverth3d6ed3a2016-04-07 11:09:51 -070086 memset(fMeasurements, 0, sizeof(fMeasurements));
jvanverth9f372462016-04-06 06:08:59 -070087
jvanverth2bb3b6d2016-04-08 07:24:09 -070088 SkDebugf("Command line arguments: ");
89 for (int i = 1; i < argc; ++i) {
90 SkDebugf("%s ", argv[i]);
91 }
92 SkDebugf("\n");
93
94 SkCommandLineFlags::Parse(argc, argv);
95
jvanverth85f758c2016-05-27 06:47:08 -070096 fBackendType = FLAGS_vulkan ? sk_app::Window::kVulkan_BackendType
97 : sk_app::Window::kNativeGL_BackendType;
98
jvanverth9f372462016-04-06 06:08:59 -070099 fWindow = Window::CreateNativeWindow(platformData);
jvanverthaf236b52016-05-20 06:01:06 -0700100 fWindow->attach(fBackendType, DisplayParams());
jvanverth9f372462016-04-06 06:08:59 -0700101
102 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700103 fCommands.attach(fWindow);
jvanverth9f372462016-04-06 06:08:59 -0700104 fWindow->registerPaintFunc(on_paint_handler, this);
liyuqiand3cdbca2016-05-17 12:44:20 -0700105 fWindow->registerTouchFunc(on_touch_handler, this);
liyuqiane5a6cd92016-05-27 08:52:52 -0700106 fWindow->registerUIStateChangedFunc(on_ui_state_changed_handler, this);
jvanverth9f372462016-04-06 06:08:59 -0700107
brianosman622c8d52016-05-10 06:50:49 -0700108 // add key-bindings
109 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
110 this->fDisplayStats = !this->fDisplayStats;
111 fWindow->inval();
112 });
113 fCommands.addCommand('c', "Modes", "Toggle sRGB color mode", [this]() {
114 DisplayParams params = fWindow->getDisplayParams();
115 params.fProfileType = (kLinear_SkColorProfileType == params.fProfileType)
116 ? kSRGB_SkColorProfileType : kLinear_SkColorProfileType;
117 fWindow->setDisplayParams(params);
118 this->updateTitle();
119 fWindow->inval();
120 });
121 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
122 int previousSlide = fCurrentSlide;
123 fCurrentSlide++;
124 if (fCurrentSlide >= fSlides.count()) {
125 fCurrentSlide = 0;
126 }
127 this->setupCurrentSlide(previousSlide);
128 });
129 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
130 int previousSlide = fCurrentSlide;
131 fCurrentSlide--;
132 if (fCurrentSlide < 0) {
133 fCurrentSlide = fSlides.count() - 1;
134 }
135 this->setupCurrentSlide(previousSlide);
136 });
137 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
138 this->changeZoomLevel(1.f / 32.f);
139 fWindow->inval();
140 });
141 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
142 this->changeZoomLevel(-1.f / 32.f);
143 fWindow->inval();
144 });
jvanverth85f758c2016-05-27 06:47:08 -0700145#if 0 // this doesn't seem to work on any platform right now
jvanverthaf236b52016-05-20 06:01:06 -0700146#ifndef SK_BUILD_FOR_ANDROID
147 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
148 fWindow->detach();
149
150 if (sk_app::Window::kVulkan_BackendType == fBackendType) {
151 fBackendType = sk_app::Window::kNativeGL_BackendType;
152 }
jvanverth85f758c2016-05-27 06:47:08 -0700153 // TODO: get Vulkan -> OpenGL working on Windows without swapchain creation failure
jvanverthaf236b52016-05-20 06:01:06 -0700154 //else if (sk_app::Window::kNativeGL_BackendType == fBackendType) {
155 // fBackendType = sk_app::Window::kVulkan_BackendType;
156 //}
157
158 fWindow->attach(fBackendType, DisplayParams());
159 this->updateTitle();
jvanverth85f758c2016-05-27 06:47:08 -0700160 fWindow->inval();
jvanverthaf236b52016-05-20 06:01:06 -0700161 });
162#endif
jvanverth85f758c2016-05-27 06:47:08 -0700163#endif
brianosman622c8d52016-05-10 06:50:49 -0700164
jvanverth2bb3b6d2016-04-08 07:24:09 -0700165 // set up slides
166 this->initSlides();
167
djsollen12d62a72016-04-21 07:59:44 -0700168 fAnimTimer.run();
169
jvanverth2bb3b6d2016-04-08 07:24:09 -0700170 // set up first frame
jvanverth2bb3b6d2016-04-08 07:24:09 -0700171 fCurrentSlide = 0;
jvanverthc265a922016-04-08 12:51:45 -0700172 setupCurrentSlide(-1);
jvanverthc265a922016-04-08 12:51:45 -0700173
jvanverth9f372462016-04-06 06:08:59 -0700174 fWindow->show();
175}
176
jvanverth34524262016-05-04 13:49:13 -0700177void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700178 fAllSlideNames = Json::Value(Json::arrayValue);
179
jvanverth2bb3b6d2016-04-08 07:24:09 -0700180 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
181 while (gms) {
182 SkAutoTDelete<skiagm::GM> gm(gms->factory()(nullptr));
183
184 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
185 sk_sp<Slide> slide(new GMSlide(gm.release()));
186 fSlides.push_back(slide);
187 }
188
189 gms = gms->next();
190 }
191
192 // reverse array
193 for (int i = 0; i < fSlides.count()/2; ++i) {
194 sk_sp<Slide> temp = fSlides[i];
195 fSlides[i] = fSlides[fSlides.count() - i - 1];
196 fSlides[fSlides.count() - i - 1] = temp;
197 }
198
199 // SKPs
200 for (int i = 0; i < FLAGS_skps.count(); i++) {
201 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700202 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
203 continue;
204 }
205
jvanverth2bb3b6d2016-04-08 07:24:09 -0700206 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700207 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700208 if (slide) {
209 fSlides.push_back(slide);
210 }
211 } else {
212 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700213 SkString skpName;
214 while (it.next(&skpName)) {
215 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
216 continue;
217 }
218
219 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
220 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700221 if (slide) {
222 fSlides.push_back(slide);
223 }
224 }
225 }
226 }
227}
228
229
jvanverth34524262016-05-04 13:49:13 -0700230Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700231 fWindow->detach();
232 delete fWindow;
233}
234
brianosman05de2162016-05-06 13:28:57 -0700235void Viewer::updateTitle() {
jvanverth34524262016-05-04 13:49:13 -0700236 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700237 title.append(fSlides[fCurrentSlide]->getName());
brianosman05de2162016-05-06 13:28:57 -0700238 if (kSRGB_SkColorProfileType == fWindow->getDisplayParams().fProfileType) {
239 title.append(" sRGB");
240 }
jvanverthaf236b52016-05-20 06:01:06 -0700241 title.append(kBackendTypeStrings[fBackendType]);
brianosman05de2162016-05-06 13:28:57 -0700242 fWindow->setTitle(title.c_str());
243}
244
245void Viewer::setupCurrentSlide(int previousSlide) {
liyuqiane5a6cd92016-05-27 08:52:52 -0700246 if (fCurrentSlide == previousSlide) {
247 return; // no change; do nothing
248 }
249
liyuqiane46e4f02016-05-20 07:32:19 -0700250 fGesture.reset();
251 fDefaultMatrix.reset();
252 fDefaultMatrixInv.reset();
253
254 if (fWindow->supportsContentRect() && fWindow->scaleContentToFit()) {
255 const SkRect contentRect = fWindow->getContentRect();
256 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
257 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
258 if (contentRect.width() > 0 && contentRect.height() > 0) {
259 fDefaultMatrix.setRectToRect(slideBounds, contentRect, SkMatrix::kStart_ScaleToFit);
liyuqianbeb1c672016-05-20 11:41:01 -0700260 SkAssertResult(fDefaultMatrix.invert(&fDefaultMatrixInv));
liyuqiane46e4f02016-05-20 07:32:19 -0700261 }
262 }
263
264 if (fWindow->supportsContentRect()) {
265 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
266 SkRect windowRect = fWindow->getContentRect();
267 fDefaultMatrixInv.mapRect(&windowRect);
jvanverth1e305ba2016-06-01 09:39:15 -0700268 fGesture.setTransLimit(SkRect::MakeWH(SkIntToScalar(slideSize.width()),
269 SkIntToScalar(slideSize.height())),
270 windowRect);
liyuqiane46e4f02016-05-20 07:32:19 -0700271 }
272
brianosman05de2162016-05-06 13:28:57 -0700273 this->updateTitle();
liyuqiane5a6cd92016-05-27 08:52:52 -0700274 this->updateUIState();
egdanielf533f112016-06-13 11:30:10 -0700275 fSlides[fCurrentSlide]->load();
jvanverthc265a922016-04-08 12:51:45 -0700276 if (previousSlide >= 0) {
277 fSlides[previousSlide]->unload();
278 }
jvanverthc265a922016-04-08 12:51:45 -0700279 fWindow->inval();
280}
281
282#define MAX_ZOOM_LEVEL 8
283#define MIN_ZOOM_LEVEL -8
284
jvanverth34524262016-05-04 13:49:13 -0700285void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700286 fZoomLevel += delta;
287 if (fZoomLevel > 0) {
288 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
289 fZoomScale = fZoomLevel + SK_Scalar1;
290 } else if (fZoomLevel < 0) {
291 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
292 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
293 } else {
294 fZoomScale = SK_Scalar1;
295 }
jvanverthc265a922016-04-08 12:51:45 -0700296}
297
liyuqiand3cdbca2016-05-17 12:44:20 -0700298SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700299 SkMatrix m;
300 m.reset();
301
302 if (fZoomLevel) {
303 SkPoint center;
304 //m = this->getLocalMatrix();//.invert(&m);
305 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
306 SkScalar cx = center.fX;
307 SkScalar cy = center.fY;
308
309 m.setTranslate(-cx, -cy);
310 m.postScale(fZoomScale, fZoomScale);
311 m.postTranslate(cx, cy);
312 }
313
liyuqiand3cdbca2016-05-17 12:44:20 -0700314 m.preConcat(fGesture.localM());
315 m.preConcat(fGesture.globalM());
jvanverthc265a922016-04-08 12:51:45 -0700316
liyuqiand3cdbca2016-05-17 12:44:20 -0700317 return m;
jvanverthc265a922016-04-08 12:51:45 -0700318}
319
jvanverth34524262016-05-04 13:49:13 -0700320void Viewer::onPaint(SkCanvas* canvas) {
jvanverthc265a922016-04-08 12:51:45 -0700321 int count = canvas->save();
djsollen12d62a72016-04-21 07:59:44 -0700322
323 if (fWindow->supportsContentRect()) {
324 SkRect contentRect = fWindow->getContentRect();
325 canvas->clipRect(contentRect);
326 canvas->translate(contentRect.fLeft, contentRect.fTop);
327 }
328
329 canvas->clear(SK_ColorWHITE);
liyuqiane46e4f02016-05-20 07:32:19 -0700330 canvas->concat(fDefaultMatrix);
liyuqiand3cdbca2016-05-17 12:44:20 -0700331 canvas->concat(computeMatrix());
jvanverth3d6ed3a2016-04-07 11:09:51 -0700332
jvanverthc265a922016-04-08 12:51:45 -0700333 fSlides[fCurrentSlide]->draw(canvas);
334 canvas->restoreToCount(count);
335
336 if (fDisplayStats) {
337 drawStats(canvas);
338 }
brianosman622c8d52016-05-10 06:50:49 -0700339 fCommands.drawHelp(canvas);
jvanverth3d6ed3a2016-04-07 11:09:51 -0700340}
341
jvanverth814e38d2016-06-06 08:48:47 -0700342bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
liyuqiand3cdbca2016-05-17 12:44:20 -0700343 void* castedOwner = reinterpret_cast<void*>(owner);
liyuqiane46e4f02016-05-20 07:32:19 -0700344 SkPoint touchPoint = fDefaultMatrixInv.mapXY(x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700345 switch (state) {
346 case Window::kUp_InputState: {
347 fGesture.touchEnd(castedOwner);
348 break;
349 }
350 case Window::kDown_InputState: {
liyuqiane46e4f02016-05-20 07:32:19 -0700351 fGesture.touchBegin(castedOwner, touchPoint.fX, touchPoint.fY);
liyuqiand3cdbca2016-05-17 12:44:20 -0700352 break;
353 }
354 case Window::kMove_InputState: {
liyuqiane46e4f02016-05-20 07:32:19 -0700355 fGesture.touchMoved(castedOwner, touchPoint.fX, touchPoint.fY);
liyuqiand3cdbca2016-05-17 12:44:20 -0700356 break;
357 }
358 }
359 fWindow->inval();
360 return true;
361}
362
jvanverth34524262016-05-04 13:49:13 -0700363void Viewer::drawStats(SkCanvas* canvas) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700364 static const float kPixelPerMS = 2.0f;
365 static const int kDisplayWidth = 130;
366 static const int kDisplayHeight = 100;
367 static const int kDisplayPadding = 10;
368 static const int kGraphPadding = 3;
369 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
370
371 SkISize canvasSize = canvas->getDeviceSize();
372 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
373 SkIntToScalar(kDisplayPadding),
374 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
375 SkPaint paint;
376 canvas->save();
377
djsollen12d62a72016-04-21 07:59:44 -0700378 if (fWindow->supportsContentRect()) {
379 SkRect contentRect = fWindow->getContentRect();
380 canvas->clipRect(contentRect);
381 canvas->translate(contentRect.fLeft, contentRect.fTop);
382 }
383
jvanverth3d6ed3a2016-04-07 11:09:51 -0700384 canvas->clipRect(rect);
385 paint.setColor(SK_ColorBLACK);
386 canvas->drawRect(rect, paint);
387 // draw the 16ms line
388 paint.setColor(SK_ColorLTGRAY);
389 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
390 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
391 paint.setColor(SK_ColorRED);
392 paint.setStyle(SkPaint::kStroke_Style);
393 canvas->drawRect(rect, paint);
394
395 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
396 const int xStep = 2;
397 const int startY = SkScalarTruncToInt(rect.fBottom);
398 int i = fCurrentMeasurement;
399 do {
400 int endY = startY - (int)(fMeasurements[i] * kPixelPerMS + 0.5); // round to nearest value
401 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
402 SkIntToScalar(x), SkIntToScalar(endY), paint);
403 i++;
404 i &= (kMeasurementCount - 1); // fast mod
405 x += xStep;
406 } while (i != fCurrentMeasurement);
jvanverth9f372462016-04-06 06:08:59 -0700407
408 canvas->restore();
409}
410
jvanverth34524262016-05-04 13:49:13 -0700411void Viewer::onIdle(double ms) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700412 // Record measurements
413 fMeasurements[fCurrentMeasurement++] = ms;
414 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod
415 SkASSERT(fCurrentMeasurement < kMeasurementCount);
416
jvanverthc265a922016-04-08 12:51:45 -0700417 fAnimTimer.updateTime();
jvanverth9d5e47f2016-04-26 08:01:33 -0700418 if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) {
jvanverthc265a922016-04-08 12:51:45 -0700419 fWindow->inval();
liyuqian1f508fd2016-06-07 06:57:40 -0700420 updateUIState(); // Update the FPS
jvanverthc265a922016-04-08 12:51:45 -0700421 }
jvanverth9f372462016-04-06 06:08:59 -0700422}
liyuqiane5a6cd92016-05-27 08:52:52 -0700423
424void Viewer::updateUIState() {
liyuqianb73c24b2016-06-03 08:47:23 -0700425 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -0700426 Json::Value slideState(Json::objectValue);
427 slideState[kName] = kSlideStateName;
428 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -0700429 if (fAllSlideNames.size() == 0) {
430 for(auto slide : fSlides) {
431 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
432 }
liyuqiane5a6cd92016-05-27 08:52:52 -0700433 }
liyuqian1f508fd2016-06-07 06:57:40 -0700434 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -0700435
liyuqianb73c24b2016-06-03 08:47:23 -0700436 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -0700437 Json::Value backendState(Json::objectValue);
438 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -0700439 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -0700440 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -0700441 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -0700442 backendState[kOptions].append(Json::Value(str));
443 }
liyuqiane5a6cd92016-05-27 08:52:52 -0700444
liyuqianb73c24b2016-06-03 08:47:23 -0700445 // Softkey state
446 Json::Value softkeyState(Json::objectValue);
447 softkeyState[kName] = kSoftkeyStateName;
448 softkeyState[kValue] = kSoftkeyHint;
449 softkeyState[kOptions] = Json::Value(Json::arrayValue);
450 softkeyState[kOptions].append(kSoftkeyHint);
451 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
452 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
453 }
454
liyuqian1f508fd2016-06-07 06:57:40 -0700455 // FPS state
456 Json::Value fpsState(Json::objectValue);
457 fpsState[kName] = kFpsStateName;
458 double measurement = fMeasurements[
459 (fCurrentMeasurement + (kMeasurementCount-1)) % kMeasurementCount
460 ];
461 fpsState[kValue] = SkStringPrintf("%8.3lf ms", measurement).c_str();
462 fpsState[kOptions] = Json::Value(Json::arrayValue);
463
liyuqiane5a6cd92016-05-27 08:52:52 -0700464 Json::Value state(Json::arrayValue);
465 state.append(slideState);
466 state.append(backendState);
liyuqianb73c24b2016-06-03 08:47:23 -0700467 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -0700468 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -0700469
470 fWindow->setUIState(state);
471}
472
473void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -0700474 // For those who will add more features to handle the state change in this function:
475 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
476 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
477 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -0700478 if (stateName.equals(kSlideStateName)) {
479 int previousSlide = fCurrentSlide;
480 fCurrentSlide = 0;
481 for(auto slide : fSlides) {
482 if (slide->getName().equals(stateValue)) {
483 setupCurrentSlide(previousSlide);
484 break;
485 }
486 fCurrentSlide++;
487 }
488 if (fCurrentSlide >= fSlides.count()) {
489 fCurrentSlide = previousSlide;
490 SkDebugf("Slide not found: %s", stateValue.c_str());
491 }
liyuqian6cb70252016-06-02 12:16:25 -0700492 } else if (stateName.equals(kBackendStateName)) {
493 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
494 if (stateValue.equals(kBackendTypeStrings[i])) {
495 if (fBackendType != i) {
496 fBackendType = (sk_app::Window::BackendType)i;
497 fWindow->detach();
498 fWindow->attach(fBackendType, DisplayParams());
499 fWindow->inval();
500 updateTitle();
501 updateUIState();
502 }
503 break;
504 }
505 }
liyuqianb73c24b2016-06-03 08:47:23 -0700506 } else if (stateName.equals(kSoftkeyStateName)) {
507 if (!stateValue.equals(kSoftkeyHint)) {
508 fCommands.onSoftkey(stateValue);
509 updateUIState(); // This is still needed to reset the value to kSoftkeyHint
510 }
liyuqiane5a6cd92016-05-27 08:52:52 -0700511 } else {
512 SkDebugf("Unknown stateName: %s", stateName.c_str());
513 }
514}