blob: cd74346deac034de06b9f0ca3055b780ff0db607 [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
Brian Osman5e7fbfd2019-05-03 13:13:35 -040059class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
60public:
61 void compileError(const char* shader, const char* errors) override {
62 fShaders.push_back(SkString(shader));
63 fErrors.push_back(SkString(errors));
64 }
65
66 void reset() {
67 fShaders.reset();
68 fErrors.reset();
69 }
70
71 SkTArray<SkString> fShaders;
72 SkTArray<SkString> fErrors;
73};
74
75static CapturingShaderErrorHandler gShaderErrorHandler;
76
jvanverth34524262016-05-04 13:49:13 -070077using namespace sk_app;
78
csmartdalton61cd31a2017-02-27 17:00:53 -070079static std::map<GpuPathRenderers, std::string> gPathRendererNames;
80
jvanverth9f372462016-04-06 06:08:59 -070081Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070082 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070083}
84
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060085static DEFINE_string(slide, "", "Start on this sample.");
86static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050087
bsalomon6c471f72016-07-26 12:56:32 -070088#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070089# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050090#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
91# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
bsalomon6c471f72016-07-26 12:56:32 -070092#else
93# define BACKENDS_STR "\"sw\" and \"gl\""
94#endif
95
Brian Osman2dd96932016-10-18 15:33:53 -040096static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070097
Mike Klein5b3f3432019-03-21 11:42:21 -050098static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070099
Mike Klein84836b72019-03-21 11:31:36 -0500100static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700101
Mike Klein84836b72019-03-21 11:31:36 -0500102static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400103
Mike Kleinc6142d82019-03-25 10:54:59 -0500104static DEFINE_string2(match, m, nullptr,
105 "[~][^]substring[$] [...] of name to run.\n"
106 "Multiple matches may be separated by spaces.\n"
107 "~ causes a matching name to always be skipped\n"
108 "^ requires the start of the name to match\n"
109 "$ requires the end of the name to match\n"
110 "^ and $ requires an exact match\n"
111 "If a name does not match any list entry,\n"
112 "it is skipped unless some list entry starts with ~");
113
Mike Klein19fb3972019-03-21 13:08:08 -0500114#if defined(SK_BUILD_FOR_ANDROID)
115 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
116 static DEFINE_string(nimas, "/data/local/tmp/nimas", "Directory to read NIMA animations from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500117 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
118 static DEFINE_string(lotties, "/data/local/tmp/lotties",
119 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500120#else
121 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
122 static DEFINE_string(nimas, "nimas", "Directory to read NIMA animations from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500123 static DEFINE_string(skps, "skps", "Directory to read skps from.");
124 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500125#endif
126
Mike Kleinc6142d82019-03-25 10:54:59 -0500127static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
128
129static DEFINE_int_2(threads, j, -1,
130 "Run threadsafe tests on a threadpool with this many extra threads, "
131 "defaulting to one extra thread per core.");
132
133
Brian Salomon194db172017-08-17 14:37:06 -0400134const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700135 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400136#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
137 "ANGLE",
138#endif
jvanverth063ece72016-06-17 09:29:14 -0700139#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700140 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700141#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -0500142#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
143 "Metal",
144#endif
csmartdalton578f0642017-02-24 16:04:47 -0700145 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700146};
147
bsalomon6c471f72016-07-26 12:56:32 -0700148static sk_app::Window::BackendType get_backend_type(const char* str) {
149#ifdef SK_VULKAN
150 if (0 == strcmp(str, "vk")) {
151 return sk_app::Window::kVulkan_BackendType;
152 } else
153#endif
Brian Salomon194db172017-08-17 14:37:06 -0400154#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
155 if (0 == strcmp(str, "angle")) {
156 return sk_app::Window::kANGLE_BackendType;
157 } else
158#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -0500159#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
160 if (0 == strcmp(str, "mtl")) {
161 return sk_app::Window::kMetal_BackendType;
162 } else
163#endif
bsalomon6c471f72016-07-26 12:56:32 -0700164 if (0 == strcmp(str, "gl")) {
165 return sk_app::Window::kNativeGL_BackendType;
166 } else if (0 == strcmp(str, "sw")) {
167 return sk_app::Window::kRaster_BackendType;
168 } else {
169 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
170 return sk_app::Window::kRaster_BackendType;
171 }
172}
173
Brian Osmana109e392017-02-24 09:49:14 -0500174static SkColorSpacePrimaries gSrgbPrimaries = {
175 0.64f, 0.33f,
176 0.30f, 0.60f,
177 0.15f, 0.06f,
178 0.3127f, 0.3290f };
179
180static SkColorSpacePrimaries gAdobePrimaries = {
181 0.64f, 0.33f,
182 0.21f, 0.71f,
183 0.15f, 0.06f,
184 0.3127f, 0.3290f };
185
186static SkColorSpacePrimaries gP3Primaries = {
187 0.680f, 0.320f,
188 0.265f, 0.690f,
189 0.150f, 0.060f,
190 0.3127f, 0.3290f };
191
192static SkColorSpacePrimaries gRec2020Primaries = {
193 0.708f, 0.292f,
194 0.170f, 0.797f,
195 0.131f, 0.046f,
196 0.3127f, 0.3290f };
197
198struct NamedPrimaries {
199 const char* fName;
200 SkColorSpacePrimaries* fPrimaries;
201} gNamedPrimaries[] = {
202 { "sRGB", &gSrgbPrimaries },
203 { "AdobeRGB", &gAdobePrimaries },
204 { "P3", &gP3Primaries },
205 { "Rec. 2020", &gRec2020Primaries },
206};
207
208static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
209 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
210}
211
Brian Osman70d2f432017-11-08 09:54:10 -0500212static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
213 // In raster mode, we still use GL for the window.
214 // This lets us render the GUI faster (and correct).
215 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
216}
217
Jim Van Verth74826c82019-03-01 14:37:30 -0500218class NullSlide : public Slide {
219 SkISize getDimensions() const override {
220 return SkISize::Make(640, 480);
221 }
222
223 void draw(SkCanvas* canvas) override {
224 canvas->clear(0xffff11ff);
225 }
226};
227
liyuqiane5a6cd92016-05-27 08:52:52 -0700228const char* kName = "name";
229const char* kValue = "value";
230const char* kOptions = "options";
231const char* kSlideStateName = "Slide";
232const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700233const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700234const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700235const char* kSoftkeyStateName = "Softkey";
236const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700237const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700238const char* kON = "ON";
239const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700240const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700241
jvanverth34524262016-05-04 13:49:13 -0700242Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500243 : fCurrentSlide(-1)
244 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500245 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400246 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500247 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500248 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500249 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500250 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400251 , fZoomWindowFixed(false)
252 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500253 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400254 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700255 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500256 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500257 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500258 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500259 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700260 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400261 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400262 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400263 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500264 , fTiled(false)
265 , fDrawTileBoundaries(false)
266 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400267 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700268{
Greg Daniel285db442016-10-14 09:12:53 -0400269 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700270
Brian Osmanf09e35e2017-12-15 14:48:09 -0500271 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500272 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500273 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
274 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
275 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
276 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700277
jvanverth2bb3b6d2016-04-08 07:24:09 -0700278 SkDebugf("Command line arguments: ");
279 for (int i = 1; i < argc; ++i) {
280 SkDebugf("%s ", argv[i]);
281 }
282 SkDebugf("\n");
283
Mike Klein88544fb2019-03-20 10:50:33 -0500284 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500285#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400286 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500287#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700288
Mike Klein19cc0f62019-03-22 15:30:07 -0500289 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500290
Brian Osmanbc8150f2017-07-24 11:38:01 -0400291 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400292 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400293
bsalomon6c471f72016-07-26 12:56:32 -0700294 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700295 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700296
csmartdalton578f0642017-02-24 16:04:47 -0700297 DisplayParams displayParams;
298 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700299 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400300 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
301 displayParams.fGrContextOptions.fDisallowGLSLBinaryCaching = true;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400302 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
303 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700304 fWindow->setRequestedDisplayParams(displayParams);
305
Brian Osman56a24812017-12-19 11:15:16 -0500306 // Configure timers
307 fStatsLayer.setActive(false);
308 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
309 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
310 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
311
jvanverth9f372462016-04-06 06:08:59 -0700312 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700313 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500314 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500315 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500316 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700317
brianosman622c8d52016-05-10 06:50:49 -0700318 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500319 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
320 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
321 fWindow->inval();
322 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500323 // Command to jump directly to the slide picker and give it focus
324 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
325 this->fShowImGuiDebugWindow = true;
326 this->fShowSlidePicker = true;
327 fWindow->inval();
328 });
329 // Alias that to Backspace, to match SampleApp
330 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
331 this->fShowImGuiDebugWindow = true;
332 this->fShowSlidePicker = true;
333 fWindow->inval();
334 });
Brian Osman79086b92017-02-10 13:36:16 -0500335 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
336 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
337 fWindow->inval();
338 });
Brian Osmanf6877092017-02-13 09:39:57 -0500339 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
340 this->fShowZoomWindow = !this->fShowZoomWindow;
341 fWindow->inval();
342 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400343 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
344 this->fZoomWindowFixed = !this->fZoomWindowFixed;
345 fWindow->inval();
346 });
Greg Danield0794cc2019-03-27 16:23:26 -0400347 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
348 DisplayParams params = fWindow->getRequestedDisplayParams();
349 params.fDisableVsync = !params.fDisableVsync;
350 fWindow->setRequestedDisplayParams(params);
351 this->updateTitle();
352 fWindow->inval();
353 });
brianosman622c8d52016-05-10 06:50:49 -0700354 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500355 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700356 fWindow->inval();
357 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400358 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500359 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400360 this->updateTitle();
361 fWindow->inval();
362 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500363 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500364 switch (fColorMode) {
365 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500366 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500367 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500368 case ColorMode::kColorManaged8888:
369 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500370 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500371 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500372 this->setColorMode(ColorMode::kLegacy);
373 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500374 }
brianosman622c8d52016-05-10 06:50:49 -0700375 });
376 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500377 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700378 });
379 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500380 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700381 });
382 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
383 this->changeZoomLevel(1.f / 32.f);
384 fWindow->inval();
385 });
386 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
387 this->changeZoomLevel(-1.f / 32.f);
388 fWindow->inval();
389 });
jvanverthaf236b52016-05-20 06:01:06 -0700390 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400391 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
392 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500393 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400394#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
395 if (newBackend == sk_app::Window::kVulkan_BackendType) {
396 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
397 sk_app::Window::kBackendTypeCount);
398 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
399 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500400 }
401#endif
Brian Osman621491e2017-02-28 15:45:01 -0500402 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700403 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500404 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
405 fSaveToSKP = true;
406 fWindow->inval();
407 });
Mike Reed376d8122019-03-14 11:39:02 -0400408 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
409 fShowSlideDimensions = !fShowSlideDimensions;
410 fWindow->inval();
411 });
Ben Wagner37c54032018-04-13 14:30:23 -0400412 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
413 DisplayParams params = fWindow->getRequestedDisplayParams();
414 uint32_t flags = params.fSurfaceProps.flags();
415 if (!fPixelGeometryOverrides) {
416 fPixelGeometryOverrides = true;
417 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
418 } else {
419 switch (params.fSurfaceProps.pixelGeometry()) {
420 case kUnknown_SkPixelGeometry:
421 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
422 break;
423 case kRGB_H_SkPixelGeometry:
424 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
425 break;
426 case kBGR_H_SkPixelGeometry:
427 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
428 break;
429 case kRGB_V_SkPixelGeometry:
430 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
431 break;
432 case kBGR_V_SkPixelGeometry:
433 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
434 fPixelGeometryOverrides = false;
435 break;
436 }
437 }
438 fWindow->setRequestedDisplayParams(params);
439 this->updateTitle();
440 fWindow->inval();
441 });
Ben Wagner9613e452019-01-23 10:34:59 -0500442 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500443 if (!fFontOverrides.fHinting) {
444 fFontOverrides.fHinting = true;
445 fFont.setHinting(kNo_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500446 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500447 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500448 case kNo_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500449 fFont.setHinting(kSlight_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500450 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500451 case kSlight_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500452 fFont.setHinting(kNormal_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500453 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500454 case kNormal_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500455 fFont.setHinting(kFull_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500456 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500457 case kFull_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500458 fFont.setHinting(kNo_SkFontHinting);
459 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500460 break;
461 }
462 }
463 this->updateTitle();
464 fWindow->inval();
465 });
466 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500467 if (!fPaintOverrides.fAntiAlias) {
468 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
469 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500470 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500471 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500472 } else {
473 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500474 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500475 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500476 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400477 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500478 break;
479 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500480 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500481 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400482 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500483 break;
484 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500485 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400486 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500487 break;
488 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500489 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
490 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500491 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
492 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500493 break;
494 }
495 }
496 this->updateTitle();
497 fWindow->inval();
498 });
Ben Wagner37c54032018-04-13 14:30:23 -0400499 fCommands.addCommand('D', "Modes", "DFT", [this]() {
500 DisplayParams params = fWindow->getRequestedDisplayParams();
501 uint32_t flags = params.fSurfaceProps.flags();
502 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
503 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
504 fWindow->setRequestedDisplayParams(params);
505 this->updateTitle();
506 fWindow->inval();
507 });
Ben Wagner9613e452019-01-23 10:34:59 -0500508 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500509 if (!fFontOverrides.fEdging) {
510 fFontOverrides.fEdging = true;
511 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500512 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500513 switch (fFont.getEdging()) {
514 case SkFont::Edging::kAlias:
515 fFont.setEdging(SkFont::Edging::kAntiAlias);
516 break;
517 case SkFont::Edging::kAntiAlias:
518 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
519 break;
520 case SkFont::Edging::kSubpixelAntiAlias:
521 fFont.setEdging(SkFont::Edging::kAlias);
522 fFontOverrides.fEdging = false;
523 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500524 }
525 }
526 this->updateTitle();
527 fWindow->inval();
528 });
Ben Wagner9613e452019-01-23 10:34:59 -0500529 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500530 if (!fFontOverrides.fSubpixel) {
531 fFontOverrides.fSubpixel = true;
532 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500533 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500534 if (!fFont.isSubpixel()) {
535 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500536 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500537 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500538 }
539 }
540 this->updateTitle();
541 fWindow->inval();
542 });
Brian Osman805a7272018-05-02 15:40:20 -0400543 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
544 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
545 : kPerspective_Real;
546 this->updateTitle();
547 fWindow->inval();
548 });
549 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
550 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
551 : kPerspective_Off;
552 this->updateTitle();
553 fWindow->inval();
554 });
Brian Osman207d4102019-01-10 09:40:58 -0500555 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
556 fAnimTimer.togglePauseResume();
557 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400558 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
559 fZoomUI = !fZoomUI;
560 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
561 fWindow->inval();
562 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500563
jvanverth2bb3b6d2016-04-08 07:24:09 -0700564 // set up slides
565 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500566 if (FLAGS_list) {
567 this->listNames();
568 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700569
Brian Osman9bb47cf2018-04-26 15:55:00 -0400570 fPerspectivePoints[0].set(0, 0);
571 fPerspectivePoints[1].set(1, 0);
572 fPerspectivePoints[2].set(0, 1);
573 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700574 fAnimTimer.run();
575
Hal Canaryc465d132017-12-08 10:21:31 -0500576 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500577 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400578 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500579 }
580 fImGuiGamutPaint.setColor(SK_ColorWHITE);
581 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
582
jongdeok.kim804f17e2019-02-26 14:39:23 +0900583 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500584 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700585}
586
jvanverth34524262016-05-04 13:49:13 -0700587void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400588 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
589 static const struct {
590 const char* fExtension;
591 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500592 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400593 const SlideFactory fFactory;
594 } gExternalSlidesInfo[] = {
595 { ".skp", "skp-dir", FLAGS_skps,
596 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
597 return sk_make_sp<SKPSlide>(name, path);}
598 },
599 { ".jpg", "jpg-dir", FLAGS_jpgs,
600 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
601 return sk_make_sp<ImageSlide>(name, path);}
602 },
Florin Malita87ccf332018-05-04 12:23:24 -0400603#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400604 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400605 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
606 return sk_make_sp<SkottieSlide>(name, path);}
607 },
Florin Malita87ccf332018-05-04 12:23:24 -0400608#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400609#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400610 { ".svg", "svg-dir", FLAGS_svgs,
611 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
612 return sk_make_sp<SvgSlide>(name, path);}
613 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400614#endif
Ruiqi Maof5101492018-06-29 14:32:21 -0400615#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
616 { ".nima", "nima-dir", FLAGS_nimas,
617 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
618 return sk_make_sp<NIMASlide>(name, path);}
619 },
620#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400621 };
jvanverthc265a922016-04-08 12:51:45 -0700622
Brian Salomon343553a2018-09-05 15:41:23 -0400623 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700624
Mike Klein88544fb2019-03-20 10:50:33 -0500625 const auto addSlide =
626 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
627 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
628 return;
629 }
liyuqian6f163d22016-06-13 12:26:45 -0700630
Mike Klein88544fb2019-03-20 10:50:33 -0500631 if (auto slide = fact(name, path)) {
632 dirSlides.push_back(slide);
633 fSlides.push_back(std::move(slide));
634 }
635 };
Florin Malita76a076b2018-02-15 18:40:48 -0500636
Florin Malita38792ce2018-05-08 10:36:18 -0400637 if (!FLAGS_file.isEmpty()) {
638 // single file mode
639 const SkString file(FLAGS_file[0]);
640
641 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
642 for (const auto& sinfo : gExternalSlidesInfo) {
643 if (file.endsWith(sinfo.fExtension)) {
644 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
645 return;
646 }
647 }
648
649 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
650 } else {
651 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
652 }
653
654 return;
655 }
656
657 // Bisect slide.
658 if (!FLAGS_bisect.isEmpty()) {
659 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500660 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400661 if (FLAGS_bisect.count() >= 2) {
662 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
663 bisect->onChar(*ch);
664 }
665 }
666 fSlides.push_back(std::move(bisect));
667 }
668 }
669
670 // GMs
671 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400672 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
673 std::unique_ptr<skiagm::GM> gm(gmFactory(nullptr));
Mike Klein88544fb2019-03-20 10:50:33 -0500674 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400675 sk_sp<Slide> slide(new GMSlide(gm.release()));
676 fSlides.push_back(std::move(slide));
677 }
Florin Malita38792ce2018-05-08 10:36:18 -0400678 }
679 // reverse gms
680 int numGMs = fSlides.count() - firstGM;
681 for (int i = 0; i < numGMs/2; ++i) {
682 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
683 }
684
685 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400686 for (const SampleFactory factory : SampleRegistry::Range()) {
687 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500688 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400689 fSlides.push_back(slide);
690 }
Florin Malita38792ce2018-05-08 10:36:18 -0400691 }
692
Brian Osman7c979f52019-02-12 13:27:51 -0500693 // Particle demo
694 {
695 // TODO: Convert this to a sample
696 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500697 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500698 fSlides.push_back(std::move(slide));
699 }
700 }
701
Florin Malita0ffa3222018-04-05 14:34:45 -0400702 for (const auto& info : gExternalSlidesInfo) {
703 for (const auto& flag : info.fFlags) {
704 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
705 // single file
706 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
707 } else {
708 // directory
709 SkOSFile::Iter it(flag.c_str(), info.fExtension);
710 SkString name;
711 while (it.next(&name)) {
712 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
713 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400714 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400715 if (!dirSlides.empty()) {
716 fSlides.push_back(
717 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
718 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500719 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400720 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400721 }
722 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500723
724 if (!fSlides.count()) {
725 sk_sp<Slide> slide(new NullSlide());
726 fSlides.push_back(std::move(slide));
727 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700728}
729
730
jvanverth34524262016-05-04 13:49:13 -0700731Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700732 fWindow->detach();
733 delete fWindow;
734}
735
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500736struct SkPaintTitleUpdater {
737 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
738 void append(const char* s) {
739 if (fCount == 0) {
740 fTitle->append(" {");
741 } else {
742 fTitle->append(", ");
743 }
744 fTitle->append(s);
745 ++fCount;
746 }
747 void done() {
748 if (fCount > 0) {
749 fTitle->append("}");
750 }
751 }
752 SkString* fTitle;
753 int fCount;
754};
755
brianosman05de2162016-05-06 13:28:57 -0700756void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700757 if (!fWindow) {
758 return;
759 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500760 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700761 return; // Surface hasn't been created yet.
762 }
763
jvanverth34524262016-05-04 13:49:13 -0700764 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700765 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700766
Mike Kleine5acd752019-03-22 09:57:16 -0500767 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400768 if (gSkForceAnalyticAA) {
769 title.append(" <FAAA>");
770 } else {
771 title.append(" <AAA>");
772 }
773 }
774
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500775 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500776 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
777 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400778 const char* on, const char* off)
779 {
Ben Wagner9613e452019-01-23 10:34:59 -0500780 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400781 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500782 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400783 };
784
Ben Wagner9613e452019-01-23 10:34:59 -0500785 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
786 const char* on, const char* off)
787 {
788 if (fFontOverrides.*flag) {
789 paintTitle.append((fFont.*isFlag)() ? on : off);
790 }
791 };
792
793 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
794 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500795 if (fPaintOverrides.fFilterQuality) {
796 switch (fPaint.getFilterQuality()) {
797 case kNone_SkFilterQuality:
798 paintTitle.append("NoFilter");
799 break;
800 case kLow_SkFilterQuality:
801 paintTitle.append("LowFilter");
802 break;
803 case kMedium_SkFilterQuality:
804 paintTitle.append("MediumFilter");
805 break;
806 case kHigh_SkFilterQuality:
807 paintTitle.append("HighFilter");
808 break;
809 }
810 }
Ben Wagner9613e452019-01-23 10:34:59 -0500811
812 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
813 "Force Autohint", "No Force Autohint");
814 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
815 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
816 "Linear Metrics", "Non-Linear Metrics");
817 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
818 "Bitmap Text", "No Bitmap Text");
819 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
820
821 if (fFontOverrides.fEdging) {
822 switch (fFont.getEdging()) {
823 case SkFont::Edging::kAlias:
824 paintTitle.append("Alias Text");
825 break;
826 case SkFont::Edging::kAntiAlias:
827 paintTitle.append("Antialias Text");
828 break;
829 case SkFont::Edging::kSubpixelAntiAlias:
830 paintTitle.append("Subpixel Antialias Text");
831 break;
832 }
833 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400834
Mike Reed3ae47332019-01-04 10:11:46 -0500835 if (fFontOverrides.fHinting) {
836 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500837 case kNo_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500838 paintTitle.append("No Hinting");
839 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500840 case kSlight_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500841 paintTitle.append("Slight Hinting");
842 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500843 case kNormal_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500844 paintTitle.append("Normal Hinting");
845 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500846 case kFull_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500847 paintTitle.append("Full Hinting");
848 break;
849 }
850 }
851 paintTitle.done();
852
Brian Osman92004802017-03-06 11:47:26 -0500853 switch (fColorMode) {
854 case ColorMode::kLegacy:
855 title.append(" Legacy 8888");
856 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500857 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500858 title.append(" ColorManaged 8888");
859 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500860 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500861 title.append(" ColorManaged F16");
862 break;
863 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500864
Brian Osman92004802017-03-06 11:47:26 -0500865 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500866 int curPrimaries = -1;
867 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
868 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
869 curPrimaries = i;
870 break;
871 }
872 }
Brian Osman03115dc2018-11-26 13:55:19 -0500873 title.appendf(" %s Gamma %f",
874 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500875 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700876 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500877
Ben Wagner37c54032018-04-13 14:30:23 -0400878 const DisplayParams& params = fWindow->getRequestedDisplayParams();
879 if (fPixelGeometryOverrides) {
880 switch (params.fSurfaceProps.pixelGeometry()) {
881 case kUnknown_SkPixelGeometry:
882 title.append( " Flat");
883 break;
884 case kRGB_H_SkPixelGeometry:
885 title.append( " RGB");
886 break;
887 case kBGR_H_SkPixelGeometry:
888 title.append( " BGR");
889 break;
890 case kRGB_V_SkPixelGeometry:
891 title.append( " RGBV");
892 break;
893 case kBGR_V_SkPixelGeometry:
894 title.append( " BGRV");
895 break;
896 }
897 }
898
899 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
900 title.append(" DFT");
901 }
902
csmartdalton578f0642017-02-24 16:04:47 -0700903 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700904 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500905 int msaa = fWindow->sampleCount();
906 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700907 title.appendf(" MSAA: %i", msaa);
908 }
909 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700910
911 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600912 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700913 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
914 }
915
Brian Osman805a7272018-05-02 15:40:20 -0400916 if (kPerspective_Real == fPerspectiveMode) {
917 title.append(" Perpsective (Real)");
918 } else if (kPerspective_Fake == fPerspectiveMode) {
919 title.append(" Perspective (Fake)");
920 }
921
brianosman05de2162016-05-06 13:28:57 -0700922 fWindow->setTitle(title.c_str());
923}
924
Florin Malitaab99c342018-01-16 16:23:03 -0500925int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500926
927 if (!FLAGS_slide.isEmpty()) {
928 int count = fSlides.count();
929 for (int i = 0; i < count; i++) {
930 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500931 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500932 }
933 }
934
935 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
936 this->listNames();
937 }
938
Florin Malitaab99c342018-01-16 16:23:03 -0500939 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500940}
941
Florin Malitaab99c342018-01-16 16:23:03 -0500942void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500943 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500944 for (const auto& slide : fSlides) {
945 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500946 }
947}
948
Florin Malitaab99c342018-01-16 16:23:03 -0500949void Viewer::setCurrentSlide(int slide) {
950 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700951
Florin Malitaab99c342018-01-16 16:23:03 -0500952 if (slide == fCurrentSlide) {
953 return;
954 }
955
956 if (fCurrentSlide >= 0) {
957 fSlides[fCurrentSlide]->unload();
958 }
959
960 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
961 SkIntToScalar(fWindow->height()));
962 fCurrentSlide = slide;
963 this->setupCurrentSlide();
964}
965
966void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500967 if (fCurrentSlide >= 0) {
968 // prepare dimensions for image slides
969 fGesture.resetTouchState();
970 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700971
Jim Van Verth0848fb02018-01-22 13:39:30 -0500972 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
973 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
974 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400975
Jim Van Verth0848fb02018-01-22 13:39:30 -0500976 // Start with a matrix that scales the slide to the available screen space
977 if (fWindow->scaleContentToFit()) {
978 if (windowRect.width() > 0 && windowRect.height() > 0) {
979 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
980 }
liyuqiane46e4f02016-05-20 07:32:19 -0700981 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500982
983 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400984 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500985
986 this->updateTitle();
987 this->updateUIState();
988
989 fStatsLayer.resetMeasurements();
990
991 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700992 }
jvanverthc265a922016-04-08 12:51:45 -0700993}
994
995#define MAX_ZOOM_LEVEL 8
996#define MIN_ZOOM_LEVEL -8
997
jvanverth34524262016-05-04 13:49:13 -0700998void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700999 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -04001000 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001001 this->preTouchMatrixChanged();
1002}
Yuqian Li755778c2018-03-28 16:23:31 -04001003
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001004void Viewer::preTouchMatrixChanged() {
1005 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001006 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1007 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1008 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1009 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1010}
1011
Brian Osman805a7272018-05-02 15:40:20 -04001012SkMatrix Viewer::computePerspectiveMatrix() {
1013 SkScalar w = fWindow->width(), h = fWindow->height();
1014 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1015 SkPoint perspPts[4] = {
1016 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1017 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1018 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1019 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1020 };
1021 SkMatrix m;
1022 m.setPolyToPoly(orthoPts, perspPts, 4);
1023 return m;
1024}
1025
Yuqian Li755778c2018-03-28 16:23:31 -04001026SkMatrix Viewer::computePreTouchMatrix() {
1027 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001028
1029 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001030 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001031 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001032
1033 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1034 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001035
Brian Osman805a7272018-05-02 15:40:20 -04001036 if (kPerspective_Real == fPerspectiveMode) {
1037 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001038 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001039 }
1040
Yuqian Li755778c2018-03-28 16:23:31 -04001041 return m;
jvanverthc265a922016-04-08 12:51:45 -07001042}
1043
liyuqiand3cdbca2016-05-17 12:44:20 -07001044SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001045 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001046 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001047 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001048 return m;
jvanverthc265a922016-04-08 12:51:45 -07001049}
1050
Brian Osman621491e2017-02-28 15:45:01 -05001051void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001052 fPersistentCache.reset();
1053 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001054 fBackendType = backendType;
1055
1056 fWindow->detach();
1057
Brian Osman70d2f432017-11-08 09:54:10 -05001058#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001059 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1060 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001061 DisplayParams params = fWindow->getRequestedDisplayParams();
1062 delete fWindow;
1063 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001064
Brian Osman70d2f432017-11-08 09:54:10 -05001065 // re-register callbacks
1066 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001067 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001068 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001069 fWindow->pushLayer(&fImGuiLayer);
1070
Brian Osman70d2f432017-11-08 09:54:10 -05001071 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1072 // will still include our correct sample count. But the re-created fWindow will lose that
1073 // information. On Windows, we need to re-create the window when changing sample count,
1074 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1075 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1076 // as if we had never changed windows at all).
1077 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001078#endif
1079
Brian Osman70d2f432017-11-08 09:54:10 -05001080 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001081}
1082
Brian Osman92004802017-03-06 11:47:26 -05001083void Viewer::setColorMode(ColorMode colorMode) {
1084 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001085 this->updateTitle();
1086 fWindow->inval();
1087}
1088
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001089class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1090public:
Mike Reed3ae47332019-01-04 10:11:46 -05001091 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1092 SkFont* font, Viewer::SkFontFields* ffields)
1093 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001094 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001095 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1096 sk_sp<SkTextBlob>* cache) {
1097 bool blobWillChange = false;
1098 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001099 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1100 bool shouldDraw = this->filterFont(&filteredFont);
1101 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001102 blobWillChange = true;
1103 break;
1104 }
1105 }
1106 if (!blobWillChange) {
1107 return blob;
1108 }
1109
1110 SkTextBlobBuilder builder;
1111 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001112 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1113 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001114 if (!shouldDraw) {
1115 continue;
1116 }
1117
Mike Reed3ae47332019-01-04 10:11:46 -05001118 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001119
Ben Wagner41e40472018-09-24 13:01:54 -04001120 const SkTextBlobBuilder::RunBuffer& runBuffer
1121 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001122 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001123 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001124 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001125 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001126 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001127 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001128 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001129 it.glyphCount(), it.textSize(), SkString())
1130 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1131 uint32_t glyphCount = it.glyphCount();
1132 if (it.glyphs()) {
1133 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1134 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1135 }
1136 if (it.pos()) {
1137 size_t posSize = sizeof(decltype(*it.pos()));
1138 uint8_t positioning = it.positioning();
1139 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1140 }
1141 if (it.text()) {
1142 size_t textSize = sizeof(decltype(*it.text()));
1143 uint32_t textCount = it.textSize();
1144 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1145 }
1146 if (it.clusters()) {
1147 size_t clusterSize = sizeof(decltype(*it.clusters()));
1148 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1149 }
1150 }
1151 *cache = builder.make();
1152 return cache->get();
1153 }
1154 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1155 const SkPaint& paint) override {
1156 sk_sp<SkTextBlob> cache;
1157 this->SkPaintFilterCanvas::onDrawTextBlob(
1158 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1159 }
Mike Reed3ae47332019-01-04 10:11:46 -05001160 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001161 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001162 font->writable()->setSize(fFont->getSize());
1163 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001164 if (fFontOverrides->fScaleX) {
1165 font->writable()->setScaleX(fFont->getScaleX());
1166 }
1167 if (fFontOverrides->fSkewX) {
1168 font->writable()->setSkewX(fFont->getSkewX());
1169 }
Mike Reed3ae47332019-01-04 10:11:46 -05001170 if (fFontOverrides->fHinting) {
1171 font->writable()->setHinting(fFont->getHinting());
1172 }
Ben Wagner9613e452019-01-23 10:34:59 -05001173 if (fFontOverrides->fEdging) {
1174 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001175 }
Ben Wagner9613e452019-01-23 10:34:59 -05001176 if (fFontOverrides->fEmbolden) {
1177 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001178 }
Ben Wagner9613e452019-01-23 10:34:59 -05001179 if (fFontOverrides->fLinearMetrics) {
1180 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001181 }
Ben Wagner9613e452019-01-23 10:34:59 -05001182 if (fFontOverrides->fSubpixel) {
1183 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001184 }
Ben Wagner9613e452019-01-23 10:34:59 -05001185 if (fFontOverrides->fEmbeddedBitmaps) {
1186 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001187 }
Ben Wagner9613e452019-01-23 10:34:59 -05001188 if (fFontOverrides->fForceAutoHinting) {
1189 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001190 }
Ben Wagner9613e452019-01-23 10:34:59 -05001191
Mike Reed3ae47332019-01-04 10:11:46 -05001192 return true;
1193 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001194 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001195 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001196 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001197 }
Ben Wagner9613e452019-01-23 10:34:59 -05001198 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001199 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001200 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001201 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001202 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001203 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001204 return true;
1205 }
1206 SkPaint* fPaint;
1207 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001208 SkFont* fFont;
1209 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001210};
1211
Robert Phillips9882dae2019-03-04 11:00:10 -05001212void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001213 if (fCurrentSlide < 0) {
1214 return;
1215 }
1216
Robert Phillips9882dae2019-03-04 11:00:10 -05001217 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001218
Brian Osmanf750fbc2017-02-08 10:47:28 -05001219 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001220 SkSurface* slideSurface = surface;
1221 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001222 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001223
Brian Osmane0d4fba2017-03-15 10:24:55 -04001224 // 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 -05001225 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001226 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001227 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001228 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001229 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001230 }
1231
Brian Osman3ac99cf2017-12-01 11:23:53 -05001232 if (fSaveToSKP) {
1233 SkPictureRecorder recorder;
1234 SkCanvas* recorderCanvas = recorder.beginRecording(
1235 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001236 fSlides[fCurrentSlide]->draw(recorderCanvas);
1237 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1238 SkFILEWStream stream("sample_app.skp");
1239 picture->serialize(&stream);
1240 fSaveToSKP = false;
1241 }
1242
Brian Osmane9ed0f02018-11-26 14:50:05 -05001243 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
1244 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode) ? kRGBA_F16_SkColorType
1245 : kN32_SkColorType;
Brian Osmane9ed0f02018-11-26 14:50:05 -05001246
1247 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001248 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1249 slideCanvas->getProps(&props);
1250
Brian Osmane9ed0f02018-11-26 14:50:05 -05001251 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1252 return Window::kRaster_BackendType == this->fBackendType
1253 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001254 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001255 };
1256
Brian Osman03115dc2018-11-26 13:55:19 -05001257 // We need to render offscreen if we're...
1258 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1259 // ... in any raster mode, because the window surface is actually GL
1260 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001261 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001262 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001263 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001264 Window::kRaster_BackendType == fBackendType ||
1265 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001266
Brian Osmane9ed0f02018-11-26 14:50:05 -05001267 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001268 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001269 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001270 }
1271
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001272 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001273 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001274 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001275 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001276 if (fTiled) {
1277 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1278 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1279 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1280 SkCanvas* tileCanvas = tileSurface->getCanvas();
1281 SkMatrix m = this->computeMatrix();
1282 for (int y = 0; y < fWindow->height(); y += tileH) {
1283 for (int x = 0; x < fWindow->width(); x += tileW) {
1284 SkAutoCanvasRestore acr(tileCanvas, true);
1285 tileCanvas->translate(-x, -y);
1286 tileCanvas->clear(SK_ColorTRANSPARENT);
1287 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001288 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1289 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001290 fSlides[fCurrentSlide]->draw(&filterCanvas);
1291 tileSurface->draw(slideCanvas, x, y, nullptr);
1292 }
1293 }
1294
1295 // Draw borders between tiles
1296 if (fDrawTileBoundaries) {
1297 SkPaint border;
1298 border.setColor(0x60FF00FF);
1299 border.setStyle(SkPaint::kStroke_Style);
1300 for (int y = 0; y < fWindow->height(); y += tileH) {
1301 for (int x = 0; x < fWindow->width(); x += tileW) {
1302 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1303 }
1304 }
1305 }
1306 } else {
1307 slideCanvas->concat(this->computeMatrix());
1308 if (kPerspective_Real == fPerspectiveMode) {
1309 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1310 }
Mike Reed3ae47332019-01-04 10:11:46 -05001311 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001312 fSlides[fCurrentSlide]->draw(&filterCanvas);
1313 }
Brian Osman56a24812017-12-19 11:15:16 -05001314 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001315 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001316
1317 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001318 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001319 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001320 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001321
1322 // If we rendered offscreen, snap an image and push the results to the window's canvas
1323 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001324 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001325
Robert Phillips9882dae2019-03-04 11:00:10 -05001326 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001327 SkPaint paint;
1328 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001329 int prePerspectiveCount = canvas->save();
1330 if (kPerspective_Fake == fPerspectiveMode) {
1331 paint.setFilterQuality(kHigh_SkFilterQuality);
1332 canvas->clear(SK_ColorWHITE);
1333 canvas->concat(this->computePerspectiveMatrix());
1334 }
Brian Osman03115dc2018-11-26 13:55:19 -05001335 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001336 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001337 }
Mike Reed376d8122019-03-14 11:39:02 -04001338
1339 if (fShowSlideDimensions) {
1340 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1341 SkPaint paint;
1342 paint.setColor(0x40FFFF00);
1343 surface->getCanvas()->drawRect(r, paint);
1344 }
liyuqian6f163d22016-06-13 12:26:45 -07001345}
1346
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001347void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001348 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001349 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001350}
Jim Van Verth6f449692017-02-14 15:16:46 -05001351
Robert Phillips9882dae2019-03-04 11:00:10 -05001352void Viewer::onPaint(SkSurface* surface) {
1353 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001354
Robert Phillips9882dae2019-03-04 11:00:10 -05001355 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001356
Brian Osmand67e5182017-12-08 16:46:09 -05001357 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001358
1359 if (GrContext* ctx = fWindow->getGrContext()) {
1360 // Clean out cache items that haven't been used in more than 10 seconds.
1361 ctx->performDeferredCleanup(std::chrono::seconds(10));
1362 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001363}
1364
Ben Wagnera1915972018-08-09 15:06:19 -04001365void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001366 if (fCurrentSlide >= 0) {
1367 fSlides[fCurrentSlide]->resize(width, height);
1368 }
Ben Wagnera1915972018-08-09 15:06:19 -04001369}
1370
Florin Malitacefc1b92018-02-19 21:43:47 -05001371SkPoint Viewer::mapEvent(float x, float y) {
1372 const auto m = this->computeMatrix();
1373 SkMatrix inv;
1374
1375 SkAssertResult(m.invert(&inv));
1376
1377 return inv.mapXY(x, y);
1378}
1379
jvanverth814e38d2016-06-06 08:48:47 -07001380bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001381 if (GestureDevice::kMouse == fGestureDevice) {
1382 return false;
1383 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001384
1385 const auto slidePt = this->mapEvent(x, y);
1386 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1387 fWindow->inval();
1388 return true;
1389 }
1390
liyuqiand3cdbca2016-05-17 12:44:20 -07001391 void* castedOwner = reinterpret_cast<void*>(owner);
1392 switch (state) {
1393 case Window::kUp_InputState: {
1394 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001395#if defined(SK_BUILD_FOR_IOS)
1396 // TODO: move IOS swipe detection higher up into the platform code
1397 SkPoint dir;
1398 if (fGesture.isFling(&dir)) {
1399 // swiping left or right
1400 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1401 if (dir.fX < 0) {
1402 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1403 fCurrentSlide + 1 : 0);
1404 } else {
1405 this->setCurrentSlide(fCurrentSlide > 0 ?
1406 fCurrentSlide - 1 : fSlides.count() - 1);
1407 }
1408 }
1409 fGesture.reset();
1410 }
1411#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001412 break;
1413 }
1414 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001415 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001416 break;
1417 }
1418 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001419 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001420 break;
1421 }
1422 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001423 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001424 fWindow->inval();
1425 return true;
1426}
1427
Brian Osman80fc07e2017-12-08 16:45:43 -05001428bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001429 if (GestureDevice::kTouch == fGestureDevice) {
1430 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001431 }
Brian Osman16c81a12017-12-20 11:58:34 -05001432
Florin Malitacefc1b92018-02-19 21:43:47 -05001433 const auto slidePt = this->mapEvent(x, y);
1434 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1435 fWindow->inval();
1436 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001437 }
1438
1439 switch (state) {
1440 case Window::kUp_InputState: {
1441 fGesture.touchEnd(nullptr);
1442 break;
1443 }
1444 case Window::kDown_InputState: {
1445 fGesture.touchBegin(nullptr, x, y);
1446 break;
1447 }
1448 case Window::kMove_InputState: {
1449 fGesture.touchMoved(nullptr, x, y);
1450 break;
1451 }
1452 }
1453 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1454
1455 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1456 fWindow->inval();
1457 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001458 return true;
1459}
1460
Brian Osmana109e392017-02-24 09:49:14 -05001461static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001462 // The gamut image covers a (0.8 x 0.9) shaped region
1463 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001464
1465 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1466 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1467 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001468 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1469 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1470 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001471
Brian Osman535c5e32019-02-09 16:32:58 -05001472 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1473 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1474 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1475 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1476 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001477}
1478
Ben Wagner3627d2e2018-06-26 14:23:20 -04001479static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001480 ImGui::DragCanvas dc(pt);
1481 dc.fillColor(IM_COL32(0, 0, 0, 128));
1482 dc.dragPoint(pt);
1483 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001484}
1485
Brian Osman9bb47cf2018-04-26 15:55:00 -04001486static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001487 ImGui::DragCanvas dc(pts);
1488 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001489
Brian Osman535c5e32019-02-09 16:32:58 -05001490 for (int i = 0; i < 4; ++i) {
1491 dc.dragPoint(pts + i);
1492 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001493
Brian Osman535c5e32019-02-09 16:32:58 -05001494 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1495 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1496 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1497 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001498
Brian Osman535c5e32019-02-09 16:32:58 -05001499 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001500}
1501
Brian Osmand67e5182017-12-08 16:46:09 -05001502void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001503 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1504 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001505 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001506 }
1507
1508 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001509 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1510 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001511 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001512 DisplayParams params = fWindow->getRequestedDisplayParams();
1513 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001514 const GrContext* ctx = fWindow->getGrContext();
1515
Brian Osmana109e392017-02-24 09:49:14 -05001516 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1517 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001518 if (ImGui::CollapsingHeader("Backend")) {
1519 int newBackend = static_cast<int>(fBackendType);
1520 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1521 ImGui::SameLine();
1522 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001523#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1524 ImGui::SameLine();
1525 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1526#endif
Brian Osman621491e2017-02-28 15:45:01 -05001527#if defined(SK_VULKAN)
1528 ImGui::SameLine();
1529 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1530#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -05001531#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
1532 ImGui::SameLine();
1533 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1534#endif
Brian Osman621491e2017-02-28 15:45:01 -05001535 if (newBackend != fBackendType) {
1536 fDeferredActions.push_back([=]() {
1537 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1538 });
1539 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001540
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001541 bool* wire = &params.fGrContextOptions.fWireframeMode;
1542 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1543 paramsChanged = true;
1544 }
Brian Salomon99a33902017-03-07 15:16:34 -05001545
Brian Osman28b12522017-03-08 17:10:24 -05001546 if (ctx) {
1547 int sampleCount = fWindow->sampleCount();
1548 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001549 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001550 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1551 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1552 ImGui::RadioButton("16", &sampleCount, 16);
1553
1554 if (sampleCount != params.fMSAASampleCount) {
1555 params.fMSAASampleCount = sampleCount;
1556 paramsChanged = true;
1557 }
1558 }
1559
Ben Wagner37c54032018-04-13 14:30:23 -04001560 int pixelGeometryIdx = 0;
1561 if (fPixelGeometryOverrides) {
1562 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1563 }
1564 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1565 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1566 {
1567 uint32_t flags = params.fSurfaceProps.flags();
1568 if (pixelGeometryIdx == 0) {
1569 fPixelGeometryOverrides = false;
1570 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1571 } else {
1572 fPixelGeometryOverrides = true;
1573 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1574 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1575 }
1576 paramsChanged = true;
1577 }
1578
1579 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1580 if (ImGui::Checkbox("DFT", &useDFT)) {
1581 uint32_t flags = params.fSurfaceProps.flags();
1582 if (useDFT) {
1583 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1584 } else {
1585 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1586 }
1587 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1588 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1589 paramsChanged = true;
1590 }
1591
Brian Osman8a9de3d2017-03-01 14:59:05 -05001592 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001593 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001594 auto prButton = [&](GpuPathRenderers x) {
1595 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001596 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1597 params.fGrContextOptions.fGpuPathRenderers = x;
1598 paramsChanged = true;
1599 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001600 }
1601 };
1602
1603 if (!ctx) {
1604 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001605 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001606 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001607 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001608 prButton(GpuPathRenderers::kStencilAndCover);
1609 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001610 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001611 prButton(GpuPathRenderers::kNone);
1612 } else {
1613 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001614 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001615 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001616 prButton(GpuPathRenderers::kCoverageCounting);
1617 }
Jim Van Verth83010462017-03-16 08:45:39 -04001618 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001619 prButton(GpuPathRenderers::kTessellating);
1620 prButton(GpuPathRenderers::kNone);
1621 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001622 ImGui::TreePop();
1623 }
Brian Osman621491e2017-02-28 15:45:01 -05001624 }
1625
Ben Wagner964571d2019-03-08 12:35:06 -05001626 if (ImGui::CollapsingHeader("Tiling")) {
1627 ImGui::Checkbox("Enable", &fTiled);
1628 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1629 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1630 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1631 }
1632
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001633 if (ImGui::CollapsingHeader("Transform")) {
1634 float zoom = fZoomLevel;
1635 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1636 fZoomLevel = zoom;
1637 this->preTouchMatrixChanged();
1638 paramsChanged = true;
1639 }
1640 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001641 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001642 fRotation = deg;
1643 this->preTouchMatrixChanged();
1644 paramsChanged = true;
1645 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001646 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1647 if (ImGui_DragLocation(&fOffset)) {
1648 this->preTouchMatrixChanged();
1649 paramsChanged = true;
1650 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001651 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1652 this->preTouchMatrixChanged();
1653 paramsChanged = true;
1654 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001655 }
Brian Osman805a7272018-05-02 15:40:20 -04001656 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1657 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1658 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001659 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001660 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001661 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001662 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001663 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001664 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001665 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001666 }
1667
Ben Wagnera580fb32018-04-17 11:16:32 -04001668 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001669 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001670 if (fPaintOverrides.fAntiAlias) {
1671 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001672 }
1673 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001674 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001675 {
1676 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1677 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001678 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001679 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1680 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001681 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001682 fPaintOverrides.fAntiAlias = true;
1683 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001684 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001685 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001686 case SkPaintFields::AntiAliasState::Alias:
1687 break;
1688 case SkPaintFields::AntiAliasState::Normal:
1689 break;
1690 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1691 gSkUseAnalyticAA = true;
1692 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001693 break;
1694 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1695 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001696 break;
1697 }
1698 }
1699 paramsChanged = true;
1700 }
1701
Ben Wagner99a78dc2018-05-09 18:23:51 -04001702 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001703 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001704 bool (SkPaint::* isFlag)() const,
1705 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001706 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001707 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001708 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001709 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001710 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001711 if (ImGui::Combo(label, &itemIndex, items)) {
1712 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001713 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001714 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001715 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001716 (fPaint.*setFlag)(itemIndex == 2);
1717 }
1718 paramsChanged = true;
1719 }
1720 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001721
Ben Wagner99a78dc2018-05-09 18:23:51 -04001722 paintFlag("Dither",
1723 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001724 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001725 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001726
1727 int filterQualityIdx = 0;
1728 if (fPaintOverrides.fFilterQuality) {
1729 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1730 }
1731 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1732 "Default\0None\0Low\0Medium\0High\0\0"))
1733 {
1734 if (filterQualityIdx == 0) {
1735 fPaintOverrides.fFilterQuality = false;
1736 fPaint.setFilterQuality(kNone_SkFilterQuality);
1737 } else {
1738 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1739 fPaintOverrides.fFilterQuality = true;
1740 }
1741 paramsChanged = true;
1742 }
Ben Wagner9613e452019-01-23 10:34:59 -05001743 }
Hal Canary02738a82019-01-21 18:51:32 +00001744
Ben Wagner9613e452019-01-23 10:34:59 -05001745 if (ImGui::CollapsingHeader("Font")) {
1746 int hintingIdx = 0;
1747 if (fFontOverrides.fHinting) {
1748 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1749 }
1750 if (ImGui::Combo("Hinting", &hintingIdx,
1751 "Default\0None\0Slight\0Normal\0Full\0\0"))
1752 {
1753 if (hintingIdx == 0) {
1754 fFontOverrides.fHinting = false;
1755 fFont.setHinting(kNo_SkFontHinting);
1756 } else {
1757 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1758 fFontOverrides.fHinting = true;
1759 }
1760 paramsChanged = true;
1761 }
Hal Canary02738a82019-01-21 18:51:32 +00001762
Ben Wagner9613e452019-01-23 10:34:59 -05001763 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1764 bool SkFontFields::* flag,
1765 bool (SkFont::* isFlag)() const,
1766 void (SkFont::* setFlag)(bool) )
1767 {
1768 int itemIndex = 0;
1769 if (fFontOverrides.*flag) {
1770 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1771 }
1772 if (ImGui::Combo(label, &itemIndex, items)) {
1773 if (itemIndex == 0) {
1774 fFontOverrides.*flag = false;
1775 } else {
1776 fFontOverrides.*flag = true;
1777 (fFont.*setFlag)(itemIndex == 2);
1778 }
1779 paramsChanged = true;
1780 }
1781 };
Hal Canary02738a82019-01-21 18:51:32 +00001782
Ben Wagner9613e452019-01-23 10:34:59 -05001783 fontFlag("Fake Bold Glyphs",
1784 "Default\0No Fake Bold\0Fake Bold\0\0",
1785 &SkFontFields::fEmbolden,
1786 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001787
Ben Wagner9613e452019-01-23 10:34:59 -05001788 fontFlag("Linear Text",
1789 "Default\0No Linear Text\0Linear Text\0\0",
1790 &SkFontFields::fLinearMetrics,
1791 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001792
Ben Wagner9613e452019-01-23 10:34:59 -05001793 fontFlag("Subpixel Position Glyphs",
1794 "Default\0Pixel Text\0Subpixel Text\0\0",
1795 &SkFontFields::fSubpixel,
1796 &SkFont::isSubpixel, &SkFont::setSubpixel);
1797
1798 fontFlag("Embedded Bitmap Text",
1799 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1800 &SkFontFields::fEmbeddedBitmaps,
1801 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1802
1803 fontFlag("Force Auto-Hinting",
1804 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1805 &SkFontFields::fForceAutoHinting,
1806 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1807
1808 int edgingIdx = 0;
1809 if (fFontOverrides.fEdging) {
1810 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1811 }
1812 if (ImGui::Combo("Edging", &edgingIdx,
1813 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1814 {
1815 if (edgingIdx == 0) {
1816 fFontOverrides.fEdging = false;
1817 fFont.setEdging(SkFont::Edging::kAlias);
1818 } else {
1819 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1820 fFontOverrides.fEdging = true;
1821 }
1822 paramsChanged = true;
1823 }
1824
Ben Wagner15a8d572019-03-21 13:35:44 -04001825 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1826 if (fFontOverrides.fSize) {
1827 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001828 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001829 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001830 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001831 fFontOverrides.fSizeRange[0],
1832 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001833 "%.6f", 2.0f))
1834 {
Mike Reed3ae47332019-01-04 10:11:46 -05001835 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001836 paramsChanged = true;
1837 }
1838 }
1839
1840 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1841 if (fFontOverrides.fScaleX) {
1842 float scaleX = fFont.getScaleX();
1843 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1844 fFont.setScaleX(scaleX);
1845 paramsChanged = true;
1846 }
1847 }
1848
1849 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1850 if (fFontOverrides.fSkewX) {
1851 float skewX = fFont.getSkewX();
1852 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1853 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001854 paramsChanged = true;
1855 }
1856 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001857 }
1858
Mike Reed81f60ec2018-05-15 10:09:52 -04001859 {
1860 SkMetaData controls;
1861 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1862 if (ImGui::CollapsingHeader("Current Slide")) {
1863 SkMetaData::Iter iter(controls);
1864 const char* name;
1865 SkMetaData::Type type;
1866 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001867 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001868 if (type == SkMetaData::kScalar_Type) {
1869 float val[3];
1870 SkASSERT(count == 3);
1871 controls.findScalars(name, &count, val);
1872 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1873 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001874 }
Ben Wagner110c7032019-03-22 17:03:59 -04001875 } else if (type == SkMetaData::kBool_Type) {
1876 bool val;
1877 SkASSERT(count == 1);
1878 controls.findBool(name, &val);
1879 if (ImGui::Checkbox(name, &val)) {
1880 controls.setBool(name, val);
1881 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001882 }
1883 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001884 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001885 }
1886 }
1887 }
1888
Ben Wagner7a3c6742018-04-23 10:01:07 -04001889 if (fShowSlidePicker) {
1890 ImGui::SetNextTreeNodeOpen(true);
1891 }
Brian Osman79086b92017-02-10 13:36:16 -05001892 if (ImGui::CollapsingHeader("Slide")) {
1893 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001894 static ImVector<const char*> filteredSlideNames;
1895 static ImVector<int> filteredSlideIndices;
1896
Brian Osmanfce09c52017-11-14 15:32:20 -05001897 if (fShowSlidePicker) {
1898 ImGui::SetKeyboardFocusHere();
1899 fShowSlidePicker = false;
1900 }
1901
Brian Osman79086b92017-02-10 13:36:16 -05001902 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001903 filteredSlideNames.clear();
1904 filteredSlideIndices.clear();
1905 int filteredIndex = 0;
1906 for (int i = 0; i < fSlides.count(); ++i) {
1907 const char* slideName = fSlides[i]->getName().c_str();
1908 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1909 if (i == fCurrentSlide) {
1910 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001911 }
Brian Osmanf479e422017-11-08 13:11:36 -05001912 filteredSlideNames.push_back(slideName);
1913 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001914 }
Brian Osman79086b92017-02-10 13:36:16 -05001915 }
Brian Osmanf479e422017-11-08 13:11:36 -05001916
Brian Osmanf479e422017-11-08 13:11:36 -05001917 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1918 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001919 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001920 }
1921 }
Brian Osmana109e392017-02-24 09:49:14 -05001922
1923 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001924 ColorMode newMode = fColorMode;
1925 auto cmButton = [&](ColorMode mode, const char* label) {
1926 if (ImGui::RadioButton(label, mode == fColorMode)) {
1927 newMode = mode;
1928 }
1929 };
1930
1931 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001932 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1933 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001934
1935 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001936 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001937 }
1938
1939 // Pick from common gamuts:
1940 int primariesIdx = 4; // Default: Custom
1941 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1942 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1943 primariesIdx = i;
1944 break;
1945 }
1946 }
1947
Brian Osman03115dc2018-11-26 13:55:19 -05001948 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05001949 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001950
Brian Osmana109e392017-02-24 09:49:14 -05001951 if (ImGui::Combo("Primaries", &primariesIdx,
1952 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1953 if (primariesIdx >= 0 && primariesIdx <= 3) {
1954 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1955 }
1956 }
1957
1958 // Allow direct editing of gamut
1959 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1960 }
Brian Osman207d4102019-01-10 09:40:58 -05001961
1962 if (ImGui::CollapsingHeader("Animation")) {
1963 bool isPaused = fAnimTimer.isPaused();
1964 if (ImGui::Checkbox("Pause", &isPaused)) {
1965 fAnimTimer.togglePauseResume();
1966 }
Brian Osman707d2022019-01-10 11:27:34 -05001967
1968 float speed = fAnimTimer.getSpeed();
1969 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
1970 fAnimTimer.setSpeed(speed);
1971 }
Brian Osman207d4102019-01-10 09:40:58 -05001972 }
Brian Osman0b8bb882019-04-12 11:47:19 -04001973
Brian Osmanfd7657c2019-04-25 11:34:07 -04001974 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
1975#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1976 || Window::kANGLE_BackendType == fBackendType
1977#endif
1978 ;
1979
1980 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
1981 // than GL, we need to force it on. Just do that on the first frame after the backend
1982 // switch, then resume normal operation.
1983 if (!backendIsGL && !params.fGrContextOptions.fCacheSKSL) {
1984 params.fGrContextOptions.fCacheSKSL = true;
1985 paramsChanged = true;
1986 fPersistentCache.reset();
1987 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04001988 // To re-load shaders from the currently active programs, we flush all caches on one
1989 // frame, then set a flag to poll the cache on the next frame.
1990 static bool gLoadPending = false;
1991 if (gLoadPending) {
1992 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
1993 int hitCount) {
1994 CachedGLSL& entry(fCachedGLSL.push_back());
1995 entry.fKey = key;
1996 SkMD5 hash;
1997 hash.write(key->bytes(), key->size());
1998 SkMD5::Digest digest = hash.finish();
1999 for (int i = 0; i < 16; ++i) {
2000 entry.fKeyString.appendf("%02x", digest.data[i]);
2001 }
2002
Brian Osmana085a412019-04-25 09:44:43 -04002003 entry.fShaderType = GrPersistentCacheUtils::UnpackCachedShaders(
2004 data.get(), entry.fShader, entry.fInputs, kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002005 };
2006 fCachedGLSL.reset();
2007 fPersistentCache.foreach(collectShaders);
2008 gLoadPending = false;
2009 }
2010
2011 // Defer actually doing the load/save logic so that we can trigger a save when we
2012 // start or finish hovering on a tree node in the list below:
2013 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002014 bool doSave = ImGui::Button("Save");
2015 if (backendIsGL) {
2016 ImGui::SameLine();
2017 if (ImGui::Checkbox("SkSL", &params.fGrContextOptions.fCacheSKSL)) {
2018 paramsChanged = true;
2019 doLoad = true;
2020 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2021 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002022 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002023
2024 ImGui::BeginChild("##ScrollingRegion");
2025 for (auto& entry : fCachedGLSL) {
2026 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2027 bool hovered = ImGui::IsItemHovered();
2028 if (hovered != entry.fHovered) {
2029 // Force a save to patch the highlight shader in/out
2030 entry.fHovered = hovered;
2031 doSave = true;
2032 }
2033 if (inTreeNode) {
2034 // Full width, and a reasonable amount of space for each shader.
2035 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2036 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2037 boxSize);
2038 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2039 boxSize);
2040 ImGui::TreePop();
2041 }
2042 }
2043 ImGui::EndChild();
2044
2045 if (doLoad) {
2046 fPersistentCache.reset();
2047 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2048 gLoadPending = true;
2049 }
2050 if (doSave) {
2051 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002052 auto shaderCaps = ctx->priv().caps()->shaderCaps();
2053 bool sksl = params.fGrContextOptions.fCacheSKSL;
2054
2055 SkSL::String highlight = shaderCaps->versionDeclString();
2056 if (shaderCaps->usesPrecisionModifiers() && !sksl) {
2057 highlight.append("precision mediump float;\n");
2058 }
2059 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002060 highlight.appendf("out %s sk_FragColor;\n"
2061 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2062 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002063
2064 fPersistentCache.reset();
2065 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2066 for (auto& entry : fCachedGLSL) {
2067 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2068 if (entry.fHovered) {
2069 entry.fShader[kFragment_GrShaderType] = highlight;
2070 }
2071
Brian Osmana085a412019-04-25 09:44:43 -04002072 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2073 entry.fShader,
2074 entry.fInputs,
2075 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002076 fPersistentCache.store(*entry.fKey, *data);
2077
2078 entry.fShader[kFragment_GrShaderType] = backup;
2079 }
2080 }
2081 }
Brian Osman79086b92017-02-10 13:36:16 -05002082 }
Brian Salomon99a33902017-03-07 15:16:34 -05002083 if (paramsChanged) {
2084 fDeferredActions.push_back([=]() {
2085 fWindow->setRequestedDisplayParams(params);
2086 fWindow->inval();
2087 this->updateTitle();
2088 });
2089 }
Brian Osman79086b92017-02-10 13:36:16 -05002090 ImGui::End();
2091 }
2092
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002093 if (gShaderErrorHandler.fErrors.count()) {
2094 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2095 ImGui::Begin("Shader Errors");
2096 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2097 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
2098 ImGui::TextWrapped("%s", gShaderErrorHandler.fShaders[i].c_str());
2099 }
2100 ImGui::End();
2101 gShaderErrorHandler.reset();
2102 }
2103
Brian Osmanf6877092017-02-13 09:39:57 -05002104 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002105 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2106 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002107 static int zoomFactor = 8;
2108 if (ImGui::Button("<<")) {
2109 zoomFactor = SkTMax(zoomFactor / 2, 4);
2110 }
2111 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2112 if (ImGui::Button(">>")) {
2113 zoomFactor = SkTMin(zoomFactor * 2, 32);
2114 }
Brian Osmanf6877092017-02-13 09:39:57 -05002115
Ben Wagner3627d2e2018-06-26 14:23:20 -04002116 if (!fZoomWindowFixed) {
2117 ImVec2 mousePos = ImGui::GetMousePos();
2118 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2119 }
2120 SkScalar x = fZoomWindowLocation.x();
2121 SkScalar y = fZoomWindowLocation.y();
2122 int xInt = SkScalarRoundToInt(x);
2123 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002124 ImVec2 avail = ImGui::GetContentRegionAvail();
2125
Brian Osmanead517d2017-11-13 15:36:36 -05002126 uint32_t pixel = 0;
2127 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002128 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002129 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002130 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002131 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002132 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002133 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2134 }
2135
Brian Osmand67e5182017-12-08 16:46:09 -05002136 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002137 // Translate so the region of the image that's under the mouse cursor is centered
2138 // in the zoom canvas:
2139 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002140 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2141 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002142 c->drawImage(this->fLastImage, 0, 0);
2143
2144 SkPaint outline;
2145 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002146 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002147 });
Brian Osmanf6877092017-02-13 09:39:57 -05002148 }
2149
2150 ImGui::End();
2151 }
Brian Osman79086b92017-02-10 13:36:16 -05002152}
2153
liyuqian2edb0f42016-07-06 14:11:32 -07002154void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002155 for (int i = 0; i < fDeferredActions.count(); ++i) {
2156 fDeferredActions[i]();
2157 }
2158 fDeferredActions.reset();
2159
Brian Osman56a24812017-12-19 11:15:16 -05002160 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002161 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05002162 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05002163 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002164
Brian Osman79086b92017-02-10 13:36:16 -05002165 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002166 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2167 // not be visible, though. So we need to redraw if there is at least one visible window, or
2168 // more than one active window. Newly created windows are active but not visible for one frame
2169 // while they determine their layout and sizing.
2170 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2171 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002172 fWindow->inval();
2173 }
jvanverth9f372462016-04-06 06:08:59 -07002174}
liyuqiane5a6cd92016-05-27 08:52:52 -07002175
Florin Malitab632df72018-06-18 21:23:06 -04002176template <typename OptionsFunc>
2177static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2178 OptionsFunc&& optionsFunc) {
2179 writer.beginObject();
2180 {
2181 writer.appendString(kName , name);
2182 writer.appendString(kValue, value);
2183
2184 writer.beginArray(kOptions);
2185 {
2186 optionsFunc(writer);
2187 }
2188 writer.endArray();
2189 }
2190 writer.endObject();
2191}
2192
2193
liyuqiane5a6cd92016-05-27 08:52:52 -07002194void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002195 if (!fWindow) {
2196 return;
2197 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002198 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002199 return; // Surface hasn't been created yet.
2200 }
2201
Florin Malitab632df72018-06-18 21:23:06 -04002202 SkDynamicMemoryWStream memStream;
2203 SkJSONWriter writer(&memStream);
2204 writer.beginArray();
2205
liyuqianb73c24b2016-06-03 08:47:23 -07002206 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002207 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2208 [this](SkJSONWriter& writer) {
2209 for(const auto& slide : fSlides) {
2210 writer.appendString(slide->getName().c_str());
2211 }
2212 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002213
liyuqianb73c24b2016-06-03 08:47:23 -07002214 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002215 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2216 [](SkJSONWriter& writer) {
2217 for (const auto& str : kBackendTypeStrings) {
2218 writer.appendString(str);
2219 }
2220 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002221
csmartdalton578f0642017-02-24 16:04:47 -07002222 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002223 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2224 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2225 [this](SkJSONWriter& writer) {
2226 writer.appendS32(0);
2227
2228 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2229 return;
2230 }
2231
2232 for (int msaa : {4, 8, 16}) {
2233 writer.appendS32(msaa);
2234 }
2235 });
csmartdalton578f0642017-02-24 16:04:47 -07002236
csmartdalton61cd31a2017-02-27 17:00:53 -07002237 // Path renderer state
2238 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002239 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2240 [this](SkJSONWriter& writer) {
2241 const GrContext* ctx = fWindow->getGrContext();
2242 if (!ctx) {
2243 writer.appendString("Software");
2244 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002245 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002246
Florin Malitab632df72018-06-18 21:23:06 -04002247 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2248 if (fWindow->sampleCount() > 1) {
2249 if (caps->shaderCaps()->pathRenderingSupport()) {
2250 writer.appendString(
2251 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2252 }
2253 } else {
2254 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2255 writer.appendString(
2256 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2257 }
2258 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2259 }
2260 writer.appendString(
2261 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2262 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2263 }
2264 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002265
liyuqianb73c24b2016-06-03 08:47:23 -07002266 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002267 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2268 [this](SkJSONWriter& writer) {
2269 writer.appendString(kSoftkeyHint);
2270 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2271 writer.appendString(softkey.c_str());
2272 }
2273 });
liyuqianb73c24b2016-06-03 08:47:23 -07002274
Florin Malitab632df72018-06-18 21:23:06 -04002275 writer.endArray();
2276 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002277
Florin Malitab632df72018-06-18 21:23:06 -04002278 auto data = memStream.detachAsData();
2279
2280 // TODO: would be cool to avoid this copy
2281 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2282
2283 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002284}
2285
2286void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002287 // For those who will add more features to handle the state change in this function:
2288 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2289 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2290 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002291 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002292 for (int i = 0; i < fSlides.count(); ++i) {
2293 if (fSlides[i]->getName().equals(stateValue)) {
2294 this->setCurrentSlide(i);
2295 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002296 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002297 }
Florin Malitaab99c342018-01-16 16:23:03 -05002298
2299 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002300 } else if (stateName.equals(kBackendStateName)) {
2301 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2302 if (stateValue.equals(kBackendTypeStrings[i])) {
2303 if (fBackendType != i) {
2304 fBackendType = (sk_app::Window::BackendType)i;
2305 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002306 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002307 }
2308 break;
2309 }
2310 }
csmartdalton578f0642017-02-24 16:04:47 -07002311 } else if (stateName.equals(kMSAAStateName)) {
2312 DisplayParams params = fWindow->getRequestedDisplayParams();
2313 int sampleCount = atoi(stateValue.c_str());
2314 if (sampleCount != params.fMSAASampleCount) {
2315 params.fMSAASampleCount = sampleCount;
2316 fWindow->setRequestedDisplayParams(params);
2317 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002318 this->updateTitle();
2319 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002320 }
2321 } else if (stateName.equals(kPathRendererStateName)) {
2322 DisplayParams params = fWindow->getRequestedDisplayParams();
2323 for (const auto& pair : gPathRendererNames) {
2324 if (pair.second == stateValue.c_str()) {
2325 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2326 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2327 fWindow->setRequestedDisplayParams(params);
2328 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002329 this->updateTitle();
2330 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002331 }
2332 break;
2333 }
csmartdalton578f0642017-02-24 16:04:47 -07002334 }
liyuqianb73c24b2016-06-03 08:47:23 -07002335 } else if (stateName.equals(kSoftkeyStateName)) {
2336 if (!stateValue.equals(kSoftkeyHint)) {
2337 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002338 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002339 }
liyuqian2edb0f42016-07-06 14:11:32 -07002340 } else if (stateName.equals(kRefreshStateName)) {
2341 // This state is actually NOT in the UI state.
2342 // We use this to allow Android to quickly set bool fRefresh.
2343 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002344 } else {
2345 SkDebugf("Unknown stateName: %s", stateName.c_str());
2346 }
2347}
Brian Osman79086b92017-02-10 13:36:16 -05002348
2349bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002350 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002351}
2352
2353bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002354 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002355 fWindow->inval();
2356 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002357 } else {
2358 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002359 }
Brian Osman79086b92017-02-10 13:36:16 -05002360}