blob: 973171aed63c529c34335c0937f013fb3aec714f [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkGraphics.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkSurface.h"
14#include "include/gpu/GrContext.h"
15#include "include/private/SkTo.h"
16#include "include/utils/SkPaintFilterCanvas.h"
17#include "src/core/SkColorSpacePriv.h"
18#include "src/core/SkImagePriv.h"
19#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkOSFile.h"
21#include "src/core/SkScan.h"
22#include "src/core/SkTaskGroup.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040023#include "src/core/SkTextBlobPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrGpu.h"
26#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070027#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
29#include "src/utils/SkJSONWriter.h"
30#include "src/utils/SkOSPath.h"
31#include "tools/Resources.h"
32#include "tools/ToolUtils.h"
33#include "tools/flags/CommandLineFlags.h"
34#include "tools/flags/CommonFlags.h"
35#include "tools/trace/EventTracingPriv.h"
36#include "tools/viewer/BisectSlide.h"
37#include "tools/viewer/GMSlide.h"
38#include "tools/viewer/ImageSlide.h"
39#include "tools/viewer/ParticlesSlide.h"
40#include "tools/viewer/SKPSlide.h"
41#include "tools/viewer/SampleSlide.h"
Brian Osmand927bd22019-12-18 11:23:12 -050042#include "tools/viewer/SkSLSlide.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "tools/viewer/SlideDir.h"
44#include "tools/viewer/SvgSlide.h"
45#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070046
Chris Dalton17dc4182020-03-25 16:18:16 -060047#include <cstdlib>
Hal Canaryc640d0d2018-06-13 09:59:02 -040048#include <map>
49
Hal Canary8a001442018-09-19 11:31:27 -040050#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040051#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040052
Brian Osmanc85f1fa2020-06-16 15:11:34 -040053#ifdef SK_VULKAN
54#include "spirv-tools/libspirv.hpp"
55#endif
56
Florin Malita87ccf332018-05-04 12:23:24 -040057#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050058 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040059#endif
Florin Malita45cd2002020-06-09 14:00:54 -040060#if defined(SK_ENABLE_SKRIVE)
61 #include "tools/viewer/SkRiveSlide.h"
62#endif
Florin Malita87ccf332018-05-04 12:23:24 -040063
Brian Osman5e7fbfd2019-05-03 13:13:35 -040064class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
65public:
66 void compileError(const char* shader, const char* errors) override {
67 fShaders.push_back(SkString(shader));
68 fErrors.push_back(SkString(errors));
69 }
70
71 void reset() {
72 fShaders.reset();
73 fErrors.reset();
74 }
75
76 SkTArray<SkString> fShaders;
77 SkTArray<SkString> fErrors;
78};
79
80static CapturingShaderErrorHandler gShaderErrorHandler;
81
Brian Osmanf847f312020-06-18 14:18:27 -040082GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; }
83
jvanverth34524262016-05-04 13:49:13 -070084using namespace sk_app;
85
csmartdalton61cd31a2017-02-27 17:00:53 -070086static std::map<GpuPathRenderers, std::string> gPathRendererNames;
87
jvanverth9f372462016-04-06 06:08:59 -070088Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070089 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070090}
91
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060092static DEFINE_string(slide, "", "Start on this sample.");
93static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050094
Jim Van Verth682a2f42020-05-13 16:54:09 -040095#ifdef SK_GL
96#define GL_BACKEND_STR ", \"gl\""
bsalomon6c471f72016-07-26 12:56:32 -070097#else
Jim Van Verth682a2f42020-05-13 16:54:09 -040098#define GL_BACKEND_STR
bsalomon6c471f72016-07-26 12:56:32 -070099#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400100#ifdef SK_VULKAN
101#define VK_BACKEND_STR ", \"vk\""
102#else
103#define VK_BACKEND_STR
104#endif
105#ifdef SK_METAL
106#define MTL_BACKEND_STR ", \"mtl\""
107#else
108#define MTL_BACKEND_STR
109#endif
110#ifdef SK_DIRECT3D
111#define D3D_BACKEND_STR ", \"d3d\""
112#else
113#define D3D_BACKEND_STR
114#endif
115#ifdef SK_DAWN
116#define DAWN_BACKEND_STR ", \"dawn\""
117#else
118#define DAWN_BACKEND_STR
119#endif
120#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn
121#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \
122 "\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR)
bsalomon6c471f72016-07-26 12:56:32 -0700123
Brian Osman2dd96932016-10-18 15:33:53 -0400124static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700125
Mike Klein5b3f3432019-03-21 11:42:21 -0500126static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -0700127
Mike Klein84836b72019-03-21 11:31:36 -0500128static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700129
Mike Klein84836b72019-03-21 11:31:36 -0500130static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400131
Mike Kleinc6142d82019-03-25 10:54:59 -0500132static DEFINE_string2(match, m, nullptr,
133 "[~][^]substring[$] [...] of name to run.\n"
134 "Multiple matches may be separated by spaces.\n"
135 "~ causes a matching name to always be skipped\n"
136 "^ requires the start of the name to match\n"
137 "$ requires the end of the name to match\n"
138 "^ and $ requires an exact match\n"
139 "If a name does not match any list entry,\n"
140 "it is skipped unless some list entry starts with ~");
141
Mike Klein19fb3972019-03-21 13:08:08 -0500142#if defined(SK_BUILD_FOR_ANDROID)
143 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500144 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
145 static DEFINE_string(lotties, "/data/local/tmp/lotties",
146 "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400147 static DEFINE_string(rives, "/data/local/tmp/rives",
148 "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500149#else
150 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500151 static DEFINE_string(skps, "skps", "Directory to read skps from.");
152 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita45cd2002020-06-09 14:00:54 -0400153 static DEFINE_string(rives, "rives", "Directory to read Rive (Flare) files from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500154#endif
155
Mike Kleinc6142d82019-03-25 10:54:59 -0500156static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
157
158static DEFINE_int_2(threads, j, -1,
159 "Run threadsafe tests on a threadpool with this many extra threads, "
160 "defaulting to one extra thread per core.");
161
Jim Van Verth7b558182019-11-14 16:47:01 -0500162static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
163
Chris Daltonc8877332020-01-06 09:48:30 -0700164static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface.");
Mike Reed862818b2020-03-21 15:07:13 -0400165static DEFINE_bool(skvm, false, "Try to use skvm blitters for raster.");
Mike Klein1e0884d2020-04-28 15:04:16 -0500166static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Kleine42af162020-04-29 07:55:53 -0500167static DEFINE_bool(stats, false, "Display stats overlay on startup.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500168
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400169#ifndef SK_GL
170static_assert(false, "viewer requires GL backend for raster.")
171#endif
172
Brian Salomon194db172017-08-17 14:37:06 -0400173const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700174 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400175#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
176 "ANGLE",
177#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400178#ifdef SK_DAWN
179 "Dawn",
180#endif
jvanverth063ece72016-06-17 09:29:14 -0700181#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700182 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700183#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400184#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500185 "Metal",
186#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400187#ifdef SK_DIRECT3D
188 "Direct3D",
189#endif
csmartdalton578f0642017-02-24 16:04:47 -0700190 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700191};
192
bsalomon6c471f72016-07-26 12:56:32 -0700193static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400194#ifdef SK_DAWN
195 if (0 == strcmp(str, "dawn")) {
196 return sk_app::Window::kDawn_BackendType;
197 } else
198#endif
bsalomon6c471f72016-07-26 12:56:32 -0700199#ifdef SK_VULKAN
200 if (0 == strcmp(str, "vk")) {
201 return sk_app::Window::kVulkan_BackendType;
202 } else
203#endif
Brian Salomon194db172017-08-17 14:37:06 -0400204#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
205 if (0 == strcmp(str, "angle")) {
206 return sk_app::Window::kANGLE_BackendType;
207 } else
208#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400209#ifdef SK_METAL
210 if (0 == strcmp(str, "mtl")) {
211 return sk_app::Window::kMetal_BackendType;
212 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500213#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -0400214#ifdef SK_DIRECT3D
215 if (0 == strcmp(str, "d3d")) {
216 return sk_app::Window::kDirect3D_BackendType;
217 } else
218#endif
219
bsalomon6c471f72016-07-26 12:56:32 -0700220 if (0 == strcmp(str, "gl")) {
221 return sk_app::Window::kNativeGL_BackendType;
222 } else if (0 == strcmp(str, "sw")) {
223 return sk_app::Window::kRaster_BackendType;
224 } else {
225 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
226 return sk_app::Window::kRaster_BackendType;
227 }
228}
229
Brian Osmana109e392017-02-24 09:49:14 -0500230static SkColorSpacePrimaries gSrgbPrimaries = {
231 0.64f, 0.33f,
232 0.30f, 0.60f,
233 0.15f, 0.06f,
234 0.3127f, 0.3290f };
235
236static SkColorSpacePrimaries gAdobePrimaries = {
237 0.64f, 0.33f,
238 0.21f, 0.71f,
239 0.15f, 0.06f,
240 0.3127f, 0.3290f };
241
242static SkColorSpacePrimaries gP3Primaries = {
243 0.680f, 0.320f,
244 0.265f, 0.690f,
245 0.150f, 0.060f,
246 0.3127f, 0.3290f };
247
248static SkColorSpacePrimaries gRec2020Primaries = {
249 0.708f, 0.292f,
250 0.170f, 0.797f,
251 0.131f, 0.046f,
252 0.3127f, 0.3290f };
253
254struct NamedPrimaries {
255 const char* fName;
256 SkColorSpacePrimaries* fPrimaries;
257} gNamedPrimaries[] = {
258 { "sRGB", &gSrgbPrimaries },
259 { "AdobeRGB", &gAdobePrimaries },
260 { "P3", &gP3Primaries },
261 { "Rec. 2020", &gRec2020Primaries },
262};
263
264static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
265 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
266}
267
Brian Osman70d2f432017-11-08 09:54:10 -0500268static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
269 // In raster mode, we still use GL for the window.
270 // This lets us render the GUI faster (and correct).
271 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
272}
273
Jim Van Verth74826c82019-03-01 14:37:30 -0500274class NullSlide : public Slide {
275 SkISize getDimensions() const override {
276 return SkISize::Make(640, 480);
277 }
278
279 void draw(SkCanvas* canvas) override {
280 canvas->clear(0xffff11ff);
281 }
282};
283
John Stiles31964fd2020-05-05 16:05:47 -0400284static const char kName[] = "name";
285static const char kValue[] = "value";
286static const char kOptions[] = "options";
287static const char kSlideStateName[] = "Slide";
288static const char kBackendStateName[] = "Backend";
289static const char kMSAAStateName[] = "MSAA";
290static const char kPathRendererStateName[] = "Path renderer";
291static const char kSoftkeyStateName[] = "Softkey";
292static const char kSoftkeyHint[] = "Please select a softkey";
293static const char kON[] = "ON";
294static const char kRefreshStateName[] = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700295
Mike Reed862818b2020-03-21 15:07:13 -0400296extern bool gUseSkVMBlitter;
Mike Klein1e0884d2020-04-28 15:04:16 -0500297extern bool gSkVMJITViaDylib;
Mike Reed862818b2020-03-21 15:07:13 -0400298
jvanverth34524262016-05-04 13:49:13 -0700299Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500300 : fCurrentSlide(-1)
301 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500302 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400303 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500304 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500305 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500306 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500307 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400308 , fZoomWindowFixed(false)
309 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500310 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400311 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700312 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500313 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500314 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500315 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500316 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700317 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400318 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400319 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400320 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500321 , fTiled(false)
322 , fDrawTileBoundaries(false)
323 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400324 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700325{
Greg Daniel285db442016-10-14 09:12:53 -0400326 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700327
Chris Dalton37ae4b02019-12-28 14:51:11 -0700328 gPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers";
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600329 gPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500330 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500331 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600332 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Chris Dalton17dc4182020-03-25 16:18:16 -0600333 gPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500334 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700335
jvanverth2bb3b6d2016-04-08 07:24:09 -0700336 SkDebugf("Command line arguments: ");
337 for (int i = 1; i < argc; ++i) {
338 SkDebugf("%s ", argv[i]);
339 }
340 SkDebugf("\n");
341
Mike Klein88544fb2019-03-20 10:50:33 -0500342 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500343#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400344 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500345#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700346
Mike Reed862818b2020-03-21 15:07:13 -0400347 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein1e0884d2020-04-28 15:04:16 -0500348 gSkVMJITViaDylib = FLAGS_dylib;
Mike Reed862818b2020-03-21 15:07:13 -0400349
Mike Klein19cc0f62019-03-22 15:30:07 -0500350 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500351
Brian Osmanbc8150f2017-07-24 11:38:01 -0400352 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400353 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400354
bsalomon6c471f72016-07-26 12:56:32 -0700355 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700356 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700357
csmartdalton578f0642017-02-24 16:04:47 -0700358 DisplayParams displayParams;
359 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700360 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400361 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400362 displayParams.fGrContextOptions.fShaderCacheStrategy =
363 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400364 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
365 displayParams.fGrContextOptions.fSuppressPrints = true;
csmartdalton578f0642017-02-24 16:04:47 -0700366 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500367 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700368
Brian Osman56a24812017-12-19 11:15:16 -0500369 // Configure timers
Mike Kleine42af162020-04-29 07:55:53 -0500370 fStatsLayer.setActive(FLAGS_stats);
Brian Osman56a24812017-12-19 11:15:16 -0500371 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
372 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
373 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
374
jvanverth9f372462016-04-06 06:08:59 -0700375 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700376 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500377 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500378 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500379 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700380
brianosman622c8d52016-05-10 06:50:49 -0700381 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500382 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
383 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
384 fWindow->inval();
385 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500386 // Command to jump directly to the slide picker and give it focus
387 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
388 this->fShowImGuiDebugWindow = true;
389 this->fShowSlidePicker = true;
390 fWindow->inval();
391 });
392 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400393 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500394 this->fShowImGuiDebugWindow = true;
395 this->fShowSlidePicker = true;
396 fWindow->inval();
397 });
Brian Osman79086b92017-02-10 13:36:16 -0500398 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
399 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
400 fWindow->inval();
401 });
Brian Osmanf6877092017-02-13 09:39:57 -0500402 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
403 this->fShowZoomWindow = !this->fShowZoomWindow;
404 fWindow->inval();
405 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400406 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
407 this->fZoomWindowFixed = !this->fZoomWindowFixed;
408 fWindow->inval();
409 });
Greg Danield0794cc2019-03-27 16:23:26 -0400410 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
411 DisplayParams params = fWindow->getRequestedDisplayParams();
412 params.fDisableVsync = !params.fDisableVsync;
413 fWindow->setRequestedDisplayParams(params);
414 this->updateTitle();
415 fWindow->inval();
416 });
Mike Reedf702ed42019-07-22 17:00:49 -0400417 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
418 fRefresh = !fRefresh;
419 fWindow->inval();
420 });
brianosman622c8d52016-05-10 06:50:49 -0700421 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500422 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700423 fWindow->inval();
424 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400425 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500426 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400427 this->updateTitle();
428 fWindow->inval();
429 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500430 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500431 switch (fColorMode) {
432 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500433 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500434 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500435 case ColorMode::kColorManaged8888:
436 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500437 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500438 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400439 this->setColorMode(ColorMode::kColorManagedF16Norm);
440 break;
441 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500442 this->setColorMode(ColorMode::kLegacy);
443 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500444 }
brianosman622c8d52016-05-10 06:50:49 -0700445 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700446 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
447 DisplayParams params = fWindow->getRequestedDisplayParams();
448 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
449 fWindow->setRequestedDisplayParams(params);
450 fWindow->inval();
451 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400452 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500453 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700454 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400455 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500456 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700457 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400458 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700459 this->changeZoomLevel(1.f / 32.f);
460 fWindow->inval();
461 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400462 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700463 this->changeZoomLevel(-1.f / 32.f);
464 fWindow->inval();
465 });
jvanverthaf236b52016-05-20 06:01:06 -0700466 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400467 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
468 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500469 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400470#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
471 if (newBackend == sk_app::Window::kVulkan_BackendType) {
472 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
473 sk_app::Window::kBackendTypeCount);
474 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
475 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500476 }
477#endif
Brian Osman621491e2017-02-28 15:45:01 -0500478 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700479 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500480 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
481 fSaveToSKP = true;
482 fWindow->inval();
483 });
Mike Reed376d8122019-03-14 11:39:02 -0400484 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
485 fShowSlideDimensions = !fShowSlideDimensions;
486 fWindow->inval();
487 });
Ben Wagner37c54032018-04-13 14:30:23 -0400488 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
489 DisplayParams params = fWindow->getRequestedDisplayParams();
490 uint32_t flags = params.fSurfaceProps.flags();
491 if (!fPixelGeometryOverrides) {
492 fPixelGeometryOverrides = true;
493 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
494 } else {
495 switch (params.fSurfaceProps.pixelGeometry()) {
496 case kUnknown_SkPixelGeometry:
497 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
498 break;
499 case kRGB_H_SkPixelGeometry:
500 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
501 break;
502 case kBGR_H_SkPixelGeometry:
503 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
504 break;
505 case kRGB_V_SkPixelGeometry:
506 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
507 break;
508 case kBGR_V_SkPixelGeometry:
509 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
510 fPixelGeometryOverrides = false;
511 break;
512 }
513 }
514 fWindow->setRequestedDisplayParams(params);
515 this->updateTitle();
516 fWindow->inval();
517 });
Ben Wagner9613e452019-01-23 10:34:59 -0500518 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500519 if (!fFontOverrides.fHinting) {
520 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400521 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500522 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500523 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400524 case SkFontHinting::kNone:
525 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500526 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400527 case SkFontHinting::kSlight:
528 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500529 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400530 case SkFontHinting::kNormal:
531 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500532 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400533 case SkFontHinting::kFull:
534 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500535 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500536 break;
537 }
538 }
539 this->updateTitle();
540 fWindow->inval();
541 });
542 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500543 if (!fPaintOverrides.fAntiAlias) {
544 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
545 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500546 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500547 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500548 } else {
549 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500550 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500551 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500552 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400553 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500554 break;
555 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500556 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500557 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400558 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500559 break;
560 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500561 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400562 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500563 break;
564 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500565 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
566 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500567 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
568 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500569 break;
570 }
571 }
572 this->updateTitle();
573 fWindow->inval();
574 });
Ben Wagner37c54032018-04-13 14:30:23 -0400575 fCommands.addCommand('D', "Modes", "DFT", [this]() {
576 DisplayParams params = fWindow->getRequestedDisplayParams();
577 uint32_t flags = params.fSurfaceProps.flags();
578 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
579 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
580 fWindow->setRequestedDisplayParams(params);
581 this->updateTitle();
582 fWindow->inval();
583 });
Ben Wagner9613e452019-01-23 10:34:59 -0500584 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500585 if (!fFontOverrides.fEdging) {
586 fFontOverrides.fEdging = true;
587 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500588 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500589 switch (fFont.getEdging()) {
590 case SkFont::Edging::kAlias:
591 fFont.setEdging(SkFont::Edging::kAntiAlias);
592 break;
593 case SkFont::Edging::kAntiAlias:
594 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
595 break;
596 case SkFont::Edging::kSubpixelAntiAlias:
597 fFont.setEdging(SkFont::Edging::kAlias);
598 fFontOverrides.fEdging = false;
599 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500600 }
601 }
602 this->updateTitle();
603 fWindow->inval();
604 });
Ben Wagner9613e452019-01-23 10:34:59 -0500605 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500606 if (!fFontOverrides.fSubpixel) {
607 fFontOverrides.fSubpixel = true;
608 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500609 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500610 if (!fFont.isSubpixel()) {
611 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500612 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500613 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500614 }
615 }
616 this->updateTitle();
617 fWindow->inval();
618 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400619 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
620 if (!fFontOverrides.fBaselineSnap) {
621 fFontOverrides.fBaselineSnap = true;
622 fFont.setBaselineSnap(false);
623 } else {
624 if (!fFont.isBaselineSnap()) {
625 fFont.setBaselineSnap(true);
626 } else {
627 fFontOverrides.fBaselineSnap = false;
628 }
629 }
630 this->updateTitle();
631 fWindow->inval();
632 });
Brian Osman805a7272018-05-02 15:40:20 -0400633 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
634 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
635 : kPerspective_Real;
636 this->updateTitle();
637 fWindow->inval();
638 });
639 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
640 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
641 : kPerspective_Off;
642 this->updateTitle();
643 fWindow->inval();
644 });
Brian Osman207d4102019-01-10 09:40:58 -0500645 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
646 fAnimTimer.togglePauseResume();
647 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400648 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
649 fZoomUI = !fZoomUI;
650 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
651 fWindow->inval();
652 });
Mike Reed59295352020-03-12 13:56:34 -0400653 fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() {
654 fDrawViaSerialize = !fDrawViaSerialize;
655 this->updateTitle();
656 fWindow->inval();
657 });
Mike Reed862818b2020-03-21 15:07:13 -0400658 fCommands.addCommand('!', "SkVM", "Toggle SkVM", [this]() {
659 gUseSkVMBlitter = !gUseSkVMBlitter;
660 this->updateTitle();
661 fWindow->inval();
662 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500663
jvanverth2bb3b6d2016-04-08 07:24:09 -0700664 // set up slides
665 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500666 if (FLAGS_list) {
667 this->listNames();
668 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700669
Brian Osman9bb47cf2018-04-26 15:55:00 -0400670 fPerspectivePoints[0].set(0, 0);
671 fPerspectivePoints[1].set(1, 0);
672 fPerspectivePoints[2].set(0, 1);
673 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700674 fAnimTimer.run();
675
Hal Canaryc465d132017-12-08 10:21:31 -0500676 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500677 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400678 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500679 }
680 fImGuiGamutPaint.setColor(SK_ColorWHITE);
681 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
682
jongdeok.kim804f17e2019-02-26 14:39:23 +0900683 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500684 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700685}
686
jvanverth34524262016-05-04 13:49:13 -0700687void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400688 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
689 static const struct {
690 const char* fExtension;
691 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500692 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400693 const SlideFactory fFactory;
694 } gExternalSlidesInfo[] = {
695 { ".skp", "skp-dir", FLAGS_skps,
696 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
697 return sk_make_sp<SKPSlide>(name, path);}
698 },
699 { ".jpg", "jpg-dir", FLAGS_jpgs,
700 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
701 return sk_make_sp<ImageSlide>(name, path);}
702 },
Florin Malita87ccf332018-05-04 12:23:24 -0400703#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400704 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400705 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
706 return sk_make_sp<SkottieSlide>(name, path);}
707 },
Florin Malita87ccf332018-05-04 12:23:24 -0400708#endif
Florin Malita45cd2002020-06-09 14:00:54 -0400709 #if defined(SK_ENABLE_SKRIVE)
710 { ".flr", "skrive-dir", FLAGS_rives,
711 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
712 return sk_make_sp<SkRiveSlide>(name, path);}
713 },
714 #endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400715#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400716 { ".svg", "svg-dir", FLAGS_svgs,
717 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
718 return sk_make_sp<SvgSlide>(name, path);}
719 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400720#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400721 };
jvanverthc265a922016-04-08 12:51:45 -0700722
Brian Salomon343553a2018-09-05 15:41:23 -0400723 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700724
Mike Klein88544fb2019-03-20 10:50:33 -0500725 const auto addSlide =
726 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
727 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
728 return;
729 }
liyuqian6f163d22016-06-13 12:26:45 -0700730
Mike Klein88544fb2019-03-20 10:50:33 -0500731 if (auto slide = fact(name, path)) {
732 dirSlides.push_back(slide);
733 fSlides.push_back(std::move(slide));
734 }
735 };
Florin Malita76a076b2018-02-15 18:40:48 -0500736
Florin Malita38792ce2018-05-08 10:36:18 -0400737 if (!FLAGS_file.isEmpty()) {
738 // single file mode
739 const SkString file(FLAGS_file[0]);
740
741 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
742 for (const auto& sinfo : gExternalSlidesInfo) {
743 if (file.endsWith(sinfo.fExtension)) {
744 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
745 return;
746 }
747 }
748
749 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
750 } else {
751 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
752 }
753
754 return;
755 }
756
757 // Bisect slide.
758 if (!FLAGS_bisect.isEmpty()) {
759 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500760 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400761 if (FLAGS_bisect.count() >= 2) {
762 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
763 bisect->onChar(*ch);
764 }
765 }
766 fSlides.push_back(std::move(bisect));
767 }
768 }
769
770 // GMs
771 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400772 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400773 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500774 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400775 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400776 fSlides.push_back(std::move(slide));
777 }
Florin Malita38792ce2018-05-08 10:36:18 -0400778 }
779 // reverse gms
780 int numGMs = fSlides.count() - firstGM;
781 for (int i = 0; i < numGMs/2; ++i) {
782 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
783 }
784
785 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400786 for (const SampleFactory factory : SampleRegistry::Range()) {
787 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500788 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400789 fSlides.push_back(slide);
790 }
Florin Malita38792ce2018-05-08 10:36:18 -0400791 }
792
Brian Osman7c979f52019-02-12 13:27:51 -0500793 // Particle demo
794 {
795 // TODO: Convert this to a sample
796 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500797 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500798 fSlides.push_back(std::move(slide));
799 }
800 }
801
Brian Osmand927bd22019-12-18 11:23:12 -0500802 // Runtime shader editor
803 {
804 sk_sp<Slide> slide(new SkSLSlide());
805 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
806 fSlides.push_back(std::move(slide));
807 }
808 }
809
Florin Malita0ffa3222018-04-05 14:34:45 -0400810 for (const auto& info : gExternalSlidesInfo) {
811 for (const auto& flag : info.fFlags) {
812 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
813 // single file
814 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
815 } else {
816 // directory
Florin Malita0ffa3222018-04-05 14:34:45 -0400817 SkString name;
Tyler Denniston31dc4812020-04-09 11:17:21 -0400818 SkTArray<SkString> sortedFilenames;
819 SkOSFile::Iter it(flag.c_str(), info.fExtension);
Florin Malita0ffa3222018-04-05 14:34:45 -0400820 while (it.next(&name)) {
Tyler Denniston31dc4812020-04-09 11:17:21 -0400821 sortedFilenames.push_back(name);
822 }
823 if (sortedFilenames.count()) {
824 SkTQSort(sortedFilenames.begin(), sortedFilenames.end() - 1,
825 [](const SkString& a, const SkString& b) {
826 return strcmp(a.c_str(), b.c_str()) < 0;
827 });
828 }
829 for (const SkString& filename : sortedFilenames) {
830 addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()),
831 info.fFactory);
Florin Malita0ffa3222018-04-05 14:34:45 -0400832 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400833 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400834 if (!dirSlides.empty()) {
835 fSlides.push_back(
836 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
837 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500838 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400839 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400840 }
841 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500842
843 if (!fSlides.count()) {
844 sk_sp<Slide> slide(new NullSlide());
845 fSlides.push_back(std::move(slide));
846 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700847}
848
849
jvanverth34524262016-05-04 13:49:13 -0700850Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700851 fWindow->detach();
852 delete fWindow;
853}
854
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500855struct SkPaintTitleUpdater {
856 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
857 void append(const char* s) {
858 if (fCount == 0) {
859 fTitle->append(" {");
860 } else {
861 fTitle->append(", ");
862 }
863 fTitle->append(s);
864 ++fCount;
865 }
866 void done() {
867 if (fCount > 0) {
868 fTitle->append("}");
869 }
870 }
871 SkString* fTitle;
872 int fCount;
873};
874
brianosman05de2162016-05-06 13:28:57 -0700875void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700876 if (!fWindow) {
877 return;
878 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500879 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700880 return; // Surface hasn't been created yet.
881 }
882
jvanverth34524262016-05-04 13:49:13 -0700883 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700884 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700885
Mike Kleine5acd752019-03-22 09:57:16 -0500886 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400887 if (gSkForceAnalyticAA) {
888 title.append(" <FAAA>");
889 } else {
890 title.append(" <AAA>");
891 }
892 }
Mike Reed59295352020-03-12 13:56:34 -0400893 if (fDrawViaSerialize) {
894 title.append(" <serialize>");
895 }
Mike Reed862818b2020-03-21 15:07:13 -0400896 if (gUseSkVMBlitter) {
897 title.append(" <skvm>");
898 }
Yuqian Li399b3c22017-08-03 11:08:15 -0400899
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500900 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500901 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
902 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400903 const char* on, const char* off)
904 {
Ben Wagner9613e452019-01-23 10:34:59 -0500905 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400906 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500907 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400908 };
909
Ben Wagner9613e452019-01-23 10:34:59 -0500910 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
911 const char* on, const char* off)
912 {
913 if (fFontOverrides.*flag) {
914 paintTitle.append((fFont.*isFlag)() ? on : off);
915 }
916 };
917
918 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
919 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500920 if (fPaintOverrides.fFilterQuality) {
921 switch (fPaint.getFilterQuality()) {
922 case kNone_SkFilterQuality:
923 paintTitle.append("NoFilter");
924 break;
925 case kLow_SkFilterQuality:
926 paintTitle.append("LowFilter");
927 break;
928 case kMedium_SkFilterQuality:
929 paintTitle.append("MediumFilter");
930 break;
931 case kHigh_SkFilterQuality:
932 paintTitle.append("HighFilter");
933 break;
934 }
935 }
Ben Wagner9613e452019-01-23 10:34:59 -0500936
937 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
938 "Force Autohint", "No Force Autohint");
939 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400940 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500941 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
942 "Linear Metrics", "Non-Linear Metrics");
943 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
944 "Bitmap Text", "No Bitmap Text");
945 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
946
947 if (fFontOverrides.fEdging) {
948 switch (fFont.getEdging()) {
949 case SkFont::Edging::kAlias:
950 paintTitle.append("Alias Text");
951 break;
952 case SkFont::Edging::kAntiAlias:
953 paintTitle.append("Antialias Text");
954 break;
955 case SkFont::Edging::kSubpixelAntiAlias:
956 paintTitle.append("Subpixel Antialias Text");
957 break;
958 }
959 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400960
Mike Reed3ae47332019-01-04 10:11:46 -0500961 if (fFontOverrides.fHinting) {
962 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400963 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500964 paintTitle.append("No Hinting");
965 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400966 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500967 paintTitle.append("Slight Hinting");
968 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400969 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500970 paintTitle.append("Normal Hinting");
971 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400972 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500973 paintTitle.append("Full Hinting");
974 break;
975 }
976 }
977 paintTitle.done();
978
Brian Osman92004802017-03-06 11:47:26 -0500979 switch (fColorMode) {
980 case ColorMode::kLegacy:
981 title.append(" Legacy 8888");
982 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500983 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500984 title.append(" ColorManaged 8888");
985 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500986 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500987 title.append(" ColorManaged F16");
988 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400989 case ColorMode::kColorManagedF16Norm:
990 title.append(" ColorManaged F16 Norm");
991 break;
Brian Osman92004802017-03-06 11:47:26 -0500992 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500993
Brian Osman92004802017-03-06 11:47:26 -0500994 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500995 int curPrimaries = -1;
996 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
997 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
998 curPrimaries = i;
999 break;
1000 }
1001 }
Brian Osman03115dc2018-11-26 13:55:19 -05001002 title.appendf(" %s Gamma %f",
1003 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -05001004 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -07001005 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001006
Ben Wagner37c54032018-04-13 14:30:23 -04001007 const DisplayParams& params = fWindow->getRequestedDisplayParams();
1008 if (fPixelGeometryOverrides) {
1009 switch (params.fSurfaceProps.pixelGeometry()) {
1010 case kUnknown_SkPixelGeometry:
1011 title.append( " Flat");
1012 break;
1013 case kRGB_H_SkPixelGeometry:
1014 title.append( " RGB");
1015 break;
1016 case kBGR_H_SkPixelGeometry:
1017 title.append( " BGR");
1018 break;
1019 case kRGB_V_SkPixelGeometry:
1020 title.append( " RGBV");
1021 break;
1022 case kBGR_V_SkPixelGeometry:
1023 title.append( " BGRV");
1024 break;
1025 }
1026 }
1027
1028 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
1029 title.append(" DFT");
1030 }
1031
csmartdalton578f0642017-02-24 16:04:47 -07001032 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -07001033 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001034 int msaa = fWindow->sampleCount();
1035 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001036 title.appendf(" MSAA: %i", msaa);
1037 }
1038 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -07001039
1040 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Dalton37ae4b02019-12-28 14:51:11 -07001041 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001042 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
1043 }
1044
Brian Osman805a7272018-05-02 15:40:20 -04001045 if (kPerspective_Real == fPerspectiveMode) {
1046 title.append(" Perpsective (Real)");
1047 } else if (kPerspective_Fake == fPerspectiveMode) {
1048 title.append(" Perspective (Fake)");
1049 }
1050
brianosman05de2162016-05-06 13:28:57 -07001051 fWindow->setTitle(title.c_str());
1052}
1053
Florin Malitaab99c342018-01-16 16:23:03 -05001054int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001055
1056 if (!FLAGS_slide.isEmpty()) {
1057 int count = fSlides.count();
1058 for (int i = 0; i < count; i++) {
1059 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -05001060 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -05001061 }
1062 }
1063
1064 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
1065 this->listNames();
1066 }
1067
Florin Malitaab99c342018-01-16 16:23:03 -05001068 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -05001069}
1070
Florin Malitaab99c342018-01-16 16:23:03 -05001071void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -05001072 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -05001073 for (const auto& slide : fSlides) {
1074 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -05001075 }
1076}
1077
Florin Malitaab99c342018-01-16 16:23:03 -05001078void Viewer::setCurrentSlide(int slide) {
1079 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -07001080
Florin Malitaab99c342018-01-16 16:23:03 -05001081 if (slide == fCurrentSlide) {
1082 return;
1083 }
1084
1085 if (fCurrentSlide >= 0) {
1086 fSlides[fCurrentSlide]->unload();
1087 }
1088
1089 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
1090 SkIntToScalar(fWindow->height()));
1091 fCurrentSlide = slide;
1092 this->setupCurrentSlide();
1093}
1094
1095void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001096 if (fCurrentSlide >= 0) {
1097 // prepare dimensions for image slides
1098 fGesture.resetTouchState();
1099 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001100
Jim Van Verth0848fb02018-01-22 13:39:30 -05001101 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1102 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1103 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001104
Jim Van Verth0848fb02018-01-22 13:39:30 -05001105 // Start with a matrix that scales the slide to the available screen space
1106 if (fWindow->scaleContentToFit()) {
1107 if (windowRect.width() > 0 && windowRect.height() > 0) {
1108 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1109 }
liyuqiane46e4f02016-05-20 07:32:19 -07001110 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001111
1112 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001113 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001114
1115 this->updateTitle();
1116 this->updateUIState();
1117
1118 fStatsLayer.resetMeasurements();
1119
1120 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001121 }
jvanverthc265a922016-04-08 12:51:45 -07001122}
1123
Brian Osmanaba642c2020-02-06 12:52:25 -05001124#define MAX_ZOOM_LEVEL 8.0f
1125#define MIN_ZOOM_LEVEL -8.0f
jvanverthc265a922016-04-08 12:51:45 -07001126
jvanverth34524262016-05-04 13:49:13 -07001127void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001128 fZoomLevel += delta;
Brian Osmanaba642c2020-02-06 12:52:25 -05001129 fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001130 this->preTouchMatrixChanged();
1131}
Yuqian Li755778c2018-03-28 16:23:31 -04001132
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001133void Viewer::preTouchMatrixChanged() {
1134 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001135 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1136 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1137 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1138 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1139}
1140
Brian Osman805a7272018-05-02 15:40:20 -04001141SkMatrix Viewer::computePerspectiveMatrix() {
1142 SkScalar w = fWindow->width(), h = fWindow->height();
1143 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1144 SkPoint perspPts[4] = {
1145 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1146 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1147 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1148 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1149 };
1150 SkMatrix m;
1151 m.setPolyToPoly(orthoPts, perspPts, 4);
1152 return m;
1153}
1154
Yuqian Li755778c2018-03-28 16:23:31 -04001155SkMatrix Viewer::computePreTouchMatrix() {
1156 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001157
1158 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001159 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001160 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001161
1162 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1163 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001164
Brian Osman805a7272018-05-02 15:40:20 -04001165 if (kPerspective_Real == fPerspectiveMode) {
1166 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001167 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001168 }
1169
Yuqian Li755778c2018-03-28 16:23:31 -04001170 return m;
jvanverthc265a922016-04-08 12:51:45 -07001171}
1172
liyuqiand3cdbca2016-05-17 12:44:20 -07001173SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001174 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001175 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001176 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001177 return m;
jvanverthc265a922016-04-08 12:51:45 -07001178}
1179
Brian Osman621491e2017-02-28 15:45:01 -05001180void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001181 fPersistentCache.reset();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04001182 fCachedShaders.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001183 fBackendType = backendType;
1184
1185 fWindow->detach();
1186
Brian Osman70d2f432017-11-08 09:54:10 -05001187#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001188 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1189 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001190 DisplayParams params = fWindow->getRequestedDisplayParams();
1191 delete fWindow;
1192 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001193
Brian Osman70d2f432017-11-08 09:54:10 -05001194 // re-register callbacks
1195 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001196 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001197 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001198 fWindow->pushLayer(&fImGuiLayer);
1199
Brian Osman70d2f432017-11-08 09:54:10 -05001200 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1201 // will still include our correct sample count. But the re-created fWindow will lose that
1202 // information. On Windows, we need to re-create the window when changing sample count,
1203 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1204 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1205 // as if we had never changed windows at all).
1206 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001207#endif
1208
Brian Osman70d2f432017-11-08 09:54:10 -05001209 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001210}
1211
Brian Osman92004802017-03-06 11:47:26 -05001212void Viewer::setColorMode(ColorMode colorMode) {
1213 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001214 this->updateTitle();
1215 fWindow->inval();
1216}
1217
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001218class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1219public:
Mike Reed3ae47332019-01-04 10:11:46 -05001220 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1221 SkFont* font, Viewer::SkFontFields* ffields)
1222 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001223 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001224 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1225 sk_sp<SkTextBlob>* cache) {
1226 bool blobWillChange = false;
1227 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001228 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1229 bool shouldDraw = this->filterFont(&filteredFont);
1230 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001231 blobWillChange = true;
1232 break;
1233 }
1234 }
1235 if (!blobWillChange) {
1236 return blob;
1237 }
1238
1239 SkTextBlobBuilder builder;
1240 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001241 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1242 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001243 if (!shouldDraw) {
1244 continue;
1245 }
1246
Mike Reed3ae47332019-01-04 10:11:46 -05001247 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001248
Ben Wagner41e40472018-09-24 13:01:54 -04001249 const SkTextBlobBuilder::RunBuffer& runBuffer
1250 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001251 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001252 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001253 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001254 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001255 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001256 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001257 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001258 it.glyphCount(), it.textSize(), SkString())
1259 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1260 uint32_t glyphCount = it.glyphCount();
1261 if (it.glyphs()) {
1262 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1263 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1264 }
1265 if (it.pos()) {
1266 size_t posSize = sizeof(decltype(*it.pos()));
1267 uint8_t positioning = it.positioning();
1268 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1269 }
1270 if (it.text()) {
1271 size_t textSize = sizeof(decltype(*it.text()));
1272 uint32_t textCount = it.textSize();
1273 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1274 }
1275 if (it.clusters()) {
1276 size_t clusterSize = sizeof(decltype(*it.clusters()));
1277 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1278 }
1279 }
1280 *cache = builder.make();
1281 return cache->get();
1282 }
1283 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1284 const SkPaint& paint) override {
1285 sk_sp<SkTextBlob> cache;
1286 this->SkPaintFilterCanvas::onDrawTextBlob(
1287 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1288 }
Mike Reed3ae47332019-01-04 10:11:46 -05001289 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001290 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001291 font->writable()->setSize(fFont->getSize());
1292 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001293 if (fFontOverrides->fScaleX) {
1294 font->writable()->setScaleX(fFont->getScaleX());
1295 }
1296 if (fFontOverrides->fSkewX) {
1297 font->writable()->setSkewX(fFont->getSkewX());
1298 }
Mike Reed3ae47332019-01-04 10:11:46 -05001299 if (fFontOverrides->fHinting) {
1300 font->writable()->setHinting(fFont->getHinting());
1301 }
Ben Wagner9613e452019-01-23 10:34:59 -05001302 if (fFontOverrides->fEdging) {
1303 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001304 }
Ben Wagner9613e452019-01-23 10:34:59 -05001305 if (fFontOverrides->fEmbolden) {
1306 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001307 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001308 if (fFontOverrides->fBaselineSnap) {
1309 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1310 }
Ben Wagner9613e452019-01-23 10:34:59 -05001311 if (fFontOverrides->fLinearMetrics) {
1312 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001313 }
Ben Wagner9613e452019-01-23 10:34:59 -05001314 if (fFontOverrides->fSubpixel) {
1315 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001316 }
Ben Wagner9613e452019-01-23 10:34:59 -05001317 if (fFontOverrides->fEmbeddedBitmaps) {
1318 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001319 }
Ben Wagner9613e452019-01-23 10:34:59 -05001320 if (fFontOverrides->fForceAutoHinting) {
1321 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001322 }
Ben Wagner9613e452019-01-23 10:34:59 -05001323
Mike Reed3ae47332019-01-04 10:11:46 -05001324 return true;
1325 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001326 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001327 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001328 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001329 }
Ben Wagner9613e452019-01-23 10:34:59 -05001330 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001331 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001332 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001333 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001334 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001335 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001336 return true;
1337 }
1338 SkPaint* fPaint;
1339 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001340 SkFont* fFont;
1341 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001342};
1343
Robert Phillips9882dae2019-03-04 11:00:10 -05001344void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001345 if (fCurrentSlide < 0) {
1346 return;
1347 }
1348
Robert Phillips9882dae2019-03-04 11:00:10 -05001349 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001350
Brian Osmanf750fbc2017-02-08 10:47:28 -05001351 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001352 SkSurface* slideSurface = surface;
1353 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001354 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001355
Brian Osmane0d4fba2017-03-15 10:24:55 -04001356 // 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 -05001357 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001358 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001359 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001360 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001361 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001362 }
1363
Brian Osman3ac99cf2017-12-01 11:23:53 -05001364 if (fSaveToSKP) {
1365 SkPictureRecorder recorder;
1366 SkCanvas* recorderCanvas = recorder.beginRecording(
1367 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001368 fSlides[fCurrentSlide]->draw(recorderCanvas);
1369 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1370 SkFILEWStream stream("sample_app.skp");
1371 picture->serialize(&stream);
1372 fSaveToSKP = false;
1373 }
1374
Brian Osmane9ed0f02018-11-26 14:50:05 -05001375 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001376 SkColorType colorType;
1377 switch (fColorMode) {
1378 case ColorMode::kLegacy:
1379 case ColorMode::kColorManaged8888:
1380 colorType = kN32_SkColorType;
1381 break;
1382 case ColorMode::kColorManagedF16:
1383 colorType = kRGBA_F16_SkColorType;
1384 break;
1385 case ColorMode::kColorManagedF16Norm:
1386 colorType = kRGBA_F16Norm_SkColorType;
1387 break;
1388 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001389
1390 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001391 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1392 slideCanvas->getProps(&props);
1393
Brian Osmane9ed0f02018-11-26 14:50:05 -05001394 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1395 return Window::kRaster_BackendType == this->fBackendType
1396 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001397 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001398 };
1399
Brian Osman03115dc2018-11-26 13:55:19 -05001400 // We need to render offscreen if we're...
1401 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1402 // ... in any raster mode, because the window surface is actually GL
1403 // ... in any color managed mode, because we always make the window surface with no color space
Chris Daltonc8877332020-01-06 09:48:30 -07001404 // ... or if the user explicitly requested offscreen rendering
Brian Osmanf750fbc2017-02-08 10:47:28 -05001405 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001406 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001407 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001408 Window::kRaster_BackendType == fBackendType ||
Chris Daltonc8877332020-01-06 09:48:30 -07001409 colorSpace != nullptr ||
1410 FLAGS_offscreen) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001411
Brian Osmane9ed0f02018-11-26 14:50:05 -05001412 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001413 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001414 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001415 }
1416
Mike Reed59295352020-03-12 13:56:34 -04001417 SkPictureRecorder recorder;
1418 SkCanvas* recorderRestoreCanvas = nullptr;
1419 if (fDrawViaSerialize) {
1420 recorderRestoreCanvas = slideCanvas;
1421 slideCanvas = recorder.beginRecording(
1422 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1423 }
1424
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001425 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001426 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001427 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001428 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001429 if (fTiled) {
1430 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1431 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
Brian Osmane9ed0f02018-11-26 14:50:05 -05001432 for (int y = 0; y < fWindow->height(); y += tileH) {
1433 for (int x = 0; x < fWindow->width(); x += tileW) {
Florin Malitaf0d5ea12020-02-19 09:23:08 -05001434 SkAutoCanvasRestore acr(slideCanvas, true);
1435 slideCanvas->clipRect(SkRect::MakeXYWH(x, y, tileW, tileH));
1436 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001437 }
1438 }
1439
1440 // Draw borders between tiles
1441 if (fDrawTileBoundaries) {
1442 SkPaint border;
1443 border.setColor(0x60FF00FF);
1444 border.setStyle(SkPaint::kStroke_Style);
1445 for (int y = 0; y < fWindow->height(); y += tileH) {
1446 for (int x = 0; x < fWindow->width(); x += tileW) {
1447 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1448 }
1449 }
1450 }
1451 } else {
1452 slideCanvas->concat(this->computeMatrix());
1453 if (kPerspective_Real == fPerspectiveMode) {
1454 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1455 }
Mike Reed3ae47332019-01-04 10:11:46 -05001456 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001457 fSlides[fCurrentSlide]->draw(&filterCanvas);
1458 }
Brian Osman56a24812017-12-19 11:15:16 -05001459 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001460 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001461
Mike Reed59295352020-03-12 13:56:34 -04001462 if (recorderRestoreCanvas) {
1463 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1464 auto data = picture->serialize();
1465 slideCanvas = recorderRestoreCanvas;
1466 slideCanvas->drawPicture(SkPicture::MakeFromData(data.get()));
1467 }
1468
Brian Osman1df161a2017-02-09 12:10:20 -05001469 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001470 fStatsLayer.beginTiming(fFlushTimer);
Greg Daniel0a2464f2020-05-14 15:45:44 -04001471 slideSurface->flushAndSubmit();
Brian Osman56a24812017-12-19 11:15:16 -05001472 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001473
1474 // If we rendered offscreen, snap an image and push the results to the window's canvas
1475 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001476 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001477
Robert Phillips9882dae2019-03-04 11:00:10 -05001478 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001479 SkPaint paint;
1480 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001481 int prePerspectiveCount = canvas->save();
1482 if (kPerspective_Fake == fPerspectiveMode) {
1483 paint.setFilterQuality(kHigh_SkFilterQuality);
1484 canvas->clear(SK_ColorWHITE);
1485 canvas->concat(this->computePerspectiveMatrix());
1486 }
Brian Osman03115dc2018-11-26 13:55:19 -05001487 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001488 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001489 }
Mike Reed376d8122019-03-14 11:39:02 -04001490
1491 if (fShowSlideDimensions) {
1492 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1493 SkPaint paint;
1494 paint.setColor(0x40FFFF00);
1495 surface->getCanvas()->drawRect(r, paint);
1496 }
liyuqian6f163d22016-06-13 12:26:45 -07001497}
1498
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001499void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001500 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001501 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001502}
Jim Van Verth6f449692017-02-14 15:16:46 -05001503
Robert Phillips9882dae2019-03-04 11:00:10 -05001504void Viewer::onPaint(SkSurface* surface) {
1505 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001506
Robert Phillips9882dae2019-03-04 11:00:10 -05001507 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001508
Brian Osmand67e5182017-12-08 16:46:09 -05001509 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001510
1511 if (GrContext* ctx = fWindow->getGrContext()) {
1512 // Clean out cache items that haven't been used in more than 10 seconds.
1513 ctx->performDeferredCleanup(std::chrono::seconds(10));
1514 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001515}
1516
Ben Wagnera1915972018-08-09 15:06:19 -04001517void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001518 if (fCurrentSlide >= 0) {
1519 fSlides[fCurrentSlide]->resize(width, height);
1520 }
Ben Wagnera1915972018-08-09 15:06:19 -04001521}
1522
Florin Malitacefc1b92018-02-19 21:43:47 -05001523SkPoint Viewer::mapEvent(float x, float y) {
1524 const auto m = this->computeMatrix();
1525 SkMatrix inv;
1526
1527 SkAssertResult(m.invert(&inv));
1528
1529 return inv.mapXY(x, y);
1530}
1531
Hal Canaryb1f411a2019-08-29 10:39:22 -04001532bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001533 if (GestureDevice::kMouse == fGestureDevice) {
1534 return false;
1535 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001536
1537 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001538 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001539 fWindow->inval();
1540 return true;
1541 }
1542
liyuqiand3cdbca2016-05-17 12:44:20 -07001543 void* castedOwner = reinterpret_cast<void*>(owner);
1544 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001545 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001546 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001547#if defined(SK_BUILD_FOR_IOS)
1548 // TODO: move IOS swipe detection higher up into the platform code
1549 SkPoint dir;
1550 if (fGesture.isFling(&dir)) {
1551 // swiping left or right
1552 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1553 if (dir.fX < 0) {
1554 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1555 fCurrentSlide + 1 : 0);
1556 } else {
1557 this->setCurrentSlide(fCurrentSlide > 0 ?
1558 fCurrentSlide - 1 : fSlides.count() - 1);
1559 }
1560 }
1561 fGesture.reset();
1562 }
1563#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001564 break;
1565 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001566 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001567 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001568 break;
1569 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001570 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001571 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001572 break;
1573 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001574 default: {
1575 // kLeft and kRight are only for swipes
1576 SkASSERT(false);
1577 break;
1578 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001579 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001580 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001581 fWindow->inval();
1582 return true;
1583}
1584
Hal Canaryb1f411a2019-08-29 10:39:22 -04001585bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001586 if (GestureDevice::kTouch == fGestureDevice) {
1587 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001588 }
Brian Osman16c81a12017-12-20 11:58:34 -05001589
Florin Malitacefc1b92018-02-19 21:43:47 -05001590 const auto slidePt = this->mapEvent(x, y);
1591 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1592 fWindow->inval();
1593 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001594 }
1595
1596 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001597 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001598 fGesture.touchEnd(nullptr);
1599 break;
1600 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001601 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001602 fGesture.touchBegin(nullptr, x, y);
1603 break;
1604 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001605 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001606 fGesture.touchMoved(nullptr, x, y);
1607 break;
1608 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001609 default: {
1610 SkASSERT(false); // shouldn't see kRight or kLeft here
1611 break;
1612 }
Brian Osman16c81a12017-12-20 11:58:34 -05001613 }
1614 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1615
Hal Canaryb1f411a2019-08-29 10:39:22 -04001616 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001617 fWindow->inval();
1618 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001619 return true;
1620}
1621
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001622bool Viewer::onFling(skui::InputState state) {
1623 if (skui::InputState::kRight == state) {
1624 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1625 return true;
1626 } else if (skui::InputState::kLeft == state) {
1627 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1628 return true;
1629 }
1630 return false;
1631}
1632
1633bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1634 switch (state) {
1635 case skui::InputState::kDown:
1636 fGesture.startZoom();
1637 return true;
1638 break;
1639 case skui::InputState::kMove:
1640 fGesture.updateZoom(scale, x, y, x, y);
1641 return true;
1642 break;
1643 case skui::InputState::kUp:
1644 fGesture.endZoom();
1645 return true;
1646 break;
1647 default:
1648 SkASSERT(false);
1649 break;
1650 }
1651
1652 return false;
1653}
1654
Brian Osmana109e392017-02-24 09:49:14 -05001655static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001656 // The gamut image covers a (0.8 x 0.9) shaped region
1657 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001658
1659 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1660 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1661 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001662 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1663 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1664 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001665
Brian Osman535c5e32019-02-09 16:32:58 -05001666 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1667 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1668 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1669 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1670 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001671}
1672
Ben Wagner3627d2e2018-06-26 14:23:20 -04001673static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001674 ImGui::DragCanvas dc(pt);
1675 dc.fillColor(IM_COL32(0, 0, 0, 128));
1676 dc.dragPoint(pt);
1677 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001678}
1679
Brian Osman9bb47cf2018-04-26 15:55:00 -04001680static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001681 ImGui::DragCanvas dc(pts);
1682 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001683
Brian Osman535c5e32019-02-09 16:32:58 -05001684 for (int i = 0; i < 4; ++i) {
1685 dc.dragPoint(pts + i);
1686 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001687
Brian Osman535c5e32019-02-09 16:32:58 -05001688 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1689 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1690 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1691 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001692
Brian Osman535c5e32019-02-09 16:32:58 -05001693 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001694}
1695
Brian Osmand67e5182017-12-08 16:46:09 -05001696void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001697 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1698 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001699 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001700 }
1701
1702 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001703 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1704 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001705 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001706 DisplayParams params = fWindow->getRequestedDisplayParams();
1707 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001708 const GrContext* ctx = fWindow->getGrContext();
1709
Brian Osmana109e392017-02-24 09:49:14 -05001710 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1711 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001712 if (ImGui::CollapsingHeader("Backend")) {
1713 int newBackend = static_cast<int>(fBackendType);
1714 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1715 ImGui::SameLine();
1716 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001717#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1718 ImGui::SameLine();
1719 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1720#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001721#if defined(SK_DAWN)
1722 ImGui::SameLine();
1723 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1724#endif
Brian Osman621491e2017-02-28 15:45:01 -05001725#if defined(SK_VULKAN)
1726 ImGui::SameLine();
1727 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1728#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001729#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001730 ImGui::SameLine();
1731 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1732#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -04001733#if defined(SK_DIRECT3D)
1734 ImGui::SameLine();
1735 ImGui::RadioButton("Direct3D", &newBackend, sk_app::Window::kDirect3D_BackendType);
1736#endif
Brian Osman621491e2017-02-28 15:45:01 -05001737 if (newBackend != fBackendType) {
1738 fDeferredActions.push_back([=]() {
1739 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1740 });
1741 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001742
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001743 bool* wire = &params.fGrContextOptions.fWireframeMode;
1744 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1745 paramsChanged = true;
1746 }
Brian Salomon99a33902017-03-07 15:16:34 -05001747
Brian Osman28b12522017-03-08 17:10:24 -05001748 if (ctx) {
John Stiles5daaa7f2020-05-06 11:06:47 -04001749 // Determine the context's max sample count for MSAA radio buttons.
Brian Osman28b12522017-03-08 17:10:24 -05001750 int sampleCount = fWindow->sampleCount();
John Stiles5daaa7f2020-05-06 11:06:47 -04001751 int maxMSAA = (fBackendType != sk_app::Window::kRaster_BackendType) ?
1752 ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) :
1753 1;
1754
1755 // Only display the MSAA radio buttons when there are options above 1x MSAA.
1756 if (maxMSAA >= 4) {
1757 ImGui::Text("MSAA: ");
1758
1759 for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) {
1760 // 2x MSAA works, but doesn't offer much of a visual improvement, so we
1761 // don't show it in the list.
1762 if (curMSAA == 2) {
1763 continue;
1764 }
1765 ImGui::SameLine();
1766 ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(),
1767 &sampleCount, curMSAA);
1768 }
1769 }
Brian Osman28b12522017-03-08 17:10:24 -05001770
1771 if (sampleCount != params.fMSAASampleCount) {
1772 params.fMSAASampleCount = sampleCount;
1773 paramsChanged = true;
1774 }
1775 }
1776
Ben Wagner37c54032018-04-13 14:30:23 -04001777 int pixelGeometryIdx = 0;
1778 if (fPixelGeometryOverrides) {
1779 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1780 }
1781 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1782 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1783 {
1784 uint32_t flags = params.fSurfaceProps.flags();
1785 if (pixelGeometryIdx == 0) {
1786 fPixelGeometryOverrides = false;
1787 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1788 } else {
1789 fPixelGeometryOverrides = true;
1790 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1791 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1792 }
1793 paramsChanged = true;
1794 }
1795
1796 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1797 if (ImGui::Checkbox("DFT", &useDFT)) {
1798 uint32_t flags = params.fSurfaceProps.flags();
1799 if (useDFT) {
1800 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1801 } else {
1802 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1803 }
1804 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1805 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1806 paramsChanged = true;
1807 }
1808
Brian Osman8a9de3d2017-03-01 14:59:05 -05001809 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001810 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001811 auto prButton = [&](GpuPathRenderers x) {
1812 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001813 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1814 params.fGrContextOptions.fGpuPathRenderers = x;
1815 paramsChanged = true;
1816 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001817 }
1818 };
1819
1820 if (!ctx) {
1821 ImGui::RadioButton("Software", true);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001822 } else {
Chris Dalton37ae4b02019-12-28 14:51:11 -07001823 const auto* caps = ctx->priv().caps();
1824 prButton(GpuPathRenderers::kDefault);
1825 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07001826 if (caps->shaderCaps()->tessellationSupport()) {
Chris Dalton0a22b1e2020-03-26 11:52:15 -06001827 prButton(GpuPathRenderers::kTessellation);
Chris Daltonb832ce62020-01-06 19:49:37 -07001828 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001829 if (caps->shaderCaps()->pathRenderingSupport()) {
1830 prButton(GpuPathRenderers::kStencilAndCover);
1831 }
Chris Dalton1a325d22017-07-14 15:17:41 -06001832 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07001833 if (1 == fWindow->sampleCount()) {
1834 if (GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1835 prButton(GpuPathRenderers::kCoverageCounting);
1836 }
1837 prButton(GpuPathRenderers::kSmall);
1838 }
Chris Dalton17dc4182020-03-25 16:18:16 -06001839 prButton(GpuPathRenderers::kTriangulating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001840 prButton(GpuPathRenderers::kNone);
1841 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001842 ImGui::TreePop();
1843 }
Brian Osman621491e2017-02-28 15:45:01 -05001844 }
1845
Ben Wagner964571d2019-03-08 12:35:06 -05001846 if (ImGui::CollapsingHeader("Tiling")) {
1847 ImGui::Checkbox("Enable", &fTiled);
1848 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1849 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1850 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1851 }
1852
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001853 if (ImGui::CollapsingHeader("Transform")) {
1854 float zoom = fZoomLevel;
1855 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1856 fZoomLevel = zoom;
1857 this->preTouchMatrixChanged();
1858 paramsChanged = true;
1859 }
1860 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001861 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001862 fRotation = deg;
1863 this->preTouchMatrixChanged();
1864 paramsChanged = true;
1865 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001866 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1867 if (ImGui_DragLocation(&fOffset)) {
1868 this->preTouchMatrixChanged();
1869 paramsChanged = true;
1870 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001871 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1872 this->preTouchMatrixChanged();
1873 paramsChanged = true;
1874 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001875 }
Brian Osman805a7272018-05-02 15:40:20 -04001876 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1877 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1878 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001879 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001880 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001881 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001882 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001883 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001884 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001885 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001886 }
1887
Ben Wagnera580fb32018-04-17 11:16:32 -04001888 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001889 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001890 if (fPaintOverrides.fAntiAlias) {
1891 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001892 }
1893 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001894 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001895 {
1896 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1897 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001898 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001899 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1900 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001901 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001902 fPaintOverrides.fAntiAlias = true;
1903 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001904 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001905 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001906 case SkPaintFields::AntiAliasState::Alias:
1907 break;
1908 case SkPaintFields::AntiAliasState::Normal:
1909 break;
1910 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1911 gSkUseAnalyticAA = true;
1912 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001913 break;
1914 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1915 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001916 break;
1917 }
1918 }
1919 paramsChanged = true;
1920 }
1921
Ben Wagner99a78dc2018-05-09 18:23:51 -04001922 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001923 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001924 bool (SkPaint::* isFlag)() const,
1925 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001926 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001927 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001928 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001929 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001930 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001931 if (ImGui::Combo(label, &itemIndex, items)) {
1932 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001933 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001934 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001935 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001936 (fPaint.*setFlag)(itemIndex == 2);
1937 }
1938 paramsChanged = true;
1939 }
1940 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001941
Ben Wagner99a78dc2018-05-09 18:23:51 -04001942 paintFlag("Dither",
1943 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001944 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001945 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001946
1947 int filterQualityIdx = 0;
1948 if (fPaintOverrides.fFilterQuality) {
1949 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1950 }
1951 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1952 "Default\0None\0Low\0Medium\0High\0\0"))
1953 {
1954 if (filterQualityIdx == 0) {
1955 fPaintOverrides.fFilterQuality = false;
1956 fPaint.setFilterQuality(kNone_SkFilterQuality);
1957 } else {
1958 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1959 fPaintOverrides.fFilterQuality = true;
1960 }
1961 paramsChanged = true;
1962 }
Ben Wagner9613e452019-01-23 10:34:59 -05001963 }
Hal Canary02738a82019-01-21 18:51:32 +00001964
Ben Wagner9613e452019-01-23 10:34:59 -05001965 if (ImGui::CollapsingHeader("Font")) {
1966 int hintingIdx = 0;
1967 if (fFontOverrides.fHinting) {
1968 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1969 }
1970 if (ImGui::Combo("Hinting", &hintingIdx,
1971 "Default\0None\0Slight\0Normal\0Full\0\0"))
1972 {
1973 if (hintingIdx == 0) {
1974 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001975 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001976 } else {
1977 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1978 fFontOverrides.fHinting = true;
1979 }
1980 paramsChanged = true;
1981 }
Hal Canary02738a82019-01-21 18:51:32 +00001982
Ben Wagner9613e452019-01-23 10:34:59 -05001983 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1984 bool SkFontFields::* flag,
1985 bool (SkFont::* isFlag)() const,
1986 void (SkFont::* setFlag)(bool) )
1987 {
1988 int itemIndex = 0;
1989 if (fFontOverrides.*flag) {
1990 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1991 }
1992 if (ImGui::Combo(label, &itemIndex, items)) {
1993 if (itemIndex == 0) {
1994 fFontOverrides.*flag = false;
1995 } else {
1996 fFontOverrides.*flag = true;
1997 (fFont.*setFlag)(itemIndex == 2);
1998 }
1999 paramsChanged = true;
2000 }
2001 };
Hal Canary02738a82019-01-21 18:51:32 +00002002
Ben Wagner9613e452019-01-23 10:34:59 -05002003 fontFlag("Fake Bold Glyphs",
2004 "Default\0No Fake Bold\0Fake Bold\0\0",
2005 &SkFontFields::fEmbolden,
2006 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00002007
Ben Wagnerc17de1d2019-08-26 16:59:09 -04002008 fontFlag("Baseline Snapping",
2009 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
2010 &SkFontFields::fBaselineSnap,
2011 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
2012
Ben Wagner9613e452019-01-23 10:34:59 -05002013 fontFlag("Linear Text",
2014 "Default\0No Linear Text\0Linear Text\0\0",
2015 &SkFontFields::fLinearMetrics,
2016 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00002017
Ben Wagner9613e452019-01-23 10:34:59 -05002018 fontFlag("Subpixel Position Glyphs",
2019 "Default\0Pixel Text\0Subpixel Text\0\0",
2020 &SkFontFields::fSubpixel,
2021 &SkFont::isSubpixel, &SkFont::setSubpixel);
2022
2023 fontFlag("Embedded Bitmap Text",
2024 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
2025 &SkFontFields::fEmbeddedBitmaps,
2026 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
2027
2028 fontFlag("Force Auto-Hinting",
2029 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
2030 &SkFontFields::fForceAutoHinting,
2031 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
2032
2033 int edgingIdx = 0;
2034 if (fFontOverrides.fEdging) {
2035 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
2036 }
2037 if (ImGui::Combo("Edging", &edgingIdx,
2038 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
2039 {
2040 if (edgingIdx == 0) {
2041 fFontOverrides.fEdging = false;
2042 fFont.setEdging(SkFont::Edging::kAlias);
2043 } else {
2044 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
2045 fFontOverrides.fEdging = true;
2046 }
2047 paramsChanged = true;
2048 }
2049
Ben Wagner15a8d572019-03-21 13:35:44 -04002050 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
2051 if (fFontOverrides.fSize) {
2052 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002053 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05002054 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002055 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04002056 fFontOverrides.fSizeRange[0],
2057 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002058 "%.6f", 2.0f))
2059 {
Mike Reed3ae47332019-01-04 10:11:46 -05002060 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04002061 paramsChanged = true;
2062 }
2063 }
2064
2065 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
2066 if (fFontOverrides.fScaleX) {
2067 float scaleX = fFont.getScaleX();
2068 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2069 fFont.setScaleX(scaleX);
2070 paramsChanged = true;
2071 }
2072 }
2073
2074 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
2075 if (fFontOverrides.fSkewX) {
2076 float skewX = fFont.getSkewX();
2077 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
2078 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04002079 paramsChanged = true;
2080 }
2081 }
Ben Wagnera580fb32018-04-17 11:16:32 -04002082 }
2083
Mike Reed81f60ec2018-05-15 10:09:52 -04002084 {
2085 SkMetaData controls;
2086 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
2087 if (ImGui::CollapsingHeader("Current Slide")) {
2088 SkMetaData::Iter iter(controls);
2089 const char* name;
2090 SkMetaData::Type type;
2091 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04002092 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04002093 if (type == SkMetaData::kScalar_Type) {
2094 float val[3];
2095 SkASSERT(count == 3);
2096 controls.findScalars(name, &count, val);
2097 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
2098 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04002099 }
Ben Wagner110c7032019-03-22 17:03:59 -04002100 } else if (type == SkMetaData::kBool_Type) {
2101 bool val;
2102 SkASSERT(count == 1);
2103 controls.findBool(name, &val);
2104 if (ImGui::Checkbox(name, &val)) {
2105 controls.setBool(name, val);
2106 }
Mike Reed81f60ec2018-05-15 10:09:52 -04002107 }
2108 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04002109 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04002110 }
2111 }
2112 }
2113
Ben Wagner7a3c6742018-04-23 10:01:07 -04002114 if (fShowSlidePicker) {
2115 ImGui::SetNextTreeNodeOpen(true);
2116 }
Brian Osman79086b92017-02-10 13:36:16 -05002117 if (ImGui::CollapsingHeader("Slide")) {
2118 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05002119 static ImVector<const char*> filteredSlideNames;
2120 static ImVector<int> filteredSlideIndices;
2121
Brian Osmanfce09c52017-11-14 15:32:20 -05002122 if (fShowSlidePicker) {
2123 ImGui::SetKeyboardFocusHere();
2124 fShowSlidePicker = false;
2125 }
2126
Brian Osman79086b92017-02-10 13:36:16 -05002127 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002128 filteredSlideNames.clear();
2129 filteredSlideIndices.clear();
2130 int filteredIndex = 0;
2131 for (int i = 0; i < fSlides.count(); ++i) {
2132 const char* slideName = fSlides[i]->getName().c_str();
2133 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2134 if (i == fCurrentSlide) {
2135 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002136 }
Brian Osmanf479e422017-11-08 13:11:36 -05002137 filteredSlideNames.push_back(slideName);
2138 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002139 }
Brian Osman79086b92017-02-10 13:36:16 -05002140 }
Brian Osmanf479e422017-11-08 13:11:36 -05002141
Brian Osmanf479e422017-11-08 13:11:36 -05002142 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2143 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002144 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002145 }
2146 }
Brian Osmana109e392017-02-24 09:49:14 -05002147
2148 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002149 ColorMode newMode = fColorMode;
2150 auto cmButton = [&](ColorMode mode, const char* label) {
2151 if (ImGui::RadioButton(label, mode == fColorMode)) {
2152 newMode = mode;
2153 }
2154 };
2155
2156 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002157 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2158 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002159 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002160
2161 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002162 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002163 }
2164
2165 // Pick from common gamuts:
2166 int primariesIdx = 4; // Default: Custom
2167 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2168 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2169 primariesIdx = i;
2170 break;
2171 }
2172 }
2173
Brian Osman03115dc2018-11-26 13:55:19 -05002174 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002175 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002176
Brian Osmana109e392017-02-24 09:49:14 -05002177 if (ImGui::Combo("Primaries", &primariesIdx,
2178 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2179 if (primariesIdx >= 0 && primariesIdx <= 3) {
2180 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2181 }
2182 }
2183
2184 // Allow direct editing of gamut
2185 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2186 }
Brian Osman207d4102019-01-10 09:40:58 -05002187
2188 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002189 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002190 if (ImGui::Checkbox("Pause", &isPaused)) {
2191 fAnimTimer.togglePauseResume();
2192 }
Brian Osman707d2022019-01-10 11:27:34 -05002193
2194 float speed = fAnimTimer.getSpeed();
2195 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2196 fAnimTimer.setSpeed(speed);
2197 }
Brian Osman207d4102019-01-10 09:40:58 -05002198 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002199
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002200 if (ImGui::CollapsingHeader("Shaders")) {
2201 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2202 GrContextOptions::ShaderCacheStrategy::kSkSL;
2203#if defined(SK_VULKAN)
2204 const bool isVulkan = fBackendType == sk_app::Window::kVulkan_BackendType;
2205#else
2206 const bool isVulkan = false;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002207#endif
Brian Osmanfd7657c2019-04-25 11:34:07 -04002208
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002209 // To re-load shaders from the currently active programs, we flush all
2210 // caches on one frame, then set a flag to poll the cache on the next frame.
Brian Osman0b8bb882019-04-12 11:47:19 -04002211 static bool gLoadPending = false;
2212 if (gLoadPending) {
2213 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2214 int hitCount) {
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002215 CachedShader& entry(fCachedShaders.push_back());
Brian Osman0b8bb882019-04-12 11:47:19 -04002216 entry.fKey = key;
2217 SkMD5 hash;
2218 hash.write(key->bytes(), key->size());
2219 SkMD5::Digest digest = hash.finish();
2220 for (int i = 0; i < 16; ++i) {
2221 entry.fKeyString.appendf("%02x", digest.data[i]);
2222 }
2223
Brian Osman9e4e4c72020-06-10 07:19:34 -04002224 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -04002225 entry.fShaderType = GrPersistentCacheUtils::GetType(&reader);
Brian Osmana66081d2019-09-03 14:59:26 -04002226 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2227 entry.fInputs,
2228 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002229 };
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002230 fCachedShaders.reset();
Brian Osman0b8bb882019-04-12 11:47:19 -04002231 fPersistentCache.foreach(collectShaders);
2232 gLoadPending = false;
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002233
2234#if defined(SK_VULKAN)
2235 if (isVulkan && !sksl) {
2236 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
2237 for (auto& entry : fCachedShaders) {
2238 for (int i = 0; i < kGrShaderTypeCount; ++i) {
2239 const SkSL::String& spirv(entry.fShader[i]);
2240 std::string disasm;
2241 tools.Disassemble((const uint32_t*)spirv.c_str(), spirv.size() / 4,
2242 &disasm);
2243 entry.fShader[i].assign(disasm);
2244 }
2245 }
2246 }
2247#endif
Brian Osman0b8bb882019-04-12 11:47:19 -04002248 }
2249
2250 // Defer actually doing the load/save logic so that we can trigger a save when we
2251 // start or finish hovering on a tree node in the list below:
2252 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002253 bool doSave = ImGui::Button("Save"); ImGui::SameLine();
2254 if (ImGui::Checkbox("SkSL", &sksl)) {
2255 params.fGrContextOptions.fShaderCacheStrategy =
2256 sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL
2257 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
2258 paramsChanged = true;
2259 doLoad = true;
2260 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
Brian Osmancbc33b82019-04-19 14:16:19 -04002261 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002262
2263 ImGui::BeginChild("##ScrollingRegion");
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002264 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002265 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2266 bool hovered = ImGui::IsItemHovered();
2267 if (hovered != entry.fHovered) {
2268 // Force a save to patch the highlight shader in/out
2269 entry.fHovered = hovered;
2270 doSave = true;
2271 }
2272 if (inTreeNode) {
2273 // Full width, and a reasonable amount of space for each shader.
2274 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2275 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2276 boxSize);
2277 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2278 boxSize);
2279 ImGui::TreePop();
2280 }
2281 }
2282 ImGui::EndChild();
2283
2284 if (doLoad) {
2285 fPersistentCache.reset();
2286 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2287 gLoadPending = true;
2288 }
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002289 // We don't support updating SPIRV shaders. We could re-assemble them (with edits),
2290 // but I'm not sure anyone wants to do that.
2291 if (isVulkan && !sksl) {
2292 doSave = false;
2293 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002294 if (doSave) {
2295 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002296 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osman5bee3902019-05-07 09:55:45 -04002297
Brian Osman072e6fc2019-06-12 11:35:41 -04002298 SkSL::String highlight;
2299 if (!sksl) {
2300 highlight = shaderCaps->versionDeclString();
2301 if (shaderCaps->usesPrecisionModifiers()) {
2302 highlight.append("precision mediump float;\n");
2303 }
Brian Osman5bee3902019-05-07 09:55:45 -04002304 }
2305 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002306 highlight.appendf("out %s sk_FragColor;\n"
2307 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2308 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002309
2310 fPersistentCache.reset();
2311 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002312 for (auto& entry : fCachedShaders) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002313 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
Brian Osmanc85f1fa2020-06-16 15:11:34 -04002314 if (entry.fHovered &&
2315 (entry.fShaderType == SkSetFourByteTag('S', 'K', 'S', 'L') ||
2316 entry.fShaderType == SkSetFourByteTag('G', 'L', 'S', 'L'))) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002317 entry.fShader[kFragment_GrShaderType] = highlight;
2318 }
2319
Brian Osmana085a412019-04-25 09:44:43 -04002320 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2321 entry.fShader,
2322 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002323 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002324 fPersistentCache.store(*entry.fKey, *data);
2325
2326 entry.fShader[kFragment_GrShaderType] = backup;
2327 }
2328 }
2329 }
Brian Osman79086b92017-02-10 13:36:16 -05002330 }
Brian Salomon99a33902017-03-07 15:16:34 -05002331 if (paramsChanged) {
2332 fDeferredActions.push_back([=]() {
2333 fWindow->setRequestedDisplayParams(params);
2334 fWindow->inval();
2335 this->updateTitle();
2336 });
2337 }
Brian Osman79086b92017-02-10 13:36:16 -05002338 ImGui::End();
2339 }
2340
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002341 if (gShaderErrorHandler.fErrors.count()) {
2342 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2343 ImGui::Begin("Shader Errors");
2344 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2345 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002346 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2347 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2348 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2349 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002350 }
2351 ImGui::End();
2352 gShaderErrorHandler.reset();
2353 }
2354
Brian Osmanf6877092017-02-13 09:39:57 -05002355 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002356 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2357 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002358 static int zoomFactor = 8;
2359 if (ImGui::Button("<<")) {
Brian Osman788b9162020-02-07 10:36:46 -05002360 zoomFactor = std::max(zoomFactor / 2, 4);
Brian Osmanead517d2017-11-13 15:36:36 -05002361 }
2362 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2363 if (ImGui::Button(">>")) {
Brian Osman788b9162020-02-07 10:36:46 -05002364 zoomFactor = std::min(zoomFactor * 2, 32);
Brian Osmanead517d2017-11-13 15:36:36 -05002365 }
Brian Osmanf6877092017-02-13 09:39:57 -05002366
Ben Wagner3627d2e2018-06-26 14:23:20 -04002367 if (!fZoomWindowFixed) {
2368 ImVec2 mousePos = ImGui::GetMousePos();
2369 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2370 }
2371 SkScalar x = fZoomWindowLocation.x();
2372 SkScalar y = fZoomWindowLocation.y();
2373 int xInt = SkScalarRoundToInt(x);
2374 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002375 ImVec2 avail = ImGui::GetContentRegionAvail();
2376
Brian Osmanead517d2017-11-13 15:36:36 -05002377 uint32_t pixel = 0;
2378 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002379 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002380 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002381 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002382 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002383 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002384 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2385 }
2386
Brian Osmand67e5182017-12-08 16:46:09 -05002387 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002388 // Translate so the region of the image that's under the mouse cursor is centered
2389 // in the zoom canvas:
2390 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002391 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2392 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002393 c->drawImage(this->fLastImage, 0, 0);
2394
2395 SkPaint outline;
2396 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002397 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002398 });
Brian Osmanf6877092017-02-13 09:39:57 -05002399 }
2400
2401 ImGui::End();
2402 }
Brian Osman79086b92017-02-10 13:36:16 -05002403}
2404
liyuqian2edb0f42016-07-06 14:11:32 -07002405void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002406 for (int i = 0; i < fDeferredActions.count(); ++i) {
2407 fDeferredActions[i]();
2408 }
2409 fDeferredActions.reset();
2410
Brian Osman56a24812017-12-19 11:15:16 -05002411 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002412 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002413 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002414 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002415
Brian Osman79086b92017-02-10 13:36:16 -05002416 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002417 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2418 // not be visible, though. So we need to redraw if there is at least one visible window, or
2419 // more than one active window. Newly created windows are active but not visible for one frame
2420 // while they determine their layout and sizing.
2421 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2422 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002423 fWindow->inval();
2424 }
jvanverth9f372462016-04-06 06:08:59 -07002425}
liyuqiane5a6cd92016-05-27 08:52:52 -07002426
Florin Malitab632df72018-06-18 21:23:06 -04002427template <typename OptionsFunc>
2428static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2429 OptionsFunc&& optionsFunc) {
2430 writer.beginObject();
2431 {
2432 writer.appendString(kName , name);
2433 writer.appendString(kValue, value);
2434
2435 writer.beginArray(kOptions);
2436 {
2437 optionsFunc(writer);
2438 }
2439 writer.endArray();
2440 }
2441 writer.endObject();
2442}
2443
2444
liyuqiane5a6cd92016-05-27 08:52:52 -07002445void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002446 if (!fWindow) {
2447 return;
2448 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002449 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002450 return; // Surface hasn't been created yet.
2451 }
2452
Florin Malitab632df72018-06-18 21:23:06 -04002453 SkDynamicMemoryWStream memStream;
2454 SkJSONWriter writer(&memStream);
2455 writer.beginArray();
2456
liyuqianb73c24b2016-06-03 08:47:23 -07002457 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002458 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2459 [this](SkJSONWriter& writer) {
2460 for(const auto& slide : fSlides) {
2461 writer.appendString(slide->getName().c_str());
2462 }
2463 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002464
liyuqianb73c24b2016-06-03 08:47:23 -07002465 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002466 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2467 [](SkJSONWriter& writer) {
2468 for (const auto& str : kBackendTypeStrings) {
2469 writer.appendString(str);
2470 }
2471 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002472
csmartdalton578f0642017-02-24 16:04:47 -07002473 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002474 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2475 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2476 [this](SkJSONWriter& writer) {
2477 writer.appendS32(0);
2478
2479 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2480 return;
2481 }
2482
2483 for (int msaa : {4, 8, 16}) {
2484 writer.appendS32(msaa);
2485 }
2486 });
csmartdalton578f0642017-02-24 16:04:47 -07002487
csmartdalton61cd31a2017-02-27 17:00:53 -07002488 // Path renderer state
2489 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002490 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2491 [this](SkJSONWriter& writer) {
2492 const GrContext* ctx = fWindow->getGrContext();
2493 if (!ctx) {
2494 writer.appendString("Software");
2495 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002496 const auto* caps = ctx->priv().caps();
Chris Dalton37ae4b02019-12-28 14:51:11 -07002497 writer.appendString(gPathRendererNames[GpuPathRenderers::kDefault].c_str());
2498 if (fWindow->sampleCount() > 1 || caps->mixedSamplesSupport()) {
Chris Daltonb832ce62020-01-06 19:49:37 -07002499 if (caps->shaderCaps()->tessellationSupport()) {
2500 writer.appendString(
Chris Dalton0a22b1e2020-03-26 11:52:15 -06002501 gPathRendererNames[GpuPathRenderers::kTessellation].c_str());
Chris Daltonb832ce62020-01-06 19:49:37 -07002502 }
Florin Malitab632df72018-06-18 21:23:06 -04002503 if (caps->shaderCaps()->pathRenderingSupport()) {
2504 writer.appendString(
Chris Dalton37ae4b02019-12-28 14:51:11 -07002505 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002506 }
Chris Dalton37ae4b02019-12-28 14:51:11 -07002507 }
2508 if (1 == fWindow->sampleCount()) {
Florin Malitab632df72018-06-18 21:23:06 -04002509 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2510 writer.appendString(
2511 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2512 }
2513 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2514 }
Chris Dalton17dc4182020-03-25 16:18:16 -06002515 writer.appendString(gPathRendererNames[GpuPathRenderers::kTriangulating].c_str());
Chris Dalton37ae4b02019-12-28 14:51:11 -07002516 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
Florin Malitab632df72018-06-18 21:23:06 -04002517 }
2518 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002519
liyuqianb73c24b2016-06-03 08:47:23 -07002520 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002521 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2522 [this](SkJSONWriter& writer) {
2523 writer.appendString(kSoftkeyHint);
2524 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2525 writer.appendString(softkey.c_str());
2526 }
2527 });
liyuqianb73c24b2016-06-03 08:47:23 -07002528
Florin Malitab632df72018-06-18 21:23:06 -04002529 writer.endArray();
2530 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002531
Florin Malitab632df72018-06-18 21:23:06 -04002532 auto data = memStream.detachAsData();
2533
2534 // TODO: would be cool to avoid this copy
2535 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2536
2537 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002538}
2539
2540void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002541 // For those who will add more features to handle the state change in this function:
2542 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2543 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2544 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002545 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002546 for (int i = 0; i < fSlides.count(); ++i) {
2547 if (fSlides[i]->getName().equals(stateValue)) {
2548 this->setCurrentSlide(i);
2549 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002550 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002551 }
Florin Malitaab99c342018-01-16 16:23:03 -05002552
2553 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002554 } else if (stateName.equals(kBackendStateName)) {
2555 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2556 if (stateValue.equals(kBackendTypeStrings[i])) {
2557 if (fBackendType != i) {
2558 fBackendType = (sk_app::Window::BackendType)i;
2559 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002560 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002561 }
2562 break;
2563 }
2564 }
csmartdalton578f0642017-02-24 16:04:47 -07002565 } else if (stateName.equals(kMSAAStateName)) {
2566 DisplayParams params = fWindow->getRequestedDisplayParams();
2567 int sampleCount = atoi(stateValue.c_str());
2568 if (sampleCount != params.fMSAASampleCount) {
2569 params.fMSAASampleCount = sampleCount;
2570 fWindow->setRequestedDisplayParams(params);
2571 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002572 this->updateTitle();
2573 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002574 }
2575 } else if (stateName.equals(kPathRendererStateName)) {
2576 DisplayParams params = fWindow->getRequestedDisplayParams();
2577 for (const auto& pair : gPathRendererNames) {
2578 if (pair.second == stateValue.c_str()) {
2579 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2580 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2581 fWindow->setRequestedDisplayParams(params);
2582 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002583 this->updateTitle();
2584 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002585 }
2586 break;
2587 }
csmartdalton578f0642017-02-24 16:04:47 -07002588 }
liyuqianb73c24b2016-06-03 08:47:23 -07002589 } else if (stateName.equals(kSoftkeyStateName)) {
2590 if (!stateValue.equals(kSoftkeyHint)) {
2591 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002592 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002593 }
liyuqian2edb0f42016-07-06 14:11:32 -07002594 } else if (stateName.equals(kRefreshStateName)) {
2595 // This state is actually NOT in the UI state.
2596 // We use this to allow Android to quickly set bool fRefresh.
2597 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002598 } else {
2599 SkDebugf("Unknown stateName: %s", stateName.c_str());
2600 }
2601}
Brian Osman79086b92017-02-10 13:36:16 -05002602
Hal Canaryb1f411a2019-08-29 10:39:22 -04002603bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002604 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002605}
2606
Hal Canaryb1f411a2019-08-29 10:39:22 -04002607bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002608 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002609 fWindow->inval();
2610 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002611 } else {
2612 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002613 }
Brian Osman79086b92017-02-10 13:36:16 -05002614}