blob: 118de4ee39a5cfd42676d5ccb3da2d4dec665825 [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"
41#include "tools/viewer/SlideDir.h"
42#include "tools/viewer/SvgSlide.h"
43#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070044
Hal Canaryc640d0d2018-06-13 09:59:02 -040045#include <stdlib.h>
46#include <map>
47
Hal Canary8a001442018-09-19 11:31:27 -040048#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040049#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040050
Florin Malita87ccf332018-05-04 12:23:24 -040051#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040053#endif
54
Brian Osman5e7fbfd2019-05-03 13:13:35 -040055class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
56public:
57 void compileError(const char* shader, const char* errors) override {
58 fShaders.push_back(SkString(shader));
59 fErrors.push_back(SkString(errors));
60 }
61
62 void reset() {
63 fShaders.reset();
64 fErrors.reset();
65 }
66
67 SkTArray<SkString> fShaders;
68 SkTArray<SkString> fErrors;
69};
70
71static CapturingShaderErrorHandler gShaderErrorHandler;
72
jvanverth34524262016-05-04 13:49:13 -070073using namespace sk_app;
74
csmartdalton61cd31a2017-02-27 17:00:53 -070075static std::map<GpuPathRenderers, std::string> gPathRendererNames;
76
jvanverth9f372462016-04-06 06:08:59 -070077Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070078 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070079}
80
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060081static DEFINE_string(slide, "", "Start on this sample.");
82static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050083
Stephen Whitea800ec92019-08-02 15:04:52 -040084#if defined(SK_VULKAN)
jvanverthb8794cc2016-07-27 14:29:18 -070085# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050086#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
87# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
Stephen Whitea800ec92019-08-02 15:04:52 -040088#elif defined(SK_DAWN)
89# define BACKENDS_STR "\"sw\", \"gl\", and \"dawn\""
bsalomon6c471f72016-07-26 12:56:32 -070090#else
91# define BACKENDS_STR "\"sw\" and \"gl\""
92#endif
93
Brian Osman2dd96932016-10-18 15:33:53 -040094static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070095
Mike Klein5b3f3432019-03-21 11:42:21 -050096static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070097
Chris Dalton1e6c5b82019-06-17 14:16:49 -060098static DEFINE_int(internalSamples, 4,
99 "Number of samples for internal draws that use MSAA or mixed samples.");
100
Mike Klein84836b72019-03-21 11:31:36 -0500101static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700102
Mike Klein84836b72019-03-21 11:31:36 -0500103static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400104
Mike Kleinc6142d82019-03-25 10:54:59 -0500105static DEFINE_string2(match, m, nullptr,
106 "[~][^]substring[$] [...] of name to run.\n"
107 "Multiple matches may be separated by spaces.\n"
108 "~ causes a matching name to always be skipped\n"
109 "^ requires the start of the name to match\n"
110 "$ requires the end of the name to match\n"
111 "^ and $ requires an exact match\n"
112 "If a name does not match any list entry,\n"
113 "it is skipped unless some list entry starts with ~");
114
Mike Klein19fb3972019-03-21 13:08:08 -0500115#if defined(SK_BUILD_FOR_ANDROID)
116 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500117 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
118 static DEFINE_string(lotties, "/data/local/tmp/lotties",
119 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500120#else
121 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500122 static DEFINE_string(skps, "skps", "Directory to read skps from.");
123 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500124#endif
125
Mike Kleinc6142d82019-03-25 10:54:59 -0500126static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
127
128static DEFINE_int_2(threads, j, -1,
129 "Run threadsafe tests on a threadpool with this many extra threads, "
130 "defaulting to one extra thread per core.");
131
Jim Van Verth7b558182019-11-14 16:47:01 -0500132static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
133
Mike Kleinc6142d82019-03-25 10:54:59 -0500134
Brian Salomon194db172017-08-17 14:37:06 -0400135const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700136 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400137#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
138 "ANGLE",
139#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400140#ifdef SK_DAWN
141 "Dawn",
142#endif
jvanverth063ece72016-06-17 09:29:14 -0700143#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700144 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700145#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400146#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500147 "Metal",
148#endif
csmartdalton578f0642017-02-24 16:04:47 -0700149 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700150};
151
bsalomon6c471f72016-07-26 12:56:32 -0700152static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400153#ifdef SK_DAWN
154 if (0 == strcmp(str, "dawn")) {
155 return sk_app::Window::kDawn_BackendType;
156 } else
157#endif
bsalomon6c471f72016-07-26 12:56:32 -0700158#ifdef SK_VULKAN
159 if (0 == strcmp(str, "vk")) {
160 return sk_app::Window::kVulkan_BackendType;
161 } else
162#endif
Brian Salomon194db172017-08-17 14:37:06 -0400163#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
164 if (0 == strcmp(str, "angle")) {
165 return sk_app::Window::kANGLE_BackendType;
166 } else
167#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400168#ifdef SK_METAL
169 if (0 == strcmp(str, "mtl")) {
170 return sk_app::Window::kMetal_BackendType;
171 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500172#endif
bsalomon6c471f72016-07-26 12:56:32 -0700173 if (0 == strcmp(str, "gl")) {
174 return sk_app::Window::kNativeGL_BackendType;
175 } else if (0 == strcmp(str, "sw")) {
176 return sk_app::Window::kRaster_BackendType;
177 } else {
178 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
179 return sk_app::Window::kRaster_BackendType;
180 }
181}
182
Brian Osmana109e392017-02-24 09:49:14 -0500183static SkColorSpacePrimaries gSrgbPrimaries = {
184 0.64f, 0.33f,
185 0.30f, 0.60f,
186 0.15f, 0.06f,
187 0.3127f, 0.3290f };
188
189static SkColorSpacePrimaries gAdobePrimaries = {
190 0.64f, 0.33f,
191 0.21f, 0.71f,
192 0.15f, 0.06f,
193 0.3127f, 0.3290f };
194
195static SkColorSpacePrimaries gP3Primaries = {
196 0.680f, 0.320f,
197 0.265f, 0.690f,
198 0.150f, 0.060f,
199 0.3127f, 0.3290f };
200
201static SkColorSpacePrimaries gRec2020Primaries = {
202 0.708f, 0.292f,
203 0.170f, 0.797f,
204 0.131f, 0.046f,
205 0.3127f, 0.3290f };
206
207struct NamedPrimaries {
208 const char* fName;
209 SkColorSpacePrimaries* fPrimaries;
210} gNamedPrimaries[] = {
211 { "sRGB", &gSrgbPrimaries },
212 { "AdobeRGB", &gAdobePrimaries },
213 { "P3", &gP3Primaries },
214 { "Rec. 2020", &gRec2020Primaries },
215};
216
217static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
218 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
219}
220
Brian Osman70d2f432017-11-08 09:54:10 -0500221static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
222 // In raster mode, we still use GL for the window.
223 // This lets us render the GUI faster (and correct).
224 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
225}
226
Jim Van Verth74826c82019-03-01 14:37:30 -0500227class NullSlide : public Slide {
228 SkISize getDimensions() const override {
229 return SkISize::Make(640, 480);
230 }
231
232 void draw(SkCanvas* canvas) override {
233 canvas->clear(0xffff11ff);
234 }
235};
236
liyuqiane5a6cd92016-05-27 08:52:52 -0700237const char* kName = "name";
238const char* kValue = "value";
239const char* kOptions = "options";
240const char* kSlideStateName = "Slide";
241const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700242const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700243const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700244const char* kSoftkeyStateName = "Softkey";
245const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700246const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700247const char* kON = "ON";
248const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700249const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700250
jvanverth34524262016-05-04 13:49:13 -0700251Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500252 : fCurrentSlide(-1)
253 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500254 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400255 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500256 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500257 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500258 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500259 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400260 , fZoomWindowFixed(false)
261 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500262 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400263 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700264 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500265 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500266 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500267 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500268 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700269 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400270 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400271 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400272 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500273 , fTiled(false)
274 , fDrawTileBoundaries(false)
275 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400276 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700277{
Greg Daniel285db442016-10-14 09:12:53 -0400278 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700279
Brian Osmanf09e35e2017-12-15 14:48:09 -0500280 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500281 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500282 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600283 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500284 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
285 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700286
jvanverth2bb3b6d2016-04-08 07:24:09 -0700287 SkDebugf("Command line arguments: ");
288 for (int i = 1; i < argc; ++i) {
289 SkDebugf("%s ", argv[i]);
290 }
291 SkDebugf("\n");
292
Mike Klein88544fb2019-03-20 10:50:33 -0500293 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500294#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400295 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500296#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700297
Mike Klein19cc0f62019-03-22 15:30:07 -0500298 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500299
Brian Osmanbc8150f2017-07-24 11:38:01 -0400300 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400301 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400302
bsalomon6c471f72016-07-26 12:56:32 -0700303 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700304 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700305
csmartdalton578f0642017-02-24 16:04:47 -0700306 DisplayParams displayParams;
307 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700308 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400309 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400310 displayParams.fGrContextOptions.fShaderCacheStrategy =
311 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400312 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
313 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltona1638a52019-06-24 11:54:24 -0600314 displayParams.fGrContextOptions.fInternalMultisampleCount = FLAGS_internalSamples;
csmartdalton578f0642017-02-24 16:04:47 -0700315 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500316 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700317
Brian Osman56a24812017-12-19 11:15:16 -0500318 // Configure timers
319 fStatsLayer.setActive(false);
320 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
321 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
322 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
323
jvanverth9f372462016-04-06 06:08:59 -0700324 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700325 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500326 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500327 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500328 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700329
brianosman622c8d52016-05-10 06:50:49 -0700330 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500331 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
332 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
333 fWindow->inval();
334 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500335 // Command to jump directly to the slide picker and give it focus
336 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
337 this->fShowImGuiDebugWindow = true;
338 this->fShowSlidePicker = true;
339 fWindow->inval();
340 });
341 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400342 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500343 this->fShowImGuiDebugWindow = true;
344 this->fShowSlidePicker = true;
345 fWindow->inval();
346 });
Brian Osman79086b92017-02-10 13:36:16 -0500347 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
348 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
349 fWindow->inval();
350 });
Brian Osmanf6877092017-02-13 09:39:57 -0500351 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
352 this->fShowZoomWindow = !this->fShowZoomWindow;
353 fWindow->inval();
354 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400355 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
356 this->fZoomWindowFixed = !this->fZoomWindowFixed;
357 fWindow->inval();
358 });
Greg Danield0794cc2019-03-27 16:23:26 -0400359 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
360 DisplayParams params = fWindow->getRequestedDisplayParams();
361 params.fDisableVsync = !params.fDisableVsync;
362 fWindow->setRequestedDisplayParams(params);
363 this->updateTitle();
364 fWindow->inval();
365 });
Mike Reedf702ed42019-07-22 17:00:49 -0400366 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
367 fRefresh = !fRefresh;
368 fWindow->inval();
369 });
brianosman622c8d52016-05-10 06:50:49 -0700370 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500371 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700372 fWindow->inval();
373 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400374 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500375 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400376 this->updateTitle();
377 fWindow->inval();
378 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500379 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500380 switch (fColorMode) {
381 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500382 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500383 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500384 case ColorMode::kColorManaged8888:
385 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500386 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500387 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400388 this->setColorMode(ColorMode::kColorManagedF16Norm);
389 break;
390 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500391 this->setColorMode(ColorMode::kLegacy);
392 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500393 }
brianosman622c8d52016-05-10 06:50:49 -0700394 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400395 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500396 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700397 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400398 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500399 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700400 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400401 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700402 this->changeZoomLevel(1.f / 32.f);
403 fWindow->inval();
404 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400405 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700406 this->changeZoomLevel(-1.f / 32.f);
407 fWindow->inval();
408 });
jvanverthaf236b52016-05-20 06:01:06 -0700409 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400410 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
411 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500412 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400413#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
414 if (newBackend == sk_app::Window::kVulkan_BackendType) {
415 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
416 sk_app::Window::kBackendTypeCount);
417 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
418 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500419 }
420#endif
Brian Osman621491e2017-02-28 15:45:01 -0500421 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700422 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500423 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
424 fSaveToSKP = true;
425 fWindow->inval();
426 });
Mike Reed376d8122019-03-14 11:39:02 -0400427 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
428 fShowSlideDimensions = !fShowSlideDimensions;
429 fWindow->inval();
430 });
Ben Wagner37c54032018-04-13 14:30:23 -0400431 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
432 DisplayParams params = fWindow->getRequestedDisplayParams();
433 uint32_t flags = params.fSurfaceProps.flags();
434 if (!fPixelGeometryOverrides) {
435 fPixelGeometryOverrides = true;
436 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
437 } else {
438 switch (params.fSurfaceProps.pixelGeometry()) {
439 case kUnknown_SkPixelGeometry:
440 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
441 break;
442 case kRGB_H_SkPixelGeometry:
443 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
444 break;
445 case kBGR_H_SkPixelGeometry:
446 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
447 break;
448 case kRGB_V_SkPixelGeometry:
449 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
450 break;
451 case kBGR_V_SkPixelGeometry:
452 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
453 fPixelGeometryOverrides = false;
454 break;
455 }
456 }
457 fWindow->setRequestedDisplayParams(params);
458 this->updateTitle();
459 fWindow->inval();
460 });
Ben Wagner9613e452019-01-23 10:34:59 -0500461 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500462 if (!fFontOverrides.fHinting) {
463 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400464 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500465 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500466 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400467 case SkFontHinting::kNone:
468 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500469 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400470 case SkFontHinting::kSlight:
471 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500472 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400473 case SkFontHinting::kNormal:
474 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500475 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400476 case SkFontHinting::kFull:
477 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500478 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500479 break;
480 }
481 }
482 this->updateTitle();
483 fWindow->inval();
484 });
485 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500486 if (!fPaintOverrides.fAntiAlias) {
487 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
488 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500489 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500490 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500491 } else {
492 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500493 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500494 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500495 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400496 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500497 break;
498 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500499 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500500 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400501 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500502 break;
503 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500504 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400505 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500506 break;
507 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500508 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
509 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500510 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
511 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500512 break;
513 }
514 }
515 this->updateTitle();
516 fWindow->inval();
517 });
Ben Wagner37c54032018-04-13 14:30:23 -0400518 fCommands.addCommand('D', "Modes", "DFT", [this]() {
519 DisplayParams params = fWindow->getRequestedDisplayParams();
520 uint32_t flags = params.fSurfaceProps.flags();
521 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
522 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
523 fWindow->setRequestedDisplayParams(params);
524 this->updateTitle();
525 fWindow->inval();
526 });
Ben Wagner9613e452019-01-23 10:34:59 -0500527 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500528 if (!fFontOverrides.fEdging) {
529 fFontOverrides.fEdging = true;
530 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500531 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500532 switch (fFont.getEdging()) {
533 case SkFont::Edging::kAlias:
534 fFont.setEdging(SkFont::Edging::kAntiAlias);
535 break;
536 case SkFont::Edging::kAntiAlias:
537 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
538 break;
539 case SkFont::Edging::kSubpixelAntiAlias:
540 fFont.setEdging(SkFont::Edging::kAlias);
541 fFontOverrides.fEdging = false;
542 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500543 }
544 }
545 this->updateTitle();
546 fWindow->inval();
547 });
Ben Wagner9613e452019-01-23 10:34:59 -0500548 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500549 if (!fFontOverrides.fSubpixel) {
550 fFontOverrides.fSubpixel = true;
551 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500552 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500553 if (!fFont.isSubpixel()) {
554 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500555 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500556 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500557 }
558 }
559 this->updateTitle();
560 fWindow->inval();
561 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400562 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
563 if (!fFontOverrides.fBaselineSnap) {
564 fFontOverrides.fBaselineSnap = true;
565 fFont.setBaselineSnap(false);
566 } else {
567 if (!fFont.isBaselineSnap()) {
568 fFont.setBaselineSnap(true);
569 } else {
570 fFontOverrides.fBaselineSnap = false;
571 }
572 }
573 this->updateTitle();
574 fWindow->inval();
575 });
Brian Osman805a7272018-05-02 15:40:20 -0400576 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
577 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
578 : kPerspective_Real;
579 this->updateTitle();
580 fWindow->inval();
581 });
582 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
583 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
584 : kPerspective_Off;
585 this->updateTitle();
586 fWindow->inval();
587 });
Brian Osman207d4102019-01-10 09:40:58 -0500588 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
589 fAnimTimer.togglePauseResume();
590 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400591 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
592 fZoomUI = !fZoomUI;
593 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
594 fWindow->inval();
595 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500596
jvanverth2bb3b6d2016-04-08 07:24:09 -0700597 // set up slides
598 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500599 if (FLAGS_list) {
600 this->listNames();
601 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700602
Brian Osman9bb47cf2018-04-26 15:55:00 -0400603 fPerspectivePoints[0].set(0, 0);
604 fPerspectivePoints[1].set(1, 0);
605 fPerspectivePoints[2].set(0, 1);
606 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700607 fAnimTimer.run();
608
Hal Canaryc465d132017-12-08 10:21:31 -0500609 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500610 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400611 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500612 }
613 fImGuiGamutPaint.setColor(SK_ColorWHITE);
614 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
615
jongdeok.kim804f17e2019-02-26 14:39:23 +0900616 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500617 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700618}
619
jvanverth34524262016-05-04 13:49:13 -0700620void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400621 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
622 static const struct {
623 const char* fExtension;
624 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500625 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400626 const SlideFactory fFactory;
627 } gExternalSlidesInfo[] = {
628 { ".skp", "skp-dir", FLAGS_skps,
629 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
630 return sk_make_sp<SKPSlide>(name, path);}
631 },
632 { ".jpg", "jpg-dir", FLAGS_jpgs,
633 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
634 return sk_make_sp<ImageSlide>(name, path);}
635 },
Florin Malita87ccf332018-05-04 12:23:24 -0400636#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400637 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400638 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
639 return sk_make_sp<SkottieSlide>(name, path);}
640 },
Florin Malita87ccf332018-05-04 12:23:24 -0400641#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400642#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400643 { ".svg", "svg-dir", FLAGS_svgs,
644 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
645 return sk_make_sp<SvgSlide>(name, path);}
646 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400647#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400648 };
jvanverthc265a922016-04-08 12:51:45 -0700649
Brian Salomon343553a2018-09-05 15:41:23 -0400650 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700651
Mike Klein88544fb2019-03-20 10:50:33 -0500652 const auto addSlide =
653 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
654 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
655 return;
656 }
liyuqian6f163d22016-06-13 12:26:45 -0700657
Mike Klein88544fb2019-03-20 10:50:33 -0500658 if (auto slide = fact(name, path)) {
659 dirSlides.push_back(slide);
660 fSlides.push_back(std::move(slide));
661 }
662 };
Florin Malita76a076b2018-02-15 18:40:48 -0500663
Florin Malita38792ce2018-05-08 10:36:18 -0400664 if (!FLAGS_file.isEmpty()) {
665 // single file mode
666 const SkString file(FLAGS_file[0]);
667
668 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
669 for (const auto& sinfo : gExternalSlidesInfo) {
670 if (file.endsWith(sinfo.fExtension)) {
671 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
672 return;
673 }
674 }
675
676 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
677 } else {
678 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
679 }
680
681 return;
682 }
683
684 // Bisect slide.
685 if (!FLAGS_bisect.isEmpty()) {
686 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500687 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400688 if (FLAGS_bisect.count() >= 2) {
689 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
690 bisect->onChar(*ch);
691 }
692 }
693 fSlides.push_back(std::move(bisect));
694 }
695 }
696
697 // GMs
698 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400699 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400700 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500701 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400702 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400703 fSlides.push_back(std::move(slide));
704 }
Florin Malita38792ce2018-05-08 10:36:18 -0400705 }
706 // reverse gms
707 int numGMs = fSlides.count() - firstGM;
708 for (int i = 0; i < numGMs/2; ++i) {
709 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
710 }
711
712 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400713 for (const SampleFactory factory : SampleRegistry::Range()) {
714 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500715 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400716 fSlides.push_back(slide);
717 }
Florin Malita38792ce2018-05-08 10:36:18 -0400718 }
719
Brian Osman7c979f52019-02-12 13:27:51 -0500720 // Particle demo
721 {
722 // TODO: Convert this to a sample
723 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500724 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500725 fSlides.push_back(std::move(slide));
726 }
727 }
728
Florin Malita0ffa3222018-04-05 14:34:45 -0400729 for (const auto& info : gExternalSlidesInfo) {
730 for (const auto& flag : info.fFlags) {
731 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
732 // single file
733 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
734 } else {
735 // directory
736 SkOSFile::Iter it(flag.c_str(), info.fExtension);
737 SkString name;
738 while (it.next(&name)) {
739 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
740 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400741 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400742 if (!dirSlides.empty()) {
743 fSlides.push_back(
744 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
745 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500746 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400747 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400748 }
749 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500750
751 if (!fSlides.count()) {
752 sk_sp<Slide> slide(new NullSlide());
753 fSlides.push_back(std::move(slide));
754 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700755}
756
757
jvanverth34524262016-05-04 13:49:13 -0700758Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700759 fWindow->detach();
760 delete fWindow;
761}
762
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500763struct SkPaintTitleUpdater {
764 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
765 void append(const char* s) {
766 if (fCount == 0) {
767 fTitle->append(" {");
768 } else {
769 fTitle->append(", ");
770 }
771 fTitle->append(s);
772 ++fCount;
773 }
774 void done() {
775 if (fCount > 0) {
776 fTitle->append("}");
777 }
778 }
779 SkString* fTitle;
780 int fCount;
781};
782
brianosman05de2162016-05-06 13:28:57 -0700783void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700784 if (!fWindow) {
785 return;
786 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500787 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700788 return; // Surface hasn't been created yet.
789 }
790
jvanverth34524262016-05-04 13:49:13 -0700791 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700792 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700793
Mike Kleine5acd752019-03-22 09:57:16 -0500794 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400795 if (gSkForceAnalyticAA) {
796 title.append(" <FAAA>");
797 } else {
798 title.append(" <AAA>");
799 }
800 }
801
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500802 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500803 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
804 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400805 const char* on, const char* off)
806 {
Ben Wagner9613e452019-01-23 10:34:59 -0500807 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400808 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500809 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400810 };
811
Ben Wagner9613e452019-01-23 10:34:59 -0500812 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
813 const char* on, const char* off)
814 {
815 if (fFontOverrides.*flag) {
816 paintTitle.append((fFont.*isFlag)() ? on : off);
817 }
818 };
819
820 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
821 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500822 if (fPaintOverrides.fFilterQuality) {
823 switch (fPaint.getFilterQuality()) {
824 case kNone_SkFilterQuality:
825 paintTitle.append("NoFilter");
826 break;
827 case kLow_SkFilterQuality:
828 paintTitle.append("LowFilter");
829 break;
830 case kMedium_SkFilterQuality:
831 paintTitle.append("MediumFilter");
832 break;
833 case kHigh_SkFilterQuality:
834 paintTitle.append("HighFilter");
835 break;
836 }
837 }
Ben Wagner9613e452019-01-23 10:34:59 -0500838
839 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
840 "Force Autohint", "No Force Autohint");
841 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400842 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500843 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
844 "Linear Metrics", "Non-Linear Metrics");
845 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
846 "Bitmap Text", "No Bitmap Text");
847 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
848
849 if (fFontOverrides.fEdging) {
850 switch (fFont.getEdging()) {
851 case SkFont::Edging::kAlias:
852 paintTitle.append("Alias Text");
853 break;
854 case SkFont::Edging::kAntiAlias:
855 paintTitle.append("Antialias Text");
856 break;
857 case SkFont::Edging::kSubpixelAntiAlias:
858 paintTitle.append("Subpixel Antialias Text");
859 break;
860 }
861 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400862
Mike Reed3ae47332019-01-04 10:11:46 -0500863 if (fFontOverrides.fHinting) {
864 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400865 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500866 paintTitle.append("No Hinting");
867 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400868 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500869 paintTitle.append("Slight Hinting");
870 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400871 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500872 paintTitle.append("Normal Hinting");
873 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400874 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500875 paintTitle.append("Full Hinting");
876 break;
877 }
878 }
879 paintTitle.done();
880
Brian Osman92004802017-03-06 11:47:26 -0500881 switch (fColorMode) {
882 case ColorMode::kLegacy:
883 title.append(" Legacy 8888");
884 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500885 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500886 title.append(" ColorManaged 8888");
887 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500888 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500889 title.append(" ColorManaged F16");
890 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400891 case ColorMode::kColorManagedF16Norm:
892 title.append(" ColorManaged F16 Norm");
893 break;
Brian Osman92004802017-03-06 11:47:26 -0500894 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500895
Brian Osman92004802017-03-06 11:47:26 -0500896 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500897 int curPrimaries = -1;
898 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
899 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
900 curPrimaries = i;
901 break;
902 }
903 }
Brian Osman03115dc2018-11-26 13:55:19 -0500904 title.appendf(" %s Gamma %f",
905 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500906 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700907 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500908
Ben Wagner37c54032018-04-13 14:30:23 -0400909 const DisplayParams& params = fWindow->getRequestedDisplayParams();
910 if (fPixelGeometryOverrides) {
911 switch (params.fSurfaceProps.pixelGeometry()) {
912 case kUnknown_SkPixelGeometry:
913 title.append( " Flat");
914 break;
915 case kRGB_H_SkPixelGeometry:
916 title.append( " RGB");
917 break;
918 case kBGR_H_SkPixelGeometry:
919 title.append( " BGR");
920 break;
921 case kRGB_V_SkPixelGeometry:
922 title.append( " RGBV");
923 break;
924 case kBGR_V_SkPixelGeometry:
925 title.append( " BGRV");
926 break;
927 }
928 }
929
930 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
931 title.append(" DFT");
932 }
933
csmartdalton578f0642017-02-24 16:04:47 -0700934 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700935 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500936 int msaa = fWindow->sampleCount();
937 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700938 title.appendf(" MSAA: %i", msaa);
939 }
940 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700941
942 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600943 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700944 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
945 }
946
Brian Osman805a7272018-05-02 15:40:20 -0400947 if (kPerspective_Real == fPerspectiveMode) {
948 title.append(" Perpsective (Real)");
949 } else if (kPerspective_Fake == fPerspectiveMode) {
950 title.append(" Perspective (Fake)");
951 }
952
brianosman05de2162016-05-06 13:28:57 -0700953 fWindow->setTitle(title.c_str());
954}
955
Florin Malitaab99c342018-01-16 16:23:03 -0500956int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500957
958 if (!FLAGS_slide.isEmpty()) {
959 int count = fSlides.count();
960 for (int i = 0; i < count; i++) {
961 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500962 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500963 }
964 }
965
966 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
967 this->listNames();
968 }
969
Florin Malitaab99c342018-01-16 16:23:03 -0500970 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500971}
972
Florin Malitaab99c342018-01-16 16:23:03 -0500973void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500974 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500975 for (const auto& slide : fSlides) {
976 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500977 }
978}
979
Florin Malitaab99c342018-01-16 16:23:03 -0500980void Viewer::setCurrentSlide(int slide) {
981 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700982
Florin Malitaab99c342018-01-16 16:23:03 -0500983 if (slide == fCurrentSlide) {
984 return;
985 }
986
987 if (fCurrentSlide >= 0) {
988 fSlides[fCurrentSlide]->unload();
989 }
990
991 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
992 SkIntToScalar(fWindow->height()));
993 fCurrentSlide = slide;
994 this->setupCurrentSlide();
995}
996
997void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500998 if (fCurrentSlide >= 0) {
999 // prepare dimensions for image slides
1000 fGesture.resetTouchState();
1001 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001002
Jim Van Verth0848fb02018-01-22 13:39:30 -05001003 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1004 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1005 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001006
Jim Van Verth0848fb02018-01-22 13:39:30 -05001007 // Start with a matrix that scales the slide to the available screen space
1008 if (fWindow->scaleContentToFit()) {
1009 if (windowRect.width() > 0 && windowRect.height() > 0) {
1010 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1011 }
liyuqiane46e4f02016-05-20 07:32:19 -07001012 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001013
1014 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001015 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001016
1017 this->updateTitle();
1018 this->updateUIState();
1019
1020 fStatsLayer.resetMeasurements();
1021
1022 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001023 }
jvanverthc265a922016-04-08 12:51:45 -07001024}
1025
1026#define MAX_ZOOM_LEVEL 8
1027#define MIN_ZOOM_LEVEL -8
1028
jvanverth34524262016-05-04 13:49:13 -07001029void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001030 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -04001031 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001032 this->preTouchMatrixChanged();
1033}
Yuqian Li755778c2018-03-28 16:23:31 -04001034
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001035void Viewer::preTouchMatrixChanged() {
1036 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001037 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1038 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1039 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1040 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1041}
1042
Brian Osman805a7272018-05-02 15:40:20 -04001043SkMatrix Viewer::computePerspectiveMatrix() {
1044 SkScalar w = fWindow->width(), h = fWindow->height();
1045 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1046 SkPoint perspPts[4] = {
1047 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1048 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1049 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1050 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1051 };
1052 SkMatrix m;
1053 m.setPolyToPoly(orthoPts, perspPts, 4);
1054 return m;
1055}
1056
Yuqian Li755778c2018-03-28 16:23:31 -04001057SkMatrix Viewer::computePreTouchMatrix() {
1058 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001059
1060 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001061 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001062 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001063
1064 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1065 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001066
Brian Osman805a7272018-05-02 15:40:20 -04001067 if (kPerspective_Real == fPerspectiveMode) {
1068 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001069 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001070 }
1071
Yuqian Li755778c2018-03-28 16:23:31 -04001072 return m;
jvanverthc265a922016-04-08 12:51:45 -07001073}
1074
liyuqiand3cdbca2016-05-17 12:44:20 -07001075SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001076 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001077 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001078 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001079 return m;
jvanverthc265a922016-04-08 12:51:45 -07001080}
1081
Brian Osman621491e2017-02-28 15:45:01 -05001082void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001083 fPersistentCache.reset();
1084 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001085 fBackendType = backendType;
1086
1087 fWindow->detach();
1088
Brian Osman70d2f432017-11-08 09:54:10 -05001089#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001090 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1091 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001092 DisplayParams params = fWindow->getRequestedDisplayParams();
1093 delete fWindow;
1094 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001095
Brian Osman70d2f432017-11-08 09:54:10 -05001096 // re-register callbacks
1097 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001098 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001099 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001100 fWindow->pushLayer(&fImGuiLayer);
1101
Brian Osman70d2f432017-11-08 09:54:10 -05001102 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1103 // will still include our correct sample count. But the re-created fWindow will lose that
1104 // information. On Windows, we need to re-create the window when changing sample count,
1105 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1106 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1107 // as if we had never changed windows at all).
1108 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001109#endif
1110
Brian Osman70d2f432017-11-08 09:54:10 -05001111 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001112}
1113
Brian Osman92004802017-03-06 11:47:26 -05001114void Viewer::setColorMode(ColorMode colorMode) {
1115 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001116 this->updateTitle();
1117 fWindow->inval();
1118}
1119
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001120class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1121public:
Mike Reed3ae47332019-01-04 10:11:46 -05001122 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1123 SkFont* font, Viewer::SkFontFields* ffields)
1124 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001125 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001126 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1127 sk_sp<SkTextBlob>* cache) {
1128 bool blobWillChange = false;
1129 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001130 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1131 bool shouldDraw = this->filterFont(&filteredFont);
1132 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001133 blobWillChange = true;
1134 break;
1135 }
1136 }
1137 if (!blobWillChange) {
1138 return blob;
1139 }
1140
1141 SkTextBlobBuilder builder;
1142 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001143 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1144 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001145 if (!shouldDraw) {
1146 continue;
1147 }
1148
Mike Reed3ae47332019-01-04 10:11:46 -05001149 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001150
Ben Wagner41e40472018-09-24 13:01:54 -04001151 const SkTextBlobBuilder::RunBuffer& runBuffer
1152 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001153 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001154 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001155 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001156 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001157 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001158 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001159 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001160 it.glyphCount(), it.textSize(), SkString())
1161 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1162 uint32_t glyphCount = it.glyphCount();
1163 if (it.glyphs()) {
1164 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1165 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1166 }
1167 if (it.pos()) {
1168 size_t posSize = sizeof(decltype(*it.pos()));
1169 uint8_t positioning = it.positioning();
1170 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1171 }
1172 if (it.text()) {
1173 size_t textSize = sizeof(decltype(*it.text()));
1174 uint32_t textCount = it.textSize();
1175 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1176 }
1177 if (it.clusters()) {
1178 size_t clusterSize = sizeof(decltype(*it.clusters()));
1179 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1180 }
1181 }
1182 *cache = builder.make();
1183 return cache->get();
1184 }
1185 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1186 const SkPaint& paint) override {
1187 sk_sp<SkTextBlob> cache;
1188 this->SkPaintFilterCanvas::onDrawTextBlob(
1189 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1190 }
Mike Reed3ae47332019-01-04 10:11:46 -05001191 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001192 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001193 font->writable()->setSize(fFont->getSize());
1194 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001195 if (fFontOverrides->fScaleX) {
1196 font->writable()->setScaleX(fFont->getScaleX());
1197 }
1198 if (fFontOverrides->fSkewX) {
1199 font->writable()->setSkewX(fFont->getSkewX());
1200 }
Mike Reed3ae47332019-01-04 10:11:46 -05001201 if (fFontOverrides->fHinting) {
1202 font->writable()->setHinting(fFont->getHinting());
1203 }
Ben Wagner9613e452019-01-23 10:34:59 -05001204 if (fFontOverrides->fEdging) {
1205 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001206 }
Ben Wagner9613e452019-01-23 10:34:59 -05001207 if (fFontOverrides->fEmbolden) {
1208 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001209 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001210 if (fFontOverrides->fBaselineSnap) {
1211 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1212 }
Ben Wagner9613e452019-01-23 10:34:59 -05001213 if (fFontOverrides->fLinearMetrics) {
1214 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001215 }
Ben Wagner9613e452019-01-23 10:34:59 -05001216 if (fFontOverrides->fSubpixel) {
1217 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001218 }
Ben Wagner9613e452019-01-23 10:34:59 -05001219 if (fFontOverrides->fEmbeddedBitmaps) {
1220 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001221 }
Ben Wagner9613e452019-01-23 10:34:59 -05001222 if (fFontOverrides->fForceAutoHinting) {
1223 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001224 }
Ben Wagner9613e452019-01-23 10:34:59 -05001225
Mike Reed3ae47332019-01-04 10:11:46 -05001226 return true;
1227 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001228 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001229 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001230 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001231 }
Ben Wagner9613e452019-01-23 10:34:59 -05001232 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001233 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001234 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001235 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001236 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001237 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001238 return true;
1239 }
1240 SkPaint* fPaint;
1241 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001242 SkFont* fFont;
1243 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001244};
1245
Robert Phillips9882dae2019-03-04 11:00:10 -05001246void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001247 if (fCurrentSlide < 0) {
1248 return;
1249 }
1250
Robert Phillips9882dae2019-03-04 11:00:10 -05001251 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001252
Brian Osmanf750fbc2017-02-08 10:47:28 -05001253 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001254 SkSurface* slideSurface = surface;
1255 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001256 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001257
Brian Osmane0d4fba2017-03-15 10:24:55 -04001258 // 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 -05001259 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001260 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001261 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001262 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001263 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001264 }
1265
Brian Osman3ac99cf2017-12-01 11:23:53 -05001266 if (fSaveToSKP) {
1267 SkPictureRecorder recorder;
1268 SkCanvas* recorderCanvas = recorder.beginRecording(
1269 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001270 fSlides[fCurrentSlide]->draw(recorderCanvas);
1271 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1272 SkFILEWStream stream("sample_app.skp");
1273 picture->serialize(&stream);
1274 fSaveToSKP = false;
1275 }
1276
Brian Osmane9ed0f02018-11-26 14:50:05 -05001277 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001278 SkColorType colorType;
1279 switch (fColorMode) {
1280 case ColorMode::kLegacy:
1281 case ColorMode::kColorManaged8888:
1282 colorType = kN32_SkColorType;
1283 break;
1284 case ColorMode::kColorManagedF16:
1285 colorType = kRGBA_F16_SkColorType;
1286 break;
1287 case ColorMode::kColorManagedF16Norm:
1288 colorType = kRGBA_F16Norm_SkColorType;
1289 break;
1290 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001291
1292 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001293 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1294 slideCanvas->getProps(&props);
1295
Brian Osmane9ed0f02018-11-26 14:50:05 -05001296 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1297 return Window::kRaster_BackendType == this->fBackendType
1298 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001299 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001300 };
1301
Brian Osman03115dc2018-11-26 13:55:19 -05001302 // We need to render offscreen if we're...
1303 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1304 // ... in any raster mode, because the window surface is actually GL
1305 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001306 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001307 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001308 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001309 Window::kRaster_BackendType == fBackendType ||
1310 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001311
Brian Osmane9ed0f02018-11-26 14:50:05 -05001312 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001313 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001314 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001315 }
1316
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001317 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001318 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001319 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001320 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001321 if (fTiled) {
1322 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1323 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1324 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1325 SkCanvas* tileCanvas = tileSurface->getCanvas();
1326 SkMatrix m = this->computeMatrix();
1327 for (int y = 0; y < fWindow->height(); y += tileH) {
1328 for (int x = 0; x < fWindow->width(); x += tileW) {
1329 SkAutoCanvasRestore acr(tileCanvas, true);
1330 tileCanvas->translate(-x, -y);
1331 tileCanvas->clear(SK_ColorTRANSPARENT);
1332 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001333 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1334 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001335 fSlides[fCurrentSlide]->draw(&filterCanvas);
1336 tileSurface->draw(slideCanvas, x, y, nullptr);
1337 }
1338 }
1339
1340 // Draw borders between tiles
1341 if (fDrawTileBoundaries) {
1342 SkPaint border;
1343 border.setColor(0x60FF00FF);
1344 border.setStyle(SkPaint::kStroke_Style);
1345 for (int y = 0; y < fWindow->height(); y += tileH) {
1346 for (int x = 0; x < fWindow->width(); x += tileW) {
1347 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1348 }
1349 }
1350 }
1351 } else {
1352 slideCanvas->concat(this->computeMatrix());
1353 if (kPerspective_Real == fPerspectiveMode) {
1354 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1355 }
Mike Reed3ae47332019-01-04 10:11:46 -05001356 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001357 fSlides[fCurrentSlide]->draw(&filterCanvas);
1358 }
Brian Osman56a24812017-12-19 11:15:16 -05001359 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001360 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001361
1362 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001363 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001364 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001365 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001366
1367 // If we rendered offscreen, snap an image and push the results to the window's canvas
1368 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001369 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001370
Robert Phillips9882dae2019-03-04 11:00:10 -05001371 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001372 SkPaint paint;
1373 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001374 int prePerspectiveCount = canvas->save();
1375 if (kPerspective_Fake == fPerspectiveMode) {
1376 paint.setFilterQuality(kHigh_SkFilterQuality);
1377 canvas->clear(SK_ColorWHITE);
1378 canvas->concat(this->computePerspectiveMatrix());
1379 }
Brian Osman03115dc2018-11-26 13:55:19 -05001380 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001381 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001382 }
Mike Reed376d8122019-03-14 11:39:02 -04001383
1384 if (fShowSlideDimensions) {
1385 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1386 SkPaint paint;
1387 paint.setColor(0x40FFFF00);
1388 surface->getCanvas()->drawRect(r, paint);
1389 }
liyuqian6f163d22016-06-13 12:26:45 -07001390}
1391
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001392void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001393 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001394 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001395}
Jim Van Verth6f449692017-02-14 15:16:46 -05001396
Robert Phillips9882dae2019-03-04 11:00:10 -05001397void Viewer::onPaint(SkSurface* surface) {
1398 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001399
Robert Phillips9882dae2019-03-04 11:00:10 -05001400 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001401
Brian Osmand67e5182017-12-08 16:46:09 -05001402 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001403
1404 if (GrContext* ctx = fWindow->getGrContext()) {
1405 // Clean out cache items that haven't been used in more than 10 seconds.
1406 ctx->performDeferredCleanup(std::chrono::seconds(10));
1407 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001408}
1409
Ben Wagnera1915972018-08-09 15:06:19 -04001410void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001411 if (fCurrentSlide >= 0) {
1412 fSlides[fCurrentSlide]->resize(width, height);
1413 }
Ben Wagnera1915972018-08-09 15:06:19 -04001414}
1415
Florin Malitacefc1b92018-02-19 21:43:47 -05001416SkPoint Viewer::mapEvent(float x, float y) {
1417 const auto m = this->computeMatrix();
1418 SkMatrix inv;
1419
1420 SkAssertResult(m.invert(&inv));
1421
1422 return inv.mapXY(x, y);
1423}
1424
Hal Canaryb1f411a2019-08-29 10:39:22 -04001425bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001426 if (GestureDevice::kMouse == fGestureDevice) {
1427 return false;
1428 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001429
1430 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001431 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001432 fWindow->inval();
1433 return true;
1434 }
1435
liyuqiand3cdbca2016-05-17 12:44:20 -07001436 void* castedOwner = reinterpret_cast<void*>(owner);
1437 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001438 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001439 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001440#if defined(SK_BUILD_FOR_IOS)
1441 // TODO: move IOS swipe detection higher up into the platform code
1442 SkPoint dir;
1443 if (fGesture.isFling(&dir)) {
1444 // swiping left or right
1445 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1446 if (dir.fX < 0) {
1447 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1448 fCurrentSlide + 1 : 0);
1449 } else {
1450 this->setCurrentSlide(fCurrentSlide > 0 ?
1451 fCurrentSlide - 1 : fSlides.count() - 1);
1452 }
1453 }
1454 fGesture.reset();
1455 }
1456#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001457 break;
1458 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001459 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001460 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001461 break;
1462 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001463 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001464 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001465 break;
1466 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001467 default: {
1468 // kLeft and kRight are only for swipes
1469 SkASSERT(false);
1470 break;
1471 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001472 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001473 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001474 fWindow->inval();
1475 return true;
1476}
1477
Hal Canaryb1f411a2019-08-29 10:39:22 -04001478bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001479 if (GestureDevice::kTouch == fGestureDevice) {
1480 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001481 }
Brian Osman16c81a12017-12-20 11:58:34 -05001482
Florin Malitacefc1b92018-02-19 21:43:47 -05001483 const auto slidePt = this->mapEvent(x, y);
1484 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1485 fWindow->inval();
1486 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001487 }
1488
1489 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001490 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001491 fGesture.touchEnd(nullptr);
1492 break;
1493 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001494 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001495 fGesture.touchBegin(nullptr, x, y);
1496 break;
1497 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001498 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001499 fGesture.touchMoved(nullptr, x, y);
1500 break;
1501 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001502 default: {
1503 SkASSERT(false); // shouldn't see kRight or kLeft here
1504 break;
1505 }
Brian Osman16c81a12017-12-20 11:58:34 -05001506 }
1507 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1508
Hal Canaryb1f411a2019-08-29 10:39:22 -04001509 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001510 fWindow->inval();
1511 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001512 return true;
1513}
1514
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001515bool Viewer::onFling(skui::InputState state) {
1516 if (skui::InputState::kRight == state) {
1517 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1518 return true;
1519 } else if (skui::InputState::kLeft == state) {
1520 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1521 return true;
1522 }
1523 return false;
1524}
1525
1526bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1527 switch (state) {
1528 case skui::InputState::kDown:
1529 fGesture.startZoom();
1530 return true;
1531 break;
1532 case skui::InputState::kMove:
1533 fGesture.updateZoom(scale, x, y, x, y);
1534 return true;
1535 break;
1536 case skui::InputState::kUp:
1537 fGesture.endZoom();
1538 return true;
1539 break;
1540 default:
1541 SkASSERT(false);
1542 break;
1543 }
1544
1545 return false;
1546}
1547
Brian Osmana109e392017-02-24 09:49:14 -05001548static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001549 // The gamut image covers a (0.8 x 0.9) shaped region
1550 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001551
1552 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1553 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1554 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001555 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1556 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1557 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001558
Brian Osman535c5e32019-02-09 16:32:58 -05001559 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1560 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1561 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1562 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1563 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001564}
1565
Ben Wagner3627d2e2018-06-26 14:23:20 -04001566static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001567 ImGui::DragCanvas dc(pt);
1568 dc.fillColor(IM_COL32(0, 0, 0, 128));
1569 dc.dragPoint(pt);
1570 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001571}
1572
Brian Osman9bb47cf2018-04-26 15:55:00 -04001573static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001574 ImGui::DragCanvas dc(pts);
1575 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001576
Brian Osman535c5e32019-02-09 16:32:58 -05001577 for (int i = 0; i < 4; ++i) {
1578 dc.dragPoint(pts + i);
1579 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001580
Brian Osman535c5e32019-02-09 16:32:58 -05001581 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1582 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1583 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1584 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001585
Brian Osman535c5e32019-02-09 16:32:58 -05001586 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001587}
1588
Brian Osmand67e5182017-12-08 16:46:09 -05001589void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001590 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1591 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001592 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001593 }
1594
1595 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001596 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1597 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001598 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001599 DisplayParams params = fWindow->getRequestedDisplayParams();
1600 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001601 const GrContext* ctx = fWindow->getGrContext();
1602
Brian Osmana109e392017-02-24 09:49:14 -05001603 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1604 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001605 if (ImGui::CollapsingHeader("Backend")) {
1606 int newBackend = static_cast<int>(fBackendType);
1607 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1608 ImGui::SameLine();
1609 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001610#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1611 ImGui::SameLine();
1612 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1613#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001614#if defined(SK_DAWN)
1615 ImGui::SameLine();
1616 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1617#endif
Brian Osman621491e2017-02-28 15:45:01 -05001618#if defined(SK_VULKAN)
1619 ImGui::SameLine();
1620 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1621#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001622#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001623 ImGui::SameLine();
1624 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1625#endif
Brian Osman621491e2017-02-28 15:45:01 -05001626 if (newBackend != fBackendType) {
1627 fDeferredActions.push_back([=]() {
1628 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1629 });
1630 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001631
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001632 bool* wire = &params.fGrContextOptions.fWireframeMode;
1633 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1634 paramsChanged = true;
1635 }
Brian Salomon99a33902017-03-07 15:16:34 -05001636
Brian Osman28b12522017-03-08 17:10:24 -05001637 if (ctx) {
1638 int sampleCount = fWindow->sampleCount();
1639 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001640 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001641 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1642 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1643 ImGui::RadioButton("16", &sampleCount, 16);
1644
1645 if (sampleCount != params.fMSAASampleCount) {
1646 params.fMSAASampleCount = sampleCount;
1647 paramsChanged = true;
1648 }
1649 }
1650
Ben Wagner37c54032018-04-13 14:30:23 -04001651 int pixelGeometryIdx = 0;
1652 if (fPixelGeometryOverrides) {
1653 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1654 }
1655 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1656 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1657 {
1658 uint32_t flags = params.fSurfaceProps.flags();
1659 if (pixelGeometryIdx == 0) {
1660 fPixelGeometryOverrides = false;
1661 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1662 } else {
1663 fPixelGeometryOverrides = true;
1664 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1665 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1666 }
1667 paramsChanged = true;
1668 }
1669
1670 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1671 if (ImGui::Checkbox("DFT", &useDFT)) {
1672 uint32_t flags = params.fSurfaceProps.flags();
1673 if (useDFT) {
1674 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1675 } else {
1676 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1677 }
1678 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1679 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1680 paramsChanged = true;
1681 }
1682
Brian Osman8a9de3d2017-03-01 14:59:05 -05001683 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001684 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001685 auto prButton = [&](GpuPathRenderers x) {
1686 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001687 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1688 params.fGrContextOptions.fGpuPathRenderers = x;
1689 paramsChanged = true;
1690 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001691 }
1692 };
1693
1694 if (!ctx) {
1695 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001696 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001697 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001698 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001699 prButton(GpuPathRenderers::kStencilAndCover);
1700 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001701 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001702 prButton(GpuPathRenderers::kNone);
1703 } else {
1704 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001705 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001706 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001707 prButton(GpuPathRenderers::kCoverageCounting);
1708 }
Jim Van Verth83010462017-03-16 08:45:39 -04001709 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001710 prButton(GpuPathRenderers::kTessellating);
1711 prButton(GpuPathRenderers::kNone);
1712 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001713 ImGui::TreePop();
1714 }
Brian Osman621491e2017-02-28 15:45:01 -05001715 }
1716
Ben Wagner964571d2019-03-08 12:35:06 -05001717 if (ImGui::CollapsingHeader("Tiling")) {
1718 ImGui::Checkbox("Enable", &fTiled);
1719 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1720 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1721 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1722 }
1723
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001724 if (ImGui::CollapsingHeader("Transform")) {
1725 float zoom = fZoomLevel;
1726 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1727 fZoomLevel = zoom;
1728 this->preTouchMatrixChanged();
1729 paramsChanged = true;
1730 }
1731 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001732 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001733 fRotation = deg;
1734 this->preTouchMatrixChanged();
1735 paramsChanged = true;
1736 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001737 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1738 if (ImGui_DragLocation(&fOffset)) {
1739 this->preTouchMatrixChanged();
1740 paramsChanged = true;
1741 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001742 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1743 this->preTouchMatrixChanged();
1744 paramsChanged = true;
1745 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001746 }
Brian Osman805a7272018-05-02 15:40:20 -04001747 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1748 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1749 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001750 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001751 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001752 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001753 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001754 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001755 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001756 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001757 }
1758
Ben Wagnera580fb32018-04-17 11:16:32 -04001759 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001760 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001761 if (fPaintOverrides.fAntiAlias) {
1762 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001763 }
1764 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001765 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001766 {
1767 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1768 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001769 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001770 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1771 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001772 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001773 fPaintOverrides.fAntiAlias = true;
1774 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001775 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001776 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001777 case SkPaintFields::AntiAliasState::Alias:
1778 break;
1779 case SkPaintFields::AntiAliasState::Normal:
1780 break;
1781 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1782 gSkUseAnalyticAA = true;
1783 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001784 break;
1785 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1786 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001787 break;
1788 }
1789 }
1790 paramsChanged = true;
1791 }
1792
Ben Wagner99a78dc2018-05-09 18:23:51 -04001793 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001794 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001795 bool (SkPaint::* isFlag)() const,
1796 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001797 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001798 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001799 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001800 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001801 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001802 if (ImGui::Combo(label, &itemIndex, items)) {
1803 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001804 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001805 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001806 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001807 (fPaint.*setFlag)(itemIndex == 2);
1808 }
1809 paramsChanged = true;
1810 }
1811 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001812
Ben Wagner99a78dc2018-05-09 18:23:51 -04001813 paintFlag("Dither",
1814 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001815 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001816 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001817
1818 int filterQualityIdx = 0;
1819 if (fPaintOverrides.fFilterQuality) {
1820 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1821 }
1822 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1823 "Default\0None\0Low\0Medium\0High\0\0"))
1824 {
1825 if (filterQualityIdx == 0) {
1826 fPaintOverrides.fFilterQuality = false;
1827 fPaint.setFilterQuality(kNone_SkFilterQuality);
1828 } else {
1829 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1830 fPaintOverrides.fFilterQuality = true;
1831 }
1832 paramsChanged = true;
1833 }
Ben Wagner9613e452019-01-23 10:34:59 -05001834 }
Hal Canary02738a82019-01-21 18:51:32 +00001835
Ben Wagner9613e452019-01-23 10:34:59 -05001836 if (ImGui::CollapsingHeader("Font")) {
1837 int hintingIdx = 0;
1838 if (fFontOverrides.fHinting) {
1839 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1840 }
1841 if (ImGui::Combo("Hinting", &hintingIdx,
1842 "Default\0None\0Slight\0Normal\0Full\0\0"))
1843 {
1844 if (hintingIdx == 0) {
1845 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001846 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001847 } else {
1848 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1849 fFontOverrides.fHinting = true;
1850 }
1851 paramsChanged = true;
1852 }
Hal Canary02738a82019-01-21 18:51:32 +00001853
Ben Wagner9613e452019-01-23 10:34:59 -05001854 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1855 bool SkFontFields::* flag,
1856 bool (SkFont::* isFlag)() const,
1857 void (SkFont::* setFlag)(bool) )
1858 {
1859 int itemIndex = 0;
1860 if (fFontOverrides.*flag) {
1861 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1862 }
1863 if (ImGui::Combo(label, &itemIndex, items)) {
1864 if (itemIndex == 0) {
1865 fFontOverrides.*flag = false;
1866 } else {
1867 fFontOverrides.*flag = true;
1868 (fFont.*setFlag)(itemIndex == 2);
1869 }
1870 paramsChanged = true;
1871 }
1872 };
Hal Canary02738a82019-01-21 18:51:32 +00001873
Ben Wagner9613e452019-01-23 10:34:59 -05001874 fontFlag("Fake Bold Glyphs",
1875 "Default\0No Fake Bold\0Fake Bold\0\0",
1876 &SkFontFields::fEmbolden,
1877 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001878
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001879 fontFlag("Baseline Snapping",
1880 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1881 &SkFontFields::fBaselineSnap,
1882 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1883
Ben Wagner9613e452019-01-23 10:34:59 -05001884 fontFlag("Linear Text",
1885 "Default\0No Linear Text\0Linear Text\0\0",
1886 &SkFontFields::fLinearMetrics,
1887 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001888
Ben Wagner9613e452019-01-23 10:34:59 -05001889 fontFlag("Subpixel Position Glyphs",
1890 "Default\0Pixel Text\0Subpixel Text\0\0",
1891 &SkFontFields::fSubpixel,
1892 &SkFont::isSubpixel, &SkFont::setSubpixel);
1893
1894 fontFlag("Embedded Bitmap Text",
1895 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1896 &SkFontFields::fEmbeddedBitmaps,
1897 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1898
1899 fontFlag("Force Auto-Hinting",
1900 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1901 &SkFontFields::fForceAutoHinting,
1902 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1903
1904 int edgingIdx = 0;
1905 if (fFontOverrides.fEdging) {
1906 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1907 }
1908 if (ImGui::Combo("Edging", &edgingIdx,
1909 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1910 {
1911 if (edgingIdx == 0) {
1912 fFontOverrides.fEdging = false;
1913 fFont.setEdging(SkFont::Edging::kAlias);
1914 } else {
1915 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1916 fFontOverrides.fEdging = true;
1917 }
1918 paramsChanged = true;
1919 }
1920
Ben Wagner15a8d572019-03-21 13:35:44 -04001921 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1922 if (fFontOverrides.fSize) {
1923 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001924 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001925 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001926 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001927 fFontOverrides.fSizeRange[0],
1928 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001929 "%.6f", 2.0f))
1930 {
Mike Reed3ae47332019-01-04 10:11:46 -05001931 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001932 paramsChanged = true;
1933 }
1934 }
1935
1936 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1937 if (fFontOverrides.fScaleX) {
1938 float scaleX = fFont.getScaleX();
1939 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1940 fFont.setScaleX(scaleX);
1941 paramsChanged = true;
1942 }
1943 }
1944
1945 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1946 if (fFontOverrides.fSkewX) {
1947 float skewX = fFont.getSkewX();
1948 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1949 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001950 paramsChanged = true;
1951 }
1952 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001953 }
1954
Mike Reed81f60ec2018-05-15 10:09:52 -04001955 {
1956 SkMetaData controls;
1957 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1958 if (ImGui::CollapsingHeader("Current Slide")) {
1959 SkMetaData::Iter iter(controls);
1960 const char* name;
1961 SkMetaData::Type type;
1962 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001963 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001964 if (type == SkMetaData::kScalar_Type) {
1965 float val[3];
1966 SkASSERT(count == 3);
1967 controls.findScalars(name, &count, val);
1968 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1969 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001970 }
Ben Wagner110c7032019-03-22 17:03:59 -04001971 } else if (type == SkMetaData::kBool_Type) {
1972 bool val;
1973 SkASSERT(count == 1);
1974 controls.findBool(name, &val);
1975 if (ImGui::Checkbox(name, &val)) {
1976 controls.setBool(name, val);
1977 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001978 }
1979 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001980 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001981 }
1982 }
1983 }
1984
Ben Wagner7a3c6742018-04-23 10:01:07 -04001985 if (fShowSlidePicker) {
1986 ImGui::SetNextTreeNodeOpen(true);
1987 }
Brian Osman79086b92017-02-10 13:36:16 -05001988 if (ImGui::CollapsingHeader("Slide")) {
1989 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001990 static ImVector<const char*> filteredSlideNames;
1991 static ImVector<int> filteredSlideIndices;
1992
Brian Osmanfce09c52017-11-14 15:32:20 -05001993 if (fShowSlidePicker) {
1994 ImGui::SetKeyboardFocusHere();
1995 fShowSlidePicker = false;
1996 }
1997
Brian Osman79086b92017-02-10 13:36:16 -05001998 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001999 filteredSlideNames.clear();
2000 filteredSlideIndices.clear();
2001 int filteredIndex = 0;
2002 for (int i = 0; i < fSlides.count(); ++i) {
2003 const char* slideName = fSlides[i]->getName().c_str();
2004 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2005 if (i == fCurrentSlide) {
2006 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002007 }
Brian Osmanf479e422017-11-08 13:11:36 -05002008 filteredSlideNames.push_back(slideName);
2009 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002010 }
Brian Osman79086b92017-02-10 13:36:16 -05002011 }
Brian Osmanf479e422017-11-08 13:11:36 -05002012
Brian Osmanf479e422017-11-08 13:11:36 -05002013 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2014 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002015 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002016 }
2017 }
Brian Osmana109e392017-02-24 09:49:14 -05002018
2019 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002020 ColorMode newMode = fColorMode;
2021 auto cmButton = [&](ColorMode mode, const char* label) {
2022 if (ImGui::RadioButton(label, mode == fColorMode)) {
2023 newMode = mode;
2024 }
2025 };
2026
2027 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002028 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2029 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002030 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002031
2032 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002033 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002034 }
2035
2036 // Pick from common gamuts:
2037 int primariesIdx = 4; // Default: Custom
2038 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2039 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2040 primariesIdx = i;
2041 break;
2042 }
2043 }
2044
Brian Osman03115dc2018-11-26 13:55:19 -05002045 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002046 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002047
Brian Osmana109e392017-02-24 09:49:14 -05002048 if (ImGui::Combo("Primaries", &primariesIdx,
2049 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2050 if (primariesIdx >= 0 && primariesIdx <= 3) {
2051 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2052 }
2053 }
2054
2055 // Allow direct editing of gamut
2056 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2057 }
Brian Osman207d4102019-01-10 09:40:58 -05002058
2059 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002060 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002061 if (ImGui::Checkbox("Pause", &isPaused)) {
2062 fAnimTimer.togglePauseResume();
2063 }
Brian Osman707d2022019-01-10 11:27:34 -05002064
2065 float speed = fAnimTimer.getSpeed();
2066 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2067 fAnimTimer.setSpeed(speed);
2068 }
Brian Osman207d4102019-01-10 09:40:58 -05002069 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002070
Brian Osmanfd7657c2019-04-25 11:34:07 -04002071 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2072#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2073 || Window::kANGLE_BackendType == fBackendType
2074#endif
2075 ;
2076
2077 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2078 // than GL, we need to force it on. Just do that on the first frame after the backend
2079 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002080 if (!backendIsGL &&
2081 params.fGrContextOptions.fShaderCacheStrategy !=
2082 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2083 params.fGrContextOptions.fShaderCacheStrategy =
2084 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002085 paramsChanged = true;
2086 fPersistentCache.reset();
2087 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002088 // To re-load shaders from the currently active programs, we flush all caches on one
2089 // frame, then set a flag to poll the cache on the next frame.
2090 static bool gLoadPending = false;
2091 if (gLoadPending) {
2092 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2093 int hitCount) {
2094 CachedGLSL& entry(fCachedGLSL.push_back());
2095 entry.fKey = key;
2096 SkMD5 hash;
2097 hash.write(key->bytes(), key->size());
2098 SkMD5::Digest digest = hash.finish();
2099 for (int i = 0; i < 16; ++i) {
2100 entry.fKeyString.appendf("%02x", digest.data[i]);
2101 }
2102
Brian Osmana66081d2019-09-03 14:59:26 -04002103 SkReader32 reader(data->data(), data->size());
2104 entry.fShaderType = reader.readU32();
2105 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2106 entry.fInputs,
2107 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002108 };
2109 fCachedGLSL.reset();
2110 fPersistentCache.foreach(collectShaders);
2111 gLoadPending = false;
2112 }
2113
2114 // Defer actually doing the load/save logic so that we can trigger a save when we
2115 // start or finish hovering on a tree node in the list below:
2116 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002117 bool doSave = ImGui::Button("Save");
2118 if (backendIsGL) {
2119 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002120 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2121 GrContextOptions::ShaderCacheStrategy::kSkSL;
2122 if (ImGui::Checkbox("SkSL", &sksl)) {
2123 params.fGrContextOptions.fShaderCacheStrategy = sksl
2124 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2125 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002126 paramsChanged = true;
2127 doLoad = true;
2128 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2129 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002130 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002131
2132 ImGui::BeginChild("##ScrollingRegion");
2133 for (auto& entry : fCachedGLSL) {
2134 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2135 bool hovered = ImGui::IsItemHovered();
2136 if (hovered != entry.fHovered) {
2137 // Force a save to patch the highlight shader in/out
2138 entry.fHovered = hovered;
2139 doSave = true;
2140 }
2141 if (inTreeNode) {
2142 // Full width, and a reasonable amount of space for each shader.
2143 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2144 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2145 boxSize);
2146 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2147 boxSize);
2148 ImGui::TreePop();
2149 }
2150 }
2151 ImGui::EndChild();
2152
2153 if (doLoad) {
2154 fPersistentCache.reset();
2155 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2156 gLoadPending = true;
2157 }
2158 if (doSave) {
2159 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002160 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002161 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2162 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002163
Brian Osman072e6fc2019-06-12 11:35:41 -04002164 SkSL::String highlight;
2165 if (!sksl) {
2166 highlight = shaderCaps->versionDeclString();
2167 if (shaderCaps->usesPrecisionModifiers()) {
2168 highlight.append("precision mediump float;\n");
2169 }
Brian Osman5bee3902019-05-07 09:55:45 -04002170 }
2171 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002172 highlight.appendf("out %s sk_FragColor;\n"
2173 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2174 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002175
2176 fPersistentCache.reset();
2177 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2178 for (auto& entry : fCachedGLSL) {
2179 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2180 if (entry.fHovered) {
2181 entry.fShader[kFragment_GrShaderType] = highlight;
2182 }
2183
Brian Osmana085a412019-04-25 09:44:43 -04002184 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2185 entry.fShader,
2186 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002187 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002188 fPersistentCache.store(*entry.fKey, *data);
2189
2190 entry.fShader[kFragment_GrShaderType] = backup;
2191 }
2192 }
2193 }
Brian Osman79086b92017-02-10 13:36:16 -05002194 }
Brian Salomon99a33902017-03-07 15:16:34 -05002195 if (paramsChanged) {
2196 fDeferredActions.push_back([=]() {
2197 fWindow->setRequestedDisplayParams(params);
2198 fWindow->inval();
2199 this->updateTitle();
2200 });
2201 }
Brian Osman79086b92017-02-10 13:36:16 -05002202 ImGui::End();
2203 }
2204
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002205 if (gShaderErrorHandler.fErrors.count()) {
2206 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2207 ImGui::Begin("Shader Errors");
2208 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2209 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002210 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2211 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2212 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2213 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002214 }
2215 ImGui::End();
2216 gShaderErrorHandler.reset();
2217 }
2218
Brian Osmanf6877092017-02-13 09:39:57 -05002219 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002220 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2221 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002222 static int zoomFactor = 8;
2223 if (ImGui::Button("<<")) {
2224 zoomFactor = SkTMax(zoomFactor / 2, 4);
2225 }
2226 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2227 if (ImGui::Button(">>")) {
2228 zoomFactor = SkTMin(zoomFactor * 2, 32);
2229 }
Brian Osmanf6877092017-02-13 09:39:57 -05002230
Ben Wagner3627d2e2018-06-26 14:23:20 -04002231 if (!fZoomWindowFixed) {
2232 ImVec2 mousePos = ImGui::GetMousePos();
2233 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2234 }
2235 SkScalar x = fZoomWindowLocation.x();
2236 SkScalar y = fZoomWindowLocation.y();
2237 int xInt = SkScalarRoundToInt(x);
2238 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002239 ImVec2 avail = ImGui::GetContentRegionAvail();
2240
Brian Osmanead517d2017-11-13 15:36:36 -05002241 uint32_t pixel = 0;
2242 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002243 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002244 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002245 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002246 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002247 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002248 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2249 }
2250
Brian Osmand67e5182017-12-08 16:46:09 -05002251 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002252 // Translate so the region of the image that's under the mouse cursor is centered
2253 // in the zoom canvas:
2254 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002255 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2256 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002257 c->drawImage(this->fLastImage, 0, 0);
2258
2259 SkPaint outline;
2260 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002261 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002262 });
Brian Osmanf6877092017-02-13 09:39:57 -05002263 }
2264
2265 ImGui::End();
2266 }
Brian Osman79086b92017-02-10 13:36:16 -05002267}
2268
liyuqian2edb0f42016-07-06 14:11:32 -07002269void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002270 for (int i = 0; i < fDeferredActions.count(); ++i) {
2271 fDeferredActions[i]();
2272 }
2273 fDeferredActions.reset();
2274
Brian Osman56a24812017-12-19 11:15:16 -05002275 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002276 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002277 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002278 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002279
Brian Osman79086b92017-02-10 13:36:16 -05002280 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002281 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2282 // not be visible, though. So we need to redraw if there is at least one visible window, or
2283 // more than one active window. Newly created windows are active but not visible for one frame
2284 // while they determine their layout and sizing.
2285 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2286 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002287 fWindow->inval();
2288 }
jvanverth9f372462016-04-06 06:08:59 -07002289}
liyuqiane5a6cd92016-05-27 08:52:52 -07002290
Florin Malitab632df72018-06-18 21:23:06 -04002291template <typename OptionsFunc>
2292static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2293 OptionsFunc&& optionsFunc) {
2294 writer.beginObject();
2295 {
2296 writer.appendString(kName , name);
2297 writer.appendString(kValue, value);
2298
2299 writer.beginArray(kOptions);
2300 {
2301 optionsFunc(writer);
2302 }
2303 writer.endArray();
2304 }
2305 writer.endObject();
2306}
2307
2308
liyuqiane5a6cd92016-05-27 08:52:52 -07002309void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002310 if (!fWindow) {
2311 return;
2312 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002313 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002314 return; // Surface hasn't been created yet.
2315 }
2316
Florin Malitab632df72018-06-18 21:23:06 -04002317 SkDynamicMemoryWStream memStream;
2318 SkJSONWriter writer(&memStream);
2319 writer.beginArray();
2320
liyuqianb73c24b2016-06-03 08:47:23 -07002321 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002322 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2323 [this](SkJSONWriter& writer) {
2324 for(const auto& slide : fSlides) {
2325 writer.appendString(slide->getName().c_str());
2326 }
2327 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002328
liyuqianb73c24b2016-06-03 08:47:23 -07002329 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002330 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2331 [](SkJSONWriter& writer) {
2332 for (const auto& str : kBackendTypeStrings) {
2333 writer.appendString(str);
2334 }
2335 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002336
csmartdalton578f0642017-02-24 16:04:47 -07002337 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002338 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2339 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2340 [this](SkJSONWriter& writer) {
2341 writer.appendS32(0);
2342
2343 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2344 return;
2345 }
2346
2347 for (int msaa : {4, 8, 16}) {
2348 writer.appendS32(msaa);
2349 }
2350 });
csmartdalton578f0642017-02-24 16:04:47 -07002351
csmartdalton61cd31a2017-02-27 17:00:53 -07002352 // Path renderer state
2353 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002354 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2355 [this](SkJSONWriter& writer) {
2356 const GrContext* ctx = fWindow->getGrContext();
2357 if (!ctx) {
2358 writer.appendString("Software");
2359 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002360 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002361
Florin Malitab632df72018-06-18 21:23:06 -04002362 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2363 if (fWindow->sampleCount() > 1) {
2364 if (caps->shaderCaps()->pathRenderingSupport()) {
2365 writer.appendString(
2366 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2367 }
2368 } else {
2369 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2370 writer.appendString(
2371 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2372 }
2373 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2374 }
2375 writer.appendString(
2376 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2377 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2378 }
2379 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002380
liyuqianb73c24b2016-06-03 08:47:23 -07002381 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002382 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2383 [this](SkJSONWriter& writer) {
2384 writer.appendString(kSoftkeyHint);
2385 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2386 writer.appendString(softkey.c_str());
2387 }
2388 });
liyuqianb73c24b2016-06-03 08:47:23 -07002389
Florin Malitab632df72018-06-18 21:23:06 -04002390 writer.endArray();
2391 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002392
Florin Malitab632df72018-06-18 21:23:06 -04002393 auto data = memStream.detachAsData();
2394
2395 // TODO: would be cool to avoid this copy
2396 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2397
2398 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002399}
2400
2401void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002402 // For those who will add more features to handle the state change in this function:
2403 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2404 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2405 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002406 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002407 for (int i = 0; i < fSlides.count(); ++i) {
2408 if (fSlides[i]->getName().equals(stateValue)) {
2409 this->setCurrentSlide(i);
2410 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002411 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002412 }
Florin Malitaab99c342018-01-16 16:23:03 -05002413
2414 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002415 } else if (stateName.equals(kBackendStateName)) {
2416 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2417 if (stateValue.equals(kBackendTypeStrings[i])) {
2418 if (fBackendType != i) {
2419 fBackendType = (sk_app::Window::BackendType)i;
2420 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002421 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002422 }
2423 break;
2424 }
2425 }
csmartdalton578f0642017-02-24 16:04:47 -07002426 } else if (stateName.equals(kMSAAStateName)) {
2427 DisplayParams params = fWindow->getRequestedDisplayParams();
2428 int sampleCount = atoi(stateValue.c_str());
2429 if (sampleCount != params.fMSAASampleCount) {
2430 params.fMSAASampleCount = sampleCount;
2431 fWindow->setRequestedDisplayParams(params);
2432 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002433 this->updateTitle();
2434 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002435 }
2436 } else if (stateName.equals(kPathRendererStateName)) {
2437 DisplayParams params = fWindow->getRequestedDisplayParams();
2438 for (const auto& pair : gPathRendererNames) {
2439 if (pair.second == stateValue.c_str()) {
2440 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2441 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2442 fWindow->setRequestedDisplayParams(params);
2443 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002444 this->updateTitle();
2445 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002446 }
2447 break;
2448 }
csmartdalton578f0642017-02-24 16:04:47 -07002449 }
liyuqianb73c24b2016-06-03 08:47:23 -07002450 } else if (stateName.equals(kSoftkeyStateName)) {
2451 if (!stateValue.equals(kSoftkeyHint)) {
2452 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002453 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002454 }
liyuqian2edb0f42016-07-06 14:11:32 -07002455 } else if (stateName.equals(kRefreshStateName)) {
2456 // This state is actually NOT in the UI state.
2457 // We use this to allow Android to quickly set bool fRefresh.
2458 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002459 } else {
2460 SkDebugf("Unknown stateName: %s", stateName.c_str());
2461 }
2462}
Brian Osman79086b92017-02-10 13:36:16 -05002463
Hal Canaryb1f411a2019-08-29 10:39:22 -04002464bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002465 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002466}
2467
Hal Canaryb1f411a2019-08-29 10:39:22 -04002468bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002469 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002470 fWindow->inval();
2471 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002472 } else {
2473 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002474 }
Brian Osman79086b92017-02-10 13:36:16 -05002475}