blob: 51c9e5c8ce9921104efe1a2b64e1cddeda7b0bee [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Brian Salomon06c9e292021-04-29 14:10:23 -04008#include "tools/viewer/Viewer.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkData.h"
12#include "include/core/SkGraphics.h"
13#include "include/core/SkPictureRecorder.h"
14#include "include/core/SkStream.h"
15#include "include/core/SkSurface.h"
Robert Phillipsed653392020-07-10 13:55:21 -040016#include "include/gpu/GrDirectContext.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050017#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkTo.h"
19#include "include/utils/SkPaintFilterCanvas.h"
20#include "src/core/SkColorSpacePriv.h"
21#include "src/core/SkImagePriv.h"
22#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkOSFile.h"
John Stilesf2c2d302021-04-09 17:56:58 -040024#include "src/core/SkReadBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkScan.h"
Chris Dalton94df5722021-04-19 17:40:40 -060026#include "src/core/SkSurfacePriv.h"
John Stilesdf078002020-07-14 09:44:57 -040027#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040029#include "src/core/SkTextBlobPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040030#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrGpu.h"
32#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070033#include "src/gpu/GrShaderUtils.h"
Chris Daltonff18ff62020-12-07 17:39:26 -070034#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Adlai Hollerbcfc5542020-08-27 12:44:07 -040035#include "src/image/SkImage_Base.h"
John Stiles2ee4d7a2021-03-30 10:30:47 -040036#include "src/sksl/SkSLCompiler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/utils/SkJSONWriter.h"
38#include "src/utils/SkOSPath.h"
39#include "tools/Resources.h"
John Stiles9671d0a2021-07-16 16:22:03 -040040#include "tools/RuntimeBlendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "tools/ToolUtils.h"
42#include "tools/flags/CommandLineFlags.h"
43#include "tools/flags/CommonFlags.h"
44#include "tools/trace/EventTracingPriv.h"
45#include "tools/viewer/BisectSlide.h"
46#include "tools/viewer/GMSlide.h"
47#include "tools/viewer/ImageSlide.h"
Brian Salomon06c9e292021-04-29 14:10:23 -040048#include "tools/viewer/MSKPSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "tools/viewer/ParticlesSlide.h"
50#include "tools/viewer/SKPSlide.h"
51#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050052#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053#include "tools/viewer/SlideDir.h"
54#include "tools/viewer/SvgSlide.h"
csmartdalton578f0642017-02-24 16:04:47 -070055
Chris Dalton17dc4182020-03-25 16:18:16 -060056#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040057#include <map>
58
Hal Canary8a001442018-09-19 11:31:27 -040059#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040060#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040061
Brian Osmanc85f1fa2020-06-16 15:11:34 -040062#ifdef SK_VULKAN
63#include "spirv-tools/libspirv.hpp"
64#endif
65
Florin Malita87ccf332018-05-04 12:23:24 -040066#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050067 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040068#endif
Florin Malita45cd2002020-06-09 14:00:54 -040069#if defined(SK_ENABLE_SKRIVE)
70 #include "tools/viewer/SkRiveSlide.h"
71#endif
Florin Malita87ccf332018-05-04 12:23:24 -040072
Brian Osman5e7fbfd2019-05-03 13:13:35 -040073class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
74public:
75 void compileError(const char* shader, const char* errors) override {
76 fShaders.push_back(SkString(shader));
77 fErrors.push_back(SkString(errors));
78 }
79
80 void reset() {
81 fShaders.reset();
82 fErrors.reset();
83 }
84
85 SkTArray<SkString> fShaders;
86 SkTArray<SkString> fErrors;
87};
88
89static CapturingShaderErrorHandler gShaderErrorHandler;
90
Brian Osmanf847f312020-06-18 14:18:27 -040091GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
92
jvanverth34524262016-05-04 13:49:13 -070093using namespace sk_app;
John Stiles2ee4d7a2021-03-30 10:30:47 -040094using SkSL::Compiler;
95using OverrideFlag = SkSL::Compiler::OverrideFlag;
jvanverth34524262016-05-04 13:49:13 -070096
csmartdalton61cd31a2017-02-27 17:00:53 -070097static std::map<GpuPathRenderers, std::string> gPathRendererNames;
98
jvanverth9f372462016-04-06 06:08:59 -070099Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -0700100 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -0700101}
102
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600103static DEFINE_string(slide, "", "Start on this sample.");
104static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -0500105
Jim Van Verth682a2f42020-05-13 16:54:09 -0400106#ifdef SK_GL
107#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700108#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400109#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700110#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400111#ifdef SK_VULKAN
112#define VK_BACKEND_STR ", \"vk\""
113#else
114#define VK_BACKEND_STR
115#endif
116#ifdef SK_METAL
117#define MTL_BACKEND_STR ", \"mtl\""
118#else
119#define MTL_BACKEND_STR
120#endif
121#ifdef SK_DIRECT3D
122#define D3D_BACKEND_STR ", \"d3d\""
123#else
124#define D3D_BACKEND_STR
125#endif
126#ifdef SK_DAWN
127#define DAWN_BACKEND_STR ", \"dawn\""
128#else
129#define DAWN_BACKEND_STR
130#endif
131#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
132#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
133 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700134
Brian Osman2dd96932016-10-18 15:33:53 -0400135static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700136
Mike Klein5b3f3432019-03-21 11:42:21 -0500137static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
Chris Daltonfd708652021-03-17 21:10:17 -0600138static DEFINE_bool(dmsaa, false, "Use internal MSAA to render to non-MSAA surfaces?");
csmartdalton008b9d82017-02-22 12:00:42 -0700139
Mike Klein84836b72019-03-21 11:31:36 -0500140static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700141
Mike Klein84836b72019-03-21 11:31:36 -0500142static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400143
Mike Kleinc6142d82019-03-25 10:54:59 -0500144static DEFINE_string2(match, m, nullptr,
145 "[~][^]substring[$] [...] of name to run.\n"
146 "Multiple matches may be separated by spaces.\n"
147 "~ causes a matching name to always be skipped\n"
148 "^ requires the start of the name to match\n"
149 "$ requires the end of the name to match\n"
150 "^ and $ requires an exact match\n"
151 "If a name does not match any list entry,\n"
152 "it is skipped unless some list entry starts with ~");
153
Mike Klein19fb3972019-03-21 13:08:08 -0500154#if defined(SK_BUILD_FOR_ANDROID)
Brian Salomon06c9e292021-04-29 14:10:23 -0400155# define PATH_PREFIX "/data/local/tmp/"
Mike Klein19fb3972019-03-21 13:08:08 -0500156#else
Brian Salomon06c9e292021-04-29 14:10:23 -0400157# define PATH_PREFIX ""
Mike Klein19fb3972019-03-21 13:08:08 -0500158#endif
159
Brian Salomon06c9e292021-04-29 14:10:23 -0400160static DEFINE_string(jpgs , PATH_PREFIX "jpgs" , "Directory to read jpgs from.");
161static DEFINE_string(skps , PATH_PREFIX "skps" , "Directory to read skps from.");
162static DEFINE_string(mskps , PATH_PREFIX "mskps" , "Directory to read mskps from.");
163static DEFINE_string(lotties, PATH_PREFIX "lotties", "Directory to read (Bodymovin) jsons from.");
164static DEFINE_string(rives , PATH_PREFIX "rives" , "Directory to read Rive (Flare) files from.");
165#undef PATH_PREFIX
166
Mike Kleinc6142d82019-03-25 10:54:59 -0500167static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
168
169static DEFINE_int_2(threads, j, -1,
170 "Run threadsafe tests on a threadpool with this many extra threads, "
171 "defaulting to one extra thread per core.");
172
Jim Van Verth7b558182019-11-14 16:47:01 -0500173static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
174
Chris Daltonc8877332020-01-06 09:48:30 -0700175static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500176static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
177static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500178static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500179static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500180static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500181
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400182#ifndef SK_GL
183static_assert(false, "viewer requires GL backend for raster.")
184#endif
185
Brian Salomon194db172017-08-17 14:37:06 -0400186const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700187 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400188#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
189 "ANGLE",
190#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400191#ifdef SK_DAWN
192 "Dawn",
193#endif
jvanverth063ece72016-06-17 09:29:14 -0700194#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700195 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700196#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400197#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500198 "Metal",
199#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400200#ifdef SK_DIRECT3D
201 "Direct3D",
202#endif
csmartdalton578f0642017-02-24 16:04:47 -0700203 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700204};
205
bsalomon6c471f72016-07-26 12:56:32 -0700206static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400207#ifdef SK_DAWN
208 if (0 == strcmp(str, "dawn")) {
209 return sk_app::Window::kDawn_BackendType;
210 } else
211#endif
bsalomon6c471f72016-07-26 12:56:32 -0700212#ifdef SK_VULKAN
213 if (0 == strcmp(str, "vk")) {
214 return sk_app::Window::kVulkan_BackendType;
215 } else
216#endif
Brian Salomon194db172017-08-17 14:37:06 -0400217#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
218 if (0 == strcmp(str, "angle")) {
219 return sk_app::Window::kANGLE_BackendType;
220 } else
221#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400222#ifdef SK_METAL
223 if (0 == strcmp(str, "mtl")) {
224 return sk_app::Window::kMetal_BackendType;
225 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500226#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400227#ifdef SK_DIRECT3D
228 if (0 == strcmp(str, "d3d")) {
229 return sk_app::Window::kDirect3D_BackendType;
230 } else
231#endif
232
bsalomon6c471f72016-07-26 12:56:32 -0700233 if (0 == strcmp(str, "gl")) {
234 return sk_app::Window::kNativeGL_BackendType;
235 } else if (0 == strcmp(str, "sw")) {
236 return sk_app::Window::kRaster_BackendType;
237 } else {
238 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
239 return sk_app::Window::kRaster_BackendType;
240 }
241}
242
Brian Osmana109e392017-02-24 09:49:14 -0500243static SkColorSpacePrimaries gSrgbPrimaries = {
244 0.64f, 0.33f,
245 0.30f, 0.60f,
246 0.15f, 0.06f,
247 0.3127f, 0.3290f };
248
249static SkColorSpacePrimaries gAdobePrimaries = {
250 0.64f, 0.33f,
251 0.21f, 0.71f,
252 0.15f, 0.06f,
253 0.3127f, 0.3290f };
254
255static SkColorSpacePrimaries gP3Primaries = {
256 0.680f, 0.320f,
257 0.265f, 0.690f,
258 0.150f, 0.060f,
259 0.3127f, 0.3290f };
260
261static SkColorSpacePrimaries gRec2020Primaries = {
262 0.708f, 0.292f,
263 0.170f, 0.797f,
264 0.131f, 0.046f,
265 0.3127f, 0.3290f };
266
267struct NamedPrimaries {
268 const char* fName;
269 SkColorSpacePrimaries* fPrimaries;
270} gNamedPrimaries[] = {
271 { "sRGB", &gSrgbPrimaries },
272 { "AdobeRGB", &gAdobePrimaries },
273 { "P3", &gP3Primaries },
274 { "Rec. 2020", &gRec2020Primaries },
275};
276
277static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
278 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
279}
280
Brian Osman70d2f432017-11-08 09:54:10 -0500281static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
282 // In raster mode, we still use GL for the window.
283 // This lets us render the GUI faster (and correct).
284 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
285}
286
Jim Van Verth74826c82019-03-01 14:37:30 -0500287class NullSlide : public Slide {
288 SkISize getDimensions() const override {
289 return SkISize::Make(640, 480);
290 }
291
292 void draw(SkCanvas* canvas) override {
293 canvas->clear(0xffff11ff);
294 }
295};
296
John Stiles31964fd2020-05-05 16:05:47 -0400297static const char kName[] = "name";
298static const char kValue[] = "value";
299static const char kOptions[] = "options";
300static const char kSlideStateName[] = "Slide";
301static const char kBackendStateName[] = "Backend";
302static const char kMSAAStateName[] = "MSAA";
303static const char kPathRendererStateName[] = "Path renderer";
304static const char kSoftkeyStateName[] = "Softkey";
305static const char kSoftkeyHint[] = "Please select a softkey";
306static const char kON[] = "ON";
307static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700308
Mike Reed862818b2020-03-21 15:07:13 -0400309extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500310extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500311extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400312
jvanverth34524262016-05-04 13:49:13 -0700313Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500314 : fCurrentSlide(-1)
315 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500316 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400317 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500318 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500319 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500320 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500321 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400322 , fZoomWindowFixed(false)
323 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500324 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400325 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700326 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500327 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500328 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500329 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500330 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500331 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700332 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400333 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400334 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400335 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500336 , fTiled(false)
337 , fDrawTileBoundaries(false)
338 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400339 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700340{
Greg Daniel285db442016-10-14 09:12:53 -0400341 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700342
Chris Dalton37ae4b02019-12-28 14:51:11 -0700343 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600344 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500345 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Dalton17dc4182020-03-25 16:18:16 -0600346 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500347 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700348
jvanverth2bb3b6d2016-04-08 07:24:09 -0700349 SkDebugf("Command line arguments: ");
350 for (int i = 1; i < argc; ++i) {
351 SkDebugf("%s ", argv[i]);
352 }
353 SkDebugf("\n");
354
Mike Klein88544fb2019-03-20 10:50:33 -0500355 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500356#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400357 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500358#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700359
Mike Reed862818b2020-03-21 15:07:13 -0400360 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500361 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500362 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400363
Mike Klein19cc0f62019-03-22 15:30:07 -0500364 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500365
Brian Osmanbc8150f2017-07-24 11:38:01 -0400366 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400367 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400368
bsalomon6c471f72016-07-26 12:56:32 -0700369 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700370 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700371
csmartdalton578f0642017-02-24 16:04:47 -0700372 DisplayParams displayParams;
373 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500374 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700375 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400376 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400377 displayParams.fGrContextOptions.fShaderCacheStrategy =
John Stiles2ee4d7a2021-03-30 10:30:47 -0400378 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400379 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
380 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Dalton94df5722021-04-19 17:40:40 -0600381 if (FLAGS_dmsaa) {
382 displayParams.fSurfaceProps = SkSurfaceProps(
Chris Dalton475c9752021-07-13 11:54:22 -0600383 displayParams.fSurfaceProps.flags() | SkSurfaceProps::kDynamicMSAA_Flag,
Chris Dalton94df5722021-04-19 17:40:40 -0600384 displayParams.fSurfaceProps.pixelGeometry());
385 }
csmartdalton578f0642017-02-24 16:04:47 -0700386 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400387 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500388 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700389
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500390 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500391 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500392
Brian Osman56a24812017-12-19 11:15:16 -0500393 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500394 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500395 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
396 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
397 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
398
jvanverth9f372462016-04-06 06:08:59 -0700399 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700400 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500401 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500402 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500403 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700404
brianosman622c8d52016-05-10 06:50:49 -0700405 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500406 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
407 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
408 fWindow->inval();
409 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500410 // Command to jump directly to the slide picker and give it focus
411 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
412 this->fShowImGuiDebugWindow = true;
413 this->fShowSlidePicker = true;
414 fWindow->inval();
415 });
416 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400417 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500418 this->fShowImGuiDebugWindow = true;
419 this->fShowSlidePicker = true;
420 fWindow->inval();
421 });
Brian Osman79086b92017-02-10 13:36:16 -0500422 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
423 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
424 fWindow->inval();
425 });
Brian Osmanf6877092017-02-13 09:39:57 -0500426 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
427 this->fShowZoomWindow = !this->fShowZoomWindow;
428 fWindow->inval();
429 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400430 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
431 this->fZoomWindowFixed = !this->fZoomWindowFixed;
432 fWindow->inval();
433 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400434 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400435 DisplayParams params = fWindow->getRequestedDisplayParams();
436 params.fDisableVsync = !params.fDisableVsync;
437 fWindow->setRequestedDisplayParams(params);
438 this->updateTitle();
439 fWindow->inval();
440 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400441 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
442 DisplayParams params = fWindow->getRequestedDisplayParams();
443 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
444 fWindow->setRequestedDisplayParams(params);
445 this->updateTitle();
446 fWindow->inval();
447 });
Mike Reedf702ed42019-07-22 17:00:49 -0400448 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
449 fRefresh = !fRefresh;
450 fWindow->inval();
451 });
brianosman622c8d52016-05-10 06:50:49 -0700452 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500453 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700454 fWindow->inval();
455 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400456 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500457 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400458 this->updateTitle();
459 fWindow->inval();
460 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500461 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500462 switch (fColorMode) {
463 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500464 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500465 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500466 case ColorMode::kColorManaged8888:
467 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500468 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500469 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400470 this->setColorMode(ColorMode::kColorManagedF16Norm);
471 break;
472 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500473 this->setColorMode(ColorMode::kLegacy);
474 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500475 }
brianosman622c8d52016-05-10 06:50:49 -0700476 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700477 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
478 DisplayParams params = fWindow->getRequestedDisplayParams();
479 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
480 fWindow->setRequestedDisplayParams(params);
481 fWindow->inval();
482 });
Brian Salomon91216d52021-04-09 11:57:59 -0400483 fCommands.addCommand('w', "Modes", "Toggle reduced shaders", [this]() {
484 DisplayParams params = fWindow->getRequestedDisplayParams();
485 params.fGrContextOptions.fReducedShaderVariations =
486 !params.fGrContextOptions.fReducedShaderVariations;
487 fWindow->setRequestedDisplayParams(params);
488 fWindow->inval();
489 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400490 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500491 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700492 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400493 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500494 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700495 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400496 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700497 this->changeZoomLevel(1.f / 32.f);
498 fWindow->inval();
499 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400500 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700501 this->changeZoomLevel(-1.f / 32.f);
502 fWindow->inval();
503 });
jvanverthaf236b52016-05-20 06:01:06 -0700504 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400505 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
506 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500507 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400508#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
509 if (newBackend == sk_app::Window::kVulkan_BackendType) {
510 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
511 sk_app::Window::kBackendTypeCount);
512 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
513 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500514 }
515#endif
Brian Osman621491e2017-02-28 15:45:01 -0500516 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700517 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500518 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
519 fSaveToSKP = true;
520 fWindow->inval();
521 });
Mike Reed376d8122019-03-14 11:39:02 -0400522 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
523 fShowSlideDimensions = !fShowSlideDimensions;
524 fWindow->inval();
525 });
Ben Wagner37c54032018-04-13 14:30:23 -0400526 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
527 DisplayParams params = fWindow->getRequestedDisplayParams();
528 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400529 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
530 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
531 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400532 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
533 } else {
534 switch (params.fSurfaceProps.pixelGeometry()) {
535 case kUnknown_SkPixelGeometry:
536 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
537 break;
538 case kRGB_H_SkPixelGeometry:
539 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
540 break;
541 case kBGR_H_SkPixelGeometry:
542 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
543 break;
544 case kRGB_V_SkPixelGeometry:
545 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
546 break;
547 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400548 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
549 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400550 break;
551 }
552 }
553 fWindow->setRequestedDisplayParams(params);
554 this->updateTitle();
555 fWindow->inval();
556 });
Ben Wagner9613e452019-01-23 10:34:59 -0500557 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500558 if (!fFontOverrides.fHinting) {
559 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400560 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500561 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500562 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400563 case SkFontHinting::kNone:
564 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500565 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400566 case SkFontHinting::kSlight:
567 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500568 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400569 case SkFontHinting::kNormal:
570 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500571 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400572 case SkFontHinting::kFull:
573 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500574 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500575 break;
576 }
577 }
578 this->updateTitle();
579 fWindow->inval();
580 });
581 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500582 if (!fPaintOverrides.fAntiAlias) {
583 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
584 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500585 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500586 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500587 } else {
588 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500589 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500590 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500591 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400592 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500593 break;
594 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500595 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500596 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400597 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500598 break;
599 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500600 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400601 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500602 break;
603 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500604 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
605 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500606 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
607 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500608 break;
609 }
610 }
611 this->updateTitle();
612 fWindow->inval();
613 });
Ben Wagner37c54032018-04-13 14:30:23 -0400614 fCommands.addCommand('D', "Modes", "DFT", [this]() {
615 DisplayParams params = fWindow->getRequestedDisplayParams();
616 uint32_t flags = params.fSurfaceProps.flags();
617 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
618 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
619 fWindow->setRequestedDisplayParams(params);
620 this->updateTitle();
621 fWindow->inval();
622 });
Ben Wagner9613e452019-01-23 10:34:59 -0500623 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500624 if (!fFontOverrides.fEdging) {
625 fFontOverrides.fEdging = true;
626 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500627 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500628 switch (fFont.getEdging()) {
629 case SkFont::Edging::kAlias:
630 fFont.setEdging(SkFont::Edging::kAntiAlias);
631 break;
632 case SkFont::Edging::kAntiAlias:
633 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
634 break;
635 case SkFont::Edging::kSubpixelAntiAlias:
636 fFont.setEdging(SkFont::Edging::kAlias);
637 fFontOverrides.fEdging = false;
638 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500639 }
640 }
641 this->updateTitle();
642 fWindow->inval();
643 });
Ben Wagner9613e452019-01-23 10:34:59 -0500644 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500645 if (!fFontOverrides.fSubpixel) {
646 fFontOverrides.fSubpixel = true;
647 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500648 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500649 if (!fFont.isSubpixel()) {
650 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500651 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500652 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500653 }
654 }
655 this->updateTitle();
656 fWindow->inval();
657 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400658 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
659 if (!fFontOverrides.fBaselineSnap) {
660 fFontOverrides.fBaselineSnap = true;
661 fFont.setBaselineSnap(false);
662 } else {
663 if (!fFont.isBaselineSnap()) {
664 fFont.setBaselineSnap(true);
665 } else {
666 fFontOverrides.fBaselineSnap = false;
667 }
668 }
669 this->updateTitle();
670 fWindow->inval();
671 });
Brian Osman805a7272018-05-02 15:40:20 -0400672 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
673 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
674 : kPerspective_Real;
675 this->updateTitle();
676 fWindow->inval();
677 });
678 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
679 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
680 : kPerspective_Off;
681 this->updateTitle();
682 fWindow->inval();
683 });
Brian Osman207d4102019-01-10 09:40:58 -0500684 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
685 fAnimTimer.togglePauseResume();
686 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400687 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
688 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500689 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400690 fWindow->inval();
691 });
Mike Reed59295352020-03-12 13:56:34 -0400692 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
693 fDrawViaSerialize = !fDrawViaSerialize;
694 this->updateTitle();
695 fWindow->inval();
696 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500697 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400698 gUseSkVMBlitter = !gUseSkVMBlitter;
699 this->updateTitle();
700 fWindow->inval();
701 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500702 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
703 gSkVMAllowJIT = !gSkVMAllowJIT;
704 this->updateTitle();
705 fWindow->inval();
706 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500707
jvanverth2bb3b6d2016-04-08 07:24:09 -0700708 // set up slides
709 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500710 if (FLAGS_list) {
711 this->listNames();
712 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700713
Brian Osman9bb47cf2018-04-26 15:55:00 -0400714 fPerspectivePoints[0].set(0, 0);
715 fPerspectivePoints[1].set(1, 0);
716 fPerspectivePoints[2].set(0, 1);
717 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700718 fAnimTimer.run();
719
Hal Canaryc465d132017-12-08 10:21:31 -0500720 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500721 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500722 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500723 }
724 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500725
jongdeok.kim804f17e2019-02-26 14:39:23 +0900726 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500727 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700728}
729
jvanverth34524262016-05-04 13:49:13 -0700730void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400731 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
732 static const struct {
733 const char* fExtension;
734 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500735 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400736 const SlideFactory fFactory;
737 } gExternalSlidesInfo[] = {
Brian Salomon06c9e292021-04-29 14:10:23 -0400738 { ".mskp", "mskp-dir", FLAGS_mskps,
739 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
740 return sk_make_sp<MSKPSlide>(name, path);}
741 },
Florin Malita0ffa3222018-04-05 14:34:45 -0400742 { ".skp", "skp-dir", FLAGS_skps,
743 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
744 return sk_make_sp<SKPSlide>(name, path);}
745 },
746 { ".jpg", "jpg-dir", FLAGS_jpgs,
747 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
748 return sk_make_sp<ImageSlide>(name, path);}
749 },
Florin Malita87ccf332018-05-04 12:23:24 -0400750#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400751 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400752 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
753 return sk_make_sp<SkottieSlide>(name, path);}
754 },
Florin Malita87ccf332018-05-04 12:23:24 -0400755#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400756 #if defined(SK_ENABLE_SKRIVE)
757 { ".flr", "skrive-dir", FLAGS_rives,
758 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
759 return sk_make_sp<SkRiveSlide>(name, path);}
760 },
761 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400762#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400763 { ".svg", "svg-dir", FLAGS_svgs,
764 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
765 return sk_make_sp<SvgSlide>(name, path);}
766 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400767#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400768 };
jvanverthc265a922016-04-08 12:51:45 -0700769
Brian Salomon343553a2018-09-05 15:41:23 -0400770 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700771
Mike Klein88544fb2019-03-20 10:50:33 -0500772 const auto addSlide =
773 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
774 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
775 return;
776 }
liyuqian6f163d22016-06-13 12:26:45 -0700777
Mike Klein88544fb2019-03-20 10:50:33 -0500778 if (auto slide = fact(name, path)) {
779 dirSlides.push_back(slide);
780 fSlides.push_back(std::move(slide));
781 }
782 };
Florin Malita76a076b2018-02-15 18:40:48 -0500783
Florin Malita38792ce2018-05-08 10:36:18 -0400784 if (!FLAGS_file.isEmpty()) {
785 // single file mode
786 const SkString file(FLAGS_file[0]);
787
788 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
789 for (const auto& sinfo : gExternalSlidesInfo) {
790 if (file.endsWith(sinfo.fExtension)) {
791 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
792 return;
793 }
794 }
795
796 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
797 } else {
798 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
799 }
800
801 return;
802 }
803
804 // Bisect slide.
805 if (!FLAGS_bisect.isEmpty()) {
806 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500807 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400808 if (FLAGS_bisect.count() >= 2) {
809 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
810 bisect->onChar(*ch);
811 }
812 }
813 fSlides.push_back(std::move(bisect));
814 }
815 }
816
817 // GMs
818 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400819 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400820 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500821 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400822 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400823 fSlides.push_back(std::move(slide));
824 }
Florin Malita38792ce2018-05-08 10:36:18 -0400825 }
826 // reverse gms
827 int numGMs = fSlides.count() - firstGM;
828 for (int i = 0; i < numGMs/2; ++i) {
829 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
830 }
831
832 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400833 for (const SampleFactory factory : SampleRegistry::Range()) {
834 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500835 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400836 fSlides.push_back(slide);
837 }
Florin Malita38792ce2018-05-08 10:36:18 -0400838 }
839
Brian Osman7c979f52019-02-12 13:27:51 -0500840 // Particle demo
841 {
842 // TODO: Convert this to a sample
843 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500844 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500845 fSlides.push_back(std::move(slide));
846 }
847 }
848
Brian Osmand927bd22019-12-18 11:23:12 -0500849 // Runtime shader editor
850 {
851 sk_sp<Slide> slide(new SkSLSlide());
852 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
853 fSlides.push_back(std::move(slide));
854 }
855 }
856
Florin Malita0ffa3222018-04-05 14:34:45 -0400857 for (const auto& info : gExternalSlidesInfo) {
858 for (const auto& flag : info.fFlags) {
859 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
860 // single file
861 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
862 } else {
863 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400864 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400865 SkTArray<SkString> sortedFilenames;
866 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400867 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400868 sortedFilenames.push_back(name);
869 }
870 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400871 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000872 [](const SkString& a, const SkString& b) {
873 return strcmp(a.c_str(), b.c_str()) < 0;
874 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400875 }
876 for (const SkString& filename : sortedFilenames) {
877 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
878 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400879 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400880 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400881 if (!dirSlides.empty()) {
882 fSlides.push_back(
883 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
884 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500885 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400886 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400887 }
888 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500889
890 if (!fSlides.count()) {
891 sk_sp<Slide> slide(new NullSlide());
892 fSlides.push_back(std::move(slide));
893 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700894}
895
896
jvanverth34524262016-05-04 13:49:13 -0700897Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400898 for(auto& slide : fSlides) {
899 slide->gpuTeardown();
900 }
901
jvanverth9f372462016-04-06 06:08:59 -0700902 fWindow->detach();
903 delete fWindow;
904}
905
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500906struct SkPaintTitleUpdater {
907 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
908 void append(const char* s) {
909 if (fCount == 0) {
910 fTitle->append(" {");
911 } else {
912 fTitle->append(", ");
913 }
914 fTitle->append(s);
915 ++fCount;
916 }
917 void done() {
918 if (fCount > 0) {
919 fTitle->append("}");
920 }
921 }
922 SkString* fTitle;
923 int fCount;
924};
925
brianosman05de2162016-05-06 13:28:57 -0700926void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700927 if (!fWindow) {
928 return;
929 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500930 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700931 return; // Surface hasn't been created yet.
932 }
933
jvanverth34524262016-05-04 13:49:13 -0700934 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700935 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700936
Mike Kleine5acd752019-03-22 09:57:16 -0500937 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400938 if (gSkForceAnalyticAA) {
939 title.append(" <FAAA>");
940 } else {
941 title.append(" <AAA>");
942 }
943 }
Mike Reed59295352020-03-12 13:56:34 -0400944 if (fDrawViaSerialize) {
945 title.append(" <serialize>");
946 }
Mike Reed862818b2020-03-21 15:07:13 -0400947 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500948 title.append(" <SkVMBlitter>");
949 }
950 if (!gSkVMAllowJIT) {
951 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400952 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400953
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500954 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500955 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
956 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400957 const char* on, const char* off)
958 {
Ben Wagner9613e452019-01-23 10:34:59 -0500959 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400960 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500961 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400962 };
963
Ben Wagner9613e452019-01-23 10:34:59 -0500964 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
965 const char* on, const char* off)
966 {
967 if (fFontOverrides.*flag) {
968 paintTitle.append((fFont.*isFlag)() ? on : off);
969 }
970 };
971
972 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
973 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
974
975 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
976 "Force Autohint", "No Force Autohint");
977 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400978 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500979 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
980 "Linear Metrics", "Non-Linear Metrics");
981 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
982 "Bitmap Text", "No Bitmap Text");
983 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
984
985 if (fFontOverrides.fEdging) {
986 switch (fFont.getEdging()) {
987 case SkFont::Edging::kAlias:
988 paintTitle.append("Alias Text");
989 break;
990 case SkFont::Edging::kAntiAlias:
991 paintTitle.append("Antialias Text");
992 break;
993 case SkFont::Edging::kSubpixelAntiAlias:
994 paintTitle.append("Subpixel Antialias Text");
995 break;
996 }
997 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400998
Mike Reed3ae47332019-01-04 10:11:46 -0500999 if (fFontOverrides.fHinting) {
1000 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -04001001 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001002 paintTitle.append("No Hinting");
1003 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001004 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001005 paintTitle.append("Slight Hinting");
1006 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001007 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001008 paintTitle.append("Normal Hinting");
1009 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001010 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001011 paintTitle.append("Full Hinting");
1012 break;
1013 }
1014 }
1015 paintTitle.done();
1016
Brian Osman92004802017-03-06 11:47:26 -05001017 switch (fColorMode) {
1018 case ColorMode::kLegacy:
1019 title.append(" Legacy 8888");
1020 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001021 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001022 title.append(" ColorManaged 8888");
1023 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001024 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001025 title.append(" ColorManaged F16");
1026 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001027 case ColorMode::kColorManagedF16Norm:
1028 title.append(" ColorManaged F16 Norm");
1029 break;
Brian Osman92004802017-03-06 11:47:26 -05001030 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001031
Brian Osman92004802017-03-06 11:47:26 -05001032 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001033 int curPrimaries = -1;
1034 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1035 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1036 curPrimaries = i;
1037 break;
1038 }
1039 }
Brian Osman03115dc2018-11-26 13:55:19 -05001040 title.appendf(" %s Gamma %f",
1041 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001042 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001043 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001044
Ben Wagner37c54032018-04-13 14:30:23 -04001045 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001046 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001047 switch (params.fSurfaceProps.pixelGeometry()) {
1048 case kUnknown_SkPixelGeometry:
1049 title.append( " Flat");
1050 break;
1051 case kRGB_H_SkPixelGeometry:
1052 title.append( " RGB");
1053 break;
1054 case kBGR_H_SkPixelGeometry:
1055 title.append( " BGR");
1056 break;
1057 case kRGB_V_SkPixelGeometry:
1058 title.append( " RGBV");
1059 break;
1060 case kBGR_V_SkPixelGeometry:
1061 title.append( " BGRV");
1062 break;
1063 }
1064 }
1065
1066 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1067 title.append(" DFT");
1068 }
1069
csmartdalton578f0642017-02-24 16:04:47 -07001070 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001071 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001072 int msaa = fWindow->sampleCount();
1073 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001074 title.appendf(" MSAA: %i", msaa);
1075 }
1076 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001077
1078 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001079 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001080 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1081 }
1082
Brian Osman805a7272018-05-02 15:40:20 -04001083 if (kPerspective_Real == fPerspectiveMode) {
1084 title.append(" Perpsective (Real)");
1085 } else if (kPerspective_Fake == fPerspectiveMode) {
1086 title.append(" Perspective (Fake)");
1087 }
1088
brianosman05de2162016-05-06 13:28:57 -07001089 fWindow->setTitle(title.c_str());
1090}
1091
Florin Malitaab99c342018-01-16 16:23:03 -05001092int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001093
1094 if (!FLAGS_slide.isEmpty()) {
1095 int count = fSlides.count();
1096 for (int i = 0; i < count; i++) {
1097 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001098 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001099 }
1100 }
1101
1102 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1103 this->listNames();
1104 }
1105
Florin Malitaab99c342018-01-16 16:23:03 -05001106 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001107}
1108
Florin Malitaab99c342018-01-16 16:23:03 -05001109void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001110 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001111 for (const auto& slide : fSlides) {
1112 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001113 }
1114}
1115
Florin Malitaab99c342018-01-16 16:23:03 -05001116void Viewer::setCurrentSlide(int slide) {
1117 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001118
Florin Malitaab99c342018-01-16 16:23:03 -05001119 if (slide == fCurrentSlide) {
1120 return;
1121 }
1122
1123 if (fCurrentSlide >= 0) {
1124 fSlides[fCurrentSlide]->unload();
1125 }
1126
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001127 SkScalar scaleFactor = 1.0;
1128 if (fApplyBackingScale) {
1129 scaleFactor = fWindow->scaleFactor();
1130 }
1131 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1132 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001133 fCurrentSlide = slide;
1134 this->setupCurrentSlide();
1135}
1136
1137void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001138 if (fCurrentSlide >= 0) {
1139 // prepare dimensions for image slides
1140 fGesture.resetTouchState();
1141 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001142
Jim Van Verth0848fb02018-01-22 13:39:30 -05001143 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1144 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1145 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001146
Jim Van Verth0848fb02018-01-22 13:39:30 -05001147 // Start with a matrix that scales the slide to the available screen space
1148 if (fWindow->scaleContentToFit()) {
1149 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001150 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1151 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001152 }
liyuqiane46e4f02016-05-20 07:32:19 -07001153 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001154
1155 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001156 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001157
1158 this->updateTitle();
1159 this->updateUIState();
1160
1161 fStatsLayer.resetMeasurements();
1162
1163 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001164 }
jvanverthc265a922016-04-08 12:51:45 -07001165}
1166
Brian Osmanaba642c2020-02-06 12:52:25 -05001167#define MAX_ZOOM_LEVEL 8.0f
1168#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001169
jvanverth34524262016-05-04 13:49:13 -07001170void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001171 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001172 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001173 this->preTouchMatrixChanged();
1174}
Yuqian Li755778c2018-03-28 16:23:31 -04001175
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001176void Viewer::preTouchMatrixChanged() {
1177 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001178 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1179 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1180 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1181 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1182}
1183
Brian Osman805a7272018-05-02 15:40:20 -04001184SkMatrix Viewer::computePerspectiveMatrix() {
1185 SkScalar w = fWindow->width(), h = fWindow->height();
1186 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1187 SkPoint perspPts[4] = {
1188 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1189 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1190 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1191 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1192 };
1193 SkMatrix m;
1194 m.setPolyToPoly(orthoPts, perspPts, 4);
1195 return m;
1196}
1197
Yuqian Li755778c2018-03-28 16:23:31 -04001198SkMatrix Viewer::computePreTouchMatrix() {
1199 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001200
1201 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001202 if (fApplyBackingScale) {
1203 zoomScale *= fWindow->scaleFactor();
1204 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001205 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001206 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001207
1208 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1209 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001210
Brian Osman805a7272018-05-02 15:40:20 -04001211 if (kPerspective_Real == fPerspectiveMode) {
1212 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001213 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001214 }
1215
Yuqian Li755778c2018-03-28 16:23:31 -04001216 return m;
jvanverthc265a922016-04-08 12:51:45 -07001217}
1218
liyuqiand3cdbca2016-05-17 12:44:20 -07001219SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001220 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001221 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001222 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001223 return m;
jvanverthc265a922016-04-08 12:51:45 -07001224}
1225
Brian Osman621491e2017-02-28 15:45:01 -05001226void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001227 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001228 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001229 fBackendType = backendType;
1230
Robert Phillipse9229532020-06-26 10:10:49 -04001231 // The active context is going away in 'detach'
1232 for(auto& slide : fSlides) {
1233 slide->gpuTeardown();
1234 }
1235
Brian Osman621491e2017-02-28 15:45:01 -05001236 fWindow->detach();
1237
Brian Osman70d2f432017-11-08 09:54:10 -05001238#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001239 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1240 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001241 DisplayParams params = fWindow->getRequestedDisplayParams();
1242 delete fWindow;
1243 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001244
Brian Osman70d2f432017-11-08 09:54:10 -05001245 // re-register callbacks
1246 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001247 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001248 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001249 fWindow->pushLayer(&fImGuiLayer);
1250
Brian Osman70d2f432017-11-08 09:54:10 -05001251 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1252 // will still include our correct sample count. But the re-created fWindow will lose that
1253 // information. On Windows, we need to re-create the window when changing sample count,
1254 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1255 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1256 // as if we had never changed windows at all).
1257 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001258#endif
1259
Brian Osman70d2f432017-11-08 09:54:10 -05001260 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001261}
1262
Brian Osman92004802017-03-06 11:47:26 -05001263void Viewer::setColorMode(ColorMode colorMode) {
1264 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001265 this->updateTitle();
1266 fWindow->inval();
1267}
1268
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001269class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1270public:
Mike Reed3ae47332019-01-04 10:11:46 -05001271 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1272 SkFont* font, Viewer::SkFontFields* ffields)
1273 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001274 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001275 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1276 sk_sp<SkTextBlob>* cache) {
1277 bool blobWillChange = false;
1278 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001279 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1280 bool shouldDraw = this->filterFont(&filteredFont);
1281 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001282 blobWillChange = true;
1283 break;
1284 }
1285 }
1286 if (!blobWillChange) {
1287 return blob;
1288 }
1289
1290 SkTextBlobBuilder builder;
1291 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001292 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1293 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001294 if (!shouldDraw) {
1295 continue;
1296 }
1297
Mike Reed3ae47332019-01-04 10:11:46 -05001298 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001299
Ben Wagner41e40472018-09-24 13:01:54 -04001300 const SkTextBlobBuilder::RunBuffer& runBuffer
1301 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001302 ? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(),
1303 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001304 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001305 ? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(),
1306 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001307 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001308 ? builder.allocRunTextPos(font, it.glyphCount(), it.textSize())
Ben Wagnere5736262021-02-08 16:52:08 -05001309 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001310 ? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001311 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1312 uint32_t glyphCount = it.glyphCount();
1313 if (it.glyphs()) {
1314 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1315 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1316 }
1317 if (it.pos()) {
1318 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001319 unsigned posPerGlyph = it.scalarsPerGlyph();
1320 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001321 }
1322 if (it.text()) {
1323 size_t textSize = sizeof(decltype(*it.text()));
1324 uint32_t textCount = it.textSize();
1325 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1326 }
1327 if (it.clusters()) {
1328 size_t clusterSize = sizeof(decltype(*it.clusters()));
1329 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1330 }
1331 }
1332 *cache = builder.make();
1333 return cache->get();
1334 }
1335 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1336 const SkPaint& paint) override {
1337 sk_sp<SkTextBlob> cache;
1338 this->SkPaintFilterCanvas::onDrawTextBlob(
1339 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1340 }
Mike Reed3ae47332019-01-04 10:11:46 -05001341 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001342 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001343 font->writable()->setSize(fFont->getSize());
1344 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001345 if (fFontOverrides->fScaleX) {
1346 font->writable()->setScaleX(fFont->getScaleX());
1347 }
1348 if (fFontOverrides->fSkewX) {
1349 font->writable()->setSkewX(fFont->getSkewX());
1350 }
Mike Reed3ae47332019-01-04 10:11:46 -05001351 if (fFontOverrides->fHinting) {
1352 font->writable()->setHinting(fFont->getHinting());
1353 }
Ben Wagner9613e452019-01-23 10:34:59 -05001354 if (fFontOverrides->fEdging) {
1355 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001356 }
Ben Wagner9613e452019-01-23 10:34:59 -05001357 if (fFontOverrides->fEmbolden) {
1358 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001359 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001360 if (fFontOverrides->fBaselineSnap) {
1361 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1362 }
Ben Wagner9613e452019-01-23 10:34:59 -05001363 if (fFontOverrides->fLinearMetrics) {
1364 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001365 }
Ben Wagner9613e452019-01-23 10:34:59 -05001366 if (fFontOverrides->fSubpixel) {
1367 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001368 }
Ben Wagner9613e452019-01-23 10:34:59 -05001369 if (fFontOverrides->fEmbeddedBitmaps) {
1370 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001371 }
Ben Wagner9613e452019-01-23 10:34:59 -05001372 if (fFontOverrides->fForceAutoHinting) {
1373 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001374 }
Ben Wagner9613e452019-01-23 10:34:59 -05001375
Mike Reed3ae47332019-01-04 10:11:46 -05001376 return true;
1377 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001378 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001379 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001380 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001381 }
Ben Wagner9613e452019-01-23 10:34:59 -05001382 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001383 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001384 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001385 if (fPaintOverrides->fStyle) {
1386 paint.setStyle(fPaint->getStyle());
1387 }
1388 if (fPaintOverrides->fWidth) {
1389 paint.setStrokeWidth(fPaint->getStrokeWidth());
1390 }
1391 if (fPaintOverrides->fMiterLimit) {
1392 paint.setStrokeMiter(fPaint->getStrokeMiter());
1393 }
1394 if (fPaintOverrides->fCapType) {
1395 paint.setStrokeCap(fPaint->getStrokeCap());
1396 }
1397 if (fPaintOverrides->fJoinType) {
1398 paint.setStrokeJoin(fPaint->getStrokeJoin());
1399 }
John Stiles9671d0a2021-07-16 16:22:03 -04001400 if (fPaintOverrides->fForceRuntimeBlend) {
1401 if (skstd::optional<SkBlendMode> mode = paint.asBlendMode()) {
1402 paint.setBlender(GetRuntimeBlendForBlendMode(*mode));
1403 }
1404 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001405 return true;
1406 }
1407 SkPaint* fPaint;
1408 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001409 SkFont* fFont;
1410 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001411};
1412
Robert Phillips9882dae2019-03-04 11:00:10 -05001413void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001414 if (fCurrentSlide < 0) {
1415 return;
1416 }
1417
Robert Phillips9882dae2019-03-04 11:00:10 -05001418 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001419
Brian Osmanf750fbc2017-02-08 10:47:28 -05001420 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001421 SkSurface* slideSurface = surface;
1422 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001423 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001424
Brian Osmane0d4fba2017-03-15 10:24:55 -04001425 // 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 -05001426 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001427 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001428 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001429 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001430 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001431 }
1432
Brian Osman3ac99cf2017-12-01 11:23:53 -05001433 if (fSaveToSKP) {
1434 SkPictureRecorder recorder;
1435 SkCanvas* recorderCanvas = recorder.beginRecording(
1436 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001437 fSlides[fCurrentSlide]->draw(recorderCanvas);
1438 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1439 SkFILEWStream stream("sample_app.skp");
1440 picture->serialize(&stream);
1441 fSaveToSKP = false;
1442 }
1443
Brian Osmane9ed0f02018-11-26 14:50:05 -05001444 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001445 SkColorType colorType;
1446 switch (fColorMode) {
1447 case ColorMode::kLegacy:
1448 case ColorMode::kColorManaged8888:
1449 colorType = kN32_SkColorType;
1450 break;
1451 case ColorMode::kColorManagedF16:
1452 colorType = kRGBA_F16_SkColorType;
1453 break;
1454 case ColorMode::kColorManagedF16Norm:
1455 colorType = kRGBA_F16Norm_SkColorType;
1456 break;
1457 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001458
1459 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001460 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001461 slideCanvas->getProps(&props);
1462
Brian Osmane9ed0f02018-11-26 14:50:05 -05001463 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1464 return Window::kRaster_BackendType == this->fBackendType
1465 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001466 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001467 };
1468
Brian Osman03115dc2018-11-26 13:55:19 -05001469 // We need to render offscreen if we're...
1470 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1471 // ... in any raster mode, because the window surface is actually GL
1472 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001473 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001474 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001475 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001476 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001477 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001478 colorSpace != nullptr ||
1479 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001480
Brian Osmane9ed0f02018-11-26 14:50:05 -05001481 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001482 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001483 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001484 }
1485
Mike Reed59295352020-03-12 13:56:34 -04001486 SkPictureRecorder recorder;
1487 SkCanvas* recorderRestoreCanvas = nullptr;
1488 if (fDrawViaSerialize) {
1489 recorderRestoreCanvas = slideCanvas;
1490 slideCanvas = recorder.beginRecording(
1491 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1492 }
1493
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001494 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001495 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001496 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001497 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001498 if (fTiled) {
1499 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1500 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001501 for (int y = 0; y < fWindow->height(); y += tileH) {
1502 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001503 SkAutoCanvasRestore acr(slideCanvas, true);
1504 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1505 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001506 }
1507 }
1508
1509 // Draw borders between tiles
1510 if (fDrawTileBoundaries) {
1511 SkPaint border;
1512 border.setColor(0x60FF00FF);
1513 border.setStyle(SkPaint::kStroke_Style);
1514 for (int y = 0; y < fWindow->height(); y += tileH) {
1515 for (int x = 0; x < fWindow->width(); x += tileW) {
1516 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1517 }
1518 }
1519 }
1520 } else {
1521 slideCanvas->concat(this->computeMatrix());
1522 if (kPerspective_Real == fPerspectiveMode) {
1523 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1524 }
John Stiles9671d0a2021-07-16 16:22:03 -04001525 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides,
1526 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001527 fSlides[fCurrentSlide]->draw(&filterCanvas);
1528 }
Brian Osman56a24812017-12-19 11:15:16 -05001529 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001530 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001531
Mike Reed59295352020-03-12 13:56:34 -04001532 if (recorderRestoreCanvas) {
1533 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1534 auto data = picture->serialize();
1535 slideCanvas = recorderRestoreCanvas;
1536 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1537 }
1538
Brian Osman1df161a2017-02-09 12:10:20 -05001539 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001540 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001541 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001542 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001543
1544 // If we rendered offscreen, snap an image and push the results to the window's canvas
1545 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001546 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001547
Robert Phillips9882dae2019-03-04 11:00:10 -05001548 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001549 SkPaint paint;
1550 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001551 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001552 int prePerspectiveCount = canvas->save();
1553 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001554 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001555 canvas->clear(SK_ColorWHITE);
1556 canvas->concat(this->computePerspectiveMatrix());
1557 }
Mike Reedb339d052021-01-28 11:20:41 -05001558 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001559 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001560 }
Mike Reed376d8122019-03-14 11:39:02 -04001561
1562 if (fShowSlideDimensions) {
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001563 SkCanvas* canvas = surface->getCanvas();
1564 SkAutoCanvasRestore acr(canvas, true);
1565 canvas->concat(this->computeMatrix());
Mike Reed376d8122019-03-14 11:39:02 -04001566 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1567 SkPaint paint;
1568 paint.setColor(0x40FFFF00);
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001569 canvas->drawRect(r, paint);
Mike Reed376d8122019-03-14 11:39:02 -04001570 }
liyuqian6f163d22016-06-13 12:26:45 -07001571}
1572
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001573void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001574 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001575 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001576}
Jim Van Verth6f449692017-02-14 15:16:46 -05001577
Robert Phillips9882dae2019-03-04 11:00:10 -05001578void Viewer::onPaint(SkSurface* surface) {
1579 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001580
Robert Phillips9882dae2019-03-04 11:00:10 -05001581 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001582
Brian Osmand67e5182017-12-08 16:46:09 -05001583 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001584
Greg Daniel427d8eb2020-09-28 15:04:18 -04001585 fLastImage.reset();
1586
Robert Phillipsed653392020-07-10 13:55:21 -04001587 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001588 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001589 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001590 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001591}
1592
Ben Wagnera1915972018-08-09 15:06:19 -04001593void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001594 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001595 SkScalar scaleFactor = 1.0;
1596 if (fApplyBackingScale) {
1597 scaleFactor = fWindow->scaleFactor();
1598 }
1599 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001600 }
Ben Wagnera1915972018-08-09 15:06:19 -04001601}
1602
Florin Malitacefc1b92018-02-19 21:43:47 -05001603SkPoint Viewer::mapEvent(float x, float y) {
1604 const auto m = this->computeMatrix();
1605 SkMatrix inv;
1606
1607 SkAssertResult(m.invert(&inv));
1608
1609 return inv.mapXY(x, y);
1610}
1611
Hal Canaryb1f411a2019-08-29 10:39:22 -04001612bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001613 if (GestureDevice::kMouse == fGestureDevice) {
1614 return false;
1615 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001616
1617 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001618 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001619 fWindow->inval();
1620 return true;
1621 }
1622
liyuqiand3cdbca2016-05-17 12:44:20 -07001623 void* castedOwner = reinterpret_cast<void*>(owner);
1624 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001625 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001626 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001627#if defined(SK_BUILD_FOR_IOS)
1628 // TODO: move IOS swipe detection higher up into the platform code
1629 SkPoint dir;
1630 if (fGesture.isFling(&dir)) {
1631 // swiping left or right
1632 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1633 if (dir.fX < 0) {
1634 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1635 fCurrentSlide + 1 : 0);
1636 } else {
1637 this->setCurrentSlide(fCurrentSlide > 0 ?
1638 fCurrentSlide - 1 : fSlides.count() - 1);
1639 }
1640 }
1641 fGesture.reset();
1642 }
1643#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001644 break;
1645 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001646 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001647 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001648 break;
1649 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001650 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001651 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001652 break;
1653 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001654 default: {
1655 // kLeft and kRight are only for swipes
1656 SkASSERT(false);
1657 break;
1658 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001659 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001660 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001661 fWindow->inval();
1662 return true;
1663}
1664
Hal Canaryb1f411a2019-08-29 10:39:22 -04001665bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001666 if (GestureDevice::kTouch == fGestureDevice) {
1667 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001668 }
Brian Osman16c81a12017-12-20 11:58:34 -05001669
Florin Malitacefc1b92018-02-19 21:43:47 -05001670 const auto slidePt = this->mapEvent(x, y);
1671 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1672 fWindow->inval();
1673 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001674 }
1675
1676 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001677 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001678 fGesture.touchEnd(nullptr);
1679 break;
1680 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001681 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001682 fGesture.touchBegin(nullptr, x, y);
1683 break;
1684 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001685 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001686 fGesture.touchMoved(nullptr, x, y);
1687 break;
1688 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001689 default: {
1690 SkASSERT(false); // shouldn't see kRight or kLeft here
1691 break;
1692 }
Brian Osman16c81a12017-12-20 11:58:34 -05001693 }
1694 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1695
Hal Canaryb1f411a2019-08-29 10:39:22 -04001696 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001697 fWindow->inval();
1698 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001699 return true;
1700}
1701
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001702bool Viewer::onFling(skui::InputState state) {
1703 if (skui::InputState::kRight == state) {
1704 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1705 return true;
1706 } else if (skui::InputState::kLeft == state) {
1707 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1708 return true;
1709 }
1710 return false;
1711}
1712
1713bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1714 switch (state) {
1715 case skui::InputState::kDown:
1716 fGesture.startZoom();
1717 return true;
1718 break;
1719 case skui::InputState::kMove:
1720 fGesture.updateZoom(scale, x, y, x, y);
1721 return true;
1722 break;
1723 case skui::InputState::kUp:
1724 fGesture.endZoom();
1725 return true;
1726 break;
1727 default:
1728 SkASSERT(false);
1729 break;
1730 }
1731
1732 return false;
1733}
1734
Brian Osmana109e392017-02-24 09:49:14 -05001735static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001736 // The gamut image covers a (0.8 x 0.9) shaped region
1737 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001738
1739 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1740 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1741 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001742 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1743 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1744 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001745
Brian Osman535c5e32019-02-09 16:32:58 -05001746 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1747 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1748 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1749 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1750 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001751}
1752
Ben Wagner3627d2e2018-06-26 14:23:20 -04001753static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001754 ImGui::DragCanvas dc(pt);
1755 dc.fillColor(IM_COL32(0, 0, 0, 128));
1756 dc.dragPoint(pt);
1757 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001758}
1759
Brian Osman9bb47cf2018-04-26 15:55:00 -04001760static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001761 ImGui::DragCanvas dc(pts);
1762 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001763
Brian Osman535c5e32019-02-09 16:32:58 -05001764 for (int i = 0; i < 4; ++i) {
1765 dc.dragPoint(pts + i);
1766 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001767
Brian Osman535c5e32019-02-09 16:32:58 -05001768 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1769 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1770 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1771 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001772
Brian Osman535c5e32019-02-09 16:32:58 -05001773 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001774}
1775
John Stiles38b7d2f2020-06-24 12:13:31 -04001776static SkSL::String build_sksl_highlight_shader() {
1777 return SkSL::String("out half4 sk_FragColor;\n"
1778 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1779}
1780
1781static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1782 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1783 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1784 size_t pos = inShader.rfind("return *_out;\n");
1785 if (pos == std::string::npos) {
1786 return inShader;
1787 }
1788
1789 SkSL::String replacementShader = inShader;
1790 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1791 return replacementShader;
1792}
1793
1794static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1795 const char* versionDecl = shaderCaps.versionDeclString();
1796 SkSL::String highlight = versionDecl ? versionDecl : "";
1797 if (shaderCaps.usesPrecisionModifiers()) {
1798 highlight.append("precision mediump float;\n");
1799 }
1800 highlight.appendf("out vec4 sk_FragColor;\n"
1801 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1802 return highlight;
1803}
1804
Brian Osmand67e5182017-12-08 16:46:09 -05001805void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001806 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1807 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001808 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001809 }
1810
1811 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001812 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1813 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001814 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001815 DisplayParams params = fWindow->getRequestedDisplayParams();
1816 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001817 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001818
Brian Osmana109e392017-02-24 09:49:14 -05001819 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1820 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001821 if (ImGui::CollapsingHeader("Backend")) {
1822 int newBackend = static_cast<int>(fBackendType);
1823 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1824 ImGui::SameLine();
1825 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001826#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1827 ImGui::SameLine();
1828 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1829#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001830#if defined(SK_DAWN)
1831 ImGui::SameLine();
1832 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1833#endif
John Stilesf7da9232020-11-19 19:58:14 -05001834#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001835 ImGui::SameLine();
1836 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1837#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001838#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001839 ImGui::SameLine();
1840 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1841#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001842#if defined(SK_DIRECT3D)
1843 ImGui::SameLine();
1844 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1845#endif
Brian Osman621491e2017-02-28 15:45:01 -05001846 if (newBackend != fBackendType) {
1847 fDeferredActions.push_back([=]() {
1848 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1849 });
1850 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001851
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001852 bool* wire = &params.fGrContextOptions.fWireframeMode;
1853 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1854 paramsChanged = true;
1855 }
Brian Salomon99a33902017-03-07 15:16:34 -05001856
Brian Salomon91216d52021-04-09 11:57:59 -04001857 bool* reducedShaders = &params.fGrContextOptions.fReducedShaderVariations;
1858 if (ctx && ImGui::Checkbox("Reduced shaders", reducedShaders)) {
1859 paramsChanged = true;
1860 }
1861
Brian Osman28b12522017-03-08 17:10:24 -05001862 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001863 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001864 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001865 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1866 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1867 1;
1868
1869 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1870 if (maxMSAA >= 4) {
1871 ImGui::Text("MSAA: ");
1872
1873 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1874 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1875 // don't show it in the list.
1876 if (curMSAA == 2) {
1877 continue;
1878 }
1879 ImGui::SameLine();
1880 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1881 &sampleCount, curMSAA);
1882 }
1883 }
Brian Osman28b12522017-03-08 17:10:24 -05001884
1885 if (sampleCount != params.fMSAASampleCount) {
1886 params.fMSAASampleCount = sampleCount;
1887 paramsChanged = true;
1888 }
1889 }
1890
Ben Wagner37c54032018-04-13 14:30:23 -04001891 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001892 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001893 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1894 }
1895 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1896 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1897 {
1898 uint32_t flags = params.fSurfaceProps.flags();
1899 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001900 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1901 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1902 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001903 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001904 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001905 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1906 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1907 }
1908 paramsChanged = true;
1909 }
1910
1911 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1912 if (ImGui::Checkbox("DFT", &useDFT)) {
1913 uint32_t flags = params.fSurfaceProps.flags();
1914 if (useDFT) {
1915 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1916 } else {
1917 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1918 }
1919 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1920 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1921 paramsChanged = true;
1922 }
1923
Brian Osman8a9de3d2017-03-01 14:59:05 -05001924 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001925 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001926 auto prButton = [&](GpuPathRenderers x) {
1927 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001928 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1929 params.fGrContextOptions.fGpuPathRenderers = x;
1930 paramsChanged = true;
1931 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001932 }
1933 };
1934
1935 if (!ctx) {
1936 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001937 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001938 const auto* caps = ctx->priv().caps();
1939 prButton(GpuPathRenderers::kDefault);
Chris Dalton57ab06c2021-04-22 12:57:28 -06001940 if (fWindow->sampleCount() > 1 || FLAGS_dmsaa) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001941 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001942 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001943 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001944 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001945 if (1 == fWindow->sampleCount()) {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001946 prButton(GpuPathRenderers::kSmall);
1947 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001948 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001949 prButton(GpuPathRenderers::kNone);
1950 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001951 ImGui::TreePop();
1952 }
Brian Osman621491e2017-02-28 15:45:01 -05001953 }
1954
Ben Wagner964571d2019-03-08 12:35:06 -05001955 if (ImGui::CollapsingHeader("Tiling")) {
1956 ImGui::Checkbox("Enable", &fTiled);
1957 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1958 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1959 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1960 }
1961
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001962 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001963 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1964 this->preTouchMatrixChanged();
1965 this->onResize(fWindow->width(), fWindow->height());
1966 paramsChanged = true;
1967 }
1968
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001969 float zoom = fZoomLevel;
1970 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1971 fZoomLevel = zoom;
1972 this->preTouchMatrixChanged();
1973 paramsChanged = true;
1974 }
1975 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001976 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001977 fRotation = deg;
1978 this->preTouchMatrixChanged();
1979 paramsChanged = true;
1980 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001981 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1982 if (ImGui_DragLocation(&fOffset)) {
1983 this->preTouchMatrixChanged();
1984 paramsChanged = true;
1985 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001986 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1987 this->preTouchMatrixChanged();
1988 paramsChanged = true;
1989 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001990 }
Brian Osman805a7272018-05-02 15:40:20 -04001991 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1992 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1993 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001994 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001995 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001996 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001997 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001998 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001999 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04002000 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04002001 }
2002
Ben Wagnera580fb32018-04-17 11:16:32 -04002003 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04002004 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002005 if (fPaintOverrides.fAntiAlias) {
2006 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002007 }
2008 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05002009 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04002010 {
2011 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
2012 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04002013 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002014 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
2015 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002016 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002017 fPaintOverrides.fAntiAlias = true;
2018 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04002019 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05002020 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04002021 case SkPaintFields::AntiAliasState::Alias:
2022 break;
2023 case SkPaintFields::AntiAliasState::Normal:
2024 break;
2025 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
2026 gSkUseAnalyticAA = true;
2027 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002028 break;
2029 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2030 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002031 break;
2032 }
2033 }
2034 paramsChanged = true;
2035 }
2036
Ben Wagner99a78dc2018-05-09 18:23:51 -04002037 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002038 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002039 bool (SkPaint::* isFlag)() const,
2040 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002041 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002042 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002043 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002044 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002045 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002046 if (ImGui::Combo(label, &itemIndex, items)) {
2047 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002048 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002049 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002050 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002051 (fPaint.*setFlag)(itemIndex == 2);
2052 }
2053 paramsChanged = true;
2054 }
2055 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002056
Ben Wagner99a78dc2018-05-09 18:23:51 -04002057 paintFlag("Dither",
2058 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002059 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002060 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002061
2062 int styleIdx = 0;
2063 if (fPaintOverrides.fStyle) {
2064 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2065 }
2066 if (ImGui::Combo("Style", &styleIdx,
2067 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2068 {
2069 if (styleIdx == 0) {
2070 fPaintOverrides.fStyle = false;
2071 fPaint.setStyle(SkPaint::kFill_Style);
2072 } else {
2073 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2074 fPaintOverrides.fStyle = true;
2075 }
2076 paramsChanged = true;
2077 }
2078
John Stiles9671d0a2021-07-16 16:22:03 -04002079 ImGui::Checkbox("Force Runtime Blends", &fPaintOverrides.fForceRuntimeBlend);
2080
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002081 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2082 if (fPaintOverrides.fWidth) {
2083 float width = fPaint.getStrokeWidth();
2084 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2085 fPaint.setStrokeWidth(width);
2086 paramsChanged = true;
2087 }
2088 }
2089
2090 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2091 if (fPaintOverrides.fMiterLimit) {
2092 float miterLimit = fPaint.getStrokeMiter();
2093 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2094 fPaint.setStrokeMiter(miterLimit);
2095 paramsChanged = true;
2096 }
2097 }
2098
2099 int capIdx = 0;
2100 if (fPaintOverrides.fCapType) {
2101 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2102 }
2103 if (ImGui::Combo("Cap Type", &capIdx,
2104 "Default\0Butt\0Round\0Square\0\0"))
2105 {
2106 if (capIdx == 0) {
2107 fPaintOverrides.fCapType = false;
2108 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2109 } else {
2110 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2111 fPaintOverrides.fCapType = true;
2112 }
2113 paramsChanged = true;
2114 }
2115
2116 int joinIdx = 0;
2117 if (fPaintOverrides.fJoinType) {
2118 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2119 }
2120 if (ImGui::Combo("Join Type", &joinIdx,
2121 "Default\0Miter\0Round\0Bevel\0\0"))
2122 {
2123 if (joinIdx == 0) {
2124 fPaintOverrides.fJoinType = false;
2125 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2126 } else {
2127 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2128 fPaintOverrides.fJoinType = true;
2129 }
2130 paramsChanged = true;
2131 }
Ben Wagner9613e452019-01-23 10:34:59 -05002132 }
Hal Canary02738a82019-01-21 18:51:32 +00002133
Ben Wagner9613e452019-01-23 10:34:59 -05002134 if (ImGui::CollapsingHeader("Font")) {
2135 int hintingIdx = 0;
2136 if (fFontOverrides.fHinting) {
2137 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2138 }
2139 if (ImGui::Combo("Hinting", &hintingIdx,
2140 "Default\0None\0Slight\0Normal\0Full\0\0"))
2141 {
2142 if (hintingIdx == 0) {
2143 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002144 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002145 } else {
2146 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2147 fFontOverrides.fHinting = true;
2148 }
2149 paramsChanged = true;
2150 }
Hal Canary02738a82019-01-21 18:51:32 +00002151
Ben Wagner9613e452019-01-23 10:34:59 -05002152 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2153 bool SkFontFields::* flag,
2154 bool (SkFont::* isFlag)() const,
2155 void (SkFont::* setFlag)(bool) )
2156 {
2157 int itemIndex = 0;
2158 if (fFontOverrides.*flag) {
2159 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2160 }
2161 if (ImGui::Combo(label, &itemIndex, items)) {
2162 if (itemIndex == 0) {
2163 fFontOverrides.*flag = false;
2164 } else {
2165 fFontOverrides.*flag = true;
2166 (fFont.*setFlag)(itemIndex == 2);
2167 }
2168 paramsChanged = true;
2169 }
2170 };
Hal Canary02738a82019-01-21 18:51:32 +00002171
Ben Wagner9613e452019-01-23 10:34:59 -05002172 fontFlag("Fake Bold Glyphs",
2173 "Default\0No Fake Bold\0Fake Bold\0\0",
2174 &SkFontFields::fEmbolden,
2175 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002176
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002177 fontFlag("Baseline Snapping",
2178 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2179 &SkFontFields::fBaselineSnap,
2180 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2181
Ben Wagner9613e452019-01-23 10:34:59 -05002182 fontFlag("Linear Text",
2183 "Default\0No Linear Text\0Linear Text\0\0",
2184 &SkFontFields::fLinearMetrics,
2185 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002186
Ben Wagner9613e452019-01-23 10:34:59 -05002187 fontFlag("Subpixel Position Glyphs",
2188 "Default\0Pixel Text\0Subpixel Text\0\0",
2189 &SkFontFields::fSubpixel,
2190 &SkFont::isSubpixel, &SkFont::setSubpixel);
2191
2192 fontFlag("Embedded Bitmap Text",
2193 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2194 &SkFontFields::fEmbeddedBitmaps,
2195 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2196
2197 fontFlag("Force Auto-Hinting",
2198 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2199 &SkFontFields::fForceAutoHinting,
2200 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2201
2202 int edgingIdx = 0;
2203 if (fFontOverrides.fEdging) {
2204 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2205 }
2206 if (ImGui::Combo("Edging", &edgingIdx,
2207 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2208 {
2209 if (edgingIdx == 0) {
2210 fFontOverrides.fEdging = false;
2211 fFont.setEdging(SkFont::Edging::kAlias);
2212 } else {
2213 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2214 fFontOverrides.fEdging = true;
2215 }
2216 paramsChanged = true;
2217 }
2218
Ben Wagner15a8d572019-03-21 13:35:44 -04002219 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2220 if (fFontOverrides.fSize) {
2221 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002222 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002223 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002224 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002225 fFontOverrides.fSizeRange[0],
2226 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002227 "%.6f", 2.0f))
2228 {
Mike Reed3ae47332019-01-04 10:11:46 -05002229 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002230 paramsChanged = true;
2231 }
2232 }
2233
2234 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2235 if (fFontOverrides.fScaleX) {
2236 float scaleX = fFont.getScaleX();
2237 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2238 fFont.setScaleX(scaleX);
2239 paramsChanged = true;
2240 }
2241 }
2242
2243 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2244 if (fFontOverrides.fSkewX) {
2245 float skewX = fFont.getSkewX();
2246 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2247 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002248 paramsChanged = true;
2249 }
2250 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002251 }
2252
Mike Reed81f60ec2018-05-15 10:09:52 -04002253 {
2254 SkMetaData controls;
2255 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2256 if (ImGui::CollapsingHeader("Current Slide")) {
2257 SkMetaData::Iter iter(controls);
2258 const char* name;
2259 SkMetaData::Type type;
2260 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002261 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002262 if (type == SkMetaData::kScalar_Type) {
2263 float val[3];
2264 SkASSERT(count == 3);
2265 controls.findScalars(name, &count, val);
2266 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2267 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002268 }
Ben Wagner110c7032019-03-22 17:03:59 -04002269 } else if (type == SkMetaData::kBool_Type) {
2270 bool val;
2271 SkASSERT(count == 1);
2272 controls.findBool(name, &val);
2273 if (ImGui::Checkbox(name, &val)) {
2274 controls.setBool(name, val);
2275 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002276 }
2277 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002278 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002279 }
2280 }
2281 }
2282
Ben Wagner7a3c6742018-04-23 10:01:07 -04002283 if (fShowSlidePicker) {
2284 ImGui::SetNextTreeNodeOpen(true);
2285 }
Brian Osman79086b92017-02-10 13:36:16 -05002286 if (ImGui::CollapsingHeader("Slide")) {
2287 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002288 static ImVector<const char*> filteredSlideNames;
2289 static ImVector<int> filteredSlideIndices;
2290
Brian Osmanfce09c52017-11-14 15:32:20 -05002291 if (fShowSlidePicker) {
2292 ImGui::SetKeyboardFocusHere();
2293 fShowSlidePicker = false;
2294 }
2295
Brian Osman79086b92017-02-10 13:36:16 -05002296 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002297 filteredSlideNames.clear();
2298 filteredSlideIndices.clear();
2299 int filteredIndex = 0;
2300 for (int i = 0; i < fSlides.count(); ++i) {
2301 const char* slideName = fSlides[i]->getName().c_str();
2302 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2303 if (i == fCurrentSlide) {
2304 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002305 }
Brian Osmanf479e422017-11-08 13:11:36 -05002306 filteredSlideNames.push_back(slideName);
2307 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002308 }
Brian Osman79086b92017-02-10 13:36:16 -05002309 }
Brian Osmanf479e422017-11-08 13:11:36 -05002310
Brian Osmanf479e422017-11-08 13:11:36 -05002311 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2312 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002313 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002314 }
2315 }
Brian Osmana109e392017-02-24 09:49:14 -05002316
2317 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002318 ColorMode newMode = fColorMode;
2319 auto cmButton = [&](ColorMode mode, const char* label) {
2320 if (ImGui::RadioButton(label, mode == fColorMode)) {
2321 newMode = mode;
2322 }
2323 };
2324
2325 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002326 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2327 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002328 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002329
2330 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002331 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002332 }
2333
2334 // Pick from common gamuts:
2335 int primariesIdx = 4; // Default: Custom
2336 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2337 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2338 primariesIdx = i;
2339 break;
2340 }
2341 }
2342
Brian Osman03115dc2018-11-26 13:55:19 -05002343 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002344 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002345
Brian Osmana109e392017-02-24 09:49:14 -05002346 if (ImGui::Combo("Primaries", &primariesIdx,
2347 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2348 if (primariesIdx >= 0 && primariesIdx <= 3) {
2349 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2350 }
2351 }
2352
2353 // Allow direct editing of gamut
2354 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2355 }
Brian Osman207d4102019-01-10 09:40:58 -05002356
2357 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002358 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002359 if (ImGui::Checkbox("Pause", &isPaused)) {
2360 fAnimTimer.togglePauseResume();
2361 }
Brian Osman707d2022019-01-10 11:27:34 -05002362
2363 float speed = fAnimTimer.getSpeed();
2364 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2365 fAnimTimer.setSpeed(speed);
2366 }
Brian Osman207d4102019-01-10 09:40:58 -05002367 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002368
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002369 if (ImGui::CollapsingHeader("Shaders")) {
2370 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2371 GrContextOptions::ShaderCacheStrategy::kSkSL;
John Stiles7247b482021-03-08 10:40:35 -05002372
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002373#if defined(SK_VULKAN)
2374 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2375#else
2376 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002377#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002378
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002379 // To re-load shaders from the currently active programs, we flush all
2380 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002381 static bool gLoadPending = false;
2382 if (gLoadPending) {
2383 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
Brian Osmanf0de96f2021-02-26 13:54:11 -05002384 const SkString& description, int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002385 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002386 entry.fKey = key;
2387 SkMD5 hash;
2388 hash.write(key->bytes(), key->size());
2389 SkMD5::Digest digest = hash.finish();
2390 for (int i = 0; i < 16; ++i) {
2391 entry.fKeyString.appendf("%02x", digest.data[i]);
2392 }
Brian Osmanf0de96f2021-02-26 13:54:11 -05002393 entry.fKeyDescription = description;
Brian Osman0b8bb882019-04-12 11:47:19 -04002394
Brian Osman9e4e4c72020-06-10 07:19:34 -04002395 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002396 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002397 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2398 entry.fInputs,
2399 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002400 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002401 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002402 fPersistentCache.foreach(collectShaders);
2403 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002404
2405#if defined(SK_VULKAN)
2406 if (isVulkan && !sksl) {
2407 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2408 for (auto& entry : fCachedShaders) {
2409 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2410 const SkSL::String& spirv(entry.fShader[i]);
2411 std::string disasm;
2412 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2413 &disasm);
2414 entry.fShader[i].assign(disasm);
2415 }
2416 }
2417 }
2418#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002419 }
2420
John Stilesa8f6b6f2021-03-05 16:00:42 -05002421 // Defer actually doing the View/Apply logic so that we can trigger an Apply when we
Brian Osman0b8bb882019-04-12 11:47:19 -04002422 // start or finish hovering on a tree node in the list below:
John Stilesa8f6b6f2021-03-05 16:00:42 -05002423 bool doView = ImGui::Button("View"); ImGui::SameLine();
2424 bool doApply = ImGui::Button("Apply Changes"); ImGui::SameLine();
John Stiles7247b482021-03-08 10:40:35 -05002425 bool doDump = ImGui::Button("Dump SkSL to resources/sksl/");
John Stilesa8f6b6f2021-03-05 16:00:42 -05002426
John Stiles2ee4d7a2021-03-30 10:30:47 -04002427 int newOptLevel = fOptLevel;
John Stiles7247b482021-03-08 10:40:35 -05002428 ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source);
2429 ImGui::SameLine();
2430 ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile);
2431 ImGui::SameLine();
2432 ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize);
2433 ImGui::SameLine();
2434 ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline);
John Stilesa8f6b6f2021-03-05 16:00:42 -05002435
John Stiles7247b482021-03-08 10:40:35 -05002436 // If we are changing the compile mode, we want to reset the cache and redo
2437 // everything.
John Stiles2ee4d7a2021-03-30 10:30:47 -04002438 if (doDump || newOptLevel != fOptLevel) {
John Stiles7247b482021-03-08 10:40:35 -05002439 sksl = doDump || (newOptLevel == kShaderOptLevel_Source);
John Stiles2ee4d7a2021-03-30 10:30:47 -04002440 fOptLevel = (ShaderOptLevel)newOptLevel;
2441 switch (fOptLevel) {
2442 case kShaderOptLevel_Source:
2443 Compiler::EnableOptimizer(OverrideFlag::kDefault);
2444 Compiler::EnableInliner(OverrideFlag::kDefault);
2445 break;
2446 case kShaderOptLevel_Compile:
2447 Compiler::EnableOptimizer(OverrideFlag::kOff);
2448 Compiler::EnableInliner(OverrideFlag::kOff);
2449 break;
2450 case kShaderOptLevel_Optimize:
2451 Compiler::EnableOptimizer(OverrideFlag::kOn);
2452 Compiler::EnableInliner(OverrideFlag::kOff);
2453 break;
2454 case kShaderOptLevel_Inline:
2455 Compiler::EnableOptimizer(OverrideFlag::kOn);
2456 Compiler::EnableInliner(OverrideFlag::kOn);
2457 break;
2458 }
John Stiles7247b482021-03-08 10:40:35 -05002459
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002460 params.fGrContextOptions.fShaderCacheStrategy =
2461 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2462 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2463 paramsChanged = true;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002464 doView = true;
2465
2466 fDeferredActions.push_back([=]() {
2467 // Reset the cache.
2468 fPersistentCache.reset();
2469 // Dump the cache once we have drawn a frame with it.
2470 if (doDump) {
2471 fDeferredActions.push_back([this]() {
2472 this->dumpShadersToResources();
2473 });
2474 }
2475 });
Brian Osmancbc33b82019-04-19 14:16:19 -04002476 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002477
2478 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002479 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002480 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2481 bool hovered = ImGui::IsItemHovered();
2482 if (hovered != entry.fHovered) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002483 // Force an Apply to patch the highlight shader in/out
Brian Osman0b8bb882019-04-12 11:47:19 -04002484 entry.fHovered = hovered;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002485 doApply = true;
Brian Osman0b8bb882019-04-12 11:47:19 -04002486 }
2487 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002488 auto stringBox = [](const char* label, std::string* str) {
2489 // Full width, and not too much space for each shader
2490 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2491 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2492 ImGui::InputTextMultiline(label, str, boxSize);
2493 };
Brian Osmanf0de96f2021-02-26 13:54:11 -05002494 if (ImGui::TreeNode("Key")) {
2495 ImGui::TextWrapped("%s", entry.fKeyDescription.c_str());
2496 ImGui::TreePop();
2497 }
Brian Osmaneb3fb902020-08-18 13:16:59 -04002498 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2499 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002500 ImGui::TreePop();
2501 }
2502 }
2503 ImGui::EndChild();
2504
John Stilesa8f6b6f2021-03-05 16:00:42 -05002505 if (doView) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002506 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002507 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002508 gLoadPending = true;
2509 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002510
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002511 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2512 // but I'm not sure anyone wants to do that.
2513 if (isVulkan && !sksl) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002514 doApply = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002515 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002516 if (doApply) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002517 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002518 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002519 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002520 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002521 if (entry.fHovered) {
2522 // The hovered item (if any) gets a special shader to make it
2523 // identifiable.
2524 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2525 switch (entry.fShaderType) {
2526 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2527 fragShader = build_sksl_highlight_shader();
2528 break;
2529 }
2530 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2531 fragShader = build_glsl_highlight_shader(
2532 *ctx->priv().caps()->shaderCaps());
2533 break;
2534 }
2535 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2536 fragShader = build_metal_highlight_shader(fragShader);
2537 break;
2538 }
2539 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002540 }
2541
Brian Osmana085a412019-04-25 09:44:43 -04002542 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2543 entry.fShader,
2544 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002545 kGrShaderTypeCount);
Brian Osmanf0de96f2021-02-26 13:54:11 -05002546 fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription);
Brian Osman0b8bb882019-04-12 11:47:19 -04002547
2548 entry.fShader[kFragment_GrShaderType] = backup;
2549 }
2550 }
2551 }
Brian Osman79086b92017-02-10 13:36:16 -05002552 }
Brian Salomon99a33902017-03-07 15:16:34 -05002553 if (paramsChanged) {
2554 fDeferredActions.push_back([=]() {
2555 fWindow->setRequestedDisplayParams(params);
2556 fWindow->inval();
2557 this->updateTitle();
2558 });
2559 }
Brian Osman79086b92017-02-10 13:36:16 -05002560 ImGui::End();
2561 }
2562
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002563 if (gShaderErrorHandler.fErrors.count()) {
2564 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002565 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002566 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2567 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002568 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2569 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2570 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2571 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002572 }
2573 ImGui::End();
2574 gShaderErrorHandler.reset();
2575 }
2576
Brian Osmanf6877092017-02-13 09:39:57 -05002577 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002578 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2579 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002580 static int zoomFactor = 8;
2581 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002582 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002583 }
2584 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2585 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002586 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002587 }
Brian Osmanf6877092017-02-13 09:39:57 -05002588
Ben Wagner3627d2e2018-06-26 14:23:20 -04002589 if (!fZoomWindowFixed) {
2590 ImVec2 mousePos = ImGui::GetMousePos();
2591 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2592 }
2593 SkScalar x = fZoomWindowLocation.x();
2594 SkScalar y = fZoomWindowLocation.y();
2595 int xInt = SkScalarRoundToInt(x);
2596 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002597 ImVec2 avail = ImGui::GetContentRegionAvail();
2598
Brian Osmanead517d2017-11-13 15:36:36 -05002599 uint32_t pixel = 0;
2600 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002601 auto dContext = fWindow->directContext();
2602 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002603 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002604 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002605 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002606 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002607 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2608 }
2609
Greg Danielfbc60b72020-11-03 17:27:02 -05002610 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002611 // Translate so the region of the image that's under the mouse cursor is centered
2612 // in the zoom canvas:
2613 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002614 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2615 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002616 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002617
2618 SkPaint outline;
2619 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002620 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002621 });
Brian Osmanf6877092017-02-13 09:39:57 -05002622 }
2623
2624 ImGui::End();
2625 }
Brian Osman79086b92017-02-10 13:36:16 -05002626}
2627
John Stilesa8f6b6f2021-03-05 16:00:42 -05002628void Viewer::dumpShadersToResources() {
2629 // Sort the list of cached shaders so we can maintain some minimal level of consistency.
2630 // It doesn't really matter, but it will keep files from switching places unpredictably.
2631 std::vector<const CachedShader*> shaders;
2632 shaders.reserve(fCachedShaders.size());
2633 for (const CachedShader& shader : fCachedShaders) {
2634 shaders.push_back(&shader);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002635 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002636
2637 std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) {
2638 return std::tie(a->fShader[kFragment_GrShaderType], a->fShader[kVertex_GrShaderType]) <
2639 std::tie(b->fShader[kFragment_GrShaderType], b->fShader[kVertex_GrShaderType]);
2640 });
2641
2642 // Make the resources/sksl/SlideName/ directory.
2643 SkString directory = SkStringPrintf("%ssksl/%s",
2644 GetResourcePath().c_str(),
2645 fSlides[fCurrentSlide]->getName().c_str());
2646 if (!sk_mkdir(directory.c_str())) {
2647 SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str());
2648 return;
2649 }
2650
2651 int index = 0;
2652 for (const auto& entry : shaders) {
2653 SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index);
2654 FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag);
2655 if (vertFile) {
2656 const SkSL::String& vertText = entry->fShader[kVertex_GrShaderType];
2657 SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile));
2658 sk_fclose(vertFile);
2659 } else {
2660 SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str());
2661 }
2662
2663 SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index);
2664 FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag);
2665 if (fragFile) {
2666 const SkSL::String& fragText = entry->fShader[kFragment_GrShaderType];
2667 SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile));
2668 sk_fclose(fragFile);
2669 } else {
2670 SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str());
2671 }
2672
2673 ++index;
2674 }
2675}
2676
2677void Viewer::onIdle() {
2678 SkTArray<std::function<void()>> actionsToRun;
2679 actionsToRun.swap(fDeferredActions);
2680
2681 for (const auto& fn : actionsToRun) {
2682 fn();
2683 }
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002684
Brian Osman56a24812017-12-19 11:15:16 -05002685 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002686 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002687 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002688 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002689
Brian Osman79086b92017-02-10 13:36:16 -05002690 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002691 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2692 // not be visible, though. So we need to redraw if there is at least one visible window, or
2693 // more than one active window. Newly created windows are active but not visible for one frame
2694 // while they determine their layout and sizing.
2695 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2696 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002697 fWindow->inval();
2698 }
jvanverth9f372462016-04-06 06:08:59 -07002699}
liyuqiane5a6cd92016-05-27 08:52:52 -07002700
Florin Malitab632df72018-06-18 21:23:06 -04002701template <typename OptionsFunc>
2702static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2703 OptionsFunc&& optionsFunc) {
2704 writer.beginObject();
2705 {
2706 writer.appendString(kName , name);
2707 writer.appendString(kValue, value);
2708
2709 writer.beginArray(kOptions);
2710 {
2711 optionsFunc(writer);
2712 }
2713 writer.endArray();
2714 }
2715 writer.endObject();
2716}
2717
2718
liyuqiane5a6cd92016-05-27 08:52:52 -07002719void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002720 if (!fWindow) {
2721 return;
2722 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002723 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002724 return; // Surface hasn't been created yet.
2725 }
2726
Florin Malitab632df72018-06-18 21:23:06 -04002727 SkDynamicMemoryWStream memStream;
2728 SkJSONWriter writer(&memStream);
2729 writer.beginArray();
2730
liyuqianb73c24b2016-06-03 08:47:23 -07002731 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002732 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2733 [this](SkJSONWriter& writer) {
2734 for(const auto& slide : fSlides) {
2735 writer.appendString(slide->getName().c_str());
2736 }
2737 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002738
liyuqianb73c24b2016-06-03 08:47:23 -07002739 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002740 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2741 [](SkJSONWriter& writer) {
2742 for (const auto& str : kBackendTypeStrings) {
2743 writer.appendString(str);
2744 }
2745 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002746
csmartdalton578f0642017-02-24 16:04:47 -07002747 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002748 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2749 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2750 [this](SkJSONWriter& writer) {
2751 writer.appendS32(0);
2752
2753 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2754 return;
2755 }
2756
2757 for (int msaa : {4, 8, 16}) {
2758 writer.appendS32(msaa);
2759 }
2760 });
csmartdalton578f0642017-02-24 16:04:47 -07002761
csmartdalton61cd31a2017-02-27 17:00:53 -07002762 // Path renderer state
2763 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002764 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2765 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002766 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002767 if (!ctx) {
2768 writer.appendString("Software");
2769 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002770 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002771 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
Chris Dalton57ab06c2021-04-22 12:57:28 -06002772 if (fWindow->sampleCount() > 1 || FLAGS_dmsaa) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002773 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002774 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002775 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002776 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002777 }
2778 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002779 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2780 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002781 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002782 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002783 }
2784 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002785
liyuqianb73c24b2016-06-03 08:47:23 -07002786 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002787 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2788 [this](SkJSONWriter& writer) {
2789 writer.appendString(kSoftkeyHint);
2790 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2791 writer.appendString(softkey.c_str());
2792 }
2793 });
liyuqianb73c24b2016-06-03 08:47:23 -07002794
Florin Malitab632df72018-06-18 21:23:06 -04002795 writer.endArray();
2796 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002797
Florin Malitab632df72018-06-18 21:23:06 -04002798 auto data = memStream.detachAsData();
2799
2800 // TODO: would be cool to avoid this copy
2801 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2802
2803 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002804}
2805
2806void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002807 // For those who will add more features to handle the state change in this function:
2808 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2809 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2810 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002811 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002812 for (int i = 0; i < fSlides.count(); ++i) {
2813 if (fSlides[i]->getName().equals(stateValue)) {
2814 this->setCurrentSlide(i);
2815 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002816 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002817 }
Florin Malitaab99c342018-01-16 16:23:03 -05002818
2819 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002820 } else if (stateName.equals(kBackendStateName)) {
2821 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2822 if (stateValue.equals(kBackendTypeStrings[i])) {
2823 if (fBackendType != i) {
2824 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002825 for(auto& slide : fSlides) {
2826 slide->gpuTeardown();
2827 }
liyuqian6cb70252016-06-02 12:16:25 -07002828 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002829 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002830 }
2831 break;
2832 }
2833 }
csmartdalton578f0642017-02-24 16:04:47 -07002834 } else if (stateName.equals(kMSAAStateName)) {
2835 DisplayParams params = fWindow->getRequestedDisplayParams();
2836 int sampleCount = atoi(stateValue.c_str());
2837 if (sampleCount != params.fMSAASampleCount) {
2838 params.fMSAASampleCount = sampleCount;
2839 fWindow->setRequestedDisplayParams(params);
2840 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002841 this->updateTitle();
2842 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002843 }
2844 } else if (stateName.equals(kPathRendererStateName)) {
2845 DisplayParams params = fWindow->getRequestedDisplayParams();
2846 for (const auto& pair : gPathRendererNames) {
2847 if (pair.second == stateValue.c_str()) {
2848 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2849 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2850 fWindow->setRequestedDisplayParams(params);
2851 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002852 this->updateTitle();
2853 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002854 }
2855 break;
2856 }
csmartdalton578f0642017-02-24 16:04:47 -07002857 }
liyuqianb73c24b2016-06-03 08:47:23 -07002858 } else if (stateName.equals(kSoftkeyStateName)) {
2859 if (!stateValue.equals(kSoftkeyHint)) {
2860 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002861 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002862 }
liyuqian2edb0f42016-07-06 14:11:32 -07002863 } else if (stateName.equals(kRefreshStateName)) {
2864 // This state is actually NOT in the UI state.
2865 // We use this to allow Android to quickly set bool fRefresh.
2866 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002867 } else {
2868 SkDebugf("Unknown stateName: %s", stateName.c_str());
2869 }
2870}
Brian Osman79086b92017-02-10 13:36:16 -05002871
Hal Canaryb1f411a2019-08-29 10:39:22 -04002872bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002873 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002874}
2875
Hal Canaryb1f411a2019-08-29 10:39:22 -04002876bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002877 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002878 fWindow->inval();
2879 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002880 } else {
2881 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002882 }
Brian Osman79086b92017-02-10 13:36:16 -05002883}