blob: 7c37b0cba3b0282698a999599f07a4ba4a5aad8f [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkGraphics.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkSurface.h"
Robert Phillipsed653392020-07-10 13:55:21 -040014#include "include/gpu/GrDirectContext.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050015#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkTo.h"
17#include "include/utils/SkPaintFilterCanvas.h"
18#include "src/core/SkColorSpacePriv.h"
19#include "src/core/SkImagePriv.h"
20#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkOSFile.h"
John Stilesf2c2d302021-04-09 17:56:58 -040022#include "src/core/SkReadBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkScan.h"
Chris Dalton94df5722021-04-19 17:40:40 -060024#include "src/core/SkSurfacePriv.h"
John Stilesdf078002020-07-14 09:44:57 -040025#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040027#include "src/core/SkTextBlobPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040028#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrGpu.h"
30#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070031#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Chris Daltonff18ff62020-12-07 17:39:26 -070033#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Adlai Hollerbcfc5542020-08-27 12:44:07 -040034#include "src/image/SkImage_Base.h"
John Stiles2ee4d7a2021-03-30 10:30:47 -040035#include "src/sksl/SkSLCompiler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/utils/SkJSONWriter.h"
37#include "src/utils/SkOSPath.h"
38#include "tools/Resources.h"
39#include "tools/ToolUtils.h"
40#include "tools/flags/CommandLineFlags.h"
41#include "tools/flags/CommonFlags.h"
42#include "tools/trace/EventTracingPriv.h"
43#include "tools/viewer/BisectSlide.h"
44#include "tools/viewer/GMSlide.h"
45#include "tools/viewer/ImageSlide.h"
46#include "tools/viewer/ParticlesSlide.h"
47#include "tools/viewer/SKPSlide.h"
48#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050049#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "tools/viewer/SlideDir.h"
51#include "tools/viewer/SvgSlide.h"
52#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070053
Chris Dalton17dc4182020-03-25 16:18:16 -060054#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040055#include <map>
56
Hal Canary8a001442018-09-19 11:31:27 -040057#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040058#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040059
Brian Osmanc85f1fa2020-06-16 15:11:34 -040060#ifdef SK_VULKAN
61#include "spirv-tools/libspirv.hpp"
62#endif
63
Florin Malita87ccf332018-05-04 12:23:24 -040064#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050065 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040066#endif
Florin Malita45cd2002020-06-09 14:00:54 -040067#if defined(SK_ENABLE_SKRIVE)
68 #include "tools/viewer/SkRiveSlide.h"
69#endif
Florin Malita87ccf332018-05-04 12:23:24 -040070
Brian Osman5e7fbfd2019-05-03 13:13:35 -040071class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
72public:
73 void compileError(const char* shader, const char* errors) override {
74 fShaders.push_back(SkString(shader));
75 fErrors.push_back(SkString(errors));
76 }
77
78 void reset() {
79 fShaders.reset();
80 fErrors.reset();
81 }
82
83 SkTArray<SkString> fShaders;
84 SkTArray<SkString> fErrors;
85};
86
87static CapturingShaderErrorHandler gShaderErrorHandler;
88
Brian Osmanf847f312020-06-18 14:18:27 -040089GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
90
jvanverth34524262016-05-04 13:49:13 -070091using namespace sk_app;
John Stiles2ee4d7a2021-03-30 10:30:47 -040092using SkSL::Compiler;
93using OverrideFlag = SkSL::Compiler::OverrideFlag;
jvanverth34524262016-05-04 13:49:13 -070094
csmartdalton61cd31a2017-02-27 17:00:53 -070095static std::map<GpuPathRenderers, std::string> gPathRendererNames;
96
jvanverth9f372462016-04-06 06:08:59 -070097Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070098 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070099}
100
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600101static DEFINE_string(slide, "", "Start on this sample.");
102static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -0500103
Jim Van Verth682a2f42020-05-13 16:54:09 -0400104#ifdef SK_GL
105#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700106#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400107#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700108#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400109#ifdef SK_VULKAN
110#define VK_BACKEND_STR ", \"vk\""
111#else
112#define VK_BACKEND_STR
113#endif
114#ifdef SK_METAL
115#define MTL_BACKEND_STR ", \"mtl\""
116#else
117#define MTL_BACKEND_STR
118#endif
119#ifdef SK_DIRECT3D
120#define D3D_BACKEND_STR ", \"d3d\""
121#else
122#define D3D_BACKEND_STR
123#endif
124#ifdef SK_DAWN
125#define DAWN_BACKEND_STR ", \"dawn\""
126#else
127#define DAWN_BACKEND_STR
128#endif
129#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
130#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
131 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700132
Brian Osman2dd96932016-10-18 15:33:53 -0400133static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700134
Mike Klein5b3f3432019-03-21 11:42:21 -0500135static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
Chris Daltonfd708652021-03-17 21:10:17 -0600136static DEFINE_bool(dmsaa, false, "Use internal MSAA to render to non-MSAA surfaces?");
csmartdalton008b9d82017-02-22 12:00:42 -0700137
Mike Klein84836b72019-03-21 11:31:36 -0500138static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700139
Mike Klein84836b72019-03-21 11:31:36 -0500140static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400141
Mike Kleinc6142d82019-03-25 10:54:59 -0500142static DEFINE_string2(match, m, nullptr,
143 "[~][^]substring[$] [...] of name to run.\n"
144 "Multiple matches may be separated by spaces.\n"
145 "~ causes a matching name to always be skipped\n"
146 "^ requires the start of the name to match\n"
147 "$ requires the end of the name to match\n"
148 "^ and $ requires an exact match\n"
149 "If a name does not match any list entry,\n"
150 "it is skipped unless some list entry starts with ~");
151
Mike Klein19fb3972019-03-21 13:08:08 -0500152#if defined(SK_BUILD_FOR_ANDROID)
153 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500154 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
155 static DEFINE_string(lotties, "/data/local/tmp/lotties",
156 "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400157 static DEFINE_string(rives, "/data/local/tmp/rives",
158 "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500159#else
160 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500161 static DEFINE_string(skps, "skps", "Directory to read skps from.");
162 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400163 static DEFINE_string(rives, "rives", "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500164#endif
165
Mike Kleinc6142d82019-03-25 10:54:59 -0500166static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
167
168static DEFINE_int_2(threads, j, -1,
169 "Run threadsafe tests on a threadpool with this many extra threads, "
170 "defaulting to one extra thread per core.");
171
Jim Van Verth7b558182019-11-14 16:47:01 -0500172static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
173
Chris Daltonc8877332020-01-06 09:48:30 -0700174static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500175static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
176static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500177static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500178static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500179static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500180
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400181#ifndef SK_GL
182static_assert(false, "viewer requires GL backend for raster.")
183#endif
184
Brian Salomon194db172017-08-17 14:37:06 -0400185const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700186 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400187#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
188 "ANGLE",
189#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400190#ifdef SK_DAWN
191 "Dawn",
192#endif
jvanverth063ece72016-06-17 09:29:14 -0700193#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700194 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700195#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400196#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500197 "Metal",
198#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400199#ifdef SK_DIRECT3D
200 "Direct3D",
201#endif
csmartdalton578f0642017-02-24 16:04:47 -0700202 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700203};
204
bsalomon6c471f72016-07-26 12:56:32 -0700205static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400206#ifdef SK_DAWN
207 if (0 == strcmp(str, "dawn")) {
208 return sk_app::Window::kDawn_BackendType;
209 } else
210#endif
bsalomon6c471f72016-07-26 12:56:32 -0700211#ifdef SK_VULKAN
212 if (0 == strcmp(str, "vk")) {
213 return sk_app::Window::kVulkan_BackendType;
214 } else
215#endif
Brian Salomon194db172017-08-17 14:37:06 -0400216#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
217 if (0 == strcmp(str, "angle")) {
218 return sk_app::Window::kANGLE_BackendType;
219 } else
220#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400221#ifdef SK_METAL
222 if (0 == strcmp(str, "mtl")) {
223 return sk_app::Window::kMetal_BackendType;
224 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500225#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400226#ifdef SK_DIRECT3D
227 if (0 == strcmp(str, "d3d")) {
228 return sk_app::Window::kDirect3D_BackendType;
229 } else
230#endif
231
bsalomon6c471f72016-07-26 12:56:32 -0700232 if (0 == strcmp(str, "gl")) {
233 return sk_app::Window::kNativeGL_BackendType;
234 } else if (0 == strcmp(str, "sw")) {
235 return sk_app::Window::kRaster_BackendType;
236 } else {
237 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
238 return sk_app::Window::kRaster_BackendType;
239 }
240}
241
Brian Osmana109e392017-02-24 09:49:14 -0500242static SkColorSpacePrimaries gSrgbPrimaries = {
243 0.64f, 0.33f,
244 0.30f, 0.60f,
245 0.15f, 0.06f,
246 0.3127f, 0.3290f };
247
248static SkColorSpacePrimaries gAdobePrimaries = {
249 0.64f, 0.33f,
250 0.21f, 0.71f,
251 0.15f, 0.06f,
252 0.3127f, 0.3290f };
253
254static SkColorSpacePrimaries gP3Primaries = {
255 0.680f, 0.320f,
256 0.265f, 0.690f,
257 0.150f, 0.060f,
258 0.3127f, 0.3290f };
259
260static SkColorSpacePrimaries gRec2020Primaries = {
261 0.708f, 0.292f,
262 0.170f, 0.797f,
263 0.131f, 0.046f,
264 0.3127f, 0.3290f };
265
266struct NamedPrimaries {
267 const char* fName;
268 SkColorSpacePrimaries* fPrimaries;
269} gNamedPrimaries[] = {
270 { "sRGB", &gSrgbPrimaries },
271 { "AdobeRGB", &gAdobePrimaries },
272 { "P3", &gP3Primaries },
273 { "Rec. 2020", &gRec2020Primaries },
274};
275
276static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
277 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
278}
279
Brian Osman70d2f432017-11-08 09:54:10 -0500280static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
281 // In raster mode, we still use GL for the window.
282 // This lets us render the GUI faster (and correct).
283 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
284}
285
Jim Van Verth74826c82019-03-01 14:37:30 -0500286class NullSlide : public Slide {
287 SkISize getDimensions() const override {
288 return SkISize::Make(640, 480);
289 }
290
291 void draw(SkCanvas* canvas) override {
292 canvas->clear(0xffff11ff);
293 }
294};
295
John Stiles31964fd2020-05-05 16:05:47 -0400296static const char kName[] = "name";
297static const char kValue[] = "value";
298static const char kOptions[] = "options";
299static const char kSlideStateName[] = "Slide";
300static const char kBackendStateName[] = "Backend";
301static const char kMSAAStateName[] = "MSAA";
302static const char kPathRendererStateName[] = "Path renderer";
303static const char kSoftkeyStateName[] = "Softkey";
304static const char kSoftkeyHint[] = "Please select a softkey";
305static const char kON[] = "ON";
306static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700307
Mike Reed862818b2020-03-21 15:07:13 -0400308extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500309extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500310extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400311
jvanverth34524262016-05-04 13:49:13 -0700312Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500313 : fCurrentSlide(-1)
314 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500315 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400316 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500317 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500318 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500319 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500320 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400321 , fZoomWindowFixed(false)
322 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500323 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400324 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700325 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500326 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500327 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500328 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500329 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500330 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700331 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400332 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400333 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400334 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500335 , fTiled(false)
336 , fDrawTileBoundaries(false)
337 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400338 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700339{
Greg Daniel285db442016-10-14 09:12:53 -0400340 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700341
Chris Dalton37ae4b02019-12-28 14:51:11 -0700342 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600343 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500344 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600345 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600346 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500347 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700348
jvanverth2bb3b6d2016-04-08 07:24:09 -0700349 SkDebugf("Command line arguments: ");
350 for (int i = 1; i < argc; ++i) {
351 SkDebugf("%s ", argv[i]);
352 }
353 SkDebugf("\n");
354
Mike Klein88544fb2019-03-20 10:50:33 -0500355 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500356#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400357 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500358#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700359
Mike Reed862818b2020-03-21 15:07:13 -0400360 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500361 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500362 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400363
Mike Klein19cc0f62019-03-22 15:30:07 -0500364 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500365
Brian Osmanbc8150f2017-07-24 11:38:01 -0400366 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400367 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400368
bsalomon6c471f72016-07-26 12:56:32 -0700369 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700370 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700371
csmartdalton578f0642017-02-24 16:04:47 -0700372 DisplayParams displayParams;
373 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500374 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700375 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400376 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400377 displayParams.fGrContextOptions.fShaderCacheStrategy =
John Stiles2ee4d7a2021-03-30 10:30:47 -0400378 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400379 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
380 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Dalton94df5722021-04-19 17:40:40 -0600381 if (FLAGS_dmsaa) {
382 displayParams.fSurfaceProps = SkSurfaceProps(
383 displayParams.fSurfaceProps.flags() | kDMSAA_SkSurfacePropsPrivateFlag,
384 displayParams.fSurfaceProps.pixelGeometry());
385 }
csmartdalton578f0642017-02-24 16:04:47 -0700386 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400387 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500388 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700389
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500390 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500391 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500392
Brian Osman56a24812017-12-19 11:15:16 -0500393 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500394 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500395 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
396 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
397 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
398
jvanverth9f372462016-04-06 06:08:59 -0700399 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700400 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500401 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500402 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500403 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700404
brianosman622c8d52016-05-10 06:50:49 -0700405 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500406 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
407 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
408 fWindow->inval();
409 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500410 // Command to jump directly to the slide picker and give it focus
411 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
412 this->fShowImGuiDebugWindow = true;
413 this->fShowSlidePicker = true;
414 fWindow->inval();
415 });
416 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400417 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500418 this->fShowImGuiDebugWindow = true;
419 this->fShowSlidePicker = true;
420 fWindow->inval();
421 });
Brian Osman79086b92017-02-10 13:36:16 -0500422 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
423 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
424 fWindow->inval();
425 });
Brian Osmanf6877092017-02-13 09:39:57 -0500426 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
427 this->fShowZoomWindow = !this->fShowZoomWindow;
428 fWindow->inval();
429 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400430 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
431 this->fZoomWindowFixed = !this->fZoomWindowFixed;
432 fWindow->inval();
433 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400434 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400435 DisplayParams params = fWindow->getRequestedDisplayParams();
436 params.fDisableVsync = !params.fDisableVsync;
437 fWindow->setRequestedDisplayParams(params);
438 this->updateTitle();
439 fWindow->inval();
440 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400441 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
442 DisplayParams params = fWindow->getRequestedDisplayParams();
443 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
444 fWindow->setRequestedDisplayParams(params);
445 this->updateTitle();
446 fWindow->inval();
447 });
Mike Reedf702ed42019-07-22 17:00:49 -0400448 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
449 fRefresh = !fRefresh;
450 fWindow->inval();
451 });
brianosman622c8d52016-05-10 06:50:49 -0700452 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500453 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700454 fWindow->inval();
455 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400456 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500457 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400458 this->updateTitle();
459 fWindow->inval();
460 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500461 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500462 switch (fColorMode) {
463 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500464 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500465 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500466 case ColorMode::kColorManaged8888:
467 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500468 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500469 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400470 this->setColorMode(ColorMode::kColorManagedF16Norm);
471 break;
472 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500473 this->setColorMode(ColorMode::kLegacy);
474 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500475 }
brianosman622c8d52016-05-10 06:50:49 -0700476 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700477 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
478 DisplayParams params = fWindow->getRequestedDisplayParams();
479 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
480 fWindow->setRequestedDisplayParams(params);
481 fWindow->inval();
482 });
Brian Salomon91216d52021-04-09 11:57:59 -0400483 fCommands.addCommand('w', "Modes", "Toggle reduced shaders", [this]() {
484 DisplayParams params = fWindow->getRequestedDisplayParams();
485 params.fGrContextOptions.fReducedShaderVariations =
486 !params.fGrContextOptions.fReducedShaderVariations;
487 fWindow->setRequestedDisplayParams(params);
488 fWindow->inval();
489 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400490 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500491 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700492 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400493 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500494 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700495 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400496 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700497 this->changeZoomLevel(1.f / 32.f);
498 fWindow->inval();
499 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400500 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700501 this->changeZoomLevel(-1.f / 32.f);
502 fWindow->inval();
503 });
jvanverthaf236b52016-05-20 06:01:06 -0700504 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400505 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
506 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500507 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400508#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
509 if (newBackend == sk_app::Window::kVulkan_BackendType) {
510 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
511 sk_app::Window::kBackendTypeCount);
512 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
513 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500514 }
515#endif
Brian Osman621491e2017-02-28 15:45:01 -0500516 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700517 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500518 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
519 fSaveToSKP = true;
520 fWindow->inval();
521 });
Mike Reed376d8122019-03-14 11:39:02 -0400522 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
523 fShowSlideDimensions = !fShowSlideDimensions;
524 fWindow->inval();
525 });
Ben Wagner37c54032018-04-13 14:30:23 -0400526 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
527 DisplayParams params = fWindow->getRequestedDisplayParams();
528 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400529 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
530 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
531 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400532 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
533 } else {
534 switch (params.fSurfaceProps.pixelGeometry()) {
535 case kUnknown_SkPixelGeometry:
536 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
537 break;
538 case kRGB_H_SkPixelGeometry:
539 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
540 break;
541 case kBGR_H_SkPixelGeometry:
542 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
543 break;
544 case kRGB_V_SkPixelGeometry:
545 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
546 break;
547 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400548 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
549 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400550 break;
551 }
552 }
553 fWindow->setRequestedDisplayParams(params);
554 this->updateTitle();
555 fWindow->inval();
556 });
Ben Wagner9613e452019-01-23 10:34:59 -0500557 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500558 if (!fFontOverrides.fHinting) {
559 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400560 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500561 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500562 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400563 case SkFontHinting::kNone:
564 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500565 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400566 case SkFontHinting::kSlight:
567 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500568 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400569 case SkFontHinting::kNormal:
570 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500571 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400572 case SkFontHinting::kFull:
573 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500574 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500575 break;
576 }
577 }
578 this->updateTitle();
579 fWindow->inval();
580 });
581 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500582 if (!fPaintOverrides.fAntiAlias) {
583 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
584 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500585 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500586 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500587 } else {
588 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500589 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500590 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500591 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400592 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500593 break;
594 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500595 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500596 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400597 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500598 break;
599 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500600 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400601 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500602 break;
603 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500604 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
605 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500606 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
607 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500608 break;
609 }
610 }
611 this->updateTitle();
612 fWindow->inval();
613 });
Ben Wagner37c54032018-04-13 14:30:23 -0400614 fCommands.addCommand('D', "Modes", "DFT", [this]() {
615 DisplayParams params = fWindow->getRequestedDisplayParams();
616 uint32_t flags = params.fSurfaceProps.flags();
617 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
618 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
619 fWindow->setRequestedDisplayParams(params);
620 this->updateTitle();
621 fWindow->inval();
622 });
Ben Wagner9613e452019-01-23 10:34:59 -0500623 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500624 if (!fFontOverrides.fEdging) {
625 fFontOverrides.fEdging = true;
626 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500627 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500628 switch (fFont.getEdging()) {
629 case SkFont::Edging::kAlias:
630 fFont.setEdging(SkFont::Edging::kAntiAlias);
631 break;
632 case SkFont::Edging::kAntiAlias:
633 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
634 break;
635 case SkFont::Edging::kSubpixelAntiAlias:
636 fFont.setEdging(SkFont::Edging::kAlias);
637 fFontOverrides.fEdging = false;
638 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500639 }
640 }
641 this->updateTitle();
642 fWindow->inval();
643 });
Ben Wagner9613e452019-01-23 10:34:59 -0500644 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500645 if (!fFontOverrides.fSubpixel) {
646 fFontOverrides.fSubpixel = true;
647 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500648 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500649 if (!fFont.isSubpixel()) {
650 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500651 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500652 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500653 }
654 }
655 this->updateTitle();
656 fWindow->inval();
657 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400658 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
659 if (!fFontOverrides.fBaselineSnap) {
660 fFontOverrides.fBaselineSnap = true;
661 fFont.setBaselineSnap(false);
662 } else {
663 if (!fFont.isBaselineSnap()) {
664 fFont.setBaselineSnap(true);
665 } else {
666 fFontOverrides.fBaselineSnap = false;
667 }
668 }
669 this->updateTitle();
670 fWindow->inval();
671 });
Brian Osman805a7272018-05-02 15:40:20 -0400672 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
673 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
674 : kPerspective_Real;
675 this->updateTitle();
676 fWindow->inval();
677 });
678 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
679 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
680 : kPerspective_Off;
681 this->updateTitle();
682 fWindow->inval();
683 });
Brian Osman207d4102019-01-10 09:40:58 -0500684 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
685 fAnimTimer.togglePauseResume();
686 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400687 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
688 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500689 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400690 fWindow->inval();
691 });
Mike Reed59295352020-03-12 13:56:34 -0400692 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
693 fDrawViaSerialize = !fDrawViaSerialize;
694 this->updateTitle();
695 fWindow->inval();
696 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500697 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400698 gUseSkVMBlitter = !gUseSkVMBlitter;
699 this->updateTitle();
700 fWindow->inval();
701 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500702 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
703 gSkVMAllowJIT = !gSkVMAllowJIT;
704 this->updateTitle();
705 fWindow->inval();
706 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500707
jvanverth2bb3b6d2016-04-08 07:24:09 -0700708 // set up slides
709 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500710 if (FLAGS_list) {
711 this->listNames();
712 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700713
Brian Osman9bb47cf2018-04-26 15:55:00 -0400714 fPerspectivePoints[0].set(0, 0);
715 fPerspectivePoints[1].set(1, 0);
716 fPerspectivePoints[2].set(0, 1);
717 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700718 fAnimTimer.run();
719
Hal Canaryc465d132017-12-08 10:21:31 -0500720 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500721 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500722 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500723 }
724 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500725
jongdeok.kim804f17e2019-02-26 14:39:23 +0900726 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500727 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700728}
729
jvanverth34524262016-05-04 13:49:13 -0700730void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400731 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
732 static const struct {
733 const char* fExtension;
734 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500735 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400736 const SlideFactory fFactory;
737 } gExternalSlidesInfo[] = {
738 { ".skp", "skp-dir", FLAGS_skps,
739 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
740 return sk_make_sp<SKPSlide>(name, path);}
741 },
742 { ".jpg", "jpg-dir", FLAGS_jpgs,
743 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
744 return sk_make_sp<ImageSlide>(name, path);}
745 },
Florin Malita87ccf332018-05-04 12:23:24 -0400746#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400747 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400748 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
749 return sk_make_sp<SkottieSlide>(name, path);}
750 },
Florin Malita87ccf332018-05-04 12:23:24 -0400751#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400752 #if defined(SK_ENABLE_SKRIVE)
753 { ".flr", "skrive-dir", FLAGS_rives,
754 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
755 return sk_make_sp<SkRiveSlide>(name, path);}
756 },
757 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400758#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400759 { ".svg", "svg-dir", FLAGS_svgs,
760 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
761 return sk_make_sp<SvgSlide>(name, path);}
762 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400763#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400764 };
jvanverthc265a922016-04-08 12:51:45 -0700765
Brian Salomon343553a2018-09-05 15:41:23 -0400766 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700767
Mike Klein88544fb2019-03-20 10:50:33 -0500768 const auto addSlide =
769 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
770 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
771 return;
772 }
liyuqian6f163d22016-06-13 12:26:45 -0700773
Mike Klein88544fb2019-03-20 10:50:33 -0500774 if (auto slide = fact(name, path)) {
775 dirSlides.push_back(slide);
776 fSlides.push_back(std::move(slide));
777 }
778 };
Florin Malita76a076b2018-02-15 18:40:48 -0500779
Florin Malita38792ce2018-05-08 10:36:18 -0400780 if (!FLAGS_file.isEmpty()) {
781 // single file mode
782 const SkString file(FLAGS_file[0]);
783
784 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
785 for (const auto& sinfo : gExternalSlidesInfo) {
786 if (file.endsWith(sinfo.fExtension)) {
787 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
788 return;
789 }
790 }
791
792 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
793 } else {
794 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
795 }
796
797 return;
798 }
799
800 // Bisect slide.
801 if (!FLAGS_bisect.isEmpty()) {
802 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500803 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400804 if (FLAGS_bisect.count() >= 2) {
805 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
806 bisect->onChar(*ch);
807 }
808 }
809 fSlides.push_back(std::move(bisect));
810 }
811 }
812
813 // GMs
814 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400815 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400816 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500817 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400818 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400819 fSlides.push_back(std::move(slide));
820 }
Florin Malita38792ce2018-05-08 10:36:18 -0400821 }
822 // reverse gms
823 int numGMs = fSlides.count() - firstGM;
824 for (int i = 0; i < numGMs/2; ++i) {
825 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
826 }
827
828 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400829 for (const SampleFactory factory : SampleRegistry::Range()) {
830 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500831 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400832 fSlides.push_back(slide);
833 }
Florin Malita38792ce2018-05-08 10:36:18 -0400834 }
835
Brian Osman7c979f52019-02-12 13:27:51 -0500836 // Particle demo
837 {
838 // TODO: Convert this to a sample
839 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500840 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500841 fSlides.push_back(std::move(slide));
842 }
843 }
844
Brian Osmand927bd22019-12-18 11:23:12 -0500845 // Runtime shader editor
846 {
847 sk_sp<Slide> slide(new SkSLSlide());
848 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
849 fSlides.push_back(std::move(slide));
850 }
851 }
852
Florin Malita0ffa3222018-04-05 14:34:45 -0400853 for (const auto& info : gExternalSlidesInfo) {
854 for (const auto& flag : info.fFlags) {
855 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
856 // single file
857 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
858 } else {
859 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400860 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400861 SkTArray<SkString> sortedFilenames;
862 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400863 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400864 sortedFilenames.push_back(name);
865 }
866 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400867 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000868 [](const SkString& a, const SkString& b) {
869 return strcmp(a.c_str(), b.c_str()) < 0;
870 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400871 }
872 for (const SkString& filename : sortedFilenames) {
873 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
874 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400875 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400876 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400877 if (!dirSlides.empty()) {
878 fSlides.push_back(
879 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
880 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500881 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400882 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400883 }
884 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500885
886 if (!fSlides.count()) {
887 sk_sp<Slide> slide(new NullSlide());
888 fSlides.push_back(std::move(slide));
889 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700890}
891
892
jvanverth34524262016-05-04 13:49:13 -0700893Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400894 for(auto& slide : fSlides) {
895 slide->gpuTeardown();
896 }
897
jvanverth9f372462016-04-06 06:08:59 -0700898 fWindow->detach();
899 delete fWindow;
900}
901
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500902struct SkPaintTitleUpdater {
903 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
904 void append(const char* s) {
905 if (fCount == 0) {
906 fTitle->append(" {");
907 } else {
908 fTitle->append(", ");
909 }
910 fTitle->append(s);
911 ++fCount;
912 }
913 void done() {
914 if (fCount > 0) {
915 fTitle->append("}");
916 }
917 }
918 SkString* fTitle;
919 int fCount;
920};
921
brianosman05de2162016-05-06 13:28:57 -0700922void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700923 if (!fWindow) {
924 return;
925 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500926 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700927 return; // Surface hasn't been created yet.
928 }
929
jvanverth34524262016-05-04 13:49:13 -0700930 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700931 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700932
Mike Kleine5acd752019-03-22 09:57:16 -0500933 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400934 if (gSkForceAnalyticAA) {
935 title.append(" <FAAA>");
936 } else {
937 title.append(" <AAA>");
938 }
939 }
Mike Reed59295352020-03-12 13:56:34 -0400940 if (fDrawViaSerialize) {
941 title.append(" <serialize>");
942 }
Mike Reed862818b2020-03-21 15:07:13 -0400943 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500944 title.append(" <SkVMBlitter>");
945 }
946 if (!gSkVMAllowJIT) {
947 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400948 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400949
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500950 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500951 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
952 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400953 const char* on, const char* off)
954 {
Ben Wagner9613e452019-01-23 10:34:59 -0500955 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400956 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500957 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400958 };
959
Ben Wagner9613e452019-01-23 10:34:59 -0500960 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
961 const char* on, const char* off)
962 {
963 if (fFontOverrides.*flag) {
964 paintTitle.append((fFont.*isFlag)() ? on : off);
965 }
966 };
967
968 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
969 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
970
971 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
972 "Force Autohint", "No Force Autohint");
973 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400974 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500975 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
976 "Linear Metrics", "Non-Linear Metrics");
977 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
978 "Bitmap Text", "No Bitmap Text");
979 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
980
981 if (fFontOverrides.fEdging) {
982 switch (fFont.getEdging()) {
983 case SkFont::Edging::kAlias:
984 paintTitle.append("Alias Text");
985 break;
986 case SkFont::Edging::kAntiAlias:
987 paintTitle.append("Antialias Text");
988 break;
989 case SkFont::Edging::kSubpixelAntiAlias:
990 paintTitle.append("Subpixel Antialias Text");
991 break;
992 }
993 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400994
Mike Reed3ae47332019-01-04 10:11:46 -0500995 if (fFontOverrides.fHinting) {
996 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400997 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500998 paintTitle.append("No Hinting");
999 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001000 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001001 paintTitle.append("Slight Hinting");
1002 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001003 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001004 paintTitle.append("Normal Hinting");
1005 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001006 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001007 paintTitle.append("Full Hinting");
1008 break;
1009 }
1010 }
1011 paintTitle.done();
1012
Brian Osman92004802017-03-06 11:47:26 -05001013 switch (fColorMode) {
1014 case ColorMode::kLegacy:
1015 title.append(" Legacy 8888");
1016 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001017 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001018 title.append(" ColorManaged 8888");
1019 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001020 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001021 title.append(" ColorManaged F16");
1022 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001023 case ColorMode::kColorManagedF16Norm:
1024 title.append(" ColorManaged F16 Norm");
1025 break;
Brian Osman92004802017-03-06 11:47:26 -05001026 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001027
Brian Osman92004802017-03-06 11:47:26 -05001028 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001029 int curPrimaries = -1;
1030 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1031 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1032 curPrimaries = i;
1033 break;
1034 }
1035 }
Brian Osman03115dc2018-11-26 13:55:19 -05001036 title.appendf(" %s Gamma %f",
1037 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001038 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001039 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001040
Ben Wagner37c54032018-04-13 14:30:23 -04001041 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001042 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001043 switch (params.fSurfaceProps.pixelGeometry()) {
1044 case kUnknown_SkPixelGeometry:
1045 title.append( " Flat");
1046 break;
1047 case kRGB_H_SkPixelGeometry:
1048 title.append( " RGB");
1049 break;
1050 case kBGR_H_SkPixelGeometry:
1051 title.append( " BGR");
1052 break;
1053 case kRGB_V_SkPixelGeometry:
1054 title.append( " RGBV");
1055 break;
1056 case kBGR_V_SkPixelGeometry:
1057 title.append( " BGRV");
1058 break;
1059 }
1060 }
1061
1062 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1063 title.append(" DFT");
1064 }
1065
csmartdalton578f0642017-02-24 16:04:47 -07001066 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001067 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001068 int msaa = fWindow->sampleCount();
1069 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001070 title.appendf(" MSAA: %i", msaa);
1071 }
1072 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001073
1074 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001075 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001076 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1077 }
1078
Brian Osman805a7272018-05-02 15:40:20 -04001079 if (kPerspective_Real == fPerspectiveMode) {
1080 title.append(" Perpsective (Real)");
1081 } else if (kPerspective_Fake == fPerspectiveMode) {
1082 title.append(" Perspective (Fake)");
1083 }
1084
brianosman05de2162016-05-06 13:28:57 -07001085 fWindow->setTitle(title.c_str());
1086}
1087
Florin Malitaab99c342018-01-16 16:23:03 -05001088int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001089
1090 if (!FLAGS_slide.isEmpty()) {
1091 int count = fSlides.count();
1092 for (int i = 0; i < count; i++) {
1093 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001094 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001095 }
1096 }
1097
1098 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1099 this->listNames();
1100 }
1101
Florin Malitaab99c342018-01-16 16:23:03 -05001102 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001103}
1104
Florin Malitaab99c342018-01-16 16:23:03 -05001105void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001106 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001107 for (const auto& slide : fSlides) {
1108 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001109 }
1110}
1111
Florin Malitaab99c342018-01-16 16:23:03 -05001112void Viewer::setCurrentSlide(int slide) {
1113 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001114
Florin Malitaab99c342018-01-16 16:23:03 -05001115 if (slide == fCurrentSlide) {
1116 return;
1117 }
1118
1119 if (fCurrentSlide >= 0) {
1120 fSlides[fCurrentSlide]->unload();
1121 }
1122
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001123 SkScalar scaleFactor = 1.0;
1124 if (fApplyBackingScale) {
1125 scaleFactor = fWindow->scaleFactor();
1126 }
1127 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1128 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001129 fCurrentSlide = slide;
1130 this->setupCurrentSlide();
1131}
1132
1133void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001134 if (fCurrentSlide >= 0) {
1135 // prepare dimensions for image slides
1136 fGesture.resetTouchState();
1137 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001138
Jim Van Verth0848fb02018-01-22 13:39:30 -05001139 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1140 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1141 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001142
Jim Van Verth0848fb02018-01-22 13:39:30 -05001143 // Start with a matrix that scales the slide to the available screen space
1144 if (fWindow->scaleContentToFit()) {
1145 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001146 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1147 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001148 }
liyuqiane46e4f02016-05-20 07:32:19 -07001149 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001150
1151 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001152 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001153
1154 this->updateTitle();
1155 this->updateUIState();
1156
1157 fStatsLayer.resetMeasurements();
1158
1159 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001160 }
jvanverthc265a922016-04-08 12:51:45 -07001161}
1162
Brian Osmanaba642c2020-02-06 12:52:25 -05001163#define MAX_ZOOM_LEVEL 8.0f
1164#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001165
jvanverth34524262016-05-04 13:49:13 -07001166void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001167 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001168 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001169 this->preTouchMatrixChanged();
1170}
Yuqian Li755778c2018-03-28 16:23:31 -04001171
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001172void Viewer::preTouchMatrixChanged() {
1173 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001174 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1175 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1176 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1177 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1178}
1179
Brian Osman805a7272018-05-02 15:40:20 -04001180SkMatrix Viewer::computePerspectiveMatrix() {
1181 SkScalar w = fWindow->width(), h = fWindow->height();
1182 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1183 SkPoint perspPts[4] = {
1184 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1185 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1186 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1187 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1188 };
1189 SkMatrix m;
1190 m.setPolyToPoly(orthoPts, perspPts, 4);
1191 return m;
1192}
1193
Yuqian Li755778c2018-03-28 16:23:31 -04001194SkMatrix Viewer::computePreTouchMatrix() {
1195 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001196
1197 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001198 if (fApplyBackingScale) {
1199 zoomScale *= fWindow->scaleFactor();
1200 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001201 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001202 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001203
1204 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1205 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001206
Brian Osman805a7272018-05-02 15:40:20 -04001207 if (kPerspective_Real == fPerspectiveMode) {
1208 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001209 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001210 }
1211
Yuqian Li755778c2018-03-28 16:23:31 -04001212 return m;
jvanverthc265a922016-04-08 12:51:45 -07001213}
1214
liyuqiand3cdbca2016-05-17 12:44:20 -07001215SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001216 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001217 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001218 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001219 return m;
jvanverthc265a922016-04-08 12:51:45 -07001220}
1221
Brian Osman621491e2017-02-28 15:45:01 -05001222void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001223 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001224 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001225 fBackendType = backendType;
1226
Robert Phillipse9229532020-06-26 10:10:49 -04001227 // The active context is going away in 'detach'
1228 for(auto& slide : fSlides) {
1229 slide->gpuTeardown();
1230 }
1231
Brian Osman621491e2017-02-28 15:45:01 -05001232 fWindow->detach();
1233
Brian Osman70d2f432017-11-08 09:54:10 -05001234#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001235 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1236 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001237 DisplayParams params = fWindow->getRequestedDisplayParams();
1238 delete fWindow;
1239 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001240
Brian Osman70d2f432017-11-08 09:54:10 -05001241 // re-register callbacks
1242 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001243 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001244 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001245 fWindow->pushLayer(&fImGuiLayer);
1246
Brian Osman70d2f432017-11-08 09:54:10 -05001247 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1248 // will still include our correct sample count. But the re-created fWindow will lose that
1249 // information. On Windows, we need to re-create the window when changing sample count,
1250 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1251 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1252 // as if we had never changed windows at all).
1253 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001254#endif
1255
Brian Osman70d2f432017-11-08 09:54:10 -05001256 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001257}
1258
Brian Osman92004802017-03-06 11:47:26 -05001259void Viewer::setColorMode(ColorMode colorMode) {
1260 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001261 this->updateTitle();
1262 fWindow->inval();
1263}
1264
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001265class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1266public:
Mike Reed3ae47332019-01-04 10:11:46 -05001267 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1268 SkFont* font, Viewer::SkFontFields* ffields)
1269 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001270 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001271 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1272 sk_sp<SkTextBlob>* cache) {
1273 bool blobWillChange = false;
1274 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001275 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1276 bool shouldDraw = this->filterFont(&filteredFont);
1277 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001278 blobWillChange = true;
1279 break;
1280 }
1281 }
1282 if (!blobWillChange) {
1283 return blob;
1284 }
1285
1286 SkTextBlobBuilder builder;
1287 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001288 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1289 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001290 if (!shouldDraw) {
1291 continue;
1292 }
1293
Mike Reed3ae47332019-01-04 10:11:46 -05001294 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001295
Ben Wagner41e40472018-09-24 13:01:54 -04001296 const SkTextBlobBuilder::RunBuffer& runBuffer
1297 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001298 ? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(),
1299 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001300 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001301 ? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(),
1302 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001303 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001304 ? builder.allocRunTextPos(font, it.glyphCount(), it.textSize())
Ben Wagnere5736262021-02-08 16:52:08 -05001305 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001306 ? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001307 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1308 uint32_t glyphCount = it.glyphCount();
1309 if (it.glyphs()) {
1310 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1311 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1312 }
1313 if (it.pos()) {
1314 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001315 unsigned posPerGlyph = it.scalarsPerGlyph();
1316 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001317 }
1318 if (it.text()) {
1319 size_t textSize = sizeof(decltype(*it.text()));
1320 uint32_t textCount = it.textSize();
1321 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1322 }
1323 if (it.clusters()) {
1324 size_t clusterSize = sizeof(decltype(*it.clusters()));
1325 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1326 }
1327 }
1328 *cache = builder.make();
1329 return cache->get();
1330 }
1331 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1332 const SkPaint& paint) override {
1333 sk_sp<SkTextBlob> cache;
1334 this->SkPaintFilterCanvas::onDrawTextBlob(
1335 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1336 }
Mike Reed3ae47332019-01-04 10:11:46 -05001337 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001338 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001339 font->writable()->setSize(fFont->getSize());
1340 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001341 if (fFontOverrides->fScaleX) {
1342 font->writable()->setScaleX(fFont->getScaleX());
1343 }
1344 if (fFontOverrides->fSkewX) {
1345 font->writable()->setSkewX(fFont->getSkewX());
1346 }
Mike Reed3ae47332019-01-04 10:11:46 -05001347 if (fFontOverrides->fHinting) {
1348 font->writable()->setHinting(fFont->getHinting());
1349 }
Ben Wagner9613e452019-01-23 10:34:59 -05001350 if (fFontOverrides->fEdging) {
1351 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001352 }
Ben Wagner9613e452019-01-23 10:34:59 -05001353 if (fFontOverrides->fEmbolden) {
1354 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001355 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001356 if (fFontOverrides->fBaselineSnap) {
1357 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1358 }
Ben Wagner9613e452019-01-23 10:34:59 -05001359 if (fFontOverrides->fLinearMetrics) {
1360 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001361 }
Ben Wagner9613e452019-01-23 10:34:59 -05001362 if (fFontOverrides->fSubpixel) {
1363 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001364 }
Ben Wagner9613e452019-01-23 10:34:59 -05001365 if (fFontOverrides->fEmbeddedBitmaps) {
1366 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001367 }
Ben Wagner9613e452019-01-23 10:34:59 -05001368 if (fFontOverrides->fForceAutoHinting) {
1369 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001370 }
Ben Wagner9613e452019-01-23 10:34:59 -05001371
Mike Reed3ae47332019-01-04 10:11:46 -05001372 return true;
1373 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001374 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001375 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001376 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001377 }
Ben Wagner9613e452019-01-23 10:34:59 -05001378 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001379 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001380 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001381 if (fPaintOverrides->fStyle) {
1382 paint.setStyle(fPaint->getStyle());
1383 }
1384 if (fPaintOverrides->fWidth) {
1385 paint.setStrokeWidth(fPaint->getStrokeWidth());
1386 }
1387 if (fPaintOverrides->fMiterLimit) {
1388 paint.setStrokeMiter(fPaint->getStrokeMiter());
1389 }
1390 if (fPaintOverrides->fCapType) {
1391 paint.setStrokeCap(fPaint->getStrokeCap());
1392 }
1393 if (fPaintOverrides->fJoinType) {
1394 paint.setStrokeJoin(fPaint->getStrokeJoin());
1395 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001396 return true;
1397 }
1398 SkPaint* fPaint;
1399 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001400 SkFont* fFont;
1401 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001402};
1403
Robert Phillips9882dae2019-03-04 11:00:10 -05001404void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001405 if (fCurrentSlide < 0) {
1406 return;
1407 }
1408
Robert Phillips9882dae2019-03-04 11:00:10 -05001409 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001410
Brian Osmanf750fbc2017-02-08 10:47:28 -05001411 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001412 SkSurface* slideSurface = surface;
1413 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001414 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001415
Brian Osmane0d4fba2017-03-15 10:24:55 -04001416 // 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 -05001417 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001418 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001419 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001420 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001421 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001422 }
1423
Brian Osman3ac99cf2017-12-01 11:23:53 -05001424 if (fSaveToSKP) {
1425 SkPictureRecorder recorder;
1426 SkCanvas* recorderCanvas = recorder.beginRecording(
1427 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001428 fSlides[fCurrentSlide]->draw(recorderCanvas);
1429 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1430 SkFILEWStream stream("sample_app.skp");
1431 picture->serialize(&stream);
1432 fSaveToSKP = false;
1433 }
1434
Brian Osmane9ed0f02018-11-26 14:50:05 -05001435 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001436 SkColorType colorType;
1437 switch (fColorMode) {
1438 case ColorMode::kLegacy:
1439 case ColorMode::kColorManaged8888:
1440 colorType = kN32_SkColorType;
1441 break;
1442 case ColorMode::kColorManagedF16:
1443 colorType = kRGBA_F16_SkColorType;
1444 break;
1445 case ColorMode::kColorManagedF16Norm:
1446 colorType = kRGBA_F16Norm_SkColorType;
1447 break;
1448 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001449
1450 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001451 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001452 slideCanvas->getProps(&props);
1453
Brian Osmane9ed0f02018-11-26 14:50:05 -05001454 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1455 return Window::kRaster_BackendType == this->fBackendType
1456 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001457 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001458 };
1459
Brian Osman03115dc2018-11-26 13:55:19 -05001460 // We need to render offscreen if we're...
1461 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1462 // ... in any raster mode, because the window surface is actually GL
1463 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001464 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001465 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001466 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001467 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001468 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001469 colorSpace != nullptr ||
1470 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001471
Brian Osmane9ed0f02018-11-26 14:50:05 -05001472 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001473 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001474 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001475 }
1476
Mike Reed59295352020-03-12 13:56:34 -04001477 SkPictureRecorder recorder;
1478 SkCanvas* recorderRestoreCanvas = nullptr;
1479 if (fDrawViaSerialize) {
1480 recorderRestoreCanvas = slideCanvas;
1481 slideCanvas = recorder.beginRecording(
1482 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1483 }
1484
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001485 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001486 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001487 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001488 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001489 if (fTiled) {
1490 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1491 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001492 for (int y = 0; y < fWindow->height(); y += tileH) {
1493 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001494 SkAutoCanvasRestore acr(slideCanvas, true);
1495 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1496 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001497 }
1498 }
1499
1500 // Draw borders between tiles
1501 if (fDrawTileBoundaries) {
1502 SkPaint border;
1503 border.setColor(0x60FF00FF);
1504 border.setStyle(SkPaint::kStroke_Style);
1505 for (int y = 0; y < fWindow->height(); y += tileH) {
1506 for (int x = 0; x < fWindow->width(); x += tileW) {
1507 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1508 }
1509 }
1510 }
1511 } else {
1512 slideCanvas->concat(this->computeMatrix());
1513 if (kPerspective_Real == fPerspectiveMode) {
1514 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1515 }
Mike Reed3ae47332019-01-04 10:11:46 -05001516 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001517 fSlides[fCurrentSlide]->draw(&filterCanvas);
1518 }
Brian Osman56a24812017-12-19 11:15:16 -05001519 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001520 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001521
Mike Reed59295352020-03-12 13:56:34 -04001522 if (recorderRestoreCanvas) {
1523 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1524 auto data = picture->serialize();
1525 slideCanvas = recorderRestoreCanvas;
1526 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1527 }
1528
Brian Osman1df161a2017-02-09 12:10:20 -05001529 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001530 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001531 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001532 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001533
1534 // If we rendered offscreen, snap an image and push the results to the window's canvas
1535 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001536 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001537
Robert Phillips9882dae2019-03-04 11:00:10 -05001538 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001539 SkPaint paint;
1540 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001541 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001542 int prePerspectiveCount = canvas->save();
1543 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001544 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001545 canvas->clear(SK_ColorWHITE);
1546 canvas->concat(this->computePerspectiveMatrix());
1547 }
Mike Reedb339d052021-01-28 11:20:41 -05001548 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001549 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001550 }
Mike Reed376d8122019-03-14 11:39:02 -04001551
1552 if (fShowSlideDimensions) {
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001553 SkCanvas* canvas = surface->getCanvas();
1554 SkAutoCanvasRestore acr(canvas, true);
1555 canvas->concat(this->computeMatrix());
Mike Reed376d8122019-03-14 11:39:02 -04001556 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1557 SkPaint paint;
1558 paint.setColor(0x40FFFF00);
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001559 canvas->drawRect(r, paint);
Mike Reed376d8122019-03-14 11:39:02 -04001560 }
liyuqian6f163d22016-06-13 12:26:45 -07001561}
1562
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001563void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001564 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001565 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001566}
Jim Van Verth6f449692017-02-14 15:16:46 -05001567
Robert Phillips9882dae2019-03-04 11:00:10 -05001568void Viewer::onPaint(SkSurface* surface) {
1569 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001570
Robert Phillips9882dae2019-03-04 11:00:10 -05001571 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001572
Brian Osmand67e5182017-12-08 16:46:09 -05001573 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001574
Greg Daniel427d8eb2020-09-28 15:04:18 -04001575 fLastImage.reset();
1576
Robert Phillipsed653392020-07-10 13:55:21 -04001577 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001578 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001579 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001580 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001581}
1582
Ben Wagnera1915972018-08-09 15:06:19 -04001583void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001584 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001585 SkScalar scaleFactor = 1.0;
1586 if (fApplyBackingScale) {
1587 scaleFactor = fWindow->scaleFactor();
1588 }
1589 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001590 }
Ben Wagnera1915972018-08-09 15:06:19 -04001591}
1592
Florin Malitacefc1b92018-02-19 21:43:47 -05001593SkPoint Viewer::mapEvent(float x, float y) {
1594 const auto m = this->computeMatrix();
1595 SkMatrix inv;
1596
1597 SkAssertResult(m.invert(&inv));
1598
1599 return inv.mapXY(x, y);
1600}
1601
Hal Canaryb1f411a2019-08-29 10:39:22 -04001602bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001603 if (GestureDevice::kMouse == fGestureDevice) {
1604 return false;
1605 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001606
1607 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001608 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001609 fWindow->inval();
1610 return true;
1611 }
1612
liyuqiand3cdbca2016-05-17 12:44:20 -07001613 void* castedOwner = reinterpret_cast<void*>(owner);
1614 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001615 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001616 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001617#if defined(SK_BUILD_FOR_IOS)
1618 // TODO: move IOS swipe detection higher up into the platform code
1619 SkPoint dir;
1620 if (fGesture.isFling(&dir)) {
1621 // swiping left or right
1622 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1623 if (dir.fX < 0) {
1624 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1625 fCurrentSlide + 1 : 0);
1626 } else {
1627 this->setCurrentSlide(fCurrentSlide > 0 ?
1628 fCurrentSlide - 1 : fSlides.count() - 1);
1629 }
1630 }
1631 fGesture.reset();
1632 }
1633#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001634 break;
1635 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001636 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001637 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001638 break;
1639 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001640 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001641 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001642 break;
1643 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001644 default: {
1645 // kLeft and kRight are only for swipes
1646 SkASSERT(false);
1647 break;
1648 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001649 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001650 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001651 fWindow->inval();
1652 return true;
1653}
1654
Hal Canaryb1f411a2019-08-29 10:39:22 -04001655bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001656 if (GestureDevice::kTouch == fGestureDevice) {
1657 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001658 }
Brian Osman16c81a12017-12-20 11:58:34 -05001659
Florin Malitacefc1b92018-02-19 21:43:47 -05001660 const auto slidePt = this->mapEvent(x, y);
1661 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1662 fWindow->inval();
1663 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001664 }
1665
1666 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001667 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001668 fGesture.touchEnd(nullptr);
1669 break;
1670 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001671 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001672 fGesture.touchBegin(nullptr, x, y);
1673 break;
1674 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001675 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001676 fGesture.touchMoved(nullptr, x, y);
1677 break;
1678 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001679 default: {
1680 SkASSERT(false); // shouldn't see kRight or kLeft here
1681 break;
1682 }
Brian Osman16c81a12017-12-20 11:58:34 -05001683 }
1684 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1685
Hal Canaryb1f411a2019-08-29 10:39:22 -04001686 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001687 fWindow->inval();
1688 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001689 return true;
1690}
1691
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001692bool Viewer::onFling(skui::InputState state) {
1693 if (skui::InputState::kRight == state) {
1694 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1695 return true;
1696 } else if (skui::InputState::kLeft == state) {
1697 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1698 return true;
1699 }
1700 return false;
1701}
1702
1703bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1704 switch (state) {
1705 case skui::InputState::kDown:
1706 fGesture.startZoom();
1707 return true;
1708 break;
1709 case skui::InputState::kMove:
1710 fGesture.updateZoom(scale, x, y, x, y);
1711 return true;
1712 break;
1713 case skui::InputState::kUp:
1714 fGesture.endZoom();
1715 return true;
1716 break;
1717 default:
1718 SkASSERT(false);
1719 break;
1720 }
1721
1722 return false;
1723}
1724
Brian Osmana109e392017-02-24 09:49:14 -05001725static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001726 // The gamut image covers a (0.8 x 0.9) shaped region
1727 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001728
1729 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1730 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1731 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001732 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1733 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1734 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001735
Brian Osman535c5e32019-02-09 16:32:58 -05001736 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1737 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1738 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1739 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1740 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001741}
1742
Ben Wagner3627d2e2018-06-26 14:23:20 -04001743static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001744 ImGui::DragCanvas dc(pt);
1745 dc.fillColor(IM_COL32(0, 0, 0, 128));
1746 dc.dragPoint(pt);
1747 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001748}
1749
Brian Osman9bb47cf2018-04-26 15:55:00 -04001750static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001751 ImGui::DragCanvas dc(pts);
1752 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001753
Brian Osman535c5e32019-02-09 16:32:58 -05001754 for (int i = 0; i < 4; ++i) {
1755 dc.dragPoint(pts + i);
1756 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001757
Brian Osman535c5e32019-02-09 16:32:58 -05001758 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1759 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1760 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1761 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001762
Brian Osman535c5e32019-02-09 16:32:58 -05001763 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001764}
1765
John Stiles38b7d2f2020-06-24 12:13:31 -04001766static SkSL::String build_sksl_highlight_shader() {
1767 return SkSL::String("out half4 sk_FragColor;\n"
1768 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1769}
1770
1771static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1772 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1773 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1774 size_t pos = inShader.rfind("return *_out;\n");
1775 if (pos == std::string::npos) {
1776 return inShader;
1777 }
1778
1779 SkSL::String replacementShader = inShader;
1780 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1781 return replacementShader;
1782}
1783
1784static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1785 const char* versionDecl = shaderCaps.versionDeclString();
1786 SkSL::String highlight = versionDecl ? versionDecl : "";
1787 if (shaderCaps.usesPrecisionModifiers()) {
1788 highlight.append("precision mediump float;\n");
1789 }
1790 highlight.appendf("out vec4 sk_FragColor;\n"
1791 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1792 return highlight;
1793}
1794
Brian Osmand67e5182017-12-08 16:46:09 -05001795void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001796 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1797 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001798 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001799 }
1800
1801 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001802 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1803 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001804 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001805 DisplayParams params = fWindow->getRequestedDisplayParams();
1806 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001807 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001808
Brian Osmana109e392017-02-24 09:49:14 -05001809 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1810 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001811 if (ImGui::CollapsingHeader("Backend")) {
1812 int newBackend = static_cast<int>(fBackendType);
1813 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1814 ImGui::SameLine();
1815 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001816#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1817 ImGui::SameLine();
1818 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1819#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001820#if defined(SK_DAWN)
1821 ImGui::SameLine();
1822 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1823#endif
John Stilesf7da9232020-11-19 19:58:14 -05001824#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001825 ImGui::SameLine();
1826 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1827#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001828#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001829 ImGui::SameLine();
1830 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1831#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001832#if defined(SK_DIRECT3D)
1833 ImGui::SameLine();
1834 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1835#endif
Brian Osman621491e2017-02-28 15:45:01 -05001836 if (newBackend != fBackendType) {
1837 fDeferredActions.push_back([=]() {
1838 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1839 });
1840 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001841
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001842 bool* wire = &params.fGrContextOptions.fWireframeMode;
1843 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1844 paramsChanged = true;
1845 }
Brian Salomon99a33902017-03-07 15:16:34 -05001846
Brian Salomon91216d52021-04-09 11:57:59 -04001847 bool* reducedShaders = &params.fGrContextOptions.fReducedShaderVariations;
1848 if (ctx && ImGui::Checkbox("Reduced shaders", reducedShaders)) {
1849 paramsChanged = true;
1850 }
1851
Brian Osman28b12522017-03-08 17:10:24 -05001852 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001853 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001854 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001855 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1856 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1857 1;
1858
1859 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1860 if (maxMSAA >= 4) {
1861 ImGui::Text("MSAA: ");
1862
1863 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1864 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1865 // don't show it in the list.
1866 if (curMSAA == 2) {
1867 continue;
1868 }
1869 ImGui::SameLine();
1870 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1871 &sampleCount, curMSAA);
1872 }
1873 }
Brian Osman28b12522017-03-08 17:10:24 -05001874
1875 if (sampleCount != params.fMSAASampleCount) {
1876 params.fMSAASampleCount = sampleCount;
1877 paramsChanged = true;
1878 }
1879 }
1880
Ben Wagner37c54032018-04-13 14:30:23 -04001881 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001882 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001883 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1884 }
1885 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1886 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1887 {
1888 uint32_t flags = params.fSurfaceProps.flags();
1889 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001890 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1891 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1892 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001893 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001894 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001895 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1896 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1897 }
1898 paramsChanged = true;
1899 }
1900
1901 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1902 if (ImGui::Checkbox("DFT", &useDFT)) {
1903 uint32_t flags = params.fSurfaceProps.flags();
1904 if (useDFT) {
1905 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1906 } else {
1907 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1908 }
1909 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1910 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1911 paramsChanged = true;
1912 }
1913
Brian Osman8a9de3d2017-03-01 14:59:05 -05001914 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001915 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001916 auto prButton = [&](GpuPathRenderers x) {
1917 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001918 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1919 params.fGrContextOptions.fGpuPathRenderers = x;
1920 paramsChanged = true;
1921 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001922 }
1923 };
1924
1925 if (!ctx) {
1926 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001927 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001928 const auto* caps = ctx->priv().caps();
1929 prButton(GpuPathRenderers::kDefault);
1930 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001931 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001932 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001933 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001934 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001935 if (1 == fWindow->sampleCount()) {
1936 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1937 prButton(GpuPathRenderers::kCoverageCounting);
1938 }
1939 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());
2763 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
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 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2771 writer.appendString(
2772 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2773 }
2774 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2775 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002776 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002777 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002778 }
2779 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002780
liyuqianb73c24b2016-06-03 08:47:23 -07002781 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002782 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2783 [this](SkJSONWriter& writer) {
2784 writer.appendString(kSoftkeyHint);
2785 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2786 writer.appendString(softkey.c_str());
2787 }
2788 });
liyuqianb73c24b2016-06-03 08:47:23 -07002789
Florin Malitab632df72018-06-18 21:23:06 -04002790 writer.endArray();
2791 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002792
Florin Malitab632df72018-06-18 21:23:06 -04002793 auto data = memStream.detachAsData();
2794
2795 // TODO: would be cool to avoid this copy
2796 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2797
2798 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002799}
2800
2801void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002802 // For those who will add more features to handle the state change in this function:
2803 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2804 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2805 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002806 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002807 for (int i = 0; i < fSlides.count(); ++i) {
2808 if (fSlides[i]->getName().equals(stateValue)) {
2809 this->setCurrentSlide(i);
2810 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002811 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002812 }
Florin Malitaab99c342018-01-16 16:23:03 -05002813
2814 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002815 } else if (stateName.equals(kBackendStateName)) {
2816 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2817 if (stateValue.equals(kBackendTypeStrings[i])) {
2818 if (fBackendType != i) {
2819 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002820 for(auto& slide : fSlides) {
2821 slide->gpuTeardown();
2822 }
liyuqian6cb70252016-06-02 12:16:25 -07002823 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002824 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002825 }
2826 break;
2827 }
2828 }
csmartdalton578f0642017-02-24 16:04:47 -07002829 } else if (stateName.equals(kMSAAStateName)) {
2830 DisplayParams params = fWindow->getRequestedDisplayParams();
2831 int sampleCount = atoi(stateValue.c_str());
2832 if (sampleCount != params.fMSAASampleCount) {
2833 params.fMSAASampleCount = sampleCount;
2834 fWindow->setRequestedDisplayParams(params);
2835 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002836 this->updateTitle();
2837 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002838 }
2839 } else if (stateName.equals(kPathRendererStateName)) {
2840 DisplayParams params = fWindow->getRequestedDisplayParams();
2841 for (const auto& pair : gPathRendererNames) {
2842 if (pair.second == stateValue.c_str()) {
2843 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2844 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2845 fWindow->setRequestedDisplayParams(params);
2846 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002847 this->updateTitle();
2848 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002849 }
2850 break;
2851 }
csmartdalton578f0642017-02-24 16:04:47 -07002852 }
liyuqianb73c24b2016-06-03 08:47:23 -07002853 } else if (stateName.equals(kSoftkeyStateName)) {
2854 if (!stateValue.equals(kSoftkeyHint)) {
2855 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002856 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002857 }
liyuqian2edb0f42016-07-06 14:11:32 -07002858 } else if (stateName.equals(kRefreshStateName)) {
2859 // This state is actually NOT in the UI state.
2860 // We use this to allow Android to quickly set bool fRefresh.
2861 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002862 } else {
2863 SkDebugf("Unknown stateName: %s", stateName.c_str());
2864 }
2865}
Brian Osman79086b92017-02-10 13:36:16 -05002866
Hal Canaryb1f411a2019-08-29 10:39:22 -04002867bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002868 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002869}
2870
Hal Canaryb1f411a2019-08-29 10:39:22 -04002871bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002872 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002873 fWindow->inval();
2874 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002875 } else {
2876 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002877 }
Brian Osman79086b92017-02-10 13:36:16 -05002878}