blob: a964555fe1187c292e112931592c1e76e88c0b73 [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
1039#define MAX_ZOOM_LEVEL 8
1040#define MIN_ZOOM_LEVEL -8
1041
jvanverth34524262016-05-04 13:49:13 -07001042void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001043 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -04001044 fZoomLevel = SkScalarPin(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());
1339 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1340 SkCanvas* tileCanvas = tileSurface->getCanvas();
1341 SkMatrix m = this->computeMatrix();
1342 for (int y = 0; y < fWindow->height(); y += tileH) {
1343 for (int x = 0; x < fWindow->width(); x += tileW) {
1344 SkAutoCanvasRestore acr(tileCanvas, true);
1345 tileCanvas->translate(-x, -y);
1346 tileCanvas->clear(SK_ColorTRANSPARENT);
1347 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001348 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1349 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001350 fSlides[fCurrentSlide]->draw(&filterCanvas);
1351 tileSurface->draw(slideCanvas, x, y, nullptr);
1352 }
1353 }
1354
1355 // Draw borders between tiles
1356 if (fDrawTileBoundaries) {
1357 SkPaint border;
1358 border.setColor(0x60FF00FF);
1359 border.setStyle(SkPaint::kStroke_Style);
1360 for (int y = 0; y < fWindow->height(); y += tileH) {
1361 for (int x = 0; x < fWindow->width(); x += tileW) {
1362 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1363 }
1364 }
1365 }
1366 } else {
1367 slideCanvas->concat(this->computeMatrix());
1368 if (kPerspective_Real == fPerspectiveMode) {
1369 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1370 }
Mike Reed3ae47332019-01-04 10:11:46 -05001371 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001372 fSlides[fCurrentSlide]->draw(&filterCanvas);
1373 }
Brian Osman56a24812017-12-19 11:15:16 -05001374 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001375 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001376
1377 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001378 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001379 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001380 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001381
1382 // If we rendered offscreen, snap an image and push the results to the window's canvas
1383 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001384 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001385
Robert Phillips9882dae2019-03-04 11:00:10 -05001386 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001387 SkPaint paint;
1388 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001389 int prePerspectiveCount = canvas->save();
1390 if (kPerspective_Fake == fPerspectiveMode) {
1391 paint.setFilterQuality(kHigh_SkFilterQuality);
1392 canvas->clear(SK_ColorWHITE);
1393 canvas->concat(this->computePerspectiveMatrix());
1394 }
Brian Osman03115dc2018-11-26 13:55:19 -05001395 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001396 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001397 }
Mike Reed376d8122019-03-14 11:39:02 -04001398
1399 if (fShowSlideDimensions) {
1400 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1401 SkPaint paint;
1402 paint.setColor(0x40FFFF00);
1403 surface->getCanvas()->drawRect(r, paint);
1404 }
liyuqian6f163d22016-06-13 12:26:45 -07001405}
1406
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001407void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001408 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001409 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001410}
Jim Van Verth6f449692017-02-14 15:16:46 -05001411
Robert Phillips9882dae2019-03-04 11:00:10 -05001412void Viewer::onPaint(SkSurface* surface) {
1413 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001414
Robert Phillips9882dae2019-03-04 11:00:10 -05001415 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001416
Brian Osmand67e5182017-12-08 16:46:09 -05001417 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001418
1419 if (GrContext* ctx = fWindow->getGrContext()) {
1420 // Clean out cache items that haven't been used in more than 10 seconds.
1421 ctx->performDeferredCleanup(std::chrono::seconds(10));
1422 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001423}
1424
Ben Wagnera1915972018-08-09 15:06:19 -04001425void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001426 if (fCurrentSlide >= 0) {
1427 fSlides[fCurrentSlide]->resize(width, height);
1428 }
Ben Wagnera1915972018-08-09 15:06:19 -04001429}
1430
Florin Malitacefc1b92018-02-19 21:43:47 -05001431SkPoint Viewer::mapEvent(float x, float y) {
1432 const auto m = this->computeMatrix();
1433 SkMatrix inv;
1434
1435 SkAssertResult(m.invert(&inv));
1436
1437 return inv.mapXY(x, y);
1438}
1439
Hal Canaryb1f411a2019-08-29 10:39:22 -04001440bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001441 if (GestureDevice::kMouse == fGestureDevice) {
1442 return false;
1443 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001444
1445 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001446 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001447 fWindow->inval();
1448 return true;
1449 }
1450
liyuqiand3cdbca2016-05-17 12:44:20 -07001451 void* castedOwner = reinterpret_cast<void*>(owner);
1452 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001453 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001454 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001455#if defined(SK_BUILD_FOR_IOS)
1456 // TODO: move IOS swipe detection higher up into the platform code
1457 SkPoint dir;
1458 if (fGesture.isFling(&dir)) {
1459 // swiping left or right
1460 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1461 if (dir.fX < 0) {
1462 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1463 fCurrentSlide + 1 : 0);
1464 } else {
1465 this->setCurrentSlide(fCurrentSlide > 0 ?
1466 fCurrentSlide - 1 : fSlides.count() - 1);
1467 }
1468 }
1469 fGesture.reset();
1470 }
1471#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001472 break;
1473 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001474 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001475 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001476 break;
1477 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001478 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001479 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001480 break;
1481 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001482 default: {
1483 // kLeft and kRight are only for swipes
1484 SkASSERT(false);
1485 break;
1486 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001487 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001488 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001489 fWindow->inval();
1490 return true;
1491}
1492
Hal Canaryb1f411a2019-08-29 10:39:22 -04001493bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001494 if (GestureDevice::kTouch == fGestureDevice) {
1495 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001496 }
Brian Osman16c81a12017-12-20 11:58:34 -05001497
Florin Malitacefc1b92018-02-19 21:43:47 -05001498 const auto slidePt = this->mapEvent(x, y);
1499 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1500 fWindow->inval();
1501 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001502 }
1503
1504 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001505 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001506 fGesture.touchEnd(nullptr);
1507 break;
1508 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001509 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001510 fGesture.touchBegin(nullptr, x, y);
1511 break;
1512 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001513 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001514 fGesture.touchMoved(nullptr, x, y);
1515 break;
1516 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001517 default: {
1518 SkASSERT(false); // shouldn't see kRight or kLeft here
1519 break;
1520 }
Brian Osman16c81a12017-12-20 11:58:34 -05001521 }
1522 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1523
Hal Canaryb1f411a2019-08-29 10:39:22 -04001524 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001525 fWindow->inval();
1526 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001527 return true;
1528}
1529
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001530bool Viewer::onFling(skui::InputState state) {
1531 if (skui::InputState::kRight == state) {
1532 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1533 return true;
1534 } else if (skui::InputState::kLeft == state) {
1535 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1536 return true;
1537 }
1538 return false;
1539}
1540
1541bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1542 switch (state) {
1543 case skui::InputState::kDown:
1544 fGesture.startZoom();
1545 return true;
1546 break;
1547 case skui::InputState::kMove:
1548 fGesture.updateZoom(scale, x, y, x, y);
1549 return true;
1550 break;
1551 case skui::InputState::kUp:
1552 fGesture.endZoom();
1553 return true;
1554 break;
1555 default:
1556 SkASSERT(false);
1557 break;
1558 }
1559
1560 return false;
1561}
1562
Brian Osmana109e392017-02-24 09:49:14 -05001563static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001564 // The gamut image covers a (0.8 x 0.9) shaped region
1565 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001566
1567 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1568 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1569 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001570 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1571 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1572 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001573
Brian Osman535c5e32019-02-09 16:32:58 -05001574 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1575 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1576 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1577 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1578 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001579}
1580
Ben Wagner3627d2e2018-06-26 14:23:20 -04001581static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001582 ImGui::DragCanvas dc(pt);
1583 dc.fillColor(IM_COL32(0, 0, 0, 128));
1584 dc.dragPoint(pt);
1585 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001586}
1587
Brian Osman9bb47cf2018-04-26 15:55:00 -04001588static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001589 ImGui::DragCanvas dc(pts);
1590 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001591
Brian Osman535c5e32019-02-09 16:32:58 -05001592 for (int i = 0; i < 4; ++i) {
1593 dc.dragPoint(pts + i);
1594 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001595
Brian Osman535c5e32019-02-09 16:32:58 -05001596 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1597 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1598 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1599 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001600
Brian Osman535c5e32019-02-09 16:32:58 -05001601 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001602}
1603
Brian Osmand67e5182017-12-08 16:46:09 -05001604void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001605 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1606 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001607 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001608 }
1609
1610 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001611 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1612 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001613 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001614 DisplayParams params = fWindow->getRequestedDisplayParams();
1615 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001616 const GrContext* ctx = fWindow->getGrContext();
1617
Brian Osmana109e392017-02-24 09:49:14 -05001618 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1619 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001620 if (ImGui::CollapsingHeader("Backend")) {
1621 int newBackend = static_cast<int>(fBackendType);
1622 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1623 ImGui::SameLine();
1624 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001625#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1626 ImGui::SameLine();
1627 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1628#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001629#if defined(SK_DAWN)
1630 ImGui::SameLine();
1631 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1632#endif
Brian Osman621491e2017-02-28 15:45:01 -05001633#if defined(SK_VULKAN)
1634 ImGui::SameLine();
1635 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1636#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001637#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001638 ImGui::SameLine();
1639 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1640#endif
Brian Osman621491e2017-02-28 15:45:01 -05001641 if (newBackend != fBackendType) {
1642 fDeferredActions.push_back([=]() {
1643 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1644 });
1645 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001646
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001647 bool* wire = &params.fGrContextOptions.fWireframeMode;
1648 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1649 paramsChanged = true;
1650 }
Brian Salomon99a33902017-03-07 15:16:34 -05001651
Brian Osman28b12522017-03-08 17:10:24 -05001652 if (ctx) {
1653 int sampleCount = fWindow->sampleCount();
1654 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001655 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001656 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1657 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1658 ImGui::RadioButton("16", &sampleCount, 16);
1659
1660 if (sampleCount != params.fMSAASampleCount) {
1661 params.fMSAASampleCount = sampleCount;
1662 paramsChanged = true;
1663 }
1664 }
1665
Ben Wagner37c54032018-04-13 14:30:23 -04001666 int pixelGeometryIdx = 0;
1667 if (fPixelGeometryOverrides) {
1668 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1669 }
1670 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1671 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1672 {
1673 uint32_t flags = params.fSurfaceProps.flags();
1674 if (pixelGeometryIdx == 0) {
1675 fPixelGeometryOverrides = false;
1676 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1677 } else {
1678 fPixelGeometryOverrides = true;
1679 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1680 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1681 }
1682 paramsChanged = true;
1683 }
1684
1685 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1686 if (ImGui::Checkbox("DFT", &useDFT)) {
1687 uint32_t flags = params.fSurfaceProps.flags();
1688 if (useDFT) {
1689 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1690 } else {
1691 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1692 }
1693 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1694 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1695 paramsChanged = true;
1696 }
1697
Brian Osman8a9de3d2017-03-01 14:59:05 -05001698 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001699 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001700 auto prButton = [&](GpuPathRenderers x) {
1701 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001702 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1703 params.fGrContextOptions.fGpuPathRenderers = x;
1704 paramsChanged = true;
1705 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001706 }
1707 };
1708
1709 if (!ctx) {
1710 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001711 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001712 const auto* caps = ctx->priv().caps();
1713 prButton(GpuPathRenderers::kDefault);
1714 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001715 if (caps->shaderCaps()->tessellationSupport()) {
1716 prButton(GpuPathRenderers::kGpuTessellation);
1717 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001718 if (caps->shaderCaps()->pathRenderingSupport()) {
1719 prButton(GpuPathRenderers::kStencilAndCover);
1720 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001721 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001722 if (1 == fWindow->sampleCount()) {
1723 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1724 prButton(GpuPathRenderers::kCoverageCounting);
1725 }
1726 prButton(GpuPathRenderers::kSmall);
1727 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001728 prButton(GpuPathRenderers::kTessellating);
1729 prButton(GpuPathRenderers::kNone);
1730 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001731 ImGui::TreePop();
1732 }
Brian Osman621491e2017-02-28 15:45:01 -05001733 }
1734
Ben Wagner964571d2019-03-08 12:35:06 -05001735 if (ImGui::CollapsingHeader("Tiling")) {
1736 ImGui::Checkbox("Enable", &fTiled);
1737 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1738 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1739 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1740 }
1741
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001742 if (ImGui::CollapsingHeader("Transform")) {
1743 float zoom = fZoomLevel;
1744 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1745 fZoomLevel = zoom;
1746 this->preTouchMatrixChanged();
1747 paramsChanged = true;
1748 }
1749 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001750 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001751 fRotation = deg;
1752 this->preTouchMatrixChanged();
1753 paramsChanged = true;
1754 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001755 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1756 if (ImGui_DragLocation(&fOffset)) {
1757 this->preTouchMatrixChanged();
1758 paramsChanged = true;
1759 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001760 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1761 this->preTouchMatrixChanged();
1762 paramsChanged = true;
1763 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001764 }
Brian Osman805a7272018-05-02 15:40:20 -04001765 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1766 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1767 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001768 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001769 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001770 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001771 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001772 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001773 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001774 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001775 }
1776
Ben Wagnera580fb32018-04-17 11:16:32 -04001777 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001778 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001779 if (fPaintOverrides.fAntiAlias) {
1780 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001781 }
1782 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001783 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001784 {
1785 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1786 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001787 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001788 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1789 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001790 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001791 fPaintOverrides.fAntiAlias = true;
1792 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001793 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001794 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001795 case SkPaintFields::AntiAliasState::Alias:
1796 break;
1797 case SkPaintFields::AntiAliasState::Normal:
1798 break;
1799 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1800 gSkUseAnalyticAA = true;
1801 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001802 break;
1803 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1804 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001805 break;
1806 }
1807 }
1808 paramsChanged = true;
1809 }
1810
Ben Wagner99a78dc2018-05-09 18:23:51 -04001811 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001812 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001813 bool (SkPaint::* isFlag)() const,
1814 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001815 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001816 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001817 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001818 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001819 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001820 if (ImGui::Combo(label, &itemIndex, items)) {
1821 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001822 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001823 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001824 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001825 (fPaint.*setFlag)(itemIndex == 2);
1826 }
1827 paramsChanged = true;
1828 }
1829 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001830
Ben Wagner99a78dc2018-05-09 18:23:51 -04001831 paintFlag("Dither",
1832 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001833 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001834 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001835
1836 int filterQualityIdx = 0;
1837 if (fPaintOverrides.fFilterQuality) {
1838 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1839 }
1840 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1841 "Default\0None\0Low\0Medium\0High\0\0"))
1842 {
1843 if (filterQualityIdx == 0) {
1844 fPaintOverrides.fFilterQuality = false;
1845 fPaint.setFilterQuality(kNone_SkFilterQuality);
1846 } else {
1847 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1848 fPaintOverrides.fFilterQuality = true;
1849 }
1850 paramsChanged = true;
1851 }
Ben Wagner9613e452019-01-23 10:34:59 -05001852 }
Hal Canary02738a82019-01-21 18:51:32 +00001853
Ben Wagner9613e452019-01-23 10:34:59 -05001854 if (ImGui::CollapsingHeader("Font")) {
1855 int hintingIdx = 0;
1856 if (fFontOverrides.fHinting) {
1857 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1858 }
1859 if (ImGui::Combo("Hinting", &hintingIdx,
1860 "Default\0None\0Slight\0Normal\0Full\0\0"))
1861 {
1862 if (hintingIdx == 0) {
1863 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001864 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001865 } else {
1866 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1867 fFontOverrides.fHinting = true;
1868 }
1869 paramsChanged = true;
1870 }
Hal Canary02738a82019-01-21 18:51:32 +00001871
Ben Wagner9613e452019-01-23 10:34:59 -05001872 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1873 bool SkFontFields::* flag,
1874 bool (SkFont::* isFlag)() const,
1875 void (SkFont::* setFlag)(bool) )
1876 {
1877 int itemIndex = 0;
1878 if (fFontOverrides.*flag) {
1879 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1880 }
1881 if (ImGui::Combo(label, &itemIndex, items)) {
1882 if (itemIndex == 0) {
1883 fFontOverrides.*flag = false;
1884 } else {
1885 fFontOverrides.*flag = true;
1886 (fFont.*setFlag)(itemIndex == 2);
1887 }
1888 paramsChanged = true;
1889 }
1890 };
Hal Canary02738a82019-01-21 18:51:32 +00001891
Ben Wagner9613e452019-01-23 10:34:59 -05001892 fontFlag("Fake Bold Glyphs",
1893 "Default\0No Fake Bold\0Fake Bold\0\0",
1894 &SkFontFields::fEmbolden,
1895 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001896
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001897 fontFlag("Baseline Snapping",
1898 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1899 &SkFontFields::fBaselineSnap,
1900 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1901
Ben Wagner9613e452019-01-23 10:34:59 -05001902 fontFlag("Linear Text",
1903 "Default\0No Linear Text\0Linear Text\0\0",
1904 &SkFontFields::fLinearMetrics,
1905 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001906
Ben Wagner9613e452019-01-23 10:34:59 -05001907 fontFlag("Subpixel Position Glyphs",
1908 "Default\0Pixel Text\0Subpixel Text\0\0",
1909 &SkFontFields::fSubpixel,
1910 &SkFont::isSubpixel, &SkFont::setSubpixel);
1911
1912 fontFlag("Embedded Bitmap Text",
1913 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1914 &SkFontFields::fEmbeddedBitmaps,
1915 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1916
1917 fontFlag("Force Auto-Hinting",
1918 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1919 &SkFontFields::fForceAutoHinting,
1920 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1921
1922 int edgingIdx = 0;
1923 if (fFontOverrides.fEdging) {
1924 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1925 }
1926 if (ImGui::Combo("Edging", &edgingIdx,
1927 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1928 {
1929 if (edgingIdx == 0) {
1930 fFontOverrides.fEdging = false;
1931 fFont.setEdging(SkFont::Edging::kAlias);
1932 } else {
1933 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1934 fFontOverrides.fEdging = true;
1935 }
1936 paramsChanged = true;
1937 }
1938
Ben Wagner15a8d572019-03-21 13:35:44 -04001939 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1940 if (fFontOverrides.fSize) {
1941 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001942 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001943 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001944 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001945 fFontOverrides.fSizeRange[0],
1946 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001947 "%.6f", 2.0f))
1948 {
Mike Reed3ae47332019-01-04 10:11:46 -05001949 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001950 paramsChanged = true;
1951 }
1952 }
1953
1954 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1955 if (fFontOverrides.fScaleX) {
1956 float scaleX = fFont.getScaleX();
1957 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1958 fFont.setScaleX(scaleX);
1959 paramsChanged = true;
1960 }
1961 }
1962
1963 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1964 if (fFontOverrides.fSkewX) {
1965 float skewX = fFont.getSkewX();
1966 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1967 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001968 paramsChanged = true;
1969 }
1970 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001971 }
1972
Mike Reed81f60ec2018-05-15 10:09:52 -04001973 {
1974 SkMetaData controls;
1975 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1976 if (ImGui::CollapsingHeader("Current Slide")) {
1977 SkMetaData::Iter iter(controls);
1978 const char* name;
1979 SkMetaData::Type type;
1980 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001981 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001982 if (type == SkMetaData::kScalar_Type) {
1983 float val[3];
1984 SkASSERT(count == 3);
1985 controls.findScalars(name, &count, val);
1986 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1987 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001988 }
Ben Wagner110c7032019-03-22 17:03:59 -04001989 } else if (type == SkMetaData::kBool_Type) {
1990 bool val;
1991 SkASSERT(count == 1);
1992 controls.findBool(name, &val);
1993 if (ImGui::Checkbox(name, &val)) {
1994 controls.setBool(name, val);
1995 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001996 }
1997 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001998 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001999 }
2000 }
2001 }
2002
Ben Wagner7a3c6742018-04-23 10:01:07 -04002003 if (fShowSlidePicker) {
2004 ImGui::SetNextTreeNodeOpen(true);
2005 }
Brian Osman79086b92017-02-10 13:36:16 -05002006 if (ImGui::CollapsingHeader("Slide")) {
2007 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002008 static ImVector<const char*> filteredSlideNames;
2009 static ImVector<int> filteredSlideIndices;
2010
Brian Osmanfce09c52017-11-14 15:32:20 -05002011 if (fShowSlidePicker) {
2012 ImGui::SetKeyboardFocusHere();
2013 fShowSlidePicker = false;
2014 }
2015
Brian Osman79086b92017-02-10 13:36:16 -05002016 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002017 filteredSlideNames.clear();
2018 filteredSlideIndices.clear();
2019 int filteredIndex = 0;
2020 for (int i = 0; i < fSlides.count(); ++i) {
2021 const char* slideName = fSlides[i]->getName().c_str();
2022 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2023 if (i == fCurrentSlide) {
2024 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002025 }
Brian Osmanf479e422017-11-08 13:11:36 -05002026 filteredSlideNames.push_back(slideName);
2027 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002028 }
Brian Osman79086b92017-02-10 13:36:16 -05002029 }
Brian Osmanf479e422017-11-08 13:11:36 -05002030
Brian Osmanf479e422017-11-08 13:11:36 -05002031 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2032 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002033 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002034 }
2035 }
Brian Osmana109e392017-02-24 09:49:14 -05002036
2037 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002038 ColorMode newMode = fColorMode;
2039 auto cmButton = [&](ColorMode mode, const char* label) {
2040 if (ImGui::RadioButton(label, mode == fColorMode)) {
2041 newMode = mode;
2042 }
2043 };
2044
2045 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002046 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2047 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002048 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002049
2050 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002051 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002052 }
2053
2054 // Pick from common gamuts:
2055 int primariesIdx = 4; // Default: Custom
2056 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2057 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2058 primariesIdx = i;
2059 break;
2060 }
2061 }
2062
Brian Osman03115dc2018-11-26 13:55:19 -05002063 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002064 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002065
Brian Osmana109e392017-02-24 09:49:14 -05002066 if (ImGui::Combo("Primaries", &primariesIdx,
2067 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2068 if (primariesIdx >= 0 && primariesIdx <= 3) {
2069 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2070 }
2071 }
2072
2073 // Allow direct editing of gamut
2074 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2075 }
Brian Osman207d4102019-01-10 09:40:58 -05002076
2077 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002078 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002079 if (ImGui::Checkbox("Pause", &isPaused)) {
2080 fAnimTimer.togglePauseResume();
2081 }
Brian Osman707d2022019-01-10 11:27:34 -05002082
2083 float speed = fAnimTimer.getSpeed();
2084 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2085 fAnimTimer.setSpeed(speed);
2086 }
Brian Osman207d4102019-01-10 09:40:58 -05002087 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002088
Brian Osmanfd7657c2019-04-25 11:34:07 -04002089 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2090#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2091 || Window::kANGLE_BackendType == fBackendType
2092#endif
2093 ;
2094
2095 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2096 // than GL, we need to force it on. Just do that on the first frame after the backend
2097 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002098 if (!backendIsGL &&
2099 params.fGrContextOptions.fShaderCacheStrategy !=
2100 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2101 params.fGrContextOptions.fShaderCacheStrategy =
2102 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002103 paramsChanged = true;
2104 fPersistentCache.reset();
2105 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002106 // To re-load shaders from the currently active programs, we flush all caches on one
2107 // frame, then set a flag to poll the cache on the next frame.
2108 static bool gLoadPending = false;
2109 if (gLoadPending) {
2110 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2111 int hitCount) {
2112 CachedGLSL& entry(fCachedGLSL.push_back());
2113 entry.fKey = key;
2114 SkMD5 hash;
2115 hash.write(key->bytes(), key->size());
2116 SkMD5::Digest digest = hash.finish();
2117 for (int i = 0; i < 16; ++i) {
2118 entry.fKeyString.appendf("%02x", digest.data[i]);
2119 }
2120
Brian Osmana66081d2019-09-03 14:59:26 -04002121 SkReader32 reader(data->data(), data->size());
2122 entry.fShaderType = reader.readU32();
2123 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2124 entry.fInputs,
2125 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002126 };
2127 fCachedGLSL.reset();
2128 fPersistentCache.foreach(collectShaders);
2129 gLoadPending = false;
2130 }
2131
2132 // Defer actually doing the load/save logic so that we can trigger a save when we
2133 // start or finish hovering on a tree node in the list below:
2134 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002135 bool doSave = ImGui::Button("Save");
2136 if (backendIsGL) {
2137 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002138 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2139 GrContextOptions::ShaderCacheStrategy::kSkSL;
2140 if (ImGui::Checkbox("SkSL", &sksl)) {
2141 params.fGrContextOptions.fShaderCacheStrategy = sksl
2142 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2143 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002144 paramsChanged = true;
2145 doLoad = true;
2146 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2147 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002148 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002149
2150 ImGui::BeginChild("##ScrollingRegion");
2151 for (auto& entry : fCachedGLSL) {
2152 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2153 bool hovered = ImGui::IsItemHovered();
2154 if (hovered != entry.fHovered) {
2155 // Force a save to patch the highlight shader in/out
2156 entry.fHovered = hovered;
2157 doSave = true;
2158 }
2159 if (inTreeNode) {
2160 // Full width, and a reasonable amount of space for each shader.
2161 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2162 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2163 boxSize);
2164 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2165 boxSize);
2166 ImGui::TreePop();
2167 }
2168 }
2169 ImGui::EndChild();
2170
2171 if (doLoad) {
2172 fPersistentCache.reset();
2173 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2174 gLoadPending = true;
2175 }
2176 if (doSave) {
2177 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002178 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002179 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2180 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002181
Brian Osman072e6fc2019-06-12 11:35:41 -04002182 SkSL::String highlight;
2183 if (!sksl) {
2184 highlight = shaderCaps->versionDeclString();
2185 if (shaderCaps->usesPrecisionModifiers()) {
2186 highlight.append("precision mediump float;\n");
2187 }
Brian Osman5bee3902019-05-07 09:55:45 -04002188 }
2189 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002190 highlight.appendf("out %s sk_FragColor;\n"
2191 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2192 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002193
2194 fPersistentCache.reset();
2195 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2196 for (auto& entry : fCachedGLSL) {
2197 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2198 if (entry.fHovered) {
2199 entry.fShader[kFragment_GrShaderType] = highlight;
2200 }
2201
Brian Osmana085a412019-04-25 09:44:43 -04002202 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2203 entry.fShader,
2204 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002205 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002206 fPersistentCache.store(*entry.fKey, *data);
2207
2208 entry.fShader[kFragment_GrShaderType] = backup;
2209 }
2210 }
2211 }
Brian Osman79086b92017-02-10 13:36:16 -05002212 }
Brian Salomon99a33902017-03-07 15:16:34 -05002213 if (paramsChanged) {
2214 fDeferredActions.push_back([=]() {
2215 fWindow->setRequestedDisplayParams(params);
2216 fWindow->inval();
2217 this->updateTitle();
2218 });
2219 }
Brian Osman79086b92017-02-10 13:36:16 -05002220 ImGui::End();
2221 }
2222
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002223 if (gShaderErrorHandler.fErrors.count()) {
2224 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2225 ImGui::Begin("Shader Errors");
2226 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2227 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002228 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2229 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2230 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2231 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002232 }
2233 ImGui::End();
2234 gShaderErrorHandler.reset();
2235 }
2236
Brian Osmanf6877092017-02-13 09:39:57 -05002237 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002238 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2239 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002240 static int zoomFactor = 8;
2241 if (ImGui::Button("<<")) {
2242 zoomFactor = SkTMax(zoomFactor / 2, 4);
2243 }
2244 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2245 if (ImGui::Button(">>")) {
2246 zoomFactor = SkTMin(zoomFactor * 2, 32);
2247 }
Brian Osmanf6877092017-02-13 09:39:57 -05002248
Ben Wagner3627d2e2018-06-26 14:23:20 -04002249 if (!fZoomWindowFixed) {
2250 ImVec2 mousePos = ImGui::GetMousePos();
2251 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2252 }
2253 SkScalar x = fZoomWindowLocation.x();
2254 SkScalar y = fZoomWindowLocation.y();
2255 int xInt = SkScalarRoundToInt(x);
2256 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002257 ImVec2 avail = ImGui::GetContentRegionAvail();
2258
Brian Osmanead517d2017-11-13 15:36:36 -05002259 uint32_t pixel = 0;
2260 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002261 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002262 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002263 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002264 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002265 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002266 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2267 }
2268
Brian Osmand67e5182017-12-08 16:46:09 -05002269 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002270 // Translate so the region of the image that's under the mouse cursor is centered
2271 // in the zoom canvas:
2272 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002273 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2274 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002275 c->drawImage(this->fLastImage, 0, 0);
2276
2277 SkPaint outline;
2278 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002279 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002280 });
Brian Osmanf6877092017-02-13 09:39:57 -05002281 }
2282
2283 ImGui::End();
2284 }
Brian Osman79086b92017-02-10 13:36:16 -05002285}
2286
liyuqian2edb0f42016-07-06 14:11:32 -07002287void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002288 for (int i = 0; i < fDeferredActions.count(); ++i) {
2289 fDeferredActions[i]();
2290 }
2291 fDeferredActions.reset();
2292
Brian Osman56a24812017-12-19 11:15:16 -05002293 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002294 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002295 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002296 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002297
Brian Osman79086b92017-02-10 13:36:16 -05002298 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002299 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2300 // not be visible, though. So we need to redraw if there is at least one visible window, or
2301 // more than one active window. Newly created windows are active but not visible for one frame
2302 // while they determine their layout and sizing.
2303 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2304 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002305 fWindow->inval();
2306 }
jvanverth9f372462016-04-06 06:08:59 -07002307}
liyuqiane5a6cd92016-05-27 08:52:52 -07002308
Florin Malitab632df72018-06-18 21:23:06 -04002309template <typename OptionsFunc>
2310static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2311 OptionsFunc&& optionsFunc) {
2312 writer.beginObject();
2313 {
2314 writer.appendString(kName , name);
2315 writer.appendString(kValue, value);
2316
2317 writer.beginArray(kOptions);
2318 {
2319 optionsFunc(writer);
2320 }
2321 writer.endArray();
2322 }
2323 writer.endObject();
2324}
2325
2326
liyuqiane5a6cd92016-05-27 08:52:52 -07002327void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002328 if (!fWindow) {
2329 return;
2330 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002331 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002332 return; // Surface hasn't been created yet.
2333 }
2334
Florin Malitab632df72018-06-18 21:23:06 -04002335 SkDynamicMemoryWStream memStream;
2336 SkJSONWriter writer(&memStream);
2337 writer.beginArray();
2338
liyuqianb73c24b2016-06-03 08:47:23 -07002339 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002340 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2341 [this](SkJSONWriter& writer) {
2342 for(const auto& slide : fSlides) {
2343 writer.appendString(slide->getName().c_str());
2344 }
2345 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002346
liyuqianb73c24b2016-06-03 08:47:23 -07002347 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002348 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2349 [](SkJSONWriter& writer) {
2350 for (const auto& str : kBackendTypeStrings) {
2351 writer.appendString(str);
2352 }
2353 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002354
csmartdalton578f0642017-02-24 16:04:47 -07002355 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002356 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2357 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2358 [this](SkJSONWriter& writer) {
2359 writer.appendS32(0);
2360
2361 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2362 return;
2363 }
2364
2365 for (int msaa : {4, 8, 16}) {
2366 writer.appendS32(msaa);
2367 }
2368 });
csmartdalton578f0642017-02-24 16:04:47 -07002369
csmartdalton61cd31a2017-02-27 17:00:53 -07002370 // Path renderer state
2371 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002372 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2373 [this](SkJSONWriter& writer) {
2374 const GrContext* ctx = fWindow->getGrContext();
2375 if (!ctx) {
2376 writer.appendString("Software");
2377 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002378 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002379 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2380 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002381 if (caps->shaderCaps()->tessellationSupport()) {
2382 writer.appendString(
2383 gPathRendererNames[GpuPathRenderers::kGpuTessellation].c_str());
2384 }
Florin Malitab632df72018-06-18 21:23:06 -04002385 if (caps->shaderCaps()->pathRenderingSupport()) {
2386 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002387 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002388 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002389 }
2390 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002391 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2392 writer.appendString(
2393 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2394 }
2395 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2396 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002397 writer.appendString(gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2398 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002399 }
2400 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002401
liyuqianb73c24b2016-06-03 08:47:23 -07002402 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002403 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2404 [this](SkJSONWriter& writer) {
2405 writer.appendString(kSoftkeyHint);
2406 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2407 writer.appendString(softkey.c_str());
2408 }
2409 });
liyuqianb73c24b2016-06-03 08:47:23 -07002410
Florin Malitab632df72018-06-18 21:23:06 -04002411 writer.endArray();
2412 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002413
Florin Malitab632df72018-06-18 21:23:06 -04002414 auto data = memStream.detachAsData();
2415
2416 // TODO: would be cool to avoid this copy
2417 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2418
2419 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002420}
2421
2422void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002423 // For those who will add more features to handle the state change in this function:
2424 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2425 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2426 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002427 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002428 for (int i = 0; i < fSlides.count(); ++i) {
2429 if (fSlides[i]->getName().equals(stateValue)) {
2430 this->setCurrentSlide(i);
2431 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002432 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002433 }
Florin Malitaab99c342018-01-16 16:23:03 -05002434
2435 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002436 } else if (stateName.equals(kBackendStateName)) {
2437 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2438 if (stateValue.equals(kBackendTypeStrings[i])) {
2439 if (fBackendType != i) {
2440 fBackendType = (sk_app::Window::BackendType)i;
2441 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002442 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002443 }
2444 break;
2445 }
2446 }
csmartdalton578f0642017-02-24 16:04:47 -07002447 } else if (stateName.equals(kMSAAStateName)) {
2448 DisplayParams params = fWindow->getRequestedDisplayParams();
2449 int sampleCount = atoi(stateValue.c_str());
2450 if (sampleCount != params.fMSAASampleCount) {
2451 params.fMSAASampleCount = sampleCount;
2452 fWindow->setRequestedDisplayParams(params);
2453 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002454 this->updateTitle();
2455 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002456 }
2457 } else if (stateName.equals(kPathRendererStateName)) {
2458 DisplayParams params = fWindow->getRequestedDisplayParams();
2459 for (const auto& pair : gPathRendererNames) {
2460 if (pair.second == stateValue.c_str()) {
2461 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2462 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2463 fWindow->setRequestedDisplayParams(params);
2464 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002465 this->updateTitle();
2466 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002467 }
2468 break;
2469 }
csmartdalton578f0642017-02-24 16:04:47 -07002470 }
liyuqianb73c24b2016-06-03 08:47:23 -07002471 } else if (stateName.equals(kSoftkeyStateName)) {
2472 if (!stateValue.equals(kSoftkeyHint)) {
2473 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002474 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002475 }
liyuqian2edb0f42016-07-06 14:11:32 -07002476 } else if (stateName.equals(kRefreshStateName)) {
2477 // This state is actually NOT in the UI state.
2478 // We use this to allow Android to quickly set bool fRefresh.
2479 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002480 } else {
2481 SkDebugf("Unknown stateName: %s", stateName.c_str());
2482 }
2483}
Brian Osman79086b92017-02-10 13:36:16 -05002484
Hal Canaryb1f411a2019-08-29 10:39:22 -04002485bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002486 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002487}
2488
Hal Canaryb1f411a2019-08-29 10:39:22 -04002489bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002490 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002491 fWindow->inval();
2492 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002493 } else {
2494 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002495 }
Brian Osman79086b92017-02-10 13:36:16 -05002496}