blob: 89f436db80f910614e06cd970af2320dd4cbc664 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkGraphics.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkSurface.h"
14#include "include/gpu/GrContext.h"
15#include "include/private/SkTo.h"
16#include "include/utils/SkPaintFilterCanvas.h"
17#include "src/core/SkColorSpacePriv.h"
18#include "src/core/SkImagePriv.h"
19#include "src/core/SkMD5.h"
20#include "src/core/SkMakeUnique.h"
21#include "src/core/SkOSFile.h"
22#include "src/core/SkScan.h"
23#include "src/core/SkTaskGroup.h"
24#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrGpu.h"
26#include "src/gpu/GrPersistentCacheUtils.h"
27#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
28#include "src/utils/SkJSONWriter.h"
29#include "src/utils/SkOSPath.h"
30#include "tools/Resources.h"
31#include "tools/ToolUtils.h"
32#include "tools/flags/CommandLineFlags.h"
33#include "tools/flags/CommonFlags.h"
34#include "tools/trace/EventTracingPriv.h"
35#include "tools/viewer/BisectSlide.h"
36#include "tools/viewer/GMSlide.h"
37#include "tools/viewer/ImageSlide.h"
38#include "tools/viewer/ParticlesSlide.h"
39#include "tools/viewer/SKPSlide.h"
40#include "tools/viewer/SampleSlide.h"
41#include "tools/viewer/SlideDir.h"
42#include "tools/viewer/SvgSlide.h"
43#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070044
Hal Canaryc640d0d2018-06-13 09:59:02 -040045#include <stdlib.h>
46#include <map>
47
Hal Canary8a001442018-09-19 11:31:27 -040048#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040049#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040050
Florin Malita87ccf332018-05-04 12:23:24 -040051#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040053#endif
54
Ruiqi Maof5101492018-06-29 14:32:21 -040055#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
Mike Kleinc0bd9f92019-04-23 12:05:21 -050056 #include "tools/viewer/NIMASlide.h"
Ruiqi Maof5101492018-06-29 14:32:21 -040057#endif
58
jvanverth34524262016-05-04 13:49:13 -070059using namespace sk_app;
60
csmartdalton61cd31a2017-02-27 17:00:53 -070061static std::map<GpuPathRenderers, std::string> gPathRendererNames;
62
jvanverth9f372462016-04-06 06:08:59 -070063Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070064 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070065}
66
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060067static DEFINE_string(slide, "", "Start on this sample.");
68static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050069
bsalomon6c471f72016-07-26 12:56:32 -070070#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070071# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050072#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
73# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
bsalomon6c471f72016-07-26 12:56:32 -070074#else
75# define BACKENDS_STR "\"sw\" and \"gl\""
76#endif
77
Brian Osman2dd96932016-10-18 15:33:53 -040078static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070079
Mike Klein5b3f3432019-03-21 11:42:21 -050080static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070081
Mike Klein84836b72019-03-21 11:31:36 -050082static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -070083
Mike Klein84836b72019-03-21 11:31:36 -050084static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -040085
Mike Kleinc6142d82019-03-25 10:54:59 -050086static DEFINE_string2(match, m, nullptr,
87 "[~][^]substring[$] [...] of name to run.\n"
88 "Multiple matches may be separated by spaces.\n"
89 "~ causes a matching name to always be skipped\n"
90 "^ requires the start of the name to match\n"
91 "$ requires the end of the name to match\n"
92 "^ and $ requires an exact match\n"
93 "If a name does not match any list entry,\n"
94 "it is skipped unless some list entry starts with ~");
95
Mike Klein19fb3972019-03-21 13:08:08 -050096#if defined(SK_BUILD_FOR_ANDROID)
97 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
98 static DEFINE_string(nimas, "/data/local/tmp/nimas", "Directory to read NIMA animations from.");
Mike Kleinc6142d82019-03-25 10:54:59 -050099 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
100 static DEFINE_string(lotties, "/data/local/tmp/lotties",
101 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500102#else
103 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
104 static DEFINE_string(nimas, "nimas", "Directory to read NIMA animations from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500105 static DEFINE_string(skps, "skps", "Directory to read skps from.");
106 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500107#endif
108
Mike Kleinc6142d82019-03-25 10:54:59 -0500109static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
110
111static DEFINE_int_2(threads, j, -1,
112 "Run threadsafe tests on a threadpool with this many extra threads, "
113 "defaulting to one extra thread per core.");
114
115
Brian Salomon194db172017-08-17 14:37:06 -0400116const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700117 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400118#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
119 "ANGLE",
120#endif
jvanverth063ece72016-06-17 09:29:14 -0700121#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700122 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700123#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -0500124#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
125 "Metal",
126#endif
csmartdalton578f0642017-02-24 16:04:47 -0700127 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700128};
129
bsalomon6c471f72016-07-26 12:56:32 -0700130static sk_app::Window::BackendType get_backend_type(const char* str) {
131#ifdef SK_VULKAN
132 if (0 == strcmp(str, "vk")) {
133 return sk_app::Window::kVulkan_BackendType;
134 } else
135#endif
Brian Salomon194db172017-08-17 14:37:06 -0400136#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
137 if (0 == strcmp(str, "angle")) {
138 return sk_app::Window::kANGLE_BackendType;
139 } else
140#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -0500141#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
142 if (0 == strcmp(str, "mtl")) {
143 return sk_app::Window::kMetal_BackendType;
144 } else
145#endif
bsalomon6c471f72016-07-26 12:56:32 -0700146 if (0 == strcmp(str, "gl")) {
147 return sk_app::Window::kNativeGL_BackendType;
148 } else if (0 == strcmp(str, "sw")) {
149 return sk_app::Window::kRaster_BackendType;
150 } else {
151 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
152 return sk_app::Window::kRaster_BackendType;
153 }
154}
155
Brian Osmana109e392017-02-24 09:49:14 -0500156static SkColorSpacePrimaries gSrgbPrimaries = {
157 0.64f, 0.33f,
158 0.30f, 0.60f,
159 0.15f, 0.06f,
160 0.3127f, 0.3290f };
161
162static SkColorSpacePrimaries gAdobePrimaries = {
163 0.64f, 0.33f,
164 0.21f, 0.71f,
165 0.15f, 0.06f,
166 0.3127f, 0.3290f };
167
168static SkColorSpacePrimaries gP3Primaries = {
169 0.680f, 0.320f,
170 0.265f, 0.690f,
171 0.150f, 0.060f,
172 0.3127f, 0.3290f };
173
174static SkColorSpacePrimaries gRec2020Primaries = {
175 0.708f, 0.292f,
176 0.170f, 0.797f,
177 0.131f, 0.046f,
178 0.3127f, 0.3290f };
179
180struct NamedPrimaries {
181 const char* fName;
182 SkColorSpacePrimaries* fPrimaries;
183} gNamedPrimaries[] = {
184 { "sRGB", &gSrgbPrimaries },
185 { "AdobeRGB", &gAdobePrimaries },
186 { "P3", &gP3Primaries },
187 { "Rec. 2020", &gRec2020Primaries },
188};
189
190static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
191 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
192}
193
Brian Osman70d2f432017-11-08 09:54:10 -0500194static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
195 // In raster mode, we still use GL for the window.
196 // This lets us render the GUI faster (and correct).
197 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
198}
199
Jim Van Verth74826c82019-03-01 14:37:30 -0500200class NullSlide : public Slide {
201 SkISize getDimensions() const override {
202 return SkISize::Make(640, 480);
203 }
204
205 void draw(SkCanvas* canvas) override {
206 canvas->clear(0xffff11ff);
207 }
208};
209
liyuqiane5a6cd92016-05-27 08:52:52 -0700210const char* kName = "name";
211const char* kValue = "value";
212const char* kOptions = "options";
213const char* kSlideStateName = "Slide";
214const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700215const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700216const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700217const char* kSoftkeyStateName = "Softkey";
218const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700219const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700220const char* kON = "ON";
221const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700222const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700223
jvanverth34524262016-05-04 13:49:13 -0700224Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500225 : fCurrentSlide(-1)
226 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500227 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400228 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500229 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500230 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500231 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500232 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400233 , fZoomWindowFixed(false)
234 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500235 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400236 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700237 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500238 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500239 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500240 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500241 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700242 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400243 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400244 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400245 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500246 , fTiled(false)
247 , fDrawTileBoundaries(false)
248 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400249 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700250{
Greg Daniel285db442016-10-14 09:12:53 -0400251 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700252
Brian Osmanf09e35e2017-12-15 14:48:09 -0500253 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500254 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500255 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
256 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
257 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
258 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700259
jvanverth2bb3b6d2016-04-08 07:24:09 -0700260 SkDebugf("Command line arguments: ");
261 for (int i = 1; i < argc; ++i) {
262 SkDebugf("%s ", argv[i]);
263 }
264 SkDebugf("\n");
265
Mike Klein88544fb2019-03-20 10:50:33 -0500266 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500267#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400268 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500269#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700270
Mike Klein19cc0f62019-03-22 15:30:07 -0500271 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500272
Brian Osmanbc8150f2017-07-24 11:38:01 -0400273 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400274 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400275
bsalomon6c471f72016-07-26 12:56:32 -0700276 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700277 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700278
csmartdalton578f0642017-02-24 16:04:47 -0700279 DisplayParams displayParams;
280 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700281 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400282 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
283 displayParams.fGrContextOptions.fDisallowGLSLBinaryCaching = true;
csmartdalton578f0642017-02-24 16:04:47 -0700284 fWindow->setRequestedDisplayParams(displayParams);
285
Brian Osman56a24812017-12-19 11:15:16 -0500286 // Configure timers
287 fStatsLayer.setActive(false);
288 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
289 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
290 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
291
jvanverth9f372462016-04-06 06:08:59 -0700292 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700293 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500294 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500295 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500296 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700297
brianosman622c8d52016-05-10 06:50:49 -0700298 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500299 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
300 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
301 fWindow->inval();
302 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500303 // Command to jump directly to the slide picker and give it focus
304 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
305 this->fShowImGuiDebugWindow = true;
306 this->fShowSlidePicker = true;
307 fWindow->inval();
308 });
309 // Alias that to Backspace, to match SampleApp
310 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
311 this->fShowImGuiDebugWindow = true;
312 this->fShowSlidePicker = true;
313 fWindow->inval();
314 });
Brian Osman79086b92017-02-10 13:36:16 -0500315 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
316 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
317 fWindow->inval();
318 });
Brian Osmanf6877092017-02-13 09:39:57 -0500319 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
320 this->fShowZoomWindow = !this->fShowZoomWindow;
321 fWindow->inval();
322 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400323 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
324 this->fZoomWindowFixed = !this->fZoomWindowFixed;
325 fWindow->inval();
326 });
Greg Danield0794cc2019-03-27 16:23:26 -0400327 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
328 DisplayParams params = fWindow->getRequestedDisplayParams();
329 params.fDisableVsync = !params.fDisableVsync;
330 fWindow->setRequestedDisplayParams(params);
331 this->updateTitle();
332 fWindow->inval();
333 });
brianosman622c8d52016-05-10 06:50:49 -0700334 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500335 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700336 fWindow->inval();
337 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400338 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500339 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400340 this->updateTitle();
341 fWindow->inval();
342 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500343 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500344 switch (fColorMode) {
345 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500346 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500347 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500348 case ColorMode::kColorManaged8888:
349 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500350 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500351 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500352 this->setColorMode(ColorMode::kLegacy);
353 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500354 }
brianosman622c8d52016-05-10 06:50:49 -0700355 });
356 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500357 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700358 });
359 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500360 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700361 });
362 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
363 this->changeZoomLevel(1.f / 32.f);
364 fWindow->inval();
365 });
366 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
367 this->changeZoomLevel(-1.f / 32.f);
368 fWindow->inval();
369 });
jvanverthaf236b52016-05-20 06:01:06 -0700370 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400371 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
372 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500373 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400374#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
375 if (newBackend == sk_app::Window::kVulkan_BackendType) {
376 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
377 sk_app::Window::kBackendTypeCount);
378 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
379 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500380 }
381#endif
Brian Osman621491e2017-02-28 15:45:01 -0500382 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700383 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500384 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
385 fSaveToSKP = true;
386 fWindow->inval();
387 });
Mike Reed376d8122019-03-14 11:39:02 -0400388 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
389 fShowSlideDimensions = !fShowSlideDimensions;
390 fWindow->inval();
391 });
Ben Wagner37c54032018-04-13 14:30:23 -0400392 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
393 DisplayParams params = fWindow->getRequestedDisplayParams();
394 uint32_t flags = params.fSurfaceProps.flags();
395 if (!fPixelGeometryOverrides) {
396 fPixelGeometryOverrides = true;
397 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
398 } else {
399 switch (params.fSurfaceProps.pixelGeometry()) {
400 case kUnknown_SkPixelGeometry:
401 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
402 break;
403 case kRGB_H_SkPixelGeometry:
404 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
405 break;
406 case kBGR_H_SkPixelGeometry:
407 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
408 break;
409 case kRGB_V_SkPixelGeometry:
410 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
411 break;
412 case kBGR_V_SkPixelGeometry:
413 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
414 fPixelGeometryOverrides = false;
415 break;
416 }
417 }
418 fWindow->setRequestedDisplayParams(params);
419 this->updateTitle();
420 fWindow->inval();
421 });
Ben Wagner9613e452019-01-23 10:34:59 -0500422 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500423 if (!fFontOverrides.fHinting) {
424 fFontOverrides.fHinting = true;
425 fFont.setHinting(kNo_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500426 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500427 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500428 case kNo_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500429 fFont.setHinting(kSlight_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500430 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500431 case kSlight_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500432 fFont.setHinting(kNormal_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500433 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500434 case kNormal_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500435 fFont.setHinting(kFull_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500436 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500437 case kFull_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500438 fFont.setHinting(kNo_SkFontHinting);
439 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500440 break;
441 }
442 }
443 this->updateTitle();
444 fWindow->inval();
445 });
446 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500447 if (!fPaintOverrides.fAntiAlias) {
448 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
449 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500450 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500451 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500452 } else {
453 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500454 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500455 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500456 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400457 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500458 break;
459 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500460 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500461 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400462 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500463 break;
464 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500465 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400466 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500467 break;
468 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500469 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
470 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500471 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
472 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500473 break;
474 }
475 }
476 this->updateTitle();
477 fWindow->inval();
478 });
Ben Wagner37c54032018-04-13 14:30:23 -0400479 fCommands.addCommand('D', "Modes", "DFT", [this]() {
480 DisplayParams params = fWindow->getRequestedDisplayParams();
481 uint32_t flags = params.fSurfaceProps.flags();
482 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
483 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
484 fWindow->setRequestedDisplayParams(params);
485 this->updateTitle();
486 fWindow->inval();
487 });
Ben Wagner9613e452019-01-23 10:34:59 -0500488 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500489 if (!fFontOverrides.fEdging) {
490 fFontOverrides.fEdging = true;
491 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500492 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500493 switch (fFont.getEdging()) {
494 case SkFont::Edging::kAlias:
495 fFont.setEdging(SkFont::Edging::kAntiAlias);
496 break;
497 case SkFont::Edging::kAntiAlias:
498 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
499 break;
500 case SkFont::Edging::kSubpixelAntiAlias:
501 fFont.setEdging(SkFont::Edging::kAlias);
502 fFontOverrides.fEdging = false;
503 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500504 }
505 }
506 this->updateTitle();
507 fWindow->inval();
508 });
Ben Wagner9613e452019-01-23 10:34:59 -0500509 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500510 if (!fFontOverrides.fSubpixel) {
511 fFontOverrides.fSubpixel = true;
512 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500513 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500514 if (!fFont.isSubpixel()) {
515 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500516 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500517 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500518 }
519 }
520 this->updateTitle();
521 fWindow->inval();
522 });
Brian Osman805a7272018-05-02 15:40:20 -0400523 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
524 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
525 : kPerspective_Real;
526 this->updateTitle();
527 fWindow->inval();
528 });
529 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
530 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
531 : kPerspective_Off;
532 this->updateTitle();
533 fWindow->inval();
534 });
Brian Osman207d4102019-01-10 09:40:58 -0500535 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
536 fAnimTimer.togglePauseResume();
537 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400538 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
539 fZoomUI = !fZoomUI;
540 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
541 fWindow->inval();
542 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500543
jvanverth2bb3b6d2016-04-08 07:24:09 -0700544 // set up slides
545 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500546 if (FLAGS_list) {
547 this->listNames();
548 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700549
Brian Osman9bb47cf2018-04-26 15:55:00 -0400550 fPerspectivePoints[0].set(0, 0);
551 fPerspectivePoints[1].set(1, 0);
552 fPerspectivePoints[2].set(0, 1);
553 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700554 fAnimTimer.run();
555
Hal Canaryc465d132017-12-08 10:21:31 -0500556 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500557 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400558 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500559 }
560 fImGuiGamutPaint.setColor(SK_ColorWHITE);
561 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
562
jongdeok.kim804f17e2019-02-26 14:39:23 +0900563 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500564 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700565}
566
jvanverth34524262016-05-04 13:49:13 -0700567void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400568 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
569 static const struct {
570 const char* fExtension;
571 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500572 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400573 const SlideFactory fFactory;
574 } gExternalSlidesInfo[] = {
575 { ".skp", "skp-dir", FLAGS_skps,
576 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
577 return sk_make_sp<SKPSlide>(name, path);}
578 },
579 { ".jpg", "jpg-dir", FLAGS_jpgs,
580 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
581 return sk_make_sp<ImageSlide>(name, path);}
582 },
Florin Malita87ccf332018-05-04 12:23:24 -0400583#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400584 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400585 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
586 return sk_make_sp<SkottieSlide>(name, path);}
587 },
Florin Malita87ccf332018-05-04 12:23:24 -0400588#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400589#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400590 { ".svg", "svg-dir", FLAGS_svgs,
591 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
592 return sk_make_sp<SvgSlide>(name, path);}
593 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400594#endif
Ruiqi Maof5101492018-06-29 14:32:21 -0400595#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
596 { ".nima", "nima-dir", FLAGS_nimas,
597 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
598 return sk_make_sp<NIMASlide>(name, path);}
599 },
600#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400601 };
jvanverthc265a922016-04-08 12:51:45 -0700602
Brian Salomon343553a2018-09-05 15:41:23 -0400603 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700604
Mike Klein88544fb2019-03-20 10:50:33 -0500605 const auto addSlide =
606 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
607 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
608 return;
609 }
liyuqian6f163d22016-06-13 12:26:45 -0700610
Mike Klein88544fb2019-03-20 10:50:33 -0500611 if (auto slide = fact(name, path)) {
612 dirSlides.push_back(slide);
613 fSlides.push_back(std::move(slide));
614 }
615 };
Florin Malita76a076b2018-02-15 18:40:48 -0500616
Florin Malita38792ce2018-05-08 10:36:18 -0400617 if (!FLAGS_file.isEmpty()) {
618 // single file mode
619 const SkString file(FLAGS_file[0]);
620
621 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
622 for (const auto& sinfo : gExternalSlidesInfo) {
623 if (file.endsWith(sinfo.fExtension)) {
624 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
625 return;
626 }
627 }
628
629 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
630 } else {
631 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
632 }
633
634 return;
635 }
636
637 // Bisect slide.
638 if (!FLAGS_bisect.isEmpty()) {
639 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500640 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400641 if (FLAGS_bisect.count() >= 2) {
642 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
643 bisect->onChar(*ch);
644 }
645 }
646 fSlides.push_back(std::move(bisect));
647 }
648 }
649
650 // GMs
651 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400652 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
653 std::unique_ptr<skiagm::GM> gm(gmFactory(nullptr));
Mike Klein88544fb2019-03-20 10:50:33 -0500654 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400655 sk_sp<Slide> slide(new GMSlide(gm.release()));
656 fSlides.push_back(std::move(slide));
657 }
Florin Malita38792ce2018-05-08 10:36:18 -0400658 }
659 // reverse gms
660 int numGMs = fSlides.count() - firstGM;
661 for (int i = 0; i < numGMs/2; ++i) {
662 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
663 }
664
665 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400666 for (const SampleFactory factory : SampleRegistry::Range()) {
667 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500668 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400669 fSlides.push_back(slide);
670 }
Florin Malita38792ce2018-05-08 10:36:18 -0400671 }
672
Brian Osman7c979f52019-02-12 13:27:51 -0500673 // Particle demo
674 {
675 // TODO: Convert this to a sample
676 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500677 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500678 fSlides.push_back(std::move(slide));
679 }
680 }
681
Florin Malita0ffa3222018-04-05 14:34:45 -0400682 for (const auto& info : gExternalSlidesInfo) {
683 for (const auto& flag : info.fFlags) {
684 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
685 // single file
686 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
687 } else {
688 // directory
689 SkOSFile::Iter it(flag.c_str(), info.fExtension);
690 SkString name;
691 while (it.next(&name)) {
692 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
693 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400694 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400695 if (!dirSlides.empty()) {
696 fSlides.push_back(
697 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
698 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500699 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400700 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400701 }
702 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500703
704 if (!fSlides.count()) {
705 sk_sp<Slide> slide(new NullSlide());
706 fSlides.push_back(std::move(slide));
707 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700708}
709
710
jvanverth34524262016-05-04 13:49:13 -0700711Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700712 fWindow->detach();
713 delete fWindow;
714}
715
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500716struct SkPaintTitleUpdater {
717 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
718 void append(const char* s) {
719 if (fCount == 0) {
720 fTitle->append(" {");
721 } else {
722 fTitle->append(", ");
723 }
724 fTitle->append(s);
725 ++fCount;
726 }
727 void done() {
728 if (fCount > 0) {
729 fTitle->append("}");
730 }
731 }
732 SkString* fTitle;
733 int fCount;
734};
735
brianosman05de2162016-05-06 13:28:57 -0700736void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700737 if (!fWindow) {
738 return;
739 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500740 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700741 return; // Surface hasn't been created yet.
742 }
743
jvanverth34524262016-05-04 13:49:13 -0700744 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700745 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700746
Mike Kleine5acd752019-03-22 09:57:16 -0500747 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400748 if (gSkForceAnalyticAA) {
749 title.append(" <FAAA>");
750 } else {
751 title.append(" <AAA>");
752 }
753 }
754
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500755 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500756 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
757 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400758 const char* on, const char* off)
759 {
Ben Wagner9613e452019-01-23 10:34:59 -0500760 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400761 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500762 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400763 };
764
Ben Wagner9613e452019-01-23 10:34:59 -0500765 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
766 const char* on, const char* off)
767 {
768 if (fFontOverrides.*flag) {
769 paintTitle.append((fFont.*isFlag)() ? on : off);
770 }
771 };
772
773 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
774 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500775 if (fPaintOverrides.fFilterQuality) {
776 switch (fPaint.getFilterQuality()) {
777 case kNone_SkFilterQuality:
778 paintTitle.append("NoFilter");
779 break;
780 case kLow_SkFilterQuality:
781 paintTitle.append("LowFilter");
782 break;
783 case kMedium_SkFilterQuality:
784 paintTitle.append("MediumFilter");
785 break;
786 case kHigh_SkFilterQuality:
787 paintTitle.append("HighFilter");
788 break;
789 }
790 }
Ben Wagner9613e452019-01-23 10:34:59 -0500791
792 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
793 "Force Autohint", "No Force Autohint");
794 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
795 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
796 "Linear Metrics", "Non-Linear Metrics");
797 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
798 "Bitmap Text", "No Bitmap Text");
799 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
800
801 if (fFontOverrides.fEdging) {
802 switch (fFont.getEdging()) {
803 case SkFont::Edging::kAlias:
804 paintTitle.append("Alias Text");
805 break;
806 case SkFont::Edging::kAntiAlias:
807 paintTitle.append("Antialias Text");
808 break;
809 case SkFont::Edging::kSubpixelAntiAlias:
810 paintTitle.append("Subpixel Antialias Text");
811 break;
812 }
813 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400814
Mike Reed3ae47332019-01-04 10:11:46 -0500815 if (fFontOverrides.fHinting) {
816 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500817 case kNo_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500818 paintTitle.append("No Hinting");
819 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500820 case kSlight_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500821 paintTitle.append("Slight Hinting");
822 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500823 case kNormal_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500824 paintTitle.append("Normal Hinting");
825 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500826 case kFull_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500827 paintTitle.append("Full Hinting");
828 break;
829 }
830 }
831 paintTitle.done();
832
Brian Osman92004802017-03-06 11:47:26 -0500833 switch (fColorMode) {
834 case ColorMode::kLegacy:
835 title.append(" Legacy 8888");
836 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500837 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500838 title.append(" ColorManaged 8888");
839 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500840 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500841 title.append(" ColorManaged F16");
842 break;
843 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500844
Brian Osman92004802017-03-06 11:47:26 -0500845 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500846 int curPrimaries = -1;
847 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
848 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
849 curPrimaries = i;
850 break;
851 }
852 }
Brian Osman03115dc2018-11-26 13:55:19 -0500853 title.appendf(" %s Gamma %f",
854 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500855 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700856 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500857
Ben Wagner37c54032018-04-13 14:30:23 -0400858 const DisplayParams& params = fWindow->getRequestedDisplayParams();
859 if (fPixelGeometryOverrides) {
860 switch (params.fSurfaceProps.pixelGeometry()) {
861 case kUnknown_SkPixelGeometry:
862 title.append( " Flat");
863 break;
864 case kRGB_H_SkPixelGeometry:
865 title.append( " RGB");
866 break;
867 case kBGR_H_SkPixelGeometry:
868 title.append( " BGR");
869 break;
870 case kRGB_V_SkPixelGeometry:
871 title.append( " RGBV");
872 break;
873 case kBGR_V_SkPixelGeometry:
874 title.append( " BGRV");
875 break;
876 }
877 }
878
879 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
880 title.append(" DFT");
881 }
882
csmartdalton578f0642017-02-24 16:04:47 -0700883 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700884 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500885 int msaa = fWindow->sampleCount();
886 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700887 title.appendf(" MSAA: %i", msaa);
888 }
889 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700890
891 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600892 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700893 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
894 }
895
Brian Osman805a7272018-05-02 15:40:20 -0400896 if (kPerspective_Real == fPerspectiveMode) {
897 title.append(" Perpsective (Real)");
898 } else if (kPerspective_Fake == fPerspectiveMode) {
899 title.append(" Perspective (Fake)");
900 }
901
brianosman05de2162016-05-06 13:28:57 -0700902 fWindow->setTitle(title.c_str());
903}
904
Florin Malitaab99c342018-01-16 16:23:03 -0500905int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500906
907 if (!FLAGS_slide.isEmpty()) {
908 int count = fSlides.count();
909 for (int i = 0; i < count; i++) {
910 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500911 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500912 }
913 }
914
915 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
916 this->listNames();
917 }
918
Florin Malitaab99c342018-01-16 16:23:03 -0500919 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500920}
921
Florin Malitaab99c342018-01-16 16:23:03 -0500922void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500923 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500924 for (const auto& slide : fSlides) {
925 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500926 }
927}
928
Florin Malitaab99c342018-01-16 16:23:03 -0500929void Viewer::setCurrentSlide(int slide) {
930 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700931
Florin Malitaab99c342018-01-16 16:23:03 -0500932 if (slide == fCurrentSlide) {
933 return;
934 }
935
936 if (fCurrentSlide >= 0) {
937 fSlides[fCurrentSlide]->unload();
938 }
939
940 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
941 SkIntToScalar(fWindow->height()));
942 fCurrentSlide = slide;
943 this->setupCurrentSlide();
944}
945
946void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500947 if (fCurrentSlide >= 0) {
948 // prepare dimensions for image slides
949 fGesture.resetTouchState();
950 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700951
Jim Van Verth0848fb02018-01-22 13:39:30 -0500952 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
953 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
954 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400955
Jim Van Verth0848fb02018-01-22 13:39:30 -0500956 // Start with a matrix that scales the slide to the available screen space
957 if (fWindow->scaleContentToFit()) {
958 if (windowRect.width() > 0 && windowRect.height() > 0) {
959 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
960 }
liyuqiane46e4f02016-05-20 07:32:19 -0700961 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500962
963 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400964 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500965
966 this->updateTitle();
967 this->updateUIState();
968
969 fStatsLayer.resetMeasurements();
970
971 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700972 }
jvanverthc265a922016-04-08 12:51:45 -0700973}
974
975#define MAX_ZOOM_LEVEL 8
976#define MIN_ZOOM_LEVEL -8
977
jvanverth34524262016-05-04 13:49:13 -0700978void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700979 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400980 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400981 this->preTouchMatrixChanged();
982}
Yuqian Li755778c2018-03-28 16:23:31 -0400983
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400984void Viewer::preTouchMatrixChanged() {
985 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400986 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
987 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
988 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
989 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
990}
991
Brian Osman805a7272018-05-02 15:40:20 -0400992SkMatrix Viewer::computePerspectiveMatrix() {
993 SkScalar w = fWindow->width(), h = fWindow->height();
994 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
995 SkPoint perspPts[4] = {
996 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
997 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
998 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
999 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1000 };
1001 SkMatrix m;
1002 m.setPolyToPoly(orthoPts, perspPts, 4);
1003 return m;
1004}
1005
Yuqian Li755778c2018-03-28 16:23:31 -04001006SkMatrix Viewer::computePreTouchMatrix() {
1007 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001008
1009 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001010 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001011 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001012
1013 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1014 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001015
Brian Osman805a7272018-05-02 15:40:20 -04001016 if (kPerspective_Real == fPerspectiveMode) {
1017 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001018 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001019 }
1020
Yuqian Li755778c2018-03-28 16:23:31 -04001021 return m;
jvanverthc265a922016-04-08 12:51:45 -07001022}
1023
liyuqiand3cdbca2016-05-17 12:44:20 -07001024SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001025 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001026 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001027 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001028 return m;
jvanverthc265a922016-04-08 12:51:45 -07001029}
1030
Brian Osman621491e2017-02-28 15:45:01 -05001031void Viewer::setBackend(sk_app::Window::BackendType backendType) {
1032 fBackendType = backendType;
1033
1034 fWindow->detach();
1035
Brian Osman70d2f432017-11-08 09:54:10 -05001036#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001037 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1038 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001039 DisplayParams params = fWindow->getRequestedDisplayParams();
1040 delete fWindow;
1041 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001042
Brian Osman70d2f432017-11-08 09:54:10 -05001043 // re-register callbacks
1044 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001045 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001046 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001047 fWindow->pushLayer(&fImGuiLayer);
1048
Brian Osman70d2f432017-11-08 09:54:10 -05001049 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1050 // will still include our correct sample count. But the re-created fWindow will lose that
1051 // information. On Windows, we need to re-create the window when changing sample count,
1052 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1053 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1054 // as if we had never changed windows at all).
1055 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001056#endif
1057
Brian Osman70d2f432017-11-08 09:54:10 -05001058 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001059}
1060
Brian Osman92004802017-03-06 11:47:26 -05001061void Viewer::setColorMode(ColorMode colorMode) {
1062 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001063 this->updateTitle();
1064 fWindow->inval();
1065}
1066
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001067class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1068public:
Mike Reed3ae47332019-01-04 10:11:46 -05001069 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1070 SkFont* font, Viewer::SkFontFields* ffields)
1071 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001072 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001073 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1074 sk_sp<SkTextBlob>* cache) {
1075 bool blobWillChange = false;
1076 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001077 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1078 bool shouldDraw = this->filterFont(&filteredFont);
1079 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001080 blobWillChange = true;
1081 break;
1082 }
1083 }
1084 if (!blobWillChange) {
1085 return blob;
1086 }
1087
1088 SkTextBlobBuilder builder;
1089 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001090 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1091 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001092 if (!shouldDraw) {
1093 continue;
1094 }
1095
Mike Reed3ae47332019-01-04 10:11:46 -05001096 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001097
Ben Wagner41e40472018-09-24 13:01:54 -04001098 const SkTextBlobBuilder::RunBuffer& runBuffer
1099 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001100 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001101 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001102 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001103 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001104 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001105 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001106 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001107 it.glyphCount(), it.textSize(), SkString())
1108 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1109 uint32_t glyphCount = it.glyphCount();
1110 if (it.glyphs()) {
1111 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1112 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1113 }
1114 if (it.pos()) {
1115 size_t posSize = sizeof(decltype(*it.pos()));
1116 uint8_t positioning = it.positioning();
1117 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1118 }
1119 if (it.text()) {
1120 size_t textSize = sizeof(decltype(*it.text()));
1121 uint32_t textCount = it.textSize();
1122 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1123 }
1124 if (it.clusters()) {
1125 size_t clusterSize = sizeof(decltype(*it.clusters()));
1126 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1127 }
1128 }
1129 *cache = builder.make();
1130 return cache->get();
1131 }
1132 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1133 const SkPaint& paint) override {
1134 sk_sp<SkTextBlob> cache;
1135 this->SkPaintFilterCanvas::onDrawTextBlob(
1136 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1137 }
Mike Reed3ae47332019-01-04 10:11:46 -05001138 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001139 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001140 font->writable()->setSize(fFont->getSize());
1141 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001142 if (fFontOverrides->fScaleX) {
1143 font->writable()->setScaleX(fFont->getScaleX());
1144 }
1145 if (fFontOverrides->fSkewX) {
1146 font->writable()->setSkewX(fFont->getSkewX());
1147 }
Mike Reed3ae47332019-01-04 10:11:46 -05001148 if (fFontOverrides->fHinting) {
1149 font->writable()->setHinting(fFont->getHinting());
1150 }
Ben Wagner9613e452019-01-23 10:34:59 -05001151 if (fFontOverrides->fEdging) {
1152 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001153 }
Ben Wagner9613e452019-01-23 10:34:59 -05001154 if (fFontOverrides->fEmbolden) {
1155 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001156 }
Ben Wagner9613e452019-01-23 10:34:59 -05001157 if (fFontOverrides->fLinearMetrics) {
1158 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001159 }
Ben Wagner9613e452019-01-23 10:34:59 -05001160 if (fFontOverrides->fSubpixel) {
1161 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001162 }
Ben Wagner9613e452019-01-23 10:34:59 -05001163 if (fFontOverrides->fEmbeddedBitmaps) {
1164 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001165 }
Ben Wagner9613e452019-01-23 10:34:59 -05001166 if (fFontOverrides->fForceAutoHinting) {
1167 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001168 }
Ben Wagner9613e452019-01-23 10:34:59 -05001169
Mike Reed3ae47332019-01-04 10:11:46 -05001170 return true;
1171 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001172 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001173 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001174 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001175 }
Ben Wagner9613e452019-01-23 10:34:59 -05001176 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001177 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001178 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001179 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001180 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001181 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001182 return true;
1183 }
1184 SkPaint* fPaint;
1185 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001186 SkFont* fFont;
1187 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001188};
1189
Robert Phillips9882dae2019-03-04 11:00:10 -05001190void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001191 if (fCurrentSlide < 0) {
1192 return;
1193 }
1194
Robert Phillips9882dae2019-03-04 11:00:10 -05001195 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001196
Brian Osmanf750fbc2017-02-08 10:47:28 -05001197 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001198 SkSurface* slideSurface = surface;
1199 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001200 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001201
Brian Osmane0d4fba2017-03-15 10:24:55 -04001202 // If we're in any of the color managed modes, construct the color space we're going to use
Brian Osman03115dc2018-11-26 13:55:19 -05001203 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001204 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001205 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001206 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001207 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001208 }
1209
Brian Osman3ac99cf2017-12-01 11:23:53 -05001210 if (fSaveToSKP) {
1211 SkPictureRecorder recorder;
1212 SkCanvas* recorderCanvas = recorder.beginRecording(
1213 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001214 fSlides[fCurrentSlide]->draw(recorderCanvas);
1215 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1216 SkFILEWStream stream("sample_app.skp");
1217 picture->serialize(&stream);
1218 fSaveToSKP = false;
1219 }
1220
Brian Osmane9ed0f02018-11-26 14:50:05 -05001221 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
1222 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode) ? kRGBA_F16_SkColorType
1223 : kN32_SkColorType;
Brian Osmane9ed0f02018-11-26 14:50:05 -05001224
1225 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001226 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1227 slideCanvas->getProps(&props);
1228
Brian Osmane9ed0f02018-11-26 14:50:05 -05001229 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1230 return Window::kRaster_BackendType == this->fBackendType
1231 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001232 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001233 };
1234
Brian Osman03115dc2018-11-26 13:55:19 -05001235 // We need to render offscreen if we're...
1236 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1237 // ... in any raster mode, because the window surface is actually GL
1238 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001239 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001240 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001241 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001242 Window::kRaster_BackendType == fBackendType ||
1243 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001244
Brian Osmane9ed0f02018-11-26 14:50:05 -05001245 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001246 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001247 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001248 }
1249
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001250 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001251 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001252 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001253 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001254 if (fTiled) {
1255 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1256 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1257 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1258 SkCanvas* tileCanvas = tileSurface->getCanvas();
1259 SkMatrix m = this->computeMatrix();
1260 for (int y = 0; y < fWindow->height(); y += tileH) {
1261 for (int x = 0; x < fWindow->width(); x += tileW) {
1262 SkAutoCanvasRestore acr(tileCanvas, true);
1263 tileCanvas->translate(-x, -y);
1264 tileCanvas->clear(SK_ColorTRANSPARENT);
1265 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001266 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1267 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001268 fSlides[fCurrentSlide]->draw(&filterCanvas);
1269 tileSurface->draw(slideCanvas, x, y, nullptr);
1270 }
1271 }
1272
1273 // Draw borders between tiles
1274 if (fDrawTileBoundaries) {
1275 SkPaint border;
1276 border.setColor(0x60FF00FF);
1277 border.setStyle(SkPaint::kStroke_Style);
1278 for (int y = 0; y < fWindow->height(); y += tileH) {
1279 for (int x = 0; x < fWindow->width(); x += tileW) {
1280 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1281 }
1282 }
1283 }
1284 } else {
1285 slideCanvas->concat(this->computeMatrix());
1286 if (kPerspective_Real == fPerspectiveMode) {
1287 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1288 }
Mike Reed3ae47332019-01-04 10:11:46 -05001289 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001290 fSlides[fCurrentSlide]->draw(&filterCanvas);
1291 }
Brian Osman56a24812017-12-19 11:15:16 -05001292 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001293 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001294
1295 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001296 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001297 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001298 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001299
1300 // If we rendered offscreen, snap an image and push the results to the window's canvas
1301 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001302 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001303
Robert Phillips9882dae2019-03-04 11:00:10 -05001304 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001305 SkPaint paint;
1306 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001307 int prePerspectiveCount = canvas->save();
1308 if (kPerspective_Fake == fPerspectiveMode) {
1309 paint.setFilterQuality(kHigh_SkFilterQuality);
1310 canvas->clear(SK_ColorWHITE);
1311 canvas->concat(this->computePerspectiveMatrix());
1312 }
Brian Osman03115dc2018-11-26 13:55:19 -05001313 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001314 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001315 }
Mike Reed376d8122019-03-14 11:39:02 -04001316
1317 if (fShowSlideDimensions) {
1318 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1319 SkPaint paint;
1320 paint.setColor(0x40FFFF00);
1321 surface->getCanvas()->drawRect(r, paint);
1322 }
liyuqian6f163d22016-06-13 12:26:45 -07001323}
1324
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001325void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001326 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001327 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001328}
Jim Van Verth6f449692017-02-14 15:16:46 -05001329
Robert Phillips9882dae2019-03-04 11:00:10 -05001330void Viewer::onPaint(SkSurface* surface) {
1331 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001332
Robert Phillips9882dae2019-03-04 11:00:10 -05001333 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001334
Brian Osmand67e5182017-12-08 16:46:09 -05001335 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001336
1337 if (GrContext* ctx = fWindow->getGrContext()) {
1338 // Clean out cache items that haven't been used in more than 10 seconds.
1339 ctx->performDeferredCleanup(std::chrono::seconds(10));
1340 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001341}
1342
Ben Wagnera1915972018-08-09 15:06:19 -04001343void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001344 if (fCurrentSlide >= 0) {
1345 fSlides[fCurrentSlide]->resize(width, height);
1346 }
Ben Wagnera1915972018-08-09 15:06:19 -04001347}
1348
Florin Malitacefc1b92018-02-19 21:43:47 -05001349SkPoint Viewer::mapEvent(float x, float y) {
1350 const auto m = this->computeMatrix();
1351 SkMatrix inv;
1352
1353 SkAssertResult(m.invert(&inv));
1354
1355 return inv.mapXY(x, y);
1356}
1357
jvanverth814e38d2016-06-06 08:48:47 -07001358bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001359 if (GestureDevice::kMouse == fGestureDevice) {
1360 return false;
1361 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001362
1363 const auto slidePt = this->mapEvent(x, y);
1364 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1365 fWindow->inval();
1366 return true;
1367 }
1368
liyuqiand3cdbca2016-05-17 12:44:20 -07001369 void* castedOwner = reinterpret_cast<void*>(owner);
1370 switch (state) {
1371 case Window::kUp_InputState: {
1372 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001373#if defined(SK_BUILD_FOR_IOS)
1374 // TODO: move IOS swipe detection higher up into the platform code
1375 SkPoint dir;
1376 if (fGesture.isFling(&dir)) {
1377 // swiping left or right
1378 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1379 if (dir.fX < 0) {
1380 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1381 fCurrentSlide + 1 : 0);
1382 } else {
1383 this->setCurrentSlide(fCurrentSlide > 0 ?
1384 fCurrentSlide - 1 : fSlides.count() - 1);
1385 }
1386 }
1387 fGesture.reset();
1388 }
1389#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001390 break;
1391 }
1392 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001393 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001394 break;
1395 }
1396 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001397 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001398 break;
1399 }
1400 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001401 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001402 fWindow->inval();
1403 return true;
1404}
1405
Brian Osman80fc07e2017-12-08 16:45:43 -05001406bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001407 if (GestureDevice::kTouch == fGestureDevice) {
1408 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001409 }
Brian Osman16c81a12017-12-20 11:58:34 -05001410
Florin Malitacefc1b92018-02-19 21:43:47 -05001411 const auto slidePt = this->mapEvent(x, y);
1412 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1413 fWindow->inval();
1414 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001415 }
1416
1417 switch (state) {
1418 case Window::kUp_InputState: {
1419 fGesture.touchEnd(nullptr);
1420 break;
1421 }
1422 case Window::kDown_InputState: {
1423 fGesture.touchBegin(nullptr, x, y);
1424 break;
1425 }
1426 case Window::kMove_InputState: {
1427 fGesture.touchMoved(nullptr, x, y);
1428 break;
1429 }
1430 }
1431 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1432
1433 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1434 fWindow->inval();
1435 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001436 return true;
1437}
1438
Brian Osmana109e392017-02-24 09:49:14 -05001439static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001440 // The gamut image covers a (0.8 x 0.9) shaped region
1441 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001442
1443 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1444 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1445 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001446 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1447 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1448 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001449
Brian Osman535c5e32019-02-09 16:32:58 -05001450 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1451 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1452 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1453 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1454 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001455}
1456
Ben Wagner3627d2e2018-06-26 14:23:20 -04001457static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001458 ImGui::DragCanvas dc(pt);
1459 dc.fillColor(IM_COL32(0, 0, 0, 128));
1460 dc.dragPoint(pt);
1461 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001462}
1463
Brian Osman9bb47cf2018-04-26 15:55:00 -04001464static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001465 ImGui::DragCanvas dc(pts);
1466 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001467
Brian Osman535c5e32019-02-09 16:32:58 -05001468 for (int i = 0; i < 4; ++i) {
1469 dc.dragPoint(pts + i);
1470 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001471
Brian Osman535c5e32019-02-09 16:32:58 -05001472 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1473 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1474 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1475 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001476
Brian Osman535c5e32019-02-09 16:32:58 -05001477 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001478}
1479
Brian Osmand67e5182017-12-08 16:46:09 -05001480void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001481 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1482 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001483 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001484 }
1485
1486 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001487 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1488 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001489 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001490 DisplayParams params = fWindow->getRequestedDisplayParams();
1491 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001492 const GrContext* ctx = fWindow->getGrContext();
1493
Brian Osmana109e392017-02-24 09:49:14 -05001494 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1495 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001496 if (ImGui::CollapsingHeader("Backend")) {
1497 int newBackend = static_cast<int>(fBackendType);
1498 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1499 ImGui::SameLine();
1500 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001501#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1502 ImGui::SameLine();
1503 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1504#endif
Brian Osman621491e2017-02-28 15:45:01 -05001505#if defined(SK_VULKAN)
1506 ImGui::SameLine();
1507 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1508#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -05001509#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
1510 ImGui::SameLine();
1511 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1512#endif
Brian Osman621491e2017-02-28 15:45:01 -05001513 if (newBackend != fBackendType) {
1514 fDeferredActions.push_back([=]() {
1515 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1516 });
1517 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001518
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001519 bool* wire = &params.fGrContextOptions.fWireframeMode;
1520 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1521 paramsChanged = true;
1522 }
Brian Salomon99a33902017-03-07 15:16:34 -05001523
Brian Osman28b12522017-03-08 17:10:24 -05001524 if (ctx) {
1525 int sampleCount = fWindow->sampleCount();
1526 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001527 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001528 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1529 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1530 ImGui::RadioButton("16", &sampleCount, 16);
1531
1532 if (sampleCount != params.fMSAASampleCount) {
1533 params.fMSAASampleCount = sampleCount;
1534 paramsChanged = true;
1535 }
1536 }
1537
Ben Wagner37c54032018-04-13 14:30:23 -04001538 int pixelGeometryIdx = 0;
1539 if (fPixelGeometryOverrides) {
1540 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1541 }
1542 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1543 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1544 {
1545 uint32_t flags = params.fSurfaceProps.flags();
1546 if (pixelGeometryIdx == 0) {
1547 fPixelGeometryOverrides = false;
1548 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1549 } else {
1550 fPixelGeometryOverrides = true;
1551 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1552 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1553 }
1554 paramsChanged = true;
1555 }
1556
1557 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1558 if (ImGui::Checkbox("DFT", &useDFT)) {
1559 uint32_t flags = params.fSurfaceProps.flags();
1560 if (useDFT) {
1561 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1562 } else {
1563 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1564 }
1565 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1566 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1567 paramsChanged = true;
1568 }
1569
Brian Osman8a9de3d2017-03-01 14:59:05 -05001570 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001571 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001572 auto prButton = [&](GpuPathRenderers x) {
1573 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001574 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1575 params.fGrContextOptions.fGpuPathRenderers = x;
1576 paramsChanged = true;
1577 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001578 }
1579 };
1580
1581 if (!ctx) {
1582 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001583 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001584 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001585 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001586 prButton(GpuPathRenderers::kStencilAndCover);
1587 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001588 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001589 prButton(GpuPathRenderers::kNone);
1590 } else {
1591 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001592 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001593 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001594 prButton(GpuPathRenderers::kCoverageCounting);
1595 }
Jim Van Verth83010462017-03-16 08:45:39 -04001596 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001597 prButton(GpuPathRenderers::kTessellating);
1598 prButton(GpuPathRenderers::kNone);
1599 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001600 ImGui::TreePop();
1601 }
Brian Osman621491e2017-02-28 15:45:01 -05001602 }
1603
Ben Wagner964571d2019-03-08 12:35:06 -05001604 if (ImGui::CollapsingHeader("Tiling")) {
1605 ImGui::Checkbox("Enable", &fTiled);
1606 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1607 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1608 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1609 }
1610
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001611 if (ImGui::CollapsingHeader("Transform")) {
1612 float zoom = fZoomLevel;
1613 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1614 fZoomLevel = zoom;
1615 this->preTouchMatrixChanged();
1616 paramsChanged = true;
1617 }
1618 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001619 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001620 fRotation = deg;
1621 this->preTouchMatrixChanged();
1622 paramsChanged = true;
1623 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001624 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1625 if (ImGui_DragLocation(&fOffset)) {
1626 this->preTouchMatrixChanged();
1627 paramsChanged = true;
1628 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001629 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1630 this->preTouchMatrixChanged();
1631 paramsChanged = true;
1632 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001633 }
Brian Osman805a7272018-05-02 15:40:20 -04001634 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1635 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1636 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001637 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001638 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001639 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001640 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001641 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001642 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001643 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001644 }
1645
Ben Wagnera580fb32018-04-17 11:16:32 -04001646 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001647 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001648 if (fPaintOverrides.fAntiAlias) {
1649 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001650 }
1651 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001652 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001653 {
1654 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1655 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001656 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001657 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1658 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001659 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001660 fPaintOverrides.fAntiAlias = true;
1661 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001662 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001663 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001664 case SkPaintFields::AntiAliasState::Alias:
1665 break;
1666 case SkPaintFields::AntiAliasState::Normal:
1667 break;
1668 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1669 gSkUseAnalyticAA = true;
1670 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001671 break;
1672 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1673 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001674 break;
1675 }
1676 }
1677 paramsChanged = true;
1678 }
1679
Ben Wagner99a78dc2018-05-09 18:23:51 -04001680 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001681 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001682 bool (SkPaint::* isFlag)() const,
1683 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001684 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001685 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001686 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001687 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001688 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001689 if (ImGui::Combo(label, &itemIndex, items)) {
1690 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001691 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001692 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001693 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001694 (fPaint.*setFlag)(itemIndex == 2);
1695 }
1696 paramsChanged = true;
1697 }
1698 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001699
Ben Wagner99a78dc2018-05-09 18:23:51 -04001700 paintFlag("Dither",
1701 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001702 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001703 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001704
1705 int filterQualityIdx = 0;
1706 if (fPaintOverrides.fFilterQuality) {
1707 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1708 }
1709 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1710 "Default\0None\0Low\0Medium\0High\0\0"))
1711 {
1712 if (filterQualityIdx == 0) {
1713 fPaintOverrides.fFilterQuality = false;
1714 fPaint.setFilterQuality(kNone_SkFilterQuality);
1715 } else {
1716 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1717 fPaintOverrides.fFilterQuality = true;
1718 }
1719 paramsChanged = true;
1720 }
Ben Wagner9613e452019-01-23 10:34:59 -05001721 }
Hal Canary02738a82019-01-21 18:51:32 +00001722
Ben Wagner9613e452019-01-23 10:34:59 -05001723 if (ImGui::CollapsingHeader("Font")) {
1724 int hintingIdx = 0;
1725 if (fFontOverrides.fHinting) {
1726 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1727 }
1728 if (ImGui::Combo("Hinting", &hintingIdx,
1729 "Default\0None\0Slight\0Normal\0Full\0\0"))
1730 {
1731 if (hintingIdx == 0) {
1732 fFontOverrides.fHinting = false;
1733 fFont.setHinting(kNo_SkFontHinting);
1734 } else {
1735 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1736 fFontOverrides.fHinting = true;
1737 }
1738 paramsChanged = true;
1739 }
Hal Canary02738a82019-01-21 18:51:32 +00001740
Ben Wagner9613e452019-01-23 10:34:59 -05001741 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1742 bool SkFontFields::* flag,
1743 bool (SkFont::* isFlag)() const,
1744 void (SkFont::* setFlag)(bool) )
1745 {
1746 int itemIndex = 0;
1747 if (fFontOverrides.*flag) {
1748 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1749 }
1750 if (ImGui::Combo(label, &itemIndex, items)) {
1751 if (itemIndex == 0) {
1752 fFontOverrides.*flag = false;
1753 } else {
1754 fFontOverrides.*flag = true;
1755 (fFont.*setFlag)(itemIndex == 2);
1756 }
1757 paramsChanged = true;
1758 }
1759 };
Hal Canary02738a82019-01-21 18:51:32 +00001760
Ben Wagner9613e452019-01-23 10:34:59 -05001761 fontFlag("Fake Bold Glyphs",
1762 "Default\0No Fake Bold\0Fake Bold\0\0",
1763 &SkFontFields::fEmbolden,
1764 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001765
Ben Wagner9613e452019-01-23 10:34:59 -05001766 fontFlag("Linear Text",
1767 "Default\0No Linear Text\0Linear Text\0\0",
1768 &SkFontFields::fLinearMetrics,
1769 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001770
Ben Wagner9613e452019-01-23 10:34:59 -05001771 fontFlag("Subpixel Position Glyphs",
1772 "Default\0Pixel Text\0Subpixel Text\0\0",
1773 &SkFontFields::fSubpixel,
1774 &SkFont::isSubpixel, &SkFont::setSubpixel);
1775
1776 fontFlag("Embedded Bitmap Text",
1777 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1778 &SkFontFields::fEmbeddedBitmaps,
1779 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1780
1781 fontFlag("Force Auto-Hinting",
1782 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1783 &SkFontFields::fForceAutoHinting,
1784 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1785
1786 int edgingIdx = 0;
1787 if (fFontOverrides.fEdging) {
1788 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1789 }
1790 if (ImGui::Combo("Edging", &edgingIdx,
1791 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1792 {
1793 if (edgingIdx == 0) {
1794 fFontOverrides.fEdging = false;
1795 fFont.setEdging(SkFont::Edging::kAlias);
1796 } else {
1797 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1798 fFontOverrides.fEdging = true;
1799 }
1800 paramsChanged = true;
1801 }
1802
Ben Wagner15a8d572019-03-21 13:35:44 -04001803 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1804 if (fFontOverrides.fSize) {
1805 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001806 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001807 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001808 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001809 fFontOverrides.fSizeRange[0],
1810 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001811 "%.6f", 2.0f))
1812 {
Mike Reed3ae47332019-01-04 10:11:46 -05001813 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001814 paramsChanged = true;
1815 }
1816 }
1817
1818 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1819 if (fFontOverrides.fScaleX) {
1820 float scaleX = fFont.getScaleX();
1821 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1822 fFont.setScaleX(scaleX);
1823 paramsChanged = true;
1824 }
1825 }
1826
1827 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1828 if (fFontOverrides.fSkewX) {
1829 float skewX = fFont.getSkewX();
1830 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1831 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001832 paramsChanged = true;
1833 }
1834 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001835 }
1836
Mike Reed81f60ec2018-05-15 10:09:52 -04001837 {
1838 SkMetaData controls;
1839 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1840 if (ImGui::CollapsingHeader("Current Slide")) {
1841 SkMetaData::Iter iter(controls);
1842 const char* name;
1843 SkMetaData::Type type;
1844 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001845 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001846 if (type == SkMetaData::kScalar_Type) {
1847 float val[3];
1848 SkASSERT(count == 3);
1849 controls.findScalars(name, &count, val);
1850 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1851 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001852 }
Ben Wagner110c7032019-03-22 17:03:59 -04001853 } else if (type == SkMetaData::kBool_Type) {
1854 bool val;
1855 SkASSERT(count == 1);
1856 controls.findBool(name, &val);
1857 if (ImGui::Checkbox(name, &val)) {
1858 controls.setBool(name, val);
1859 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001860 }
1861 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001862 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001863 }
1864 }
1865 }
1866
Ben Wagner7a3c6742018-04-23 10:01:07 -04001867 if (fShowSlidePicker) {
1868 ImGui::SetNextTreeNodeOpen(true);
1869 }
Brian Osman79086b92017-02-10 13:36:16 -05001870 if (ImGui::CollapsingHeader("Slide")) {
1871 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001872 static ImVector<const char*> filteredSlideNames;
1873 static ImVector<int> filteredSlideIndices;
1874
Brian Osmanfce09c52017-11-14 15:32:20 -05001875 if (fShowSlidePicker) {
1876 ImGui::SetKeyboardFocusHere();
1877 fShowSlidePicker = false;
1878 }
1879
Brian Osman79086b92017-02-10 13:36:16 -05001880 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001881 filteredSlideNames.clear();
1882 filteredSlideIndices.clear();
1883 int filteredIndex = 0;
1884 for (int i = 0; i < fSlides.count(); ++i) {
1885 const char* slideName = fSlides[i]->getName().c_str();
1886 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1887 if (i == fCurrentSlide) {
1888 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001889 }
Brian Osmanf479e422017-11-08 13:11:36 -05001890 filteredSlideNames.push_back(slideName);
1891 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001892 }
Brian Osman79086b92017-02-10 13:36:16 -05001893 }
Brian Osmanf479e422017-11-08 13:11:36 -05001894
Brian Osmanf479e422017-11-08 13:11:36 -05001895 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1896 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001897 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001898 }
1899 }
Brian Osmana109e392017-02-24 09:49:14 -05001900
1901 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001902 ColorMode newMode = fColorMode;
1903 auto cmButton = [&](ColorMode mode, const char* label) {
1904 if (ImGui::RadioButton(label, mode == fColorMode)) {
1905 newMode = mode;
1906 }
1907 };
1908
1909 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001910 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1911 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001912
1913 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001914 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001915 }
1916
1917 // Pick from common gamuts:
1918 int primariesIdx = 4; // Default: Custom
1919 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1920 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1921 primariesIdx = i;
1922 break;
1923 }
1924 }
1925
Brian Osman03115dc2018-11-26 13:55:19 -05001926 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05001927 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001928
Brian Osmana109e392017-02-24 09:49:14 -05001929 if (ImGui::Combo("Primaries", &primariesIdx,
1930 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1931 if (primariesIdx >= 0 && primariesIdx <= 3) {
1932 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1933 }
1934 }
1935
1936 // Allow direct editing of gamut
1937 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1938 }
Brian Osman207d4102019-01-10 09:40:58 -05001939
1940 if (ImGui::CollapsingHeader("Animation")) {
1941 bool isPaused = fAnimTimer.isPaused();
1942 if (ImGui::Checkbox("Pause", &isPaused)) {
1943 fAnimTimer.togglePauseResume();
1944 }
Brian Osman707d2022019-01-10 11:27:34 -05001945
1946 float speed = fAnimTimer.getSpeed();
1947 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
1948 fAnimTimer.setSpeed(speed);
1949 }
Brian Osman207d4102019-01-10 09:40:58 -05001950 }
Brian Osman0b8bb882019-04-12 11:47:19 -04001951
Brian Osmanfd7657c2019-04-25 11:34:07 -04001952 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
1953#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1954 || Window::kANGLE_BackendType == fBackendType
1955#endif
1956 ;
1957
1958 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
1959 // than GL, we need to force it on. Just do that on the first frame after the backend
1960 // switch, then resume normal operation.
1961 if (!backendIsGL && !params.fGrContextOptions.fCacheSKSL) {
1962 params.fGrContextOptions.fCacheSKSL = true;
1963 paramsChanged = true;
1964 fPersistentCache.reset();
1965 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04001966 // To re-load shaders from the currently active programs, we flush all caches on one
1967 // frame, then set a flag to poll the cache on the next frame.
1968 static bool gLoadPending = false;
1969 if (gLoadPending) {
1970 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
1971 int hitCount) {
1972 CachedGLSL& entry(fCachedGLSL.push_back());
1973 entry.fKey = key;
1974 SkMD5 hash;
1975 hash.write(key->bytes(), key->size());
1976 SkMD5::Digest digest = hash.finish();
1977 for (int i = 0; i < 16; ++i) {
1978 entry.fKeyString.appendf("%02x", digest.data[i]);
1979 }
1980
Brian Osmana085a412019-04-25 09:44:43 -04001981 entry.fShaderType = GrPersistentCacheUtils::UnpackCachedShaders(
1982 data.get(), entry.fShader, entry.fInputs, kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04001983 };
1984 fCachedGLSL.reset();
1985 fPersistentCache.foreach(collectShaders);
1986 gLoadPending = false;
1987 }
1988
1989 // Defer actually doing the load/save logic so that we can trigger a save when we
1990 // start or finish hovering on a tree node in the list below:
1991 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04001992 bool doSave = ImGui::Button("Save");
1993 if (backendIsGL) {
1994 ImGui::SameLine();
1995 if (ImGui::Checkbox("SkSL", &params.fGrContextOptions.fCacheSKSL)) {
1996 paramsChanged = true;
1997 doLoad = true;
1998 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
1999 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002000 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002001
2002 ImGui::BeginChild("##ScrollingRegion");
2003 for (auto& entry : fCachedGLSL) {
2004 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2005 bool hovered = ImGui::IsItemHovered();
2006 if (hovered != entry.fHovered) {
2007 // Force a save to patch the highlight shader in/out
2008 entry.fHovered = hovered;
2009 doSave = true;
2010 }
2011 if (inTreeNode) {
2012 // Full width, and a reasonable amount of space for each shader.
2013 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2014 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2015 boxSize);
2016 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2017 boxSize);
2018 ImGui::TreePop();
2019 }
2020 }
2021 ImGui::EndChild();
2022
2023 if (doLoad) {
2024 fPersistentCache.reset();
2025 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2026 gLoadPending = true;
2027 }
2028 if (doSave) {
2029 // The hovered item (if any) gets a special shader to make it identifiable
2030 SkSL::String highlight = ctx->priv().caps()->shaderCaps()->versionDeclString();
Brian Osmancbc33b82019-04-19 14:16:19 -04002031 const char* f4Type = params.fGrContextOptions.fCacheSKSL ? "half4" : "vec4";
2032 highlight.appendf("out %s sk_FragColor;\n"
2033 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2034 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002035
2036 fPersistentCache.reset();
2037 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2038 for (auto& entry : fCachedGLSL) {
2039 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2040 if (entry.fHovered) {
2041 entry.fShader[kFragment_GrShaderType] = highlight;
2042 }
2043
Brian Osmana085a412019-04-25 09:44:43 -04002044 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2045 entry.fShader,
2046 entry.fInputs,
2047 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002048 fPersistentCache.store(*entry.fKey, *data);
2049
2050 entry.fShader[kFragment_GrShaderType] = backup;
2051 }
2052 }
2053 }
Brian Osman79086b92017-02-10 13:36:16 -05002054 }
Brian Salomon99a33902017-03-07 15:16:34 -05002055 if (paramsChanged) {
2056 fDeferredActions.push_back([=]() {
2057 fWindow->setRequestedDisplayParams(params);
2058 fWindow->inval();
2059 this->updateTitle();
2060 });
2061 }
Brian Osman79086b92017-02-10 13:36:16 -05002062 ImGui::End();
2063 }
2064
Brian Osmanf6877092017-02-13 09:39:57 -05002065 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002066 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2067 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002068 static int zoomFactor = 8;
2069 if (ImGui::Button("<<")) {
2070 zoomFactor = SkTMax(zoomFactor / 2, 4);
2071 }
2072 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2073 if (ImGui::Button(">>")) {
2074 zoomFactor = SkTMin(zoomFactor * 2, 32);
2075 }
Brian Osmanf6877092017-02-13 09:39:57 -05002076
Ben Wagner3627d2e2018-06-26 14:23:20 -04002077 if (!fZoomWindowFixed) {
2078 ImVec2 mousePos = ImGui::GetMousePos();
2079 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2080 }
2081 SkScalar x = fZoomWindowLocation.x();
2082 SkScalar y = fZoomWindowLocation.y();
2083 int xInt = SkScalarRoundToInt(x);
2084 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002085 ImVec2 avail = ImGui::GetContentRegionAvail();
2086
Brian Osmanead517d2017-11-13 15:36:36 -05002087 uint32_t pixel = 0;
2088 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002089 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002090 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002091 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002092 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002093 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002094 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2095 }
2096
Brian Osmand67e5182017-12-08 16:46:09 -05002097 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002098 // Translate so the region of the image that's under the mouse cursor is centered
2099 // in the zoom canvas:
2100 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002101 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2102 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002103 c->drawImage(this->fLastImage, 0, 0);
2104
2105 SkPaint outline;
2106 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002107 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002108 });
Brian Osmanf6877092017-02-13 09:39:57 -05002109 }
2110
2111 ImGui::End();
2112 }
Brian Osman79086b92017-02-10 13:36:16 -05002113}
2114
liyuqian2edb0f42016-07-06 14:11:32 -07002115void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002116 for (int i = 0; i < fDeferredActions.count(); ++i) {
2117 fDeferredActions[i]();
2118 }
2119 fDeferredActions.reset();
2120
Brian Osman56a24812017-12-19 11:15:16 -05002121 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002122 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05002123 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05002124 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002125
Brian Osman79086b92017-02-10 13:36:16 -05002126 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002127 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2128 // not be visible, though. So we need to redraw if there is at least one visible window, or
2129 // more than one active window. Newly created windows are active but not visible for one frame
2130 // while they determine their layout and sizing.
2131 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2132 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002133 fWindow->inval();
2134 }
jvanverth9f372462016-04-06 06:08:59 -07002135}
liyuqiane5a6cd92016-05-27 08:52:52 -07002136
Florin Malitab632df72018-06-18 21:23:06 -04002137template <typename OptionsFunc>
2138static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2139 OptionsFunc&& optionsFunc) {
2140 writer.beginObject();
2141 {
2142 writer.appendString(kName , name);
2143 writer.appendString(kValue, value);
2144
2145 writer.beginArray(kOptions);
2146 {
2147 optionsFunc(writer);
2148 }
2149 writer.endArray();
2150 }
2151 writer.endObject();
2152}
2153
2154
liyuqiane5a6cd92016-05-27 08:52:52 -07002155void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002156 if (!fWindow) {
2157 return;
2158 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002159 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002160 return; // Surface hasn't been created yet.
2161 }
2162
Florin Malitab632df72018-06-18 21:23:06 -04002163 SkDynamicMemoryWStream memStream;
2164 SkJSONWriter writer(&memStream);
2165 writer.beginArray();
2166
liyuqianb73c24b2016-06-03 08:47:23 -07002167 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002168 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2169 [this](SkJSONWriter& writer) {
2170 for(const auto& slide : fSlides) {
2171 writer.appendString(slide->getName().c_str());
2172 }
2173 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002174
liyuqianb73c24b2016-06-03 08:47:23 -07002175 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002176 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2177 [](SkJSONWriter& writer) {
2178 for (const auto& str : kBackendTypeStrings) {
2179 writer.appendString(str);
2180 }
2181 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002182
csmartdalton578f0642017-02-24 16:04:47 -07002183 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002184 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2185 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2186 [this](SkJSONWriter& writer) {
2187 writer.appendS32(0);
2188
2189 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2190 return;
2191 }
2192
2193 for (int msaa : {4, 8, 16}) {
2194 writer.appendS32(msaa);
2195 }
2196 });
csmartdalton578f0642017-02-24 16:04:47 -07002197
csmartdalton61cd31a2017-02-27 17:00:53 -07002198 // Path renderer state
2199 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002200 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2201 [this](SkJSONWriter& writer) {
2202 const GrContext* ctx = fWindow->getGrContext();
2203 if (!ctx) {
2204 writer.appendString("Software");
2205 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002206 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002207
Florin Malitab632df72018-06-18 21:23:06 -04002208 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2209 if (fWindow->sampleCount() > 1) {
2210 if (caps->shaderCaps()->pathRenderingSupport()) {
2211 writer.appendString(
2212 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2213 }
2214 } else {
2215 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2216 writer.appendString(
2217 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2218 }
2219 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2220 }
2221 writer.appendString(
2222 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2223 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2224 }
2225 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002226
liyuqianb73c24b2016-06-03 08:47:23 -07002227 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002228 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2229 [this](SkJSONWriter& writer) {
2230 writer.appendString(kSoftkeyHint);
2231 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2232 writer.appendString(softkey.c_str());
2233 }
2234 });
liyuqianb73c24b2016-06-03 08:47:23 -07002235
Florin Malitab632df72018-06-18 21:23:06 -04002236 writer.endArray();
2237 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002238
Florin Malitab632df72018-06-18 21:23:06 -04002239 auto data = memStream.detachAsData();
2240
2241 // TODO: would be cool to avoid this copy
2242 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2243
2244 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002245}
2246
2247void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002248 // For those who will add more features to handle the state change in this function:
2249 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2250 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2251 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002252 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002253 for (int i = 0; i < fSlides.count(); ++i) {
2254 if (fSlides[i]->getName().equals(stateValue)) {
2255 this->setCurrentSlide(i);
2256 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002257 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002258 }
Florin Malitaab99c342018-01-16 16:23:03 -05002259
2260 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002261 } else if (stateName.equals(kBackendStateName)) {
2262 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2263 if (stateValue.equals(kBackendTypeStrings[i])) {
2264 if (fBackendType != i) {
2265 fBackendType = (sk_app::Window::BackendType)i;
2266 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002267 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002268 }
2269 break;
2270 }
2271 }
csmartdalton578f0642017-02-24 16:04:47 -07002272 } else if (stateName.equals(kMSAAStateName)) {
2273 DisplayParams params = fWindow->getRequestedDisplayParams();
2274 int sampleCount = atoi(stateValue.c_str());
2275 if (sampleCount != params.fMSAASampleCount) {
2276 params.fMSAASampleCount = sampleCount;
2277 fWindow->setRequestedDisplayParams(params);
2278 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002279 this->updateTitle();
2280 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002281 }
2282 } else if (stateName.equals(kPathRendererStateName)) {
2283 DisplayParams params = fWindow->getRequestedDisplayParams();
2284 for (const auto& pair : gPathRendererNames) {
2285 if (pair.second == stateValue.c_str()) {
2286 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2287 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2288 fWindow->setRequestedDisplayParams(params);
2289 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002290 this->updateTitle();
2291 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002292 }
2293 break;
2294 }
csmartdalton578f0642017-02-24 16:04:47 -07002295 }
liyuqianb73c24b2016-06-03 08:47:23 -07002296 } else if (stateName.equals(kSoftkeyStateName)) {
2297 if (!stateValue.equals(kSoftkeyHint)) {
2298 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002299 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002300 }
liyuqian2edb0f42016-07-06 14:11:32 -07002301 } else if (stateName.equals(kRefreshStateName)) {
2302 // This state is actually NOT in the UI state.
2303 // We use this to allow Android to quickly set bool fRefresh.
2304 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002305 } else {
2306 SkDebugf("Unknown stateName: %s", stateName.c_str());
2307 }
2308}
Brian Osman79086b92017-02-10 13:36:16 -05002309
2310bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002311 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002312}
2313
2314bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002315 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002316 fWindow->inval();
2317 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002318 } else {
2319 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002320 }
Brian Osman79086b92017-02-10 13:36:16 -05002321}