blob: 665da4ace033aa340e0145d483c63b7f525dfca1 [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 Kleinc6142d82019-03-25 10:54:59 -0500133
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400134#ifndef SK_GL
135static_assert(false, "viewer requires GL backend for raster.")
136#endif
137
Brian Salomon194db172017-08-17 14:37:06 -0400138const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700139 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400140#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
141 "ANGLE",
142#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400143#ifdef SK_DAWN
144 "Dawn",
145#endif
jvanverth063ece72016-06-17 09:29:14 -0700146#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700147 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700148#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400149#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500150 "Metal",
151#endif
csmartdalton578f0642017-02-24 16:04:47 -0700152 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700153};
154
bsalomon6c471f72016-07-26 12:56:32 -0700155static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400156#ifdef SK_DAWN
157 if (0 == strcmp(str, "dawn")) {
158 return sk_app::Window::kDawn_BackendType;
159 } else
160#endif
bsalomon6c471f72016-07-26 12:56:32 -0700161#ifdef SK_VULKAN
162 if (0 == strcmp(str, "vk")) {
163 return sk_app::Window::kVulkan_BackendType;
164 } else
165#endif
Brian Salomon194db172017-08-17 14:37:06 -0400166#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
167 if (0 == strcmp(str, "angle")) {
168 return sk_app::Window::kANGLE_BackendType;
169 } else
170#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400171#ifdef SK_METAL
172 if (0 == strcmp(str, "mtl")) {
173 return sk_app::Window::kMetal_BackendType;
174 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500175#endif
bsalomon6c471f72016-07-26 12:56:32 -0700176 if (0 == strcmp(str, "gl")) {
177 return sk_app::Window::kNativeGL_BackendType;
178 } else if (0 == strcmp(str, "sw")) {
179 return sk_app::Window::kRaster_BackendType;
180 } else {
181 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
182 return sk_app::Window::kRaster_BackendType;
183 }
184}
185
Brian Osmana109e392017-02-24 09:49:14 -0500186static SkColorSpacePrimaries gSrgbPrimaries = {
187 0.64f, 0.33f,
188 0.30f, 0.60f,
189 0.15f, 0.06f,
190 0.3127f, 0.3290f };
191
192static SkColorSpacePrimaries gAdobePrimaries = {
193 0.64f, 0.33f,
194 0.21f, 0.71f,
195 0.15f, 0.06f,
196 0.3127f, 0.3290f };
197
198static SkColorSpacePrimaries gP3Primaries = {
199 0.680f, 0.320f,
200 0.265f, 0.690f,
201 0.150f, 0.060f,
202 0.3127f, 0.3290f };
203
204static SkColorSpacePrimaries gRec2020Primaries = {
205 0.708f, 0.292f,
206 0.170f, 0.797f,
207 0.131f, 0.046f,
208 0.3127f, 0.3290f };
209
210struct NamedPrimaries {
211 const char* fName;
212 SkColorSpacePrimaries* fPrimaries;
213} gNamedPrimaries[] = {
214 { "sRGB", &gSrgbPrimaries },
215 { "AdobeRGB", &gAdobePrimaries },
216 { "P3", &gP3Primaries },
217 { "Rec. 2020", &gRec2020Primaries },
218};
219
220static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
221 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
222}
223
Brian Osman70d2f432017-11-08 09:54:10 -0500224static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
225 // In raster mode, we still use GL for the window.
226 // This lets us render the GUI faster (and correct).
227 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
228}
229
Jim Van Verth74826c82019-03-01 14:37:30 -0500230class NullSlide : public Slide {
231 SkISize getDimensions() const override {
232 return SkISize::Make(640, 480);
233 }
234
235 void draw(SkCanvas* canvas) override {
236 canvas->clear(0xffff11ff);
237 }
238};
239
liyuqiane5a6cd92016-05-27 08:52:52 -0700240const char* kName = "name";
241const char* kValue = "value";
242const char* kOptions = "options";
243const char* kSlideStateName = "Slide";
244const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700245const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700246const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700247const char* kSoftkeyStateName = "Softkey";
248const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700249const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700250const char* kON = "ON";
251const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700252const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700253
jvanverth34524262016-05-04 13:49:13 -0700254Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500255 : fCurrentSlide(-1)
256 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500257 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400258 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500259 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500260 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500261 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500262 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400263 , fZoomWindowFixed(false)
264 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500265 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400266 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700267 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500268 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500269 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500270 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500271 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700272 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400273 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400274 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400275 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500276 , fTiled(false)
277 , fDrawTileBoundaries(false)
278 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400279 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700280{
Greg Daniel285db442016-10-14 09:12:53 -0400281 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700282
Chris Dalton37ae4b02019-12-28 14:51:11 -0700283 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Daltonb832ce62020-01-06 19:49:37 -0700284 gPathRendererNames[GpuPathRenderers::kGpuTessellation] = "GPU Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500285 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500286 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600287 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500288 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
289 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700290
jvanverth2bb3b6d2016-04-08 07:24:09 -0700291 SkDebugf("Command line arguments: ");
292 for (int i = 1; i < argc; ++i) {
293 SkDebugf("%s ", argv[i]);
294 }
295 SkDebugf("\n");
296
Mike Klein88544fb2019-03-20 10:50:33 -0500297 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500298#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400299 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500300#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700301
Mike Klein19cc0f62019-03-22 15:30:07 -0500302 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500303
Brian Osmanbc8150f2017-07-24 11:38:01 -0400304 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400305 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400306
bsalomon6c471f72016-07-26 12:56:32 -0700307 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700308 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700309
csmartdalton578f0642017-02-24 16:04:47 -0700310 DisplayParams displayParams;
311 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700312 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400313 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400314 displayParams.fGrContextOptions.fShaderCacheStrategy =
315 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400316 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
317 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700318 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500319 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700320
Brian Osman56a24812017-12-19 11:15:16 -0500321 // Configure timers
322 fStatsLayer.setActive(false);
323 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
324 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
325 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
326
jvanverth9f372462016-04-06 06:08:59 -0700327 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700328 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500329 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500330 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500331 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700332
brianosman622c8d52016-05-10 06:50:49 -0700333 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500334 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
335 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
336 fWindow->inval();
337 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500338 // Command to jump directly to the slide picker and give it focus
339 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
340 this->fShowImGuiDebugWindow = true;
341 this->fShowSlidePicker = true;
342 fWindow->inval();
343 });
344 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400345 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500346 this->fShowImGuiDebugWindow = true;
347 this->fShowSlidePicker = true;
348 fWindow->inval();
349 });
Brian Osman79086b92017-02-10 13:36:16 -0500350 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
351 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
352 fWindow->inval();
353 });
Brian Osmanf6877092017-02-13 09:39:57 -0500354 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
355 this->fShowZoomWindow = !this->fShowZoomWindow;
356 fWindow->inval();
357 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400358 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
359 this->fZoomWindowFixed = !this->fZoomWindowFixed;
360 fWindow->inval();
361 });
Greg Danield0794cc2019-03-27 16:23:26 -0400362 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
363 DisplayParams params = fWindow->getRequestedDisplayParams();
364 params.fDisableVsync = !params.fDisableVsync;
365 fWindow->setRequestedDisplayParams(params);
366 this->updateTitle();
367 fWindow->inval();
368 });
Mike Reedf702ed42019-07-22 17:00:49 -0400369 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
370 fRefresh = !fRefresh;
371 fWindow->inval();
372 });
brianosman622c8d52016-05-10 06:50:49 -0700373 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500374 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700375 fWindow->inval();
376 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400377 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500378 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400379 this->updateTitle();
380 fWindow->inval();
381 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500382 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500383 switch (fColorMode) {
384 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500385 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500386 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500387 case ColorMode::kColorManaged8888:
388 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500389 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500390 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400391 this->setColorMode(ColorMode::kColorManagedF16Norm);
392 break;
393 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500394 this->setColorMode(ColorMode::kLegacy);
395 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500396 }
brianosman622c8d52016-05-10 06:50:49 -0700397 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700398 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
399 DisplayParams params = fWindow->getRequestedDisplayParams();
400 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
401 fWindow->setRequestedDisplayParams(params);
402 fWindow->inval();
403 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400404 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500405 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700406 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400407 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500408 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700409 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400410 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700411 this->changeZoomLevel(1.f / 32.f);
412 fWindow->inval();
413 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400414 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700415 this->changeZoomLevel(-1.f / 32.f);
416 fWindow->inval();
417 });
jvanverthaf236b52016-05-20 06:01:06 -0700418 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400419 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
420 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500421 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400422#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
423 if (newBackend == sk_app::Window::kVulkan_BackendType) {
424 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
425 sk_app::Window::kBackendTypeCount);
426 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
427 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500428 }
429#endif
Brian Osman621491e2017-02-28 15:45:01 -0500430 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700431 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500432 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
433 fSaveToSKP = true;
434 fWindow->inval();
435 });
Mike Reed376d8122019-03-14 11:39:02 -0400436 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
437 fShowSlideDimensions = !fShowSlideDimensions;
438 fWindow->inval();
439 });
Ben Wagner37c54032018-04-13 14:30:23 -0400440 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
441 DisplayParams params = fWindow->getRequestedDisplayParams();
442 uint32_t flags = params.fSurfaceProps.flags();
443 if (!fPixelGeometryOverrides) {
444 fPixelGeometryOverrides = true;
445 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
446 } else {
447 switch (params.fSurfaceProps.pixelGeometry()) {
448 case kUnknown_SkPixelGeometry:
449 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
450 break;
451 case kRGB_H_SkPixelGeometry:
452 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
453 break;
454 case kBGR_H_SkPixelGeometry:
455 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
456 break;
457 case kRGB_V_SkPixelGeometry:
458 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
459 break;
460 case kBGR_V_SkPixelGeometry:
461 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
462 fPixelGeometryOverrides = false;
463 break;
464 }
465 }
466 fWindow->setRequestedDisplayParams(params);
467 this->updateTitle();
468 fWindow->inval();
469 });
Ben Wagner9613e452019-01-23 10:34:59 -0500470 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500471 if (!fFontOverrides.fHinting) {
472 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400473 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500474 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500475 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400476 case SkFontHinting::kNone:
477 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500478 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400479 case SkFontHinting::kSlight:
480 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500481 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400482 case SkFontHinting::kNormal:
483 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500484 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400485 case SkFontHinting::kFull:
486 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500487 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500488 break;
489 }
490 }
491 this->updateTitle();
492 fWindow->inval();
493 });
494 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500495 if (!fPaintOverrides.fAntiAlias) {
496 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
497 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500498 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500499 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500500 } else {
501 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500502 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500503 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500504 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400505 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500506 break;
507 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500508 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500509 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400510 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500511 break;
512 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500513 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400514 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500515 break;
516 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500517 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
518 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500519 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
520 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500521 break;
522 }
523 }
524 this->updateTitle();
525 fWindow->inval();
526 });
Ben Wagner37c54032018-04-13 14:30:23 -0400527 fCommands.addCommand('D', "Modes", "DFT", [this]() {
528 DisplayParams params = fWindow->getRequestedDisplayParams();
529 uint32_t flags = params.fSurfaceProps.flags();
530 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
531 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
532 fWindow->setRequestedDisplayParams(params);
533 this->updateTitle();
534 fWindow->inval();
535 });
Ben Wagner9613e452019-01-23 10:34:59 -0500536 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500537 if (!fFontOverrides.fEdging) {
538 fFontOverrides.fEdging = true;
539 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500540 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500541 switch (fFont.getEdging()) {
542 case SkFont::Edging::kAlias:
543 fFont.setEdging(SkFont::Edging::kAntiAlias);
544 break;
545 case SkFont::Edging::kAntiAlias:
546 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
547 break;
548 case SkFont::Edging::kSubpixelAntiAlias:
549 fFont.setEdging(SkFont::Edging::kAlias);
550 fFontOverrides.fEdging = false;
551 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500552 }
553 }
554 this->updateTitle();
555 fWindow->inval();
556 });
Ben Wagner9613e452019-01-23 10:34:59 -0500557 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500558 if (!fFontOverrides.fSubpixel) {
559 fFontOverrides.fSubpixel = true;
560 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500561 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500562 if (!fFont.isSubpixel()) {
563 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500564 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500565 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500566 }
567 }
568 this->updateTitle();
569 fWindow->inval();
570 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400571 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
572 if (!fFontOverrides.fBaselineSnap) {
573 fFontOverrides.fBaselineSnap = true;
574 fFont.setBaselineSnap(false);
575 } else {
576 if (!fFont.isBaselineSnap()) {
577 fFont.setBaselineSnap(true);
578 } else {
579 fFontOverrides.fBaselineSnap = false;
580 }
581 }
582 this->updateTitle();
583 fWindow->inval();
584 });
Brian Osman805a7272018-05-02 15:40:20 -0400585 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
586 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
587 : kPerspective_Real;
588 this->updateTitle();
589 fWindow->inval();
590 });
591 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
592 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
593 : kPerspective_Off;
594 this->updateTitle();
595 fWindow->inval();
596 });
Brian Osman207d4102019-01-10 09:40:58 -0500597 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
598 fAnimTimer.togglePauseResume();
599 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400600 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
601 fZoomUI = !fZoomUI;
602 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
603 fWindow->inval();
604 });
Mike Reed59295352020-03-12 13:56:34 -0400605 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
606 fDrawViaSerialize = !fDrawViaSerialize;
607 this->updateTitle();
608 fWindow->inval();
609 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500610
jvanverth2bb3b6d2016-04-08 07:24:09 -0700611 // set up slides
612 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500613 if (FLAGS_list) {
614 this->listNames();
615 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700616
Brian Osman9bb47cf2018-04-26 15:55:00 -0400617 fPerspectivePoints[0].set(0, 0);
618 fPerspectivePoints[1].set(1, 0);
619 fPerspectivePoints[2].set(0, 1);
620 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700621 fAnimTimer.run();
622
Hal Canaryc465d132017-12-08 10:21:31 -0500623 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500624 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400625 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500626 }
627 fImGuiGamutPaint.setColor(SK_ColorWHITE);
628 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
629
jongdeok.kim804f17e2019-02-26 14:39:23 +0900630 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500631 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700632}
633
jvanverth34524262016-05-04 13:49:13 -0700634void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400635 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
636 static const struct {
637 const char* fExtension;
638 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500639 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400640 const SlideFactory fFactory;
641 } gExternalSlidesInfo[] = {
642 { ".skp", "skp-dir", FLAGS_skps,
643 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
644 return sk_make_sp<SKPSlide>(name, path);}
645 },
646 { ".jpg", "jpg-dir", FLAGS_jpgs,
647 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
648 return sk_make_sp<ImageSlide>(name, path);}
649 },
Florin Malita87ccf332018-05-04 12:23:24 -0400650#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400651 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400652 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
653 return sk_make_sp<SkottieSlide>(name, path);}
654 },
Florin Malita87ccf332018-05-04 12:23:24 -0400655#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400656#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400657 { ".svg", "svg-dir", FLAGS_svgs,
658 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
659 return sk_make_sp<SvgSlide>(name, path);}
660 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400661#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400662 };
jvanverthc265a922016-04-08 12:51:45 -0700663
Brian Salomon343553a2018-09-05 15:41:23 -0400664 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700665
Mike Klein88544fb2019-03-20 10:50:33 -0500666 const auto addSlide =
667 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
668 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
669 return;
670 }
liyuqian6f163d22016-06-13 12:26:45 -0700671
Mike Klein88544fb2019-03-20 10:50:33 -0500672 if (auto slide = fact(name, path)) {
673 dirSlides.push_back(slide);
674 fSlides.push_back(std::move(slide));
675 }
676 };
Florin Malita76a076b2018-02-15 18:40:48 -0500677
Florin Malita38792ce2018-05-08 10:36:18 -0400678 if (!FLAGS_file.isEmpty()) {
679 // single file mode
680 const SkString file(FLAGS_file[0]);
681
682 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
683 for (const auto& sinfo : gExternalSlidesInfo) {
684 if (file.endsWith(sinfo.fExtension)) {
685 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
686 return;
687 }
688 }
689
690 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
691 } else {
692 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
693 }
694
695 return;
696 }
697
698 // Bisect slide.
699 if (!FLAGS_bisect.isEmpty()) {
700 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500701 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400702 if (FLAGS_bisect.count() >= 2) {
703 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
704 bisect->onChar(*ch);
705 }
706 }
707 fSlides.push_back(std::move(bisect));
708 }
709 }
710
711 // GMs
712 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400713 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400714 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500715 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400716 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400717 fSlides.push_back(std::move(slide));
718 }
Florin Malita38792ce2018-05-08 10:36:18 -0400719 }
720 // reverse gms
721 int numGMs = fSlides.count() - firstGM;
722 for (int i = 0; i < numGMs/2; ++i) {
723 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
724 }
725
726 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400727 for (const SampleFactory factory : SampleRegistry::Range()) {
728 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500729 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400730 fSlides.push_back(slide);
731 }
Florin Malita38792ce2018-05-08 10:36:18 -0400732 }
733
Brian Osman7c979f52019-02-12 13:27:51 -0500734 // Particle demo
735 {
736 // TODO: Convert this to a sample
737 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500738 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500739 fSlides.push_back(std::move(slide));
740 }
741 }
742
Brian Osmand927bd22019-12-18 11:23:12 -0500743 // Runtime shader editor
744 {
745 sk_sp<Slide> slide(new SkSLSlide());
746 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
747 fSlides.push_back(std::move(slide));
748 }
749 }
750
Florin Malita0ffa3222018-04-05 14:34:45 -0400751 for (const auto& info : gExternalSlidesInfo) {
752 for (const auto& flag : info.fFlags) {
753 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
754 // single file
755 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
756 } else {
757 // directory
758 SkOSFile::Iter it(flag.c_str(), info.fExtension);
759 SkString name;
760 while (it.next(&name)) {
761 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
762 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400763 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400764 if (!dirSlides.empty()) {
765 fSlides.push_back(
766 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
767 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500768 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400769 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400770 }
771 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500772
773 if (!fSlides.count()) {
774 sk_sp<Slide> slide(new NullSlide());
775 fSlides.push_back(std::move(slide));
776 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700777}
778
779
jvanverth34524262016-05-04 13:49:13 -0700780Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700781 fWindow->detach();
782 delete fWindow;
783}
784
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500785struct SkPaintTitleUpdater {
786 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
787 void append(const char* s) {
788 if (fCount == 0) {
789 fTitle->append(" {");
790 } else {
791 fTitle->append(", ");
792 }
793 fTitle->append(s);
794 ++fCount;
795 }
796 void done() {
797 if (fCount > 0) {
798 fTitle->append("}");
799 }
800 }
801 SkString* fTitle;
802 int fCount;
803};
804
brianosman05de2162016-05-06 13:28:57 -0700805void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700806 if (!fWindow) {
807 return;
808 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500809 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700810 return; // Surface hasn't been created yet.
811 }
812
jvanverth34524262016-05-04 13:49:13 -0700813 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700814 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700815
Mike Kleine5acd752019-03-22 09:57:16 -0500816 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400817 if (gSkForceAnalyticAA) {
818 title.append(" <FAAA>");
819 } else {
820 title.append(" <AAA>");
821 }
822 }
Mike Reed59295352020-03-12 13:56:34 -0400823 if (fDrawViaSerialize) {
824 title.append(" <serialize>");
825 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400826
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500827 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500828 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
829 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400830 const char* on, const char* off)
831 {
Ben Wagner9613e452019-01-23 10:34:59 -0500832 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400833 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500834 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400835 };
836
Ben Wagner9613e452019-01-23 10:34:59 -0500837 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
838 const char* on, const char* off)
839 {
840 if (fFontOverrides.*flag) {
841 paintTitle.append((fFont.*isFlag)() ? on : off);
842 }
843 };
844
845 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
846 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500847 if (fPaintOverrides.fFilterQuality) {
848 switch (fPaint.getFilterQuality()) {
849 case kNone_SkFilterQuality:
850 paintTitle.append("NoFilter");
851 break;
852 case kLow_SkFilterQuality:
853 paintTitle.append("LowFilter");
854 break;
855 case kMedium_SkFilterQuality:
856 paintTitle.append("MediumFilter");
857 break;
858 case kHigh_SkFilterQuality:
859 paintTitle.append("HighFilter");
860 break;
861 }
862 }
Ben Wagner9613e452019-01-23 10:34:59 -0500863
864 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
865 "Force Autohint", "No Force Autohint");
866 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400867 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500868 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
869 "Linear Metrics", "Non-Linear Metrics");
870 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
871 "Bitmap Text", "No Bitmap Text");
872 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
873
874 if (fFontOverrides.fEdging) {
875 switch (fFont.getEdging()) {
876 case SkFont::Edging::kAlias:
877 paintTitle.append("Alias Text");
878 break;
879 case SkFont::Edging::kAntiAlias:
880 paintTitle.append("Antialias Text");
881 break;
882 case SkFont::Edging::kSubpixelAntiAlias:
883 paintTitle.append("Subpixel Antialias Text");
884 break;
885 }
886 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400887
Mike Reed3ae47332019-01-04 10:11:46 -0500888 if (fFontOverrides.fHinting) {
889 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400890 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500891 paintTitle.append("No Hinting");
892 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400893 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500894 paintTitle.append("Slight Hinting");
895 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400896 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500897 paintTitle.append("Normal Hinting");
898 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400899 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500900 paintTitle.append("Full Hinting");
901 break;
902 }
903 }
904 paintTitle.done();
905
Brian Osman92004802017-03-06 11:47:26 -0500906 switch (fColorMode) {
907 case ColorMode::kLegacy:
908 title.append(" Legacy 8888");
909 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500910 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500911 title.append(" ColorManaged 8888");
912 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500913 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500914 title.append(" ColorManaged F16");
915 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400916 case ColorMode::kColorManagedF16Norm:
917 title.append(" ColorManaged F16 Norm");
918 break;
Brian Osman92004802017-03-06 11:47:26 -0500919 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500920
Brian Osman92004802017-03-06 11:47:26 -0500921 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500922 int curPrimaries = -1;
923 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
924 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
925 curPrimaries = i;
926 break;
927 }
928 }
Brian Osman03115dc2018-11-26 13:55:19 -0500929 title.appendf(" %s Gamma %f",
930 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500931 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700932 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500933
Ben Wagner37c54032018-04-13 14:30:23 -0400934 const DisplayParams& params = fWindow->getRequestedDisplayParams();
935 if (fPixelGeometryOverrides) {
936 switch (params.fSurfaceProps.pixelGeometry()) {
937 case kUnknown_SkPixelGeometry:
938 title.append( " Flat");
939 break;
940 case kRGB_H_SkPixelGeometry:
941 title.append( " RGB");
942 break;
943 case kBGR_H_SkPixelGeometry:
944 title.append( " BGR");
945 break;
946 case kRGB_V_SkPixelGeometry:
947 title.append( " RGBV");
948 break;
949 case kBGR_V_SkPixelGeometry:
950 title.append( " BGRV");
951 break;
952 }
953 }
954
955 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
956 title.append(" DFT");
957 }
958
csmartdalton578f0642017-02-24 16:04:47 -0700959 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700960 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500961 int msaa = fWindow->sampleCount();
962 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700963 title.appendf(" MSAA: %i", msaa);
964 }
965 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700966
967 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -0700968 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700969 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
970 }
971
Brian Osman805a7272018-05-02 15:40:20 -0400972 if (kPerspective_Real == fPerspectiveMode) {
973 title.append(" Perpsective (Real)");
974 } else if (kPerspective_Fake == fPerspectiveMode) {
975 title.append(" Perspective (Fake)");
976 }
977
brianosman05de2162016-05-06 13:28:57 -0700978 fWindow->setTitle(title.c_str());
979}
980
Florin Malitaab99c342018-01-16 16:23:03 -0500981int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500982
983 if (!FLAGS_slide.isEmpty()) {
984 int count = fSlides.count();
985 for (int i = 0; i < count; i++) {
986 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500987 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500988 }
989 }
990
991 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
992 this->listNames();
993 }
994
Florin Malitaab99c342018-01-16 16:23:03 -0500995 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500996}
997
Florin Malitaab99c342018-01-16 16:23:03 -0500998void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500999 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001000 for (const auto& slide : fSlides) {
1001 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001002 }
1003}
1004
Florin Malitaab99c342018-01-16 16:23:03 -05001005void Viewer::setCurrentSlide(int slide) {
1006 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001007
Florin Malitaab99c342018-01-16 16:23:03 -05001008 if (slide == fCurrentSlide) {
1009 return;
1010 }
1011
1012 if (fCurrentSlide >= 0) {
1013 fSlides[fCurrentSlide]->unload();
1014 }
1015
1016 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1017 SkIntToScalar(fWindow->height()));
1018 fCurrentSlide = slide;
1019 this->setupCurrentSlide();
1020}
1021
1022void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001023 if (fCurrentSlide >= 0) {
1024 // prepare dimensions for image slides
1025 fGesture.resetTouchState();
1026 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001027
Jim Van Verth0848fb02018-01-22 13:39:30 -05001028 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1029 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1030 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001031
Jim Van Verth0848fb02018-01-22 13:39:30 -05001032 // Start with a matrix that scales the slide to the available screen space
1033 if (fWindow->scaleContentToFit()) {
1034 if (windowRect.width() > 0 && windowRect.height() > 0) {
1035 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1036 }
liyuqiane46e4f02016-05-20 07:32:19 -07001037 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001038
1039 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001040 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001041
1042 this->updateTitle();
1043 this->updateUIState();
1044
1045 fStatsLayer.resetMeasurements();
1046
1047 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001048 }
jvanverthc265a922016-04-08 12:51:45 -07001049}
1050
Brian Osmanaba642c2020-02-06 12:52:25 -05001051#define MAX_ZOOM_LEVEL 8.0f
1052#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001053
jvanverth34524262016-05-04 13:49:13 -07001054void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001055 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001056 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001057 this->preTouchMatrixChanged();
1058}
Yuqian Li755778c2018-03-28 16:23:31 -04001059
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001060void Viewer::preTouchMatrixChanged() {
1061 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001062 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1063 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1064 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1065 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1066}
1067
Brian Osman805a7272018-05-02 15:40:20 -04001068SkMatrix Viewer::computePerspectiveMatrix() {
1069 SkScalar w = fWindow->width(), h = fWindow->height();
1070 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1071 SkPoint perspPts[4] = {
1072 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1073 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1074 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1075 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1076 };
1077 SkMatrix m;
1078 m.setPolyToPoly(orthoPts, perspPts, 4);
1079 return m;
1080}
1081
Yuqian Li755778c2018-03-28 16:23:31 -04001082SkMatrix Viewer::computePreTouchMatrix() {
1083 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001084
1085 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001086 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001087 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001088
1089 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1090 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001091
Brian Osman805a7272018-05-02 15:40:20 -04001092 if (kPerspective_Real == fPerspectiveMode) {
1093 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001094 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001095 }
1096
Yuqian Li755778c2018-03-28 16:23:31 -04001097 return m;
jvanverthc265a922016-04-08 12:51:45 -07001098}
1099
liyuqiand3cdbca2016-05-17 12:44:20 -07001100SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001101 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001102 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001103 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001104 return m;
jvanverthc265a922016-04-08 12:51:45 -07001105}
1106
Brian Osman621491e2017-02-28 15:45:01 -05001107void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001108 fPersistentCache.reset();
1109 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001110 fBackendType = backendType;
1111
1112 fWindow->detach();
1113
Brian Osman70d2f432017-11-08 09:54:10 -05001114#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001115 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1116 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001117 DisplayParams params = fWindow->getRequestedDisplayParams();
1118 delete fWindow;
1119 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001120
Brian Osman70d2f432017-11-08 09:54:10 -05001121 // re-register callbacks
1122 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001123 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001124 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001125 fWindow->pushLayer(&fImGuiLayer);
1126
Brian Osman70d2f432017-11-08 09:54:10 -05001127 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1128 // will still include our correct sample count. But the re-created fWindow will lose that
1129 // information. On Windows, we need to re-create the window when changing sample count,
1130 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1131 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1132 // as if we had never changed windows at all).
1133 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001134#endif
1135
Brian Osman70d2f432017-11-08 09:54:10 -05001136 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001137}
1138
Brian Osman92004802017-03-06 11:47:26 -05001139void Viewer::setColorMode(ColorMode colorMode) {
1140 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001141 this->updateTitle();
1142 fWindow->inval();
1143}
1144
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001145class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1146public:
Mike Reed3ae47332019-01-04 10:11:46 -05001147 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1148 SkFont* font, Viewer::SkFontFields* ffields)
1149 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001150 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001151 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1152 sk_sp<SkTextBlob>* cache) {
1153 bool blobWillChange = false;
1154 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001155 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1156 bool shouldDraw = this->filterFont(&filteredFont);
1157 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001158 blobWillChange = true;
1159 break;
1160 }
1161 }
1162 if (!blobWillChange) {
1163 return blob;
1164 }
1165
1166 SkTextBlobBuilder builder;
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);
Ben Wagner41e40472018-09-24 13:01:54 -04001170 if (!shouldDraw) {
1171 continue;
1172 }
1173
Mike Reed3ae47332019-01-04 10:11:46 -05001174 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001175
Ben Wagner41e40472018-09-24 13:01:54 -04001176 const SkTextBlobBuilder::RunBuffer& runBuffer
1177 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001178 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001179 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001180 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001181 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001182 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001183 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001184 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001185 it.glyphCount(), it.textSize(), SkString())
1186 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1187 uint32_t glyphCount = it.glyphCount();
1188 if (it.glyphs()) {
1189 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1190 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1191 }
1192 if (it.pos()) {
1193 size_t posSize = sizeof(decltype(*it.pos()));
1194 uint8_t positioning = it.positioning();
1195 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1196 }
1197 if (it.text()) {
1198 size_t textSize = sizeof(decltype(*it.text()));
1199 uint32_t textCount = it.textSize();
1200 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1201 }
1202 if (it.clusters()) {
1203 size_t clusterSize = sizeof(decltype(*it.clusters()));
1204 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1205 }
1206 }
1207 *cache = builder.make();
1208 return cache->get();
1209 }
1210 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1211 const SkPaint& paint) override {
1212 sk_sp<SkTextBlob> cache;
1213 this->SkPaintFilterCanvas::onDrawTextBlob(
1214 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1215 }
Mike Reed3ae47332019-01-04 10:11:46 -05001216 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001217 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001218 font->writable()->setSize(fFont->getSize());
1219 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001220 if (fFontOverrides->fScaleX) {
1221 font->writable()->setScaleX(fFont->getScaleX());
1222 }
1223 if (fFontOverrides->fSkewX) {
1224 font->writable()->setSkewX(fFont->getSkewX());
1225 }
Mike Reed3ae47332019-01-04 10:11:46 -05001226 if (fFontOverrides->fHinting) {
1227 font->writable()->setHinting(fFont->getHinting());
1228 }
Ben Wagner9613e452019-01-23 10:34:59 -05001229 if (fFontOverrides->fEdging) {
1230 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001231 }
Ben Wagner9613e452019-01-23 10:34:59 -05001232 if (fFontOverrides->fEmbolden) {
1233 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001234 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001235 if (fFontOverrides->fBaselineSnap) {
1236 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1237 }
Ben Wagner9613e452019-01-23 10:34:59 -05001238 if (fFontOverrides->fLinearMetrics) {
1239 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001240 }
Ben Wagner9613e452019-01-23 10:34:59 -05001241 if (fFontOverrides->fSubpixel) {
1242 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001243 }
Ben Wagner9613e452019-01-23 10:34:59 -05001244 if (fFontOverrides->fEmbeddedBitmaps) {
1245 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001246 }
Ben Wagner9613e452019-01-23 10:34:59 -05001247 if (fFontOverrides->fForceAutoHinting) {
1248 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001249 }
Ben Wagner9613e452019-01-23 10:34:59 -05001250
Mike Reed3ae47332019-01-04 10:11:46 -05001251 return true;
1252 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001253 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001254 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001255 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001256 }
Ben Wagner9613e452019-01-23 10:34:59 -05001257 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001258 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001259 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001260 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001261 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001262 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001263 return true;
1264 }
1265 SkPaint* fPaint;
1266 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001267 SkFont* fFont;
1268 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001269};
1270
Robert Phillips9882dae2019-03-04 11:00:10 -05001271void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001272 if (fCurrentSlide < 0) {
1273 return;
1274 }
1275
Robert Phillips9882dae2019-03-04 11:00:10 -05001276 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001277
Brian Osmanf750fbc2017-02-08 10:47:28 -05001278 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001279 SkSurface* slideSurface = surface;
1280 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001281 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001282
Brian Osmane0d4fba2017-03-15 10:24:55 -04001283 // 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 -05001284 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001285 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001286 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001287 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001288 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001289 }
1290
Brian Osman3ac99cf2017-12-01 11:23:53 -05001291 if (fSaveToSKP) {
1292 SkPictureRecorder recorder;
1293 SkCanvas* recorderCanvas = recorder.beginRecording(
1294 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001295 fSlides[fCurrentSlide]->draw(recorderCanvas);
1296 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1297 SkFILEWStream stream("sample_app.skp");
1298 picture->serialize(&stream);
1299 fSaveToSKP = false;
1300 }
1301
Brian Osmane9ed0f02018-11-26 14:50:05 -05001302 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001303 SkColorType colorType;
1304 switch (fColorMode) {
1305 case ColorMode::kLegacy:
1306 case ColorMode::kColorManaged8888:
1307 colorType = kN32_SkColorType;
1308 break;
1309 case ColorMode::kColorManagedF16:
1310 colorType = kRGBA_F16_SkColorType;
1311 break;
1312 case ColorMode::kColorManagedF16Norm:
1313 colorType = kRGBA_F16Norm_SkColorType;
1314 break;
1315 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001316
1317 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001318 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1319 slideCanvas->getProps(&props);
1320
Brian Osmane9ed0f02018-11-26 14:50:05 -05001321 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1322 return Window::kRaster_BackendType == this->fBackendType
1323 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001324 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001325 };
1326
Brian Osman03115dc2018-11-26 13:55:19 -05001327 // We need to render offscreen if we're...
1328 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1329 // ... in any raster mode, because the window surface is actually GL
1330 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001331 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001332 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001333 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001334 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001335 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001336 colorSpace != nullptr ||
1337 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001338
Brian Osmane9ed0f02018-11-26 14:50:05 -05001339 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001340 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001341 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001342 }
1343
Mike Reed59295352020-03-12 13:56:34 -04001344 SkPictureRecorder recorder;
1345 SkCanvas* recorderRestoreCanvas = nullptr;
1346 if (fDrawViaSerialize) {
1347 recorderRestoreCanvas = slideCanvas;
1348 slideCanvas = recorder.beginRecording(
1349 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1350 }
1351
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001352 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001353 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001354 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001355 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001356 if (fTiled) {
1357 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1358 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001359 for (int y = 0; y < fWindow->height(); y += tileH) {
1360 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001361 SkAutoCanvasRestore acr(slideCanvas, true);
1362 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1363 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001364 }
1365 }
1366
1367 // Draw borders between tiles
1368 if (fDrawTileBoundaries) {
1369 SkPaint border;
1370 border.setColor(0x60FF00FF);
1371 border.setStyle(SkPaint::kStroke_Style);
1372 for (int y = 0; y < fWindow->height(); y += tileH) {
1373 for (int x = 0; x < fWindow->width(); x += tileW) {
1374 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1375 }
1376 }
1377 }
1378 } else {
1379 slideCanvas->concat(this->computeMatrix());
1380 if (kPerspective_Real == fPerspectiveMode) {
1381 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1382 }
Mike Reed3ae47332019-01-04 10:11:46 -05001383 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001384 fSlides[fCurrentSlide]->draw(&filterCanvas);
1385 }
Brian Osman56a24812017-12-19 11:15:16 -05001386 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001387 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001388
Mike Reed59295352020-03-12 13:56:34 -04001389 if (recorderRestoreCanvas) {
1390 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1391 auto data = picture->serialize();
1392 slideCanvas = recorderRestoreCanvas;
1393 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1394 }
1395
Brian Osman1df161a2017-02-09 12:10:20 -05001396 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001397 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001398 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001399 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001400
1401 // If we rendered offscreen, snap an image and push the results to the window's canvas
1402 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001403 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001404
Robert Phillips9882dae2019-03-04 11:00:10 -05001405 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001406 SkPaint paint;
1407 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001408 int prePerspectiveCount = canvas->save();
1409 if (kPerspective_Fake == fPerspectiveMode) {
1410 paint.setFilterQuality(kHigh_SkFilterQuality);
1411 canvas->clear(SK_ColorWHITE);
1412 canvas->concat(this->computePerspectiveMatrix());
1413 }
Brian Osman03115dc2018-11-26 13:55:19 -05001414 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001415 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001416 }
Mike Reed376d8122019-03-14 11:39:02 -04001417
1418 if (fShowSlideDimensions) {
1419 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1420 SkPaint paint;
1421 paint.setColor(0x40FFFF00);
1422 surface->getCanvas()->drawRect(r, paint);
1423 }
liyuqian6f163d22016-06-13 12:26:45 -07001424}
1425
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001426void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001427 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001428 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001429}
Jim Van Verth6f449692017-02-14 15:16:46 -05001430
Robert Phillips9882dae2019-03-04 11:00:10 -05001431void Viewer::onPaint(SkSurface* surface) {
1432 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001433
Robert Phillips9882dae2019-03-04 11:00:10 -05001434 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001435
Brian Osmand67e5182017-12-08 16:46:09 -05001436 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001437
1438 if (GrContext* ctx = fWindow->getGrContext()) {
1439 // Clean out cache items that haven't been used in more than 10 seconds.
1440 ctx->performDeferredCleanup(std::chrono::seconds(10));
1441 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001442}
1443
Ben Wagnera1915972018-08-09 15:06:19 -04001444void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001445 if (fCurrentSlide >= 0) {
1446 fSlides[fCurrentSlide]->resize(width, height);
1447 }
Ben Wagnera1915972018-08-09 15:06:19 -04001448}
1449
Florin Malitacefc1b92018-02-19 21:43:47 -05001450SkPoint Viewer::mapEvent(float x, float y) {
1451 const auto m = this->computeMatrix();
1452 SkMatrix inv;
1453
1454 SkAssertResult(m.invert(&inv));
1455
1456 return inv.mapXY(x, y);
1457}
1458
Hal Canaryb1f411a2019-08-29 10:39:22 -04001459bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001460 if (GestureDevice::kMouse == fGestureDevice) {
1461 return false;
1462 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001463
1464 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001465 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001466 fWindow->inval();
1467 return true;
1468 }
1469
liyuqiand3cdbca2016-05-17 12:44:20 -07001470 void* castedOwner = reinterpret_cast<void*>(owner);
1471 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001472 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001473 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001474#if defined(SK_BUILD_FOR_IOS)
1475 // TODO: move IOS swipe detection higher up into the platform code
1476 SkPoint dir;
1477 if (fGesture.isFling(&dir)) {
1478 // swiping left or right
1479 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1480 if (dir.fX < 0) {
1481 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1482 fCurrentSlide + 1 : 0);
1483 } else {
1484 this->setCurrentSlide(fCurrentSlide > 0 ?
1485 fCurrentSlide - 1 : fSlides.count() - 1);
1486 }
1487 }
1488 fGesture.reset();
1489 }
1490#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001491 break;
1492 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001493 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001494 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001495 break;
1496 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001497 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001498 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001499 break;
1500 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001501 default: {
1502 // kLeft and kRight are only for swipes
1503 SkASSERT(false);
1504 break;
1505 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001506 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001507 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001508 fWindow->inval();
1509 return true;
1510}
1511
Hal Canaryb1f411a2019-08-29 10:39:22 -04001512bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001513 if (GestureDevice::kTouch == fGestureDevice) {
1514 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001515 }
Brian Osman16c81a12017-12-20 11:58:34 -05001516
Florin Malitacefc1b92018-02-19 21:43:47 -05001517 const auto slidePt = this->mapEvent(x, y);
1518 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1519 fWindow->inval();
1520 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001521 }
1522
1523 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001524 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001525 fGesture.touchEnd(nullptr);
1526 break;
1527 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001528 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001529 fGesture.touchBegin(nullptr, x, y);
1530 break;
1531 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001532 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001533 fGesture.touchMoved(nullptr, x, y);
1534 break;
1535 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001536 default: {
1537 SkASSERT(false); // shouldn't see kRight or kLeft here
1538 break;
1539 }
Brian Osman16c81a12017-12-20 11:58:34 -05001540 }
1541 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1542
Hal Canaryb1f411a2019-08-29 10:39:22 -04001543 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001544 fWindow->inval();
1545 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001546 return true;
1547}
1548
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001549bool Viewer::onFling(skui::InputState state) {
1550 if (skui::InputState::kRight == state) {
1551 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1552 return true;
1553 } else if (skui::InputState::kLeft == state) {
1554 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1555 return true;
1556 }
1557 return false;
1558}
1559
1560bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1561 switch (state) {
1562 case skui::InputState::kDown:
1563 fGesture.startZoom();
1564 return true;
1565 break;
1566 case skui::InputState::kMove:
1567 fGesture.updateZoom(scale, x, y, x, y);
1568 return true;
1569 break;
1570 case skui::InputState::kUp:
1571 fGesture.endZoom();
1572 return true;
1573 break;
1574 default:
1575 SkASSERT(false);
1576 break;
1577 }
1578
1579 return false;
1580}
1581
Brian Osmana109e392017-02-24 09:49:14 -05001582static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001583 // The gamut image covers a (0.8 x 0.9) shaped region
1584 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001585
1586 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1587 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1588 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001589 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1590 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1591 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001592
Brian Osman535c5e32019-02-09 16:32:58 -05001593 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1594 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1595 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1596 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1597 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001598}
1599
Ben Wagner3627d2e2018-06-26 14:23:20 -04001600static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001601 ImGui::DragCanvas dc(pt);
1602 dc.fillColor(IM_COL32(0, 0, 0, 128));
1603 dc.dragPoint(pt);
1604 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001605}
1606
Brian Osman9bb47cf2018-04-26 15:55:00 -04001607static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001608 ImGui::DragCanvas dc(pts);
1609 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001610
Brian Osman535c5e32019-02-09 16:32:58 -05001611 for (int i = 0; i < 4; ++i) {
1612 dc.dragPoint(pts + i);
1613 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001614
Brian Osman535c5e32019-02-09 16:32:58 -05001615 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1616 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1617 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1618 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001619
Brian Osman535c5e32019-02-09 16:32:58 -05001620 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001621}
1622
Brian Osmand67e5182017-12-08 16:46:09 -05001623void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001624 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1625 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001626 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001627 }
1628
1629 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001630 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1631 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001632 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001633 DisplayParams params = fWindow->getRequestedDisplayParams();
1634 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001635 const GrContext* ctx = fWindow->getGrContext();
1636
Brian Osmana109e392017-02-24 09:49:14 -05001637 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1638 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001639 if (ImGui::CollapsingHeader("Backend")) {
1640 int newBackend = static_cast<int>(fBackendType);
1641 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1642 ImGui::SameLine();
1643 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001644#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1645 ImGui::SameLine();
1646 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1647#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001648#if defined(SK_DAWN)
1649 ImGui::SameLine();
1650 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1651#endif
Brian Osman621491e2017-02-28 15:45:01 -05001652#if defined(SK_VULKAN)
1653 ImGui::SameLine();
1654 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1655#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001656#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001657 ImGui::SameLine();
1658 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1659#endif
Brian Osman621491e2017-02-28 15:45:01 -05001660 if (newBackend != fBackendType) {
1661 fDeferredActions.push_back([=]() {
1662 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1663 });
1664 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001665
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001666 bool* wire = &params.fGrContextOptions.fWireframeMode;
1667 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1668 paramsChanged = true;
1669 }
Brian Salomon99a33902017-03-07 15:16:34 -05001670
Brian Osman28b12522017-03-08 17:10:24 -05001671 if (ctx) {
1672 int sampleCount = fWindow->sampleCount();
1673 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001674 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001675 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1676 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1677 ImGui::RadioButton("16", &sampleCount, 16);
1678
1679 if (sampleCount != params.fMSAASampleCount) {
1680 params.fMSAASampleCount = sampleCount;
1681 paramsChanged = true;
1682 }
1683 }
1684
Ben Wagner37c54032018-04-13 14:30:23 -04001685 int pixelGeometryIdx = 0;
1686 if (fPixelGeometryOverrides) {
1687 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1688 }
1689 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1690 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1691 {
1692 uint32_t flags = params.fSurfaceProps.flags();
1693 if (pixelGeometryIdx == 0) {
1694 fPixelGeometryOverrides = false;
1695 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1696 } else {
1697 fPixelGeometryOverrides = true;
1698 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1699 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1700 }
1701 paramsChanged = true;
1702 }
1703
1704 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1705 if (ImGui::Checkbox("DFT", &useDFT)) {
1706 uint32_t flags = params.fSurfaceProps.flags();
1707 if (useDFT) {
1708 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1709 } else {
1710 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1711 }
1712 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1713 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1714 paramsChanged = true;
1715 }
1716
Brian Osman8a9de3d2017-03-01 14:59:05 -05001717 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001718 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001719 auto prButton = [&](GpuPathRenderers x) {
1720 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001721 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1722 params.fGrContextOptions.fGpuPathRenderers = x;
1723 paramsChanged = true;
1724 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001725 }
1726 };
1727
1728 if (!ctx) {
1729 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001730 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001731 const auto* caps = ctx->priv().caps();
1732 prButton(GpuPathRenderers::kDefault);
1733 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001734 if (caps->shaderCaps()->tessellationSupport()) {
1735 prButton(GpuPathRenderers::kGpuTessellation);
1736 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001737 if (caps->shaderCaps()->pathRenderingSupport()) {
1738 prButton(GpuPathRenderers::kStencilAndCover);
1739 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001740 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001741 if (1 == fWindow->sampleCount()) {
1742 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1743 prButton(GpuPathRenderers::kCoverageCounting);
1744 }
1745 prButton(GpuPathRenderers::kSmall);
1746 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001747 prButton(GpuPathRenderers::kTessellating);
1748 prButton(GpuPathRenderers::kNone);
1749 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001750 ImGui::TreePop();
1751 }
Brian Osman621491e2017-02-28 15:45:01 -05001752 }
1753
Ben Wagner964571d2019-03-08 12:35:06 -05001754 if (ImGui::CollapsingHeader("Tiling")) {
1755 ImGui::Checkbox("Enable", &fTiled);
1756 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1757 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1758 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1759 }
1760
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001761 if (ImGui::CollapsingHeader("Transform")) {
1762 float zoom = fZoomLevel;
1763 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1764 fZoomLevel = zoom;
1765 this->preTouchMatrixChanged();
1766 paramsChanged = true;
1767 }
1768 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001769 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001770 fRotation = deg;
1771 this->preTouchMatrixChanged();
1772 paramsChanged = true;
1773 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001774 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1775 if (ImGui_DragLocation(&fOffset)) {
1776 this->preTouchMatrixChanged();
1777 paramsChanged = true;
1778 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001779 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1780 this->preTouchMatrixChanged();
1781 paramsChanged = true;
1782 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001783 }
Brian Osman805a7272018-05-02 15:40:20 -04001784 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1785 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1786 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001787 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001788 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001789 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001790 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001791 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001792 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001793 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001794 }
1795
Ben Wagnera580fb32018-04-17 11:16:32 -04001796 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001797 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001798 if (fPaintOverrides.fAntiAlias) {
1799 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001800 }
1801 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001802 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001803 {
1804 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1805 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001806 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001807 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1808 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001809 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001810 fPaintOverrides.fAntiAlias = true;
1811 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001812 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001813 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001814 case SkPaintFields::AntiAliasState::Alias:
1815 break;
1816 case SkPaintFields::AntiAliasState::Normal:
1817 break;
1818 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1819 gSkUseAnalyticAA = true;
1820 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001821 break;
1822 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1823 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001824 break;
1825 }
1826 }
1827 paramsChanged = true;
1828 }
1829
Ben Wagner99a78dc2018-05-09 18:23:51 -04001830 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001831 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001832 bool (SkPaint::* isFlag)() const,
1833 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001834 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001835 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001836 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001837 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001838 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001839 if (ImGui::Combo(label, &itemIndex, items)) {
1840 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001841 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001842 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001843 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001844 (fPaint.*setFlag)(itemIndex == 2);
1845 }
1846 paramsChanged = true;
1847 }
1848 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001849
Ben Wagner99a78dc2018-05-09 18:23:51 -04001850 paintFlag("Dither",
1851 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001852 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001853 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001854
1855 int filterQualityIdx = 0;
1856 if (fPaintOverrides.fFilterQuality) {
1857 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1858 }
1859 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1860 "Default\0None\0Low\0Medium\0High\0\0"))
1861 {
1862 if (filterQualityIdx == 0) {
1863 fPaintOverrides.fFilterQuality = false;
1864 fPaint.setFilterQuality(kNone_SkFilterQuality);
1865 } else {
1866 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1867 fPaintOverrides.fFilterQuality = true;
1868 }
1869 paramsChanged = true;
1870 }
Ben Wagner9613e452019-01-23 10:34:59 -05001871 }
Hal Canary02738a82019-01-21 18:51:32 +00001872
Ben Wagner9613e452019-01-23 10:34:59 -05001873 if (ImGui::CollapsingHeader("Font")) {
1874 int hintingIdx = 0;
1875 if (fFontOverrides.fHinting) {
1876 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1877 }
1878 if (ImGui::Combo("Hinting", &hintingIdx,
1879 "Default\0None\0Slight\0Normal\0Full\0\0"))
1880 {
1881 if (hintingIdx == 0) {
1882 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001883 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001884 } else {
1885 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1886 fFontOverrides.fHinting = true;
1887 }
1888 paramsChanged = true;
1889 }
Hal Canary02738a82019-01-21 18:51:32 +00001890
Ben Wagner9613e452019-01-23 10:34:59 -05001891 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1892 bool SkFontFields::* flag,
1893 bool (SkFont::* isFlag)() const,
1894 void (SkFont::* setFlag)(bool) )
1895 {
1896 int itemIndex = 0;
1897 if (fFontOverrides.*flag) {
1898 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1899 }
1900 if (ImGui::Combo(label, &itemIndex, items)) {
1901 if (itemIndex == 0) {
1902 fFontOverrides.*flag = false;
1903 } else {
1904 fFontOverrides.*flag = true;
1905 (fFont.*setFlag)(itemIndex == 2);
1906 }
1907 paramsChanged = true;
1908 }
1909 };
Hal Canary02738a82019-01-21 18:51:32 +00001910
Ben Wagner9613e452019-01-23 10:34:59 -05001911 fontFlag("Fake Bold Glyphs",
1912 "Default\0No Fake Bold\0Fake Bold\0\0",
1913 &SkFontFields::fEmbolden,
1914 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001915
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001916 fontFlag("Baseline Snapping",
1917 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1918 &SkFontFields::fBaselineSnap,
1919 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1920
Ben Wagner9613e452019-01-23 10:34:59 -05001921 fontFlag("Linear Text",
1922 "Default\0No Linear Text\0Linear Text\0\0",
1923 &SkFontFields::fLinearMetrics,
1924 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001925
Ben Wagner9613e452019-01-23 10:34:59 -05001926 fontFlag("Subpixel Position Glyphs",
1927 "Default\0Pixel Text\0Subpixel Text\0\0",
1928 &SkFontFields::fSubpixel,
1929 &SkFont::isSubpixel, &SkFont::setSubpixel);
1930
1931 fontFlag("Embedded Bitmap Text",
1932 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1933 &SkFontFields::fEmbeddedBitmaps,
1934 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1935
1936 fontFlag("Force Auto-Hinting",
1937 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1938 &SkFontFields::fForceAutoHinting,
1939 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1940
1941 int edgingIdx = 0;
1942 if (fFontOverrides.fEdging) {
1943 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1944 }
1945 if (ImGui::Combo("Edging", &edgingIdx,
1946 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1947 {
1948 if (edgingIdx == 0) {
1949 fFontOverrides.fEdging = false;
1950 fFont.setEdging(SkFont::Edging::kAlias);
1951 } else {
1952 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1953 fFontOverrides.fEdging = true;
1954 }
1955 paramsChanged = true;
1956 }
1957
Ben Wagner15a8d572019-03-21 13:35:44 -04001958 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1959 if (fFontOverrides.fSize) {
1960 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001961 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001962 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001963 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001964 fFontOverrides.fSizeRange[0],
1965 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001966 "%.6f", 2.0f))
1967 {
Mike Reed3ae47332019-01-04 10:11:46 -05001968 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001969 paramsChanged = true;
1970 }
1971 }
1972
1973 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1974 if (fFontOverrides.fScaleX) {
1975 float scaleX = fFont.getScaleX();
1976 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1977 fFont.setScaleX(scaleX);
1978 paramsChanged = true;
1979 }
1980 }
1981
1982 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1983 if (fFontOverrides.fSkewX) {
1984 float skewX = fFont.getSkewX();
1985 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1986 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001987 paramsChanged = true;
1988 }
1989 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001990 }
1991
Mike Reed81f60ec2018-05-15 10:09:52 -04001992 {
1993 SkMetaData controls;
1994 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1995 if (ImGui::CollapsingHeader("Current Slide")) {
1996 SkMetaData::Iter iter(controls);
1997 const char* name;
1998 SkMetaData::Type type;
1999 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002000 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002001 if (type == SkMetaData::kScalar_Type) {
2002 float val[3];
2003 SkASSERT(count == 3);
2004 controls.findScalars(name, &count, val);
2005 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2006 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002007 }
Ben Wagner110c7032019-03-22 17:03:59 -04002008 } else if (type == SkMetaData::kBool_Type) {
2009 bool val;
2010 SkASSERT(count == 1);
2011 controls.findBool(name, &val);
2012 if (ImGui::Checkbox(name, &val)) {
2013 controls.setBool(name, val);
2014 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002015 }
2016 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002017 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002018 }
2019 }
2020 }
2021
Ben Wagner7a3c6742018-04-23 10:01:07 -04002022 if (fShowSlidePicker) {
2023 ImGui::SetNextTreeNodeOpen(true);
2024 }
Brian Osman79086b92017-02-10 13:36:16 -05002025 if (ImGui::CollapsingHeader("Slide")) {
2026 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002027 static ImVector<const char*> filteredSlideNames;
2028 static ImVector<int> filteredSlideIndices;
2029
Brian Osmanfce09c52017-11-14 15:32:20 -05002030 if (fShowSlidePicker) {
2031 ImGui::SetKeyboardFocusHere();
2032 fShowSlidePicker = false;
2033 }
2034
Brian Osman79086b92017-02-10 13:36:16 -05002035 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002036 filteredSlideNames.clear();
2037 filteredSlideIndices.clear();
2038 int filteredIndex = 0;
2039 for (int i = 0; i < fSlides.count(); ++i) {
2040 const char* slideName = fSlides[i]->getName().c_str();
2041 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2042 if (i == fCurrentSlide) {
2043 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002044 }
Brian Osmanf479e422017-11-08 13:11:36 -05002045 filteredSlideNames.push_back(slideName);
2046 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002047 }
Brian Osman79086b92017-02-10 13:36:16 -05002048 }
Brian Osmanf479e422017-11-08 13:11:36 -05002049
Brian Osmanf479e422017-11-08 13:11:36 -05002050 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2051 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002052 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002053 }
2054 }
Brian Osmana109e392017-02-24 09:49:14 -05002055
2056 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002057 ColorMode newMode = fColorMode;
2058 auto cmButton = [&](ColorMode mode, const char* label) {
2059 if (ImGui::RadioButton(label, mode == fColorMode)) {
2060 newMode = mode;
2061 }
2062 };
2063
2064 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002065 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2066 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002067 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002068
2069 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002070 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002071 }
2072
2073 // Pick from common gamuts:
2074 int primariesIdx = 4; // Default: Custom
2075 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2076 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2077 primariesIdx = i;
2078 break;
2079 }
2080 }
2081
Brian Osman03115dc2018-11-26 13:55:19 -05002082 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002083 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002084
Brian Osmana109e392017-02-24 09:49:14 -05002085 if (ImGui::Combo("Primaries", &primariesIdx,
2086 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2087 if (primariesIdx >= 0 && primariesIdx <= 3) {
2088 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2089 }
2090 }
2091
2092 // Allow direct editing of gamut
2093 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2094 }
Brian Osman207d4102019-01-10 09:40:58 -05002095
2096 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002097 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002098 if (ImGui::Checkbox("Pause", &isPaused)) {
2099 fAnimTimer.togglePauseResume();
2100 }
Brian Osman707d2022019-01-10 11:27:34 -05002101
2102 float speed = fAnimTimer.getSpeed();
2103 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2104 fAnimTimer.setSpeed(speed);
2105 }
Brian Osman207d4102019-01-10 09:40:58 -05002106 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002107
Brian Osmanfd7657c2019-04-25 11:34:07 -04002108 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2109#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2110 || Window::kANGLE_BackendType == fBackendType
2111#endif
2112 ;
2113
2114 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2115 // than GL, we need to force it on. Just do that on the first frame after the backend
2116 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002117 if (!backendIsGL &&
2118 params.fGrContextOptions.fShaderCacheStrategy !=
2119 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2120 params.fGrContextOptions.fShaderCacheStrategy =
2121 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002122 paramsChanged = true;
2123 fPersistentCache.reset();
2124 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002125 // To re-load shaders from the currently active programs, we flush all caches on one
2126 // frame, then set a flag to poll the cache on the next frame.
2127 static bool gLoadPending = false;
2128 if (gLoadPending) {
2129 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2130 int hitCount) {
2131 CachedGLSL& entry(fCachedGLSL.push_back());
2132 entry.fKey = key;
2133 SkMD5 hash;
2134 hash.write(key->bytes(), key->size());
2135 SkMD5::Digest digest = hash.finish();
2136 for (int i = 0; i < 16; ++i) {
2137 entry.fKeyString.appendf("%02x", digest.data[i]);
2138 }
2139
Brian Osmana66081d2019-09-03 14:59:26 -04002140 SkReader32 reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002141 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002142 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2143 entry.fInputs,
2144 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002145 };
2146 fCachedGLSL.reset();
2147 fPersistentCache.foreach(collectShaders);
2148 gLoadPending = false;
2149 }
2150
2151 // Defer actually doing the load/save logic so that we can trigger a save when we
2152 // start or finish hovering on a tree node in the list below:
2153 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002154 bool doSave = ImGui::Button("Save");
2155 if (backendIsGL) {
2156 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002157 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2158 GrContextOptions::ShaderCacheStrategy::kSkSL;
2159 if (ImGui::Checkbox("SkSL", &sksl)) {
2160 params.fGrContextOptions.fShaderCacheStrategy = sksl
2161 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2162 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002163 paramsChanged = true;
2164 doLoad = true;
2165 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2166 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002167 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002168
2169 ImGui::BeginChild("##ScrollingRegion");
2170 for (auto& entry : fCachedGLSL) {
2171 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2172 bool hovered = ImGui::IsItemHovered();
2173 if (hovered != entry.fHovered) {
2174 // Force a save to patch the highlight shader in/out
2175 entry.fHovered = hovered;
2176 doSave = true;
2177 }
2178 if (inTreeNode) {
2179 // Full width, and a reasonable amount of space for each shader.
2180 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2181 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2182 boxSize);
2183 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2184 boxSize);
2185 ImGui::TreePop();
2186 }
2187 }
2188 ImGui::EndChild();
2189
2190 if (doLoad) {
2191 fPersistentCache.reset();
2192 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2193 gLoadPending = true;
2194 }
2195 if (doSave) {
2196 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002197 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002198 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2199 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002200
Brian Osman072e6fc2019-06-12 11:35:41 -04002201 SkSL::String highlight;
2202 if (!sksl) {
2203 highlight = shaderCaps->versionDeclString();
2204 if (shaderCaps->usesPrecisionModifiers()) {
2205 highlight.append("precision mediump float;\n");
2206 }
Brian Osman5bee3902019-05-07 09:55:45 -04002207 }
2208 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002209 highlight.appendf("out %s sk_FragColor;\n"
2210 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2211 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002212
2213 fPersistentCache.reset();
2214 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2215 for (auto& entry : fCachedGLSL) {
2216 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2217 if (entry.fHovered) {
2218 entry.fShader[kFragment_GrShaderType] = highlight;
2219 }
2220
Brian Osmana085a412019-04-25 09:44:43 -04002221 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2222 entry.fShader,
2223 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002224 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002225 fPersistentCache.store(*entry.fKey, *data);
2226
2227 entry.fShader[kFragment_GrShaderType] = backup;
2228 }
2229 }
2230 }
Brian Osman79086b92017-02-10 13:36:16 -05002231 }
Brian Salomon99a33902017-03-07 15:16:34 -05002232 if (paramsChanged) {
2233 fDeferredActions.push_back([=]() {
2234 fWindow->setRequestedDisplayParams(params);
2235 fWindow->inval();
2236 this->updateTitle();
2237 });
2238 }
Brian Osman79086b92017-02-10 13:36:16 -05002239 ImGui::End();
2240 }
2241
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002242 if (gShaderErrorHandler.fErrors.count()) {
2243 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2244 ImGui::Begin("Shader Errors");
2245 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2246 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002247 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2248 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2249 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2250 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002251 }
2252 ImGui::End();
2253 gShaderErrorHandler.reset();
2254 }
2255
Brian Osmanf6877092017-02-13 09:39:57 -05002256 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002257 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2258 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002259 static int zoomFactor = 8;
2260 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002261 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002262 }
2263 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2264 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002265 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002266 }
Brian Osmanf6877092017-02-13 09:39:57 -05002267
Ben Wagner3627d2e2018-06-26 14:23:20 -04002268 if (!fZoomWindowFixed) {
2269 ImVec2 mousePos = ImGui::GetMousePos();
2270 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2271 }
2272 SkScalar x = fZoomWindowLocation.x();
2273 SkScalar y = fZoomWindowLocation.y();
2274 int xInt = SkScalarRoundToInt(x);
2275 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002276 ImVec2 avail = ImGui::GetContentRegionAvail();
2277
Brian Osmanead517d2017-11-13 15:36:36 -05002278 uint32_t pixel = 0;
2279 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002280 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002281 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002282 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002283 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002284 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002285 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2286 }
2287
Brian Osmand67e5182017-12-08 16:46:09 -05002288 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002289 // Translate so the region of the image that's under the mouse cursor is centered
2290 // in the zoom canvas:
2291 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002292 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2293 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002294 c->drawImage(this->fLastImage, 0, 0);
2295
2296 SkPaint outline;
2297 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002298 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002299 });
Brian Osmanf6877092017-02-13 09:39:57 -05002300 }
2301
2302 ImGui::End();
2303 }
Brian Osman79086b92017-02-10 13:36:16 -05002304}
2305
liyuqian2edb0f42016-07-06 14:11:32 -07002306void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002307 for (int i = 0; i < fDeferredActions.count(); ++i) {
2308 fDeferredActions[i]();
2309 }
2310 fDeferredActions.reset();
2311
Brian Osman56a24812017-12-19 11:15:16 -05002312 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002313 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002314 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002315 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002316
Brian Osman79086b92017-02-10 13:36:16 -05002317 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002318 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2319 // not be visible, though. So we need to redraw if there is at least one visible window, or
2320 // more than one active window. Newly created windows are active but not visible for one frame
2321 // while they determine their layout and sizing.
2322 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2323 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002324 fWindow->inval();
2325 }
jvanverth9f372462016-04-06 06:08:59 -07002326}
liyuqiane5a6cd92016-05-27 08:52:52 -07002327
Florin Malitab632df72018-06-18 21:23:06 -04002328template <typename OptionsFunc>
2329static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2330 OptionsFunc&& optionsFunc) {
2331 writer.beginObject();
2332 {
2333 writer.appendString(kName , name);
2334 writer.appendString(kValue, value);
2335
2336 writer.beginArray(kOptions);
2337 {
2338 optionsFunc(writer);
2339 }
2340 writer.endArray();
2341 }
2342 writer.endObject();
2343}
2344
2345
liyuqiane5a6cd92016-05-27 08:52:52 -07002346void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002347 if (!fWindow) {
2348 return;
2349 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002350 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002351 return; // Surface hasn't been created yet.
2352 }
2353
Florin Malitab632df72018-06-18 21:23:06 -04002354 SkDynamicMemoryWStream memStream;
2355 SkJSONWriter writer(&memStream);
2356 writer.beginArray();
2357
liyuqianb73c24b2016-06-03 08:47:23 -07002358 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002359 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2360 [this](SkJSONWriter& writer) {
2361 for(const auto& slide : fSlides) {
2362 writer.appendString(slide->getName().c_str());
2363 }
2364 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002365
liyuqianb73c24b2016-06-03 08:47:23 -07002366 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002367 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2368 [](SkJSONWriter& writer) {
2369 for (const auto& str : kBackendTypeStrings) {
2370 writer.appendString(str);
2371 }
2372 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002373
csmartdalton578f0642017-02-24 16:04:47 -07002374 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002375 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2376 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2377 [this](SkJSONWriter& writer) {
2378 writer.appendS32(0);
2379
2380 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2381 return;
2382 }
2383
2384 for (int msaa : {4, 8, 16}) {
2385 writer.appendS32(msaa);
2386 }
2387 });
csmartdalton578f0642017-02-24 16:04:47 -07002388
csmartdalton61cd31a2017-02-27 17:00:53 -07002389 // Path renderer state
2390 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002391 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2392 [this](SkJSONWriter& writer) {
2393 const GrContext* ctx = fWindow->getGrContext();
2394 if (!ctx) {
2395 writer.appendString("Software");
2396 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002397 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002398 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2399 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002400 if (caps->shaderCaps()->tessellationSupport()) {
2401 writer.appendString(
2402 gPathRendererNames[GpuPathRenderers::kGpuTessellation].c_str());
2403 }
Florin Malitab632df72018-06-18 21:23:06 -04002404 if (caps->shaderCaps()->pathRenderingSupport()) {
2405 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002406 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002407 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002408 }
2409 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002410 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2411 writer.appendString(
2412 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2413 }
2414 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2415 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002416 writer.appendString(gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2417 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002418 }
2419 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002420
liyuqianb73c24b2016-06-03 08:47:23 -07002421 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002422 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2423 [this](SkJSONWriter& writer) {
2424 writer.appendString(kSoftkeyHint);
2425 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2426 writer.appendString(softkey.c_str());
2427 }
2428 });
liyuqianb73c24b2016-06-03 08:47:23 -07002429
Florin Malitab632df72018-06-18 21:23:06 -04002430 writer.endArray();
2431 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002432
Florin Malitab632df72018-06-18 21:23:06 -04002433 auto data = memStream.detachAsData();
2434
2435 // TODO: would be cool to avoid this copy
2436 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2437
2438 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002439}
2440
2441void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002442 // For those who will add more features to handle the state change in this function:
2443 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2444 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2445 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002446 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002447 for (int i = 0; i < fSlides.count(); ++i) {
2448 if (fSlides[i]->getName().equals(stateValue)) {
2449 this->setCurrentSlide(i);
2450 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002451 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002452 }
Florin Malitaab99c342018-01-16 16:23:03 -05002453
2454 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002455 } else if (stateName.equals(kBackendStateName)) {
2456 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2457 if (stateValue.equals(kBackendTypeStrings[i])) {
2458 if (fBackendType != i) {
2459 fBackendType = (sk_app::Window::BackendType)i;
2460 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002461 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002462 }
2463 break;
2464 }
2465 }
csmartdalton578f0642017-02-24 16:04:47 -07002466 } else if (stateName.equals(kMSAAStateName)) {
2467 DisplayParams params = fWindow->getRequestedDisplayParams();
2468 int sampleCount = atoi(stateValue.c_str());
2469 if (sampleCount != params.fMSAASampleCount) {
2470 params.fMSAASampleCount = sampleCount;
2471 fWindow->setRequestedDisplayParams(params);
2472 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002473 this->updateTitle();
2474 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002475 }
2476 } else if (stateName.equals(kPathRendererStateName)) {
2477 DisplayParams params = fWindow->getRequestedDisplayParams();
2478 for (const auto& pair : gPathRendererNames) {
2479 if (pair.second == stateValue.c_str()) {
2480 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2481 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2482 fWindow->setRequestedDisplayParams(params);
2483 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002484 this->updateTitle();
2485 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002486 }
2487 break;
2488 }
csmartdalton578f0642017-02-24 16:04:47 -07002489 }
liyuqianb73c24b2016-06-03 08:47:23 -07002490 } else if (stateName.equals(kSoftkeyStateName)) {
2491 if (!stateValue.equals(kSoftkeyHint)) {
2492 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002493 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002494 }
liyuqian2edb0f42016-07-06 14:11:32 -07002495 } else if (stateName.equals(kRefreshStateName)) {
2496 // This state is actually NOT in the UI state.
2497 // We use this to allow Android to quickly set bool fRefresh.
2498 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002499 } else {
2500 SkDebugf("Unknown stateName: %s", stateName.c_str());
2501 }
2502}
Brian Osman79086b92017-02-10 13:36:16 -05002503
Hal Canaryb1f411a2019-08-29 10:39:22 -04002504bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002505 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002506}
2507
Hal Canaryb1f411a2019-08-29 10:39:22 -04002508bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002509 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002510 fWindow->inval();
2511 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002512 } else {
2513 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002514 }
Brian Osman79086b92017-02-10 13:36:16 -05002515}