blob: 22ae76254e2f729e7de5ee30c134b94a4bc1b5f3 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkGraphics.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkSurface.h"
14#include "include/gpu/GrContext.h"
15#include "include/private/SkTo.h"
16#include "include/utils/SkPaintFilterCanvas.h"
17#include "src/core/SkColorSpacePriv.h"
18#include "src/core/SkImagePriv.h"
19#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkOSFile.h"
21#include "src/core/SkScan.h"
22#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040023#include "src/core/SkTextBlobPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrGpu.h"
26#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070027#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
29#include "src/utils/SkJSONWriter.h"
30#include "src/utils/SkOSPath.h"
31#include "tools/Resources.h"
32#include "tools/ToolUtils.h"
33#include "tools/flags/CommandLineFlags.h"
34#include "tools/flags/CommonFlags.h"
35#include "tools/trace/EventTracingPriv.h"
36#include "tools/viewer/BisectSlide.h"
37#include "tools/viewer/GMSlide.h"
38#include "tools/viewer/ImageSlide.h"
39#include "tools/viewer/ParticlesSlide.h"
40#include "tools/viewer/SKPSlide.h"
41#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050042#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "tools/viewer/SlideDir.h"
44#include "tools/viewer/SvgSlide.h"
45#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070046
Chris Dalton17dc4182020-03-25 16:18:16 -060047#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040048#include <map>
49
Hal Canary8a001442018-09-19 11:31:27 -040050#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040051#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040052
Florin Malita87ccf332018-05-04 12:23:24 -040053#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040055#endif
56
Brian Osman5e7fbfd2019-05-03 13:13:35 -040057class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
58public:
59 void compileError(const char* shader, const char* errors) override {
60 fShaders.push_back(SkString(shader));
61 fErrors.push_back(SkString(errors));
62 }
63
64 void reset() {
65 fShaders.reset();
66 fErrors.reset();
67 }
68
69 SkTArray<SkString> fShaders;
70 SkTArray<SkString> fErrors;
71};
72
73static CapturingShaderErrorHandler gShaderErrorHandler;
74
jvanverth34524262016-05-04 13:49:13 -070075using namespace sk_app;
76
csmartdalton61cd31a2017-02-27 17:00:53 -070077static std::map<GpuPathRenderers, std::string> gPathRendererNames;
78
jvanverth9f372462016-04-06 06:08:59 -070079Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070080 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070081}
82
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060083static DEFINE_string(slide, "", "Start on this sample.");
84static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050085
Jim Van Verth682a2f42020-05-13 16:54:09 -040086#ifdef SK_GL
87#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -070088#else
Jim Van Verth682a2f42020-05-13 16:54:09 -040089#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -070090#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -040091#ifdef SK_VULKAN
92#define VK_BACKEND_STR ", \"vk\""
93#else
94#define VK_BACKEND_STR
95#endif
96#ifdef SK_METAL
97#define MTL_BACKEND_STR ", \"mtl\""
98#else
99#define MTL_BACKEND_STR
100#endif
101#ifdef SK_DIRECT3D
102#define D3D_BACKEND_STR ", \"d3d\""
103#else
104#define D3D_BACKEND_STR
105#endif
106#ifdef SK_DAWN
107#define DAWN_BACKEND_STR ", \"dawn\""
108#else
109#define DAWN_BACKEND_STR
110#endif
111#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
112#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
113 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700114
Brian Osman2dd96932016-10-18 15:33:53 -0400115static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700116
Mike Klein5b3f3432019-03-21 11:42:21 -0500117static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -0700118
Mike Klein84836b72019-03-21 11:31:36 -0500119static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700120
Mike Klein84836b72019-03-21 11:31:36 -0500121static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400122
Mike Kleinc6142d82019-03-25 10:54:59 -0500123static DEFINE_string2(match, m, nullptr,
124 "[~][^]substring[$] [...] of name to run.\n"
125 "Multiple matches may be separated by spaces.\n"
126 "~ causes a matching name to always be skipped\n"
127 "^ requires the start of the name to match\n"
128 "$ requires the end of the name to match\n"
129 "^ and $ requires an exact match\n"
130 "If a name does not match any list entry,\n"
131 "it is skipped unless some list entry starts with ~");
132
Mike Klein19fb3972019-03-21 13:08:08 -0500133#if defined(SK_BUILD_FOR_ANDROID)
134 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500135 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
136 static DEFINE_string(lotties, "/data/local/tmp/lotties",
137 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500138#else
139 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500140 static DEFINE_string(skps, "skps", "Directory to read skps from.");
141 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500142#endif
143
Mike Kleinc6142d82019-03-25 10:54:59 -0500144static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
145
146static DEFINE_int_2(threads, j, -1,
147 "Run threadsafe tests on a threadpool with this many extra threads, "
148 "defaulting to one extra thread per core.");
149
Jim Van Verth7b558182019-11-14 16:47:01 -0500150static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
151
Chris Daltonc8877332020-01-06 09:48:30 -0700152static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Reed862818b2020-03-21 15:07:13 -0400153static DEFINE_bool(skvm, false, "Try to use skvm blitters for raster.");
Mike Klein1e0884d2020-04-28 15:04:16 -0500154static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500155static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500156
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400157#ifndef SK_GL
158static_assert(false, "viewer requires GL backend for raster.")
159#endif
160
Brian Salomon194db172017-08-17 14:37:06 -0400161const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700162 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400163#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
164 "ANGLE",
165#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400166#ifdef SK_DAWN
167 "Dawn",
168#endif
jvanverth063ece72016-06-17 09:29:14 -0700169#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700170 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700171#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400172#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500173 "Metal",
174#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400175#ifdef SK_DIRECT3D
176 "Direct3D",
177#endif
csmartdalton578f0642017-02-24 16:04:47 -0700178 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700179};
180
bsalomon6c471f72016-07-26 12:56:32 -0700181static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400182#ifdef SK_DAWN
183 if (0 == strcmp(str, "dawn")) {
184 return sk_app::Window::kDawn_BackendType;
185 } else
186#endif
bsalomon6c471f72016-07-26 12:56:32 -0700187#ifdef SK_VULKAN
188 if (0 == strcmp(str, "vk")) {
189 return sk_app::Window::kVulkan_BackendType;
190 } else
191#endif
Brian Salomon194db172017-08-17 14:37:06 -0400192#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
193 if (0 == strcmp(str, "angle")) {
194 return sk_app::Window::kANGLE_BackendType;
195 } else
196#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400197#ifdef SK_METAL
198 if (0 == strcmp(str, "mtl")) {
199 return sk_app::Window::kMetal_BackendType;
200 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500201#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400202#ifdef SK_DIRECT3D
203 if (0 == strcmp(str, "d3d")) {
204 return sk_app::Window::kDirect3D_BackendType;
205 } else
206#endif
207
bsalomon6c471f72016-07-26 12:56:32 -0700208 if (0 == strcmp(str, "gl")) {
209 return sk_app::Window::kNativeGL_BackendType;
210 } else if (0 == strcmp(str, "sw")) {
211 return sk_app::Window::kRaster_BackendType;
212 } else {
213 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
214 return sk_app::Window::kRaster_BackendType;
215 }
216}
217
Brian Osmana109e392017-02-24 09:49:14 -0500218static SkColorSpacePrimaries gSrgbPrimaries = {
219 0.64f, 0.33f,
220 0.30f, 0.60f,
221 0.15f, 0.06f,
222 0.3127f, 0.3290f };
223
224static SkColorSpacePrimaries gAdobePrimaries = {
225 0.64f, 0.33f,
226 0.21f, 0.71f,
227 0.15f, 0.06f,
228 0.3127f, 0.3290f };
229
230static SkColorSpacePrimaries gP3Primaries = {
231 0.680f, 0.320f,
232 0.265f, 0.690f,
233 0.150f, 0.060f,
234 0.3127f, 0.3290f };
235
236static SkColorSpacePrimaries gRec2020Primaries = {
237 0.708f, 0.292f,
238 0.170f, 0.797f,
239 0.131f, 0.046f,
240 0.3127f, 0.3290f };
241
242struct NamedPrimaries {
243 const char* fName;
244 SkColorSpacePrimaries* fPrimaries;
245} gNamedPrimaries[] = {
246 { "sRGB", &gSrgbPrimaries },
247 { "AdobeRGB", &gAdobePrimaries },
248 { "P3", &gP3Primaries },
249 { "Rec. 2020", &gRec2020Primaries },
250};
251
252static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
253 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
254}
255
Brian Osman70d2f432017-11-08 09:54:10 -0500256static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
257 // In raster mode, we still use GL for the window.
258 // This lets us render the GUI faster (and correct).
259 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
260}
261
Jim Van Verth74826c82019-03-01 14:37:30 -0500262class NullSlide : public Slide {
263 SkISize getDimensions() const override {
264 return SkISize::Make(640, 480);
265 }
266
267 void draw(SkCanvas* canvas) override {
268 canvas->clear(0xffff11ff);
269 }
270};
271
John Stiles31964fd2020-05-05 16:05:47 -0400272static const char kName[] = "name";
273static const char kValue[] = "value";
274static const char kOptions[] = "options";
275static const char kSlideStateName[] = "Slide";
276static const char kBackendStateName[] = "Backend";
277static const char kMSAAStateName[] = "MSAA";
278static const char kPathRendererStateName[] = "Path renderer";
279static const char kSoftkeyStateName[] = "Softkey";
280static const char kSoftkeyHint[] = "Please select a softkey";
281static const char kON[] = "ON";
282static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700283
Mike Reed862818b2020-03-21 15:07:13 -0400284extern bool gUseSkVMBlitter;
Mike Klein1e0884d2020-04-28 15:04:16 -0500285extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400286
jvanverth34524262016-05-04 13:49:13 -0700287Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500288 : fCurrentSlide(-1)
289 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500290 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400291 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500292 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500293 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500294 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500295 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400296 , fZoomWindowFixed(false)
297 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500298 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400299 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700300 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500301 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500302 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500303 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500304 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700305 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400306 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400307 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400308 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500309 , fTiled(false)
310 , fDrawTileBoundaries(false)
311 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400312 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700313{
Greg Daniel285db442016-10-14 09:12:53 -0400314 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700315
Chris Dalton37ae4b02019-12-28 14:51:11 -0700316 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600317 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500318 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500319 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600320 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600321 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500322 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700323
jvanverth2bb3b6d2016-04-08 07:24:09 -0700324 SkDebugf("Command line arguments: ");
325 for (int i = 1; i < argc; ++i) {
326 SkDebugf("%s ", argv[i]);
327 }
328 SkDebugf("\n");
329
Mike Klein88544fb2019-03-20 10:50:33 -0500330 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500331#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400332 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500333#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700334
Mike Reed862818b2020-03-21 15:07:13 -0400335 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein1e0884d2020-04-28 15:04:16 -0500336 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400337
Mike Klein19cc0f62019-03-22 15:30:07 -0500338 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500339
Brian Osmanbc8150f2017-07-24 11:38:01 -0400340 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400341 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400342
bsalomon6c471f72016-07-26 12:56:32 -0700343 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700344 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700345
csmartdalton578f0642017-02-24 16:04:47 -0700346 DisplayParams displayParams;
347 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700348 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400349 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400350 displayParams.fGrContextOptions.fShaderCacheStrategy =
351 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400352 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
353 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700354 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500355 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700356
Brian Osman56a24812017-12-19 11:15:16 -0500357 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500358 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500359 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
360 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
361 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
362
jvanverth9f372462016-04-06 06:08:59 -0700363 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700364 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500365 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500366 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500367 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700368
brianosman622c8d52016-05-10 06:50:49 -0700369 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500370 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
371 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
372 fWindow->inval();
373 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500374 // Command to jump directly to the slide picker and give it focus
375 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
376 this->fShowImGuiDebugWindow = true;
377 this->fShowSlidePicker = true;
378 fWindow->inval();
379 });
380 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400381 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500382 this->fShowImGuiDebugWindow = true;
383 this->fShowSlidePicker = true;
384 fWindow->inval();
385 });
Brian Osman79086b92017-02-10 13:36:16 -0500386 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
387 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
388 fWindow->inval();
389 });
Brian Osmanf6877092017-02-13 09:39:57 -0500390 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
391 this->fShowZoomWindow = !this->fShowZoomWindow;
392 fWindow->inval();
393 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400394 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
395 this->fZoomWindowFixed = !this->fZoomWindowFixed;
396 fWindow->inval();
397 });
Greg Danield0794cc2019-03-27 16:23:26 -0400398 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
399 DisplayParams params = fWindow->getRequestedDisplayParams();
400 params.fDisableVsync = !params.fDisableVsync;
401 fWindow->setRequestedDisplayParams(params);
402 this->updateTitle();
403 fWindow->inval();
404 });
Mike Reedf702ed42019-07-22 17:00:49 -0400405 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
406 fRefresh = !fRefresh;
407 fWindow->inval();
408 });
brianosman622c8d52016-05-10 06:50:49 -0700409 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500410 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700411 fWindow->inval();
412 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400413 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500414 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400415 this->updateTitle();
416 fWindow->inval();
417 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500418 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500419 switch (fColorMode) {
420 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500421 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500422 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500423 case ColorMode::kColorManaged8888:
424 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500425 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500426 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400427 this->setColorMode(ColorMode::kColorManagedF16Norm);
428 break;
429 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500430 this->setColorMode(ColorMode::kLegacy);
431 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500432 }
brianosman622c8d52016-05-10 06:50:49 -0700433 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700434 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
435 DisplayParams params = fWindow->getRequestedDisplayParams();
436 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
437 fWindow->setRequestedDisplayParams(params);
438 fWindow->inval();
439 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400440 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500441 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700442 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400443 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500444 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700445 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400446 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700447 this->changeZoomLevel(1.f / 32.f);
448 fWindow->inval();
449 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400450 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700451 this->changeZoomLevel(-1.f / 32.f);
452 fWindow->inval();
453 });
jvanverthaf236b52016-05-20 06:01:06 -0700454 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400455 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
456 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500457 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400458#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
459 if (newBackend == sk_app::Window::kVulkan_BackendType) {
460 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
461 sk_app::Window::kBackendTypeCount);
462 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
463 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500464 }
465#endif
Brian Osman621491e2017-02-28 15:45:01 -0500466 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700467 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500468 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
469 fSaveToSKP = true;
470 fWindow->inval();
471 });
Mike Reed376d8122019-03-14 11:39:02 -0400472 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
473 fShowSlideDimensions = !fShowSlideDimensions;
474 fWindow->inval();
475 });
Ben Wagner37c54032018-04-13 14:30:23 -0400476 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
477 DisplayParams params = fWindow->getRequestedDisplayParams();
478 uint32_t flags = params.fSurfaceProps.flags();
479 if (!fPixelGeometryOverrides) {
480 fPixelGeometryOverrides = true;
481 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
482 } else {
483 switch (params.fSurfaceProps.pixelGeometry()) {
484 case kUnknown_SkPixelGeometry:
485 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
486 break;
487 case kRGB_H_SkPixelGeometry:
488 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
489 break;
490 case kBGR_H_SkPixelGeometry:
491 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
492 break;
493 case kRGB_V_SkPixelGeometry:
494 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
495 break;
496 case kBGR_V_SkPixelGeometry:
497 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
498 fPixelGeometryOverrides = false;
499 break;
500 }
501 }
502 fWindow->setRequestedDisplayParams(params);
503 this->updateTitle();
504 fWindow->inval();
505 });
Ben Wagner9613e452019-01-23 10:34:59 -0500506 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500507 if (!fFontOverrides.fHinting) {
508 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400509 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500510 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500511 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400512 case SkFontHinting::kNone:
513 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500514 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400515 case SkFontHinting::kSlight:
516 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500517 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400518 case SkFontHinting::kNormal:
519 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500520 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400521 case SkFontHinting::kFull:
522 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500523 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500524 break;
525 }
526 }
527 this->updateTitle();
528 fWindow->inval();
529 });
530 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500531 if (!fPaintOverrides.fAntiAlias) {
532 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
533 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500534 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500535 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500536 } else {
537 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500538 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500539 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500540 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400541 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500542 break;
543 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500544 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500545 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400546 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500547 break;
548 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500549 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400550 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500551 break;
552 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500553 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
554 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500555 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
556 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500557 break;
558 }
559 }
560 this->updateTitle();
561 fWindow->inval();
562 });
Ben Wagner37c54032018-04-13 14:30:23 -0400563 fCommands.addCommand('D', "Modes", "DFT", [this]() {
564 DisplayParams params = fWindow->getRequestedDisplayParams();
565 uint32_t flags = params.fSurfaceProps.flags();
566 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
567 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
568 fWindow->setRequestedDisplayParams(params);
569 this->updateTitle();
570 fWindow->inval();
571 });
Ben Wagner9613e452019-01-23 10:34:59 -0500572 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500573 if (!fFontOverrides.fEdging) {
574 fFontOverrides.fEdging = true;
575 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500576 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500577 switch (fFont.getEdging()) {
578 case SkFont::Edging::kAlias:
579 fFont.setEdging(SkFont::Edging::kAntiAlias);
580 break;
581 case SkFont::Edging::kAntiAlias:
582 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
583 break;
584 case SkFont::Edging::kSubpixelAntiAlias:
585 fFont.setEdging(SkFont::Edging::kAlias);
586 fFontOverrides.fEdging = false;
587 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500588 }
589 }
590 this->updateTitle();
591 fWindow->inval();
592 });
Ben Wagner9613e452019-01-23 10:34:59 -0500593 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500594 if (!fFontOverrides.fSubpixel) {
595 fFontOverrides.fSubpixel = true;
596 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500597 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500598 if (!fFont.isSubpixel()) {
599 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500600 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500601 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500602 }
603 }
604 this->updateTitle();
605 fWindow->inval();
606 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400607 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
608 if (!fFontOverrides.fBaselineSnap) {
609 fFontOverrides.fBaselineSnap = true;
610 fFont.setBaselineSnap(false);
611 } else {
612 if (!fFont.isBaselineSnap()) {
613 fFont.setBaselineSnap(true);
614 } else {
615 fFontOverrides.fBaselineSnap = false;
616 }
617 }
618 this->updateTitle();
619 fWindow->inval();
620 });
Brian Osman805a7272018-05-02 15:40:20 -0400621 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
622 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
623 : kPerspective_Real;
624 this->updateTitle();
625 fWindow->inval();
626 });
627 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
628 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
629 : kPerspective_Off;
630 this->updateTitle();
631 fWindow->inval();
632 });
Brian Osman207d4102019-01-10 09:40:58 -0500633 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
634 fAnimTimer.togglePauseResume();
635 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400636 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
637 fZoomUI = !fZoomUI;
638 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
639 fWindow->inval();
640 });
Mike Reed59295352020-03-12 13:56:34 -0400641 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
642 fDrawViaSerialize = !fDrawViaSerialize;
643 this->updateTitle();
644 fWindow->inval();
645 });
Mike Reed862818b2020-03-21 15:07:13 -0400646 fCommands.addCommand('!', "SkVM", "Toggle SkVM", [this]() {
647 gUseSkVMBlitter = !gUseSkVMBlitter;
648 this->updateTitle();
649 fWindow->inval();
650 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500651
jvanverth2bb3b6d2016-04-08 07:24:09 -0700652 // set up slides
653 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500654 if (FLAGS_list) {
655 this->listNames();
656 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700657
Brian Osman9bb47cf2018-04-26 15:55:00 -0400658 fPerspectivePoints[0].set(0, 0);
659 fPerspectivePoints[1].set(1, 0);
660 fPerspectivePoints[2].set(0, 1);
661 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700662 fAnimTimer.run();
663
Hal Canaryc465d132017-12-08 10:21:31 -0500664 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500665 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400666 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500667 }
668 fImGuiGamutPaint.setColor(SK_ColorWHITE);
669 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
670
jongdeok.kim804f17e2019-02-26 14:39:23 +0900671 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500672 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700673}
674
jvanverth34524262016-05-04 13:49:13 -0700675void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400676 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
677 static const struct {
678 const char* fExtension;
679 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500680 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400681 const SlideFactory fFactory;
682 } gExternalSlidesInfo[] = {
683 { ".skp", "skp-dir", FLAGS_skps,
684 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
685 return sk_make_sp<SKPSlide>(name, path);}
686 },
687 { ".jpg", "jpg-dir", FLAGS_jpgs,
688 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
689 return sk_make_sp<ImageSlide>(name, path);}
690 },
Florin Malita87ccf332018-05-04 12:23:24 -0400691#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400692 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400693 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
694 return sk_make_sp<SkottieSlide>(name, path);}
695 },
Florin Malita87ccf332018-05-04 12:23:24 -0400696#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400697#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400698 { ".svg", "svg-dir", FLAGS_svgs,
699 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
700 return sk_make_sp<SvgSlide>(name, path);}
701 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400702#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400703 };
jvanverthc265a922016-04-08 12:51:45 -0700704
Brian Salomon343553a2018-09-05 15:41:23 -0400705 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700706
Mike Klein88544fb2019-03-20 10:50:33 -0500707 const auto addSlide =
708 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
709 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
710 return;
711 }
liyuqian6f163d22016-06-13 12:26:45 -0700712
Mike Klein88544fb2019-03-20 10:50:33 -0500713 if (auto slide = fact(name, path)) {
714 dirSlides.push_back(slide);
715 fSlides.push_back(std::move(slide));
716 }
717 };
Florin Malita76a076b2018-02-15 18:40:48 -0500718
Florin Malita38792ce2018-05-08 10:36:18 -0400719 if (!FLAGS_file.isEmpty()) {
720 // single file mode
721 const SkString file(FLAGS_file[0]);
722
723 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
724 for (const auto& sinfo : gExternalSlidesInfo) {
725 if (file.endsWith(sinfo.fExtension)) {
726 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
727 return;
728 }
729 }
730
731 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
732 } else {
733 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
734 }
735
736 return;
737 }
738
739 // Bisect slide.
740 if (!FLAGS_bisect.isEmpty()) {
741 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500742 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400743 if (FLAGS_bisect.count() >= 2) {
744 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
745 bisect->onChar(*ch);
746 }
747 }
748 fSlides.push_back(std::move(bisect));
749 }
750 }
751
752 // GMs
753 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400754 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400755 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500756 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400757 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400758 fSlides.push_back(std::move(slide));
759 }
Florin Malita38792ce2018-05-08 10:36:18 -0400760 }
761 // reverse gms
762 int numGMs = fSlides.count() - firstGM;
763 for (int i = 0; i < numGMs/2; ++i) {
764 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
765 }
766
767 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400768 for (const SampleFactory factory : SampleRegistry::Range()) {
769 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500770 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400771 fSlides.push_back(slide);
772 }
Florin Malita38792ce2018-05-08 10:36:18 -0400773 }
774
Brian Osman7c979f52019-02-12 13:27:51 -0500775 // Particle demo
776 {
777 // TODO: Convert this to a sample
778 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500779 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500780 fSlides.push_back(std::move(slide));
781 }
782 }
783
Brian Osmand927bd22019-12-18 11:23:12 -0500784 // Runtime shader editor
785 {
786 sk_sp<Slide> slide(new SkSLSlide());
787 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
788 fSlides.push_back(std::move(slide));
789 }
790 }
791
Florin Malita0ffa3222018-04-05 14:34:45 -0400792 for (const auto& info : gExternalSlidesInfo) {
793 for (const auto& flag : info.fFlags) {
794 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
795 // single file
796 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
797 } else {
798 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400799 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400800 SkTArray<SkString> sortedFilenames;
801 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400802 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400803 sortedFilenames.push_back(name);
804 }
805 if (sortedFilenames.count()) {
806 SkTQSort(sortedFilenames.begin(), sortedFilenames.end() - 1,
807 [](const SkString& a, const SkString& b) {
808 return strcmp(a.c_str(), b.c_str()) < 0;
809 });
810 }
811 for (const SkString& filename : sortedFilenames) {
812 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
813 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400814 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400815 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400816 if (!dirSlides.empty()) {
817 fSlides.push_back(
818 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
819 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500820 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400821 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400822 }
823 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500824
825 if (!fSlides.count()) {
826 sk_sp<Slide> slide(new NullSlide());
827 fSlides.push_back(std::move(slide));
828 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700829}
830
831
jvanverth34524262016-05-04 13:49:13 -0700832Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700833 fWindow->detach();
834 delete fWindow;
835}
836
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500837struct SkPaintTitleUpdater {
838 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
839 void append(const char* s) {
840 if (fCount == 0) {
841 fTitle->append(" {");
842 } else {
843 fTitle->append(", ");
844 }
845 fTitle->append(s);
846 ++fCount;
847 }
848 void done() {
849 if (fCount > 0) {
850 fTitle->append("}");
851 }
852 }
853 SkString* fTitle;
854 int fCount;
855};
856
brianosman05de2162016-05-06 13:28:57 -0700857void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700858 if (!fWindow) {
859 return;
860 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500861 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700862 return; // Surface hasn't been created yet.
863 }
864
jvanverth34524262016-05-04 13:49:13 -0700865 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700866 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700867
Mike Kleine5acd752019-03-22 09:57:16 -0500868 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400869 if (gSkForceAnalyticAA) {
870 title.append(" <FAAA>");
871 } else {
872 title.append(" <AAA>");
873 }
874 }
Mike Reed59295352020-03-12 13:56:34 -0400875 if (fDrawViaSerialize) {
876 title.append(" <serialize>");
877 }
Mike Reed862818b2020-03-21 15:07:13 -0400878 if (gUseSkVMBlitter) {
879 title.append(" <skvm>");
880 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400881
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500882 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500883 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
884 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400885 const char* on, const char* off)
886 {
Ben Wagner9613e452019-01-23 10:34:59 -0500887 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400888 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500889 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400890 };
891
Ben Wagner9613e452019-01-23 10:34:59 -0500892 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
893 const char* on, const char* off)
894 {
895 if (fFontOverrides.*flag) {
896 paintTitle.append((fFont.*isFlag)() ? on : off);
897 }
898 };
899
900 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
901 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500902 if (fPaintOverrides.fFilterQuality) {
903 switch (fPaint.getFilterQuality()) {
904 case kNone_SkFilterQuality:
905 paintTitle.append("NoFilter");
906 break;
907 case kLow_SkFilterQuality:
908 paintTitle.append("LowFilter");
909 break;
910 case kMedium_SkFilterQuality:
911 paintTitle.append("MediumFilter");
912 break;
913 case kHigh_SkFilterQuality:
914 paintTitle.append("HighFilter");
915 break;
916 }
917 }
Ben Wagner9613e452019-01-23 10:34:59 -0500918
919 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
920 "Force Autohint", "No Force Autohint");
921 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400922 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500923 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
924 "Linear Metrics", "Non-Linear Metrics");
925 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
926 "Bitmap Text", "No Bitmap Text");
927 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
928
929 if (fFontOverrides.fEdging) {
930 switch (fFont.getEdging()) {
931 case SkFont::Edging::kAlias:
932 paintTitle.append("Alias Text");
933 break;
934 case SkFont::Edging::kAntiAlias:
935 paintTitle.append("Antialias Text");
936 break;
937 case SkFont::Edging::kSubpixelAntiAlias:
938 paintTitle.append("Subpixel Antialias Text");
939 break;
940 }
941 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400942
Mike Reed3ae47332019-01-04 10:11:46 -0500943 if (fFontOverrides.fHinting) {
944 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400945 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500946 paintTitle.append("No Hinting");
947 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400948 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500949 paintTitle.append("Slight Hinting");
950 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400951 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500952 paintTitle.append("Normal Hinting");
953 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400954 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500955 paintTitle.append("Full Hinting");
956 break;
957 }
958 }
959 paintTitle.done();
960
Brian Osman92004802017-03-06 11:47:26 -0500961 switch (fColorMode) {
962 case ColorMode::kLegacy:
963 title.append(" Legacy 8888");
964 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500965 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500966 title.append(" ColorManaged 8888");
967 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500968 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500969 title.append(" ColorManaged F16");
970 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400971 case ColorMode::kColorManagedF16Norm:
972 title.append(" ColorManaged F16 Norm");
973 break;
Brian Osman92004802017-03-06 11:47:26 -0500974 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500975
Brian Osman92004802017-03-06 11:47:26 -0500976 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500977 int curPrimaries = -1;
978 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
979 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
980 curPrimaries = i;
981 break;
982 }
983 }
Brian Osman03115dc2018-11-26 13:55:19 -0500984 title.appendf(" %s Gamma %f",
985 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500986 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700987 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500988
Ben Wagner37c54032018-04-13 14:30:23 -0400989 const DisplayParams& params = fWindow->getRequestedDisplayParams();
990 if (fPixelGeometryOverrides) {
991 switch (params.fSurfaceProps.pixelGeometry()) {
992 case kUnknown_SkPixelGeometry:
993 title.append( " Flat");
994 break;
995 case kRGB_H_SkPixelGeometry:
996 title.append( " RGB");
997 break;
998 case kBGR_H_SkPixelGeometry:
999 title.append( " BGR");
1000 break;
1001 case kRGB_V_SkPixelGeometry:
1002 title.append( " RGBV");
1003 break;
1004 case kBGR_V_SkPixelGeometry:
1005 title.append( " BGRV");
1006 break;
1007 }
1008 }
1009
1010 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1011 title.append(" DFT");
1012 }
1013
csmartdalton578f0642017-02-24 16:04:47 -07001014 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001015 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001016 int msaa = fWindow->sampleCount();
1017 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001018 title.appendf(" MSAA: %i", msaa);
1019 }
1020 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001021
1022 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001023 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001024 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1025 }
1026
Brian Osman805a7272018-05-02 15:40:20 -04001027 if (kPerspective_Real == fPerspectiveMode) {
1028 title.append(" Perpsective (Real)");
1029 } else if (kPerspective_Fake == fPerspectiveMode) {
1030 title.append(" Perspective (Fake)");
1031 }
1032
brianosman05de2162016-05-06 13:28:57 -07001033 fWindow->setTitle(title.c_str());
1034}
1035
Florin Malitaab99c342018-01-16 16:23:03 -05001036int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001037
1038 if (!FLAGS_slide.isEmpty()) {
1039 int count = fSlides.count();
1040 for (int i = 0; i < count; i++) {
1041 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001042 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001043 }
1044 }
1045
1046 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1047 this->listNames();
1048 }
1049
Florin Malitaab99c342018-01-16 16:23:03 -05001050 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001051}
1052
Florin Malitaab99c342018-01-16 16:23:03 -05001053void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001054 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001055 for (const auto& slide : fSlides) {
1056 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001057 }
1058}
1059
Florin Malitaab99c342018-01-16 16:23:03 -05001060void Viewer::setCurrentSlide(int slide) {
1061 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001062
Florin Malitaab99c342018-01-16 16:23:03 -05001063 if (slide == fCurrentSlide) {
1064 return;
1065 }
1066
1067 if (fCurrentSlide >= 0) {
1068 fSlides[fCurrentSlide]->unload();
1069 }
1070
1071 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1072 SkIntToScalar(fWindow->height()));
1073 fCurrentSlide = slide;
1074 this->setupCurrentSlide();
1075}
1076
1077void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001078 if (fCurrentSlide >= 0) {
1079 // prepare dimensions for image slides
1080 fGesture.resetTouchState();
1081 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001082
Jim Van Verth0848fb02018-01-22 13:39:30 -05001083 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1084 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1085 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001086
Jim Van Verth0848fb02018-01-22 13:39:30 -05001087 // Start with a matrix that scales the slide to the available screen space
1088 if (fWindow->scaleContentToFit()) {
1089 if (windowRect.width() > 0 && windowRect.height() > 0) {
1090 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1091 }
liyuqiane46e4f02016-05-20 07:32:19 -07001092 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001093
1094 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001095 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001096
1097 this->updateTitle();
1098 this->updateUIState();
1099
1100 fStatsLayer.resetMeasurements();
1101
1102 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001103 }
jvanverthc265a922016-04-08 12:51:45 -07001104}
1105
Brian Osmanaba642c2020-02-06 12:52:25 -05001106#define MAX_ZOOM_LEVEL 8.0f
1107#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001108
jvanverth34524262016-05-04 13:49:13 -07001109void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001110 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001111 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001112 this->preTouchMatrixChanged();
1113}
Yuqian Li755778c2018-03-28 16:23:31 -04001114
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001115void Viewer::preTouchMatrixChanged() {
1116 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001117 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1118 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1119 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1120 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1121}
1122
Brian Osman805a7272018-05-02 15:40:20 -04001123SkMatrix Viewer::computePerspectiveMatrix() {
1124 SkScalar w = fWindow->width(), h = fWindow->height();
1125 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1126 SkPoint perspPts[4] = {
1127 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1128 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1129 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1130 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1131 };
1132 SkMatrix m;
1133 m.setPolyToPoly(orthoPts, perspPts, 4);
1134 return m;
1135}
1136
Yuqian Li755778c2018-03-28 16:23:31 -04001137SkMatrix Viewer::computePreTouchMatrix() {
1138 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001139
1140 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001141 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001142 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001143
1144 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1145 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001146
Brian Osman805a7272018-05-02 15:40:20 -04001147 if (kPerspective_Real == fPerspectiveMode) {
1148 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001149 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001150 }
1151
Yuqian Li755778c2018-03-28 16:23:31 -04001152 return m;
jvanverthc265a922016-04-08 12:51:45 -07001153}
1154
liyuqiand3cdbca2016-05-17 12:44:20 -07001155SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001156 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001157 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001158 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001159 return m;
jvanverthc265a922016-04-08 12:51:45 -07001160}
1161
Brian Osman621491e2017-02-28 15:45:01 -05001162void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001163 fPersistentCache.reset();
1164 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001165 fBackendType = backendType;
1166
1167 fWindow->detach();
1168
Brian Osman70d2f432017-11-08 09:54:10 -05001169#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001170 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1171 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001172 DisplayParams params = fWindow->getRequestedDisplayParams();
1173 delete fWindow;
1174 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001175
Brian Osman70d2f432017-11-08 09:54:10 -05001176 // re-register callbacks
1177 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001178 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001179 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001180 fWindow->pushLayer(&fImGuiLayer);
1181
Brian Osman70d2f432017-11-08 09:54:10 -05001182 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1183 // will still include our correct sample count. But the re-created fWindow will lose that
1184 // information. On Windows, we need to re-create the window when changing sample count,
1185 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1186 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1187 // as if we had never changed windows at all).
1188 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001189#endif
1190
Brian Osman70d2f432017-11-08 09:54:10 -05001191 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001192}
1193
Brian Osman92004802017-03-06 11:47:26 -05001194void Viewer::setColorMode(ColorMode colorMode) {
1195 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001196 this->updateTitle();
1197 fWindow->inval();
1198}
1199
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001200class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1201public:
Mike Reed3ae47332019-01-04 10:11:46 -05001202 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1203 SkFont* font, Viewer::SkFontFields* ffields)
1204 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001205 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001206 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1207 sk_sp<SkTextBlob>* cache) {
1208 bool blobWillChange = false;
1209 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001210 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1211 bool shouldDraw = this->filterFont(&filteredFont);
1212 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001213 blobWillChange = true;
1214 break;
1215 }
1216 }
1217 if (!blobWillChange) {
1218 return blob;
1219 }
1220
1221 SkTextBlobBuilder builder;
1222 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001223 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1224 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001225 if (!shouldDraw) {
1226 continue;
1227 }
1228
Mike Reed3ae47332019-01-04 10:11:46 -05001229 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001230
Ben Wagner41e40472018-09-24 13:01:54 -04001231 const SkTextBlobBuilder::RunBuffer& runBuffer
1232 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001233 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001234 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001235 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001236 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001237 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001238 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001239 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001240 it.glyphCount(), it.textSize(), SkString())
1241 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1242 uint32_t glyphCount = it.glyphCount();
1243 if (it.glyphs()) {
1244 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1245 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1246 }
1247 if (it.pos()) {
1248 size_t posSize = sizeof(decltype(*it.pos()));
1249 uint8_t positioning = it.positioning();
1250 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1251 }
1252 if (it.text()) {
1253 size_t textSize = sizeof(decltype(*it.text()));
1254 uint32_t textCount = it.textSize();
1255 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1256 }
1257 if (it.clusters()) {
1258 size_t clusterSize = sizeof(decltype(*it.clusters()));
1259 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1260 }
1261 }
1262 *cache = builder.make();
1263 return cache->get();
1264 }
1265 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1266 const SkPaint& paint) override {
1267 sk_sp<SkTextBlob> cache;
1268 this->SkPaintFilterCanvas::onDrawTextBlob(
1269 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1270 }
Mike Reed3ae47332019-01-04 10:11:46 -05001271 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001272 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001273 font->writable()->setSize(fFont->getSize());
1274 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001275 if (fFontOverrides->fScaleX) {
1276 font->writable()->setScaleX(fFont->getScaleX());
1277 }
1278 if (fFontOverrides->fSkewX) {
1279 font->writable()->setSkewX(fFont->getSkewX());
1280 }
Mike Reed3ae47332019-01-04 10:11:46 -05001281 if (fFontOverrides->fHinting) {
1282 font->writable()->setHinting(fFont->getHinting());
1283 }
Ben Wagner9613e452019-01-23 10:34:59 -05001284 if (fFontOverrides->fEdging) {
1285 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001286 }
Ben Wagner9613e452019-01-23 10:34:59 -05001287 if (fFontOverrides->fEmbolden) {
1288 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001289 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001290 if (fFontOverrides->fBaselineSnap) {
1291 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1292 }
Ben Wagner9613e452019-01-23 10:34:59 -05001293 if (fFontOverrides->fLinearMetrics) {
1294 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001295 }
Ben Wagner9613e452019-01-23 10:34:59 -05001296 if (fFontOverrides->fSubpixel) {
1297 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001298 }
Ben Wagner9613e452019-01-23 10:34:59 -05001299 if (fFontOverrides->fEmbeddedBitmaps) {
1300 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001301 }
Ben Wagner9613e452019-01-23 10:34:59 -05001302 if (fFontOverrides->fForceAutoHinting) {
1303 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001304 }
Ben Wagner9613e452019-01-23 10:34:59 -05001305
Mike Reed3ae47332019-01-04 10:11:46 -05001306 return true;
1307 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001308 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001309 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001310 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001311 }
Ben Wagner9613e452019-01-23 10:34:59 -05001312 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001313 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001314 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001315 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001316 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001317 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001318 return true;
1319 }
1320 SkPaint* fPaint;
1321 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001322 SkFont* fFont;
1323 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001324};
1325
Robert Phillips9882dae2019-03-04 11:00:10 -05001326void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001327 if (fCurrentSlide < 0) {
1328 return;
1329 }
1330
Robert Phillips9882dae2019-03-04 11:00:10 -05001331 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001332
Brian Osmanf750fbc2017-02-08 10:47:28 -05001333 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001334 SkSurface* slideSurface = surface;
1335 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001336 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001337
Brian Osmane0d4fba2017-03-15 10:24:55 -04001338 // 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 -05001339 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001340 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001341 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001342 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001343 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001344 }
1345
Brian Osman3ac99cf2017-12-01 11:23:53 -05001346 if (fSaveToSKP) {
1347 SkPictureRecorder recorder;
1348 SkCanvas* recorderCanvas = recorder.beginRecording(
1349 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001350 fSlides[fCurrentSlide]->draw(recorderCanvas);
1351 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1352 SkFILEWStream stream("sample_app.skp");
1353 picture->serialize(&stream);
1354 fSaveToSKP = false;
1355 }
1356
Brian Osmane9ed0f02018-11-26 14:50:05 -05001357 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001358 SkColorType colorType;
1359 switch (fColorMode) {
1360 case ColorMode::kLegacy:
1361 case ColorMode::kColorManaged8888:
1362 colorType = kN32_SkColorType;
1363 break;
1364 case ColorMode::kColorManagedF16:
1365 colorType = kRGBA_F16_SkColorType;
1366 break;
1367 case ColorMode::kColorManagedF16Norm:
1368 colorType = kRGBA_F16Norm_SkColorType;
1369 break;
1370 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001371
1372 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001373 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1374 slideCanvas->getProps(&props);
1375
Brian Osmane9ed0f02018-11-26 14:50:05 -05001376 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1377 return Window::kRaster_BackendType == this->fBackendType
1378 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001379 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001380 };
1381
Brian Osman03115dc2018-11-26 13:55:19 -05001382 // We need to render offscreen if we're...
1383 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1384 // ... in any raster mode, because the window surface is actually GL
1385 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001386 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001387 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001388 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001389 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001390 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001391 colorSpace != nullptr ||
1392 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001393
Brian Osmane9ed0f02018-11-26 14:50:05 -05001394 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001395 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001396 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001397 }
1398
Mike Reed59295352020-03-12 13:56:34 -04001399 SkPictureRecorder recorder;
1400 SkCanvas* recorderRestoreCanvas = nullptr;
1401 if (fDrawViaSerialize) {
1402 recorderRestoreCanvas = slideCanvas;
1403 slideCanvas = recorder.beginRecording(
1404 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1405 }
1406
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001407 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001408 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001409 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001410 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001411 if (fTiled) {
1412 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1413 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001414 for (int y = 0; y < fWindow->height(); y += tileH) {
1415 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001416 SkAutoCanvasRestore acr(slideCanvas, true);
1417 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1418 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001419 }
1420 }
1421
1422 // Draw borders between tiles
1423 if (fDrawTileBoundaries) {
1424 SkPaint border;
1425 border.setColor(0x60FF00FF);
1426 border.setStyle(SkPaint::kStroke_Style);
1427 for (int y = 0; y < fWindow->height(); y += tileH) {
1428 for (int x = 0; x < fWindow->width(); x += tileW) {
1429 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1430 }
1431 }
1432 }
1433 } else {
1434 slideCanvas->concat(this->computeMatrix());
1435 if (kPerspective_Real == fPerspectiveMode) {
1436 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1437 }
Mike Reed3ae47332019-01-04 10:11:46 -05001438 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001439 fSlides[fCurrentSlide]->draw(&filterCanvas);
1440 }
Brian Osman56a24812017-12-19 11:15:16 -05001441 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001442 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001443
Mike Reed59295352020-03-12 13:56:34 -04001444 if (recorderRestoreCanvas) {
1445 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1446 auto data = picture->serialize();
1447 slideCanvas = recorderRestoreCanvas;
1448 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1449 }
1450
Brian Osman1df161a2017-02-09 12:10:20 -05001451 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001452 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001453 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001454 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001455
1456 // If we rendered offscreen, snap an image and push the results to the window's canvas
1457 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001458 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001459
Robert Phillips9882dae2019-03-04 11:00:10 -05001460 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001461 SkPaint paint;
1462 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001463 int prePerspectiveCount = canvas->save();
1464 if (kPerspective_Fake == fPerspectiveMode) {
1465 paint.setFilterQuality(kHigh_SkFilterQuality);
1466 canvas->clear(SK_ColorWHITE);
1467 canvas->concat(this->computePerspectiveMatrix());
1468 }
Brian Osman03115dc2018-11-26 13:55:19 -05001469 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001470 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001471 }
Mike Reed376d8122019-03-14 11:39:02 -04001472
1473 if (fShowSlideDimensions) {
1474 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1475 SkPaint paint;
1476 paint.setColor(0x40FFFF00);
1477 surface->getCanvas()->drawRect(r, paint);
1478 }
liyuqian6f163d22016-06-13 12:26:45 -07001479}
1480
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001481void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001482 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001483 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001484}
Jim Van Verth6f449692017-02-14 15:16:46 -05001485
Robert Phillips9882dae2019-03-04 11:00:10 -05001486void Viewer::onPaint(SkSurface* surface) {
1487 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001488
Robert Phillips9882dae2019-03-04 11:00:10 -05001489 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001490
Brian Osmand67e5182017-12-08 16:46:09 -05001491 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001492
1493 if (GrContext* ctx = fWindow->getGrContext()) {
1494 // Clean out cache items that haven't been used in more than 10 seconds.
1495 ctx->performDeferredCleanup(std::chrono::seconds(10));
1496 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001497}
1498
Ben Wagnera1915972018-08-09 15:06:19 -04001499void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001500 if (fCurrentSlide >= 0) {
1501 fSlides[fCurrentSlide]->resize(width, height);
1502 }
Ben Wagnera1915972018-08-09 15:06:19 -04001503}
1504
Florin Malitacefc1b92018-02-19 21:43:47 -05001505SkPoint Viewer::mapEvent(float x, float y) {
1506 const auto m = this->computeMatrix();
1507 SkMatrix inv;
1508
1509 SkAssertResult(m.invert(&inv));
1510
1511 return inv.mapXY(x, y);
1512}
1513
Hal Canaryb1f411a2019-08-29 10:39:22 -04001514bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001515 if (GestureDevice::kMouse == fGestureDevice) {
1516 return false;
1517 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001518
1519 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001520 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001521 fWindow->inval();
1522 return true;
1523 }
1524
liyuqiand3cdbca2016-05-17 12:44:20 -07001525 void* castedOwner = reinterpret_cast<void*>(owner);
1526 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001527 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001528 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001529#if defined(SK_BUILD_FOR_IOS)
1530 // TODO: move IOS swipe detection higher up into the platform code
1531 SkPoint dir;
1532 if (fGesture.isFling(&dir)) {
1533 // swiping left or right
1534 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1535 if (dir.fX < 0) {
1536 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1537 fCurrentSlide + 1 : 0);
1538 } else {
1539 this->setCurrentSlide(fCurrentSlide > 0 ?
1540 fCurrentSlide - 1 : fSlides.count() - 1);
1541 }
1542 }
1543 fGesture.reset();
1544 }
1545#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001546 break;
1547 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001548 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001549 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001550 break;
1551 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001552 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001553 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001554 break;
1555 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001556 default: {
1557 // kLeft and kRight are only for swipes
1558 SkASSERT(false);
1559 break;
1560 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001561 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001562 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001563 fWindow->inval();
1564 return true;
1565}
1566
Hal Canaryb1f411a2019-08-29 10:39:22 -04001567bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001568 if (GestureDevice::kTouch == fGestureDevice) {
1569 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001570 }
Brian Osman16c81a12017-12-20 11:58:34 -05001571
Florin Malitacefc1b92018-02-19 21:43:47 -05001572 const auto slidePt = this->mapEvent(x, y);
1573 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1574 fWindow->inval();
1575 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001576 }
1577
1578 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001579 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001580 fGesture.touchEnd(nullptr);
1581 break;
1582 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001583 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001584 fGesture.touchBegin(nullptr, x, y);
1585 break;
1586 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001587 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001588 fGesture.touchMoved(nullptr, x, y);
1589 break;
1590 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001591 default: {
1592 SkASSERT(false); // shouldn't see kRight or kLeft here
1593 break;
1594 }
Brian Osman16c81a12017-12-20 11:58:34 -05001595 }
1596 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1597
Hal Canaryb1f411a2019-08-29 10:39:22 -04001598 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001599 fWindow->inval();
1600 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001601 return true;
1602}
1603
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001604bool Viewer::onFling(skui::InputState state) {
1605 if (skui::InputState::kRight == state) {
1606 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1607 return true;
1608 } else if (skui::InputState::kLeft == state) {
1609 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1610 return true;
1611 }
1612 return false;
1613}
1614
1615bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1616 switch (state) {
1617 case skui::InputState::kDown:
1618 fGesture.startZoom();
1619 return true;
1620 break;
1621 case skui::InputState::kMove:
1622 fGesture.updateZoom(scale, x, y, x, y);
1623 return true;
1624 break;
1625 case skui::InputState::kUp:
1626 fGesture.endZoom();
1627 return true;
1628 break;
1629 default:
1630 SkASSERT(false);
1631 break;
1632 }
1633
1634 return false;
1635}
1636
Brian Osmana109e392017-02-24 09:49:14 -05001637static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001638 // The gamut image covers a (0.8 x 0.9) shaped region
1639 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001640
1641 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1642 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1643 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001644 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1645 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1646 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001647
Brian Osman535c5e32019-02-09 16:32:58 -05001648 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1649 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1650 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1651 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1652 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001653}
1654
Ben Wagner3627d2e2018-06-26 14:23:20 -04001655static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001656 ImGui::DragCanvas dc(pt);
1657 dc.fillColor(IM_COL32(0, 0, 0, 128));
1658 dc.dragPoint(pt);
1659 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001660}
1661
Brian Osman9bb47cf2018-04-26 15:55:00 -04001662static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001663 ImGui::DragCanvas dc(pts);
1664 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001665
Brian Osman535c5e32019-02-09 16:32:58 -05001666 for (int i = 0; i < 4; ++i) {
1667 dc.dragPoint(pts + i);
1668 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001669
Brian Osman535c5e32019-02-09 16:32:58 -05001670 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1671 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1672 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1673 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001674
Brian Osman535c5e32019-02-09 16:32:58 -05001675 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001676}
1677
Brian Osmand67e5182017-12-08 16:46:09 -05001678void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001679 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1680 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001681 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001682 }
1683
1684 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001685 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1686 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001687 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001688 DisplayParams params = fWindow->getRequestedDisplayParams();
1689 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001690 const GrContext* ctx = fWindow->getGrContext();
1691
Brian Osmana109e392017-02-24 09:49:14 -05001692 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1693 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001694 if (ImGui::CollapsingHeader("Backend")) {
1695 int newBackend = static_cast<int>(fBackendType);
1696 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1697 ImGui::SameLine();
1698 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001699#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1700 ImGui::SameLine();
1701 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1702#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001703#if defined(SK_DAWN)
1704 ImGui::SameLine();
1705 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1706#endif
Brian Osman621491e2017-02-28 15:45:01 -05001707#if defined(SK_VULKAN)
1708 ImGui::SameLine();
1709 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1710#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001711#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001712 ImGui::SameLine();
1713 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1714#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001715#if defined(SK_DIRECT3D)
1716 ImGui::SameLine();
1717 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1718#endif
Brian Osman621491e2017-02-28 15:45:01 -05001719 if (newBackend != fBackendType) {
1720 fDeferredActions.push_back([=]() {
1721 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1722 });
1723 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001724
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001725 bool* wire = &params.fGrContextOptions.fWireframeMode;
1726 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1727 paramsChanged = true;
1728 }
Brian Salomon99a33902017-03-07 15:16:34 -05001729
Brian Osman28b12522017-03-08 17:10:24 -05001730 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001731 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001732 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001733 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1734 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1735 1;
1736
1737 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1738 if (maxMSAA >= 4) {
1739 ImGui::Text("MSAA: ");
1740
1741 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1742 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1743 // don't show it in the list.
1744 if (curMSAA == 2) {
1745 continue;
1746 }
1747 ImGui::SameLine();
1748 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1749 &sampleCount, curMSAA);
1750 }
1751 }
Brian Osman28b12522017-03-08 17:10:24 -05001752
1753 if (sampleCount != params.fMSAASampleCount) {
1754 params.fMSAASampleCount = sampleCount;
1755 paramsChanged = true;
1756 }
1757 }
1758
Ben Wagner37c54032018-04-13 14:30:23 -04001759 int pixelGeometryIdx = 0;
1760 if (fPixelGeometryOverrides) {
1761 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1762 }
1763 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1764 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1765 {
1766 uint32_t flags = params.fSurfaceProps.flags();
1767 if (pixelGeometryIdx == 0) {
1768 fPixelGeometryOverrides = false;
1769 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1770 } else {
1771 fPixelGeometryOverrides = true;
1772 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1773 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1774 }
1775 paramsChanged = true;
1776 }
1777
1778 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1779 if (ImGui::Checkbox("DFT", &useDFT)) {
1780 uint32_t flags = params.fSurfaceProps.flags();
1781 if (useDFT) {
1782 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1783 } else {
1784 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1785 }
1786 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1787 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1788 paramsChanged = true;
1789 }
1790
Brian Osman8a9de3d2017-03-01 14:59:05 -05001791 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001792 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001793 auto prButton = [&](GpuPathRenderers x) {
1794 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001795 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1796 params.fGrContextOptions.fGpuPathRenderers = x;
1797 paramsChanged = true;
1798 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001799 }
1800 };
1801
1802 if (!ctx) {
1803 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001804 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001805 const auto* caps = ctx->priv().caps();
1806 prButton(GpuPathRenderers::kDefault);
1807 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001808 if (caps->shaderCaps()->tessellationSupport()) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001809 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001810 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001811 if (caps->shaderCaps()->pathRenderingSupport()) {
1812 prButton(GpuPathRenderers::kStencilAndCover);
1813 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001814 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001815 if (1 == fWindow->sampleCount()) {
1816 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1817 prButton(GpuPathRenderers::kCoverageCounting);
1818 }
1819 prButton(GpuPathRenderers::kSmall);
1820 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001821 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001822 prButton(GpuPathRenderers::kNone);
1823 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001824 ImGui::TreePop();
1825 }
Brian Osman621491e2017-02-28 15:45:01 -05001826 }
1827
Ben Wagner964571d2019-03-08 12:35:06 -05001828 if (ImGui::CollapsingHeader("Tiling")) {
1829 ImGui::Checkbox("Enable", &fTiled);
1830 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1831 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1832 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1833 }
1834
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001835 if (ImGui::CollapsingHeader("Transform")) {
1836 float zoom = fZoomLevel;
1837 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1838 fZoomLevel = zoom;
1839 this->preTouchMatrixChanged();
1840 paramsChanged = true;
1841 }
1842 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001843 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001844 fRotation = deg;
1845 this->preTouchMatrixChanged();
1846 paramsChanged = true;
1847 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001848 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1849 if (ImGui_DragLocation(&fOffset)) {
1850 this->preTouchMatrixChanged();
1851 paramsChanged = true;
1852 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001853 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1854 this->preTouchMatrixChanged();
1855 paramsChanged = true;
1856 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001857 }
Brian Osman805a7272018-05-02 15:40:20 -04001858 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1859 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1860 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001861 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001862 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001863 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001864 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001865 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001866 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001867 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001868 }
1869
Ben Wagnera580fb32018-04-17 11:16:32 -04001870 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001871 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001872 if (fPaintOverrides.fAntiAlias) {
1873 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001874 }
1875 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001876 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001877 {
1878 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1879 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001880 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001881 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1882 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001883 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001884 fPaintOverrides.fAntiAlias = true;
1885 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001886 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001887 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001888 case SkPaintFields::AntiAliasState::Alias:
1889 break;
1890 case SkPaintFields::AntiAliasState::Normal:
1891 break;
1892 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1893 gSkUseAnalyticAA = true;
1894 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001895 break;
1896 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1897 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001898 break;
1899 }
1900 }
1901 paramsChanged = true;
1902 }
1903
Ben Wagner99a78dc2018-05-09 18:23:51 -04001904 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001905 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001906 bool (SkPaint::* isFlag)() const,
1907 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001908 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001909 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001910 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001911 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001912 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001913 if (ImGui::Combo(label, &itemIndex, items)) {
1914 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001915 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001916 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001917 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001918 (fPaint.*setFlag)(itemIndex == 2);
1919 }
1920 paramsChanged = true;
1921 }
1922 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001923
Ben Wagner99a78dc2018-05-09 18:23:51 -04001924 paintFlag("Dither",
1925 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001926 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001927 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001928
1929 int filterQualityIdx = 0;
1930 if (fPaintOverrides.fFilterQuality) {
1931 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1932 }
1933 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1934 "Default\0None\0Low\0Medium\0High\0\0"))
1935 {
1936 if (filterQualityIdx == 0) {
1937 fPaintOverrides.fFilterQuality = false;
1938 fPaint.setFilterQuality(kNone_SkFilterQuality);
1939 } else {
1940 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1941 fPaintOverrides.fFilterQuality = true;
1942 }
1943 paramsChanged = true;
1944 }
Ben Wagner9613e452019-01-23 10:34:59 -05001945 }
Hal Canary02738a82019-01-21 18:51:32 +00001946
Ben Wagner9613e452019-01-23 10:34:59 -05001947 if (ImGui::CollapsingHeader("Font")) {
1948 int hintingIdx = 0;
1949 if (fFontOverrides.fHinting) {
1950 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1951 }
1952 if (ImGui::Combo("Hinting", &hintingIdx,
1953 "Default\0None\0Slight\0Normal\0Full\0\0"))
1954 {
1955 if (hintingIdx == 0) {
1956 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001957 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001958 } else {
1959 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1960 fFontOverrides.fHinting = true;
1961 }
1962 paramsChanged = true;
1963 }
Hal Canary02738a82019-01-21 18:51:32 +00001964
Ben Wagner9613e452019-01-23 10:34:59 -05001965 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1966 bool SkFontFields::* flag,
1967 bool (SkFont::* isFlag)() const,
1968 void (SkFont::* setFlag)(bool) )
1969 {
1970 int itemIndex = 0;
1971 if (fFontOverrides.*flag) {
1972 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1973 }
1974 if (ImGui::Combo(label, &itemIndex, items)) {
1975 if (itemIndex == 0) {
1976 fFontOverrides.*flag = false;
1977 } else {
1978 fFontOverrides.*flag = true;
1979 (fFont.*setFlag)(itemIndex == 2);
1980 }
1981 paramsChanged = true;
1982 }
1983 };
Hal Canary02738a82019-01-21 18:51:32 +00001984
Ben Wagner9613e452019-01-23 10:34:59 -05001985 fontFlag("Fake Bold Glyphs",
1986 "Default\0No Fake Bold\0Fake Bold\0\0",
1987 &SkFontFields::fEmbolden,
1988 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001989
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001990 fontFlag("Baseline Snapping",
1991 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1992 &SkFontFields::fBaselineSnap,
1993 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1994
Ben Wagner9613e452019-01-23 10:34:59 -05001995 fontFlag("Linear Text",
1996 "Default\0No Linear Text\0Linear Text\0\0",
1997 &SkFontFields::fLinearMetrics,
1998 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001999
Ben Wagner9613e452019-01-23 10:34:59 -05002000 fontFlag("Subpixel Position Glyphs",
2001 "Default\0Pixel Text\0Subpixel Text\0\0",
2002 &SkFontFields::fSubpixel,
2003 &SkFont::isSubpixel, &SkFont::setSubpixel);
2004
2005 fontFlag("Embedded Bitmap Text",
2006 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2007 &SkFontFields::fEmbeddedBitmaps,
2008 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2009
2010 fontFlag("Force Auto-Hinting",
2011 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2012 &SkFontFields::fForceAutoHinting,
2013 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2014
2015 int edgingIdx = 0;
2016 if (fFontOverrides.fEdging) {
2017 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2018 }
2019 if (ImGui::Combo("Edging", &edgingIdx,
2020 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2021 {
2022 if (edgingIdx == 0) {
2023 fFontOverrides.fEdging = false;
2024 fFont.setEdging(SkFont::Edging::kAlias);
2025 } else {
2026 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2027 fFontOverrides.fEdging = true;
2028 }
2029 paramsChanged = true;
2030 }
2031
Ben Wagner15a8d572019-03-21 13:35:44 -04002032 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2033 if (fFontOverrides.fSize) {
2034 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002035 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002036 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002037 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002038 fFontOverrides.fSizeRange[0],
2039 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002040 "%.6f", 2.0f))
2041 {
Mike Reed3ae47332019-01-04 10:11:46 -05002042 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002043 paramsChanged = true;
2044 }
2045 }
2046
2047 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2048 if (fFontOverrides.fScaleX) {
2049 float scaleX = fFont.getScaleX();
2050 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2051 fFont.setScaleX(scaleX);
2052 paramsChanged = true;
2053 }
2054 }
2055
2056 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2057 if (fFontOverrides.fSkewX) {
2058 float skewX = fFont.getSkewX();
2059 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2060 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002061 paramsChanged = true;
2062 }
2063 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002064 }
2065
Mike Reed81f60ec2018-05-15 10:09:52 -04002066 {
2067 SkMetaData controls;
2068 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2069 if (ImGui::CollapsingHeader("Current Slide")) {
2070 SkMetaData::Iter iter(controls);
2071 const char* name;
2072 SkMetaData::Type type;
2073 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002074 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002075 if (type == SkMetaData::kScalar_Type) {
2076 float val[3];
2077 SkASSERT(count == 3);
2078 controls.findScalars(name, &count, val);
2079 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2080 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002081 }
Ben Wagner110c7032019-03-22 17:03:59 -04002082 } else if (type == SkMetaData::kBool_Type) {
2083 bool val;
2084 SkASSERT(count == 1);
2085 controls.findBool(name, &val);
2086 if (ImGui::Checkbox(name, &val)) {
2087 controls.setBool(name, val);
2088 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002089 }
2090 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002091 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002092 }
2093 }
2094 }
2095
Ben Wagner7a3c6742018-04-23 10:01:07 -04002096 if (fShowSlidePicker) {
2097 ImGui::SetNextTreeNodeOpen(true);
2098 }
Brian Osman79086b92017-02-10 13:36:16 -05002099 if (ImGui::CollapsingHeader("Slide")) {
2100 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002101 static ImVector<const char*> filteredSlideNames;
2102 static ImVector<int> filteredSlideIndices;
2103
Brian Osmanfce09c52017-11-14 15:32:20 -05002104 if (fShowSlidePicker) {
2105 ImGui::SetKeyboardFocusHere();
2106 fShowSlidePicker = false;
2107 }
2108
Brian Osman79086b92017-02-10 13:36:16 -05002109 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002110 filteredSlideNames.clear();
2111 filteredSlideIndices.clear();
2112 int filteredIndex = 0;
2113 for (int i = 0; i < fSlides.count(); ++i) {
2114 const char* slideName = fSlides[i]->getName().c_str();
2115 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2116 if (i == fCurrentSlide) {
2117 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002118 }
Brian Osmanf479e422017-11-08 13:11:36 -05002119 filteredSlideNames.push_back(slideName);
2120 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002121 }
Brian Osman79086b92017-02-10 13:36:16 -05002122 }
Brian Osmanf479e422017-11-08 13:11:36 -05002123
Brian Osmanf479e422017-11-08 13:11:36 -05002124 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2125 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002126 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002127 }
2128 }
Brian Osmana109e392017-02-24 09:49:14 -05002129
2130 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002131 ColorMode newMode = fColorMode;
2132 auto cmButton = [&](ColorMode mode, const char* label) {
2133 if (ImGui::RadioButton(label, mode == fColorMode)) {
2134 newMode = mode;
2135 }
2136 };
2137
2138 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002139 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2140 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002141 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002142
2143 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002144 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002145 }
2146
2147 // Pick from common gamuts:
2148 int primariesIdx = 4; // Default: Custom
2149 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2150 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2151 primariesIdx = i;
2152 break;
2153 }
2154 }
2155
Brian Osman03115dc2018-11-26 13:55:19 -05002156 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002157 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002158
Brian Osmana109e392017-02-24 09:49:14 -05002159 if (ImGui::Combo("Primaries", &primariesIdx,
2160 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2161 if (primariesIdx >= 0 && primariesIdx <= 3) {
2162 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2163 }
2164 }
2165
2166 // Allow direct editing of gamut
2167 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2168 }
Brian Osman207d4102019-01-10 09:40:58 -05002169
2170 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002171 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002172 if (ImGui::Checkbox("Pause", &isPaused)) {
2173 fAnimTimer.togglePauseResume();
2174 }
Brian Osman707d2022019-01-10 11:27:34 -05002175
2176 float speed = fAnimTimer.getSpeed();
2177 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2178 fAnimTimer.setSpeed(speed);
2179 }
Brian Osman207d4102019-01-10 09:40:58 -05002180 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002181
Brian Osmanfd7657c2019-04-25 11:34:07 -04002182 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2183#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2184 || Window::kANGLE_BackendType == fBackendType
2185#endif
2186 ;
2187
2188 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2189 // than GL, we need to force it on. Just do that on the first frame after the backend
2190 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002191 if (!backendIsGL &&
2192 params.fGrContextOptions.fShaderCacheStrategy !=
2193 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2194 params.fGrContextOptions.fShaderCacheStrategy =
2195 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002196 paramsChanged = true;
2197 fPersistentCache.reset();
2198 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002199 // To re-load shaders from the currently active programs, we flush all caches on one
2200 // frame, then set a flag to poll the cache on the next frame.
2201 static bool gLoadPending = false;
2202 if (gLoadPending) {
2203 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2204 int hitCount) {
2205 CachedGLSL& entry(fCachedGLSL.push_back());
2206 entry.fKey = key;
2207 SkMD5 hash;
2208 hash.write(key->bytes(), key->size());
2209 SkMD5::Digest digest = hash.finish();
2210 for (int i = 0; i < 16; ++i) {
2211 entry.fKeyString.appendf("%02x", digest.data[i]);
2212 }
2213
Brian Osmana66081d2019-09-03 14:59:26 -04002214 SkReader32 reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002215 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002216 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2217 entry.fInputs,
2218 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002219 };
2220 fCachedGLSL.reset();
2221 fPersistentCache.foreach(collectShaders);
2222 gLoadPending = false;
2223 }
2224
2225 // Defer actually doing the load/save logic so that we can trigger a save when we
2226 // start or finish hovering on a tree node in the list below:
2227 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002228 bool doSave = ImGui::Button("Save");
2229 if (backendIsGL) {
2230 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002231 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2232 GrContextOptions::ShaderCacheStrategy::kSkSL;
2233 if (ImGui::Checkbox("SkSL", &sksl)) {
2234 params.fGrContextOptions.fShaderCacheStrategy = sksl
2235 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2236 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002237 paramsChanged = true;
2238 doLoad = true;
2239 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2240 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002241 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002242
2243 ImGui::BeginChild("##ScrollingRegion");
2244 for (auto& entry : fCachedGLSL) {
2245 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2246 bool hovered = ImGui::IsItemHovered();
2247 if (hovered != entry.fHovered) {
2248 // Force a save to patch the highlight shader in/out
2249 entry.fHovered = hovered;
2250 doSave = true;
2251 }
2252 if (inTreeNode) {
2253 // Full width, and a reasonable amount of space for each shader.
2254 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2255 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2256 boxSize);
2257 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2258 boxSize);
2259 ImGui::TreePop();
2260 }
2261 }
2262 ImGui::EndChild();
2263
2264 if (doLoad) {
2265 fPersistentCache.reset();
2266 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2267 gLoadPending = true;
2268 }
2269 if (doSave) {
2270 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002271 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002272 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2273 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002274
Brian Osman072e6fc2019-06-12 11:35:41 -04002275 SkSL::String highlight;
2276 if (!sksl) {
2277 highlight = shaderCaps->versionDeclString();
2278 if (shaderCaps->usesPrecisionModifiers()) {
2279 highlight.append("precision mediump float;\n");
2280 }
Brian Osman5bee3902019-05-07 09:55:45 -04002281 }
2282 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002283 highlight.appendf("out %s sk_FragColor;\n"
2284 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2285 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002286
2287 fPersistentCache.reset();
2288 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2289 for (auto& entry : fCachedGLSL) {
2290 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2291 if (entry.fHovered) {
2292 entry.fShader[kFragment_GrShaderType] = highlight;
2293 }
2294
Brian Osmana085a412019-04-25 09:44:43 -04002295 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2296 entry.fShader,
2297 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002298 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002299 fPersistentCache.store(*entry.fKey, *data);
2300
2301 entry.fShader[kFragment_GrShaderType] = backup;
2302 }
2303 }
2304 }
Brian Osman79086b92017-02-10 13:36:16 -05002305 }
Brian Salomon99a33902017-03-07 15:16:34 -05002306 if (paramsChanged) {
2307 fDeferredActions.push_back([=]() {
2308 fWindow->setRequestedDisplayParams(params);
2309 fWindow->inval();
2310 this->updateTitle();
2311 });
2312 }
Brian Osman79086b92017-02-10 13:36:16 -05002313 ImGui::End();
2314 }
2315
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002316 if (gShaderErrorHandler.fErrors.count()) {
2317 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2318 ImGui::Begin("Shader Errors");
2319 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2320 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002321 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2322 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2323 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2324 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002325 }
2326 ImGui::End();
2327 gShaderErrorHandler.reset();
2328 }
2329
Brian Osmanf6877092017-02-13 09:39:57 -05002330 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002331 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2332 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002333 static int zoomFactor = 8;
2334 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002335 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002336 }
2337 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2338 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002339 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002340 }
Brian Osmanf6877092017-02-13 09:39:57 -05002341
Ben Wagner3627d2e2018-06-26 14:23:20 -04002342 if (!fZoomWindowFixed) {
2343 ImVec2 mousePos = ImGui::GetMousePos();
2344 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2345 }
2346 SkScalar x = fZoomWindowLocation.x();
2347 SkScalar y = fZoomWindowLocation.y();
2348 int xInt = SkScalarRoundToInt(x);
2349 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002350 ImVec2 avail = ImGui::GetContentRegionAvail();
2351
Brian Osmanead517d2017-11-13 15:36:36 -05002352 uint32_t pixel = 0;
2353 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002354 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002355 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002356 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002357 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002358 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002359 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2360 }
2361
Brian Osmand67e5182017-12-08 16:46:09 -05002362 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002363 // Translate so the region of the image that's under the mouse cursor is centered
2364 // in the zoom canvas:
2365 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002366 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2367 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002368 c->drawImage(this->fLastImage, 0, 0);
2369
2370 SkPaint outline;
2371 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002372 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002373 });
Brian Osmanf6877092017-02-13 09:39:57 -05002374 }
2375
2376 ImGui::End();
2377 }
Brian Osman79086b92017-02-10 13:36:16 -05002378}
2379
liyuqian2edb0f42016-07-06 14:11:32 -07002380void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002381 for (int i = 0; i < fDeferredActions.count(); ++i) {
2382 fDeferredActions[i]();
2383 }
2384 fDeferredActions.reset();
2385
Brian Osman56a24812017-12-19 11:15:16 -05002386 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002387 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002388 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002389 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002390
Brian Osman79086b92017-02-10 13:36:16 -05002391 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002392 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2393 // not be visible, though. So we need to redraw if there is at least one visible window, or
2394 // more than one active window. Newly created windows are active but not visible for one frame
2395 // while they determine their layout and sizing.
2396 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2397 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002398 fWindow->inval();
2399 }
jvanverth9f372462016-04-06 06:08:59 -07002400}
liyuqiane5a6cd92016-05-27 08:52:52 -07002401
Florin Malitab632df72018-06-18 21:23:06 -04002402template <typename OptionsFunc>
2403static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2404 OptionsFunc&& optionsFunc) {
2405 writer.beginObject();
2406 {
2407 writer.appendString(kName , name);
2408 writer.appendString(kValue, value);
2409
2410 writer.beginArray(kOptions);
2411 {
2412 optionsFunc(writer);
2413 }
2414 writer.endArray();
2415 }
2416 writer.endObject();
2417}
2418
2419
liyuqiane5a6cd92016-05-27 08:52:52 -07002420void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002421 if (!fWindow) {
2422 return;
2423 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002424 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002425 return; // Surface hasn't been created yet.
2426 }
2427
Florin Malitab632df72018-06-18 21:23:06 -04002428 SkDynamicMemoryWStream memStream;
2429 SkJSONWriter writer(&memStream);
2430 writer.beginArray();
2431
liyuqianb73c24b2016-06-03 08:47:23 -07002432 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002433 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2434 [this](SkJSONWriter& writer) {
2435 for(const auto& slide : fSlides) {
2436 writer.appendString(slide->getName().c_str());
2437 }
2438 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002439
liyuqianb73c24b2016-06-03 08:47:23 -07002440 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002441 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2442 [](SkJSONWriter& writer) {
2443 for (const auto& str : kBackendTypeStrings) {
2444 writer.appendString(str);
2445 }
2446 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002447
csmartdalton578f0642017-02-24 16:04:47 -07002448 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002449 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2450 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2451 [this](SkJSONWriter& writer) {
2452 writer.appendS32(0);
2453
2454 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2455 return;
2456 }
2457
2458 for (int msaa : {4, 8, 16}) {
2459 writer.appendS32(msaa);
2460 }
2461 });
csmartdalton578f0642017-02-24 16:04:47 -07002462
csmartdalton61cd31a2017-02-27 17:00:53 -07002463 // Path renderer state
2464 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002465 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2466 [this](SkJSONWriter& writer) {
2467 const GrContext* ctx = fWindow->getGrContext();
2468 if (!ctx) {
2469 writer.appendString("Software");
2470 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002471 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002472 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2473 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002474 if (caps->shaderCaps()->tessellationSupport()) {
2475 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002476 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002477 }
Florin Malitab632df72018-06-18 21:23:06 -04002478 if (caps->shaderCaps()->pathRenderingSupport()) {
2479 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002480 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002481 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002482 }
2483 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002484 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2485 writer.appendString(
2486 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2487 }
2488 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2489 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002490 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002491 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002492 }
2493 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002494
liyuqianb73c24b2016-06-03 08:47:23 -07002495 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002496 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2497 [this](SkJSONWriter& writer) {
2498 writer.appendString(kSoftkeyHint);
2499 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2500 writer.appendString(softkey.c_str());
2501 }
2502 });
liyuqianb73c24b2016-06-03 08:47:23 -07002503
Florin Malitab632df72018-06-18 21:23:06 -04002504 writer.endArray();
2505 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002506
Florin Malitab632df72018-06-18 21:23:06 -04002507 auto data = memStream.detachAsData();
2508
2509 // TODO: would be cool to avoid this copy
2510 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2511
2512 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002513}
2514
2515void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002516 // For those who will add more features to handle the state change in this function:
2517 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2518 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2519 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002520 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002521 for (int i = 0; i < fSlides.count(); ++i) {
2522 if (fSlides[i]->getName().equals(stateValue)) {
2523 this->setCurrentSlide(i);
2524 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002525 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002526 }
Florin Malitaab99c342018-01-16 16:23:03 -05002527
2528 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002529 } else if (stateName.equals(kBackendStateName)) {
2530 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2531 if (stateValue.equals(kBackendTypeStrings[i])) {
2532 if (fBackendType != i) {
2533 fBackendType = (sk_app::Window::BackendType)i;
2534 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002535 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002536 }
2537 break;
2538 }
2539 }
csmartdalton578f0642017-02-24 16:04:47 -07002540 } else if (stateName.equals(kMSAAStateName)) {
2541 DisplayParams params = fWindow->getRequestedDisplayParams();
2542 int sampleCount = atoi(stateValue.c_str());
2543 if (sampleCount != params.fMSAASampleCount) {
2544 params.fMSAASampleCount = sampleCount;
2545 fWindow->setRequestedDisplayParams(params);
2546 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002547 this->updateTitle();
2548 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002549 }
2550 } else if (stateName.equals(kPathRendererStateName)) {
2551 DisplayParams params = fWindow->getRequestedDisplayParams();
2552 for (const auto& pair : gPathRendererNames) {
2553 if (pair.second == stateValue.c_str()) {
2554 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2555 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2556 fWindow->setRequestedDisplayParams(params);
2557 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002558 this->updateTitle();
2559 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002560 }
2561 break;
2562 }
csmartdalton578f0642017-02-24 16:04:47 -07002563 }
liyuqianb73c24b2016-06-03 08:47:23 -07002564 } else if (stateName.equals(kSoftkeyStateName)) {
2565 if (!stateValue.equals(kSoftkeyHint)) {
2566 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002567 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002568 }
liyuqian2edb0f42016-07-06 14:11:32 -07002569 } else if (stateName.equals(kRefreshStateName)) {
2570 // This state is actually NOT in the UI state.
2571 // We use this to allow Android to quickly set bool fRefresh.
2572 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002573 } else {
2574 SkDebugf("Unknown stateName: %s", stateName.c_str());
2575 }
2576}
Brian Osman79086b92017-02-10 13:36:16 -05002577
Hal Canaryb1f411a2019-08-29 10:39:22 -04002578bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002579 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002580}
2581
Hal Canaryb1f411a2019-08-29 10:39:22 -04002582bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002583 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002584 fWindow->inval();
2585 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002586 } else {
2587 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002588 }
Brian Osman79086b92017-02-10 13:36:16 -05002589}