blob: 061c4f7ffd0ecbdc23eb67fd25a975b2088c7946 [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"
40#include "tools/ToolUtils.h"
41#include "tools/flags/CommandLineFlags.h"
42#include "tools/flags/CommonFlags.h"
43#include "tools/trace/EventTracingPriv.h"
44#include "tools/viewer/BisectSlide.h"
45#include "tools/viewer/GMSlide.h"
46#include "tools/viewer/ImageSlide.h"
Brian Salomon06c9e292021-04-29 14:10:23 -040047#include "tools/viewer/MSKPSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "tools/viewer/ParticlesSlide.h"
49#include "tools/viewer/SKPSlide.h"
50#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050051#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052#include "tools/viewer/SlideDir.h"
53#include "tools/viewer/SvgSlide.h"
csmartdalton578f0642017-02-24 16:04:47 -070054
Chris Dalton17dc4182020-03-25 16:18:16 -060055#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040056#include <map>
57
Hal Canary8a001442018-09-19 11:31:27 -040058#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040059#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040060
Brian Osmanc85f1fa2020-06-16 15:11:34 -040061#ifdef SK_VULKAN
62#include "spirv-tools/libspirv.hpp"
63#endif
64
Florin Malita87ccf332018-05-04 12:23:24 -040065#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050066 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040067#endif
Florin Malita45cd2002020-06-09 14:00:54 -040068#if defined(SK_ENABLE_SKRIVE)
69 #include "tools/viewer/SkRiveSlide.h"
70#endif
Florin Malita87ccf332018-05-04 12:23:24 -040071
Brian Osman5e7fbfd2019-05-03 13:13:35 -040072class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
73public:
74 void compileError(const char* shader, const char* errors) override {
75 fShaders.push_back(SkString(shader));
76 fErrors.push_back(SkString(errors));
77 }
78
79 void reset() {
80 fShaders.reset();
81 fErrors.reset();
82 }
83
84 SkTArray<SkString> fShaders;
85 SkTArray<SkString> fErrors;
86};
87
88static CapturingShaderErrorHandler gShaderErrorHandler;
89
Brian Osmanf847f312020-06-18 14:18:27 -040090GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
91
jvanverth34524262016-05-04 13:49:13 -070092using namespace sk_app;
John Stiles2ee4d7a2021-03-30 10:30:47 -040093using SkSL::Compiler;
94using OverrideFlag = SkSL::Compiler::OverrideFlag;
jvanverth34524262016-05-04 13:49:13 -070095
csmartdalton61cd31a2017-02-27 17:00:53 -070096static std::map<GpuPathRenderers, std::string> gPathRendererNames;
97
jvanverth9f372462016-04-06 06:08:59 -070098Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070099 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -0700100}
101
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600102static DEFINE_string(slide, "", "Start on this sample.");
103static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -0500104
Jim Van Verth682a2f42020-05-13 16:54:09 -0400105#ifdef SK_GL
106#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700107#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400108#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700109#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400110#ifdef SK_VULKAN
111#define VK_BACKEND_STR ", \"vk\""
112#else
113#define VK_BACKEND_STR
114#endif
115#ifdef SK_METAL
116#define MTL_BACKEND_STR ", \"mtl\""
117#else
118#define MTL_BACKEND_STR
119#endif
120#ifdef SK_DIRECT3D
121#define D3D_BACKEND_STR ", \"d3d\""
122#else
123#define D3D_BACKEND_STR
124#endif
125#ifdef SK_DAWN
126#define DAWN_BACKEND_STR ", \"dawn\""
127#else
128#define DAWN_BACKEND_STR
129#endif
130#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
131#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
132 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700133
Brian Osman2dd96932016-10-18 15:33:53 -0400134static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700135
Mike Klein5b3f3432019-03-21 11:42:21 -0500136static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
Chris Daltonfd708652021-03-17 21:10:17 -0600137static DEFINE_bool(dmsaa, false, "Use internal MSAA to render to non-MSAA surfaces?");
csmartdalton008b9d82017-02-22 12:00:42 -0700138
Mike Klein84836b72019-03-21 11:31:36 -0500139static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700140
Mike Klein84836b72019-03-21 11:31:36 -0500141static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400142
Mike Kleinc6142d82019-03-25 10:54:59 -0500143static DEFINE_string2(match, m, nullptr,
144 "[~][^]substring[$] [...] of name to run.\n"
145 "Multiple matches may be separated by spaces.\n"
146 "~ causes a matching name to always be skipped\n"
147 "^ requires the start of the name to match\n"
148 "$ requires the end of the name to match\n"
149 "^ and $ requires an exact match\n"
150 "If a name does not match any list entry,\n"
151 "it is skipped unless some list entry starts with ~");
152
Mike Klein19fb3972019-03-21 13:08:08 -0500153#if defined(SK_BUILD_FOR_ANDROID)
Brian Salomon06c9e292021-04-29 14:10:23 -0400154# define PATH_PREFIX "/data/local/tmp/"
Mike Klein19fb3972019-03-21 13:08:08 -0500155#else
Brian Salomon06c9e292021-04-29 14:10:23 -0400156# define PATH_PREFIX ""
Mike Klein19fb3972019-03-21 13:08:08 -0500157#endif
158
Brian Salomon06c9e292021-04-29 14:10:23 -0400159static DEFINE_string(jpgs , PATH_PREFIX "jpgs" , "Directory to read jpgs from.");
160static DEFINE_string(skps , PATH_PREFIX "skps" , "Directory to read skps from.");
161static DEFINE_string(mskps , PATH_PREFIX "mskps" , "Directory to read mskps from.");
162static DEFINE_string(lotties, PATH_PREFIX "lotties", "Directory to read (Bodymovin) jsons from.");
163static DEFINE_string(rives , PATH_PREFIX "rives" , "Directory to read Rive (Flare) files from.");
164#undef PATH_PREFIX
165
Mike Kleinc6142d82019-03-25 10:54:59 -0500166static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
167
168static DEFINE_int_2(threads, j, -1,
169 "Run threadsafe tests on a threadpool with this many extra threads, "
170 "defaulting to one extra thread per core.");
171
Jim Van Verth7b558182019-11-14 16:47:01 -0500172static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
173
Chris Daltonc8877332020-01-06 09:48:30 -0700174static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500175static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
176static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500177static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500178static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500179static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500180
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400181#ifndef SK_GL
182static_assert(false, "viewer requires GL backend for raster.")
183#endif
184
Brian Salomon194db172017-08-17 14:37:06 -0400185const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700186 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400187#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
188 "ANGLE",
189#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400190#ifdef SK_DAWN
191 "Dawn",
192#endif
jvanverth063ece72016-06-17 09:29:14 -0700193#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700194 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700195#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400196#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500197 "Metal",
198#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400199#ifdef SK_DIRECT3D
200 "Direct3D",
201#endif
csmartdalton578f0642017-02-24 16:04:47 -0700202 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700203};
204
bsalomon6c471f72016-07-26 12:56:32 -0700205static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400206#ifdef SK_DAWN
207 if (0 == strcmp(str, "dawn")) {
208 return sk_app::Window::kDawn_BackendType;
209 } else
210#endif
bsalomon6c471f72016-07-26 12:56:32 -0700211#ifdef SK_VULKAN
212 if (0 == strcmp(str, "vk")) {
213 return sk_app::Window::kVulkan_BackendType;
214 } else
215#endif
Brian Salomon194db172017-08-17 14:37:06 -0400216#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
217 if (0 == strcmp(str, "angle")) {
218 return sk_app::Window::kANGLE_BackendType;
219 } else
220#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400221#ifdef SK_METAL
222 if (0 == strcmp(str, "mtl")) {
223 return sk_app::Window::kMetal_BackendType;
224 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500225#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400226#ifdef SK_DIRECT3D
227 if (0 == strcmp(str, "d3d")) {
228 return sk_app::Window::kDirect3D_BackendType;
229 } else
230#endif
231
bsalomon6c471f72016-07-26 12:56:32 -0700232 if (0 == strcmp(str, "gl")) {
233 return sk_app::Window::kNativeGL_BackendType;
234 } else if (0 == strcmp(str, "sw")) {
235 return sk_app::Window::kRaster_BackendType;
236 } else {
237 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
238 return sk_app::Window::kRaster_BackendType;
239 }
240}
241
Brian Osmana109e392017-02-24 09:49:14 -0500242static SkColorSpacePrimaries gSrgbPrimaries = {
243 0.64f, 0.33f,
244 0.30f, 0.60f,
245 0.15f, 0.06f,
246 0.3127f, 0.3290f };
247
248static SkColorSpacePrimaries gAdobePrimaries = {
249 0.64f, 0.33f,
250 0.21f, 0.71f,
251 0.15f, 0.06f,
252 0.3127f, 0.3290f };
253
254static SkColorSpacePrimaries gP3Primaries = {
255 0.680f, 0.320f,
256 0.265f, 0.690f,
257 0.150f, 0.060f,
258 0.3127f, 0.3290f };
259
260static SkColorSpacePrimaries gRec2020Primaries = {
261 0.708f, 0.292f,
262 0.170f, 0.797f,
263 0.131f, 0.046f,
264 0.3127f, 0.3290f };
265
266struct NamedPrimaries {
267 const char* fName;
268 SkColorSpacePrimaries* fPrimaries;
269} gNamedPrimaries[] = {
270 { "sRGB", &gSrgbPrimaries },
271 { "AdobeRGB", &gAdobePrimaries },
272 { "P3", &gP3Primaries },
273 { "Rec. 2020", &gRec2020Primaries },
274};
275
276static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
277 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
278}
279
Brian Osman70d2f432017-11-08 09:54:10 -0500280static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
281 // In raster mode, we still use GL for the window.
282 // This lets us render the GUI faster (and correct).
283 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
284}
285
Jim Van Verth74826c82019-03-01 14:37:30 -0500286class NullSlide : public Slide {
287 SkISize getDimensions() const override {
288 return SkISize::Make(640, 480);
289 }
290
291 void draw(SkCanvas* canvas) override {
292 canvas->clear(0xffff11ff);
293 }
294};
295
John Stiles31964fd2020-05-05 16:05:47 -0400296static const char kName[] = "name";
297static const char kValue[] = "value";
298static const char kOptions[] = "options";
299static const char kSlideStateName[] = "Slide";
300static const char kBackendStateName[] = "Backend";
301static const char kMSAAStateName[] = "MSAA";
302static const char kPathRendererStateName[] = "Path renderer";
303static const char kSoftkeyStateName[] = "Softkey";
304static const char kSoftkeyHint[] = "Please select a softkey";
305static const char kON[] = "ON";
306static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700307
Mike Reed862818b2020-03-21 15:07:13 -0400308extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500309extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500310extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400311
jvanverth34524262016-05-04 13:49:13 -0700312Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500313 : fCurrentSlide(-1)
314 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500315 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400316 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500317 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500318 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500319 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500320 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400321 , fZoomWindowFixed(false)
322 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500323 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400324 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700325 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500326 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500327 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500328 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500329 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500330 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700331 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400332 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400333 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400334 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500335 , fTiled(false)
336 , fDrawTileBoundaries(false)
337 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400338 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700339{
Greg Daniel285db442016-10-14 09:12:53 -0400340 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700341
Chris Dalton37ae4b02019-12-28 14:51:11 -0700342 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600343 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500344 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Dalton17dc4182020-03-25 16:18:16 -0600345 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500346 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700347
jvanverth2bb3b6d2016-04-08 07:24:09 -0700348 SkDebugf("Command line arguments: ");
349 for (int i = 1; i < argc; ++i) {
350 SkDebugf("%s ", argv[i]);
351 }
352 SkDebugf("\n");
353
Mike Klein88544fb2019-03-20 10:50:33 -0500354 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500355#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400356 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500357#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700358
Mike Reed862818b2020-03-21 15:07:13 -0400359 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500360 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500361 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400362
Mike Klein19cc0f62019-03-22 15:30:07 -0500363 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500364
Brian Osmanbc8150f2017-07-24 11:38:01 -0400365 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400366 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400367
bsalomon6c471f72016-07-26 12:56:32 -0700368 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700369 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700370
csmartdalton578f0642017-02-24 16:04:47 -0700371 DisplayParams displayParams;
372 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500373 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700374 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400375 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400376 displayParams.fGrContextOptions.fShaderCacheStrategy =
John Stiles2ee4d7a2021-03-30 10:30:47 -0400377 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400378 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
379 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Dalton94df5722021-04-19 17:40:40 -0600380 if (FLAGS_dmsaa) {
381 displayParams.fSurfaceProps = SkSurfaceProps(
Chris Dalton475c9752021-07-13 11:54:22 -0600382 displayParams.fSurfaceProps.flags() | SkSurfaceProps::kDynamicMSAA_Flag,
Chris Dalton94df5722021-04-19 17:40:40 -0600383 displayParams.fSurfaceProps.pixelGeometry());
384 }
csmartdalton578f0642017-02-24 16:04:47 -0700385 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400386 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500387 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700388
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500389 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500390 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500391
Brian Osman56a24812017-12-19 11:15:16 -0500392 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500393 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500394 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
395 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
396 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
397
jvanverth9f372462016-04-06 06:08:59 -0700398 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700399 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500400 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500401 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500402 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700403
brianosman622c8d52016-05-10 06:50:49 -0700404 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500405 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
406 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
407 fWindow->inval();
408 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500409 // Command to jump directly to the slide picker and give it focus
410 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
411 this->fShowImGuiDebugWindow = true;
412 this->fShowSlidePicker = true;
413 fWindow->inval();
414 });
415 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400416 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500417 this->fShowImGuiDebugWindow = true;
418 this->fShowSlidePicker = true;
419 fWindow->inval();
420 });
Brian Osman79086b92017-02-10 13:36:16 -0500421 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
422 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
423 fWindow->inval();
424 });
Brian Osmanf6877092017-02-13 09:39:57 -0500425 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
426 this->fShowZoomWindow = !this->fShowZoomWindow;
427 fWindow->inval();
428 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400429 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
430 this->fZoomWindowFixed = !this->fZoomWindowFixed;
431 fWindow->inval();
432 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400433 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400434 DisplayParams params = fWindow->getRequestedDisplayParams();
435 params.fDisableVsync = !params.fDisableVsync;
436 fWindow->setRequestedDisplayParams(params);
437 this->updateTitle();
438 fWindow->inval();
439 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400440 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
441 DisplayParams params = fWindow->getRequestedDisplayParams();
442 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
443 fWindow->setRequestedDisplayParams(params);
444 this->updateTitle();
445 fWindow->inval();
446 });
Mike Reedf702ed42019-07-22 17:00:49 -0400447 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
448 fRefresh = !fRefresh;
449 fWindow->inval();
450 });
brianosman622c8d52016-05-10 06:50:49 -0700451 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500452 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700453 fWindow->inval();
454 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400455 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500456 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400457 this->updateTitle();
458 fWindow->inval();
459 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500460 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500461 switch (fColorMode) {
462 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500463 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500464 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500465 case ColorMode::kColorManaged8888:
466 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500467 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500468 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400469 this->setColorMode(ColorMode::kColorManagedF16Norm);
470 break;
471 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500472 this->setColorMode(ColorMode::kLegacy);
473 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500474 }
brianosman622c8d52016-05-10 06:50:49 -0700475 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700476 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
477 DisplayParams params = fWindow->getRequestedDisplayParams();
478 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
479 fWindow->setRequestedDisplayParams(params);
480 fWindow->inval();
481 });
Brian Salomon91216d52021-04-09 11:57:59 -0400482 fCommands.addCommand('w', "Modes", "Toggle reduced shaders", [this]() {
483 DisplayParams params = fWindow->getRequestedDisplayParams();
484 params.fGrContextOptions.fReducedShaderVariations =
485 !params.fGrContextOptions.fReducedShaderVariations;
486 fWindow->setRequestedDisplayParams(params);
487 fWindow->inval();
488 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400489 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500490 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700491 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400492 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500493 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700494 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400495 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700496 this->changeZoomLevel(1.f / 32.f);
497 fWindow->inval();
498 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400499 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700500 this->changeZoomLevel(-1.f / 32.f);
501 fWindow->inval();
502 });
jvanverthaf236b52016-05-20 06:01:06 -0700503 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400504 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
505 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500506 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400507#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
508 if (newBackend == sk_app::Window::kVulkan_BackendType) {
509 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
510 sk_app::Window::kBackendTypeCount);
511 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
512 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500513 }
514#endif
Brian Osman621491e2017-02-28 15:45:01 -0500515 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700516 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500517 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
518 fSaveToSKP = true;
519 fWindow->inval();
520 });
Mike Reed376d8122019-03-14 11:39:02 -0400521 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
522 fShowSlideDimensions = !fShowSlideDimensions;
523 fWindow->inval();
524 });
Ben Wagner37c54032018-04-13 14:30:23 -0400525 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
526 DisplayParams params = fWindow->getRequestedDisplayParams();
527 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400528 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
529 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
530 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400531 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
532 } else {
533 switch (params.fSurfaceProps.pixelGeometry()) {
534 case kUnknown_SkPixelGeometry:
535 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
536 break;
537 case kRGB_H_SkPixelGeometry:
538 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
539 break;
540 case kBGR_H_SkPixelGeometry:
541 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
542 break;
543 case kRGB_V_SkPixelGeometry:
544 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
545 break;
546 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400547 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
548 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400549 break;
550 }
551 }
552 fWindow->setRequestedDisplayParams(params);
553 this->updateTitle();
554 fWindow->inval();
555 });
Ben Wagner9613e452019-01-23 10:34:59 -0500556 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500557 if (!fFontOverrides.fHinting) {
558 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400559 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500560 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500561 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400562 case SkFontHinting::kNone:
563 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500564 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400565 case SkFontHinting::kSlight:
566 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500567 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400568 case SkFontHinting::kNormal:
569 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500570 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400571 case SkFontHinting::kFull:
572 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500573 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500574 break;
575 }
576 }
577 this->updateTitle();
578 fWindow->inval();
579 });
580 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500581 if (!fPaintOverrides.fAntiAlias) {
582 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
583 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500584 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500585 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500586 } else {
587 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500588 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500589 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500590 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400591 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500592 break;
593 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500594 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500595 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400596 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500597 break;
598 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500599 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400600 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500601 break;
602 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500603 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
604 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500605 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
606 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500607 break;
608 }
609 }
610 this->updateTitle();
611 fWindow->inval();
612 });
Ben Wagner37c54032018-04-13 14:30:23 -0400613 fCommands.addCommand('D', "Modes", "DFT", [this]() {
614 DisplayParams params = fWindow->getRequestedDisplayParams();
615 uint32_t flags = params.fSurfaceProps.flags();
616 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
617 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
618 fWindow->setRequestedDisplayParams(params);
619 this->updateTitle();
620 fWindow->inval();
621 });
Ben Wagner9613e452019-01-23 10:34:59 -0500622 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500623 if (!fFontOverrides.fEdging) {
624 fFontOverrides.fEdging = true;
625 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500626 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500627 switch (fFont.getEdging()) {
628 case SkFont::Edging::kAlias:
629 fFont.setEdging(SkFont::Edging::kAntiAlias);
630 break;
631 case SkFont::Edging::kAntiAlias:
632 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
633 break;
634 case SkFont::Edging::kSubpixelAntiAlias:
635 fFont.setEdging(SkFont::Edging::kAlias);
636 fFontOverrides.fEdging = false;
637 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500638 }
639 }
640 this->updateTitle();
641 fWindow->inval();
642 });
Ben Wagner9613e452019-01-23 10:34:59 -0500643 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500644 if (!fFontOverrides.fSubpixel) {
645 fFontOverrides.fSubpixel = true;
646 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500647 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500648 if (!fFont.isSubpixel()) {
649 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500650 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500651 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500652 }
653 }
654 this->updateTitle();
655 fWindow->inval();
656 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400657 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
658 if (!fFontOverrides.fBaselineSnap) {
659 fFontOverrides.fBaselineSnap = true;
660 fFont.setBaselineSnap(false);
661 } else {
662 if (!fFont.isBaselineSnap()) {
663 fFont.setBaselineSnap(true);
664 } else {
665 fFontOverrides.fBaselineSnap = false;
666 }
667 }
668 this->updateTitle();
669 fWindow->inval();
670 });
Brian Osman805a7272018-05-02 15:40:20 -0400671 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
672 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
673 : kPerspective_Real;
674 this->updateTitle();
675 fWindow->inval();
676 });
677 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
678 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
679 : kPerspective_Off;
680 this->updateTitle();
681 fWindow->inval();
682 });
Brian Osman207d4102019-01-10 09:40:58 -0500683 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
684 fAnimTimer.togglePauseResume();
685 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400686 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
687 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500688 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400689 fWindow->inval();
690 });
Mike Reed59295352020-03-12 13:56:34 -0400691 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
692 fDrawViaSerialize = !fDrawViaSerialize;
693 this->updateTitle();
694 fWindow->inval();
695 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500696 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400697 gUseSkVMBlitter = !gUseSkVMBlitter;
698 this->updateTitle();
699 fWindow->inval();
700 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500701 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
702 gSkVMAllowJIT = !gSkVMAllowJIT;
703 this->updateTitle();
704 fWindow->inval();
705 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500706
jvanverth2bb3b6d2016-04-08 07:24:09 -0700707 // set up slides
708 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500709 if (FLAGS_list) {
710 this->listNames();
711 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700712
Brian Osman9bb47cf2018-04-26 15:55:00 -0400713 fPerspectivePoints[0].set(0, 0);
714 fPerspectivePoints[1].set(1, 0);
715 fPerspectivePoints[2].set(0, 1);
716 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700717 fAnimTimer.run();
718
Hal Canaryc465d132017-12-08 10:21:31 -0500719 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500720 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500721 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500722 }
723 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500724
jongdeok.kim804f17e2019-02-26 14:39:23 +0900725 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500726 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700727}
728
jvanverth34524262016-05-04 13:49:13 -0700729void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400730 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
731 static const struct {
732 const char* fExtension;
733 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500734 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400735 const SlideFactory fFactory;
736 } gExternalSlidesInfo[] = {
Brian Salomon06c9e292021-04-29 14:10:23 -0400737 { ".mskp", "mskp-dir", FLAGS_mskps,
738 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
739 return sk_make_sp<MSKPSlide>(name, path);}
740 },
Florin Malita0ffa3222018-04-05 14:34:45 -0400741 { ".skp", "skp-dir", FLAGS_skps,
742 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
743 return sk_make_sp<SKPSlide>(name, path);}
744 },
745 { ".jpg", "jpg-dir", FLAGS_jpgs,
746 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
747 return sk_make_sp<ImageSlide>(name, path);}
748 },
Florin Malita87ccf332018-05-04 12:23:24 -0400749#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400750 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400751 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
752 return sk_make_sp<SkottieSlide>(name, path);}
753 },
Florin Malita87ccf332018-05-04 12:23:24 -0400754#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400755 #if defined(SK_ENABLE_SKRIVE)
756 { ".flr", "skrive-dir", FLAGS_rives,
757 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
758 return sk_make_sp<SkRiveSlide>(name, path);}
759 },
760 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400761#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400762 { ".svg", "svg-dir", FLAGS_svgs,
763 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
764 return sk_make_sp<SvgSlide>(name, path);}
765 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400766#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400767 };
jvanverthc265a922016-04-08 12:51:45 -0700768
Brian Salomon343553a2018-09-05 15:41:23 -0400769 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700770
Mike Klein88544fb2019-03-20 10:50:33 -0500771 const auto addSlide =
772 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
773 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
774 return;
775 }
liyuqian6f163d22016-06-13 12:26:45 -0700776
Mike Klein88544fb2019-03-20 10:50:33 -0500777 if (auto slide = fact(name, path)) {
778 dirSlides.push_back(slide);
779 fSlides.push_back(std::move(slide));
780 }
781 };
Florin Malita76a076b2018-02-15 18:40:48 -0500782
Florin Malita38792ce2018-05-08 10:36:18 -0400783 if (!FLAGS_file.isEmpty()) {
784 // single file mode
785 const SkString file(FLAGS_file[0]);
786
787 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
788 for (const auto& sinfo : gExternalSlidesInfo) {
789 if (file.endsWith(sinfo.fExtension)) {
790 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
791 return;
792 }
793 }
794
795 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
796 } else {
797 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
798 }
799
800 return;
801 }
802
803 // Bisect slide.
804 if (!FLAGS_bisect.isEmpty()) {
805 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500806 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400807 if (FLAGS_bisect.count() >= 2) {
808 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
809 bisect->onChar(*ch);
810 }
811 }
812 fSlides.push_back(std::move(bisect));
813 }
814 }
815
816 // GMs
817 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400818 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400819 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500820 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400821 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400822 fSlides.push_back(std::move(slide));
823 }
Florin Malita38792ce2018-05-08 10:36:18 -0400824 }
825 // reverse gms
826 int numGMs = fSlides.count() - firstGM;
827 for (int i = 0; i < numGMs/2; ++i) {
828 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
829 }
830
831 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400832 for (const SampleFactory factory : SampleRegistry::Range()) {
833 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500834 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400835 fSlides.push_back(slide);
836 }
Florin Malita38792ce2018-05-08 10:36:18 -0400837 }
838
Brian Osman7c979f52019-02-12 13:27:51 -0500839 // Particle demo
840 {
841 // TODO: Convert this to a sample
842 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500843 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500844 fSlides.push_back(std::move(slide));
845 }
846 }
847
Brian Osmand927bd22019-12-18 11:23:12 -0500848 // Runtime shader editor
849 {
850 sk_sp<Slide> slide(new SkSLSlide());
851 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
852 fSlides.push_back(std::move(slide));
853 }
854 }
855
Florin Malita0ffa3222018-04-05 14:34:45 -0400856 for (const auto& info : gExternalSlidesInfo) {
857 for (const auto& flag : info.fFlags) {
858 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
859 // single file
860 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
861 } else {
862 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400863 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400864 SkTArray<SkString> sortedFilenames;
865 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400866 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400867 sortedFilenames.push_back(name);
868 }
869 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400870 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000871 [](const SkString& a, const SkString& b) {
872 return strcmp(a.c_str(), b.c_str()) < 0;
873 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400874 }
875 for (const SkString& filename : sortedFilenames) {
876 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
877 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400878 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400879 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400880 if (!dirSlides.empty()) {
881 fSlides.push_back(
882 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
883 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500884 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400885 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400886 }
887 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500888
889 if (!fSlides.count()) {
890 sk_sp<Slide> slide(new NullSlide());
891 fSlides.push_back(std::move(slide));
892 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700893}
894
895
jvanverth34524262016-05-04 13:49:13 -0700896Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400897 for(auto& slide : fSlides) {
898 slide->gpuTeardown();
899 }
900
jvanverth9f372462016-04-06 06:08:59 -0700901 fWindow->detach();
902 delete fWindow;
903}
904
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500905struct SkPaintTitleUpdater {
906 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
907 void append(const char* s) {
908 if (fCount == 0) {
909 fTitle->append(" {");
910 } else {
911 fTitle->append(", ");
912 }
913 fTitle->append(s);
914 ++fCount;
915 }
916 void done() {
917 if (fCount > 0) {
918 fTitle->append("}");
919 }
920 }
921 SkString* fTitle;
922 int fCount;
923};
924
brianosman05de2162016-05-06 13:28:57 -0700925void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700926 if (!fWindow) {
927 return;
928 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500929 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700930 return; // Surface hasn't been created yet.
931 }
932
jvanverth34524262016-05-04 13:49:13 -0700933 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700934 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700935
Mike Kleine5acd752019-03-22 09:57:16 -0500936 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400937 if (gSkForceAnalyticAA) {
938 title.append(" <FAAA>");
939 } else {
940 title.append(" <AAA>");
941 }
942 }
Mike Reed59295352020-03-12 13:56:34 -0400943 if (fDrawViaSerialize) {
944 title.append(" <serialize>");
945 }
Mike Reed862818b2020-03-21 15:07:13 -0400946 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500947 title.append(" <SkVMBlitter>");
948 }
949 if (!gSkVMAllowJIT) {
950 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400951 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400952
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500953 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500954 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
955 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400956 const char* on, const char* off)
957 {
Ben Wagner9613e452019-01-23 10:34:59 -0500958 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400959 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500960 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400961 };
962
Ben Wagner9613e452019-01-23 10:34:59 -0500963 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
964 const char* on, const char* off)
965 {
966 if (fFontOverrides.*flag) {
967 paintTitle.append((fFont.*isFlag)() ? on : off);
968 }
969 };
970
971 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
972 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
973
974 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
975 "Force Autohint", "No Force Autohint");
976 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400977 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500978 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
979 "Linear Metrics", "Non-Linear Metrics");
980 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
981 "Bitmap Text", "No Bitmap Text");
982 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
983
984 if (fFontOverrides.fEdging) {
985 switch (fFont.getEdging()) {
986 case SkFont::Edging::kAlias:
987 paintTitle.append("Alias Text");
988 break;
989 case SkFont::Edging::kAntiAlias:
990 paintTitle.append("Antialias Text");
991 break;
992 case SkFont::Edging::kSubpixelAntiAlias:
993 paintTitle.append("Subpixel Antialias Text");
994 break;
995 }
996 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400997
Mike Reed3ae47332019-01-04 10:11:46 -0500998 if (fFontOverrides.fHinting) {
999 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -04001000 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001001 paintTitle.append("No Hinting");
1002 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001003 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001004 paintTitle.append("Slight Hinting");
1005 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001006 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001007 paintTitle.append("Normal Hinting");
1008 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001009 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001010 paintTitle.append("Full Hinting");
1011 break;
1012 }
1013 }
1014 paintTitle.done();
1015
Brian Osman92004802017-03-06 11:47:26 -05001016 switch (fColorMode) {
1017 case ColorMode::kLegacy:
1018 title.append(" Legacy 8888");
1019 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001020 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001021 title.append(" ColorManaged 8888");
1022 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001023 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001024 title.append(" ColorManaged F16");
1025 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001026 case ColorMode::kColorManagedF16Norm:
1027 title.append(" ColorManaged F16 Norm");
1028 break;
Brian Osman92004802017-03-06 11:47:26 -05001029 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001030
Brian Osman92004802017-03-06 11:47:26 -05001031 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001032 int curPrimaries = -1;
1033 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1034 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1035 curPrimaries = i;
1036 break;
1037 }
1038 }
Brian Osman03115dc2018-11-26 13:55:19 -05001039 title.appendf(" %s Gamma %f",
1040 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001041 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001042 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001043
Ben Wagner37c54032018-04-13 14:30:23 -04001044 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001045 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001046 switch (params.fSurfaceProps.pixelGeometry()) {
1047 case kUnknown_SkPixelGeometry:
1048 title.append( " Flat");
1049 break;
1050 case kRGB_H_SkPixelGeometry:
1051 title.append( " RGB");
1052 break;
1053 case kBGR_H_SkPixelGeometry:
1054 title.append( " BGR");
1055 break;
1056 case kRGB_V_SkPixelGeometry:
1057 title.append( " RGBV");
1058 break;
1059 case kBGR_V_SkPixelGeometry:
1060 title.append( " BGRV");
1061 break;
1062 }
1063 }
1064
1065 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1066 title.append(" DFT");
1067 }
1068
csmartdalton578f0642017-02-24 16:04:47 -07001069 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001070 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001071 int msaa = fWindow->sampleCount();
1072 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001073 title.appendf(" MSAA: %i", msaa);
1074 }
1075 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001076
1077 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001078 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001079 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1080 }
1081
Brian Osman805a7272018-05-02 15:40:20 -04001082 if (kPerspective_Real == fPerspectiveMode) {
1083 title.append(" Perpsective (Real)");
1084 } else if (kPerspective_Fake == fPerspectiveMode) {
1085 title.append(" Perspective (Fake)");
1086 }
1087
brianosman05de2162016-05-06 13:28:57 -07001088 fWindow->setTitle(title.c_str());
1089}
1090
Florin Malitaab99c342018-01-16 16:23:03 -05001091int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001092
1093 if (!FLAGS_slide.isEmpty()) {
1094 int count = fSlides.count();
1095 for (int i = 0; i < count; i++) {
1096 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001097 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001098 }
1099 }
1100
1101 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1102 this->listNames();
1103 }
1104
Florin Malitaab99c342018-01-16 16:23:03 -05001105 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001106}
1107
Florin Malitaab99c342018-01-16 16:23:03 -05001108void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001109 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001110 for (const auto& slide : fSlides) {
1111 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001112 }
1113}
1114
Florin Malitaab99c342018-01-16 16:23:03 -05001115void Viewer::setCurrentSlide(int slide) {
1116 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001117
Florin Malitaab99c342018-01-16 16:23:03 -05001118 if (slide == fCurrentSlide) {
1119 return;
1120 }
1121
1122 if (fCurrentSlide >= 0) {
1123 fSlides[fCurrentSlide]->unload();
1124 }
1125
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001126 SkScalar scaleFactor = 1.0;
1127 if (fApplyBackingScale) {
1128 scaleFactor = fWindow->scaleFactor();
1129 }
1130 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1131 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001132 fCurrentSlide = slide;
1133 this->setupCurrentSlide();
1134}
1135
1136void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001137 if (fCurrentSlide >= 0) {
1138 // prepare dimensions for image slides
1139 fGesture.resetTouchState();
1140 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001141
Jim Van Verth0848fb02018-01-22 13:39:30 -05001142 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1143 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1144 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001145
Jim Van Verth0848fb02018-01-22 13:39:30 -05001146 // Start with a matrix that scales the slide to the available screen space
1147 if (fWindow->scaleContentToFit()) {
1148 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001149 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1150 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001151 }
liyuqiane46e4f02016-05-20 07:32:19 -07001152 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001153
1154 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001155 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001156
1157 this->updateTitle();
1158 this->updateUIState();
1159
1160 fStatsLayer.resetMeasurements();
1161
1162 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001163 }
jvanverthc265a922016-04-08 12:51:45 -07001164}
1165
Brian Osmanaba642c2020-02-06 12:52:25 -05001166#define MAX_ZOOM_LEVEL 8.0f
1167#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001168
jvanverth34524262016-05-04 13:49:13 -07001169void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001170 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001171 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001172 this->preTouchMatrixChanged();
1173}
Yuqian Li755778c2018-03-28 16:23:31 -04001174
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001175void Viewer::preTouchMatrixChanged() {
1176 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001177 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1178 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1179 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1180 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1181}
1182
Brian Osman805a7272018-05-02 15:40:20 -04001183SkMatrix Viewer::computePerspectiveMatrix() {
1184 SkScalar w = fWindow->width(), h = fWindow->height();
1185 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1186 SkPoint perspPts[4] = {
1187 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1188 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1189 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1190 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1191 };
1192 SkMatrix m;
1193 m.setPolyToPoly(orthoPts, perspPts, 4);
1194 return m;
1195}
1196
Yuqian Li755778c2018-03-28 16:23:31 -04001197SkMatrix Viewer::computePreTouchMatrix() {
1198 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001199
1200 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001201 if (fApplyBackingScale) {
1202 zoomScale *= fWindow->scaleFactor();
1203 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001204 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001205 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001206
1207 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1208 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001209
Brian Osman805a7272018-05-02 15:40:20 -04001210 if (kPerspective_Real == fPerspectiveMode) {
1211 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001212 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001213 }
1214
Yuqian Li755778c2018-03-28 16:23:31 -04001215 return m;
jvanverthc265a922016-04-08 12:51:45 -07001216}
1217
liyuqiand3cdbca2016-05-17 12:44:20 -07001218SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001219 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001220 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001221 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001222 return m;
jvanverthc265a922016-04-08 12:51:45 -07001223}
1224
Brian Osman621491e2017-02-28 15:45:01 -05001225void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001226 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001227 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001228 fBackendType = backendType;
1229
Robert Phillipse9229532020-06-26 10:10:49 -04001230 // The active context is going away in 'detach'
1231 for(auto& slide : fSlides) {
1232 slide->gpuTeardown();
1233 }
1234
Brian Osman621491e2017-02-28 15:45:01 -05001235 fWindow->detach();
1236
Brian Osman70d2f432017-11-08 09:54:10 -05001237#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001238 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1239 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001240 DisplayParams params = fWindow->getRequestedDisplayParams();
1241 delete fWindow;
1242 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001243
Brian Osman70d2f432017-11-08 09:54:10 -05001244 // re-register callbacks
1245 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001246 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001247 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001248 fWindow->pushLayer(&fImGuiLayer);
1249
Brian Osman70d2f432017-11-08 09:54:10 -05001250 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1251 // will still include our correct sample count. But the re-created fWindow will lose that
1252 // information. On Windows, we need to re-create the window when changing sample count,
1253 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1254 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1255 // as if we had never changed windows at all).
1256 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001257#endif
1258
Brian Osman70d2f432017-11-08 09:54:10 -05001259 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001260}
1261
Brian Osman92004802017-03-06 11:47:26 -05001262void Viewer::setColorMode(ColorMode colorMode) {
1263 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001264 this->updateTitle();
1265 fWindow->inval();
1266}
1267
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001268class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1269public:
Mike Reed3ae47332019-01-04 10:11:46 -05001270 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1271 SkFont* font, Viewer::SkFontFields* ffields)
1272 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001273 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001274 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1275 sk_sp<SkTextBlob>* cache) {
1276 bool blobWillChange = false;
1277 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001278 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1279 bool shouldDraw = this->filterFont(&filteredFont);
1280 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001281 blobWillChange = true;
1282 break;
1283 }
1284 }
1285 if (!blobWillChange) {
1286 return blob;
1287 }
1288
1289 SkTextBlobBuilder builder;
1290 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001291 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1292 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001293 if (!shouldDraw) {
1294 continue;
1295 }
1296
Mike Reed3ae47332019-01-04 10:11:46 -05001297 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001298
Ben Wagner41e40472018-09-24 13:01:54 -04001299 const SkTextBlobBuilder::RunBuffer& runBuffer
1300 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001301 ? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(),
1302 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001303 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001304 ? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(),
1305 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001306 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001307 ? builder.allocRunTextPos(font, it.glyphCount(), it.textSize())
Ben Wagnere5736262021-02-08 16:52:08 -05001308 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001309 ? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001310 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1311 uint32_t glyphCount = it.glyphCount();
1312 if (it.glyphs()) {
1313 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1314 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1315 }
1316 if (it.pos()) {
1317 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001318 unsigned posPerGlyph = it.scalarsPerGlyph();
1319 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001320 }
1321 if (it.text()) {
1322 size_t textSize = sizeof(decltype(*it.text()));
1323 uint32_t textCount = it.textSize();
1324 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1325 }
1326 if (it.clusters()) {
1327 size_t clusterSize = sizeof(decltype(*it.clusters()));
1328 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1329 }
1330 }
1331 *cache = builder.make();
1332 return cache->get();
1333 }
1334 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1335 const SkPaint& paint) override {
1336 sk_sp<SkTextBlob> cache;
1337 this->SkPaintFilterCanvas::onDrawTextBlob(
1338 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1339 }
Mike Reed3ae47332019-01-04 10:11:46 -05001340 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001341 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001342 font->writable()->setSize(fFont->getSize());
1343 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001344 if (fFontOverrides->fScaleX) {
1345 font->writable()->setScaleX(fFont->getScaleX());
1346 }
1347 if (fFontOverrides->fSkewX) {
1348 font->writable()->setSkewX(fFont->getSkewX());
1349 }
Mike Reed3ae47332019-01-04 10:11:46 -05001350 if (fFontOverrides->fHinting) {
1351 font->writable()->setHinting(fFont->getHinting());
1352 }
Ben Wagner9613e452019-01-23 10:34:59 -05001353 if (fFontOverrides->fEdging) {
1354 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001355 }
Ben Wagner9613e452019-01-23 10:34:59 -05001356 if (fFontOverrides->fEmbolden) {
1357 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001358 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001359 if (fFontOverrides->fBaselineSnap) {
1360 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1361 }
Ben Wagner9613e452019-01-23 10:34:59 -05001362 if (fFontOverrides->fLinearMetrics) {
1363 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001364 }
Ben Wagner9613e452019-01-23 10:34:59 -05001365 if (fFontOverrides->fSubpixel) {
1366 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001367 }
Ben Wagner9613e452019-01-23 10:34:59 -05001368 if (fFontOverrides->fEmbeddedBitmaps) {
1369 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001370 }
Ben Wagner9613e452019-01-23 10:34:59 -05001371 if (fFontOverrides->fForceAutoHinting) {
1372 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001373 }
Ben Wagner9613e452019-01-23 10:34:59 -05001374
Mike Reed3ae47332019-01-04 10:11:46 -05001375 return true;
1376 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001377 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001378 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001379 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001380 }
Ben Wagner9613e452019-01-23 10:34:59 -05001381 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001382 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001383 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001384 if (fPaintOverrides->fStyle) {
1385 paint.setStyle(fPaint->getStyle());
1386 }
1387 if (fPaintOverrides->fWidth) {
1388 paint.setStrokeWidth(fPaint->getStrokeWidth());
1389 }
1390 if (fPaintOverrides->fMiterLimit) {
1391 paint.setStrokeMiter(fPaint->getStrokeMiter());
1392 }
1393 if (fPaintOverrides->fCapType) {
1394 paint.setStrokeCap(fPaint->getStrokeCap());
1395 }
1396 if (fPaintOverrides->fJoinType) {
1397 paint.setStrokeJoin(fPaint->getStrokeJoin());
1398 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001399 return true;
1400 }
1401 SkPaint* fPaint;
1402 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001403 SkFont* fFont;
1404 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001405};
1406
Robert Phillips9882dae2019-03-04 11:00:10 -05001407void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001408 if (fCurrentSlide < 0) {
1409 return;
1410 }
1411
Robert Phillips9882dae2019-03-04 11:00:10 -05001412 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001413
Brian Osmanf750fbc2017-02-08 10:47:28 -05001414 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001415 SkSurface* slideSurface = surface;
1416 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001417 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001418
Brian Osmane0d4fba2017-03-15 10:24:55 -04001419 // 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 -05001420 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001421 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001422 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001423 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001424 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001425 }
1426
Brian Osman3ac99cf2017-12-01 11:23:53 -05001427 if (fSaveToSKP) {
1428 SkPictureRecorder recorder;
1429 SkCanvas* recorderCanvas = recorder.beginRecording(
1430 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001431 fSlides[fCurrentSlide]->draw(recorderCanvas);
1432 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1433 SkFILEWStream stream("sample_app.skp");
1434 picture->serialize(&stream);
1435 fSaveToSKP = false;
1436 }
1437
Brian Osmane9ed0f02018-11-26 14:50:05 -05001438 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001439 SkColorType colorType;
1440 switch (fColorMode) {
1441 case ColorMode::kLegacy:
1442 case ColorMode::kColorManaged8888:
1443 colorType = kN32_SkColorType;
1444 break;
1445 case ColorMode::kColorManagedF16:
1446 colorType = kRGBA_F16_SkColorType;
1447 break;
1448 case ColorMode::kColorManagedF16Norm:
1449 colorType = kRGBA_F16Norm_SkColorType;
1450 break;
1451 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001452
1453 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001454 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001455 slideCanvas->getProps(&props);
1456
Brian Osmane9ed0f02018-11-26 14:50:05 -05001457 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1458 return Window::kRaster_BackendType == this->fBackendType
1459 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001460 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001461 };
1462
Brian Osman03115dc2018-11-26 13:55:19 -05001463 // We need to render offscreen if we're...
1464 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1465 // ... in any raster mode, because the window surface is actually GL
1466 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001467 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001468 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001469 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001470 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001471 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001472 colorSpace != nullptr ||
1473 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001474
Brian Osmane9ed0f02018-11-26 14:50:05 -05001475 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001476 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001477 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001478 }
1479
Mike Reed59295352020-03-12 13:56:34 -04001480 SkPictureRecorder recorder;
1481 SkCanvas* recorderRestoreCanvas = nullptr;
1482 if (fDrawViaSerialize) {
1483 recorderRestoreCanvas = slideCanvas;
1484 slideCanvas = recorder.beginRecording(
1485 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1486 }
1487
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001488 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001489 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001490 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001491 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001492 if (fTiled) {
1493 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1494 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001495 for (int y = 0; y < fWindow->height(); y += tileH) {
1496 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001497 SkAutoCanvasRestore acr(slideCanvas, true);
1498 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1499 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001500 }
1501 }
1502
1503 // Draw borders between tiles
1504 if (fDrawTileBoundaries) {
1505 SkPaint border;
1506 border.setColor(0x60FF00FF);
1507 border.setStyle(SkPaint::kStroke_Style);
1508 for (int y = 0; y < fWindow->height(); y += tileH) {
1509 for (int x = 0; x < fWindow->width(); x += tileW) {
1510 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1511 }
1512 }
1513 }
1514 } else {
1515 slideCanvas->concat(this->computeMatrix());
1516 if (kPerspective_Real == fPerspectiveMode) {
1517 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1518 }
Mike Reed3ae47332019-01-04 10:11:46 -05001519 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001520 fSlides[fCurrentSlide]->draw(&filterCanvas);
1521 }
Brian Osman56a24812017-12-19 11:15:16 -05001522 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001523 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001524
Mike Reed59295352020-03-12 13:56:34 -04001525 if (recorderRestoreCanvas) {
1526 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1527 auto data = picture->serialize();
1528 slideCanvas = recorderRestoreCanvas;
1529 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1530 }
1531
Brian Osman1df161a2017-02-09 12:10:20 -05001532 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001533 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001534 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001535 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001536
1537 // If we rendered offscreen, snap an image and push the results to the window's canvas
1538 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001539 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001540
Robert Phillips9882dae2019-03-04 11:00:10 -05001541 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001542 SkPaint paint;
1543 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001544 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001545 int prePerspectiveCount = canvas->save();
1546 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001547 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001548 canvas->clear(SK_ColorWHITE);
1549 canvas->concat(this->computePerspectiveMatrix());
1550 }
Mike Reedb339d052021-01-28 11:20:41 -05001551 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001552 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001553 }
Mike Reed376d8122019-03-14 11:39:02 -04001554
1555 if (fShowSlideDimensions) {
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001556 SkCanvas* canvas = surface->getCanvas();
1557 SkAutoCanvasRestore acr(canvas, true);
1558 canvas->concat(this->computeMatrix());
Mike Reed376d8122019-03-14 11:39:02 -04001559 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1560 SkPaint paint;
1561 paint.setColor(0x40FFFF00);
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001562 canvas->drawRect(r, paint);
Mike Reed376d8122019-03-14 11:39:02 -04001563 }
liyuqian6f163d22016-06-13 12:26:45 -07001564}
1565
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001566void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001567 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001568 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001569}
Jim Van Verth6f449692017-02-14 15:16:46 -05001570
Robert Phillips9882dae2019-03-04 11:00:10 -05001571void Viewer::onPaint(SkSurface* surface) {
1572 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001573
Robert Phillips9882dae2019-03-04 11:00:10 -05001574 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001575
Brian Osmand67e5182017-12-08 16:46:09 -05001576 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001577
Greg Daniel427d8eb2020-09-28 15:04:18 -04001578 fLastImage.reset();
1579
Robert Phillipsed653392020-07-10 13:55:21 -04001580 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001581 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001582 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001583 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001584}
1585
Ben Wagnera1915972018-08-09 15:06:19 -04001586void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001587 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001588 SkScalar scaleFactor = 1.0;
1589 if (fApplyBackingScale) {
1590 scaleFactor = fWindow->scaleFactor();
1591 }
1592 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001593 }
Ben Wagnera1915972018-08-09 15:06:19 -04001594}
1595
Florin Malitacefc1b92018-02-19 21:43:47 -05001596SkPoint Viewer::mapEvent(float x, float y) {
1597 const auto m = this->computeMatrix();
1598 SkMatrix inv;
1599
1600 SkAssertResult(m.invert(&inv));
1601
1602 return inv.mapXY(x, y);
1603}
1604
Hal Canaryb1f411a2019-08-29 10:39:22 -04001605bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001606 if (GestureDevice::kMouse == fGestureDevice) {
1607 return false;
1608 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001609
1610 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001611 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001612 fWindow->inval();
1613 return true;
1614 }
1615
liyuqiand3cdbca2016-05-17 12:44:20 -07001616 void* castedOwner = reinterpret_cast<void*>(owner);
1617 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001618 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001619 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001620#if defined(SK_BUILD_FOR_IOS)
1621 // TODO: move IOS swipe detection higher up into the platform code
1622 SkPoint dir;
1623 if (fGesture.isFling(&dir)) {
1624 // swiping left or right
1625 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1626 if (dir.fX < 0) {
1627 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1628 fCurrentSlide + 1 : 0);
1629 } else {
1630 this->setCurrentSlide(fCurrentSlide > 0 ?
1631 fCurrentSlide - 1 : fSlides.count() - 1);
1632 }
1633 }
1634 fGesture.reset();
1635 }
1636#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001637 break;
1638 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001639 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001640 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001641 break;
1642 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001643 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001644 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001645 break;
1646 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001647 default: {
1648 // kLeft and kRight are only for swipes
1649 SkASSERT(false);
1650 break;
1651 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001652 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001653 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001654 fWindow->inval();
1655 return true;
1656}
1657
Hal Canaryb1f411a2019-08-29 10:39:22 -04001658bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001659 if (GestureDevice::kTouch == fGestureDevice) {
1660 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001661 }
Brian Osman16c81a12017-12-20 11:58:34 -05001662
Florin Malitacefc1b92018-02-19 21:43:47 -05001663 const auto slidePt = this->mapEvent(x, y);
1664 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1665 fWindow->inval();
1666 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001667 }
1668
1669 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001670 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001671 fGesture.touchEnd(nullptr);
1672 break;
1673 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001674 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001675 fGesture.touchBegin(nullptr, x, y);
1676 break;
1677 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001678 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001679 fGesture.touchMoved(nullptr, x, y);
1680 break;
1681 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001682 default: {
1683 SkASSERT(false); // shouldn't see kRight or kLeft here
1684 break;
1685 }
Brian Osman16c81a12017-12-20 11:58:34 -05001686 }
1687 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1688
Hal Canaryb1f411a2019-08-29 10:39:22 -04001689 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001690 fWindow->inval();
1691 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001692 return true;
1693}
1694
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001695bool Viewer::onFling(skui::InputState state) {
1696 if (skui::InputState::kRight == state) {
1697 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1698 return true;
1699 } else if (skui::InputState::kLeft == state) {
1700 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1701 return true;
1702 }
1703 return false;
1704}
1705
1706bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1707 switch (state) {
1708 case skui::InputState::kDown:
1709 fGesture.startZoom();
1710 return true;
1711 break;
1712 case skui::InputState::kMove:
1713 fGesture.updateZoom(scale, x, y, x, y);
1714 return true;
1715 break;
1716 case skui::InputState::kUp:
1717 fGesture.endZoom();
1718 return true;
1719 break;
1720 default:
1721 SkASSERT(false);
1722 break;
1723 }
1724
1725 return false;
1726}
1727
Brian Osmana109e392017-02-24 09:49:14 -05001728static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001729 // The gamut image covers a (0.8 x 0.9) shaped region
1730 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001731
1732 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1733 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1734 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001735 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1736 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1737 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001738
Brian Osman535c5e32019-02-09 16:32:58 -05001739 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1740 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1741 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1742 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1743 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001744}
1745
Ben Wagner3627d2e2018-06-26 14:23:20 -04001746static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001747 ImGui::DragCanvas dc(pt);
1748 dc.fillColor(IM_COL32(0, 0, 0, 128));
1749 dc.dragPoint(pt);
1750 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001751}
1752
Brian Osman9bb47cf2018-04-26 15:55:00 -04001753static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001754 ImGui::DragCanvas dc(pts);
1755 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001756
Brian Osman535c5e32019-02-09 16:32:58 -05001757 for (int i = 0; i < 4; ++i) {
1758 dc.dragPoint(pts + i);
1759 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001760
Brian Osman535c5e32019-02-09 16:32:58 -05001761 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1762 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1763 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1764 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001765
Brian Osman535c5e32019-02-09 16:32:58 -05001766 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001767}
1768
John Stiles38b7d2f2020-06-24 12:13:31 -04001769static SkSL::String build_sksl_highlight_shader() {
1770 return SkSL::String("out half4 sk_FragColor;\n"
1771 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1772}
1773
1774static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1775 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1776 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1777 size_t pos = inShader.rfind("return *_out;\n");
1778 if (pos == std::string::npos) {
1779 return inShader;
1780 }
1781
1782 SkSL::String replacementShader = inShader;
1783 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1784 return replacementShader;
1785}
1786
1787static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1788 const char* versionDecl = shaderCaps.versionDeclString();
1789 SkSL::String highlight = versionDecl ? versionDecl : "";
1790 if (shaderCaps.usesPrecisionModifiers()) {
1791 highlight.append("precision mediump float;\n");
1792 }
1793 highlight.appendf("out vec4 sk_FragColor;\n"
1794 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1795 return highlight;
1796}
1797
Brian Osmand67e5182017-12-08 16:46:09 -05001798void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001799 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1800 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001801 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001802 }
1803
1804 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001805 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1806 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001807 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001808 DisplayParams params = fWindow->getRequestedDisplayParams();
1809 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001810 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001811
Brian Osmana109e392017-02-24 09:49:14 -05001812 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1813 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001814 if (ImGui::CollapsingHeader("Backend")) {
1815 int newBackend = static_cast<int>(fBackendType);
1816 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1817 ImGui::SameLine();
1818 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001819#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1820 ImGui::SameLine();
1821 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1822#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001823#if defined(SK_DAWN)
1824 ImGui::SameLine();
1825 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1826#endif
John Stilesf7da9232020-11-19 19:58:14 -05001827#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001828 ImGui::SameLine();
1829 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1830#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001831#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001832 ImGui::SameLine();
1833 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1834#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001835#if defined(SK_DIRECT3D)
1836 ImGui::SameLine();
1837 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1838#endif
Brian Osman621491e2017-02-28 15:45:01 -05001839 if (newBackend != fBackendType) {
1840 fDeferredActions.push_back([=]() {
1841 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1842 });
1843 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001844
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001845 bool* wire = &params.fGrContextOptions.fWireframeMode;
1846 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1847 paramsChanged = true;
1848 }
Brian Salomon99a33902017-03-07 15:16:34 -05001849
Brian Salomon91216d52021-04-09 11:57:59 -04001850 bool* reducedShaders = &params.fGrContextOptions.fReducedShaderVariations;
1851 if (ctx && ImGui::Checkbox("Reduced shaders", reducedShaders)) {
1852 paramsChanged = true;
1853 }
1854
Brian Osman28b12522017-03-08 17:10:24 -05001855 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001856 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001857 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001858 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1859 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1860 1;
1861
1862 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1863 if (maxMSAA >= 4) {
1864 ImGui::Text("MSAA: ");
1865
1866 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1867 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1868 // don't show it in the list.
1869 if (curMSAA == 2) {
1870 continue;
1871 }
1872 ImGui::SameLine();
1873 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1874 &sampleCount, curMSAA);
1875 }
1876 }
Brian Osman28b12522017-03-08 17:10:24 -05001877
1878 if (sampleCount != params.fMSAASampleCount) {
1879 params.fMSAASampleCount = sampleCount;
1880 paramsChanged = true;
1881 }
1882 }
1883
Ben Wagner37c54032018-04-13 14:30:23 -04001884 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001885 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001886 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1887 }
1888 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1889 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1890 {
1891 uint32_t flags = params.fSurfaceProps.flags();
1892 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001893 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1894 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1895 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001896 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001897 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001898 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1899 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1900 }
1901 paramsChanged = true;
1902 }
1903
1904 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1905 if (ImGui::Checkbox("DFT", &useDFT)) {
1906 uint32_t flags = params.fSurfaceProps.flags();
1907 if (useDFT) {
1908 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1909 } else {
1910 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1911 }
1912 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1913 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1914 paramsChanged = true;
1915 }
1916
Brian Osman8a9de3d2017-03-01 14:59:05 -05001917 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001918 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001919 auto prButton = [&](GpuPathRenderers x) {
1920 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001921 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1922 params.fGrContextOptions.fGpuPathRenderers = x;
1923 paramsChanged = true;
1924 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001925 }
1926 };
1927
1928 if (!ctx) {
1929 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001930 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001931 const auto* caps = ctx->priv().caps();
1932 prButton(GpuPathRenderers::kDefault);
Chris Dalton57ab06c2021-04-22 12:57:28 -06001933 if (fWindow->sampleCount() > 1 || FLAGS_dmsaa) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001934 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001935 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001936 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001937 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001938 if (1 == fWindow->sampleCount()) {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001939 prButton(GpuPathRenderers::kSmall);
1940 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001941 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001942 prButton(GpuPathRenderers::kNone);
1943 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001944 ImGui::TreePop();
1945 }
Brian Osman621491e2017-02-28 15:45:01 -05001946 }
1947
Ben Wagner964571d2019-03-08 12:35:06 -05001948 if (ImGui::CollapsingHeader("Tiling")) {
1949 ImGui::Checkbox("Enable", &fTiled);
1950 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1951 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1952 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1953 }
1954
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001955 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001956 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1957 this->preTouchMatrixChanged();
1958 this->onResize(fWindow->width(), fWindow->height());
1959 paramsChanged = true;
1960 }
1961
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001962 float zoom = fZoomLevel;
1963 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1964 fZoomLevel = zoom;
1965 this->preTouchMatrixChanged();
1966 paramsChanged = true;
1967 }
1968 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001969 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001970 fRotation = deg;
1971 this->preTouchMatrixChanged();
1972 paramsChanged = true;
1973 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001974 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1975 if (ImGui_DragLocation(&fOffset)) {
1976 this->preTouchMatrixChanged();
1977 paramsChanged = true;
1978 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001979 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1980 this->preTouchMatrixChanged();
1981 paramsChanged = true;
1982 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001983 }
Brian Osman805a7272018-05-02 15:40:20 -04001984 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1985 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1986 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001987 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001988 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001989 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001990 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001991 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001992 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001993 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001994 }
1995
Ben Wagnera580fb32018-04-17 11:16:32 -04001996 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001997 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001998 if (fPaintOverrides.fAntiAlias) {
1999 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002000 }
2001 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05002002 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04002003 {
2004 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
2005 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04002006 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002007 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
2008 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002009 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002010 fPaintOverrides.fAntiAlias = true;
2011 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04002012 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05002013 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04002014 case SkPaintFields::AntiAliasState::Alias:
2015 break;
2016 case SkPaintFields::AntiAliasState::Normal:
2017 break;
2018 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
2019 gSkUseAnalyticAA = true;
2020 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002021 break;
2022 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2023 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002024 break;
2025 }
2026 }
2027 paramsChanged = true;
2028 }
2029
Ben Wagner99a78dc2018-05-09 18:23:51 -04002030 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002031 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002032 bool (SkPaint::* isFlag)() const,
2033 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002034 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002035 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002036 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002037 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002038 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002039 if (ImGui::Combo(label, &itemIndex, items)) {
2040 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002041 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002042 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002043 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002044 (fPaint.*setFlag)(itemIndex == 2);
2045 }
2046 paramsChanged = true;
2047 }
2048 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002049
Ben Wagner99a78dc2018-05-09 18:23:51 -04002050 paintFlag("Dither",
2051 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002052 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002053 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002054
2055 int styleIdx = 0;
2056 if (fPaintOverrides.fStyle) {
2057 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2058 }
2059 if (ImGui::Combo("Style", &styleIdx,
2060 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2061 {
2062 if (styleIdx == 0) {
2063 fPaintOverrides.fStyle = false;
2064 fPaint.setStyle(SkPaint::kFill_Style);
2065 } else {
2066 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2067 fPaintOverrides.fStyle = true;
2068 }
2069 paramsChanged = true;
2070 }
2071
2072 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2073 if (fPaintOverrides.fWidth) {
2074 float width = fPaint.getStrokeWidth();
2075 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2076 fPaint.setStrokeWidth(width);
2077 paramsChanged = true;
2078 }
2079 }
2080
2081 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2082 if (fPaintOverrides.fMiterLimit) {
2083 float miterLimit = fPaint.getStrokeMiter();
2084 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2085 fPaint.setStrokeMiter(miterLimit);
2086 paramsChanged = true;
2087 }
2088 }
2089
2090 int capIdx = 0;
2091 if (fPaintOverrides.fCapType) {
2092 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2093 }
2094 if (ImGui::Combo("Cap Type", &capIdx,
2095 "Default\0Butt\0Round\0Square\0\0"))
2096 {
2097 if (capIdx == 0) {
2098 fPaintOverrides.fCapType = false;
2099 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2100 } else {
2101 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2102 fPaintOverrides.fCapType = true;
2103 }
2104 paramsChanged = true;
2105 }
2106
2107 int joinIdx = 0;
2108 if (fPaintOverrides.fJoinType) {
2109 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2110 }
2111 if (ImGui::Combo("Join Type", &joinIdx,
2112 "Default\0Miter\0Round\0Bevel\0\0"))
2113 {
2114 if (joinIdx == 0) {
2115 fPaintOverrides.fJoinType = false;
2116 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2117 } else {
2118 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2119 fPaintOverrides.fJoinType = true;
2120 }
2121 paramsChanged = true;
2122 }
Ben Wagner9613e452019-01-23 10:34:59 -05002123 }
Hal Canary02738a82019-01-21 18:51:32 +00002124
Ben Wagner9613e452019-01-23 10:34:59 -05002125 if (ImGui::CollapsingHeader("Font")) {
2126 int hintingIdx = 0;
2127 if (fFontOverrides.fHinting) {
2128 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2129 }
2130 if (ImGui::Combo("Hinting", &hintingIdx,
2131 "Default\0None\0Slight\0Normal\0Full\0\0"))
2132 {
2133 if (hintingIdx == 0) {
2134 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002135 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002136 } else {
2137 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2138 fFontOverrides.fHinting = true;
2139 }
2140 paramsChanged = true;
2141 }
Hal Canary02738a82019-01-21 18:51:32 +00002142
Ben Wagner9613e452019-01-23 10:34:59 -05002143 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2144 bool SkFontFields::* flag,
2145 bool (SkFont::* isFlag)() const,
2146 void (SkFont::* setFlag)(bool) )
2147 {
2148 int itemIndex = 0;
2149 if (fFontOverrides.*flag) {
2150 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2151 }
2152 if (ImGui::Combo(label, &itemIndex, items)) {
2153 if (itemIndex == 0) {
2154 fFontOverrides.*flag = false;
2155 } else {
2156 fFontOverrides.*flag = true;
2157 (fFont.*setFlag)(itemIndex == 2);
2158 }
2159 paramsChanged = true;
2160 }
2161 };
Hal Canary02738a82019-01-21 18:51:32 +00002162
Ben Wagner9613e452019-01-23 10:34:59 -05002163 fontFlag("Fake Bold Glyphs",
2164 "Default\0No Fake Bold\0Fake Bold\0\0",
2165 &SkFontFields::fEmbolden,
2166 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002167
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002168 fontFlag("Baseline Snapping",
2169 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2170 &SkFontFields::fBaselineSnap,
2171 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2172
Ben Wagner9613e452019-01-23 10:34:59 -05002173 fontFlag("Linear Text",
2174 "Default\0No Linear Text\0Linear Text\0\0",
2175 &SkFontFields::fLinearMetrics,
2176 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002177
Ben Wagner9613e452019-01-23 10:34:59 -05002178 fontFlag("Subpixel Position Glyphs",
2179 "Default\0Pixel Text\0Subpixel Text\0\0",
2180 &SkFontFields::fSubpixel,
2181 &SkFont::isSubpixel, &SkFont::setSubpixel);
2182
2183 fontFlag("Embedded Bitmap Text",
2184 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2185 &SkFontFields::fEmbeddedBitmaps,
2186 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2187
2188 fontFlag("Force Auto-Hinting",
2189 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2190 &SkFontFields::fForceAutoHinting,
2191 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2192
2193 int edgingIdx = 0;
2194 if (fFontOverrides.fEdging) {
2195 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2196 }
2197 if (ImGui::Combo("Edging", &edgingIdx,
2198 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2199 {
2200 if (edgingIdx == 0) {
2201 fFontOverrides.fEdging = false;
2202 fFont.setEdging(SkFont::Edging::kAlias);
2203 } else {
2204 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2205 fFontOverrides.fEdging = true;
2206 }
2207 paramsChanged = true;
2208 }
2209
Ben Wagner15a8d572019-03-21 13:35:44 -04002210 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2211 if (fFontOverrides.fSize) {
2212 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002213 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002214 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002215 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002216 fFontOverrides.fSizeRange[0],
2217 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002218 "%.6f", 2.0f))
2219 {
Mike Reed3ae47332019-01-04 10:11:46 -05002220 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002221 paramsChanged = true;
2222 }
2223 }
2224
2225 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2226 if (fFontOverrides.fScaleX) {
2227 float scaleX = fFont.getScaleX();
2228 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2229 fFont.setScaleX(scaleX);
2230 paramsChanged = true;
2231 }
2232 }
2233
2234 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2235 if (fFontOverrides.fSkewX) {
2236 float skewX = fFont.getSkewX();
2237 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2238 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002239 paramsChanged = true;
2240 }
2241 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002242 }
2243
Mike Reed81f60ec2018-05-15 10:09:52 -04002244 {
2245 SkMetaData controls;
2246 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2247 if (ImGui::CollapsingHeader("Current Slide")) {
2248 SkMetaData::Iter iter(controls);
2249 const char* name;
2250 SkMetaData::Type type;
2251 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002252 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002253 if (type == SkMetaData::kScalar_Type) {
2254 float val[3];
2255 SkASSERT(count == 3);
2256 controls.findScalars(name, &count, val);
2257 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2258 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002259 }
Ben Wagner110c7032019-03-22 17:03:59 -04002260 } else if (type == SkMetaData::kBool_Type) {
2261 bool val;
2262 SkASSERT(count == 1);
2263 controls.findBool(name, &val);
2264 if (ImGui::Checkbox(name, &val)) {
2265 controls.setBool(name, val);
2266 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002267 }
2268 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002269 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002270 }
2271 }
2272 }
2273
Ben Wagner7a3c6742018-04-23 10:01:07 -04002274 if (fShowSlidePicker) {
2275 ImGui::SetNextTreeNodeOpen(true);
2276 }
Brian Osman79086b92017-02-10 13:36:16 -05002277 if (ImGui::CollapsingHeader("Slide")) {
2278 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002279 static ImVector<const char*> filteredSlideNames;
2280 static ImVector<int> filteredSlideIndices;
2281
Brian Osmanfce09c52017-11-14 15:32:20 -05002282 if (fShowSlidePicker) {
2283 ImGui::SetKeyboardFocusHere();
2284 fShowSlidePicker = false;
2285 }
2286
Brian Osman79086b92017-02-10 13:36:16 -05002287 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002288 filteredSlideNames.clear();
2289 filteredSlideIndices.clear();
2290 int filteredIndex = 0;
2291 for (int i = 0; i < fSlides.count(); ++i) {
2292 const char* slideName = fSlides[i]->getName().c_str();
2293 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2294 if (i == fCurrentSlide) {
2295 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002296 }
Brian Osmanf479e422017-11-08 13:11:36 -05002297 filteredSlideNames.push_back(slideName);
2298 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002299 }
Brian Osman79086b92017-02-10 13:36:16 -05002300 }
Brian Osmanf479e422017-11-08 13:11:36 -05002301
Brian Osmanf479e422017-11-08 13:11:36 -05002302 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2303 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002304 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002305 }
2306 }
Brian Osmana109e392017-02-24 09:49:14 -05002307
2308 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002309 ColorMode newMode = fColorMode;
2310 auto cmButton = [&](ColorMode mode, const char* label) {
2311 if (ImGui::RadioButton(label, mode == fColorMode)) {
2312 newMode = mode;
2313 }
2314 };
2315
2316 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002317 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2318 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002319 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002320
2321 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002322 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002323 }
2324
2325 // Pick from common gamuts:
2326 int primariesIdx = 4; // Default: Custom
2327 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2328 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2329 primariesIdx = i;
2330 break;
2331 }
2332 }
2333
Brian Osman03115dc2018-11-26 13:55:19 -05002334 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002335 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002336
Brian Osmana109e392017-02-24 09:49:14 -05002337 if (ImGui::Combo("Primaries", &primariesIdx,
2338 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2339 if (primariesIdx >= 0 && primariesIdx <= 3) {
2340 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2341 }
2342 }
2343
2344 // Allow direct editing of gamut
2345 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2346 }
Brian Osman207d4102019-01-10 09:40:58 -05002347
2348 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002349 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002350 if (ImGui::Checkbox("Pause", &isPaused)) {
2351 fAnimTimer.togglePauseResume();
2352 }
Brian Osman707d2022019-01-10 11:27:34 -05002353
2354 float speed = fAnimTimer.getSpeed();
2355 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2356 fAnimTimer.setSpeed(speed);
2357 }
Brian Osman207d4102019-01-10 09:40:58 -05002358 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002359
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002360 if (ImGui::CollapsingHeader("Shaders")) {
2361 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2362 GrContextOptions::ShaderCacheStrategy::kSkSL;
John Stiles7247b482021-03-08 10:40:35 -05002363
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002364#if defined(SK_VULKAN)
2365 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2366#else
2367 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002368#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002369
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002370 // To re-load shaders from the currently active programs, we flush all
2371 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002372 static bool gLoadPending = false;
2373 if (gLoadPending) {
2374 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
Brian Osmanf0de96f2021-02-26 13:54:11 -05002375 const SkString& description, int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002376 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002377 entry.fKey = key;
2378 SkMD5 hash;
2379 hash.write(key->bytes(), key->size());
2380 SkMD5::Digest digest = hash.finish();
2381 for (int i = 0; i < 16; ++i) {
2382 entry.fKeyString.appendf("%02x", digest.data[i]);
2383 }
Brian Osmanf0de96f2021-02-26 13:54:11 -05002384 entry.fKeyDescription = description;
Brian Osman0b8bb882019-04-12 11:47:19 -04002385
Brian Osman9e4e4c72020-06-10 07:19:34 -04002386 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002387 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002388 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2389 entry.fInputs,
2390 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002391 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002392 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002393 fPersistentCache.foreach(collectShaders);
2394 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002395
2396#if defined(SK_VULKAN)
2397 if (isVulkan && !sksl) {
2398 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2399 for (auto& entry : fCachedShaders) {
2400 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2401 const SkSL::String& spirv(entry.fShader[i]);
2402 std::string disasm;
2403 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2404 &disasm);
2405 entry.fShader[i].assign(disasm);
2406 }
2407 }
2408 }
2409#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002410 }
2411
John Stilesa8f6b6f2021-03-05 16:00:42 -05002412 // Defer actually doing the View/Apply logic so that we can trigger an Apply when we
Brian Osman0b8bb882019-04-12 11:47:19 -04002413 // start or finish hovering on a tree node in the list below:
John Stilesa8f6b6f2021-03-05 16:00:42 -05002414 bool doView = ImGui::Button("View"); ImGui::SameLine();
2415 bool doApply = ImGui::Button("Apply Changes"); ImGui::SameLine();
John Stiles7247b482021-03-08 10:40:35 -05002416 bool doDump = ImGui::Button("Dump SkSL to resources/sksl/");
John Stilesa8f6b6f2021-03-05 16:00:42 -05002417
John Stiles2ee4d7a2021-03-30 10:30:47 -04002418 int newOptLevel = fOptLevel;
John Stiles7247b482021-03-08 10:40:35 -05002419 ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source);
2420 ImGui::SameLine();
2421 ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile);
2422 ImGui::SameLine();
2423 ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize);
2424 ImGui::SameLine();
2425 ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline);
John Stilesa8f6b6f2021-03-05 16:00:42 -05002426
John Stiles7247b482021-03-08 10:40:35 -05002427 // If we are changing the compile mode, we want to reset the cache and redo
2428 // everything.
John Stiles2ee4d7a2021-03-30 10:30:47 -04002429 if (doDump || newOptLevel != fOptLevel) {
John Stiles7247b482021-03-08 10:40:35 -05002430 sksl = doDump || (newOptLevel == kShaderOptLevel_Source);
John Stiles2ee4d7a2021-03-30 10:30:47 -04002431 fOptLevel = (ShaderOptLevel)newOptLevel;
2432 switch (fOptLevel) {
2433 case kShaderOptLevel_Source:
2434 Compiler::EnableOptimizer(OverrideFlag::kDefault);
2435 Compiler::EnableInliner(OverrideFlag::kDefault);
2436 break;
2437 case kShaderOptLevel_Compile:
2438 Compiler::EnableOptimizer(OverrideFlag::kOff);
2439 Compiler::EnableInliner(OverrideFlag::kOff);
2440 break;
2441 case kShaderOptLevel_Optimize:
2442 Compiler::EnableOptimizer(OverrideFlag::kOn);
2443 Compiler::EnableInliner(OverrideFlag::kOff);
2444 break;
2445 case kShaderOptLevel_Inline:
2446 Compiler::EnableOptimizer(OverrideFlag::kOn);
2447 Compiler::EnableInliner(OverrideFlag::kOn);
2448 break;
2449 }
John Stiles7247b482021-03-08 10:40:35 -05002450
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002451 params.fGrContextOptions.fShaderCacheStrategy =
2452 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2453 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2454 paramsChanged = true;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002455 doView = true;
2456
2457 fDeferredActions.push_back([=]() {
2458 // Reset the cache.
2459 fPersistentCache.reset();
2460 // Dump the cache once we have drawn a frame with it.
2461 if (doDump) {
2462 fDeferredActions.push_back([this]() {
2463 this->dumpShadersToResources();
2464 });
2465 }
2466 });
Brian Osmancbc33b82019-04-19 14:16:19 -04002467 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002468
2469 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002470 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002471 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2472 bool hovered = ImGui::IsItemHovered();
2473 if (hovered != entry.fHovered) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002474 // Force an Apply to patch the highlight shader in/out
Brian Osman0b8bb882019-04-12 11:47:19 -04002475 entry.fHovered = hovered;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002476 doApply = true;
Brian Osman0b8bb882019-04-12 11:47:19 -04002477 }
2478 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002479 auto stringBox = [](const char* label, std::string* str) {
2480 // Full width, and not too much space for each shader
2481 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2482 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2483 ImGui::InputTextMultiline(label, str, boxSize);
2484 };
Brian Osmanf0de96f2021-02-26 13:54:11 -05002485 if (ImGui::TreeNode("Key")) {
2486 ImGui::TextWrapped("%s", entry.fKeyDescription.c_str());
2487 ImGui::TreePop();
2488 }
Brian Osmaneb3fb902020-08-18 13:16:59 -04002489 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2490 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002491 ImGui::TreePop();
2492 }
2493 }
2494 ImGui::EndChild();
2495
John Stilesa8f6b6f2021-03-05 16:00:42 -05002496 if (doView) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002497 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002498 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002499 gLoadPending = true;
2500 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002501
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002502 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2503 // but I'm not sure anyone wants to do that.
2504 if (isVulkan && !sksl) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002505 doApply = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002506 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002507 if (doApply) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002508 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002509 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002510 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002511 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002512 if (entry.fHovered) {
2513 // The hovered item (if any) gets a special shader to make it
2514 // identifiable.
2515 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2516 switch (entry.fShaderType) {
2517 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2518 fragShader = build_sksl_highlight_shader();
2519 break;
2520 }
2521 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2522 fragShader = build_glsl_highlight_shader(
2523 *ctx->priv().caps()->shaderCaps());
2524 break;
2525 }
2526 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2527 fragShader = build_metal_highlight_shader(fragShader);
2528 break;
2529 }
2530 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002531 }
2532
Brian Osmana085a412019-04-25 09:44:43 -04002533 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2534 entry.fShader,
2535 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002536 kGrShaderTypeCount);
Brian Osmanf0de96f2021-02-26 13:54:11 -05002537 fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription);
Brian Osman0b8bb882019-04-12 11:47:19 -04002538
2539 entry.fShader[kFragment_GrShaderType] = backup;
2540 }
2541 }
2542 }
Brian Osman79086b92017-02-10 13:36:16 -05002543 }
Brian Salomon99a33902017-03-07 15:16:34 -05002544 if (paramsChanged) {
2545 fDeferredActions.push_back([=]() {
2546 fWindow->setRequestedDisplayParams(params);
2547 fWindow->inval();
2548 this->updateTitle();
2549 });
2550 }
Brian Osman79086b92017-02-10 13:36:16 -05002551 ImGui::End();
2552 }
2553
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002554 if (gShaderErrorHandler.fErrors.count()) {
2555 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002556 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002557 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2558 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002559 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2560 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2561 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2562 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002563 }
2564 ImGui::End();
2565 gShaderErrorHandler.reset();
2566 }
2567
Brian Osmanf6877092017-02-13 09:39:57 -05002568 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002569 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2570 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002571 static int zoomFactor = 8;
2572 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002573 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002574 }
2575 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2576 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002577 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002578 }
Brian Osmanf6877092017-02-13 09:39:57 -05002579
Ben Wagner3627d2e2018-06-26 14:23:20 -04002580 if (!fZoomWindowFixed) {
2581 ImVec2 mousePos = ImGui::GetMousePos();
2582 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2583 }
2584 SkScalar x = fZoomWindowLocation.x();
2585 SkScalar y = fZoomWindowLocation.y();
2586 int xInt = SkScalarRoundToInt(x);
2587 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002588 ImVec2 avail = ImGui::GetContentRegionAvail();
2589
Brian Osmanead517d2017-11-13 15:36:36 -05002590 uint32_t pixel = 0;
2591 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002592 auto dContext = fWindow->directContext();
2593 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002594 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002595 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002596 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002597 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002598 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2599 }
2600
Greg Danielfbc60b72020-11-03 17:27:02 -05002601 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002602 // Translate so the region of the image that's under the mouse cursor is centered
2603 // in the zoom canvas:
2604 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002605 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2606 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002607 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002608
2609 SkPaint outline;
2610 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002611 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002612 });
Brian Osmanf6877092017-02-13 09:39:57 -05002613 }
2614
2615 ImGui::End();
2616 }
Brian Osman79086b92017-02-10 13:36:16 -05002617}
2618
John Stilesa8f6b6f2021-03-05 16:00:42 -05002619void Viewer::dumpShadersToResources() {
2620 // Sort the list of cached shaders so we can maintain some minimal level of consistency.
2621 // It doesn't really matter, but it will keep files from switching places unpredictably.
2622 std::vector<const CachedShader*> shaders;
2623 shaders.reserve(fCachedShaders.size());
2624 for (const CachedShader& shader : fCachedShaders) {
2625 shaders.push_back(&shader);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002626 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002627
2628 std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) {
2629 return std::tie(a->fShader[kFragment_GrShaderType], a->fShader[kVertex_GrShaderType]) <
2630 std::tie(b->fShader[kFragment_GrShaderType], b->fShader[kVertex_GrShaderType]);
2631 });
2632
2633 // Make the resources/sksl/SlideName/ directory.
2634 SkString directory = SkStringPrintf("%ssksl/%s",
2635 GetResourcePath().c_str(),
2636 fSlides[fCurrentSlide]->getName().c_str());
2637 if (!sk_mkdir(directory.c_str())) {
2638 SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str());
2639 return;
2640 }
2641
2642 int index = 0;
2643 for (const auto& entry : shaders) {
2644 SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index);
2645 FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag);
2646 if (vertFile) {
2647 const SkSL::String& vertText = entry->fShader[kVertex_GrShaderType];
2648 SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile));
2649 sk_fclose(vertFile);
2650 } else {
2651 SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str());
2652 }
2653
2654 SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index);
2655 FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag);
2656 if (fragFile) {
2657 const SkSL::String& fragText = entry->fShader[kFragment_GrShaderType];
2658 SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile));
2659 sk_fclose(fragFile);
2660 } else {
2661 SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str());
2662 }
2663
2664 ++index;
2665 }
2666}
2667
2668void Viewer::onIdle() {
2669 SkTArray<std::function<void()>> actionsToRun;
2670 actionsToRun.swap(fDeferredActions);
2671
2672 for (const auto& fn : actionsToRun) {
2673 fn();
2674 }
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002675
Brian Osman56a24812017-12-19 11:15:16 -05002676 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002677 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002678 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002679 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002680
Brian Osman79086b92017-02-10 13:36:16 -05002681 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002682 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2683 // not be visible, though. So we need to redraw if there is at least one visible window, or
2684 // more than one active window. Newly created windows are active but not visible for one frame
2685 // while they determine their layout and sizing.
2686 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2687 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002688 fWindow->inval();
2689 }
jvanverth9f372462016-04-06 06:08:59 -07002690}
liyuqiane5a6cd92016-05-27 08:52:52 -07002691
Florin Malitab632df72018-06-18 21:23:06 -04002692template <typename OptionsFunc>
2693static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2694 OptionsFunc&& optionsFunc) {
2695 writer.beginObject();
2696 {
2697 writer.appendString(kName , name);
2698 writer.appendString(kValue, value);
2699
2700 writer.beginArray(kOptions);
2701 {
2702 optionsFunc(writer);
2703 }
2704 writer.endArray();
2705 }
2706 writer.endObject();
2707}
2708
2709
liyuqiane5a6cd92016-05-27 08:52:52 -07002710void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002711 if (!fWindow) {
2712 return;
2713 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002714 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002715 return; // Surface hasn't been created yet.
2716 }
2717
Florin Malitab632df72018-06-18 21:23:06 -04002718 SkDynamicMemoryWStream memStream;
2719 SkJSONWriter writer(&memStream);
2720 writer.beginArray();
2721
liyuqianb73c24b2016-06-03 08:47:23 -07002722 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002723 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2724 [this](SkJSONWriter& writer) {
2725 for(const auto& slide : fSlides) {
2726 writer.appendString(slide->getName().c_str());
2727 }
2728 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002729
liyuqianb73c24b2016-06-03 08:47:23 -07002730 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002731 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2732 [](SkJSONWriter& writer) {
2733 for (const auto& str : kBackendTypeStrings) {
2734 writer.appendString(str);
2735 }
2736 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002737
csmartdalton578f0642017-02-24 16:04:47 -07002738 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002739 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2740 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2741 [this](SkJSONWriter& writer) {
2742 writer.appendS32(0);
2743
2744 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2745 return;
2746 }
2747
2748 for (int msaa : {4, 8, 16}) {
2749 writer.appendS32(msaa);
2750 }
2751 });
csmartdalton578f0642017-02-24 16:04:47 -07002752
csmartdalton61cd31a2017-02-27 17:00:53 -07002753 // Path renderer state
2754 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002755 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2756 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002757 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002758 if (!ctx) {
2759 writer.appendString("Software");
2760 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002761 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002762 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
Chris Dalton57ab06c2021-04-22 12:57:28 -06002763 if (fWindow->sampleCount() > 1 || FLAGS_dmsaa) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002764 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002765 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002766 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002767 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002768 }
2769 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002770 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2771 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002772 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002773 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002774 }
2775 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002776
liyuqianb73c24b2016-06-03 08:47:23 -07002777 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002778 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2779 [this](SkJSONWriter& writer) {
2780 writer.appendString(kSoftkeyHint);
2781 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2782 writer.appendString(softkey.c_str());
2783 }
2784 });
liyuqianb73c24b2016-06-03 08:47:23 -07002785
Florin Malitab632df72018-06-18 21:23:06 -04002786 writer.endArray();
2787 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002788
Florin Malitab632df72018-06-18 21:23:06 -04002789 auto data = memStream.detachAsData();
2790
2791 // TODO: would be cool to avoid this copy
2792 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2793
2794 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002795}
2796
2797void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002798 // For those who will add more features to handle the state change in this function:
2799 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2800 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2801 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002802 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002803 for (int i = 0; i < fSlides.count(); ++i) {
2804 if (fSlides[i]->getName().equals(stateValue)) {
2805 this->setCurrentSlide(i);
2806 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002807 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002808 }
Florin Malitaab99c342018-01-16 16:23:03 -05002809
2810 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002811 } else if (stateName.equals(kBackendStateName)) {
2812 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2813 if (stateValue.equals(kBackendTypeStrings[i])) {
2814 if (fBackendType != i) {
2815 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002816 for(auto& slide : fSlides) {
2817 slide->gpuTeardown();
2818 }
liyuqian6cb70252016-06-02 12:16:25 -07002819 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002820 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002821 }
2822 break;
2823 }
2824 }
csmartdalton578f0642017-02-24 16:04:47 -07002825 } else if (stateName.equals(kMSAAStateName)) {
2826 DisplayParams params = fWindow->getRequestedDisplayParams();
2827 int sampleCount = atoi(stateValue.c_str());
2828 if (sampleCount != params.fMSAASampleCount) {
2829 params.fMSAASampleCount = sampleCount;
2830 fWindow->setRequestedDisplayParams(params);
2831 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002832 this->updateTitle();
2833 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002834 }
2835 } else if (stateName.equals(kPathRendererStateName)) {
2836 DisplayParams params = fWindow->getRequestedDisplayParams();
2837 for (const auto& pair : gPathRendererNames) {
2838 if (pair.second == stateValue.c_str()) {
2839 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2840 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2841 fWindow->setRequestedDisplayParams(params);
2842 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002843 this->updateTitle();
2844 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002845 }
2846 break;
2847 }
csmartdalton578f0642017-02-24 16:04:47 -07002848 }
liyuqianb73c24b2016-06-03 08:47:23 -07002849 } else if (stateName.equals(kSoftkeyStateName)) {
2850 if (!stateValue.equals(kSoftkeyHint)) {
2851 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002852 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002853 }
liyuqian2edb0f42016-07-06 14:11:32 -07002854 } else if (stateName.equals(kRefreshStateName)) {
2855 // This state is actually NOT in the UI state.
2856 // We use this to allow Android to quickly set bool fRefresh.
2857 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002858 } else {
2859 SkDebugf("Unknown stateName: %s", stateName.c_str());
2860 }
2861}
Brian Osman79086b92017-02-10 13:36:16 -05002862
Hal Canaryb1f411a2019-08-29 10:39:22 -04002863bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002864 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002865}
2866
Hal Canaryb1f411a2019-08-29 10:39:22 -04002867bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002868 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002869 fWindow->inval();
2870 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002871 } else {
2872 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002873 }
Brian Osman79086b92017-02-10 13:36:16 -05002874}