blob: f28337f9aa733c22c084aa3717cfeeb577698ba4 [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"
26#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
27#include "src/utils/SkJSONWriter.h"
28#include "src/utils/SkOSPath.h"
29#include "tools/Resources.h"
30#include "tools/ToolUtils.h"
31#include "tools/flags/CommandLineFlags.h"
32#include "tools/flags/CommonFlags.h"
33#include "tools/trace/EventTracingPriv.h"
34#include "tools/viewer/BisectSlide.h"
35#include "tools/viewer/GMSlide.h"
36#include "tools/viewer/ImageSlide.h"
37#include "tools/viewer/ParticlesSlide.h"
38#include "tools/viewer/SKPSlide.h"
39#include "tools/viewer/SampleSlide.h"
40#include "tools/viewer/SlideDir.h"
41#include "tools/viewer/SvgSlide.h"
42#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070043
Hal Canaryc640d0d2018-06-13 09:59:02 -040044#include <stdlib.h>
45#include <map>
46
Hal Canary8a001442018-09-19 11:31:27 -040047#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040048#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040049
Florin Malita87ccf332018-05-04 12:23:24 -040050#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040052#endif
53
Brian Osman5e7fbfd2019-05-03 13:13:35 -040054class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
55public:
56 void compileError(const char* shader, const char* errors) override {
57 fShaders.push_back(SkString(shader));
58 fErrors.push_back(SkString(errors));
59 }
60
61 void reset() {
62 fShaders.reset();
63 fErrors.reset();
64 }
65
66 SkTArray<SkString> fShaders;
67 SkTArray<SkString> fErrors;
68};
69
70static CapturingShaderErrorHandler gShaderErrorHandler;
71
jvanverth34524262016-05-04 13:49:13 -070072using namespace sk_app;
73
csmartdalton61cd31a2017-02-27 17:00:53 -070074static std::map<GpuPathRenderers, std::string> gPathRendererNames;
75
jvanverth9f372462016-04-06 06:08:59 -070076Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070077 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070078}
79
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060080static DEFINE_string(slide, "", "Start on this sample.");
81static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050082
Stephen Whitea800ec92019-08-02 15:04:52 -040083#if defined(SK_VULKAN)
jvanverthb8794cc2016-07-27 14:29:18 -070084# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050085#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
86# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
Stephen Whitea800ec92019-08-02 15:04:52 -040087#elif defined(SK_DAWN)
88# define BACKENDS_STR "\"sw\", \"gl\", and \"dawn\""
bsalomon6c471f72016-07-26 12:56:32 -070089#else
90# define BACKENDS_STR "\"sw\" and \"gl\""
91#endif
92
Brian Osman2dd96932016-10-18 15:33:53 -040093static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070094
Mike Klein5b3f3432019-03-21 11:42:21 -050095static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070096
Chris Dalton1e6c5b82019-06-17 14:16:49 -060097static DEFINE_int(internalSamples, 4,
98 "Number of samples for internal draws that use MSAA or mixed samples.");
99
Mike Klein84836b72019-03-21 11:31:36 -0500100static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700101
Mike Klein84836b72019-03-21 11:31:36 -0500102static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400103
Mike Kleinc6142d82019-03-25 10:54:59 -0500104static DEFINE_string2(match, m, nullptr,
105 "[~][^]substring[$] [...] of name to run.\n"
106 "Multiple matches may be separated by spaces.\n"
107 "~ causes a matching name to always be skipped\n"
108 "^ requires the start of the name to match\n"
109 "$ requires the end of the name to match\n"
110 "^ and $ requires an exact match\n"
111 "If a name does not match any list entry,\n"
112 "it is skipped unless some list entry starts with ~");
113
Mike Klein19fb3972019-03-21 13:08:08 -0500114#if defined(SK_BUILD_FOR_ANDROID)
115 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500116 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
117 static DEFINE_string(lotties, "/data/local/tmp/lotties",
118 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500119#else
120 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500121 static DEFINE_string(skps, "skps", "Directory to read skps from.");
122 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500123#endif
124
Mike Kleinc6142d82019-03-25 10:54:59 -0500125static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
126
127static DEFINE_int_2(threads, j, -1,
128 "Run threadsafe tests on a threadpool with this many extra threads, "
129 "defaulting to one extra thread per core.");
130
Jim Van Verth7b558182019-11-14 16:47:01 -0500131static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
132
Mike Kleinc6142d82019-03-25 10:54:59 -0500133
Brian Salomon194db172017-08-17 14:37:06 -0400134const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700135 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400136#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
137 "ANGLE",
138#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400139#ifdef SK_DAWN
140 "Dawn",
141#endif
jvanverth063ece72016-06-17 09:29:14 -0700142#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700143 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700144#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400145#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500146 "Metal",
147#endif
csmartdalton578f0642017-02-24 16:04:47 -0700148 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700149};
150
bsalomon6c471f72016-07-26 12:56:32 -0700151static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400152#ifdef SK_DAWN
153 if (0 == strcmp(str, "dawn")) {
154 return sk_app::Window::kDawn_BackendType;
155 } else
156#endif
bsalomon6c471f72016-07-26 12:56:32 -0700157#ifdef SK_VULKAN
158 if (0 == strcmp(str, "vk")) {
159 return sk_app::Window::kVulkan_BackendType;
160 } else
161#endif
Brian Salomon194db172017-08-17 14:37:06 -0400162#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
163 if (0 == strcmp(str, "angle")) {
164 return sk_app::Window::kANGLE_BackendType;
165 } else
166#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400167#ifdef SK_METAL
168 if (0 == strcmp(str, "mtl")) {
169 return sk_app::Window::kMetal_BackendType;
170 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500171#endif
bsalomon6c471f72016-07-26 12:56:32 -0700172 if (0 == strcmp(str, "gl")) {
173 return sk_app::Window::kNativeGL_BackendType;
174 } else if (0 == strcmp(str, "sw")) {
175 return sk_app::Window::kRaster_BackendType;
176 } else {
177 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
178 return sk_app::Window::kRaster_BackendType;
179 }
180}
181
Brian Osmana109e392017-02-24 09:49:14 -0500182static SkColorSpacePrimaries gSrgbPrimaries = {
183 0.64f, 0.33f,
184 0.30f, 0.60f,
185 0.15f, 0.06f,
186 0.3127f, 0.3290f };
187
188static SkColorSpacePrimaries gAdobePrimaries = {
189 0.64f, 0.33f,
190 0.21f, 0.71f,
191 0.15f, 0.06f,
192 0.3127f, 0.3290f };
193
194static SkColorSpacePrimaries gP3Primaries = {
195 0.680f, 0.320f,
196 0.265f, 0.690f,
197 0.150f, 0.060f,
198 0.3127f, 0.3290f };
199
200static SkColorSpacePrimaries gRec2020Primaries = {
201 0.708f, 0.292f,
202 0.170f, 0.797f,
203 0.131f, 0.046f,
204 0.3127f, 0.3290f };
205
206struct NamedPrimaries {
207 const char* fName;
208 SkColorSpacePrimaries* fPrimaries;
209} gNamedPrimaries[] = {
210 { "sRGB", &gSrgbPrimaries },
211 { "AdobeRGB", &gAdobePrimaries },
212 { "P3", &gP3Primaries },
213 { "Rec. 2020", &gRec2020Primaries },
214};
215
216static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
217 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
218}
219
Brian Osman70d2f432017-11-08 09:54:10 -0500220static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
221 // In raster mode, we still use GL for the window.
222 // This lets us render the GUI faster (and correct).
223 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
224}
225
Jim Van Verth74826c82019-03-01 14:37:30 -0500226class NullSlide : public Slide {
227 SkISize getDimensions() const override {
228 return SkISize::Make(640, 480);
229 }
230
231 void draw(SkCanvas* canvas) override {
232 canvas->clear(0xffff11ff);
233 }
234};
235
liyuqiane5a6cd92016-05-27 08:52:52 -0700236const char* kName = "name";
237const char* kValue = "value";
238const char* kOptions = "options";
239const char* kSlideStateName = "Slide";
240const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700241const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700242const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700243const char* kSoftkeyStateName = "Softkey";
244const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700245const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700246const char* kON = "ON";
247const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700248const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700249
jvanverth34524262016-05-04 13:49:13 -0700250Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500251 : fCurrentSlide(-1)
252 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500253 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400254 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500255 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500256 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500257 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500258 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400259 , fZoomWindowFixed(false)
260 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500261 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400262 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700263 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500264 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500265 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500266 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500267 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700268 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400269 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400270 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400271 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500272 , fTiled(false)
273 , fDrawTileBoundaries(false)
274 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400275 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700276{
Greg Daniel285db442016-10-14 09:12:53 -0400277 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700278
Brian Osmanf09e35e2017-12-15 14:48:09 -0500279 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500280 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500281 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600282 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500283 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
284 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700285
jvanverth2bb3b6d2016-04-08 07:24:09 -0700286 SkDebugf("Command line arguments: ");
287 for (int i = 1; i < argc; ++i) {
288 SkDebugf("%s ", argv[i]);
289 }
290 SkDebugf("\n");
291
Mike Klein88544fb2019-03-20 10:50:33 -0500292 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500293#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400294 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500295#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700296
Mike Klein19cc0f62019-03-22 15:30:07 -0500297 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500298
Brian Osmanbc8150f2017-07-24 11:38:01 -0400299 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400300 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400301
bsalomon6c471f72016-07-26 12:56:32 -0700302 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700303 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700304
csmartdalton578f0642017-02-24 16:04:47 -0700305 DisplayParams displayParams;
306 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700307 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400308 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400309 displayParams.fGrContextOptions.fShaderCacheStrategy =
310 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400311 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
312 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltona1638a52019-06-24 11:54:24 -0600313 displayParams.fGrContextOptions.fInternalMultisampleCount = FLAGS_internalSamples;
csmartdalton578f0642017-02-24 16:04:47 -0700314 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500315 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700316
Brian Osman56a24812017-12-19 11:15:16 -0500317 // Configure timers
318 fStatsLayer.setActive(false);
319 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
320 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
321 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
322
jvanverth9f372462016-04-06 06:08:59 -0700323 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700324 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500325 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500326 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500327 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700328
brianosman622c8d52016-05-10 06:50:49 -0700329 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500330 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
331 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
332 fWindow->inval();
333 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500334 // Command to jump directly to the slide picker and give it focus
335 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
336 this->fShowImGuiDebugWindow = true;
337 this->fShowSlidePicker = true;
338 fWindow->inval();
339 });
340 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400341 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500342 this->fShowImGuiDebugWindow = true;
343 this->fShowSlidePicker = true;
344 fWindow->inval();
345 });
Brian Osman79086b92017-02-10 13:36:16 -0500346 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
347 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
348 fWindow->inval();
349 });
Brian Osmanf6877092017-02-13 09:39:57 -0500350 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
351 this->fShowZoomWindow = !this->fShowZoomWindow;
352 fWindow->inval();
353 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400354 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
355 this->fZoomWindowFixed = !this->fZoomWindowFixed;
356 fWindow->inval();
357 });
Greg Danield0794cc2019-03-27 16:23:26 -0400358 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
359 DisplayParams params = fWindow->getRequestedDisplayParams();
360 params.fDisableVsync = !params.fDisableVsync;
361 fWindow->setRequestedDisplayParams(params);
362 this->updateTitle();
363 fWindow->inval();
364 });
Mike Reedf702ed42019-07-22 17:00:49 -0400365 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
366 fRefresh = !fRefresh;
367 fWindow->inval();
368 });
brianosman622c8d52016-05-10 06:50:49 -0700369 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500370 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700371 fWindow->inval();
372 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400373 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500374 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400375 this->updateTitle();
376 fWindow->inval();
377 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500378 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500379 switch (fColorMode) {
380 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500381 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500382 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500383 case ColorMode::kColorManaged8888:
384 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500385 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500386 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400387 this->setColorMode(ColorMode::kColorManagedF16Norm);
388 break;
389 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500390 this->setColorMode(ColorMode::kLegacy);
391 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500392 }
brianosman622c8d52016-05-10 06:50:49 -0700393 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400394 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500395 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700396 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400397 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500398 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700399 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400400 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700401 this->changeZoomLevel(1.f / 32.f);
402 fWindow->inval();
403 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400404 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700405 this->changeZoomLevel(-1.f / 32.f);
406 fWindow->inval();
407 });
jvanverthaf236b52016-05-20 06:01:06 -0700408 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400409 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
410 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500411 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400412#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
413 if (newBackend == sk_app::Window::kVulkan_BackendType) {
414 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
415 sk_app::Window::kBackendTypeCount);
416 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
417 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500418 }
419#endif
Brian Osman621491e2017-02-28 15:45:01 -0500420 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700421 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500422 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
423 fSaveToSKP = true;
424 fWindow->inval();
425 });
Mike Reed376d8122019-03-14 11:39:02 -0400426 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
427 fShowSlideDimensions = !fShowSlideDimensions;
428 fWindow->inval();
429 });
Ben Wagner37c54032018-04-13 14:30:23 -0400430 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
431 DisplayParams params = fWindow->getRequestedDisplayParams();
432 uint32_t flags = params.fSurfaceProps.flags();
433 if (!fPixelGeometryOverrides) {
434 fPixelGeometryOverrides = true;
435 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
436 } else {
437 switch (params.fSurfaceProps.pixelGeometry()) {
438 case kUnknown_SkPixelGeometry:
439 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
440 break;
441 case kRGB_H_SkPixelGeometry:
442 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
443 break;
444 case kBGR_H_SkPixelGeometry:
445 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
446 break;
447 case kRGB_V_SkPixelGeometry:
448 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
449 break;
450 case kBGR_V_SkPixelGeometry:
451 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
452 fPixelGeometryOverrides = false;
453 break;
454 }
455 }
456 fWindow->setRequestedDisplayParams(params);
457 this->updateTitle();
458 fWindow->inval();
459 });
Ben Wagner9613e452019-01-23 10:34:59 -0500460 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500461 if (!fFontOverrides.fHinting) {
462 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400463 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500464 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500465 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400466 case SkFontHinting::kNone:
467 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500468 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400469 case SkFontHinting::kSlight:
470 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500471 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400472 case SkFontHinting::kNormal:
473 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500474 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400475 case SkFontHinting::kFull:
476 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500477 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500478 break;
479 }
480 }
481 this->updateTitle();
482 fWindow->inval();
483 });
484 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500485 if (!fPaintOverrides.fAntiAlias) {
486 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
487 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500488 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500489 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500490 } else {
491 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500492 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500493 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500494 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400495 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500496 break;
497 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500498 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500499 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400500 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500501 break;
502 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500503 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400504 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500505 break;
506 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500507 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
508 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500509 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
510 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500511 break;
512 }
513 }
514 this->updateTitle();
515 fWindow->inval();
516 });
Ben Wagner37c54032018-04-13 14:30:23 -0400517 fCommands.addCommand('D', "Modes", "DFT", [this]() {
518 DisplayParams params = fWindow->getRequestedDisplayParams();
519 uint32_t flags = params.fSurfaceProps.flags();
520 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
521 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
522 fWindow->setRequestedDisplayParams(params);
523 this->updateTitle();
524 fWindow->inval();
525 });
Ben Wagner9613e452019-01-23 10:34:59 -0500526 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500527 if (!fFontOverrides.fEdging) {
528 fFontOverrides.fEdging = true;
529 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500530 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500531 switch (fFont.getEdging()) {
532 case SkFont::Edging::kAlias:
533 fFont.setEdging(SkFont::Edging::kAntiAlias);
534 break;
535 case SkFont::Edging::kAntiAlias:
536 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
537 break;
538 case SkFont::Edging::kSubpixelAntiAlias:
539 fFont.setEdging(SkFont::Edging::kAlias);
540 fFontOverrides.fEdging = false;
541 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500542 }
543 }
544 this->updateTitle();
545 fWindow->inval();
546 });
Ben Wagner9613e452019-01-23 10:34:59 -0500547 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500548 if (!fFontOverrides.fSubpixel) {
549 fFontOverrides.fSubpixel = true;
550 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500551 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500552 if (!fFont.isSubpixel()) {
553 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500554 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500555 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500556 }
557 }
558 this->updateTitle();
559 fWindow->inval();
560 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400561 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
562 if (!fFontOverrides.fBaselineSnap) {
563 fFontOverrides.fBaselineSnap = true;
564 fFont.setBaselineSnap(false);
565 } else {
566 if (!fFont.isBaselineSnap()) {
567 fFont.setBaselineSnap(true);
568 } else {
569 fFontOverrides.fBaselineSnap = false;
570 }
571 }
572 this->updateTitle();
573 fWindow->inval();
574 });
Brian Osman805a7272018-05-02 15:40:20 -0400575 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
576 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
577 : kPerspective_Real;
578 this->updateTitle();
579 fWindow->inval();
580 });
581 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
582 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
583 : kPerspective_Off;
584 this->updateTitle();
585 fWindow->inval();
586 });
Brian Osman207d4102019-01-10 09:40:58 -0500587 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
588 fAnimTimer.togglePauseResume();
589 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400590 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
591 fZoomUI = !fZoomUI;
592 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
593 fWindow->inval();
594 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500595
jvanverth2bb3b6d2016-04-08 07:24:09 -0700596 // set up slides
597 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500598 if (FLAGS_list) {
599 this->listNames();
600 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700601
Brian Osman9bb47cf2018-04-26 15:55:00 -0400602 fPerspectivePoints[0].set(0, 0);
603 fPerspectivePoints[1].set(1, 0);
604 fPerspectivePoints[2].set(0, 1);
605 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700606 fAnimTimer.run();
607
Hal Canaryc465d132017-12-08 10:21:31 -0500608 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500609 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400610 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500611 }
612 fImGuiGamutPaint.setColor(SK_ColorWHITE);
613 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
614
jongdeok.kim804f17e2019-02-26 14:39:23 +0900615 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500616 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700617}
618
jvanverth34524262016-05-04 13:49:13 -0700619void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400620 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
621 static const struct {
622 const char* fExtension;
623 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500624 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400625 const SlideFactory fFactory;
626 } gExternalSlidesInfo[] = {
627 { ".skp", "skp-dir", FLAGS_skps,
628 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
629 return sk_make_sp<SKPSlide>(name, path);}
630 },
631 { ".jpg", "jpg-dir", FLAGS_jpgs,
632 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
633 return sk_make_sp<ImageSlide>(name, path);}
634 },
Florin Malita87ccf332018-05-04 12:23:24 -0400635#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400636 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400637 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
638 return sk_make_sp<SkottieSlide>(name, path);}
639 },
Florin Malita87ccf332018-05-04 12:23:24 -0400640#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400641#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400642 { ".svg", "svg-dir", FLAGS_svgs,
643 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
644 return sk_make_sp<SvgSlide>(name, path);}
645 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400646#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400647 };
jvanverthc265a922016-04-08 12:51:45 -0700648
Brian Salomon343553a2018-09-05 15:41:23 -0400649 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700650
Mike Klein88544fb2019-03-20 10:50:33 -0500651 const auto addSlide =
652 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
653 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
654 return;
655 }
liyuqian6f163d22016-06-13 12:26:45 -0700656
Mike Klein88544fb2019-03-20 10:50:33 -0500657 if (auto slide = fact(name, path)) {
658 dirSlides.push_back(slide);
659 fSlides.push_back(std::move(slide));
660 }
661 };
Florin Malita76a076b2018-02-15 18:40:48 -0500662
Florin Malita38792ce2018-05-08 10:36:18 -0400663 if (!FLAGS_file.isEmpty()) {
664 // single file mode
665 const SkString file(FLAGS_file[0]);
666
667 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
668 for (const auto& sinfo : gExternalSlidesInfo) {
669 if (file.endsWith(sinfo.fExtension)) {
670 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
671 return;
672 }
673 }
674
675 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
676 } else {
677 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
678 }
679
680 return;
681 }
682
683 // Bisect slide.
684 if (!FLAGS_bisect.isEmpty()) {
685 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500686 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400687 if (FLAGS_bisect.count() >= 2) {
688 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
689 bisect->onChar(*ch);
690 }
691 }
692 fSlides.push_back(std::move(bisect));
693 }
694 }
695
696 // GMs
697 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400698 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400699 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500700 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400701 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400702 fSlides.push_back(std::move(slide));
703 }
Florin Malita38792ce2018-05-08 10:36:18 -0400704 }
705 // reverse gms
706 int numGMs = fSlides.count() - firstGM;
707 for (int i = 0; i < numGMs/2; ++i) {
708 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
709 }
710
711 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400712 for (const SampleFactory factory : SampleRegistry::Range()) {
713 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500714 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400715 fSlides.push_back(slide);
716 }
Florin Malita38792ce2018-05-08 10:36:18 -0400717 }
718
Brian Osman7c979f52019-02-12 13:27:51 -0500719 // Particle demo
720 {
721 // TODO: Convert this to a sample
722 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500723 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500724 fSlides.push_back(std::move(slide));
725 }
726 }
727
Florin Malita0ffa3222018-04-05 14:34:45 -0400728 for (const auto& info : gExternalSlidesInfo) {
729 for (const auto& flag : info.fFlags) {
730 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
731 // single file
732 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
733 } else {
734 // directory
735 SkOSFile::Iter it(flag.c_str(), info.fExtension);
736 SkString name;
737 while (it.next(&name)) {
738 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
739 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400740 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400741 if (!dirSlides.empty()) {
742 fSlides.push_back(
743 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
744 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500745 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400746 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400747 }
748 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500749
750 if (!fSlides.count()) {
751 sk_sp<Slide> slide(new NullSlide());
752 fSlides.push_back(std::move(slide));
753 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700754}
755
756
jvanverth34524262016-05-04 13:49:13 -0700757Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700758 fWindow->detach();
759 delete fWindow;
760}
761
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500762struct SkPaintTitleUpdater {
763 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
764 void append(const char* s) {
765 if (fCount == 0) {
766 fTitle->append(" {");
767 } else {
768 fTitle->append(", ");
769 }
770 fTitle->append(s);
771 ++fCount;
772 }
773 void done() {
774 if (fCount > 0) {
775 fTitle->append("}");
776 }
777 }
778 SkString* fTitle;
779 int fCount;
780};
781
brianosman05de2162016-05-06 13:28:57 -0700782void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700783 if (!fWindow) {
784 return;
785 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500786 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700787 return; // Surface hasn't been created yet.
788 }
789
jvanverth34524262016-05-04 13:49:13 -0700790 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700791 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700792
Mike Kleine5acd752019-03-22 09:57:16 -0500793 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400794 if (gSkForceAnalyticAA) {
795 title.append(" <FAAA>");
796 } else {
797 title.append(" <AAA>");
798 }
799 }
800
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500801 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500802 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
803 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400804 const char* on, const char* off)
805 {
Ben Wagner9613e452019-01-23 10:34:59 -0500806 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400807 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500808 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400809 };
810
Ben Wagner9613e452019-01-23 10:34:59 -0500811 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
812 const char* on, const char* off)
813 {
814 if (fFontOverrides.*flag) {
815 paintTitle.append((fFont.*isFlag)() ? on : off);
816 }
817 };
818
819 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
820 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500821 if (fPaintOverrides.fFilterQuality) {
822 switch (fPaint.getFilterQuality()) {
823 case kNone_SkFilterQuality:
824 paintTitle.append("NoFilter");
825 break;
826 case kLow_SkFilterQuality:
827 paintTitle.append("LowFilter");
828 break;
829 case kMedium_SkFilterQuality:
830 paintTitle.append("MediumFilter");
831 break;
832 case kHigh_SkFilterQuality:
833 paintTitle.append("HighFilter");
834 break;
835 }
836 }
Ben Wagner9613e452019-01-23 10:34:59 -0500837
838 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
839 "Force Autohint", "No Force Autohint");
840 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400841 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500842 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
843 "Linear Metrics", "Non-Linear Metrics");
844 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
845 "Bitmap Text", "No Bitmap Text");
846 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
847
848 if (fFontOverrides.fEdging) {
849 switch (fFont.getEdging()) {
850 case SkFont::Edging::kAlias:
851 paintTitle.append("Alias Text");
852 break;
853 case SkFont::Edging::kAntiAlias:
854 paintTitle.append("Antialias Text");
855 break;
856 case SkFont::Edging::kSubpixelAntiAlias:
857 paintTitle.append("Subpixel Antialias Text");
858 break;
859 }
860 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400861
Mike Reed3ae47332019-01-04 10:11:46 -0500862 if (fFontOverrides.fHinting) {
863 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400864 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500865 paintTitle.append("No Hinting");
866 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400867 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500868 paintTitle.append("Slight Hinting");
869 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400870 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500871 paintTitle.append("Normal Hinting");
872 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400873 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500874 paintTitle.append("Full Hinting");
875 break;
876 }
877 }
878 paintTitle.done();
879
Brian Osman92004802017-03-06 11:47:26 -0500880 switch (fColorMode) {
881 case ColorMode::kLegacy:
882 title.append(" Legacy 8888");
883 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500884 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500885 title.append(" ColorManaged 8888");
886 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500887 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500888 title.append(" ColorManaged F16");
889 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400890 case ColorMode::kColorManagedF16Norm:
891 title.append(" ColorManaged F16 Norm");
892 break;
Brian Osman92004802017-03-06 11:47:26 -0500893 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500894
Brian Osman92004802017-03-06 11:47:26 -0500895 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500896 int curPrimaries = -1;
897 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
898 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
899 curPrimaries = i;
900 break;
901 }
902 }
Brian Osman03115dc2018-11-26 13:55:19 -0500903 title.appendf(" %s Gamma %f",
904 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500905 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700906 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500907
Ben Wagner37c54032018-04-13 14:30:23 -0400908 const DisplayParams& params = fWindow->getRequestedDisplayParams();
909 if (fPixelGeometryOverrides) {
910 switch (params.fSurfaceProps.pixelGeometry()) {
911 case kUnknown_SkPixelGeometry:
912 title.append( " Flat");
913 break;
914 case kRGB_H_SkPixelGeometry:
915 title.append( " RGB");
916 break;
917 case kBGR_H_SkPixelGeometry:
918 title.append( " BGR");
919 break;
920 case kRGB_V_SkPixelGeometry:
921 title.append( " RGBV");
922 break;
923 case kBGR_V_SkPixelGeometry:
924 title.append( " BGRV");
925 break;
926 }
927 }
928
929 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
930 title.append(" DFT");
931 }
932
csmartdalton578f0642017-02-24 16:04:47 -0700933 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700934 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500935 int msaa = fWindow->sampleCount();
936 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700937 title.appendf(" MSAA: %i", msaa);
938 }
939 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700940
941 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600942 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700943 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
944 }
945
Brian Osman805a7272018-05-02 15:40:20 -0400946 if (kPerspective_Real == fPerspectiveMode) {
947 title.append(" Perpsective (Real)");
948 } else if (kPerspective_Fake == fPerspectiveMode) {
949 title.append(" Perspective (Fake)");
950 }
951
brianosman05de2162016-05-06 13:28:57 -0700952 fWindow->setTitle(title.c_str());
953}
954
Florin Malitaab99c342018-01-16 16:23:03 -0500955int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500956
957 if (!FLAGS_slide.isEmpty()) {
958 int count = fSlides.count();
959 for (int i = 0; i < count; i++) {
960 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500961 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500962 }
963 }
964
965 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
966 this->listNames();
967 }
968
Florin Malitaab99c342018-01-16 16:23:03 -0500969 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500970}
971
Florin Malitaab99c342018-01-16 16:23:03 -0500972void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500973 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500974 for (const auto& slide : fSlides) {
975 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500976 }
977}
978
Florin Malitaab99c342018-01-16 16:23:03 -0500979void Viewer::setCurrentSlide(int slide) {
980 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700981
Florin Malitaab99c342018-01-16 16:23:03 -0500982 if (slide == fCurrentSlide) {
983 return;
984 }
985
986 if (fCurrentSlide >= 0) {
987 fSlides[fCurrentSlide]->unload();
988 }
989
990 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
991 SkIntToScalar(fWindow->height()));
992 fCurrentSlide = slide;
993 this->setupCurrentSlide();
994}
995
996void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500997 if (fCurrentSlide >= 0) {
998 // prepare dimensions for image slides
999 fGesture.resetTouchState();
1000 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001001
Jim Van Verth0848fb02018-01-22 13:39:30 -05001002 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1003 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1004 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001005
Jim Van Verth0848fb02018-01-22 13:39:30 -05001006 // Start with a matrix that scales the slide to the available screen space
1007 if (fWindow->scaleContentToFit()) {
1008 if (windowRect.width() > 0 && windowRect.height() > 0) {
1009 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1010 }
liyuqiane46e4f02016-05-20 07:32:19 -07001011 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001012
1013 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001014 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001015
1016 this->updateTitle();
1017 this->updateUIState();
1018
1019 fStatsLayer.resetMeasurements();
1020
1021 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001022 }
jvanverthc265a922016-04-08 12:51:45 -07001023}
1024
1025#define MAX_ZOOM_LEVEL 8
1026#define MIN_ZOOM_LEVEL -8
1027
jvanverth34524262016-05-04 13:49:13 -07001028void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001029 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -04001030 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001031 this->preTouchMatrixChanged();
1032}
Yuqian Li755778c2018-03-28 16:23:31 -04001033
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001034void Viewer::preTouchMatrixChanged() {
1035 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001036 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1037 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1038 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1039 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1040}
1041
Brian Osman805a7272018-05-02 15:40:20 -04001042SkMatrix Viewer::computePerspectiveMatrix() {
1043 SkScalar w = fWindow->width(), h = fWindow->height();
1044 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1045 SkPoint perspPts[4] = {
1046 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1047 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1048 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1049 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1050 };
1051 SkMatrix m;
1052 m.setPolyToPoly(orthoPts, perspPts, 4);
1053 return m;
1054}
1055
Yuqian Li755778c2018-03-28 16:23:31 -04001056SkMatrix Viewer::computePreTouchMatrix() {
1057 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001058
1059 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001060 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001061 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001062
1063 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1064 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001065
Brian Osman805a7272018-05-02 15:40:20 -04001066 if (kPerspective_Real == fPerspectiveMode) {
1067 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001068 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001069 }
1070
Yuqian Li755778c2018-03-28 16:23:31 -04001071 return m;
jvanverthc265a922016-04-08 12:51:45 -07001072}
1073
liyuqiand3cdbca2016-05-17 12:44:20 -07001074SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001075 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001076 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001077 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001078 return m;
jvanverthc265a922016-04-08 12:51:45 -07001079}
1080
Brian Osman621491e2017-02-28 15:45:01 -05001081void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001082 fPersistentCache.reset();
1083 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001084 fBackendType = backendType;
1085
1086 fWindow->detach();
1087
Brian Osman70d2f432017-11-08 09:54:10 -05001088#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001089 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1090 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001091 DisplayParams params = fWindow->getRequestedDisplayParams();
1092 delete fWindow;
1093 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001094
Brian Osman70d2f432017-11-08 09:54:10 -05001095 // re-register callbacks
1096 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001097 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001098 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001099 fWindow->pushLayer(&fImGuiLayer);
1100
Brian Osman70d2f432017-11-08 09:54:10 -05001101 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1102 // will still include our correct sample count. But the re-created fWindow will lose that
1103 // information. On Windows, we need to re-create the window when changing sample count,
1104 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1105 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1106 // as if we had never changed windows at all).
1107 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001108#endif
1109
Brian Osman70d2f432017-11-08 09:54:10 -05001110 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001111}
1112
Brian Osman92004802017-03-06 11:47:26 -05001113void Viewer::setColorMode(ColorMode colorMode) {
1114 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001115 this->updateTitle();
1116 fWindow->inval();
1117}
1118
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001119class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1120public:
Mike Reed3ae47332019-01-04 10:11:46 -05001121 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1122 SkFont* font, Viewer::SkFontFields* ffields)
1123 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001124 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001125 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1126 sk_sp<SkTextBlob>* cache) {
1127 bool blobWillChange = false;
1128 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001129 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1130 bool shouldDraw = this->filterFont(&filteredFont);
1131 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001132 blobWillChange = true;
1133 break;
1134 }
1135 }
1136 if (!blobWillChange) {
1137 return blob;
1138 }
1139
1140 SkTextBlobBuilder builder;
1141 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001142 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1143 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001144 if (!shouldDraw) {
1145 continue;
1146 }
1147
Mike Reed3ae47332019-01-04 10:11:46 -05001148 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001149
Ben Wagner41e40472018-09-24 13:01:54 -04001150 const SkTextBlobBuilder::RunBuffer& runBuffer
1151 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001152 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001153 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001154 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001155 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001156 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001157 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001158 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001159 it.glyphCount(), it.textSize(), SkString())
1160 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1161 uint32_t glyphCount = it.glyphCount();
1162 if (it.glyphs()) {
1163 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1164 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1165 }
1166 if (it.pos()) {
1167 size_t posSize = sizeof(decltype(*it.pos()));
1168 uint8_t positioning = it.positioning();
1169 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1170 }
1171 if (it.text()) {
1172 size_t textSize = sizeof(decltype(*it.text()));
1173 uint32_t textCount = it.textSize();
1174 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1175 }
1176 if (it.clusters()) {
1177 size_t clusterSize = sizeof(decltype(*it.clusters()));
1178 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1179 }
1180 }
1181 *cache = builder.make();
1182 return cache->get();
1183 }
1184 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1185 const SkPaint& paint) override {
1186 sk_sp<SkTextBlob> cache;
1187 this->SkPaintFilterCanvas::onDrawTextBlob(
1188 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1189 }
Mike Reed3ae47332019-01-04 10:11:46 -05001190 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001191 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001192 font->writable()->setSize(fFont->getSize());
1193 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001194 if (fFontOverrides->fScaleX) {
1195 font->writable()->setScaleX(fFont->getScaleX());
1196 }
1197 if (fFontOverrides->fSkewX) {
1198 font->writable()->setSkewX(fFont->getSkewX());
1199 }
Mike Reed3ae47332019-01-04 10:11:46 -05001200 if (fFontOverrides->fHinting) {
1201 font->writable()->setHinting(fFont->getHinting());
1202 }
Ben Wagner9613e452019-01-23 10:34:59 -05001203 if (fFontOverrides->fEdging) {
1204 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001205 }
Ben Wagner9613e452019-01-23 10:34:59 -05001206 if (fFontOverrides->fEmbolden) {
1207 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001208 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001209 if (fFontOverrides->fBaselineSnap) {
1210 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1211 }
Ben Wagner9613e452019-01-23 10:34:59 -05001212 if (fFontOverrides->fLinearMetrics) {
1213 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001214 }
Ben Wagner9613e452019-01-23 10:34:59 -05001215 if (fFontOverrides->fSubpixel) {
1216 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001217 }
Ben Wagner9613e452019-01-23 10:34:59 -05001218 if (fFontOverrides->fEmbeddedBitmaps) {
1219 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001220 }
Ben Wagner9613e452019-01-23 10:34:59 -05001221 if (fFontOverrides->fForceAutoHinting) {
1222 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001223 }
Ben Wagner9613e452019-01-23 10:34:59 -05001224
Mike Reed3ae47332019-01-04 10:11:46 -05001225 return true;
1226 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001227 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001228 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001229 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001230 }
Ben Wagner9613e452019-01-23 10:34:59 -05001231 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001232 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001233 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001234 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001235 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001236 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001237 return true;
1238 }
1239 SkPaint* fPaint;
1240 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001241 SkFont* fFont;
1242 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001243};
1244
Robert Phillips9882dae2019-03-04 11:00:10 -05001245void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001246 if (fCurrentSlide < 0) {
1247 return;
1248 }
1249
Robert Phillips9882dae2019-03-04 11:00:10 -05001250 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001251
Brian Osmanf750fbc2017-02-08 10:47:28 -05001252 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001253 SkSurface* slideSurface = surface;
1254 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001255 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001256
Brian Osmane0d4fba2017-03-15 10:24:55 -04001257 // 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 -05001258 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001259 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001260 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001261 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001262 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001263 }
1264
Brian Osman3ac99cf2017-12-01 11:23:53 -05001265 if (fSaveToSKP) {
1266 SkPictureRecorder recorder;
1267 SkCanvas* recorderCanvas = recorder.beginRecording(
1268 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001269 fSlides[fCurrentSlide]->draw(recorderCanvas);
1270 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1271 SkFILEWStream stream("sample_app.skp");
1272 picture->serialize(&stream);
1273 fSaveToSKP = false;
1274 }
1275
Brian Osmane9ed0f02018-11-26 14:50:05 -05001276 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001277 SkColorType colorType;
1278 switch (fColorMode) {
1279 case ColorMode::kLegacy:
1280 case ColorMode::kColorManaged8888:
1281 colorType = kN32_SkColorType;
1282 break;
1283 case ColorMode::kColorManagedF16:
1284 colorType = kRGBA_F16_SkColorType;
1285 break;
1286 case ColorMode::kColorManagedF16Norm:
1287 colorType = kRGBA_F16Norm_SkColorType;
1288 break;
1289 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001290
1291 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001292 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1293 slideCanvas->getProps(&props);
1294
Brian Osmane9ed0f02018-11-26 14:50:05 -05001295 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1296 return Window::kRaster_BackendType == this->fBackendType
1297 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001298 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001299 };
1300
Brian Osman03115dc2018-11-26 13:55:19 -05001301 // We need to render offscreen if we're...
1302 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1303 // ... in any raster mode, because the window surface is actually GL
1304 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001305 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001306 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001307 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001308 Window::kRaster_BackendType == fBackendType ||
1309 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001310
Brian Osmane9ed0f02018-11-26 14:50:05 -05001311 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001312 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001313 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001314 }
1315
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001316 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001317 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001318 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001319 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001320 if (fTiled) {
1321 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1322 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1323 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1324 SkCanvas* tileCanvas = tileSurface->getCanvas();
1325 SkMatrix m = this->computeMatrix();
1326 for (int y = 0; y < fWindow->height(); y += tileH) {
1327 for (int x = 0; x < fWindow->width(); x += tileW) {
1328 SkAutoCanvasRestore acr(tileCanvas, true);
1329 tileCanvas->translate(-x, -y);
1330 tileCanvas->clear(SK_ColorTRANSPARENT);
1331 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001332 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1333 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001334 fSlides[fCurrentSlide]->draw(&filterCanvas);
1335 tileSurface->draw(slideCanvas, x, y, nullptr);
1336 }
1337 }
1338
1339 // Draw borders between tiles
1340 if (fDrawTileBoundaries) {
1341 SkPaint border;
1342 border.setColor(0x60FF00FF);
1343 border.setStyle(SkPaint::kStroke_Style);
1344 for (int y = 0; y < fWindow->height(); y += tileH) {
1345 for (int x = 0; x < fWindow->width(); x += tileW) {
1346 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1347 }
1348 }
1349 }
1350 } else {
1351 slideCanvas->concat(this->computeMatrix());
1352 if (kPerspective_Real == fPerspectiveMode) {
1353 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1354 }
Mike Reed3ae47332019-01-04 10:11:46 -05001355 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001356 fSlides[fCurrentSlide]->draw(&filterCanvas);
1357 }
Brian Osman56a24812017-12-19 11:15:16 -05001358 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001359 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001360
1361 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001362 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001363 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001364 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001365
1366 // If we rendered offscreen, snap an image and push the results to the window's canvas
1367 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001368 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001369
Robert Phillips9882dae2019-03-04 11:00:10 -05001370 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001371 SkPaint paint;
1372 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001373 int prePerspectiveCount = canvas->save();
1374 if (kPerspective_Fake == fPerspectiveMode) {
1375 paint.setFilterQuality(kHigh_SkFilterQuality);
1376 canvas->clear(SK_ColorWHITE);
1377 canvas->concat(this->computePerspectiveMatrix());
1378 }
Brian Osman03115dc2018-11-26 13:55:19 -05001379 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001380 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001381 }
Mike Reed376d8122019-03-14 11:39:02 -04001382
1383 if (fShowSlideDimensions) {
1384 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1385 SkPaint paint;
1386 paint.setColor(0x40FFFF00);
1387 surface->getCanvas()->drawRect(r, paint);
1388 }
liyuqian6f163d22016-06-13 12:26:45 -07001389}
1390
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001391void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001392 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001393 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001394}
Jim Van Verth6f449692017-02-14 15:16:46 -05001395
Robert Phillips9882dae2019-03-04 11:00:10 -05001396void Viewer::onPaint(SkSurface* surface) {
1397 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001398
Robert Phillips9882dae2019-03-04 11:00:10 -05001399 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001400
Brian Osmand67e5182017-12-08 16:46:09 -05001401 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001402
1403 if (GrContext* ctx = fWindow->getGrContext()) {
1404 // Clean out cache items that haven't been used in more than 10 seconds.
1405 ctx->performDeferredCleanup(std::chrono::seconds(10));
1406 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001407}
1408
Ben Wagnera1915972018-08-09 15:06:19 -04001409void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001410 if (fCurrentSlide >= 0) {
1411 fSlides[fCurrentSlide]->resize(width, height);
1412 }
Ben Wagnera1915972018-08-09 15:06:19 -04001413}
1414
Florin Malitacefc1b92018-02-19 21:43:47 -05001415SkPoint Viewer::mapEvent(float x, float y) {
1416 const auto m = this->computeMatrix();
1417 SkMatrix inv;
1418
1419 SkAssertResult(m.invert(&inv));
1420
1421 return inv.mapXY(x, y);
1422}
1423
Hal Canaryb1f411a2019-08-29 10:39:22 -04001424bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001425 if (GestureDevice::kMouse == fGestureDevice) {
1426 return false;
1427 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001428
1429 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001430 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001431 fWindow->inval();
1432 return true;
1433 }
1434
liyuqiand3cdbca2016-05-17 12:44:20 -07001435 void* castedOwner = reinterpret_cast<void*>(owner);
1436 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001437 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001438 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001439#if defined(SK_BUILD_FOR_IOS)
1440 // TODO: move IOS swipe detection higher up into the platform code
1441 SkPoint dir;
1442 if (fGesture.isFling(&dir)) {
1443 // swiping left or right
1444 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1445 if (dir.fX < 0) {
1446 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1447 fCurrentSlide + 1 : 0);
1448 } else {
1449 this->setCurrentSlide(fCurrentSlide > 0 ?
1450 fCurrentSlide - 1 : fSlides.count() - 1);
1451 }
1452 }
1453 fGesture.reset();
1454 }
1455#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001456 break;
1457 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001458 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001459 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001460 break;
1461 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001462 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001463 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001464 break;
1465 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001466 default: {
1467 // kLeft and kRight are only for swipes
1468 SkASSERT(false);
1469 break;
1470 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001471 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001472 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001473 fWindow->inval();
1474 return true;
1475}
1476
Hal Canaryb1f411a2019-08-29 10:39:22 -04001477bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001478 if (GestureDevice::kTouch == fGestureDevice) {
1479 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001480 }
Brian Osman16c81a12017-12-20 11:58:34 -05001481
Florin Malitacefc1b92018-02-19 21:43:47 -05001482 const auto slidePt = this->mapEvent(x, y);
1483 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1484 fWindow->inval();
1485 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001486 }
1487
1488 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001489 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001490 fGesture.touchEnd(nullptr);
1491 break;
1492 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001493 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001494 fGesture.touchBegin(nullptr, x, y);
1495 break;
1496 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001497 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001498 fGesture.touchMoved(nullptr, x, y);
1499 break;
1500 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001501 default: {
1502 SkASSERT(false); // shouldn't see kRight or kLeft here
1503 break;
1504 }
Brian Osman16c81a12017-12-20 11:58:34 -05001505 }
1506 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1507
Hal Canaryb1f411a2019-08-29 10:39:22 -04001508 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001509 fWindow->inval();
1510 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001511 return true;
1512}
1513
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001514bool Viewer::onFling(skui::InputState state) {
1515 if (skui::InputState::kRight == state) {
1516 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1517 return true;
1518 } else if (skui::InputState::kLeft == state) {
1519 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1520 return true;
1521 }
1522 return false;
1523}
1524
1525bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1526 switch (state) {
1527 case skui::InputState::kDown:
1528 fGesture.startZoom();
1529 return true;
1530 break;
1531 case skui::InputState::kMove:
1532 fGesture.updateZoom(scale, x, y, x, y);
1533 return true;
1534 break;
1535 case skui::InputState::kUp:
1536 fGesture.endZoom();
1537 return true;
1538 break;
1539 default:
1540 SkASSERT(false);
1541 break;
1542 }
1543
1544 return false;
1545}
1546
Brian Osmana109e392017-02-24 09:49:14 -05001547static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001548 // The gamut image covers a (0.8 x 0.9) shaped region
1549 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001550
1551 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1552 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1553 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001554 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1555 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1556 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001557
Brian Osman535c5e32019-02-09 16:32:58 -05001558 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1559 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1560 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1561 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1562 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001563}
1564
Ben Wagner3627d2e2018-06-26 14:23:20 -04001565static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001566 ImGui::DragCanvas dc(pt);
1567 dc.fillColor(IM_COL32(0, 0, 0, 128));
1568 dc.dragPoint(pt);
1569 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001570}
1571
Brian Osman9bb47cf2018-04-26 15:55:00 -04001572static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001573 ImGui::DragCanvas dc(pts);
1574 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001575
Brian Osman535c5e32019-02-09 16:32:58 -05001576 for (int i = 0; i < 4; ++i) {
1577 dc.dragPoint(pts + i);
1578 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001579
Brian Osman535c5e32019-02-09 16:32:58 -05001580 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1581 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1582 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1583 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001584
Brian Osman535c5e32019-02-09 16:32:58 -05001585 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001586}
1587
Brian Osmand67e5182017-12-08 16:46:09 -05001588void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001589 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1590 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001591 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001592 }
1593
1594 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001595 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1596 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001597 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001598 DisplayParams params = fWindow->getRequestedDisplayParams();
1599 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001600 const GrContext* ctx = fWindow->getGrContext();
1601
Brian Osmana109e392017-02-24 09:49:14 -05001602 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1603 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001604 if (ImGui::CollapsingHeader("Backend")) {
1605 int newBackend = static_cast<int>(fBackendType);
1606 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1607 ImGui::SameLine();
1608 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001609#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1610 ImGui::SameLine();
1611 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1612#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001613#if defined(SK_DAWN)
1614 ImGui::SameLine();
1615 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1616#endif
Brian Osman621491e2017-02-28 15:45:01 -05001617#if defined(SK_VULKAN)
1618 ImGui::SameLine();
1619 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1620#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001621#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001622 ImGui::SameLine();
1623 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1624#endif
Brian Osman621491e2017-02-28 15:45:01 -05001625 if (newBackend != fBackendType) {
1626 fDeferredActions.push_back([=]() {
1627 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1628 });
1629 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001630
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001631 bool* wire = &params.fGrContextOptions.fWireframeMode;
1632 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1633 paramsChanged = true;
1634 }
Brian Salomon99a33902017-03-07 15:16:34 -05001635
Brian Osman28b12522017-03-08 17:10:24 -05001636 if (ctx) {
1637 int sampleCount = fWindow->sampleCount();
1638 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001639 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001640 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1641 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1642 ImGui::RadioButton("16", &sampleCount, 16);
1643
1644 if (sampleCount != params.fMSAASampleCount) {
1645 params.fMSAASampleCount = sampleCount;
1646 paramsChanged = true;
1647 }
1648 }
1649
Ben Wagner37c54032018-04-13 14:30:23 -04001650 int pixelGeometryIdx = 0;
1651 if (fPixelGeometryOverrides) {
1652 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1653 }
1654 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1655 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1656 {
1657 uint32_t flags = params.fSurfaceProps.flags();
1658 if (pixelGeometryIdx == 0) {
1659 fPixelGeometryOverrides = false;
1660 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1661 } else {
1662 fPixelGeometryOverrides = true;
1663 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1664 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1665 }
1666 paramsChanged = true;
1667 }
1668
1669 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1670 if (ImGui::Checkbox("DFT", &useDFT)) {
1671 uint32_t flags = params.fSurfaceProps.flags();
1672 if (useDFT) {
1673 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1674 } else {
1675 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1676 }
1677 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1678 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1679 paramsChanged = true;
1680 }
1681
Brian Osman8a9de3d2017-03-01 14:59:05 -05001682 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001683 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001684 auto prButton = [&](GpuPathRenderers x) {
1685 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001686 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1687 params.fGrContextOptions.fGpuPathRenderers = x;
1688 paramsChanged = true;
1689 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001690 }
1691 };
1692
1693 if (!ctx) {
1694 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001695 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001696 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001697 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001698 prButton(GpuPathRenderers::kStencilAndCover);
1699 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001700 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001701 prButton(GpuPathRenderers::kNone);
1702 } else {
1703 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001704 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001705 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001706 prButton(GpuPathRenderers::kCoverageCounting);
1707 }
Jim Van Verth83010462017-03-16 08:45:39 -04001708 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001709 prButton(GpuPathRenderers::kTessellating);
1710 prButton(GpuPathRenderers::kNone);
1711 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001712 ImGui::TreePop();
1713 }
Brian Osman621491e2017-02-28 15:45:01 -05001714 }
1715
Ben Wagner964571d2019-03-08 12:35:06 -05001716 if (ImGui::CollapsingHeader("Tiling")) {
1717 ImGui::Checkbox("Enable", &fTiled);
1718 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1719 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1720 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1721 }
1722
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001723 if (ImGui::CollapsingHeader("Transform")) {
1724 float zoom = fZoomLevel;
1725 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1726 fZoomLevel = zoom;
1727 this->preTouchMatrixChanged();
1728 paramsChanged = true;
1729 }
1730 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001731 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001732 fRotation = deg;
1733 this->preTouchMatrixChanged();
1734 paramsChanged = true;
1735 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001736 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1737 if (ImGui_DragLocation(&fOffset)) {
1738 this->preTouchMatrixChanged();
1739 paramsChanged = true;
1740 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001741 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1742 this->preTouchMatrixChanged();
1743 paramsChanged = true;
1744 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001745 }
Brian Osman805a7272018-05-02 15:40:20 -04001746 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1747 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1748 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001749 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001750 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001751 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001752 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001753 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001754 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001755 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001756 }
1757
Ben Wagnera580fb32018-04-17 11:16:32 -04001758 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001759 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001760 if (fPaintOverrides.fAntiAlias) {
1761 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001762 }
1763 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001764 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001765 {
1766 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1767 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001768 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001769 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1770 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001771 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001772 fPaintOverrides.fAntiAlias = true;
1773 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001774 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001775 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001776 case SkPaintFields::AntiAliasState::Alias:
1777 break;
1778 case SkPaintFields::AntiAliasState::Normal:
1779 break;
1780 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1781 gSkUseAnalyticAA = true;
1782 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001783 break;
1784 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1785 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001786 break;
1787 }
1788 }
1789 paramsChanged = true;
1790 }
1791
Ben Wagner99a78dc2018-05-09 18:23:51 -04001792 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001793 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001794 bool (SkPaint::* isFlag)() const,
1795 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001796 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001797 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001798 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001799 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001800 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001801 if (ImGui::Combo(label, &itemIndex, items)) {
1802 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001803 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001804 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001805 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001806 (fPaint.*setFlag)(itemIndex == 2);
1807 }
1808 paramsChanged = true;
1809 }
1810 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001811
Ben Wagner99a78dc2018-05-09 18:23:51 -04001812 paintFlag("Dither",
1813 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001814 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001815 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001816
1817 int filterQualityIdx = 0;
1818 if (fPaintOverrides.fFilterQuality) {
1819 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1820 }
1821 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1822 "Default\0None\0Low\0Medium\0High\0\0"))
1823 {
1824 if (filterQualityIdx == 0) {
1825 fPaintOverrides.fFilterQuality = false;
1826 fPaint.setFilterQuality(kNone_SkFilterQuality);
1827 } else {
1828 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1829 fPaintOverrides.fFilterQuality = true;
1830 }
1831 paramsChanged = true;
1832 }
Ben Wagner9613e452019-01-23 10:34:59 -05001833 }
Hal Canary02738a82019-01-21 18:51:32 +00001834
Ben Wagner9613e452019-01-23 10:34:59 -05001835 if (ImGui::CollapsingHeader("Font")) {
1836 int hintingIdx = 0;
1837 if (fFontOverrides.fHinting) {
1838 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1839 }
1840 if (ImGui::Combo("Hinting", &hintingIdx,
1841 "Default\0None\0Slight\0Normal\0Full\0\0"))
1842 {
1843 if (hintingIdx == 0) {
1844 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001845 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001846 } else {
1847 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1848 fFontOverrides.fHinting = true;
1849 }
1850 paramsChanged = true;
1851 }
Hal Canary02738a82019-01-21 18:51:32 +00001852
Ben Wagner9613e452019-01-23 10:34:59 -05001853 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1854 bool SkFontFields::* flag,
1855 bool (SkFont::* isFlag)() const,
1856 void (SkFont::* setFlag)(bool) )
1857 {
1858 int itemIndex = 0;
1859 if (fFontOverrides.*flag) {
1860 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1861 }
1862 if (ImGui::Combo(label, &itemIndex, items)) {
1863 if (itemIndex == 0) {
1864 fFontOverrides.*flag = false;
1865 } else {
1866 fFontOverrides.*flag = true;
1867 (fFont.*setFlag)(itemIndex == 2);
1868 }
1869 paramsChanged = true;
1870 }
1871 };
Hal Canary02738a82019-01-21 18:51:32 +00001872
Ben Wagner9613e452019-01-23 10:34:59 -05001873 fontFlag("Fake Bold Glyphs",
1874 "Default\0No Fake Bold\0Fake Bold\0\0",
1875 &SkFontFields::fEmbolden,
1876 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001877
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001878 fontFlag("Baseline Snapping",
1879 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1880 &SkFontFields::fBaselineSnap,
1881 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1882
Ben Wagner9613e452019-01-23 10:34:59 -05001883 fontFlag("Linear Text",
1884 "Default\0No Linear Text\0Linear Text\0\0",
1885 &SkFontFields::fLinearMetrics,
1886 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001887
Ben Wagner9613e452019-01-23 10:34:59 -05001888 fontFlag("Subpixel Position Glyphs",
1889 "Default\0Pixel Text\0Subpixel Text\0\0",
1890 &SkFontFields::fSubpixel,
1891 &SkFont::isSubpixel, &SkFont::setSubpixel);
1892
1893 fontFlag("Embedded Bitmap Text",
1894 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1895 &SkFontFields::fEmbeddedBitmaps,
1896 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1897
1898 fontFlag("Force Auto-Hinting",
1899 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1900 &SkFontFields::fForceAutoHinting,
1901 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1902
1903 int edgingIdx = 0;
1904 if (fFontOverrides.fEdging) {
1905 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1906 }
1907 if (ImGui::Combo("Edging", &edgingIdx,
1908 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1909 {
1910 if (edgingIdx == 0) {
1911 fFontOverrides.fEdging = false;
1912 fFont.setEdging(SkFont::Edging::kAlias);
1913 } else {
1914 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1915 fFontOverrides.fEdging = true;
1916 }
1917 paramsChanged = true;
1918 }
1919
Ben Wagner15a8d572019-03-21 13:35:44 -04001920 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1921 if (fFontOverrides.fSize) {
1922 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001923 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001924 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001925 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001926 fFontOverrides.fSizeRange[0],
1927 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001928 "%.6f", 2.0f))
1929 {
Mike Reed3ae47332019-01-04 10:11:46 -05001930 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001931 paramsChanged = true;
1932 }
1933 }
1934
1935 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1936 if (fFontOverrides.fScaleX) {
1937 float scaleX = fFont.getScaleX();
1938 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1939 fFont.setScaleX(scaleX);
1940 paramsChanged = true;
1941 }
1942 }
1943
1944 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1945 if (fFontOverrides.fSkewX) {
1946 float skewX = fFont.getSkewX();
1947 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1948 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001949 paramsChanged = true;
1950 }
1951 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001952 }
1953
Mike Reed81f60ec2018-05-15 10:09:52 -04001954 {
1955 SkMetaData controls;
1956 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1957 if (ImGui::CollapsingHeader("Current Slide")) {
1958 SkMetaData::Iter iter(controls);
1959 const char* name;
1960 SkMetaData::Type type;
1961 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001962 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001963 if (type == SkMetaData::kScalar_Type) {
1964 float val[3];
1965 SkASSERT(count == 3);
1966 controls.findScalars(name, &count, val);
1967 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1968 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001969 }
Ben Wagner110c7032019-03-22 17:03:59 -04001970 } else if (type == SkMetaData::kBool_Type) {
1971 bool val;
1972 SkASSERT(count == 1);
1973 controls.findBool(name, &val);
1974 if (ImGui::Checkbox(name, &val)) {
1975 controls.setBool(name, val);
1976 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001977 }
1978 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001979 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001980 }
1981 }
1982 }
1983
Ben Wagner7a3c6742018-04-23 10:01:07 -04001984 if (fShowSlidePicker) {
1985 ImGui::SetNextTreeNodeOpen(true);
1986 }
Brian Osman79086b92017-02-10 13:36:16 -05001987 if (ImGui::CollapsingHeader("Slide")) {
1988 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001989 static ImVector<const char*> filteredSlideNames;
1990 static ImVector<int> filteredSlideIndices;
1991
Brian Osmanfce09c52017-11-14 15:32:20 -05001992 if (fShowSlidePicker) {
1993 ImGui::SetKeyboardFocusHere();
1994 fShowSlidePicker = false;
1995 }
1996
Brian Osman79086b92017-02-10 13:36:16 -05001997 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001998 filteredSlideNames.clear();
1999 filteredSlideIndices.clear();
2000 int filteredIndex = 0;
2001 for (int i = 0; i < fSlides.count(); ++i) {
2002 const char* slideName = fSlides[i]->getName().c_str();
2003 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2004 if (i == fCurrentSlide) {
2005 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002006 }
Brian Osmanf479e422017-11-08 13:11:36 -05002007 filteredSlideNames.push_back(slideName);
2008 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002009 }
Brian Osman79086b92017-02-10 13:36:16 -05002010 }
Brian Osmanf479e422017-11-08 13:11:36 -05002011
Brian Osmanf479e422017-11-08 13:11:36 -05002012 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2013 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002014 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002015 }
2016 }
Brian Osmana109e392017-02-24 09:49:14 -05002017
2018 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002019 ColorMode newMode = fColorMode;
2020 auto cmButton = [&](ColorMode mode, const char* label) {
2021 if (ImGui::RadioButton(label, mode == fColorMode)) {
2022 newMode = mode;
2023 }
2024 };
2025
2026 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002027 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2028 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002029 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002030
2031 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002032 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002033 }
2034
2035 // Pick from common gamuts:
2036 int primariesIdx = 4; // Default: Custom
2037 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2038 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2039 primariesIdx = i;
2040 break;
2041 }
2042 }
2043
Brian Osman03115dc2018-11-26 13:55:19 -05002044 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002045 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002046
Brian Osmana109e392017-02-24 09:49:14 -05002047 if (ImGui::Combo("Primaries", &primariesIdx,
2048 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2049 if (primariesIdx >= 0 && primariesIdx <= 3) {
2050 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2051 }
2052 }
2053
2054 // Allow direct editing of gamut
2055 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2056 }
Brian Osman207d4102019-01-10 09:40:58 -05002057
2058 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002059 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002060 if (ImGui::Checkbox("Pause", &isPaused)) {
2061 fAnimTimer.togglePauseResume();
2062 }
Brian Osman707d2022019-01-10 11:27:34 -05002063
2064 float speed = fAnimTimer.getSpeed();
2065 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2066 fAnimTimer.setSpeed(speed);
2067 }
Brian Osman207d4102019-01-10 09:40:58 -05002068 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002069
Brian Osmanfd7657c2019-04-25 11:34:07 -04002070 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2071#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2072 || Window::kANGLE_BackendType == fBackendType
2073#endif
2074 ;
2075
2076 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2077 // than GL, we need to force it on. Just do that on the first frame after the backend
2078 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002079 if (!backendIsGL &&
2080 params.fGrContextOptions.fShaderCacheStrategy !=
2081 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2082 params.fGrContextOptions.fShaderCacheStrategy =
2083 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002084 paramsChanged = true;
2085 fPersistentCache.reset();
2086 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002087 // To re-load shaders from the currently active programs, we flush all caches on one
2088 // frame, then set a flag to poll the cache on the next frame.
2089 static bool gLoadPending = false;
2090 if (gLoadPending) {
2091 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2092 int hitCount) {
2093 CachedGLSL& entry(fCachedGLSL.push_back());
2094 entry.fKey = key;
2095 SkMD5 hash;
2096 hash.write(key->bytes(), key->size());
2097 SkMD5::Digest digest = hash.finish();
2098 for (int i = 0; i < 16; ++i) {
2099 entry.fKeyString.appendf("%02x", digest.data[i]);
2100 }
2101
Brian Osmana66081d2019-09-03 14:59:26 -04002102 SkReader32 reader(data->data(), data->size());
2103 entry.fShaderType = reader.readU32();
2104 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2105 entry.fInputs,
2106 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002107 };
2108 fCachedGLSL.reset();
2109 fPersistentCache.foreach(collectShaders);
2110 gLoadPending = false;
2111 }
2112
2113 // Defer actually doing the load/save logic so that we can trigger a save when we
2114 // start or finish hovering on a tree node in the list below:
2115 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002116 bool doSave = ImGui::Button("Save");
2117 if (backendIsGL) {
2118 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002119 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2120 GrContextOptions::ShaderCacheStrategy::kSkSL;
2121 if (ImGui::Checkbox("SkSL", &sksl)) {
2122 params.fGrContextOptions.fShaderCacheStrategy = sksl
2123 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2124 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002125 paramsChanged = true;
2126 doLoad = true;
2127 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2128 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002129 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002130
2131 ImGui::BeginChild("##ScrollingRegion");
2132 for (auto& entry : fCachedGLSL) {
2133 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2134 bool hovered = ImGui::IsItemHovered();
2135 if (hovered != entry.fHovered) {
2136 // Force a save to patch the highlight shader in/out
2137 entry.fHovered = hovered;
2138 doSave = true;
2139 }
2140 if (inTreeNode) {
2141 // Full width, and a reasonable amount of space for each shader.
2142 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2143 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2144 boxSize);
2145 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2146 boxSize);
2147 ImGui::TreePop();
2148 }
2149 }
2150 ImGui::EndChild();
2151
2152 if (doLoad) {
2153 fPersistentCache.reset();
2154 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2155 gLoadPending = true;
2156 }
2157 if (doSave) {
2158 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002159 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002160 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2161 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002162
Brian Osman072e6fc2019-06-12 11:35:41 -04002163 SkSL::String highlight;
2164 if (!sksl) {
2165 highlight = shaderCaps->versionDeclString();
2166 if (shaderCaps->usesPrecisionModifiers()) {
2167 highlight.append("precision mediump float;\n");
2168 }
Brian Osman5bee3902019-05-07 09:55:45 -04002169 }
2170 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002171 highlight.appendf("out %s sk_FragColor;\n"
2172 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2173 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002174
2175 fPersistentCache.reset();
2176 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2177 for (auto& entry : fCachedGLSL) {
2178 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2179 if (entry.fHovered) {
2180 entry.fShader[kFragment_GrShaderType] = highlight;
2181 }
2182
Brian Osmana085a412019-04-25 09:44:43 -04002183 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2184 entry.fShader,
2185 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002186 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002187 fPersistentCache.store(*entry.fKey, *data);
2188
2189 entry.fShader[kFragment_GrShaderType] = backup;
2190 }
2191 }
2192 }
Brian Osman79086b92017-02-10 13:36:16 -05002193 }
Brian Salomon99a33902017-03-07 15:16:34 -05002194 if (paramsChanged) {
2195 fDeferredActions.push_back([=]() {
2196 fWindow->setRequestedDisplayParams(params);
2197 fWindow->inval();
2198 this->updateTitle();
2199 });
2200 }
Brian Osman79086b92017-02-10 13:36:16 -05002201 ImGui::End();
2202 }
2203
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002204 if (gShaderErrorHandler.fErrors.count()) {
2205 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2206 ImGui::Begin("Shader Errors");
2207 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2208 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
2209 ImGui::TextWrapped("%s", gShaderErrorHandler.fShaders[i].c_str());
2210 }
2211 ImGui::End();
2212 gShaderErrorHandler.reset();
2213 }
2214
Brian Osmanf6877092017-02-13 09:39:57 -05002215 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002216 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2217 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002218 static int zoomFactor = 8;
2219 if (ImGui::Button("<<")) {
2220 zoomFactor = SkTMax(zoomFactor / 2, 4);
2221 }
2222 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2223 if (ImGui::Button(">>")) {
2224 zoomFactor = SkTMin(zoomFactor * 2, 32);
2225 }
Brian Osmanf6877092017-02-13 09:39:57 -05002226
Ben Wagner3627d2e2018-06-26 14:23:20 -04002227 if (!fZoomWindowFixed) {
2228 ImVec2 mousePos = ImGui::GetMousePos();
2229 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2230 }
2231 SkScalar x = fZoomWindowLocation.x();
2232 SkScalar y = fZoomWindowLocation.y();
2233 int xInt = SkScalarRoundToInt(x);
2234 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002235 ImVec2 avail = ImGui::GetContentRegionAvail();
2236
Brian Osmanead517d2017-11-13 15:36:36 -05002237 uint32_t pixel = 0;
2238 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002239 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002240 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002241 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002242 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002243 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002244 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2245 }
2246
Brian Osmand67e5182017-12-08 16:46:09 -05002247 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002248 // Translate so the region of the image that's under the mouse cursor is centered
2249 // in the zoom canvas:
2250 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002251 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2252 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002253 c->drawImage(this->fLastImage, 0, 0);
2254
2255 SkPaint outline;
2256 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002257 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002258 });
Brian Osmanf6877092017-02-13 09:39:57 -05002259 }
2260
2261 ImGui::End();
2262 }
Brian Osman79086b92017-02-10 13:36:16 -05002263}
2264
liyuqian2edb0f42016-07-06 14:11:32 -07002265void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002266 for (int i = 0; i < fDeferredActions.count(); ++i) {
2267 fDeferredActions[i]();
2268 }
2269 fDeferredActions.reset();
2270
Brian Osman56a24812017-12-19 11:15:16 -05002271 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002272 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002273 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002274 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002275
Brian Osman79086b92017-02-10 13:36:16 -05002276 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002277 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2278 // not be visible, though. So we need to redraw if there is at least one visible window, or
2279 // more than one active window. Newly created windows are active but not visible for one frame
2280 // while they determine their layout and sizing.
2281 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2282 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002283 fWindow->inval();
2284 }
jvanverth9f372462016-04-06 06:08:59 -07002285}
liyuqiane5a6cd92016-05-27 08:52:52 -07002286
Florin Malitab632df72018-06-18 21:23:06 -04002287template <typename OptionsFunc>
2288static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2289 OptionsFunc&& optionsFunc) {
2290 writer.beginObject();
2291 {
2292 writer.appendString(kName , name);
2293 writer.appendString(kValue, value);
2294
2295 writer.beginArray(kOptions);
2296 {
2297 optionsFunc(writer);
2298 }
2299 writer.endArray();
2300 }
2301 writer.endObject();
2302}
2303
2304
liyuqiane5a6cd92016-05-27 08:52:52 -07002305void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002306 if (!fWindow) {
2307 return;
2308 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002309 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002310 return; // Surface hasn't been created yet.
2311 }
2312
Florin Malitab632df72018-06-18 21:23:06 -04002313 SkDynamicMemoryWStream memStream;
2314 SkJSONWriter writer(&memStream);
2315 writer.beginArray();
2316
liyuqianb73c24b2016-06-03 08:47:23 -07002317 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002318 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2319 [this](SkJSONWriter& writer) {
2320 for(const auto& slide : fSlides) {
2321 writer.appendString(slide->getName().c_str());
2322 }
2323 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002324
liyuqianb73c24b2016-06-03 08:47:23 -07002325 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002326 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2327 [](SkJSONWriter& writer) {
2328 for (const auto& str : kBackendTypeStrings) {
2329 writer.appendString(str);
2330 }
2331 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002332
csmartdalton578f0642017-02-24 16:04:47 -07002333 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002334 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2335 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2336 [this](SkJSONWriter& writer) {
2337 writer.appendS32(0);
2338
2339 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2340 return;
2341 }
2342
2343 for (int msaa : {4, 8, 16}) {
2344 writer.appendS32(msaa);
2345 }
2346 });
csmartdalton578f0642017-02-24 16:04:47 -07002347
csmartdalton61cd31a2017-02-27 17:00:53 -07002348 // Path renderer state
2349 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002350 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2351 [this](SkJSONWriter& writer) {
2352 const GrContext* ctx = fWindow->getGrContext();
2353 if (!ctx) {
2354 writer.appendString("Software");
2355 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002356 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002357
Florin Malitab632df72018-06-18 21:23:06 -04002358 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2359 if (fWindow->sampleCount() > 1) {
2360 if (caps->shaderCaps()->pathRenderingSupport()) {
2361 writer.appendString(
2362 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2363 }
2364 } else {
2365 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2366 writer.appendString(
2367 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2368 }
2369 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2370 }
2371 writer.appendString(
2372 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2373 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2374 }
2375 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002376
liyuqianb73c24b2016-06-03 08:47:23 -07002377 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002378 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2379 [this](SkJSONWriter& writer) {
2380 writer.appendString(kSoftkeyHint);
2381 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2382 writer.appendString(softkey.c_str());
2383 }
2384 });
liyuqianb73c24b2016-06-03 08:47:23 -07002385
Florin Malitab632df72018-06-18 21:23:06 -04002386 writer.endArray();
2387 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002388
Florin Malitab632df72018-06-18 21:23:06 -04002389 auto data = memStream.detachAsData();
2390
2391 // TODO: would be cool to avoid this copy
2392 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2393
2394 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002395}
2396
2397void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002398 // For those who will add more features to handle the state change in this function:
2399 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2400 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2401 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002402 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002403 for (int i = 0; i < fSlides.count(); ++i) {
2404 if (fSlides[i]->getName().equals(stateValue)) {
2405 this->setCurrentSlide(i);
2406 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002407 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002408 }
Florin Malitaab99c342018-01-16 16:23:03 -05002409
2410 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002411 } else if (stateName.equals(kBackendStateName)) {
2412 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2413 if (stateValue.equals(kBackendTypeStrings[i])) {
2414 if (fBackendType != i) {
2415 fBackendType = (sk_app::Window::BackendType)i;
2416 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002417 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002418 }
2419 break;
2420 }
2421 }
csmartdalton578f0642017-02-24 16:04:47 -07002422 } else if (stateName.equals(kMSAAStateName)) {
2423 DisplayParams params = fWindow->getRequestedDisplayParams();
2424 int sampleCount = atoi(stateValue.c_str());
2425 if (sampleCount != params.fMSAASampleCount) {
2426 params.fMSAASampleCount = sampleCount;
2427 fWindow->setRequestedDisplayParams(params);
2428 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002429 this->updateTitle();
2430 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002431 }
2432 } else if (stateName.equals(kPathRendererStateName)) {
2433 DisplayParams params = fWindow->getRequestedDisplayParams();
2434 for (const auto& pair : gPathRendererNames) {
2435 if (pair.second == stateValue.c_str()) {
2436 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2437 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2438 fWindow->setRequestedDisplayParams(params);
2439 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002440 this->updateTitle();
2441 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002442 }
2443 break;
2444 }
csmartdalton578f0642017-02-24 16:04:47 -07002445 }
liyuqianb73c24b2016-06-03 08:47:23 -07002446 } else if (stateName.equals(kSoftkeyStateName)) {
2447 if (!stateValue.equals(kSoftkeyHint)) {
2448 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002449 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002450 }
liyuqian2edb0f42016-07-06 14:11:32 -07002451 } else if (stateName.equals(kRefreshStateName)) {
2452 // This state is actually NOT in the UI state.
2453 // We use this to allow Android to quickly set bool fRefresh.
2454 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002455 } else {
2456 SkDebugf("Unknown stateName: %s", stateName.c_str());
2457 }
2458}
Brian Osman79086b92017-02-10 13:36:16 -05002459
Hal Canaryb1f411a2019-08-29 10:39:22 -04002460bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002461 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002462}
2463
Hal Canaryb1f411a2019-08-29 10:39:22 -04002464bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002465 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002466 fWindow->inval();
2467 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002468 } else {
2469 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002470 }
Brian Osman79086b92017-02-10 13:36:16 -05002471}