blob: 9b6a89adf16a9b3e87d8db843629b87f7cda6d2b [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"
Florin Malitac659c2c2018-04-05 11:57:21 -040018#include "SvgSlide.h"
jvanverth9f372462016-04-06 06:08:59 -070019
csmartdalton61cd31a2017-02-27 17:00:53 -070020#include "GrContext.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070021#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050022#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040023#include "SkColorSpaceXformCanvas.h"
Ben Wagner483c7722018-02-20 17:06:07 -050024#include "SkCommonFlags.h"
Brian Osman2dd96932016-10-18 15:33:53 -040025#include "SkCommandLineFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070026#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040027#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050028#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040029#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050030#include "SkImagePriv.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070031#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050032#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050033#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050034#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040035#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070036#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070037#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050038#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050039#include "SkTestFontMgr.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050040#include "SkThreadedBMPDevice.h"
jvanverth9f372462016-04-06 06:08:59 -070041
Brian Osman79086b92017-02-10 13:36:16 -050042#include "imgui.h"
43
Chris Dalton1a325d22017-07-14 15:17:41 -060044#include "ccpr/GrCoverageCountingPathRenderer.h"
45
csmartdalton578f0642017-02-24 16:04:47 -070046#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070047#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070048
jvanverth34524262016-05-04 13:49:13 -070049using namespace sk_app;
50
csmartdalton61cd31a2017-02-27 17:00:53 -070051static std::map<GpuPathRenderers, std::string> gPathRendererNames;
52
jvanverth9f372462016-04-06 06:08:59 -070053Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070054 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070055}
56
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060057static DEFINE_string(slide, "", "Start on this sample.");
58static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050059
bsalomon6c471f72016-07-26 12:56:32 -070060#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070061# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070062#else
63# define BACKENDS_STR "\"sw\" and \"gl\""
64#endif
65
Brian Osman2dd96932016-10-18 15:33:53 -040066static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070067
Brian Salomonbdecacf2018-02-02 20:32:49 -050068static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070069
Chris Dalton2d18f412018-02-20 13:23:32 -070070DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
71
Brian Osman53136aa2017-07-20 15:43:35 -040072DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050073
Brian Salomon194db172017-08-17 14:37:06 -040074const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070075 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040076#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
77 "ANGLE",
78#endif
jvanverth063ece72016-06-17 09:29:14 -070079#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070080 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070081#endif
csmartdalton578f0642017-02-24 16:04:47 -070082 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070083};
84
bsalomon6c471f72016-07-26 12:56:32 -070085static sk_app::Window::BackendType get_backend_type(const char* str) {
86#ifdef SK_VULKAN
87 if (0 == strcmp(str, "vk")) {
88 return sk_app::Window::kVulkan_BackendType;
89 } else
90#endif
Brian Salomon194db172017-08-17 14:37:06 -040091#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
92 if (0 == strcmp(str, "angle")) {
93 return sk_app::Window::kANGLE_BackendType;
94 } else
95#endif
bsalomon6c471f72016-07-26 12:56:32 -070096 if (0 == strcmp(str, "gl")) {
97 return sk_app::Window::kNativeGL_BackendType;
98 } else if (0 == strcmp(str, "sw")) {
99 return sk_app::Window::kRaster_BackendType;
100 } else {
101 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
102 return sk_app::Window::kRaster_BackendType;
103 }
104}
105
Brian Osmana109e392017-02-24 09:49:14 -0500106static SkColorSpacePrimaries gSrgbPrimaries = {
107 0.64f, 0.33f,
108 0.30f, 0.60f,
109 0.15f, 0.06f,
110 0.3127f, 0.3290f };
111
112static SkColorSpacePrimaries gAdobePrimaries = {
113 0.64f, 0.33f,
114 0.21f, 0.71f,
115 0.15f, 0.06f,
116 0.3127f, 0.3290f };
117
118static SkColorSpacePrimaries gP3Primaries = {
119 0.680f, 0.320f,
120 0.265f, 0.690f,
121 0.150f, 0.060f,
122 0.3127f, 0.3290f };
123
124static SkColorSpacePrimaries gRec2020Primaries = {
125 0.708f, 0.292f,
126 0.170f, 0.797f,
127 0.131f, 0.046f,
128 0.3127f, 0.3290f };
129
130struct NamedPrimaries {
131 const char* fName;
132 SkColorSpacePrimaries* fPrimaries;
133} gNamedPrimaries[] = {
134 { "sRGB", &gSrgbPrimaries },
135 { "AdobeRGB", &gAdobePrimaries },
136 { "P3", &gP3Primaries },
137 { "Rec. 2020", &gRec2020Primaries },
138};
139
140static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
141 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
142}
143
Brian Osman70d2f432017-11-08 09:54:10 -0500144static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
145 // In raster mode, we still use GL for the window.
146 // This lets us render the GUI faster (and correct).
147 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
148}
149
liyuqiane5a6cd92016-05-27 08:52:52 -0700150const char* kName = "name";
151const char* kValue = "value";
152const char* kOptions = "options";
153const char* kSlideStateName = "Slide";
154const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700155const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700156const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700157const char* kSoftkeyStateName = "Softkey";
158const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700159const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700160const char* kON = "ON";
161const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700162const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700163
jvanverth34524262016-05-04 13:49:13 -0700164Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500165 : fCurrentSlide(-1)
166 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500167 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500168 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500169 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500170 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500171 , fShowZoomWindow(false)
172 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700173 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500174 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500175 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500176 // Our UI can only tweak gamma (currently), so start out gamma-only
177 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700178 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400179 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500180 , fTileCnt(0)
181 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700182{
Greg Daniel285db442016-10-14 09:12:53 -0400183 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700184
Brian Osmanf09e35e2017-12-15 14:48:09 -0500185 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
186 gPathRendererNames[GpuPathRenderers::kDefault] =
187 "Default Ganesh Behavior (best path renderer, not including CCPR)";
188 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
189 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
190 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
191 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
192 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
193 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700194
jvanverth2bb3b6d2016-04-08 07:24:09 -0700195 SkDebugf("Command line arguments: ");
196 for (int i = 1; i < argc; ++i) {
197 SkDebugf("%s ", argv[i]);
198 }
199 SkDebugf("\n");
200
201 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500202#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400203 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500204#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700205
Ben Wagner483c7722018-02-20 17:06:07 -0500206 if (!FLAGS_nativeFonts) {
207 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
208 }
209
Brian Osmanbc8150f2017-07-24 11:38:01 -0400210 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400211 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400212
bsalomon6c471f72016-07-26 12:56:32 -0700213 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700214 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700215
csmartdalton578f0642017-02-24 16:04:47 -0700216 DisplayParams displayParams;
217 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700218 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700219 fWindow->setRequestedDisplayParams(displayParams);
220
Brian Osman56a24812017-12-19 11:15:16 -0500221 // Configure timers
222 fStatsLayer.setActive(false);
223 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
224 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
225 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
226
jvanverth9f372462016-04-06 06:08:59 -0700227 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700228 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500229 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500230 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500231 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700232
brianosman622c8d52016-05-10 06:50:49 -0700233 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500234 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
235 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
236 fWindow->inval();
237 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500238 // Command to jump directly to the slide picker and give it focus
239 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
240 this->fShowImGuiDebugWindow = true;
241 this->fShowSlidePicker = true;
242 fWindow->inval();
243 });
244 // Alias that to Backspace, to match SampleApp
245 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
246 this->fShowImGuiDebugWindow = true;
247 this->fShowSlidePicker = true;
248 fWindow->inval();
249 });
Brian Osman79086b92017-02-10 13:36:16 -0500250 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
251 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
252 fWindow->inval();
253 });
Brian Osmanf6877092017-02-13 09:39:57 -0500254 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
255 this->fShowZoomWindow = !this->fShowZoomWindow;
256 fWindow->inval();
257 });
brianosman622c8d52016-05-10 06:50:49 -0700258 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500259 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700260 fWindow->inval();
261 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400262 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500263 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400264 this->updateTitle();
265 fWindow->inval();
266 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500267 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500268 switch (fColorMode) {
269 case ColorMode::kLegacy:
270 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
271 break;
272 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
273 this->setColorMode(ColorMode::kColorManagedSRGB8888);
274 break;
275 case ColorMode::kColorManagedSRGB8888:
276 this->setColorMode(ColorMode::kColorManagedLinearF16);
277 break;
278 case ColorMode::kColorManagedLinearF16:
279 this->setColorMode(ColorMode::kLegacy);
280 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500281 }
brianosman622c8d52016-05-10 06:50:49 -0700282 });
283 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500284 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700285 });
286 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500287 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700288 });
289 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
290 this->changeZoomLevel(1.f / 32.f);
291 fWindow->inval();
292 });
293 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
294 this->changeZoomLevel(-1.f / 32.f);
295 fWindow->inval();
296 });
jvanverthaf236b52016-05-20 06:01:06 -0700297 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400298 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
299 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500300 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400301#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
302 if (newBackend == sk_app::Window::kVulkan_BackendType) {
303 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
304 sk_app::Window::kBackendTypeCount);
305 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
306 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500307 }
308#endif
Brian Osman621491e2017-02-28 15:45:01 -0500309 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700310 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500311 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
312 fTileCnt++;
313 if (fThreadCnt == 0) {
314 this->resetExecutor();
315 }
316 this->updateTitle();
317 fWindow->inval();
318 });
319 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
320 fTileCnt = SkTMax(0, fTileCnt - 1);
321 if (fThreadCnt == 0) {
322 this->resetExecutor();
323 }
324 this->updateTitle();
325 fWindow->inval();
326 });
327 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
328 if (fTileCnt == 0) {
329 return;
330 }
331 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
332 this->resetExecutor();
333 this->updateTitle();
334 fWindow->inval();
335 });
336 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
337 if (fTileCnt == 0) {
338 return;
339 }
340 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
341 this->resetExecutor();
342 this->updateTitle();
343 fWindow->inval();
344 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500345 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
346 fSaveToSKP = true;
347 fWindow->inval();
348 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500349 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
350 if (!fPaintOverrides.fHinting) {
351 fPaintOverrides.fHinting = true;
352 fPaint.setHinting(SkPaint::kNo_Hinting);
353 } else {
354 switch (fPaint.getHinting()) {
355 case SkPaint::kNo_Hinting:
356 fPaint.setHinting(SkPaint::kSlight_Hinting);
357 break;
358 case SkPaint::kSlight_Hinting:
359 fPaint.setHinting(SkPaint::kNormal_Hinting);
360 break;
361 case SkPaint::kNormal_Hinting:
362 fPaint.setHinting(SkPaint::kFull_Hinting);
363 break;
364 case SkPaint::kFull_Hinting:
365 fPaint.setHinting(SkPaint::kNo_Hinting);
366 fPaintOverrides.fHinting = false;
367 break;
368 }
369 }
370 this->updateTitle();
371 fWindow->inval();
372 });
373 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
374 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
375 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
376 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
377 fPaint.setAntiAlias(false);
378 fPaintOverrides.fOriginalSkUseAnalyticAA = gSkUseAnalyticAA;
379 fPaintOverrides.fOriginalSkForceAnalyticAA = gSkForceAnalyticAA;
380 fPaintOverrides.fOriginalSkUseDeltaAA = gSkUseDeltaAA;
381 fPaintOverrides.fOriginalSkForceDeltaAA = gSkForceDeltaAA;
382 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
383 gSkUseDeltaAA = gSkForceDeltaAA = false;
384 } else {
385 fPaint.setAntiAlias(true);
386 switch (fPaintOverrides.fAntiAlias) {
387 case SkPaintFields::AntiAliasState::Alias:
388 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
389 break;
390 case SkPaintFields::AntiAliasState::Normal:
391 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
392 gSkUseDeltaAA = gSkForceDeltaAA = false;
393 gSkUseAnalyticAA = true;
394 break;
395 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
396 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
397 gSkForceAnalyticAA = true;
398 break;
399 case SkPaintFields::AntiAliasState::AnalyticAAForced:
400 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
401 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
402 gSkUseDeltaAA = true;
403 break;
404 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
405 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
406 gSkForceDeltaAA = true;
407 break;
408 case SkPaintFields::AntiAliasState::DeltaAAForced:
409 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
410 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
411 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
412 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
413 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
414 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
415 break;
416 }
417 }
418 this->updateTitle();
419 fWindow->inval();
420 });
421 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
422 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
423 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
424 fPaint.setLCDRenderText(false);
425 } else {
426 if (!fPaint.isLCDRenderText()) {
427 fPaint.setLCDRenderText(true);
428 } else {
429 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
430 }
431 }
432 this->updateTitle();
433 fWindow->inval();
434 });
435 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
436 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
437 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
438 fPaint.setSubpixelText(false);
439 } else {
440 if (!fPaint.isSubpixelText()) {
441 fPaint.setSubpixelText(true);
442 } else {
443 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
444 }
445 }
446 this->updateTitle();
447 fWindow->inval();
448 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500449
jvanverth2bb3b6d2016-04-08 07:24:09 -0700450 // set up slides
451 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500452 if (FLAGS_list) {
453 this->listNames();
454 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700455
djsollen12d62a72016-04-21 07:59:44 -0700456 fAnimTimer.run();
457
Hal Canaryc465d132017-12-08 10:21:31 -0500458 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500459 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400460 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500461 }
462 fImGuiGamutPaint.setColor(SK_ColorWHITE);
463 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
464
Brian Osman70d2f432017-11-08 09:54:10 -0500465 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500466 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700467}
468
jvanverth34524262016-05-04 13:49:13 -0700469void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700470 fAllSlideNames = Json::Value(Json::arrayValue);
471
Chris Dalton2d18f412018-02-20 13:23:32 -0700472 // Bisect slide.
473 if (!FLAGS_bisect.isEmpty()) {
474 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
475 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
476 if (FLAGS_bisect.count() >= 2) {
477 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
478 bisect->onChar(*ch);
479 }
480 }
481 fSlides.push_back(std::move(bisect));
482 }
483 }
484
485 // GMs
486 int firstGM = fSlides.count();
jvanverth2bb3b6d2016-04-08 07:24:09 -0700487 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
488 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400489 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700490
491 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
492 sk_sp<Slide> slide(new GMSlide(gm.release()));
Chris Dalton2d18f412018-02-20 13:23:32 -0700493 fSlides.push_back(std::move(slide));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700494 }
495
496 gms = gms->next();
497 }
Chris Dalton2d18f412018-02-20 13:23:32 -0700498 // reverse gms
499 int numGMs = fSlides.count() - firstGM;
500 for (int i = 0; i < numGMs/2; ++i) {
501 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
jvanverth2bb3b6d2016-04-08 07:24:09 -0700502 }
503
jvanverthc7027ab2016-06-16 09:52:35 -0700504 // samples
505 const SkViewRegister* reg = SkViewRegister::Head();
506 while (reg) {
507 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700508 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
509 fSlides.push_back(slide);
510 }
jvanverthc7027ab2016-06-16 09:52:35 -0700511 reg = reg->next();
512 }
513
Florin Malita0ffa3222018-04-05 14:34:45 -0400514 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
515 static const struct {
516 const char* fExtension;
517 const char* fDirName;
518 const SkCommandLineFlags::StringArray& fFlags;
519 const SlideFactory fFactory;
520 } gExternalSlidesInfo[] = {
521 { ".skp", "skp-dir", FLAGS_skps,
522 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
523 return sk_make_sp<SKPSlide>(name, path);}
524 },
525 { ".jpg", "jpg-dir", FLAGS_jpgs,
526 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
527 return sk_make_sp<ImageSlide>(name, path);}
528 },
529 { ".json", "skottie-dir", FLAGS_jsons,
530 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
531 return sk_make_sp<SkottieSlide>(name, path);}
532 },
533 { ".svg", "svg-dir", FLAGS_svgs,
534 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
535 return sk_make_sp<SvgSlide>(name, path);}
536 },
537 };
jvanverthc265a922016-04-08 12:51:45 -0700538
Florin Malita0ffa3222018-04-05 14:34:45 -0400539 SkTArray<sk_sp<Slide>, true> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700540
Florin Malita0ffa3222018-04-05 14:34:45 -0400541 const auto addSlide = [&](const SkString& name,
542 const SkString& path,
543 const SlideFactory& fact) {
544 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
545 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700546 }
liyuqian6f163d22016-06-13 12:26:45 -0700547
Florin Malita0ffa3222018-04-05 14:34:45 -0400548 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500549 dirSlides.push_back(slide);
550 fSlides.push_back(std::move(slide));
551 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400552 };
Florin Malita76a076b2018-02-15 18:40:48 -0500553
Florin Malita0ffa3222018-04-05 14:34:45 -0400554 for (const auto& info : gExternalSlidesInfo) {
555 for (const auto& flag : info.fFlags) {
556 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
557 // single file
558 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
559 } else {
560 // directory
561 SkOSFile::Iter it(flag.c_str(), info.fExtension);
562 SkString name;
563 while (it.next(&name)) {
564 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
565 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400566 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400567
568 if (!dirSlides.empty()) {
569 fSlides.push_back(
570 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
571 std::move(dirSlides)));
572 dirSlides.reset();
573 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400574 }
575 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700576}
577
578
jvanverth34524262016-05-04 13:49:13 -0700579Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700580 fWindow->detach();
581 delete fWindow;
582}
583
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500584struct SkPaintTitleUpdater {
585 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
586 void append(const char* s) {
587 if (fCount == 0) {
588 fTitle->append(" {");
589 } else {
590 fTitle->append(", ");
591 }
592 fTitle->append(s);
593 ++fCount;
594 }
595 void done() {
596 if (fCount > 0) {
597 fTitle->append("}");
598 }
599 }
600 SkString* fTitle;
601 int fCount;
602};
603
brianosman05de2162016-05-06 13:28:57 -0700604void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700605 if (!fWindow) {
606 return;
607 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500608 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700609 return; // Surface hasn't been created yet.
610 }
611
jvanverth34524262016-05-04 13:49:13 -0700612 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700613 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700614
Yuqian Li399b3c22017-08-03 11:08:15 -0400615 if (gSkUseDeltaAA) {
616 if (gSkForceDeltaAA) {
617 title.append(" <FDAA>");
618 } else {
619 title.append(" <DAA>");
620 }
621 } else if (gSkUseAnalyticAA) {
622 if (gSkForceAnalyticAA) {
623 title.append(" <FAAA>");
624 } else {
625 title.append(" <AAA>");
626 }
627 }
628
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500629 SkPaintTitleUpdater paintTitle(&title);
630 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
631 if (fPaint.isAntiAlias()) {
632 paintTitle.append("Antialias");
633 } else {
634 paintTitle.append("Alias");
635 }
636 }
637 if (fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag) {
638 if (fPaint.isLCDRenderText()) {
639 paintTitle.append("LCD");
640 } else {
641 paintTitle.append("lcd");
642 }
643 }
644 if (fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag) {
645 if (fPaint.isSubpixelText()) {
646 paintTitle.append("Subpixel Glyphs");
647 } else {
648 paintTitle.append("Pixel Glyphs");
649 }
650 }
651 if (fPaintOverrides.fHinting) {
652 switch (fPaint.getHinting()) {
653 case SkPaint::kNo_Hinting:
654 paintTitle.append("No Hinting");
655 break;
656 case SkPaint::kSlight_Hinting:
657 paintTitle.append("Slight Hinting");
658 break;
659 case SkPaint::kNormal_Hinting:
660 paintTitle.append("Normal Hinting");
661 break;
662 case SkPaint::kFull_Hinting:
663 paintTitle.append("Full Hinting");
664 break;
665 }
666 }
667 paintTitle.done();
668
Yuqian Lib2ba6642017-11-22 12:07:41 -0500669 if (fTileCnt > 0) {
670 title.appendf(" T%d", fTileCnt);
671 if (fThreadCnt > 0) {
672 title.appendf("/%d", fThreadCnt);
673 }
674 }
675
Brian Osman92004802017-03-06 11:47:26 -0500676 switch (fColorMode) {
677 case ColorMode::kLegacy:
678 title.append(" Legacy 8888");
679 break;
680 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
681 title.append(" ColorManaged 8888 (Nonlinear blending)");
682 break;
683 case ColorMode::kColorManagedSRGB8888:
684 title.append(" ColorManaged 8888");
685 break;
686 case ColorMode::kColorManagedLinearF16:
687 title.append(" ColorManaged F16");
688 break;
689 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500690
Brian Osman92004802017-03-06 11:47:26 -0500691 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500692 int curPrimaries = -1;
693 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
694 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
695 curPrimaries = i;
696 break;
697 }
698 }
699 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500700
701 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
702 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
703 }
brianosman05de2162016-05-06 13:28:57 -0700704 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500705
csmartdalton578f0642017-02-24 16:04:47 -0700706 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700707 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500708 int msaa = fWindow->sampleCount();
709 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700710 title.appendf(" MSAA: %i", msaa);
711 }
712 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700713
714 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400715 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700716 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
717 }
718
brianosman05de2162016-05-06 13:28:57 -0700719 fWindow->setTitle(title.c_str());
720}
721
Florin Malitaab99c342018-01-16 16:23:03 -0500722int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500723
724 if (!FLAGS_slide.isEmpty()) {
725 int count = fSlides.count();
726 for (int i = 0; i < count; i++) {
727 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500728 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500729 }
730 }
731
732 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
733 this->listNames();
734 }
735
Florin Malitaab99c342018-01-16 16:23:03 -0500736 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500737}
738
Florin Malitaab99c342018-01-16 16:23:03 -0500739void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500740 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500741 for (const auto& slide : fSlides) {
742 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500743 }
744}
745
Florin Malitaab99c342018-01-16 16:23:03 -0500746void Viewer::setCurrentSlide(int slide) {
747 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700748
Florin Malitaab99c342018-01-16 16:23:03 -0500749 if (slide == fCurrentSlide) {
750 return;
751 }
752
753 if (fCurrentSlide >= 0) {
754 fSlides[fCurrentSlide]->unload();
755 }
756
757 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
758 SkIntToScalar(fWindow->height()));
759 fCurrentSlide = slide;
760 this->setupCurrentSlide();
761}
762
763void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500764 if (fCurrentSlide >= 0) {
765 // prepare dimensions for image slides
766 fGesture.resetTouchState();
767 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700768
Jim Van Verth0848fb02018-01-22 13:39:30 -0500769 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
770 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
771 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400772
Jim Van Verth0848fb02018-01-22 13:39:30 -0500773 // Start with a matrix that scales the slide to the available screen space
774 if (fWindow->scaleContentToFit()) {
775 if (windowRect.width() > 0 && windowRect.height() > 0) {
776 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
777 }
liyuqiane46e4f02016-05-20 07:32:19 -0700778 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500779
780 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400781 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500782
783 this->updateTitle();
784 this->updateUIState();
785
786 fStatsLayer.resetMeasurements();
787
788 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700789 }
jvanverthc265a922016-04-08 12:51:45 -0700790}
791
792#define MAX_ZOOM_LEVEL 8
793#define MIN_ZOOM_LEVEL -8
794
jvanverth34524262016-05-04 13:49:13 -0700795void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700796 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400797 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Yuqian Li755778c2018-03-28 16:23:31 -0400798
799 // Update the trans limit as the zoom level changes.
800 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
801 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
802 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
803 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
804}
805
806SkMatrix Viewer::computePreTouchMatrix() {
807 SkMatrix m = fDefaultMatrix;
808 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
809 : SK_Scalar1 + fZoomLevel;
810 m.preScale(zoomScale, zoomScale);
811 return m;
jvanverthc265a922016-04-08 12:51:45 -0700812}
813
liyuqiand3cdbca2016-05-17 12:44:20 -0700814SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400815 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700816 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400817 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700818 return m;
jvanverthc265a922016-04-08 12:51:45 -0700819}
820
Brian Osman621491e2017-02-28 15:45:01 -0500821void Viewer::setBackend(sk_app::Window::BackendType backendType) {
822 fBackendType = backendType;
823
824 fWindow->detach();
825
Brian Osman70d2f432017-11-08 09:54:10 -0500826#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400827 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
828 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500829 DisplayParams params = fWindow->getRequestedDisplayParams();
830 delete fWindow;
831 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500832
Brian Osman70d2f432017-11-08 09:54:10 -0500833 // re-register callbacks
834 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500835 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500836 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500837 fWindow->pushLayer(&fImGuiLayer);
838
Brian Osman70d2f432017-11-08 09:54:10 -0500839 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
840 // will still include our correct sample count. But the re-created fWindow will lose that
841 // information. On Windows, we need to re-create the window when changing sample count,
842 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
843 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
844 // as if we had never changed windows at all).
845 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500846#endif
847
Brian Osman70d2f432017-11-08 09:54:10 -0500848 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500849}
850
Brian Osman92004802017-03-06 11:47:26 -0500851void Viewer::setColorMode(ColorMode colorMode) {
852 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700853
Brian Osmanf750fbc2017-02-08 10:47:28 -0500854 // 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 -0400855 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700856 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500857 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400858 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
859 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500860 if (wasInLegacy != wantLegacy) {
861 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700862 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500863 }
864
865 this->updateTitle();
866 fWindow->inval();
867}
868
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500869class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
870public:
871 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
872 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
873 { }
874 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -0400875 if (*paint == nullptr) {
876 return true;
877 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500878 if (fPaintOverrides->fHinting) {
879 paint->writable()->setHinting(fPaint->getHinting());
880 }
881 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
882 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
883 }
884 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
885 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
886 }
887 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
888 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
889 }
890 return true;
891 }
892 SkPaint* fPaint;
893 Viewer::SkPaintFields* fPaintOverrides;
894};
895
Brian Osmanf750fbc2017-02-08 10:47:28 -0500896void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500897 SkAutoCanvasRestore autorestore(canvas, false);
898
Brian Osmanf750fbc2017-02-08 10:47:28 -0500899 // By default, we render directly into the window's surface/canvas
900 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500901 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700902
Brian Osmane0d4fba2017-03-15 10:24:55 -0400903 // If we're in any of the color managed modes, construct the color space we're going to use
904 sk_sp<SkColorSpace> cs = nullptr;
905 if (ColorMode::kLegacy != fColorMode) {
906 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
907 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400908 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400909 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500910 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
911 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
912 } else {
913 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
914 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400915 }
916
Brian Osman3ac99cf2017-12-01 11:23:53 -0500917 if (fSaveToSKP) {
918 SkPictureRecorder recorder;
919 SkCanvas* recorderCanvas = recorder.beginRecording(
920 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
921 // In xform-canvas mode, record the transformed output
922 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
923 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
924 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
925 recorderCanvas = xformCanvas.get();
926 }
927 fSlides[fCurrentSlide]->draw(recorderCanvas);
928 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
929 SkFILEWStream stream("sample_app.skp");
930 picture->serialize(&stream);
931 fSaveToSKP = false;
932 }
933
Brian Osmane0d4fba2017-03-15 10:24:55 -0400934 // 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 -0500935 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
936 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500937 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -0500938 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
939 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -0500940 if (Window::kRaster_BackendType == fBackendType ||
941 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500942 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400943 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
944 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
945
Brian Osman92004802017-03-06 11:47:26 -0500946 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
947 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400948 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
949 // with a special canvas (below) that has the color space attached
950 sk_sp<SkColorSpace> offscreenColorSpace =
951 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500952 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400953 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500954 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
955 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -0500956 SkPixmap offscreenPixmap;
957 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
958 SkBitmap offscreenBitmap;
959 offscreenBitmap.installPixels(offscreenPixmap);
960 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
961 fThreadCnt, fExecutor.get()));
962 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
963 slideCanvas = threadedCanvas.get();
964 } else {
965 slideCanvas = offscreenSurface->getCanvas();
966 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500967 }
968
Brian Osmane0d4fba2017-03-15 10:24:55 -0400969 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
970 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
971 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
972 slideCanvas = xformCanvas.get();
973 }
974
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500975 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500976 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500977 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500978 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -0500979 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500980 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
981 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -0500982 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500983 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500984
985 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -0500986 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -0500987 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -0500988 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500989
990 // If we rendered offscreen, snap an image and push the results to the window's canvas
991 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500992 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500993
994 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400995 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500996 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400997 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500998 SkPaint paint;
999 paint.setBlendMode(SkBlendMode::kSrc);
1000 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -07001001 }
liyuqian6f163d22016-06-13 12:26:45 -07001002}
1003
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001004void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001005 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001006 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001007}
Jim Van Verth6f449692017-02-14 15:16:46 -05001008
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001009void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001010 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001011
brianosman622c8d52016-05-10 06:50:49 -07001012 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001013
Brian Osmand67e5182017-12-08 16:46:09 -05001014 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -05001015
Brian Osman1df161a2017-02-09 12:10:20 -05001016 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -04001017 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001018}
1019
Florin Malitacefc1b92018-02-19 21:43:47 -05001020SkPoint Viewer::mapEvent(float x, float y) {
1021 const auto m = this->computeMatrix();
1022 SkMatrix inv;
1023
1024 SkAssertResult(m.invert(&inv));
1025
1026 return inv.mapXY(x, y);
1027}
1028
jvanverth814e38d2016-06-06 08:48:47 -07001029bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001030 if (GestureDevice::kMouse == fGestureDevice) {
1031 return false;
1032 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001033
1034 const auto slidePt = this->mapEvent(x, y);
1035 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1036 fWindow->inval();
1037 return true;
1038 }
1039
liyuqiand3cdbca2016-05-17 12:44:20 -07001040 void* castedOwner = reinterpret_cast<void*>(owner);
1041 switch (state) {
1042 case Window::kUp_InputState: {
1043 fGesture.touchEnd(castedOwner);
1044 break;
1045 }
1046 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001047 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001048 break;
1049 }
1050 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001051 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001052 break;
1053 }
1054 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001055 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001056 fWindow->inval();
1057 return true;
1058}
1059
Brian Osman80fc07e2017-12-08 16:45:43 -05001060bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001061 if (GestureDevice::kTouch == fGestureDevice) {
1062 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001063 }
Brian Osman16c81a12017-12-20 11:58:34 -05001064
Florin Malitacefc1b92018-02-19 21:43:47 -05001065 const auto slidePt = this->mapEvent(x, y);
1066 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1067 fWindow->inval();
1068 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001069 }
1070
1071 switch (state) {
1072 case Window::kUp_InputState: {
1073 fGesture.touchEnd(nullptr);
1074 break;
1075 }
1076 case Window::kDown_InputState: {
1077 fGesture.touchBegin(nullptr, x, y);
1078 break;
1079 }
1080 case Window::kMove_InputState: {
1081 fGesture.touchMoved(nullptr, x, y);
1082 break;
1083 }
1084 }
1085 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1086
1087 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1088 fWindow->inval();
1089 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001090 return true;
1091}
1092
Brian Osmana109e392017-02-24 09:49:14 -05001093static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1094 const ImVec2& pos, const ImVec2& size) {
1095 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1096 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1097
1098 // Invisible 10x10 button
1099 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1100 ImGui::InvisibleButton(label, ImVec2(10, 10));
1101
1102 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1103 ImGuiIO& io = ImGui::GetIO();
1104 // Normalized mouse position, relative to our gamut box
1105 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1106 // Clamp to edge of box, convert back to primary scale
1107 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1108 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1109 }
1110
1111 if (ImGui::IsItemHovered()) {
1112 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1113 }
1114
1115 // Return screen coordinates for the caller. We could just return center here, but we'd have
1116 // one frame of lag during drag.
1117 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1118}
1119
1120static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1121 ImDrawList* drawList = ImGui::GetWindowDrawList();
1122
1123 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1124 // width, and scale the height to maintain aspect ratio.
1125 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1126 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1127 ImVec2 pos = ImGui::GetCursorScreenPos();
1128
1129 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1130 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1131 // Magic numbers are pixel locations of the origin and upper-right corner.
1132 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1133 ImVec2(242, 61), ImVec2(1897, 1922));
1134 ImVec2 endPos = ImGui::GetCursorPos();
1135
1136 // Primary markers
1137 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1138 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1139 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1140 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1141
1142 // Gamut triangle
1143 drawList->AddCircle(r, 5.0f, 0xFF000040);
1144 drawList->AddCircle(g, 5.0f, 0xFF004000);
1145 drawList->AddCircle(b, 5.0f, 0xFF400000);
1146 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1147 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1148
1149 // Re-position cursor immediate after the diagram for subsequent controls
1150 ImGui::SetCursorPos(endPos);
1151}
1152
Brian Osmand67e5182017-12-08 16:46:09 -05001153void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001154 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1155 if (fShowImGuiTestWindow) {
1156 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1157 }
1158
1159 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001160 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1161 // always visible, we can end up in a layout feedback loop.
1162 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001163 DisplayParams params = fWindow->getRequestedDisplayParams();
1164 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001165 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1166 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001167 if (ImGui::CollapsingHeader("Backend")) {
1168 int newBackend = static_cast<int>(fBackendType);
1169 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1170 ImGui::SameLine();
1171 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001172#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1173 ImGui::SameLine();
1174 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1175#endif
Brian Osman621491e2017-02-28 15:45:01 -05001176#if defined(SK_VULKAN)
1177 ImGui::SameLine();
1178 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1179#endif
1180 if (newBackend != fBackendType) {
1181 fDeferredActions.push_back([=]() {
1182 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1183 });
1184 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001185
Brian Salomon99a33902017-03-07 15:16:34 -05001186 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001187 bool* wire = &params.fGrContextOptions.fWireframeMode;
1188 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1189 paramsChanged = true;
1190 }
Brian Salomon99a33902017-03-07 15:16:34 -05001191
Brian Osman28b12522017-03-08 17:10:24 -05001192 if (ctx) {
1193 int sampleCount = fWindow->sampleCount();
1194 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001195 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001196 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1197 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1198 ImGui::RadioButton("16", &sampleCount, 16);
1199
1200 if (sampleCount != params.fMSAASampleCount) {
1201 params.fMSAASampleCount = sampleCount;
1202 paramsChanged = true;
1203 }
1204 }
1205
Brian Osman8a9de3d2017-03-01 14:59:05 -05001206 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001207 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001208 auto prButton = [&](GpuPathRenderers x) {
1209 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001210 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1211 params.fGrContextOptions.fGpuPathRenderers = x;
1212 paramsChanged = true;
1213 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001214 }
1215 };
1216
1217 if (!ctx) {
1218 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001219 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001220 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001221 prButton(GpuPathRenderers::kAll);
1222 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1223 prButton(GpuPathRenderers::kStencilAndCover);
1224 }
1225 if (ctx->caps()->sampleShadingSupport()) {
1226 prButton(GpuPathRenderers::kMSAA);
1227 }
1228 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001229 prButton(GpuPathRenderers::kNone);
1230 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001231 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001232 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001233 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1234 prButton(GpuPathRenderers::kCoverageCounting);
1235 }
Jim Van Verth83010462017-03-16 08:45:39 -04001236 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001237 prButton(GpuPathRenderers::kTessellating);
1238 prButton(GpuPathRenderers::kNone);
1239 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001240 ImGui::TreePop();
1241 }
Brian Osman621491e2017-02-28 15:45:01 -05001242 }
1243
Brian Osmanfce09c52017-11-14 15:32:20 -05001244 if (fShowSlidePicker) {
1245 ImGui::SetNextTreeNodeOpen(true);
1246 }
1247
Brian Osman79086b92017-02-10 13:36:16 -05001248 if (ImGui::CollapsingHeader("Slide")) {
1249 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001250 static ImVector<const char*> filteredSlideNames;
1251 static ImVector<int> filteredSlideIndices;
1252
Brian Osmanfce09c52017-11-14 15:32:20 -05001253 if (fShowSlidePicker) {
1254 ImGui::SetKeyboardFocusHere();
1255 fShowSlidePicker = false;
1256 }
1257
Brian Osman79086b92017-02-10 13:36:16 -05001258 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001259 filteredSlideNames.clear();
1260 filteredSlideIndices.clear();
1261 int filteredIndex = 0;
1262 for (int i = 0; i < fSlides.count(); ++i) {
1263 const char* slideName = fSlides[i]->getName().c_str();
1264 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1265 if (i == fCurrentSlide) {
1266 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001267 }
Brian Osmanf479e422017-11-08 13:11:36 -05001268 filteredSlideNames.push_back(slideName);
1269 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001270 }
Brian Osman79086b92017-02-10 13:36:16 -05001271 }
Brian Osmanf479e422017-11-08 13:11:36 -05001272
Brian Osmanf479e422017-11-08 13:11:36 -05001273 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1274 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001275 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001276 }
1277 }
Brian Osmana109e392017-02-24 09:49:14 -05001278
1279 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001280 ColorMode newMode = fColorMode;
1281 auto cmButton = [&](ColorMode mode, const char* label) {
1282 if (ImGui::RadioButton(label, mode == fColorMode)) {
1283 newMode = mode;
1284 }
1285 };
1286
1287 cmButton(ColorMode::kLegacy, "Legacy 8888");
1288 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1289 "Color Managed 8888 (Nonlinear blending)");
1290 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1291 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1292
1293 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001294 // It isn't safe to switch color mode now (in the middle of painting). We might
1295 // tear down the back-end, etc... Defer this change until the next onIdle.
1296 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001297 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001298 });
Brian Osmana109e392017-02-24 09:49:14 -05001299 }
1300
1301 // Pick from common gamuts:
1302 int primariesIdx = 4; // Default: Custom
1303 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1304 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1305 primariesIdx = i;
1306 break;
1307 }
1308 }
1309
Brian Osmanfdab5762017-11-09 10:27:55 -05001310 // When we're in xform canvas mode, we can alter the transfer function, too
1311 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1312 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1313 }
1314
Brian Osmana109e392017-02-24 09:49:14 -05001315 if (ImGui::Combo("Primaries", &primariesIdx,
1316 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1317 if (primariesIdx >= 0 && primariesIdx <= 3) {
1318 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1319 }
1320 }
1321
1322 // Allow direct editing of gamut
1323 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1324 }
Brian Osman79086b92017-02-10 13:36:16 -05001325 }
Brian Salomon99a33902017-03-07 15:16:34 -05001326 if (paramsChanged) {
1327 fDeferredActions.push_back([=]() {
1328 fWindow->setRequestedDisplayParams(params);
1329 fWindow->inval();
1330 this->updateTitle();
1331 });
1332 }
Brian Osman79086b92017-02-10 13:36:16 -05001333 ImGui::End();
1334 }
1335
Brian Osmanf6877092017-02-13 09:39:57 -05001336 if (fShowZoomWindow && fLastImage) {
1337 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001338 static int zoomFactor = 8;
1339 if (ImGui::Button("<<")) {
1340 zoomFactor = SkTMax(zoomFactor / 2, 4);
1341 }
1342 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1343 if (ImGui::Button(">>")) {
1344 zoomFactor = SkTMin(zoomFactor * 2, 32);
1345 }
Brian Osmanf6877092017-02-13 09:39:57 -05001346
Brian Osmanf6877092017-02-13 09:39:57 -05001347 ImVec2 mousePos = ImGui::GetMousePos();
1348 ImVec2 avail = ImGui::GetContentRegionAvail();
1349
Brian Osmanead517d2017-11-13 15:36:36 -05001350 uint32_t pixel = 0;
1351 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1352 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1353 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001354 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1355 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1356 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001357 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1358 }
1359
Brian Osmand67e5182017-12-08 16:46:09 -05001360 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001361 // Translate so the region of the image that's under the mouse cursor is centered
1362 // in the zoom canvas:
1363 c->scale(zoomFactor, zoomFactor);
1364 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1365 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1366 c->drawImage(this->fLastImage, 0, 0);
1367
1368 SkPaint outline;
1369 outline.setStyle(SkPaint::kStroke_Style);
1370 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1371 });
Brian Osmanf6877092017-02-13 09:39:57 -05001372 }
1373
1374 ImGui::End();
1375 }
Brian Osman79086b92017-02-10 13:36:16 -05001376}
1377
liyuqian2edb0f42016-07-06 14:11:32 -07001378void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001379 for (int i = 0; i < fDeferredActions.count(); ++i) {
1380 fDeferredActions[i]();
1381 }
1382 fDeferredActions.reset();
1383
Brian Osman56a24812017-12-19 11:15:16 -05001384 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001385 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001386 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001387 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001388
Brian Osman79086b92017-02-10 13:36:16 -05001389 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001390 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001391 fWindow->inval();
1392 }
jvanverth9f372462016-04-06 06:08:59 -07001393}
liyuqiane5a6cd92016-05-27 08:52:52 -07001394
1395void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001396 if (!fWindow) {
1397 return;
1398 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001399 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001400 return; // Surface hasn't been created yet.
1401 }
1402
liyuqianb73c24b2016-06-03 08:47:23 -07001403 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001404 Json::Value slideState(Json::objectValue);
1405 slideState[kName] = kSlideStateName;
1406 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001407 if (fAllSlideNames.size() == 0) {
1408 for(auto slide : fSlides) {
1409 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1410 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001411 }
liyuqian1f508fd2016-06-07 06:57:40 -07001412 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001413
liyuqianb73c24b2016-06-03 08:47:23 -07001414 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001415 Json::Value backendState(Json::objectValue);
1416 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001417 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001418 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001419 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001420 backendState[kOptions].append(Json::Value(str));
1421 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001422
csmartdalton578f0642017-02-24 16:04:47 -07001423 // MSAA state
1424 Json::Value msaaState(Json::objectValue);
1425 msaaState[kName] = kMSAAStateName;
1426 msaaState[kValue] = fWindow->sampleCount();
1427 msaaState[kOptions] = Json::Value(Json::arrayValue);
1428 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1429 msaaState[kOptions].append(Json::Value(0));
1430 } else {
1431 for (int msaa : {0, 4, 8, 16}) {
1432 msaaState[kOptions].append(Json::Value(msaa));
1433 }
1434 }
1435
csmartdalton61cd31a2017-02-27 17:00:53 -07001436 // Path renderer state
1437 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1438 Json::Value prState(Json::objectValue);
1439 prState[kName] = kPathRendererStateName;
1440 prState[kValue] = gPathRendererNames[pr];
1441 prState[kOptions] = Json::Value(Json::arrayValue);
1442 const GrContext* ctx = fWindow->getGrContext();
1443 if (!ctx) {
1444 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001445 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001446 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001447 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1448 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1449 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1450 }
1451 if (ctx->caps()->sampleShadingSupport()) {
1452 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1453 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001454 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001455 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1456 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001457 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001458 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001459 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1460 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1461 }
Jim Van Verth83010462017-03-16 08:45:39 -04001462 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001463 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001464 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1465 }
1466
liyuqianb73c24b2016-06-03 08:47:23 -07001467 // Softkey state
1468 Json::Value softkeyState(Json::objectValue);
1469 softkeyState[kName] = kSoftkeyStateName;
1470 softkeyState[kValue] = kSoftkeyHint;
1471 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1472 softkeyState[kOptions].append(kSoftkeyHint);
1473 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1474 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1475 }
1476
liyuqian1f508fd2016-06-07 06:57:40 -07001477 // FPS state
1478 Json::Value fpsState(Json::objectValue);
1479 fpsState[kName] = kFpsStateName;
Brian Osman56a24812017-12-19 11:15:16 -05001480 double animTime = fStatsLayer.getLastTime(fAnimateTimer);
1481 double paintTime = fStatsLayer.getLastTime(fPaintTimer);
1482 double flushTime = fStatsLayer.getLastTime(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001483 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
Brian Osman56a24812017-12-19 11:15:16 -05001484 animTime + paintTime + flushTime,
1485 animTime, paintTime, flushTime).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001486 fpsState[kOptions] = Json::Value(Json::arrayValue);
1487
liyuqiane5a6cd92016-05-27 08:52:52 -07001488 Json::Value state(Json::arrayValue);
1489 state.append(slideState);
1490 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001491 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001492 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001493 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001494 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001495
Brian Osmaneff04b52017-11-21 13:18:02 -05001496 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001497}
1498
1499void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001500 // For those who will add more features to handle the state change in this function:
1501 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1502 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1503 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001504 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001505 for (int i = 0; i < fSlides.count(); ++i) {
1506 if (fSlides[i]->getName().equals(stateValue)) {
1507 this->setCurrentSlide(i);
1508 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001509 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001510 }
Florin Malitaab99c342018-01-16 16:23:03 -05001511
1512 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001513 } else if (stateName.equals(kBackendStateName)) {
1514 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1515 if (stateValue.equals(kBackendTypeStrings[i])) {
1516 if (fBackendType != i) {
1517 fBackendType = (sk_app::Window::BackendType)i;
1518 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001519 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001520 }
1521 break;
1522 }
1523 }
csmartdalton578f0642017-02-24 16:04:47 -07001524 } else if (stateName.equals(kMSAAStateName)) {
1525 DisplayParams params = fWindow->getRequestedDisplayParams();
1526 int sampleCount = atoi(stateValue.c_str());
1527 if (sampleCount != params.fMSAASampleCount) {
1528 params.fMSAASampleCount = sampleCount;
1529 fWindow->setRequestedDisplayParams(params);
1530 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001531 this->updateTitle();
1532 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001533 }
1534 } else if (stateName.equals(kPathRendererStateName)) {
1535 DisplayParams params = fWindow->getRequestedDisplayParams();
1536 for (const auto& pair : gPathRendererNames) {
1537 if (pair.second == stateValue.c_str()) {
1538 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1539 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1540 fWindow->setRequestedDisplayParams(params);
1541 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001542 this->updateTitle();
1543 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001544 }
1545 break;
1546 }
csmartdalton578f0642017-02-24 16:04:47 -07001547 }
liyuqianb73c24b2016-06-03 08:47:23 -07001548 } else if (stateName.equals(kSoftkeyStateName)) {
1549 if (!stateValue.equals(kSoftkeyHint)) {
1550 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001551 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001552 }
liyuqian2edb0f42016-07-06 14:11:32 -07001553 } else if (stateName.equals(kRefreshStateName)) {
1554 // This state is actually NOT in the UI state.
1555 // We use this to allow Android to quickly set bool fRefresh.
1556 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001557 } else {
1558 SkDebugf("Unknown stateName: %s", stateName.c_str());
1559 }
1560}
Brian Osman79086b92017-02-10 13:36:16 -05001561
1562bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001563 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001564}
1565
1566bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001567 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001568 fWindow->inval();
1569 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001570 } else {
1571 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001572 }
Brian Osman79086b92017-02-10 13:36:16 -05001573}