blob: 8d7a7be7b36830508d91b692b40ea8fcbf98bb18 [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"
Chris Daltonc3176002021-07-23 15:33:09 -060034#include "src/gpu/ops/GrAtlasPathRenderer.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"
John Stiles9671d0a2021-07-16 16:22:03 -040041#include "tools/RuntimeBlendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "tools/ToolUtils.h"
43#include "tools/flags/CommandLineFlags.h"
44#include "tools/flags/CommonFlags.h"
45#include "tools/trace/EventTracingPriv.h"
46#include "tools/viewer/BisectSlide.h"
47#include "tools/viewer/GMSlide.h"
48#include "tools/viewer/ImageSlide.h"
Brian Salomon06c9e292021-04-29 14:10:23 -040049#include "tools/viewer/MSKPSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "tools/viewer/ParticlesSlide.h"
51#include "tools/viewer/SKPSlide.h"
52#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050053#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054#include "tools/viewer/SlideDir.h"
55#include "tools/viewer/SvgSlide.h"
csmartdalton578f0642017-02-24 16:04:47 -070056
Chris Dalton17dc4182020-03-25 16:18:16 -060057#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040058#include <map>
59
Hal Canary8a001442018-09-19 11:31:27 -040060#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040061#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040062
Brian Osmanc85f1fa2020-06-16 15:11:34 -040063#ifdef SK_VULKAN
64#include "spirv-tools/libspirv.hpp"
65#endif
66
Florin Malita87ccf332018-05-04 12:23:24 -040067#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050068 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040069#endif
Florin Malita45cd2002020-06-09 14:00:54 -040070#if defined(SK_ENABLE_SKRIVE)
71 #include "tools/viewer/SkRiveSlide.h"
72#endif
Florin Malita87ccf332018-05-04 12:23:24 -040073
Brian Osman5e7fbfd2019-05-03 13:13:35 -040074class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
75public:
76 void compileError(const char* shader, const char* errors) override {
77 fShaders.push_back(SkString(shader));
78 fErrors.push_back(SkString(errors));
79 }
80
81 void reset() {
82 fShaders.reset();
83 fErrors.reset();
84 }
85
86 SkTArray<SkString> fShaders;
87 SkTArray<SkString> fErrors;
88};
89
90static CapturingShaderErrorHandler gShaderErrorHandler;
91
Brian Osmanf847f312020-06-18 14:18:27 -040092GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
93
jvanverth34524262016-05-04 13:49:13 -070094using namespace sk_app;
John Stiles2ee4d7a2021-03-30 10:30:47 -040095using SkSL::Compiler;
96using OverrideFlag = SkSL::Compiler::OverrideFlag;
jvanverth34524262016-05-04 13:49:13 -070097
csmartdalton61cd31a2017-02-27 17:00:53 -070098static std::map<GpuPathRenderers, std::string> gPathRendererNames;
99
jvanverth9f372462016-04-06 06:08:59 -0700100Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -0700101 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -0700102}
103
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600104static DEFINE_string(slide, "", "Start on this sample.");
105static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -0500106
Jim Van Verth682a2f42020-05-13 16:54:09 -0400107#ifdef SK_GL
108#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700109#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400110#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700111#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400112#ifdef SK_VULKAN
113#define VK_BACKEND_STR ", \"vk\""
114#else
115#define VK_BACKEND_STR
116#endif
117#ifdef SK_METAL
118#define MTL_BACKEND_STR ", \"mtl\""
119#else
120#define MTL_BACKEND_STR
121#endif
122#ifdef SK_DIRECT3D
123#define D3D_BACKEND_STR ", \"d3d\""
124#else
125#define D3D_BACKEND_STR
126#endif
127#ifdef SK_DAWN
128#define DAWN_BACKEND_STR ", \"dawn\""
129#else
130#define DAWN_BACKEND_STR
131#endif
132#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
133#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
134 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700135
Brian Osman2dd96932016-10-18 15:33:53 -0400136static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700137
Mike Klein5b3f3432019-03-21 11:42:21 -0500138static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
Chris Daltonfd708652021-03-17 21:10:17 -0600139static DEFINE_bool(dmsaa, false, "Use internal MSAA to render to non-MSAA surfaces?");
csmartdalton008b9d82017-02-22 12:00:42 -0700140
Mike Klein84836b72019-03-21 11:31:36 -0500141static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700142
Mike Klein84836b72019-03-21 11:31:36 -0500143static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400144
Mike Kleinc6142d82019-03-25 10:54:59 -0500145static DEFINE_string2(match, m, nullptr,
146 "[~][^]substring[$] [...] of name to run.\n"
147 "Multiple matches may be separated by spaces.\n"
148 "~ causes a matching name to always be skipped\n"
149 "^ requires the start of the name to match\n"
150 "$ requires the end of the name to match\n"
151 "^ and $ requires an exact match\n"
152 "If a name does not match any list entry,\n"
153 "it is skipped unless some list entry starts with ~");
154
Mike Klein19fb3972019-03-21 13:08:08 -0500155#if defined(SK_BUILD_FOR_ANDROID)
Brian Salomon06c9e292021-04-29 14:10:23 -0400156# define PATH_PREFIX "/data/local/tmp/"
Mike Klein19fb3972019-03-21 13:08:08 -0500157#else
Brian Salomon06c9e292021-04-29 14:10:23 -0400158# define PATH_PREFIX ""
Mike Klein19fb3972019-03-21 13:08:08 -0500159#endif
160
Brian Salomon06c9e292021-04-29 14:10:23 -0400161static DEFINE_string(jpgs , PATH_PREFIX "jpgs" , "Directory to read jpgs from.");
162static DEFINE_string(skps , PATH_PREFIX "skps" , "Directory to read skps from.");
163static DEFINE_string(mskps , PATH_PREFIX "mskps" , "Directory to read mskps from.");
164static DEFINE_string(lotties, PATH_PREFIX "lotties", "Directory to read (Bodymovin) jsons from.");
165static DEFINE_string(rives , PATH_PREFIX "rives" , "Directory to read Rive (Flare) files from.");
166#undef PATH_PREFIX
167
Mike Kleinc6142d82019-03-25 10:54:59 -0500168static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
169
170static DEFINE_int_2(threads, j, -1,
171 "Run threadsafe tests on a threadpool with this many extra threads, "
172 "defaulting to one extra thread per core.");
173
Jim Van Verth7b558182019-11-14 16:47:01 -0500174static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
175
Chris Daltonc8877332020-01-06 09:48:30 -0700176static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500177static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
178static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500179static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500180static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500181static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500182
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400183#ifndef SK_GL
184static_assert(false, "viewer requires GL backend for raster.")
185#endif
186
Brian Salomon194db172017-08-17 14:37:06 -0400187const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700188 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400189#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
190 "ANGLE",
191#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400192#ifdef SK_DAWN
193 "Dawn",
194#endif
jvanverth063ece72016-06-17 09:29:14 -0700195#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700196 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700197#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400198#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500199 "Metal",
200#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400201#ifdef SK_DIRECT3D
202 "Direct3D",
203#endif
csmartdalton578f0642017-02-24 16:04:47 -0700204 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700205};
206
bsalomon6c471f72016-07-26 12:56:32 -0700207static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400208#ifdef SK_DAWN
209 if (0 == strcmp(str, "dawn")) {
210 return sk_app::Window::kDawn_BackendType;
211 } else
212#endif
bsalomon6c471f72016-07-26 12:56:32 -0700213#ifdef SK_VULKAN
214 if (0 == strcmp(str, "vk")) {
215 return sk_app::Window::kVulkan_BackendType;
216 } else
217#endif
Brian Salomon194db172017-08-17 14:37:06 -0400218#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
219 if (0 == strcmp(str, "angle")) {
220 return sk_app::Window::kANGLE_BackendType;
221 } else
222#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400223#ifdef SK_METAL
224 if (0 == strcmp(str, "mtl")) {
225 return sk_app::Window::kMetal_BackendType;
226 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500227#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400228#ifdef SK_DIRECT3D
229 if (0 == strcmp(str, "d3d")) {
230 return sk_app::Window::kDirect3D_BackendType;
231 } else
232#endif
233
bsalomon6c471f72016-07-26 12:56:32 -0700234 if (0 == strcmp(str, "gl")) {
235 return sk_app::Window::kNativeGL_BackendType;
236 } else if (0 == strcmp(str, "sw")) {
237 return sk_app::Window::kRaster_BackendType;
238 } else {
239 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
240 return sk_app::Window::kRaster_BackendType;
241 }
242}
243
Brian Osmana109e392017-02-24 09:49:14 -0500244static SkColorSpacePrimaries gSrgbPrimaries = {
245 0.64f, 0.33f,
246 0.30f, 0.60f,
247 0.15f, 0.06f,
248 0.3127f, 0.3290f };
249
250static SkColorSpacePrimaries gAdobePrimaries = {
251 0.64f, 0.33f,
252 0.21f, 0.71f,
253 0.15f, 0.06f,
254 0.3127f, 0.3290f };
255
256static SkColorSpacePrimaries gP3Primaries = {
257 0.680f, 0.320f,
258 0.265f, 0.690f,
259 0.150f, 0.060f,
260 0.3127f, 0.3290f };
261
262static SkColorSpacePrimaries gRec2020Primaries = {
263 0.708f, 0.292f,
264 0.170f, 0.797f,
265 0.131f, 0.046f,
266 0.3127f, 0.3290f };
267
268struct NamedPrimaries {
269 const char* fName;
270 SkColorSpacePrimaries* fPrimaries;
271} gNamedPrimaries[] = {
272 { "sRGB", &gSrgbPrimaries },
273 { "AdobeRGB", &gAdobePrimaries },
274 { "P3", &gP3Primaries },
275 { "Rec. 2020", &gRec2020Primaries },
276};
277
278static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
279 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
280}
281
Brian Osman70d2f432017-11-08 09:54:10 -0500282static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
283 // In raster mode, we still use GL for the window.
284 // This lets us render the GUI faster (and correct).
285 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
286}
287
Jim Van Verth74826c82019-03-01 14:37:30 -0500288class NullSlide : public Slide {
289 SkISize getDimensions() const override {
290 return SkISize::Make(640, 480);
291 }
292
293 void draw(SkCanvas* canvas) override {
294 canvas->clear(0xffff11ff);
295 }
296};
297
John Stiles31964fd2020-05-05 16:05:47 -0400298static const char kName[] = "name";
299static const char kValue[] = "value";
300static const char kOptions[] = "options";
301static const char kSlideStateName[] = "Slide";
302static const char kBackendStateName[] = "Backend";
303static const char kMSAAStateName[] = "MSAA";
304static const char kPathRendererStateName[] = "Path renderer";
305static const char kSoftkeyStateName[] = "Softkey";
306static const char kSoftkeyHint[] = "Please select a softkey";
307static const char kON[] = "ON";
308static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700309
Mike Reed862818b2020-03-21 15:07:13 -0400310extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500311extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500312extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400313
jvanverth34524262016-05-04 13:49:13 -0700314Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500315 : fCurrentSlide(-1)
316 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500317 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400318 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500319 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500320 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500321 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500322 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400323 , fZoomWindowFixed(false)
324 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500325 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400326 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700327 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500328 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500329 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500330 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500331 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500332 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700333 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400334 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400335 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400336 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500337 , fTiled(false)
338 , fDrawTileBoundaries(false)
339 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400340 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700341{
Greg Daniel285db442016-10-14 09:12:53 -0400342 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700343
Chris Dalton37ae4b02019-12-28 14:51:11 -0700344 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Daltonc3176002021-07-23 15:33:09 -0600345 gPathRendererNames[GpuPathRenderers::kAtlas] = "Atlas (tessellation)";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600346 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500347 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Dalton17dc4182020-03-25 16:18:16 -0600348 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500349 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700350
jvanverth2bb3b6d2016-04-08 07:24:09 -0700351 SkDebugf("Command line arguments: ");
352 for (int i = 1; i < argc; ++i) {
353 SkDebugf("%s ", argv[i]);
354 }
355 SkDebugf("\n");
356
Mike Klein88544fb2019-03-20 10:50:33 -0500357 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500358#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400359 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500360#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700361
Mike Reed862818b2020-03-21 15:07:13 -0400362 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500363 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500364 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400365
Mike Klein19cc0f62019-03-22 15:30:07 -0500366 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500367
Brian Osmanbc8150f2017-07-24 11:38:01 -0400368 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400369 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400370
bsalomon6c471f72016-07-26 12:56:32 -0700371 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700372 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700373
csmartdalton578f0642017-02-24 16:04:47 -0700374 DisplayParams displayParams;
375 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500376 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700377 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400378 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400379 displayParams.fGrContextOptions.fShaderCacheStrategy =
John Stiles2ee4d7a2021-03-30 10:30:47 -0400380 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400381 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
382 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Dalton94df5722021-04-19 17:40:40 -0600383 if (FLAGS_dmsaa) {
384 displayParams.fSurfaceProps = SkSurfaceProps(
Chris Dalton475c9752021-07-13 11:54:22 -0600385 displayParams.fSurfaceProps.flags() | SkSurfaceProps::kDynamicMSAA_Flag,
Chris Dalton94df5722021-04-19 17:40:40 -0600386 displayParams.fSurfaceProps.pixelGeometry());
387 }
csmartdalton578f0642017-02-24 16:04:47 -0700388 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400389 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500390 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700391
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500392 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500393 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500394
Brian Osman56a24812017-12-19 11:15:16 -0500395 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500396 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500397 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
398 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
399 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
400
jvanverth9f372462016-04-06 06:08:59 -0700401 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700402 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500403 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500404 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500405 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700406
brianosman622c8d52016-05-10 06:50:49 -0700407 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500408 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
409 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
410 fWindow->inval();
411 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500412 // Command to jump directly to the slide picker and give it focus
413 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
414 this->fShowImGuiDebugWindow = true;
415 this->fShowSlidePicker = true;
416 fWindow->inval();
417 });
418 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400419 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500420 this->fShowImGuiDebugWindow = true;
421 this->fShowSlidePicker = true;
422 fWindow->inval();
423 });
Brian Osman79086b92017-02-10 13:36:16 -0500424 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
425 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
426 fWindow->inval();
427 });
Brian Osmanf6877092017-02-13 09:39:57 -0500428 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
429 this->fShowZoomWindow = !this->fShowZoomWindow;
430 fWindow->inval();
431 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400432 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
433 this->fZoomWindowFixed = !this->fZoomWindowFixed;
434 fWindow->inval();
435 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400436 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400437 DisplayParams params = fWindow->getRequestedDisplayParams();
438 params.fDisableVsync = !params.fDisableVsync;
439 fWindow->setRequestedDisplayParams(params);
440 this->updateTitle();
441 fWindow->inval();
442 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400443 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
444 DisplayParams params = fWindow->getRequestedDisplayParams();
445 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
446 fWindow->setRequestedDisplayParams(params);
447 this->updateTitle();
448 fWindow->inval();
449 });
Mike Reedf702ed42019-07-22 17:00:49 -0400450 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
451 fRefresh = !fRefresh;
452 fWindow->inval();
453 });
brianosman622c8d52016-05-10 06:50:49 -0700454 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500455 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700456 fWindow->inval();
457 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400458 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500459 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400460 this->updateTitle();
461 fWindow->inval();
462 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500463 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500464 switch (fColorMode) {
465 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500466 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500467 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500468 case ColorMode::kColorManaged8888:
469 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500470 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500471 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400472 this->setColorMode(ColorMode::kColorManagedF16Norm);
473 break;
474 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500475 this->setColorMode(ColorMode::kLegacy);
476 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500477 }
brianosman622c8d52016-05-10 06:50:49 -0700478 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700479 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
480 DisplayParams params = fWindow->getRequestedDisplayParams();
481 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
482 fWindow->setRequestedDisplayParams(params);
483 fWindow->inval();
484 });
Brian Salomon91216d52021-04-09 11:57:59 -0400485 fCommands.addCommand('w', "Modes", "Toggle reduced shaders", [this]() {
486 DisplayParams params = fWindow->getRequestedDisplayParams();
487 params.fGrContextOptions.fReducedShaderVariations =
488 !params.fGrContextOptions.fReducedShaderVariations;
489 fWindow->setRequestedDisplayParams(params);
490 fWindow->inval();
491 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400492 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500493 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700494 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400495 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500496 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700497 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400498 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700499 this->changeZoomLevel(1.f / 32.f);
500 fWindow->inval();
501 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400502 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700503 this->changeZoomLevel(-1.f / 32.f);
504 fWindow->inval();
505 });
jvanverthaf236b52016-05-20 06:01:06 -0700506 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400507 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
508 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500509 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400510#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
511 if (newBackend == sk_app::Window::kVulkan_BackendType) {
512 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
513 sk_app::Window::kBackendTypeCount);
514 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
515 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500516 }
517#endif
Brian Osman621491e2017-02-28 15:45:01 -0500518 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700519 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500520 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
521 fSaveToSKP = true;
522 fWindow->inval();
523 });
Mike Reed376d8122019-03-14 11:39:02 -0400524 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
525 fShowSlideDimensions = !fShowSlideDimensions;
526 fWindow->inval();
527 });
Ben Wagner37c54032018-04-13 14:30:23 -0400528 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
529 DisplayParams params = fWindow->getRequestedDisplayParams();
530 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400531 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
532 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
533 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400534 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
535 } else {
536 switch (params.fSurfaceProps.pixelGeometry()) {
537 case kUnknown_SkPixelGeometry:
538 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
539 break;
540 case kRGB_H_SkPixelGeometry:
541 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
542 break;
543 case kBGR_H_SkPixelGeometry:
544 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
545 break;
546 case kRGB_V_SkPixelGeometry:
547 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
548 break;
549 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400550 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
551 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400552 break;
553 }
554 }
555 fWindow->setRequestedDisplayParams(params);
556 this->updateTitle();
557 fWindow->inval();
558 });
Ben Wagner9613e452019-01-23 10:34:59 -0500559 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500560 if (!fFontOverrides.fHinting) {
561 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400562 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500563 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500564 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400565 case SkFontHinting::kNone:
566 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500567 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400568 case SkFontHinting::kSlight:
569 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500570 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400571 case SkFontHinting::kNormal:
572 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500573 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400574 case SkFontHinting::kFull:
575 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500576 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500577 break;
578 }
579 }
580 this->updateTitle();
581 fWindow->inval();
582 });
583 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500584 if (!fPaintOverrides.fAntiAlias) {
585 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
586 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500587 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500588 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500589 } else {
590 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500591 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500592 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500593 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400594 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500595 break;
596 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500597 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500598 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400599 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500600 break;
601 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500602 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400603 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500604 break;
605 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500606 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
607 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500608 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
609 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500610 break;
611 }
612 }
613 this->updateTitle();
614 fWindow->inval();
615 });
Ben Wagner37c54032018-04-13 14:30:23 -0400616 fCommands.addCommand('D', "Modes", "DFT", [this]() {
617 DisplayParams params = fWindow->getRequestedDisplayParams();
618 uint32_t flags = params.fSurfaceProps.flags();
619 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
620 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
621 fWindow->setRequestedDisplayParams(params);
622 this->updateTitle();
623 fWindow->inval();
624 });
Ben Wagner9613e452019-01-23 10:34:59 -0500625 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500626 if (!fFontOverrides.fEdging) {
627 fFontOverrides.fEdging = true;
628 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500629 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500630 switch (fFont.getEdging()) {
631 case SkFont::Edging::kAlias:
632 fFont.setEdging(SkFont::Edging::kAntiAlias);
633 break;
634 case SkFont::Edging::kAntiAlias:
635 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
636 break;
637 case SkFont::Edging::kSubpixelAntiAlias:
638 fFont.setEdging(SkFont::Edging::kAlias);
639 fFontOverrides.fEdging = false;
640 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500641 }
642 }
643 this->updateTitle();
644 fWindow->inval();
645 });
Ben Wagner9613e452019-01-23 10:34:59 -0500646 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500647 if (!fFontOverrides.fSubpixel) {
648 fFontOverrides.fSubpixel = true;
649 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500650 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500651 if (!fFont.isSubpixel()) {
652 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500653 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500654 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500655 }
656 }
657 this->updateTitle();
658 fWindow->inval();
659 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400660 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
661 if (!fFontOverrides.fBaselineSnap) {
662 fFontOverrides.fBaselineSnap = true;
663 fFont.setBaselineSnap(false);
664 } else {
665 if (!fFont.isBaselineSnap()) {
666 fFont.setBaselineSnap(true);
667 } else {
668 fFontOverrides.fBaselineSnap = false;
669 }
670 }
671 this->updateTitle();
672 fWindow->inval();
673 });
Brian Osman805a7272018-05-02 15:40:20 -0400674 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
675 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
676 : kPerspective_Real;
677 this->updateTitle();
678 fWindow->inval();
679 });
680 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
681 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
682 : kPerspective_Off;
683 this->updateTitle();
684 fWindow->inval();
685 });
Brian Osman207d4102019-01-10 09:40:58 -0500686 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
687 fAnimTimer.togglePauseResume();
688 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400689 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
690 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500691 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400692 fWindow->inval();
693 });
Mike Reed59295352020-03-12 13:56:34 -0400694 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
695 fDrawViaSerialize = !fDrawViaSerialize;
696 this->updateTitle();
697 fWindow->inval();
698 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500699 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400700 gUseSkVMBlitter = !gUseSkVMBlitter;
701 this->updateTitle();
702 fWindow->inval();
703 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500704 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
705 gSkVMAllowJIT = !gSkVMAllowJIT;
706 this->updateTitle();
707 fWindow->inval();
708 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500709
jvanverth2bb3b6d2016-04-08 07:24:09 -0700710 // set up slides
711 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500712 if (FLAGS_list) {
713 this->listNames();
714 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700715
Brian Osman9bb47cf2018-04-26 15:55:00 -0400716 fPerspectivePoints[0].set(0, 0);
717 fPerspectivePoints[1].set(1, 0);
718 fPerspectivePoints[2].set(0, 1);
719 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700720 fAnimTimer.run();
721
Hal Canaryc465d132017-12-08 10:21:31 -0500722 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500723 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500724 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500725 }
726 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500727
jongdeok.kim804f17e2019-02-26 14:39:23 +0900728 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500729 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700730}
731
jvanverth34524262016-05-04 13:49:13 -0700732void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400733 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
734 static const struct {
735 const char* fExtension;
736 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500737 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400738 const SlideFactory fFactory;
739 } gExternalSlidesInfo[] = {
Brian Salomon06c9e292021-04-29 14:10:23 -0400740 { ".mskp", "mskp-dir", FLAGS_mskps,
741 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
742 return sk_make_sp<MSKPSlide>(name, path);}
743 },
Florin Malita0ffa3222018-04-05 14:34:45 -0400744 { ".skp", "skp-dir", FLAGS_skps,
745 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
746 return sk_make_sp<SKPSlide>(name, path);}
747 },
748 { ".jpg", "jpg-dir", FLAGS_jpgs,
749 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
750 return sk_make_sp<ImageSlide>(name, path);}
751 },
Florin Malita87ccf332018-05-04 12:23:24 -0400752#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400753 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400754 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
755 return sk_make_sp<SkottieSlide>(name, path);}
756 },
Florin Malita87ccf332018-05-04 12:23:24 -0400757#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400758 #if defined(SK_ENABLE_SKRIVE)
759 { ".flr", "skrive-dir", FLAGS_rives,
760 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
761 return sk_make_sp<SkRiveSlide>(name, path);}
762 },
763 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400764#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400765 { ".svg", "svg-dir", FLAGS_svgs,
766 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
767 return sk_make_sp<SvgSlide>(name, path);}
768 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400769#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400770 };
jvanverthc265a922016-04-08 12:51:45 -0700771
Brian Salomon343553a2018-09-05 15:41:23 -0400772 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700773
Mike Klein88544fb2019-03-20 10:50:33 -0500774 const auto addSlide =
775 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
776 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
777 return;
778 }
liyuqian6f163d22016-06-13 12:26:45 -0700779
Mike Klein88544fb2019-03-20 10:50:33 -0500780 if (auto slide = fact(name, path)) {
781 dirSlides.push_back(slide);
782 fSlides.push_back(std::move(slide));
783 }
784 };
Florin Malita76a076b2018-02-15 18:40:48 -0500785
Florin Malita38792ce2018-05-08 10:36:18 -0400786 if (!FLAGS_file.isEmpty()) {
787 // single file mode
788 const SkString file(FLAGS_file[0]);
789
790 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
791 for (const auto& sinfo : gExternalSlidesInfo) {
792 if (file.endsWith(sinfo.fExtension)) {
793 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
794 return;
795 }
796 }
797
798 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
799 } else {
800 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
801 }
802
803 return;
804 }
805
806 // Bisect slide.
807 if (!FLAGS_bisect.isEmpty()) {
808 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500809 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400810 if (FLAGS_bisect.count() >= 2) {
811 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
812 bisect->onChar(*ch);
813 }
814 }
815 fSlides.push_back(std::move(bisect));
816 }
817 }
818
819 // GMs
820 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400821 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400822 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500823 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400824 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400825 fSlides.push_back(std::move(slide));
826 }
Florin Malita38792ce2018-05-08 10:36:18 -0400827 }
828 // reverse gms
829 int numGMs = fSlides.count() - firstGM;
830 for (int i = 0; i < numGMs/2; ++i) {
831 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
832 }
833
834 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400835 for (const SampleFactory factory : SampleRegistry::Range()) {
836 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500837 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400838 fSlides.push_back(slide);
839 }
Florin Malita38792ce2018-05-08 10:36:18 -0400840 }
841
Brian Osman7c979f52019-02-12 13:27:51 -0500842 // Particle demo
843 {
844 // TODO: Convert this to a sample
845 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500846 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500847 fSlides.push_back(std::move(slide));
848 }
849 }
850
Brian Osmand927bd22019-12-18 11:23:12 -0500851 // Runtime shader editor
852 {
853 sk_sp<Slide> slide(new SkSLSlide());
854 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
855 fSlides.push_back(std::move(slide));
856 }
857 }
858
Florin Malita0ffa3222018-04-05 14:34:45 -0400859 for (const auto& info : gExternalSlidesInfo) {
860 for (const auto& flag : info.fFlags) {
861 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
862 // single file
863 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
864 } else {
865 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400866 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400867 SkTArray<SkString> sortedFilenames;
868 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400869 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400870 sortedFilenames.push_back(name);
871 }
872 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400873 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000874 [](const SkString& a, const SkString& b) {
875 return strcmp(a.c_str(), b.c_str()) < 0;
876 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400877 }
878 for (const SkString& filename : sortedFilenames) {
879 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
880 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400881 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400882 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400883 if (!dirSlides.empty()) {
884 fSlides.push_back(
885 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
886 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500887 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400888 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400889 }
890 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500891
892 if (!fSlides.count()) {
893 sk_sp<Slide> slide(new NullSlide());
894 fSlides.push_back(std::move(slide));
895 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700896}
897
898
jvanverth34524262016-05-04 13:49:13 -0700899Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400900 for(auto& slide : fSlides) {
901 slide->gpuTeardown();
902 }
903
jvanverth9f372462016-04-06 06:08:59 -0700904 fWindow->detach();
905 delete fWindow;
906}
907
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500908struct SkPaintTitleUpdater {
909 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
910 void append(const char* s) {
911 if (fCount == 0) {
912 fTitle->append(" {");
913 } else {
914 fTitle->append(", ");
915 }
916 fTitle->append(s);
917 ++fCount;
918 }
919 void done() {
920 if (fCount > 0) {
921 fTitle->append("}");
922 }
923 }
924 SkString* fTitle;
925 int fCount;
926};
927
brianosman05de2162016-05-06 13:28:57 -0700928void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700929 if (!fWindow) {
930 return;
931 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500932 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700933 return; // Surface hasn't been created yet.
934 }
935
jvanverth34524262016-05-04 13:49:13 -0700936 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700937 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700938
Mike Kleine5acd752019-03-22 09:57:16 -0500939 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400940 if (gSkForceAnalyticAA) {
941 title.append(" <FAAA>");
942 } else {
943 title.append(" <AAA>");
944 }
945 }
Mike Reed59295352020-03-12 13:56:34 -0400946 if (fDrawViaSerialize) {
947 title.append(" <serialize>");
948 }
Mike Reed862818b2020-03-21 15:07:13 -0400949 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500950 title.append(" <SkVMBlitter>");
951 }
952 if (!gSkVMAllowJIT) {
953 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400954 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400955
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500956 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500957 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
958 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400959 const char* on, const char* off)
960 {
Ben Wagner9613e452019-01-23 10:34:59 -0500961 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400962 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500963 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400964 };
965
Ben Wagner9613e452019-01-23 10:34:59 -0500966 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
967 const char* on, const char* off)
968 {
969 if (fFontOverrides.*flag) {
970 paintTitle.append((fFont.*isFlag)() ? on : off);
971 }
972 };
973
974 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
975 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
976
977 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
978 "Force Autohint", "No Force Autohint");
979 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400980 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500981 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
982 "Linear Metrics", "Non-Linear Metrics");
983 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
984 "Bitmap Text", "No Bitmap Text");
985 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
986
987 if (fFontOverrides.fEdging) {
988 switch (fFont.getEdging()) {
989 case SkFont::Edging::kAlias:
990 paintTitle.append("Alias Text");
991 break;
992 case SkFont::Edging::kAntiAlias:
993 paintTitle.append("Antialias Text");
994 break;
995 case SkFont::Edging::kSubpixelAntiAlias:
996 paintTitle.append("Subpixel Antialias Text");
997 break;
998 }
999 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001000
Mike Reed3ae47332019-01-04 10:11:46 -05001001 if (fFontOverrides.fHinting) {
1002 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -04001003 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001004 paintTitle.append("No Hinting");
1005 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001006 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001007 paintTitle.append("Slight Hinting");
1008 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001009 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001010 paintTitle.append("Normal Hinting");
1011 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001012 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001013 paintTitle.append("Full Hinting");
1014 break;
1015 }
1016 }
1017 paintTitle.done();
1018
Brian Osman92004802017-03-06 11:47:26 -05001019 switch (fColorMode) {
1020 case ColorMode::kLegacy:
1021 title.append(" Legacy 8888");
1022 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001023 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001024 title.append(" ColorManaged 8888");
1025 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001026 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001027 title.append(" ColorManaged F16");
1028 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001029 case ColorMode::kColorManagedF16Norm:
1030 title.append(" ColorManaged F16 Norm");
1031 break;
Brian Osman92004802017-03-06 11:47:26 -05001032 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001033
Brian Osman92004802017-03-06 11:47:26 -05001034 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001035 int curPrimaries = -1;
1036 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1037 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1038 curPrimaries = i;
1039 break;
1040 }
1041 }
Brian Osman03115dc2018-11-26 13:55:19 -05001042 title.appendf(" %s Gamma %f",
1043 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001044 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001045 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001046
Ben Wagner37c54032018-04-13 14:30:23 -04001047 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001048 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001049 switch (params.fSurfaceProps.pixelGeometry()) {
1050 case kUnknown_SkPixelGeometry:
1051 title.append( " Flat");
1052 break;
1053 case kRGB_H_SkPixelGeometry:
1054 title.append( " RGB");
1055 break;
1056 case kBGR_H_SkPixelGeometry:
1057 title.append( " BGR");
1058 break;
1059 case kRGB_V_SkPixelGeometry:
1060 title.append( " RGBV");
1061 break;
1062 case kBGR_V_SkPixelGeometry:
1063 title.append( " BGRV");
1064 break;
1065 }
1066 }
1067
1068 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1069 title.append(" DFT");
1070 }
1071
csmartdalton578f0642017-02-24 16:04:47 -07001072 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001073 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001074 int msaa = fWindow->sampleCount();
1075 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001076 title.appendf(" MSAA: %i", msaa);
1077 }
1078 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001079
1080 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001081 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001082 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1083 }
1084
Brian Osman805a7272018-05-02 15:40:20 -04001085 if (kPerspective_Real == fPerspectiveMode) {
1086 title.append(" Perpsective (Real)");
1087 } else if (kPerspective_Fake == fPerspectiveMode) {
1088 title.append(" Perspective (Fake)");
1089 }
1090
brianosman05de2162016-05-06 13:28:57 -07001091 fWindow->setTitle(title.c_str());
1092}
1093
Florin Malitaab99c342018-01-16 16:23:03 -05001094int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001095
1096 if (!FLAGS_slide.isEmpty()) {
1097 int count = fSlides.count();
1098 for (int i = 0; i < count; i++) {
1099 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001100 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001101 }
1102 }
1103
1104 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1105 this->listNames();
1106 }
1107
Florin Malitaab99c342018-01-16 16:23:03 -05001108 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001109}
1110
Florin Malitaab99c342018-01-16 16:23:03 -05001111void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001112 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001113 for (const auto& slide : fSlides) {
1114 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001115 }
1116}
1117
Florin Malitaab99c342018-01-16 16:23:03 -05001118void Viewer::setCurrentSlide(int slide) {
1119 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001120
Florin Malitaab99c342018-01-16 16:23:03 -05001121 if (slide == fCurrentSlide) {
1122 return;
1123 }
1124
1125 if (fCurrentSlide >= 0) {
1126 fSlides[fCurrentSlide]->unload();
1127 }
1128
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001129 SkScalar scaleFactor = 1.0;
1130 if (fApplyBackingScale) {
1131 scaleFactor = fWindow->scaleFactor();
1132 }
1133 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1134 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001135 fCurrentSlide = slide;
1136 this->setupCurrentSlide();
1137}
1138
1139void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001140 if (fCurrentSlide >= 0) {
1141 // prepare dimensions for image slides
1142 fGesture.resetTouchState();
1143 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001144
Jim Van Verth0848fb02018-01-22 13:39:30 -05001145 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1146 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1147 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001148
Jim Van Verth0848fb02018-01-22 13:39:30 -05001149 // Start with a matrix that scales the slide to the available screen space
1150 if (fWindow->scaleContentToFit()) {
1151 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001152 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1153 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001154 }
liyuqiane46e4f02016-05-20 07:32:19 -07001155 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001156
1157 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001158 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001159
1160 this->updateTitle();
1161 this->updateUIState();
1162
1163 fStatsLayer.resetMeasurements();
1164
1165 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001166 }
jvanverthc265a922016-04-08 12:51:45 -07001167}
1168
Brian Osmanaba642c2020-02-06 12:52:25 -05001169#define MAX_ZOOM_LEVEL 8.0f
1170#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001171
jvanverth34524262016-05-04 13:49:13 -07001172void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001173 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001174 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001175 this->preTouchMatrixChanged();
1176}
Yuqian Li755778c2018-03-28 16:23:31 -04001177
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001178void Viewer::preTouchMatrixChanged() {
1179 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001180 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1181 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1182 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1183 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1184}
1185
Brian Osman805a7272018-05-02 15:40:20 -04001186SkMatrix Viewer::computePerspectiveMatrix() {
1187 SkScalar w = fWindow->width(), h = fWindow->height();
1188 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1189 SkPoint perspPts[4] = {
1190 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1191 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1192 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1193 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1194 };
1195 SkMatrix m;
1196 m.setPolyToPoly(orthoPts, perspPts, 4);
1197 return m;
1198}
1199
Yuqian Li755778c2018-03-28 16:23:31 -04001200SkMatrix Viewer::computePreTouchMatrix() {
1201 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001202
1203 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001204 if (fApplyBackingScale) {
1205 zoomScale *= fWindow->scaleFactor();
1206 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001207 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001208 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001209
1210 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1211 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001212
Brian Osman805a7272018-05-02 15:40:20 -04001213 if (kPerspective_Real == fPerspectiveMode) {
1214 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001215 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001216 }
1217
Yuqian Li755778c2018-03-28 16:23:31 -04001218 return m;
jvanverthc265a922016-04-08 12:51:45 -07001219}
1220
liyuqiand3cdbca2016-05-17 12:44:20 -07001221SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001222 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001223 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001224 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001225 return m;
jvanverthc265a922016-04-08 12:51:45 -07001226}
1227
Brian Osman621491e2017-02-28 15:45:01 -05001228void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001229 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001230 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001231 fBackendType = backendType;
1232
Robert Phillipse9229532020-06-26 10:10:49 -04001233 // The active context is going away in 'detach'
1234 for(auto& slide : fSlides) {
1235 slide->gpuTeardown();
1236 }
1237
Brian Osman621491e2017-02-28 15:45:01 -05001238 fWindow->detach();
1239
Brian Osman70d2f432017-11-08 09:54:10 -05001240#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001241 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1242 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001243 DisplayParams params = fWindow->getRequestedDisplayParams();
1244 delete fWindow;
1245 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001246
Brian Osman70d2f432017-11-08 09:54:10 -05001247 // re-register callbacks
1248 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001249 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001250 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001251 fWindow->pushLayer(&fImGuiLayer);
1252
Brian Osman70d2f432017-11-08 09:54:10 -05001253 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1254 // will still include our correct sample count. But the re-created fWindow will lose that
1255 // information. On Windows, we need to re-create the window when changing sample count,
1256 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1257 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1258 // as if we had never changed windows at all).
1259 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001260#endif
1261
Brian Osman70d2f432017-11-08 09:54:10 -05001262 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001263}
1264
Brian Osman92004802017-03-06 11:47:26 -05001265void Viewer::setColorMode(ColorMode colorMode) {
1266 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001267 this->updateTitle();
1268 fWindow->inval();
1269}
1270
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001271class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1272public:
Mike Reed3ae47332019-01-04 10:11:46 -05001273 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1274 SkFont* font, Viewer::SkFontFields* ffields)
1275 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001276 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001277 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1278 sk_sp<SkTextBlob>* cache) {
1279 bool blobWillChange = false;
1280 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001281 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1282 bool shouldDraw = this->filterFont(&filteredFont);
1283 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001284 blobWillChange = true;
1285 break;
1286 }
1287 }
1288 if (!blobWillChange) {
1289 return blob;
1290 }
1291
1292 SkTextBlobBuilder builder;
1293 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001294 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1295 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001296 if (!shouldDraw) {
1297 continue;
1298 }
1299
Mike Reed3ae47332019-01-04 10:11:46 -05001300 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001301
Ben Wagner41e40472018-09-24 13:01:54 -04001302 const SkTextBlobBuilder::RunBuffer& runBuffer
1303 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001304 ? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(),
1305 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001306 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001307 ? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(),
1308 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001309 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001310 ? builder.allocRunTextPos(font, it.glyphCount(), it.textSize())
Ben Wagnere5736262021-02-08 16:52:08 -05001311 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001312 ? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001313 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1314 uint32_t glyphCount = it.glyphCount();
1315 if (it.glyphs()) {
1316 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1317 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1318 }
1319 if (it.pos()) {
1320 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001321 unsigned posPerGlyph = it.scalarsPerGlyph();
1322 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001323 }
1324 if (it.text()) {
1325 size_t textSize = sizeof(decltype(*it.text()));
1326 uint32_t textCount = it.textSize();
1327 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1328 }
1329 if (it.clusters()) {
1330 size_t clusterSize = sizeof(decltype(*it.clusters()));
1331 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1332 }
1333 }
1334 *cache = builder.make();
1335 return cache->get();
1336 }
1337 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1338 const SkPaint& paint) override {
1339 sk_sp<SkTextBlob> cache;
1340 this->SkPaintFilterCanvas::onDrawTextBlob(
1341 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1342 }
Mike Reed3ae47332019-01-04 10:11:46 -05001343 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001344 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001345 font->writable()->setSize(fFont->getSize());
1346 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001347 if (fFontOverrides->fScaleX) {
1348 font->writable()->setScaleX(fFont->getScaleX());
1349 }
1350 if (fFontOverrides->fSkewX) {
1351 font->writable()->setSkewX(fFont->getSkewX());
1352 }
Mike Reed3ae47332019-01-04 10:11:46 -05001353 if (fFontOverrides->fHinting) {
1354 font->writable()->setHinting(fFont->getHinting());
1355 }
Ben Wagner9613e452019-01-23 10:34:59 -05001356 if (fFontOverrides->fEdging) {
1357 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001358 }
Ben Wagner9613e452019-01-23 10:34:59 -05001359 if (fFontOverrides->fEmbolden) {
1360 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001361 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001362 if (fFontOverrides->fBaselineSnap) {
1363 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1364 }
Ben Wagner9613e452019-01-23 10:34:59 -05001365 if (fFontOverrides->fLinearMetrics) {
1366 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001367 }
Ben Wagner9613e452019-01-23 10:34:59 -05001368 if (fFontOverrides->fSubpixel) {
1369 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001370 }
Ben Wagner9613e452019-01-23 10:34:59 -05001371 if (fFontOverrides->fEmbeddedBitmaps) {
1372 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001373 }
Ben Wagner9613e452019-01-23 10:34:59 -05001374 if (fFontOverrides->fForceAutoHinting) {
1375 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001376 }
Ben Wagner9613e452019-01-23 10:34:59 -05001377
Mike Reed3ae47332019-01-04 10:11:46 -05001378 return true;
1379 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001380 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001381 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001382 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001383 }
Ben Wagner9613e452019-01-23 10:34:59 -05001384 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001385 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001386 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001387 if (fPaintOverrides->fStyle) {
1388 paint.setStyle(fPaint->getStyle());
1389 }
1390 if (fPaintOverrides->fWidth) {
1391 paint.setStrokeWidth(fPaint->getStrokeWidth());
1392 }
1393 if (fPaintOverrides->fMiterLimit) {
1394 paint.setStrokeMiter(fPaint->getStrokeMiter());
1395 }
1396 if (fPaintOverrides->fCapType) {
1397 paint.setStrokeCap(fPaint->getStrokeCap());
1398 }
1399 if (fPaintOverrides->fJoinType) {
1400 paint.setStrokeJoin(fPaint->getStrokeJoin());
1401 }
John Stiles9671d0a2021-07-16 16:22:03 -04001402 if (fPaintOverrides->fForceRuntimeBlend) {
1403 if (skstd::optional<SkBlendMode> mode = paint.asBlendMode()) {
1404 paint.setBlender(GetRuntimeBlendForBlendMode(*mode));
1405 }
1406 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001407 return true;
1408 }
1409 SkPaint* fPaint;
1410 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001411 SkFont* fFont;
1412 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001413};
1414
Robert Phillips9882dae2019-03-04 11:00:10 -05001415void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001416 if (fCurrentSlide < 0) {
1417 return;
1418 }
1419
Robert Phillips9882dae2019-03-04 11:00:10 -05001420 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001421
Brian Osmanf750fbc2017-02-08 10:47:28 -05001422 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001423 SkSurface* slideSurface = surface;
1424 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001425 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001426
Brian Osmane0d4fba2017-03-15 10:24:55 -04001427 // 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 -05001428 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001429 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001430 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001431 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001432 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001433 }
1434
Brian Osman3ac99cf2017-12-01 11:23:53 -05001435 if (fSaveToSKP) {
1436 SkPictureRecorder recorder;
1437 SkCanvas* recorderCanvas = recorder.beginRecording(
1438 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001439 fSlides[fCurrentSlide]->draw(recorderCanvas);
1440 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1441 SkFILEWStream stream("sample_app.skp");
1442 picture->serialize(&stream);
1443 fSaveToSKP = false;
1444 }
1445
Brian Osmane9ed0f02018-11-26 14:50:05 -05001446 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001447 SkColorType colorType;
1448 switch (fColorMode) {
1449 case ColorMode::kLegacy:
1450 case ColorMode::kColorManaged8888:
1451 colorType = kN32_SkColorType;
1452 break;
1453 case ColorMode::kColorManagedF16:
1454 colorType = kRGBA_F16_SkColorType;
1455 break;
1456 case ColorMode::kColorManagedF16Norm:
1457 colorType = kRGBA_F16Norm_SkColorType;
1458 break;
1459 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001460
1461 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001462 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001463 slideCanvas->getProps(&props);
1464
Brian Osmane9ed0f02018-11-26 14:50:05 -05001465 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1466 return Window::kRaster_BackendType == this->fBackendType
1467 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001468 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001469 };
1470
Brian Osman03115dc2018-11-26 13:55:19 -05001471 // We need to render offscreen if we're...
1472 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1473 // ... in any raster mode, because the window surface is actually GL
1474 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001475 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001476 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001477 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001478 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001479 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001480 colorSpace != nullptr ||
1481 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001482
Brian Osmane9ed0f02018-11-26 14:50:05 -05001483 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001484 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001485 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001486 }
1487
Mike Reed59295352020-03-12 13:56:34 -04001488 SkPictureRecorder recorder;
1489 SkCanvas* recorderRestoreCanvas = nullptr;
1490 if (fDrawViaSerialize) {
1491 recorderRestoreCanvas = slideCanvas;
1492 slideCanvas = recorder.beginRecording(
1493 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1494 }
1495
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001496 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001497 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001498 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001499 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001500 if (fTiled) {
1501 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1502 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001503 for (int y = 0; y < fWindow->height(); y += tileH) {
1504 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001505 SkAutoCanvasRestore acr(slideCanvas, true);
1506 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1507 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001508 }
1509 }
1510
1511 // Draw borders between tiles
1512 if (fDrawTileBoundaries) {
1513 SkPaint border;
1514 border.setColor(0x60FF00FF);
1515 border.setStyle(SkPaint::kStroke_Style);
1516 for (int y = 0; y < fWindow->height(); y += tileH) {
1517 for (int x = 0; x < fWindow->width(); x += tileW) {
1518 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1519 }
1520 }
1521 }
1522 } else {
1523 slideCanvas->concat(this->computeMatrix());
1524 if (kPerspective_Real == fPerspectiveMode) {
1525 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1526 }
John Stiles9671d0a2021-07-16 16:22:03 -04001527 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides,
1528 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001529 fSlides[fCurrentSlide]->draw(&filterCanvas);
1530 }
Brian Osman56a24812017-12-19 11:15:16 -05001531 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001532 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001533
Mike Reed59295352020-03-12 13:56:34 -04001534 if (recorderRestoreCanvas) {
1535 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1536 auto data = picture->serialize();
1537 slideCanvas = recorderRestoreCanvas;
1538 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1539 }
1540
Brian Osman1df161a2017-02-09 12:10:20 -05001541 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001542 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001543 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001544 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001545
1546 // If we rendered offscreen, snap an image and push the results to the window's canvas
1547 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001548 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001549
Robert Phillips9882dae2019-03-04 11:00:10 -05001550 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001551 SkPaint paint;
1552 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001553 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001554 int prePerspectiveCount = canvas->save();
1555 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001556 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001557 canvas->clear(SK_ColorWHITE);
1558 canvas->concat(this->computePerspectiveMatrix());
1559 }
Mike Reedb339d052021-01-28 11:20:41 -05001560 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001561 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001562 }
Mike Reed376d8122019-03-14 11:39:02 -04001563
1564 if (fShowSlideDimensions) {
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001565 SkCanvas* canvas = surface->getCanvas();
1566 SkAutoCanvasRestore acr(canvas, true);
1567 canvas->concat(this->computeMatrix());
Mike Reed376d8122019-03-14 11:39:02 -04001568 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1569 SkPaint paint;
1570 paint.setColor(0x40FFFF00);
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001571 canvas->drawRect(r, paint);
Mike Reed376d8122019-03-14 11:39:02 -04001572 }
liyuqian6f163d22016-06-13 12:26:45 -07001573}
1574
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001575void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001576 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001577 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001578}
Jim Van Verth6f449692017-02-14 15:16:46 -05001579
Robert Phillips9882dae2019-03-04 11:00:10 -05001580void Viewer::onPaint(SkSurface* surface) {
1581 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001582
Robert Phillips9882dae2019-03-04 11:00:10 -05001583 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001584
Brian Osmand67e5182017-12-08 16:46:09 -05001585 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001586
Greg Daniel427d8eb2020-09-28 15:04:18 -04001587 fLastImage.reset();
1588
Robert Phillipsed653392020-07-10 13:55:21 -04001589 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001590 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001591 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001592 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001593}
1594
Ben Wagnera1915972018-08-09 15:06:19 -04001595void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001596 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001597 SkScalar scaleFactor = 1.0;
1598 if (fApplyBackingScale) {
1599 scaleFactor = fWindow->scaleFactor();
1600 }
1601 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001602 }
Ben Wagnera1915972018-08-09 15:06:19 -04001603}
1604
Florin Malitacefc1b92018-02-19 21:43:47 -05001605SkPoint Viewer::mapEvent(float x, float y) {
1606 const auto m = this->computeMatrix();
1607 SkMatrix inv;
1608
1609 SkAssertResult(m.invert(&inv));
1610
1611 return inv.mapXY(x, y);
1612}
1613
Hal Canaryb1f411a2019-08-29 10:39:22 -04001614bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001615 if (GestureDevice::kMouse == fGestureDevice) {
1616 return false;
1617 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001618
1619 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001620 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001621 fWindow->inval();
1622 return true;
1623 }
1624
liyuqiand3cdbca2016-05-17 12:44:20 -07001625 void* castedOwner = reinterpret_cast<void*>(owner);
1626 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001627 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001628 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001629#if defined(SK_BUILD_FOR_IOS)
1630 // TODO: move IOS swipe detection higher up into the platform code
1631 SkPoint dir;
1632 if (fGesture.isFling(&dir)) {
1633 // swiping left or right
1634 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1635 if (dir.fX < 0) {
1636 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1637 fCurrentSlide + 1 : 0);
1638 } else {
1639 this->setCurrentSlide(fCurrentSlide > 0 ?
1640 fCurrentSlide - 1 : fSlides.count() - 1);
1641 }
1642 }
1643 fGesture.reset();
1644 }
1645#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001646 break;
1647 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001648 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001649 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001650 break;
1651 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001652 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001653 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001654 break;
1655 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001656 default: {
1657 // kLeft and kRight are only for swipes
1658 SkASSERT(false);
1659 break;
1660 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001661 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001662 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001663 fWindow->inval();
1664 return true;
1665}
1666
Hal Canaryb1f411a2019-08-29 10:39:22 -04001667bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001668 if (GestureDevice::kTouch == fGestureDevice) {
1669 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001670 }
Brian Osman16c81a12017-12-20 11:58:34 -05001671
Florin Malitacefc1b92018-02-19 21:43:47 -05001672 const auto slidePt = this->mapEvent(x, y);
1673 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1674 fWindow->inval();
1675 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001676 }
1677
1678 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001679 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001680 fGesture.touchEnd(nullptr);
1681 break;
1682 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001683 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001684 fGesture.touchBegin(nullptr, x, y);
1685 break;
1686 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001687 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001688 fGesture.touchMoved(nullptr, x, y);
1689 break;
1690 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001691 default: {
1692 SkASSERT(false); // shouldn't see kRight or kLeft here
1693 break;
1694 }
Brian Osman16c81a12017-12-20 11:58:34 -05001695 }
1696 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1697
Hal Canaryb1f411a2019-08-29 10:39:22 -04001698 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001699 fWindow->inval();
1700 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001701 return true;
1702}
1703
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001704bool Viewer::onFling(skui::InputState state) {
1705 if (skui::InputState::kRight == state) {
1706 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1707 return true;
1708 } else if (skui::InputState::kLeft == state) {
1709 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1710 return true;
1711 }
1712 return false;
1713}
1714
1715bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1716 switch (state) {
1717 case skui::InputState::kDown:
1718 fGesture.startZoom();
1719 return true;
1720 break;
1721 case skui::InputState::kMove:
1722 fGesture.updateZoom(scale, x, y, x, y);
1723 return true;
1724 break;
1725 case skui::InputState::kUp:
1726 fGesture.endZoom();
1727 return true;
1728 break;
1729 default:
1730 SkASSERT(false);
1731 break;
1732 }
1733
1734 return false;
1735}
1736
Brian Osmana109e392017-02-24 09:49:14 -05001737static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001738 // The gamut image covers a (0.8 x 0.9) shaped region
1739 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001740
1741 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1742 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1743 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001744 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1745 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1746 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001747
Brian Osman535c5e32019-02-09 16:32:58 -05001748 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1749 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1750 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1751 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1752 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001753}
1754
Ben Wagner3627d2e2018-06-26 14:23:20 -04001755static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001756 ImGui::DragCanvas dc(pt);
1757 dc.fillColor(IM_COL32(0, 0, 0, 128));
1758 dc.dragPoint(pt);
1759 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001760}
1761
Brian Osman9bb47cf2018-04-26 15:55:00 -04001762static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001763 ImGui::DragCanvas dc(pts);
1764 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001765
Brian Osman535c5e32019-02-09 16:32:58 -05001766 for (int i = 0; i < 4; ++i) {
1767 dc.dragPoint(pts + i);
1768 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001769
Brian Osman535c5e32019-02-09 16:32:58 -05001770 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1771 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1772 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1773 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001774
Brian Osman535c5e32019-02-09 16:32:58 -05001775 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001776}
1777
John Stiles38b7d2f2020-06-24 12:13:31 -04001778static SkSL::String build_sksl_highlight_shader() {
1779 return SkSL::String("out half4 sk_FragColor;\n"
1780 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1781}
1782
1783static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1784 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1785 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1786 size_t pos = inShader.rfind("return *_out;\n");
1787 if (pos == std::string::npos) {
1788 return inShader;
1789 }
1790
1791 SkSL::String replacementShader = inShader;
1792 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1793 return replacementShader;
1794}
1795
1796static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1797 const char* versionDecl = shaderCaps.versionDeclString();
1798 SkSL::String highlight = versionDecl ? versionDecl : "";
1799 if (shaderCaps.usesPrecisionModifiers()) {
1800 highlight.append("precision mediump float;\n");
1801 }
1802 highlight.appendf("out vec4 sk_FragColor;\n"
1803 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1804 return highlight;
1805}
1806
Brian Osmand67e5182017-12-08 16:46:09 -05001807void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001808 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1809 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001810 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001811 }
1812
1813 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001814 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1815 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001816 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001817 DisplayParams params = fWindow->getRequestedDisplayParams();
1818 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001819 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001820
Brian Osmana109e392017-02-24 09:49:14 -05001821 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1822 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001823 if (ImGui::CollapsingHeader("Backend")) {
1824 int newBackend = static_cast<int>(fBackendType);
1825 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1826 ImGui::SameLine();
1827 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001828#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1829 ImGui::SameLine();
1830 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1831#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001832#if defined(SK_DAWN)
1833 ImGui::SameLine();
1834 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1835#endif
John Stilesf7da9232020-11-19 19:58:14 -05001836#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001837 ImGui::SameLine();
1838 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1839#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001840#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001841 ImGui::SameLine();
1842 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1843#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001844#if defined(SK_DIRECT3D)
1845 ImGui::SameLine();
1846 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1847#endif
Brian Osman621491e2017-02-28 15:45:01 -05001848 if (newBackend != fBackendType) {
1849 fDeferredActions.push_back([=]() {
1850 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1851 });
1852 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001853
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001854 bool* wire = &params.fGrContextOptions.fWireframeMode;
1855 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1856 paramsChanged = true;
1857 }
Brian Salomon99a33902017-03-07 15:16:34 -05001858
Brian Salomon91216d52021-04-09 11:57:59 -04001859 bool* reducedShaders = &params.fGrContextOptions.fReducedShaderVariations;
1860 if (ctx && ImGui::Checkbox("Reduced shaders", reducedShaders)) {
1861 paramsChanged = true;
1862 }
1863
Brian Osman28b12522017-03-08 17:10:24 -05001864 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001865 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001866 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001867 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1868 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1869 1;
1870
1871 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1872 if (maxMSAA >= 4) {
1873 ImGui::Text("MSAA: ");
1874
1875 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1876 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1877 // don't show it in the list.
1878 if (curMSAA == 2) {
1879 continue;
1880 }
1881 ImGui::SameLine();
1882 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1883 &sampleCount, curMSAA);
1884 }
1885 }
Brian Osman28b12522017-03-08 17:10:24 -05001886
1887 if (sampleCount != params.fMSAASampleCount) {
1888 params.fMSAASampleCount = sampleCount;
1889 paramsChanged = true;
1890 }
1891 }
1892
Ben Wagner37c54032018-04-13 14:30:23 -04001893 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001894 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001895 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1896 }
1897 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1898 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1899 {
1900 uint32_t flags = params.fSurfaceProps.flags();
1901 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001902 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1903 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1904 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001905 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001906 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001907 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1908 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1909 }
1910 paramsChanged = true;
1911 }
1912
1913 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1914 if (ImGui::Checkbox("DFT", &useDFT)) {
1915 uint32_t flags = params.fSurfaceProps.flags();
1916 if (useDFT) {
1917 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1918 } else {
1919 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1920 }
1921 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1922 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1923 paramsChanged = true;
1924 }
1925
Brian Osman8a9de3d2017-03-01 14:59:05 -05001926 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001927 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001928 auto prButton = [&](GpuPathRenderers x) {
1929 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001930 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1931 params.fGrContextOptions.fGpuPathRenderers = x;
1932 paramsChanged = true;
1933 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001934 }
1935 };
1936
1937 if (!ctx) {
1938 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001939 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001940 const auto* caps = ctx->priv().caps();
1941 prButton(GpuPathRenderers::kDefault);
Chris Dalton57ab06c2021-04-22 12:57:28 -06001942 if (fWindow->sampleCount() > 1 || FLAGS_dmsaa) {
Chris Daltonc3176002021-07-23 15:33:09 -06001943 if (GrAtlasPathRenderer::IsSupported(ctx)) {
1944 prButton(GpuPathRenderers::kAtlas);
1945 }
Chris Daltonff18ff62020-12-07 17:39:26 -07001946 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001947 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001948 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001949 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001950 if (1 == fWindow->sampleCount()) {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001951 prButton(GpuPathRenderers::kSmall);
1952 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001953 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001954 prButton(GpuPathRenderers::kNone);
1955 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001956 ImGui::TreePop();
1957 }
Brian Osman621491e2017-02-28 15:45:01 -05001958 }
1959
Ben Wagner964571d2019-03-08 12:35:06 -05001960 if (ImGui::CollapsingHeader("Tiling")) {
1961 ImGui::Checkbox("Enable", &fTiled);
1962 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1963 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1964 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1965 }
1966
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001967 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001968 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1969 this->preTouchMatrixChanged();
1970 this->onResize(fWindow->width(), fWindow->height());
1971 paramsChanged = true;
1972 }
1973
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001974 float zoom = fZoomLevel;
1975 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1976 fZoomLevel = zoom;
1977 this->preTouchMatrixChanged();
1978 paramsChanged = true;
1979 }
1980 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001981 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001982 fRotation = deg;
1983 this->preTouchMatrixChanged();
1984 paramsChanged = true;
1985 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001986 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1987 if (ImGui_DragLocation(&fOffset)) {
1988 this->preTouchMatrixChanged();
1989 paramsChanged = true;
1990 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001991 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1992 this->preTouchMatrixChanged();
1993 paramsChanged = true;
1994 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001995 }
Brian Osman805a7272018-05-02 15:40:20 -04001996 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1997 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1998 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001999 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04002000 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04002001 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04002002 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04002003 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04002004 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04002005 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04002006 }
2007
Ben Wagnera580fb32018-04-17 11:16:32 -04002008 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04002009 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002010 if (fPaintOverrides.fAntiAlias) {
2011 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002012 }
2013 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05002014 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04002015 {
2016 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
2017 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04002018 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002019 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
2020 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002021 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002022 fPaintOverrides.fAntiAlias = true;
2023 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04002024 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05002025 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04002026 case SkPaintFields::AntiAliasState::Alias:
2027 break;
2028 case SkPaintFields::AntiAliasState::Normal:
2029 break;
2030 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
2031 gSkUseAnalyticAA = true;
2032 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002033 break;
2034 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2035 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002036 break;
2037 }
2038 }
2039 paramsChanged = true;
2040 }
2041
Ben Wagner99a78dc2018-05-09 18:23:51 -04002042 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002043 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002044 bool (SkPaint::* isFlag)() const,
2045 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002046 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002047 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002048 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002049 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002050 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002051 if (ImGui::Combo(label, &itemIndex, items)) {
2052 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002053 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002054 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002055 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002056 (fPaint.*setFlag)(itemIndex == 2);
2057 }
2058 paramsChanged = true;
2059 }
2060 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002061
Ben Wagner99a78dc2018-05-09 18:23:51 -04002062 paintFlag("Dither",
2063 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002064 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002065 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002066
2067 int styleIdx = 0;
2068 if (fPaintOverrides.fStyle) {
2069 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2070 }
2071 if (ImGui::Combo("Style", &styleIdx,
2072 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2073 {
2074 if (styleIdx == 0) {
2075 fPaintOverrides.fStyle = false;
2076 fPaint.setStyle(SkPaint::kFill_Style);
2077 } else {
2078 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2079 fPaintOverrides.fStyle = true;
2080 }
2081 paramsChanged = true;
2082 }
2083
John Stiles9671d0a2021-07-16 16:22:03 -04002084 ImGui::Checkbox("Force Runtime Blends", &fPaintOverrides.fForceRuntimeBlend);
2085
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002086 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2087 if (fPaintOverrides.fWidth) {
2088 float width = fPaint.getStrokeWidth();
2089 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2090 fPaint.setStrokeWidth(width);
2091 paramsChanged = true;
2092 }
2093 }
2094
2095 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2096 if (fPaintOverrides.fMiterLimit) {
2097 float miterLimit = fPaint.getStrokeMiter();
2098 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2099 fPaint.setStrokeMiter(miterLimit);
2100 paramsChanged = true;
2101 }
2102 }
2103
2104 int capIdx = 0;
2105 if (fPaintOverrides.fCapType) {
2106 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2107 }
2108 if (ImGui::Combo("Cap Type", &capIdx,
2109 "Default\0Butt\0Round\0Square\0\0"))
2110 {
2111 if (capIdx == 0) {
2112 fPaintOverrides.fCapType = false;
2113 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2114 } else {
2115 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2116 fPaintOverrides.fCapType = true;
2117 }
2118 paramsChanged = true;
2119 }
2120
2121 int joinIdx = 0;
2122 if (fPaintOverrides.fJoinType) {
2123 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2124 }
2125 if (ImGui::Combo("Join Type", &joinIdx,
2126 "Default\0Miter\0Round\0Bevel\0\0"))
2127 {
2128 if (joinIdx == 0) {
2129 fPaintOverrides.fJoinType = false;
2130 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2131 } else {
2132 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2133 fPaintOverrides.fJoinType = true;
2134 }
2135 paramsChanged = true;
2136 }
Ben Wagner9613e452019-01-23 10:34:59 -05002137 }
Hal Canary02738a82019-01-21 18:51:32 +00002138
Ben Wagner9613e452019-01-23 10:34:59 -05002139 if (ImGui::CollapsingHeader("Font")) {
2140 int hintingIdx = 0;
2141 if (fFontOverrides.fHinting) {
2142 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2143 }
2144 if (ImGui::Combo("Hinting", &hintingIdx,
2145 "Default\0None\0Slight\0Normal\0Full\0\0"))
2146 {
2147 if (hintingIdx == 0) {
2148 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002149 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002150 } else {
2151 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2152 fFontOverrides.fHinting = true;
2153 }
2154 paramsChanged = true;
2155 }
Hal Canary02738a82019-01-21 18:51:32 +00002156
Ben Wagner9613e452019-01-23 10:34:59 -05002157 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2158 bool SkFontFields::* flag,
2159 bool (SkFont::* isFlag)() const,
2160 void (SkFont::* setFlag)(bool) )
2161 {
2162 int itemIndex = 0;
2163 if (fFontOverrides.*flag) {
2164 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2165 }
2166 if (ImGui::Combo(label, &itemIndex, items)) {
2167 if (itemIndex == 0) {
2168 fFontOverrides.*flag = false;
2169 } else {
2170 fFontOverrides.*flag = true;
2171 (fFont.*setFlag)(itemIndex == 2);
2172 }
2173 paramsChanged = true;
2174 }
2175 };
Hal Canary02738a82019-01-21 18:51:32 +00002176
Ben Wagner9613e452019-01-23 10:34:59 -05002177 fontFlag("Fake Bold Glyphs",
2178 "Default\0No Fake Bold\0Fake Bold\0\0",
2179 &SkFontFields::fEmbolden,
2180 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002181
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002182 fontFlag("Baseline Snapping",
2183 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2184 &SkFontFields::fBaselineSnap,
2185 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2186
Ben Wagner9613e452019-01-23 10:34:59 -05002187 fontFlag("Linear Text",
2188 "Default\0No Linear Text\0Linear Text\0\0",
2189 &SkFontFields::fLinearMetrics,
2190 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002191
Ben Wagner9613e452019-01-23 10:34:59 -05002192 fontFlag("Subpixel Position Glyphs",
2193 "Default\0Pixel Text\0Subpixel Text\0\0",
2194 &SkFontFields::fSubpixel,
2195 &SkFont::isSubpixel, &SkFont::setSubpixel);
2196
2197 fontFlag("Embedded Bitmap Text",
2198 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2199 &SkFontFields::fEmbeddedBitmaps,
2200 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2201
2202 fontFlag("Force Auto-Hinting",
2203 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2204 &SkFontFields::fForceAutoHinting,
2205 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2206
2207 int edgingIdx = 0;
2208 if (fFontOverrides.fEdging) {
2209 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2210 }
2211 if (ImGui::Combo("Edging", &edgingIdx,
2212 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2213 {
2214 if (edgingIdx == 0) {
2215 fFontOverrides.fEdging = false;
2216 fFont.setEdging(SkFont::Edging::kAlias);
2217 } else {
2218 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2219 fFontOverrides.fEdging = true;
2220 }
2221 paramsChanged = true;
2222 }
2223
Ben Wagner15a8d572019-03-21 13:35:44 -04002224 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2225 if (fFontOverrides.fSize) {
2226 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002227 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002228 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002229 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002230 fFontOverrides.fSizeRange[0],
2231 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002232 "%.6f", 2.0f))
2233 {
Mike Reed3ae47332019-01-04 10:11:46 -05002234 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002235 paramsChanged = true;
2236 }
2237 }
2238
2239 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2240 if (fFontOverrides.fScaleX) {
2241 float scaleX = fFont.getScaleX();
2242 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2243 fFont.setScaleX(scaleX);
2244 paramsChanged = true;
2245 }
2246 }
2247
2248 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2249 if (fFontOverrides.fSkewX) {
2250 float skewX = fFont.getSkewX();
2251 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2252 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002253 paramsChanged = true;
2254 }
2255 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002256 }
2257
Mike Reed81f60ec2018-05-15 10:09:52 -04002258 {
2259 SkMetaData controls;
2260 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2261 if (ImGui::CollapsingHeader("Current Slide")) {
2262 SkMetaData::Iter iter(controls);
2263 const char* name;
2264 SkMetaData::Type type;
2265 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002266 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002267 if (type == SkMetaData::kScalar_Type) {
2268 float val[3];
2269 SkASSERT(count == 3);
2270 controls.findScalars(name, &count, val);
2271 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2272 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002273 }
Ben Wagner110c7032019-03-22 17:03:59 -04002274 } else if (type == SkMetaData::kBool_Type) {
2275 bool val;
2276 SkASSERT(count == 1);
2277 controls.findBool(name, &val);
2278 if (ImGui::Checkbox(name, &val)) {
2279 controls.setBool(name, val);
2280 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002281 }
2282 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002283 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002284 }
2285 }
2286 }
2287
Ben Wagner7a3c6742018-04-23 10:01:07 -04002288 if (fShowSlidePicker) {
2289 ImGui::SetNextTreeNodeOpen(true);
2290 }
Brian Osman79086b92017-02-10 13:36:16 -05002291 if (ImGui::CollapsingHeader("Slide")) {
2292 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002293 static ImVector<const char*> filteredSlideNames;
2294 static ImVector<int> filteredSlideIndices;
2295
Brian Osmanfce09c52017-11-14 15:32:20 -05002296 if (fShowSlidePicker) {
2297 ImGui::SetKeyboardFocusHere();
2298 fShowSlidePicker = false;
2299 }
2300
Brian Osman79086b92017-02-10 13:36:16 -05002301 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002302 filteredSlideNames.clear();
2303 filteredSlideIndices.clear();
2304 int filteredIndex = 0;
2305 for (int i = 0; i < fSlides.count(); ++i) {
2306 const char* slideName = fSlides[i]->getName().c_str();
2307 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2308 if (i == fCurrentSlide) {
2309 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002310 }
Brian Osmanf479e422017-11-08 13:11:36 -05002311 filteredSlideNames.push_back(slideName);
2312 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002313 }
Brian Osman79086b92017-02-10 13:36:16 -05002314 }
Brian Osmanf479e422017-11-08 13:11:36 -05002315
Brian Osmanf479e422017-11-08 13:11:36 -05002316 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2317 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002318 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002319 }
2320 }
Brian Osmana109e392017-02-24 09:49:14 -05002321
2322 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002323 ColorMode newMode = fColorMode;
2324 auto cmButton = [&](ColorMode mode, const char* label) {
2325 if (ImGui::RadioButton(label, mode == fColorMode)) {
2326 newMode = mode;
2327 }
2328 };
2329
2330 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002331 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2332 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002333 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002334
2335 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002336 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002337 }
2338
2339 // Pick from common gamuts:
2340 int primariesIdx = 4; // Default: Custom
2341 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2342 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2343 primariesIdx = i;
2344 break;
2345 }
2346 }
2347
Brian Osman03115dc2018-11-26 13:55:19 -05002348 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002349 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002350
Brian Osmana109e392017-02-24 09:49:14 -05002351 if (ImGui::Combo("Primaries", &primariesIdx,
2352 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2353 if (primariesIdx >= 0 && primariesIdx <= 3) {
2354 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2355 }
2356 }
2357
2358 // Allow direct editing of gamut
2359 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2360 }
Brian Osman207d4102019-01-10 09:40:58 -05002361
2362 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002363 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002364 if (ImGui::Checkbox("Pause", &isPaused)) {
2365 fAnimTimer.togglePauseResume();
2366 }
Brian Osman707d2022019-01-10 11:27:34 -05002367
2368 float speed = fAnimTimer.getSpeed();
2369 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2370 fAnimTimer.setSpeed(speed);
2371 }
Brian Osman207d4102019-01-10 09:40:58 -05002372 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002373
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002374 if (ImGui::CollapsingHeader("Shaders")) {
2375 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2376 GrContextOptions::ShaderCacheStrategy::kSkSL;
John Stiles7247b482021-03-08 10:40:35 -05002377
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002378#if defined(SK_VULKAN)
2379 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2380#else
2381 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002382#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002383
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002384 // To re-load shaders from the currently active programs, we flush all
2385 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002386 static bool gLoadPending = false;
2387 if (gLoadPending) {
2388 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
Brian Osmanf0de96f2021-02-26 13:54:11 -05002389 const SkString& description, int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002390 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002391 entry.fKey = key;
2392 SkMD5 hash;
2393 hash.write(key->bytes(), key->size());
2394 SkMD5::Digest digest = hash.finish();
2395 for (int i = 0; i < 16; ++i) {
2396 entry.fKeyString.appendf("%02x", digest.data[i]);
2397 }
Brian Osmanf0de96f2021-02-26 13:54:11 -05002398 entry.fKeyDescription = description;
Brian Osman0b8bb882019-04-12 11:47:19 -04002399
Brian Osman9e4e4c72020-06-10 07:19:34 -04002400 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002401 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002402 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2403 entry.fInputs,
2404 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002405 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002406 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002407 fPersistentCache.foreach(collectShaders);
2408 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002409
2410#if defined(SK_VULKAN)
2411 if (isVulkan && !sksl) {
2412 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2413 for (auto& entry : fCachedShaders) {
2414 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2415 const SkSL::String& spirv(entry.fShader[i]);
2416 std::string disasm;
2417 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2418 &disasm);
2419 entry.fShader[i].assign(disasm);
2420 }
2421 }
2422 }
2423#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002424 }
2425
John Stilesa8f6b6f2021-03-05 16:00:42 -05002426 // Defer actually doing the View/Apply logic so that we can trigger an Apply when we
Brian Osman0b8bb882019-04-12 11:47:19 -04002427 // start or finish hovering on a tree node in the list below:
John Stilesa8f6b6f2021-03-05 16:00:42 -05002428 bool doView = ImGui::Button("View"); ImGui::SameLine();
2429 bool doApply = ImGui::Button("Apply Changes"); ImGui::SameLine();
John Stiles7247b482021-03-08 10:40:35 -05002430 bool doDump = ImGui::Button("Dump SkSL to resources/sksl/");
John Stilesa8f6b6f2021-03-05 16:00:42 -05002431
John Stiles2ee4d7a2021-03-30 10:30:47 -04002432 int newOptLevel = fOptLevel;
John Stiles7247b482021-03-08 10:40:35 -05002433 ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source);
2434 ImGui::SameLine();
2435 ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile);
2436 ImGui::SameLine();
2437 ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize);
2438 ImGui::SameLine();
2439 ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline);
John Stilesa8f6b6f2021-03-05 16:00:42 -05002440
John Stiles7247b482021-03-08 10:40:35 -05002441 // If we are changing the compile mode, we want to reset the cache and redo
2442 // everything.
John Stiles2ee4d7a2021-03-30 10:30:47 -04002443 if (doDump || newOptLevel != fOptLevel) {
John Stiles7247b482021-03-08 10:40:35 -05002444 sksl = doDump || (newOptLevel == kShaderOptLevel_Source);
John Stiles2ee4d7a2021-03-30 10:30:47 -04002445 fOptLevel = (ShaderOptLevel)newOptLevel;
2446 switch (fOptLevel) {
2447 case kShaderOptLevel_Source:
2448 Compiler::EnableOptimizer(OverrideFlag::kDefault);
2449 Compiler::EnableInliner(OverrideFlag::kDefault);
2450 break;
2451 case kShaderOptLevel_Compile:
2452 Compiler::EnableOptimizer(OverrideFlag::kOff);
2453 Compiler::EnableInliner(OverrideFlag::kOff);
2454 break;
2455 case kShaderOptLevel_Optimize:
2456 Compiler::EnableOptimizer(OverrideFlag::kOn);
2457 Compiler::EnableInliner(OverrideFlag::kOff);
2458 break;
2459 case kShaderOptLevel_Inline:
2460 Compiler::EnableOptimizer(OverrideFlag::kOn);
2461 Compiler::EnableInliner(OverrideFlag::kOn);
2462 break;
2463 }
John Stiles7247b482021-03-08 10:40:35 -05002464
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002465 params.fGrContextOptions.fShaderCacheStrategy =
2466 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2467 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2468 paramsChanged = true;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002469 doView = true;
2470
2471 fDeferredActions.push_back([=]() {
2472 // Reset the cache.
2473 fPersistentCache.reset();
2474 // Dump the cache once we have drawn a frame with it.
2475 if (doDump) {
2476 fDeferredActions.push_back([this]() {
2477 this->dumpShadersToResources();
2478 });
2479 }
2480 });
Brian Osmancbc33b82019-04-19 14:16:19 -04002481 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002482
2483 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002484 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002485 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2486 bool hovered = ImGui::IsItemHovered();
2487 if (hovered != entry.fHovered) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002488 // Force an Apply to patch the highlight shader in/out
Brian Osman0b8bb882019-04-12 11:47:19 -04002489 entry.fHovered = hovered;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002490 doApply = true;
Brian Osman0b8bb882019-04-12 11:47:19 -04002491 }
2492 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002493 auto stringBox = [](const char* label, std::string* str) {
2494 // Full width, and not too much space for each shader
2495 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2496 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2497 ImGui::InputTextMultiline(label, str, boxSize);
2498 };
Brian Osmanf0de96f2021-02-26 13:54:11 -05002499 if (ImGui::TreeNode("Key")) {
2500 ImGui::TextWrapped("%s", entry.fKeyDescription.c_str());
2501 ImGui::TreePop();
2502 }
Brian Osmaneb3fb902020-08-18 13:16:59 -04002503 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2504 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002505 ImGui::TreePop();
2506 }
2507 }
2508 ImGui::EndChild();
2509
John Stilesa8f6b6f2021-03-05 16:00:42 -05002510 if (doView) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002511 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002512 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002513 gLoadPending = true;
2514 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002515
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002516 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2517 // but I'm not sure anyone wants to do that.
2518 if (isVulkan && !sksl) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002519 doApply = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002520 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002521 if (doApply) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002522 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002523 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002524 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002525 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002526 if (entry.fHovered) {
2527 // The hovered item (if any) gets a special shader to make it
2528 // identifiable.
2529 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2530 switch (entry.fShaderType) {
2531 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2532 fragShader = build_sksl_highlight_shader();
2533 break;
2534 }
2535 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2536 fragShader = build_glsl_highlight_shader(
2537 *ctx->priv().caps()->shaderCaps());
2538 break;
2539 }
2540 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2541 fragShader = build_metal_highlight_shader(fragShader);
2542 break;
2543 }
2544 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002545 }
2546
Brian Osmana085a412019-04-25 09:44:43 -04002547 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2548 entry.fShader,
2549 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002550 kGrShaderTypeCount);
Brian Osmanf0de96f2021-02-26 13:54:11 -05002551 fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription);
Brian Osman0b8bb882019-04-12 11:47:19 -04002552
2553 entry.fShader[kFragment_GrShaderType] = backup;
2554 }
2555 }
2556 }
Brian Osman79086b92017-02-10 13:36:16 -05002557 }
Brian Salomon99a33902017-03-07 15:16:34 -05002558 if (paramsChanged) {
2559 fDeferredActions.push_back([=]() {
2560 fWindow->setRequestedDisplayParams(params);
2561 fWindow->inval();
2562 this->updateTitle();
2563 });
2564 }
Brian Osman79086b92017-02-10 13:36:16 -05002565 ImGui::End();
2566 }
2567
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002568 if (gShaderErrorHandler.fErrors.count()) {
2569 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002570 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002571 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2572 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002573 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2574 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2575 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2576 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002577 }
2578 ImGui::End();
2579 gShaderErrorHandler.reset();
2580 }
2581
Brian Osmanf6877092017-02-13 09:39:57 -05002582 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002583 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2584 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002585 static int zoomFactor = 8;
2586 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002587 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002588 }
2589 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2590 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002591 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002592 }
Brian Osmanf6877092017-02-13 09:39:57 -05002593
Ben Wagner3627d2e2018-06-26 14:23:20 -04002594 if (!fZoomWindowFixed) {
2595 ImVec2 mousePos = ImGui::GetMousePos();
2596 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2597 }
2598 SkScalar x = fZoomWindowLocation.x();
2599 SkScalar y = fZoomWindowLocation.y();
2600 int xInt = SkScalarRoundToInt(x);
2601 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002602 ImVec2 avail = ImGui::GetContentRegionAvail();
2603
Brian Osmanead517d2017-11-13 15:36:36 -05002604 uint32_t pixel = 0;
2605 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002606 auto dContext = fWindow->directContext();
2607 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002608 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002609 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002610 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002611 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002612 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2613 }
2614
Greg Danielfbc60b72020-11-03 17:27:02 -05002615 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002616 // Translate so the region of the image that's under the mouse cursor is centered
2617 // in the zoom canvas:
2618 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002619 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2620 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002621 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002622
2623 SkPaint outline;
2624 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002625 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002626 });
Brian Osmanf6877092017-02-13 09:39:57 -05002627 }
2628
2629 ImGui::End();
2630 }
Brian Osman79086b92017-02-10 13:36:16 -05002631}
2632
John Stilesa8f6b6f2021-03-05 16:00:42 -05002633void Viewer::dumpShadersToResources() {
2634 // Sort the list of cached shaders so we can maintain some minimal level of consistency.
2635 // It doesn't really matter, but it will keep files from switching places unpredictably.
2636 std::vector<const CachedShader*> shaders;
2637 shaders.reserve(fCachedShaders.size());
2638 for (const CachedShader& shader : fCachedShaders) {
2639 shaders.push_back(&shader);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002640 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002641
2642 std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) {
2643 return std::tie(a->fShader[kFragment_GrShaderType], a->fShader[kVertex_GrShaderType]) <
2644 std::tie(b->fShader[kFragment_GrShaderType], b->fShader[kVertex_GrShaderType]);
2645 });
2646
2647 // Make the resources/sksl/SlideName/ directory.
2648 SkString directory = SkStringPrintf("%ssksl/%s",
2649 GetResourcePath().c_str(),
2650 fSlides[fCurrentSlide]->getName().c_str());
2651 if (!sk_mkdir(directory.c_str())) {
2652 SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str());
2653 return;
2654 }
2655
2656 int index = 0;
2657 for (const auto& entry : shaders) {
2658 SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index);
2659 FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag);
2660 if (vertFile) {
2661 const SkSL::String& vertText = entry->fShader[kVertex_GrShaderType];
2662 SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile));
2663 sk_fclose(vertFile);
2664 } else {
2665 SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str());
2666 }
2667
2668 SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index);
2669 FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag);
2670 if (fragFile) {
2671 const SkSL::String& fragText = entry->fShader[kFragment_GrShaderType];
2672 SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile));
2673 sk_fclose(fragFile);
2674 } else {
2675 SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str());
2676 }
2677
2678 ++index;
2679 }
2680}
2681
2682void Viewer::onIdle() {
2683 SkTArray<std::function<void()>> actionsToRun;
2684 actionsToRun.swap(fDeferredActions);
2685
2686 for (const auto& fn : actionsToRun) {
2687 fn();
2688 }
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002689
Brian Osman56a24812017-12-19 11:15:16 -05002690 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002691 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002692 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002693 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002694
Brian Osman79086b92017-02-10 13:36:16 -05002695 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002696 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2697 // not be visible, though. So we need to redraw if there is at least one visible window, or
2698 // more than one active window. Newly created windows are active but not visible for one frame
2699 // while they determine their layout and sizing.
2700 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2701 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002702 fWindow->inval();
2703 }
jvanverth9f372462016-04-06 06:08:59 -07002704}
liyuqiane5a6cd92016-05-27 08:52:52 -07002705
Florin Malitab632df72018-06-18 21:23:06 -04002706template <typename OptionsFunc>
2707static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2708 OptionsFunc&& optionsFunc) {
2709 writer.beginObject();
2710 {
2711 writer.appendString(kName , name);
2712 writer.appendString(kValue, value);
2713
2714 writer.beginArray(kOptions);
2715 {
2716 optionsFunc(writer);
2717 }
2718 writer.endArray();
2719 }
2720 writer.endObject();
2721}
2722
2723
liyuqiane5a6cd92016-05-27 08:52:52 -07002724void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002725 if (!fWindow) {
2726 return;
2727 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002728 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002729 return; // Surface hasn't been created yet.
2730 }
2731
Florin Malitab632df72018-06-18 21:23:06 -04002732 SkDynamicMemoryWStream memStream;
2733 SkJSONWriter writer(&memStream);
2734 writer.beginArray();
2735
liyuqianb73c24b2016-06-03 08:47:23 -07002736 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002737 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2738 [this](SkJSONWriter& writer) {
2739 for(const auto& slide : fSlides) {
2740 writer.appendString(slide->getName().c_str());
2741 }
2742 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002743
liyuqianb73c24b2016-06-03 08:47:23 -07002744 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002745 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2746 [](SkJSONWriter& writer) {
2747 for (const auto& str : kBackendTypeStrings) {
2748 writer.appendString(str);
2749 }
2750 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002751
csmartdalton578f0642017-02-24 16:04:47 -07002752 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002753 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2754 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2755 [this](SkJSONWriter& writer) {
2756 writer.appendS32(0);
2757
2758 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2759 return;
2760 }
2761
2762 for (int msaa : {4, 8, 16}) {
2763 writer.appendS32(msaa);
2764 }
2765 });
csmartdalton578f0642017-02-24 16:04:47 -07002766
csmartdalton61cd31a2017-02-27 17:00:53 -07002767 // Path renderer state
2768 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002769 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2770 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002771 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002772 if (!ctx) {
2773 writer.appendString("Software");
2774 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002775 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002776 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
Chris Dalton57ab06c2021-04-22 12:57:28 -06002777 if (fWindow->sampleCount() > 1 || FLAGS_dmsaa) {
Chris Daltonc3176002021-07-23 15:33:09 -06002778 if (GrAtlasPathRenderer::IsSupported(ctx)) {
2779 writer.appendString(
2780 gPathRendererNames[GpuPathRenderers::kAtlas].c_str());
2781 }
Chris Daltonff18ff62020-12-07 17:39:26 -07002782 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002783 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002784 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002785 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002786 }
2787 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002788 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2789 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002790 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002791 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002792 }
2793 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002794
liyuqianb73c24b2016-06-03 08:47:23 -07002795 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002796 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2797 [this](SkJSONWriter& writer) {
2798 writer.appendString(kSoftkeyHint);
2799 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2800 writer.appendString(softkey.c_str());
2801 }
2802 });
liyuqianb73c24b2016-06-03 08:47:23 -07002803
Florin Malitab632df72018-06-18 21:23:06 -04002804 writer.endArray();
2805 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002806
Florin Malitab632df72018-06-18 21:23:06 -04002807 auto data = memStream.detachAsData();
2808
2809 // TODO: would be cool to avoid this copy
2810 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2811
2812 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002813}
2814
2815void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002816 // For those who will add more features to handle the state change in this function:
2817 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2818 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2819 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002820 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002821 for (int i = 0; i < fSlides.count(); ++i) {
2822 if (fSlides[i]->getName().equals(stateValue)) {
2823 this->setCurrentSlide(i);
2824 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002825 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002826 }
Florin Malitaab99c342018-01-16 16:23:03 -05002827
2828 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002829 } else if (stateName.equals(kBackendStateName)) {
2830 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2831 if (stateValue.equals(kBackendTypeStrings[i])) {
2832 if (fBackendType != i) {
2833 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002834 for(auto& slide : fSlides) {
2835 slide->gpuTeardown();
2836 }
liyuqian6cb70252016-06-02 12:16:25 -07002837 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002838 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002839 }
2840 break;
2841 }
2842 }
csmartdalton578f0642017-02-24 16:04:47 -07002843 } else if (stateName.equals(kMSAAStateName)) {
2844 DisplayParams params = fWindow->getRequestedDisplayParams();
2845 int sampleCount = atoi(stateValue.c_str());
2846 if (sampleCount != params.fMSAASampleCount) {
2847 params.fMSAASampleCount = sampleCount;
2848 fWindow->setRequestedDisplayParams(params);
2849 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002850 this->updateTitle();
2851 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002852 }
2853 } else if (stateName.equals(kPathRendererStateName)) {
2854 DisplayParams params = fWindow->getRequestedDisplayParams();
2855 for (const auto& pair : gPathRendererNames) {
2856 if (pair.second == stateValue.c_str()) {
2857 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2858 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2859 fWindow->setRequestedDisplayParams(params);
2860 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002861 this->updateTitle();
2862 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002863 }
2864 break;
2865 }
csmartdalton578f0642017-02-24 16:04:47 -07002866 }
liyuqianb73c24b2016-06-03 08:47:23 -07002867 } else if (stateName.equals(kSoftkeyStateName)) {
2868 if (!stateValue.equals(kSoftkeyHint)) {
2869 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002870 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002871 }
liyuqian2edb0f42016-07-06 14:11:32 -07002872 } else if (stateName.equals(kRefreshStateName)) {
2873 // This state is actually NOT in the UI state.
2874 // We use this to allow Android to quickly set bool fRefresh.
2875 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002876 } else {
2877 SkDebugf("Unknown stateName: %s", stateName.c_str());
2878 }
2879}
Brian Osman79086b92017-02-10 13:36:16 -05002880
Hal Canaryb1f411a2019-08-29 10:39:22 -04002881bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002882 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002883}
2884
Hal Canaryb1f411a2019-08-29 10:39:22 -04002885bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002886 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002887 fWindow->inval();
2888 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002889 } else {
2890 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002891 }
Brian Osman79086b92017-02-10 13:36:16 -05002892}