blob: 82a005eb4dd5b809e53bdceab532245ad5968ac6 [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
Brian Salomon06c9e292021-04-29 14:10:23 -04008#include "tools/viewer/Viewer.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkData.h"
12#include "include/core/SkGraphics.h"
13#include "include/core/SkPictureRecorder.h"
14#include "include/core/SkStream.h"
15#include "include/core/SkSurface.h"
Robert Phillipsed653392020-07-10 13:55:21 -040016#include "include/gpu/GrDirectContext.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050017#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkTo.h"
19#include "include/utils/SkPaintFilterCanvas.h"
20#include "src/core/SkColorSpacePriv.h"
21#include "src/core/SkImagePriv.h"
22#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkOSFile.h"
John Stilesf2c2d302021-04-09 17:56:58 -040024#include "src/core/SkReadBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkScan.h"
Chris Dalton94df5722021-04-19 17:40:40 -060026#include "src/core/SkSurfacePriv.h"
John Stilesdf078002020-07-14 09:44:57 -040027#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040029#include "src/core/SkTextBlobPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040030#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrGpu.h"
32#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070033#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Chris Daltonff18ff62020-12-07 17:39:26 -070035#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Adlai Hollerbcfc5542020-08-27 12:44:07 -040036#include "src/image/SkImage_Base.h"
John Stiles2ee4d7a2021-03-30 10:30:47 -040037#include "src/sksl/SkSLCompiler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/utils/SkJSONWriter.h"
39#include "src/utils/SkOSPath.h"
40#include "tools/Resources.h"
41#include "tools/ToolUtils.h"
42#include "tools/flags/CommandLineFlags.h"
43#include "tools/flags/CommonFlags.h"
44#include "tools/trace/EventTracingPriv.h"
45#include "tools/viewer/BisectSlide.h"
46#include "tools/viewer/GMSlide.h"
47#include "tools/viewer/ImageSlide.h"
Brian Salomon06c9e292021-04-29 14:10:23 -040048#include "tools/viewer/MSKPSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "tools/viewer/ParticlesSlide.h"
50#include "tools/viewer/SKPSlide.h"
51#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050052#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053#include "tools/viewer/SlideDir.h"
54#include "tools/viewer/SvgSlide.h"
csmartdalton578f0642017-02-24 16:04:47 -070055
Chris Dalton17dc4182020-03-25 16:18:16 -060056#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040057#include <map>
58
Hal Canary8a001442018-09-19 11:31:27 -040059#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040060#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040061
Brian Osmanc85f1fa2020-06-16 15:11:34 -040062#ifdef SK_VULKAN
63#include "spirv-tools/libspirv.hpp"
64#endif
65
Florin Malita87ccf332018-05-04 12:23:24 -040066#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050067 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040068#endif
Florin Malita45cd2002020-06-09 14:00:54 -040069#if defined(SK_ENABLE_SKRIVE)
70 #include "tools/viewer/SkRiveSlide.h"
71#endif
Florin Malita87ccf332018-05-04 12:23:24 -040072
Brian Osman5e7fbfd2019-05-03 13:13:35 -040073class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
74public:
75 void compileError(const char* shader, const char* errors) override {
76 fShaders.push_back(SkString(shader));
77 fErrors.push_back(SkString(errors));
78 }
79
80 void reset() {
81 fShaders.reset();
82 fErrors.reset();
83 }
84
85 SkTArray<SkString> fShaders;
86 SkTArray<SkString> fErrors;
87};
88
89static CapturingShaderErrorHandler gShaderErrorHandler;
90
Brian Osmanf847f312020-06-18 14:18:27 -040091GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
92
jvanverth34524262016-05-04 13:49:13 -070093using namespace sk_app;
John Stiles2ee4d7a2021-03-30 10:30:47 -040094using SkSL::Compiler;
95using OverrideFlag = SkSL::Compiler::OverrideFlag;
jvanverth34524262016-05-04 13:49:13 -070096
csmartdalton61cd31a2017-02-27 17:00:53 -070097static std::map<GpuPathRenderers, std::string> gPathRendererNames;
98
jvanverth9f372462016-04-06 06:08:59 -070099Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -0700100 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -0700101}
102
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600103static DEFINE_string(slide, "", "Start on this sample.");
104static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -0500105
Jim Van Verth682a2f42020-05-13 16:54:09 -0400106#ifdef SK_GL
107#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700108#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400109#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700110#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400111#ifdef SK_VULKAN
112#define VK_BACKEND_STR ", \"vk\""
113#else
114#define VK_BACKEND_STR
115#endif
116#ifdef SK_METAL
117#define MTL_BACKEND_STR ", \"mtl\""
118#else
119#define MTL_BACKEND_STR
120#endif
121#ifdef SK_DIRECT3D
122#define D3D_BACKEND_STR ", \"d3d\""
123#else
124#define D3D_BACKEND_STR
125#endif
126#ifdef SK_DAWN
127#define DAWN_BACKEND_STR ", \"dawn\""
128#else
129#define DAWN_BACKEND_STR
130#endif
131#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
132#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
133 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700134
Brian Osman2dd96932016-10-18 15:33:53 -0400135static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700136
Mike Klein5b3f3432019-03-21 11:42:21 -0500137static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
Chris Daltonfd708652021-03-17 21:10:17 -0600138static DEFINE_bool(dmsaa, false, "Use internal MSAA to render to non-MSAA surfaces?");
csmartdalton008b9d82017-02-22 12:00:42 -0700139
Mike Klein84836b72019-03-21 11:31:36 -0500140static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700141
Mike Klein84836b72019-03-21 11:31:36 -0500142static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400143
Mike Kleinc6142d82019-03-25 10:54:59 -0500144static DEFINE_string2(match, m, nullptr,
145 "[~][^]substring[$] [...] of name to run.\n"
146 "Multiple matches may be separated by spaces.\n"
147 "~ causes a matching name to always be skipped\n"
148 "^ requires the start of the name to match\n"
149 "$ requires the end of the name to match\n"
150 "^ and $ requires an exact match\n"
151 "If a name does not match any list entry,\n"
152 "it is skipped unless some list entry starts with ~");
153
Mike Klein19fb3972019-03-21 13:08:08 -0500154#if defined(SK_BUILD_FOR_ANDROID)
Brian Salomon06c9e292021-04-29 14:10:23 -0400155# define PATH_PREFIX "/data/local/tmp/"
Mike Klein19fb3972019-03-21 13:08:08 -0500156#else
Brian Salomon06c9e292021-04-29 14:10:23 -0400157# define PATH_PREFIX ""
Mike Klein19fb3972019-03-21 13:08:08 -0500158#endif
159
Brian Salomon06c9e292021-04-29 14:10:23 -0400160static DEFINE_string(jpgs , PATH_PREFIX "jpgs" , "Directory to read jpgs from.");
161static DEFINE_string(skps , PATH_PREFIX "skps" , "Directory to read skps from.");
162static DEFINE_string(mskps , PATH_PREFIX "mskps" , "Directory to read mskps from.");
163static DEFINE_string(lotties, PATH_PREFIX "lotties", "Directory to read (Bodymovin) jsons from.");
164static DEFINE_string(rives , PATH_PREFIX "rives" , "Directory to read Rive (Flare) files from.");
165#undef PATH_PREFIX
166
Mike Kleinc6142d82019-03-25 10:54:59 -0500167static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
168
169static DEFINE_int_2(threads, j, -1,
170 "Run threadsafe tests on a threadpool with this many extra threads, "
171 "defaulting to one extra thread per core.");
172
Jim Van Verth7b558182019-11-14 16:47:01 -0500173static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
174
Chris Daltonc8877332020-01-06 09:48:30 -0700175static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500176static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
177static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500178static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500179static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500180static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500181
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400182#ifndef SK_GL
183static_assert(false, "viewer requires GL backend for raster.")
184#endif
185
Brian Salomon194db172017-08-17 14:37:06 -0400186const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700187 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400188#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
189 "ANGLE",
190#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400191#ifdef SK_DAWN
192 "Dawn",
193#endif
jvanverth063ece72016-06-17 09:29:14 -0700194#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700195 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700196#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400197#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500198 "Metal",
199#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400200#ifdef SK_DIRECT3D
201 "Direct3D",
202#endif
csmartdalton578f0642017-02-24 16:04:47 -0700203 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700204};
205
bsalomon6c471f72016-07-26 12:56:32 -0700206static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400207#ifdef SK_DAWN
208 if (0 == strcmp(str, "dawn")) {
209 return sk_app::Window::kDawn_BackendType;
210 } else
211#endif
bsalomon6c471f72016-07-26 12:56:32 -0700212#ifdef SK_VULKAN
213 if (0 == strcmp(str, "vk")) {
214 return sk_app::Window::kVulkan_BackendType;
215 } else
216#endif
Brian Salomon194db172017-08-17 14:37:06 -0400217#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
218 if (0 == strcmp(str, "angle")) {
219 return sk_app::Window::kANGLE_BackendType;
220 } else
221#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400222#ifdef SK_METAL
223 if (0 == strcmp(str, "mtl")) {
224 return sk_app::Window::kMetal_BackendType;
225 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500226#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400227#ifdef SK_DIRECT3D
228 if (0 == strcmp(str, "d3d")) {
229 return sk_app::Window::kDirect3D_BackendType;
230 } else
231#endif
232
bsalomon6c471f72016-07-26 12:56:32 -0700233 if (0 == strcmp(str, "gl")) {
234 return sk_app::Window::kNativeGL_BackendType;
235 } else if (0 == strcmp(str, "sw")) {
236 return sk_app::Window::kRaster_BackendType;
237 } else {
238 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
239 return sk_app::Window::kRaster_BackendType;
240 }
241}
242
Brian Osmana109e392017-02-24 09:49:14 -0500243static SkColorSpacePrimaries gSrgbPrimaries = {
244 0.64f, 0.33f,
245 0.30f, 0.60f,
246 0.15f, 0.06f,
247 0.3127f, 0.3290f };
248
249static SkColorSpacePrimaries gAdobePrimaries = {
250 0.64f, 0.33f,
251 0.21f, 0.71f,
252 0.15f, 0.06f,
253 0.3127f, 0.3290f };
254
255static SkColorSpacePrimaries gP3Primaries = {
256 0.680f, 0.320f,
257 0.265f, 0.690f,
258 0.150f, 0.060f,
259 0.3127f, 0.3290f };
260
261static SkColorSpacePrimaries gRec2020Primaries = {
262 0.708f, 0.292f,
263 0.170f, 0.797f,
264 0.131f, 0.046f,
265 0.3127f, 0.3290f };
266
267struct NamedPrimaries {
268 const char* fName;
269 SkColorSpacePrimaries* fPrimaries;
270} gNamedPrimaries[] = {
271 { "sRGB", &gSrgbPrimaries },
272 { "AdobeRGB", &gAdobePrimaries },
273 { "P3", &gP3Primaries },
274 { "Rec. 2020", &gRec2020Primaries },
275};
276
277static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
278 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
279}
280
Brian Osman70d2f432017-11-08 09:54:10 -0500281static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
282 // In raster mode, we still use GL for the window.
283 // This lets us render the GUI faster (and correct).
284 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
285}
286
Jim Van Verth74826c82019-03-01 14:37:30 -0500287class NullSlide : public Slide {
288 SkISize getDimensions() const override {
289 return SkISize::Make(640, 480);
290 }
291
292 void draw(SkCanvas* canvas) override {
293 canvas->clear(0xffff11ff);
294 }
295};
296
John Stiles31964fd2020-05-05 16:05:47 -0400297static const char kName[] = "name";
298static const char kValue[] = "value";
299static const char kOptions[] = "options";
300static const char kSlideStateName[] = "Slide";
301static const char kBackendStateName[] = "Backend";
302static const char kMSAAStateName[] = "MSAA";
303static const char kPathRendererStateName[] = "Path renderer";
304static const char kSoftkeyStateName[] = "Softkey";
305static const char kSoftkeyHint[] = "Please select a softkey";
306static const char kON[] = "ON";
307static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700308
Mike Reed862818b2020-03-21 15:07:13 -0400309extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500310extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500311extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400312
jvanverth34524262016-05-04 13:49:13 -0700313Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500314 : fCurrentSlide(-1)
315 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500316 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400317 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500318 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500319 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500320 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500321 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400322 , fZoomWindowFixed(false)
323 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500324 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400325 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700326 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500327 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500328 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500329 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500330 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500331 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700332 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400333 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400334 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400335 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500336 , fTiled(false)
337 , fDrawTileBoundaries(false)
338 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400339 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700340{
Greg Daniel285db442016-10-14 09:12:53 -0400341 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700342
Chris Dalton37ae4b02019-12-28 14:51:11 -0700343 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600344 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500345 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600346 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600347 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500348 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700349
jvanverth2bb3b6d2016-04-08 07:24:09 -0700350 SkDebugf("Command line arguments: ");
351 for (int i = 1; i < argc; ++i) {
352 SkDebugf("%s ", argv[i]);
353 }
354 SkDebugf("\n");
355
Mike Klein88544fb2019-03-20 10:50:33 -0500356 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500357#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400358 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500359#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700360
Mike Reed862818b2020-03-21 15:07:13 -0400361 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500362 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500363 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400364
Mike Klein19cc0f62019-03-22 15:30:07 -0500365 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500366
Brian Osmanbc8150f2017-07-24 11:38:01 -0400367 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400368 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400369
bsalomon6c471f72016-07-26 12:56:32 -0700370 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700371 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700372
csmartdalton578f0642017-02-24 16:04:47 -0700373 DisplayParams displayParams;
374 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500375 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700376 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400377 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400378 displayParams.fGrContextOptions.fShaderCacheStrategy =
John Stiles2ee4d7a2021-03-30 10:30:47 -0400379 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400380 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
381 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Dalton94df5722021-04-19 17:40:40 -0600382 if (FLAGS_dmsaa) {
383 displayParams.fSurfaceProps = SkSurfaceProps(
384 displayParams.fSurfaceProps.flags() | kDMSAA_SkSurfacePropsPrivateFlag,
385 displayParams.fSurfaceProps.pixelGeometry());
386 }
csmartdalton578f0642017-02-24 16:04:47 -0700387 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400388 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500389 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700390
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500391 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500392 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500393
Brian Osman56a24812017-12-19 11:15:16 -0500394 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500395 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500396 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
397 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
398 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
399
jvanverth9f372462016-04-06 06:08:59 -0700400 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700401 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500402 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500403 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500404 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700405
brianosman622c8d52016-05-10 06:50:49 -0700406 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500407 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
408 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
409 fWindow->inval();
410 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500411 // Command to jump directly to the slide picker and give it focus
412 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
413 this->fShowImGuiDebugWindow = true;
414 this->fShowSlidePicker = true;
415 fWindow->inval();
416 });
417 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400418 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500419 this->fShowImGuiDebugWindow = true;
420 this->fShowSlidePicker = true;
421 fWindow->inval();
422 });
Brian Osman79086b92017-02-10 13:36:16 -0500423 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
424 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
425 fWindow->inval();
426 });
Brian Osmanf6877092017-02-13 09:39:57 -0500427 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
428 this->fShowZoomWindow = !this->fShowZoomWindow;
429 fWindow->inval();
430 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400431 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
432 this->fZoomWindowFixed = !this->fZoomWindowFixed;
433 fWindow->inval();
434 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400435 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400436 DisplayParams params = fWindow->getRequestedDisplayParams();
437 params.fDisableVsync = !params.fDisableVsync;
438 fWindow->setRequestedDisplayParams(params);
439 this->updateTitle();
440 fWindow->inval();
441 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400442 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
443 DisplayParams params = fWindow->getRequestedDisplayParams();
444 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
445 fWindow->setRequestedDisplayParams(params);
446 this->updateTitle();
447 fWindow->inval();
448 });
Mike Reedf702ed42019-07-22 17:00:49 -0400449 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
450 fRefresh = !fRefresh;
451 fWindow->inval();
452 });
brianosman622c8d52016-05-10 06:50:49 -0700453 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500454 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700455 fWindow->inval();
456 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400457 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500458 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400459 this->updateTitle();
460 fWindow->inval();
461 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500462 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500463 switch (fColorMode) {
464 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500465 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500466 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500467 case ColorMode::kColorManaged8888:
468 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500469 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500470 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400471 this->setColorMode(ColorMode::kColorManagedF16Norm);
472 break;
473 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500474 this->setColorMode(ColorMode::kLegacy);
475 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500476 }
brianosman622c8d52016-05-10 06:50:49 -0700477 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700478 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
479 DisplayParams params = fWindow->getRequestedDisplayParams();
480 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
481 fWindow->setRequestedDisplayParams(params);
482 fWindow->inval();
483 });
Brian Salomon91216d52021-04-09 11:57:59 -0400484 fCommands.addCommand('w', "Modes", "Toggle reduced shaders", [this]() {
485 DisplayParams params = fWindow->getRequestedDisplayParams();
486 params.fGrContextOptions.fReducedShaderVariations =
487 !params.fGrContextOptions.fReducedShaderVariations;
488 fWindow->setRequestedDisplayParams(params);
489 fWindow->inval();
490 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400491 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500492 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700493 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400494 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500495 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700496 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400497 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700498 this->changeZoomLevel(1.f / 32.f);
499 fWindow->inval();
500 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400501 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700502 this->changeZoomLevel(-1.f / 32.f);
503 fWindow->inval();
504 });
jvanverthaf236b52016-05-20 06:01:06 -0700505 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400506 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
507 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500508 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400509#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
510 if (newBackend == sk_app::Window::kVulkan_BackendType) {
511 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
512 sk_app::Window::kBackendTypeCount);
513 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
514 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500515 }
516#endif
Brian Osman621491e2017-02-28 15:45:01 -0500517 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700518 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500519 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
520 fSaveToSKP = true;
521 fWindow->inval();
522 });
Mike Reed376d8122019-03-14 11:39:02 -0400523 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
524 fShowSlideDimensions = !fShowSlideDimensions;
525 fWindow->inval();
526 });
Ben Wagner37c54032018-04-13 14:30:23 -0400527 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
528 DisplayParams params = fWindow->getRequestedDisplayParams();
529 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400530 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
531 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
532 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400533 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
534 } else {
535 switch (params.fSurfaceProps.pixelGeometry()) {
536 case kUnknown_SkPixelGeometry:
537 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
538 break;
539 case kRGB_H_SkPixelGeometry:
540 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
541 break;
542 case kBGR_H_SkPixelGeometry:
543 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
544 break;
545 case kRGB_V_SkPixelGeometry:
546 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
547 break;
548 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400549 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
550 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400551 break;
552 }
553 }
554 fWindow->setRequestedDisplayParams(params);
555 this->updateTitle();
556 fWindow->inval();
557 });
Ben Wagner9613e452019-01-23 10:34:59 -0500558 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500559 if (!fFontOverrides.fHinting) {
560 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400561 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500562 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500563 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400564 case SkFontHinting::kNone:
565 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500566 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400567 case SkFontHinting::kSlight:
568 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500569 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400570 case SkFontHinting::kNormal:
571 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500572 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400573 case SkFontHinting::kFull:
574 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500575 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500576 break;
577 }
578 }
579 this->updateTitle();
580 fWindow->inval();
581 });
582 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500583 if (!fPaintOverrides.fAntiAlias) {
584 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
585 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500586 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500587 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500588 } else {
589 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500590 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500591 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500592 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400593 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500594 break;
595 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500596 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500597 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400598 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500599 break;
600 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500601 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400602 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500603 break;
604 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500605 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
606 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500607 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
608 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500609 break;
610 }
611 }
612 this->updateTitle();
613 fWindow->inval();
614 });
Ben Wagner37c54032018-04-13 14:30:23 -0400615 fCommands.addCommand('D', "Modes", "DFT", [this]() {
616 DisplayParams params = fWindow->getRequestedDisplayParams();
617 uint32_t flags = params.fSurfaceProps.flags();
618 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
619 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
620 fWindow->setRequestedDisplayParams(params);
621 this->updateTitle();
622 fWindow->inval();
623 });
Ben Wagner9613e452019-01-23 10:34:59 -0500624 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500625 if (!fFontOverrides.fEdging) {
626 fFontOverrides.fEdging = true;
627 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500628 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500629 switch (fFont.getEdging()) {
630 case SkFont::Edging::kAlias:
631 fFont.setEdging(SkFont::Edging::kAntiAlias);
632 break;
633 case SkFont::Edging::kAntiAlias:
634 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
635 break;
636 case SkFont::Edging::kSubpixelAntiAlias:
637 fFont.setEdging(SkFont::Edging::kAlias);
638 fFontOverrides.fEdging = false;
639 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500640 }
641 }
642 this->updateTitle();
643 fWindow->inval();
644 });
Ben Wagner9613e452019-01-23 10:34:59 -0500645 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500646 if (!fFontOverrides.fSubpixel) {
647 fFontOverrides.fSubpixel = true;
648 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500649 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500650 if (!fFont.isSubpixel()) {
651 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500652 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500653 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500654 }
655 }
656 this->updateTitle();
657 fWindow->inval();
658 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400659 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
660 if (!fFontOverrides.fBaselineSnap) {
661 fFontOverrides.fBaselineSnap = true;
662 fFont.setBaselineSnap(false);
663 } else {
664 if (!fFont.isBaselineSnap()) {
665 fFont.setBaselineSnap(true);
666 } else {
667 fFontOverrides.fBaselineSnap = false;
668 }
669 }
670 this->updateTitle();
671 fWindow->inval();
672 });
Brian Osman805a7272018-05-02 15:40:20 -0400673 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
674 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
675 : kPerspective_Real;
676 this->updateTitle();
677 fWindow->inval();
678 });
679 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
680 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
681 : kPerspective_Off;
682 this->updateTitle();
683 fWindow->inval();
684 });
Brian Osman207d4102019-01-10 09:40:58 -0500685 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
686 fAnimTimer.togglePauseResume();
687 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400688 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
689 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500690 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400691 fWindow->inval();
692 });
Mike Reed59295352020-03-12 13:56:34 -0400693 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
694 fDrawViaSerialize = !fDrawViaSerialize;
695 this->updateTitle();
696 fWindow->inval();
697 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500698 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400699 gUseSkVMBlitter = !gUseSkVMBlitter;
700 this->updateTitle();
701 fWindow->inval();
702 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500703 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
704 gSkVMAllowJIT = !gSkVMAllowJIT;
705 this->updateTitle();
706 fWindow->inval();
707 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500708
jvanverth2bb3b6d2016-04-08 07:24:09 -0700709 // set up slides
710 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500711 if (FLAGS_list) {
712 this->listNames();
713 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700714
Brian Osman9bb47cf2018-04-26 15:55:00 -0400715 fPerspectivePoints[0].set(0, 0);
716 fPerspectivePoints[1].set(1, 0);
717 fPerspectivePoints[2].set(0, 1);
718 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700719 fAnimTimer.run();
720
Hal Canaryc465d132017-12-08 10:21:31 -0500721 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500722 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500723 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500724 }
725 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500726
jongdeok.kim804f17e2019-02-26 14:39:23 +0900727 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500728 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700729}
730
jvanverth34524262016-05-04 13:49:13 -0700731void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400732 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
733 static const struct {
734 const char* fExtension;
735 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500736 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400737 const SlideFactory fFactory;
738 } gExternalSlidesInfo[] = {
Brian Salomon06c9e292021-04-29 14:10:23 -0400739 { ".mskp", "mskp-dir", FLAGS_mskps,
740 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
741 return sk_make_sp<MSKPSlide>(name, path);}
742 },
Florin Malita0ffa3222018-04-05 14:34:45 -0400743 { ".skp", "skp-dir", FLAGS_skps,
744 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
745 return sk_make_sp<SKPSlide>(name, path);}
746 },
747 { ".jpg", "jpg-dir", FLAGS_jpgs,
748 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
749 return sk_make_sp<ImageSlide>(name, path);}
750 },
Florin Malita87ccf332018-05-04 12:23:24 -0400751#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400752 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400753 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
754 return sk_make_sp<SkottieSlide>(name, path);}
755 },
Florin Malita87ccf332018-05-04 12:23:24 -0400756#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400757 #if defined(SK_ENABLE_SKRIVE)
758 { ".flr", "skrive-dir", FLAGS_rives,
759 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
760 return sk_make_sp<SkRiveSlide>(name, path);}
761 },
762 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400763#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400764 { ".svg", "svg-dir", FLAGS_svgs,
765 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
766 return sk_make_sp<SvgSlide>(name, path);}
767 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400768#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400769 };
jvanverthc265a922016-04-08 12:51:45 -0700770
Brian Salomon343553a2018-09-05 15:41:23 -0400771 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700772
Mike Klein88544fb2019-03-20 10:50:33 -0500773 const auto addSlide =
774 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
775 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
776 return;
777 }
liyuqian6f163d22016-06-13 12:26:45 -0700778
Mike Klein88544fb2019-03-20 10:50:33 -0500779 if (auto slide = fact(name, path)) {
780 dirSlides.push_back(slide);
781 fSlides.push_back(std::move(slide));
782 }
783 };
Florin Malita76a076b2018-02-15 18:40:48 -0500784
Florin Malita38792ce2018-05-08 10:36:18 -0400785 if (!FLAGS_file.isEmpty()) {
786 // single file mode
787 const SkString file(FLAGS_file[0]);
788
789 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
790 for (const auto& sinfo : gExternalSlidesInfo) {
791 if (file.endsWith(sinfo.fExtension)) {
792 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
793 return;
794 }
795 }
796
797 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
798 } else {
799 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
800 }
801
802 return;
803 }
804
805 // Bisect slide.
806 if (!FLAGS_bisect.isEmpty()) {
807 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500808 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400809 if (FLAGS_bisect.count() >= 2) {
810 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
811 bisect->onChar(*ch);
812 }
813 }
814 fSlides.push_back(std::move(bisect));
815 }
816 }
817
818 // GMs
819 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400820 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400821 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500822 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400823 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400824 fSlides.push_back(std::move(slide));
825 }
Florin Malita38792ce2018-05-08 10:36:18 -0400826 }
827 // reverse gms
828 int numGMs = fSlides.count() - firstGM;
829 for (int i = 0; i < numGMs/2; ++i) {
830 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
831 }
832
833 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400834 for (const SampleFactory factory : SampleRegistry::Range()) {
835 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500836 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400837 fSlides.push_back(slide);
838 }
Florin Malita38792ce2018-05-08 10:36:18 -0400839 }
840
Brian Osman7c979f52019-02-12 13:27:51 -0500841 // Particle demo
842 {
843 // TODO: Convert this to a sample
844 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500845 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500846 fSlides.push_back(std::move(slide));
847 }
848 }
849
Brian Osmand927bd22019-12-18 11:23:12 -0500850 // Runtime shader editor
851 {
852 sk_sp<Slide> slide(new SkSLSlide());
853 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
854 fSlides.push_back(std::move(slide));
855 }
856 }
857
Florin Malita0ffa3222018-04-05 14:34:45 -0400858 for (const auto& info : gExternalSlidesInfo) {
859 for (const auto& flag : info.fFlags) {
860 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
861 // single file
862 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
863 } else {
864 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400865 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400866 SkTArray<SkString> sortedFilenames;
867 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400868 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400869 sortedFilenames.push_back(name);
870 }
871 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400872 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000873 [](const SkString& a, const SkString& b) {
874 return strcmp(a.c_str(), b.c_str()) < 0;
875 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400876 }
877 for (const SkString& filename : sortedFilenames) {
878 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
879 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400880 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400881 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400882 if (!dirSlides.empty()) {
883 fSlides.push_back(
884 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
885 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500886 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400887 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400888 }
889 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500890
891 if (!fSlides.count()) {
892 sk_sp<Slide> slide(new NullSlide());
893 fSlides.push_back(std::move(slide));
894 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700895}
896
897
jvanverth34524262016-05-04 13:49:13 -0700898Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400899 for(auto& slide : fSlides) {
900 slide->gpuTeardown();
901 }
902
jvanverth9f372462016-04-06 06:08:59 -0700903 fWindow->detach();
904 delete fWindow;
905}
906
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500907struct SkPaintTitleUpdater {
908 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
909 void append(const char* s) {
910 if (fCount == 0) {
911 fTitle->append(" {");
912 } else {
913 fTitle->append(", ");
914 }
915 fTitle->append(s);
916 ++fCount;
917 }
918 void done() {
919 if (fCount > 0) {
920 fTitle->append("}");
921 }
922 }
923 SkString* fTitle;
924 int fCount;
925};
926
brianosman05de2162016-05-06 13:28:57 -0700927void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700928 if (!fWindow) {
929 return;
930 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500931 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700932 return; // Surface hasn't been created yet.
933 }
934
jvanverth34524262016-05-04 13:49:13 -0700935 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700936 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700937
Mike Kleine5acd752019-03-22 09:57:16 -0500938 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400939 if (gSkForceAnalyticAA) {
940 title.append(" <FAAA>");
941 } else {
942 title.append(" <AAA>");
943 }
944 }
Mike Reed59295352020-03-12 13:56:34 -0400945 if (fDrawViaSerialize) {
946 title.append(" <serialize>");
947 }
Mike Reed862818b2020-03-21 15:07:13 -0400948 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500949 title.append(" <SkVMBlitter>");
950 }
951 if (!gSkVMAllowJIT) {
952 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400953 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400954
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500955 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500956 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
957 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400958 const char* on, const char* off)
959 {
Ben Wagner9613e452019-01-23 10:34:59 -0500960 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400961 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500962 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400963 };
964
Ben Wagner9613e452019-01-23 10:34:59 -0500965 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
966 const char* on, const char* off)
967 {
968 if (fFontOverrides.*flag) {
969 paintTitle.append((fFont.*isFlag)() ? on : off);
970 }
971 };
972
973 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
974 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
975
976 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
977 "Force Autohint", "No Force Autohint");
978 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400979 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500980 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
981 "Linear Metrics", "Non-Linear Metrics");
982 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
983 "Bitmap Text", "No Bitmap Text");
984 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
985
986 if (fFontOverrides.fEdging) {
987 switch (fFont.getEdging()) {
988 case SkFont::Edging::kAlias:
989 paintTitle.append("Alias Text");
990 break;
991 case SkFont::Edging::kAntiAlias:
992 paintTitle.append("Antialias Text");
993 break;
994 case SkFont::Edging::kSubpixelAntiAlias:
995 paintTitle.append("Subpixel Antialias Text");
996 break;
997 }
998 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400999
Mike Reed3ae47332019-01-04 10:11:46 -05001000 if (fFontOverrides.fHinting) {
1001 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -04001002 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001003 paintTitle.append("No Hinting");
1004 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001005 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001006 paintTitle.append("Slight Hinting");
1007 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001008 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001009 paintTitle.append("Normal Hinting");
1010 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001011 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001012 paintTitle.append("Full Hinting");
1013 break;
1014 }
1015 }
1016 paintTitle.done();
1017
Brian Osman92004802017-03-06 11:47:26 -05001018 switch (fColorMode) {
1019 case ColorMode::kLegacy:
1020 title.append(" Legacy 8888");
1021 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001022 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001023 title.append(" ColorManaged 8888");
1024 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001025 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001026 title.append(" ColorManaged F16");
1027 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001028 case ColorMode::kColorManagedF16Norm:
1029 title.append(" ColorManaged F16 Norm");
1030 break;
Brian Osman92004802017-03-06 11:47:26 -05001031 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001032
Brian Osman92004802017-03-06 11:47:26 -05001033 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001034 int curPrimaries = -1;
1035 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1036 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1037 curPrimaries = i;
1038 break;
1039 }
1040 }
Brian Osman03115dc2018-11-26 13:55:19 -05001041 title.appendf(" %s Gamma %f",
1042 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001043 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001044 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001045
Ben Wagner37c54032018-04-13 14:30:23 -04001046 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001047 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001048 switch (params.fSurfaceProps.pixelGeometry()) {
1049 case kUnknown_SkPixelGeometry:
1050 title.append( " Flat");
1051 break;
1052 case kRGB_H_SkPixelGeometry:
1053 title.append( " RGB");
1054 break;
1055 case kBGR_H_SkPixelGeometry:
1056 title.append( " BGR");
1057 break;
1058 case kRGB_V_SkPixelGeometry:
1059 title.append( " RGBV");
1060 break;
1061 case kBGR_V_SkPixelGeometry:
1062 title.append( " BGRV");
1063 break;
1064 }
1065 }
1066
1067 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1068 title.append(" DFT");
1069 }
1070
csmartdalton578f0642017-02-24 16:04:47 -07001071 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001072 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001073 int msaa = fWindow->sampleCount();
1074 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001075 title.appendf(" MSAA: %i", msaa);
1076 }
1077 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001078
1079 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001080 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001081 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1082 }
1083
Brian Osman805a7272018-05-02 15:40:20 -04001084 if (kPerspective_Real == fPerspectiveMode) {
1085 title.append(" Perpsective (Real)");
1086 } else if (kPerspective_Fake == fPerspectiveMode) {
1087 title.append(" Perspective (Fake)");
1088 }
1089
brianosman05de2162016-05-06 13:28:57 -07001090 fWindow->setTitle(title.c_str());
1091}
1092
Florin Malitaab99c342018-01-16 16:23:03 -05001093int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001094
1095 if (!FLAGS_slide.isEmpty()) {
1096 int count = fSlides.count();
1097 for (int i = 0; i < count; i++) {
1098 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001099 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001100 }
1101 }
1102
1103 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1104 this->listNames();
1105 }
1106
Florin Malitaab99c342018-01-16 16:23:03 -05001107 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001108}
1109
Florin Malitaab99c342018-01-16 16:23:03 -05001110void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001111 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001112 for (const auto& slide : fSlides) {
1113 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001114 }
1115}
1116
Florin Malitaab99c342018-01-16 16:23:03 -05001117void Viewer::setCurrentSlide(int slide) {
1118 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001119
Florin Malitaab99c342018-01-16 16:23:03 -05001120 if (slide == fCurrentSlide) {
1121 return;
1122 }
1123
1124 if (fCurrentSlide >= 0) {
1125 fSlides[fCurrentSlide]->unload();
1126 }
1127
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001128 SkScalar scaleFactor = 1.0;
1129 if (fApplyBackingScale) {
1130 scaleFactor = fWindow->scaleFactor();
1131 }
1132 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1133 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001134 fCurrentSlide = slide;
1135 this->setupCurrentSlide();
1136}
1137
1138void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001139 if (fCurrentSlide >= 0) {
1140 // prepare dimensions for image slides
1141 fGesture.resetTouchState();
1142 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001143
Jim Van Verth0848fb02018-01-22 13:39:30 -05001144 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1145 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1146 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001147
Jim Van Verth0848fb02018-01-22 13:39:30 -05001148 // Start with a matrix that scales the slide to the available screen space
1149 if (fWindow->scaleContentToFit()) {
1150 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001151 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1152 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001153 }
liyuqiane46e4f02016-05-20 07:32:19 -07001154 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001155
1156 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001157 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001158
1159 this->updateTitle();
1160 this->updateUIState();
1161
1162 fStatsLayer.resetMeasurements();
1163
1164 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001165 }
jvanverthc265a922016-04-08 12:51:45 -07001166}
1167
Brian Osmanaba642c2020-02-06 12:52:25 -05001168#define MAX_ZOOM_LEVEL 8.0f
1169#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001170
jvanverth34524262016-05-04 13:49:13 -07001171void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001172 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001173 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001174 this->preTouchMatrixChanged();
1175}
Yuqian Li755778c2018-03-28 16:23:31 -04001176
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001177void Viewer::preTouchMatrixChanged() {
1178 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001179 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1180 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1181 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1182 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1183}
1184
Brian Osman805a7272018-05-02 15:40:20 -04001185SkMatrix Viewer::computePerspectiveMatrix() {
1186 SkScalar w = fWindow->width(), h = fWindow->height();
1187 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1188 SkPoint perspPts[4] = {
1189 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1190 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1191 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1192 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1193 };
1194 SkMatrix m;
1195 m.setPolyToPoly(orthoPts, perspPts, 4);
1196 return m;
1197}
1198
Yuqian Li755778c2018-03-28 16:23:31 -04001199SkMatrix Viewer::computePreTouchMatrix() {
1200 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001201
1202 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001203 if (fApplyBackingScale) {
1204 zoomScale *= fWindow->scaleFactor();
1205 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001206 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001207 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001208
1209 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1210 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001211
Brian Osman805a7272018-05-02 15:40:20 -04001212 if (kPerspective_Real == fPerspectiveMode) {
1213 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001214 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001215 }
1216
Yuqian Li755778c2018-03-28 16:23:31 -04001217 return m;
jvanverthc265a922016-04-08 12:51:45 -07001218}
1219
liyuqiand3cdbca2016-05-17 12:44:20 -07001220SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001221 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001222 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001223 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001224 return m;
jvanverthc265a922016-04-08 12:51:45 -07001225}
1226
Brian Osman621491e2017-02-28 15:45:01 -05001227void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001228 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001229 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001230 fBackendType = backendType;
1231
Robert Phillipse9229532020-06-26 10:10:49 -04001232 // The active context is going away in 'detach'
1233 for(auto& slide : fSlides) {
1234 slide->gpuTeardown();
1235 }
1236
Brian Osman621491e2017-02-28 15:45:01 -05001237 fWindow->detach();
1238
Brian Osman70d2f432017-11-08 09:54:10 -05001239#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001240 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1241 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001242 DisplayParams params = fWindow->getRequestedDisplayParams();
1243 delete fWindow;
1244 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001245
Brian Osman70d2f432017-11-08 09:54:10 -05001246 // re-register callbacks
1247 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001248 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001249 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001250 fWindow->pushLayer(&fImGuiLayer);
1251
Brian Osman70d2f432017-11-08 09:54:10 -05001252 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1253 // will still include our correct sample count. But the re-created fWindow will lose that
1254 // information. On Windows, we need to re-create the window when changing sample count,
1255 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1256 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1257 // as if we had never changed windows at all).
1258 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001259#endif
1260
Brian Osman70d2f432017-11-08 09:54:10 -05001261 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001262}
1263
Brian Osman92004802017-03-06 11:47:26 -05001264void Viewer::setColorMode(ColorMode colorMode) {
1265 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001266 this->updateTitle();
1267 fWindow->inval();
1268}
1269
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001270class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1271public:
Mike Reed3ae47332019-01-04 10:11:46 -05001272 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1273 SkFont* font, Viewer::SkFontFields* ffields)
1274 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001275 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001276 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1277 sk_sp<SkTextBlob>* cache) {
1278 bool blobWillChange = false;
1279 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001280 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1281 bool shouldDraw = this->filterFont(&filteredFont);
1282 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001283 blobWillChange = true;
1284 break;
1285 }
1286 }
1287 if (!blobWillChange) {
1288 return blob;
1289 }
1290
1291 SkTextBlobBuilder builder;
1292 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001293 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1294 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001295 if (!shouldDraw) {
1296 continue;
1297 }
1298
Mike Reed3ae47332019-01-04 10:11:46 -05001299 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001300
Ben Wagner41e40472018-09-24 13:01:54 -04001301 const SkTextBlobBuilder::RunBuffer& runBuffer
1302 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001303 ? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(),
1304 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001305 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001306 ? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(),
1307 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001308 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001309 ? builder.allocRunTextPos(font, it.glyphCount(), it.textSize())
Ben Wagnere5736262021-02-08 16:52:08 -05001310 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001311 ? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001312 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1313 uint32_t glyphCount = it.glyphCount();
1314 if (it.glyphs()) {
1315 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1316 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1317 }
1318 if (it.pos()) {
1319 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001320 unsigned posPerGlyph = it.scalarsPerGlyph();
1321 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001322 }
1323 if (it.text()) {
1324 size_t textSize = sizeof(decltype(*it.text()));
1325 uint32_t textCount = it.textSize();
1326 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1327 }
1328 if (it.clusters()) {
1329 size_t clusterSize = sizeof(decltype(*it.clusters()));
1330 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1331 }
1332 }
1333 *cache = builder.make();
1334 return cache->get();
1335 }
1336 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1337 const SkPaint& paint) override {
1338 sk_sp<SkTextBlob> cache;
1339 this->SkPaintFilterCanvas::onDrawTextBlob(
1340 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1341 }
Mike Reed3ae47332019-01-04 10:11:46 -05001342 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001343 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001344 font->writable()->setSize(fFont->getSize());
1345 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001346 if (fFontOverrides->fScaleX) {
1347 font->writable()->setScaleX(fFont->getScaleX());
1348 }
1349 if (fFontOverrides->fSkewX) {
1350 font->writable()->setSkewX(fFont->getSkewX());
1351 }
Mike Reed3ae47332019-01-04 10:11:46 -05001352 if (fFontOverrides->fHinting) {
1353 font->writable()->setHinting(fFont->getHinting());
1354 }
Ben Wagner9613e452019-01-23 10:34:59 -05001355 if (fFontOverrides->fEdging) {
1356 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001357 }
Ben Wagner9613e452019-01-23 10:34:59 -05001358 if (fFontOverrides->fEmbolden) {
1359 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001360 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001361 if (fFontOverrides->fBaselineSnap) {
1362 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1363 }
Ben Wagner9613e452019-01-23 10:34:59 -05001364 if (fFontOverrides->fLinearMetrics) {
1365 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001366 }
Ben Wagner9613e452019-01-23 10:34:59 -05001367 if (fFontOverrides->fSubpixel) {
1368 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001369 }
Ben Wagner9613e452019-01-23 10:34:59 -05001370 if (fFontOverrides->fEmbeddedBitmaps) {
1371 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001372 }
Ben Wagner9613e452019-01-23 10:34:59 -05001373 if (fFontOverrides->fForceAutoHinting) {
1374 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001375 }
Ben Wagner9613e452019-01-23 10:34:59 -05001376
Mike Reed3ae47332019-01-04 10:11:46 -05001377 return true;
1378 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001379 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001380 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001381 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001382 }
Ben Wagner9613e452019-01-23 10:34:59 -05001383 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001384 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001385 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001386 if (fPaintOverrides->fStyle) {
1387 paint.setStyle(fPaint->getStyle());
1388 }
1389 if (fPaintOverrides->fWidth) {
1390 paint.setStrokeWidth(fPaint->getStrokeWidth());
1391 }
1392 if (fPaintOverrides->fMiterLimit) {
1393 paint.setStrokeMiter(fPaint->getStrokeMiter());
1394 }
1395 if (fPaintOverrides->fCapType) {
1396 paint.setStrokeCap(fPaint->getStrokeCap());
1397 }
1398 if (fPaintOverrides->fJoinType) {
1399 paint.setStrokeJoin(fPaint->getStrokeJoin());
1400 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001401 return true;
1402 }
1403 SkPaint* fPaint;
1404 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001405 SkFont* fFont;
1406 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001407};
1408
Robert Phillips9882dae2019-03-04 11:00:10 -05001409void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001410 if (fCurrentSlide < 0) {
1411 return;
1412 }
1413
Robert Phillips9882dae2019-03-04 11:00:10 -05001414 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001415
Brian Osmanf750fbc2017-02-08 10:47:28 -05001416 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001417 SkSurface* slideSurface = surface;
1418 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001419 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001420
Brian Osmane0d4fba2017-03-15 10:24:55 -04001421 // 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 -05001422 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001423 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001424 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001425 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001426 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001427 }
1428
Brian Osman3ac99cf2017-12-01 11:23:53 -05001429 if (fSaveToSKP) {
1430 SkPictureRecorder recorder;
1431 SkCanvas* recorderCanvas = recorder.beginRecording(
1432 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001433 fSlides[fCurrentSlide]->draw(recorderCanvas);
1434 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1435 SkFILEWStream stream("sample_app.skp");
1436 picture->serialize(&stream);
1437 fSaveToSKP = false;
1438 }
1439
Brian Osmane9ed0f02018-11-26 14:50:05 -05001440 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001441 SkColorType colorType;
1442 switch (fColorMode) {
1443 case ColorMode::kLegacy:
1444 case ColorMode::kColorManaged8888:
1445 colorType = kN32_SkColorType;
1446 break;
1447 case ColorMode::kColorManagedF16:
1448 colorType = kRGBA_F16_SkColorType;
1449 break;
1450 case ColorMode::kColorManagedF16Norm:
1451 colorType = kRGBA_F16Norm_SkColorType;
1452 break;
1453 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001454
1455 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001456 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001457 slideCanvas->getProps(&props);
1458
Brian Osmane9ed0f02018-11-26 14:50:05 -05001459 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1460 return Window::kRaster_BackendType == this->fBackendType
1461 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001462 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001463 };
1464
Brian Osman03115dc2018-11-26 13:55:19 -05001465 // We need to render offscreen if we're...
1466 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1467 // ... in any raster mode, because the window surface is actually GL
1468 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001469 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001470 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001471 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001472 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001473 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001474 colorSpace != nullptr ||
1475 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001476
Brian Osmane9ed0f02018-11-26 14:50:05 -05001477 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001478 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001479 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001480 }
1481
Mike Reed59295352020-03-12 13:56:34 -04001482 SkPictureRecorder recorder;
1483 SkCanvas* recorderRestoreCanvas = nullptr;
1484 if (fDrawViaSerialize) {
1485 recorderRestoreCanvas = slideCanvas;
1486 slideCanvas = recorder.beginRecording(
1487 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1488 }
1489
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001490 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001491 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001492 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001493 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001494 if (fTiled) {
1495 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1496 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001497 for (int y = 0; y < fWindow->height(); y += tileH) {
1498 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001499 SkAutoCanvasRestore acr(slideCanvas, true);
1500 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1501 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001502 }
1503 }
1504
1505 // Draw borders between tiles
1506 if (fDrawTileBoundaries) {
1507 SkPaint border;
1508 border.setColor(0x60FF00FF);
1509 border.setStyle(SkPaint::kStroke_Style);
1510 for (int y = 0; y < fWindow->height(); y += tileH) {
1511 for (int x = 0; x < fWindow->width(); x += tileW) {
1512 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1513 }
1514 }
1515 }
1516 } else {
1517 slideCanvas->concat(this->computeMatrix());
1518 if (kPerspective_Real == fPerspectiveMode) {
1519 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1520 }
Mike Reed3ae47332019-01-04 10:11:46 -05001521 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001522 fSlides[fCurrentSlide]->draw(&filterCanvas);
1523 }
Brian Osman56a24812017-12-19 11:15:16 -05001524 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001525 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001526
Mike Reed59295352020-03-12 13:56:34 -04001527 if (recorderRestoreCanvas) {
1528 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1529 auto data = picture->serialize();
1530 slideCanvas = recorderRestoreCanvas;
1531 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1532 }
1533
Brian Osman1df161a2017-02-09 12:10:20 -05001534 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001535 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001536 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001537 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001538
1539 // If we rendered offscreen, snap an image and push the results to the window's canvas
1540 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001541 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001542
Robert Phillips9882dae2019-03-04 11:00:10 -05001543 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001544 SkPaint paint;
1545 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001546 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001547 int prePerspectiveCount = canvas->save();
1548 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001549 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001550 canvas->clear(SK_ColorWHITE);
1551 canvas->concat(this->computePerspectiveMatrix());
1552 }
Mike Reedb339d052021-01-28 11:20:41 -05001553 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001554 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001555 }
Mike Reed376d8122019-03-14 11:39:02 -04001556
1557 if (fShowSlideDimensions) {
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001558 SkCanvas* canvas = surface->getCanvas();
1559 SkAutoCanvasRestore acr(canvas, true);
1560 canvas->concat(this->computeMatrix());
Mike Reed376d8122019-03-14 11:39:02 -04001561 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1562 SkPaint paint;
1563 paint.setColor(0x40FFFF00);
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001564 canvas->drawRect(r, paint);
Mike Reed376d8122019-03-14 11:39:02 -04001565 }
liyuqian6f163d22016-06-13 12:26:45 -07001566}
1567
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001568void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001569 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001570 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001571}
Jim Van Verth6f449692017-02-14 15:16:46 -05001572
Robert Phillips9882dae2019-03-04 11:00:10 -05001573void Viewer::onPaint(SkSurface* surface) {
1574 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001575
Robert Phillips9882dae2019-03-04 11:00:10 -05001576 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001577
Brian Osmand67e5182017-12-08 16:46:09 -05001578 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001579
Greg Daniel427d8eb2020-09-28 15:04:18 -04001580 fLastImage.reset();
1581
Robert Phillipsed653392020-07-10 13:55:21 -04001582 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001583 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001584 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001585 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001586}
1587
Ben Wagnera1915972018-08-09 15:06:19 -04001588void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001589 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001590 SkScalar scaleFactor = 1.0;
1591 if (fApplyBackingScale) {
1592 scaleFactor = fWindow->scaleFactor();
1593 }
1594 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001595 }
Ben Wagnera1915972018-08-09 15:06:19 -04001596}
1597
Florin Malitacefc1b92018-02-19 21:43:47 -05001598SkPoint Viewer::mapEvent(float x, float y) {
1599 const auto m = this->computeMatrix();
1600 SkMatrix inv;
1601
1602 SkAssertResult(m.invert(&inv));
1603
1604 return inv.mapXY(x, y);
1605}
1606
Hal Canaryb1f411a2019-08-29 10:39:22 -04001607bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001608 if (GestureDevice::kMouse == fGestureDevice) {
1609 return false;
1610 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001611
1612 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001613 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001614 fWindow->inval();
1615 return true;
1616 }
1617
liyuqiand3cdbca2016-05-17 12:44:20 -07001618 void* castedOwner = reinterpret_cast<void*>(owner);
1619 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001620 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001621 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001622#if defined(SK_BUILD_FOR_IOS)
1623 // TODO: move IOS swipe detection higher up into the platform code
1624 SkPoint dir;
1625 if (fGesture.isFling(&dir)) {
1626 // swiping left or right
1627 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1628 if (dir.fX < 0) {
1629 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1630 fCurrentSlide + 1 : 0);
1631 } else {
1632 this->setCurrentSlide(fCurrentSlide > 0 ?
1633 fCurrentSlide - 1 : fSlides.count() - 1);
1634 }
1635 }
1636 fGesture.reset();
1637 }
1638#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001639 break;
1640 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001641 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001642 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001643 break;
1644 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001645 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001646 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001647 break;
1648 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001649 default: {
1650 // kLeft and kRight are only for swipes
1651 SkASSERT(false);
1652 break;
1653 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001654 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001655 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001656 fWindow->inval();
1657 return true;
1658}
1659
Hal Canaryb1f411a2019-08-29 10:39:22 -04001660bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001661 if (GestureDevice::kTouch == fGestureDevice) {
1662 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001663 }
Brian Osman16c81a12017-12-20 11:58:34 -05001664
Florin Malitacefc1b92018-02-19 21:43:47 -05001665 const auto slidePt = this->mapEvent(x, y);
1666 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1667 fWindow->inval();
1668 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001669 }
1670
1671 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001672 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001673 fGesture.touchEnd(nullptr);
1674 break;
1675 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001676 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001677 fGesture.touchBegin(nullptr, x, y);
1678 break;
1679 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001680 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001681 fGesture.touchMoved(nullptr, x, y);
1682 break;
1683 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001684 default: {
1685 SkASSERT(false); // shouldn't see kRight or kLeft here
1686 break;
1687 }
Brian Osman16c81a12017-12-20 11:58:34 -05001688 }
1689 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1690
Hal Canaryb1f411a2019-08-29 10:39:22 -04001691 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001692 fWindow->inval();
1693 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001694 return true;
1695}
1696
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001697bool Viewer::onFling(skui::InputState state) {
1698 if (skui::InputState::kRight == state) {
1699 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1700 return true;
1701 } else if (skui::InputState::kLeft == state) {
1702 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1703 return true;
1704 }
1705 return false;
1706}
1707
1708bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1709 switch (state) {
1710 case skui::InputState::kDown:
1711 fGesture.startZoom();
1712 return true;
1713 break;
1714 case skui::InputState::kMove:
1715 fGesture.updateZoom(scale, x, y, x, y);
1716 return true;
1717 break;
1718 case skui::InputState::kUp:
1719 fGesture.endZoom();
1720 return true;
1721 break;
1722 default:
1723 SkASSERT(false);
1724 break;
1725 }
1726
1727 return false;
1728}
1729
Brian Osmana109e392017-02-24 09:49:14 -05001730static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001731 // The gamut image covers a (0.8 x 0.9) shaped region
1732 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001733
1734 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1735 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1736 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001737 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1738 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1739 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001740
Brian Osman535c5e32019-02-09 16:32:58 -05001741 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1742 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1743 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1744 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1745 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001746}
1747
Ben Wagner3627d2e2018-06-26 14:23:20 -04001748static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001749 ImGui::DragCanvas dc(pt);
1750 dc.fillColor(IM_COL32(0, 0, 0, 128));
1751 dc.dragPoint(pt);
1752 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001753}
1754
Brian Osman9bb47cf2018-04-26 15:55:00 -04001755static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001756 ImGui::DragCanvas dc(pts);
1757 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001758
Brian Osman535c5e32019-02-09 16:32:58 -05001759 for (int i = 0; i < 4; ++i) {
1760 dc.dragPoint(pts + i);
1761 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001762
Brian Osman535c5e32019-02-09 16:32:58 -05001763 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1764 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1765 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1766 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001767
Brian Osman535c5e32019-02-09 16:32:58 -05001768 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001769}
1770
John Stiles38b7d2f2020-06-24 12:13:31 -04001771static SkSL::String build_sksl_highlight_shader() {
1772 return SkSL::String("out half4 sk_FragColor;\n"
1773 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1774}
1775
1776static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1777 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1778 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1779 size_t pos = inShader.rfind("return *_out;\n");
1780 if (pos == std::string::npos) {
1781 return inShader;
1782 }
1783
1784 SkSL::String replacementShader = inShader;
1785 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1786 return replacementShader;
1787}
1788
1789static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1790 const char* versionDecl = shaderCaps.versionDeclString();
1791 SkSL::String highlight = versionDecl ? versionDecl : "";
1792 if (shaderCaps.usesPrecisionModifiers()) {
1793 highlight.append("precision mediump float;\n");
1794 }
1795 highlight.appendf("out vec4 sk_FragColor;\n"
1796 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1797 return highlight;
1798}
1799
Brian Osmand67e5182017-12-08 16:46:09 -05001800void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001801 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1802 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001803 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001804 }
1805
1806 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001807 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1808 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001809 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001810 DisplayParams params = fWindow->getRequestedDisplayParams();
1811 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001812 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001813
Brian Osmana109e392017-02-24 09:49:14 -05001814 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1815 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001816 if (ImGui::CollapsingHeader("Backend")) {
1817 int newBackend = static_cast<int>(fBackendType);
1818 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1819 ImGui::SameLine();
1820 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001821#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1822 ImGui::SameLine();
1823 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1824#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001825#if defined(SK_DAWN)
1826 ImGui::SameLine();
1827 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1828#endif
John Stilesf7da9232020-11-19 19:58:14 -05001829#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001830 ImGui::SameLine();
1831 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1832#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001833#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001834 ImGui::SameLine();
1835 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1836#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001837#if defined(SK_DIRECT3D)
1838 ImGui::SameLine();
1839 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1840#endif
Brian Osman621491e2017-02-28 15:45:01 -05001841 if (newBackend != fBackendType) {
1842 fDeferredActions.push_back([=]() {
1843 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1844 });
1845 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001846
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001847 bool* wire = &params.fGrContextOptions.fWireframeMode;
1848 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1849 paramsChanged = true;
1850 }
Brian Salomon99a33902017-03-07 15:16:34 -05001851
Brian Salomon91216d52021-04-09 11:57:59 -04001852 bool* reducedShaders = &params.fGrContextOptions.fReducedShaderVariations;
1853 if (ctx && ImGui::Checkbox("Reduced shaders", reducedShaders)) {
1854 paramsChanged = true;
1855 }
1856
Brian Osman28b12522017-03-08 17:10:24 -05001857 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001858 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001859 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001860 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1861 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1862 1;
1863
1864 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1865 if (maxMSAA >= 4) {
1866 ImGui::Text("MSAA: ");
1867
1868 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1869 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1870 // don't show it in the list.
1871 if (curMSAA == 2) {
1872 continue;
1873 }
1874 ImGui::SameLine();
1875 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1876 &sampleCount, curMSAA);
1877 }
1878 }
Brian Osman28b12522017-03-08 17:10:24 -05001879
1880 if (sampleCount != params.fMSAASampleCount) {
1881 params.fMSAASampleCount = sampleCount;
1882 paramsChanged = true;
1883 }
1884 }
1885
Ben Wagner37c54032018-04-13 14:30:23 -04001886 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001887 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001888 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1889 }
1890 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1891 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1892 {
1893 uint32_t flags = params.fSurfaceProps.flags();
1894 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001895 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1896 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1897 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001898 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001899 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001900 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1901 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1902 }
1903 paramsChanged = true;
1904 }
1905
1906 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1907 if (ImGui::Checkbox("DFT", &useDFT)) {
1908 uint32_t flags = params.fSurfaceProps.flags();
1909 if (useDFT) {
1910 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1911 } else {
1912 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1913 }
1914 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1915 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1916 paramsChanged = true;
1917 }
1918
Brian Osman8a9de3d2017-03-01 14:59:05 -05001919 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001920 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001921 auto prButton = [&](GpuPathRenderers x) {
1922 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001923 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1924 params.fGrContextOptions.fGpuPathRenderers = x;
1925 paramsChanged = true;
1926 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001927 }
1928 };
1929
1930 if (!ctx) {
1931 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001932 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001933 const auto* caps = ctx->priv().caps();
1934 prButton(GpuPathRenderers::kDefault);
Chris Dalton57ab06c2021-04-22 12:57:28 -06001935 if (fWindow->sampleCount() > 1 || FLAGS_dmsaa) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001936 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001937 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001938 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001939 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001940 if (1 == fWindow->sampleCount()) {
1941 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1942 prButton(GpuPathRenderers::kCoverageCounting);
1943 }
1944 prButton(GpuPathRenderers::kSmall);
1945 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001946 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001947 prButton(GpuPathRenderers::kNone);
1948 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001949 ImGui::TreePop();
1950 }
Brian Osman621491e2017-02-28 15:45:01 -05001951 }
1952
Ben Wagner964571d2019-03-08 12:35:06 -05001953 if (ImGui::CollapsingHeader("Tiling")) {
1954 ImGui::Checkbox("Enable", &fTiled);
1955 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1956 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1957 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1958 }
1959
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001960 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001961 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1962 this->preTouchMatrixChanged();
1963 this->onResize(fWindow->width(), fWindow->height());
1964 paramsChanged = true;
1965 }
1966
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001967 float zoom = fZoomLevel;
1968 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1969 fZoomLevel = zoom;
1970 this->preTouchMatrixChanged();
1971 paramsChanged = true;
1972 }
1973 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001974 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001975 fRotation = deg;
1976 this->preTouchMatrixChanged();
1977 paramsChanged = true;
1978 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001979 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1980 if (ImGui_DragLocation(&fOffset)) {
1981 this->preTouchMatrixChanged();
1982 paramsChanged = true;
1983 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001984 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1985 this->preTouchMatrixChanged();
1986 paramsChanged = true;
1987 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001988 }
Brian Osman805a7272018-05-02 15:40:20 -04001989 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1990 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1991 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001992 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001993 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001994 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001995 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001996 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001997 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001998 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001999 }
2000
Ben Wagnera580fb32018-04-17 11:16:32 -04002001 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04002002 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002003 if (fPaintOverrides.fAntiAlias) {
2004 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002005 }
2006 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05002007 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04002008 {
2009 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
2010 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04002011 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002012 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
2013 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002014 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002015 fPaintOverrides.fAntiAlias = true;
2016 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04002017 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05002018 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04002019 case SkPaintFields::AntiAliasState::Alias:
2020 break;
2021 case SkPaintFields::AntiAliasState::Normal:
2022 break;
2023 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
2024 gSkUseAnalyticAA = true;
2025 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002026 break;
2027 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2028 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002029 break;
2030 }
2031 }
2032 paramsChanged = true;
2033 }
2034
Ben Wagner99a78dc2018-05-09 18:23:51 -04002035 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002036 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002037 bool (SkPaint::* isFlag)() const,
2038 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002039 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002040 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002041 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002042 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002043 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002044 if (ImGui::Combo(label, &itemIndex, items)) {
2045 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002046 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002047 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002048 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002049 (fPaint.*setFlag)(itemIndex == 2);
2050 }
2051 paramsChanged = true;
2052 }
2053 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002054
Ben Wagner99a78dc2018-05-09 18:23:51 -04002055 paintFlag("Dither",
2056 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002057 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002058 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002059
2060 int styleIdx = 0;
2061 if (fPaintOverrides.fStyle) {
2062 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2063 }
2064 if (ImGui::Combo("Style", &styleIdx,
2065 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2066 {
2067 if (styleIdx == 0) {
2068 fPaintOverrides.fStyle = false;
2069 fPaint.setStyle(SkPaint::kFill_Style);
2070 } else {
2071 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2072 fPaintOverrides.fStyle = true;
2073 }
2074 paramsChanged = true;
2075 }
2076
2077 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2078 if (fPaintOverrides.fWidth) {
2079 float width = fPaint.getStrokeWidth();
2080 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2081 fPaint.setStrokeWidth(width);
2082 paramsChanged = true;
2083 }
2084 }
2085
2086 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2087 if (fPaintOverrides.fMiterLimit) {
2088 float miterLimit = fPaint.getStrokeMiter();
2089 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2090 fPaint.setStrokeMiter(miterLimit);
2091 paramsChanged = true;
2092 }
2093 }
2094
2095 int capIdx = 0;
2096 if (fPaintOverrides.fCapType) {
2097 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2098 }
2099 if (ImGui::Combo("Cap Type", &capIdx,
2100 "Default\0Butt\0Round\0Square\0\0"))
2101 {
2102 if (capIdx == 0) {
2103 fPaintOverrides.fCapType = false;
2104 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2105 } else {
2106 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2107 fPaintOverrides.fCapType = true;
2108 }
2109 paramsChanged = true;
2110 }
2111
2112 int joinIdx = 0;
2113 if (fPaintOverrides.fJoinType) {
2114 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2115 }
2116 if (ImGui::Combo("Join Type", &joinIdx,
2117 "Default\0Miter\0Round\0Bevel\0\0"))
2118 {
2119 if (joinIdx == 0) {
2120 fPaintOverrides.fJoinType = false;
2121 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2122 } else {
2123 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2124 fPaintOverrides.fJoinType = true;
2125 }
2126 paramsChanged = true;
2127 }
Ben Wagner9613e452019-01-23 10:34:59 -05002128 }
Hal Canary02738a82019-01-21 18:51:32 +00002129
Ben Wagner9613e452019-01-23 10:34:59 -05002130 if (ImGui::CollapsingHeader("Font")) {
2131 int hintingIdx = 0;
2132 if (fFontOverrides.fHinting) {
2133 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2134 }
2135 if (ImGui::Combo("Hinting", &hintingIdx,
2136 "Default\0None\0Slight\0Normal\0Full\0\0"))
2137 {
2138 if (hintingIdx == 0) {
2139 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002140 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002141 } else {
2142 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2143 fFontOverrides.fHinting = true;
2144 }
2145 paramsChanged = true;
2146 }
Hal Canary02738a82019-01-21 18:51:32 +00002147
Ben Wagner9613e452019-01-23 10:34:59 -05002148 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2149 bool SkFontFields::* flag,
2150 bool (SkFont::* isFlag)() const,
2151 void (SkFont::* setFlag)(bool) )
2152 {
2153 int itemIndex = 0;
2154 if (fFontOverrides.*flag) {
2155 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2156 }
2157 if (ImGui::Combo(label, &itemIndex, items)) {
2158 if (itemIndex == 0) {
2159 fFontOverrides.*flag = false;
2160 } else {
2161 fFontOverrides.*flag = true;
2162 (fFont.*setFlag)(itemIndex == 2);
2163 }
2164 paramsChanged = true;
2165 }
2166 };
Hal Canary02738a82019-01-21 18:51:32 +00002167
Ben Wagner9613e452019-01-23 10:34:59 -05002168 fontFlag("Fake Bold Glyphs",
2169 "Default\0No Fake Bold\0Fake Bold\0\0",
2170 &SkFontFields::fEmbolden,
2171 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002172
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002173 fontFlag("Baseline Snapping",
2174 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2175 &SkFontFields::fBaselineSnap,
2176 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2177
Ben Wagner9613e452019-01-23 10:34:59 -05002178 fontFlag("Linear Text",
2179 "Default\0No Linear Text\0Linear Text\0\0",
2180 &SkFontFields::fLinearMetrics,
2181 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002182
Ben Wagner9613e452019-01-23 10:34:59 -05002183 fontFlag("Subpixel Position Glyphs",
2184 "Default\0Pixel Text\0Subpixel Text\0\0",
2185 &SkFontFields::fSubpixel,
2186 &SkFont::isSubpixel, &SkFont::setSubpixel);
2187
2188 fontFlag("Embedded Bitmap Text",
2189 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2190 &SkFontFields::fEmbeddedBitmaps,
2191 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2192
2193 fontFlag("Force Auto-Hinting",
2194 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2195 &SkFontFields::fForceAutoHinting,
2196 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2197
2198 int edgingIdx = 0;
2199 if (fFontOverrides.fEdging) {
2200 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2201 }
2202 if (ImGui::Combo("Edging", &edgingIdx,
2203 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2204 {
2205 if (edgingIdx == 0) {
2206 fFontOverrides.fEdging = false;
2207 fFont.setEdging(SkFont::Edging::kAlias);
2208 } else {
2209 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2210 fFontOverrides.fEdging = true;
2211 }
2212 paramsChanged = true;
2213 }
2214
Ben Wagner15a8d572019-03-21 13:35:44 -04002215 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2216 if (fFontOverrides.fSize) {
2217 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002218 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002219 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002220 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002221 fFontOverrides.fSizeRange[0],
2222 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002223 "%.6f", 2.0f))
2224 {
Mike Reed3ae47332019-01-04 10:11:46 -05002225 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002226 paramsChanged = true;
2227 }
2228 }
2229
2230 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2231 if (fFontOverrides.fScaleX) {
2232 float scaleX = fFont.getScaleX();
2233 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2234 fFont.setScaleX(scaleX);
2235 paramsChanged = true;
2236 }
2237 }
2238
2239 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2240 if (fFontOverrides.fSkewX) {
2241 float skewX = fFont.getSkewX();
2242 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2243 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002244 paramsChanged = true;
2245 }
2246 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002247 }
2248
Mike Reed81f60ec2018-05-15 10:09:52 -04002249 {
2250 SkMetaData controls;
2251 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2252 if (ImGui::CollapsingHeader("Current Slide")) {
2253 SkMetaData::Iter iter(controls);
2254 const char* name;
2255 SkMetaData::Type type;
2256 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002257 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002258 if (type == SkMetaData::kScalar_Type) {
2259 float val[3];
2260 SkASSERT(count == 3);
2261 controls.findScalars(name, &count, val);
2262 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2263 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002264 }
Ben Wagner110c7032019-03-22 17:03:59 -04002265 } else if (type == SkMetaData::kBool_Type) {
2266 bool val;
2267 SkASSERT(count == 1);
2268 controls.findBool(name, &val);
2269 if (ImGui::Checkbox(name, &val)) {
2270 controls.setBool(name, val);
2271 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002272 }
2273 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002274 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002275 }
2276 }
2277 }
2278
Ben Wagner7a3c6742018-04-23 10:01:07 -04002279 if (fShowSlidePicker) {
2280 ImGui::SetNextTreeNodeOpen(true);
2281 }
Brian Osman79086b92017-02-10 13:36:16 -05002282 if (ImGui::CollapsingHeader("Slide")) {
2283 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002284 static ImVector<const char*> filteredSlideNames;
2285 static ImVector<int> filteredSlideIndices;
2286
Brian Osmanfce09c52017-11-14 15:32:20 -05002287 if (fShowSlidePicker) {
2288 ImGui::SetKeyboardFocusHere();
2289 fShowSlidePicker = false;
2290 }
2291
Brian Osman79086b92017-02-10 13:36:16 -05002292 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002293 filteredSlideNames.clear();
2294 filteredSlideIndices.clear();
2295 int filteredIndex = 0;
2296 for (int i = 0; i < fSlides.count(); ++i) {
2297 const char* slideName = fSlides[i]->getName().c_str();
2298 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2299 if (i == fCurrentSlide) {
2300 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002301 }
Brian Osmanf479e422017-11-08 13:11:36 -05002302 filteredSlideNames.push_back(slideName);
2303 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002304 }
Brian Osman79086b92017-02-10 13:36:16 -05002305 }
Brian Osmanf479e422017-11-08 13:11:36 -05002306
Brian Osmanf479e422017-11-08 13:11:36 -05002307 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2308 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002309 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002310 }
2311 }
Brian Osmana109e392017-02-24 09:49:14 -05002312
2313 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002314 ColorMode newMode = fColorMode;
2315 auto cmButton = [&](ColorMode mode, const char* label) {
2316 if (ImGui::RadioButton(label, mode == fColorMode)) {
2317 newMode = mode;
2318 }
2319 };
2320
2321 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002322 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2323 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002324 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002325
2326 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002327 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002328 }
2329
2330 // Pick from common gamuts:
2331 int primariesIdx = 4; // Default: Custom
2332 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2333 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2334 primariesIdx = i;
2335 break;
2336 }
2337 }
2338
Brian Osman03115dc2018-11-26 13:55:19 -05002339 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002340 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002341
Brian Osmana109e392017-02-24 09:49:14 -05002342 if (ImGui::Combo("Primaries", &primariesIdx,
2343 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2344 if (primariesIdx >= 0 && primariesIdx <= 3) {
2345 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2346 }
2347 }
2348
2349 // Allow direct editing of gamut
2350 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2351 }
Brian Osman207d4102019-01-10 09:40:58 -05002352
2353 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002354 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002355 if (ImGui::Checkbox("Pause", &isPaused)) {
2356 fAnimTimer.togglePauseResume();
2357 }
Brian Osman707d2022019-01-10 11:27:34 -05002358
2359 float speed = fAnimTimer.getSpeed();
2360 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2361 fAnimTimer.setSpeed(speed);
2362 }
Brian Osman207d4102019-01-10 09:40:58 -05002363 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002364
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002365 if (ImGui::CollapsingHeader("Shaders")) {
2366 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2367 GrContextOptions::ShaderCacheStrategy::kSkSL;
John Stiles7247b482021-03-08 10:40:35 -05002368
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002369#if defined(SK_VULKAN)
2370 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2371#else
2372 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002373#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002374
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002375 // To re-load shaders from the currently active programs, we flush all
2376 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002377 static bool gLoadPending = false;
2378 if (gLoadPending) {
2379 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
Brian Osmanf0de96f2021-02-26 13:54:11 -05002380 const SkString& description, int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002381 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002382 entry.fKey = key;
2383 SkMD5 hash;
2384 hash.write(key->bytes(), key->size());
2385 SkMD5::Digest digest = hash.finish();
2386 for (int i = 0; i < 16; ++i) {
2387 entry.fKeyString.appendf("%02x", digest.data[i]);
2388 }
Brian Osmanf0de96f2021-02-26 13:54:11 -05002389 entry.fKeyDescription = description;
Brian Osman0b8bb882019-04-12 11:47:19 -04002390
Brian Osman9e4e4c72020-06-10 07:19:34 -04002391 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002392 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002393 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2394 entry.fInputs,
2395 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002396 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002397 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002398 fPersistentCache.foreach(collectShaders);
2399 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002400
2401#if defined(SK_VULKAN)
2402 if (isVulkan && !sksl) {
2403 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2404 for (auto& entry : fCachedShaders) {
2405 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2406 const SkSL::String& spirv(entry.fShader[i]);
2407 std::string disasm;
2408 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2409 &disasm);
2410 entry.fShader[i].assign(disasm);
2411 }
2412 }
2413 }
2414#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002415 }
2416
John Stilesa8f6b6f2021-03-05 16:00:42 -05002417 // Defer actually doing the View/Apply logic so that we can trigger an Apply when we
Brian Osman0b8bb882019-04-12 11:47:19 -04002418 // start or finish hovering on a tree node in the list below:
John Stilesa8f6b6f2021-03-05 16:00:42 -05002419 bool doView = ImGui::Button("View"); ImGui::SameLine();
2420 bool doApply = ImGui::Button("Apply Changes"); ImGui::SameLine();
John Stiles7247b482021-03-08 10:40:35 -05002421 bool doDump = ImGui::Button("Dump SkSL to resources/sksl/");
John Stilesa8f6b6f2021-03-05 16:00:42 -05002422
John Stiles2ee4d7a2021-03-30 10:30:47 -04002423 int newOptLevel = fOptLevel;
John Stiles7247b482021-03-08 10:40:35 -05002424 ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source);
2425 ImGui::SameLine();
2426 ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile);
2427 ImGui::SameLine();
2428 ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize);
2429 ImGui::SameLine();
2430 ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline);
John Stilesa8f6b6f2021-03-05 16:00:42 -05002431
John Stiles7247b482021-03-08 10:40:35 -05002432 // If we are changing the compile mode, we want to reset the cache and redo
2433 // everything.
John Stiles2ee4d7a2021-03-30 10:30:47 -04002434 if (doDump || newOptLevel != fOptLevel) {
John Stiles7247b482021-03-08 10:40:35 -05002435 sksl = doDump || (newOptLevel == kShaderOptLevel_Source);
John Stiles2ee4d7a2021-03-30 10:30:47 -04002436 fOptLevel = (ShaderOptLevel)newOptLevel;
2437 switch (fOptLevel) {
2438 case kShaderOptLevel_Source:
2439 Compiler::EnableOptimizer(OverrideFlag::kDefault);
2440 Compiler::EnableInliner(OverrideFlag::kDefault);
2441 break;
2442 case kShaderOptLevel_Compile:
2443 Compiler::EnableOptimizer(OverrideFlag::kOff);
2444 Compiler::EnableInliner(OverrideFlag::kOff);
2445 break;
2446 case kShaderOptLevel_Optimize:
2447 Compiler::EnableOptimizer(OverrideFlag::kOn);
2448 Compiler::EnableInliner(OverrideFlag::kOff);
2449 break;
2450 case kShaderOptLevel_Inline:
2451 Compiler::EnableOptimizer(OverrideFlag::kOn);
2452 Compiler::EnableInliner(OverrideFlag::kOn);
2453 break;
2454 }
John Stiles7247b482021-03-08 10:40:35 -05002455
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002456 params.fGrContextOptions.fShaderCacheStrategy =
2457 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2458 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2459 paramsChanged = true;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002460 doView = true;
2461
2462 fDeferredActions.push_back([=]() {
2463 // Reset the cache.
2464 fPersistentCache.reset();
2465 // Dump the cache once we have drawn a frame with it.
2466 if (doDump) {
2467 fDeferredActions.push_back([this]() {
2468 this->dumpShadersToResources();
2469 });
2470 }
2471 });
Brian Osmancbc33b82019-04-19 14:16:19 -04002472 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002473
2474 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002475 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002476 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2477 bool hovered = ImGui::IsItemHovered();
2478 if (hovered != entry.fHovered) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002479 // Force an Apply to patch the highlight shader in/out
Brian Osman0b8bb882019-04-12 11:47:19 -04002480 entry.fHovered = hovered;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002481 doApply = true;
Brian Osman0b8bb882019-04-12 11:47:19 -04002482 }
2483 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002484 auto stringBox = [](const char* label, std::string* str) {
2485 // Full width, and not too much space for each shader
2486 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2487 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2488 ImGui::InputTextMultiline(label, str, boxSize);
2489 };
Brian Osmanf0de96f2021-02-26 13:54:11 -05002490 if (ImGui::TreeNode("Key")) {
2491 ImGui::TextWrapped("%s", entry.fKeyDescription.c_str());
2492 ImGui::TreePop();
2493 }
Brian Osmaneb3fb902020-08-18 13:16:59 -04002494 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2495 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002496 ImGui::TreePop();
2497 }
2498 }
2499 ImGui::EndChild();
2500
John Stilesa8f6b6f2021-03-05 16:00:42 -05002501 if (doView) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002502 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002503 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002504 gLoadPending = true;
2505 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002506
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002507 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2508 // but I'm not sure anyone wants to do that.
2509 if (isVulkan && !sksl) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002510 doApply = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002511 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002512 if (doApply) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002513 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002514 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002515 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002516 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002517 if (entry.fHovered) {
2518 // The hovered item (if any) gets a special shader to make it
2519 // identifiable.
2520 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2521 switch (entry.fShaderType) {
2522 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2523 fragShader = build_sksl_highlight_shader();
2524 break;
2525 }
2526 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2527 fragShader = build_glsl_highlight_shader(
2528 *ctx->priv().caps()->shaderCaps());
2529 break;
2530 }
2531 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2532 fragShader = build_metal_highlight_shader(fragShader);
2533 break;
2534 }
2535 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002536 }
2537
Brian Osmana085a412019-04-25 09:44:43 -04002538 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2539 entry.fShader,
2540 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002541 kGrShaderTypeCount);
Brian Osmanf0de96f2021-02-26 13:54:11 -05002542 fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription);
Brian Osman0b8bb882019-04-12 11:47:19 -04002543
2544 entry.fShader[kFragment_GrShaderType] = backup;
2545 }
2546 }
2547 }
Brian Osman79086b92017-02-10 13:36:16 -05002548 }
Brian Salomon99a33902017-03-07 15:16:34 -05002549 if (paramsChanged) {
2550 fDeferredActions.push_back([=]() {
2551 fWindow->setRequestedDisplayParams(params);
2552 fWindow->inval();
2553 this->updateTitle();
2554 });
2555 }
Brian Osman79086b92017-02-10 13:36:16 -05002556 ImGui::End();
2557 }
2558
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002559 if (gShaderErrorHandler.fErrors.count()) {
2560 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002561 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002562 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2563 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002564 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2565 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2566 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2567 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002568 }
2569 ImGui::End();
2570 gShaderErrorHandler.reset();
2571 }
2572
Brian Osmanf6877092017-02-13 09:39:57 -05002573 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002574 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2575 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002576 static int zoomFactor = 8;
2577 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002578 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002579 }
2580 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2581 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002582 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002583 }
Brian Osmanf6877092017-02-13 09:39:57 -05002584
Ben Wagner3627d2e2018-06-26 14:23:20 -04002585 if (!fZoomWindowFixed) {
2586 ImVec2 mousePos = ImGui::GetMousePos();
2587 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2588 }
2589 SkScalar x = fZoomWindowLocation.x();
2590 SkScalar y = fZoomWindowLocation.y();
2591 int xInt = SkScalarRoundToInt(x);
2592 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002593 ImVec2 avail = ImGui::GetContentRegionAvail();
2594
Brian Osmanead517d2017-11-13 15:36:36 -05002595 uint32_t pixel = 0;
2596 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002597 auto dContext = fWindow->directContext();
2598 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002599 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002600 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002601 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002602 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002603 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2604 }
2605
Greg Danielfbc60b72020-11-03 17:27:02 -05002606 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002607 // Translate so the region of the image that's under the mouse cursor is centered
2608 // in the zoom canvas:
2609 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002610 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2611 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002612 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002613
2614 SkPaint outline;
2615 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002616 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002617 });
Brian Osmanf6877092017-02-13 09:39:57 -05002618 }
2619
2620 ImGui::End();
2621 }
Brian Osman79086b92017-02-10 13:36:16 -05002622}
2623
John Stilesa8f6b6f2021-03-05 16:00:42 -05002624void Viewer::dumpShadersToResources() {
2625 // Sort the list of cached shaders so we can maintain some minimal level of consistency.
2626 // It doesn't really matter, but it will keep files from switching places unpredictably.
2627 std::vector<const CachedShader*> shaders;
2628 shaders.reserve(fCachedShaders.size());
2629 for (const CachedShader& shader : fCachedShaders) {
2630 shaders.push_back(&shader);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002631 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002632
2633 std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) {
2634 return std::tie(a->fShader[kFragment_GrShaderType], a->fShader[kVertex_GrShaderType]) <
2635 std::tie(b->fShader[kFragment_GrShaderType], b->fShader[kVertex_GrShaderType]);
2636 });
2637
2638 // Make the resources/sksl/SlideName/ directory.
2639 SkString directory = SkStringPrintf("%ssksl/%s",
2640 GetResourcePath().c_str(),
2641 fSlides[fCurrentSlide]->getName().c_str());
2642 if (!sk_mkdir(directory.c_str())) {
2643 SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str());
2644 return;
2645 }
2646
2647 int index = 0;
2648 for (const auto& entry : shaders) {
2649 SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index);
2650 FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag);
2651 if (vertFile) {
2652 const SkSL::String& vertText = entry->fShader[kVertex_GrShaderType];
2653 SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile));
2654 sk_fclose(vertFile);
2655 } else {
2656 SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str());
2657 }
2658
2659 SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index);
2660 FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag);
2661 if (fragFile) {
2662 const SkSL::String& fragText = entry->fShader[kFragment_GrShaderType];
2663 SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile));
2664 sk_fclose(fragFile);
2665 } else {
2666 SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str());
2667 }
2668
2669 ++index;
2670 }
2671}
2672
2673void Viewer::onIdle() {
2674 SkTArray<std::function<void()>> actionsToRun;
2675 actionsToRun.swap(fDeferredActions);
2676
2677 for (const auto& fn : actionsToRun) {
2678 fn();
2679 }
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002680
Brian Osman56a24812017-12-19 11:15:16 -05002681 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002682 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002683 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002684 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002685
Brian Osman79086b92017-02-10 13:36:16 -05002686 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002687 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2688 // not be visible, though. So we need to redraw if there is at least one visible window, or
2689 // more than one active window. Newly created windows are active but not visible for one frame
2690 // while they determine their layout and sizing.
2691 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2692 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002693 fWindow->inval();
2694 }
jvanverth9f372462016-04-06 06:08:59 -07002695}
liyuqiane5a6cd92016-05-27 08:52:52 -07002696
Florin Malitab632df72018-06-18 21:23:06 -04002697template <typename OptionsFunc>
2698static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2699 OptionsFunc&& optionsFunc) {
2700 writer.beginObject();
2701 {
2702 writer.appendString(kName , name);
2703 writer.appendString(kValue, value);
2704
2705 writer.beginArray(kOptions);
2706 {
2707 optionsFunc(writer);
2708 }
2709 writer.endArray();
2710 }
2711 writer.endObject();
2712}
2713
2714
liyuqiane5a6cd92016-05-27 08:52:52 -07002715void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002716 if (!fWindow) {
2717 return;
2718 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002719 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002720 return; // Surface hasn't been created yet.
2721 }
2722
Florin Malitab632df72018-06-18 21:23:06 -04002723 SkDynamicMemoryWStream memStream;
2724 SkJSONWriter writer(&memStream);
2725 writer.beginArray();
2726
liyuqianb73c24b2016-06-03 08:47:23 -07002727 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002728 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2729 [this](SkJSONWriter& writer) {
2730 for(const auto& slide : fSlides) {
2731 writer.appendString(slide->getName().c_str());
2732 }
2733 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002734
liyuqianb73c24b2016-06-03 08:47:23 -07002735 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002736 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2737 [](SkJSONWriter& writer) {
2738 for (const auto& str : kBackendTypeStrings) {
2739 writer.appendString(str);
2740 }
2741 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002742
csmartdalton578f0642017-02-24 16:04:47 -07002743 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002744 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2745 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2746 [this](SkJSONWriter& writer) {
2747 writer.appendS32(0);
2748
2749 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2750 return;
2751 }
2752
2753 for (int msaa : {4, 8, 16}) {
2754 writer.appendS32(msaa);
2755 }
2756 });
csmartdalton578f0642017-02-24 16:04:47 -07002757
csmartdalton61cd31a2017-02-27 17:00:53 -07002758 // Path renderer state
2759 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002760 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2761 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002762 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002763 if (!ctx) {
2764 writer.appendString("Software");
2765 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002766 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002767 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
Chris Dalton57ab06c2021-04-22 12:57:28 -06002768 if (fWindow->sampleCount() > 1 || FLAGS_dmsaa) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002769 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002770 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002771 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002772 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002773 }
2774 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002775 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2776 writer.appendString(
2777 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2778 }
2779 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2780 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002781 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002782 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002783 }
2784 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002785
liyuqianb73c24b2016-06-03 08:47:23 -07002786 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002787 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2788 [this](SkJSONWriter& writer) {
2789 writer.appendString(kSoftkeyHint);
2790 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2791 writer.appendString(softkey.c_str());
2792 }
2793 });
liyuqianb73c24b2016-06-03 08:47:23 -07002794
Florin Malitab632df72018-06-18 21:23:06 -04002795 writer.endArray();
2796 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002797
Florin Malitab632df72018-06-18 21:23:06 -04002798 auto data = memStream.detachAsData();
2799
2800 // TODO: would be cool to avoid this copy
2801 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2802
2803 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002804}
2805
2806void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002807 // For those who will add more features to handle the state change in this function:
2808 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2809 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2810 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002811 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002812 for (int i = 0; i < fSlides.count(); ++i) {
2813 if (fSlides[i]->getName().equals(stateValue)) {
2814 this->setCurrentSlide(i);
2815 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002816 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002817 }
Florin Malitaab99c342018-01-16 16:23:03 -05002818
2819 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002820 } else if (stateName.equals(kBackendStateName)) {
2821 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2822 if (stateValue.equals(kBackendTypeStrings[i])) {
2823 if (fBackendType != i) {
2824 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002825 for(auto& slide : fSlides) {
2826 slide->gpuTeardown();
2827 }
liyuqian6cb70252016-06-02 12:16:25 -07002828 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002829 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002830 }
2831 break;
2832 }
2833 }
csmartdalton578f0642017-02-24 16:04:47 -07002834 } else if (stateName.equals(kMSAAStateName)) {
2835 DisplayParams params = fWindow->getRequestedDisplayParams();
2836 int sampleCount = atoi(stateValue.c_str());
2837 if (sampleCount != params.fMSAASampleCount) {
2838 params.fMSAASampleCount = sampleCount;
2839 fWindow->setRequestedDisplayParams(params);
2840 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002841 this->updateTitle();
2842 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002843 }
2844 } else if (stateName.equals(kPathRendererStateName)) {
2845 DisplayParams params = fWindow->getRequestedDisplayParams();
2846 for (const auto& pair : gPathRendererNames) {
2847 if (pair.second == stateValue.c_str()) {
2848 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2849 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2850 fWindow->setRequestedDisplayParams(params);
2851 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002852 this->updateTitle();
2853 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002854 }
2855 break;
2856 }
csmartdalton578f0642017-02-24 16:04:47 -07002857 }
liyuqianb73c24b2016-06-03 08:47:23 -07002858 } else if (stateName.equals(kSoftkeyStateName)) {
2859 if (!stateValue.equals(kSoftkeyHint)) {
2860 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002861 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002862 }
liyuqian2edb0f42016-07-06 14:11:32 -07002863 } else if (stateName.equals(kRefreshStateName)) {
2864 // This state is actually NOT in the UI state.
2865 // We use this to allow Android to quickly set bool fRefresh.
2866 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002867 } else {
2868 SkDebugf("Unknown stateName: %s", stateName.c_str());
2869 }
2870}
Brian Osman79086b92017-02-10 13:36:16 -05002871
Hal Canaryb1f411a2019-08-29 10:39:22 -04002872bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002873 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002874}
2875
Hal Canaryb1f411a2019-08-29 10:39:22 -04002876bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002877 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002878 fWindow->inval();
2879 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002880 } else {
2881 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002882 }
Brian Osman79086b92017-02-10 13:36:16 -05002883}