blob: 94660b0477f70798d0ce1633f2d11156e6d2f1fa [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkOSFile.h"
21#include "src/core/SkScan.h"
22#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040023#include "src/core/SkTextBlobPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrGpu.h"
26#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070027#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
29#include "src/utils/SkJSONWriter.h"
30#include "src/utils/SkOSPath.h"
31#include "tools/Resources.h"
32#include "tools/ToolUtils.h"
33#include "tools/flags/CommandLineFlags.h"
34#include "tools/flags/CommonFlags.h"
35#include "tools/trace/EventTracingPriv.h"
36#include "tools/viewer/BisectSlide.h"
37#include "tools/viewer/GMSlide.h"
38#include "tools/viewer/ImageSlide.h"
39#include "tools/viewer/ParticlesSlide.h"
40#include "tools/viewer/SKPSlide.h"
41#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050042#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "tools/viewer/SlideDir.h"
44#include "tools/viewer/SvgSlide.h"
45#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070046
Chris Dalton17dc4182020-03-25 16:18:16 -060047#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040048#include <map>
49
Hal Canary8a001442018-09-19 11:31:27 -040050#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040051#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040052
Florin Malita87ccf332018-05-04 12:23:24 -040053#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040055#endif
56
Brian Osman5e7fbfd2019-05-03 13:13:35 -040057class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
58public:
59 void compileError(const char* shader, const char* errors) override {
60 fShaders.push_back(SkString(shader));
61 fErrors.push_back(SkString(errors));
62 }
63
64 void reset() {
65 fShaders.reset();
66 fErrors.reset();
67 }
68
69 SkTArray<SkString> fShaders;
70 SkTArray<SkString> fErrors;
71};
72
73static CapturingShaderErrorHandler gShaderErrorHandler;
74
jvanverth34524262016-05-04 13:49:13 -070075using namespace sk_app;
76
csmartdalton61cd31a2017-02-27 17:00:53 -070077static std::map<GpuPathRenderers, std::string> gPathRendererNames;
78
jvanverth9f372462016-04-06 06:08:59 -070079Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070080 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070081}
82
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060083static DEFINE_string(slide, "", "Start on this sample.");
84static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050085
Stephen Whitea800ec92019-08-02 15:04:52 -040086#if defined(SK_VULKAN)
jvanverthb8794cc2016-07-27 14:29:18 -070087# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050088#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
89# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
Stephen Whitea800ec92019-08-02 15:04:52 -040090#elif defined(SK_DAWN)
91# define BACKENDS_STR "\"sw\", \"gl\", and \"dawn\""
bsalomon6c471f72016-07-26 12:56:32 -070092#else
93# define BACKENDS_STR "\"sw\" and \"gl\""
94#endif
95
Brian Osman2dd96932016-10-18 15:33:53 -040096static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070097
Mike Klein5b3f3432019-03-21 11:42:21 -050098static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070099
Mike Klein84836b72019-03-21 11:31:36 -0500100static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700101
Mike Klein84836b72019-03-21 11:31:36 -0500102static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400103
Mike Kleinc6142d82019-03-25 10:54:59 -0500104static DEFINE_string2(match, m, nullptr,
105 "[~][^]substring[$] [...] of name to run.\n"
106 "Multiple matches may be separated by spaces.\n"
107 "~ causes a matching name to always be skipped\n"
108 "^ requires the start of the name to match\n"
109 "$ requires the end of the name to match\n"
110 "^ and $ requires an exact match\n"
111 "If a name does not match any list entry,\n"
112 "it is skipped unless some list entry starts with ~");
113
Mike Klein19fb3972019-03-21 13:08:08 -0500114#if defined(SK_BUILD_FOR_ANDROID)
115 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500116 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
117 static DEFINE_string(lotties, "/data/local/tmp/lotties",
118 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500119#else
120 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500121 static DEFINE_string(skps, "skps", "Directory to read skps from.");
122 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500123#endif
124
Mike Kleinc6142d82019-03-25 10:54:59 -0500125static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
126
127static DEFINE_int_2(threads, j, -1,
128 "Run threadsafe tests on a threadpool with this many extra threads, "
129 "defaulting to one extra thread per core.");
130
Jim Van Verth7b558182019-11-14 16:47:01 -0500131static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
132
Chris Daltonc8877332020-01-06 09:48:30 -0700133static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Reed862818b2020-03-21 15:07:13 -0400134static DEFINE_bool(skvm, false, "Try to use skvm blitters for raster.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500135
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400136#ifndef SK_GL
137static_assert(false, "viewer requires GL backend for raster.")
138#endif
139
Brian Salomon194db172017-08-17 14:37:06 -0400140const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700141 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400142#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
143 "ANGLE",
144#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400145#ifdef SK_DAWN
146 "Dawn",
147#endif
jvanverth063ece72016-06-17 09:29:14 -0700148#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700149 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700150#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400151#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500152 "Metal",
153#endif
csmartdalton578f0642017-02-24 16:04:47 -0700154 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700155};
156
bsalomon6c471f72016-07-26 12:56:32 -0700157static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400158#ifdef SK_DAWN
159 if (0 == strcmp(str, "dawn")) {
160 return sk_app::Window::kDawn_BackendType;
161 } else
162#endif
bsalomon6c471f72016-07-26 12:56:32 -0700163#ifdef SK_VULKAN
164 if (0 == strcmp(str, "vk")) {
165 return sk_app::Window::kVulkan_BackendType;
166 } else
167#endif
Brian Salomon194db172017-08-17 14:37:06 -0400168#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
169 if (0 == strcmp(str, "angle")) {
170 return sk_app::Window::kANGLE_BackendType;
171 } else
172#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400173#ifdef SK_METAL
174 if (0 == strcmp(str, "mtl")) {
175 return sk_app::Window::kMetal_BackendType;
176 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500177#endif
bsalomon6c471f72016-07-26 12:56:32 -0700178 if (0 == strcmp(str, "gl")) {
179 return sk_app::Window::kNativeGL_BackendType;
180 } else if (0 == strcmp(str, "sw")) {
181 return sk_app::Window::kRaster_BackendType;
182 } else {
183 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
184 return sk_app::Window::kRaster_BackendType;
185 }
186}
187
Brian Osmana109e392017-02-24 09:49:14 -0500188static SkColorSpacePrimaries gSrgbPrimaries = {
189 0.64f, 0.33f,
190 0.30f, 0.60f,
191 0.15f, 0.06f,
192 0.3127f, 0.3290f };
193
194static SkColorSpacePrimaries gAdobePrimaries = {
195 0.64f, 0.33f,
196 0.21f, 0.71f,
197 0.15f, 0.06f,
198 0.3127f, 0.3290f };
199
200static SkColorSpacePrimaries gP3Primaries = {
201 0.680f, 0.320f,
202 0.265f, 0.690f,
203 0.150f, 0.060f,
204 0.3127f, 0.3290f };
205
206static SkColorSpacePrimaries gRec2020Primaries = {
207 0.708f, 0.292f,
208 0.170f, 0.797f,
209 0.131f, 0.046f,
210 0.3127f, 0.3290f };
211
212struct NamedPrimaries {
213 const char* fName;
214 SkColorSpacePrimaries* fPrimaries;
215} gNamedPrimaries[] = {
216 { "sRGB", &gSrgbPrimaries },
217 { "AdobeRGB", &gAdobePrimaries },
218 { "P3", &gP3Primaries },
219 { "Rec. 2020", &gRec2020Primaries },
220};
221
222static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
223 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
224}
225
Brian Osman70d2f432017-11-08 09:54:10 -0500226static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
227 // In raster mode, we still use GL for the window.
228 // This lets us render the GUI faster (and correct).
229 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
230}
231
Jim Van Verth74826c82019-03-01 14:37:30 -0500232class NullSlide : public Slide {
233 SkISize getDimensions() const override {
234 return SkISize::Make(640, 480);
235 }
236
237 void draw(SkCanvas* canvas) override {
238 canvas->clear(0xffff11ff);
239 }
240};
241
liyuqiane5a6cd92016-05-27 08:52:52 -0700242const char* kName = "name";
243const char* kValue = "value";
244const char* kOptions = "options";
245const char* kSlideStateName = "Slide";
246const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700247const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700248const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700249const char* kSoftkeyStateName = "Softkey";
250const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700251const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700252const char* kON = "ON";
253const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700254const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700255
Mike Reed862818b2020-03-21 15:07:13 -0400256extern bool gUseSkVMBlitter;
257
jvanverth34524262016-05-04 13:49:13 -0700258Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500259 : fCurrentSlide(-1)
260 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500261 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400262 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500263 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500264 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500265 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500266 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400267 , fZoomWindowFixed(false)
268 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500269 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400270 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700271 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500272 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500273 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500274 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500275 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700276 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400277 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400278 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400279 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500280 , fTiled(false)
281 , fDrawTileBoundaries(false)
282 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400283 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700284{
Greg Daniel285db442016-10-14 09:12:53 -0400285 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700286
Chris Dalton37ae4b02019-12-28 14:51:11 -0700287 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600288 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500289 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500290 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600291 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600292 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500293 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700294
jvanverth2bb3b6d2016-04-08 07:24:09 -0700295 SkDebugf("Command line arguments: ");
296 for (int i = 1; i < argc; ++i) {
297 SkDebugf("%s ", argv[i]);
298 }
299 SkDebugf("\n");
300
Mike Klein88544fb2019-03-20 10:50:33 -0500301 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500302#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400303 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500304#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700305
Mike Reed862818b2020-03-21 15:07:13 -0400306 gUseSkVMBlitter = FLAGS_skvm;
307
Mike Klein19cc0f62019-03-22 15:30:07 -0500308 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500309
Brian Osmanbc8150f2017-07-24 11:38:01 -0400310 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400311 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400312
bsalomon6c471f72016-07-26 12:56:32 -0700313 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700314 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700315
csmartdalton578f0642017-02-24 16:04:47 -0700316 DisplayParams displayParams;
317 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700318 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400319 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400320 displayParams.fGrContextOptions.fShaderCacheStrategy =
321 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400322 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
323 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700324 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500325 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700326
Brian Osman56a24812017-12-19 11:15:16 -0500327 // Configure timers
328 fStatsLayer.setActive(false);
329 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
330 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
331 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
332
jvanverth9f372462016-04-06 06:08:59 -0700333 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700334 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500335 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500336 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500337 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700338
brianosman622c8d52016-05-10 06:50:49 -0700339 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500340 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
341 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
342 fWindow->inval();
343 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500344 // Command to jump directly to the slide picker and give it focus
345 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
346 this->fShowImGuiDebugWindow = true;
347 this->fShowSlidePicker = true;
348 fWindow->inval();
349 });
350 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400351 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500352 this->fShowImGuiDebugWindow = true;
353 this->fShowSlidePicker = true;
354 fWindow->inval();
355 });
Brian Osman79086b92017-02-10 13:36:16 -0500356 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
357 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
358 fWindow->inval();
359 });
Brian Osmanf6877092017-02-13 09:39:57 -0500360 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
361 this->fShowZoomWindow = !this->fShowZoomWindow;
362 fWindow->inval();
363 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400364 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
365 this->fZoomWindowFixed = !this->fZoomWindowFixed;
366 fWindow->inval();
367 });
Greg Danield0794cc2019-03-27 16:23:26 -0400368 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
369 DisplayParams params = fWindow->getRequestedDisplayParams();
370 params.fDisableVsync = !params.fDisableVsync;
371 fWindow->setRequestedDisplayParams(params);
372 this->updateTitle();
373 fWindow->inval();
374 });
Mike Reedf702ed42019-07-22 17:00:49 -0400375 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
376 fRefresh = !fRefresh;
377 fWindow->inval();
378 });
brianosman622c8d52016-05-10 06:50:49 -0700379 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500380 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700381 fWindow->inval();
382 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400383 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500384 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400385 this->updateTitle();
386 fWindow->inval();
387 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500388 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500389 switch (fColorMode) {
390 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500391 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500392 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500393 case ColorMode::kColorManaged8888:
394 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500395 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500396 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400397 this->setColorMode(ColorMode::kColorManagedF16Norm);
398 break;
399 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500400 this->setColorMode(ColorMode::kLegacy);
401 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500402 }
brianosman622c8d52016-05-10 06:50:49 -0700403 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700404 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
405 DisplayParams params = fWindow->getRequestedDisplayParams();
406 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
407 fWindow->setRequestedDisplayParams(params);
408 fWindow->inval();
409 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400410 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500411 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700412 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400413 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500414 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700415 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400416 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700417 this->changeZoomLevel(1.f / 32.f);
418 fWindow->inval();
419 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400420 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700421 this->changeZoomLevel(-1.f / 32.f);
422 fWindow->inval();
423 });
jvanverthaf236b52016-05-20 06:01:06 -0700424 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400425 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
426 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500427 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400428#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
429 if (newBackend == sk_app::Window::kVulkan_BackendType) {
430 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
431 sk_app::Window::kBackendTypeCount);
432 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
433 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500434 }
435#endif
Brian Osman621491e2017-02-28 15:45:01 -0500436 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700437 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500438 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
439 fSaveToSKP = true;
440 fWindow->inval();
441 });
Mike Reed376d8122019-03-14 11:39:02 -0400442 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
443 fShowSlideDimensions = !fShowSlideDimensions;
444 fWindow->inval();
445 });
Ben Wagner37c54032018-04-13 14:30:23 -0400446 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
447 DisplayParams params = fWindow->getRequestedDisplayParams();
448 uint32_t flags = params.fSurfaceProps.flags();
449 if (!fPixelGeometryOverrides) {
450 fPixelGeometryOverrides = true;
451 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
452 } else {
453 switch (params.fSurfaceProps.pixelGeometry()) {
454 case kUnknown_SkPixelGeometry:
455 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
456 break;
457 case kRGB_H_SkPixelGeometry:
458 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
459 break;
460 case kBGR_H_SkPixelGeometry:
461 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
462 break;
463 case kRGB_V_SkPixelGeometry:
464 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
465 break;
466 case kBGR_V_SkPixelGeometry:
467 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
468 fPixelGeometryOverrides = false;
469 break;
470 }
471 }
472 fWindow->setRequestedDisplayParams(params);
473 this->updateTitle();
474 fWindow->inval();
475 });
Ben Wagner9613e452019-01-23 10:34:59 -0500476 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500477 if (!fFontOverrides.fHinting) {
478 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400479 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500480 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500481 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400482 case SkFontHinting::kNone:
483 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500484 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400485 case SkFontHinting::kSlight:
486 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500487 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400488 case SkFontHinting::kNormal:
489 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500490 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400491 case SkFontHinting::kFull:
492 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500493 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500494 break;
495 }
496 }
497 this->updateTitle();
498 fWindow->inval();
499 });
500 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500501 if (!fPaintOverrides.fAntiAlias) {
502 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
503 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500504 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500505 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500506 } else {
507 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500508 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500509 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500510 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400511 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500512 break;
513 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500514 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500515 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400516 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500517 break;
518 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500519 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400520 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500521 break;
522 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500523 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
524 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500525 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
526 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500527 break;
528 }
529 }
530 this->updateTitle();
531 fWindow->inval();
532 });
Ben Wagner37c54032018-04-13 14:30:23 -0400533 fCommands.addCommand('D', "Modes", "DFT", [this]() {
534 DisplayParams params = fWindow->getRequestedDisplayParams();
535 uint32_t flags = params.fSurfaceProps.flags();
536 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
537 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
538 fWindow->setRequestedDisplayParams(params);
539 this->updateTitle();
540 fWindow->inval();
541 });
Ben Wagner9613e452019-01-23 10:34:59 -0500542 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500543 if (!fFontOverrides.fEdging) {
544 fFontOverrides.fEdging = true;
545 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500546 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500547 switch (fFont.getEdging()) {
548 case SkFont::Edging::kAlias:
549 fFont.setEdging(SkFont::Edging::kAntiAlias);
550 break;
551 case SkFont::Edging::kAntiAlias:
552 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
553 break;
554 case SkFont::Edging::kSubpixelAntiAlias:
555 fFont.setEdging(SkFont::Edging::kAlias);
556 fFontOverrides.fEdging = false;
557 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500558 }
559 }
560 this->updateTitle();
561 fWindow->inval();
562 });
Ben Wagner9613e452019-01-23 10:34:59 -0500563 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500564 if (!fFontOverrides.fSubpixel) {
565 fFontOverrides.fSubpixel = true;
566 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500567 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500568 if (!fFont.isSubpixel()) {
569 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500570 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500571 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500572 }
573 }
574 this->updateTitle();
575 fWindow->inval();
576 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400577 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
578 if (!fFontOverrides.fBaselineSnap) {
579 fFontOverrides.fBaselineSnap = true;
580 fFont.setBaselineSnap(false);
581 } else {
582 if (!fFont.isBaselineSnap()) {
583 fFont.setBaselineSnap(true);
584 } else {
585 fFontOverrides.fBaselineSnap = false;
586 }
587 }
588 this->updateTitle();
589 fWindow->inval();
590 });
Brian Osman805a7272018-05-02 15:40:20 -0400591 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
592 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
593 : kPerspective_Real;
594 this->updateTitle();
595 fWindow->inval();
596 });
597 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
598 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
599 : kPerspective_Off;
600 this->updateTitle();
601 fWindow->inval();
602 });
Brian Osman207d4102019-01-10 09:40:58 -0500603 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
604 fAnimTimer.togglePauseResume();
605 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400606 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
607 fZoomUI = !fZoomUI;
608 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
609 fWindow->inval();
610 });
Mike Reed59295352020-03-12 13:56:34 -0400611 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
612 fDrawViaSerialize = !fDrawViaSerialize;
613 this->updateTitle();
614 fWindow->inval();
615 });
Mike Reed862818b2020-03-21 15:07:13 -0400616 fCommands.addCommand('!', "SkVM", "Toggle SkVM", [this]() {
617 gUseSkVMBlitter = !gUseSkVMBlitter;
618 this->updateTitle();
619 fWindow->inval();
620 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500621
jvanverth2bb3b6d2016-04-08 07:24:09 -0700622 // set up slides
623 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500624 if (FLAGS_list) {
625 this->listNames();
626 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700627
Brian Osman9bb47cf2018-04-26 15:55:00 -0400628 fPerspectivePoints[0].set(0, 0);
629 fPerspectivePoints[1].set(1, 0);
630 fPerspectivePoints[2].set(0, 1);
631 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700632 fAnimTimer.run();
633
Hal Canaryc465d132017-12-08 10:21:31 -0500634 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500635 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400636 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500637 }
638 fImGuiGamutPaint.setColor(SK_ColorWHITE);
639 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
640
jongdeok.kim804f17e2019-02-26 14:39:23 +0900641 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500642 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700643}
644
jvanverth34524262016-05-04 13:49:13 -0700645void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400646 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
647 static const struct {
648 const char* fExtension;
649 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500650 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400651 const SlideFactory fFactory;
652 } gExternalSlidesInfo[] = {
653 { ".skp", "skp-dir", FLAGS_skps,
654 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
655 return sk_make_sp<SKPSlide>(name, path);}
656 },
657 { ".jpg", "jpg-dir", FLAGS_jpgs,
658 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
659 return sk_make_sp<ImageSlide>(name, path);}
660 },
Florin Malita87ccf332018-05-04 12:23:24 -0400661#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400662 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400663 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
664 return sk_make_sp<SkottieSlide>(name, path);}
665 },
Florin Malita87ccf332018-05-04 12:23:24 -0400666#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400667#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400668 { ".svg", "svg-dir", FLAGS_svgs,
669 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
670 return sk_make_sp<SvgSlide>(name, path);}
671 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400672#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400673 };
jvanverthc265a922016-04-08 12:51:45 -0700674
Brian Salomon343553a2018-09-05 15:41:23 -0400675 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700676
Mike Klein88544fb2019-03-20 10:50:33 -0500677 const auto addSlide =
678 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
679 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
680 return;
681 }
liyuqian6f163d22016-06-13 12:26:45 -0700682
Mike Klein88544fb2019-03-20 10:50:33 -0500683 if (auto slide = fact(name, path)) {
684 dirSlides.push_back(slide);
685 fSlides.push_back(std::move(slide));
686 }
687 };
Florin Malita76a076b2018-02-15 18:40:48 -0500688
Florin Malita38792ce2018-05-08 10:36:18 -0400689 if (!FLAGS_file.isEmpty()) {
690 // single file mode
691 const SkString file(FLAGS_file[0]);
692
693 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
694 for (const auto& sinfo : gExternalSlidesInfo) {
695 if (file.endsWith(sinfo.fExtension)) {
696 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
697 return;
698 }
699 }
700
701 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
702 } else {
703 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
704 }
705
706 return;
707 }
708
709 // Bisect slide.
710 if (!FLAGS_bisect.isEmpty()) {
711 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500712 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400713 if (FLAGS_bisect.count() >= 2) {
714 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
715 bisect->onChar(*ch);
716 }
717 }
718 fSlides.push_back(std::move(bisect));
719 }
720 }
721
722 // GMs
723 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400724 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400725 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500726 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400727 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400728 fSlides.push_back(std::move(slide));
729 }
Florin Malita38792ce2018-05-08 10:36:18 -0400730 }
731 // reverse gms
732 int numGMs = fSlides.count() - firstGM;
733 for (int i = 0; i < numGMs/2; ++i) {
734 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
735 }
736
737 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400738 for (const SampleFactory factory : SampleRegistry::Range()) {
739 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500740 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400741 fSlides.push_back(slide);
742 }
Florin Malita38792ce2018-05-08 10:36:18 -0400743 }
744
Brian Osman7c979f52019-02-12 13:27:51 -0500745 // Particle demo
746 {
747 // TODO: Convert this to a sample
748 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500749 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500750 fSlides.push_back(std::move(slide));
751 }
752 }
753
Brian Osmand927bd22019-12-18 11:23:12 -0500754 // Runtime shader editor
755 {
756 sk_sp<Slide> slide(new SkSLSlide());
757 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
758 fSlides.push_back(std::move(slide));
759 }
760 }
761
Florin Malita0ffa3222018-04-05 14:34:45 -0400762 for (const auto& info : gExternalSlidesInfo) {
763 for (const auto& flag : info.fFlags) {
764 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
765 // single file
766 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
767 } else {
768 // directory
769 SkOSFile::Iter it(flag.c_str(), info.fExtension);
770 SkString name;
771 while (it.next(&name)) {
772 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
773 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400774 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400775 if (!dirSlides.empty()) {
776 fSlides.push_back(
777 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
778 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500779 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400780 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400781 }
782 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500783
784 if (!fSlides.count()) {
785 sk_sp<Slide> slide(new NullSlide());
786 fSlides.push_back(std::move(slide));
787 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700788}
789
790
jvanverth34524262016-05-04 13:49:13 -0700791Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700792 fWindow->detach();
793 delete fWindow;
794}
795
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500796struct SkPaintTitleUpdater {
797 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
798 void append(const char* s) {
799 if (fCount == 0) {
800 fTitle->append(" {");
801 } else {
802 fTitle->append(", ");
803 }
804 fTitle->append(s);
805 ++fCount;
806 }
807 void done() {
808 if (fCount > 0) {
809 fTitle->append("}");
810 }
811 }
812 SkString* fTitle;
813 int fCount;
814};
815
brianosman05de2162016-05-06 13:28:57 -0700816void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700817 if (!fWindow) {
818 return;
819 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500820 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700821 return; // Surface hasn't been created yet.
822 }
823
jvanverth34524262016-05-04 13:49:13 -0700824 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700825 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700826
Mike Kleine5acd752019-03-22 09:57:16 -0500827 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400828 if (gSkForceAnalyticAA) {
829 title.append(" <FAAA>");
830 } else {
831 title.append(" <AAA>");
832 }
833 }
Mike Reed59295352020-03-12 13:56:34 -0400834 if (fDrawViaSerialize) {
835 title.append(" <serialize>");
836 }
Mike Reed862818b2020-03-21 15:07:13 -0400837 if (gUseSkVMBlitter) {
838 title.append(" <skvm>");
839 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400840
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500841 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500842 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
843 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400844 const char* on, const char* off)
845 {
Ben Wagner9613e452019-01-23 10:34:59 -0500846 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400847 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500848 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400849 };
850
Ben Wagner9613e452019-01-23 10:34:59 -0500851 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
852 const char* on, const char* off)
853 {
854 if (fFontOverrides.*flag) {
855 paintTitle.append((fFont.*isFlag)() ? on : off);
856 }
857 };
858
859 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
860 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500861 if (fPaintOverrides.fFilterQuality) {
862 switch (fPaint.getFilterQuality()) {
863 case kNone_SkFilterQuality:
864 paintTitle.append("NoFilter");
865 break;
866 case kLow_SkFilterQuality:
867 paintTitle.append("LowFilter");
868 break;
869 case kMedium_SkFilterQuality:
870 paintTitle.append("MediumFilter");
871 break;
872 case kHigh_SkFilterQuality:
873 paintTitle.append("HighFilter");
874 break;
875 }
876 }
Ben Wagner9613e452019-01-23 10:34:59 -0500877
878 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
879 "Force Autohint", "No Force Autohint");
880 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400881 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500882 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
883 "Linear Metrics", "Non-Linear Metrics");
884 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
885 "Bitmap Text", "No Bitmap Text");
886 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
887
888 if (fFontOverrides.fEdging) {
889 switch (fFont.getEdging()) {
890 case SkFont::Edging::kAlias:
891 paintTitle.append("Alias Text");
892 break;
893 case SkFont::Edging::kAntiAlias:
894 paintTitle.append("Antialias Text");
895 break;
896 case SkFont::Edging::kSubpixelAntiAlias:
897 paintTitle.append("Subpixel Antialias Text");
898 break;
899 }
900 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400901
Mike Reed3ae47332019-01-04 10:11:46 -0500902 if (fFontOverrides.fHinting) {
903 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400904 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500905 paintTitle.append("No Hinting");
906 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400907 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500908 paintTitle.append("Slight Hinting");
909 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400910 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500911 paintTitle.append("Normal Hinting");
912 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400913 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500914 paintTitle.append("Full Hinting");
915 break;
916 }
917 }
918 paintTitle.done();
919
Brian Osman92004802017-03-06 11:47:26 -0500920 switch (fColorMode) {
921 case ColorMode::kLegacy:
922 title.append(" Legacy 8888");
923 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500924 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500925 title.append(" ColorManaged 8888");
926 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500927 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500928 title.append(" ColorManaged F16");
929 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400930 case ColorMode::kColorManagedF16Norm:
931 title.append(" ColorManaged F16 Norm");
932 break;
Brian Osman92004802017-03-06 11:47:26 -0500933 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500934
Brian Osman92004802017-03-06 11:47:26 -0500935 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500936 int curPrimaries = -1;
937 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
938 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
939 curPrimaries = i;
940 break;
941 }
942 }
Brian Osman03115dc2018-11-26 13:55:19 -0500943 title.appendf(" %s Gamma %f",
944 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500945 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700946 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500947
Ben Wagner37c54032018-04-13 14:30:23 -0400948 const DisplayParams& params = fWindow->getRequestedDisplayParams();
949 if (fPixelGeometryOverrides) {
950 switch (params.fSurfaceProps.pixelGeometry()) {
951 case kUnknown_SkPixelGeometry:
952 title.append( " Flat");
953 break;
954 case kRGB_H_SkPixelGeometry:
955 title.append( " RGB");
956 break;
957 case kBGR_H_SkPixelGeometry:
958 title.append( " BGR");
959 break;
960 case kRGB_V_SkPixelGeometry:
961 title.append( " RGBV");
962 break;
963 case kBGR_V_SkPixelGeometry:
964 title.append( " BGRV");
965 break;
966 }
967 }
968
969 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
970 title.append(" DFT");
971 }
972
csmartdalton578f0642017-02-24 16:04:47 -0700973 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700974 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500975 int msaa = fWindow->sampleCount();
976 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700977 title.appendf(" MSAA: %i", msaa);
978 }
979 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700980
981 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -0700982 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700983 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
984 }
985
Brian Osman805a7272018-05-02 15:40:20 -0400986 if (kPerspective_Real == fPerspectiveMode) {
987 title.append(" Perpsective (Real)");
988 } else if (kPerspective_Fake == fPerspectiveMode) {
989 title.append(" Perspective (Fake)");
990 }
991
brianosman05de2162016-05-06 13:28:57 -0700992 fWindow->setTitle(title.c_str());
993}
994
Florin Malitaab99c342018-01-16 16:23:03 -0500995int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500996
997 if (!FLAGS_slide.isEmpty()) {
998 int count = fSlides.count();
999 for (int i = 0; i < count; i++) {
1000 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001001 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001002 }
1003 }
1004
1005 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1006 this->listNames();
1007 }
1008
Florin Malitaab99c342018-01-16 16:23:03 -05001009 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001010}
1011
Florin Malitaab99c342018-01-16 16:23:03 -05001012void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001013 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001014 for (const auto& slide : fSlides) {
1015 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001016 }
1017}
1018
Florin Malitaab99c342018-01-16 16:23:03 -05001019void Viewer::setCurrentSlide(int slide) {
1020 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001021
Florin Malitaab99c342018-01-16 16:23:03 -05001022 if (slide == fCurrentSlide) {
1023 return;
1024 }
1025
1026 if (fCurrentSlide >= 0) {
1027 fSlides[fCurrentSlide]->unload();
1028 }
1029
1030 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1031 SkIntToScalar(fWindow->height()));
1032 fCurrentSlide = slide;
1033 this->setupCurrentSlide();
1034}
1035
1036void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001037 if (fCurrentSlide >= 0) {
1038 // prepare dimensions for image slides
1039 fGesture.resetTouchState();
1040 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001041
Jim Van Verth0848fb02018-01-22 13:39:30 -05001042 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1043 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1044 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001045
Jim Van Verth0848fb02018-01-22 13:39:30 -05001046 // Start with a matrix that scales the slide to the available screen space
1047 if (fWindow->scaleContentToFit()) {
1048 if (windowRect.width() > 0 && windowRect.height() > 0) {
1049 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1050 }
liyuqiane46e4f02016-05-20 07:32:19 -07001051 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001052
1053 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001054 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001055
1056 this->updateTitle();
1057 this->updateUIState();
1058
1059 fStatsLayer.resetMeasurements();
1060
1061 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001062 }
jvanverthc265a922016-04-08 12:51:45 -07001063}
1064
Brian Osmanaba642c2020-02-06 12:52:25 -05001065#define MAX_ZOOM_LEVEL 8.0f
1066#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001067
jvanverth34524262016-05-04 13:49:13 -07001068void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001069 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001070 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001071 this->preTouchMatrixChanged();
1072}
Yuqian Li755778c2018-03-28 16:23:31 -04001073
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001074void Viewer::preTouchMatrixChanged() {
1075 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001076 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1077 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1078 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1079 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1080}
1081
Brian Osman805a7272018-05-02 15:40:20 -04001082SkMatrix Viewer::computePerspectiveMatrix() {
1083 SkScalar w = fWindow->width(), h = fWindow->height();
1084 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1085 SkPoint perspPts[4] = {
1086 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1087 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1088 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1089 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1090 };
1091 SkMatrix m;
1092 m.setPolyToPoly(orthoPts, perspPts, 4);
1093 return m;
1094}
1095
Yuqian Li755778c2018-03-28 16:23:31 -04001096SkMatrix Viewer::computePreTouchMatrix() {
1097 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001098
1099 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001100 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001101 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001102
1103 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1104 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001105
Brian Osman805a7272018-05-02 15:40:20 -04001106 if (kPerspective_Real == fPerspectiveMode) {
1107 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001108 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001109 }
1110
Yuqian Li755778c2018-03-28 16:23:31 -04001111 return m;
jvanverthc265a922016-04-08 12:51:45 -07001112}
1113
liyuqiand3cdbca2016-05-17 12:44:20 -07001114SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001115 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001116 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001117 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001118 return m;
jvanverthc265a922016-04-08 12:51:45 -07001119}
1120
Brian Osman621491e2017-02-28 15:45:01 -05001121void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001122 fPersistentCache.reset();
1123 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001124 fBackendType = backendType;
1125
1126 fWindow->detach();
1127
Brian Osman70d2f432017-11-08 09:54:10 -05001128#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001129 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1130 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001131 DisplayParams params = fWindow->getRequestedDisplayParams();
1132 delete fWindow;
1133 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001134
Brian Osman70d2f432017-11-08 09:54:10 -05001135 // re-register callbacks
1136 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001137 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001138 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001139 fWindow->pushLayer(&fImGuiLayer);
1140
Brian Osman70d2f432017-11-08 09:54:10 -05001141 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1142 // will still include our correct sample count. But the re-created fWindow will lose that
1143 // information. On Windows, we need to re-create the window when changing sample count,
1144 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1145 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1146 // as if we had never changed windows at all).
1147 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001148#endif
1149
Brian Osman70d2f432017-11-08 09:54:10 -05001150 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001151}
1152
Brian Osman92004802017-03-06 11:47:26 -05001153void Viewer::setColorMode(ColorMode colorMode) {
1154 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001155 this->updateTitle();
1156 fWindow->inval();
1157}
1158
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001159class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1160public:
Mike Reed3ae47332019-01-04 10:11:46 -05001161 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1162 SkFont* font, Viewer::SkFontFields* ffields)
1163 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001164 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001165 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1166 sk_sp<SkTextBlob>* cache) {
1167 bool blobWillChange = false;
1168 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001169 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1170 bool shouldDraw = this->filterFont(&filteredFont);
1171 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001172 blobWillChange = true;
1173 break;
1174 }
1175 }
1176 if (!blobWillChange) {
1177 return blob;
1178 }
1179
1180 SkTextBlobBuilder builder;
1181 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001182 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1183 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001184 if (!shouldDraw) {
1185 continue;
1186 }
1187
Mike Reed3ae47332019-01-04 10:11:46 -05001188 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001189
Ben Wagner41e40472018-09-24 13:01:54 -04001190 const SkTextBlobBuilder::RunBuffer& runBuffer
1191 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001192 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001193 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001194 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001195 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001196 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001197 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001198 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001199 it.glyphCount(), it.textSize(), SkString())
1200 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1201 uint32_t glyphCount = it.glyphCount();
1202 if (it.glyphs()) {
1203 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1204 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1205 }
1206 if (it.pos()) {
1207 size_t posSize = sizeof(decltype(*it.pos()));
1208 uint8_t positioning = it.positioning();
1209 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1210 }
1211 if (it.text()) {
1212 size_t textSize = sizeof(decltype(*it.text()));
1213 uint32_t textCount = it.textSize();
1214 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1215 }
1216 if (it.clusters()) {
1217 size_t clusterSize = sizeof(decltype(*it.clusters()));
1218 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1219 }
1220 }
1221 *cache = builder.make();
1222 return cache->get();
1223 }
1224 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1225 const SkPaint& paint) override {
1226 sk_sp<SkTextBlob> cache;
1227 this->SkPaintFilterCanvas::onDrawTextBlob(
1228 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1229 }
Mike Reed3ae47332019-01-04 10:11:46 -05001230 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001231 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001232 font->writable()->setSize(fFont->getSize());
1233 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001234 if (fFontOverrides->fScaleX) {
1235 font->writable()->setScaleX(fFont->getScaleX());
1236 }
1237 if (fFontOverrides->fSkewX) {
1238 font->writable()->setSkewX(fFont->getSkewX());
1239 }
Mike Reed3ae47332019-01-04 10:11:46 -05001240 if (fFontOverrides->fHinting) {
1241 font->writable()->setHinting(fFont->getHinting());
1242 }
Ben Wagner9613e452019-01-23 10:34:59 -05001243 if (fFontOverrides->fEdging) {
1244 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001245 }
Ben Wagner9613e452019-01-23 10:34:59 -05001246 if (fFontOverrides->fEmbolden) {
1247 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001248 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001249 if (fFontOverrides->fBaselineSnap) {
1250 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1251 }
Ben Wagner9613e452019-01-23 10:34:59 -05001252 if (fFontOverrides->fLinearMetrics) {
1253 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001254 }
Ben Wagner9613e452019-01-23 10:34:59 -05001255 if (fFontOverrides->fSubpixel) {
1256 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001257 }
Ben Wagner9613e452019-01-23 10:34:59 -05001258 if (fFontOverrides->fEmbeddedBitmaps) {
1259 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001260 }
Ben Wagner9613e452019-01-23 10:34:59 -05001261 if (fFontOverrides->fForceAutoHinting) {
1262 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001263 }
Ben Wagner9613e452019-01-23 10:34:59 -05001264
Mike Reed3ae47332019-01-04 10:11:46 -05001265 return true;
1266 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001267 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001268 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001269 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001270 }
Ben Wagner9613e452019-01-23 10:34:59 -05001271 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001272 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001273 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001274 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001275 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001276 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001277 return true;
1278 }
1279 SkPaint* fPaint;
1280 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001281 SkFont* fFont;
1282 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001283};
1284
Robert Phillips9882dae2019-03-04 11:00:10 -05001285void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001286 if (fCurrentSlide < 0) {
1287 return;
1288 }
1289
Robert Phillips9882dae2019-03-04 11:00:10 -05001290 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001291
Brian Osmanf750fbc2017-02-08 10:47:28 -05001292 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001293 SkSurface* slideSurface = surface;
1294 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001295 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001296
Brian Osmane0d4fba2017-03-15 10:24:55 -04001297 // 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 -05001298 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001299 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001300 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001301 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001302 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001303 }
1304
Brian Osman3ac99cf2017-12-01 11:23:53 -05001305 if (fSaveToSKP) {
1306 SkPictureRecorder recorder;
1307 SkCanvas* recorderCanvas = recorder.beginRecording(
1308 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001309 fSlides[fCurrentSlide]->draw(recorderCanvas);
1310 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1311 SkFILEWStream stream("sample_app.skp");
1312 picture->serialize(&stream);
1313 fSaveToSKP = false;
1314 }
1315
Brian Osmane9ed0f02018-11-26 14:50:05 -05001316 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001317 SkColorType colorType;
1318 switch (fColorMode) {
1319 case ColorMode::kLegacy:
1320 case ColorMode::kColorManaged8888:
1321 colorType = kN32_SkColorType;
1322 break;
1323 case ColorMode::kColorManagedF16:
1324 colorType = kRGBA_F16_SkColorType;
1325 break;
1326 case ColorMode::kColorManagedF16Norm:
1327 colorType = kRGBA_F16Norm_SkColorType;
1328 break;
1329 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001330
1331 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001332 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1333 slideCanvas->getProps(&props);
1334
Brian Osmane9ed0f02018-11-26 14:50:05 -05001335 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1336 return Window::kRaster_BackendType == this->fBackendType
1337 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001338 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001339 };
1340
Brian Osman03115dc2018-11-26 13:55:19 -05001341 // We need to render offscreen if we're...
1342 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1343 // ... in any raster mode, because the window surface is actually GL
1344 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001345 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001346 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001347 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001348 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001349 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001350 colorSpace != nullptr ||
1351 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001352
Brian Osmane9ed0f02018-11-26 14:50:05 -05001353 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001354 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001355 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001356 }
1357
Mike Reed59295352020-03-12 13:56:34 -04001358 SkPictureRecorder recorder;
1359 SkCanvas* recorderRestoreCanvas = nullptr;
1360 if (fDrawViaSerialize) {
1361 recorderRestoreCanvas = slideCanvas;
1362 slideCanvas = recorder.beginRecording(
1363 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1364 }
1365
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001366 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001367 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001368 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001369 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001370 if (fTiled) {
1371 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1372 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001373 for (int y = 0; y < fWindow->height(); y += tileH) {
1374 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001375 SkAutoCanvasRestore acr(slideCanvas, true);
1376 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1377 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001378 }
1379 }
1380
1381 // Draw borders between tiles
1382 if (fDrawTileBoundaries) {
1383 SkPaint border;
1384 border.setColor(0x60FF00FF);
1385 border.setStyle(SkPaint::kStroke_Style);
1386 for (int y = 0; y < fWindow->height(); y += tileH) {
1387 for (int x = 0; x < fWindow->width(); x += tileW) {
1388 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1389 }
1390 }
1391 }
1392 } else {
1393 slideCanvas->concat(this->computeMatrix());
1394 if (kPerspective_Real == fPerspectiveMode) {
1395 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1396 }
Mike Reed3ae47332019-01-04 10:11:46 -05001397 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001398 fSlides[fCurrentSlide]->draw(&filterCanvas);
1399 }
Brian Osman56a24812017-12-19 11:15:16 -05001400 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001401 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001402
Mike Reed59295352020-03-12 13:56:34 -04001403 if (recorderRestoreCanvas) {
1404 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1405 auto data = picture->serialize();
1406 slideCanvas = recorderRestoreCanvas;
1407 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1408 }
1409
Brian Osman1df161a2017-02-09 12:10:20 -05001410 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001411 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001412 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001413 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001414
1415 // If we rendered offscreen, snap an image and push the results to the window's canvas
1416 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001417 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001418
Robert Phillips9882dae2019-03-04 11:00:10 -05001419 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001420 SkPaint paint;
1421 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001422 int prePerspectiveCount = canvas->save();
1423 if (kPerspective_Fake == fPerspectiveMode) {
1424 paint.setFilterQuality(kHigh_SkFilterQuality);
1425 canvas->clear(SK_ColorWHITE);
1426 canvas->concat(this->computePerspectiveMatrix());
1427 }
Brian Osman03115dc2018-11-26 13:55:19 -05001428 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001429 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001430 }
Mike Reed376d8122019-03-14 11:39:02 -04001431
1432 if (fShowSlideDimensions) {
1433 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1434 SkPaint paint;
1435 paint.setColor(0x40FFFF00);
1436 surface->getCanvas()->drawRect(r, paint);
1437 }
liyuqian6f163d22016-06-13 12:26:45 -07001438}
1439
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001440void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001441 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001442 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001443}
Jim Van Verth6f449692017-02-14 15:16:46 -05001444
Robert Phillips9882dae2019-03-04 11:00:10 -05001445void Viewer::onPaint(SkSurface* surface) {
1446 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001447
Robert Phillips9882dae2019-03-04 11:00:10 -05001448 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001449
Brian Osmand67e5182017-12-08 16:46:09 -05001450 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001451
1452 if (GrContext* ctx = fWindow->getGrContext()) {
1453 // Clean out cache items that haven't been used in more than 10 seconds.
1454 ctx->performDeferredCleanup(std::chrono::seconds(10));
1455 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001456}
1457
Ben Wagnera1915972018-08-09 15:06:19 -04001458void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001459 if (fCurrentSlide >= 0) {
1460 fSlides[fCurrentSlide]->resize(width, height);
1461 }
Ben Wagnera1915972018-08-09 15:06:19 -04001462}
1463
Florin Malitacefc1b92018-02-19 21:43:47 -05001464SkPoint Viewer::mapEvent(float x, float y) {
1465 const auto m = this->computeMatrix();
1466 SkMatrix inv;
1467
1468 SkAssertResult(m.invert(&inv));
1469
1470 return inv.mapXY(x, y);
1471}
1472
Hal Canaryb1f411a2019-08-29 10:39:22 -04001473bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001474 if (GestureDevice::kMouse == fGestureDevice) {
1475 return false;
1476 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001477
1478 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001479 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001480 fWindow->inval();
1481 return true;
1482 }
1483
liyuqiand3cdbca2016-05-17 12:44:20 -07001484 void* castedOwner = reinterpret_cast<void*>(owner);
1485 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001486 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001487 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001488#if defined(SK_BUILD_FOR_IOS)
1489 // TODO: move IOS swipe detection higher up into the platform code
1490 SkPoint dir;
1491 if (fGesture.isFling(&dir)) {
1492 // swiping left or right
1493 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1494 if (dir.fX < 0) {
1495 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1496 fCurrentSlide + 1 : 0);
1497 } else {
1498 this->setCurrentSlide(fCurrentSlide > 0 ?
1499 fCurrentSlide - 1 : fSlides.count() - 1);
1500 }
1501 }
1502 fGesture.reset();
1503 }
1504#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001505 break;
1506 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001507 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001508 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001509 break;
1510 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001511 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001512 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001513 break;
1514 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001515 default: {
1516 // kLeft and kRight are only for swipes
1517 SkASSERT(false);
1518 break;
1519 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001520 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001521 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001522 fWindow->inval();
1523 return true;
1524}
1525
Hal Canaryb1f411a2019-08-29 10:39:22 -04001526bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001527 if (GestureDevice::kTouch == fGestureDevice) {
1528 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001529 }
Brian Osman16c81a12017-12-20 11:58:34 -05001530
Florin Malitacefc1b92018-02-19 21:43:47 -05001531 const auto slidePt = this->mapEvent(x, y);
1532 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1533 fWindow->inval();
1534 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001535 }
1536
1537 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001538 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001539 fGesture.touchEnd(nullptr);
1540 break;
1541 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001542 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001543 fGesture.touchBegin(nullptr, x, y);
1544 break;
1545 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001546 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001547 fGesture.touchMoved(nullptr, x, y);
1548 break;
1549 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001550 default: {
1551 SkASSERT(false); // shouldn't see kRight or kLeft here
1552 break;
1553 }
Brian Osman16c81a12017-12-20 11:58:34 -05001554 }
1555 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1556
Hal Canaryb1f411a2019-08-29 10:39:22 -04001557 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001558 fWindow->inval();
1559 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001560 return true;
1561}
1562
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001563bool Viewer::onFling(skui::InputState state) {
1564 if (skui::InputState::kRight == state) {
1565 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1566 return true;
1567 } else if (skui::InputState::kLeft == state) {
1568 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1569 return true;
1570 }
1571 return false;
1572}
1573
1574bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1575 switch (state) {
1576 case skui::InputState::kDown:
1577 fGesture.startZoom();
1578 return true;
1579 break;
1580 case skui::InputState::kMove:
1581 fGesture.updateZoom(scale, x, y, x, y);
1582 return true;
1583 break;
1584 case skui::InputState::kUp:
1585 fGesture.endZoom();
1586 return true;
1587 break;
1588 default:
1589 SkASSERT(false);
1590 break;
1591 }
1592
1593 return false;
1594}
1595
Brian Osmana109e392017-02-24 09:49:14 -05001596static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001597 // The gamut image covers a (0.8 x 0.9) shaped region
1598 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001599
1600 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1601 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1602 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001603 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1604 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1605 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001606
Brian Osman535c5e32019-02-09 16:32:58 -05001607 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1608 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1609 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1610 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1611 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001612}
1613
Ben Wagner3627d2e2018-06-26 14:23:20 -04001614static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001615 ImGui::DragCanvas dc(pt);
1616 dc.fillColor(IM_COL32(0, 0, 0, 128));
1617 dc.dragPoint(pt);
1618 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001619}
1620
Brian Osman9bb47cf2018-04-26 15:55:00 -04001621static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001622 ImGui::DragCanvas dc(pts);
1623 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001624
Brian Osman535c5e32019-02-09 16:32:58 -05001625 for (int i = 0; i < 4; ++i) {
1626 dc.dragPoint(pts + i);
1627 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001628
Brian Osman535c5e32019-02-09 16:32:58 -05001629 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1630 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1631 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1632 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001633
Brian Osman535c5e32019-02-09 16:32:58 -05001634 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001635}
1636
Brian Osmand67e5182017-12-08 16:46:09 -05001637void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001638 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1639 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001640 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001641 }
1642
1643 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001644 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1645 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001646 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001647 DisplayParams params = fWindow->getRequestedDisplayParams();
1648 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001649 const GrContext* ctx = fWindow->getGrContext();
1650
Brian Osmana109e392017-02-24 09:49:14 -05001651 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1652 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001653 if (ImGui::CollapsingHeader("Backend")) {
1654 int newBackend = static_cast<int>(fBackendType);
1655 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1656 ImGui::SameLine();
1657 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001658#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1659 ImGui::SameLine();
1660 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1661#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001662#if defined(SK_DAWN)
1663 ImGui::SameLine();
1664 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1665#endif
Brian Osman621491e2017-02-28 15:45:01 -05001666#if defined(SK_VULKAN)
1667 ImGui::SameLine();
1668 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1669#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001670#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001671 ImGui::SameLine();
1672 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1673#endif
Brian Osman621491e2017-02-28 15:45:01 -05001674 if (newBackend != fBackendType) {
1675 fDeferredActions.push_back([=]() {
1676 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1677 });
1678 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001679
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001680 bool* wire = &params.fGrContextOptions.fWireframeMode;
1681 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1682 paramsChanged = true;
1683 }
Brian Salomon99a33902017-03-07 15:16:34 -05001684
Brian Osman28b12522017-03-08 17:10:24 -05001685 if (ctx) {
1686 int sampleCount = fWindow->sampleCount();
1687 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001688 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001689 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1690 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1691 ImGui::RadioButton("16", &sampleCount, 16);
1692
1693 if (sampleCount != params.fMSAASampleCount) {
1694 params.fMSAASampleCount = sampleCount;
1695 paramsChanged = true;
1696 }
1697 }
1698
Ben Wagner37c54032018-04-13 14:30:23 -04001699 int pixelGeometryIdx = 0;
1700 if (fPixelGeometryOverrides) {
1701 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1702 }
1703 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1704 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1705 {
1706 uint32_t flags = params.fSurfaceProps.flags();
1707 if (pixelGeometryIdx == 0) {
1708 fPixelGeometryOverrides = false;
1709 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1710 } else {
1711 fPixelGeometryOverrides = true;
1712 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1713 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1714 }
1715 paramsChanged = true;
1716 }
1717
1718 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1719 if (ImGui::Checkbox("DFT", &useDFT)) {
1720 uint32_t flags = params.fSurfaceProps.flags();
1721 if (useDFT) {
1722 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1723 } else {
1724 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1725 }
1726 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1727 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1728 paramsChanged = true;
1729 }
1730
Brian Osman8a9de3d2017-03-01 14:59:05 -05001731 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001732 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001733 auto prButton = [&](GpuPathRenderers x) {
1734 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001735 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1736 params.fGrContextOptions.fGpuPathRenderers = x;
1737 paramsChanged = true;
1738 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001739 }
1740 };
1741
1742 if (!ctx) {
1743 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001744 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001745 const auto* caps = ctx->priv().caps();
1746 prButton(GpuPathRenderers::kDefault);
1747 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001748 if (caps->shaderCaps()->tessellationSupport()) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001749 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001750 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001751 if (caps->shaderCaps()->pathRenderingSupport()) {
1752 prButton(GpuPathRenderers::kStencilAndCover);
1753 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001754 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001755 if (1 == fWindow->sampleCount()) {
1756 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1757 prButton(GpuPathRenderers::kCoverageCounting);
1758 }
1759 prButton(GpuPathRenderers::kSmall);
1760 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001761 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001762 prButton(GpuPathRenderers::kNone);
1763 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001764 ImGui::TreePop();
1765 }
Brian Osman621491e2017-02-28 15:45:01 -05001766 }
1767
Ben Wagner964571d2019-03-08 12:35:06 -05001768 if (ImGui::CollapsingHeader("Tiling")) {
1769 ImGui::Checkbox("Enable", &fTiled);
1770 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1771 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1772 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1773 }
1774
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001775 if (ImGui::CollapsingHeader("Transform")) {
1776 float zoom = fZoomLevel;
1777 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1778 fZoomLevel = zoom;
1779 this->preTouchMatrixChanged();
1780 paramsChanged = true;
1781 }
1782 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001783 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001784 fRotation = deg;
1785 this->preTouchMatrixChanged();
1786 paramsChanged = true;
1787 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001788 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1789 if (ImGui_DragLocation(&fOffset)) {
1790 this->preTouchMatrixChanged();
1791 paramsChanged = true;
1792 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001793 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1794 this->preTouchMatrixChanged();
1795 paramsChanged = true;
1796 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001797 }
Brian Osman805a7272018-05-02 15:40:20 -04001798 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1799 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1800 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001801 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001802 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001803 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001804 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001805 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001806 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001807 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001808 }
1809
Ben Wagnera580fb32018-04-17 11:16:32 -04001810 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001811 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001812 if (fPaintOverrides.fAntiAlias) {
1813 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001814 }
1815 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001816 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001817 {
1818 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1819 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001820 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001821 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1822 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001823 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001824 fPaintOverrides.fAntiAlias = true;
1825 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001826 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001827 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001828 case SkPaintFields::AntiAliasState::Alias:
1829 break;
1830 case SkPaintFields::AntiAliasState::Normal:
1831 break;
1832 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1833 gSkUseAnalyticAA = true;
1834 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001835 break;
1836 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1837 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001838 break;
1839 }
1840 }
1841 paramsChanged = true;
1842 }
1843
Ben Wagner99a78dc2018-05-09 18:23:51 -04001844 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001845 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001846 bool (SkPaint::* isFlag)() const,
1847 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001848 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001849 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001850 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001851 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001852 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001853 if (ImGui::Combo(label, &itemIndex, items)) {
1854 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001855 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001856 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001857 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001858 (fPaint.*setFlag)(itemIndex == 2);
1859 }
1860 paramsChanged = true;
1861 }
1862 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001863
Ben Wagner99a78dc2018-05-09 18:23:51 -04001864 paintFlag("Dither",
1865 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001866 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001867 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001868
1869 int filterQualityIdx = 0;
1870 if (fPaintOverrides.fFilterQuality) {
1871 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1872 }
1873 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1874 "Default\0None\0Low\0Medium\0High\0\0"))
1875 {
1876 if (filterQualityIdx == 0) {
1877 fPaintOverrides.fFilterQuality = false;
1878 fPaint.setFilterQuality(kNone_SkFilterQuality);
1879 } else {
1880 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1881 fPaintOverrides.fFilterQuality = true;
1882 }
1883 paramsChanged = true;
1884 }
Ben Wagner9613e452019-01-23 10:34:59 -05001885 }
Hal Canary02738a82019-01-21 18:51:32 +00001886
Ben Wagner9613e452019-01-23 10:34:59 -05001887 if (ImGui::CollapsingHeader("Font")) {
1888 int hintingIdx = 0;
1889 if (fFontOverrides.fHinting) {
1890 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1891 }
1892 if (ImGui::Combo("Hinting", &hintingIdx,
1893 "Default\0None\0Slight\0Normal\0Full\0\0"))
1894 {
1895 if (hintingIdx == 0) {
1896 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001897 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001898 } else {
1899 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1900 fFontOverrides.fHinting = true;
1901 }
1902 paramsChanged = true;
1903 }
Hal Canary02738a82019-01-21 18:51:32 +00001904
Ben Wagner9613e452019-01-23 10:34:59 -05001905 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1906 bool SkFontFields::* flag,
1907 bool (SkFont::* isFlag)() const,
1908 void (SkFont::* setFlag)(bool) )
1909 {
1910 int itemIndex = 0;
1911 if (fFontOverrides.*flag) {
1912 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1913 }
1914 if (ImGui::Combo(label, &itemIndex, items)) {
1915 if (itemIndex == 0) {
1916 fFontOverrides.*flag = false;
1917 } else {
1918 fFontOverrides.*flag = true;
1919 (fFont.*setFlag)(itemIndex == 2);
1920 }
1921 paramsChanged = true;
1922 }
1923 };
Hal Canary02738a82019-01-21 18:51:32 +00001924
Ben Wagner9613e452019-01-23 10:34:59 -05001925 fontFlag("Fake Bold Glyphs",
1926 "Default\0No Fake Bold\0Fake Bold\0\0",
1927 &SkFontFields::fEmbolden,
1928 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001929
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001930 fontFlag("Baseline Snapping",
1931 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1932 &SkFontFields::fBaselineSnap,
1933 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1934
Ben Wagner9613e452019-01-23 10:34:59 -05001935 fontFlag("Linear Text",
1936 "Default\0No Linear Text\0Linear Text\0\0",
1937 &SkFontFields::fLinearMetrics,
1938 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001939
Ben Wagner9613e452019-01-23 10:34:59 -05001940 fontFlag("Subpixel Position Glyphs",
1941 "Default\0Pixel Text\0Subpixel Text\0\0",
1942 &SkFontFields::fSubpixel,
1943 &SkFont::isSubpixel, &SkFont::setSubpixel);
1944
1945 fontFlag("Embedded Bitmap Text",
1946 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1947 &SkFontFields::fEmbeddedBitmaps,
1948 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1949
1950 fontFlag("Force Auto-Hinting",
1951 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1952 &SkFontFields::fForceAutoHinting,
1953 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1954
1955 int edgingIdx = 0;
1956 if (fFontOverrides.fEdging) {
1957 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1958 }
1959 if (ImGui::Combo("Edging", &edgingIdx,
1960 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1961 {
1962 if (edgingIdx == 0) {
1963 fFontOverrides.fEdging = false;
1964 fFont.setEdging(SkFont::Edging::kAlias);
1965 } else {
1966 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1967 fFontOverrides.fEdging = true;
1968 }
1969 paramsChanged = true;
1970 }
1971
Ben Wagner15a8d572019-03-21 13:35:44 -04001972 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1973 if (fFontOverrides.fSize) {
1974 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001975 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001976 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001977 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001978 fFontOverrides.fSizeRange[0],
1979 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001980 "%.6f", 2.0f))
1981 {
Mike Reed3ae47332019-01-04 10:11:46 -05001982 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001983 paramsChanged = true;
1984 }
1985 }
1986
1987 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1988 if (fFontOverrides.fScaleX) {
1989 float scaleX = fFont.getScaleX();
1990 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1991 fFont.setScaleX(scaleX);
1992 paramsChanged = true;
1993 }
1994 }
1995
1996 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1997 if (fFontOverrides.fSkewX) {
1998 float skewX = fFont.getSkewX();
1999 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2000 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002001 paramsChanged = true;
2002 }
2003 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002004 }
2005
Mike Reed81f60ec2018-05-15 10:09:52 -04002006 {
2007 SkMetaData controls;
2008 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2009 if (ImGui::CollapsingHeader("Current Slide")) {
2010 SkMetaData::Iter iter(controls);
2011 const char* name;
2012 SkMetaData::Type type;
2013 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002014 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002015 if (type == SkMetaData::kScalar_Type) {
2016 float val[3];
2017 SkASSERT(count == 3);
2018 controls.findScalars(name, &count, val);
2019 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2020 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002021 }
Ben Wagner110c7032019-03-22 17:03:59 -04002022 } else if (type == SkMetaData::kBool_Type) {
2023 bool val;
2024 SkASSERT(count == 1);
2025 controls.findBool(name, &val);
2026 if (ImGui::Checkbox(name, &val)) {
2027 controls.setBool(name, val);
2028 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002029 }
2030 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002031 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002032 }
2033 }
2034 }
2035
Ben Wagner7a3c6742018-04-23 10:01:07 -04002036 if (fShowSlidePicker) {
2037 ImGui::SetNextTreeNodeOpen(true);
2038 }
Brian Osman79086b92017-02-10 13:36:16 -05002039 if (ImGui::CollapsingHeader("Slide")) {
2040 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002041 static ImVector<const char*> filteredSlideNames;
2042 static ImVector<int> filteredSlideIndices;
2043
Brian Osmanfce09c52017-11-14 15:32:20 -05002044 if (fShowSlidePicker) {
2045 ImGui::SetKeyboardFocusHere();
2046 fShowSlidePicker = false;
2047 }
2048
Brian Osman79086b92017-02-10 13:36:16 -05002049 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002050 filteredSlideNames.clear();
2051 filteredSlideIndices.clear();
2052 int filteredIndex = 0;
2053 for (int i = 0; i < fSlides.count(); ++i) {
2054 const char* slideName = fSlides[i]->getName().c_str();
2055 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2056 if (i == fCurrentSlide) {
2057 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002058 }
Brian Osmanf479e422017-11-08 13:11:36 -05002059 filteredSlideNames.push_back(slideName);
2060 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002061 }
Brian Osman79086b92017-02-10 13:36:16 -05002062 }
Brian Osmanf479e422017-11-08 13:11:36 -05002063
Brian Osmanf479e422017-11-08 13:11:36 -05002064 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2065 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002066 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002067 }
2068 }
Brian Osmana109e392017-02-24 09:49:14 -05002069
2070 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002071 ColorMode newMode = fColorMode;
2072 auto cmButton = [&](ColorMode mode, const char* label) {
2073 if (ImGui::RadioButton(label, mode == fColorMode)) {
2074 newMode = mode;
2075 }
2076 };
2077
2078 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002079 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2080 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002081 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002082
2083 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002084 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002085 }
2086
2087 // Pick from common gamuts:
2088 int primariesIdx = 4; // Default: Custom
2089 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2090 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2091 primariesIdx = i;
2092 break;
2093 }
2094 }
2095
Brian Osman03115dc2018-11-26 13:55:19 -05002096 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002097 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002098
Brian Osmana109e392017-02-24 09:49:14 -05002099 if (ImGui::Combo("Primaries", &primariesIdx,
2100 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2101 if (primariesIdx >= 0 && primariesIdx <= 3) {
2102 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2103 }
2104 }
2105
2106 // Allow direct editing of gamut
2107 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2108 }
Brian Osman207d4102019-01-10 09:40:58 -05002109
2110 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002111 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002112 if (ImGui::Checkbox("Pause", &isPaused)) {
2113 fAnimTimer.togglePauseResume();
2114 }
Brian Osman707d2022019-01-10 11:27:34 -05002115
2116 float speed = fAnimTimer.getSpeed();
2117 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2118 fAnimTimer.setSpeed(speed);
2119 }
Brian Osman207d4102019-01-10 09:40:58 -05002120 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002121
Brian Osmanfd7657c2019-04-25 11:34:07 -04002122 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2123#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2124 || Window::kANGLE_BackendType == fBackendType
2125#endif
2126 ;
2127
2128 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2129 // than GL, we need to force it on. Just do that on the first frame after the backend
2130 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002131 if (!backendIsGL &&
2132 params.fGrContextOptions.fShaderCacheStrategy !=
2133 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2134 params.fGrContextOptions.fShaderCacheStrategy =
2135 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002136 paramsChanged = true;
2137 fPersistentCache.reset();
2138 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002139 // To re-load shaders from the currently active programs, we flush all caches on one
2140 // frame, then set a flag to poll the cache on the next frame.
2141 static bool gLoadPending = false;
2142 if (gLoadPending) {
2143 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2144 int hitCount) {
2145 CachedGLSL& entry(fCachedGLSL.push_back());
2146 entry.fKey = key;
2147 SkMD5 hash;
2148 hash.write(key->bytes(), key->size());
2149 SkMD5::Digest digest = hash.finish();
2150 for (int i = 0; i < 16; ++i) {
2151 entry.fKeyString.appendf("%02x", digest.data[i]);
2152 }
2153
Brian Osmana66081d2019-09-03 14:59:26 -04002154 SkReader32 reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002155 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002156 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2157 entry.fInputs,
2158 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002159 };
2160 fCachedGLSL.reset();
2161 fPersistentCache.foreach(collectShaders);
2162 gLoadPending = false;
2163 }
2164
2165 // Defer actually doing the load/save logic so that we can trigger a save when we
2166 // start or finish hovering on a tree node in the list below:
2167 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002168 bool doSave = ImGui::Button("Save");
2169 if (backendIsGL) {
2170 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002171 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2172 GrContextOptions::ShaderCacheStrategy::kSkSL;
2173 if (ImGui::Checkbox("SkSL", &sksl)) {
2174 params.fGrContextOptions.fShaderCacheStrategy = sksl
2175 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2176 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002177 paramsChanged = true;
2178 doLoad = true;
2179 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2180 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002181 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002182
2183 ImGui::BeginChild("##ScrollingRegion");
2184 for (auto& entry : fCachedGLSL) {
2185 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2186 bool hovered = ImGui::IsItemHovered();
2187 if (hovered != entry.fHovered) {
2188 // Force a save to patch the highlight shader in/out
2189 entry.fHovered = hovered;
2190 doSave = true;
2191 }
2192 if (inTreeNode) {
2193 // Full width, and a reasonable amount of space for each shader.
2194 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2195 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2196 boxSize);
2197 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2198 boxSize);
2199 ImGui::TreePop();
2200 }
2201 }
2202 ImGui::EndChild();
2203
2204 if (doLoad) {
2205 fPersistentCache.reset();
2206 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2207 gLoadPending = true;
2208 }
2209 if (doSave) {
2210 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002211 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002212 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2213 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002214
Brian Osman072e6fc2019-06-12 11:35:41 -04002215 SkSL::String highlight;
2216 if (!sksl) {
2217 highlight = shaderCaps->versionDeclString();
2218 if (shaderCaps->usesPrecisionModifiers()) {
2219 highlight.append("precision mediump float;\n");
2220 }
Brian Osman5bee3902019-05-07 09:55:45 -04002221 }
2222 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002223 highlight.appendf("out %s sk_FragColor;\n"
2224 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2225 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002226
2227 fPersistentCache.reset();
2228 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2229 for (auto& entry : fCachedGLSL) {
2230 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2231 if (entry.fHovered) {
2232 entry.fShader[kFragment_GrShaderType] = highlight;
2233 }
2234
Brian Osmana085a412019-04-25 09:44:43 -04002235 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2236 entry.fShader,
2237 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002238 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002239 fPersistentCache.store(*entry.fKey, *data);
2240
2241 entry.fShader[kFragment_GrShaderType] = backup;
2242 }
2243 }
2244 }
Brian Osman79086b92017-02-10 13:36:16 -05002245 }
Brian Salomon99a33902017-03-07 15:16:34 -05002246 if (paramsChanged) {
2247 fDeferredActions.push_back([=]() {
2248 fWindow->setRequestedDisplayParams(params);
2249 fWindow->inval();
2250 this->updateTitle();
2251 });
2252 }
Brian Osman79086b92017-02-10 13:36:16 -05002253 ImGui::End();
2254 }
2255
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002256 if (gShaderErrorHandler.fErrors.count()) {
2257 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2258 ImGui::Begin("Shader Errors");
2259 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2260 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002261 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2262 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2263 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2264 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002265 }
2266 ImGui::End();
2267 gShaderErrorHandler.reset();
2268 }
2269
Brian Osmanf6877092017-02-13 09:39:57 -05002270 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002271 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2272 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002273 static int zoomFactor = 8;
2274 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002275 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002276 }
2277 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2278 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002279 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002280 }
Brian Osmanf6877092017-02-13 09:39:57 -05002281
Ben Wagner3627d2e2018-06-26 14:23:20 -04002282 if (!fZoomWindowFixed) {
2283 ImVec2 mousePos = ImGui::GetMousePos();
2284 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2285 }
2286 SkScalar x = fZoomWindowLocation.x();
2287 SkScalar y = fZoomWindowLocation.y();
2288 int xInt = SkScalarRoundToInt(x);
2289 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002290 ImVec2 avail = ImGui::GetContentRegionAvail();
2291
Brian Osmanead517d2017-11-13 15:36:36 -05002292 uint32_t pixel = 0;
2293 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002294 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002295 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002296 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002297 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002298 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002299 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2300 }
2301
Brian Osmand67e5182017-12-08 16:46:09 -05002302 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002303 // Translate so the region of the image that's under the mouse cursor is centered
2304 // in the zoom canvas:
2305 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002306 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2307 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002308 c->drawImage(this->fLastImage, 0, 0);
2309
2310 SkPaint outline;
2311 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002312 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002313 });
Brian Osmanf6877092017-02-13 09:39:57 -05002314 }
2315
2316 ImGui::End();
2317 }
Brian Osman79086b92017-02-10 13:36:16 -05002318}
2319
liyuqian2edb0f42016-07-06 14:11:32 -07002320void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002321 for (int i = 0; i < fDeferredActions.count(); ++i) {
2322 fDeferredActions[i]();
2323 }
2324 fDeferredActions.reset();
2325
Brian Osman56a24812017-12-19 11:15:16 -05002326 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002327 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002328 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002329 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002330
Brian Osman79086b92017-02-10 13:36:16 -05002331 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002332 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2333 // not be visible, though. So we need to redraw if there is at least one visible window, or
2334 // more than one active window. Newly created windows are active but not visible for one frame
2335 // while they determine their layout and sizing.
2336 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2337 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002338 fWindow->inval();
2339 }
jvanverth9f372462016-04-06 06:08:59 -07002340}
liyuqiane5a6cd92016-05-27 08:52:52 -07002341
Florin Malitab632df72018-06-18 21:23:06 -04002342template <typename OptionsFunc>
2343static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2344 OptionsFunc&& optionsFunc) {
2345 writer.beginObject();
2346 {
2347 writer.appendString(kName , name);
2348 writer.appendString(kValue, value);
2349
2350 writer.beginArray(kOptions);
2351 {
2352 optionsFunc(writer);
2353 }
2354 writer.endArray();
2355 }
2356 writer.endObject();
2357}
2358
2359
liyuqiane5a6cd92016-05-27 08:52:52 -07002360void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002361 if (!fWindow) {
2362 return;
2363 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002364 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002365 return; // Surface hasn't been created yet.
2366 }
2367
Florin Malitab632df72018-06-18 21:23:06 -04002368 SkDynamicMemoryWStream memStream;
2369 SkJSONWriter writer(&memStream);
2370 writer.beginArray();
2371
liyuqianb73c24b2016-06-03 08:47:23 -07002372 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002373 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2374 [this](SkJSONWriter& writer) {
2375 for(const auto& slide : fSlides) {
2376 writer.appendString(slide->getName().c_str());
2377 }
2378 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002379
liyuqianb73c24b2016-06-03 08:47:23 -07002380 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002381 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2382 [](SkJSONWriter& writer) {
2383 for (const auto& str : kBackendTypeStrings) {
2384 writer.appendString(str);
2385 }
2386 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002387
csmartdalton578f0642017-02-24 16:04:47 -07002388 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002389 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2390 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2391 [this](SkJSONWriter& writer) {
2392 writer.appendS32(0);
2393
2394 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2395 return;
2396 }
2397
2398 for (int msaa : {4, 8, 16}) {
2399 writer.appendS32(msaa);
2400 }
2401 });
csmartdalton578f0642017-02-24 16:04:47 -07002402
csmartdalton61cd31a2017-02-27 17:00:53 -07002403 // Path renderer state
2404 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002405 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2406 [this](SkJSONWriter& writer) {
2407 const GrContext* ctx = fWindow->getGrContext();
2408 if (!ctx) {
2409 writer.appendString("Software");
2410 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002411 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002412 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2413 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002414 if (caps->shaderCaps()->tessellationSupport()) {
2415 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002416 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002417 }
Florin Malitab632df72018-06-18 21:23:06 -04002418 if (caps->shaderCaps()->pathRenderingSupport()) {
2419 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002420 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002421 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002422 }
2423 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002424 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2425 writer.appendString(
2426 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2427 }
2428 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2429 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002430 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002431 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002432 }
2433 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002434
liyuqianb73c24b2016-06-03 08:47:23 -07002435 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002436 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2437 [this](SkJSONWriter& writer) {
2438 writer.appendString(kSoftkeyHint);
2439 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2440 writer.appendString(softkey.c_str());
2441 }
2442 });
liyuqianb73c24b2016-06-03 08:47:23 -07002443
Florin Malitab632df72018-06-18 21:23:06 -04002444 writer.endArray();
2445 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002446
Florin Malitab632df72018-06-18 21:23:06 -04002447 auto data = memStream.detachAsData();
2448
2449 // TODO: would be cool to avoid this copy
2450 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2451
2452 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002453}
2454
2455void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002456 // For those who will add more features to handle the state change in this function:
2457 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2458 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2459 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002460 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002461 for (int i = 0; i < fSlides.count(); ++i) {
2462 if (fSlides[i]->getName().equals(stateValue)) {
2463 this->setCurrentSlide(i);
2464 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002465 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002466 }
Florin Malitaab99c342018-01-16 16:23:03 -05002467
2468 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002469 } else if (stateName.equals(kBackendStateName)) {
2470 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2471 if (stateValue.equals(kBackendTypeStrings[i])) {
2472 if (fBackendType != i) {
2473 fBackendType = (sk_app::Window::BackendType)i;
2474 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002475 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002476 }
2477 break;
2478 }
2479 }
csmartdalton578f0642017-02-24 16:04:47 -07002480 } else if (stateName.equals(kMSAAStateName)) {
2481 DisplayParams params = fWindow->getRequestedDisplayParams();
2482 int sampleCount = atoi(stateValue.c_str());
2483 if (sampleCount != params.fMSAASampleCount) {
2484 params.fMSAASampleCount = sampleCount;
2485 fWindow->setRequestedDisplayParams(params);
2486 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002487 this->updateTitle();
2488 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002489 }
2490 } else if (stateName.equals(kPathRendererStateName)) {
2491 DisplayParams params = fWindow->getRequestedDisplayParams();
2492 for (const auto& pair : gPathRendererNames) {
2493 if (pair.second == stateValue.c_str()) {
2494 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2495 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2496 fWindow->setRequestedDisplayParams(params);
2497 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002498 this->updateTitle();
2499 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002500 }
2501 break;
2502 }
csmartdalton578f0642017-02-24 16:04:47 -07002503 }
liyuqianb73c24b2016-06-03 08:47:23 -07002504 } else if (stateName.equals(kSoftkeyStateName)) {
2505 if (!stateValue.equals(kSoftkeyHint)) {
2506 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002507 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002508 }
liyuqian2edb0f42016-07-06 14:11:32 -07002509 } else if (stateName.equals(kRefreshStateName)) {
2510 // This state is actually NOT in the UI state.
2511 // We use this to allow Android to quickly set bool fRefresh.
2512 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002513 } else {
2514 SkDebugf("Unknown stateName: %s", stateName.c_str());
2515 }
2516}
Brian Osman79086b92017-02-10 13:36:16 -05002517
Hal Canaryb1f411a2019-08-29 10:39:22 -04002518bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002519 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002520}
2521
Hal Canaryb1f411a2019-08-29 10:39:22 -04002522bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002523 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002524 fWindow->inval();
2525 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002526 } else {
2527 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002528 }
Brian Osman79086b92017-02-10 13:36:16 -05002529}