blob: f02f5b95c03b4d46399703134fdc82e570cbf353 [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"
20#include "src/core/SkMakeUnique.h"
21#include "src/core/SkOSFile.h"
22#include "src/core/SkScan.h"
23#include "src/core/SkTaskGroup.h"
24#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrGpu.h"
26#include "src/gpu/GrPersistentCacheUtils.h"
27#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
28#include "src/utils/SkJSONWriter.h"
29#include "src/utils/SkOSPath.h"
30#include "tools/Resources.h"
31#include "tools/ToolUtils.h"
32#include "tools/flags/CommandLineFlags.h"
33#include "tools/flags/CommonFlags.h"
34#include "tools/trace/EventTracingPriv.h"
35#include "tools/viewer/BisectSlide.h"
36#include "tools/viewer/GMSlide.h"
37#include "tools/viewer/ImageSlide.h"
38#include "tools/viewer/ParticlesSlide.h"
39#include "tools/viewer/SKPSlide.h"
40#include "tools/viewer/SampleSlide.h"
41#include "tools/viewer/SlideDir.h"
42#include "tools/viewer/SvgSlide.h"
43#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070044
Hal Canaryc640d0d2018-06-13 09:59:02 -040045#include <stdlib.h>
46#include <map>
47
Hal Canary8a001442018-09-19 11:31:27 -040048#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040049#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040050
Florin Malita87ccf332018-05-04 12:23:24 -040051#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040053#endif
54
Brian Osman5e7fbfd2019-05-03 13:13:35 -040055class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
56public:
57 void compileError(const char* shader, const char* errors) override {
58 fShaders.push_back(SkString(shader));
59 fErrors.push_back(SkString(errors));
60 }
61
62 void reset() {
63 fShaders.reset();
64 fErrors.reset();
65 }
66
67 SkTArray<SkString> fShaders;
68 SkTArray<SkString> fErrors;
69};
70
71static CapturingShaderErrorHandler gShaderErrorHandler;
72
jvanverth34524262016-05-04 13:49:13 -070073using namespace sk_app;
74
csmartdalton61cd31a2017-02-27 17:00:53 -070075static std::map<GpuPathRenderers, std::string> gPathRendererNames;
76
jvanverth9f372462016-04-06 06:08:59 -070077Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070078 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070079}
80
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060081static DEFINE_string(slide, "", "Start on this sample.");
82static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050083
Stephen Whitea800ec92019-08-02 15:04:52 -040084#if defined(SK_VULKAN)
jvanverthb8794cc2016-07-27 14:29:18 -070085# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050086#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
87# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
Stephen Whitea800ec92019-08-02 15:04:52 -040088#elif defined(SK_DAWN)
89# define BACKENDS_STR "\"sw\", \"gl\", and \"dawn\""
bsalomon6c471f72016-07-26 12:56:32 -070090#else
91# define BACKENDS_STR "\"sw\" and \"gl\""
92#endif
93
Brian Osman2dd96932016-10-18 15:33:53 -040094static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070095
Mike Klein5b3f3432019-03-21 11:42:21 -050096static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070097
Chris Dalton1e6c5b82019-06-17 14:16:49 -060098static DEFINE_int(internalSamples, 4,
99 "Number of samples for internal draws that use MSAA or mixed samples.");
100
Mike Klein84836b72019-03-21 11:31:36 -0500101static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700102
Mike Klein84836b72019-03-21 11:31:36 -0500103static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400104
Mike Kleinc6142d82019-03-25 10:54:59 -0500105static DEFINE_string2(match, m, nullptr,
106 "[~][^]substring[$] [...] of name to run.\n"
107 "Multiple matches may be separated by spaces.\n"
108 "~ causes a matching name to always be skipped\n"
109 "^ requires the start of the name to match\n"
110 "$ requires the end of the name to match\n"
111 "^ and $ requires an exact match\n"
112 "If a name does not match any list entry,\n"
113 "it is skipped unless some list entry starts with ~");
114
Mike Klein19fb3972019-03-21 13:08:08 -0500115#if defined(SK_BUILD_FOR_ANDROID)
116 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500117 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
118 static DEFINE_string(lotties, "/data/local/tmp/lotties",
119 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500120#else
121 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500122 static DEFINE_string(skps, "skps", "Directory to read skps from.");
123 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500124#endif
125
Mike Kleinc6142d82019-03-25 10:54:59 -0500126static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
127
128static DEFINE_int_2(threads, j, -1,
129 "Run threadsafe tests on a threadpool with this many extra threads, "
130 "defaulting to one extra thread per core.");
131
132
Brian Salomon194db172017-08-17 14:37:06 -0400133const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700134 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400135#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
136 "ANGLE",
137#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400138#ifdef SK_DAWN
139 "Dawn",
140#endif
jvanverth063ece72016-06-17 09:29:14 -0700141#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700142 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700143#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400144#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500145 "Metal",
146#endif
csmartdalton578f0642017-02-24 16:04:47 -0700147 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700148};
149
bsalomon6c471f72016-07-26 12:56:32 -0700150static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400151#ifdef SK_DAWN
152 if (0 == strcmp(str, "dawn")) {
153 return sk_app::Window::kDawn_BackendType;
154 } else
155#endif
bsalomon6c471f72016-07-26 12:56:32 -0700156#ifdef SK_VULKAN
157 if (0 == strcmp(str, "vk")) {
158 return sk_app::Window::kVulkan_BackendType;
159 } else
160#endif
Brian Salomon194db172017-08-17 14:37:06 -0400161#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
162 if (0 == strcmp(str, "angle")) {
163 return sk_app::Window::kANGLE_BackendType;
164 } else
165#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400166#ifdef SK_METAL
167 if (0 == strcmp(str, "mtl")) {
168 return sk_app::Window::kMetal_BackendType;
169 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500170#endif
bsalomon6c471f72016-07-26 12:56:32 -0700171 if (0 == strcmp(str, "gl")) {
172 return sk_app::Window::kNativeGL_BackendType;
173 } else if (0 == strcmp(str, "sw")) {
174 return sk_app::Window::kRaster_BackendType;
175 } else {
176 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
177 return sk_app::Window::kRaster_BackendType;
178 }
179}
180
Brian Osmana109e392017-02-24 09:49:14 -0500181static SkColorSpacePrimaries gSrgbPrimaries = {
182 0.64f, 0.33f,
183 0.30f, 0.60f,
184 0.15f, 0.06f,
185 0.3127f, 0.3290f };
186
187static SkColorSpacePrimaries gAdobePrimaries = {
188 0.64f, 0.33f,
189 0.21f, 0.71f,
190 0.15f, 0.06f,
191 0.3127f, 0.3290f };
192
193static SkColorSpacePrimaries gP3Primaries = {
194 0.680f, 0.320f,
195 0.265f, 0.690f,
196 0.150f, 0.060f,
197 0.3127f, 0.3290f };
198
199static SkColorSpacePrimaries gRec2020Primaries = {
200 0.708f, 0.292f,
201 0.170f, 0.797f,
202 0.131f, 0.046f,
203 0.3127f, 0.3290f };
204
205struct NamedPrimaries {
206 const char* fName;
207 SkColorSpacePrimaries* fPrimaries;
208} gNamedPrimaries[] = {
209 { "sRGB", &gSrgbPrimaries },
210 { "AdobeRGB", &gAdobePrimaries },
211 { "P3", &gP3Primaries },
212 { "Rec. 2020", &gRec2020Primaries },
213};
214
215static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
216 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
217}
218
Brian Osman70d2f432017-11-08 09:54:10 -0500219static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
220 // In raster mode, we still use GL for the window.
221 // This lets us render the GUI faster (and correct).
222 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
223}
224
Jim Van Verth74826c82019-03-01 14:37:30 -0500225class NullSlide : public Slide {
226 SkISize getDimensions() const override {
227 return SkISize::Make(640, 480);
228 }
229
230 void draw(SkCanvas* canvas) override {
231 canvas->clear(0xffff11ff);
232 }
233};
234
liyuqiane5a6cd92016-05-27 08:52:52 -0700235const char* kName = "name";
236const char* kValue = "value";
237const char* kOptions = "options";
238const char* kSlideStateName = "Slide";
239const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700240const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700241const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700242const char* kSoftkeyStateName = "Softkey";
243const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700244const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700245const char* kON = "ON";
246const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700247const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700248
jvanverth34524262016-05-04 13:49:13 -0700249Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500250 : fCurrentSlide(-1)
251 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500252 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400253 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500254 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500255 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500256 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500257 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400258 , fZoomWindowFixed(false)
259 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500260 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400261 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700262 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500263 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500264 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500265 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500266 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700267 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400268 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400269 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400270 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500271 , fTiled(false)
272 , fDrawTileBoundaries(false)
273 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400274 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700275{
Greg Daniel285db442016-10-14 09:12:53 -0400276 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700277
Brian Osmanf09e35e2017-12-15 14:48:09 -0500278 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500279 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500280 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600281 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500282 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
283 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700284
jvanverth2bb3b6d2016-04-08 07:24:09 -0700285 SkDebugf("Command line arguments: ");
286 for (int i = 1; i < argc; ++i) {
287 SkDebugf("%s ", argv[i]);
288 }
289 SkDebugf("\n");
290
Mike Klein88544fb2019-03-20 10:50:33 -0500291 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500292#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400293 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500294#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700295
Mike Klein19cc0f62019-03-22 15:30:07 -0500296 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500297
Brian Osmanbc8150f2017-07-24 11:38:01 -0400298 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400299 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400300
bsalomon6c471f72016-07-26 12:56:32 -0700301 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700302 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700303
csmartdalton578f0642017-02-24 16:04:47 -0700304 DisplayParams displayParams;
305 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700306 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400307 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400308 displayParams.fGrContextOptions.fShaderCacheStrategy =
309 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400310 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
311 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltona1638a52019-06-24 11:54:24 -0600312 displayParams.fGrContextOptions.fInternalMultisampleCount = FLAGS_internalSamples;
csmartdalton578f0642017-02-24 16:04:47 -0700313 fWindow->setRequestedDisplayParams(displayParams);
314
Brian Osman56a24812017-12-19 11:15:16 -0500315 // Configure timers
316 fStatsLayer.setActive(false);
317 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
318 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
319 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
320
jvanverth9f372462016-04-06 06:08:59 -0700321 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700322 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500323 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500324 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500325 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700326
brianosman622c8d52016-05-10 06:50:49 -0700327 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500328 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
329 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
330 fWindow->inval();
331 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500332 // Command to jump directly to the slide picker and give it focus
333 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
334 this->fShowImGuiDebugWindow = true;
335 this->fShowSlidePicker = true;
336 fWindow->inval();
337 });
338 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400339 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500340 this->fShowImGuiDebugWindow = true;
341 this->fShowSlidePicker = true;
342 fWindow->inval();
343 });
Brian Osman79086b92017-02-10 13:36:16 -0500344 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
345 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
346 fWindow->inval();
347 });
Brian Osmanf6877092017-02-13 09:39:57 -0500348 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
349 this->fShowZoomWindow = !this->fShowZoomWindow;
350 fWindow->inval();
351 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400352 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
353 this->fZoomWindowFixed = !this->fZoomWindowFixed;
354 fWindow->inval();
355 });
Greg Danield0794cc2019-03-27 16:23:26 -0400356 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
357 DisplayParams params = fWindow->getRequestedDisplayParams();
358 params.fDisableVsync = !params.fDisableVsync;
359 fWindow->setRequestedDisplayParams(params);
360 this->updateTitle();
361 fWindow->inval();
362 });
Mike Reedf702ed42019-07-22 17:00:49 -0400363 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
364 fRefresh = !fRefresh;
365 fWindow->inval();
366 });
brianosman622c8d52016-05-10 06:50:49 -0700367 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500368 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700369 fWindow->inval();
370 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400371 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500372 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400373 this->updateTitle();
374 fWindow->inval();
375 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500376 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500377 switch (fColorMode) {
378 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500379 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500380 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500381 case ColorMode::kColorManaged8888:
382 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500383 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500384 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400385 this->setColorMode(ColorMode::kColorManagedF16Norm);
386 break;
387 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500388 this->setColorMode(ColorMode::kLegacy);
389 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500390 }
brianosman622c8d52016-05-10 06:50:49 -0700391 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400392 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500393 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700394 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400395 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500396 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700397 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400398 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700399 this->changeZoomLevel(1.f / 32.f);
400 fWindow->inval();
401 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400402 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700403 this->changeZoomLevel(-1.f / 32.f);
404 fWindow->inval();
405 });
jvanverthaf236b52016-05-20 06:01:06 -0700406 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400407 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
408 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500409 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400410#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
411 if (newBackend == sk_app::Window::kVulkan_BackendType) {
412 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
413 sk_app::Window::kBackendTypeCount);
414 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
415 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500416 }
417#endif
Brian Osman621491e2017-02-28 15:45:01 -0500418 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700419 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500420 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
421 fSaveToSKP = true;
422 fWindow->inval();
423 });
Mike Reed376d8122019-03-14 11:39:02 -0400424 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
425 fShowSlideDimensions = !fShowSlideDimensions;
426 fWindow->inval();
427 });
Ben Wagner37c54032018-04-13 14:30:23 -0400428 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
429 DisplayParams params = fWindow->getRequestedDisplayParams();
430 uint32_t flags = params.fSurfaceProps.flags();
431 if (!fPixelGeometryOverrides) {
432 fPixelGeometryOverrides = true;
433 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
434 } else {
435 switch (params.fSurfaceProps.pixelGeometry()) {
436 case kUnknown_SkPixelGeometry:
437 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
438 break;
439 case kRGB_H_SkPixelGeometry:
440 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
441 break;
442 case kBGR_H_SkPixelGeometry:
443 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
444 break;
445 case kRGB_V_SkPixelGeometry:
446 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
447 break;
448 case kBGR_V_SkPixelGeometry:
449 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
450 fPixelGeometryOverrides = false;
451 break;
452 }
453 }
454 fWindow->setRequestedDisplayParams(params);
455 this->updateTitle();
456 fWindow->inval();
457 });
Ben Wagner9613e452019-01-23 10:34:59 -0500458 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500459 if (!fFontOverrides.fHinting) {
460 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400461 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500462 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500463 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400464 case SkFontHinting::kNone:
465 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500466 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400467 case SkFontHinting::kSlight:
468 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500469 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400470 case SkFontHinting::kNormal:
471 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500472 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400473 case SkFontHinting::kFull:
474 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500475 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500476 break;
477 }
478 }
479 this->updateTitle();
480 fWindow->inval();
481 });
482 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500483 if (!fPaintOverrides.fAntiAlias) {
484 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
485 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500486 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500487 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500488 } else {
489 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500490 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500491 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500492 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400493 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500494 break;
495 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500496 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500497 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400498 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500499 break;
500 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500501 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400502 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500503 break;
504 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500505 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
506 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500507 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
508 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500509 break;
510 }
511 }
512 this->updateTitle();
513 fWindow->inval();
514 });
Ben Wagner37c54032018-04-13 14:30:23 -0400515 fCommands.addCommand('D', "Modes", "DFT", [this]() {
516 DisplayParams params = fWindow->getRequestedDisplayParams();
517 uint32_t flags = params.fSurfaceProps.flags();
518 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
519 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
520 fWindow->setRequestedDisplayParams(params);
521 this->updateTitle();
522 fWindow->inval();
523 });
Ben Wagner9613e452019-01-23 10:34:59 -0500524 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500525 if (!fFontOverrides.fEdging) {
526 fFontOverrides.fEdging = true;
527 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500528 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500529 switch (fFont.getEdging()) {
530 case SkFont::Edging::kAlias:
531 fFont.setEdging(SkFont::Edging::kAntiAlias);
532 break;
533 case SkFont::Edging::kAntiAlias:
534 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
535 break;
536 case SkFont::Edging::kSubpixelAntiAlias:
537 fFont.setEdging(SkFont::Edging::kAlias);
538 fFontOverrides.fEdging = false;
539 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500540 }
541 }
542 this->updateTitle();
543 fWindow->inval();
544 });
Ben Wagner9613e452019-01-23 10:34:59 -0500545 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500546 if (!fFontOverrides.fSubpixel) {
547 fFontOverrides.fSubpixel = true;
548 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500549 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500550 if (!fFont.isSubpixel()) {
551 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500552 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500553 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500554 }
555 }
556 this->updateTitle();
557 fWindow->inval();
558 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400559 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
560 if (!fFontOverrides.fBaselineSnap) {
561 fFontOverrides.fBaselineSnap = true;
562 fFont.setBaselineSnap(false);
563 } else {
564 if (!fFont.isBaselineSnap()) {
565 fFont.setBaselineSnap(true);
566 } else {
567 fFontOverrides.fBaselineSnap = false;
568 }
569 }
570 this->updateTitle();
571 fWindow->inval();
572 });
Brian Osman805a7272018-05-02 15:40:20 -0400573 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
574 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
575 : kPerspective_Real;
576 this->updateTitle();
577 fWindow->inval();
578 });
579 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
580 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
581 : kPerspective_Off;
582 this->updateTitle();
583 fWindow->inval();
584 });
Brian Osman207d4102019-01-10 09:40:58 -0500585 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
586 fAnimTimer.togglePauseResume();
587 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400588 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
589 fZoomUI = !fZoomUI;
590 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
591 fWindow->inval();
592 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500593
jvanverth2bb3b6d2016-04-08 07:24:09 -0700594 // set up slides
595 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500596 if (FLAGS_list) {
597 this->listNames();
598 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700599
Brian Osman9bb47cf2018-04-26 15:55:00 -0400600 fPerspectivePoints[0].set(0, 0);
601 fPerspectivePoints[1].set(1, 0);
602 fPerspectivePoints[2].set(0, 1);
603 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700604 fAnimTimer.run();
605
Hal Canaryc465d132017-12-08 10:21:31 -0500606 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500607 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400608 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500609 }
610 fImGuiGamutPaint.setColor(SK_ColorWHITE);
611 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
612
jongdeok.kim804f17e2019-02-26 14:39:23 +0900613 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500614 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700615}
616
jvanverth34524262016-05-04 13:49:13 -0700617void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400618 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
619 static const struct {
620 const char* fExtension;
621 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500622 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400623 const SlideFactory fFactory;
624 } gExternalSlidesInfo[] = {
625 { ".skp", "skp-dir", FLAGS_skps,
626 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
627 return sk_make_sp<SKPSlide>(name, path);}
628 },
629 { ".jpg", "jpg-dir", FLAGS_jpgs,
630 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
631 return sk_make_sp<ImageSlide>(name, path);}
632 },
Florin Malita87ccf332018-05-04 12:23:24 -0400633#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400634 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400635 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
636 return sk_make_sp<SkottieSlide>(name, path);}
637 },
Florin Malita87ccf332018-05-04 12:23:24 -0400638#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400639#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400640 { ".svg", "svg-dir", FLAGS_svgs,
641 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
642 return sk_make_sp<SvgSlide>(name, path);}
643 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400644#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400645 };
jvanverthc265a922016-04-08 12:51:45 -0700646
Brian Salomon343553a2018-09-05 15:41:23 -0400647 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700648
Mike Klein88544fb2019-03-20 10:50:33 -0500649 const auto addSlide =
650 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
651 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
652 return;
653 }
liyuqian6f163d22016-06-13 12:26:45 -0700654
Mike Klein88544fb2019-03-20 10:50:33 -0500655 if (auto slide = fact(name, path)) {
656 dirSlides.push_back(slide);
657 fSlides.push_back(std::move(slide));
658 }
659 };
Florin Malita76a076b2018-02-15 18:40:48 -0500660
Florin Malita38792ce2018-05-08 10:36:18 -0400661 if (!FLAGS_file.isEmpty()) {
662 // single file mode
663 const SkString file(FLAGS_file[0]);
664
665 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
666 for (const auto& sinfo : gExternalSlidesInfo) {
667 if (file.endsWith(sinfo.fExtension)) {
668 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
669 return;
670 }
671 }
672
673 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
674 } else {
675 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
676 }
677
678 return;
679 }
680
681 // Bisect slide.
682 if (!FLAGS_bisect.isEmpty()) {
683 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500684 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400685 if (FLAGS_bisect.count() >= 2) {
686 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
687 bisect->onChar(*ch);
688 }
689 }
690 fSlides.push_back(std::move(bisect));
691 }
692 }
693
694 // GMs
695 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400696 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400697 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500698 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400699 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400700 fSlides.push_back(std::move(slide));
701 }
Florin Malita38792ce2018-05-08 10:36:18 -0400702 }
703 // reverse gms
704 int numGMs = fSlides.count() - firstGM;
705 for (int i = 0; i < numGMs/2; ++i) {
706 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
707 }
708
709 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400710 for (const SampleFactory factory : SampleRegistry::Range()) {
711 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500712 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400713 fSlides.push_back(slide);
714 }
Florin Malita38792ce2018-05-08 10:36:18 -0400715 }
716
Brian Osman7c979f52019-02-12 13:27:51 -0500717 // Particle demo
718 {
719 // TODO: Convert this to a sample
720 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500721 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500722 fSlides.push_back(std::move(slide));
723 }
724 }
725
Florin Malita0ffa3222018-04-05 14:34:45 -0400726 for (const auto& info : gExternalSlidesInfo) {
727 for (const auto& flag : info.fFlags) {
728 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
729 // single file
730 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
731 } else {
732 // directory
733 SkOSFile::Iter it(flag.c_str(), info.fExtension);
734 SkString name;
735 while (it.next(&name)) {
736 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
737 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400738 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400739 if (!dirSlides.empty()) {
740 fSlides.push_back(
741 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
742 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500743 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400744 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400745 }
746 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500747
748 if (!fSlides.count()) {
749 sk_sp<Slide> slide(new NullSlide());
750 fSlides.push_back(std::move(slide));
751 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700752}
753
754
jvanverth34524262016-05-04 13:49:13 -0700755Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700756 fWindow->detach();
757 delete fWindow;
758}
759
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500760struct SkPaintTitleUpdater {
761 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
762 void append(const char* s) {
763 if (fCount == 0) {
764 fTitle->append(" {");
765 } else {
766 fTitle->append(", ");
767 }
768 fTitle->append(s);
769 ++fCount;
770 }
771 void done() {
772 if (fCount > 0) {
773 fTitle->append("}");
774 }
775 }
776 SkString* fTitle;
777 int fCount;
778};
779
brianosman05de2162016-05-06 13:28:57 -0700780void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700781 if (!fWindow) {
782 return;
783 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500784 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700785 return; // Surface hasn't been created yet.
786 }
787
jvanverth34524262016-05-04 13:49:13 -0700788 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700789 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700790
Mike Kleine5acd752019-03-22 09:57:16 -0500791 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400792 if (gSkForceAnalyticAA) {
793 title.append(" <FAAA>");
794 } else {
795 title.append(" <AAA>");
796 }
797 }
798
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500799 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500800 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
801 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400802 const char* on, const char* off)
803 {
Ben Wagner9613e452019-01-23 10:34:59 -0500804 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400805 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500806 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400807 };
808
Ben Wagner9613e452019-01-23 10:34:59 -0500809 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
810 const char* on, const char* off)
811 {
812 if (fFontOverrides.*flag) {
813 paintTitle.append((fFont.*isFlag)() ? on : off);
814 }
815 };
816
817 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
818 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500819 if (fPaintOverrides.fFilterQuality) {
820 switch (fPaint.getFilterQuality()) {
821 case kNone_SkFilterQuality:
822 paintTitle.append("NoFilter");
823 break;
824 case kLow_SkFilterQuality:
825 paintTitle.append("LowFilter");
826 break;
827 case kMedium_SkFilterQuality:
828 paintTitle.append("MediumFilter");
829 break;
830 case kHigh_SkFilterQuality:
831 paintTitle.append("HighFilter");
832 break;
833 }
834 }
Ben Wagner9613e452019-01-23 10:34:59 -0500835
836 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
837 "Force Autohint", "No Force Autohint");
838 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400839 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500840 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
841 "Linear Metrics", "Non-Linear Metrics");
842 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
843 "Bitmap Text", "No Bitmap Text");
844 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
845
846 if (fFontOverrides.fEdging) {
847 switch (fFont.getEdging()) {
848 case SkFont::Edging::kAlias:
849 paintTitle.append("Alias Text");
850 break;
851 case SkFont::Edging::kAntiAlias:
852 paintTitle.append("Antialias Text");
853 break;
854 case SkFont::Edging::kSubpixelAntiAlias:
855 paintTitle.append("Subpixel Antialias Text");
856 break;
857 }
858 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400859
Mike Reed3ae47332019-01-04 10:11:46 -0500860 if (fFontOverrides.fHinting) {
861 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400862 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500863 paintTitle.append("No Hinting");
864 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400865 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500866 paintTitle.append("Slight Hinting");
867 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400868 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500869 paintTitle.append("Normal Hinting");
870 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400871 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500872 paintTitle.append("Full Hinting");
873 break;
874 }
875 }
876 paintTitle.done();
877
Brian Osman92004802017-03-06 11:47:26 -0500878 switch (fColorMode) {
879 case ColorMode::kLegacy:
880 title.append(" Legacy 8888");
881 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500882 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500883 title.append(" ColorManaged 8888");
884 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500885 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500886 title.append(" ColorManaged F16");
887 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400888 case ColorMode::kColorManagedF16Norm:
889 title.append(" ColorManaged F16 Norm");
890 break;
Brian Osman92004802017-03-06 11:47:26 -0500891 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500892
Brian Osman92004802017-03-06 11:47:26 -0500893 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500894 int curPrimaries = -1;
895 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
896 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
897 curPrimaries = i;
898 break;
899 }
900 }
Brian Osman03115dc2018-11-26 13:55:19 -0500901 title.appendf(" %s Gamma %f",
902 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500903 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700904 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500905
Ben Wagner37c54032018-04-13 14:30:23 -0400906 const DisplayParams& params = fWindow->getRequestedDisplayParams();
907 if (fPixelGeometryOverrides) {
908 switch (params.fSurfaceProps.pixelGeometry()) {
909 case kUnknown_SkPixelGeometry:
910 title.append( " Flat");
911 break;
912 case kRGB_H_SkPixelGeometry:
913 title.append( " RGB");
914 break;
915 case kBGR_H_SkPixelGeometry:
916 title.append( " BGR");
917 break;
918 case kRGB_V_SkPixelGeometry:
919 title.append( " RGBV");
920 break;
921 case kBGR_V_SkPixelGeometry:
922 title.append( " BGRV");
923 break;
924 }
925 }
926
927 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
928 title.append(" DFT");
929 }
930
csmartdalton578f0642017-02-24 16:04:47 -0700931 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700932 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500933 int msaa = fWindow->sampleCount();
934 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700935 title.appendf(" MSAA: %i", msaa);
936 }
937 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700938
939 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600940 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700941 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
942 }
943
Brian Osman805a7272018-05-02 15:40:20 -0400944 if (kPerspective_Real == fPerspectiveMode) {
945 title.append(" Perpsective (Real)");
946 } else if (kPerspective_Fake == fPerspectiveMode) {
947 title.append(" Perspective (Fake)");
948 }
949
brianosman05de2162016-05-06 13:28:57 -0700950 fWindow->setTitle(title.c_str());
951}
952
Florin Malitaab99c342018-01-16 16:23:03 -0500953int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500954
955 if (!FLAGS_slide.isEmpty()) {
956 int count = fSlides.count();
957 for (int i = 0; i < count; i++) {
958 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500959 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500960 }
961 }
962
963 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
964 this->listNames();
965 }
966
Florin Malitaab99c342018-01-16 16:23:03 -0500967 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500968}
969
Florin Malitaab99c342018-01-16 16:23:03 -0500970void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500971 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500972 for (const auto& slide : fSlides) {
973 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500974 }
975}
976
Florin Malitaab99c342018-01-16 16:23:03 -0500977void Viewer::setCurrentSlide(int slide) {
978 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700979
Florin Malitaab99c342018-01-16 16:23:03 -0500980 if (slide == fCurrentSlide) {
981 return;
982 }
983
984 if (fCurrentSlide >= 0) {
985 fSlides[fCurrentSlide]->unload();
986 }
987
988 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
989 SkIntToScalar(fWindow->height()));
990 fCurrentSlide = slide;
991 this->setupCurrentSlide();
992}
993
994void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500995 if (fCurrentSlide >= 0) {
996 // prepare dimensions for image slides
997 fGesture.resetTouchState();
998 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700999
Jim Van Verth0848fb02018-01-22 13:39:30 -05001000 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1001 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1002 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001003
Jim Van Verth0848fb02018-01-22 13:39:30 -05001004 // Start with a matrix that scales the slide to the available screen space
1005 if (fWindow->scaleContentToFit()) {
1006 if (windowRect.width() > 0 && windowRect.height() > 0) {
1007 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1008 }
liyuqiane46e4f02016-05-20 07:32:19 -07001009 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001010
1011 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001012 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001013
1014 this->updateTitle();
1015 this->updateUIState();
1016
1017 fStatsLayer.resetMeasurements();
1018
1019 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001020 }
jvanverthc265a922016-04-08 12:51:45 -07001021}
1022
1023#define MAX_ZOOM_LEVEL 8
1024#define MIN_ZOOM_LEVEL -8
1025
jvanverth34524262016-05-04 13:49:13 -07001026void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001027 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -04001028 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001029 this->preTouchMatrixChanged();
1030}
Yuqian Li755778c2018-03-28 16:23:31 -04001031
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001032void Viewer::preTouchMatrixChanged() {
1033 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001034 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1035 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1036 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1037 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1038}
1039
Brian Osman805a7272018-05-02 15:40:20 -04001040SkMatrix Viewer::computePerspectiveMatrix() {
1041 SkScalar w = fWindow->width(), h = fWindow->height();
1042 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1043 SkPoint perspPts[4] = {
1044 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1045 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1046 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1047 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1048 };
1049 SkMatrix m;
1050 m.setPolyToPoly(orthoPts, perspPts, 4);
1051 return m;
1052}
1053
Yuqian Li755778c2018-03-28 16:23:31 -04001054SkMatrix Viewer::computePreTouchMatrix() {
1055 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001056
1057 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001058 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001059 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001060
1061 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1062 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001063
Brian Osman805a7272018-05-02 15:40:20 -04001064 if (kPerspective_Real == fPerspectiveMode) {
1065 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001066 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001067 }
1068
Yuqian Li755778c2018-03-28 16:23:31 -04001069 return m;
jvanverthc265a922016-04-08 12:51:45 -07001070}
1071
liyuqiand3cdbca2016-05-17 12:44:20 -07001072SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001073 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001074 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001075 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001076 return m;
jvanverthc265a922016-04-08 12:51:45 -07001077}
1078
Brian Osman621491e2017-02-28 15:45:01 -05001079void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001080 fPersistentCache.reset();
1081 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001082 fBackendType = backendType;
1083
1084 fWindow->detach();
1085
Brian Osman70d2f432017-11-08 09:54:10 -05001086#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001087 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1088 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001089 DisplayParams params = fWindow->getRequestedDisplayParams();
1090 delete fWindow;
1091 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001092
Brian Osman70d2f432017-11-08 09:54:10 -05001093 // re-register callbacks
1094 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001095 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001096 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001097 fWindow->pushLayer(&fImGuiLayer);
1098
Brian Osman70d2f432017-11-08 09:54:10 -05001099 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1100 // will still include our correct sample count. But the re-created fWindow will lose that
1101 // information. On Windows, we need to re-create the window when changing sample count,
1102 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1103 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1104 // as if we had never changed windows at all).
1105 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001106#endif
1107
Brian Osman70d2f432017-11-08 09:54:10 -05001108 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001109}
1110
Brian Osman92004802017-03-06 11:47:26 -05001111void Viewer::setColorMode(ColorMode colorMode) {
1112 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001113 this->updateTitle();
1114 fWindow->inval();
1115}
1116
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001117class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1118public:
Mike Reed3ae47332019-01-04 10:11:46 -05001119 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1120 SkFont* font, Viewer::SkFontFields* ffields)
1121 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001122 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001123 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1124 sk_sp<SkTextBlob>* cache) {
1125 bool blobWillChange = false;
1126 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001127 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1128 bool shouldDraw = this->filterFont(&filteredFont);
1129 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001130 blobWillChange = true;
1131 break;
1132 }
1133 }
1134 if (!blobWillChange) {
1135 return blob;
1136 }
1137
1138 SkTextBlobBuilder builder;
1139 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001140 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1141 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001142 if (!shouldDraw) {
1143 continue;
1144 }
1145
Mike Reed3ae47332019-01-04 10:11:46 -05001146 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001147
Ben Wagner41e40472018-09-24 13:01:54 -04001148 const SkTextBlobBuilder::RunBuffer& runBuffer
1149 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001150 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001151 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001152 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001153 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001154 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001155 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001156 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001157 it.glyphCount(), it.textSize(), SkString())
1158 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1159 uint32_t glyphCount = it.glyphCount();
1160 if (it.glyphs()) {
1161 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1162 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1163 }
1164 if (it.pos()) {
1165 size_t posSize = sizeof(decltype(*it.pos()));
1166 uint8_t positioning = it.positioning();
1167 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1168 }
1169 if (it.text()) {
1170 size_t textSize = sizeof(decltype(*it.text()));
1171 uint32_t textCount = it.textSize();
1172 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1173 }
1174 if (it.clusters()) {
1175 size_t clusterSize = sizeof(decltype(*it.clusters()));
1176 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1177 }
1178 }
1179 *cache = builder.make();
1180 return cache->get();
1181 }
1182 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1183 const SkPaint& paint) override {
1184 sk_sp<SkTextBlob> cache;
1185 this->SkPaintFilterCanvas::onDrawTextBlob(
1186 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1187 }
Mike Reed3ae47332019-01-04 10:11:46 -05001188 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001189 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001190 font->writable()->setSize(fFont->getSize());
1191 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001192 if (fFontOverrides->fScaleX) {
1193 font->writable()->setScaleX(fFont->getScaleX());
1194 }
1195 if (fFontOverrides->fSkewX) {
1196 font->writable()->setSkewX(fFont->getSkewX());
1197 }
Mike Reed3ae47332019-01-04 10:11:46 -05001198 if (fFontOverrides->fHinting) {
1199 font->writable()->setHinting(fFont->getHinting());
1200 }
Ben Wagner9613e452019-01-23 10:34:59 -05001201 if (fFontOverrides->fEdging) {
1202 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001203 }
Ben Wagner9613e452019-01-23 10:34:59 -05001204 if (fFontOverrides->fEmbolden) {
1205 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001206 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001207 if (fFontOverrides->fBaselineSnap) {
1208 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1209 }
Ben Wagner9613e452019-01-23 10:34:59 -05001210 if (fFontOverrides->fLinearMetrics) {
1211 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001212 }
Ben Wagner9613e452019-01-23 10:34:59 -05001213 if (fFontOverrides->fSubpixel) {
1214 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001215 }
Ben Wagner9613e452019-01-23 10:34:59 -05001216 if (fFontOverrides->fEmbeddedBitmaps) {
1217 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001218 }
Ben Wagner9613e452019-01-23 10:34:59 -05001219 if (fFontOverrides->fForceAutoHinting) {
1220 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001221 }
Ben Wagner9613e452019-01-23 10:34:59 -05001222
Mike Reed3ae47332019-01-04 10:11:46 -05001223 return true;
1224 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001225 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001226 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001227 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001228 }
Ben Wagner9613e452019-01-23 10:34:59 -05001229 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001230 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001231 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001232 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001233 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001234 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001235 return true;
1236 }
1237 SkPaint* fPaint;
1238 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001239 SkFont* fFont;
1240 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001241};
1242
Robert Phillips9882dae2019-03-04 11:00:10 -05001243void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001244 if (fCurrentSlide < 0) {
1245 return;
1246 }
1247
Robert Phillips9882dae2019-03-04 11:00:10 -05001248 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001249
Brian Osmanf750fbc2017-02-08 10:47:28 -05001250 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001251 SkSurface* slideSurface = surface;
1252 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001253 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001254
Brian Osmane0d4fba2017-03-15 10:24:55 -04001255 // 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 -05001256 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001257 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001258 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001259 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001260 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001261 }
1262
Brian Osman3ac99cf2017-12-01 11:23:53 -05001263 if (fSaveToSKP) {
1264 SkPictureRecorder recorder;
1265 SkCanvas* recorderCanvas = recorder.beginRecording(
1266 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001267 fSlides[fCurrentSlide]->draw(recorderCanvas);
1268 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1269 SkFILEWStream stream("sample_app.skp");
1270 picture->serialize(&stream);
1271 fSaveToSKP = false;
1272 }
1273
Brian Osmane9ed0f02018-11-26 14:50:05 -05001274 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001275 SkColorType colorType;
1276 switch (fColorMode) {
1277 case ColorMode::kLegacy:
1278 case ColorMode::kColorManaged8888:
1279 colorType = kN32_SkColorType;
1280 break;
1281 case ColorMode::kColorManagedF16:
1282 colorType = kRGBA_F16_SkColorType;
1283 break;
1284 case ColorMode::kColorManagedF16Norm:
1285 colorType = kRGBA_F16Norm_SkColorType;
1286 break;
1287 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001288
1289 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001290 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1291 slideCanvas->getProps(&props);
1292
Brian Osmane9ed0f02018-11-26 14:50:05 -05001293 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1294 return Window::kRaster_BackendType == this->fBackendType
1295 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001296 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001297 };
1298
Brian Osman03115dc2018-11-26 13:55:19 -05001299 // We need to render offscreen if we're...
1300 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1301 // ... in any raster mode, because the window surface is actually GL
1302 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001303 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001304 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001305 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001306 Window::kRaster_BackendType == fBackendType ||
1307 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001308
Brian Osmane9ed0f02018-11-26 14:50:05 -05001309 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001310 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001311 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001312 }
1313
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001314 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001315 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001316 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001317 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001318 if (fTiled) {
1319 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1320 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1321 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1322 SkCanvas* tileCanvas = tileSurface->getCanvas();
1323 SkMatrix m = this->computeMatrix();
1324 for (int y = 0; y < fWindow->height(); y += tileH) {
1325 for (int x = 0; x < fWindow->width(); x += tileW) {
1326 SkAutoCanvasRestore acr(tileCanvas, true);
1327 tileCanvas->translate(-x, -y);
1328 tileCanvas->clear(SK_ColorTRANSPARENT);
1329 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001330 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1331 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001332 fSlides[fCurrentSlide]->draw(&filterCanvas);
1333 tileSurface->draw(slideCanvas, x, y, nullptr);
1334 }
1335 }
1336
1337 // Draw borders between tiles
1338 if (fDrawTileBoundaries) {
1339 SkPaint border;
1340 border.setColor(0x60FF00FF);
1341 border.setStyle(SkPaint::kStroke_Style);
1342 for (int y = 0; y < fWindow->height(); y += tileH) {
1343 for (int x = 0; x < fWindow->width(); x += tileW) {
1344 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1345 }
1346 }
1347 }
1348 } else {
1349 slideCanvas->concat(this->computeMatrix());
1350 if (kPerspective_Real == fPerspectiveMode) {
1351 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1352 }
Mike Reed3ae47332019-01-04 10:11:46 -05001353 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001354 fSlides[fCurrentSlide]->draw(&filterCanvas);
1355 }
Brian Osman56a24812017-12-19 11:15:16 -05001356 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001357 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001358
1359 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001360 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001361 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001362 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001363
1364 // If we rendered offscreen, snap an image and push the results to the window's canvas
1365 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001366 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001367
Robert Phillips9882dae2019-03-04 11:00:10 -05001368 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001369 SkPaint paint;
1370 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001371 int prePerspectiveCount = canvas->save();
1372 if (kPerspective_Fake == fPerspectiveMode) {
1373 paint.setFilterQuality(kHigh_SkFilterQuality);
1374 canvas->clear(SK_ColorWHITE);
1375 canvas->concat(this->computePerspectiveMatrix());
1376 }
Brian Osman03115dc2018-11-26 13:55:19 -05001377 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001378 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001379 }
Mike Reed376d8122019-03-14 11:39:02 -04001380
1381 if (fShowSlideDimensions) {
1382 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1383 SkPaint paint;
1384 paint.setColor(0x40FFFF00);
1385 surface->getCanvas()->drawRect(r, paint);
1386 }
liyuqian6f163d22016-06-13 12:26:45 -07001387}
1388
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001389void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001390 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001391 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001392}
Jim Van Verth6f449692017-02-14 15:16:46 -05001393
Robert Phillips9882dae2019-03-04 11:00:10 -05001394void Viewer::onPaint(SkSurface* surface) {
1395 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001396
Robert Phillips9882dae2019-03-04 11:00:10 -05001397 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001398
Brian Osmand67e5182017-12-08 16:46:09 -05001399 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001400
1401 if (GrContext* ctx = fWindow->getGrContext()) {
1402 // Clean out cache items that haven't been used in more than 10 seconds.
1403 ctx->performDeferredCleanup(std::chrono::seconds(10));
1404 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001405}
1406
Ben Wagnera1915972018-08-09 15:06:19 -04001407void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001408 if (fCurrentSlide >= 0) {
1409 fSlides[fCurrentSlide]->resize(width, height);
1410 }
Ben Wagnera1915972018-08-09 15:06:19 -04001411}
1412
Florin Malitacefc1b92018-02-19 21:43:47 -05001413SkPoint Viewer::mapEvent(float x, float y) {
1414 const auto m = this->computeMatrix();
1415 SkMatrix inv;
1416
1417 SkAssertResult(m.invert(&inv));
1418
1419 return inv.mapXY(x, y);
1420}
1421
Hal Canaryb1f411a2019-08-29 10:39:22 -04001422bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001423 if (GestureDevice::kMouse == fGestureDevice) {
1424 return false;
1425 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001426
1427 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001428 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001429 fWindow->inval();
1430 return true;
1431 }
1432
liyuqiand3cdbca2016-05-17 12:44:20 -07001433 void* castedOwner = reinterpret_cast<void*>(owner);
1434 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001435 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001436 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001437#if defined(SK_BUILD_FOR_IOS)
1438 // TODO: move IOS swipe detection higher up into the platform code
1439 SkPoint dir;
1440 if (fGesture.isFling(&dir)) {
1441 // swiping left or right
1442 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1443 if (dir.fX < 0) {
1444 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1445 fCurrentSlide + 1 : 0);
1446 } else {
1447 this->setCurrentSlide(fCurrentSlide > 0 ?
1448 fCurrentSlide - 1 : fSlides.count() - 1);
1449 }
1450 }
1451 fGesture.reset();
1452 }
1453#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001454 break;
1455 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001456 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001457 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001458 break;
1459 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001460 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001461 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001462 break;
1463 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001464 default: {
1465 // kLeft and kRight are only for swipes
1466 SkASSERT(false);
1467 break;
1468 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001469 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001470 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001471 fWindow->inval();
1472 return true;
1473}
1474
Hal Canaryb1f411a2019-08-29 10:39:22 -04001475bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001476 if (GestureDevice::kTouch == fGestureDevice) {
1477 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001478 }
Brian Osman16c81a12017-12-20 11:58:34 -05001479
Florin Malitacefc1b92018-02-19 21:43:47 -05001480 const auto slidePt = this->mapEvent(x, y);
1481 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1482 fWindow->inval();
1483 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001484 }
1485
1486 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001487 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001488 fGesture.touchEnd(nullptr);
1489 break;
1490 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001491 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001492 fGesture.touchBegin(nullptr, x, y);
1493 break;
1494 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001495 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001496 fGesture.touchMoved(nullptr, x, y);
1497 break;
1498 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001499 default: {
1500 SkASSERT(false); // shouldn't see kRight or kLeft here
1501 break;
1502 }
Brian Osman16c81a12017-12-20 11:58:34 -05001503 }
1504 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1505
Hal Canaryb1f411a2019-08-29 10:39:22 -04001506 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001507 fWindow->inval();
1508 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001509 return true;
1510}
1511
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001512bool Viewer::onFling(skui::InputState state) {
1513 if (skui::InputState::kRight == state) {
1514 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1515 return true;
1516 } else if (skui::InputState::kLeft == state) {
1517 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1518 return true;
1519 }
1520 return false;
1521}
1522
1523bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1524 switch (state) {
1525 case skui::InputState::kDown:
1526 fGesture.startZoom();
1527 return true;
1528 break;
1529 case skui::InputState::kMove:
1530 fGesture.updateZoom(scale, x, y, x, y);
1531 return true;
1532 break;
1533 case skui::InputState::kUp:
1534 fGesture.endZoom();
1535 return true;
1536 break;
1537 default:
1538 SkASSERT(false);
1539 break;
1540 }
1541
1542 return false;
1543}
1544
Brian Osmana109e392017-02-24 09:49:14 -05001545static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001546 // The gamut image covers a (0.8 x 0.9) shaped region
1547 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001548
1549 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1550 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1551 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001552 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1553 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1554 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001555
Brian Osman535c5e32019-02-09 16:32:58 -05001556 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1557 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1558 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1559 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1560 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001561}
1562
Ben Wagner3627d2e2018-06-26 14:23:20 -04001563static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001564 ImGui::DragCanvas dc(pt);
1565 dc.fillColor(IM_COL32(0, 0, 0, 128));
1566 dc.dragPoint(pt);
1567 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001568}
1569
Brian Osman9bb47cf2018-04-26 15:55:00 -04001570static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001571 ImGui::DragCanvas dc(pts);
1572 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001573
Brian Osman535c5e32019-02-09 16:32:58 -05001574 for (int i = 0; i < 4; ++i) {
1575 dc.dragPoint(pts + i);
1576 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001577
Brian Osman535c5e32019-02-09 16:32:58 -05001578 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1579 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1580 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1581 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001582
Brian Osman535c5e32019-02-09 16:32:58 -05001583 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001584}
1585
Brian Osmand67e5182017-12-08 16:46:09 -05001586void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001587 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1588 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001589 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001590 }
1591
1592 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001593 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1594 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001595 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001596 DisplayParams params = fWindow->getRequestedDisplayParams();
1597 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001598 const GrContext* ctx = fWindow->getGrContext();
1599
Brian Osmana109e392017-02-24 09:49:14 -05001600 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1601 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001602 if (ImGui::CollapsingHeader("Backend")) {
1603 int newBackend = static_cast<int>(fBackendType);
1604 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1605 ImGui::SameLine();
1606 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001607#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1608 ImGui::SameLine();
1609 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1610#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001611#if defined(SK_DAWN)
1612 ImGui::SameLine();
1613 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1614#endif
Brian Osman621491e2017-02-28 15:45:01 -05001615#if defined(SK_VULKAN)
1616 ImGui::SameLine();
1617 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1618#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001619#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001620 ImGui::SameLine();
1621 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1622#endif
Brian Osman621491e2017-02-28 15:45:01 -05001623 if (newBackend != fBackendType) {
1624 fDeferredActions.push_back([=]() {
1625 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1626 });
1627 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001628
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001629 bool* wire = &params.fGrContextOptions.fWireframeMode;
1630 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1631 paramsChanged = true;
1632 }
Brian Salomon99a33902017-03-07 15:16:34 -05001633
Brian Osman28b12522017-03-08 17:10:24 -05001634 if (ctx) {
1635 int sampleCount = fWindow->sampleCount();
1636 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001637 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001638 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1639 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1640 ImGui::RadioButton("16", &sampleCount, 16);
1641
1642 if (sampleCount != params.fMSAASampleCount) {
1643 params.fMSAASampleCount = sampleCount;
1644 paramsChanged = true;
1645 }
1646 }
1647
Ben Wagner37c54032018-04-13 14:30:23 -04001648 int pixelGeometryIdx = 0;
1649 if (fPixelGeometryOverrides) {
1650 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1651 }
1652 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1653 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1654 {
1655 uint32_t flags = params.fSurfaceProps.flags();
1656 if (pixelGeometryIdx == 0) {
1657 fPixelGeometryOverrides = false;
1658 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1659 } else {
1660 fPixelGeometryOverrides = true;
1661 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1662 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1663 }
1664 paramsChanged = true;
1665 }
1666
1667 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1668 if (ImGui::Checkbox("DFT", &useDFT)) {
1669 uint32_t flags = params.fSurfaceProps.flags();
1670 if (useDFT) {
1671 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1672 } else {
1673 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1674 }
1675 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1676 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1677 paramsChanged = true;
1678 }
1679
Brian Osman8a9de3d2017-03-01 14:59:05 -05001680 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001681 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001682 auto prButton = [&](GpuPathRenderers x) {
1683 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001684 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1685 params.fGrContextOptions.fGpuPathRenderers = x;
1686 paramsChanged = true;
1687 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001688 }
1689 };
1690
1691 if (!ctx) {
1692 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001693 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001694 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001695 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001696 prButton(GpuPathRenderers::kStencilAndCover);
1697 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001698 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001699 prButton(GpuPathRenderers::kNone);
1700 } else {
1701 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001702 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001703 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001704 prButton(GpuPathRenderers::kCoverageCounting);
1705 }
Jim Van Verth83010462017-03-16 08:45:39 -04001706 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001707 prButton(GpuPathRenderers::kTessellating);
1708 prButton(GpuPathRenderers::kNone);
1709 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001710 ImGui::TreePop();
1711 }
Brian Osman621491e2017-02-28 15:45:01 -05001712 }
1713
Ben Wagner964571d2019-03-08 12:35:06 -05001714 if (ImGui::CollapsingHeader("Tiling")) {
1715 ImGui::Checkbox("Enable", &fTiled);
1716 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1717 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1718 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1719 }
1720
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001721 if (ImGui::CollapsingHeader("Transform")) {
1722 float zoom = fZoomLevel;
1723 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1724 fZoomLevel = zoom;
1725 this->preTouchMatrixChanged();
1726 paramsChanged = true;
1727 }
1728 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001729 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001730 fRotation = deg;
1731 this->preTouchMatrixChanged();
1732 paramsChanged = true;
1733 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001734 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1735 if (ImGui_DragLocation(&fOffset)) {
1736 this->preTouchMatrixChanged();
1737 paramsChanged = true;
1738 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001739 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1740 this->preTouchMatrixChanged();
1741 paramsChanged = true;
1742 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001743 }
Brian Osman805a7272018-05-02 15:40:20 -04001744 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1745 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1746 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001747 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001748 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001749 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001750 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001751 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001752 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001753 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001754 }
1755
Ben Wagnera580fb32018-04-17 11:16:32 -04001756 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001757 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001758 if (fPaintOverrides.fAntiAlias) {
1759 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001760 }
1761 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001762 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001763 {
1764 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1765 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001766 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001767 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1768 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001769 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001770 fPaintOverrides.fAntiAlias = true;
1771 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001772 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001773 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001774 case SkPaintFields::AntiAliasState::Alias:
1775 break;
1776 case SkPaintFields::AntiAliasState::Normal:
1777 break;
1778 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1779 gSkUseAnalyticAA = true;
1780 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001781 break;
1782 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1783 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001784 break;
1785 }
1786 }
1787 paramsChanged = true;
1788 }
1789
Ben Wagner99a78dc2018-05-09 18:23:51 -04001790 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001791 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001792 bool (SkPaint::* isFlag)() const,
1793 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001794 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001795 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001796 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001797 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001798 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001799 if (ImGui::Combo(label, &itemIndex, items)) {
1800 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001801 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001802 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001803 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001804 (fPaint.*setFlag)(itemIndex == 2);
1805 }
1806 paramsChanged = true;
1807 }
1808 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001809
Ben Wagner99a78dc2018-05-09 18:23:51 -04001810 paintFlag("Dither",
1811 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001812 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001813 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001814
1815 int filterQualityIdx = 0;
1816 if (fPaintOverrides.fFilterQuality) {
1817 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1818 }
1819 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1820 "Default\0None\0Low\0Medium\0High\0\0"))
1821 {
1822 if (filterQualityIdx == 0) {
1823 fPaintOverrides.fFilterQuality = false;
1824 fPaint.setFilterQuality(kNone_SkFilterQuality);
1825 } else {
1826 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1827 fPaintOverrides.fFilterQuality = true;
1828 }
1829 paramsChanged = true;
1830 }
Ben Wagner9613e452019-01-23 10:34:59 -05001831 }
Hal Canary02738a82019-01-21 18:51:32 +00001832
Ben Wagner9613e452019-01-23 10:34:59 -05001833 if (ImGui::CollapsingHeader("Font")) {
1834 int hintingIdx = 0;
1835 if (fFontOverrides.fHinting) {
1836 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1837 }
1838 if (ImGui::Combo("Hinting", &hintingIdx,
1839 "Default\0None\0Slight\0Normal\0Full\0\0"))
1840 {
1841 if (hintingIdx == 0) {
1842 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001843 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001844 } else {
1845 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1846 fFontOverrides.fHinting = true;
1847 }
1848 paramsChanged = true;
1849 }
Hal Canary02738a82019-01-21 18:51:32 +00001850
Ben Wagner9613e452019-01-23 10:34:59 -05001851 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1852 bool SkFontFields::* flag,
1853 bool (SkFont::* isFlag)() const,
1854 void (SkFont::* setFlag)(bool) )
1855 {
1856 int itemIndex = 0;
1857 if (fFontOverrides.*flag) {
1858 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1859 }
1860 if (ImGui::Combo(label, &itemIndex, items)) {
1861 if (itemIndex == 0) {
1862 fFontOverrides.*flag = false;
1863 } else {
1864 fFontOverrides.*flag = true;
1865 (fFont.*setFlag)(itemIndex == 2);
1866 }
1867 paramsChanged = true;
1868 }
1869 };
Hal Canary02738a82019-01-21 18:51:32 +00001870
Ben Wagner9613e452019-01-23 10:34:59 -05001871 fontFlag("Fake Bold Glyphs",
1872 "Default\0No Fake Bold\0Fake Bold\0\0",
1873 &SkFontFields::fEmbolden,
1874 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001875
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001876 fontFlag("Baseline Snapping",
1877 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1878 &SkFontFields::fBaselineSnap,
1879 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1880
Ben Wagner9613e452019-01-23 10:34:59 -05001881 fontFlag("Linear Text",
1882 "Default\0No Linear Text\0Linear Text\0\0",
1883 &SkFontFields::fLinearMetrics,
1884 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001885
Ben Wagner9613e452019-01-23 10:34:59 -05001886 fontFlag("Subpixel Position Glyphs",
1887 "Default\0Pixel Text\0Subpixel Text\0\0",
1888 &SkFontFields::fSubpixel,
1889 &SkFont::isSubpixel, &SkFont::setSubpixel);
1890
1891 fontFlag("Embedded Bitmap Text",
1892 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1893 &SkFontFields::fEmbeddedBitmaps,
1894 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1895
1896 fontFlag("Force Auto-Hinting",
1897 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1898 &SkFontFields::fForceAutoHinting,
1899 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1900
1901 int edgingIdx = 0;
1902 if (fFontOverrides.fEdging) {
1903 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1904 }
1905 if (ImGui::Combo("Edging", &edgingIdx,
1906 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1907 {
1908 if (edgingIdx == 0) {
1909 fFontOverrides.fEdging = false;
1910 fFont.setEdging(SkFont::Edging::kAlias);
1911 } else {
1912 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1913 fFontOverrides.fEdging = true;
1914 }
1915 paramsChanged = true;
1916 }
1917
Ben Wagner15a8d572019-03-21 13:35:44 -04001918 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1919 if (fFontOverrides.fSize) {
1920 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001921 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001922 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001923 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001924 fFontOverrides.fSizeRange[0],
1925 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001926 "%.6f", 2.0f))
1927 {
Mike Reed3ae47332019-01-04 10:11:46 -05001928 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001929 paramsChanged = true;
1930 }
1931 }
1932
1933 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1934 if (fFontOverrides.fScaleX) {
1935 float scaleX = fFont.getScaleX();
1936 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1937 fFont.setScaleX(scaleX);
1938 paramsChanged = true;
1939 }
1940 }
1941
1942 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1943 if (fFontOverrides.fSkewX) {
1944 float skewX = fFont.getSkewX();
1945 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1946 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001947 paramsChanged = true;
1948 }
1949 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001950 }
1951
Mike Reed81f60ec2018-05-15 10:09:52 -04001952 {
1953 SkMetaData controls;
1954 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1955 if (ImGui::CollapsingHeader("Current Slide")) {
1956 SkMetaData::Iter iter(controls);
1957 const char* name;
1958 SkMetaData::Type type;
1959 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001960 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001961 if (type == SkMetaData::kScalar_Type) {
1962 float val[3];
1963 SkASSERT(count == 3);
1964 controls.findScalars(name, &count, val);
1965 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1966 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001967 }
Ben Wagner110c7032019-03-22 17:03:59 -04001968 } else if (type == SkMetaData::kBool_Type) {
1969 bool val;
1970 SkASSERT(count == 1);
1971 controls.findBool(name, &val);
1972 if (ImGui::Checkbox(name, &val)) {
1973 controls.setBool(name, val);
1974 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001975 }
1976 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001977 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001978 }
1979 }
1980 }
1981
Ben Wagner7a3c6742018-04-23 10:01:07 -04001982 if (fShowSlidePicker) {
1983 ImGui::SetNextTreeNodeOpen(true);
1984 }
Brian Osman79086b92017-02-10 13:36:16 -05001985 if (ImGui::CollapsingHeader("Slide")) {
1986 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001987 static ImVector<const char*> filteredSlideNames;
1988 static ImVector<int> filteredSlideIndices;
1989
Brian Osmanfce09c52017-11-14 15:32:20 -05001990 if (fShowSlidePicker) {
1991 ImGui::SetKeyboardFocusHere();
1992 fShowSlidePicker = false;
1993 }
1994
Brian Osman79086b92017-02-10 13:36:16 -05001995 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001996 filteredSlideNames.clear();
1997 filteredSlideIndices.clear();
1998 int filteredIndex = 0;
1999 for (int i = 0; i < fSlides.count(); ++i) {
2000 const char* slideName = fSlides[i]->getName().c_str();
2001 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2002 if (i == fCurrentSlide) {
2003 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002004 }
Brian Osmanf479e422017-11-08 13:11:36 -05002005 filteredSlideNames.push_back(slideName);
2006 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002007 }
Brian Osman79086b92017-02-10 13:36:16 -05002008 }
Brian Osmanf479e422017-11-08 13:11:36 -05002009
Brian Osmanf479e422017-11-08 13:11:36 -05002010 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2011 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002012 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002013 }
2014 }
Brian Osmana109e392017-02-24 09:49:14 -05002015
2016 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002017 ColorMode newMode = fColorMode;
2018 auto cmButton = [&](ColorMode mode, const char* label) {
2019 if (ImGui::RadioButton(label, mode == fColorMode)) {
2020 newMode = mode;
2021 }
2022 };
2023
2024 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002025 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2026 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002027 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002028
2029 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002030 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002031 }
2032
2033 // Pick from common gamuts:
2034 int primariesIdx = 4; // Default: Custom
2035 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2036 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2037 primariesIdx = i;
2038 break;
2039 }
2040 }
2041
Brian Osman03115dc2018-11-26 13:55:19 -05002042 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002043 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002044
Brian Osmana109e392017-02-24 09:49:14 -05002045 if (ImGui::Combo("Primaries", &primariesIdx,
2046 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2047 if (primariesIdx >= 0 && primariesIdx <= 3) {
2048 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2049 }
2050 }
2051
2052 // Allow direct editing of gamut
2053 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2054 }
Brian Osman207d4102019-01-10 09:40:58 -05002055
2056 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002057 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002058 if (ImGui::Checkbox("Pause", &isPaused)) {
2059 fAnimTimer.togglePauseResume();
2060 }
Brian Osman707d2022019-01-10 11:27:34 -05002061
2062 float speed = fAnimTimer.getSpeed();
2063 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2064 fAnimTimer.setSpeed(speed);
2065 }
Brian Osman207d4102019-01-10 09:40:58 -05002066 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002067
Brian Osmanfd7657c2019-04-25 11:34:07 -04002068 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2069#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2070 || Window::kANGLE_BackendType == fBackendType
2071#endif
2072 ;
2073
2074 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2075 // than GL, we need to force it on. Just do that on the first frame after the backend
2076 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002077 if (!backendIsGL &&
2078 params.fGrContextOptions.fShaderCacheStrategy !=
2079 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2080 params.fGrContextOptions.fShaderCacheStrategy =
2081 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002082 paramsChanged = true;
2083 fPersistentCache.reset();
2084 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002085 // To re-load shaders from the currently active programs, we flush all caches on one
2086 // frame, then set a flag to poll the cache on the next frame.
2087 static bool gLoadPending = false;
2088 if (gLoadPending) {
2089 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2090 int hitCount) {
2091 CachedGLSL& entry(fCachedGLSL.push_back());
2092 entry.fKey = key;
2093 SkMD5 hash;
2094 hash.write(key->bytes(), key->size());
2095 SkMD5::Digest digest = hash.finish();
2096 for (int i = 0; i < 16; ++i) {
2097 entry.fKeyString.appendf("%02x", digest.data[i]);
2098 }
2099
Brian Osmana66081d2019-09-03 14:59:26 -04002100 SkReader32 reader(data->data(), data->size());
2101 entry.fShaderType = reader.readU32();
2102 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2103 entry.fInputs,
2104 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002105 };
2106 fCachedGLSL.reset();
2107 fPersistentCache.foreach(collectShaders);
2108 gLoadPending = false;
2109 }
2110
2111 // Defer actually doing the load/save logic so that we can trigger a save when we
2112 // start or finish hovering on a tree node in the list below:
2113 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002114 bool doSave = ImGui::Button("Save");
2115 if (backendIsGL) {
2116 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002117 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2118 GrContextOptions::ShaderCacheStrategy::kSkSL;
2119 if (ImGui::Checkbox("SkSL", &sksl)) {
2120 params.fGrContextOptions.fShaderCacheStrategy = sksl
2121 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2122 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002123 paramsChanged = true;
2124 doLoad = true;
2125 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2126 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002127 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002128
2129 ImGui::BeginChild("##ScrollingRegion");
2130 for (auto& entry : fCachedGLSL) {
2131 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2132 bool hovered = ImGui::IsItemHovered();
2133 if (hovered != entry.fHovered) {
2134 // Force a save to patch the highlight shader in/out
2135 entry.fHovered = hovered;
2136 doSave = true;
2137 }
2138 if (inTreeNode) {
2139 // Full width, and a reasonable amount of space for each shader.
2140 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2141 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2142 boxSize);
2143 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2144 boxSize);
2145 ImGui::TreePop();
2146 }
2147 }
2148 ImGui::EndChild();
2149
2150 if (doLoad) {
2151 fPersistentCache.reset();
2152 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2153 gLoadPending = true;
2154 }
2155 if (doSave) {
2156 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002157 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002158 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2159 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002160
Brian Osman072e6fc2019-06-12 11:35:41 -04002161 SkSL::String highlight;
2162 if (!sksl) {
2163 highlight = shaderCaps->versionDeclString();
2164 if (shaderCaps->usesPrecisionModifiers()) {
2165 highlight.append("precision mediump float;\n");
2166 }
Brian Osman5bee3902019-05-07 09:55:45 -04002167 }
2168 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002169 highlight.appendf("out %s sk_FragColor;\n"
2170 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2171 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002172
2173 fPersistentCache.reset();
2174 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2175 for (auto& entry : fCachedGLSL) {
2176 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2177 if (entry.fHovered) {
2178 entry.fShader[kFragment_GrShaderType] = highlight;
2179 }
2180
Brian Osmana085a412019-04-25 09:44:43 -04002181 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2182 entry.fShader,
2183 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002184 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002185 fPersistentCache.store(*entry.fKey, *data);
2186
2187 entry.fShader[kFragment_GrShaderType] = backup;
2188 }
2189 }
2190 }
Brian Osman79086b92017-02-10 13:36:16 -05002191 }
Brian Salomon99a33902017-03-07 15:16:34 -05002192 if (paramsChanged) {
2193 fDeferredActions.push_back([=]() {
2194 fWindow->setRequestedDisplayParams(params);
2195 fWindow->inval();
2196 this->updateTitle();
2197 });
2198 }
Brian Osman79086b92017-02-10 13:36:16 -05002199 ImGui::End();
2200 }
2201
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002202 if (gShaderErrorHandler.fErrors.count()) {
2203 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2204 ImGui::Begin("Shader Errors");
2205 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2206 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
2207 ImGui::TextWrapped("%s", gShaderErrorHandler.fShaders[i].c_str());
2208 }
2209 ImGui::End();
2210 gShaderErrorHandler.reset();
2211 }
2212
Brian Osmanf6877092017-02-13 09:39:57 -05002213 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002214 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2215 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002216 static int zoomFactor = 8;
2217 if (ImGui::Button("<<")) {
2218 zoomFactor = SkTMax(zoomFactor / 2, 4);
2219 }
2220 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2221 if (ImGui::Button(">>")) {
2222 zoomFactor = SkTMin(zoomFactor * 2, 32);
2223 }
Brian Osmanf6877092017-02-13 09:39:57 -05002224
Ben Wagner3627d2e2018-06-26 14:23:20 -04002225 if (!fZoomWindowFixed) {
2226 ImVec2 mousePos = ImGui::GetMousePos();
2227 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2228 }
2229 SkScalar x = fZoomWindowLocation.x();
2230 SkScalar y = fZoomWindowLocation.y();
2231 int xInt = SkScalarRoundToInt(x);
2232 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002233 ImVec2 avail = ImGui::GetContentRegionAvail();
2234
Brian Osmanead517d2017-11-13 15:36:36 -05002235 uint32_t pixel = 0;
2236 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002237 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002238 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002239 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002240 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002241 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002242 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2243 }
2244
Brian Osmand67e5182017-12-08 16:46:09 -05002245 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002246 // Translate so the region of the image that's under the mouse cursor is centered
2247 // in the zoom canvas:
2248 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002249 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2250 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002251 c->drawImage(this->fLastImage, 0, 0);
2252
2253 SkPaint outline;
2254 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002255 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002256 });
Brian Osmanf6877092017-02-13 09:39:57 -05002257 }
2258
2259 ImGui::End();
2260 }
Brian Osman79086b92017-02-10 13:36:16 -05002261}
2262
liyuqian2edb0f42016-07-06 14:11:32 -07002263void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002264 for (int i = 0; i < fDeferredActions.count(); ++i) {
2265 fDeferredActions[i]();
2266 }
2267 fDeferredActions.reset();
2268
Brian Osman56a24812017-12-19 11:15:16 -05002269 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002270 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002271 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002272 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002273
Brian Osman79086b92017-02-10 13:36:16 -05002274 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002275 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2276 // not be visible, though. So we need to redraw if there is at least one visible window, or
2277 // more than one active window. Newly created windows are active but not visible for one frame
2278 // while they determine their layout and sizing.
2279 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2280 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002281 fWindow->inval();
2282 }
jvanverth9f372462016-04-06 06:08:59 -07002283}
liyuqiane5a6cd92016-05-27 08:52:52 -07002284
Florin Malitab632df72018-06-18 21:23:06 -04002285template <typename OptionsFunc>
2286static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2287 OptionsFunc&& optionsFunc) {
2288 writer.beginObject();
2289 {
2290 writer.appendString(kName , name);
2291 writer.appendString(kValue, value);
2292
2293 writer.beginArray(kOptions);
2294 {
2295 optionsFunc(writer);
2296 }
2297 writer.endArray();
2298 }
2299 writer.endObject();
2300}
2301
2302
liyuqiane5a6cd92016-05-27 08:52:52 -07002303void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002304 if (!fWindow) {
2305 return;
2306 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002307 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002308 return; // Surface hasn't been created yet.
2309 }
2310
Florin Malitab632df72018-06-18 21:23:06 -04002311 SkDynamicMemoryWStream memStream;
2312 SkJSONWriter writer(&memStream);
2313 writer.beginArray();
2314
liyuqianb73c24b2016-06-03 08:47:23 -07002315 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002316 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2317 [this](SkJSONWriter& writer) {
2318 for(const auto& slide : fSlides) {
2319 writer.appendString(slide->getName().c_str());
2320 }
2321 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002322
liyuqianb73c24b2016-06-03 08:47:23 -07002323 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002324 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2325 [](SkJSONWriter& writer) {
2326 for (const auto& str : kBackendTypeStrings) {
2327 writer.appendString(str);
2328 }
2329 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002330
csmartdalton578f0642017-02-24 16:04:47 -07002331 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002332 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2333 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2334 [this](SkJSONWriter& writer) {
2335 writer.appendS32(0);
2336
2337 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2338 return;
2339 }
2340
2341 for (int msaa : {4, 8, 16}) {
2342 writer.appendS32(msaa);
2343 }
2344 });
csmartdalton578f0642017-02-24 16:04:47 -07002345
csmartdalton61cd31a2017-02-27 17:00:53 -07002346 // Path renderer state
2347 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002348 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2349 [this](SkJSONWriter& writer) {
2350 const GrContext* ctx = fWindow->getGrContext();
2351 if (!ctx) {
2352 writer.appendString("Software");
2353 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002354 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002355
Florin Malitab632df72018-06-18 21:23:06 -04002356 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2357 if (fWindow->sampleCount() > 1) {
2358 if (caps->shaderCaps()->pathRenderingSupport()) {
2359 writer.appendString(
2360 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2361 }
2362 } else {
2363 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2364 writer.appendString(
2365 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2366 }
2367 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2368 }
2369 writer.appendString(
2370 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2371 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2372 }
2373 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002374
liyuqianb73c24b2016-06-03 08:47:23 -07002375 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002376 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2377 [this](SkJSONWriter& writer) {
2378 writer.appendString(kSoftkeyHint);
2379 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2380 writer.appendString(softkey.c_str());
2381 }
2382 });
liyuqianb73c24b2016-06-03 08:47:23 -07002383
Florin Malitab632df72018-06-18 21:23:06 -04002384 writer.endArray();
2385 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002386
Florin Malitab632df72018-06-18 21:23:06 -04002387 auto data = memStream.detachAsData();
2388
2389 // TODO: would be cool to avoid this copy
2390 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2391
2392 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002393}
2394
2395void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002396 // For those who will add more features to handle the state change in this function:
2397 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2398 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2399 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002400 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002401 for (int i = 0; i < fSlides.count(); ++i) {
2402 if (fSlides[i]->getName().equals(stateValue)) {
2403 this->setCurrentSlide(i);
2404 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002405 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002406 }
Florin Malitaab99c342018-01-16 16:23:03 -05002407
2408 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002409 } else if (stateName.equals(kBackendStateName)) {
2410 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2411 if (stateValue.equals(kBackendTypeStrings[i])) {
2412 if (fBackendType != i) {
2413 fBackendType = (sk_app::Window::BackendType)i;
2414 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002415 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002416 }
2417 break;
2418 }
2419 }
csmartdalton578f0642017-02-24 16:04:47 -07002420 } else if (stateName.equals(kMSAAStateName)) {
2421 DisplayParams params = fWindow->getRequestedDisplayParams();
2422 int sampleCount = atoi(stateValue.c_str());
2423 if (sampleCount != params.fMSAASampleCount) {
2424 params.fMSAASampleCount = sampleCount;
2425 fWindow->setRequestedDisplayParams(params);
2426 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002427 this->updateTitle();
2428 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002429 }
2430 } else if (stateName.equals(kPathRendererStateName)) {
2431 DisplayParams params = fWindow->getRequestedDisplayParams();
2432 for (const auto& pair : gPathRendererNames) {
2433 if (pair.second == stateValue.c_str()) {
2434 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2435 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2436 fWindow->setRequestedDisplayParams(params);
2437 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002438 this->updateTitle();
2439 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002440 }
2441 break;
2442 }
csmartdalton578f0642017-02-24 16:04:47 -07002443 }
liyuqianb73c24b2016-06-03 08:47:23 -07002444 } else if (stateName.equals(kSoftkeyStateName)) {
2445 if (!stateValue.equals(kSoftkeyHint)) {
2446 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002447 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002448 }
liyuqian2edb0f42016-07-06 14:11:32 -07002449 } else if (stateName.equals(kRefreshStateName)) {
2450 // This state is actually NOT in the UI state.
2451 // We use this to allow Android to quickly set bool fRefresh.
2452 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002453 } else {
2454 SkDebugf("Unknown stateName: %s", stateName.c_str());
2455 }
2456}
Brian Osman79086b92017-02-10 13:36:16 -05002457
Hal Canaryb1f411a2019-08-29 10:39:22 -04002458bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002459 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002460}
2461
Hal Canaryb1f411a2019-08-29 10:39:22 -04002462bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002463 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002464 fWindow->inval();
2465 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002466 } else {
2467 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002468 }
Brian Osman79086b92017-02-10 13:36:16 -05002469}