blob: ef26b8df9ee2fc01ef7d7f5a1e53eed092d1b39d [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
Brian Osman5e7fbfd2019-05-03 13:13:35 -040055class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
56public:
57 void compileError(const char* shader, const char* errors) override {
58 fShaders.push_back(SkString(shader));
59 fErrors.push_back(SkString(errors));
60 }
61
62 void reset() {
63 fShaders.reset();
64 fErrors.reset();
65 }
66
67 SkTArray<SkString> fShaders;
68 SkTArray<SkString> fErrors;
69};
70
71static CapturingShaderErrorHandler gShaderErrorHandler;
72
jvanverth34524262016-05-04 13:49:13 -070073using namespace sk_app;
74
csmartdalton61cd31a2017-02-27 17:00:53 -070075static std::map<GpuPathRenderers, std::string> gPathRendererNames;
76
jvanverth9f372462016-04-06 06:08:59 -070077Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070078 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070079}
80
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060081static DEFINE_string(slide, "", "Start on this sample.");
82static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050083
bsalomon6c471f72016-07-26 12:56:32 -070084#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070085# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050086#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
87# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
bsalomon6c471f72016-07-26 12:56:32 -070088#else
89# define BACKENDS_STR "\"sw\" and \"gl\""
90#endif
91
Brian Osman2dd96932016-10-18 15:33:53 -040092static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070093
Mike Klein5b3f3432019-03-21 11:42:21 -050094static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070095
Chris Dalton1e6c5b82019-06-17 14:16:49 -060096static DEFINE_int(internalSamples, 4,
97 "Number of samples for internal draws that use MSAA or mixed samples.");
98
Mike Klein84836b72019-03-21 11:31:36 -050099static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700100
Mike Klein84836b72019-03-21 11:31:36 -0500101static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400102
Mike Kleinc6142d82019-03-25 10:54:59 -0500103static DEFINE_string2(match, m, nullptr,
104 "[~][^]substring[$] [...] of name to run.\n"
105 "Multiple matches may be separated by spaces.\n"
106 "~ causes a matching name to always be skipped\n"
107 "^ requires the start of the name to match\n"
108 "$ requires the end of the name to match\n"
109 "^ and $ requires an exact match\n"
110 "If a name does not match any list entry,\n"
111 "it is skipped unless some list entry starts with ~");
112
Mike Klein19fb3972019-03-21 13:08:08 -0500113#if defined(SK_BUILD_FOR_ANDROID)
114 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500115 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
116 static DEFINE_string(lotties, "/data/local/tmp/lotties",
117 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500118#else
119 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500120 static DEFINE_string(skps, "skps", "Directory to read skps from.");
121 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500122#endif
123
Mike Kleinc6142d82019-03-25 10:54:59 -0500124static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
125
126static DEFINE_int_2(threads, j, -1,
127 "Run threadsafe tests on a threadpool with this many extra threads, "
128 "defaulting to one extra thread per core.");
129
130
Brian Salomon194db172017-08-17 14:37:06 -0400131const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700132 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400133#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
134 "ANGLE",
135#endif
jvanverth063ece72016-06-17 09:29:14 -0700136#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700137 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700138#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -0500139#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
140 "Metal",
141#endif
csmartdalton578f0642017-02-24 16:04:47 -0700142 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700143};
144
bsalomon6c471f72016-07-26 12:56:32 -0700145static sk_app::Window::BackendType get_backend_type(const char* str) {
146#ifdef SK_VULKAN
147 if (0 == strcmp(str, "vk")) {
148 return sk_app::Window::kVulkan_BackendType;
149 } else
150#endif
Brian Salomon194db172017-08-17 14:37:06 -0400151#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
152 if (0 == strcmp(str, "angle")) {
153 return sk_app::Window::kANGLE_BackendType;
154 } else
155#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -0500156#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
157 if (0 == strcmp(str, "mtl")) {
158 return sk_app::Window::kMetal_BackendType;
159 } else
160#endif
bsalomon6c471f72016-07-26 12:56:32 -0700161 if (0 == strcmp(str, "gl")) {
162 return sk_app::Window::kNativeGL_BackendType;
163 } else if (0 == strcmp(str, "sw")) {
164 return sk_app::Window::kRaster_BackendType;
165 } else {
166 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
167 return sk_app::Window::kRaster_BackendType;
168 }
169}
170
Brian Osmana109e392017-02-24 09:49:14 -0500171static SkColorSpacePrimaries gSrgbPrimaries = {
172 0.64f, 0.33f,
173 0.30f, 0.60f,
174 0.15f, 0.06f,
175 0.3127f, 0.3290f };
176
177static SkColorSpacePrimaries gAdobePrimaries = {
178 0.64f, 0.33f,
179 0.21f, 0.71f,
180 0.15f, 0.06f,
181 0.3127f, 0.3290f };
182
183static SkColorSpacePrimaries gP3Primaries = {
184 0.680f, 0.320f,
185 0.265f, 0.690f,
186 0.150f, 0.060f,
187 0.3127f, 0.3290f };
188
189static SkColorSpacePrimaries gRec2020Primaries = {
190 0.708f, 0.292f,
191 0.170f, 0.797f,
192 0.131f, 0.046f,
193 0.3127f, 0.3290f };
194
195struct NamedPrimaries {
196 const char* fName;
197 SkColorSpacePrimaries* fPrimaries;
198} gNamedPrimaries[] = {
199 { "sRGB", &gSrgbPrimaries },
200 { "AdobeRGB", &gAdobePrimaries },
201 { "P3", &gP3Primaries },
202 { "Rec. 2020", &gRec2020Primaries },
203};
204
205static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
206 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
207}
208
Brian Osman70d2f432017-11-08 09:54:10 -0500209static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
210 // In raster mode, we still use GL for the window.
211 // This lets us render the GUI faster (and correct).
212 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
213}
214
Jim Van Verth74826c82019-03-01 14:37:30 -0500215class NullSlide : public Slide {
216 SkISize getDimensions() const override {
217 return SkISize::Make(640, 480);
218 }
219
220 void draw(SkCanvas* canvas) override {
221 canvas->clear(0xffff11ff);
222 }
223};
224
liyuqiane5a6cd92016-05-27 08:52:52 -0700225const char* kName = "name";
226const char* kValue = "value";
227const char* kOptions = "options";
228const char* kSlideStateName = "Slide";
229const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700230const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700231const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700232const char* kSoftkeyStateName = "Softkey";
233const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700234const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700235const char* kON = "ON";
236const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700237const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700238
jvanverth34524262016-05-04 13:49:13 -0700239Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500240 : fCurrentSlide(-1)
241 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500242 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400243 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500244 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500245 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500246 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500247 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400248 , fZoomWindowFixed(false)
249 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500250 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400251 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700252 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500253 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500254 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500255 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500256 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700257 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400258 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400259 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400260 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500261 , fTiled(false)
262 , fDrawTileBoundaries(false)
263 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400264 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700265{
Greg Daniel285db442016-10-14 09:12:53 -0400266 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700267
Brian Osmanf09e35e2017-12-15 14:48:09 -0500268 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500269 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500270 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600271 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500272 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
273 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700274
jvanverth2bb3b6d2016-04-08 07:24:09 -0700275 SkDebugf("Command line arguments: ");
276 for (int i = 1; i < argc; ++i) {
277 SkDebugf("%s ", argv[i]);
278 }
279 SkDebugf("\n");
280
Mike Klein88544fb2019-03-20 10:50:33 -0500281 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500282#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400283 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500284#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700285
Mike Klein19cc0f62019-03-22 15:30:07 -0500286 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500287
Brian Osmanbc8150f2017-07-24 11:38:01 -0400288 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400289 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400290
bsalomon6c471f72016-07-26 12:56:32 -0700291 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700292 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700293
csmartdalton578f0642017-02-24 16:04:47 -0700294 DisplayParams displayParams;
295 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700296 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400297 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
298 displayParams.fGrContextOptions.fDisallowGLSLBinaryCaching = true;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400299 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
300 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltona1638a52019-06-24 11:54:24 -0600301 displayParams.fGrContextOptions.fInternalMultisampleCount = FLAGS_internalSamples;
csmartdalton578f0642017-02-24 16:04:47 -0700302 fWindow->setRequestedDisplayParams(displayParams);
303
Brian Osman56a24812017-12-19 11:15:16 -0500304 // Configure timers
305 fStatsLayer.setActive(false);
306 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
307 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
308 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
309
jvanverth9f372462016-04-06 06:08:59 -0700310 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700311 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500312 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500313 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500314 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700315
brianosman622c8d52016-05-10 06:50:49 -0700316 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500317 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
318 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
319 fWindow->inval();
320 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500321 // Command to jump directly to the slide picker and give it focus
322 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
323 this->fShowImGuiDebugWindow = true;
324 this->fShowSlidePicker = true;
325 fWindow->inval();
326 });
327 // Alias that to Backspace, to match SampleApp
328 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
329 this->fShowImGuiDebugWindow = true;
330 this->fShowSlidePicker = true;
331 fWindow->inval();
332 });
Brian Osman79086b92017-02-10 13:36:16 -0500333 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
334 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
335 fWindow->inval();
336 });
Brian Osmanf6877092017-02-13 09:39:57 -0500337 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
338 this->fShowZoomWindow = !this->fShowZoomWindow;
339 fWindow->inval();
340 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400341 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
342 this->fZoomWindowFixed = !this->fZoomWindowFixed;
343 fWindow->inval();
344 });
Greg Danield0794cc2019-03-27 16:23:26 -0400345 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
346 DisplayParams params = fWindow->getRequestedDisplayParams();
347 params.fDisableVsync = !params.fDisableVsync;
348 fWindow->setRequestedDisplayParams(params);
349 this->updateTitle();
350 fWindow->inval();
351 });
Mike Reedf702ed42019-07-22 17:00:49 -0400352 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
353 fRefresh = !fRefresh;
354 fWindow->inval();
355 });
brianosman622c8d52016-05-10 06:50:49 -0700356 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500357 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700358 fWindow->inval();
359 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400360 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500361 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400362 this->updateTitle();
363 fWindow->inval();
364 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500365 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500366 switch (fColorMode) {
367 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500368 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500369 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500370 case ColorMode::kColorManaged8888:
371 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500372 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500373 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500374 this->setColorMode(ColorMode::kLegacy);
375 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500376 }
brianosman622c8d52016-05-10 06:50:49 -0700377 });
378 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500379 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700380 });
381 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500382 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700383 });
384 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
385 this->changeZoomLevel(1.f / 32.f);
386 fWindow->inval();
387 });
388 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
389 this->changeZoomLevel(-1.f / 32.f);
390 fWindow->inval();
391 });
jvanverthaf236b52016-05-20 06:01:06 -0700392 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400393 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
394 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500395 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400396#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
397 if (newBackend == sk_app::Window::kVulkan_BackendType) {
398 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
399 sk_app::Window::kBackendTypeCount);
400 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
401 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500402 }
403#endif
Brian Osman621491e2017-02-28 15:45:01 -0500404 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700405 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500406 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
407 fSaveToSKP = true;
408 fWindow->inval();
409 });
Mike Reed376d8122019-03-14 11:39:02 -0400410 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
411 fShowSlideDimensions = !fShowSlideDimensions;
412 fWindow->inval();
413 });
Ben Wagner37c54032018-04-13 14:30:23 -0400414 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
415 DisplayParams params = fWindow->getRequestedDisplayParams();
416 uint32_t flags = params.fSurfaceProps.flags();
417 if (!fPixelGeometryOverrides) {
418 fPixelGeometryOverrides = true;
419 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
420 } else {
421 switch (params.fSurfaceProps.pixelGeometry()) {
422 case kUnknown_SkPixelGeometry:
423 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
424 break;
425 case kRGB_H_SkPixelGeometry:
426 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
427 break;
428 case kBGR_H_SkPixelGeometry:
429 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
430 break;
431 case kRGB_V_SkPixelGeometry:
432 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
433 break;
434 case kBGR_V_SkPixelGeometry:
435 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
436 fPixelGeometryOverrides = false;
437 break;
438 }
439 }
440 fWindow->setRequestedDisplayParams(params);
441 this->updateTitle();
442 fWindow->inval();
443 });
Ben Wagner9613e452019-01-23 10:34:59 -0500444 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500445 if (!fFontOverrides.fHinting) {
446 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400447 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500448 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500449 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400450 case SkFontHinting::kNone:
451 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500452 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400453 case SkFontHinting::kSlight:
454 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500455 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400456 case SkFontHinting::kNormal:
457 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500458 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400459 case SkFontHinting::kFull:
460 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500461 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500462 break;
463 }
464 }
465 this->updateTitle();
466 fWindow->inval();
467 });
468 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500469 if (!fPaintOverrides.fAntiAlias) {
470 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
471 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500472 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500473 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500474 } else {
475 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500476 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500477 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500478 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400479 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500480 break;
481 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500482 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500483 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400484 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500485 break;
486 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500487 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400488 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500489 break;
490 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500491 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
492 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500493 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
494 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500495 break;
496 }
497 }
498 this->updateTitle();
499 fWindow->inval();
500 });
Ben Wagner37c54032018-04-13 14:30:23 -0400501 fCommands.addCommand('D', "Modes", "DFT", [this]() {
502 DisplayParams params = fWindow->getRequestedDisplayParams();
503 uint32_t flags = params.fSurfaceProps.flags();
504 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
505 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
506 fWindow->setRequestedDisplayParams(params);
507 this->updateTitle();
508 fWindow->inval();
509 });
Ben Wagner9613e452019-01-23 10:34:59 -0500510 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500511 if (!fFontOverrides.fEdging) {
512 fFontOverrides.fEdging = true;
513 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500514 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500515 switch (fFont.getEdging()) {
516 case SkFont::Edging::kAlias:
517 fFont.setEdging(SkFont::Edging::kAntiAlias);
518 break;
519 case SkFont::Edging::kAntiAlias:
520 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
521 break;
522 case SkFont::Edging::kSubpixelAntiAlias:
523 fFont.setEdging(SkFont::Edging::kAlias);
524 fFontOverrides.fEdging = false;
525 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500526 }
527 }
528 this->updateTitle();
529 fWindow->inval();
530 });
Ben Wagner9613e452019-01-23 10:34:59 -0500531 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500532 if (!fFontOverrides.fSubpixel) {
533 fFontOverrides.fSubpixel = true;
534 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500535 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500536 if (!fFont.isSubpixel()) {
537 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500538 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500539 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500540 }
541 }
542 this->updateTitle();
543 fWindow->inval();
544 });
Brian Osman805a7272018-05-02 15:40:20 -0400545 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
546 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
547 : kPerspective_Real;
548 this->updateTitle();
549 fWindow->inval();
550 });
551 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
552 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
553 : kPerspective_Off;
554 this->updateTitle();
555 fWindow->inval();
556 });
Brian Osman207d4102019-01-10 09:40:58 -0500557 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
558 fAnimTimer.togglePauseResume();
559 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400560 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
561 fZoomUI = !fZoomUI;
562 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
563 fWindow->inval();
564 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500565
jvanverth2bb3b6d2016-04-08 07:24:09 -0700566 // set up slides
567 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500568 if (FLAGS_list) {
569 this->listNames();
570 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700571
Brian Osman9bb47cf2018-04-26 15:55:00 -0400572 fPerspectivePoints[0].set(0, 0);
573 fPerspectivePoints[1].set(1, 0);
574 fPerspectivePoints[2].set(0, 1);
575 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700576 fAnimTimer.run();
577
Hal Canaryc465d132017-12-08 10:21:31 -0500578 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500579 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400580 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500581 }
582 fImGuiGamutPaint.setColor(SK_ColorWHITE);
583 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
584
jongdeok.kim804f17e2019-02-26 14:39:23 +0900585 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500586 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700587}
588
jvanverth34524262016-05-04 13:49:13 -0700589void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400590 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
591 static const struct {
592 const char* fExtension;
593 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500594 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400595 const SlideFactory fFactory;
596 } gExternalSlidesInfo[] = {
597 { ".skp", "skp-dir", FLAGS_skps,
598 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
599 return sk_make_sp<SKPSlide>(name, path);}
600 },
601 { ".jpg", "jpg-dir", FLAGS_jpgs,
602 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
603 return sk_make_sp<ImageSlide>(name, path);}
604 },
Florin Malita87ccf332018-05-04 12:23:24 -0400605#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400606 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400607 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
608 return sk_make_sp<SkottieSlide>(name, path);}
609 },
Florin Malita87ccf332018-05-04 12:23:24 -0400610#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400611#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400612 { ".svg", "svg-dir", FLAGS_svgs,
613 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
614 return sk_make_sp<SvgSlide>(name, path);}
615 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400616#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400617 };
jvanverthc265a922016-04-08 12:51:45 -0700618
Brian Salomon343553a2018-09-05 15:41:23 -0400619 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700620
Mike Klein88544fb2019-03-20 10:50:33 -0500621 const auto addSlide =
622 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
623 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
624 return;
625 }
liyuqian6f163d22016-06-13 12:26:45 -0700626
Mike Klein88544fb2019-03-20 10:50:33 -0500627 if (auto slide = fact(name, path)) {
628 dirSlides.push_back(slide);
629 fSlides.push_back(std::move(slide));
630 }
631 };
Florin Malita76a076b2018-02-15 18:40:48 -0500632
Florin Malita38792ce2018-05-08 10:36:18 -0400633 if (!FLAGS_file.isEmpty()) {
634 // single file mode
635 const SkString file(FLAGS_file[0]);
636
637 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
638 for (const auto& sinfo : gExternalSlidesInfo) {
639 if (file.endsWith(sinfo.fExtension)) {
640 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
641 return;
642 }
643 }
644
645 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
646 } else {
647 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
648 }
649
650 return;
651 }
652
653 // Bisect slide.
654 if (!FLAGS_bisect.isEmpty()) {
655 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500656 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400657 if (FLAGS_bisect.count() >= 2) {
658 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
659 bisect->onChar(*ch);
660 }
661 }
662 fSlides.push_back(std::move(bisect));
663 }
664 }
665
666 // GMs
667 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400668 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
669 std::unique_ptr<skiagm::GM> gm(gmFactory(nullptr));
Mike Klein88544fb2019-03-20 10:50:33 -0500670 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400671 sk_sp<Slide> slide(new GMSlide(gm.release()));
672 fSlides.push_back(std::move(slide));
673 }
Florin Malita38792ce2018-05-08 10:36:18 -0400674 }
675 // reverse gms
676 int numGMs = fSlides.count() - firstGM;
677 for (int i = 0; i < numGMs/2; ++i) {
678 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
679 }
680
681 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400682 for (const SampleFactory factory : SampleRegistry::Range()) {
683 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500684 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400685 fSlides.push_back(slide);
686 }
Florin Malita38792ce2018-05-08 10:36:18 -0400687 }
688
Brian Osman7c979f52019-02-12 13:27:51 -0500689 // Particle demo
690 {
691 // TODO: Convert this to a sample
692 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500693 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500694 fSlides.push_back(std::move(slide));
695 }
696 }
697
Florin Malita0ffa3222018-04-05 14:34:45 -0400698 for (const auto& info : gExternalSlidesInfo) {
699 for (const auto& flag : info.fFlags) {
700 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
701 // single file
702 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
703 } else {
704 // directory
705 SkOSFile::Iter it(flag.c_str(), info.fExtension);
706 SkString name;
707 while (it.next(&name)) {
708 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
709 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400710 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400711 if (!dirSlides.empty()) {
712 fSlides.push_back(
713 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
714 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500715 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400716 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400717 }
718 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500719
720 if (!fSlides.count()) {
721 sk_sp<Slide> slide(new NullSlide());
722 fSlides.push_back(std::move(slide));
723 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700724}
725
726
jvanverth34524262016-05-04 13:49:13 -0700727Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700728 fWindow->detach();
729 delete fWindow;
730}
731
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500732struct SkPaintTitleUpdater {
733 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
734 void append(const char* s) {
735 if (fCount == 0) {
736 fTitle->append(" {");
737 } else {
738 fTitle->append(", ");
739 }
740 fTitle->append(s);
741 ++fCount;
742 }
743 void done() {
744 if (fCount > 0) {
745 fTitle->append("}");
746 }
747 }
748 SkString* fTitle;
749 int fCount;
750};
751
brianosman05de2162016-05-06 13:28:57 -0700752void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700753 if (!fWindow) {
754 return;
755 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500756 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700757 return; // Surface hasn't been created yet.
758 }
759
jvanverth34524262016-05-04 13:49:13 -0700760 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700761 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700762
Mike Kleine5acd752019-03-22 09:57:16 -0500763 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400764 if (gSkForceAnalyticAA) {
765 title.append(" <FAAA>");
766 } else {
767 title.append(" <AAA>");
768 }
769 }
770
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500771 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500772 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
773 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400774 const char* on, const char* off)
775 {
Ben Wagner9613e452019-01-23 10:34:59 -0500776 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400777 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500778 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400779 };
780
Ben Wagner9613e452019-01-23 10:34:59 -0500781 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
782 const char* on, const char* off)
783 {
784 if (fFontOverrides.*flag) {
785 paintTitle.append((fFont.*isFlag)() ? on : off);
786 }
787 };
788
789 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
790 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500791 if (fPaintOverrides.fFilterQuality) {
792 switch (fPaint.getFilterQuality()) {
793 case kNone_SkFilterQuality:
794 paintTitle.append("NoFilter");
795 break;
796 case kLow_SkFilterQuality:
797 paintTitle.append("LowFilter");
798 break;
799 case kMedium_SkFilterQuality:
800 paintTitle.append("MediumFilter");
801 break;
802 case kHigh_SkFilterQuality:
803 paintTitle.append("HighFilter");
804 break;
805 }
806 }
Ben Wagner9613e452019-01-23 10:34:59 -0500807
808 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
809 "Force Autohint", "No Force Autohint");
810 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
811 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
812 "Linear Metrics", "Non-Linear Metrics");
813 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
814 "Bitmap Text", "No Bitmap Text");
815 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
816
817 if (fFontOverrides.fEdging) {
818 switch (fFont.getEdging()) {
819 case SkFont::Edging::kAlias:
820 paintTitle.append("Alias Text");
821 break;
822 case SkFont::Edging::kAntiAlias:
823 paintTitle.append("Antialias Text");
824 break;
825 case SkFont::Edging::kSubpixelAntiAlias:
826 paintTitle.append("Subpixel Antialias Text");
827 break;
828 }
829 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400830
Mike Reed3ae47332019-01-04 10:11:46 -0500831 if (fFontOverrides.fHinting) {
832 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400833 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500834 paintTitle.append("No Hinting");
835 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400836 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500837 paintTitle.append("Slight Hinting");
838 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400839 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500840 paintTitle.append("Normal Hinting");
841 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400842 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500843 paintTitle.append("Full Hinting");
844 break;
845 }
846 }
847 paintTitle.done();
848
Brian Osman92004802017-03-06 11:47:26 -0500849 switch (fColorMode) {
850 case ColorMode::kLegacy:
851 title.append(" Legacy 8888");
852 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500853 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500854 title.append(" ColorManaged 8888");
855 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500856 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500857 title.append(" ColorManaged F16");
858 break;
859 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500860
Brian Osman92004802017-03-06 11:47:26 -0500861 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500862 int curPrimaries = -1;
863 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
864 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
865 curPrimaries = i;
866 break;
867 }
868 }
Brian Osman03115dc2018-11-26 13:55:19 -0500869 title.appendf(" %s Gamma %f",
870 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500871 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700872 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500873
Ben Wagner37c54032018-04-13 14:30:23 -0400874 const DisplayParams& params = fWindow->getRequestedDisplayParams();
875 if (fPixelGeometryOverrides) {
876 switch (params.fSurfaceProps.pixelGeometry()) {
877 case kUnknown_SkPixelGeometry:
878 title.append( " Flat");
879 break;
880 case kRGB_H_SkPixelGeometry:
881 title.append( " RGB");
882 break;
883 case kBGR_H_SkPixelGeometry:
884 title.append( " BGR");
885 break;
886 case kRGB_V_SkPixelGeometry:
887 title.append( " RGBV");
888 break;
889 case kBGR_V_SkPixelGeometry:
890 title.append( " BGRV");
891 break;
892 }
893 }
894
895 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
896 title.append(" DFT");
897 }
898
csmartdalton578f0642017-02-24 16:04:47 -0700899 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700900 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500901 int msaa = fWindow->sampleCount();
902 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700903 title.appendf(" MSAA: %i", msaa);
904 }
905 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700906
907 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600908 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700909 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
910 }
911
Brian Osman805a7272018-05-02 15:40:20 -0400912 if (kPerspective_Real == fPerspectiveMode) {
913 title.append(" Perpsective (Real)");
914 } else if (kPerspective_Fake == fPerspectiveMode) {
915 title.append(" Perspective (Fake)");
916 }
917
brianosman05de2162016-05-06 13:28:57 -0700918 fWindow->setTitle(title.c_str());
919}
920
Florin Malitaab99c342018-01-16 16:23:03 -0500921int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500922
923 if (!FLAGS_slide.isEmpty()) {
924 int count = fSlides.count();
925 for (int i = 0; i < count; i++) {
926 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500927 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500928 }
929 }
930
931 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
932 this->listNames();
933 }
934
Florin Malitaab99c342018-01-16 16:23:03 -0500935 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500936}
937
Florin Malitaab99c342018-01-16 16:23:03 -0500938void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500939 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500940 for (const auto& slide : fSlides) {
941 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500942 }
943}
944
Florin Malitaab99c342018-01-16 16:23:03 -0500945void Viewer::setCurrentSlide(int slide) {
946 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700947
Florin Malitaab99c342018-01-16 16:23:03 -0500948 if (slide == fCurrentSlide) {
949 return;
950 }
951
952 if (fCurrentSlide >= 0) {
953 fSlides[fCurrentSlide]->unload();
954 }
955
956 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
957 SkIntToScalar(fWindow->height()));
958 fCurrentSlide = slide;
959 this->setupCurrentSlide();
960}
961
962void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500963 if (fCurrentSlide >= 0) {
964 // prepare dimensions for image slides
965 fGesture.resetTouchState();
966 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700967
Jim Van Verth0848fb02018-01-22 13:39:30 -0500968 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
969 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
970 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400971
Jim Van Verth0848fb02018-01-22 13:39:30 -0500972 // Start with a matrix that scales the slide to the available screen space
973 if (fWindow->scaleContentToFit()) {
974 if (windowRect.width() > 0 && windowRect.height() > 0) {
975 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
976 }
liyuqiane46e4f02016-05-20 07:32:19 -0700977 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500978
979 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400980 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500981
982 this->updateTitle();
983 this->updateUIState();
984
985 fStatsLayer.resetMeasurements();
986
987 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700988 }
jvanverthc265a922016-04-08 12:51:45 -0700989}
990
991#define MAX_ZOOM_LEVEL 8
992#define MIN_ZOOM_LEVEL -8
993
jvanverth34524262016-05-04 13:49:13 -0700994void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700995 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400996 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400997 this->preTouchMatrixChanged();
998}
Yuqian Li755778c2018-03-28 16:23:31 -0400999
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001000void Viewer::preTouchMatrixChanged() {
1001 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001002 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1003 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1004 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1005 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1006}
1007
Brian Osman805a7272018-05-02 15:40:20 -04001008SkMatrix Viewer::computePerspectiveMatrix() {
1009 SkScalar w = fWindow->width(), h = fWindow->height();
1010 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1011 SkPoint perspPts[4] = {
1012 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1013 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1014 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1015 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1016 };
1017 SkMatrix m;
1018 m.setPolyToPoly(orthoPts, perspPts, 4);
1019 return m;
1020}
1021
Yuqian Li755778c2018-03-28 16:23:31 -04001022SkMatrix Viewer::computePreTouchMatrix() {
1023 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001024
1025 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001026 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001027 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001028
1029 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1030 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001031
Brian Osman805a7272018-05-02 15:40:20 -04001032 if (kPerspective_Real == fPerspectiveMode) {
1033 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001034 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001035 }
1036
Yuqian Li755778c2018-03-28 16:23:31 -04001037 return m;
jvanverthc265a922016-04-08 12:51:45 -07001038}
1039
liyuqiand3cdbca2016-05-17 12:44:20 -07001040SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001041 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001042 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001043 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001044 return m;
jvanverthc265a922016-04-08 12:51:45 -07001045}
1046
Brian Osman621491e2017-02-28 15:45:01 -05001047void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001048 fPersistentCache.reset();
1049 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001050 fBackendType = backendType;
1051
1052 fWindow->detach();
1053
Brian Osman70d2f432017-11-08 09:54:10 -05001054#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001055 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1056 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001057 DisplayParams params = fWindow->getRequestedDisplayParams();
1058 delete fWindow;
1059 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001060
Brian Osman70d2f432017-11-08 09:54:10 -05001061 // re-register callbacks
1062 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001063 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001064 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001065 fWindow->pushLayer(&fImGuiLayer);
1066
Brian Osman70d2f432017-11-08 09:54:10 -05001067 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1068 // will still include our correct sample count. But the re-created fWindow will lose that
1069 // information. On Windows, we need to re-create the window when changing sample count,
1070 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1071 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1072 // as if we had never changed windows at all).
1073 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001074#endif
1075
Brian Osman70d2f432017-11-08 09:54:10 -05001076 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001077}
1078
Brian Osman92004802017-03-06 11:47:26 -05001079void Viewer::setColorMode(ColorMode colorMode) {
1080 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001081 this->updateTitle();
1082 fWindow->inval();
1083}
1084
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001085class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1086public:
Mike Reed3ae47332019-01-04 10:11:46 -05001087 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1088 SkFont* font, Viewer::SkFontFields* ffields)
1089 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001090 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001091 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1092 sk_sp<SkTextBlob>* cache) {
1093 bool blobWillChange = false;
1094 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001095 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1096 bool shouldDraw = this->filterFont(&filteredFont);
1097 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001098 blobWillChange = true;
1099 break;
1100 }
1101 }
1102 if (!blobWillChange) {
1103 return blob;
1104 }
1105
1106 SkTextBlobBuilder builder;
1107 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001108 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1109 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001110 if (!shouldDraw) {
1111 continue;
1112 }
1113
Mike Reed3ae47332019-01-04 10:11:46 -05001114 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001115
Ben Wagner41e40472018-09-24 13:01:54 -04001116 const SkTextBlobBuilder::RunBuffer& runBuffer
1117 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001118 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001119 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001120 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001121 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001122 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001123 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001124 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001125 it.glyphCount(), it.textSize(), SkString())
1126 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1127 uint32_t glyphCount = it.glyphCount();
1128 if (it.glyphs()) {
1129 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1130 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1131 }
1132 if (it.pos()) {
1133 size_t posSize = sizeof(decltype(*it.pos()));
1134 uint8_t positioning = it.positioning();
1135 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1136 }
1137 if (it.text()) {
1138 size_t textSize = sizeof(decltype(*it.text()));
1139 uint32_t textCount = it.textSize();
1140 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1141 }
1142 if (it.clusters()) {
1143 size_t clusterSize = sizeof(decltype(*it.clusters()));
1144 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1145 }
1146 }
1147 *cache = builder.make();
1148 return cache->get();
1149 }
1150 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1151 const SkPaint& paint) override {
1152 sk_sp<SkTextBlob> cache;
1153 this->SkPaintFilterCanvas::onDrawTextBlob(
1154 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1155 }
Mike Reed3ae47332019-01-04 10:11:46 -05001156 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001157 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001158 font->writable()->setSize(fFont->getSize());
1159 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001160 if (fFontOverrides->fScaleX) {
1161 font->writable()->setScaleX(fFont->getScaleX());
1162 }
1163 if (fFontOverrides->fSkewX) {
1164 font->writable()->setSkewX(fFont->getSkewX());
1165 }
Mike Reed3ae47332019-01-04 10:11:46 -05001166 if (fFontOverrides->fHinting) {
1167 font->writable()->setHinting(fFont->getHinting());
1168 }
Ben Wagner9613e452019-01-23 10:34:59 -05001169 if (fFontOverrides->fEdging) {
1170 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001171 }
Ben Wagner9613e452019-01-23 10:34:59 -05001172 if (fFontOverrides->fEmbolden) {
1173 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001174 }
Ben Wagner9613e452019-01-23 10:34:59 -05001175 if (fFontOverrides->fLinearMetrics) {
1176 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001177 }
Ben Wagner9613e452019-01-23 10:34:59 -05001178 if (fFontOverrides->fSubpixel) {
1179 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001180 }
Ben Wagner9613e452019-01-23 10:34:59 -05001181 if (fFontOverrides->fEmbeddedBitmaps) {
1182 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001183 }
Ben Wagner9613e452019-01-23 10:34:59 -05001184 if (fFontOverrides->fForceAutoHinting) {
1185 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001186 }
Ben Wagner9613e452019-01-23 10:34:59 -05001187
Mike Reed3ae47332019-01-04 10:11:46 -05001188 return true;
1189 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001190 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001191 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001192 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001193 }
Ben Wagner9613e452019-01-23 10:34:59 -05001194 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001195 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001196 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001197 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001198 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001199 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001200 return true;
1201 }
1202 SkPaint* fPaint;
1203 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001204 SkFont* fFont;
1205 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001206};
1207
Robert Phillips9882dae2019-03-04 11:00:10 -05001208void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001209 if (fCurrentSlide < 0) {
1210 return;
1211 }
1212
Robert Phillips9882dae2019-03-04 11:00:10 -05001213 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001214
Brian Osmanf750fbc2017-02-08 10:47:28 -05001215 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001216 SkSurface* slideSurface = surface;
1217 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001218 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001219
Brian Osmane0d4fba2017-03-15 10:24:55 -04001220 // 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 -05001221 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001222 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001223 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001224 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001225 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001226 }
1227
Brian Osman3ac99cf2017-12-01 11:23:53 -05001228 if (fSaveToSKP) {
1229 SkPictureRecorder recorder;
1230 SkCanvas* recorderCanvas = recorder.beginRecording(
1231 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001232 fSlides[fCurrentSlide]->draw(recorderCanvas);
1233 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1234 SkFILEWStream stream("sample_app.skp");
1235 picture->serialize(&stream);
1236 fSaveToSKP = false;
1237 }
1238
Brian Osmane9ed0f02018-11-26 14:50:05 -05001239 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
1240 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode) ? kRGBA_F16_SkColorType
1241 : kN32_SkColorType;
Brian Osmane9ed0f02018-11-26 14:50:05 -05001242
1243 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001244 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1245 slideCanvas->getProps(&props);
1246
Brian Osmane9ed0f02018-11-26 14:50:05 -05001247 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1248 return Window::kRaster_BackendType == this->fBackendType
1249 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001250 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001251 };
1252
Brian Osman03115dc2018-11-26 13:55:19 -05001253 // We need to render offscreen if we're...
1254 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1255 // ... in any raster mode, because the window surface is actually GL
1256 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001257 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001258 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001259 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001260 Window::kRaster_BackendType == fBackendType ||
1261 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001262
Brian Osmane9ed0f02018-11-26 14:50:05 -05001263 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001264 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001265 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001266 }
1267
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001268 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001269 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001270 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001271 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001272 if (fTiled) {
1273 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1274 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1275 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1276 SkCanvas* tileCanvas = tileSurface->getCanvas();
1277 SkMatrix m = this->computeMatrix();
1278 for (int y = 0; y < fWindow->height(); y += tileH) {
1279 for (int x = 0; x < fWindow->width(); x += tileW) {
1280 SkAutoCanvasRestore acr(tileCanvas, true);
1281 tileCanvas->translate(-x, -y);
1282 tileCanvas->clear(SK_ColorTRANSPARENT);
1283 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001284 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1285 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001286 fSlides[fCurrentSlide]->draw(&filterCanvas);
1287 tileSurface->draw(slideCanvas, x, y, nullptr);
1288 }
1289 }
1290
1291 // Draw borders between tiles
1292 if (fDrawTileBoundaries) {
1293 SkPaint border;
1294 border.setColor(0x60FF00FF);
1295 border.setStyle(SkPaint::kStroke_Style);
1296 for (int y = 0; y < fWindow->height(); y += tileH) {
1297 for (int x = 0; x < fWindow->width(); x += tileW) {
1298 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1299 }
1300 }
1301 }
1302 } else {
1303 slideCanvas->concat(this->computeMatrix());
1304 if (kPerspective_Real == fPerspectiveMode) {
1305 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1306 }
Mike Reed3ae47332019-01-04 10:11:46 -05001307 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001308 fSlides[fCurrentSlide]->draw(&filterCanvas);
1309 }
Brian Osman56a24812017-12-19 11:15:16 -05001310 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001311 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001312
1313 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001314 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001315 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001316 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001317
1318 // If we rendered offscreen, snap an image and push the results to the window's canvas
1319 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001320 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001321
Robert Phillips9882dae2019-03-04 11:00:10 -05001322 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001323 SkPaint paint;
1324 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001325 int prePerspectiveCount = canvas->save();
1326 if (kPerspective_Fake == fPerspectiveMode) {
1327 paint.setFilterQuality(kHigh_SkFilterQuality);
1328 canvas->clear(SK_ColorWHITE);
1329 canvas->concat(this->computePerspectiveMatrix());
1330 }
Brian Osman03115dc2018-11-26 13:55:19 -05001331 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001332 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001333 }
Mike Reed376d8122019-03-14 11:39:02 -04001334
1335 if (fShowSlideDimensions) {
1336 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1337 SkPaint paint;
1338 paint.setColor(0x40FFFF00);
1339 surface->getCanvas()->drawRect(r, paint);
1340 }
liyuqian6f163d22016-06-13 12:26:45 -07001341}
1342
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001343void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001344 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001345 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001346}
Jim Van Verth6f449692017-02-14 15:16:46 -05001347
Robert Phillips9882dae2019-03-04 11:00:10 -05001348void Viewer::onPaint(SkSurface* surface) {
1349 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001350
Robert Phillips9882dae2019-03-04 11:00:10 -05001351 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001352
Brian Osmand67e5182017-12-08 16:46:09 -05001353 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001354
1355 if (GrContext* ctx = fWindow->getGrContext()) {
1356 // Clean out cache items that haven't been used in more than 10 seconds.
1357 ctx->performDeferredCleanup(std::chrono::seconds(10));
1358 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001359}
1360
Ben Wagnera1915972018-08-09 15:06:19 -04001361void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001362 if (fCurrentSlide >= 0) {
1363 fSlides[fCurrentSlide]->resize(width, height);
1364 }
Ben Wagnera1915972018-08-09 15:06:19 -04001365}
1366
Florin Malitacefc1b92018-02-19 21:43:47 -05001367SkPoint Viewer::mapEvent(float x, float y) {
1368 const auto m = this->computeMatrix();
1369 SkMatrix inv;
1370
1371 SkAssertResult(m.invert(&inv));
1372
1373 return inv.mapXY(x, y);
1374}
1375
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001376bool Viewer::onTouch(intptr_t owner, InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001377 if (GestureDevice::kMouse == fGestureDevice) {
1378 return false;
1379 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001380
1381 const auto slidePt = this->mapEvent(x, y);
Hal Canary3a85ed12019-07-08 16:07:57 -04001382 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001383 fWindow->inval();
1384 return true;
1385 }
1386
liyuqiand3cdbca2016-05-17 12:44:20 -07001387 void* castedOwner = reinterpret_cast<void*>(owner);
1388 switch (state) {
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001389 case InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001390 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001391#if defined(SK_BUILD_FOR_IOS)
1392 // TODO: move IOS swipe detection higher up into the platform code
1393 SkPoint dir;
1394 if (fGesture.isFling(&dir)) {
1395 // swiping left or right
1396 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1397 if (dir.fX < 0) {
1398 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1399 fCurrentSlide + 1 : 0);
1400 } else {
1401 this->setCurrentSlide(fCurrentSlide > 0 ?
1402 fCurrentSlide - 1 : fSlides.count() - 1);
1403 }
1404 }
1405 fGesture.reset();
1406 }
1407#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001408 break;
1409 }
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001410 case InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001411 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001412 break;
1413 }
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001414 case InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001415 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001416 break;
1417 }
1418 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001419 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001420 fWindow->inval();
1421 return true;
1422}
1423
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001424bool Viewer::onMouse(int x, int y, InputState state, ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001425 if (GestureDevice::kTouch == fGestureDevice) {
1426 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001427 }
Brian Osman16c81a12017-12-20 11:58:34 -05001428
Florin Malitacefc1b92018-02-19 21:43:47 -05001429 const auto slidePt = this->mapEvent(x, y);
1430 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1431 fWindow->inval();
1432 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001433 }
1434
1435 switch (state) {
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001436 case InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001437 fGesture.touchEnd(nullptr);
1438 break;
1439 }
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001440 case InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001441 fGesture.touchBegin(nullptr, x, y);
1442 break;
1443 }
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001444 case InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001445 fGesture.touchMoved(nullptr, x, y);
1446 break;
1447 }
1448 }
1449 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1450
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001451 if (state != InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001452 fWindow->inval();
1453 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001454 return true;
1455}
1456
Brian Osmana109e392017-02-24 09:49:14 -05001457static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001458 // The gamut image covers a (0.8 x 0.9) shaped region
1459 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001460
1461 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1462 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1463 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001464 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1465 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1466 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001467
Brian Osman535c5e32019-02-09 16:32:58 -05001468 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1469 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1470 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1471 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1472 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001473}
1474
Ben Wagner3627d2e2018-06-26 14:23:20 -04001475static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001476 ImGui::DragCanvas dc(pt);
1477 dc.fillColor(IM_COL32(0, 0, 0, 128));
1478 dc.dragPoint(pt);
1479 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001480}
1481
Brian Osman9bb47cf2018-04-26 15:55:00 -04001482static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001483 ImGui::DragCanvas dc(pts);
1484 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001485
Brian Osman535c5e32019-02-09 16:32:58 -05001486 for (int i = 0; i < 4; ++i) {
1487 dc.dragPoint(pts + i);
1488 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001489
Brian Osman535c5e32019-02-09 16:32:58 -05001490 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1491 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1492 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1493 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001494
Brian Osman535c5e32019-02-09 16:32:58 -05001495 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001496}
1497
Brian Osmand67e5182017-12-08 16:46:09 -05001498void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001499 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1500 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001501 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001502 }
1503
1504 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001505 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1506 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001507 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001508 DisplayParams params = fWindow->getRequestedDisplayParams();
1509 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001510 const GrContext* ctx = fWindow->getGrContext();
1511
Brian Osmana109e392017-02-24 09:49:14 -05001512 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1513 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001514 if (ImGui::CollapsingHeader("Backend")) {
1515 int newBackend = static_cast<int>(fBackendType);
1516 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1517 ImGui::SameLine();
1518 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001519#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1520 ImGui::SameLine();
1521 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1522#endif
Brian Osman621491e2017-02-28 15:45:01 -05001523#if defined(SK_VULKAN)
1524 ImGui::SameLine();
1525 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1526#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -05001527#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
1528 ImGui::SameLine();
1529 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1530#endif
Brian Osman621491e2017-02-28 15:45:01 -05001531 if (newBackend != fBackendType) {
1532 fDeferredActions.push_back([=]() {
1533 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1534 });
1535 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001536
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001537 bool* wire = &params.fGrContextOptions.fWireframeMode;
1538 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1539 paramsChanged = true;
1540 }
Brian Salomon99a33902017-03-07 15:16:34 -05001541
Brian Osman28b12522017-03-08 17:10:24 -05001542 if (ctx) {
1543 int sampleCount = fWindow->sampleCount();
1544 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001545 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001546 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1547 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1548 ImGui::RadioButton("16", &sampleCount, 16);
1549
1550 if (sampleCount != params.fMSAASampleCount) {
1551 params.fMSAASampleCount = sampleCount;
1552 paramsChanged = true;
1553 }
1554 }
1555
Ben Wagner37c54032018-04-13 14:30:23 -04001556 int pixelGeometryIdx = 0;
1557 if (fPixelGeometryOverrides) {
1558 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1559 }
1560 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1561 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1562 {
1563 uint32_t flags = params.fSurfaceProps.flags();
1564 if (pixelGeometryIdx == 0) {
1565 fPixelGeometryOverrides = false;
1566 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1567 } else {
1568 fPixelGeometryOverrides = true;
1569 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1570 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1571 }
1572 paramsChanged = true;
1573 }
1574
1575 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1576 if (ImGui::Checkbox("DFT", &useDFT)) {
1577 uint32_t flags = params.fSurfaceProps.flags();
1578 if (useDFT) {
1579 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1580 } else {
1581 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1582 }
1583 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1584 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1585 paramsChanged = true;
1586 }
1587
Brian Osman8a9de3d2017-03-01 14:59:05 -05001588 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001589 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001590 auto prButton = [&](GpuPathRenderers x) {
1591 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001592 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1593 params.fGrContextOptions.fGpuPathRenderers = x;
1594 paramsChanged = true;
1595 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001596 }
1597 };
1598
1599 if (!ctx) {
1600 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001601 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001602 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001603 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001604 prButton(GpuPathRenderers::kStencilAndCover);
1605 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001606 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001607 prButton(GpuPathRenderers::kNone);
1608 } else {
1609 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001610 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001611 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001612 prButton(GpuPathRenderers::kCoverageCounting);
1613 }
Jim Van Verth83010462017-03-16 08:45:39 -04001614 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001615 prButton(GpuPathRenderers::kTessellating);
1616 prButton(GpuPathRenderers::kNone);
1617 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001618 ImGui::TreePop();
1619 }
Brian Osman621491e2017-02-28 15:45:01 -05001620 }
1621
Ben Wagner964571d2019-03-08 12:35:06 -05001622 if (ImGui::CollapsingHeader("Tiling")) {
1623 ImGui::Checkbox("Enable", &fTiled);
1624 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1625 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1626 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1627 }
1628
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001629 if (ImGui::CollapsingHeader("Transform")) {
1630 float zoom = fZoomLevel;
1631 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1632 fZoomLevel = zoom;
1633 this->preTouchMatrixChanged();
1634 paramsChanged = true;
1635 }
1636 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001637 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001638 fRotation = deg;
1639 this->preTouchMatrixChanged();
1640 paramsChanged = true;
1641 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001642 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1643 if (ImGui_DragLocation(&fOffset)) {
1644 this->preTouchMatrixChanged();
1645 paramsChanged = true;
1646 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001647 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1648 this->preTouchMatrixChanged();
1649 paramsChanged = true;
1650 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001651 }
Brian Osman805a7272018-05-02 15:40:20 -04001652 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1653 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1654 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001655 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001656 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001657 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001658 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
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 Wagnerd02a74d2018-04-23 12:55:06 -04001662 }
1663
Ben Wagnera580fb32018-04-17 11:16:32 -04001664 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001665 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001666 if (fPaintOverrides.fAntiAlias) {
1667 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001668 }
1669 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001670 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001671 {
1672 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1673 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001674 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001675 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1676 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001677 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001678 fPaintOverrides.fAntiAlias = true;
1679 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001680 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001681 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001682 case SkPaintFields::AntiAliasState::Alias:
1683 break;
1684 case SkPaintFields::AntiAliasState::Normal:
1685 break;
1686 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1687 gSkUseAnalyticAA = true;
1688 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001689 break;
1690 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1691 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001692 break;
1693 }
1694 }
1695 paramsChanged = true;
1696 }
1697
Ben Wagner99a78dc2018-05-09 18:23:51 -04001698 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001699 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001700 bool (SkPaint::* isFlag)() const,
1701 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001702 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001703 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001704 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001705 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001706 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001707 if (ImGui::Combo(label, &itemIndex, items)) {
1708 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001709 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001710 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001711 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001712 (fPaint.*setFlag)(itemIndex == 2);
1713 }
1714 paramsChanged = true;
1715 }
1716 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001717
Ben Wagner99a78dc2018-05-09 18:23:51 -04001718 paintFlag("Dither",
1719 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001720 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001721 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001722
1723 int filterQualityIdx = 0;
1724 if (fPaintOverrides.fFilterQuality) {
1725 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1726 }
1727 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1728 "Default\0None\0Low\0Medium\0High\0\0"))
1729 {
1730 if (filterQualityIdx == 0) {
1731 fPaintOverrides.fFilterQuality = false;
1732 fPaint.setFilterQuality(kNone_SkFilterQuality);
1733 } else {
1734 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1735 fPaintOverrides.fFilterQuality = true;
1736 }
1737 paramsChanged = true;
1738 }
Ben Wagner9613e452019-01-23 10:34:59 -05001739 }
Hal Canary02738a82019-01-21 18:51:32 +00001740
Ben Wagner9613e452019-01-23 10:34:59 -05001741 if (ImGui::CollapsingHeader("Font")) {
1742 int hintingIdx = 0;
1743 if (fFontOverrides.fHinting) {
1744 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1745 }
1746 if (ImGui::Combo("Hinting", &hintingIdx,
1747 "Default\0None\0Slight\0Normal\0Full\0\0"))
1748 {
1749 if (hintingIdx == 0) {
1750 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001751 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001752 } else {
1753 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1754 fFontOverrides.fHinting = true;
1755 }
1756 paramsChanged = true;
1757 }
Hal Canary02738a82019-01-21 18:51:32 +00001758
Ben Wagner9613e452019-01-23 10:34:59 -05001759 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1760 bool SkFontFields::* flag,
1761 bool (SkFont::* isFlag)() const,
1762 void (SkFont::* setFlag)(bool) )
1763 {
1764 int itemIndex = 0;
1765 if (fFontOverrides.*flag) {
1766 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1767 }
1768 if (ImGui::Combo(label, &itemIndex, items)) {
1769 if (itemIndex == 0) {
1770 fFontOverrides.*flag = false;
1771 } else {
1772 fFontOverrides.*flag = true;
1773 (fFont.*setFlag)(itemIndex == 2);
1774 }
1775 paramsChanged = true;
1776 }
1777 };
Hal Canary02738a82019-01-21 18:51:32 +00001778
Ben Wagner9613e452019-01-23 10:34:59 -05001779 fontFlag("Fake Bold Glyphs",
1780 "Default\0No Fake Bold\0Fake Bold\0\0",
1781 &SkFontFields::fEmbolden,
1782 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001783
Ben Wagner9613e452019-01-23 10:34:59 -05001784 fontFlag("Linear Text",
1785 "Default\0No Linear Text\0Linear Text\0\0",
1786 &SkFontFields::fLinearMetrics,
1787 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001788
Ben Wagner9613e452019-01-23 10:34:59 -05001789 fontFlag("Subpixel Position Glyphs",
1790 "Default\0Pixel Text\0Subpixel Text\0\0",
1791 &SkFontFields::fSubpixel,
1792 &SkFont::isSubpixel, &SkFont::setSubpixel);
1793
1794 fontFlag("Embedded Bitmap Text",
1795 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1796 &SkFontFields::fEmbeddedBitmaps,
1797 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1798
1799 fontFlag("Force Auto-Hinting",
1800 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1801 &SkFontFields::fForceAutoHinting,
1802 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1803
1804 int edgingIdx = 0;
1805 if (fFontOverrides.fEdging) {
1806 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1807 }
1808 if (ImGui::Combo("Edging", &edgingIdx,
1809 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1810 {
1811 if (edgingIdx == 0) {
1812 fFontOverrides.fEdging = false;
1813 fFont.setEdging(SkFont::Edging::kAlias);
1814 } else {
1815 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1816 fFontOverrides.fEdging = true;
1817 }
1818 paramsChanged = true;
1819 }
1820
Ben Wagner15a8d572019-03-21 13:35:44 -04001821 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1822 if (fFontOverrides.fSize) {
1823 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001824 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001825 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001826 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001827 fFontOverrides.fSizeRange[0],
1828 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001829 "%.6f", 2.0f))
1830 {
Mike Reed3ae47332019-01-04 10:11:46 -05001831 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001832 paramsChanged = true;
1833 }
1834 }
1835
1836 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1837 if (fFontOverrides.fScaleX) {
1838 float scaleX = fFont.getScaleX();
1839 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1840 fFont.setScaleX(scaleX);
1841 paramsChanged = true;
1842 }
1843 }
1844
1845 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1846 if (fFontOverrides.fSkewX) {
1847 float skewX = fFont.getSkewX();
1848 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1849 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001850 paramsChanged = true;
1851 }
1852 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001853 }
1854
Mike Reed81f60ec2018-05-15 10:09:52 -04001855 {
1856 SkMetaData controls;
1857 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1858 if (ImGui::CollapsingHeader("Current Slide")) {
1859 SkMetaData::Iter iter(controls);
1860 const char* name;
1861 SkMetaData::Type type;
1862 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001863 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001864 if (type == SkMetaData::kScalar_Type) {
1865 float val[3];
1866 SkASSERT(count == 3);
1867 controls.findScalars(name, &count, val);
1868 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1869 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001870 }
Ben Wagner110c7032019-03-22 17:03:59 -04001871 } else if (type == SkMetaData::kBool_Type) {
1872 bool val;
1873 SkASSERT(count == 1);
1874 controls.findBool(name, &val);
1875 if (ImGui::Checkbox(name, &val)) {
1876 controls.setBool(name, val);
1877 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001878 }
1879 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001880 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001881 }
1882 }
1883 }
1884
Ben Wagner7a3c6742018-04-23 10:01:07 -04001885 if (fShowSlidePicker) {
1886 ImGui::SetNextTreeNodeOpen(true);
1887 }
Brian Osman79086b92017-02-10 13:36:16 -05001888 if (ImGui::CollapsingHeader("Slide")) {
1889 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001890 static ImVector<const char*> filteredSlideNames;
1891 static ImVector<int> filteredSlideIndices;
1892
Brian Osmanfce09c52017-11-14 15:32:20 -05001893 if (fShowSlidePicker) {
1894 ImGui::SetKeyboardFocusHere();
1895 fShowSlidePicker = false;
1896 }
1897
Brian Osman79086b92017-02-10 13:36:16 -05001898 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001899 filteredSlideNames.clear();
1900 filteredSlideIndices.clear();
1901 int filteredIndex = 0;
1902 for (int i = 0; i < fSlides.count(); ++i) {
1903 const char* slideName = fSlides[i]->getName().c_str();
1904 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1905 if (i == fCurrentSlide) {
1906 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001907 }
Brian Osmanf479e422017-11-08 13:11:36 -05001908 filteredSlideNames.push_back(slideName);
1909 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001910 }
Brian Osman79086b92017-02-10 13:36:16 -05001911 }
Brian Osmanf479e422017-11-08 13:11:36 -05001912
Brian Osmanf479e422017-11-08 13:11:36 -05001913 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1914 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001915 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001916 }
1917 }
Brian Osmana109e392017-02-24 09:49:14 -05001918
1919 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001920 ColorMode newMode = fColorMode;
1921 auto cmButton = [&](ColorMode mode, const char* label) {
1922 if (ImGui::RadioButton(label, mode == fColorMode)) {
1923 newMode = mode;
1924 }
1925 };
1926
1927 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001928 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1929 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001930
1931 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001932 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001933 }
1934
1935 // Pick from common gamuts:
1936 int primariesIdx = 4; // Default: Custom
1937 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1938 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1939 primariesIdx = i;
1940 break;
1941 }
1942 }
1943
Brian Osman03115dc2018-11-26 13:55:19 -05001944 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05001945 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001946
Brian Osmana109e392017-02-24 09:49:14 -05001947 if (ImGui::Combo("Primaries", &primariesIdx,
1948 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1949 if (primariesIdx >= 0 && primariesIdx <= 3) {
1950 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1951 }
1952 }
1953
1954 // Allow direct editing of gamut
1955 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1956 }
Brian Osman207d4102019-01-10 09:40:58 -05001957
1958 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04001959 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05001960 if (ImGui::Checkbox("Pause", &isPaused)) {
1961 fAnimTimer.togglePauseResume();
1962 }
Brian Osman707d2022019-01-10 11:27:34 -05001963
1964 float speed = fAnimTimer.getSpeed();
1965 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
1966 fAnimTimer.setSpeed(speed);
1967 }
Brian Osman207d4102019-01-10 09:40:58 -05001968 }
Brian Osman0b8bb882019-04-12 11:47:19 -04001969
Brian Osmanfd7657c2019-04-25 11:34:07 -04001970 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
1971#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1972 || Window::kANGLE_BackendType == fBackendType
1973#endif
1974 ;
1975
1976 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
1977 // than GL, we need to force it on. Just do that on the first frame after the backend
1978 // switch, then resume normal operation.
1979 if (!backendIsGL && !params.fGrContextOptions.fCacheSKSL) {
1980 params.fGrContextOptions.fCacheSKSL = true;
1981 paramsChanged = true;
1982 fPersistentCache.reset();
1983 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04001984 // To re-load shaders from the currently active programs, we flush all caches on one
1985 // frame, then set a flag to poll the cache on the next frame.
1986 static bool gLoadPending = false;
1987 if (gLoadPending) {
1988 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
1989 int hitCount) {
1990 CachedGLSL& entry(fCachedGLSL.push_back());
1991 entry.fKey = key;
1992 SkMD5 hash;
1993 hash.write(key->bytes(), key->size());
1994 SkMD5::Digest digest = hash.finish();
1995 for (int i = 0; i < 16; ++i) {
1996 entry.fKeyString.appendf("%02x", digest.data[i]);
1997 }
1998
Brian Osmana085a412019-04-25 09:44:43 -04001999 entry.fShaderType = GrPersistentCacheUtils::UnpackCachedShaders(
2000 data.get(), entry.fShader, entry.fInputs, kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002001 };
2002 fCachedGLSL.reset();
2003 fPersistentCache.foreach(collectShaders);
2004 gLoadPending = false;
2005 }
2006
2007 // Defer actually doing the load/save logic so that we can trigger a save when we
2008 // start or finish hovering on a tree node in the list below:
2009 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002010 bool doSave = ImGui::Button("Save");
2011 if (backendIsGL) {
2012 ImGui::SameLine();
2013 if (ImGui::Checkbox("SkSL", &params.fGrContextOptions.fCacheSKSL)) {
2014 paramsChanged = true;
2015 doLoad = true;
2016 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2017 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002018 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002019
2020 ImGui::BeginChild("##ScrollingRegion");
2021 for (auto& entry : fCachedGLSL) {
2022 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2023 bool hovered = ImGui::IsItemHovered();
2024 if (hovered != entry.fHovered) {
2025 // Force a save to patch the highlight shader in/out
2026 entry.fHovered = hovered;
2027 doSave = true;
2028 }
2029 if (inTreeNode) {
2030 // Full width, and a reasonable amount of space for each shader.
2031 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2032 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2033 boxSize);
2034 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2035 boxSize);
2036 ImGui::TreePop();
2037 }
2038 }
2039 ImGui::EndChild();
2040
2041 if (doLoad) {
2042 fPersistentCache.reset();
2043 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2044 gLoadPending = true;
2045 }
2046 if (doSave) {
2047 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002048 auto shaderCaps = ctx->priv().caps()->shaderCaps();
2049 bool sksl = params.fGrContextOptions.fCacheSKSL;
2050
Brian Osman072e6fc2019-06-12 11:35:41 -04002051 SkSL::String highlight;
2052 if (!sksl) {
2053 highlight = shaderCaps->versionDeclString();
2054 if (shaderCaps->usesPrecisionModifiers()) {
2055 highlight.append("precision mediump float;\n");
2056 }
Brian Osman5bee3902019-05-07 09:55:45 -04002057 }
2058 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002059 highlight.appendf("out %s sk_FragColor;\n"
2060 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2061 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002062
2063 fPersistentCache.reset();
2064 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2065 for (auto& entry : fCachedGLSL) {
2066 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2067 if (entry.fHovered) {
2068 entry.fShader[kFragment_GrShaderType] = highlight;
2069 }
2070
Brian Osmana085a412019-04-25 09:44:43 -04002071 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2072 entry.fShader,
2073 entry.fInputs,
2074 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002075 fPersistentCache.store(*entry.fKey, *data);
2076
2077 entry.fShader[kFragment_GrShaderType] = backup;
2078 }
2079 }
2080 }
Brian Osman79086b92017-02-10 13:36:16 -05002081 }
Brian Salomon99a33902017-03-07 15:16:34 -05002082 if (paramsChanged) {
2083 fDeferredActions.push_back([=]() {
2084 fWindow->setRequestedDisplayParams(params);
2085 fWindow->inval();
2086 this->updateTitle();
2087 });
2088 }
Brian Osman79086b92017-02-10 13:36:16 -05002089 ImGui::End();
2090 }
2091
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002092 if (gShaderErrorHandler.fErrors.count()) {
2093 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2094 ImGui::Begin("Shader Errors");
2095 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2096 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
2097 ImGui::TextWrapped("%s", gShaderErrorHandler.fShaders[i].c_str());
2098 }
2099 ImGui::End();
2100 gShaderErrorHandler.reset();
2101 }
2102
Brian Osmanf6877092017-02-13 09:39:57 -05002103 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002104 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2105 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002106 static int zoomFactor = 8;
2107 if (ImGui::Button("<<")) {
2108 zoomFactor = SkTMax(zoomFactor / 2, 4);
2109 }
2110 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2111 if (ImGui::Button(">>")) {
2112 zoomFactor = SkTMin(zoomFactor * 2, 32);
2113 }
Brian Osmanf6877092017-02-13 09:39:57 -05002114
Ben Wagner3627d2e2018-06-26 14:23:20 -04002115 if (!fZoomWindowFixed) {
2116 ImVec2 mousePos = ImGui::GetMousePos();
2117 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2118 }
2119 SkScalar x = fZoomWindowLocation.x();
2120 SkScalar y = fZoomWindowLocation.y();
2121 int xInt = SkScalarRoundToInt(x);
2122 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002123 ImVec2 avail = ImGui::GetContentRegionAvail();
2124
Brian Osmanead517d2017-11-13 15:36:36 -05002125 uint32_t pixel = 0;
2126 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002127 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002128 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002129 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002130 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002131 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002132 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2133 }
2134
Brian Osmand67e5182017-12-08 16:46:09 -05002135 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002136 // Translate so the region of the image that's under the mouse cursor is centered
2137 // in the zoom canvas:
2138 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002139 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2140 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002141 c->drawImage(this->fLastImage, 0, 0);
2142
2143 SkPaint outline;
2144 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002145 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002146 });
Brian Osmanf6877092017-02-13 09:39:57 -05002147 }
2148
2149 ImGui::End();
2150 }
Brian Osman79086b92017-02-10 13:36:16 -05002151}
2152
liyuqian2edb0f42016-07-06 14:11:32 -07002153void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002154 for (int i = 0; i < fDeferredActions.count(); ++i) {
2155 fDeferredActions[i]();
2156 }
2157 fDeferredActions.reset();
2158
Brian Osman56a24812017-12-19 11:15:16 -05002159 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002160 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002161 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002162 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002163
Brian Osman79086b92017-02-10 13:36:16 -05002164 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002165 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2166 // not be visible, though. So we need to redraw if there is at least one visible window, or
2167 // more than one active window. Newly created windows are active but not visible for one frame
2168 // while they determine their layout and sizing.
2169 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2170 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002171 fWindow->inval();
2172 }
jvanverth9f372462016-04-06 06:08:59 -07002173}
liyuqiane5a6cd92016-05-27 08:52:52 -07002174
Florin Malitab632df72018-06-18 21:23:06 -04002175template <typename OptionsFunc>
2176static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2177 OptionsFunc&& optionsFunc) {
2178 writer.beginObject();
2179 {
2180 writer.appendString(kName , name);
2181 writer.appendString(kValue, value);
2182
2183 writer.beginArray(kOptions);
2184 {
2185 optionsFunc(writer);
2186 }
2187 writer.endArray();
2188 }
2189 writer.endObject();
2190}
2191
2192
liyuqiane5a6cd92016-05-27 08:52:52 -07002193void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002194 if (!fWindow) {
2195 return;
2196 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002197 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002198 return; // Surface hasn't been created yet.
2199 }
2200
Florin Malitab632df72018-06-18 21:23:06 -04002201 SkDynamicMemoryWStream memStream;
2202 SkJSONWriter writer(&memStream);
2203 writer.beginArray();
2204
liyuqianb73c24b2016-06-03 08:47:23 -07002205 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002206 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2207 [this](SkJSONWriter& writer) {
2208 for(const auto& slide : fSlides) {
2209 writer.appendString(slide->getName().c_str());
2210 }
2211 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002212
liyuqianb73c24b2016-06-03 08:47:23 -07002213 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002214 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2215 [](SkJSONWriter& writer) {
2216 for (const auto& str : kBackendTypeStrings) {
2217 writer.appendString(str);
2218 }
2219 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002220
csmartdalton578f0642017-02-24 16:04:47 -07002221 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002222 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2223 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2224 [this](SkJSONWriter& writer) {
2225 writer.appendS32(0);
2226
2227 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2228 return;
2229 }
2230
2231 for (int msaa : {4, 8, 16}) {
2232 writer.appendS32(msaa);
2233 }
2234 });
csmartdalton578f0642017-02-24 16:04:47 -07002235
csmartdalton61cd31a2017-02-27 17:00:53 -07002236 // Path renderer state
2237 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002238 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2239 [this](SkJSONWriter& writer) {
2240 const GrContext* ctx = fWindow->getGrContext();
2241 if (!ctx) {
2242 writer.appendString("Software");
2243 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002244 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002245
Florin Malitab632df72018-06-18 21:23:06 -04002246 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2247 if (fWindow->sampleCount() > 1) {
2248 if (caps->shaderCaps()->pathRenderingSupport()) {
2249 writer.appendString(
2250 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2251 }
2252 } else {
2253 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2254 writer.appendString(
2255 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2256 }
2257 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2258 }
2259 writer.appendString(
2260 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2261 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2262 }
2263 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002264
liyuqianb73c24b2016-06-03 08:47:23 -07002265 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002266 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2267 [this](SkJSONWriter& writer) {
2268 writer.appendString(kSoftkeyHint);
2269 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2270 writer.appendString(softkey.c_str());
2271 }
2272 });
liyuqianb73c24b2016-06-03 08:47:23 -07002273
Florin Malitab632df72018-06-18 21:23:06 -04002274 writer.endArray();
2275 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002276
Florin Malitab632df72018-06-18 21:23:06 -04002277 auto data = memStream.detachAsData();
2278
2279 // TODO: would be cool to avoid this copy
2280 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2281
2282 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002283}
2284
2285void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002286 // For those who will add more features to handle the state change in this function:
2287 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2288 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2289 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002290 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002291 for (int i = 0; i < fSlides.count(); ++i) {
2292 if (fSlides[i]->getName().equals(stateValue)) {
2293 this->setCurrentSlide(i);
2294 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002295 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002296 }
Florin Malitaab99c342018-01-16 16:23:03 -05002297
2298 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002299 } else if (stateName.equals(kBackendStateName)) {
2300 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2301 if (stateValue.equals(kBackendTypeStrings[i])) {
2302 if (fBackendType != i) {
2303 fBackendType = (sk_app::Window::BackendType)i;
2304 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002305 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002306 }
2307 break;
2308 }
2309 }
csmartdalton578f0642017-02-24 16:04:47 -07002310 } else if (stateName.equals(kMSAAStateName)) {
2311 DisplayParams params = fWindow->getRequestedDisplayParams();
2312 int sampleCount = atoi(stateValue.c_str());
2313 if (sampleCount != params.fMSAASampleCount) {
2314 params.fMSAASampleCount = sampleCount;
2315 fWindow->setRequestedDisplayParams(params);
2316 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002317 this->updateTitle();
2318 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002319 }
2320 } else if (stateName.equals(kPathRendererStateName)) {
2321 DisplayParams params = fWindow->getRequestedDisplayParams();
2322 for (const auto& pair : gPathRendererNames) {
2323 if (pair.second == stateValue.c_str()) {
2324 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2325 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2326 fWindow->setRequestedDisplayParams(params);
2327 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002328 this->updateTitle();
2329 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002330 }
2331 break;
2332 }
csmartdalton578f0642017-02-24 16:04:47 -07002333 }
liyuqianb73c24b2016-06-03 08:47:23 -07002334 } else if (stateName.equals(kSoftkeyStateName)) {
2335 if (!stateValue.equals(kSoftkeyHint)) {
2336 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002337 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002338 }
liyuqian2edb0f42016-07-06 14:11:32 -07002339 } else if (stateName.equals(kRefreshStateName)) {
2340 // This state is actually NOT in the UI state.
2341 // We use this to allow Android to quickly set bool fRefresh.
2342 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002343 } else {
2344 SkDebugf("Unknown stateName: %s", stateName.c_str());
2345 }
2346}
Brian Osman79086b92017-02-10 13:36:16 -05002347
Hal Canaryff2e8fe2019-07-16 09:58:43 -04002348bool Viewer::onKey(sk_app::Window::Key key, InputState state, ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002349 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002350}
2351
Hal Canary3a85ed12019-07-08 16:07:57 -04002352bool Viewer::onChar(SkUnichar c, ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002353 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002354 fWindow->inval();
2355 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002356 } else {
2357 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002358 }
Brian Osman79086b92017-02-10 13:36:16 -05002359}