blob: 9a857211e4db8e6fdf09d579541c229b9c4125bd [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"
Robert Phillipsed653392020-07-10 13:55:21 -040014#include "include/gpu/GrDirectContext.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050015#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkTo.h"
17#include "include/utils/SkPaintFilterCanvas.h"
18#include "src/core/SkColorSpacePriv.h"
19#include "src/core/SkImagePriv.h"
20#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkOSFile.h"
John Stilesf2c2d302021-04-09 17:56:58 -040022#include "src/core/SkReadBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkScan.h"
John Stilesdf078002020-07-14 09:44:57 -040024#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040026#include "src/core/SkTextBlobPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040027#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrGpu.h"
29#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070030#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Chris Daltonff18ff62020-12-07 17:39:26 -070032#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Adlai Hollerbcfc5542020-08-27 12:44:07 -040033#include "src/image/SkImage_Base.h"
John Stiles2ee4d7a2021-03-30 10:30:47 -040034#include "src/sksl/SkSLCompiler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/utils/SkJSONWriter.h"
36#include "src/utils/SkOSPath.h"
37#include "tools/Resources.h"
38#include "tools/ToolUtils.h"
39#include "tools/flags/CommandLineFlags.h"
40#include "tools/flags/CommonFlags.h"
41#include "tools/trace/EventTracingPriv.h"
42#include "tools/viewer/BisectSlide.h"
43#include "tools/viewer/GMSlide.h"
44#include "tools/viewer/ImageSlide.h"
45#include "tools/viewer/ParticlesSlide.h"
46#include "tools/viewer/SKPSlide.h"
47#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050048#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "tools/viewer/SlideDir.h"
50#include "tools/viewer/SvgSlide.h"
51#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070052
Chris Dalton17dc4182020-03-25 16:18:16 -060053#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040054#include <map>
55
Hal Canary8a001442018-09-19 11:31:27 -040056#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040057#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040058
Brian Osmanc85f1fa2020-06-16 15:11:34 -040059#ifdef SK_VULKAN
60#include "spirv-tools/libspirv.hpp"
61#endif
62
Florin Malita87ccf332018-05-04 12:23:24 -040063#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050064 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040065#endif
Florin Malita45cd2002020-06-09 14:00:54 -040066#if defined(SK_ENABLE_SKRIVE)
67 #include "tools/viewer/SkRiveSlide.h"
68#endif
Florin Malita87ccf332018-05-04 12:23:24 -040069
Brian Osman5e7fbfd2019-05-03 13:13:35 -040070class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
71public:
72 void compileError(const char* shader, const char* errors) override {
73 fShaders.push_back(SkString(shader));
74 fErrors.push_back(SkString(errors));
75 }
76
77 void reset() {
78 fShaders.reset();
79 fErrors.reset();
80 }
81
82 SkTArray<SkString> fShaders;
83 SkTArray<SkString> fErrors;
84};
85
86static CapturingShaderErrorHandler gShaderErrorHandler;
87
Brian Osmanf847f312020-06-18 14:18:27 -040088GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
89
jvanverth34524262016-05-04 13:49:13 -070090using namespace sk_app;
John Stiles2ee4d7a2021-03-30 10:30:47 -040091using SkSL::Compiler;
92using OverrideFlag = SkSL::Compiler::OverrideFlag;
jvanverth34524262016-05-04 13:49:13 -070093
csmartdalton61cd31a2017-02-27 17:00:53 -070094static std::map<GpuPathRenderers, std::string> gPathRendererNames;
95
jvanverth9f372462016-04-06 06:08:59 -070096Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070097 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070098}
99
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600100static DEFINE_string(slide, "", "Start on this sample.");
101static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -0500102
Jim Van Verth682a2f42020-05-13 16:54:09 -0400103#ifdef SK_GL
104#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700105#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400106#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700107#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400108#ifdef SK_VULKAN
109#define VK_BACKEND_STR ", \"vk\""
110#else
111#define VK_BACKEND_STR
112#endif
113#ifdef SK_METAL
114#define MTL_BACKEND_STR ", \"mtl\""
115#else
116#define MTL_BACKEND_STR
117#endif
118#ifdef SK_DIRECT3D
119#define D3D_BACKEND_STR ", \"d3d\""
120#else
121#define D3D_BACKEND_STR
122#endif
123#ifdef SK_DAWN
124#define DAWN_BACKEND_STR ", \"dawn\""
125#else
126#define DAWN_BACKEND_STR
127#endif
128#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
129#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
130 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700131
Brian Osman2dd96932016-10-18 15:33:53 -0400132static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700133
Mike Klein5b3f3432019-03-21 11:42:21 -0500134static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
Chris Daltonfd708652021-03-17 21:10:17 -0600135static DEFINE_bool(dmsaa, false, "Use internal MSAA to render to non-MSAA surfaces?");
csmartdalton008b9d82017-02-22 12:00:42 -0700136
Mike Klein84836b72019-03-21 11:31:36 -0500137static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700138
Mike Klein84836b72019-03-21 11:31:36 -0500139static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400140
Mike Kleinc6142d82019-03-25 10:54:59 -0500141static DEFINE_string2(match, m, nullptr,
142 "[~][^]substring[$] [...] of name to run.\n"
143 "Multiple matches may be separated by spaces.\n"
144 "~ causes a matching name to always be skipped\n"
145 "^ requires the start of the name to match\n"
146 "$ requires the end of the name to match\n"
147 "^ and $ requires an exact match\n"
148 "If a name does not match any list entry,\n"
149 "it is skipped unless some list entry starts with ~");
150
Mike Klein19fb3972019-03-21 13:08:08 -0500151#if defined(SK_BUILD_FOR_ANDROID)
152 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500153 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
154 static DEFINE_string(lotties, "/data/local/tmp/lotties",
155 "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400156 static DEFINE_string(rives, "/data/local/tmp/rives",
157 "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500158#else
159 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500160 static DEFINE_string(skps, "skps", "Directory to read skps from.");
161 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400162 static DEFINE_string(rives, "rives", "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500163#endif
164
Mike Kleinc6142d82019-03-25 10:54:59 -0500165static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
166
167static DEFINE_int_2(threads, j, -1,
168 "Run threadsafe tests on a threadpool with this many extra threads, "
169 "defaulting to one extra thread per core.");
170
Jim Van Verth7b558182019-11-14 16:47:01 -0500171static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
172
Chris Daltonc8877332020-01-06 09:48:30 -0700173static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500174static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
175static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500176static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500177static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500178static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500179
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400180#ifndef SK_GL
181static_assert(false, "viewer requires GL backend for raster.")
182#endif
183
Brian Salomon194db172017-08-17 14:37:06 -0400184const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700185 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400186#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
187 "ANGLE",
188#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400189#ifdef SK_DAWN
190 "Dawn",
191#endif
jvanverth063ece72016-06-17 09:29:14 -0700192#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700193 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700194#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400195#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500196 "Metal",
197#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400198#ifdef SK_DIRECT3D
199 "Direct3D",
200#endif
csmartdalton578f0642017-02-24 16:04:47 -0700201 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700202};
203
bsalomon6c471f72016-07-26 12:56:32 -0700204static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400205#ifdef SK_DAWN
206 if (0 == strcmp(str, "dawn")) {
207 return sk_app::Window::kDawn_BackendType;
208 } else
209#endif
bsalomon6c471f72016-07-26 12:56:32 -0700210#ifdef SK_VULKAN
211 if (0 == strcmp(str, "vk")) {
212 return sk_app::Window::kVulkan_BackendType;
213 } else
214#endif
Brian Salomon194db172017-08-17 14:37:06 -0400215#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
216 if (0 == strcmp(str, "angle")) {
217 return sk_app::Window::kANGLE_BackendType;
218 } else
219#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400220#ifdef SK_METAL
221 if (0 == strcmp(str, "mtl")) {
222 return sk_app::Window::kMetal_BackendType;
223 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500224#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400225#ifdef SK_DIRECT3D
226 if (0 == strcmp(str, "d3d")) {
227 return sk_app::Window::kDirect3D_BackendType;
228 } else
229#endif
230
bsalomon6c471f72016-07-26 12:56:32 -0700231 if (0 == strcmp(str, "gl")) {
232 return sk_app::Window::kNativeGL_BackendType;
233 } else if (0 == strcmp(str, "sw")) {
234 return sk_app::Window::kRaster_BackendType;
235 } else {
236 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
237 return sk_app::Window::kRaster_BackendType;
238 }
239}
240
Brian Osmana109e392017-02-24 09:49:14 -0500241static SkColorSpacePrimaries gSrgbPrimaries = {
242 0.64f, 0.33f,
243 0.30f, 0.60f,
244 0.15f, 0.06f,
245 0.3127f, 0.3290f };
246
247static SkColorSpacePrimaries gAdobePrimaries = {
248 0.64f, 0.33f,
249 0.21f, 0.71f,
250 0.15f, 0.06f,
251 0.3127f, 0.3290f };
252
253static SkColorSpacePrimaries gP3Primaries = {
254 0.680f, 0.320f,
255 0.265f, 0.690f,
256 0.150f, 0.060f,
257 0.3127f, 0.3290f };
258
259static SkColorSpacePrimaries gRec2020Primaries = {
260 0.708f, 0.292f,
261 0.170f, 0.797f,
262 0.131f, 0.046f,
263 0.3127f, 0.3290f };
264
265struct NamedPrimaries {
266 const char* fName;
267 SkColorSpacePrimaries* fPrimaries;
268} gNamedPrimaries[] = {
269 { "sRGB", &gSrgbPrimaries },
270 { "AdobeRGB", &gAdobePrimaries },
271 { "P3", &gP3Primaries },
272 { "Rec. 2020", &gRec2020Primaries },
273};
274
275static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
276 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
277}
278
Brian Osman70d2f432017-11-08 09:54:10 -0500279static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
280 // In raster mode, we still use GL for the window.
281 // This lets us render the GUI faster (and correct).
282 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
283}
284
Jim Van Verth74826c82019-03-01 14:37:30 -0500285class NullSlide : public Slide {
286 SkISize getDimensions() const override {
287 return SkISize::Make(640, 480);
288 }
289
290 void draw(SkCanvas* canvas) override {
291 canvas->clear(0xffff11ff);
292 }
293};
294
John Stiles31964fd2020-05-05 16:05:47 -0400295static const char kName[] = "name";
296static const char kValue[] = "value";
297static const char kOptions[] = "options";
298static const char kSlideStateName[] = "Slide";
299static const char kBackendStateName[] = "Backend";
300static const char kMSAAStateName[] = "MSAA";
301static const char kPathRendererStateName[] = "Path renderer";
302static const char kSoftkeyStateName[] = "Softkey";
303static const char kSoftkeyHint[] = "Please select a softkey";
304static const char kON[] = "ON";
305static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700306
Mike Reed862818b2020-03-21 15:07:13 -0400307extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500308extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500309extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400310
jvanverth34524262016-05-04 13:49:13 -0700311Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500312 : fCurrentSlide(-1)
313 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500314 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400315 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500316 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500317 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500318 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500319 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400320 , fZoomWindowFixed(false)
321 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500322 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400323 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700324 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500325 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500326 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500327 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500328 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500329 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700330 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400331 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400332 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400333 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500334 , fTiled(false)
335 , fDrawTileBoundaries(false)
336 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400337 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700338{
Greg Daniel285db442016-10-14 09:12:53 -0400339 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700340
Chris Dalton37ae4b02019-12-28 14:51:11 -0700341 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600342 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500343 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600344 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600345 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500346 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700347
jvanverth2bb3b6d2016-04-08 07:24:09 -0700348 SkDebugf("Command line arguments: ");
349 for (int i = 1; i < argc; ++i) {
350 SkDebugf("%s ", argv[i]);
351 }
352 SkDebugf("\n");
353
Mike Klein88544fb2019-03-20 10:50:33 -0500354 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500355#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400356 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500357#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700358
Mike Reed862818b2020-03-21 15:07:13 -0400359 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500360 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500361 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400362
Mike Klein19cc0f62019-03-22 15:30:07 -0500363 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500364
Brian Osmanbc8150f2017-07-24 11:38:01 -0400365 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400366 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400367
bsalomon6c471f72016-07-26 12:56:32 -0700368 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700369 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700370
csmartdalton578f0642017-02-24 16:04:47 -0700371 DisplayParams displayParams;
372 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500373 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700374 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400375 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400376 displayParams.fGrContextOptions.fShaderCacheStrategy =
John Stiles2ee4d7a2021-03-30 10:30:47 -0400377 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400378 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
379 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltonfd708652021-03-17 21:10:17 -0600380 displayParams.fGrContextOptions.fAlwaysAntialias = FLAGS_dmsaa;
csmartdalton578f0642017-02-24 16:04:47 -0700381 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400382 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500383 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700384
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500385 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500386 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500387
Brian Osman56a24812017-12-19 11:15:16 -0500388 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500389 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500390 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
391 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
392 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
393
jvanverth9f372462016-04-06 06:08:59 -0700394 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700395 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500396 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500397 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500398 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700399
brianosman622c8d52016-05-10 06:50:49 -0700400 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500401 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
402 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
403 fWindow->inval();
404 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500405 // Command to jump directly to the slide picker and give it focus
406 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
407 this->fShowImGuiDebugWindow = true;
408 this->fShowSlidePicker = true;
409 fWindow->inval();
410 });
411 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400412 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500413 this->fShowImGuiDebugWindow = true;
414 this->fShowSlidePicker = true;
415 fWindow->inval();
416 });
Brian Osman79086b92017-02-10 13:36:16 -0500417 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
418 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
419 fWindow->inval();
420 });
Brian Osmanf6877092017-02-13 09:39:57 -0500421 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
422 this->fShowZoomWindow = !this->fShowZoomWindow;
423 fWindow->inval();
424 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400425 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
426 this->fZoomWindowFixed = !this->fZoomWindowFixed;
427 fWindow->inval();
428 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400429 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400430 DisplayParams params = fWindow->getRequestedDisplayParams();
431 params.fDisableVsync = !params.fDisableVsync;
432 fWindow->setRequestedDisplayParams(params);
433 this->updateTitle();
434 fWindow->inval();
435 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400436 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
437 DisplayParams params = fWindow->getRequestedDisplayParams();
438 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
439 fWindow->setRequestedDisplayParams(params);
440 this->updateTitle();
441 fWindow->inval();
442 });
Mike Reedf702ed42019-07-22 17:00:49 -0400443 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
444 fRefresh = !fRefresh;
445 fWindow->inval();
446 });
brianosman622c8d52016-05-10 06:50:49 -0700447 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500448 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700449 fWindow->inval();
450 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400451 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500452 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400453 this->updateTitle();
454 fWindow->inval();
455 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500456 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500457 switch (fColorMode) {
458 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500459 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500460 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500461 case ColorMode::kColorManaged8888:
462 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500463 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500464 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400465 this->setColorMode(ColorMode::kColorManagedF16Norm);
466 break;
467 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500468 this->setColorMode(ColorMode::kLegacy);
469 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500470 }
brianosman622c8d52016-05-10 06:50:49 -0700471 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700472 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
473 DisplayParams params = fWindow->getRequestedDisplayParams();
474 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
475 fWindow->setRequestedDisplayParams(params);
476 fWindow->inval();
477 });
Brian Salomon91216d52021-04-09 11:57:59 -0400478 fCommands.addCommand('w', "Modes", "Toggle reduced shaders", [this]() {
479 DisplayParams params = fWindow->getRequestedDisplayParams();
480 params.fGrContextOptions.fReducedShaderVariations =
481 !params.fGrContextOptions.fReducedShaderVariations;
482 fWindow->setRequestedDisplayParams(params);
483 fWindow->inval();
484 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400485 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500486 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700487 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400488 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500489 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700490 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400491 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700492 this->changeZoomLevel(1.f / 32.f);
493 fWindow->inval();
494 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400495 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700496 this->changeZoomLevel(-1.f / 32.f);
497 fWindow->inval();
498 });
jvanverthaf236b52016-05-20 06:01:06 -0700499 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400500 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
501 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500502 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400503#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
504 if (newBackend == sk_app::Window::kVulkan_BackendType) {
505 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
506 sk_app::Window::kBackendTypeCount);
507 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
508 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500509 }
510#endif
Brian Osman621491e2017-02-28 15:45:01 -0500511 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700512 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500513 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
514 fSaveToSKP = true;
515 fWindow->inval();
516 });
Mike Reed376d8122019-03-14 11:39:02 -0400517 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
518 fShowSlideDimensions = !fShowSlideDimensions;
519 fWindow->inval();
520 });
Ben Wagner37c54032018-04-13 14:30:23 -0400521 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
522 DisplayParams params = fWindow->getRequestedDisplayParams();
523 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400524 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
525 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
526 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400527 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
528 } else {
529 switch (params.fSurfaceProps.pixelGeometry()) {
530 case kUnknown_SkPixelGeometry:
531 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
532 break;
533 case kRGB_H_SkPixelGeometry:
534 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
535 break;
536 case kBGR_H_SkPixelGeometry:
537 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
538 break;
539 case kRGB_V_SkPixelGeometry:
540 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
541 break;
542 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400543 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
544 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400545 break;
546 }
547 }
548 fWindow->setRequestedDisplayParams(params);
549 this->updateTitle();
550 fWindow->inval();
551 });
Ben Wagner9613e452019-01-23 10:34:59 -0500552 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500553 if (!fFontOverrides.fHinting) {
554 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400555 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500556 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500557 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400558 case SkFontHinting::kNone:
559 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500560 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400561 case SkFontHinting::kSlight:
562 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500563 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400564 case SkFontHinting::kNormal:
565 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500566 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400567 case SkFontHinting::kFull:
568 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500569 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500570 break;
571 }
572 }
573 this->updateTitle();
574 fWindow->inval();
575 });
576 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500577 if (!fPaintOverrides.fAntiAlias) {
578 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
579 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500580 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500581 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500582 } else {
583 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500584 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500585 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500586 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400587 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500588 break;
589 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500590 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500591 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400592 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500593 break;
594 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500595 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400596 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500597 break;
598 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500599 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
600 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500601 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
602 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500603 break;
604 }
605 }
606 this->updateTitle();
607 fWindow->inval();
608 });
Ben Wagner37c54032018-04-13 14:30:23 -0400609 fCommands.addCommand('D', "Modes", "DFT", [this]() {
610 DisplayParams params = fWindow->getRequestedDisplayParams();
611 uint32_t flags = params.fSurfaceProps.flags();
612 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
613 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
614 fWindow->setRequestedDisplayParams(params);
615 this->updateTitle();
616 fWindow->inval();
617 });
Ben Wagner9613e452019-01-23 10:34:59 -0500618 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500619 if (!fFontOverrides.fEdging) {
620 fFontOverrides.fEdging = true;
621 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500622 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500623 switch (fFont.getEdging()) {
624 case SkFont::Edging::kAlias:
625 fFont.setEdging(SkFont::Edging::kAntiAlias);
626 break;
627 case SkFont::Edging::kAntiAlias:
628 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
629 break;
630 case SkFont::Edging::kSubpixelAntiAlias:
631 fFont.setEdging(SkFont::Edging::kAlias);
632 fFontOverrides.fEdging = false;
633 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500634 }
635 }
636 this->updateTitle();
637 fWindow->inval();
638 });
Ben Wagner9613e452019-01-23 10:34:59 -0500639 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500640 if (!fFontOverrides.fSubpixel) {
641 fFontOverrides.fSubpixel = true;
642 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500643 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500644 if (!fFont.isSubpixel()) {
645 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500646 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500647 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500648 }
649 }
650 this->updateTitle();
651 fWindow->inval();
652 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400653 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
654 if (!fFontOverrides.fBaselineSnap) {
655 fFontOverrides.fBaselineSnap = true;
656 fFont.setBaselineSnap(false);
657 } else {
658 if (!fFont.isBaselineSnap()) {
659 fFont.setBaselineSnap(true);
660 } else {
661 fFontOverrides.fBaselineSnap = false;
662 }
663 }
664 this->updateTitle();
665 fWindow->inval();
666 });
Brian Osman805a7272018-05-02 15:40:20 -0400667 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
668 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
669 : kPerspective_Real;
670 this->updateTitle();
671 fWindow->inval();
672 });
673 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
674 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
675 : kPerspective_Off;
676 this->updateTitle();
677 fWindow->inval();
678 });
Brian Osman207d4102019-01-10 09:40:58 -0500679 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
680 fAnimTimer.togglePauseResume();
681 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400682 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
683 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500684 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400685 fWindow->inval();
686 });
Mike Reed59295352020-03-12 13:56:34 -0400687 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
688 fDrawViaSerialize = !fDrawViaSerialize;
689 this->updateTitle();
690 fWindow->inval();
691 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500692 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400693 gUseSkVMBlitter = !gUseSkVMBlitter;
694 this->updateTitle();
695 fWindow->inval();
696 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500697 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
698 gSkVMAllowJIT = !gSkVMAllowJIT;
699 this->updateTitle();
700 fWindow->inval();
701 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500702
jvanverth2bb3b6d2016-04-08 07:24:09 -0700703 // set up slides
704 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500705 if (FLAGS_list) {
706 this->listNames();
707 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700708
Brian Osman9bb47cf2018-04-26 15:55:00 -0400709 fPerspectivePoints[0].set(0, 0);
710 fPerspectivePoints[1].set(1, 0);
711 fPerspectivePoints[2].set(0, 1);
712 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700713 fAnimTimer.run();
714
Hal Canaryc465d132017-12-08 10:21:31 -0500715 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500716 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500717 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500718 }
719 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500720
jongdeok.kim804f17e2019-02-26 14:39:23 +0900721 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500722 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700723}
724
jvanverth34524262016-05-04 13:49:13 -0700725void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400726 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
727 static const struct {
728 const char* fExtension;
729 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500730 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400731 const SlideFactory fFactory;
732 } gExternalSlidesInfo[] = {
733 { ".skp", "skp-dir", FLAGS_skps,
734 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
735 return sk_make_sp<SKPSlide>(name, path);}
736 },
737 { ".jpg", "jpg-dir", FLAGS_jpgs,
738 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
739 return sk_make_sp<ImageSlide>(name, path);}
740 },
Florin Malita87ccf332018-05-04 12:23:24 -0400741#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400742 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400743 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
744 return sk_make_sp<SkottieSlide>(name, path);}
745 },
Florin Malita87ccf332018-05-04 12:23:24 -0400746#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400747 #if defined(SK_ENABLE_SKRIVE)
748 { ".flr", "skrive-dir", FLAGS_rives,
749 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
750 return sk_make_sp<SkRiveSlide>(name, path);}
751 },
752 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400753#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400754 { ".svg", "svg-dir", FLAGS_svgs,
755 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
756 return sk_make_sp<SvgSlide>(name, path);}
757 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400758#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400759 };
jvanverthc265a922016-04-08 12:51:45 -0700760
Brian Salomon343553a2018-09-05 15:41:23 -0400761 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700762
Mike Klein88544fb2019-03-20 10:50:33 -0500763 const auto addSlide =
764 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
765 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
766 return;
767 }
liyuqian6f163d22016-06-13 12:26:45 -0700768
Mike Klein88544fb2019-03-20 10:50:33 -0500769 if (auto slide = fact(name, path)) {
770 dirSlides.push_back(slide);
771 fSlides.push_back(std::move(slide));
772 }
773 };
Florin Malita76a076b2018-02-15 18:40:48 -0500774
Florin Malita38792ce2018-05-08 10:36:18 -0400775 if (!FLAGS_file.isEmpty()) {
776 // single file mode
777 const SkString file(FLAGS_file[0]);
778
779 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
780 for (const auto& sinfo : gExternalSlidesInfo) {
781 if (file.endsWith(sinfo.fExtension)) {
782 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
783 return;
784 }
785 }
786
787 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
788 } else {
789 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
790 }
791
792 return;
793 }
794
795 // Bisect slide.
796 if (!FLAGS_bisect.isEmpty()) {
797 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500798 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400799 if (FLAGS_bisect.count() >= 2) {
800 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
801 bisect->onChar(*ch);
802 }
803 }
804 fSlides.push_back(std::move(bisect));
805 }
806 }
807
808 // GMs
809 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400810 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400811 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500812 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400813 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400814 fSlides.push_back(std::move(slide));
815 }
Florin Malita38792ce2018-05-08 10:36:18 -0400816 }
817 // reverse gms
818 int numGMs = fSlides.count() - firstGM;
819 for (int i = 0; i < numGMs/2; ++i) {
820 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
821 }
822
823 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400824 for (const SampleFactory factory : SampleRegistry::Range()) {
825 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500826 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400827 fSlides.push_back(slide);
828 }
Florin Malita38792ce2018-05-08 10:36:18 -0400829 }
830
Brian Osman7c979f52019-02-12 13:27:51 -0500831 // Particle demo
832 {
833 // TODO: Convert this to a sample
834 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500835 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500836 fSlides.push_back(std::move(slide));
837 }
838 }
839
Brian Osmand927bd22019-12-18 11:23:12 -0500840 // Runtime shader editor
841 {
842 sk_sp<Slide> slide(new SkSLSlide());
843 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
844 fSlides.push_back(std::move(slide));
845 }
846 }
847
Florin Malita0ffa3222018-04-05 14:34:45 -0400848 for (const auto& info : gExternalSlidesInfo) {
849 for (const auto& flag : info.fFlags) {
850 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
851 // single file
852 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
853 } else {
854 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400855 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400856 SkTArray<SkString> sortedFilenames;
857 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400858 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400859 sortedFilenames.push_back(name);
860 }
861 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400862 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000863 [](const SkString& a, const SkString& b) {
864 return strcmp(a.c_str(), b.c_str()) < 0;
865 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400866 }
867 for (const SkString& filename : sortedFilenames) {
868 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
869 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400870 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400871 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400872 if (!dirSlides.empty()) {
873 fSlides.push_back(
874 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
875 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500876 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400877 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400878 }
879 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500880
881 if (!fSlides.count()) {
882 sk_sp<Slide> slide(new NullSlide());
883 fSlides.push_back(std::move(slide));
884 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700885}
886
887
jvanverth34524262016-05-04 13:49:13 -0700888Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400889 for(auto& slide : fSlides) {
890 slide->gpuTeardown();
891 }
892
jvanverth9f372462016-04-06 06:08:59 -0700893 fWindow->detach();
894 delete fWindow;
895}
896
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500897struct SkPaintTitleUpdater {
898 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
899 void append(const char* s) {
900 if (fCount == 0) {
901 fTitle->append(" {");
902 } else {
903 fTitle->append(", ");
904 }
905 fTitle->append(s);
906 ++fCount;
907 }
908 void done() {
909 if (fCount > 0) {
910 fTitle->append("}");
911 }
912 }
913 SkString* fTitle;
914 int fCount;
915};
916
brianosman05de2162016-05-06 13:28:57 -0700917void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700918 if (!fWindow) {
919 return;
920 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500921 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700922 return; // Surface hasn't been created yet.
923 }
924
jvanverth34524262016-05-04 13:49:13 -0700925 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700926 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700927
Mike Kleine5acd752019-03-22 09:57:16 -0500928 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400929 if (gSkForceAnalyticAA) {
930 title.append(" <FAAA>");
931 } else {
932 title.append(" <AAA>");
933 }
934 }
Mike Reed59295352020-03-12 13:56:34 -0400935 if (fDrawViaSerialize) {
936 title.append(" <serialize>");
937 }
Mike Reed862818b2020-03-21 15:07:13 -0400938 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500939 title.append(" <SkVMBlitter>");
940 }
941 if (!gSkVMAllowJIT) {
942 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400943 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400944
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500945 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500946 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
947 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400948 const char* on, const char* off)
949 {
Ben Wagner9613e452019-01-23 10:34:59 -0500950 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400951 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500952 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400953 };
954
Ben Wagner9613e452019-01-23 10:34:59 -0500955 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
956 const char* on, const char* off)
957 {
958 if (fFontOverrides.*flag) {
959 paintTitle.append((fFont.*isFlag)() ? on : off);
960 }
961 };
962
963 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
964 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
965
966 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
967 "Force Autohint", "No Force Autohint");
968 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400969 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500970 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
971 "Linear Metrics", "Non-Linear Metrics");
972 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
973 "Bitmap Text", "No Bitmap Text");
974 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
975
976 if (fFontOverrides.fEdging) {
977 switch (fFont.getEdging()) {
978 case SkFont::Edging::kAlias:
979 paintTitle.append("Alias Text");
980 break;
981 case SkFont::Edging::kAntiAlias:
982 paintTitle.append("Antialias Text");
983 break;
984 case SkFont::Edging::kSubpixelAntiAlias:
985 paintTitle.append("Subpixel Antialias Text");
986 break;
987 }
988 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400989
Mike Reed3ae47332019-01-04 10:11:46 -0500990 if (fFontOverrides.fHinting) {
991 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400992 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500993 paintTitle.append("No Hinting");
994 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400995 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500996 paintTitle.append("Slight Hinting");
997 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400998 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500999 paintTitle.append("Normal Hinting");
1000 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001001 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001002 paintTitle.append("Full Hinting");
1003 break;
1004 }
1005 }
1006 paintTitle.done();
1007
Brian Osman92004802017-03-06 11:47:26 -05001008 switch (fColorMode) {
1009 case ColorMode::kLegacy:
1010 title.append(" Legacy 8888");
1011 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001012 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001013 title.append(" ColorManaged 8888");
1014 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001015 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001016 title.append(" ColorManaged F16");
1017 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001018 case ColorMode::kColorManagedF16Norm:
1019 title.append(" ColorManaged F16 Norm");
1020 break;
Brian Osman92004802017-03-06 11:47:26 -05001021 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001022
Brian Osman92004802017-03-06 11:47:26 -05001023 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001024 int curPrimaries = -1;
1025 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1026 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1027 curPrimaries = i;
1028 break;
1029 }
1030 }
Brian Osman03115dc2018-11-26 13:55:19 -05001031 title.appendf(" %s Gamma %f",
1032 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001033 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001034 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001035
Ben Wagner37c54032018-04-13 14:30:23 -04001036 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001037 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001038 switch (params.fSurfaceProps.pixelGeometry()) {
1039 case kUnknown_SkPixelGeometry:
1040 title.append( " Flat");
1041 break;
1042 case kRGB_H_SkPixelGeometry:
1043 title.append( " RGB");
1044 break;
1045 case kBGR_H_SkPixelGeometry:
1046 title.append( " BGR");
1047 break;
1048 case kRGB_V_SkPixelGeometry:
1049 title.append( " RGBV");
1050 break;
1051 case kBGR_V_SkPixelGeometry:
1052 title.append( " BGRV");
1053 break;
1054 }
1055 }
1056
1057 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1058 title.append(" DFT");
1059 }
1060
csmartdalton578f0642017-02-24 16:04:47 -07001061 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001062 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001063 int msaa = fWindow->sampleCount();
1064 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001065 title.appendf(" MSAA: %i", msaa);
1066 }
1067 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001068
1069 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001070 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001071 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1072 }
1073
Brian Osman805a7272018-05-02 15:40:20 -04001074 if (kPerspective_Real == fPerspectiveMode) {
1075 title.append(" Perpsective (Real)");
1076 } else if (kPerspective_Fake == fPerspectiveMode) {
1077 title.append(" Perspective (Fake)");
1078 }
1079
brianosman05de2162016-05-06 13:28:57 -07001080 fWindow->setTitle(title.c_str());
1081}
1082
Florin Malitaab99c342018-01-16 16:23:03 -05001083int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001084
1085 if (!FLAGS_slide.isEmpty()) {
1086 int count = fSlides.count();
1087 for (int i = 0; i < count; i++) {
1088 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001089 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001090 }
1091 }
1092
1093 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1094 this->listNames();
1095 }
1096
Florin Malitaab99c342018-01-16 16:23:03 -05001097 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001098}
1099
Florin Malitaab99c342018-01-16 16:23:03 -05001100void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001101 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001102 for (const auto& slide : fSlides) {
1103 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001104 }
1105}
1106
Florin Malitaab99c342018-01-16 16:23:03 -05001107void Viewer::setCurrentSlide(int slide) {
1108 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001109
Florin Malitaab99c342018-01-16 16:23:03 -05001110 if (slide == fCurrentSlide) {
1111 return;
1112 }
1113
1114 if (fCurrentSlide >= 0) {
1115 fSlides[fCurrentSlide]->unload();
1116 }
1117
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001118 SkScalar scaleFactor = 1.0;
1119 if (fApplyBackingScale) {
1120 scaleFactor = fWindow->scaleFactor();
1121 }
1122 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1123 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001124 fCurrentSlide = slide;
1125 this->setupCurrentSlide();
1126}
1127
1128void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001129 if (fCurrentSlide >= 0) {
1130 // prepare dimensions for image slides
1131 fGesture.resetTouchState();
1132 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001133
Jim Van Verth0848fb02018-01-22 13:39:30 -05001134 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1135 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1136 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001137
Jim Van Verth0848fb02018-01-22 13:39:30 -05001138 // Start with a matrix that scales the slide to the available screen space
1139 if (fWindow->scaleContentToFit()) {
1140 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001141 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1142 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001143 }
liyuqiane46e4f02016-05-20 07:32:19 -07001144 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001145
1146 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001147 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001148
1149 this->updateTitle();
1150 this->updateUIState();
1151
1152 fStatsLayer.resetMeasurements();
1153
1154 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001155 }
jvanverthc265a922016-04-08 12:51:45 -07001156}
1157
Brian Osmanaba642c2020-02-06 12:52:25 -05001158#define MAX_ZOOM_LEVEL 8.0f
1159#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001160
jvanverth34524262016-05-04 13:49:13 -07001161void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001162 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001163 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001164 this->preTouchMatrixChanged();
1165}
Yuqian Li755778c2018-03-28 16:23:31 -04001166
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001167void Viewer::preTouchMatrixChanged() {
1168 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001169 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1170 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1171 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1172 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1173}
1174
Brian Osman805a7272018-05-02 15:40:20 -04001175SkMatrix Viewer::computePerspectiveMatrix() {
1176 SkScalar w = fWindow->width(), h = fWindow->height();
1177 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1178 SkPoint perspPts[4] = {
1179 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1180 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1181 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1182 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1183 };
1184 SkMatrix m;
1185 m.setPolyToPoly(orthoPts, perspPts, 4);
1186 return m;
1187}
1188
Yuqian Li755778c2018-03-28 16:23:31 -04001189SkMatrix Viewer::computePreTouchMatrix() {
1190 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001191
1192 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001193 if (fApplyBackingScale) {
1194 zoomScale *= fWindow->scaleFactor();
1195 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001196 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001197 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001198
1199 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1200 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001201
Brian Osman805a7272018-05-02 15:40:20 -04001202 if (kPerspective_Real == fPerspectiveMode) {
1203 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001204 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001205 }
1206
Yuqian Li755778c2018-03-28 16:23:31 -04001207 return m;
jvanverthc265a922016-04-08 12:51:45 -07001208}
1209
liyuqiand3cdbca2016-05-17 12:44:20 -07001210SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001211 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001212 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001213 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001214 return m;
jvanverthc265a922016-04-08 12:51:45 -07001215}
1216
Brian Osman621491e2017-02-28 15:45:01 -05001217void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001218 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001219 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001220 fBackendType = backendType;
1221
Robert Phillipse9229532020-06-26 10:10:49 -04001222 // The active context is going away in 'detach'
1223 for(auto& slide : fSlides) {
1224 slide->gpuTeardown();
1225 }
1226
Brian Osman621491e2017-02-28 15:45:01 -05001227 fWindow->detach();
1228
Brian Osman70d2f432017-11-08 09:54:10 -05001229#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001230 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1231 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001232 DisplayParams params = fWindow->getRequestedDisplayParams();
1233 delete fWindow;
1234 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001235
Brian Osman70d2f432017-11-08 09:54:10 -05001236 // re-register callbacks
1237 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001238 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001239 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001240 fWindow->pushLayer(&fImGuiLayer);
1241
Brian Osman70d2f432017-11-08 09:54:10 -05001242 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1243 // will still include our correct sample count. But the re-created fWindow will lose that
1244 // information. On Windows, we need to re-create the window when changing sample count,
1245 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1246 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1247 // as if we had never changed windows at all).
1248 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001249#endif
1250
Brian Osman70d2f432017-11-08 09:54:10 -05001251 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001252}
1253
Brian Osman92004802017-03-06 11:47:26 -05001254void Viewer::setColorMode(ColorMode colorMode) {
1255 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001256 this->updateTitle();
1257 fWindow->inval();
1258}
1259
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001260class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1261public:
Mike Reed3ae47332019-01-04 10:11:46 -05001262 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1263 SkFont* font, Viewer::SkFontFields* ffields)
1264 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001265 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001266 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1267 sk_sp<SkTextBlob>* cache) {
1268 bool blobWillChange = false;
1269 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001270 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1271 bool shouldDraw = this->filterFont(&filteredFont);
1272 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001273 blobWillChange = true;
1274 break;
1275 }
1276 }
1277 if (!blobWillChange) {
1278 return blob;
1279 }
1280
1281 SkTextBlobBuilder builder;
1282 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001283 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1284 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001285 if (!shouldDraw) {
1286 continue;
1287 }
1288
Mike Reed3ae47332019-01-04 10:11:46 -05001289 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001290
Ben Wagner41e40472018-09-24 13:01:54 -04001291 const SkTextBlobBuilder::RunBuffer& runBuffer
1292 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001293 ? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(),
1294 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001295 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001296 ? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(),
1297 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001298 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001299 ? builder.allocRunTextPos(font, it.glyphCount(), it.textSize())
Ben Wagnere5736262021-02-08 16:52:08 -05001300 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001301 ? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001302 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1303 uint32_t glyphCount = it.glyphCount();
1304 if (it.glyphs()) {
1305 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1306 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1307 }
1308 if (it.pos()) {
1309 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001310 unsigned posPerGlyph = it.scalarsPerGlyph();
1311 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001312 }
1313 if (it.text()) {
1314 size_t textSize = sizeof(decltype(*it.text()));
1315 uint32_t textCount = it.textSize();
1316 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1317 }
1318 if (it.clusters()) {
1319 size_t clusterSize = sizeof(decltype(*it.clusters()));
1320 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1321 }
1322 }
1323 *cache = builder.make();
1324 return cache->get();
1325 }
1326 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1327 const SkPaint& paint) override {
1328 sk_sp<SkTextBlob> cache;
1329 this->SkPaintFilterCanvas::onDrawTextBlob(
1330 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1331 }
Mike Reed3ae47332019-01-04 10:11:46 -05001332 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001333 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001334 font->writable()->setSize(fFont->getSize());
1335 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001336 if (fFontOverrides->fScaleX) {
1337 font->writable()->setScaleX(fFont->getScaleX());
1338 }
1339 if (fFontOverrides->fSkewX) {
1340 font->writable()->setSkewX(fFont->getSkewX());
1341 }
Mike Reed3ae47332019-01-04 10:11:46 -05001342 if (fFontOverrides->fHinting) {
1343 font->writable()->setHinting(fFont->getHinting());
1344 }
Ben Wagner9613e452019-01-23 10:34:59 -05001345 if (fFontOverrides->fEdging) {
1346 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001347 }
Ben Wagner9613e452019-01-23 10:34:59 -05001348 if (fFontOverrides->fEmbolden) {
1349 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001350 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001351 if (fFontOverrides->fBaselineSnap) {
1352 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1353 }
Ben Wagner9613e452019-01-23 10:34:59 -05001354 if (fFontOverrides->fLinearMetrics) {
1355 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001356 }
Ben Wagner9613e452019-01-23 10:34:59 -05001357 if (fFontOverrides->fSubpixel) {
1358 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001359 }
Ben Wagner9613e452019-01-23 10:34:59 -05001360 if (fFontOverrides->fEmbeddedBitmaps) {
1361 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001362 }
Ben Wagner9613e452019-01-23 10:34:59 -05001363 if (fFontOverrides->fForceAutoHinting) {
1364 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001365 }
Ben Wagner9613e452019-01-23 10:34:59 -05001366
Mike Reed3ae47332019-01-04 10:11:46 -05001367 return true;
1368 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001369 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001370 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001371 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001372 }
Ben Wagner9613e452019-01-23 10:34:59 -05001373 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001374 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001375 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001376 if (fPaintOverrides->fStyle) {
1377 paint.setStyle(fPaint->getStyle());
1378 }
1379 if (fPaintOverrides->fWidth) {
1380 paint.setStrokeWidth(fPaint->getStrokeWidth());
1381 }
1382 if (fPaintOverrides->fMiterLimit) {
1383 paint.setStrokeMiter(fPaint->getStrokeMiter());
1384 }
1385 if (fPaintOverrides->fCapType) {
1386 paint.setStrokeCap(fPaint->getStrokeCap());
1387 }
1388 if (fPaintOverrides->fJoinType) {
1389 paint.setStrokeJoin(fPaint->getStrokeJoin());
1390 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001391 return true;
1392 }
1393 SkPaint* fPaint;
1394 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001395 SkFont* fFont;
1396 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001397};
1398
Robert Phillips9882dae2019-03-04 11:00:10 -05001399void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001400 if (fCurrentSlide < 0) {
1401 return;
1402 }
1403
Robert Phillips9882dae2019-03-04 11:00:10 -05001404 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001405
Brian Osmanf750fbc2017-02-08 10:47:28 -05001406 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001407 SkSurface* slideSurface = surface;
1408 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001409 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001410
Brian Osmane0d4fba2017-03-15 10:24:55 -04001411 // 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 -05001412 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001413 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001414 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001415 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001416 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001417 }
1418
Brian Osman3ac99cf2017-12-01 11:23:53 -05001419 if (fSaveToSKP) {
1420 SkPictureRecorder recorder;
1421 SkCanvas* recorderCanvas = recorder.beginRecording(
1422 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001423 fSlides[fCurrentSlide]->draw(recorderCanvas);
1424 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1425 SkFILEWStream stream("sample_app.skp");
1426 picture->serialize(&stream);
1427 fSaveToSKP = false;
1428 }
1429
Brian Osmane9ed0f02018-11-26 14:50:05 -05001430 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001431 SkColorType colorType;
1432 switch (fColorMode) {
1433 case ColorMode::kLegacy:
1434 case ColorMode::kColorManaged8888:
1435 colorType = kN32_SkColorType;
1436 break;
1437 case ColorMode::kColorManagedF16:
1438 colorType = kRGBA_F16_SkColorType;
1439 break;
1440 case ColorMode::kColorManagedF16Norm:
1441 colorType = kRGBA_F16Norm_SkColorType;
1442 break;
1443 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001444
1445 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001446 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001447 slideCanvas->getProps(&props);
1448
Brian Osmane9ed0f02018-11-26 14:50:05 -05001449 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1450 return Window::kRaster_BackendType == this->fBackendType
1451 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001452 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001453 };
1454
Brian Osman03115dc2018-11-26 13:55:19 -05001455 // We need to render offscreen if we're...
1456 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1457 // ... in any raster mode, because the window surface is actually GL
1458 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001459 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001460 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001461 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001462 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001463 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001464 colorSpace != nullptr ||
1465 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001466
Brian Osmane9ed0f02018-11-26 14:50:05 -05001467 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001468 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001469 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001470 }
1471
Mike Reed59295352020-03-12 13:56:34 -04001472 SkPictureRecorder recorder;
1473 SkCanvas* recorderRestoreCanvas = nullptr;
1474 if (fDrawViaSerialize) {
1475 recorderRestoreCanvas = slideCanvas;
1476 slideCanvas = recorder.beginRecording(
1477 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1478 }
1479
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001480 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001481 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001482 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001483 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001484 if (fTiled) {
1485 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1486 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001487 for (int y = 0; y < fWindow->height(); y += tileH) {
1488 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001489 SkAutoCanvasRestore acr(slideCanvas, true);
1490 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1491 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001492 }
1493 }
1494
1495 // Draw borders between tiles
1496 if (fDrawTileBoundaries) {
1497 SkPaint border;
1498 border.setColor(0x60FF00FF);
1499 border.setStyle(SkPaint::kStroke_Style);
1500 for (int y = 0; y < fWindow->height(); y += tileH) {
1501 for (int x = 0; x < fWindow->width(); x += tileW) {
1502 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1503 }
1504 }
1505 }
1506 } else {
1507 slideCanvas->concat(this->computeMatrix());
1508 if (kPerspective_Real == fPerspectiveMode) {
1509 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1510 }
Mike Reed3ae47332019-01-04 10:11:46 -05001511 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001512 fSlides[fCurrentSlide]->draw(&filterCanvas);
1513 }
Brian Osman56a24812017-12-19 11:15:16 -05001514 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001515 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001516
Mike Reed59295352020-03-12 13:56:34 -04001517 if (recorderRestoreCanvas) {
1518 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1519 auto data = picture->serialize();
1520 slideCanvas = recorderRestoreCanvas;
1521 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1522 }
1523
Brian Osman1df161a2017-02-09 12:10:20 -05001524 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001525 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001526 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001527 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001528
1529 // If we rendered offscreen, snap an image and push the results to the window's canvas
1530 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001531 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001532
Robert Phillips9882dae2019-03-04 11:00:10 -05001533 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001534 SkPaint paint;
1535 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001536 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001537 int prePerspectiveCount = canvas->save();
1538 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001539 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001540 canvas->clear(SK_ColorWHITE);
1541 canvas->concat(this->computePerspectiveMatrix());
1542 }
Mike Reedb339d052021-01-28 11:20:41 -05001543 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001544 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001545 }
Mike Reed376d8122019-03-14 11:39:02 -04001546
1547 if (fShowSlideDimensions) {
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001548 SkCanvas* canvas = surface->getCanvas();
1549 SkAutoCanvasRestore acr(canvas, true);
1550 canvas->concat(this->computeMatrix());
Mike Reed376d8122019-03-14 11:39:02 -04001551 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1552 SkPaint paint;
1553 paint.setColor(0x40FFFF00);
Jim Van Verthb5ea7862021-04-16 14:39:00 -04001554 canvas->drawRect(r, paint);
Mike Reed376d8122019-03-14 11:39:02 -04001555 }
liyuqian6f163d22016-06-13 12:26:45 -07001556}
1557
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001558void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001559 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001560 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001561}
Jim Van Verth6f449692017-02-14 15:16:46 -05001562
Robert Phillips9882dae2019-03-04 11:00:10 -05001563void Viewer::onPaint(SkSurface* surface) {
1564 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001565
Robert Phillips9882dae2019-03-04 11:00:10 -05001566 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001567
Brian Osmand67e5182017-12-08 16:46:09 -05001568 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001569
Greg Daniel427d8eb2020-09-28 15:04:18 -04001570 fLastImage.reset();
1571
Robert Phillipsed653392020-07-10 13:55:21 -04001572 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001573 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001574 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001575 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001576}
1577
Ben Wagnera1915972018-08-09 15:06:19 -04001578void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001579 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001580 SkScalar scaleFactor = 1.0;
1581 if (fApplyBackingScale) {
1582 scaleFactor = fWindow->scaleFactor();
1583 }
1584 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001585 }
Ben Wagnera1915972018-08-09 15:06:19 -04001586}
1587
Florin Malitacefc1b92018-02-19 21:43:47 -05001588SkPoint Viewer::mapEvent(float x, float y) {
1589 const auto m = this->computeMatrix();
1590 SkMatrix inv;
1591
1592 SkAssertResult(m.invert(&inv));
1593
1594 return inv.mapXY(x, y);
1595}
1596
Hal Canaryb1f411a2019-08-29 10:39:22 -04001597bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001598 if (GestureDevice::kMouse == fGestureDevice) {
1599 return false;
1600 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001601
1602 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001603 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001604 fWindow->inval();
1605 return true;
1606 }
1607
liyuqiand3cdbca2016-05-17 12:44:20 -07001608 void* castedOwner = reinterpret_cast<void*>(owner);
1609 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001610 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001611 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001612#if defined(SK_BUILD_FOR_IOS)
1613 // TODO: move IOS swipe detection higher up into the platform code
1614 SkPoint dir;
1615 if (fGesture.isFling(&dir)) {
1616 // swiping left or right
1617 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1618 if (dir.fX < 0) {
1619 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1620 fCurrentSlide + 1 : 0);
1621 } else {
1622 this->setCurrentSlide(fCurrentSlide > 0 ?
1623 fCurrentSlide - 1 : fSlides.count() - 1);
1624 }
1625 }
1626 fGesture.reset();
1627 }
1628#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001629 break;
1630 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001631 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001632 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001633 break;
1634 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001635 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001636 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001637 break;
1638 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001639 default: {
1640 // kLeft and kRight are only for swipes
1641 SkASSERT(false);
1642 break;
1643 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001644 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001645 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001646 fWindow->inval();
1647 return true;
1648}
1649
Hal Canaryb1f411a2019-08-29 10:39:22 -04001650bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001651 if (GestureDevice::kTouch == fGestureDevice) {
1652 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001653 }
Brian Osman16c81a12017-12-20 11:58:34 -05001654
Florin Malitacefc1b92018-02-19 21:43:47 -05001655 const auto slidePt = this->mapEvent(x, y);
1656 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1657 fWindow->inval();
1658 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001659 }
1660
1661 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001662 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001663 fGesture.touchEnd(nullptr);
1664 break;
1665 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001666 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001667 fGesture.touchBegin(nullptr, x, y);
1668 break;
1669 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001670 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001671 fGesture.touchMoved(nullptr, x, y);
1672 break;
1673 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001674 default: {
1675 SkASSERT(false); // shouldn't see kRight or kLeft here
1676 break;
1677 }
Brian Osman16c81a12017-12-20 11:58:34 -05001678 }
1679 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1680
Hal Canaryb1f411a2019-08-29 10:39:22 -04001681 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001682 fWindow->inval();
1683 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001684 return true;
1685}
1686
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001687bool Viewer::onFling(skui::InputState state) {
1688 if (skui::InputState::kRight == state) {
1689 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1690 return true;
1691 } else if (skui::InputState::kLeft == state) {
1692 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1693 return true;
1694 }
1695 return false;
1696}
1697
1698bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1699 switch (state) {
1700 case skui::InputState::kDown:
1701 fGesture.startZoom();
1702 return true;
1703 break;
1704 case skui::InputState::kMove:
1705 fGesture.updateZoom(scale, x, y, x, y);
1706 return true;
1707 break;
1708 case skui::InputState::kUp:
1709 fGesture.endZoom();
1710 return true;
1711 break;
1712 default:
1713 SkASSERT(false);
1714 break;
1715 }
1716
1717 return false;
1718}
1719
Brian Osmana109e392017-02-24 09:49:14 -05001720static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001721 // The gamut image covers a (0.8 x 0.9) shaped region
1722 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001723
1724 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1725 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1726 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001727 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1728 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1729 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001730
Brian Osman535c5e32019-02-09 16:32:58 -05001731 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1732 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1733 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1734 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1735 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001736}
1737
Ben Wagner3627d2e2018-06-26 14:23:20 -04001738static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001739 ImGui::DragCanvas dc(pt);
1740 dc.fillColor(IM_COL32(0, 0, 0, 128));
1741 dc.dragPoint(pt);
1742 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001743}
1744
Brian Osman9bb47cf2018-04-26 15:55:00 -04001745static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001746 ImGui::DragCanvas dc(pts);
1747 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001748
Brian Osman535c5e32019-02-09 16:32:58 -05001749 for (int i = 0; i < 4; ++i) {
1750 dc.dragPoint(pts + i);
1751 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001752
Brian Osman535c5e32019-02-09 16:32:58 -05001753 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1754 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1755 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1756 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001757
Brian Osman535c5e32019-02-09 16:32:58 -05001758 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001759}
1760
John Stiles38b7d2f2020-06-24 12:13:31 -04001761static SkSL::String build_sksl_highlight_shader() {
1762 return SkSL::String("out half4 sk_FragColor;\n"
1763 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1764}
1765
1766static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1767 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1768 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1769 size_t pos = inShader.rfind("return *_out;\n");
1770 if (pos == std::string::npos) {
1771 return inShader;
1772 }
1773
1774 SkSL::String replacementShader = inShader;
1775 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1776 return replacementShader;
1777}
1778
1779static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1780 const char* versionDecl = shaderCaps.versionDeclString();
1781 SkSL::String highlight = versionDecl ? versionDecl : "";
1782 if (shaderCaps.usesPrecisionModifiers()) {
1783 highlight.append("precision mediump float;\n");
1784 }
1785 highlight.appendf("out vec4 sk_FragColor;\n"
1786 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1787 return highlight;
1788}
1789
Brian Osmand67e5182017-12-08 16:46:09 -05001790void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001791 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1792 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001793 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001794 }
1795
1796 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001797 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1798 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001799 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001800 DisplayParams params = fWindow->getRequestedDisplayParams();
1801 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001802 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001803
Brian Osmana109e392017-02-24 09:49:14 -05001804 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1805 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001806 if (ImGui::CollapsingHeader("Backend")) {
1807 int newBackend = static_cast<int>(fBackendType);
1808 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1809 ImGui::SameLine();
1810 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001811#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1812 ImGui::SameLine();
1813 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1814#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001815#if defined(SK_DAWN)
1816 ImGui::SameLine();
1817 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1818#endif
John Stilesf7da9232020-11-19 19:58:14 -05001819#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001820 ImGui::SameLine();
1821 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1822#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001823#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001824 ImGui::SameLine();
1825 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1826#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001827#if defined(SK_DIRECT3D)
1828 ImGui::SameLine();
1829 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1830#endif
Brian Osman621491e2017-02-28 15:45:01 -05001831 if (newBackend != fBackendType) {
1832 fDeferredActions.push_back([=]() {
1833 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1834 });
1835 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001836
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001837 bool* wire = &params.fGrContextOptions.fWireframeMode;
1838 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1839 paramsChanged = true;
1840 }
Brian Salomon99a33902017-03-07 15:16:34 -05001841
Brian Salomon91216d52021-04-09 11:57:59 -04001842 bool* reducedShaders = &params.fGrContextOptions.fReducedShaderVariations;
1843 if (ctx && ImGui::Checkbox("Reduced shaders", reducedShaders)) {
1844 paramsChanged = true;
1845 }
1846
Brian Osman28b12522017-03-08 17:10:24 -05001847 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001848 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001849 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001850 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1851 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1852 1;
1853
1854 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1855 if (maxMSAA >= 4) {
1856 ImGui::Text("MSAA: ");
1857
1858 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1859 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1860 // don't show it in the list.
1861 if (curMSAA == 2) {
1862 continue;
1863 }
1864 ImGui::SameLine();
1865 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1866 &sampleCount, curMSAA);
1867 }
1868 }
Brian Osman28b12522017-03-08 17:10:24 -05001869
1870 if (sampleCount != params.fMSAASampleCount) {
1871 params.fMSAASampleCount = sampleCount;
1872 paramsChanged = true;
1873 }
1874 }
1875
Ben Wagner37c54032018-04-13 14:30:23 -04001876 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001877 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001878 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1879 }
1880 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1881 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1882 {
1883 uint32_t flags = params.fSurfaceProps.flags();
1884 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001885 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1886 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1887 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001888 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001889 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001890 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1891 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1892 }
1893 paramsChanged = true;
1894 }
1895
1896 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1897 if (ImGui::Checkbox("DFT", &useDFT)) {
1898 uint32_t flags = params.fSurfaceProps.flags();
1899 if (useDFT) {
1900 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1901 } else {
1902 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1903 }
1904 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1905 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1906 paramsChanged = true;
1907 }
1908
Brian Osman8a9de3d2017-03-01 14:59:05 -05001909 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001910 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001911 auto prButton = [&](GpuPathRenderers x) {
1912 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001913 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1914 params.fGrContextOptions.fGpuPathRenderers = x;
1915 paramsChanged = true;
1916 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001917 }
1918 };
1919
1920 if (!ctx) {
1921 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001922 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001923 const auto* caps = ctx->priv().caps();
1924 prButton(GpuPathRenderers::kDefault);
1925 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001926 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001927 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001928 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001929 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001930 if (1 == fWindow->sampleCount()) {
1931 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1932 prButton(GpuPathRenderers::kCoverageCounting);
1933 }
1934 prButton(GpuPathRenderers::kSmall);
1935 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001936 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001937 prButton(GpuPathRenderers::kNone);
1938 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001939 ImGui::TreePop();
1940 }
Brian Osman621491e2017-02-28 15:45:01 -05001941 }
1942
Ben Wagner964571d2019-03-08 12:35:06 -05001943 if (ImGui::CollapsingHeader("Tiling")) {
1944 ImGui::Checkbox("Enable", &fTiled);
1945 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1946 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1947 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1948 }
1949
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001950 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001951 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1952 this->preTouchMatrixChanged();
1953 this->onResize(fWindow->width(), fWindow->height());
1954 paramsChanged = true;
1955 }
1956
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001957 float zoom = fZoomLevel;
1958 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1959 fZoomLevel = zoom;
1960 this->preTouchMatrixChanged();
1961 paramsChanged = true;
1962 }
1963 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001964 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001965 fRotation = deg;
1966 this->preTouchMatrixChanged();
1967 paramsChanged = true;
1968 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001969 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1970 if (ImGui_DragLocation(&fOffset)) {
1971 this->preTouchMatrixChanged();
1972 paramsChanged = true;
1973 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001974 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1975 this->preTouchMatrixChanged();
1976 paramsChanged = true;
1977 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001978 }
Brian Osman805a7272018-05-02 15:40:20 -04001979 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1980 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1981 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001982 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001983 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001984 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001985 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001986 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001987 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001988 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001989 }
1990
Ben Wagnera580fb32018-04-17 11:16:32 -04001991 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001992 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001993 if (fPaintOverrides.fAntiAlias) {
1994 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001995 }
1996 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001997 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001998 {
1999 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
2000 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04002001 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002002 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
2003 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002004 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002005 fPaintOverrides.fAntiAlias = true;
2006 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04002007 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05002008 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04002009 case SkPaintFields::AntiAliasState::Alias:
2010 break;
2011 case SkPaintFields::AntiAliasState::Normal:
2012 break;
2013 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
2014 gSkUseAnalyticAA = true;
2015 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002016 break;
2017 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2018 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002019 break;
2020 }
2021 }
2022 paramsChanged = true;
2023 }
2024
Ben Wagner99a78dc2018-05-09 18:23:51 -04002025 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002026 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002027 bool (SkPaint::* isFlag)() const,
2028 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002029 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002030 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002031 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002032 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002033 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002034 if (ImGui::Combo(label, &itemIndex, items)) {
2035 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002036 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002037 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002038 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002039 (fPaint.*setFlag)(itemIndex == 2);
2040 }
2041 paramsChanged = true;
2042 }
2043 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002044
Ben Wagner99a78dc2018-05-09 18:23:51 -04002045 paintFlag("Dither",
2046 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002047 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002048 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002049
2050 int styleIdx = 0;
2051 if (fPaintOverrides.fStyle) {
2052 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2053 }
2054 if (ImGui::Combo("Style", &styleIdx,
2055 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2056 {
2057 if (styleIdx == 0) {
2058 fPaintOverrides.fStyle = false;
2059 fPaint.setStyle(SkPaint::kFill_Style);
2060 } else {
2061 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2062 fPaintOverrides.fStyle = true;
2063 }
2064 paramsChanged = true;
2065 }
2066
2067 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2068 if (fPaintOverrides.fWidth) {
2069 float width = fPaint.getStrokeWidth();
2070 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2071 fPaint.setStrokeWidth(width);
2072 paramsChanged = true;
2073 }
2074 }
2075
2076 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2077 if (fPaintOverrides.fMiterLimit) {
2078 float miterLimit = fPaint.getStrokeMiter();
2079 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2080 fPaint.setStrokeMiter(miterLimit);
2081 paramsChanged = true;
2082 }
2083 }
2084
2085 int capIdx = 0;
2086 if (fPaintOverrides.fCapType) {
2087 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2088 }
2089 if (ImGui::Combo("Cap Type", &capIdx,
2090 "Default\0Butt\0Round\0Square\0\0"))
2091 {
2092 if (capIdx == 0) {
2093 fPaintOverrides.fCapType = false;
2094 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2095 } else {
2096 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2097 fPaintOverrides.fCapType = true;
2098 }
2099 paramsChanged = true;
2100 }
2101
2102 int joinIdx = 0;
2103 if (fPaintOverrides.fJoinType) {
2104 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2105 }
2106 if (ImGui::Combo("Join Type", &joinIdx,
2107 "Default\0Miter\0Round\0Bevel\0\0"))
2108 {
2109 if (joinIdx == 0) {
2110 fPaintOverrides.fJoinType = false;
2111 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2112 } else {
2113 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2114 fPaintOverrides.fJoinType = true;
2115 }
2116 paramsChanged = true;
2117 }
Ben Wagner9613e452019-01-23 10:34:59 -05002118 }
Hal Canary02738a82019-01-21 18:51:32 +00002119
Ben Wagner9613e452019-01-23 10:34:59 -05002120 if (ImGui::CollapsingHeader("Font")) {
2121 int hintingIdx = 0;
2122 if (fFontOverrides.fHinting) {
2123 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2124 }
2125 if (ImGui::Combo("Hinting", &hintingIdx,
2126 "Default\0None\0Slight\0Normal\0Full\0\0"))
2127 {
2128 if (hintingIdx == 0) {
2129 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002130 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002131 } else {
2132 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2133 fFontOverrides.fHinting = true;
2134 }
2135 paramsChanged = true;
2136 }
Hal Canary02738a82019-01-21 18:51:32 +00002137
Ben Wagner9613e452019-01-23 10:34:59 -05002138 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2139 bool SkFontFields::* flag,
2140 bool (SkFont::* isFlag)() const,
2141 void (SkFont::* setFlag)(bool) )
2142 {
2143 int itemIndex = 0;
2144 if (fFontOverrides.*flag) {
2145 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2146 }
2147 if (ImGui::Combo(label, &itemIndex, items)) {
2148 if (itemIndex == 0) {
2149 fFontOverrides.*flag = false;
2150 } else {
2151 fFontOverrides.*flag = true;
2152 (fFont.*setFlag)(itemIndex == 2);
2153 }
2154 paramsChanged = true;
2155 }
2156 };
Hal Canary02738a82019-01-21 18:51:32 +00002157
Ben Wagner9613e452019-01-23 10:34:59 -05002158 fontFlag("Fake Bold Glyphs",
2159 "Default\0No Fake Bold\0Fake Bold\0\0",
2160 &SkFontFields::fEmbolden,
2161 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002162
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002163 fontFlag("Baseline Snapping",
2164 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2165 &SkFontFields::fBaselineSnap,
2166 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2167
Ben Wagner9613e452019-01-23 10:34:59 -05002168 fontFlag("Linear Text",
2169 "Default\0No Linear Text\0Linear Text\0\0",
2170 &SkFontFields::fLinearMetrics,
2171 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002172
Ben Wagner9613e452019-01-23 10:34:59 -05002173 fontFlag("Subpixel Position Glyphs",
2174 "Default\0Pixel Text\0Subpixel Text\0\0",
2175 &SkFontFields::fSubpixel,
2176 &SkFont::isSubpixel, &SkFont::setSubpixel);
2177
2178 fontFlag("Embedded Bitmap Text",
2179 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2180 &SkFontFields::fEmbeddedBitmaps,
2181 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2182
2183 fontFlag("Force Auto-Hinting",
2184 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2185 &SkFontFields::fForceAutoHinting,
2186 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2187
2188 int edgingIdx = 0;
2189 if (fFontOverrides.fEdging) {
2190 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2191 }
2192 if (ImGui::Combo("Edging", &edgingIdx,
2193 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2194 {
2195 if (edgingIdx == 0) {
2196 fFontOverrides.fEdging = false;
2197 fFont.setEdging(SkFont::Edging::kAlias);
2198 } else {
2199 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2200 fFontOverrides.fEdging = true;
2201 }
2202 paramsChanged = true;
2203 }
2204
Ben Wagner15a8d572019-03-21 13:35:44 -04002205 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2206 if (fFontOverrides.fSize) {
2207 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002208 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002209 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002210 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002211 fFontOverrides.fSizeRange[0],
2212 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002213 "%.6f", 2.0f))
2214 {
Mike Reed3ae47332019-01-04 10:11:46 -05002215 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002216 paramsChanged = true;
2217 }
2218 }
2219
2220 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2221 if (fFontOverrides.fScaleX) {
2222 float scaleX = fFont.getScaleX();
2223 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2224 fFont.setScaleX(scaleX);
2225 paramsChanged = true;
2226 }
2227 }
2228
2229 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2230 if (fFontOverrides.fSkewX) {
2231 float skewX = fFont.getSkewX();
2232 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2233 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002234 paramsChanged = true;
2235 }
2236 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002237 }
2238
Mike Reed81f60ec2018-05-15 10:09:52 -04002239 {
2240 SkMetaData controls;
2241 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2242 if (ImGui::CollapsingHeader("Current Slide")) {
2243 SkMetaData::Iter iter(controls);
2244 const char* name;
2245 SkMetaData::Type type;
2246 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002247 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002248 if (type == SkMetaData::kScalar_Type) {
2249 float val[3];
2250 SkASSERT(count == 3);
2251 controls.findScalars(name, &count, val);
2252 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2253 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002254 }
Ben Wagner110c7032019-03-22 17:03:59 -04002255 } else if (type == SkMetaData::kBool_Type) {
2256 bool val;
2257 SkASSERT(count == 1);
2258 controls.findBool(name, &val);
2259 if (ImGui::Checkbox(name, &val)) {
2260 controls.setBool(name, val);
2261 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002262 }
2263 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002264 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002265 }
2266 }
2267 }
2268
Ben Wagner7a3c6742018-04-23 10:01:07 -04002269 if (fShowSlidePicker) {
2270 ImGui::SetNextTreeNodeOpen(true);
2271 }
Brian Osman79086b92017-02-10 13:36:16 -05002272 if (ImGui::CollapsingHeader("Slide")) {
2273 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002274 static ImVector<const char*> filteredSlideNames;
2275 static ImVector<int> filteredSlideIndices;
2276
Brian Osmanfce09c52017-11-14 15:32:20 -05002277 if (fShowSlidePicker) {
2278 ImGui::SetKeyboardFocusHere();
2279 fShowSlidePicker = false;
2280 }
2281
Brian Osman79086b92017-02-10 13:36:16 -05002282 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002283 filteredSlideNames.clear();
2284 filteredSlideIndices.clear();
2285 int filteredIndex = 0;
2286 for (int i = 0; i < fSlides.count(); ++i) {
2287 const char* slideName = fSlides[i]->getName().c_str();
2288 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2289 if (i == fCurrentSlide) {
2290 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002291 }
Brian Osmanf479e422017-11-08 13:11:36 -05002292 filteredSlideNames.push_back(slideName);
2293 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002294 }
Brian Osman79086b92017-02-10 13:36:16 -05002295 }
Brian Osmanf479e422017-11-08 13:11:36 -05002296
Brian Osmanf479e422017-11-08 13:11:36 -05002297 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2298 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002299 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002300 }
2301 }
Brian Osmana109e392017-02-24 09:49:14 -05002302
2303 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002304 ColorMode newMode = fColorMode;
2305 auto cmButton = [&](ColorMode mode, const char* label) {
2306 if (ImGui::RadioButton(label, mode == fColorMode)) {
2307 newMode = mode;
2308 }
2309 };
2310
2311 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002312 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2313 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002314 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002315
2316 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002317 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002318 }
2319
2320 // Pick from common gamuts:
2321 int primariesIdx = 4; // Default: Custom
2322 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2323 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2324 primariesIdx = i;
2325 break;
2326 }
2327 }
2328
Brian Osman03115dc2018-11-26 13:55:19 -05002329 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002330 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002331
Brian Osmana109e392017-02-24 09:49:14 -05002332 if (ImGui::Combo("Primaries", &primariesIdx,
2333 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2334 if (primariesIdx >= 0 && primariesIdx <= 3) {
2335 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2336 }
2337 }
2338
2339 // Allow direct editing of gamut
2340 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2341 }
Brian Osman207d4102019-01-10 09:40:58 -05002342
2343 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002344 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002345 if (ImGui::Checkbox("Pause", &isPaused)) {
2346 fAnimTimer.togglePauseResume();
2347 }
Brian Osman707d2022019-01-10 11:27:34 -05002348
2349 float speed = fAnimTimer.getSpeed();
2350 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2351 fAnimTimer.setSpeed(speed);
2352 }
Brian Osman207d4102019-01-10 09:40:58 -05002353 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002354
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002355 if (ImGui::CollapsingHeader("Shaders")) {
2356 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2357 GrContextOptions::ShaderCacheStrategy::kSkSL;
John Stiles7247b482021-03-08 10:40:35 -05002358
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002359#if defined(SK_VULKAN)
2360 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2361#else
2362 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002363#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002364
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002365 // To re-load shaders from the currently active programs, we flush all
2366 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002367 static bool gLoadPending = false;
2368 if (gLoadPending) {
2369 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
Brian Osmanf0de96f2021-02-26 13:54:11 -05002370 const SkString& description, int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002371 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002372 entry.fKey = key;
2373 SkMD5 hash;
2374 hash.write(key->bytes(), key->size());
2375 SkMD5::Digest digest = hash.finish();
2376 for (int i = 0; i < 16; ++i) {
2377 entry.fKeyString.appendf("%02x", digest.data[i]);
2378 }
Brian Osmanf0de96f2021-02-26 13:54:11 -05002379 entry.fKeyDescription = description;
Brian Osman0b8bb882019-04-12 11:47:19 -04002380
Brian Osman9e4e4c72020-06-10 07:19:34 -04002381 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002382 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002383 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2384 entry.fInputs,
2385 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002386 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002387 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002388 fPersistentCache.foreach(collectShaders);
2389 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002390
2391#if defined(SK_VULKAN)
2392 if (isVulkan && !sksl) {
2393 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2394 for (auto& entry : fCachedShaders) {
2395 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2396 const SkSL::String& spirv(entry.fShader[i]);
2397 std::string disasm;
2398 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2399 &disasm);
2400 entry.fShader[i].assign(disasm);
2401 }
2402 }
2403 }
2404#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002405 }
2406
John Stilesa8f6b6f2021-03-05 16:00:42 -05002407 // Defer actually doing the View/Apply logic so that we can trigger an Apply when we
Brian Osman0b8bb882019-04-12 11:47:19 -04002408 // start or finish hovering on a tree node in the list below:
John Stilesa8f6b6f2021-03-05 16:00:42 -05002409 bool doView = ImGui::Button("View"); ImGui::SameLine();
2410 bool doApply = ImGui::Button("Apply Changes"); ImGui::SameLine();
John Stiles7247b482021-03-08 10:40:35 -05002411 bool doDump = ImGui::Button("Dump SkSL to resources/sksl/");
John Stilesa8f6b6f2021-03-05 16:00:42 -05002412
John Stiles2ee4d7a2021-03-30 10:30:47 -04002413 int newOptLevel = fOptLevel;
John Stiles7247b482021-03-08 10:40:35 -05002414 ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source);
2415 ImGui::SameLine();
2416 ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile);
2417 ImGui::SameLine();
2418 ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize);
2419 ImGui::SameLine();
2420 ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline);
John Stilesa8f6b6f2021-03-05 16:00:42 -05002421
John Stiles7247b482021-03-08 10:40:35 -05002422 // If we are changing the compile mode, we want to reset the cache and redo
2423 // everything.
John Stiles2ee4d7a2021-03-30 10:30:47 -04002424 if (doDump || newOptLevel != fOptLevel) {
John Stiles7247b482021-03-08 10:40:35 -05002425 sksl = doDump || (newOptLevel == kShaderOptLevel_Source);
John Stiles2ee4d7a2021-03-30 10:30:47 -04002426 fOptLevel = (ShaderOptLevel)newOptLevel;
2427 switch (fOptLevel) {
2428 case kShaderOptLevel_Source:
2429 Compiler::EnableOptimizer(OverrideFlag::kDefault);
2430 Compiler::EnableInliner(OverrideFlag::kDefault);
2431 break;
2432 case kShaderOptLevel_Compile:
2433 Compiler::EnableOptimizer(OverrideFlag::kOff);
2434 Compiler::EnableInliner(OverrideFlag::kOff);
2435 break;
2436 case kShaderOptLevel_Optimize:
2437 Compiler::EnableOptimizer(OverrideFlag::kOn);
2438 Compiler::EnableInliner(OverrideFlag::kOff);
2439 break;
2440 case kShaderOptLevel_Inline:
2441 Compiler::EnableOptimizer(OverrideFlag::kOn);
2442 Compiler::EnableInliner(OverrideFlag::kOn);
2443 break;
2444 }
John Stiles7247b482021-03-08 10:40:35 -05002445
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002446 params.fGrContextOptions.fShaderCacheStrategy =
2447 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2448 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2449 paramsChanged = true;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002450 doView = true;
2451
2452 fDeferredActions.push_back([=]() {
2453 // Reset the cache.
2454 fPersistentCache.reset();
2455 // Dump the cache once we have drawn a frame with it.
2456 if (doDump) {
2457 fDeferredActions.push_back([this]() {
2458 this->dumpShadersToResources();
2459 });
2460 }
2461 });
Brian Osmancbc33b82019-04-19 14:16:19 -04002462 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002463
2464 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002465 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002466 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2467 bool hovered = ImGui::IsItemHovered();
2468 if (hovered != entry.fHovered) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002469 // Force an Apply to patch the highlight shader in/out
Brian Osman0b8bb882019-04-12 11:47:19 -04002470 entry.fHovered = hovered;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002471 doApply = true;
Brian Osman0b8bb882019-04-12 11:47:19 -04002472 }
2473 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002474 auto stringBox = [](const char* label, std::string* str) {
2475 // Full width, and not too much space for each shader
2476 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2477 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2478 ImGui::InputTextMultiline(label, str, boxSize);
2479 };
Brian Osmanf0de96f2021-02-26 13:54:11 -05002480 if (ImGui::TreeNode("Key")) {
2481 ImGui::TextWrapped("%s", entry.fKeyDescription.c_str());
2482 ImGui::TreePop();
2483 }
Brian Osmaneb3fb902020-08-18 13:16:59 -04002484 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2485 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002486 ImGui::TreePop();
2487 }
2488 }
2489 ImGui::EndChild();
2490
John Stilesa8f6b6f2021-03-05 16:00:42 -05002491 if (doView) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002492 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002493 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002494 gLoadPending = true;
2495 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002496
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002497 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2498 // but I'm not sure anyone wants to do that.
2499 if (isVulkan && !sksl) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002500 doApply = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002501 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002502 if (doApply) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002503 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002504 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002505 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002506 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002507 if (entry.fHovered) {
2508 // The hovered item (if any) gets a special shader to make it
2509 // identifiable.
2510 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2511 switch (entry.fShaderType) {
2512 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2513 fragShader = build_sksl_highlight_shader();
2514 break;
2515 }
2516 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2517 fragShader = build_glsl_highlight_shader(
2518 *ctx->priv().caps()->shaderCaps());
2519 break;
2520 }
2521 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2522 fragShader = build_metal_highlight_shader(fragShader);
2523 break;
2524 }
2525 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002526 }
2527
Brian Osmana085a412019-04-25 09:44:43 -04002528 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2529 entry.fShader,
2530 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002531 kGrShaderTypeCount);
Brian Osmanf0de96f2021-02-26 13:54:11 -05002532 fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription);
Brian Osman0b8bb882019-04-12 11:47:19 -04002533
2534 entry.fShader[kFragment_GrShaderType] = backup;
2535 }
2536 }
2537 }
Brian Osman79086b92017-02-10 13:36:16 -05002538 }
Brian Salomon99a33902017-03-07 15:16:34 -05002539 if (paramsChanged) {
2540 fDeferredActions.push_back([=]() {
2541 fWindow->setRequestedDisplayParams(params);
2542 fWindow->inval();
2543 this->updateTitle();
2544 });
2545 }
Brian Osman79086b92017-02-10 13:36:16 -05002546 ImGui::End();
2547 }
2548
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002549 if (gShaderErrorHandler.fErrors.count()) {
2550 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002551 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002552 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2553 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002554 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2555 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2556 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2557 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002558 }
2559 ImGui::End();
2560 gShaderErrorHandler.reset();
2561 }
2562
Brian Osmanf6877092017-02-13 09:39:57 -05002563 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002564 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2565 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002566 static int zoomFactor = 8;
2567 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002568 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002569 }
2570 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2571 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002572 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002573 }
Brian Osmanf6877092017-02-13 09:39:57 -05002574
Ben Wagner3627d2e2018-06-26 14:23:20 -04002575 if (!fZoomWindowFixed) {
2576 ImVec2 mousePos = ImGui::GetMousePos();
2577 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2578 }
2579 SkScalar x = fZoomWindowLocation.x();
2580 SkScalar y = fZoomWindowLocation.y();
2581 int xInt = SkScalarRoundToInt(x);
2582 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002583 ImVec2 avail = ImGui::GetContentRegionAvail();
2584
Brian Osmanead517d2017-11-13 15:36:36 -05002585 uint32_t pixel = 0;
2586 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002587 auto dContext = fWindow->directContext();
2588 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002589 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002590 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002591 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002592 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002593 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2594 }
2595
Greg Danielfbc60b72020-11-03 17:27:02 -05002596 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002597 // Translate so the region of the image that's under the mouse cursor is centered
2598 // in the zoom canvas:
2599 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002600 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2601 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002602 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002603
2604 SkPaint outline;
2605 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002606 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002607 });
Brian Osmanf6877092017-02-13 09:39:57 -05002608 }
2609
2610 ImGui::End();
2611 }
Brian Osman79086b92017-02-10 13:36:16 -05002612}
2613
John Stilesa8f6b6f2021-03-05 16:00:42 -05002614void Viewer::dumpShadersToResources() {
2615 // Sort the list of cached shaders so we can maintain some minimal level of consistency.
2616 // It doesn't really matter, but it will keep files from switching places unpredictably.
2617 std::vector<const CachedShader*> shaders;
2618 shaders.reserve(fCachedShaders.size());
2619 for (const CachedShader& shader : fCachedShaders) {
2620 shaders.push_back(&shader);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002621 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002622
2623 std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) {
2624 return std::tie(a->fShader[kFragment_GrShaderType], a->fShader[kVertex_GrShaderType]) <
2625 std::tie(b->fShader[kFragment_GrShaderType], b->fShader[kVertex_GrShaderType]);
2626 });
2627
2628 // Make the resources/sksl/SlideName/ directory.
2629 SkString directory = SkStringPrintf("%ssksl/%s",
2630 GetResourcePath().c_str(),
2631 fSlides[fCurrentSlide]->getName().c_str());
2632 if (!sk_mkdir(directory.c_str())) {
2633 SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str());
2634 return;
2635 }
2636
2637 int index = 0;
2638 for (const auto& entry : shaders) {
2639 SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index);
2640 FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag);
2641 if (vertFile) {
2642 const SkSL::String& vertText = entry->fShader[kVertex_GrShaderType];
2643 SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile));
2644 sk_fclose(vertFile);
2645 } else {
2646 SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str());
2647 }
2648
2649 SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index);
2650 FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag);
2651 if (fragFile) {
2652 const SkSL::String& fragText = entry->fShader[kFragment_GrShaderType];
2653 SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile));
2654 sk_fclose(fragFile);
2655 } else {
2656 SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str());
2657 }
2658
2659 ++index;
2660 }
2661}
2662
2663void Viewer::onIdle() {
2664 SkTArray<std::function<void()>> actionsToRun;
2665 actionsToRun.swap(fDeferredActions);
2666
2667 for (const auto& fn : actionsToRun) {
2668 fn();
2669 }
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002670
Brian Osman56a24812017-12-19 11:15:16 -05002671 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002672 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002673 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002674 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002675
Brian Osman79086b92017-02-10 13:36:16 -05002676 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002677 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2678 // not be visible, though. So we need to redraw if there is at least one visible window, or
2679 // more than one active window. Newly created windows are active but not visible for one frame
2680 // while they determine their layout and sizing.
2681 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2682 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002683 fWindow->inval();
2684 }
jvanverth9f372462016-04-06 06:08:59 -07002685}
liyuqiane5a6cd92016-05-27 08:52:52 -07002686
Florin Malitab632df72018-06-18 21:23:06 -04002687template <typename OptionsFunc>
2688static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2689 OptionsFunc&& optionsFunc) {
2690 writer.beginObject();
2691 {
2692 writer.appendString(kName , name);
2693 writer.appendString(kValue, value);
2694
2695 writer.beginArray(kOptions);
2696 {
2697 optionsFunc(writer);
2698 }
2699 writer.endArray();
2700 }
2701 writer.endObject();
2702}
2703
2704
liyuqiane5a6cd92016-05-27 08:52:52 -07002705void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002706 if (!fWindow) {
2707 return;
2708 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002709 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002710 return; // Surface hasn't been created yet.
2711 }
2712
Florin Malitab632df72018-06-18 21:23:06 -04002713 SkDynamicMemoryWStream memStream;
2714 SkJSONWriter writer(&memStream);
2715 writer.beginArray();
2716
liyuqianb73c24b2016-06-03 08:47:23 -07002717 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002718 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2719 [this](SkJSONWriter& writer) {
2720 for(const auto& slide : fSlides) {
2721 writer.appendString(slide->getName().c_str());
2722 }
2723 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002724
liyuqianb73c24b2016-06-03 08:47:23 -07002725 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002726 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2727 [](SkJSONWriter& writer) {
2728 for (const auto& str : kBackendTypeStrings) {
2729 writer.appendString(str);
2730 }
2731 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002732
csmartdalton578f0642017-02-24 16:04:47 -07002733 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002734 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2735 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2736 [this](SkJSONWriter& writer) {
2737 writer.appendS32(0);
2738
2739 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2740 return;
2741 }
2742
2743 for (int msaa : {4, 8, 16}) {
2744 writer.appendS32(msaa);
2745 }
2746 });
csmartdalton578f0642017-02-24 16:04:47 -07002747
csmartdalton61cd31a2017-02-27 17:00:53 -07002748 // Path renderer state
2749 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002750 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2751 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002752 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002753 if (!ctx) {
2754 writer.appendString("Software");
2755 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002756 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002757 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2758 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002759 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002760 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002761 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002762 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002763 }
2764 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002765 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2766 writer.appendString(
2767 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2768 }
2769 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2770 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002771 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002772 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002773 }
2774 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002775
liyuqianb73c24b2016-06-03 08:47:23 -07002776 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002777 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2778 [this](SkJSONWriter& writer) {
2779 writer.appendString(kSoftkeyHint);
2780 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2781 writer.appendString(softkey.c_str());
2782 }
2783 });
liyuqianb73c24b2016-06-03 08:47:23 -07002784
Florin Malitab632df72018-06-18 21:23:06 -04002785 writer.endArray();
2786 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002787
Florin Malitab632df72018-06-18 21:23:06 -04002788 auto data = memStream.detachAsData();
2789
2790 // TODO: would be cool to avoid this copy
2791 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2792
2793 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002794}
2795
2796void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002797 // For those who will add more features to handle the state change in this function:
2798 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2799 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2800 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002801 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002802 for (int i = 0; i < fSlides.count(); ++i) {
2803 if (fSlides[i]->getName().equals(stateValue)) {
2804 this->setCurrentSlide(i);
2805 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002806 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002807 }
Florin Malitaab99c342018-01-16 16:23:03 -05002808
2809 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002810 } else if (stateName.equals(kBackendStateName)) {
2811 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2812 if (stateValue.equals(kBackendTypeStrings[i])) {
2813 if (fBackendType != i) {
2814 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002815 for(auto& slide : fSlides) {
2816 slide->gpuTeardown();
2817 }
liyuqian6cb70252016-06-02 12:16:25 -07002818 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002819 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002820 }
2821 break;
2822 }
2823 }
csmartdalton578f0642017-02-24 16:04:47 -07002824 } else if (stateName.equals(kMSAAStateName)) {
2825 DisplayParams params = fWindow->getRequestedDisplayParams();
2826 int sampleCount = atoi(stateValue.c_str());
2827 if (sampleCount != params.fMSAASampleCount) {
2828 params.fMSAASampleCount = sampleCount;
2829 fWindow->setRequestedDisplayParams(params);
2830 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002831 this->updateTitle();
2832 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002833 }
2834 } else if (stateName.equals(kPathRendererStateName)) {
2835 DisplayParams params = fWindow->getRequestedDisplayParams();
2836 for (const auto& pair : gPathRendererNames) {
2837 if (pair.second == stateValue.c_str()) {
2838 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2839 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2840 fWindow->setRequestedDisplayParams(params);
2841 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002842 this->updateTitle();
2843 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002844 }
2845 break;
2846 }
csmartdalton578f0642017-02-24 16:04:47 -07002847 }
liyuqianb73c24b2016-06-03 08:47:23 -07002848 } else if (stateName.equals(kSoftkeyStateName)) {
2849 if (!stateValue.equals(kSoftkeyHint)) {
2850 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002851 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002852 }
liyuqian2edb0f42016-07-06 14:11:32 -07002853 } else if (stateName.equals(kRefreshStateName)) {
2854 // This state is actually NOT in the UI state.
2855 // We use this to allow Android to quickly set bool fRefresh.
2856 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002857 } else {
2858 SkDebugf("Unknown stateName: %s", stateName.c_str());
2859 }
2860}
Brian Osman79086b92017-02-10 13:36:16 -05002861
Hal Canaryb1f411a2019-08-29 10:39:22 -04002862bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002863 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002864}
2865
Hal Canaryb1f411a2019-08-29 10:39:22 -04002866bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002867 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002868 fWindow->inval();
2869 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002870 } else {
2871 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002872 }
Brian Osman79086b92017-02-10 13:36:16 -05002873}