blob: d84aa51f62975ee7ea145e9515bab66ade6838f1 [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 });
Brian Salomon91216d52021-04-09 11:57:59 -0400477 fCommands.addCommand('w', "Modes", "Toggle reduced shaders", [this]() {
478 DisplayParams params = fWindow->getRequestedDisplayParams();
479 params.fGrContextOptions.fReducedShaderVariations =
480 !params.fGrContextOptions.fReducedShaderVariations;
481 fWindow->setRequestedDisplayParams(params);
482 fWindow->inval();
483 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400484 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500485 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700486 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400487 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500488 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700489 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400490 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700491 this->changeZoomLevel(1.f / 32.f);
492 fWindow->inval();
493 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400494 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700495 this->changeZoomLevel(-1.f / 32.f);
496 fWindow->inval();
497 });
jvanverthaf236b52016-05-20 06:01:06 -0700498 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400499 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
500 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500501 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400502#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
503 if (newBackend == sk_app::Window::kVulkan_BackendType) {
504 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
505 sk_app::Window::kBackendTypeCount);
506 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
507 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500508 }
509#endif
Brian Osman621491e2017-02-28 15:45:01 -0500510 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700511 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500512 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
513 fSaveToSKP = true;
514 fWindow->inval();
515 });
Mike Reed376d8122019-03-14 11:39:02 -0400516 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
517 fShowSlideDimensions = !fShowSlideDimensions;
518 fWindow->inval();
519 });
Ben Wagner37c54032018-04-13 14:30:23 -0400520 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
521 DisplayParams params = fWindow->getRequestedDisplayParams();
522 uint32_t flags = params.fSurfaceProps.flags();
Ben Wagnerae4bb982020-09-24 14:49:00 -0400523 SkPixelGeometry defaultPixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
524 if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
525 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -0400526 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
527 } else {
528 switch (params.fSurfaceProps.pixelGeometry()) {
529 case kUnknown_SkPixelGeometry:
530 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
531 break;
532 case kRGB_H_SkPixelGeometry:
533 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
534 break;
535 case kBGR_H_SkPixelGeometry:
536 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
537 break;
538 case kRGB_V_SkPixelGeometry:
539 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
540 break;
541 case kBGR_V_SkPixelGeometry:
Ben Wagnerae4bb982020-09-24 14:49:00 -0400542 params.fSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry);
543 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
Ben Wagner37c54032018-04-13 14:30:23 -0400544 break;
545 }
546 }
547 fWindow->setRequestedDisplayParams(params);
548 this->updateTitle();
549 fWindow->inval();
550 });
Ben Wagner9613e452019-01-23 10:34:59 -0500551 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500552 if (!fFontOverrides.fHinting) {
553 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400554 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500555 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500556 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400557 case SkFontHinting::kNone:
558 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500559 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400560 case SkFontHinting::kSlight:
561 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500562 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400563 case SkFontHinting::kNormal:
564 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500565 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400566 case SkFontHinting::kFull:
567 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500568 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500569 break;
570 }
571 }
572 this->updateTitle();
573 fWindow->inval();
574 });
575 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500576 if (!fPaintOverrides.fAntiAlias) {
577 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
578 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500579 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500580 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500581 } else {
582 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500583 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500584 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500585 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400586 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500587 break;
588 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500589 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500590 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400591 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500592 break;
593 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500594 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400595 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500596 break;
597 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500598 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
599 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500600 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
601 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500602 break;
603 }
604 }
605 this->updateTitle();
606 fWindow->inval();
607 });
Ben Wagner37c54032018-04-13 14:30:23 -0400608 fCommands.addCommand('D', "Modes", "DFT", [this]() {
609 DisplayParams params = fWindow->getRequestedDisplayParams();
610 uint32_t flags = params.fSurfaceProps.flags();
611 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
612 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
613 fWindow->setRequestedDisplayParams(params);
614 this->updateTitle();
615 fWindow->inval();
616 });
Ben Wagner9613e452019-01-23 10:34:59 -0500617 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500618 if (!fFontOverrides.fEdging) {
619 fFontOverrides.fEdging = true;
620 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500621 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500622 switch (fFont.getEdging()) {
623 case SkFont::Edging::kAlias:
624 fFont.setEdging(SkFont::Edging::kAntiAlias);
625 break;
626 case SkFont::Edging::kAntiAlias:
627 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
628 break;
629 case SkFont::Edging::kSubpixelAntiAlias:
630 fFont.setEdging(SkFont::Edging::kAlias);
631 fFontOverrides.fEdging = false;
632 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500633 }
634 }
635 this->updateTitle();
636 fWindow->inval();
637 });
Ben Wagner9613e452019-01-23 10:34:59 -0500638 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500639 if (!fFontOverrides.fSubpixel) {
640 fFontOverrides.fSubpixel = true;
641 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500642 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500643 if (!fFont.isSubpixel()) {
644 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500645 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500646 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500647 }
648 }
649 this->updateTitle();
650 fWindow->inval();
651 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400652 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
653 if (!fFontOverrides.fBaselineSnap) {
654 fFontOverrides.fBaselineSnap = true;
655 fFont.setBaselineSnap(false);
656 } else {
657 if (!fFont.isBaselineSnap()) {
658 fFont.setBaselineSnap(true);
659 } else {
660 fFontOverrides.fBaselineSnap = false;
661 }
662 }
663 this->updateTitle();
664 fWindow->inval();
665 });
Brian Osman805a7272018-05-02 15:40:20 -0400666 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
667 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
668 : kPerspective_Real;
669 this->updateTitle();
670 fWindow->inval();
671 });
672 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
673 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
674 : kPerspective_Off;
675 this->updateTitle();
676 fWindow->inval();
677 });
Brian Osman207d4102019-01-10 09:40:58 -0500678 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
679 fAnimTimer.togglePauseResume();
680 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400681 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
682 fZoomUI = !fZoomUI;
Ben Wagner9a7fcf72021-02-23 13:18:50 -0500683 fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor());
Brian Osmanb63f6002018-07-24 18:01:53 -0400684 fWindow->inval();
685 });
Mike Reed59295352020-03-12 13:56:34 -0400686 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
687 fDrawViaSerialize = !fDrawViaSerialize;
688 this->updateTitle();
689 fWindow->inval();
690 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500691 fCommands.addCommand('!', "SkVM", "Toggle SkVM blitter", [this]() {
Mike Reed862818b2020-03-21 15:07:13 -0400692 gUseSkVMBlitter = !gUseSkVMBlitter;
693 this->updateTitle();
694 fWindow->inval();
695 });
Mike Klein813e8cc2020-08-05 09:33:38 -0500696 fCommands.addCommand('@', "SkVM", "Toggle SkVM JIT", [this]() {
697 gSkVMAllowJIT = !gSkVMAllowJIT;
698 this->updateTitle();
699 fWindow->inval();
700 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500701
jvanverth2bb3b6d2016-04-08 07:24:09 -0700702 // set up slides
703 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500704 if (FLAGS_list) {
705 this->listNames();
706 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700707
Brian Osman9bb47cf2018-04-26 15:55:00 -0400708 fPerspectivePoints[0].set(0, 0);
709 fPerspectivePoints[1].set(1, 0);
710 fPerspectivePoints[2].set(0, 1);
711 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700712 fAnimTimer.run();
713
Hal Canaryc465d132017-12-08 10:21:31 -0500714 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500715 if (gamutImage) {
Mike Reed5ec22382021-01-14 21:59:01 -0500716 fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear)));
Brian Osmana109e392017-02-24 09:49:14 -0500717 }
718 fImGuiGamutPaint.setColor(SK_ColorWHITE);
Brian Osmana109e392017-02-24 09:49:14 -0500719
jongdeok.kim804f17e2019-02-26 14:39:23 +0900720 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500721 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700722}
723
jvanverth34524262016-05-04 13:49:13 -0700724void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400725 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
726 static const struct {
727 const char* fExtension;
728 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500729 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400730 const SlideFactory fFactory;
731 } gExternalSlidesInfo[] = {
732 { ".skp", "skp-dir", FLAGS_skps,
733 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
734 return sk_make_sp<SKPSlide>(name, path);}
735 },
736 { ".jpg", "jpg-dir", FLAGS_jpgs,
737 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
738 return sk_make_sp<ImageSlide>(name, path);}
739 },
Florin Malita87ccf332018-05-04 12:23:24 -0400740#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400741 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400742 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
743 return sk_make_sp<SkottieSlide>(name, path);}
744 },
Florin Malita87ccf332018-05-04 12:23:24 -0400745#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400746 #if defined(SK_ENABLE_SKRIVE)
747 { ".flr", "skrive-dir", FLAGS_rives,
748 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
749 return sk_make_sp<SkRiveSlide>(name, path);}
750 },
751 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400752#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400753 { ".svg", "svg-dir", FLAGS_svgs,
754 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
755 return sk_make_sp<SvgSlide>(name, path);}
756 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400757#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400758 };
jvanverthc265a922016-04-08 12:51:45 -0700759
Brian Salomon343553a2018-09-05 15:41:23 -0400760 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700761
Mike Klein88544fb2019-03-20 10:50:33 -0500762 const auto addSlide =
763 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
764 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
765 return;
766 }
liyuqian6f163d22016-06-13 12:26:45 -0700767
Mike Klein88544fb2019-03-20 10:50:33 -0500768 if (auto slide = fact(name, path)) {
769 dirSlides.push_back(slide);
770 fSlides.push_back(std::move(slide));
771 }
772 };
Florin Malita76a076b2018-02-15 18:40:48 -0500773
Florin Malita38792ce2018-05-08 10:36:18 -0400774 if (!FLAGS_file.isEmpty()) {
775 // single file mode
776 const SkString file(FLAGS_file[0]);
777
778 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
779 for (const auto& sinfo : gExternalSlidesInfo) {
780 if (file.endsWith(sinfo.fExtension)) {
781 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
782 return;
783 }
784 }
785
786 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
787 } else {
788 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
789 }
790
791 return;
792 }
793
794 // Bisect slide.
795 if (!FLAGS_bisect.isEmpty()) {
796 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500797 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400798 if (FLAGS_bisect.count() >= 2) {
799 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
800 bisect->onChar(*ch);
801 }
802 }
803 fSlides.push_back(std::move(bisect));
804 }
805 }
806
807 // GMs
808 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400809 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400810 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500811 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400812 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400813 fSlides.push_back(std::move(slide));
814 }
Florin Malita38792ce2018-05-08 10:36:18 -0400815 }
816 // reverse gms
817 int numGMs = fSlides.count() - firstGM;
818 for (int i = 0; i < numGMs/2; ++i) {
819 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
820 }
821
822 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400823 for (const SampleFactory factory : SampleRegistry::Range()) {
824 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500825 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400826 fSlides.push_back(slide);
827 }
Florin Malita38792ce2018-05-08 10:36:18 -0400828 }
829
Brian Osman7c979f52019-02-12 13:27:51 -0500830 // Particle demo
831 {
832 // TODO: Convert this to a sample
833 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500834 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500835 fSlides.push_back(std::move(slide));
836 }
837 }
838
Brian Osmand927bd22019-12-18 11:23:12 -0500839 // Runtime shader editor
840 {
841 sk_sp<Slide> slide(new SkSLSlide());
842 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
843 fSlides.push_back(std::move(slide));
844 }
845 }
846
Florin Malita0ffa3222018-04-05 14:34:45 -0400847 for (const auto& info : gExternalSlidesInfo) {
848 for (const auto& flag : info.fFlags) {
849 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
850 // single file
851 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
852 } else {
853 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400854 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400855 SkTArray<SkString> sortedFilenames;
856 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400857 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400858 sortedFilenames.push_back(name);
859 }
860 if (sortedFilenames.count()) {
John Stiles886a9042020-07-14 16:28:33 -0400861 SkTQSort(sortedFilenames.begin(), sortedFilenames.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000862 [](const SkString& a, const SkString& b) {
863 return strcmp(a.c_str(), b.c_str()) < 0;
864 });
Tyler Denniston31dc4812020-04-09 11:17:21 -0400865 }
866 for (const SkString& filename : sortedFilenames) {
867 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
868 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400869 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400870 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400871 if (!dirSlides.empty()) {
872 fSlides.push_back(
873 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
874 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500875 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400876 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400877 }
878 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500879
880 if (!fSlides.count()) {
881 sk_sp<Slide> slide(new NullSlide());
882 fSlides.push_back(std::move(slide));
883 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700884}
885
886
jvanverth34524262016-05-04 13:49:13 -0700887Viewer::~Viewer() {
Robert Phillipse9229532020-06-26 10:10:49 -0400888 for(auto& slide : fSlides) {
889 slide->gpuTeardown();
890 }
891
jvanverth9f372462016-04-06 06:08:59 -0700892 fWindow->detach();
893 delete fWindow;
894}
895
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500896struct SkPaintTitleUpdater {
897 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
898 void append(const char* s) {
899 if (fCount == 0) {
900 fTitle->append(" {");
901 } else {
902 fTitle->append(", ");
903 }
904 fTitle->append(s);
905 ++fCount;
906 }
907 void done() {
908 if (fCount > 0) {
909 fTitle->append("}");
910 }
911 }
912 SkString* fTitle;
913 int fCount;
914};
915
brianosman05de2162016-05-06 13:28:57 -0700916void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700917 if (!fWindow) {
918 return;
919 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500920 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700921 return; // Surface hasn't been created yet.
922 }
923
jvanverth34524262016-05-04 13:49:13 -0700924 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700925 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700926
Mike Kleine5acd752019-03-22 09:57:16 -0500927 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400928 if (gSkForceAnalyticAA) {
929 title.append(" <FAAA>");
930 } else {
931 title.append(" <AAA>");
932 }
933 }
Mike Reed59295352020-03-12 13:56:34 -0400934 if (fDrawViaSerialize) {
935 title.append(" <serialize>");
936 }
Mike Reed862818b2020-03-21 15:07:13 -0400937 if (gUseSkVMBlitter) {
Mike Klein813e8cc2020-08-05 09:33:38 -0500938 title.append(" <SkVMBlitter>");
939 }
940 if (!gSkVMAllowJIT) {
941 title.append(" <SkVM interpreter>");
Mike Reed862818b2020-03-21 15:07:13 -0400942 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400943
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500944 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500945 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
946 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400947 const char* on, const char* off)
948 {
Ben Wagner9613e452019-01-23 10:34:59 -0500949 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400950 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500951 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400952 };
953
Ben Wagner9613e452019-01-23 10:34:59 -0500954 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
955 const char* on, const char* off)
956 {
957 if (fFontOverrides.*flag) {
958 paintTitle.append((fFont.*isFlag)() ? on : off);
959 }
960 };
961
962 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
963 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
964
965 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
966 "Force Autohint", "No Force Autohint");
967 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400968 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500969 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
970 "Linear Metrics", "Non-Linear Metrics");
971 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
972 "Bitmap Text", "No Bitmap Text");
973 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
974
975 if (fFontOverrides.fEdging) {
976 switch (fFont.getEdging()) {
977 case SkFont::Edging::kAlias:
978 paintTitle.append("Alias Text");
979 break;
980 case SkFont::Edging::kAntiAlias:
981 paintTitle.append("Antialias Text");
982 break;
983 case SkFont::Edging::kSubpixelAntiAlias:
984 paintTitle.append("Subpixel Antialias Text");
985 break;
986 }
987 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400988
Mike Reed3ae47332019-01-04 10:11:46 -0500989 if (fFontOverrides.fHinting) {
990 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400991 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500992 paintTitle.append("No Hinting");
993 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400994 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500995 paintTitle.append("Slight Hinting");
996 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400997 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500998 paintTitle.append("Normal Hinting");
999 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001000 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001001 paintTitle.append("Full Hinting");
1002 break;
1003 }
1004 }
1005 paintTitle.done();
1006
Brian Osman92004802017-03-06 11:47:26 -05001007 switch (fColorMode) {
1008 case ColorMode::kLegacy:
1009 title.append(" Legacy 8888");
1010 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001011 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -05001012 title.append(" ColorManaged 8888");
1013 break;
Brian Osman03115dc2018-11-26 13:55:19 -05001014 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -05001015 title.append(" ColorManaged F16");
1016 break;
Brian Salomon8391bac2019-09-18 11:22:44 -04001017 case ColorMode::kColorManagedF16Norm:
1018 title.append(" ColorManaged F16 Norm");
1019 break;
Brian Osman92004802017-03-06 11:47:26 -05001020 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001021
Brian Osman92004802017-03-06 11:47:26 -05001022 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -05001023 int curPrimaries = -1;
1024 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1025 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1026 curPrimaries = i;
1027 break;
1028 }
1029 }
Brian Osman03115dc2018-11-26 13:55:19 -05001030 title.appendf(" %s Gamma %f",
1031 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001032 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001033 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001034
Ben Wagner37c54032018-04-13 14:30:23 -04001035 const DisplayParams& params = fWindow->getRequestedDisplayParams();
Ben Wagnerae4bb982020-09-24 14:49:00 -04001036 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001037 switch (params.fSurfaceProps.pixelGeometry()) {
1038 case kUnknown_SkPixelGeometry:
1039 title.append( " Flat");
1040 break;
1041 case kRGB_H_SkPixelGeometry:
1042 title.append( " RGB");
1043 break;
1044 case kBGR_H_SkPixelGeometry:
1045 title.append( " BGR");
1046 break;
1047 case kRGB_V_SkPixelGeometry:
1048 title.append( " RGBV");
1049 break;
1050 case kBGR_V_SkPixelGeometry:
1051 title.append( " BGRV");
1052 break;
1053 }
1054 }
1055
1056 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1057 title.append(" DFT");
1058 }
1059
csmartdalton578f0642017-02-24 16:04:47 -07001060 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001061 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001062 int msaa = fWindow->sampleCount();
1063 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001064 title.appendf(" MSAA: %i", msaa);
1065 }
1066 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001067
1068 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001069 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001070 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1071 }
1072
Brian Osman805a7272018-05-02 15:40:20 -04001073 if (kPerspective_Real == fPerspectiveMode) {
1074 title.append(" Perpsective (Real)");
1075 } else if (kPerspective_Fake == fPerspectiveMode) {
1076 title.append(" Perspective (Fake)");
1077 }
1078
brianosman05de2162016-05-06 13:28:57 -07001079 fWindow->setTitle(title.c_str());
1080}
1081
Florin Malitaab99c342018-01-16 16:23:03 -05001082int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001083
1084 if (!FLAGS_slide.isEmpty()) {
1085 int count = fSlides.count();
1086 for (int i = 0; i < count; i++) {
1087 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001088 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001089 }
1090 }
1091
1092 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1093 this->listNames();
1094 }
1095
Florin Malitaab99c342018-01-16 16:23:03 -05001096 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001097}
1098
Florin Malitaab99c342018-01-16 16:23:03 -05001099void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001100 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001101 for (const auto& slide : fSlides) {
1102 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001103 }
1104}
1105
Florin Malitaab99c342018-01-16 16:23:03 -05001106void Viewer::setCurrentSlide(int slide) {
1107 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001108
Florin Malitaab99c342018-01-16 16:23:03 -05001109 if (slide == fCurrentSlide) {
1110 return;
1111 }
1112
1113 if (fCurrentSlide >= 0) {
1114 fSlides[fCurrentSlide]->unload();
1115 }
1116
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001117 SkScalar scaleFactor = 1.0;
1118 if (fApplyBackingScale) {
1119 scaleFactor = fWindow->scaleFactor();
1120 }
1121 fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor,
1122 SkIntToScalar(fWindow->height()) / scaleFactor);
Florin Malitaab99c342018-01-16 16:23:03 -05001123 fCurrentSlide = slide;
1124 this->setupCurrentSlide();
1125}
1126
1127void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001128 if (fCurrentSlide >= 0) {
1129 // prepare dimensions for image slides
1130 fGesture.resetTouchState();
1131 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001132
Jim Van Verth0848fb02018-01-22 13:39:30 -05001133 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1134 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1135 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001136
Jim Van Verth0848fb02018-01-22 13:39:30 -05001137 // Start with a matrix that scales the slide to the available screen space
1138 if (fWindow->scaleContentToFit()) {
1139 if (windowRect.width() > 0 && windowRect.height() > 0) {
Mike Reed2ac6ce82021-01-15 12:26:22 -05001140 fDefaultMatrix = SkMatrix::RectToRect(slideBounds, windowRect,
1141 SkMatrix::kStart_ScaleToFit);
Jim Van Verth0848fb02018-01-22 13:39:30 -05001142 }
liyuqiane46e4f02016-05-20 07:32:19 -07001143 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001144
1145 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001146 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001147
1148 this->updateTitle();
1149 this->updateUIState();
1150
1151 fStatsLayer.resetMeasurements();
1152
1153 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001154 }
jvanverthc265a922016-04-08 12:51:45 -07001155}
1156
Brian Osmanaba642c2020-02-06 12:52:25 -05001157#define MAX_ZOOM_LEVEL 8.0f
1158#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001159
jvanverth34524262016-05-04 13:49:13 -07001160void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001161 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001162 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001163 this->preTouchMatrixChanged();
1164}
Yuqian Li755778c2018-03-28 16:23:31 -04001165
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001166void Viewer::preTouchMatrixChanged() {
1167 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001168 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1169 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1170 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1171 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1172}
1173
Brian Osman805a7272018-05-02 15:40:20 -04001174SkMatrix Viewer::computePerspectiveMatrix() {
1175 SkScalar w = fWindow->width(), h = fWindow->height();
1176 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1177 SkPoint perspPts[4] = {
1178 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1179 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1180 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1181 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1182 };
1183 SkMatrix m;
1184 m.setPolyToPoly(orthoPts, perspPts, 4);
1185 return m;
1186}
1187
Yuqian Li755778c2018-03-28 16:23:31 -04001188SkMatrix Viewer::computePreTouchMatrix() {
1189 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001190
1191 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001192 if (fApplyBackingScale) {
1193 zoomScale *= fWindow->scaleFactor();
1194 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001195 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001196 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001197
1198 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1199 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001200
Brian Osman805a7272018-05-02 15:40:20 -04001201 if (kPerspective_Real == fPerspectiveMode) {
1202 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001203 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001204 }
1205
Yuqian Li755778c2018-03-28 16:23:31 -04001206 return m;
jvanverthc265a922016-04-08 12:51:45 -07001207}
1208
liyuqiand3cdbca2016-05-17 12:44:20 -07001209SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001210 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001211 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001212 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001213 return m;
jvanverthc265a922016-04-08 12:51:45 -07001214}
1215
Brian Osman621491e2017-02-28 15:45:01 -05001216void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001217 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001218 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001219 fBackendType = backendType;
1220
Robert Phillipse9229532020-06-26 10:10:49 -04001221 // The active context is going away in 'detach'
1222 for(auto& slide : fSlides) {
1223 slide->gpuTeardown();
1224 }
1225
Brian Osman621491e2017-02-28 15:45:01 -05001226 fWindow->detach();
1227
Brian Osman70d2f432017-11-08 09:54:10 -05001228#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001229 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1230 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001231 DisplayParams params = fWindow->getRequestedDisplayParams();
1232 delete fWindow;
1233 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001234
Brian Osman70d2f432017-11-08 09:54:10 -05001235 // re-register callbacks
1236 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001237 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001238 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001239 fWindow->pushLayer(&fImGuiLayer);
1240
Brian Osman70d2f432017-11-08 09:54:10 -05001241 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1242 // will still include our correct sample count. But the re-created fWindow will lose that
1243 // information. On Windows, we need to re-create the window when changing sample count,
1244 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1245 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1246 // as if we had never changed windows at all).
1247 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001248#endif
1249
Brian Osman70d2f432017-11-08 09:54:10 -05001250 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001251}
1252
Brian Osman92004802017-03-06 11:47:26 -05001253void Viewer::setColorMode(ColorMode colorMode) {
1254 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001255 this->updateTitle();
1256 fWindow->inval();
1257}
1258
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001259class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1260public:
Mike Reed3ae47332019-01-04 10:11:46 -05001261 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1262 SkFont* font, Viewer::SkFontFields* ffields)
1263 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001264 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001265 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1266 sk_sp<SkTextBlob>* cache) {
1267 bool blobWillChange = false;
1268 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001269 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1270 bool shouldDraw = this->filterFont(&filteredFont);
1271 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001272 blobWillChange = true;
1273 break;
1274 }
1275 }
1276 if (!blobWillChange) {
1277 return blob;
1278 }
1279
1280 SkTextBlobBuilder builder;
1281 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001282 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1283 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001284 if (!shouldDraw) {
1285 continue;
1286 }
1287
Mike Reed3ae47332019-01-04 10:11:46 -05001288 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001289
Ben Wagner41e40472018-09-24 13:01:54 -04001290 const SkTextBlobBuilder::RunBuffer& runBuffer
1291 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001292 ? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(),
1293 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001294 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001295 ? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(),
1296 it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001297 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001298 ? builder.allocRunTextPos(font, it.glyphCount(), it.textSize())
Ben Wagnere5736262021-02-08 16:52:08 -05001299 : it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning
Ben Wagner5d9c20e2021-02-24 11:43:07 -05001300 ? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize())
Ben Wagner41e40472018-09-24 13:01:54 -04001301 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1302 uint32_t glyphCount = it.glyphCount();
1303 if (it.glyphs()) {
1304 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1305 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1306 }
1307 if (it.pos()) {
1308 size_t posSize = sizeof(decltype(*it.pos()));
Ben Wagnere5736262021-02-08 16:52:08 -05001309 unsigned posPerGlyph = it.scalarsPerGlyph();
1310 memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize);
Ben Wagner41e40472018-09-24 13:01:54 -04001311 }
1312 if (it.text()) {
1313 size_t textSize = sizeof(decltype(*it.text()));
1314 uint32_t textCount = it.textSize();
1315 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1316 }
1317 if (it.clusters()) {
1318 size_t clusterSize = sizeof(decltype(*it.clusters()));
1319 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1320 }
1321 }
1322 *cache = builder.make();
1323 return cache->get();
1324 }
1325 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1326 const SkPaint& paint) override {
1327 sk_sp<SkTextBlob> cache;
1328 this->SkPaintFilterCanvas::onDrawTextBlob(
1329 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1330 }
Mike Reed3ae47332019-01-04 10:11:46 -05001331 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001332 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001333 font->writable()->setSize(fFont->getSize());
1334 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001335 if (fFontOverrides->fScaleX) {
1336 font->writable()->setScaleX(fFont->getScaleX());
1337 }
1338 if (fFontOverrides->fSkewX) {
1339 font->writable()->setSkewX(fFont->getSkewX());
1340 }
Mike Reed3ae47332019-01-04 10:11:46 -05001341 if (fFontOverrides->fHinting) {
1342 font->writable()->setHinting(fFont->getHinting());
1343 }
Ben Wagner9613e452019-01-23 10:34:59 -05001344 if (fFontOverrides->fEdging) {
1345 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001346 }
Ben Wagner9613e452019-01-23 10:34:59 -05001347 if (fFontOverrides->fEmbolden) {
1348 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001349 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001350 if (fFontOverrides->fBaselineSnap) {
1351 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1352 }
Ben Wagner9613e452019-01-23 10:34:59 -05001353 if (fFontOverrides->fLinearMetrics) {
1354 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001355 }
Ben Wagner9613e452019-01-23 10:34:59 -05001356 if (fFontOverrides->fSubpixel) {
1357 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001358 }
Ben Wagner9613e452019-01-23 10:34:59 -05001359 if (fFontOverrides->fEmbeddedBitmaps) {
1360 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001361 }
Ben Wagner9613e452019-01-23 10:34:59 -05001362 if (fFontOverrides->fForceAutoHinting) {
1363 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001364 }
Ben Wagner9613e452019-01-23 10:34:59 -05001365
Mike Reed3ae47332019-01-04 10:11:46 -05001366 return true;
1367 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001368 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001369 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001370 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001371 }
Ben Wagner9613e452019-01-23 10:34:59 -05001372 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001373 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001374 }
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05001375 if (fPaintOverrides->fStyle) {
1376 paint.setStyle(fPaint->getStyle());
1377 }
1378 if (fPaintOverrides->fWidth) {
1379 paint.setStrokeWidth(fPaint->getStrokeWidth());
1380 }
1381 if (fPaintOverrides->fMiterLimit) {
1382 paint.setStrokeMiter(fPaint->getStrokeMiter());
1383 }
1384 if (fPaintOverrides->fCapType) {
1385 paint.setStrokeCap(fPaint->getStrokeCap());
1386 }
1387 if (fPaintOverrides->fJoinType) {
1388 paint.setStrokeJoin(fPaint->getStrokeJoin());
1389 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001390 return true;
1391 }
1392 SkPaint* fPaint;
1393 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001394 SkFont* fFont;
1395 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001396};
1397
Robert Phillips9882dae2019-03-04 11:00:10 -05001398void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001399 if (fCurrentSlide < 0) {
1400 return;
1401 }
1402
Robert Phillips9882dae2019-03-04 11:00:10 -05001403 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001404
Brian Osmanf750fbc2017-02-08 10:47:28 -05001405 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001406 SkSurface* slideSurface = surface;
1407 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001408 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001409
Brian Osmane0d4fba2017-03-15 10:24:55 -04001410 // 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 -05001411 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001412 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001413 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001414 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001415 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001416 }
1417
Brian Osman3ac99cf2017-12-01 11:23:53 -05001418 if (fSaveToSKP) {
1419 SkPictureRecorder recorder;
1420 SkCanvas* recorderCanvas = recorder.beginRecording(
1421 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001422 fSlides[fCurrentSlide]->draw(recorderCanvas);
1423 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1424 SkFILEWStream stream("sample_app.skp");
1425 picture->serialize(&stream);
1426 fSaveToSKP = false;
1427 }
1428
Brian Osmane9ed0f02018-11-26 14:50:05 -05001429 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001430 SkColorType colorType;
1431 switch (fColorMode) {
1432 case ColorMode::kLegacy:
1433 case ColorMode::kColorManaged8888:
1434 colorType = kN32_SkColorType;
1435 break;
1436 case ColorMode::kColorManagedF16:
1437 colorType = kRGBA_F16_SkColorType;
1438 break;
1439 case ColorMode::kColorManagedF16Norm:
1440 colorType = kRGBA_F16Norm_SkColorType;
1441 break;
1442 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001443
1444 auto make_surface = [=](int w, int h) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001445 SkSurfaceProps props(fWindow->getRequestedDisplayParams().fSurfaceProps);
Robert Phillips9882dae2019-03-04 11:00:10 -05001446 slideCanvas->getProps(&props);
1447
Brian Osmane9ed0f02018-11-26 14:50:05 -05001448 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1449 return Window::kRaster_BackendType == this->fBackendType
1450 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001451 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001452 };
1453
Brian Osman03115dc2018-11-26 13:55:19 -05001454 // We need to render offscreen if we're...
1455 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1456 // ... in any raster mode, because the window surface is actually GL
1457 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001458 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001459 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001460 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001461 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001462 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001463 colorSpace != nullptr ||
1464 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001465
Brian Osmane9ed0f02018-11-26 14:50:05 -05001466 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001467 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001468 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001469 }
1470
Mike Reed59295352020-03-12 13:56:34 -04001471 SkPictureRecorder recorder;
1472 SkCanvas* recorderRestoreCanvas = nullptr;
1473 if (fDrawViaSerialize) {
1474 recorderRestoreCanvas = slideCanvas;
1475 slideCanvas = recorder.beginRecording(
1476 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1477 }
1478
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001479 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001480 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001481 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001482 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001483 if (fTiled) {
1484 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1485 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001486 for (int y = 0; y < fWindow->height(); y += tileH) {
1487 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001488 SkAutoCanvasRestore acr(slideCanvas, true);
1489 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1490 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001491 }
1492 }
1493
1494 // Draw borders between tiles
1495 if (fDrawTileBoundaries) {
1496 SkPaint border;
1497 border.setColor(0x60FF00FF);
1498 border.setStyle(SkPaint::kStroke_Style);
1499 for (int y = 0; y < fWindow->height(); y += tileH) {
1500 for (int x = 0; x < fWindow->width(); x += tileW) {
1501 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1502 }
1503 }
1504 }
1505 } else {
1506 slideCanvas->concat(this->computeMatrix());
1507 if (kPerspective_Real == fPerspectiveMode) {
1508 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1509 }
Mike Reed3ae47332019-01-04 10:11:46 -05001510 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001511 fSlides[fCurrentSlide]->draw(&filterCanvas);
1512 }
Brian Osman56a24812017-12-19 11:15:16 -05001513 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001514 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001515
Mike Reed59295352020-03-12 13:56:34 -04001516 if (recorderRestoreCanvas) {
1517 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1518 auto data = picture->serialize();
1519 slideCanvas = recorderRestoreCanvas;
1520 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1521 }
1522
Brian Osman1df161a2017-02-09 12:10:20 -05001523 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001524 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001525 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001526 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001527
1528 // If we rendered offscreen, snap an image and push the results to the window's canvas
1529 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001530 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001531
Robert Phillips9882dae2019-03-04 11:00:10 -05001532 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001533 SkPaint paint;
1534 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb339d052021-01-28 11:20:41 -05001535 SkSamplingOptions sampling;
Brian Osman805a7272018-05-02 15:40:20 -04001536 int prePerspectiveCount = canvas->save();
1537 if (kPerspective_Fake == fPerspectiveMode) {
Mike Reedb339d052021-01-28 11:20:41 -05001538 sampling = SkSamplingOptions({1.0f/3, 1.0f/3});
Brian Osman805a7272018-05-02 15:40:20 -04001539 canvas->clear(SK_ColorWHITE);
1540 canvas->concat(this->computePerspectiveMatrix());
1541 }
Mike Reedb339d052021-01-28 11:20:41 -05001542 canvas->drawImage(fLastImage, 0, 0, sampling, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001543 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001544 }
Mike Reed376d8122019-03-14 11:39:02 -04001545
1546 if (fShowSlideDimensions) {
1547 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1548 SkPaint paint;
1549 paint.setColor(0x40FFFF00);
1550 surface->getCanvas()->drawRect(r, paint);
1551 }
liyuqian6f163d22016-06-13 12:26:45 -07001552}
1553
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001554void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001555 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001556 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001557}
Jim Van Verth6f449692017-02-14 15:16:46 -05001558
Robert Phillips9882dae2019-03-04 11:00:10 -05001559void Viewer::onPaint(SkSurface* surface) {
1560 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001561
Robert Phillips9882dae2019-03-04 11:00:10 -05001562 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001563
Brian Osmand67e5182017-12-08 16:46:09 -05001564 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001565
Greg Daniel427d8eb2020-09-28 15:04:18 -04001566 fLastImage.reset();
1567
Robert Phillipsed653392020-07-10 13:55:21 -04001568 if (auto direct = fWindow->directContext()) {
Chris Dalton89305752018-11-01 10:52:34 -06001569 // Clean out cache items that haven't been used in more than 10 seconds.
Robert Phillipsed653392020-07-10 13:55:21 -04001570 direct->performDeferredCleanup(std::chrono::seconds(10));
Chris Dalton89305752018-11-01 10:52:34 -06001571 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001572}
1573
Ben Wagnera1915972018-08-09 15:06:19 -04001574void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001575 if (fCurrentSlide >= 0) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001576 SkScalar scaleFactor = 1.0;
1577 if (fApplyBackingScale) {
1578 scaleFactor = fWindow->scaleFactor();
1579 }
1580 fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor);
Jim Van Verthb35c6552018-08-13 10:42:17 -04001581 }
Ben Wagnera1915972018-08-09 15:06:19 -04001582}
1583
Florin Malitacefc1b92018-02-19 21:43:47 -05001584SkPoint Viewer::mapEvent(float x, float y) {
1585 const auto m = this->computeMatrix();
1586 SkMatrix inv;
1587
1588 SkAssertResult(m.invert(&inv));
1589
1590 return inv.mapXY(x, y);
1591}
1592
Hal Canaryb1f411a2019-08-29 10:39:22 -04001593bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001594 if (GestureDevice::kMouse == fGestureDevice) {
1595 return false;
1596 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001597
1598 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001599 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001600 fWindow->inval();
1601 return true;
1602 }
1603
liyuqiand3cdbca2016-05-17 12:44:20 -07001604 void* castedOwner = reinterpret_cast<void*>(owner);
1605 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001606 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001607 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001608#if defined(SK_BUILD_FOR_IOS)
1609 // TODO: move IOS swipe detection higher up into the platform code
1610 SkPoint dir;
1611 if (fGesture.isFling(&dir)) {
1612 // swiping left or right
1613 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1614 if (dir.fX < 0) {
1615 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1616 fCurrentSlide + 1 : 0);
1617 } else {
1618 this->setCurrentSlide(fCurrentSlide > 0 ?
1619 fCurrentSlide - 1 : fSlides.count() - 1);
1620 }
1621 }
1622 fGesture.reset();
1623 }
1624#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001625 break;
1626 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001627 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001628 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001629 break;
1630 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001631 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001632 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001633 break;
1634 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001635 default: {
1636 // kLeft and kRight are only for swipes
1637 SkASSERT(false);
1638 break;
1639 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001640 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001641 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001642 fWindow->inval();
1643 return true;
1644}
1645
Hal Canaryb1f411a2019-08-29 10:39:22 -04001646bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001647 if (GestureDevice::kTouch == fGestureDevice) {
1648 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001649 }
Brian Osman16c81a12017-12-20 11:58:34 -05001650
Florin Malitacefc1b92018-02-19 21:43:47 -05001651 const auto slidePt = this->mapEvent(x, y);
1652 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1653 fWindow->inval();
1654 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001655 }
1656
1657 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001658 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001659 fGesture.touchEnd(nullptr);
1660 break;
1661 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001662 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001663 fGesture.touchBegin(nullptr, x, y);
1664 break;
1665 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001666 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001667 fGesture.touchMoved(nullptr, x, y);
1668 break;
1669 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001670 default: {
1671 SkASSERT(false); // shouldn't see kRight or kLeft here
1672 break;
1673 }
Brian Osman16c81a12017-12-20 11:58:34 -05001674 }
1675 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1676
Hal Canaryb1f411a2019-08-29 10:39:22 -04001677 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001678 fWindow->inval();
1679 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001680 return true;
1681}
1682
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001683bool Viewer::onFling(skui::InputState state) {
1684 if (skui::InputState::kRight == state) {
1685 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1686 return true;
1687 } else if (skui::InputState::kLeft == state) {
1688 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1689 return true;
1690 }
1691 return false;
1692}
1693
1694bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1695 switch (state) {
1696 case skui::InputState::kDown:
1697 fGesture.startZoom();
1698 return true;
1699 break;
1700 case skui::InputState::kMove:
1701 fGesture.updateZoom(scale, x, y, x, y);
1702 return true;
1703 break;
1704 case skui::InputState::kUp:
1705 fGesture.endZoom();
1706 return true;
1707 break;
1708 default:
1709 SkASSERT(false);
1710 break;
1711 }
1712
1713 return false;
1714}
1715
Brian Osmana109e392017-02-24 09:49:14 -05001716static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001717 // The gamut image covers a (0.8 x 0.9) shaped region
1718 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001719
1720 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1721 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1722 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001723 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1724 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1725 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001726
Brian Osman535c5e32019-02-09 16:32:58 -05001727 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1728 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1729 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1730 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1731 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001732}
1733
Ben Wagner3627d2e2018-06-26 14:23:20 -04001734static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001735 ImGui::DragCanvas dc(pt);
1736 dc.fillColor(IM_COL32(0, 0, 0, 128));
1737 dc.dragPoint(pt);
1738 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001739}
1740
Brian Osman9bb47cf2018-04-26 15:55:00 -04001741static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001742 ImGui::DragCanvas dc(pts);
1743 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001744
Brian Osman535c5e32019-02-09 16:32:58 -05001745 for (int i = 0; i < 4; ++i) {
1746 dc.dragPoint(pts + i);
1747 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001748
Brian Osman535c5e32019-02-09 16:32:58 -05001749 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1750 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1751 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1752 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001753
Brian Osman535c5e32019-02-09 16:32:58 -05001754 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001755}
1756
John Stiles38b7d2f2020-06-24 12:13:31 -04001757static SkSL::String build_sksl_highlight_shader() {
1758 return SkSL::String("out half4 sk_FragColor;\n"
1759 "void main() { sk_FragColor = half4(1, 0, 1, 0.5); }");
1760}
1761
1762static SkSL::String build_metal_highlight_shader(const SkSL::String& inShader) {
1763 // Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute
1764 // here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor.
1765 size_t pos = inShader.rfind("return *_out;\n");
1766 if (pos == std::string::npos) {
1767 return inShader;
1768 }
1769
1770 SkSL::String replacementShader = inShader;
1771 replacementShader.insert(pos, "_out->sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); ");
1772 return replacementShader;
1773}
1774
1775static SkSL::String build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) {
1776 const char* versionDecl = shaderCaps.versionDeclString();
1777 SkSL::String highlight = versionDecl ? versionDecl : "";
1778 if (shaderCaps.usesPrecisionModifiers()) {
1779 highlight.append("precision mediump float;\n");
1780 }
1781 highlight.appendf("out vec4 sk_FragColor;\n"
1782 "void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }");
1783 return highlight;
1784}
1785
Brian Osmand67e5182017-12-08 16:46:09 -05001786void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001787 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1788 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001789 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001790 }
1791
1792 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001793 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1794 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001795 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001796 DisplayParams params = fWindow->getRequestedDisplayParams();
1797 bool paramsChanged = false;
Robert Phillipsed653392020-07-10 13:55:21 -04001798 auto ctx = fWindow->directContext();
Brian Osman0b8bb882019-04-12 11:47:19 -04001799
Brian Osmana109e392017-02-24 09:49:14 -05001800 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1801 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001802 if (ImGui::CollapsingHeader("Backend")) {
1803 int newBackend = static_cast<int>(fBackendType);
1804 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1805 ImGui::SameLine();
1806 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001807#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1808 ImGui::SameLine();
1809 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1810#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001811#if defined(SK_DAWN)
1812 ImGui::SameLine();
1813 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1814#endif
John Stilesf7da9232020-11-19 19:58:14 -05001815#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC)
Brian Osman621491e2017-02-28 15:45:01 -05001816 ImGui::SameLine();
1817 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1818#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001819#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001820 ImGui::SameLine();
1821 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1822#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001823#if defined(SK_DIRECT3D)
1824 ImGui::SameLine();
1825 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1826#endif
Brian Osman621491e2017-02-28 15:45:01 -05001827 if (newBackend != fBackendType) {
1828 fDeferredActions.push_back([=]() {
1829 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1830 });
1831 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001832
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001833 bool* wire = &params.fGrContextOptions.fWireframeMode;
1834 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1835 paramsChanged = true;
1836 }
Brian Salomon99a33902017-03-07 15:16:34 -05001837
Brian Salomon91216d52021-04-09 11:57:59 -04001838 bool* reducedShaders = &params.fGrContextOptions.fReducedShaderVariations;
1839 if (ctx && ImGui::Checkbox("Reduced shaders", reducedShaders)) {
1840 paramsChanged = true;
1841 }
1842
Brian Osman28b12522017-03-08 17:10:24 -05001843 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001844 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001845 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001846 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1847 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1848 1;
1849
1850 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1851 if (maxMSAA >= 4) {
1852 ImGui::Text("MSAA: ");
1853
1854 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1855 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1856 // don't show it in the list.
1857 if (curMSAA == 2) {
1858 continue;
1859 }
1860 ImGui::SameLine();
1861 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1862 &sampleCount, curMSAA);
1863 }
1864 }
Brian Osman28b12522017-03-08 17:10:24 -05001865
1866 if (sampleCount != params.fMSAASampleCount) {
1867 params.fMSAASampleCount = sampleCount;
1868 paramsChanged = true;
1869 }
1870 }
1871
Ben Wagner37c54032018-04-13 14:30:23 -04001872 int pixelGeometryIdx = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -04001873 if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) {
Ben Wagner37c54032018-04-13 14:30:23 -04001874 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1875 }
1876 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1877 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1878 {
1879 uint32_t flags = params.fSurfaceProps.flags();
1880 if (pixelGeometryIdx == 0) {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001881 fDisplayOverrides.fSurfaceProps.fPixelGeometry = false;
1882 SkPixelGeometry pixelGeometry = fDisplay.fSurfaceProps.pixelGeometry();
1883 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
Ben Wagner37c54032018-04-13 14:30:23 -04001884 } else {
Ben Wagnerae4bb982020-09-24 14:49:00 -04001885 fDisplayOverrides.fSurfaceProps.fPixelGeometry = true;
Ben Wagner37c54032018-04-13 14:30:23 -04001886 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1887 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1888 }
1889 paramsChanged = true;
1890 }
1891
1892 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1893 if (ImGui::Checkbox("DFT", &useDFT)) {
1894 uint32_t flags = params.fSurfaceProps.flags();
1895 if (useDFT) {
1896 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1897 } else {
1898 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1899 }
1900 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1901 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1902 paramsChanged = true;
1903 }
1904
Brian Osman8a9de3d2017-03-01 14:59:05 -05001905 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001906 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001907 auto prButton = [&](GpuPathRenderers x) {
1908 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001909 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1910 params.fGrContextOptions.fGpuPathRenderers = x;
1911 paramsChanged = true;
1912 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001913 }
1914 };
1915
1916 if (!ctx) {
1917 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001918 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001919 const auto* caps = ctx->priv().caps();
1920 prButton(GpuPathRenderers::kDefault);
1921 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07001922 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001923 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001924 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001925 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001926 if (1 == fWindow->sampleCount()) {
1927 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1928 prButton(GpuPathRenderers::kCoverageCounting);
1929 }
1930 prButton(GpuPathRenderers::kSmall);
1931 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001932 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001933 prButton(GpuPathRenderers::kNone);
1934 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001935 ImGui::TreePop();
1936 }
Brian Osman621491e2017-02-28 15:45:01 -05001937 }
1938
Ben Wagner964571d2019-03-08 12:35:06 -05001939 if (ImGui::CollapsingHeader("Tiling")) {
1940 ImGui::Checkbox("Enable", &fTiled);
1941 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1942 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1943 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1944 }
1945
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001946 if (ImGui::CollapsingHeader("Transform")) {
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -05001947 if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) {
1948 this->preTouchMatrixChanged();
1949 this->onResize(fWindow->width(), fWindow->height());
1950 paramsChanged = true;
1951 }
1952
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001953 float zoom = fZoomLevel;
1954 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1955 fZoomLevel = zoom;
1956 this->preTouchMatrixChanged();
1957 paramsChanged = true;
1958 }
1959 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001960 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001961 fRotation = deg;
1962 this->preTouchMatrixChanged();
1963 paramsChanged = true;
1964 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001965 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1966 if (ImGui_DragLocation(&fOffset)) {
1967 this->preTouchMatrixChanged();
1968 paramsChanged = true;
1969 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001970 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1971 this->preTouchMatrixChanged();
1972 paramsChanged = true;
1973 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001974 }
Brian Osman805a7272018-05-02 15:40:20 -04001975 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1976 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1977 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001978 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001979 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001980 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001981 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001982 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001983 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001984 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001985 }
1986
Ben Wagnera580fb32018-04-17 11:16:32 -04001987 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001988 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001989 if (fPaintOverrides.fAntiAlias) {
1990 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001991 }
1992 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001993 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001994 {
1995 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1996 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001997 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001998 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1999 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002000 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002001 fPaintOverrides.fAntiAlias = true;
2002 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04002003 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05002004 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04002005 case SkPaintFields::AntiAliasState::Alias:
2006 break;
2007 case SkPaintFields::AntiAliasState::Normal:
2008 break;
2009 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
2010 gSkUseAnalyticAA = true;
2011 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04002012 break;
2013 case SkPaintFields::AntiAliasState::AnalyticAAForced:
2014 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04002015 break;
2016 }
2017 }
2018 paramsChanged = true;
2019 }
2020
Ben Wagner99a78dc2018-05-09 18:23:51 -04002021 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05002022 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002023 bool (SkPaint::* isFlag)() const,
2024 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04002025 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002026 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05002027 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04002028 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04002029 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04002030 if (ImGui::Combo(label, &itemIndex, items)) {
2031 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05002032 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002033 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05002034 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04002035 (fPaint.*setFlag)(itemIndex == 2);
2036 }
2037 paramsChanged = true;
2038 }
2039 };
Ben Wagnera580fb32018-04-17 11:16:32 -04002040
Ben Wagner99a78dc2018-05-09 18:23:51 -04002041 paintFlag("Dither",
2042 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05002043 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04002044 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerf5cbbc62021-02-08 22:02:14 -05002045
2046 int styleIdx = 0;
2047 if (fPaintOverrides.fStyle) {
2048 styleIdx = SkTo<int>(fPaint.getStyle()) + 1;
2049 }
2050 if (ImGui::Combo("Style", &styleIdx,
2051 "Default\0Fill\0Stroke\0Stroke and Fill\0\0"))
2052 {
2053 if (styleIdx == 0) {
2054 fPaintOverrides.fStyle = false;
2055 fPaint.setStyle(SkPaint::kFill_Style);
2056 } else {
2057 fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1));
2058 fPaintOverrides.fStyle = true;
2059 }
2060 paramsChanged = true;
2061 }
2062
2063 ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fWidth);
2064 if (fPaintOverrides.fWidth) {
2065 float width = fPaint.getStrokeWidth();
2066 if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) {
2067 fPaint.setStrokeWidth(width);
2068 paramsChanged = true;
2069 }
2070 }
2071
2072 ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit);
2073 if (fPaintOverrides.fMiterLimit) {
2074 float miterLimit = fPaint.getStrokeMiter();
2075 if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) {
2076 fPaint.setStrokeMiter(miterLimit);
2077 paramsChanged = true;
2078 }
2079 }
2080
2081 int capIdx = 0;
2082 if (fPaintOverrides.fCapType) {
2083 capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1;
2084 }
2085 if (ImGui::Combo("Cap Type", &capIdx,
2086 "Default\0Butt\0Round\0Square\0\0"))
2087 {
2088 if (capIdx == 0) {
2089 fPaintOverrides.fCapType = false;
2090 fPaint.setStrokeCap(SkPaint::kDefault_Cap);
2091 } else {
2092 fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1));
2093 fPaintOverrides.fCapType = true;
2094 }
2095 paramsChanged = true;
2096 }
2097
2098 int joinIdx = 0;
2099 if (fPaintOverrides.fJoinType) {
2100 joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1;
2101 }
2102 if (ImGui::Combo("Join Type", &joinIdx,
2103 "Default\0Miter\0Round\0Bevel\0\0"))
2104 {
2105 if (joinIdx == 0) {
2106 fPaintOverrides.fJoinType = false;
2107 fPaint.setStrokeJoin(SkPaint::kDefault_Join);
2108 } else {
2109 fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1));
2110 fPaintOverrides.fJoinType = true;
2111 }
2112 paramsChanged = true;
2113 }
Ben Wagner9613e452019-01-23 10:34:59 -05002114 }
Hal Canary02738a82019-01-21 18:51:32 +00002115
Ben Wagner9613e452019-01-23 10:34:59 -05002116 if (ImGui::CollapsingHeader("Font")) {
2117 int hintingIdx = 0;
2118 if (fFontOverrides.fHinting) {
2119 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
2120 }
2121 if (ImGui::Combo("Hinting", &hintingIdx,
2122 "Default\0None\0Slight\0Normal\0Full\0\0"))
2123 {
2124 if (hintingIdx == 0) {
2125 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04002126 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05002127 } else {
2128 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
2129 fFontOverrides.fHinting = true;
2130 }
2131 paramsChanged = true;
2132 }
Hal Canary02738a82019-01-21 18:51:32 +00002133
Ben Wagner9613e452019-01-23 10:34:59 -05002134 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
2135 bool SkFontFields::* flag,
2136 bool (SkFont::* isFlag)() const,
2137 void (SkFont::* setFlag)(bool) )
2138 {
2139 int itemIndex = 0;
2140 if (fFontOverrides.*flag) {
2141 itemIndex = (fFont.*isFlag)() ? 2 : 1;
2142 }
2143 if (ImGui::Combo(label, &itemIndex, items)) {
2144 if (itemIndex == 0) {
2145 fFontOverrides.*flag = false;
2146 } else {
2147 fFontOverrides.*flag = true;
2148 (fFont.*setFlag)(itemIndex == 2);
2149 }
2150 paramsChanged = true;
2151 }
2152 };
Hal Canary02738a82019-01-21 18:51:32 +00002153
Ben Wagner9613e452019-01-23 10:34:59 -05002154 fontFlag("Fake Bold Glyphs",
2155 "Default\0No Fake Bold\0Fake Bold\0\0",
2156 &SkFontFields::fEmbolden,
2157 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002158
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002159 fontFlag("Baseline Snapping",
2160 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2161 &SkFontFields::fBaselineSnap,
2162 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2163
Ben Wagner9613e452019-01-23 10:34:59 -05002164 fontFlag("Linear Text",
2165 "Default\0No Linear Text\0Linear Text\0\0",
2166 &SkFontFields::fLinearMetrics,
2167 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002168
Ben Wagner9613e452019-01-23 10:34:59 -05002169 fontFlag("Subpixel Position Glyphs",
2170 "Default\0Pixel Text\0Subpixel Text\0\0",
2171 &SkFontFields::fSubpixel,
2172 &SkFont::isSubpixel, &SkFont::setSubpixel);
2173
2174 fontFlag("Embedded Bitmap Text",
2175 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2176 &SkFontFields::fEmbeddedBitmaps,
2177 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2178
2179 fontFlag("Force Auto-Hinting",
2180 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2181 &SkFontFields::fForceAutoHinting,
2182 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2183
2184 int edgingIdx = 0;
2185 if (fFontOverrides.fEdging) {
2186 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2187 }
2188 if (ImGui::Combo("Edging", &edgingIdx,
2189 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2190 {
2191 if (edgingIdx == 0) {
2192 fFontOverrides.fEdging = false;
2193 fFont.setEdging(SkFont::Edging::kAlias);
2194 } else {
2195 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2196 fFontOverrides.fEdging = true;
2197 }
2198 paramsChanged = true;
2199 }
2200
Ben Wagner15a8d572019-03-21 13:35:44 -04002201 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2202 if (fFontOverrides.fSize) {
2203 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002204 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002205 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002206 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002207 fFontOverrides.fSizeRange[0],
2208 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002209 "%.6f", 2.0f))
2210 {
Mike Reed3ae47332019-01-04 10:11:46 -05002211 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002212 paramsChanged = true;
2213 }
2214 }
2215
2216 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2217 if (fFontOverrides.fScaleX) {
2218 float scaleX = fFont.getScaleX();
2219 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2220 fFont.setScaleX(scaleX);
2221 paramsChanged = true;
2222 }
2223 }
2224
2225 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2226 if (fFontOverrides.fSkewX) {
2227 float skewX = fFont.getSkewX();
2228 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2229 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002230 paramsChanged = true;
2231 }
2232 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002233 }
2234
Mike Reed81f60ec2018-05-15 10:09:52 -04002235 {
2236 SkMetaData controls;
2237 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2238 if (ImGui::CollapsingHeader("Current Slide")) {
2239 SkMetaData::Iter iter(controls);
2240 const char* name;
2241 SkMetaData::Type type;
2242 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002243 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002244 if (type == SkMetaData::kScalar_Type) {
2245 float val[3];
2246 SkASSERT(count == 3);
2247 controls.findScalars(name, &count, val);
2248 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2249 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002250 }
Ben Wagner110c7032019-03-22 17:03:59 -04002251 } else if (type == SkMetaData::kBool_Type) {
2252 bool val;
2253 SkASSERT(count == 1);
2254 controls.findBool(name, &val);
2255 if (ImGui::Checkbox(name, &val)) {
2256 controls.setBool(name, val);
2257 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002258 }
2259 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002260 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002261 }
2262 }
2263 }
2264
Ben Wagner7a3c6742018-04-23 10:01:07 -04002265 if (fShowSlidePicker) {
2266 ImGui::SetNextTreeNodeOpen(true);
2267 }
Brian Osman79086b92017-02-10 13:36:16 -05002268 if (ImGui::CollapsingHeader("Slide")) {
2269 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002270 static ImVector<const char*> filteredSlideNames;
2271 static ImVector<int> filteredSlideIndices;
2272
Brian Osmanfce09c52017-11-14 15:32:20 -05002273 if (fShowSlidePicker) {
2274 ImGui::SetKeyboardFocusHere();
2275 fShowSlidePicker = false;
2276 }
2277
Brian Osman79086b92017-02-10 13:36:16 -05002278 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002279 filteredSlideNames.clear();
2280 filteredSlideIndices.clear();
2281 int filteredIndex = 0;
2282 for (int i = 0; i < fSlides.count(); ++i) {
2283 const char* slideName = fSlides[i]->getName().c_str();
2284 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2285 if (i == fCurrentSlide) {
2286 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002287 }
Brian Osmanf479e422017-11-08 13:11:36 -05002288 filteredSlideNames.push_back(slideName);
2289 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002290 }
Brian Osman79086b92017-02-10 13:36:16 -05002291 }
Brian Osmanf479e422017-11-08 13:11:36 -05002292
Brian Osmanf479e422017-11-08 13:11:36 -05002293 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2294 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002295 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002296 }
2297 }
Brian Osmana109e392017-02-24 09:49:14 -05002298
2299 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002300 ColorMode newMode = fColorMode;
2301 auto cmButton = [&](ColorMode mode, const char* label) {
2302 if (ImGui::RadioButton(label, mode == fColorMode)) {
2303 newMode = mode;
2304 }
2305 };
2306
2307 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002308 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2309 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002310 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002311
2312 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002313 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002314 }
2315
2316 // Pick from common gamuts:
2317 int primariesIdx = 4; // Default: Custom
2318 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2319 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2320 primariesIdx = i;
2321 break;
2322 }
2323 }
2324
Brian Osman03115dc2018-11-26 13:55:19 -05002325 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002326 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002327
Brian Osmana109e392017-02-24 09:49:14 -05002328 if (ImGui::Combo("Primaries", &primariesIdx,
2329 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2330 if (primariesIdx >= 0 && primariesIdx <= 3) {
2331 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2332 }
2333 }
2334
2335 // Allow direct editing of gamut
2336 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2337 }
Brian Osman207d4102019-01-10 09:40:58 -05002338
2339 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002340 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002341 if (ImGui::Checkbox("Pause", &isPaused)) {
2342 fAnimTimer.togglePauseResume();
2343 }
Brian Osman707d2022019-01-10 11:27:34 -05002344
2345 float speed = fAnimTimer.getSpeed();
2346 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2347 fAnimTimer.setSpeed(speed);
2348 }
Brian Osman207d4102019-01-10 09:40:58 -05002349 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002350
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002351 if (ImGui::CollapsingHeader("Shaders")) {
2352 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2353 GrContextOptions::ShaderCacheStrategy::kSkSL;
John Stiles7247b482021-03-08 10:40:35 -05002354
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002355#if defined(SK_VULKAN)
2356 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2357#else
2358 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002359#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002360
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002361 // To re-load shaders from the currently active programs, we flush all
2362 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002363 static bool gLoadPending = false;
2364 if (gLoadPending) {
2365 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
Brian Osmanf0de96f2021-02-26 13:54:11 -05002366 const SkString& description, int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002367 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002368 entry.fKey = key;
2369 SkMD5 hash;
2370 hash.write(key->bytes(), key->size());
2371 SkMD5::Digest digest = hash.finish();
2372 for (int i = 0; i < 16; ++i) {
2373 entry.fKeyString.appendf("%02x", digest.data[i]);
2374 }
Brian Osmanf0de96f2021-02-26 13:54:11 -05002375 entry.fKeyDescription = description;
Brian Osman0b8bb882019-04-12 11:47:19 -04002376
Brian Osman9e4e4c72020-06-10 07:19:34 -04002377 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002378 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002379 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2380 entry.fInputs,
2381 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002382 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002383 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002384 fPersistentCache.foreach(collectShaders);
2385 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002386
2387#if defined(SK_VULKAN)
2388 if (isVulkan && !sksl) {
2389 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2390 for (auto& entry : fCachedShaders) {
2391 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2392 const SkSL::String& spirv(entry.fShader[i]);
2393 std::string disasm;
2394 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2395 &disasm);
2396 entry.fShader[i].assign(disasm);
2397 }
2398 }
2399 }
2400#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002401 }
2402
John Stilesa8f6b6f2021-03-05 16:00:42 -05002403 // Defer actually doing the View/Apply logic so that we can trigger an Apply when we
Brian Osman0b8bb882019-04-12 11:47:19 -04002404 // start or finish hovering on a tree node in the list below:
John Stilesa8f6b6f2021-03-05 16:00:42 -05002405 bool doView = ImGui::Button("View"); ImGui::SameLine();
2406 bool doApply = ImGui::Button("Apply Changes"); ImGui::SameLine();
John Stiles7247b482021-03-08 10:40:35 -05002407 bool doDump = ImGui::Button("Dump SkSL to resources/sksl/");
John Stilesa8f6b6f2021-03-05 16:00:42 -05002408
John Stiles2ee4d7a2021-03-30 10:30:47 -04002409 int newOptLevel = fOptLevel;
John Stiles7247b482021-03-08 10:40:35 -05002410 ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source);
2411 ImGui::SameLine();
2412 ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile);
2413 ImGui::SameLine();
2414 ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize);
2415 ImGui::SameLine();
2416 ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline);
John Stilesa8f6b6f2021-03-05 16:00:42 -05002417
John Stiles7247b482021-03-08 10:40:35 -05002418 // If we are changing the compile mode, we want to reset the cache and redo
2419 // everything.
John Stiles2ee4d7a2021-03-30 10:30:47 -04002420 if (doDump || newOptLevel != fOptLevel) {
John Stiles7247b482021-03-08 10:40:35 -05002421 sksl = doDump || (newOptLevel == kShaderOptLevel_Source);
John Stiles2ee4d7a2021-03-30 10:30:47 -04002422 fOptLevel = (ShaderOptLevel)newOptLevel;
2423 switch (fOptLevel) {
2424 case kShaderOptLevel_Source:
2425 Compiler::EnableOptimizer(OverrideFlag::kDefault);
2426 Compiler::EnableInliner(OverrideFlag::kDefault);
2427 break;
2428 case kShaderOptLevel_Compile:
2429 Compiler::EnableOptimizer(OverrideFlag::kOff);
2430 Compiler::EnableInliner(OverrideFlag::kOff);
2431 break;
2432 case kShaderOptLevel_Optimize:
2433 Compiler::EnableOptimizer(OverrideFlag::kOn);
2434 Compiler::EnableInliner(OverrideFlag::kOff);
2435 break;
2436 case kShaderOptLevel_Inline:
2437 Compiler::EnableOptimizer(OverrideFlag::kOn);
2438 Compiler::EnableInliner(OverrideFlag::kOn);
2439 break;
2440 }
John Stiles7247b482021-03-08 10:40:35 -05002441
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002442 params.fGrContextOptions.fShaderCacheStrategy =
2443 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2444 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2445 paramsChanged = true;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002446 doView = true;
2447
2448 fDeferredActions.push_back([=]() {
2449 // Reset the cache.
2450 fPersistentCache.reset();
2451 // Dump the cache once we have drawn a frame with it.
2452 if (doDump) {
2453 fDeferredActions.push_back([this]() {
2454 this->dumpShadersToResources();
2455 });
2456 }
2457 });
Brian Osmancbc33b82019-04-19 14:16:19 -04002458 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002459
2460 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002461 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002462 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2463 bool hovered = ImGui::IsItemHovered();
2464 if (hovered != entry.fHovered) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002465 // Force an Apply to patch the highlight shader in/out
Brian Osman0b8bb882019-04-12 11:47:19 -04002466 entry.fHovered = hovered;
John Stilesa8f6b6f2021-03-05 16:00:42 -05002467 doApply = true;
Brian Osman0b8bb882019-04-12 11:47:19 -04002468 }
2469 if (inTreeNode) {
Brian Osmaneb3fb902020-08-18 13:16:59 -04002470 auto stringBox = [](const char* label, std::string* str) {
2471 // Full width, and not too much space for each shader
2472 int lines = std::count(str->begin(), str->end(), '\n') + 2;
2473 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30));
2474 ImGui::InputTextMultiline(label, str, boxSize);
2475 };
Brian Osmanf0de96f2021-02-26 13:54:11 -05002476 if (ImGui::TreeNode("Key")) {
2477 ImGui::TextWrapped("%s", entry.fKeyDescription.c_str());
2478 ImGui::TreePop();
2479 }
Brian Osmaneb3fb902020-08-18 13:16:59 -04002480 stringBox("##VP", &entry.fShader[kVertex_GrShaderType]);
2481 stringBox("##FP", &entry.fShader[kFragment_GrShaderType]);
Brian Osman0b8bb882019-04-12 11:47:19 -04002482 ImGui::TreePop();
2483 }
2484 }
2485 ImGui::EndChild();
2486
John Stilesa8f6b6f2021-03-05 16:00:42 -05002487 if (doView) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002488 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002489 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osman0b8bb882019-04-12 11:47:19 -04002490 gLoadPending = true;
2491 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002492
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002493 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2494 // but I'm not sure anyone wants to do that.
2495 if (isVulkan && !sksl) {
John Stilesa8f6b6f2021-03-05 16:00:42 -05002496 doApply = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002497 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002498 if (doApply) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002499 fPersistentCache.reset();
Robert Phillipsed653392020-07-10 13:55:21 -04002500 ctx->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002501 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002502 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
John Stiles38b7d2f2020-06-24 12:13:31 -04002503 if (entry.fHovered) {
2504 // The hovered item (if any) gets a special shader to make it
2505 // identifiable.
2506 SkSL::String& fragShader = entry.fShader[kFragment_GrShaderType];
2507 switch (entry.fShaderType) {
2508 case SkSetFourByteTag('S', 'K', 'S', 'L'): {
2509 fragShader = build_sksl_highlight_shader();
2510 break;
2511 }
2512 case SkSetFourByteTag('G', 'L', 'S', 'L'): {
2513 fragShader = build_glsl_highlight_shader(
2514 *ctx->priv().caps()->shaderCaps());
2515 break;
2516 }
2517 case SkSetFourByteTag('M', 'S', 'L', ' '): {
2518 fragShader = build_metal_highlight_shader(fragShader);
2519 break;
2520 }
2521 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002522 }
2523
Brian Osmana085a412019-04-25 09:44:43 -04002524 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2525 entry.fShader,
2526 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002527 kGrShaderTypeCount);
Brian Osmanf0de96f2021-02-26 13:54:11 -05002528 fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription);
Brian Osman0b8bb882019-04-12 11:47:19 -04002529
2530 entry.fShader[kFragment_GrShaderType] = backup;
2531 }
2532 }
2533 }
Brian Osman79086b92017-02-10 13:36:16 -05002534 }
Brian Salomon99a33902017-03-07 15:16:34 -05002535 if (paramsChanged) {
2536 fDeferredActions.push_back([=]() {
2537 fWindow->setRequestedDisplayParams(params);
2538 fWindow->inval();
2539 this->updateTitle();
2540 });
2541 }
Brian Osman79086b92017-02-10 13:36:16 -05002542 ImGui::End();
2543 }
2544
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002545 if (gShaderErrorHandler.fErrors.count()) {
2546 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Osman31890e22020-07-10 16:48:14 -04002547 ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002548 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2549 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002550 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2551 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2552 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2553 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002554 }
2555 ImGui::End();
2556 gShaderErrorHandler.reset();
2557 }
2558
Brian Osmanf6877092017-02-13 09:39:57 -05002559 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002560 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2561 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002562 static int zoomFactor = 8;
2563 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002564 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002565 }
2566 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2567 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002568 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002569 }
Brian Osmanf6877092017-02-13 09:39:57 -05002570
Ben Wagner3627d2e2018-06-26 14:23:20 -04002571 if (!fZoomWindowFixed) {
2572 ImVec2 mousePos = ImGui::GetMousePos();
2573 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2574 }
2575 SkScalar x = fZoomWindowLocation.x();
2576 SkScalar y = fZoomWindowLocation.y();
2577 int xInt = SkScalarRoundToInt(x);
2578 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002579 ImVec2 avail = ImGui::GetContentRegionAvail();
2580
Brian Osmanead517d2017-11-13 15:36:36 -05002581 uint32_t pixel = 0;
2582 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04002583 auto dContext = fWindow->directContext();
2584 if (fLastImage->readPixels(dContext, info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002585 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002586 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002587 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002588 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002589 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2590 }
2591
Greg Danielfbc60b72020-11-03 17:27:02 -05002592 fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002593 // Translate so the region of the image that's under the mouse cursor is centered
2594 // in the zoom canvas:
2595 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002596 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2597 avail.y * 0.5f / zoomFactor - y - 0.5f);
Greg Danielfbc60b72020-11-03 17:27:02 -05002598 c->drawImage(lastImage, 0, 0);
Brian Osmanead517d2017-11-13 15:36:36 -05002599
2600 SkPaint outline;
2601 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002602 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002603 });
Brian Osmanf6877092017-02-13 09:39:57 -05002604 }
2605
2606 ImGui::End();
2607 }
Brian Osman79086b92017-02-10 13:36:16 -05002608}
2609
John Stilesa8f6b6f2021-03-05 16:00:42 -05002610void Viewer::dumpShadersToResources() {
2611 // Sort the list of cached shaders so we can maintain some minimal level of consistency.
2612 // It doesn't really matter, but it will keep files from switching places unpredictably.
2613 std::vector<const CachedShader*> shaders;
2614 shaders.reserve(fCachedShaders.size());
2615 for (const CachedShader& shader : fCachedShaders) {
2616 shaders.push_back(&shader);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002617 }
John Stilesa8f6b6f2021-03-05 16:00:42 -05002618
2619 std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) {
2620 return std::tie(a->fShader[kFragment_GrShaderType], a->fShader[kVertex_GrShaderType]) <
2621 std::tie(b->fShader[kFragment_GrShaderType], b->fShader[kVertex_GrShaderType]);
2622 });
2623
2624 // Make the resources/sksl/SlideName/ directory.
2625 SkString directory = SkStringPrintf("%ssksl/%s",
2626 GetResourcePath().c_str(),
2627 fSlides[fCurrentSlide]->getName().c_str());
2628 if (!sk_mkdir(directory.c_str())) {
2629 SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str());
2630 return;
2631 }
2632
2633 int index = 0;
2634 for (const auto& entry : shaders) {
2635 SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index);
2636 FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag);
2637 if (vertFile) {
2638 const SkSL::String& vertText = entry->fShader[kVertex_GrShaderType];
2639 SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile));
2640 sk_fclose(vertFile);
2641 } else {
2642 SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str());
2643 }
2644
2645 SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index);
2646 FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag);
2647 if (fragFile) {
2648 const SkSL::String& fragText = entry->fShader[kFragment_GrShaderType];
2649 SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile));
2650 sk_fclose(fragFile);
2651 } else {
2652 SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str());
2653 }
2654
2655 ++index;
2656 }
2657}
2658
2659void Viewer::onIdle() {
2660 SkTArray<std::function<void()>> actionsToRun;
2661 actionsToRun.swap(fDeferredActions);
2662
2663 for (const auto& fn : actionsToRun) {
2664 fn();
2665 }
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002666
Brian Osman56a24812017-12-19 11:15:16 -05002667 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002668 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002669 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002670 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002671
Brian Osman79086b92017-02-10 13:36:16 -05002672 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002673 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2674 // not be visible, though. So we need to redraw if there is at least one visible window, or
2675 // more than one active window. Newly created windows are active but not visible for one frame
2676 // while they determine their layout and sizing.
2677 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2678 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002679 fWindow->inval();
2680 }
jvanverth9f372462016-04-06 06:08:59 -07002681}
liyuqiane5a6cd92016-05-27 08:52:52 -07002682
Florin Malitab632df72018-06-18 21:23:06 -04002683template <typename OptionsFunc>
2684static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2685 OptionsFunc&& optionsFunc) {
2686 writer.beginObject();
2687 {
2688 writer.appendString(kName , name);
2689 writer.appendString(kValue, value);
2690
2691 writer.beginArray(kOptions);
2692 {
2693 optionsFunc(writer);
2694 }
2695 writer.endArray();
2696 }
2697 writer.endObject();
2698}
2699
2700
liyuqiane5a6cd92016-05-27 08:52:52 -07002701void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002702 if (!fWindow) {
2703 return;
2704 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002705 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002706 return; // Surface hasn't been created yet.
2707 }
2708
Florin Malitab632df72018-06-18 21:23:06 -04002709 SkDynamicMemoryWStream memStream;
2710 SkJSONWriter writer(&memStream);
2711 writer.beginArray();
2712
liyuqianb73c24b2016-06-03 08:47:23 -07002713 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002714 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2715 [this](SkJSONWriter& writer) {
2716 for(const auto& slide : fSlides) {
2717 writer.appendString(slide->getName().c_str());
2718 }
2719 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002720
liyuqianb73c24b2016-06-03 08:47:23 -07002721 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002722 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2723 [](SkJSONWriter& writer) {
2724 for (const auto& str : kBackendTypeStrings) {
2725 writer.appendString(str);
2726 }
2727 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002728
csmartdalton578f0642017-02-24 16:04:47 -07002729 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002730 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2731 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2732 [this](SkJSONWriter& writer) {
2733 writer.appendS32(0);
2734
2735 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2736 return;
2737 }
2738
2739 for (int msaa : {4, 8, 16}) {
2740 writer.appendS32(msaa);
2741 }
2742 });
csmartdalton578f0642017-02-24 16:04:47 -07002743
csmartdalton61cd31a2017-02-27 17:00:53 -07002744 // Path renderer state
2745 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002746 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2747 [this](SkJSONWriter& writer) {
Robert Phillipsed653392020-07-10 13:55:21 -04002748 auto ctx = fWindow->directContext();
Florin Malitab632df72018-06-18 21:23:06 -04002749 if (!ctx) {
2750 writer.appendString("Software");
2751 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002752 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002753 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2754 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonff18ff62020-12-07 17:39:26 -07002755 if (GrTessellationPathRenderer::IsSupported(*caps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002756 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002757 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002758 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002759 }
2760 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002761 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2762 writer.appendString(
2763 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2764 }
2765 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2766 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002767 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002768 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002769 }
2770 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002771
liyuqianb73c24b2016-06-03 08:47:23 -07002772 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002773 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2774 [this](SkJSONWriter& writer) {
2775 writer.appendString(kSoftkeyHint);
2776 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2777 writer.appendString(softkey.c_str());
2778 }
2779 });
liyuqianb73c24b2016-06-03 08:47:23 -07002780
Florin Malitab632df72018-06-18 21:23:06 -04002781 writer.endArray();
2782 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002783
Florin Malitab632df72018-06-18 21:23:06 -04002784 auto data = memStream.detachAsData();
2785
2786 // TODO: would be cool to avoid this copy
2787 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2788
2789 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002790}
2791
2792void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002793 // For those who will add more features to handle the state change in this function:
2794 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2795 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2796 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002797 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002798 for (int i = 0; i < fSlides.count(); ++i) {
2799 if (fSlides[i]->getName().equals(stateValue)) {
2800 this->setCurrentSlide(i);
2801 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002802 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002803 }
Florin Malitaab99c342018-01-16 16:23:03 -05002804
2805 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002806 } else if (stateName.equals(kBackendStateName)) {
2807 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2808 if (stateValue.equals(kBackendTypeStrings[i])) {
2809 if (fBackendType != i) {
2810 fBackendType = (sk_app::Window::BackendType)i;
Robert Phillipse9229532020-06-26 10:10:49 -04002811 for(auto& slide : fSlides) {
2812 slide->gpuTeardown();
2813 }
liyuqian6cb70252016-06-02 12:16:25 -07002814 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002815 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002816 }
2817 break;
2818 }
2819 }
csmartdalton578f0642017-02-24 16:04:47 -07002820 } else if (stateName.equals(kMSAAStateName)) {
2821 DisplayParams params = fWindow->getRequestedDisplayParams();
2822 int sampleCount = atoi(stateValue.c_str());
2823 if (sampleCount != params.fMSAASampleCount) {
2824 params.fMSAASampleCount = sampleCount;
2825 fWindow->setRequestedDisplayParams(params);
2826 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002827 this->updateTitle();
2828 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002829 }
2830 } else if (stateName.equals(kPathRendererStateName)) {
2831 DisplayParams params = fWindow->getRequestedDisplayParams();
2832 for (const auto& pair : gPathRendererNames) {
2833 if (pair.second == stateValue.c_str()) {
2834 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2835 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2836 fWindow->setRequestedDisplayParams(params);
2837 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002838 this->updateTitle();
2839 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002840 }
2841 break;
2842 }
csmartdalton578f0642017-02-24 16:04:47 -07002843 }
liyuqianb73c24b2016-06-03 08:47:23 -07002844 } else if (stateName.equals(kSoftkeyStateName)) {
2845 if (!stateValue.equals(kSoftkeyHint)) {
2846 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002847 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002848 }
liyuqian2edb0f42016-07-06 14:11:32 -07002849 } else if (stateName.equals(kRefreshStateName)) {
2850 // This state is actually NOT in the UI state.
2851 // We use this to allow Android to quickly set bool fRefresh.
2852 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002853 } else {
2854 SkDebugf("Unknown stateName: %s", stateName.c_str());
2855 }
2856}
Brian Osman79086b92017-02-10 13:36:16 -05002857
Hal Canaryb1f411a2019-08-29 10:39:22 -04002858bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002859 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002860}
2861
Hal Canaryb1f411a2019-08-29 10:39:22 -04002862bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002863 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002864 fWindow->inval();
2865 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002866 } else {
2867 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002868 }
Brian Osman79086b92017-02-10 13:36:16 -05002869}