blob: fcab247547501d33eb1518dd67c36619c1018ac5 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkGraphics.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkSurface.h"
14#include "include/gpu/GrContext.h"
15#include "include/private/SkTo.h"
16#include "include/utils/SkPaintFilterCanvas.h"
17#include "src/core/SkColorSpacePriv.h"
18#include "src/core/SkImagePriv.h"
19#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkOSFile.h"
21#include "src/core/SkScan.h"
22#include "src/core/SkTaskGroup.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrGpu.h"
25#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070026#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
28#include "src/utils/SkJSONWriter.h"
29#include "src/utils/SkOSPath.h"
30#include "tools/Resources.h"
31#include "tools/ToolUtils.h"
32#include "tools/flags/CommandLineFlags.h"
33#include "tools/flags/CommonFlags.h"
34#include "tools/trace/EventTracingPriv.h"
35#include "tools/viewer/BisectSlide.h"
36#include "tools/viewer/GMSlide.h"
37#include "tools/viewer/ImageSlide.h"
38#include "tools/viewer/ParticlesSlide.h"
39#include "tools/viewer/SKPSlide.h"
40#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050041#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "tools/viewer/SlideDir.h"
43#include "tools/viewer/SvgSlide.h"
44#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070045
Hal Canaryc640d0d2018-06-13 09:59:02 -040046#include <stdlib.h>
47#include <map>
48
Hal Canary8a001442018-09-19 11:31:27 -040049#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040050#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040051
Florin Malita87ccf332018-05-04 12:23:24 -040052#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040054#endif
55
Brian Osman5e7fbfd2019-05-03 13:13:35 -040056class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
57public:
58 void compileError(const char* shader, const char* errors) override {
59 fShaders.push_back(SkString(shader));
60 fErrors.push_back(SkString(errors));
61 }
62
63 void reset() {
64 fShaders.reset();
65 fErrors.reset();
66 }
67
68 SkTArray<SkString> fShaders;
69 SkTArray<SkString> fErrors;
70};
71
72static CapturingShaderErrorHandler gShaderErrorHandler;
73
jvanverth34524262016-05-04 13:49:13 -070074using namespace sk_app;
75
csmartdalton61cd31a2017-02-27 17:00:53 -070076static std::map<GpuPathRenderers, std::string> gPathRendererNames;
77
jvanverth9f372462016-04-06 06:08:59 -070078Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070079 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070080}
81
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060082static DEFINE_string(slide, "", "Start on this sample.");
83static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050084
Stephen Whitea800ec92019-08-02 15:04:52 -040085#if defined(SK_VULKAN)
jvanverthb8794cc2016-07-27 14:29:18 -070086# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050087#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
88# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
Stephen Whitea800ec92019-08-02 15:04:52 -040089#elif defined(SK_DAWN)
90# define BACKENDS_STR "\"sw\", \"gl\", and \"dawn\""
bsalomon6c471f72016-07-26 12:56:32 -070091#else
92# define BACKENDS_STR "\"sw\" and \"gl\""
93#endif
94
Brian Osman2dd96932016-10-18 15:33:53 -040095static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070096
Mike Klein5b3f3432019-03-21 11:42:21 -050097static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070098
Mike Klein84836b72019-03-21 11:31:36 -050099static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700100
Mike Klein84836b72019-03-21 11:31:36 -0500101static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400102
Mike Kleinc6142d82019-03-25 10:54:59 -0500103static DEFINE_string2(match, m, nullptr,
104 "[~][^]substring[$] [...] of name to run.\n"
105 "Multiple matches may be separated by spaces.\n"
106 "~ causes a matching name to always be skipped\n"
107 "^ requires the start of the name to match\n"
108 "$ requires the end of the name to match\n"
109 "^ and $ requires an exact match\n"
110 "If a name does not match any list entry,\n"
111 "it is skipped unless some list entry starts with ~");
112
Mike Klein19fb3972019-03-21 13:08:08 -0500113#if defined(SK_BUILD_FOR_ANDROID)
114 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500115 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
116 static DEFINE_string(lotties, "/data/local/tmp/lotties",
117 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500118#else
119 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500120 static DEFINE_string(skps, "skps", "Directory to read skps from.");
121 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500122#endif
123
Mike Kleinc6142d82019-03-25 10:54:59 -0500124static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
125
126static DEFINE_int_2(threads, j, -1,
127 "Run threadsafe tests on a threadpool with this many extra threads, "
128 "defaulting to one extra thread per core.");
129
Jim Van Verth7b558182019-11-14 16:47:01 -0500130static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
131
Mike Kleinc6142d82019-03-25 10:54:59 -0500132
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
Chris Dalton37ae4b02019-12-28 14:51:11 -0700278 gPathRendererNames[GpuPathRenderers::kDefault] = "Default 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;
csmartdalton578f0642017-02-24 16:04:47 -0700312 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500313 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700314
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 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700392 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
393 DisplayParams params = fWindow->getRequestedDisplayParams();
394 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
395 fWindow->setRequestedDisplayParams(params);
396 fWindow->inval();
397 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400398 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500399 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700400 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400401 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500402 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700403 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400404 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700405 this->changeZoomLevel(1.f / 32.f);
406 fWindow->inval();
407 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400408 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700409 this->changeZoomLevel(-1.f / 32.f);
410 fWindow->inval();
411 });
jvanverthaf236b52016-05-20 06:01:06 -0700412 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400413 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
414 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500415 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400416#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
417 if (newBackend == sk_app::Window::kVulkan_BackendType) {
418 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
419 sk_app::Window::kBackendTypeCount);
420 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
421 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500422 }
423#endif
Brian Osman621491e2017-02-28 15:45:01 -0500424 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700425 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500426 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
427 fSaveToSKP = true;
428 fWindow->inval();
429 });
Mike Reed376d8122019-03-14 11:39:02 -0400430 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
431 fShowSlideDimensions = !fShowSlideDimensions;
432 fWindow->inval();
433 });
Ben Wagner37c54032018-04-13 14:30:23 -0400434 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
435 DisplayParams params = fWindow->getRequestedDisplayParams();
436 uint32_t flags = params.fSurfaceProps.flags();
437 if (!fPixelGeometryOverrides) {
438 fPixelGeometryOverrides = true;
439 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
440 } else {
441 switch (params.fSurfaceProps.pixelGeometry()) {
442 case kUnknown_SkPixelGeometry:
443 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
444 break;
445 case kRGB_H_SkPixelGeometry:
446 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
447 break;
448 case kBGR_H_SkPixelGeometry:
449 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
450 break;
451 case kRGB_V_SkPixelGeometry:
452 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
453 break;
454 case kBGR_V_SkPixelGeometry:
455 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
456 fPixelGeometryOverrides = false;
457 break;
458 }
459 }
460 fWindow->setRequestedDisplayParams(params);
461 this->updateTitle();
462 fWindow->inval();
463 });
Ben Wagner9613e452019-01-23 10:34:59 -0500464 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500465 if (!fFontOverrides.fHinting) {
466 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400467 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500468 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500469 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400470 case SkFontHinting::kNone:
471 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500472 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400473 case SkFontHinting::kSlight:
474 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500475 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400476 case SkFontHinting::kNormal:
477 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500478 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400479 case SkFontHinting::kFull:
480 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500481 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500482 break;
483 }
484 }
485 this->updateTitle();
486 fWindow->inval();
487 });
488 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500489 if (!fPaintOverrides.fAntiAlias) {
490 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
491 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500492 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500493 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500494 } else {
495 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500496 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500497 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500498 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400499 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500500 break;
501 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500502 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500503 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400504 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500505 break;
506 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500507 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400508 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500509 break;
510 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500511 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
512 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500513 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
514 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500515 break;
516 }
517 }
518 this->updateTitle();
519 fWindow->inval();
520 });
Ben Wagner37c54032018-04-13 14:30:23 -0400521 fCommands.addCommand('D', "Modes", "DFT", [this]() {
522 DisplayParams params = fWindow->getRequestedDisplayParams();
523 uint32_t flags = params.fSurfaceProps.flags();
524 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
525 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
526 fWindow->setRequestedDisplayParams(params);
527 this->updateTitle();
528 fWindow->inval();
529 });
Ben Wagner9613e452019-01-23 10:34:59 -0500530 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500531 if (!fFontOverrides.fEdging) {
532 fFontOverrides.fEdging = true;
533 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500534 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500535 switch (fFont.getEdging()) {
536 case SkFont::Edging::kAlias:
537 fFont.setEdging(SkFont::Edging::kAntiAlias);
538 break;
539 case SkFont::Edging::kAntiAlias:
540 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
541 break;
542 case SkFont::Edging::kSubpixelAntiAlias:
543 fFont.setEdging(SkFont::Edging::kAlias);
544 fFontOverrides.fEdging = false;
545 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500546 }
547 }
548 this->updateTitle();
549 fWindow->inval();
550 });
Ben Wagner9613e452019-01-23 10:34:59 -0500551 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500552 if (!fFontOverrides.fSubpixel) {
553 fFontOverrides.fSubpixel = true;
554 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500555 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500556 if (!fFont.isSubpixel()) {
557 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500558 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500559 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500560 }
561 }
562 this->updateTitle();
563 fWindow->inval();
564 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400565 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
566 if (!fFontOverrides.fBaselineSnap) {
567 fFontOverrides.fBaselineSnap = true;
568 fFont.setBaselineSnap(false);
569 } else {
570 if (!fFont.isBaselineSnap()) {
571 fFont.setBaselineSnap(true);
572 } else {
573 fFontOverrides.fBaselineSnap = false;
574 }
575 }
576 this->updateTitle();
577 fWindow->inval();
578 });
Brian Osman805a7272018-05-02 15:40:20 -0400579 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
580 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
581 : kPerspective_Real;
582 this->updateTitle();
583 fWindow->inval();
584 });
585 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
586 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
587 : kPerspective_Off;
588 this->updateTitle();
589 fWindow->inval();
590 });
Brian Osman207d4102019-01-10 09:40:58 -0500591 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
592 fAnimTimer.togglePauseResume();
593 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400594 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
595 fZoomUI = !fZoomUI;
596 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
597 fWindow->inval();
598 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500599
jvanverth2bb3b6d2016-04-08 07:24:09 -0700600 // set up slides
601 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500602 if (FLAGS_list) {
603 this->listNames();
604 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700605
Brian Osman9bb47cf2018-04-26 15:55:00 -0400606 fPerspectivePoints[0].set(0, 0);
607 fPerspectivePoints[1].set(1, 0);
608 fPerspectivePoints[2].set(0, 1);
609 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700610 fAnimTimer.run();
611
Hal Canaryc465d132017-12-08 10:21:31 -0500612 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500613 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400614 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500615 }
616 fImGuiGamutPaint.setColor(SK_ColorWHITE);
617 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
618
jongdeok.kim804f17e2019-02-26 14:39:23 +0900619 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500620 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700621}
622
jvanverth34524262016-05-04 13:49:13 -0700623void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400624 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
625 static const struct {
626 const char* fExtension;
627 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500628 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400629 const SlideFactory fFactory;
630 } gExternalSlidesInfo[] = {
631 { ".skp", "skp-dir", FLAGS_skps,
632 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
633 return sk_make_sp<SKPSlide>(name, path);}
634 },
635 { ".jpg", "jpg-dir", FLAGS_jpgs,
636 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
637 return sk_make_sp<ImageSlide>(name, path);}
638 },
Florin Malita87ccf332018-05-04 12:23:24 -0400639#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400640 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400641 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
642 return sk_make_sp<SkottieSlide>(name, path);}
643 },
Florin Malita87ccf332018-05-04 12:23:24 -0400644#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400645#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400646 { ".svg", "svg-dir", FLAGS_svgs,
647 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
648 return sk_make_sp<SvgSlide>(name, path);}
649 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400650#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400651 };
jvanverthc265a922016-04-08 12:51:45 -0700652
Brian Salomon343553a2018-09-05 15:41:23 -0400653 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700654
Mike Klein88544fb2019-03-20 10:50:33 -0500655 const auto addSlide =
656 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
657 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
658 return;
659 }
liyuqian6f163d22016-06-13 12:26:45 -0700660
Mike Klein88544fb2019-03-20 10:50:33 -0500661 if (auto slide = fact(name, path)) {
662 dirSlides.push_back(slide);
663 fSlides.push_back(std::move(slide));
664 }
665 };
Florin Malita76a076b2018-02-15 18:40:48 -0500666
Florin Malita38792ce2018-05-08 10:36:18 -0400667 if (!FLAGS_file.isEmpty()) {
668 // single file mode
669 const SkString file(FLAGS_file[0]);
670
671 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
672 for (const auto& sinfo : gExternalSlidesInfo) {
673 if (file.endsWith(sinfo.fExtension)) {
674 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
675 return;
676 }
677 }
678
679 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
680 } else {
681 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
682 }
683
684 return;
685 }
686
687 // Bisect slide.
688 if (!FLAGS_bisect.isEmpty()) {
689 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500690 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400691 if (FLAGS_bisect.count() >= 2) {
692 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
693 bisect->onChar(*ch);
694 }
695 }
696 fSlides.push_back(std::move(bisect));
697 }
698 }
699
700 // GMs
701 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400702 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400703 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500704 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400705 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400706 fSlides.push_back(std::move(slide));
707 }
Florin Malita38792ce2018-05-08 10:36:18 -0400708 }
709 // reverse gms
710 int numGMs = fSlides.count() - firstGM;
711 for (int i = 0; i < numGMs/2; ++i) {
712 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
713 }
714
715 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400716 for (const SampleFactory factory : SampleRegistry::Range()) {
717 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500718 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400719 fSlides.push_back(slide);
720 }
Florin Malita38792ce2018-05-08 10:36:18 -0400721 }
722
Brian Osman7c979f52019-02-12 13:27:51 -0500723 // Particle demo
724 {
725 // TODO: Convert this to a sample
726 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500727 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500728 fSlides.push_back(std::move(slide));
729 }
730 }
731
Brian Osmand927bd22019-12-18 11:23:12 -0500732 // Runtime shader editor
733 {
734 sk_sp<Slide> slide(new SkSLSlide());
735 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
736 fSlides.push_back(std::move(slide));
737 }
738 }
739
Florin Malita0ffa3222018-04-05 14:34:45 -0400740 for (const auto& info : gExternalSlidesInfo) {
741 for (const auto& flag : info.fFlags) {
742 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
743 // single file
744 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
745 } else {
746 // directory
747 SkOSFile::Iter it(flag.c_str(), info.fExtension);
748 SkString name;
749 while (it.next(&name)) {
750 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
751 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400752 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400753 if (!dirSlides.empty()) {
754 fSlides.push_back(
755 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
756 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500757 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400758 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400759 }
760 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500761
762 if (!fSlides.count()) {
763 sk_sp<Slide> slide(new NullSlide());
764 fSlides.push_back(std::move(slide));
765 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700766}
767
768
jvanverth34524262016-05-04 13:49:13 -0700769Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700770 fWindow->detach();
771 delete fWindow;
772}
773
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500774struct SkPaintTitleUpdater {
775 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
776 void append(const char* s) {
777 if (fCount == 0) {
778 fTitle->append(" {");
779 } else {
780 fTitle->append(", ");
781 }
782 fTitle->append(s);
783 ++fCount;
784 }
785 void done() {
786 if (fCount > 0) {
787 fTitle->append("}");
788 }
789 }
790 SkString* fTitle;
791 int fCount;
792};
793
brianosman05de2162016-05-06 13:28:57 -0700794void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700795 if (!fWindow) {
796 return;
797 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500798 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700799 return; // Surface hasn't been created yet.
800 }
801
jvanverth34524262016-05-04 13:49:13 -0700802 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700803 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700804
Mike Kleine5acd752019-03-22 09:57:16 -0500805 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400806 if (gSkForceAnalyticAA) {
807 title.append(" <FAAA>");
808 } else {
809 title.append(" <AAA>");
810 }
811 }
812
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500813 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500814 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
815 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400816 const char* on, const char* off)
817 {
Ben Wagner9613e452019-01-23 10:34:59 -0500818 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400819 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500820 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400821 };
822
Ben Wagner9613e452019-01-23 10:34:59 -0500823 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
824 const char* on, const char* off)
825 {
826 if (fFontOverrides.*flag) {
827 paintTitle.append((fFont.*isFlag)() ? on : off);
828 }
829 };
830
831 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
832 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500833 if (fPaintOverrides.fFilterQuality) {
834 switch (fPaint.getFilterQuality()) {
835 case kNone_SkFilterQuality:
836 paintTitle.append("NoFilter");
837 break;
838 case kLow_SkFilterQuality:
839 paintTitle.append("LowFilter");
840 break;
841 case kMedium_SkFilterQuality:
842 paintTitle.append("MediumFilter");
843 break;
844 case kHigh_SkFilterQuality:
845 paintTitle.append("HighFilter");
846 break;
847 }
848 }
Ben Wagner9613e452019-01-23 10:34:59 -0500849
850 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
851 "Force Autohint", "No Force Autohint");
852 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400853 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500854 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
855 "Linear Metrics", "Non-Linear Metrics");
856 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
857 "Bitmap Text", "No Bitmap Text");
858 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
859
860 if (fFontOverrides.fEdging) {
861 switch (fFont.getEdging()) {
862 case SkFont::Edging::kAlias:
863 paintTitle.append("Alias Text");
864 break;
865 case SkFont::Edging::kAntiAlias:
866 paintTitle.append("Antialias Text");
867 break;
868 case SkFont::Edging::kSubpixelAntiAlias:
869 paintTitle.append("Subpixel Antialias Text");
870 break;
871 }
872 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400873
Mike Reed3ae47332019-01-04 10:11:46 -0500874 if (fFontOverrides.fHinting) {
875 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400876 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500877 paintTitle.append("No Hinting");
878 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400879 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500880 paintTitle.append("Slight Hinting");
881 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400882 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500883 paintTitle.append("Normal Hinting");
884 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400885 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500886 paintTitle.append("Full Hinting");
887 break;
888 }
889 }
890 paintTitle.done();
891
Brian Osman92004802017-03-06 11:47:26 -0500892 switch (fColorMode) {
893 case ColorMode::kLegacy:
894 title.append(" Legacy 8888");
895 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500896 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500897 title.append(" ColorManaged 8888");
898 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500899 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500900 title.append(" ColorManaged F16");
901 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400902 case ColorMode::kColorManagedF16Norm:
903 title.append(" ColorManaged F16 Norm");
904 break;
Brian Osman92004802017-03-06 11:47:26 -0500905 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500906
Brian Osman92004802017-03-06 11:47:26 -0500907 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500908 int curPrimaries = -1;
909 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
910 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
911 curPrimaries = i;
912 break;
913 }
914 }
Brian Osman03115dc2018-11-26 13:55:19 -0500915 title.appendf(" %s Gamma %f",
916 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500917 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700918 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500919
Ben Wagner37c54032018-04-13 14:30:23 -0400920 const DisplayParams& params = fWindow->getRequestedDisplayParams();
921 if (fPixelGeometryOverrides) {
922 switch (params.fSurfaceProps.pixelGeometry()) {
923 case kUnknown_SkPixelGeometry:
924 title.append( " Flat");
925 break;
926 case kRGB_H_SkPixelGeometry:
927 title.append( " RGB");
928 break;
929 case kBGR_H_SkPixelGeometry:
930 title.append( " BGR");
931 break;
932 case kRGB_V_SkPixelGeometry:
933 title.append( " RGBV");
934 break;
935 case kBGR_V_SkPixelGeometry:
936 title.append( " BGRV");
937 break;
938 }
939 }
940
941 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
942 title.append(" DFT");
943 }
944
csmartdalton578f0642017-02-24 16:04:47 -0700945 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700946 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500947 int msaa = fWindow->sampleCount();
948 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700949 title.appendf(" MSAA: %i", msaa);
950 }
951 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700952
953 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -0700954 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700955 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
956 }
957
Brian Osman805a7272018-05-02 15:40:20 -0400958 if (kPerspective_Real == fPerspectiveMode) {
959 title.append(" Perpsective (Real)");
960 } else if (kPerspective_Fake == fPerspectiveMode) {
961 title.append(" Perspective (Fake)");
962 }
963
brianosman05de2162016-05-06 13:28:57 -0700964 fWindow->setTitle(title.c_str());
965}
966
Florin Malitaab99c342018-01-16 16:23:03 -0500967int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500968
969 if (!FLAGS_slide.isEmpty()) {
970 int count = fSlides.count();
971 for (int i = 0; i < count; i++) {
972 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500973 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500974 }
975 }
976
977 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
978 this->listNames();
979 }
980
Florin Malitaab99c342018-01-16 16:23:03 -0500981 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500982}
983
Florin Malitaab99c342018-01-16 16:23:03 -0500984void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500985 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500986 for (const auto& slide : fSlides) {
987 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500988 }
989}
990
Florin Malitaab99c342018-01-16 16:23:03 -0500991void Viewer::setCurrentSlide(int slide) {
992 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700993
Florin Malitaab99c342018-01-16 16:23:03 -0500994 if (slide == fCurrentSlide) {
995 return;
996 }
997
998 if (fCurrentSlide >= 0) {
999 fSlides[fCurrentSlide]->unload();
1000 }
1001
1002 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1003 SkIntToScalar(fWindow->height()));
1004 fCurrentSlide = slide;
1005 this->setupCurrentSlide();
1006}
1007
1008void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001009 if (fCurrentSlide >= 0) {
1010 // prepare dimensions for image slides
1011 fGesture.resetTouchState();
1012 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001013
Jim Van Verth0848fb02018-01-22 13:39:30 -05001014 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1015 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1016 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001017
Jim Van Verth0848fb02018-01-22 13:39:30 -05001018 // Start with a matrix that scales the slide to the available screen space
1019 if (fWindow->scaleContentToFit()) {
1020 if (windowRect.width() > 0 && windowRect.height() > 0) {
1021 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1022 }
liyuqiane46e4f02016-05-20 07:32:19 -07001023 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001024
1025 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001026 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001027
1028 this->updateTitle();
1029 this->updateUIState();
1030
1031 fStatsLayer.resetMeasurements();
1032
1033 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001034 }
jvanverthc265a922016-04-08 12:51:45 -07001035}
1036
1037#define MAX_ZOOM_LEVEL 8
1038#define MIN_ZOOM_LEVEL -8
1039
jvanverth34524262016-05-04 13:49:13 -07001040void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001041 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -04001042 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001043 this->preTouchMatrixChanged();
1044}
Yuqian Li755778c2018-03-28 16:23:31 -04001045
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001046void Viewer::preTouchMatrixChanged() {
1047 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001048 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1049 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1050 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1051 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1052}
1053
Brian Osman805a7272018-05-02 15:40:20 -04001054SkMatrix Viewer::computePerspectiveMatrix() {
1055 SkScalar w = fWindow->width(), h = fWindow->height();
1056 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1057 SkPoint perspPts[4] = {
1058 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1059 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1060 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1061 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1062 };
1063 SkMatrix m;
1064 m.setPolyToPoly(orthoPts, perspPts, 4);
1065 return m;
1066}
1067
Yuqian Li755778c2018-03-28 16:23:31 -04001068SkMatrix Viewer::computePreTouchMatrix() {
1069 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001070
1071 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001072 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001073 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001074
1075 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1076 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001077
Brian Osman805a7272018-05-02 15:40:20 -04001078 if (kPerspective_Real == fPerspectiveMode) {
1079 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001080 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001081 }
1082
Yuqian Li755778c2018-03-28 16:23:31 -04001083 return m;
jvanverthc265a922016-04-08 12:51:45 -07001084}
1085
liyuqiand3cdbca2016-05-17 12:44:20 -07001086SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001087 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001088 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001089 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001090 return m;
jvanverthc265a922016-04-08 12:51:45 -07001091}
1092
Brian Osman621491e2017-02-28 15:45:01 -05001093void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001094 fPersistentCache.reset();
1095 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001096 fBackendType = backendType;
1097
1098 fWindow->detach();
1099
Brian Osman70d2f432017-11-08 09:54:10 -05001100#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001101 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1102 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001103 DisplayParams params = fWindow->getRequestedDisplayParams();
1104 delete fWindow;
1105 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001106
Brian Osman70d2f432017-11-08 09:54:10 -05001107 // re-register callbacks
1108 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001109 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001110 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001111 fWindow->pushLayer(&fImGuiLayer);
1112
Brian Osman70d2f432017-11-08 09:54:10 -05001113 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1114 // will still include our correct sample count. But the re-created fWindow will lose that
1115 // information. On Windows, we need to re-create the window when changing sample count,
1116 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1117 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1118 // as if we had never changed windows at all).
1119 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001120#endif
1121
Brian Osman70d2f432017-11-08 09:54:10 -05001122 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001123}
1124
Brian Osman92004802017-03-06 11:47:26 -05001125void Viewer::setColorMode(ColorMode colorMode) {
1126 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001127 this->updateTitle();
1128 fWindow->inval();
1129}
1130
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001131class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1132public:
Mike Reed3ae47332019-01-04 10:11:46 -05001133 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1134 SkFont* font, Viewer::SkFontFields* ffields)
1135 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001136 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001137 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1138 sk_sp<SkTextBlob>* cache) {
1139 bool blobWillChange = false;
1140 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001141 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1142 bool shouldDraw = this->filterFont(&filteredFont);
1143 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001144 blobWillChange = true;
1145 break;
1146 }
1147 }
1148 if (!blobWillChange) {
1149 return blob;
1150 }
1151
1152 SkTextBlobBuilder builder;
1153 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001154 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1155 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001156 if (!shouldDraw) {
1157 continue;
1158 }
1159
Mike Reed3ae47332019-01-04 10:11:46 -05001160 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001161
Ben Wagner41e40472018-09-24 13:01:54 -04001162 const SkTextBlobBuilder::RunBuffer& runBuffer
1163 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001164 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001165 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001166 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001167 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001168 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001169 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001170 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001171 it.glyphCount(), it.textSize(), SkString())
1172 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1173 uint32_t glyphCount = it.glyphCount();
1174 if (it.glyphs()) {
1175 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1176 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1177 }
1178 if (it.pos()) {
1179 size_t posSize = sizeof(decltype(*it.pos()));
1180 uint8_t positioning = it.positioning();
1181 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1182 }
1183 if (it.text()) {
1184 size_t textSize = sizeof(decltype(*it.text()));
1185 uint32_t textCount = it.textSize();
1186 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1187 }
1188 if (it.clusters()) {
1189 size_t clusterSize = sizeof(decltype(*it.clusters()));
1190 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1191 }
1192 }
1193 *cache = builder.make();
1194 return cache->get();
1195 }
1196 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1197 const SkPaint& paint) override {
1198 sk_sp<SkTextBlob> cache;
1199 this->SkPaintFilterCanvas::onDrawTextBlob(
1200 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1201 }
Mike Reed3ae47332019-01-04 10:11:46 -05001202 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001203 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001204 font->writable()->setSize(fFont->getSize());
1205 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001206 if (fFontOverrides->fScaleX) {
1207 font->writable()->setScaleX(fFont->getScaleX());
1208 }
1209 if (fFontOverrides->fSkewX) {
1210 font->writable()->setSkewX(fFont->getSkewX());
1211 }
Mike Reed3ae47332019-01-04 10:11:46 -05001212 if (fFontOverrides->fHinting) {
1213 font->writable()->setHinting(fFont->getHinting());
1214 }
Ben Wagner9613e452019-01-23 10:34:59 -05001215 if (fFontOverrides->fEdging) {
1216 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001217 }
Ben Wagner9613e452019-01-23 10:34:59 -05001218 if (fFontOverrides->fEmbolden) {
1219 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001220 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001221 if (fFontOverrides->fBaselineSnap) {
1222 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1223 }
Ben Wagner9613e452019-01-23 10:34:59 -05001224 if (fFontOverrides->fLinearMetrics) {
1225 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001226 }
Ben Wagner9613e452019-01-23 10:34:59 -05001227 if (fFontOverrides->fSubpixel) {
1228 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001229 }
Ben Wagner9613e452019-01-23 10:34:59 -05001230 if (fFontOverrides->fEmbeddedBitmaps) {
1231 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001232 }
Ben Wagner9613e452019-01-23 10:34:59 -05001233 if (fFontOverrides->fForceAutoHinting) {
1234 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001235 }
Ben Wagner9613e452019-01-23 10:34:59 -05001236
Mike Reed3ae47332019-01-04 10:11:46 -05001237 return true;
1238 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001239 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001240 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001241 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001242 }
Ben Wagner9613e452019-01-23 10:34:59 -05001243 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001244 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001245 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001246 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001247 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001248 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001249 return true;
1250 }
1251 SkPaint* fPaint;
1252 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001253 SkFont* fFont;
1254 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001255};
1256
Robert Phillips9882dae2019-03-04 11:00:10 -05001257void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001258 if (fCurrentSlide < 0) {
1259 return;
1260 }
1261
Robert Phillips9882dae2019-03-04 11:00:10 -05001262 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001263
Brian Osmanf750fbc2017-02-08 10:47:28 -05001264 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001265 SkSurface* slideSurface = surface;
1266 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001267 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001268
Brian Osmane0d4fba2017-03-15 10:24:55 -04001269 // 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 -05001270 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001271 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001272 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001273 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001274 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001275 }
1276
Brian Osman3ac99cf2017-12-01 11:23:53 -05001277 if (fSaveToSKP) {
1278 SkPictureRecorder recorder;
1279 SkCanvas* recorderCanvas = recorder.beginRecording(
1280 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001281 fSlides[fCurrentSlide]->draw(recorderCanvas);
1282 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1283 SkFILEWStream stream("sample_app.skp");
1284 picture->serialize(&stream);
1285 fSaveToSKP = false;
1286 }
1287
Brian Osmane9ed0f02018-11-26 14:50:05 -05001288 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001289 SkColorType colorType;
1290 switch (fColorMode) {
1291 case ColorMode::kLegacy:
1292 case ColorMode::kColorManaged8888:
1293 colorType = kN32_SkColorType;
1294 break;
1295 case ColorMode::kColorManagedF16:
1296 colorType = kRGBA_F16_SkColorType;
1297 break;
1298 case ColorMode::kColorManagedF16Norm:
1299 colorType = kRGBA_F16Norm_SkColorType;
1300 break;
1301 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001302
1303 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001304 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1305 slideCanvas->getProps(&props);
1306
Brian Osmane9ed0f02018-11-26 14:50:05 -05001307 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1308 return Window::kRaster_BackendType == this->fBackendType
1309 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001310 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001311 };
1312
Brian Osman03115dc2018-11-26 13:55:19 -05001313 // We need to render offscreen if we're...
1314 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1315 // ... in any raster mode, because the window surface is actually GL
1316 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001317 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001318 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001319 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001320 Window::kRaster_BackendType == fBackendType ||
1321 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001322
Brian Osmane9ed0f02018-11-26 14:50:05 -05001323 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001324 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001325 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001326 }
1327
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001328 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001329 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001330 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001331 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001332 if (fTiled) {
1333 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1334 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1335 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1336 SkCanvas* tileCanvas = tileSurface->getCanvas();
1337 SkMatrix m = this->computeMatrix();
1338 for (int y = 0; y < fWindow->height(); y += tileH) {
1339 for (int x = 0; x < fWindow->width(); x += tileW) {
1340 SkAutoCanvasRestore acr(tileCanvas, true);
1341 tileCanvas->translate(-x, -y);
1342 tileCanvas->clear(SK_ColorTRANSPARENT);
1343 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001344 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1345 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001346 fSlides[fCurrentSlide]->draw(&filterCanvas);
1347 tileSurface->draw(slideCanvas, x, y, nullptr);
1348 }
1349 }
1350
1351 // Draw borders between tiles
1352 if (fDrawTileBoundaries) {
1353 SkPaint border;
1354 border.setColor(0x60FF00FF);
1355 border.setStyle(SkPaint::kStroke_Style);
1356 for (int y = 0; y < fWindow->height(); y += tileH) {
1357 for (int x = 0; x < fWindow->width(); x += tileW) {
1358 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1359 }
1360 }
1361 }
1362 } else {
1363 slideCanvas->concat(this->computeMatrix());
1364 if (kPerspective_Real == fPerspectiveMode) {
1365 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1366 }
Mike Reed3ae47332019-01-04 10:11:46 -05001367 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001368 fSlides[fCurrentSlide]->draw(&filterCanvas);
1369 }
Brian Osman56a24812017-12-19 11:15:16 -05001370 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001371 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001372
1373 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001374 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001375 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001376 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001377
1378 // If we rendered offscreen, snap an image and push the results to the window's canvas
1379 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001380 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001381
Robert Phillips9882dae2019-03-04 11:00:10 -05001382 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001383 SkPaint paint;
1384 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001385 int prePerspectiveCount = canvas->save();
1386 if (kPerspective_Fake == fPerspectiveMode) {
1387 paint.setFilterQuality(kHigh_SkFilterQuality);
1388 canvas->clear(SK_ColorWHITE);
1389 canvas->concat(this->computePerspectiveMatrix());
1390 }
Brian Osman03115dc2018-11-26 13:55:19 -05001391 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001392 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001393 }
Mike Reed376d8122019-03-14 11:39:02 -04001394
1395 if (fShowSlideDimensions) {
1396 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1397 SkPaint paint;
1398 paint.setColor(0x40FFFF00);
1399 surface->getCanvas()->drawRect(r, paint);
1400 }
liyuqian6f163d22016-06-13 12:26:45 -07001401}
1402
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001403void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001404 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001405 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001406}
Jim Van Verth6f449692017-02-14 15:16:46 -05001407
Robert Phillips9882dae2019-03-04 11:00:10 -05001408void Viewer::onPaint(SkSurface* surface) {
1409 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001410
Robert Phillips9882dae2019-03-04 11:00:10 -05001411 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001412
Brian Osmand67e5182017-12-08 16:46:09 -05001413 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001414
1415 if (GrContext* ctx = fWindow->getGrContext()) {
1416 // Clean out cache items that haven't been used in more than 10 seconds.
1417 ctx->performDeferredCleanup(std::chrono::seconds(10));
1418 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001419}
1420
Ben Wagnera1915972018-08-09 15:06:19 -04001421void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001422 if (fCurrentSlide >= 0) {
1423 fSlides[fCurrentSlide]->resize(width, height);
1424 }
Ben Wagnera1915972018-08-09 15:06:19 -04001425}
1426
Florin Malitacefc1b92018-02-19 21:43:47 -05001427SkPoint Viewer::mapEvent(float x, float y) {
1428 const auto m = this->computeMatrix();
1429 SkMatrix inv;
1430
1431 SkAssertResult(m.invert(&inv));
1432
1433 return inv.mapXY(x, y);
1434}
1435
Hal Canaryb1f411a2019-08-29 10:39:22 -04001436bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001437 if (GestureDevice::kMouse == fGestureDevice) {
1438 return false;
1439 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001440
1441 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001442 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001443 fWindow->inval();
1444 return true;
1445 }
1446
liyuqiand3cdbca2016-05-17 12:44:20 -07001447 void* castedOwner = reinterpret_cast<void*>(owner);
1448 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001449 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001450 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001451#if defined(SK_BUILD_FOR_IOS)
1452 // TODO: move IOS swipe detection higher up into the platform code
1453 SkPoint dir;
1454 if (fGesture.isFling(&dir)) {
1455 // swiping left or right
1456 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1457 if (dir.fX < 0) {
1458 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1459 fCurrentSlide + 1 : 0);
1460 } else {
1461 this->setCurrentSlide(fCurrentSlide > 0 ?
1462 fCurrentSlide - 1 : fSlides.count() - 1);
1463 }
1464 }
1465 fGesture.reset();
1466 }
1467#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001468 break;
1469 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001470 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001471 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001472 break;
1473 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001474 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001475 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001476 break;
1477 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001478 default: {
1479 // kLeft and kRight are only for swipes
1480 SkASSERT(false);
1481 break;
1482 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001483 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001484 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001485 fWindow->inval();
1486 return true;
1487}
1488
Hal Canaryb1f411a2019-08-29 10:39:22 -04001489bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001490 if (GestureDevice::kTouch == fGestureDevice) {
1491 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001492 }
Brian Osman16c81a12017-12-20 11:58:34 -05001493
Florin Malitacefc1b92018-02-19 21:43:47 -05001494 const auto slidePt = this->mapEvent(x, y);
1495 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1496 fWindow->inval();
1497 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001498 }
1499
1500 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001501 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001502 fGesture.touchEnd(nullptr);
1503 break;
1504 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001505 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001506 fGesture.touchBegin(nullptr, x, y);
1507 break;
1508 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001509 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001510 fGesture.touchMoved(nullptr, x, y);
1511 break;
1512 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001513 default: {
1514 SkASSERT(false); // shouldn't see kRight or kLeft here
1515 break;
1516 }
Brian Osman16c81a12017-12-20 11:58:34 -05001517 }
1518 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1519
Hal Canaryb1f411a2019-08-29 10:39:22 -04001520 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001521 fWindow->inval();
1522 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001523 return true;
1524}
1525
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001526bool Viewer::onFling(skui::InputState state) {
1527 if (skui::InputState::kRight == state) {
1528 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1529 return true;
1530 } else if (skui::InputState::kLeft == state) {
1531 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1532 return true;
1533 }
1534 return false;
1535}
1536
1537bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1538 switch (state) {
1539 case skui::InputState::kDown:
1540 fGesture.startZoom();
1541 return true;
1542 break;
1543 case skui::InputState::kMove:
1544 fGesture.updateZoom(scale, x, y, x, y);
1545 return true;
1546 break;
1547 case skui::InputState::kUp:
1548 fGesture.endZoom();
1549 return true;
1550 break;
1551 default:
1552 SkASSERT(false);
1553 break;
1554 }
1555
1556 return false;
1557}
1558
Brian Osmana109e392017-02-24 09:49:14 -05001559static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001560 // The gamut image covers a (0.8 x 0.9) shaped region
1561 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001562
1563 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1564 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1565 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001566 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1567 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1568 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001569
Brian Osman535c5e32019-02-09 16:32:58 -05001570 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1571 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1572 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1573 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1574 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001575}
1576
Ben Wagner3627d2e2018-06-26 14:23:20 -04001577static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001578 ImGui::DragCanvas dc(pt);
1579 dc.fillColor(IM_COL32(0, 0, 0, 128));
1580 dc.dragPoint(pt);
1581 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001582}
1583
Brian Osman9bb47cf2018-04-26 15:55:00 -04001584static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001585 ImGui::DragCanvas dc(pts);
1586 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001587
Brian Osman535c5e32019-02-09 16:32:58 -05001588 for (int i = 0; i < 4; ++i) {
1589 dc.dragPoint(pts + i);
1590 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001591
Brian Osman535c5e32019-02-09 16:32:58 -05001592 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1593 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1594 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1595 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001596
Brian Osman535c5e32019-02-09 16:32:58 -05001597 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001598}
1599
Brian Osmand67e5182017-12-08 16:46:09 -05001600void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001601 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1602 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001603 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001604 }
1605
1606 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001607 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1608 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001609 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001610 DisplayParams params = fWindow->getRequestedDisplayParams();
1611 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001612 const GrContext* ctx = fWindow->getGrContext();
1613
Brian Osmana109e392017-02-24 09:49:14 -05001614 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1615 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001616 if (ImGui::CollapsingHeader("Backend")) {
1617 int newBackend = static_cast<int>(fBackendType);
1618 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1619 ImGui::SameLine();
1620 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001621#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1622 ImGui::SameLine();
1623 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1624#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001625#if defined(SK_DAWN)
1626 ImGui::SameLine();
1627 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1628#endif
Brian Osman621491e2017-02-28 15:45:01 -05001629#if defined(SK_VULKAN)
1630 ImGui::SameLine();
1631 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1632#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001633#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001634 ImGui::SameLine();
1635 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1636#endif
Brian Osman621491e2017-02-28 15:45:01 -05001637 if (newBackend != fBackendType) {
1638 fDeferredActions.push_back([=]() {
1639 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1640 });
1641 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001642
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001643 bool* wire = &params.fGrContextOptions.fWireframeMode;
1644 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1645 paramsChanged = true;
1646 }
Brian Salomon99a33902017-03-07 15:16:34 -05001647
Brian Osman28b12522017-03-08 17:10:24 -05001648 if (ctx) {
1649 int sampleCount = fWindow->sampleCount();
1650 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001651 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001652 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1653 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1654 ImGui::RadioButton("16", &sampleCount, 16);
1655
1656 if (sampleCount != params.fMSAASampleCount) {
1657 params.fMSAASampleCount = sampleCount;
1658 paramsChanged = true;
1659 }
1660 }
1661
Ben Wagner37c54032018-04-13 14:30:23 -04001662 int pixelGeometryIdx = 0;
1663 if (fPixelGeometryOverrides) {
1664 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1665 }
1666 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1667 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1668 {
1669 uint32_t flags = params.fSurfaceProps.flags();
1670 if (pixelGeometryIdx == 0) {
1671 fPixelGeometryOverrides = false;
1672 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1673 } else {
1674 fPixelGeometryOverrides = true;
1675 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1676 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1677 }
1678 paramsChanged = true;
1679 }
1680
1681 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1682 if (ImGui::Checkbox("DFT", &useDFT)) {
1683 uint32_t flags = params.fSurfaceProps.flags();
1684 if (useDFT) {
1685 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1686 } else {
1687 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1688 }
1689 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1690 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1691 paramsChanged = true;
1692 }
1693
Brian Osman8a9de3d2017-03-01 14:59:05 -05001694 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001695 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001696 auto prButton = [&](GpuPathRenderers x) {
1697 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001698 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1699 params.fGrContextOptions.fGpuPathRenderers = x;
1700 paramsChanged = true;
1701 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001702 }
1703 };
1704
1705 if (!ctx) {
1706 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001707 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001708 const auto* caps = ctx->priv().caps();
1709 prButton(GpuPathRenderers::kDefault);
1710 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
1711 if (caps->shaderCaps()->pathRenderingSupport()) {
1712 prButton(GpuPathRenderers::kStencilAndCover);
1713 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001714 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001715 if (1 == fWindow->sampleCount()) {
1716 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1717 prButton(GpuPathRenderers::kCoverageCounting);
1718 }
1719 prButton(GpuPathRenderers::kSmall);
1720 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001721 prButton(GpuPathRenderers::kTessellating);
1722 prButton(GpuPathRenderers::kNone);
1723 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001724 ImGui::TreePop();
1725 }
Brian Osman621491e2017-02-28 15:45:01 -05001726 }
1727
Ben Wagner964571d2019-03-08 12:35:06 -05001728 if (ImGui::CollapsingHeader("Tiling")) {
1729 ImGui::Checkbox("Enable", &fTiled);
1730 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1731 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1732 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1733 }
1734
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001735 if (ImGui::CollapsingHeader("Transform")) {
1736 float zoom = fZoomLevel;
1737 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1738 fZoomLevel = zoom;
1739 this->preTouchMatrixChanged();
1740 paramsChanged = true;
1741 }
1742 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001743 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001744 fRotation = deg;
1745 this->preTouchMatrixChanged();
1746 paramsChanged = true;
1747 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001748 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1749 if (ImGui_DragLocation(&fOffset)) {
1750 this->preTouchMatrixChanged();
1751 paramsChanged = true;
1752 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001753 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1754 this->preTouchMatrixChanged();
1755 paramsChanged = true;
1756 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001757 }
Brian Osman805a7272018-05-02 15:40:20 -04001758 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1759 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1760 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001761 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001762 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001763 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001764 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001765 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001766 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001767 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001768 }
1769
Ben Wagnera580fb32018-04-17 11:16:32 -04001770 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001771 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001772 if (fPaintOverrides.fAntiAlias) {
1773 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001774 }
1775 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001776 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001777 {
1778 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1779 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001780 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001781 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1782 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001783 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001784 fPaintOverrides.fAntiAlias = true;
1785 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001786 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001787 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001788 case SkPaintFields::AntiAliasState::Alias:
1789 break;
1790 case SkPaintFields::AntiAliasState::Normal:
1791 break;
1792 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1793 gSkUseAnalyticAA = true;
1794 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001795 break;
1796 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1797 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001798 break;
1799 }
1800 }
1801 paramsChanged = true;
1802 }
1803
Ben Wagner99a78dc2018-05-09 18:23:51 -04001804 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001805 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001806 bool (SkPaint::* isFlag)() const,
1807 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001808 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001809 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001810 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001811 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001812 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001813 if (ImGui::Combo(label, &itemIndex, items)) {
1814 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001815 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001816 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001817 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001818 (fPaint.*setFlag)(itemIndex == 2);
1819 }
1820 paramsChanged = true;
1821 }
1822 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001823
Ben Wagner99a78dc2018-05-09 18:23:51 -04001824 paintFlag("Dither",
1825 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001826 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001827 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001828
1829 int filterQualityIdx = 0;
1830 if (fPaintOverrides.fFilterQuality) {
1831 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1832 }
1833 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1834 "Default\0None\0Low\0Medium\0High\0\0"))
1835 {
1836 if (filterQualityIdx == 0) {
1837 fPaintOverrides.fFilterQuality = false;
1838 fPaint.setFilterQuality(kNone_SkFilterQuality);
1839 } else {
1840 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1841 fPaintOverrides.fFilterQuality = true;
1842 }
1843 paramsChanged = true;
1844 }
Ben Wagner9613e452019-01-23 10:34:59 -05001845 }
Hal Canary02738a82019-01-21 18:51:32 +00001846
Ben Wagner9613e452019-01-23 10:34:59 -05001847 if (ImGui::CollapsingHeader("Font")) {
1848 int hintingIdx = 0;
1849 if (fFontOverrides.fHinting) {
1850 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1851 }
1852 if (ImGui::Combo("Hinting", &hintingIdx,
1853 "Default\0None\0Slight\0Normal\0Full\0\0"))
1854 {
1855 if (hintingIdx == 0) {
1856 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001857 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001858 } else {
1859 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1860 fFontOverrides.fHinting = true;
1861 }
1862 paramsChanged = true;
1863 }
Hal Canary02738a82019-01-21 18:51:32 +00001864
Ben Wagner9613e452019-01-23 10:34:59 -05001865 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1866 bool SkFontFields::* flag,
1867 bool (SkFont::* isFlag)() const,
1868 void (SkFont::* setFlag)(bool) )
1869 {
1870 int itemIndex = 0;
1871 if (fFontOverrides.*flag) {
1872 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1873 }
1874 if (ImGui::Combo(label, &itemIndex, items)) {
1875 if (itemIndex == 0) {
1876 fFontOverrides.*flag = false;
1877 } else {
1878 fFontOverrides.*flag = true;
1879 (fFont.*setFlag)(itemIndex == 2);
1880 }
1881 paramsChanged = true;
1882 }
1883 };
Hal Canary02738a82019-01-21 18:51:32 +00001884
Ben Wagner9613e452019-01-23 10:34:59 -05001885 fontFlag("Fake Bold Glyphs",
1886 "Default\0No Fake Bold\0Fake Bold\0\0",
1887 &SkFontFields::fEmbolden,
1888 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001889
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001890 fontFlag("Baseline Snapping",
1891 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1892 &SkFontFields::fBaselineSnap,
1893 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1894
Ben Wagner9613e452019-01-23 10:34:59 -05001895 fontFlag("Linear Text",
1896 "Default\0No Linear Text\0Linear Text\0\0",
1897 &SkFontFields::fLinearMetrics,
1898 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001899
Ben Wagner9613e452019-01-23 10:34:59 -05001900 fontFlag("Subpixel Position Glyphs",
1901 "Default\0Pixel Text\0Subpixel Text\0\0",
1902 &SkFontFields::fSubpixel,
1903 &SkFont::isSubpixel, &SkFont::setSubpixel);
1904
1905 fontFlag("Embedded Bitmap Text",
1906 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1907 &SkFontFields::fEmbeddedBitmaps,
1908 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1909
1910 fontFlag("Force Auto-Hinting",
1911 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1912 &SkFontFields::fForceAutoHinting,
1913 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1914
1915 int edgingIdx = 0;
1916 if (fFontOverrides.fEdging) {
1917 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1918 }
1919 if (ImGui::Combo("Edging", &edgingIdx,
1920 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1921 {
1922 if (edgingIdx == 0) {
1923 fFontOverrides.fEdging = false;
1924 fFont.setEdging(SkFont::Edging::kAlias);
1925 } else {
1926 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1927 fFontOverrides.fEdging = true;
1928 }
1929 paramsChanged = true;
1930 }
1931
Ben Wagner15a8d572019-03-21 13:35:44 -04001932 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1933 if (fFontOverrides.fSize) {
1934 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001935 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001936 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001937 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001938 fFontOverrides.fSizeRange[0],
1939 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001940 "%.6f", 2.0f))
1941 {
Mike Reed3ae47332019-01-04 10:11:46 -05001942 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001943 paramsChanged = true;
1944 }
1945 }
1946
1947 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1948 if (fFontOverrides.fScaleX) {
1949 float scaleX = fFont.getScaleX();
1950 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1951 fFont.setScaleX(scaleX);
1952 paramsChanged = true;
1953 }
1954 }
1955
1956 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1957 if (fFontOverrides.fSkewX) {
1958 float skewX = fFont.getSkewX();
1959 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1960 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001961 paramsChanged = true;
1962 }
1963 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001964 }
1965
Mike Reed81f60ec2018-05-15 10:09:52 -04001966 {
1967 SkMetaData controls;
1968 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1969 if (ImGui::CollapsingHeader("Current Slide")) {
1970 SkMetaData::Iter iter(controls);
1971 const char* name;
1972 SkMetaData::Type type;
1973 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001974 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001975 if (type == SkMetaData::kScalar_Type) {
1976 float val[3];
1977 SkASSERT(count == 3);
1978 controls.findScalars(name, &count, val);
1979 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1980 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001981 }
Ben Wagner110c7032019-03-22 17:03:59 -04001982 } else if (type == SkMetaData::kBool_Type) {
1983 bool val;
1984 SkASSERT(count == 1);
1985 controls.findBool(name, &val);
1986 if (ImGui::Checkbox(name, &val)) {
1987 controls.setBool(name, val);
1988 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001989 }
1990 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001991 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001992 }
1993 }
1994 }
1995
Ben Wagner7a3c6742018-04-23 10:01:07 -04001996 if (fShowSlidePicker) {
1997 ImGui::SetNextTreeNodeOpen(true);
1998 }
Brian Osman79086b92017-02-10 13:36:16 -05001999 if (ImGui::CollapsingHeader("Slide")) {
2000 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002001 static ImVector<const char*> filteredSlideNames;
2002 static ImVector<int> filteredSlideIndices;
2003
Brian Osmanfce09c52017-11-14 15:32:20 -05002004 if (fShowSlidePicker) {
2005 ImGui::SetKeyboardFocusHere();
2006 fShowSlidePicker = false;
2007 }
2008
Brian Osman79086b92017-02-10 13:36:16 -05002009 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002010 filteredSlideNames.clear();
2011 filteredSlideIndices.clear();
2012 int filteredIndex = 0;
2013 for (int i = 0; i < fSlides.count(); ++i) {
2014 const char* slideName = fSlides[i]->getName().c_str();
2015 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2016 if (i == fCurrentSlide) {
2017 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002018 }
Brian Osmanf479e422017-11-08 13:11:36 -05002019 filteredSlideNames.push_back(slideName);
2020 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002021 }
Brian Osman79086b92017-02-10 13:36:16 -05002022 }
Brian Osmanf479e422017-11-08 13:11:36 -05002023
Brian Osmanf479e422017-11-08 13:11:36 -05002024 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2025 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002026 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002027 }
2028 }
Brian Osmana109e392017-02-24 09:49:14 -05002029
2030 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002031 ColorMode newMode = fColorMode;
2032 auto cmButton = [&](ColorMode mode, const char* label) {
2033 if (ImGui::RadioButton(label, mode == fColorMode)) {
2034 newMode = mode;
2035 }
2036 };
2037
2038 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002039 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2040 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002041 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002042
2043 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002044 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002045 }
2046
2047 // Pick from common gamuts:
2048 int primariesIdx = 4; // Default: Custom
2049 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2050 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2051 primariesIdx = i;
2052 break;
2053 }
2054 }
2055
Brian Osman03115dc2018-11-26 13:55:19 -05002056 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002057 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002058
Brian Osmana109e392017-02-24 09:49:14 -05002059 if (ImGui::Combo("Primaries", &primariesIdx,
2060 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2061 if (primariesIdx >= 0 && primariesIdx <= 3) {
2062 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2063 }
2064 }
2065
2066 // Allow direct editing of gamut
2067 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2068 }
Brian Osman207d4102019-01-10 09:40:58 -05002069
2070 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002071 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002072 if (ImGui::Checkbox("Pause", &isPaused)) {
2073 fAnimTimer.togglePauseResume();
2074 }
Brian Osman707d2022019-01-10 11:27:34 -05002075
2076 float speed = fAnimTimer.getSpeed();
2077 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2078 fAnimTimer.setSpeed(speed);
2079 }
Brian Osman207d4102019-01-10 09:40:58 -05002080 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002081
Brian Osmanfd7657c2019-04-25 11:34:07 -04002082 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2083#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2084 || Window::kANGLE_BackendType == fBackendType
2085#endif
2086 ;
2087
2088 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2089 // than GL, we need to force it on. Just do that on the first frame after the backend
2090 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002091 if (!backendIsGL &&
2092 params.fGrContextOptions.fShaderCacheStrategy !=
2093 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2094 params.fGrContextOptions.fShaderCacheStrategy =
2095 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002096 paramsChanged = true;
2097 fPersistentCache.reset();
2098 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002099 // To re-load shaders from the currently active programs, we flush all caches on one
2100 // frame, then set a flag to poll the cache on the next frame.
2101 static bool gLoadPending = false;
2102 if (gLoadPending) {
2103 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2104 int hitCount) {
2105 CachedGLSL& entry(fCachedGLSL.push_back());
2106 entry.fKey = key;
2107 SkMD5 hash;
2108 hash.write(key->bytes(), key->size());
2109 SkMD5::Digest digest = hash.finish();
2110 for (int i = 0; i < 16; ++i) {
2111 entry.fKeyString.appendf("%02x", digest.data[i]);
2112 }
2113
Brian Osmana66081d2019-09-03 14:59:26 -04002114 SkReader32 reader(data->data(), data->size());
2115 entry.fShaderType = reader.readU32();
2116 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2117 entry.fInputs,
2118 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002119 };
2120 fCachedGLSL.reset();
2121 fPersistentCache.foreach(collectShaders);
2122 gLoadPending = false;
2123 }
2124
2125 // Defer actually doing the load/save logic so that we can trigger a save when we
2126 // start or finish hovering on a tree node in the list below:
2127 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002128 bool doSave = ImGui::Button("Save");
2129 if (backendIsGL) {
2130 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002131 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2132 GrContextOptions::ShaderCacheStrategy::kSkSL;
2133 if (ImGui::Checkbox("SkSL", &sksl)) {
2134 params.fGrContextOptions.fShaderCacheStrategy = sksl
2135 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2136 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002137 paramsChanged = true;
2138 doLoad = true;
2139 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2140 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002141 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002142
2143 ImGui::BeginChild("##ScrollingRegion");
2144 for (auto& entry : fCachedGLSL) {
2145 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2146 bool hovered = ImGui::IsItemHovered();
2147 if (hovered != entry.fHovered) {
2148 // Force a save to patch the highlight shader in/out
2149 entry.fHovered = hovered;
2150 doSave = true;
2151 }
2152 if (inTreeNode) {
2153 // Full width, and a reasonable amount of space for each shader.
2154 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2155 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2156 boxSize);
2157 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2158 boxSize);
2159 ImGui::TreePop();
2160 }
2161 }
2162 ImGui::EndChild();
2163
2164 if (doLoad) {
2165 fPersistentCache.reset();
2166 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2167 gLoadPending = true;
2168 }
2169 if (doSave) {
2170 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002171 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002172 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2173 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002174
Brian Osman072e6fc2019-06-12 11:35:41 -04002175 SkSL::String highlight;
2176 if (!sksl) {
2177 highlight = shaderCaps->versionDeclString();
2178 if (shaderCaps->usesPrecisionModifiers()) {
2179 highlight.append("precision mediump float;\n");
2180 }
Brian Osman5bee3902019-05-07 09:55:45 -04002181 }
2182 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002183 highlight.appendf("out %s sk_FragColor;\n"
2184 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2185 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002186
2187 fPersistentCache.reset();
2188 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2189 for (auto& entry : fCachedGLSL) {
2190 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2191 if (entry.fHovered) {
2192 entry.fShader[kFragment_GrShaderType] = highlight;
2193 }
2194
Brian Osmana085a412019-04-25 09:44:43 -04002195 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2196 entry.fShader,
2197 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002198 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002199 fPersistentCache.store(*entry.fKey, *data);
2200
2201 entry.fShader[kFragment_GrShaderType] = backup;
2202 }
2203 }
2204 }
Brian Osman79086b92017-02-10 13:36:16 -05002205 }
Brian Salomon99a33902017-03-07 15:16:34 -05002206 if (paramsChanged) {
2207 fDeferredActions.push_back([=]() {
2208 fWindow->setRequestedDisplayParams(params);
2209 fWindow->inval();
2210 this->updateTitle();
2211 });
2212 }
Brian Osman79086b92017-02-10 13:36:16 -05002213 ImGui::End();
2214 }
2215
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002216 if (gShaderErrorHandler.fErrors.count()) {
2217 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2218 ImGui::Begin("Shader Errors");
2219 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2220 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002221 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2222 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2223 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2224 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002225 }
2226 ImGui::End();
2227 gShaderErrorHandler.reset();
2228 }
2229
Brian Osmanf6877092017-02-13 09:39:57 -05002230 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002231 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2232 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002233 static int zoomFactor = 8;
2234 if (ImGui::Button("<<")) {
2235 zoomFactor = SkTMax(zoomFactor / 2, 4);
2236 }
2237 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2238 if (ImGui::Button(">>")) {
2239 zoomFactor = SkTMin(zoomFactor * 2, 32);
2240 }
Brian Osmanf6877092017-02-13 09:39:57 -05002241
Ben Wagner3627d2e2018-06-26 14:23:20 -04002242 if (!fZoomWindowFixed) {
2243 ImVec2 mousePos = ImGui::GetMousePos();
2244 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2245 }
2246 SkScalar x = fZoomWindowLocation.x();
2247 SkScalar y = fZoomWindowLocation.y();
2248 int xInt = SkScalarRoundToInt(x);
2249 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002250 ImVec2 avail = ImGui::GetContentRegionAvail();
2251
Brian Osmanead517d2017-11-13 15:36:36 -05002252 uint32_t pixel = 0;
2253 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002254 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002255 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002256 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002257 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002258 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002259 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2260 }
2261
Brian Osmand67e5182017-12-08 16:46:09 -05002262 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002263 // Translate so the region of the image that's under the mouse cursor is centered
2264 // in the zoom canvas:
2265 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002266 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2267 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002268 c->drawImage(this->fLastImage, 0, 0);
2269
2270 SkPaint outline;
2271 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002272 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002273 });
Brian Osmanf6877092017-02-13 09:39:57 -05002274 }
2275
2276 ImGui::End();
2277 }
Brian Osman79086b92017-02-10 13:36:16 -05002278}
2279
liyuqian2edb0f42016-07-06 14:11:32 -07002280void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002281 for (int i = 0; i < fDeferredActions.count(); ++i) {
2282 fDeferredActions[i]();
2283 }
2284 fDeferredActions.reset();
2285
Brian Osman56a24812017-12-19 11:15:16 -05002286 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002287 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002288 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002289 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002290
Brian Osman79086b92017-02-10 13:36:16 -05002291 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002292 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2293 // not be visible, though. So we need to redraw if there is at least one visible window, or
2294 // more than one active window. Newly created windows are active but not visible for one frame
2295 // while they determine their layout and sizing.
2296 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2297 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002298 fWindow->inval();
2299 }
jvanverth9f372462016-04-06 06:08:59 -07002300}
liyuqiane5a6cd92016-05-27 08:52:52 -07002301
Florin Malitab632df72018-06-18 21:23:06 -04002302template <typename OptionsFunc>
2303static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2304 OptionsFunc&& optionsFunc) {
2305 writer.beginObject();
2306 {
2307 writer.appendString(kName , name);
2308 writer.appendString(kValue, value);
2309
2310 writer.beginArray(kOptions);
2311 {
2312 optionsFunc(writer);
2313 }
2314 writer.endArray();
2315 }
2316 writer.endObject();
2317}
2318
2319
liyuqiane5a6cd92016-05-27 08:52:52 -07002320void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002321 if (!fWindow) {
2322 return;
2323 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002324 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002325 return; // Surface hasn't been created yet.
2326 }
2327
Florin Malitab632df72018-06-18 21:23:06 -04002328 SkDynamicMemoryWStream memStream;
2329 SkJSONWriter writer(&memStream);
2330 writer.beginArray();
2331
liyuqianb73c24b2016-06-03 08:47:23 -07002332 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002333 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2334 [this](SkJSONWriter& writer) {
2335 for(const auto& slide : fSlides) {
2336 writer.appendString(slide->getName().c_str());
2337 }
2338 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002339
liyuqianb73c24b2016-06-03 08:47:23 -07002340 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002341 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2342 [](SkJSONWriter& writer) {
2343 for (const auto& str : kBackendTypeStrings) {
2344 writer.appendString(str);
2345 }
2346 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002347
csmartdalton578f0642017-02-24 16:04:47 -07002348 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002349 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2350 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2351 [this](SkJSONWriter& writer) {
2352 writer.appendS32(0);
2353
2354 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2355 return;
2356 }
2357
2358 for (int msaa : {4, 8, 16}) {
2359 writer.appendS32(msaa);
2360 }
2361 });
csmartdalton578f0642017-02-24 16:04:47 -07002362
csmartdalton61cd31a2017-02-27 17:00:53 -07002363 // Path renderer state
2364 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002365 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2366 [this](SkJSONWriter& writer) {
2367 const GrContext* ctx = fWindow->getGrContext();
2368 if (!ctx) {
2369 writer.appendString("Software");
2370 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002371 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002372 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2373 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Florin Malitab632df72018-06-18 21:23:06 -04002374 if (caps->shaderCaps()->pathRenderingSupport()) {
2375 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002376 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002377 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002378 }
2379 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002380 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2381 writer.appendString(
2382 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2383 }
2384 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2385 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002386 writer.appendString(gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2387 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002388 }
2389 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002390
liyuqianb73c24b2016-06-03 08:47:23 -07002391 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002392 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2393 [this](SkJSONWriter& writer) {
2394 writer.appendString(kSoftkeyHint);
2395 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2396 writer.appendString(softkey.c_str());
2397 }
2398 });
liyuqianb73c24b2016-06-03 08:47:23 -07002399
Florin Malitab632df72018-06-18 21:23:06 -04002400 writer.endArray();
2401 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002402
Florin Malitab632df72018-06-18 21:23:06 -04002403 auto data = memStream.detachAsData();
2404
2405 // TODO: would be cool to avoid this copy
2406 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2407
2408 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002409}
2410
2411void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002412 // For those who will add more features to handle the state change in this function:
2413 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2414 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2415 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002416 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002417 for (int i = 0; i < fSlides.count(); ++i) {
2418 if (fSlides[i]->getName().equals(stateValue)) {
2419 this->setCurrentSlide(i);
2420 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002421 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002422 }
Florin Malitaab99c342018-01-16 16:23:03 -05002423
2424 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002425 } else if (stateName.equals(kBackendStateName)) {
2426 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2427 if (stateValue.equals(kBackendTypeStrings[i])) {
2428 if (fBackendType != i) {
2429 fBackendType = (sk_app::Window::BackendType)i;
2430 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002431 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002432 }
2433 break;
2434 }
2435 }
csmartdalton578f0642017-02-24 16:04:47 -07002436 } else if (stateName.equals(kMSAAStateName)) {
2437 DisplayParams params = fWindow->getRequestedDisplayParams();
2438 int sampleCount = atoi(stateValue.c_str());
2439 if (sampleCount != params.fMSAASampleCount) {
2440 params.fMSAASampleCount = sampleCount;
2441 fWindow->setRequestedDisplayParams(params);
2442 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002443 this->updateTitle();
2444 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002445 }
2446 } else if (stateName.equals(kPathRendererStateName)) {
2447 DisplayParams params = fWindow->getRequestedDisplayParams();
2448 for (const auto& pair : gPathRendererNames) {
2449 if (pair.second == stateValue.c_str()) {
2450 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2451 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2452 fWindow->setRequestedDisplayParams(params);
2453 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002454 this->updateTitle();
2455 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002456 }
2457 break;
2458 }
csmartdalton578f0642017-02-24 16:04:47 -07002459 }
liyuqianb73c24b2016-06-03 08:47:23 -07002460 } else if (stateName.equals(kSoftkeyStateName)) {
2461 if (!stateValue.equals(kSoftkeyHint)) {
2462 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002463 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002464 }
liyuqian2edb0f42016-07-06 14:11:32 -07002465 } else if (stateName.equals(kRefreshStateName)) {
2466 // This state is actually NOT in the UI state.
2467 // We use this to allow Android to quickly set bool fRefresh.
2468 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002469 } else {
2470 SkDebugf("Unknown stateName: %s", stateName.c_str());
2471 }
2472}
Brian Osman79086b92017-02-10 13:36:16 -05002473
Hal Canaryb1f411a2019-08-29 10:39:22 -04002474bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002475 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002476}
2477
Hal Canaryb1f411a2019-08-29 10:39:22 -04002478bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002479 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002480 fWindow->inval();
2481 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002482 } else {
2483 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002484 }
Brian Osman79086b92017-02-10 13:36:16 -05002485}