blob: 3d519108c3d5ce81209f03e5047a0f836b1bd208 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkGraphics.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkSurface.h"
14#include "include/gpu/GrContext.h"
15#include "include/private/SkTo.h"
16#include "include/utils/SkPaintFilterCanvas.h"
17#include "src/core/SkColorSpacePriv.h"
18#include "src/core/SkImagePriv.h"
19#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkOSFile.h"
21#include "src/core/SkScan.h"
22#include "src/core/SkTaskGroup.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrGpu.h"
25#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070026#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
28#include "src/utils/SkJSONWriter.h"
29#include "src/utils/SkOSPath.h"
30#include "tools/Resources.h"
31#include "tools/ToolUtils.h"
32#include "tools/flags/CommandLineFlags.h"
33#include "tools/flags/CommonFlags.h"
34#include "tools/trace/EventTracingPriv.h"
35#include "tools/viewer/BisectSlide.h"
36#include "tools/viewer/GMSlide.h"
37#include "tools/viewer/ImageSlide.h"
38#include "tools/viewer/ParticlesSlide.h"
39#include "tools/viewer/SKPSlide.h"
40#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050041#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "tools/viewer/SlideDir.h"
43#include "tools/viewer/SvgSlide.h"
44#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070045
Hal Canaryc640d0d2018-06-13 09:59:02 -040046#include <stdlib.h>
47#include <map>
48
Hal Canary8a001442018-09-19 11:31:27 -040049#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040050#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040051
Florin Malita87ccf332018-05-04 12:23:24 -040052#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040054#endif
55
Brian Osman5e7fbfd2019-05-03 13:13:35 -040056class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
57public:
58 void compileError(const char* shader, const char* errors) override {
59 fShaders.push_back(SkString(shader));
60 fErrors.push_back(SkString(errors));
61 }
62
63 void reset() {
64 fShaders.reset();
65 fErrors.reset();
66 }
67
68 SkTArray<SkString> fShaders;
69 SkTArray<SkString> fErrors;
70};
71
72static CapturingShaderErrorHandler gShaderErrorHandler;
73
jvanverth34524262016-05-04 13:49:13 -070074using namespace sk_app;
75
csmartdalton61cd31a2017-02-27 17:00:53 -070076static std::map<GpuPathRenderers, std::string> gPathRendererNames;
77
jvanverth9f372462016-04-06 06:08:59 -070078Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070079 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070080}
81
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060082static DEFINE_string(slide, "", "Start on this sample.");
83static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050084
Stephen Whitea800ec92019-08-02 15:04:52 -040085#if defined(SK_VULKAN)
jvanverthb8794cc2016-07-27 14:29:18 -070086# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050087#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
88# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
Stephen Whitea800ec92019-08-02 15:04:52 -040089#elif defined(SK_DAWN)
90# define BACKENDS_STR "\"sw\", \"gl\", and \"dawn\""
bsalomon6c471f72016-07-26 12:56:32 -070091#else
92# define BACKENDS_STR "\"sw\" and \"gl\""
93#endif
94
Brian Osman2dd96932016-10-18 15:33:53 -040095static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070096
Mike Klein5b3f3432019-03-21 11:42:21 -050097static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070098
Mike Klein84836b72019-03-21 11:31:36 -050099static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700100
Mike Klein84836b72019-03-21 11:31:36 -0500101static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400102
Mike Kleinc6142d82019-03-25 10:54:59 -0500103static DEFINE_string2(match, m, nullptr,
104 "[~][^]substring[$] [...] of name to run.\n"
105 "Multiple matches may be separated by spaces.\n"
106 "~ causes a matching name to always be skipped\n"
107 "^ requires the start of the name to match\n"
108 "$ requires the end of the name to match\n"
109 "^ and $ requires an exact match\n"
110 "If a name does not match any list entry,\n"
111 "it is skipped unless some list entry starts with ~");
112
Mike Klein19fb3972019-03-21 13:08:08 -0500113#if defined(SK_BUILD_FOR_ANDROID)
114 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500115 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
116 static DEFINE_string(lotties, "/data/local/tmp/lotties",
117 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500118#else
119 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500120 static DEFINE_string(skps, "skps", "Directory to read skps from.");
121 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500122#endif
123
Mike Kleinc6142d82019-03-25 10:54:59 -0500124static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
125
126static DEFINE_int_2(threads, j, -1,
127 "Run threadsafe tests on a threadpool with this many extra threads, "
128 "defaulting to one extra thread per core.");
129
Jim Van Verth7b558182019-11-14 16:47:01 -0500130static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
131
Chris Daltonc8877332020-01-06 09:48:30 -0700132static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Reed862818b2020-03-21 15:07:13 -0400133static DEFINE_bool(skvm, false, "Try to use skvm blitters for raster.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500134
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400135#ifndef SK_GL
136static_assert(false, "viewer requires GL backend for raster.")
137#endif
138
Brian Salomon194db172017-08-17 14:37:06 -0400139const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700140 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400141#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
142 "ANGLE",
143#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400144#ifdef SK_DAWN
145 "Dawn",
146#endif
jvanverth063ece72016-06-17 09:29:14 -0700147#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700148 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700149#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400150#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500151 "Metal",
152#endif
csmartdalton578f0642017-02-24 16:04:47 -0700153 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700154};
155
bsalomon6c471f72016-07-26 12:56:32 -0700156static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400157#ifdef SK_DAWN
158 if (0 == strcmp(str, "dawn")) {
159 return sk_app::Window::kDawn_BackendType;
160 } else
161#endif
bsalomon6c471f72016-07-26 12:56:32 -0700162#ifdef SK_VULKAN
163 if (0 == strcmp(str, "vk")) {
164 return sk_app::Window::kVulkan_BackendType;
165 } else
166#endif
Brian Salomon194db172017-08-17 14:37:06 -0400167#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
168 if (0 == strcmp(str, "angle")) {
169 return sk_app::Window::kANGLE_BackendType;
170 } else
171#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400172#ifdef SK_METAL
173 if (0 == strcmp(str, "mtl")) {
174 return sk_app::Window::kMetal_BackendType;
175 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500176#endif
bsalomon6c471f72016-07-26 12:56:32 -0700177 if (0 == strcmp(str, "gl")) {
178 return sk_app::Window::kNativeGL_BackendType;
179 } else if (0 == strcmp(str, "sw")) {
180 return sk_app::Window::kRaster_BackendType;
181 } else {
182 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
183 return sk_app::Window::kRaster_BackendType;
184 }
185}
186
Brian Osmana109e392017-02-24 09:49:14 -0500187static SkColorSpacePrimaries gSrgbPrimaries = {
188 0.64f, 0.33f,
189 0.30f, 0.60f,
190 0.15f, 0.06f,
191 0.3127f, 0.3290f };
192
193static SkColorSpacePrimaries gAdobePrimaries = {
194 0.64f, 0.33f,
195 0.21f, 0.71f,
196 0.15f, 0.06f,
197 0.3127f, 0.3290f };
198
199static SkColorSpacePrimaries gP3Primaries = {
200 0.680f, 0.320f,
201 0.265f, 0.690f,
202 0.150f, 0.060f,
203 0.3127f, 0.3290f };
204
205static SkColorSpacePrimaries gRec2020Primaries = {
206 0.708f, 0.292f,
207 0.170f, 0.797f,
208 0.131f, 0.046f,
209 0.3127f, 0.3290f };
210
211struct NamedPrimaries {
212 const char* fName;
213 SkColorSpacePrimaries* fPrimaries;
214} gNamedPrimaries[] = {
215 { "sRGB", &gSrgbPrimaries },
216 { "AdobeRGB", &gAdobePrimaries },
217 { "P3", &gP3Primaries },
218 { "Rec. 2020", &gRec2020Primaries },
219};
220
221static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
222 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
223}
224
Brian Osman70d2f432017-11-08 09:54:10 -0500225static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
226 // In raster mode, we still use GL for the window.
227 // This lets us render the GUI faster (and correct).
228 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
229}
230
Jim Van Verth74826c82019-03-01 14:37:30 -0500231class NullSlide : public Slide {
232 SkISize getDimensions() const override {
233 return SkISize::Make(640, 480);
234 }
235
236 void draw(SkCanvas* canvas) override {
237 canvas->clear(0xffff11ff);
238 }
239};
240
liyuqiane5a6cd92016-05-27 08:52:52 -0700241const char* kName = "name";
242const char* kValue = "value";
243const char* kOptions = "options";
244const char* kSlideStateName = "Slide";
245const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700246const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700247const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700248const char* kSoftkeyStateName = "Softkey";
249const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700250const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700251const char* kON = "ON";
252const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700253const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700254
Mike Reed862818b2020-03-21 15:07:13 -0400255extern bool gUseSkVMBlitter;
256
jvanverth34524262016-05-04 13:49:13 -0700257Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500258 : fCurrentSlide(-1)
259 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500260 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400261 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500262 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500263 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500264 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500265 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400266 , fZoomWindowFixed(false)
267 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500268 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400269 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700270 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500271 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500272 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500273 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500274 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700275 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400276 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400277 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400278 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500279 , fTiled(false)
280 , fDrawTileBoundaries(false)
281 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400282 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700283{
Greg Daniel285db442016-10-14 09:12:53 -0400284 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700285
Chris Dalton37ae4b02019-12-28 14:51:11 -0700286 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Daltonb832ce62020-01-06 19:49:37 -0700287 gPathRendererNames[GpuPathRenderers::kGpuTessellation] = "GPU Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500288 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500289 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600290 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500291 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
292 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700293
jvanverth2bb3b6d2016-04-08 07:24:09 -0700294 SkDebugf("Command line arguments: ");
295 for (int i = 1; i < argc; ++i) {
296 SkDebugf("%s ", argv[i]);
297 }
298 SkDebugf("\n");
299
Mike Klein88544fb2019-03-20 10:50:33 -0500300 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500301#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400302 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500303#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700304
Mike Reed862818b2020-03-21 15:07:13 -0400305 gUseSkVMBlitter = FLAGS_skvm;
306
Mike Klein19cc0f62019-03-22 15:30:07 -0500307 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500308
Brian Osmanbc8150f2017-07-24 11:38:01 -0400309 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400310 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400311
bsalomon6c471f72016-07-26 12:56:32 -0700312 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700313 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700314
csmartdalton578f0642017-02-24 16:04:47 -0700315 DisplayParams displayParams;
316 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700317 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400318 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400319 displayParams.fGrContextOptions.fShaderCacheStrategy =
320 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400321 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
322 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700323 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500324 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700325
Brian Osman56a24812017-12-19 11:15:16 -0500326 // Configure timers
327 fStatsLayer.setActive(false);
328 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
329 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
330 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
331
jvanverth9f372462016-04-06 06:08:59 -0700332 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700333 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500334 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500335 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500336 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700337
brianosman622c8d52016-05-10 06:50:49 -0700338 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500339 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
340 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
341 fWindow->inval();
342 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500343 // Command to jump directly to the slide picker and give it focus
344 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
345 this->fShowImGuiDebugWindow = true;
346 this->fShowSlidePicker = true;
347 fWindow->inval();
348 });
349 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400350 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500351 this->fShowImGuiDebugWindow = true;
352 this->fShowSlidePicker = true;
353 fWindow->inval();
354 });
Brian Osman79086b92017-02-10 13:36:16 -0500355 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
356 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
357 fWindow->inval();
358 });
Brian Osmanf6877092017-02-13 09:39:57 -0500359 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
360 this->fShowZoomWindow = !this->fShowZoomWindow;
361 fWindow->inval();
362 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400363 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
364 this->fZoomWindowFixed = !this->fZoomWindowFixed;
365 fWindow->inval();
366 });
Greg Danield0794cc2019-03-27 16:23:26 -0400367 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
368 DisplayParams params = fWindow->getRequestedDisplayParams();
369 params.fDisableVsync = !params.fDisableVsync;
370 fWindow->setRequestedDisplayParams(params);
371 this->updateTitle();
372 fWindow->inval();
373 });
Mike Reedf702ed42019-07-22 17:00:49 -0400374 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
375 fRefresh = !fRefresh;
376 fWindow->inval();
377 });
brianosman622c8d52016-05-10 06:50:49 -0700378 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500379 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700380 fWindow->inval();
381 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400382 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500383 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400384 this->updateTitle();
385 fWindow->inval();
386 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500387 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500388 switch (fColorMode) {
389 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500390 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500391 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500392 case ColorMode::kColorManaged8888:
393 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500394 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500395 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400396 this->setColorMode(ColorMode::kColorManagedF16Norm);
397 break;
398 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500399 this->setColorMode(ColorMode::kLegacy);
400 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500401 }
brianosman622c8d52016-05-10 06:50:49 -0700402 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700403 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
404 DisplayParams params = fWindow->getRequestedDisplayParams();
405 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
406 fWindow->setRequestedDisplayParams(params);
407 fWindow->inval();
408 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400409 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500410 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700411 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400412 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500413 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700414 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400415 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700416 this->changeZoomLevel(1.f / 32.f);
417 fWindow->inval();
418 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400419 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700420 this->changeZoomLevel(-1.f / 32.f);
421 fWindow->inval();
422 });
jvanverthaf236b52016-05-20 06:01:06 -0700423 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400424 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
425 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500426 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400427#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
428 if (newBackend == sk_app::Window::kVulkan_BackendType) {
429 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
430 sk_app::Window::kBackendTypeCount);
431 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
432 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500433 }
434#endif
Brian Osman621491e2017-02-28 15:45:01 -0500435 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700436 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500437 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
438 fSaveToSKP = true;
439 fWindow->inval();
440 });
Mike Reed376d8122019-03-14 11:39:02 -0400441 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
442 fShowSlideDimensions = !fShowSlideDimensions;
443 fWindow->inval();
444 });
Ben Wagner37c54032018-04-13 14:30:23 -0400445 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
446 DisplayParams params = fWindow->getRequestedDisplayParams();
447 uint32_t flags = params.fSurfaceProps.flags();
448 if (!fPixelGeometryOverrides) {
449 fPixelGeometryOverrides = true;
450 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
451 } else {
452 switch (params.fSurfaceProps.pixelGeometry()) {
453 case kUnknown_SkPixelGeometry:
454 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
455 break;
456 case kRGB_H_SkPixelGeometry:
457 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
458 break;
459 case kBGR_H_SkPixelGeometry:
460 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
461 break;
462 case kRGB_V_SkPixelGeometry:
463 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
464 break;
465 case kBGR_V_SkPixelGeometry:
466 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
467 fPixelGeometryOverrides = false;
468 break;
469 }
470 }
471 fWindow->setRequestedDisplayParams(params);
472 this->updateTitle();
473 fWindow->inval();
474 });
Ben Wagner9613e452019-01-23 10:34:59 -0500475 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500476 if (!fFontOverrides.fHinting) {
477 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400478 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500479 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500480 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400481 case SkFontHinting::kNone:
482 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500483 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400484 case SkFontHinting::kSlight:
485 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500486 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400487 case SkFontHinting::kNormal:
488 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500489 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400490 case SkFontHinting::kFull:
491 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500492 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500493 break;
494 }
495 }
496 this->updateTitle();
497 fWindow->inval();
498 });
499 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500500 if (!fPaintOverrides.fAntiAlias) {
501 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
502 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500503 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500504 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500505 } else {
506 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500507 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500508 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500509 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400510 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500511 break;
512 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500513 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500514 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400515 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500516 break;
517 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500518 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400519 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500520 break;
521 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500522 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
523 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500524 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
525 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500526 break;
527 }
528 }
529 this->updateTitle();
530 fWindow->inval();
531 });
Ben Wagner37c54032018-04-13 14:30:23 -0400532 fCommands.addCommand('D', "Modes", "DFT", [this]() {
533 DisplayParams params = fWindow->getRequestedDisplayParams();
534 uint32_t flags = params.fSurfaceProps.flags();
535 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
536 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
537 fWindow->setRequestedDisplayParams(params);
538 this->updateTitle();
539 fWindow->inval();
540 });
Ben Wagner9613e452019-01-23 10:34:59 -0500541 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500542 if (!fFontOverrides.fEdging) {
543 fFontOverrides.fEdging = true;
544 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500545 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500546 switch (fFont.getEdging()) {
547 case SkFont::Edging::kAlias:
548 fFont.setEdging(SkFont::Edging::kAntiAlias);
549 break;
550 case SkFont::Edging::kAntiAlias:
551 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
552 break;
553 case SkFont::Edging::kSubpixelAntiAlias:
554 fFont.setEdging(SkFont::Edging::kAlias);
555 fFontOverrides.fEdging = false;
556 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500557 }
558 }
559 this->updateTitle();
560 fWindow->inval();
561 });
Ben Wagner9613e452019-01-23 10:34:59 -0500562 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500563 if (!fFontOverrides.fSubpixel) {
564 fFontOverrides.fSubpixel = true;
565 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500566 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500567 if (!fFont.isSubpixel()) {
568 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500569 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500570 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500571 }
572 }
573 this->updateTitle();
574 fWindow->inval();
575 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400576 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
577 if (!fFontOverrides.fBaselineSnap) {
578 fFontOverrides.fBaselineSnap = true;
579 fFont.setBaselineSnap(false);
580 } else {
581 if (!fFont.isBaselineSnap()) {
582 fFont.setBaselineSnap(true);
583 } else {
584 fFontOverrides.fBaselineSnap = false;
585 }
586 }
587 this->updateTitle();
588 fWindow->inval();
589 });
Brian Osman805a7272018-05-02 15:40:20 -0400590 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
591 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
592 : kPerspective_Real;
593 this->updateTitle();
594 fWindow->inval();
595 });
596 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
597 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
598 : kPerspective_Off;
599 this->updateTitle();
600 fWindow->inval();
601 });
Brian Osman207d4102019-01-10 09:40:58 -0500602 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
603 fAnimTimer.togglePauseResume();
604 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400605 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
606 fZoomUI = !fZoomUI;
607 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
608 fWindow->inval();
609 });
Mike Reed59295352020-03-12 13:56:34 -0400610 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
611 fDrawViaSerialize = !fDrawViaSerialize;
612 this->updateTitle();
613 fWindow->inval();
614 });
Mike Reed862818b2020-03-21 15:07:13 -0400615 fCommands.addCommand('!', "SkVM", "Toggle SkVM", [this]() {
616 gUseSkVMBlitter = !gUseSkVMBlitter;
617 this->updateTitle();
618 fWindow->inval();
619 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500620
jvanverth2bb3b6d2016-04-08 07:24:09 -0700621 // set up slides
622 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500623 if (FLAGS_list) {
624 this->listNames();
625 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700626
Brian Osman9bb47cf2018-04-26 15:55:00 -0400627 fPerspectivePoints[0].set(0, 0);
628 fPerspectivePoints[1].set(1, 0);
629 fPerspectivePoints[2].set(0, 1);
630 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700631 fAnimTimer.run();
632
Hal Canaryc465d132017-12-08 10:21:31 -0500633 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500634 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400635 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500636 }
637 fImGuiGamutPaint.setColor(SK_ColorWHITE);
638 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
639
jongdeok.kim804f17e2019-02-26 14:39:23 +0900640 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500641 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700642}
643
jvanverth34524262016-05-04 13:49:13 -0700644void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400645 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
646 static const struct {
647 const char* fExtension;
648 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500649 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400650 const SlideFactory fFactory;
651 } gExternalSlidesInfo[] = {
652 { ".skp", "skp-dir", FLAGS_skps,
653 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
654 return sk_make_sp<SKPSlide>(name, path);}
655 },
656 { ".jpg", "jpg-dir", FLAGS_jpgs,
657 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
658 return sk_make_sp<ImageSlide>(name, path);}
659 },
Florin Malita87ccf332018-05-04 12:23:24 -0400660#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400661 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400662 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
663 return sk_make_sp<SkottieSlide>(name, path);}
664 },
Florin Malita87ccf332018-05-04 12:23:24 -0400665#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400666#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400667 { ".svg", "svg-dir", FLAGS_svgs,
668 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
669 return sk_make_sp<SvgSlide>(name, path);}
670 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400671#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400672 };
jvanverthc265a922016-04-08 12:51:45 -0700673
Brian Salomon343553a2018-09-05 15:41:23 -0400674 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700675
Mike Klein88544fb2019-03-20 10:50:33 -0500676 const auto addSlide =
677 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
678 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
679 return;
680 }
liyuqian6f163d22016-06-13 12:26:45 -0700681
Mike Klein88544fb2019-03-20 10:50:33 -0500682 if (auto slide = fact(name, path)) {
683 dirSlides.push_back(slide);
684 fSlides.push_back(std::move(slide));
685 }
686 };
Florin Malita76a076b2018-02-15 18:40:48 -0500687
Florin Malita38792ce2018-05-08 10:36:18 -0400688 if (!FLAGS_file.isEmpty()) {
689 // single file mode
690 const SkString file(FLAGS_file[0]);
691
692 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
693 for (const auto& sinfo : gExternalSlidesInfo) {
694 if (file.endsWith(sinfo.fExtension)) {
695 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
696 return;
697 }
698 }
699
700 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
701 } else {
702 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
703 }
704
705 return;
706 }
707
708 // Bisect slide.
709 if (!FLAGS_bisect.isEmpty()) {
710 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500711 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400712 if (FLAGS_bisect.count() >= 2) {
713 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
714 bisect->onChar(*ch);
715 }
716 }
717 fSlides.push_back(std::move(bisect));
718 }
719 }
720
721 // GMs
722 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400723 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400724 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500725 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400726 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400727 fSlides.push_back(std::move(slide));
728 }
Florin Malita38792ce2018-05-08 10:36:18 -0400729 }
730 // reverse gms
731 int numGMs = fSlides.count() - firstGM;
732 for (int i = 0; i < numGMs/2; ++i) {
733 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
734 }
735
736 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400737 for (const SampleFactory factory : SampleRegistry::Range()) {
738 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500739 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400740 fSlides.push_back(slide);
741 }
Florin Malita38792ce2018-05-08 10:36:18 -0400742 }
743
Brian Osman7c979f52019-02-12 13:27:51 -0500744 // Particle demo
745 {
746 // TODO: Convert this to a sample
747 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500748 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500749 fSlides.push_back(std::move(slide));
750 }
751 }
752
Brian Osmand927bd22019-12-18 11:23:12 -0500753 // Runtime shader editor
754 {
755 sk_sp<Slide> slide(new SkSLSlide());
756 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
757 fSlides.push_back(std::move(slide));
758 }
759 }
760
Florin Malita0ffa3222018-04-05 14:34:45 -0400761 for (const auto& info : gExternalSlidesInfo) {
762 for (const auto& flag : info.fFlags) {
763 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
764 // single file
765 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
766 } else {
767 // directory
768 SkOSFile::Iter it(flag.c_str(), info.fExtension);
769 SkString name;
770 while (it.next(&name)) {
771 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
772 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400773 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400774 if (!dirSlides.empty()) {
775 fSlides.push_back(
776 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
777 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500778 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400779 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400780 }
781 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500782
783 if (!fSlides.count()) {
784 sk_sp<Slide> slide(new NullSlide());
785 fSlides.push_back(std::move(slide));
786 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700787}
788
789
jvanverth34524262016-05-04 13:49:13 -0700790Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700791 fWindow->detach();
792 delete fWindow;
793}
794
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500795struct SkPaintTitleUpdater {
796 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
797 void append(const char* s) {
798 if (fCount == 0) {
799 fTitle->append(" {");
800 } else {
801 fTitle->append(", ");
802 }
803 fTitle->append(s);
804 ++fCount;
805 }
806 void done() {
807 if (fCount > 0) {
808 fTitle->append("}");
809 }
810 }
811 SkString* fTitle;
812 int fCount;
813};
814
brianosman05de2162016-05-06 13:28:57 -0700815void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700816 if (!fWindow) {
817 return;
818 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500819 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700820 return; // Surface hasn't been created yet.
821 }
822
jvanverth34524262016-05-04 13:49:13 -0700823 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700824 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700825
Mike Kleine5acd752019-03-22 09:57:16 -0500826 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400827 if (gSkForceAnalyticAA) {
828 title.append(" <FAAA>");
829 } else {
830 title.append(" <AAA>");
831 }
832 }
Mike Reed59295352020-03-12 13:56:34 -0400833 if (fDrawViaSerialize) {
834 title.append(" <serialize>");
835 }
Mike Reed862818b2020-03-21 15:07:13 -0400836 if (gUseSkVMBlitter) {
837 title.append(" <skvm>");
838 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400839
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500840 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500841 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
842 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400843 const char* on, const char* off)
844 {
Ben Wagner9613e452019-01-23 10:34:59 -0500845 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400846 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500847 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400848 };
849
Ben Wagner9613e452019-01-23 10:34:59 -0500850 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
851 const char* on, const char* off)
852 {
853 if (fFontOverrides.*flag) {
854 paintTitle.append((fFont.*isFlag)() ? on : off);
855 }
856 };
857
858 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
859 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500860 if (fPaintOverrides.fFilterQuality) {
861 switch (fPaint.getFilterQuality()) {
862 case kNone_SkFilterQuality:
863 paintTitle.append("NoFilter");
864 break;
865 case kLow_SkFilterQuality:
866 paintTitle.append("LowFilter");
867 break;
868 case kMedium_SkFilterQuality:
869 paintTitle.append("MediumFilter");
870 break;
871 case kHigh_SkFilterQuality:
872 paintTitle.append("HighFilter");
873 break;
874 }
875 }
Ben Wagner9613e452019-01-23 10:34:59 -0500876
877 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
878 "Force Autohint", "No Force Autohint");
879 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400880 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500881 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
882 "Linear Metrics", "Non-Linear Metrics");
883 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
884 "Bitmap Text", "No Bitmap Text");
885 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
886
887 if (fFontOverrides.fEdging) {
888 switch (fFont.getEdging()) {
889 case SkFont::Edging::kAlias:
890 paintTitle.append("Alias Text");
891 break;
892 case SkFont::Edging::kAntiAlias:
893 paintTitle.append("Antialias Text");
894 break;
895 case SkFont::Edging::kSubpixelAntiAlias:
896 paintTitle.append("Subpixel Antialias Text");
897 break;
898 }
899 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400900
Mike Reed3ae47332019-01-04 10:11:46 -0500901 if (fFontOverrides.fHinting) {
902 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400903 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500904 paintTitle.append("No Hinting");
905 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400906 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500907 paintTitle.append("Slight Hinting");
908 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400909 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500910 paintTitle.append("Normal Hinting");
911 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400912 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500913 paintTitle.append("Full Hinting");
914 break;
915 }
916 }
917 paintTitle.done();
918
Brian Osman92004802017-03-06 11:47:26 -0500919 switch (fColorMode) {
920 case ColorMode::kLegacy:
921 title.append(" Legacy 8888");
922 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500923 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500924 title.append(" ColorManaged 8888");
925 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500926 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500927 title.append(" ColorManaged F16");
928 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400929 case ColorMode::kColorManagedF16Norm:
930 title.append(" ColorManaged F16 Norm");
931 break;
Brian Osman92004802017-03-06 11:47:26 -0500932 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500933
Brian Osman92004802017-03-06 11:47:26 -0500934 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500935 int curPrimaries = -1;
936 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
937 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
938 curPrimaries = i;
939 break;
940 }
941 }
Brian Osman03115dc2018-11-26 13:55:19 -0500942 title.appendf(" %s Gamma %f",
943 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500944 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700945 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500946
Ben Wagner37c54032018-04-13 14:30:23 -0400947 const DisplayParams& params = fWindow->getRequestedDisplayParams();
948 if (fPixelGeometryOverrides) {
949 switch (params.fSurfaceProps.pixelGeometry()) {
950 case kUnknown_SkPixelGeometry:
951 title.append( " Flat");
952 break;
953 case kRGB_H_SkPixelGeometry:
954 title.append( " RGB");
955 break;
956 case kBGR_H_SkPixelGeometry:
957 title.append( " BGR");
958 break;
959 case kRGB_V_SkPixelGeometry:
960 title.append( " RGBV");
961 break;
962 case kBGR_V_SkPixelGeometry:
963 title.append( " BGRV");
964 break;
965 }
966 }
967
968 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
969 title.append(" DFT");
970 }
971
csmartdalton578f0642017-02-24 16:04:47 -0700972 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700973 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500974 int msaa = fWindow->sampleCount();
975 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700976 title.appendf(" MSAA: %i", msaa);
977 }
978 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700979
980 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -0700981 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700982 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
983 }
984
Brian Osman805a7272018-05-02 15:40:20 -0400985 if (kPerspective_Real == fPerspectiveMode) {
986 title.append(" Perpsective (Real)");
987 } else if (kPerspective_Fake == fPerspectiveMode) {
988 title.append(" Perspective (Fake)");
989 }
990
brianosman05de2162016-05-06 13:28:57 -0700991 fWindow->setTitle(title.c_str());
992}
993
Florin Malitaab99c342018-01-16 16:23:03 -0500994int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500995
996 if (!FLAGS_slide.isEmpty()) {
997 int count = fSlides.count();
998 for (int i = 0; i < count; i++) {
999 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001000 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001001 }
1002 }
1003
1004 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1005 this->listNames();
1006 }
1007
Florin Malitaab99c342018-01-16 16:23:03 -05001008 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001009}
1010
Florin Malitaab99c342018-01-16 16:23:03 -05001011void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001012 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001013 for (const auto& slide : fSlides) {
1014 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001015 }
1016}
1017
Florin Malitaab99c342018-01-16 16:23:03 -05001018void Viewer::setCurrentSlide(int slide) {
1019 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001020
Florin Malitaab99c342018-01-16 16:23:03 -05001021 if (slide == fCurrentSlide) {
1022 return;
1023 }
1024
1025 if (fCurrentSlide >= 0) {
1026 fSlides[fCurrentSlide]->unload();
1027 }
1028
1029 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1030 SkIntToScalar(fWindow->height()));
1031 fCurrentSlide = slide;
1032 this->setupCurrentSlide();
1033}
1034
1035void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001036 if (fCurrentSlide >= 0) {
1037 // prepare dimensions for image slides
1038 fGesture.resetTouchState();
1039 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001040
Jim Van Verth0848fb02018-01-22 13:39:30 -05001041 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1042 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1043 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001044
Jim Van Verth0848fb02018-01-22 13:39:30 -05001045 // Start with a matrix that scales the slide to the available screen space
1046 if (fWindow->scaleContentToFit()) {
1047 if (windowRect.width() > 0 && windowRect.height() > 0) {
1048 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1049 }
liyuqiane46e4f02016-05-20 07:32:19 -07001050 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001051
1052 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001053 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001054
1055 this->updateTitle();
1056 this->updateUIState();
1057
1058 fStatsLayer.resetMeasurements();
1059
1060 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001061 }
jvanverthc265a922016-04-08 12:51:45 -07001062}
1063
Brian Osmanaba642c2020-02-06 12:52:25 -05001064#define MAX_ZOOM_LEVEL 8.0f
1065#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001066
jvanverth34524262016-05-04 13:49:13 -07001067void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001068 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001069 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001070 this->preTouchMatrixChanged();
1071}
Yuqian Li755778c2018-03-28 16:23:31 -04001072
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001073void Viewer::preTouchMatrixChanged() {
1074 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001075 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1076 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1077 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1078 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1079}
1080
Brian Osman805a7272018-05-02 15:40:20 -04001081SkMatrix Viewer::computePerspectiveMatrix() {
1082 SkScalar w = fWindow->width(), h = fWindow->height();
1083 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1084 SkPoint perspPts[4] = {
1085 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1086 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1087 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1088 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1089 };
1090 SkMatrix m;
1091 m.setPolyToPoly(orthoPts, perspPts, 4);
1092 return m;
1093}
1094
Yuqian Li755778c2018-03-28 16:23:31 -04001095SkMatrix Viewer::computePreTouchMatrix() {
1096 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001097
1098 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001099 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001100 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001101
1102 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1103 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001104
Brian Osman805a7272018-05-02 15:40:20 -04001105 if (kPerspective_Real == fPerspectiveMode) {
1106 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001107 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001108 }
1109
Yuqian Li755778c2018-03-28 16:23:31 -04001110 return m;
jvanverthc265a922016-04-08 12:51:45 -07001111}
1112
liyuqiand3cdbca2016-05-17 12:44:20 -07001113SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001114 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001115 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001116 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001117 return m;
jvanverthc265a922016-04-08 12:51:45 -07001118}
1119
Brian Osman621491e2017-02-28 15:45:01 -05001120void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001121 fPersistentCache.reset();
1122 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001123 fBackendType = backendType;
1124
1125 fWindow->detach();
1126
Brian Osman70d2f432017-11-08 09:54:10 -05001127#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001128 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1129 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001130 DisplayParams params = fWindow->getRequestedDisplayParams();
1131 delete fWindow;
1132 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001133
Brian Osman70d2f432017-11-08 09:54:10 -05001134 // re-register callbacks
1135 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001136 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001137 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001138 fWindow->pushLayer(&fImGuiLayer);
1139
Brian Osman70d2f432017-11-08 09:54:10 -05001140 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1141 // will still include our correct sample count. But the re-created fWindow will lose that
1142 // information. On Windows, we need to re-create the window when changing sample count,
1143 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1144 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1145 // as if we had never changed windows at all).
1146 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001147#endif
1148
Brian Osman70d2f432017-11-08 09:54:10 -05001149 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001150}
1151
Brian Osman92004802017-03-06 11:47:26 -05001152void Viewer::setColorMode(ColorMode colorMode) {
1153 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001154 this->updateTitle();
1155 fWindow->inval();
1156}
1157
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001158class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1159public:
Mike Reed3ae47332019-01-04 10:11:46 -05001160 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1161 SkFont* font, Viewer::SkFontFields* ffields)
1162 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001163 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001164 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1165 sk_sp<SkTextBlob>* cache) {
1166 bool blobWillChange = false;
1167 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001168 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1169 bool shouldDraw = this->filterFont(&filteredFont);
1170 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001171 blobWillChange = true;
1172 break;
1173 }
1174 }
1175 if (!blobWillChange) {
1176 return blob;
1177 }
1178
1179 SkTextBlobBuilder builder;
1180 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001181 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1182 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001183 if (!shouldDraw) {
1184 continue;
1185 }
1186
Mike Reed3ae47332019-01-04 10:11:46 -05001187 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001188
Ben Wagner41e40472018-09-24 13:01:54 -04001189 const SkTextBlobBuilder::RunBuffer& runBuffer
1190 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001191 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001192 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001193 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001194 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001195 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001196 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001197 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001198 it.glyphCount(), it.textSize(), SkString())
1199 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1200 uint32_t glyphCount = it.glyphCount();
1201 if (it.glyphs()) {
1202 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1203 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1204 }
1205 if (it.pos()) {
1206 size_t posSize = sizeof(decltype(*it.pos()));
1207 uint8_t positioning = it.positioning();
1208 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1209 }
1210 if (it.text()) {
1211 size_t textSize = sizeof(decltype(*it.text()));
1212 uint32_t textCount = it.textSize();
1213 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1214 }
1215 if (it.clusters()) {
1216 size_t clusterSize = sizeof(decltype(*it.clusters()));
1217 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1218 }
1219 }
1220 *cache = builder.make();
1221 return cache->get();
1222 }
1223 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1224 const SkPaint& paint) override {
1225 sk_sp<SkTextBlob> cache;
1226 this->SkPaintFilterCanvas::onDrawTextBlob(
1227 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1228 }
Mike Reed3ae47332019-01-04 10:11:46 -05001229 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001230 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001231 font->writable()->setSize(fFont->getSize());
1232 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001233 if (fFontOverrides->fScaleX) {
1234 font->writable()->setScaleX(fFont->getScaleX());
1235 }
1236 if (fFontOverrides->fSkewX) {
1237 font->writable()->setSkewX(fFont->getSkewX());
1238 }
Mike Reed3ae47332019-01-04 10:11:46 -05001239 if (fFontOverrides->fHinting) {
1240 font->writable()->setHinting(fFont->getHinting());
1241 }
Ben Wagner9613e452019-01-23 10:34:59 -05001242 if (fFontOverrides->fEdging) {
1243 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001244 }
Ben Wagner9613e452019-01-23 10:34:59 -05001245 if (fFontOverrides->fEmbolden) {
1246 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001247 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001248 if (fFontOverrides->fBaselineSnap) {
1249 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1250 }
Ben Wagner9613e452019-01-23 10:34:59 -05001251 if (fFontOverrides->fLinearMetrics) {
1252 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001253 }
Ben Wagner9613e452019-01-23 10:34:59 -05001254 if (fFontOverrides->fSubpixel) {
1255 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001256 }
Ben Wagner9613e452019-01-23 10:34:59 -05001257 if (fFontOverrides->fEmbeddedBitmaps) {
1258 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001259 }
Ben Wagner9613e452019-01-23 10:34:59 -05001260 if (fFontOverrides->fForceAutoHinting) {
1261 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001262 }
Ben Wagner9613e452019-01-23 10:34:59 -05001263
Mike Reed3ae47332019-01-04 10:11:46 -05001264 return true;
1265 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001266 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001267 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001268 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001269 }
Ben Wagner9613e452019-01-23 10:34:59 -05001270 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001271 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001272 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001273 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001274 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001275 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001276 return true;
1277 }
1278 SkPaint* fPaint;
1279 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001280 SkFont* fFont;
1281 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001282};
1283
Robert Phillips9882dae2019-03-04 11:00:10 -05001284void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001285 if (fCurrentSlide < 0) {
1286 return;
1287 }
1288
Robert Phillips9882dae2019-03-04 11:00:10 -05001289 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001290
Brian Osmanf750fbc2017-02-08 10:47:28 -05001291 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001292 SkSurface* slideSurface = surface;
1293 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001294 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001295
Brian Osmane0d4fba2017-03-15 10:24:55 -04001296 // 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 -05001297 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001298 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001299 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001300 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001301 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001302 }
1303
Brian Osman3ac99cf2017-12-01 11:23:53 -05001304 if (fSaveToSKP) {
1305 SkPictureRecorder recorder;
1306 SkCanvas* recorderCanvas = recorder.beginRecording(
1307 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001308 fSlides[fCurrentSlide]->draw(recorderCanvas);
1309 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1310 SkFILEWStream stream("sample_app.skp");
1311 picture->serialize(&stream);
1312 fSaveToSKP = false;
1313 }
1314
Brian Osmane9ed0f02018-11-26 14:50:05 -05001315 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001316 SkColorType colorType;
1317 switch (fColorMode) {
1318 case ColorMode::kLegacy:
1319 case ColorMode::kColorManaged8888:
1320 colorType = kN32_SkColorType;
1321 break;
1322 case ColorMode::kColorManagedF16:
1323 colorType = kRGBA_F16_SkColorType;
1324 break;
1325 case ColorMode::kColorManagedF16Norm:
1326 colorType = kRGBA_F16Norm_SkColorType;
1327 break;
1328 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001329
1330 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001331 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1332 slideCanvas->getProps(&props);
1333
Brian Osmane9ed0f02018-11-26 14:50:05 -05001334 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1335 return Window::kRaster_BackendType == this->fBackendType
1336 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001337 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001338 };
1339
Brian Osman03115dc2018-11-26 13:55:19 -05001340 // We need to render offscreen if we're...
1341 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1342 // ... in any raster mode, because the window surface is actually GL
1343 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001344 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001345 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001346 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001347 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001348 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001349 colorSpace != nullptr ||
1350 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001351
Brian Osmane9ed0f02018-11-26 14:50:05 -05001352 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001353 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001354 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001355 }
1356
Mike Reed59295352020-03-12 13:56:34 -04001357 SkPictureRecorder recorder;
1358 SkCanvas* recorderRestoreCanvas = nullptr;
1359 if (fDrawViaSerialize) {
1360 recorderRestoreCanvas = slideCanvas;
1361 slideCanvas = recorder.beginRecording(
1362 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1363 }
1364
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001365 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001366 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001367 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001368 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001369 if (fTiled) {
1370 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1371 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001372 for (int y = 0; y < fWindow->height(); y += tileH) {
1373 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001374 SkAutoCanvasRestore acr(slideCanvas, true);
1375 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1376 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001377 }
1378 }
1379
1380 // Draw borders between tiles
1381 if (fDrawTileBoundaries) {
1382 SkPaint border;
1383 border.setColor(0x60FF00FF);
1384 border.setStyle(SkPaint::kStroke_Style);
1385 for (int y = 0; y < fWindow->height(); y += tileH) {
1386 for (int x = 0; x < fWindow->width(); x += tileW) {
1387 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1388 }
1389 }
1390 }
1391 } else {
1392 slideCanvas->concat(this->computeMatrix());
1393 if (kPerspective_Real == fPerspectiveMode) {
1394 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1395 }
Mike Reed3ae47332019-01-04 10:11:46 -05001396 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001397 fSlides[fCurrentSlide]->draw(&filterCanvas);
1398 }
Brian Osman56a24812017-12-19 11:15:16 -05001399 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001400 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001401
Mike Reed59295352020-03-12 13:56:34 -04001402 if (recorderRestoreCanvas) {
1403 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1404 auto data = picture->serialize();
1405 slideCanvas = recorderRestoreCanvas;
1406 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1407 }
1408
Brian Osman1df161a2017-02-09 12:10:20 -05001409 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001410 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001411 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001412 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001413
1414 // If we rendered offscreen, snap an image and push the results to the window's canvas
1415 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001416 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001417
Robert Phillips9882dae2019-03-04 11:00:10 -05001418 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001419 SkPaint paint;
1420 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001421 int prePerspectiveCount = canvas->save();
1422 if (kPerspective_Fake == fPerspectiveMode) {
1423 paint.setFilterQuality(kHigh_SkFilterQuality);
1424 canvas->clear(SK_ColorWHITE);
1425 canvas->concat(this->computePerspectiveMatrix());
1426 }
Brian Osman03115dc2018-11-26 13:55:19 -05001427 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001428 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001429 }
Mike Reed376d8122019-03-14 11:39:02 -04001430
1431 if (fShowSlideDimensions) {
1432 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1433 SkPaint paint;
1434 paint.setColor(0x40FFFF00);
1435 surface->getCanvas()->drawRect(r, paint);
1436 }
liyuqian6f163d22016-06-13 12:26:45 -07001437}
1438
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001439void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001440 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001441 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001442}
Jim Van Verth6f449692017-02-14 15:16:46 -05001443
Robert Phillips9882dae2019-03-04 11:00:10 -05001444void Viewer::onPaint(SkSurface* surface) {
1445 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001446
Robert Phillips9882dae2019-03-04 11:00:10 -05001447 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001448
Brian Osmand67e5182017-12-08 16:46:09 -05001449 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001450
1451 if (GrContext* ctx = fWindow->getGrContext()) {
1452 // Clean out cache items that haven't been used in more than 10 seconds.
1453 ctx->performDeferredCleanup(std::chrono::seconds(10));
1454 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001455}
1456
Ben Wagnera1915972018-08-09 15:06:19 -04001457void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001458 if (fCurrentSlide >= 0) {
1459 fSlides[fCurrentSlide]->resize(width, height);
1460 }
Ben Wagnera1915972018-08-09 15:06:19 -04001461}
1462
Florin Malitacefc1b92018-02-19 21:43:47 -05001463SkPoint Viewer::mapEvent(float x, float y) {
1464 const auto m = this->computeMatrix();
1465 SkMatrix inv;
1466
1467 SkAssertResult(m.invert(&inv));
1468
1469 return inv.mapXY(x, y);
1470}
1471
Hal Canaryb1f411a2019-08-29 10:39:22 -04001472bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001473 if (GestureDevice::kMouse == fGestureDevice) {
1474 return false;
1475 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001476
1477 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001478 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001479 fWindow->inval();
1480 return true;
1481 }
1482
liyuqiand3cdbca2016-05-17 12:44:20 -07001483 void* castedOwner = reinterpret_cast<void*>(owner);
1484 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001485 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001486 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001487#if defined(SK_BUILD_FOR_IOS)
1488 // TODO: move IOS swipe detection higher up into the platform code
1489 SkPoint dir;
1490 if (fGesture.isFling(&dir)) {
1491 // swiping left or right
1492 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1493 if (dir.fX < 0) {
1494 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1495 fCurrentSlide + 1 : 0);
1496 } else {
1497 this->setCurrentSlide(fCurrentSlide > 0 ?
1498 fCurrentSlide - 1 : fSlides.count() - 1);
1499 }
1500 }
1501 fGesture.reset();
1502 }
1503#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001504 break;
1505 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001506 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001507 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001508 break;
1509 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001510 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001511 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001512 break;
1513 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001514 default: {
1515 // kLeft and kRight are only for swipes
1516 SkASSERT(false);
1517 break;
1518 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001519 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001520 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001521 fWindow->inval();
1522 return true;
1523}
1524
Hal Canaryb1f411a2019-08-29 10:39:22 -04001525bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001526 if (GestureDevice::kTouch == fGestureDevice) {
1527 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001528 }
Brian Osman16c81a12017-12-20 11:58:34 -05001529
Florin Malitacefc1b92018-02-19 21:43:47 -05001530 const auto slidePt = this->mapEvent(x, y);
1531 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1532 fWindow->inval();
1533 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001534 }
1535
1536 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001537 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001538 fGesture.touchEnd(nullptr);
1539 break;
1540 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001541 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001542 fGesture.touchBegin(nullptr, x, y);
1543 break;
1544 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001545 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001546 fGesture.touchMoved(nullptr, x, y);
1547 break;
1548 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001549 default: {
1550 SkASSERT(false); // shouldn't see kRight or kLeft here
1551 break;
1552 }
Brian Osman16c81a12017-12-20 11:58:34 -05001553 }
1554 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1555
Hal Canaryb1f411a2019-08-29 10:39:22 -04001556 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001557 fWindow->inval();
1558 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001559 return true;
1560}
1561
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001562bool Viewer::onFling(skui::InputState state) {
1563 if (skui::InputState::kRight == state) {
1564 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1565 return true;
1566 } else if (skui::InputState::kLeft == state) {
1567 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1568 return true;
1569 }
1570 return false;
1571}
1572
1573bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1574 switch (state) {
1575 case skui::InputState::kDown:
1576 fGesture.startZoom();
1577 return true;
1578 break;
1579 case skui::InputState::kMove:
1580 fGesture.updateZoom(scale, x, y, x, y);
1581 return true;
1582 break;
1583 case skui::InputState::kUp:
1584 fGesture.endZoom();
1585 return true;
1586 break;
1587 default:
1588 SkASSERT(false);
1589 break;
1590 }
1591
1592 return false;
1593}
1594
Brian Osmana109e392017-02-24 09:49:14 -05001595static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001596 // The gamut image covers a (0.8 x 0.9) shaped region
1597 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001598
1599 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1600 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1601 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001602 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1603 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1604 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001605
Brian Osman535c5e32019-02-09 16:32:58 -05001606 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1607 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1608 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1609 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1610 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001611}
1612
Ben Wagner3627d2e2018-06-26 14:23:20 -04001613static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001614 ImGui::DragCanvas dc(pt);
1615 dc.fillColor(IM_COL32(0, 0, 0, 128));
1616 dc.dragPoint(pt);
1617 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001618}
1619
Brian Osman9bb47cf2018-04-26 15:55:00 -04001620static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001621 ImGui::DragCanvas dc(pts);
1622 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001623
Brian Osman535c5e32019-02-09 16:32:58 -05001624 for (int i = 0; i < 4; ++i) {
1625 dc.dragPoint(pts + i);
1626 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001627
Brian Osman535c5e32019-02-09 16:32:58 -05001628 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1629 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1630 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1631 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001632
Brian Osman535c5e32019-02-09 16:32:58 -05001633 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001634}
1635
Brian Osmand67e5182017-12-08 16:46:09 -05001636void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001637 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1638 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001639 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001640 }
1641
1642 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001643 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1644 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001645 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001646 DisplayParams params = fWindow->getRequestedDisplayParams();
1647 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001648 const GrContext* ctx = fWindow->getGrContext();
1649
Brian Osmana109e392017-02-24 09:49:14 -05001650 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1651 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001652 if (ImGui::CollapsingHeader("Backend")) {
1653 int newBackend = static_cast<int>(fBackendType);
1654 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1655 ImGui::SameLine();
1656 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001657#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1658 ImGui::SameLine();
1659 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1660#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001661#if defined(SK_DAWN)
1662 ImGui::SameLine();
1663 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1664#endif
Brian Osman621491e2017-02-28 15:45:01 -05001665#if defined(SK_VULKAN)
1666 ImGui::SameLine();
1667 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1668#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001669#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001670 ImGui::SameLine();
1671 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1672#endif
Brian Osman621491e2017-02-28 15:45:01 -05001673 if (newBackend != fBackendType) {
1674 fDeferredActions.push_back([=]() {
1675 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1676 });
1677 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001678
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001679 bool* wire = &params.fGrContextOptions.fWireframeMode;
1680 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1681 paramsChanged = true;
1682 }
Brian Salomon99a33902017-03-07 15:16:34 -05001683
Brian Osman28b12522017-03-08 17:10:24 -05001684 if (ctx) {
1685 int sampleCount = fWindow->sampleCount();
1686 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001687 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001688 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1689 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1690 ImGui::RadioButton("16", &sampleCount, 16);
1691
1692 if (sampleCount != params.fMSAASampleCount) {
1693 params.fMSAASampleCount = sampleCount;
1694 paramsChanged = true;
1695 }
1696 }
1697
Ben Wagner37c54032018-04-13 14:30:23 -04001698 int pixelGeometryIdx = 0;
1699 if (fPixelGeometryOverrides) {
1700 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1701 }
1702 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1703 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1704 {
1705 uint32_t flags = params.fSurfaceProps.flags();
1706 if (pixelGeometryIdx == 0) {
1707 fPixelGeometryOverrides = false;
1708 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1709 } else {
1710 fPixelGeometryOverrides = true;
1711 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1712 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1713 }
1714 paramsChanged = true;
1715 }
1716
1717 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1718 if (ImGui::Checkbox("DFT", &useDFT)) {
1719 uint32_t flags = params.fSurfaceProps.flags();
1720 if (useDFT) {
1721 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1722 } else {
1723 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1724 }
1725 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1726 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1727 paramsChanged = true;
1728 }
1729
Brian Osman8a9de3d2017-03-01 14:59:05 -05001730 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001731 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001732 auto prButton = [&](GpuPathRenderers x) {
1733 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001734 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1735 params.fGrContextOptions.fGpuPathRenderers = x;
1736 paramsChanged = true;
1737 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001738 }
1739 };
1740
1741 if (!ctx) {
1742 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001743 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001744 const auto* caps = ctx->priv().caps();
1745 prButton(GpuPathRenderers::kDefault);
1746 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001747 if (caps->shaderCaps()->tessellationSupport()) {
1748 prButton(GpuPathRenderers::kGpuTessellation);
1749 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001750 if (caps->shaderCaps()->pathRenderingSupport()) {
1751 prButton(GpuPathRenderers::kStencilAndCover);
1752 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001753 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001754 if (1 == fWindow->sampleCount()) {
1755 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1756 prButton(GpuPathRenderers::kCoverageCounting);
1757 }
1758 prButton(GpuPathRenderers::kSmall);
1759 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001760 prButton(GpuPathRenderers::kTessellating);
1761 prButton(GpuPathRenderers::kNone);
1762 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001763 ImGui::TreePop();
1764 }
Brian Osman621491e2017-02-28 15:45:01 -05001765 }
1766
Ben Wagner964571d2019-03-08 12:35:06 -05001767 if (ImGui::CollapsingHeader("Tiling")) {
1768 ImGui::Checkbox("Enable", &fTiled);
1769 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1770 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1771 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1772 }
1773
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001774 if (ImGui::CollapsingHeader("Transform")) {
1775 float zoom = fZoomLevel;
1776 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1777 fZoomLevel = zoom;
1778 this->preTouchMatrixChanged();
1779 paramsChanged = true;
1780 }
1781 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001782 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001783 fRotation = deg;
1784 this->preTouchMatrixChanged();
1785 paramsChanged = true;
1786 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001787 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1788 if (ImGui_DragLocation(&fOffset)) {
1789 this->preTouchMatrixChanged();
1790 paramsChanged = true;
1791 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001792 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1793 this->preTouchMatrixChanged();
1794 paramsChanged = true;
1795 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001796 }
Brian Osman805a7272018-05-02 15:40:20 -04001797 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1798 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1799 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001800 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001801 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001802 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001803 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001804 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001805 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001806 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001807 }
1808
Ben Wagnera580fb32018-04-17 11:16:32 -04001809 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001810 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001811 if (fPaintOverrides.fAntiAlias) {
1812 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001813 }
1814 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001815 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001816 {
1817 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1818 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001819 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001820 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1821 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001822 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001823 fPaintOverrides.fAntiAlias = true;
1824 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001825 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001826 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001827 case SkPaintFields::AntiAliasState::Alias:
1828 break;
1829 case SkPaintFields::AntiAliasState::Normal:
1830 break;
1831 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1832 gSkUseAnalyticAA = true;
1833 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001834 break;
1835 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1836 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001837 break;
1838 }
1839 }
1840 paramsChanged = true;
1841 }
1842
Ben Wagner99a78dc2018-05-09 18:23:51 -04001843 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001844 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001845 bool (SkPaint::* isFlag)() const,
1846 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001847 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001848 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001849 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001850 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001851 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001852 if (ImGui::Combo(label, &itemIndex, items)) {
1853 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001854 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001855 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001856 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001857 (fPaint.*setFlag)(itemIndex == 2);
1858 }
1859 paramsChanged = true;
1860 }
1861 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001862
Ben Wagner99a78dc2018-05-09 18:23:51 -04001863 paintFlag("Dither",
1864 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001865 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001866 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001867
1868 int filterQualityIdx = 0;
1869 if (fPaintOverrides.fFilterQuality) {
1870 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1871 }
1872 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1873 "Default\0None\0Low\0Medium\0High\0\0"))
1874 {
1875 if (filterQualityIdx == 0) {
1876 fPaintOverrides.fFilterQuality = false;
1877 fPaint.setFilterQuality(kNone_SkFilterQuality);
1878 } else {
1879 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1880 fPaintOverrides.fFilterQuality = true;
1881 }
1882 paramsChanged = true;
1883 }
Ben Wagner9613e452019-01-23 10:34:59 -05001884 }
Hal Canary02738a82019-01-21 18:51:32 +00001885
Ben Wagner9613e452019-01-23 10:34:59 -05001886 if (ImGui::CollapsingHeader("Font")) {
1887 int hintingIdx = 0;
1888 if (fFontOverrides.fHinting) {
1889 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1890 }
1891 if (ImGui::Combo("Hinting", &hintingIdx,
1892 "Default\0None\0Slight\0Normal\0Full\0\0"))
1893 {
1894 if (hintingIdx == 0) {
1895 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001896 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001897 } else {
1898 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1899 fFontOverrides.fHinting = true;
1900 }
1901 paramsChanged = true;
1902 }
Hal Canary02738a82019-01-21 18:51:32 +00001903
Ben Wagner9613e452019-01-23 10:34:59 -05001904 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1905 bool SkFontFields::* flag,
1906 bool (SkFont::* isFlag)() const,
1907 void (SkFont::* setFlag)(bool) )
1908 {
1909 int itemIndex = 0;
1910 if (fFontOverrides.*flag) {
1911 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1912 }
1913 if (ImGui::Combo(label, &itemIndex, items)) {
1914 if (itemIndex == 0) {
1915 fFontOverrides.*flag = false;
1916 } else {
1917 fFontOverrides.*flag = true;
1918 (fFont.*setFlag)(itemIndex == 2);
1919 }
1920 paramsChanged = true;
1921 }
1922 };
Hal Canary02738a82019-01-21 18:51:32 +00001923
Ben Wagner9613e452019-01-23 10:34:59 -05001924 fontFlag("Fake Bold Glyphs",
1925 "Default\0No Fake Bold\0Fake Bold\0\0",
1926 &SkFontFields::fEmbolden,
1927 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001928
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001929 fontFlag("Baseline Snapping",
1930 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1931 &SkFontFields::fBaselineSnap,
1932 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1933
Ben Wagner9613e452019-01-23 10:34:59 -05001934 fontFlag("Linear Text",
1935 "Default\0No Linear Text\0Linear Text\0\0",
1936 &SkFontFields::fLinearMetrics,
1937 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001938
Ben Wagner9613e452019-01-23 10:34:59 -05001939 fontFlag("Subpixel Position Glyphs",
1940 "Default\0Pixel Text\0Subpixel Text\0\0",
1941 &SkFontFields::fSubpixel,
1942 &SkFont::isSubpixel, &SkFont::setSubpixel);
1943
1944 fontFlag("Embedded Bitmap Text",
1945 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1946 &SkFontFields::fEmbeddedBitmaps,
1947 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1948
1949 fontFlag("Force Auto-Hinting",
1950 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1951 &SkFontFields::fForceAutoHinting,
1952 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1953
1954 int edgingIdx = 0;
1955 if (fFontOverrides.fEdging) {
1956 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1957 }
1958 if (ImGui::Combo("Edging", &edgingIdx,
1959 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1960 {
1961 if (edgingIdx == 0) {
1962 fFontOverrides.fEdging = false;
1963 fFont.setEdging(SkFont::Edging::kAlias);
1964 } else {
1965 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1966 fFontOverrides.fEdging = true;
1967 }
1968 paramsChanged = true;
1969 }
1970
Ben Wagner15a8d572019-03-21 13:35:44 -04001971 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1972 if (fFontOverrides.fSize) {
1973 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001974 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001975 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001976 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001977 fFontOverrides.fSizeRange[0],
1978 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001979 "%.6f", 2.0f))
1980 {
Mike Reed3ae47332019-01-04 10:11:46 -05001981 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001982 paramsChanged = true;
1983 }
1984 }
1985
1986 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1987 if (fFontOverrides.fScaleX) {
1988 float scaleX = fFont.getScaleX();
1989 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1990 fFont.setScaleX(scaleX);
1991 paramsChanged = true;
1992 }
1993 }
1994
1995 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1996 if (fFontOverrides.fSkewX) {
1997 float skewX = fFont.getSkewX();
1998 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1999 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002000 paramsChanged = true;
2001 }
2002 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002003 }
2004
Mike Reed81f60ec2018-05-15 10:09:52 -04002005 {
2006 SkMetaData controls;
2007 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2008 if (ImGui::CollapsingHeader("Current Slide")) {
2009 SkMetaData::Iter iter(controls);
2010 const char* name;
2011 SkMetaData::Type type;
2012 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002013 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002014 if (type == SkMetaData::kScalar_Type) {
2015 float val[3];
2016 SkASSERT(count == 3);
2017 controls.findScalars(name, &count, val);
2018 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2019 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002020 }
Ben Wagner110c7032019-03-22 17:03:59 -04002021 } else if (type == SkMetaData::kBool_Type) {
2022 bool val;
2023 SkASSERT(count == 1);
2024 controls.findBool(name, &val);
2025 if (ImGui::Checkbox(name, &val)) {
2026 controls.setBool(name, val);
2027 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002028 }
2029 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002030 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002031 }
2032 }
2033 }
2034
Ben Wagner7a3c6742018-04-23 10:01:07 -04002035 if (fShowSlidePicker) {
2036 ImGui::SetNextTreeNodeOpen(true);
2037 }
Brian Osman79086b92017-02-10 13:36:16 -05002038 if (ImGui::CollapsingHeader("Slide")) {
2039 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002040 static ImVector<const char*> filteredSlideNames;
2041 static ImVector<int> filteredSlideIndices;
2042
Brian Osmanfce09c52017-11-14 15:32:20 -05002043 if (fShowSlidePicker) {
2044 ImGui::SetKeyboardFocusHere();
2045 fShowSlidePicker = false;
2046 }
2047
Brian Osman79086b92017-02-10 13:36:16 -05002048 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002049 filteredSlideNames.clear();
2050 filteredSlideIndices.clear();
2051 int filteredIndex = 0;
2052 for (int i = 0; i < fSlides.count(); ++i) {
2053 const char* slideName = fSlides[i]->getName().c_str();
2054 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2055 if (i == fCurrentSlide) {
2056 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002057 }
Brian Osmanf479e422017-11-08 13:11:36 -05002058 filteredSlideNames.push_back(slideName);
2059 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002060 }
Brian Osman79086b92017-02-10 13:36:16 -05002061 }
Brian Osmanf479e422017-11-08 13:11:36 -05002062
Brian Osmanf479e422017-11-08 13:11:36 -05002063 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2064 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002065 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002066 }
2067 }
Brian Osmana109e392017-02-24 09:49:14 -05002068
2069 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002070 ColorMode newMode = fColorMode;
2071 auto cmButton = [&](ColorMode mode, const char* label) {
2072 if (ImGui::RadioButton(label, mode == fColorMode)) {
2073 newMode = mode;
2074 }
2075 };
2076
2077 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002078 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2079 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002080 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002081
2082 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002083 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002084 }
2085
2086 // Pick from common gamuts:
2087 int primariesIdx = 4; // Default: Custom
2088 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2089 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2090 primariesIdx = i;
2091 break;
2092 }
2093 }
2094
Brian Osman03115dc2018-11-26 13:55:19 -05002095 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002096 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002097
Brian Osmana109e392017-02-24 09:49:14 -05002098 if (ImGui::Combo("Primaries", &primariesIdx,
2099 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2100 if (primariesIdx >= 0 && primariesIdx <= 3) {
2101 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2102 }
2103 }
2104
2105 // Allow direct editing of gamut
2106 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2107 }
Brian Osman207d4102019-01-10 09:40:58 -05002108
2109 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002110 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002111 if (ImGui::Checkbox("Pause", &isPaused)) {
2112 fAnimTimer.togglePauseResume();
2113 }
Brian Osman707d2022019-01-10 11:27:34 -05002114
2115 float speed = fAnimTimer.getSpeed();
2116 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2117 fAnimTimer.setSpeed(speed);
2118 }
Brian Osman207d4102019-01-10 09:40:58 -05002119 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002120
Brian Osmanfd7657c2019-04-25 11:34:07 -04002121 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2122#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2123 || Window::kANGLE_BackendType == fBackendType
2124#endif
2125 ;
2126
2127 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2128 // than GL, we need to force it on. Just do that on the first frame after the backend
2129 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002130 if (!backendIsGL &&
2131 params.fGrContextOptions.fShaderCacheStrategy !=
2132 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2133 params.fGrContextOptions.fShaderCacheStrategy =
2134 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002135 paramsChanged = true;
2136 fPersistentCache.reset();
2137 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002138 // To re-load shaders from the currently active programs, we flush all caches on one
2139 // frame, then set a flag to poll the cache on the next frame.
2140 static bool gLoadPending = false;
2141 if (gLoadPending) {
2142 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2143 int hitCount) {
2144 CachedGLSL& entry(fCachedGLSL.push_back());
2145 entry.fKey = key;
2146 SkMD5 hash;
2147 hash.write(key->bytes(), key->size());
2148 SkMD5::Digest digest = hash.finish();
2149 for (int i = 0; i < 16; ++i) {
2150 entry.fKeyString.appendf("%02x", digest.data[i]);
2151 }
2152
Brian Osmana66081d2019-09-03 14:59:26 -04002153 SkReader32 reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002154 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002155 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2156 entry.fInputs,
2157 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002158 };
2159 fCachedGLSL.reset();
2160 fPersistentCache.foreach(collectShaders);
2161 gLoadPending = false;
2162 }
2163
2164 // Defer actually doing the load/save logic so that we can trigger a save when we
2165 // start or finish hovering on a tree node in the list below:
2166 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002167 bool doSave = ImGui::Button("Save");
2168 if (backendIsGL) {
2169 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002170 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2171 GrContextOptions::ShaderCacheStrategy::kSkSL;
2172 if (ImGui::Checkbox("SkSL", &sksl)) {
2173 params.fGrContextOptions.fShaderCacheStrategy = sksl
2174 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2175 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002176 paramsChanged = true;
2177 doLoad = true;
2178 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2179 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002180 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002181
2182 ImGui::BeginChild("##ScrollingRegion");
2183 for (auto& entry : fCachedGLSL) {
2184 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2185 bool hovered = ImGui::IsItemHovered();
2186 if (hovered != entry.fHovered) {
2187 // Force a save to patch the highlight shader in/out
2188 entry.fHovered = hovered;
2189 doSave = true;
2190 }
2191 if (inTreeNode) {
2192 // Full width, and a reasonable amount of space for each shader.
2193 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2194 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2195 boxSize);
2196 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2197 boxSize);
2198 ImGui::TreePop();
2199 }
2200 }
2201 ImGui::EndChild();
2202
2203 if (doLoad) {
2204 fPersistentCache.reset();
2205 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2206 gLoadPending = true;
2207 }
2208 if (doSave) {
2209 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002210 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002211 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2212 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002213
Brian Osman072e6fc2019-06-12 11:35:41 -04002214 SkSL::String highlight;
2215 if (!sksl) {
2216 highlight = shaderCaps->versionDeclString();
2217 if (shaderCaps->usesPrecisionModifiers()) {
2218 highlight.append("precision mediump float;\n");
2219 }
Brian Osman5bee3902019-05-07 09:55:45 -04002220 }
2221 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002222 highlight.appendf("out %s sk_FragColor;\n"
2223 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2224 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002225
2226 fPersistentCache.reset();
2227 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2228 for (auto& entry : fCachedGLSL) {
2229 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2230 if (entry.fHovered) {
2231 entry.fShader[kFragment_GrShaderType] = highlight;
2232 }
2233
Brian Osmana085a412019-04-25 09:44:43 -04002234 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2235 entry.fShader,
2236 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002237 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002238 fPersistentCache.store(*entry.fKey, *data);
2239
2240 entry.fShader[kFragment_GrShaderType] = backup;
2241 }
2242 }
2243 }
Brian Osman79086b92017-02-10 13:36:16 -05002244 }
Brian Salomon99a33902017-03-07 15:16:34 -05002245 if (paramsChanged) {
2246 fDeferredActions.push_back([=]() {
2247 fWindow->setRequestedDisplayParams(params);
2248 fWindow->inval();
2249 this->updateTitle();
2250 });
2251 }
Brian Osman79086b92017-02-10 13:36:16 -05002252 ImGui::End();
2253 }
2254
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002255 if (gShaderErrorHandler.fErrors.count()) {
2256 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2257 ImGui::Begin("Shader Errors");
2258 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2259 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002260 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2261 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2262 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2263 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002264 }
2265 ImGui::End();
2266 gShaderErrorHandler.reset();
2267 }
2268
Brian Osmanf6877092017-02-13 09:39:57 -05002269 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002270 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2271 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002272 static int zoomFactor = 8;
2273 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002274 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002275 }
2276 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2277 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002278 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002279 }
Brian Osmanf6877092017-02-13 09:39:57 -05002280
Ben Wagner3627d2e2018-06-26 14:23:20 -04002281 if (!fZoomWindowFixed) {
2282 ImVec2 mousePos = ImGui::GetMousePos();
2283 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2284 }
2285 SkScalar x = fZoomWindowLocation.x();
2286 SkScalar y = fZoomWindowLocation.y();
2287 int xInt = SkScalarRoundToInt(x);
2288 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002289 ImVec2 avail = ImGui::GetContentRegionAvail();
2290
Brian Osmanead517d2017-11-13 15:36:36 -05002291 uint32_t pixel = 0;
2292 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002293 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002294 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002295 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002296 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002297 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002298 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2299 }
2300
Brian Osmand67e5182017-12-08 16:46:09 -05002301 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002302 // Translate so the region of the image that's under the mouse cursor is centered
2303 // in the zoom canvas:
2304 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002305 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2306 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002307 c->drawImage(this->fLastImage, 0, 0);
2308
2309 SkPaint outline;
2310 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002311 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002312 });
Brian Osmanf6877092017-02-13 09:39:57 -05002313 }
2314
2315 ImGui::End();
2316 }
Brian Osman79086b92017-02-10 13:36:16 -05002317}
2318
liyuqian2edb0f42016-07-06 14:11:32 -07002319void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002320 for (int i = 0; i < fDeferredActions.count(); ++i) {
2321 fDeferredActions[i]();
2322 }
2323 fDeferredActions.reset();
2324
Brian Osman56a24812017-12-19 11:15:16 -05002325 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002326 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002327 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002328 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002329
Brian Osman79086b92017-02-10 13:36:16 -05002330 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002331 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2332 // not be visible, though. So we need to redraw if there is at least one visible window, or
2333 // more than one active window. Newly created windows are active but not visible for one frame
2334 // while they determine their layout and sizing.
2335 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2336 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002337 fWindow->inval();
2338 }
jvanverth9f372462016-04-06 06:08:59 -07002339}
liyuqiane5a6cd92016-05-27 08:52:52 -07002340
Florin Malitab632df72018-06-18 21:23:06 -04002341template <typename OptionsFunc>
2342static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2343 OptionsFunc&& optionsFunc) {
2344 writer.beginObject();
2345 {
2346 writer.appendString(kName , name);
2347 writer.appendString(kValue, value);
2348
2349 writer.beginArray(kOptions);
2350 {
2351 optionsFunc(writer);
2352 }
2353 writer.endArray();
2354 }
2355 writer.endObject();
2356}
2357
2358
liyuqiane5a6cd92016-05-27 08:52:52 -07002359void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002360 if (!fWindow) {
2361 return;
2362 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002363 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002364 return; // Surface hasn't been created yet.
2365 }
2366
Florin Malitab632df72018-06-18 21:23:06 -04002367 SkDynamicMemoryWStream memStream;
2368 SkJSONWriter writer(&memStream);
2369 writer.beginArray();
2370
liyuqianb73c24b2016-06-03 08:47:23 -07002371 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002372 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2373 [this](SkJSONWriter& writer) {
2374 for(const auto& slide : fSlides) {
2375 writer.appendString(slide->getName().c_str());
2376 }
2377 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002378
liyuqianb73c24b2016-06-03 08:47:23 -07002379 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002380 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2381 [](SkJSONWriter& writer) {
2382 for (const auto& str : kBackendTypeStrings) {
2383 writer.appendString(str);
2384 }
2385 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002386
csmartdalton578f0642017-02-24 16:04:47 -07002387 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002388 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2389 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2390 [this](SkJSONWriter& writer) {
2391 writer.appendS32(0);
2392
2393 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2394 return;
2395 }
2396
2397 for (int msaa : {4, 8, 16}) {
2398 writer.appendS32(msaa);
2399 }
2400 });
csmartdalton578f0642017-02-24 16:04:47 -07002401
csmartdalton61cd31a2017-02-27 17:00:53 -07002402 // Path renderer state
2403 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002404 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2405 [this](SkJSONWriter& writer) {
2406 const GrContext* ctx = fWindow->getGrContext();
2407 if (!ctx) {
2408 writer.appendString("Software");
2409 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002410 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002411 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2412 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002413 if (caps->shaderCaps()->tessellationSupport()) {
2414 writer.appendString(
2415 gPathRendererNames[GpuPathRenderers::kGpuTessellation].c_str());
2416 }
Florin Malitab632df72018-06-18 21:23:06 -04002417 if (caps->shaderCaps()->pathRenderingSupport()) {
2418 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002419 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002420 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002421 }
2422 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002423 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2424 writer.appendString(
2425 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2426 }
2427 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2428 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002429 writer.appendString(gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2430 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002431 }
2432 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002433
liyuqianb73c24b2016-06-03 08:47:23 -07002434 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002435 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2436 [this](SkJSONWriter& writer) {
2437 writer.appendString(kSoftkeyHint);
2438 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2439 writer.appendString(softkey.c_str());
2440 }
2441 });
liyuqianb73c24b2016-06-03 08:47:23 -07002442
Florin Malitab632df72018-06-18 21:23:06 -04002443 writer.endArray();
2444 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002445
Florin Malitab632df72018-06-18 21:23:06 -04002446 auto data = memStream.detachAsData();
2447
2448 // TODO: would be cool to avoid this copy
2449 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2450
2451 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002452}
2453
2454void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002455 // For those who will add more features to handle the state change in this function:
2456 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2457 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2458 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002459 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002460 for (int i = 0; i < fSlides.count(); ++i) {
2461 if (fSlides[i]->getName().equals(stateValue)) {
2462 this->setCurrentSlide(i);
2463 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002464 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002465 }
Florin Malitaab99c342018-01-16 16:23:03 -05002466
2467 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002468 } else if (stateName.equals(kBackendStateName)) {
2469 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2470 if (stateValue.equals(kBackendTypeStrings[i])) {
2471 if (fBackendType != i) {
2472 fBackendType = (sk_app::Window::BackendType)i;
2473 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002474 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002475 }
2476 break;
2477 }
2478 }
csmartdalton578f0642017-02-24 16:04:47 -07002479 } else if (stateName.equals(kMSAAStateName)) {
2480 DisplayParams params = fWindow->getRequestedDisplayParams();
2481 int sampleCount = atoi(stateValue.c_str());
2482 if (sampleCount != params.fMSAASampleCount) {
2483 params.fMSAASampleCount = sampleCount;
2484 fWindow->setRequestedDisplayParams(params);
2485 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002486 this->updateTitle();
2487 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002488 }
2489 } else if (stateName.equals(kPathRendererStateName)) {
2490 DisplayParams params = fWindow->getRequestedDisplayParams();
2491 for (const auto& pair : gPathRendererNames) {
2492 if (pair.second == stateValue.c_str()) {
2493 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2494 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2495 fWindow->setRequestedDisplayParams(params);
2496 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002497 this->updateTitle();
2498 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002499 }
2500 break;
2501 }
csmartdalton578f0642017-02-24 16:04:47 -07002502 }
liyuqianb73c24b2016-06-03 08:47:23 -07002503 } else if (stateName.equals(kSoftkeyStateName)) {
2504 if (!stateValue.equals(kSoftkeyHint)) {
2505 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002506 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002507 }
liyuqian2edb0f42016-07-06 14:11:32 -07002508 } else if (stateName.equals(kRefreshStateName)) {
2509 // This state is actually NOT in the UI state.
2510 // We use this to allow Android to quickly set bool fRefresh.
2511 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002512 } else {
2513 SkDebugf("Unknown stateName: %s", stateName.c_str());
2514 }
2515}
Brian Osman79086b92017-02-10 13:36:16 -05002516
Hal Canaryb1f411a2019-08-29 10:39:22 -04002517bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002518 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002519}
2520
Hal Canaryb1f411a2019-08-29 10:39:22 -04002521bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002522 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002523 fWindow->inval();
2524 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002525 } else {
2526 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002527 }
Brian Osman79086b92017-02-10 13:36:16 -05002528}