blob: ad02a6574be2c945f1583d15e4aae2d9bf8a405e [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
Chris Dalton2d18f412018-02-20 13:23:32 -070010#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070011#include "GMSlide.h"
liyuqian6f163d22016-06-13 12:26:45 -070012#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050013#include "Resources.h"
jvanverthc7027ab2016-06-16 09:52:35 -070014#include "SampleSlide.h"
Florin Malita54f65c42018-01-16 17:04:30 -050015#include "SkottieSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070016#include "SKPSlide.h"
Florin Malita76a076b2018-02-15 18:40:48 -050017#include "SlideDir.h"
jvanverth9f372462016-04-06 06:08:59 -070018
csmartdalton61cd31a2017-02-27 17:00:53 -070019#include "GrContext.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070020#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050021#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040022#include "SkColorSpaceXformCanvas.h"
Ben Wagner483c7722018-02-20 17:06:07 -050023#include "SkCommonFlags.h"
Brian Osman2dd96932016-10-18 15:33:53 -040024#include "SkCommandLineFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070025#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040026#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050027#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040028#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050029#include "SkImagePriv.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070030#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050031#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050032#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050033#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040034#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070035#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070036#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050037#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050038#include "SkTestFontMgr.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050039#include "SkThreadedBMPDevice.h"
jvanverth9f372462016-04-06 06:08:59 -070040
Brian Osman79086b92017-02-10 13:36:16 -050041#include "imgui.h"
42
Chris Dalton1a325d22017-07-14 15:17:41 -060043#include "ccpr/GrCoverageCountingPathRenderer.h"
44
csmartdalton578f0642017-02-24 16:04:47 -070045#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070046#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070047
jvanverth34524262016-05-04 13:49:13 -070048using namespace sk_app;
49
csmartdalton61cd31a2017-02-27 17:00:53 -070050static std::map<GpuPathRenderers, std::string> gPathRendererNames;
51
jvanverth9f372462016-04-06 06:08:59 -070052Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070053 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070054}
55
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060056static DEFINE_string(slide, "", "Start on this sample.");
57static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050058
bsalomon6c471f72016-07-26 12:56:32 -070059#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070060# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070061#else
62# define BACKENDS_STR "\"sw\" and \"gl\""
63#endif
64
Brian Osman2dd96932016-10-18 15:33:53 -040065static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070066
Brian Salomonbdecacf2018-02-02 20:32:49 -050067static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070068
Chris Dalton2d18f412018-02-20 13:23:32 -070069DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
70
Brian Osman53136aa2017-07-20 15:43:35 -040071DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050072
Brian Salomon194db172017-08-17 14:37:06 -040073const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070074 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040075#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
76 "ANGLE",
77#endif
jvanverth063ece72016-06-17 09:29:14 -070078#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070079 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070080#endif
csmartdalton578f0642017-02-24 16:04:47 -070081 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070082};
83
bsalomon6c471f72016-07-26 12:56:32 -070084static sk_app::Window::BackendType get_backend_type(const char* str) {
85#ifdef SK_VULKAN
86 if (0 == strcmp(str, "vk")) {
87 return sk_app::Window::kVulkan_BackendType;
88 } else
89#endif
Brian Salomon194db172017-08-17 14:37:06 -040090#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
91 if (0 == strcmp(str, "angle")) {
92 return sk_app::Window::kANGLE_BackendType;
93 } else
94#endif
bsalomon6c471f72016-07-26 12:56:32 -070095 if (0 == strcmp(str, "gl")) {
96 return sk_app::Window::kNativeGL_BackendType;
97 } else if (0 == strcmp(str, "sw")) {
98 return sk_app::Window::kRaster_BackendType;
99 } else {
100 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
101 return sk_app::Window::kRaster_BackendType;
102 }
103}
104
Brian Osmana109e392017-02-24 09:49:14 -0500105static SkColorSpacePrimaries gSrgbPrimaries = {
106 0.64f, 0.33f,
107 0.30f, 0.60f,
108 0.15f, 0.06f,
109 0.3127f, 0.3290f };
110
111static SkColorSpacePrimaries gAdobePrimaries = {
112 0.64f, 0.33f,
113 0.21f, 0.71f,
114 0.15f, 0.06f,
115 0.3127f, 0.3290f };
116
117static SkColorSpacePrimaries gP3Primaries = {
118 0.680f, 0.320f,
119 0.265f, 0.690f,
120 0.150f, 0.060f,
121 0.3127f, 0.3290f };
122
123static SkColorSpacePrimaries gRec2020Primaries = {
124 0.708f, 0.292f,
125 0.170f, 0.797f,
126 0.131f, 0.046f,
127 0.3127f, 0.3290f };
128
129struct NamedPrimaries {
130 const char* fName;
131 SkColorSpacePrimaries* fPrimaries;
132} gNamedPrimaries[] = {
133 { "sRGB", &gSrgbPrimaries },
134 { "AdobeRGB", &gAdobePrimaries },
135 { "P3", &gP3Primaries },
136 { "Rec. 2020", &gRec2020Primaries },
137};
138
139static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
140 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
141}
142
Brian Osman70d2f432017-11-08 09:54:10 -0500143static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
144 // In raster mode, we still use GL for the window.
145 // This lets us render the GUI faster (and correct).
146 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
147}
148
liyuqiane5a6cd92016-05-27 08:52:52 -0700149const char* kName = "name";
150const char* kValue = "value";
151const char* kOptions = "options";
152const char* kSlideStateName = "Slide";
153const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700154const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700155const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700156const char* kSoftkeyStateName = "Softkey";
157const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700158const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700159const char* kON = "ON";
160const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700161const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700162
jvanverth34524262016-05-04 13:49:13 -0700163Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500164 : fCurrentSlide(-1)
165 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500166 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500167 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500168 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500169 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500170 , fShowZoomWindow(false)
171 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700172 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500173 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500174 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500175 // Our UI can only tweak gamma (currently), so start out gamma-only
176 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700177 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400178 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500179 , fTileCnt(0)
180 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700181{
Greg Daniel285db442016-10-14 09:12:53 -0400182 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700183
Brian Osmanf09e35e2017-12-15 14:48:09 -0500184 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
185 gPathRendererNames[GpuPathRenderers::kDefault] =
186 "Default Ganesh Behavior (best path renderer, not including CCPR)";
187 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
188 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
189 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
190 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
191 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
192 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700193
jvanverth2bb3b6d2016-04-08 07:24:09 -0700194 SkDebugf("Command line arguments: ");
195 for (int i = 1; i < argc; ++i) {
196 SkDebugf("%s ", argv[i]);
197 }
198 SkDebugf("\n");
199
200 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500201#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400202 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500203#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700204
Ben Wagner483c7722018-02-20 17:06:07 -0500205 if (!FLAGS_nativeFonts) {
206 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
207 }
208
Brian Osmanbc8150f2017-07-24 11:38:01 -0400209 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400210 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400211
bsalomon6c471f72016-07-26 12:56:32 -0700212 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700213 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700214
csmartdalton578f0642017-02-24 16:04:47 -0700215 DisplayParams displayParams;
216 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700217 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700218 fWindow->setRequestedDisplayParams(displayParams);
219
Brian Osman56a24812017-12-19 11:15:16 -0500220 // Configure timers
221 fStatsLayer.setActive(false);
222 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
223 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
224 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
225
jvanverth9f372462016-04-06 06:08:59 -0700226 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700227 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500228 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500229 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500230 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700231
brianosman622c8d52016-05-10 06:50:49 -0700232 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500233 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
234 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
235 fWindow->inval();
236 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500237 // Command to jump directly to the slide picker and give it focus
238 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
239 this->fShowImGuiDebugWindow = true;
240 this->fShowSlidePicker = true;
241 fWindow->inval();
242 });
243 // Alias that to Backspace, to match SampleApp
244 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
245 this->fShowImGuiDebugWindow = true;
246 this->fShowSlidePicker = true;
247 fWindow->inval();
248 });
Brian Osman79086b92017-02-10 13:36:16 -0500249 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
250 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
251 fWindow->inval();
252 });
Brian Osmanf6877092017-02-13 09:39:57 -0500253 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
254 this->fShowZoomWindow = !this->fShowZoomWindow;
255 fWindow->inval();
256 });
brianosman622c8d52016-05-10 06:50:49 -0700257 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500258 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700259 fWindow->inval();
260 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400261 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500262 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400263 this->updateTitle();
264 fWindow->inval();
265 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500266 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500267 switch (fColorMode) {
268 case ColorMode::kLegacy:
269 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
270 break;
271 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
272 this->setColorMode(ColorMode::kColorManagedSRGB8888);
273 break;
274 case ColorMode::kColorManagedSRGB8888:
275 this->setColorMode(ColorMode::kColorManagedLinearF16);
276 break;
277 case ColorMode::kColorManagedLinearF16:
278 this->setColorMode(ColorMode::kLegacy);
279 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500280 }
brianosman622c8d52016-05-10 06:50:49 -0700281 });
282 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500283 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700284 });
285 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500286 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700287 });
288 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
289 this->changeZoomLevel(1.f / 32.f);
290 fWindow->inval();
291 });
292 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
293 this->changeZoomLevel(-1.f / 32.f);
294 fWindow->inval();
295 });
jvanverthaf236b52016-05-20 06:01:06 -0700296 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400297 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
298 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500299 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400300#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
301 if (newBackend == sk_app::Window::kVulkan_BackendType) {
302 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
303 sk_app::Window::kBackendTypeCount);
304 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
305 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500306 }
307#endif
Brian Osman621491e2017-02-28 15:45:01 -0500308 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700309 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500310 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
311 fTileCnt++;
312 if (fThreadCnt == 0) {
313 this->resetExecutor();
314 }
315 this->updateTitle();
316 fWindow->inval();
317 });
318 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
319 fTileCnt = SkTMax(0, fTileCnt - 1);
320 if (fThreadCnt == 0) {
321 this->resetExecutor();
322 }
323 this->updateTitle();
324 fWindow->inval();
325 });
326 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
327 if (fTileCnt == 0) {
328 return;
329 }
330 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
331 this->resetExecutor();
332 this->updateTitle();
333 fWindow->inval();
334 });
335 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
336 if (fTileCnt == 0) {
337 return;
338 }
339 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
340 this->resetExecutor();
341 this->updateTitle();
342 fWindow->inval();
343 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500344 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
345 fSaveToSKP = true;
346 fWindow->inval();
347 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500348 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
349 if (!fPaintOverrides.fHinting) {
350 fPaintOverrides.fHinting = true;
351 fPaint.setHinting(SkPaint::kNo_Hinting);
352 } else {
353 switch (fPaint.getHinting()) {
354 case SkPaint::kNo_Hinting:
355 fPaint.setHinting(SkPaint::kSlight_Hinting);
356 break;
357 case SkPaint::kSlight_Hinting:
358 fPaint.setHinting(SkPaint::kNormal_Hinting);
359 break;
360 case SkPaint::kNormal_Hinting:
361 fPaint.setHinting(SkPaint::kFull_Hinting);
362 break;
363 case SkPaint::kFull_Hinting:
364 fPaint.setHinting(SkPaint::kNo_Hinting);
365 fPaintOverrides.fHinting = false;
366 break;
367 }
368 }
369 this->updateTitle();
370 fWindow->inval();
371 });
372 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
373 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
374 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
375 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
376 fPaint.setAntiAlias(false);
377 fPaintOverrides.fOriginalSkUseAnalyticAA = gSkUseAnalyticAA;
378 fPaintOverrides.fOriginalSkForceAnalyticAA = gSkForceAnalyticAA;
379 fPaintOverrides.fOriginalSkUseDeltaAA = gSkUseDeltaAA;
380 fPaintOverrides.fOriginalSkForceDeltaAA = gSkForceDeltaAA;
381 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
382 gSkUseDeltaAA = gSkForceDeltaAA = false;
383 } else {
384 fPaint.setAntiAlias(true);
385 switch (fPaintOverrides.fAntiAlias) {
386 case SkPaintFields::AntiAliasState::Alias:
387 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
388 break;
389 case SkPaintFields::AntiAliasState::Normal:
390 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
391 gSkUseDeltaAA = gSkForceDeltaAA = false;
392 gSkUseAnalyticAA = true;
393 break;
394 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
395 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
396 gSkForceAnalyticAA = true;
397 break;
398 case SkPaintFields::AntiAliasState::AnalyticAAForced:
399 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
400 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
401 gSkUseDeltaAA = true;
402 break;
403 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
404 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
405 gSkForceDeltaAA = true;
406 break;
407 case SkPaintFields::AntiAliasState::DeltaAAForced:
408 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
409 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
410 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
411 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
412 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
413 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
414 break;
415 }
416 }
417 this->updateTitle();
418 fWindow->inval();
419 });
420 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
421 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
422 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
423 fPaint.setLCDRenderText(false);
424 } else {
425 if (!fPaint.isLCDRenderText()) {
426 fPaint.setLCDRenderText(true);
427 } else {
428 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
429 }
430 }
431 this->updateTitle();
432 fWindow->inval();
433 });
434 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
435 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
436 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
437 fPaint.setSubpixelText(false);
438 } else {
439 if (!fPaint.isSubpixelText()) {
440 fPaint.setSubpixelText(true);
441 } else {
442 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
443 }
444 }
445 this->updateTitle();
446 fWindow->inval();
447 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500448
jvanverth2bb3b6d2016-04-08 07:24:09 -0700449 // set up slides
450 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500451 if (FLAGS_list) {
452 this->listNames();
453 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700454
djsollen12d62a72016-04-21 07:59:44 -0700455 fAnimTimer.run();
456
Hal Canaryc465d132017-12-08 10:21:31 -0500457 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500458 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400459 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500460 }
461 fImGuiGamutPaint.setColor(SK_ColorWHITE);
462 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
463
Brian Osman70d2f432017-11-08 09:54:10 -0500464 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500465 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700466}
467
jvanverth34524262016-05-04 13:49:13 -0700468void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700469 fAllSlideNames = Json::Value(Json::arrayValue);
470
Chris Dalton2d18f412018-02-20 13:23:32 -0700471 // Bisect slide.
472 if (!FLAGS_bisect.isEmpty()) {
473 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
474 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
475 if (FLAGS_bisect.count() >= 2) {
476 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
477 bisect->onChar(*ch);
478 }
479 }
480 fSlides.push_back(std::move(bisect));
481 }
482 }
483
484 // GMs
485 int firstGM = fSlides.count();
jvanverth2bb3b6d2016-04-08 07:24:09 -0700486 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
487 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400488 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700489
490 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
491 sk_sp<Slide> slide(new GMSlide(gm.release()));
Chris Dalton2d18f412018-02-20 13:23:32 -0700492 fSlides.push_back(std::move(slide));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700493 }
494
495 gms = gms->next();
496 }
Chris Dalton2d18f412018-02-20 13:23:32 -0700497 // reverse gms
498 int numGMs = fSlides.count() - firstGM;
499 for (int i = 0; i < numGMs/2; ++i) {
500 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
jvanverth2bb3b6d2016-04-08 07:24:09 -0700501 }
502
jvanverthc7027ab2016-06-16 09:52:35 -0700503 // samples
504 const SkViewRegister* reg = SkViewRegister::Head();
505 while (reg) {
506 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700507 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
508 fSlides.push_back(slide);
509 }
jvanverthc7027ab2016-06-16 09:52:35 -0700510 reg = reg->next();
511 }
512
jvanverth2bb3b6d2016-04-08 07:24:09 -0700513 // SKPs
514 for (int i = 0; i < FLAGS_skps.count(); i++) {
515 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700516 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
517 continue;
518 }
519
jvanverth2bb3b6d2016-04-08 07:24:09 -0700520 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700521 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700522 if (slide) {
523 fSlides.push_back(slide);
524 }
525 } else {
526 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700527 SkString skpName;
528 while (it.next(&skpName)) {
529 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
530 continue;
531 }
532
533 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
534 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700535 if (slide) {
536 fSlides.push_back(slide);
537 }
538 }
539 }
540 }
liyuqian6f163d22016-06-13 12:26:45 -0700541
542 // JPGs
543 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
544 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
545 SkString jpgName;
546 while (it.next(&jpgName)) {
brianosmane1d20072016-07-12 09:07:33 -0700547 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) {
548 continue;
549 }
550
liyuqian6f163d22016-06-13 12:26:45 -0700551 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
552 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
553 if (slide) {
554 fSlides.push_back(slide);
555 }
556 }
557 }
Florin Malita094ccde2017-12-30 12:27:00 -0500558
559 // JSONs
560 for (const auto& json : FLAGS_jsons) {
Florin Malita76a076b2018-02-15 18:40:48 -0500561 SkTArray<sk_sp<Slide>, true> dirSlides;
562
Florin Malita094ccde2017-12-30 12:27:00 -0500563 SkOSFile::Iter it(json.c_str(), ".json");
564 SkString jsonName;
565 while (it.next(&jsonName)) {
566 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jsonName.c_str())) {
567 continue;
568 }
Florin Malita76a076b2018-02-15 18:40:48 -0500569 auto slide = sk_make_sp<SkottieSlide>(jsonName, SkOSPath::Join(json.c_str(),
570 jsonName.c_str()));
571 dirSlides.push_back(slide);
572 fSlides.push_back(std::move(slide));
573 }
574
575 if (!dirSlides.empty()) {
576 fSlides.push_back(sk_make_sp<SlideDir>(SkStringPrintf("skottie-dir[%s]", json.c_str()),
577 std::move(dirSlides)));
Florin Malita094ccde2017-12-30 12:27:00 -0500578 }
579 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700580}
581
582
jvanverth34524262016-05-04 13:49:13 -0700583Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700584 fWindow->detach();
585 delete fWindow;
586}
587
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500588struct SkPaintTitleUpdater {
589 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
590 void append(const char* s) {
591 if (fCount == 0) {
592 fTitle->append(" {");
593 } else {
594 fTitle->append(", ");
595 }
596 fTitle->append(s);
597 ++fCount;
598 }
599 void done() {
600 if (fCount > 0) {
601 fTitle->append("}");
602 }
603 }
604 SkString* fTitle;
605 int fCount;
606};
607
brianosman05de2162016-05-06 13:28:57 -0700608void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700609 if (!fWindow) {
610 return;
611 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500612 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700613 return; // Surface hasn't been created yet.
614 }
615
jvanverth34524262016-05-04 13:49:13 -0700616 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700617 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700618
Yuqian Li399b3c22017-08-03 11:08:15 -0400619 if (gSkUseDeltaAA) {
620 if (gSkForceDeltaAA) {
621 title.append(" <FDAA>");
622 } else {
623 title.append(" <DAA>");
624 }
625 } else if (gSkUseAnalyticAA) {
626 if (gSkForceAnalyticAA) {
627 title.append(" <FAAA>");
628 } else {
629 title.append(" <AAA>");
630 }
631 }
632
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500633 SkPaintTitleUpdater paintTitle(&title);
634 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
635 if (fPaint.isAntiAlias()) {
636 paintTitle.append("Antialias");
637 } else {
638 paintTitle.append("Alias");
639 }
640 }
641 if (fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag) {
642 if (fPaint.isLCDRenderText()) {
643 paintTitle.append("LCD");
644 } else {
645 paintTitle.append("lcd");
646 }
647 }
648 if (fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag) {
649 if (fPaint.isSubpixelText()) {
650 paintTitle.append("Subpixel Glyphs");
651 } else {
652 paintTitle.append("Pixel Glyphs");
653 }
654 }
655 if (fPaintOverrides.fHinting) {
656 switch (fPaint.getHinting()) {
657 case SkPaint::kNo_Hinting:
658 paintTitle.append("No Hinting");
659 break;
660 case SkPaint::kSlight_Hinting:
661 paintTitle.append("Slight Hinting");
662 break;
663 case SkPaint::kNormal_Hinting:
664 paintTitle.append("Normal Hinting");
665 break;
666 case SkPaint::kFull_Hinting:
667 paintTitle.append("Full Hinting");
668 break;
669 }
670 }
671 paintTitle.done();
672
Yuqian Lib2ba6642017-11-22 12:07:41 -0500673 if (fTileCnt > 0) {
674 title.appendf(" T%d", fTileCnt);
675 if (fThreadCnt > 0) {
676 title.appendf("/%d", fThreadCnt);
677 }
678 }
679
Brian Osman92004802017-03-06 11:47:26 -0500680 switch (fColorMode) {
681 case ColorMode::kLegacy:
682 title.append(" Legacy 8888");
683 break;
684 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
685 title.append(" ColorManaged 8888 (Nonlinear blending)");
686 break;
687 case ColorMode::kColorManagedSRGB8888:
688 title.append(" ColorManaged 8888");
689 break;
690 case ColorMode::kColorManagedLinearF16:
691 title.append(" ColorManaged F16");
692 break;
693 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500694
Brian Osman92004802017-03-06 11:47:26 -0500695 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500696 int curPrimaries = -1;
697 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
698 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
699 curPrimaries = i;
700 break;
701 }
702 }
703 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500704
705 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
706 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
707 }
brianosman05de2162016-05-06 13:28:57 -0700708 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500709
csmartdalton578f0642017-02-24 16:04:47 -0700710 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700711 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500712 int msaa = fWindow->sampleCount();
713 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700714 title.appendf(" MSAA: %i", msaa);
715 }
716 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700717
718 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400719 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700720 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
721 }
722
brianosman05de2162016-05-06 13:28:57 -0700723 fWindow->setTitle(title.c_str());
724}
725
Florin Malitaab99c342018-01-16 16:23:03 -0500726int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500727
728 if (!FLAGS_slide.isEmpty()) {
729 int count = fSlides.count();
730 for (int i = 0; i < count; i++) {
731 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500732 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500733 }
734 }
735
736 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
737 this->listNames();
738 }
739
Florin Malitaab99c342018-01-16 16:23:03 -0500740 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500741}
742
Florin Malitaab99c342018-01-16 16:23:03 -0500743void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500744 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500745 for (const auto& slide : fSlides) {
746 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500747 }
748}
749
Florin Malitaab99c342018-01-16 16:23:03 -0500750void Viewer::setCurrentSlide(int slide) {
751 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700752
Florin Malitaab99c342018-01-16 16:23:03 -0500753 if (slide == fCurrentSlide) {
754 return;
755 }
756
757 if (fCurrentSlide >= 0) {
758 fSlides[fCurrentSlide]->unload();
759 }
760
761 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
762 SkIntToScalar(fWindow->height()));
763 fCurrentSlide = slide;
764 this->setupCurrentSlide();
765}
766
767void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500768 if (fCurrentSlide >= 0) {
769 // prepare dimensions for image slides
770 fGesture.resetTouchState();
771 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700772
Jim Van Verth0848fb02018-01-22 13:39:30 -0500773 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
774 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
775 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400776
Jim Van Verth0848fb02018-01-22 13:39:30 -0500777 // Start with a matrix that scales the slide to the available screen space
778 if (fWindow->scaleContentToFit()) {
779 if (windowRect.width() > 0 && windowRect.height() > 0) {
780 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
781 }
liyuqiane46e4f02016-05-20 07:32:19 -0700782 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500783
784 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400785 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500786
787 this->updateTitle();
788 this->updateUIState();
789
790 fStatsLayer.resetMeasurements();
791
792 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700793 }
jvanverthc265a922016-04-08 12:51:45 -0700794}
795
796#define MAX_ZOOM_LEVEL 8
797#define MIN_ZOOM_LEVEL -8
798
jvanverth34524262016-05-04 13:49:13 -0700799void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700800 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400801 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Yuqian Li755778c2018-03-28 16:23:31 -0400802
803 // Update the trans limit as the zoom level changes.
804 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
805 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
806 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
807 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
808}
809
810SkMatrix Viewer::computePreTouchMatrix() {
811 SkMatrix m = fDefaultMatrix;
812 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
813 : SK_Scalar1 + fZoomLevel;
814 m.preScale(zoomScale, zoomScale);
815 return m;
jvanverthc265a922016-04-08 12:51:45 -0700816}
817
liyuqiand3cdbca2016-05-17 12:44:20 -0700818SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400819 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700820 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400821 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700822 return m;
jvanverthc265a922016-04-08 12:51:45 -0700823}
824
Brian Osman621491e2017-02-28 15:45:01 -0500825void Viewer::setBackend(sk_app::Window::BackendType backendType) {
826 fBackendType = backendType;
827
828 fWindow->detach();
829
Brian Osman70d2f432017-11-08 09:54:10 -0500830#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400831 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
832 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500833 DisplayParams params = fWindow->getRequestedDisplayParams();
834 delete fWindow;
835 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500836
Brian Osman70d2f432017-11-08 09:54:10 -0500837 // re-register callbacks
838 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500839 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500840 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500841 fWindow->pushLayer(&fImGuiLayer);
842
Brian Osman70d2f432017-11-08 09:54:10 -0500843 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
844 // will still include our correct sample count. But the re-created fWindow will lose that
845 // information. On Windows, we need to re-create the window when changing sample count,
846 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
847 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
848 // as if we had never changed windows at all).
849 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500850#endif
851
Brian Osman70d2f432017-11-08 09:54:10 -0500852 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500853}
854
Brian Osman92004802017-03-06 11:47:26 -0500855void Viewer::setColorMode(ColorMode colorMode) {
856 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700857
Brian Osmanf750fbc2017-02-08 10:47:28 -0500858 // When we're in color managed mode, we tag our window surface as sRGB. If we've switched into
Brian Osmane0d4fba2017-03-15 10:24:55 -0400859 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700860 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500861 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400862 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
863 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500864 if (wasInLegacy != wantLegacy) {
865 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700866 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500867 }
868
869 this->updateTitle();
870 fWindow->inval();
871}
872
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500873class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
874public:
875 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
876 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
877 { }
878 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -0400879 if (*paint == nullptr) {
880 return true;
881 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500882 if (fPaintOverrides->fHinting) {
883 paint->writable()->setHinting(fPaint->getHinting());
884 }
885 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
886 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
887 }
888 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
889 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
890 }
891 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
892 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
893 }
894 return true;
895 }
896 SkPaint* fPaint;
897 Viewer::SkPaintFields* fPaintOverrides;
898};
899
Brian Osmanf750fbc2017-02-08 10:47:28 -0500900void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500901 SkAutoCanvasRestore autorestore(canvas, false);
902
Brian Osmanf750fbc2017-02-08 10:47:28 -0500903 // By default, we render directly into the window's surface/canvas
904 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500905 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700906
Brian Osmane0d4fba2017-03-15 10:24:55 -0400907 // If we're in any of the color managed modes, construct the color space we're going to use
908 sk_sp<SkColorSpace> cs = nullptr;
909 if (ColorMode::kLegacy != fColorMode) {
910 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
911 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400912 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400913 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500914 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
915 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
916 } else {
917 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
918 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400919 }
920
Brian Osman3ac99cf2017-12-01 11:23:53 -0500921 if (fSaveToSKP) {
922 SkPictureRecorder recorder;
923 SkCanvas* recorderCanvas = recorder.beginRecording(
924 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
925 // In xform-canvas mode, record the transformed output
926 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
927 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
928 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
929 recorderCanvas = xformCanvas.get();
930 }
931 fSlides[fCurrentSlide]->draw(recorderCanvas);
932 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
933 SkFILEWStream stream("sample_app.skp");
934 picture->serialize(&stream);
935 fSaveToSKP = false;
936 }
937
Brian Osmane0d4fba2017-03-15 10:24:55 -0400938 // If we're in F16, or we're zooming, or we're in color correct 8888 and the gamut isn't sRGB,
Brian Osman70d2f432017-11-08 09:54:10 -0500939 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
940 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500941 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -0500942 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
943 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -0500944 if (Window::kRaster_BackendType == fBackendType ||
945 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500946 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400947 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
948 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
949
Brian Osman92004802017-03-06 11:47:26 -0500950 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
951 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400952 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
953 // with a special canvas (below) that has the color space attached
954 sk_sp<SkColorSpace> offscreenColorSpace =
955 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500956 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400957 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500958 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
959 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -0500960 SkPixmap offscreenPixmap;
961 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
962 SkBitmap offscreenBitmap;
963 offscreenBitmap.installPixels(offscreenPixmap);
964 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
965 fThreadCnt, fExecutor.get()));
966 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
967 slideCanvas = threadedCanvas.get();
968 } else {
969 slideCanvas = offscreenSurface->getCanvas();
970 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500971 }
972
Brian Osmane0d4fba2017-03-15 10:24:55 -0400973 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
974 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
975 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
976 slideCanvas = xformCanvas.get();
977 }
978
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500979 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500980 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500981 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500982 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -0500983 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500984 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
985 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -0500986 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500987 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500988
989 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -0500990 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -0500991 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -0500992 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500993
994 // If we rendered offscreen, snap an image and push the results to the window's canvas
995 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500996 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500997
998 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400999 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001000 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001001 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001002 SkPaint paint;
1003 paint.setBlendMode(SkBlendMode::kSrc);
1004 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -07001005 }
liyuqian6f163d22016-06-13 12:26:45 -07001006}
1007
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001008void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001009 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001010 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001011}
Jim Van Verth6f449692017-02-14 15:16:46 -05001012
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001013void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001014 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001015
brianosman622c8d52016-05-10 06:50:49 -07001016 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001017
Brian Osmand67e5182017-12-08 16:46:09 -05001018 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -05001019
Brian Osman1df161a2017-02-09 12:10:20 -05001020 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -04001021 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001022}
1023
Florin Malitacefc1b92018-02-19 21:43:47 -05001024SkPoint Viewer::mapEvent(float x, float y) {
1025 const auto m = this->computeMatrix();
1026 SkMatrix inv;
1027
1028 SkAssertResult(m.invert(&inv));
1029
1030 return inv.mapXY(x, y);
1031}
1032
jvanverth814e38d2016-06-06 08:48:47 -07001033bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001034 if (GestureDevice::kMouse == fGestureDevice) {
1035 return false;
1036 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001037
1038 const auto slidePt = this->mapEvent(x, y);
1039 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1040 fWindow->inval();
1041 return true;
1042 }
1043
liyuqiand3cdbca2016-05-17 12:44:20 -07001044 void* castedOwner = reinterpret_cast<void*>(owner);
1045 switch (state) {
1046 case Window::kUp_InputState: {
1047 fGesture.touchEnd(castedOwner);
1048 break;
1049 }
1050 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001051 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001052 break;
1053 }
1054 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001055 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001056 break;
1057 }
1058 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001059 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001060 fWindow->inval();
1061 return true;
1062}
1063
Brian Osman80fc07e2017-12-08 16:45:43 -05001064bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001065 if (GestureDevice::kTouch == fGestureDevice) {
1066 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001067 }
Brian Osman16c81a12017-12-20 11:58:34 -05001068
Florin Malitacefc1b92018-02-19 21:43:47 -05001069 const auto slidePt = this->mapEvent(x, y);
1070 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1071 fWindow->inval();
1072 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001073 }
1074
1075 switch (state) {
1076 case Window::kUp_InputState: {
1077 fGesture.touchEnd(nullptr);
1078 break;
1079 }
1080 case Window::kDown_InputState: {
1081 fGesture.touchBegin(nullptr, x, y);
1082 break;
1083 }
1084 case Window::kMove_InputState: {
1085 fGesture.touchMoved(nullptr, x, y);
1086 break;
1087 }
1088 }
1089 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1090
1091 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1092 fWindow->inval();
1093 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001094 return true;
1095}
1096
Brian Osmana109e392017-02-24 09:49:14 -05001097static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1098 const ImVec2& pos, const ImVec2& size) {
1099 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1100 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1101
1102 // Invisible 10x10 button
1103 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1104 ImGui::InvisibleButton(label, ImVec2(10, 10));
1105
1106 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1107 ImGuiIO& io = ImGui::GetIO();
1108 // Normalized mouse position, relative to our gamut box
1109 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1110 // Clamp to edge of box, convert back to primary scale
1111 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1112 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1113 }
1114
1115 if (ImGui::IsItemHovered()) {
1116 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1117 }
1118
1119 // Return screen coordinates for the caller. We could just return center here, but we'd have
1120 // one frame of lag during drag.
1121 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1122}
1123
1124static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1125 ImDrawList* drawList = ImGui::GetWindowDrawList();
1126
1127 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1128 // width, and scale the height to maintain aspect ratio.
1129 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1130 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1131 ImVec2 pos = ImGui::GetCursorScreenPos();
1132
1133 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1134 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1135 // Magic numbers are pixel locations of the origin and upper-right corner.
1136 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1137 ImVec2(242, 61), ImVec2(1897, 1922));
1138 ImVec2 endPos = ImGui::GetCursorPos();
1139
1140 // Primary markers
1141 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1142 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1143 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1144 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1145
1146 // Gamut triangle
1147 drawList->AddCircle(r, 5.0f, 0xFF000040);
1148 drawList->AddCircle(g, 5.0f, 0xFF004000);
1149 drawList->AddCircle(b, 5.0f, 0xFF400000);
1150 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1151 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1152
1153 // Re-position cursor immediate after the diagram for subsequent controls
1154 ImGui::SetCursorPos(endPos);
1155}
1156
Brian Osmand67e5182017-12-08 16:46:09 -05001157void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001158 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1159 if (fShowImGuiTestWindow) {
1160 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1161 }
1162
1163 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001164 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1165 // always visible, we can end up in a layout feedback loop.
1166 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001167 DisplayParams params = fWindow->getRequestedDisplayParams();
1168 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001169 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1170 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001171 if (ImGui::CollapsingHeader("Backend")) {
1172 int newBackend = static_cast<int>(fBackendType);
1173 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1174 ImGui::SameLine();
1175 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001176#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1177 ImGui::SameLine();
1178 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1179#endif
Brian Osman621491e2017-02-28 15:45:01 -05001180#if defined(SK_VULKAN)
1181 ImGui::SameLine();
1182 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1183#endif
1184 if (newBackend != fBackendType) {
1185 fDeferredActions.push_back([=]() {
1186 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1187 });
1188 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001189
Brian Salomon99a33902017-03-07 15:16:34 -05001190 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001191 bool* wire = &params.fGrContextOptions.fWireframeMode;
1192 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1193 paramsChanged = true;
1194 }
Brian Salomon99a33902017-03-07 15:16:34 -05001195
Brian Osman28b12522017-03-08 17:10:24 -05001196 if (ctx) {
1197 int sampleCount = fWindow->sampleCount();
1198 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001199 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001200 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1201 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1202 ImGui::RadioButton("16", &sampleCount, 16);
1203
1204 if (sampleCount != params.fMSAASampleCount) {
1205 params.fMSAASampleCount = sampleCount;
1206 paramsChanged = true;
1207 }
1208 }
1209
Brian Osman8a9de3d2017-03-01 14:59:05 -05001210 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001211 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001212 auto prButton = [&](GpuPathRenderers x) {
1213 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001214 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1215 params.fGrContextOptions.fGpuPathRenderers = x;
1216 paramsChanged = true;
1217 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001218 }
1219 };
1220
1221 if (!ctx) {
1222 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001223 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001224 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001225 prButton(GpuPathRenderers::kAll);
1226 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1227 prButton(GpuPathRenderers::kStencilAndCover);
1228 }
1229 if (ctx->caps()->sampleShadingSupport()) {
1230 prButton(GpuPathRenderers::kMSAA);
1231 }
1232 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001233 prButton(GpuPathRenderers::kNone);
1234 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001235 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001236 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001237 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1238 prButton(GpuPathRenderers::kCoverageCounting);
1239 }
Jim Van Verth83010462017-03-16 08:45:39 -04001240 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001241 prButton(GpuPathRenderers::kTessellating);
1242 prButton(GpuPathRenderers::kNone);
1243 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001244 ImGui::TreePop();
1245 }
Brian Osman621491e2017-02-28 15:45:01 -05001246 }
1247
Brian Osmanfce09c52017-11-14 15:32:20 -05001248 if (fShowSlidePicker) {
1249 ImGui::SetNextTreeNodeOpen(true);
1250 }
1251
Brian Osman79086b92017-02-10 13:36:16 -05001252 if (ImGui::CollapsingHeader("Slide")) {
1253 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001254 static ImVector<const char*> filteredSlideNames;
1255 static ImVector<int> filteredSlideIndices;
1256
Brian Osmanfce09c52017-11-14 15:32:20 -05001257 if (fShowSlidePicker) {
1258 ImGui::SetKeyboardFocusHere();
1259 fShowSlidePicker = false;
1260 }
1261
Brian Osman79086b92017-02-10 13:36:16 -05001262 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001263 filteredSlideNames.clear();
1264 filteredSlideIndices.clear();
1265 int filteredIndex = 0;
1266 for (int i = 0; i < fSlides.count(); ++i) {
1267 const char* slideName = fSlides[i]->getName().c_str();
1268 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1269 if (i == fCurrentSlide) {
1270 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001271 }
Brian Osmanf479e422017-11-08 13:11:36 -05001272 filteredSlideNames.push_back(slideName);
1273 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001274 }
Brian Osman79086b92017-02-10 13:36:16 -05001275 }
Brian Osmanf479e422017-11-08 13:11:36 -05001276
Brian Osmanf479e422017-11-08 13:11:36 -05001277 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1278 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001279 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001280 }
1281 }
Brian Osmana109e392017-02-24 09:49:14 -05001282
1283 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001284 ColorMode newMode = fColorMode;
1285 auto cmButton = [&](ColorMode mode, const char* label) {
1286 if (ImGui::RadioButton(label, mode == fColorMode)) {
1287 newMode = mode;
1288 }
1289 };
1290
1291 cmButton(ColorMode::kLegacy, "Legacy 8888");
1292 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1293 "Color Managed 8888 (Nonlinear blending)");
1294 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1295 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1296
1297 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001298 // It isn't safe to switch color mode now (in the middle of painting). We might
1299 // tear down the back-end, etc... Defer this change until the next onIdle.
1300 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001301 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001302 });
Brian Osmana109e392017-02-24 09:49:14 -05001303 }
1304
1305 // Pick from common gamuts:
1306 int primariesIdx = 4; // Default: Custom
1307 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1308 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1309 primariesIdx = i;
1310 break;
1311 }
1312 }
1313
Brian Osmanfdab5762017-11-09 10:27:55 -05001314 // When we're in xform canvas mode, we can alter the transfer function, too
1315 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1316 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1317 }
1318
Brian Osmana109e392017-02-24 09:49:14 -05001319 if (ImGui::Combo("Primaries", &primariesIdx,
1320 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1321 if (primariesIdx >= 0 && primariesIdx <= 3) {
1322 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1323 }
1324 }
1325
1326 // Allow direct editing of gamut
1327 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1328 }
Brian Osman79086b92017-02-10 13:36:16 -05001329 }
Brian Salomon99a33902017-03-07 15:16:34 -05001330 if (paramsChanged) {
1331 fDeferredActions.push_back([=]() {
1332 fWindow->setRequestedDisplayParams(params);
1333 fWindow->inval();
1334 this->updateTitle();
1335 });
1336 }
Brian Osman79086b92017-02-10 13:36:16 -05001337 ImGui::End();
1338 }
1339
Brian Osmanf6877092017-02-13 09:39:57 -05001340 if (fShowZoomWindow && fLastImage) {
1341 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001342 static int zoomFactor = 8;
1343 if (ImGui::Button("<<")) {
1344 zoomFactor = SkTMax(zoomFactor / 2, 4);
1345 }
1346 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1347 if (ImGui::Button(">>")) {
1348 zoomFactor = SkTMin(zoomFactor * 2, 32);
1349 }
Brian Osmanf6877092017-02-13 09:39:57 -05001350
Brian Osmanf6877092017-02-13 09:39:57 -05001351 ImVec2 mousePos = ImGui::GetMousePos();
1352 ImVec2 avail = ImGui::GetContentRegionAvail();
1353
Brian Osmanead517d2017-11-13 15:36:36 -05001354 uint32_t pixel = 0;
1355 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1356 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1357 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001358 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1359 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1360 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001361 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1362 }
1363
Brian Osmand67e5182017-12-08 16:46:09 -05001364 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001365 // Translate so the region of the image that's under the mouse cursor is centered
1366 // in the zoom canvas:
1367 c->scale(zoomFactor, zoomFactor);
1368 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1369 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1370 c->drawImage(this->fLastImage, 0, 0);
1371
1372 SkPaint outline;
1373 outline.setStyle(SkPaint::kStroke_Style);
1374 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1375 });
Brian Osmanf6877092017-02-13 09:39:57 -05001376 }
1377
1378 ImGui::End();
1379 }
Brian Osman79086b92017-02-10 13:36:16 -05001380}
1381
liyuqian2edb0f42016-07-06 14:11:32 -07001382void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001383 for (int i = 0; i < fDeferredActions.count(); ++i) {
1384 fDeferredActions[i]();
1385 }
1386 fDeferredActions.reset();
1387
Brian Osman56a24812017-12-19 11:15:16 -05001388 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001389 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001390 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001391 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001392
Brian Osman79086b92017-02-10 13:36:16 -05001393 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001394 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001395 fWindow->inval();
1396 }
jvanverth9f372462016-04-06 06:08:59 -07001397}
liyuqiane5a6cd92016-05-27 08:52:52 -07001398
1399void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001400 if (!fWindow) {
1401 return;
1402 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001403 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001404 return; // Surface hasn't been created yet.
1405 }
1406
liyuqianb73c24b2016-06-03 08:47:23 -07001407 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001408 Json::Value slideState(Json::objectValue);
1409 slideState[kName] = kSlideStateName;
1410 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001411 if (fAllSlideNames.size() == 0) {
1412 for(auto slide : fSlides) {
1413 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1414 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001415 }
liyuqian1f508fd2016-06-07 06:57:40 -07001416 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001417
liyuqianb73c24b2016-06-03 08:47:23 -07001418 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001419 Json::Value backendState(Json::objectValue);
1420 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001421 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001422 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001423 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001424 backendState[kOptions].append(Json::Value(str));
1425 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001426
csmartdalton578f0642017-02-24 16:04:47 -07001427 // MSAA state
1428 Json::Value msaaState(Json::objectValue);
1429 msaaState[kName] = kMSAAStateName;
1430 msaaState[kValue] = fWindow->sampleCount();
1431 msaaState[kOptions] = Json::Value(Json::arrayValue);
1432 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1433 msaaState[kOptions].append(Json::Value(0));
1434 } else {
1435 for (int msaa : {0, 4, 8, 16}) {
1436 msaaState[kOptions].append(Json::Value(msaa));
1437 }
1438 }
1439
csmartdalton61cd31a2017-02-27 17:00:53 -07001440 // Path renderer state
1441 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1442 Json::Value prState(Json::objectValue);
1443 prState[kName] = kPathRendererStateName;
1444 prState[kValue] = gPathRendererNames[pr];
1445 prState[kOptions] = Json::Value(Json::arrayValue);
1446 const GrContext* ctx = fWindow->getGrContext();
1447 if (!ctx) {
1448 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001449 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001450 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001451 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1452 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1453 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1454 }
1455 if (ctx->caps()->sampleShadingSupport()) {
1456 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1457 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001458 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001459 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1460 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001461 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001462 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001463 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1464 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1465 }
Jim Van Verth83010462017-03-16 08:45:39 -04001466 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001467 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001468 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1469 }
1470
liyuqianb73c24b2016-06-03 08:47:23 -07001471 // Softkey state
1472 Json::Value softkeyState(Json::objectValue);
1473 softkeyState[kName] = kSoftkeyStateName;
1474 softkeyState[kValue] = kSoftkeyHint;
1475 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1476 softkeyState[kOptions].append(kSoftkeyHint);
1477 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1478 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1479 }
1480
liyuqian1f508fd2016-06-07 06:57:40 -07001481 // FPS state
1482 Json::Value fpsState(Json::objectValue);
1483 fpsState[kName] = kFpsStateName;
Brian Osman56a24812017-12-19 11:15:16 -05001484 double animTime = fStatsLayer.getLastTime(fAnimateTimer);
1485 double paintTime = fStatsLayer.getLastTime(fPaintTimer);
1486 double flushTime = fStatsLayer.getLastTime(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001487 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
Brian Osman56a24812017-12-19 11:15:16 -05001488 animTime + paintTime + flushTime,
1489 animTime, paintTime, flushTime).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001490 fpsState[kOptions] = Json::Value(Json::arrayValue);
1491
liyuqiane5a6cd92016-05-27 08:52:52 -07001492 Json::Value state(Json::arrayValue);
1493 state.append(slideState);
1494 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001495 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001496 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001497 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001498 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001499
Brian Osmaneff04b52017-11-21 13:18:02 -05001500 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001501}
1502
1503void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001504 // For those who will add more features to handle the state change in this function:
1505 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1506 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1507 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001508 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001509 for (int i = 0; i < fSlides.count(); ++i) {
1510 if (fSlides[i]->getName().equals(stateValue)) {
1511 this->setCurrentSlide(i);
1512 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001513 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001514 }
Florin Malitaab99c342018-01-16 16:23:03 -05001515
1516 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001517 } else if (stateName.equals(kBackendStateName)) {
1518 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1519 if (stateValue.equals(kBackendTypeStrings[i])) {
1520 if (fBackendType != i) {
1521 fBackendType = (sk_app::Window::BackendType)i;
1522 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001523 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001524 }
1525 break;
1526 }
1527 }
csmartdalton578f0642017-02-24 16:04:47 -07001528 } else if (stateName.equals(kMSAAStateName)) {
1529 DisplayParams params = fWindow->getRequestedDisplayParams();
1530 int sampleCount = atoi(stateValue.c_str());
1531 if (sampleCount != params.fMSAASampleCount) {
1532 params.fMSAASampleCount = sampleCount;
1533 fWindow->setRequestedDisplayParams(params);
1534 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001535 this->updateTitle();
1536 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001537 }
1538 } else if (stateName.equals(kPathRendererStateName)) {
1539 DisplayParams params = fWindow->getRequestedDisplayParams();
1540 for (const auto& pair : gPathRendererNames) {
1541 if (pair.second == stateValue.c_str()) {
1542 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1543 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1544 fWindow->setRequestedDisplayParams(params);
1545 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001546 this->updateTitle();
1547 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001548 }
1549 break;
1550 }
csmartdalton578f0642017-02-24 16:04:47 -07001551 }
liyuqianb73c24b2016-06-03 08:47:23 -07001552 } else if (stateName.equals(kSoftkeyStateName)) {
1553 if (!stateValue.equals(kSoftkeyHint)) {
1554 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001555 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001556 }
liyuqian2edb0f42016-07-06 14:11:32 -07001557 } else if (stateName.equals(kRefreshStateName)) {
1558 // This state is actually NOT in the UI state.
1559 // We use this to allow Android to quickly set bool fRefresh.
1560 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001561 } else {
1562 SkDebugf("Unknown stateName: %s", stateName.c_str());
1563 }
1564}
Brian Osman79086b92017-02-10 13:36:16 -05001565
1566bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001567 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001568}
1569
1570bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001571 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001572 fWindow->inval();
1573 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001574 } else {
1575 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001576 }
Brian Osman79086b92017-02-10 13:36:16 -05001577}