blob: 1f35021e7efeced1d278ddb1b8df3b49150664aa [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
Brian Osman5e7fbfd2019-05-03 13:13:35 -040068class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
69public:
70 void compileError(const char* shader, const char* errors) override {
71 fShaders.push_back(SkString(shader));
72 fErrors.push_back(SkString(errors));
73 }
74
75 void reset() {
76 fShaders.reset();
77 fErrors.reset();
78 }
79
80 SkTArray<SkString> fShaders;
81 SkTArray<SkString> fErrors;
82};
83
84static CapturingShaderErrorHandler gShaderErrorHandler;
85
Brian Osmanf847f312020-06-18 14:18:27 -040086GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
87
jvanverth34524262016-05-04 13:49:13 -070088using namespace sk_app;
89
csmartdalton61cd31a2017-02-27 17:00:53 -070090static std::map<GpuPathRenderers, std::string> gPathRendererNames;
91
jvanverth9f372462016-04-06 06:08:59 -070092Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070093 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070094}
95
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060096static DEFINE_string(slide, "", "Start on this sample.");
97static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050098
Jim Van Verth682a2f42020-05-13 16:54:09 -040099#ifdef SK_GL
100#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700101#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400102#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700103#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400104#ifdef SK_VULKAN
105#define VK_BACKEND_STR ", \"vk\""
106#else
107#define VK_BACKEND_STR
108#endif
109#ifdef SK_METAL
110#define MTL_BACKEND_STR ", \"mtl\""
111#else
112#define MTL_BACKEND_STR
113#endif
114#ifdef SK_DIRECT3D
115#define D3D_BACKEND_STR ", \"d3d\""
116#else
117#define D3D_BACKEND_STR
118#endif
119#ifdef SK_DAWN
120#define DAWN_BACKEND_STR ", \"dawn\""
121#else
122#define DAWN_BACKEND_STR
123#endif
124#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
125#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
126 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700127
Brian Osman2dd96932016-10-18 15:33:53 -0400128static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700129
Mike Klein5b3f3432019-03-21 11:42:21 -0500130static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -0700131
Mike Klein84836b72019-03-21 11:31:36 -0500132static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700133
Mike Klein84836b72019-03-21 11:31:36 -0500134static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400135
Mike Kleinc6142d82019-03-25 10:54:59 -0500136static DEFINE_string2(match, m, nullptr,
137 "[~][^]substring[$] [...] of name to run.\n"
138 "Multiple matches may be separated by spaces.\n"
139 "~ causes a matching name to always be skipped\n"
140 "^ requires the start of the name to match\n"
141 "$ requires the end of the name to match\n"
142 "^ and $ requires an exact match\n"
143 "If a name does not match any list entry,\n"
144 "it is skipped unless some list entry starts with ~");
145
Mike Klein19fb3972019-03-21 13:08:08 -0500146#if defined(SK_BUILD_FOR_ANDROID)
147 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500148 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
149 static DEFINE_string(lotties, "/data/local/tmp/lotties",
150 "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400151 static DEFINE_string(rives, "/data/local/tmp/rives",
152 "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500153#else
154 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500155 static DEFINE_string(skps, "skps", "Directory to read skps from.");
156 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400157 static DEFINE_string(rives, "rives", "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500158#endif
159
Mike Kleinc6142d82019-03-25 10:54:59 -0500160static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
161
162static DEFINE_int_2(threads, j, -1,
163 "Run threadsafe tests on a threadpool with this many extra threads, "
164 "defaulting to one extra thread per core.");
165
Jim Van Verth7b558182019-11-14 16:47:01 -0500166static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
167
Chris Daltonc8877332020-01-06 09:48:30 -0700168static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500169static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
170static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500171static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500172static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500173static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500174
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400175#ifndef SK_GL
176static_assert(false, "viewer requires GL backend for raster.")
177#endif
178
Brian Salomon194db172017-08-17 14:37:06 -0400179const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700180 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400181#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
182 "ANGLE",
183#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400184#ifdef SK_DAWN
185 "Dawn",
186#endif
jvanverth063ece72016-06-17 09:29:14 -0700187#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700188 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700189#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400190#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500191 "Metal",
192#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400193#ifdef SK_DIRECT3D
194 "Direct3D",
195#endif
csmartdalton578f0642017-02-24 16:04:47 -0700196 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700197};
198
bsalomon6c471f72016-07-26 12:56:32 -0700199static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400200#ifdef SK_DAWN
201 if (0 == strcmp(str, "dawn")) {
202 return sk_app::Window::kDawn_BackendType;
203 } else
204#endif
bsalomon6c471f72016-07-26 12:56:32 -0700205#ifdef SK_VULKAN
206 if (0 == strcmp(str, "vk")) {
207 return sk_app::Window::kVulkan_BackendType;
208 } else
209#endif
Brian Salomon194db172017-08-17 14:37:06 -0400210#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
211 if (0 == strcmp(str, "angle")) {
212 return sk_app::Window::kANGLE_BackendType;
213 } else
214#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400215#ifdef SK_METAL
216 if (0 == strcmp(str, "mtl")) {
217 return sk_app::Window::kMetal_BackendType;
218 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500219#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400220#ifdef SK_DIRECT3D
221 if (0 == strcmp(str, "d3d")) {
222 return sk_app::Window::kDirect3D_BackendType;
223 } else
224#endif
225
bsalomon6c471f72016-07-26 12:56:32 -0700226 if (0 == strcmp(str, "gl")) {
227 return sk_app::Window::kNativeGL_BackendType;
228 } else if (0 == strcmp(str, "sw")) {
229 return sk_app::Window::kRaster_BackendType;
230 } else {
231 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
232 return sk_app::Window::kRaster_BackendType;
233 }
234}
235
Brian Osmana109e392017-02-24 09:49:14 -0500236static SkColorSpacePrimaries gSrgbPrimaries = {
237 0.64f, 0.33f,
238 0.30f, 0.60f,
239 0.15f, 0.06f,
240 0.3127f, 0.3290f };
241
242static SkColorSpacePrimaries gAdobePrimaries = {
243 0.64f, 0.33f,
244 0.21f, 0.71f,
245 0.15f, 0.06f,
246 0.3127f, 0.3290f };
247
248static SkColorSpacePrimaries gP3Primaries = {
249 0.680f, 0.320f,
250 0.265f, 0.690f,
251 0.150f, 0.060f,
252 0.3127f, 0.3290f };
253
254static SkColorSpacePrimaries gRec2020Primaries = {
255 0.708f, 0.292f,
256 0.170f, 0.797f,
257 0.131f, 0.046f,
258 0.3127f, 0.3290f };
259
260struct NamedPrimaries {
261 const char* fName;
262 SkColorSpacePrimaries* fPrimaries;
263} gNamedPrimaries[] = {
264 { "sRGB", &gSrgbPrimaries },
265 { "AdobeRGB", &gAdobePrimaries },
266 { "P3", &gP3Primaries },
267 { "Rec. 2020", &gRec2020Primaries },
268};
269
270static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
271 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
272}
273
Brian Osman70d2f432017-11-08 09:54:10 -0500274static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
275 // In raster mode, we still use GL for the window.
276 // This lets us render the GUI faster (and correct).
277 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
278}
279
Jim Van Verth74826c82019-03-01 14:37:30 -0500280class NullSlide : public Slide {
281 SkISize getDimensions() const override {
282 return SkISize::Make(640, 480);
283 }
284
285 void draw(SkCanvas* canvas) override {
286 canvas->clear(0xffff11ff);
287 }
288};
289
John Stiles31964fd2020-05-05 16:05:47 -0400290static const char kName[] = "name";
291static const char kValue[] = "value";
292static const char kOptions[] = "options";
293static const char kSlideStateName[] = "Slide";
294static const char kBackendStateName[] = "Backend";
295static const char kMSAAStateName[] = "MSAA";
296static const char kPathRendererStateName[] = "Path renderer";
297static const char kSoftkeyStateName[] = "Softkey";
298static const char kSoftkeyHint[] = "Please select a softkey";
299static const char kON[] = "ON";
300static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700301
Mike Reed862818b2020-03-21 15:07:13 -0400302extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500303extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500304extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400305
jvanverth34524262016-05-04 13:49:13 -0700306Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500307 : fCurrentSlide(-1)
308 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500309 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400310 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500311 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500312 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500313 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500314 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400315 , fZoomWindowFixed(false)
316 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500317 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400318 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700319 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500320 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500321 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500322 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500323 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500324 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700325 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400326 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400327 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400328 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500329 , fTiled(false)
330 , fDrawTileBoundaries(false)
331 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400332 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700333{
Greg Daniel285db442016-10-14 09:12:53 -0400334 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700335
Chris Dalton37ae4b02019-12-28 14:51:11 -0700336 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600337 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500338 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500339 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600340 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600341 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500342 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700343
jvanverth2bb3b6d2016-04-08 07:24:09 -0700344 SkDebugf("Command line arguments: ");
345 for (int i = 1; i < argc; ++i) {
346 SkDebugf("%s ", argv[i]);
347 }
348 SkDebugf("\n");
349
Mike Klein88544fb2019-03-20 10:50:33 -0500350 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500351#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400352 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500353#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700354
Mike Reed862818b2020-03-21 15:07:13 -0400355 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500356 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500357 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400358
Mike Klein19cc0f62019-03-22 15:30:07 -0500359 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500360
Brian Osmanbc8150f2017-07-24 11:38:01 -0400361 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400362 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400363
bsalomon6c471f72016-07-26 12:56:32 -0700364 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700365 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700366
csmartdalton578f0642017-02-24 16:04:47 -0700367 DisplayParams displayParams;
368 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500369 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700370 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400371 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400372 displayParams.fGrContextOptions.fShaderCacheStrategy =
373 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400374 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
375 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700376 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400377 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500378 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700379
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500380 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500381 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500382
Brian Osman56a24812017-12-19 11:15:16 -0500383 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500384 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500385 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
386 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
387 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
388
jvanverth9f372462016-04-06 06:08:59 -0700389 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700390 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500391 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500392 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500393 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700394
brianosman622c8d52016-05-10 06:50:49 -0700395 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500396 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
397 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
398 fWindow->inval();
399 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500400 // Command to jump directly to the slide picker and give it focus
401 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
402 this->fShowImGuiDebugWindow = true;
403 this->fShowSlidePicker = true;
404 fWindow->inval();
405 });
406 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400407 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500408 this->fShowImGuiDebugWindow = true;
409 this->fShowSlidePicker = true;
410 fWindow->inval();
411 });
Brian Osman79086b92017-02-10 13:36:16 -0500412 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
413 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
414 fWindow->inval();
415 });
Brian Osmanf6877092017-02-13 09:39:57 -0500416 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
417 this->fShowZoomWindow = !this->fShowZoomWindow;
418 fWindow->inval();
419 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400420 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
421 this->fZoomWindowFixed = !this->fZoomWindowFixed;
422 fWindow->inval();
423 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400424 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400425 DisplayParams params = fWindow->getRequestedDisplayParams();
426 params.fDisableVsync = !params.fDisableVsync;
427 fWindow->setRequestedDisplayParams(params);
428 this->updateTitle();
429 fWindow->inval();
430 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400431 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
432 DisplayParams params = fWindow->getRequestedDisplayParams();
433 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
434 fWindow->setRequestedDisplayParams(params);
435 this->updateTitle();
436 fWindow->inval();
437 });
Mike Reedf702ed42019-07-22 17:00:49 -0400438 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
439 fRefresh = !fRefresh;
440 fWindow->inval();
441 });
brianosman622c8d52016-05-10 06:50:49 -0700442 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500443 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700444 fWindow->inval();
445 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400446 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500447 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400448 this->updateTitle();
449 fWindow->inval();
450 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500451 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500452 switch (fColorMode) {
453 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500454 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500455 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500456 case ColorMode::kColorManaged8888:
457 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500458 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500459 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400460 this->setColorMode(ColorMode::kColorManagedF16Norm);
461 break;
462 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500463 this->setColorMode(ColorMode::kLegacy);
464 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500465 }
brianosman622c8d52016-05-10 06:50:49 -0700466 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700467 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
468 DisplayParams params = fWindow->getRequestedDisplayParams();
469 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
470 fWindow->setRequestedDisplayParams(params);
471 fWindow->inval();
472 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400473 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500474 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700475 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400476 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500477 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700478 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400479 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700480 this->changeZoomLevel(1.f / 32.f);
481 fWindow->inval();
482 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400483 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700484 this->changeZoomLevel(-1.f / 32.f);
485 fWindow->inval();
486 });
jvanverthaf236b52016-05-20 06:01:06 -0700487 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400488 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
489 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500490 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400491#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
492 if (newBackend == sk_app::Window::kVulkan_BackendType) {
493 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
494 sk_app::Window::kBackendTypeCount);
495 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
496 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500497 }
498#endif
Brian Osman621491e2017-02-28 15:45:01 -0500499 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700500 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500501 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
502 fSaveToSKP = true;
503 fWindow->inval();
504 });
Mike Reed376d8122019-03-14 11:39:02 -0400505 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
506 fShowSlideDimensions = !fShowSlideDimensions;
507 fWindow->inval();
508 });
Ben Wagner37c54032018-04-13 14:30:23 -0400509 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
510 DisplayParams params = fWindow->getRequestedDisplayParams();
511 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400512 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
513 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
514 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400515 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
516 } else {
517 switch (params.fSurfaceProps.pixelGeometry()) {
518 case kUnknown_SkPixelGeometry:
519 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
520 break;
521 case kRGB_H_SkPixelGeometry:
522 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
523 break;
524 case kBGR_H_SkPixelGeometry:
525 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
526 break;
527 case kRGB_V_SkPixelGeometry:
528 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
529 break;
530 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400531 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
532 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400533 break;
534 }
535 }
536 fWindow->setRequestedDisplayParams(params);
537 this->updateTitle();
538 fWindow->inval();
539 });
Ben Wagner9613e452019-01-23 10:34:59 -0500540 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500541 if (!fFontOverrides.fHinting) {
542 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400543 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500544 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500545 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400546 case SkFontHinting::kNone:
547 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500548 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400549 case SkFontHinting::kSlight:
550 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500551 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400552 case SkFontHinting::kNormal:
553 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500554 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400555 case SkFontHinting::kFull:
556 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500557 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500558 break;
559 }
560 }
561 this->updateTitle();
562 fWindow->inval();
563 });
564 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500565 if (!fPaintOverrides.fAntiAlias) {
566 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
567 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500568 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500569 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500570 } else {
571 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500572 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500573 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500574 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400575 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500576 break;
577 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500578 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500579 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400580 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500581 break;
582 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500583 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400584 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500585 break;
586 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500587 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
588 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500589 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
590 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500591 break;
592 }
593 }
594 this->updateTitle();
595 fWindow->inval();
596 });
Ben Wagner37c54032018-04-13 14:30:23 -0400597 fCommands.addCommand('D', "Modes", "DFT", [this]() {
598 DisplayParams params = fWindow->getRequestedDisplayParams();
599 uint32_t flags = params.fSurfaceProps.flags();
600 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
601 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
602 fWindow->setRequestedDisplayParams(params);
603 this->updateTitle();
604 fWindow->inval();
605 });
Ben Wagner9613e452019-01-23 10:34:59 -0500606 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500607 if (!fFontOverrides.fEdging) {
608 fFontOverrides.fEdging = true;
609 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500610 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500611 switch (fFont.getEdging()) {
612 case SkFont::Edging::kAlias:
613 fFont.setEdging(SkFont::Edging::kAntiAlias);
614 break;
615 case SkFont::Edging::kAntiAlias:
616 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
617 break;
618 case SkFont::Edging::kSubpixelAntiAlias:
619 fFont.setEdging(SkFont::Edging::kAlias);
620 fFontOverrides.fEdging = false;
621 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500622 }
623 }
624 this->updateTitle();
625 fWindow->inval();
626 });
Ben Wagner9613e452019-01-23 10:34:59 -0500627 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500628 if (!fFontOverrides.fSubpixel) {
629 fFontOverrides.fSubpixel = true;
630 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500631 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500632 if (!fFont.isSubpixel()) {
633 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500634 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500635 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500636 }
637 }
638 this->updateTitle();
639 fWindow->inval();
640 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400641 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
642 if (!fFontOverrides.fBaselineSnap) {
643 fFontOverrides.fBaselineSnap = true;
644 fFont.setBaselineSnap(false);
645 } else {
646 if (!fFont.isBaselineSnap()) {
647 fFont.setBaselineSnap(true);
648 } else {
649 fFontOverrides.fBaselineSnap = false;
650 }
651 }
652 this->updateTitle();
653 fWindow->inval();
654 });
Brian Osman805a7272018-05-02 15:40:20 -0400655 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
656 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
657 : kPerspective_Real;
658 this->updateTitle();
659 fWindow->inval();
660 });
661 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
662 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
663 : kPerspective_Off;
664 this->updateTitle();
665 fWindow->inval();
666 });
Brian Osman207d4102019-01-10 09:40:58 -0500667 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
668 fAnimTimer.togglePauseResume();
669 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400670 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
671 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500672 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400673 fWindow->inval();
674 });
Mike Reed59295352020-03-12 13:56:34 -0400675 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
676 fDrawViaSerialize = !fDrawViaSerialize;
677 this->updateTitle();
678 fWindow->inval();
679 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500680 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400681 gUseSkVMBlitter = !gUseSkVMBlitter;
682 this->updateTitle();
683 fWindow->inval();
684 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500685 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
686 gSkVMAllowJIT = !gSkVMAllowJIT;
687 this->updateTitle();
688 fWindow->inval();
689 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500690
jvanverth2bb3b6d2016-04-08 07:24:09 -0700691 // set up slides
692 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500693 if (FLAGS_list) {
694 this->listNames();
695 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700696
Brian Osman9bb47cf2018-04-26 15:55:00 -0400697 fPerspectivePoints[0].set(0, 0);
698 fPerspectivePoints[1].set(1, 0);
699 fPerspectivePoints[2].set(0, 1);
700 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700701 fAnimTimer.run();
702
Hal Canaryc465d132017-12-08 10:21:31 -0500703 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500704 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500705 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500706 }
707 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500708
jongdeok.kim804f17e2019-02-26 14:39:23 +0900709 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500710 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700711}
712
jvanverth34524262016-05-04 13:49:13 -0700713void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400714 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
715 static const struct {
716 const char* fExtension;
717 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500718 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400719 const SlideFactory fFactory;
720 } gExternalSlidesInfo[] = {
721 { ".skp", "skp-dir", FLAGS_skps,
722 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
723 return sk_make_sp<SKPSlide>(name, path);}
724 },
725 { ".jpg", "jpg-dir", FLAGS_jpgs,
726 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
727 return sk_make_sp<ImageSlide>(name, path);}
728 },
Florin Malita87ccf332018-05-04 12:23:24 -0400729#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400730 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400731 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
732 return sk_make_sp<SkottieSlide>(name, path);}
733 },
Florin Malita87ccf332018-05-04 12:23:24 -0400734#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400735 #if defined(SK_ENABLE_SKRIVE)
736 { ".flr", "skrive-dir", FLAGS_rives,
737 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
738 return sk_make_sp<SkRiveSlide>(name, path);}
739 },
740 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400741#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400742 { ".svg", "svg-dir", FLAGS_svgs,
743 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
744 return sk_make_sp<SvgSlide>(name, path);}
745 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400746#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400747 };
jvanverthc265a922016-04-08 12:51:45 -0700748
Brian Salomon343553a2018-09-05 15:41:23 -0400749 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700750
Mike Klein88544fb2019-03-20 10:50:33 -0500751 const auto addSlide =
752 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
753 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
754 return;
755 }
liyuqian6f163d22016-06-13 12:26:45 -0700756
Mike Klein88544fb2019-03-20 10:50:33 -0500757 if (auto slide = fact(name, path)) {
758 dirSlides.push_back(slide);
759 fSlides.push_back(std::move(slide));
760 }
761 };
Florin Malita76a076b2018-02-15 18:40:48 -0500762
Florin Malita38792ce2018-05-08 10:36:18 -0400763 if (!FLAGS_file.isEmpty()) {
764 // single file mode
765 const SkString file(FLAGS_file[0]);
766
767 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
768 for (const auto& sinfo : gExternalSlidesInfo) {
769 if (file.endsWith(sinfo.fExtension)) {
770 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
771 return;
772 }
773 }
774
775 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
776 } else {
777 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
778 }
779
780 return;
781 }
782
783 // Bisect slide.
784 if (!FLAGS_bisect.isEmpty()) {
785 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500786 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400787 if (FLAGS_bisect.count() >= 2) {
788 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
789 bisect->onChar(*ch);
790 }
791 }
792 fSlides.push_back(std::move(bisect));
793 }
794 }
795
796 // GMs
797 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400798 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400799 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500800 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400801 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400802 fSlides.push_back(std::move(slide));
803 }
Florin Malita38792ce2018-05-08 10:36:18 -0400804 }
805 // reverse gms
806 int numGMs = fSlides.count() - firstGM;
807 for (int i = 0; i < numGMs/2; ++i) {
808 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
809 }
810
811 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400812 for (const SampleFactory factory : SampleRegistry::Range()) {
813 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500814 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400815 fSlides.push_back(slide);
816 }
Florin Malita38792ce2018-05-08 10:36:18 -0400817 }
818
Brian Osman7c979f52019-02-12 13:27:51 -0500819 // Particle demo
820 {
821 // TODO: Convert this to a sample
822 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500823 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500824 fSlides.push_back(std::move(slide));
825 }
826 }
827
Brian Osmand927bd22019-12-18 11:23:12 -0500828 // Runtime shader editor
829 {
830 sk_sp<Slide> slide(new SkSLSlide());
831 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
832 fSlides.push_back(std::move(slide));
833 }
834 }
835
Florin Malita0ffa3222018-04-05 14:34:45 -0400836 for (const auto& info : gExternalSlidesInfo) {
837 for (const auto& flag : info.fFlags) {
838 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
839 // single file
840 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
841 } else {
842 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400843 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400844 SkTArray<SkString> sortedFilenames;
845 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400846 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400847 sortedFilenames.push_back(name);
848 }
849 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400850 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000851 [](const SkString& a, const SkString& b) {
852 return strcmp(a.c_str(), b.c_str()) < 0;
853 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400854 }
855 for (const SkString& filename : sortedFilenames) {
856 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
857 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400858 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400859 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400860 if (!dirSlides.empty()) {
861 fSlides.push_back(
862 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
863 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500864 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400865 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400866 }
867 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500868
869 if (!fSlides.count()) {
870 sk_sp<Slide> slide(new NullSlide());
871 fSlides.push_back(std::move(slide));
872 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700873}
874
875
jvanverth34524262016-05-04 13:49:13 -0700876Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400877 for(auto& slide : fSlides) {
878 slide->gpuTeardown();
879 }
880
jvanverth9f372462016-04-06 06:08:59 -0700881 fWindow->detach();
882 delete fWindow;
883}
884
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500885struct SkPaintTitleUpdater {
886 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
887 void append(const char* s) {
888 if (fCount == 0) {
889 fTitle->append(" {");
890 } else {
891 fTitle->append(", ");
892 }
893 fTitle->append(s);
894 ++fCount;
895 }
896 void done() {
897 if (fCount > 0) {
898 fTitle->append("}");
899 }
900 }
901 SkString* fTitle;
902 int fCount;
903};
904
brianosman05de2162016-05-06 13:28:57 -0700905void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700906 if (!fWindow) {
907 return;
908 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500909 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700910 return; // Surface hasn't been created yet.
911 }
912
jvanverth34524262016-05-04 13:49:13 -0700913 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700914 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700915
Mike Kleine5acd752019-03-22 09:57:16 -0500916 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400917 if (gSkForceAnalyticAA) {
918 title.append(" <FAAA>");
919 } else {
920 title.append(" <AAA>");
921 }
922 }
Mike Reed59295352020-03-12 13:56:34 -0400923 if (fDrawViaSerialize) {
924 title.append(" <serialize>");
925 }
Mike Reed862818b2020-03-21 15:07:13 -0400926 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500927 title.append(" <SkVMBlitter>");
928 }
929 if (!gSkVMAllowJIT) {
930 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400931 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400932
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500933 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500934 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
935 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400936 const char* on, const char* off)
937 {
Ben Wagner9613e452019-01-23 10:34:59 -0500938 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400939 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500940 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400941 };
942
Ben Wagner9613e452019-01-23 10:34:59 -0500943 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
944 const char* on, const char* off)
945 {
946 if (fFontOverrides.*flag) {
947 paintTitle.append((fFont.*isFlag)() ? on : off);
948 }
949 };
950
951 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
952 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
953
954 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
955 "Force Autohint", "No Force Autohint");
956 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400957 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500958 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
959 "Linear Metrics", "Non-Linear Metrics");
960 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
961 "Bitmap Text", "No Bitmap Text");
962 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
963
964 if (fFontOverrides.fEdging) {
965 switch (fFont.getEdging()) {
966 case SkFont::Edging::kAlias:
967 paintTitle.append("Alias Text");
968 break;
969 case SkFont::Edging::kAntiAlias:
970 paintTitle.append("Antialias Text");
971 break;
972 case SkFont::Edging::kSubpixelAntiAlias:
973 paintTitle.append("Subpixel Antialias Text");
974 break;
975 }
976 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400977
Mike Reed3ae47332019-01-04 10:11:46 -0500978 if (fFontOverrides.fHinting) {
979 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400980 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500981 paintTitle.append("No Hinting");
982 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400983 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500984 paintTitle.append("Slight Hinting");
985 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400986 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500987 paintTitle.append("Normal Hinting");
988 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400989 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500990 paintTitle.append("Full Hinting");
991 break;
992 }
993 }
994 paintTitle.done();
995
Brian Osman92004802017-03-06 11:47:26 -0500996 switch (fColorMode) {
997 case ColorMode::kLegacy:
998 title.append(" Legacy 8888");
999 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001000 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001001 title.append(" ColorManaged 8888");
1002 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001003 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001004 title.append(" ColorManaged F16");
1005 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001006 case ColorMode::kColorManagedF16Norm:
1007 title.append(" ColorManaged F16 Norm");
1008 break;
Brian Osman92004802017-03-06 11:47:26 -05001009 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001010
Brian Osman92004802017-03-06 11:47:26 -05001011 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001012 int curPrimaries = -1;
1013 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1014 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1015 curPrimaries = i;
1016 break;
1017 }
1018 }
Brian Osman03115dc2018-11-26 13:55:19 -05001019 title.appendf(" %s Gamma %f",
1020 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001021 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001022 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001023
Ben Wagner37c54032018-04-13 14:30:23 -04001024 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001025 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001026 switch (params.fSurfaceProps.pixelGeometry()) {
1027 case kUnknown_SkPixelGeometry:
1028 title.append( " Flat");
1029 break;
1030 case kRGB_H_SkPixelGeometry:
1031 title.append( " RGB");
1032 break;
1033 case kBGR_H_SkPixelGeometry:
1034 title.append( " BGR");
1035 break;
1036 case kRGB_V_SkPixelGeometry:
1037 title.append( " RGBV");
1038 break;
1039 case kBGR_V_SkPixelGeometry:
1040 title.append( " BGRV");
1041 break;
1042 }
1043 }
1044
1045 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1046 title.append(" DFT");
1047 }
1048
csmartdalton578f0642017-02-24 16:04:47 -07001049 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001050 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001051 int msaa = fWindow->sampleCount();
1052 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001053 title.appendf(" MSAA: %i", msaa);
1054 }
1055 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001056
1057 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001058 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001059 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1060 }
1061
Brian Osman805a7272018-05-02 15:40:20 -04001062 if (kPerspective_Real == fPerspectiveMode) {
1063 title.append(" Perpsective (Real)");
1064 } else if (kPerspective_Fake == fPerspectiveMode) {
1065 title.append(" Perspective (Fake)");
1066 }
1067
brianosman05de2162016-05-06 13:28:57 -07001068 fWindow->setTitle(title.c_str());
1069}
1070
Florin Malitaab99c342018-01-16 16:23:03 -05001071int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001072
1073 if (!FLAGS_slide.isEmpty()) {
1074 int count = fSlides.count();
1075 for (int i = 0; i < count; i++) {
1076 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001077 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001078 }
1079 }
1080
1081 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1082 this->listNames();
1083 }
1084
Florin Malitaab99c342018-01-16 16:23:03 -05001085 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001086}
1087
Florin Malitaab99c342018-01-16 16:23:03 -05001088void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001089 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001090 for (const auto& slide : fSlides) {
1091 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001092 }
1093}
1094
Florin Malitaab99c342018-01-16 16:23:03 -05001095void Viewer::setCurrentSlide(int slide) {
1096 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001097
Florin Malitaab99c342018-01-16 16:23:03 -05001098 if (slide == fCurrentSlide) {
1099 return;
1100 }
1101
1102 if (fCurrentSlide >= 0) {
1103 fSlides[fCurrentSlide]->unload();
1104 }
1105
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001106 SkScalar scaleFactor = 1.0;
1107 if (fApplyBackingScale) {
1108 scaleFactor = fWindow->scaleFactor();
1109 }
1110 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1111 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001112 fCurrentSlide = slide;
1113 this->setupCurrentSlide();
1114}
1115
1116void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001117 if (fCurrentSlide >= 0) {
1118 // prepare dimensions for image slides
1119 fGesture.resetTouchState();
1120 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001121
Jim Van Verth0848fb02018-01-22 13:39:30 -05001122 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1123 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1124 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001125
Jim Van Verth0848fb02018-01-22 13:39:30 -05001126 // Start with a matrix that scales the slide to the available screen space
1127 if (fWindow->scaleContentToFit()) {
1128 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001129 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1130 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001131 }
liyuqiane46e4f02016-05-20 07:32:19 -07001132 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001133
1134 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001135 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001136
1137 this->updateTitle();
1138 this->updateUIState();
1139
1140 fStatsLayer.resetMeasurements();
1141
1142 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001143 }
jvanverthc265a922016-04-08 12:51:45 -07001144}
1145
Brian Osmanaba642c2020-02-06 12:52:25 -05001146#define MAX_ZOOM_LEVEL 8.0f
1147#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001148
jvanverth34524262016-05-04 13:49:13 -07001149void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001150 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001151 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001152 this->preTouchMatrixChanged();
1153}
Yuqian Li755778c2018-03-28 16:23:31 -04001154
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001155void Viewer::preTouchMatrixChanged() {
1156 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001157 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1158 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1159 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1160 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1161}
1162
Brian Osman805a7272018-05-02 15:40:20 -04001163SkMatrix Viewer::computePerspectiveMatrix() {
1164 SkScalar w = fWindow->width(), h = fWindow->height();
1165 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1166 SkPoint perspPts[4] = {
1167 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1168 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1169 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1170 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1171 };
1172 SkMatrix m;
1173 m.setPolyToPoly(orthoPts, perspPts, 4);
1174 return m;
1175}
1176
Yuqian Li755778c2018-03-28 16:23:31 -04001177SkMatrix Viewer::computePreTouchMatrix() {
1178 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001179
1180 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001181 if (fApplyBackingScale) {
1182 zoomScale *= fWindow->scaleFactor();
1183 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001184 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001185 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001186
1187 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1188 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001189
Brian Osman805a7272018-05-02 15:40:20 -04001190 if (kPerspective_Real == fPerspectiveMode) {
1191 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001192 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001193 }
1194
Yuqian Li755778c2018-03-28 16:23:31 -04001195 return m;
jvanverthc265a922016-04-08 12:51:45 -07001196}
1197
liyuqiand3cdbca2016-05-17 12:44:20 -07001198SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001199 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001200 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001201 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001202 return m;
jvanverthc265a922016-04-08 12:51:45 -07001203}
1204
Brian Osman621491e2017-02-28 15:45:01 -05001205void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001206 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001207 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001208 fBackendType = backendType;
1209
Robert Phillipse9229532020-06-26 10:10:49 -04001210 // The active context is going away in 'detach'
1211 for(auto& slide : fSlides) {
1212 slide->gpuTeardown();
1213 }
1214
Brian Osman621491e2017-02-28 15:45:01 -05001215 fWindow->detach();
1216
Brian Osman70d2f432017-11-08 09:54:10 -05001217#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001218 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1219 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001220 DisplayParams params = fWindow->getRequestedDisplayParams();
1221 delete fWindow;
1222 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001223
Brian Osman70d2f432017-11-08 09:54:10 -05001224 // re-register callbacks
1225 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001226 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001227 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001228 fWindow->pushLayer(&fImGuiLayer);
1229
Brian Osman70d2f432017-11-08 09:54:10 -05001230 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1231 // will still include our correct sample count. But the re-created fWindow will lose that
1232 // information. On Windows, we need to re-create the window when changing sample count,
1233 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1234 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1235 // as if we had never changed windows at all).
1236 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001237#endif
1238
Brian Osman70d2f432017-11-08 09:54:10 -05001239 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001240}
1241
Brian Osman92004802017-03-06 11:47:26 -05001242void Viewer::setColorMode(ColorMode colorMode) {
1243 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001244 this->updateTitle();
1245 fWindow->inval();
1246}
1247
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001248class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1249public:
Mike Reed3ae47332019-01-04 10:11:46 -05001250 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1251 SkFont* font, Viewer::SkFontFields* ffields)
1252 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001253 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001254 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1255 sk_sp<SkTextBlob>* cache) {
1256 bool blobWillChange = false;
1257 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001258 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1259 bool shouldDraw = this->filterFont(&filteredFont);
1260 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001261 blobWillChange = true;
1262 break;
1263 }
1264 }
1265 if (!blobWillChange) {
1266 return blob;
1267 }
1268
1269 SkTextBlobBuilder builder;
1270 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001271 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1272 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001273 if (!shouldDraw) {
1274 continue;
1275 }
1276
Mike Reed3ae47332019-01-04 10:11:46 -05001277 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001278
Ben Wagner41e40472018-09-24 13:01:54 -04001279 const SkTextBlobBuilder::RunBuffer& runBuffer
1280 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001281 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001282 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001283 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001284 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001285 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001286 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001287 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001288 it.glyphCount(), it.textSize(), SkString())
Ben Wagnere5736262021-02-08 16:52:08 -05001289 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
1290 ? SkTextBlobBuilderPriv::AllocRunRSXForm(&builder, font,
1291 it.glyphCount(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001292 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1293 uint32_t glyphCount = it.glyphCount();
1294 if (it.glyphs()) {
1295 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1296 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1297 }
1298 if (it.pos()) {
1299 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001300 unsigned posPerGlyph = it.scalarsPerGlyph();
1301 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001302 }
1303 if (it.text()) {
1304 size_t textSize = sizeof(decltype(*it.text()));
1305 uint32_t textCount = it.textSize();
1306 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1307 }
1308 if (it.clusters()) {
1309 size_t clusterSize = sizeof(decltype(*it.clusters()));
1310 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1311 }
1312 }
1313 *cache = builder.make();
1314 return cache->get();
1315 }
1316 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1317 const SkPaint& paint) override {
1318 sk_sp<SkTextBlob> cache;
1319 this->SkPaintFilterCanvas::onDrawTextBlob(
1320 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1321 }
Mike Reed3ae47332019-01-04 10:11:46 -05001322 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001323 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001324 font->writable()->setSize(fFont->getSize());
1325 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001326 if (fFontOverrides->fScaleX) {
1327 font->writable()->setScaleX(fFont->getScaleX());
1328 }
1329 if (fFontOverrides->fSkewX) {
1330 font->writable()->setSkewX(fFont->getSkewX());
1331 }
Mike Reed3ae47332019-01-04 10:11:46 -05001332 if (fFontOverrides->fHinting) {
1333 font->writable()->setHinting(fFont->getHinting());
1334 }
Ben Wagner9613e452019-01-23 10:34:59 -05001335 if (fFontOverrides->fEdging) {
1336 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001337 }
Ben Wagner9613e452019-01-23 10:34:59 -05001338 if (fFontOverrides->fEmbolden) {
1339 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001340 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001341 if (fFontOverrides->fBaselineSnap) {
1342 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1343 }
Ben Wagner9613e452019-01-23 10:34:59 -05001344 if (fFontOverrides->fLinearMetrics) {
1345 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001346 }
Ben Wagner9613e452019-01-23 10:34:59 -05001347 if (fFontOverrides->fSubpixel) {
1348 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001349 }
Ben Wagner9613e452019-01-23 10:34:59 -05001350 if (fFontOverrides->fEmbeddedBitmaps) {
1351 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001352 }
Ben Wagner9613e452019-01-23 10:34:59 -05001353 if (fFontOverrides->fForceAutoHinting) {
1354 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001355 }
Ben Wagner9613e452019-01-23 10:34:59 -05001356
Mike Reed3ae47332019-01-04 10:11:46 -05001357 return true;
1358 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001359 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001360 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001361 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001362 }
Ben Wagner9613e452019-01-23 10:34:59 -05001363 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001364 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001365 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001366 if (fPaintOverrides->fStyle) {
1367 paint.setStyle(fPaint->getStyle());
1368 }
1369 if (fPaintOverrides->fWidth) {
1370 paint.setStrokeWidth(fPaint->getStrokeWidth());
1371 }
1372 if (fPaintOverrides->fMiterLimit) {
1373 paint.setStrokeMiter(fPaint->getStrokeMiter());
1374 }
1375 if (fPaintOverrides->fCapType) {
1376 paint.setStrokeCap(fPaint->getStrokeCap());
1377 }
1378 if (fPaintOverrides->fJoinType) {
1379 paint.setStrokeJoin(fPaint->getStrokeJoin());
1380 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001381 return true;
1382 }
1383 SkPaint* fPaint;
1384 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001385 SkFont* fFont;
1386 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001387};
1388
Robert Phillips9882dae2019-03-04 11:00:10 -05001389void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001390 if (fCurrentSlide < 0) {
1391 return;
1392 }
1393
Robert Phillips9882dae2019-03-04 11:00:10 -05001394 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001395
Brian Osmanf750fbc2017-02-08 10:47:28 -05001396 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001397 SkSurface* slideSurface = surface;
1398 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001399 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001400
Brian Osmane0d4fba2017-03-15 10:24:55 -04001401 // 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 -05001402 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001403 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001404 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001405 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001406 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001407 }
1408
Brian Osman3ac99cf2017-12-01 11:23:53 -05001409 if (fSaveToSKP) {
1410 SkPictureRecorder recorder;
1411 SkCanvas* recorderCanvas = recorder.beginRecording(
1412 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001413 fSlides[fCurrentSlide]->draw(recorderCanvas);
1414 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1415 SkFILEWStream stream("sample_app.skp");
1416 picture->serialize(&stream);
1417 fSaveToSKP = false;
1418 }
1419
Brian Osmane9ed0f02018-11-26 14:50:05 -05001420 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001421 SkColorType colorType;
1422 switch (fColorMode) {
1423 case ColorMode::kLegacy:
1424 case ColorMode::kColorManaged8888:
1425 colorType = kN32_SkColorType;
1426 break;
1427 case ColorMode::kColorManagedF16:
1428 colorType = kRGBA_F16_SkColorType;
1429 break;
1430 case ColorMode::kColorManagedF16Norm:
1431 colorType = kRGBA_F16Norm_SkColorType;
1432 break;
1433 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001434
1435 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001436 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001437 slideCanvas->getProps(&props);
1438
Brian Osmane9ed0f02018-11-26 14:50:05 -05001439 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1440 return Window::kRaster_BackendType == this->fBackendType
1441 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001442 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001443 };
1444
Brian Osman03115dc2018-11-26 13:55:19 -05001445 // We need to render offscreen if we're...
1446 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1447 // ... in any raster mode, because the window surface is actually GL
1448 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001449 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001450 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001451 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001452 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001453 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001454 colorSpace != nullptr ||
1455 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001456
Brian Osmane9ed0f02018-11-26 14:50:05 -05001457 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001458 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001459 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001460 }
1461
Mike Reed59295352020-03-12 13:56:34 -04001462 SkPictureRecorder recorder;
1463 SkCanvas* recorderRestoreCanvas = nullptr;
1464 if (fDrawViaSerialize) {
1465 recorderRestoreCanvas = slideCanvas;
1466 slideCanvas = recorder.beginRecording(
1467 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1468 }
1469
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001470 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001471 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001472 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001473 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001474 if (fTiled) {
1475 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1476 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001477 for (int y = 0; y < fWindow->height(); y += tileH) {
1478 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001479 SkAutoCanvasRestore acr(slideCanvas, true);
1480 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1481 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001482 }
1483 }
1484
1485 // Draw borders between tiles
1486 if (fDrawTileBoundaries) {
1487 SkPaint border;
1488 border.setColor(0x60FF00FF);
1489 border.setStyle(SkPaint::kStroke_Style);
1490 for (int y = 0; y < fWindow->height(); y += tileH) {
1491 for (int x = 0; x < fWindow->width(); x += tileW) {
1492 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1493 }
1494 }
1495 }
1496 } else {
1497 slideCanvas->concat(this->computeMatrix());
1498 if (kPerspective_Real == fPerspectiveMode) {
1499 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1500 }
Mike Reed3ae47332019-01-04 10:11:46 -05001501 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001502 fSlides[fCurrentSlide]->draw(&filterCanvas);
1503 }
Brian Osman56a24812017-12-19 11:15:16 -05001504 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001505 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001506
Mike Reed59295352020-03-12 13:56:34 -04001507 if (recorderRestoreCanvas) {
1508 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1509 auto data = picture->serialize();
1510 slideCanvas = recorderRestoreCanvas;
1511 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1512 }
1513
Brian Osman1df161a2017-02-09 12:10:20 -05001514 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001515 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001516 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001517 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001518
1519 // If we rendered offscreen, snap an image and push the results to the window's canvas
1520 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001521 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001522
Robert Phillips9882dae2019-03-04 11:00:10 -05001523 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001524 SkPaint paint;
1525 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001526 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001527 int prePerspectiveCount = canvas->save();
1528 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001529 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001530 canvas->clear(SK_ColorWHITE);
1531 canvas->concat(this->computePerspectiveMatrix());
1532 }
Mike Reedb339d052021-01-28 11:20:41 -05001533 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001534 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001535 }
Mike Reed376d8122019-03-14 11:39:02 -04001536
1537 if (fShowSlideDimensions) {
1538 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1539 SkPaint paint;
1540 paint.setColor(0x40FFFF00);
1541 surface->getCanvas()->drawRect(r, paint);
1542 }
liyuqian6f163d22016-06-13 12:26:45 -07001543}
1544
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001545void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001546 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001547 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001548}
Jim Van Verth6f449692017-02-14 15:16:46 -05001549
Robert Phillips9882dae2019-03-04 11:00:10 -05001550void Viewer::onPaint(SkSurface* surface) {
1551 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001552
Robert Phillips9882dae2019-03-04 11:00:10 -05001553 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001554
Brian Osmand67e5182017-12-08 16:46:09 -05001555 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001556
Greg Daniel427d8eb2020-09-28 15:04:18 -04001557 fLastImage.reset();
1558
Robert Phillipsed653392020-07-10 13:55:21 -04001559 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001560 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001561 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001562 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001563}
1564
Ben Wagnera1915972018-08-09 15:06:19 -04001565void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001566 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001567 SkScalar scaleFactor = 1.0;
1568 if (fApplyBackingScale) {
1569 scaleFactor = fWindow->scaleFactor();
1570 }
1571 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001572 }
Ben Wagnera1915972018-08-09 15:06:19 -04001573}
1574
Florin Malitacefc1b92018-02-19 21:43:47 -05001575SkPoint Viewer::mapEvent(float x, float y) {
1576 const auto m = this->computeMatrix();
1577 SkMatrix inv;
1578
1579 SkAssertResult(m.invert(&inv));
1580
1581 return inv.mapXY(x, y);
1582}
1583
Hal Canaryb1f411a2019-08-29 10:39:22 -04001584bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001585 if (GestureDevice::kMouse == fGestureDevice) {
1586 return false;
1587 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001588
1589 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001590 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001591 fWindow->inval();
1592 return true;
1593 }
1594
liyuqiand3cdbca2016-05-17 12:44:20 -07001595 void* castedOwner = reinterpret_cast<void*>(owner);
1596 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001597 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001598 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001599#if defined(SK_BUILD_FOR_IOS)
1600 // TODO: move IOS swipe detection higher up into the platform code
1601 SkPoint dir;
1602 if (fGesture.isFling(&dir)) {
1603 // swiping left or right
1604 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1605 if (dir.fX < 0) {
1606 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1607 fCurrentSlide + 1 : 0);
1608 } else {
1609 this->setCurrentSlide(fCurrentSlide > 0 ?
1610 fCurrentSlide - 1 : fSlides.count() - 1);
1611 }
1612 }
1613 fGesture.reset();
1614 }
1615#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001616 break;
1617 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001618 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001619 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001620 break;
1621 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001622 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001623 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001624 break;
1625 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001626 default: {
1627 // kLeft and kRight are only for swipes
1628 SkASSERT(false);
1629 break;
1630 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001631 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001632 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001633 fWindow->inval();
1634 return true;
1635}
1636
Hal Canaryb1f411a2019-08-29 10:39:22 -04001637bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001638 if (GestureDevice::kTouch == fGestureDevice) {
1639 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001640 }
Brian Osman16c81a12017-12-20 11:58:34 -05001641
Florin Malitacefc1b92018-02-19 21:43:47 -05001642 const auto slidePt = this->mapEvent(x, y);
1643 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1644 fWindow->inval();
1645 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001646 }
1647
1648 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001649 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001650 fGesture.touchEnd(nullptr);
1651 break;
1652 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001653 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001654 fGesture.touchBegin(nullptr, x, y);
1655 break;
1656 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001657 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001658 fGesture.touchMoved(nullptr, x, y);
1659 break;
1660 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001661 default: {
1662 SkASSERT(false); // shouldn't see kRight or kLeft here
1663 break;
1664 }
Brian Osman16c81a12017-12-20 11:58:34 -05001665 }
1666 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1667
Hal Canaryb1f411a2019-08-29 10:39:22 -04001668 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001669 fWindow->inval();
1670 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001671 return true;
1672}
1673
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001674bool Viewer::onFling(skui::InputState state) {
1675 if (skui::InputState::kRight == state) {
1676 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1677 return true;
1678 } else if (skui::InputState::kLeft == state) {
1679 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1680 return true;
1681 }
1682 return false;
1683}
1684
1685bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1686 switch (state) {
1687 case skui::InputState::kDown:
1688 fGesture.startZoom();
1689 return true;
1690 break;
1691 case skui::InputState::kMove:
1692 fGesture.updateZoom(scale, x, y, x, y);
1693 return true;
1694 break;
1695 case skui::InputState::kUp:
1696 fGesture.endZoom();
1697 return true;
1698 break;
1699 default:
1700 SkASSERT(false);
1701 break;
1702 }
1703
1704 return false;
1705}
1706
Brian Osmana109e392017-02-24 09:49:14 -05001707static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001708 // The gamut image covers a (0.8 x 0.9) shaped region
1709 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001710
1711 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1712 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1713 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001714 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1715 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1716 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001717
Brian Osman535c5e32019-02-09 16:32:58 -05001718 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1719 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1720 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1721 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1722 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001723}
1724
Ben Wagner3627d2e2018-06-26 14:23:20 -04001725static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001726 ImGui::DragCanvas dc(pt);
1727 dc.fillColor(IM_COL32(0, 0, 0, 128));
1728 dc.dragPoint(pt);
1729 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001730}
1731
Brian Osman9bb47cf2018-04-26 15:55:00 -04001732static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001733 ImGui::DragCanvas dc(pts);
1734 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001735
Brian Osman535c5e32019-02-09 16:32:58 -05001736 for (int i = 0; i < 4; ++i) {
1737 dc.dragPoint(pts + i);
1738 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001739
Brian Osman535c5e32019-02-09 16:32:58 -05001740 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1741 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1742 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1743 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001744
Brian Osman535c5e32019-02-09 16:32:58 -05001745 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001746}
1747
John Stiles38b7d2f2020-06-24 12:13:31 -04001748static SkSL::String build_sksl_highlight_shader() {
1749 return SkSL::String("out half4 sk_FragColor;\n"
1750 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1751}
1752
1753static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1754 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1755 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1756 size_t pos = inShader.rfind("return *_out;\n");
1757 if (pos == std::string::npos) {
1758 return inShader;
1759 }
1760
1761 SkSL::String replacementShader = inShader;
1762 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1763 return replacementShader;
1764}
1765
1766static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1767 const char* versionDecl = shaderCaps.versionDeclString();
1768 SkSL::String highlight = versionDecl ? versionDecl : "";
1769 if (shaderCaps.usesPrecisionModifiers()) {
1770 highlight.append("precision mediump float;\n");
1771 }
1772 highlight.appendf("out vec4 sk_FragColor;\n"
1773 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1774 return highlight;
1775}
1776
Brian Osmand67e5182017-12-08 16:46:09 -05001777void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001778 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1779 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001780 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001781 }
1782
1783 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001784 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1785 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001786 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001787 DisplayParams params = fWindow->getRequestedDisplayParams();
1788 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001789 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001790
Brian Osmana109e392017-02-24 09:49:14 -05001791 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1792 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001793 if (ImGui::CollapsingHeader("Backend")) {
1794 int newBackend = static_cast<int>(fBackendType);
1795 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1796 ImGui::SameLine();
1797 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001798#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1799 ImGui::SameLine();
1800 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1801#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001802#if defined(SK_DAWN)
1803 ImGui::SameLine();
1804 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1805#endif
John Stilesf7da9232020-11-19 19:58:14 -05001806#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001807 ImGui::SameLine();
1808 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1809#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001810#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001811 ImGui::SameLine();
1812 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1813#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001814#if defined(SK_DIRECT3D)
1815 ImGui::SameLine();
1816 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1817#endif
Brian Osman621491e2017-02-28 15:45:01 -05001818 if (newBackend != fBackendType) {
1819 fDeferredActions.push_back([=]() {
1820 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1821 });
1822 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001823
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001824 bool* wire = &params.fGrContextOptions.fWireframeMode;
1825 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1826 paramsChanged = true;
1827 }
Brian Salomon99a33902017-03-07 15:16:34 -05001828
Brian Osman28b12522017-03-08 17:10:24 -05001829 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001830 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001831 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001832 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1833 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1834 1;
1835
1836 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1837 if (maxMSAA >= 4) {
1838 ImGui::Text("MSAA: ");
1839
1840 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1841 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1842 // don't show it in the list.
1843 if (curMSAA == 2) {
1844 continue;
1845 }
1846 ImGui::SameLine();
1847 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1848 &sampleCount, curMSAA);
1849 }
1850 }
Brian Osman28b12522017-03-08 17:10:24 -05001851
1852 if (sampleCount != params.fMSAASampleCount) {
1853 params.fMSAASampleCount = sampleCount;
1854 paramsChanged = true;
1855 }
1856 }
1857
Ben Wagner37c54032018-04-13 14:30:23 -04001858 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001859 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001860 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1861 }
1862 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1863 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1864 {
1865 uint32_t flags = params.fSurfaceProps.flags();
1866 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001867 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1868 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1869 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001870 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001871 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001872 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1873 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1874 }
1875 paramsChanged = true;
1876 }
1877
1878 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1879 if (ImGui::Checkbox("DFT", &useDFT)) {
1880 uint32_t flags = params.fSurfaceProps.flags();
1881 if (useDFT) {
1882 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1883 } else {
1884 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1885 }
1886 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1887 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1888 paramsChanged = true;
1889 }
1890
Brian Osman8a9de3d2017-03-01 14:59:05 -05001891 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001892 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001893 auto prButton = [&](GpuPathRenderers x) {
1894 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001895 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1896 params.fGrContextOptions.fGpuPathRenderers = x;
1897 paramsChanged = true;
1898 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001899 }
1900 };
1901
1902 if (!ctx) {
1903 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001904 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001905 const auto* caps = ctx->priv().caps();
1906 prButton(GpuPathRenderers::kDefault);
1907 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001908 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001909 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001910 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001911 if (caps->shaderCaps()->pathRenderingSupport()) {
1912 prButton(GpuPathRenderers::kStencilAndCover);
1913 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001914 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001915 if (1 == fWindow->sampleCount()) {
1916 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1917 prButton(GpuPathRenderers::kCoverageCounting);
1918 }
1919 prButton(GpuPathRenderers::kSmall);
1920 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001921 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001922 prButton(GpuPathRenderers::kNone);
1923 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001924 ImGui::TreePop();
1925 }
Brian Osman621491e2017-02-28 15:45:01 -05001926 }
1927
Ben Wagner964571d2019-03-08 12:35:06 -05001928 if (ImGui::CollapsingHeader("Tiling")) {
1929 ImGui::Checkbox("Enable", &fTiled);
1930 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1931 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1932 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1933 }
1934
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001935 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001936 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1937 this->preTouchMatrixChanged();
1938 this->onResize(fWindow->width(), fWindow->height());
1939 paramsChanged = true;
1940 }
1941
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001942 float zoom = fZoomLevel;
1943 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1944 fZoomLevel = zoom;
1945 this->preTouchMatrixChanged();
1946 paramsChanged = true;
1947 }
1948 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001949 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001950 fRotation = deg;
1951 this->preTouchMatrixChanged();
1952 paramsChanged = true;
1953 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001954 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1955 if (ImGui_DragLocation(&fOffset)) {
1956 this->preTouchMatrixChanged();
1957 paramsChanged = true;
1958 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001959 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1960 this->preTouchMatrixChanged();
1961 paramsChanged = true;
1962 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001963 }
Brian Osman805a7272018-05-02 15:40:20 -04001964 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1965 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1966 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001967 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001968 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001969 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001970 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001971 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001972 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001973 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001974 }
1975
Ben Wagnera580fb32018-04-17 11:16:32 -04001976 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001977 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001978 if (fPaintOverrides.fAntiAlias) {
1979 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001980 }
1981 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001982 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001983 {
1984 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1985 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001986 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001987 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1988 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001989 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001990 fPaintOverrides.fAntiAlias = true;
1991 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001992 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001993 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001994 case SkPaintFields::AntiAliasState::Alias:
1995 break;
1996 case SkPaintFields::AntiAliasState::Normal:
1997 break;
1998 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1999 gSkUseAnalyticAA = true;
2000 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002001 break;
2002 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2003 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002004 break;
2005 }
2006 }
2007 paramsChanged = true;
2008 }
2009
Ben Wagner99a78dc2018-05-09 18:23:51 -04002010 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002011 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002012 bool (SkPaint::* isFlag)() const,
2013 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002014 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002015 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002016 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002017 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002018 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002019 if (ImGui::Combo(label, &itemIndex, items)) {
2020 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002021 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002022 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002023 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002024 (fPaint.*setFlag)(itemIndex == 2);
2025 }
2026 paramsChanged = true;
2027 }
2028 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002029
Ben Wagner99a78dc2018-05-09 18:23:51 -04002030 paintFlag("Dither",
2031 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002032 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002033 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002034
2035 int styleIdx = 0;
2036 if (fPaintOverrides.fStyle) {
2037 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2038 }
2039 if (ImGui::Combo("Style", &styleIdx,
2040 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2041 {
2042 if (styleIdx == 0) {
2043 fPaintOverrides.fStyle = false;
2044 fPaint.setStyle(SkPaint::kFill_Style);
2045 } else {
2046 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2047 fPaintOverrides.fStyle = true;
2048 }
2049 paramsChanged = true;
2050 }
2051
2052 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2053 if (fPaintOverrides.fWidth) {
2054 float width = fPaint.getStrokeWidth();
2055 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2056 fPaint.setStrokeWidth(width);
2057 paramsChanged = true;
2058 }
2059 }
2060
2061 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2062 if (fPaintOverrides.fMiterLimit) {
2063 float miterLimit = fPaint.getStrokeMiter();
2064 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2065 fPaint.setStrokeMiter(miterLimit);
2066 paramsChanged = true;
2067 }
2068 }
2069
2070 int capIdx = 0;
2071 if (fPaintOverrides.fCapType) {
2072 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2073 }
2074 if (ImGui::Combo("Cap Type", &capIdx,
2075 "Default\0Butt\0Round\0Square\0\0"))
2076 {
2077 if (capIdx == 0) {
2078 fPaintOverrides.fCapType = false;
2079 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2080 } else {
2081 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2082 fPaintOverrides.fCapType = true;
2083 }
2084 paramsChanged = true;
2085 }
2086
2087 int joinIdx = 0;
2088 if (fPaintOverrides.fJoinType) {
2089 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2090 }
2091 if (ImGui::Combo("Join Type", &joinIdx,
2092 "Default\0Miter\0Round\0Bevel\0\0"))
2093 {
2094 if (joinIdx == 0) {
2095 fPaintOverrides.fJoinType = false;
2096 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2097 } else {
2098 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2099 fPaintOverrides.fJoinType = true;
2100 }
2101 paramsChanged = true;
2102 }
Ben Wagner9613e452019-01-23 10:34:59 -05002103 }
Hal Canary02738a82019-01-21 18:51:32 +00002104
Ben Wagner9613e452019-01-23 10:34:59 -05002105 if (ImGui::CollapsingHeader("Font")) {
2106 int hintingIdx = 0;
2107 if (fFontOverrides.fHinting) {
2108 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2109 }
2110 if (ImGui::Combo("Hinting", &hintingIdx,
2111 "Default\0None\0Slight\0Normal\0Full\0\0"))
2112 {
2113 if (hintingIdx == 0) {
2114 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002115 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002116 } else {
2117 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2118 fFontOverrides.fHinting = true;
2119 }
2120 paramsChanged = true;
2121 }
Hal Canary02738a82019-01-21 18:51:32 +00002122
Ben Wagner9613e452019-01-23 10:34:59 -05002123 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2124 bool SkFontFields::* flag,
2125 bool (SkFont::* isFlag)() const,
2126 void (SkFont::* setFlag)(bool) )
2127 {
2128 int itemIndex = 0;
2129 if (fFontOverrides.*flag) {
2130 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2131 }
2132 if (ImGui::Combo(label, &itemIndex, items)) {
2133 if (itemIndex == 0) {
2134 fFontOverrides.*flag = false;
2135 } else {
2136 fFontOverrides.*flag = true;
2137 (fFont.*setFlag)(itemIndex == 2);
2138 }
2139 paramsChanged = true;
2140 }
2141 };
Hal Canary02738a82019-01-21 18:51:32 +00002142
Ben Wagner9613e452019-01-23 10:34:59 -05002143 fontFlag("Fake Bold Glyphs",
2144 "Default\0No Fake Bold\0Fake Bold\0\0",
2145 &SkFontFields::fEmbolden,
2146 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002147
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002148 fontFlag("Baseline Snapping",
2149 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2150 &SkFontFields::fBaselineSnap,
2151 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2152
Ben Wagner9613e452019-01-23 10:34:59 -05002153 fontFlag("Linear Text",
2154 "Default\0No Linear Text\0Linear Text\0\0",
2155 &SkFontFields::fLinearMetrics,
2156 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002157
Ben Wagner9613e452019-01-23 10:34:59 -05002158 fontFlag("Subpixel Position Glyphs",
2159 "Default\0Pixel Text\0Subpixel Text\0\0",
2160 &SkFontFields::fSubpixel,
2161 &SkFont::isSubpixel, &SkFont::setSubpixel);
2162
2163 fontFlag("Embedded Bitmap Text",
2164 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2165 &SkFontFields::fEmbeddedBitmaps,
2166 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2167
2168 fontFlag("Force Auto-Hinting",
2169 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2170 &SkFontFields::fForceAutoHinting,
2171 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2172
2173 int edgingIdx = 0;
2174 if (fFontOverrides.fEdging) {
2175 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2176 }
2177 if (ImGui::Combo("Edging", &edgingIdx,
2178 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2179 {
2180 if (edgingIdx == 0) {
2181 fFontOverrides.fEdging = false;
2182 fFont.setEdging(SkFont::Edging::kAlias);
2183 } else {
2184 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2185 fFontOverrides.fEdging = true;
2186 }
2187 paramsChanged = true;
2188 }
2189
Ben Wagner15a8d572019-03-21 13:35:44 -04002190 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2191 if (fFontOverrides.fSize) {
2192 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002193 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002194 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002195 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002196 fFontOverrides.fSizeRange[0],
2197 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002198 "%.6f", 2.0f))
2199 {
Mike Reed3ae47332019-01-04 10:11:46 -05002200 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002201 paramsChanged = true;
2202 }
2203 }
2204
2205 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2206 if (fFontOverrides.fScaleX) {
2207 float scaleX = fFont.getScaleX();
2208 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2209 fFont.setScaleX(scaleX);
2210 paramsChanged = true;
2211 }
2212 }
2213
2214 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2215 if (fFontOverrides.fSkewX) {
2216 float skewX = fFont.getSkewX();
2217 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2218 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002219 paramsChanged = true;
2220 }
2221 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002222 }
2223
Mike Reed81f60ec2018-05-15 10:09:52 -04002224 {
2225 SkMetaData controls;
2226 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2227 if (ImGui::CollapsingHeader("Current Slide")) {
2228 SkMetaData::Iter iter(controls);
2229 const char* name;
2230 SkMetaData::Type type;
2231 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002232 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002233 if (type == SkMetaData::kScalar_Type) {
2234 float val[3];
2235 SkASSERT(count == 3);
2236 controls.findScalars(name, &count, val);
2237 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2238 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002239 }
Ben Wagner110c7032019-03-22 17:03:59 -04002240 } else if (type == SkMetaData::kBool_Type) {
2241 bool val;
2242 SkASSERT(count == 1);
2243 controls.findBool(name, &val);
2244 if (ImGui::Checkbox(name, &val)) {
2245 controls.setBool(name, val);
2246 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002247 }
2248 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002249 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002250 }
2251 }
2252 }
2253
Ben Wagner7a3c6742018-04-23 10:01:07 -04002254 if (fShowSlidePicker) {
2255 ImGui::SetNextTreeNodeOpen(true);
2256 }
Brian Osman79086b92017-02-10 13:36:16 -05002257 if (ImGui::CollapsingHeader("Slide")) {
2258 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002259 static ImVector<const char*> filteredSlideNames;
2260 static ImVector<int> filteredSlideIndices;
2261
Brian Osmanfce09c52017-11-14 15:32:20 -05002262 if (fShowSlidePicker) {
2263 ImGui::SetKeyboardFocusHere();
2264 fShowSlidePicker = false;
2265 }
2266
Brian Osman79086b92017-02-10 13:36:16 -05002267 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002268 filteredSlideNames.clear();
2269 filteredSlideIndices.clear();
2270 int filteredIndex = 0;
2271 for (int i = 0; i < fSlides.count(); ++i) {
2272 const char* slideName = fSlides[i]->getName().c_str();
2273 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2274 if (i == fCurrentSlide) {
2275 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002276 }
Brian Osmanf479e422017-11-08 13:11:36 -05002277 filteredSlideNames.push_back(slideName);
2278 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002279 }
Brian Osman79086b92017-02-10 13:36:16 -05002280 }
Brian Osmanf479e422017-11-08 13:11:36 -05002281
Brian Osmanf479e422017-11-08 13:11:36 -05002282 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2283 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002284 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002285 }
2286 }
Brian Osmana109e392017-02-24 09:49:14 -05002287
2288 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002289 ColorMode newMode = fColorMode;
2290 auto cmButton = [&](ColorMode mode, const char* label) {
2291 if (ImGui::RadioButton(label, mode == fColorMode)) {
2292 newMode = mode;
2293 }
2294 };
2295
2296 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002297 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2298 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002299 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002300
2301 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002302 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002303 }
2304
2305 // Pick from common gamuts:
2306 int primariesIdx = 4; // Default: Custom
2307 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2308 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2309 primariesIdx = i;
2310 break;
2311 }
2312 }
2313
Brian Osman03115dc2018-11-26 13:55:19 -05002314 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002315 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002316
Brian Osmana109e392017-02-24 09:49:14 -05002317 if (ImGui::Combo("Primaries", &primariesIdx,
2318 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2319 if (primariesIdx >= 0 && primariesIdx <= 3) {
2320 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2321 }
2322 }
2323
2324 // Allow direct editing of gamut
2325 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2326 }
Brian Osman207d4102019-01-10 09:40:58 -05002327
2328 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002329 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002330 if (ImGui::Checkbox("Pause", &isPaused)) {
2331 fAnimTimer.togglePauseResume();
2332 }
Brian Osman707d2022019-01-10 11:27:34 -05002333
2334 float speed = fAnimTimer.getSpeed();
2335 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2336 fAnimTimer.setSpeed(speed);
2337 }
Brian Osman207d4102019-01-10 09:40:58 -05002338 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002339
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002340 if (ImGui::CollapsingHeader("Shaders")) {
2341 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2342 GrContextOptions::ShaderCacheStrategy::kSkSL;
2343#if defined(SK_VULKAN)
2344 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2345#else
2346 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002347#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002348
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002349 // To re-load shaders from the currently active programs, we flush all
2350 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002351 static bool gLoadPending = false;
2352 if (gLoadPending) {
2353 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2354 int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002355 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002356 entry.fKey = key;
2357 SkMD5 hash;
2358 hash.write(key->bytes(), key->size());
2359 SkMD5::Digest digest = hash.finish();
2360 for (int i = 0; i < 16; ++i) {
2361 entry.fKeyString.appendf("%02x", digest.data[i]);
2362 }
2363
Brian Osman9e4e4c72020-06-10 07:19:34 -04002364 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002365 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002366 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2367 entry.fInputs,
2368 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002369 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002370 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002371 fPersistentCache.foreach(collectShaders);
2372 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002373
2374#if defined(SK_VULKAN)
2375 if (isVulkan && !sksl) {
2376 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2377 for (auto& entry : fCachedShaders) {
2378 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2379 const SkSL::String& spirv(entry.fShader[i]);
2380 std::string disasm;
2381 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2382 &disasm);
2383 entry.fShader[i].assign(disasm);
2384 }
2385 }
2386 }
2387#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002388 }
2389
2390 // Defer actually doing the load/save logic so that we can trigger a save when we
2391 // start or finish hovering on a tree node in the list below:
2392 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002393 bool doSave = ImGui::Button("Save"); ImGui::SameLine();
2394 if (ImGui::Checkbox("SkSL", &sksl)) {
2395 params.fGrContextOptions.fShaderCacheStrategy =
2396 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2397 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2398 paramsChanged = true;
2399 doLoad = true;
2400 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
Brian Osmancbc33b82019-04-19 14:16:19 -04002401 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002402
2403 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002404 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002405 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2406 bool hovered = ImGui::IsItemHovered();
2407 if (hovered != entry.fHovered) {
2408 // Force a save to patch the highlight shader in/out
2409 entry.fHovered = hovered;
2410 doSave = true;
2411 }
2412 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002413 auto stringBox = [](const char* label, std::string* str) {
2414 // Full width, and not too much space for each shader
2415 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2416 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2417 ImGui::InputTextMultiline(label, str, boxSize);
2418 };
2419 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2420 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002421 ImGui::TreePop();
2422 }
2423 }
2424 ImGui::EndChild();
2425
2426 if (doLoad) {
2427 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002428 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002429 gLoadPending = true;
2430 }
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002431 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2432 // but I'm not sure anyone wants to do that.
2433 if (isVulkan && !sksl) {
2434 doSave = false;
2435 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002436 if (doSave) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002437 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002438 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002439 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002440 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002441 if (entry.fHovered) {
2442 // The hovered item (if any) gets a special shader to make it
2443 // identifiable.
2444 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2445 switch (entry.fShaderType) {
2446 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2447 fragShader = build_sksl_highlight_shader();
2448 break;
2449 }
2450 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2451 fragShader = build_glsl_highlight_shader(
2452 *ctx->priv().caps()->shaderCaps());
2453 break;
2454 }
2455 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2456 fragShader = build_metal_highlight_shader(fragShader);
2457 break;
2458 }
2459 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002460 }
2461
Brian Osmana085a412019-04-25 09:44:43 -04002462 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2463 entry.fShader,
2464 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002465 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002466 fPersistentCache.store(*entry.fKey, *data);
2467
2468 entry.fShader[kFragment_GrShaderType] = backup;
2469 }
2470 }
2471 }
Brian Osman79086b92017-02-10 13:36:16 -05002472 }
Brian Salomon99a33902017-03-07 15:16:34 -05002473 if (paramsChanged) {
2474 fDeferredActions.push_back([=]() {
2475 fWindow->setRequestedDisplayParams(params);
2476 fWindow->inval();
2477 this->updateTitle();
2478 });
2479 }
Brian Osman79086b92017-02-10 13:36:16 -05002480 ImGui::End();
2481 }
2482
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002483 if (gShaderErrorHandler.fErrors.count()) {
2484 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002485 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002486 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2487 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002488 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2489 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2490 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2491 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002492 }
2493 ImGui::End();
2494 gShaderErrorHandler.reset();
2495 }
2496
Brian Osmanf6877092017-02-13 09:39:57 -05002497 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002498 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2499 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002500 static int zoomFactor = 8;
2501 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002502 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002503 }
2504 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2505 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002506 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002507 }
Brian Osmanf6877092017-02-13 09:39:57 -05002508
Ben Wagner3627d2e2018-06-26 14:23:20 -04002509 if (!fZoomWindowFixed) {
2510 ImVec2 mousePos = ImGui::GetMousePos();
2511 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2512 }
2513 SkScalar x = fZoomWindowLocation.x();
2514 SkScalar y = fZoomWindowLocation.y();
2515 int xInt = SkScalarRoundToInt(x);
2516 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002517 ImVec2 avail = ImGui::GetContentRegionAvail();
2518
Brian Osmanead517d2017-11-13 15:36:36 -05002519 uint32_t pixel = 0;
2520 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002521 auto dContext = fWindow->directContext();
2522 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002523 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002524 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002525 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002526 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002527 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2528 }
2529
Greg Danielfbc60b72020-11-03 17:27:02 -05002530 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002531 // Translate so the region of the image that's under the mouse cursor is centered
2532 // in the zoom canvas:
2533 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002534 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2535 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002536 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002537
2538 SkPaint outline;
2539 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002540 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002541 });
Brian Osmanf6877092017-02-13 09:39:57 -05002542 }
2543
2544 ImGui::End();
2545 }
Brian Osman79086b92017-02-10 13:36:16 -05002546}
2547
liyuqian2edb0f42016-07-06 14:11:32 -07002548void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002549 for (int i = 0; i < fDeferredActions.count(); ++i) {
2550 fDeferredActions[i]();
2551 }
2552 fDeferredActions.reset();
2553
Brian Osman56a24812017-12-19 11:15:16 -05002554 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002555 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002556 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002557 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002558
Brian Osman79086b92017-02-10 13:36:16 -05002559 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002560 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2561 // not be visible, though. So we need to redraw if there is at least one visible window, or
2562 // more than one active window. Newly created windows are active but not visible for one frame
2563 // while they determine their layout and sizing.
2564 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2565 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002566 fWindow->inval();
2567 }
jvanverth9f372462016-04-06 06:08:59 -07002568}
liyuqiane5a6cd92016-05-27 08:52:52 -07002569
Florin Malitab632df72018-06-18 21:23:06 -04002570template <typename OptionsFunc>
2571static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2572 OptionsFunc&& optionsFunc) {
2573 writer.beginObject();
2574 {
2575 writer.appendString(kName , name);
2576 writer.appendString(kValue, value);
2577
2578 writer.beginArray(kOptions);
2579 {
2580 optionsFunc(writer);
2581 }
2582 writer.endArray();
2583 }
2584 writer.endObject();
2585}
2586
2587
liyuqiane5a6cd92016-05-27 08:52:52 -07002588void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002589 if (!fWindow) {
2590 return;
2591 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002592 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002593 return; // Surface hasn't been created yet.
2594 }
2595
Florin Malitab632df72018-06-18 21:23:06 -04002596 SkDynamicMemoryWStream memStream;
2597 SkJSONWriter writer(&memStream);
2598 writer.beginArray();
2599
liyuqianb73c24b2016-06-03 08:47:23 -07002600 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002601 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2602 [this](SkJSONWriter& writer) {
2603 for(const auto& slide : fSlides) {
2604 writer.appendString(slide->getName().c_str());
2605 }
2606 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002607
liyuqianb73c24b2016-06-03 08:47:23 -07002608 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002609 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2610 [](SkJSONWriter& writer) {
2611 for (const auto& str : kBackendTypeStrings) {
2612 writer.appendString(str);
2613 }
2614 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002615
csmartdalton578f0642017-02-24 16:04:47 -07002616 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002617 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2618 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2619 [this](SkJSONWriter& writer) {
2620 writer.appendS32(0);
2621
2622 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2623 return;
2624 }
2625
2626 for (int msaa : {4, 8, 16}) {
2627 writer.appendS32(msaa);
2628 }
2629 });
csmartdalton578f0642017-02-24 16:04:47 -07002630
csmartdalton61cd31a2017-02-27 17:00:53 -07002631 // Path renderer state
2632 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002633 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2634 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002635 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002636 if (!ctx) {
2637 writer.appendString("Software");
2638 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002639 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002640 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2641 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002642 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002643 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002644 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002645 }
Florin Malitab632df72018-06-18 21:23:06 -04002646 if (caps->shaderCaps()->pathRenderingSupport()) {
2647 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002648 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002649 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002650 }
2651 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002652 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2653 writer.appendString(
2654 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2655 }
2656 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2657 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002658 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002659 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002660 }
2661 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002662
liyuqianb73c24b2016-06-03 08:47:23 -07002663 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002664 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2665 [this](SkJSONWriter& writer) {
2666 writer.appendString(kSoftkeyHint);
2667 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2668 writer.appendString(softkey.c_str());
2669 }
2670 });
liyuqianb73c24b2016-06-03 08:47:23 -07002671
Florin Malitab632df72018-06-18 21:23:06 -04002672 writer.endArray();
2673 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002674
Florin Malitab632df72018-06-18 21:23:06 -04002675 auto data = memStream.detachAsData();
2676
2677 // TODO: would be cool to avoid this copy
2678 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2679
2680 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002681}
2682
2683void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002684 // For those who will add more features to handle the state change in this function:
2685 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2686 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2687 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002688 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002689 for (int i = 0; i < fSlides.count(); ++i) {
2690 if (fSlides[i]->getName().equals(stateValue)) {
2691 this->setCurrentSlide(i);
2692 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002693 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002694 }
Florin Malitaab99c342018-01-16 16:23:03 -05002695
2696 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002697 } else if (stateName.equals(kBackendStateName)) {
2698 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2699 if (stateValue.equals(kBackendTypeStrings[i])) {
2700 if (fBackendType != i) {
2701 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002702 for(auto& slide : fSlides) {
2703 slide->gpuTeardown();
2704 }
liyuqian6cb70252016-06-02 12:16:25 -07002705 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002706 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002707 }
2708 break;
2709 }
2710 }
csmartdalton578f0642017-02-24 16:04:47 -07002711 } else if (stateName.equals(kMSAAStateName)) {
2712 DisplayParams params = fWindow->getRequestedDisplayParams();
2713 int sampleCount = atoi(stateValue.c_str());
2714 if (sampleCount != params.fMSAASampleCount) {
2715 params.fMSAASampleCount = sampleCount;
2716 fWindow->setRequestedDisplayParams(params);
2717 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002718 this->updateTitle();
2719 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002720 }
2721 } else if (stateName.equals(kPathRendererStateName)) {
2722 DisplayParams params = fWindow->getRequestedDisplayParams();
2723 for (const auto& pair : gPathRendererNames) {
2724 if (pair.second == stateValue.c_str()) {
2725 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2726 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2727 fWindow->setRequestedDisplayParams(params);
2728 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002729 this->updateTitle();
2730 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002731 }
2732 break;
2733 }
csmartdalton578f0642017-02-24 16:04:47 -07002734 }
liyuqianb73c24b2016-06-03 08:47:23 -07002735 } else if (stateName.equals(kSoftkeyStateName)) {
2736 if (!stateValue.equals(kSoftkeyHint)) {
2737 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002738 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002739 }
liyuqian2edb0f42016-07-06 14:11:32 -07002740 } else if (stateName.equals(kRefreshStateName)) {
2741 // This state is actually NOT in the UI state.
2742 // We use this to allow Android to quickly set bool fRefresh.
2743 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002744 } else {
2745 SkDebugf("Unknown stateName: %s", stateName.c_str());
2746 }
2747}
Brian Osman79086b92017-02-10 13:36:16 -05002748
Hal Canaryb1f411a2019-08-29 10:39:22 -04002749bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002750 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002751}
2752
Hal Canaryb1f411a2019-08-29 10:39:22 -04002753bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002754 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002755 fWindow->inval();
2756 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002757 } else {
2758 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002759 }
Brian Osman79086b92017-02-10 13:36:16 -05002760}