blob: 07b3caeb504a4d3adc818b66233452dfc0b3052c [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
Florin Malita0ffa3222018-04-05 14:34:45 -0400769 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400770 SkTArray<SkString> sortedFilenames;
771 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400772 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400773 sortedFilenames.push_back(name);
774 }
775 if (sortedFilenames.count()) {
776 SkTQSort(sortedFilenames.begin(), sortedFilenames.end() - 1,
777 [](const SkString& a, const SkString& b) {
778 return strcmp(a.c_str(), b.c_str()) < 0;
779 });
780 }
781 for (const SkString& filename : sortedFilenames) {
782 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
783 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400784 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400785 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400786 if (!dirSlides.empty()) {
787 fSlides.push_back(
788 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
789 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500790 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400791 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400792 }
793 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500794
795 if (!fSlides.count()) {
796 sk_sp<Slide> slide(new NullSlide());
797 fSlides.push_back(std::move(slide));
798 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700799}
800
801
jvanverth34524262016-05-04 13:49:13 -0700802Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700803 fWindow->detach();
804 delete fWindow;
805}
806
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500807struct SkPaintTitleUpdater {
808 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
809 void append(const char* s) {
810 if (fCount == 0) {
811 fTitle->append(" {");
812 } else {
813 fTitle->append(", ");
814 }
815 fTitle->append(s);
816 ++fCount;
817 }
818 void done() {
819 if (fCount > 0) {
820 fTitle->append("}");
821 }
822 }
823 SkString* fTitle;
824 int fCount;
825};
826
brianosman05de2162016-05-06 13:28:57 -0700827void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700828 if (!fWindow) {
829 return;
830 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500831 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700832 return; // Surface hasn't been created yet.
833 }
834
jvanverth34524262016-05-04 13:49:13 -0700835 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700836 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700837
Mike Kleine5acd752019-03-22 09:57:16 -0500838 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400839 if (gSkForceAnalyticAA) {
840 title.append(" <FAAA>");
841 } else {
842 title.append(" <AAA>");
843 }
844 }
Mike Reed59295352020-03-12 13:56:34 -0400845 if (fDrawViaSerialize) {
846 title.append(" <serialize>");
847 }
Mike Reed862818b2020-03-21 15:07:13 -0400848 if (gUseSkVMBlitter) {
849 title.append(" <skvm>");
850 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400851
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500852 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500853 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
854 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400855 const char* on, const char* off)
856 {
Ben Wagner9613e452019-01-23 10:34:59 -0500857 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400858 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500859 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400860 };
861
Ben Wagner9613e452019-01-23 10:34:59 -0500862 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
863 const char* on, const char* off)
864 {
865 if (fFontOverrides.*flag) {
866 paintTitle.append((fFont.*isFlag)() ? on : off);
867 }
868 };
869
870 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
871 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500872 if (fPaintOverrides.fFilterQuality) {
873 switch (fPaint.getFilterQuality()) {
874 case kNone_SkFilterQuality:
875 paintTitle.append("NoFilter");
876 break;
877 case kLow_SkFilterQuality:
878 paintTitle.append("LowFilter");
879 break;
880 case kMedium_SkFilterQuality:
881 paintTitle.append("MediumFilter");
882 break;
883 case kHigh_SkFilterQuality:
884 paintTitle.append("HighFilter");
885 break;
886 }
887 }
Ben Wagner9613e452019-01-23 10:34:59 -0500888
889 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
890 "Force Autohint", "No Force Autohint");
891 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400892 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500893 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
894 "Linear Metrics", "Non-Linear Metrics");
895 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
896 "Bitmap Text", "No Bitmap Text");
897 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
898
899 if (fFontOverrides.fEdging) {
900 switch (fFont.getEdging()) {
901 case SkFont::Edging::kAlias:
902 paintTitle.append("Alias Text");
903 break;
904 case SkFont::Edging::kAntiAlias:
905 paintTitle.append("Antialias Text");
906 break;
907 case SkFont::Edging::kSubpixelAntiAlias:
908 paintTitle.append("Subpixel Antialias Text");
909 break;
910 }
911 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400912
Mike Reed3ae47332019-01-04 10:11:46 -0500913 if (fFontOverrides.fHinting) {
914 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400915 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500916 paintTitle.append("No Hinting");
917 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400918 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500919 paintTitle.append("Slight Hinting");
920 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400921 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500922 paintTitle.append("Normal Hinting");
923 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400924 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500925 paintTitle.append("Full Hinting");
926 break;
927 }
928 }
929 paintTitle.done();
930
Brian Osman92004802017-03-06 11:47:26 -0500931 switch (fColorMode) {
932 case ColorMode::kLegacy:
933 title.append(" Legacy 8888");
934 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500935 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500936 title.append(" ColorManaged 8888");
937 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500938 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500939 title.append(" ColorManaged F16");
940 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400941 case ColorMode::kColorManagedF16Norm:
942 title.append(" ColorManaged F16 Norm");
943 break;
Brian Osman92004802017-03-06 11:47:26 -0500944 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500945
Brian Osman92004802017-03-06 11:47:26 -0500946 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500947 int curPrimaries = -1;
948 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
949 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
950 curPrimaries = i;
951 break;
952 }
953 }
Brian Osman03115dc2018-11-26 13:55:19 -0500954 title.appendf(" %s Gamma %f",
955 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500956 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700957 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500958
Ben Wagner37c54032018-04-13 14:30:23 -0400959 const DisplayParams& params = fWindow->getRequestedDisplayParams();
960 if (fPixelGeometryOverrides) {
961 switch (params.fSurfaceProps.pixelGeometry()) {
962 case kUnknown_SkPixelGeometry:
963 title.append( " Flat");
964 break;
965 case kRGB_H_SkPixelGeometry:
966 title.append( " RGB");
967 break;
968 case kBGR_H_SkPixelGeometry:
969 title.append( " BGR");
970 break;
971 case kRGB_V_SkPixelGeometry:
972 title.append( " RGBV");
973 break;
974 case kBGR_V_SkPixelGeometry:
975 title.append( " BGRV");
976 break;
977 }
978 }
979
980 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
981 title.append(" DFT");
982 }
983
csmartdalton578f0642017-02-24 16:04:47 -0700984 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700985 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500986 int msaa = fWindow->sampleCount();
987 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700988 title.appendf(" MSAA: %i", msaa);
989 }
990 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700991
992 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -0700993 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700994 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
995 }
996
Brian Osman805a7272018-05-02 15:40:20 -0400997 if (kPerspective_Real == fPerspectiveMode) {
998 title.append(" Perpsective (Real)");
999 } else if (kPerspective_Fake == fPerspectiveMode) {
1000 title.append(" Perspective (Fake)");
1001 }
1002
brianosman05de2162016-05-06 13:28:57 -07001003 fWindow->setTitle(title.c_str());
1004}
1005
Florin Malitaab99c342018-01-16 16:23:03 -05001006int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001007
1008 if (!FLAGS_slide.isEmpty()) {
1009 int count = fSlides.count();
1010 for (int i = 0; i < count; i++) {
1011 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001012 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001013 }
1014 }
1015
1016 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1017 this->listNames();
1018 }
1019
Florin Malitaab99c342018-01-16 16:23:03 -05001020 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001021}
1022
Florin Malitaab99c342018-01-16 16:23:03 -05001023void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001024 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001025 for (const auto& slide : fSlides) {
1026 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001027 }
1028}
1029
Florin Malitaab99c342018-01-16 16:23:03 -05001030void Viewer::setCurrentSlide(int slide) {
1031 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001032
Florin Malitaab99c342018-01-16 16:23:03 -05001033 if (slide == fCurrentSlide) {
1034 return;
1035 }
1036
1037 if (fCurrentSlide >= 0) {
1038 fSlides[fCurrentSlide]->unload();
1039 }
1040
1041 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1042 SkIntToScalar(fWindow->height()));
1043 fCurrentSlide = slide;
1044 this->setupCurrentSlide();
1045}
1046
1047void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001048 if (fCurrentSlide >= 0) {
1049 // prepare dimensions for image slides
1050 fGesture.resetTouchState();
1051 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001052
Jim Van Verth0848fb02018-01-22 13:39:30 -05001053 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1054 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1055 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001056
Jim Van Verth0848fb02018-01-22 13:39:30 -05001057 // Start with a matrix that scales the slide to the available screen space
1058 if (fWindow->scaleContentToFit()) {
1059 if (windowRect.width() > 0 && windowRect.height() > 0) {
1060 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1061 }
liyuqiane46e4f02016-05-20 07:32:19 -07001062 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001063
1064 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001065 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001066
1067 this->updateTitle();
1068 this->updateUIState();
1069
1070 fStatsLayer.resetMeasurements();
1071
1072 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001073 }
jvanverthc265a922016-04-08 12:51:45 -07001074}
1075
Brian Osmanaba642c2020-02-06 12:52:25 -05001076#define MAX_ZOOM_LEVEL 8.0f
1077#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001078
jvanverth34524262016-05-04 13:49:13 -07001079void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001080 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001081 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001082 this->preTouchMatrixChanged();
1083}
Yuqian Li755778c2018-03-28 16:23:31 -04001084
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001085void Viewer::preTouchMatrixChanged() {
1086 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001087 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1088 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1089 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1090 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1091}
1092
Brian Osman805a7272018-05-02 15:40:20 -04001093SkMatrix Viewer::computePerspectiveMatrix() {
1094 SkScalar w = fWindow->width(), h = fWindow->height();
1095 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1096 SkPoint perspPts[4] = {
1097 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1098 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1099 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1100 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1101 };
1102 SkMatrix m;
1103 m.setPolyToPoly(orthoPts, perspPts, 4);
1104 return m;
1105}
1106
Yuqian Li755778c2018-03-28 16:23:31 -04001107SkMatrix Viewer::computePreTouchMatrix() {
1108 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001109
1110 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001111 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001112 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001113
1114 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1115 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001116
Brian Osman805a7272018-05-02 15:40:20 -04001117 if (kPerspective_Real == fPerspectiveMode) {
1118 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001119 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001120 }
1121
Yuqian Li755778c2018-03-28 16:23:31 -04001122 return m;
jvanverthc265a922016-04-08 12:51:45 -07001123}
1124
liyuqiand3cdbca2016-05-17 12:44:20 -07001125SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001126 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001127 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001128 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001129 return m;
jvanverthc265a922016-04-08 12:51:45 -07001130}
1131
Brian Osman621491e2017-02-28 15:45:01 -05001132void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001133 fPersistentCache.reset();
1134 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001135 fBackendType = backendType;
1136
1137 fWindow->detach();
1138
Brian Osman70d2f432017-11-08 09:54:10 -05001139#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001140 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1141 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001142 DisplayParams params = fWindow->getRequestedDisplayParams();
1143 delete fWindow;
1144 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001145
Brian Osman70d2f432017-11-08 09:54:10 -05001146 // re-register callbacks
1147 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001148 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001149 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001150 fWindow->pushLayer(&fImGuiLayer);
1151
Brian Osman70d2f432017-11-08 09:54:10 -05001152 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1153 // will still include our correct sample count. But the re-created fWindow will lose that
1154 // information. On Windows, we need to re-create the window when changing sample count,
1155 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1156 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1157 // as if we had never changed windows at all).
1158 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001159#endif
1160
Brian Osman70d2f432017-11-08 09:54:10 -05001161 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001162}
1163
Brian Osman92004802017-03-06 11:47:26 -05001164void Viewer::setColorMode(ColorMode colorMode) {
1165 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001166 this->updateTitle();
1167 fWindow->inval();
1168}
1169
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001170class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1171public:
Mike Reed3ae47332019-01-04 10:11:46 -05001172 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1173 SkFont* font, Viewer::SkFontFields* ffields)
1174 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001175 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001176 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1177 sk_sp<SkTextBlob>* cache) {
1178 bool blobWillChange = false;
1179 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001180 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1181 bool shouldDraw = this->filterFont(&filteredFont);
1182 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001183 blobWillChange = true;
1184 break;
1185 }
1186 }
1187 if (!blobWillChange) {
1188 return blob;
1189 }
1190
1191 SkTextBlobBuilder builder;
1192 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001193 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1194 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001195 if (!shouldDraw) {
1196 continue;
1197 }
1198
Mike Reed3ae47332019-01-04 10:11:46 -05001199 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001200
Ben Wagner41e40472018-09-24 13:01:54 -04001201 const SkTextBlobBuilder::RunBuffer& runBuffer
1202 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001203 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001204 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001205 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001206 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001207 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001208 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001209 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001210 it.glyphCount(), it.textSize(), SkString())
1211 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1212 uint32_t glyphCount = it.glyphCount();
1213 if (it.glyphs()) {
1214 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1215 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1216 }
1217 if (it.pos()) {
1218 size_t posSize = sizeof(decltype(*it.pos()));
1219 uint8_t positioning = it.positioning();
1220 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1221 }
1222 if (it.text()) {
1223 size_t textSize = sizeof(decltype(*it.text()));
1224 uint32_t textCount = it.textSize();
1225 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1226 }
1227 if (it.clusters()) {
1228 size_t clusterSize = sizeof(decltype(*it.clusters()));
1229 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1230 }
1231 }
1232 *cache = builder.make();
1233 return cache->get();
1234 }
1235 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1236 const SkPaint& paint) override {
1237 sk_sp<SkTextBlob> cache;
1238 this->SkPaintFilterCanvas::onDrawTextBlob(
1239 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1240 }
Mike Reed3ae47332019-01-04 10:11:46 -05001241 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001242 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001243 font->writable()->setSize(fFont->getSize());
1244 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001245 if (fFontOverrides->fScaleX) {
1246 font->writable()->setScaleX(fFont->getScaleX());
1247 }
1248 if (fFontOverrides->fSkewX) {
1249 font->writable()->setSkewX(fFont->getSkewX());
1250 }
Mike Reed3ae47332019-01-04 10:11:46 -05001251 if (fFontOverrides->fHinting) {
1252 font->writable()->setHinting(fFont->getHinting());
1253 }
Ben Wagner9613e452019-01-23 10:34:59 -05001254 if (fFontOverrides->fEdging) {
1255 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001256 }
Ben Wagner9613e452019-01-23 10:34:59 -05001257 if (fFontOverrides->fEmbolden) {
1258 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001259 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001260 if (fFontOverrides->fBaselineSnap) {
1261 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1262 }
Ben Wagner9613e452019-01-23 10:34:59 -05001263 if (fFontOverrides->fLinearMetrics) {
1264 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001265 }
Ben Wagner9613e452019-01-23 10:34:59 -05001266 if (fFontOverrides->fSubpixel) {
1267 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001268 }
Ben Wagner9613e452019-01-23 10:34:59 -05001269 if (fFontOverrides->fEmbeddedBitmaps) {
1270 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001271 }
Ben Wagner9613e452019-01-23 10:34:59 -05001272 if (fFontOverrides->fForceAutoHinting) {
1273 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001274 }
Ben Wagner9613e452019-01-23 10:34:59 -05001275
Mike Reed3ae47332019-01-04 10:11:46 -05001276 return true;
1277 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001278 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001279 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001280 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001281 }
Ben Wagner9613e452019-01-23 10:34:59 -05001282 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001283 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001284 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001285 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001286 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001287 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001288 return true;
1289 }
1290 SkPaint* fPaint;
1291 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001292 SkFont* fFont;
1293 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001294};
1295
Robert Phillips9882dae2019-03-04 11:00:10 -05001296void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001297 if (fCurrentSlide < 0) {
1298 return;
1299 }
1300
Robert Phillips9882dae2019-03-04 11:00:10 -05001301 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001302
Brian Osmanf750fbc2017-02-08 10:47:28 -05001303 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001304 SkSurface* slideSurface = surface;
1305 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001306 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001307
Brian Osmane0d4fba2017-03-15 10:24:55 -04001308 // 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 -05001309 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001310 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001311 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001312 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001313 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001314 }
1315
Brian Osman3ac99cf2017-12-01 11:23:53 -05001316 if (fSaveToSKP) {
1317 SkPictureRecorder recorder;
1318 SkCanvas* recorderCanvas = recorder.beginRecording(
1319 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001320 fSlides[fCurrentSlide]->draw(recorderCanvas);
1321 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1322 SkFILEWStream stream("sample_app.skp");
1323 picture->serialize(&stream);
1324 fSaveToSKP = false;
1325 }
1326
Brian Osmane9ed0f02018-11-26 14:50:05 -05001327 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001328 SkColorType colorType;
1329 switch (fColorMode) {
1330 case ColorMode::kLegacy:
1331 case ColorMode::kColorManaged8888:
1332 colorType = kN32_SkColorType;
1333 break;
1334 case ColorMode::kColorManagedF16:
1335 colorType = kRGBA_F16_SkColorType;
1336 break;
1337 case ColorMode::kColorManagedF16Norm:
1338 colorType = kRGBA_F16Norm_SkColorType;
1339 break;
1340 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001341
1342 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001343 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1344 slideCanvas->getProps(&props);
1345
Brian Osmane9ed0f02018-11-26 14:50:05 -05001346 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1347 return Window::kRaster_BackendType == this->fBackendType
1348 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001349 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001350 };
1351
Brian Osman03115dc2018-11-26 13:55:19 -05001352 // We need to render offscreen if we're...
1353 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1354 // ... in any raster mode, because the window surface is actually GL
1355 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001356 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001357 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001358 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001359 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001360 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001361 colorSpace != nullptr ||
1362 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001363
Brian Osmane9ed0f02018-11-26 14:50:05 -05001364 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001365 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001366 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001367 }
1368
Mike Reed59295352020-03-12 13:56:34 -04001369 SkPictureRecorder recorder;
1370 SkCanvas* recorderRestoreCanvas = nullptr;
1371 if (fDrawViaSerialize) {
1372 recorderRestoreCanvas = slideCanvas;
1373 slideCanvas = recorder.beginRecording(
1374 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1375 }
1376
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001377 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001378 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001379 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001380 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001381 if (fTiled) {
1382 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1383 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001384 for (int y = 0; y < fWindow->height(); y += tileH) {
1385 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001386 SkAutoCanvasRestore acr(slideCanvas, true);
1387 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1388 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001389 }
1390 }
1391
1392 // Draw borders between tiles
1393 if (fDrawTileBoundaries) {
1394 SkPaint border;
1395 border.setColor(0x60FF00FF);
1396 border.setStyle(SkPaint::kStroke_Style);
1397 for (int y = 0; y < fWindow->height(); y += tileH) {
1398 for (int x = 0; x < fWindow->width(); x += tileW) {
1399 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1400 }
1401 }
1402 }
1403 } else {
1404 slideCanvas->concat(this->computeMatrix());
1405 if (kPerspective_Real == fPerspectiveMode) {
1406 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1407 }
Mike Reed3ae47332019-01-04 10:11:46 -05001408 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001409 fSlides[fCurrentSlide]->draw(&filterCanvas);
1410 }
Brian Osman56a24812017-12-19 11:15:16 -05001411 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001412 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001413
Mike Reed59295352020-03-12 13:56:34 -04001414 if (recorderRestoreCanvas) {
1415 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1416 auto data = picture->serialize();
1417 slideCanvas = recorderRestoreCanvas;
1418 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1419 }
1420
Brian Osman1df161a2017-02-09 12:10:20 -05001421 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001422 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001423 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001424 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001425
1426 // If we rendered offscreen, snap an image and push the results to the window's canvas
1427 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001428 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001429
Robert Phillips9882dae2019-03-04 11:00:10 -05001430 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001431 SkPaint paint;
1432 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001433 int prePerspectiveCount = canvas->save();
1434 if (kPerspective_Fake == fPerspectiveMode) {
1435 paint.setFilterQuality(kHigh_SkFilterQuality);
1436 canvas->clear(SK_ColorWHITE);
1437 canvas->concat(this->computePerspectiveMatrix());
1438 }
Brian Osman03115dc2018-11-26 13:55:19 -05001439 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001440 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001441 }
Mike Reed376d8122019-03-14 11:39:02 -04001442
1443 if (fShowSlideDimensions) {
1444 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1445 SkPaint paint;
1446 paint.setColor(0x40FFFF00);
1447 surface->getCanvas()->drawRect(r, paint);
1448 }
liyuqian6f163d22016-06-13 12:26:45 -07001449}
1450
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001451void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001452 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001453 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001454}
Jim Van Verth6f449692017-02-14 15:16:46 -05001455
Robert Phillips9882dae2019-03-04 11:00:10 -05001456void Viewer::onPaint(SkSurface* surface) {
1457 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001458
Robert Phillips9882dae2019-03-04 11:00:10 -05001459 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001460
Brian Osmand67e5182017-12-08 16:46:09 -05001461 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001462
1463 if (GrContext* ctx = fWindow->getGrContext()) {
1464 // Clean out cache items that haven't been used in more than 10 seconds.
1465 ctx->performDeferredCleanup(std::chrono::seconds(10));
1466 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001467}
1468
Ben Wagnera1915972018-08-09 15:06:19 -04001469void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001470 if (fCurrentSlide >= 0) {
1471 fSlides[fCurrentSlide]->resize(width, height);
1472 }
Ben Wagnera1915972018-08-09 15:06:19 -04001473}
1474
Florin Malitacefc1b92018-02-19 21:43:47 -05001475SkPoint Viewer::mapEvent(float x, float y) {
1476 const auto m = this->computeMatrix();
1477 SkMatrix inv;
1478
1479 SkAssertResult(m.invert(&inv));
1480
1481 return inv.mapXY(x, y);
1482}
1483
Hal Canaryb1f411a2019-08-29 10:39:22 -04001484bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001485 if (GestureDevice::kMouse == fGestureDevice) {
1486 return false;
1487 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001488
1489 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001490 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001491 fWindow->inval();
1492 return true;
1493 }
1494
liyuqiand3cdbca2016-05-17 12:44:20 -07001495 void* castedOwner = reinterpret_cast<void*>(owner);
1496 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001497 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001498 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001499#if defined(SK_BUILD_FOR_IOS)
1500 // TODO: move IOS swipe detection higher up into the platform code
1501 SkPoint dir;
1502 if (fGesture.isFling(&dir)) {
1503 // swiping left or right
1504 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1505 if (dir.fX < 0) {
1506 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1507 fCurrentSlide + 1 : 0);
1508 } else {
1509 this->setCurrentSlide(fCurrentSlide > 0 ?
1510 fCurrentSlide - 1 : fSlides.count() - 1);
1511 }
1512 }
1513 fGesture.reset();
1514 }
1515#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001516 break;
1517 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001518 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001519 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001520 break;
1521 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001522 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001523 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001524 break;
1525 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001526 default: {
1527 // kLeft and kRight are only for swipes
1528 SkASSERT(false);
1529 break;
1530 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001531 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001532 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001533 fWindow->inval();
1534 return true;
1535}
1536
Hal Canaryb1f411a2019-08-29 10:39:22 -04001537bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001538 if (GestureDevice::kTouch == fGestureDevice) {
1539 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001540 }
Brian Osman16c81a12017-12-20 11:58:34 -05001541
Florin Malitacefc1b92018-02-19 21:43:47 -05001542 const auto slidePt = this->mapEvent(x, y);
1543 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1544 fWindow->inval();
1545 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001546 }
1547
1548 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001549 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001550 fGesture.touchEnd(nullptr);
1551 break;
1552 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001553 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001554 fGesture.touchBegin(nullptr, x, y);
1555 break;
1556 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001557 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001558 fGesture.touchMoved(nullptr, x, y);
1559 break;
1560 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001561 default: {
1562 SkASSERT(false); // shouldn't see kRight or kLeft here
1563 break;
1564 }
Brian Osman16c81a12017-12-20 11:58:34 -05001565 }
1566 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1567
Hal Canaryb1f411a2019-08-29 10:39:22 -04001568 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001569 fWindow->inval();
1570 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001571 return true;
1572}
1573
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001574bool Viewer::onFling(skui::InputState state) {
1575 if (skui::InputState::kRight == state) {
1576 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1577 return true;
1578 } else if (skui::InputState::kLeft == state) {
1579 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1580 return true;
1581 }
1582 return false;
1583}
1584
1585bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1586 switch (state) {
1587 case skui::InputState::kDown:
1588 fGesture.startZoom();
1589 return true;
1590 break;
1591 case skui::InputState::kMove:
1592 fGesture.updateZoom(scale, x, y, x, y);
1593 return true;
1594 break;
1595 case skui::InputState::kUp:
1596 fGesture.endZoom();
1597 return true;
1598 break;
1599 default:
1600 SkASSERT(false);
1601 break;
1602 }
1603
1604 return false;
1605}
1606
Brian Osmana109e392017-02-24 09:49:14 -05001607static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001608 // The gamut image covers a (0.8 x 0.9) shaped region
1609 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001610
1611 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1612 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1613 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001614 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1615 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1616 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001617
Brian Osman535c5e32019-02-09 16:32:58 -05001618 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1619 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1620 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1621 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1622 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001623}
1624
Ben Wagner3627d2e2018-06-26 14:23:20 -04001625static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001626 ImGui::DragCanvas dc(pt);
1627 dc.fillColor(IM_COL32(0, 0, 0, 128));
1628 dc.dragPoint(pt);
1629 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001630}
1631
Brian Osman9bb47cf2018-04-26 15:55:00 -04001632static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001633 ImGui::DragCanvas dc(pts);
1634 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001635
Brian Osman535c5e32019-02-09 16:32:58 -05001636 for (int i = 0; i < 4; ++i) {
1637 dc.dragPoint(pts + i);
1638 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001639
Brian Osman535c5e32019-02-09 16:32:58 -05001640 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1641 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1642 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1643 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001644
Brian Osman535c5e32019-02-09 16:32:58 -05001645 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001646}
1647
Brian Osmand67e5182017-12-08 16:46:09 -05001648void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001649 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1650 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001651 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001652 }
1653
1654 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001655 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1656 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001657 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001658 DisplayParams params = fWindow->getRequestedDisplayParams();
1659 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001660 const GrContext* ctx = fWindow->getGrContext();
1661
Brian Osmana109e392017-02-24 09:49:14 -05001662 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1663 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001664 if (ImGui::CollapsingHeader("Backend")) {
1665 int newBackend = static_cast<int>(fBackendType);
1666 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1667 ImGui::SameLine();
1668 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001669#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1670 ImGui::SameLine();
1671 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1672#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001673#if defined(SK_DAWN)
1674 ImGui::SameLine();
1675 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1676#endif
Brian Osman621491e2017-02-28 15:45:01 -05001677#if defined(SK_VULKAN)
1678 ImGui::SameLine();
1679 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1680#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001681#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001682 ImGui::SameLine();
1683 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1684#endif
Brian Osman621491e2017-02-28 15:45:01 -05001685 if (newBackend != fBackendType) {
1686 fDeferredActions.push_back([=]() {
1687 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1688 });
1689 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001690
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001691 bool* wire = &params.fGrContextOptions.fWireframeMode;
1692 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1693 paramsChanged = true;
1694 }
Brian Salomon99a33902017-03-07 15:16:34 -05001695
Brian Osman28b12522017-03-08 17:10:24 -05001696 if (ctx) {
1697 int sampleCount = fWindow->sampleCount();
1698 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001699 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001700 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1701 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1702 ImGui::RadioButton("16", &sampleCount, 16);
1703
1704 if (sampleCount != params.fMSAASampleCount) {
1705 params.fMSAASampleCount = sampleCount;
1706 paramsChanged = true;
1707 }
1708 }
1709
Ben Wagner37c54032018-04-13 14:30:23 -04001710 int pixelGeometryIdx = 0;
1711 if (fPixelGeometryOverrides) {
1712 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1713 }
1714 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1715 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1716 {
1717 uint32_t flags = params.fSurfaceProps.flags();
1718 if (pixelGeometryIdx == 0) {
1719 fPixelGeometryOverrides = false;
1720 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1721 } else {
1722 fPixelGeometryOverrides = true;
1723 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1724 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1725 }
1726 paramsChanged = true;
1727 }
1728
1729 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1730 if (ImGui::Checkbox("DFT", &useDFT)) {
1731 uint32_t flags = params.fSurfaceProps.flags();
1732 if (useDFT) {
1733 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1734 } else {
1735 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1736 }
1737 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1738 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1739 paramsChanged = true;
1740 }
1741
Brian Osman8a9de3d2017-03-01 14:59:05 -05001742 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001743 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001744 auto prButton = [&](GpuPathRenderers x) {
1745 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001746 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1747 params.fGrContextOptions.fGpuPathRenderers = x;
1748 paramsChanged = true;
1749 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001750 }
1751 };
1752
1753 if (!ctx) {
1754 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001755 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001756 const auto* caps = ctx->priv().caps();
1757 prButton(GpuPathRenderers::kDefault);
1758 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001759 if (caps->shaderCaps()->tessellationSupport()) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001760 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001761 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001762 if (caps->shaderCaps()->pathRenderingSupport()) {
1763 prButton(GpuPathRenderers::kStencilAndCover);
1764 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001765 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001766 if (1 == fWindow->sampleCount()) {
1767 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1768 prButton(GpuPathRenderers::kCoverageCounting);
1769 }
1770 prButton(GpuPathRenderers::kSmall);
1771 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001772 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001773 prButton(GpuPathRenderers::kNone);
1774 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001775 ImGui::TreePop();
1776 }
Brian Osman621491e2017-02-28 15:45:01 -05001777 }
1778
Ben Wagner964571d2019-03-08 12:35:06 -05001779 if (ImGui::CollapsingHeader("Tiling")) {
1780 ImGui::Checkbox("Enable", &fTiled);
1781 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1782 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1783 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1784 }
1785
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001786 if (ImGui::CollapsingHeader("Transform")) {
1787 float zoom = fZoomLevel;
1788 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1789 fZoomLevel = zoom;
1790 this->preTouchMatrixChanged();
1791 paramsChanged = true;
1792 }
1793 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001794 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001795 fRotation = deg;
1796 this->preTouchMatrixChanged();
1797 paramsChanged = true;
1798 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001799 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1800 if (ImGui_DragLocation(&fOffset)) {
1801 this->preTouchMatrixChanged();
1802 paramsChanged = true;
1803 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001804 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1805 this->preTouchMatrixChanged();
1806 paramsChanged = true;
1807 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001808 }
Brian Osman805a7272018-05-02 15:40:20 -04001809 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1810 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1811 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001812 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001813 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001814 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001815 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001816 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001817 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001818 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001819 }
1820
Ben Wagnera580fb32018-04-17 11:16:32 -04001821 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001822 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001823 if (fPaintOverrides.fAntiAlias) {
1824 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001825 }
1826 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001827 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001828 {
1829 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1830 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001831 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001832 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1833 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001834 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001835 fPaintOverrides.fAntiAlias = true;
1836 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001837 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001838 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001839 case SkPaintFields::AntiAliasState::Alias:
1840 break;
1841 case SkPaintFields::AntiAliasState::Normal:
1842 break;
1843 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1844 gSkUseAnalyticAA = true;
1845 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001846 break;
1847 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1848 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001849 break;
1850 }
1851 }
1852 paramsChanged = true;
1853 }
1854
Ben Wagner99a78dc2018-05-09 18:23:51 -04001855 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001856 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001857 bool (SkPaint::* isFlag)() const,
1858 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001859 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001860 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001861 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001862 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001863 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001864 if (ImGui::Combo(label, &itemIndex, items)) {
1865 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001866 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001867 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001868 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001869 (fPaint.*setFlag)(itemIndex == 2);
1870 }
1871 paramsChanged = true;
1872 }
1873 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001874
Ben Wagner99a78dc2018-05-09 18:23:51 -04001875 paintFlag("Dither",
1876 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001877 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001878 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001879
1880 int filterQualityIdx = 0;
1881 if (fPaintOverrides.fFilterQuality) {
1882 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1883 }
1884 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1885 "Default\0None\0Low\0Medium\0High\0\0"))
1886 {
1887 if (filterQualityIdx == 0) {
1888 fPaintOverrides.fFilterQuality = false;
1889 fPaint.setFilterQuality(kNone_SkFilterQuality);
1890 } else {
1891 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1892 fPaintOverrides.fFilterQuality = true;
1893 }
1894 paramsChanged = true;
1895 }
Ben Wagner9613e452019-01-23 10:34:59 -05001896 }
Hal Canary02738a82019-01-21 18:51:32 +00001897
Ben Wagner9613e452019-01-23 10:34:59 -05001898 if (ImGui::CollapsingHeader("Font")) {
1899 int hintingIdx = 0;
1900 if (fFontOverrides.fHinting) {
1901 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1902 }
1903 if (ImGui::Combo("Hinting", &hintingIdx,
1904 "Default\0None\0Slight\0Normal\0Full\0\0"))
1905 {
1906 if (hintingIdx == 0) {
1907 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001908 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001909 } else {
1910 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1911 fFontOverrides.fHinting = true;
1912 }
1913 paramsChanged = true;
1914 }
Hal Canary02738a82019-01-21 18:51:32 +00001915
Ben Wagner9613e452019-01-23 10:34:59 -05001916 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1917 bool SkFontFields::* flag,
1918 bool (SkFont::* isFlag)() const,
1919 void (SkFont::* setFlag)(bool) )
1920 {
1921 int itemIndex = 0;
1922 if (fFontOverrides.*flag) {
1923 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1924 }
1925 if (ImGui::Combo(label, &itemIndex, items)) {
1926 if (itemIndex == 0) {
1927 fFontOverrides.*flag = false;
1928 } else {
1929 fFontOverrides.*flag = true;
1930 (fFont.*setFlag)(itemIndex == 2);
1931 }
1932 paramsChanged = true;
1933 }
1934 };
Hal Canary02738a82019-01-21 18:51:32 +00001935
Ben Wagner9613e452019-01-23 10:34:59 -05001936 fontFlag("Fake Bold Glyphs",
1937 "Default\0No Fake Bold\0Fake Bold\0\0",
1938 &SkFontFields::fEmbolden,
1939 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001940
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001941 fontFlag("Baseline Snapping",
1942 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1943 &SkFontFields::fBaselineSnap,
1944 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1945
Ben Wagner9613e452019-01-23 10:34:59 -05001946 fontFlag("Linear Text",
1947 "Default\0No Linear Text\0Linear Text\0\0",
1948 &SkFontFields::fLinearMetrics,
1949 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001950
Ben Wagner9613e452019-01-23 10:34:59 -05001951 fontFlag("Subpixel Position Glyphs",
1952 "Default\0Pixel Text\0Subpixel Text\0\0",
1953 &SkFontFields::fSubpixel,
1954 &SkFont::isSubpixel, &SkFont::setSubpixel);
1955
1956 fontFlag("Embedded Bitmap Text",
1957 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1958 &SkFontFields::fEmbeddedBitmaps,
1959 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1960
1961 fontFlag("Force Auto-Hinting",
1962 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1963 &SkFontFields::fForceAutoHinting,
1964 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1965
1966 int edgingIdx = 0;
1967 if (fFontOverrides.fEdging) {
1968 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1969 }
1970 if (ImGui::Combo("Edging", &edgingIdx,
1971 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1972 {
1973 if (edgingIdx == 0) {
1974 fFontOverrides.fEdging = false;
1975 fFont.setEdging(SkFont::Edging::kAlias);
1976 } else {
1977 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1978 fFontOverrides.fEdging = true;
1979 }
1980 paramsChanged = true;
1981 }
1982
Ben Wagner15a8d572019-03-21 13:35:44 -04001983 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1984 if (fFontOverrides.fSize) {
1985 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001986 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001987 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001988 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001989 fFontOverrides.fSizeRange[0],
1990 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001991 "%.6f", 2.0f))
1992 {
Mike Reed3ae47332019-01-04 10:11:46 -05001993 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001994 paramsChanged = true;
1995 }
1996 }
1997
1998 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1999 if (fFontOverrides.fScaleX) {
2000 float scaleX = fFont.getScaleX();
2001 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2002 fFont.setScaleX(scaleX);
2003 paramsChanged = true;
2004 }
2005 }
2006
2007 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2008 if (fFontOverrides.fSkewX) {
2009 float skewX = fFont.getSkewX();
2010 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2011 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002012 paramsChanged = true;
2013 }
2014 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002015 }
2016
Mike Reed81f60ec2018-05-15 10:09:52 -04002017 {
2018 SkMetaData controls;
2019 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2020 if (ImGui::CollapsingHeader("Current Slide")) {
2021 SkMetaData::Iter iter(controls);
2022 const char* name;
2023 SkMetaData::Type type;
2024 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002025 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002026 if (type == SkMetaData::kScalar_Type) {
2027 float val[3];
2028 SkASSERT(count == 3);
2029 controls.findScalars(name, &count, val);
2030 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2031 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002032 }
Ben Wagner110c7032019-03-22 17:03:59 -04002033 } else if (type == SkMetaData::kBool_Type) {
2034 bool val;
2035 SkASSERT(count == 1);
2036 controls.findBool(name, &val);
2037 if (ImGui::Checkbox(name, &val)) {
2038 controls.setBool(name, val);
2039 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002040 }
2041 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002042 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002043 }
2044 }
2045 }
2046
Ben Wagner7a3c6742018-04-23 10:01:07 -04002047 if (fShowSlidePicker) {
2048 ImGui::SetNextTreeNodeOpen(true);
2049 }
Brian Osman79086b92017-02-10 13:36:16 -05002050 if (ImGui::CollapsingHeader("Slide")) {
2051 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002052 static ImVector<const char*> filteredSlideNames;
2053 static ImVector<int> filteredSlideIndices;
2054
Brian Osmanfce09c52017-11-14 15:32:20 -05002055 if (fShowSlidePicker) {
2056 ImGui::SetKeyboardFocusHere();
2057 fShowSlidePicker = false;
2058 }
2059
Brian Osman79086b92017-02-10 13:36:16 -05002060 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002061 filteredSlideNames.clear();
2062 filteredSlideIndices.clear();
2063 int filteredIndex = 0;
2064 for (int i = 0; i < fSlides.count(); ++i) {
2065 const char* slideName = fSlides[i]->getName().c_str();
2066 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2067 if (i == fCurrentSlide) {
2068 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002069 }
Brian Osmanf479e422017-11-08 13:11:36 -05002070 filteredSlideNames.push_back(slideName);
2071 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002072 }
Brian Osman79086b92017-02-10 13:36:16 -05002073 }
Brian Osmanf479e422017-11-08 13:11:36 -05002074
Brian Osmanf479e422017-11-08 13:11:36 -05002075 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2076 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002077 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002078 }
2079 }
Brian Osmana109e392017-02-24 09:49:14 -05002080
2081 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002082 ColorMode newMode = fColorMode;
2083 auto cmButton = [&](ColorMode mode, const char* label) {
2084 if (ImGui::RadioButton(label, mode == fColorMode)) {
2085 newMode = mode;
2086 }
2087 };
2088
2089 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002090 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2091 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002092 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002093
2094 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002095 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002096 }
2097
2098 // Pick from common gamuts:
2099 int primariesIdx = 4; // Default: Custom
2100 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2101 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2102 primariesIdx = i;
2103 break;
2104 }
2105 }
2106
Brian Osman03115dc2018-11-26 13:55:19 -05002107 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002108 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002109
Brian Osmana109e392017-02-24 09:49:14 -05002110 if (ImGui::Combo("Primaries", &primariesIdx,
2111 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2112 if (primariesIdx >= 0 && primariesIdx <= 3) {
2113 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2114 }
2115 }
2116
2117 // Allow direct editing of gamut
2118 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2119 }
Brian Osman207d4102019-01-10 09:40:58 -05002120
2121 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002122 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002123 if (ImGui::Checkbox("Pause", &isPaused)) {
2124 fAnimTimer.togglePauseResume();
2125 }
Brian Osman707d2022019-01-10 11:27:34 -05002126
2127 float speed = fAnimTimer.getSpeed();
2128 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2129 fAnimTimer.setSpeed(speed);
2130 }
Brian Osman207d4102019-01-10 09:40:58 -05002131 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002132
Brian Osmanfd7657c2019-04-25 11:34:07 -04002133 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2134#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2135 || Window::kANGLE_BackendType == fBackendType
2136#endif
2137 ;
2138
2139 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2140 // than GL, we need to force it on. Just do that on the first frame after the backend
2141 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002142 if (!backendIsGL &&
2143 params.fGrContextOptions.fShaderCacheStrategy !=
2144 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2145 params.fGrContextOptions.fShaderCacheStrategy =
2146 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002147 paramsChanged = true;
2148 fPersistentCache.reset();
2149 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002150 // To re-load shaders from the currently active programs, we flush all caches on one
2151 // frame, then set a flag to poll the cache on the next frame.
2152 static bool gLoadPending = false;
2153 if (gLoadPending) {
2154 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2155 int hitCount) {
2156 CachedGLSL& entry(fCachedGLSL.push_back());
2157 entry.fKey = key;
2158 SkMD5 hash;
2159 hash.write(key->bytes(), key->size());
2160 SkMD5::Digest digest = hash.finish();
2161 for (int i = 0; i < 16; ++i) {
2162 entry.fKeyString.appendf("%02x", digest.data[i]);
2163 }
2164
Brian Osmana66081d2019-09-03 14:59:26 -04002165 SkReader32 reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002166 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002167 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2168 entry.fInputs,
2169 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002170 };
2171 fCachedGLSL.reset();
2172 fPersistentCache.foreach(collectShaders);
2173 gLoadPending = false;
2174 }
2175
2176 // Defer actually doing the load/save logic so that we can trigger a save when we
2177 // start or finish hovering on a tree node in the list below:
2178 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002179 bool doSave = ImGui::Button("Save");
2180 if (backendIsGL) {
2181 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002182 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2183 GrContextOptions::ShaderCacheStrategy::kSkSL;
2184 if (ImGui::Checkbox("SkSL", &sksl)) {
2185 params.fGrContextOptions.fShaderCacheStrategy = sksl
2186 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2187 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002188 paramsChanged = true;
2189 doLoad = true;
2190 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2191 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002192 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002193
2194 ImGui::BeginChild("##ScrollingRegion");
2195 for (auto& entry : fCachedGLSL) {
2196 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2197 bool hovered = ImGui::IsItemHovered();
2198 if (hovered != entry.fHovered) {
2199 // Force a save to patch the highlight shader in/out
2200 entry.fHovered = hovered;
2201 doSave = true;
2202 }
2203 if (inTreeNode) {
2204 // Full width, and a reasonable amount of space for each shader.
2205 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2206 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2207 boxSize);
2208 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2209 boxSize);
2210 ImGui::TreePop();
2211 }
2212 }
2213 ImGui::EndChild();
2214
2215 if (doLoad) {
2216 fPersistentCache.reset();
2217 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2218 gLoadPending = true;
2219 }
2220 if (doSave) {
2221 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002222 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002223 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2224 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002225
Brian Osman072e6fc2019-06-12 11:35:41 -04002226 SkSL::String highlight;
2227 if (!sksl) {
2228 highlight = shaderCaps->versionDeclString();
2229 if (shaderCaps->usesPrecisionModifiers()) {
2230 highlight.append("precision mediump float;\n");
2231 }
Brian Osman5bee3902019-05-07 09:55:45 -04002232 }
2233 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002234 highlight.appendf("out %s sk_FragColor;\n"
2235 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2236 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002237
2238 fPersistentCache.reset();
2239 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2240 for (auto& entry : fCachedGLSL) {
2241 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2242 if (entry.fHovered) {
2243 entry.fShader[kFragment_GrShaderType] = highlight;
2244 }
2245
Brian Osmana085a412019-04-25 09:44:43 -04002246 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2247 entry.fShader,
2248 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002249 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002250 fPersistentCache.store(*entry.fKey, *data);
2251
2252 entry.fShader[kFragment_GrShaderType] = backup;
2253 }
2254 }
2255 }
Brian Osman79086b92017-02-10 13:36:16 -05002256 }
Brian Salomon99a33902017-03-07 15:16:34 -05002257 if (paramsChanged) {
2258 fDeferredActions.push_back([=]() {
2259 fWindow->setRequestedDisplayParams(params);
2260 fWindow->inval();
2261 this->updateTitle();
2262 });
2263 }
Brian Osman79086b92017-02-10 13:36:16 -05002264 ImGui::End();
2265 }
2266
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002267 if (gShaderErrorHandler.fErrors.count()) {
2268 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2269 ImGui::Begin("Shader Errors");
2270 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2271 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002272 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2273 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2274 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2275 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002276 }
2277 ImGui::End();
2278 gShaderErrorHandler.reset();
2279 }
2280
Brian Osmanf6877092017-02-13 09:39:57 -05002281 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002282 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2283 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002284 static int zoomFactor = 8;
2285 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002286 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002287 }
2288 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2289 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002290 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002291 }
Brian Osmanf6877092017-02-13 09:39:57 -05002292
Ben Wagner3627d2e2018-06-26 14:23:20 -04002293 if (!fZoomWindowFixed) {
2294 ImVec2 mousePos = ImGui::GetMousePos();
2295 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2296 }
2297 SkScalar x = fZoomWindowLocation.x();
2298 SkScalar y = fZoomWindowLocation.y();
2299 int xInt = SkScalarRoundToInt(x);
2300 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002301 ImVec2 avail = ImGui::GetContentRegionAvail();
2302
Brian Osmanead517d2017-11-13 15:36:36 -05002303 uint32_t pixel = 0;
2304 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002305 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002306 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002307 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002308 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002309 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002310 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2311 }
2312
Brian Osmand67e5182017-12-08 16:46:09 -05002313 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002314 // Translate so the region of the image that's under the mouse cursor is centered
2315 // in the zoom canvas:
2316 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002317 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2318 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002319 c->drawImage(this->fLastImage, 0, 0);
2320
2321 SkPaint outline;
2322 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002323 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002324 });
Brian Osmanf6877092017-02-13 09:39:57 -05002325 }
2326
2327 ImGui::End();
2328 }
Brian Osman79086b92017-02-10 13:36:16 -05002329}
2330
liyuqian2edb0f42016-07-06 14:11:32 -07002331void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002332 for (int i = 0; i < fDeferredActions.count(); ++i) {
2333 fDeferredActions[i]();
2334 }
2335 fDeferredActions.reset();
2336
Brian Osman56a24812017-12-19 11:15:16 -05002337 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002338 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002339 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002340 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002341
Brian Osman79086b92017-02-10 13:36:16 -05002342 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002343 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2344 // not be visible, though. So we need to redraw if there is at least one visible window, or
2345 // more than one active window. Newly created windows are active but not visible for one frame
2346 // while they determine their layout and sizing.
2347 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2348 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002349 fWindow->inval();
2350 }
jvanverth9f372462016-04-06 06:08:59 -07002351}
liyuqiane5a6cd92016-05-27 08:52:52 -07002352
Florin Malitab632df72018-06-18 21:23:06 -04002353template <typename OptionsFunc>
2354static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2355 OptionsFunc&& optionsFunc) {
2356 writer.beginObject();
2357 {
2358 writer.appendString(kName , name);
2359 writer.appendString(kValue, value);
2360
2361 writer.beginArray(kOptions);
2362 {
2363 optionsFunc(writer);
2364 }
2365 writer.endArray();
2366 }
2367 writer.endObject();
2368}
2369
2370
liyuqiane5a6cd92016-05-27 08:52:52 -07002371void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002372 if (!fWindow) {
2373 return;
2374 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002375 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002376 return; // Surface hasn't been created yet.
2377 }
2378
Florin Malitab632df72018-06-18 21:23:06 -04002379 SkDynamicMemoryWStream memStream;
2380 SkJSONWriter writer(&memStream);
2381 writer.beginArray();
2382
liyuqianb73c24b2016-06-03 08:47:23 -07002383 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002384 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2385 [this](SkJSONWriter& writer) {
2386 for(const auto& slide : fSlides) {
2387 writer.appendString(slide->getName().c_str());
2388 }
2389 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002390
liyuqianb73c24b2016-06-03 08:47:23 -07002391 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002392 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2393 [](SkJSONWriter& writer) {
2394 for (const auto& str : kBackendTypeStrings) {
2395 writer.appendString(str);
2396 }
2397 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002398
csmartdalton578f0642017-02-24 16:04:47 -07002399 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002400 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2401 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2402 [this](SkJSONWriter& writer) {
2403 writer.appendS32(0);
2404
2405 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2406 return;
2407 }
2408
2409 for (int msaa : {4, 8, 16}) {
2410 writer.appendS32(msaa);
2411 }
2412 });
csmartdalton578f0642017-02-24 16:04:47 -07002413
csmartdalton61cd31a2017-02-27 17:00:53 -07002414 // Path renderer state
2415 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002416 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2417 [this](SkJSONWriter& writer) {
2418 const GrContext* ctx = fWindow->getGrContext();
2419 if (!ctx) {
2420 writer.appendString("Software");
2421 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002422 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002423 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2424 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002425 if (caps->shaderCaps()->tessellationSupport()) {
2426 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002427 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002428 }
Florin Malitab632df72018-06-18 21:23:06 -04002429 if (caps->shaderCaps()->pathRenderingSupport()) {
2430 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002431 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002432 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002433 }
2434 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002435 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2436 writer.appendString(
2437 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2438 }
2439 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2440 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002441 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002442 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002443 }
2444 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002445
liyuqianb73c24b2016-06-03 08:47:23 -07002446 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002447 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2448 [this](SkJSONWriter& writer) {
2449 writer.appendString(kSoftkeyHint);
2450 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2451 writer.appendString(softkey.c_str());
2452 }
2453 });
liyuqianb73c24b2016-06-03 08:47:23 -07002454
Florin Malitab632df72018-06-18 21:23:06 -04002455 writer.endArray();
2456 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002457
Florin Malitab632df72018-06-18 21:23:06 -04002458 auto data = memStream.detachAsData();
2459
2460 // TODO: would be cool to avoid this copy
2461 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2462
2463 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002464}
2465
2466void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002467 // For those who will add more features to handle the state change in this function:
2468 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2469 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2470 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002471 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002472 for (int i = 0; i < fSlides.count(); ++i) {
2473 if (fSlides[i]->getName().equals(stateValue)) {
2474 this->setCurrentSlide(i);
2475 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002476 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002477 }
Florin Malitaab99c342018-01-16 16:23:03 -05002478
2479 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002480 } else if (stateName.equals(kBackendStateName)) {
2481 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2482 if (stateValue.equals(kBackendTypeStrings[i])) {
2483 if (fBackendType != i) {
2484 fBackendType = (sk_app::Window::BackendType)i;
2485 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002486 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002487 }
2488 break;
2489 }
2490 }
csmartdalton578f0642017-02-24 16:04:47 -07002491 } else if (stateName.equals(kMSAAStateName)) {
2492 DisplayParams params = fWindow->getRequestedDisplayParams();
2493 int sampleCount = atoi(stateValue.c_str());
2494 if (sampleCount != params.fMSAASampleCount) {
2495 params.fMSAASampleCount = sampleCount;
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 } else if (stateName.equals(kPathRendererStateName)) {
2502 DisplayParams params = fWindow->getRequestedDisplayParams();
2503 for (const auto& pair : gPathRendererNames) {
2504 if (pair.second == stateValue.c_str()) {
2505 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2506 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2507 fWindow->setRequestedDisplayParams(params);
2508 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002509 this->updateTitle();
2510 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002511 }
2512 break;
2513 }
csmartdalton578f0642017-02-24 16:04:47 -07002514 }
liyuqianb73c24b2016-06-03 08:47:23 -07002515 } else if (stateName.equals(kSoftkeyStateName)) {
2516 if (!stateValue.equals(kSoftkeyHint)) {
2517 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002518 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002519 }
liyuqian2edb0f42016-07-06 14:11:32 -07002520 } else if (stateName.equals(kRefreshStateName)) {
2521 // This state is actually NOT in the UI state.
2522 // We use this to allow Android to quickly set bool fRefresh.
2523 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002524 } else {
2525 SkDebugf("Unknown stateName: %s", stateName.c_str());
2526 }
2527}
Brian Osman79086b92017-02-10 13:36:16 -05002528
Hal Canaryb1f411a2019-08-29 10:39:22 -04002529bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002530 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002531}
2532
Hal Canaryb1f411a2019-08-29 10:39:22 -04002533bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002534 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002535 fWindow->inval();
2536 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002537 } else {
2538 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002539 }
Brian Osman79086b92017-02-10 13:36:16 -05002540}