blob: dd64e849e81c9c9aa0081afb45ed8edcb1d36da3 [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"
20#include "src/core/SkMakeUnique.h"
21#include "src/core/SkOSFile.h"
22#include "src/core/SkScan.h"
23#include "src/core/SkTaskGroup.h"
24#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrGpu.h"
26#include "src/gpu/GrPersistentCacheUtils.h"
27#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
28#include "src/utils/SkJSONWriter.h"
29#include "src/utils/SkOSPath.h"
30#include "tools/Resources.h"
31#include "tools/ToolUtils.h"
32#include "tools/flags/CommandLineFlags.h"
33#include "tools/flags/CommonFlags.h"
34#include "tools/trace/EventTracingPriv.h"
35#include "tools/viewer/BisectSlide.h"
36#include "tools/viewer/GMSlide.h"
37#include "tools/viewer/ImageSlide.h"
38#include "tools/viewer/ParticlesSlide.h"
39#include "tools/viewer/SKPSlide.h"
40#include "tools/viewer/SampleSlide.h"
41#include "tools/viewer/SlideDir.h"
42#include "tools/viewer/SvgSlide.h"
43#include "tools/viewer/Viewer.h"
csmartdalton578f0642017-02-24 16:04:47 -070044
Hal Canaryc640d0d2018-06-13 09:59:02 -040045#include <stdlib.h>
46#include <map>
47
Hal Canary8a001442018-09-19 11:31:27 -040048#include "imgui.h"
Brian Osman0b8bb882019-04-12 11:47:19 -040049#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string
Florin Malita3b526b02018-05-25 12:43:51 -040050
Florin Malita87ccf332018-05-04 12:23:24 -040051#if defined(SK_ENABLE_SKOTTIE)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052 #include "tools/viewer/SkottieSlide.h"
Florin Malita87ccf332018-05-04 12:23:24 -040053#endif
54
Brian Osman5e7fbfd2019-05-03 13:13:35 -040055class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
56public:
57 void compileError(const char* shader, const char* errors) override {
58 fShaders.push_back(SkString(shader));
59 fErrors.push_back(SkString(errors));
60 }
61
62 void reset() {
63 fShaders.reset();
64 fErrors.reset();
65 }
66
67 SkTArray<SkString> fShaders;
68 SkTArray<SkString> fErrors;
69};
70
71static CapturingShaderErrorHandler gShaderErrorHandler;
72
jvanverth34524262016-05-04 13:49:13 -070073using namespace sk_app;
74
csmartdalton61cd31a2017-02-27 17:00:53 -070075static std::map<GpuPathRenderers, std::string> gPathRendererNames;
76
jvanverth9f372462016-04-06 06:08:59 -070077Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070078 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070079}
80
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060081static DEFINE_string(slide, "", "Start on this sample.");
82static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050083
Stephen Whitea800ec92019-08-02 15:04:52 -040084#if defined(SK_VULKAN)
jvanverthb8794cc2016-07-27 14:29:18 -070085# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050086#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
87# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
Stephen Whitea800ec92019-08-02 15:04:52 -040088#elif defined(SK_DAWN)
89# define BACKENDS_STR "\"sw\", \"gl\", and \"dawn\""
bsalomon6c471f72016-07-26 12:56:32 -070090#else
91# define BACKENDS_STR "\"sw\" and \"gl\""
92#endif
93
Brian Osman2dd96932016-10-18 15:33:53 -040094static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070095
Mike Klein5b3f3432019-03-21 11:42:21 -050096static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070097
Chris Dalton1e6c5b82019-06-17 14:16:49 -060098static DEFINE_int(internalSamples, 4,
99 "Number of samples for internal draws that use MSAA or mixed samples.");
100
Mike Klein84836b72019-03-21 11:31:36 -0500101static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
Chris Dalton2d18f412018-02-20 13:23:32 -0700102
Mike Klein84836b72019-03-21 11:31:36 -0500103static DEFINE_string2(file, f, "", "Open a single file for viewing.");
Florin Malita38792ce2018-05-08 10:36:18 -0400104
Mike Kleinc6142d82019-03-25 10:54:59 -0500105static DEFINE_string2(match, m, nullptr,
106 "[~][^]substring[$] [...] of name to run.\n"
107 "Multiple matches may be separated by spaces.\n"
108 "~ causes a matching name to always be skipped\n"
109 "^ requires the start of the name to match\n"
110 "$ requires the end of the name to match\n"
111 "^ and $ requires an exact match\n"
112 "If a name does not match any list entry,\n"
113 "it is skipped unless some list entry starts with ~");
114
Mike Klein19fb3972019-03-21 13:08:08 -0500115#if defined(SK_BUILD_FOR_ANDROID)
116 static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500117 static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
118 static DEFINE_string(lotties, "/data/local/tmp/lotties",
119 "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500120#else
121 static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500122 static DEFINE_string(skps, "skps", "Directory to read skps from.");
123 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Mike Klein19fb3972019-03-21 13:08:08 -0500124#endif
125
Mike Kleinc6142d82019-03-25 10:54:59 -0500126static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
127
128static DEFINE_int_2(threads, j, -1,
129 "Run threadsafe tests on a threadpool with this many extra threads, "
130 "defaulting to one extra thread per core.");
131
132
Brian Salomon194db172017-08-17 14:37:06 -0400133const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700134 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400135#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
136 "ANGLE",
137#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400138#ifdef SK_DAWN
139 "Dawn",
140#endif
jvanverth063ece72016-06-17 09:29:14 -0700141#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700142 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700143#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400144#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500145 "Metal",
146#endif
csmartdalton578f0642017-02-24 16:04:47 -0700147 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700148};
149
bsalomon6c471f72016-07-26 12:56:32 -0700150static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400151#ifdef SK_DAWN
152 if (0 == strcmp(str, "dawn")) {
153 return sk_app::Window::kDawn_BackendType;
154 } else
155#endif
bsalomon6c471f72016-07-26 12:56:32 -0700156#ifdef SK_VULKAN
157 if (0 == strcmp(str, "vk")) {
158 return sk_app::Window::kVulkan_BackendType;
159 } else
160#endif
Brian Salomon194db172017-08-17 14:37:06 -0400161#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
162 if (0 == strcmp(str, "angle")) {
163 return sk_app::Window::kANGLE_BackendType;
164 } else
165#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400166#ifdef SK_METAL
167 if (0 == strcmp(str, "mtl")) {
168 return sk_app::Window::kMetal_BackendType;
169 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500170#endif
bsalomon6c471f72016-07-26 12:56:32 -0700171 if (0 == strcmp(str, "gl")) {
172 return sk_app::Window::kNativeGL_BackendType;
173 } else if (0 == strcmp(str, "sw")) {
174 return sk_app::Window::kRaster_BackendType;
175 } else {
176 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
177 return sk_app::Window::kRaster_BackendType;
178 }
179}
180
Brian Osmana109e392017-02-24 09:49:14 -0500181static SkColorSpacePrimaries gSrgbPrimaries = {
182 0.64f, 0.33f,
183 0.30f, 0.60f,
184 0.15f, 0.06f,
185 0.3127f, 0.3290f };
186
187static SkColorSpacePrimaries gAdobePrimaries = {
188 0.64f, 0.33f,
189 0.21f, 0.71f,
190 0.15f, 0.06f,
191 0.3127f, 0.3290f };
192
193static SkColorSpacePrimaries gP3Primaries = {
194 0.680f, 0.320f,
195 0.265f, 0.690f,
196 0.150f, 0.060f,
197 0.3127f, 0.3290f };
198
199static SkColorSpacePrimaries gRec2020Primaries = {
200 0.708f, 0.292f,
201 0.170f, 0.797f,
202 0.131f, 0.046f,
203 0.3127f, 0.3290f };
204
205struct NamedPrimaries {
206 const char* fName;
207 SkColorSpacePrimaries* fPrimaries;
208} gNamedPrimaries[] = {
209 { "sRGB", &gSrgbPrimaries },
210 { "AdobeRGB", &gAdobePrimaries },
211 { "P3", &gP3Primaries },
212 { "Rec. 2020", &gRec2020Primaries },
213};
214
215static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
216 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
217}
218
Brian Osman70d2f432017-11-08 09:54:10 -0500219static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
220 // In raster mode, we still use GL for the window.
221 // This lets us render the GUI faster (and correct).
222 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
223}
224
Jim Van Verth74826c82019-03-01 14:37:30 -0500225class NullSlide : public Slide {
226 SkISize getDimensions() const override {
227 return SkISize::Make(640, 480);
228 }
229
230 void draw(SkCanvas* canvas) override {
231 canvas->clear(0xffff11ff);
232 }
233};
234
liyuqiane5a6cd92016-05-27 08:52:52 -0700235const char* kName = "name";
236const char* kValue = "value";
237const char* kOptions = "options";
238const char* kSlideStateName = "Slide";
239const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700240const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700241const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700242const char* kSoftkeyStateName = "Softkey";
243const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700244const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700245const char* kON = "ON";
246const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700247const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700248
jvanverth34524262016-05-04 13:49:13 -0700249Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500250 : fCurrentSlide(-1)
251 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500252 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400253 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500254 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500255 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500256 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500257 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400258 , fZoomWindowFixed(false)
259 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500260 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400261 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700262 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500263 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500264 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500265 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500266 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700267 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400268 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400269 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400270 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500271 , fTiled(false)
272 , fDrawTileBoundaries(false)
273 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400274 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700275{
Greg Daniel285db442016-10-14 09:12:53 -0400276 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700277
Brian Osmanf09e35e2017-12-15 14:48:09 -0500278 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500279 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500280 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600281 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500282 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
283 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700284
jvanverth2bb3b6d2016-04-08 07:24:09 -0700285 SkDebugf("Command line arguments: ");
286 for (int i = 1; i < argc; ++i) {
287 SkDebugf("%s ", argv[i]);
288 }
289 SkDebugf("\n");
290
Mike Klein88544fb2019-03-20 10:50:33 -0500291 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500292#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400293 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500294#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700295
Mike Klein19cc0f62019-03-22 15:30:07 -0500296 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500297
Brian Osmanbc8150f2017-07-24 11:38:01 -0400298 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400299 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400300
bsalomon6c471f72016-07-26 12:56:32 -0700301 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700302 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700303
csmartdalton578f0642017-02-24 16:04:47 -0700304 DisplayParams displayParams;
305 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700306 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400307 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400308 displayParams.fGrContextOptions.fShaderCacheStrategy =
309 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400310 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
311 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltona1638a52019-06-24 11:54:24 -0600312 displayParams.fGrContextOptions.fInternalMultisampleCount = FLAGS_internalSamples;
csmartdalton578f0642017-02-24 16:04:47 -0700313 fWindow->setRequestedDisplayParams(displayParams);
314
Brian Osman56a24812017-12-19 11:15:16 -0500315 // Configure timers
316 fStatsLayer.setActive(false);
317 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
318 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
319 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
320
jvanverth9f372462016-04-06 06:08:59 -0700321 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700322 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500323 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500324 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500325 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700326
brianosman622c8d52016-05-10 06:50:49 -0700327 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500328 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
329 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
330 fWindow->inval();
331 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500332 // Command to jump directly to the slide picker and give it focus
333 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
334 this->fShowImGuiDebugWindow = true;
335 this->fShowSlidePicker = true;
336 fWindow->inval();
337 });
338 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400339 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500340 this->fShowImGuiDebugWindow = true;
341 this->fShowSlidePicker = true;
342 fWindow->inval();
343 });
Brian Osman79086b92017-02-10 13:36:16 -0500344 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
345 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
346 fWindow->inval();
347 });
Brian Osmanf6877092017-02-13 09:39:57 -0500348 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
349 this->fShowZoomWindow = !this->fShowZoomWindow;
350 fWindow->inval();
351 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400352 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
353 this->fZoomWindowFixed = !this->fZoomWindowFixed;
354 fWindow->inval();
355 });
Greg Danield0794cc2019-03-27 16:23:26 -0400356 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
357 DisplayParams params = fWindow->getRequestedDisplayParams();
358 params.fDisableVsync = !params.fDisableVsync;
359 fWindow->setRequestedDisplayParams(params);
360 this->updateTitle();
361 fWindow->inval();
362 });
Mike Reedf702ed42019-07-22 17:00:49 -0400363 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
364 fRefresh = !fRefresh;
365 fWindow->inval();
366 });
brianosman622c8d52016-05-10 06:50:49 -0700367 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500368 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700369 fWindow->inval();
370 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400371 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500372 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400373 this->updateTitle();
374 fWindow->inval();
375 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500376 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500377 switch (fColorMode) {
378 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500379 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500380 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500381 case ColorMode::kColorManaged8888:
382 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500383 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500384 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500385 this->setColorMode(ColorMode::kLegacy);
386 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500387 }
brianosman622c8d52016-05-10 06:50:49 -0700388 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400389 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500390 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700391 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400392 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500393 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700394 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400395 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700396 this->changeZoomLevel(1.f / 32.f);
397 fWindow->inval();
398 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400399 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700400 this->changeZoomLevel(-1.f / 32.f);
401 fWindow->inval();
402 });
jvanverthaf236b52016-05-20 06:01:06 -0700403 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400404 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
405 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500406 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400407#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
408 if (newBackend == sk_app::Window::kVulkan_BackendType) {
409 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
410 sk_app::Window::kBackendTypeCount);
411 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
412 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500413 }
414#endif
Brian Osman621491e2017-02-28 15:45:01 -0500415 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700416 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500417 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
418 fSaveToSKP = true;
419 fWindow->inval();
420 });
Mike Reed376d8122019-03-14 11:39:02 -0400421 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
422 fShowSlideDimensions = !fShowSlideDimensions;
423 fWindow->inval();
424 });
Ben Wagner37c54032018-04-13 14:30:23 -0400425 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
426 DisplayParams params = fWindow->getRequestedDisplayParams();
427 uint32_t flags = params.fSurfaceProps.flags();
428 if (!fPixelGeometryOverrides) {
429 fPixelGeometryOverrides = true;
430 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
431 } else {
432 switch (params.fSurfaceProps.pixelGeometry()) {
433 case kUnknown_SkPixelGeometry:
434 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
435 break;
436 case kRGB_H_SkPixelGeometry:
437 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
438 break;
439 case kBGR_H_SkPixelGeometry:
440 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
441 break;
442 case kRGB_V_SkPixelGeometry:
443 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
444 break;
445 case kBGR_V_SkPixelGeometry:
446 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
447 fPixelGeometryOverrides = false;
448 break;
449 }
450 }
451 fWindow->setRequestedDisplayParams(params);
452 this->updateTitle();
453 fWindow->inval();
454 });
Ben Wagner9613e452019-01-23 10:34:59 -0500455 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500456 if (!fFontOverrides.fHinting) {
457 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400458 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500459 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500460 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400461 case SkFontHinting::kNone:
462 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500463 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400464 case SkFontHinting::kSlight:
465 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500466 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400467 case SkFontHinting::kNormal:
468 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500469 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400470 case SkFontHinting::kFull:
471 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500472 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500473 break;
474 }
475 }
476 this->updateTitle();
477 fWindow->inval();
478 });
479 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500480 if (!fPaintOverrides.fAntiAlias) {
481 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
482 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500483 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500484 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500485 } else {
486 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500487 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500488 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500489 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400490 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500491 break;
492 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500493 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500494 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400495 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500496 break;
497 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500498 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400499 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500500 break;
501 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500502 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
503 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500504 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
505 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500506 break;
507 }
508 }
509 this->updateTitle();
510 fWindow->inval();
511 });
Ben Wagner37c54032018-04-13 14:30:23 -0400512 fCommands.addCommand('D', "Modes", "DFT", [this]() {
513 DisplayParams params = fWindow->getRequestedDisplayParams();
514 uint32_t flags = params.fSurfaceProps.flags();
515 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
516 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
517 fWindow->setRequestedDisplayParams(params);
518 this->updateTitle();
519 fWindow->inval();
520 });
Ben Wagner9613e452019-01-23 10:34:59 -0500521 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500522 if (!fFontOverrides.fEdging) {
523 fFontOverrides.fEdging = true;
524 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500525 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500526 switch (fFont.getEdging()) {
527 case SkFont::Edging::kAlias:
528 fFont.setEdging(SkFont::Edging::kAntiAlias);
529 break;
530 case SkFont::Edging::kAntiAlias:
531 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
532 break;
533 case SkFont::Edging::kSubpixelAntiAlias:
534 fFont.setEdging(SkFont::Edging::kAlias);
535 fFontOverrides.fEdging = false;
536 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500537 }
538 }
539 this->updateTitle();
540 fWindow->inval();
541 });
Ben Wagner9613e452019-01-23 10:34:59 -0500542 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500543 if (!fFontOverrides.fSubpixel) {
544 fFontOverrides.fSubpixel = true;
545 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500546 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500547 if (!fFont.isSubpixel()) {
548 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500549 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500550 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500551 }
552 }
553 this->updateTitle();
554 fWindow->inval();
555 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400556 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
557 if (!fFontOverrides.fBaselineSnap) {
558 fFontOverrides.fBaselineSnap = true;
559 fFont.setBaselineSnap(false);
560 } else {
561 if (!fFont.isBaselineSnap()) {
562 fFont.setBaselineSnap(true);
563 } else {
564 fFontOverrides.fBaselineSnap = false;
565 }
566 }
567 this->updateTitle();
568 fWindow->inval();
569 });
Brian Osman805a7272018-05-02 15:40:20 -0400570 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
571 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
572 : kPerspective_Real;
573 this->updateTitle();
574 fWindow->inval();
575 });
576 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
577 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
578 : kPerspective_Off;
579 this->updateTitle();
580 fWindow->inval();
581 });
Brian Osman207d4102019-01-10 09:40:58 -0500582 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
583 fAnimTimer.togglePauseResume();
584 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400585 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
586 fZoomUI = !fZoomUI;
587 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
588 fWindow->inval();
589 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500590
jvanverth2bb3b6d2016-04-08 07:24:09 -0700591 // set up slides
592 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500593 if (FLAGS_list) {
594 this->listNames();
595 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700596
Brian Osman9bb47cf2018-04-26 15:55:00 -0400597 fPerspectivePoints[0].set(0, 0);
598 fPerspectivePoints[1].set(1, 0);
599 fPerspectivePoints[2].set(0, 1);
600 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700601 fAnimTimer.run();
602
Hal Canaryc465d132017-12-08 10:21:31 -0500603 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500604 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400605 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500606 }
607 fImGuiGamutPaint.setColor(SK_ColorWHITE);
608 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
609
jongdeok.kim804f17e2019-02-26 14:39:23 +0900610 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500611 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700612}
613
jvanverth34524262016-05-04 13:49:13 -0700614void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400615 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
616 static const struct {
617 const char* fExtension;
618 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500619 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400620 const SlideFactory fFactory;
621 } gExternalSlidesInfo[] = {
622 { ".skp", "skp-dir", FLAGS_skps,
623 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
624 return sk_make_sp<SKPSlide>(name, path);}
625 },
626 { ".jpg", "jpg-dir", FLAGS_jpgs,
627 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
628 return sk_make_sp<ImageSlide>(name, path);}
629 },
Florin Malita87ccf332018-05-04 12:23:24 -0400630#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400631 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400632 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
633 return sk_make_sp<SkottieSlide>(name, path);}
634 },
Florin Malita87ccf332018-05-04 12:23:24 -0400635#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400636#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400637 { ".svg", "svg-dir", FLAGS_svgs,
638 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
639 return sk_make_sp<SvgSlide>(name, path);}
640 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400641#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400642 };
jvanverthc265a922016-04-08 12:51:45 -0700643
Brian Salomon343553a2018-09-05 15:41:23 -0400644 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700645
Mike Klein88544fb2019-03-20 10:50:33 -0500646 const auto addSlide =
647 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
648 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
649 return;
650 }
liyuqian6f163d22016-06-13 12:26:45 -0700651
Mike Klein88544fb2019-03-20 10:50:33 -0500652 if (auto slide = fact(name, path)) {
653 dirSlides.push_back(slide);
654 fSlides.push_back(std::move(slide));
655 }
656 };
Florin Malita76a076b2018-02-15 18:40:48 -0500657
Florin Malita38792ce2018-05-08 10:36:18 -0400658 if (!FLAGS_file.isEmpty()) {
659 // single file mode
660 const SkString file(FLAGS_file[0]);
661
662 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
663 for (const auto& sinfo : gExternalSlidesInfo) {
664 if (file.endsWith(sinfo.fExtension)) {
665 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
666 return;
667 }
668 }
669
670 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
671 } else {
672 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
673 }
674
675 return;
676 }
677
678 // Bisect slide.
679 if (!FLAGS_bisect.isEmpty()) {
680 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500681 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400682 if (FLAGS_bisect.count() >= 2) {
683 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
684 bisect->onChar(*ch);
685 }
686 }
687 fSlides.push_back(std::move(bisect));
688 }
689 }
690
691 // GMs
692 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400693 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400694 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500695 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400696 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400697 fSlides.push_back(std::move(slide));
698 }
Florin Malita38792ce2018-05-08 10:36:18 -0400699 }
700 // reverse gms
701 int numGMs = fSlides.count() - firstGM;
702 for (int i = 0; i < numGMs/2; ++i) {
703 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
704 }
705
706 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400707 for (const SampleFactory factory : SampleRegistry::Range()) {
708 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500709 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400710 fSlides.push_back(slide);
711 }
Florin Malita38792ce2018-05-08 10:36:18 -0400712 }
713
Brian Osman7c979f52019-02-12 13:27:51 -0500714 // Particle demo
715 {
716 // TODO: Convert this to a sample
717 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500718 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500719 fSlides.push_back(std::move(slide));
720 }
721 }
722
Florin Malita0ffa3222018-04-05 14:34:45 -0400723 for (const auto& info : gExternalSlidesInfo) {
724 for (const auto& flag : info.fFlags) {
725 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
726 // single file
727 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
728 } else {
729 // directory
730 SkOSFile::Iter it(flag.c_str(), info.fExtension);
731 SkString name;
732 while (it.next(&name)) {
733 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
734 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400735 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400736 if (!dirSlides.empty()) {
737 fSlides.push_back(
738 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
739 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500740 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400741 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400742 }
743 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500744
745 if (!fSlides.count()) {
746 sk_sp<Slide> slide(new NullSlide());
747 fSlides.push_back(std::move(slide));
748 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700749}
750
751
jvanverth34524262016-05-04 13:49:13 -0700752Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700753 fWindow->detach();
754 delete fWindow;
755}
756
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500757struct SkPaintTitleUpdater {
758 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
759 void append(const char* s) {
760 if (fCount == 0) {
761 fTitle->append(" {");
762 } else {
763 fTitle->append(", ");
764 }
765 fTitle->append(s);
766 ++fCount;
767 }
768 void done() {
769 if (fCount > 0) {
770 fTitle->append("}");
771 }
772 }
773 SkString* fTitle;
774 int fCount;
775};
776
brianosman05de2162016-05-06 13:28:57 -0700777void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700778 if (!fWindow) {
779 return;
780 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500781 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700782 return; // Surface hasn't been created yet.
783 }
784
jvanverth34524262016-05-04 13:49:13 -0700785 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700786 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700787
Mike Kleine5acd752019-03-22 09:57:16 -0500788 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400789 if (gSkForceAnalyticAA) {
790 title.append(" <FAAA>");
791 } else {
792 title.append(" <AAA>");
793 }
794 }
795
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500796 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500797 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
798 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400799 const char* on, const char* off)
800 {
Ben Wagner9613e452019-01-23 10:34:59 -0500801 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400802 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500803 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400804 };
805
Ben Wagner9613e452019-01-23 10:34:59 -0500806 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
807 const char* on, const char* off)
808 {
809 if (fFontOverrides.*flag) {
810 paintTitle.append((fFont.*isFlag)() ? on : off);
811 }
812 };
813
814 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
815 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500816 if (fPaintOverrides.fFilterQuality) {
817 switch (fPaint.getFilterQuality()) {
818 case kNone_SkFilterQuality:
819 paintTitle.append("NoFilter");
820 break;
821 case kLow_SkFilterQuality:
822 paintTitle.append("LowFilter");
823 break;
824 case kMedium_SkFilterQuality:
825 paintTitle.append("MediumFilter");
826 break;
827 case kHigh_SkFilterQuality:
828 paintTitle.append("HighFilter");
829 break;
830 }
831 }
Ben Wagner9613e452019-01-23 10:34:59 -0500832
833 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
834 "Force Autohint", "No Force Autohint");
835 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400836 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500837 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
838 "Linear Metrics", "Non-Linear Metrics");
839 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
840 "Bitmap Text", "No Bitmap Text");
841 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
842
843 if (fFontOverrides.fEdging) {
844 switch (fFont.getEdging()) {
845 case SkFont::Edging::kAlias:
846 paintTitle.append("Alias Text");
847 break;
848 case SkFont::Edging::kAntiAlias:
849 paintTitle.append("Antialias Text");
850 break;
851 case SkFont::Edging::kSubpixelAntiAlias:
852 paintTitle.append("Subpixel Antialias Text");
853 break;
854 }
855 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400856
Mike Reed3ae47332019-01-04 10:11:46 -0500857 if (fFontOverrides.fHinting) {
858 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400859 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500860 paintTitle.append("No Hinting");
861 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400862 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500863 paintTitle.append("Slight Hinting");
864 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400865 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500866 paintTitle.append("Normal Hinting");
867 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400868 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500869 paintTitle.append("Full Hinting");
870 break;
871 }
872 }
873 paintTitle.done();
874
Brian Osman92004802017-03-06 11:47:26 -0500875 switch (fColorMode) {
876 case ColorMode::kLegacy:
877 title.append(" Legacy 8888");
878 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500879 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500880 title.append(" ColorManaged 8888");
881 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500882 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500883 title.append(" ColorManaged F16");
884 break;
885 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500886
Brian Osman92004802017-03-06 11:47:26 -0500887 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500888 int curPrimaries = -1;
889 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
890 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
891 curPrimaries = i;
892 break;
893 }
894 }
Brian Osman03115dc2018-11-26 13:55:19 -0500895 title.appendf(" %s Gamma %f",
896 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500897 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700898 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500899
Ben Wagner37c54032018-04-13 14:30:23 -0400900 const DisplayParams& params = fWindow->getRequestedDisplayParams();
901 if (fPixelGeometryOverrides) {
902 switch (params.fSurfaceProps.pixelGeometry()) {
903 case kUnknown_SkPixelGeometry:
904 title.append( " Flat");
905 break;
906 case kRGB_H_SkPixelGeometry:
907 title.append( " RGB");
908 break;
909 case kBGR_H_SkPixelGeometry:
910 title.append( " BGR");
911 break;
912 case kRGB_V_SkPixelGeometry:
913 title.append( " RGBV");
914 break;
915 case kBGR_V_SkPixelGeometry:
916 title.append( " BGRV");
917 break;
918 }
919 }
920
921 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
922 title.append(" DFT");
923 }
924
csmartdalton578f0642017-02-24 16:04:47 -0700925 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700926 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500927 int msaa = fWindow->sampleCount();
928 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700929 title.appendf(" MSAA: %i", msaa);
930 }
931 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700932
933 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600934 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700935 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
936 }
937
Brian Osman805a7272018-05-02 15:40:20 -0400938 if (kPerspective_Real == fPerspectiveMode) {
939 title.append(" Perpsective (Real)");
940 } else if (kPerspective_Fake == fPerspectiveMode) {
941 title.append(" Perspective (Fake)");
942 }
943
brianosman05de2162016-05-06 13:28:57 -0700944 fWindow->setTitle(title.c_str());
945}
946
Florin Malitaab99c342018-01-16 16:23:03 -0500947int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500948
949 if (!FLAGS_slide.isEmpty()) {
950 int count = fSlides.count();
951 for (int i = 0; i < count; i++) {
952 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500953 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500954 }
955 }
956
957 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
958 this->listNames();
959 }
960
Florin Malitaab99c342018-01-16 16:23:03 -0500961 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500962}
963
Florin Malitaab99c342018-01-16 16:23:03 -0500964void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500965 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500966 for (const auto& slide : fSlides) {
967 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500968 }
969}
970
Florin Malitaab99c342018-01-16 16:23:03 -0500971void Viewer::setCurrentSlide(int slide) {
972 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700973
Florin Malitaab99c342018-01-16 16:23:03 -0500974 if (slide == fCurrentSlide) {
975 return;
976 }
977
978 if (fCurrentSlide >= 0) {
979 fSlides[fCurrentSlide]->unload();
980 }
981
982 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
983 SkIntToScalar(fWindow->height()));
984 fCurrentSlide = slide;
985 this->setupCurrentSlide();
986}
987
988void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500989 if (fCurrentSlide >= 0) {
990 // prepare dimensions for image slides
991 fGesture.resetTouchState();
992 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700993
Jim Van Verth0848fb02018-01-22 13:39:30 -0500994 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
995 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
996 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400997
Jim Van Verth0848fb02018-01-22 13:39:30 -0500998 // Start with a matrix that scales the slide to the available screen space
999 if (fWindow->scaleContentToFit()) {
1000 if (windowRect.width() > 0 && windowRect.height() > 0) {
1001 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1002 }
liyuqiane46e4f02016-05-20 07:32:19 -07001003 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001004
1005 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001006 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001007
1008 this->updateTitle();
1009 this->updateUIState();
1010
1011 fStatsLayer.resetMeasurements();
1012
1013 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001014 }
jvanverthc265a922016-04-08 12:51:45 -07001015}
1016
1017#define MAX_ZOOM_LEVEL 8
1018#define MIN_ZOOM_LEVEL -8
1019
jvanverth34524262016-05-04 13:49:13 -07001020void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001021 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -04001022 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001023 this->preTouchMatrixChanged();
1024}
Yuqian Li755778c2018-03-28 16:23:31 -04001025
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001026void Viewer::preTouchMatrixChanged() {
1027 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001028 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1029 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1030 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1031 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1032}
1033
Brian Osman805a7272018-05-02 15:40:20 -04001034SkMatrix Viewer::computePerspectiveMatrix() {
1035 SkScalar w = fWindow->width(), h = fWindow->height();
1036 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1037 SkPoint perspPts[4] = {
1038 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1039 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1040 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1041 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1042 };
1043 SkMatrix m;
1044 m.setPolyToPoly(orthoPts, perspPts, 4);
1045 return m;
1046}
1047
Yuqian Li755778c2018-03-28 16:23:31 -04001048SkMatrix Viewer::computePreTouchMatrix() {
1049 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001050
1051 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001052 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001053 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001054
1055 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1056 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001057
Brian Osman805a7272018-05-02 15:40:20 -04001058 if (kPerspective_Real == fPerspectiveMode) {
1059 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001060 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001061 }
1062
Yuqian Li755778c2018-03-28 16:23:31 -04001063 return m;
jvanverthc265a922016-04-08 12:51:45 -07001064}
1065
liyuqiand3cdbca2016-05-17 12:44:20 -07001066SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001067 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001068 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001069 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001070 return m;
jvanverthc265a922016-04-08 12:51:45 -07001071}
1072
Brian Osman621491e2017-02-28 15:45:01 -05001073void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001074 fPersistentCache.reset();
1075 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001076 fBackendType = backendType;
1077
1078 fWindow->detach();
1079
Brian Osman70d2f432017-11-08 09:54:10 -05001080#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001081 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1082 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001083 DisplayParams params = fWindow->getRequestedDisplayParams();
1084 delete fWindow;
1085 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001086
Brian Osman70d2f432017-11-08 09:54:10 -05001087 // re-register callbacks
1088 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001089 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001090 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001091 fWindow->pushLayer(&fImGuiLayer);
1092
Brian Osman70d2f432017-11-08 09:54:10 -05001093 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1094 // will still include our correct sample count. But the re-created fWindow will lose that
1095 // information. On Windows, we need to re-create the window when changing sample count,
1096 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1097 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1098 // as if we had never changed windows at all).
1099 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001100#endif
1101
Brian Osman70d2f432017-11-08 09:54:10 -05001102 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001103}
1104
Brian Osman92004802017-03-06 11:47:26 -05001105void Viewer::setColorMode(ColorMode colorMode) {
1106 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001107 this->updateTitle();
1108 fWindow->inval();
1109}
1110
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001111class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1112public:
Mike Reed3ae47332019-01-04 10:11:46 -05001113 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1114 SkFont* font, Viewer::SkFontFields* ffields)
1115 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001116 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001117 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1118 sk_sp<SkTextBlob>* cache) {
1119 bool blobWillChange = false;
1120 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001121 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1122 bool shouldDraw = this->filterFont(&filteredFont);
1123 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001124 blobWillChange = true;
1125 break;
1126 }
1127 }
1128 if (!blobWillChange) {
1129 return blob;
1130 }
1131
1132 SkTextBlobBuilder builder;
1133 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001134 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1135 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001136 if (!shouldDraw) {
1137 continue;
1138 }
1139
Mike Reed3ae47332019-01-04 10:11:46 -05001140 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001141
Ben Wagner41e40472018-09-24 13:01:54 -04001142 const SkTextBlobBuilder::RunBuffer& runBuffer
1143 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001144 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001145 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001146 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001147 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001148 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001149 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001150 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001151 it.glyphCount(), it.textSize(), SkString())
1152 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1153 uint32_t glyphCount = it.glyphCount();
1154 if (it.glyphs()) {
1155 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1156 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1157 }
1158 if (it.pos()) {
1159 size_t posSize = sizeof(decltype(*it.pos()));
1160 uint8_t positioning = it.positioning();
1161 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1162 }
1163 if (it.text()) {
1164 size_t textSize = sizeof(decltype(*it.text()));
1165 uint32_t textCount = it.textSize();
1166 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1167 }
1168 if (it.clusters()) {
1169 size_t clusterSize = sizeof(decltype(*it.clusters()));
1170 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1171 }
1172 }
1173 *cache = builder.make();
1174 return cache->get();
1175 }
1176 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1177 const SkPaint& paint) override {
1178 sk_sp<SkTextBlob> cache;
1179 this->SkPaintFilterCanvas::onDrawTextBlob(
1180 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1181 }
Mike Reed3ae47332019-01-04 10:11:46 -05001182 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001183 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001184 font->writable()->setSize(fFont->getSize());
1185 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001186 if (fFontOverrides->fScaleX) {
1187 font->writable()->setScaleX(fFont->getScaleX());
1188 }
1189 if (fFontOverrides->fSkewX) {
1190 font->writable()->setSkewX(fFont->getSkewX());
1191 }
Mike Reed3ae47332019-01-04 10:11:46 -05001192 if (fFontOverrides->fHinting) {
1193 font->writable()->setHinting(fFont->getHinting());
1194 }
Ben Wagner9613e452019-01-23 10:34:59 -05001195 if (fFontOverrides->fEdging) {
1196 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001197 }
Ben Wagner9613e452019-01-23 10:34:59 -05001198 if (fFontOverrides->fEmbolden) {
1199 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001200 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001201 if (fFontOverrides->fBaselineSnap) {
1202 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1203 }
Ben Wagner9613e452019-01-23 10:34:59 -05001204 if (fFontOverrides->fLinearMetrics) {
1205 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001206 }
Ben Wagner9613e452019-01-23 10:34:59 -05001207 if (fFontOverrides->fSubpixel) {
1208 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001209 }
Ben Wagner9613e452019-01-23 10:34:59 -05001210 if (fFontOverrides->fEmbeddedBitmaps) {
1211 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001212 }
Ben Wagner9613e452019-01-23 10:34:59 -05001213 if (fFontOverrides->fForceAutoHinting) {
1214 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001215 }
Ben Wagner9613e452019-01-23 10:34:59 -05001216
Mike Reed3ae47332019-01-04 10:11:46 -05001217 return true;
1218 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001219 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001220 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001221 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001222 }
Ben Wagner9613e452019-01-23 10:34:59 -05001223 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001224 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001225 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001226 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001227 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001228 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001229 return true;
1230 }
1231 SkPaint* fPaint;
1232 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001233 SkFont* fFont;
1234 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001235};
1236
Robert Phillips9882dae2019-03-04 11:00:10 -05001237void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001238 if (fCurrentSlide < 0) {
1239 return;
1240 }
1241
Robert Phillips9882dae2019-03-04 11:00:10 -05001242 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001243
Brian Osmanf750fbc2017-02-08 10:47:28 -05001244 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001245 SkSurface* slideSurface = surface;
1246 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001247 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001248
Brian Osmane0d4fba2017-03-15 10:24:55 -04001249 // 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 -05001250 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001251 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001252 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001253 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001254 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001255 }
1256
Brian Osman3ac99cf2017-12-01 11:23:53 -05001257 if (fSaveToSKP) {
1258 SkPictureRecorder recorder;
1259 SkCanvas* recorderCanvas = recorder.beginRecording(
1260 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001261 fSlides[fCurrentSlide]->draw(recorderCanvas);
1262 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1263 SkFILEWStream stream("sample_app.skp");
1264 picture->serialize(&stream);
1265 fSaveToSKP = false;
1266 }
1267
Brian Osmane9ed0f02018-11-26 14:50:05 -05001268 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
1269 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode) ? kRGBA_F16_SkColorType
1270 : kN32_SkColorType;
Brian Osmane9ed0f02018-11-26 14:50:05 -05001271
1272 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001273 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1274 slideCanvas->getProps(&props);
1275
Brian Osmane9ed0f02018-11-26 14:50:05 -05001276 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1277 return Window::kRaster_BackendType == this->fBackendType
1278 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001279 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001280 };
1281
Brian Osman03115dc2018-11-26 13:55:19 -05001282 // We need to render offscreen if we're...
1283 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1284 // ... in any raster mode, because the window surface is actually GL
1285 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001286 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001287 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001288 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001289 Window::kRaster_BackendType == fBackendType ||
1290 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001291
Brian Osmane9ed0f02018-11-26 14:50:05 -05001292 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001293 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001294 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001295 }
1296
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001297 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001298 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001299 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001300 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001301 if (fTiled) {
1302 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1303 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1304 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1305 SkCanvas* tileCanvas = tileSurface->getCanvas();
1306 SkMatrix m = this->computeMatrix();
1307 for (int y = 0; y < fWindow->height(); y += tileH) {
1308 for (int x = 0; x < fWindow->width(); x += tileW) {
1309 SkAutoCanvasRestore acr(tileCanvas, true);
1310 tileCanvas->translate(-x, -y);
1311 tileCanvas->clear(SK_ColorTRANSPARENT);
1312 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001313 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1314 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001315 fSlides[fCurrentSlide]->draw(&filterCanvas);
1316 tileSurface->draw(slideCanvas, x, y, nullptr);
1317 }
1318 }
1319
1320 // Draw borders between tiles
1321 if (fDrawTileBoundaries) {
1322 SkPaint border;
1323 border.setColor(0x60FF00FF);
1324 border.setStyle(SkPaint::kStroke_Style);
1325 for (int y = 0; y < fWindow->height(); y += tileH) {
1326 for (int x = 0; x < fWindow->width(); x += tileW) {
1327 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1328 }
1329 }
1330 }
1331 } else {
1332 slideCanvas->concat(this->computeMatrix());
1333 if (kPerspective_Real == fPerspectiveMode) {
1334 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1335 }
Mike Reed3ae47332019-01-04 10:11:46 -05001336 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001337 fSlides[fCurrentSlide]->draw(&filterCanvas);
1338 }
Brian Osman56a24812017-12-19 11:15:16 -05001339 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001340 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001341
1342 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001343 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001344 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001345 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001346
1347 // If we rendered offscreen, snap an image and push the results to the window's canvas
1348 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001349 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001350
Robert Phillips9882dae2019-03-04 11:00:10 -05001351 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001352 SkPaint paint;
1353 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001354 int prePerspectiveCount = canvas->save();
1355 if (kPerspective_Fake == fPerspectiveMode) {
1356 paint.setFilterQuality(kHigh_SkFilterQuality);
1357 canvas->clear(SK_ColorWHITE);
1358 canvas->concat(this->computePerspectiveMatrix());
1359 }
Brian Osman03115dc2018-11-26 13:55:19 -05001360 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001361 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001362 }
Mike Reed376d8122019-03-14 11:39:02 -04001363
1364 if (fShowSlideDimensions) {
1365 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1366 SkPaint paint;
1367 paint.setColor(0x40FFFF00);
1368 surface->getCanvas()->drawRect(r, paint);
1369 }
liyuqian6f163d22016-06-13 12:26:45 -07001370}
1371
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001372void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001373 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001374 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001375}
Jim Van Verth6f449692017-02-14 15:16:46 -05001376
Robert Phillips9882dae2019-03-04 11:00:10 -05001377void Viewer::onPaint(SkSurface* surface) {
1378 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001379
Robert Phillips9882dae2019-03-04 11:00:10 -05001380 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001381
Brian Osmand67e5182017-12-08 16:46:09 -05001382 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001383
1384 if (GrContext* ctx = fWindow->getGrContext()) {
1385 // Clean out cache items that haven't been used in more than 10 seconds.
1386 ctx->performDeferredCleanup(std::chrono::seconds(10));
1387 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001388}
1389
Ben Wagnera1915972018-08-09 15:06:19 -04001390void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001391 if (fCurrentSlide >= 0) {
1392 fSlides[fCurrentSlide]->resize(width, height);
1393 }
Ben Wagnera1915972018-08-09 15:06:19 -04001394}
1395
Florin Malitacefc1b92018-02-19 21:43:47 -05001396SkPoint Viewer::mapEvent(float x, float y) {
1397 const auto m = this->computeMatrix();
1398 SkMatrix inv;
1399
1400 SkAssertResult(m.invert(&inv));
1401
1402 return inv.mapXY(x, y);
1403}
1404
Hal Canaryb1f411a2019-08-29 10:39:22 -04001405bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001406 if (GestureDevice::kMouse == fGestureDevice) {
1407 return false;
1408 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001409
1410 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001411 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001412 fWindow->inval();
1413 return true;
1414 }
1415
liyuqiand3cdbca2016-05-17 12:44:20 -07001416 void* castedOwner = reinterpret_cast<void*>(owner);
1417 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001418 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001419 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001420#if defined(SK_BUILD_FOR_IOS)
1421 // TODO: move IOS swipe detection higher up into the platform code
1422 SkPoint dir;
1423 if (fGesture.isFling(&dir)) {
1424 // swiping left or right
1425 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1426 if (dir.fX < 0) {
1427 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1428 fCurrentSlide + 1 : 0);
1429 } else {
1430 this->setCurrentSlide(fCurrentSlide > 0 ?
1431 fCurrentSlide - 1 : fSlides.count() - 1);
1432 }
1433 }
1434 fGesture.reset();
1435 }
1436#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001437 break;
1438 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001439 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001440 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001441 break;
1442 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001443 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001444 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001445 break;
1446 }
1447 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001448 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001449 fWindow->inval();
1450 return true;
1451}
1452
Hal Canaryb1f411a2019-08-29 10:39:22 -04001453bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001454 if (GestureDevice::kTouch == fGestureDevice) {
1455 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001456 }
Brian Osman16c81a12017-12-20 11:58:34 -05001457
Florin Malitacefc1b92018-02-19 21:43:47 -05001458 const auto slidePt = this->mapEvent(x, y);
1459 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1460 fWindow->inval();
1461 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001462 }
1463
1464 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001465 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001466 fGesture.touchEnd(nullptr);
1467 break;
1468 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001469 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001470 fGesture.touchBegin(nullptr, x, y);
1471 break;
1472 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001473 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001474 fGesture.touchMoved(nullptr, x, y);
1475 break;
1476 }
1477 }
1478 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1479
Hal Canaryb1f411a2019-08-29 10:39:22 -04001480 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001481 fWindow->inval();
1482 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001483 return true;
1484}
1485
Brian Osmana109e392017-02-24 09:49:14 -05001486static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001487 // The gamut image covers a (0.8 x 0.9) shaped region
1488 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001489
1490 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1491 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1492 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001493 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1494 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1495 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001496
Brian Osman535c5e32019-02-09 16:32:58 -05001497 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1498 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1499 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1500 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1501 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001502}
1503
Ben Wagner3627d2e2018-06-26 14:23:20 -04001504static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001505 ImGui::DragCanvas dc(pt);
1506 dc.fillColor(IM_COL32(0, 0, 0, 128));
1507 dc.dragPoint(pt);
1508 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001509}
1510
Brian Osman9bb47cf2018-04-26 15:55:00 -04001511static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001512 ImGui::DragCanvas dc(pts);
1513 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001514
Brian Osman535c5e32019-02-09 16:32:58 -05001515 for (int i = 0; i < 4; ++i) {
1516 dc.dragPoint(pts + i);
1517 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001518
Brian Osman535c5e32019-02-09 16:32:58 -05001519 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1520 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1521 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1522 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001523
Brian Osman535c5e32019-02-09 16:32:58 -05001524 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001525}
1526
Brian Osmand67e5182017-12-08 16:46:09 -05001527void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001528 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1529 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001530 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001531 }
1532
1533 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001534 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1535 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001536 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001537 DisplayParams params = fWindow->getRequestedDisplayParams();
1538 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001539 const GrContext* ctx = fWindow->getGrContext();
1540
Brian Osmana109e392017-02-24 09:49:14 -05001541 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1542 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001543 if (ImGui::CollapsingHeader("Backend")) {
1544 int newBackend = static_cast<int>(fBackendType);
1545 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1546 ImGui::SameLine();
1547 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001548#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1549 ImGui::SameLine();
1550 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1551#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001552#if defined(SK_DAWN)
1553 ImGui::SameLine();
1554 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1555#endif
Brian Osman621491e2017-02-28 15:45:01 -05001556#if defined(SK_VULKAN)
1557 ImGui::SameLine();
1558 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1559#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001560#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001561 ImGui::SameLine();
1562 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1563#endif
Brian Osman621491e2017-02-28 15:45:01 -05001564 if (newBackend != fBackendType) {
1565 fDeferredActions.push_back([=]() {
1566 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1567 });
1568 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001569
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001570 bool* wire = &params.fGrContextOptions.fWireframeMode;
1571 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1572 paramsChanged = true;
1573 }
Brian Salomon99a33902017-03-07 15:16:34 -05001574
Brian Osman28b12522017-03-08 17:10:24 -05001575 if (ctx) {
1576 int sampleCount = fWindow->sampleCount();
1577 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001578 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001579 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1580 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1581 ImGui::RadioButton("16", &sampleCount, 16);
1582
1583 if (sampleCount != params.fMSAASampleCount) {
1584 params.fMSAASampleCount = sampleCount;
1585 paramsChanged = true;
1586 }
1587 }
1588
Ben Wagner37c54032018-04-13 14:30:23 -04001589 int pixelGeometryIdx = 0;
1590 if (fPixelGeometryOverrides) {
1591 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1592 }
1593 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1594 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1595 {
1596 uint32_t flags = params.fSurfaceProps.flags();
1597 if (pixelGeometryIdx == 0) {
1598 fPixelGeometryOverrides = false;
1599 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1600 } else {
1601 fPixelGeometryOverrides = true;
1602 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1603 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1604 }
1605 paramsChanged = true;
1606 }
1607
1608 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1609 if (ImGui::Checkbox("DFT", &useDFT)) {
1610 uint32_t flags = params.fSurfaceProps.flags();
1611 if (useDFT) {
1612 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1613 } else {
1614 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1615 }
1616 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1617 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1618 paramsChanged = true;
1619 }
1620
Brian Osman8a9de3d2017-03-01 14:59:05 -05001621 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001622 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001623 auto prButton = [&](GpuPathRenderers x) {
1624 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001625 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1626 params.fGrContextOptions.fGpuPathRenderers = x;
1627 paramsChanged = true;
1628 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001629 }
1630 };
1631
1632 if (!ctx) {
1633 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001634 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001635 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001636 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001637 prButton(GpuPathRenderers::kStencilAndCover);
1638 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001639 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001640 prButton(GpuPathRenderers::kNone);
1641 } else {
1642 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001643 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001644 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001645 prButton(GpuPathRenderers::kCoverageCounting);
1646 }
Jim Van Verth83010462017-03-16 08:45:39 -04001647 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001648 prButton(GpuPathRenderers::kTessellating);
1649 prButton(GpuPathRenderers::kNone);
1650 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001651 ImGui::TreePop();
1652 }
Brian Osman621491e2017-02-28 15:45:01 -05001653 }
1654
Ben Wagner964571d2019-03-08 12:35:06 -05001655 if (ImGui::CollapsingHeader("Tiling")) {
1656 ImGui::Checkbox("Enable", &fTiled);
1657 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1658 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1659 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1660 }
1661
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001662 if (ImGui::CollapsingHeader("Transform")) {
1663 float zoom = fZoomLevel;
1664 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1665 fZoomLevel = zoom;
1666 this->preTouchMatrixChanged();
1667 paramsChanged = true;
1668 }
1669 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001670 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001671 fRotation = deg;
1672 this->preTouchMatrixChanged();
1673 paramsChanged = true;
1674 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001675 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1676 if (ImGui_DragLocation(&fOffset)) {
1677 this->preTouchMatrixChanged();
1678 paramsChanged = true;
1679 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001680 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1681 this->preTouchMatrixChanged();
1682 paramsChanged = true;
1683 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001684 }
Brian Osman805a7272018-05-02 15:40:20 -04001685 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1686 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1687 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001688 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001689 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001690 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001691 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001692 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001693 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001694 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001695 }
1696
Ben Wagnera580fb32018-04-17 11:16:32 -04001697 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001698 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001699 if (fPaintOverrides.fAntiAlias) {
1700 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001701 }
1702 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001703 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001704 {
1705 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1706 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001707 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001708 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1709 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001710 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001711 fPaintOverrides.fAntiAlias = true;
1712 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001713 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001714 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001715 case SkPaintFields::AntiAliasState::Alias:
1716 break;
1717 case SkPaintFields::AntiAliasState::Normal:
1718 break;
1719 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1720 gSkUseAnalyticAA = true;
1721 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001722 break;
1723 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1724 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001725 break;
1726 }
1727 }
1728 paramsChanged = true;
1729 }
1730
Ben Wagner99a78dc2018-05-09 18:23:51 -04001731 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001732 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001733 bool (SkPaint::* isFlag)() const,
1734 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001735 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001736 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001737 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001738 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001739 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001740 if (ImGui::Combo(label, &itemIndex, items)) {
1741 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001742 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001743 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001744 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001745 (fPaint.*setFlag)(itemIndex == 2);
1746 }
1747 paramsChanged = true;
1748 }
1749 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001750
Ben Wagner99a78dc2018-05-09 18:23:51 -04001751 paintFlag("Dither",
1752 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001753 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001754 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001755
1756 int filterQualityIdx = 0;
1757 if (fPaintOverrides.fFilterQuality) {
1758 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1759 }
1760 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1761 "Default\0None\0Low\0Medium\0High\0\0"))
1762 {
1763 if (filterQualityIdx == 0) {
1764 fPaintOverrides.fFilterQuality = false;
1765 fPaint.setFilterQuality(kNone_SkFilterQuality);
1766 } else {
1767 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1768 fPaintOverrides.fFilterQuality = true;
1769 }
1770 paramsChanged = true;
1771 }
Ben Wagner9613e452019-01-23 10:34:59 -05001772 }
Hal Canary02738a82019-01-21 18:51:32 +00001773
Ben Wagner9613e452019-01-23 10:34:59 -05001774 if (ImGui::CollapsingHeader("Font")) {
1775 int hintingIdx = 0;
1776 if (fFontOverrides.fHinting) {
1777 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1778 }
1779 if (ImGui::Combo("Hinting", &hintingIdx,
1780 "Default\0None\0Slight\0Normal\0Full\0\0"))
1781 {
1782 if (hintingIdx == 0) {
1783 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001784 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001785 } else {
1786 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1787 fFontOverrides.fHinting = true;
1788 }
1789 paramsChanged = true;
1790 }
Hal Canary02738a82019-01-21 18:51:32 +00001791
Ben Wagner9613e452019-01-23 10:34:59 -05001792 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1793 bool SkFontFields::* flag,
1794 bool (SkFont::* isFlag)() const,
1795 void (SkFont::* setFlag)(bool) )
1796 {
1797 int itemIndex = 0;
1798 if (fFontOverrides.*flag) {
1799 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1800 }
1801 if (ImGui::Combo(label, &itemIndex, items)) {
1802 if (itemIndex == 0) {
1803 fFontOverrides.*flag = false;
1804 } else {
1805 fFontOverrides.*flag = true;
1806 (fFont.*setFlag)(itemIndex == 2);
1807 }
1808 paramsChanged = true;
1809 }
1810 };
Hal Canary02738a82019-01-21 18:51:32 +00001811
Ben Wagner9613e452019-01-23 10:34:59 -05001812 fontFlag("Fake Bold Glyphs",
1813 "Default\0No Fake Bold\0Fake Bold\0\0",
1814 &SkFontFields::fEmbolden,
1815 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001816
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001817 fontFlag("Baseline Snapping",
1818 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1819 &SkFontFields::fBaselineSnap,
1820 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1821
Ben Wagner9613e452019-01-23 10:34:59 -05001822 fontFlag("Linear Text",
1823 "Default\0No Linear Text\0Linear Text\0\0",
1824 &SkFontFields::fLinearMetrics,
1825 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001826
Ben Wagner9613e452019-01-23 10:34:59 -05001827 fontFlag("Subpixel Position Glyphs",
1828 "Default\0Pixel Text\0Subpixel Text\0\0",
1829 &SkFontFields::fSubpixel,
1830 &SkFont::isSubpixel, &SkFont::setSubpixel);
1831
1832 fontFlag("Embedded Bitmap Text",
1833 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1834 &SkFontFields::fEmbeddedBitmaps,
1835 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1836
1837 fontFlag("Force Auto-Hinting",
1838 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1839 &SkFontFields::fForceAutoHinting,
1840 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1841
1842 int edgingIdx = 0;
1843 if (fFontOverrides.fEdging) {
1844 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1845 }
1846 if (ImGui::Combo("Edging", &edgingIdx,
1847 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1848 {
1849 if (edgingIdx == 0) {
1850 fFontOverrides.fEdging = false;
1851 fFont.setEdging(SkFont::Edging::kAlias);
1852 } else {
1853 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1854 fFontOverrides.fEdging = true;
1855 }
1856 paramsChanged = true;
1857 }
1858
Ben Wagner15a8d572019-03-21 13:35:44 -04001859 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1860 if (fFontOverrides.fSize) {
1861 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001862 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001863 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001864 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001865 fFontOverrides.fSizeRange[0],
1866 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001867 "%.6f", 2.0f))
1868 {
Mike Reed3ae47332019-01-04 10:11:46 -05001869 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001870 paramsChanged = true;
1871 }
1872 }
1873
1874 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1875 if (fFontOverrides.fScaleX) {
1876 float scaleX = fFont.getScaleX();
1877 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1878 fFont.setScaleX(scaleX);
1879 paramsChanged = true;
1880 }
1881 }
1882
1883 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1884 if (fFontOverrides.fSkewX) {
1885 float skewX = fFont.getSkewX();
1886 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1887 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001888 paramsChanged = true;
1889 }
1890 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001891 }
1892
Mike Reed81f60ec2018-05-15 10:09:52 -04001893 {
1894 SkMetaData controls;
1895 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1896 if (ImGui::CollapsingHeader("Current Slide")) {
1897 SkMetaData::Iter iter(controls);
1898 const char* name;
1899 SkMetaData::Type type;
1900 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001901 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001902 if (type == SkMetaData::kScalar_Type) {
1903 float val[3];
1904 SkASSERT(count == 3);
1905 controls.findScalars(name, &count, val);
1906 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1907 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001908 }
Ben Wagner110c7032019-03-22 17:03:59 -04001909 } else if (type == SkMetaData::kBool_Type) {
1910 bool val;
1911 SkASSERT(count == 1);
1912 controls.findBool(name, &val);
1913 if (ImGui::Checkbox(name, &val)) {
1914 controls.setBool(name, val);
1915 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001916 }
1917 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001918 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001919 }
1920 }
1921 }
1922
Ben Wagner7a3c6742018-04-23 10:01:07 -04001923 if (fShowSlidePicker) {
1924 ImGui::SetNextTreeNodeOpen(true);
1925 }
Brian Osman79086b92017-02-10 13:36:16 -05001926 if (ImGui::CollapsingHeader("Slide")) {
1927 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001928 static ImVector<const char*> filteredSlideNames;
1929 static ImVector<int> filteredSlideIndices;
1930
Brian Osmanfce09c52017-11-14 15:32:20 -05001931 if (fShowSlidePicker) {
1932 ImGui::SetKeyboardFocusHere();
1933 fShowSlidePicker = false;
1934 }
1935
Brian Osman79086b92017-02-10 13:36:16 -05001936 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001937 filteredSlideNames.clear();
1938 filteredSlideIndices.clear();
1939 int filteredIndex = 0;
1940 for (int i = 0; i < fSlides.count(); ++i) {
1941 const char* slideName = fSlides[i]->getName().c_str();
1942 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1943 if (i == fCurrentSlide) {
1944 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001945 }
Brian Osmanf479e422017-11-08 13:11:36 -05001946 filteredSlideNames.push_back(slideName);
1947 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001948 }
Brian Osman79086b92017-02-10 13:36:16 -05001949 }
Brian Osmanf479e422017-11-08 13:11:36 -05001950
Brian Osmanf479e422017-11-08 13:11:36 -05001951 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1952 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001953 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001954 }
1955 }
Brian Osmana109e392017-02-24 09:49:14 -05001956
1957 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001958 ColorMode newMode = fColorMode;
1959 auto cmButton = [&](ColorMode mode, const char* label) {
1960 if (ImGui::RadioButton(label, mode == fColorMode)) {
1961 newMode = mode;
1962 }
1963 };
1964
1965 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001966 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1967 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001968
1969 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001970 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001971 }
1972
1973 // Pick from common gamuts:
1974 int primariesIdx = 4; // Default: Custom
1975 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1976 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1977 primariesIdx = i;
1978 break;
1979 }
1980 }
1981
Brian Osman03115dc2018-11-26 13:55:19 -05001982 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05001983 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001984
Brian Osmana109e392017-02-24 09:49:14 -05001985 if (ImGui::Combo("Primaries", &primariesIdx,
1986 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1987 if (primariesIdx >= 0 && primariesIdx <= 3) {
1988 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1989 }
1990 }
1991
1992 // Allow direct editing of gamut
1993 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1994 }
Brian Osman207d4102019-01-10 09:40:58 -05001995
1996 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04001997 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05001998 if (ImGui::Checkbox("Pause", &isPaused)) {
1999 fAnimTimer.togglePauseResume();
2000 }
Brian Osman707d2022019-01-10 11:27:34 -05002001
2002 float speed = fAnimTimer.getSpeed();
2003 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2004 fAnimTimer.setSpeed(speed);
2005 }
Brian Osman207d4102019-01-10 09:40:58 -05002006 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002007
Brian Osmanfd7657c2019-04-25 11:34:07 -04002008 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2009#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2010 || Window::kANGLE_BackendType == fBackendType
2011#endif
2012 ;
2013
2014 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2015 // than GL, we need to force it on. Just do that on the first frame after the backend
2016 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002017 if (!backendIsGL &&
2018 params.fGrContextOptions.fShaderCacheStrategy !=
2019 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2020 params.fGrContextOptions.fShaderCacheStrategy =
2021 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002022 paramsChanged = true;
2023 fPersistentCache.reset();
2024 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002025 // To re-load shaders from the currently active programs, we flush all caches on one
2026 // frame, then set a flag to poll the cache on the next frame.
2027 static bool gLoadPending = false;
2028 if (gLoadPending) {
2029 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2030 int hitCount) {
2031 CachedGLSL& entry(fCachedGLSL.push_back());
2032 entry.fKey = key;
2033 SkMD5 hash;
2034 hash.write(key->bytes(), key->size());
2035 SkMD5::Digest digest = hash.finish();
2036 for (int i = 0; i < 16; ++i) {
2037 entry.fKeyString.appendf("%02x", digest.data[i]);
2038 }
2039
Brian Osmana66081d2019-09-03 14:59:26 -04002040 SkReader32 reader(data->data(), data->size());
2041 entry.fShaderType = reader.readU32();
2042 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2043 entry.fInputs,
2044 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002045 };
2046 fCachedGLSL.reset();
2047 fPersistentCache.foreach(collectShaders);
2048 gLoadPending = false;
2049 }
2050
2051 // Defer actually doing the load/save logic so that we can trigger a save when we
2052 // start or finish hovering on a tree node in the list below:
2053 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002054 bool doSave = ImGui::Button("Save");
2055 if (backendIsGL) {
2056 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002057 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2058 GrContextOptions::ShaderCacheStrategy::kSkSL;
2059 if (ImGui::Checkbox("SkSL", &sksl)) {
2060 params.fGrContextOptions.fShaderCacheStrategy = sksl
2061 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2062 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002063 paramsChanged = true;
2064 doLoad = true;
2065 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2066 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002067 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002068
2069 ImGui::BeginChild("##ScrollingRegion");
2070 for (auto& entry : fCachedGLSL) {
2071 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2072 bool hovered = ImGui::IsItemHovered();
2073 if (hovered != entry.fHovered) {
2074 // Force a save to patch the highlight shader in/out
2075 entry.fHovered = hovered;
2076 doSave = true;
2077 }
2078 if (inTreeNode) {
2079 // Full width, and a reasonable amount of space for each shader.
2080 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2081 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2082 boxSize);
2083 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2084 boxSize);
2085 ImGui::TreePop();
2086 }
2087 }
2088 ImGui::EndChild();
2089
2090 if (doLoad) {
2091 fPersistentCache.reset();
2092 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2093 gLoadPending = true;
2094 }
2095 if (doSave) {
2096 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002097 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002098 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2099 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002100
Brian Osman072e6fc2019-06-12 11:35:41 -04002101 SkSL::String highlight;
2102 if (!sksl) {
2103 highlight = shaderCaps->versionDeclString();
2104 if (shaderCaps->usesPrecisionModifiers()) {
2105 highlight.append("precision mediump float;\n");
2106 }
Brian Osman5bee3902019-05-07 09:55:45 -04002107 }
2108 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002109 highlight.appendf("out %s sk_FragColor;\n"
2110 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2111 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002112
2113 fPersistentCache.reset();
2114 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2115 for (auto& entry : fCachedGLSL) {
2116 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2117 if (entry.fHovered) {
2118 entry.fShader[kFragment_GrShaderType] = highlight;
2119 }
2120
Brian Osmana085a412019-04-25 09:44:43 -04002121 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2122 entry.fShader,
2123 entry.fInputs,
2124 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002125 fPersistentCache.store(*entry.fKey, *data);
2126
2127 entry.fShader[kFragment_GrShaderType] = backup;
2128 }
2129 }
2130 }
Brian Osman79086b92017-02-10 13:36:16 -05002131 }
Brian Salomon99a33902017-03-07 15:16:34 -05002132 if (paramsChanged) {
2133 fDeferredActions.push_back([=]() {
2134 fWindow->setRequestedDisplayParams(params);
2135 fWindow->inval();
2136 this->updateTitle();
2137 });
2138 }
Brian Osman79086b92017-02-10 13:36:16 -05002139 ImGui::End();
2140 }
2141
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002142 if (gShaderErrorHandler.fErrors.count()) {
2143 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2144 ImGui::Begin("Shader Errors");
2145 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2146 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
2147 ImGui::TextWrapped("%s", gShaderErrorHandler.fShaders[i].c_str());
2148 }
2149 ImGui::End();
2150 gShaderErrorHandler.reset();
2151 }
2152
Brian Osmanf6877092017-02-13 09:39:57 -05002153 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002154 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2155 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002156 static int zoomFactor = 8;
2157 if (ImGui::Button("<<")) {
2158 zoomFactor = SkTMax(zoomFactor / 2, 4);
2159 }
2160 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2161 if (ImGui::Button(">>")) {
2162 zoomFactor = SkTMin(zoomFactor * 2, 32);
2163 }
Brian Osmanf6877092017-02-13 09:39:57 -05002164
Ben Wagner3627d2e2018-06-26 14:23:20 -04002165 if (!fZoomWindowFixed) {
2166 ImVec2 mousePos = ImGui::GetMousePos();
2167 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2168 }
2169 SkScalar x = fZoomWindowLocation.x();
2170 SkScalar y = fZoomWindowLocation.y();
2171 int xInt = SkScalarRoundToInt(x);
2172 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002173 ImVec2 avail = ImGui::GetContentRegionAvail();
2174
Brian Osmanead517d2017-11-13 15:36:36 -05002175 uint32_t pixel = 0;
2176 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002177 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002178 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002179 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002180 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002181 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002182 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2183 }
2184
Brian Osmand67e5182017-12-08 16:46:09 -05002185 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002186 // Translate so the region of the image that's under the mouse cursor is centered
2187 // in the zoom canvas:
2188 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002189 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2190 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002191 c->drawImage(this->fLastImage, 0, 0);
2192
2193 SkPaint outline;
2194 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002195 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002196 });
Brian Osmanf6877092017-02-13 09:39:57 -05002197 }
2198
2199 ImGui::End();
2200 }
Brian Osman79086b92017-02-10 13:36:16 -05002201}
2202
liyuqian2edb0f42016-07-06 14:11:32 -07002203void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002204 for (int i = 0; i < fDeferredActions.count(); ++i) {
2205 fDeferredActions[i]();
2206 }
2207 fDeferredActions.reset();
2208
Brian Osman56a24812017-12-19 11:15:16 -05002209 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002210 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002211 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002212 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002213
Brian Osman79086b92017-02-10 13:36:16 -05002214 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002215 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2216 // not be visible, though. So we need to redraw if there is at least one visible window, or
2217 // more than one active window. Newly created windows are active but not visible for one frame
2218 // while they determine their layout and sizing.
2219 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2220 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002221 fWindow->inval();
2222 }
jvanverth9f372462016-04-06 06:08:59 -07002223}
liyuqiane5a6cd92016-05-27 08:52:52 -07002224
Florin Malitab632df72018-06-18 21:23:06 -04002225template <typename OptionsFunc>
2226static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2227 OptionsFunc&& optionsFunc) {
2228 writer.beginObject();
2229 {
2230 writer.appendString(kName , name);
2231 writer.appendString(kValue, value);
2232
2233 writer.beginArray(kOptions);
2234 {
2235 optionsFunc(writer);
2236 }
2237 writer.endArray();
2238 }
2239 writer.endObject();
2240}
2241
2242
liyuqiane5a6cd92016-05-27 08:52:52 -07002243void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002244 if (!fWindow) {
2245 return;
2246 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002247 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002248 return; // Surface hasn't been created yet.
2249 }
2250
Florin Malitab632df72018-06-18 21:23:06 -04002251 SkDynamicMemoryWStream memStream;
2252 SkJSONWriter writer(&memStream);
2253 writer.beginArray();
2254
liyuqianb73c24b2016-06-03 08:47:23 -07002255 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002256 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2257 [this](SkJSONWriter& writer) {
2258 for(const auto& slide : fSlides) {
2259 writer.appendString(slide->getName().c_str());
2260 }
2261 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002262
liyuqianb73c24b2016-06-03 08:47:23 -07002263 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002264 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2265 [](SkJSONWriter& writer) {
2266 for (const auto& str : kBackendTypeStrings) {
2267 writer.appendString(str);
2268 }
2269 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002270
csmartdalton578f0642017-02-24 16:04:47 -07002271 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002272 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2273 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2274 [this](SkJSONWriter& writer) {
2275 writer.appendS32(0);
2276
2277 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2278 return;
2279 }
2280
2281 for (int msaa : {4, 8, 16}) {
2282 writer.appendS32(msaa);
2283 }
2284 });
csmartdalton578f0642017-02-24 16:04:47 -07002285
csmartdalton61cd31a2017-02-27 17:00:53 -07002286 // Path renderer state
2287 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002288 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2289 [this](SkJSONWriter& writer) {
2290 const GrContext* ctx = fWindow->getGrContext();
2291 if (!ctx) {
2292 writer.appendString("Software");
2293 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002294 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002295
Florin Malitab632df72018-06-18 21:23:06 -04002296 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2297 if (fWindow->sampleCount() > 1) {
2298 if (caps->shaderCaps()->pathRenderingSupport()) {
2299 writer.appendString(
2300 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2301 }
2302 } else {
2303 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2304 writer.appendString(
2305 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2306 }
2307 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2308 }
2309 writer.appendString(
2310 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2311 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2312 }
2313 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002314
liyuqianb73c24b2016-06-03 08:47:23 -07002315 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002316 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2317 [this](SkJSONWriter& writer) {
2318 writer.appendString(kSoftkeyHint);
2319 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2320 writer.appendString(softkey.c_str());
2321 }
2322 });
liyuqianb73c24b2016-06-03 08:47:23 -07002323
Florin Malitab632df72018-06-18 21:23:06 -04002324 writer.endArray();
2325 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002326
Florin Malitab632df72018-06-18 21:23:06 -04002327 auto data = memStream.detachAsData();
2328
2329 // TODO: would be cool to avoid this copy
2330 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2331
2332 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002333}
2334
2335void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002336 // For those who will add more features to handle the state change in this function:
2337 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2338 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2339 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002340 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002341 for (int i = 0; i < fSlides.count(); ++i) {
2342 if (fSlides[i]->getName().equals(stateValue)) {
2343 this->setCurrentSlide(i);
2344 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002345 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002346 }
Florin Malitaab99c342018-01-16 16:23:03 -05002347
2348 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002349 } else if (stateName.equals(kBackendStateName)) {
2350 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2351 if (stateValue.equals(kBackendTypeStrings[i])) {
2352 if (fBackendType != i) {
2353 fBackendType = (sk_app::Window::BackendType)i;
2354 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002355 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002356 }
2357 break;
2358 }
2359 }
csmartdalton578f0642017-02-24 16:04:47 -07002360 } else if (stateName.equals(kMSAAStateName)) {
2361 DisplayParams params = fWindow->getRequestedDisplayParams();
2362 int sampleCount = atoi(stateValue.c_str());
2363 if (sampleCount != params.fMSAASampleCount) {
2364 params.fMSAASampleCount = sampleCount;
2365 fWindow->setRequestedDisplayParams(params);
2366 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002367 this->updateTitle();
2368 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002369 }
2370 } else if (stateName.equals(kPathRendererStateName)) {
2371 DisplayParams params = fWindow->getRequestedDisplayParams();
2372 for (const auto& pair : gPathRendererNames) {
2373 if (pair.second == stateValue.c_str()) {
2374 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2375 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2376 fWindow->setRequestedDisplayParams(params);
2377 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002378 this->updateTitle();
2379 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002380 }
2381 break;
2382 }
csmartdalton578f0642017-02-24 16:04:47 -07002383 }
liyuqianb73c24b2016-06-03 08:47:23 -07002384 } else if (stateName.equals(kSoftkeyStateName)) {
2385 if (!stateValue.equals(kSoftkeyHint)) {
2386 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002387 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002388 }
liyuqian2edb0f42016-07-06 14:11:32 -07002389 } else if (stateName.equals(kRefreshStateName)) {
2390 // This state is actually NOT in the UI state.
2391 // We use this to allow Android to quickly set bool fRefresh.
2392 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002393 } else {
2394 SkDebugf("Unknown stateName: %s", stateName.c_str());
2395 }
2396}
Brian Osman79086b92017-02-10 13:36:16 -05002397
Hal Canaryb1f411a2019-08-29 10:39:22 -04002398bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002399 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002400}
2401
Hal Canaryb1f411a2019-08-29 10:39:22 -04002402bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002403 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002404 fWindow->inval();
2405 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002406 } else {
2407 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002408 }
Brian Osman79086b92017-02-10 13:36:16 -05002409}