blob: 8e20bb5a3a07ce5ee7349b0dd97151d5cdff8fb6 [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 Klein1e0884d2020-04-28 15:04:16 -0500135static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleinc6142d82019-03-25 10:54:59 -0500136
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400137#ifndef SK_GL
138static_assert(false, "viewer requires GL backend for raster.")
139#endif
140
Brian Salomon194db172017-08-17 14:37:06 -0400141const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700142 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400143#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
144 "ANGLE",
145#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400146#ifdef SK_DAWN
147 "Dawn",
148#endif
jvanverth063ece72016-06-17 09:29:14 -0700149#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700150 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700151#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400152#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500153 "Metal",
154#endif
csmartdalton578f0642017-02-24 16:04:47 -0700155 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700156};
157
bsalomon6c471f72016-07-26 12:56:32 -0700158static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400159#ifdef SK_DAWN
160 if (0 == strcmp(str, "dawn")) {
161 return sk_app::Window::kDawn_BackendType;
162 } else
163#endif
bsalomon6c471f72016-07-26 12:56:32 -0700164#ifdef SK_VULKAN
165 if (0 == strcmp(str, "vk")) {
166 return sk_app::Window::kVulkan_BackendType;
167 } else
168#endif
Brian Salomon194db172017-08-17 14:37:06 -0400169#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
170 if (0 == strcmp(str, "angle")) {
171 return sk_app::Window::kANGLE_BackendType;
172 } else
173#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400174#ifdef SK_METAL
175 if (0 == strcmp(str, "mtl")) {
176 return sk_app::Window::kMetal_BackendType;
177 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500178#endif
bsalomon6c471f72016-07-26 12:56:32 -0700179 if (0 == strcmp(str, "gl")) {
180 return sk_app::Window::kNativeGL_BackendType;
181 } else if (0 == strcmp(str, "sw")) {
182 return sk_app::Window::kRaster_BackendType;
183 } else {
184 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
185 return sk_app::Window::kRaster_BackendType;
186 }
187}
188
Brian Osmana109e392017-02-24 09:49:14 -0500189static SkColorSpacePrimaries gSrgbPrimaries = {
190 0.64f, 0.33f,
191 0.30f, 0.60f,
192 0.15f, 0.06f,
193 0.3127f, 0.3290f };
194
195static SkColorSpacePrimaries gAdobePrimaries = {
196 0.64f, 0.33f,
197 0.21f, 0.71f,
198 0.15f, 0.06f,
199 0.3127f, 0.3290f };
200
201static SkColorSpacePrimaries gP3Primaries = {
202 0.680f, 0.320f,
203 0.265f, 0.690f,
204 0.150f, 0.060f,
205 0.3127f, 0.3290f };
206
207static SkColorSpacePrimaries gRec2020Primaries = {
208 0.708f, 0.292f,
209 0.170f, 0.797f,
210 0.131f, 0.046f,
211 0.3127f, 0.3290f };
212
213struct NamedPrimaries {
214 const char* fName;
215 SkColorSpacePrimaries* fPrimaries;
216} gNamedPrimaries[] = {
217 { "sRGB", &gSrgbPrimaries },
218 { "AdobeRGB", &gAdobePrimaries },
219 { "P3", &gP3Primaries },
220 { "Rec. 2020", &gRec2020Primaries },
221};
222
223static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
224 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
225}
226
Brian Osman70d2f432017-11-08 09:54:10 -0500227static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
228 // In raster mode, we still use GL for the window.
229 // This lets us render the GUI faster (and correct).
230 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
231}
232
Jim Van Verth74826c82019-03-01 14:37:30 -0500233class NullSlide : public Slide {
234 SkISize getDimensions() const override {
235 return SkISize::Make(640, 480);
236 }
237
238 void draw(SkCanvas* canvas) override {
239 canvas->clear(0xffff11ff);
240 }
241};
242
liyuqiane5a6cd92016-05-27 08:52:52 -0700243const char* kName = "name";
244const char* kValue = "value";
245const char* kOptions = "options";
246const char* kSlideStateName = "Slide";
247const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700248const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700249const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700250const char* kSoftkeyStateName = "Softkey";
251const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700252const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700253const char* kON = "ON";
254const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700255const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700256
Mike Reed862818b2020-03-21 15:07:13 -0400257extern bool gUseSkVMBlitter;
Mike Klein1e0884d2020-04-28 15:04:16 -0500258extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400259
jvanverth34524262016-05-04 13:49:13 -0700260Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500261 : fCurrentSlide(-1)
262 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500263 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400264 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500265 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500266 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500267 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500268 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400269 , fZoomWindowFixed(false)
270 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500271 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400272 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700273 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500274 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500275 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500276 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500277 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700278 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400279 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400280 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400281 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500282 , fTiled(false)
283 , fDrawTileBoundaries(false)
284 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400285 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700286{
Greg Daniel285db442016-10-14 09:12:53 -0400287 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700288
Chris Dalton37ae4b02019-12-28 14:51:11 -0700289 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600290 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500291 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500292 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600293 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600294 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500295 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700296
jvanverth2bb3b6d2016-04-08 07:24:09 -0700297 SkDebugf("Command line arguments: ");
298 for (int i = 1; i < argc; ++i) {
299 SkDebugf("%s ", argv[i]);
300 }
301 SkDebugf("\n");
302
Mike Klein88544fb2019-03-20 10:50:33 -0500303 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500304#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400305 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500306#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700307
Mike Reed862818b2020-03-21 15:07:13 -0400308 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein1e0884d2020-04-28 15:04:16 -0500309 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400310
Mike Klein19cc0f62019-03-22 15:30:07 -0500311 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500312
Brian Osmanbc8150f2017-07-24 11:38:01 -0400313 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400314 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400315
bsalomon6c471f72016-07-26 12:56:32 -0700316 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700317 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700318
csmartdalton578f0642017-02-24 16:04:47 -0700319 DisplayParams displayParams;
320 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700321 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400322 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400323 displayParams.fGrContextOptions.fShaderCacheStrategy =
324 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400325 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
326 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700327 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500328 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700329
Brian Osman56a24812017-12-19 11:15:16 -0500330 // Configure timers
331 fStatsLayer.setActive(false);
332 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
333 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
334 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
335
jvanverth9f372462016-04-06 06:08:59 -0700336 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700337 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500338 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500339 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500340 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700341
brianosman622c8d52016-05-10 06:50:49 -0700342 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500343 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
344 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
345 fWindow->inval();
346 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500347 // Command to jump directly to the slide picker and give it focus
348 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
349 this->fShowImGuiDebugWindow = true;
350 this->fShowSlidePicker = true;
351 fWindow->inval();
352 });
353 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400354 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500355 this->fShowImGuiDebugWindow = true;
356 this->fShowSlidePicker = true;
357 fWindow->inval();
358 });
Brian Osman79086b92017-02-10 13:36:16 -0500359 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
360 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
361 fWindow->inval();
362 });
Brian Osmanf6877092017-02-13 09:39:57 -0500363 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
364 this->fShowZoomWindow = !this->fShowZoomWindow;
365 fWindow->inval();
366 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400367 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
368 this->fZoomWindowFixed = !this->fZoomWindowFixed;
369 fWindow->inval();
370 });
Greg Danield0794cc2019-03-27 16:23:26 -0400371 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
372 DisplayParams params = fWindow->getRequestedDisplayParams();
373 params.fDisableVsync = !params.fDisableVsync;
374 fWindow->setRequestedDisplayParams(params);
375 this->updateTitle();
376 fWindow->inval();
377 });
Mike Reedf702ed42019-07-22 17:00:49 -0400378 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
379 fRefresh = !fRefresh;
380 fWindow->inval();
381 });
brianosman622c8d52016-05-10 06:50:49 -0700382 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500383 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700384 fWindow->inval();
385 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400386 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500387 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400388 this->updateTitle();
389 fWindow->inval();
390 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500391 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500392 switch (fColorMode) {
393 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500394 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500395 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500396 case ColorMode::kColorManaged8888:
397 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500398 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500399 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400400 this->setColorMode(ColorMode::kColorManagedF16Norm);
401 break;
402 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500403 this->setColorMode(ColorMode::kLegacy);
404 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500405 }
brianosman622c8d52016-05-10 06:50:49 -0700406 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700407 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
408 DisplayParams params = fWindow->getRequestedDisplayParams();
409 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
410 fWindow->setRequestedDisplayParams(params);
411 fWindow->inval();
412 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400413 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500414 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700415 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400416 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500417 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700418 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400419 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700420 this->changeZoomLevel(1.f / 32.f);
421 fWindow->inval();
422 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400423 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700424 this->changeZoomLevel(-1.f / 32.f);
425 fWindow->inval();
426 });
jvanverthaf236b52016-05-20 06:01:06 -0700427 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400428 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
429 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500430 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400431#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
432 if (newBackend == sk_app::Window::kVulkan_BackendType) {
433 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
434 sk_app::Window::kBackendTypeCount);
435 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
436 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500437 }
438#endif
Brian Osman621491e2017-02-28 15:45:01 -0500439 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700440 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500441 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
442 fSaveToSKP = true;
443 fWindow->inval();
444 });
Mike Reed376d8122019-03-14 11:39:02 -0400445 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
446 fShowSlideDimensions = !fShowSlideDimensions;
447 fWindow->inval();
448 });
Ben Wagner37c54032018-04-13 14:30:23 -0400449 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
450 DisplayParams params = fWindow->getRequestedDisplayParams();
451 uint32_t flags = params.fSurfaceProps.flags();
452 if (!fPixelGeometryOverrides) {
453 fPixelGeometryOverrides = true;
454 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
455 } else {
456 switch (params.fSurfaceProps.pixelGeometry()) {
457 case kUnknown_SkPixelGeometry:
458 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
459 break;
460 case kRGB_H_SkPixelGeometry:
461 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
462 break;
463 case kBGR_H_SkPixelGeometry:
464 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
465 break;
466 case kRGB_V_SkPixelGeometry:
467 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
468 break;
469 case kBGR_V_SkPixelGeometry:
470 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
471 fPixelGeometryOverrides = false;
472 break;
473 }
474 }
475 fWindow->setRequestedDisplayParams(params);
476 this->updateTitle();
477 fWindow->inval();
478 });
Ben Wagner9613e452019-01-23 10:34:59 -0500479 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500480 if (!fFontOverrides.fHinting) {
481 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400482 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500483 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500484 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400485 case SkFontHinting::kNone:
486 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500487 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400488 case SkFontHinting::kSlight:
489 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500490 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400491 case SkFontHinting::kNormal:
492 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500493 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400494 case SkFontHinting::kFull:
495 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500496 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500497 break;
498 }
499 }
500 this->updateTitle();
501 fWindow->inval();
502 });
503 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500504 if (!fPaintOverrides.fAntiAlias) {
505 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
506 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500507 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500508 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500509 } else {
510 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500511 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500512 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500513 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400514 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500515 break;
516 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500517 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500518 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400519 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500520 break;
521 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500522 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400523 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500524 break;
525 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500526 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
527 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500528 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
529 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500530 break;
531 }
532 }
533 this->updateTitle();
534 fWindow->inval();
535 });
Ben Wagner37c54032018-04-13 14:30:23 -0400536 fCommands.addCommand('D', "Modes", "DFT", [this]() {
537 DisplayParams params = fWindow->getRequestedDisplayParams();
538 uint32_t flags = params.fSurfaceProps.flags();
539 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
540 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
541 fWindow->setRequestedDisplayParams(params);
542 this->updateTitle();
543 fWindow->inval();
544 });
Ben Wagner9613e452019-01-23 10:34:59 -0500545 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500546 if (!fFontOverrides.fEdging) {
547 fFontOverrides.fEdging = true;
548 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500549 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500550 switch (fFont.getEdging()) {
551 case SkFont::Edging::kAlias:
552 fFont.setEdging(SkFont::Edging::kAntiAlias);
553 break;
554 case SkFont::Edging::kAntiAlias:
555 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
556 break;
557 case SkFont::Edging::kSubpixelAntiAlias:
558 fFont.setEdging(SkFont::Edging::kAlias);
559 fFontOverrides.fEdging = false;
560 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500561 }
562 }
563 this->updateTitle();
564 fWindow->inval();
565 });
Ben Wagner9613e452019-01-23 10:34:59 -0500566 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500567 if (!fFontOverrides.fSubpixel) {
568 fFontOverrides.fSubpixel = true;
569 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500570 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500571 if (!fFont.isSubpixel()) {
572 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500573 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500574 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500575 }
576 }
577 this->updateTitle();
578 fWindow->inval();
579 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400580 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
581 if (!fFontOverrides.fBaselineSnap) {
582 fFontOverrides.fBaselineSnap = true;
583 fFont.setBaselineSnap(false);
584 } else {
585 if (!fFont.isBaselineSnap()) {
586 fFont.setBaselineSnap(true);
587 } else {
588 fFontOverrides.fBaselineSnap = false;
589 }
590 }
591 this->updateTitle();
592 fWindow->inval();
593 });
Brian Osman805a7272018-05-02 15:40:20 -0400594 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
595 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
596 : kPerspective_Real;
597 this->updateTitle();
598 fWindow->inval();
599 });
600 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
601 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
602 : kPerspective_Off;
603 this->updateTitle();
604 fWindow->inval();
605 });
Brian Osman207d4102019-01-10 09:40:58 -0500606 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
607 fAnimTimer.togglePauseResume();
608 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400609 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
610 fZoomUI = !fZoomUI;
611 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
612 fWindow->inval();
613 });
Mike Reed59295352020-03-12 13:56:34 -0400614 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
615 fDrawViaSerialize = !fDrawViaSerialize;
616 this->updateTitle();
617 fWindow->inval();
618 });
Mike Reed862818b2020-03-21 15:07:13 -0400619 fCommands.addCommand('!', "SkVM", "Toggle SkVM", [this]() {
620 gUseSkVMBlitter = !gUseSkVMBlitter;
621 this->updateTitle();
622 fWindow->inval();
623 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500624
jvanverth2bb3b6d2016-04-08 07:24:09 -0700625 // set up slides
626 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500627 if (FLAGS_list) {
628 this->listNames();
629 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700630
Brian Osman9bb47cf2018-04-26 15:55:00 -0400631 fPerspectivePoints[0].set(0, 0);
632 fPerspectivePoints[1].set(1, 0);
633 fPerspectivePoints[2].set(0, 1);
634 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700635 fAnimTimer.run();
636
Hal Canaryc465d132017-12-08 10:21:31 -0500637 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500638 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400639 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500640 }
641 fImGuiGamutPaint.setColor(SK_ColorWHITE);
642 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
643
jongdeok.kim804f17e2019-02-26 14:39:23 +0900644 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500645 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700646}
647
jvanverth34524262016-05-04 13:49:13 -0700648void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400649 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
650 static const struct {
651 const char* fExtension;
652 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500653 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400654 const SlideFactory fFactory;
655 } gExternalSlidesInfo[] = {
656 { ".skp", "skp-dir", FLAGS_skps,
657 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
658 return sk_make_sp<SKPSlide>(name, path);}
659 },
660 { ".jpg", "jpg-dir", FLAGS_jpgs,
661 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
662 return sk_make_sp<ImageSlide>(name, path);}
663 },
Florin Malita87ccf332018-05-04 12:23:24 -0400664#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400665 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400666 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
667 return sk_make_sp<SkottieSlide>(name, path);}
668 },
Florin Malita87ccf332018-05-04 12:23:24 -0400669#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400670#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400671 { ".svg", "svg-dir", FLAGS_svgs,
672 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
673 return sk_make_sp<SvgSlide>(name, path);}
674 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400675#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400676 };
jvanverthc265a922016-04-08 12:51:45 -0700677
Brian Salomon343553a2018-09-05 15:41:23 -0400678 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700679
Mike Klein88544fb2019-03-20 10:50:33 -0500680 const auto addSlide =
681 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
682 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
683 return;
684 }
liyuqian6f163d22016-06-13 12:26:45 -0700685
Mike Klein88544fb2019-03-20 10:50:33 -0500686 if (auto slide = fact(name, path)) {
687 dirSlides.push_back(slide);
688 fSlides.push_back(std::move(slide));
689 }
690 };
Florin Malita76a076b2018-02-15 18:40:48 -0500691
Florin Malita38792ce2018-05-08 10:36:18 -0400692 if (!FLAGS_file.isEmpty()) {
693 // single file mode
694 const SkString file(FLAGS_file[0]);
695
696 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
697 for (const auto& sinfo : gExternalSlidesInfo) {
698 if (file.endsWith(sinfo.fExtension)) {
699 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
700 return;
701 }
702 }
703
704 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
705 } else {
706 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
707 }
708
709 return;
710 }
711
712 // Bisect slide.
713 if (!FLAGS_bisect.isEmpty()) {
714 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500715 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400716 if (FLAGS_bisect.count() >= 2) {
717 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
718 bisect->onChar(*ch);
719 }
720 }
721 fSlides.push_back(std::move(bisect));
722 }
723 }
724
725 // GMs
726 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400727 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400728 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500729 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400730 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400731 fSlides.push_back(std::move(slide));
732 }
Florin Malita38792ce2018-05-08 10:36:18 -0400733 }
734 // reverse gms
735 int numGMs = fSlides.count() - firstGM;
736 for (int i = 0; i < numGMs/2; ++i) {
737 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
738 }
739
740 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400741 for (const SampleFactory factory : SampleRegistry::Range()) {
742 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500743 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400744 fSlides.push_back(slide);
745 }
Florin Malita38792ce2018-05-08 10:36:18 -0400746 }
747
Brian Osman7c979f52019-02-12 13:27:51 -0500748 // Particle demo
749 {
750 // TODO: Convert this to a sample
751 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500752 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500753 fSlides.push_back(std::move(slide));
754 }
755 }
756
Brian Osmand927bd22019-12-18 11:23:12 -0500757 // Runtime shader editor
758 {
759 sk_sp<Slide> slide(new SkSLSlide());
760 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
761 fSlides.push_back(std::move(slide));
762 }
763 }
764
Florin Malita0ffa3222018-04-05 14:34:45 -0400765 for (const auto& info : gExternalSlidesInfo) {
766 for (const auto& flag : info.fFlags) {
767 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
768 // single file
769 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
770 } else {
771 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400772 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400773 SkTArray<SkString> sortedFilenames;
774 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400775 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400776 sortedFilenames.push_back(name);
777 }
778 if (sortedFilenames.count()) {
779 SkTQSort(sortedFilenames.begin(), sortedFilenames.end() - 1,
780 [](const SkString& a, const SkString& b) {
781 return strcmp(a.c_str(), b.c_str()) < 0;
782 });
783 }
784 for (const SkString& filename : sortedFilenames) {
785 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
786 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400787 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400788 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400789 if (!dirSlides.empty()) {
790 fSlides.push_back(
791 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
792 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500793 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400794 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400795 }
796 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500797
798 if (!fSlides.count()) {
799 sk_sp<Slide> slide(new NullSlide());
800 fSlides.push_back(std::move(slide));
801 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700802}
803
804
jvanverth34524262016-05-04 13:49:13 -0700805Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700806 fWindow->detach();
807 delete fWindow;
808}
809
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500810struct SkPaintTitleUpdater {
811 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
812 void append(const char* s) {
813 if (fCount == 0) {
814 fTitle->append(" {");
815 } else {
816 fTitle->append(", ");
817 }
818 fTitle->append(s);
819 ++fCount;
820 }
821 void done() {
822 if (fCount > 0) {
823 fTitle->append("}");
824 }
825 }
826 SkString* fTitle;
827 int fCount;
828};
829
brianosman05de2162016-05-06 13:28:57 -0700830void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700831 if (!fWindow) {
832 return;
833 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500834 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700835 return; // Surface hasn't been created yet.
836 }
837
jvanverth34524262016-05-04 13:49:13 -0700838 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700839 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700840
Mike Kleine5acd752019-03-22 09:57:16 -0500841 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400842 if (gSkForceAnalyticAA) {
843 title.append(" <FAAA>");
844 } else {
845 title.append(" <AAA>");
846 }
847 }
Mike Reed59295352020-03-12 13:56:34 -0400848 if (fDrawViaSerialize) {
849 title.append(" <serialize>");
850 }
Mike Reed862818b2020-03-21 15:07:13 -0400851 if (gUseSkVMBlitter) {
852 title.append(" <skvm>");
853 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400854
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500855 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500856 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
857 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400858 const char* on, const char* off)
859 {
Ben Wagner9613e452019-01-23 10:34:59 -0500860 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400861 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500862 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400863 };
864
Ben Wagner9613e452019-01-23 10:34:59 -0500865 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
866 const char* on, const char* off)
867 {
868 if (fFontOverrides.*flag) {
869 paintTitle.append((fFont.*isFlag)() ? on : off);
870 }
871 };
872
873 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
874 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500875 if (fPaintOverrides.fFilterQuality) {
876 switch (fPaint.getFilterQuality()) {
877 case kNone_SkFilterQuality:
878 paintTitle.append("NoFilter");
879 break;
880 case kLow_SkFilterQuality:
881 paintTitle.append("LowFilter");
882 break;
883 case kMedium_SkFilterQuality:
884 paintTitle.append("MediumFilter");
885 break;
886 case kHigh_SkFilterQuality:
887 paintTitle.append("HighFilter");
888 break;
889 }
890 }
Ben Wagner9613e452019-01-23 10:34:59 -0500891
892 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
893 "Force Autohint", "No Force Autohint");
894 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400895 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500896 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
897 "Linear Metrics", "Non-Linear Metrics");
898 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
899 "Bitmap Text", "No Bitmap Text");
900 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
901
902 if (fFontOverrides.fEdging) {
903 switch (fFont.getEdging()) {
904 case SkFont::Edging::kAlias:
905 paintTitle.append("Alias Text");
906 break;
907 case SkFont::Edging::kAntiAlias:
908 paintTitle.append("Antialias Text");
909 break;
910 case SkFont::Edging::kSubpixelAntiAlias:
911 paintTitle.append("Subpixel Antialias Text");
912 break;
913 }
914 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400915
Mike Reed3ae47332019-01-04 10:11:46 -0500916 if (fFontOverrides.fHinting) {
917 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400918 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500919 paintTitle.append("No Hinting");
920 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400921 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500922 paintTitle.append("Slight Hinting");
923 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400924 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500925 paintTitle.append("Normal Hinting");
926 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400927 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500928 paintTitle.append("Full Hinting");
929 break;
930 }
931 }
932 paintTitle.done();
933
Brian Osman92004802017-03-06 11:47:26 -0500934 switch (fColorMode) {
935 case ColorMode::kLegacy:
936 title.append(" Legacy 8888");
937 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500938 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500939 title.append(" ColorManaged 8888");
940 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500941 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500942 title.append(" ColorManaged F16");
943 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400944 case ColorMode::kColorManagedF16Norm:
945 title.append(" ColorManaged F16 Norm");
946 break;
Brian Osman92004802017-03-06 11:47:26 -0500947 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500948
Brian Osman92004802017-03-06 11:47:26 -0500949 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500950 int curPrimaries = -1;
951 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
952 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
953 curPrimaries = i;
954 break;
955 }
956 }
Brian Osman03115dc2018-11-26 13:55:19 -0500957 title.appendf(" %s Gamma %f",
958 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500959 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700960 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500961
Ben Wagner37c54032018-04-13 14:30:23 -0400962 const DisplayParams& params = fWindow->getRequestedDisplayParams();
963 if (fPixelGeometryOverrides) {
964 switch (params.fSurfaceProps.pixelGeometry()) {
965 case kUnknown_SkPixelGeometry:
966 title.append( " Flat");
967 break;
968 case kRGB_H_SkPixelGeometry:
969 title.append( " RGB");
970 break;
971 case kBGR_H_SkPixelGeometry:
972 title.append( " BGR");
973 break;
974 case kRGB_V_SkPixelGeometry:
975 title.append( " RGBV");
976 break;
977 case kBGR_V_SkPixelGeometry:
978 title.append( " BGRV");
979 break;
980 }
981 }
982
983 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
984 title.append(" DFT");
985 }
986
csmartdalton578f0642017-02-24 16:04:47 -0700987 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700988 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500989 int msaa = fWindow->sampleCount();
990 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700991 title.appendf(" MSAA: %i", msaa);
992 }
993 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700994
995 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -0700996 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700997 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
998 }
999
Brian Osman805a7272018-05-02 15:40:20 -04001000 if (kPerspective_Real == fPerspectiveMode) {
1001 title.append(" Perpsective (Real)");
1002 } else if (kPerspective_Fake == fPerspectiveMode) {
1003 title.append(" Perspective (Fake)");
1004 }
1005
brianosman05de2162016-05-06 13:28:57 -07001006 fWindow->setTitle(title.c_str());
1007}
1008
Florin Malitaab99c342018-01-16 16:23:03 -05001009int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001010
1011 if (!FLAGS_slide.isEmpty()) {
1012 int count = fSlides.count();
1013 for (int i = 0; i < count; i++) {
1014 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001015 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001016 }
1017 }
1018
1019 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1020 this->listNames();
1021 }
1022
Florin Malitaab99c342018-01-16 16:23:03 -05001023 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001024}
1025
Florin Malitaab99c342018-01-16 16:23:03 -05001026void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001027 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001028 for (const auto& slide : fSlides) {
1029 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001030 }
1031}
1032
Florin Malitaab99c342018-01-16 16:23:03 -05001033void Viewer::setCurrentSlide(int slide) {
1034 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001035
Florin Malitaab99c342018-01-16 16:23:03 -05001036 if (slide == fCurrentSlide) {
1037 return;
1038 }
1039
1040 if (fCurrentSlide >= 0) {
1041 fSlides[fCurrentSlide]->unload();
1042 }
1043
1044 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1045 SkIntToScalar(fWindow->height()));
1046 fCurrentSlide = slide;
1047 this->setupCurrentSlide();
1048}
1049
1050void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001051 if (fCurrentSlide >= 0) {
1052 // prepare dimensions for image slides
1053 fGesture.resetTouchState();
1054 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001055
Jim Van Verth0848fb02018-01-22 13:39:30 -05001056 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1057 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1058 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001059
Jim Van Verth0848fb02018-01-22 13:39:30 -05001060 // Start with a matrix that scales the slide to the available screen space
1061 if (fWindow->scaleContentToFit()) {
1062 if (windowRect.width() > 0 && windowRect.height() > 0) {
1063 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1064 }
liyuqiane46e4f02016-05-20 07:32:19 -07001065 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001066
1067 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001068 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001069
1070 this->updateTitle();
1071 this->updateUIState();
1072
1073 fStatsLayer.resetMeasurements();
1074
1075 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001076 }
jvanverthc265a922016-04-08 12:51:45 -07001077}
1078
Brian Osmanaba642c2020-02-06 12:52:25 -05001079#define MAX_ZOOM_LEVEL 8.0f
1080#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001081
jvanverth34524262016-05-04 13:49:13 -07001082void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001083 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001084 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001085 this->preTouchMatrixChanged();
1086}
Yuqian Li755778c2018-03-28 16:23:31 -04001087
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001088void Viewer::preTouchMatrixChanged() {
1089 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001090 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1091 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1092 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1093 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1094}
1095
Brian Osman805a7272018-05-02 15:40:20 -04001096SkMatrix Viewer::computePerspectiveMatrix() {
1097 SkScalar w = fWindow->width(), h = fWindow->height();
1098 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1099 SkPoint perspPts[4] = {
1100 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1101 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1102 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1103 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1104 };
1105 SkMatrix m;
1106 m.setPolyToPoly(orthoPts, perspPts, 4);
1107 return m;
1108}
1109
Yuqian Li755778c2018-03-28 16:23:31 -04001110SkMatrix Viewer::computePreTouchMatrix() {
1111 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001112
1113 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001114 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001115 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001116
1117 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1118 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001119
Brian Osman805a7272018-05-02 15:40:20 -04001120 if (kPerspective_Real == fPerspectiveMode) {
1121 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001122 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001123 }
1124
Yuqian Li755778c2018-03-28 16:23:31 -04001125 return m;
jvanverthc265a922016-04-08 12:51:45 -07001126}
1127
liyuqiand3cdbca2016-05-17 12:44:20 -07001128SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001129 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001130 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001131 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001132 return m;
jvanverthc265a922016-04-08 12:51:45 -07001133}
1134
Brian Osman621491e2017-02-28 15:45:01 -05001135void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001136 fPersistentCache.reset();
1137 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001138 fBackendType = backendType;
1139
1140 fWindow->detach();
1141
Brian Osman70d2f432017-11-08 09:54:10 -05001142#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001143 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1144 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001145 DisplayParams params = fWindow->getRequestedDisplayParams();
1146 delete fWindow;
1147 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001148
Brian Osman70d2f432017-11-08 09:54:10 -05001149 // re-register callbacks
1150 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001151 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001152 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001153 fWindow->pushLayer(&fImGuiLayer);
1154
Brian Osman70d2f432017-11-08 09:54:10 -05001155 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1156 // will still include our correct sample count. But the re-created fWindow will lose that
1157 // information. On Windows, we need to re-create the window when changing sample count,
1158 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1159 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1160 // as if we had never changed windows at all).
1161 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001162#endif
1163
Brian Osman70d2f432017-11-08 09:54:10 -05001164 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001165}
1166
Brian Osman92004802017-03-06 11:47:26 -05001167void Viewer::setColorMode(ColorMode colorMode) {
1168 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001169 this->updateTitle();
1170 fWindow->inval();
1171}
1172
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001173class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1174public:
Mike Reed3ae47332019-01-04 10:11:46 -05001175 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1176 SkFont* font, Viewer::SkFontFields* ffields)
1177 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001178 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001179 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1180 sk_sp<SkTextBlob>* cache) {
1181 bool blobWillChange = false;
1182 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001183 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1184 bool shouldDraw = this->filterFont(&filteredFont);
1185 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001186 blobWillChange = true;
1187 break;
1188 }
1189 }
1190 if (!blobWillChange) {
1191 return blob;
1192 }
1193
1194 SkTextBlobBuilder builder;
1195 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001196 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1197 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001198 if (!shouldDraw) {
1199 continue;
1200 }
1201
Mike Reed3ae47332019-01-04 10:11:46 -05001202 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001203
Ben Wagner41e40472018-09-24 13:01:54 -04001204 const SkTextBlobBuilder::RunBuffer& runBuffer
1205 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001206 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001207 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001208 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001209 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001210 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001211 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001212 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001213 it.glyphCount(), it.textSize(), SkString())
1214 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1215 uint32_t glyphCount = it.glyphCount();
1216 if (it.glyphs()) {
1217 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1218 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1219 }
1220 if (it.pos()) {
1221 size_t posSize = sizeof(decltype(*it.pos()));
1222 uint8_t positioning = it.positioning();
1223 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1224 }
1225 if (it.text()) {
1226 size_t textSize = sizeof(decltype(*it.text()));
1227 uint32_t textCount = it.textSize();
1228 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1229 }
1230 if (it.clusters()) {
1231 size_t clusterSize = sizeof(decltype(*it.clusters()));
1232 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1233 }
1234 }
1235 *cache = builder.make();
1236 return cache->get();
1237 }
1238 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1239 const SkPaint& paint) override {
1240 sk_sp<SkTextBlob> cache;
1241 this->SkPaintFilterCanvas::onDrawTextBlob(
1242 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1243 }
Mike Reed3ae47332019-01-04 10:11:46 -05001244 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001245 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001246 font->writable()->setSize(fFont->getSize());
1247 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001248 if (fFontOverrides->fScaleX) {
1249 font->writable()->setScaleX(fFont->getScaleX());
1250 }
1251 if (fFontOverrides->fSkewX) {
1252 font->writable()->setSkewX(fFont->getSkewX());
1253 }
Mike Reed3ae47332019-01-04 10:11:46 -05001254 if (fFontOverrides->fHinting) {
1255 font->writable()->setHinting(fFont->getHinting());
1256 }
Ben Wagner9613e452019-01-23 10:34:59 -05001257 if (fFontOverrides->fEdging) {
1258 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001259 }
Ben Wagner9613e452019-01-23 10:34:59 -05001260 if (fFontOverrides->fEmbolden) {
1261 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001262 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001263 if (fFontOverrides->fBaselineSnap) {
1264 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1265 }
Ben Wagner9613e452019-01-23 10:34:59 -05001266 if (fFontOverrides->fLinearMetrics) {
1267 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001268 }
Ben Wagner9613e452019-01-23 10:34:59 -05001269 if (fFontOverrides->fSubpixel) {
1270 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001271 }
Ben Wagner9613e452019-01-23 10:34:59 -05001272 if (fFontOverrides->fEmbeddedBitmaps) {
1273 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001274 }
Ben Wagner9613e452019-01-23 10:34:59 -05001275 if (fFontOverrides->fForceAutoHinting) {
1276 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001277 }
Ben Wagner9613e452019-01-23 10:34:59 -05001278
Mike Reed3ae47332019-01-04 10:11:46 -05001279 return true;
1280 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001281 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001282 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001283 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001284 }
Ben Wagner9613e452019-01-23 10:34:59 -05001285 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001286 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001287 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001288 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001289 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001290 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001291 return true;
1292 }
1293 SkPaint* fPaint;
1294 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001295 SkFont* fFont;
1296 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001297};
1298
Robert Phillips9882dae2019-03-04 11:00:10 -05001299void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001300 if (fCurrentSlide < 0) {
1301 return;
1302 }
1303
Robert Phillips9882dae2019-03-04 11:00:10 -05001304 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001305
Brian Osmanf750fbc2017-02-08 10:47:28 -05001306 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001307 SkSurface* slideSurface = surface;
1308 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001309 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001310
Brian Osmane0d4fba2017-03-15 10:24:55 -04001311 // 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 -05001312 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001313 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001314 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001315 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001316 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001317 }
1318
Brian Osman3ac99cf2017-12-01 11:23:53 -05001319 if (fSaveToSKP) {
1320 SkPictureRecorder recorder;
1321 SkCanvas* recorderCanvas = recorder.beginRecording(
1322 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001323 fSlides[fCurrentSlide]->draw(recorderCanvas);
1324 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1325 SkFILEWStream stream("sample_app.skp");
1326 picture->serialize(&stream);
1327 fSaveToSKP = false;
1328 }
1329
Brian Osmane9ed0f02018-11-26 14:50:05 -05001330 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001331 SkColorType colorType;
1332 switch (fColorMode) {
1333 case ColorMode::kLegacy:
1334 case ColorMode::kColorManaged8888:
1335 colorType = kN32_SkColorType;
1336 break;
1337 case ColorMode::kColorManagedF16:
1338 colorType = kRGBA_F16_SkColorType;
1339 break;
1340 case ColorMode::kColorManagedF16Norm:
1341 colorType = kRGBA_F16Norm_SkColorType;
1342 break;
1343 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001344
1345 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001346 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1347 slideCanvas->getProps(&props);
1348
Brian Osmane9ed0f02018-11-26 14:50:05 -05001349 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1350 return Window::kRaster_BackendType == this->fBackendType
1351 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001352 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001353 };
1354
Brian Osman03115dc2018-11-26 13:55:19 -05001355 // We need to render offscreen if we're...
1356 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1357 // ... in any raster mode, because the window surface is actually GL
1358 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001359 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001360 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001361 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001362 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001363 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001364 colorSpace != nullptr ||
1365 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001366
Brian Osmane9ed0f02018-11-26 14:50:05 -05001367 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001368 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001369 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001370 }
1371
Mike Reed59295352020-03-12 13:56:34 -04001372 SkPictureRecorder recorder;
1373 SkCanvas* recorderRestoreCanvas = nullptr;
1374 if (fDrawViaSerialize) {
1375 recorderRestoreCanvas = slideCanvas;
1376 slideCanvas = recorder.beginRecording(
1377 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1378 }
1379
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001380 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001381 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001382 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001383 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001384 if (fTiled) {
1385 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1386 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001387 for (int y = 0; y < fWindow->height(); y += tileH) {
1388 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001389 SkAutoCanvasRestore acr(slideCanvas, true);
1390 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1391 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001392 }
1393 }
1394
1395 // Draw borders between tiles
1396 if (fDrawTileBoundaries) {
1397 SkPaint border;
1398 border.setColor(0x60FF00FF);
1399 border.setStyle(SkPaint::kStroke_Style);
1400 for (int y = 0; y < fWindow->height(); y += tileH) {
1401 for (int x = 0; x < fWindow->width(); x += tileW) {
1402 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1403 }
1404 }
1405 }
1406 } else {
1407 slideCanvas->concat(this->computeMatrix());
1408 if (kPerspective_Real == fPerspectiveMode) {
1409 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1410 }
Mike Reed3ae47332019-01-04 10:11:46 -05001411 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001412 fSlides[fCurrentSlide]->draw(&filterCanvas);
1413 }
Brian Osman56a24812017-12-19 11:15:16 -05001414 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001415 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001416
Mike Reed59295352020-03-12 13:56:34 -04001417 if (recorderRestoreCanvas) {
1418 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1419 auto data = picture->serialize();
1420 slideCanvas = recorderRestoreCanvas;
1421 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1422 }
1423
Brian Osman1df161a2017-02-09 12:10:20 -05001424 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001425 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001426 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001427 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001428
1429 // If we rendered offscreen, snap an image and push the results to the window's canvas
1430 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001431 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001432
Robert Phillips9882dae2019-03-04 11:00:10 -05001433 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001434 SkPaint paint;
1435 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001436 int prePerspectiveCount = canvas->save();
1437 if (kPerspective_Fake == fPerspectiveMode) {
1438 paint.setFilterQuality(kHigh_SkFilterQuality);
1439 canvas->clear(SK_ColorWHITE);
1440 canvas->concat(this->computePerspectiveMatrix());
1441 }
Brian Osman03115dc2018-11-26 13:55:19 -05001442 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001443 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001444 }
Mike Reed376d8122019-03-14 11:39:02 -04001445
1446 if (fShowSlideDimensions) {
1447 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1448 SkPaint paint;
1449 paint.setColor(0x40FFFF00);
1450 surface->getCanvas()->drawRect(r, paint);
1451 }
liyuqian6f163d22016-06-13 12:26:45 -07001452}
1453
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001454void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001455 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001456 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001457}
Jim Van Verth6f449692017-02-14 15:16:46 -05001458
Robert Phillips9882dae2019-03-04 11:00:10 -05001459void Viewer::onPaint(SkSurface* surface) {
1460 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001461
Robert Phillips9882dae2019-03-04 11:00:10 -05001462 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001463
Brian Osmand67e5182017-12-08 16:46:09 -05001464 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001465
1466 if (GrContext* ctx = fWindow->getGrContext()) {
1467 // Clean out cache items that haven't been used in more than 10 seconds.
1468 ctx->performDeferredCleanup(std::chrono::seconds(10));
1469 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001470}
1471
Ben Wagnera1915972018-08-09 15:06:19 -04001472void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001473 if (fCurrentSlide >= 0) {
1474 fSlides[fCurrentSlide]->resize(width, height);
1475 }
Ben Wagnera1915972018-08-09 15:06:19 -04001476}
1477
Florin Malitacefc1b92018-02-19 21:43:47 -05001478SkPoint Viewer::mapEvent(float x, float y) {
1479 const auto m = this->computeMatrix();
1480 SkMatrix inv;
1481
1482 SkAssertResult(m.invert(&inv));
1483
1484 return inv.mapXY(x, y);
1485}
1486
Hal Canaryb1f411a2019-08-29 10:39:22 -04001487bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001488 if (GestureDevice::kMouse == fGestureDevice) {
1489 return false;
1490 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001491
1492 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001493 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001494 fWindow->inval();
1495 return true;
1496 }
1497
liyuqiand3cdbca2016-05-17 12:44:20 -07001498 void* castedOwner = reinterpret_cast<void*>(owner);
1499 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001500 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001501 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001502#if defined(SK_BUILD_FOR_IOS)
1503 // TODO: move IOS swipe detection higher up into the platform code
1504 SkPoint dir;
1505 if (fGesture.isFling(&dir)) {
1506 // swiping left or right
1507 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1508 if (dir.fX < 0) {
1509 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1510 fCurrentSlide + 1 : 0);
1511 } else {
1512 this->setCurrentSlide(fCurrentSlide > 0 ?
1513 fCurrentSlide - 1 : fSlides.count() - 1);
1514 }
1515 }
1516 fGesture.reset();
1517 }
1518#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001519 break;
1520 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001521 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001522 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001523 break;
1524 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001525 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001526 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001527 break;
1528 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001529 default: {
1530 // kLeft and kRight are only for swipes
1531 SkASSERT(false);
1532 break;
1533 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001534 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001535 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001536 fWindow->inval();
1537 return true;
1538}
1539
Hal Canaryb1f411a2019-08-29 10:39:22 -04001540bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001541 if (GestureDevice::kTouch == fGestureDevice) {
1542 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001543 }
Brian Osman16c81a12017-12-20 11:58:34 -05001544
Florin Malitacefc1b92018-02-19 21:43:47 -05001545 const auto slidePt = this->mapEvent(x, y);
1546 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1547 fWindow->inval();
1548 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001549 }
1550
1551 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001552 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001553 fGesture.touchEnd(nullptr);
1554 break;
1555 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001556 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001557 fGesture.touchBegin(nullptr, x, y);
1558 break;
1559 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001560 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001561 fGesture.touchMoved(nullptr, x, y);
1562 break;
1563 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001564 default: {
1565 SkASSERT(false); // shouldn't see kRight or kLeft here
1566 break;
1567 }
Brian Osman16c81a12017-12-20 11:58:34 -05001568 }
1569 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1570
Hal Canaryb1f411a2019-08-29 10:39:22 -04001571 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001572 fWindow->inval();
1573 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001574 return true;
1575}
1576
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001577bool Viewer::onFling(skui::InputState state) {
1578 if (skui::InputState::kRight == state) {
1579 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1580 return true;
1581 } else if (skui::InputState::kLeft == state) {
1582 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1583 return true;
1584 }
1585 return false;
1586}
1587
1588bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1589 switch (state) {
1590 case skui::InputState::kDown:
1591 fGesture.startZoom();
1592 return true;
1593 break;
1594 case skui::InputState::kMove:
1595 fGesture.updateZoom(scale, x, y, x, y);
1596 return true;
1597 break;
1598 case skui::InputState::kUp:
1599 fGesture.endZoom();
1600 return true;
1601 break;
1602 default:
1603 SkASSERT(false);
1604 break;
1605 }
1606
1607 return false;
1608}
1609
Brian Osmana109e392017-02-24 09:49:14 -05001610static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001611 // The gamut image covers a (0.8 x 0.9) shaped region
1612 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001613
1614 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1615 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1616 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001617 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1618 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1619 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001620
Brian Osman535c5e32019-02-09 16:32:58 -05001621 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1622 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1623 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1624 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1625 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001626}
1627
Ben Wagner3627d2e2018-06-26 14:23:20 -04001628static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001629 ImGui::DragCanvas dc(pt);
1630 dc.fillColor(IM_COL32(0, 0, 0, 128));
1631 dc.dragPoint(pt);
1632 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001633}
1634
Brian Osman9bb47cf2018-04-26 15:55:00 -04001635static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001636 ImGui::DragCanvas dc(pts);
1637 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001638
Brian Osman535c5e32019-02-09 16:32:58 -05001639 for (int i = 0; i < 4; ++i) {
1640 dc.dragPoint(pts + i);
1641 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001642
Brian Osman535c5e32019-02-09 16:32:58 -05001643 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1644 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1645 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1646 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001647
Brian Osman535c5e32019-02-09 16:32:58 -05001648 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001649}
1650
Brian Osmand67e5182017-12-08 16:46:09 -05001651void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001652 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1653 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001654 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001655 }
1656
1657 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001658 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1659 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001660 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001661 DisplayParams params = fWindow->getRequestedDisplayParams();
1662 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001663 const GrContext* ctx = fWindow->getGrContext();
1664
Brian Osmana109e392017-02-24 09:49:14 -05001665 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1666 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001667 if (ImGui::CollapsingHeader("Backend")) {
1668 int newBackend = static_cast<int>(fBackendType);
1669 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1670 ImGui::SameLine();
1671 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001672#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1673 ImGui::SameLine();
1674 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1675#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001676#if defined(SK_DAWN)
1677 ImGui::SameLine();
1678 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1679#endif
Brian Osman621491e2017-02-28 15:45:01 -05001680#if defined(SK_VULKAN)
1681 ImGui::SameLine();
1682 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1683#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001684#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001685 ImGui::SameLine();
1686 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1687#endif
Brian Osman621491e2017-02-28 15:45:01 -05001688 if (newBackend != fBackendType) {
1689 fDeferredActions.push_back([=]() {
1690 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1691 });
1692 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001693
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001694 bool* wire = &params.fGrContextOptions.fWireframeMode;
1695 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1696 paramsChanged = true;
1697 }
Brian Salomon99a33902017-03-07 15:16:34 -05001698
Brian Osman28b12522017-03-08 17:10:24 -05001699 if (ctx) {
1700 int sampleCount = fWindow->sampleCount();
1701 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001702 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001703 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1704 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1705 ImGui::RadioButton("16", &sampleCount, 16);
1706
1707 if (sampleCount != params.fMSAASampleCount) {
1708 params.fMSAASampleCount = sampleCount;
1709 paramsChanged = true;
1710 }
1711 }
1712
Ben Wagner37c54032018-04-13 14:30:23 -04001713 int pixelGeometryIdx = 0;
1714 if (fPixelGeometryOverrides) {
1715 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1716 }
1717 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1718 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1719 {
1720 uint32_t flags = params.fSurfaceProps.flags();
1721 if (pixelGeometryIdx == 0) {
1722 fPixelGeometryOverrides = false;
1723 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1724 } else {
1725 fPixelGeometryOverrides = true;
1726 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1727 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1728 }
1729 paramsChanged = true;
1730 }
1731
1732 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1733 if (ImGui::Checkbox("DFT", &useDFT)) {
1734 uint32_t flags = params.fSurfaceProps.flags();
1735 if (useDFT) {
1736 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1737 } else {
1738 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1739 }
1740 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1741 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1742 paramsChanged = true;
1743 }
1744
Brian Osman8a9de3d2017-03-01 14:59:05 -05001745 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001746 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001747 auto prButton = [&](GpuPathRenderers x) {
1748 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001749 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1750 params.fGrContextOptions.fGpuPathRenderers = x;
1751 paramsChanged = true;
1752 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001753 }
1754 };
1755
1756 if (!ctx) {
1757 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001758 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001759 const auto* caps = ctx->priv().caps();
1760 prButton(GpuPathRenderers::kDefault);
1761 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001762 if (caps->shaderCaps()->tessellationSupport()) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001763 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001764 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001765 if (caps->shaderCaps()->pathRenderingSupport()) {
1766 prButton(GpuPathRenderers::kStencilAndCover);
1767 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001768 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001769 if (1 == fWindow->sampleCount()) {
1770 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1771 prButton(GpuPathRenderers::kCoverageCounting);
1772 }
1773 prButton(GpuPathRenderers::kSmall);
1774 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001775 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001776 prButton(GpuPathRenderers::kNone);
1777 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001778 ImGui::TreePop();
1779 }
Brian Osman621491e2017-02-28 15:45:01 -05001780 }
1781
Ben Wagner964571d2019-03-08 12:35:06 -05001782 if (ImGui::CollapsingHeader("Tiling")) {
1783 ImGui::Checkbox("Enable", &fTiled);
1784 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1785 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1786 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1787 }
1788
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001789 if (ImGui::CollapsingHeader("Transform")) {
1790 float zoom = fZoomLevel;
1791 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1792 fZoomLevel = zoom;
1793 this->preTouchMatrixChanged();
1794 paramsChanged = true;
1795 }
1796 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001797 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001798 fRotation = deg;
1799 this->preTouchMatrixChanged();
1800 paramsChanged = true;
1801 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001802 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1803 if (ImGui_DragLocation(&fOffset)) {
1804 this->preTouchMatrixChanged();
1805 paramsChanged = true;
1806 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001807 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1808 this->preTouchMatrixChanged();
1809 paramsChanged = true;
1810 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001811 }
Brian Osman805a7272018-05-02 15:40:20 -04001812 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1813 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1814 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001815 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001816 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001817 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001818 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001819 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001820 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001821 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001822 }
1823
Ben Wagnera580fb32018-04-17 11:16:32 -04001824 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001825 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001826 if (fPaintOverrides.fAntiAlias) {
1827 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001828 }
1829 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001830 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001831 {
1832 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1833 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001834 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001835 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1836 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001837 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001838 fPaintOverrides.fAntiAlias = true;
1839 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001840 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001841 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001842 case SkPaintFields::AntiAliasState::Alias:
1843 break;
1844 case SkPaintFields::AntiAliasState::Normal:
1845 break;
1846 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1847 gSkUseAnalyticAA = true;
1848 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001849 break;
1850 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1851 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001852 break;
1853 }
1854 }
1855 paramsChanged = true;
1856 }
1857
Ben Wagner99a78dc2018-05-09 18:23:51 -04001858 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001859 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001860 bool (SkPaint::* isFlag)() const,
1861 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001862 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001863 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001864 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001865 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001866 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001867 if (ImGui::Combo(label, &itemIndex, items)) {
1868 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001869 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001870 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001871 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001872 (fPaint.*setFlag)(itemIndex == 2);
1873 }
1874 paramsChanged = true;
1875 }
1876 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001877
Ben Wagner99a78dc2018-05-09 18:23:51 -04001878 paintFlag("Dither",
1879 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001880 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001881 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001882
1883 int filterQualityIdx = 0;
1884 if (fPaintOverrides.fFilterQuality) {
1885 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1886 }
1887 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1888 "Default\0None\0Low\0Medium\0High\0\0"))
1889 {
1890 if (filterQualityIdx == 0) {
1891 fPaintOverrides.fFilterQuality = false;
1892 fPaint.setFilterQuality(kNone_SkFilterQuality);
1893 } else {
1894 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1895 fPaintOverrides.fFilterQuality = true;
1896 }
1897 paramsChanged = true;
1898 }
Ben Wagner9613e452019-01-23 10:34:59 -05001899 }
Hal Canary02738a82019-01-21 18:51:32 +00001900
Ben Wagner9613e452019-01-23 10:34:59 -05001901 if (ImGui::CollapsingHeader("Font")) {
1902 int hintingIdx = 0;
1903 if (fFontOverrides.fHinting) {
1904 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1905 }
1906 if (ImGui::Combo("Hinting", &hintingIdx,
1907 "Default\0None\0Slight\0Normal\0Full\0\0"))
1908 {
1909 if (hintingIdx == 0) {
1910 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001911 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001912 } else {
1913 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1914 fFontOverrides.fHinting = true;
1915 }
1916 paramsChanged = true;
1917 }
Hal Canary02738a82019-01-21 18:51:32 +00001918
Ben Wagner9613e452019-01-23 10:34:59 -05001919 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1920 bool SkFontFields::* flag,
1921 bool (SkFont::* isFlag)() const,
1922 void (SkFont::* setFlag)(bool) )
1923 {
1924 int itemIndex = 0;
1925 if (fFontOverrides.*flag) {
1926 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1927 }
1928 if (ImGui::Combo(label, &itemIndex, items)) {
1929 if (itemIndex == 0) {
1930 fFontOverrides.*flag = false;
1931 } else {
1932 fFontOverrides.*flag = true;
1933 (fFont.*setFlag)(itemIndex == 2);
1934 }
1935 paramsChanged = true;
1936 }
1937 };
Hal Canary02738a82019-01-21 18:51:32 +00001938
Ben Wagner9613e452019-01-23 10:34:59 -05001939 fontFlag("Fake Bold Glyphs",
1940 "Default\0No Fake Bold\0Fake Bold\0\0",
1941 &SkFontFields::fEmbolden,
1942 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001943
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001944 fontFlag("Baseline Snapping",
1945 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1946 &SkFontFields::fBaselineSnap,
1947 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1948
Ben Wagner9613e452019-01-23 10:34:59 -05001949 fontFlag("Linear Text",
1950 "Default\0No Linear Text\0Linear Text\0\0",
1951 &SkFontFields::fLinearMetrics,
1952 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001953
Ben Wagner9613e452019-01-23 10:34:59 -05001954 fontFlag("Subpixel Position Glyphs",
1955 "Default\0Pixel Text\0Subpixel Text\0\0",
1956 &SkFontFields::fSubpixel,
1957 &SkFont::isSubpixel, &SkFont::setSubpixel);
1958
1959 fontFlag("Embedded Bitmap Text",
1960 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1961 &SkFontFields::fEmbeddedBitmaps,
1962 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1963
1964 fontFlag("Force Auto-Hinting",
1965 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1966 &SkFontFields::fForceAutoHinting,
1967 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1968
1969 int edgingIdx = 0;
1970 if (fFontOverrides.fEdging) {
1971 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1972 }
1973 if (ImGui::Combo("Edging", &edgingIdx,
1974 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1975 {
1976 if (edgingIdx == 0) {
1977 fFontOverrides.fEdging = false;
1978 fFont.setEdging(SkFont::Edging::kAlias);
1979 } else {
1980 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1981 fFontOverrides.fEdging = true;
1982 }
1983 paramsChanged = true;
1984 }
1985
Ben Wagner15a8d572019-03-21 13:35:44 -04001986 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1987 if (fFontOverrides.fSize) {
1988 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001989 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001990 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001991 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001992 fFontOverrides.fSizeRange[0],
1993 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001994 "%.6f", 2.0f))
1995 {
Mike Reed3ae47332019-01-04 10:11:46 -05001996 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001997 paramsChanged = true;
1998 }
1999 }
2000
2001 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2002 if (fFontOverrides.fScaleX) {
2003 float scaleX = fFont.getScaleX();
2004 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2005 fFont.setScaleX(scaleX);
2006 paramsChanged = true;
2007 }
2008 }
2009
2010 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2011 if (fFontOverrides.fSkewX) {
2012 float skewX = fFont.getSkewX();
2013 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2014 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002015 paramsChanged = true;
2016 }
2017 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002018 }
2019
Mike Reed81f60ec2018-05-15 10:09:52 -04002020 {
2021 SkMetaData controls;
2022 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2023 if (ImGui::CollapsingHeader("Current Slide")) {
2024 SkMetaData::Iter iter(controls);
2025 const char* name;
2026 SkMetaData::Type type;
2027 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002028 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002029 if (type == SkMetaData::kScalar_Type) {
2030 float val[3];
2031 SkASSERT(count == 3);
2032 controls.findScalars(name, &count, val);
2033 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2034 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002035 }
Ben Wagner110c7032019-03-22 17:03:59 -04002036 } else if (type == SkMetaData::kBool_Type) {
2037 bool val;
2038 SkASSERT(count == 1);
2039 controls.findBool(name, &val);
2040 if (ImGui::Checkbox(name, &val)) {
2041 controls.setBool(name, val);
2042 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002043 }
2044 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002045 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002046 }
2047 }
2048 }
2049
Ben Wagner7a3c6742018-04-23 10:01:07 -04002050 if (fShowSlidePicker) {
2051 ImGui::SetNextTreeNodeOpen(true);
2052 }
Brian Osman79086b92017-02-10 13:36:16 -05002053 if (ImGui::CollapsingHeader("Slide")) {
2054 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002055 static ImVector<const char*> filteredSlideNames;
2056 static ImVector<int> filteredSlideIndices;
2057
Brian Osmanfce09c52017-11-14 15:32:20 -05002058 if (fShowSlidePicker) {
2059 ImGui::SetKeyboardFocusHere();
2060 fShowSlidePicker = false;
2061 }
2062
Brian Osman79086b92017-02-10 13:36:16 -05002063 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002064 filteredSlideNames.clear();
2065 filteredSlideIndices.clear();
2066 int filteredIndex = 0;
2067 for (int i = 0; i < fSlides.count(); ++i) {
2068 const char* slideName = fSlides[i]->getName().c_str();
2069 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2070 if (i == fCurrentSlide) {
2071 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002072 }
Brian Osmanf479e422017-11-08 13:11:36 -05002073 filteredSlideNames.push_back(slideName);
2074 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002075 }
Brian Osman79086b92017-02-10 13:36:16 -05002076 }
Brian Osmanf479e422017-11-08 13:11:36 -05002077
Brian Osmanf479e422017-11-08 13:11:36 -05002078 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2079 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002080 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002081 }
2082 }
Brian Osmana109e392017-02-24 09:49:14 -05002083
2084 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002085 ColorMode newMode = fColorMode;
2086 auto cmButton = [&](ColorMode mode, const char* label) {
2087 if (ImGui::RadioButton(label, mode == fColorMode)) {
2088 newMode = mode;
2089 }
2090 };
2091
2092 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002093 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2094 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002095 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002096
2097 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002098 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002099 }
2100
2101 // Pick from common gamuts:
2102 int primariesIdx = 4; // Default: Custom
2103 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2104 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2105 primariesIdx = i;
2106 break;
2107 }
2108 }
2109
Brian Osman03115dc2018-11-26 13:55:19 -05002110 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002111 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002112
Brian Osmana109e392017-02-24 09:49:14 -05002113 if (ImGui::Combo("Primaries", &primariesIdx,
2114 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2115 if (primariesIdx >= 0 && primariesIdx <= 3) {
2116 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2117 }
2118 }
2119
2120 // Allow direct editing of gamut
2121 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2122 }
Brian Osman207d4102019-01-10 09:40:58 -05002123
2124 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002125 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002126 if (ImGui::Checkbox("Pause", &isPaused)) {
2127 fAnimTimer.togglePauseResume();
2128 }
Brian Osman707d2022019-01-10 11:27:34 -05002129
2130 float speed = fAnimTimer.getSpeed();
2131 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2132 fAnimTimer.setSpeed(speed);
2133 }
Brian Osman207d4102019-01-10 09:40:58 -05002134 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002135
Brian Osmanfd7657c2019-04-25 11:34:07 -04002136 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2137#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2138 || Window::kANGLE_BackendType == fBackendType
2139#endif
2140 ;
2141
2142 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2143 // than GL, we need to force it on. Just do that on the first frame after the backend
2144 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002145 if (!backendIsGL &&
2146 params.fGrContextOptions.fShaderCacheStrategy !=
2147 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2148 params.fGrContextOptions.fShaderCacheStrategy =
2149 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002150 paramsChanged = true;
2151 fPersistentCache.reset();
2152 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002153 // To re-load shaders from the currently active programs, we flush all caches on one
2154 // frame, then set a flag to poll the cache on the next frame.
2155 static bool gLoadPending = false;
2156 if (gLoadPending) {
2157 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2158 int hitCount) {
2159 CachedGLSL& entry(fCachedGLSL.push_back());
2160 entry.fKey = key;
2161 SkMD5 hash;
2162 hash.write(key->bytes(), key->size());
2163 SkMD5::Digest digest = hash.finish();
2164 for (int i = 0; i < 16; ++i) {
2165 entry.fKeyString.appendf("%02x", digest.data[i]);
2166 }
2167
Brian Osmana66081d2019-09-03 14:59:26 -04002168 SkReader32 reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002169 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002170 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2171 entry.fInputs,
2172 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002173 };
2174 fCachedGLSL.reset();
2175 fPersistentCache.foreach(collectShaders);
2176 gLoadPending = false;
2177 }
2178
2179 // Defer actually doing the load/save logic so that we can trigger a save when we
2180 // start or finish hovering on a tree node in the list below:
2181 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002182 bool doSave = ImGui::Button("Save");
2183 if (backendIsGL) {
2184 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002185 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2186 GrContextOptions::ShaderCacheStrategy::kSkSL;
2187 if (ImGui::Checkbox("SkSL", &sksl)) {
2188 params.fGrContextOptions.fShaderCacheStrategy = sksl
2189 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2190 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002191 paramsChanged = true;
2192 doLoad = true;
2193 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2194 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002195 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002196
2197 ImGui::BeginChild("##ScrollingRegion");
2198 for (auto& entry : fCachedGLSL) {
2199 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2200 bool hovered = ImGui::IsItemHovered();
2201 if (hovered != entry.fHovered) {
2202 // Force a save to patch the highlight shader in/out
2203 entry.fHovered = hovered;
2204 doSave = true;
2205 }
2206 if (inTreeNode) {
2207 // Full width, and a reasonable amount of space for each shader.
2208 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2209 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2210 boxSize);
2211 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2212 boxSize);
2213 ImGui::TreePop();
2214 }
2215 }
2216 ImGui::EndChild();
2217
2218 if (doLoad) {
2219 fPersistentCache.reset();
2220 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2221 gLoadPending = true;
2222 }
2223 if (doSave) {
2224 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002225 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002226 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2227 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002228
Brian Osman072e6fc2019-06-12 11:35:41 -04002229 SkSL::String highlight;
2230 if (!sksl) {
2231 highlight = shaderCaps->versionDeclString();
2232 if (shaderCaps->usesPrecisionModifiers()) {
2233 highlight.append("precision mediump float;\n");
2234 }
Brian Osman5bee3902019-05-07 09:55:45 -04002235 }
2236 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002237 highlight.appendf("out %s sk_FragColor;\n"
2238 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2239 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002240
2241 fPersistentCache.reset();
2242 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2243 for (auto& entry : fCachedGLSL) {
2244 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2245 if (entry.fHovered) {
2246 entry.fShader[kFragment_GrShaderType] = highlight;
2247 }
2248
Brian Osmana085a412019-04-25 09:44:43 -04002249 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2250 entry.fShader,
2251 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002252 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002253 fPersistentCache.store(*entry.fKey, *data);
2254
2255 entry.fShader[kFragment_GrShaderType] = backup;
2256 }
2257 }
2258 }
Brian Osman79086b92017-02-10 13:36:16 -05002259 }
Brian Salomon99a33902017-03-07 15:16:34 -05002260 if (paramsChanged) {
2261 fDeferredActions.push_back([=]() {
2262 fWindow->setRequestedDisplayParams(params);
2263 fWindow->inval();
2264 this->updateTitle();
2265 });
2266 }
Brian Osman79086b92017-02-10 13:36:16 -05002267 ImGui::End();
2268 }
2269
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002270 if (gShaderErrorHandler.fErrors.count()) {
2271 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2272 ImGui::Begin("Shader Errors");
2273 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2274 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002275 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2276 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2277 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2278 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002279 }
2280 ImGui::End();
2281 gShaderErrorHandler.reset();
2282 }
2283
Brian Osmanf6877092017-02-13 09:39:57 -05002284 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002285 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2286 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002287 static int zoomFactor = 8;
2288 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002289 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002290 }
2291 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2292 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002293 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002294 }
Brian Osmanf6877092017-02-13 09:39:57 -05002295
Ben Wagner3627d2e2018-06-26 14:23:20 -04002296 if (!fZoomWindowFixed) {
2297 ImVec2 mousePos = ImGui::GetMousePos();
2298 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2299 }
2300 SkScalar x = fZoomWindowLocation.x();
2301 SkScalar y = fZoomWindowLocation.y();
2302 int xInt = SkScalarRoundToInt(x);
2303 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002304 ImVec2 avail = ImGui::GetContentRegionAvail();
2305
Brian Osmanead517d2017-11-13 15:36:36 -05002306 uint32_t pixel = 0;
2307 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002308 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002309 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002310 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002311 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002312 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002313 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2314 }
2315
Brian Osmand67e5182017-12-08 16:46:09 -05002316 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002317 // Translate so the region of the image that's under the mouse cursor is centered
2318 // in the zoom canvas:
2319 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002320 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2321 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002322 c->drawImage(this->fLastImage, 0, 0);
2323
2324 SkPaint outline;
2325 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002326 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002327 });
Brian Osmanf6877092017-02-13 09:39:57 -05002328 }
2329
2330 ImGui::End();
2331 }
Brian Osman79086b92017-02-10 13:36:16 -05002332}
2333
liyuqian2edb0f42016-07-06 14:11:32 -07002334void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002335 for (int i = 0; i < fDeferredActions.count(); ++i) {
2336 fDeferredActions[i]();
2337 }
2338 fDeferredActions.reset();
2339
Brian Osman56a24812017-12-19 11:15:16 -05002340 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002341 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002342 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002343 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002344
Brian Osman79086b92017-02-10 13:36:16 -05002345 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002346 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2347 // not be visible, though. So we need to redraw if there is at least one visible window, or
2348 // more than one active window. Newly created windows are active but not visible for one frame
2349 // while they determine their layout and sizing.
2350 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2351 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002352 fWindow->inval();
2353 }
jvanverth9f372462016-04-06 06:08:59 -07002354}
liyuqiane5a6cd92016-05-27 08:52:52 -07002355
Florin Malitab632df72018-06-18 21:23:06 -04002356template <typename OptionsFunc>
2357static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2358 OptionsFunc&& optionsFunc) {
2359 writer.beginObject();
2360 {
2361 writer.appendString(kName , name);
2362 writer.appendString(kValue, value);
2363
2364 writer.beginArray(kOptions);
2365 {
2366 optionsFunc(writer);
2367 }
2368 writer.endArray();
2369 }
2370 writer.endObject();
2371}
2372
2373
liyuqiane5a6cd92016-05-27 08:52:52 -07002374void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002375 if (!fWindow) {
2376 return;
2377 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002378 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002379 return; // Surface hasn't been created yet.
2380 }
2381
Florin Malitab632df72018-06-18 21:23:06 -04002382 SkDynamicMemoryWStream memStream;
2383 SkJSONWriter writer(&memStream);
2384 writer.beginArray();
2385
liyuqianb73c24b2016-06-03 08:47:23 -07002386 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002387 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2388 [this](SkJSONWriter& writer) {
2389 for(const auto& slide : fSlides) {
2390 writer.appendString(slide->getName().c_str());
2391 }
2392 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002393
liyuqianb73c24b2016-06-03 08:47:23 -07002394 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002395 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2396 [](SkJSONWriter& writer) {
2397 for (const auto& str : kBackendTypeStrings) {
2398 writer.appendString(str);
2399 }
2400 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002401
csmartdalton578f0642017-02-24 16:04:47 -07002402 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002403 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2404 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2405 [this](SkJSONWriter& writer) {
2406 writer.appendS32(0);
2407
2408 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2409 return;
2410 }
2411
2412 for (int msaa : {4, 8, 16}) {
2413 writer.appendS32(msaa);
2414 }
2415 });
csmartdalton578f0642017-02-24 16:04:47 -07002416
csmartdalton61cd31a2017-02-27 17:00:53 -07002417 // Path renderer state
2418 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002419 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2420 [this](SkJSONWriter& writer) {
2421 const GrContext* ctx = fWindow->getGrContext();
2422 if (!ctx) {
2423 writer.appendString("Software");
2424 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002425 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002426 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2427 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002428 if (caps->shaderCaps()->tessellationSupport()) {
2429 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002430 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002431 }
Florin Malitab632df72018-06-18 21:23:06 -04002432 if (caps->shaderCaps()->pathRenderingSupport()) {
2433 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002434 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002435 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002436 }
2437 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002438 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2439 writer.appendString(
2440 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2441 }
2442 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2443 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002444 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002445 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002446 }
2447 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002448
liyuqianb73c24b2016-06-03 08:47:23 -07002449 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002450 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2451 [this](SkJSONWriter& writer) {
2452 writer.appendString(kSoftkeyHint);
2453 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2454 writer.appendString(softkey.c_str());
2455 }
2456 });
liyuqianb73c24b2016-06-03 08:47:23 -07002457
Florin Malitab632df72018-06-18 21:23:06 -04002458 writer.endArray();
2459 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002460
Florin Malitab632df72018-06-18 21:23:06 -04002461 auto data = memStream.detachAsData();
2462
2463 // TODO: would be cool to avoid this copy
2464 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2465
2466 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002467}
2468
2469void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002470 // For those who will add more features to handle the state change in this function:
2471 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2472 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2473 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002474 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002475 for (int i = 0; i < fSlides.count(); ++i) {
2476 if (fSlides[i]->getName().equals(stateValue)) {
2477 this->setCurrentSlide(i);
2478 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002479 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002480 }
Florin Malitaab99c342018-01-16 16:23:03 -05002481
2482 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002483 } else if (stateName.equals(kBackendStateName)) {
2484 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2485 if (stateValue.equals(kBackendTypeStrings[i])) {
2486 if (fBackendType != i) {
2487 fBackendType = (sk_app::Window::BackendType)i;
2488 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002489 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002490 }
2491 break;
2492 }
2493 }
csmartdalton578f0642017-02-24 16:04:47 -07002494 } else if (stateName.equals(kMSAAStateName)) {
2495 DisplayParams params = fWindow->getRequestedDisplayParams();
2496 int sampleCount = atoi(stateValue.c_str());
2497 if (sampleCount != params.fMSAASampleCount) {
2498 params.fMSAASampleCount = sampleCount;
2499 fWindow->setRequestedDisplayParams(params);
2500 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002501 this->updateTitle();
2502 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002503 }
2504 } else if (stateName.equals(kPathRendererStateName)) {
2505 DisplayParams params = fWindow->getRequestedDisplayParams();
2506 for (const auto& pair : gPathRendererNames) {
2507 if (pair.second == stateValue.c_str()) {
2508 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2509 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2510 fWindow->setRequestedDisplayParams(params);
2511 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002512 this->updateTitle();
2513 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002514 }
2515 break;
2516 }
csmartdalton578f0642017-02-24 16:04:47 -07002517 }
liyuqianb73c24b2016-06-03 08:47:23 -07002518 } else if (stateName.equals(kSoftkeyStateName)) {
2519 if (!stateValue.equals(kSoftkeyHint)) {
2520 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002521 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002522 }
liyuqian2edb0f42016-07-06 14:11:32 -07002523 } else if (stateName.equals(kRefreshStateName)) {
2524 // This state is actually NOT in the UI state.
2525 // We use this to allow Android to quickly set bool fRefresh.
2526 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002527 } else {
2528 SkDebugf("Unknown stateName: %s", stateName.c_str());
2529 }
2530}
Brian Osman79086b92017-02-10 13:36:16 -05002531
Hal Canaryb1f411a2019-08-29 10:39:22 -04002532bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002533 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002534}
2535
Hal Canaryb1f411a2019-08-29 10:39:22 -04002536bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002537 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002538 fWindow->inval();
2539 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002540 } else {
2541 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002542 }
Brian Osman79086b92017-02-10 13:36:16 -05002543}