blob: 6d41aad1ce7f851805384ce7ab89a3a769760475 [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
Stephen Whitea800ec92019-08-02 15:04:52 -040086#if defined(SK_VULKAN)
jvanverthb8794cc2016-07-27 14:29:18 -070087# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050088#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
89# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
Stephen Whitea800ec92019-08-02 15:04:52 -040090#elif defined(SK_DAWN)
91# define BACKENDS_STR "\"sw\", \"gl\", and \"dawn\""
bsalomon6c471f72016-07-26 12:56:32 -070092#else
93# define BACKENDS_STR "\"sw\" and \"gl\""
94#endif
95
Brian Osman2dd96932016-10-18 15:33:53 -040096static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070097
Mike Klein5b3f3432019-03-21 11:42:21 -050098static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070099
Mike Klein84836b72019-03-21 11:31:36 -0500100static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700101
Mike Klein84836b72019-03-21 11:31:36 -0500102static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400103
Mike Kleinc6142d82019-03-25 10:54:59 -0500104static DEFINE_string2(match, m, nullptr,
105 "[~][^]substring[$] [...] of name to run.\n"
106 "Multiple matches may be separated by spaces.\n"
107 "~ causes a matching name to always be skipped\n"
108 "^ requires the start of the name to match\n"
109 "$ requires the end of the name to match\n"
110 "^ and $ requires an exact match\n"
111 "If a name does not match any list entry,\n"
112 "it is skipped unless some list entry starts with ~");
113
Mike Klein19fb3972019-03-21 13:08:08 -0500114#if defined(SK_BUILD_FOR_ANDROID)
115 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500116 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
117 static DEFINE_string(lotties, "/data/local/tmp/lotties",
118 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500119#else
120 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500121 static DEFINE_string(skps, "skps", "Directory to read skps from.");
122 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500123#endif
124
Mike Kleinc6142d82019-03-25 10:54:59 -0500125static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
126
127static DEFINE_int_2(threads, j, -1,
128 "Run threadsafe tests on a threadpool with this many extra threads, "
129 "defaulting to one extra thread per core.");
130
Jim Van Verth7b558182019-11-14 16:47:01 -0500131static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
132
Chris Daltonc8877332020-01-06 09:48:30 -0700133static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Reed862818b2020-03-21 15:07:13 -0400134static DEFINE_bool(skvm, false, "Try to use skvm blitters for raster.");
Mike Klein1e0884d2020-04-28 15:04:16 -0500135static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500136static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500137
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400138#ifndef SK_GL
139static_assert(false, "viewer requires GL backend for raster.")
140#endif
141
Brian Salomon194db172017-08-17 14:37:06 -0400142const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700143 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400144#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
145 "ANGLE",
146#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400147#ifdef SK_DAWN
148 "Dawn",
149#endif
jvanverth063ece72016-06-17 09:29:14 -0700150#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700151 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700152#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400153#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500154 "Metal",
155#endif
csmartdalton578f0642017-02-24 16:04:47 -0700156 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700157};
158
bsalomon6c471f72016-07-26 12:56:32 -0700159static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400160#ifdef SK_DAWN
161 if (0 == strcmp(str, "dawn")) {
162 return sk_app::Window::kDawn_BackendType;
163 } else
164#endif
bsalomon6c471f72016-07-26 12:56:32 -0700165#ifdef SK_VULKAN
166 if (0 == strcmp(str, "vk")) {
167 return sk_app::Window::kVulkan_BackendType;
168 } else
169#endif
Brian Salomon194db172017-08-17 14:37:06 -0400170#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
171 if (0 == strcmp(str, "angle")) {
172 return sk_app::Window::kANGLE_BackendType;
173 } else
174#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400175#ifdef SK_METAL
176 if (0 == strcmp(str, "mtl")) {
177 return sk_app::Window::kMetal_BackendType;
178 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500179#endif
bsalomon6c471f72016-07-26 12:56:32 -0700180 if (0 == strcmp(str, "gl")) {
181 return sk_app::Window::kNativeGL_BackendType;
182 } else if (0 == strcmp(str, "sw")) {
183 return sk_app::Window::kRaster_BackendType;
184 } else {
185 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
186 return sk_app::Window::kRaster_BackendType;
187 }
188}
189
Brian Osmana109e392017-02-24 09:49:14 -0500190static SkColorSpacePrimaries gSrgbPrimaries = {
191 0.64f, 0.33f,
192 0.30f, 0.60f,
193 0.15f, 0.06f,
194 0.3127f, 0.3290f };
195
196static SkColorSpacePrimaries gAdobePrimaries = {
197 0.64f, 0.33f,
198 0.21f, 0.71f,
199 0.15f, 0.06f,
200 0.3127f, 0.3290f };
201
202static SkColorSpacePrimaries gP3Primaries = {
203 0.680f, 0.320f,
204 0.265f, 0.690f,
205 0.150f, 0.060f,
206 0.3127f, 0.3290f };
207
208static SkColorSpacePrimaries gRec2020Primaries = {
209 0.708f, 0.292f,
210 0.170f, 0.797f,
211 0.131f, 0.046f,
212 0.3127f, 0.3290f };
213
214struct NamedPrimaries {
215 const char* fName;
216 SkColorSpacePrimaries* fPrimaries;
217} gNamedPrimaries[] = {
218 { "sRGB", &gSrgbPrimaries },
219 { "AdobeRGB", &gAdobePrimaries },
220 { "P3", &gP3Primaries },
221 { "Rec. 2020", &gRec2020Primaries },
222};
223
224static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
225 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
226}
227
Brian Osman70d2f432017-11-08 09:54:10 -0500228static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
229 // In raster mode, we still use GL for the window.
230 // This lets us render the GUI faster (and correct).
231 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
232}
233
Jim Van Verth74826c82019-03-01 14:37:30 -0500234class NullSlide : public Slide {
235 SkISize getDimensions() const override {
236 return SkISize::Make(640, 480);
237 }
238
239 void draw(SkCanvas* canvas) override {
240 canvas->clear(0xffff11ff);
241 }
242};
243
John Stiles31964fd2020-05-05 16:05:47 -0400244static const char kName[] = "name";
245static const char kValue[] = "value";
246static const char kOptions[] = "options";
247static const char kSlideStateName[] = "Slide";
248static const char kBackendStateName[] = "Backend";
249static const char kMSAAStateName[] = "MSAA";
250static const char kPathRendererStateName[] = "Path renderer";
251static const char kSoftkeyStateName[] = "Softkey";
252static const char kSoftkeyHint[] = "Please select a softkey";
253static const char kON[] = "ON";
254static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700255
Mike Reed862818b2020-03-21 15:07:13 -0400256extern bool gUseSkVMBlitter;
Mike Klein1e0884d2020-04-28 15:04:16 -0500257extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400258
jvanverth34524262016-05-04 13:49:13 -0700259Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500260 : fCurrentSlide(-1)
261 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500262 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400263 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500264 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500265 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500266 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500267 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400268 , fZoomWindowFixed(false)
269 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500270 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400271 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700272 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500273 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500274 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500275 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500276 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700277 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400278 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400279 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400280 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500281 , fTiled(false)
282 , fDrawTileBoundaries(false)
283 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400284 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700285{
Greg Daniel285db442016-10-14 09:12:53 -0400286 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700287
Chris Dalton37ae4b02019-12-28 14:51:11 -0700288 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600289 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500290 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500291 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600292 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600293 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500294 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700295
jvanverth2bb3b6d2016-04-08 07:24:09 -0700296 SkDebugf("Command line arguments: ");
297 for (int i = 1; i < argc; ++i) {
298 SkDebugf("%s ", argv[i]);
299 }
300 SkDebugf("\n");
301
Mike Klein88544fb2019-03-20 10:50:33 -0500302 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500303#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400304 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500305#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700306
Mike Reed862818b2020-03-21 15:07:13 -0400307 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein1e0884d2020-04-28 15:04:16 -0500308 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400309
Mike Klein19cc0f62019-03-22 15:30:07 -0500310 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500311
Brian Osmanbc8150f2017-07-24 11:38:01 -0400312 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400313 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400314
bsalomon6c471f72016-07-26 12:56:32 -0700315 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700316 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700317
csmartdalton578f0642017-02-24 16:04:47 -0700318 DisplayParams displayParams;
319 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700320 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400321 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400322 displayParams.fGrContextOptions.fShaderCacheStrategy =
323 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400324 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
325 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700326 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500327 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700328
Brian Osman56a24812017-12-19 11:15:16 -0500329 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500330 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500331 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
332 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
333 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
334
jvanverth9f372462016-04-06 06:08:59 -0700335 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700336 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500337 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500338 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500339 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700340
brianosman622c8d52016-05-10 06:50:49 -0700341 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500342 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
343 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
344 fWindow->inval();
345 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500346 // Command to jump directly to the slide picker and give it focus
347 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
348 this->fShowImGuiDebugWindow = true;
349 this->fShowSlidePicker = true;
350 fWindow->inval();
351 });
352 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400353 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500354 this->fShowImGuiDebugWindow = true;
355 this->fShowSlidePicker = true;
356 fWindow->inval();
357 });
Brian Osman79086b92017-02-10 13:36:16 -0500358 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
359 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
360 fWindow->inval();
361 });
Brian Osmanf6877092017-02-13 09:39:57 -0500362 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
363 this->fShowZoomWindow = !this->fShowZoomWindow;
364 fWindow->inval();
365 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400366 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
367 this->fZoomWindowFixed = !this->fZoomWindowFixed;
368 fWindow->inval();
369 });
Greg Danield0794cc2019-03-27 16:23:26 -0400370 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
371 DisplayParams params = fWindow->getRequestedDisplayParams();
372 params.fDisableVsync = !params.fDisableVsync;
373 fWindow->setRequestedDisplayParams(params);
374 this->updateTitle();
375 fWindow->inval();
376 });
Mike Reedf702ed42019-07-22 17:00:49 -0400377 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
378 fRefresh = !fRefresh;
379 fWindow->inval();
380 });
brianosman622c8d52016-05-10 06:50:49 -0700381 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500382 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700383 fWindow->inval();
384 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400385 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500386 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400387 this->updateTitle();
388 fWindow->inval();
389 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500390 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500391 switch (fColorMode) {
392 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500393 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500394 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500395 case ColorMode::kColorManaged8888:
396 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500397 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500398 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400399 this->setColorMode(ColorMode::kColorManagedF16Norm);
400 break;
401 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500402 this->setColorMode(ColorMode::kLegacy);
403 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500404 }
brianosman622c8d52016-05-10 06:50:49 -0700405 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700406 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
407 DisplayParams params = fWindow->getRequestedDisplayParams();
408 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
409 fWindow->setRequestedDisplayParams(params);
410 fWindow->inval();
411 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400412 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500413 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700414 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400415 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500416 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700417 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400418 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700419 this->changeZoomLevel(1.f / 32.f);
420 fWindow->inval();
421 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400422 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700423 this->changeZoomLevel(-1.f / 32.f);
424 fWindow->inval();
425 });
jvanverthaf236b52016-05-20 06:01:06 -0700426 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400427 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
428 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500429 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400430#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
431 if (newBackend == sk_app::Window::kVulkan_BackendType) {
432 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
433 sk_app::Window::kBackendTypeCount);
434 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
435 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500436 }
437#endif
Brian Osman621491e2017-02-28 15:45:01 -0500438 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700439 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500440 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
441 fSaveToSKP = true;
442 fWindow->inval();
443 });
Mike Reed376d8122019-03-14 11:39:02 -0400444 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
445 fShowSlideDimensions = !fShowSlideDimensions;
446 fWindow->inval();
447 });
Ben Wagner37c54032018-04-13 14:30:23 -0400448 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
449 DisplayParams params = fWindow->getRequestedDisplayParams();
450 uint32_t flags = params.fSurfaceProps.flags();
451 if (!fPixelGeometryOverrides) {
452 fPixelGeometryOverrides = true;
453 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
454 } else {
455 switch (params.fSurfaceProps.pixelGeometry()) {
456 case kUnknown_SkPixelGeometry:
457 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
458 break;
459 case kRGB_H_SkPixelGeometry:
460 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
461 break;
462 case kBGR_H_SkPixelGeometry:
463 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
464 break;
465 case kRGB_V_SkPixelGeometry:
466 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
467 break;
468 case kBGR_V_SkPixelGeometry:
469 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
470 fPixelGeometryOverrides = false;
471 break;
472 }
473 }
474 fWindow->setRequestedDisplayParams(params);
475 this->updateTitle();
476 fWindow->inval();
477 });
Ben Wagner9613e452019-01-23 10:34:59 -0500478 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500479 if (!fFontOverrides.fHinting) {
480 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400481 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500482 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500483 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400484 case SkFontHinting::kNone:
485 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500486 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400487 case SkFontHinting::kSlight:
488 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500489 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400490 case SkFontHinting::kNormal:
491 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500492 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400493 case SkFontHinting::kFull:
494 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500495 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500496 break;
497 }
498 }
499 this->updateTitle();
500 fWindow->inval();
501 });
502 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500503 if (!fPaintOverrides.fAntiAlias) {
504 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
505 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500506 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500507 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500508 } else {
509 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500510 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500511 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500512 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400513 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500514 break;
515 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500516 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500517 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400518 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500519 break;
520 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500521 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400522 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500523 break;
524 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500525 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
526 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500527 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
528 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500529 break;
530 }
531 }
532 this->updateTitle();
533 fWindow->inval();
534 });
Ben Wagner37c54032018-04-13 14:30:23 -0400535 fCommands.addCommand('D', "Modes", "DFT", [this]() {
536 DisplayParams params = fWindow->getRequestedDisplayParams();
537 uint32_t flags = params.fSurfaceProps.flags();
538 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
539 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
540 fWindow->setRequestedDisplayParams(params);
541 this->updateTitle();
542 fWindow->inval();
543 });
Ben Wagner9613e452019-01-23 10:34:59 -0500544 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500545 if (!fFontOverrides.fEdging) {
546 fFontOverrides.fEdging = true;
547 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500548 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500549 switch (fFont.getEdging()) {
550 case SkFont::Edging::kAlias:
551 fFont.setEdging(SkFont::Edging::kAntiAlias);
552 break;
553 case SkFont::Edging::kAntiAlias:
554 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
555 break;
556 case SkFont::Edging::kSubpixelAntiAlias:
557 fFont.setEdging(SkFont::Edging::kAlias);
558 fFontOverrides.fEdging = false;
559 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500560 }
561 }
562 this->updateTitle();
563 fWindow->inval();
564 });
Ben Wagner9613e452019-01-23 10:34:59 -0500565 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500566 if (!fFontOverrides.fSubpixel) {
567 fFontOverrides.fSubpixel = true;
568 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500569 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500570 if (!fFont.isSubpixel()) {
571 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500572 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500573 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500574 }
575 }
576 this->updateTitle();
577 fWindow->inval();
578 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400579 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
580 if (!fFontOverrides.fBaselineSnap) {
581 fFontOverrides.fBaselineSnap = true;
582 fFont.setBaselineSnap(false);
583 } else {
584 if (!fFont.isBaselineSnap()) {
585 fFont.setBaselineSnap(true);
586 } else {
587 fFontOverrides.fBaselineSnap = false;
588 }
589 }
590 this->updateTitle();
591 fWindow->inval();
592 });
Brian Osman805a7272018-05-02 15:40:20 -0400593 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
594 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
595 : kPerspective_Real;
596 this->updateTitle();
597 fWindow->inval();
598 });
599 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
600 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
601 : kPerspective_Off;
602 this->updateTitle();
603 fWindow->inval();
604 });
Brian Osman207d4102019-01-10 09:40:58 -0500605 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
606 fAnimTimer.togglePauseResume();
607 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400608 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
609 fZoomUI = !fZoomUI;
610 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
611 fWindow->inval();
612 });
Mike Reed59295352020-03-12 13:56:34 -0400613 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
614 fDrawViaSerialize = !fDrawViaSerialize;
615 this->updateTitle();
616 fWindow->inval();
617 });
Mike Reed862818b2020-03-21 15:07:13 -0400618 fCommands.addCommand('!', "SkVM", "Toggle SkVM", [this]() {
619 gUseSkVMBlitter = !gUseSkVMBlitter;
620 this->updateTitle();
621 fWindow->inval();
622 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500623
jvanverth2bb3b6d2016-04-08 07:24:09 -0700624 // set up slides
625 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500626 if (FLAGS_list) {
627 this->listNames();
628 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700629
Brian Osman9bb47cf2018-04-26 15:55:00 -0400630 fPerspectivePoints[0].set(0, 0);
631 fPerspectivePoints[1].set(1, 0);
632 fPerspectivePoints[2].set(0, 1);
633 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700634 fAnimTimer.run();
635
Hal Canaryc465d132017-12-08 10:21:31 -0500636 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500637 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400638 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500639 }
640 fImGuiGamutPaint.setColor(SK_ColorWHITE);
641 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
642
jongdeok.kim804f17e2019-02-26 14:39:23 +0900643 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500644 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700645}
646
jvanverth34524262016-05-04 13:49:13 -0700647void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400648 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
649 static const struct {
650 const char* fExtension;
651 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500652 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400653 const SlideFactory fFactory;
654 } gExternalSlidesInfo[] = {
655 { ".skp", "skp-dir", FLAGS_skps,
656 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
657 return sk_make_sp<SKPSlide>(name, path);}
658 },
659 { ".jpg", "jpg-dir", FLAGS_jpgs,
660 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
661 return sk_make_sp<ImageSlide>(name, path);}
662 },
Florin Malita87ccf332018-05-04 12:23:24 -0400663#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400664 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400665 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
666 return sk_make_sp<SkottieSlide>(name, path);}
667 },
Florin Malita87ccf332018-05-04 12:23:24 -0400668#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400669#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400670 { ".svg", "svg-dir", FLAGS_svgs,
671 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
672 return sk_make_sp<SvgSlide>(name, path);}
673 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400674#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400675 };
jvanverthc265a922016-04-08 12:51:45 -0700676
Brian Salomon343553a2018-09-05 15:41:23 -0400677 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700678
Mike Klein88544fb2019-03-20 10:50:33 -0500679 const auto addSlide =
680 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
681 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
682 return;
683 }
liyuqian6f163d22016-06-13 12:26:45 -0700684
Mike Klein88544fb2019-03-20 10:50:33 -0500685 if (auto slide = fact(name, path)) {
686 dirSlides.push_back(slide);
687 fSlides.push_back(std::move(slide));
688 }
689 };
Florin Malita76a076b2018-02-15 18:40:48 -0500690
Florin Malita38792ce2018-05-08 10:36:18 -0400691 if (!FLAGS_file.isEmpty()) {
692 // single file mode
693 const SkString file(FLAGS_file[0]);
694
695 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
696 for (const auto& sinfo : gExternalSlidesInfo) {
697 if (file.endsWith(sinfo.fExtension)) {
698 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
699 return;
700 }
701 }
702
703 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
704 } else {
705 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
706 }
707
708 return;
709 }
710
711 // Bisect slide.
712 if (!FLAGS_bisect.isEmpty()) {
713 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500714 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400715 if (FLAGS_bisect.count() >= 2) {
716 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
717 bisect->onChar(*ch);
718 }
719 }
720 fSlides.push_back(std::move(bisect));
721 }
722 }
723
724 // GMs
725 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400726 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400727 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500728 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400729 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400730 fSlides.push_back(std::move(slide));
731 }
Florin Malita38792ce2018-05-08 10:36:18 -0400732 }
733 // reverse gms
734 int numGMs = fSlides.count() - firstGM;
735 for (int i = 0; i < numGMs/2; ++i) {
736 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
737 }
738
739 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400740 for (const SampleFactory factory : SampleRegistry::Range()) {
741 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500742 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400743 fSlides.push_back(slide);
744 }
Florin Malita38792ce2018-05-08 10:36:18 -0400745 }
746
Brian Osman7c979f52019-02-12 13:27:51 -0500747 // Particle demo
748 {
749 // TODO: Convert this to a sample
750 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500751 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500752 fSlides.push_back(std::move(slide));
753 }
754 }
755
Brian Osmand927bd22019-12-18 11:23:12 -0500756 // Runtime shader editor
757 {
758 sk_sp<Slide> slide(new SkSLSlide());
759 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
760 fSlides.push_back(std::move(slide));
761 }
762 }
763
Florin Malita0ffa3222018-04-05 14:34:45 -0400764 for (const auto& info : gExternalSlidesInfo) {
765 for (const auto& flag : info.fFlags) {
766 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
767 // single file
768 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
769 } else {
770 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400771 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400772 SkTArray<SkString> sortedFilenames;
773 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400774 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400775 sortedFilenames.push_back(name);
776 }
777 if (sortedFilenames.count()) {
778 SkTQSort(sortedFilenames.begin(), sortedFilenames.end() - 1,
779 [](const SkString& a, const SkString& b) {
780 return strcmp(a.c_str(), b.c_str()) < 0;
781 });
782 }
783 for (const SkString& filename : sortedFilenames) {
784 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
785 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400786 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400787 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400788 if (!dirSlides.empty()) {
789 fSlides.push_back(
790 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
791 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500792 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400793 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400794 }
795 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500796
797 if (!fSlides.count()) {
798 sk_sp<Slide> slide(new NullSlide());
799 fSlides.push_back(std::move(slide));
800 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700801}
802
803
jvanverth34524262016-05-04 13:49:13 -0700804Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700805 fWindow->detach();
806 delete fWindow;
807}
808
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500809struct SkPaintTitleUpdater {
810 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
811 void append(const char* s) {
812 if (fCount == 0) {
813 fTitle->append(" {");
814 } else {
815 fTitle->append(", ");
816 }
817 fTitle->append(s);
818 ++fCount;
819 }
820 void done() {
821 if (fCount > 0) {
822 fTitle->append("}");
823 }
824 }
825 SkString* fTitle;
826 int fCount;
827};
828
brianosman05de2162016-05-06 13:28:57 -0700829void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700830 if (!fWindow) {
831 return;
832 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500833 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700834 return; // Surface hasn't been created yet.
835 }
836
jvanverth34524262016-05-04 13:49:13 -0700837 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700838 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700839
Mike Kleine5acd752019-03-22 09:57:16 -0500840 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400841 if (gSkForceAnalyticAA) {
842 title.append(" <FAAA>");
843 } else {
844 title.append(" <AAA>");
845 }
846 }
Mike Reed59295352020-03-12 13:56:34 -0400847 if (fDrawViaSerialize) {
848 title.append(" <serialize>");
849 }
Mike Reed862818b2020-03-21 15:07:13 -0400850 if (gUseSkVMBlitter) {
851 title.append(" <skvm>");
852 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400853
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500854 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500855 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
856 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400857 const char* on, const char* off)
858 {
Ben Wagner9613e452019-01-23 10:34:59 -0500859 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400860 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500861 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400862 };
863
Ben Wagner9613e452019-01-23 10:34:59 -0500864 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
865 const char* on, const char* off)
866 {
867 if (fFontOverrides.*flag) {
868 paintTitle.append((fFont.*isFlag)() ? on : off);
869 }
870 };
871
872 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
873 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500874 if (fPaintOverrides.fFilterQuality) {
875 switch (fPaint.getFilterQuality()) {
876 case kNone_SkFilterQuality:
877 paintTitle.append("NoFilter");
878 break;
879 case kLow_SkFilterQuality:
880 paintTitle.append("LowFilter");
881 break;
882 case kMedium_SkFilterQuality:
883 paintTitle.append("MediumFilter");
884 break;
885 case kHigh_SkFilterQuality:
886 paintTitle.append("HighFilter");
887 break;
888 }
889 }
Ben Wagner9613e452019-01-23 10:34:59 -0500890
891 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
892 "Force Autohint", "No Force Autohint");
893 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400894 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500895 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
896 "Linear Metrics", "Non-Linear Metrics");
897 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
898 "Bitmap Text", "No Bitmap Text");
899 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
900
901 if (fFontOverrides.fEdging) {
902 switch (fFont.getEdging()) {
903 case SkFont::Edging::kAlias:
904 paintTitle.append("Alias Text");
905 break;
906 case SkFont::Edging::kAntiAlias:
907 paintTitle.append("Antialias Text");
908 break;
909 case SkFont::Edging::kSubpixelAntiAlias:
910 paintTitle.append("Subpixel Antialias Text");
911 break;
912 }
913 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400914
Mike Reed3ae47332019-01-04 10:11:46 -0500915 if (fFontOverrides.fHinting) {
916 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400917 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500918 paintTitle.append("No Hinting");
919 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400920 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500921 paintTitle.append("Slight Hinting");
922 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400923 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500924 paintTitle.append("Normal Hinting");
925 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400926 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500927 paintTitle.append("Full Hinting");
928 break;
929 }
930 }
931 paintTitle.done();
932
Brian Osman92004802017-03-06 11:47:26 -0500933 switch (fColorMode) {
934 case ColorMode::kLegacy:
935 title.append(" Legacy 8888");
936 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500937 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500938 title.append(" ColorManaged 8888");
939 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500940 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500941 title.append(" ColorManaged F16");
942 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400943 case ColorMode::kColorManagedF16Norm:
944 title.append(" ColorManaged F16 Norm");
945 break;
Brian Osman92004802017-03-06 11:47:26 -0500946 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500947
Brian Osman92004802017-03-06 11:47:26 -0500948 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500949 int curPrimaries = -1;
950 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
951 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
952 curPrimaries = i;
953 break;
954 }
955 }
Brian Osman03115dc2018-11-26 13:55:19 -0500956 title.appendf(" %s Gamma %f",
957 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500958 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700959 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500960
Ben Wagner37c54032018-04-13 14:30:23 -0400961 const DisplayParams& params = fWindow->getRequestedDisplayParams();
962 if (fPixelGeometryOverrides) {
963 switch (params.fSurfaceProps.pixelGeometry()) {
964 case kUnknown_SkPixelGeometry:
965 title.append( " Flat");
966 break;
967 case kRGB_H_SkPixelGeometry:
968 title.append( " RGB");
969 break;
970 case kBGR_H_SkPixelGeometry:
971 title.append( " BGR");
972 break;
973 case kRGB_V_SkPixelGeometry:
974 title.append( " RGBV");
975 break;
976 case kBGR_V_SkPixelGeometry:
977 title.append( " BGRV");
978 break;
979 }
980 }
981
982 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
983 title.append(" DFT");
984 }
985
csmartdalton578f0642017-02-24 16:04:47 -0700986 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700987 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500988 int msaa = fWindow->sampleCount();
989 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700990 title.appendf(" MSAA: %i", msaa);
991 }
992 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700993
994 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -0700995 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700996 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
997 }
998
Brian Osman805a7272018-05-02 15:40:20 -0400999 if (kPerspective_Real == fPerspectiveMode) {
1000 title.append(" Perpsective (Real)");
1001 } else if (kPerspective_Fake == fPerspectiveMode) {
1002 title.append(" Perspective (Fake)");
1003 }
1004
brianosman05de2162016-05-06 13:28:57 -07001005 fWindow->setTitle(title.c_str());
1006}
1007
Florin Malitaab99c342018-01-16 16:23:03 -05001008int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001009
1010 if (!FLAGS_slide.isEmpty()) {
1011 int count = fSlides.count();
1012 for (int i = 0; i < count; i++) {
1013 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001014 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001015 }
1016 }
1017
1018 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1019 this->listNames();
1020 }
1021
Florin Malitaab99c342018-01-16 16:23:03 -05001022 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001023}
1024
Florin Malitaab99c342018-01-16 16:23:03 -05001025void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001026 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001027 for (const auto& slide : fSlides) {
1028 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001029 }
1030}
1031
Florin Malitaab99c342018-01-16 16:23:03 -05001032void Viewer::setCurrentSlide(int slide) {
1033 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001034
Florin Malitaab99c342018-01-16 16:23:03 -05001035 if (slide == fCurrentSlide) {
1036 return;
1037 }
1038
1039 if (fCurrentSlide >= 0) {
1040 fSlides[fCurrentSlide]->unload();
1041 }
1042
1043 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1044 SkIntToScalar(fWindow->height()));
1045 fCurrentSlide = slide;
1046 this->setupCurrentSlide();
1047}
1048
1049void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001050 if (fCurrentSlide >= 0) {
1051 // prepare dimensions for image slides
1052 fGesture.resetTouchState();
1053 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001054
Jim Van Verth0848fb02018-01-22 13:39:30 -05001055 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1056 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1057 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001058
Jim Van Verth0848fb02018-01-22 13:39:30 -05001059 // Start with a matrix that scales the slide to the available screen space
1060 if (fWindow->scaleContentToFit()) {
1061 if (windowRect.width() > 0 && windowRect.height() > 0) {
1062 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1063 }
liyuqiane46e4f02016-05-20 07:32:19 -07001064 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001065
1066 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001067 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001068
1069 this->updateTitle();
1070 this->updateUIState();
1071
1072 fStatsLayer.resetMeasurements();
1073
1074 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001075 }
jvanverthc265a922016-04-08 12:51:45 -07001076}
1077
Brian Osmanaba642c2020-02-06 12:52:25 -05001078#define MAX_ZOOM_LEVEL 8.0f
1079#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001080
jvanverth34524262016-05-04 13:49:13 -07001081void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001082 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001083 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001084 this->preTouchMatrixChanged();
1085}
Yuqian Li755778c2018-03-28 16:23:31 -04001086
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001087void Viewer::preTouchMatrixChanged() {
1088 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001089 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1090 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1091 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1092 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1093}
1094
Brian Osman805a7272018-05-02 15:40:20 -04001095SkMatrix Viewer::computePerspectiveMatrix() {
1096 SkScalar w = fWindow->width(), h = fWindow->height();
1097 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1098 SkPoint perspPts[4] = {
1099 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1100 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1101 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1102 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1103 };
1104 SkMatrix m;
1105 m.setPolyToPoly(orthoPts, perspPts, 4);
1106 return m;
1107}
1108
Yuqian Li755778c2018-03-28 16:23:31 -04001109SkMatrix Viewer::computePreTouchMatrix() {
1110 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001111
1112 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001113 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001114 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001115
1116 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1117 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001118
Brian Osman805a7272018-05-02 15:40:20 -04001119 if (kPerspective_Real == fPerspectiveMode) {
1120 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001121 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001122 }
1123
Yuqian Li755778c2018-03-28 16:23:31 -04001124 return m;
jvanverthc265a922016-04-08 12:51:45 -07001125}
1126
liyuqiand3cdbca2016-05-17 12:44:20 -07001127SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001128 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001129 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001130 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001131 return m;
jvanverthc265a922016-04-08 12:51:45 -07001132}
1133
Brian Osman621491e2017-02-28 15:45:01 -05001134void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001135 fPersistentCache.reset();
1136 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001137 fBackendType = backendType;
1138
1139 fWindow->detach();
1140
Brian Osman70d2f432017-11-08 09:54:10 -05001141#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001142 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1143 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001144 DisplayParams params = fWindow->getRequestedDisplayParams();
1145 delete fWindow;
1146 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001147
Brian Osman70d2f432017-11-08 09:54:10 -05001148 // re-register callbacks
1149 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001150 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001151 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001152 fWindow->pushLayer(&fImGuiLayer);
1153
Brian Osman70d2f432017-11-08 09:54:10 -05001154 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1155 // will still include our correct sample count. But the re-created fWindow will lose that
1156 // information. On Windows, we need to re-create the window when changing sample count,
1157 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1158 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1159 // as if we had never changed windows at all).
1160 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001161#endif
1162
Brian Osman70d2f432017-11-08 09:54:10 -05001163 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001164}
1165
Brian Osman92004802017-03-06 11:47:26 -05001166void Viewer::setColorMode(ColorMode colorMode) {
1167 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001168 this->updateTitle();
1169 fWindow->inval();
1170}
1171
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001172class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1173public:
Mike Reed3ae47332019-01-04 10:11:46 -05001174 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1175 SkFont* font, Viewer::SkFontFields* ffields)
1176 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001177 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001178 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1179 sk_sp<SkTextBlob>* cache) {
1180 bool blobWillChange = false;
1181 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001182 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1183 bool shouldDraw = this->filterFont(&filteredFont);
1184 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001185 blobWillChange = true;
1186 break;
1187 }
1188 }
1189 if (!blobWillChange) {
1190 return blob;
1191 }
1192
1193 SkTextBlobBuilder builder;
1194 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001195 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1196 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001197 if (!shouldDraw) {
1198 continue;
1199 }
1200
Mike Reed3ae47332019-01-04 10:11:46 -05001201 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001202
Ben Wagner41e40472018-09-24 13:01:54 -04001203 const SkTextBlobBuilder::RunBuffer& runBuffer
1204 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001205 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001206 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001207 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001208 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001209 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001210 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001211 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001212 it.glyphCount(), it.textSize(), SkString())
1213 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1214 uint32_t glyphCount = it.glyphCount();
1215 if (it.glyphs()) {
1216 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1217 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1218 }
1219 if (it.pos()) {
1220 size_t posSize = sizeof(decltype(*it.pos()));
1221 uint8_t positioning = it.positioning();
1222 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1223 }
1224 if (it.text()) {
1225 size_t textSize = sizeof(decltype(*it.text()));
1226 uint32_t textCount = it.textSize();
1227 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1228 }
1229 if (it.clusters()) {
1230 size_t clusterSize = sizeof(decltype(*it.clusters()));
1231 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1232 }
1233 }
1234 *cache = builder.make();
1235 return cache->get();
1236 }
1237 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1238 const SkPaint& paint) override {
1239 sk_sp<SkTextBlob> cache;
1240 this->SkPaintFilterCanvas::onDrawTextBlob(
1241 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1242 }
Mike Reed3ae47332019-01-04 10:11:46 -05001243 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001244 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001245 font->writable()->setSize(fFont->getSize());
1246 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001247 if (fFontOverrides->fScaleX) {
1248 font->writable()->setScaleX(fFont->getScaleX());
1249 }
1250 if (fFontOverrides->fSkewX) {
1251 font->writable()->setSkewX(fFont->getSkewX());
1252 }
Mike Reed3ae47332019-01-04 10:11:46 -05001253 if (fFontOverrides->fHinting) {
1254 font->writable()->setHinting(fFont->getHinting());
1255 }
Ben Wagner9613e452019-01-23 10:34:59 -05001256 if (fFontOverrides->fEdging) {
1257 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001258 }
Ben Wagner9613e452019-01-23 10:34:59 -05001259 if (fFontOverrides->fEmbolden) {
1260 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001261 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001262 if (fFontOverrides->fBaselineSnap) {
1263 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1264 }
Ben Wagner9613e452019-01-23 10:34:59 -05001265 if (fFontOverrides->fLinearMetrics) {
1266 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001267 }
Ben Wagner9613e452019-01-23 10:34:59 -05001268 if (fFontOverrides->fSubpixel) {
1269 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001270 }
Ben Wagner9613e452019-01-23 10:34:59 -05001271 if (fFontOverrides->fEmbeddedBitmaps) {
1272 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001273 }
Ben Wagner9613e452019-01-23 10:34:59 -05001274 if (fFontOverrides->fForceAutoHinting) {
1275 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001276 }
Ben Wagner9613e452019-01-23 10:34:59 -05001277
Mike Reed3ae47332019-01-04 10:11:46 -05001278 return true;
1279 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001280 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001281 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001282 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001283 }
Ben Wagner9613e452019-01-23 10:34:59 -05001284 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001285 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001286 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001287 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001288 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001289 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001290 return true;
1291 }
1292 SkPaint* fPaint;
1293 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001294 SkFont* fFont;
1295 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001296};
1297
Robert Phillips9882dae2019-03-04 11:00:10 -05001298void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001299 if (fCurrentSlide < 0) {
1300 return;
1301 }
1302
Robert Phillips9882dae2019-03-04 11:00:10 -05001303 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001304
Brian Osmanf750fbc2017-02-08 10:47:28 -05001305 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001306 SkSurface* slideSurface = surface;
1307 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001308 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001309
Brian Osmane0d4fba2017-03-15 10:24:55 -04001310 // 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 -05001311 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001312 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001313 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001314 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001315 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001316 }
1317
Brian Osman3ac99cf2017-12-01 11:23:53 -05001318 if (fSaveToSKP) {
1319 SkPictureRecorder recorder;
1320 SkCanvas* recorderCanvas = recorder.beginRecording(
1321 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001322 fSlides[fCurrentSlide]->draw(recorderCanvas);
1323 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1324 SkFILEWStream stream("sample_app.skp");
1325 picture->serialize(&stream);
1326 fSaveToSKP = false;
1327 }
1328
Brian Osmane9ed0f02018-11-26 14:50:05 -05001329 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001330 SkColorType colorType;
1331 switch (fColorMode) {
1332 case ColorMode::kLegacy:
1333 case ColorMode::kColorManaged8888:
1334 colorType = kN32_SkColorType;
1335 break;
1336 case ColorMode::kColorManagedF16:
1337 colorType = kRGBA_F16_SkColorType;
1338 break;
1339 case ColorMode::kColorManagedF16Norm:
1340 colorType = kRGBA_F16Norm_SkColorType;
1341 break;
1342 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001343
1344 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001345 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1346 slideCanvas->getProps(&props);
1347
Brian Osmane9ed0f02018-11-26 14:50:05 -05001348 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1349 return Window::kRaster_BackendType == this->fBackendType
1350 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001351 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001352 };
1353
Brian Osman03115dc2018-11-26 13:55:19 -05001354 // We need to render offscreen if we're...
1355 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1356 // ... in any raster mode, because the window surface is actually GL
1357 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001358 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001359 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001360 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001361 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001362 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001363 colorSpace != nullptr ||
1364 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001365
Brian Osmane9ed0f02018-11-26 14:50:05 -05001366 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001367 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001368 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001369 }
1370
Mike Reed59295352020-03-12 13:56:34 -04001371 SkPictureRecorder recorder;
1372 SkCanvas* recorderRestoreCanvas = nullptr;
1373 if (fDrawViaSerialize) {
1374 recorderRestoreCanvas = slideCanvas;
1375 slideCanvas = recorder.beginRecording(
1376 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1377 }
1378
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001379 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001380 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001381 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001382 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001383 if (fTiled) {
1384 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1385 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001386 for (int y = 0; y < fWindow->height(); y += tileH) {
1387 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001388 SkAutoCanvasRestore acr(slideCanvas, true);
1389 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1390 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001391 }
1392 }
1393
1394 // Draw borders between tiles
1395 if (fDrawTileBoundaries) {
1396 SkPaint border;
1397 border.setColor(0x60FF00FF);
1398 border.setStyle(SkPaint::kStroke_Style);
1399 for (int y = 0; y < fWindow->height(); y += tileH) {
1400 for (int x = 0; x < fWindow->width(); x += tileW) {
1401 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1402 }
1403 }
1404 }
1405 } else {
1406 slideCanvas->concat(this->computeMatrix());
1407 if (kPerspective_Real == fPerspectiveMode) {
1408 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1409 }
Mike Reed3ae47332019-01-04 10:11:46 -05001410 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001411 fSlides[fCurrentSlide]->draw(&filterCanvas);
1412 }
Brian Osman56a24812017-12-19 11:15:16 -05001413 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001414 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001415
Mike Reed59295352020-03-12 13:56:34 -04001416 if (recorderRestoreCanvas) {
1417 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1418 auto data = picture->serialize();
1419 slideCanvas = recorderRestoreCanvas;
1420 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1421 }
1422
Brian Osman1df161a2017-02-09 12:10:20 -05001423 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001424 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001425 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001426 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001427
1428 // If we rendered offscreen, snap an image and push the results to the window's canvas
1429 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001430 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001431
Robert Phillips9882dae2019-03-04 11:00:10 -05001432 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001433 SkPaint paint;
1434 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001435 int prePerspectiveCount = canvas->save();
1436 if (kPerspective_Fake == fPerspectiveMode) {
1437 paint.setFilterQuality(kHigh_SkFilterQuality);
1438 canvas->clear(SK_ColorWHITE);
1439 canvas->concat(this->computePerspectiveMatrix());
1440 }
Brian Osman03115dc2018-11-26 13:55:19 -05001441 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001442 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001443 }
Mike Reed376d8122019-03-14 11:39:02 -04001444
1445 if (fShowSlideDimensions) {
1446 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1447 SkPaint paint;
1448 paint.setColor(0x40FFFF00);
1449 surface->getCanvas()->drawRect(r, paint);
1450 }
liyuqian6f163d22016-06-13 12:26:45 -07001451}
1452
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001453void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001454 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001455 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001456}
Jim Van Verth6f449692017-02-14 15:16:46 -05001457
Robert Phillips9882dae2019-03-04 11:00:10 -05001458void Viewer::onPaint(SkSurface* surface) {
1459 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001460
Robert Phillips9882dae2019-03-04 11:00:10 -05001461 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001462
Brian Osmand67e5182017-12-08 16:46:09 -05001463 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001464
1465 if (GrContext* ctx = fWindow->getGrContext()) {
1466 // Clean out cache items that haven't been used in more than 10 seconds.
1467 ctx->performDeferredCleanup(std::chrono::seconds(10));
1468 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001469}
1470
Ben Wagnera1915972018-08-09 15:06:19 -04001471void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001472 if (fCurrentSlide >= 0) {
1473 fSlides[fCurrentSlide]->resize(width, height);
1474 }
Ben Wagnera1915972018-08-09 15:06:19 -04001475}
1476
Florin Malitacefc1b92018-02-19 21:43:47 -05001477SkPoint Viewer::mapEvent(float x, float y) {
1478 const auto m = this->computeMatrix();
1479 SkMatrix inv;
1480
1481 SkAssertResult(m.invert(&inv));
1482
1483 return inv.mapXY(x, y);
1484}
1485
Hal Canaryb1f411a2019-08-29 10:39:22 -04001486bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001487 if (GestureDevice::kMouse == fGestureDevice) {
1488 return false;
1489 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001490
1491 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001492 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001493 fWindow->inval();
1494 return true;
1495 }
1496
liyuqiand3cdbca2016-05-17 12:44:20 -07001497 void* castedOwner = reinterpret_cast<void*>(owner);
1498 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001499 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001500 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001501#if defined(SK_BUILD_FOR_IOS)
1502 // TODO: move IOS swipe detection higher up into the platform code
1503 SkPoint dir;
1504 if (fGesture.isFling(&dir)) {
1505 // swiping left or right
1506 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1507 if (dir.fX < 0) {
1508 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1509 fCurrentSlide + 1 : 0);
1510 } else {
1511 this->setCurrentSlide(fCurrentSlide > 0 ?
1512 fCurrentSlide - 1 : fSlides.count() - 1);
1513 }
1514 }
1515 fGesture.reset();
1516 }
1517#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001518 break;
1519 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001520 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001521 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001522 break;
1523 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001524 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001525 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001526 break;
1527 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001528 default: {
1529 // kLeft and kRight are only for swipes
1530 SkASSERT(false);
1531 break;
1532 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001533 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001534 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001535 fWindow->inval();
1536 return true;
1537}
1538
Hal Canaryb1f411a2019-08-29 10:39:22 -04001539bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001540 if (GestureDevice::kTouch == fGestureDevice) {
1541 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001542 }
Brian Osman16c81a12017-12-20 11:58:34 -05001543
Florin Malitacefc1b92018-02-19 21:43:47 -05001544 const auto slidePt = this->mapEvent(x, y);
1545 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1546 fWindow->inval();
1547 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001548 }
1549
1550 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001551 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001552 fGesture.touchEnd(nullptr);
1553 break;
1554 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001555 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001556 fGesture.touchBegin(nullptr, x, y);
1557 break;
1558 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001559 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001560 fGesture.touchMoved(nullptr, x, y);
1561 break;
1562 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001563 default: {
1564 SkASSERT(false); // shouldn't see kRight or kLeft here
1565 break;
1566 }
Brian Osman16c81a12017-12-20 11:58:34 -05001567 }
1568 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1569
Hal Canaryb1f411a2019-08-29 10:39:22 -04001570 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001571 fWindow->inval();
1572 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001573 return true;
1574}
1575
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001576bool Viewer::onFling(skui::InputState state) {
1577 if (skui::InputState::kRight == state) {
1578 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1579 return true;
1580 } else if (skui::InputState::kLeft == state) {
1581 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1582 return true;
1583 }
1584 return false;
1585}
1586
1587bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1588 switch (state) {
1589 case skui::InputState::kDown:
1590 fGesture.startZoom();
1591 return true;
1592 break;
1593 case skui::InputState::kMove:
1594 fGesture.updateZoom(scale, x, y, x, y);
1595 return true;
1596 break;
1597 case skui::InputState::kUp:
1598 fGesture.endZoom();
1599 return true;
1600 break;
1601 default:
1602 SkASSERT(false);
1603 break;
1604 }
1605
1606 return false;
1607}
1608
Brian Osmana109e392017-02-24 09:49:14 -05001609static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001610 // The gamut image covers a (0.8 x 0.9) shaped region
1611 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001612
1613 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1614 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1615 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001616 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1617 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1618 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001619
Brian Osman535c5e32019-02-09 16:32:58 -05001620 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1621 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1622 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1623 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1624 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001625}
1626
Ben Wagner3627d2e2018-06-26 14:23:20 -04001627static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001628 ImGui::DragCanvas dc(pt);
1629 dc.fillColor(IM_COL32(0, 0, 0, 128));
1630 dc.dragPoint(pt);
1631 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001632}
1633
Brian Osman9bb47cf2018-04-26 15:55:00 -04001634static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001635 ImGui::DragCanvas dc(pts);
1636 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001637
Brian Osman535c5e32019-02-09 16:32:58 -05001638 for (int i = 0; i < 4; ++i) {
1639 dc.dragPoint(pts + i);
1640 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001641
Brian Osman535c5e32019-02-09 16:32:58 -05001642 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1643 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1644 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1645 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001646
Brian Osman535c5e32019-02-09 16:32:58 -05001647 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001648}
1649
Brian Osmand67e5182017-12-08 16:46:09 -05001650void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001651 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1652 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001653 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001654 }
1655
1656 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001657 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1658 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001659 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001660 DisplayParams params = fWindow->getRequestedDisplayParams();
1661 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001662 const GrContext* ctx = fWindow->getGrContext();
1663
Brian Osmana109e392017-02-24 09:49:14 -05001664 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1665 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001666 if (ImGui::CollapsingHeader("Backend")) {
1667 int newBackend = static_cast<int>(fBackendType);
1668 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1669 ImGui::SameLine();
1670 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001671#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1672 ImGui::SameLine();
1673 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1674#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001675#if defined(SK_DAWN)
1676 ImGui::SameLine();
1677 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1678#endif
Brian Osman621491e2017-02-28 15:45:01 -05001679#if defined(SK_VULKAN)
1680 ImGui::SameLine();
1681 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1682#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001683#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001684 ImGui::SameLine();
1685 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1686#endif
Brian Osman621491e2017-02-28 15:45:01 -05001687 if (newBackend != fBackendType) {
1688 fDeferredActions.push_back([=]() {
1689 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1690 });
1691 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001692
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001693 bool* wire = &params.fGrContextOptions.fWireframeMode;
1694 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1695 paramsChanged = true;
1696 }
Brian Salomon99a33902017-03-07 15:16:34 -05001697
Brian Osman28b12522017-03-08 17:10:24 -05001698 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001699 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001700 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001701 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1702 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1703 1;
1704
1705 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1706 if (maxMSAA >= 4) {
1707 ImGui::Text("MSAA: ");
1708
1709 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1710 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1711 // don't show it in the list.
1712 if (curMSAA == 2) {
1713 continue;
1714 }
1715 ImGui::SameLine();
1716 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1717 &sampleCount, curMSAA);
1718 }
1719 }
Brian Osman28b12522017-03-08 17:10:24 -05001720
1721 if (sampleCount != params.fMSAASampleCount) {
1722 params.fMSAASampleCount = sampleCount;
1723 paramsChanged = true;
1724 }
1725 }
1726
Ben Wagner37c54032018-04-13 14:30:23 -04001727 int pixelGeometryIdx = 0;
1728 if (fPixelGeometryOverrides) {
1729 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1730 }
1731 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1732 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1733 {
1734 uint32_t flags = params.fSurfaceProps.flags();
1735 if (pixelGeometryIdx == 0) {
1736 fPixelGeometryOverrides = false;
1737 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1738 } else {
1739 fPixelGeometryOverrides = true;
1740 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1741 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1742 }
1743 paramsChanged = true;
1744 }
1745
1746 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1747 if (ImGui::Checkbox("DFT", &useDFT)) {
1748 uint32_t flags = params.fSurfaceProps.flags();
1749 if (useDFT) {
1750 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1751 } else {
1752 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1753 }
1754 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1755 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1756 paramsChanged = true;
1757 }
1758
Brian Osman8a9de3d2017-03-01 14:59:05 -05001759 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001760 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001761 auto prButton = [&](GpuPathRenderers x) {
1762 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001763 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1764 params.fGrContextOptions.fGpuPathRenderers = x;
1765 paramsChanged = true;
1766 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001767 }
1768 };
1769
1770 if (!ctx) {
1771 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001772 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001773 const auto* caps = ctx->priv().caps();
1774 prButton(GpuPathRenderers::kDefault);
1775 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001776 if (caps->shaderCaps()->tessellationSupport()) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001777 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001778 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001779 if (caps->shaderCaps()->pathRenderingSupport()) {
1780 prButton(GpuPathRenderers::kStencilAndCover);
1781 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001782 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001783 if (1 == fWindow->sampleCount()) {
1784 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1785 prButton(GpuPathRenderers::kCoverageCounting);
1786 }
1787 prButton(GpuPathRenderers::kSmall);
1788 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001789 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001790 prButton(GpuPathRenderers::kNone);
1791 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001792 ImGui::TreePop();
1793 }
Brian Osman621491e2017-02-28 15:45:01 -05001794 }
1795
Ben Wagner964571d2019-03-08 12:35:06 -05001796 if (ImGui::CollapsingHeader("Tiling")) {
1797 ImGui::Checkbox("Enable", &fTiled);
1798 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1799 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1800 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1801 }
1802
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001803 if (ImGui::CollapsingHeader("Transform")) {
1804 float zoom = fZoomLevel;
1805 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1806 fZoomLevel = zoom;
1807 this->preTouchMatrixChanged();
1808 paramsChanged = true;
1809 }
1810 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001811 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001812 fRotation = deg;
1813 this->preTouchMatrixChanged();
1814 paramsChanged = true;
1815 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001816 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1817 if (ImGui_DragLocation(&fOffset)) {
1818 this->preTouchMatrixChanged();
1819 paramsChanged = true;
1820 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001821 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1822 this->preTouchMatrixChanged();
1823 paramsChanged = true;
1824 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001825 }
Brian Osman805a7272018-05-02 15:40:20 -04001826 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1827 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1828 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001829 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001830 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001831 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001832 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001833 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001834 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001835 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001836 }
1837
Ben Wagnera580fb32018-04-17 11:16:32 -04001838 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001839 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001840 if (fPaintOverrides.fAntiAlias) {
1841 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001842 }
1843 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001844 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001845 {
1846 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1847 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001848 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001849 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1850 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001851 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001852 fPaintOverrides.fAntiAlias = true;
1853 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001854 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001855 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001856 case SkPaintFields::AntiAliasState::Alias:
1857 break;
1858 case SkPaintFields::AntiAliasState::Normal:
1859 break;
1860 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1861 gSkUseAnalyticAA = true;
1862 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001863 break;
1864 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1865 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001866 break;
1867 }
1868 }
1869 paramsChanged = true;
1870 }
1871
Ben Wagner99a78dc2018-05-09 18:23:51 -04001872 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001873 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001874 bool (SkPaint::* isFlag)() const,
1875 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001876 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001877 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001878 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001879 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001880 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001881 if (ImGui::Combo(label, &itemIndex, items)) {
1882 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001883 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001884 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001885 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001886 (fPaint.*setFlag)(itemIndex == 2);
1887 }
1888 paramsChanged = true;
1889 }
1890 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001891
Ben Wagner99a78dc2018-05-09 18:23:51 -04001892 paintFlag("Dither",
1893 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001894 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001895 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001896
1897 int filterQualityIdx = 0;
1898 if (fPaintOverrides.fFilterQuality) {
1899 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1900 }
1901 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1902 "Default\0None\0Low\0Medium\0High\0\0"))
1903 {
1904 if (filterQualityIdx == 0) {
1905 fPaintOverrides.fFilterQuality = false;
1906 fPaint.setFilterQuality(kNone_SkFilterQuality);
1907 } else {
1908 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1909 fPaintOverrides.fFilterQuality = true;
1910 }
1911 paramsChanged = true;
1912 }
Ben Wagner9613e452019-01-23 10:34:59 -05001913 }
Hal Canary02738a82019-01-21 18:51:32 +00001914
Ben Wagner9613e452019-01-23 10:34:59 -05001915 if (ImGui::CollapsingHeader("Font")) {
1916 int hintingIdx = 0;
1917 if (fFontOverrides.fHinting) {
1918 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1919 }
1920 if (ImGui::Combo("Hinting", &hintingIdx,
1921 "Default\0None\0Slight\0Normal\0Full\0\0"))
1922 {
1923 if (hintingIdx == 0) {
1924 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001925 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001926 } else {
1927 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1928 fFontOverrides.fHinting = true;
1929 }
1930 paramsChanged = true;
1931 }
Hal Canary02738a82019-01-21 18:51:32 +00001932
Ben Wagner9613e452019-01-23 10:34:59 -05001933 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1934 bool SkFontFields::* flag,
1935 bool (SkFont::* isFlag)() const,
1936 void (SkFont::* setFlag)(bool) )
1937 {
1938 int itemIndex = 0;
1939 if (fFontOverrides.*flag) {
1940 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1941 }
1942 if (ImGui::Combo(label, &itemIndex, items)) {
1943 if (itemIndex == 0) {
1944 fFontOverrides.*flag = false;
1945 } else {
1946 fFontOverrides.*flag = true;
1947 (fFont.*setFlag)(itemIndex == 2);
1948 }
1949 paramsChanged = true;
1950 }
1951 };
Hal Canary02738a82019-01-21 18:51:32 +00001952
Ben Wagner9613e452019-01-23 10:34:59 -05001953 fontFlag("Fake Bold Glyphs",
1954 "Default\0No Fake Bold\0Fake Bold\0\0",
1955 &SkFontFields::fEmbolden,
1956 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001957
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001958 fontFlag("Baseline Snapping",
1959 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1960 &SkFontFields::fBaselineSnap,
1961 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1962
Ben Wagner9613e452019-01-23 10:34:59 -05001963 fontFlag("Linear Text",
1964 "Default\0No Linear Text\0Linear Text\0\0",
1965 &SkFontFields::fLinearMetrics,
1966 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001967
Ben Wagner9613e452019-01-23 10:34:59 -05001968 fontFlag("Subpixel Position Glyphs",
1969 "Default\0Pixel Text\0Subpixel Text\0\0",
1970 &SkFontFields::fSubpixel,
1971 &SkFont::isSubpixel, &SkFont::setSubpixel);
1972
1973 fontFlag("Embedded Bitmap Text",
1974 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1975 &SkFontFields::fEmbeddedBitmaps,
1976 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1977
1978 fontFlag("Force Auto-Hinting",
1979 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1980 &SkFontFields::fForceAutoHinting,
1981 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1982
1983 int edgingIdx = 0;
1984 if (fFontOverrides.fEdging) {
1985 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1986 }
1987 if (ImGui::Combo("Edging", &edgingIdx,
1988 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1989 {
1990 if (edgingIdx == 0) {
1991 fFontOverrides.fEdging = false;
1992 fFont.setEdging(SkFont::Edging::kAlias);
1993 } else {
1994 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1995 fFontOverrides.fEdging = true;
1996 }
1997 paramsChanged = true;
1998 }
1999
Ben Wagner15a8d572019-03-21 13:35:44 -04002000 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2001 if (fFontOverrides.fSize) {
2002 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002003 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002004 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002005 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002006 fFontOverrides.fSizeRange[0],
2007 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002008 "%.6f", 2.0f))
2009 {
Mike Reed3ae47332019-01-04 10:11:46 -05002010 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002011 paramsChanged = true;
2012 }
2013 }
2014
2015 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2016 if (fFontOverrides.fScaleX) {
2017 float scaleX = fFont.getScaleX();
2018 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2019 fFont.setScaleX(scaleX);
2020 paramsChanged = true;
2021 }
2022 }
2023
2024 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2025 if (fFontOverrides.fSkewX) {
2026 float skewX = fFont.getSkewX();
2027 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2028 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002029 paramsChanged = true;
2030 }
2031 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002032 }
2033
Mike Reed81f60ec2018-05-15 10:09:52 -04002034 {
2035 SkMetaData controls;
2036 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2037 if (ImGui::CollapsingHeader("Current Slide")) {
2038 SkMetaData::Iter iter(controls);
2039 const char* name;
2040 SkMetaData::Type type;
2041 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002042 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002043 if (type == SkMetaData::kScalar_Type) {
2044 float val[3];
2045 SkASSERT(count == 3);
2046 controls.findScalars(name, &count, val);
2047 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2048 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002049 }
Ben Wagner110c7032019-03-22 17:03:59 -04002050 } else if (type == SkMetaData::kBool_Type) {
2051 bool val;
2052 SkASSERT(count == 1);
2053 controls.findBool(name, &val);
2054 if (ImGui::Checkbox(name, &val)) {
2055 controls.setBool(name, val);
2056 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002057 }
2058 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002059 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002060 }
2061 }
2062 }
2063
Ben Wagner7a3c6742018-04-23 10:01:07 -04002064 if (fShowSlidePicker) {
2065 ImGui::SetNextTreeNodeOpen(true);
2066 }
Brian Osman79086b92017-02-10 13:36:16 -05002067 if (ImGui::CollapsingHeader("Slide")) {
2068 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002069 static ImVector<const char*> filteredSlideNames;
2070 static ImVector<int> filteredSlideIndices;
2071
Brian Osmanfce09c52017-11-14 15:32:20 -05002072 if (fShowSlidePicker) {
2073 ImGui::SetKeyboardFocusHere();
2074 fShowSlidePicker = false;
2075 }
2076
Brian Osman79086b92017-02-10 13:36:16 -05002077 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002078 filteredSlideNames.clear();
2079 filteredSlideIndices.clear();
2080 int filteredIndex = 0;
2081 for (int i = 0; i < fSlides.count(); ++i) {
2082 const char* slideName = fSlides[i]->getName().c_str();
2083 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2084 if (i == fCurrentSlide) {
2085 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002086 }
Brian Osmanf479e422017-11-08 13:11:36 -05002087 filteredSlideNames.push_back(slideName);
2088 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002089 }
Brian Osman79086b92017-02-10 13:36:16 -05002090 }
Brian Osmanf479e422017-11-08 13:11:36 -05002091
Brian Osmanf479e422017-11-08 13:11:36 -05002092 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2093 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002094 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002095 }
2096 }
Brian Osmana109e392017-02-24 09:49:14 -05002097
2098 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002099 ColorMode newMode = fColorMode;
2100 auto cmButton = [&](ColorMode mode, const char* label) {
2101 if (ImGui::RadioButton(label, mode == fColorMode)) {
2102 newMode = mode;
2103 }
2104 };
2105
2106 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002107 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2108 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002109 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002110
2111 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002112 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002113 }
2114
2115 // Pick from common gamuts:
2116 int primariesIdx = 4; // Default: Custom
2117 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2118 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2119 primariesIdx = i;
2120 break;
2121 }
2122 }
2123
Brian Osman03115dc2018-11-26 13:55:19 -05002124 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002125 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002126
Brian Osmana109e392017-02-24 09:49:14 -05002127 if (ImGui::Combo("Primaries", &primariesIdx,
2128 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2129 if (primariesIdx >= 0 && primariesIdx <= 3) {
2130 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2131 }
2132 }
2133
2134 // Allow direct editing of gamut
2135 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2136 }
Brian Osman207d4102019-01-10 09:40:58 -05002137
2138 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002139 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002140 if (ImGui::Checkbox("Pause", &isPaused)) {
2141 fAnimTimer.togglePauseResume();
2142 }
Brian Osman707d2022019-01-10 11:27:34 -05002143
2144 float speed = fAnimTimer.getSpeed();
2145 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2146 fAnimTimer.setSpeed(speed);
2147 }
Brian Osman207d4102019-01-10 09:40:58 -05002148 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002149
Brian Osmanfd7657c2019-04-25 11:34:07 -04002150 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2151#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2152 || Window::kANGLE_BackendType == fBackendType
2153#endif
2154 ;
2155
2156 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2157 // than GL, we need to force it on. Just do that on the first frame after the backend
2158 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002159 if (!backendIsGL &&
2160 params.fGrContextOptions.fShaderCacheStrategy !=
2161 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2162 params.fGrContextOptions.fShaderCacheStrategy =
2163 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002164 paramsChanged = true;
2165 fPersistentCache.reset();
2166 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002167 // To re-load shaders from the currently active programs, we flush all caches on one
2168 // frame, then set a flag to poll the cache on the next frame.
2169 static bool gLoadPending = false;
2170 if (gLoadPending) {
2171 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2172 int hitCount) {
2173 CachedGLSL& entry(fCachedGLSL.push_back());
2174 entry.fKey = key;
2175 SkMD5 hash;
2176 hash.write(key->bytes(), key->size());
2177 SkMD5::Digest digest = hash.finish();
2178 for (int i = 0; i < 16; ++i) {
2179 entry.fKeyString.appendf("%02x", digest.data[i]);
2180 }
2181
Brian Osmana66081d2019-09-03 14:59:26 -04002182 SkReader32 reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002183 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002184 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2185 entry.fInputs,
2186 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002187 };
2188 fCachedGLSL.reset();
2189 fPersistentCache.foreach(collectShaders);
2190 gLoadPending = false;
2191 }
2192
2193 // Defer actually doing the load/save logic so that we can trigger a save when we
2194 // start or finish hovering on a tree node in the list below:
2195 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002196 bool doSave = ImGui::Button("Save");
2197 if (backendIsGL) {
2198 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002199 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2200 GrContextOptions::ShaderCacheStrategy::kSkSL;
2201 if (ImGui::Checkbox("SkSL", &sksl)) {
2202 params.fGrContextOptions.fShaderCacheStrategy = sksl
2203 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2204 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002205 paramsChanged = true;
2206 doLoad = true;
2207 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2208 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002209 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002210
2211 ImGui::BeginChild("##ScrollingRegion");
2212 for (auto& entry : fCachedGLSL) {
2213 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2214 bool hovered = ImGui::IsItemHovered();
2215 if (hovered != entry.fHovered) {
2216 // Force a save to patch the highlight shader in/out
2217 entry.fHovered = hovered;
2218 doSave = true;
2219 }
2220 if (inTreeNode) {
2221 // Full width, and a reasonable amount of space for each shader.
2222 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2223 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2224 boxSize);
2225 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2226 boxSize);
2227 ImGui::TreePop();
2228 }
2229 }
2230 ImGui::EndChild();
2231
2232 if (doLoad) {
2233 fPersistentCache.reset();
2234 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2235 gLoadPending = true;
2236 }
2237 if (doSave) {
2238 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002239 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002240 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2241 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002242
Brian Osman072e6fc2019-06-12 11:35:41 -04002243 SkSL::String highlight;
2244 if (!sksl) {
2245 highlight = shaderCaps->versionDeclString();
2246 if (shaderCaps->usesPrecisionModifiers()) {
2247 highlight.append("precision mediump float;\n");
2248 }
Brian Osman5bee3902019-05-07 09:55:45 -04002249 }
2250 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002251 highlight.appendf("out %s sk_FragColor;\n"
2252 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2253 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002254
2255 fPersistentCache.reset();
2256 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2257 for (auto& entry : fCachedGLSL) {
2258 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2259 if (entry.fHovered) {
2260 entry.fShader[kFragment_GrShaderType] = highlight;
2261 }
2262
Brian Osmana085a412019-04-25 09:44:43 -04002263 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2264 entry.fShader,
2265 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002266 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002267 fPersistentCache.store(*entry.fKey, *data);
2268
2269 entry.fShader[kFragment_GrShaderType] = backup;
2270 }
2271 }
2272 }
Brian Osman79086b92017-02-10 13:36:16 -05002273 }
Brian Salomon99a33902017-03-07 15:16:34 -05002274 if (paramsChanged) {
2275 fDeferredActions.push_back([=]() {
2276 fWindow->setRequestedDisplayParams(params);
2277 fWindow->inval();
2278 this->updateTitle();
2279 });
2280 }
Brian Osman79086b92017-02-10 13:36:16 -05002281 ImGui::End();
2282 }
2283
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002284 if (gShaderErrorHandler.fErrors.count()) {
2285 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2286 ImGui::Begin("Shader Errors");
2287 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2288 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002289 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2290 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2291 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2292 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002293 }
2294 ImGui::End();
2295 gShaderErrorHandler.reset();
2296 }
2297
Brian Osmanf6877092017-02-13 09:39:57 -05002298 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002299 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2300 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002301 static int zoomFactor = 8;
2302 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002303 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002304 }
2305 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2306 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002307 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002308 }
Brian Osmanf6877092017-02-13 09:39:57 -05002309
Ben Wagner3627d2e2018-06-26 14:23:20 -04002310 if (!fZoomWindowFixed) {
2311 ImVec2 mousePos = ImGui::GetMousePos();
2312 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2313 }
2314 SkScalar x = fZoomWindowLocation.x();
2315 SkScalar y = fZoomWindowLocation.y();
2316 int xInt = SkScalarRoundToInt(x);
2317 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002318 ImVec2 avail = ImGui::GetContentRegionAvail();
2319
Brian Osmanead517d2017-11-13 15:36:36 -05002320 uint32_t pixel = 0;
2321 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002322 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002323 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002324 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002325 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002326 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002327 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2328 }
2329
Brian Osmand67e5182017-12-08 16:46:09 -05002330 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002331 // Translate so the region of the image that's under the mouse cursor is centered
2332 // in the zoom canvas:
2333 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002334 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2335 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002336 c->drawImage(this->fLastImage, 0, 0);
2337
2338 SkPaint outline;
2339 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002340 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002341 });
Brian Osmanf6877092017-02-13 09:39:57 -05002342 }
2343
2344 ImGui::End();
2345 }
Brian Osman79086b92017-02-10 13:36:16 -05002346}
2347
liyuqian2edb0f42016-07-06 14:11:32 -07002348void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002349 for (int i = 0; i < fDeferredActions.count(); ++i) {
2350 fDeferredActions[i]();
2351 }
2352 fDeferredActions.reset();
2353
Brian Osman56a24812017-12-19 11:15:16 -05002354 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002355 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002356 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002357 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002358
Brian Osman79086b92017-02-10 13:36:16 -05002359 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002360 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2361 // not be visible, though. So we need to redraw if there is at least one visible window, or
2362 // more than one active window. Newly created windows are active but not visible for one frame
2363 // while they determine their layout and sizing.
2364 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2365 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002366 fWindow->inval();
2367 }
jvanverth9f372462016-04-06 06:08:59 -07002368}
liyuqiane5a6cd92016-05-27 08:52:52 -07002369
Florin Malitab632df72018-06-18 21:23:06 -04002370template <typename OptionsFunc>
2371static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2372 OptionsFunc&& optionsFunc) {
2373 writer.beginObject();
2374 {
2375 writer.appendString(kName , name);
2376 writer.appendString(kValue, value);
2377
2378 writer.beginArray(kOptions);
2379 {
2380 optionsFunc(writer);
2381 }
2382 writer.endArray();
2383 }
2384 writer.endObject();
2385}
2386
2387
liyuqiane5a6cd92016-05-27 08:52:52 -07002388void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002389 if (!fWindow) {
2390 return;
2391 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002392 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002393 return; // Surface hasn't been created yet.
2394 }
2395
Florin Malitab632df72018-06-18 21:23:06 -04002396 SkDynamicMemoryWStream memStream;
2397 SkJSONWriter writer(&memStream);
2398 writer.beginArray();
2399
liyuqianb73c24b2016-06-03 08:47:23 -07002400 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002401 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2402 [this](SkJSONWriter& writer) {
2403 for(const auto& slide : fSlides) {
2404 writer.appendString(slide->getName().c_str());
2405 }
2406 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002407
liyuqianb73c24b2016-06-03 08:47:23 -07002408 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002409 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2410 [](SkJSONWriter& writer) {
2411 for (const auto& str : kBackendTypeStrings) {
2412 writer.appendString(str);
2413 }
2414 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002415
csmartdalton578f0642017-02-24 16:04:47 -07002416 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002417 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2418 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2419 [this](SkJSONWriter& writer) {
2420 writer.appendS32(0);
2421
2422 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2423 return;
2424 }
2425
2426 for (int msaa : {4, 8, 16}) {
2427 writer.appendS32(msaa);
2428 }
2429 });
csmartdalton578f0642017-02-24 16:04:47 -07002430
csmartdalton61cd31a2017-02-27 17:00:53 -07002431 // Path renderer state
2432 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002433 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2434 [this](SkJSONWriter& writer) {
2435 const GrContext* ctx = fWindow->getGrContext();
2436 if (!ctx) {
2437 writer.appendString("Software");
2438 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002439 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002440 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2441 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002442 if (caps->shaderCaps()->tessellationSupport()) {
2443 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002444 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002445 }
Florin Malitab632df72018-06-18 21:23:06 -04002446 if (caps->shaderCaps()->pathRenderingSupport()) {
2447 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002448 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002449 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002450 }
2451 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002452 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2453 writer.appendString(
2454 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2455 }
2456 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2457 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002458 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002459 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002460 }
2461 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002462
liyuqianb73c24b2016-06-03 08:47:23 -07002463 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002464 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2465 [this](SkJSONWriter& writer) {
2466 writer.appendString(kSoftkeyHint);
2467 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2468 writer.appendString(softkey.c_str());
2469 }
2470 });
liyuqianb73c24b2016-06-03 08:47:23 -07002471
Florin Malitab632df72018-06-18 21:23:06 -04002472 writer.endArray();
2473 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002474
Florin Malitab632df72018-06-18 21:23:06 -04002475 auto data = memStream.detachAsData();
2476
2477 // TODO: would be cool to avoid this copy
2478 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2479
2480 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002481}
2482
2483void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002484 // For those who will add more features to handle the state change in this function:
2485 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2486 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2487 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002488 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002489 for (int i = 0; i < fSlides.count(); ++i) {
2490 if (fSlides[i]->getName().equals(stateValue)) {
2491 this->setCurrentSlide(i);
2492 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002493 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002494 }
Florin Malitaab99c342018-01-16 16:23:03 -05002495
2496 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002497 } else if (stateName.equals(kBackendStateName)) {
2498 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2499 if (stateValue.equals(kBackendTypeStrings[i])) {
2500 if (fBackendType != i) {
2501 fBackendType = (sk_app::Window::BackendType)i;
2502 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002503 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002504 }
2505 break;
2506 }
2507 }
csmartdalton578f0642017-02-24 16:04:47 -07002508 } else if (stateName.equals(kMSAAStateName)) {
2509 DisplayParams params = fWindow->getRequestedDisplayParams();
2510 int sampleCount = atoi(stateValue.c_str());
2511 if (sampleCount != params.fMSAASampleCount) {
2512 params.fMSAASampleCount = sampleCount;
2513 fWindow->setRequestedDisplayParams(params);
2514 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002515 this->updateTitle();
2516 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002517 }
2518 } else if (stateName.equals(kPathRendererStateName)) {
2519 DisplayParams params = fWindow->getRequestedDisplayParams();
2520 for (const auto& pair : gPathRendererNames) {
2521 if (pair.second == stateValue.c_str()) {
2522 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2523 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2524 fWindow->setRequestedDisplayParams(params);
2525 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002526 this->updateTitle();
2527 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002528 }
2529 break;
2530 }
csmartdalton578f0642017-02-24 16:04:47 -07002531 }
liyuqianb73c24b2016-06-03 08:47:23 -07002532 } else if (stateName.equals(kSoftkeyStateName)) {
2533 if (!stateValue.equals(kSoftkeyHint)) {
2534 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002535 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002536 }
liyuqian2edb0f42016-07-06 14:11:32 -07002537 } else if (stateName.equals(kRefreshStateName)) {
2538 // This state is actually NOT in the UI state.
2539 // We use this to allow Android to quickly set bool fRefresh.
2540 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002541 } else {
2542 SkDebugf("Unknown stateName: %s", stateName.c_str());
2543 }
2544}
Brian Osman79086b92017-02-10 13:36:16 -05002545
Hal Canaryb1f411a2019-08-29 10:39:22 -04002546bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002547 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002548}
2549
Hal Canaryb1f411a2019-08-29 10:39:22 -04002550bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002551 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002552 fWindow->inval();
2553 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002554 } else {
2555 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002556 }
Brian Osman79086b92017-02-10 13:36:16 -05002557}