blob: fe116a94e3c0099aac990ea6521b66a764147f5d [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 });
brianosman622c8d52016-05-10 06:50:49 -0700352 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500353 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700354 fWindow->inval();
355 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400356 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500357 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400358 this->updateTitle();
359 fWindow->inval();
360 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500361 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500362 switch (fColorMode) {
363 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500364 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500365 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500366 case ColorMode::kColorManaged8888:
367 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500368 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500369 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500370 this->setColorMode(ColorMode::kLegacy);
371 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500372 }
brianosman622c8d52016-05-10 06:50:49 -0700373 });
374 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500375 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700376 });
377 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500378 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700379 });
380 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
381 this->changeZoomLevel(1.f / 32.f);
382 fWindow->inval();
383 });
384 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
385 this->changeZoomLevel(-1.f / 32.f);
386 fWindow->inval();
387 });
jvanverthaf236b52016-05-20 06:01:06 -0700388 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400389 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
390 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500391 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400392#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
393 if (newBackend == sk_app::Window::kVulkan_BackendType) {
394 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
395 sk_app::Window::kBackendTypeCount);
396 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
397 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500398 }
399#endif
Brian Osman621491e2017-02-28 15:45:01 -0500400 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700401 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500402 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
403 fSaveToSKP = true;
404 fWindow->inval();
405 });
Mike Reed376d8122019-03-14 11:39:02 -0400406 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
407 fShowSlideDimensions = !fShowSlideDimensions;
408 fWindow->inval();
409 });
Ben Wagner37c54032018-04-13 14:30:23 -0400410 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
411 DisplayParams params = fWindow->getRequestedDisplayParams();
412 uint32_t flags = params.fSurfaceProps.flags();
413 if (!fPixelGeometryOverrides) {
414 fPixelGeometryOverrides = true;
415 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
416 } else {
417 switch (params.fSurfaceProps.pixelGeometry()) {
418 case kUnknown_SkPixelGeometry:
419 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
420 break;
421 case kRGB_H_SkPixelGeometry:
422 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
423 break;
424 case kBGR_H_SkPixelGeometry:
425 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
426 break;
427 case kRGB_V_SkPixelGeometry:
428 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
429 break;
430 case kBGR_V_SkPixelGeometry:
431 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
432 fPixelGeometryOverrides = false;
433 break;
434 }
435 }
436 fWindow->setRequestedDisplayParams(params);
437 this->updateTitle();
438 fWindow->inval();
439 });
Ben Wagner9613e452019-01-23 10:34:59 -0500440 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500441 if (!fFontOverrides.fHinting) {
442 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400443 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500444 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500445 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400446 case SkFontHinting::kNone:
447 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500448 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400449 case SkFontHinting::kSlight:
450 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500451 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400452 case SkFontHinting::kNormal:
453 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500454 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400455 case SkFontHinting::kFull:
456 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500457 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500458 break;
459 }
460 }
461 this->updateTitle();
462 fWindow->inval();
463 });
464 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500465 if (!fPaintOverrides.fAntiAlias) {
466 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
467 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500468 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500469 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500470 } else {
471 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500472 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500473 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500474 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400475 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500476 break;
477 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500478 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500479 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400480 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500481 break;
482 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500483 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400484 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500485 break;
486 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500487 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
488 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500489 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
490 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500491 break;
492 }
493 }
494 this->updateTitle();
495 fWindow->inval();
496 });
Ben Wagner37c54032018-04-13 14:30:23 -0400497 fCommands.addCommand('D', "Modes", "DFT", [this]() {
498 DisplayParams params = fWindow->getRequestedDisplayParams();
499 uint32_t flags = params.fSurfaceProps.flags();
500 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
501 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
502 fWindow->setRequestedDisplayParams(params);
503 this->updateTitle();
504 fWindow->inval();
505 });
Ben Wagner9613e452019-01-23 10:34:59 -0500506 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500507 if (!fFontOverrides.fEdging) {
508 fFontOverrides.fEdging = true;
509 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500510 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500511 switch (fFont.getEdging()) {
512 case SkFont::Edging::kAlias:
513 fFont.setEdging(SkFont::Edging::kAntiAlias);
514 break;
515 case SkFont::Edging::kAntiAlias:
516 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
517 break;
518 case SkFont::Edging::kSubpixelAntiAlias:
519 fFont.setEdging(SkFont::Edging::kAlias);
520 fFontOverrides.fEdging = false;
521 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500522 }
523 }
524 this->updateTitle();
525 fWindow->inval();
526 });
Ben Wagner9613e452019-01-23 10:34:59 -0500527 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500528 if (!fFontOverrides.fSubpixel) {
529 fFontOverrides.fSubpixel = true;
530 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500531 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500532 if (!fFont.isSubpixel()) {
533 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500534 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500535 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500536 }
537 }
538 this->updateTitle();
539 fWindow->inval();
540 });
Brian Osman805a7272018-05-02 15:40:20 -0400541 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
542 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
543 : kPerspective_Real;
544 this->updateTitle();
545 fWindow->inval();
546 });
547 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
548 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
549 : kPerspective_Off;
550 this->updateTitle();
551 fWindow->inval();
552 });
Brian Osman207d4102019-01-10 09:40:58 -0500553 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
554 fAnimTimer.togglePauseResume();
555 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400556 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
557 fZoomUI = !fZoomUI;
558 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
559 fWindow->inval();
560 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500561
jvanverth2bb3b6d2016-04-08 07:24:09 -0700562 // set up slides
563 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500564 if (FLAGS_list) {
565 this->listNames();
566 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700567
Brian Osman9bb47cf2018-04-26 15:55:00 -0400568 fPerspectivePoints[0].set(0, 0);
569 fPerspectivePoints[1].set(1, 0);
570 fPerspectivePoints[2].set(0, 1);
571 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700572 fAnimTimer.run();
573
Hal Canaryc465d132017-12-08 10:21:31 -0500574 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500575 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400576 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500577 }
578 fImGuiGamutPaint.setColor(SK_ColorWHITE);
579 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
580
jongdeok.kim804f17e2019-02-26 14:39:23 +0900581 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500582 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700583}
584
jvanverth34524262016-05-04 13:49:13 -0700585void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400586 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
587 static const struct {
588 const char* fExtension;
589 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500590 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400591 const SlideFactory fFactory;
592 } gExternalSlidesInfo[] = {
593 { ".skp", "skp-dir", FLAGS_skps,
594 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
595 return sk_make_sp<SKPSlide>(name, path);}
596 },
597 { ".jpg", "jpg-dir", FLAGS_jpgs,
598 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
599 return sk_make_sp<ImageSlide>(name, path);}
600 },
Florin Malita87ccf332018-05-04 12:23:24 -0400601#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400602 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400603 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
604 return sk_make_sp<SkottieSlide>(name, path);}
605 },
Florin Malita87ccf332018-05-04 12:23:24 -0400606#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400607#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400608 { ".svg", "svg-dir", FLAGS_svgs,
609 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
610 return sk_make_sp<SvgSlide>(name, path);}
611 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400612#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400613 };
jvanverthc265a922016-04-08 12:51:45 -0700614
Brian Salomon343553a2018-09-05 15:41:23 -0400615 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700616
Mike Klein88544fb2019-03-20 10:50:33 -0500617 const auto addSlide =
618 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
619 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
620 return;
621 }
liyuqian6f163d22016-06-13 12:26:45 -0700622
Mike Klein88544fb2019-03-20 10:50:33 -0500623 if (auto slide = fact(name, path)) {
624 dirSlides.push_back(slide);
625 fSlides.push_back(std::move(slide));
626 }
627 };
Florin Malita76a076b2018-02-15 18:40:48 -0500628
Florin Malita38792ce2018-05-08 10:36:18 -0400629 if (!FLAGS_file.isEmpty()) {
630 // single file mode
631 const SkString file(FLAGS_file[0]);
632
633 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
634 for (const auto& sinfo : gExternalSlidesInfo) {
635 if (file.endsWith(sinfo.fExtension)) {
636 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
637 return;
638 }
639 }
640
641 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
642 } else {
643 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
644 }
645
646 return;
647 }
648
649 // Bisect slide.
650 if (!FLAGS_bisect.isEmpty()) {
651 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500652 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400653 if (FLAGS_bisect.count() >= 2) {
654 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
655 bisect->onChar(*ch);
656 }
657 }
658 fSlides.push_back(std::move(bisect));
659 }
660 }
661
662 // GMs
663 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400664 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
665 std::unique_ptr<skiagm::GM> gm(gmFactory(nullptr));
Mike Klein88544fb2019-03-20 10:50:33 -0500666 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400667 sk_sp<Slide> slide(new GMSlide(gm.release()));
668 fSlides.push_back(std::move(slide));
669 }
Florin Malita38792ce2018-05-08 10:36:18 -0400670 }
671 // reverse gms
672 int numGMs = fSlides.count() - firstGM;
673 for (int i = 0; i < numGMs/2; ++i) {
674 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
675 }
676
677 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400678 for (const SampleFactory factory : SampleRegistry::Range()) {
679 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500680 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400681 fSlides.push_back(slide);
682 }
Florin Malita38792ce2018-05-08 10:36:18 -0400683 }
684
Brian Osman7c979f52019-02-12 13:27:51 -0500685 // Particle demo
686 {
687 // TODO: Convert this to a sample
688 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500689 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500690 fSlides.push_back(std::move(slide));
691 }
692 }
693
Florin Malita0ffa3222018-04-05 14:34:45 -0400694 for (const auto& info : gExternalSlidesInfo) {
695 for (const auto& flag : info.fFlags) {
696 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
697 // single file
698 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
699 } else {
700 // directory
701 SkOSFile::Iter it(flag.c_str(), info.fExtension);
702 SkString name;
703 while (it.next(&name)) {
704 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
705 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400706 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400707 if (!dirSlides.empty()) {
708 fSlides.push_back(
709 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
710 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500711 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400712 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400713 }
714 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500715
716 if (!fSlides.count()) {
717 sk_sp<Slide> slide(new NullSlide());
718 fSlides.push_back(std::move(slide));
719 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700720}
721
722
jvanverth34524262016-05-04 13:49:13 -0700723Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700724 fWindow->detach();
725 delete fWindow;
726}
727
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500728struct SkPaintTitleUpdater {
729 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
730 void append(const char* s) {
731 if (fCount == 0) {
732 fTitle->append(" {");
733 } else {
734 fTitle->append(", ");
735 }
736 fTitle->append(s);
737 ++fCount;
738 }
739 void done() {
740 if (fCount > 0) {
741 fTitle->append("}");
742 }
743 }
744 SkString* fTitle;
745 int fCount;
746};
747
brianosman05de2162016-05-06 13:28:57 -0700748void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700749 if (!fWindow) {
750 return;
751 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500752 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700753 return; // Surface hasn't been created yet.
754 }
755
jvanverth34524262016-05-04 13:49:13 -0700756 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700757 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700758
Mike Kleine5acd752019-03-22 09:57:16 -0500759 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400760 if (gSkForceAnalyticAA) {
761 title.append(" <FAAA>");
762 } else {
763 title.append(" <AAA>");
764 }
765 }
766
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500767 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500768 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
769 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400770 const char* on, const char* off)
771 {
Ben Wagner9613e452019-01-23 10:34:59 -0500772 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400773 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500774 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400775 };
776
Ben Wagner9613e452019-01-23 10:34:59 -0500777 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
778 const char* on, const char* off)
779 {
780 if (fFontOverrides.*flag) {
781 paintTitle.append((fFont.*isFlag)() ? on : off);
782 }
783 };
784
785 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
786 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500787 if (fPaintOverrides.fFilterQuality) {
788 switch (fPaint.getFilterQuality()) {
789 case kNone_SkFilterQuality:
790 paintTitle.append("NoFilter");
791 break;
792 case kLow_SkFilterQuality:
793 paintTitle.append("LowFilter");
794 break;
795 case kMedium_SkFilterQuality:
796 paintTitle.append("MediumFilter");
797 break;
798 case kHigh_SkFilterQuality:
799 paintTitle.append("HighFilter");
800 break;
801 }
802 }
Ben Wagner9613e452019-01-23 10:34:59 -0500803
804 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
805 "Force Autohint", "No Force Autohint");
806 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
807 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
808 "Linear Metrics", "Non-Linear Metrics");
809 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
810 "Bitmap Text", "No Bitmap Text");
811 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
812
813 if (fFontOverrides.fEdging) {
814 switch (fFont.getEdging()) {
815 case SkFont::Edging::kAlias:
816 paintTitle.append("Alias Text");
817 break;
818 case SkFont::Edging::kAntiAlias:
819 paintTitle.append("Antialias Text");
820 break;
821 case SkFont::Edging::kSubpixelAntiAlias:
822 paintTitle.append("Subpixel Antialias Text");
823 break;
824 }
825 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400826
Mike Reed3ae47332019-01-04 10:11:46 -0500827 if (fFontOverrides.fHinting) {
828 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400829 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500830 paintTitle.append("No Hinting");
831 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400832 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500833 paintTitle.append("Slight Hinting");
834 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400835 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500836 paintTitle.append("Normal Hinting");
837 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400838 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500839 paintTitle.append("Full Hinting");
840 break;
841 }
842 }
843 paintTitle.done();
844
Brian Osman92004802017-03-06 11:47:26 -0500845 switch (fColorMode) {
846 case ColorMode::kLegacy:
847 title.append(" Legacy 8888");
848 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500849 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500850 title.append(" ColorManaged 8888");
851 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500852 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500853 title.append(" ColorManaged F16");
854 break;
855 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500856
Brian Osman92004802017-03-06 11:47:26 -0500857 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500858 int curPrimaries = -1;
859 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
860 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
861 curPrimaries = i;
862 break;
863 }
864 }
Brian Osman03115dc2018-11-26 13:55:19 -0500865 title.appendf(" %s Gamma %f",
866 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500867 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700868 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500869
Ben Wagner37c54032018-04-13 14:30:23 -0400870 const DisplayParams& params = fWindow->getRequestedDisplayParams();
871 if (fPixelGeometryOverrides) {
872 switch (params.fSurfaceProps.pixelGeometry()) {
873 case kUnknown_SkPixelGeometry:
874 title.append( " Flat");
875 break;
876 case kRGB_H_SkPixelGeometry:
877 title.append( " RGB");
878 break;
879 case kBGR_H_SkPixelGeometry:
880 title.append( " BGR");
881 break;
882 case kRGB_V_SkPixelGeometry:
883 title.append( " RGBV");
884 break;
885 case kBGR_V_SkPixelGeometry:
886 title.append( " BGRV");
887 break;
888 }
889 }
890
891 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
892 title.append(" DFT");
893 }
894
csmartdalton578f0642017-02-24 16:04:47 -0700895 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700896 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500897 int msaa = fWindow->sampleCount();
898 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700899 title.appendf(" MSAA: %i", msaa);
900 }
901 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700902
903 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600904 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700905 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
906 }
907
Brian Osman805a7272018-05-02 15:40:20 -0400908 if (kPerspective_Real == fPerspectiveMode) {
909 title.append(" Perpsective (Real)");
910 } else if (kPerspective_Fake == fPerspectiveMode) {
911 title.append(" Perspective (Fake)");
912 }
913
brianosman05de2162016-05-06 13:28:57 -0700914 fWindow->setTitle(title.c_str());
915}
916
Florin Malitaab99c342018-01-16 16:23:03 -0500917int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500918
919 if (!FLAGS_slide.isEmpty()) {
920 int count = fSlides.count();
921 for (int i = 0; i < count; i++) {
922 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500923 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500924 }
925 }
926
927 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
928 this->listNames();
929 }
930
Florin Malitaab99c342018-01-16 16:23:03 -0500931 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500932}
933
Florin Malitaab99c342018-01-16 16:23:03 -0500934void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500935 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500936 for (const auto& slide : fSlides) {
937 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500938 }
939}
940
Florin Malitaab99c342018-01-16 16:23:03 -0500941void Viewer::setCurrentSlide(int slide) {
942 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700943
Florin Malitaab99c342018-01-16 16:23:03 -0500944 if (slide == fCurrentSlide) {
945 return;
946 }
947
948 if (fCurrentSlide >= 0) {
949 fSlides[fCurrentSlide]->unload();
950 }
951
952 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
953 SkIntToScalar(fWindow->height()));
954 fCurrentSlide = slide;
955 this->setupCurrentSlide();
956}
957
958void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500959 if (fCurrentSlide >= 0) {
960 // prepare dimensions for image slides
961 fGesture.resetTouchState();
962 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700963
Jim Van Verth0848fb02018-01-22 13:39:30 -0500964 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
965 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
966 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400967
Jim Van Verth0848fb02018-01-22 13:39:30 -0500968 // Start with a matrix that scales the slide to the available screen space
969 if (fWindow->scaleContentToFit()) {
970 if (windowRect.width() > 0 && windowRect.height() > 0) {
971 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
972 }
liyuqiane46e4f02016-05-20 07:32:19 -0700973 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500974
975 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400976 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500977
978 this->updateTitle();
979 this->updateUIState();
980
981 fStatsLayer.resetMeasurements();
982
983 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700984 }
jvanverthc265a922016-04-08 12:51:45 -0700985}
986
987#define MAX_ZOOM_LEVEL 8
988#define MIN_ZOOM_LEVEL -8
989
jvanverth34524262016-05-04 13:49:13 -0700990void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700991 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400992 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400993 this->preTouchMatrixChanged();
994}
Yuqian Li755778c2018-03-28 16:23:31 -0400995
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400996void Viewer::preTouchMatrixChanged() {
997 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400998 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
999 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1000 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1001 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1002}
1003
Brian Osman805a7272018-05-02 15:40:20 -04001004SkMatrix Viewer::computePerspectiveMatrix() {
1005 SkScalar w = fWindow->width(), h = fWindow->height();
1006 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1007 SkPoint perspPts[4] = {
1008 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1009 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1010 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1011 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1012 };
1013 SkMatrix m;
1014 m.setPolyToPoly(orthoPts, perspPts, 4);
1015 return m;
1016}
1017
Yuqian Li755778c2018-03-28 16:23:31 -04001018SkMatrix Viewer::computePreTouchMatrix() {
1019 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001020
1021 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001022 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001023 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001024
1025 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1026 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001027
Brian Osman805a7272018-05-02 15:40:20 -04001028 if (kPerspective_Real == fPerspectiveMode) {
1029 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001030 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001031 }
1032
Yuqian Li755778c2018-03-28 16:23:31 -04001033 return m;
jvanverthc265a922016-04-08 12:51:45 -07001034}
1035
liyuqiand3cdbca2016-05-17 12:44:20 -07001036SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001037 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001038 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001039 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001040 return m;
jvanverthc265a922016-04-08 12:51:45 -07001041}
1042
Brian Osman621491e2017-02-28 15:45:01 -05001043void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001044 fPersistentCache.reset();
1045 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001046 fBackendType = backendType;
1047
1048 fWindow->detach();
1049
Brian Osman70d2f432017-11-08 09:54:10 -05001050#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001051 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1052 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001053 DisplayParams params = fWindow->getRequestedDisplayParams();
1054 delete fWindow;
1055 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001056
Brian Osman70d2f432017-11-08 09:54:10 -05001057 // re-register callbacks
1058 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001059 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001060 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001061 fWindow->pushLayer(&fImGuiLayer);
1062
Brian Osman70d2f432017-11-08 09:54:10 -05001063 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1064 // will still include our correct sample count. But the re-created fWindow will lose that
1065 // information. On Windows, we need to re-create the window when changing sample count,
1066 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1067 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1068 // as if we had never changed windows at all).
1069 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001070#endif
1071
Brian Osman70d2f432017-11-08 09:54:10 -05001072 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001073}
1074
Brian Osman92004802017-03-06 11:47:26 -05001075void Viewer::setColorMode(ColorMode colorMode) {
1076 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001077 this->updateTitle();
1078 fWindow->inval();
1079}
1080
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001081class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1082public:
Mike Reed3ae47332019-01-04 10:11:46 -05001083 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1084 SkFont* font, Viewer::SkFontFields* ffields)
1085 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001086 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001087 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1088 sk_sp<SkTextBlob>* cache) {
1089 bool blobWillChange = false;
1090 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001091 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1092 bool shouldDraw = this->filterFont(&filteredFont);
1093 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001094 blobWillChange = true;
1095 break;
1096 }
1097 }
1098 if (!blobWillChange) {
1099 return blob;
1100 }
1101
1102 SkTextBlobBuilder builder;
1103 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001104 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1105 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001106 if (!shouldDraw) {
1107 continue;
1108 }
1109
Mike Reed3ae47332019-01-04 10:11:46 -05001110 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001111
Ben Wagner41e40472018-09-24 13:01:54 -04001112 const SkTextBlobBuilder::RunBuffer& runBuffer
1113 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001114 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001115 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001116 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001117 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001118 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001119 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001120 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001121 it.glyphCount(), it.textSize(), SkString())
1122 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1123 uint32_t glyphCount = it.glyphCount();
1124 if (it.glyphs()) {
1125 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1126 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1127 }
1128 if (it.pos()) {
1129 size_t posSize = sizeof(decltype(*it.pos()));
1130 uint8_t positioning = it.positioning();
1131 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1132 }
1133 if (it.text()) {
1134 size_t textSize = sizeof(decltype(*it.text()));
1135 uint32_t textCount = it.textSize();
1136 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1137 }
1138 if (it.clusters()) {
1139 size_t clusterSize = sizeof(decltype(*it.clusters()));
1140 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1141 }
1142 }
1143 *cache = builder.make();
1144 return cache->get();
1145 }
1146 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1147 const SkPaint& paint) override {
1148 sk_sp<SkTextBlob> cache;
1149 this->SkPaintFilterCanvas::onDrawTextBlob(
1150 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1151 }
Mike Reed3ae47332019-01-04 10:11:46 -05001152 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001153 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001154 font->writable()->setSize(fFont->getSize());
1155 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001156 if (fFontOverrides->fScaleX) {
1157 font->writable()->setScaleX(fFont->getScaleX());
1158 }
1159 if (fFontOverrides->fSkewX) {
1160 font->writable()->setSkewX(fFont->getSkewX());
1161 }
Mike Reed3ae47332019-01-04 10:11:46 -05001162 if (fFontOverrides->fHinting) {
1163 font->writable()->setHinting(fFont->getHinting());
1164 }
Ben Wagner9613e452019-01-23 10:34:59 -05001165 if (fFontOverrides->fEdging) {
1166 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001167 }
Ben Wagner9613e452019-01-23 10:34:59 -05001168 if (fFontOverrides->fEmbolden) {
1169 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001170 }
Ben Wagner9613e452019-01-23 10:34:59 -05001171 if (fFontOverrides->fLinearMetrics) {
1172 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001173 }
Ben Wagner9613e452019-01-23 10:34:59 -05001174 if (fFontOverrides->fSubpixel) {
1175 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001176 }
Ben Wagner9613e452019-01-23 10:34:59 -05001177 if (fFontOverrides->fEmbeddedBitmaps) {
1178 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001179 }
Ben Wagner9613e452019-01-23 10:34:59 -05001180 if (fFontOverrides->fForceAutoHinting) {
1181 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001182 }
Ben Wagner9613e452019-01-23 10:34:59 -05001183
Mike Reed3ae47332019-01-04 10:11:46 -05001184 return true;
1185 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001186 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001187 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001188 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001189 }
Ben Wagner9613e452019-01-23 10:34:59 -05001190 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001191 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001192 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001193 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001194 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001195 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001196 return true;
1197 }
1198 SkPaint* fPaint;
1199 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001200 SkFont* fFont;
1201 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001202};
1203
Robert Phillips9882dae2019-03-04 11:00:10 -05001204void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001205 if (fCurrentSlide < 0) {
1206 return;
1207 }
1208
Robert Phillips9882dae2019-03-04 11:00:10 -05001209 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001210
Brian Osmanf750fbc2017-02-08 10:47:28 -05001211 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001212 SkSurface* slideSurface = surface;
1213 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001214 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001215
Brian Osmane0d4fba2017-03-15 10:24:55 -04001216 // 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 -05001217 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001218 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001219 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001220 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001221 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001222 }
1223
Brian Osman3ac99cf2017-12-01 11:23:53 -05001224 if (fSaveToSKP) {
1225 SkPictureRecorder recorder;
1226 SkCanvas* recorderCanvas = recorder.beginRecording(
1227 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001228 fSlides[fCurrentSlide]->draw(recorderCanvas);
1229 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1230 SkFILEWStream stream("sample_app.skp");
1231 picture->serialize(&stream);
1232 fSaveToSKP = false;
1233 }
1234
Brian Osmane9ed0f02018-11-26 14:50:05 -05001235 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
1236 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode) ? kRGBA_F16_SkColorType
1237 : kN32_SkColorType;
Brian Osmane9ed0f02018-11-26 14:50:05 -05001238
1239 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001240 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1241 slideCanvas->getProps(&props);
1242
Brian Osmane9ed0f02018-11-26 14:50:05 -05001243 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1244 return Window::kRaster_BackendType == this->fBackendType
1245 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001246 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001247 };
1248
Brian Osman03115dc2018-11-26 13:55:19 -05001249 // We need to render offscreen if we're...
1250 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1251 // ... in any raster mode, because the window surface is actually GL
1252 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001253 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001254 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001255 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001256 Window::kRaster_BackendType == fBackendType ||
1257 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001258
Brian Osmane9ed0f02018-11-26 14:50:05 -05001259 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001260 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001261 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001262 }
1263
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001264 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001265 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001266 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001267 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001268 if (fTiled) {
1269 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1270 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1271 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1272 SkCanvas* tileCanvas = tileSurface->getCanvas();
1273 SkMatrix m = this->computeMatrix();
1274 for (int y = 0; y < fWindow->height(); y += tileH) {
1275 for (int x = 0; x < fWindow->width(); x += tileW) {
1276 SkAutoCanvasRestore acr(tileCanvas, true);
1277 tileCanvas->translate(-x, -y);
1278 tileCanvas->clear(SK_ColorTRANSPARENT);
1279 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001280 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1281 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001282 fSlides[fCurrentSlide]->draw(&filterCanvas);
1283 tileSurface->draw(slideCanvas, x, y, nullptr);
1284 }
1285 }
1286
1287 // Draw borders between tiles
1288 if (fDrawTileBoundaries) {
1289 SkPaint border;
1290 border.setColor(0x60FF00FF);
1291 border.setStyle(SkPaint::kStroke_Style);
1292 for (int y = 0; y < fWindow->height(); y += tileH) {
1293 for (int x = 0; x < fWindow->width(); x += tileW) {
1294 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1295 }
1296 }
1297 }
1298 } else {
1299 slideCanvas->concat(this->computeMatrix());
1300 if (kPerspective_Real == fPerspectiveMode) {
1301 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1302 }
Mike Reed3ae47332019-01-04 10:11:46 -05001303 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001304 fSlides[fCurrentSlide]->draw(&filterCanvas);
1305 }
Brian Osman56a24812017-12-19 11:15:16 -05001306 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001307 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001308
1309 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001310 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001311 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001312 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001313
1314 // If we rendered offscreen, snap an image and push the results to the window's canvas
1315 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001316 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001317
Robert Phillips9882dae2019-03-04 11:00:10 -05001318 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001319 SkPaint paint;
1320 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001321 int prePerspectiveCount = canvas->save();
1322 if (kPerspective_Fake == fPerspectiveMode) {
1323 paint.setFilterQuality(kHigh_SkFilterQuality);
1324 canvas->clear(SK_ColorWHITE);
1325 canvas->concat(this->computePerspectiveMatrix());
1326 }
Brian Osman03115dc2018-11-26 13:55:19 -05001327 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001328 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001329 }
Mike Reed376d8122019-03-14 11:39:02 -04001330
1331 if (fShowSlideDimensions) {
1332 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1333 SkPaint paint;
1334 paint.setColor(0x40FFFF00);
1335 surface->getCanvas()->drawRect(r, paint);
1336 }
liyuqian6f163d22016-06-13 12:26:45 -07001337}
1338
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001339void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001340 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001341 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001342}
Jim Van Verth6f449692017-02-14 15:16:46 -05001343
Robert Phillips9882dae2019-03-04 11:00:10 -05001344void Viewer::onPaint(SkSurface* surface) {
1345 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001346
Robert Phillips9882dae2019-03-04 11:00:10 -05001347 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001348
Brian Osmand67e5182017-12-08 16:46:09 -05001349 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001350
1351 if (GrContext* ctx = fWindow->getGrContext()) {
1352 // Clean out cache items that haven't been used in more than 10 seconds.
1353 ctx->performDeferredCleanup(std::chrono::seconds(10));
1354 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001355}
1356
Ben Wagnera1915972018-08-09 15:06:19 -04001357void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001358 if (fCurrentSlide >= 0) {
1359 fSlides[fCurrentSlide]->resize(width, height);
1360 }
Ben Wagnera1915972018-08-09 15:06:19 -04001361}
1362
Florin Malitacefc1b92018-02-19 21:43:47 -05001363SkPoint Viewer::mapEvent(float x, float y) {
1364 const auto m = this->computeMatrix();
1365 SkMatrix inv;
1366
1367 SkAssertResult(m.invert(&inv));
1368
1369 return inv.mapXY(x, y);
1370}
1371
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001372bool Viewer::onTouch(intptr_t owner, InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001373 if (GestureDevice::kMouse == fGestureDevice) {
1374 return false;
1375 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001376
1377 const auto slidePt = this->mapEvent(x, y);
Hal Canary3a85ed12019-07-08 16:07:57 -04001378 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001379 fWindow->inval();
1380 return true;
1381 }
1382
liyuqiand3cdbca2016-05-17 12:44:20 -07001383 void* castedOwner = reinterpret_cast<void*>(owner);
1384 switch (state) {
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001385 case InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001386 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001387#if defined(SK_BUILD_FOR_IOS)
1388 // TODO: move IOS swipe detection higher up into the platform code
1389 SkPoint dir;
1390 if (fGesture.isFling(&dir)) {
1391 // swiping left or right
1392 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1393 if (dir.fX < 0) {
1394 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1395 fCurrentSlide + 1 : 0);
1396 } else {
1397 this->setCurrentSlide(fCurrentSlide > 0 ?
1398 fCurrentSlide - 1 : fSlides.count() - 1);
1399 }
1400 }
1401 fGesture.reset();
1402 }
1403#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001404 break;
1405 }
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001406 case InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001407 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001408 break;
1409 }
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001410 case InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001411 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001412 break;
1413 }
1414 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001415 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001416 fWindow->inval();
1417 return true;
1418}
1419
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001420bool Viewer::onMouse(int x, int y, InputState state, ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001421 if (GestureDevice::kTouch == fGestureDevice) {
1422 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001423 }
Brian Osman16c81a12017-12-20 11:58:34 -05001424
Florin Malitacefc1b92018-02-19 21:43:47 -05001425 const auto slidePt = this->mapEvent(x, y);
1426 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1427 fWindow->inval();
1428 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001429 }
1430
1431 switch (state) {
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001432 case InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001433 fGesture.touchEnd(nullptr);
1434 break;
1435 }
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001436 case InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001437 fGesture.touchBegin(nullptr, x, y);
1438 break;
1439 }
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001440 case InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001441 fGesture.touchMoved(nullptr, x, y);
1442 break;
1443 }
1444 }
1445 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1446
Hal Canaryff2e8fe2019-07-16 09:58:43 -04001447 if (state != InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001448 fWindow->inval();
1449 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001450 return true;
1451}
1452
Brian Osmana109e392017-02-24 09:49:14 -05001453static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001454 // The gamut image covers a (0.8 x 0.9) shaped region
1455 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001456
1457 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1458 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1459 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001460 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1461 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1462 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001463
Brian Osman535c5e32019-02-09 16:32:58 -05001464 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1465 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1466 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1467 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1468 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001469}
1470
Ben Wagner3627d2e2018-06-26 14:23:20 -04001471static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001472 ImGui::DragCanvas dc(pt);
1473 dc.fillColor(IM_COL32(0, 0, 0, 128));
1474 dc.dragPoint(pt);
1475 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001476}
1477
Brian Osman9bb47cf2018-04-26 15:55:00 -04001478static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001479 ImGui::DragCanvas dc(pts);
1480 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001481
Brian Osman535c5e32019-02-09 16:32:58 -05001482 for (int i = 0; i < 4; ++i) {
1483 dc.dragPoint(pts + i);
1484 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001485
Brian Osman535c5e32019-02-09 16:32:58 -05001486 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1487 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1488 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1489 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001490
Brian Osman535c5e32019-02-09 16:32:58 -05001491 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001492}
1493
Brian Osmand67e5182017-12-08 16:46:09 -05001494void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001495 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1496 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001497 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001498 }
1499
1500 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001501 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1502 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001503 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001504 DisplayParams params = fWindow->getRequestedDisplayParams();
1505 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001506 const GrContext* ctx = fWindow->getGrContext();
1507
Brian Osmana109e392017-02-24 09:49:14 -05001508 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1509 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001510 if (ImGui::CollapsingHeader("Backend")) {
1511 int newBackend = static_cast<int>(fBackendType);
1512 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1513 ImGui::SameLine();
1514 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001515#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1516 ImGui::SameLine();
1517 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1518#endif
Brian Osman621491e2017-02-28 15:45:01 -05001519#if defined(SK_VULKAN)
1520 ImGui::SameLine();
1521 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1522#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -05001523#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
1524 ImGui::SameLine();
1525 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1526#endif
Brian Osman621491e2017-02-28 15:45:01 -05001527 if (newBackend != fBackendType) {
1528 fDeferredActions.push_back([=]() {
1529 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1530 });
1531 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001532
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001533 bool* wire = &params.fGrContextOptions.fWireframeMode;
1534 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1535 paramsChanged = true;
1536 }
Brian Salomon99a33902017-03-07 15:16:34 -05001537
Brian Osman28b12522017-03-08 17:10:24 -05001538 if (ctx) {
1539 int sampleCount = fWindow->sampleCount();
1540 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001541 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001542 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1543 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1544 ImGui::RadioButton("16", &sampleCount, 16);
1545
1546 if (sampleCount != params.fMSAASampleCount) {
1547 params.fMSAASampleCount = sampleCount;
1548 paramsChanged = true;
1549 }
1550 }
1551
Ben Wagner37c54032018-04-13 14:30:23 -04001552 int pixelGeometryIdx = 0;
1553 if (fPixelGeometryOverrides) {
1554 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1555 }
1556 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1557 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1558 {
1559 uint32_t flags = params.fSurfaceProps.flags();
1560 if (pixelGeometryIdx == 0) {
1561 fPixelGeometryOverrides = false;
1562 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1563 } else {
1564 fPixelGeometryOverrides = true;
1565 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1566 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1567 }
1568 paramsChanged = true;
1569 }
1570
1571 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1572 if (ImGui::Checkbox("DFT", &useDFT)) {
1573 uint32_t flags = params.fSurfaceProps.flags();
1574 if (useDFT) {
1575 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1576 } else {
1577 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1578 }
1579 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1580 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1581 paramsChanged = true;
1582 }
1583
Brian Osman8a9de3d2017-03-01 14:59:05 -05001584 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001585 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001586 auto prButton = [&](GpuPathRenderers x) {
1587 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001588 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1589 params.fGrContextOptions.fGpuPathRenderers = x;
1590 paramsChanged = true;
1591 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001592 }
1593 };
1594
1595 if (!ctx) {
1596 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001597 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001598 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001599 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001600 prButton(GpuPathRenderers::kStencilAndCover);
1601 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001602 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001603 prButton(GpuPathRenderers::kNone);
1604 } else {
1605 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001606 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001607 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001608 prButton(GpuPathRenderers::kCoverageCounting);
1609 }
Jim Van Verth83010462017-03-16 08:45:39 -04001610 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001611 prButton(GpuPathRenderers::kTessellating);
1612 prButton(GpuPathRenderers::kNone);
1613 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001614 ImGui::TreePop();
1615 }
Brian Osman621491e2017-02-28 15:45:01 -05001616 }
1617
Ben Wagner964571d2019-03-08 12:35:06 -05001618 if (ImGui::CollapsingHeader("Tiling")) {
1619 ImGui::Checkbox("Enable", &fTiled);
1620 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1621 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1622 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1623 }
1624
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001625 if (ImGui::CollapsingHeader("Transform")) {
1626 float zoom = fZoomLevel;
1627 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1628 fZoomLevel = zoom;
1629 this->preTouchMatrixChanged();
1630 paramsChanged = true;
1631 }
1632 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001633 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001634 fRotation = deg;
1635 this->preTouchMatrixChanged();
1636 paramsChanged = true;
1637 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001638 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1639 if (ImGui_DragLocation(&fOffset)) {
1640 this->preTouchMatrixChanged();
1641 paramsChanged = true;
1642 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001643 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1644 this->preTouchMatrixChanged();
1645 paramsChanged = true;
1646 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001647 }
Brian Osman805a7272018-05-02 15:40:20 -04001648 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1649 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1650 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001651 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001652 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001653 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001654 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
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 Wagnerd02a74d2018-04-23 12:55:06 -04001658 }
1659
Ben Wagnera580fb32018-04-17 11:16:32 -04001660 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001661 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001662 if (fPaintOverrides.fAntiAlias) {
1663 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001664 }
1665 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001666 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001667 {
1668 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1669 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001670 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001671 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1672 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001673 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001674 fPaintOverrides.fAntiAlias = true;
1675 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001676 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001677 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001678 case SkPaintFields::AntiAliasState::Alias:
1679 break;
1680 case SkPaintFields::AntiAliasState::Normal:
1681 break;
1682 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1683 gSkUseAnalyticAA = true;
1684 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001685 break;
1686 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1687 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001688 break;
1689 }
1690 }
1691 paramsChanged = true;
1692 }
1693
Ben Wagner99a78dc2018-05-09 18:23:51 -04001694 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001695 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001696 bool (SkPaint::* isFlag)() const,
1697 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001698 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001699 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001700 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001701 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001702 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001703 if (ImGui::Combo(label, &itemIndex, items)) {
1704 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001705 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001706 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001707 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001708 (fPaint.*setFlag)(itemIndex == 2);
1709 }
1710 paramsChanged = true;
1711 }
1712 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001713
Ben Wagner99a78dc2018-05-09 18:23:51 -04001714 paintFlag("Dither",
1715 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001716 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001717 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001718
1719 int filterQualityIdx = 0;
1720 if (fPaintOverrides.fFilterQuality) {
1721 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1722 }
1723 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1724 "Default\0None\0Low\0Medium\0High\0\0"))
1725 {
1726 if (filterQualityIdx == 0) {
1727 fPaintOverrides.fFilterQuality = false;
1728 fPaint.setFilterQuality(kNone_SkFilterQuality);
1729 } else {
1730 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1731 fPaintOverrides.fFilterQuality = true;
1732 }
1733 paramsChanged = true;
1734 }
Ben Wagner9613e452019-01-23 10:34:59 -05001735 }
Hal Canary02738a82019-01-21 18:51:32 +00001736
Ben Wagner9613e452019-01-23 10:34:59 -05001737 if (ImGui::CollapsingHeader("Font")) {
1738 int hintingIdx = 0;
1739 if (fFontOverrides.fHinting) {
1740 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1741 }
1742 if (ImGui::Combo("Hinting", &hintingIdx,
1743 "Default\0None\0Slight\0Normal\0Full\0\0"))
1744 {
1745 if (hintingIdx == 0) {
1746 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001747 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001748 } else {
1749 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1750 fFontOverrides.fHinting = true;
1751 }
1752 paramsChanged = true;
1753 }
Hal Canary02738a82019-01-21 18:51:32 +00001754
Ben Wagner9613e452019-01-23 10:34:59 -05001755 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1756 bool SkFontFields::* flag,
1757 bool (SkFont::* isFlag)() const,
1758 void (SkFont::* setFlag)(bool) )
1759 {
1760 int itemIndex = 0;
1761 if (fFontOverrides.*flag) {
1762 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1763 }
1764 if (ImGui::Combo(label, &itemIndex, items)) {
1765 if (itemIndex == 0) {
1766 fFontOverrides.*flag = false;
1767 } else {
1768 fFontOverrides.*flag = true;
1769 (fFont.*setFlag)(itemIndex == 2);
1770 }
1771 paramsChanged = true;
1772 }
1773 };
Hal Canary02738a82019-01-21 18:51:32 +00001774
Ben Wagner9613e452019-01-23 10:34:59 -05001775 fontFlag("Fake Bold Glyphs",
1776 "Default\0No Fake Bold\0Fake Bold\0\0",
1777 &SkFontFields::fEmbolden,
1778 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001779
Ben Wagner9613e452019-01-23 10:34:59 -05001780 fontFlag("Linear Text",
1781 "Default\0No Linear Text\0Linear Text\0\0",
1782 &SkFontFields::fLinearMetrics,
1783 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001784
Ben Wagner9613e452019-01-23 10:34:59 -05001785 fontFlag("Subpixel Position Glyphs",
1786 "Default\0Pixel Text\0Subpixel Text\0\0",
1787 &SkFontFields::fSubpixel,
1788 &SkFont::isSubpixel, &SkFont::setSubpixel);
1789
1790 fontFlag("Embedded Bitmap Text",
1791 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1792 &SkFontFields::fEmbeddedBitmaps,
1793 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1794
1795 fontFlag("Force Auto-Hinting",
1796 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1797 &SkFontFields::fForceAutoHinting,
1798 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1799
1800 int edgingIdx = 0;
1801 if (fFontOverrides.fEdging) {
1802 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1803 }
1804 if (ImGui::Combo("Edging", &edgingIdx,
1805 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1806 {
1807 if (edgingIdx == 0) {
1808 fFontOverrides.fEdging = false;
1809 fFont.setEdging(SkFont::Edging::kAlias);
1810 } else {
1811 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1812 fFontOverrides.fEdging = true;
1813 }
1814 paramsChanged = true;
1815 }
1816
Ben Wagner15a8d572019-03-21 13:35:44 -04001817 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1818 if (fFontOverrides.fSize) {
1819 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001820 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001821 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001822 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001823 fFontOverrides.fSizeRange[0],
1824 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001825 "%.6f", 2.0f))
1826 {
Mike Reed3ae47332019-01-04 10:11:46 -05001827 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001828 paramsChanged = true;
1829 }
1830 }
1831
1832 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1833 if (fFontOverrides.fScaleX) {
1834 float scaleX = fFont.getScaleX();
1835 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1836 fFont.setScaleX(scaleX);
1837 paramsChanged = true;
1838 }
1839 }
1840
1841 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1842 if (fFontOverrides.fSkewX) {
1843 float skewX = fFont.getSkewX();
1844 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1845 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001846 paramsChanged = true;
1847 }
1848 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001849 }
1850
Mike Reed81f60ec2018-05-15 10:09:52 -04001851 {
1852 SkMetaData controls;
1853 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1854 if (ImGui::CollapsingHeader("Current Slide")) {
1855 SkMetaData::Iter iter(controls);
1856 const char* name;
1857 SkMetaData::Type type;
1858 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001859 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001860 if (type == SkMetaData::kScalar_Type) {
1861 float val[3];
1862 SkASSERT(count == 3);
1863 controls.findScalars(name, &count, val);
1864 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1865 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001866 }
Ben Wagner110c7032019-03-22 17:03:59 -04001867 } else if (type == SkMetaData::kBool_Type) {
1868 bool val;
1869 SkASSERT(count == 1);
1870 controls.findBool(name, &val);
1871 if (ImGui::Checkbox(name, &val)) {
1872 controls.setBool(name, val);
1873 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001874 }
1875 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001876 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001877 }
1878 }
1879 }
1880
Ben Wagner7a3c6742018-04-23 10:01:07 -04001881 if (fShowSlidePicker) {
1882 ImGui::SetNextTreeNodeOpen(true);
1883 }
Brian Osman79086b92017-02-10 13:36:16 -05001884 if (ImGui::CollapsingHeader("Slide")) {
1885 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001886 static ImVector<const char*> filteredSlideNames;
1887 static ImVector<int> filteredSlideIndices;
1888
Brian Osmanfce09c52017-11-14 15:32:20 -05001889 if (fShowSlidePicker) {
1890 ImGui::SetKeyboardFocusHere();
1891 fShowSlidePicker = false;
1892 }
1893
Brian Osman79086b92017-02-10 13:36:16 -05001894 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001895 filteredSlideNames.clear();
1896 filteredSlideIndices.clear();
1897 int filteredIndex = 0;
1898 for (int i = 0; i < fSlides.count(); ++i) {
1899 const char* slideName = fSlides[i]->getName().c_str();
1900 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1901 if (i == fCurrentSlide) {
1902 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001903 }
Brian Osmanf479e422017-11-08 13:11:36 -05001904 filteredSlideNames.push_back(slideName);
1905 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001906 }
Brian Osman79086b92017-02-10 13:36:16 -05001907 }
Brian Osmanf479e422017-11-08 13:11:36 -05001908
Brian Osmanf479e422017-11-08 13:11:36 -05001909 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1910 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001911 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001912 }
1913 }
Brian Osmana109e392017-02-24 09:49:14 -05001914
1915 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001916 ColorMode newMode = fColorMode;
1917 auto cmButton = [&](ColorMode mode, const char* label) {
1918 if (ImGui::RadioButton(label, mode == fColorMode)) {
1919 newMode = mode;
1920 }
1921 };
1922
1923 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001924 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1925 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001926
1927 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001928 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001929 }
1930
1931 // Pick from common gamuts:
1932 int primariesIdx = 4; // Default: Custom
1933 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1934 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1935 primariesIdx = i;
1936 break;
1937 }
1938 }
1939
Brian Osman03115dc2018-11-26 13:55:19 -05001940 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05001941 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001942
Brian Osmana109e392017-02-24 09:49:14 -05001943 if (ImGui::Combo("Primaries", &primariesIdx,
1944 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1945 if (primariesIdx >= 0 && primariesIdx <= 3) {
1946 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1947 }
1948 }
1949
1950 // Allow direct editing of gamut
1951 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1952 }
Brian Osman207d4102019-01-10 09:40:58 -05001953
1954 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04001955 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05001956 if (ImGui::Checkbox("Pause", &isPaused)) {
1957 fAnimTimer.togglePauseResume();
1958 }
Brian Osman707d2022019-01-10 11:27:34 -05001959
1960 float speed = fAnimTimer.getSpeed();
1961 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
1962 fAnimTimer.setSpeed(speed);
1963 }
Brian Osman207d4102019-01-10 09:40:58 -05001964 }
Brian Osman0b8bb882019-04-12 11:47:19 -04001965
Brian Osmanfd7657c2019-04-25 11:34:07 -04001966 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
1967#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1968 || Window::kANGLE_BackendType == fBackendType
1969#endif
1970 ;
1971
1972 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
1973 // than GL, we need to force it on. Just do that on the first frame after the backend
1974 // switch, then resume normal operation.
1975 if (!backendIsGL && !params.fGrContextOptions.fCacheSKSL) {
1976 params.fGrContextOptions.fCacheSKSL = true;
1977 paramsChanged = true;
1978 fPersistentCache.reset();
1979 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04001980 // To re-load shaders from the currently active programs, we flush all caches on one
1981 // frame, then set a flag to poll the cache on the next frame.
1982 static bool gLoadPending = false;
1983 if (gLoadPending) {
1984 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
1985 int hitCount) {
1986 CachedGLSL& entry(fCachedGLSL.push_back());
1987 entry.fKey = key;
1988 SkMD5 hash;
1989 hash.write(key->bytes(), key->size());
1990 SkMD5::Digest digest = hash.finish();
1991 for (int i = 0; i < 16; ++i) {
1992 entry.fKeyString.appendf("%02x", digest.data[i]);
1993 }
1994
Brian Osmana085a412019-04-25 09:44:43 -04001995 entry.fShaderType = GrPersistentCacheUtils::UnpackCachedShaders(
1996 data.get(), entry.fShader, entry.fInputs, kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04001997 };
1998 fCachedGLSL.reset();
1999 fPersistentCache.foreach(collectShaders);
2000 gLoadPending = false;
2001 }
2002
2003 // Defer actually doing the load/save logic so that we can trigger a save when we
2004 // start or finish hovering on a tree node in the list below:
2005 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002006 bool doSave = ImGui::Button("Save");
2007 if (backendIsGL) {
2008 ImGui::SameLine();
2009 if (ImGui::Checkbox("SkSL", &params.fGrContextOptions.fCacheSKSL)) {
2010 paramsChanged = true;
2011 doLoad = true;
2012 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2013 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002014 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002015
2016 ImGui::BeginChild("##ScrollingRegion");
2017 for (auto& entry : fCachedGLSL) {
2018 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2019 bool hovered = ImGui::IsItemHovered();
2020 if (hovered != entry.fHovered) {
2021 // Force a save to patch the highlight shader in/out
2022 entry.fHovered = hovered;
2023 doSave = true;
2024 }
2025 if (inTreeNode) {
2026 // Full width, and a reasonable amount of space for each shader.
2027 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2028 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2029 boxSize);
2030 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2031 boxSize);
2032 ImGui::TreePop();
2033 }
2034 }
2035 ImGui::EndChild();
2036
2037 if (doLoad) {
2038 fPersistentCache.reset();
2039 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2040 gLoadPending = true;
2041 }
2042 if (doSave) {
2043 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002044 auto shaderCaps = ctx->priv().caps()->shaderCaps();
2045 bool sksl = params.fGrContextOptions.fCacheSKSL;
2046
Brian Osman072e6fc2019-06-12 11:35:41 -04002047 SkSL::String highlight;
2048 if (!sksl) {
2049 highlight = shaderCaps->versionDeclString();
2050 if (shaderCaps->usesPrecisionModifiers()) {
2051 highlight.append("precision mediump float;\n");
2052 }
Brian Osman5bee3902019-05-07 09:55:45 -04002053 }
2054 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002055 highlight.appendf("out %s sk_FragColor;\n"
2056 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2057 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002058
2059 fPersistentCache.reset();
2060 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2061 for (auto& entry : fCachedGLSL) {
2062 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2063 if (entry.fHovered) {
2064 entry.fShader[kFragment_GrShaderType] = highlight;
2065 }
2066
Brian Osmana085a412019-04-25 09:44:43 -04002067 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2068 entry.fShader,
2069 entry.fInputs,
2070 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002071 fPersistentCache.store(*entry.fKey, *data);
2072
2073 entry.fShader[kFragment_GrShaderType] = backup;
2074 }
2075 }
2076 }
Brian Osman79086b92017-02-10 13:36:16 -05002077 }
Brian Salomon99a33902017-03-07 15:16:34 -05002078 if (paramsChanged) {
2079 fDeferredActions.push_back([=]() {
2080 fWindow->setRequestedDisplayParams(params);
2081 fWindow->inval();
2082 this->updateTitle();
2083 });
2084 }
Brian Osman79086b92017-02-10 13:36:16 -05002085 ImGui::End();
2086 }
2087
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002088 if (gShaderErrorHandler.fErrors.count()) {
2089 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2090 ImGui::Begin("Shader Errors");
2091 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2092 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
2093 ImGui::TextWrapped("%s", gShaderErrorHandler.fShaders[i].c_str());
2094 }
2095 ImGui::End();
2096 gShaderErrorHandler.reset();
2097 }
2098
Brian Osmanf6877092017-02-13 09:39:57 -05002099 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002100 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2101 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002102 static int zoomFactor = 8;
2103 if (ImGui::Button("<<")) {
2104 zoomFactor = SkTMax(zoomFactor / 2, 4);
2105 }
2106 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2107 if (ImGui::Button(">>")) {
2108 zoomFactor = SkTMin(zoomFactor * 2, 32);
2109 }
Brian Osmanf6877092017-02-13 09:39:57 -05002110
Ben Wagner3627d2e2018-06-26 14:23:20 -04002111 if (!fZoomWindowFixed) {
2112 ImVec2 mousePos = ImGui::GetMousePos();
2113 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2114 }
2115 SkScalar x = fZoomWindowLocation.x();
2116 SkScalar y = fZoomWindowLocation.y();
2117 int xInt = SkScalarRoundToInt(x);
2118 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002119 ImVec2 avail = ImGui::GetContentRegionAvail();
2120
Brian Osmanead517d2017-11-13 15:36:36 -05002121 uint32_t pixel = 0;
2122 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002123 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002124 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002125 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002126 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002127 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002128 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2129 }
2130
Brian Osmand67e5182017-12-08 16:46:09 -05002131 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002132 // Translate so the region of the image that's under the mouse cursor is centered
2133 // in the zoom canvas:
2134 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002135 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2136 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002137 c->drawImage(this->fLastImage, 0, 0);
2138
2139 SkPaint outline;
2140 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002141 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002142 });
Brian Osmanf6877092017-02-13 09:39:57 -05002143 }
2144
2145 ImGui::End();
2146 }
Brian Osman79086b92017-02-10 13:36:16 -05002147}
2148
liyuqian2edb0f42016-07-06 14:11:32 -07002149void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002150 for (int i = 0; i < fDeferredActions.count(); ++i) {
2151 fDeferredActions[i]();
2152 }
2153 fDeferredActions.reset();
2154
Brian Osman56a24812017-12-19 11:15:16 -05002155 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002156 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002157 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002158 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002159
Brian Osman79086b92017-02-10 13:36:16 -05002160 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002161 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2162 // not be visible, though. So we need to redraw if there is at least one visible window, or
2163 // more than one active window. Newly created windows are active but not visible for one frame
2164 // while they determine their layout and sizing.
2165 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2166 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002167 fWindow->inval();
2168 }
jvanverth9f372462016-04-06 06:08:59 -07002169}
liyuqiane5a6cd92016-05-27 08:52:52 -07002170
Florin Malitab632df72018-06-18 21:23:06 -04002171template <typename OptionsFunc>
2172static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2173 OptionsFunc&& optionsFunc) {
2174 writer.beginObject();
2175 {
2176 writer.appendString(kName , name);
2177 writer.appendString(kValue, value);
2178
2179 writer.beginArray(kOptions);
2180 {
2181 optionsFunc(writer);
2182 }
2183 writer.endArray();
2184 }
2185 writer.endObject();
2186}
2187
2188
liyuqiane5a6cd92016-05-27 08:52:52 -07002189void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002190 if (!fWindow) {
2191 return;
2192 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002193 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002194 return; // Surface hasn't been created yet.
2195 }
2196
Florin Malitab632df72018-06-18 21:23:06 -04002197 SkDynamicMemoryWStream memStream;
2198 SkJSONWriter writer(&memStream);
2199 writer.beginArray();
2200
liyuqianb73c24b2016-06-03 08:47:23 -07002201 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002202 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2203 [this](SkJSONWriter& writer) {
2204 for(const auto& slide : fSlides) {
2205 writer.appendString(slide->getName().c_str());
2206 }
2207 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002208
liyuqianb73c24b2016-06-03 08:47:23 -07002209 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002210 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2211 [](SkJSONWriter& writer) {
2212 for (const auto& str : kBackendTypeStrings) {
2213 writer.appendString(str);
2214 }
2215 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002216
csmartdalton578f0642017-02-24 16:04:47 -07002217 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002218 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2219 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2220 [this](SkJSONWriter& writer) {
2221 writer.appendS32(0);
2222
2223 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2224 return;
2225 }
2226
2227 for (int msaa : {4, 8, 16}) {
2228 writer.appendS32(msaa);
2229 }
2230 });
csmartdalton578f0642017-02-24 16:04:47 -07002231
csmartdalton61cd31a2017-02-27 17:00:53 -07002232 // Path renderer state
2233 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002234 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2235 [this](SkJSONWriter& writer) {
2236 const GrContext* ctx = fWindow->getGrContext();
2237 if (!ctx) {
2238 writer.appendString("Software");
2239 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002240 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002241
Florin Malitab632df72018-06-18 21:23:06 -04002242 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2243 if (fWindow->sampleCount() > 1) {
2244 if (caps->shaderCaps()->pathRenderingSupport()) {
2245 writer.appendString(
2246 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2247 }
2248 } else {
2249 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2250 writer.appendString(
2251 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2252 }
2253 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2254 }
2255 writer.appendString(
2256 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2257 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2258 }
2259 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002260
liyuqianb73c24b2016-06-03 08:47:23 -07002261 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002262 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2263 [this](SkJSONWriter& writer) {
2264 writer.appendString(kSoftkeyHint);
2265 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2266 writer.appendString(softkey.c_str());
2267 }
2268 });
liyuqianb73c24b2016-06-03 08:47:23 -07002269
Florin Malitab632df72018-06-18 21:23:06 -04002270 writer.endArray();
2271 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002272
Florin Malitab632df72018-06-18 21:23:06 -04002273 auto data = memStream.detachAsData();
2274
2275 // TODO: would be cool to avoid this copy
2276 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2277
2278 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002279}
2280
2281void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002282 // For those who will add more features to handle the state change in this function:
2283 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2284 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2285 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002286 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002287 for (int i = 0; i < fSlides.count(); ++i) {
2288 if (fSlides[i]->getName().equals(stateValue)) {
2289 this->setCurrentSlide(i);
2290 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002291 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002292 }
Florin Malitaab99c342018-01-16 16:23:03 -05002293
2294 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002295 } else if (stateName.equals(kBackendStateName)) {
2296 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2297 if (stateValue.equals(kBackendTypeStrings[i])) {
2298 if (fBackendType != i) {
2299 fBackendType = (sk_app::Window::BackendType)i;
2300 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002301 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002302 }
2303 break;
2304 }
2305 }
csmartdalton578f0642017-02-24 16:04:47 -07002306 } else if (stateName.equals(kMSAAStateName)) {
2307 DisplayParams params = fWindow->getRequestedDisplayParams();
2308 int sampleCount = atoi(stateValue.c_str());
2309 if (sampleCount != params.fMSAASampleCount) {
2310 params.fMSAASampleCount = sampleCount;
2311 fWindow->setRequestedDisplayParams(params);
2312 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002313 this->updateTitle();
2314 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002315 }
2316 } else if (stateName.equals(kPathRendererStateName)) {
2317 DisplayParams params = fWindow->getRequestedDisplayParams();
2318 for (const auto& pair : gPathRendererNames) {
2319 if (pair.second == stateValue.c_str()) {
2320 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2321 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2322 fWindow->setRequestedDisplayParams(params);
2323 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002324 this->updateTitle();
2325 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002326 }
2327 break;
2328 }
csmartdalton578f0642017-02-24 16:04:47 -07002329 }
liyuqianb73c24b2016-06-03 08:47:23 -07002330 } else if (stateName.equals(kSoftkeyStateName)) {
2331 if (!stateValue.equals(kSoftkeyHint)) {
2332 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002333 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002334 }
liyuqian2edb0f42016-07-06 14:11:32 -07002335 } else if (stateName.equals(kRefreshStateName)) {
2336 // This state is actually NOT in the UI state.
2337 // We use this to allow Android to quickly set bool fRefresh.
2338 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002339 } else {
2340 SkDebugf("Unknown stateName: %s", stateName.c_str());
2341 }
2342}
Brian Osman79086b92017-02-10 13:36:16 -05002343
Hal Canaryff2e8fe2019-07-16 09:58:43 -04002344bool Viewer::onKey(sk_app::Window::Key key, InputState state, ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002345 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002346}
2347
Hal Canary3a85ed12019-07-08 16:07:57 -04002348bool Viewer::onChar(SkUnichar c, ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002349 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002350 fWindow->inval();
2351 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002352 } else {
2353 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002354 }
Brian Osman79086b92017-02-10 13:36:16 -05002355}