blob: 2b54be1061a42fe0e0171f70f770694140e8b91c [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 Wagnerfa8b5e42021-01-28 14:30:59 -0500381
Brian Osman56a24812017-12-19 11:15:16 -0500382 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500383 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500384 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
385 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
386 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
387
jvanverth9f372462016-04-06 06:08:59 -0700388 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700389 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500390 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500391 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500392 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700393
brianosman622c8d52016-05-10 06:50:49 -0700394 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500395 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
396 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
397 fWindow->inval();
398 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500399 // Command to jump directly to the slide picker and give it focus
400 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
401 this->fShowImGuiDebugWindow = true;
402 this->fShowSlidePicker = true;
403 fWindow->inval();
404 });
405 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400406 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500407 this->fShowImGuiDebugWindow = true;
408 this->fShowSlidePicker = true;
409 fWindow->inval();
410 });
Brian Osman79086b92017-02-10 13:36:16 -0500411 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
412 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
413 fWindow->inval();
414 });
Brian Osmanf6877092017-02-13 09:39:57 -0500415 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
416 this->fShowZoomWindow = !this->fShowZoomWindow;
417 fWindow->inval();
418 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400419 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
420 this->fZoomWindowFixed = !this->fZoomWindowFixed;
421 fWindow->inval();
422 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400423 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400424 DisplayParams params = fWindow->getRequestedDisplayParams();
425 params.fDisableVsync = !params.fDisableVsync;
426 fWindow->setRequestedDisplayParams(params);
427 this->updateTitle();
428 fWindow->inval();
429 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400430 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
431 DisplayParams params = fWindow->getRequestedDisplayParams();
432 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
433 fWindow->setRequestedDisplayParams(params);
434 this->updateTitle();
435 fWindow->inval();
436 });
Mike Reedf702ed42019-07-22 17:00:49 -0400437 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
438 fRefresh = !fRefresh;
439 fWindow->inval();
440 });
brianosman622c8d52016-05-10 06:50:49 -0700441 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500442 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700443 fWindow->inval();
444 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400445 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500446 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400447 this->updateTitle();
448 fWindow->inval();
449 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500450 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500451 switch (fColorMode) {
452 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500453 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500454 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500455 case ColorMode::kColorManaged8888:
456 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500457 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500458 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400459 this->setColorMode(ColorMode::kColorManagedF16Norm);
460 break;
461 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500462 this->setColorMode(ColorMode::kLegacy);
463 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500464 }
brianosman622c8d52016-05-10 06:50:49 -0700465 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700466 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
467 DisplayParams params = fWindow->getRequestedDisplayParams();
468 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
469 fWindow->setRequestedDisplayParams(params);
470 fWindow->inval();
471 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400472 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500473 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700474 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400475 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500476 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700477 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400478 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700479 this->changeZoomLevel(1.f / 32.f);
480 fWindow->inval();
481 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400482 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700483 this->changeZoomLevel(-1.f / 32.f);
484 fWindow->inval();
485 });
jvanverthaf236b52016-05-20 06:01:06 -0700486 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400487 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
488 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500489 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400490#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
491 if (newBackend == sk_app::Window::kVulkan_BackendType) {
492 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
493 sk_app::Window::kBackendTypeCount);
494 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
495 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500496 }
497#endif
Brian Osman621491e2017-02-28 15:45:01 -0500498 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700499 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500500 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
501 fSaveToSKP = true;
502 fWindow->inval();
503 });
Mike Reed376d8122019-03-14 11:39:02 -0400504 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
505 fShowSlideDimensions = !fShowSlideDimensions;
506 fWindow->inval();
507 });
Ben Wagner37c54032018-04-13 14:30:23 -0400508 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
509 DisplayParams params = fWindow->getRequestedDisplayParams();
510 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400511 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
512 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
513 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400514 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
515 } else {
516 switch (params.fSurfaceProps.pixelGeometry()) {
517 case kUnknown_SkPixelGeometry:
518 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
519 break;
520 case kRGB_H_SkPixelGeometry:
521 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
522 break;
523 case kBGR_H_SkPixelGeometry:
524 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
525 break;
526 case kRGB_V_SkPixelGeometry:
527 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
528 break;
529 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400530 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
531 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400532 break;
533 }
534 }
535 fWindow->setRequestedDisplayParams(params);
536 this->updateTitle();
537 fWindow->inval();
538 });
Ben Wagner9613e452019-01-23 10:34:59 -0500539 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500540 if (!fFontOverrides.fHinting) {
541 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400542 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500543 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500544 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400545 case SkFontHinting::kNone:
546 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500547 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400548 case SkFontHinting::kSlight:
549 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500550 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400551 case SkFontHinting::kNormal:
552 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500553 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400554 case SkFontHinting::kFull:
555 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500556 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500557 break;
558 }
559 }
560 this->updateTitle();
561 fWindow->inval();
562 });
563 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500564 if (!fPaintOverrides.fAntiAlias) {
565 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
566 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500567 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500568 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500569 } else {
570 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500571 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500572 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500573 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400574 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500575 break;
576 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500577 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500578 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400579 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500580 break;
581 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500582 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400583 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500584 break;
585 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500586 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
587 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500588 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
589 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500590 break;
591 }
592 }
593 this->updateTitle();
594 fWindow->inval();
595 });
Ben Wagner37c54032018-04-13 14:30:23 -0400596 fCommands.addCommand('D', "Modes", "DFT", [this]() {
597 DisplayParams params = fWindow->getRequestedDisplayParams();
598 uint32_t flags = params.fSurfaceProps.flags();
599 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
600 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
601 fWindow->setRequestedDisplayParams(params);
602 this->updateTitle();
603 fWindow->inval();
604 });
Ben Wagner9613e452019-01-23 10:34:59 -0500605 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500606 if (!fFontOverrides.fEdging) {
607 fFontOverrides.fEdging = true;
608 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500609 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500610 switch (fFont.getEdging()) {
611 case SkFont::Edging::kAlias:
612 fFont.setEdging(SkFont::Edging::kAntiAlias);
613 break;
614 case SkFont::Edging::kAntiAlias:
615 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
616 break;
617 case SkFont::Edging::kSubpixelAntiAlias:
618 fFont.setEdging(SkFont::Edging::kAlias);
619 fFontOverrides.fEdging = false;
620 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500621 }
622 }
623 this->updateTitle();
624 fWindow->inval();
625 });
Ben Wagner9613e452019-01-23 10:34:59 -0500626 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500627 if (!fFontOverrides.fSubpixel) {
628 fFontOverrides.fSubpixel = true;
629 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500630 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500631 if (!fFont.isSubpixel()) {
632 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500633 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500634 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500635 }
636 }
637 this->updateTitle();
638 fWindow->inval();
639 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400640 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
641 if (!fFontOverrides.fBaselineSnap) {
642 fFontOverrides.fBaselineSnap = true;
643 fFont.setBaselineSnap(false);
644 } else {
645 if (!fFont.isBaselineSnap()) {
646 fFont.setBaselineSnap(true);
647 } else {
648 fFontOverrides.fBaselineSnap = false;
649 }
650 }
651 this->updateTitle();
652 fWindow->inval();
653 });
Brian Osman805a7272018-05-02 15:40:20 -0400654 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
655 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
656 : kPerspective_Real;
657 this->updateTitle();
658 fWindow->inval();
659 });
660 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
661 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
662 : kPerspective_Off;
663 this->updateTitle();
664 fWindow->inval();
665 });
Brian Osman207d4102019-01-10 09:40:58 -0500666 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
667 fAnimTimer.togglePauseResume();
668 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400669 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
670 fZoomUI = !fZoomUI;
671 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
672 fWindow->inval();
673 });
Mike Reed59295352020-03-12 13:56:34 -0400674 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
675 fDrawViaSerialize = !fDrawViaSerialize;
676 this->updateTitle();
677 fWindow->inval();
678 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500679 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400680 gUseSkVMBlitter = !gUseSkVMBlitter;
681 this->updateTitle();
682 fWindow->inval();
683 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500684 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
685 gSkVMAllowJIT = !gSkVMAllowJIT;
686 this->updateTitle();
687 fWindow->inval();
688 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500689
jvanverth2bb3b6d2016-04-08 07:24:09 -0700690 // set up slides
691 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500692 if (FLAGS_list) {
693 this->listNames();
694 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700695
Brian Osman9bb47cf2018-04-26 15:55:00 -0400696 fPerspectivePoints[0].set(0, 0);
697 fPerspectivePoints[1].set(1, 0);
698 fPerspectivePoints[2].set(0, 1);
699 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700700 fAnimTimer.run();
701
Hal Canaryc465d132017-12-08 10:21:31 -0500702 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500703 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500704 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500705 }
706 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500707
jongdeok.kim804f17e2019-02-26 14:39:23 +0900708 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500709 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700710}
711
jvanverth34524262016-05-04 13:49:13 -0700712void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400713 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
714 static const struct {
715 const char* fExtension;
716 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500717 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400718 const SlideFactory fFactory;
719 } gExternalSlidesInfo[] = {
720 { ".skp", "skp-dir", FLAGS_skps,
721 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
722 return sk_make_sp<SKPSlide>(name, path);}
723 },
724 { ".jpg", "jpg-dir", FLAGS_jpgs,
725 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
726 return sk_make_sp<ImageSlide>(name, path);}
727 },
Florin Malita87ccf332018-05-04 12:23:24 -0400728#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400729 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400730 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
731 return sk_make_sp<SkottieSlide>(name, path);}
732 },
Florin Malita87ccf332018-05-04 12:23:24 -0400733#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400734 #if defined(SK_ENABLE_SKRIVE)
735 { ".flr", "skrive-dir", FLAGS_rives,
736 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
737 return sk_make_sp<SkRiveSlide>(name, path);}
738 },
739 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400740#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400741 { ".svg", "svg-dir", FLAGS_svgs,
742 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
743 return sk_make_sp<SvgSlide>(name, path);}
744 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400745#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400746 };
jvanverthc265a922016-04-08 12:51:45 -0700747
Brian Salomon343553a2018-09-05 15:41:23 -0400748 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700749
Mike Klein88544fb2019-03-20 10:50:33 -0500750 const auto addSlide =
751 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
752 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
753 return;
754 }
liyuqian6f163d22016-06-13 12:26:45 -0700755
Mike Klein88544fb2019-03-20 10:50:33 -0500756 if (auto slide = fact(name, path)) {
757 dirSlides.push_back(slide);
758 fSlides.push_back(std::move(slide));
759 }
760 };
Florin Malita76a076b2018-02-15 18:40:48 -0500761
Florin Malita38792ce2018-05-08 10:36:18 -0400762 if (!FLAGS_file.isEmpty()) {
763 // single file mode
764 const SkString file(FLAGS_file[0]);
765
766 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
767 for (const auto& sinfo : gExternalSlidesInfo) {
768 if (file.endsWith(sinfo.fExtension)) {
769 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
770 return;
771 }
772 }
773
774 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
775 } else {
776 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
777 }
778
779 return;
780 }
781
782 // Bisect slide.
783 if (!FLAGS_bisect.isEmpty()) {
784 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500785 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400786 if (FLAGS_bisect.count() >= 2) {
787 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
788 bisect->onChar(*ch);
789 }
790 }
791 fSlides.push_back(std::move(bisect));
792 }
793 }
794
795 // GMs
796 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400797 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400798 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500799 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400800 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400801 fSlides.push_back(std::move(slide));
802 }
Florin Malita38792ce2018-05-08 10:36:18 -0400803 }
804 // reverse gms
805 int numGMs = fSlides.count() - firstGM;
806 for (int i = 0; i < numGMs/2; ++i) {
807 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
808 }
809
810 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400811 for (const SampleFactory factory : SampleRegistry::Range()) {
812 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500813 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400814 fSlides.push_back(slide);
815 }
Florin Malita38792ce2018-05-08 10:36:18 -0400816 }
817
Brian Osman7c979f52019-02-12 13:27:51 -0500818 // Particle demo
819 {
820 // TODO: Convert this to a sample
821 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500822 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500823 fSlides.push_back(std::move(slide));
824 }
825 }
826
Brian Osmand927bd22019-12-18 11:23:12 -0500827 // Runtime shader editor
828 {
829 sk_sp<Slide> slide(new SkSLSlide());
830 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
831 fSlides.push_back(std::move(slide));
832 }
833 }
834
Florin Malita0ffa3222018-04-05 14:34:45 -0400835 for (const auto& info : gExternalSlidesInfo) {
836 for (const auto& flag : info.fFlags) {
837 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
838 // single file
839 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
840 } else {
841 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400842 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400843 SkTArray<SkString> sortedFilenames;
844 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400845 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400846 sortedFilenames.push_back(name);
847 }
848 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400849 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000850 [](const SkString& a, const SkString& b) {
851 return strcmp(a.c_str(), b.c_str()) < 0;
852 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400853 }
854 for (const SkString& filename : sortedFilenames) {
855 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
856 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400857 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400858 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400859 if (!dirSlides.empty()) {
860 fSlides.push_back(
861 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
862 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500863 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400864 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400865 }
866 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500867
868 if (!fSlides.count()) {
869 sk_sp<Slide> slide(new NullSlide());
870 fSlides.push_back(std::move(slide));
871 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700872}
873
874
jvanverth34524262016-05-04 13:49:13 -0700875Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400876 for(auto& slide : fSlides) {
877 slide->gpuTeardown();
878 }
879
jvanverth9f372462016-04-06 06:08:59 -0700880 fWindow->detach();
881 delete fWindow;
882}
883
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500884struct SkPaintTitleUpdater {
885 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
886 void append(const char* s) {
887 if (fCount == 0) {
888 fTitle->append(" {");
889 } else {
890 fTitle->append(", ");
891 }
892 fTitle->append(s);
893 ++fCount;
894 }
895 void done() {
896 if (fCount > 0) {
897 fTitle->append("}");
898 }
899 }
900 SkString* fTitle;
901 int fCount;
902};
903
brianosman05de2162016-05-06 13:28:57 -0700904void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700905 if (!fWindow) {
906 return;
907 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500908 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700909 return; // Surface hasn't been created yet.
910 }
911
jvanverth34524262016-05-04 13:49:13 -0700912 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700913 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700914
Mike Kleine5acd752019-03-22 09:57:16 -0500915 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400916 if (gSkForceAnalyticAA) {
917 title.append(" <FAAA>");
918 } else {
919 title.append(" <AAA>");
920 }
921 }
Mike Reed59295352020-03-12 13:56:34 -0400922 if (fDrawViaSerialize) {
923 title.append(" <serialize>");
924 }
Mike Reed862818b2020-03-21 15:07:13 -0400925 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500926 title.append(" <SkVMBlitter>");
927 }
928 if (!gSkVMAllowJIT) {
929 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400930 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400931
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500932 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500933 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
934 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400935 const char* on, const char* off)
936 {
Ben Wagner9613e452019-01-23 10:34:59 -0500937 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400938 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500939 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400940 };
941
Ben Wagner9613e452019-01-23 10:34:59 -0500942 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
943 const char* on, const char* off)
944 {
945 if (fFontOverrides.*flag) {
946 paintTitle.append((fFont.*isFlag)() ? on : off);
947 }
948 };
949
950 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
951 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
952
953 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
954 "Force Autohint", "No Force Autohint");
955 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400956 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500957 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
958 "Linear Metrics", "Non-Linear Metrics");
959 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
960 "Bitmap Text", "No Bitmap Text");
961 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
962
963 if (fFontOverrides.fEdging) {
964 switch (fFont.getEdging()) {
965 case SkFont::Edging::kAlias:
966 paintTitle.append("Alias Text");
967 break;
968 case SkFont::Edging::kAntiAlias:
969 paintTitle.append("Antialias Text");
970 break;
971 case SkFont::Edging::kSubpixelAntiAlias:
972 paintTitle.append("Subpixel Antialias Text");
973 break;
974 }
975 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400976
Mike Reed3ae47332019-01-04 10:11:46 -0500977 if (fFontOverrides.fHinting) {
978 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400979 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500980 paintTitle.append("No Hinting");
981 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400982 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500983 paintTitle.append("Slight Hinting");
984 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400985 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500986 paintTitle.append("Normal Hinting");
987 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400988 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500989 paintTitle.append("Full Hinting");
990 break;
991 }
992 }
993 paintTitle.done();
994
Brian Osman92004802017-03-06 11:47:26 -0500995 switch (fColorMode) {
996 case ColorMode::kLegacy:
997 title.append(" Legacy 8888");
998 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500999 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001000 title.append(" ColorManaged 8888");
1001 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001002 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001003 title.append(" ColorManaged F16");
1004 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001005 case ColorMode::kColorManagedF16Norm:
1006 title.append(" ColorManaged F16 Norm");
1007 break;
Brian Osman92004802017-03-06 11:47:26 -05001008 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001009
Brian Osman92004802017-03-06 11:47:26 -05001010 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001011 int curPrimaries = -1;
1012 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1013 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1014 curPrimaries = i;
1015 break;
1016 }
1017 }
Brian Osman03115dc2018-11-26 13:55:19 -05001018 title.appendf(" %s Gamma %f",
1019 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001020 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001021 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001022
Ben Wagner37c54032018-04-13 14:30:23 -04001023 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001024 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001025 switch (params.fSurfaceProps.pixelGeometry()) {
1026 case kUnknown_SkPixelGeometry:
1027 title.append( " Flat");
1028 break;
1029 case kRGB_H_SkPixelGeometry:
1030 title.append( " RGB");
1031 break;
1032 case kBGR_H_SkPixelGeometry:
1033 title.append( " BGR");
1034 break;
1035 case kRGB_V_SkPixelGeometry:
1036 title.append( " RGBV");
1037 break;
1038 case kBGR_V_SkPixelGeometry:
1039 title.append( " BGRV");
1040 break;
1041 }
1042 }
1043
1044 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1045 title.append(" DFT");
1046 }
1047
csmartdalton578f0642017-02-24 16:04:47 -07001048 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001049 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001050 int msaa = fWindow->sampleCount();
1051 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001052 title.appendf(" MSAA: %i", msaa);
1053 }
1054 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001055
1056 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001057 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001058 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1059 }
1060
Brian Osman805a7272018-05-02 15:40:20 -04001061 if (kPerspective_Real == fPerspectiveMode) {
1062 title.append(" Perpsective (Real)");
1063 } else if (kPerspective_Fake == fPerspectiveMode) {
1064 title.append(" Perspective (Fake)");
1065 }
1066
brianosman05de2162016-05-06 13:28:57 -07001067 fWindow->setTitle(title.c_str());
1068}
1069
Florin Malitaab99c342018-01-16 16:23:03 -05001070int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001071
1072 if (!FLAGS_slide.isEmpty()) {
1073 int count = fSlides.count();
1074 for (int i = 0; i < count; i++) {
1075 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001076 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001077 }
1078 }
1079
1080 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1081 this->listNames();
1082 }
1083
Florin Malitaab99c342018-01-16 16:23:03 -05001084 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001085}
1086
Florin Malitaab99c342018-01-16 16:23:03 -05001087void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001088 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001089 for (const auto& slide : fSlides) {
1090 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001091 }
1092}
1093
Florin Malitaab99c342018-01-16 16:23:03 -05001094void Viewer::setCurrentSlide(int slide) {
1095 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001096
Florin Malitaab99c342018-01-16 16:23:03 -05001097 if (slide == fCurrentSlide) {
1098 return;
1099 }
1100
1101 if (fCurrentSlide >= 0) {
1102 fSlides[fCurrentSlide]->unload();
1103 }
1104
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001105 SkScalar scaleFactor = 1.0;
1106 if (fApplyBackingScale) {
1107 scaleFactor = fWindow->scaleFactor();
1108 }
1109 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1110 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001111 fCurrentSlide = slide;
1112 this->setupCurrentSlide();
1113}
1114
1115void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001116 if (fCurrentSlide >= 0) {
1117 // prepare dimensions for image slides
1118 fGesture.resetTouchState();
1119 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001120
Jim Van Verth0848fb02018-01-22 13:39:30 -05001121 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1122 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1123 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001124
Jim Van Verth0848fb02018-01-22 13:39:30 -05001125 // Start with a matrix that scales the slide to the available screen space
1126 if (fWindow->scaleContentToFit()) {
1127 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001128 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1129 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001130 }
liyuqiane46e4f02016-05-20 07:32:19 -07001131 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001132
1133 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001134 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001135
1136 this->updateTitle();
1137 this->updateUIState();
1138
1139 fStatsLayer.resetMeasurements();
1140
1141 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001142 }
jvanverthc265a922016-04-08 12:51:45 -07001143}
1144
Brian Osmanaba642c2020-02-06 12:52:25 -05001145#define MAX_ZOOM_LEVEL 8.0f
1146#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001147
jvanverth34524262016-05-04 13:49:13 -07001148void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001149 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001150 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001151 this->preTouchMatrixChanged();
1152}
Yuqian Li755778c2018-03-28 16:23:31 -04001153
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001154void Viewer::preTouchMatrixChanged() {
1155 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001156 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1157 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1158 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1159 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1160}
1161
Brian Osman805a7272018-05-02 15:40:20 -04001162SkMatrix Viewer::computePerspectiveMatrix() {
1163 SkScalar w = fWindow->width(), h = fWindow->height();
1164 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1165 SkPoint perspPts[4] = {
1166 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1167 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1168 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1169 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1170 };
1171 SkMatrix m;
1172 m.setPolyToPoly(orthoPts, perspPts, 4);
1173 return m;
1174}
1175
Yuqian Li755778c2018-03-28 16:23:31 -04001176SkMatrix Viewer::computePreTouchMatrix() {
1177 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001178
1179 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001180 if (fApplyBackingScale) {
1181 zoomScale *= fWindow->scaleFactor();
1182 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001183 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001184 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001185
1186 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1187 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001188
Brian Osman805a7272018-05-02 15:40:20 -04001189 if (kPerspective_Real == fPerspectiveMode) {
1190 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001191 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001192 }
1193
Yuqian Li755778c2018-03-28 16:23:31 -04001194 return m;
jvanverthc265a922016-04-08 12:51:45 -07001195}
1196
liyuqiand3cdbca2016-05-17 12:44:20 -07001197SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001198 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001199 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001200 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001201 return m;
jvanverthc265a922016-04-08 12:51:45 -07001202}
1203
Brian Osman621491e2017-02-28 15:45:01 -05001204void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001205 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001206 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001207 fBackendType = backendType;
1208
Robert Phillipse9229532020-06-26 10:10:49 -04001209 // The active context is going away in 'detach'
1210 for(auto& slide : fSlides) {
1211 slide->gpuTeardown();
1212 }
1213
Brian Osman621491e2017-02-28 15:45:01 -05001214 fWindow->detach();
1215
Brian Osman70d2f432017-11-08 09:54:10 -05001216#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001217 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1218 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001219 DisplayParams params = fWindow->getRequestedDisplayParams();
1220 delete fWindow;
1221 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001222
Brian Osman70d2f432017-11-08 09:54:10 -05001223 // re-register callbacks
1224 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001225 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001226 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001227 fWindow->pushLayer(&fImGuiLayer);
1228
Brian Osman70d2f432017-11-08 09:54:10 -05001229 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1230 // will still include our correct sample count. But the re-created fWindow will lose that
1231 // information. On Windows, we need to re-create the window when changing sample count,
1232 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1233 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1234 // as if we had never changed windows at all).
1235 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001236#endif
1237
Brian Osman70d2f432017-11-08 09:54:10 -05001238 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001239}
1240
Brian Osman92004802017-03-06 11:47:26 -05001241void Viewer::setColorMode(ColorMode colorMode) {
1242 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001243 this->updateTitle();
1244 fWindow->inval();
1245}
1246
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001247class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1248public:
Mike Reed3ae47332019-01-04 10:11:46 -05001249 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1250 SkFont* font, Viewer::SkFontFields* ffields)
1251 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001252 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001253 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1254 sk_sp<SkTextBlob>* cache) {
1255 bool blobWillChange = false;
1256 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001257 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1258 bool shouldDraw = this->filterFont(&filteredFont);
1259 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001260 blobWillChange = true;
1261 break;
1262 }
1263 }
1264 if (!blobWillChange) {
1265 return blob;
1266 }
1267
1268 SkTextBlobBuilder builder;
1269 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001270 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1271 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001272 if (!shouldDraw) {
1273 continue;
1274 }
1275
Mike Reed3ae47332019-01-04 10:11:46 -05001276 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001277
Ben Wagner41e40472018-09-24 13:01:54 -04001278 const SkTextBlobBuilder::RunBuffer& runBuffer
1279 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001280 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001281 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001282 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001283 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001284 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001285 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001286 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001287 it.glyphCount(), it.textSize(), SkString())
Ben Wagnere5736262021-02-08 16:52:08 -05001288 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
1289 ? SkTextBlobBuilderPriv::AllocRunRSXForm(&builder, font,
1290 it.glyphCount(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001291 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1292 uint32_t glyphCount = it.glyphCount();
1293 if (it.glyphs()) {
1294 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1295 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1296 }
1297 if (it.pos()) {
1298 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001299 unsigned posPerGlyph = it.scalarsPerGlyph();
1300 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001301 }
1302 if (it.text()) {
1303 size_t textSize = sizeof(decltype(*it.text()));
1304 uint32_t textCount = it.textSize();
1305 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1306 }
1307 if (it.clusters()) {
1308 size_t clusterSize = sizeof(decltype(*it.clusters()));
1309 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1310 }
1311 }
1312 *cache = builder.make();
1313 return cache->get();
1314 }
1315 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1316 const SkPaint& paint) override {
1317 sk_sp<SkTextBlob> cache;
1318 this->SkPaintFilterCanvas::onDrawTextBlob(
1319 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1320 }
Mike Reed3ae47332019-01-04 10:11:46 -05001321 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001322 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001323 font->writable()->setSize(fFont->getSize());
1324 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001325 if (fFontOverrides->fScaleX) {
1326 font->writable()->setScaleX(fFont->getScaleX());
1327 }
1328 if (fFontOverrides->fSkewX) {
1329 font->writable()->setSkewX(fFont->getSkewX());
1330 }
Mike Reed3ae47332019-01-04 10:11:46 -05001331 if (fFontOverrides->fHinting) {
1332 font->writable()->setHinting(fFont->getHinting());
1333 }
Ben Wagner9613e452019-01-23 10:34:59 -05001334 if (fFontOverrides->fEdging) {
1335 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001336 }
Ben Wagner9613e452019-01-23 10:34:59 -05001337 if (fFontOverrides->fEmbolden) {
1338 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001339 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001340 if (fFontOverrides->fBaselineSnap) {
1341 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1342 }
Ben Wagner9613e452019-01-23 10:34:59 -05001343 if (fFontOverrides->fLinearMetrics) {
1344 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001345 }
Ben Wagner9613e452019-01-23 10:34:59 -05001346 if (fFontOverrides->fSubpixel) {
1347 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001348 }
Ben Wagner9613e452019-01-23 10:34:59 -05001349 if (fFontOverrides->fEmbeddedBitmaps) {
1350 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001351 }
Ben Wagner9613e452019-01-23 10:34:59 -05001352 if (fFontOverrides->fForceAutoHinting) {
1353 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001354 }
Ben Wagner9613e452019-01-23 10:34:59 -05001355
Mike Reed3ae47332019-01-04 10:11:46 -05001356 return true;
1357 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001358 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001359 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001360 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001361 }
Ben Wagner9613e452019-01-23 10:34:59 -05001362 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001363 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001364 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001365 return true;
1366 }
1367 SkPaint* fPaint;
1368 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001369 SkFont* fFont;
1370 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001371};
1372
Robert Phillips9882dae2019-03-04 11:00:10 -05001373void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001374 if (fCurrentSlide < 0) {
1375 return;
1376 }
1377
Robert Phillips9882dae2019-03-04 11:00:10 -05001378 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001379
Brian Osmanf750fbc2017-02-08 10:47:28 -05001380 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001381 SkSurface* slideSurface = surface;
1382 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001383 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001384
Brian Osmane0d4fba2017-03-15 10:24:55 -04001385 // 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 -05001386 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001387 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001388 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001389 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001390 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001391 }
1392
Brian Osman3ac99cf2017-12-01 11:23:53 -05001393 if (fSaveToSKP) {
1394 SkPictureRecorder recorder;
1395 SkCanvas* recorderCanvas = recorder.beginRecording(
1396 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001397 fSlides[fCurrentSlide]->draw(recorderCanvas);
1398 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1399 SkFILEWStream stream("sample_app.skp");
1400 picture->serialize(&stream);
1401 fSaveToSKP = false;
1402 }
1403
Brian Osmane9ed0f02018-11-26 14:50:05 -05001404 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001405 SkColorType colorType;
1406 switch (fColorMode) {
1407 case ColorMode::kLegacy:
1408 case ColorMode::kColorManaged8888:
1409 colorType = kN32_SkColorType;
1410 break;
1411 case ColorMode::kColorManagedF16:
1412 colorType = kRGBA_F16_SkColorType;
1413 break;
1414 case ColorMode::kColorManagedF16Norm:
1415 colorType = kRGBA_F16Norm_SkColorType;
1416 break;
1417 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001418
1419 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001420 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001421 slideCanvas->getProps(&props);
1422
Brian Osmane9ed0f02018-11-26 14:50:05 -05001423 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1424 return Window::kRaster_BackendType == this->fBackendType
1425 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001426 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001427 };
1428
Brian Osman03115dc2018-11-26 13:55:19 -05001429 // We need to render offscreen if we're...
1430 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1431 // ... in any raster mode, because the window surface is actually GL
1432 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001433 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001434 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001435 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001436 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001437 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001438 colorSpace != nullptr ||
1439 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001440
Brian Osmane9ed0f02018-11-26 14:50:05 -05001441 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001442 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001443 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001444 }
1445
Mike Reed59295352020-03-12 13:56:34 -04001446 SkPictureRecorder recorder;
1447 SkCanvas* recorderRestoreCanvas = nullptr;
1448 if (fDrawViaSerialize) {
1449 recorderRestoreCanvas = slideCanvas;
1450 slideCanvas = recorder.beginRecording(
1451 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1452 }
1453
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001454 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001455 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001456 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001457 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001458 if (fTiled) {
1459 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1460 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001461 for (int y = 0; y < fWindow->height(); y += tileH) {
1462 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001463 SkAutoCanvasRestore acr(slideCanvas, true);
1464 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1465 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001466 }
1467 }
1468
1469 // Draw borders between tiles
1470 if (fDrawTileBoundaries) {
1471 SkPaint border;
1472 border.setColor(0x60FF00FF);
1473 border.setStyle(SkPaint::kStroke_Style);
1474 for (int y = 0; y < fWindow->height(); y += tileH) {
1475 for (int x = 0; x < fWindow->width(); x += tileW) {
1476 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1477 }
1478 }
1479 }
1480 } else {
1481 slideCanvas->concat(this->computeMatrix());
1482 if (kPerspective_Real == fPerspectiveMode) {
1483 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1484 }
Mike Reed3ae47332019-01-04 10:11:46 -05001485 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001486 fSlides[fCurrentSlide]->draw(&filterCanvas);
1487 }
Brian Osman56a24812017-12-19 11:15:16 -05001488 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001489 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001490
Mike Reed59295352020-03-12 13:56:34 -04001491 if (recorderRestoreCanvas) {
1492 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1493 auto data = picture->serialize();
1494 slideCanvas = recorderRestoreCanvas;
1495 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1496 }
1497
Brian Osman1df161a2017-02-09 12:10:20 -05001498 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001499 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001500 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001501 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001502
1503 // If we rendered offscreen, snap an image and push the results to the window's canvas
1504 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001505 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001506
Robert Phillips9882dae2019-03-04 11:00:10 -05001507 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001508 SkPaint paint;
1509 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001510 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001511 int prePerspectiveCount = canvas->save();
1512 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001513 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001514 canvas->clear(SK_ColorWHITE);
1515 canvas->concat(this->computePerspectiveMatrix());
1516 }
Mike Reedb339d052021-01-28 11:20:41 -05001517 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001518 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001519 }
Mike Reed376d8122019-03-14 11:39:02 -04001520
1521 if (fShowSlideDimensions) {
1522 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1523 SkPaint paint;
1524 paint.setColor(0x40FFFF00);
1525 surface->getCanvas()->drawRect(r, paint);
1526 }
liyuqian6f163d22016-06-13 12:26:45 -07001527}
1528
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001529void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001530 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001531 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001532}
Jim Van Verth6f449692017-02-14 15:16:46 -05001533
Robert Phillips9882dae2019-03-04 11:00:10 -05001534void Viewer::onPaint(SkSurface* surface) {
1535 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001536
Robert Phillips9882dae2019-03-04 11:00:10 -05001537 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001538
Brian Osmand67e5182017-12-08 16:46:09 -05001539 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001540
Greg Daniel427d8eb2020-09-28 15:04:18 -04001541 fLastImage.reset();
1542
Robert Phillipsed653392020-07-10 13:55:21 -04001543 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001544 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001545 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001546 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001547}
1548
Ben Wagnera1915972018-08-09 15:06:19 -04001549void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001550 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001551 SkScalar scaleFactor = 1.0;
1552 if (fApplyBackingScale) {
1553 scaleFactor = fWindow->scaleFactor();
1554 }
1555 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001556 }
Ben Wagnera1915972018-08-09 15:06:19 -04001557}
1558
Florin Malitacefc1b92018-02-19 21:43:47 -05001559SkPoint Viewer::mapEvent(float x, float y) {
1560 const auto m = this->computeMatrix();
1561 SkMatrix inv;
1562
1563 SkAssertResult(m.invert(&inv));
1564
1565 return inv.mapXY(x, y);
1566}
1567
Hal Canaryb1f411a2019-08-29 10:39:22 -04001568bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001569 if (GestureDevice::kMouse == fGestureDevice) {
1570 return false;
1571 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001572
1573 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001574 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001575 fWindow->inval();
1576 return true;
1577 }
1578
liyuqiand3cdbca2016-05-17 12:44:20 -07001579 void* castedOwner = reinterpret_cast<void*>(owner);
1580 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001581 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001582 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001583#if defined(SK_BUILD_FOR_IOS)
1584 // TODO: move IOS swipe detection higher up into the platform code
1585 SkPoint dir;
1586 if (fGesture.isFling(&dir)) {
1587 // swiping left or right
1588 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1589 if (dir.fX < 0) {
1590 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1591 fCurrentSlide + 1 : 0);
1592 } else {
1593 this->setCurrentSlide(fCurrentSlide > 0 ?
1594 fCurrentSlide - 1 : fSlides.count() - 1);
1595 }
1596 }
1597 fGesture.reset();
1598 }
1599#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001600 break;
1601 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001602 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001603 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001604 break;
1605 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001606 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001607 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001608 break;
1609 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001610 default: {
1611 // kLeft and kRight are only for swipes
1612 SkASSERT(false);
1613 break;
1614 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001615 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001616 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001617 fWindow->inval();
1618 return true;
1619}
1620
Hal Canaryb1f411a2019-08-29 10:39:22 -04001621bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001622 if (GestureDevice::kTouch == fGestureDevice) {
1623 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001624 }
Brian Osman16c81a12017-12-20 11:58:34 -05001625
Florin Malitacefc1b92018-02-19 21:43:47 -05001626 const auto slidePt = this->mapEvent(x, y);
1627 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1628 fWindow->inval();
1629 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001630 }
1631
1632 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001633 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001634 fGesture.touchEnd(nullptr);
1635 break;
1636 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001637 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001638 fGesture.touchBegin(nullptr, x, y);
1639 break;
1640 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001641 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001642 fGesture.touchMoved(nullptr, x, y);
1643 break;
1644 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001645 default: {
1646 SkASSERT(false); // shouldn't see kRight or kLeft here
1647 break;
1648 }
Brian Osman16c81a12017-12-20 11:58:34 -05001649 }
1650 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1651
Hal Canaryb1f411a2019-08-29 10:39:22 -04001652 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001653 fWindow->inval();
1654 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001655 return true;
1656}
1657
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001658bool Viewer::onFling(skui::InputState state) {
1659 if (skui::InputState::kRight == state) {
1660 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1661 return true;
1662 } else if (skui::InputState::kLeft == state) {
1663 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1664 return true;
1665 }
1666 return false;
1667}
1668
1669bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1670 switch (state) {
1671 case skui::InputState::kDown:
1672 fGesture.startZoom();
1673 return true;
1674 break;
1675 case skui::InputState::kMove:
1676 fGesture.updateZoom(scale, x, y, x, y);
1677 return true;
1678 break;
1679 case skui::InputState::kUp:
1680 fGesture.endZoom();
1681 return true;
1682 break;
1683 default:
1684 SkASSERT(false);
1685 break;
1686 }
1687
1688 return false;
1689}
1690
Brian Osmana109e392017-02-24 09:49:14 -05001691static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001692 // The gamut image covers a (0.8 x 0.9) shaped region
1693 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001694
1695 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1696 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1697 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001698 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1699 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1700 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001701
Brian Osman535c5e32019-02-09 16:32:58 -05001702 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1703 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1704 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1705 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1706 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001707}
1708
Ben Wagner3627d2e2018-06-26 14:23:20 -04001709static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001710 ImGui::DragCanvas dc(pt);
1711 dc.fillColor(IM_COL32(0, 0, 0, 128));
1712 dc.dragPoint(pt);
1713 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001714}
1715
Brian Osman9bb47cf2018-04-26 15:55:00 -04001716static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001717 ImGui::DragCanvas dc(pts);
1718 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001719
Brian Osman535c5e32019-02-09 16:32:58 -05001720 for (int i = 0; i < 4; ++i) {
1721 dc.dragPoint(pts + i);
1722 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001723
Brian Osman535c5e32019-02-09 16:32:58 -05001724 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1725 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1726 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1727 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001728
Brian Osman535c5e32019-02-09 16:32:58 -05001729 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001730}
1731
John Stiles38b7d2f2020-06-24 12:13:31 -04001732static SkSL::String build_sksl_highlight_shader() {
1733 return SkSL::String("out half4 sk_FragColor;\n"
1734 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1735}
1736
1737static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1738 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1739 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1740 size_t pos = inShader.rfind("return *_out;\n");
1741 if (pos == std::string::npos) {
1742 return inShader;
1743 }
1744
1745 SkSL::String replacementShader = inShader;
1746 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1747 return replacementShader;
1748}
1749
1750static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1751 const char* versionDecl = shaderCaps.versionDeclString();
1752 SkSL::String highlight = versionDecl ? versionDecl : "";
1753 if (shaderCaps.usesPrecisionModifiers()) {
1754 highlight.append("precision mediump float;\n");
1755 }
1756 highlight.appendf("out vec4 sk_FragColor;\n"
1757 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1758 return highlight;
1759}
1760
Brian Osmand67e5182017-12-08 16:46:09 -05001761void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001762 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1763 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001764 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001765 }
1766
1767 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001768 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1769 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001770 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001771 DisplayParams params = fWindow->getRequestedDisplayParams();
1772 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001773 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001774
Brian Osmana109e392017-02-24 09:49:14 -05001775 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1776 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001777 if (ImGui::CollapsingHeader("Backend")) {
1778 int newBackend = static_cast<int>(fBackendType);
1779 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1780 ImGui::SameLine();
1781 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001782#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1783 ImGui::SameLine();
1784 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1785#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001786#if defined(SK_DAWN)
1787 ImGui::SameLine();
1788 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1789#endif
John Stilesf7da9232020-11-19 19:58:14 -05001790#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001791 ImGui::SameLine();
1792 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1793#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001794#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001795 ImGui::SameLine();
1796 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1797#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001798#if defined(SK_DIRECT3D)
1799 ImGui::SameLine();
1800 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1801#endif
Brian Osman621491e2017-02-28 15:45:01 -05001802 if (newBackend != fBackendType) {
1803 fDeferredActions.push_back([=]() {
1804 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1805 });
1806 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001807
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001808 bool* wire = &params.fGrContextOptions.fWireframeMode;
1809 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1810 paramsChanged = true;
1811 }
Brian Salomon99a33902017-03-07 15:16:34 -05001812
Brian Osman28b12522017-03-08 17:10:24 -05001813 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001814 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001815 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001816 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1817 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1818 1;
1819
1820 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1821 if (maxMSAA >= 4) {
1822 ImGui::Text("MSAA: ");
1823
1824 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1825 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1826 // don't show it in the list.
1827 if (curMSAA == 2) {
1828 continue;
1829 }
1830 ImGui::SameLine();
1831 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1832 &sampleCount, curMSAA);
1833 }
1834 }
Brian Osman28b12522017-03-08 17:10:24 -05001835
1836 if (sampleCount != params.fMSAASampleCount) {
1837 params.fMSAASampleCount = sampleCount;
1838 paramsChanged = true;
1839 }
1840 }
1841
Ben Wagner37c54032018-04-13 14:30:23 -04001842 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001843 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001844 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1845 }
1846 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1847 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1848 {
1849 uint32_t flags = params.fSurfaceProps.flags();
1850 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001851 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1852 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1853 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001854 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001855 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001856 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1857 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1858 }
1859 paramsChanged = true;
1860 }
1861
1862 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1863 if (ImGui::Checkbox("DFT", &useDFT)) {
1864 uint32_t flags = params.fSurfaceProps.flags();
1865 if (useDFT) {
1866 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1867 } else {
1868 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1869 }
1870 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1871 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1872 paramsChanged = true;
1873 }
1874
Brian Osman8a9de3d2017-03-01 14:59:05 -05001875 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001876 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001877 auto prButton = [&](GpuPathRenderers x) {
1878 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001879 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1880 params.fGrContextOptions.fGpuPathRenderers = x;
1881 paramsChanged = true;
1882 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001883 }
1884 };
1885
1886 if (!ctx) {
1887 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001888 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001889 const auto* caps = ctx->priv().caps();
1890 prButton(GpuPathRenderers::kDefault);
1891 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001892 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001893 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001894 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001895 if (caps->shaderCaps()->pathRenderingSupport()) {
1896 prButton(GpuPathRenderers::kStencilAndCover);
1897 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001898 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001899 if (1 == fWindow->sampleCount()) {
1900 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1901 prButton(GpuPathRenderers::kCoverageCounting);
1902 }
1903 prButton(GpuPathRenderers::kSmall);
1904 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001905 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001906 prButton(GpuPathRenderers::kNone);
1907 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001908 ImGui::TreePop();
1909 }
Brian Osman621491e2017-02-28 15:45:01 -05001910 }
1911
Ben Wagner964571d2019-03-08 12:35:06 -05001912 if (ImGui::CollapsingHeader("Tiling")) {
1913 ImGui::Checkbox("Enable", &fTiled);
1914 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1915 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1916 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1917 }
1918
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001919 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001920 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1921 this->preTouchMatrixChanged();
1922 this->onResize(fWindow->width(), fWindow->height());
1923 paramsChanged = true;
1924 }
1925
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001926 float zoom = fZoomLevel;
1927 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1928 fZoomLevel = zoom;
1929 this->preTouchMatrixChanged();
1930 paramsChanged = true;
1931 }
1932 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001933 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001934 fRotation = deg;
1935 this->preTouchMatrixChanged();
1936 paramsChanged = true;
1937 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001938 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1939 if (ImGui_DragLocation(&fOffset)) {
1940 this->preTouchMatrixChanged();
1941 paramsChanged = true;
1942 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001943 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1944 this->preTouchMatrixChanged();
1945 paramsChanged = true;
1946 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001947 }
Brian Osman805a7272018-05-02 15:40:20 -04001948 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1949 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1950 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001951 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001952 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001953 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001954 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001955 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001956 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001957 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001958 }
1959
Ben Wagnera580fb32018-04-17 11:16:32 -04001960 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001961 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001962 if (fPaintOverrides.fAntiAlias) {
1963 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001964 }
1965 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001966 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001967 {
1968 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1969 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001970 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001971 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1972 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001973 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001974 fPaintOverrides.fAntiAlias = true;
1975 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001976 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001977 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001978 case SkPaintFields::AntiAliasState::Alias:
1979 break;
1980 case SkPaintFields::AntiAliasState::Normal:
1981 break;
1982 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1983 gSkUseAnalyticAA = true;
1984 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001985 break;
1986 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1987 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001988 break;
1989 }
1990 }
1991 paramsChanged = true;
1992 }
1993
Ben Wagner99a78dc2018-05-09 18:23:51 -04001994 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001995 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001996 bool (SkPaint::* isFlag)() const,
1997 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001998 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001999 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002000 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002001 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002002 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002003 if (ImGui::Combo(label, &itemIndex, items)) {
2004 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002005 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002006 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002007 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002008 (fPaint.*setFlag)(itemIndex == 2);
2009 }
2010 paramsChanged = true;
2011 }
2012 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002013
Ben Wagner99a78dc2018-05-09 18:23:51 -04002014 paintFlag("Dither",
2015 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002016 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002017 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagner9613e452019-01-23 10:34:59 -05002018 }
Hal Canary02738a82019-01-21 18:51:32 +00002019
Ben Wagner9613e452019-01-23 10:34:59 -05002020 if (ImGui::CollapsingHeader("Font")) {
2021 int hintingIdx = 0;
2022 if (fFontOverrides.fHinting) {
2023 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2024 }
2025 if (ImGui::Combo("Hinting", &hintingIdx,
2026 "Default\0None\0Slight\0Normal\0Full\0\0"))
2027 {
2028 if (hintingIdx == 0) {
2029 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002030 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002031 } else {
2032 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2033 fFontOverrides.fHinting = true;
2034 }
2035 paramsChanged = true;
2036 }
Hal Canary02738a82019-01-21 18:51:32 +00002037
Ben Wagner9613e452019-01-23 10:34:59 -05002038 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2039 bool SkFontFields::* flag,
2040 bool (SkFont::* isFlag)() const,
2041 void (SkFont::* setFlag)(bool) )
2042 {
2043 int itemIndex = 0;
2044 if (fFontOverrides.*flag) {
2045 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2046 }
2047 if (ImGui::Combo(label, &itemIndex, items)) {
2048 if (itemIndex == 0) {
2049 fFontOverrides.*flag = false;
2050 } else {
2051 fFontOverrides.*flag = true;
2052 (fFont.*setFlag)(itemIndex == 2);
2053 }
2054 paramsChanged = true;
2055 }
2056 };
Hal Canary02738a82019-01-21 18:51:32 +00002057
Ben Wagner9613e452019-01-23 10:34:59 -05002058 fontFlag("Fake Bold Glyphs",
2059 "Default\0No Fake Bold\0Fake Bold\0\0",
2060 &SkFontFields::fEmbolden,
2061 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002062
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002063 fontFlag("Baseline Snapping",
2064 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2065 &SkFontFields::fBaselineSnap,
2066 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2067
Ben Wagner9613e452019-01-23 10:34:59 -05002068 fontFlag("Linear Text",
2069 "Default\0No Linear Text\0Linear Text\0\0",
2070 &SkFontFields::fLinearMetrics,
2071 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002072
Ben Wagner9613e452019-01-23 10:34:59 -05002073 fontFlag("Subpixel Position Glyphs",
2074 "Default\0Pixel Text\0Subpixel Text\0\0",
2075 &SkFontFields::fSubpixel,
2076 &SkFont::isSubpixel, &SkFont::setSubpixel);
2077
2078 fontFlag("Embedded Bitmap Text",
2079 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2080 &SkFontFields::fEmbeddedBitmaps,
2081 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2082
2083 fontFlag("Force Auto-Hinting",
2084 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2085 &SkFontFields::fForceAutoHinting,
2086 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2087
2088 int edgingIdx = 0;
2089 if (fFontOverrides.fEdging) {
2090 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2091 }
2092 if (ImGui::Combo("Edging", &edgingIdx,
2093 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2094 {
2095 if (edgingIdx == 0) {
2096 fFontOverrides.fEdging = false;
2097 fFont.setEdging(SkFont::Edging::kAlias);
2098 } else {
2099 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2100 fFontOverrides.fEdging = true;
2101 }
2102 paramsChanged = true;
2103 }
2104
Ben Wagner15a8d572019-03-21 13:35:44 -04002105 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2106 if (fFontOverrides.fSize) {
2107 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002108 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002109 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002110 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002111 fFontOverrides.fSizeRange[0],
2112 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002113 "%.6f", 2.0f))
2114 {
Mike Reed3ae47332019-01-04 10:11:46 -05002115 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002116 paramsChanged = true;
2117 }
2118 }
2119
2120 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2121 if (fFontOverrides.fScaleX) {
2122 float scaleX = fFont.getScaleX();
2123 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2124 fFont.setScaleX(scaleX);
2125 paramsChanged = true;
2126 }
2127 }
2128
2129 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2130 if (fFontOverrides.fSkewX) {
2131 float skewX = fFont.getSkewX();
2132 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2133 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002134 paramsChanged = true;
2135 }
2136 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002137 }
2138
Mike Reed81f60ec2018-05-15 10:09:52 -04002139 {
2140 SkMetaData controls;
2141 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2142 if (ImGui::CollapsingHeader("Current Slide")) {
2143 SkMetaData::Iter iter(controls);
2144 const char* name;
2145 SkMetaData::Type type;
2146 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002147 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002148 if (type == SkMetaData::kScalar_Type) {
2149 float val[3];
2150 SkASSERT(count == 3);
2151 controls.findScalars(name, &count, val);
2152 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2153 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002154 }
Ben Wagner110c7032019-03-22 17:03:59 -04002155 } else if (type == SkMetaData::kBool_Type) {
2156 bool val;
2157 SkASSERT(count == 1);
2158 controls.findBool(name, &val);
2159 if (ImGui::Checkbox(name, &val)) {
2160 controls.setBool(name, val);
2161 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002162 }
2163 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002164 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002165 }
2166 }
2167 }
2168
Ben Wagner7a3c6742018-04-23 10:01:07 -04002169 if (fShowSlidePicker) {
2170 ImGui::SetNextTreeNodeOpen(true);
2171 }
Brian Osman79086b92017-02-10 13:36:16 -05002172 if (ImGui::CollapsingHeader("Slide")) {
2173 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002174 static ImVector<const char*> filteredSlideNames;
2175 static ImVector<int> filteredSlideIndices;
2176
Brian Osmanfce09c52017-11-14 15:32:20 -05002177 if (fShowSlidePicker) {
2178 ImGui::SetKeyboardFocusHere();
2179 fShowSlidePicker = false;
2180 }
2181
Brian Osman79086b92017-02-10 13:36:16 -05002182 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002183 filteredSlideNames.clear();
2184 filteredSlideIndices.clear();
2185 int filteredIndex = 0;
2186 for (int i = 0; i < fSlides.count(); ++i) {
2187 const char* slideName = fSlides[i]->getName().c_str();
2188 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2189 if (i == fCurrentSlide) {
2190 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002191 }
Brian Osmanf479e422017-11-08 13:11:36 -05002192 filteredSlideNames.push_back(slideName);
2193 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002194 }
Brian Osman79086b92017-02-10 13:36:16 -05002195 }
Brian Osmanf479e422017-11-08 13:11:36 -05002196
Brian Osmanf479e422017-11-08 13:11:36 -05002197 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2198 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002199 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002200 }
2201 }
Brian Osmana109e392017-02-24 09:49:14 -05002202
2203 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002204 ColorMode newMode = fColorMode;
2205 auto cmButton = [&](ColorMode mode, const char* label) {
2206 if (ImGui::RadioButton(label, mode == fColorMode)) {
2207 newMode = mode;
2208 }
2209 };
2210
2211 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002212 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2213 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002214 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002215
2216 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002217 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002218 }
2219
2220 // Pick from common gamuts:
2221 int primariesIdx = 4; // Default: Custom
2222 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2223 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2224 primariesIdx = i;
2225 break;
2226 }
2227 }
2228
Brian Osman03115dc2018-11-26 13:55:19 -05002229 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002230 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002231
Brian Osmana109e392017-02-24 09:49:14 -05002232 if (ImGui::Combo("Primaries", &primariesIdx,
2233 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2234 if (primariesIdx >= 0 && primariesIdx <= 3) {
2235 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2236 }
2237 }
2238
2239 // Allow direct editing of gamut
2240 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2241 }
Brian Osman207d4102019-01-10 09:40:58 -05002242
2243 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002244 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002245 if (ImGui::Checkbox("Pause", &isPaused)) {
2246 fAnimTimer.togglePauseResume();
2247 }
Brian Osman707d2022019-01-10 11:27:34 -05002248
2249 float speed = fAnimTimer.getSpeed();
2250 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2251 fAnimTimer.setSpeed(speed);
2252 }
Brian Osman207d4102019-01-10 09:40:58 -05002253 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002254
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002255 if (ImGui::CollapsingHeader("Shaders")) {
2256 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2257 GrContextOptions::ShaderCacheStrategy::kSkSL;
2258#if defined(SK_VULKAN)
2259 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2260#else
2261 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002262#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002263
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002264 // To re-load shaders from the currently active programs, we flush all
2265 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002266 static bool gLoadPending = false;
2267 if (gLoadPending) {
2268 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2269 int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002270 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002271 entry.fKey = key;
2272 SkMD5 hash;
2273 hash.write(key->bytes(), key->size());
2274 SkMD5::Digest digest = hash.finish();
2275 for (int i = 0; i < 16; ++i) {
2276 entry.fKeyString.appendf("%02x", digest.data[i]);
2277 }
2278
Brian Osman9e4e4c72020-06-10 07:19:34 -04002279 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002280 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002281 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2282 entry.fInputs,
2283 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002284 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002285 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002286 fPersistentCache.foreach(collectShaders);
2287 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002288
2289#if defined(SK_VULKAN)
2290 if (isVulkan && !sksl) {
2291 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2292 for (auto& entry : fCachedShaders) {
2293 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2294 const SkSL::String& spirv(entry.fShader[i]);
2295 std::string disasm;
2296 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2297 &disasm);
2298 entry.fShader[i].assign(disasm);
2299 }
2300 }
2301 }
2302#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002303 }
2304
2305 // Defer actually doing the load/save logic so that we can trigger a save when we
2306 // start or finish hovering on a tree node in the list below:
2307 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002308 bool doSave = ImGui::Button("Save"); ImGui::SameLine();
2309 if (ImGui::Checkbox("SkSL", &sksl)) {
2310 params.fGrContextOptions.fShaderCacheStrategy =
2311 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2312 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2313 paramsChanged = true;
2314 doLoad = true;
2315 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
Brian Osmancbc33b82019-04-19 14:16:19 -04002316 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002317
2318 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002319 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002320 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2321 bool hovered = ImGui::IsItemHovered();
2322 if (hovered != entry.fHovered) {
2323 // Force a save to patch the highlight shader in/out
2324 entry.fHovered = hovered;
2325 doSave = true;
2326 }
2327 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002328 auto stringBox = [](const char* label, std::string* str) {
2329 // Full width, and not too much space for each shader
2330 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2331 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2332 ImGui::InputTextMultiline(label, str, boxSize);
2333 };
2334 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2335 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002336 ImGui::TreePop();
2337 }
2338 }
2339 ImGui::EndChild();
2340
2341 if (doLoad) {
2342 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002343 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002344 gLoadPending = true;
2345 }
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002346 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2347 // but I'm not sure anyone wants to do that.
2348 if (isVulkan && !sksl) {
2349 doSave = false;
2350 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002351 if (doSave) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002352 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002353 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002354 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002355 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002356 if (entry.fHovered) {
2357 // The hovered item (if any) gets a special shader to make it
2358 // identifiable.
2359 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2360 switch (entry.fShaderType) {
2361 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2362 fragShader = build_sksl_highlight_shader();
2363 break;
2364 }
2365 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2366 fragShader = build_glsl_highlight_shader(
2367 *ctx->priv().caps()->shaderCaps());
2368 break;
2369 }
2370 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2371 fragShader = build_metal_highlight_shader(fragShader);
2372 break;
2373 }
2374 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002375 }
2376
Brian Osmana085a412019-04-25 09:44:43 -04002377 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2378 entry.fShader,
2379 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002380 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002381 fPersistentCache.store(*entry.fKey, *data);
2382
2383 entry.fShader[kFragment_GrShaderType] = backup;
2384 }
2385 }
2386 }
Brian Osman79086b92017-02-10 13:36:16 -05002387 }
Brian Salomon99a33902017-03-07 15:16:34 -05002388 if (paramsChanged) {
2389 fDeferredActions.push_back([=]() {
2390 fWindow->setRequestedDisplayParams(params);
2391 fWindow->inval();
2392 this->updateTitle();
2393 });
2394 }
Brian Osman79086b92017-02-10 13:36:16 -05002395 ImGui::End();
2396 }
2397
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002398 if (gShaderErrorHandler.fErrors.count()) {
2399 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002400 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002401 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2402 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002403 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2404 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2405 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2406 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002407 }
2408 ImGui::End();
2409 gShaderErrorHandler.reset();
2410 }
2411
Brian Osmanf6877092017-02-13 09:39:57 -05002412 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002413 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2414 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002415 static int zoomFactor = 8;
2416 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002417 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002418 }
2419 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2420 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002421 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002422 }
Brian Osmanf6877092017-02-13 09:39:57 -05002423
Ben Wagner3627d2e2018-06-26 14:23:20 -04002424 if (!fZoomWindowFixed) {
2425 ImVec2 mousePos = ImGui::GetMousePos();
2426 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2427 }
2428 SkScalar x = fZoomWindowLocation.x();
2429 SkScalar y = fZoomWindowLocation.y();
2430 int xInt = SkScalarRoundToInt(x);
2431 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002432 ImVec2 avail = ImGui::GetContentRegionAvail();
2433
Brian Osmanead517d2017-11-13 15:36:36 -05002434 uint32_t pixel = 0;
2435 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002436 auto dContext = fWindow->directContext();
2437 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002438 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002439 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002440 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002441 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002442 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2443 }
2444
Greg Danielfbc60b72020-11-03 17:27:02 -05002445 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002446 // Translate so the region of the image that's under the mouse cursor is centered
2447 // in the zoom canvas:
2448 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002449 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2450 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002451 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002452
2453 SkPaint outline;
2454 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002455 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002456 });
Brian Osmanf6877092017-02-13 09:39:57 -05002457 }
2458
2459 ImGui::End();
2460 }
Brian Osman79086b92017-02-10 13:36:16 -05002461}
2462
liyuqian2edb0f42016-07-06 14:11:32 -07002463void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002464 for (int i = 0; i < fDeferredActions.count(); ++i) {
2465 fDeferredActions[i]();
2466 }
2467 fDeferredActions.reset();
2468
Brian Osman56a24812017-12-19 11:15:16 -05002469 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002470 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002471 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002472 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002473
Brian Osman79086b92017-02-10 13:36:16 -05002474 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002475 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2476 // not be visible, though. So we need to redraw if there is at least one visible window, or
2477 // more than one active window. Newly created windows are active but not visible for one frame
2478 // while they determine their layout and sizing.
2479 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2480 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002481 fWindow->inval();
2482 }
jvanverth9f372462016-04-06 06:08:59 -07002483}
liyuqiane5a6cd92016-05-27 08:52:52 -07002484
Florin Malitab632df72018-06-18 21:23:06 -04002485template <typename OptionsFunc>
2486static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2487 OptionsFunc&& optionsFunc) {
2488 writer.beginObject();
2489 {
2490 writer.appendString(kName , name);
2491 writer.appendString(kValue, value);
2492
2493 writer.beginArray(kOptions);
2494 {
2495 optionsFunc(writer);
2496 }
2497 writer.endArray();
2498 }
2499 writer.endObject();
2500}
2501
2502
liyuqiane5a6cd92016-05-27 08:52:52 -07002503void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002504 if (!fWindow) {
2505 return;
2506 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002507 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002508 return; // Surface hasn't been created yet.
2509 }
2510
Florin Malitab632df72018-06-18 21:23:06 -04002511 SkDynamicMemoryWStream memStream;
2512 SkJSONWriter writer(&memStream);
2513 writer.beginArray();
2514
liyuqianb73c24b2016-06-03 08:47:23 -07002515 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002516 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2517 [this](SkJSONWriter& writer) {
2518 for(const auto& slide : fSlides) {
2519 writer.appendString(slide->getName().c_str());
2520 }
2521 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002522
liyuqianb73c24b2016-06-03 08:47:23 -07002523 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002524 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2525 [](SkJSONWriter& writer) {
2526 for (const auto& str : kBackendTypeStrings) {
2527 writer.appendString(str);
2528 }
2529 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002530
csmartdalton578f0642017-02-24 16:04:47 -07002531 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002532 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2533 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2534 [this](SkJSONWriter& writer) {
2535 writer.appendS32(0);
2536
2537 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2538 return;
2539 }
2540
2541 for (int msaa : {4, 8, 16}) {
2542 writer.appendS32(msaa);
2543 }
2544 });
csmartdalton578f0642017-02-24 16:04:47 -07002545
csmartdalton61cd31a2017-02-27 17:00:53 -07002546 // Path renderer state
2547 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002548 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2549 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002550 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002551 if (!ctx) {
2552 writer.appendString("Software");
2553 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002554 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002555 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2556 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002557 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002558 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002559 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002560 }
Florin Malitab632df72018-06-18 21:23:06 -04002561 if (caps->shaderCaps()->pathRenderingSupport()) {
2562 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002563 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002564 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002565 }
2566 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002567 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2568 writer.appendString(
2569 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2570 }
2571 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2572 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002573 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002574 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002575 }
2576 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002577
liyuqianb73c24b2016-06-03 08:47:23 -07002578 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002579 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2580 [this](SkJSONWriter& writer) {
2581 writer.appendString(kSoftkeyHint);
2582 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2583 writer.appendString(softkey.c_str());
2584 }
2585 });
liyuqianb73c24b2016-06-03 08:47:23 -07002586
Florin Malitab632df72018-06-18 21:23:06 -04002587 writer.endArray();
2588 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002589
Florin Malitab632df72018-06-18 21:23:06 -04002590 auto data = memStream.detachAsData();
2591
2592 // TODO: would be cool to avoid this copy
2593 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2594
2595 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002596}
2597
2598void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002599 // For those who will add more features to handle the state change in this function:
2600 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2601 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2602 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002603 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002604 for (int i = 0; i < fSlides.count(); ++i) {
2605 if (fSlides[i]->getName().equals(stateValue)) {
2606 this->setCurrentSlide(i);
2607 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002608 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002609 }
Florin Malitaab99c342018-01-16 16:23:03 -05002610
2611 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002612 } else if (stateName.equals(kBackendStateName)) {
2613 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2614 if (stateValue.equals(kBackendTypeStrings[i])) {
2615 if (fBackendType != i) {
2616 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002617 for(auto& slide : fSlides) {
2618 slide->gpuTeardown();
2619 }
liyuqian6cb70252016-06-02 12:16:25 -07002620 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002621 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002622 }
2623 break;
2624 }
2625 }
csmartdalton578f0642017-02-24 16:04:47 -07002626 } else if (stateName.equals(kMSAAStateName)) {
2627 DisplayParams params = fWindow->getRequestedDisplayParams();
2628 int sampleCount = atoi(stateValue.c_str());
2629 if (sampleCount != params.fMSAASampleCount) {
2630 params.fMSAASampleCount = sampleCount;
2631 fWindow->setRequestedDisplayParams(params);
2632 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002633 this->updateTitle();
2634 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002635 }
2636 } else if (stateName.equals(kPathRendererStateName)) {
2637 DisplayParams params = fWindow->getRequestedDisplayParams();
2638 for (const auto& pair : gPathRendererNames) {
2639 if (pair.second == stateValue.c_str()) {
2640 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2641 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2642 fWindow->setRequestedDisplayParams(params);
2643 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002644 this->updateTitle();
2645 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002646 }
2647 break;
2648 }
csmartdalton578f0642017-02-24 16:04:47 -07002649 }
liyuqianb73c24b2016-06-03 08:47:23 -07002650 } else if (stateName.equals(kSoftkeyStateName)) {
2651 if (!stateValue.equals(kSoftkeyHint)) {
2652 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002653 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002654 }
liyuqian2edb0f42016-07-06 14:11:32 -07002655 } else if (stateName.equals(kRefreshStateName)) {
2656 // This state is actually NOT in the UI state.
2657 // We use this to allow Android to quickly set bool fRefresh.
2658 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002659 } else {
2660 SkDebugf("Unknown stateName: %s", stateName.c_str());
2661 }
2662}
Brian Osman79086b92017-02-10 13:36:16 -05002663
Hal Canaryb1f411a2019-08-29 10:39:22 -04002664bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002665 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002666}
2667
Hal Canaryb1f411a2019-08-29 10:39:22 -04002668bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002669 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002670 fWindow->inval();
2671 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002672 } else {
2673 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002674 }
Brian Osman79086b92017-02-10 13:36:16 -05002675}