blob: 6c433400b87e5142b6c1f7b9f859ff0a535a5732 [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) {
1548 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1549 SkPaint paint;
1550 paint.setColor(0x40FFFF00);
1551 surface->getCanvas()->drawRect(r, paint);
1552 }
liyuqian6f163d22016-06-13 12:26:45 -07001553}
1554
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001555void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001556 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001557 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001558}
Jim Van Verth6f449692017-02-14 15:16:46 -05001559
Robert Phillips9882dae2019-03-04 11:00:10 -05001560void Viewer::onPaint(SkSurface* surface) {
1561 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001562
Robert Phillips9882dae2019-03-04 11:00:10 -05001563 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001564
Brian Osmand67e5182017-12-08 16:46:09 -05001565 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001566
Greg Daniel427d8eb2020-09-28 15:04:18 -04001567 fLastImage.reset();
1568
Robert Phillipsed653392020-07-10 13:55:21 -04001569 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001570 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001571 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001572 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001573}
1574
Ben Wagnera1915972018-08-09 15:06:19 -04001575void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001576 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001577 SkScalar scaleFactor = 1.0;
1578 if (fApplyBackingScale) {
1579 scaleFactor = fWindow->scaleFactor();
1580 }
1581 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001582 }
Ben Wagnera1915972018-08-09 15:06:19 -04001583}
1584
Florin Malitacefc1b92018-02-19 21:43:47 -05001585SkPoint Viewer::mapEvent(float x, float y) {
1586 const auto m = this->computeMatrix();
1587 SkMatrix inv;
1588
1589 SkAssertResult(m.invert(&inv));
1590
1591 return inv.mapXY(x, y);
1592}
1593
Hal Canaryb1f411a2019-08-29 10:39:22 -04001594bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001595 if (GestureDevice::kMouse == fGestureDevice) {
1596 return false;
1597 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001598
1599 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001600 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001601 fWindow->inval();
1602 return true;
1603 }
1604
liyuqiand3cdbca2016-05-17 12:44:20 -07001605 void* castedOwner = reinterpret_cast<void*>(owner);
1606 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001607 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001608 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001609#if defined(SK_BUILD_FOR_IOS)
1610 // TODO: move IOS swipe detection higher up into the platform code
1611 SkPoint dir;
1612 if (fGesture.isFling(&dir)) {
1613 // swiping left or right
1614 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1615 if (dir.fX < 0) {
1616 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1617 fCurrentSlide + 1 : 0);
1618 } else {
1619 this->setCurrentSlide(fCurrentSlide > 0 ?
1620 fCurrentSlide - 1 : fSlides.count() - 1);
1621 }
1622 }
1623 fGesture.reset();
1624 }
1625#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001626 break;
1627 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001628 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001629 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001630 break;
1631 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001632 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001633 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001634 break;
1635 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001636 default: {
1637 // kLeft and kRight are only for swipes
1638 SkASSERT(false);
1639 break;
1640 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001641 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001642 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001643 fWindow->inval();
1644 return true;
1645}
1646
Hal Canaryb1f411a2019-08-29 10:39:22 -04001647bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001648 if (GestureDevice::kTouch == fGestureDevice) {
1649 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001650 }
Brian Osman16c81a12017-12-20 11:58:34 -05001651
Florin Malitacefc1b92018-02-19 21:43:47 -05001652 const auto slidePt = this->mapEvent(x, y);
1653 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1654 fWindow->inval();
1655 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001656 }
1657
1658 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001659 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001660 fGesture.touchEnd(nullptr);
1661 break;
1662 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001663 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001664 fGesture.touchBegin(nullptr, x, y);
1665 break;
1666 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001667 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001668 fGesture.touchMoved(nullptr, x, y);
1669 break;
1670 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001671 default: {
1672 SkASSERT(false); // shouldn't see kRight or kLeft here
1673 break;
1674 }
Brian Osman16c81a12017-12-20 11:58:34 -05001675 }
1676 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1677
Hal Canaryb1f411a2019-08-29 10:39:22 -04001678 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001679 fWindow->inval();
1680 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001681 return true;
1682}
1683
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001684bool Viewer::onFling(skui::InputState state) {
1685 if (skui::InputState::kRight == state) {
1686 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1687 return true;
1688 } else if (skui::InputState::kLeft == state) {
1689 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1690 return true;
1691 }
1692 return false;
1693}
1694
1695bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1696 switch (state) {
1697 case skui::InputState::kDown:
1698 fGesture.startZoom();
1699 return true;
1700 break;
1701 case skui::InputState::kMove:
1702 fGesture.updateZoom(scale, x, y, x, y);
1703 return true;
1704 break;
1705 case skui::InputState::kUp:
1706 fGesture.endZoom();
1707 return true;
1708 break;
1709 default:
1710 SkASSERT(false);
1711 break;
1712 }
1713
1714 return false;
1715}
1716
Brian Osmana109e392017-02-24 09:49:14 -05001717static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001718 // The gamut image covers a (0.8 x 0.9) shaped region
1719 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001720
1721 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1722 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1723 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001724 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1725 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1726 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001727
Brian Osman535c5e32019-02-09 16:32:58 -05001728 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1729 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1730 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1731 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1732 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001733}
1734
Ben Wagner3627d2e2018-06-26 14:23:20 -04001735static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001736 ImGui::DragCanvas dc(pt);
1737 dc.fillColor(IM_COL32(0, 0, 0, 128));
1738 dc.dragPoint(pt);
1739 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001740}
1741
Brian Osman9bb47cf2018-04-26 15:55:00 -04001742static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001743 ImGui::DragCanvas dc(pts);
1744 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001745
Brian Osman535c5e32019-02-09 16:32:58 -05001746 for (int i = 0; i < 4; ++i) {
1747 dc.dragPoint(pts + i);
1748 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001749
Brian Osman535c5e32019-02-09 16:32:58 -05001750 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1751 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1752 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1753 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001754
Brian Osman535c5e32019-02-09 16:32:58 -05001755 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001756}
1757
John Stiles38b7d2f2020-06-24 12:13:31 -04001758static SkSL::String build_sksl_highlight_shader() {
1759 return SkSL::String("out half4 sk_FragColor;\n"
1760 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1761}
1762
1763static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1764 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1765 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1766 size_t pos = inShader.rfind("return *_out;\n");
1767 if (pos == std::string::npos) {
1768 return inShader;
1769 }
1770
1771 SkSL::String replacementShader = inShader;
1772 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1773 return replacementShader;
1774}
1775
1776static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1777 const char* versionDecl = shaderCaps.versionDeclString();
1778 SkSL::String highlight = versionDecl ? versionDecl : "";
1779 if (shaderCaps.usesPrecisionModifiers()) {
1780 highlight.append("precision mediump float;\n");
1781 }
1782 highlight.appendf("out vec4 sk_FragColor;\n"
1783 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1784 return highlight;
1785}
1786
Brian Osmand67e5182017-12-08 16:46:09 -05001787void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001788 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1789 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001790 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001791 }
1792
1793 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001794 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1795 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001796 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001797 DisplayParams params = fWindow->getRequestedDisplayParams();
1798 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001799 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001800
Brian Osmana109e392017-02-24 09:49:14 -05001801 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1802 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001803 if (ImGui::CollapsingHeader("Backend")) {
1804 int newBackend = static_cast<int>(fBackendType);
1805 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1806 ImGui::SameLine();
1807 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001808#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1809 ImGui::SameLine();
1810 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1811#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001812#if defined(SK_DAWN)
1813 ImGui::SameLine();
1814 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1815#endif
John Stilesf7da9232020-11-19 19:58:14 -05001816#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001817 ImGui::SameLine();
1818 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1819#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001820#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001821 ImGui::SameLine();
1822 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1823#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001824#if defined(SK_DIRECT3D)
1825 ImGui::SameLine();
1826 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1827#endif
Brian Osman621491e2017-02-28 15:45:01 -05001828 if (newBackend != fBackendType) {
1829 fDeferredActions.push_back([=]() {
1830 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1831 });
1832 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001833
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001834 bool* wire = &params.fGrContextOptions.fWireframeMode;
1835 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1836 paramsChanged = true;
1837 }
Brian Salomon99a33902017-03-07 15:16:34 -05001838
Brian Salomon91216d52021-04-09 11:57:59 -04001839 bool* reducedShaders = &params.fGrContextOptions.fReducedShaderVariations;
1840 if (ctx && ImGui::Checkbox("Reduced shaders", reducedShaders)) {
1841 paramsChanged = true;
1842 }
1843
Brian Osman28b12522017-03-08 17:10:24 -05001844 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001845 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001846 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001847 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1848 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1849 1;
1850
1851 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1852 if (maxMSAA >= 4) {
1853 ImGui::Text("MSAA: ");
1854
1855 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1856 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1857 // don't show it in the list.
1858 if (curMSAA == 2) {
1859 continue;
1860 }
1861 ImGui::SameLine();
1862 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1863 &sampleCount, curMSAA);
1864 }
1865 }
Brian Osman28b12522017-03-08 17:10:24 -05001866
1867 if (sampleCount != params.fMSAASampleCount) {
1868 params.fMSAASampleCount = sampleCount;
1869 paramsChanged = true;
1870 }
1871 }
1872
Ben Wagner37c54032018-04-13 14:30:23 -04001873 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001874 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001875 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1876 }
1877 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1878 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1879 {
1880 uint32_t flags = params.fSurfaceProps.flags();
1881 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001882 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1883 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1884 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001885 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001886 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001887 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1888 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1889 }
1890 paramsChanged = true;
1891 }
1892
1893 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1894 if (ImGui::Checkbox("DFT", &useDFT)) {
1895 uint32_t flags = params.fSurfaceProps.flags();
1896 if (useDFT) {
1897 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1898 } else {
1899 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1900 }
1901 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1902 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1903 paramsChanged = true;
1904 }
1905
Brian Osman8a9de3d2017-03-01 14:59:05 -05001906 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001907 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001908 auto prButton = [&](GpuPathRenderers x) {
1909 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001910 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1911 params.fGrContextOptions.fGpuPathRenderers = x;
1912 paramsChanged = true;
1913 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001914 }
1915 };
1916
1917 if (!ctx) {
1918 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001919 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001920 const auto* caps = ctx->priv().caps();
1921 prButton(GpuPathRenderers::kDefault);
1922 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001923 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001924 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001925 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001926 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001927 if (1 == fWindow->sampleCount()) {
1928 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1929 prButton(GpuPathRenderers::kCoverageCounting);
1930 }
1931 prButton(GpuPathRenderers::kSmall);
1932 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001933 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001934 prButton(GpuPathRenderers::kNone);
1935 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001936 ImGui::TreePop();
1937 }
Brian Osman621491e2017-02-28 15:45:01 -05001938 }
1939
Ben Wagner964571d2019-03-08 12:35:06 -05001940 if (ImGui::CollapsingHeader("Tiling")) {
1941 ImGui::Checkbox("Enable", &fTiled);
1942 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1943 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1944 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1945 }
1946
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001947 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001948 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1949 this->preTouchMatrixChanged();
1950 this->onResize(fWindow->width(), fWindow->height());
1951 paramsChanged = true;
1952 }
1953
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001954 float zoom = fZoomLevel;
1955 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1956 fZoomLevel = zoom;
1957 this->preTouchMatrixChanged();
1958 paramsChanged = true;
1959 }
1960 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001961 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001962 fRotation = deg;
1963 this->preTouchMatrixChanged();
1964 paramsChanged = true;
1965 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001966 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1967 if (ImGui_DragLocation(&fOffset)) {
1968 this->preTouchMatrixChanged();
1969 paramsChanged = true;
1970 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001971 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1972 this->preTouchMatrixChanged();
1973 paramsChanged = true;
1974 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001975 }
Brian Osman805a7272018-05-02 15:40:20 -04001976 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1977 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1978 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001979 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001980 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001981 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001982 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001983 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001984 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001985 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001986 }
1987
Ben Wagnera580fb32018-04-17 11:16:32 -04001988 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001989 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001990 if (fPaintOverrides.fAntiAlias) {
1991 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001992 }
1993 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001994 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001995 {
1996 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1997 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001998 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001999 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
2000 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002001 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002002 fPaintOverrides.fAntiAlias = true;
2003 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04002004 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05002005 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04002006 case SkPaintFields::AntiAliasState::Alias:
2007 break;
2008 case SkPaintFields::AntiAliasState::Normal:
2009 break;
2010 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
2011 gSkUseAnalyticAA = true;
2012 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002013 break;
2014 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2015 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002016 break;
2017 }
2018 }
2019 paramsChanged = true;
2020 }
2021
Ben Wagner99a78dc2018-05-09 18:23:51 -04002022 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002023 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002024 bool (SkPaint::* isFlag)() const,
2025 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002026 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002027 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002028 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002029 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002030 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002031 if (ImGui::Combo(label, &itemIndex, items)) {
2032 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002033 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002034 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002035 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002036 (fPaint.*setFlag)(itemIndex == 2);
2037 }
2038 paramsChanged = true;
2039 }
2040 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002041
Ben Wagner99a78dc2018-05-09 18:23:51 -04002042 paintFlag("Dither",
2043 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002044 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002045 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002046
2047 int styleIdx = 0;
2048 if (fPaintOverrides.fStyle) {
2049 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2050 }
2051 if (ImGui::Combo("Style", &styleIdx,
2052 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2053 {
2054 if (styleIdx == 0) {
2055 fPaintOverrides.fStyle = false;
2056 fPaint.setStyle(SkPaint::kFill_Style);
2057 } else {
2058 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2059 fPaintOverrides.fStyle = true;
2060 }
2061 paramsChanged = true;
2062 }
2063
2064 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2065 if (fPaintOverrides.fWidth) {
2066 float width = fPaint.getStrokeWidth();
2067 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2068 fPaint.setStrokeWidth(width);
2069 paramsChanged = true;
2070 }
2071 }
2072
2073 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2074 if (fPaintOverrides.fMiterLimit) {
2075 float miterLimit = fPaint.getStrokeMiter();
2076 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2077 fPaint.setStrokeMiter(miterLimit);
2078 paramsChanged = true;
2079 }
2080 }
2081
2082 int capIdx = 0;
2083 if (fPaintOverrides.fCapType) {
2084 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2085 }
2086 if (ImGui::Combo("Cap Type", &capIdx,
2087 "Default\0Butt\0Round\0Square\0\0"))
2088 {
2089 if (capIdx == 0) {
2090 fPaintOverrides.fCapType = false;
2091 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2092 } else {
2093 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2094 fPaintOverrides.fCapType = true;
2095 }
2096 paramsChanged = true;
2097 }
2098
2099 int joinIdx = 0;
2100 if (fPaintOverrides.fJoinType) {
2101 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2102 }
2103 if (ImGui::Combo("Join Type", &joinIdx,
2104 "Default\0Miter\0Round\0Bevel\0\0"))
2105 {
2106 if (joinIdx == 0) {
2107 fPaintOverrides.fJoinType = false;
2108 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2109 } else {
2110 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2111 fPaintOverrides.fJoinType = true;
2112 }
2113 paramsChanged = true;
2114 }
Ben Wagner9613e452019-01-23 10:34:59 -05002115 }
Hal Canary02738a82019-01-21 18:51:32 +00002116
Ben Wagner9613e452019-01-23 10:34:59 -05002117 if (ImGui::CollapsingHeader("Font")) {
2118 int hintingIdx = 0;
2119 if (fFontOverrides.fHinting) {
2120 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2121 }
2122 if (ImGui::Combo("Hinting", &hintingIdx,
2123 "Default\0None\0Slight\0Normal\0Full\0\0"))
2124 {
2125 if (hintingIdx == 0) {
2126 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002127 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002128 } else {
2129 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2130 fFontOverrides.fHinting = true;
2131 }
2132 paramsChanged = true;
2133 }
Hal Canary02738a82019-01-21 18:51:32 +00002134
Ben Wagner9613e452019-01-23 10:34:59 -05002135 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2136 bool SkFontFields::* flag,
2137 bool (SkFont::* isFlag)() const,
2138 void (SkFont::* setFlag)(bool) )
2139 {
2140 int itemIndex = 0;
2141 if (fFontOverrides.*flag) {
2142 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2143 }
2144 if (ImGui::Combo(label, &itemIndex, items)) {
2145 if (itemIndex == 0) {
2146 fFontOverrides.*flag = false;
2147 } else {
2148 fFontOverrides.*flag = true;
2149 (fFont.*setFlag)(itemIndex == 2);
2150 }
2151 paramsChanged = true;
2152 }
2153 };
Hal Canary02738a82019-01-21 18:51:32 +00002154
Ben Wagner9613e452019-01-23 10:34:59 -05002155 fontFlag("Fake Bold Glyphs",
2156 "Default\0No Fake Bold\0Fake Bold\0\0",
2157 &SkFontFields::fEmbolden,
2158 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002159
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002160 fontFlag("Baseline Snapping",
2161 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2162 &SkFontFields::fBaselineSnap,
2163 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2164
Ben Wagner9613e452019-01-23 10:34:59 -05002165 fontFlag("Linear Text",
2166 "Default\0No Linear Text\0Linear Text\0\0",
2167 &SkFontFields::fLinearMetrics,
2168 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002169
Ben Wagner9613e452019-01-23 10:34:59 -05002170 fontFlag("Subpixel Position Glyphs",
2171 "Default\0Pixel Text\0Subpixel Text\0\0",
2172 &SkFontFields::fSubpixel,
2173 &SkFont::isSubpixel, &SkFont::setSubpixel);
2174
2175 fontFlag("Embedded Bitmap Text",
2176 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2177 &SkFontFields::fEmbeddedBitmaps,
2178 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2179
2180 fontFlag("Force Auto-Hinting",
2181 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2182 &SkFontFields::fForceAutoHinting,
2183 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2184
2185 int edgingIdx = 0;
2186 if (fFontOverrides.fEdging) {
2187 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2188 }
2189 if (ImGui::Combo("Edging", &edgingIdx,
2190 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2191 {
2192 if (edgingIdx == 0) {
2193 fFontOverrides.fEdging = false;
2194 fFont.setEdging(SkFont::Edging::kAlias);
2195 } else {
2196 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2197 fFontOverrides.fEdging = true;
2198 }
2199 paramsChanged = true;
2200 }
2201
Ben Wagner15a8d572019-03-21 13:35:44 -04002202 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2203 if (fFontOverrides.fSize) {
2204 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002205 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002206 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002207 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002208 fFontOverrides.fSizeRange[0],
2209 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002210 "%.6f", 2.0f))
2211 {
Mike Reed3ae47332019-01-04 10:11:46 -05002212 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002213 paramsChanged = true;
2214 }
2215 }
2216
2217 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2218 if (fFontOverrides.fScaleX) {
2219 float scaleX = fFont.getScaleX();
2220 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2221 fFont.setScaleX(scaleX);
2222 paramsChanged = true;
2223 }
2224 }
2225
2226 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2227 if (fFontOverrides.fSkewX) {
2228 float skewX = fFont.getSkewX();
2229 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2230 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002231 paramsChanged = true;
2232 }
2233 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002234 }
2235
Mike Reed81f60ec2018-05-15 10:09:52 -04002236 {
2237 SkMetaData controls;
2238 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2239 if (ImGui::CollapsingHeader("Current Slide")) {
2240 SkMetaData::Iter iter(controls);
2241 const char* name;
2242 SkMetaData::Type type;
2243 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002244 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002245 if (type == SkMetaData::kScalar_Type) {
2246 float val[3];
2247 SkASSERT(count == 3);
2248 controls.findScalars(name, &count, val);
2249 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2250 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002251 }
Ben Wagner110c7032019-03-22 17:03:59 -04002252 } else if (type == SkMetaData::kBool_Type) {
2253 bool val;
2254 SkASSERT(count == 1);
2255 controls.findBool(name, &val);
2256 if (ImGui::Checkbox(name, &val)) {
2257 controls.setBool(name, val);
2258 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002259 }
2260 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002261 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002262 }
2263 }
2264 }
2265
Ben Wagner7a3c6742018-04-23 10:01:07 -04002266 if (fShowSlidePicker) {
2267 ImGui::SetNextTreeNodeOpen(true);
2268 }
Brian Osman79086b92017-02-10 13:36:16 -05002269 if (ImGui::CollapsingHeader("Slide")) {
2270 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002271 static ImVector<const char*> filteredSlideNames;
2272 static ImVector<int> filteredSlideIndices;
2273
Brian Osmanfce09c52017-11-14 15:32:20 -05002274 if (fShowSlidePicker) {
2275 ImGui::SetKeyboardFocusHere();
2276 fShowSlidePicker = false;
2277 }
2278
Brian Osman79086b92017-02-10 13:36:16 -05002279 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002280 filteredSlideNames.clear();
2281 filteredSlideIndices.clear();
2282 int filteredIndex = 0;
2283 for (int i = 0; i < fSlides.count(); ++i) {
2284 const char* slideName = fSlides[i]->getName().c_str();
2285 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2286 if (i == fCurrentSlide) {
2287 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002288 }
Brian Osmanf479e422017-11-08 13:11:36 -05002289 filteredSlideNames.push_back(slideName);
2290 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002291 }
Brian Osman79086b92017-02-10 13:36:16 -05002292 }
Brian Osmanf479e422017-11-08 13:11:36 -05002293
Brian Osmanf479e422017-11-08 13:11:36 -05002294 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2295 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002296 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002297 }
2298 }
Brian Osmana109e392017-02-24 09:49:14 -05002299
2300 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002301 ColorMode newMode = fColorMode;
2302 auto cmButton = [&](ColorMode mode, const char* label) {
2303 if (ImGui::RadioButton(label, mode == fColorMode)) {
2304 newMode = mode;
2305 }
2306 };
2307
2308 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002309 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2310 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002311 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002312
2313 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002314 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002315 }
2316
2317 // Pick from common gamuts:
2318 int primariesIdx = 4; // Default: Custom
2319 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2320 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2321 primariesIdx = i;
2322 break;
2323 }
2324 }
2325
Brian Osman03115dc2018-11-26 13:55:19 -05002326 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002327 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002328
Brian Osmana109e392017-02-24 09:49:14 -05002329 if (ImGui::Combo("Primaries", &primariesIdx,
2330 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2331 if (primariesIdx >= 0 && primariesIdx <= 3) {
2332 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2333 }
2334 }
2335
2336 // Allow direct editing of gamut
2337 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2338 }
Brian Osman207d4102019-01-10 09:40:58 -05002339
2340 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002341 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002342 if (ImGui::Checkbox("Pause", &isPaused)) {
2343 fAnimTimer.togglePauseResume();
2344 }
Brian Osman707d2022019-01-10 11:27:34 -05002345
2346 float speed = fAnimTimer.getSpeed();
2347 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2348 fAnimTimer.setSpeed(speed);
2349 }
Brian Osman207d4102019-01-10 09:40:58 -05002350 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002351
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002352 if (ImGui::CollapsingHeader("Shaders")) {
2353 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2354 GrContextOptions::ShaderCacheStrategy::kSkSL;
John Stiles7247b482021-03-08 10:40:35 -05002355
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002356#if defined(SK_VULKAN)
2357 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2358#else
2359 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002360#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002361
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002362 // To re-load shaders from the currently active programs, we flush all
2363 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002364 static bool gLoadPending = false;
2365 if (gLoadPending) {
2366 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
Brian Osmanf0de96f2021-02-26 13:54:11 -05002367 const SkString& description, int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002368 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002369 entry.fKey = key;
2370 SkMD5 hash;
2371 hash.write(key->bytes(), key->size());
2372 SkMD5::Digest digest = hash.finish();
2373 for (int i = 0; i < 16; ++i) {
2374 entry.fKeyString.appendf("%02x", digest.data[i]);
2375 }
Brian Osmanf0de96f2021-02-26 13:54:11 -05002376 entry.fKeyDescription = description;
Brian Osman0b8bb882019-04-12 11:47:19 -04002377
Brian Osman9e4e4c72020-06-10 07:19:34 -04002378 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002379 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002380 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2381 entry.fInputs,
2382 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002383 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002384 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002385 fPersistentCache.foreach(collectShaders);
2386 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002387
2388#if defined(SK_VULKAN)
2389 if (isVulkan && !sksl) {
2390 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2391 for (auto& entry : fCachedShaders) {
2392 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2393 const SkSL::String& spirv(entry.fShader[i]);
2394 std::string disasm;
2395 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2396 &disasm);
2397 entry.fShader[i].assign(disasm);
2398 }
2399 }
2400 }
2401#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002402 }
2403
John Stilesa8f6b6f2021-03-05 16:00:42 -05002404 // Defer actually doing the View/Apply logic so that we can trigger an Apply when we
Brian Osman0b8bb882019-04-12 11:47:19 -04002405 // start or finish hovering on a tree node in the list below:
John Stilesa8f6b6f2021-03-05 16:00:42 -05002406 bool doView = ImGui::Button("View"); ImGui::SameLine();
2407 bool doApply = ImGui::Button("Apply Changes"); ImGui::SameLine();
John Stiles7247b482021-03-08 10:40:35 -05002408 bool doDump = ImGui::Button("Dump SkSL to resources/sksl/");
John Stilesa8f6b6f2021-03-05 16:00:42 -05002409
John Stiles2ee4d7a2021-03-30 10:30:47 -04002410 int newOptLevel = fOptLevel;
John Stiles7247b482021-03-08 10:40:35 -05002411 ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source);
2412 ImGui::SameLine();
2413 ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile);
2414 ImGui::SameLine();
2415 ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize);
2416 ImGui::SameLine();
2417 ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline);
John Stilesa8f6b6f2021-03-05 16:00:42 -05002418
John Stiles7247b482021-03-08 10:40:35 -05002419 // If we are changing the compile mode, we want to reset the cache and redo
2420 // everything.
John Stiles2ee4d7a2021-03-30 10:30:47 -04002421 if (doDump || newOptLevel != fOptLevel) {
John Stiles7247b482021-03-08 10:40:35 -05002422 sksl = doDump || (newOptLevel == kShaderOptLevel_Source);
John Stiles2ee4d7a2021-03-30 10:30:47 -04002423 fOptLevel = (ShaderOptLevel)newOptLevel;
2424 switch (fOptLevel) {
2425 case kShaderOptLevel_Source:
2426 Compiler::EnableOptimizer(OverrideFlag::kDefault);
2427 Compiler::EnableInliner(OverrideFlag::kDefault);
2428 break;
2429 case kShaderOptLevel_Compile:
2430 Compiler::EnableOptimizer(OverrideFlag::kOff);
2431 Compiler::EnableInliner(OverrideFlag::kOff);
2432 break;
2433 case kShaderOptLevel_Optimize:
2434 Compiler::EnableOptimizer(OverrideFlag::kOn);
2435 Compiler::EnableInliner(OverrideFlag::kOff);
2436 break;
2437 case kShaderOptLevel_Inline:
2438 Compiler::EnableOptimizer(OverrideFlag::kOn);
2439 Compiler::EnableInliner(OverrideFlag::kOn);
2440 break;
2441 }
John Stiles7247b482021-03-08 10:40:35 -05002442
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002443 params.fGrContextOptions.fShaderCacheStrategy =
2444 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2445 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2446 paramsChanged = true;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002447 doView = true;
2448
2449 fDeferredActions.push_back([=]() {
2450 // Reset the cache.
2451 fPersistentCache.reset();
2452 // Dump the cache once we have drawn a frame with it.
2453 if (doDump) {
2454 fDeferredActions.push_back([this]() {
2455 this->dumpShadersToResources();
2456 });
2457 }
2458 });
Brian Osmancbc33b82019-04-19 14:16:19 -04002459 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002460
2461 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002462 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002463 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2464 bool hovered = ImGui::IsItemHovered();
2465 if (hovered != entry.fHovered) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002466 // Force an Apply to patch the highlight shader in/out
Brian Osman0b8bb882019-04-12 11:47:19 -04002467 entry.fHovered = hovered;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002468 doApply = true;
Brian Osman0b8bb882019-04-12 11:47:19 -04002469 }
2470 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002471 auto stringBox = [](const char* label, std::string* str) {
2472 // Full width, and not too much space for each shader
2473 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2474 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2475 ImGui::InputTextMultiline(label, str, boxSize);
2476 };
Brian Osmanf0de96f2021-02-26 13:54:11 -05002477 if (ImGui::TreeNode("Key")) {
2478 ImGui::TextWrapped("%s", entry.fKeyDescription.c_str());
2479 ImGui::TreePop();
2480 }
Brian Osmaneb3fb902020-08-18 13:16:59 -04002481 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2482 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002483 ImGui::TreePop();
2484 }
2485 }
2486 ImGui::EndChild();
2487
John Stilesa8f6b6f2021-03-05 16:00:42 -05002488 if (doView) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002489 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002490 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002491 gLoadPending = true;
2492 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002493
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002494 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2495 // but I'm not sure anyone wants to do that.
2496 if (isVulkan && !sksl) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002497 doApply = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002498 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002499 if (doApply) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002500 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002501 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002502 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002503 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002504 if (entry.fHovered) {
2505 // The hovered item (if any) gets a special shader to make it
2506 // identifiable.
2507 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2508 switch (entry.fShaderType) {
2509 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2510 fragShader = build_sksl_highlight_shader();
2511 break;
2512 }
2513 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2514 fragShader = build_glsl_highlight_shader(
2515 *ctx->priv().caps()->shaderCaps());
2516 break;
2517 }
2518 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2519 fragShader = build_metal_highlight_shader(fragShader);
2520 break;
2521 }
2522 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002523 }
2524
Brian Osmana085a412019-04-25 09:44:43 -04002525 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2526 entry.fShader,
2527 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002528 kGrShaderTypeCount);
Brian Osmanf0de96f2021-02-26 13:54:11 -05002529 fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription);
Brian Osman0b8bb882019-04-12 11:47:19 -04002530
2531 entry.fShader[kFragment_GrShaderType] = backup;
2532 }
2533 }
2534 }
Brian Osman79086b92017-02-10 13:36:16 -05002535 }
Brian Salomon99a33902017-03-07 15:16:34 -05002536 if (paramsChanged) {
2537 fDeferredActions.push_back([=]() {
2538 fWindow->setRequestedDisplayParams(params);
2539 fWindow->inval();
2540 this->updateTitle();
2541 });
2542 }
Brian Osman79086b92017-02-10 13:36:16 -05002543 ImGui::End();
2544 }
2545
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002546 if (gShaderErrorHandler.fErrors.count()) {
2547 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002548 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002549 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2550 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002551 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2552 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2553 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2554 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002555 }
2556 ImGui::End();
2557 gShaderErrorHandler.reset();
2558 }
2559
Brian Osmanf6877092017-02-13 09:39:57 -05002560 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002561 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2562 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002563 static int zoomFactor = 8;
2564 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002565 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002566 }
2567 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2568 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002569 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002570 }
Brian Osmanf6877092017-02-13 09:39:57 -05002571
Ben Wagner3627d2e2018-06-26 14:23:20 -04002572 if (!fZoomWindowFixed) {
2573 ImVec2 mousePos = ImGui::GetMousePos();
2574 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2575 }
2576 SkScalar x = fZoomWindowLocation.x();
2577 SkScalar y = fZoomWindowLocation.y();
2578 int xInt = SkScalarRoundToInt(x);
2579 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002580 ImVec2 avail = ImGui::GetContentRegionAvail();
2581
Brian Osmanead517d2017-11-13 15:36:36 -05002582 uint32_t pixel = 0;
2583 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002584 auto dContext = fWindow->directContext();
2585 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002586 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002587 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002588 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002589 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002590 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2591 }
2592
Greg Danielfbc60b72020-11-03 17:27:02 -05002593 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002594 // Translate so the region of the image that's under the mouse cursor is centered
2595 // in the zoom canvas:
2596 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002597 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2598 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002599 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002600
2601 SkPaint outline;
2602 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002603 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002604 });
Brian Osmanf6877092017-02-13 09:39:57 -05002605 }
2606
2607 ImGui::End();
2608 }
Brian Osman79086b92017-02-10 13:36:16 -05002609}
2610
John Stilesa8f6b6f2021-03-05 16:00:42 -05002611void Viewer::dumpShadersToResources() {
2612 // Sort the list of cached shaders so we can maintain some minimal level of consistency.
2613 // It doesn't really matter, but it will keep files from switching places unpredictably.
2614 std::vector<const CachedShader*> shaders;
2615 shaders.reserve(fCachedShaders.size());
2616 for (const CachedShader& shader : fCachedShaders) {
2617 shaders.push_back(&shader);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002618 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002619
2620 std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) {
2621 return std::tie(a->fShader[kFragment_GrShaderType], a->fShader[kVertex_GrShaderType]) <
2622 std::tie(b->fShader[kFragment_GrShaderType], b->fShader[kVertex_GrShaderType]);
2623 });
2624
2625 // Make the resources/sksl/SlideName/ directory.
2626 SkString directory = SkStringPrintf("%ssksl/%s",
2627 GetResourcePath().c_str(),
2628 fSlides[fCurrentSlide]->getName().c_str());
2629 if (!sk_mkdir(directory.c_str())) {
2630 SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str());
2631 return;
2632 }
2633
2634 int index = 0;
2635 for (const auto& entry : shaders) {
2636 SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index);
2637 FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag);
2638 if (vertFile) {
2639 const SkSL::String& vertText = entry->fShader[kVertex_GrShaderType];
2640 SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile));
2641 sk_fclose(vertFile);
2642 } else {
2643 SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str());
2644 }
2645
2646 SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index);
2647 FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag);
2648 if (fragFile) {
2649 const SkSL::String& fragText = entry->fShader[kFragment_GrShaderType];
2650 SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile));
2651 sk_fclose(fragFile);
2652 } else {
2653 SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str());
2654 }
2655
2656 ++index;
2657 }
2658}
2659
2660void Viewer::onIdle() {
2661 SkTArray<std::function<void()>> actionsToRun;
2662 actionsToRun.swap(fDeferredActions);
2663
2664 for (const auto& fn : actionsToRun) {
2665 fn();
2666 }
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002667
Brian Osman56a24812017-12-19 11:15:16 -05002668 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002669 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002670 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002671 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002672
Brian Osman79086b92017-02-10 13:36:16 -05002673 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002674 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2675 // not be visible, though. So we need to redraw if there is at least one visible window, or
2676 // more than one active window. Newly created windows are active but not visible for one frame
2677 // while they determine their layout and sizing.
2678 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2679 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002680 fWindow->inval();
2681 }
jvanverth9f372462016-04-06 06:08:59 -07002682}
liyuqiane5a6cd92016-05-27 08:52:52 -07002683
Florin Malitab632df72018-06-18 21:23:06 -04002684template <typename OptionsFunc>
2685static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2686 OptionsFunc&& optionsFunc) {
2687 writer.beginObject();
2688 {
2689 writer.appendString(kName , name);
2690 writer.appendString(kValue, value);
2691
2692 writer.beginArray(kOptions);
2693 {
2694 optionsFunc(writer);
2695 }
2696 writer.endArray();
2697 }
2698 writer.endObject();
2699}
2700
2701
liyuqiane5a6cd92016-05-27 08:52:52 -07002702void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002703 if (!fWindow) {
2704 return;
2705 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002706 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002707 return; // Surface hasn't been created yet.
2708 }
2709
Florin Malitab632df72018-06-18 21:23:06 -04002710 SkDynamicMemoryWStream memStream;
2711 SkJSONWriter writer(&memStream);
2712 writer.beginArray();
2713
liyuqianb73c24b2016-06-03 08:47:23 -07002714 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002715 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2716 [this](SkJSONWriter& writer) {
2717 for(const auto& slide : fSlides) {
2718 writer.appendString(slide->getName().c_str());
2719 }
2720 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002721
liyuqianb73c24b2016-06-03 08:47:23 -07002722 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002723 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2724 [](SkJSONWriter& writer) {
2725 for (const auto& str : kBackendTypeStrings) {
2726 writer.appendString(str);
2727 }
2728 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002729
csmartdalton578f0642017-02-24 16:04:47 -07002730 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002731 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2732 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2733 [this](SkJSONWriter& writer) {
2734 writer.appendS32(0);
2735
2736 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2737 return;
2738 }
2739
2740 for (int msaa : {4, 8, 16}) {
2741 writer.appendS32(msaa);
2742 }
2743 });
csmartdalton578f0642017-02-24 16:04:47 -07002744
csmartdalton61cd31a2017-02-27 17:00:53 -07002745 // Path renderer state
2746 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002747 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2748 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002749 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002750 if (!ctx) {
2751 writer.appendString("Software");
2752 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002753 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002754 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2755 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002756 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002757 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002758 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002759 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002760 }
2761 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002762 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2763 writer.appendString(
2764 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2765 }
2766 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2767 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002768 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002769 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002770 }
2771 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002772
liyuqianb73c24b2016-06-03 08:47:23 -07002773 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002774 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2775 [this](SkJSONWriter& writer) {
2776 writer.appendString(kSoftkeyHint);
2777 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2778 writer.appendString(softkey.c_str());
2779 }
2780 });
liyuqianb73c24b2016-06-03 08:47:23 -07002781
Florin Malitab632df72018-06-18 21:23:06 -04002782 writer.endArray();
2783 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002784
Florin Malitab632df72018-06-18 21:23:06 -04002785 auto data = memStream.detachAsData();
2786
2787 // TODO: would be cool to avoid this copy
2788 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2789
2790 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002791}
2792
2793void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002794 // For those who will add more features to handle the state change in this function:
2795 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2796 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2797 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002798 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002799 for (int i = 0; i < fSlides.count(); ++i) {
2800 if (fSlides[i]->getName().equals(stateValue)) {
2801 this->setCurrentSlide(i);
2802 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002803 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002804 }
Florin Malitaab99c342018-01-16 16:23:03 -05002805
2806 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002807 } else if (stateName.equals(kBackendStateName)) {
2808 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2809 if (stateValue.equals(kBackendTypeStrings[i])) {
2810 if (fBackendType != i) {
2811 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002812 for(auto& slide : fSlides) {
2813 slide->gpuTeardown();
2814 }
liyuqian6cb70252016-06-02 12:16:25 -07002815 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002816 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002817 }
2818 break;
2819 }
2820 }
csmartdalton578f0642017-02-24 16:04:47 -07002821 } else if (stateName.equals(kMSAAStateName)) {
2822 DisplayParams params = fWindow->getRequestedDisplayParams();
2823 int sampleCount = atoi(stateValue.c_str());
2824 if (sampleCount != params.fMSAASampleCount) {
2825 params.fMSAASampleCount = sampleCount;
2826 fWindow->setRequestedDisplayParams(params);
2827 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002828 this->updateTitle();
2829 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002830 }
2831 } else if (stateName.equals(kPathRendererStateName)) {
2832 DisplayParams params = fWindow->getRequestedDisplayParams();
2833 for (const auto& pair : gPathRendererNames) {
2834 if (pair.second == stateValue.c_str()) {
2835 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2836 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2837 fWindow->setRequestedDisplayParams(params);
2838 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002839 this->updateTitle();
2840 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002841 }
2842 break;
2843 }
csmartdalton578f0642017-02-24 16:04:47 -07002844 }
liyuqianb73c24b2016-06-03 08:47:23 -07002845 } else if (stateName.equals(kSoftkeyStateName)) {
2846 if (!stateValue.equals(kSoftkeyHint)) {
2847 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002848 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002849 }
liyuqian2edb0f42016-07-06 14:11:32 -07002850 } else if (stateName.equals(kRefreshStateName)) {
2851 // This state is actually NOT in the UI state.
2852 // We use this to allow Android to quickly set bool fRefresh.
2853 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002854 } else {
2855 SkDebugf("Unknown stateName: %s", stateName.c_str());
2856 }
2857}
Brian Osman79086b92017-02-10 13:36:16 -05002858
Hal Canaryb1f411a2019-08-29 10:39:22 -04002859bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002860 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002861}
2862
Hal Canaryb1f411a2019-08-29 10:39:22 -04002863bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002864 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002865 fWindow->inval();
2866 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002867 } else {
2868 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002869 }
Brian Osman79086b92017-02-10 13:36:16 -05002870}