blob: ad8e7c382f1f5475a36821340d7028a15bafc96e [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"
14#include "include/gpu/GrContext.h"
15#include "include/private/SkTo.h"
16#include "include/utils/SkPaintFilterCanvas.h"
17#include "src/core/SkColorSpacePriv.h"
18#include "src/core/SkImagePriv.h"
19#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkOSFile.h"
21#include "src/core/SkScan.h"
22#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040023#include "src/core/SkTextBlobPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrGpu.h"
26#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070027#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
29#include "src/utils/SkJSONWriter.h"
30#include "src/utils/SkOSPath.h"
31#include "tools/Resources.h"
32#include "tools/ToolUtils.h"
33#include "tools/flags/CommandLineFlags.h"
34#include "tools/flags/CommonFlags.h"
35#include "tools/trace/EventTracingPriv.h"
36#include "tools/viewer/BisectSlide.h"
37#include "tools/viewer/GMSlide.h"
38#include "tools/viewer/ImageSlide.h"
39#include "tools/viewer/ParticlesSlide.h"
40#include "tools/viewer/SKPSlide.h"
41#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050042#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "tools/viewer/SlideDir.h"
44#include "tools/viewer/SvgSlide.h"
45#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070046
Chris Dalton17dc4182020-03-25 16:18:16 -060047#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040048#include <map>
49
Hal Canary8a001442018-09-19 11:31:27 -040050#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040051#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040052
Florin Malita87ccf332018-05-04 12:23:24 -040053#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040055#endif
Florin Malita45cd2002020-06-09 14:00:54 -040056#if defined(SK_ENABLE_SKRIVE)
57 #include "tools/viewer/SkRiveSlide.h"
58#endif
Florin Malita87ccf332018-05-04 12:23:24 -040059
Brian Osman5e7fbfd2019-05-03 13:13:35 -040060class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
61public:
62 void compileError(const char* shader, const char* errors) override {
63 fShaders.push_back(SkString(shader));
64 fErrors.push_back(SkString(errors));
65 }
66
67 void reset() {
68 fShaders.reset();
69 fErrors.reset();
70 }
71
72 SkTArray<SkString> fShaders;
73 SkTArray<SkString> fErrors;
74};
75
76static CapturingShaderErrorHandler gShaderErrorHandler;
77
jvanverth34524262016-05-04 13:49:13 -070078using namespace sk_app;
79
csmartdalton61cd31a2017-02-27 17:00:53 -070080static std::map<GpuPathRenderers, std::string> gPathRendererNames;
81
jvanverth9f372462016-04-06 06:08:59 -070082Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070083 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070084}
85
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060086static DEFINE_string(slide, "", "Start on this sample.");
87static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050088
Jim Van Verth682a2f42020-05-13 16:54:09 -040089#ifdef SK_GL
90#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -070091#else
Jim Van Verth682a2f42020-05-13 16:54:09 -040092#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -070093#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -040094#ifdef SK_VULKAN
95#define VK_BACKEND_STR ", \"vk\""
96#else
97#define VK_BACKEND_STR
98#endif
99#ifdef SK_METAL
100#define MTL_BACKEND_STR ", \"mtl\""
101#else
102#define MTL_BACKEND_STR
103#endif
104#ifdef SK_DIRECT3D
105#define D3D_BACKEND_STR ", \"d3d\""
106#else
107#define D3D_BACKEND_STR
108#endif
109#ifdef SK_DAWN
110#define DAWN_BACKEND_STR ", \"dawn\""
111#else
112#define DAWN_BACKEND_STR
113#endif
114#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
115#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
116 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700117
Brian Osman2dd96932016-10-18 15:33:53 -0400118static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700119
Mike Klein5b3f3432019-03-21 11:42:21 -0500120static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -0700121
Mike Klein84836b72019-03-21 11:31:36 -0500122static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700123
Mike Klein84836b72019-03-21 11:31:36 -0500124static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400125
Mike Kleinc6142d82019-03-25 10:54:59 -0500126static DEFINE_string2(match, m, nullptr,
127 "[~][^]substring[$] [...] of name to run.\n"
128 "Multiple matches may be separated by spaces.\n"
129 "~ causes a matching name to always be skipped\n"
130 "^ requires the start of the name to match\n"
131 "$ requires the end of the name to match\n"
132 "^ and $ requires an exact match\n"
133 "If a name does not match any list entry,\n"
134 "it is skipped unless some list entry starts with ~");
135
Mike Klein19fb3972019-03-21 13:08:08 -0500136#if defined(SK_BUILD_FOR_ANDROID)
137 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500138 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
139 static DEFINE_string(lotties, "/data/local/tmp/lotties",
140 "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400141 static DEFINE_string(rives, "/data/local/tmp/rives",
142 "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500143#else
144 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500145 static DEFINE_string(skps, "skps", "Directory to read skps from.");
146 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400147 static DEFINE_string(rives, "rives", "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500148#endif
149
Mike Kleinc6142d82019-03-25 10:54:59 -0500150static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
151
152static DEFINE_int_2(threads, j, -1,
153 "Run threadsafe tests on a threadpool with this many extra threads, "
154 "defaulting to one extra thread per core.");
155
Jim Van Verth7b558182019-11-14 16:47:01 -0500156static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
157
Chris Daltonc8877332020-01-06 09:48:30 -0700158static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Reed862818b2020-03-21 15:07:13 -0400159static DEFINE_bool(skvm, false, "Try to use skvm blitters for raster.");
Mike Klein1e0884d2020-04-28 15:04:16 -0500160static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500161static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500162
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400163#ifndef SK_GL
164static_assert(false, "viewer requires GL backend for raster.")
165#endif
166
Brian Salomon194db172017-08-17 14:37:06 -0400167const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700168 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400169#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
170 "ANGLE",
171#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400172#ifdef SK_DAWN
173 "Dawn",
174#endif
jvanverth063ece72016-06-17 09:29:14 -0700175#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700176 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700177#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400178#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500179 "Metal",
180#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400181#ifdef SK_DIRECT3D
182 "Direct3D",
183#endif
csmartdalton578f0642017-02-24 16:04:47 -0700184 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700185};
186
bsalomon6c471f72016-07-26 12:56:32 -0700187static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400188#ifdef SK_DAWN
189 if (0 == strcmp(str, "dawn")) {
190 return sk_app::Window::kDawn_BackendType;
191 } else
192#endif
bsalomon6c471f72016-07-26 12:56:32 -0700193#ifdef SK_VULKAN
194 if (0 == strcmp(str, "vk")) {
195 return sk_app::Window::kVulkan_BackendType;
196 } else
197#endif
Brian Salomon194db172017-08-17 14:37:06 -0400198#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
199 if (0 == strcmp(str, "angle")) {
200 return sk_app::Window::kANGLE_BackendType;
201 } else
202#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400203#ifdef SK_METAL
204 if (0 == strcmp(str, "mtl")) {
205 return sk_app::Window::kMetal_BackendType;
206 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500207#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400208#ifdef SK_DIRECT3D
209 if (0 == strcmp(str, "d3d")) {
210 return sk_app::Window::kDirect3D_BackendType;
211 } else
212#endif
213
bsalomon6c471f72016-07-26 12:56:32 -0700214 if (0 == strcmp(str, "gl")) {
215 return sk_app::Window::kNativeGL_BackendType;
216 } else if (0 == strcmp(str, "sw")) {
217 return sk_app::Window::kRaster_BackendType;
218 } else {
219 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
220 return sk_app::Window::kRaster_BackendType;
221 }
222}
223
Brian Osmana109e392017-02-24 09:49:14 -0500224static SkColorSpacePrimaries gSrgbPrimaries = {
225 0.64f, 0.33f,
226 0.30f, 0.60f,
227 0.15f, 0.06f,
228 0.3127f, 0.3290f };
229
230static SkColorSpacePrimaries gAdobePrimaries = {
231 0.64f, 0.33f,
232 0.21f, 0.71f,
233 0.15f, 0.06f,
234 0.3127f, 0.3290f };
235
236static SkColorSpacePrimaries gP3Primaries = {
237 0.680f, 0.320f,
238 0.265f, 0.690f,
239 0.150f, 0.060f,
240 0.3127f, 0.3290f };
241
242static SkColorSpacePrimaries gRec2020Primaries = {
243 0.708f, 0.292f,
244 0.170f, 0.797f,
245 0.131f, 0.046f,
246 0.3127f, 0.3290f };
247
248struct NamedPrimaries {
249 const char* fName;
250 SkColorSpacePrimaries* fPrimaries;
251} gNamedPrimaries[] = {
252 { "sRGB", &gSrgbPrimaries },
253 { "AdobeRGB", &gAdobePrimaries },
254 { "P3", &gP3Primaries },
255 { "Rec. 2020", &gRec2020Primaries },
256};
257
258static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
259 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
260}
261
Brian Osman70d2f432017-11-08 09:54:10 -0500262static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
263 // In raster mode, we still use GL for the window.
264 // This lets us render the GUI faster (and correct).
265 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
266}
267
Jim Van Verth74826c82019-03-01 14:37:30 -0500268class NullSlide : public Slide {
269 SkISize getDimensions() const override {
270 return SkISize::Make(640, 480);
271 }
272
273 void draw(SkCanvas* canvas) override {
274 canvas->clear(0xffff11ff);
275 }
276};
277
John Stiles31964fd2020-05-05 16:05:47 -0400278static const char kName[] = "name";
279static const char kValue[] = "value";
280static const char kOptions[] = "options";
281static const char kSlideStateName[] = "Slide";
282static const char kBackendStateName[] = "Backend";
283static const char kMSAAStateName[] = "MSAA";
284static const char kPathRendererStateName[] = "Path renderer";
285static const char kSoftkeyStateName[] = "Softkey";
286static const char kSoftkeyHint[] = "Please select a softkey";
287static const char kON[] = "ON";
288static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700289
Mike Reed862818b2020-03-21 15:07:13 -0400290extern bool gUseSkVMBlitter;
Mike Klein1e0884d2020-04-28 15:04:16 -0500291extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400292
jvanverth34524262016-05-04 13:49:13 -0700293Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500294 : fCurrentSlide(-1)
295 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500296 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400297 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500298 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500299 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500300 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500301 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400302 , fZoomWindowFixed(false)
303 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500304 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400305 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700306 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500307 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500308 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500309 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500310 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700311 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400312 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400313 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400314 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500315 , fTiled(false)
316 , fDrawTileBoundaries(false)
317 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400318 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700319{
Greg Daniel285db442016-10-14 09:12:53 -0400320 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700321
Chris Dalton37ae4b02019-12-28 14:51:11 -0700322 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600323 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500324 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500325 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600326 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600327 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500328 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700329
jvanverth2bb3b6d2016-04-08 07:24:09 -0700330 SkDebugf("Command line arguments: ");
331 for (int i = 1; i < argc; ++i) {
332 SkDebugf("%s ", argv[i]);
333 }
334 SkDebugf("\n");
335
Mike Klein88544fb2019-03-20 10:50:33 -0500336 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500337#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400338 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500339#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700340
Mike Reed862818b2020-03-21 15:07:13 -0400341 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein1e0884d2020-04-28 15:04:16 -0500342 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400343
Mike Klein19cc0f62019-03-22 15:30:07 -0500344 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500345
Brian Osmanbc8150f2017-07-24 11:38:01 -0400346 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400347 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400348
bsalomon6c471f72016-07-26 12:56:32 -0700349 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700350 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700351
csmartdalton578f0642017-02-24 16:04:47 -0700352 DisplayParams displayParams;
353 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700354 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400355 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400356 displayParams.fGrContextOptions.fShaderCacheStrategy =
357 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400358 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
359 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700360 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500361 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700362
Brian Osman56a24812017-12-19 11:15:16 -0500363 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500364 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500365 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
366 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
367 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
368
jvanverth9f372462016-04-06 06:08:59 -0700369 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700370 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500371 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500372 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500373 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700374
brianosman622c8d52016-05-10 06:50:49 -0700375 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500376 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
377 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
378 fWindow->inval();
379 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500380 // Command to jump directly to the slide picker and give it focus
381 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
382 this->fShowImGuiDebugWindow = true;
383 this->fShowSlidePicker = true;
384 fWindow->inval();
385 });
386 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400387 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500388 this->fShowImGuiDebugWindow = true;
389 this->fShowSlidePicker = true;
390 fWindow->inval();
391 });
Brian Osman79086b92017-02-10 13:36:16 -0500392 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
393 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
394 fWindow->inval();
395 });
Brian Osmanf6877092017-02-13 09:39:57 -0500396 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
397 this->fShowZoomWindow = !this->fShowZoomWindow;
398 fWindow->inval();
399 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400400 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
401 this->fZoomWindowFixed = !this->fZoomWindowFixed;
402 fWindow->inval();
403 });
Greg Danield0794cc2019-03-27 16:23:26 -0400404 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
405 DisplayParams params = fWindow->getRequestedDisplayParams();
406 params.fDisableVsync = !params.fDisableVsync;
407 fWindow->setRequestedDisplayParams(params);
408 this->updateTitle();
409 fWindow->inval();
410 });
Mike Reedf702ed42019-07-22 17:00:49 -0400411 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
412 fRefresh = !fRefresh;
413 fWindow->inval();
414 });
brianosman622c8d52016-05-10 06:50:49 -0700415 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500416 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700417 fWindow->inval();
418 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400419 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500420 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400421 this->updateTitle();
422 fWindow->inval();
423 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500424 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500425 switch (fColorMode) {
426 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500427 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500428 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500429 case ColorMode::kColorManaged8888:
430 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500431 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500432 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400433 this->setColorMode(ColorMode::kColorManagedF16Norm);
434 break;
435 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500436 this->setColorMode(ColorMode::kLegacy);
437 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500438 }
brianosman622c8d52016-05-10 06:50:49 -0700439 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700440 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
441 DisplayParams params = fWindow->getRequestedDisplayParams();
442 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
443 fWindow->setRequestedDisplayParams(params);
444 fWindow->inval();
445 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400446 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500447 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700448 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400449 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500450 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700451 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400452 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700453 this->changeZoomLevel(1.f / 32.f);
454 fWindow->inval();
455 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400456 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700457 this->changeZoomLevel(-1.f / 32.f);
458 fWindow->inval();
459 });
jvanverthaf236b52016-05-20 06:01:06 -0700460 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400461 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
462 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500463 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400464#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
465 if (newBackend == sk_app::Window::kVulkan_BackendType) {
466 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
467 sk_app::Window::kBackendTypeCount);
468 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
469 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500470 }
471#endif
Brian Osman621491e2017-02-28 15:45:01 -0500472 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700473 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500474 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
475 fSaveToSKP = true;
476 fWindow->inval();
477 });
Mike Reed376d8122019-03-14 11:39:02 -0400478 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
479 fShowSlideDimensions = !fShowSlideDimensions;
480 fWindow->inval();
481 });
Ben Wagner37c54032018-04-13 14:30:23 -0400482 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
483 DisplayParams params = fWindow->getRequestedDisplayParams();
484 uint32_t flags = params.fSurfaceProps.flags();
485 if (!fPixelGeometryOverrides) {
486 fPixelGeometryOverrides = true;
487 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
488 } else {
489 switch (params.fSurfaceProps.pixelGeometry()) {
490 case kUnknown_SkPixelGeometry:
491 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
492 break;
493 case kRGB_H_SkPixelGeometry:
494 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
495 break;
496 case kBGR_H_SkPixelGeometry:
497 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
498 break;
499 case kRGB_V_SkPixelGeometry:
500 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
501 break;
502 case kBGR_V_SkPixelGeometry:
503 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
504 fPixelGeometryOverrides = false;
505 break;
506 }
507 }
508 fWindow->setRequestedDisplayParams(params);
509 this->updateTitle();
510 fWindow->inval();
511 });
Ben Wagner9613e452019-01-23 10:34:59 -0500512 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500513 if (!fFontOverrides.fHinting) {
514 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400515 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500516 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500517 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400518 case SkFontHinting::kNone:
519 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500520 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400521 case SkFontHinting::kSlight:
522 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500523 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400524 case SkFontHinting::kNormal:
525 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500526 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400527 case SkFontHinting::kFull:
528 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500529 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500530 break;
531 }
532 }
533 this->updateTitle();
534 fWindow->inval();
535 });
536 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500537 if (!fPaintOverrides.fAntiAlias) {
538 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
539 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500540 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500541 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500542 } else {
543 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500544 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500545 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500546 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400547 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500548 break;
549 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500550 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500551 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400552 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500553 break;
554 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500555 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400556 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500557 break;
558 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500559 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
560 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500561 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
562 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500563 break;
564 }
565 }
566 this->updateTitle();
567 fWindow->inval();
568 });
Ben Wagner37c54032018-04-13 14:30:23 -0400569 fCommands.addCommand('D', "Modes", "DFT", [this]() {
570 DisplayParams params = fWindow->getRequestedDisplayParams();
571 uint32_t flags = params.fSurfaceProps.flags();
572 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
573 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
574 fWindow->setRequestedDisplayParams(params);
575 this->updateTitle();
576 fWindow->inval();
577 });
Ben Wagner9613e452019-01-23 10:34:59 -0500578 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500579 if (!fFontOverrides.fEdging) {
580 fFontOverrides.fEdging = true;
581 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500582 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500583 switch (fFont.getEdging()) {
584 case SkFont::Edging::kAlias:
585 fFont.setEdging(SkFont::Edging::kAntiAlias);
586 break;
587 case SkFont::Edging::kAntiAlias:
588 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
589 break;
590 case SkFont::Edging::kSubpixelAntiAlias:
591 fFont.setEdging(SkFont::Edging::kAlias);
592 fFontOverrides.fEdging = false;
593 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500594 }
595 }
596 this->updateTitle();
597 fWindow->inval();
598 });
Ben Wagner9613e452019-01-23 10:34:59 -0500599 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500600 if (!fFontOverrides.fSubpixel) {
601 fFontOverrides.fSubpixel = true;
602 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500603 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500604 if (!fFont.isSubpixel()) {
605 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500606 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500607 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500608 }
609 }
610 this->updateTitle();
611 fWindow->inval();
612 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400613 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
614 if (!fFontOverrides.fBaselineSnap) {
615 fFontOverrides.fBaselineSnap = true;
616 fFont.setBaselineSnap(false);
617 } else {
618 if (!fFont.isBaselineSnap()) {
619 fFont.setBaselineSnap(true);
620 } else {
621 fFontOverrides.fBaselineSnap = false;
622 }
623 }
624 this->updateTitle();
625 fWindow->inval();
626 });
Brian Osman805a7272018-05-02 15:40:20 -0400627 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
628 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
629 : kPerspective_Real;
630 this->updateTitle();
631 fWindow->inval();
632 });
633 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
634 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
635 : kPerspective_Off;
636 this->updateTitle();
637 fWindow->inval();
638 });
Brian Osman207d4102019-01-10 09:40:58 -0500639 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
640 fAnimTimer.togglePauseResume();
641 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400642 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
643 fZoomUI = !fZoomUI;
644 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
645 fWindow->inval();
646 });
Mike Reed59295352020-03-12 13:56:34 -0400647 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
648 fDrawViaSerialize = !fDrawViaSerialize;
649 this->updateTitle();
650 fWindow->inval();
651 });
Mike Reed862818b2020-03-21 15:07:13 -0400652 fCommands.addCommand('!', "SkVM", "Toggle SkVM", [this]() {
653 gUseSkVMBlitter = !gUseSkVMBlitter;
654 this->updateTitle();
655 fWindow->inval();
656 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500657
jvanverth2bb3b6d2016-04-08 07:24:09 -0700658 // set up slides
659 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500660 if (FLAGS_list) {
661 this->listNames();
662 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700663
Brian Osman9bb47cf2018-04-26 15:55:00 -0400664 fPerspectivePoints[0].set(0, 0);
665 fPerspectivePoints[1].set(1, 0);
666 fPerspectivePoints[2].set(0, 1);
667 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700668 fAnimTimer.run();
669
Hal Canaryc465d132017-12-08 10:21:31 -0500670 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500671 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400672 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500673 }
674 fImGuiGamutPaint.setColor(SK_ColorWHITE);
675 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
676
jongdeok.kim804f17e2019-02-26 14:39:23 +0900677 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500678 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700679}
680
jvanverth34524262016-05-04 13:49:13 -0700681void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400682 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
683 static const struct {
684 const char* fExtension;
685 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500686 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400687 const SlideFactory fFactory;
688 } gExternalSlidesInfo[] = {
689 { ".skp", "skp-dir", FLAGS_skps,
690 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
691 return sk_make_sp<SKPSlide>(name, path);}
692 },
693 { ".jpg", "jpg-dir", FLAGS_jpgs,
694 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
695 return sk_make_sp<ImageSlide>(name, path);}
696 },
Florin Malita87ccf332018-05-04 12:23:24 -0400697#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400698 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400699 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
700 return sk_make_sp<SkottieSlide>(name, path);}
701 },
Florin Malita87ccf332018-05-04 12:23:24 -0400702#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400703 #if defined(SK_ENABLE_SKRIVE)
704 { ".flr", "skrive-dir", FLAGS_rives,
705 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
706 return sk_make_sp<SkRiveSlide>(name, path);}
707 },
708 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400709#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400710 { ".svg", "svg-dir", FLAGS_svgs,
711 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
712 return sk_make_sp<SvgSlide>(name, path);}
713 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400714#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400715 };
jvanverthc265a922016-04-08 12:51:45 -0700716
Brian Salomon343553a2018-09-05 15:41:23 -0400717 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700718
Mike Klein88544fb2019-03-20 10:50:33 -0500719 const auto addSlide =
720 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
721 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
722 return;
723 }
liyuqian6f163d22016-06-13 12:26:45 -0700724
Mike Klein88544fb2019-03-20 10:50:33 -0500725 if (auto slide = fact(name, path)) {
726 dirSlides.push_back(slide);
727 fSlides.push_back(std::move(slide));
728 }
729 };
Florin Malita76a076b2018-02-15 18:40:48 -0500730
Florin Malita38792ce2018-05-08 10:36:18 -0400731 if (!FLAGS_file.isEmpty()) {
732 // single file mode
733 const SkString file(FLAGS_file[0]);
734
735 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
736 for (const auto& sinfo : gExternalSlidesInfo) {
737 if (file.endsWith(sinfo.fExtension)) {
738 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
739 return;
740 }
741 }
742
743 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
744 } else {
745 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
746 }
747
748 return;
749 }
750
751 // Bisect slide.
752 if (!FLAGS_bisect.isEmpty()) {
753 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500754 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400755 if (FLAGS_bisect.count() >= 2) {
756 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
757 bisect->onChar(*ch);
758 }
759 }
760 fSlides.push_back(std::move(bisect));
761 }
762 }
763
764 // GMs
765 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400766 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400767 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500768 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400769 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400770 fSlides.push_back(std::move(slide));
771 }
Florin Malita38792ce2018-05-08 10:36:18 -0400772 }
773 // reverse gms
774 int numGMs = fSlides.count() - firstGM;
775 for (int i = 0; i < numGMs/2; ++i) {
776 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
777 }
778
779 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400780 for (const SampleFactory factory : SampleRegistry::Range()) {
781 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500782 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400783 fSlides.push_back(slide);
784 }
Florin Malita38792ce2018-05-08 10:36:18 -0400785 }
786
Brian Osman7c979f52019-02-12 13:27:51 -0500787 // Particle demo
788 {
789 // TODO: Convert this to a sample
790 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500791 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500792 fSlides.push_back(std::move(slide));
793 }
794 }
795
Brian Osmand927bd22019-12-18 11:23:12 -0500796 // Runtime shader editor
797 {
798 sk_sp<Slide> slide(new SkSLSlide());
799 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
800 fSlides.push_back(std::move(slide));
801 }
802 }
803
Florin Malita0ffa3222018-04-05 14:34:45 -0400804 for (const auto& info : gExternalSlidesInfo) {
805 for (const auto& flag : info.fFlags) {
806 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
807 // single file
808 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
809 } else {
810 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400811 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400812 SkTArray<SkString> sortedFilenames;
813 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400814 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400815 sortedFilenames.push_back(name);
816 }
817 if (sortedFilenames.count()) {
818 SkTQSort(sortedFilenames.begin(), sortedFilenames.end() - 1,
819 [](const SkString& a, const SkString& b) {
820 return strcmp(a.c_str(), b.c_str()) < 0;
821 });
822 }
823 for (const SkString& filename : sortedFilenames) {
824 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
825 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400826 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400827 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400828 if (!dirSlides.empty()) {
829 fSlides.push_back(
830 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
831 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500832 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400833 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400834 }
835 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500836
837 if (!fSlides.count()) {
838 sk_sp<Slide> slide(new NullSlide());
839 fSlides.push_back(std::move(slide));
840 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700841}
842
843
jvanverth34524262016-05-04 13:49:13 -0700844Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700845 fWindow->detach();
846 delete fWindow;
847}
848
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500849struct SkPaintTitleUpdater {
850 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
851 void append(const char* s) {
852 if (fCount == 0) {
853 fTitle->append(" {");
854 } else {
855 fTitle->append(", ");
856 }
857 fTitle->append(s);
858 ++fCount;
859 }
860 void done() {
861 if (fCount > 0) {
862 fTitle->append("}");
863 }
864 }
865 SkString* fTitle;
866 int fCount;
867};
868
brianosman05de2162016-05-06 13:28:57 -0700869void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700870 if (!fWindow) {
871 return;
872 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500873 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700874 return; // Surface hasn't been created yet.
875 }
876
jvanverth34524262016-05-04 13:49:13 -0700877 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700878 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700879
Mike Kleine5acd752019-03-22 09:57:16 -0500880 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400881 if (gSkForceAnalyticAA) {
882 title.append(" <FAAA>");
883 } else {
884 title.append(" <AAA>");
885 }
886 }
Mike Reed59295352020-03-12 13:56:34 -0400887 if (fDrawViaSerialize) {
888 title.append(" <serialize>");
889 }
Mike Reed862818b2020-03-21 15:07:13 -0400890 if (gUseSkVMBlitter) {
891 title.append(" <skvm>");
892 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400893
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500894 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500895 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
896 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400897 const char* on, const char* off)
898 {
Ben Wagner9613e452019-01-23 10:34:59 -0500899 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400900 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500901 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400902 };
903
Ben Wagner9613e452019-01-23 10:34:59 -0500904 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
905 const char* on, const char* off)
906 {
907 if (fFontOverrides.*flag) {
908 paintTitle.append((fFont.*isFlag)() ? on : off);
909 }
910 };
911
912 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
913 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500914 if (fPaintOverrides.fFilterQuality) {
915 switch (fPaint.getFilterQuality()) {
916 case kNone_SkFilterQuality:
917 paintTitle.append("NoFilter");
918 break;
919 case kLow_SkFilterQuality:
920 paintTitle.append("LowFilter");
921 break;
922 case kMedium_SkFilterQuality:
923 paintTitle.append("MediumFilter");
924 break;
925 case kHigh_SkFilterQuality:
926 paintTitle.append("HighFilter");
927 break;
928 }
929 }
Ben Wagner9613e452019-01-23 10:34:59 -0500930
931 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
932 "Force Autohint", "No Force Autohint");
933 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400934 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500935 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
936 "Linear Metrics", "Non-Linear Metrics");
937 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
938 "Bitmap Text", "No Bitmap Text");
939 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
940
941 if (fFontOverrides.fEdging) {
942 switch (fFont.getEdging()) {
943 case SkFont::Edging::kAlias:
944 paintTitle.append("Alias Text");
945 break;
946 case SkFont::Edging::kAntiAlias:
947 paintTitle.append("Antialias Text");
948 break;
949 case SkFont::Edging::kSubpixelAntiAlias:
950 paintTitle.append("Subpixel Antialias Text");
951 break;
952 }
953 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400954
Mike Reed3ae47332019-01-04 10:11:46 -0500955 if (fFontOverrides.fHinting) {
956 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400957 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500958 paintTitle.append("No Hinting");
959 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400960 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500961 paintTitle.append("Slight Hinting");
962 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400963 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500964 paintTitle.append("Normal Hinting");
965 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400966 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500967 paintTitle.append("Full Hinting");
968 break;
969 }
970 }
971 paintTitle.done();
972
Brian Osman92004802017-03-06 11:47:26 -0500973 switch (fColorMode) {
974 case ColorMode::kLegacy:
975 title.append(" Legacy 8888");
976 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500977 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500978 title.append(" ColorManaged 8888");
979 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500980 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500981 title.append(" ColorManaged F16");
982 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400983 case ColorMode::kColorManagedF16Norm:
984 title.append(" ColorManaged F16 Norm");
985 break;
Brian Osman92004802017-03-06 11:47:26 -0500986 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500987
Brian Osman92004802017-03-06 11:47:26 -0500988 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500989 int curPrimaries = -1;
990 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
991 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
992 curPrimaries = i;
993 break;
994 }
995 }
Brian Osman03115dc2018-11-26 13:55:19 -0500996 title.appendf(" %s Gamma %f",
997 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500998 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700999 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001000
Ben Wagner37c54032018-04-13 14:30:23 -04001001 const DisplayParams& params = fWindow->getRequestedDisplayParams();
1002 if (fPixelGeometryOverrides) {
1003 switch (params.fSurfaceProps.pixelGeometry()) {
1004 case kUnknown_SkPixelGeometry:
1005 title.append( " Flat");
1006 break;
1007 case kRGB_H_SkPixelGeometry:
1008 title.append( " RGB");
1009 break;
1010 case kBGR_H_SkPixelGeometry:
1011 title.append( " BGR");
1012 break;
1013 case kRGB_V_SkPixelGeometry:
1014 title.append( " RGBV");
1015 break;
1016 case kBGR_V_SkPixelGeometry:
1017 title.append( " BGRV");
1018 break;
1019 }
1020 }
1021
1022 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1023 title.append(" DFT");
1024 }
1025
csmartdalton578f0642017-02-24 16:04:47 -07001026 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001027 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001028 int msaa = fWindow->sampleCount();
1029 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001030 title.appendf(" MSAA: %i", msaa);
1031 }
1032 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001033
1034 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001035 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001036 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1037 }
1038
Brian Osman805a7272018-05-02 15:40:20 -04001039 if (kPerspective_Real == fPerspectiveMode) {
1040 title.append(" Perpsective (Real)");
1041 } else if (kPerspective_Fake == fPerspectiveMode) {
1042 title.append(" Perspective (Fake)");
1043 }
1044
brianosman05de2162016-05-06 13:28:57 -07001045 fWindow->setTitle(title.c_str());
1046}
1047
Florin Malitaab99c342018-01-16 16:23:03 -05001048int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001049
1050 if (!FLAGS_slide.isEmpty()) {
1051 int count = fSlides.count();
1052 for (int i = 0; i < count; i++) {
1053 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001054 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001055 }
1056 }
1057
1058 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1059 this->listNames();
1060 }
1061
Florin Malitaab99c342018-01-16 16:23:03 -05001062 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001063}
1064
Florin Malitaab99c342018-01-16 16:23:03 -05001065void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001066 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001067 for (const auto& slide : fSlides) {
1068 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001069 }
1070}
1071
Florin Malitaab99c342018-01-16 16:23:03 -05001072void Viewer::setCurrentSlide(int slide) {
1073 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001074
Florin Malitaab99c342018-01-16 16:23:03 -05001075 if (slide == fCurrentSlide) {
1076 return;
1077 }
1078
1079 if (fCurrentSlide >= 0) {
1080 fSlides[fCurrentSlide]->unload();
1081 }
1082
1083 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1084 SkIntToScalar(fWindow->height()));
1085 fCurrentSlide = slide;
1086 this->setupCurrentSlide();
1087}
1088
1089void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001090 if (fCurrentSlide >= 0) {
1091 // prepare dimensions for image slides
1092 fGesture.resetTouchState();
1093 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001094
Jim Van Verth0848fb02018-01-22 13:39:30 -05001095 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1096 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1097 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001098
Jim Van Verth0848fb02018-01-22 13:39:30 -05001099 // Start with a matrix that scales the slide to the available screen space
1100 if (fWindow->scaleContentToFit()) {
1101 if (windowRect.width() > 0 && windowRect.height() > 0) {
1102 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1103 }
liyuqiane46e4f02016-05-20 07:32:19 -07001104 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001105
1106 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001107 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001108
1109 this->updateTitle();
1110 this->updateUIState();
1111
1112 fStatsLayer.resetMeasurements();
1113
1114 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001115 }
jvanverthc265a922016-04-08 12:51:45 -07001116}
1117
Brian Osmanaba642c2020-02-06 12:52:25 -05001118#define MAX_ZOOM_LEVEL 8.0f
1119#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001120
jvanverth34524262016-05-04 13:49:13 -07001121void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001122 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001123 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001124 this->preTouchMatrixChanged();
1125}
Yuqian Li755778c2018-03-28 16:23:31 -04001126
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001127void Viewer::preTouchMatrixChanged() {
1128 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001129 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1130 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1131 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1132 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1133}
1134
Brian Osman805a7272018-05-02 15:40:20 -04001135SkMatrix Viewer::computePerspectiveMatrix() {
1136 SkScalar w = fWindow->width(), h = fWindow->height();
1137 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1138 SkPoint perspPts[4] = {
1139 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1140 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1141 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1142 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1143 };
1144 SkMatrix m;
1145 m.setPolyToPoly(orthoPts, perspPts, 4);
1146 return m;
1147}
1148
Yuqian Li755778c2018-03-28 16:23:31 -04001149SkMatrix Viewer::computePreTouchMatrix() {
1150 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001151
1152 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001153 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001154 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001155
1156 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1157 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001158
Brian Osman805a7272018-05-02 15:40:20 -04001159 if (kPerspective_Real == fPerspectiveMode) {
1160 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001161 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001162 }
1163
Yuqian Li755778c2018-03-28 16:23:31 -04001164 return m;
jvanverthc265a922016-04-08 12:51:45 -07001165}
1166
liyuqiand3cdbca2016-05-17 12:44:20 -07001167SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001168 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001169 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001170 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001171 return m;
jvanverthc265a922016-04-08 12:51:45 -07001172}
1173
Brian Osman621491e2017-02-28 15:45:01 -05001174void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001175 fPersistentCache.reset();
1176 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001177 fBackendType = backendType;
1178
1179 fWindow->detach();
1180
Brian Osman70d2f432017-11-08 09:54:10 -05001181#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001182 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1183 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001184 DisplayParams params = fWindow->getRequestedDisplayParams();
1185 delete fWindow;
1186 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001187
Brian Osman70d2f432017-11-08 09:54:10 -05001188 // re-register callbacks
1189 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001190 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001191 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001192 fWindow->pushLayer(&fImGuiLayer);
1193
Brian Osman70d2f432017-11-08 09:54:10 -05001194 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1195 // will still include our correct sample count. But the re-created fWindow will lose that
1196 // information. On Windows, we need to re-create the window when changing sample count,
1197 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1198 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1199 // as if we had never changed windows at all).
1200 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001201#endif
1202
Brian Osman70d2f432017-11-08 09:54:10 -05001203 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001204}
1205
Brian Osman92004802017-03-06 11:47:26 -05001206void Viewer::setColorMode(ColorMode colorMode) {
1207 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001208 this->updateTitle();
1209 fWindow->inval();
1210}
1211
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001212class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1213public:
Mike Reed3ae47332019-01-04 10:11:46 -05001214 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1215 SkFont* font, Viewer::SkFontFields* ffields)
1216 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001217 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001218 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1219 sk_sp<SkTextBlob>* cache) {
1220 bool blobWillChange = false;
1221 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001222 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1223 bool shouldDraw = this->filterFont(&filteredFont);
1224 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001225 blobWillChange = true;
1226 break;
1227 }
1228 }
1229 if (!blobWillChange) {
1230 return blob;
1231 }
1232
1233 SkTextBlobBuilder builder;
1234 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001235 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1236 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001237 if (!shouldDraw) {
1238 continue;
1239 }
1240
Mike Reed3ae47332019-01-04 10:11:46 -05001241 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001242
Ben Wagner41e40472018-09-24 13:01:54 -04001243 const SkTextBlobBuilder::RunBuffer& runBuffer
1244 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001245 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001246 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001247 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001248 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001249 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001250 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001251 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001252 it.glyphCount(), it.textSize(), SkString())
1253 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1254 uint32_t glyphCount = it.glyphCount();
1255 if (it.glyphs()) {
1256 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1257 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1258 }
1259 if (it.pos()) {
1260 size_t posSize = sizeof(decltype(*it.pos()));
1261 uint8_t positioning = it.positioning();
1262 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1263 }
1264 if (it.text()) {
1265 size_t textSize = sizeof(decltype(*it.text()));
1266 uint32_t textCount = it.textSize();
1267 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1268 }
1269 if (it.clusters()) {
1270 size_t clusterSize = sizeof(decltype(*it.clusters()));
1271 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1272 }
1273 }
1274 *cache = builder.make();
1275 return cache->get();
1276 }
1277 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1278 const SkPaint& paint) override {
1279 sk_sp<SkTextBlob> cache;
1280 this->SkPaintFilterCanvas::onDrawTextBlob(
1281 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1282 }
Mike Reed3ae47332019-01-04 10:11:46 -05001283 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001284 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001285 font->writable()->setSize(fFont->getSize());
1286 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001287 if (fFontOverrides->fScaleX) {
1288 font->writable()->setScaleX(fFont->getScaleX());
1289 }
1290 if (fFontOverrides->fSkewX) {
1291 font->writable()->setSkewX(fFont->getSkewX());
1292 }
Mike Reed3ae47332019-01-04 10:11:46 -05001293 if (fFontOverrides->fHinting) {
1294 font->writable()->setHinting(fFont->getHinting());
1295 }
Ben Wagner9613e452019-01-23 10:34:59 -05001296 if (fFontOverrides->fEdging) {
1297 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001298 }
Ben Wagner9613e452019-01-23 10:34:59 -05001299 if (fFontOverrides->fEmbolden) {
1300 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001301 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001302 if (fFontOverrides->fBaselineSnap) {
1303 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1304 }
Ben Wagner9613e452019-01-23 10:34:59 -05001305 if (fFontOverrides->fLinearMetrics) {
1306 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001307 }
Ben Wagner9613e452019-01-23 10:34:59 -05001308 if (fFontOverrides->fSubpixel) {
1309 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001310 }
Ben Wagner9613e452019-01-23 10:34:59 -05001311 if (fFontOverrides->fEmbeddedBitmaps) {
1312 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001313 }
Ben Wagner9613e452019-01-23 10:34:59 -05001314 if (fFontOverrides->fForceAutoHinting) {
1315 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001316 }
Ben Wagner9613e452019-01-23 10:34:59 -05001317
Mike Reed3ae47332019-01-04 10:11:46 -05001318 return true;
1319 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001320 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001321 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001322 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001323 }
Ben Wagner9613e452019-01-23 10:34:59 -05001324 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001325 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001326 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001327 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001328 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001329 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001330 return true;
1331 }
1332 SkPaint* fPaint;
1333 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001334 SkFont* fFont;
1335 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001336};
1337
Robert Phillips9882dae2019-03-04 11:00:10 -05001338void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001339 if (fCurrentSlide < 0) {
1340 return;
1341 }
1342
Robert Phillips9882dae2019-03-04 11:00:10 -05001343 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001344
Brian Osmanf750fbc2017-02-08 10:47:28 -05001345 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001346 SkSurface* slideSurface = surface;
1347 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001348 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001349
Brian Osmane0d4fba2017-03-15 10:24:55 -04001350 // 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 -05001351 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001352 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001353 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001354 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001355 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001356 }
1357
Brian Osman3ac99cf2017-12-01 11:23:53 -05001358 if (fSaveToSKP) {
1359 SkPictureRecorder recorder;
1360 SkCanvas* recorderCanvas = recorder.beginRecording(
1361 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001362 fSlides[fCurrentSlide]->draw(recorderCanvas);
1363 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1364 SkFILEWStream stream("sample_app.skp");
1365 picture->serialize(&stream);
1366 fSaveToSKP = false;
1367 }
1368
Brian Osmane9ed0f02018-11-26 14:50:05 -05001369 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001370 SkColorType colorType;
1371 switch (fColorMode) {
1372 case ColorMode::kLegacy:
1373 case ColorMode::kColorManaged8888:
1374 colorType = kN32_SkColorType;
1375 break;
1376 case ColorMode::kColorManagedF16:
1377 colorType = kRGBA_F16_SkColorType;
1378 break;
1379 case ColorMode::kColorManagedF16Norm:
1380 colorType = kRGBA_F16Norm_SkColorType;
1381 break;
1382 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001383
1384 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001385 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1386 slideCanvas->getProps(&props);
1387
Brian Osmane9ed0f02018-11-26 14:50:05 -05001388 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1389 return Window::kRaster_BackendType == this->fBackendType
1390 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001391 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001392 };
1393
Brian Osman03115dc2018-11-26 13:55:19 -05001394 // We need to render offscreen if we're...
1395 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1396 // ... in any raster mode, because the window surface is actually GL
1397 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001398 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001399 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001400 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001401 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001402 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001403 colorSpace != nullptr ||
1404 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001405
Brian Osmane9ed0f02018-11-26 14:50:05 -05001406 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001407 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001408 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001409 }
1410
Mike Reed59295352020-03-12 13:56:34 -04001411 SkPictureRecorder recorder;
1412 SkCanvas* recorderRestoreCanvas = nullptr;
1413 if (fDrawViaSerialize) {
1414 recorderRestoreCanvas = slideCanvas;
1415 slideCanvas = recorder.beginRecording(
1416 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1417 }
1418
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001419 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001420 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001421 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001422 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001423 if (fTiled) {
1424 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1425 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001426 for (int y = 0; y < fWindow->height(); y += tileH) {
1427 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001428 SkAutoCanvasRestore acr(slideCanvas, true);
1429 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1430 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001431 }
1432 }
1433
1434 // Draw borders between tiles
1435 if (fDrawTileBoundaries) {
1436 SkPaint border;
1437 border.setColor(0x60FF00FF);
1438 border.setStyle(SkPaint::kStroke_Style);
1439 for (int y = 0; y < fWindow->height(); y += tileH) {
1440 for (int x = 0; x < fWindow->width(); x += tileW) {
1441 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1442 }
1443 }
1444 }
1445 } else {
1446 slideCanvas->concat(this->computeMatrix());
1447 if (kPerspective_Real == fPerspectiveMode) {
1448 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1449 }
Mike Reed3ae47332019-01-04 10:11:46 -05001450 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001451 fSlides[fCurrentSlide]->draw(&filterCanvas);
1452 }
Brian Osman56a24812017-12-19 11:15:16 -05001453 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001454 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001455
Mike Reed59295352020-03-12 13:56:34 -04001456 if (recorderRestoreCanvas) {
1457 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1458 auto data = picture->serialize();
1459 slideCanvas = recorderRestoreCanvas;
1460 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1461 }
1462
Brian Osman1df161a2017-02-09 12:10:20 -05001463 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001464 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001465 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001466 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001467
1468 // If we rendered offscreen, snap an image and push the results to the window's canvas
1469 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001470 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001471
Robert Phillips9882dae2019-03-04 11:00:10 -05001472 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001473 SkPaint paint;
1474 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001475 int prePerspectiveCount = canvas->save();
1476 if (kPerspective_Fake == fPerspectiveMode) {
1477 paint.setFilterQuality(kHigh_SkFilterQuality);
1478 canvas->clear(SK_ColorWHITE);
1479 canvas->concat(this->computePerspectiveMatrix());
1480 }
Brian Osman03115dc2018-11-26 13:55:19 -05001481 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001482 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001483 }
Mike Reed376d8122019-03-14 11:39:02 -04001484
1485 if (fShowSlideDimensions) {
1486 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1487 SkPaint paint;
1488 paint.setColor(0x40FFFF00);
1489 surface->getCanvas()->drawRect(r, paint);
1490 }
liyuqian6f163d22016-06-13 12:26:45 -07001491}
1492
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001493void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001494 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001495 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001496}
Jim Van Verth6f449692017-02-14 15:16:46 -05001497
Robert Phillips9882dae2019-03-04 11:00:10 -05001498void Viewer::onPaint(SkSurface* surface) {
1499 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001500
Robert Phillips9882dae2019-03-04 11:00:10 -05001501 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001502
Brian Osmand67e5182017-12-08 16:46:09 -05001503 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001504
1505 if (GrContext* ctx = fWindow->getGrContext()) {
1506 // Clean out cache items that haven't been used in more than 10 seconds.
1507 ctx->performDeferredCleanup(std::chrono::seconds(10));
1508 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001509}
1510
Ben Wagnera1915972018-08-09 15:06:19 -04001511void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001512 if (fCurrentSlide >= 0) {
1513 fSlides[fCurrentSlide]->resize(width, height);
1514 }
Ben Wagnera1915972018-08-09 15:06:19 -04001515}
1516
Florin Malitacefc1b92018-02-19 21:43:47 -05001517SkPoint Viewer::mapEvent(float x, float y) {
1518 const auto m = this->computeMatrix();
1519 SkMatrix inv;
1520
1521 SkAssertResult(m.invert(&inv));
1522
1523 return inv.mapXY(x, y);
1524}
1525
Hal Canaryb1f411a2019-08-29 10:39:22 -04001526bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001527 if (GestureDevice::kMouse == fGestureDevice) {
1528 return false;
1529 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001530
1531 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001532 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001533 fWindow->inval();
1534 return true;
1535 }
1536
liyuqiand3cdbca2016-05-17 12:44:20 -07001537 void* castedOwner = reinterpret_cast<void*>(owner);
1538 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001539 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001540 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001541#if defined(SK_BUILD_FOR_IOS)
1542 // TODO: move IOS swipe detection higher up into the platform code
1543 SkPoint dir;
1544 if (fGesture.isFling(&dir)) {
1545 // swiping left or right
1546 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1547 if (dir.fX < 0) {
1548 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1549 fCurrentSlide + 1 : 0);
1550 } else {
1551 this->setCurrentSlide(fCurrentSlide > 0 ?
1552 fCurrentSlide - 1 : fSlides.count() - 1);
1553 }
1554 }
1555 fGesture.reset();
1556 }
1557#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001558 break;
1559 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001560 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001561 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001562 break;
1563 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001564 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001565 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001566 break;
1567 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001568 default: {
1569 // kLeft and kRight are only for swipes
1570 SkASSERT(false);
1571 break;
1572 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001573 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001574 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001575 fWindow->inval();
1576 return true;
1577}
1578
Hal Canaryb1f411a2019-08-29 10:39:22 -04001579bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001580 if (GestureDevice::kTouch == fGestureDevice) {
1581 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001582 }
Brian Osman16c81a12017-12-20 11:58:34 -05001583
Florin Malitacefc1b92018-02-19 21:43:47 -05001584 const auto slidePt = this->mapEvent(x, y);
1585 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1586 fWindow->inval();
1587 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001588 }
1589
1590 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001591 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001592 fGesture.touchEnd(nullptr);
1593 break;
1594 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001595 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001596 fGesture.touchBegin(nullptr, x, y);
1597 break;
1598 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001599 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001600 fGesture.touchMoved(nullptr, x, y);
1601 break;
1602 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001603 default: {
1604 SkASSERT(false); // shouldn't see kRight or kLeft here
1605 break;
1606 }
Brian Osman16c81a12017-12-20 11:58:34 -05001607 }
1608 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1609
Hal Canaryb1f411a2019-08-29 10:39:22 -04001610 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001611 fWindow->inval();
1612 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001613 return true;
1614}
1615
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001616bool Viewer::onFling(skui::InputState state) {
1617 if (skui::InputState::kRight == state) {
1618 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1619 return true;
1620 } else if (skui::InputState::kLeft == state) {
1621 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1622 return true;
1623 }
1624 return false;
1625}
1626
1627bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1628 switch (state) {
1629 case skui::InputState::kDown:
1630 fGesture.startZoom();
1631 return true;
1632 break;
1633 case skui::InputState::kMove:
1634 fGesture.updateZoom(scale, x, y, x, y);
1635 return true;
1636 break;
1637 case skui::InputState::kUp:
1638 fGesture.endZoom();
1639 return true;
1640 break;
1641 default:
1642 SkASSERT(false);
1643 break;
1644 }
1645
1646 return false;
1647}
1648
Brian Osmana109e392017-02-24 09:49:14 -05001649static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001650 // The gamut image covers a (0.8 x 0.9) shaped region
1651 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001652
1653 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1654 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1655 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001656 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1657 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1658 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001659
Brian Osman535c5e32019-02-09 16:32:58 -05001660 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1661 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1662 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1663 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1664 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001665}
1666
Ben Wagner3627d2e2018-06-26 14:23:20 -04001667static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001668 ImGui::DragCanvas dc(pt);
1669 dc.fillColor(IM_COL32(0, 0, 0, 128));
1670 dc.dragPoint(pt);
1671 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001672}
1673
Brian Osman9bb47cf2018-04-26 15:55:00 -04001674static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001675 ImGui::DragCanvas dc(pts);
1676 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001677
Brian Osman535c5e32019-02-09 16:32:58 -05001678 for (int i = 0; i < 4; ++i) {
1679 dc.dragPoint(pts + i);
1680 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001681
Brian Osman535c5e32019-02-09 16:32:58 -05001682 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1683 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1684 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1685 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001686
Brian Osman535c5e32019-02-09 16:32:58 -05001687 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001688}
1689
Brian Osmand67e5182017-12-08 16:46:09 -05001690void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001691 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1692 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001693 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001694 }
1695
1696 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001697 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1698 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001699 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001700 DisplayParams params = fWindow->getRequestedDisplayParams();
1701 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001702 const GrContext* ctx = fWindow->getGrContext();
1703
Brian Osmana109e392017-02-24 09:49:14 -05001704 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1705 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001706 if (ImGui::CollapsingHeader("Backend")) {
1707 int newBackend = static_cast<int>(fBackendType);
1708 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1709 ImGui::SameLine();
1710 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001711#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1712 ImGui::SameLine();
1713 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1714#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001715#if defined(SK_DAWN)
1716 ImGui::SameLine();
1717 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1718#endif
Brian Osman621491e2017-02-28 15:45:01 -05001719#if defined(SK_VULKAN)
1720 ImGui::SameLine();
1721 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1722#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001723#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001724 ImGui::SameLine();
1725 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1726#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001727#if defined(SK_DIRECT3D)
1728 ImGui::SameLine();
1729 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1730#endif
Brian Osman621491e2017-02-28 15:45:01 -05001731 if (newBackend != fBackendType) {
1732 fDeferredActions.push_back([=]() {
1733 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1734 });
1735 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001736
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001737 bool* wire = &params.fGrContextOptions.fWireframeMode;
1738 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1739 paramsChanged = true;
1740 }
Brian Salomon99a33902017-03-07 15:16:34 -05001741
Brian Osman28b12522017-03-08 17:10:24 -05001742 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001743 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001744 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001745 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1746 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1747 1;
1748
1749 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1750 if (maxMSAA >= 4) {
1751 ImGui::Text("MSAA: ");
1752
1753 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1754 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1755 // don't show it in the list.
1756 if (curMSAA == 2) {
1757 continue;
1758 }
1759 ImGui::SameLine();
1760 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1761 &sampleCount, curMSAA);
1762 }
1763 }
Brian Osman28b12522017-03-08 17:10:24 -05001764
1765 if (sampleCount != params.fMSAASampleCount) {
1766 params.fMSAASampleCount = sampleCount;
1767 paramsChanged = true;
1768 }
1769 }
1770
Ben Wagner37c54032018-04-13 14:30:23 -04001771 int pixelGeometryIdx = 0;
1772 if (fPixelGeometryOverrides) {
1773 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1774 }
1775 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1776 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1777 {
1778 uint32_t flags = params.fSurfaceProps.flags();
1779 if (pixelGeometryIdx == 0) {
1780 fPixelGeometryOverrides = false;
1781 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1782 } else {
1783 fPixelGeometryOverrides = true;
1784 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1785 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1786 }
1787 paramsChanged = true;
1788 }
1789
1790 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1791 if (ImGui::Checkbox("DFT", &useDFT)) {
1792 uint32_t flags = params.fSurfaceProps.flags();
1793 if (useDFT) {
1794 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1795 } else {
1796 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1797 }
1798 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1799 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1800 paramsChanged = true;
1801 }
1802
Brian Osman8a9de3d2017-03-01 14:59:05 -05001803 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001804 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001805 auto prButton = [&](GpuPathRenderers x) {
1806 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001807 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1808 params.fGrContextOptions.fGpuPathRenderers = x;
1809 paramsChanged = true;
1810 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001811 }
1812 };
1813
1814 if (!ctx) {
1815 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001816 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001817 const auto* caps = ctx->priv().caps();
1818 prButton(GpuPathRenderers::kDefault);
1819 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001820 if (caps->shaderCaps()->tessellationSupport()) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001821 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001822 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001823 if (caps->shaderCaps()->pathRenderingSupport()) {
1824 prButton(GpuPathRenderers::kStencilAndCover);
1825 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001826 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001827 if (1 == fWindow->sampleCount()) {
1828 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1829 prButton(GpuPathRenderers::kCoverageCounting);
1830 }
1831 prButton(GpuPathRenderers::kSmall);
1832 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001833 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001834 prButton(GpuPathRenderers::kNone);
1835 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001836 ImGui::TreePop();
1837 }
Brian Osman621491e2017-02-28 15:45:01 -05001838 }
1839
Ben Wagner964571d2019-03-08 12:35:06 -05001840 if (ImGui::CollapsingHeader("Tiling")) {
1841 ImGui::Checkbox("Enable", &fTiled);
1842 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1843 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1844 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1845 }
1846
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001847 if (ImGui::CollapsingHeader("Transform")) {
1848 float zoom = fZoomLevel;
1849 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1850 fZoomLevel = zoom;
1851 this->preTouchMatrixChanged();
1852 paramsChanged = true;
1853 }
1854 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001855 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001856 fRotation = deg;
1857 this->preTouchMatrixChanged();
1858 paramsChanged = true;
1859 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001860 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1861 if (ImGui_DragLocation(&fOffset)) {
1862 this->preTouchMatrixChanged();
1863 paramsChanged = true;
1864 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001865 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1866 this->preTouchMatrixChanged();
1867 paramsChanged = true;
1868 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001869 }
Brian Osman805a7272018-05-02 15:40:20 -04001870 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1871 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1872 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001873 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001874 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001875 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001876 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001877 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001878 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001879 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001880 }
1881
Ben Wagnera580fb32018-04-17 11:16:32 -04001882 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001883 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001884 if (fPaintOverrides.fAntiAlias) {
1885 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001886 }
1887 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001888 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001889 {
1890 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1891 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001892 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001893 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1894 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001895 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001896 fPaintOverrides.fAntiAlias = true;
1897 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001898 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001899 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001900 case SkPaintFields::AntiAliasState::Alias:
1901 break;
1902 case SkPaintFields::AntiAliasState::Normal:
1903 break;
1904 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1905 gSkUseAnalyticAA = true;
1906 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001907 break;
1908 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1909 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001910 break;
1911 }
1912 }
1913 paramsChanged = true;
1914 }
1915
Ben Wagner99a78dc2018-05-09 18:23:51 -04001916 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001917 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001918 bool (SkPaint::* isFlag)() const,
1919 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001920 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001921 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001922 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001923 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001924 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001925 if (ImGui::Combo(label, &itemIndex, items)) {
1926 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001927 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001928 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001929 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001930 (fPaint.*setFlag)(itemIndex == 2);
1931 }
1932 paramsChanged = true;
1933 }
1934 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001935
Ben Wagner99a78dc2018-05-09 18:23:51 -04001936 paintFlag("Dither",
1937 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001938 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001939 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001940
1941 int filterQualityIdx = 0;
1942 if (fPaintOverrides.fFilterQuality) {
1943 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1944 }
1945 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1946 "Default\0None\0Low\0Medium\0High\0\0"))
1947 {
1948 if (filterQualityIdx == 0) {
1949 fPaintOverrides.fFilterQuality = false;
1950 fPaint.setFilterQuality(kNone_SkFilterQuality);
1951 } else {
1952 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1953 fPaintOverrides.fFilterQuality = true;
1954 }
1955 paramsChanged = true;
1956 }
Ben Wagner9613e452019-01-23 10:34:59 -05001957 }
Hal Canary02738a82019-01-21 18:51:32 +00001958
Ben Wagner9613e452019-01-23 10:34:59 -05001959 if (ImGui::CollapsingHeader("Font")) {
1960 int hintingIdx = 0;
1961 if (fFontOverrides.fHinting) {
1962 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1963 }
1964 if (ImGui::Combo("Hinting", &hintingIdx,
1965 "Default\0None\0Slight\0Normal\0Full\0\0"))
1966 {
1967 if (hintingIdx == 0) {
1968 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001969 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001970 } else {
1971 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1972 fFontOverrides.fHinting = true;
1973 }
1974 paramsChanged = true;
1975 }
Hal Canary02738a82019-01-21 18:51:32 +00001976
Ben Wagner9613e452019-01-23 10:34:59 -05001977 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1978 bool SkFontFields::* flag,
1979 bool (SkFont::* isFlag)() const,
1980 void (SkFont::* setFlag)(bool) )
1981 {
1982 int itemIndex = 0;
1983 if (fFontOverrides.*flag) {
1984 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1985 }
1986 if (ImGui::Combo(label, &itemIndex, items)) {
1987 if (itemIndex == 0) {
1988 fFontOverrides.*flag = false;
1989 } else {
1990 fFontOverrides.*flag = true;
1991 (fFont.*setFlag)(itemIndex == 2);
1992 }
1993 paramsChanged = true;
1994 }
1995 };
Hal Canary02738a82019-01-21 18:51:32 +00001996
Ben Wagner9613e452019-01-23 10:34:59 -05001997 fontFlag("Fake Bold Glyphs",
1998 "Default\0No Fake Bold\0Fake Bold\0\0",
1999 &SkFontFields::fEmbolden,
2000 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002001
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002002 fontFlag("Baseline Snapping",
2003 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2004 &SkFontFields::fBaselineSnap,
2005 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2006
Ben Wagner9613e452019-01-23 10:34:59 -05002007 fontFlag("Linear Text",
2008 "Default\0No Linear Text\0Linear Text\0\0",
2009 &SkFontFields::fLinearMetrics,
2010 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002011
Ben Wagner9613e452019-01-23 10:34:59 -05002012 fontFlag("Subpixel Position Glyphs",
2013 "Default\0Pixel Text\0Subpixel Text\0\0",
2014 &SkFontFields::fSubpixel,
2015 &SkFont::isSubpixel, &SkFont::setSubpixel);
2016
2017 fontFlag("Embedded Bitmap Text",
2018 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2019 &SkFontFields::fEmbeddedBitmaps,
2020 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2021
2022 fontFlag("Force Auto-Hinting",
2023 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2024 &SkFontFields::fForceAutoHinting,
2025 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2026
2027 int edgingIdx = 0;
2028 if (fFontOverrides.fEdging) {
2029 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2030 }
2031 if (ImGui::Combo("Edging", &edgingIdx,
2032 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2033 {
2034 if (edgingIdx == 0) {
2035 fFontOverrides.fEdging = false;
2036 fFont.setEdging(SkFont::Edging::kAlias);
2037 } else {
2038 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2039 fFontOverrides.fEdging = true;
2040 }
2041 paramsChanged = true;
2042 }
2043
Ben Wagner15a8d572019-03-21 13:35:44 -04002044 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2045 if (fFontOverrides.fSize) {
2046 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002047 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002048 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002049 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002050 fFontOverrides.fSizeRange[0],
2051 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002052 "%.6f", 2.0f))
2053 {
Mike Reed3ae47332019-01-04 10:11:46 -05002054 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002055 paramsChanged = true;
2056 }
2057 }
2058
2059 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2060 if (fFontOverrides.fScaleX) {
2061 float scaleX = fFont.getScaleX();
2062 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2063 fFont.setScaleX(scaleX);
2064 paramsChanged = true;
2065 }
2066 }
2067
2068 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2069 if (fFontOverrides.fSkewX) {
2070 float skewX = fFont.getSkewX();
2071 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2072 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002073 paramsChanged = true;
2074 }
2075 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002076 }
2077
Mike Reed81f60ec2018-05-15 10:09:52 -04002078 {
2079 SkMetaData controls;
2080 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2081 if (ImGui::CollapsingHeader("Current Slide")) {
2082 SkMetaData::Iter iter(controls);
2083 const char* name;
2084 SkMetaData::Type type;
2085 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002086 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002087 if (type == SkMetaData::kScalar_Type) {
2088 float val[3];
2089 SkASSERT(count == 3);
2090 controls.findScalars(name, &count, val);
2091 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2092 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002093 }
Ben Wagner110c7032019-03-22 17:03:59 -04002094 } else if (type == SkMetaData::kBool_Type) {
2095 bool val;
2096 SkASSERT(count == 1);
2097 controls.findBool(name, &val);
2098 if (ImGui::Checkbox(name, &val)) {
2099 controls.setBool(name, val);
2100 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002101 }
2102 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002103 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002104 }
2105 }
2106 }
2107
Ben Wagner7a3c6742018-04-23 10:01:07 -04002108 if (fShowSlidePicker) {
2109 ImGui::SetNextTreeNodeOpen(true);
2110 }
Brian Osman79086b92017-02-10 13:36:16 -05002111 if (ImGui::CollapsingHeader("Slide")) {
2112 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002113 static ImVector<const char*> filteredSlideNames;
2114 static ImVector<int> filteredSlideIndices;
2115
Brian Osmanfce09c52017-11-14 15:32:20 -05002116 if (fShowSlidePicker) {
2117 ImGui::SetKeyboardFocusHere();
2118 fShowSlidePicker = false;
2119 }
2120
Brian Osman79086b92017-02-10 13:36:16 -05002121 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002122 filteredSlideNames.clear();
2123 filteredSlideIndices.clear();
2124 int filteredIndex = 0;
2125 for (int i = 0; i < fSlides.count(); ++i) {
2126 const char* slideName = fSlides[i]->getName().c_str();
2127 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2128 if (i == fCurrentSlide) {
2129 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002130 }
Brian Osmanf479e422017-11-08 13:11:36 -05002131 filteredSlideNames.push_back(slideName);
2132 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002133 }
Brian Osman79086b92017-02-10 13:36:16 -05002134 }
Brian Osmanf479e422017-11-08 13:11:36 -05002135
Brian Osmanf479e422017-11-08 13:11:36 -05002136 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2137 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002138 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002139 }
2140 }
Brian Osmana109e392017-02-24 09:49:14 -05002141
2142 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002143 ColorMode newMode = fColorMode;
2144 auto cmButton = [&](ColorMode mode, const char* label) {
2145 if (ImGui::RadioButton(label, mode == fColorMode)) {
2146 newMode = mode;
2147 }
2148 };
2149
2150 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002151 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2152 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002153 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002154
2155 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002156 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002157 }
2158
2159 // Pick from common gamuts:
2160 int primariesIdx = 4; // Default: Custom
2161 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2162 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2163 primariesIdx = i;
2164 break;
2165 }
2166 }
2167
Brian Osman03115dc2018-11-26 13:55:19 -05002168 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002169 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002170
Brian Osmana109e392017-02-24 09:49:14 -05002171 if (ImGui::Combo("Primaries", &primariesIdx,
2172 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2173 if (primariesIdx >= 0 && primariesIdx <= 3) {
2174 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2175 }
2176 }
2177
2178 // Allow direct editing of gamut
2179 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2180 }
Brian Osman207d4102019-01-10 09:40:58 -05002181
2182 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002183 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002184 if (ImGui::Checkbox("Pause", &isPaused)) {
2185 fAnimTimer.togglePauseResume();
2186 }
Brian Osman707d2022019-01-10 11:27:34 -05002187
2188 float speed = fAnimTimer.getSpeed();
2189 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2190 fAnimTimer.setSpeed(speed);
2191 }
Brian Osman207d4102019-01-10 09:40:58 -05002192 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002193
Brian Osmanfd7657c2019-04-25 11:34:07 -04002194 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2195#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2196 || Window::kANGLE_BackendType == fBackendType
2197#endif
2198 ;
2199
2200 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2201 // than GL, we need to force it on. Just do that on the first frame after the backend
2202 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002203 if (!backendIsGL &&
2204 params.fGrContextOptions.fShaderCacheStrategy !=
2205 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2206 params.fGrContextOptions.fShaderCacheStrategy =
2207 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002208 paramsChanged = true;
2209 fPersistentCache.reset();
2210 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002211 // To re-load shaders from the currently active programs, we flush all caches on one
2212 // frame, then set a flag to poll the cache on the next frame.
2213 static bool gLoadPending = false;
2214 if (gLoadPending) {
2215 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2216 int hitCount) {
2217 CachedGLSL& entry(fCachedGLSL.push_back());
2218 entry.fKey = key;
2219 SkMD5 hash;
2220 hash.write(key->bytes(), key->size());
2221 SkMD5::Digest digest = hash.finish();
2222 for (int i = 0; i < 16; ++i) {
2223 entry.fKeyString.appendf("%02x", digest.data[i]);
2224 }
2225
Brian Osmana66081d2019-09-03 14:59:26 -04002226 SkReader32 reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002227 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002228 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2229 entry.fInputs,
2230 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002231 };
2232 fCachedGLSL.reset();
2233 fPersistentCache.foreach(collectShaders);
2234 gLoadPending = false;
2235 }
2236
2237 // Defer actually doing the load/save logic so that we can trigger a save when we
2238 // start or finish hovering on a tree node in the list below:
2239 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002240 bool doSave = ImGui::Button("Save");
2241 if (backendIsGL) {
2242 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002243 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2244 GrContextOptions::ShaderCacheStrategy::kSkSL;
2245 if (ImGui::Checkbox("SkSL", &sksl)) {
2246 params.fGrContextOptions.fShaderCacheStrategy = sksl
2247 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2248 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002249 paramsChanged = true;
2250 doLoad = true;
2251 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2252 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002253 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002254
2255 ImGui::BeginChild("##ScrollingRegion");
2256 for (auto& entry : fCachedGLSL) {
2257 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2258 bool hovered = ImGui::IsItemHovered();
2259 if (hovered != entry.fHovered) {
2260 // Force a save to patch the highlight shader in/out
2261 entry.fHovered = hovered;
2262 doSave = true;
2263 }
2264 if (inTreeNode) {
2265 // Full width, and a reasonable amount of space for each shader.
2266 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2267 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2268 boxSize);
2269 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2270 boxSize);
2271 ImGui::TreePop();
2272 }
2273 }
2274 ImGui::EndChild();
2275
2276 if (doLoad) {
2277 fPersistentCache.reset();
2278 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2279 gLoadPending = true;
2280 }
2281 if (doSave) {
2282 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002283 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002284 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2285 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002286
Brian Osman072e6fc2019-06-12 11:35:41 -04002287 SkSL::String highlight;
2288 if (!sksl) {
2289 highlight = shaderCaps->versionDeclString();
2290 if (shaderCaps->usesPrecisionModifiers()) {
2291 highlight.append("precision mediump float;\n");
2292 }
Brian Osman5bee3902019-05-07 09:55:45 -04002293 }
2294 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002295 highlight.appendf("out %s sk_FragColor;\n"
2296 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2297 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002298
2299 fPersistentCache.reset();
2300 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2301 for (auto& entry : fCachedGLSL) {
2302 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2303 if (entry.fHovered) {
2304 entry.fShader[kFragment_GrShaderType] = highlight;
2305 }
2306
Brian Osmana085a412019-04-25 09:44:43 -04002307 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2308 entry.fShader,
2309 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002310 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002311 fPersistentCache.store(*entry.fKey, *data);
2312
2313 entry.fShader[kFragment_GrShaderType] = backup;
2314 }
2315 }
2316 }
Brian Osman79086b92017-02-10 13:36:16 -05002317 }
Brian Salomon99a33902017-03-07 15:16:34 -05002318 if (paramsChanged) {
2319 fDeferredActions.push_back([=]() {
2320 fWindow->setRequestedDisplayParams(params);
2321 fWindow->inval();
2322 this->updateTitle();
2323 });
2324 }
Brian Osman79086b92017-02-10 13:36:16 -05002325 ImGui::End();
2326 }
2327
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002328 if (gShaderErrorHandler.fErrors.count()) {
2329 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2330 ImGui::Begin("Shader Errors");
2331 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2332 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002333 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2334 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2335 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2336 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002337 }
2338 ImGui::End();
2339 gShaderErrorHandler.reset();
2340 }
2341
Brian Osmanf6877092017-02-13 09:39:57 -05002342 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002343 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2344 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002345 static int zoomFactor = 8;
2346 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002347 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002348 }
2349 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2350 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002351 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002352 }
Brian Osmanf6877092017-02-13 09:39:57 -05002353
Ben Wagner3627d2e2018-06-26 14:23:20 -04002354 if (!fZoomWindowFixed) {
2355 ImVec2 mousePos = ImGui::GetMousePos();
2356 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2357 }
2358 SkScalar x = fZoomWindowLocation.x();
2359 SkScalar y = fZoomWindowLocation.y();
2360 int xInt = SkScalarRoundToInt(x);
2361 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002362 ImVec2 avail = ImGui::GetContentRegionAvail();
2363
Brian Osmanead517d2017-11-13 15:36:36 -05002364 uint32_t pixel = 0;
2365 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002366 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002367 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002368 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002369 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002370 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002371 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2372 }
2373
Brian Osmand67e5182017-12-08 16:46:09 -05002374 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002375 // Translate so the region of the image that's under the mouse cursor is centered
2376 // in the zoom canvas:
2377 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002378 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2379 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002380 c->drawImage(this->fLastImage, 0, 0);
2381
2382 SkPaint outline;
2383 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002384 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002385 });
Brian Osmanf6877092017-02-13 09:39:57 -05002386 }
2387
2388 ImGui::End();
2389 }
Brian Osman79086b92017-02-10 13:36:16 -05002390}
2391
liyuqian2edb0f42016-07-06 14:11:32 -07002392void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002393 for (int i = 0; i < fDeferredActions.count(); ++i) {
2394 fDeferredActions[i]();
2395 }
2396 fDeferredActions.reset();
2397
Brian Osman56a24812017-12-19 11:15:16 -05002398 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002399 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002400 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002401 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002402
Brian Osman79086b92017-02-10 13:36:16 -05002403 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002404 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2405 // not be visible, though. So we need to redraw if there is at least one visible window, or
2406 // more than one active window. Newly created windows are active but not visible for one frame
2407 // while they determine their layout and sizing.
2408 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2409 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002410 fWindow->inval();
2411 }
jvanverth9f372462016-04-06 06:08:59 -07002412}
liyuqiane5a6cd92016-05-27 08:52:52 -07002413
Florin Malitab632df72018-06-18 21:23:06 -04002414template <typename OptionsFunc>
2415static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2416 OptionsFunc&& optionsFunc) {
2417 writer.beginObject();
2418 {
2419 writer.appendString(kName , name);
2420 writer.appendString(kValue, value);
2421
2422 writer.beginArray(kOptions);
2423 {
2424 optionsFunc(writer);
2425 }
2426 writer.endArray();
2427 }
2428 writer.endObject();
2429}
2430
2431
liyuqiane5a6cd92016-05-27 08:52:52 -07002432void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002433 if (!fWindow) {
2434 return;
2435 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002436 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002437 return; // Surface hasn't been created yet.
2438 }
2439
Florin Malitab632df72018-06-18 21:23:06 -04002440 SkDynamicMemoryWStream memStream;
2441 SkJSONWriter writer(&memStream);
2442 writer.beginArray();
2443
liyuqianb73c24b2016-06-03 08:47:23 -07002444 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002445 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2446 [this](SkJSONWriter& writer) {
2447 for(const auto& slide : fSlides) {
2448 writer.appendString(slide->getName().c_str());
2449 }
2450 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002451
liyuqianb73c24b2016-06-03 08:47:23 -07002452 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002453 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2454 [](SkJSONWriter& writer) {
2455 for (const auto& str : kBackendTypeStrings) {
2456 writer.appendString(str);
2457 }
2458 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002459
csmartdalton578f0642017-02-24 16:04:47 -07002460 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002461 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2462 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2463 [this](SkJSONWriter& writer) {
2464 writer.appendS32(0);
2465
2466 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2467 return;
2468 }
2469
2470 for (int msaa : {4, 8, 16}) {
2471 writer.appendS32(msaa);
2472 }
2473 });
csmartdalton578f0642017-02-24 16:04:47 -07002474
csmartdalton61cd31a2017-02-27 17:00:53 -07002475 // Path renderer state
2476 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002477 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2478 [this](SkJSONWriter& writer) {
2479 const GrContext* ctx = fWindow->getGrContext();
2480 if (!ctx) {
2481 writer.appendString("Software");
2482 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002483 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002484 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2485 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002486 if (caps->shaderCaps()->tessellationSupport()) {
2487 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002488 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002489 }
Florin Malitab632df72018-06-18 21:23:06 -04002490 if (caps->shaderCaps()->pathRenderingSupport()) {
2491 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002492 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002493 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002494 }
2495 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002496 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2497 writer.appendString(
2498 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2499 }
2500 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2501 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002502 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002503 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002504 }
2505 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002506
liyuqianb73c24b2016-06-03 08:47:23 -07002507 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002508 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2509 [this](SkJSONWriter& writer) {
2510 writer.appendString(kSoftkeyHint);
2511 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2512 writer.appendString(softkey.c_str());
2513 }
2514 });
liyuqianb73c24b2016-06-03 08:47:23 -07002515
Florin Malitab632df72018-06-18 21:23:06 -04002516 writer.endArray();
2517 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002518
Florin Malitab632df72018-06-18 21:23:06 -04002519 auto data = memStream.detachAsData();
2520
2521 // TODO: would be cool to avoid this copy
2522 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2523
2524 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002525}
2526
2527void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002528 // For those who will add more features to handle the state change in this function:
2529 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2530 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2531 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002532 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002533 for (int i = 0; i < fSlides.count(); ++i) {
2534 if (fSlides[i]->getName().equals(stateValue)) {
2535 this->setCurrentSlide(i);
2536 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002537 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002538 }
Florin Malitaab99c342018-01-16 16:23:03 -05002539
2540 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002541 } else if (stateName.equals(kBackendStateName)) {
2542 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2543 if (stateValue.equals(kBackendTypeStrings[i])) {
2544 if (fBackendType != i) {
2545 fBackendType = (sk_app::Window::BackendType)i;
2546 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002547 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002548 }
2549 break;
2550 }
2551 }
csmartdalton578f0642017-02-24 16:04:47 -07002552 } else if (stateName.equals(kMSAAStateName)) {
2553 DisplayParams params = fWindow->getRequestedDisplayParams();
2554 int sampleCount = atoi(stateValue.c_str());
2555 if (sampleCount != params.fMSAASampleCount) {
2556 params.fMSAASampleCount = sampleCount;
2557 fWindow->setRequestedDisplayParams(params);
2558 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002559 this->updateTitle();
2560 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002561 }
2562 } else if (stateName.equals(kPathRendererStateName)) {
2563 DisplayParams params = fWindow->getRequestedDisplayParams();
2564 for (const auto& pair : gPathRendererNames) {
2565 if (pair.second == stateValue.c_str()) {
2566 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2567 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2568 fWindow->setRequestedDisplayParams(params);
2569 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002570 this->updateTitle();
2571 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002572 }
2573 break;
2574 }
csmartdalton578f0642017-02-24 16:04:47 -07002575 }
liyuqianb73c24b2016-06-03 08:47:23 -07002576 } else if (stateName.equals(kSoftkeyStateName)) {
2577 if (!stateValue.equals(kSoftkeyHint)) {
2578 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002579 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002580 }
liyuqian2edb0f42016-07-06 14:11:32 -07002581 } else if (stateName.equals(kRefreshStateName)) {
2582 // This state is actually NOT in the UI state.
2583 // We use this to allow Android to quickly set bool fRefresh.
2584 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002585 } else {
2586 SkDebugf("Unknown stateName: %s", stateName.c_str());
2587 }
2588}
Brian Osman79086b92017-02-10 13:36:16 -05002589
Hal Canaryb1f411a2019-08-29 10:39:22 -04002590bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002591 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002592}
2593
Hal Canaryb1f411a2019-08-29 10:39:22 -04002594bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002595 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002596 fWindow->inval();
2597 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002598 } else {
2599 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002600 }
Brian Osman79086b92017-02-10 13:36:16 -05002601}