blob: b1c38192e4e7113e8bb2573b5a3bf99a38f3b033 [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 });
Mike Reed59295352020-03-12 13:56:34 -0400601 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
602 fDrawViaSerialize = !fDrawViaSerialize;
603 this->updateTitle();
604 fWindow->inval();
605 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500606
jvanverth2bb3b6d2016-04-08 07:24:09 -0700607 // set up slides
608 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500609 if (FLAGS_list) {
610 this->listNames();
611 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700612
Brian Osman9bb47cf2018-04-26 15:55:00 -0400613 fPerspectivePoints[0].set(0, 0);
614 fPerspectivePoints[1].set(1, 0);
615 fPerspectivePoints[2].set(0, 1);
616 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700617 fAnimTimer.run();
618
Hal Canaryc465d132017-12-08 10:21:31 -0500619 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500620 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400621 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500622 }
623 fImGuiGamutPaint.setColor(SK_ColorWHITE);
624 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
625
jongdeok.kim804f17e2019-02-26 14:39:23 +0900626 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500627 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700628}
629
jvanverth34524262016-05-04 13:49:13 -0700630void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400631 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
632 static const struct {
633 const char* fExtension;
634 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500635 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400636 const SlideFactory fFactory;
637 } gExternalSlidesInfo[] = {
638 { ".skp", "skp-dir", FLAGS_skps,
639 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
640 return sk_make_sp<SKPSlide>(name, path);}
641 },
642 { ".jpg", "jpg-dir", FLAGS_jpgs,
643 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
644 return sk_make_sp<ImageSlide>(name, path);}
645 },
Florin Malita87ccf332018-05-04 12:23:24 -0400646#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400647 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400648 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
649 return sk_make_sp<SkottieSlide>(name, path);}
650 },
Florin Malita87ccf332018-05-04 12:23:24 -0400651#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400652#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400653 { ".svg", "svg-dir", FLAGS_svgs,
654 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
655 return sk_make_sp<SvgSlide>(name, path);}
656 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400657#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400658 };
jvanverthc265a922016-04-08 12:51:45 -0700659
Brian Salomon343553a2018-09-05 15:41:23 -0400660 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700661
Mike Klein88544fb2019-03-20 10:50:33 -0500662 const auto addSlide =
663 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
664 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
665 return;
666 }
liyuqian6f163d22016-06-13 12:26:45 -0700667
Mike Klein88544fb2019-03-20 10:50:33 -0500668 if (auto slide = fact(name, path)) {
669 dirSlides.push_back(slide);
670 fSlides.push_back(std::move(slide));
671 }
672 };
Florin Malita76a076b2018-02-15 18:40:48 -0500673
Florin Malita38792ce2018-05-08 10:36:18 -0400674 if (!FLAGS_file.isEmpty()) {
675 // single file mode
676 const SkString file(FLAGS_file[0]);
677
678 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
679 for (const auto& sinfo : gExternalSlidesInfo) {
680 if (file.endsWith(sinfo.fExtension)) {
681 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
682 return;
683 }
684 }
685
686 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
687 } else {
688 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
689 }
690
691 return;
692 }
693
694 // Bisect slide.
695 if (!FLAGS_bisect.isEmpty()) {
696 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500697 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400698 if (FLAGS_bisect.count() >= 2) {
699 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
700 bisect->onChar(*ch);
701 }
702 }
703 fSlides.push_back(std::move(bisect));
704 }
705 }
706
707 // GMs
708 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400709 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400710 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500711 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400712 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400713 fSlides.push_back(std::move(slide));
714 }
Florin Malita38792ce2018-05-08 10:36:18 -0400715 }
716 // reverse gms
717 int numGMs = fSlides.count() - firstGM;
718 for (int i = 0; i < numGMs/2; ++i) {
719 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
720 }
721
722 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400723 for (const SampleFactory factory : SampleRegistry::Range()) {
724 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500725 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400726 fSlides.push_back(slide);
727 }
Florin Malita38792ce2018-05-08 10:36:18 -0400728 }
729
Brian Osman7c979f52019-02-12 13:27:51 -0500730 // Particle demo
731 {
732 // TODO: Convert this to a sample
733 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500734 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500735 fSlides.push_back(std::move(slide));
736 }
737 }
738
Brian Osmand927bd22019-12-18 11:23:12 -0500739 // Runtime shader editor
740 {
741 sk_sp<Slide> slide(new SkSLSlide());
742 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
743 fSlides.push_back(std::move(slide));
744 }
745 }
746
Florin Malita0ffa3222018-04-05 14:34:45 -0400747 for (const auto& info : gExternalSlidesInfo) {
748 for (const auto& flag : info.fFlags) {
749 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
750 // single file
751 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
752 } else {
753 // directory
754 SkOSFile::Iter it(flag.c_str(), info.fExtension);
755 SkString name;
756 while (it.next(&name)) {
757 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
758 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400759 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400760 if (!dirSlides.empty()) {
761 fSlides.push_back(
762 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
763 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500764 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400765 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400766 }
767 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500768
769 if (!fSlides.count()) {
770 sk_sp<Slide> slide(new NullSlide());
771 fSlides.push_back(std::move(slide));
772 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700773}
774
775
jvanverth34524262016-05-04 13:49:13 -0700776Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700777 fWindow->detach();
778 delete fWindow;
779}
780
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500781struct SkPaintTitleUpdater {
782 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
783 void append(const char* s) {
784 if (fCount == 0) {
785 fTitle->append(" {");
786 } else {
787 fTitle->append(", ");
788 }
789 fTitle->append(s);
790 ++fCount;
791 }
792 void done() {
793 if (fCount > 0) {
794 fTitle->append("}");
795 }
796 }
797 SkString* fTitle;
798 int fCount;
799};
800
brianosman05de2162016-05-06 13:28:57 -0700801void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700802 if (!fWindow) {
803 return;
804 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500805 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700806 return; // Surface hasn't been created yet.
807 }
808
jvanverth34524262016-05-04 13:49:13 -0700809 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700810 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700811
Mike Kleine5acd752019-03-22 09:57:16 -0500812 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400813 if (gSkForceAnalyticAA) {
814 title.append(" <FAAA>");
815 } else {
816 title.append(" <AAA>");
817 }
818 }
Mike Reed59295352020-03-12 13:56:34 -0400819 if (fDrawViaSerialize) {
820 title.append(" <serialize>");
821 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400822
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500823 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500824 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
825 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400826 const char* on, const char* off)
827 {
Ben Wagner9613e452019-01-23 10:34:59 -0500828 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400829 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500830 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400831 };
832
Ben Wagner9613e452019-01-23 10:34:59 -0500833 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
834 const char* on, const char* off)
835 {
836 if (fFontOverrides.*flag) {
837 paintTitle.append((fFont.*isFlag)() ? on : off);
838 }
839 };
840
841 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
842 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500843 if (fPaintOverrides.fFilterQuality) {
844 switch (fPaint.getFilterQuality()) {
845 case kNone_SkFilterQuality:
846 paintTitle.append("NoFilter");
847 break;
848 case kLow_SkFilterQuality:
849 paintTitle.append("LowFilter");
850 break;
851 case kMedium_SkFilterQuality:
852 paintTitle.append("MediumFilter");
853 break;
854 case kHigh_SkFilterQuality:
855 paintTitle.append("HighFilter");
856 break;
857 }
858 }
Ben Wagner9613e452019-01-23 10:34:59 -0500859
860 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
861 "Force Autohint", "No Force Autohint");
862 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400863 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500864 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
865 "Linear Metrics", "Non-Linear Metrics");
866 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
867 "Bitmap Text", "No Bitmap Text");
868 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
869
870 if (fFontOverrides.fEdging) {
871 switch (fFont.getEdging()) {
872 case SkFont::Edging::kAlias:
873 paintTitle.append("Alias Text");
874 break;
875 case SkFont::Edging::kAntiAlias:
876 paintTitle.append("Antialias Text");
877 break;
878 case SkFont::Edging::kSubpixelAntiAlias:
879 paintTitle.append("Subpixel Antialias Text");
880 break;
881 }
882 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400883
Mike Reed3ae47332019-01-04 10:11:46 -0500884 if (fFontOverrides.fHinting) {
885 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400886 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500887 paintTitle.append("No Hinting");
888 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400889 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500890 paintTitle.append("Slight Hinting");
891 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400892 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500893 paintTitle.append("Normal Hinting");
894 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400895 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500896 paintTitle.append("Full Hinting");
897 break;
898 }
899 }
900 paintTitle.done();
901
Brian Osman92004802017-03-06 11:47:26 -0500902 switch (fColorMode) {
903 case ColorMode::kLegacy:
904 title.append(" Legacy 8888");
905 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500906 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500907 title.append(" ColorManaged 8888");
908 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500909 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500910 title.append(" ColorManaged F16");
911 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400912 case ColorMode::kColorManagedF16Norm:
913 title.append(" ColorManaged F16 Norm");
914 break;
Brian Osman92004802017-03-06 11:47:26 -0500915 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500916
Brian Osman92004802017-03-06 11:47:26 -0500917 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500918 int curPrimaries = -1;
919 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
920 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
921 curPrimaries = i;
922 break;
923 }
924 }
Brian Osman03115dc2018-11-26 13:55:19 -0500925 title.appendf(" %s Gamma %f",
926 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500927 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700928 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500929
Ben Wagner37c54032018-04-13 14:30:23 -0400930 const DisplayParams& params = fWindow->getRequestedDisplayParams();
931 if (fPixelGeometryOverrides) {
932 switch (params.fSurfaceProps.pixelGeometry()) {
933 case kUnknown_SkPixelGeometry:
934 title.append( " Flat");
935 break;
936 case kRGB_H_SkPixelGeometry:
937 title.append( " RGB");
938 break;
939 case kBGR_H_SkPixelGeometry:
940 title.append( " BGR");
941 break;
942 case kRGB_V_SkPixelGeometry:
943 title.append( " RGBV");
944 break;
945 case kBGR_V_SkPixelGeometry:
946 title.append( " BGRV");
947 break;
948 }
949 }
950
951 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
952 title.append(" DFT");
953 }
954
csmartdalton578f0642017-02-24 16:04:47 -0700955 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700956 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500957 int msaa = fWindow->sampleCount();
958 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700959 title.appendf(" MSAA: %i", msaa);
960 }
961 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700962
963 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -0700964 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700965 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
966 }
967
Brian Osman805a7272018-05-02 15:40:20 -0400968 if (kPerspective_Real == fPerspectiveMode) {
969 title.append(" Perpsective (Real)");
970 } else if (kPerspective_Fake == fPerspectiveMode) {
971 title.append(" Perspective (Fake)");
972 }
973
brianosman05de2162016-05-06 13:28:57 -0700974 fWindow->setTitle(title.c_str());
975}
976
Florin Malitaab99c342018-01-16 16:23:03 -0500977int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500978
979 if (!FLAGS_slide.isEmpty()) {
980 int count = fSlides.count();
981 for (int i = 0; i < count; i++) {
982 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500983 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500984 }
985 }
986
987 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
988 this->listNames();
989 }
990
Florin Malitaab99c342018-01-16 16:23:03 -0500991 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500992}
993
Florin Malitaab99c342018-01-16 16:23:03 -0500994void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500995 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500996 for (const auto& slide : fSlides) {
997 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500998 }
999}
1000
Florin Malitaab99c342018-01-16 16:23:03 -05001001void Viewer::setCurrentSlide(int slide) {
1002 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001003
Florin Malitaab99c342018-01-16 16:23:03 -05001004 if (slide == fCurrentSlide) {
1005 return;
1006 }
1007
1008 if (fCurrentSlide >= 0) {
1009 fSlides[fCurrentSlide]->unload();
1010 }
1011
1012 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1013 SkIntToScalar(fWindow->height()));
1014 fCurrentSlide = slide;
1015 this->setupCurrentSlide();
1016}
1017
1018void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001019 if (fCurrentSlide >= 0) {
1020 // prepare dimensions for image slides
1021 fGesture.resetTouchState();
1022 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001023
Jim Van Verth0848fb02018-01-22 13:39:30 -05001024 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1025 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1026 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001027
Jim Van Verth0848fb02018-01-22 13:39:30 -05001028 // Start with a matrix that scales the slide to the available screen space
1029 if (fWindow->scaleContentToFit()) {
1030 if (windowRect.width() > 0 && windowRect.height() > 0) {
1031 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1032 }
liyuqiane46e4f02016-05-20 07:32:19 -07001033 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001034
1035 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001036 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001037
1038 this->updateTitle();
1039 this->updateUIState();
1040
1041 fStatsLayer.resetMeasurements();
1042
1043 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001044 }
jvanverthc265a922016-04-08 12:51:45 -07001045}
1046
Brian Osmanaba642c2020-02-06 12:52:25 -05001047#define MAX_ZOOM_LEVEL 8.0f
1048#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001049
jvanverth34524262016-05-04 13:49:13 -07001050void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001051 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001052 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001053 this->preTouchMatrixChanged();
1054}
Yuqian Li755778c2018-03-28 16:23:31 -04001055
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001056void Viewer::preTouchMatrixChanged() {
1057 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001058 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1059 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1060 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1061 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1062}
1063
Brian Osman805a7272018-05-02 15:40:20 -04001064SkMatrix Viewer::computePerspectiveMatrix() {
1065 SkScalar w = fWindow->width(), h = fWindow->height();
1066 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1067 SkPoint perspPts[4] = {
1068 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1069 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1070 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1071 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1072 };
1073 SkMatrix m;
1074 m.setPolyToPoly(orthoPts, perspPts, 4);
1075 return m;
1076}
1077
Yuqian Li755778c2018-03-28 16:23:31 -04001078SkMatrix Viewer::computePreTouchMatrix() {
1079 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001080
1081 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001082 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001083 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001084
1085 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1086 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001087
Brian Osman805a7272018-05-02 15:40:20 -04001088 if (kPerspective_Real == fPerspectiveMode) {
1089 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001090 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001091 }
1092
Yuqian Li755778c2018-03-28 16:23:31 -04001093 return m;
jvanverthc265a922016-04-08 12:51:45 -07001094}
1095
liyuqiand3cdbca2016-05-17 12:44:20 -07001096SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001097 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001098 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001099 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001100 return m;
jvanverthc265a922016-04-08 12:51:45 -07001101}
1102
Brian Osman621491e2017-02-28 15:45:01 -05001103void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001104 fPersistentCache.reset();
1105 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001106 fBackendType = backendType;
1107
1108 fWindow->detach();
1109
Brian Osman70d2f432017-11-08 09:54:10 -05001110#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001111 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1112 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001113 DisplayParams params = fWindow->getRequestedDisplayParams();
1114 delete fWindow;
1115 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001116
Brian Osman70d2f432017-11-08 09:54:10 -05001117 // re-register callbacks
1118 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001119 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001120 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001121 fWindow->pushLayer(&fImGuiLayer);
1122
Brian Osman70d2f432017-11-08 09:54:10 -05001123 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1124 // will still include our correct sample count. But the re-created fWindow will lose that
1125 // information. On Windows, we need to re-create the window when changing sample count,
1126 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1127 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1128 // as if we had never changed windows at all).
1129 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001130#endif
1131
Brian Osman70d2f432017-11-08 09:54:10 -05001132 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001133}
1134
Brian Osman92004802017-03-06 11:47:26 -05001135void Viewer::setColorMode(ColorMode colorMode) {
1136 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001137 this->updateTitle();
1138 fWindow->inval();
1139}
1140
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001141class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1142public:
Mike Reed3ae47332019-01-04 10:11:46 -05001143 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1144 SkFont* font, Viewer::SkFontFields* ffields)
1145 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001146 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001147 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1148 sk_sp<SkTextBlob>* cache) {
1149 bool blobWillChange = false;
1150 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001151 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1152 bool shouldDraw = this->filterFont(&filteredFont);
1153 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001154 blobWillChange = true;
1155 break;
1156 }
1157 }
1158 if (!blobWillChange) {
1159 return blob;
1160 }
1161
1162 SkTextBlobBuilder builder;
1163 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001164 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1165 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001166 if (!shouldDraw) {
1167 continue;
1168 }
1169
Mike Reed3ae47332019-01-04 10:11:46 -05001170 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001171
Ben Wagner41e40472018-09-24 13:01:54 -04001172 const SkTextBlobBuilder::RunBuffer& runBuffer
1173 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001174 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001175 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001176 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001177 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001178 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001179 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001180 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001181 it.glyphCount(), it.textSize(), SkString())
1182 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1183 uint32_t glyphCount = it.glyphCount();
1184 if (it.glyphs()) {
1185 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1186 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1187 }
1188 if (it.pos()) {
1189 size_t posSize = sizeof(decltype(*it.pos()));
1190 uint8_t positioning = it.positioning();
1191 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1192 }
1193 if (it.text()) {
1194 size_t textSize = sizeof(decltype(*it.text()));
1195 uint32_t textCount = it.textSize();
1196 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1197 }
1198 if (it.clusters()) {
1199 size_t clusterSize = sizeof(decltype(*it.clusters()));
1200 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1201 }
1202 }
1203 *cache = builder.make();
1204 return cache->get();
1205 }
1206 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1207 const SkPaint& paint) override {
1208 sk_sp<SkTextBlob> cache;
1209 this->SkPaintFilterCanvas::onDrawTextBlob(
1210 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1211 }
Mike Reed3ae47332019-01-04 10:11:46 -05001212 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001213 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001214 font->writable()->setSize(fFont->getSize());
1215 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001216 if (fFontOverrides->fScaleX) {
1217 font->writable()->setScaleX(fFont->getScaleX());
1218 }
1219 if (fFontOverrides->fSkewX) {
1220 font->writable()->setSkewX(fFont->getSkewX());
1221 }
Mike Reed3ae47332019-01-04 10:11:46 -05001222 if (fFontOverrides->fHinting) {
1223 font->writable()->setHinting(fFont->getHinting());
1224 }
Ben Wagner9613e452019-01-23 10:34:59 -05001225 if (fFontOverrides->fEdging) {
1226 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001227 }
Ben Wagner9613e452019-01-23 10:34:59 -05001228 if (fFontOverrides->fEmbolden) {
1229 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001230 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001231 if (fFontOverrides->fBaselineSnap) {
1232 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1233 }
Ben Wagner9613e452019-01-23 10:34:59 -05001234 if (fFontOverrides->fLinearMetrics) {
1235 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001236 }
Ben Wagner9613e452019-01-23 10:34:59 -05001237 if (fFontOverrides->fSubpixel) {
1238 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001239 }
Ben Wagner9613e452019-01-23 10:34:59 -05001240 if (fFontOverrides->fEmbeddedBitmaps) {
1241 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001242 }
Ben Wagner9613e452019-01-23 10:34:59 -05001243 if (fFontOverrides->fForceAutoHinting) {
1244 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001245 }
Ben Wagner9613e452019-01-23 10:34:59 -05001246
Mike Reed3ae47332019-01-04 10:11:46 -05001247 return true;
1248 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001249 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001250 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001251 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001252 }
Ben Wagner9613e452019-01-23 10:34:59 -05001253 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001254 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001255 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001256 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001257 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001258 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001259 return true;
1260 }
1261 SkPaint* fPaint;
1262 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001263 SkFont* fFont;
1264 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001265};
1266
Robert Phillips9882dae2019-03-04 11:00:10 -05001267void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001268 if (fCurrentSlide < 0) {
1269 return;
1270 }
1271
Robert Phillips9882dae2019-03-04 11:00:10 -05001272 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001273
Brian Osmanf750fbc2017-02-08 10:47:28 -05001274 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001275 SkSurface* slideSurface = surface;
1276 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001277 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001278
Brian Osmane0d4fba2017-03-15 10:24:55 -04001279 // 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 -05001280 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001281 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001282 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001283 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001284 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001285 }
1286
Brian Osman3ac99cf2017-12-01 11:23:53 -05001287 if (fSaveToSKP) {
1288 SkPictureRecorder recorder;
1289 SkCanvas* recorderCanvas = recorder.beginRecording(
1290 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001291 fSlides[fCurrentSlide]->draw(recorderCanvas);
1292 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1293 SkFILEWStream stream("sample_app.skp");
1294 picture->serialize(&stream);
1295 fSaveToSKP = false;
1296 }
1297
Brian Osmane9ed0f02018-11-26 14:50:05 -05001298 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001299 SkColorType colorType;
1300 switch (fColorMode) {
1301 case ColorMode::kLegacy:
1302 case ColorMode::kColorManaged8888:
1303 colorType = kN32_SkColorType;
1304 break;
1305 case ColorMode::kColorManagedF16:
1306 colorType = kRGBA_F16_SkColorType;
1307 break;
1308 case ColorMode::kColorManagedF16Norm:
1309 colorType = kRGBA_F16Norm_SkColorType;
1310 break;
1311 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001312
1313 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001314 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1315 slideCanvas->getProps(&props);
1316
Brian Osmane9ed0f02018-11-26 14:50:05 -05001317 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1318 return Window::kRaster_BackendType == this->fBackendType
1319 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001320 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001321 };
1322
Brian Osman03115dc2018-11-26 13:55:19 -05001323 // We need to render offscreen if we're...
1324 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1325 // ... in any raster mode, because the window surface is actually GL
1326 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001327 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001328 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001329 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001330 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001331 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001332 colorSpace != nullptr ||
1333 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001334
Brian Osmane9ed0f02018-11-26 14:50:05 -05001335 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001336 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001337 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001338 }
1339
Mike Reed59295352020-03-12 13:56:34 -04001340 SkPictureRecorder recorder;
1341 SkCanvas* recorderRestoreCanvas = nullptr;
1342 if (fDrawViaSerialize) {
1343 recorderRestoreCanvas = slideCanvas;
1344 slideCanvas = recorder.beginRecording(
1345 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1346 }
1347
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001348 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001349 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001350 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001351 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001352 if (fTiled) {
1353 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1354 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001355 for (int y = 0; y < fWindow->height(); y += tileH) {
1356 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001357 SkAutoCanvasRestore acr(slideCanvas, true);
1358 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1359 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001360 }
1361 }
1362
1363 // Draw borders between tiles
1364 if (fDrawTileBoundaries) {
1365 SkPaint border;
1366 border.setColor(0x60FF00FF);
1367 border.setStyle(SkPaint::kStroke_Style);
1368 for (int y = 0; y < fWindow->height(); y += tileH) {
1369 for (int x = 0; x < fWindow->width(); x += tileW) {
1370 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1371 }
1372 }
1373 }
1374 } else {
1375 slideCanvas->concat(this->computeMatrix());
1376 if (kPerspective_Real == fPerspectiveMode) {
1377 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1378 }
Mike Reed3ae47332019-01-04 10:11:46 -05001379 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001380 fSlides[fCurrentSlide]->draw(&filterCanvas);
1381 }
Brian Osman56a24812017-12-19 11:15:16 -05001382 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001383 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001384
Mike Reed59295352020-03-12 13:56:34 -04001385 if (recorderRestoreCanvas) {
1386 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1387 auto data = picture->serialize();
1388 slideCanvas = recorderRestoreCanvas;
1389 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1390 }
1391
Brian Osman1df161a2017-02-09 12:10:20 -05001392 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001393 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001394 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001395 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001396
1397 // If we rendered offscreen, snap an image and push the results to the window's canvas
1398 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001399 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001400
Robert Phillips9882dae2019-03-04 11:00:10 -05001401 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001402 SkPaint paint;
1403 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001404 int prePerspectiveCount = canvas->save();
1405 if (kPerspective_Fake == fPerspectiveMode) {
1406 paint.setFilterQuality(kHigh_SkFilterQuality);
1407 canvas->clear(SK_ColorWHITE);
1408 canvas->concat(this->computePerspectiveMatrix());
1409 }
Brian Osman03115dc2018-11-26 13:55:19 -05001410 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001411 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001412 }
Mike Reed376d8122019-03-14 11:39:02 -04001413
1414 if (fShowSlideDimensions) {
1415 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1416 SkPaint paint;
1417 paint.setColor(0x40FFFF00);
1418 surface->getCanvas()->drawRect(r, paint);
1419 }
liyuqian6f163d22016-06-13 12:26:45 -07001420}
1421
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001422void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001423 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001424 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001425}
Jim Van Verth6f449692017-02-14 15:16:46 -05001426
Robert Phillips9882dae2019-03-04 11:00:10 -05001427void Viewer::onPaint(SkSurface* surface) {
1428 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001429
Robert Phillips9882dae2019-03-04 11:00:10 -05001430 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001431
Brian Osmand67e5182017-12-08 16:46:09 -05001432 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001433
1434 if (GrContext* ctx = fWindow->getGrContext()) {
1435 // Clean out cache items that haven't been used in more than 10 seconds.
1436 ctx->performDeferredCleanup(std::chrono::seconds(10));
1437 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001438}
1439
Ben Wagnera1915972018-08-09 15:06:19 -04001440void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001441 if (fCurrentSlide >= 0) {
1442 fSlides[fCurrentSlide]->resize(width, height);
1443 }
Ben Wagnera1915972018-08-09 15:06:19 -04001444}
1445
Florin Malitacefc1b92018-02-19 21:43:47 -05001446SkPoint Viewer::mapEvent(float x, float y) {
1447 const auto m = this->computeMatrix();
1448 SkMatrix inv;
1449
1450 SkAssertResult(m.invert(&inv));
1451
1452 return inv.mapXY(x, y);
1453}
1454
Hal Canaryb1f411a2019-08-29 10:39:22 -04001455bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001456 if (GestureDevice::kMouse == fGestureDevice) {
1457 return false;
1458 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001459
1460 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001461 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001462 fWindow->inval();
1463 return true;
1464 }
1465
liyuqiand3cdbca2016-05-17 12:44:20 -07001466 void* castedOwner = reinterpret_cast<void*>(owner);
1467 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001468 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001469 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001470#if defined(SK_BUILD_FOR_IOS)
1471 // TODO: move IOS swipe detection higher up into the platform code
1472 SkPoint dir;
1473 if (fGesture.isFling(&dir)) {
1474 // swiping left or right
1475 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1476 if (dir.fX < 0) {
1477 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1478 fCurrentSlide + 1 : 0);
1479 } else {
1480 this->setCurrentSlide(fCurrentSlide > 0 ?
1481 fCurrentSlide - 1 : fSlides.count() - 1);
1482 }
1483 }
1484 fGesture.reset();
1485 }
1486#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001487 break;
1488 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001489 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001490 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001491 break;
1492 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001493 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001494 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001495 break;
1496 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001497 default: {
1498 // kLeft and kRight are only for swipes
1499 SkASSERT(false);
1500 break;
1501 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001502 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001503 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001504 fWindow->inval();
1505 return true;
1506}
1507
Hal Canaryb1f411a2019-08-29 10:39:22 -04001508bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001509 if (GestureDevice::kTouch == fGestureDevice) {
1510 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001511 }
Brian Osman16c81a12017-12-20 11:58:34 -05001512
Florin Malitacefc1b92018-02-19 21:43:47 -05001513 const auto slidePt = this->mapEvent(x, y);
1514 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1515 fWindow->inval();
1516 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001517 }
1518
1519 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001520 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001521 fGesture.touchEnd(nullptr);
1522 break;
1523 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001524 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001525 fGesture.touchBegin(nullptr, x, y);
1526 break;
1527 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001528 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001529 fGesture.touchMoved(nullptr, x, y);
1530 break;
1531 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001532 default: {
1533 SkASSERT(false); // shouldn't see kRight or kLeft here
1534 break;
1535 }
Brian Osman16c81a12017-12-20 11:58:34 -05001536 }
1537 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1538
Hal Canaryb1f411a2019-08-29 10:39:22 -04001539 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001540 fWindow->inval();
1541 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001542 return true;
1543}
1544
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001545bool Viewer::onFling(skui::InputState state) {
1546 if (skui::InputState::kRight == state) {
1547 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1548 return true;
1549 } else if (skui::InputState::kLeft == state) {
1550 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1551 return true;
1552 }
1553 return false;
1554}
1555
1556bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1557 switch (state) {
1558 case skui::InputState::kDown:
1559 fGesture.startZoom();
1560 return true;
1561 break;
1562 case skui::InputState::kMove:
1563 fGesture.updateZoom(scale, x, y, x, y);
1564 return true;
1565 break;
1566 case skui::InputState::kUp:
1567 fGesture.endZoom();
1568 return true;
1569 break;
1570 default:
1571 SkASSERT(false);
1572 break;
1573 }
1574
1575 return false;
1576}
1577
Brian Osmana109e392017-02-24 09:49:14 -05001578static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001579 // The gamut image covers a (0.8 x 0.9) shaped region
1580 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001581
1582 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1583 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1584 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001585 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1586 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1587 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001588
Brian Osman535c5e32019-02-09 16:32:58 -05001589 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1590 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1591 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1592 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1593 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001594}
1595
Ben Wagner3627d2e2018-06-26 14:23:20 -04001596static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001597 ImGui::DragCanvas dc(pt);
1598 dc.fillColor(IM_COL32(0, 0, 0, 128));
1599 dc.dragPoint(pt);
1600 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001601}
1602
Brian Osman9bb47cf2018-04-26 15:55:00 -04001603static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001604 ImGui::DragCanvas dc(pts);
1605 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001606
Brian Osman535c5e32019-02-09 16:32:58 -05001607 for (int i = 0; i < 4; ++i) {
1608 dc.dragPoint(pts + i);
1609 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001610
Brian Osman535c5e32019-02-09 16:32:58 -05001611 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1612 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1613 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1614 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001615
Brian Osman535c5e32019-02-09 16:32:58 -05001616 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001617}
1618
Brian Osmand67e5182017-12-08 16:46:09 -05001619void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001620 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1621 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001622 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001623 }
1624
1625 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001626 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1627 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001628 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001629 DisplayParams params = fWindow->getRequestedDisplayParams();
1630 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001631 const GrContext* ctx = fWindow->getGrContext();
1632
Brian Osmana109e392017-02-24 09:49:14 -05001633 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1634 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001635 if (ImGui::CollapsingHeader("Backend")) {
1636 int newBackend = static_cast<int>(fBackendType);
1637 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1638 ImGui::SameLine();
1639 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001640#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1641 ImGui::SameLine();
1642 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1643#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001644#if defined(SK_DAWN)
1645 ImGui::SameLine();
1646 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1647#endif
Brian Osman621491e2017-02-28 15:45:01 -05001648#if defined(SK_VULKAN)
1649 ImGui::SameLine();
1650 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1651#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001652#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001653 ImGui::SameLine();
1654 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1655#endif
Brian Osman621491e2017-02-28 15:45:01 -05001656 if (newBackend != fBackendType) {
1657 fDeferredActions.push_back([=]() {
1658 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1659 });
1660 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001661
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001662 bool* wire = &params.fGrContextOptions.fWireframeMode;
1663 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1664 paramsChanged = true;
1665 }
Brian Salomon99a33902017-03-07 15:16:34 -05001666
Brian Osman28b12522017-03-08 17:10:24 -05001667 if (ctx) {
1668 int sampleCount = fWindow->sampleCount();
1669 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001670 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001671 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1672 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1673 ImGui::RadioButton("16", &sampleCount, 16);
1674
1675 if (sampleCount != params.fMSAASampleCount) {
1676 params.fMSAASampleCount = sampleCount;
1677 paramsChanged = true;
1678 }
1679 }
1680
Ben Wagner37c54032018-04-13 14:30:23 -04001681 int pixelGeometryIdx = 0;
1682 if (fPixelGeometryOverrides) {
1683 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1684 }
1685 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1686 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1687 {
1688 uint32_t flags = params.fSurfaceProps.flags();
1689 if (pixelGeometryIdx == 0) {
1690 fPixelGeometryOverrides = false;
1691 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1692 } else {
1693 fPixelGeometryOverrides = true;
1694 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1695 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1696 }
1697 paramsChanged = true;
1698 }
1699
1700 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1701 if (ImGui::Checkbox("DFT", &useDFT)) {
1702 uint32_t flags = params.fSurfaceProps.flags();
1703 if (useDFT) {
1704 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1705 } else {
1706 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1707 }
1708 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1709 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1710 paramsChanged = true;
1711 }
1712
Brian Osman8a9de3d2017-03-01 14:59:05 -05001713 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001714 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001715 auto prButton = [&](GpuPathRenderers x) {
1716 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001717 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1718 params.fGrContextOptions.fGpuPathRenderers = x;
1719 paramsChanged = true;
1720 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001721 }
1722 };
1723
1724 if (!ctx) {
1725 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001726 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001727 const auto* caps = ctx->priv().caps();
1728 prButton(GpuPathRenderers::kDefault);
1729 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001730 if (caps->shaderCaps()->tessellationSupport()) {
1731 prButton(GpuPathRenderers::kGpuTessellation);
1732 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001733 if (caps->shaderCaps()->pathRenderingSupport()) {
1734 prButton(GpuPathRenderers::kStencilAndCover);
1735 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001736 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001737 if (1 == fWindow->sampleCount()) {
1738 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1739 prButton(GpuPathRenderers::kCoverageCounting);
1740 }
1741 prButton(GpuPathRenderers::kSmall);
1742 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001743 prButton(GpuPathRenderers::kTessellating);
1744 prButton(GpuPathRenderers::kNone);
1745 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001746 ImGui::TreePop();
1747 }
Brian Osman621491e2017-02-28 15:45:01 -05001748 }
1749
Ben Wagner964571d2019-03-08 12:35:06 -05001750 if (ImGui::CollapsingHeader("Tiling")) {
1751 ImGui::Checkbox("Enable", &fTiled);
1752 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1753 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1754 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1755 }
1756
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001757 if (ImGui::CollapsingHeader("Transform")) {
1758 float zoom = fZoomLevel;
1759 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1760 fZoomLevel = zoom;
1761 this->preTouchMatrixChanged();
1762 paramsChanged = true;
1763 }
1764 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001765 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001766 fRotation = deg;
1767 this->preTouchMatrixChanged();
1768 paramsChanged = true;
1769 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001770 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1771 if (ImGui_DragLocation(&fOffset)) {
1772 this->preTouchMatrixChanged();
1773 paramsChanged = true;
1774 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001775 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1776 this->preTouchMatrixChanged();
1777 paramsChanged = true;
1778 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001779 }
Brian Osman805a7272018-05-02 15:40:20 -04001780 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1781 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1782 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001783 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001784 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001785 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001786 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001787 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001788 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001789 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001790 }
1791
Ben Wagnera580fb32018-04-17 11:16:32 -04001792 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001793 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001794 if (fPaintOverrides.fAntiAlias) {
1795 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001796 }
1797 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001798 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001799 {
1800 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1801 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001802 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001803 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1804 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001805 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001806 fPaintOverrides.fAntiAlias = true;
1807 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001808 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001809 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001810 case SkPaintFields::AntiAliasState::Alias:
1811 break;
1812 case SkPaintFields::AntiAliasState::Normal:
1813 break;
1814 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1815 gSkUseAnalyticAA = true;
1816 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001817 break;
1818 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1819 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001820 break;
1821 }
1822 }
1823 paramsChanged = true;
1824 }
1825
Ben Wagner99a78dc2018-05-09 18:23:51 -04001826 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001827 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001828 bool (SkPaint::* isFlag)() const,
1829 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001830 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001831 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001832 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001833 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001834 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001835 if (ImGui::Combo(label, &itemIndex, items)) {
1836 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001837 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001838 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001839 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001840 (fPaint.*setFlag)(itemIndex == 2);
1841 }
1842 paramsChanged = true;
1843 }
1844 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001845
Ben Wagner99a78dc2018-05-09 18:23:51 -04001846 paintFlag("Dither",
1847 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001848 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001849 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001850
1851 int filterQualityIdx = 0;
1852 if (fPaintOverrides.fFilterQuality) {
1853 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1854 }
1855 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1856 "Default\0None\0Low\0Medium\0High\0\0"))
1857 {
1858 if (filterQualityIdx == 0) {
1859 fPaintOverrides.fFilterQuality = false;
1860 fPaint.setFilterQuality(kNone_SkFilterQuality);
1861 } else {
1862 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1863 fPaintOverrides.fFilterQuality = true;
1864 }
1865 paramsChanged = true;
1866 }
Ben Wagner9613e452019-01-23 10:34:59 -05001867 }
Hal Canary02738a82019-01-21 18:51:32 +00001868
Ben Wagner9613e452019-01-23 10:34:59 -05001869 if (ImGui::CollapsingHeader("Font")) {
1870 int hintingIdx = 0;
1871 if (fFontOverrides.fHinting) {
1872 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1873 }
1874 if (ImGui::Combo("Hinting", &hintingIdx,
1875 "Default\0None\0Slight\0Normal\0Full\0\0"))
1876 {
1877 if (hintingIdx == 0) {
1878 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001879 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001880 } else {
1881 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1882 fFontOverrides.fHinting = true;
1883 }
1884 paramsChanged = true;
1885 }
Hal Canary02738a82019-01-21 18:51:32 +00001886
Ben Wagner9613e452019-01-23 10:34:59 -05001887 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1888 bool SkFontFields::* flag,
1889 bool (SkFont::* isFlag)() const,
1890 void (SkFont::* setFlag)(bool) )
1891 {
1892 int itemIndex = 0;
1893 if (fFontOverrides.*flag) {
1894 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1895 }
1896 if (ImGui::Combo(label, &itemIndex, items)) {
1897 if (itemIndex == 0) {
1898 fFontOverrides.*flag = false;
1899 } else {
1900 fFontOverrides.*flag = true;
1901 (fFont.*setFlag)(itemIndex == 2);
1902 }
1903 paramsChanged = true;
1904 }
1905 };
Hal Canary02738a82019-01-21 18:51:32 +00001906
Ben Wagner9613e452019-01-23 10:34:59 -05001907 fontFlag("Fake Bold Glyphs",
1908 "Default\0No Fake Bold\0Fake Bold\0\0",
1909 &SkFontFields::fEmbolden,
1910 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001911
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001912 fontFlag("Baseline Snapping",
1913 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1914 &SkFontFields::fBaselineSnap,
1915 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1916
Ben Wagner9613e452019-01-23 10:34:59 -05001917 fontFlag("Linear Text",
1918 "Default\0No Linear Text\0Linear Text\0\0",
1919 &SkFontFields::fLinearMetrics,
1920 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001921
Ben Wagner9613e452019-01-23 10:34:59 -05001922 fontFlag("Subpixel Position Glyphs",
1923 "Default\0Pixel Text\0Subpixel Text\0\0",
1924 &SkFontFields::fSubpixel,
1925 &SkFont::isSubpixel, &SkFont::setSubpixel);
1926
1927 fontFlag("Embedded Bitmap Text",
1928 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1929 &SkFontFields::fEmbeddedBitmaps,
1930 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1931
1932 fontFlag("Force Auto-Hinting",
1933 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1934 &SkFontFields::fForceAutoHinting,
1935 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1936
1937 int edgingIdx = 0;
1938 if (fFontOverrides.fEdging) {
1939 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1940 }
1941 if (ImGui::Combo("Edging", &edgingIdx,
1942 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1943 {
1944 if (edgingIdx == 0) {
1945 fFontOverrides.fEdging = false;
1946 fFont.setEdging(SkFont::Edging::kAlias);
1947 } else {
1948 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1949 fFontOverrides.fEdging = true;
1950 }
1951 paramsChanged = true;
1952 }
1953
Ben Wagner15a8d572019-03-21 13:35:44 -04001954 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1955 if (fFontOverrides.fSize) {
1956 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001957 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001958 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001959 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001960 fFontOverrides.fSizeRange[0],
1961 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001962 "%.6f", 2.0f))
1963 {
Mike Reed3ae47332019-01-04 10:11:46 -05001964 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001965 paramsChanged = true;
1966 }
1967 }
1968
1969 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1970 if (fFontOverrides.fScaleX) {
1971 float scaleX = fFont.getScaleX();
1972 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1973 fFont.setScaleX(scaleX);
1974 paramsChanged = true;
1975 }
1976 }
1977
1978 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1979 if (fFontOverrides.fSkewX) {
1980 float skewX = fFont.getSkewX();
1981 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1982 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001983 paramsChanged = true;
1984 }
1985 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001986 }
1987
Mike Reed81f60ec2018-05-15 10:09:52 -04001988 {
1989 SkMetaData controls;
1990 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1991 if (ImGui::CollapsingHeader("Current Slide")) {
1992 SkMetaData::Iter iter(controls);
1993 const char* name;
1994 SkMetaData::Type type;
1995 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001996 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001997 if (type == SkMetaData::kScalar_Type) {
1998 float val[3];
1999 SkASSERT(count == 3);
2000 controls.findScalars(name, &count, val);
2001 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2002 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002003 }
Ben Wagner110c7032019-03-22 17:03:59 -04002004 } else if (type == SkMetaData::kBool_Type) {
2005 bool val;
2006 SkASSERT(count == 1);
2007 controls.findBool(name, &val);
2008 if (ImGui::Checkbox(name, &val)) {
2009 controls.setBool(name, val);
2010 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002011 }
2012 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002013 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002014 }
2015 }
2016 }
2017
Ben Wagner7a3c6742018-04-23 10:01:07 -04002018 if (fShowSlidePicker) {
2019 ImGui::SetNextTreeNodeOpen(true);
2020 }
Brian Osman79086b92017-02-10 13:36:16 -05002021 if (ImGui::CollapsingHeader("Slide")) {
2022 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002023 static ImVector<const char*> filteredSlideNames;
2024 static ImVector<int> filteredSlideIndices;
2025
Brian Osmanfce09c52017-11-14 15:32:20 -05002026 if (fShowSlidePicker) {
2027 ImGui::SetKeyboardFocusHere();
2028 fShowSlidePicker = false;
2029 }
2030
Brian Osman79086b92017-02-10 13:36:16 -05002031 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002032 filteredSlideNames.clear();
2033 filteredSlideIndices.clear();
2034 int filteredIndex = 0;
2035 for (int i = 0; i < fSlides.count(); ++i) {
2036 const char* slideName = fSlides[i]->getName().c_str();
2037 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2038 if (i == fCurrentSlide) {
2039 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002040 }
Brian Osmanf479e422017-11-08 13:11:36 -05002041 filteredSlideNames.push_back(slideName);
2042 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002043 }
Brian Osman79086b92017-02-10 13:36:16 -05002044 }
Brian Osmanf479e422017-11-08 13:11:36 -05002045
Brian Osmanf479e422017-11-08 13:11:36 -05002046 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2047 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002048 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002049 }
2050 }
Brian Osmana109e392017-02-24 09:49:14 -05002051
2052 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002053 ColorMode newMode = fColorMode;
2054 auto cmButton = [&](ColorMode mode, const char* label) {
2055 if (ImGui::RadioButton(label, mode == fColorMode)) {
2056 newMode = mode;
2057 }
2058 };
2059
2060 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002061 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2062 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002063 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002064
2065 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002066 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002067 }
2068
2069 // Pick from common gamuts:
2070 int primariesIdx = 4; // Default: Custom
2071 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2072 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2073 primariesIdx = i;
2074 break;
2075 }
2076 }
2077
Brian Osman03115dc2018-11-26 13:55:19 -05002078 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002079 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002080
Brian Osmana109e392017-02-24 09:49:14 -05002081 if (ImGui::Combo("Primaries", &primariesIdx,
2082 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2083 if (primariesIdx >= 0 && primariesIdx <= 3) {
2084 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2085 }
2086 }
2087
2088 // Allow direct editing of gamut
2089 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2090 }
Brian Osman207d4102019-01-10 09:40:58 -05002091
2092 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002093 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002094 if (ImGui::Checkbox("Pause", &isPaused)) {
2095 fAnimTimer.togglePauseResume();
2096 }
Brian Osman707d2022019-01-10 11:27:34 -05002097
2098 float speed = fAnimTimer.getSpeed();
2099 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2100 fAnimTimer.setSpeed(speed);
2101 }
Brian Osman207d4102019-01-10 09:40:58 -05002102 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002103
Brian Osmanfd7657c2019-04-25 11:34:07 -04002104 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2105#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2106 || Window::kANGLE_BackendType == fBackendType
2107#endif
2108 ;
2109
2110 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2111 // than GL, we need to force it on. Just do that on the first frame after the backend
2112 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002113 if (!backendIsGL &&
2114 params.fGrContextOptions.fShaderCacheStrategy !=
2115 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2116 params.fGrContextOptions.fShaderCacheStrategy =
2117 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002118 paramsChanged = true;
2119 fPersistentCache.reset();
2120 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002121 // To re-load shaders from the currently active programs, we flush all caches on one
2122 // frame, then set a flag to poll the cache on the next frame.
2123 static bool gLoadPending = false;
2124 if (gLoadPending) {
2125 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2126 int hitCount) {
2127 CachedGLSL& entry(fCachedGLSL.push_back());
2128 entry.fKey = key;
2129 SkMD5 hash;
2130 hash.write(key->bytes(), key->size());
2131 SkMD5::Digest digest = hash.finish();
2132 for (int i = 0; i < 16; ++i) {
2133 entry.fKeyString.appendf("%02x", digest.data[i]);
2134 }
2135
Brian Osmana66081d2019-09-03 14:59:26 -04002136 SkReader32 reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002137 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002138 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2139 entry.fInputs,
2140 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002141 };
2142 fCachedGLSL.reset();
2143 fPersistentCache.foreach(collectShaders);
2144 gLoadPending = false;
2145 }
2146
2147 // Defer actually doing the load/save logic so that we can trigger a save when we
2148 // start or finish hovering on a tree node in the list below:
2149 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002150 bool doSave = ImGui::Button("Save");
2151 if (backendIsGL) {
2152 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002153 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2154 GrContextOptions::ShaderCacheStrategy::kSkSL;
2155 if (ImGui::Checkbox("SkSL", &sksl)) {
2156 params.fGrContextOptions.fShaderCacheStrategy = sksl
2157 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2158 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002159 paramsChanged = true;
2160 doLoad = true;
2161 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2162 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002163 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002164
2165 ImGui::BeginChild("##ScrollingRegion");
2166 for (auto& entry : fCachedGLSL) {
2167 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2168 bool hovered = ImGui::IsItemHovered();
2169 if (hovered != entry.fHovered) {
2170 // Force a save to patch the highlight shader in/out
2171 entry.fHovered = hovered;
2172 doSave = true;
2173 }
2174 if (inTreeNode) {
2175 // Full width, and a reasonable amount of space for each shader.
2176 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2177 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2178 boxSize);
2179 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2180 boxSize);
2181 ImGui::TreePop();
2182 }
2183 }
2184 ImGui::EndChild();
2185
2186 if (doLoad) {
2187 fPersistentCache.reset();
2188 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2189 gLoadPending = true;
2190 }
2191 if (doSave) {
2192 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002193 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002194 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2195 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002196
Brian Osman072e6fc2019-06-12 11:35:41 -04002197 SkSL::String highlight;
2198 if (!sksl) {
2199 highlight = shaderCaps->versionDeclString();
2200 if (shaderCaps->usesPrecisionModifiers()) {
2201 highlight.append("precision mediump float;\n");
2202 }
Brian Osman5bee3902019-05-07 09:55:45 -04002203 }
2204 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002205 highlight.appendf("out %s sk_FragColor;\n"
2206 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2207 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002208
2209 fPersistentCache.reset();
2210 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2211 for (auto& entry : fCachedGLSL) {
2212 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2213 if (entry.fHovered) {
2214 entry.fShader[kFragment_GrShaderType] = highlight;
2215 }
2216
Brian Osmana085a412019-04-25 09:44:43 -04002217 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2218 entry.fShader,
2219 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002220 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002221 fPersistentCache.store(*entry.fKey, *data);
2222
2223 entry.fShader[kFragment_GrShaderType] = backup;
2224 }
2225 }
2226 }
Brian Osman79086b92017-02-10 13:36:16 -05002227 }
Brian Salomon99a33902017-03-07 15:16:34 -05002228 if (paramsChanged) {
2229 fDeferredActions.push_back([=]() {
2230 fWindow->setRequestedDisplayParams(params);
2231 fWindow->inval();
2232 this->updateTitle();
2233 });
2234 }
Brian Osman79086b92017-02-10 13:36:16 -05002235 ImGui::End();
2236 }
2237
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002238 if (gShaderErrorHandler.fErrors.count()) {
2239 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2240 ImGui::Begin("Shader Errors");
2241 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2242 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002243 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2244 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2245 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2246 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002247 }
2248 ImGui::End();
2249 gShaderErrorHandler.reset();
2250 }
2251
Brian Osmanf6877092017-02-13 09:39:57 -05002252 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002253 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2254 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002255 static int zoomFactor = 8;
2256 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002257 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002258 }
2259 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2260 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002261 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002262 }
Brian Osmanf6877092017-02-13 09:39:57 -05002263
Ben Wagner3627d2e2018-06-26 14:23:20 -04002264 if (!fZoomWindowFixed) {
2265 ImVec2 mousePos = ImGui::GetMousePos();
2266 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2267 }
2268 SkScalar x = fZoomWindowLocation.x();
2269 SkScalar y = fZoomWindowLocation.y();
2270 int xInt = SkScalarRoundToInt(x);
2271 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002272 ImVec2 avail = ImGui::GetContentRegionAvail();
2273
Brian Osmanead517d2017-11-13 15:36:36 -05002274 uint32_t pixel = 0;
2275 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002276 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002277 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002278 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002279 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002280 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002281 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2282 }
2283
Brian Osmand67e5182017-12-08 16:46:09 -05002284 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002285 // Translate so the region of the image that's under the mouse cursor is centered
2286 // in the zoom canvas:
2287 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002288 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2289 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002290 c->drawImage(this->fLastImage, 0, 0);
2291
2292 SkPaint outline;
2293 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002294 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002295 });
Brian Osmanf6877092017-02-13 09:39:57 -05002296 }
2297
2298 ImGui::End();
2299 }
Brian Osman79086b92017-02-10 13:36:16 -05002300}
2301
liyuqian2edb0f42016-07-06 14:11:32 -07002302void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002303 for (int i = 0; i < fDeferredActions.count(); ++i) {
2304 fDeferredActions[i]();
2305 }
2306 fDeferredActions.reset();
2307
Brian Osman56a24812017-12-19 11:15:16 -05002308 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002309 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002310 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002311 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002312
Brian Osman79086b92017-02-10 13:36:16 -05002313 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002314 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2315 // not be visible, though. So we need to redraw if there is at least one visible window, or
2316 // more than one active window. Newly created windows are active but not visible for one frame
2317 // while they determine their layout and sizing.
2318 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2319 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002320 fWindow->inval();
2321 }
jvanverth9f372462016-04-06 06:08:59 -07002322}
liyuqiane5a6cd92016-05-27 08:52:52 -07002323
Florin Malitab632df72018-06-18 21:23:06 -04002324template <typename OptionsFunc>
2325static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2326 OptionsFunc&& optionsFunc) {
2327 writer.beginObject();
2328 {
2329 writer.appendString(kName , name);
2330 writer.appendString(kValue, value);
2331
2332 writer.beginArray(kOptions);
2333 {
2334 optionsFunc(writer);
2335 }
2336 writer.endArray();
2337 }
2338 writer.endObject();
2339}
2340
2341
liyuqiane5a6cd92016-05-27 08:52:52 -07002342void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002343 if (!fWindow) {
2344 return;
2345 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002346 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002347 return; // Surface hasn't been created yet.
2348 }
2349
Florin Malitab632df72018-06-18 21:23:06 -04002350 SkDynamicMemoryWStream memStream;
2351 SkJSONWriter writer(&memStream);
2352 writer.beginArray();
2353
liyuqianb73c24b2016-06-03 08:47:23 -07002354 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002355 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2356 [this](SkJSONWriter& writer) {
2357 for(const auto& slide : fSlides) {
2358 writer.appendString(slide->getName().c_str());
2359 }
2360 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002361
liyuqianb73c24b2016-06-03 08:47:23 -07002362 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002363 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2364 [](SkJSONWriter& writer) {
2365 for (const auto& str : kBackendTypeStrings) {
2366 writer.appendString(str);
2367 }
2368 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002369
csmartdalton578f0642017-02-24 16:04:47 -07002370 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002371 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2372 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2373 [this](SkJSONWriter& writer) {
2374 writer.appendS32(0);
2375
2376 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2377 return;
2378 }
2379
2380 for (int msaa : {4, 8, 16}) {
2381 writer.appendS32(msaa);
2382 }
2383 });
csmartdalton578f0642017-02-24 16:04:47 -07002384
csmartdalton61cd31a2017-02-27 17:00:53 -07002385 // Path renderer state
2386 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002387 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2388 [this](SkJSONWriter& writer) {
2389 const GrContext* ctx = fWindow->getGrContext();
2390 if (!ctx) {
2391 writer.appendString("Software");
2392 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002393 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002394 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2395 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002396 if (caps->shaderCaps()->tessellationSupport()) {
2397 writer.appendString(
2398 gPathRendererNames[GpuPathRenderers::kGpuTessellation].c_str());
2399 }
Florin Malitab632df72018-06-18 21:23:06 -04002400 if (caps->shaderCaps()->pathRenderingSupport()) {
2401 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002402 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002403 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002404 }
2405 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002406 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2407 writer.appendString(
2408 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2409 }
2410 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2411 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002412 writer.appendString(gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2413 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002414 }
2415 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002416
liyuqianb73c24b2016-06-03 08:47:23 -07002417 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002418 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2419 [this](SkJSONWriter& writer) {
2420 writer.appendString(kSoftkeyHint);
2421 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2422 writer.appendString(softkey.c_str());
2423 }
2424 });
liyuqianb73c24b2016-06-03 08:47:23 -07002425
Florin Malitab632df72018-06-18 21:23:06 -04002426 writer.endArray();
2427 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002428
Florin Malitab632df72018-06-18 21:23:06 -04002429 auto data = memStream.detachAsData();
2430
2431 // TODO: would be cool to avoid this copy
2432 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2433
2434 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002435}
2436
2437void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002438 // For those who will add more features to handle the state change in this function:
2439 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2440 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2441 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002442 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002443 for (int i = 0; i < fSlides.count(); ++i) {
2444 if (fSlides[i]->getName().equals(stateValue)) {
2445 this->setCurrentSlide(i);
2446 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002447 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002448 }
Florin Malitaab99c342018-01-16 16:23:03 -05002449
2450 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002451 } else if (stateName.equals(kBackendStateName)) {
2452 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2453 if (stateValue.equals(kBackendTypeStrings[i])) {
2454 if (fBackendType != i) {
2455 fBackendType = (sk_app::Window::BackendType)i;
2456 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002457 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002458 }
2459 break;
2460 }
2461 }
csmartdalton578f0642017-02-24 16:04:47 -07002462 } else if (stateName.equals(kMSAAStateName)) {
2463 DisplayParams params = fWindow->getRequestedDisplayParams();
2464 int sampleCount = atoi(stateValue.c_str());
2465 if (sampleCount != params.fMSAASampleCount) {
2466 params.fMSAASampleCount = sampleCount;
2467 fWindow->setRequestedDisplayParams(params);
2468 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002469 this->updateTitle();
2470 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002471 }
2472 } else if (stateName.equals(kPathRendererStateName)) {
2473 DisplayParams params = fWindow->getRequestedDisplayParams();
2474 for (const auto& pair : gPathRendererNames) {
2475 if (pair.second == stateValue.c_str()) {
2476 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2477 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2478 fWindow->setRequestedDisplayParams(params);
2479 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002480 this->updateTitle();
2481 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002482 }
2483 break;
2484 }
csmartdalton578f0642017-02-24 16:04:47 -07002485 }
liyuqianb73c24b2016-06-03 08:47:23 -07002486 } else if (stateName.equals(kSoftkeyStateName)) {
2487 if (!stateValue.equals(kSoftkeyHint)) {
2488 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002489 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002490 }
liyuqian2edb0f42016-07-06 14:11:32 -07002491 } else if (stateName.equals(kRefreshStateName)) {
2492 // This state is actually NOT in the UI state.
2493 // We use this to allow Android to quickly set bool fRefresh.
2494 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002495 } else {
2496 SkDebugf("Unknown stateName: %s", stateName.c_str());
2497 }
2498}
Brian Osman79086b92017-02-10 13:36:16 -05002499
Hal Canaryb1f411a2019-08-29 10:39:22 -04002500bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002501 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002502}
2503
Hal Canaryb1f411a2019-08-29 10:39:22 -04002504bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002505 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002506 fWindow->inval();
2507 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002508 } else {
2509 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002510 }
Brian Osman79086b92017-02-10 13:36:16 -05002511}