blob: 43697f88b8cc297fca7d9ef33fb26c0540b70ba7 [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"
liyuqian6f163d22016-06-13 12:26:45 -070011#include "ImageSlide.h"
jvanverthc7027ab2016-06-16 09:52:35 -070012#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070013#include "SKPSlide.h"
jvanverth9f372462016-04-06 06:08:59 -070014
jvanverth2bb3b6d2016-04-08 07:24:09 -070015#include "SkCanvas.h"
16#include "SkCommonFlags.h"
liyuqian6f163d22016-06-13 12:26:45 -070017#include "SkMetaData.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070018#include "SkOSFile.h"
19#include "SkRandom.h"
20#include "SkStream.h"
jvanverth9f372462016-04-06 06:08:59 -070021
jvanverth34524262016-05-04 13:49:13 -070022using namespace sk_app;
23
jvanverth9f372462016-04-06 06:08:59 -070024Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070025 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070026}
27
jvanverth9f372462016-04-06 06:08:59 -070028static void on_paint_handler(SkCanvas* canvas, void* userData) {
jvanverth34524262016-05-04 13:49:13 -070029 Viewer* vv = reinterpret_cast<Viewer*>(userData);
jvanverth9f372462016-04-06 06:08:59 -070030
31 return vv->onPaint(canvas);
32}
33
jvanverth814e38d2016-06-06 08:48:47 -070034static bool on_touch_handler(intptr_t owner, Window::InputState state, float x, float y, void* userData)
liyuqiand3cdbca2016-05-17 12:44:20 -070035{
36 Viewer* viewer = reinterpret_cast<Viewer*>(userData);
37
38 return viewer->onTouch(owner, state, x, y);
39}
40
liyuqiane5a6cd92016-05-27 08:52:52 -070041static void on_ui_state_changed_handler(const SkString& stateName, const SkString& stateValue, void* userData) {
42 Viewer* viewer = reinterpret_cast<Viewer*>(userData);
43
44 return viewer->onUIStateChanged(stateName, stateValue);
45}
46
47DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
egdanielf533f112016-06-13 11:30:10 -070048DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifying this builder.");
jvanverth2bb3b6d2016-04-08 07:24:09 -070049DEFINE_string2(match, m, nullptr,
50 "[~][^]substring[$] [...] of bench name to run.\n"
51 "Multiple matches may be separated by spaces.\n"
52 "~ causes a matching bench to always be skipped\n"
53 "^ requires the start of the bench to match\n"
54 "$ requires the end of the bench to match\n"
55 "^ and $ requires an exact match\n"
56 "If a bench does not match any list entry,\n"
57 "it is skipped unless some list entry starts with ~");
liyuqian71491dc2016-06-09 12:02:34 -070058#ifdef SK_BUILD_FOR_ANDROID
liyuqian6f163d22016-06-13 12:26:45 -070059DEFINE_string(skps, "/data/local/tmp/skia", "Directory to read skps from.");
60DEFINE_string(jpgs, "/data/local/tmp/skia", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070061DEFINE_bool(vulkan, false, "Run with Vulkan.");
62#else
liyuqian6f163d22016-06-13 12:26:45 -070063DEFINE_string(skps, "skps", "Directory to read skps from.");
64DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
jvanverth85f758c2016-05-27 06:47:08 -070065DEFINE_bool(vulkan, true, "Run with Vulkan.");
liyuqian71491dc2016-06-09 12:02:34 -070066#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -070067
jvanverthaf236b52016-05-20 06:01:06 -070068const char *kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
69 " [OpenGL]",
liyuqiand94ad582016-06-07 14:22:37 -070070 " [Vulkan]",
71 " [Raster]"
jvanverthaf236b52016-05-20 06:01:06 -070072};
73
liyuqiane5a6cd92016-05-27 08:52:52 -070074const char* kName = "name";
75const char* kValue = "value";
76const char* kOptions = "options";
77const char* kSlideStateName = "Slide";
78const char* kBackendStateName = "Backend";
liyuqianb73c24b2016-06-03 08:47:23 -070079const char* kSoftkeyStateName = "Softkey";
80const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -070081const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -070082const char* kSplitScreenStateName = "Split screen";
83const char* kON = "ON";
84const char* kOFF = "OFF";
liyuqiane5a6cd92016-05-27 08:52:52 -070085
jvanverth34524262016-05-04 13:49:13 -070086Viewer::Viewer(int argc, char** argv, void* platformData)
jvanverthc265a922016-04-08 12:51:45 -070087 : fCurrentMeasurement(0)
88 , fDisplayStats(false)
liyuqian6f163d22016-06-13 12:26:45 -070089 , fSplitScreen(false)
egdaniel963632f2016-06-15 14:23:40 -070090 , fBackendType(sk_app::Window::kVulkan_BackendType)
egdaniel2a0bb0a2016-04-11 08:30:40 -070091 , fZoomCenterX(0.0f)
92 , fZoomCenterY(0.0f)
93 , fZoomLevel(0.0f)
94 , fZoomScale(SK_Scalar1)
jvanverthc265a922016-04-08 12:51:45 -070095{
jvanverth3d6ed3a2016-04-07 11:09:51 -070096 memset(fMeasurements, 0, sizeof(fMeasurements));
jvanverth9f372462016-04-06 06:08:59 -070097
jvanverth2bb3b6d2016-04-08 07:24:09 -070098 SkDebugf("Command line arguments: ");
99 for (int i = 1; i < argc; ++i) {
100 SkDebugf("%s ", argv[i]);
101 }
102 SkDebugf("\n");
103
104 SkCommandLineFlags::Parse(argc, argv);
105
jvanverth85f758c2016-05-27 06:47:08 -0700106 fBackendType = FLAGS_vulkan ? sk_app::Window::kVulkan_BackendType
107 : sk_app::Window::kNativeGL_BackendType;
egdaniel963632f2016-06-15 14:23:40 -0700108
jvanverth9f372462016-04-06 06:08:59 -0700109 fWindow = Window::CreateNativeWindow(platformData);
jvanverthaf236b52016-05-20 06:01:06 -0700110 fWindow->attach(fBackendType, DisplayParams());
jvanverth9f372462016-04-06 06:08:59 -0700111
112 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700113 fCommands.attach(fWindow);
jvanverth9f372462016-04-06 06:08:59 -0700114 fWindow->registerPaintFunc(on_paint_handler, this);
liyuqiand3cdbca2016-05-17 12:44:20 -0700115 fWindow->registerTouchFunc(on_touch_handler, this);
liyuqiane5a6cd92016-05-27 08:52:52 -0700116 fWindow->registerUIStateChangedFunc(on_ui_state_changed_handler, this);
jvanverth9f372462016-04-06 06:08:59 -0700117
brianosman622c8d52016-05-10 06:50:49 -0700118 // add key-bindings
119 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
120 this->fDisplayStats = !this->fDisplayStats;
121 fWindow->inval();
122 });
123 fCommands.addCommand('c', "Modes", "Toggle sRGB color mode", [this]() {
124 DisplayParams params = fWindow->getDisplayParams();
brianosmanab824182016-06-16 11:41:44 -0700125 params.fProfileType = (kLinear_SkColorProfileType == params.fProfileType)
126 ? kSRGB_SkColorProfileType : kLinear_SkColorProfileType;
brianosman622c8d52016-05-10 06:50:49 -0700127 fWindow->setDisplayParams(params);
128 this->updateTitle();
129 fWindow->inval();
130 });
131 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
132 int previousSlide = fCurrentSlide;
133 fCurrentSlide++;
134 if (fCurrentSlide >= fSlides.count()) {
135 fCurrentSlide = 0;
136 }
137 this->setupCurrentSlide(previousSlide);
138 });
139 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
140 int previousSlide = fCurrentSlide;
141 fCurrentSlide--;
142 if (fCurrentSlide < 0) {
143 fCurrentSlide = fSlides.count() - 1;
144 }
145 this->setupCurrentSlide(previousSlide);
146 });
147 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
148 this->changeZoomLevel(1.f / 32.f);
149 fWindow->inval();
150 });
151 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
152 this->changeZoomLevel(-1.f / 32.f);
153 fWindow->inval();
154 });
jvanverth85f758c2016-05-27 06:47:08 -0700155#if 0 // this doesn't seem to work on any platform right now
jvanverthaf236b52016-05-20 06:01:06 -0700156#ifndef SK_BUILD_FOR_ANDROID
157 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
158 fWindow->detach();
159
160 if (sk_app::Window::kVulkan_BackendType == fBackendType) {
161 fBackendType = sk_app::Window::kNativeGL_BackendType;
162 }
jvanverth85f758c2016-05-27 06:47:08 -0700163 // TODO: get Vulkan -> OpenGL working on Windows without swapchain creation failure
jvanverthaf236b52016-05-20 06:01:06 -0700164 //else if (sk_app::Window::kNativeGL_BackendType == fBackendType) {
165 // fBackendType = sk_app::Window::kVulkan_BackendType;
166 //}
167
168 fWindow->attach(fBackendType, DisplayParams());
169 this->updateTitle();
jvanverth85f758c2016-05-27 06:47:08 -0700170 fWindow->inval();
jvanverthaf236b52016-05-20 06:01:06 -0700171 });
172#endif
jvanverth85f758c2016-05-27 06:47:08 -0700173#endif
brianosman622c8d52016-05-10 06:50:49 -0700174
jvanverth2bb3b6d2016-04-08 07:24:09 -0700175 // set up slides
176 this->initSlides();
177
djsollen12d62a72016-04-21 07:59:44 -0700178 fAnimTimer.run();
179
jvanverth2bb3b6d2016-04-08 07:24:09 -0700180 // set up first frame
jvanverth2bb3b6d2016-04-08 07:24:09 -0700181 fCurrentSlide = 0;
jvanverthc265a922016-04-08 12:51:45 -0700182 setupCurrentSlide(-1);
jvanverthc265a922016-04-08 12:51:45 -0700183
jvanverth9f372462016-04-06 06:08:59 -0700184 fWindow->show();
185}
186
jvanverth34524262016-05-04 13:49:13 -0700187void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700188 fAllSlideNames = Json::Value(Json::arrayValue);
189
jvanverth2bb3b6d2016-04-08 07:24:09 -0700190 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
191 while (gms) {
192 SkAutoTDelete<skiagm::GM> gm(gms->factory()(nullptr));
193
194 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
195 sk_sp<Slide> slide(new GMSlide(gm.release()));
196 fSlides.push_back(slide);
197 }
198
199 gms = gms->next();
200 }
201
202 // reverse array
203 for (int i = 0; i < fSlides.count()/2; ++i) {
204 sk_sp<Slide> temp = fSlides[i];
205 fSlides[i] = fSlides[fSlides.count() - i - 1];
206 fSlides[fSlides.count() - i - 1] = temp;
207 }
208
jvanverthc7027ab2016-06-16 09:52:35 -0700209 // samples
210 const SkViewRegister* reg = SkViewRegister::Head();
211 while (reg) {
212 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
213 fSlides.push_back(slide);
214 reg = reg->next();
215 }
216
jvanverth2bb3b6d2016-04-08 07:24:09 -0700217 // SKPs
218 for (int i = 0; i < FLAGS_skps.count(); i++) {
219 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700220 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
221 continue;
222 }
223
jvanverth2bb3b6d2016-04-08 07:24:09 -0700224 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700225 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700226 if (slide) {
227 fSlides.push_back(slide);
228 }
229 } else {
230 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700231 SkString skpName;
232 while (it.next(&skpName)) {
233 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
234 continue;
235 }
236
237 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
238 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700239 if (slide) {
240 fSlides.push_back(slide);
241 }
242 }
243 }
244 }
liyuqian6f163d22016-06-13 12:26:45 -0700245
246 // JPGs
247 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
248 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
249 SkString jpgName;
250 while (it.next(&jpgName)) {
251 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
252 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
253 if (slide) {
254 fSlides.push_back(slide);
255 }
256 }
257 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700258}
259
260
jvanverth34524262016-05-04 13:49:13 -0700261Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700262 fWindow->detach();
263 delete fWindow;
264}
265
brianosman05de2162016-05-06 13:28:57 -0700266void Viewer::updateTitle() {
jvanverth34524262016-05-04 13:49:13 -0700267 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700268 title.append(fSlides[fCurrentSlide]->getName());
brianosmanab824182016-06-16 11:41:44 -0700269 if (kSRGB_SkColorProfileType == fWindow->getDisplayParams().fProfileType) {
brianosman05de2162016-05-06 13:28:57 -0700270 title.append(" sRGB");
271 }
jvanverthaf236b52016-05-20 06:01:06 -0700272 title.append(kBackendTypeStrings[fBackendType]);
brianosman05de2162016-05-06 13:28:57 -0700273 fWindow->setTitle(title.c_str());
274}
275
276void Viewer::setupCurrentSlide(int previousSlide) {
liyuqiane5a6cd92016-05-27 08:52:52 -0700277 if (fCurrentSlide == previousSlide) {
278 return; // no change; do nothing
279 }
280
liyuqian6f163d22016-06-13 12:26:45 -0700281 // prepare dimensions for image slides
jvanverthc7027ab2016-06-16 09:52:35 -0700282 fSlides[fCurrentSlide]->load(SkIntToScalar(fWindow->width()), SkIntToScalar(fWindow->height()));
liyuqian6f163d22016-06-13 12:26:45 -0700283
liyuqiane46e4f02016-05-20 07:32:19 -0700284 fGesture.reset();
285 fDefaultMatrix.reset();
286 fDefaultMatrixInv.reset();
287
288 if (fWindow->supportsContentRect() && fWindow->scaleContentToFit()) {
289 const SkRect contentRect = fWindow->getContentRect();
290 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
291 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
292 if (contentRect.width() > 0 && contentRect.height() > 0) {
293 fDefaultMatrix.setRectToRect(slideBounds, contentRect, SkMatrix::kStart_ScaleToFit);
liyuqianbeb1c672016-05-20 11:41:01 -0700294 SkAssertResult(fDefaultMatrix.invert(&fDefaultMatrixInv));
liyuqiane46e4f02016-05-20 07:32:19 -0700295 }
296 }
297
298 if (fWindow->supportsContentRect()) {
299 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
300 SkRect windowRect = fWindow->getContentRect();
301 fDefaultMatrixInv.mapRect(&windowRect);
jvanverth1e305ba2016-06-01 09:39:15 -0700302 fGesture.setTransLimit(SkRect::MakeWH(SkIntToScalar(slideSize.width()),
303 SkIntToScalar(slideSize.height())),
304 windowRect);
liyuqiane46e4f02016-05-20 07:32:19 -0700305 }
306
brianosman05de2162016-05-06 13:28:57 -0700307 this->updateTitle();
liyuqiane5a6cd92016-05-27 08:52:52 -0700308 this->updateUIState();
jvanverthc265a922016-04-08 12:51:45 -0700309 if (previousSlide >= 0) {
310 fSlides[previousSlide]->unload();
311 }
jvanverthc265a922016-04-08 12:51:45 -0700312 fWindow->inval();
313}
314
315#define MAX_ZOOM_LEVEL 8
316#define MIN_ZOOM_LEVEL -8
317
jvanverth34524262016-05-04 13:49:13 -0700318void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700319 fZoomLevel += delta;
320 if (fZoomLevel > 0) {
321 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
322 fZoomScale = fZoomLevel + SK_Scalar1;
323 } else if (fZoomLevel < 0) {
324 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
325 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
326 } else {
327 fZoomScale = SK_Scalar1;
328 }
jvanverthc265a922016-04-08 12:51:45 -0700329}
330
liyuqiand3cdbca2016-05-17 12:44:20 -0700331SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700332 SkMatrix m;
333 m.reset();
334
335 if (fZoomLevel) {
336 SkPoint center;
337 //m = this->getLocalMatrix();//.invert(&m);
338 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
339 SkScalar cx = center.fX;
340 SkScalar cy = center.fY;
341
342 m.setTranslate(-cx, -cy);
343 m.postScale(fZoomScale, fZoomScale);
344 m.postTranslate(cx, cy);
345 }
346
liyuqiand3cdbca2016-05-17 12:44:20 -0700347 m.preConcat(fGesture.localM());
348 m.preConcat(fGesture.globalM());
jvanverthc265a922016-04-08 12:51:45 -0700349
liyuqiand3cdbca2016-05-17 12:44:20 -0700350 return m;
jvanverthc265a922016-04-08 12:51:45 -0700351}
352
liyuqian6f163d22016-06-13 12:26:45 -0700353void Viewer::drawSlide(SkCanvas* canvas, bool inSplitScreen) {
354 SkASSERT(!inSplitScreen || fWindow->supportsContentRect());
355
jvanverthc265a922016-04-08 12:51:45 -0700356 int count = canvas->save();
djsollen12d62a72016-04-21 07:59:44 -0700357
358 if (fWindow->supportsContentRect()) {
359 SkRect contentRect = fWindow->getContentRect();
liyuqian6f163d22016-06-13 12:26:45 -0700360 // If inSplitScreen, translate the image half screen to the right.
361 // Thus we have two copies of the image on each half of the screen.
liyuqian401cf482016-06-13 14:38:35 -0700362 contentRect.fLeft +=
363 inSplitScreen ? (contentRect.fRight - contentRect.fLeft) * 0.5f : 0.0f;
djsollen12d62a72016-04-21 07:59:44 -0700364 canvas->clipRect(contentRect);
365 canvas->translate(contentRect.fLeft, contentRect.fTop);
366 }
367
368 canvas->clear(SK_ColorWHITE);
liyuqiane46e4f02016-05-20 07:32:19 -0700369 canvas->concat(fDefaultMatrix);
liyuqiand3cdbca2016-05-17 12:44:20 -0700370 canvas->concat(computeMatrix());
jvanverth3d6ed3a2016-04-07 11:09:51 -0700371
liyuqian6f163d22016-06-13 12:26:45 -0700372 canvas->getMetaData().setBool(kImageColorXformMetaData, inSplitScreen);
jvanverthc265a922016-04-08 12:51:45 -0700373 fSlides[fCurrentSlide]->draw(canvas);
374 canvas->restoreToCount(count);
liyuqian6f163d22016-06-13 12:26:45 -0700375}
376
377void Viewer::onPaint(SkCanvas* canvas) {
378 drawSlide(canvas, false);
379 if (fSplitScreen && fWindow->supportsContentRect()) {
380 drawSlide(canvas, true);
381 }
jvanverthc265a922016-04-08 12:51:45 -0700382
383 if (fDisplayStats) {
384 drawStats(canvas);
385 }
brianosman622c8d52016-05-10 06:50:49 -0700386 fCommands.drawHelp(canvas);
jvanverth3d6ed3a2016-04-07 11:09:51 -0700387}
388
jvanverth814e38d2016-06-06 08:48:47 -0700389bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
liyuqiand3cdbca2016-05-17 12:44:20 -0700390 void* castedOwner = reinterpret_cast<void*>(owner);
liyuqiane46e4f02016-05-20 07:32:19 -0700391 SkPoint touchPoint = fDefaultMatrixInv.mapXY(x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700392 switch (state) {
393 case Window::kUp_InputState: {
394 fGesture.touchEnd(castedOwner);
395 break;
396 }
397 case Window::kDown_InputState: {
liyuqiane46e4f02016-05-20 07:32:19 -0700398 fGesture.touchBegin(castedOwner, touchPoint.fX, touchPoint.fY);
liyuqiand3cdbca2016-05-17 12:44:20 -0700399 break;
400 }
401 case Window::kMove_InputState: {
liyuqiane46e4f02016-05-20 07:32:19 -0700402 fGesture.touchMoved(castedOwner, touchPoint.fX, touchPoint.fY);
liyuqiand3cdbca2016-05-17 12:44:20 -0700403 break;
404 }
405 }
406 fWindow->inval();
407 return true;
408}
409
jvanverth34524262016-05-04 13:49:13 -0700410void Viewer::drawStats(SkCanvas* canvas) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700411 static const float kPixelPerMS = 2.0f;
412 static const int kDisplayWidth = 130;
413 static const int kDisplayHeight = 100;
414 static const int kDisplayPadding = 10;
415 static const int kGraphPadding = 3;
416 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
417
418 SkISize canvasSize = canvas->getDeviceSize();
419 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
420 SkIntToScalar(kDisplayPadding),
421 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
422 SkPaint paint;
423 canvas->save();
424
djsollen12d62a72016-04-21 07:59:44 -0700425 if (fWindow->supportsContentRect()) {
426 SkRect contentRect = fWindow->getContentRect();
427 canvas->clipRect(contentRect);
428 canvas->translate(contentRect.fLeft, contentRect.fTop);
429 }
430
jvanverth3d6ed3a2016-04-07 11:09:51 -0700431 canvas->clipRect(rect);
432 paint.setColor(SK_ColorBLACK);
433 canvas->drawRect(rect, paint);
434 // draw the 16ms line
435 paint.setColor(SK_ColorLTGRAY);
436 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
437 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
438 paint.setColor(SK_ColorRED);
439 paint.setStyle(SkPaint::kStroke_Style);
440 canvas->drawRect(rect, paint);
441
442 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
443 const int xStep = 2;
444 const int startY = SkScalarTruncToInt(rect.fBottom);
445 int i = fCurrentMeasurement;
446 do {
447 int endY = startY - (int)(fMeasurements[i] * kPixelPerMS + 0.5); // round to nearest value
448 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
449 SkIntToScalar(x), SkIntToScalar(endY), paint);
450 i++;
451 i &= (kMeasurementCount - 1); // fast mod
452 x += xStep;
453 } while (i != fCurrentMeasurement);
jvanverth9f372462016-04-06 06:08:59 -0700454
455 canvas->restore();
456}
457
jvanverth34524262016-05-04 13:49:13 -0700458void Viewer::onIdle(double ms) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700459 // Record measurements
460 fMeasurements[fCurrentMeasurement++] = ms;
461 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod
462 SkASSERT(fCurrentMeasurement < kMeasurementCount);
463
jvanverthc265a922016-04-08 12:51:45 -0700464 fAnimTimer.updateTime();
jvanverth9d5e47f2016-04-26 08:01:33 -0700465 if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) {
jvanverthc265a922016-04-08 12:51:45 -0700466 fWindow->inval();
liyuqian1f508fd2016-06-07 06:57:40 -0700467 updateUIState(); // Update the FPS
jvanverthc265a922016-04-08 12:51:45 -0700468 }
jvanverth9f372462016-04-06 06:08:59 -0700469}
liyuqiane5a6cd92016-05-27 08:52:52 -0700470
471void Viewer::updateUIState() {
liyuqianb73c24b2016-06-03 08:47:23 -0700472 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -0700473 Json::Value slideState(Json::objectValue);
474 slideState[kName] = kSlideStateName;
475 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -0700476 if (fAllSlideNames.size() == 0) {
477 for(auto slide : fSlides) {
478 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
479 }
liyuqiane5a6cd92016-05-27 08:52:52 -0700480 }
liyuqian1f508fd2016-06-07 06:57:40 -0700481 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -0700482
liyuqianb73c24b2016-06-03 08:47:23 -0700483 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -0700484 Json::Value backendState(Json::objectValue);
485 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -0700486 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -0700487 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -0700488 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -0700489 backendState[kOptions].append(Json::Value(str));
490 }
liyuqiane5a6cd92016-05-27 08:52:52 -0700491
liyuqianb73c24b2016-06-03 08:47:23 -0700492 // Softkey state
493 Json::Value softkeyState(Json::objectValue);
494 softkeyState[kName] = kSoftkeyStateName;
495 softkeyState[kValue] = kSoftkeyHint;
496 softkeyState[kOptions] = Json::Value(Json::arrayValue);
497 softkeyState[kOptions].append(kSoftkeyHint);
498 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
499 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
500 }
501
liyuqian1f508fd2016-06-07 06:57:40 -0700502 // FPS state
503 Json::Value fpsState(Json::objectValue);
504 fpsState[kName] = kFpsStateName;
505 double measurement = fMeasurements[
506 (fCurrentMeasurement + (kMeasurementCount-1)) % kMeasurementCount
507 ];
508 fpsState[kValue] = SkStringPrintf("%8.3lf ms", measurement).c_str();
509 fpsState[kOptions] = Json::Value(Json::arrayValue);
510
liyuqian6f163d22016-06-13 12:26:45 -0700511 // Split screen state
512 Json::Value splitScreenState(Json::objectValue);
513 splitScreenState[kName] = kSplitScreenStateName;
514 splitScreenState[kValue] = fSplitScreen ? kON : kOFF;
515 splitScreenState[kOptions] = Json::Value(Json::arrayValue);
516 splitScreenState[kOptions].append(kON);
517 splitScreenState[kOptions].append(kOFF);
518
liyuqiane5a6cd92016-05-27 08:52:52 -0700519 Json::Value state(Json::arrayValue);
520 state.append(slideState);
521 state.append(backendState);
liyuqianb73c24b2016-06-03 08:47:23 -0700522 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -0700523 state.append(fpsState);
liyuqian6f163d22016-06-13 12:26:45 -0700524 state.append(splitScreenState);
liyuqiane5a6cd92016-05-27 08:52:52 -0700525
526 fWindow->setUIState(state);
527}
528
529void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -0700530 // For those who will add more features to handle the state change in this function:
531 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
532 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
533 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -0700534 if (stateName.equals(kSlideStateName)) {
535 int previousSlide = fCurrentSlide;
536 fCurrentSlide = 0;
537 for(auto slide : fSlides) {
538 if (slide->getName().equals(stateValue)) {
539 setupCurrentSlide(previousSlide);
540 break;
541 }
542 fCurrentSlide++;
543 }
544 if (fCurrentSlide >= fSlides.count()) {
545 fCurrentSlide = previousSlide;
546 SkDebugf("Slide not found: %s", stateValue.c_str());
547 }
liyuqian6cb70252016-06-02 12:16:25 -0700548 } else if (stateName.equals(kBackendStateName)) {
549 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
550 if (stateValue.equals(kBackendTypeStrings[i])) {
551 if (fBackendType != i) {
552 fBackendType = (sk_app::Window::BackendType)i;
553 fWindow->detach();
554 fWindow->attach(fBackendType, DisplayParams());
555 fWindow->inval();
556 updateTitle();
557 updateUIState();
558 }
559 break;
560 }
561 }
liyuqianb73c24b2016-06-03 08:47:23 -0700562 } else if (stateName.equals(kSoftkeyStateName)) {
563 if (!stateValue.equals(kSoftkeyHint)) {
564 fCommands.onSoftkey(stateValue);
565 updateUIState(); // This is still needed to reset the value to kSoftkeyHint
566 }
liyuqian6f163d22016-06-13 12:26:45 -0700567 } else if (stateName.equals(kSplitScreenStateName)) {
568 bool newSplitScreen = stateValue.equals(kON);
569 if (newSplitScreen != fSplitScreen) {
570 fSplitScreen = newSplitScreen;
571 fWindow->inval();
572 updateUIState();
573 }
liyuqiane5a6cd92016-05-27 08:52:52 -0700574 } else {
575 SkDebugf("Unknown stateName: %s", stateName.c_str());
576 }
577}