blob: fa7b4f758a9acb4729f9dc59a3007ec925591b6b [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
Brian Osmanc85f1fa2020-06-16 15:11:34 -040053#ifdef SK_VULKAN
54#include "spirv-tools/libspirv.hpp"
55#endif
56
Florin Malita87ccf332018-05-04 12:23:24 -040057#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050058 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040059#endif
Florin Malita45cd2002020-06-09 14:00:54 -040060#if defined(SK_ENABLE_SKRIVE)
61 #include "tools/viewer/SkRiveSlide.h"
62#endif
Florin Malita87ccf332018-05-04 12:23:24 -040063
Brian Osman5e7fbfd2019-05-03 13:13:35 -040064class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
65public:
66 void compileError(const char* shader, const char* errors) override {
67 fShaders.push_back(SkString(shader));
68 fErrors.push_back(SkString(errors));
69 }
70
71 void reset() {
72 fShaders.reset();
73 fErrors.reset();
74 }
75
76 SkTArray<SkString> fShaders;
77 SkTArray<SkString> fErrors;
78};
79
80static CapturingShaderErrorHandler gShaderErrorHandler;
81
jvanverth34524262016-05-04 13:49:13 -070082using namespace sk_app;
83
csmartdalton61cd31a2017-02-27 17:00:53 -070084static std::map<GpuPathRenderers, std::string> gPathRendererNames;
85
jvanverth9f372462016-04-06 06:08:59 -070086Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070087 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070088}
89
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060090static DEFINE_string(slide, "", "Start on this sample.");
91static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050092
Jim Van Verth682a2f42020-05-13 16:54:09 -040093#ifdef SK_GL
94#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -070095#else
Jim Van Verth682a2f42020-05-13 16:54:09 -040096#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -070097#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -040098#ifdef SK_VULKAN
99#define VK_BACKEND_STR ", \"vk\""
100#else
101#define VK_BACKEND_STR
102#endif
103#ifdef SK_METAL
104#define MTL_BACKEND_STR ", \"mtl\""
105#else
106#define MTL_BACKEND_STR
107#endif
108#ifdef SK_DIRECT3D
109#define D3D_BACKEND_STR ", \"d3d\""
110#else
111#define D3D_BACKEND_STR
112#endif
113#ifdef SK_DAWN
114#define DAWN_BACKEND_STR ", \"dawn\""
115#else
116#define DAWN_BACKEND_STR
117#endif
118#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
119#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
120 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700121
Brian Osman2dd96932016-10-18 15:33:53 -0400122static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700123
Mike Klein5b3f3432019-03-21 11:42:21 -0500124static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -0700125
Mike Klein84836b72019-03-21 11:31:36 -0500126static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700127
Mike Klein84836b72019-03-21 11:31:36 -0500128static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400129
Mike Kleinc6142d82019-03-25 10:54:59 -0500130static DEFINE_string2(match, m, nullptr,
131 "[~][^]substring[$] [...] of name to run.\n"
132 "Multiple matches may be separated by spaces.\n"
133 "~ causes a matching name to always be skipped\n"
134 "^ requires the start of the name to match\n"
135 "$ requires the end of the name to match\n"
136 "^ and $ requires an exact match\n"
137 "If a name does not match any list entry,\n"
138 "it is skipped unless some list entry starts with ~");
139
Mike Klein19fb3972019-03-21 13:08:08 -0500140#if defined(SK_BUILD_FOR_ANDROID)
141 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500142 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
143 static DEFINE_string(lotties, "/data/local/tmp/lotties",
144 "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400145 static DEFINE_string(rives, "/data/local/tmp/rives",
146 "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500147#else
148 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500149 static DEFINE_string(skps, "skps", "Directory to read skps from.");
150 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400151 static DEFINE_string(rives, "rives", "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500152#endif
153
Mike Kleinc6142d82019-03-25 10:54:59 -0500154static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
155
156static DEFINE_int_2(threads, j, -1,
157 "Run threadsafe tests on a threadpool with this many extra threads, "
158 "defaulting to one extra thread per core.");
159
Jim Van Verth7b558182019-11-14 16:47:01 -0500160static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
161
Chris Daltonc8877332020-01-06 09:48:30 -0700162static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Reed862818b2020-03-21 15:07:13 -0400163static DEFINE_bool(skvm, false, "Try to use skvm blitters for raster.");
Mike Klein1e0884d2020-04-28 15:04:16 -0500164static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500165static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500166
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400167#ifndef SK_GL
168static_assert(false, "viewer requires GL backend for raster.")
169#endif
170
Brian Salomon194db172017-08-17 14:37:06 -0400171const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700172 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400173#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
174 "ANGLE",
175#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400176#ifdef SK_DAWN
177 "Dawn",
178#endif
jvanverth063ece72016-06-17 09:29:14 -0700179#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700180 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700181#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400182#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500183 "Metal",
184#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400185#ifdef SK_DIRECT3D
186 "Direct3D",
187#endif
csmartdalton578f0642017-02-24 16:04:47 -0700188 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700189};
190
bsalomon6c471f72016-07-26 12:56:32 -0700191static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400192#ifdef SK_DAWN
193 if (0 == strcmp(str, "dawn")) {
194 return sk_app::Window::kDawn_BackendType;
195 } else
196#endif
bsalomon6c471f72016-07-26 12:56:32 -0700197#ifdef SK_VULKAN
198 if (0 == strcmp(str, "vk")) {
199 return sk_app::Window::kVulkan_BackendType;
200 } else
201#endif
Brian Salomon194db172017-08-17 14:37:06 -0400202#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
203 if (0 == strcmp(str, "angle")) {
204 return sk_app::Window::kANGLE_BackendType;
205 } else
206#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400207#ifdef SK_METAL
208 if (0 == strcmp(str, "mtl")) {
209 return sk_app::Window::kMetal_BackendType;
210 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500211#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400212#ifdef SK_DIRECT3D
213 if (0 == strcmp(str, "d3d")) {
214 return sk_app::Window::kDirect3D_BackendType;
215 } else
216#endif
217
bsalomon6c471f72016-07-26 12:56:32 -0700218 if (0 == strcmp(str, "gl")) {
219 return sk_app::Window::kNativeGL_BackendType;
220 } else if (0 == strcmp(str, "sw")) {
221 return sk_app::Window::kRaster_BackendType;
222 } else {
223 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
224 return sk_app::Window::kRaster_BackendType;
225 }
226}
227
Brian Osmana109e392017-02-24 09:49:14 -0500228static SkColorSpacePrimaries gSrgbPrimaries = {
229 0.64f, 0.33f,
230 0.30f, 0.60f,
231 0.15f, 0.06f,
232 0.3127f, 0.3290f };
233
234static SkColorSpacePrimaries gAdobePrimaries = {
235 0.64f, 0.33f,
236 0.21f, 0.71f,
237 0.15f, 0.06f,
238 0.3127f, 0.3290f };
239
240static SkColorSpacePrimaries gP3Primaries = {
241 0.680f, 0.320f,
242 0.265f, 0.690f,
243 0.150f, 0.060f,
244 0.3127f, 0.3290f };
245
246static SkColorSpacePrimaries gRec2020Primaries = {
247 0.708f, 0.292f,
248 0.170f, 0.797f,
249 0.131f, 0.046f,
250 0.3127f, 0.3290f };
251
252struct NamedPrimaries {
253 const char* fName;
254 SkColorSpacePrimaries* fPrimaries;
255} gNamedPrimaries[] = {
256 { "sRGB", &gSrgbPrimaries },
257 { "AdobeRGB", &gAdobePrimaries },
258 { "P3", &gP3Primaries },
259 { "Rec. 2020", &gRec2020Primaries },
260};
261
262static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
263 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
264}
265
Brian Osman70d2f432017-11-08 09:54:10 -0500266static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
267 // In raster mode, we still use GL for the window.
268 // This lets us render the GUI faster (and correct).
269 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
270}
271
Jim Van Verth74826c82019-03-01 14:37:30 -0500272class NullSlide : public Slide {
273 SkISize getDimensions() const override {
274 return SkISize::Make(640, 480);
275 }
276
277 void draw(SkCanvas* canvas) override {
278 canvas->clear(0xffff11ff);
279 }
280};
281
John Stiles31964fd2020-05-05 16:05:47 -0400282static const char kName[] = "name";
283static const char kValue[] = "value";
284static const char kOptions[] = "options";
285static const char kSlideStateName[] = "Slide";
286static const char kBackendStateName[] = "Backend";
287static const char kMSAAStateName[] = "MSAA";
288static const char kPathRendererStateName[] = "Path renderer";
289static const char kSoftkeyStateName[] = "Softkey";
290static const char kSoftkeyHint[] = "Please select a softkey";
291static const char kON[] = "ON";
292static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700293
Mike Reed862818b2020-03-21 15:07:13 -0400294extern bool gUseSkVMBlitter;
Mike Klein1e0884d2020-04-28 15:04:16 -0500295extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400296
jvanverth34524262016-05-04 13:49:13 -0700297Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500298 : fCurrentSlide(-1)
299 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500300 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400301 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500302 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500303 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500304 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500305 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400306 , fZoomWindowFixed(false)
307 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500308 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400309 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700310 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500311 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500312 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500313 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500314 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700315 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400316 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400317 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400318 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500319 , fTiled(false)
320 , fDrawTileBoundaries(false)
321 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400322 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700323{
Greg Daniel285db442016-10-14 09:12:53 -0400324 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700325
Chris Dalton37ae4b02019-12-28 14:51:11 -0700326 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600327 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500328 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500329 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600330 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600331 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500332 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700333
jvanverth2bb3b6d2016-04-08 07:24:09 -0700334 SkDebugf("Command line arguments: ");
335 for (int i = 1; i < argc; ++i) {
336 SkDebugf("%s ", argv[i]);
337 }
338 SkDebugf("\n");
339
Mike Klein88544fb2019-03-20 10:50:33 -0500340 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500341#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400342 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500343#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700344
Mike Reed862818b2020-03-21 15:07:13 -0400345 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein1e0884d2020-04-28 15:04:16 -0500346 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400347
Mike Klein19cc0f62019-03-22 15:30:07 -0500348 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500349
Brian Osmanbc8150f2017-07-24 11:38:01 -0400350 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400351 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400352
bsalomon6c471f72016-07-26 12:56:32 -0700353 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700354 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700355
csmartdalton578f0642017-02-24 16:04:47 -0700356 DisplayParams displayParams;
357 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700358 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400359 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400360 displayParams.fGrContextOptions.fShaderCacheStrategy =
361 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400362 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
363 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700364 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500365 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700366
Brian Osman56a24812017-12-19 11:15:16 -0500367 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500368 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500369 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
370 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
371 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
372
jvanverth9f372462016-04-06 06:08:59 -0700373 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700374 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500375 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500376 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500377 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700378
brianosman622c8d52016-05-10 06:50:49 -0700379 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500380 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
381 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
382 fWindow->inval();
383 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500384 // Command to jump directly to the slide picker and give it focus
385 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
386 this->fShowImGuiDebugWindow = true;
387 this->fShowSlidePicker = true;
388 fWindow->inval();
389 });
390 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400391 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500392 this->fShowImGuiDebugWindow = true;
393 this->fShowSlidePicker = true;
394 fWindow->inval();
395 });
Brian Osman79086b92017-02-10 13:36:16 -0500396 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
397 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
398 fWindow->inval();
399 });
Brian Osmanf6877092017-02-13 09:39:57 -0500400 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
401 this->fShowZoomWindow = !this->fShowZoomWindow;
402 fWindow->inval();
403 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400404 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
405 this->fZoomWindowFixed = !this->fZoomWindowFixed;
406 fWindow->inval();
407 });
Greg Danield0794cc2019-03-27 16:23:26 -0400408 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
409 DisplayParams params = fWindow->getRequestedDisplayParams();
410 params.fDisableVsync = !params.fDisableVsync;
411 fWindow->setRequestedDisplayParams(params);
412 this->updateTitle();
413 fWindow->inval();
414 });
Mike Reedf702ed42019-07-22 17:00:49 -0400415 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
416 fRefresh = !fRefresh;
417 fWindow->inval();
418 });
brianosman622c8d52016-05-10 06:50:49 -0700419 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500420 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700421 fWindow->inval();
422 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400423 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500424 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400425 this->updateTitle();
426 fWindow->inval();
427 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500428 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500429 switch (fColorMode) {
430 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500431 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500432 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500433 case ColorMode::kColorManaged8888:
434 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500435 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500436 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400437 this->setColorMode(ColorMode::kColorManagedF16Norm);
438 break;
439 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500440 this->setColorMode(ColorMode::kLegacy);
441 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500442 }
brianosman622c8d52016-05-10 06:50:49 -0700443 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700444 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
445 DisplayParams params = fWindow->getRequestedDisplayParams();
446 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
447 fWindow->setRequestedDisplayParams(params);
448 fWindow->inval();
449 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400450 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500451 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700452 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400453 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500454 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700455 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400456 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700457 this->changeZoomLevel(1.f / 32.f);
458 fWindow->inval();
459 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400460 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700461 this->changeZoomLevel(-1.f / 32.f);
462 fWindow->inval();
463 });
jvanverthaf236b52016-05-20 06:01:06 -0700464 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400465 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
466 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500467 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400468#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
469 if (newBackend == sk_app::Window::kVulkan_BackendType) {
470 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
471 sk_app::Window::kBackendTypeCount);
472 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
473 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500474 }
475#endif
Brian Osman621491e2017-02-28 15:45:01 -0500476 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700477 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500478 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
479 fSaveToSKP = true;
480 fWindow->inval();
481 });
Mike Reed376d8122019-03-14 11:39:02 -0400482 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
483 fShowSlideDimensions = !fShowSlideDimensions;
484 fWindow->inval();
485 });
Ben Wagner37c54032018-04-13 14:30:23 -0400486 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
487 DisplayParams params = fWindow->getRequestedDisplayParams();
488 uint32_t flags = params.fSurfaceProps.flags();
489 if (!fPixelGeometryOverrides) {
490 fPixelGeometryOverrides = true;
491 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
492 } else {
493 switch (params.fSurfaceProps.pixelGeometry()) {
494 case kUnknown_SkPixelGeometry:
495 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
496 break;
497 case kRGB_H_SkPixelGeometry:
498 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
499 break;
500 case kBGR_H_SkPixelGeometry:
501 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
502 break;
503 case kRGB_V_SkPixelGeometry:
504 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
505 break;
506 case kBGR_V_SkPixelGeometry:
507 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
508 fPixelGeometryOverrides = false;
509 break;
510 }
511 }
512 fWindow->setRequestedDisplayParams(params);
513 this->updateTitle();
514 fWindow->inval();
515 });
Ben Wagner9613e452019-01-23 10:34:59 -0500516 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500517 if (!fFontOverrides.fHinting) {
518 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400519 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500520 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500521 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400522 case SkFontHinting::kNone:
523 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500524 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400525 case SkFontHinting::kSlight:
526 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500527 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400528 case SkFontHinting::kNormal:
529 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500530 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400531 case SkFontHinting::kFull:
532 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500533 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500534 break;
535 }
536 }
537 this->updateTitle();
538 fWindow->inval();
539 });
540 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500541 if (!fPaintOverrides.fAntiAlias) {
542 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
543 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500544 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500545 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500546 } else {
547 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500548 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500549 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500550 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400551 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500552 break;
553 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500554 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500555 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400556 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500557 break;
558 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500559 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400560 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500561 break;
562 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500563 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
564 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500565 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
566 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500567 break;
568 }
569 }
570 this->updateTitle();
571 fWindow->inval();
572 });
Ben Wagner37c54032018-04-13 14:30:23 -0400573 fCommands.addCommand('D', "Modes", "DFT", [this]() {
574 DisplayParams params = fWindow->getRequestedDisplayParams();
575 uint32_t flags = params.fSurfaceProps.flags();
576 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
577 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
578 fWindow->setRequestedDisplayParams(params);
579 this->updateTitle();
580 fWindow->inval();
581 });
Ben Wagner9613e452019-01-23 10:34:59 -0500582 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500583 if (!fFontOverrides.fEdging) {
584 fFontOverrides.fEdging = true;
585 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500586 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500587 switch (fFont.getEdging()) {
588 case SkFont::Edging::kAlias:
589 fFont.setEdging(SkFont::Edging::kAntiAlias);
590 break;
591 case SkFont::Edging::kAntiAlias:
592 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
593 break;
594 case SkFont::Edging::kSubpixelAntiAlias:
595 fFont.setEdging(SkFont::Edging::kAlias);
596 fFontOverrides.fEdging = false;
597 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500598 }
599 }
600 this->updateTitle();
601 fWindow->inval();
602 });
Ben Wagner9613e452019-01-23 10:34:59 -0500603 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500604 if (!fFontOverrides.fSubpixel) {
605 fFontOverrides.fSubpixel = true;
606 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500607 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500608 if (!fFont.isSubpixel()) {
609 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500610 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500611 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500612 }
613 }
614 this->updateTitle();
615 fWindow->inval();
616 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400617 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
618 if (!fFontOverrides.fBaselineSnap) {
619 fFontOverrides.fBaselineSnap = true;
620 fFont.setBaselineSnap(false);
621 } else {
622 if (!fFont.isBaselineSnap()) {
623 fFont.setBaselineSnap(true);
624 } else {
625 fFontOverrides.fBaselineSnap = false;
626 }
627 }
628 this->updateTitle();
629 fWindow->inval();
630 });
Brian Osman805a7272018-05-02 15:40:20 -0400631 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
632 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
633 : kPerspective_Real;
634 this->updateTitle();
635 fWindow->inval();
636 });
637 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
638 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
639 : kPerspective_Off;
640 this->updateTitle();
641 fWindow->inval();
642 });
Brian Osman207d4102019-01-10 09:40:58 -0500643 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
644 fAnimTimer.togglePauseResume();
645 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400646 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
647 fZoomUI = !fZoomUI;
648 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
649 fWindow->inval();
650 });
Mike Reed59295352020-03-12 13:56:34 -0400651 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
652 fDrawViaSerialize = !fDrawViaSerialize;
653 this->updateTitle();
654 fWindow->inval();
655 });
Mike Reed862818b2020-03-21 15:07:13 -0400656 fCommands.addCommand('!', "SkVM", "Toggle SkVM", [this]() {
657 gUseSkVMBlitter = !gUseSkVMBlitter;
658 this->updateTitle();
659 fWindow->inval();
660 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500661
jvanverth2bb3b6d2016-04-08 07:24:09 -0700662 // set up slides
663 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500664 if (FLAGS_list) {
665 this->listNames();
666 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700667
Brian Osman9bb47cf2018-04-26 15:55:00 -0400668 fPerspectivePoints[0].set(0, 0);
669 fPerspectivePoints[1].set(1, 0);
670 fPerspectivePoints[2].set(0, 1);
671 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700672 fAnimTimer.run();
673
Hal Canaryc465d132017-12-08 10:21:31 -0500674 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500675 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400676 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500677 }
678 fImGuiGamutPaint.setColor(SK_ColorWHITE);
679 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
680
jongdeok.kim804f17e2019-02-26 14:39:23 +0900681 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500682 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700683}
684
jvanverth34524262016-05-04 13:49:13 -0700685void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400686 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
687 static const struct {
688 const char* fExtension;
689 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500690 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400691 const SlideFactory fFactory;
692 } gExternalSlidesInfo[] = {
693 { ".skp", "skp-dir", FLAGS_skps,
694 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
695 return sk_make_sp<SKPSlide>(name, path);}
696 },
697 { ".jpg", "jpg-dir", FLAGS_jpgs,
698 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
699 return sk_make_sp<ImageSlide>(name, path);}
700 },
Florin Malita87ccf332018-05-04 12:23:24 -0400701#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400702 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400703 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
704 return sk_make_sp<SkottieSlide>(name, path);}
705 },
Florin Malita87ccf332018-05-04 12:23:24 -0400706#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400707 #if defined(SK_ENABLE_SKRIVE)
708 { ".flr", "skrive-dir", FLAGS_rives,
709 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
710 return sk_make_sp<SkRiveSlide>(name, path);}
711 },
712 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400713#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400714 { ".svg", "svg-dir", FLAGS_svgs,
715 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
716 return sk_make_sp<SvgSlide>(name, path);}
717 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400718#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400719 };
jvanverthc265a922016-04-08 12:51:45 -0700720
Brian Salomon343553a2018-09-05 15:41:23 -0400721 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700722
Mike Klein88544fb2019-03-20 10:50:33 -0500723 const auto addSlide =
724 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
725 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
726 return;
727 }
liyuqian6f163d22016-06-13 12:26:45 -0700728
Mike Klein88544fb2019-03-20 10:50:33 -0500729 if (auto slide = fact(name, path)) {
730 dirSlides.push_back(slide);
731 fSlides.push_back(std::move(slide));
732 }
733 };
Florin Malita76a076b2018-02-15 18:40:48 -0500734
Florin Malita38792ce2018-05-08 10:36:18 -0400735 if (!FLAGS_file.isEmpty()) {
736 // single file mode
737 const SkString file(FLAGS_file[0]);
738
739 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
740 for (const auto& sinfo : gExternalSlidesInfo) {
741 if (file.endsWith(sinfo.fExtension)) {
742 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
743 return;
744 }
745 }
746
747 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
748 } else {
749 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
750 }
751
752 return;
753 }
754
755 // Bisect slide.
756 if (!FLAGS_bisect.isEmpty()) {
757 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500758 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400759 if (FLAGS_bisect.count() >= 2) {
760 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
761 bisect->onChar(*ch);
762 }
763 }
764 fSlides.push_back(std::move(bisect));
765 }
766 }
767
768 // GMs
769 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400770 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400771 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500772 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400773 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400774 fSlides.push_back(std::move(slide));
775 }
Florin Malita38792ce2018-05-08 10:36:18 -0400776 }
777 // reverse gms
778 int numGMs = fSlides.count() - firstGM;
779 for (int i = 0; i < numGMs/2; ++i) {
780 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
781 }
782
783 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400784 for (const SampleFactory factory : SampleRegistry::Range()) {
785 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500786 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400787 fSlides.push_back(slide);
788 }
Florin Malita38792ce2018-05-08 10:36:18 -0400789 }
790
Brian Osman7c979f52019-02-12 13:27:51 -0500791 // Particle demo
792 {
793 // TODO: Convert this to a sample
794 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500795 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500796 fSlides.push_back(std::move(slide));
797 }
798 }
799
Brian Osmand927bd22019-12-18 11:23:12 -0500800 // Runtime shader editor
801 {
802 sk_sp<Slide> slide(new SkSLSlide());
803 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
804 fSlides.push_back(std::move(slide));
805 }
806 }
807
Florin Malita0ffa3222018-04-05 14:34:45 -0400808 for (const auto& info : gExternalSlidesInfo) {
809 for (const auto& flag : info.fFlags) {
810 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
811 // single file
812 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
813 } else {
814 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400815 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400816 SkTArray<SkString> sortedFilenames;
817 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400818 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400819 sortedFilenames.push_back(name);
820 }
821 if (sortedFilenames.count()) {
822 SkTQSort(sortedFilenames.begin(), sortedFilenames.end() - 1,
823 [](const SkString& a, const SkString& b) {
824 return strcmp(a.c_str(), b.c_str()) < 0;
825 });
826 }
827 for (const SkString& filename : sortedFilenames) {
828 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
829 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400830 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400831 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400832 if (!dirSlides.empty()) {
833 fSlides.push_back(
834 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
835 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500836 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400837 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400838 }
839 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500840
841 if (!fSlides.count()) {
842 sk_sp<Slide> slide(new NullSlide());
843 fSlides.push_back(std::move(slide));
844 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700845}
846
847
jvanverth34524262016-05-04 13:49:13 -0700848Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700849 fWindow->detach();
850 delete fWindow;
851}
852
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500853struct SkPaintTitleUpdater {
854 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
855 void append(const char* s) {
856 if (fCount == 0) {
857 fTitle->append(" {");
858 } else {
859 fTitle->append(", ");
860 }
861 fTitle->append(s);
862 ++fCount;
863 }
864 void done() {
865 if (fCount > 0) {
866 fTitle->append("}");
867 }
868 }
869 SkString* fTitle;
870 int fCount;
871};
872
brianosman05de2162016-05-06 13:28:57 -0700873void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700874 if (!fWindow) {
875 return;
876 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500877 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700878 return; // Surface hasn't been created yet.
879 }
880
jvanverth34524262016-05-04 13:49:13 -0700881 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700882 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700883
Mike Kleine5acd752019-03-22 09:57:16 -0500884 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400885 if (gSkForceAnalyticAA) {
886 title.append(" <FAAA>");
887 } else {
888 title.append(" <AAA>");
889 }
890 }
Mike Reed59295352020-03-12 13:56:34 -0400891 if (fDrawViaSerialize) {
892 title.append(" <serialize>");
893 }
Mike Reed862818b2020-03-21 15:07:13 -0400894 if (gUseSkVMBlitter) {
895 title.append(" <skvm>");
896 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400897
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500898 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500899 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
900 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400901 const char* on, const char* off)
902 {
Ben Wagner9613e452019-01-23 10:34:59 -0500903 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400904 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500905 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400906 };
907
Ben Wagner9613e452019-01-23 10:34:59 -0500908 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
909 const char* on, const char* off)
910 {
911 if (fFontOverrides.*flag) {
912 paintTitle.append((fFont.*isFlag)() ? on : off);
913 }
914 };
915
916 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
917 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500918 if (fPaintOverrides.fFilterQuality) {
919 switch (fPaint.getFilterQuality()) {
920 case kNone_SkFilterQuality:
921 paintTitle.append("NoFilter");
922 break;
923 case kLow_SkFilterQuality:
924 paintTitle.append("LowFilter");
925 break;
926 case kMedium_SkFilterQuality:
927 paintTitle.append("MediumFilter");
928 break;
929 case kHigh_SkFilterQuality:
930 paintTitle.append("HighFilter");
931 break;
932 }
933 }
Ben Wagner9613e452019-01-23 10:34:59 -0500934
935 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
936 "Force Autohint", "No Force Autohint");
937 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400938 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500939 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
940 "Linear Metrics", "Non-Linear Metrics");
941 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
942 "Bitmap Text", "No Bitmap Text");
943 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
944
945 if (fFontOverrides.fEdging) {
946 switch (fFont.getEdging()) {
947 case SkFont::Edging::kAlias:
948 paintTitle.append("Alias Text");
949 break;
950 case SkFont::Edging::kAntiAlias:
951 paintTitle.append("Antialias Text");
952 break;
953 case SkFont::Edging::kSubpixelAntiAlias:
954 paintTitle.append("Subpixel Antialias Text");
955 break;
956 }
957 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400958
Mike Reed3ae47332019-01-04 10:11:46 -0500959 if (fFontOverrides.fHinting) {
960 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400961 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500962 paintTitle.append("No Hinting");
963 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400964 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500965 paintTitle.append("Slight Hinting");
966 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400967 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500968 paintTitle.append("Normal Hinting");
969 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400970 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500971 paintTitle.append("Full Hinting");
972 break;
973 }
974 }
975 paintTitle.done();
976
Brian Osman92004802017-03-06 11:47:26 -0500977 switch (fColorMode) {
978 case ColorMode::kLegacy:
979 title.append(" Legacy 8888");
980 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500981 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500982 title.append(" ColorManaged 8888");
983 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500984 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500985 title.append(" ColorManaged F16");
986 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400987 case ColorMode::kColorManagedF16Norm:
988 title.append(" ColorManaged F16 Norm");
989 break;
Brian Osman92004802017-03-06 11:47:26 -0500990 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500991
Brian Osman92004802017-03-06 11:47:26 -0500992 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500993 int curPrimaries = -1;
994 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
995 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
996 curPrimaries = i;
997 break;
998 }
999 }
Brian Osman03115dc2018-11-26 13:55:19 -05001000 title.appendf(" %s Gamma %f",
1001 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001002 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001003 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001004
Ben Wagner37c54032018-04-13 14:30:23 -04001005 const DisplayParams& params = fWindow->getRequestedDisplayParams();
1006 if (fPixelGeometryOverrides) {
1007 switch (params.fSurfaceProps.pixelGeometry()) {
1008 case kUnknown_SkPixelGeometry:
1009 title.append( " Flat");
1010 break;
1011 case kRGB_H_SkPixelGeometry:
1012 title.append( " RGB");
1013 break;
1014 case kBGR_H_SkPixelGeometry:
1015 title.append( " BGR");
1016 break;
1017 case kRGB_V_SkPixelGeometry:
1018 title.append( " RGBV");
1019 break;
1020 case kBGR_V_SkPixelGeometry:
1021 title.append( " BGRV");
1022 break;
1023 }
1024 }
1025
1026 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1027 title.append(" DFT");
1028 }
1029
csmartdalton578f0642017-02-24 16:04:47 -07001030 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001031 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001032 int msaa = fWindow->sampleCount();
1033 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001034 title.appendf(" MSAA: %i", msaa);
1035 }
1036 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001037
1038 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001039 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001040 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1041 }
1042
Brian Osman805a7272018-05-02 15:40:20 -04001043 if (kPerspective_Real == fPerspectiveMode) {
1044 title.append(" Perpsective (Real)");
1045 } else if (kPerspective_Fake == fPerspectiveMode) {
1046 title.append(" Perspective (Fake)");
1047 }
1048
brianosman05de2162016-05-06 13:28:57 -07001049 fWindow->setTitle(title.c_str());
1050}
1051
Florin Malitaab99c342018-01-16 16:23:03 -05001052int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001053
1054 if (!FLAGS_slide.isEmpty()) {
1055 int count = fSlides.count();
1056 for (int i = 0; i < count; i++) {
1057 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001058 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001059 }
1060 }
1061
1062 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1063 this->listNames();
1064 }
1065
Florin Malitaab99c342018-01-16 16:23:03 -05001066 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001067}
1068
Florin Malitaab99c342018-01-16 16:23:03 -05001069void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001070 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001071 for (const auto& slide : fSlides) {
1072 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001073 }
1074}
1075
Florin Malitaab99c342018-01-16 16:23:03 -05001076void Viewer::setCurrentSlide(int slide) {
1077 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001078
Florin Malitaab99c342018-01-16 16:23:03 -05001079 if (slide == fCurrentSlide) {
1080 return;
1081 }
1082
1083 if (fCurrentSlide >= 0) {
1084 fSlides[fCurrentSlide]->unload();
1085 }
1086
1087 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1088 SkIntToScalar(fWindow->height()));
1089 fCurrentSlide = slide;
1090 this->setupCurrentSlide();
1091}
1092
1093void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001094 if (fCurrentSlide >= 0) {
1095 // prepare dimensions for image slides
1096 fGesture.resetTouchState();
1097 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001098
Jim Van Verth0848fb02018-01-22 13:39:30 -05001099 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1100 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1101 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001102
Jim Van Verth0848fb02018-01-22 13:39:30 -05001103 // Start with a matrix that scales the slide to the available screen space
1104 if (fWindow->scaleContentToFit()) {
1105 if (windowRect.width() > 0 && windowRect.height() > 0) {
1106 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1107 }
liyuqiane46e4f02016-05-20 07:32:19 -07001108 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001109
1110 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001111 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001112
1113 this->updateTitle();
1114 this->updateUIState();
1115
1116 fStatsLayer.resetMeasurements();
1117
1118 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001119 }
jvanverthc265a922016-04-08 12:51:45 -07001120}
1121
Brian Osmanaba642c2020-02-06 12:52:25 -05001122#define MAX_ZOOM_LEVEL 8.0f
1123#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001124
jvanverth34524262016-05-04 13:49:13 -07001125void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001126 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001127 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001128 this->preTouchMatrixChanged();
1129}
Yuqian Li755778c2018-03-28 16:23:31 -04001130
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001131void Viewer::preTouchMatrixChanged() {
1132 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001133 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1134 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1135 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1136 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1137}
1138
Brian Osman805a7272018-05-02 15:40:20 -04001139SkMatrix Viewer::computePerspectiveMatrix() {
1140 SkScalar w = fWindow->width(), h = fWindow->height();
1141 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1142 SkPoint perspPts[4] = {
1143 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1144 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1145 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1146 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1147 };
1148 SkMatrix m;
1149 m.setPolyToPoly(orthoPts, perspPts, 4);
1150 return m;
1151}
1152
Yuqian Li755778c2018-03-28 16:23:31 -04001153SkMatrix Viewer::computePreTouchMatrix() {
1154 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001155
1156 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001157 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001158 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001159
1160 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1161 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001162
Brian Osman805a7272018-05-02 15:40:20 -04001163 if (kPerspective_Real == fPerspectiveMode) {
1164 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001165 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001166 }
1167
Yuqian Li755778c2018-03-28 16:23:31 -04001168 return m;
jvanverthc265a922016-04-08 12:51:45 -07001169}
1170
liyuqiand3cdbca2016-05-17 12:44:20 -07001171SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001172 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001173 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001174 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001175 return m;
jvanverthc265a922016-04-08 12:51:45 -07001176}
1177
Brian Osman621491e2017-02-28 15:45:01 -05001178void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001179 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001180 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001181 fBackendType = backendType;
1182
1183 fWindow->detach();
1184
Brian Osman70d2f432017-11-08 09:54:10 -05001185#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001186 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1187 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001188 DisplayParams params = fWindow->getRequestedDisplayParams();
1189 delete fWindow;
1190 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001191
Brian Osman70d2f432017-11-08 09:54:10 -05001192 // re-register callbacks
1193 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001194 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001195 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001196 fWindow->pushLayer(&fImGuiLayer);
1197
Brian Osman70d2f432017-11-08 09:54:10 -05001198 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1199 // will still include our correct sample count. But the re-created fWindow will lose that
1200 // information. On Windows, we need to re-create the window when changing sample count,
1201 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1202 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1203 // as if we had never changed windows at all).
1204 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001205#endif
1206
Brian Osman70d2f432017-11-08 09:54:10 -05001207 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001208}
1209
Brian Osman92004802017-03-06 11:47:26 -05001210void Viewer::setColorMode(ColorMode colorMode) {
1211 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001212 this->updateTitle();
1213 fWindow->inval();
1214}
1215
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001216class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1217public:
Mike Reed3ae47332019-01-04 10:11:46 -05001218 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1219 SkFont* font, Viewer::SkFontFields* ffields)
1220 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001221 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001222 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1223 sk_sp<SkTextBlob>* cache) {
1224 bool blobWillChange = false;
1225 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001226 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1227 bool shouldDraw = this->filterFont(&filteredFont);
1228 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001229 blobWillChange = true;
1230 break;
1231 }
1232 }
1233 if (!blobWillChange) {
1234 return blob;
1235 }
1236
1237 SkTextBlobBuilder builder;
1238 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001239 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1240 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001241 if (!shouldDraw) {
1242 continue;
1243 }
1244
Mike Reed3ae47332019-01-04 10:11:46 -05001245 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001246
Ben Wagner41e40472018-09-24 13:01:54 -04001247 const SkTextBlobBuilder::RunBuffer& runBuffer
1248 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001249 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001250 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001251 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001252 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001253 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001254 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001255 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001256 it.glyphCount(), it.textSize(), SkString())
1257 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1258 uint32_t glyphCount = it.glyphCount();
1259 if (it.glyphs()) {
1260 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1261 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1262 }
1263 if (it.pos()) {
1264 size_t posSize = sizeof(decltype(*it.pos()));
1265 uint8_t positioning = it.positioning();
1266 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1267 }
1268 if (it.text()) {
1269 size_t textSize = sizeof(decltype(*it.text()));
1270 uint32_t textCount = it.textSize();
1271 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1272 }
1273 if (it.clusters()) {
1274 size_t clusterSize = sizeof(decltype(*it.clusters()));
1275 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1276 }
1277 }
1278 *cache = builder.make();
1279 return cache->get();
1280 }
1281 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1282 const SkPaint& paint) override {
1283 sk_sp<SkTextBlob> cache;
1284 this->SkPaintFilterCanvas::onDrawTextBlob(
1285 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1286 }
Mike Reed3ae47332019-01-04 10:11:46 -05001287 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001288 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001289 font->writable()->setSize(fFont->getSize());
1290 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001291 if (fFontOverrides->fScaleX) {
1292 font->writable()->setScaleX(fFont->getScaleX());
1293 }
1294 if (fFontOverrides->fSkewX) {
1295 font->writable()->setSkewX(fFont->getSkewX());
1296 }
Mike Reed3ae47332019-01-04 10:11:46 -05001297 if (fFontOverrides->fHinting) {
1298 font->writable()->setHinting(fFont->getHinting());
1299 }
Ben Wagner9613e452019-01-23 10:34:59 -05001300 if (fFontOverrides->fEdging) {
1301 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001302 }
Ben Wagner9613e452019-01-23 10:34:59 -05001303 if (fFontOverrides->fEmbolden) {
1304 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001305 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001306 if (fFontOverrides->fBaselineSnap) {
1307 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1308 }
Ben Wagner9613e452019-01-23 10:34:59 -05001309 if (fFontOverrides->fLinearMetrics) {
1310 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001311 }
Ben Wagner9613e452019-01-23 10:34:59 -05001312 if (fFontOverrides->fSubpixel) {
1313 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001314 }
Ben Wagner9613e452019-01-23 10:34:59 -05001315 if (fFontOverrides->fEmbeddedBitmaps) {
1316 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001317 }
Ben Wagner9613e452019-01-23 10:34:59 -05001318 if (fFontOverrides->fForceAutoHinting) {
1319 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001320 }
Ben Wagner9613e452019-01-23 10:34:59 -05001321
Mike Reed3ae47332019-01-04 10:11:46 -05001322 return true;
1323 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001324 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001325 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001326 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001327 }
Ben Wagner9613e452019-01-23 10:34:59 -05001328 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001329 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001330 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001331 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001332 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001333 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001334 return true;
1335 }
1336 SkPaint* fPaint;
1337 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001338 SkFont* fFont;
1339 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001340};
1341
Robert Phillips9882dae2019-03-04 11:00:10 -05001342void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001343 if (fCurrentSlide < 0) {
1344 return;
1345 }
1346
Robert Phillips9882dae2019-03-04 11:00:10 -05001347 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001348
Brian Osmanf750fbc2017-02-08 10:47:28 -05001349 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001350 SkSurface* slideSurface = surface;
1351 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001352 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001353
Brian Osmane0d4fba2017-03-15 10:24:55 -04001354 // 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 -05001355 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001356 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001357 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001358 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001359 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001360 }
1361
Brian Osman3ac99cf2017-12-01 11:23:53 -05001362 if (fSaveToSKP) {
1363 SkPictureRecorder recorder;
1364 SkCanvas* recorderCanvas = recorder.beginRecording(
1365 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001366 fSlides[fCurrentSlide]->draw(recorderCanvas);
1367 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1368 SkFILEWStream stream("sample_app.skp");
1369 picture->serialize(&stream);
1370 fSaveToSKP = false;
1371 }
1372
Brian Osmane9ed0f02018-11-26 14:50:05 -05001373 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001374 SkColorType colorType;
1375 switch (fColorMode) {
1376 case ColorMode::kLegacy:
1377 case ColorMode::kColorManaged8888:
1378 colorType = kN32_SkColorType;
1379 break;
1380 case ColorMode::kColorManagedF16:
1381 colorType = kRGBA_F16_SkColorType;
1382 break;
1383 case ColorMode::kColorManagedF16Norm:
1384 colorType = kRGBA_F16Norm_SkColorType;
1385 break;
1386 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001387
1388 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001389 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1390 slideCanvas->getProps(&props);
1391
Brian Osmane9ed0f02018-11-26 14:50:05 -05001392 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1393 return Window::kRaster_BackendType == this->fBackendType
1394 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001395 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001396 };
1397
Brian Osman03115dc2018-11-26 13:55:19 -05001398 // We need to render offscreen if we're...
1399 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1400 // ... in any raster mode, because the window surface is actually GL
1401 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001402 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001403 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001404 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001405 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001406 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001407 colorSpace != nullptr ||
1408 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001409
Brian Osmane9ed0f02018-11-26 14:50:05 -05001410 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001411 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001412 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001413 }
1414
Mike Reed59295352020-03-12 13:56:34 -04001415 SkPictureRecorder recorder;
1416 SkCanvas* recorderRestoreCanvas = nullptr;
1417 if (fDrawViaSerialize) {
1418 recorderRestoreCanvas = slideCanvas;
1419 slideCanvas = recorder.beginRecording(
1420 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1421 }
1422
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001423 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001424 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001425 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001426 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001427 if (fTiled) {
1428 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1429 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001430 for (int y = 0; y < fWindow->height(); y += tileH) {
1431 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001432 SkAutoCanvasRestore acr(slideCanvas, true);
1433 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1434 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001435 }
1436 }
1437
1438 // Draw borders between tiles
1439 if (fDrawTileBoundaries) {
1440 SkPaint border;
1441 border.setColor(0x60FF00FF);
1442 border.setStyle(SkPaint::kStroke_Style);
1443 for (int y = 0; y < fWindow->height(); y += tileH) {
1444 for (int x = 0; x < fWindow->width(); x += tileW) {
1445 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1446 }
1447 }
1448 }
1449 } else {
1450 slideCanvas->concat(this->computeMatrix());
1451 if (kPerspective_Real == fPerspectiveMode) {
1452 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1453 }
Mike Reed3ae47332019-01-04 10:11:46 -05001454 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001455 fSlides[fCurrentSlide]->draw(&filterCanvas);
1456 }
Brian Osman56a24812017-12-19 11:15:16 -05001457 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001458 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001459
Mike Reed59295352020-03-12 13:56:34 -04001460 if (recorderRestoreCanvas) {
1461 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1462 auto data = picture->serialize();
1463 slideCanvas = recorderRestoreCanvas;
1464 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1465 }
1466
Brian Osman1df161a2017-02-09 12:10:20 -05001467 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001468 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001469 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001470 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001471
1472 // If we rendered offscreen, snap an image and push the results to the window's canvas
1473 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001474 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001475
Robert Phillips9882dae2019-03-04 11:00:10 -05001476 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001477 SkPaint paint;
1478 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001479 int prePerspectiveCount = canvas->save();
1480 if (kPerspective_Fake == fPerspectiveMode) {
1481 paint.setFilterQuality(kHigh_SkFilterQuality);
1482 canvas->clear(SK_ColorWHITE);
1483 canvas->concat(this->computePerspectiveMatrix());
1484 }
Brian Osman03115dc2018-11-26 13:55:19 -05001485 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001486 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001487 }
Mike Reed376d8122019-03-14 11:39:02 -04001488
1489 if (fShowSlideDimensions) {
1490 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1491 SkPaint paint;
1492 paint.setColor(0x40FFFF00);
1493 surface->getCanvas()->drawRect(r, paint);
1494 }
liyuqian6f163d22016-06-13 12:26:45 -07001495}
1496
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001497void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001498 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001499 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001500}
Jim Van Verth6f449692017-02-14 15:16:46 -05001501
Robert Phillips9882dae2019-03-04 11:00:10 -05001502void Viewer::onPaint(SkSurface* surface) {
1503 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001504
Robert Phillips9882dae2019-03-04 11:00:10 -05001505 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001506
Brian Osmand67e5182017-12-08 16:46:09 -05001507 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001508
1509 if (GrContext* ctx = fWindow->getGrContext()) {
1510 // Clean out cache items that haven't been used in more than 10 seconds.
1511 ctx->performDeferredCleanup(std::chrono::seconds(10));
1512 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001513}
1514
Ben Wagnera1915972018-08-09 15:06:19 -04001515void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001516 if (fCurrentSlide >= 0) {
1517 fSlides[fCurrentSlide]->resize(width, height);
1518 }
Ben Wagnera1915972018-08-09 15:06:19 -04001519}
1520
Florin Malitacefc1b92018-02-19 21:43:47 -05001521SkPoint Viewer::mapEvent(float x, float y) {
1522 const auto m = this->computeMatrix();
1523 SkMatrix inv;
1524
1525 SkAssertResult(m.invert(&inv));
1526
1527 return inv.mapXY(x, y);
1528}
1529
Hal Canaryb1f411a2019-08-29 10:39:22 -04001530bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001531 if (GestureDevice::kMouse == fGestureDevice) {
1532 return false;
1533 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001534
1535 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001536 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001537 fWindow->inval();
1538 return true;
1539 }
1540
liyuqiand3cdbca2016-05-17 12:44:20 -07001541 void* castedOwner = reinterpret_cast<void*>(owner);
1542 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001543 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001544 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001545#if defined(SK_BUILD_FOR_IOS)
1546 // TODO: move IOS swipe detection higher up into the platform code
1547 SkPoint dir;
1548 if (fGesture.isFling(&dir)) {
1549 // swiping left or right
1550 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1551 if (dir.fX < 0) {
1552 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1553 fCurrentSlide + 1 : 0);
1554 } else {
1555 this->setCurrentSlide(fCurrentSlide > 0 ?
1556 fCurrentSlide - 1 : fSlides.count() - 1);
1557 }
1558 }
1559 fGesture.reset();
1560 }
1561#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001562 break;
1563 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001564 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001565 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001566 break;
1567 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001568 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001569 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001570 break;
1571 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001572 default: {
1573 // kLeft and kRight are only for swipes
1574 SkASSERT(false);
1575 break;
1576 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001577 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001578 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001579 fWindow->inval();
1580 return true;
1581}
1582
Hal Canaryb1f411a2019-08-29 10:39:22 -04001583bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001584 if (GestureDevice::kTouch == fGestureDevice) {
1585 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001586 }
Brian Osman16c81a12017-12-20 11:58:34 -05001587
Florin Malitacefc1b92018-02-19 21:43:47 -05001588 const auto slidePt = this->mapEvent(x, y);
1589 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1590 fWindow->inval();
1591 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001592 }
1593
1594 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001595 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001596 fGesture.touchEnd(nullptr);
1597 break;
1598 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001599 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001600 fGesture.touchBegin(nullptr, x, y);
1601 break;
1602 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001603 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001604 fGesture.touchMoved(nullptr, x, y);
1605 break;
1606 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001607 default: {
1608 SkASSERT(false); // shouldn't see kRight or kLeft here
1609 break;
1610 }
Brian Osman16c81a12017-12-20 11:58:34 -05001611 }
1612 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1613
Hal Canaryb1f411a2019-08-29 10:39:22 -04001614 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001615 fWindow->inval();
1616 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001617 return true;
1618}
1619
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001620bool Viewer::onFling(skui::InputState state) {
1621 if (skui::InputState::kRight == state) {
1622 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1623 return true;
1624 } else if (skui::InputState::kLeft == state) {
1625 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1626 return true;
1627 }
1628 return false;
1629}
1630
1631bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1632 switch (state) {
1633 case skui::InputState::kDown:
1634 fGesture.startZoom();
1635 return true;
1636 break;
1637 case skui::InputState::kMove:
1638 fGesture.updateZoom(scale, x, y, x, y);
1639 return true;
1640 break;
1641 case skui::InputState::kUp:
1642 fGesture.endZoom();
1643 return true;
1644 break;
1645 default:
1646 SkASSERT(false);
1647 break;
1648 }
1649
1650 return false;
1651}
1652
Brian Osmana109e392017-02-24 09:49:14 -05001653static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001654 // The gamut image covers a (0.8 x 0.9) shaped region
1655 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001656
1657 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1658 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1659 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001660 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1661 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1662 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001663
Brian Osman535c5e32019-02-09 16:32:58 -05001664 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1665 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1666 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1667 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1668 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001669}
1670
Ben Wagner3627d2e2018-06-26 14:23:20 -04001671static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001672 ImGui::DragCanvas dc(pt);
1673 dc.fillColor(IM_COL32(0, 0, 0, 128));
1674 dc.dragPoint(pt);
1675 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001676}
1677
Brian Osman9bb47cf2018-04-26 15:55:00 -04001678static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001679 ImGui::DragCanvas dc(pts);
1680 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001681
Brian Osman535c5e32019-02-09 16:32:58 -05001682 for (int i = 0; i < 4; ++i) {
1683 dc.dragPoint(pts + i);
1684 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001685
Brian Osman535c5e32019-02-09 16:32:58 -05001686 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1687 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1688 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1689 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001690
Brian Osman535c5e32019-02-09 16:32:58 -05001691 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001692}
1693
Brian Osmand67e5182017-12-08 16:46:09 -05001694void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001695 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1696 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001697 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001698 }
1699
1700 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001701 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1702 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001703 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001704 DisplayParams params = fWindow->getRequestedDisplayParams();
1705 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001706 const GrContext* ctx = fWindow->getGrContext();
1707
Brian Osmana109e392017-02-24 09:49:14 -05001708 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1709 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001710 if (ImGui::CollapsingHeader("Backend")) {
1711 int newBackend = static_cast<int>(fBackendType);
1712 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1713 ImGui::SameLine();
1714 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001715#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1716 ImGui::SameLine();
1717 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1718#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001719#if defined(SK_DAWN)
1720 ImGui::SameLine();
1721 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1722#endif
Brian Osman621491e2017-02-28 15:45:01 -05001723#if defined(SK_VULKAN)
1724 ImGui::SameLine();
1725 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1726#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001727#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001728 ImGui::SameLine();
1729 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1730#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001731#if defined(SK_DIRECT3D)
1732 ImGui::SameLine();
1733 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1734#endif
Brian Osman621491e2017-02-28 15:45:01 -05001735 if (newBackend != fBackendType) {
1736 fDeferredActions.push_back([=]() {
1737 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1738 });
1739 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001740
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001741 bool* wire = &params.fGrContextOptions.fWireframeMode;
1742 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1743 paramsChanged = true;
1744 }
Brian Salomon99a33902017-03-07 15:16:34 -05001745
Brian Osman28b12522017-03-08 17:10:24 -05001746 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001747 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001748 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001749 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1750 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1751 1;
1752
1753 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1754 if (maxMSAA >= 4) {
1755 ImGui::Text("MSAA: ");
1756
1757 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1758 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1759 // don't show it in the list.
1760 if (curMSAA == 2) {
1761 continue;
1762 }
1763 ImGui::SameLine();
1764 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1765 &sampleCount, curMSAA);
1766 }
1767 }
Brian Osman28b12522017-03-08 17:10:24 -05001768
1769 if (sampleCount != params.fMSAASampleCount) {
1770 params.fMSAASampleCount = sampleCount;
1771 paramsChanged = true;
1772 }
1773 }
1774
Ben Wagner37c54032018-04-13 14:30:23 -04001775 int pixelGeometryIdx = 0;
1776 if (fPixelGeometryOverrides) {
1777 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1778 }
1779 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1780 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1781 {
1782 uint32_t flags = params.fSurfaceProps.flags();
1783 if (pixelGeometryIdx == 0) {
1784 fPixelGeometryOverrides = false;
1785 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1786 } else {
1787 fPixelGeometryOverrides = true;
1788 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1789 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1790 }
1791 paramsChanged = true;
1792 }
1793
1794 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1795 if (ImGui::Checkbox("DFT", &useDFT)) {
1796 uint32_t flags = params.fSurfaceProps.flags();
1797 if (useDFT) {
1798 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1799 } else {
1800 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1801 }
1802 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1803 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1804 paramsChanged = true;
1805 }
1806
Brian Osman8a9de3d2017-03-01 14:59:05 -05001807 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001808 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001809 auto prButton = [&](GpuPathRenderers x) {
1810 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001811 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1812 params.fGrContextOptions.fGpuPathRenderers = x;
1813 paramsChanged = true;
1814 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001815 }
1816 };
1817
1818 if (!ctx) {
1819 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001820 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001821 const auto* caps = ctx->priv().caps();
1822 prButton(GpuPathRenderers::kDefault);
1823 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001824 if (caps->shaderCaps()->tessellationSupport()) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001825 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001826 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001827 if (caps->shaderCaps()->pathRenderingSupport()) {
1828 prButton(GpuPathRenderers::kStencilAndCover);
1829 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001830 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001831 if (1 == fWindow->sampleCount()) {
1832 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1833 prButton(GpuPathRenderers::kCoverageCounting);
1834 }
1835 prButton(GpuPathRenderers::kSmall);
1836 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001837 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001838 prButton(GpuPathRenderers::kNone);
1839 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001840 ImGui::TreePop();
1841 }
Brian Osman621491e2017-02-28 15:45:01 -05001842 }
1843
Ben Wagner964571d2019-03-08 12:35:06 -05001844 if (ImGui::CollapsingHeader("Tiling")) {
1845 ImGui::Checkbox("Enable", &fTiled);
1846 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1847 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1848 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1849 }
1850
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001851 if (ImGui::CollapsingHeader("Transform")) {
1852 float zoom = fZoomLevel;
1853 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1854 fZoomLevel = zoom;
1855 this->preTouchMatrixChanged();
1856 paramsChanged = true;
1857 }
1858 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001859 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001860 fRotation = deg;
1861 this->preTouchMatrixChanged();
1862 paramsChanged = true;
1863 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001864 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1865 if (ImGui_DragLocation(&fOffset)) {
1866 this->preTouchMatrixChanged();
1867 paramsChanged = true;
1868 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001869 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1870 this->preTouchMatrixChanged();
1871 paramsChanged = true;
1872 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001873 }
Brian Osman805a7272018-05-02 15:40:20 -04001874 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1875 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1876 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
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 Wagner3627d2e2018-06-26 14:23:20 -04001880 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001881 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001882 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001883 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001884 }
1885
Ben Wagnera580fb32018-04-17 11:16:32 -04001886 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001887 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001888 if (fPaintOverrides.fAntiAlias) {
1889 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001890 }
1891 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001892 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001893 {
1894 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1895 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001896 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001897 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1898 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001899 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001900 fPaintOverrides.fAntiAlias = true;
1901 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001902 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001903 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001904 case SkPaintFields::AntiAliasState::Alias:
1905 break;
1906 case SkPaintFields::AntiAliasState::Normal:
1907 break;
1908 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1909 gSkUseAnalyticAA = true;
1910 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001911 break;
1912 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1913 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001914 break;
1915 }
1916 }
1917 paramsChanged = true;
1918 }
1919
Ben Wagner99a78dc2018-05-09 18:23:51 -04001920 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001921 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001922 bool (SkPaint::* isFlag)() const,
1923 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001924 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001925 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001926 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001927 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001928 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001929 if (ImGui::Combo(label, &itemIndex, items)) {
1930 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001931 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001932 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001933 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001934 (fPaint.*setFlag)(itemIndex == 2);
1935 }
1936 paramsChanged = true;
1937 }
1938 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001939
Ben Wagner99a78dc2018-05-09 18:23:51 -04001940 paintFlag("Dither",
1941 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001942 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001943 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001944
1945 int filterQualityIdx = 0;
1946 if (fPaintOverrides.fFilterQuality) {
1947 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1948 }
1949 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1950 "Default\0None\0Low\0Medium\0High\0\0"))
1951 {
1952 if (filterQualityIdx == 0) {
1953 fPaintOverrides.fFilterQuality = false;
1954 fPaint.setFilterQuality(kNone_SkFilterQuality);
1955 } else {
1956 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1957 fPaintOverrides.fFilterQuality = true;
1958 }
1959 paramsChanged = true;
1960 }
Ben Wagner9613e452019-01-23 10:34:59 -05001961 }
Hal Canary02738a82019-01-21 18:51:32 +00001962
Ben Wagner9613e452019-01-23 10:34:59 -05001963 if (ImGui::CollapsingHeader("Font")) {
1964 int hintingIdx = 0;
1965 if (fFontOverrides.fHinting) {
1966 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1967 }
1968 if (ImGui::Combo("Hinting", &hintingIdx,
1969 "Default\0None\0Slight\0Normal\0Full\0\0"))
1970 {
1971 if (hintingIdx == 0) {
1972 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001973 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001974 } else {
1975 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1976 fFontOverrides.fHinting = true;
1977 }
1978 paramsChanged = true;
1979 }
Hal Canary02738a82019-01-21 18:51:32 +00001980
Ben Wagner9613e452019-01-23 10:34:59 -05001981 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1982 bool SkFontFields::* flag,
1983 bool (SkFont::* isFlag)() const,
1984 void (SkFont::* setFlag)(bool) )
1985 {
1986 int itemIndex = 0;
1987 if (fFontOverrides.*flag) {
1988 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1989 }
1990 if (ImGui::Combo(label, &itemIndex, items)) {
1991 if (itemIndex == 0) {
1992 fFontOverrides.*flag = false;
1993 } else {
1994 fFontOverrides.*flag = true;
1995 (fFont.*setFlag)(itemIndex == 2);
1996 }
1997 paramsChanged = true;
1998 }
1999 };
Hal Canary02738a82019-01-21 18:51:32 +00002000
Ben Wagner9613e452019-01-23 10:34:59 -05002001 fontFlag("Fake Bold Glyphs",
2002 "Default\0No Fake Bold\0Fake Bold\0\0",
2003 &SkFontFields::fEmbolden,
2004 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002005
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002006 fontFlag("Baseline Snapping",
2007 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2008 &SkFontFields::fBaselineSnap,
2009 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2010
Ben Wagner9613e452019-01-23 10:34:59 -05002011 fontFlag("Linear Text",
2012 "Default\0No Linear Text\0Linear Text\0\0",
2013 &SkFontFields::fLinearMetrics,
2014 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002015
Ben Wagner9613e452019-01-23 10:34:59 -05002016 fontFlag("Subpixel Position Glyphs",
2017 "Default\0Pixel Text\0Subpixel Text\0\0",
2018 &SkFontFields::fSubpixel,
2019 &SkFont::isSubpixel, &SkFont::setSubpixel);
2020
2021 fontFlag("Embedded Bitmap Text",
2022 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2023 &SkFontFields::fEmbeddedBitmaps,
2024 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2025
2026 fontFlag("Force Auto-Hinting",
2027 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2028 &SkFontFields::fForceAutoHinting,
2029 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2030
2031 int edgingIdx = 0;
2032 if (fFontOverrides.fEdging) {
2033 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2034 }
2035 if (ImGui::Combo("Edging", &edgingIdx,
2036 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2037 {
2038 if (edgingIdx == 0) {
2039 fFontOverrides.fEdging = false;
2040 fFont.setEdging(SkFont::Edging::kAlias);
2041 } else {
2042 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2043 fFontOverrides.fEdging = true;
2044 }
2045 paramsChanged = true;
2046 }
2047
Ben Wagner15a8d572019-03-21 13:35:44 -04002048 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2049 if (fFontOverrides.fSize) {
2050 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002051 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002052 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002053 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002054 fFontOverrides.fSizeRange[0],
2055 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002056 "%.6f", 2.0f))
2057 {
Mike Reed3ae47332019-01-04 10:11:46 -05002058 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002059 paramsChanged = true;
2060 }
2061 }
2062
2063 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2064 if (fFontOverrides.fScaleX) {
2065 float scaleX = fFont.getScaleX();
2066 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2067 fFont.setScaleX(scaleX);
2068 paramsChanged = true;
2069 }
2070 }
2071
2072 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2073 if (fFontOverrides.fSkewX) {
2074 float skewX = fFont.getSkewX();
2075 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2076 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002077 paramsChanged = true;
2078 }
2079 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002080 }
2081
Mike Reed81f60ec2018-05-15 10:09:52 -04002082 {
2083 SkMetaData controls;
2084 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2085 if (ImGui::CollapsingHeader("Current Slide")) {
2086 SkMetaData::Iter iter(controls);
2087 const char* name;
2088 SkMetaData::Type type;
2089 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002090 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002091 if (type == SkMetaData::kScalar_Type) {
2092 float val[3];
2093 SkASSERT(count == 3);
2094 controls.findScalars(name, &count, val);
2095 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2096 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002097 }
Ben Wagner110c7032019-03-22 17:03:59 -04002098 } else if (type == SkMetaData::kBool_Type) {
2099 bool val;
2100 SkASSERT(count == 1);
2101 controls.findBool(name, &val);
2102 if (ImGui::Checkbox(name, &val)) {
2103 controls.setBool(name, val);
2104 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002105 }
2106 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002107 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002108 }
2109 }
2110 }
2111
Ben Wagner7a3c6742018-04-23 10:01:07 -04002112 if (fShowSlidePicker) {
2113 ImGui::SetNextTreeNodeOpen(true);
2114 }
Brian Osman79086b92017-02-10 13:36:16 -05002115 if (ImGui::CollapsingHeader("Slide")) {
2116 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002117 static ImVector<const char*> filteredSlideNames;
2118 static ImVector<int> filteredSlideIndices;
2119
Brian Osmanfce09c52017-11-14 15:32:20 -05002120 if (fShowSlidePicker) {
2121 ImGui::SetKeyboardFocusHere();
2122 fShowSlidePicker = false;
2123 }
2124
Brian Osman79086b92017-02-10 13:36:16 -05002125 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002126 filteredSlideNames.clear();
2127 filteredSlideIndices.clear();
2128 int filteredIndex = 0;
2129 for (int i = 0; i < fSlides.count(); ++i) {
2130 const char* slideName = fSlides[i]->getName().c_str();
2131 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2132 if (i == fCurrentSlide) {
2133 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002134 }
Brian Osmanf479e422017-11-08 13:11:36 -05002135 filteredSlideNames.push_back(slideName);
2136 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002137 }
Brian Osman79086b92017-02-10 13:36:16 -05002138 }
Brian Osmanf479e422017-11-08 13:11:36 -05002139
Brian Osmanf479e422017-11-08 13:11:36 -05002140 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2141 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002142 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002143 }
2144 }
Brian Osmana109e392017-02-24 09:49:14 -05002145
2146 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002147 ColorMode newMode = fColorMode;
2148 auto cmButton = [&](ColorMode mode, const char* label) {
2149 if (ImGui::RadioButton(label, mode == fColorMode)) {
2150 newMode = mode;
2151 }
2152 };
2153
2154 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002155 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2156 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002157 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002158
2159 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002160 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002161 }
2162
2163 // Pick from common gamuts:
2164 int primariesIdx = 4; // Default: Custom
2165 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2166 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2167 primariesIdx = i;
2168 break;
2169 }
2170 }
2171
Brian Osman03115dc2018-11-26 13:55:19 -05002172 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002173 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002174
Brian Osmana109e392017-02-24 09:49:14 -05002175 if (ImGui::Combo("Primaries", &primariesIdx,
2176 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2177 if (primariesIdx >= 0 && primariesIdx <= 3) {
2178 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2179 }
2180 }
2181
2182 // Allow direct editing of gamut
2183 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2184 }
Brian Osman207d4102019-01-10 09:40:58 -05002185
2186 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002187 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002188 if (ImGui::Checkbox("Pause", &isPaused)) {
2189 fAnimTimer.togglePauseResume();
2190 }
Brian Osman707d2022019-01-10 11:27:34 -05002191
2192 float speed = fAnimTimer.getSpeed();
2193 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2194 fAnimTimer.setSpeed(speed);
2195 }
Brian Osman207d4102019-01-10 09:40:58 -05002196 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002197
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002198 if (ImGui::CollapsingHeader("Shaders")) {
2199 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2200 GrContextOptions::ShaderCacheStrategy::kSkSL;
2201#if defined(SK_VULKAN)
2202 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2203#else
2204 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002205#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002206
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002207 // To re-load shaders from the currently active programs, we flush all
2208 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002209 static bool gLoadPending = false;
2210 if (gLoadPending) {
2211 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2212 int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002213 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002214 entry.fKey = key;
2215 SkMD5 hash;
2216 hash.write(key->bytes(), key->size());
2217 SkMD5::Digest digest = hash.finish();
2218 for (int i = 0; i < 16; ++i) {
2219 entry.fKeyString.appendf("%02x", digest.data[i]);
2220 }
2221
Brian Osman9e4e4c72020-06-10 07:19:34 -04002222 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002223 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002224 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2225 entry.fInputs,
2226 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002227 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002228 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002229 fPersistentCache.foreach(collectShaders);
2230 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002231
2232#if defined(SK_VULKAN)
2233 if (isVulkan && !sksl) {
2234 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2235 for (auto& entry : fCachedShaders) {
2236 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2237 const SkSL::String& spirv(entry.fShader[i]);
2238 std::string disasm;
2239 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2240 &disasm);
2241 entry.fShader[i].assign(disasm);
2242 }
2243 }
2244 }
2245#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002246 }
2247
2248 // Defer actually doing the load/save logic so that we can trigger a save when we
2249 // start or finish hovering on a tree node in the list below:
2250 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002251 bool doSave = ImGui::Button("Save"); ImGui::SameLine();
2252 if (ImGui::Checkbox("SkSL", &sksl)) {
2253 params.fGrContextOptions.fShaderCacheStrategy =
2254 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2255 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2256 paramsChanged = true;
2257 doLoad = true;
2258 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
Brian Osmancbc33b82019-04-19 14:16:19 -04002259 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002260
2261 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002262 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002263 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2264 bool hovered = ImGui::IsItemHovered();
2265 if (hovered != entry.fHovered) {
2266 // Force a save to patch the highlight shader in/out
2267 entry.fHovered = hovered;
2268 doSave = true;
2269 }
2270 if (inTreeNode) {
2271 // Full width, and a reasonable amount of space for each shader.
2272 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2273 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2274 boxSize);
2275 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2276 boxSize);
2277 ImGui::TreePop();
2278 }
2279 }
2280 ImGui::EndChild();
2281
2282 if (doLoad) {
2283 fPersistentCache.reset();
2284 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2285 gLoadPending = true;
2286 }
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002287 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2288 // but I'm not sure anyone wants to do that.
2289 if (isVulkan && !sksl) {
2290 doSave = false;
2291 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002292 if (doSave) {
2293 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002294 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osman5bee3902019-05-07 09:55:45 -04002295
Brian Osman072e6fc2019-06-12 11:35:41 -04002296 SkSL::String highlight;
2297 if (!sksl) {
2298 highlight = shaderCaps->versionDeclString();
2299 if (shaderCaps->usesPrecisionModifiers()) {
2300 highlight.append("precision mediump float;\n");
2301 }
Brian Osman5bee3902019-05-07 09:55:45 -04002302 }
2303 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002304 highlight.appendf("out %s sk_FragColor;\n"
2305 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2306 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002307
2308 fPersistentCache.reset();
2309 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002310 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002311 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002312 if (entry.fHovered &&
2313 (entry.fShaderType == SkSetFourByteTag('S', 'K', 'S', 'L') ||
2314 entry.fShaderType == SkSetFourByteTag('G', 'L', 'S', 'L'))) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002315 entry.fShader[kFragment_GrShaderType] = highlight;
2316 }
2317
Brian Osmana085a412019-04-25 09:44:43 -04002318 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2319 entry.fShader,
2320 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002321 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002322 fPersistentCache.store(*entry.fKey, *data);
2323
2324 entry.fShader[kFragment_GrShaderType] = backup;
2325 }
2326 }
2327 }
Brian Osman79086b92017-02-10 13:36:16 -05002328 }
Brian Salomon99a33902017-03-07 15:16:34 -05002329 if (paramsChanged) {
2330 fDeferredActions.push_back([=]() {
2331 fWindow->setRequestedDisplayParams(params);
2332 fWindow->inval();
2333 this->updateTitle();
2334 });
2335 }
Brian Osman79086b92017-02-10 13:36:16 -05002336 ImGui::End();
2337 }
2338
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002339 if (gShaderErrorHandler.fErrors.count()) {
2340 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2341 ImGui::Begin("Shader Errors");
2342 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2343 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002344 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2345 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2346 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2347 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002348 }
2349 ImGui::End();
2350 gShaderErrorHandler.reset();
2351 }
2352
Brian Osmanf6877092017-02-13 09:39:57 -05002353 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002354 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2355 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002356 static int zoomFactor = 8;
2357 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002358 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002359 }
2360 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2361 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002362 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002363 }
Brian Osmanf6877092017-02-13 09:39:57 -05002364
Ben Wagner3627d2e2018-06-26 14:23:20 -04002365 if (!fZoomWindowFixed) {
2366 ImVec2 mousePos = ImGui::GetMousePos();
2367 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2368 }
2369 SkScalar x = fZoomWindowLocation.x();
2370 SkScalar y = fZoomWindowLocation.y();
2371 int xInt = SkScalarRoundToInt(x);
2372 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002373 ImVec2 avail = ImGui::GetContentRegionAvail();
2374
Brian Osmanead517d2017-11-13 15:36:36 -05002375 uint32_t pixel = 0;
2376 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002377 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002378 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002379 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002380 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002381 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002382 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2383 }
2384
Brian Osmand67e5182017-12-08 16:46:09 -05002385 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002386 // Translate so the region of the image that's under the mouse cursor is centered
2387 // in the zoom canvas:
2388 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002389 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2390 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002391 c->drawImage(this->fLastImage, 0, 0);
2392
2393 SkPaint outline;
2394 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002395 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002396 });
Brian Osmanf6877092017-02-13 09:39:57 -05002397 }
2398
2399 ImGui::End();
2400 }
Brian Osman79086b92017-02-10 13:36:16 -05002401}
2402
liyuqian2edb0f42016-07-06 14:11:32 -07002403void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002404 for (int i = 0; i < fDeferredActions.count(); ++i) {
2405 fDeferredActions[i]();
2406 }
2407 fDeferredActions.reset();
2408
Brian Osman56a24812017-12-19 11:15:16 -05002409 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002410 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002411 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002412 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002413
Brian Osman79086b92017-02-10 13:36:16 -05002414 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002415 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2416 // not be visible, though. So we need to redraw if there is at least one visible window, or
2417 // more than one active window. Newly created windows are active but not visible for one frame
2418 // while they determine their layout and sizing.
2419 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2420 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002421 fWindow->inval();
2422 }
jvanverth9f372462016-04-06 06:08:59 -07002423}
liyuqiane5a6cd92016-05-27 08:52:52 -07002424
Florin Malitab632df72018-06-18 21:23:06 -04002425template <typename OptionsFunc>
2426static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2427 OptionsFunc&& optionsFunc) {
2428 writer.beginObject();
2429 {
2430 writer.appendString(kName , name);
2431 writer.appendString(kValue, value);
2432
2433 writer.beginArray(kOptions);
2434 {
2435 optionsFunc(writer);
2436 }
2437 writer.endArray();
2438 }
2439 writer.endObject();
2440}
2441
2442
liyuqiane5a6cd92016-05-27 08:52:52 -07002443void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002444 if (!fWindow) {
2445 return;
2446 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002447 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002448 return; // Surface hasn't been created yet.
2449 }
2450
Florin Malitab632df72018-06-18 21:23:06 -04002451 SkDynamicMemoryWStream memStream;
2452 SkJSONWriter writer(&memStream);
2453 writer.beginArray();
2454
liyuqianb73c24b2016-06-03 08:47:23 -07002455 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002456 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2457 [this](SkJSONWriter& writer) {
2458 for(const auto& slide : fSlides) {
2459 writer.appendString(slide->getName().c_str());
2460 }
2461 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002462
liyuqianb73c24b2016-06-03 08:47:23 -07002463 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002464 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2465 [](SkJSONWriter& writer) {
2466 for (const auto& str : kBackendTypeStrings) {
2467 writer.appendString(str);
2468 }
2469 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002470
csmartdalton578f0642017-02-24 16:04:47 -07002471 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002472 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2473 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2474 [this](SkJSONWriter& writer) {
2475 writer.appendS32(0);
2476
2477 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2478 return;
2479 }
2480
2481 for (int msaa : {4, 8, 16}) {
2482 writer.appendS32(msaa);
2483 }
2484 });
csmartdalton578f0642017-02-24 16:04:47 -07002485
csmartdalton61cd31a2017-02-27 17:00:53 -07002486 // Path renderer state
2487 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002488 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2489 [this](SkJSONWriter& writer) {
2490 const GrContext* ctx = fWindow->getGrContext();
2491 if (!ctx) {
2492 writer.appendString("Software");
2493 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002494 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002495 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2496 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002497 if (caps->shaderCaps()->tessellationSupport()) {
2498 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002499 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002500 }
Florin Malitab632df72018-06-18 21:23:06 -04002501 if (caps->shaderCaps()->pathRenderingSupport()) {
2502 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002503 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002504 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002505 }
2506 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002507 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2508 writer.appendString(
2509 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2510 }
2511 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2512 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002513 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002514 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002515 }
2516 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002517
liyuqianb73c24b2016-06-03 08:47:23 -07002518 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002519 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2520 [this](SkJSONWriter& writer) {
2521 writer.appendString(kSoftkeyHint);
2522 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2523 writer.appendString(softkey.c_str());
2524 }
2525 });
liyuqianb73c24b2016-06-03 08:47:23 -07002526
Florin Malitab632df72018-06-18 21:23:06 -04002527 writer.endArray();
2528 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002529
Florin Malitab632df72018-06-18 21:23:06 -04002530 auto data = memStream.detachAsData();
2531
2532 // TODO: would be cool to avoid this copy
2533 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2534
2535 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002536}
2537
2538void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002539 // For those who will add more features to handle the state change in this function:
2540 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2541 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2542 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002543 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002544 for (int i = 0; i < fSlides.count(); ++i) {
2545 if (fSlides[i]->getName().equals(stateValue)) {
2546 this->setCurrentSlide(i);
2547 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002548 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002549 }
Florin Malitaab99c342018-01-16 16:23:03 -05002550
2551 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002552 } else if (stateName.equals(kBackendStateName)) {
2553 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2554 if (stateValue.equals(kBackendTypeStrings[i])) {
2555 if (fBackendType != i) {
2556 fBackendType = (sk_app::Window::BackendType)i;
2557 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002558 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002559 }
2560 break;
2561 }
2562 }
csmartdalton578f0642017-02-24 16:04:47 -07002563 } else if (stateName.equals(kMSAAStateName)) {
2564 DisplayParams params = fWindow->getRequestedDisplayParams();
2565 int sampleCount = atoi(stateValue.c_str());
2566 if (sampleCount != params.fMSAASampleCount) {
2567 params.fMSAASampleCount = sampleCount;
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 } else if (stateName.equals(kPathRendererStateName)) {
2574 DisplayParams params = fWindow->getRequestedDisplayParams();
2575 for (const auto& pair : gPathRendererNames) {
2576 if (pair.second == stateValue.c_str()) {
2577 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2578 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2579 fWindow->setRequestedDisplayParams(params);
2580 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002581 this->updateTitle();
2582 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002583 }
2584 break;
2585 }
csmartdalton578f0642017-02-24 16:04:47 -07002586 }
liyuqianb73c24b2016-06-03 08:47:23 -07002587 } else if (stateName.equals(kSoftkeyStateName)) {
2588 if (!stateValue.equals(kSoftkeyHint)) {
2589 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002590 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002591 }
liyuqian2edb0f42016-07-06 14:11:32 -07002592 } else if (stateName.equals(kRefreshStateName)) {
2593 // This state is actually NOT in the UI state.
2594 // We use this to allow Android to quickly set bool fRefresh.
2595 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002596 } else {
2597 SkDebugf("Unknown stateName: %s", stateName.c_str());
2598 }
2599}
Brian Osman79086b92017-02-10 13:36:16 -05002600
Hal Canaryb1f411a2019-08-29 10:39:22 -04002601bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002602 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002603}
2604
Hal Canaryb1f411a2019-08-29 10:39:22 -04002605bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002606 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002607 fWindow->inval();
2608 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002609 } else {
2610 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002611 }
Brian Osman79086b92017-02-10 13:36:16 -05002612}