blob: d26eda27d06f744c8d1b71a3b1b81d871d432eeb [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkGraphics.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkSurface.h"
Robert Phillipsed653392020-07-10 13:55:21 -040014#include "include/gpu/GrDirectContext.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050015#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkTo.h"
17#include "include/utils/SkPaintFilterCanvas.h"
18#include "src/core/SkColorSpacePriv.h"
19#include "src/core/SkImagePriv.h"
20#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkOSFile.h"
22#include "src/core/SkScan.h"
John Stilesdf078002020-07-14 09:44:57 -040023#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040025#include "src/core/SkTextBlobPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040026#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrGpu.h"
28#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070029#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Chris Daltonff18ff62020-12-07 17:39:26 -070031#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Adlai Hollerbcfc5542020-08-27 12:44:07 -040032#include "src/image/SkImage_Base.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/utils/SkJSONWriter.h"
34#include "src/utils/SkOSPath.h"
35#include "tools/Resources.h"
36#include "tools/ToolUtils.h"
37#include "tools/flags/CommandLineFlags.h"
38#include "tools/flags/CommonFlags.h"
39#include "tools/trace/EventTracingPriv.h"
40#include "tools/viewer/BisectSlide.h"
41#include "tools/viewer/GMSlide.h"
42#include "tools/viewer/ImageSlide.h"
43#include "tools/viewer/ParticlesSlide.h"
44#include "tools/viewer/SKPSlide.h"
45#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050046#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "tools/viewer/SlideDir.h"
48#include "tools/viewer/SvgSlide.h"
49#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070050
Chris Dalton17dc4182020-03-25 16:18:16 -060051#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040052#include <map>
53
Hal Canary8a001442018-09-19 11:31:27 -040054#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040055#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040056
Brian Osmanc85f1fa2020-06-16 15:11:34 -040057#ifdef SK_VULKAN
58#include "spirv-tools/libspirv.hpp"
59#endif
60
Florin Malita87ccf332018-05-04 12:23:24 -040061#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050062 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040063#endif
Florin Malita45cd2002020-06-09 14:00:54 -040064#if defined(SK_ENABLE_SKRIVE)
65 #include "tools/viewer/SkRiveSlide.h"
66#endif
Florin Malita87ccf332018-05-04 12:23:24 -040067
John Stiles8ef4d6c2021-03-05 16:01:45 -050068namespace SkSL {
John Stiles7247b482021-03-08 10:40:35 -050069extern bool gSkSLOptimizer;
70extern bool gSkSLInliner;
John Stiles8ef4d6c2021-03-05 16:01:45 -050071}
72
Brian Osman5e7fbfd2019-05-03 13:13:35 -040073class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
74public:
75 void compileError(const char* shader, const char* errors) override {
76 fShaders.push_back(SkString(shader));
77 fErrors.push_back(SkString(errors));
78 }
79
80 void reset() {
81 fShaders.reset();
82 fErrors.reset();
83 }
84
85 SkTArray<SkString> fShaders;
86 SkTArray<SkString> fErrors;
87};
88
89static CapturingShaderErrorHandler gShaderErrorHandler;
90
Brian Osmanf847f312020-06-18 14:18:27 -040091GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
92
jvanverth34524262016-05-04 13:49:13 -070093using namespace sk_app;
94
csmartdalton61cd31a2017-02-27 17:00:53 -070095static std::map<GpuPathRenderers, std::string> gPathRendererNames;
96
jvanverth9f372462016-04-06 06:08:59 -070097Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070098 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070099}
100
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600101static DEFINE_string(slide, "", "Start on this sample.");
102static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -0500103
Jim Van Verth682a2f42020-05-13 16:54:09 -0400104#ifdef SK_GL
105#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700106#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400107#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700108#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400109#ifdef SK_VULKAN
110#define VK_BACKEND_STR ", \"vk\""
111#else
112#define VK_BACKEND_STR
113#endif
114#ifdef SK_METAL
115#define MTL_BACKEND_STR ", \"mtl\""
116#else
117#define MTL_BACKEND_STR
118#endif
119#ifdef SK_DIRECT3D
120#define D3D_BACKEND_STR ", \"d3d\""
121#else
122#define D3D_BACKEND_STR
123#endif
124#ifdef SK_DAWN
125#define DAWN_BACKEND_STR ", \"dawn\""
126#else
127#define DAWN_BACKEND_STR
128#endif
129#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
130#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
131 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700132
Brian Osman2dd96932016-10-18 15:33:53 -0400133static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700134
Mike Klein5b3f3432019-03-21 11:42:21 -0500135static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -0700136
Mike Klein84836b72019-03-21 11:31:36 -0500137static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700138
Mike Klein84836b72019-03-21 11:31:36 -0500139static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400140
Mike Kleinc6142d82019-03-25 10:54:59 -0500141static DEFINE_string2(match, m, nullptr,
142 "[~][^]substring[$] [...] of name to run.\n"
143 "Multiple matches may be separated by spaces.\n"
144 "~ causes a matching name to always be skipped\n"
145 "^ requires the start of the name to match\n"
146 "$ requires the end of the name to match\n"
147 "^ and $ requires an exact match\n"
148 "If a name does not match any list entry,\n"
149 "it is skipped unless some list entry starts with ~");
150
Mike Klein19fb3972019-03-21 13:08:08 -0500151#if defined(SK_BUILD_FOR_ANDROID)
152 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500153 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
154 static DEFINE_string(lotties, "/data/local/tmp/lotties",
155 "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400156 static DEFINE_string(rives, "/data/local/tmp/rives",
157 "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500158#else
159 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500160 static DEFINE_string(skps, "skps", "Directory to read skps from.");
161 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400162 static DEFINE_string(rives, "rives", "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500163#endif
164
Mike Kleinc6142d82019-03-25 10:54:59 -0500165static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
166
167static DEFINE_int_2(threads, j, -1,
168 "Run threadsafe tests on a threadpool with this many extra threads, "
169 "defaulting to one extra thread per core.");
170
Jim Van Verth7b558182019-11-14 16:47:01 -0500171static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
172
Chris Daltonc8877332020-01-06 09:48:30 -0700173static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500174static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
175static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500176static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500177static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500178static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500179
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400180#ifndef SK_GL
181static_assert(false, "viewer requires GL backend for raster.")
182#endif
183
Brian Salomon194db172017-08-17 14:37:06 -0400184const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700185 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400186#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
187 "ANGLE",
188#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400189#ifdef SK_DAWN
190 "Dawn",
191#endif
jvanverth063ece72016-06-17 09:29:14 -0700192#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700193 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700194#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400195#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500196 "Metal",
197#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400198#ifdef SK_DIRECT3D
199 "Direct3D",
200#endif
csmartdalton578f0642017-02-24 16:04:47 -0700201 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700202};
203
bsalomon6c471f72016-07-26 12:56:32 -0700204static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400205#ifdef SK_DAWN
206 if (0 == strcmp(str, "dawn")) {
207 return sk_app::Window::kDawn_BackendType;
208 } else
209#endif
bsalomon6c471f72016-07-26 12:56:32 -0700210#ifdef SK_VULKAN
211 if (0 == strcmp(str, "vk")) {
212 return sk_app::Window::kVulkan_BackendType;
213 } else
214#endif
Brian Salomon194db172017-08-17 14:37:06 -0400215#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
216 if (0 == strcmp(str, "angle")) {
217 return sk_app::Window::kANGLE_BackendType;
218 } else
219#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400220#ifdef SK_METAL
221 if (0 == strcmp(str, "mtl")) {
222 return sk_app::Window::kMetal_BackendType;
223 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500224#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400225#ifdef SK_DIRECT3D
226 if (0 == strcmp(str, "d3d")) {
227 return sk_app::Window::kDirect3D_BackendType;
228 } else
229#endif
230
bsalomon6c471f72016-07-26 12:56:32 -0700231 if (0 == strcmp(str, "gl")) {
232 return sk_app::Window::kNativeGL_BackendType;
233 } else if (0 == strcmp(str, "sw")) {
234 return sk_app::Window::kRaster_BackendType;
235 } else {
236 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
237 return sk_app::Window::kRaster_BackendType;
238 }
239}
240
Brian Osmana109e392017-02-24 09:49:14 -0500241static SkColorSpacePrimaries gSrgbPrimaries = {
242 0.64f, 0.33f,
243 0.30f, 0.60f,
244 0.15f, 0.06f,
245 0.3127f, 0.3290f };
246
247static SkColorSpacePrimaries gAdobePrimaries = {
248 0.64f, 0.33f,
249 0.21f, 0.71f,
250 0.15f, 0.06f,
251 0.3127f, 0.3290f };
252
253static SkColorSpacePrimaries gP3Primaries = {
254 0.680f, 0.320f,
255 0.265f, 0.690f,
256 0.150f, 0.060f,
257 0.3127f, 0.3290f };
258
259static SkColorSpacePrimaries gRec2020Primaries = {
260 0.708f, 0.292f,
261 0.170f, 0.797f,
262 0.131f, 0.046f,
263 0.3127f, 0.3290f };
264
265struct NamedPrimaries {
266 const char* fName;
267 SkColorSpacePrimaries* fPrimaries;
268} gNamedPrimaries[] = {
269 { "sRGB", &gSrgbPrimaries },
270 { "AdobeRGB", &gAdobePrimaries },
271 { "P3", &gP3Primaries },
272 { "Rec. 2020", &gRec2020Primaries },
273};
274
275static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
276 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
277}
278
Brian Osman70d2f432017-11-08 09:54:10 -0500279static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
280 // In raster mode, we still use GL for the window.
281 // This lets us render the GUI faster (and correct).
282 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
283}
284
Jim Van Verth74826c82019-03-01 14:37:30 -0500285class NullSlide : public Slide {
286 SkISize getDimensions() const override {
287 return SkISize::Make(640, 480);
288 }
289
290 void draw(SkCanvas* canvas) override {
291 canvas->clear(0xffff11ff);
292 }
293};
294
John Stiles31964fd2020-05-05 16:05:47 -0400295static const char kName[] = "name";
296static const char kValue[] = "value";
297static const char kOptions[] = "options";
298static const char kSlideStateName[] = "Slide";
299static const char kBackendStateName[] = "Backend";
300static const char kMSAAStateName[] = "MSAA";
301static const char kPathRendererStateName[] = "Path renderer";
302static const char kSoftkeyStateName[] = "Softkey";
303static const char kSoftkeyHint[] = "Please select a softkey";
304static const char kON[] = "ON";
305static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700306
Mike Reed862818b2020-03-21 15:07:13 -0400307extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500308extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500309extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400310
jvanverth34524262016-05-04 13:49:13 -0700311Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500312 : fCurrentSlide(-1)
313 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500314 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400315 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500316 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500317 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500318 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500319 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400320 , fZoomWindowFixed(false)
321 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500322 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400323 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700324 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500325 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500326 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500327 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500328 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500329 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700330 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400331 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400332 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400333 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500334 , fTiled(false)
335 , fDrawTileBoundaries(false)
336 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400337 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700338{
Greg Daniel285db442016-10-14 09:12:53 -0400339 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700340
Chris Dalton37ae4b02019-12-28 14:51:11 -0700341 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600342 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500343 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
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 =
378 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400379 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
380 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700381 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400382 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500383 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700384
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500385 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500386 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500387
Brian Osman56a24812017-12-19 11:15:16 -0500388 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500389 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500390 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
391 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
392 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
393
jvanverth9f372462016-04-06 06:08:59 -0700394 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700395 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500396 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500397 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500398 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700399
brianosman622c8d52016-05-10 06:50:49 -0700400 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500401 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
402 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
403 fWindow->inval();
404 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500405 // Command to jump directly to the slide picker and give it focus
406 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
407 this->fShowImGuiDebugWindow = true;
408 this->fShowSlidePicker = true;
409 fWindow->inval();
410 });
411 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400412 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500413 this->fShowImGuiDebugWindow = true;
414 this->fShowSlidePicker = true;
415 fWindow->inval();
416 });
Brian Osman79086b92017-02-10 13:36:16 -0500417 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
418 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
419 fWindow->inval();
420 });
Brian Osmanf6877092017-02-13 09:39:57 -0500421 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
422 this->fShowZoomWindow = !this->fShowZoomWindow;
423 fWindow->inval();
424 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400425 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
426 this->fZoomWindowFixed = !this->fZoomWindowFixed;
427 fWindow->inval();
428 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400429 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400430 DisplayParams params = fWindow->getRequestedDisplayParams();
431 params.fDisableVsync = !params.fDisableVsync;
432 fWindow->setRequestedDisplayParams(params);
433 this->updateTitle();
434 fWindow->inval();
435 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400436 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
437 DisplayParams params = fWindow->getRequestedDisplayParams();
438 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
439 fWindow->setRequestedDisplayParams(params);
440 this->updateTitle();
441 fWindow->inval();
442 });
Mike Reedf702ed42019-07-22 17:00:49 -0400443 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
444 fRefresh = !fRefresh;
445 fWindow->inval();
446 });
brianosman622c8d52016-05-10 06:50:49 -0700447 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500448 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700449 fWindow->inval();
450 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400451 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500452 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400453 this->updateTitle();
454 fWindow->inval();
455 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500456 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500457 switch (fColorMode) {
458 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500459 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500460 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500461 case ColorMode::kColorManaged8888:
462 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500463 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500464 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400465 this->setColorMode(ColorMode::kColorManagedF16Norm);
466 break;
467 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500468 this->setColorMode(ColorMode::kLegacy);
469 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500470 }
brianosman622c8d52016-05-10 06:50:49 -0700471 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700472 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
473 DisplayParams params = fWindow->getRequestedDisplayParams();
474 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
475 fWindow->setRequestedDisplayParams(params);
476 fWindow->inval();
477 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400478 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500479 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700480 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400481 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500482 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700483 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400484 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700485 this->changeZoomLevel(1.f / 32.f);
486 fWindow->inval();
487 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400488 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700489 this->changeZoomLevel(-1.f / 32.f);
490 fWindow->inval();
491 });
jvanverthaf236b52016-05-20 06:01:06 -0700492 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400493 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
494 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500495 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400496#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
497 if (newBackend == sk_app::Window::kVulkan_BackendType) {
498 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
499 sk_app::Window::kBackendTypeCount);
500 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
501 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500502 }
503#endif
Brian Osman621491e2017-02-28 15:45:01 -0500504 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700505 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500506 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
507 fSaveToSKP = true;
508 fWindow->inval();
509 });
Mike Reed376d8122019-03-14 11:39:02 -0400510 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
511 fShowSlideDimensions = !fShowSlideDimensions;
512 fWindow->inval();
513 });
Ben Wagner37c54032018-04-13 14:30:23 -0400514 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
515 DisplayParams params = fWindow->getRequestedDisplayParams();
516 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400517 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
518 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
519 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400520 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
521 } else {
522 switch (params.fSurfaceProps.pixelGeometry()) {
523 case kUnknown_SkPixelGeometry:
524 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
525 break;
526 case kRGB_H_SkPixelGeometry:
527 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
528 break;
529 case kBGR_H_SkPixelGeometry:
530 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
531 break;
532 case kRGB_V_SkPixelGeometry:
533 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
534 break;
535 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400536 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
537 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400538 break;
539 }
540 }
541 fWindow->setRequestedDisplayParams(params);
542 this->updateTitle();
543 fWindow->inval();
544 });
Ben Wagner9613e452019-01-23 10:34:59 -0500545 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500546 if (!fFontOverrides.fHinting) {
547 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400548 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500549 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500550 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400551 case SkFontHinting::kNone:
552 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500553 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400554 case SkFontHinting::kSlight:
555 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500556 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400557 case SkFontHinting::kNormal:
558 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500559 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400560 case SkFontHinting::kFull:
561 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500562 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500563 break;
564 }
565 }
566 this->updateTitle();
567 fWindow->inval();
568 });
569 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500570 if (!fPaintOverrides.fAntiAlias) {
571 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
572 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500573 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500574 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500575 } else {
576 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500577 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500578 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500579 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400580 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500581 break;
582 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500583 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500584 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400585 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500586 break;
587 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500588 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400589 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500590 break;
591 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500592 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
593 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500594 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
595 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500596 break;
597 }
598 }
599 this->updateTitle();
600 fWindow->inval();
601 });
Ben Wagner37c54032018-04-13 14:30:23 -0400602 fCommands.addCommand('D', "Modes", "DFT", [this]() {
603 DisplayParams params = fWindow->getRequestedDisplayParams();
604 uint32_t flags = params.fSurfaceProps.flags();
605 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
606 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
607 fWindow->setRequestedDisplayParams(params);
608 this->updateTitle();
609 fWindow->inval();
610 });
Ben Wagner9613e452019-01-23 10:34:59 -0500611 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500612 if (!fFontOverrides.fEdging) {
613 fFontOverrides.fEdging = true;
614 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500615 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500616 switch (fFont.getEdging()) {
617 case SkFont::Edging::kAlias:
618 fFont.setEdging(SkFont::Edging::kAntiAlias);
619 break;
620 case SkFont::Edging::kAntiAlias:
621 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
622 break;
623 case SkFont::Edging::kSubpixelAntiAlias:
624 fFont.setEdging(SkFont::Edging::kAlias);
625 fFontOverrides.fEdging = false;
626 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500627 }
628 }
629 this->updateTitle();
630 fWindow->inval();
631 });
Ben Wagner9613e452019-01-23 10:34:59 -0500632 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500633 if (!fFontOverrides.fSubpixel) {
634 fFontOverrides.fSubpixel = true;
635 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500636 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500637 if (!fFont.isSubpixel()) {
638 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500639 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500640 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500641 }
642 }
643 this->updateTitle();
644 fWindow->inval();
645 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400646 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
647 if (!fFontOverrides.fBaselineSnap) {
648 fFontOverrides.fBaselineSnap = true;
649 fFont.setBaselineSnap(false);
650 } else {
651 if (!fFont.isBaselineSnap()) {
652 fFont.setBaselineSnap(true);
653 } else {
654 fFontOverrides.fBaselineSnap = false;
655 }
656 }
657 this->updateTitle();
658 fWindow->inval();
659 });
Brian Osman805a7272018-05-02 15:40:20 -0400660 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
661 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
662 : kPerspective_Real;
663 this->updateTitle();
664 fWindow->inval();
665 });
666 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
667 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
668 : kPerspective_Off;
669 this->updateTitle();
670 fWindow->inval();
671 });
Brian Osman207d4102019-01-10 09:40:58 -0500672 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
673 fAnimTimer.togglePauseResume();
674 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400675 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
676 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500677 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400678 fWindow->inval();
679 });
Mike Reed59295352020-03-12 13:56:34 -0400680 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
681 fDrawViaSerialize = !fDrawViaSerialize;
682 this->updateTitle();
683 fWindow->inval();
684 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500685 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400686 gUseSkVMBlitter = !gUseSkVMBlitter;
687 this->updateTitle();
688 fWindow->inval();
689 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500690 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
691 gSkVMAllowJIT = !gSkVMAllowJIT;
692 this->updateTitle();
693 fWindow->inval();
694 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500695
jvanverth2bb3b6d2016-04-08 07:24:09 -0700696 // set up slides
697 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500698 if (FLAGS_list) {
699 this->listNames();
700 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700701
Brian Osman9bb47cf2018-04-26 15:55:00 -0400702 fPerspectivePoints[0].set(0, 0);
703 fPerspectivePoints[1].set(1, 0);
704 fPerspectivePoints[2].set(0, 1);
705 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700706 fAnimTimer.run();
707
Hal Canaryc465d132017-12-08 10:21:31 -0500708 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500709 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500710 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500711 }
712 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500713
jongdeok.kim804f17e2019-02-26 14:39:23 +0900714 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500715 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700716}
717
jvanverth34524262016-05-04 13:49:13 -0700718void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400719 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
720 static const struct {
721 const char* fExtension;
722 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500723 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400724 const SlideFactory fFactory;
725 } gExternalSlidesInfo[] = {
726 { ".skp", "skp-dir", FLAGS_skps,
727 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
728 return sk_make_sp<SKPSlide>(name, path);}
729 },
730 { ".jpg", "jpg-dir", FLAGS_jpgs,
731 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
732 return sk_make_sp<ImageSlide>(name, path);}
733 },
Florin Malita87ccf332018-05-04 12:23:24 -0400734#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400735 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400736 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
737 return sk_make_sp<SkottieSlide>(name, path);}
738 },
Florin Malita87ccf332018-05-04 12:23:24 -0400739#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400740 #if defined(SK_ENABLE_SKRIVE)
741 { ".flr", "skrive-dir", FLAGS_rives,
742 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
743 return sk_make_sp<SkRiveSlide>(name, path);}
744 },
745 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400746#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400747 { ".svg", "svg-dir", FLAGS_svgs,
748 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
749 return sk_make_sp<SvgSlide>(name, path);}
750 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400751#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400752 };
jvanverthc265a922016-04-08 12:51:45 -0700753
Brian Salomon343553a2018-09-05 15:41:23 -0400754 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700755
Mike Klein88544fb2019-03-20 10:50:33 -0500756 const auto addSlide =
757 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
758 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
759 return;
760 }
liyuqian6f163d22016-06-13 12:26:45 -0700761
Mike Klein88544fb2019-03-20 10:50:33 -0500762 if (auto slide = fact(name, path)) {
763 dirSlides.push_back(slide);
764 fSlides.push_back(std::move(slide));
765 }
766 };
Florin Malita76a076b2018-02-15 18:40:48 -0500767
Florin Malita38792ce2018-05-08 10:36:18 -0400768 if (!FLAGS_file.isEmpty()) {
769 // single file mode
770 const SkString file(FLAGS_file[0]);
771
772 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
773 for (const auto& sinfo : gExternalSlidesInfo) {
774 if (file.endsWith(sinfo.fExtension)) {
775 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
776 return;
777 }
778 }
779
780 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
781 } else {
782 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
783 }
784
785 return;
786 }
787
788 // Bisect slide.
789 if (!FLAGS_bisect.isEmpty()) {
790 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500791 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400792 if (FLAGS_bisect.count() >= 2) {
793 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
794 bisect->onChar(*ch);
795 }
796 }
797 fSlides.push_back(std::move(bisect));
798 }
799 }
800
801 // GMs
802 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400803 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400804 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500805 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400806 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400807 fSlides.push_back(std::move(slide));
808 }
Florin Malita38792ce2018-05-08 10:36:18 -0400809 }
810 // reverse gms
811 int numGMs = fSlides.count() - firstGM;
812 for (int i = 0; i < numGMs/2; ++i) {
813 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
814 }
815
816 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400817 for (const SampleFactory factory : SampleRegistry::Range()) {
818 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500819 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400820 fSlides.push_back(slide);
821 }
Florin Malita38792ce2018-05-08 10:36:18 -0400822 }
823
Brian Osman7c979f52019-02-12 13:27:51 -0500824 // Particle demo
825 {
826 // TODO: Convert this to a sample
827 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500828 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500829 fSlides.push_back(std::move(slide));
830 }
831 }
832
Brian Osmand927bd22019-12-18 11:23:12 -0500833 // Runtime shader editor
834 {
835 sk_sp<Slide> slide(new SkSLSlide());
836 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
837 fSlides.push_back(std::move(slide));
838 }
839 }
840
Florin Malita0ffa3222018-04-05 14:34:45 -0400841 for (const auto& info : gExternalSlidesInfo) {
842 for (const auto& flag : info.fFlags) {
843 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
844 // single file
845 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
846 } else {
847 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400848 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400849 SkTArray<SkString> sortedFilenames;
850 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400851 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400852 sortedFilenames.push_back(name);
853 }
854 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400855 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000856 [](const SkString& a, const SkString& b) {
857 return strcmp(a.c_str(), b.c_str()) < 0;
858 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400859 }
860 for (const SkString& filename : sortedFilenames) {
861 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
862 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400863 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400864 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400865 if (!dirSlides.empty()) {
866 fSlides.push_back(
867 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
868 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500869 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400870 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400871 }
872 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500873
874 if (!fSlides.count()) {
875 sk_sp<Slide> slide(new NullSlide());
876 fSlides.push_back(std::move(slide));
877 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700878}
879
880
jvanverth34524262016-05-04 13:49:13 -0700881Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400882 for(auto& slide : fSlides) {
883 slide->gpuTeardown();
884 }
885
jvanverth9f372462016-04-06 06:08:59 -0700886 fWindow->detach();
887 delete fWindow;
888}
889
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500890struct SkPaintTitleUpdater {
891 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
892 void append(const char* s) {
893 if (fCount == 0) {
894 fTitle->append(" {");
895 } else {
896 fTitle->append(", ");
897 }
898 fTitle->append(s);
899 ++fCount;
900 }
901 void done() {
902 if (fCount > 0) {
903 fTitle->append("}");
904 }
905 }
906 SkString* fTitle;
907 int fCount;
908};
909
brianosman05de2162016-05-06 13:28:57 -0700910void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700911 if (!fWindow) {
912 return;
913 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500914 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700915 return; // Surface hasn't been created yet.
916 }
917
jvanverth34524262016-05-04 13:49:13 -0700918 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700919 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700920
Mike Kleine5acd752019-03-22 09:57:16 -0500921 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400922 if (gSkForceAnalyticAA) {
923 title.append(" <FAAA>");
924 } else {
925 title.append(" <AAA>");
926 }
927 }
Mike Reed59295352020-03-12 13:56:34 -0400928 if (fDrawViaSerialize) {
929 title.append(" <serialize>");
930 }
Mike Reed862818b2020-03-21 15:07:13 -0400931 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500932 title.append(" <SkVMBlitter>");
933 }
934 if (!gSkVMAllowJIT) {
935 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400936 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400937
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500938 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500939 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
940 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400941 const char* on, const char* off)
942 {
Ben Wagner9613e452019-01-23 10:34:59 -0500943 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400944 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500945 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400946 };
947
Ben Wagner9613e452019-01-23 10:34:59 -0500948 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
949 const char* on, const char* off)
950 {
951 if (fFontOverrides.*flag) {
952 paintTitle.append((fFont.*isFlag)() ? on : off);
953 }
954 };
955
956 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
957 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
958
959 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
960 "Force Autohint", "No Force Autohint");
961 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400962 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500963 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
964 "Linear Metrics", "Non-Linear Metrics");
965 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
966 "Bitmap Text", "No Bitmap Text");
967 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
968
969 if (fFontOverrides.fEdging) {
970 switch (fFont.getEdging()) {
971 case SkFont::Edging::kAlias:
972 paintTitle.append("Alias Text");
973 break;
974 case SkFont::Edging::kAntiAlias:
975 paintTitle.append("Antialias Text");
976 break;
977 case SkFont::Edging::kSubpixelAntiAlias:
978 paintTitle.append("Subpixel Antialias Text");
979 break;
980 }
981 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400982
Mike Reed3ae47332019-01-04 10:11:46 -0500983 if (fFontOverrides.fHinting) {
984 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400985 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500986 paintTitle.append("No Hinting");
987 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400988 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500989 paintTitle.append("Slight Hinting");
990 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400991 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500992 paintTitle.append("Normal Hinting");
993 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400994 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500995 paintTitle.append("Full Hinting");
996 break;
997 }
998 }
999 paintTitle.done();
1000
Brian Osman92004802017-03-06 11:47:26 -05001001 switch (fColorMode) {
1002 case ColorMode::kLegacy:
1003 title.append(" Legacy 8888");
1004 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001005 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001006 title.append(" ColorManaged 8888");
1007 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001008 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001009 title.append(" ColorManaged F16");
1010 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001011 case ColorMode::kColorManagedF16Norm:
1012 title.append(" ColorManaged F16 Norm");
1013 break;
Brian Osman92004802017-03-06 11:47:26 -05001014 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001015
Brian Osman92004802017-03-06 11:47:26 -05001016 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001017 int curPrimaries = -1;
1018 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1019 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1020 curPrimaries = i;
1021 break;
1022 }
1023 }
Brian Osman03115dc2018-11-26 13:55:19 -05001024 title.appendf(" %s Gamma %f",
1025 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001026 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001027 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001028
Ben Wagner37c54032018-04-13 14:30:23 -04001029 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001030 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001031 switch (params.fSurfaceProps.pixelGeometry()) {
1032 case kUnknown_SkPixelGeometry:
1033 title.append( " Flat");
1034 break;
1035 case kRGB_H_SkPixelGeometry:
1036 title.append( " RGB");
1037 break;
1038 case kBGR_H_SkPixelGeometry:
1039 title.append( " BGR");
1040 break;
1041 case kRGB_V_SkPixelGeometry:
1042 title.append( " RGBV");
1043 break;
1044 case kBGR_V_SkPixelGeometry:
1045 title.append( " BGRV");
1046 break;
1047 }
1048 }
1049
1050 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1051 title.append(" DFT");
1052 }
1053
csmartdalton578f0642017-02-24 16:04:47 -07001054 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001055 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001056 int msaa = fWindow->sampleCount();
1057 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001058 title.appendf(" MSAA: %i", msaa);
1059 }
1060 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001061
1062 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001063 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001064 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1065 }
1066
Brian Osman805a7272018-05-02 15:40:20 -04001067 if (kPerspective_Real == fPerspectiveMode) {
1068 title.append(" Perpsective (Real)");
1069 } else if (kPerspective_Fake == fPerspectiveMode) {
1070 title.append(" Perspective (Fake)");
1071 }
1072
brianosman05de2162016-05-06 13:28:57 -07001073 fWindow->setTitle(title.c_str());
1074}
1075
Florin Malitaab99c342018-01-16 16:23:03 -05001076int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001077
1078 if (!FLAGS_slide.isEmpty()) {
1079 int count = fSlides.count();
1080 for (int i = 0; i < count; i++) {
1081 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001082 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001083 }
1084 }
1085
1086 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1087 this->listNames();
1088 }
1089
Florin Malitaab99c342018-01-16 16:23:03 -05001090 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001091}
1092
Florin Malitaab99c342018-01-16 16:23:03 -05001093void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001094 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001095 for (const auto& slide : fSlides) {
1096 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001097 }
1098}
1099
Florin Malitaab99c342018-01-16 16:23:03 -05001100void Viewer::setCurrentSlide(int slide) {
1101 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001102
Florin Malitaab99c342018-01-16 16:23:03 -05001103 if (slide == fCurrentSlide) {
1104 return;
1105 }
1106
1107 if (fCurrentSlide >= 0) {
1108 fSlides[fCurrentSlide]->unload();
1109 }
1110
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001111 SkScalar scaleFactor = 1.0;
1112 if (fApplyBackingScale) {
1113 scaleFactor = fWindow->scaleFactor();
1114 }
1115 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1116 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001117 fCurrentSlide = slide;
1118 this->setupCurrentSlide();
1119}
1120
1121void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001122 if (fCurrentSlide >= 0) {
1123 // prepare dimensions for image slides
1124 fGesture.resetTouchState();
1125 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001126
Jim Van Verth0848fb02018-01-22 13:39:30 -05001127 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1128 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1129 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001130
Jim Van Verth0848fb02018-01-22 13:39:30 -05001131 // Start with a matrix that scales the slide to the available screen space
1132 if (fWindow->scaleContentToFit()) {
1133 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001134 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1135 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001136 }
liyuqiane46e4f02016-05-20 07:32:19 -07001137 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001138
1139 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001140 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001141
1142 this->updateTitle();
1143 this->updateUIState();
1144
1145 fStatsLayer.resetMeasurements();
1146
1147 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001148 }
jvanverthc265a922016-04-08 12:51:45 -07001149}
1150
Brian Osmanaba642c2020-02-06 12:52:25 -05001151#define MAX_ZOOM_LEVEL 8.0f
1152#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001153
jvanverth34524262016-05-04 13:49:13 -07001154void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001155 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001156 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001157 this->preTouchMatrixChanged();
1158}
Yuqian Li755778c2018-03-28 16:23:31 -04001159
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001160void Viewer::preTouchMatrixChanged() {
1161 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001162 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1163 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1164 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1165 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1166}
1167
Brian Osman805a7272018-05-02 15:40:20 -04001168SkMatrix Viewer::computePerspectiveMatrix() {
1169 SkScalar w = fWindow->width(), h = fWindow->height();
1170 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1171 SkPoint perspPts[4] = {
1172 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1173 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1174 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1175 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1176 };
1177 SkMatrix m;
1178 m.setPolyToPoly(orthoPts, perspPts, 4);
1179 return m;
1180}
1181
Yuqian Li755778c2018-03-28 16:23:31 -04001182SkMatrix Viewer::computePreTouchMatrix() {
1183 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001184
1185 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001186 if (fApplyBackingScale) {
1187 zoomScale *= fWindow->scaleFactor();
1188 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001189 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001190 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001191
1192 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1193 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001194
Brian Osman805a7272018-05-02 15:40:20 -04001195 if (kPerspective_Real == fPerspectiveMode) {
1196 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001197 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001198 }
1199
Yuqian Li755778c2018-03-28 16:23:31 -04001200 return m;
jvanverthc265a922016-04-08 12:51:45 -07001201}
1202
liyuqiand3cdbca2016-05-17 12:44:20 -07001203SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001204 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001205 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001206 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001207 return m;
jvanverthc265a922016-04-08 12:51:45 -07001208}
1209
Brian Osman621491e2017-02-28 15:45:01 -05001210void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001211 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001212 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001213 fBackendType = backendType;
1214
Robert Phillipse9229532020-06-26 10:10:49 -04001215 // The active context is going away in 'detach'
1216 for(auto& slide : fSlides) {
1217 slide->gpuTeardown();
1218 }
1219
Brian Osman621491e2017-02-28 15:45:01 -05001220 fWindow->detach();
1221
Brian Osman70d2f432017-11-08 09:54:10 -05001222#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001223 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1224 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001225 DisplayParams params = fWindow->getRequestedDisplayParams();
1226 delete fWindow;
1227 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001228
Brian Osman70d2f432017-11-08 09:54:10 -05001229 // re-register callbacks
1230 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001231 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001232 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001233 fWindow->pushLayer(&fImGuiLayer);
1234
Brian Osman70d2f432017-11-08 09:54:10 -05001235 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1236 // will still include our correct sample count. But the re-created fWindow will lose that
1237 // information. On Windows, we need to re-create the window when changing sample count,
1238 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1239 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1240 // as if we had never changed windows at all).
1241 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001242#endif
1243
Brian Osman70d2f432017-11-08 09:54:10 -05001244 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001245}
1246
Brian Osman92004802017-03-06 11:47:26 -05001247void Viewer::setColorMode(ColorMode colorMode) {
1248 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001249 this->updateTitle();
1250 fWindow->inval();
1251}
1252
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001253class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1254public:
Mike Reed3ae47332019-01-04 10:11:46 -05001255 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1256 SkFont* font, Viewer::SkFontFields* ffields)
1257 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001258 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001259 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1260 sk_sp<SkTextBlob>* cache) {
1261 bool blobWillChange = false;
1262 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001263 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1264 bool shouldDraw = this->filterFont(&filteredFont);
1265 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001266 blobWillChange = true;
1267 break;
1268 }
1269 }
1270 if (!blobWillChange) {
1271 return blob;
1272 }
1273
1274 SkTextBlobBuilder builder;
1275 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001276 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1277 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001278 if (!shouldDraw) {
1279 continue;
1280 }
1281
Mike Reed3ae47332019-01-04 10:11:46 -05001282 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001283
Ben Wagner41e40472018-09-24 13:01:54 -04001284 const SkTextBlobBuilder::RunBuffer& runBuffer
1285 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001286 ? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(),
1287 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001288 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001289 ? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(),
1290 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001291 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001292 ? builder.allocRunTextPos(font, it.glyphCount(), it.textSize())
Ben Wagnere5736262021-02-08 16:52:08 -05001293 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001294 ? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001295 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1296 uint32_t glyphCount = it.glyphCount();
1297 if (it.glyphs()) {
1298 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1299 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1300 }
1301 if (it.pos()) {
1302 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001303 unsigned posPerGlyph = it.scalarsPerGlyph();
1304 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001305 }
1306 if (it.text()) {
1307 size_t textSize = sizeof(decltype(*it.text()));
1308 uint32_t textCount = it.textSize();
1309 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1310 }
1311 if (it.clusters()) {
1312 size_t clusterSize = sizeof(decltype(*it.clusters()));
1313 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1314 }
1315 }
1316 *cache = builder.make();
1317 return cache->get();
1318 }
1319 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1320 const SkPaint& paint) override {
1321 sk_sp<SkTextBlob> cache;
1322 this->SkPaintFilterCanvas::onDrawTextBlob(
1323 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1324 }
Mike Reed3ae47332019-01-04 10:11:46 -05001325 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001326 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001327 font->writable()->setSize(fFont->getSize());
1328 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001329 if (fFontOverrides->fScaleX) {
1330 font->writable()->setScaleX(fFont->getScaleX());
1331 }
1332 if (fFontOverrides->fSkewX) {
1333 font->writable()->setSkewX(fFont->getSkewX());
1334 }
Mike Reed3ae47332019-01-04 10:11:46 -05001335 if (fFontOverrides->fHinting) {
1336 font->writable()->setHinting(fFont->getHinting());
1337 }
Ben Wagner9613e452019-01-23 10:34:59 -05001338 if (fFontOverrides->fEdging) {
1339 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001340 }
Ben Wagner9613e452019-01-23 10:34:59 -05001341 if (fFontOverrides->fEmbolden) {
1342 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001343 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001344 if (fFontOverrides->fBaselineSnap) {
1345 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1346 }
Ben Wagner9613e452019-01-23 10:34:59 -05001347 if (fFontOverrides->fLinearMetrics) {
1348 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001349 }
Ben Wagner9613e452019-01-23 10:34:59 -05001350 if (fFontOverrides->fSubpixel) {
1351 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001352 }
Ben Wagner9613e452019-01-23 10:34:59 -05001353 if (fFontOverrides->fEmbeddedBitmaps) {
1354 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001355 }
Ben Wagner9613e452019-01-23 10:34:59 -05001356 if (fFontOverrides->fForceAutoHinting) {
1357 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001358 }
Ben Wagner9613e452019-01-23 10:34:59 -05001359
Mike Reed3ae47332019-01-04 10:11:46 -05001360 return true;
1361 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001362 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001363 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001364 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001365 }
Ben Wagner9613e452019-01-23 10:34:59 -05001366 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001367 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001368 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001369 if (fPaintOverrides->fStyle) {
1370 paint.setStyle(fPaint->getStyle());
1371 }
1372 if (fPaintOverrides->fWidth) {
1373 paint.setStrokeWidth(fPaint->getStrokeWidth());
1374 }
1375 if (fPaintOverrides->fMiterLimit) {
1376 paint.setStrokeMiter(fPaint->getStrokeMiter());
1377 }
1378 if (fPaintOverrides->fCapType) {
1379 paint.setStrokeCap(fPaint->getStrokeCap());
1380 }
1381 if (fPaintOverrides->fJoinType) {
1382 paint.setStrokeJoin(fPaint->getStrokeJoin());
1383 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001384 return true;
1385 }
1386 SkPaint* fPaint;
1387 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001388 SkFont* fFont;
1389 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001390};
1391
Robert Phillips9882dae2019-03-04 11:00:10 -05001392void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001393 if (fCurrentSlide < 0) {
1394 return;
1395 }
1396
Robert Phillips9882dae2019-03-04 11:00:10 -05001397 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001398
Brian Osmanf750fbc2017-02-08 10:47:28 -05001399 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001400 SkSurface* slideSurface = surface;
1401 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001402 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001403
Brian Osmane0d4fba2017-03-15 10:24:55 -04001404 // 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 -05001405 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001406 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001407 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001408 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001409 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001410 }
1411
Brian Osman3ac99cf2017-12-01 11:23:53 -05001412 if (fSaveToSKP) {
1413 SkPictureRecorder recorder;
1414 SkCanvas* recorderCanvas = recorder.beginRecording(
1415 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001416 fSlides[fCurrentSlide]->draw(recorderCanvas);
1417 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1418 SkFILEWStream stream("sample_app.skp");
1419 picture->serialize(&stream);
1420 fSaveToSKP = false;
1421 }
1422
Brian Osmane9ed0f02018-11-26 14:50:05 -05001423 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001424 SkColorType colorType;
1425 switch (fColorMode) {
1426 case ColorMode::kLegacy:
1427 case ColorMode::kColorManaged8888:
1428 colorType = kN32_SkColorType;
1429 break;
1430 case ColorMode::kColorManagedF16:
1431 colorType = kRGBA_F16_SkColorType;
1432 break;
1433 case ColorMode::kColorManagedF16Norm:
1434 colorType = kRGBA_F16Norm_SkColorType;
1435 break;
1436 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001437
1438 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001439 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001440 slideCanvas->getProps(&props);
1441
Brian Osmane9ed0f02018-11-26 14:50:05 -05001442 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1443 return Window::kRaster_BackendType == this->fBackendType
1444 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001445 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001446 };
1447
Brian Osman03115dc2018-11-26 13:55:19 -05001448 // We need to render offscreen if we're...
1449 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1450 // ... in any raster mode, because the window surface is actually GL
1451 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001452 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001453 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001454 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001455 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001456 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001457 colorSpace != nullptr ||
1458 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001459
Brian Osmane9ed0f02018-11-26 14:50:05 -05001460 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001461 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001462 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001463 }
1464
Mike Reed59295352020-03-12 13:56:34 -04001465 SkPictureRecorder recorder;
1466 SkCanvas* recorderRestoreCanvas = nullptr;
1467 if (fDrawViaSerialize) {
1468 recorderRestoreCanvas = slideCanvas;
1469 slideCanvas = recorder.beginRecording(
1470 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1471 }
1472
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001473 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001474 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001475 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001476 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001477 if (fTiled) {
1478 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1479 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001480 for (int y = 0; y < fWindow->height(); y += tileH) {
1481 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001482 SkAutoCanvasRestore acr(slideCanvas, true);
1483 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1484 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001485 }
1486 }
1487
1488 // Draw borders between tiles
1489 if (fDrawTileBoundaries) {
1490 SkPaint border;
1491 border.setColor(0x60FF00FF);
1492 border.setStyle(SkPaint::kStroke_Style);
1493 for (int y = 0; y < fWindow->height(); y += tileH) {
1494 for (int x = 0; x < fWindow->width(); x += tileW) {
1495 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1496 }
1497 }
1498 }
1499 } else {
1500 slideCanvas->concat(this->computeMatrix());
1501 if (kPerspective_Real == fPerspectiveMode) {
1502 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1503 }
Mike Reed3ae47332019-01-04 10:11:46 -05001504 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001505 fSlides[fCurrentSlide]->draw(&filterCanvas);
1506 }
Brian Osman56a24812017-12-19 11:15:16 -05001507 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001508 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001509
Mike Reed59295352020-03-12 13:56:34 -04001510 if (recorderRestoreCanvas) {
1511 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1512 auto data = picture->serialize();
1513 slideCanvas = recorderRestoreCanvas;
1514 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1515 }
1516
Brian Osman1df161a2017-02-09 12:10:20 -05001517 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001518 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001519 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001520 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001521
1522 // If we rendered offscreen, snap an image and push the results to the window's canvas
1523 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001524 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001525
Robert Phillips9882dae2019-03-04 11:00:10 -05001526 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001527 SkPaint paint;
1528 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001529 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001530 int prePerspectiveCount = canvas->save();
1531 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001532 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001533 canvas->clear(SK_ColorWHITE);
1534 canvas->concat(this->computePerspectiveMatrix());
1535 }
Mike Reedb339d052021-01-28 11:20:41 -05001536 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001537 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001538 }
Mike Reed376d8122019-03-14 11:39:02 -04001539
1540 if (fShowSlideDimensions) {
1541 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1542 SkPaint paint;
1543 paint.setColor(0x40FFFF00);
1544 surface->getCanvas()->drawRect(r, paint);
1545 }
liyuqian6f163d22016-06-13 12:26:45 -07001546}
1547
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001548void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001549 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001550 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001551}
Jim Van Verth6f449692017-02-14 15:16:46 -05001552
Robert Phillips9882dae2019-03-04 11:00:10 -05001553void Viewer::onPaint(SkSurface* surface) {
1554 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001555
Robert Phillips9882dae2019-03-04 11:00:10 -05001556 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001557
Brian Osmand67e5182017-12-08 16:46:09 -05001558 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001559
Greg Daniel427d8eb2020-09-28 15:04:18 -04001560 fLastImage.reset();
1561
Robert Phillipsed653392020-07-10 13:55:21 -04001562 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001563 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001564 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001565 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001566}
1567
Ben Wagnera1915972018-08-09 15:06:19 -04001568void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001569 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001570 SkScalar scaleFactor = 1.0;
1571 if (fApplyBackingScale) {
1572 scaleFactor = fWindow->scaleFactor();
1573 }
1574 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001575 }
Ben Wagnera1915972018-08-09 15:06:19 -04001576}
1577
Florin Malitacefc1b92018-02-19 21:43:47 -05001578SkPoint Viewer::mapEvent(float x, float y) {
1579 const auto m = this->computeMatrix();
1580 SkMatrix inv;
1581
1582 SkAssertResult(m.invert(&inv));
1583
1584 return inv.mapXY(x, y);
1585}
1586
Hal Canaryb1f411a2019-08-29 10:39:22 -04001587bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001588 if (GestureDevice::kMouse == fGestureDevice) {
1589 return false;
1590 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001591
1592 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001593 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001594 fWindow->inval();
1595 return true;
1596 }
1597
liyuqiand3cdbca2016-05-17 12:44:20 -07001598 void* castedOwner = reinterpret_cast<void*>(owner);
1599 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001600 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001601 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001602#if defined(SK_BUILD_FOR_IOS)
1603 // TODO: move IOS swipe detection higher up into the platform code
1604 SkPoint dir;
1605 if (fGesture.isFling(&dir)) {
1606 // swiping left or right
1607 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1608 if (dir.fX < 0) {
1609 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1610 fCurrentSlide + 1 : 0);
1611 } else {
1612 this->setCurrentSlide(fCurrentSlide > 0 ?
1613 fCurrentSlide - 1 : fSlides.count() - 1);
1614 }
1615 }
1616 fGesture.reset();
1617 }
1618#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001619 break;
1620 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001621 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001622 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001623 break;
1624 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001625 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001626 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001627 break;
1628 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001629 default: {
1630 // kLeft and kRight are only for swipes
1631 SkASSERT(false);
1632 break;
1633 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001634 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001635 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001636 fWindow->inval();
1637 return true;
1638}
1639
Hal Canaryb1f411a2019-08-29 10:39:22 -04001640bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001641 if (GestureDevice::kTouch == fGestureDevice) {
1642 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001643 }
Brian Osman16c81a12017-12-20 11:58:34 -05001644
Florin Malitacefc1b92018-02-19 21:43:47 -05001645 const auto slidePt = this->mapEvent(x, y);
1646 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1647 fWindow->inval();
1648 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001649 }
1650
1651 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001652 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001653 fGesture.touchEnd(nullptr);
1654 break;
1655 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001656 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001657 fGesture.touchBegin(nullptr, x, y);
1658 break;
1659 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001660 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001661 fGesture.touchMoved(nullptr, x, y);
1662 break;
1663 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001664 default: {
1665 SkASSERT(false); // shouldn't see kRight or kLeft here
1666 break;
1667 }
Brian Osman16c81a12017-12-20 11:58:34 -05001668 }
1669 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1670
Hal Canaryb1f411a2019-08-29 10:39:22 -04001671 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001672 fWindow->inval();
1673 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001674 return true;
1675}
1676
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001677bool Viewer::onFling(skui::InputState state) {
1678 if (skui::InputState::kRight == state) {
1679 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1680 return true;
1681 } else if (skui::InputState::kLeft == state) {
1682 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1683 return true;
1684 }
1685 return false;
1686}
1687
1688bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1689 switch (state) {
1690 case skui::InputState::kDown:
1691 fGesture.startZoom();
1692 return true;
1693 break;
1694 case skui::InputState::kMove:
1695 fGesture.updateZoom(scale, x, y, x, y);
1696 return true;
1697 break;
1698 case skui::InputState::kUp:
1699 fGesture.endZoom();
1700 return true;
1701 break;
1702 default:
1703 SkASSERT(false);
1704 break;
1705 }
1706
1707 return false;
1708}
1709
Brian Osmana109e392017-02-24 09:49:14 -05001710static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001711 // The gamut image covers a (0.8 x 0.9) shaped region
1712 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001713
1714 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1715 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1716 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001717 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1718 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1719 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001720
Brian Osman535c5e32019-02-09 16:32:58 -05001721 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1722 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1723 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1724 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1725 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001726}
1727
Ben Wagner3627d2e2018-06-26 14:23:20 -04001728static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001729 ImGui::DragCanvas dc(pt);
1730 dc.fillColor(IM_COL32(0, 0, 0, 128));
1731 dc.dragPoint(pt);
1732 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001733}
1734
Brian Osman9bb47cf2018-04-26 15:55:00 -04001735static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001736 ImGui::DragCanvas dc(pts);
1737 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001738
Brian Osman535c5e32019-02-09 16:32:58 -05001739 for (int i = 0; i < 4; ++i) {
1740 dc.dragPoint(pts + i);
1741 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001742
Brian Osman535c5e32019-02-09 16:32:58 -05001743 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1744 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1745 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1746 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001747
Brian Osman535c5e32019-02-09 16:32:58 -05001748 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001749}
1750
John Stiles38b7d2f2020-06-24 12:13:31 -04001751static SkSL::String build_sksl_highlight_shader() {
1752 return SkSL::String("out half4 sk_FragColor;\n"
1753 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1754}
1755
1756static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1757 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1758 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1759 size_t pos = inShader.rfind("return *_out;\n");
1760 if (pos == std::string::npos) {
1761 return inShader;
1762 }
1763
1764 SkSL::String replacementShader = inShader;
1765 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1766 return replacementShader;
1767}
1768
1769static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1770 const char* versionDecl = shaderCaps.versionDeclString();
1771 SkSL::String highlight = versionDecl ? versionDecl : "";
1772 if (shaderCaps.usesPrecisionModifiers()) {
1773 highlight.append("precision mediump float;\n");
1774 }
1775 highlight.appendf("out vec4 sk_FragColor;\n"
1776 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1777 return highlight;
1778}
1779
Brian Osmand67e5182017-12-08 16:46:09 -05001780void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001781 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1782 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001783 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001784 }
1785
1786 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001787 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1788 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001789 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001790 DisplayParams params = fWindow->getRequestedDisplayParams();
1791 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001792 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001793
Brian Osmana109e392017-02-24 09:49:14 -05001794 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1795 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001796 if (ImGui::CollapsingHeader("Backend")) {
1797 int newBackend = static_cast<int>(fBackendType);
1798 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1799 ImGui::SameLine();
1800 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001801#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1802 ImGui::SameLine();
1803 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1804#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001805#if defined(SK_DAWN)
1806 ImGui::SameLine();
1807 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1808#endif
John Stilesf7da9232020-11-19 19:58:14 -05001809#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001810 ImGui::SameLine();
1811 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1812#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001813#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001814 ImGui::SameLine();
1815 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1816#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001817#if defined(SK_DIRECT3D)
1818 ImGui::SameLine();
1819 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1820#endif
Brian Osman621491e2017-02-28 15:45:01 -05001821 if (newBackend != fBackendType) {
1822 fDeferredActions.push_back([=]() {
1823 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1824 });
1825 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001826
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001827 bool* wire = &params.fGrContextOptions.fWireframeMode;
1828 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1829 paramsChanged = true;
1830 }
Brian Salomon99a33902017-03-07 15:16:34 -05001831
Brian Osman28b12522017-03-08 17:10:24 -05001832 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001833 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001834 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001835 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1836 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1837 1;
1838
1839 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1840 if (maxMSAA >= 4) {
1841 ImGui::Text("MSAA: ");
1842
1843 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1844 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1845 // don't show it in the list.
1846 if (curMSAA == 2) {
1847 continue;
1848 }
1849 ImGui::SameLine();
1850 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1851 &sampleCount, curMSAA);
1852 }
1853 }
Brian Osman28b12522017-03-08 17:10:24 -05001854
1855 if (sampleCount != params.fMSAASampleCount) {
1856 params.fMSAASampleCount = sampleCount;
1857 paramsChanged = true;
1858 }
1859 }
1860
Ben Wagner37c54032018-04-13 14:30:23 -04001861 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001862 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001863 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1864 }
1865 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1866 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1867 {
1868 uint32_t flags = params.fSurfaceProps.flags();
1869 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001870 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1871 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1872 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001873 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001874 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001875 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1876 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1877 }
1878 paramsChanged = true;
1879 }
1880
1881 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1882 if (ImGui::Checkbox("DFT", &useDFT)) {
1883 uint32_t flags = params.fSurfaceProps.flags();
1884 if (useDFT) {
1885 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1886 } else {
1887 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1888 }
1889 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1890 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1891 paramsChanged = true;
1892 }
1893
Brian Osman8a9de3d2017-03-01 14:59:05 -05001894 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001895 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001896 auto prButton = [&](GpuPathRenderers x) {
1897 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001898 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1899 params.fGrContextOptions.fGpuPathRenderers = x;
1900 paramsChanged = true;
1901 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001902 }
1903 };
1904
1905 if (!ctx) {
1906 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001907 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001908 const auto* caps = ctx->priv().caps();
1909 prButton(GpuPathRenderers::kDefault);
1910 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001911 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001912 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001913 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001914 if (caps->shaderCaps()->pathRenderingSupport()) {
1915 prButton(GpuPathRenderers::kStencilAndCover);
1916 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001917 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001918 if (1 == fWindow->sampleCount()) {
1919 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1920 prButton(GpuPathRenderers::kCoverageCounting);
1921 }
1922 prButton(GpuPathRenderers::kSmall);
1923 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001924 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001925 prButton(GpuPathRenderers::kNone);
1926 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001927 ImGui::TreePop();
1928 }
Brian Osman621491e2017-02-28 15:45:01 -05001929 }
1930
Ben Wagner964571d2019-03-08 12:35:06 -05001931 if (ImGui::CollapsingHeader("Tiling")) {
1932 ImGui::Checkbox("Enable", &fTiled);
1933 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1934 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1935 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1936 }
1937
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001938 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001939 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1940 this->preTouchMatrixChanged();
1941 this->onResize(fWindow->width(), fWindow->height());
1942 paramsChanged = true;
1943 }
1944
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001945 float zoom = fZoomLevel;
1946 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1947 fZoomLevel = zoom;
1948 this->preTouchMatrixChanged();
1949 paramsChanged = true;
1950 }
1951 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001952 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001953 fRotation = deg;
1954 this->preTouchMatrixChanged();
1955 paramsChanged = true;
1956 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001957 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1958 if (ImGui_DragLocation(&fOffset)) {
1959 this->preTouchMatrixChanged();
1960 paramsChanged = true;
1961 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001962 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1963 this->preTouchMatrixChanged();
1964 paramsChanged = true;
1965 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001966 }
Brian Osman805a7272018-05-02 15:40:20 -04001967 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1968 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1969 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001970 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001971 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001972 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001973 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001974 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001975 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001976 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001977 }
1978
Ben Wagnera580fb32018-04-17 11:16:32 -04001979 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001980 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001981 if (fPaintOverrides.fAntiAlias) {
1982 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001983 }
1984 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001985 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001986 {
1987 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1988 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001989 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001990 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1991 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001992 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001993 fPaintOverrides.fAntiAlias = true;
1994 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001995 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001996 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001997 case SkPaintFields::AntiAliasState::Alias:
1998 break;
1999 case SkPaintFields::AntiAliasState::Normal:
2000 break;
2001 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
2002 gSkUseAnalyticAA = true;
2003 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002004 break;
2005 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2006 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002007 break;
2008 }
2009 }
2010 paramsChanged = true;
2011 }
2012
Ben Wagner99a78dc2018-05-09 18:23:51 -04002013 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002014 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002015 bool (SkPaint::* isFlag)() const,
2016 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002017 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002018 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002019 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002020 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002021 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002022 if (ImGui::Combo(label, &itemIndex, items)) {
2023 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002024 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002025 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002026 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002027 (fPaint.*setFlag)(itemIndex == 2);
2028 }
2029 paramsChanged = true;
2030 }
2031 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002032
Ben Wagner99a78dc2018-05-09 18:23:51 -04002033 paintFlag("Dither",
2034 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002035 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002036 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002037
2038 int styleIdx = 0;
2039 if (fPaintOverrides.fStyle) {
2040 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2041 }
2042 if (ImGui::Combo("Style", &styleIdx,
2043 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2044 {
2045 if (styleIdx == 0) {
2046 fPaintOverrides.fStyle = false;
2047 fPaint.setStyle(SkPaint::kFill_Style);
2048 } else {
2049 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2050 fPaintOverrides.fStyle = true;
2051 }
2052 paramsChanged = true;
2053 }
2054
2055 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2056 if (fPaintOverrides.fWidth) {
2057 float width = fPaint.getStrokeWidth();
2058 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2059 fPaint.setStrokeWidth(width);
2060 paramsChanged = true;
2061 }
2062 }
2063
2064 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2065 if (fPaintOverrides.fMiterLimit) {
2066 float miterLimit = fPaint.getStrokeMiter();
2067 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2068 fPaint.setStrokeMiter(miterLimit);
2069 paramsChanged = true;
2070 }
2071 }
2072
2073 int capIdx = 0;
2074 if (fPaintOverrides.fCapType) {
2075 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2076 }
2077 if (ImGui::Combo("Cap Type", &capIdx,
2078 "Default\0Butt\0Round\0Square\0\0"))
2079 {
2080 if (capIdx == 0) {
2081 fPaintOverrides.fCapType = false;
2082 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2083 } else {
2084 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2085 fPaintOverrides.fCapType = true;
2086 }
2087 paramsChanged = true;
2088 }
2089
2090 int joinIdx = 0;
2091 if (fPaintOverrides.fJoinType) {
2092 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2093 }
2094 if (ImGui::Combo("Join Type", &joinIdx,
2095 "Default\0Miter\0Round\0Bevel\0\0"))
2096 {
2097 if (joinIdx == 0) {
2098 fPaintOverrides.fJoinType = false;
2099 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2100 } else {
2101 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2102 fPaintOverrides.fJoinType = true;
2103 }
2104 paramsChanged = true;
2105 }
Ben Wagner9613e452019-01-23 10:34:59 -05002106 }
Hal Canary02738a82019-01-21 18:51:32 +00002107
Ben Wagner9613e452019-01-23 10:34:59 -05002108 if (ImGui::CollapsingHeader("Font")) {
2109 int hintingIdx = 0;
2110 if (fFontOverrides.fHinting) {
2111 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2112 }
2113 if (ImGui::Combo("Hinting", &hintingIdx,
2114 "Default\0None\0Slight\0Normal\0Full\0\0"))
2115 {
2116 if (hintingIdx == 0) {
2117 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002118 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002119 } else {
2120 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2121 fFontOverrides.fHinting = true;
2122 }
2123 paramsChanged = true;
2124 }
Hal Canary02738a82019-01-21 18:51:32 +00002125
Ben Wagner9613e452019-01-23 10:34:59 -05002126 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2127 bool SkFontFields::* flag,
2128 bool (SkFont::* isFlag)() const,
2129 void (SkFont::* setFlag)(bool) )
2130 {
2131 int itemIndex = 0;
2132 if (fFontOverrides.*flag) {
2133 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2134 }
2135 if (ImGui::Combo(label, &itemIndex, items)) {
2136 if (itemIndex == 0) {
2137 fFontOverrides.*flag = false;
2138 } else {
2139 fFontOverrides.*flag = true;
2140 (fFont.*setFlag)(itemIndex == 2);
2141 }
2142 paramsChanged = true;
2143 }
2144 };
Hal Canary02738a82019-01-21 18:51:32 +00002145
Ben Wagner9613e452019-01-23 10:34:59 -05002146 fontFlag("Fake Bold Glyphs",
2147 "Default\0No Fake Bold\0Fake Bold\0\0",
2148 &SkFontFields::fEmbolden,
2149 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002150
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002151 fontFlag("Baseline Snapping",
2152 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2153 &SkFontFields::fBaselineSnap,
2154 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2155
Ben Wagner9613e452019-01-23 10:34:59 -05002156 fontFlag("Linear Text",
2157 "Default\0No Linear Text\0Linear Text\0\0",
2158 &SkFontFields::fLinearMetrics,
2159 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002160
Ben Wagner9613e452019-01-23 10:34:59 -05002161 fontFlag("Subpixel Position Glyphs",
2162 "Default\0Pixel Text\0Subpixel Text\0\0",
2163 &SkFontFields::fSubpixel,
2164 &SkFont::isSubpixel, &SkFont::setSubpixel);
2165
2166 fontFlag("Embedded Bitmap Text",
2167 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2168 &SkFontFields::fEmbeddedBitmaps,
2169 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2170
2171 fontFlag("Force Auto-Hinting",
2172 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2173 &SkFontFields::fForceAutoHinting,
2174 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2175
2176 int edgingIdx = 0;
2177 if (fFontOverrides.fEdging) {
2178 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2179 }
2180 if (ImGui::Combo("Edging", &edgingIdx,
2181 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2182 {
2183 if (edgingIdx == 0) {
2184 fFontOverrides.fEdging = false;
2185 fFont.setEdging(SkFont::Edging::kAlias);
2186 } else {
2187 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2188 fFontOverrides.fEdging = true;
2189 }
2190 paramsChanged = true;
2191 }
2192
Ben Wagner15a8d572019-03-21 13:35:44 -04002193 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2194 if (fFontOverrides.fSize) {
2195 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002196 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002197 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002198 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002199 fFontOverrides.fSizeRange[0],
2200 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002201 "%.6f", 2.0f))
2202 {
Mike Reed3ae47332019-01-04 10:11:46 -05002203 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002204 paramsChanged = true;
2205 }
2206 }
2207
2208 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2209 if (fFontOverrides.fScaleX) {
2210 float scaleX = fFont.getScaleX();
2211 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2212 fFont.setScaleX(scaleX);
2213 paramsChanged = true;
2214 }
2215 }
2216
2217 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2218 if (fFontOverrides.fSkewX) {
2219 float skewX = fFont.getSkewX();
2220 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2221 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002222 paramsChanged = true;
2223 }
2224 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002225 }
2226
Mike Reed81f60ec2018-05-15 10:09:52 -04002227 {
2228 SkMetaData controls;
2229 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2230 if (ImGui::CollapsingHeader("Current Slide")) {
2231 SkMetaData::Iter iter(controls);
2232 const char* name;
2233 SkMetaData::Type type;
2234 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002235 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002236 if (type == SkMetaData::kScalar_Type) {
2237 float val[3];
2238 SkASSERT(count == 3);
2239 controls.findScalars(name, &count, val);
2240 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2241 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002242 }
Ben Wagner110c7032019-03-22 17:03:59 -04002243 } else if (type == SkMetaData::kBool_Type) {
2244 bool val;
2245 SkASSERT(count == 1);
2246 controls.findBool(name, &val);
2247 if (ImGui::Checkbox(name, &val)) {
2248 controls.setBool(name, val);
2249 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002250 }
2251 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002252 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002253 }
2254 }
2255 }
2256
Ben Wagner7a3c6742018-04-23 10:01:07 -04002257 if (fShowSlidePicker) {
2258 ImGui::SetNextTreeNodeOpen(true);
2259 }
Brian Osman79086b92017-02-10 13:36:16 -05002260 if (ImGui::CollapsingHeader("Slide")) {
2261 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002262 static ImVector<const char*> filteredSlideNames;
2263 static ImVector<int> filteredSlideIndices;
2264
Brian Osmanfce09c52017-11-14 15:32:20 -05002265 if (fShowSlidePicker) {
2266 ImGui::SetKeyboardFocusHere();
2267 fShowSlidePicker = false;
2268 }
2269
Brian Osman79086b92017-02-10 13:36:16 -05002270 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002271 filteredSlideNames.clear();
2272 filteredSlideIndices.clear();
2273 int filteredIndex = 0;
2274 for (int i = 0; i < fSlides.count(); ++i) {
2275 const char* slideName = fSlides[i]->getName().c_str();
2276 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2277 if (i == fCurrentSlide) {
2278 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002279 }
Brian Osmanf479e422017-11-08 13:11:36 -05002280 filteredSlideNames.push_back(slideName);
2281 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002282 }
Brian Osman79086b92017-02-10 13:36:16 -05002283 }
Brian Osmanf479e422017-11-08 13:11:36 -05002284
Brian Osmanf479e422017-11-08 13:11:36 -05002285 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2286 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002287 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002288 }
2289 }
Brian Osmana109e392017-02-24 09:49:14 -05002290
2291 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002292 ColorMode newMode = fColorMode;
2293 auto cmButton = [&](ColorMode mode, const char* label) {
2294 if (ImGui::RadioButton(label, mode == fColorMode)) {
2295 newMode = mode;
2296 }
2297 };
2298
2299 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002300 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2301 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002302 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002303
2304 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002305 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002306 }
2307
2308 // Pick from common gamuts:
2309 int primariesIdx = 4; // Default: Custom
2310 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2311 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2312 primariesIdx = i;
2313 break;
2314 }
2315 }
2316
Brian Osman03115dc2018-11-26 13:55:19 -05002317 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002318 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002319
Brian Osmana109e392017-02-24 09:49:14 -05002320 if (ImGui::Combo("Primaries", &primariesIdx,
2321 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2322 if (primariesIdx >= 0 && primariesIdx <= 3) {
2323 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2324 }
2325 }
2326
2327 // Allow direct editing of gamut
2328 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2329 }
Brian Osman207d4102019-01-10 09:40:58 -05002330
2331 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002332 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002333 if (ImGui::Checkbox("Pause", &isPaused)) {
2334 fAnimTimer.togglePauseResume();
2335 }
Brian Osman707d2022019-01-10 11:27:34 -05002336
2337 float speed = fAnimTimer.getSpeed();
2338 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2339 fAnimTimer.setSpeed(speed);
2340 }
Brian Osman207d4102019-01-10 09:40:58 -05002341 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002342
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002343 if (ImGui::CollapsingHeader("Shaders")) {
2344 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2345 GrContextOptions::ShaderCacheStrategy::kSkSL;
John Stiles7247b482021-03-08 10:40:35 -05002346
2347 int optLevel = sksl ? kShaderOptLevel_Source :
John Stiles7247b482021-03-08 10:40:35 -05002348 SkSL::gSkSLInliner ? kShaderOptLevel_Inline :
2349 SkSL::gSkSLOptimizer ? kShaderOptLevel_Optimize :
2350 kShaderOptLevel_Compile;
2351
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002352#if defined(SK_VULKAN)
2353 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2354#else
2355 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002356#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002357
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002358 // To re-load shaders from the currently active programs, we flush all
2359 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002360 static bool gLoadPending = false;
2361 if (gLoadPending) {
2362 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
Brian Osmanf0de96f2021-02-26 13:54:11 -05002363 const SkString& description, int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002364 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002365 entry.fKey = key;
2366 SkMD5 hash;
2367 hash.write(key->bytes(), key->size());
2368 SkMD5::Digest digest = hash.finish();
2369 for (int i = 0; i < 16; ++i) {
2370 entry.fKeyString.appendf("%02x", digest.data[i]);
2371 }
Brian Osmanf0de96f2021-02-26 13:54:11 -05002372 entry.fKeyDescription = description;
Brian Osman0b8bb882019-04-12 11:47:19 -04002373
Brian Osman9e4e4c72020-06-10 07:19:34 -04002374 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002375 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002376 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2377 entry.fInputs,
2378 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002379 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002380 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002381 fPersistentCache.foreach(collectShaders);
2382 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002383
2384#if defined(SK_VULKAN)
2385 if (isVulkan && !sksl) {
2386 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2387 for (auto& entry : fCachedShaders) {
2388 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2389 const SkSL::String& spirv(entry.fShader[i]);
2390 std::string disasm;
2391 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2392 &disasm);
2393 entry.fShader[i].assign(disasm);
2394 }
2395 }
2396 }
2397#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002398 }
2399
John Stilesa8f6b6f2021-03-05 16:00:42 -05002400 // Defer actually doing the View/Apply logic so that we can trigger an Apply when we
Brian Osman0b8bb882019-04-12 11:47:19 -04002401 // start or finish hovering on a tree node in the list below:
John Stilesa8f6b6f2021-03-05 16:00:42 -05002402 bool doView = ImGui::Button("View"); ImGui::SameLine();
2403 bool doApply = ImGui::Button("Apply Changes"); ImGui::SameLine();
John Stiles7247b482021-03-08 10:40:35 -05002404 bool doDump = ImGui::Button("Dump SkSL to resources/sksl/");
John Stilesa8f6b6f2021-03-05 16:00:42 -05002405
John Stiles7247b482021-03-08 10:40:35 -05002406 int newOptLevel = optLevel;
2407 ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source);
2408 ImGui::SameLine();
2409 ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile);
2410 ImGui::SameLine();
2411 ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize);
2412 ImGui::SameLine();
2413 ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline);
John Stilesa8f6b6f2021-03-05 16:00:42 -05002414
John Stiles7247b482021-03-08 10:40:35 -05002415 // If we are changing the compile mode, we want to reset the cache and redo
2416 // everything.
2417 if (doDump || newOptLevel != optLevel) {
2418 sksl = doDump || (newOptLevel == kShaderOptLevel_Source);
2419 SkSL::gSkSLOptimizer = (newOptLevel >= kShaderOptLevel_Optimize);
2420 SkSL::gSkSLInliner = (newOptLevel >= kShaderOptLevel_Inline);
John Stiles7247b482021-03-08 10:40:35 -05002421
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002422 params.fGrContextOptions.fShaderCacheStrategy =
2423 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2424 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2425 paramsChanged = true;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002426 doView = true;
2427
2428 fDeferredActions.push_back([=]() {
2429 // Reset the cache.
2430 fPersistentCache.reset();
2431 // Dump the cache once we have drawn a frame with it.
2432 if (doDump) {
2433 fDeferredActions.push_back([this]() {
2434 this->dumpShadersToResources();
2435 });
2436 }
2437 });
Brian Osmancbc33b82019-04-19 14:16:19 -04002438 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002439
2440 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002441 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002442 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2443 bool hovered = ImGui::IsItemHovered();
2444 if (hovered != entry.fHovered) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002445 // Force an Apply to patch the highlight shader in/out
Brian Osman0b8bb882019-04-12 11:47:19 -04002446 entry.fHovered = hovered;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002447 doApply = true;
Brian Osman0b8bb882019-04-12 11:47:19 -04002448 }
2449 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002450 auto stringBox = [](const char* label, std::string* str) {
2451 // Full width, and not too much space for each shader
2452 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2453 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2454 ImGui::InputTextMultiline(label, str, boxSize);
2455 };
Brian Osmanf0de96f2021-02-26 13:54:11 -05002456 if (ImGui::TreeNode("Key")) {
2457 ImGui::TextWrapped("%s", entry.fKeyDescription.c_str());
2458 ImGui::TreePop();
2459 }
Brian Osmaneb3fb902020-08-18 13:16:59 -04002460 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2461 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002462 ImGui::TreePop();
2463 }
2464 }
2465 ImGui::EndChild();
2466
John Stilesa8f6b6f2021-03-05 16:00:42 -05002467 if (doView) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002468 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002469 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002470 gLoadPending = true;
2471 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002472
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002473 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2474 // but I'm not sure anyone wants to do that.
2475 if (isVulkan && !sksl) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002476 doApply = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002477 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002478 if (doApply) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002479 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002480 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002481 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002482 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002483 if (entry.fHovered) {
2484 // The hovered item (if any) gets a special shader to make it
2485 // identifiable.
2486 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2487 switch (entry.fShaderType) {
2488 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2489 fragShader = build_sksl_highlight_shader();
2490 break;
2491 }
2492 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2493 fragShader = build_glsl_highlight_shader(
2494 *ctx->priv().caps()->shaderCaps());
2495 break;
2496 }
2497 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2498 fragShader = build_metal_highlight_shader(fragShader);
2499 break;
2500 }
2501 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002502 }
2503
Brian Osmana085a412019-04-25 09:44:43 -04002504 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2505 entry.fShader,
2506 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002507 kGrShaderTypeCount);
Brian Osmanf0de96f2021-02-26 13:54:11 -05002508 fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription);
Brian Osman0b8bb882019-04-12 11:47:19 -04002509
2510 entry.fShader[kFragment_GrShaderType] = backup;
2511 }
2512 }
2513 }
Brian Osman79086b92017-02-10 13:36:16 -05002514 }
Brian Salomon99a33902017-03-07 15:16:34 -05002515 if (paramsChanged) {
2516 fDeferredActions.push_back([=]() {
2517 fWindow->setRequestedDisplayParams(params);
2518 fWindow->inval();
2519 this->updateTitle();
2520 });
2521 }
Brian Osman79086b92017-02-10 13:36:16 -05002522 ImGui::End();
2523 }
2524
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002525 if (gShaderErrorHandler.fErrors.count()) {
2526 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002527 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002528 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2529 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002530 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2531 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2532 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2533 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002534 }
2535 ImGui::End();
2536 gShaderErrorHandler.reset();
2537 }
2538
Brian Osmanf6877092017-02-13 09:39:57 -05002539 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002540 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2541 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002542 static int zoomFactor = 8;
2543 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002544 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002545 }
2546 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2547 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002548 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002549 }
Brian Osmanf6877092017-02-13 09:39:57 -05002550
Ben Wagner3627d2e2018-06-26 14:23:20 -04002551 if (!fZoomWindowFixed) {
2552 ImVec2 mousePos = ImGui::GetMousePos();
2553 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2554 }
2555 SkScalar x = fZoomWindowLocation.x();
2556 SkScalar y = fZoomWindowLocation.y();
2557 int xInt = SkScalarRoundToInt(x);
2558 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002559 ImVec2 avail = ImGui::GetContentRegionAvail();
2560
Brian Osmanead517d2017-11-13 15:36:36 -05002561 uint32_t pixel = 0;
2562 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002563 auto dContext = fWindow->directContext();
2564 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002565 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002566 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002567 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002568 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002569 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2570 }
2571
Greg Danielfbc60b72020-11-03 17:27:02 -05002572 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002573 // Translate so the region of the image that's under the mouse cursor is centered
2574 // in the zoom canvas:
2575 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002576 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2577 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002578 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002579
2580 SkPaint outline;
2581 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002582 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002583 });
Brian Osmanf6877092017-02-13 09:39:57 -05002584 }
2585
2586 ImGui::End();
2587 }
Brian Osman79086b92017-02-10 13:36:16 -05002588}
2589
John Stilesa8f6b6f2021-03-05 16:00:42 -05002590void Viewer::dumpShadersToResources() {
2591 // Sort the list of cached shaders so we can maintain some minimal level of consistency.
2592 // It doesn't really matter, but it will keep files from switching places unpredictably.
2593 std::vector<const CachedShader*> shaders;
2594 shaders.reserve(fCachedShaders.size());
2595 for (const CachedShader& shader : fCachedShaders) {
2596 shaders.push_back(&shader);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002597 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002598
2599 std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) {
2600 return std::tie(a->fShader[kFragment_GrShaderType], a->fShader[kVertex_GrShaderType]) <
2601 std::tie(b->fShader[kFragment_GrShaderType], b->fShader[kVertex_GrShaderType]);
2602 });
2603
2604 // Make the resources/sksl/SlideName/ directory.
2605 SkString directory = SkStringPrintf("%ssksl/%s",
2606 GetResourcePath().c_str(),
2607 fSlides[fCurrentSlide]->getName().c_str());
2608 if (!sk_mkdir(directory.c_str())) {
2609 SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str());
2610 return;
2611 }
2612
2613 int index = 0;
2614 for (const auto& entry : shaders) {
2615 SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index);
2616 FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag);
2617 if (vertFile) {
2618 const SkSL::String& vertText = entry->fShader[kVertex_GrShaderType];
2619 SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile));
2620 sk_fclose(vertFile);
2621 } else {
2622 SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str());
2623 }
2624
2625 SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index);
2626 FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag);
2627 if (fragFile) {
2628 const SkSL::String& fragText = entry->fShader[kFragment_GrShaderType];
2629 SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile));
2630 sk_fclose(fragFile);
2631 } else {
2632 SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str());
2633 }
2634
2635 ++index;
2636 }
2637}
2638
2639void Viewer::onIdle() {
2640 SkTArray<std::function<void()>> actionsToRun;
2641 actionsToRun.swap(fDeferredActions);
2642
2643 for (const auto& fn : actionsToRun) {
2644 fn();
2645 }
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002646
Brian Osman56a24812017-12-19 11:15:16 -05002647 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002648 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002649 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002650 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002651
Brian Osman79086b92017-02-10 13:36:16 -05002652 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002653 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2654 // not be visible, though. So we need to redraw if there is at least one visible window, or
2655 // more than one active window. Newly created windows are active but not visible for one frame
2656 // while they determine their layout and sizing.
2657 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2658 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002659 fWindow->inval();
2660 }
jvanverth9f372462016-04-06 06:08:59 -07002661}
liyuqiane5a6cd92016-05-27 08:52:52 -07002662
Florin Malitab632df72018-06-18 21:23:06 -04002663template <typename OptionsFunc>
2664static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2665 OptionsFunc&& optionsFunc) {
2666 writer.beginObject();
2667 {
2668 writer.appendString(kName , name);
2669 writer.appendString(kValue, value);
2670
2671 writer.beginArray(kOptions);
2672 {
2673 optionsFunc(writer);
2674 }
2675 writer.endArray();
2676 }
2677 writer.endObject();
2678}
2679
2680
liyuqiane5a6cd92016-05-27 08:52:52 -07002681void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002682 if (!fWindow) {
2683 return;
2684 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002685 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002686 return; // Surface hasn't been created yet.
2687 }
2688
Florin Malitab632df72018-06-18 21:23:06 -04002689 SkDynamicMemoryWStream memStream;
2690 SkJSONWriter writer(&memStream);
2691 writer.beginArray();
2692
liyuqianb73c24b2016-06-03 08:47:23 -07002693 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002694 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2695 [this](SkJSONWriter& writer) {
2696 for(const auto& slide : fSlides) {
2697 writer.appendString(slide->getName().c_str());
2698 }
2699 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002700
liyuqianb73c24b2016-06-03 08:47:23 -07002701 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002702 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2703 [](SkJSONWriter& writer) {
2704 for (const auto& str : kBackendTypeStrings) {
2705 writer.appendString(str);
2706 }
2707 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002708
csmartdalton578f0642017-02-24 16:04:47 -07002709 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002710 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2711 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2712 [this](SkJSONWriter& writer) {
2713 writer.appendS32(0);
2714
2715 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2716 return;
2717 }
2718
2719 for (int msaa : {4, 8, 16}) {
2720 writer.appendS32(msaa);
2721 }
2722 });
csmartdalton578f0642017-02-24 16:04:47 -07002723
csmartdalton61cd31a2017-02-27 17:00:53 -07002724 // Path renderer state
2725 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002726 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2727 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002728 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002729 if (!ctx) {
2730 writer.appendString("Software");
2731 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002732 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002733 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2734 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002735 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002736 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002737 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002738 }
Florin Malitab632df72018-06-18 21:23:06 -04002739 if (caps->shaderCaps()->pathRenderingSupport()) {
2740 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002741 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002742 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002743 }
2744 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002745 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2746 writer.appendString(
2747 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2748 }
2749 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2750 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002751 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002752 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002753 }
2754 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002755
liyuqianb73c24b2016-06-03 08:47:23 -07002756 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002757 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2758 [this](SkJSONWriter& writer) {
2759 writer.appendString(kSoftkeyHint);
2760 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2761 writer.appendString(softkey.c_str());
2762 }
2763 });
liyuqianb73c24b2016-06-03 08:47:23 -07002764
Florin Malitab632df72018-06-18 21:23:06 -04002765 writer.endArray();
2766 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002767
Florin Malitab632df72018-06-18 21:23:06 -04002768 auto data = memStream.detachAsData();
2769
2770 // TODO: would be cool to avoid this copy
2771 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2772
2773 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002774}
2775
2776void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002777 // For those who will add more features to handle the state change in this function:
2778 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2779 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2780 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002781 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002782 for (int i = 0; i < fSlides.count(); ++i) {
2783 if (fSlides[i]->getName().equals(stateValue)) {
2784 this->setCurrentSlide(i);
2785 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002786 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002787 }
Florin Malitaab99c342018-01-16 16:23:03 -05002788
2789 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002790 } else if (stateName.equals(kBackendStateName)) {
2791 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2792 if (stateValue.equals(kBackendTypeStrings[i])) {
2793 if (fBackendType != i) {
2794 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002795 for(auto& slide : fSlides) {
2796 slide->gpuTeardown();
2797 }
liyuqian6cb70252016-06-02 12:16:25 -07002798 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002799 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002800 }
2801 break;
2802 }
2803 }
csmartdalton578f0642017-02-24 16:04:47 -07002804 } else if (stateName.equals(kMSAAStateName)) {
2805 DisplayParams params = fWindow->getRequestedDisplayParams();
2806 int sampleCount = atoi(stateValue.c_str());
2807 if (sampleCount != params.fMSAASampleCount) {
2808 params.fMSAASampleCount = sampleCount;
2809 fWindow->setRequestedDisplayParams(params);
2810 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002811 this->updateTitle();
2812 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002813 }
2814 } else if (stateName.equals(kPathRendererStateName)) {
2815 DisplayParams params = fWindow->getRequestedDisplayParams();
2816 for (const auto& pair : gPathRendererNames) {
2817 if (pair.second == stateValue.c_str()) {
2818 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2819 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2820 fWindow->setRequestedDisplayParams(params);
2821 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002822 this->updateTitle();
2823 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002824 }
2825 break;
2826 }
csmartdalton578f0642017-02-24 16:04:47 -07002827 }
liyuqianb73c24b2016-06-03 08:47:23 -07002828 } else if (stateName.equals(kSoftkeyStateName)) {
2829 if (!stateValue.equals(kSoftkeyHint)) {
2830 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002831 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002832 }
liyuqian2edb0f42016-07-06 14:11:32 -07002833 } else if (stateName.equals(kRefreshStateName)) {
2834 // This state is actually NOT in the UI state.
2835 // We use this to allow Android to quickly set bool fRefresh.
2836 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002837 } else {
2838 SkDebugf("Unknown stateName: %s", stateName.c_str());
2839 }
2840}
Brian Osman79086b92017-02-10 13:36:16 -05002841
Hal Canaryb1f411a2019-08-29 10:39:22 -04002842bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002843 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002844}
2845
Hal Canaryb1f411a2019-08-29 10:39:22 -04002846bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002847 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002848 fWindow->inval();
2849 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002850 } else {
2851 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002852 }
Brian Osman79086b92017-02-10 13:36:16 -05002853}