blob: 81746e863597afe6598d6eae3003c4fc94a56f8d [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)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700324 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400325 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400326 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400327 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500328 , fTiled(false)
329 , fDrawTileBoundaries(false)
330 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400331 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700332{
Greg Daniel285db442016-10-14 09:12:53 -0400333 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700334
Chris Dalton37ae4b02019-12-28 14:51:11 -0700335 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600336 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500337 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500338 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600339 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600340 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500341 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700342
jvanverth2bb3b6d2016-04-08 07:24:09 -0700343 SkDebugf("Command line arguments: ");
344 for (int i = 1; i < argc; ++i) {
345 SkDebugf("%s ", argv[i]);
346 }
347 SkDebugf("\n");
348
Mike Klein88544fb2019-03-20 10:50:33 -0500349 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500350#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400351 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500352#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700353
Mike Reed862818b2020-03-21 15:07:13 -0400354 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500355 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500356 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400357
Mike Klein19cc0f62019-03-22 15:30:07 -0500358 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500359
Brian Osmanbc8150f2017-07-24 11:38:01 -0400360 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400361 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400362
bsalomon6c471f72016-07-26 12:56:32 -0700363 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700364 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700365
csmartdalton578f0642017-02-24 16:04:47 -0700366 DisplayParams displayParams;
367 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500368 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700369 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400370 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400371 displayParams.fGrContextOptions.fShaderCacheStrategy =
372 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400373 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
374 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700375 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400376 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500377 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700378
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500379 // computePreTouchMatrix uses exp(fZoomLevel) to compute the scale.
380 float scaleFactor = fWindow->scaleFactor();
381 fZoomLevel = log(scaleFactor);
382 fImGuiLayer.setScaleFactor(scaleFactor);
383
Brian Osman56a24812017-12-19 11:15:16 -0500384 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500385 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500386 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
387 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
388 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
389
jvanverth9f372462016-04-06 06:08:59 -0700390 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700391 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500392 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500393 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500394 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700395
brianosman622c8d52016-05-10 06:50:49 -0700396 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500397 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
398 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
399 fWindow->inval();
400 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500401 // Command to jump directly to the slide picker and give it focus
402 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
403 this->fShowImGuiDebugWindow = true;
404 this->fShowSlidePicker = true;
405 fWindow->inval();
406 });
407 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400408 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500409 this->fShowImGuiDebugWindow = true;
410 this->fShowSlidePicker = true;
411 fWindow->inval();
412 });
Brian Osman79086b92017-02-10 13:36:16 -0500413 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
414 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
415 fWindow->inval();
416 });
Brian Osmanf6877092017-02-13 09:39:57 -0500417 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
418 this->fShowZoomWindow = !this->fShowZoomWindow;
419 fWindow->inval();
420 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400421 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
422 this->fZoomWindowFixed = !this->fZoomWindowFixed;
423 fWindow->inval();
424 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400425 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400426 DisplayParams params = fWindow->getRequestedDisplayParams();
427 params.fDisableVsync = !params.fDisableVsync;
428 fWindow->setRequestedDisplayParams(params);
429 this->updateTitle();
430 fWindow->inval();
431 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400432 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
433 DisplayParams params = fWindow->getRequestedDisplayParams();
434 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
435 fWindow->setRequestedDisplayParams(params);
436 this->updateTitle();
437 fWindow->inval();
438 });
Mike Reedf702ed42019-07-22 17:00:49 -0400439 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
440 fRefresh = !fRefresh;
441 fWindow->inval();
442 });
brianosman622c8d52016-05-10 06:50:49 -0700443 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500444 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700445 fWindow->inval();
446 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400447 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500448 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400449 this->updateTitle();
450 fWindow->inval();
451 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500452 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500453 switch (fColorMode) {
454 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500455 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500456 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500457 case ColorMode::kColorManaged8888:
458 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500459 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500460 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400461 this->setColorMode(ColorMode::kColorManagedF16Norm);
462 break;
463 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500464 this->setColorMode(ColorMode::kLegacy);
465 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500466 }
brianosman622c8d52016-05-10 06:50:49 -0700467 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700468 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
469 DisplayParams params = fWindow->getRequestedDisplayParams();
470 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
471 fWindow->setRequestedDisplayParams(params);
472 fWindow->inval();
473 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400474 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500475 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700476 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400477 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500478 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700479 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400480 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700481 this->changeZoomLevel(1.f / 32.f);
482 fWindow->inval();
483 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400484 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700485 this->changeZoomLevel(-1.f / 32.f);
486 fWindow->inval();
487 });
jvanverthaf236b52016-05-20 06:01:06 -0700488 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400489 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
490 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500491 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400492#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
493 if (newBackend == sk_app::Window::kVulkan_BackendType) {
494 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
495 sk_app::Window::kBackendTypeCount);
496 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
497 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500498 }
499#endif
Brian Osman621491e2017-02-28 15:45:01 -0500500 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700501 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500502 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
503 fSaveToSKP = true;
504 fWindow->inval();
505 });
Mike Reed376d8122019-03-14 11:39:02 -0400506 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
507 fShowSlideDimensions = !fShowSlideDimensions;
508 fWindow->inval();
509 });
Ben Wagner37c54032018-04-13 14:30:23 -0400510 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
511 DisplayParams params = fWindow->getRequestedDisplayParams();
512 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400513 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
514 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
515 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400516 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
517 } else {
518 switch (params.fSurfaceProps.pixelGeometry()) {
519 case kUnknown_SkPixelGeometry:
520 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
521 break;
522 case kRGB_H_SkPixelGeometry:
523 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
524 break;
525 case kBGR_H_SkPixelGeometry:
526 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
527 break;
528 case kRGB_V_SkPixelGeometry:
529 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
530 break;
531 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400532 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
533 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400534 break;
535 }
536 }
537 fWindow->setRequestedDisplayParams(params);
538 this->updateTitle();
539 fWindow->inval();
540 });
Ben Wagner9613e452019-01-23 10:34:59 -0500541 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500542 if (!fFontOverrides.fHinting) {
543 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400544 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500545 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500546 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400547 case SkFontHinting::kNone:
548 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500549 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400550 case SkFontHinting::kSlight:
551 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500552 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400553 case SkFontHinting::kNormal:
554 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500555 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400556 case SkFontHinting::kFull:
557 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500558 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500559 break;
560 }
561 }
562 this->updateTitle();
563 fWindow->inval();
564 });
565 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500566 if (!fPaintOverrides.fAntiAlias) {
567 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
568 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500569 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500570 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500571 } else {
572 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500573 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500574 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500575 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400576 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500577 break;
578 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500579 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500580 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400581 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500582 break;
583 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500584 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400585 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500586 break;
587 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500588 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
589 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500590 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
591 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500592 break;
593 }
594 }
595 this->updateTitle();
596 fWindow->inval();
597 });
Ben Wagner37c54032018-04-13 14:30:23 -0400598 fCommands.addCommand('D', "Modes", "DFT", [this]() {
599 DisplayParams params = fWindow->getRequestedDisplayParams();
600 uint32_t flags = params.fSurfaceProps.flags();
601 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
602 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
603 fWindow->setRequestedDisplayParams(params);
604 this->updateTitle();
605 fWindow->inval();
606 });
Ben Wagner9613e452019-01-23 10:34:59 -0500607 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500608 if (!fFontOverrides.fEdging) {
609 fFontOverrides.fEdging = true;
610 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500611 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500612 switch (fFont.getEdging()) {
613 case SkFont::Edging::kAlias:
614 fFont.setEdging(SkFont::Edging::kAntiAlias);
615 break;
616 case SkFont::Edging::kAntiAlias:
617 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
618 break;
619 case SkFont::Edging::kSubpixelAntiAlias:
620 fFont.setEdging(SkFont::Edging::kAlias);
621 fFontOverrides.fEdging = false;
622 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500623 }
624 }
625 this->updateTitle();
626 fWindow->inval();
627 });
Ben Wagner9613e452019-01-23 10:34:59 -0500628 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500629 if (!fFontOverrides.fSubpixel) {
630 fFontOverrides.fSubpixel = true;
631 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500632 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500633 if (!fFont.isSubpixel()) {
634 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500635 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500636 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500637 }
638 }
639 this->updateTitle();
640 fWindow->inval();
641 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400642 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
643 if (!fFontOverrides.fBaselineSnap) {
644 fFontOverrides.fBaselineSnap = true;
645 fFont.setBaselineSnap(false);
646 } else {
647 if (!fFont.isBaselineSnap()) {
648 fFont.setBaselineSnap(true);
649 } else {
650 fFontOverrides.fBaselineSnap = false;
651 }
652 }
653 this->updateTitle();
654 fWindow->inval();
655 });
Brian Osman805a7272018-05-02 15:40:20 -0400656 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
657 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
658 : kPerspective_Real;
659 this->updateTitle();
660 fWindow->inval();
661 });
662 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
663 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
664 : kPerspective_Off;
665 this->updateTitle();
666 fWindow->inval();
667 });
Brian Osman207d4102019-01-10 09:40:58 -0500668 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
669 fAnimTimer.togglePauseResume();
670 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400671 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
672 fZoomUI = !fZoomUI;
673 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
674 fWindow->inval();
675 });
Mike Reed59295352020-03-12 13:56:34 -0400676 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
677 fDrawViaSerialize = !fDrawViaSerialize;
678 this->updateTitle();
679 fWindow->inval();
680 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500681 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400682 gUseSkVMBlitter = !gUseSkVMBlitter;
683 this->updateTitle();
684 fWindow->inval();
685 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500686 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
687 gSkVMAllowJIT = !gSkVMAllowJIT;
688 this->updateTitle();
689 fWindow->inval();
690 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500691
jvanverth2bb3b6d2016-04-08 07:24:09 -0700692 // set up slides
693 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500694 if (FLAGS_list) {
695 this->listNames();
696 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700697
Brian Osman9bb47cf2018-04-26 15:55:00 -0400698 fPerspectivePoints[0].set(0, 0);
699 fPerspectivePoints[1].set(1, 0);
700 fPerspectivePoints[2].set(0, 1);
701 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700702 fAnimTimer.run();
703
Hal Canaryc465d132017-12-08 10:21:31 -0500704 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500705 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500706 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500707 }
708 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500709
jongdeok.kim804f17e2019-02-26 14:39:23 +0900710 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500711 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700712}
713
jvanverth34524262016-05-04 13:49:13 -0700714void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400715 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
716 static const struct {
717 const char* fExtension;
718 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500719 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400720 const SlideFactory fFactory;
721 } gExternalSlidesInfo[] = {
722 { ".skp", "skp-dir", FLAGS_skps,
723 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
724 return sk_make_sp<SKPSlide>(name, path);}
725 },
726 { ".jpg", "jpg-dir", FLAGS_jpgs,
727 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
728 return sk_make_sp<ImageSlide>(name, path);}
729 },
Florin Malita87ccf332018-05-04 12:23:24 -0400730#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400731 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400732 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
733 return sk_make_sp<SkottieSlide>(name, path);}
734 },
Florin Malita87ccf332018-05-04 12:23:24 -0400735#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400736 #if defined(SK_ENABLE_SKRIVE)
737 { ".flr", "skrive-dir", FLAGS_rives,
738 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
739 return sk_make_sp<SkRiveSlide>(name, path);}
740 },
741 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400742#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400743 { ".svg", "svg-dir", FLAGS_svgs,
744 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
745 return sk_make_sp<SvgSlide>(name, path);}
746 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400747#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400748 };
jvanverthc265a922016-04-08 12:51:45 -0700749
Brian Salomon343553a2018-09-05 15:41:23 -0400750 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700751
Mike Klein88544fb2019-03-20 10:50:33 -0500752 const auto addSlide =
753 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
754 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
755 return;
756 }
liyuqian6f163d22016-06-13 12:26:45 -0700757
Mike Klein88544fb2019-03-20 10:50:33 -0500758 if (auto slide = fact(name, path)) {
759 dirSlides.push_back(slide);
760 fSlides.push_back(std::move(slide));
761 }
762 };
Florin Malita76a076b2018-02-15 18:40:48 -0500763
Florin Malita38792ce2018-05-08 10:36:18 -0400764 if (!FLAGS_file.isEmpty()) {
765 // single file mode
766 const SkString file(FLAGS_file[0]);
767
768 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
769 for (const auto& sinfo : gExternalSlidesInfo) {
770 if (file.endsWith(sinfo.fExtension)) {
771 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
772 return;
773 }
774 }
775
776 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
777 } else {
778 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
779 }
780
781 return;
782 }
783
784 // Bisect slide.
785 if (!FLAGS_bisect.isEmpty()) {
786 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500787 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400788 if (FLAGS_bisect.count() >= 2) {
789 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
790 bisect->onChar(*ch);
791 }
792 }
793 fSlides.push_back(std::move(bisect));
794 }
795 }
796
797 // GMs
798 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400799 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400800 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500801 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400802 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400803 fSlides.push_back(std::move(slide));
804 }
Florin Malita38792ce2018-05-08 10:36:18 -0400805 }
806 // reverse gms
807 int numGMs = fSlides.count() - firstGM;
808 for (int i = 0; i < numGMs/2; ++i) {
809 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
810 }
811
812 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400813 for (const SampleFactory factory : SampleRegistry::Range()) {
814 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500815 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400816 fSlides.push_back(slide);
817 }
Florin Malita38792ce2018-05-08 10:36:18 -0400818 }
819
Brian Osman7c979f52019-02-12 13:27:51 -0500820 // Particle demo
821 {
822 // TODO: Convert this to a sample
823 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500824 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500825 fSlides.push_back(std::move(slide));
826 }
827 }
828
Brian Osmand927bd22019-12-18 11:23:12 -0500829 // Runtime shader editor
830 {
831 sk_sp<Slide> slide(new SkSLSlide());
832 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
833 fSlides.push_back(std::move(slide));
834 }
835 }
836
Florin Malita0ffa3222018-04-05 14:34:45 -0400837 for (const auto& info : gExternalSlidesInfo) {
838 for (const auto& flag : info.fFlags) {
839 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
840 // single file
841 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
842 } else {
843 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400844 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400845 SkTArray<SkString> sortedFilenames;
846 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400847 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400848 sortedFilenames.push_back(name);
849 }
850 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400851 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000852 [](const SkString& a, const SkString& b) {
853 return strcmp(a.c_str(), b.c_str()) < 0;
854 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400855 }
856 for (const SkString& filename : sortedFilenames) {
857 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
858 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400859 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400860 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400861 if (!dirSlides.empty()) {
862 fSlides.push_back(
863 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
864 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500865 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400866 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400867 }
868 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500869
870 if (!fSlides.count()) {
871 sk_sp<Slide> slide(new NullSlide());
872 fSlides.push_back(std::move(slide));
873 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700874}
875
876
jvanverth34524262016-05-04 13:49:13 -0700877Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400878 for(auto& slide : fSlides) {
879 slide->gpuTeardown();
880 }
881
jvanverth9f372462016-04-06 06:08:59 -0700882 fWindow->detach();
883 delete fWindow;
884}
885
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500886struct SkPaintTitleUpdater {
887 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
888 void append(const char* s) {
889 if (fCount == 0) {
890 fTitle->append(" {");
891 } else {
892 fTitle->append(", ");
893 }
894 fTitle->append(s);
895 ++fCount;
896 }
897 void done() {
898 if (fCount > 0) {
899 fTitle->append("}");
900 }
901 }
902 SkString* fTitle;
903 int fCount;
904};
905
brianosman05de2162016-05-06 13:28:57 -0700906void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700907 if (!fWindow) {
908 return;
909 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500910 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700911 return; // Surface hasn't been created yet.
912 }
913
jvanverth34524262016-05-04 13:49:13 -0700914 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700915 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700916
Mike Kleine5acd752019-03-22 09:57:16 -0500917 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400918 if (gSkForceAnalyticAA) {
919 title.append(" <FAAA>");
920 } else {
921 title.append(" <AAA>");
922 }
923 }
Mike Reed59295352020-03-12 13:56:34 -0400924 if (fDrawViaSerialize) {
925 title.append(" <serialize>");
926 }
Mike Reed862818b2020-03-21 15:07:13 -0400927 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500928 title.append(" <SkVMBlitter>");
929 }
930 if (!gSkVMAllowJIT) {
931 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400932 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400933
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500934 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500935 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
936 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400937 const char* on, const char* off)
938 {
Ben Wagner9613e452019-01-23 10:34:59 -0500939 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400940 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500941 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400942 };
943
Ben Wagner9613e452019-01-23 10:34:59 -0500944 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
945 const char* on, const char* off)
946 {
947 if (fFontOverrides.*flag) {
948 paintTitle.append((fFont.*isFlag)() ? on : off);
949 }
950 };
951
952 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
953 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
954
955 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
956 "Force Autohint", "No Force Autohint");
957 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400958 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500959 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
960 "Linear Metrics", "Non-Linear Metrics");
961 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
962 "Bitmap Text", "No Bitmap Text");
963 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
964
965 if (fFontOverrides.fEdging) {
966 switch (fFont.getEdging()) {
967 case SkFont::Edging::kAlias:
968 paintTitle.append("Alias Text");
969 break;
970 case SkFont::Edging::kAntiAlias:
971 paintTitle.append("Antialias Text");
972 break;
973 case SkFont::Edging::kSubpixelAntiAlias:
974 paintTitle.append("Subpixel Antialias Text");
975 break;
976 }
977 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400978
Mike Reed3ae47332019-01-04 10:11:46 -0500979 if (fFontOverrides.fHinting) {
980 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400981 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500982 paintTitle.append("No Hinting");
983 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400984 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500985 paintTitle.append("Slight Hinting");
986 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400987 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500988 paintTitle.append("Normal Hinting");
989 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400990 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500991 paintTitle.append("Full Hinting");
992 break;
993 }
994 }
995 paintTitle.done();
996
Brian Osman92004802017-03-06 11:47:26 -0500997 switch (fColorMode) {
998 case ColorMode::kLegacy:
999 title.append(" Legacy 8888");
1000 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001001 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001002 title.append(" ColorManaged 8888");
1003 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001004 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001005 title.append(" ColorManaged F16");
1006 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001007 case ColorMode::kColorManagedF16Norm:
1008 title.append(" ColorManaged F16 Norm");
1009 break;
Brian Osman92004802017-03-06 11:47:26 -05001010 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001011
Brian Osman92004802017-03-06 11:47:26 -05001012 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001013 int curPrimaries = -1;
1014 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1015 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1016 curPrimaries = i;
1017 break;
1018 }
1019 }
Brian Osman03115dc2018-11-26 13:55:19 -05001020 title.appendf(" %s Gamma %f",
1021 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001022 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001023 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001024
Ben Wagner37c54032018-04-13 14:30:23 -04001025 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001026 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001027 switch (params.fSurfaceProps.pixelGeometry()) {
1028 case kUnknown_SkPixelGeometry:
1029 title.append( " Flat");
1030 break;
1031 case kRGB_H_SkPixelGeometry:
1032 title.append( " RGB");
1033 break;
1034 case kBGR_H_SkPixelGeometry:
1035 title.append( " BGR");
1036 break;
1037 case kRGB_V_SkPixelGeometry:
1038 title.append( " RGBV");
1039 break;
1040 case kBGR_V_SkPixelGeometry:
1041 title.append( " BGRV");
1042 break;
1043 }
1044 }
1045
1046 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1047 title.append(" DFT");
1048 }
1049
csmartdalton578f0642017-02-24 16:04:47 -07001050 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001051 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001052 int msaa = fWindow->sampleCount();
1053 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001054 title.appendf(" MSAA: %i", msaa);
1055 }
1056 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001057
1058 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001059 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001060 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1061 }
1062
Brian Osman805a7272018-05-02 15:40:20 -04001063 if (kPerspective_Real == fPerspectiveMode) {
1064 title.append(" Perpsective (Real)");
1065 } else if (kPerspective_Fake == fPerspectiveMode) {
1066 title.append(" Perspective (Fake)");
1067 }
1068
brianosman05de2162016-05-06 13:28:57 -07001069 fWindow->setTitle(title.c_str());
1070}
1071
Florin Malitaab99c342018-01-16 16:23:03 -05001072int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001073
1074 if (!FLAGS_slide.isEmpty()) {
1075 int count = fSlides.count();
1076 for (int i = 0; i < count; i++) {
1077 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001078 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001079 }
1080 }
1081
1082 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1083 this->listNames();
1084 }
1085
Florin Malitaab99c342018-01-16 16:23:03 -05001086 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001087}
1088
Florin Malitaab99c342018-01-16 16:23:03 -05001089void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001090 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001091 for (const auto& slide : fSlides) {
1092 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001093 }
1094}
1095
Florin Malitaab99c342018-01-16 16:23:03 -05001096void Viewer::setCurrentSlide(int slide) {
1097 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001098
Florin Malitaab99c342018-01-16 16:23:03 -05001099 if (slide == fCurrentSlide) {
1100 return;
1101 }
1102
1103 if (fCurrentSlide >= 0) {
1104 fSlides[fCurrentSlide]->unload();
1105 }
1106
1107 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1108 SkIntToScalar(fWindow->height()));
1109 fCurrentSlide = slide;
1110 this->setupCurrentSlide();
1111}
1112
1113void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001114 if (fCurrentSlide >= 0) {
1115 // prepare dimensions for image slides
1116 fGesture.resetTouchState();
1117 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001118
Jim Van Verth0848fb02018-01-22 13:39:30 -05001119 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1120 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1121 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001122
Jim Van Verth0848fb02018-01-22 13:39:30 -05001123 // Start with a matrix that scales the slide to the available screen space
1124 if (fWindow->scaleContentToFit()) {
1125 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001126 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1127 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001128 }
liyuqiane46e4f02016-05-20 07:32:19 -07001129 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001130
1131 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001132 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001133
1134 this->updateTitle();
1135 this->updateUIState();
1136
1137 fStatsLayer.resetMeasurements();
1138
1139 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001140 }
jvanverthc265a922016-04-08 12:51:45 -07001141}
1142
Brian Osmanaba642c2020-02-06 12:52:25 -05001143#define MAX_ZOOM_LEVEL 8.0f
1144#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001145
jvanverth34524262016-05-04 13:49:13 -07001146void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001147 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001148 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001149 this->preTouchMatrixChanged();
1150}
Yuqian Li755778c2018-03-28 16:23:31 -04001151
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001152void Viewer::preTouchMatrixChanged() {
1153 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001154 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1155 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1156 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1157 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1158}
1159
Brian Osman805a7272018-05-02 15:40:20 -04001160SkMatrix Viewer::computePerspectiveMatrix() {
1161 SkScalar w = fWindow->width(), h = fWindow->height();
1162 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1163 SkPoint perspPts[4] = {
1164 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1165 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1166 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1167 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1168 };
1169 SkMatrix m;
1170 m.setPolyToPoly(orthoPts, perspPts, 4);
1171 return m;
1172}
1173
Yuqian Li755778c2018-03-28 16:23:31 -04001174SkMatrix Viewer::computePreTouchMatrix() {
1175 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001176
1177 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001178 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001179 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001180
1181 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1182 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001183
Brian Osman805a7272018-05-02 15:40:20 -04001184 if (kPerspective_Real == fPerspectiveMode) {
1185 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001186 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001187 }
1188
Yuqian Li755778c2018-03-28 16:23:31 -04001189 return m;
jvanverthc265a922016-04-08 12:51:45 -07001190}
1191
liyuqiand3cdbca2016-05-17 12:44:20 -07001192SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001193 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001194 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001195 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001196 return m;
jvanverthc265a922016-04-08 12:51:45 -07001197}
1198
Brian Osman621491e2017-02-28 15:45:01 -05001199void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001200 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001201 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001202 fBackendType = backendType;
1203
Robert Phillipse9229532020-06-26 10:10:49 -04001204 // The active context is going away in 'detach'
1205 for(auto& slide : fSlides) {
1206 slide->gpuTeardown();
1207 }
1208
Brian Osman621491e2017-02-28 15:45:01 -05001209 fWindow->detach();
1210
Brian Osman70d2f432017-11-08 09:54:10 -05001211#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001212 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1213 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001214 DisplayParams params = fWindow->getRequestedDisplayParams();
1215 delete fWindow;
1216 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001217
Brian Osman70d2f432017-11-08 09:54:10 -05001218 // re-register callbacks
1219 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001220 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001221 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001222 fWindow->pushLayer(&fImGuiLayer);
1223
Brian Osman70d2f432017-11-08 09:54:10 -05001224 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1225 // will still include our correct sample count. But the re-created fWindow will lose that
1226 // information. On Windows, we need to re-create the window when changing sample count,
1227 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1228 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1229 // as if we had never changed windows at all).
1230 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001231#endif
1232
Brian Osman70d2f432017-11-08 09:54:10 -05001233 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001234}
1235
Brian Osman92004802017-03-06 11:47:26 -05001236void Viewer::setColorMode(ColorMode colorMode) {
1237 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001238 this->updateTitle();
1239 fWindow->inval();
1240}
1241
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001242class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1243public:
Mike Reed3ae47332019-01-04 10:11:46 -05001244 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1245 SkFont* font, Viewer::SkFontFields* ffields)
1246 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001247 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001248 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1249 sk_sp<SkTextBlob>* cache) {
1250 bool blobWillChange = false;
1251 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001252 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1253 bool shouldDraw = this->filterFont(&filteredFont);
1254 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001255 blobWillChange = true;
1256 break;
1257 }
1258 }
1259 if (!blobWillChange) {
1260 return blob;
1261 }
1262
1263 SkTextBlobBuilder builder;
1264 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001265 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1266 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001267 if (!shouldDraw) {
1268 continue;
1269 }
1270
Mike Reed3ae47332019-01-04 10:11:46 -05001271 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001272
Ben Wagner41e40472018-09-24 13:01:54 -04001273 const SkTextBlobBuilder::RunBuffer& runBuffer
1274 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001275 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001276 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001277 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001278 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001279 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001280 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001281 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001282 it.glyphCount(), it.textSize(), SkString())
1283 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1284 uint32_t glyphCount = it.glyphCount();
1285 if (it.glyphs()) {
1286 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1287 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1288 }
1289 if (it.pos()) {
1290 size_t posSize = sizeof(decltype(*it.pos()));
1291 uint8_t positioning = it.positioning();
1292 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1293 }
1294 if (it.text()) {
1295 size_t textSize = sizeof(decltype(*it.text()));
1296 uint32_t textCount = it.textSize();
1297 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1298 }
1299 if (it.clusters()) {
1300 size_t clusterSize = sizeof(decltype(*it.clusters()));
1301 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1302 }
1303 }
1304 *cache = builder.make();
1305 return cache->get();
1306 }
1307 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1308 const SkPaint& paint) override {
1309 sk_sp<SkTextBlob> cache;
1310 this->SkPaintFilterCanvas::onDrawTextBlob(
1311 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1312 }
Mike Reed3ae47332019-01-04 10:11:46 -05001313 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001314 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001315 font->writable()->setSize(fFont->getSize());
1316 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001317 if (fFontOverrides->fScaleX) {
1318 font->writable()->setScaleX(fFont->getScaleX());
1319 }
1320 if (fFontOverrides->fSkewX) {
1321 font->writable()->setSkewX(fFont->getSkewX());
1322 }
Mike Reed3ae47332019-01-04 10:11:46 -05001323 if (fFontOverrides->fHinting) {
1324 font->writable()->setHinting(fFont->getHinting());
1325 }
Ben Wagner9613e452019-01-23 10:34:59 -05001326 if (fFontOverrides->fEdging) {
1327 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001328 }
Ben Wagner9613e452019-01-23 10:34:59 -05001329 if (fFontOverrides->fEmbolden) {
1330 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001331 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001332 if (fFontOverrides->fBaselineSnap) {
1333 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1334 }
Ben Wagner9613e452019-01-23 10:34:59 -05001335 if (fFontOverrides->fLinearMetrics) {
1336 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001337 }
Ben Wagner9613e452019-01-23 10:34:59 -05001338 if (fFontOverrides->fSubpixel) {
1339 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001340 }
Ben Wagner9613e452019-01-23 10:34:59 -05001341 if (fFontOverrides->fEmbeddedBitmaps) {
1342 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001343 }
Ben Wagner9613e452019-01-23 10:34:59 -05001344 if (fFontOverrides->fForceAutoHinting) {
1345 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001346 }
Ben Wagner9613e452019-01-23 10:34:59 -05001347
Mike Reed3ae47332019-01-04 10:11:46 -05001348 return true;
1349 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001350 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001351 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001352 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001353 }
Ben Wagner9613e452019-01-23 10:34:59 -05001354 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001355 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001356 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001357 return true;
1358 }
1359 SkPaint* fPaint;
1360 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001361 SkFont* fFont;
1362 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001363};
1364
Robert Phillips9882dae2019-03-04 11:00:10 -05001365void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001366 if (fCurrentSlide < 0) {
1367 return;
1368 }
1369
Robert Phillips9882dae2019-03-04 11:00:10 -05001370 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001371
Brian Osmanf750fbc2017-02-08 10:47:28 -05001372 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001373 SkSurface* slideSurface = surface;
1374 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001375 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001376
Brian Osmane0d4fba2017-03-15 10:24:55 -04001377 // 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 -05001378 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001379 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001380 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001381 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001382 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001383 }
1384
Brian Osman3ac99cf2017-12-01 11:23:53 -05001385 if (fSaveToSKP) {
1386 SkPictureRecorder recorder;
1387 SkCanvas* recorderCanvas = recorder.beginRecording(
1388 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001389 fSlides[fCurrentSlide]->draw(recorderCanvas);
1390 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1391 SkFILEWStream stream("sample_app.skp");
1392 picture->serialize(&stream);
1393 fSaveToSKP = false;
1394 }
1395
Brian Osmane9ed0f02018-11-26 14:50:05 -05001396 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001397 SkColorType colorType;
1398 switch (fColorMode) {
1399 case ColorMode::kLegacy:
1400 case ColorMode::kColorManaged8888:
1401 colorType = kN32_SkColorType;
1402 break;
1403 case ColorMode::kColorManagedF16:
1404 colorType = kRGBA_F16_SkColorType;
1405 break;
1406 case ColorMode::kColorManagedF16Norm:
1407 colorType = kRGBA_F16Norm_SkColorType;
1408 break;
1409 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001410
1411 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001412 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001413 slideCanvas->getProps(&props);
1414
Brian Osmane9ed0f02018-11-26 14:50:05 -05001415 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1416 return Window::kRaster_BackendType == this->fBackendType
1417 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001418 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001419 };
1420
Brian Osman03115dc2018-11-26 13:55:19 -05001421 // We need to render offscreen if we're...
1422 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1423 // ... in any raster mode, because the window surface is actually GL
1424 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001425 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001426 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001427 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001428 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001429 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001430 colorSpace != nullptr ||
1431 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001432
Brian Osmane9ed0f02018-11-26 14:50:05 -05001433 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001434 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001435 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001436 }
1437
Mike Reed59295352020-03-12 13:56:34 -04001438 SkPictureRecorder recorder;
1439 SkCanvas* recorderRestoreCanvas = nullptr;
1440 if (fDrawViaSerialize) {
1441 recorderRestoreCanvas = slideCanvas;
1442 slideCanvas = recorder.beginRecording(
1443 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1444 }
1445
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001446 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001447 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001448 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001449 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001450 if (fTiled) {
1451 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1452 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001453 for (int y = 0; y < fWindow->height(); y += tileH) {
1454 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001455 SkAutoCanvasRestore acr(slideCanvas, true);
1456 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1457 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001458 }
1459 }
1460
1461 // Draw borders between tiles
1462 if (fDrawTileBoundaries) {
1463 SkPaint border;
1464 border.setColor(0x60FF00FF);
1465 border.setStyle(SkPaint::kStroke_Style);
1466 for (int y = 0; y < fWindow->height(); y += tileH) {
1467 for (int x = 0; x < fWindow->width(); x += tileW) {
1468 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1469 }
1470 }
1471 }
1472 } else {
1473 slideCanvas->concat(this->computeMatrix());
1474 if (kPerspective_Real == fPerspectiveMode) {
1475 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1476 }
Mike Reed3ae47332019-01-04 10:11:46 -05001477 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001478 fSlides[fCurrentSlide]->draw(&filterCanvas);
1479 }
Brian Osman56a24812017-12-19 11:15:16 -05001480 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001481 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001482
Mike Reed59295352020-03-12 13:56:34 -04001483 if (recorderRestoreCanvas) {
1484 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1485 auto data = picture->serialize();
1486 slideCanvas = recorderRestoreCanvas;
1487 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1488 }
1489
Brian Osman1df161a2017-02-09 12:10:20 -05001490 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001491 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001492 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001493 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001494
1495 // If we rendered offscreen, snap an image and push the results to the window's canvas
1496 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001497 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001498
Robert Phillips9882dae2019-03-04 11:00:10 -05001499 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001500 SkPaint paint;
1501 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001502 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001503 int prePerspectiveCount = canvas->save();
1504 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001505 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001506 canvas->clear(SK_ColorWHITE);
1507 canvas->concat(this->computePerspectiveMatrix());
1508 }
Mike Reedb339d052021-01-28 11:20:41 -05001509 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001510 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001511 }
Mike Reed376d8122019-03-14 11:39:02 -04001512
1513 if (fShowSlideDimensions) {
1514 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1515 SkPaint paint;
1516 paint.setColor(0x40FFFF00);
1517 surface->getCanvas()->drawRect(r, paint);
1518 }
liyuqian6f163d22016-06-13 12:26:45 -07001519}
1520
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001521void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001522 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001523 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001524}
Jim Van Verth6f449692017-02-14 15:16:46 -05001525
Robert Phillips9882dae2019-03-04 11:00:10 -05001526void Viewer::onPaint(SkSurface* surface) {
1527 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001528
Robert Phillips9882dae2019-03-04 11:00:10 -05001529 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001530
Brian Osmand67e5182017-12-08 16:46:09 -05001531 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001532
Greg Daniel427d8eb2020-09-28 15:04:18 -04001533 fLastImage.reset();
1534
Robert Phillipsed653392020-07-10 13:55:21 -04001535 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001536 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001537 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001538 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001539}
1540
Ben Wagnera1915972018-08-09 15:06:19 -04001541void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001542 if (fCurrentSlide >= 0) {
1543 fSlides[fCurrentSlide]->resize(width, height);
1544 }
Ben Wagnera1915972018-08-09 15:06:19 -04001545}
1546
Florin Malitacefc1b92018-02-19 21:43:47 -05001547SkPoint Viewer::mapEvent(float x, float y) {
1548 const auto m = this->computeMatrix();
1549 SkMatrix inv;
1550
1551 SkAssertResult(m.invert(&inv));
1552
1553 return inv.mapXY(x, y);
1554}
1555
Hal Canaryb1f411a2019-08-29 10:39:22 -04001556bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001557 if (GestureDevice::kMouse == fGestureDevice) {
1558 return false;
1559 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001560
1561 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001562 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001563 fWindow->inval();
1564 return true;
1565 }
1566
liyuqiand3cdbca2016-05-17 12:44:20 -07001567 void* castedOwner = reinterpret_cast<void*>(owner);
1568 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001569 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001570 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001571#if defined(SK_BUILD_FOR_IOS)
1572 // TODO: move IOS swipe detection higher up into the platform code
1573 SkPoint dir;
1574 if (fGesture.isFling(&dir)) {
1575 // swiping left or right
1576 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1577 if (dir.fX < 0) {
1578 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1579 fCurrentSlide + 1 : 0);
1580 } else {
1581 this->setCurrentSlide(fCurrentSlide > 0 ?
1582 fCurrentSlide - 1 : fSlides.count() - 1);
1583 }
1584 }
1585 fGesture.reset();
1586 }
1587#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001588 break;
1589 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001590 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001591 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001592 break;
1593 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001594 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001595 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001596 break;
1597 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001598 default: {
1599 // kLeft and kRight are only for swipes
1600 SkASSERT(false);
1601 break;
1602 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001603 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001604 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001605 fWindow->inval();
1606 return true;
1607}
1608
Hal Canaryb1f411a2019-08-29 10:39:22 -04001609bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001610 if (GestureDevice::kTouch == fGestureDevice) {
1611 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001612 }
Brian Osman16c81a12017-12-20 11:58:34 -05001613
Florin Malitacefc1b92018-02-19 21:43:47 -05001614 const auto slidePt = this->mapEvent(x, y);
1615 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1616 fWindow->inval();
1617 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001618 }
1619
1620 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001621 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001622 fGesture.touchEnd(nullptr);
1623 break;
1624 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001625 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001626 fGesture.touchBegin(nullptr, x, y);
1627 break;
1628 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001629 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001630 fGesture.touchMoved(nullptr, x, y);
1631 break;
1632 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001633 default: {
1634 SkASSERT(false); // shouldn't see kRight or kLeft here
1635 break;
1636 }
Brian Osman16c81a12017-12-20 11:58:34 -05001637 }
1638 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1639
Hal Canaryb1f411a2019-08-29 10:39:22 -04001640 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001641 fWindow->inval();
1642 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001643 return true;
1644}
1645
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001646bool Viewer::onFling(skui::InputState state) {
1647 if (skui::InputState::kRight == state) {
1648 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1649 return true;
1650 } else if (skui::InputState::kLeft == state) {
1651 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1652 return true;
1653 }
1654 return false;
1655}
1656
1657bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1658 switch (state) {
1659 case skui::InputState::kDown:
1660 fGesture.startZoom();
1661 return true;
1662 break;
1663 case skui::InputState::kMove:
1664 fGesture.updateZoom(scale, x, y, x, y);
1665 return true;
1666 break;
1667 case skui::InputState::kUp:
1668 fGesture.endZoom();
1669 return true;
1670 break;
1671 default:
1672 SkASSERT(false);
1673 break;
1674 }
1675
1676 return false;
1677}
1678
Brian Osmana109e392017-02-24 09:49:14 -05001679static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001680 // The gamut image covers a (0.8 x 0.9) shaped region
1681 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001682
1683 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1684 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1685 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001686 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1687 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1688 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001689
Brian Osman535c5e32019-02-09 16:32:58 -05001690 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1691 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1692 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1693 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1694 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001695}
1696
Ben Wagner3627d2e2018-06-26 14:23:20 -04001697static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001698 ImGui::DragCanvas dc(pt);
1699 dc.fillColor(IM_COL32(0, 0, 0, 128));
1700 dc.dragPoint(pt);
1701 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001702}
1703
Brian Osman9bb47cf2018-04-26 15:55:00 -04001704static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001705 ImGui::DragCanvas dc(pts);
1706 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001707
Brian Osman535c5e32019-02-09 16:32:58 -05001708 for (int i = 0; i < 4; ++i) {
1709 dc.dragPoint(pts + i);
1710 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001711
Brian Osman535c5e32019-02-09 16:32:58 -05001712 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1713 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1714 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1715 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001716
Brian Osman535c5e32019-02-09 16:32:58 -05001717 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001718}
1719
John Stiles38b7d2f2020-06-24 12:13:31 -04001720static SkSL::String build_sksl_highlight_shader() {
1721 return SkSL::String("out half4 sk_FragColor;\n"
1722 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1723}
1724
1725static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1726 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1727 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1728 size_t pos = inShader.rfind("return *_out;\n");
1729 if (pos == std::string::npos) {
1730 return inShader;
1731 }
1732
1733 SkSL::String replacementShader = inShader;
1734 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1735 return replacementShader;
1736}
1737
1738static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1739 const char* versionDecl = shaderCaps.versionDeclString();
1740 SkSL::String highlight = versionDecl ? versionDecl : "";
1741 if (shaderCaps.usesPrecisionModifiers()) {
1742 highlight.append("precision mediump float;\n");
1743 }
1744 highlight.appendf("out vec4 sk_FragColor;\n"
1745 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1746 return highlight;
1747}
1748
Brian Osmand67e5182017-12-08 16:46:09 -05001749void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001750 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1751 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001752 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001753 }
1754
1755 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001756 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1757 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001758 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001759 DisplayParams params = fWindow->getRequestedDisplayParams();
1760 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001761 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001762
Brian Osmana109e392017-02-24 09:49:14 -05001763 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1764 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001765 if (ImGui::CollapsingHeader("Backend")) {
1766 int newBackend = static_cast<int>(fBackendType);
1767 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1768 ImGui::SameLine();
1769 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001770#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1771 ImGui::SameLine();
1772 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1773#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001774#if defined(SK_DAWN)
1775 ImGui::SameLine();
1776 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1777#endif
John Stilesf7da9232020-11-19 19:58:14 -05001778#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001779 ImGui::SameLine();
1780 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1781#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001782#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001783 ImGui::SameLine();
1784 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1785#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001786#if defined(SK_DIRECT3D)
1787 ImGui::SameLine();
1788 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1789#endif
Brian Osman621491e2017-02-28 15:45:01 -05001790 if (newBackend != fBackendType) {
1791 fDeferredActions.push_back([=]() {
1792 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1793 });
1794 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001795
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001796 bool* wire = &params.fGrContextOptions.fWireframeMode;
1797 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1798 paramsChanged = true;
1799 }
Brian Salomon99a33902017-03-07 15:16:34 -05001800
Brian Osman28b12522017-03-08 17:10:24 -05001801 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001802 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001803 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001804 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1805 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1806 1;
1807
1808 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1809 if (maxMSAA >= 4) {
1810 ImGui::Text("MSAA: ");
1811
1812 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1813 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1814 // don't show it in the list.
1815 if (curMSAA == 2) {
1816 continue;
1817 }
1818 ImGui::SameLine();
1819 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1820 &sampleCount, curMSAA);
1821 }
1822 }
Brian Osman28b12522017-03-08 17:10:24 -05001823
1824 if (sampleCount != params.fMSAASampleCount) {
1825 params.fMSAASampleCount = sampleCount;
1826 paramsChanged = true;
1827 }
1828 }
1829
Ben Wagner37c54032018-04-13 14:30:23 -04001830 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001831 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001832 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1833 }
1834 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1835 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1836 {
1837 uint32_t flags = params.fSurfaceProps.flags();
1838 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001839 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1840 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1841 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001842 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001843 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001844 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1845 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1846 }
1847 paramsChanged = true;
1848 }
1849
1850 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1851 if (ImGui::Checkbox("DFT", &useDFT)) {
1852 uint32_t flags = params.fSurfaceProps.flags();
1853 if (useDFT) {
1854 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1855 } else {
1856 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1857 }
1858 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1859 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1860 paramsChanged = true;
1861 }
1862
Brian Osman8a9de3d2017-03-01 14:59:05 -05001863 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001864 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001865 auto prButton = [&](GpuPathRenderers x) {
1866 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001867 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1868 params.fGrContextOptions.fGpuPathRenderers = x;
1869 paramsChanged = true;
1870 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001871 }
1872 };
1873
1874 if (!ctx) {
1875 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001876 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001877 const auto* caps = ctx->priv().caps();
1878 prButton(GpuPathRenderers::kDefault);
1879 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001880 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001881 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001882 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001883 if (caps->shaderCaps()->pathRenderingSupport()) {
1884 prButton(GpuPathRenderers::kStencilAndCover);
1885 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001886 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001887 if (1 == fWindow->sampleCount()) {
1888 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1889 prButton(GpuPathRenderers::kCoverageCounting);
1890 }
1891 prButton(GpuPathRenderers::kSmall);
1892 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001893 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001894 prButton(GpuPathRenderers::kNone);
1895 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001896 ImGui::TreePop();
1897 }
Brian Osman621491e2017-02-28 15:45:01 -05001898 }
1899
Ben Wagner964571d2019-03-08 12:35:06 -05001900 if (ImGui::CollapsingHeader("Tiling")) {
1901 ImGui::Checkbox("Enable", &fTiled);
1902 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1903 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1904 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1905 }
1906
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001907 if (ImGui::CollapsingHeader("Transform")) {
1908 float zoom = fZoomLevel;
1909 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1910 fZoomLevel = zoom;
1911 this->preTouchMatrixChanged();
1912 paramsChanged = true;
1913 }
1914 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001915 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001916 fRotation = deg;
1917 this->preTouchMatrixChanged();
1918 paramsChanged = true;
1919 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001920 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1921 if (ImGui_DragLocation(&fOffset)) {
1922 this->preTouchMatrixChanged();
1923 paramsChanged = true;
1924 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001925 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1926 this->preTouchMatrixChanged();
1927 paramsChanged = true;
1928 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001929 }
Brian Osman805a7272018-05-02 15:40:20 -04001930 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1931 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1932 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001933 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001934 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001935 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001936 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001937 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001938 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001939 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001940 }
1941
Ben Wagnera580fb32018-04-17 11:16:32 -04001942 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001943 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001944 if (fPaintOverrides.fAntiAlias) {
1945 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001946 }
1947 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001948 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001949 {
1950 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1951 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001952 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001953 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1954 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001955 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001956 fPaintOverrides.fAntiAlias = true;
1957 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001958 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001959 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001960 case SkPaintFields::AntiAliasState::Alias:
1961 break;
1962 case SkPaintFields::AntiAliasState::Normal:
1963 break;
1964 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1965 gSkUseAnalyticAA = true;
1966 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001967 break;
1968 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1969 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001970 break;
1971 }
1972 }
1973 paramsChanged = true;
1974 }
1975
Ben Wagner99a78dc2018-05-09 18:23:51 -04001976 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001977 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001978 bool (SkPaint::* isFlag)() const,
1979 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001980 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001981 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001982 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001983 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001984 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001985 if (ImGui::Combo(label, &itemIndex, items)) {
1986 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001987 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001988 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001989 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001990 (fPaint.*setFlag)(itemIndex == 2);
1991 }
1992 paramsChanged = true;
1993 }
1994 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001995
Ben Wagner99a78dc2018-05-09 18:23:51 -04001996 paintFlag("Dither",
1997 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001998 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001999 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagner9613e452019-01-23 10:34:59 -05002000 }
Hal Canary02738a82019-01-21 18:51:32 +00002001
Ben Wagner9613e452019-01-23 10:34:59 -05002002 if (ImGui::CollapsingHeader("Font")) {
2003 int hintingIdx = 0;
2004 if (fFontOverrides.fHinting) {
2005 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2006 }
2007 if (ImGui::Combo("Hinting", &hintingIdx,
2008 "Default\0None\0Slight\0Normal\0Full\0\0"))
2009 {
2010 if (hintingIdx == 0) {
2011 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002012 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002013 } else {
2014 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2015 fFontOverrides.fHinting = true;
2016 }
2017 paramsChanged = true;
2018 }
Hal Canary02738a82019-01-21 18:51:32 +00002019
Ben Wagner9613e452019-01-23 10:34:59 -05002020 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2021 bool SkFontFields::* flag,
2022 bool (SkFont::* isFlag)() const,
2023 void (SkFont::* setFlag)(bool) )
2024 {
2025 int itemIndex = 0;
2026 if (fFontOverrides.*flag) {
2027 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2028 }
2029 if (ImGui::Combo(label, &itemIndex, items)) {
2030 if (itemIndex == 0) {
2031 fFontOverrides.*flag = false;
2032 } else {
2033 fFontOverrides.*flag = true;
2034 (fFont.*setFlag)(itemIndex == 2);
2035 }
2036 paramsChanged = true;
2037 }
2038 };
Hal Canary02738a82019-01-21 18:51:32 +00002039
Ben Wagner9613e452019-01-23 10:34:59 -05002040 fontFlag("Fake Bold Glyphs",
2041 "Default\0No Fake Bold\0Fake Bold\0\0",
2042 &SkFontFields::fEmbolden,
2043 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002044
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002045 fontFlag("Baseline Snapping",
2046 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2047 &SkFontFields::fBaselineSnap,
2048 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2049
Ben Wagner9613e452019-01-23 10:34:59 -05002050 fontFlag("Linear Text",
2051 "Default\0No Linear Text\0Linear Text\0\0",
2052 &SkFontFields::fLinearMetrics,
2053 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002054
Ben Wagner9613e452019-01-23 10:34:59 -05002055 fontFlag("Subpixel Position Glyphs",
2056 "Default\0Pixel Text\0Subpixel Text\0\0",
2057 &SkFontFields::fSubpixel,
2058 &SkFont::isSubpixel, &SkFont::setSubpixel);
2059
2060 fontFlag("Embedded Bitmap Text",
2061 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2062 &SkFontFields::fEmbeddedBitmaps,
2063 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2064
2065 fontFlag("Force Auto-Hinting",
2066 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2067 &SkFontFields::fForceAutoHinting,
2068 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2069
2070 int edgingIdx = 0;
2071 if (fFontOverrides.fEdging) {
2072 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2073 }
2074 if (ImGui::Combo("Edging", &edgingIdx,
2075 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2076 {
2077 if (edgingIdx == 0) {
2078 fFontOverrides.fEdging = false;
2079 fFont.setEdging(SkFont::Edging::kAlias);
2080 } else {
2081 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2082 fFontOverrides.fEdging = true;
2083 }
2084 paramsChanged = true;
2085 }
2086
Ben Wagner15a8d572019-03-21 13:35:44 -04002087 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2088 if (fFontOverrides.fSize) {
2089 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002090 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002091 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002092 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002093 fFontOverrides.fSizeRange[0],
2094 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002095 "%.6f", 2.0f))
2096 {
Mike Reed3ae47332019-01-04 10:11:46 -05002097 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002098 paramsChanged = true;
2099 }
2100 }
2101
2102 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2103 if (fFontOverrides.fScaleX) {
2104 float scaleX = fFont.getScaleX();
2105 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2106 fFont.setScaleX(scaleX);
2107 paramsChanged = true;
2108 }
2109 }
2110
2111 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2112 if (fFontOverrides.fSkewX) {
2113 float skewX = fFont.getSkewX();
2114 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2115 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002116 paramsChanged = true;
2117 }
2118 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002119 }
2120
Mike Reed81f60ec2018-05-15 10:09:52 -04002121 {
2122 SkMetaData controls;
2123 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2124 if (ImGui::CollapsingHeader("Current Slide")) {
2125 SkMetaData::Iter iter(controls);
2126 const char* name;
2127 SkMetaData::Type type;
2128 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002129 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002130 if (type == SkMetaData::kScalar_Type) {
2131 float val[3];
2132 SkASSERT(count == 3);
2133 controls.findScalars(name, &count, val);
2134 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2135 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002136 }
Ben Wagner110c7032019-03-22 17:03:59 -04002137 } else if (type == SkMetaData::kBool_Type) {
2138 bool val;
2139 SkASSERT(count == 1);
2140 controls.findBool(name, &val);
2141 if (ImGui::Checkbox(name, &val)) {
2142 controls.setBool(name, val);
2143 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002144 }
2145 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002146 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002147 }
2148 }
2149 }
2150
Ben Wagner7a3c6742018-04-23 10:01:07 -04002151 if (fShowSlidePicker) {
2152 ImGui::SetNextTreeNodeOpen(true);
2153 }
Brian Osman79086b92017-02-10 13:36:16 -05002154 if (ImGui::CollapsingHeader("Slide")) {
2155 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002156 static ImVector<const char*> filteredSlideNames;
2157 static ImVector<int> filteredSlideIndices;
2158
Brian Osmanfce09c52017-11-14 15:32:20 -05002159 if (fShowSlidePicker) {
2160 ImGui::SetKeyboardFocusHere();
2161 fShowSlidePicker = false;
2162 }
2163
Brian Osman79086b92017-02-10 13:36:16 -05002164 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002165 filteredSlideNames.clear();
2166 filteredSlideIndices.clear();
2167 int filteredIndex = 0;
2168 for (int i = 0; i < fSlides.count(); ++i) {
2169 const char* slideName = fSlides[i]->getName().c_str();
2170 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2171 if (i == fCurrentSlide) {
2172 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002173 }
Brian Osmanf479e422017-11-08 13:11:36 -05002174 filteredSlideNames.push_back(slideName);
2175 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002176 }
Brian Osman79086b92017-02-10 13:36:16 -05002177 }
Brian Osmanf479e422017-11-08 13:11:36 -05002178
Brian Osmanf479e422017-11-08 13:11:36 -05002179 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2180 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002181 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002182 }
2183 }
Brian Osmana109e392017-02-24 09:49:14 -05002184
2185 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002186 ColorMode newMode = fColorMode;
2187 auto cmButton = [&](ColorMode mode, const char* label) {
2188 if (ImGui::RadioButton(label, mode == fColorMode)) {
2189 newMode = mode;
2190 }
2191 };
2192
2193 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002194 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2195 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002196 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002197
2198 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002199 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002200 }
2201
2202 // Pick from common gamuts:
2203 int primariesIdx = 4; // Default: Custom
2204 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2205 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2206 primariesIdx = i;
2207 break;
2208 }
2209 }
2210
Brian Osman03115dc2018-11-26 13:55:19 -05002211 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002212 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002213
Brian Osmana109e392017-02-24 09:49:14 -05002214 if (ImGui::Combo("Primaries", &primariesIdx,
2215 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2216 if (primariesIdx >= 0 && primariesIdx <= 3) {
2217 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2218 }
2219 }
2220
2221 // Allow direct editing of gamut
2222 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2223 }
Brian Osman207d4102019-01-10 09:40:58 -05002224
2225 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002226 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002227 if (ImGui::Checkbox("Pause", &isPaused)) {
2228 fAnimTimer.togglePauseResume();
2229 }
Brian Osman707d2022019-01-10 11:27:34 -05002230
2231 float speed = fAnimTimer.getSpeed();
2232 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2233 fAnimTimer.setSpeed(speed);
2234 }
Brian Osman207d4102019-01-10 09:40:58 -05002235 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002236
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002237 if (ImGui::CollapsingHeader("Shaders")) {
2238 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2239 GrContextOptions::ShaderCacheStrategy::kSkSL;
2240#if defined(SK_VULKAN)
2241 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2242#else
2243 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002244#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002245
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002246 // To re-load shaders from the currently active programs, we flush all
2247 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002248 static bool gLoadPending = false;
2249 if (gLoadPending) {
2250 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2251 int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002252 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002253 entry.fKey = key;
2254 SkMD5 hash;
2255 hash.write(key->bytes(), key->size());
2256 SkMD5::Digest digest = hash.finish();
2257 for (int i = 0; i < 16; ++i) {
2258 entry.fKeyString.appendf("%02x", digest.data[i]);
2259 }
2260
Brian Osman9e4e4c72020-06-10 07:19:34 -04002261 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002262 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002263 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2264 entry.fInputs,
2265 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002266 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002267 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002268 fPersistentCache.foreach(collectShaders);
2269 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002270
2271#if defined(SK_VULKAN)
2272 if (isVulkan && !sksl) {
2273 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2274 for (auto& entry : fCachedShaders) {
2275 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2276 const SkSL::String& spirv(entry.fShader[i]);
2277 std::string disasm;
2278 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2279 &disasm);
2280 entry.fShader[i].assign(disasm);
2281 }
2282 }
2283 }
2284#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002285 }
2286
2287 // Defer actually doing the load/save logic so that we can trigger a save when we
2288 // start or finish hovering on a tree node in the list below:
2289 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002290 bool doSave = ImGui::Button("Save"); ImGui::SameLine();
2291 if (ImGui::Checkbox("SkSL", &sksl)) {
2292 params.fGrContextOptions.fShaderCacheStrategy =
2293 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2294 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2295 paramsChanged = true;
2296 doLoad = true;
2297 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
Brian Osmancbc33b82019-04-19 14:16:19 -04002298 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002299
2300 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002301 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002302 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2303 bool hovered = ImGui::IsItemHovered();
2304 if (hovered != entry.fHovered) {
2305 // Force a save to patch the highlight shader in/out
2306 entry.fHovered = hovered;
2307 doSave = true;
2308 }
2309 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002310 auto stringBox = [](const char* label, std::string* str) {
2311 // Full width, and not too much space for each shader
2312 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2313 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2314 ImGui::InputTextMultiline(label, str, boxSize);
2315 };
2316 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2317 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002318 ImGui::TreePop();
2319 }
2320 }
2321 ImGui::EndChild();
2322
2323 if (doLoad) {
2324 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002325 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002326 gLoadPending = true;
2327 }
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002328 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2329 // but I'm not sure anyone wants to do that.
2330 if (isVulkan && !sksl) {
2331 doSave = false;
2332 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002333 if (doSave) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002334 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002335 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002336 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002337 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002338 if (entry.fHovered) {
2339 // The hovered item (if any) gets a special shader to make it
2340 // identifiable.
2341 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2342 switch (entry.fShaderType) {
2343 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2344 fragShader = build_sksl_highlight_shader();
2345 break;
2346 }
2347 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2348 fragShader = build_glsl_highlight_shader(
2349 *ctx->priv().caps()->shaderCaps());
2350 break;
2351 }
2352 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2353 fragShader = build_metal_highlight_shader(fragShader);
2354 break;
2355 }
2356 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002357 }
2358
Brian Osmana085a412019-04-25 09:44:43 -04002359 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2360 entry.fShader,
2361 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002362 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002363 fPersistentCache.store(*entry.fKey, *data);
2364
2365 entry.fShader[kFragment_GrShaderType] = backup;
2366 }
2367 }
2368 }
Brian Osman79086b92017-02-10 13:36:16 -05002369 }
Brian Salomon99a33902017-03-07 15:16:34 -05002370 if (paramsChanged) {
2371 fDeferredActions.push_back([=]() {
2372 fWindow->setRequestedDisplayParams(params);
2373 fWindow->inval();
2374 this->updateTitle();
2375 });
2376 }
Brian Osman79086b92017-02-10 13:36:16 -05002377 ImGui::End();
2378 }
2379
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002380 if (gShaderErrorHandler.fErrors.count()) {
2381 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002382 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002383 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2384 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002385 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2386 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2387 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2388 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002389 }
2390 ImGui::End();
2391 gShaderErrorHandler.reset();
2392 }
2393
Brian Osmanf6877092017-02-13 09:39:57 -05002394 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002395 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2396 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002397 static int zoomFactor = 8;
2398 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002399 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002400 }
2401 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2402 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002403 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002404 }
Brian Osmanf6877092017-02-13 09:39:57 -05002405
Ben Wagner3627d2e2018-06-26 14:23:20 -04002406 if (!fZoomWindowFixed) {
2407 ImVec2 mousePos = ImGui::GetMousePos();
2408 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2409 }
2410 SkScalar x = fZoomWindowLocation.x();
2411 SkScalar y = fZoomWindowLocation.y();
2412 int xInt = SkScalarRoundToInt(x);
2413 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002414 ImVec2 avail = ImGui::GetContentRegionAvail();
2415
Brian Osmanead517d2017-11-13 15:36:36 -05002416 uint32_t pixel = 0;
2417 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002418 auto dContext = fWindow->directContext();
2419 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002420 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002421 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002422 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002423 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002424 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2425 }
2426
Greg Danielfbc60b72020-11-03 17:27:02 -05002427 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002428 // Translate so the region of the image that's under the mouse cursor is centered
2429 // in the zoom canvas:
2430 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002431 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2432 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002433 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002434
2435 SkPaint outline;
2436 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002437 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002438 });
Brian Osmanf6877092017-02-13 09:39:57 -05002439 }
2440
2441 ImGui::End();
2442 }
Brian Osman79086b92017-02-10 13:36:16 -05002443}
2444
liyuqian2edb0f42016-07-06 14:11:32 -07002445void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002446 for (int i = 0; i < fDeferredActions.count(); ++i) {
2447 fDeferredActions[i]();
2448 }
2449 fDeferredActions.reset();
2450
Brian Osman56a24812017-12-19 11:15:16 -05002451 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002452 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002453 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002454 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002455
Brian Osman79086b92017-02-10 13:36:16 -05002456 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002457 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2458 // not be visible, though. So we need to redraw if there is at least one visible window, or
2459 // more than one active window. Newly created windows are active but not visible for one frame
2460 // while they determine their layout and sizing.
2461 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2462 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002463 fWindow->inval();
2464 }
jvanverth9f372462016-04-06 06:08:59 -07002465}
liyuqiane5a6cd92016-05-27 08:52:52 -07002466
Florin Malitab632df72018-06-18 21:23:06 -04002467template <typename OptionsFunc>
2468static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2469 OptionsFunc&& optionsFunc) {
2470 writer.beginObject();
2471 {
2472 writer.appendString(kName , name);
2473 writer.appendString(kValue, value);
2474
2475 writer.beginArray(kOptions);
2476 {
2477 optionsFunc(writer);
2478 }
2479 writer.endArray();
2480 }
2481 writer.endObject();
2482}
2483
2484
liyuqiane5a6cd92016-05-27 08:52:52 -07002485void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002486 if (!fWindow) {
2487 return;
2488 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002489 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002490 return; // Surface hasn't been created yet.
2491 }
2492
Florin Malitab632df72018-06-18 21:23:06 -04002493 SkDynamicMemoryWStream memStream;
2494 SkJSONWriter writer(&memStream);
2495 writer.beginArray();
2496
liyuqianb73c24b2016-06-03 08:47:23 -07002497 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002498 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2499 [this](SkJSONWriter& writer) {
2500 for(const auto& slide : fSlides) {
2501 writer.appendString(slide->getName().c_str());
2502 }
2503 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002504
liyuqianb73c24b2016-06-03 08:47:23 -07002505 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002506 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2507 [](SkJSONWriter& writer) {
2508 for (const auto& str : kBackendTypeStrings) {
2509 writer.appendString(str);
2510 }
2511 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002512
csmartdalton578f0642017-02-24 16:04:47 -07002513 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002514 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2515 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2516 [this](SkJSONWriter& writer) {
2517 writer.appendS32(0);
2518
2519 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2520 return;
2521 }
2522
2523 for (int msaa : {4, 8, 16}) {
2524 writer.appendS32(msaa);
2525 }
2526 });
csmartdalton578f0642017-02-24 16:04:47 -07002527
csmartdalton61cd31a2017-02-27 17:00:53 -07002528 // Path renderer state
2529 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002530 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2531 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002532 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002533 if (!ctx) {
2534 writer.appendString("Software");
2535 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002536 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002537 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2538 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002539 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002540 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002541 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002542 }
Florin Malitab632df72018-06-18 21:23:06 -04002543 if (caps->shaderCaps()->pathRenderingSupport()) {
2544 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002545 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002546 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002547 }
2548 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002549 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2550 writer.appendString(
2551 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2552 }
2553 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2554 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002555 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002556 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002557 }
2558 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002559
liyuqianb73c24b2016-06-03 08:47:23 -07002560 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002561 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2562 [this](SkJSONWriter& writer) {
2563 writer.appendString(kSoftkeyHint);
2564 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2565 writer.appendString(softkey.c_str());
2566 }
2567 });
liyuqianb73c24b2016-06-03 08:47:23 -07002568
Florin Malitab632df72018-06-18 21:23:06 -04002569 writer.endArray();
2570 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002571
Florin Malitab632df72018-06-18 21:23:06 -04002572 auto data = memStream.detachAsData();
2573
2574 // TODO: would be cool to avoid this copy
2575 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2576
2577 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002578}
2579
2580void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002581 // For those who will add more features to handle the state change in this function:
2582 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2583 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2584 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002585 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002586 for (int i = 0; i < fSlides.count(); ++i) {
2587 if (fSlides[i]->getName().equals(stateValue)) {
2588 this->setCurrentSlide(i);
2589 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002590 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002591 }
Florin Malitaab99c342018-01-16 16:23:03 -05002592
2593 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002594 } else if (stateName.equals(kBackendStateName)) {
2595 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2596 if (stateValue.equals(kBackendTypeStrings[i])) {
2597 if (fBackendType != i) {
2598 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002599 for(auto& slide : fSlides) {
2600 slide->gpuTeardown();
2601 }
liyuqian6cb70252016-06-02 12:16:25 -07002602 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002603 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002604 }
2605 break;
2606 }
2607 }
csmartdalton578f0642017-02-24 16:04:47 -07002608 } else if (stateName.equals(kMSAAStateName)) {
2609 DisplayParams params = fWindow->getRequestedDisplayParams();
2610 int sampleCount = atoi(stateValue.c_str());
2611 if (sampleCount != params.fMSAASampleCount) {
2612 params.fMSAASampleCount = sampleCount;
2613 fWindow->setRequestedDisplayParams(params);
2614 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002615 this->updateTitle();
2616 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002617 }
2618 } else if (stateName.equals(kPathRendererStateName)) {
2619 DisplayParams params = fWindow->getRequestedDisplayParams();
2620 for (const auto& pair : gPathRendererNames) {
2621 if (pair.second == stateValue.c_str()) {
2622 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2623 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2624 fWindow->setRequestedDisplayParams(params);
2625 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002626 this->updateTitle();
2627 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002628 }
2629 break;
2630 }
csmartdalton578f0642017-02-24 16:04:47 -07002631 }
liyuqianb73c24b2016-06-03 08:47:23 -07002632 } else if (stateName.equals(kSoftkeyStateName)) {
2633 if (!stateValue.equals(kSoftkeyHint)) {
2634 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002635 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002636 }
liyuqian2edb0f42016-07-06 14:11:32 -07002637 } else if (stateName.equals(kRefreshStateName)) {
2638 // This state is actually NOT in the UI state.
2639 // We use this to allow Android to quickly set bool fRefresh.
2640 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002641 } else {
2642 SkDebugf("Unknown stateName: %s", stateName.c_str());
2643 }
2644}
Brian Osman79086b92017-02-10 13:36:16 -05002645
Hal Canaryb1f411a2019-08-29 10:39:22 -04002646bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002647 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002648}
2649
Hal Canaryb1f411a2019-08-29 10:39:22 -04002650bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002651 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002652 fWindow->inval();
2653 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002654 } else {
2655 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002656 }
Brian Osman79086b92017-02-10 13:36:16 -05002657}