blob: 49ae040da3e98ff2233549cd9840c0c88eed0e90 [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 Salomon194db172017-08-17 14:37:06 -0400134const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700135 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400136#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
137 "ANGLE",
138#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400139#ifdef SK_DAWN
140 "Dawn",
141#endif
jvanverth063ece72016-06-17 09:29:14 -0700142#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700143 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700144#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400145#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500146 "Metal",
147#endif
csmartdalton578f0642017-02-24 16:04:47 -0700148 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700149};
150
bsalomon6c471f72016-07-26 12:56:32 -0700151static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400152#ifdef SK_DAWN
153 if (0 == strcmp(str, "dawn")) {
154 return sk_app::Window::kDawn_BackendType;
155 } else
156#endif
bsalomon6c471f72016-07-26 12:56:32 -0700157#ifdef SK_VULKAN
158 if (0 == strcmp(str, "vk")) {
159 return sk_app::Window::kVulkan_BackendType;
160 } else
161#endif
Brian Salomon194db172017-08-17 14:37:06 -0400162#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
163 if (0 == strcmp(str, "angle")) {
164 return sk_app::Window::kANGLE_BackendType;
165 } else
166#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400167#ifdef SK_METAL
168 if (0 == strcmp(str, "mtl")) {
169 return sk_app::Window::kMetal_BackendType;
170 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500171#endif
bsalomon6c471f72016-07-26 12:56:32 -0700172 if (0 == strcmp(str, "gl")) {
173 return sk_app::Window::kNativeGL_BackendType;
174 } else if (0 == strcmp(str, "sw")) {
175 return sk_app::Window::kRaster_BackendType;
176 } else {
177 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
178 return sk_app::Window::kRaster_BackendType;
179 }
180}
181
Brian Osmana109e392017-02-24 09:49:14 -0500182static SkColorSpacePrimaries gSrgbPrimaries = {
183 0.64f, 0.33f,
184 0.30f, 0.60f,
185 0.15f, 0.06f,
186 0.3127f, 0.3290f };
187
188static SkColorSpacePrimaries gAdobePrimaries = {
189 0.64f, 0.33f,
190 0.21f, 0.71f,
191 0.15f, 0.06f,
192 0.3127f, 0.3290f };
193
194static SkColorSpacePrimaries gP3Primaries = {
195 0.680f, 0.320f,
196 0.265f, 0.690f,
197 0.150f, 0.060f,
198 0.3127f, 0.3290f };
199
200static SkColorSpacePrimaries gRec2020Primaries = {
201 0.708f, 0.292f,
202 0.170f, 0.797f,
203 0.131f, 0.046f,
204 0.3127f, 0.3290f };
205
206struct NamedPrimaries {
207 const char* fName;
208 SkColorSpacePrimaries* fPrimaries;
209} gNamedPrimaries[] = {
210 { "sRGB", &gSrgbPrimaries },
211 { "AdobeRGB", &gAdobePrimaries },
212 { "P3", &gP3Primaries },
213 { "Rec. 2020", &gRec2020Primaries },
214};
215
216static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
217 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
218}
219
Brian Osman70d2f432017-11-08 09:54:10 -0500220static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
221 // In raster mode, we still use GL for the window.
222 // This lets us render the GUI faster (and correct).
223 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
224}
225
Jim Van Verth74826c82019-03-01 14:37:30 -0500226class NullSlide : public Slide {
227 SkISize getDimensions() const override {
228 return SkISize::Make(640, 480);
229 }
230
231 void draw(SkCanvas* canvas) override {
232 canvas->clear(0xffff11ff);
233 }
234};
235
liyuqiane5a6cd92016-05-27 08:52:52 -0700236const char* kName = "name";
237const char* kValue = "value";
238const char* kOptions = "options";
239const char* kSlideStateName = "Slide";
240const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700241const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700242const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700243const char* kSoftkeyStateName = "Softkey";
244const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700245const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700246const char* kON = "ON";
247const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700248const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700249
jvanverth34524262016-05-04 13:49:13 -0700250Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500251 : fCurrentSlide(-1)
252 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500253 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400254 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500255 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500256 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500257 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500258 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400259 , fZoomWindowFixed(false)
260 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500261 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400262 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700263 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500264 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500265 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500266 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500267 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700268 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400269 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400270 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400271 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500272 , fTiled(false)
273 , fDrawTileBoundaries(false)
274 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400275 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700276{
Greg Daniel285db442016-10-14 09:12:53 -0400277 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700278
Chris Dalton37ae4b02019-12-28 14:51:11 -0700279 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Daltonb832ce62020-01-06 19:49:37 -0700280 gPathRendererNames[GpuPathRenderers::kGpuTessellation] = "GPU Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500281 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500282 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600283 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500284 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
285 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700286
jvanverth2bb3b6d2016-04-08 07:24:09 -0700287 SkDebugf("Command line arguments: ");
288 for (int i = 1; i < argc; ++i) {
289 SkDebugf("%s ", argv[i]);
290 }
291 SkDebugf("\n");
292
Mike Klein88544fb2019-03-20 10:50:33 -0500293 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500294#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400295 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500296#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700297
Mike Klein19cc0f62019-03-22 15:30:07 -0500298 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500299
Brian Osmanbc8150f2017-07-24 11:38:01 -0400300 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400301 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400302
bsalomon6c471f72016-07-26 12:56:32 -0700303 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700304 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700305
csmartdalton578f0642017-02-24 16:04:47 -0700306 DisplayParams displayParams;
307 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700308 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400309 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400310 displayParams.fGrContextOptions.fShaderCacheStrategy =
311 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400312 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
313 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700314 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500315 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700316
Brian Osman56a24812017-12-19 11:15:16 -0500317 // Configure timers
318 fStatsLayer.setActive(false);
319 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
320 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
321 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
322
jvanverth9f372462016-04-06 06:08:59 -0700323 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700324 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500325 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500326 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500327 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700328
brianosman622c8d52016-05-10 06:50:49 -0700329 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500330 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
331 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
332 fWindow->inval();
333 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500334 // Command to jump directly to the slide picker and give it focus
335 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
336 this->fShowImGuiDebugWindow = true;
337 this->fShowSlidePicker = true;
338 fWindow->inval();
339 });
340 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400341 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500342 this->fShowImGuiDebugWindow = true;
343 this->fShowSlidePicker = true;
344 fWindow->inval();
345 });
Brian Osman79086b92017-02-10 13:36:16 -0500346 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
347 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
348 fWindow->inval();
349 });
Brian Osmanf6877092017-02-13 09:39:57 -0500350 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
351 this->fShowZoomWindow = !this->fShowZoomWindow;
352 fWindow->inval();
353 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400354 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
355 this->fZoomWindowFixed = !this->fZoomWindowFixed;
356 fWindow->inval();
357 });
Greg Danield0794cc2019-03-27 16:23:26 -0400358 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
359 DisplayParams params = fWindow->getRequestedDisplayParams();
360 params.fDisableVsync = !params.fDisableVsync;
361 fWindow->setRequestedDisplayParams(params);
362 this->updateTitle();
363 fWindow->inval();
364 });
Mike Reedf702ed42019-07-22 17:00:49 -0400365 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
366 fRefresh = !fRefresh;
367 fWindow->inval();
368 });
brianosman622c8d52016-05-10 06:50:49 -0700369 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500370 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700371 fWindow->inval();
372 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400373 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500374 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400375 this->updateTitle();
376 fWindow->inval();
377 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500378 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500379 switch (fColorMode) {
380 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500381 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500382 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500383 case ColorMode::kColorManaged8888:
384 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500385 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500386 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400387 this->setColorMode(ColorMode::kColorManagedF16Norm);
388 break;
389 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500390 this->setColorMode(ColorMode::kLegacy);
391 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500392 }
brianosman622c8d52016-05-10 06:50:49 -0700393 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700394 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
395 DisplayParams params = fWindow->getRequestedDisplayParams();
396 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
397 fWindow->setRequestedDisplayParams(params);
398 fWindow->inval();
399 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400400 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500401 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700402 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400403 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500404 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700405 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400406 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700407 this->changeZoomLevel(1.f / 32.f);
408 fWindow->inval();
409 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400410 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700411 this->changeZoomLevel(-1.f / 32.f);
412 fWindow->inval();
413 });
jvanverthaf236b52016-05-20 06:01:06 -0700414 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400415 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
416 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500417 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400418#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
419 if (newBackend == sk_app::Window::kVulkan_BackendType) {
420 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
421 sk_app::Window::kBackendTypeCount);
422 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
423 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500424 }
425#endif
Brian Osman621491e2017-02-28 15:45:01 -0500426 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700427 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500428 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
429 fSaveToSKP = true;
430 fWindow->inval();
431 });
Mike Reed376d8122019-03-14 11:39:02 -0400432 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
433 fShowSlideDimensions = !fShowSlideDimensions;
434 fWindow->inval();
435 });
Ben Wagner37c54032018-04-13 14:30:23 -0400436 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
437 DisplayParams params = fWindow->getRequestedDisplayParams();
438 uint32_t flags = params.fSurfaceProps.flags();
439 if (!fPixelGeometryOverrides) {
440 fPixelGeometryOverrides = true;
441 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
442 } else {
443 switch (params.fSurfaceProps.pixelGeometry()) {
444 case kUnknown_SkPixelGeometry:
445 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
446 break;
447 case kRGB_H_SkPixelGeometry:
448 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
449 break;
450 case kBGR_H_SkPixelGeometry:
451 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
452 break;
453 case kRGB_V_SkPixelGeometry:
454 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
455 break;
456 case kBGR_V_SkPixelGeometry:
457 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
458 fPixelGeometryOverrides = false;
459 break;
460 }
461 }
462 fWindow->setRequestedDisplayParams(params);
463 this->updateTitle();
464 fWindow->inval();
465 });
Ben Wagner9613e452019-01-23 10:34:59 -0500466 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500467 if (!fFontOverrides.fHinting) {
468 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400469 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500470 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500471 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400472 case SkFontHinting::kNone:
473 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500474 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400475 case SkFontHinting::kSlight:
476 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500477 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400478 case SkFontHinting::kNormal:
479 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500480 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400481 case SkFontHinting::kFull:
482 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500483 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500484 break;
485 }
486 }
487 this->updateTitle();
488 fWindow->inval();
489 });
490 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500491 if (!fPaintOverrides.fAntiAlias) {
492 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
493 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500494 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500495 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500496 } else {
497 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500498 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500499 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500500 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400501 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500502 break;
503 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500504 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500505 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400506 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500507 break;
508 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500509 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400510 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500511 break;
512 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500513 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
514 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500515 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
516 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500517 break;
518 }
519 }
520 this->updateTitle();
521 fWindow->inval();
522 });
Ben Wagner37c54032018-04-13 14:30:23 -0400523 fCommands.addCommand('D', "Modes", "DFT", [this]() {
524 DisplayParams params = fWindow->getRequestedDisplayParams();
525 uint32_t flags = params.fSurfaceProps.flags();
526 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
527 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
528 fWindow->setRequestedDisplayParams(params);
529 this->updateTitle();
530 fWindow->inval();
531 });
Ben Wagner9613e452019-01-23 10:34:59 -0500532 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500533 if (!fFontOverrides.fEdging) {
534 fFontOverrides.fEdging = true;
535 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500536 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500537 switch (fFont.getEdging()) {
538 case SkFont::Edging::kAlias:
539 fFont.setEdging(SkFont::Edging::kAntiAlias);
540 break;
541 case SkFont::Edging::kAntiAlias:
542 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
543 break;
544 case SkFont::Edging::kSubpixelAntiAlias:
545 fFont.setEdging(SkFont::Edging::kAlias);
546 fFontOverrides.fEdging = false;
547 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500548 }
549 }
550 this->updateTitle();
551 fWindow->inval();
552 });
Ben Wagner9613e452019-01-23 10:34:59 -0500553 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500554 if (!fFontOverrides.fSubpixel) {
555 fFontOverrides.fSubpixel = true;
556 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500557 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500558 if (!fFont.isSubpixel()) {
559 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500560 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500561 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500562 }
563 }
564 this->updateTitle();
565 fWindow->inval();
566 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400567 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
568 if (!fFontOverrides.fBaselineSnap) {
569 fFontOverrides.fBaselineSnap = true;
570 fFont.setBaselineSnap(false);
571 } else {
572 if (!fFont.isBaselineSnap()) {
573 fFont.setBaselineSnap(true);
574 } else {
575 fFontOverrides.fBaselineSnap = false;
576 }
577 }
578 this->updateTitle();
579 fWindow->inval();
580 });
Brian Osman805a7272018-05-02 15:40:20 -0400581 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
582 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
583 : kPerspective_Real;
584 this->updateTitle();
585 fWindow->inval();
586 });
587 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
588 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
589 : kPerspective_Off;
590 this->updateTitle();
591 fWindow->inval();
592 });
Brian Osman207d4102019-01-10 09:40:58 -0500593 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
594 fAnimTimer.togglePauseResume();
595 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400596 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
597 fZoomUI = !fZoomUI;
598 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
599 fWindow->inval();
600 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500601
jvanverth2bb3b6d2016-04-08 07:24:09 -0700602 // set up slides
603 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500604 if (FLAGS_list) {
605 this->listNames();
606 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700607
Brian Osman9bb47cf2018-04-26 15:55:00 -0400608 fPerspectivePoints[0].set(0, 0);
609 fPerspectivePoints[1].set(1, 0);
610 fPerspectivePoints[2].set(0, 1);
611 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700612 fAnimTimer.run();
613
Hal Canaryc465d132017-12-08 10:21:31 -0500614 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500615 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400616 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500617 }
618 fImGuiGamutPaint.setColor(SK_ColorWHITE);
619 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
620
jongdeok.kim804f17e2019-02-26 14:39:23 +0900621 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500622 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700623}
624
jvanverth34524262016-05-04 13:49:13 -0700625void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400626 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
627 static const struct {
628 const char* fExtension;
629 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500630 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400631 const SlideFactory fFactory;
632 } gExternalSlidesInfo[] = {
633 { ".skp", "skp-dir", FLAGS_skps,
634 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
635 return sk_make_sp<SKPSlide>(name, path);}
636 },
637 { ".jpg", "jpg-dir", FLAGS_jpgs,
638 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
639 return sk_make_sp<ImageSlide>(name, path);}
640 },
Florin Malita87ccf332018-05-04 12:23:24 -0400641#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400642 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400643 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
644 return sk_make_sp<SkottieSlide>(name, path);}
645 },
Florin Malita87ccf332018-05-04 12:23:24 -0400646#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400647#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400648 { ".svg", "svg-dir", FLAGS_svgs,
649 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
650 return sk_make_sp<SvgSlide>(name, path);}
651 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400652#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400653 };
jvanverthc265a922016-04-08 12:51:45 -0700654
Brian Salomon343553a2018-09-05 15:41:23 -0400655 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700656
Mike Klein88544fb2019-03-20 10:50:33 -0500657 const auto addSlide =
658 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
659 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
660 return;
661 }
liyuqian6f163d22016-06-13 12:26:45 -0700662
Mike Klein88544fb2019-03-20 10:50:33 -0500663 if (auto slide = fact(name, path)) {
664 dirSlides.push_back(slide);
665 fSlides.push_back(std::move(slide));
666 }
667 };
Florin Malita76a076b2018-02-15 18:40:48 -0500668
Florin Malita38792ce2018-05-08 10:36:18 -0400669 if (!FLAGS_file.isEmpty()) {
670 // single file mode
671 const SkString file(FLAGS_file[0]);
672
673 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
674 for (const auto& sinfo : gExternalSlidesInfo) {
675 if (file.endsWith(sinfo.fExtension)) {
676 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
677 return;
678 }
679 }
680
681 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
682 } else {
683 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
684 }
685
686 return;
687 }
688
689 // Bisect slide.
690 if (!FLAGS_bisect.isEmpty()) {
691 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500692 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400693 if (FLAGS_bisect.count() >= 2) {
694 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
695 bisect->onChar(*ch);
696 }
697 }
698 fSlides.push_back(std::move(bisect));
699 }
700 }
701
702 // GMs
703 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400704 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400705 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500706 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400707 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400708 fSlides.push_back(std::move(slide));
709 }
Florin Malita38792ce2018-05-08 10:36:18 -0400710 }
711 // reverse gms
712 int numGMs = fSlides.count() - firstGM;
713 for (int i = 0; i < numGMs/2; ++i) {
714 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
715 }
716
717 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400718 for (const SampleFactory factory : SampleRegistry::Range()) {
719 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500720 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400721 fSlides.push_back(slide);
722 }
Florin Malita38792ce2018-05-08 10:36:18 -0400723 }
724
Brian Osman7c979f52019-02-12 13:27:51 -0500725 // Particle demo
726 {
727 // TODO: Convert this to a sample
728 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500729 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500730 fSlides.push_back(std::move(slide));
731 }
732 }
733
Brian Osmand927bd22019-12-18 11:23:12 -0500734 // Runtime shader editor
735 {
736 sk_sp<Slide> slide(new SkSLSlide());
737 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
738 fSlides.push_back(std::move(slide));
739 }
740 }
741
Florin Malita0ffa3222018-04-05 14:34:45 -0400742 for (const auto& info : gExternalSlidesInfo) {
743 for (const auto& flag : info.fFlags) {
744 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
745 // single file
746 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
747 } else {
748 // directory
749 SkOSFile::Iter it(flag.c_str(), info.fExtension);
750 SkString name;
751 while (it.next(&name)) {
752 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
753 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400754 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400755 if (!dirSlides.empty()) {
756 fSlides.push_back(
757 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
758 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500759 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400760 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400761 }
762 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500763
764 if (!fSlides.count()) {
765 sk_sp<Slide> slide(new NullSlide());
766 fSlides.push_back(std::move(slide));
767 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700768}
769
770
jvanverth34524262016-05-04 13:49:13 -0700771Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700772 fWindow->detach();
773 delete fWindow;
774}
775
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500776struct SkPaintTitleUpdater {
777 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
778 void append(const char* s) {
779 if (fCount == 0) {
780 fTitle->append(" {");
781 } else {
782 fTitle->append(", ");
783 }
784 fTitle->append(s);
785 ++fCount;
786 }
787 void done() {
788 if (fCount > 0) {
789 fTitle->append("}");
790 }
791 }
792 SkString* fTitle;
793 int fCount;
794};
795
brianosman05de2162016-05-06 13:28:57 -0700796void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700797 if (!fWindow) {
798 return;
799 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500800 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700801 return; // Surface hasn't been created yet.
802 }
803
jvanverth34524262016-05-04 13:49:13 -0700804 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700805 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700806
Mike Kleine5acd752019-03-22 09:57:16 -0500807 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400808 if (gSkForceAnalyticAA) {
809 title.append(" <FAAA>");
810 } else {
811 title.append(" <AAA>");
812 }
813 }
814
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500815 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500816 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
817 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400818 const char* on, const char* off)
819 {
Ben Wagner9613e452019-01-23 10:34:59 -0500820 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400821 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500822 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400823 };
824
Ben Wagner9613e452019-01-23 10:34:59 -0500825 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
826 const char* on, const char* off)
827 {
828 if (fFontOverrides.*flag) {
829 paintTitle.append((fFont.*isFlag)() ? on : off);
830 }
831 };
832
833 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
834 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500835 if (fPaintOverrides.fFilterQuality) {
836 switch (fPaint.getFilterQuality()) {
837 case kNone_SkFilterQuality:
838 paintTitle.append("NoFilter");
839 break;
840 case kLow_SkFilterQuality:
841 paintTitle.append("LowFilter");
842 break;
843 case kMedium_SkFilterQuality:
844 paintTitle.append("MediumFilter");
845 break;
846 case kHigh_SkFilterQuality:
847 paintTitle.append("HighFilter");
848 break;
849 }
850 }
Ben Wagner9613e452019-01-23 10:34:59 -0500851
852 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
853 "Force Autohint", "No Force Autohint");
854 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400855 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500856 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
857 "Linear Metrics", "Non-Linear Metrics");
858 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
859 "Bitmap Text", "No Bitmap Text");
860 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
861
862 if (fFontOverrides.fEdging) {
863 switch (fFont.getEdging()) {
864 case SkFont::Edging::kAlias:
865 paintTitle.append("Alias Text");
866 break;
867 case SkFont::Edging::kAntiAlias:
868 paintTitle.append("Antialias Text");
869 break;
870 case SkFont::Edging::kSubpixelAntiAlias:
871 paintTitle.append("Subpixel Antialias Text");
872 break;
873 }
874 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400875
Mike Reed3ae47332019-01-04 10:11:46 -0500876 if (fFontOverrides.fHinting) {
877 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400878 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500879 paintTitle.append("No Hinting");
880 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400881 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500882 paintTitle.append("Slight Hinting");
883 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400884 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500885 paintTitle.append("Normal Hinting");
886 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400887 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500888 paintTitle.append("Full Hinting");
889 break;
890 }
891 }
892 paintTitle.done();
893
Brian Osman92004802017-03-06 11:47:26 -0500894 switch (fColorMode) {
895 case ColorMode::kLegacy:
896 title.append(" Legacy 8888");
897 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500898 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500899 title.append(" ColorManaged 8888");
900 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500901 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500902 title.append(" ColorManaged F16");
903 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400904 case ColorMode::kColorManagedF16Norm:
905 title.append(" ColorManaged F16 Norm");
906 break;
Brian Osman92004802017-03-06 11:47:26 -0500907 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500908
Brian Osman92004802017-03-06 11:47:26 -0500909 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500910 int curPrimaries = -1;
911 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
912 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
913 curPrimaries = i;
914 break;
915 }
916 }
Brian Osman03115dc2018-11-26 13:55:19 -0500917 title.appendf(" %s Gamma %f",
918 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500919 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700920 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500921
Ben Wagner37c54032018-04-13 14:30:23 -0400922 const DisplayParams& params = fWindow->getRequestedDisplayParams();
923 if (fPixelGeometryOverrides) {
924 switch (params.fSurfaceProps.pixelGeometry()) {
925 case kUnknown_SkPixelGeometry:
926 title.append( " Flat");
927 break;
928 case kRGB_H_SkPixelGeometry:
929 title.append( " RGB");
930 break;
931 case kBGR_H_SkPixelGeometry:
932 title.append( " BGR");
933 break;
934 case kRGB_V_SkPixelGeometry:
935 title.append( " RGBV");
936 break;
937 case kBGR_V_SkPixelGeometry:
938 title.append( " BGRV");
939 break;
940 }
941 }
942
943 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
944 title.append(" DFT");
945 }
946
csmartdalton578f0642017-02-24 16:04:47 -0700947 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700948 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500949 int msaa = fWindow->sampleCount();
950 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700951 title.appendf(" MSAA: %i", msaa);
952 }
953 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700954
955 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -0700956 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700957 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
958 }
959
Brian Osman805a7272018-05-02 15:40:20 -0400960 if (kPerspective_Real == fPerspectiveMode) {
961 title.append(" Perpsective (Real)");
962 } else if (kPerspective_Fake == fPerspectiveMode) {
963 title.append(" Perspective (Fake)");
964 }
965
brianosman05de2162016-05-06 13:28:57 -0700966 fWindow->setTitle(title.c_str());
967}
968
Florin Malitaab99c342018-01-16 16:23:03 -0500969int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500970
971 if (!FLAGS_slide.isEmpty()) {
972 int count = fSlides.count();
973 for (int i = 0; i < count; i++) {
974 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500975 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500976 }
977 }
978
979 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
980 this->listNames();
981 }
982
Florin Malitaab99c342018-01-16 16:23:03 -0500983 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500984}
985
Florin Malitaab99c342018-01-16 16:23:03 -0500986void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500987 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500988 for (const auto& slide : fSlides) {
989 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500990 }
991}
992
Florin Malitaab99c342018-01-16 16:23:03 -0500993void Viewer::setCurrentSlide(int slide) {
994 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700995
Florin Malitaab99c342018-01-16 16:23:03 -0500996 if (slide == fCurrentSlide) {
997 return;
998 }
999
1000 if (fCurrentSlide >= 0) {
1001 fSlides[fCurrentSlide]->unload();
1002 }
1003
1004 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1005 SkIntToScalar(fWindow->height()));
1006 fCurrentSlide = slide;
1007 this->setupCurrentSlide();
1008}
1009
1010void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001011 if (fCurrentSlide >= 0) {
1012 // prepare dimensions for image slides
1013 fGesture.resetTouchState();
1014 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001015
Jim Van Verth0848fb02018-01-22 13:39:30 -05001016 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1017 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1018 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001019
Jim Van Verth0848fb02018-01-22 13:39:30 -05001020 // Start with a matrix that scales the slide to the available screen space
1021 if (fWindow->scaleContentToFit()) {
1022 if (windowRect.width() > 0 && windowRect.height() > 0) {
1023 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1024 }
liyuqiane46e4f02016-05-20 07:32:19 -07001025 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001026
1027 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001028 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001029
1030 this->updateTitle();
1031 this->updateUIState();
1032
1033 fStatsLayer.resetMeasurements();
1034
1035 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001036 }
jvanverthc265a922016-04-08 12:51:45 -07001037}
1038
Brian Osmanaba642c2020-02-06 12:52:25 -05001039#define MAX_ZOOM_LEVEL 8.0f
1040#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001041
jvanverth34524262016-05-04 13:49:13 -07001042void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001043 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001044 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001045 this->preTouchMatrixChanged();
1046}
Yuqian Li755778c2018-03-28 16:23:31 -04001047
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001048void Viewer::preTouchMatrixChanged() {
1049 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001050 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1051 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1052 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1053 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1054}
1055
Brian Osman805a7272018-05-02 15:40:20 -04001056SkMatrix Viewer::computePerspectiveMatrix() {
1057 SkScalar w = fWindow->width(), h = fWindow->height();
1058 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1059 SkPoint perspPts[4] = {
1060 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1061 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1062 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1063 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1064 };
1065 SkMatrix m;
1066 m.setPolyToPoly(orthoPts, perspPts, 4);
1067 return m;
1068}
1069
Yuqian Li755778c2018-03-28 16:23:31 -04001070SkMatrix Viewer::computePreTouchMatrix() {
1071 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001072
1073 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001074 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001075 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001076
1077 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1078 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001079
Brian Osman805a7272018-05-02 15:40:20 -04001080 if (kPerspective_Real == fPerspectiveMode) {
1081 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001082 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001083 }
1084
Yuqian Li755778c2018-03-28 16:23:31 -04001085 return m;
jvanverthc265a922016-04-08 12:51:45 -07001086}
1087
liyuqiand3cdbca2016-05-17 12:44:20 -07001088SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001089 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001090 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001091 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001092 return m;
jvanverthc265a922016-04-08 12:51:45 -07001093}
1094
Brian Osman621491e2017-02-28 15:45:01 -05001095void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001096 fPersistentCache.reset();
1097 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001098 fBackendType = backendType;
1099
1100 fWindow->detach();
1101
Brian Osman70d2f432017-11-08 09:54:10 -05001102#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001103 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1104 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001105 DisplayParams params = fWindow->getRequestedDisplayParams();
1106 delete fWindow;
1107 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001108
Brian Osman70d2f432017-11-08 09:54:10 -05001109 // re-register callbacks
1110 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001111 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001112 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001113 fWindow->pushLayer(&fImGuiLayer);
1114
Brian Osman70d2f432017-11-08 09:54:10 -05001115 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1116 // will still include our correct sample count. But the re-created fWindow will lose that
1117 // information. On Windows, we need to re-create the window when changing sample count,
1118 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1119 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1120 // as if we had never changed windows at all).
1121 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001122#endif
1123
Brian Osman70d2f432017-11-08 09:54:10 -05001124 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001125}
1126
Brian Osman92004802017-03-06 11:47:26 -05001127void Viewer::setColorMode(ColorMode colorMode) {
1128 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001129 this->updateTitle();
1130 fWindow->inval();
1131}
1132
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001133class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1134public:
Mike Reed3ae47332019-01-04 10:11:46 -05001135 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1136 SkFont* font, Viewer::SkFontFields* ffields)
1137 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001138 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001139 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1140 sk_sp<SkTextBlob>* cache) {
1141 bool blobWillChange = false;
1142 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001143 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1144 bool shouldDraw = this->filterFont(&filteredFont);
1145 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001146 blobWillChange = true;
1147 break;
1148 }
1149 }
1150 if (!blobWillChange) {
1151 return blob;
1152 }
1153
1154 SkTextBlobBuilder builder;
1155 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001156 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1157 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001158 if (!shouldDraw) {
1159 continue;
1160 }
1161
Mike Reed3ae47332019-01-04 10:11:46 -05001162 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001163
Ben Wagner41e40472018-09-24 13:01:54 -04001164 const SkTextBlobBuilder::RunBuffer& runBuffer
1165 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001166 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001167 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001168 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001169 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001170 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001171 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001172 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001173 it.glyphCount(), it.textSize(), SkString())
1174 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1175 uint32_t glyphCount = it.glyphCount();
1176 if (it.glyphs()) {
1177 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1178 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1179 }
1180 if (it.pos()) {
1181 size_t posSize = sizeof(decltype(*it.pos()));
1182 uint8_t positioning = it.positioning();
1183 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1184 }
1185 if (it.text()) {
1186 size_t textSize = sizeof(decltype(*it.text()));
1187 uint32_t textCount = it.textSize();
1188 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1189 }
1190 if (it.clusters()) {
1191 size_t clusterSize = sizeof(decltype(*it.clusters()));
1192 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1193 }
1194 }
1195 *cache = builder.make();
1196 return cache->get();
1197 }
1198 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1199 const SkPaint& paint) override {
1200 sk_sp<SkTextBlob> cache;
1201 this->SkPaintFilterCanvas::onDrawTextBlob(
1202 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1203 }
Mike Reed3ae47332019-01-04 10:11:46 -05001204 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001205 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001206 font->writable()->setSize(fFont->getSize());
1207 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001208 if (fFontOverrides->fScaleX) {
1209 font->writable()->setScaleX(fFont->getScaleX());
1210 }
1211 if (fFontOverrides->fSkewX) {
1212 font->writable()->setSkewX(fFont->getSkewX());
1213 }
Mike Reed3ae47332019-01-04 10:11:46 -05001214 if (fFontOverrides->fHinting) {
1215 font->writable()->setHinting(fFont->getHinting());
1216 }
Ben Wagner9613e452019-01-23 10:34:59 -05001217 if (fFontOverrides->fEdging) {
1218 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001219 }
Ben Wagner9613e452019-01-23 10:34:59 -05001220 if (fFontOverrides->fEmbolden) {
1221 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001222 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001223 if (fFontOverrides->fBaselineSnap) {
1224 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1225 }
Ben Wagner9613e452019-01-23 10:34:59 -05001226 if (fFontOverrides->fLinearMetrics) {
1227 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001228 }
Ben Wagner9613e452019-01-23 10:34:59 -05001229 if (fFontOverrides->fSubpixel) {
1230 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001231 }
Ben Wagner9613e452019-01-23 10:34:59 -05001232 if (fFontOverrides->fEmbeddedBitmaps) {
1233 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001234 }
Ben Wagner9613e452019-01-23 10:34:59 -05001235 if (fFontOverrides->fForceAutoHinting) {
1236 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001237 }
Ben Wagner9613e452019-01-23 10:34:59 -05001238
Mike Reed3ae47332019-01-04 10:11:46 -05001239 return true;
1240 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001241 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001242 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001243 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001244 }
Ben Wagner9613e452019-01-23 10:34:59 -05001245 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001246 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001247 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001248 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001249 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001250 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001251 return true;
1252 }
1253 SkPaint* fPaint;
1254 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001255 SkFont* fFont;
1256 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001257};
1258
Robert Phillips9882dae2019-03-04 11:00:10 -05001259void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001260 if (fCurrentSlide < 0) {
1261 return;
1262 }
1263
Robert Phillips9882dae2019-03-04 11:00:10 -05001264 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001265
Brian Osmanf750fbc2017-02-08 10:47:28 -05001266 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001267 SkSurface* slideSurface = surface;
1268 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001269 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001270
Brian Osmane0d4fba2017-03-15 10:24:55 -04001271 // 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 -05001272 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001273 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001274 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001275 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001276 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001277 }
1278
Brian Osman3ac99cf2017-12-01 11:23:53 -05001279 if (fSaveToSKP) {
1280 SkPictureRecorder recorder;
1281 SkCanvas* recorderCanvas = recorder.beginRecording(
1282 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001283 fSlides[fCurrentSlide]->draw(recorderCanvas);
1284 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1285 SkFILEWStream stream("sample_app.skp");
1286 picture->serialize(&stream);
1287 fSaveToSKP = false;
1288 }
1289
Brian Osmane9ed0f02018-11-26 14:50:05 -05001290 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001291 SkColorType colorType;
1292 switch (fColorMode) {
1293 case ColorMode::kLegacy:
1294 case ColorMode::kColorManaged8888:
1295 colorType = kN32_SkColorType;
1296 break;
1297 case ColorMode::kColorManagedF16:
1298 colorType = kRGBA_F16_SkColorType;
1299 break;
1300 case ColorMode::kColorManagedF16Norm:
1301 colorType = kRGBA_F16Norm_SkColorType;
1302 break;
1303 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001304
1305 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001306 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1307 slideCanvas->getProps(&props);
1308
Brian Osmane9ed0f02018-11-26 14:50:05 -05001309 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1310 return Window::kRaster_BackendType == this->fBackendType
1311 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001312 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001313 };
1314
Brian Osman03115dc2018-11-26 13:55:19 -05001315 // We need to render offscreen if we're...
1316 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1317 // ... in any raster mode, because the window surface is actually GL
1318 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001319 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001320 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001321 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001322 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001323 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001324 colorSpace != nullptr ||
1325 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001326
Brian Osmane9ed0f02018-11-26 14:50:05 -05001327 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001328 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001329 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001330 }
1331
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001332 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001333 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001334 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001335 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001336 if (fTiled) {
1337 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1338 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001339 for (int y = 0; y < fWindow->height(); y += tileH) {
1340 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001341 SkAutoCanvasRestore acr(slideCanvas, true);
1342 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1343 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001344 }
1345 }
1346
1347 // Draw borders between tiles
1348 if (fDrawTileBoundaries) {
1349 SkPaint border;
1350 border.setColor(0x60FF00FF);
1351 border.setStyle(SkPaint::kStroke_Style);
1352 for (int y = 0; y < fWindow->height(); y += tileH) {
1353 for (int x = 0; x < fWindow->width(); x += tileW) {
1354 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1355 }
1356 }
1357 }
1358 } else {
1359 slideCanvas->concat(this->computeMatrix());
1360 if (kPerspective_Real == fPerspectiveMode) {
1361 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1362 }
Mike Reed3ae47332019-01-04 10:11:46 -05001363 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001364 fSlides[fCurrentSlide]->draw(&filterCanvas);
1365 }
Brian Osman56a24812017-12-19 11:15:16 -05001366 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001367 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001368
1369 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001370 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001371 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001372 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001373
1374 // If we rendered offscreen, snap an image and push the results to the window's canvas
1375 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001376 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001377
Robert Phillips9882dae2019-03-04 11:00:10 -05001378 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001379 SkPaint paint;
1380 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001381 int prePerspectiveCount = canvas->save();
1382 if (kPerspective_Fake == fPerspectiveMode) {
1383 paint.setFilterQuality(kHigh_SkFilterQuality);
1384 canvas->clear(SK_ColorWHITE);
1385 canvas->concat(this->computePerspectiveMatrix());
1386 }
Brian Osman03115dc2018-11-26 13:55:19 -05001387 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001388 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001389 }
Mike Reed376d8122019-03-14 11:39:02 -04001390
1391 if (fShowSlideDimensions) {
1392 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1393 SkPaint paint;
1394 paint.setColor(0x40FFFF00);
1395 surface->getCanvas()->drawRect(r, paint);
1396 }
liyuqian6f163d22016-06-13 12:26:45 -07001397}
1398
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001399void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001400 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001401 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001402}
Jim Van Verth6f449692017-02-14 15:16:46 -05001403
Robert Phillips9882dae2019-03-04 11:00:10 -05001404void Viewer::onPaint(SkSurface* surface) {
1405 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001406
Robert Phillips9882dae2019-03-04 11:00:10 -05001407 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001408
Brian Osmand67e5182017-12-08 16:46:09 -05001409 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001410
1411 if (GrContext* ctx = fWindow->getGrContext()) {
1412 // Clean out cache items that haven't been used in more than 10 seconds.
1413 ctx->performDeferredCleanup(std::chrono::seconds(10));
1414 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001415}
1416
Ben Wagnera1915972018-08-09 15:06:19 -04001417void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001418 if (fCurrentSlide >= 0) {
1419 fSlides[fCurrentSlide]->resize(width, height);
1420 }
Ben Wagnera1915972018-08-09 15:06:19 -04001421}
1422
Florin Malitacefc1b92018-02-19 21:43:47 -05001423SkPoint Viewer::mapEvent(float x, float y) {
1424 const auto m = this->computeMatrix();
1425 SkMatrix inv;
1426
1427 SkAssertResult(m.invert(&inv));
1428
1429 return inv.mapXY(x, y);
1430}
1431
Hal Canaryb1f411a2019-08-29 10:39:22 -04001432bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001433 if (GestureDevice::kMouse == fGestureDevice) {
1434 return false;
1435 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001436
1437 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001438 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001439 fWindow->inval();
1440 return true;
1441 }
1442
liyuqiand3cdbca2016-05-17 12:44:20 -07001443 void* castedOwner = reinterpret_cast<void*>(owner);
1444 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001445 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001446 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001447#if defined(SK_BUILD_FOR_IOS)
1448 // TODO: move IOS swipe detection higher up into the platform code
1449 SkPoint dir;
1450 if (fGesture.isFling(&dir)) {
1451 // swiping left or right
1452 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1453 if (dir.fX < 0) {
1454 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1455 fCurrentSlide + 1 : 0);
1456 } else {
1457 this->setCurrentSlide(fCurrentSlide > 0 ?
1458 fCurrentSlide - 1 : fSlides.count() - 1);
1459 }
1460 }
1461 fGesture.reset();
1462 }
1463#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001464 break;
1465 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001466 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001467 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001468 break;
1469 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001470 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001471 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001472 break;
1473 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001474 default: {
1475 // kLeft and kRight are only for swipes
1476 SkASSERT(false);
1477 break;
1478 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001479 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001480 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001481 fWindow->inval();
1482 return true;
1483}
1484
Hal Canaryb1f411a2019-08-29 10:39:22 -04001485bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001486 if (GestureDevice::kTouch == fGestureDevice) {
1487 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001488 }
Brian Osman16c81a12017-12-20 11:58:34 -05001489
Florin Malitacefc1b92018-02-19 21:43:47 -05001490 const auto slidePt = this->mapEvent(x, y);
1491 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1492 fWindow->inval();
1493 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001494 }
1495
1496 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001497 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001498 fGesture.touchEnd(nullptr);
1499 break;
1500 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001501 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001502 fGesture.touchBegin(nullptr, x, y);
1503 break;
1504 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001505 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001506 fGesture.touchMoved(nullptr, x, y);
1507 break;
1508 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001509 default: {
1510 SkASSERT(false); // shouldn't see kRight or kLeft here
1511 break;
1512 }
Brian Osman16c81a12017-12-20 11:58:34 -05001513 }
1514 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1515
Hal Canaryb1f411a2019-08-29 10:39:22 -04001516 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001517 fWindow->inval();
1518 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001519 return true;
1520}
1521
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001522bool Viewer::onFling(skui::InputState state) {
1523 if (skui::InputState::kRight == state) {
1524 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1525 return true;
1526 } else if (skui::InputState::kLeft == state) {
1527 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1528 return true;
1529 }
1530 return false;
1531}
1532
1533bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1534 switch (state) {
1535 case skui::InputState::kDown:
1536 fGesture.startZoom();
1537 return true;
1538 break;
1539 case skui::InputState::kMove:
1540 fGesture.updateZoom(scale, x, y, x, y);
1541 return true;
1542 break;
1543 case skui::InputState::kUp:
1544 fGesture.endZoom();
1545 return true;
1546 break;
1547 default:
1548 SkASSERT(false);
1549 break;
1550 }
1551
1552 return false;
1553}
1554
Brian Osmana109e392017-02-24 09:49:14 -05001555static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001556 // The gamut image covers a (0.8 x 0.9) shaped region
1557 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001558
1559 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1560 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1561 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001562 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1563 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1564 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001565
Brian Osman535c5e32019-02-09 16:32:58 -05001566 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1567 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1568 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1569 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1570 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001571}
1572
Ben Wagner3627d2e2018-06-26 14:23:20 -04001573static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001574 ImGui::DragCanvas dc(pt);
1575 dc.fillColor(IM_COL32(0, 0, 0, 128));
1576 dc.dragPoint(pt);
1577 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001578}
1579
Brian Osman9bb47cf2018-04-26 15:55:00 -04001580static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001581 ImGui::DragCanvas dc(pts);
1582 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001583
Brian Osman535c5e32019-02-09 16:32:58 -05001584 for (int i = 0; i < 4; ++i) {
1585 dc.dragPoint(pts + i);
1586 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001587
Brian Osman535c5e32019-02-09 16:32:58 -05001588 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1589 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1590 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1591 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001592
Brian Osman535c5e32019-02-09 16:32:58 -05001593 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001594}
1595
Brian Osmand67e5182017-12-08 16:46:09 -05001596void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001597 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1598 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001599 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001600 }
1601
1602 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001603 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1604 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001605 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001606 DisplayParams params = fWindow->getRequestedDisplayParams();
1607 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001608 const GrContext* ctx = fWindow->getGrContext();
1609
Brian Osmana109e392017-02-24 09:49:14 -05001610 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1611 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001612 if (ImGui::CollapsingHeader("Backend")) {
1613 int newBackend = static_cast<int>(fBackendType);
1614 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1615 ImGui::SameLine();
1616 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001617#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1618 ImGui::SameLine();
1619 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1620#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001621#if defined(SK_DAWN)
1622 ImGui::SameLine();
1623 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1624#endif
Brian Osman621491e2017-02-28 15:45:01 -05001625#if defined(SK_VULKAN)
1626 ImGui::SameLine();
1627 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1628#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001629#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001630 ImGui::SameLine();
1631 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1632#endif
Brian Osman621491e2017-02-28 15:45:01 -05001633 if (newBackend != fBackendType) {
1634 fDeferredActions.push_back([=]() {
1635 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1636 });
1637 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001638
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001639 bool* wire = &params.fGrContextOptions.fWireframeMode;
1640 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1641 paramsChanged = true;
1642 }
Brian Salomon99a33902017-03-07 15:16:34 -05001643
Brian Osman28b12522017-03-08 17:10:24 -05001644 if (ctx) {
1645 int sampleCount = fWindow->sampleCount();
1646 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001647 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001648 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1649 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1650 ImGui::RadioButton("16", &sampleCount, 16);
1651
1652 if (sampleCount != params.fMSAASampleCount) {
1653 params.fMSAASampleCount = sampleCount;
1654 paramsChanged = true;
1655 }
1656 }
1657
Ben Wagner37c54032018-04-13 14:30:23 -04001658 int pixelGeometryIdx = 0;
1659 if (fPixelGeometryOverrides) {
1660 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1661 }
1662 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1663 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1664 {
1665 uint32_t flags = params.fSurfaceProps.flags();
1666 if (pixelGeometryIdx == 0) {
1667 fPixelGeometryOverrides = false;
1668 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1669 } else {
1670 fPixelGeometryOverrides = true;
1671 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1672 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1673 }
1674 paramsChanged = true;
1675 }
1676
1677 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1678 if (ImGui::Checkbox("DFT", &useDFT)) {
1679 uint32_t flags = params.fSurfaceProps.flags();
1680 if (useDFT) {
1681 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1682 } else {
1683 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1684 }
1685 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1686 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1687 paramsChanged = true;
1688 }
1689
Brian Osman8a9de3d2017-03-01 14:59:05 -05001690 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001691 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001692 auto prButton = [&](GpuPathRenderers x) {
1693 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001694 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1695 params.fGrContextOptions.fGpuPathRenderers = x;
1696 paramsChanged = true;
1697 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001698 }
1699 };
1700
1701 if (!ctx) {
1702 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001703 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001704 const auto* caps = ctx->priv().caps();
1705 prButton(GpuPathRenderers::kDefault);
1706 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001707 if (caps->shaderCaps()->tessellationSupport()) {
1708 prButton(GpuPathRenderers::kGpuTessellation);
1709 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001710 if (caps->shaderCaps()->pathRenderingSupport()) {
1711 prButton(GpuPathRenderers::kStencilAndCover);
1712 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001713 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001714 if (1 == fWindow->sampleCount()) {
1715 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1716 prButton(GpuPathRenderers::kCoverageCounting);
1717 }
1718 prButton(GpuPathRenderers::kSmall);
1719 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001720 prButton(GpuPathRenderers::kTessellating);
1721 prButton(GpuPathRenderers::kNone);
1722 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001723 ImGui::TreePop();
1724 }
Brian Osman621491e2017-02-28 15:45:01 -05001725 }
1726
Ben Wagner964571d2019-03-08 12:35:06 -05001727 if (ImGui::CollapsingHeader("Tiling")) {
1728 ImGui::Checkbox("Enable", &fTiled);
1729 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1730 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1731 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1732 }
1733
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001734 if (ImGui::CollapsingHeader("Transform")) {
1735 float zoom = fZoomLevel;
1736 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1737 fZoomLevel = zoom;
1738 this->preTouchMatrixChanged();
1739 paramsChanged = true;
1740 }
1741 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001742 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001743 fRotation = deg;
1744 this->preTouchMatrixChanged();
1745 paramsChanged = true;
1746 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001747 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1748 if (ImGui_DragLocation(&fOffset)) {
1749 this->preTouchMatrixChanged();
1750 paramsChanged = true;
1751 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001752 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1753 this->preTouchMatrixChanged();
1754 paramsChanged = true;
1755 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001756 }
Brian Osman805a7272018-05-02 15:40:20 -04001757 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1758 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1759 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001760 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001761 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001762 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001763 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001764 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001765 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001766 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001767 }
1768
Ben Wagnera580fb32018-04-17 11:16:32 -04001769 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001770 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001771 if (fPaintOverrides.fAntiAlias) {
1772 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001773 }
1774 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001775 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001776 {
1777 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1778 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001779 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001780 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1781 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001782 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001783 fPaintOverrides.fAntiAlias = true;
1784 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001785 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001786 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001787 case SkPaintFields::AntiAliasState::Alias:
1788 break;
1789 case SkPaintFields::AntiAliasState::Normal:
1790 break;
1791 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1792 gSkUseAnalyticAA = true;
1793 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001794 break;
1795 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1796 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001797 break;
1798 }
1799 }
1800 paramsChanged = true;
1801 }
1802
Ben Wagner99a78dc2018-05-09 18:23:51 -04001803 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001804 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001805 bool (SkPaint::* isFlag)() const,
1806 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001807 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001808 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001809 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001810 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001811 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001812 if (ImGui::Combo(label, &itemIndex, items)) {
1813 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001814 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001815 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001816 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001817 (fPaint.*setFlag)(itemIndex == 2);
1818 }
1819 paramsChanged = true;
1820 }
1821 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001822
Ben Wagner99a78dc2018-05-09 18:23:51 -04001823 paintFlag("Dither",
1824 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001825 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001826 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001827
1828 int filterQualityIdx = 0;
1829 if (fPaintOverrides.fFilterQuality) {
1830 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1831 }
1832 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1833 "Default\0None\0Low\0Medium\0High\0\0"))
1834 {
1835 if (filterQualityIdx == 0) {
1836 fPaintOverrides.fFilterQuality = false;
1837 fPaint.setFilterQuality(kNone_SkFilterQuality);
1838 } else {
1839 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1840 fPaintOverrides.fFilterQuality = true;
1841 }
1842 paramsChanged = true;
1843 }
Ben Wagner9613e452019-01-23 10:34:59 -05001844 }
Hal Canary02738a82019-01-21 18:51:32 +00001845
Ben Wagner9613e452019-01-23 10:34:59 -05001846 if (ImGui::CollapsingHeader("Font")) {
1847 int hintingIdx = 0;
1848 if (fFontOverrides.fHinting) {
1849 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1850 }
1851 if (ImGui::Combo("Hinting", &hintingIdx,
1852 "Default\0None\0Slight\0Normal\0Full\0\0"))
1853 {
1854 if (hintingIdx == 0) {
1855 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001856 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001857 } else {
1858 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1859 fFontOverrides.fHinting = true;
1860 }
1861 paramsChanged = true;
1862 }
Hal Canary02738a82019-01-21 18:51:32 +00001863
Ben Wagner9613e452019-01-23 10:34:59 -05001864 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1865 bool SkFontFields::* flag,
1866 bool (SkFont::* isFlag)() const,
1867 void (SkFont::* setFlag)(bool) )
1868 {
1869 int itemIndex = 0;
1870 if (fFontOverrides.*flag) {
1871 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1872 }
1873 if (ImGui::Combo(label, &itemIndex, items)) {
1874 if (itemIndex == 0) {
1875 fFontOverrides.*flag = false;
1876 } else {
1877 fFontOverrides.*flag = true;
1878 (fFont.*setFlag)(itemIndex == 2);
1879 }
1880 paramsChanged = true;
1881 }
1882 };
Hal Canary02738a82019-01-21 18:51:32 +00001883
Ben Wagner9613e452019-01-23 10:34:59 -05001884 fontFlag("Fake Bold Glyphs",
1885 "Default\0No Fake Bold\0Fake Bold\0\0",
1886 &SkFontFields::fEmbolden,
1887 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001888
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001889 fontFlag("Baseline Snapping",
1890 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1891 &SkFontFields::fBaselineSnap,
1892 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1893
Ben Wagner9613e452019-01-23 10:34:59 -05001894 fontFlag("Linear Text",
1895 "Default\0No Linear Text\0Linear Text\0\0",
1896 &SkFontFields::fLinearMetrics,
1897 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001898
Ben Wagner9613e452019-01-23 10:34:59 -05001899 fontFlag("Subpixel Position Glyphs",
1900 "Default\0Pixel Text\0Subpixel Text\0\0",
1901 &SkFontFields::fSubpixel,
1902 &SkFont::isSubpixel, &SkFont::setSubpixel);
1903
1904 fontFlag("Embedded Bitmap Text",
1905 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1906 &SkFontFields::fEmbeddedBitmaps,
1907 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1908
1909 fontFlag("Force Auto-Hinting",
1910 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1911 &SkFontFields::fForceAutoHinting,
1912 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1913
1914 int edgingIdx = 0;
1915 if (fFontOverrides.fEdging) {
1916 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1917 }
1918 if (ImGui::Combo("Edging", &edgingIdx,
1919 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1920 {
1921 if (edgingIdx == 0) {
1922 fFontOverrides.fEdging = false;
1923 fFont.setEdging(SkFont::Edging::kAlias);
1924 } else {
1925 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1926 fFontOverrides.fEdging = true;
1927 }
1928 paramsChanged = true;
1929 }
1930
Ben Wagner15a8d572019-03-21 13:35:44 -04001931 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1932 if (fFontOverrides.fSize) {
1933 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001934 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001935 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001936 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001937 fFontOverrides.fSizeRange[0],
1938 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001939 "%.6f", 2.0f))
1940 {
Mike Reed3ae47332019-01-04 10:11:46 -05001941 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001942 paramsChanged = true;
1943 }
1944 }
1945
1946 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1947 if (fFontOverrides.fScaleX) {
1948 float scaleX = fFont.getScaleX();
1949 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1950 fFont.setScaleX(scaleX);
1951 paramsChanged = true;
1952 }
1953 }
1954
1955 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1956 if (fFontOverrides.fSkewX) {
1957 float skewX = fFont.getSkewX();
1958 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1959 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001960 paramsChanged = true;
1961 }
1962 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001963 }
1964
Mike Reed81f60ec2018-05-15 10:09:52 -04001965 {
1966 SkMetaData controls;
1967 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1968 if (ImGui::CollapsingHeader("Current Slide")) {
1969 SkMetaData::Iter iter(controls);
1970 const char* name;
1971 SkMetaData::Type type;
1972 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001973 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001974 if (type == SkMetaData::kScalar_Type) {
1975 float val[3];
1976 SkASSERT(count == 3);
1977 controls.findScalars(name, &count, val);
1978 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1979 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001980 }
Ben Wagner110c7032019-03-22 17:03:59 -04001981 } else if (type == SkMetaData::kBool_Type) {
1982 bool val;
1983 SkASSERT(count == 1);
1984 controls.findBool(name, &val);
1985 if (ImGui::Checkbox(name, &val)) {
1986 controls.setBool(name, val);
1987 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001988 }
1989 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001990 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001991 }
1992 }
1993 }
1994
Ben Wagner7a3c6742018-04-23 10:01:07 -04001995 if (fShowSlidePicker) {
1996 ImGui::SetNextTreeNodeOpen(true);
1997 }
Brian Osman79086b92017-02-10 13:36:16 -05001998 if (ImGui::CollapsingHeader("Slide")) {
1999 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002000 static ImVector<const char*> filteredSlideNames;
2001 static ImVector<int> filteredSlideIndices;
2002
Brian Osmanfce09c52017-11-14 15:32:20 -05002003 if (fShowSlidePicker) {
2004 ImGui::SetKeyboardFocusHere();
2005 fShowSlidePicker = false;
2006 }
2007
Brian Osman79086b92017-02-10 13:36:16 -05002008 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002009 filteredSlideNames.clear();
2010 filteredSlideIndices.clear();
2011 int filteredIndex = 0;
2012 for (int i = 0; i < fSlides.count(); ++i) {
2013 const char* slideName = fSlides[i]->getName().c_str();
2014 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2015 if (i == fCurrentSlide) {
2016 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002017 }
Brian Osmanf479e422017-11-08 13:11:36 -05002018 filteredSlideNames.push_back(slideName);
2019 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002020 }
Brian Osman79086b92017-02-10 13:36:16 -05002021 }
Brian Osmanf479e422017-11-08 13:11:36 -05002022
Brian Osmanf479e422017-11-08 13:11:36 -05002023 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2024 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002025 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002026 }
2027 }
Brian Osmana109e392017-02-24 09:49:14 -05002028
2029 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002030 ColorMode newMode = fColorMode;
2031 auto cmButton = [&](ColorMode mode, const char* label) {
2032 if (ImGui::RadioButton(label, mode == fColorMode)) {
2033 newMode = mode;
2034 }
2035 };
2036
2037 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002038 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2039 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002040 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002041
2042 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002043 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002044 }
2045
2046 // Pick from common gamuts:
2047 int primariesIdx = 4; // Default: Custom
2048 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2049 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2050 primariesIdx = i;
2051 break;
2052 }
2053 }
2054
Brian Osman03115dc2018-11-26 13:55:19 -05002055 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002056 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002057
Brian Osmana109e392017-02-24 09:49:14 -05002058 if (ImGui::Combo("Primaries", &primariesIdx,
2059 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2060 if (primariesIdx >= 0 && primariesIdx <= 3) {
2061 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2062 }
2063 }
2064
2065 // Allow direct editing of gamut
2066 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2067 }
Brian Osman207d4102019-01-10 09:40:58 -05002068
2069 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002070 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002071 if (ImGui::Checkbox("Pause", &isPaused)) {
2072 fAnimTimer.togglePauseResume();
2073 }
Brian Osman707d2022019-01-10 11:27:34 -05002074
2075 float speed = fAnimTimer.getSpeed();
2076 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2077 fAnimTimer.setSpeed(speed);
2078 }
Brian Osman207d4102019-01-10 09:40:58 -05002079 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002080
Brian Osmanfd7657c2019-04-25 11:34:07 -04002081 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2082#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2083 || Window::kANGLE_BackendType == fBackendType
2084#endif
2085 ;
2086
2087 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2088 // than GL, we need to force it on. Just do that on the first frame after the backend
2089 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002090 if (!backendIsGL &&
2091 params.fGrContextOptions.fShaderCacheStrategy !=
2092 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2093 params.fGrContextOptions.fShaderCacheStrategy =
2094 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002095 paramsChanged = true;
2096 fPersistentCache.reset();
2097 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002098 // To re-load shaders from the currently active programs, we flush all caches on one
2099 // frame, then set a flag to poll the cache on the next frame.
2100 static bool gLoadPending = false;
2101 if (gLoadPending) {
2102 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2103 int hitCount) {
2104 CachedGLSL& entry(fCachedGLSL.push_back());
2105 entry.fKey = key;
2106 SkMD5 hash;
2107 hash.write(key->bytes(), key->size());
2108 SkMD5::Digest digest = hash.finish();
2109 for (int i = 0; i < 16; ++i) {
2110 entry.fKeyString.appendf("%02x", digest.data[i]);
2111 }
2112
Brian Osmana66081d2019-09-03 14:59:26 -04002113 SkReader32 reader(data->data(), data->size());
2114 entry.fShaderType = reader.readU32();
2115 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2116 entry.fInputs,
2117 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002118 };
2119 fCachedGLSL.reset();
2120 fPersistentCache.foreach(collectShaders);
2121 gLoadPending = false;
2122 }
2123
2124 // Defer actually doing the load/save logic so that we can trigger a save when we
2125 // start or finish hovering on a tree node in the list below:
2126 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002127 bool doSave = ImGui::Button("Save");
2128 if (backendIsGL) {
2129 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002130 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2131 GrContextOptions::ShaderCacheStrategy::kSkSL;
2132 if (ImGui::Checkbox("SkSL", &sksl)) {
2133 params.fGrContextOptions.fShaderCacheStrategy = sksl
2134 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2135 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002136 paramsChanged = true;
2137 doLoad = true;
2138 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2139 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002140 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002141
2142 ImGui::BeginChild("##ScrollingRegion");
2143 for (auto& entry : fCachedGLSL) {
2144 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2145 bool hovered = ImGui::IsItemHovered();
2146 if (hovered != entry.fHovered) {
2147 // Force a save to patch the highlight shader in/out
2148 entry.fHovered = hovered;
2149 doSave = true;
2150 }
2151 if (inTreeNode) {
2152 // Full width, and a reasonable amount of space for each shader.
2153 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2154 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2155 boxSize);
2156 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2157 boxSize);
2158 ImGui::TreePop();
2159 }
2160 }
2161 ImGui::EndChild();
2162
2163 if (doLoad) {
2164 fPersistentCache.reset();
2165 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2166 gLoadPending = true;
2167 }
2168 if (doSave) {
2169 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002170 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002171 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2172 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002173
Brian Osman072e6fc2019-06-12 11:35:41 -04002174 SkSL::String highlight;
2175 if (!sksl) {
2176 highlight = shaderCaps->versionDeclString();
2177 if (shaderCaps->usesPrecisionModifiers()) {
2178 highlight.append("precision mediump float;\n");
2179 }
Brian Osman5bee3902019-05-07 09:55:45 -04002180 }
2181 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002182 highlight.appendf("out %s sk_FragColor;\n"
2183 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2184 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002185
2186 fPersistentCache.reset();
2187 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2188 for (auto& entry : fCachedGLSL) {
2189 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2190 if (entry.fHovered) {
2191 entry.fShader[kFragment_GrShaderType] = highlight;
2192 }
2193
Brian Osmana085a412019-04-25 09:44:43 -04002194 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2195 entry.fShader,
2196 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002197 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002198 fPersistentCache.store(*entry.fKey, *data);
2199
2200 entry.fShader[kFragment_GrShaderType] = backup;
2201 }
2202 }
2203 }
Brian Osman79086b92017-02-10 13:36:16 -05002204 }
Brian Salomon99a33902017-03-07 15:16:34 -05002205 if (paramsChanged) {
2206 fDeferredActions.push_back([=]() {
2207 fWindow->setRequestedDisplayParams(params);
2208 fWindow->inval();
2209 this->updateTitle();
2210 });
2211 }
Brian Osman79086b92017-02-10 13:36:16 -05002212 ImGui::End();
2213 }
2214
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002215 if (gShaderErrorHandler.fErrors.count()) {
2216 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2217 ImGui::Begin("Shader Errors");
2218 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2219 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002220 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2221 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2222 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2223 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002224 }
2225 ImGui::End();
2226 gShaderErrorHandler.reset();
2227 }
2228
Brian Osmanf6877092017-02-13 09:39:57 -05002229 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002230 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2231 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002232 static int zoomFactor = 8;
2233 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002234 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002235 }
2236 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2237 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002238 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002239 }
Brian Osmanf6877092017-02-13 09:39:57 -05002240
Ben Wagner3627d2e2018-06-26 14:23:20 -04002241 if (!fZoomWindowFixed) {
2242 ImVec2 mousePos = ImGui::GetMousePos();
2243 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2244 }
2245 SkScalar x = fZoomWindowLocation.x();
2246 SkScalar y = fZoomWindowLocation.y();
2247 int xInt = SkScalarRoundToInt(x);
2248 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002249 ImVec2 avail = ImGui::GetContentRegionAvail();
2250
Brian Osmanead517d2017-11-13 15:36:36 -05002251 uint32_t pixel = 0;
2252 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002253 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002254 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002255 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002256 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002257 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002258 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2259 }
2260
Brian Osmand67e5182017-12-08 16:46:09 -05002261 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002262 // Translate so the region of the image that's under the mouse cursor is centered
2263 // in the zoom canvas:
2264 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002265 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2266 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002267 c->drawImage(this->fLastImage, 0, 0);
2268
2269 SkPaint outline;
2270 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002271 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002272 });
Brian Osmanf6877092017-02-13 09:39:57 -05002273 }
2274
2275 ImGui::End();
2276 }
Brian Osman79086b92017-02-10 13:36:16 -05002277}
2278
liyuqian2edb0f42016-07-06 14:11:32 -07002279void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002280 for (int i = 0; i < fDeferredActions.count(); ++i) {
2281 fDeferredActions[i]();
2282 }
2283 fDeferredActions.reset();
2284
Brian Osman56a24812017-12-19 11:15:16 -05002285 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002286 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002287 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002288 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002289
Brian Osman79086b92017-02-10 13:36:16 -05002290 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002291 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2292 // not be visible, though. So we need to redraw if there is at least one visible window, or
2293 // more than one active window. Newly created windows are active but not visible for one frame
2294 // while they determine their layout and sizing.
2295 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2296 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002297 fWindow->inval();
2298 }
jvanverth9f372462016-04-06 06:08:59 -07002299}
liyuqiane5a6cd92016-05-27 08:52:52 -07002300
Florin Malitab632df72018-06-18 21:23:06 -04002301template <typename OptionsFunc>
2302static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2303 OptionsFunc&& optionsFunc) {
2304 writer.beginObject();
2305 {
2306 writer.appendString(kName , name);
2307 writer.appendString(kValue, value);
2308
2309 writer.beginArray(kOptions);
2310 {
2311 optionsFunc(writer);
2312 }
2313 writer.endArray();
2314 }
2315 writer.endObject();
2316}
2317
2318
liyuqiane5a6cd92016-05-27 08:52:52 -07002319void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002320 if (!fWindow) {
2321 return;
2322 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002323 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002324 return; // Surface hasn't been created yet.
2325 }
2326
Florin Malitab632df72018-06-18 21:23:06 -04002327 SkDynamicMemoryWStream memStream;
2328 SkJSONWriter writer(&memStream);
2329 writer.beginArray();
2330
liyuqianb73c24b2016-06-03 08:47:23 -07002331 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002332 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2333 [this](SkJSONWriter& writer) {
2334 for(const auto& slide : fSlides) {
2335 writer.appendString(slide->getName().c_str());
2336 }
2337 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002338
liyuqianb73c24b2016-06-03 08:47:23 -07002339 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002340 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2341 [](SkJSONWriter& writer) {
2342 for (const auto& str : kBackendTypeStrings) {
2343 writer.appendString(str);
2344 }
2345 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002346
csmartdalton578f0642017-02-24 16:04:47 -07002347 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002348 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2349 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2350 [this](SkJSONWriter& writer) {
2351 writer.appendS32(0);
2352
2353 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2354 return;
2355 }
2356
2357 for (int msaa : {4, 8, 16}) {
2358 writer.appendS32(msaa);
2359 }
2360 });
csmartdalton578f0642017-02-24 16:04:47 -07002361
csmartdalton61cd31a2017-02-27 17:00:53 -07002362 // Path renderer state
2363 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002364 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2365 [this](SkJSONWriter& writer) {
2366 const GrContext* ctx = fWindow->getGrContext();
2367 if (!ctx) {
2368 writer.appendString("Software");
2369 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002370 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002371 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2372 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002373 if (caps->shaderCaps()->tessellationSupport()) {
2374 writer.appendString(
2375 gPathRendererNames[GpuPathRenderers::kGpuTessellation].c_str());
2376 }
Florin Malitab632df72018-06-18 21:23:06 -04002377 if (caps->shaderCaps()->pathRenderingSupport()) {
2378 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002379 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002380 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002381 }
2382 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002383 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2384 writer.appendString(
2385 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2386 }
2387 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2388 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002389 writer.appendString(gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2390 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002391 }
2392 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002393
liyuqianb73c24b2016-06-03 08:47:23 -07002394 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002395 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2396 [this](SkJSONWriter& writer) {
2397 writer.appendString(kSoftkeyHint);
2398 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2399 writer.appendString(softkey.c_str());
2400 }
2401 });
liyuqianb73c24b2016-06-03 08:47:23 -07002402
Florin Malitab632df72018-06-18 21:23:06 -04002403 writer.endArray();
2404 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002405
Florin Malitab632df72018-06-18 21:23:06 -04002406 auto data = memStream.detachAsData();
2407
2408 // TODO: would be cool to avoid this copy
2409 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2410
2411 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002412}
2413
2414void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002415 // For those who will add more features to handle the state change in this function:
2416 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2417 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2418 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002419 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002420 for (int i = 0; i < fSlides.count(); ++i) {
2421 if (fSlides[i]->getName().equals(stateValue)) {
2422 this->setCurrentSlide(i);
2423 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002424 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002425 }
Florin Malitaab99c342018-01-16 16:23:03 -05002426
2427 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002428 } else if (stateName.equals(kBackendStateName)) {
2429 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2430 if (stateValue.equals(kBackendTypeStrings[i])) {
2431 if (fBackendType != i) {
2432 fBackendType = (sk_app::Window::BackendType)i;
2433 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002434 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002435 }
2436 break;
2437 }
2438 }
csmartdalton578f0642017-02-24 16:04:47 -07002439 } else if (stateName.equals(kMSAAStateName)) {
2440 DisplayParams params = fWindow->getRequestedDisplayParams();
2441 int sampleCount = atoi(stateValue.c_str());
2442 if (sampleCount != params.fMSAASampleCount) {
2443 params.fMSAASampleCount = sampleCount;
2444 fWindow->setRequestedDisplayParams(params);
2445 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002446 this->updateTitle();
2447 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002448 }
2449 } else if (stateName.equals(kPathRendererStateName)) {
2450 DisplayParams params = fWindow->getRequestedDisplayParams();
2451 for (const auto& pair : gPathRendererNames) {
2452 if (pair.second == stateValue.c_str()) {
2453 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2454 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2455 fWindow->setRequestedDisplayParams(params);
2456 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002457 this->updateTitle();
2458 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002459 }
2460 break;
2461 }
csmartdalton578f0642017-02-24 16:04:47 -07002462 }
liyuqianb73c24b2016-06-03 08:47:23 -07002463 } else if (stateName.equals(kSoftkeyStateName)) {
2464 if (!stateValue.equals(kSoftkeyHint)) {
2465 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002466 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002467 }
liyuqian2edb0f42016-07-06 14:11:32 -07002468 } else if (stateName.equals(kRefreshStateName)) {
2469 // This state is actually NOT in the UI state.
2470 // We use this to allow Android to quickly set bool fRefresh.
2471 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002472 } else {
2473 SkDebugf("Unknown stateName: %s", stateName.c_str());
2474 }
2475}
Brian Osman79086b92017-02-10 13:36:16 -05002476
Hal Canaryb1f411a2019-08-29 10:39:22 -04002477bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002478 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002479}
2480
Hal Canaryb1f411a2019-08-29 10:39:22 -04002481bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002482 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002483 fWindow->inval();
2484 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002485 } else {
2486 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002487 }
Brian Osman79086b92017-02-10 13:36:16 -05002488}