blob: dc1c7c3c957de51ca3b3b8232202a3a3042a5086 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkGraphics.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkSurface.h"
Robert Phillipsed653392020-07-10 13:55:21 -040014#include "include/gpu/GrDirectContext.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050015#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkTo.h"
17#include "include/utils/SkPaintFilterCanvas.h"
18#include "src/core/SkColorSpacePriv.h"
19#include "src/core/SkImagePriv.h"
20#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkOSFile.h"
22#include "src/core/SkScan.h"
John Stilesdf078002020-07-14 09:44:57 -040023#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040025#include "src/core/SkTextBlobPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040026#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrGpu.h"
28#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070029#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Chris Daltonff18ff62020-12-07 17:39:26 -070031#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Adlai Hollerbcfc5542020-08-27 12:44:07 -040032#include "src/image/SkImage_Base.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/utils/SkJSONWriter.h"
34#include "src/utils/SkOSPath.h"
35#include "tools/Resources.h"
36#include "tools/ToolUtils.h"
37#include "tools/flags/CommandLineFlags.h"
38#include "tools/flags/CommonFlags.h"
39#include "tools/trace/EventTracingPriv.h"
40#include "tools/viewer/BisectSlide.h"
41#include "tools/viewer/GMSlide.h"
42#include "tools/viewer/ImageSlide.h"
43#include "tools/viewer/ParticlesSlide.h"
44#include "tools/viewer/SKPSlide.h"
45#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050046#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "tools/viewer/SlideDir.h"
48#include "tools/viewer/SvgSlide.h"
49#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070050
Chris Dalton17dc4182020-03-25 16:18:16 -060051#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040052#include <map>
53
Hal Canary8a001442018-09-19 11:31:27 -040054#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040055#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040056
Brian Osmanc85f1fa2020-06-16 15:11:34 -040057#ifdef SK_VULKAN
58#include "spirv-tools/libspirv.hpp"
59#endif
60
Florin Malita87ccf332018-05-04 12:23:24 -040061#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050062 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040063#endif
Florin Malita45cd2002020-06-09 14:00:54 -040064#if defined(SK_ENABLE_SKRIVE)
65 #include "tools/viewer/SkRiveSlide.h"
66#endif
Florin Malita87ccf332018-05-04 12:23:24 -040067
John Stiles8ef4d6c2021-03-05 16:01:45 -050068namespace SkSL {
John Stiles7247b482021-03-08 10:40:35 -050069extern bool gSkSLOptimizer;
70extern bool gSkSLInliner;
John Stiles8ef4d6c2021-03-05 16:01:45 -050071}
72
Brian Osman5e7fbfd2019-05-03 13:13:35 -040073class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
74public:
75 void compileError(const char* shader, const char* errors) override {
76 fShaders.push_back(SkString(shader));
77 fErrors.push_back(SkString(errors));
78 }
79
80 void reset() {
81 fShaders.reset();
82 fErrors.reset();
83 }
84
85 SkTArray<SkString> fShaders;
86 SkTArray<SkString> fErrors;
87};
88
89static CapturingShaderErrorHandler gShaderErrorHandler;
90
Brian Osmanf847f312020-06-18 14:18:27 -040091GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
92
jvanverth34524262016-05-04 13:49:13 -070093using namespace sk_app;
94
csmartdalton61cd31a2017-02-27 17:00:53 -070095static std::map<GpuPathRenderers, std::string> gPathRendererNames;
96
jvanverth9f372462016-04-06 06:08:59 -070097Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070098 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070099}
100
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600101static DEFINE_string(slide, "", "Start on this sample.");
102static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -0500103
Jim Van Verth682a2f42020-05-13 16:54:09 -0400104#ifdef SK_GL
105#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700106#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400107#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700108#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400109#ifdef SK_VULKAN
110#define VK_BACKEND_STR ", \"vk\""
111#else
112#define VK_BACKEND_STR
113#endif
114#ifdef SK_METAL
115#define MTL_BACKEND_STR ", \"mtl\""
116#else
117#define MTL_BACKEND_STR
118#endif
119#ifdef SK_DIRECT3D
120#define D3D_BACKEND_STR ", \"d3d\""
121#else
122#define D3D_BACKEND_STR
123#endif
124#ifdef SK_DAWN
125#define DAWN_BACKEND_STR ", \"dawn\""
126#else
127#define DAWN_BACKEND_STR
128#endif
129#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
130#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
131 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700132
Brian Osman2dd96932016-10-18 15:33:53 -0400133static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700134
Mike Klein5b3f3432019-03-21 11:42:21 -0500135static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
Chris Daltonfd708652021-03-17 21:10:17 -0600136static DEFINE_bool(dmsaa, false, "Use internal MSAA to render to non-MSAA surfaces?");
csmartdalton008b9d82017-02-22 12:00:42 -0700137
Mike Klein84836b72019-03-21 11:31:36 -0500138static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700139
Mike Klein84836b72019-03-21 11:31:36 -0500140static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400141
Mike Kleinc6142d82019-03-25 10:54:59 -0500142static DEFINE_string2(match, m, nullptr,
143 "[~][^]substring[$] [...] of name to run.\n"
144 "Multiple matches may be separated by spaces.\n"
145 "~ causes a matching name to always be skipped\n"
146 "^ requires the start of the name to match\n"
147 "$ requires the end of the name to match\n"
148 "^ and $ requires an exact match\n"
149 "If a name does not match any list entry,\n"
150 "it is skipped unless some list entry starts with ~");
151
Mike Klein19fb3972019-03-21 13:08:08 -0500152#if defined(SK_BUILD_FOR_ANDROID)
153 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500154 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
155 static DEFINE_string(lotties, "/data/local/tmp/lotties",
156 "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400157 static DEFINE_string(rives, "/data/local/tmp/rives",
158 "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500159#else
160 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500161 static DEFINE_string(skps, "skps", "Directory to read skps from.");
162 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400163 static DEFINE_string(rives, "rives", "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500164#endif
165
Mike Kleinc6142d82019-03-25 10:54:59 -0500166static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
167
168static DEFINE_int_2(threads, j, -1,
169 "Run threadsafe tests on a threadpool with this many extra threads, "
170 "defaulting to one extra thread per core.");
171
Jim Van Verth7b558182019-11-14 16:47:01 -0500172static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
173
Chris Daltonc8877332020-01-06 09:48:30 -0700174static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500175static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
176static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500177static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500178static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500179static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500180
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400181#ifndef SK_GL
182static_assert(false, "viewer requires GL backend for raster.")
183#endif
184
Brian Salomon194db172017-08-17 14:37:06 -0400185const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700186 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400187#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
188 "ANGLE",
189#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400190#ifdef SK_DAWN
191 "Dawn",
192#endif
jvanverth063ece72016-06-17 09:29:14 -0700193#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700194 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700195#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400196#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500197 "Metal",
198#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400199#ifdef SK_DIRECT3D
200 "Direct3D",
201#endif
csmartdalton578f0642017-02-24 16:04:47 -0700202 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700203};
204
bsalomon6c471f72016-07-26 12:56:32 -0700205static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400206#ifdef SK_DAWN
207 if (0 == strcmp(str, "dawn")) {
208 return sk_app::Window::kDawn_BackendType;
209 } else
210#endif
bsalomon6c471f72016-07-26 12:56:32 -0700211#ifdef SK_VULKAN
212 if (0 == strcmp(str, "vk")) {
213 return sk_app::Window::kVulkan_BackendType;
214 } else
215#endif
Brian Salomon194db172017-08-17 14:37:06 -0400216#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
217 if (0 == strcmp(str, "angle")) {
218 return sk_app::Window::kANGLE_BackendType;
219 } else
220#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400221#ifdef SK_METAL
222 if (0 == strcmp(str, "mtl")) {
223 return sk_app::Window::kMetal_BackendType;
224 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500225#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400226#ifdef SK_DIRECT3D
227 if (0 == strcmp(str, "d3d")) {
228 return sk_app::Window::kDirect3D_BackendType;
229 } else
230#endif
231
bsalomon6c471f72016-07-26 12:56:32 -0700232 if (0 == strcmp(str, "gl")) {
233 return sk_app::Window::kNativeGL_BackendType;
234 } else if (0 == strcmp(str, "sw")) {
235 return sk_app::Window::kRaster_BackendType;
236 } else {
237 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
238 return sk_app::Window::kRaster_BackendType;
239 }
240}
241
Brian Osmana109e392017-02-24 09:49:14 -0500242static SkColorSpacePrimaries gSrgbPrimaries = {
243 0.64f, 0.33f,
244 0.30f, 0.60f,
245 0.15f, 0.06f,
246 0.3127f, 0.3290f };
247
248static SkColorSpacePrimaries gAdobePrimaries = {
249 0.64f, 0.33f,
250 0.21f, 0.71f,
251 0.15f, 0.06f,
252 0.3127f, 0.3290f };
253
254static SkColorSpacePrimaries gP3Primaries = {
255 0.680f, 0.320f,
256 0.265f, 0.690f,
257 0.150f, 0.060f,
258 0.3127f, 0.3290f };
259
260static SkColorSpacePrimaries gRec2020Primaries = {
261 0.708f, 0.292f,
262 0.170f, 0.797f,
263 0.131f, 0.046f,
264 0.3127f, 0.3290f };
265
266struct NamedPrimaries {
267 const char* fName;
268 SkColorSpacePrimaries* fPrimaries;
269} gNamedPrimaries[] = {
270 { "sRGB", &gSrgbPrimaries },
271 { "AdobeRGB", &gAdobePrimaries },
272 { "P3", &gP3Primaries },
273 { "Rec. 2020", &gRec2020Primaries },
274};
275
276static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
277 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
278}
279
Brian Osman70d2f432017-11-08 09:54:10 -0500280static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
281 // In raster mode, we still use GL for the window.
282 // This lets us render the GUI faster (and correct).
283 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
284}
285
Jim Van Verth74826c82019-03-01 14:37:30 -0500286class NullSlide : public Slide {
287 SkISize getDimensions() const override {
288 return SkISize::Make(640, 480);
289 }
290
291 void draw(SkCanvas* canvas) override {
292 canvas->clear(0xffff11ff);
293 }
294};
295
John Stiles31964fd2020-05-05 16:05:47 -0400296static const char kName[] = "name";
297static const char kValue[] = "value";
298static const char kOptions[] = "options";
299static const char kSlideStateName[] = "Slide";
300static const char kBackendStateName[] = "Backend";
301static const char kMSAAStateName[] = "MSAA";
302static const char kPathRendererStateName[] = "Path renderer";
303static const char kSoftkeyStateName[] = "Softkey";
304static const char kSoftkeyHint[] = "Please select a softkey";
305static const char kON[] = "ON";
306static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700307
Mike Reed862818b2020-03-21 15:07:13 -0400308extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500309extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500310extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400311
jvanverth34524262016-05-04 13:49:13 -0700312Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500313 : fCurrentSlide(-1)
314 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500315 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400316 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500317 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500318 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500319 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500320 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400321 , fZoomWindowFixed(false)
322 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500323 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400324 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700325 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500326 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500327 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500328 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500329 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500330 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700331 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400332 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400333 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400334 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500335 , fTiled(false)
336 , fDrawTileBoundaries(false)
337 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400338 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700339{
Greg Daniel285db442016-10-14 09:12:53 -0400340 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700341
Chris Dalton37ae4b02019-12-28 14:51:11 -0700342 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600343 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500344 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500345 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600346 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600347 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500348 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700349
jvanverth2bb3b6d2016-04-08 07:24:09 -0700350 SkDebugf("Command line arguments: ");
351 for (int i = 1; i < argc; ++i) {
352 SkDebugf("%s ", argv[i]);
353 }
354 SkDebugf("\n");
355
Mike Klein88544fb2019-03-20 10:50:33 -0500356 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500357#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400358 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500359#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700360
Mike Reed862818b2020-03-21 15:07:13 -0400361 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500362 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500363 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400364
Mike Klein19cc0f62019-03-22 15:30:07 -0500365 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500366
Brian Osmanbc8150f2017-07-24 11:38:01 -0400367 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400368 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400369
bsalomon6c471f72016-07-26 12:56:32 -0700370 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700371 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700372
csmartdalton578f0642017-02-24 16:04:47 -0700373 DisplayParams displayParams;
374 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500375 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700376 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400377 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400378 displayParams.fGrContextOptions.fShaderCacheStrategy =
379 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400380 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
381 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltonfd708652021-03-17 21:10:17 -0600382 displayParams.fGrContextOptions.fAlwaysAntialias = FLAGS_dmsaa;
csmartdalton578f0642017-02-24 16:04:47 -0700383 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400384 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500385 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700386
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500387 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500388 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500389
Brian Osman56a24812017-12-19 11:15:16 -0500390 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500391 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500392 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
393 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
394 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
395
jvanverth9f372462016-04-06 06:08:59 -0700396 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700397 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500398 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500399 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500400 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700401
brianosman622c8d52016-05-10 06:50:49 -0700402 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500403 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
404 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
405 fWindow->inval();
406 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500407 // Command to jump directly to the slide picker and give it focus
408 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
409 this->fShowImGuiDebugWindow = true;
410 this->fShowSlidePicker = true;
411 fWindow->inval();
412 });
413 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400414 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500415 this->fShowImGuiDebugWindow = true;
416 this->fShowSlidePicker = true;
417 fWindow->inval();
418 });
Brian Osman79086b92017-02-10 13:36:16 -0500419 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
420 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
421 fWindow->inval();
422 });
Brian Osmanf6877092017-02-13 09:39:57 -0500423 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
424 this->fShowZoomWindow = !this->fShowZoomWindow;
425 fWindow->inval();
426 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400427 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
428 this->fZoomWindowFixed = !this->fZoomWindowFixed;
429 fWindow->inval();
430 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400431 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400432 DisplayParams params = fWindow->getRequestedDisplayParams();
433 params.fDisableVsync = !params.fDisableVsync;
434 fWindow->setRequestedDisplayParams(params);
435 this->updateTitle();
436 fWindow->inval();
437 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400438 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
439 DisplayParams params = fWindow->getRequestedDisplayParams();
440 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
441 fWindow->setRequestedDisplayParams(params);
442 this->updateTitle();
443 fWindow->inval();
444 });
Mike Reedf702ed42019-07-22 17:00:49 -0400445 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
446 fRefresh = !fRefresh;
447 fWindow->inval();
448 });
brianosman622c8d52016-05-10 06:50:49 -0700449 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500450 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700451 fWindow->inval();
452 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400453 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500454 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400455 this->updateTitle();
456 fWindow->inval();
457 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500458 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500459 switch (fColorMode) {
460 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500461 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500462 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500463 case ColorMode::kColorManaged8888:
464 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500465 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500466 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400467 this->setColorMode(ColorMode::kColorManagedF16Norm);
468 break;
469 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500470 this->setColorMode(ColorMode::kLegacy);
471 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500472 }
brianosman622c8d52016-05-10 06:50:49 -0700473 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700474 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
475 DisplayParams params = fWindow->getRequestedDisplayParams();
476 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
477 fWindow->setRequestedDisplayParams(params);
478 fWindow->inval();
479 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400480 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500481 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700482 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400483 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500484 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700485 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400486 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700487 this->changeZoomLevel(1.f / 32.f);
488 fWindow->inval();
489 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400490 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700491 this->changeZoomLevel(-1.f / 32.f);
492 fWindow->inval();
493 });
jvanverthaf236b52016-05-20 06:01:06 -0700494 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400495 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
496 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500497 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400498#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
499 if (newBackend == sk_app::Window::kVulkan_BackendType) {
500 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
501 sk_app::Window::kBackendTypeCount);
502 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
503 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500504 }
505#endif
Brian Osman621491e2017-02-28 15:45:01 -0500506 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700507 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500508 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
509 fSaveToSKP = true;
510 fWindow->inval();
511 });
Mike Reed376d8122019-03-14 11:39:02 -0400512 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
513 fShowSlideDimensions = !fShowSlideDimensions;
514 fWindow->inval();
515 });
Ben Wagner37c54032018-04-13 14:30:23 -0400516 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
517 DisplayParams params = fWindow->getRequestedDisplayParams();
518 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400519 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
520 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
521 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400522 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
523 } else {
524 switch (params.fSurfaceProps.pixelGeometry()) {
525 case kUnknown_SkPixelGeometry:
526 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
527 break;
528 case kRGB_H_SkPixelGeometry:
529 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
530 break;
531 case kBGR_H_SkPixelGeometry:
532 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
533 break;
534 case kRGB_V_SkPixelGeometry:
535 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
536 break;
537 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400538 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
539 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400540 break;
541 }
542 }
543 fWindow->setRequestedDisplayParams(params);
544 this->updateTitle();
545 fWindow->inval();
546 });
Ben Wagner9613e452019-01-23 10:34:59 -0500547 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500548 if (!fFontOverrides.fHinting) {
549 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400550 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500551 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500552 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400553 case SkFontHinting::kNone:
554 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500555 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400556 case SkFontHinting::kSlight:
557 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500558 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400559 case SkFontHinting::kNormal:
560 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500561 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400562 case SkFontHinting::kFull:
563 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500564 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500565 break;
566 }
567 }
568 this->updateTitle();
569 fWindow->inval();
570 });
571 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500572 if (!fPaintOverrides.fAntiAlias) {
573 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
574 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500575 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500576 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500577 } else {
578 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500579 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500580 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500581 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400582 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500583 break;
584 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500585 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500586 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400587 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500588 break;
589 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500590 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400591 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500592 break;
593 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500594 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
595 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500596 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
597 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500598 break;
599 }
600 }
601 this->updateTitle();
602 fWindow->inval();
603 });
Ben Wagner37c54032018-04-13 14:30:23 -0400604 fCommands.addCommand('D', "Modes", "DFT", [this]() {
605 DisplayParams params = fWindow->getRequestedDisplayParams();
606 uint32_t flags = params.fSurfaceProps.flags();
607 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
608 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
609 fWindow->setRequestedDisplayParams(params);
610 this->updateTitle();
611 fWindow->inval();
612 });
Ben Wagner9613e452019-01-23 10:34:59 -0500613 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500614 if (!fFontOverrides.fEdging) {
615 fFontOverrides.fEdging = true;
616 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500617 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500618 switch (fFont.getEdging()) {
619 case SkFont::Edging::kAlias:
620 fFont.setEdging(SkFont::Edging::kAntiAlias);
621 break;
622 case SkFont::Edging::kAntiAlias:
623 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
624 break;
625 case SkFont::Edging::kSubpixelAntiAlias:
626 fFont.setEdging(SkFont::Edging::kAlias);
627 fFontOverrides.fEdging = false;
628 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500629 }
630 }
631 this->updateTitle();
632 fWindow->inval();
633 });
Ben Wagner9613e452019-01-23 10:34:59 -0500634 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500635 if (!fFontOverrides.fSubpixel) {
636 fFontOverrides.fSubpixel = true;
637 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500638 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500639 if (!fFont.isSubpixel()) {
640 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500641 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500642 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500643 }
644 }
645 this->updateTitle();
646 fWindow->inval();
647 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400648 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
649 if (!fFontOverrides.fBaselineSnap) {
650 fFontOverrides.fBaselineSnap = true;
651 fFont.setBaselineSnap(false);
652 } else {
653 if (!fFont.isBaselineSnap()) {
654 fFont.setBaselineSnap(true);
655 } else {
656 fFontOverrides.fBaselineSnap = false;
657 }
658 }
659 this->updateTitle();
660 fWindow->inval();
661 });
Brian Osman805a7272018-05-02 15:40:20 -0400662 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
663 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
664 : kPerspective_Real;
665 this->updateTitle();
666 fWindow->inval();
667 });
668 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
669 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
670 : kPerspective_Off;
671 this->updateTitle();
672 fWindow->inval();
673 });
Brian Osman207d4102019-01-10 09:40:58 -0500674 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
675 fAnimTimer.togglePauseResume();
676 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400677 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
678 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500679 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400680 fWindow->inval();
681 });
Mike Reed59295352020-03-12 13:56:34 -0400682 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
683 fDrawViaSerialize = !fDrawViaSerialize;
684 this->updateTitle();
685 fWindow->inval();
686 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500687 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400688 gUseSkVMBlitter = !gUseSkVMBlitter;
689 this->updateTitle();
690 fWindow->inval();
691 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500692 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
693 gSkVMAllowJIT = !gSkVMAllowJIT;
694 this->updateTitle();
695 fWindow->inval();
696 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500697
jvanverth2bb3b6d2016-04-08 07:24:09 -0700698 // set up slides
699 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500700 if (FLAGS_list) {
701 this->listNames();
702 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700703
Brian Osman9bb47cf2018-04-26 15:55:00 -0400704 fPerspectivePoints[0].set(0, 0);
705 fPerspectivePoints[1].set(1, 0);
706 fPerspectivePoints[2].set(0, 1);
707 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700708 fAnimTimer.run();
709
Hal Canaryc465d132017-12-08 10:21:31 -0500710 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500711 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500712 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500713 }
714 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500715
jongdeok.kim804f17e2019-02-26 14:39:23 +0900716 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500717 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700718}
719
jvanverth34524262016-05-04 13:49:13 -0700720void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400721 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
722 static const struct {
723 const char* fExtension;
724 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500725 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400726 const SlideFactory fFactory;
727 } gExternalSlidesInfo[] = {
728 { ".skp", "skp-dir", FLAGS_skps,
729 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
730 return sk_make_sp<SKPSlide>(name, path);}
731 },
732 { ".jpg", "jpg-dir", FLAGS_jpgs,
733 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
734 return sk_make_sp<ImageSlide>(name, path);}
735 },
Florin Malita87ccf332018-05-04 12:23:24 -0400736#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400737 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400738 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
739 return sk_make_sp<SkottieSlide>(name, path);}
740 },
Florin Malita87ccf332018-05-04 12:23:24 -0400741#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400742 #if defined(SK_ENABLE_SKRIVE)
743 { ".flr", "skrive-dir", FLAGS_rives,
744 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
745 return sk_make_sp<SkRiveSlide>(name, path);}
746 },
747 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400748#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400749 { ".svg", "svg-dir", FLAGS_svgs,
750 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
751 return sk_make_sp<SvgSlide>(name, path);}
752 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400753#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400754 };
jvanverthc265a922016-04-08 12:51:45 -0700755
Brian Salomon343553a2018-09-05 15:41:23 -0400756 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700757
Mike Klein88544fb2019-03-20 10:50:33 -0500758 const auto addSlide =
759 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
760 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
761 return;
762 }
liyuqian6f163d22016-06-13 12:26:45 -0700763
Mike Klein88544fb2019-03-20 10:50:33 -0500764 if (auto slide = fact(name, path)) {
765 dirSlides.push_back(slide);
766 fSlides.push_back(std::move(slide));
767 }
768 };
Florin Malita76a076b2018-02-15 18:40:48 -0500769
Florin Malita38792ce2018-05-08 10:36:18 -0400770 if (!FLAGS_file.isEmpty()) {
771 // single file mode
772 const SkString file(FLAGS_file[0]);
773
774 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
775 for (const auto& sinfo : gExternalSlidesInfo) {
776 if (file.endsWith(sinfo.fExtension)) {
777 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
778 return;
779 }
780 }
781
782 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
783 } else {
784 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
785 }
786
787 return;
788 }
789
790 // Bisect slide.
791 if (!FLAGS_bisect.isEmpty()) {
792 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500793 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400794 if (FLAGS_bisect.count() >= 2) {
795 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
796 bisect->onChar(*ch);
797 }
798 }
799 fSlides.push_back(std::move(bisect));
800 }
801 }
802
803 // GMs
804 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400805 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400806 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500807 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400808 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400809 fSlides.push_back(std::move(slide));
810 }
Florin Malita38792ce2018-05-08 10:36:18 -0400811 }
812 // reverse gms
813 int numGMs = fSlides.count() - firstGM;
814 for (int i = 0; i < numGMs/2; ++i) {
815 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
816 }
817
818 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400819 for (const SampleFactory factory : SampleRegistry::Range()) {
820 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500821 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400822 fSlides.push_back(slide);
823 }
Florin Malita38792ce2018-05-08 10:36:18 -0400824 }
825
Brian Osman7c979f52019-02-12 13:27:51 -0500826 // Particle demo
827 {
828 // TODO: Convert this to a sample
829 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500830 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500831 fSlides.push_back(std::move(slide));
832 }
833 }
834
Brian Osmand927bd22019-12-18 11:23:12 -0500835 // Runtime shader editor
836 {
837 sk_sp<Slide> slide(new SkSLSlide());
838 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
839 fSlides.push_back(std::move(slide));
840 }
841 }
842
Florin Malita0ffa3222018-04-05 14:34:45 -0400843 for (const auto& info : gExternalSlidesInfo) {
844 for (const auto& flag : info.fFlags) {
845 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
846 // single file
847 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
848 } else {
849 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400850 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400851 SkTArray<SkString> sortedFilenames;
852 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400853 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400854 sortedFilenames.push_back(name);
855 }
856 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400857 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000858 [](const SkString& a, const SkString& b) {
859 return strcmp(a.c_str(), b.c_str()) < 0;
860 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400861 }
862 for (const SkString& filename : sortedFilenames) {
863 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
864 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400865 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400866 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400867 if (!dirSlides.empty()) {
868 fSlides.push_back(
869 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
870 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500871 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400872 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400873 }
874 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500875
876 if (!fSlides.count()) {
877 sk_sp<Slide> slide(new NullSlide());
878 fSlides.push_back(std::move(slide));
879 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700880}
881
882
jvanverth34524262016-05-04 13:49:13 -0700883Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400884 for(auto& slide : fSlides) {
885 slide->gpuTeardown();
886 }
887
jvanverth9f372462016-04-06 06:08:59 -0700888 fWindow->detach();
889 delete fWindow;
890}
891
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500892struct SkPaintTitleUpdater {
893 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
894 void append(const char* s) {
895 if (fCount == 0) {
896 fTitle->append(" {");
897 } else {
898 fTitle->append(", ");
899 }
900 fTitle->append(s);
901 ++fCount;
902 }
903 void done() {
904 if (fCount > 0) {
905 fTitle->append("}");
906 }
907 }
908 SkString* fTitle;
909 int fCount;
910};
911
brianosman05de2162016-05-06 13:28:57 -0700912void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700913 if (!fWindow) {
914 return;
915 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500916 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700917 return; // Surface hasn't been created yet.
918 }
919
jvanverth34524262016-05-04 13:49:13 -0700920 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700921 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700922
Mike Kleine5acd752019-03-22 09:57:16 -0500923 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400924 if (gSkForceAnalyticAA) {
925 title.append(" <FAAA>");
926 } else {
927 title.append(" <AAA>");
928 }
929 }
Mike Reed59295352020-03-12 13:56:34 -0400930 if (fDrawViaSerialize) {
931 title.append(" <serialize>");
932 }
Mike Reed862818b2020-03-21 15:07:13 -0400933 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500934 title.append(" <SkVMBlitter>");
935 }
936 if (!gSkVMAllowJIT) {
937 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400938 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400939
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500940 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500941 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
942 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400943 const char* on, const char* off)
944 {
Ben Wagner9613e452019-01-23 10:34:59 -0500945 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400946 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500947 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400948 };
949
Ben Wagner9613e452019-01-23 10:34:59 -0500950 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
951 const char* on, const char* off)
952 {
953 if (fFontOverrides.*flag) {
954 paintTitle.append((fFont.*isFlag)() ? on : off);
955 }
956 };
957
958 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
959 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
960
961 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
962 "Force Autohint", "No Force Autohint");
963 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400964 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500965 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
966 "Linear Metrics", "Non-Linear Metrics");
967 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
968 "Bitmap Text", "No Bitmap Text");
969 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
970
971 if (fFontOverrides.fEdging) {
972 switch (fFont.getEdging()) {
973 case SkFont::Edging::kAlias:
974 paintTitle.append("Alias Text");
975 break;
976 case SkFont::Edging::kAntiAlias:
977 paintTitle.append("Antialias Text");
978 break;
979 case SkFont::Edging::kSubpixelAntiAlias:
980 paintTitle.append("Subpixel Antialias Text");
981 break;
982 }
983 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400984
Mike Reed3ae47332019-01-04 10:11:46 -0500985 if (fFontOverrides.fHinting) {
986 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400987 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500988 paintTitle.append("No Hinting");
989 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400990 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500991 paintTitle.append("Slight Hinting");
992 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400993 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500994 paintTitle.append("Normal Hinting");
995 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400996 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500997 paintTitle.append("Full Hinting");
998 break;
999 }
1000 }
1001 paintTitle.done();
1002
Brian Osman92004802017-03-06 11:47:26 -05001003 switch (fColorMode) {
1004 case ColorMode::kLegacy:
1005 title.append(" Legacy 8888");
1006 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001007 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001008 title.append(" ColorManaged 8888");
1009 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001010 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001011 title.append(" ColorManaged F16");
1012 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001013 case ColorMode::kColorManagedF16Norm:
1014 title.append(" ColorManaged F16 Norm");
1015 break;
Brian Osman92004802017-03-06 11:47:26 -05001016 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001017
Brian Osman92004802017-03-06 11:47:26 -05001018 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001019 int curPrimaries = -1;
1020 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1021 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1022 curPrimaries = i;
1023 break;
1024 }
1025 }
Brian Osman03115dc2018-11-26 13:55:19 -05001026 title.appendf(" %s Gamma %f",
1027 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001028 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001029 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001030
Ben Wagner37c54032018-04-13 14:30:23 -04001031 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001032 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001033 switch (params.fSurfaceProps.pixelGeometry()) {
1034 case kUnknown_SkPixelGeometry:
1035 title.append( " Flat");
1036 break;
1037 case kRGB_H_SkPixelGeometry:
1038 title.append( " RGB");
1039 break;
1040 case kBGR_H_SkPixelGeometry:
1041 title.append( " BGR");
1042 break;
1043 case kRGB_V_SkPixelGeometry:
1044 title.append( " RGBV");
1045 break;
1046 case kBGR_V_SkPixelGeometry:
1047 title.append( " BGRV");
1048 break;
1049 }
1050 }
1051
1052 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1053 title.append(" DFT");
1054 }
1055
csmartdalton578f0642017-02-24 16:04:47 -07001056 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001057 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001058 int msaa = fWindow->sampleCount();
1059 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001060 title.appendf(" MSAA: %i", msaa);
1061 }
1062 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001063
1064 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001065 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001066 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1067 }
1068
Brian Osman805a7272018-05-02 15:40:20 -04001069 if (kPerspective_Real == fPerspectiveMode) {
1070 title.append(" Perpsective (Real)");
1071 } else if (kPerspective_Fake == fPerspectiveMode) {
1072 title.append(" Perspective (Fake)");
1073 }
1074
brianosman05de2162016-05-06 13:28:57 -07001075 fWindow->setTitle(title.c_str());
1076}
1077
Florin Malitaab99c342018-01-16 16:23:03 -05001078int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001079
1080 if (!FLAGS_slide.isEmpty()) {
1081 int count = fSlides.count();
1082 for (int i = 0; i < count; i++) {
1083 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001084 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001085 }
1086 }
1087
1088 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1089 this->listNames();
1090 }
1091
Florin Malitaab99c342018-01-16 16:23:03 -05001092 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001093}
1094
Florin Malitaab99c342018-01-16 16:23:03 -05001095void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001096 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001097 for (const auto& slide : fSlides) {
1098 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001099 }
1100}
1101
Florin Malitaab99c342018-01-16 16:23:03 -05001102void Viewer::setCurrentSlide(int slide) {
1103 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001104
Florin Malitaab99c342018-01-16 16:23:03 -05001105 if (slide == fCurrentSlide) {
1106 return;
1107 }
1108
1109 if (fCurrentSlide >= 0) {
1110 fSlides[fCurrentSlide]->unload();
1111 }
1112
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001113 SkScalar scaleFactor = 1.0;
1114 if (fApplyBackingScale) {
1115 scaleFactor = fWindow->scaleFactor();
1116 }
1117 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1118 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001119 fCurrentSlide = slide;
1120 this->setupCurrentSlide();
1121}
1122
1123void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001124 if (fCurrentSlide >= 0) {
1125 // prepare dimensions for image slides
1126 fGesture.resetTouchState();
1127 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001128
Jim Van Verth0848fb02018-01-22 13:39:30 -05001129 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1130 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1131 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001132
Jim Van Verth0848fb02018-01-22 13:39:30 -05001133 // Start with a matrix that scales the slide to the available screen space
1134 if (fWindow->scaleContentToFit()) {
1135 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001136 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1137 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001138 }
liyuqiane46e4f02016-05-20 07:32:19 -07001139 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001140
1141 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001142 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001143
1144 this->updateTitle();
1145 this->updateUIState();
1146
1147 fStatsLayer.resetMeasurements();
1148
1149 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001150 }
jvanverthc265a922016-04-08 12:51:45 -07001151}
1152
Brian Osmanaba642c2020-02-06 12:52:25 -05001153#define MAX_ZOOM_LEVEL 8.0f
1154#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001155
jvanverth34524262016-05-04 13:49:13 -07001156void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001157 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001158 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001159 this->preTouchMatrixChanged();
1160}
Yuqian Li755778c2018-03-28 16:23:31 -04001161
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001162void Viewer::preTouchMatrixChanged() {
1163 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001164 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1165 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1166 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1167 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1168}
1169
Brian Osman805a7272018-05-02 15:40:20 -04001170SkMatrix Viewer::computePerspectiveMatrix() {
1171 SkScalar w = fWindow->width(), h = fWindow->height();
1172 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1173 SkPoint perspPts[4] = {
1174 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1175 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1176 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1177 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1178 };
1179 SkMatrix m;
1180 m.setPolyToPoly(orthoPts, perspPts, 4);
1181 return m;
1182}
1183
Yuqian Li755778c2018-03-28 16:23:31 -04001184SkMatrix Viewer::computePreTouchMatrix() {
1185 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001186
1187 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001188 if (fApplyBackingScale) {
1189 zoomScale *= fWindow->scaleFactor();
1190 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001191 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001192 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001193
1194 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1195 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001196
Brian Osman805a7272018-05-02 15:40:20 -04001197 if (kPerspective_Real == fPerspectiveMode) {
1198 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001199 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001200 }
1201
Yuqian Li755778c2018-03-28 16:23:31 -04001202 return m;
jvanverthc265a922016-04-08 12:51:45 -07001203}
1204
liyuqiand3cdbca2016-05-17 12:44:20 -07001205SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001206 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001207 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001208 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001209 return m;
jvanverthc265a922016-04-08 12:51:45 -07001210}
1211
Brian Osman621491e2017-02-28 15:45:01 -05001212void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001213 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001214 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001215 fBackendType = backendType;
1216
Robert Phillipse9229532020-06-26 10:10:49 -04001217 // The active context is going away in 'detach'
1218 for(auto& slide : fSlides) {
1219 slide->gpuTeardown();
1220 }
1221
Brian Osman621491e2017-02-28 15:45:01 -05001222 fWindow->detach();
1223
Brian Osman70d2f432017-11-08 09:54:10 -05001224#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001225 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1226 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001227 DisplayParams params = fWindow->getRequestedDisplayParams();
1228 delete fWindow;
1229 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001230
Brian Osman70d2f432017-11-08 09:54:10 -05001231 // re-register callbacks
1232 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001233 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001234 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001235 fWindow->pushLayer(&fImGuiLayer);
1236
Brian Osman70d2f432017-11-08 09:54:10 -05001237 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1238 // will still include our correct sample count. But the re-created fWindow will lose that
1239 // information. On Windows, we need to re-create the window when changing sample count,
1240 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1241 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1242 // as if we had never changed windows at all).
1243 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001244#endif
1245
Brian Osman70d2f432017-11-08 09:54:10 -05001246 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001247}
1248
Brian Osman92004802017-03-06 11:47:26 -05001249void Viewer::setColorMode(ColorMode colorMode) {
1250 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001251 this->updateTitle();
1252 fWindow->inval();
1253}
1254
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001255class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1256public:
Mike Reed3ae47332019-01-04 10:11:46 -05001257 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1258 SkFont* font, Viewer::SkFontFields* ffields)
1259 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001260 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001261 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1262 sk_sp<SkTextBlob>* cache) {
1263 bool blobWillChange = false;
1264 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001265 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1266 bool shouldDraw = this->filterFont(&filteredFont);
1267 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001268 blobWillChange = true;
1269 break;
1270 }
1271 }
1272 if (!blobWillChange) {
1273 return blob;
1274 }
1275
1276 SkTextBlobBuilder builder;
1277 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001278 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1279 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001280 if (!shouldDraw) {
1281 continue;
1282 }
1283
Mike Reed3ae47332019-01-04 10:11:46 -05001284 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001285
Ben Wagner41e40472018-09-24 13:01:54 -04001286 const SkTextBlobBuilder::RunBuffer& runBuffer
1287 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001288 ? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(),
1289 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001290 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001291 ? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(),
1292 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001293 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001294 ? builder.allocRunTextPos(font, it.glyphCount(), it.textSize())
Ben Wagnere5736262021-02-08 16:52:08 -05001295 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001296 ? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001297 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1298 uint32_t glyphCount = it.glyphCount();
1299 if (it.glyphs()) {
1300 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1301 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1302 }
1303 if (it.pos()) {
1304 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001305 unsigned posPerGlyph = it.scalarsPerGlyph();
1306 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001307 }
1308 if (it.text()) {
1309 size_t textSize = sizeof(decltype(*it.text()));
1310 uint32_t textCount = it.textSize();
1311 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1312 }
1313 if (it.clusters()) {
1314 size_t clusterSize = sizeof(decltype(*it.clusters()));
1315 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1316 }
1317 }
1318 *cache = builder.make();
1319 return cache->get();
1320 }
1321 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1322 const SkPaint& paint) override {
1323 sk_sp<SkTextBlob> cache;
1324 this->SkPaintFilterCanvas::onDrawTextBlob(
1325 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1326 }
Mike Reed3ae47332019-01-04 10:11:46 -05001327 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001328 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001329 font->writable()->setSize(fFont->getSize());
1330 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001331 if (fFontOverrides->fScaleX) {
1332 font->writable()->setScaleX(fFont->getScaleX());
1333 }
1334 if (fFontOverrides->fSkewX) {
1335 font->writable()->setSkewX(fFont->getSkewX());
1336 }
Mike Reed3ae47332019-01-04 10:11:46 -05001337 if (fFontOverrides->fHinting) {
1338 font->writable()->setHinting(fFont->getHinting());
1339 }
Ben Wagner9613e452019-01-23 10:34:59 -05001340 if (fFontOverrides->fEdging) {
1341 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001342 }
Ben Wagner9613e452019-01-23 10:34:59 -05001343 if (fFontOverrides->fEmbolden) {
1344 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001345 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001346 if (fFontOverrides->fBaselineSnap) {
1347 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1348 }
Ben Wagner9613e452019-01-23 10:34:59 -05001349 if (fFontOverrides->fLinearMetrics) {
1350 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001351 }
Ben Wagner9613e452019-01-23 10:34:59 -05001352 if (fFontOverrides->fSubpixel) {
1353 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001354 }
Ben Wagner9613e452019-01-23 10:34:59 -05001355 if (fFontOverrides->fEmbeddedBitmaps) {
1356 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001357 }
Ben Wagner9613e452019-01-23 10:34:59 -05001358 if (fFontOverrides->fForceAutoHinting) {
1359 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001360 }
Ben Wagner9613e452019-01-23 10:34:59 -05001361
Mike Reed3ae47332019-01-04 10:11:46 -05001362 return true;
1363 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001364 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001365 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001366 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001367 }
Ben Wagner9613e452019-01-23 10:34:59 -05001368 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001369 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001370 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001371 if (fPaintOverrides->fStyle) {
1372 paint.setStyle(fPaint->getStyle());
1373 }
1374 if (fPaintOverrides->fWidth) {
1375 paint.setStrokeWidth(fPaint->getStrokeWidth());
1376 }
1377 if (fPaintOverrides->fMiterLimit) {
1378 paint.setStrokeMiter(fPaint->getStrokeMiter());
1379 }
1380 if (fPaintOverrides->fCapType) {
1381 paint.setStrokeCap(fPaint->getStrokeCap());
1382 }
1383 if (fPaintOverrides->fJoinType) {
1384 paint.setStrokeJoin(fPaint->getStrokeJoin());
1385 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001386 return true;
1387 }
1388 SkPaint* fPaint;
1389 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001390 SkFont* fFont;
1391 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001392};
1393
Robert Phillips9882dae2019-03-04 11:00:10 -05001394void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001395 if (fCurrentSlide < 0) {
1396 return;
1397 }
1398
Robert Phillips9882dae2019-03-04 11:00:10 -05001399 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001400
Brian Osmanf750fbc2017-02-08 10:47:28 -05001401 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001402 SkSurface* slideSurface = surface;
1403 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001404 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001405
Brian Osmane0d4fba2017-03-15 10:24:55 -04001406 // 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 -05001407 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001408 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001409 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001410 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001411 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001412 }
1413
Brian Osman3ac99cf2017-12-01 11:23:53 -05001414 if (fSaveToSKP) {
1415 SkPictureRecorder recorder;
1416 SkCanvas* recorderCanvas = recorder.beginRecording(
1417 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001418 fSlides[fCurrentSlide]->draw(recorderCanvas);
1419 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1420 SkFILEWStream stream("sample_app.skp");
1421 picture->serialize(&stream);
1422 fSaveToSKP = false;
1423 }
1424
Brian Osmane9ed0f02018-11-26 14:50:05 -05001425 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001426 SkColorType colorType;
1427 switch (fColorMode) {
1428 case ColorMode::kLegacy:
1429 case ColorMode::kColorManaged8888:
1430 colorType = kN32_SkColorType;
1431 break;
1432 case ColorMode::kColorManagedF16:
1433 colorType = kRGBA_F16_SkColorType;
1434 break;
1435 case ColorMode::kColorManagedF16Norm:
1436 colorType = kRGBA_F16Norm_SkColorType;
1437 break;
1438 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001439
1440 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001441 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001442 slideCanvas->getProps(&props);
1443
Brian Osmane9ed0f02018-11-26 14:50:05 -05001444 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1445 return Window::kRaster_BackendType == this->fBackendType
1446 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001447 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001448 };
1449
Brian Osman03115dc2018-11-26 13:55:19 -05001450 // We need to render offscreen if we're...
1451 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1452 // ... in any raster mode, because the window surface is actually GL
1453 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001454 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001455 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001456 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001457 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001458 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001459 colorSpace != nullptr ||
1460 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001461
Brian Osmane9ed0f02018-11-26 14:50:05 -05001462 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001463 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001464 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001465 }
1466
Mike Reed59295352020-03-12 13:56:34 -04001467 SkPictureRecorder recorder;
1468 SkCanvas* recorderRestoreCanvas = nullptr;
1469 if (fDrawViaSerialize) {
1470 recorderRestoreCanvas = slideCanvas;
1471 slideCanvas = recorder.beginRecording(
1472 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1473 }
1474
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001475 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001476 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001477 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001478 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001479 if (fTiled) {
1480 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1481 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001482 for (int y = 0; y < fWindow->height(); y += tileH) {
1483 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001484 SkAutoCanvasRestore acr(slideCanvas, true);
1485 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1486 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001487 }
1488 }
1489
1490 // Draw borders between tiles
1491 if (fDrawTileBoundaries) {
1492 SkPaint border;
1493 border.setColor(0x60FF00FF);
1494 border.setStyle(SkPaint::kStroke_Style);
1495 for (int y = 0; y < fWindow->height(); y += tileH) {
1496 for (int x = 0; x < fWindow->width(); x += tileW) {
1497 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1498 }
1499 }
1500 }
1501 } else {
1502 slideCanvas->concat(this->computeMatrix());
1503 if (kPerspective_Real == fPerspectiveMode) {
1504 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1505 }
Mike Reed3ae47332019-01-04 10:11:46 -05001506 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001507 fSlides[fCurrentSlide]->draw(&filterCanvas);
1508 }
Brian Osman56a24812017-12-19 11:15:16 -05001509 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001510 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001511
Mike Reed59295352020-03-12 13:56:34 -04001512 if (recorderRestoreCanvas) {
1513 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1514 auto data = picture->serialize();
1515 slideCanvas = recorderRestoreCanvas;
1516 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1517 }
1518
Brian Osman1df161a2017-02-09 12:10:20 -05001519 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001520 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001521 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001522 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001523
1524 // If we rendered offscreen, snap an image and push the results to the window's canvas
1525 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001526 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001527
Robert Phillips9882dae2019-03-04 11:00:10 -05001528 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001529 SkPaint paint;
1530 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001531 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001532 int prePerspectiveCount = canvas->save();
1533 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001534 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001535 canvas->clear(SK_ColorWHITE);
1536 canvas->concat(this->computePerspectiveMatrix());
1537 }
Mike Reedb339d052021-01-28 11:20:41 -05001538 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001539 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001540 }
Mike Reed376d8122019-03-14 11:39:02 -04001541
1542 if (fShowSlideDimensions) {
1543 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1544 SkPaint paint;
1545 paint.setColor(0x40FFFF00);
1546 surface->getCanvas()->drawRect(r, paint);
1547 }
liyuqian6f163d22016-06-13 12:26:45 -07001548}
1549
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001550void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001551 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001552 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001553}
Jim Van Verth6f449692017-02-14 15:16:46 -05001554
Robert Phillips9882dae2019-03-04 11:00:10 -05001555void Viewer::onPaint(SkSurface* surface) {
1556 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001557
Robert Phillips9882dae2019-03-04 11:00:10 -05001558 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001559
Brian Osmand67e5182017-12-08 16:46:09 -05001560 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001561
Greg Daniel427d8eb2020-09-28 15:04:18 -04001562 fLastImage.reset();
1563
Robert Phillipsed653392020-07-10 13:55:21 -04001564 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001565 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001566 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001567 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001568}
1569
Ben Wagnera1915972018-08-09 15:06:19 -04001570void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001571 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001572 SkScalar scaleFactor = 1.0;
1573 if (fApplyBackingScale) {
1574 scaleFactor = fWindow->scaleFactor();
1575 }
1576 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001577 }
Ben Wagnera1915972018-08-09 15:06:19 -04001578}
1579
Florin Malitacefc1b92018-02-19 21:43:47 -05001580SkPoint Viewer::mapEvent(float x, float y) {
1581 const auto m = this->computeMatrix();
1582 SkMatrix inv;
1583
1584 SkAssertResult(m.invert(&inv));
1585
1586 return inv.mapXY(x, y);
1587}
1588
Hal Canaryb1f411a2019-08-29 10:39:22 -04001589bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001590 if (GestureDevice::kMouse == fGestureDevice) {
1591 return false;
1592 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001593
1594 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001595 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001596 fWindow->inval();
1597 return true;
1598 }
1599
liyuqiand3cdbca2016-05-17 12:44:20 -07001600 void* castedOwner = reinterpret_cast<void*>(owner);
1601 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001602 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001603 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001604#if defined(SK_BUILD_FOR_IOS)
1605 // TODO: move IOS swipe detection higher up into the platform code
1606 SkPoint dir;
1607 if (fGesture.isFling(&dir)) {
1608 // swiping left or right
1609 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1610 if (dir.fX < 0) {
1611 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1612 fCurrentSlide + 1 : 0);
1613 } else {
1614 this->setCurrentSlide(fCurrentSlide > 0 ?
1615 fCurrentSlide - 1 : fSlides.count() - 1);
1616 }
1617 }
1618 fGesture.reset();
1619 }
1620#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001621 break;
1622 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001623 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001624 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001625 break;
1626 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001627 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001628 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001629 break;
1630 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001631 default: {
1632 // kLeft and kRight are only for swipes
1633 SkASSERT(false);
1634 break;
1635 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001636 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001637 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001638 fWindow->inval();
1639 return true;
1640}
1641
Hal Canaryb1f411a2019-08-29 10:39:22 -04001642bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001643 if (GestureDevice::kTouch == fGestureDevice) {
1644 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001645 }
Brian Osman16c81a12017-12-20 11:58:34 -05001646
Florin Malitacefc1b92018-02-19 21:43:47 -05001647 const auto slidePt = this->mapEvent(x, y);
1648 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1649 fWindow->inval();
1650 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001651 }
1652
1653 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001654 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001655 fGesture.touchEnd(nullptr);
1656 break;
1657 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001658 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001659 fGesture.touchBegin(nullptr, x, y);
1660 break;
1661 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001662 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001663 fGesture.touchMoved(nullptr, x, y);
1664 break;
1665 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001666 default: {
1667 SkASSERT(false); // shouldn't see kRight or kLeft here
1668 break;
1669 }
Brian Osman16c81a12017-12-20 11:58:34 -05001670 }
1671 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1672
Hal Canaryb1f411a2019-08-29 10:39:22 -04001673 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001674 fWindow->inval();
1675 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001676 return true;
1677}
1678
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001679bool Viewer::onFling(skui::InputState state) {
1680 if (skui::InputState::kRight == state) {
1681 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1682 return true;
1683 } else if (skui::InputState::kLeft == state) {
1684 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1685 return true;
1686 }
1687 return false;
1688}
1689
1690bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1691 switch (state) {
1692 case skui::InputState::kDown:
1693 fGesture.startZoom();
1694 return true;
1695 break;
1696 case skui::InputState::kMove:
1697 fGesture.updateZoom(scale, x, y, x, y);
1698 return true;
1699 break;
1700 case skui::InputState::kUp:
1701 fGesture.endZoom();
1702 return true;
1703 break;
1704 default:
1705 SkASSERT(false);
1706 break;
1707 }
1708
1709 return false;
1710}
1711
Brian Osmana109e392017-02-24 09:49:14 -05001712static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001713 // The gamut image covers a (0.8 x 0.9) shaped region
1714 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001715
1716 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1717 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1718 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001719 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1720 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1721 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001722
Brian Osman535c5e32019-02-09 16:32:58 -05001723 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1724 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1725 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1726 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1727 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001728}
1729
Ben Wagner3627d2e2018-06-26 14:23:20 -04001730static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001731 ImGui::DragCanvas dc(pt);
1732 dc.fillColor(IM_COL32(0, 0, 0, 128));
1733 dc.dragPoint(pt);
1734 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001735}
1736
Brian Osman9bb47cf2018-04-26 15:55:00 -04001737static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001738 ImGui::DragCanvas dc(pts);
1739 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001740
Brian Osman535c5e32019-02-09 16:32:58 -05001741 for (int i = 0; i < 4; ++i) {
1742 dc.dragPoint(pts + i);
1743 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001744
Brian Osman535c5e32019-02-09 16:32:58 -05001745 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1746 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1747 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1748 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001749
Brian Osman535c5e32019-02-09 16:32:58 -05001750 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001751}
1752
John Stiles38b7d2f2020-06-24 12:13:31 -04001753static SkSL::String build_sksl_highlight_shader() {
1754 return SkSL::String("out half4 sk_FragColor;\n"
1755 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1756}
1757
1758static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1759 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1760 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1761 size_t pos = inShader.rfind("return *_out;\n");
1762 if (pos == std::string::npos) {
1763 return inShader;
1764 }
1765
1766 SkSL::String replacementShader = inShader;
1767 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1768 return replacementShader;
1769}
1770
1771static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1772 const char* versionDecl = shaderCaps.versionDeclString();
1773 SkSL::String highlight = versionDecl ? versionDecl : "";
1774 if (shaderCaps.usesPrecisionModifiers()) {
1775 highlight.append("precision mediump float;\n");
1776 }
1777 highlight.appendf("out vec4 sk_FragColor;\n"
1778 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1779 return highlight;
1780}
1781
Brian Osmand67e5182017-12-08 16:46:09 -05001782void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001783 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1784 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001785 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001786 }
1787
1788 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001789 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1790 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001791 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001792 DisplayParams params = fWindow->getRequestedDisplayParams();
1793 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001794 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001795
Brian Osmana109e392017-02-24 09:49:14 -05001796 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1797 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001798 if (ImGui::CollapsingHeader("Backend")) {
1799 int newBackend = static_cast<int>(fBackendType);
1800 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1801 ImGui::SameLine();
1802 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001803#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1804 ImGui::SameLine();
1805 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1806#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001807#if defined(SK_DAWN)
1808 ImGui::SameLine();
1809 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1810#endif
John Stilesf7da9232020-11-19 19:58:14 -05001811#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001812 ImGui::SameLine();
1813 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1814#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001815#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001816 ImGui::SameLine();
1817 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1818#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001819#if defined(SK_DIRECT3D)
1820 ImGui::SameLine();
1821 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1822#endif
Brian Osman621491e2017-02-28 15:45:01 -05001823 if (newBackend != fBackendType) {
1824 fDeferredActions.push_back([=]() {
1825 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1826 });
1827 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001828
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001829 bool* wire = &params.fGrContextOptions.fWireframeMode;
1830 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1831 paramsChanged = true;
1832 }
Brian Salomon99a33902017-03-07 15:16:34 -05001833
Brian Osman28b12522017-03-08 17:10:24 -05001834 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001835 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001836 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001837 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1838 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1839 1;
1840
1841 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1842 if (maxMSAA >= 4) {
1843 ImGui::Text("MSAA: ");
1844
1845 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1846 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1847 // don't show it in the list.
1848 if (curMSAA == 2) {
1849 continue;
1850 }
1851 ImGui::SameLine();
1852 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1853 &sampleCount, curMSAA);
1854 }
1855 }
Brian Osman28b12522017-03-08 17:10:24 -05001856
1857 if (sampleCount != params.fMSAASampleCount) {
1858 params.fMSAASampleCount = sampleCount;
1859 paramsChanged = true;
1860 }
1861 }
1862
Ben Wagner37c54032018-04-13 14:30:23 -04001863 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001864 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001865 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1866 }
1867 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1868 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1869 {
1870 uint32_t flags = params.fSurfaceProps.flags();
1871 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001872 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1873 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1874 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001875 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001876 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001877 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1878 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1879 }
1880 paramsChanged = true;
1881 }
1882
1883 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1884 if (ImGui::Checkbox("DFT", &useDFT)) {
1885 uint32_t flags = params.fSurfaceProps.flags();
1886 if (useDFT) {
1887 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1888 } else {
1889 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1890 }
1891 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1892 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1893 paramsChanged = true;
1894 }
1895
Brian Osman8a9de3d2017-03-01 14:59:05 -05001896 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001897 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001898 auto prButton = [&](GpuPathRenderers x) {
1899 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001900 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1901 params.fGrContextOptions.fGpuPathRenderers = x;
1902 paramsChanged = true;
1903 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001904 }
1905 };
1906
1907 if (!ctx) {
1908 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001909 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001910 const auto* caps = ctx->priv().caps();
1911 prButton(GpuPathRenderers::kDefault);
1912 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001913 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001914 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001915 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001916 if (caps->shaderCaps()->pathRenderingSupport()) {
1917 prButton(GpuPathRenderers::kStencilAndCover);
1918 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001919 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001920 if (1 == fWindow->sampleCount()) {
1921 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1922 prButton(GpuPathRenderers::kCoverageCounting);
1923 }
1924 prButton(GpuPathRenderers::kSmall);
1925 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001926 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001927 prButton(GpuPathRenderers::kNone);
1928 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001929 ImGui::TreePop();
1930 }
Brian Osman621491e2017-02-28 15:45:01 -05001931 }
1932
Ben Wagner964571d2019-03-08 12:35:06 -05001933 if (ImGui::CollapsingHeader("Tiling")) {
1934 ImGui::Checkbox("Enable", &fTiled);
1935 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1936 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1937 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1938 }
1939
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001940 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001941 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1942 this->preTouchMatrixChanged();
1943 this->onResize(fWindow->width(), fWindow->height());
1944 paramsChanged = true;
1945 }
1946
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001947 float zoom = fZoomLevel;
1948 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1949 fZoomLevel = zoom;
1950 this->preTouchMatrixChanged();
1951 paramsChanged = true;
1952 }
1953 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001954 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001955 fRotation = deg;
1956 this->preTouchMatrixChanged();
1957 paramsChanged = true;
1958 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001959 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1960 if (ImGui_DragLocation(&fOffset)) {
1961 this->preTouchMatrixChanged();
1962 paramsChanged = true;
1963 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001964 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1965 this->preTouchMatrixChanged();
1966 paramsChanged = true;
1967 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001968 }
Brian Osman805a7272018-05-02 15:40:20 -04001969 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1970 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1971 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001972 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001973 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001974 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001975 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001976 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001977 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001978 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001979 }
1980
Ben Wagnera580fb32018-04-17 11:16:32 -04001981 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001982 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001983 if (fPaintOverrides.fAntiAlias) {
1984 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001985 }
1986 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001987 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001988 {
1989 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1990 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001991 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001992 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1993 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001994 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001995 fPaintOverrides.fAntiAlias = true;
1996 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001997 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001998 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001999 case SkPaintFields::AntiAliasState::Alias:
2000 break;
2001 case SkPaintFields::AntiAliasState::Normal:
2002 break;
2003 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
2004 gSkUseAnalyticAA = true;
2005 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002006 break;
2007 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2008 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002009 break;
2010 }
2011 }
2012 paramsChanged = true;
2013 }
2014
Ben Wagner99a78dc2018-05-09 18:23:51 -04002015 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002016 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002017 bool (SkPaint::* isFlag)() const,
2018 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002019 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002020 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002021 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002022 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002023 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002024 if (ImGui::Combo(label, &itemIndex, items)) {
2025 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002026 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002027 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002028 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002029 (fPaint.*setFlag)(itemIndex == 2);
2030 }
2031 paramsChanged = true;
2032 }
2033 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002034
Ben Wagner99a78dc2018-05-09 18:23:51 -04002035 paintFlag("Dither",
2036 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002037 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002038 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002039
2040 int styleIdx = 0;
2041 if (fPaintOverrides.fStyle) {
2042 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2043 }
2044 if (ImGui::Combo("Style", &styleIdx,
2045 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2046 {
2047 if (styleIdx == 0) {
2048 fPaintOverrides.fStyle = false;
2049 fPaint.setStyle(SkPaint::kFill_Style);
2050 } else {
2051 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2052 fPaintOverrides.fStyle = true;
2053 }
2054 paramsChanged = true;
2055 }
2056
2057 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2058 if (fPaintOverrides.fWidth) {
2059 float width = fPaint.getStrokeWidth();
2060 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2061 fPaint.setStrokeWidth(width);
2062 paramsChanged = true;
2063 }
2064 }
2065
2066 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2067 if (fPaintOverrides.fMiterLimit) {
2068 float miterLimit = fPaint.getStrokeMiter();
2069 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2070 fPaint.setStrokeMiter(miterLimit);
2071 paramsChanged = true;
2072 }
2073 }
2074
2075 int capIdx = 0;
2076 if (fPaintOverrides.fCapType) {
2077 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2078 }
2079 if (ImGui::Combo("Cap Type", &capIdx,
2080 "Default\0Butt\0Round\0Square\0\0"))
2081 {
2082 if (capIdx == 0) {
2083 fPaintOverrides.fCapType = false;
2084 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2085 } else {
2086 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2087 fPaintOverrides.fCapType = true;
2088 }
2089 paramsChanged = true;
2090 }
2091
2092 int joinIdx = 0;
2093 if (fPaintOverrides.fJoinType) {
2094 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2095 }
2096 if (ImGui::Combo("Join Type", &joinIdx,
2097 "Default\0Miter\0Round\0Bevel\0\0"))
2098 {
2099 if (joinIdx == 0) {
2100 fPaintOverrides.fJoinType = false;
2101 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2102 } else {
2103 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2104 fPaintOverrides.fJoinType = true;
2105 }
2106 paramsChanged = true;
2107 }
Ben Wagner9613e452019-01-23 10:34:59 -05002108 }
Hal Canary02738a82019-01-21 18:51:32 +00002109
Ben Wagner9613e452019-01-23 10:34:59 -05002110 if (ImGui::CollapsingHeader("Font")) {
2111 int hintingIdx = 0;
2112 if (fFontOverrides.fHinting) {
2113 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2114 }
2115 if (ImGui::Combo("Hinting", &hintingIdx,
2116 "Default\0None\0Slight\0Normal\0Full\0\0"))
2117 {
2118 if (hintingIdx == 0) {
2119 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002120 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002121 } else {
2122 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2123 fFontOverrides.fHinting = true;
2124 }
2125 paramsChanged = true;
2126 }
Hal Canary02738a82019-01-21 18:51:32 +00002127
Ben Wagner9613e452019-01-23 10:34:59 -05002128 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2129 bool SkFontFields::* flag,
2130 bool (SkFont::* isFlag)() const,
2131 void (SkFont::* setFlag)(bool) )
2132 {
2133 int itemIndex = 0;
2134 if (fFontOverrides.*flag) {
2135 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2136 }
2137 if (ImGui::Combo(label, &itemIndex, items)) {
2138 if (itemIndex == 0) {
2139 fFontOverrides.*flag = false;
2140 } else {
2141 fFontOverrides.*flag = true;
2142 (fFont.*setFlag)(itemIndex == 2);
2143 }
2144 paramsChanged = true;
2145 }
2146 };
Hal Canary02738a82019-01-21 18:51:32 +00002147
Ben Wagner9613e452019-01-23 10:34:59 -05002148 fontFlag("Fake Bold Glyphs",
2149 "Default\0No Fake Bold\0Fake Bold\0\0",
2150 &SkFontFields::fEmbolden,
2151 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002152
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002153 fontFlag("Baseline Snapping",
2154 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2155 &SkFontFields::fBaselineSnap,
2156 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2157
Ben Wagner9613e452019-01-23 10:34:59 -05002158 fontFlag("Linear Text",
2159 "Default\0No Linear Text\0Linear Text\0\0",
2160 &SkFontFields::fLinearMetrics,
2161 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002162
Ben Wagner9613e452019-01-23 10:34:59 -05002163 fontFlag("Subpixel Position Glyphs",
2164 "Default\0Pixel Text\0Subpixel Text\0\0",
2165 &SkFontFields::fSubpixel,
2166 &SkFont::isSubpixel, &SkFont::setSubpixel);
2167
2168 fontFlag("Embedded Bitmap Text",
2169 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2170 &SkFontFields::fEmbeddedBitmaps,
2171 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2172
2173 fontFlag("Force Auto-Hinting",
2174 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2175 &SkFontFields::fForceAutoHinting,
2176 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2177
2178 int edgingIdx = 0;
2179 if (fFontOverrides.fEdging) {
2180 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2181 }
2182 if (ImGui::Combo("Edging", &edgingIdx,
2183 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2184 {
2185 if (edgingIdx == 0) {
2186 fFontOverrides.fEdging = false;
2187 fFont.setEdging(SkFont::Edging::kAlias);
2188 } else {
2189 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2190 fFontOverrides.fEdging = true;
2191 }
2192 paramsChanged = true;
2193 }
2194
Ben Wagner15a8d572019-03-21 13:35:44 -04002195 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2196 if (fFontOverrides.fSize) {
2197 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002198 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002199 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002200 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002201 fFontOverrides.fSizeRange[0],
2202 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002203 "%.6f", 2.0f))
2204 {
Mike Reed3ae47332019-01-04 10:11:46 -05002205 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002206 paramsChanged = true;
2207 }
2208 }
2209
2210 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2211 if (fFontOverrides.fScaleX) {
2212 float scaleX = fFont.getScaleX();
2213 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2214 fFont.setScaleX(scaleX);
2215 paramsChanged = true;
2216 }
2217 }
2218
2219 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2220 if (fFontOverrides.fSkewX) {
2221 float skewX = fFont.getSkewX();
2222 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2223 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002224 paramsChanged = true;
2225 }
2226 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002227 }
2228
Mike Reed81f60ec2018-05-15 10:09:52 -04002229 {
2230 SkMetaData controls;
2231 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2232 if (ImGui::CollapsingHeader("Current Slide")) {
2233 SkMetaData::Iter iter(controls);
2234 const char* name;
2235 SkMetaData::Type type;
2236 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002237 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002238 if (type == SkMetaData::kScalar_Type) {
2239 float val[3];
2240 SkASSERT(count == 3);
2241 controls.findScalars(name, &count, val);
2242 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2243 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002244 }
Ben Wagner110c7032019-03-22 17:03:59 -04002245 } else if (type == SkMetaData::kBool_Type) {
2246 bool val;
2247 SkASSERT(count == 1);
2248 controls.findBool(name, &val);
2249 if (ImGui::Checkbox(name, &val)) {
2250 controls.setBool(name, val);
2251 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002252 }
2253 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002254 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002255 }
2256 }
2257 }
2258
Ben Wagner7a3c6742018-04-23 10:01:07 -04002259 if (fShowSlidePicker) {
2260 ImGui::SetNextTreeNodeOpen(true);
2261 }
Brian Osman79086b92017-02-10 13:36:16 -05002262 if (ImGui::CollapsingHeader("Slide")) {
2263 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002264 static ImVector<const char*> filteredSlideNames;
2265 static ImVector<int> filteredSlideIndices;
2266
Brian Osmanfce09c52017-11-14 15:32:20 -05002267 if (fShowSlidePicker) {
2268 ImGui::SetKeyboardFocusHere();
2269 fShowSlidePicker = false;
2270 }
2271
Brian Osman79086b92017-02-10 13:36:16 -05002272 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002273 filteredSlideNames.clear();
2274 filteredSlideIndices.clear();
2275 int filteredIndex = 0;
2276 for (int i = 0; i < fSlides.count(); ++i) {
2277 const char* slideName = fSlides[i]->getName().c_str();
2278 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2279 if (i == fCurrentSlide) {
2280 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002281 }
Brian Osmanf479e422017-11-08 13:11:36 -05002282 filteredSlideNames.push_back(slideName);
2283 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002284 }
Brian Osman79086b92017-02-10 13:36:16 -05002285 }
Brian Osmanf479e422017-11-08 13:11:36 -05002286
Brian Osmanf479e422017-11-08 13:11:36 -05002287 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2288 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002289 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002290 }
2291 }
Brian Osmana109e392017-02-24 09:49:14 -05002292
2293 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002294 ColorMode newMode = fColorMode;
2295 auto cmButton = [&](ColorMode mode, const char* label) {
2296 if (ImGui::RadioButton(label, mode == fColorMode)) {
2297 newMode = mode;
2298 }
2299 };
2300
2301 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002302 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2303 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002304 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002305
2306 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002307 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002308 }
2309
2310 // Pick from common gamuts:
2311 int primariesIdx = 4; // Default: Custom
2312 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2313 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2314 primariesIdx = i;
2315 break;
2316 }
2317 }
2318
Brian Osman03115dc2018-11-26 13:55:19 -05002319 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002320 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002321
Brian Osmana109e392017-02-24 09:49:14 -05002322 if (ImGui::Combo("Primaries", &primariesIdx,
2323 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2324 if (primariesIdx >= 0 && primariesIdx <= 3) {
2325 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2326 }
2327 }
2328
2329 // Allow direct editing of gamut
2330 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2331 }
Brian Osman207d4102019-01-10 09:40:58 -05002332
2333 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002334 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002335 if (ImGui::Checkbox("Pause", &isPaused)) {
2336 fAnimTimer.togglePauseResume();
2337 }
Brian Osman707d2022019-01-10 11:27:34 -05002338
2339 float speed = fAnimTimer.getSpeed();
2340 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2341 fAnimTimer.setSpeed(speed);
2342 }
Brian Osman207d4102019-01-10 09:40:58 -05002343 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002344
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002345 if (ImGui::CollapsingHeader("Shaders")) {
2346 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2347 GrContextOptions::ShaderCacheStrategy::kSkSL;
John Stiles7247b482021-03-08 10:40:35 -05002348
2349 int optLevel = sksl ? kShaderOptLevel_Source :
John Stiles7247b482021-03-08 10:40:35 -05002350 SkSL::gSkSLInliner ? kShaderOptLevel_Inline :
2351 SkSL::gSkSLOptimizer ? kShaderOptLevel_Optimize :
2352 kShaderOptLevel_Compile;
2353
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002354#if defined(SK_VULKAN)
2355 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2356#else
2357 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002358#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002359
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002360 // To re-load shaders from the currently active programs, we flush all
2361 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002362 static bool gLoadPending = false;
2363 if (gLoadPending) {
2364 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
Brian Osmanf0de96f2021-02-26 13:54:11 -05002365 const SkString& description, int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002366 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002367 entry.fKey = key;
2368 SkMD5 hash;
2369 hash.write(key->bytes(), key->size());
2370 SkMD5::Digest digest = hash.finish();
2371 for (int i = 0; i < 16; ++i) {
2372 entry.fKeyString.appendf("%02x", digest.data[i]);
2373 }
Brian Osmanf0de96f2021-02-26 13:54:11 -05002374 entry.fKeyDescription = description;
Brian Osman0b8bb882019-04-12 11:47:19 -04002375
Brian Osman9e4e4c72020-06-10 07:19:34 -04002376 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002377 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002378 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2379 entry.fInputs,
2380 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002381 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002382 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002383 fPersistentCache.foreach(collectShaders);
2384 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002385
2386#if defined(SK_VULKAN)
2387 if (isVulkan && !sksl) {
2388 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2389 for (auto& entry : fCachedShaders) {
2390 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2391 const SkSL::String& spirv(entry.fShader[i]);
2392 std::string disasm;
2393 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2394 &disasm);
2395 entry.fShader[i].assign(disasm);
2396 }
2397 }
2398 }
2399#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002400 }
2401
John Stilesa8f6b6f2021-03-05 16:00:42 -05002402 // Defer actually doing the View/Apply logic so that we can trigger an Apply when we
Brian Osman0b8bb882019-04-12 11:47:19 -04002403 // start or finish hovering on a tree node in the list below:
John Stilesa8f6b6f2021-03-05 16:00:42 -05002404 bool doView = ImGui::Button("View"); ImGui::SameLine();
2405 bool doApply = ImGui::Button("Apply Changes"); ImGui::SameLine();
John Stiles7247b482021-03-08 10:40:35 -05002406 bool doDump = ImGui::Button("Dump SkSL to resources/sksl/");
John Stilesa8f6b6f2021-03-05 16:00:42 -05002407
John Stiles7247b482021-03-08 10:40:35 -05002408 int newOptLevel = optLevel;
2409 ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source);
2410 ImGui::SameLine();
2411 ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile);
2412 ImGui::SameLine();
2413 ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize);
2414 ImGui::SameLine();
2415 ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline);
John Stilesa8f6b6f2021-03-05 16:00:42 -05002416
John Stiles7247b482021-03-08 10:40:35 -05002417 // If we are changing the compile mode, we want to reset the cache and redo
2418 // everything.
2419 if (doDump || newOptLevel != optLevel) {
2420 sksl = doDump || (newOptLevel == kShaderOptLevel_Source);
2421 SkSL::gSkSLOptimizer = (newOptLevel >= kShaderOptLevel_Optimize);
2422 SkSL::gSkSLInliner = (newOptLevel >= kShaderOptLevel_Inline);
John Stiles7247b482021-03-08 10:40:35 -05002423
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002424 params.fGrContextOptions.fShaderCacheStrategy =
2425 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2426 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2427 paramsChanged = true;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002428 doView = true;
2429
2430 fDeferredActions.push_back([=]() {
2431 // Reset the cache.
2432 fPersistentCache.reset();
2433 // Dump the cache once we have drawn a frame with it.
2434 if (doDump) {
2435 fDeferredActions.push_back([this]() {
2436 this->dumpShadersToResources();
2437 });
2438 }
2439 });
Brian Osmancbc33b82019-04-19 14:16:19 -04002440 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002441
2442 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002443 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002444 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2445 bool hovered = ImGui::IsItemHovered();
2446 if (hovered != entry.fHovered) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002447 // Force an Apply to patch the highlight shader in/out
Brian Osman0b8bb882019-04-12 11:47:19 -04002448 entry.fHovered = hovered;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002449 doApply = true;
Brian Osman0b8bb882019-04-12 11:47:19 -04002450 }
2451 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002452 auto stringBox = [](const char* label, std::string* str) {
2453 // Full width, and not too much space for each shader
2454 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2455 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2456 ImGui::InputTextMultiline(label, str, boxSize);
2457 };
Brian Osmanf0de96f2021-02-26 13:54:11 -05002458 if (ImGui::TreeNode("Key")) {
2459 ImGui::TextWrapped("%s", entry.fKeyDescription.c_str());
2460 ImGui::TreePop();
2461 }
Brian Osmaneb3fb902020-08-18 13:16:59 -04002462 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2463 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002464 ImGui::TreePop();
2465 }
2466 }
2467 ImGui::EndChild();
2468
John Stilesa8f6b6f2021-03-05 16:00:42 -05002469 if (doView) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002470 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002471 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002472 gLoadPending = true;
2473 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002474
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002475 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2476 // but I'm not sure anyone wants to do that.
2477 if (isVulkan && !sksl) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002478 doApply = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002479 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002480 if (doApply) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002481 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002482 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002483 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002484 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002485 if (entry.fHovered) {
2486 // The hovered item (if any) gets a special shader to make it
2487 // identifiable.
2488 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2489 switch (entry.fShaderType) {
2490 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2491 fragShader = build_sksl_highlight_shader();
2492 break;
2493 }
2494 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2495 fragShader = build_glsl_highlight_shader(
2496 *ctx->priv().caps()->shaderCaps());
2497 break;
2498 }
2499 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2500 fragShader = build_metal_highlight_shader(fragShader);
2501 break;
2502 }
2503 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002504 }
2505
Brian Osmana085a412019-04-25 09:44:43 -04002506 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2507 entry.fShader,
2508 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002509 kGrShaderTypeCount);
Brian Osmanf0de96f2021-02-26 13:54:11 -05002510 fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription);
Brian Osman0b8bb882019-04-12 11:47:19 -04002511
2512 entry.fShader[kFragment_GrShaderType] = backup;
2513 }
2514 }
2515 }
Brian Osman79086b92017-02-10 13:36:16 -05002516 }
Brian Salomon99a33902017-03-07 15:16:34 -05002517 if (paramsChanged) {
2518 fDeferredActions.push_back([=]() {
2519 fWindow->setRequestedDisplayParams(params);
2520 fWindow->inval();
2521 this->updateTitle();
2522 });
2523 }
Brian Osman79086b92017-02-10 13:36:16 -05002524 ImGui::End();
2525 }
2526
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002527 if (gShaderErrorHandler.fErrors.count()) {
2528 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002529 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002530 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2531 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002532 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2533 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2534 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2535 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002536 }
2537 ImGui::End();
2538 gShaderErrorHandler.reset();
2539 }
2540
Brian Osmanf6877092017-02-13 09:39:57 -05002541 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002542 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2543 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002544 static int zoomFactor = 8;
2545 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002546 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002547 }
2548 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2549 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002550 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002551 }
Brian Osmanf6877092017-02-13 09:39:57 -05002552
Ben Wagner3627d2e2018-06-26 14:23:20 -04002553 if (!fZoomWindowFixed) {
2554 ImVec2 mousePos = ImGui::GetMousePos();
2555 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2556 }
2557 SkScalar x = fZoomWindowLocation.x();
2558 SkScalar y = fZoomWindowLocation.y();
2559 int xInt = SkScalarRoundToInt(x);
2560 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002561 ImVec2 avail = ImGui::GetContentRegionAvail();
2562
Brian Osmanead517d2017-11-13 15:36:36 -05002563 uint32_t pixel = 0;
2564 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002565 auto dContext = fWindow->directContext();
2566 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002567 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002568 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002569 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002570 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002571 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2572 }
2573
Greg Danielfbc60b72020-11-03 17:27:02 -05002574 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002575 // Translate so the region of the image that's under the mouse cursor is centered
2576 // in the zoom canvas:
2577 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002578 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2579 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002580 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002581
2582 SkPaint outline;
2583 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002584 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002585 });
Brian Osmanf6877092017-02-13 09:39:57 -05002586 }
2587
2588 ImGui::End();
2589 }
Brian Osman79086b92017-02-10 13:36:16 -05002590}
2591
John Stilesa8f6b6f2021-03-05 16:00:42 -05002592void Viewer::dumpShadersToResources() {
2593 // Sort the list of cached shaders so we can maintain some minimal level of consistency.
2594 // It doesn't really matter, but it will keep files from switching places unpredictably.
2595 std::vector<const CachedShader*> shaders;
2596 shaders.reserve(fCachedShaders.size());
2597 for (const CachedShader& shader : fCachedShaders) {
2598 shaders.push_back(&shader);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002599 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002600
2601 std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) {
2602 return std::tie(a->fShader[kFragment_GrShaderType], a->fShader[kVertex_GrShaderType]) <
2603 std::tie(b->fShader[kFragment_GrShaderType], b->fShader[kVertex_GrShaderType]);
2604 });
2605
2606 // Make the resources/sksl/SlideName/ directory.
2607 SkString directory = SkStringPrintf("%ssksl/%s",
2608 GetResourcePath().c_str(),
2609 fSlides[fCurrentSlide]->getName().c_str());
2610 if (!sk_mkdir(directory.c_str())) {
2611 SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str());
2612 return;
2613 }
2614
2615 int index = 0;
2616 for (const auto& entry : shaders) {
2617 SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index);
2618 FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag);
2619 if (vertFile) {
2620 const SkSL::String& vertText = entry->fShader[kVertex_GrShaderType];
2621 SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile));
2622 sk_fclose(vertFile);
2623 } else {
2624 SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str());
2625 }
2626
2627 SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index);
2628 FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag);
2629 if (fragFile) {
2630 const SkSL::String& fragText = entry->fShader[kFragment_GrShaderType];
2631 SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile));
2632 sk_fclose(fragFile);
2633 } else {
2634 SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str());
2635 }
2636
2637 ++index;
2638 }
2639}
2640
2641void Viewer::onIdle() {
2642 SkTArray<std::function<void()>> actionsToRun;
2643 actionsToRun.swap(fDeferredActions);
2644
2645 for (const auto& fn : actionsToRun) {
2646 fn();
2647 }
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002648
Brian Osman56a24812017-12-19 11:15:16 -05002649 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002650 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002651 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002652 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002653
Brian Osman79086b92017-02-10 13:36:16 -05002654 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002655 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2656 // not be visible, though. So we need to redraw if there is at least one visible window, or
2657 // more than one active window. Newly created windows are active but not visible for one frame
2658 // while they determine their layout and sizing.
2659 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2660 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002661 fWindow->inval();
2662 }
jvanverth9f372462016-04-06 06:08:59 -07002663}
liyuqiane5a6cd92016-05-27 08:52:52 -07002664
Florin Malitab632df72018-06-18 21:23:06 -04002665template <typename OptionsFunc>
2666static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2667 OptionsFunc&& optionsFunc) {
2668 writer.beginObject();
2669 {
2670 writer.appendString(kName , name);
2671 writer.appendString(kValue, value);
2672
2673 writer.beginArray(kOptions);
2674 {
2675 optionsFunc(writer);
2676 }
2677 writer.endArray();
2678 }
2679 writer.endObject();
2680}
2681
2682
liyuqiane5a6cd92016-05-27 08:52:52 -07002683void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002684 if (!fWindow) {
2685 return;
2686 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002687 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002688 return; // Surface hasn't been created yet.
2689 }
2690
Florin Malitab632df72018-06-18 21:23:06 -04002691 SkDynamicMemoryWStream memStream;
2692 SkJSONWriter writer(&memStream);
2693 writer.beginArray();
2694
liyuqianb73c24b2016-06-03 08:47:23 -07002695 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002696 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2697 [this](SkJSONWriter& writer) {
2698 for(const auto& slide : fSlides) {
2699 writer.appendString(slide->getName().c_str());
2700 }
2701 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002702
liyuqianb73c24b2016-06-03 08:47:23 -07002703 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002704 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2705 [](SkJSONWriter& writer) {
2706 for (const auto& str : kBackendTypeStrings) {
2707 writer.appendString(str);
2708 }
2709 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002710
csmartdalton578f0642017-02-24 16:04:47 -07002711 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002712 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2713 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2714 [this](SkJSONWriter& writer) {
2715 writer.appendS32(0);
2716
2717 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2718 return;
2719 }
2720
2721 for (int msaa : {4, 8, 16}) {
2722 writer.appendS32(msaa);
2723 }
2724 });
csmartdalton578f0642017-02-24 16:04:47 -07002725
csmartdalton61cd31a2017-02-27 17:00:53 -07002726 // Path renderer state
2727 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002728 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2729 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002730 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002731 if (!ctx) {
2732 writer.appendString("Software");
2733 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002734 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002735 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2736 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002737 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002738 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002739 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002740 }
Florin Malitab632df72018-06-18 21:23:06 -04002741 if (caps->shaderCaps()->pathRenderingSupport()) {
2742 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002743 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002744 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002745 }
2746 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002747 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2748 writer.appendString(
2749 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2750 }
2751 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2752 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002753 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002754 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002755 }
2756 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002757
liyuqianb73c24b2016-06-03 08:47:23 -07002758 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002759 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2760 [this](SkJSONWriter& writer) {
2761 writer.appendString(kSoftkeyHint);
2762 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2763 writer.appendString(softkey.c_str());
2764 }
2765 });
liyuqianb73c24b2016-06-03 08:47:23 -07002766
Florin Malitab632df72018-06-18 21:23:06 -04002767 writer.endArray();
2768 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002769
Florin Malitab632df72018-06-18 21:23:06 -04002770 auto data = memStream.detachAsData();
2771
2772 // TODO: would be cool to avoid this copy
2773 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2774
2775 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002776}
2777
2778void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002779 // For those who will add more features to handle the state change in this function:
2780 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2781 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2782 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002783 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002784 for (int i = 0; i < fSlides.count(); ++i) {
2785 if (fSlides[i]->getName().equals(stateValue)) {
2786 this->setCurrentSlide(i);
2787 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002788 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002789 }
Florin Malitaab99c342018-01-16 16:23:03 -05002790
2791 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002792 } else if (stateName.equals(kBackendStateName)) {
2793 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2794 if (stateValue.equals(kBackendTypeStrings[i])) {
2795 if (fBackendType != i) {
2796 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002797 for(auto& slide : fSlides) {
2798 slide->gpuTeardown();
2799 }
liyuqian6cb70252016-06-02 12:16:25 -07002800 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002801 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002802 }
2803 break;
2804 }
2805 }
csmartdalton578f0642017-02-24 16:04:47 -07002806 } else if (stateName.equals(kMSAAStateName)) {
2807 DisplayParams params = fWindow->getRequestedDisplayParams();
2808 int sampleCount = atoi(stateValue.c_str());
2809 if (sampleCount != params.fMSAASampleCount) {
2810 params.fMSAASampleCount = sampleCount;
2811 fWindow->setRequestedDisplayParams(params);
2812 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002813 this->updateTitle();
2814 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002815 }
2816 } else if (stateName.equals(kPathRendererStateName)) {
2817 DisplayParams params = fWindow->getRequestedDisplayParams();
2818 for (const auto& pair : gPathRendererNames) {
2819 if (pair.second == stateValue.c_str()) {
2820 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2821 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2822 fWindow->setRequestedDisplayParams(params);
2823 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002824 this->updateTitle();
2825 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002826 }
2827 break;
2828 }
csmartdalton578f0642017-02-24 16:04:47 -07002829 }
liyuqianb73c24b2016-06-03 08:47:23 -07002830 } else if (stateName.equals(kSoftkeyStateName)) {
2831 if (!stateValue.equals(kSoftkeyHint)) {
2832 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002833 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002834 }
liyuqian2edb0f42016-07-06 14:11:32 -07002835 } else if (stateName.equals(kRefreshStateName)) {
2836 // This state is actually NOT in the UI state.
2837 // We use this to allow Android to quickly set bool fRefresh.
2838 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002839 } else {
2840 SkDebugf("Unknown stateName: %s", stateName.c_str());
2841 }
2842}
Brian Osman79086b92017-02-10 13:36:16 -05002843
Hal Canaryb1f411a2019-08-29 10:39:22 -04002844bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002845 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002846}
2847
Hal Canaryb1f411a2019-08-29 10:39:22 -04002848bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002849 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002850 fWindow->inval();
2851 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002852 } else {
2853 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002854 }
Brian Osman79086b92017-02-10 13:36:16 -05002855}