blob: 31d075508631697863eb93ce1034deac8064721a [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"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrGpu.h"
25#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070026#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
28#include "src/utils/SkJSONWriter.h"
29#include "src/utils/SkOSPath.h"
30#include "tools/Resources.h"
31#include "tools/ToolUtils.h"
32#include "tools/flags/CommandLineFlags.h"
33#include "tools/flags/CommonFlags.h"
34#include "tools/trace/EventTracingPriv.h"
35#include "tools/viewer/BisectSlide.h"
36#include "tools/viewer/GMSlide.h"
37#include "tools/viewer/ImageSlide.h"
38#include "tools/viewer/ParticlesSlide.h"
39#include "tools/viewer/SKPSlide.h"
40#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050041#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "tools/viewer/SlideDir.h"
43#include "tools/viewer/SvgSlide.h"
44#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070045
Hal Canaryc640d0d2018-06-13 09:59:02 -040046#include <stdlib.h>
47#include <map>
48
Hal Canary8a001442018-09-19 11:31:27 -040049#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040050#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040051
Florin Malita87ccf332018-05-04 12:23:24 -040052#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040054#endif
55
Brian Osman5e7fbfd2019-05-03 13:13:35 -040056class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
57public:
58 void compileError(const char* shader, const char* errors) override {
59 fShaders.push_back(SkString(shader));
60 fErrors.push_back(SkString(errors));
61 }
62
63 void reset() {
64 fShaders.reset();
65 fErrors.reset();
66 }
67
68 SkTArray<SkString> fShaders;
69 SkTArray<SkString> fErrors;
70};
71
72static CapturingShaderErrorHandler gShaderErrorHandler;
73
jvanverth34524262016-05-04 13:49:13 -070074using namespace sk_app;
75
csmartdalton61cd31a2017-02-27 17:00:53 -070076static std::map<GpuPathRenderers, std::string> gPathRendererNames;
77
jvanverth9f372462016-04-06 06:08:59 -070078Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070079 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070080}
81
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060082static DEFINE_string(slide, "", "Start on this sample.");
83static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050084
Stephen Whitea800ec92019-08-02 15:04:52 -040085#if defined(SK_VULKAN)
jvanverthb8794cc2016-07-27 14:29:18 -070086# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050087#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
88# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
Stephen Whitea800ec92019-08-02 15:04:52 -040089#elif defined(SK_DAWN)
90# define BACKENDS_STR "\"sw\", \"gl\", and \"dawn\""
bsalomon6c471f72016-07-26 12:56:32 -070091#else
92# define BACKENDS_STR "\"sw\" and \"gl\""
93#endif
94
Brian Osman2dd96932016-10-18 15:33:53 -040095static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070096
Mike Klein5b3f3432019-03-21 11:42:21 -050097static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070098
Chris Dalton1e6c5b82019-06-17 14:16:49 -060099static DEFINE_int(internalSamples, 4,
100 "Number of samples for internal draws that use MSAA or mixed samples.");
101
Mike Klein84836b72019-03-21 11:31:36 -0500102static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700103
Mike Klein84836b72019-03-21 11:31:36 -0500104static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400105
Mike Kleinc6142d82019-03-25 10:54:59 -0500106static DEFINE_string2(match, m, nullptr,
107 "[~][^]substring[$] [...] of name to run.\n"
108 "Multiple matches may be separated by spaces.\n"
109 "~ causes a matching name to always be skipped\n"
110 "^ requires the start of the name to match\n"
111 "$ requires the end of the name to match\n"
112 "^ and $ requires an exact match\n"
113 "If a name does not match any list entry,\n"
114 "it is skipped unless some list entry starts with ~");
115
Mike Klein19fb3972019-03-21 13:08:08 -0500116#if defined(SK_BUILD_FOR_ANDROID)
117 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500118 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
119 static DEFINE_string(lotties, "/data/local/tmp/lotties",
120 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500121#else
122 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500123 static DEFINE_string(skps, "skps", "Directory to read skps from.");
124 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500125#endif
126
Mike Kleinc6142d82019-03-25 10:54:59 -0500127static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
128
129static DEFINE_int_2(threads, j, -1,
130 "Run threadsafe tests on a threadpool with this many extra threads, "
131 "defaulting to one extra thread per core.");
132
Jim Van Verth7b558182019-11-14 16:47:01 -0500133static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
134
Mike Kleinc6142d82019-03-25 10:54:59 -0500135
Brian Salomon194db172017-08-17 14:37:06 -0400136const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700137 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400138#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
139 "ANGLE",
140#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400141#ifdef SK_DAWN
142 "Dawn",
143#endif
jvanverth063ece72016-06-17 09:29:14 -0700144#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700145 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700146#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400147#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500148 "Metal",
149#endif
csmartdalton578f0642017-02-24 16:04:47 -0700150 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700151};
152
bsalomon6c471f72016-07-26 12:56:32 -0700153static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400154#ifdef SK_DAWN
155 if (0 == strcmp(str, "dawn")) {
156 return sk_app::Window::kDawn_BackendType;
157 } else
158#endif
bsalomon6c471f72016-07-26 12:56:32 -0700159#ifdef SK_VULKAN
160 if (0 == strcmp(str, "vk")) {
161 return sk_app::Window::kVulkan_BackendType;
162 } else
163#endif
Brian Salomon194db172017-08-17 14:37:06 -0400164#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
165 if (0 == strcmp(str, "angle")) {
166 return sk_app::Window::kANGLE_BackendType;
167 } else
168#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400169#ifdef SK_METAL
170 if (0 == strcmp(str, "mtl")) {
171 return sk_app::Window::kMetal_BackendType;
172 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500173#endif
bsalomon6c471f72016-07-26 12:56:32 -0700174 if (0 == strcmp(str, "gl")) {
175 return sk_app::Window::kNativeGL_BackendType;
176 } else if (0 == strcmp(str, "sw")) {
177 return sk_app::Window::kRaster_BackendType;
178 } else {
179 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
180 return sk_app::Window::kRaster_BackendType;
181 }
182}
183
Brian Osmana109e392017-02-24 09:49:14 -0500184static SkColorSpacePrimaries gSrgbPrimaries = {
185 0.64f, 0.33f,
186 0.30f, 0.60f,
187 0.15f, 0.06f,
188 0.3127f, 0.3290f };
189
190static SkColorSpacePrimaries gAdobePrimaries = {
191 0.64f, 0.33f,
192 0.21f, 0.71f,
193 0.15f, 0.06f,
194 0.3127f, 0.3290f };
195
196static SkColorSpacePrimaries gP3Primaries = {
197 0.680f, 0.320f,
198 0.265f, 0.690f,
199 0.150f, 0.060f,
200 0.3127f, 0.3290f };
201
202static SkColorSpacePrimaries gRec2020Primaries = {
203 0.708f, 0.292f,
204 0.170f, 0.797f,
205 0.131f, 0.046f,
206 0.3127f, 0.3290f };
207
208struct NamedPrimaries {
209 const char* fName;
210 SkColorSpacePrimaries* fPrimaries;
211} gNamedPrimaries[] = {
212 { "sRGB", &gSrgbPrimaries },
213 { "AdobeRGB", &gAdobePrimaries },
214 { "P3", &gP3Primaries },
215 { "Rec. 2020", &gRec2020Primaries },
216};
217
218static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
219 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
220}
221
Brian Osman70d2f432017-11-08 09:54:10 -0500222static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
223 // In raster mode, we still use GL for the window.
224 // This lets us render the GUI faster (and correct).
225 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
226}
227
Jim Van Verth74826c82019-03-01 14:37:30 -0500228class NullSlide : public Slide {
229 SkISize getDimensions() const override {
230 return SkISize::Make(640, 480);
231 }
232
233 void draw(SkCanvas* canvas) override {
234 canvas->clear(0xffff11ff);
235 }
236};
237
liyuqiane5a6cd92016-05-27 08:52:52 -0700238const char* kName = "name";
239const char* kValue = "value";
240const char* kOptions = "options";
241const char* kSlideStateName = "Slide";
242const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700243const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700244const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700245const char* kSoftkeyStateName = "Softkey";
246const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700247const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700248const char* kON = "ON";
249const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700250const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700251
jvanverth34524262016-05-04 13:49:13 -0700252Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500253 : fCurrentSlide(-1)
254 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500255 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400256 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500257 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500258 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500259 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500260 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400261 , fZoomWindowFixed(false)
262 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500263 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400264 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700265 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500266 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500267 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500268 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500269 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700270 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400271 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400272 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400273 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500274 , fTiled(false)
275 , fDrawTileBoundaries(false)
276 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400277 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700278{
Greg Daniel285db442016-10-14 09:12:53 -0400279 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700280
Brian Osmanf09e35e2017-12-15 14:48:09 -0500281 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500282 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500283 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600284 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500285 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
286 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700287
jvanverth2bb3b6d2016-04-08 07:24:09 -0700288 SkDebugf("Command line arguments: ");
289 for (int i = 1; i < argc; ++i) {
290 SkDebugf("%s ", argv[i]);
291 }
292 SkDebugf("\n");
293
Mike Klein88544fb2019-03-20 10:50:33 -0500294 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500295#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400296 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500297#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700298
Mike Klein19cc0f62019-03-22 15:30:07 -0500299 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500300
Brian Osmanbc8150f2017-07-24 11:38:01 -0400301 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400302 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400303
bsalomon6c471f72016-07-26 12:56:32 -0700304 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700305 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700306
csmartdalton578f0642017-02-24 16:04:47 -0700307 DisplayParams displayParams;
308 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700309 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400310 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400311 displayParams.fGrContextOptions.fShaderCacheStrategy =
312 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400313 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
314 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltona1638a52019-06-24 11:54:24 -0600315 displayParams.fGrContextOptions.fInternalMultisampleCount = FLAGS_internalSamples;
csmartdalton578f0642017-02-24 16:04:47 -0700316 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500317 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700318
Brian Osman56a24812017-12-19 11:15:16 -0500319 // Configure timers
320 fStatsLayer.setActive(false);
321 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
322 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
323 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
324
jvanverth9f372462016-04-06 06:08:59 -0700325 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700326 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500327 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500328 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500329 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700330
brianosman622c8d52016-05-10 06:50:49 -0700331 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500332 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
333 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
334 fWindow->inval();
335 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500336 // Command to jump directly to the slide picker and give it focus
337 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
338 this->fShowImGuiDebugWindow = true;
339 this->fShowSlidePicker = true;
340 fWindow->inval();
341 });
342 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400343 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500344 this->fShowImGuiDebugWindow = true;
345 this->fShowSlidePicker = true;
346 fWindow->inval();
347 });
Brian Osman79086b92017-02-10 13:36:16 -0500348 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
349 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
350 fWindow->inval();
351 });
Brian Osmanf6877092017-02-13 09:39:57 -0500352 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
353 this->fShowZoomWindow = !this->fShowZoomWindow;
354 fWindow->inval();
355 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400356 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
357 this->fZoomWindowFixed = !this->fZoomWindowFixed;
358 fWindow->inval();
359 });
Greg Danield0794cc2019-03-27 16:23:26 -0400360 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
361 DisplayParams params = fWindow->getRequestedDisplayParams();
362 params.fDisableVsync = !params.fDisableVsync;
363 fWindow->setRequestedDisplayParams(params);
364 this->updateTitle();
365 fWindow->inval();
366 });
Mike Reedf702ed42019-07-22 17:00:49 -0400367 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
368 fRefresh = !fRefresh;
369 fWindow->inval();
370 });
brianosman622c8d52016-05-10 06:50:49 -0700371 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500372 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700373 fWindow->inval();
374 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400375 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500376 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400377 this->updateTitle();
378 fWindow->inval();
379 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500380 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500381 switch (fColorMode) {
382 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500383 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500384 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500385 case ColorMode::kColorManaged8888:
386 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500387 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500388 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400389 this->setColorMode(ColorMode::kColorManagedF16Norm);
390 break;
391 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500392 this->setColorMode(ColorMode::kLegacy);
393 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500394 }
brianosman622c8d52016-05-10 06:50:49 -0700395 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700396 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
397 DisplayParams params = fWindow->getRequestedDisplayParams();
398 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
399 fWindow->setRequestedDisplayParams(params);
400 fWindow->inval();
401 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400402 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500403 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700404 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400405 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500406 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700407 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400408 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700409 this->changeZoomLevel(1.f / 32.f);
410 fWindow->inval();
411 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400412 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700413 this->changeZoomLevel(-1.f / 32.f);
414 fWindow->inval();
415 });
jvanverthaf236b52016-05-20 06:01:06 -0700416 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400417 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
418 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500419 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400420#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
421 if (newBackend == sk_app::Window::kVulkan_BackendType) {
422 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
423 sk_app::Window::kBackendTypeCount);
424 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
425 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500426 }
427#endif
Brian Osman621491e2017-02-28 15:45:01 -0500428 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700429 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500430 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
431 fSaveToSKP = true;
432 fWindow->inval();
433 });
Mike Reed376d8122019-03-14 11:39:02 -0400434 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
435 fShowSlideDimensions = !fShowSlideDimensions;
436 fWindow->inval();
437 });
Ben Wagner37c54032018-04-13 14:30:23 -0400438 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
439 DisplayParams params = fWindow->getRequestedDisplayParams();
440 uint32_t flags = params.fSurfaceProps.flags();
441 if (!fPixelGeometryOverrides) {
442 fPixelGeometryOverrides = true;
443 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
444 } else {
445 switch (params.fSurfaceProps.pixelGeometry()) {
446 case kUnknown_SkPixelGeometry:
447 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
448 break;
449 case kRGB_H_SkPixelGeometry:
450 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
451 break;
452 case kBGR_H_SkPixelGeometry:
453 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
454 break;
455 case kRGB_V_SkPixelGeometry:
456 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
457 break;
458 case kBGR_V_SkPixelGeometry:
459 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
460 fPixelGeometryOverrides = false;
461 break;
462 }
463 }
464 fWindow->setRequestedDisplayParams(params);
465 this->updateTitle();
466 fWindow->inval();
467 });
Ben Wagner9613e452019-01-23 10:34:59 -0500468 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500469 if (!fFontOverrides.fHinting) {
470 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400471 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500472 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500473 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400474 case SkFontHinting::kNone:
475 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500476 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400477 case SkFontHinting::kSlight:
478 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500479 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400480 case SkFontHinting::kNormal:
481 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500482 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400483 case SkFontHinting::kFull:
484 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500485 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500486 break;
487 }
488 }
489 this->updateTitle();
490 fWindow->inval();
491 });
492 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500493 if (!fPaintOverrides.fAntiAlias) {
494 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
495 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500496 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500497 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500498 } else {
499 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500500 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500501 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500502 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400503 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500504 break;
505 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500506 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500507 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400508 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500509 break;
510 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500511 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400512 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500513 break;
514 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500515 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
516 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500517 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
518 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500519 break;
520 }
521 }
522 this->updateTitle();
523 fWindow->inval();
524 });
Ben Wagner37c54032018-04-13 14:30:23 -0400525 fCommands.addCommand('D', "Modes", "DFT", [this]() {
526 DisplayParams params = fWindow->getRequestedDisplayParams();
527 uint32_t flags = params.fSurfaceProps.flags();
528 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
529 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
530 fWindow->setRequestedDisplayParams(params);
531 this->updateTitle();
532 fWindow->inval();
533 });
Ben Wagner9613e452019-01-23 10:34:59 -0500534 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500535 if (!fFontOverrides.fEdging) {
536 fFontOverrides.fEdging = true;
537 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500538 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500539 switch (fFont.getEdging()) {
540 case SkFont::Edging::kAlias:
541 fFont.setEdging(SkFont::Edging::kAntiAlias);
542 break;
543 case SkFont::Edging::kAntiAlias:
544 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
545 break;
546 case SkFont::Edging::kSubpixelAntiAlias:
547 fFont.setEdging(SkFont::Edging::kAlias);
548 fFontOverrides.fEdging = false;
549 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500550 }
551 }
552 this->updateTitle();
553 fWindow->inval();
554 });
Ben Wagner9613e452019-01-23 10:34:59 -0500555 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500556 if (!fFontOverrides.fSubpixel) {
557 fFontOverrides.fSubpixel = true;
558 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500559 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500560 if (!fFont.isSubpixel()) {
561 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500562 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500563 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500564 }
565 }
566 this->updateTitle();
567 fWindow->inval();
568 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400569 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
570 if (!fFontOverrides.fBaselineSnap) {
571 fFontOverrides.fBaselineSnap = true;
572 fFont.setBaselineSnap(false);
573 } else {
574 if (!fFont.isBaselineSnap()) {
575 fFont.setBaselineSnap(true);
576 } else {
577 fFontOverrides.fBaselineSnap = false;
578 }
579 }
580 this->updateTitle();
581 fWindow->inval();
582 });
Brian Osman805a7272018-05-02 15:40:20 -0400583 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
584 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
585 : kPerspective_Real;
586 this->updateTitle();
587 fWindow->inval();
588 });
589 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
590 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
591 : kPerspective_Off;
592 this->updateTitle();
593 fWindow->inval();
594 });
Brian Osman207d4102019-01-10 09:40:58 -0500595 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
596 fAnimTimer.togglePauseResume();
597 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400598 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
599 fZoomUI = !fZoomUI;
600 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
601 fWindow->inval();
602 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500603
jvanverth2bb3b6d2016-04-08 07:24:09 -0700604 // set up slides
605 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500606 if (FLAGS_list) {
607 this->listNames();
608 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700609
Brian Osman9bb47cf2018-04-26 15:55:00 -0400610 fPerspectivePoints[0].set(0, 0);
611 fPerspectivePoints[1].set(1, 0);
612 fPerspectivePoints[2].set(0, 1);
613 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700614 fAnimTimer.run();
615
Hal Canaryc465d132017-12-08 10:21:31 -0500616 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500617 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400618 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500619 }
620 fImGuiGamutPaint.setColor(SK_ColorWHITE);
621 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
622
jongdeok.kim804f17e2019-02-26 14:39:23 +0900623 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500624 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700625}
626
jvanverth34524262016-05-04 13:49:13 -0700627void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400628 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
629 static const struct {
630 const char* fExtension;
631 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500632 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400633 const SlideFactory fFactory;
634 } gExternalSlidesInfo[] = {
635 { ".skp", "skp-dir", FLAGS_skps,
636 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
637 return sk_make_sp<SKPSlide>(name, path);}
638 },
639 { ".jpg", "jpg-dir", FLAGS_jpgs,
640 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
641 return sk_make_sp<ImageSlide>(name, path);}
642 },
Florin Malita87ccf332018-05-04 12:23:24 -0400643#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400644 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400645 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
646 return sk_make_sp<SkottieSlide>(name, path);}
647 },
Florin Malita87ccf332018-05-04 12:23:24 -0400648#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400649#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400650 { ".svg", "svg-dir", FLAGS_svgs,
651 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
652 return sk_make_sp<SvgSlide>(name, path);}
653 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400654#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400655 };
jvanverthc265a922016-04-08 12:51:45 -0700656
Brian Salomon343553a2018-09-05 15:41:23 -0400657 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700658
Mike Klein88544fb2019-03-20 10:50:33 -0500659 const auto addSlide =
660 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
661 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
662 return;
663 }
liyuqian6f163d22016-06-13 12:26:45 -0700664
Mike Klein88544fb2019-03-20 10:50:33 -0500665 if (auto slide = fact(name, path)) {
666 dirSlides.push_back(slide);
667 fSlides.push_back(std::move(slide));
668 }
669 };
Florin Malita76a076b2018-02-15 18:40:48 -0500670
Florin Malita38792ce2018-05-08 10:36:18 -0400671 if (!FLAGS_file.isEmpty()) {
672 // single file mode
673 const SkString file(FLAGS_file[0]);
674
675 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
676 for (const auto& sinfo : gExternalSlidesInfo) {
677 if (file.endsWith(sinfo.fExtension)) {
678 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
679 return;
680 }
681 }
682
683 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
684 } else {
685 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
686 }
687
688 return;
689 }
690
691 // Bisect slide.
692 if (!FLAGS_bisect.isEmpty()) {
693 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500694 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400695 if (FLAGS_bisect.count() >= 2) {
696 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
697 bisect->onChar(*ch);
698 }
699 }
700 fSlides.push_back(std::move(bisect));
701 }
702 }
703
704 // GMs
705 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400706 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400707 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500708 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400709 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400710 fSlides.push_back(std::move(slide));
711 }
Florin Malita38792ce2018-05-08 10:36:18 -0400712 }
713 // reverse gms
714 int numGMs = fSlides.count() - firstGM;
715 for (int i = 0; i < numGMs/2; ++i) {
716 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
717 }
718
719 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400720 for (const SampleFactory factory : SampleRegistry::Range()) {
721 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500722 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400723 fSlides.push_back(slide);
724 }
Florin Malita38792ce2018-05-08 10:36:18 -0400725 }
726
Brian Osman7c979f52019-02-12 13:27:51 -0500727 // Particle demo
728 {
729 // TODO: Convert this to a sample
730 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500731 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500732 fSlides.push_back(std::move(slide));
733 }
734 }
735
Brian Osmand927bd22019-12-18 11:23:12 -0500736 // Runtime shader editor
737 {
738 sk_sp<Slide> slide(new SkSLSlide());
739 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
740 fSlides.push_back(std::move(slide));
741 }
742 }
743
Florin Malita0ffa3222018-04-05 14:34:45 -0400744 for (const auto& info : gExternalSlidesInfo) {
745 for (const auto& flag : info.fFlags) {
746 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
747 // single file
748 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
749 } else {
750 // directory
751 SkOSFile::Iter it(flag.c_str(), info.fExtension);
752 SkString name;
753 while (it.next(&name)) {
754 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
755 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400756 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400757 if (!dirSlides.empty()) {
758 fSlides.push_back(
759 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
760 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500761 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400762 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400763 }
764 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500765
766 if (!fSlides.count()) {
767 sk_sp<Slide> slide(new NullSlide());
768 fSlides.push_back(std::move(slide));
769 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700770}
771
772
jvanverth34524262016-05-04 13:49:13 -0700773Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700774 fWindow->detach();
775 delete fWindow;
776}
777
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500778struct SkPaintTitleUpdater {
779 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
780 void append(const char* s) {
781 if (fCount == 0) {
782 fTitle->append(" {");
783 } else {
784 fTitle->append(", ");
785 }
786 fTitle->append(s);
787 ++fCount;
788 }
789 void done() {
790 if (fCount > 0) {
791 fTitle->append("}");
792 }
793 }
794 SkString* fTitle;
795 int fCount;
796};
797
brianosman05de2162016-05-06 13:28:57 -0700798void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700799 if (!fWindow) {
800 return;
801 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500802 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700803 return; // Surface hasn't been created yet.
804 }
805
jvanverth34524262016-05-04 13:49:13 -0700806 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700807 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700808
Mike Kleine5acd752019-03-22 09:57:16 -0500809 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400810 if (gSkForceAnalyticAA) {
811 title.append(" <FAAA>");
812 } else {
813 title.append(" <AAA>");
814 }
815 }
816
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500817 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500818 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
819 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400820 const char* on, const char* off)
821 {
Ben Wagner9613e452019-01-23 10:34:59 -0500822 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400823 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500824 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400825 };
826
Ben Wagner9613e452019-01-23 10:34:59 -0500827 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
828 const char* on, const char* off)
829 {
830 if (fFontOverrides.*flag) {
831 paintTitle.append((fFont.*isFlag)() ? on : off);
832 }
833 };
834
835 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
836 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500837 if (fPaintOverrides.fFilterQuality) {
838 switch (fPaint.getFilterQuality()) {
839 case kNone_SkFilterQuality:
840 paintTitle.append("NoFilter");
841 break;
842 case kLow_SkFilterQuality:
843 paintTitle.append("LowFilter");
844 break;
845 case kMedium_SkFilterQuality:
846 paintTitle.append("MediumFilter");
847 break;
848 case kHigh_SkFilterQuality:
849 paintTitle.append("HighFilter");
850 break;
851 }
852 }
Ben Wagner9613e452019-01-23 10:34:59 -0500853
854 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
855 "Force Autohint", "No Force Autohint");
856 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400857 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500858 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
859 "Linear Metrics", "Non-Linear Metrics");
860 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
861 "Bitmap Text", "No Bitmap Text");
862 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
863
864 if (fFontOverrides.fEdging) {
865 switch (fFont.getEdging()) {
866 case SkFont::Edging::kAlias:
867 paintTitle.append("Alias Text");
868 break;
869 case SkFont::Edging::kAntiAlias:
870 paintTitle.append("Antialias Text");
871 break;
872 case SkFont::Edging::kSubpixelAntiAlias:
873 paintTitle.append("Subpixel Antialias Text");
874 break;
875 }
876 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400877
Mike Reed3ae47332019-01-04 10:11:46 -0500878 if (fFontOverrides.fHinting) {
879 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400880 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500881 paintTitle.append("No Hinting");
882 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400883 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500884 paintTitle.append("Slight Hinting");
885 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400886 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500887 paintTitle.append("Normal Hinting");
888 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400889 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500890 paintTitle.append("Full Hinting");
891 break;
892 }
893 }
894 paintTitle.done();
895
Brian Osman92004802017-03-06 11:47:26 -0500896 switch (fColorMode) {
897 case ColorMode::kLegacy:
898 title.append(" Legacy 8888");
899 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500900 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500901 title.append(" ColorManaged 8888");
902 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500903 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500904 title.append(" ColorManaged F16");
905 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400906 case ColorMode::kColorManagedF16Norm:
907 title.append(" ColorManaged F16 Norm");
908 break;
Brian Osman92004802017-03-06 11:47:26 -0500909 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500910
Brian Osman92004802017-03-06 11:47:26 -0500911 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500912 int curPrimaries = -1;
913 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
914 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
915 curPrimaries = i;
916 break;
917 }
918 }
Brian Osman03115dc2018-11-26 13:55:19 -0500919 title.appendf(" %s Gamma %f",
920 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500921 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700922 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500923
Ben Wagner37c54032018-04-13 14:30:23 -0400924 const DisplayParams& params = fWindow->getRequestedDisplayParams();
925 if (fPixelGeometryOverrides) {
926 switch (params.fSurfaceProps.pixelGeometry()) {
927 case kUnknown_SkPixelGeometry:
928 title.append( " Flat");
929 break;
930 case kRGB_H_SkPixelGeometry:
931 title.append( " RGB");
932 break;
933 case kBGR_H_SkPixelGeometry:
934 title.append( " BGR");
935 break;
936 case kRGB_V_SkPixelGeometry:
937 title.append( " RGBV");
938 break;
939 case kBGR_V_SkPixelGeometry:
940 title.append( " BGRV");
941 break;
942 }
943 }
944
945 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
946 title.append(" DFT");
947 }
948
csmartdalton578f0642017-02-24 16:04:47 -0700949 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700950 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500951 int msaa = fWindow->sampleCount();
952 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700953 title.appendf(" MSAA: %i", msaa);
954 }
955 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700956
957 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600958 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700959 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
960 }
961
Brian Osman805a7272018-05-02 15:40:20 -0400962 if (kPerspective_Real == fPerspectiveMode) {
963 title.append(" Perpsective (Real)");
964 } else if (kPerspective_Fake == fPerspectiveMode) {
965 title.append(" Perspective (Fake)");
966 }
967
brianosman05de2162016-05-06 13:28:57 -0700968 fWindow->setTitle(title.c_str());
969}
970
Florin Malitaab99c342018-01-16 16:23:03 -0500971int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500972
973 if (!FLAGS_slide.isEmpty()) {
974 int count = fSlides.count();
975 for (int i = 0; i < count; i++) {
976 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500977 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500978 }
979 }
980
981 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
982 this->listNames();
983 }
984
Florin Malitaab99c342018-01-16 16:23:03 -0500985 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500986}
987
Florin Malitaab99c342018-01-16 16:23:03 -0500988void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500989 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500990 for (const auto& slide : fSlides) {
991 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500992 }
993}
994
Florin Malitaab99c342018-01-16 16:23:03 -0500995void Viewer::setCurrentSlide(int slide) {
996 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700997
Florin Malitaab99c342018-01-16 16:23:03 -0500998 if (slide == fCurrentSlide) {
999 return;
1000 }
1001
1002 if (fCurrentSlide >= 0) {
1003 fSlides[fCurrentSlide]->unload();
1004 }
1005
1006 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1007 SkIntToScalar(fWindow->height()));
1008 fCurrentSlide = slide;
1009 this->setupCurrentSlide();
1010}
1011
1012void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001013 if (fCurrentSlide >= 0) {
1014 // prepare dimensions for image slides
1015 fGesture.resetTouchState();
1016 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001017
Jim Van Verth0848fb02018-01-22 13:39:30 -05001018 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1019 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1020 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001021
Jim Van Verth0848fb02018-01-22 13:39:30 -05001022 // Start with a matrix that scales the slide to the available screen space
1023 if (fWindow->scaleContentToFit()) {
1024 if (windowRect.width() > 0 && windowRect.height() > 0) {
1025 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1026 }
liyuqiane46e4f02016-05-20 07:32:19 -07001027 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001028
1029 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001030 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001031
1032 this->updateTitle();
1033 this->updateUIState();
1034
1035 fStatsLayer.resetMeasurements();
1036
1037 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001038 }
jvanverthc265a922016-04-08 12:51:45 -07001039}
1040
1041#define MAX_ZOOM_LEVEL 8
1042#define MIN_ZOOM_LEVEL -8
1043
jvanverth34524262016-05-04 13:49:13 -07001044void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001045 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -04001046 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001047 this->preTouchMatrixChanged();
1048}
Yuqian Li755778c2018-03-28 16:23:31 -04001049
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001050void Viewer::preTouchMatrixChanged() {
1051 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001052 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1053 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1054 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1055 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1056}
1057
Brian Osman805a7272018-05-02 15:40:20 -04001058SkMatrix Viewer::computePerspectiveMatrix() {
1059 SkScalar w = fWindow->width(), h = fWindow->height();
1060 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1061 SkPoint perspPts[4] = {
1062 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1063 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1064 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1065 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1066 };
1067 SkMatrix m;
1068 m.setPolyToPoly(orthoPts, perspPts, 4);
1069 return m;
1070}
1071
Yuqian Li755778c2018-03-28 16:23:31 -04001072SkMatrix Viewer::computePreTouchMatrix() {
1073 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001074
1075 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001076 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001077 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001078
1079 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1080 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001081
Brian Osman805a7272018-05-02 15:40:20 -04001082 if (kPerspective_Real == fPerspectiveMode) {
1083 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001084 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001085 }
1086
Yuqian Li755778c2018-03-28 16:23:31 -04001087 return m;
jvanverthc265a922016-04-08 12:51:45 -07001088}
1089
liyuqiand3cdbca2016-05-17 12:44:20 -07001090SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001091 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001092 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001093 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001094 return m;
jvanverthc265a922016-04-08 12:51:45 -07001095}
1096
Brian Osman621491e2017-02-28 15:45:01 -05001097void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001098 fPersistentCache.reset();
1099 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001100 fBackendType = backendType;
1101
1102 fWindow->detach();
1103
Brian Osman70d2f432017-11-08 09:54:10 -05001104#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001105 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1106 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001107 DisplayParams params = fWindow->getRequestedDisplayParams();
1108 delete fWindow;
1109 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001110
Brian Osman70d2f432017-11-08 09:54:10 -05001111 // re-register callbacks
1112 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001113 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001114 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001115 fWindow->pushLayer(&fImGuiLayer);
1116
Brian Osman70d2f432017-11-08 09:54:10 -05001117 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1118 // will still include our correct sample count. But the re-created fWindow will lose that
1119 // information. On Windows, we need to re-create the window when changing sample count,
1120 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1121 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1122 // as if we had never changed windows at all).
1123 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001124#endif
1125
Brian Osman70d2f432017-11-08 09:54:10 -05001126 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001127}
1128
Brian Osman92004802017-03-06 11:47:26 -05001129void Viewer::setColorMode(ColorMode colorMode) {
1130 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001131 this->updateTitle();
1132 fWindow->inval();
1133}
1134
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001135class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1136public:
Mike Reed3ae47332019-01-04 10:11:46 -05001137 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1138 SkFont* font, Viewer::SkFontFields* ffields)
1139 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001140 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001141 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1142 sk_sp<SkTextBlob>* cache) {
1143 bool blobWillChange = false;
1144 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001145 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1146 bool shouldDraw = this->filterFont(&filteredFont);
1147 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001148 blobWillChange = true;
1149 break;
1150 }
1151 }
1152 if (!blobWillChange) {
1153 return blob;
1154 }
1155
1156 SkTextBlobBuilder builder;
1157 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001158 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1159 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001160 if (!shouldDraw) {
1161 continue;
1162 }
1163
Mike Reed3ae47332019-01-04 10:11:46 -05001164 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001165
Ben Wagner41e40472018-09-24 13:01:54 -04001166 const SkTextBlobBuilder::RunBuffer& runBuffer
1167 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001168 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001169 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001170 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001171 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001172 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001173 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001174 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001175 it.glyphCount(), it.textSize(), SkString())
1176 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1177 uint32_t glyphCount = it.glyphCount();
1178 if (it.glyphs()) {
1179 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1180 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1181 }
1182 if (it.pos()) {
1183 size_t posSize = sizeof(decltype(*it.pos()));
1184 uint8_t positioning = it.positioning();
1185 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1186 }
1187 if (it.text()) {
1188 size_t textSize = sizeof(decltype(*it.text()));
1189 uint32_t textCount = it.textSize();
1190 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1191 }
1192 if (it.clusters()) {
1193 size_t clusterSize = sizeof(decltype(*it.clusters()));
1194 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1195 }
1196 }
1197 *cache = builder.make();
1198 return cache->get();
1199 }
1200 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1201 const SkPaint& paint) override {
1202 sk_sp<SkTextBlob> cache;
1203 this->SkPaintFilterCanvas::onDrawTextBlob(
1204 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1205 }
Mike Reed3ae47332019-01-04 10:11:46 -05001206 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001207 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001208 font->writable()->setSize(fFont->getSize());
1209 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001210 if (fFontOverrides->fScaleX) {
1211 font->writable()->setScaleX(fFont->getScaleX());
1212 }
1213 if (fFontOverrides->fSkewX) {
1214 font->writable()->setSkewX(fFont->getSkewX());
1215 }
Mike Reed3ae47332019-01-04 10:11:46 -05001216 if (fFontOverrides->fHinting) {
1217 font->writable()->setHinting(fFont->getHinting());
1218 }
Ben Wagner9613e452019-01-23 10:34:59 -05001219 if (fFontOverrides->fEdging) {
1220 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001221 }
Ben Wagner9613e452019-01-23 10:34:59 -05001222 if (fFontOverrides->fEmbolden) {
1223 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001224 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001225 if (fFontOverrides->fBaselineSnap) {
1226 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1227 }
Ben Wagner9613e452019-01-23 10:34:59 -05001228 if (fFontOverrides->fLinearMetrics) {
1229 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001230 }
Ben Wagner9613e452019-01-23 10:34:59 -05001231 if (fFontOverrides->fSubpixel) {
1232 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001233 }
Ben Wagner9613e452019-01-23 10:34:59 -05001234 if (fFontOverrides->fEmbeddedBitmaps) {
1235 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001236 }
Ben Wagner9613e452019-01-23 10:34:59 -05001237 if (fFontOverrides->fForceAutoHinting) {
1238 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001239 }
Ben Wagner9613e452019-01-23 10:34:59 -05001240
Mike Reed3ae47332019-01-04 10:11:46 -05001241 return true;
1242 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001243 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001244 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001245 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001246 }
Ben Wagner9613e452019-01-23 10:34:59 -05001247 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001248 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001249 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001250 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001251 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001252 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001253 return true;
1254 }
1255 SkPaint* fPaint;
1256 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001257 SkFont* fFont;
1258 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001259};
1260
Robert Phillips9882dae2019-03-04 11:00:10 -05001261void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001262 if (fCurrentSlide < 0) {
1263 return;
1264 }
1265
Robert Phillips9882dae2019-03-04 11:00:10 -05001266 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001267
Brian Osmanf750fbc2017-02-08 10:47:28 -05001268 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001269 SkSurface* slideSurface = surface;
1270 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001271 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001272
Brian Osmane0d4fba2017-03-15 10:24:55 -04001273 // 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 -05001274 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001275 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001276 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001277 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001278 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001279 }
1280
Brian Osman3ac99cf2017-12-01 11:23:53 -05001281 if (fSaveToSKP) {
1282 SkPictureRecorder recorder;
1283 SkCanvas* recorderCanvas = recorder.beginRecording(
1284 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001285 fSlides[fCurrentSlide]->draw(recorderCanvas);
1286 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1287 SkFILEWStream stream("sample_app.skp");
1288 picture->serialize(&stream);
1289 fSaveToSKP = false;
1290 }
1291
Brian Osmane9ed0f02018-11-26 14:50:05 -05001292 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001293 SkColorType colorType;
1294 switch (fColorMode) {
1295 case ColorMode::kLegacy:
1296 case ColorMode::kColorManaged8888:
1297 colorType = kN32_SkColorType;
1298 break;
1299 case ColorMode::kColorManagedF16:
1300 colorType = kRGBA_F16_SkColorType;
1301 break;
1302 case ColorMode::kColorManagedF16Norm:
1303 colorType = kRGBA_F16Norm_SkColorType;
1304 break;
1305 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001306
1307 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001308 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1309 slideCanvas->getProps(&props);
1310
Brian Osmane9ed0f02018-11-26 14:50:05 -05001311 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1312 return Window::kRaster_BackendType == this->fBackendType
1313 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001314 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001315 };
1316
Brian Osman03115dc2018-11-26 13:55:19 -05001317 // We need to render offscreen if we're...
1318 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1319 // ... in any raster mode, because the window surface is actually GL
1320 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001321 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001322 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001323 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001324 Window::kRaster_BackendType == fBackendType ||
1325 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001326
Brian Osmane9ed0f02018-11-26 14:50:05 -05001327 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001328 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001329 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001330 }
1331
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001332 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001333 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001334 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001335 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001336 if (fTiled) {
1337 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1338 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1339 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1340 SkCanvas* tileCanvas = tileSurface->getCanvas();
1341 SkMatrix m = this->computeMatrix();
1342 for (int y = 0; y < fWindow->height(); y += tileH) {
1343 for (int x = 0; x < fWindow->width(); x += tileW) {
1344 SkAutoCanvasRestore acr(tileCanvas, true);
1345 tileCanvas->translate(-x, -y);
1346 tileCanvas->clear(SK_ColorTRANSPARENT);
1347 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001348 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1349 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001350 fSlides[fCurrentSlide]->draw(&filterCanvas);
1351 tileSurface->draw(slideCanvas, x, y, nullptr);
1352 }
1353 }
1354
1355 // Draw borders between tiles
1356 if (fDrawTileBoundaries) {
1357 SkPaint border;
1358 border.setColor(0x60FF00FF);
1359 border.setStyle(SkPaint::kStroke_Style);
1360 for (int y = 0; y < fWindow->height(); y += tileH) {
1361 for (int x = 0; x < fWindow->width(); x += tileW) {
1362 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1363 }
1364 }
1365 }
1366 } else {
1367 slideCanvas->concat(this->computeMatrix());
1368 if (kPerspective_Real == fPerspectiveMode) {
1369 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1370 }
Mike Reed3ae47332019-01-04 10:11:46 -05001371 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001372 fSlides[fCurrentSlide]->draw(&filterCanvas);
1373 }
Brian Osman56a24812017-12-19 11:15:16 -05001374 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001375 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001376
1377 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001378 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001379 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001380 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001381
1382 // If we rendered offscreen, snap an image and push the results to the window's canvas
1383 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001384 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001385
Robert Phillips9882dae2019-03-04 11:00:10 -05001386 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001387 SkPaint paint;
1388 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001389 int prePerspectiveCount = canvas->save();
1390 if (kPerspective_Fake == fPerspectiveMode) {
1391 paint.setFilterQuality(kHigh_SkFilterQuality);
1392 canvas->clear(SK_ColorWHITE);
1393 canvas->concat(this->computePerspectiveMatrix());
1394 }
Brian Osman03115dc2018-11-26 13:55:19 -05001395 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001396 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001397 }
Mike Reed376d8122019-03-14 11:39:02 -04001398
1399 if (fShowSlideDimensions) {
1400 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1401 SkPaint paint;
1402 paint.setColor(0x40FFFF00);
1403 surface->getCanvas()->drawRect(r, paint);
1404 }
liyuqian6f163d22016-06-13 12:26:45 -07001405}
1406
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001407void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001408 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001409 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001410}
Jim Van Verth6f449692017-02-14 15:16:46 -05001411
Robert Phillips9882dae2019-03-04 11:00:10 -05001412void Viewer::onPaint(SkSurface* surface) {
1413 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001414
Robert Phillips9882dae2019-03-04 11:00:10 -05001415 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001416
Brian Osmand67e5182017-12-08 16:46:09 -05001417 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001418
1419 if (GrContext* ctx = fWindow->getGrContext()) {
1420 // Clean out cache items that haven't been used in more than 10 seconds.
1421 ctx->performDeferredCleanup(std::chrono::seconds(10));
1422 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001423}
1424
Ben Wagnera1915972018-08-09 15:06:19 -04001425void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001426 if (fCurrentSlide >= 0) {
1427 fSlides[fCurrentSlide]->resize(width, height);
1428 }
Ben Wagnera1915972018-08-09 15:06:19 -04001429}
1430
Florin Malitacefc1b92018-02-19 21:43:47 -05001431SkPoint Viewer::mapEvent(float x, float y) {
1432 const auto m = this->computeMatrix();
1433 SkMatrix inv;
1434
1435 SkAssertResult(m.invert(&inv));
1436
1437 return inv.mapXY(x, y);
1438}
1439
Hal Canaryb1f411a2019-08-29 10:39:22 -04001440bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001441 if (GestureDevice::kMouse == fGestureDevice) {
1442 return false;
1443 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001444
1445 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001446 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001447 fWindow->inval();
1448 return true;
1449 }
1450
liyuqiand3cdbca2016-05-17 12:44:20 -07001451 void* castedOwner = reinterpret_cast<void*>(owner);
1452 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001453 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001454 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001455#if defined(SK_BUILD_FOR_IOS)
1456 // TODO: move IOS swipe detection higher up into the platform code
1457 SkPoint dir;
1458 if (fGesture.isFling(&dir)) {
1459 // swiping left or right
1460 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1461 if (dir.fX < 0) {
1462 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1463 fCurrentSlide + 1 : 0);
1464 } else {
1465 this->setCurrentSlide(fCurrentSlide > 0 ?
1466 fCurrentSlide - 1 : fSlides.count() - 1);
1467 }
1468 }
1469 fGesture.reset();
1470 }
1471#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001472 break;
1473 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001474 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001475 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001476 break;
1477 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001478 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001479 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001480 break;
1481 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001482 default: {
1483 // kLeft and kRight are only for swipes
1484 SkASSERT(false);
1485 break;
1486 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001487 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001488 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001489 fWindow->inval();
1490 return true;
1491}
1492
Hal Canaryb1f411a2019-08-29 10:39:22 -04001493bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001494 if (GestureDevice::kTouch == fGestureDevice) {
1495 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001496 }
Brian Osman16c81a12017-12-20 11:58:34 -05001497
Florin Malitacefc1b92018-02-19 21:43:47 -05001498 const auto slidePt = this->mapEvent(x, y);
1499 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1500 fWindow->inval();
1501 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001502 }
1503
1504 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001505 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001506 fGesture.touchEnd(nullptr);
1507 break;
1508 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001509 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001510 fGesture.touchBegin(nullptr, x, y);
1511 break;
1512 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001513 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001514 fGesture.touchMoved(nullptr, x, y);
1515 break;
1516 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001517 default: {
1518 SkASSERT(false); // shouldn't see kRight or kLeft here
1519 break;
1520 }
Brian Osman16c81a12017-12-20 11:58:34 -05001521 }
1522 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1523
Hal Canaryb1f411a2019-08-29 10:39:22 -04001524 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001525 fWindow->inval();
1526 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001527 return true;
1528}
1529
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001530bool Viewer::onFling(skui::InputState state) {
1531 if (skui::InputState::kRight == state) {
1532 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1533 return true;
1534 } else if (skui::InputState::kLeft == state) {
1535 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1536 return true;
1537 }
1538 return false;
1539}
1540
1541bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1542 switch (state) {
1543 case skui::InputState::kDown:
1544 fGesture.startZoom();
1545 return true;
1546 break;
1547 case skui::InputState::kMove:
1548 fGesture.updateZoom(scale, x, y, x, y);
1549 return true;
1550 break;
1551 case skui::InputState::kUp:
1552 fGesture.endZoom();
1553 return true;
1554 break;
1555 default:
1556 SkASSERT(false);
1557 break;
1558 }
1559
1560 return false;
1561}
1562
Brian Osmana109e392017-02-24 09:49:14 -05001563static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001564 // The gamut image covers a (0.8 x 0.9) shaped region
1565 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001566
1567 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1568 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1569 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001570 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1571 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1572 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001573
Brian Osman535c5e32019-02-09 16:32:58 -05001574 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1575 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1576 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1577 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1578 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001579}
1580
Ben Wagner3627d2e2018-06-26 14:23:20 -04001581static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001582 ImGui::DragCanvas dc(pt);
1583 dc.fillColor(IM_COL32(0, 0, 0, 128));
1584 dc.dragPoint(pt);
1585 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001586}
1587
Brian Osman9bb47cf2018-04-26 15:55:00 -04001588static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001589 ImGui::DragCanvas dc(pts);
1590 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001591
Brian Osman535c5e32019-02-09 16:32:58 -05001592 for (int i = 0; i < 4; ++i) {
1593 dc.dragPoint(pts + i);
1594 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001595
Brian Osman535c5e32019-02-09 16:32:58 -05001596 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1597 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1598 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1599 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001600
Brian Osman535c5e32019-02-09 16:32:58 -05001601 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001602}
1603
Brian Osmand67e5182017-12-08 16:46:09 -05001604void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001605 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1606 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001607 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001608 }
1609
1610 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001611 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1612 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001613 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001614 DisplayParams params = fWindow->getRequestedDisplayParams();
1615 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001616 const GrContext* ctx = fWindow->getGrContext();
1617
Brian Osmana109e392017-02-24 09:49:14 -05001618 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1619 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001620 if (ImGui::CollapsingHeader("Backend")) {
1621 int newBackend = static_cast<int>(fBackendType);
1622 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1623 ImGui::SameLine();
1624 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001625#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1626 ImGui::SameLine();
1627 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1628#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001629#if defined(SK_DAWN)
1630 ImGui::SameLine();
1631 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1632#endif
Brian Osman621491e2017-02-28 15:45:01 -05001633#if defined(SK_VULKAN)
1634 ImGui::SameLine();
1635 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1636#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001637#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001638 ImGui::SameLine();
1639 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1640#endif
Brian Osman621491e2017-02-28 15:45:01 -05001641 if (newBackend != fBackendType) {
1642 fDeferredActions.push_back([=]() {
1643 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1644 });
1645 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001646
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001647 bool* wire = &params.fGrContextOptions.fWireframeMode;
1648 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1649 paramsChanged = true;
1650 }
Brian Salomon99a33902017-03-07 15:16:34 -05001651
Brian Osman28b12522017-03-08 17:10:24 -05001652 if (ctx) {
1653 int sampleCount = fWindow->sampleCount();
1654 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001655 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001656 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1657 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1658 ImGui::RadioButton("16", &sampleCount, 16);
1659
1660 if (sampleCount != params.fMSAASampleCount) {
1661 params.fMSAASampleCount = sampleCount;
1662 paramsChanged = true;
1663 }
1664 }
1665
Ben Wagner37c54032018-04-13 14:30:23 -04001666 int pixelGeometryIdx = 0;
1667 if (fPixelGeometryOverrides) {
1668 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1669 }
1670 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1671 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1672 {
1673 uint32_t flags = params.fSurfaceProps.flags();
1674 if (pixelGeometryIdx == 0) {
1675 fPixelGeometryOverrides = false;
1676 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1677 } else {
1678 fPixelGeometryOverrides = true;
1679 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1680 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1681 }
1682 paramsChanged = true;
1683 }
1684
1685 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1686 if (ImGui::Checkbox("DFT", &useDFT)) {
1687 uint32_t flags = params.fSurfaceProps.flags();
1688 if (useDFT) {
1689 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1690 } else {
1691 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1692 }
1693 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1694 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1695 paramsChanged = true;
1696 }
1697
Brian Osman8a9de3d2017-03-01 14:59:05 -05001698 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001699 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001700 auto prButton = [&](GpuPathRenderers x) {
1701 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001702 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1703 params.fGrContextOptions.fGpuPathRenderers = x;
1704 paramsChanged = true;
1705 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001706 }
1707 };
1708
1709 if (!ctx) {
1710 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001711 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001712 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001713 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001714 prButton(GpuPathRenderers::kStencilAndCover);
1715 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001716 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001717 prButton(GpuPathRenderers::kNone);
1718 } else {
1719 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001720 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001721 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001722 prButton(GpuPathRenderers::kCoverageCounting);
1723 }
Jim Van Verth83010462017-03-16 08:45:39 -04001724 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001725 prButton(GpuPathRenderers::kTessellating);
1726 prButton(GpuPathRenderers::kNone);
1727 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001728 ImGui::TreePop();
1729 }
Brian Osman621491e2017-02-28 15:45:01 -05001730 }
1731
Ben Wagner964571d2019-03-08 12:35:06 -05001732 if (ImGui::CollapsingHeader("Tiling")) {
1733 ImGui::Checkbox("Enable", &fTiled);
1734 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1735 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1736 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1737 }
1738
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001739 if (ImGui::CollapsingHeader("Transform")) {
1740 float zoom = fZoomLevel;
1741 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1742 fZoomLevel = zoom;
1743 this->preTouchMatrixChanged();
1744 paramsChanged = true;
1745 }
1746 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001747 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001748 fRotation = deg;
1749 this->preTouchMatrixChanged();
1750 paramsChanged = true;
1751 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001752 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1753 if (ImGui_DragLocation(&fOffset)) {
1754 this->preTouchMatrixChanged();
1755 paramsChanged = true;
1756 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001757 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1758 this->preTouchMatrixChanged();
1759 paramsChanged = true;
1760 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001761 }
Brian Osman805a7272018-05-02 15:40:20 -04001762 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1763 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1764 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001765 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001766 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001767 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001768 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001769 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001770 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001771 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001772 }
1773
Ben Wagnera580fb32018-04-17 11:16:32 -04001774 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001775 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001776 if (fPaintOverrides.fAntiAlias) {
1777 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001778 }
1779 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001780 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001781 {
1782 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1783 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001784 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001785 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1786 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001787 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001788 fPaintOverrides.fAntiAlias = true;
1789 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001790 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001791 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001792 case SkPaintFields::AntiAliasState::Alias:
1793 break;
1794 case SkPaintFields::AntiAliasState::Normal:
1795 break;
1796 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1797 gSkUseAnalyticAA = true;
1798 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001799 break;
1800 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1801 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001802 break;
1803 }
1804 }
1805 paramsChanged = true;
1806 }
1807
Ben Wagner99a78dc2018-05-09 18:23:51 -04001808 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001809 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001810 bool (SkPaint::* isFlag)() const,
1811 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001812 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001813 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001814 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001815 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001816 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001817 if (ImGui::Combo(label, &itemIndex, items)) {
1818 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001819 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001820 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001821 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001822 (fPaint.*setFlag)(itemIndex == 2);
1823 }
1824 paramsChanged = true;
1825 }
1826 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001827
Ben Wagner99a78dc2018-05-09 18:23:51 -04001828 paintFlag("Dither",
1829 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001830 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001831 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001832
1833 int filterQualityIdx = 0;
1834 if (fPaintOverrides.fFilterQuality) {
1835 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1836 }
1837 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1838 "Default\0None\0Low\0Medium\0High\0\0"))
1839 {
1840 if (filterQualityIdx == 0) {
1841 fPaintOverrides.fFilterQuality = false;
1842 fPaint.setFilterQuality(kNone_SkFilterQuality);
1843 } else {
1844 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1845 fPaintOverrides.fFilterQuality = true;
1846 }
1847 paramsChanged = true;
1848 }
Ben Wagner9613e452019-01-23 10:34:59 -05001849 }
Hal Canary02738a82019-01-21 18:51:32 +00001850
Ben Wagner9613e452019-01-23 10:34:59 -05001851 if (ImGui::CollapsingHeader("Font")) {
1852 int hintingIdx = 0;
1853 if (fFontOverrides.fHinting) {
1854 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1855 }
1856 if (ImGui::Combo("Hinting", &hintingIdx,
1857 "Default\0None\0Slight\0Normal\0Full\0\0"))
1858 {
1859 if (hintingIdx == 0) {
1860 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001861 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001862 } else {
1863 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1864 fFontOverrides.fHinting = true;
1865 }
1866 paramsChanged = true;
1867 }
Hal Canary02738a82019-01-21 18:51:32 +00001868
Ben Wagner9613e452019-01-23 10:34:59 -05001869 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1870 bool SkFontFields::* flag,
1871 bool (SkFont::* isFlag)() const,
1872 void (SkFont::* setFlag)(bool) )
1873 {
1874 int itemIndex = 0;
1875 if (fFontOverrides.*flag) {
1876 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1877 }
1878 if (ImGui::Combo(label, &itemIndex, items)) {
1879 if (itemIndex == 0) {
1880 fFontOverrides.*flag = false;
1881 } else {
1882 fFontOverrides.*flag = true;
1883 (fFont.*setFlag)(itemIndex == 2);
1884 }
1885 paramsChanged = true;
1886 }
1887 };
Hal Canary02738a82019-01-21 18:51:32 +00001888
Ben Wagner9613e452019-01-23 10:34:59 -05001889 fontFlag("Fake Bold Glyphs",
1890 "Default\0No Fake Bold\0Fake Bold\0\0",
1891 &SkFontFields::fEmbolden,
1892 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001893
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001894 fontFlag("Baseline Snapping",
1895 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1896 &SkFontFields::fBaselineSnap,
1897 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1898
Ben Wagner9613e452019-01-23 10:34:59 -05001899 fontFlag("Linear Text",
1900 "Default\0No Linear Text\0Linear Text\0\0",
1901 &SkFontFields::fLinearMetrics,
1902 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001903
Ben Wagner9613e452019-01-23 10:34:59 -05001904 fontFlag("Subpixel Position Glyphs",
1905 "Default\0Pixel Text\0Subpixel Text\0\0",
1906 &SkFontFields::fSubpixel,
1907 &SkFont::isSubpixel, &SkFont::setSubpixel);
1908
1909 fontFlag("Embedded Bitmap Text",
1910 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1911 &SkFontFields::fEmbeddedBitmaps,
1912 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1913
1914 fontFlag("Force Auto-Hinting",
1915 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1916 &SkFontFields::fForceAutoHinting,
1917 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1918
1919 int edgingIdx = 0;
1920 if (fFontOverrides.fEdging) {
1921 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1922 }
1923 if (ImGui::Combo("Edging", &edgingIdx,
1924 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1925 {
1926 if (edgingIdx == 0) {
1927 fFontOverrides.fEdging = false;
1928 fFont.setEdging(SkFont::Edging::kAlias);
1929 } else {
1930 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1931 fFontOverrides.fEdging = true;
1932 }
1933 paramsChanged = true;
1934 }
1935
Ben Wagner15a8d572019-03-21 13:35:44 -04001936 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1937 if (fFontOverrides.fSize) {
1938 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001939 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001940 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001941 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001942 fFontOverrides.fSizeRange[0],
1943 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001944 "%.6f", 2.0f))
1945 {
Mike Reed3ae47332019-01-04 10:11:46 -05001946 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001947 paramsChanged = true;
1948 }
1949 }
1950
1951 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1952 if (fFontOverrides.fScaleX) {
1953 float scaleX = fFont.getScaleX();
1954 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1955 fFont.setScaleX(scaleX);
1956 paramsChanged = true;
1957 }
1958 }
1959
1960 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1961 if (fFontOverrides.fSkewX) {
1962 float skewX = fFont.getSkewX();
1963 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1964 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001965 paramsChanged = true;
1966 }
1967 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001968 }
1969
Mike Reed81f60ec2018-05-15 10:09:52 -04001970 {
1971 SkMetaData controls;
1972 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1973 if (ImGui::CollapsingHeader("Current Slide")) {
1974 SkMetaData::Iter iter(controls);
1975 const char* name;
1976 SkMetaData::Type type;
1977 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001978 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001979 if (type == SkMetaData::kScalar_Type) {
1980 float val[3];
1981 SkASSERT(count == 3);
1982 controls.findScalars(name, &count, val);
1983 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1984 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001985 }
Ben Wagner110c7032019-03-22 17:03:59 -04001986 } else if (type == SkMetaData::kBool_Type) {
1987 bool val;
1988 SkASSERT(count == 1);
1989 controls.findBool(name, &val);
1990 if (ImGui::Checkbox(name, &val)) {
1991 controls.setBool(name, val);
1992 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001993 }
1994 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001995 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001996 }
1997 }
1998 }
1999
Ben Wagner7a3c6742018-04-23 10:01:07 -04002000 if (fShowSlidePicker) {
2001 ImGui::SetNextTreeNodeOpen(true);
2002 }
Brian Osman79086b92017-02-10 13:36:16 -05002003 if (ImGui::CollapsingHeader("Slide")) {
2004 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002005 static ImVector<const char*> filteredSlideNames;
2006 static ImVector<int> filteredSlideIndices;
2007
Brian Osmanfce09c52017-11-14 15:32:20 -05002008 if (fShowSlidePicker) {
2009 ImGui::SetKeyboardFocusHere();
2010 fShowSlidePicker = false;
2011 }
2012
Brian Osman79086b92017-02-10 13:36:16 -05002013 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002014 filteredSlideNames.clear();
2015 filteredSlideIndices.clear();
2016 int filteredIndex = 0;
2017 for (int i = 0; i < fSlides.count(); ++i) {
2018 const char* slideName = fSlides[i]->getName().c_str();
2019 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2020 if (i == fCurrentSlide) {
2021 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002022 }
Brian Osmanf479e422017-11-08 13:11:36 -05002023 filteredSlideNames.push_back(slideName);
2024 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002025 }
Brian Osman79086b92017-02-10 13:36:16 -05002026 }
Brian Osmanf479e422017-11-08 13:11:36 -05002027
Brian Osmanf479e422017-11-08 13:11:36 -05002028 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2029 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002030 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002031 }
2032 }
Brian Osmana109e392017-02-24 09:49:14 -05002033
2034 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002035 ColorMode newMode = fColorMode;
2036 auto cmButton = [&](ColorMode mode, const char* label) {
2037 if (ImGui::RadioButton(label, mode == fColorMode)) {
2038 newMode = mode;
2039 }
2040 };
2041
2042 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002043 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2044 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002045 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002046
2047 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002048 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002049 }
2050
2051 // Pick from common gamuts:
2052 int primariesIdx = 4; // Default: Custom
2053 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2054 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2055 primariesIdx = i;
2056 break;
2057 }
2058 }
2059
Brian Osman03115dc2018-11-26 13:55:19 -05002060 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002061 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002062
Brian Osmana109e392017-02-24 09:49:14 -05002063 if (ImGui::Combo("Primaries", &primariesIdx,
2064 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2065 if (primariesIdx >= 0 && primariesIdx <= 3) {
2066 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2067 }
2068 }
2069
2070 // Allow direct editing of gamut
2071 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2072 }
Brian Osman207d4102019-01-10 09:40:58 -05002073
2074 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002075 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002076 if (ImGui::Checkbox("Pause", &isPaused)) {
2077 fAnimTimer.togglePauseResume();
2078 }
Brian Osman707d2022019-01-10 11:27:34 -05002079
2080 float speed = fAnimTimer.getSpeed();
2081 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2082 fAnimTimer.setSpeed(speed);
2083 }
Brian Osman207d4102019-01-10 09:40:58 -05002084 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002085
Brian Osmanfd7657c2019-04-25 11:34:07 -04002086 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2087#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2088 || Window::kANGLE_BackendType == fBackendType
2089#endif
2090 ;
2091
2092 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2093 // than GL, we need to force it on. Just do that on the first frame after the backend
2094 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002095 if (!backendIsGL &&
2096 params.fGrContextOptions.fShaderCacheStrategy !=
2097 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2098 params.fGrContextOptions.fShaderCacheStrategy =
2099 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002100 paramsChanged = true;
2101 fPersistentCache.reset();
2102 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002103 // To re-load shaders from the currently active programs, we flush all caches on one
2104 // frame, then set a flag to poll the cache on the next frame.
2105 static bool gLoadPending = false;
2106 if (gLoadPending) {
2107 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2108 int hitCount) {
2109 CachedGLSL& entry(fCachedGLSL.push_back());
2110 entry.fKey = key;
2111 SkMD5 hash;
2112 hash.write(key->bytes(), key->size());
2113 SkMD5::Digest digest = hash.finish();
2114 for (int i = 0; i < 16; ++i) {
2115 entry.fKeyString.appendf("%02x", digest.data[i]);
2116 }
2117
Brian Osmana66081d2019-09-03 14:59:26 -04002118 SkReader32 reader(data->data(), data->size());
2119 entry.fShaderType = reader.readU32();
2120 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2121 entry.fInputs,
2122 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002123 };
2124 fCachedGLSL.reset();
2125 fPersistentCache.foreach(collectShaders);
2126 gLoadPending = false;
2127 }
2128
2129 // Defer actually doing the load/save logic so that we can trigger a save when we
2130 // start or finish hovering on a tree node in the list below:
2131 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002132 bool doSave = ImGui::Button("Save");
2133 if (backendIsGL) {
2134 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002135 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2136 GrContextOptions::ShaderCacheStrategy::kSkSL;
2137 if (ImGui::Checkbox("SkSL", &sksl)) {
2138 params.fGrContextOptions.fShaderCacheStrategy = sksl
2139 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2140 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002141 paramsChanged = true;
2142 doLoad = true;
2143 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2144 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002145 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002146
2147 ImGui::BeginChild("##ScrollingRegion");
2148 for (auto& entry : fCachedGLSL) {
2149 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2150 bool hovered = ImGui::IsItemHovered();
2151 if (hovered != entry.fHovered) {
2152 // Force a save to patch the highlight shader in/out
2153 entry.fHovered = hovered;
2154 doSave = true;
2155 }
2156 if (inTreeNode) {
2157 // Full width, and a reasonable amount of space for each shader.
2158 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2159 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2160 boxSize);
2161 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2162 boxSize);
2163 ImGui::TreePop();
2164 }
2165 }
2166 ImGui::EndChild();
2167
2168 if (doLoad) {
2169 fPersistentCache.reset();
2170 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2171 gLoadPending = true;
2172 }
2173 if (doSave) {
2174 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002175 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002176 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2177 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002178
Brian Osman072e6fc2019-06-12 11:35:41 -04002179 SkSL::String highlight;
2180 if (!sksl) {
2181 highlight = shaderCaps->versionDeclString();
2182 if (shaderCaps->usesPrecisionModifiers()) {
2183 highlight.append("precision mediump float;\n");
2184 }
Brian Osman5bee3902019-05-07 09:55:45 -04002185 }
2186 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002187 highlight.appendf("out %s sk_FragColor;\n"
2188 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2189 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002190
2191 fPersistentCache.reset();
2192 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2193 for (auto& entry : fCachedGLSL) {
2194 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2195 if (entry.fHovered) {
2196 entry.fShader[kFragment_GrShaderType] = highlight;
2197 }
2198
Brian Osmana085a412019-04-25 09:44:43 -04002199 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2200 entry.fShader,
2201 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002202 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002203 fPersistentCache.store(*entry.fKey, *data);
2204
2205 entry.fShader[kFragment_GrShaderType] = backup;
2206 }
2207 }
2208 }
Brian Osman79086b92017-02-10 13:36:16 -05002209 }
Brian Salomon99a33902017-03-07 15:16:34 -05002210 if (paramsChanged) {
2211 fDeferredActions.push_back([=]() {
2212 fWindow->setRequestedDisplayParams(params);
2213 fWindow->inval();
2214 this->updateTitle();
2215 });
2216 }
Brian Osman79086b92017-02-10 13:36:16 -05002217 ImGui::End();
2218 }
2219
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002220 if (gShaderErrorHandler.fErrors.count()) {
2221 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2222 ImGui::Begin("Shader Errors");
2223 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2224 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002225 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2226 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2227 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2228 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002229 }
2230 ImGui::End();
2231 gShaderErrorHandler.reset();
2232 }
2233
Brian Osmanf6877092017-02-13 09:39:57 -05002234 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002235 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2236 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002237 static int zoomFactor = 8;
2238 if (ImGui::Button("<<")) {
2239 zoomFactor = SkTMax(zoomFactor / 2, 4);
2240 }
2241 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2242 if (ImGui::Button(">>")) {
2243 zoomFactor = SkTMin(zoomFactor * 2, 32);
2244 }
Brian Osmanf6877092017-02-13 09:39:57 -05002245
Ben Wagner3627d2e2018-06-26 14:23:20 -04002246 if (!fZoomWindowFixed) {
2247 ImVec2 mousePos = ImGui::GetMousePos();
2248 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2249 }
2250 SkScalar x = fZoomWindowLocation.x();
2251 SkScalar y = fZoomWindowLocation.y();
2252 int xInt = SkScalarRoundToInt(x);
2253 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002254 ImVec2 avail = ImGui::GetContentRegionAvail();
2255
Brian Osmanead517d2017-11-13 15:36:36 -05002256 uint32_t pixel = 0;
2257 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002258 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002259 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002260 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002261 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002262 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002263 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2264 }
2265
Brian Osmand67e5182017-12-08 16:46:09 -05002266 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002267 // Translate so the region of the image that's under the mouse cursor is centered
2268 // in the zoom canvas:
2269 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002270 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2271 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002272 c->drawImage(this->fLastImage, 0, 0);
2273
2274 SkPaint outline;
2275 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002276 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002277 });
Brian Osmanf6877092017-02-13 09:39:57 -05002278 }
2279
2280 ImGui::End();
2281 }
Brian Osman79086b92017-02-10 13:36:16 -05002282}
2283
liyuqian2edb0f42016-07-06 14:11:32 -07002284void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002285 for (int i = 0; i < fDeferredActions.count(); ++i) {
2286 fDeferredActions[i]();
2287 }
2288 fDeferredActions.reset();
2289
Brian Osman56a24812017-12-19 11:15:16 -05002290 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002291 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002292 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002293 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002294
Brian Osman79086b92017-02-10 13:36:16 -05002295 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002296 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2297 // not be visible, though. So we need to redraw if there is at least one visible window, or
2298 // more than one active window. Newly created windows are active but not visible for one frame
2299 // while they determine their layout and sizing.
2300 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2301 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002302 fWindow->inval();
2303 }
jvanverth9f372462016-04-06 06:08:59 -07002304}
liyuqiane5a6cd92016-05-27 08:52:52 -07002305
Florin Malitab632df72018-06-18 21:23:06 -04002306template <typename OptionsFunc>
2307static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2308 OptionsFunc&& optionsFunc) {
2309 writer.beginObject();
2310 {
2311 writer.appendString(kName , name);
2312 writer.appendString(kValue, value);
2313
2314 writer.beginArray(kOptions);
2315 {
2316 optionsFunc(writer);
2317 }
2318 writer.endArray();
2319 }
2320 writer.endObject();
2321}
2322
2323
liyuqiane5a6cd92016-05-27 08:52:52 -07002324void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002325 if (!fWindow) {
2326 return;
2327 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002328 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002329 return; // Surface hasn't been created yet.
2330 }
2331
Florin Malitab632df72018-06-18 21:23:06 -04002332 SkDynamicMemoryWStream memStream;
2333 SkJSONWriter writer(&memStream);
2334 writer.beginArray();
2335
liyuqianb73c24b2016-06-03 08:47:23 -07002336 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002337 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2338 [this](SkJSONWriter& writer) {
2339 for(const auto& slide : fSlides) {
2340 writer.appendString(slide->getName().c_str());
2341 }
2342 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002343
liyuqianb73c24b2016-06-03 08:47:23 -07002344 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002345 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2346 [](SkJSONWriter& writer) {
2347 for (const auto& str : kBackendTypeStrings) {
2348 writer.appendString(str);
2349 }
2350 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002351
csmartdalton578f0642017-02-24 16:04:47 -07002352 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002353 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2354 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2355 [this](SkJSONWriter& writer) {
2356 writer.appendS32(0);
2357
2358 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2359 return;
2360 }
2361
2362 for (int msaa : {4, 8, 16}) {
2363 writer.appendS32(msaa);
2364 }
2365 });
csmartdalton578f0642017-02-24 16:04:47 -07002366
csmartdalton61cd31a2017-02-27 17:00:53 -07002367 // Path renderer state
2368 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002369 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2370 [this](SkJSONWriter& writer) {
2371 const GrContext* ctx = fWindow->getGrContext();
2372 if (!ctx) {
2373 writer.appendString("Software");
2374 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002375 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002376
Florin Malitab632df72018-06-18 21:23:06 -04002377 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2378 if (fWindow->sampleCount() > 1) {
2379 if (caps->shaderCaps()->pathRenderingSupport()) {
2380 writer.appendString(
2381 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2382 }
2383 } else {
2384 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2385 writer.appendString(
2386 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2387 }
2388 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2389 }
2390 writer.appendString(
2391 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2392 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2393 }
2394 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002395
liyuqianb73c24b2016-06-03 08:47:23 -07002396 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002397 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2398 [this](SkJSONWriter& writer) {
2399 writer.appendString(kSoftkeyHint);
2400 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2401 writer.appendString(softkey.c_str());
2402 }
2403 });
liyuqianb73c24b2016-06-03 08:47:23 -07002404
Florin Malitab632df72018-06-18 21:23:06 -04002405 writer.endArray();
2406 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002407
Florin Malitab632df72018-06-18 21:23:06 -04002408 auto data = memStream.detachAsData();
2409
2410 // TODO: would be cool to avoid this copy
2411 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2412
2413 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002414}
2415
2416void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002417 // For those who will add more features to handle the state change in this function:
2418 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2419 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2420 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002421 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002422 for (int i = 0; i < fSlides.count(); ++i) {
2423 if (fSlides[i]->getName().equals(stateValue)) {
2424 this->setCurrentSlide(i);
2425 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002426 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002427 }
Florin Malitaab99c342018-01-16 16:23:03 -05002428
2429 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002430 } else if (stateName.equals(kBackendStateName)) {
2431 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2432 if (stateValue.equals(kBackendTypeStrings[i])) {
2433 if (fBackendType != i) {
2434 fBackendType = (sk_app::Window::BackendType)i;
2435 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002436 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002437 }
2438 break;
2439 }
2440 }
csmartdalton578f0642017-02-24 16:04:47 -07002441 } else if (stateName.equals(kMSAAStateName)) {
2442 DisplayParams params = fWindow->getRequestedDisplayParams();
2443 int sampleCount = atoi(stateValue.c_str());
2444 if (sampleCount != params.fMSAASampleCount) {
2445 params.fMSAASampleCount = sampleCount;
2446 fWindow->setRequestedDisplayParams(params);
2447 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002448 this->updateTitle();
2449 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002450 }
2451 } else if (stateName.equals(kPathRendererStateName)) {
2452 DisplayParams params = fWindow->getRequestedDisplayParams();
2453 for (const auto& pair : gPathRendererNames) {
2454 if (pair.second == stateValue.c_str()) {
2455 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2456 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2457 fWindow->setRequestedDisplayParams(params);
2458 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002459 this->updateTitle();
2460 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002461 }
2462 break;
2463 }
csmartdalton578f0642017-02-24 16:04:47 -07002464 }
liyuqianb73c24b2016-06-03 08:47:23 -07002465 } else if (stateName.equals(kSoftkeyStateName)) {
2466 if (!stateValue.equals(kSoftkeyHint)) {
2467 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002468 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002469 }
liyuqian2edb0f42016-07-06 14:11:32 -07002470 } else if (stateName.equals(kRefreshStateName)) {
2471 // This state is actually NOT in the UI state.
2472 // We use this to allow Android to quickly set bool fRefresh.
2473 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002474 } else {
2475 SkDebugf("Unknown stateName: %s", stateName.c_str());
2476 }
2477}
Brian Osman79086b92017-02-10 13:36:16 -05002478
Hal Canaryb1f411a2019-08-29 10:39:22 -04002479bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002480 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002481}
2482
Hal Canaryb1f411a2019-08-29 10:39:22 -04002483bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002484 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002485 fWindow->inval();
2486 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002487 } else {
2488 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002489 }
Brian Osman79086b92017-02-10 13:36:16 -05002490}