blob: 0e6e2e5c64c64ccbe08511faf66717eb9a7a8cf6 [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"
22#include "src/core/SkScan.h"
John Stilesdf078002020-07-14 09:44:57 -040023#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040025#include "src/core/SkTextBlobPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040026#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrGpu.h"
28#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070029#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Chris Daltonff18ff62020-12-07 17:39:26 -070031#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Adlai Hollerbcfc5542020-08-27 12:44:07 -040032#include "src/image/SkImage_Base.h"
John Stiles2ee4d7a2021-03-30 10:30:47 -040033#include "src/sksl/SkSLCompiler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/utils/SkJSONWriter.h"
35#include "src/utils/SkOSPath.h"
36#include "tools/Resources.h"
37#include "tools/ToolUtils.h"
38#include "tools/flags/CommandLineFlags.h"
39#include "tools/flags/CommonFlags.h"
40#include "tools/trace/EventTracingPriv.h"
41#include "tools/viewer/BisectSlide.h"
42#include "tools/viewer/GMSlide.h"
43#include "tools/viewer/ImageSlide.h"
44#include "tools/viewer/ParticlesSlide.h"
45#include "tools/viewer/SKPSlide.h"
46#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050047#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "tools/viewer/SlideDir.h"
49#include "tools/viewer/SvgSlide.h"
50#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070051
Chris Dalton17dc4182020-03-25 16:18:16 -060052#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040053#include <map>
54
Hal Canary8a001442018-09-19 11:31:27 -040055#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040056#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040057
Brian Osmanc85f1fa2020-06-16 15:11:34 -040058#ifdef SK_VULKAN
59#include "spirv-tools/libspirv.hpp"
60#endif
61
Florin Malita87ccf332018-05-04 12:23:24 -040062#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050063 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040064#endif
Florin Malita45cd2002020-06-09 14:00:54 -040065#if defined(SK_ENABLE_SKRIVE)
66 #include "tools/viewer/SkRiveSlide.h"
67#endif
Florin Malita87ccf332018-05-04 12:23:24 -040068
Brian Osman5e7fbfd2019-05-03 13:13:35 -040069class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
70public:
71 void compileError(const char* shader, const char* errors) override {
72 fShaders.push_back(SkString(shader));
73 fErrors.push_back(SkString(errors));
74 }
75
76 void reset() {
77 fShaders.reset();
78 fErrors.reset();
79 }
80
81 SkTArray<SkString> fShaders;
82 SkTArray<SkString> fErrors;
83};
84
85static CapturingShaderErrorHandler gShaderErrorHandler;
86
Brian Osmanf847f312020-06-18 14:18:27 -040087GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
88
jvanverth34524262016-05-04 13:49:13 -070089using namespace sk_app;
John Stiles2ee4d7a2021-03-30 10:30:47 -040090using SkSL::Compiler;
91using OverrideFlag = SkSL::Compiler::OverrideFlag;
jvanverth34524262016-05-04 13:49:13 -070092
csmartdalton61cd31a2017-02-27 17:00:53 -070093static std::map<GpuPathRenderers, std::string> gPathRendererNames;
94
jvanverth9f372462016-04-06 06:08:59 -070095Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070096 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070097}
98
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060099static DEFINE_string(slide, "", "Start on this sample.");
100static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -0500101
Jim Van Verth682a2f42020-05-13 16:54:09 -0400102#ifdef SK_GL
103#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -0700104#else
Jim Van Verth682a2f42020-05-13 16:54:09 -0400105#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -0700106#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400107#ifdef SK_VULKAN
108#define VK_BACKEND_STR ", \"vk\""
109#else
110#define VK_BACKEND_STR
111#endif
112#ifdef SK_METAL
113#define MTL_BACKEND_STR ", \"mtl\""
114#else
115#define MTL_BACKEND_STR
116#endif
117#ifdef SK_DIRECT3D
118#define D3D_BACKEND_STR ", \"d3d\""
119#else
120#define D3D_BACKEND_STR
121#endif
122#ifdef SK_DAWN
123#define DAWN_BACKEND_STR ", \"dawn\""
124#else
125#define DAWN_BACKEND_STR
126#endif
127#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
128#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
129 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700130
Brian Osman2dd96932016-10-18 15:33:53 -0400131static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700132
Mike Klein5b3f3432019-03-21 11:42:21 -0500133static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
Chris Daltonfd708652021-03-17 21:10:17 -0600134static DEFINE_bool(dmsaa, false, "Use internal MSAA to render to non-MSAA surfaces?");
csmartdalton008b9d82017-02-22 12:00:42 -0700135
Mike Klein84836b72019-03-21 11:31:36 -0500136static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700137
Mike Klein84836b72019-03-21 11:31:36 -0500138static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400139
Mike Kleinc6142d82019-03-25 10:54:59 -0500140static DEFINE_string2(match, m, nullptr,
141 "[~][^]substring[$] [...] of name to run.\n"
142 "Multiple matches may be separated by spaces.\n"
143 "~ causes a matching name to always be skipped\n"
144 "^ requires the start of the name to match\n"
145 "$ requires the end of the name to match\n"
146 "^ and $ requires an exact match\n"
147 "If a name does not match any list entry,\n"
148 "it is skipped unless some list entry starts with ~");
149
Mike Klein19fb3972019-03-21 13:08:08 -0500150#if defined(SK_BUILD_FOR_ANDROID)
151 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500152 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
153 static DEFINE_string(lotties, "/data/local/tmp/lotties",
154 "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400155 static DEFINE_string(rives, "/data/local/tmp/rives",
156 "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500157#else
158 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500159 static DEFINE_string(skps, "skps", "Directory to read skps from.");
160 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400161 static DEFINE_string(rives, "rives", "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500162#endif
163
Mike Kleinc6142d82019-03-25 10:54:59 -0500164static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
165
166static DEFINE_int_2(threads, j, -1,
167 "Run threadsafe tests on a threadpool with this many extra threads, "
168 "defaulting to one extra thread per core.");
169
Jim Van Verth7b558182019-11-14 16:47:01 -0500170static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
171
Chris Daltonc8877332020-01-06 09:48:30 -0700172static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Klein813e8cc2020-08-05 09:33:38 -0500173static DEFINE_bool(skvm, false, "Force skvm blitters for raster.");
174static DEFINE_bool(jit, true, "JIT SkVM?");
Mike Klein1e0884d2020-04-28 15:04:16 -0500175static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500176static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Jim Van Verthecc91082020-11-20 15:30:25 -0500177static DEFINE_bool(binaryarchive, false, "Enable MTLBinaryArchive use (if available).");
Mike Kleinc6142d82019-03-25 10:54:59 -0500178
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400179#ifndef SK_GL
180static_assert(false, "viewer requires GL backend for raster.")
181#endif
182
Brian Salomon194db172017-08-17 14:37:06 -0400183const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700184 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400185#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
186 "ANGLE",
187#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400188#ifdef SK_DAWN
189 "Dawn",
190#endif
jvanverth063ece72016-06-17 09:29:14 -0700191#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700192 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700193#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400194#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500195 "Metal",
196#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400197#ifdef SK_DIRECT3D
198 "Direct3D",
199#endif
csmartdalton578f0642017-02-24 16:04:47 -0700200 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700201};
202
bsalomon6c471f72016-07-26 12:56:32 -0700203static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400204#ifdef SK_DAWN
205 if (0 == strcmp(str, "dawn")) {
206 return sk_app::Window::kDawn_BackendType;
207 } else
208#endif
bsalomon6c471f72016-07-26 12:56:32 -0700209#ifdef SK_VULKAN
210 if (0 == strcmp(str, "vk")) {
211 return sk_app::Window::kVulkan_BackendType;
212 } else
213#endif
Brian Salomon194db172017-08-17 14:37:06 -0400214#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
215 if (0 == strcmp(str, "angle")) {
216 return sk_app::Window::kANGLE_BackendType;
217 } else
218#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400219#ifdef SK_METAL
220 if (0 == strcmp(str, "mtl")) {
221 return sk_app::Window::kMetal_BackendType;
222 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500223#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400224#ifdef SK_DIRECT3D
225 if (0 == strcmp(str, "d3d")) {
226 return sk_app::Window::kDirect3D_BackendType;
227 } else
228#endif
229
bsalomon6c471f72016-07-26 12:56:32 -0700230 if (0 == strcmp(str, "gl")) {
231 return sk_app::Window::kNativeGL_BackendType;
232 } else if (0 == strcmp(str, "sw")) {
233 return sk_app::Window::kRaster_BackendType;
234 } else {
235 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
236 return sk_app::Window::kRaster_BackendType;
237 }
238}
239
Brian Osmana109e392017-02-24 09:49:14 -0500240static SkColorSpacePrimaries gSrgbPrimaries = {
241 0.64f, 0.33f,
242 0.30f, 0.60f,
243 0.15f, 0.06f,
244 0.3127f, 0.3290f };
245
246static SkColorSpacePrimaries gAdobePrimaries = {
247 0.64f, 0.33f,
248 0.21f, 0.71f,
249 0.15f, 0.06f,
250 0.3127f, 0.3290f };
251
252static SkColorSpacePrimaries gP3Primaries = {
253 0.680f, 0.320f,
254 0.265f, 0.690f,
255 0.150f, 0.060f,
256 0.3127f, 0.3290f };
257
258static SkColorSpacePrimaries gRec2020Primaries = {
259 0.708f, 0.292f,
260 0.170f, 0.797f,
261 0.131f, 0.046f,
262 0.3127f, 0.3290f };
263
264struct NamedPrimaries {
265 const char* fName;
266 SkColorSpacePrimaries* fPrimaries;
267} gNamedPrimaries[] = {
268 { "sRGB", &gSrgbPrimaries },
269 { "AdobeRGB", &gAdobePrimaries },
270 { "P3", &gP3Primaries },
271 { "Rec. 2020", &gRec2020Primaries },
272};
273
274static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
275 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
276}
277
Brian Osman70d2f432017-11-08 09:54:10 -0500278static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
279 // In raster mode, we still use GL for the window.
280 // This lets us render the GUI faster (and correct).
281 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
282}
283
Jim Van Verth74826c82019-03-01 14:37:30 -0500284class NullSlide : public Slide {
285 SkISize getDimensions() const override {
286 return SkISize::Make(640, 480);
287 }
288
289 void draw(SkCanvas* canvas) override {
290 canvas->clear(0xffff11ff);
291 }
292};
293
John Stiles31964fd2020-05-05 16:05:47 -0400294static const char kName[] = "name";
295static const char kValue[] = "value";
296static const char kOptions[] = "options";
297static const char kSlideStateName[] = "Slide";
298static const char kBackendStateName[] = "Backend";
299static const char kMSAAStateName[] = "MSAA";
300static const char kPathRendererStateName[] = "Path renderer";
301static const char kSoftkeyStateName[] = "Softkey";
302static const char kSoftkeyHint[] = "Please select a softkey";
303static const char kON[] = "ON";
304static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700305
Mike Reed862818b2020-03-21 15:07:13 -0400306extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -0500307extern bool gSkVMAllowJIT;
Mike Klein1e0884d2020-04-28 15:04:16 -0500308extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400309
jvanverth34524262016-05-04 13:49:13 -0700310Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500311 : fCurrentSlide(-1)
312 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500313 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400314 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500315 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500316 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500317 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500318 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400319 , fZoomWindowFixed(false)
320 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500321 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400322 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700323 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500324 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500325 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500326 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500327 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500328 , fApplyBackingScale(true)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700329 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400330 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400331 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400332 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500333 , fTiled(false)
334 , fDrawTileBoundaries(false)
335 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400336 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700337{
Greg Daniel285db442016-10-14 09:12:53 -0400338 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700339
Chris Dalton37ae4b02019-12-28 14:51:11 -0700340 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600341 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500342 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600343 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600344 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500345 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700346
jvanverth2bb3b6d2016-04-08 07:24:09 -0700347 SkDebugf("Command line arguments: ");
348 for (int i = 1; i < argc; ++i) {
349 SkDebugf("%s ", argv[i]);
350 }
351 SkDebugf("\n");
352
Mike Klein88544fb2019-03-20 10:50:33 -0500353 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500354#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400355 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500356#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700357
Mike Reed862818b2020-03-21 15:07:13 -0400358 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -0500359 gSkVMAllowJIT = FLAGS_jit;
Mike Klein1e0884d2020-04-28 15:04:16 -0500360 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400361
Mike Klein19cc0f62019-03-22 15:30:07 -0500362 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500363
Brian Osmanbc8150f2017-07-24 11:38:01 -0400364 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400365 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400366
bsalomon6c471f72016-07-26 12:56:32 -0700367 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700368 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700369
csmartdalton578f0642017-02-24 16:04:47 -0700370 DisplayParams displayParams;
371 displayParams.fMSAASampleCount = FLAGS_msaa;
Jim Van Verthecc91082020-11-20 15:30:25 -0500372 displayParams.fEnableBinaryArchive = FLAGS_binaryarchive;
Chris Dalton040238b2017-12-18 14:22:34 -0700373 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400374 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400375 displayParams.fGrContextOptions.fShaderCacheStrategy =
John Stiles2ee4d7a2021-03-30 10:30:47 -0400376 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400377 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
378 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltonfd708652021-03-17 21:10:17 -0600379 displayParams.fGrContextOptions.fAlwaysAntialias = FLAGS_dmsaa;
csmartdalton578f0642017-02-24 16:04:47 -0700380 fWindow->setRequestedDisplayParams(displayParams);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400381 fDisplay = fWindow->getRequestedDisplayParams();
Jim Van Verth7b558182019-11-14 16:47:01 -0500382 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700383
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500384 fImGuiLayer.setScaleFactor(fWindow->scaleFactor());
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500385 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500386
Brian Osman56a24812017-12-19 11:15:16 -0500387 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500388 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500389 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
390 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
391 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
392
jvanverth9f372462016-04-06 06:08:59 -0700393 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700394 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500395 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500396 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500397 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700398
brianosman622c8d52016-05-10 06:50:49 -0700399 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500400 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
401 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
402 fWindow->inval();
403 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500404 // Command to jump directly to the slide picker and give it focus
405 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
406 this->fShowImGuiDebugWindow = true;
407 this->fShowSlidePicker = true;
408 fWindow->inval();
409 });
410 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400411 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500412 this->fShowImGuiDebugWindow = true;
413 this->fShowSlidePicker = true;
414 fWindow->inval();
415 });
Brian Osman79086b92017-02-10 13:36:16 -0500416 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
417 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
418 fWindow->inval();
419 });
Brian Osmanf6877092017-02-13 09:39:57 -0500420 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
421 this->fShowZoomWindow = !this->fShowZoomWindow;
422 fWindow->inval();
423 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400424 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
425 this->fZoomWindowFixed = !this->fZoomWindowFixed;
426 fWindow->inval();
427 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400428 fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() {
Greg Danield0794cc2019-03-27 16:23:26 -0400429 DisplayParams params = fWindow->getRequestedDisplayParams();
430 params.fDisableVsync = !params.fDisableVsync;
431 fWindow->setRequestedDisplayParams(params);
432 this->updateTitle();
433 fWindow->inval();
434 });
Jim Van Verth7c647982020-10-23 12:47:57 -0400435 fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() {
436 DisplayParams params = fWindow->getRequestedDisplayParams();
437 params.fDelayDrawableAcquisition = !params.fDelayDrawableAcquisition;
438 fWindow->setRequestedDisplayParams(params);
439 this->updateTitle();
440 fWindow->inval();
441 });
Mike Reedf702ed42019-07-22 17:00:49 -0400442 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
443 fRefresh = !fRefresh;
444 fWindow->inval();
445 });
brianosman622c8d52016-05-10 06:50:49 -0700446 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500447 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700448 fWindow->inval();
449 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400450 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500451 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400452 this->updateTitle();
453 fWindow->inval();
454 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500455 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500456 switch (fColorMode) {
457 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500458 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500459 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500460 case ColorMode::kColorManaged8888:
461 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500462 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500463 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400464 this->setColorMode(ColorMode::kColorManagedF16Norm);
465 break;
466 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500467 this->setColorMode(ColorMode::kLegacy);
468 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500469 }
brianosman622c8d52016-05-10 06:50:49 -0700470 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700471 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
472 DisplayParams params = fWindow->getRequestedDisplayParams();
473 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
474 fWindow->setRequestedDisplayParams(params);
475 fWindow->inval();
476 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400477 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500478 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700479 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400480 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500481 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700482 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400483 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700484 this->changeZoomLevel(1.f / 32.f);
485 fWindow->inval();
486 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400487 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700488 this->changeZoomLevel(-1.f / 32.f);
489 fWindow->inval();
490 });
jvanverthaf236b52016-05-20 06:01:06 -0700491 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400492 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
493 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500494 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400495#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
496 if (newBackend == sk_app::Window::kVulkan_BackendType) {
497 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
498 sk_app::Window::kBackendTypeCount);
499 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
500 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500501 }
502#endif
Brian Osman621491e2017-02-28 15:45:01 -0500503 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700504 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500505 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
506 fSaveToSKP = true;
507 fWindow->inval();
508 });
Mike Reed376d8122019-03-14 11:39:02 -0400509 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
510 fShowSlideDimensions = !fShowSlideDimensions;
511 fWindow->inval();
512 });
Ben Wagner37c54032018-04-13 14:30:23 -0400513 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
514 DisplayParams params = fWindow->getRequestedDisplayParams();
515 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400516 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
517 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
518 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400519 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
520 } else {
521 switch (params.fSurfaceProps.pixelGeometry()) {
522 case kUnknown_SkPixelGeometry:
523 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
524 break;
525 case kRGB_H_SkPixelGeometry:
526 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
527 break;
528 case kBGR_H_SkPixelGeometry:
529 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
530 break;
531 case kRGB_V_SkPixelGeometry:
532 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
533 break;
534 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400535 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
536 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400537 break;
538 }
539 }
540 fWindow->setRequestedDisplayParams(params);
541 this->updateTitle();
542 fWindow->inval();
543 });
Ben Wagner9613e452019-01-23 10:34:59 -0500544 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500545 if (!fFontOverrides.fHinting) {
546 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400547 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500548 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500549 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400550 case SkFontHinting::kNone:
551 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500552 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400553 case SkFontHinting::kSlight:
554 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500555 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400556 case SkFontHinting::kNormal:
557 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500558 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400559 case SkFontHinting::kFull:
560 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500561 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500562 break;
563 }
564 }
565 this->updateTitle();
566 fWindow->inval();
567 });
568 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500569 if (!fPaintOverrides.fAntiAlias) {
570 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
571 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500572 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500573 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500574 } else {
575 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500576 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500577 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500578 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400579 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500580 break;
581 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500582 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500583 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400584 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500585 break;
586 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500587 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400588 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500589 break;
590 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500591 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
592 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500593 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
594 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500595 break;
596 }
597 }
598 this->updateTitle();
599 fWindow->inval();
600 });
Ben Wagner37c54032018-04-13 14:30:23 -0400601 fCommands.addCommand('D', "Modes", "DFT", [this]() {
602 DisplayParams params = fWindow->getRequestedDisplayParams();
603 uint32_t flags = params.fSurfaceProps.flags();
604 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
605 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
606 fWindow->setRequestedDisplayParams(params);
607 this->updateTitle();
608 fWindow->inval();
609 });
Ben Wagner9613e452019-01-23 10:34:59 -0500610 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500611 if (!fFontOverrides.fEdging) {
612 fFontOverrides.fEdging = true;
613 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500614 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500615 switch (fFont.getEdging()) {
616 case SkFont::Edging::kAlias:
617 fFont.setEdging(SkFont::Edging::kAntiAlias);
618 break;
619 case SkFont::Edging::kAntiAlias:
620 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
621 break;
622 case SkFont::Edging::kSubpixelAntiAlias:
623 fFont.setEdging(SkFont::Edging::kAlias);
624 fFontOverrides.fEdging = false;
625 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500626 }
627 }
628 this->updateTitle();
629 fWindow->inval();
630 });
Ben Wagner9613e452019-01-23 10:34:59 -0500631 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500632 if (!fFontOverrides.fSubpixel) {
633 fFontOverrides.fSubpixel = true;
634 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500635 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500636 if (!fFont.isSubpixel()) {
637 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500638 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500639 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500640 }
641 }
642 this->updateTitle();
643 fWindow->inval();
644 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400645 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
646 if (!fFontOverrides.fBaselineSnap) {
647 fFontOverrides.fBaselineSnap = true;
648 fFont.setBaselineSnap(false);
649 } else {
650 if (!fFont.isBaselineSnap()) {
651 fFont.setBaselineSnap(true);
652 } else {
653 fFontOverrides.fBaselineSnap = false;
654 }
655 }
656 this->updateTitle();
657 fWindow->inval();
658 });
Brian Osman805a7272018-05-02 15:40:20 -0400659 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
660 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
661 : kPerspective_Real;
662 this->updateTitle();
663 fWindow->inval();
664 });
665 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
666 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
667 : kPerspective_Off;
668 this->updateTitle();
669 fWindow->inval();
670 });
Brian Osman207d4102019-01-10 09:40:58 -0500671 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
672 fAnimTimer.togglePauseResume();
673 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400674 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
675 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500676 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400677 fWindow->inval();
678 });
Mike Reed59295352020-03-12 13:56:34 -0400679 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
680 fDrawViaSerialize = !fDrawViaSerialize;
681 this->updateTitle();
682 fWindow->inval();
683 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500684 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400685 gUseSkVMBlitter = !gUseSkVMBlitter;
686 this->updateTitle();
687 fWindow->inval();
688 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500689 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
690 gSkVMAllowJIT = !gSkVMAllowJIT;
691 this->updateTitle();
692 fWindow->inval();
693 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500694
jvanverth2bb3b6d2016-04-08 07:24:09 -0700695 // set up slides
696 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500697 if (FLAGS_list) {
698 this->listNames();
699 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700700
Brian Osman9bb47cf2018-04-26 15:55:00 -0400701 fPerspectivePoints[0].set(0, 0);
702 fPerspectivePoints[1].set(1, 0);
703 fPerspectivePoints[2].set(0, 1);
704 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700705 fAnimTimer.run();
706
Hal Canaryc465d132017-12-08 10:21:31 -0500707 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500708 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500709 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500710 }
711 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500712
jongdeok.kim804f17e2019-02-26 14:39:23 +0900713 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500714 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700715}
716
jvanverth34524262016-05-04 13:49:13 -0700717void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400718 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
719 static const struct {
720 const char* fExtension;
721 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500722 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400723 const SlideFactory fFactory;
724 } gExternalSlidesInfo[] = {
725 { ".skp", "skp-dir", FLAGS_skps,
726 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
727 return sk_make_sp<SKPSlide>(name, path);}
728 },
729 { ".jpg", "jpg-dir", FLAGS_jpgs,
730 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
731 return sk_make_sp<ImageSlide>(name, path);}
732 },
Florin Malita87ccf332018-05-04 12:23:24 -0400733#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400734 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400735 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
736 return sk_make_sp<SkottieSlide>(name, path);}
737 },
Florin Malita87ccf332018-05-04 12:23:24 -0400738#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400739 #if defined(SK_ENABLE_SKRIVE)
740 { ".flr", "skrive-dir", FLAGS_rives,
741 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
742 return sk_make_sp<SkRiveSlide>(name, path);}
743 },
744 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400745#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400746 { ".svg", "svg-dir", FLAGS_svgs,
747 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
748 return sk_make_sp<SvgSlide>(name, path);}
749 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400750#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400751 };
jvanverthc265a922016-04-08 12:51:45 -0700752
Brian Salomon343553a2018-09-05 15:41:23 -0400753 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700754
Mike Klein88544fb2019-03-20 10:50:33 -0500755 const auto addSlide =
756 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
757 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
758 return;
759 }
liyuqian6f163d22016-06-13 12:26:45 -0700760
Mike Klein88544fb2019-03-20 10:50:33 -0500761 if (auto slide = fact(name, path)) {
762 dirSlides.push_back(slide);
763 fSlides.push_back(std::move(slide));
764 }
765 };
Florin Malita76a076b2018-02-15 18:40:48 -0500766
Florin Malita38792ce2018-05-08 10:36:18 -0400767 if (!FLAGS_file.isEmpty()) {
768 // single file mode
769 const SkString file(FLAGS_file[0]);
770
771 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
772 for (const auto& sinfo : gExternalSlidesInfo) {
773 if (file.endsWith(sinfo.fExtension)) {
774 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
775 return;
776 }
777 }
778
779 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
780 } else {
781 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
782 }
783
784 return;
785 }
786
787 // Bisect slide.
788 if (!FLAGS_bisect.isEmpty()) {
789 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500790 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400791 if (FLAGS_bisect.count() >= 2) {
792 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
793 bisect->onChar(*ch);
794 }
795 }
796 fSlides.push_back(std::move(bisect));
797 }
798 }
799
800 // GMs
801 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400802 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400803 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500804 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400805 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400806 fSlides.push_back(std::move(slide));
807 }
Florin Malita38792ce2018-05-08 10:36:18 -0400808 }
809 // reverse gms
810 int numGMs = fSlides.count() - firstGM;
811 for (int i = 0; i < numGMs/2; ++i) {
812 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
813 }
814
815 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400816 for (const SampleFactory factory : SampleRegistry::Range()) {
817 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500818 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400819 fSlides.push_back(slide);
820 }
Florin Malita38792ce2018-05-08 10:36:18 -0400821 }
822
Brian Osman7c979f52019-02-12 13:27:51 -0500823 // Particle demo
824 {
825 // TODO: Convert this to a sample
826 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500827 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500828 fSlides.push_back(std::move(slide));
829 }
830 }
831
Brian Osmand927bd22019-12-18 11:23:12 -0500832 // Runtime shader editor
833 {
834 sk_sp<Slide> slide(new SkSLSlide());
835 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
836 fSlides.push_back(std::move(slide));
837 }
838 }
839
Florin Malita0ffa3222018-04-05 14:34:45 -0400840 for (const auto& info : gExternalSlidesInfo) {
841 for (const auto& flag : info.fFlags) {
842 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
843 // single file
844 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
845 } else {
846 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400847 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400848 SkTArray<SkString> sortedFilenames;
849 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400850 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400851 sortedFilenames.push_back(name);
852 }
853 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400854 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000855 [](const SkString& a, const SkString& b) {
856 return strcmp(a.c_str(), b.c_str()) < 0;
857 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400858 }
859 for (const SkString& filename : sortedFilenames) {
860 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
861 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400862 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400863 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400864 if (!dirSlides.empty()) {
865 fSlides.push_back(
866 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
867 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500868 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400869 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400870 }
871 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500872
873 if (!fSlides.count()) {
874 sk_sp<Slide> slide(new NullSlide());
875 fSlides.push_back(std::move(slide));
876 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700877}
878
879
jvanverth34524262016-05-04 13:49:13 -0700880Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400881 for(auto& slide : fSlides) {
882 slide->gpuTeardown();
883 }
884
jvanverth9f372462016-04-06 06:08:59 -0700885 fWindow->detach();
886 delete fWindow;
887}
888
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500889struct SkPaintTitleUpdater {
890 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
891 void append(const char* s) {
892 if (fCount == 0) {
893 fTitle->append(" {");
894 } else {
895 fTitle->append(", ");
896 }
897 fTitle->append(s);
898 ++fCount;
899 }
900 void done() {
901 if (fCount > 0) {
902 fTitle->append("}");
903 }
904 }
905 SkString* fTitle;
906 int fCount;
907};
908
brianosman05de2162016-05-06 13:28:57 -0700909void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700910 if (!fWindow) {
911 return;
912 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500913 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700914 return; // Surface hasn't been created yet.
915 }
916
jvanverth34524262016-05-04 13:49:13 -0700917 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700918 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700919
Mike Kleine5acd752019-03-22 09:57:16 -0500920 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400921 if (gSkForceAnalyticAA) {
922 title.append(" <FAAA>");
923 } else {
924 title.append(" <AAA>");
925 }
926 }
Mike Reed59295352020-03-12 13:56:34 -0400927 if (fDrawViaSerialize) {
928 title.append(" <serialize>");
929 }
Mike Reed862818b2020-03-21 15:07:13 -0400930 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500931 title.append(" <SkVMBlitter>");
932 }
933 if (!gSkVMAllowJIT) {
934 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400935 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400936
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500937 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500938 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
939 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400940 const char* on, const char* off)
941 {
Ben Wagner9613e452019-01-23 10:34:59 -0500942 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400943 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500944 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400945 };
946
Ben Wagner9613e452019-01-23 10:34:59 -0500947 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
948 const char* on, const char* off)
949 {
950 if (fFontOverrides.*flag) {
951 paintTitle.append((fFont.*isFlag)() ? on : off);
952 }
953 };
954
955 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
956 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
957
958 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
959 "Force Autohint", "No Force Autohint");
960 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400961 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500962 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
963 "Linear Metrics", "Non-Linear Metrics");
964 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
965 "Bitmap Text", "No Bitmap Text");
966 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
967
968 if (fFontOverrides.fEdging) {
969 switch (fFont.getEdging()) {
970 case SkFont::Edging::kAlias:
971 paintTitle.append("Alias Text");
972 break;
973 case SkFont::Edging::kAntiAlias:
974 paintTitle.append("Antialias Text");
975 break;
976 case SkFont::Edging::kSubpixelAntiAlias:
977 paintTitle.append("Subpixel Antialias Text");
978 break;
979 }
980 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400981
Mike Reed3ae47332019-01-04 10:11:46 -0500982 if (fFontOverrides.fHinting) {
983 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400984 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500985 paintTitle.append("No Hinting");
986 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400987 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500988 paintTitle.append("Slight Hinting");
989 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400990 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500991 paintTitle.append("Normal Hinting");
992 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400993 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500994 paintTitle.append("Full Hinting");
995 break;
996 }
997 }
998 paintTitle.done();
999
Brian Osman92004802017-03-06 11:47:26 -05001000 switch (fColorMode) {
1001 case ColorMode::kLegacy:
1002 title.append(" Legacy 8888");
1003 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001004 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001005 title.append(" ColorManaged 8888");
1006 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001007 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001008 title.append(" ColorManaged F16");
1009 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001010 case ColorMode::kColorManagedF16Norm:
1011 title.append(" ColorManaged F16 Norm");
1012 break;
Brian Osman92004802017-03-06 11:47:26 -05001013 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001014
Brian Osman92004802017-03-06 11:47:26 -05001015 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001016 int curPrimaries = -1;
1017 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1018 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1019 curPrimaries = i;
1020 break;
1021 }
1022 }
Brian Osman03115dc2018-11-26 13:55:19 -05001023 title.appendf(" %s Gamma %f",
1024 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001025 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001026 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001027
Ben Wagner37c54032018-04-13 14:30:23 -04001028 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001029 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001030 switch (params.fSurfaceProps.pixelGeometry()) {
1031 case kUnknown_SkPixelGeometry:
1032 title.append( " Flat");
1033 break;
1034 case kRGB_H_SkPixelGeometry:
1035 title.append( " RGB");
1036 break;
1037 case kBGR_H_SkPixelGeometry:
1038 title.append( " BGR");
1039 break;
1040 case kRGB_V_SkPixelGeometry:
1041 title.append( " RGBV");
1042 break;
1043 case kBGR_V_SkPixelGeometry:
1044 title.append( " BGRV");
1045 break;
1046 }
1047 }
1048
1049 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1050 title.append(" DFT");
1051 }
1052
csmartdalton578f0642017-02-24 16:04:47 -07001053 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001054 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001055 int msaa = fWindow->sampleCount();
1056 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001057 title.appendf(" MSAA: %i", msaa);
1058 }
1059 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001060
1061 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001062 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001063 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1064 }
1065
Brian Osman805a7272018-05-02 15:40:20 -04001066 if (kPerspective_Real == fPerspectiveMode) {
1067 title.append(" Perpsective (Real)");
1068 } else if (kPerspective_Fake == fPerspectiveMode) {
1069 title.append(" Perspective (Fake)");
1070 }
1071
brianosman05de2162016-05-06 13:28:57 -07001072 fWindow->setTitle(title.c_str());
1073}
1074
Florin Malitaab99c342018-01-16 16:23:03 -05001075int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001076
1077 if (!FLAGS_slide.isEmpty()) {
1078 int count = fSlides.count();
1079 for (int i = 0; i < count; i++) {
1080 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001081 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001082 }
1083 }
1084
1085 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1086 this->listNames();
1087 }
1088
Florin Malitaab99c342018-01-16 16:23:03 -05001089 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001090}
1091
Florin Malitaab99c342018-01-16 16:23:03 -05001092void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001093 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001094 for (const auto& slide : fSlides) {
1095 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001096 }
1097}
1098
Florin Malitaab99c342018-01-16 16:23:03 -05001099void Viewer::setCurrentSlide(int slide) {
1100 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001101
Florin Malitaab99c342018-01-16 16:23:03 -05001102 if (slide == fCurrentSlide) {
1103 return;
1104 }
1105
1106 if (fCurrentSlide >= 0) {
1107 fSlides[fCurrentSlide]->unload();
1108 }
1109
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001110 SkScalar scaleFactor = 1.0;
1111 if (fApplyBackingScale) {
1112 scaleFactor = fWindow->scaleFactor();
1113 }
1114 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1115 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001116 fCurrentSlide = slide;
1117 this->setupCurrentSlide();
1118}
1119
1120void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001121 if (fCurrentSlide >= 0) {
1122 // prepare dimensions for image slides
1123 fGesture.resetTouchState();
1124 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001125
Jim Van Verth0848fb02018-01-22 13:39:30 -05001126 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1127 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1128 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001129
Jim Van Verth0848fb02018-01-22 13:39:30 -05001130 // Start with a matrix that scales the slide to the available screen space
1131 if (fWindow->scaleContentToFit()) {
1132 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001133 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1134 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001135 }
liyuqiane46e4f02016-05-20 07:32:19 -07001136 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001137
1138 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001139 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001140
1141 this->updateTitle();
1142 this->updateUIState();
1143
1144 fStatsLayer.resetMeasurements();
1145
1146 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001147 }
jvanverthc265a922016-04-08 12:51:45 -07001148}
1149
Brian Osmanaba642c2020-02-06 12:52:25 -05001150#define MAX_ZOOM_LEVEL 8.0f
1151#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001152
jvanverth34524262016-05-04 13:49:13 -07001153void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001154 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001155 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001156 this->preTouchMatrixChanged();
1157}
Yuqian Li755778c2018-03-28 16:23:31 -04001158
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001159void Viewer::preTouchMatrixChanged() {
1160 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001161 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1162 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1163 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1164 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1165}
1166
Brian Osman805a7272018-05-02 15:40:20 -04001167SkMatrix Viewer::computePerspectiveMatrix() {
1168 SkScalar w = fWindow->width(), h = fWindow->height();
1169 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1170 SkPoint perspPts[4] = {
1171 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1172 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1173 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1174 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1175 };
1176 SkMatrix m;
1177 m.setPolyToPoly(orthoPts, perspPts, 4);
1178 return m;
1179}
1180
Yuqian Li755778c2018-03-28 16:23:31 -04001181SkMatrix Viewer::computePreTouchMatrix() {
1182 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001183
1184 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001185 if (fApplyBackingScale) {
1186 zoomScale *= fWindow->scaleFactor();
1187 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001188 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001189 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001190
1191 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1192 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001193
Brian Osman805a7272018-05-02 15:40:20 -04001194 if (kPerspective_Real == fPerspectiveMode) {
1195 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001196 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001197 }
1198
Yuqian Li755778c2018-03-28 16:23:31 -04001199 return m;
jvanverthc265a922016-04-08 12:51:45 -07001200}
1201
liyuqiand3cdbca2016-05-17 12:44:20 -07001202SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001203 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001204 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001205 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001206 return m;
jvanverthc265a922016-04-08 12:51:45 -07001207}
1208
Brian Osman621491e2017-02-28 15:45:01 -05001209void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001210 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001211 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001212 fBackendType = backendType;
1213
Robert Phillipse9229532020-06-26 10:10:49 -04001214 // The active context is going away in 'detach'
1215 for(auto& slide : fSlides) {
1216 slide->gpuTeardown();
1217 }
1218
Brian Osman621491e2017-02-28 15:45:01 -05001219 fWindow->detach();
1220
Brian Osman70d2f432017-11-08 09:54:10 -05001221#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001222 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1223 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001224 DisplayParams params = fWindow->getRequestedDisplayParams();
1225 delete fWindow;
1226 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001227
Brian Osman70d2f432017-11-08 09:54:10 -05001228 // re-register callbacks
1229 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001230 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001231 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001232 fWindow->pushLayer(&fImGuiLayer);
1233
Brian Osman70d2f432017-11-08 09:54:10 -05001234 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1235 // will still include our correct sample count. But the re-created fWindow will lose that
1236 // information. On Windows, we need to re-create the window when changing sample count,
1237 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1238 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1239 // as if we had never changed windows at all).
1240 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001241#endif
1242
Brian Osman70d2f432017-11-08 09:54:10 -05001243 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001244}
1245
Brian Osman92004802017-03-06 11:47:26 -05001246void Viewer::setColorMode(ColorMode colorMode) {
1247 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001248 this->updateTitle();
1249 fWindow->inval();
1250}
1251
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001252class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1253public:
Mike Reed3ae47332019-01-04 10:11:46 -05001254 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1255 SkFont* font, Viewer::SkFontFields* ffields)
1256 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001257 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001258 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1259 sk_sp<SkTextBlob>* cache) {
1260 bool blobWillChange = false;
1261 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001262 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1263 bool shouldDraw = this->filterFont(&filteredFont);
1264 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001265 blobWillChange = true;
1266 break;
1267 }
1268 }
1269 if (!blobWillChange) {
1270 return blob;
1271 }
1272
1273 SkTextBlobBuilder builder;
1274 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001275 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1276 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001277 if (!shouldDraw) {
1278 continue;
1279 }
1280
Mike Reed3ae47332019-01-04 10:11:46 -05001281 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001282
Ben Wagner41e40472018-09-24 13:01:54 -04001283 const SkTextBlobBuilder::RunBuffer& runBuffer
1284 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001285 ? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(),
1286 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001287 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001288 ? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(),
1289 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001290 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001291 ? builder.allocRunTextPos(font, it.glyphCount(), it.textSize())
Ben Wagnere5736262021-02-08 16:52:08 -05001292 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001293 ? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001294 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1295 uint32_t glyphCount = it.glyphCount();
1296 if (it.glyphs()) {
1297 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1298 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1299 }
1300 if (it.pos()) {
1301 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001302 unsigned posPerGlyph = it.scalarsPerGlyph();
1303 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001304 }
1305 if (it.text()) {
1306 size_t textSize = sizeof(decltype(*it.text()));
1307 uint32_t textCount = it.textSize();
1308 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1309 }
1310 if (it.clusters()) {
1311 size_t clusterSize = sizeof(decltype(*it.clusters()));
1312 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1313 }
1314 }
1315 *cache = builder.make();
1316 return cache->get();
1317 }
1318 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1319 const SkPaint& paint) override {
1320 sk_sp<SkTextBlob> cache;
1321 this->SkPaintFilterCanvas::onDrawTextBlob(
1322 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1323 }
Mike Reed3ae47332019-01-04 10:11:46 -05001324 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001325 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001326 font->writable()->setSize(fFont->getSize());
1327 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001328 if (fFontOverrides->fScaleX) {
1329 font->writable()->setScaleX(fFont->getScaleX());
1330 }
1331 if (fFontOverrides->fSkewX) {
1332 font->writable()->setSkewX(fFont->getSkewX());
1333 }
Mike Reed3ae47332019-01-04 10:11:46 -05001334 if (fFontOverrides->fHinting) {
1335 font->writable()->setHinting(fFont->getHinting());
1336 }
Ben Wagner9613e452019-01-23 10:34:59 -05001337 if (fFontOverrides->fEdging) {
1338 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001339 }
Ben Wagner9613e452019-01-23 10:34:59 -05001340 if (fFontOverrides->fEmbolden) {
1341 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001342 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001343 if (fFontOverrides->fBaselineSnap) {
1344 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1345 }
Ben Wagner9613e452019-01-23 10:34:59 -05001346 if (fFontOverrides->fLinearMetrics) {
1347 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001348 }
Ben Wagner9613e452019-01-23 10:34:59 -05001349 if (fFontOverrides->fSubpixel) {
1350 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001351 }
Ben Wagner9613e452019-01-23 10:34:59 -05001352 if (fFontOverrides->fEmbeddedBitmaps) {
1353 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001354 }
Ben Wagner9613e452019-01-23 10:34:59 -05001355 if (fFontOverrides->fForceAutoHinting) {
1356 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001357 }
Ben Wagner9613e452019-01-23 10:34:59 -05001358
Mike Reed3ae47332019-01-04 10:11:46 -05001359 return true;
1360 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001361 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001362 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001363 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001364 }
Ben Wagner9613e452019-01-23 10:34:59 -05001365 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001366 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001367 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001368 if (fPaintOverrides->fStyle) {
1369 paint.setStyle(fPaint->getStyle());
1370 }
1371 if (fPaintOverrides->fWidth) {
1372 paint.setStrokeWidth(fPaint->getStrokeWidth());
1373 }
1374 if (fPaintOverrides->fMiterLimit) {
1375 paint.setStrokeMiter(fPaint->getStrokeMiter());
1376 }
1377 if (fPaintOverrides->fCapType) {
1378 paint.setStrokeCap(fPaint->getStrokeCap());
1379 }
1380 if (fPaintOverrides->fJoinType) {
1381 paint.setStrokeJoin(fPaint->getStrokeJoin());
1382 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001383 return true;
1384 }
1385 SkPaint* fPaint;
1386 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001387 SkFont* fFont;
1388 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001389};
1390
Robert Phillips9882dae2019-03-04 11:00:10 -05001391void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001392 if (fCurrentSlide < 0) {
1393 return;
1394 }
1395
Robert Phillips9882dae2019-03-04 11:00:10 -05001396 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001397
Brian Osmanf750fbc2017-02-08 10:47:28 -05001398 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001399 SkSurface* slideSurface = surface;
1400 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001401 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001402
Brian Osmane0d4fba2017-03-15 10:24:55 -04001403 // 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 -05001404 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001405 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001406 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001407 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001408 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001409 }
1410
Brian Osman3ac99cf2017-12-01 11:23:53 -05001411 if (fSaveToSKP) {
1412 SkPictureRecorder recorder;
1413 SkCanvas* recorderCanvas = recorder.beginRecording(
1414 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001415 fSlides[fCurrentSlide]->draw(recorderCanvas);
1416 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1417 SkFILEWStream stream("sample_app.skp");
1418 picture->serialize(&stream);
1419 fSaveToSKP = false;
1420 }
1421
Brian Osmane9ed0f02018-11-26 14:50:05 -05001422 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001423 SkColorType colorType;
1424 switch (fColorMode) {
1425 case ColorMode::kLegacy:
1426 case ColorMode::kColorManaged8888:
1427 colorType = kN32_SkColorType;
1428 break;
1429 case ColorMode::kColorManagedF16:
1430 colorType = kRGBA_F16_SkColorType;
1431 break;
1432 case ColorMode::kColorManagedF16Norm:
1433 colorType = kRGBA_F16Norm_SkColorType;
1434 break;
1435 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001436
1437 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001438 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001439 slideCanvas->getProps(&props);
1440
Brian Osmane9ed0f02018-11-26 14:50:05 -05001441 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1442 return Window::kRaster_BackendType == this->fBackendType
1443 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001444 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001445 };
1446
Brian Osman03115dc2018-11-26 13:55:19 -05001447 // We need to render offscreen if we're...
1448 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1449 // ... in any raster mode, because the window surface is actually GL
1450 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001451 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001452 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001453 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001454 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001455 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001456 colorSpace != nullptr ||
1457 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001458
Brian Osmane9ed0f02018-11-26 14:50:05 -05001459 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001460 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001461 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001462 }
1463
Mike Reed59295352020-03-12 13:56:34 -04001464 SkPictureRecorder recorder;
1465 SkCanvas* recorderRestoreCanvas = nullptr;
1466 if (fDrawViaSerialize) {
1467 recorderRestoreCanvas = slideCanvas;
1468 slideCanvas = recorder.beginRecording(
1469 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1470 }
1471
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001472 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001473 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001474 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001475 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001476 if (fTiled) {
1477 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1478 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001479 for (int y = 0; y < fWindow->height(); y += tileH) {
1480 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001481 SkAutoCanvasRestore acr(slideCanvas, true);
1482 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1483 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001484 }
1485 }
1486
1487 // Draw borders between tiles
1488 if (fDrawTileBoundaries) {
1489 SkPaint border;
1490 border.setColor(0x60FF00FF);
1491 border.setStyle(SkPaint::kStroke_Style);
1492 for (int y = 0; y < fWindow->height(); y += tileH) {
1493 for (int x = 0; x < fWindow->width(); x += tileW) {
1494 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1495 }
1496 }
1497 }
1498 } else {
1499 slideCanvas->concat(this->computeMatrix());
1500 if (kPerspective_Real == fPerspectiveMode) {
1501 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1502 }
Mike Reed3ae47332019-01-04 10:11:46 -05001503 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001504 fSlides[fCurrentSlide]->draw(&filterCanvas);
1505 }
Brian Osman56a24812017-12-19 11:15:16 -05001506 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001507 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001508
Mike Reed59295352020-03-12 13:56:34 -04001509 if (recorderRestoreCanvas) {
1510 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1511 auto data = picture->serialize();
1512 slideCanvas = recorderRestoreCanvas;
1513 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1514 }
1515
Brian Osman1df161a2017-02-09 12:10:20 -05001516 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001517 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001518 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001519 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001520
1521 // If we rendered offscreen, snap an image and push the results to the window's canvas
1522 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001523 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001524
Robert Phillips9882dae2019-03-04 11:00:10 -05001525 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001526 SkPaint paint;
1527 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001528 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001529 int prePerspectiveCount = canvas->save();
1530 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001531 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001532 canvas->clear(SK_ColorWHITE);
1533 canvas->concat(this->computePerspectiveMatrix());
1534 }
Mike Reedb339d052021-01-28 11:20:41 -05001535 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001536 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001537 }
Mike Reed376d8122019-03-14 11:39:02 -04001538
1539 if (fShowSlideDimensions) {
1540 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1541 SkPaint paint;
1542 paint.setColor(0x40FFFF00);
1543 surface->getCanvas()->drawRect(r, paint);
1544 }
liyuqian6f163d22016-06-13 12:26:45 -07001545}
1546
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001547void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001548 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001549 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001550}
Jim Van Verth6f449692017-02-14 15:16:46 -05001551
Robert Phillips9882dae2019-03-04 11:00:10 -05001552void Viewer::onPaint(SkSurface* surface) {
1553 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001554
Robert Phillips9882dae2019-03-04 11:00:10 -05001555 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001556
Brian Osmand67e5182017-12-08 16:46:09 -05001557 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001558
Greg Daniel427d8eb2020-09-28 15:04:18 -04001559 fLastImage.reset();
1560
Robert Phillipsed653392020-07-10 13:55:21 -04001561 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001562 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001563 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001564 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001565}
1566
Ben Wagnera1915972018-08-09 15:06:19 -04001567void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001568 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001569 SkScalar scaleFactor = 1.0;
1570 if (fApplyBackingScale) {
1571 scaleFactor = fWindow->scaleFactor();
1572 }
1573 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001574 }
Ben Wagnera1915972018-08-09 15:06:19 -04001575}
1576
Florin Malitacefc1b92018-02-19 21:43:47 -05001577SkPoint Viewer::mapEvent(float x, float y) {
1578 const auto m = this->computeMatrix();
1579 SkMatrix inv;
1580
1581 SkAssertResult(m.invert(&inv));
1582
1583 return inv.mapXY(x, y);
1584}
1585
Hal Canaryb1f411a2019-08-29 10:39:22 -04001586bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001587 if (GestureDevice::kMouse == fGestureDevice) {
1588 return false;
1589 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001590
1591 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001592 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001593 fWindow->inval();
1594 return true;
1595 }
1596
liyuqiand3cdbca2016-05-17 12:44:20 -07001597 void* castedOwner = reinterpret_cast<void*>(owner);
1598 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001599 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001600 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001601#if defined(SK_BUILD_FOR_IOS)
1602 // TODO: move IOS swipe detection higher up into the platform code
1603 SkPoint dir;
1604 if (fGesture.isFling(&dir)) {
1605 // swiping left or right
1606 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1607 if (dir.fX < 0) {
1608 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1609 fCurrentSlide + 1 : 0);
1610 } else {
1611 this->setCurrentSlide(fCurrentSlide > 0 ?
1612 fCurrentSlide - 1 : fSlides.count() - 1);
1613 }
1614 }
1615 fGesture.reset();
1616 }
1617#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001618 break;
1619 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001620 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001621 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001622 break;
1623 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001624 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001625 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001626 break;
1627 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001628 default: {
1629 // kLeft and kRight are only for swipes
1630 SkASSERT(false);
1631 break;
1632 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001633 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001634 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001635 fWindow->inval();
1636 return true;
1637}
1638
Hal Canaryb1f411a2019-08-29 10:39:22 -04001639bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001640 if (GestureDevice::kTouch == fGestureDevice) {
1641 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001642 }
Brian Osman16c81a12017-12-20 11:58:34 -05001643
Florin Malitacefc1b92018-02-19 21:43:47 -05001644 const auto slidePt = this->mapEvent(x, y);
1645 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1646 fWindow->inval();
1647 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001648 }
1649
1650 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001651 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001652 fGesture.touchEnd(nullptr);
1653 break;
1654 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001655 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001656 fGesture.touchBegin(nullptr, x, y);
1657 break;
1658 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001659 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001660 fGesture.touchMoved(nullptr, x, y);
1661 break;
1662 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001663 default: {
1664 SkASSERT(false); // shouldn't see kRight or kLeft here
1665 break;
1666 }
Brian Osman16c81a12017-12-20 11:58:34 -05001667 }
1668 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1669
Hal Canaryb1f411a2019-08-29 10:39:22 -04001670 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001671 fWindow->inval();
1672 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001673 return true;
1674}
1675
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001676bool Viewer::onFling(skui::InputState state) {
1677 if (skui::InputState::kRight == state) {
1678 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1679 return true;
1680 } else if (skui::InputState::kLeft == state) {
1681 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1682 return true;
1683 }
1684 return false;
1685}
1686
1687bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1688 switch (state) {
1689 case skui::InputState::kDown:
1690 fGesture.startZoom();
1691 return true;
1692 break;
1693 case skui::InputState::kMove:
1694 fGesture.updateZoom(scale, x, y, x, y);
1695 return true;
1696 break;
1697 case skui::InputState::kUp:
1698 fGesture.endZoom();
1699 return true;
1700 break;
1701 default:
1702 SkASSERT(false);
1703 break;
1704 }
1705
1706 return false;
1707}
1708
Brian Osmana109e392017-02-24 09:49:14 -05001709static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001710 // The gamut image covers a (0.8 x 0.9) shaped region
1711 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001712
1713 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1714 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1715 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001716 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1717 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1718 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001719
Brian Osman535c5e32019-02-09 16:32:58 -05001720 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1721 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1722 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1723 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1724 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001725}
1726
Ben Wagner3627d2e2018-06-26 14:23:20 -04001727static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001728 ImGui::DragCanvas dc(pt);
1729 dc.fillColor(IM_COL32(0, 0, 0, 128));
1730 dc.dragPoint(pt);
1731 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001732}
1733
Brian Osman9bb47cf2018-04-26 15:55:00 -04001734static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001735 ImGui::DragCanvas dc(pts);
1736 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001737
Brian Osman535c5e32019-02-09 16:32:58 -05001738 for (int i = 0; i < 4; ++i) {
1739 dc.dragPoint(pts + i);
1740 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001741
Brian Osman535c5e32019-02-09 16:32:58 -05001742 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1743 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1744 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1745 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001746
Brian Osman535c5e32019-02-09 16:32:58 -05001747 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001748}
1749
John Stiles38b7d2f2020-06-24 12:13:31 -04001750static SkSL::String build_sksl_highlight_shader() {
1751 return SkSL::String("out half4 sk_FragColor;\n"
1752 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1753}
1754
1755static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1756 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1757 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1758 size_t pos = inShader.rfind("return *_out;\n");
1759 if (pos == std::string::npos) {
1760 return inShader;
1761 }
1762
1763 SkSL::String replacementShader = inShader;
1764 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1765 return replacementShader;
1766}
1767
1768static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1769 const char* versionDecl = shaderCaps.versionDeclString();
1770 SkSL::String highlight = versionDecl ? versionDecl : "";
1771 if (shaderCaps.usesPrecisionModifiers()) {
1772 highlight.append("precision mediump float;\n");
1773 }
1774 highlight.appendf("out vec4 sk_FragColor;\n"
1775 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1776 return highlight;
1777}
1778
Brian Osmand67e5182017-12-08 16:46:09 -05001779void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001780 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1781 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001782 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001783 }
1784
1785 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001786 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1787 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001788 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001789 DisplayParams params = fWindow->getRequestedDisplayParams();
1790 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001791 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001792
Brian Osmana109e392017-02-24 09:49:14 -05001793 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1794 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001795 if (ImGui::CollapsingHeader("Backend")) {
1796 int newBackend = static_cast<int>(fBackendType);
1797 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1798 ImGui::SameLine();
1799 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001800#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1801 ImGui::SameLine();
1802 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1803#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001804#if defined(SK_DAWN)
1805 ImGui::SameLine();
1806 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1807#endif
John Stilesf7da9232020-11-19 19:58:14 -05001808#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001809 ImGui::SameLine();
1810 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1811#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001812#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001813 ImGui::SameLine();
1814 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1815#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001816#if defined(SK_DIRECT3D)
1817 ImGui::SameLine();
1818 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1819#endif
Brian Osman621491e2017-02-28 15:45:01 -05001820 if (newBackend != fBackendType) {
1821 fDeferredActions.push_back([=]() {
1822 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1823 });
1824 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001825
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001826 bool* wire = &params.fGrContextOptions.fWireframeMode;
1827 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1828 paramsChanged = true;
1829 }
Brian Salomon99a33902017-03-07 15:16:34 -05001830
Brian Osman28b12522017-03-08 17:10:24 -05001831 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001832 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001833 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001834 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1835 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1836 1;
1837
1838 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1839 if (maxMSAA >= 4) {
1840 ImGui::Text("MSAA: ");
1841
1842 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1843 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1844 // don't show it in the list.
1845 if (curMSAA == 2) {
1846 continue;
1847 }
1848 ImGui::SameLine();
1849 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1850 &sampleCount, curMSAA);
1851 }
1852 }
Brian Osman28b12522017-03-08 17:10:24 -05001853
1854 if (sampleCount != params.fMSAASampleCount) {
1855 params.fMSAASampleCount = sampleCount;
1856 paramsChanged = true;
1857 }
1858 }
1859
Ben Wagner37c54032018-04-13 14:30:23 -04001860 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001861 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001862 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1863 }
1864 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1865 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1866 {
1867 uint32_t flags = params.fSurfaceProps.flags();
1868 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001869 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1870 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1871 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001872 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001873 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001874 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1875 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1876 }
1877 paramsChanged = true;
1878 }
1879
1880 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1881 if (ImGui::Checkbox("DFT", &useDFT)) {
1882 uint32_t flags = params.fSurfaceProps.flags();
1883 if (useDFT) {
1884 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1885 } else {
1886 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1887 }
1888 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1889 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1890 paramsChanged = true;
1891 }
1892
Brian Osman8a9de3d2017-03-01 14:59:05 -05001893 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001894 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001895 auto prButton = [&](GpuPathRenderers x) {
1896 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001897 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1898 params.fGrContextOptions.fGpuPathRenderers = x;
1899 paramsChanged = true;
1900 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001901 }
1902 };
1903
1904 if (!ctx) {
1905 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001906 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001907 const auto* caps = ctx->priv().caps();
1908 prButton(GpuPathRenderers::kDefault);
1909 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001910 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001911 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001912 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001913 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001914 if (1 == fWindow->sampleCount()) {
1915 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1916 prButton(GpuPathRenderers::kCoverageCounting);
1917 }
1918 prButton(GpuPathRenderers::kSmall);
1919 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001920 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001921 prButton(GpuPathRenderers::kNone);
1922 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001923 ImGui::TreePop();
1924 }
Brian Osman621491e2017-02-28 15:45:01 -05001925 }
1926
Ben Wagner964571d2019-03-08 12:35:06 -05001927 if (ImGui::CollapsingHeader("Tiling")) {
1928 ImGui::Checkbox("Enable", &fTiled);
1929 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1930 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1931 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1932 }
1933
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001934 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001935 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1936 this->preTouchMatrixChanged();
1937 this->onResize(fWindow->width(), fWindow->height());
1938 paramsChanged = true;
1939 }
1940
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001941 float zoom = fZoomLevel;
1942 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1943 fZoomLevel = zoom;
1944 this->preTouchMatrixChanged();
1945 paramsChanged = true;
1946 }
1947 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001948 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001949 fRotation = deg;
1950 this->preTouchMatrixChanged();
1951 paramsChanged = true;
1952 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001953 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1954 if (ImGui_DragLocation(&fOffset)) {
1955 this->preTouchMatrixChanged();
1956 paramsChanged = true;
1957 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001958 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1959 this->preTouchMatrixChanged();
1960 paramsChanged = true;
1961 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001962 }
Brian Osman805a7272018-05-02 15:40:20 -04001963 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1964 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1965 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001966 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001967 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001968 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001969 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001970 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001971 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001972 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001973 }
1974
Ben Wagnera580fb32018-04-17 11:16:32 -04001975 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001976 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001977 if (fPaintOverrides.fAntiAlias) {
1978 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001979 }
1980 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001981 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001982 {
1983 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1984 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001985 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001986 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1987 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001988 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001989 fPaintOverrides.fAntiAlias = true;
1990 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001991 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001992 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001993 case SkPaintFields::AntiAliasState::Alias:
1994 break;
1995 case SkPaintFields::AntiAliasState::Normal:
1996 break;
1997 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1998 gSkUseAnalyticAA = true;
1999 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002000 break;
2001 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2002 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002003 break;
2004 }
2005 }
2006 paramsChanged = true;
2007 }
2008
Ben Wagner99a78dc2018-05-09 18:23:51 -04002009 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002010 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002011 bool (SkPaint::* isFlag)() const,
2012 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002013 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002014 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002015 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002016 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002017 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002018 if (ImGui::Combo(label, &itemIndex, items)) {
2019 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002020 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002021 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002022 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002023 (fPaint.*setFlag)(itemIndex == 2);
2024 }
2025 paramsChanged = true;
2026 }
2027 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002028
Ben Wagner99a78dc2018-05-09 18:23:51 -04002029 paintFlag("Dither",
2030 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002031 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002032 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002033
2034 int styleIdx = 0;
2035 if (fPaintOverrides.fStyle) {
2036 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2037 }
2038 if (ImGui::Combo("Style", &styleIdx,
2039 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2040 {
2041 if (styleIdx == 0) {
2042 fPaintOverrides.fStyle = false;
2043 fPaint.setStyle(SkPaint::kFill_Style);
2044 } else {
2045 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2046 fPaintOverrides.fStyle = true;
2047 }
2048 paramsChanged = true;
2049 }
2050
2051 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2052 if (fPaintOverrides.fWidth) {
2053 float width = fPaint.getStrokeWidth();
2054 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2055 fPaint.setStrokeWidth(width);
2056 paramsChanged = true;
2057 }
2058 }
2059
2060 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2061 if (fPaintOverrides.fMiterLimit) {
2062 float miterLimit = fPaint.getStrokeMiter();
2063 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2064 fPaint.setStrokeMiter(miterLimit);
2065 paramsChanged = true;
2066 }
2067 }
2068
2069 int capIdx = 0;
2070 if (fPaintOverrides.fCapType) {
2071 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2072 }
2073 if (ImGui::Combo("Cap Type", &capIdx,
2074 "Default\0Butt\0Round\0Square\0\0"))
2075 {
2076 if (capIdx == 0) {
2077 fPaintOverrides.fCapType = false;
2078 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2079 } else {
2080 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2081 fPaintOverrides.fCapType = true;
2082 }
2083 paramsChanged = true;
2084 }
2085
2086 int joinIdx = 0;
2087 if (fPaintOverrides.fJoinType) {
2088 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2089 }
2090 if (ImGui::Combo("Join Type", &joinIdx,
2091 "Default\0Miter\0Round\0Bevel\0\0"))
2092 {
2093 if (joinIdx == 0) {
2094 fPaintOverrides.fJoinType = false;
2095 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2096 } else {
2097 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2098 fPaintOverrides.fJoinType = true;
2099 }
2100 paramsChanged = true;
2101 }
Ben Wagner9613e452019-01-23 10:34:59 -05002102 }
Hal Canary02738a82019-01-21 18:51:32 +00002103
Ben Wagner9613e452019-01-23 10:34:59 -05002104 if (ImGui::CollapsingHeader("Font")) {
2105 int hintingIdx = 0;
2106 if (fFontOverrides.fHinting) {
2107 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2108 }
2109 if (ImGui::Combo("Hinting", &hintingIdx,
2110 "Default\0None\0Slight\0Normal\0Full\0\0"))
2111 {
2112 if (hintingIdx == 0) {
2113 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002114 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002115 } else {
2116 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2117 fFontOverrides.fHinting = true;
2118 }
2119 paramsChanged = true;
2120 }
Hal Canary02738a82019-01-21 18:51:32 +00002121
Ben Wagner9613e452019-01-23 10:34:59 -05002122 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2123 bool SkFontFields::* flag,
2124 bool (SkFont::* isFlag)() const,
2125 void (SkFont::* setFlag)(bool) )
2126 {
2127 int itemIndex = 0;
2128 if (fFontOverrides.*flag) {
2129 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2130 }
2131 if (ImGui::Combo(label, &itemIndex, items)) {
2132 if (itemIndex == 0) {
2133 fFontOverrides.*flag = false;
2134 } else {
2135 fFontOverrides.*flag = true;
2136 (fFont.*setFlag)(itemIndex == 2);
2137 }
2138 paramsChanged = true;
2139 }
2140 };
Hal Canary02738a82019-01-21 18:51:32 +00002141
Ben Wagner9613e452019-01-23 10:34:59 -05002142 fontFlag("Fake Bold Glyphs",
2143 "Default\0No Fake Bold\0Fake Bold\0\0",
2144 &SkFontFields::fEmbolden,
2145 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002146
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002147 fontFlag("Baseline Snapping",
2148 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2149 &SkFontFields::fBaselineSnap,
2150 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2151
Ben Wagner9613e452019-01-23 10:34:59 -05002152 fontFlag("Linear Text",
2153 "Default\0No Linear Text\0Linear Text\0\0",
2154 &SkFontFields::fLinearMetrics,
2155 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002156
Ben Wagner9613e452019-01-23 10:34:59 -05002157 fontFlag("Subpixel Position Glyphs",
2158 "Default\0Pixel Text\0Subpixel Text\0\0",
2159 &SkFontFields::fSubpixel,
2160 &SkFont::isSubpixel, &SkFont::setSubpixel);
2161
2162 fontFlag("Embedded Bitmap Text",
2163 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2164 &SkFontFields::fEmbeddedBitmaps,
2165 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2166
2167 fontFlag("Force Auto-Hinting",
2168 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2169 &SkFontFields::fForceAutoHinting,
2170 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2171
2172 int edgingIdx = 0;
2173 if (fFontOverrides.fEdging) {
2174 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2175 }
2176 if (ImGui::Combo("Edging", &edgingIdx,
2177 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2178 {
2179 if (edgingIdx == 0) {
2180 fFontOverrides.fEdging = false;
2181 fFont.setEdging(SkFont::Edging::kAlias);
2182 } else {
2183 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2184 fFontOverrides.fEdging = true;
2185 }
2186 paramsChanged = true;
2187 }
2188
Ben Wagner15a8d572019-03-21 13:35:44 -04002189 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2190 if (fFontOverrides.fSize) {
2191 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002192 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002193 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002194 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002195 fFontOverrides.fSizeRange[0],
2196 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002197 "%.6f", 2.0f))
2198 {
Mike Reed3ae47332019-01-04 10:11:46 -05002199 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002200 paramsChanged = true;
2201 }
2202 }
2203
2204 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2205 if (fFontOverrides.fScaleX) {
2206 float scaleX = fFont.getScaleX();
2207 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2208 fFont.setScaleX(scaleX);
2209 paramsChanged = true;
2210 }
2211 }
2212
2213 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2214 if (fFontOverrides.fSkewX) {
2215 float skewX = fFont.getSkewX();
2216 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2217 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002218 paramsChanged = true;
2219 }
2220 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002221 }
2222
Mike Reed81f60ec2018-05-15 10:09:52 -04002223 {
2224 SkMetaData controls;
2225 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2226 if (ImGui::CollapsingHeader("Current Slide")) {
2227 SkMetaData::Iter iter(controls);
2228 const char* name;
2229 SkMetaData::Type type;
2230 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002231 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002232 if (type == SkMetaData::kScalar_Type) {
2233 float val[3];
2234 SkASSERT(count == 3);
2235 controls.findScalars(name, &count, val);
2236 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2237 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002238 }
Ben Wagner110c7032019-03-22 17:03:59 -04002239 } else if (type == SkMetaData::kBool_Type) {
2240 bool val;
2241 SkASSERT(count == 1);
2242 controls.findBool(name, &val);
2243 if (ImGui::Checkbox(name, &val)) {
2244 controls.setBool(name, val);
2245 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002246 }
2247 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002248 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002249 }
2250 }
2251 }
2252
Ben Wagner7a3c6742018-04-23 10:01:07 -04002253 if (fShowSlidePicker) {
2254 ImGui::SetNextTreeNodeOpen(true);
2255 }
Brian Osman79086b92017-02-10 13:36:16 -05002256 if (ImGui::CollapsingHeader("Slide")) {
2257 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002258 static ImVector<const char*> filteredSlideNames;
2259 static ImVector<int> filteredSlideIndices;
2260
Brian Osmanfce09c52017-11-14 15:32:20 -05002261 if (fShowSlidePicker) {
2262 ImGui::SetKeyboardFocusHere();
2263 fShowSlidePicker = false;
2264 }
2265
Brian Osman79086b92017-02-10 13:36:16 -05002266 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002267 filteredSlideNames.clear();
2268 filteredSlideIndices.clear();
2269 int filteredIndex = 0;
2270 for (int i = 0; i < fSlides.count(); ++i) {
2271 const char* slideName = fSlides[i]->getName().c_str();
2272 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2273 if (i == fCurrentSlide) {
2274 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002275 }
Brian Osmanf479e422017-11-08 13:11:36 -05002276 filteredSlideNames.push_back(slideName);
2277 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002278 }
Brian Osman79086b92017-02-10 13:36:16 -05002279 }
Brian Osmanf479e422017-11-08 13:11:36 -05002280
Brian Osmanf479e422017-11-08 13:11:36 -05002281 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2282 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002283 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002284 }
2285 }
Brian Osmana109e392017-02-24 09:49:14 -05002286
2287 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002288 ColorMode newMode = fColorMode;
2289 auto cmButton = [&](ColorMode mode, const char* label) {
2290 if (ImGui::RadioButton(label, mode == fColorMode)) {
2291 newMode = mode;
2292 }
2293 };
2294
2295 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002296 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2297 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002298 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002299
2300 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002301 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002302 }
2303
2304 // Pick from common gamuts:
2305 int primariesIdx = 4; // Default: Custom
2306 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2307 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2308 primariesIdx = i;
2309 break;
2310 }
2311 }
2312
Brian Osman03115dc2018-11-26 13:55:19 -05002313 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002314 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002315
Brian Osmana109e392017-02-24 09:49:14 -05002316 if (ImGui::Combo("Primaries", &primariesIdx,
2317 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2318 if (primariesIdx >= 0 && primariesIdx <= 3) {
2319 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2320 }
2321 }
2322
2323 // Allow direct editing of gamut
2324 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2325 }
Brian Osman207d4102019-01-10 09:40:58 -05002326
2327 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002328 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002329 if (ImGui::Checkbox("Pause", &isPaused)) {
2330 fAnimTimer.togglePauseResume();
2331 }
Brian Osman707d2022019-01-10 11:27:34 -05002332
2333 float speed = fAnimTimer.getSpeed();
2334 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2335 fAnimTimer.setSpeed(speed);
2336 }
Brian Osman207d4102019-01-10 09:40:58 -05002337 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002338
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002339 if (ImGui::CollapsingHeader("Shaders")) {
2340 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2341 GrContextOptions::ShaderCacheStrategy::kSkSL;
John Stiles7247b482021-03-08 10:40:35 -05002342
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002343#if defined(SK_VULKAN)
2344 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2345#else
2346 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002347#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002348
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002349 // To re-load shaders from the currently active programs, we flush all
2350 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002351 static bool gLoadPending = false;
2352 if (gLoadPending) {
2353 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
Brian Osmanf0de96f2021-02-26 13:54:11 -05002354 const SkString& description, int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002355 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002356 entry.fKey = key;
2357 SkMD5 hash;
2358 hash.write(key->bytes(), key->size());
2359 SkMD5::Digest digest = hash.finish();
2360 for (int i = 0; i < 16; ++i) {
2361 entry.fKeyString.appendf("%02x", digest.data[i]);
2362 }
Brian Osmanf0de96f2021-02-26 13:54:11 -05002363 entry.fKeyDescription = description;
Brian Osman0b8bb882019-04-12 11:47:19 -04002364
Brian Osman9e4e4c72020-06-10 07:19:34 -04002365 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002366 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002367 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2368 entry.fInputs,
2369 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002370 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002371 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002372 fPersistentCache.foreach(collectShaders);
2373 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002374
2375#if defined(SK_VULKAN)
2376 if (isVulkan && !sksl) {
2377 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2378 for (auto& entry : fCachedShaders) {
2379 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2380 const SkSL::String& spirv(entry.fShader[i]);
2381 std::string disasm;
2382 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2383 &disasm);
2384 entry.fShader[i].assign(disasm);
2385 }
2386 }
2387 }
2388#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002389 }
2390
John Stilesa8f6b6f2021-03-05 16:00:42 -05002391 // Defer actually doing the View/Apply logic so that we can trigger an Apply when we
Brian Osman0b8bb882019-04-12 11:47:19 -04002392 // start or finish hovering on a tree node in the list below:
John Stilesa8f6b6f2021-03-05 16:00:42 -05002393 bool doView = ImGui::Button("View"); ImGui::SameLine();
2394 bool doApply = ImGui::Button("Apply Changes"); ImGui::SameLine();
John Stiles7247b482021-03-08 10:40:35 -05002395 bool doDump = ImGui::Button("Dump SkSL to resources/sksl/");
John Stilesa8f6b6f2021-03-05 16:00:42 -05002396
John Stiles2ee4d7a2021-03-30 10:30:47 -04002397 int newOptLevel = fOptLevel;
John Stiles7247b482021-03-08 10:40:35 -05002398 ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source);
2399 ImGui::SameLine();
2400 ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile);
2401 ImGui::SameLine();
2402 ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize);
2403 ImGui::SameLine();
2404 ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline);
John Stilesa8f6b6f2021-03-05 16:00:42 -05002405
John Stiles7247b482021-03-08 10:40:35 -05002406 // If we are changing the compile mode, we want to reset the cache and redo
2407 // everything.
John Stiles2ee4d7a2021-03-30 10:30:47 -04002408 if (doDump || newOptLevel != fOptLevel) {
John Stiles7247b482021-03-08 10:40:35 -05002409 sksl = doDump || (newOptLevel == kShaderOptLevel_Source);
John Stiles2ee4d7a2021-03-30 10:30:47 -04002410 fOptLevel = (ShaderOptLevel)newOptLevel;
2411 switch (fOptLevel) {
2412 case kShaderOptLevel_Source:
2413 Compiler::EnableOptimizer(OverrideFlag::kDefault);
2414 Compiler::EnableInliner(OverrideFlag::kDefault);
2415 break;
2416 case kShaderOptLevel_Compile:
2417 Compiler::EnableOptimizer(OverrideFlag::kOff);
2418 Compiler::EnableInliner(OverrideFlag::kOff);
2419 break;
2420 case kShaderOptLevel_Optimize:
2421 Compiler::EnableOptimizer(OverrideFlag::kOn);
2422 Compiler::EnableInliner(OverrideFlag::kOff);
2423 break;
2424 case kShaderOptLevel_Inline:
2425 Compiler::EnableOptimizer(OverrideFlag::kOn);
2426 Compiler::EnableInliner(OverrideFlag::kOn);
2427 break;
2428 }
John Stiles7247b482021-03-08 10:40:35 -05002429
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002430 params.fGrContextOptions.fShaderCacheStrategy =
2431 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2432 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2433 paramsChanged = true;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002434 doView = true;
2435
2436 fDeferredActions.push_back([=]() {
2437 // Reset the cache.
2438 fPersistentCache.reset();
2439 // Dump the cache once we have drawn a frame with it.
2440 if (doDump) {
2441 fDeferredActions.push_back([this]() {
2442 this->dumpShadersToResources();
2443 });
2444 }
2445 });
Brian Osmancbc33b82019-04-19 14:16:19 -04002446 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002447
2448 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002449 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002450 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2451 bool hovered = ImGui::IsItemHovered();
2452 if (hovered != entry.fHovered) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002453 // Force an Apply to patch the highlight shader in/out
Brian Osman0b8bb882019-04-12 11:47:19 -04002454 entry.fHovered = hovered;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002455 doApply = true;
Brian Osman0b8bb882019-04-12 11:47:19 -04002456 }
2457 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002458 auto stringBox = [](const char* label, std::string* str) {
2459 // Full width, and not too much space for each shader
2460 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2461 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2462 ImGui::InputTextMultiline(label, str, boxSize);
2463 };
Brian Osmanf0de96f2021-02-26 13:54:11 -05002464 if (ImGui::TreeNode("Key")) {
2465 ImGui::TextWrapped("%s", entry.fKeyDescription.c_str());
2466 ImGui::TreePop();
2467 }
Brian Osmaneb3fb902020-08-18 13:16:59 -04002468 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2469 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002470 ImGui::TreePop();
2471 }
2472 }
2473 ImGui::EndChild();
2474
John Stilesa8f6b6f2021-03-05 16:00:42 -05002475 if (doView) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002476 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002477 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002478 gLoadPending = true;
2479 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002480
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002481 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2482 // but I'm not sure anyone wants to do that.
2483 if (isVulkan && !sksl) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002484 doApply = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002485 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002486 if (doApply) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002487 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002488 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002489 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002490 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002491 if (entry.fHovered) {
2492 // The hovered item (if any) gets a special shader to make it
2493 // identifiable.
2494 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2495 switch (entry.fShaderType) {
2496 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2497 fragShader = build_sksl_highlight_shader();
2498 break;
2499 }
2500 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2501 fragShader = build_glsl_highlight_shader(
2502 *ctx->priv().caps()->shaderCaps());
2503 break;
2504 }
2505 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2506 fragShader = build_metal_highlight_shader(fragShader);
2507 break;
2508 }
2509 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002510 }
2511
Brian Osmana085a412019-04-25 09:44:43 -04002512 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2513 entry.fShader,
2514 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002515 kGrShaderTypeCount);
Brian Osmanf0de96f2021-02-26 13:54:11 -05002516 fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription);
Brian Osman0b8bb882019-04-12 11:47:19 -04002517
2518 entry.fShader[kFragment_GrShaderType] = backup;
2519 }
2520 }
2521 }
Brian Osman79086b92017-02-10 13:36:16 -05002522 }
Brian Salomon99a33902017-03-07 15:16:34 -05002523 if (paramsChanged) {
2524 fDeferredActions.push_back([=]() {
2525 fWindow->setRequestedDisplayParams(params);
2526 fWindow->inval();
2527 this->updateTitle();
2528 });
2529 }
Brian Osman79086b92017-02-10 13:36:16 -05002530 ImGui::End();
2531 }
2532
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002533 if (gShaderErrorHandler.fErrors.count()) {
2534 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002535 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002536 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2537 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002538 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2539 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2540 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2541 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002542 }
2543 ImGui::End();
2544 gShaderErrorHandler.reset();
2545 }
2546
Brian Osmanf6877092017-02-13 09:39:57 -05002547 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002548 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2549 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002550 static int zoomFactor = 8;
2551 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002552 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002553 }
2554 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2555 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002556 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002557 }
Brian Osmanf6877092017-02-13 09:39:57 -05002558
Ben Wagner3627d2e2018-06-26 14:23:20 -04002559 if (!fZoomWindowFixed) {
2560 ImVec2 mousePos = ImGui::GetMousePos();
2561 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2562 }
2563 SkScalar x = fZoomWindowLocation.x();
2564 SkScalar y = fZoomWindowLocation.y();
2565 int xInt = SkScalarRoundToInt(x);
2566 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002567 ImVec2 avail = ImGui::GetContentRegionAvail();
2568
Brian Osmanead517d2017-11-13 15:36:36 -05002569 uint32_t pixel = 0;
2570 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002571 auto dContext = fWindow->directContext();
2572 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002573 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002574 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002575 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002576 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002577 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2578 }
2579
Greg Danielfbc60b72020-11-03 17:27:02 -05002580 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002581 // Translate so the region of the image that's under the mouse cursor is centered
2582 // in the zoom canvas:
2583 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002584 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2585 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002586 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002587
2588 SkPaint outline;
2589 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002590 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002591 });
Brian Osmanf6877092017-02-13 09:39:57 -05002592 }
2593
2594 ImGui::End();
2595 }
Brian Osman79086b92017-02-10 13:36:16 -05002596}
2597
John Stilesa8f6b6f2021-03-05 16:00:42 -05002598void Viewer::dumpShadersToResources() {
2599 // Sort the list of cached shaders so we can maintain some minimal level of consistency.
2600 // It doesn't really matter, but it will keep files from switching places unpredictably.
2601 std::vector<const CachedShader*> shaders;
2602 shaders.reserve(fCachedShaders.size());
2603 for (const CachedShader& shader : fCachedShaders) {
2604 shaders.push_back(&shader);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002605 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002606
2607 std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) {
2608 return std::tie(a->fShader[kFragment_GrShaderType], a->fShader[kVertex_GrShaderType]) <
2609 std::tie(b->fShader[kFragment_GrShaderType], b->fShader[kVertex_GrShaderType]);
2610 });
2611
2612 // Make the resources/sksl/SlideName/ directory.
2613 SkString directory = SkStringPrintf("%ssksl/%s",
2614 GetResourcePath().c_str(),
2615 fSlides[fCurrentSlide]->getName().c_str());
2616 if (!sk_mkdir(directory.c_str())) {
2617 SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str());
2618 return;
2619 }
2620
2621 int index = 0;
2622 for (const auto& entry : shaders) {
2623 SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index);
2624 FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag);
2625 if (vertFile) {
2626 const SkSL::String& vertText = entry->fShader[kVertex_GrShaderType];
2627 SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile));
2628 sk_fclose(vertFile);
2629 } else {
2630 SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str());
2631 }
2632
2633 SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index);
2634 FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag);
2635 if (fragFile) {
2636 const SkSL::String& fragText = entry->fShader[kFragment_GrShaderType];
2637 SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile));
2638 sk_fclose(fragFile);
2639 } else {
2640 SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str());
2641 }
2642
2643 ++index;
2644 }
2645}
2646
2647void Viewer::onIdle() {
2648 SkTArray<std::function<void()>> actionsToRun;
2649 actionsToRun.swap(fDeferredActions);
2650
2651 for (const auto& fn : actionsToRun) {
2652 fn();
2653 }
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002654
Brian Osman56a24812017-12-19 11:15:16 -05002655 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002656 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002657 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002658 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002659
Brian Osman79086b92017-02-10 13:36:16 -05002660 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002661 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2662 // not be visible, though. So we need to redraw if there is at least one visible window, or
2663 // more than one active window. Newly created windows are active but not visible for one frame
2664 // while they determine their layout and sizing.
2665 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2666 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002667 fWindow->inval();
2668 }
jvanverth9f372462016-04-06 06:08:59 -07002669}
liyuqiane5a6cd92016-05-27 08:52:52 -07002670
Florin Malitab632df72018-06-18 21:23:06 -04002671template <typename OptionsFunc>
2672static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2673 OptionsFunc&& optionsFunc) {
2674 writer.beginObject();
2675 {
2676 writer.appendString(kName , name);
2677 writer.appendString(kValue, value);
2678
2679 writer.beginArray(kOptions);
2680 {
2681 optionsFunc(writer);
2682 }
2683 writer.endArray();
2684 }
2685 writer.endObject();
2686}
2687
2688
liyuqiane5a6cd92016-05-27 08:52:52 -07002689void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002690 if (!fWindow) {
2691 return;
2692 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002693 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002694 return; // Surface hasn't been created yet.
2695 }
2696
Florin Malitab632df72018-06-18 21:23:06 -04002697 SkDynamicMemoryWStream memStream;
2698 SkJSONWriter writer(&memStream);
2699 writer.beginArray();
2700
liyuqianb73c24b2016-06-03 08:47:23 -07002701 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002702 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2703 [this](SkJSONWriter& writer) {
2704 for(const auto& slide : fSlides) {
2705 writer.appendString(slide->getName().c_str());
2706 }
2707 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002708
liyuqianb73c24b2016-06-03 08:47:23 -07002709 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002710 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2711 [](SkJSONWriter& writer) {
2712 for (const auto& str : kBackendTypeStrings) {
2713 writer.appendString(str);
2714 }
2715 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002716
csmartdalton578f0642017-02-24 16:04:47 -07002717 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002718 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2719 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2720 [this](SkJSONWriter& writer) {
2721 writer.appendS32(0);
2722
2723 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2724 return;
2725 }
2726
2727 for (int msaa : {4, 8, 16}) {
2728 writer.appendS32(msaa);
2729 }
2730 });
csmartdalton578f0642017-02-24 16:04:47 -07002731
csmartdalton61cd31a2017-02-27 17:00:53 -07002732 // Path renderer state
2733 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002734 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2735 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002736 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002737 if (!ctx) {
2738 writer.appendString("Software");
2739 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002740 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002741 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2742 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002743 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002744 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002745 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002746 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002747 }
2748 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002749 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2750 writer.appendString(
2751 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2752 }
2753 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2754 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002755 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002756 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002757 }
2758 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002759
liyuqianb73c24b2016-06-03 08:47:23 -07002760 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002761 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2762 [this](SkJSONWriter& writer) {
2763 writer.appendString(kSoftkeyHint);
2764 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2765 writer.appendString(softkey.c_str());
2766 }
2767 });
liyuqianb73c24b2016-06-03 08:47:23 -07002768
Florin Malitab632df72018-06-18 21:23:06 -04002769 writer.endArray();
2770 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002771
Florin Malitab632df72018-06-18 21:23:06 -04002772 auto data = memStream.detachAsData();
2773
2774 // TODO: would be cool to avoid this copy
2775 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2776
2777 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002778}
2779
2780void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002781 // For those who will add more features to handle the state change in this function:
2782 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2783 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2784 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002785 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002786 for (int i = 0; i < fSlides.count(); ++i) {
2787 if (fSlides[i]->getName().equals(stateValue)) {
2788 this->setCurrentSlide(i);
2789 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002790 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002791 }
Florin Malitaab99c342018-01-16 16:23:03 -05002792
2793 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002794 } else if (stateName.equals(kBackendStateName)) {
2795 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2796 if (stateValue.equals(kBackendTypeStrings[i])) {
2797 if (fBackendType != i) {
2798 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002799 for(auto& slide : fSlides) {
2800 slide->gpuTeardown();
2801 }
liyuqian6cb70252016-06-02 12:16:25 -07002802 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002803 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002804 }
2805 break;
2806 }
2807 }
csmartdalton578f0642017-02-24 16:04:47 -07002808 } else if (stateName.equals(kMSAAStateName)) {
2809 DisplayParams params = fWindow->getRequestedDisplayParams();
2810 int sampleCount = atoi(stateValue.c_str());
2811 if (sampleCount != params.fMSAASampleCount) {
2812 params.fMSAASampleCount = sampleCount;
2813 fWindow->setRequestedDisplayParams(params);
2814 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002815 this->updateTitle();
2816 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002817 }
2818 } else if (stateName.equals(kPathRendererStateName)) {
2819 DisplayParams params = fWindow->getRequestedDisplayParams();
2820 for (const auto& pair : gPathRendererNames) {
2821 if (pair.second == stateValue.c_str()) {
2822 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2823 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2824 fWindow->setRequestedDisplayParams(params);
2825 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002826 this->updateTitle();
2827 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002828 }
2829 break;
2830 }
csmartdalton578f0642017-02-24 16:04:47 -07002831 }
liyuqianb73c24b2016-06-03 08:47:23 -07002832 } else if (stateName.equals(kSoftkeyStateName)) {
2833 if (!stateValue.equals(kSoftkeyHint)) {
2834 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002835 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002836 }
liyuqian2edb0f42016-07-06 14:11:32 -07002837 } else if (stateName.equals(kRefreshStateName)) {
2838 // This state is actually NOT in the UI state.
2839 // We use this to allow Android to quickly set bool fRefresh.
2840 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002841 } else {
2842 SkDebugf("Unknown stateName: %s", stateName.c_str());
2843 }
2844}
Brian Osman79086b92017-02-10 13:36:16 -05002845
Hal Canaryb1f411a2019-08-29 10:39:22 -04002846bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002847 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002848}
2849
Hal Canaryb1f411a2019-08-29 10:39:22 -04002850bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002851 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002852 fWindow->inval();
2853 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002854 } else {
2855 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002856 }
Brian Osman79086b92017-02-10 13:36:16 -05002857}