blob: 9c2e6e67826f7e88c755a9aa97f3d464217536df [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkGraphics.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkSurface.h"
Robert Phillipsed653392020-07-10 13:55:21 -040014#include "include/gpu/GrDirectContext.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050015#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkTo.h"
17#include "include/utils/SkPaintFilterCanvas.h"
18#include "src/core/SkColorSpacePriv.h"
19#include "src/core/SkImagePriv.h"
20#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkOSFile.h"
22#include "src/core/SkScan.h"
John Stilesdf078002020-07-14 09:44:57 -040023#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040025#include "src/core/SkTextBlobPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040026#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrGpu.h"
28#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070029#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Chris Daltonff18ff62020-12-07 17:39:26 -070031#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Adlai Hollerbcfc5542020-08-27 12:44:07 -040032#include "src/image/SkImage_Base.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/utils/SkJSONWriter.h"
34#include "src/utils/SkOSPath.h"
35#include "tools/Resources.h"
36#include "tools/ToolUtils.h"
37#include "tools/flags/CommandLineFlags.h"
38#include "tools/flags/CommonFlags.h"
39#include "tools/trace/EventTracingPriv.h"
40#include "tools/viewer/BisectSlide.h"
41#include "tools/viewer/GMSlide.h"
42#include "tools/viewer/ImageSlide.h"
43#include "tools/viewer/ParticlesSlide.h"
44#include "tools/viewer/SKPSlide.h"
45#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050046#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "tools/viewer/SlideDir.h"
48#include "tools/viewer/SvgSlide.h"
49#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070050
Chris Dalton17dc4182020-03-25 16:18:16 -060051#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040052#include <map>
53
Hal Canary8a001442018-09-19 11:31:27 -040054#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040055#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040056
Brian Osmanc85f1fa2020-06-16 15:11:34 -040057#ifdef SK_VULKAN
58#include "spirv-tools/libspirv.hpp"
59#endif
60
Florin Malita87ccf332018-05-04 12:23:24 -040061#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050062 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040063#endif
Florin Malita45cd2002020-06-09 14:00:54 -040064#if defined(SK_ENABLE_SKRIVE)
65 #include "tools/viewer/SkRiveSlide.h"
66#endif
Florin Malita87ccf332018-05-04 12:23:24 -040067
Brian Osman5e7fbfd2019-05-03 13:13:35 -040068class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
69public:
70 void compileError(const char* shader, const char* errors) override {
71 fShaders.push_back(SkString(shader));
72 fErrors.push_back(SkString(errors));
73 }
74
75 void reset() {
76 fShaders.reset();
77 fErrors.reset();
78 }
79
80 SkTArray<SkString> fShaders;
81 SkTArray<SkString> fErrors;
82};
83
84static CapturingShaderErrorHandler gShaderErrorHandler;
85
Brian Osmanf847f312020-06-18 14:18:27 -040086GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
87
jvanverth34524262016-05-04 13:49:13 -070088using namespace sk_app;
89
csmartdalton61cd31a2017-02-27 17:00:53 -070090static std::map<GpuPathRenderers, std::string> gPathRendererNames;
91
jvanverth9f372462016-04-06 06:08:59 -070092Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070093 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070094}
95
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060096static DEFINE_string(slide, "", "Start on this sample.");
97static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050098
Jim Van Verth682a2f42020-05-13 16:54:09 -040099#ifdef SK_GL
100#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700101#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400102#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700103#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400104#ifdef SK_VULKAN
105#define VK_BACKEND_STR ", \"vk\""
106#else
107#define VK_BACKEND_STR
108#endif
109#ifdef SK_METAL
110#define MTL_BACKEND_STR ", \"mtl\""
111#else
112#define MTL_BACKEND_STR
113#endif
114#ifdef SK_DIRECT3D
115#define D3D_BACKEND_STR ", \"d3d\""
116#else
117#define D3D_BACKEND_STR
118#endif
119#ifdef SK_DAWN
120#define DAWN_BACKEND_STR ", \"dawn\""
121#else
122#define DAWN_BACKEND_STR
123#endif
124#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
125#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
126 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700127
Brian Osman2dd96932016-10-18 15:33:53 -0400128static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700129
Mike Klein5b3f3432019-03-21 11:42:21 -0500130static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -0700131
Mike Klein84836b72019-03-21 11:31:36 -0500132static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700133
Mike Klein84836b72019-03-21 11:31:36 -0500134static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400135
Mike Kleinc6142d82019-03-25 10:54:59 -0500136static DEFINE_string2(match, m, nullptr,
137 "[~][^]substring[$] [...] of name to run.\n"
138 "Multiple matches may be separated by spaces.\n"
139 "~ causes a matching name to always be skipped\n"
140 "^ requires the start of the name to match\n"
141 "$ requires the end of the name to match\n"
142 "^ and $ requires an exact match\n"
143 "If a name does not match any list entry,\n"
144 "it is skipped unless some list entry starts with ~");
145
Mike Klein19fb3972019-03-21 13:08:08 -0500146#if defined(SK_BUILD_FOR_ANDROID)
147 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500148 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
149 static DEFINE_string(lotties, "/data/local/tmp/lotties",
150 "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400151 static DEFINE_string(rives, "/data/local/tmp/rives",
152 "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500153#else
154 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500155 static DEFINE_string(skps, "skps", "Directory to read skps from.");
156 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400157 static DEFINE_string(rives, "rives", "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500158#endif
159
Mike Kleinc6142d82019-03-25 10:54:59 -0500160static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
161
162static DEFINE_int_2(threads, j, -1,
163 "Run threadsafe tests on a threadpool with this many extra threads, "
164 "defaulting to one extra thread per core.");
165
Jim Van Verth7b558182019-11-14 16:47:01 -0500166static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
167
Chris Daltonc8877332020-01-06 09:48:30 -0700168static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500169static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
170static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500171static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500172static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500173static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500174
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400175#ifndef SK_GL
176static_assert(false, "viewer requires GL backend for raster.")
177#endif
178
Brian Salomon194db172017-08-17 14:37:06 -0400179const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700180 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400181#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
182 "ANGLE",
183#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400184#ifdef SK_DAWN
185 "Dawn",
186#endif
jvanverth063ece72016-06-17 09:29:14 -0700187#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700188 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700189#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400190#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500191 "Metal",
192#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400193#ifdef SK_DIRECT3D
194 "Direct3D",
195#endif
csmartdalton578f0642017-02-24 16:04:47 -0700196 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700197};
198
bsalomon6c471f72016-07-26 12:56:32 -0700199static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400200#ifdef SK_DAWN
201 if (0 == strcmp(str, "dawn")) {
202 return sk_app::Window::kDawn_BackendType;
203 } else
204#endif
bsalomon6c471f72016-07-26 12:56:32 -0700205#ifdef SK_VULKAN
206 if (0 == strcmp(str, "vk")) {
207 return sk_app::Window::kVulkan_BackendType;
208 } else
209#endif
Brian Salomon194db172017-08-17 14:37:06 -0400210#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
211 if (0 == strcmp(str, "angle")) {
212 return sk_app::Window::kANGLE_BackendType;
213 } else
214#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400215#ifdef SK_METAL
216 if (0 == strcmp(str, "mtl")) {
217 return sk_app::Window::kMetal_BackendType;
218 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500219#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400220#ifdef SK_DIRECT3D
221 if (0 == strcmp(str, "d3d")) {
222 return sk_app::Window::kDirect3D_BackendType;
223 } else
224#endif
225
bsalomon6c471f72016-07-26 12:56:32 -0700226 if (0 == strcmp(str, "gl")) {
227 return sk_app::Window::kNativeGL_BackendType;
228 } else if (0 == strcmp(str, "sw")) {
229 return sk_app::Window::kRaster_BackendType;
230 } else {
231 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
232 return sk_app::Window::kRaster_BackendType;
233 }
234}
235
Brian Osmana109e392017-02-24 09:49:14 -0500236static SkColorSpacePrimaries gSrgbPrimaries = {
237 0.64f, 0.33f,
238 0.30f, 0.60f,
239 0.15f, 0.06f,
240 0.3127f, 0.3290f };
241
242static SkColorSpacePrimaries gAdobePrimaries = {
243 0.64f, 0.33f,
244 0.21f, 0.71f,
245 0.15f, 0.06f,
246 0.3127f, 0.3290f };
247
248static SkColorSpacePrimaries gP3Primaries = {
249 0.680f, 0.320f,
250 0.265f, 0.690f,
251 0.150f, 0.060f,
252 0.3127f, 0.3290f };
253
254static SkColorSpacePrimaries gRec2020Primaries = {
255 0.708f, 0.292f,
256 0.170f, 0.797f,
257 0.131f, 0.046f,
258 0.3127f, 0.3290f };
259
260struct NamedPrimaries {
261 const char* fName;
262 SkColorSpacePrimaries* fPrimaries;
263} gNamedPrimaries[] = {
264 { "sRGB", &gSrgbPrimaries },
265 { "AdobeRGB", &gAdobePrimaries },
266 { "P3", &gP3Primaries },
267 { "Rec. 2020", &gRec2020Primaries },
268};
269
270static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
271 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
272}
273
Brian Osman70d2f432017-11-08 09:54:10 -0500274static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
275 // In raster mode, we still use GL for the window.
276 // This lets us render the GUI faster (and correct).
277 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
278}
279
Jim Van Verth74826c82019-03-01 14:37:30 -0500280class NullSlide : public Slide {
281 SkISize getDimensions() const override {
282 return SkISize::Make(640, 480);
283 }
284
285 void draw(SkCanvas* canvas) override {
286 canvas->clear(0xffff11ff);
287 }
288};
289
John Stiles31964fd2020-05-05 16:05:47 -0400290static const char kName[] = "name";
291static const char kValue[] = "value";
292static const char kOptions[] = "options";
293static const char kSlideStateName[] = "Slide";
294static const char kBackendStateName[] = "Backend";
295static const char kMSAAStateName[] = "MSAA";
296static const char kPathRendererStateName[] = "Path renderer";
297static const char kSoftkeyStateName[] = "Softkey";
298static const char kSoftkeyHint[] = "Please select a softkey";
299static const char kON[] = "ON";
300static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700301
Mike Reed862818b2020-03-21 15:07:13 -0400302extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500303extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500304extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400305
jvanverth34524262016-05-04 13:49:13 -0700306Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500307 : fCurrentSlide(-1)
308 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500309 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400310 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500311 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500312 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500313 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500314 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400315 , fZoomWindowFixed(false)
316 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500317 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400318 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700319 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500320 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500321 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500322 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500323 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500324 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700325 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400326 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400327 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400328 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500329 , fTiled(false)
330 , fDrawTileBoundaries(false)
331 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400332 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700333{
Greg Daniel285db442016-10-14 09:12:53 -0400334 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700335
Chris Dalton37ae4b02019-12-28 14:51:11 -0700336 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600337 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500338 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500339 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600340 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600341 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500342 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700343
jvanverth2bb3b6d2016-04-08 07:24:09 -0700344 SkDebugf("Command line arguments: ");
345 for (int i = 1; i < argc; ++i) {
346 SkDebugf("%s ", argv[i]);
347 }
348 SkDebugf("\n");
349
Mike Klein88544fb2019-03-20 10:50:33 -0500350 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500351#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400352 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500353#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700354
Mike Reed862818b2020-03-21 15:07:13 -0400355 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500356 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500357 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400358
Mike Klein19cc0f62019-03-22 15:30:07 -0500359 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500360
Brian Osmanbc8150f2017-07-24 11:38:01 -0400361 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400362 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400363
bsalomon6c471f72016-07-26 12:56:32 -0700364 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700365 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700366
csmartdalton578f0642017-02-24 16:04:47 -0700367 DisplayParams displayParams;
368 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500369 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700370 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400371 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400372 displayParams.fGrContextOptions.fShaderCacheStrategy =
373 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400374 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
375 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700376 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400377 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500378 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700379
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500380 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500381
Brian Osman56a24812017-12-19 11:15:16 -0500382 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500383 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500384 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
385 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
386 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
387
jvanverth9f372462016-04-06 06:08:59 -0700388 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700389 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500390 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500391 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500392 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700393
brianosman622c8d52016-05-10 06:50:49 -0700394 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500395 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
396 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
397 fWindow->inval();
398 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500399 // Command to jump directly to the slide picker and give it focus
400 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
401 this->fShowImGuiDebugWindow = true;
402 this->fShowSlidePicker = true;
403 fWindow->inval();
404 });
405 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400406 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500407 this->fShowImGuiDebugWindow = true;
408 this->fShowSlidePicker = true;
409 fWindow->inval();
410 });
Brian Osman79086b92017-02-10 13:36:16 -0500411 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
412 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
413 fWindow->inval();
414 });
Brian Osmanf6877092017-02-13 09:39:57 -0500415 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
416 this->fShowZoomWindow = !this->fShowZoomWindow;
417 fWindow->inval();
418 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400419 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
420 this->fZoomWindowFixed = !this->fZoomWindowFixed;
421 fWindow->inval();
422 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400423 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400424 DisplayParams params = fWindow->getRequestedDisplayParams();
425 params.fDisableVsync = !params.fDisableVsync;
426 fWindow->setRequestedDisplayParams(params);
427 this->updateTitle();
428 fWindow->inval();
429 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400430 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
431 DisplayParams params = fWindow->getRequestedDisplayParams();
432 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
433 fWindow->setRequestedDisplayParams(params);
434 this->updateTitle();
435 fWindow->inval();
436 });
Mike Reedf702ed42019-07-22 17:00:49 -0400437 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
438 fRefresh = !fRefresh;
439 fWindow->inval();
440 });
brianosman622c8d52016-05-10 06:50:49 -0700441 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500442 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700443 fWindow->inval();
444 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400445 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500446 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400447 this->updateTitle();
448 fWindow->inval();
449 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500450 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500451 switch (fColorMode) {
452 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500453 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500454 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500455 case ColorMode::kColorManaged8888:
456 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500457 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500458 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400459 this->setColorMode(ColorMode::kColorManagedF16Norm);
460 break;
461 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500462 this->setColorMode(ColorMode::kLegacy);
463 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500464 }
brianosman622c8d52016-05-10 06:50:49 -0700465 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700466 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
467 DisplayParams params = fWindow->getRequestedDisplayParams();
468 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
469 fWindow->setRequestedDisplayParams(params);
470 fWindow->inval();
471 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400472 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500473 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700474 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400475 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500476 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700477 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400478 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700479 this->changeZoomLevel(1.f / 32.f);
480 fWindow->inval();
481 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400482 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700483 this->changeZoomLevel(-1.f / 32.f);
484 fWindow->inval();
485 });
jvanverthaf236b52016-05-20 06:01:06 -0700486 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400487 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
488 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500489 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400490#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
491 if (newBackend == sk_app::Window::kVulkan_BackendType) {
492 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
493 sk_app::Window::kBackendTypeCount);
494 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
495 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500496 }
497#endif
Brian Osman621491e2017-02-28 15:45:01 -0500498 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700499 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500500 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
501 fSaveToSKP = true;
502 fWindow->inval();
503 });
Mike Reed376d8122019-03-14 11:39:02 -0400504 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
505 fShowSlideDimensions = !fShowSlideDimensions;
506 fWindow->inval();
507 });
Ben Wagner37c54032018-04-13 14:30:23 -0400508 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
509 DisplayParams params = fWindow->getRequestedDisplayParams();
510 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400511 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
512 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
513 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400514 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
515 } else {
516 switch (params.fSurfaceProps.pixelGeometry()) {
517 case kUnknown_SkPixelGeometry:
518 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
519 break;
520 case kRGB_H_SkPixelGeometry:
521 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
522 break;
523 case kBGR_H_SkPixelGeometry:
524 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
525 break;
526 case kRGB_V_SkPixelGeometry:
527 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
528 break;
529 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400530 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
531 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400532 break;
533 }
534 }
535 fWindow->setRequestedDisplayParams(params);
536 this->updateTitle();
537 fWindow->inval();
538 });
Ben Wagner9613e452019-01-23 10:34:59 -0500539 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500540 if (!fFontOverrides.fHinting) {
541 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400542 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500543 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500544 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400545 case SkFontHinting::kNone:
546 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500547 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400548 case SkFontHinting::kSlight:
549 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500550 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400551 case SkFontHinting::kNormal:
552 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500553 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400554 case SkFontHinting::kFull:
555 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500556 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500557 break;
558 }
559 }
560 this->updateTitle();
561 fWindow->inval();
562 });
563 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500564 if (!fPaintOverrides.fAntiAlias) {
565 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
566 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500567 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500568 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500569 } else {
570 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500571 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500572 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500573 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400574 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500575 break;
576 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500577 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500578 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400579 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500580 break;
581 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500582 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400583 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500584 break;
585 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500586 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
587 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500588 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
589 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500590 break;
591 }
592 }
593 this->updateTitle();
594 fWindow->inval();
595 });
Ben Wagner37c54032018-04-13 14:30:23 -0400596 fCommands.addCommand('D', "Modes", "DFT", [this]() {
597 DisplayParams params = fWindow->getRequestedDisplayParams();
598 uint32_t flags = params.fSurfaceProps.flags();
599 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
600 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
601 fWindow->setRequestedDisplayParams(params);
602 this->updateTitle();
603 fWindow->inval();
604 });
Ben Wagner9613e452019-01-23 10:34:59 -0500605 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500606 if (!fFontOverrides.fEdging) {
607 fFontOverrides.fEdging = true;
608 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500609 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500610 switch (fFont.getEdging()) {
611 case SkFont::Edging::kAlias:
612 fFont.setEdging(SkFont::Edging::kAntiAlias);
613 break;
614 case SkFont::Edging::kAntiAlias:
615 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
616 break;
617 case SkFont::Edging::kSubpixelAntiAlias:
618 fFont.setEdging(SkFont::Edging::kAlias);
619 fFontOverrides.fEdging = false;
620 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500621 }
622 }
623 this->updateTitle();
624 fWindow->inval();
625 });
Ben Wagner9613e452019-01-23 10:34:59 -0500626 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500627 if (!fFontOverrides.fSubpixel) {
628 fFontOverrides.fSubpixel = true;
629 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500630 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500631 if (!fFont.isSubpixel()) {
632 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500633 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500634 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500635 }
636 }
637 this->updateTitle();
638 fWindow->inval();
639 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400640 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
641 if (!fFontOverrides.fBaselineSnap) {
642 fFontOverrides.fBaselineSnap = true;
643 fFont.setBaselineSnap(false);
644 } else {
645 if (!fFont.isBaselineSnap()) {
646 fFont.setBaselineSnap(true);
647 } else {
648 fFontOverrides.fBaselineSnap = false;
649 }
650 }
651 this->updateTitle();
652 fWindow->inval();
653 });
Brian Osman805a7272018-05-02 15:40:20 -0400654 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
655 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
656 : kPerspective_Real;
657 this->updateTitle();
658 fWindow->inval();
659 });
660 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
661 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
662 : kPerspective_Off;
663 this->updateTitle();
664 fWindow->inval();
665 });
Brian Osman207d4102019-01-10 09:40:58 -0500666 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
667 fAnimTimer.togglePauseResume();
668 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400669 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
670 fZoomUI = !fZoomUI;
671 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
672 fWindow->inval();
673 });
Mike Reed59295352020-03-12 13:56:34 -0400674 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
675 fDrawViaSerialize = !fDrawViaSerialize;
676 this->updateTitle();
677 fWindow->inval();
678 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500679 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400680 gUseSkVMBlitter = !gUseSkVMBlitter;
681 this->updateTitle();
682 fWindow->inval();
683 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500684 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
685 gSkVMAllowJIT = !gSkVMAllowJIT;
686 this->updateTitle();
687 fWindow->inval();
688 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500689
jvanverth2bb3b6d2016-04-08 07:24:09 -0700690 // set up slides
691 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500692 if (FLAGS_list) {
693 this->listNames();
694 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700695
Brian Osman9bb47cf2018-04-26 15:55:00 -0400696 fPerspectivePoints[0].set(0, 0);
697 fPerspectivePoints[1].set(1, 0);
698 fPerspectivePoints[2].set(0, 1);
699 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700700 fAnimTimer.run();
701
Hal Canaryc465d132017-12-08 10:21:31 -0500702 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500703 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500704 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500705 }
706 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500707
jongdeok.kim804f17e2019-02-26 14:39:23 +0900708 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500709 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700710}
711
jvanverth34524262016-05-04 13:49:13 -0700712void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400713 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
714 static const struct {
715 const char* fExtension;
716 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500717 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400718 const SlideFactory fFactory;
719 } gExternalSlidesInfo[] = {
720 { ".skp", "skp-dir", FLAGS_skps,
721 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
722 return sk_make_sp<SKPSlide>(name, path);}
723 },
724 { ".jpg", "jpg-dir", FLAGS_jpgs,
725 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
726 return sk_make_sp<ImageSlide>(name, path);}
727 },
Florin Malita87ccf332018-05-04 12:23:24 -0400728#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400729 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400730 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
731 return sk_make_sp<SkottieSlide>(name, path);}
732 },
Florin Malita87ccf332018-05-04 12:23:24 -0400733#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400734 #if defined(SK_ENABLE_SKRIVE)
735 { ".flr", "skrive-dir", FLAGS_rives,
736 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
737 return sk_make_sp<SkRiveSlide>(name, path);}
738 },
739 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400740#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400741 { ".svg", "svg-dir", FLAGS_svgs,
742 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
743 return sk_make_sp<SvgSlide>(name, path);}
744 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400745#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400746 };
jvanverthc265a922016-04-08 12:51:45 -0700747
Brian Salomon343553a2018-09-05 15:41:23 -0400748 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700749
Mike Klein88544fb2019-03-20 10:50:33 -0500750 const auto addSlide =
751 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
752 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
753 return;
754 }
liyuqian6f163d22016-06-13 12:26:45 -0700755
Mike Klein88544fb2019-03-20 10:50:33 -0500756 if (auto slide = fact(name, path)) {
757 dirSlides.push_back(slide);
758 fSlides.push_back(std::move(slide));
759 }
760 };
Florin Malita76a076b2018-02-15 18:40:48 -0500761
Florin Malita38792ce2018-05-08 10:36:18 -0400762 if (!FLAGS_file.isEmpty()) {
763 // single file mode
764 const SkString file(FLAGS_file[0]);
765
766 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
767 for (const auto& sinfo : gExternalSlidesInfo) {
768 if (file.endsWith(sinfo.fExtension)) {
769 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
770 return;
771 }
772 }
773
774 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
775 } else {
776 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
777 }
778
779 return;
780 }
781
782 // Bisect slide.
783 if (!FLAGS_bisect.isEmpty()) {
784 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500785 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400786 if (FLAGS_bisect.count() >= 2) {
787 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
788 bisect->onChar(*ch);
789 }
790 }
791 fSlides.push_back(std::move(bisect));
792 }
793 }
794
795 // GMs
796 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400797 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400798 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500799 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400800 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400801 fSlides.push_back(std::move(slide));
802 }
Florin Malita38792ce2018-05-08 10:36:18 -0400803 }
804 // reverse gms
805 int numGMs = fSlides.count() - firstGM;
806 for (int i = 0; i < numGMs/2; ++i) {
807 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
808 }
809
810 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400811 for (const SampleFactory factory : SampleRegistry::Range()) {
812 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500813 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400814 fSlides.push_back(slide);
815 }
Florin Malita38792ce2018-05-08 10:36:18 -0400816 }
817
Brian Osman7c979f52019-02-12 13:27:51 -0500818 // Particle demo
819 {
820 // TODO: Convert this to a sample
821 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500822 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500823 fSlides.push_back(std::move(slide));
824 }
825 }
826
Brian Osmand927bd22019-12-18 11:23:12 -0500827 // Runtime shader editor
828 {
829 sk_sp<Slide> slide(new SkSLSlide());
830 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
831 fSlides.push_back(std::move(slide));
832 }
833 }
834
Florin Malita0ffa3222018-04-05 14:34:45 -0400835 for (const auto& info : gExternalSlidesInfo) {
836 for (const auto& flag : info.fFlags) {
837 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
838 // single file
839 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
840 } else {
841 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400842 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400843 SkTArray<SkString> sortedFilenames;
844 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400845 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400846 sortedFilenames.push_back(name);
847 }
848 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400849 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000850 [](const SkString& a, const SkString& b) {
851 return strcmp(a.c_str(), b.c_str()) < 0;
852 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400853 }
854 for (const SkString& filename : sortedFilenames) {
855 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
856 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400857 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400858 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400859 if (!dirSlides.empty()) {
860 fSlides.push_back(
861 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
862 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500863 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400864 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400865 }
866 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500867
868 if (!fSlides.count()) {
869 sk_sp<Slide> slide(new NullSlide());
870 fSlides.push_back(std::move(slide));
871 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700872}
873
874
jvanverth34524262016-05-04 13:49:13 -0700875Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400876 for(auto& slide : fSlides) {
877 slide->gpuTeardown();
878 }
879
jvanverth9f372462016-04-06 06:08:59 -0700880 fWindow->detach();
881 delete fWindow;
882}
883
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500884struct SkPaintTitleUpdater {
885 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
886 void append(const char* s) {
887 if (fCount == 0) {
888 fTitle->append(" {");
889 } else {
890 fTitle->append(", ");
891 }
892 fTitle->append(s);
893 ++fCount;
894 }
895 void done() {
896 if (fCount > 0) {
897 fTitle->append("}");
898 }
899 }
900 SkString* fTitle;
901 int fCount;
902};
903
brianosman05de2162016-05-06 13:28:57 -0700904void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700905 if (!fWindow) {
906 return;
907 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500908 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700909 return; // Surface hasn't been created yet.
910 }
911
jvanverth34524262016-05-04 13:49:13 -0700912 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700913 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700914
Mike Kleine5acd752019-03-22 09:57:16 -0500915 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400916 if (gSkForceAnalyticAA) {
917 title.append(" <FAAA>");
918 } else {
919 title.append(" <AAA>");
920 }
921 }
Mike Reed59295352020-03-12 13:56:34 -0400922 if (fDrawViaSerialize) {
923 title.append(" <serialize>");
924 }
Mike Reed862818b2020-03-21 15:07:13 -0400925 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500926 title.append(" <SkVMBlitter>");
927 }
928 if (!gSkVMAllowJIT) {
929 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400930 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400931
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500932 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500933 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
934 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400935 const char* on, const char* off)
936 {
Ben Wagner9613e452019-01-23 10:34:59 -0500937 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400938 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500939 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400940 };
941
Ben Wagner9613e452019-01-23 10:34:59 -0500942 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
943 const char* on, const char* off)
944 {
945 if (fFontOverrides.*flag) {
946 paintTitle.append((fFont.*isFlag)() ? on : off);
947 }
948 };
949
950 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
951 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
952
953 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
954 "Force Autohint", "No Force Autohint");
955 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400956 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500957 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
958 "Linear Metrics", "Non-Linear Metrics");
959 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
960 "Bitmap Text", "No Bitmap Text");
961 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
962
963 if (fFontOverrides.fEdging) {
964 switch (fFont.getEdging()) {
965 case SkFont::Edging::kAlias:
966 paintTitle.append("Alias Text");
967 break;
968 case SkFont::Edging::kAntiAlias:
969 paintTitle.append("Antialias Text");
970 break;
971 case SkFont::Edging::kSubpixelAntiAlias:
972 paintTitle.append("Subpixel Antialias Text");
973 break;
974 }
975 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400976
Mike Reed3ae47332019-01-04 10:11:46 -0500977 if (fFontOverrides.fHinting) {
978 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400979 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500980 paintTitle.append("No Hinting");
981 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400982 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500983 paintTitle.append("Slight Hinting");
984 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400985 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500986 paintTitle.append("Normal Hinting");
987 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400988 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500989 paintTitle.append("Full Hinting");
990 break;
991 }
992 }
993 paintTitle.done();
994
Brian Osman92004802017-03-06 11:47:26 -0500995 switch (fColorMode) {
996 case ColorMode::kLegacy:
997 title.append(" Legacy 8888");
998 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500999 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001000 title.append(" ColorManaged 8888");
1001 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001002 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001003 title.append(" ColorManaged F16");
1004 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001005 case ColorMode::kColorManagedF16Norm:
1006 title.append(" ColorManaged F16 Norm");
1007 break;
Brian Osman92004802017-03-06 11:47:26 -05001008 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001009
Brian Osman92004802017-03-06 11:47:26 -05001010 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001011 int curPrimaries = -1;
1012 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1013 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1014 curPrimaries = i;
1015 break;
1016 }
1017 }
Brian Osman03115dc2018-11-26 13:55:19 -05001018 title.appendf(" %s Gamma %f",
1019 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001020 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001021 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001022
Ben Wagner37c54032018-04-13 14:30:23 -04001023 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001024 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001025 switch (params.fSurfaceProps.pixelGeometry()) {
1026 case kUnknown_SkPixelGeometry:
1027 title.append( " Flat");
1028 break;
1029 case kRGB_H_SkPixelGeometry:
1030 title.append( " RGB");
1031 break;
1032 case kBGR_H_SkPixelGeometry:
1033 title.append( " BGR");
1034 break;
1035 case kRGB_V_SkPixelGeometry:
1036 title.append( " RGBV");
1037 break;
1038 case kBGR_V_SkPixelGeometry:
1039 title.append( " BGRV");
1040 break;
1041 }
1042 }
1043
1044 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1045 title.append(" DFT");
1046 }
1047
csmartdalton578f0642017-02-24 16:04:47 -07001048 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001049 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001050 int msaa = fWindow->sampleCount();
1051 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001052 title.appendf(" MSAA: %i", msaa);
1053 }
1054 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001055
1056 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001057 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001058 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1059 }
1060
Brian Osman805a7272018-05-02 15:40:20 -04001061 if (kPerspective_Real == fPerspectiveMode) {
1062 title.append(" Perpsective (Real)");
1063 } else if (kPerspective_Fake == fPerspectiveMode) {
1064 title.append(" Perspective (Fake)");
1065 }
1066
brianosman05de2162016-05-06 13:28:57 -07001067 fWindow->setTitle(title.c_str());
1068}
1069
Florin Malitaab99c342018-01-16 16:23:03 -05001070int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001071
1072 if (!FLAGS_slide.isEmpty()) {
1073 int count = fSlides.count();
1074 for (int i = 0; i < count; i++) {
1075 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001076 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001077 }
1078 }
1079
1080 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1081 this->listNames();
1082 }
1083
Florin Malitaab99c342018-01-16 16:23:03 -05001084 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001085}
1086
Florin Malitaab99c342018-01-16 16:23:03 -05001087void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001088 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001089 for (const auto& slide : fSlides) {
1090 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001091 }
1092}
1093
Florin Malitaab99c342018-01-16 16:23:03 -05001094void Viewer::setCurrentSlide(int slide) {
1095 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001096
Florin Malitaab99c342018-01-16 16:23:03 -05001097 if (slide == fCurrentSlide) {
1098 return;
1099 }
1100
1101 if (fCurrentSlide >= 0) {
1102 fSlides[fCurrentSlide]->unload();
1103 }
1104
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001105 SkScalar scaleFactor = 1.0;
1106 if (fApplyBackingScale) {
1107 scaleFactor = fWindow->scaleFactor();
1108 }
1109 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1110 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001111 fCurrentSlide = slide;
1112 this->setupCurrentSlide();
1113}
1114
1115void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001116 if (fCurrentSlide >= 0) {
1117 // prepare dimensions for image slides
1118 fGesture.resetTouchState();
1119 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001120
Jim Van Verth0848fb02018-01-22 13:39:30 -05001121 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1122 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1123 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001124
Jim Van Verth0848fb02018-01-22 13:39:30 -05001125 // Start with a matrix that scales the slide to the available screen space
1126 if (fWindow->scaleContentToFit()) {
1127 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001128 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1129 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001130 }
liyuqiane46e4f02016-05-20 07:32:19 -07001131 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001132
1133 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001134 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001135
1136 this->updateTitle();
1137 this->updateUIState();
1138
1139 fStatsLayer.resetMeasurements();
1140
1141 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001142 }
jvanverthc265a922016-04-08 12:51:45 -07001143}
1144
Brian Osmanaba642c2020-02-06 12:52:25 -05001145#define MAX_ZOOM_LEVEL 8.0f
1146#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001147
jvanverth34524262016-05-04 13:49:13 -07001148void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001149 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001150 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001151 this->preTouchMatrixChanged();
1152}
Yuqian Li755778c2018-03-28 16:23:31 -04001153
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001154void Viewer::preTouchMatrixChanged() {
1155 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001156 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1157 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1158 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1159 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1160}
1161
Brian Osman805a7272018-05-02 15:40:20 -04001162SkMatrix Viewer::computePerspectiveMatrix() {
1163 SkScalar w = fWindow->width(), h = fWindow->height();
1164 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1165 SkPoint perspPts[4] = {
1166 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1167 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1168 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1169 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1170 };
1171 SkMatrix m;
1172 m.setPolyToPoly(orthoPts, perspPts, 4);
1173 return m;
1174}
1175
Yuqian Li755778c2018-03-28 16:23:31 -04001176SkMatrix Viewer::computePreTouchMatrix() {
1177 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001178
1179 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001180 if (fApplyBackingScale) {
1181 zoomScale *= fWindow->scaleFactor();
1182 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001183 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001184 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001185
1186 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1187 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001188
Brian Osman805a7272018-05-02 15:40:20 -04001189 if (kPerspective_Real == fPerspectiveMode) {
1190 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001191 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001192 }
1193
Yuqian Li755778c2018-03-28 16:23:31 -04001194 return m;
jvanverthc265a922016-04-08 12:51:45 -07001195}
1196
liyuqiand3cdbca2016-05-17 12:44:20 -07001197SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001198 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001199 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001200 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001201 return m;
jvanverthc265a922016-04-08 12:51:45 -07001202}
1203
Brian Osman621491e2017-02-28 15:45:01 -05001204void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001205 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001206 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001207 fBackendType = backendType;
1208
Robert Phillipse9229532020-06-26 10:10:49 -04001209 // The active context is going away in 'detach'
1210 for(auto& slide : fSlides) {
1211 slide->gpuTeardown();
1212 }
1213
Brian Osman621491e2017-02-28 15:45:01 -05001214 fWindow->detach();
1215
Brian Osman70d2f432017-11-08 09:54:10 -05001216#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001217 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1218 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001219 DisplayParams params = fWindow->getRequestedDisplayParams();
1220 delete fWindow;
1221 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001222
Brian Osman70d2f432017-11-08 09:54:10 -05001223 // re-register callbacks
1224 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001225 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001226 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001227 fWindow->pushLayer(&fImGuiLayer);
1228
Brian Osman70d2f432017-11-08 09:54:10 -05001229 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1230 // will still include our correct sample count. But the re-created fWindow will lose that
1231 // information. On Windows, we need to re-create the window when changing sample count,
1232 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1233 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1234 // as if we had never changed windows at all).
1235 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001236#endif
1237
Brian Osman70d2f432017-11-08 09:54:10 -05001238 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001239}
1240
Brian Osman92004802017-03-06 11:47:26 -05001241void Viewer::setColorMode(ColorMode colorMode) {
1242 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001243 this->updateTitle();
1244 fWindow->inval();
1245}
1246
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001247class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1248public:
Mike Reed3ae47332019-01-04 10:11:46 -05001249 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1250 SkFont* font, Viewer::SkFontFields* ffields)
1251 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001252 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001253 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1254 sk_sp<SkTextBlob>* cache) {
1255 bool blobWillChange = false;
1256 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001257 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1258 bool shouldDraw = this->filterFont(&filteredFont);
1259 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001260 blobWillChange = true;
1261 break;
1262 }
1263 }
1264 if (!blobWillChange) {
1265 return blob;
1266 }
1267
1268 SkTextBlobBuilder builder;
1269 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001270 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1271 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001272 if (!shouldDraw) {
1273 continue;
1274 }
1275
Mike Reed3ae47332019-01-04 10:11:46 -05001276 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001277
Ben Wagner41e40472018-09-24 13:01:54 -04001278 const SkTextBlobBuilder::RunBuffer& runBuffer
1279 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001280 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001281 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001282 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001283 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001284 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001285 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001286 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001287 it.glyphCount(), it.textSize(), SkString())
1288 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1289 uint32_t glyphCount = it.glyphCount();
1290 if (it.glyphs()) {
1291 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1292 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1293 }
1294 if (it.pos()) {
1295 size_t posSize = sizeof(decltype(*it.pos()));
1296 uint8_t positioning = it.positioning();
1297 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1298 }
1299 if (it.text()) {
1300 size_t textSize = sizeof(decltype(*it.text()));
1301 uint32_t textCount = it.textSize();
1302 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1303 }
1304 if (it.clusters()) {
1305 size_t clusterSize = sizeof(decltype(*it.clusters()));
1306 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1307 }
1308 }
1309 *cache = builder.make();
1310 return cache->get();
1311 }
1312 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1313 const SkPaint& paint) override {
1314 sk_sp<SkTextBlob> cache;
1315 this->SkPaintFilterCanvas::onDrawTextBlob(
1316 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1317 }
Mike Reed3ae47332019-01-04 10:11:46 -05001318 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001319 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001320 font->writable()->setSize(fFont->getSize());
1321 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001322 if (fFontOverrides->fScaleX) {
1323 font->writable()->setScaleX(fFont->getScaleX());
1324 }
1325 if (fFontOverrides->fSkewX) {
1326 font->writable()->setSkewX(fFont->getSkewX());
1327 }
Mike Reed3ae47332019-01-04 10:11:46 -05001328 if (fFontOverrides->fHinting) {
1329 font->writable()->setHinting(fFont->getHinting());
1330 }
Ben Wagner9613e452019-01-23 10:34:59 -05001331 if (fFontOverrides->fEdging) {
1332 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001333 }
Ben Wagner9613e452019-01-23 10:34:59 -05001334 if (fFontOverrides->fEmbolden) {
1335 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001336 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001337 if (fFontOverrides->fBaselineSnap) {
1338 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1339 }
Ben Wagner9613e452019-01-23 10:34:59 -05001340 if (fFontOverrides->fLinearMetrics) {
1341 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001342 }
Ben Wagner9613e452019-01-23 10:34:59 -05001343 if (fFontOverrides->fSubpixel) {
1344 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001345 }
Ben Wagner9613e452019-01-23 10:34:59 -05001346 if (fFontOverrides->fEmbeddedBitmaps) {
1347 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001348 }
Ben Wagner9613e452019-01-23 10:34:59 -05001349 if (fFontOverrides->fForceAutoHinting) {
1350 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001351 }
Ben Wagner9613e452019-01-23 10:34:59 -05001352
Mike Reed3ae47332019-01-04 10:11:46 -05001353 return true;
1354 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001355 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001356 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001357 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001358 }
Ben Wagner9613e452019-01-23 10:34:59 -05001359 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001360 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001361 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001362 return true;
1363 }
1364 SkPaint* fPaint;
1365 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001366 SkFont* fFont;
1367 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001368};
1369
Robert Phillips9882dae2019-03-04 11:00:10 -05001370void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001371 if (fCurrentSlide < 0) {
1372 return;
1373 }
1374
Robert Phillips9882dae2019-03-04 11:00:10 -05001375 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001376
Brian Osmanf750fbc2017-02-08 10:47:28 -05001377 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001378 SkSurface* slideSurface = surface;
1379 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001380 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001381
Brian Osmane0d4fba2017-03-15 10:24:55 -04001382 // 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 -05001383 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001384 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001385 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001386 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001387 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001388 }
1389
Brian Osman3ac99cf2017-12-01 11:23:53 -05001390 if (fSaveToSKP) {
1391 SkPictureRecorder recorder;
1392 SkCanvas* recorderCanvas = recorder.beginRecording(
1393 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001394 fSlides[fCurrentSlide]->draw(recorderCanvas);
1395 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1396 SkFILEWStream stream("sample_app.skp");
1397 picture->serialize(&stream);
1398 fSaveToSKP = false;
1399 }
1400
Brian Osmane9ed0f02018-11-26 14:50:05 -05001401 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001402 SkColorType colorType;
1403 switch (fColorMode) {
1404 case ColorMode::kLegacy:
1405 case ColorMode::kColorManaged8888:
1406 colorType = kN32_SkColorType;
1407 break;
1408 case ColorMode::kColorManagedF16:
1409 colorType = kRGBA_F16_SkColorType;
1410 break;
1411 case ColorMode::kColorManagedF16Norm:
1412 colorType = kRGBA_F16Norm_SkColorType;
1413 break;
1414 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001415
1416 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001417 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001418 slideCanvas->getProps(&props);
1419
Brian Osmane9ed0f02018-11-26 14:50:05 -05001420 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1421 return Window::kRaster_BackendType == this->fBackendType
1422 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001423 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001424 };
1425
Brian Osman03115dc2018-11-26 13:55:19 -05001426 // We need to render offscreen if we're...
1427 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1428 // ... in any raster mode, because the window surface is actually GL
1429 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001430 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001431 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001432 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001433 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001434 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001435 colorSpace != nullptr ||
1436 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001437
Brian Osmane9ed0f02018-11-26 14:50:05 -05001438 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001439 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001440 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001441 }
1442
Mike Reed59295352020-03-12 13:56:34 -04001443 SkPictureRecorder recorder;
1444 SkCanvas* recorderRestoreCanvas = nullptr;
1445 if (fDrawViaSerialize) {
1446 recorderRestoreCanvas = slideCanvas;
1447 slideCanvas = recorder.beginRecording(
1448 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1449 }
1450
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001451 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001452 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001453 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001454 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001455 if (fTiled) {
1456 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1457 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001458 for (int y = 0; y < fWindow->height(); y += tileH) {
1459 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001460 SkAutoCanvasRestore acr(slideCanvas, true);
1461 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1462 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001463 }
1464 }
1465
1466 // Draw borders between tiles
1467 if (fDrawTileBoundaries) {
1468 SkPaint border;
1469 border.setColor(0x60FF00FF);
1470 border.setStyle(SkPaint::kStroke_Style);
1471 for (int y = 0; y < fWindow->height(); y += tileH) {
1472 for (int x = 0; x < fWindow->width(); x += tileW) {
1473 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1474 }
1475 }
1476 }
1477 } else {
1478 slideCanvas->concat(this->computeMatrix());
1479 if (kPerspective_Real == fPerspectiveMode) {
1480 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1481 }
Mike Reed3ae47332019-01-04 10:11:46 -05001482 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001483 fSlides[fCurrentSlide]->draw(&filterCanvas);
1484 }
Brian Osman56a24812017-12-19 11:15:16 -05001485 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001486 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001487
Mike Reed59295352020-03-12 13:56:34 -04001488 if (recorderRestoreCanvas) {
1489 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1490 auto data = picture->serialize();
1491 slideCanvas = recorderRestoreCanvas;
1492 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1493 }
1494
Brian Osman1df161a2017-02-09 12:10:20 -05001495 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001496 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001497 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001498 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001499
1500 // If we rendered offscreen, snap an image and push the results to the window's canvas
1501 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001502 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001503
Robert Phillips9882dae2019-03-04 11:00:10 -05001504 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001505 SkPaint paint;
1506 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001507 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001508 int prePerspectiveCount = canvas->save();
1509 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001510 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001511 canvas->clear(SK_ColorWHITE);
1512 canvas->concat(this->computePerspectiveMatrix());
1513 }
Mike Reedb339d052021-01-28 11:20:41 -05001514 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001515 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001516 }
Mike Reed376d8122019-03-14 11:39:02 -04001517
1518 if (fShowSlideDimensions) {
1519 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1520 SkPaint paint;
1521 paint.setColor(0x40FFFF00);
1522 surface->getCanvas()->drawRect(r, paint);
1523 }
liyuqian6f163d22016-06-13 12:26:45 -07001524}
1525
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001526void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001527 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001528 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001529}
Jim Van Verth6f449692017-02-14 15:16:46 -05001530
Robert Phillips9882dae2019-03-04 11:00:10 -05001531void Viewer::onPaint(SkSurface* surface) {
1532 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001533
Robert Phillips9882dae2019-03-04 11:00:10 -05001534 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001535
Brian Osmand67e5182017-12-08 16:46:09 -05001536 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001537
Greg Daniel427d8eb2020-09-28 15:04:18 -04001538 fLastImage.reset();
1539
Robert Phillipsed653392020-07-10 13:55:21 -04001540 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001541 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001542 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001543 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001544}
1545
Ben Wagnera1915972018-08-09 15:06:19 -04001546void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001547 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001548 SkScalar scaleFactor = 1.0;
1549 if (fApplyBackingScale) {
1550 scaleFactor = fWindow->scaleFactor();
1551 }
1552 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001553 }
Ben Wagnera1915972018-08-09 15:06:19 -04001554}
1555
Florin Malitacefc1b92018-02-19 21:43:47 -05001556SkPoint Viewer::mapEvent(float x, float y) {
1557 const auto m = this->computeMatrix();
1558 SkMatrix inv;
1559
1560 SkAssertResult(m.invert(&inv));
1561
1562 return inv.mapXY(x, y);
1563}
1564
Hal Canaryb1f411a2019-08-29 10:39:22 -04001565bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001566 if (GestureDevice::kMouse == fGestureDevice) {
1567 return false;
1568 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001569
1570 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001571 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001572 fWindow->inval();
1573 return true;
1574 }
1575
liyuqiand3cdbca2016-05-17 12:44:20 -07001576 void* castedOwner = reinterpret_cast<void*>(owner);
1577 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001578 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001579 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001580#if defined(SK_BUILD_FOR_IOS)
1581 // TODO: move IOS swipe detection higher up into the platform code
1582 SkPoint dir;
1583 if (fGesture.isFling(&dir)) {
1584 // swiping left or right
1585 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1586 if (dir.fX < 0) {
1587 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1588 fCurrentSlide + 1 : 0);
1589 } else {
1590 this->setCurrentSlide(fCurrentSlide > 0 ?
1591 fCurrentSlide - 1 : fSlides.count() - 1);
1592 }
1593 }
1594 fGesture.reset();
1595 }
1596#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001597 break;
1598 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001599 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001600 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001601 break;
1602 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001603 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001604 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001605 break;
1606 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001607 default: {
1608 // kLeft and kRight are only for swipes
1609 SkASSERT(false);
1610 break;
1611 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001612 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001613 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001614 fWindow->inval();
1615 return true;
1616}
1617
Hal Canaryb1f411a2019-08-29 10:39:22 -04001618bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001619 if (GestureDevice::kTouch == fGestureDevice) {
1620 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001621 }
Brian Osman16c81a12017-12-20 11:58:34 -05001622
Florin Malitacefc1b92018-02-19 21:43:47 -05001623 const auto slidePt = this->mapEvent(x, y);
1624 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1625 fWindow->inval();
1626 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001627 }
1628
1629 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001630 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001631 fGesture.touchEnd(nullptr);
1632 break;
1633 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001634 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001635 fGesture.touchBegin(nullptr, x, y);
1636 break;
1637 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001638 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001639 fGesture.touchMoved(nullptr, x, y);
1640 break;
1641 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001642 default: {
1643 SkASSERT(false); // shouldn't see kRight or kLeft here
1644 break;
1645 }
Brian Osman16c81a12017-12-20 11:58:34 -05001646 }
1647 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1648
Hal Canaryb1f411a2019-08-29 10:39:22 -04001649 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001650 fWindow->inval();
1651 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001652 return true;
1653}
1654
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001655bool Viewer::onFling(skui::InputState state) {
1656 if (skui::InputState::kRight == state) {
1657 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1658 return true;
1659 } else if (skui::InputState::kLeft == state) {
1660 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1661 return true;
1662 }
1663 return false;
1664}
1665
1666bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1667 switch (state) {
1668 case skui::InputState::kDown:
1669 fGesture.startZoom();
1670 return true;
1671 break;
1672 case skui::InputState::kMove:
1673 fGesture.updateZoom(scale, x, y, x, y);
1674 return true;
1675 break;
1676 case skui::InputState::kUp:
1677 fGesture.endZoom();
1678 return true;
1679 break;
1680 default:
1681 SkASSERT(false);
1682 break;
1683 }
1684
1685 return false;
1686}
1687
Brian Osmana109e392017-02-24 09:49:14 -05001688static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001689 // The gamut image covers a (0.8 x 0.9) shaped region
1690 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001691
1692 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1693 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1694 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001695 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1696 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1697 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001698
Brian Osman535c5e32019-02-09 16:32:58 -05001699 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1700 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1701 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1702 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1703 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001704}
1705
Ben Wagner3627d2e2018-06-26 14:23:20 -04001706static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001707 ImGui::DragCanvas dc(pt);
1708 dc.fillColor(IM_COL32(0, 0, 0, 128));
1709 dc.dragPoint(pt);
1710 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001711}
1712
Brian Osman9bb47cf2018-04-26 15:55:00 -04001713static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001714 ImGui::DragCanvas dc(pts);
1715 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001716
Brian Osman535c5e32019-02-09 16:32:58 -05001717 for (int i = 0; i < 4; ++i) {
1718 dc.dragPoint(pts + i);
1719 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001720
Brian Osman535c5e32019-02-09 16:32:58 -05001721 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1722 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1723 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1724 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001725
Brian Osman535c5e32019-02-09 16:32:58 -05001726 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001727}
1728
John Stiles38b7d2f2020-06-24 12:13:31 -04001729static SkSL::String build_sksl_highlight_shader() {
1730 return SkSL::String("out half4 sk_FragColor;\n"
1731 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1732}
1733
1734static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1735 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1736 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1737 size_t pos = inShader.rfind("return *_out;\n");
1738 if (pos == std::string::npos) {
1739 return inShader;
1740 }
1741
1742 SkSL::String replacementShader = inShader;
1743 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1744 return replacementShader;
1745}
1746
1747static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1748 const char* versionDecl = shaderCaps.versionDeclString();
1749 SkSL::String highlight = versionDecl ? versionDecl : "";
1750 if (shaderCaps.usesPrecisionModifiers()) {
1751 highlight.append("precision mediump float;\n");
1752 }
1753 highlight.appendf("out vec4 sk_FragColor;\n"
1754 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1755 return highlight;
1756}
1757
Brian Osmand67e5182017-12-08 16:46:09 -05001758void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001759 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1760 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001761 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001762 }
1763
1764 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001765 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1766 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001767 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001768 DisplayParams params = fWindow->getRequestedDisplayParams();
1769 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001770 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001771
Brian Osmana109e392017-02-24 09:49:14 -05001772 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1773 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001774 if (ImGui::CollapsingHeader("Backend")) {
1775 int newBackend = static_cast<int>(fBackendType);
1776 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1777 ImGui::SameLine();
1778 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001779#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1780 ImGui::SameLine();
1781 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1782#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001783#if defined(SK_DAWN)
1784 ImGui::SameLine();
1785 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1786#endif
John Stilesf7da9232020-11-19 19:58:14 -05001787#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001788 ImGui::SameLine();
1789 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1790#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001791#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001792 ImGui::SameLine();
1793 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1794#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001795#if defined(SK_DIRECT3D)
1796 ImGui::SameLine();
1797 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1798#endif
Brian Osman621491e2017-02-28 15:45:01 -05001799 if (newBackend != fBackendType) {
1800 fDeferredActions.push_back([=]() {
1801 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1802 });
1803 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001804
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001805 bool* wire = &params.fGrContextOptions.fWireframeMode;
1806 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1807 paramsChanged = true;
1808 }
Brian Salomon99a33902017-03-07 15:16:34 -05001809
Brian Osman28b12522017-03-08 17:10:24 -05001810 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001811 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001812 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001813 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1814 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1815 1;
1816
1817 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1818 if (maxMSAA >= 4) {
1819 ImGui::Text("MSAA: ");
1820
1821 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1822 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1823 // don't show it in the list.
1824 if (curMSAA == 2) {
1825 continue;
1826 }
1827 ImGui::SameLine();
1828 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1829 &sampleCount, curMSAA);
1830 }
1831 }
Brian Osman28b12522017-03-08 17:10:24 -05001832
1833 if (sampleCount != params.fMSAASampleCount) {
1834 params.fMSAASampleCount = sampleCount;
1835 paramsChanged = true;
1836 }
1837 }
1838
Ben Wagner37c54032018-04-13 14:30:23 -04001839 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001840 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001841 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1842 }
1843 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1844 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1845 {
1846 uint32_t flags = params.fSurfaceProps.flags();
1847 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001848 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1849 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1850 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001851 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001852 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001853 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1854 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1855 }
1856 paramsChanged = true;
1857 }
1858
1859 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1860 if (ImGui::Checkbox("DFT", &useDFT)) {
1861 uint32_t flags = params.fSurfaceProps.flags();
1862 if (useDFT) {
1863 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1864 } else {
1865 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1866 }
1867 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1868 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1869 paramsChanged = true;
1870 }
1871
Brian Osman8a9de3d2017-03-01 14:59:05 -05001872 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001873 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001874 auto prButton = [&](GpuPathRenderers x) {
1875 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001876 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1877 params.fGrContextOptions.fGpuPathRenderers = x;
1878 paramsChanged = true;
1879 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001880 }
1881 };
1882
1883 if (!ctx) {
1884 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001885 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001886 const auto* caps = ctx->priv().caps();
1887 prButton(GpuPathRenderers::kDefault);
1888 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001889 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001890 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001891 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001892 if (caps->shaderCaps()->pathRenderingSupport()) {
1893 prButton(GpuPathRenderers::kStencilAndCover);
1894 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001895 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001896 if (1 == fWindow->sampleCount()) {
1897 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1898 prButton(GpuPathRenderers::kCoverageCounting);
1899 }
1900 prButton(GpuPathRenderers::kSmall);
1901 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001902 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001903 prButton(GpuPathRenderers::kNone);
1904 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001905 ImGui::TreePop();
1906 }
Brian Osman621491e2017-02-28 15:45:01 -05001907 }
1908
Ben Wagner964571d2019-03-08 12:35:06 -05001909 if (ImGui::CollapsingHeader("Tiling")) {
1910 ImGui::Checkbox("Enable", &fTiled);
1911 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1912 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1913 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1914 }
1915
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001916 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001917 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1918 this->preTouchMatrixChanged();
1919 this->onResize(fWindow->width(), fWindow->height());
1920 paramsChanged = true;
1921 }
1922
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001923 float zoom = fZoomLevel;
1924 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1925 fZoomLevel = zoom;
1926 this->preTouchMatrixChanged();
1927 paramsChanged = true;
1928 }
1929 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001930 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001931 fRotation = deg;
1932 this->preTouchMatrixChanged();
1933 paramsChanged = true;
1934 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001935 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1936 if (ImGui_DragLocation(&fOffset)) {
1937 this->preTouchMatrixChanged();
1938 paramsChanged = true;
1939 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001940 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1941 this->preTouchMatrixChanged();
1942 paramsChanged = true;
1943 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001944 }
Brian Osman805a7272018-05-02 15:40:20 -04001945 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1946 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1947 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001948 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001949 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001950 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001951 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001952 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001953 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001954 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001955 }
1956
Ben Wagnera580fb32018-04-17 11:16:32 -04001957 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001958 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001959 if (fPaintOverrides.fAntiAlias) {
1960 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001961 }
1962 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001963 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001964 {
1965 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1966 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001967 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001968 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1969 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001970 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001971 fPaintOverrides.fAntiAlias = true;
1972 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001973 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001974 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001975 case SkPaintFields::AntiAliasState::Alias:
1976 break;
1977 case SkPaintFields::AntiAliasState::Normal:
1978 break;
1979 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1980 gSkUseAnalyticAA = true;
1981 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001982 break;
1983 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1984 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001985 break;
1986 }
1987 }
1988 paramsChanged = true;
1989 }
1990
Ben Wagner99a78dc2018-05-09 18:23:51 -04001991 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001992 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001993 bool (SkPaint::* isFlag)() const,
1994 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001995 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001996 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001997 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001998 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001999 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002000 if (ImGui::Combo(label, &itemIndex, items)) {
2001 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002002 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002003 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002004 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002005 (fPaint.*setFlag)(itemIndex == 2);
2006 }
2007 paramsChanged = true;
2008 }
2009 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002010
Ben Wagner99a78dc2018-05-09 18:23:51 -04002011 paintFlag("Dither",
2012 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002013 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002014 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagner9613e452019-01-23 10:34:59 -05002015 }
Hal Canary02738a82019-01-21 18:51:32 +00002016
Ben Wagner9613e452019-01-23 10:34:59 -05002017 if (ImGui::CollapsingHeader("Font")) {
2018 int hintingIdx = 0;
2019 if (fFontOverrides.fHinting) {
2020 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2021 }
2022 if (ImGui::Combo("Hinting", &hintingIdx,
2023 "Default\0None\0Slight\0Normal\0Full\0\0"))
2024 {
2025 if (hintingIdx == 0) {
2026 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002027 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002028 } else {
2029 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2030 fFontOverrides.fHinting = true;
2031 }
2032 paramsChanged = true;
2033 }
Hal Canary02738a82019-01-21 18:51:32 +00002034
Ben Wagner9613e452019-01-23 10:34:59 -05002035 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2036 bool SkFontFields::* flag,
2037 bool (SkFont::* isFlag)() const,
2038 void (SkFont::* setFlag)(bool) )
2039 {
2040 int itemIndex = 0;
2041 if (fFontOverrides.*flag) {
2042 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2043 }
2044 if (ImGui::Combo(label, &itemIndex, items)) {
2045 if (itemIndex == 0) {
2046 fFontOverrides.*flag = false;
2047 } else {
2048 fFontOverrides.*flag = true;
2049 (fFont.*setFlag)(itemIndex == 2);
2050 }
2051 paramsChanged = true;
2052 }
2053 };
Hal Canary02738a82019-01-21 18:51:32 +00002054
Ben Wagner9613e452019-01-23 10:34:59 -05002055 fontFlag("Fake Bold Glyphs",
2056 "Default\0No Fake Bold\0Fake Bold\0\0",
2057 &SkFontFields::fEmbolden,
2058 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002059
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002060 fontFlag("Baseline Snapping",
2061 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2062 &SkFontFields::fBaselineSnap,
2063 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2064
Ben Wagner9613e452019-01-23 10:34:59 -05002065 fontFlag("Linear Text",
2066 "Default\0No Linear Text\0Linear Text\0\0",
2067 &SkFontFields::fLinearMetrics,
2068 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002069
Ben Wagner9613e452019-01-23 10:34:59 -05002070 fontFlag("Subpixel Position Glyphs",
2071 "Default\0Pixel Text\0Subpixel Text\0\0",
2072 &SkFontFields::fSubpixel,
2073 &SkFont::isSubpixel, &SkFont::setSubpixel);
2074
2075 fontFlag("Embedded Bitmap Text",
2076 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2077 &SkFontFields::fEmbeddedBitmaps,
2078 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2079
2080 fontFlag("Force Auto-Hinting",
2081 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2082 &SkFontFields::fForceAutoHinting,
2083 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2084
2085 int edgingIdx = 0;
2086 if (fFontOverrides.fEdging) {
2087 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2088 }
2089 if (ImGui::Combo("Edging", &edgingIdx,
2090 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2091 {
2092 if (edgingIdx == 0) {
2093 fFontOverrides.fEdging = false;
2094 fFont.setEdging(SkFont::Edging::kAlias);
2095 } else {
2096 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2097 fFontOverrides.fEdging = true;
2098 }
2099 paramsChanged = true;
2100 }
2101
Ben Wagner15a8d572019-03-21 13:35:44 -04002102 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2103 if (fFontOverrides.fSize) {
2104 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002105 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002106 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002107 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002108 fFontOverrides.fSizeRange[0],
2109 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002110 "%.6f", 2.0f))
2111 {
Mike Reed3ae47332019-01-04 10:11:46 -05002112 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002113 paramsChanged = true;
2114 }
2115 }
2116
2117 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2118 if (fFontOverrides.fScaleX) {
2119 float scaleX = fFont.getScaleX();
2120 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2121 fFont.setScaleX(scaleX);
2122 paramsChanged = true;
2123 }
2124 }
2125
2126 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2127 if (fFontOverrides.fSkewX) {
2128 float skewX = fFont.getSkewX();
2129 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2130 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002131 paramsChanged = true;
2132 }
2133 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002134 }
2135
Mike Reed81f60ec2018-05-15 10:09:52 -04002136 {
2137 SkMetaData controls;
2138 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2139 if (ImGui::CollapsingHeader("Current Slide")) {
2140 SkMetaData::Iter iter(controls);
2141 const char* name;
2142 SkMetaData::Type type;
2143 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002144 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002145 if (type == SkMetaData::kScalar_Type) {
2146 float val[3];
2147 SkASSERT(count == 3);
2148 controls.findScalars(name, &count, val);
2149 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2150 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002151 }
Ben Wagner110c7032019-03-22 17:03:59 -04002152 } else if (type == SkMetaData::kBool_Type) {
2153 bool val;
2154 SkASSERT(count == 1);
2155 controls.findBool(name, &val);
2156 if (ImGui::Checkbox(name, &val)) {
2157 controls.setBool(name, val);
2158 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002159 }
2160 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002161 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002162 }
2163 }
2164 }
2165
Ben Wagner7a3c6742018-04-23 10:01:07 -04002166 if (fShowSlidePicker) {
2167 ImGui::SetNextTreeNodeOpen(true);
2168 }
Brian Osman79086b92017-02-10 13:36:16 -05002169 if (ImGui::CollapsingHeader("Slide")) {
2170 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002171 static ImVector<const char*> filteredSlideNames;
2172 static ImVector<int> filteredSlideIndices;
2173
Brian Osmanfce09c52017-11-14 15:32:20 -05002174 if (fShowSlidePicker) {
2175 ImGui::SetKeyboardFocusHere();
2176 fShowSlidePicker = false;
2177 }
2178
Brian Osman79086b92017-02-10 13:36:16 -05002179 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002180 filteredSlideNames.clear();
2181 filteredSlideIndices.clear();
2182 int filteredIndex = 0;
2183 for (int i = 0; i < fSlides.count(); ++i) {
2184 const char* slideName = fSlides[i]->getName().c_str();
2185 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2186 if (i == fCurrentSlide) {
2187 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002188 }
Brian Osmanf479e422017-11-08 13:11:36 -05002189 filteredSlideNames.push_back(slideName);
2190 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002191 }
Brian Osman79086b92017-02-10 13:36:16 -05002192 }
Brian Osmanf479e422017-11-08 13:11:36 -05002193
Brian Osmanf479e422017-11-08 13:11:36 -05002194 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2195 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002196 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002197 }
2198 }
Brian Osmana109e392017-02-24 09:49:14 -05002199
2200 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002201 ColorMode newMode = fColorMode;
2202 auto cmButton = [&](ColorMode mode, const char* label) {
2203 if (ImGui::RadioButton(label, mode == fColorMode)) {
2204 newMode = mode;
2205 }
2206 };
2207
2208 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002209 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2210 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002211 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002212
2213 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002214 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002215 }
2216
2217 // Pick from common gamuts:
2218 int primariesIdx = 4; // Default: Custom
2219 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2220 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2221 primariesIdx = i;
2222 break;
2223 }
2224 }
2225
Brian Osman03115dc2018-11-26 13:55:19 -05002226 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002227 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002228
Brian Osmana109e392017-02-24 09:49:14 -05002229 if (ImGui::Combo("Primaries", &primariesIdx,
2230 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2231 if (primariesIdx >= 0 && primariesIdx <= 3) {
2232 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2233 }
2234 }
2235
2236 // Allow direct editing of gamut
2237 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2238 }
Brian Osman207d4102019-01-10 09:40:58 -05002239
2240 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002241 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002242 if (ImGui::Checkbox("Pause", &isPaused)) {
2243 fAnimTimer.togglePauseResume();
2244 }
Brian Osman707d2022019-01-10 11:27:34 -05002245
2246 float speed = fAnimTimer.getSpeed();
2247 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2248 fAnimTimer.setSpeed(speed);
2249 }
Brian Osman207d4102019-01-10 09:40:58 -05002250 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002251
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002252 if (ImGui::CollapsingHeader("Shaders")) {
2253 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2254 GrContextOptions::ShaderCacheStrategy::kSkSL;
2255#if defined(SK_VULKAN)
2256 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2257#else
2258 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002259#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002260
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002261 // To re-load shaders from the currently active programs, we flush all
2262 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002263 static bool gLoadPending = false;
2264 if (gLoadPending) {
2265 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2266 int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002267 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002268 entry.fKey = key;
2269 SkMD5 hash;
2270 hash.write(key->bytes(), key->size());
2271 SkMD5::Digest digest = hash.finish();
2272 for (int i = 0; i < 16; ++i) {
2273 entry.fKeyString.appendf("%02x", digest.data[i]);
2274 }
2275
Brian Osman9e4e4c72020-06-10 07:19:34 -04002276 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002277 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002278 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2279 entry.fInputs,
2280 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002281 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002282 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002283 fPersistentCache.foreach(collectShaders);
2284 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002285
2286#if defined(SK_VULKAN)
2287 if (isVulkan && !sksl) {
2288 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2289 for (auto& entry : fCachedShaders) {
2290 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2291 const SkSL::String& spirv(entry.fShader[i]);
2292 std::string disasm;
2293 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2294 &disasm);
2295 entry.fShader[i].assign(disasm);
2296 }
2297 }
2298 }
2299#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002300 }
2301
2302 // Defer actually doing the load/save logic so that we can trigger a save when we
2303 // start or finish hovering on a tree node in the list below:
2304 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002305 bool doSave = ImGui::Button("Save"); ImGui::SameLine();
2306 if (ImGui::Checkbox("SkSL", &sksl)) {
2307 params.fGrContextOptions.fShaderCacheStrategy =
2308 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2309 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2310 paramsChanged = true;
2311 doLoad = true;
2312 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
Brian Osmancbc33b82019-04-19 14:16:19 -04002313 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002314
2315 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002316 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002317 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2318 bool hovered = ImGui::IsItemHovered();
2319 if (hovered != entry.fHovered) {
2320 // Force a save to patch the highlight shader in/out
2321 entry.fHovered = hovered;
2322 doSave = true;
2323 }
2324 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002325 auto stringBox = [](const char* label, std::string* str) {
2326 // Full width, and not too much space for each shader
2327 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2328 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2329 ImGui::InputTextMultiline(label, str, boxSize);
2330 };
2331 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2332 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002333 ImGui::TreePop();
2334 }
2335 }
2336 ImGui::EndChild();
2337
2338 if (doLoad) {
2339 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002340 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002341 gLoadPending = true;
2342 }
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002343 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2344 // but I'm not sure anyone wants to do that.
2345 if (isVulkan && !sksl) {
2346 doSave = false;
2347 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002348 if (doSave) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002349 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002350 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002351 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002352 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002353 if (entry.fHovered) {
2354 // The hovered item (if any) gets a special shader to make it
2355 // identifiable.
2356 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2357 switch (entry.fShaderType) {
2358 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2359 fragShader = build_sksl_highlight_shader();
2360 break;
2361 }
2362 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2363 fragShader = build_glsl_highlight_shader(
2364 *ctx->priv().caps()->shaderCaps());
2365 break;
2366 }
2367 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2368 fragShader = build_metal_highlight_shader(fragShader);
2369 break;
2370 }
2371 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002372 }
2373
Brian Osmana085a412019-04-25 09:44:43 -04002374 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2375 entry.fShader,
2376 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002377 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002378 fPersistentCache.store(*entry.fKey, *data);
2379
2380 entry.fShader[kFragment_GrShaderType] = backup;
2381 }
2382 }
2383 }
Brian Osman79086b92017-02-10 13:36:16 -05002384 }
Brian Salomon99a33902017-03-07 15:16:34 -05002385 if (paramsChanged) {
2386 fDeferredActions.push_back([=]() {
2387 fWindow->setRequestedDisplayParams(params);
2388 fWindow->inval();
2389 this->updateTitle();
2390 });
2391 }
Brian Osman79086b92017-02-10 13:36:16 -05002392 ImGui::End();
2393 }
2394
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002395 if (gShaderErrorHandler.fErrors.count()) {
2396 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002397 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002398 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2399 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002400 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2401 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2402 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2403 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002404 }
2405 ImGui::End();
2406 gShaderErrorHandler.reset();
2407 }
2408
Brian Osmanf6877092017-02-13 09:39:57 -05002409 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002410 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2411 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002412 static int zoomFactor = 8;
2413 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002414 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002415 }
2416 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2417 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002418 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002419 }
Brian Osmanf6877092017-02-13 09:39:57 -05002420
Ben Wagner3627d2e2018-06-26 14:23:20 -04002421 if (!fZoomWindowFixed) {
2422 ImVec2 mousePos = ImGui::GetMousePos();
2423 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2424 }
2425 SkScalar x = fZoomWindowLocation.x();
2426 SkScalar y = fZoomWindowLocation.y();
2427 int xInt = SkScalarRoundToInt(x);
2428 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002429 ImVec2 avail = ImGui::GetContentRegionAvail();
2430
Brian Osmanead517d2017-11-13 15:36:36 -05002431 uint32_t pixel = 0;
2432 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002433 auto dContext = fWindow->directContext();
2434 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002435 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002436 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002437 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002438 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002439 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2440 }
2441
Greg Danielfbc60b72020-11-03 17:27:02 -05002442 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002443 // Translate so the region of the image that's under the mouse cursor is centered
2444 // in the zoom canvas:
2445 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002446 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2447 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002448 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002449
2450 SkPaint outline;
2451 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002452 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002453 });
Brian Osmanf6877092017-02-13 09:39:57 -05002454 }
2455
2456 ImGui::End();
2457 }
Brian Osman79086b92017-02-10 13:36:16 -05002458}
2459
liyuqian2edb0f42016-07-06 14:11:32 -07002460void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002461 for (int i = 0; i < fDeferredActions.count(); ++i) {
2462 fDeferredActions[i]();
2463 }
2464 fDeferredActions.reset();
2465
Brian Osman56a24812017-12-19 11:15:16 -05002466 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002467 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002468 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002469 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002470
Brian Osman79086b92017-02-10 13:36:16 -05002471 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002472 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2473 // not be visible, though. So we need to redraw if there is at least one visible window, or
2474 // more than one active window. Newly created windows are active but not visible for one frame
2475 // while they determine their layout and sizing.
2476 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2477 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002478 fWindow->inval();
2479 }
jvanverth9f372462016-04-06 06:08:59 -07002480}
liyuqiane5a6cd92016-05-27 08:52:52 -07002481
Florin Malitab632df72018-06-18 21:23:06 -04002482template <typename OptionsFunc>
2483static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2484 OptionsFunc&& optionsFunc) {
2485 writer.beginObject();
2486 {
2487 writer.appendString(kName , name);
2488 writer.appendString(kValue, value);
2489
2490 writer.beginArray(kOptions);
2491 {
2492 optionsFunc(writer);
2493 }
2494 writer.endArray();
2495 }
2496 writer.endObject();
2497}
2498
2499
liyuqiane5a6cd92016-05-27 08:52:52 -07002500void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002501 if (!fWindow) {
2502 return;
2503 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002504 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002505 return; // Surface hasn't been created yet.
2506 }
2507
Florin Malitab632df72018-06-18 21:23:06 -04002508 SkDynamicMemoryWStream memStream;
2509 SkJSONWriter writer(&memStream);
2510 writer.beginArray();
2511
liyuqianb73c24b2016-06-03 08:47:23 -07002512 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002513 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2514 [this](SkJSONWriter& writer) {
2515 for(const auto& slide : fSlides) {
2516 writer.appendString(slide->getName().c_str());
2517 }
2518 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002519
liyuqianb73c24b2016-06-03 08:47:23 -07002520 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002521 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2522 [](SkJSONWriter& writer) {
2523 for (const auto& str : kBackendTypeStrings) {
2524 writer.appendString(str);
2525 }
2526 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002527
csmartdalton578f0642017-02-24 16:04:47 -07002528 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002529 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2530 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2531 [this](SkJSONWriter& writer) {
2532 writer.appendS32(0);
2533
2534 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2535 return;
2536 }
2537
2538 for (int msaa : {4, 8, 16}) {
2539 writer.appendS32(msaa);
2540 }
2541 });
csmartdalton578f0642017-02-24 16:04:47 -07002542
csmartdalton61cd31a2017-02-27 17:00:53 -07002543 // Path renderer state
2544 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002545 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2546 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002547 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002548 if (!ctx) {
2549 writer.appendString("Software");
2550 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002551 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002552 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2553 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002554 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002555 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002556 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002557 }
Florin Malitab632df72018-06-18 21:23:06 -04002558 if (caps->shaderCaps()->pathRenderingSupport()) {
2559 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002560 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002561 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002562 }
2563 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002564 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2565 writer.appendString(
2566 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2567 }
2568 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2569 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002570 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002571 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002572 }
2573 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002574
liyuqianb73c24b2016-06-03 08:47:23 -07002575 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002576 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2577 [this](SkJSONWriter& writer) {
2578 writer.appendString(kSoftkeyHint);
2579 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2580 writer.appendString(softkey.c_str());
2581 }
2582 });
liyuqianb73c24b2016-06-03 08:47:23 -07002583
Florin Malitab632df72018-06-18 21:23:06 -04002584 writer.endArray();
2585 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002586
Florin Malitab632df72018-06-18 21:23:06 -04002587 auto data = memStream.detachAsData();
2588
2589 // TODO: would be cool to avoid this copy
2590 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2591
2592 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002593}
2594
2595void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002596 // For those who will add more features to handle the state change in this function:
2597 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2598 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2599 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002600 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002601 for (int i = 0; i < fSlides.count(); ++i) {
2602 if (fSlides[i]->getName().equals(stateValue)) {
2603 this->setCurrentSlide(i);
2604 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002605 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002606 }
Florin Malitaab99c342018-01-16 16:23:03 -05002607
2608 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002609 } else if (stateName.equals(kBackendStateName)) {
2610 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2611 if (stateValue.equals(kBackendTypeStrings[i])) {
2612 if (fBackendType != i) {
2613 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002614 for(auto& slide : fSlides) {
2615 slide->gpuTeardown();
2616 }
liyuqian6cb70252016-06-02 12:16:25 -07002617 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002618 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002619 }
2620 break;
2621 }
2622 }
csmartdalton578f0642017-02-24 16:04:47 -07002623 } else if (stateName.equals(kMSAAStateName)) {
2624 DisplayParams params = fWindow->getRequestedDisplayParams();
2625 int sampleCount = atoi(stateValue.c_str());
2626 if (sampleCount != params.fMSAASampleCount) {
2627 params.fMSAASampleCount = sampleCount;
2628 fWindow->setRequestedDisplayParams(params);
2629 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002630 this->updateTitle();
2631 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002632 }
2633 } else if (stateName.equals(kPathRendererStateName)) {
2634 DisplayParams params = fWindow->getRequestedDisplayParams();
2635 for (const auto& pair : gPathRendererNames) {
2636 if (pair.second == stateValue.c_str()) {
2637 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2638 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2639 fWindow->setRequestedDisplayParams(params);
2640 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002641 this->updateTitle();
2642 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002643 }
2644 break;
2645 }
csmartdalton578f0642017-02-24 16:04:47 -07002646 }
liyuqianb73c24b2016-06-03 08:47:23 -07002647 } else if (stateName.equals(kSoftkeyStateName)) {
2648 if (!stateValue.equals(kSoftkeyHint)) {
2649 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002650 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002651 }
liyuqian2edb0f42016-07-06 14:11:32 -07002652 } else if (stateName.equals(kRefreshStateName)) {
2653 // This state is actually NOT in the UI state.
2654 // We use this to allow Android to quickly set bool fRefresh.
2655 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002656 } else {
2657 SkDebugf("Unknown stateName: %s", stateName.c_str());
2658 }
2659}
Brian Osman79086b92017-02-10 13:36:16 -05002660
Hal Canaryb1f411a2019-08-29 10:39:22 -04002661bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002662 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002663}
2664
Hal Canaryb1f411a2019-08-29 10:39:22 -04002665bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002666 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002667 fWindow->inval();
2668 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002669 } else {
2670 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002671 }
Brian Osman79086b92017-02-10 13:36:16 -05002672}