blob: 27c03d2a6af54f18672e3da5bffb62b3464447aa [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;
308 displayParams.fGrContextOptions.fDisallowGLSLBinaryCaching = true;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400309 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
310 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltona1638a52019-06-24 11:54:24 -0600311 displayParams.fGrContextOptions.fInternalMultisampleCount = FLAGS_internalSamples;
csmartdalton578f0642017-02-24 16:04:47 -0700312 fWindow->setRequestedDisplayParams(displayParams);
313
Brian Osman56a24812017-12-19 11:15:16 -0500314 // Configure timers
315 fStatsLayer.setActive(false);
316 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
317 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
318 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
319
jvanverth9f372462016-04-06 06:08:59 -0700320 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700321 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500322 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500323 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500324 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700325
brianosman622c8d52016-05-10 06:50:49 -0700326 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500327 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
328 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
329 fWindow->inval();
330 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500331 // Command to jump directly to the slide picker and give it focus
332 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
333 this->fShowImGuiDebugWindow = true;
334 this->fShowSlidePicker = true;
335 fWindow->inval();
336 });
337 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400338 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500339 this->fShowImGuiDebugWindow = true;
340 this->fShowSlidePicker = true;
341 fWindow->inval();
342 });
Brian Osman79086b92017-02-10 13:36:16 -0500343 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
344 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
345 fWindow->inval();
346 });
Brian Osmanf6877092017-02-13 09:39:57 -0500347 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
348 this->fShowZoomWindow = !this->fShowZoomWindow;
349 fWindow->inval();
350 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400351 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
352 this->fZoomWindowFixed = !this->fZoomWindowFixed;
353 fWindow->inval();
354 });
Greg Danield0794cc2019-03-27 16:23:26 -0400355 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
356 DisplayParams params = fWindow->getRequestedDisplayParams();
357 params.fDisableVsync = !params.fDisableVsync;
358 fWindow->setRequestedDisplayParams(params);
359 this->updateTitle();
360 fWindow->inval();
361 });
Mike Reedf702ed42019-07-22 17:00:49 -0400362 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
363 fRefresh = !fRefresh;
364 fWindow->inval();
365 });
brianosman622c8d52016-05-10 06:50:49 -0700366 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500367 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700368 fWindow->inval();
369 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400370 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500371 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400372 this->updateTitle();
373 fWindow->inval();
374 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500375 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500376 switch (fColorMode) {
377 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500378 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500379 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500380 case ColorMode::kColorManaged8888:
381 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500382 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500383 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500384 this->setColorMode(ColorMode::kLegacy);
385 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500386 }
brianosman622c8d52016-05-10 06:50:49 -0700387 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400388 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500389 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700390 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400391 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500392 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700393 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400394 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700395 this->changeZoomLevel(1.f / 32.f);
396 fWindow->inval();
397 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400398 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700399 this->changeZoomLevel(-1.f / 32.f);
400 fWindow->inval();
401 });
jvanverthaf236b52016-05-20 06:01:06 -0700402 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400403 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
404 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500405 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400406#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
407 if (newBackend == sk_app::Window::kVulkan_BackendType) {
408 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
409 sk_app::Window::kBackendTypeCount);
410 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
411 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500412 }
413#endif
Brian Osman621491e2017-02-28 15:45:01 -0500414 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700415 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500416 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
417 fSaveToSKP = true;
418 fWindow->inval();
419 });
Mike Reed376d8122019-03-14 11:39:02 -0400420 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
421 fShowSlideDimensions = !fShowSlideDimensions;
422 fWindow->inval();
423 });
Ben Wagner37c54032018-04-13 14:30:23 -0400424 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
425 DisplayParams params = fWindow->getRequestedDisplayParams();
426 uint32_t flags = params.fSurfaceProps.flags();
427 if (!fPixelGeometryOverrides) {
428 fPixelGeometryOverrides = true;
429 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
430 } else {
431 switch (params.fSurfaceProps.pixelGeometry()) {
432 case kUnknown_SkPixelGeometry:
433 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
434 break;
435 case kRGB_H_SkPixelGeometry:
436 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
437 break;
438 case kBGR_H_SkPixelGeometry:
439 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
440 break;
441 case kRGB_V_SkPixelGeometry:
442 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
443 break;
444 case kBGR_V_SkPixelGeometry:
445 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
446 fPixelGeometryOverrides = false;
447 break;
448 }
449 }
450 fWindow->setRequestedDisplayParams(params);
451 this->updateTitle();
452 fWindow->inval();
453 });
Ben Wagner9613e452019-01-23 10:34:59 -0500454 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500455 if (!fFontOverrides.fHinting) {
456 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400457 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500458 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500459 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400460 case SkFontHinting::kNone:
461 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500462 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400463 case SkFontHinting::kSlight:
464 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500465 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400466 case SkFontHinting::kNormal:
467 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500468 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400469 case SkFontHinting::kFull:
470 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500471 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500472 break;
473 }
474 }
475 this->updateTitle();
476 fWindow->inval();
477 });
478 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500479 if (!fPaintOverrides.fAntiAlias) {
480 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
481 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500482 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500483 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500484 } else {
485 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500486 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500487 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500488 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400489 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500490 break;
491 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500492 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500493 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400494 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500495 break;
496 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500497 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400498 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500499 break;
500 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500501 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
502 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500503 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
504 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500505 break;
506 }
507 }
508 this->updateTitle();
509 fWindow->inval();
510 });
Ben Wagner37c54032018-04-13 14:30:23 -0400511 fCommands.addCommand('D', "Modes", "DFT", [this]() {
512 DisplayParams params = fWindow->getRequestedDisplayParams();
513 uint32_t flags = params.fSurfaceProps.flags();
514 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
515 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
516 fWindow->setRequestedDisplayParams(params);
517 this->updateTitle();
518 fWindow->inval();
519 });
Ben Wagner9613e452019-01-23 10:34:59 -0500520 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500521 if (!fFontOverrides.fEdging) {
522 fFontOverrides.fEdging = true;
523 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500524 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500525 switch (fFont.getEdging()) {
526 case SkFont::Edging::kAlias:
527 fFont.setEdging(SkFont::Edging::kAntiAlias);
528 break;
529 case SkFont::Edging::kAntiAlias:
530 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
531 break;
532 case SkFont::Edging::kSubpixelAntiAlias:
533 fFont.setEdging(SkFont::Edging::kAlias);
534 fFontOverrides.fEdging = false;
535 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500536 }
537 }
538 this->updateTitle();
539 fWindow->inval();
540 });
Ben Wagner9613e452019-01-23 10:34:59 -0500541 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500542 if (!fFontOverrides.fSubpixel) {
543 fFontOverrides.fSubpixel = true;
544 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500545 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500546 if (!fFont.isSubpixel()) {
547 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500548 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500549 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500550 }
551 }
552 this->updateTitle();
553 fWindow->inval();
554 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400555 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
556 if (!fFontOverrides.fBaselineSnap) {
557 fFontOverrides.fBaselineSnap = true;
558 fFont.setBaselineSnap(false);
559 } else {
560 if (!fFont.isBaselineSnap()) {
561 fFont.setBaselineSnap(true);
562 } else {
563 fFontOverrides.fBaselineSnap = false;
564 }
565 }
566 this->updateTitle();
567 fWindow->inval();
568 });
Brian Osman805a7272018-05-02 15:40:20 -0400569 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
570 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
571 : kPerspective_Real;
572 this->updateTitle();
573 fWindow->inval();
574 });
575 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
576 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
577 : kPerspective_Off;
578 this->updateTitle();
579 fWindow->inval();
580 });
Brian Osman207d4102019-01-10 09:40:58 -0500581 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
582 fAnimTimer.togglePauseResume();
583 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400584 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
585 fZoomUI = !fZoomUI;
586 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
587 fWindow->inval();
588 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500589
jvanverth2bb3b6d2016-04-08 07:24:09 -0700590 // set up slides
591 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500592 if (FLAGS_list) {
593 this->listNames();
594 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700595
Brian Osman9bb47cf2018-04-26 15:55:00 -0400596 fPerspectivePoints[0].set(0, 0);
597 fPerspectivePoints[1].set(1, 0);
598 fPerspectivePoints[2].set(0, 1);
599 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700600 fAnimTimer.run();
601
Hal Canaryc465d132017-12-08 10:21:31 -0500602 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500603 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400604 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500605 }
606 fImGuiGamutPaint.setColor(SK_ColorWHITE);
607 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
608
jongdeok.kim804f17e2019-02-26 14:39:23 +0900609 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500610 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700611}
612
jvanverth34524262016-05-04 13:49:13 -0700613void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400614 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
615 static const struct {
616 const char* fExtension;
617 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500618 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400619 const SlideFactory fFactory;
620 } gExternalSlidesInfo[] = {
621 { ".skp", "skp-dir", FLAGS_skps,
622 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
623 return sk_make_sp<SKPSlide>(name, path);}
624 },
625 { ".jpg", "jpg-dir", FLAGS_jpgs,
626 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
627 return sk_make_sp<ImageSlide>(name, path);}
628 },
Florin Malita87ccf332018-05-04 12:23:24 -0400629#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400630 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400631 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
632 return sk_make_sp<SkottieSlide>(name, path);}
633 },
Florin Malita87ccf332018-05-04 12:23:24 -0400634#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400635#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400636 { ".svg", "svg-dir", FLAGS_svgs,
637 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
638 return sk_make_sp<SvgSlide>(name, path);}
639 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400640#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400641 };
jvanverthc265a922016-04-08 12:51:45 -0700642
Brian Salomon343553a2018-09-05 15:41:23 -0400643 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700644
Mike Klein88544fb2019-03-20 10:50:33 -0500645 const auto addSlide =
646 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
647 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
648 return;
649 }
liyuqian6f163d22016-06-13 12:26:45 -0700650
Mike Klein88544fb2019-03-20 10:50:33 -0500651 if (auto slide = fact(name, path)) {
652 dirSlides.push_back(slide);
653 fSlides.push_back(std::move(slide));
654 }
655 };
Florin Malita76a076b2018-02-15 18:40:48 -0500656
Florin Malita38792ce2018-05-08 10:36:18 -0400657 if (!FLAGS_file.isEmpty()) {
658 // single file mode
659 const SkString file(FLAGS_file[0]);
660
661 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
662 for (const auto& sinfo : gExternalSlidesInfo) {
663 if (file.endsWith(sinfo.fExtension)) {
664 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
665 return;
666 }
667 }
668
669 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
670 } else {
671 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
672 }
673
674 return;
675 }
676
677 // Bisect slide.
678 if (!FLAGS_bisect.isEmpty()) {
679 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500680 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400681 if (FLAGS_bisect.count() >= 2) {
682 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
683 bisect->onChar(*ch);
684 }
685 }
686 fSlides.push_back(std::move(bisect));
687 }
688 }
689
690 // GMs
691 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400692 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400693 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500694 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400695 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400696 fSlides.push_back(std::move(slide));
697 }
Florin Malita38792ce2018-05-08 10:36:18 -0400698 }
699 // reverse gms
700 int numGMs = fSlides.count() - firstGM;
701 for (int i = 0; i < numGMs/2; ++i) {
702 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
703 }
704
705 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400706 for (const SampleFactory factory : SampleRegistry::Range()) {
707 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500708 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400709 fSlides.push_back(slide);
710 }
Florin Malita38792ce2018-05-08 10:36:18 -0400711 }
712
Brian Osman7c979f52019-02-12 13:27:51 -0500713 // Particle demo
714 {
715 // TODO: Convert this to a sample
716 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500717 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500718 fSlides.push_back(std::move(slide));
719 }
720 }
721
Florin Malita0ffa3222018-04-05 14:34:45 -0400722 for (const auto& info : gExternalSlidesInfo) {
723 for (const auto& flag : info.fFlags) {
724 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
725 // single file
726 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
727 } else {
728 // directory
729 SkOSFile::Iter it(flag.c_str(), info.fExtension);
730 SkString name;
731 while (it.next(&name)) {
732 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
733 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400734 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400735 if (!dirSlides.empty()) {
736 fSlides.push_back(
737 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
738 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500739 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400740 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400741 }
742 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500743
744 if (!fSlides.count()) {
745 sk_sp<Slide> slide(new NullSlide());
746 fSlides.push_back(std::move(slide));
747 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700748}
749
750
jvanverth34524262016-05-04 13:49:13 -0700751Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700752 fWindow->detach();
753 delete fWindow;
754}
755
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500756struct SkPaintTitleUpdater {
757 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
758 void append(const char* s) {
759 if (fCount == 0) {
760 fTitle->append(" {");
761 } else {
762 fTitle->append(", ");
763 }
764 fTitle->append(s);
765 ++fCount;
766 }
767 void done() {
768 if (fCount > 0) {
769 fTitle->append("}");
770 }
771 }
772 SkString* fTitle;
773 int fCount;
774};
775
brianosman05de2162016-05-06 13:28:57 -0700776void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700777 if (!fWindow) {
778 return;
779 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500780 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700781 return; // Surface hasn't been created yet.
782 }
783
jvanverth34524262016-05-04 13:49:13 -0700784 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700785 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700786
Mike Kleine5acd752019-03-22 09:57:16 -0500787 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400788 if (gSkForceAnalyticAA) {
789 title.append(" <FAAA>");
790 } else {
791 title.append(" <AAA>");
792 }
793 }
794
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500795 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500796 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
797 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400798 const char* on, const char* off)
799 {
Ben Wagner9613e452019-01-23 10:34:59 -0500800 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400801 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500802 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400803 };
804
Ben Wagner9613e452019-01-23 10:34:59 -0500805 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
806 const char* on, const char* off)
807 {
808 if (fFontOverrides.*flag) {
809 paintTitle.append((fFont.*isFlag)() ? on : off);
810 }
811 };
812
813 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
814 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500815 if (fPaintOverrides.fFilterQuality) {
816 switch (fPaint.getFilterQuality()) {
817 case kNone_SkFilterQuality:
818 paintTitle.append("NoFilter");
819 break;
820 case kLow_SkFilterQuality:
821 paintTitle.append("LowFilter");
822 break;
823 case kMedium_SkFilterQuality:
824 paintTitle.append("MediumFilter");
825 break;
826 case kHigh_SkFilterQuality:
827 paintTitle.append("HighFilter");
828 break;
829 }
830 }
Ben Wagner9613e452019-01-23 10:34:59 -0500831
832 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
833 "Force Autohint", "No Force Autohint");
834 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400835 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500836 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
837 "Linear Metrics", "Non-Linear Metrics");
838 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
839 "Bitmap Text", "No Bitmap Text");
840 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
841
842 if (fFontOverrides.fEdging) {
843 switch (fFont.getEdging()) {
844 case SkFont::Edging::kAlias:
845 paintTitle.append("Alias Text");
846 break;
847 case SkFont::Edging::kAntiAlias:
848 paintTitle.append("Antialias Text");
849 break;
850 case SkFont::Edging::kSubpixelAntiAlias:
851 paintTitle.append("Subpixel Antialias Text");
852 break;
853 }
854 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400855
Mike Reed3ae47332019-01-04 10:11:46 -0500856 if (fFontOverrides.fHinting) {
857 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400858 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500859 paintTitle.append("No Hinting");
860 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400861 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500862 paintTitle.append("Slight Hinting");
863 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400864 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500865 paintTitle.append("Normal Hinting");
866 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400867 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500868 paintTitle.append("Full Hinting");
869 break;
870 }
871 }
872 paintTitle.done();
873
Brian Osman92004802017-03-06 11:47:26 -0500874 switch (fColorMode) {
875 case ColorMode::kLegacy:
876 title.append(" Legacy 8888");
877 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500878 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500879 title.append(" ColorManaged 8888");
880 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500881 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500882 title.append(" ColorManaged F16");
883 break;
884 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500885
Brian Osman92004802017-03-06 11:47:26 -0500886 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500887 int curPrimaries = -1;
888 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
889 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
890 curPrimaries = i;
891 break;
892 }
893 }
Brian Osman03115dc2018-11-26 13:55:19 -0500894 title.appendf(" %s Gamma %f",
895 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500896 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700897 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500898
Ben Wagner37c54032018-04-13 14:30:23 -0400899 const DisplayParams& params = fWindow->getRequestedDisplayParams();
900 if (fPixelGeometryOverrides) {
901 switch (params.fSurfaceProps.pixelGeometry()) {
902 case kUnknown_SkPixelGeometry:
903 title.append( " Flat");
904 break;
905 case kRGB_H_SkPixelGeometry:
906 title.append( " RGB");
907 break;
908 case kBGR_H_SkPixelGeometry:
909 title.append( " BGR");
910 break;
911 case kRGB_V_SkPixelGeometry:
912 title.append( " RGBV");
913 break;
914 case kBGR_V_SkPixelGeometry:
915 title.append( " BGRV");
916 break;
917 }
918 }
919
920 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
921 title.append(" DFT");
922 }
923
csmartdalton578f0642017-02-24 16:04:47 -0700924 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700925 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500926 int msaa = fWindow->sampleCount();
927 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700928 title.appendf(" MSAA: %i", msaa);
929 }
930 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700931
932 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600933 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700934 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
935 }
936
Brian Osman805a7272018-05-02 15:40:20 -0400937 if (kPerspective_Real == fPerspectiveMode) {
938 title.append(" Perpsective (Real)");
939 } else if (kPerspective_Fake == fPerspectiveMode) {
940 title.append(" Perspective (Fake)");
941 }
942
brianosman05de2162016-05-06 13:28:57 -0700943 fWindow->setTitle(title.c_str());
944}
945
Florin Malitaab99c342018-01-16 16:23:03 -0500946int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500947
948 if (!FLAGS_slide.isEmpty()) {
949 int count = fSlides.count();
950 for (int i = 0; i < count; i++) {
951 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500952 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500953 }
954 }
955
956 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
957 this->listNames();
958 }
959
Florin Malitaab99c342018-01-16 16:23:03 -0500960 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500961}
962
Florin Malitaab99c342018-01-16 16:23:03 -0500963void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500964 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500965 for (const auto& slide : fSlides) {
966 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500967 }
968}
969
Florin Malitaab99c342018-01-16 16:23:03 -0500970void Viewer::setCurrentSlide(int slide) {
971 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700972
Florin Malitaab99c342018-01-16 16:23:03 -0500973 if (slide == fCurrentSlide) {
974 return;
975 }
976
977 if (fCurrentSlide >= 0) {
978 fSlides[fCurrentSlide]->unload();
979 }
980
981 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
982 SkIntToScalar(fWindow->height()));
983 fCurrentSlide = slide;
984 this->setupCurrentSlide();
985}
986
987void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500988 if (fCurrentSlide >= 0) {
989 // prepare dimensions for image slides
990 fGesture.resetTouchState();
991 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700992
Jim Van Verth0848fb02018-01-22 13:39:30 -0500993 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
994 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
995 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400996
Jim Van Verth0848fb02018-01-22 13:39:30 -0500997 // Start with a matrix that scales the slide to the available screen space
998 if (fWindow->scaleContentToFit()) {
999 if (windowRect.width() > 0 && windowRect.height() > 0) {
1000 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1001 }
liyuqiane46e4f02016-05-20 07:32:19 -07001002 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001003
1004 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001005 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001006
1007 this->updateTitle();
1008 this->updateUIState();
1009
1010 fStatsLayer.resetMeasurements();
1011
1012 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001013 }
jvanverthc265a922016-04-08 12:51:45 -07001014}
1015
1016#define MAX_ZOOM_LEVEL 8
1017#define MIN_ZOOM_LEVEL -8
1018
jvanverth34524262016-05-04 13:49:13 -07001019void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001020 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -04001021 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001022 this->preTouchMatrixChanged();
1023}
Yuqian Li755778c2018-03-28 16:23:31 -04001024
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001025void Viewer::preTouchMatrixChanged() {
1026 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001027 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1028 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1029 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1030 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1031}
1032
Brian Osman805a7272018-05-02 15:40:20 -04001033SkMatrix Viewer::computePerspectiveMatrix() {
1034 SkScalar w = fWindow->width(), h = fWindow->height();
1035 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1036 SkPoint perspPts[4] = {
1037 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1038 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1039 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1040 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1041 };
1042 SkMatrix m;
1043 m.setPolyToPoly(orthoPts, perspPts, 4);
1044 return m;
1045}
1046
Yuqian Li755778c2018-03-28 16:23:31 -04001047SkMatrix Viewer::computePreTouchMatrix() {
1048 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001049
1050 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001051 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001052 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001053
1054 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1055 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001056
Brian Osman805a7272018-05-02 15:40:20 -04001057 if (kPerspective_Real == fPerspectiveMode) {
1058 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001059 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001060 }
1061
Yuqian Li755778c2018-03-28 16:23:31 -04001062 return m;
jvanverthc265a922016-04-08 12:51:45 -07001063}
1064
liyuqiand3cdbca2016-05-17 12:44:20 -07001065SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001066 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001067 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001068 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001069 return m;
jvanverthc265a922016-04-08 12:51:45 -07001070}
1071
Brian Osman621491e2017-02-28 15:45:01 -05001072void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001073 fPersistentCache.reset();
1074 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001075 fBackendType = backendType;
1076
1077 fWindow->detach();
1078
Brian Osman70d2f432017-11-08 09:54:10 -05001079#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001080 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1081 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001082 DisplayParams params = fWindow->getRequestedDisplayParams();
1083 delete fWindow;
1084 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001085
Brian Osman70d2f432017-11-08 09:54:10 -05001086 // re-register callbacks
1087 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001088 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001089 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001090 fWindow->pushLayer(&fImGuiLayer);
1091
Brian Osman70d2f432017-11-08 09:54:10 -05001092 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1093 // will still include our correct sample count. But the re-created fWindow will lose that
1094 // information. On Windows, we need to re-create the window when changing sample count,
1095 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1096 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1097 // as if we had never changed windows at all).
1098 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001099#endif
1100
Brian Osman70d2f432017-11-08 09:54:10 -05001101 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001102}
1103
Brian Osman92004802017-03-06 11:47:26 -05001104void Viewer::setColorMode(ColorMode colorMode) {
1105 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001106 this->updateTitle();
1107 fWindow->inval();
1108}
1109
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001110class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1111public:
Mike Reed3ae47332019-01-04 10:11:46 -05001112 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1113 SkFont* font, Viewer::SkFontFields* ffields)
1114 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001115 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001116 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1117 sk_sp<SkTextBlob>* cache) {
1118 bool blobWillChange = false;
1119 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001120 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1121 bool shouldDraw = this->filterFont(&filteredFont);
1122 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001123 blobWillChange = true;
1124 break;
1125 }
1126 }
1127 if (!blobWillChange) {
1128 return blob;
1129 }
1130
1131 SkTextBlobBuilder builder;
1132 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001133 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1134 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001135 if (!shouldDraw) {
1136 continue;
1137 }
1138
Mike Reed3ae47332019-01-04 10:11:46 -05001139 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001140
Ben Wagner41e40472018-09-24 13:01:54 -04001141 const SkTextBlobBuilder::RunBuffer& runBuffer
1142 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001143 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001144 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001145 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001146 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001147 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001148 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001149 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001150 it.glyphCount(), it.textSize(), SkString())
1151 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1152 uint32_t glyphCount = it.glyphCount();
1153 if (it.glyphs()) {
1154 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1155 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1156 }
1157 if (it.pos()) {
1158 size_t posSize = sizeof(decltype(*it.pos()));
1159 uint8_t positioning = it.positioning();
1160 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1161 }
1162 if (it.text()) {
1163 size_t textSize = sizeof(decltype(*it.text()));
1164 uint32_t textCount = it.textSize();
1165 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1166 }
1167 if (it.clusters()) {
1168 size_t clusterSize = sizeof(decltype(*it.clusters()));
1169 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1170 }
1171 }
1172 *cache = builder.make();
1173 return cache->get();
1174 }
1175 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1176 const SkPaint& paint) override {
1177 sk_sp<SkTextBlob> cache;
1178 this->SkPaintFilterCanvas::onDrawTextBlob(
1179 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1180 }
Mike Reed3ae47332019-01-04 10:11:46 -05001181 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001182 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001183 font->writable()->setSize(fFont->getSize());
1184 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001185 if (fFontOverrides->fScaleX) {
1186 font->writable()->setScaleX(fFont->getScaleX());
1187 }
1188 if (fFontOverrides->fSkewX) {
1189 font->writable()->setSkewX(fFont->getSkewX());
1190 }
Mike Reed3ae47332019-01-04 10:11:46 -05001191 if (fFontOverrides->fHinting) {
1192 font->writable()->setHinting(fFont->getHinting());
1193 }
Ben Wagner9613e452019-01-23 10:34:59 -05001194 if (fFontOverrides->fEdging) {
1195 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001196 }
Ben Wagner9613e452019-01-23 10:34:59 -05001197 if (fFontOverrides->fEmbolden) {
1198 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001199 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001200 if (fFontOverrides->fBaselineSnap) {
1201 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1202 }
Ben Wagner9613e452019-01-23 10:34:59 -05001203 if (fFontOverrides->fLinearMetrics) {
1204 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001205 }
Ben Wagner9613e452019-01-23 10:34:59 -05001206 if (fFontOverrides->fSubpixel) {
1207 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001208 }
Ben Wagner9613e452019-01-23 10:34:59 -05001209 if (fFontOverrides->fEmbeddedBitmaps) {
1210 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001211 }
Ben Wagner9613e452019-01-23 10:34:59 -05001212 if (fFontOverrides->fForceAutoHinting) {
1213 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001214 }
Ben Wagner9613e452019-01-23 10:34:59 -05001215
Mike Reed3ae47332019-01-04 10:11:46 -05001216 return true;
1217 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001218 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001219 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001220 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001221 }
Ben Wagner9613e452019-01-23 10:34:59 -05001222 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001223 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001224 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001225 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001226 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001227 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001228 return true;
1229 }
1230 SkPaint* fPaint;
1231 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001232 SkFont* fFont;
1233 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001234};
1235
Robert Phillips9882dae2019-03-04 11:00:10 -05001236void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001237 if (fCurrentSlide < 0) {
1238 return;
1239 }
1240
Robert Phillips9882dae2019-03-04 11:00:10 -05001241 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001242
Brian Osmanf750fbc2017-02-08 10:47:28 -05001243 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001244 SkSurface* slideSurface = surface;
1245 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001246 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001247
Brian Osmane0d4fba2017-03-15 10:24:55 -04001248 // 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 -05001249 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001250 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001251 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001252 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001253 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001254 }
1255
Brian Osman3ac99cf2017-12-01 11:23:53 -05001256 if (fSaveToSKP) {
1257 SkPictureRecorder recorder;
1258 SkCanvas* recorderCanvas = recorder.beginRecording(
1259 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001260 fSlides[fCurrentSlide]->draw(recorderCanvas);
1261 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1262 SkFILEWStream stream("sample_app.skp");
1263 picture->serialize(&stream);
1264 fSaveToSKP = false;
1265 }
1266
Brian Osmane9ed0f02018-11-26 14:50:05 -05001267 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
1268 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode) ? kRGBA_F16_SkColorType
1269 : kN32_SkColorType;
Brian Osmane9ed0f02018-11-26 14:50:05 -05001270
1271 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001272 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1273 slideCanvas->getProps(&props);
1274
Brian Osmane9ed0f02018-11-26 14:50:05 -05001275 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1276 return Window::kRaster_BackendType == this->fBackendType
1277 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001278 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001279 };
1280
Brian Osman03115dc2018-11-26 13:55:19 -05001281 // We need to render offscreen if we're...
1282 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1283 // ... in any raster mode, because the window surface is actually GL
1284 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001285 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001286 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001287 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001288 Window::kRaster_BackendType == fBackendType ||
1289 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001290
Brian Osmane9ed0f02018-11-26 14:50:05 -05001291 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001292 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001293 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001294 }
1295
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001296 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001297 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001298 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001299 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001300 if (fTiled) {
1301 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1302 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1303 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1304 SkCanvas* tileCanvas = tileSurface->getCanvas();
1305 SkMatrix m = this->computeMatrix();
1306 for (int y = 0; y < fWindow->height(); y += tileH) {
1307 for (int x = 0; x < fWindow->width(); x += tileW) {
1308 SkAutoCanvasRestore acr(tileCanvas, true);
1309 tileCanvas->translate(-x, -y);
1310 tileCanvas->clear(SK_ColorTRANSPARENT);
1311 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001312 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1313 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001314 fSlides[fCurrentSlide]->draw(&filterCanvas);
1315 tileSurface->draw(slideCanvas, x, y, nullptr);
1316 }
1317 }
1318
1319 // Draw borders between tiles
1320 if (fDrawTileBoundaries) {
1321 SkPaint border;
1322 border.setColor(0x60FF00FF);
1323 border.setStyle(SkPaint::kStroke_Style);
1324 for (int y = 0; y < fWindow->height(); y += tileH) {
1325 for (int x = 0; x < fWindow->width(); x += tileW) {
1326 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1327 }
1328 }
1329 }
1330 } else {
1331 slideCanvas->concat(this->computeMatrix());
1332 if (kPerspective_Real == fPerspectiveMode) {
1333 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1334 }
Mike Reed3ae47332019-01-04 10:11:46 -05001335 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001336 fSlides[fCurrentSlide]->draw(&filterCanvas);
1337 }
Brian Osman56a24812017-12-19 11:15:16 -05001338 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001339 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001340
1341 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001342 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001343 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001344 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001345
1346 // If we rendered offscreen, snap an image and push the results to the window's canvas
1347 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001348 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001349
Robert Phillips9882dae2019-03-04 11:00:10 -05001350 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001351 SkPaint paint;
1352 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001353 int prePerspectiveCount = canvas->save();
1354 if (kPerspective_Fake == fPerspectiveMode) {
1355 paint.setFilterQuality(kHigh_SkFilterQuality);
1356 canvas->clear(SK_ColorWHITE);
1357 canvas->concat(this->computePerspectiveMatrix());
1358 }
Brian Osman03115dc2018-11-26 13:55:19 -05001359 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001360 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001361 }
Mike Reed376d8122019-03-14 11:39:02 -04001362
1363 if (fShowSlideDimensions) {
1364 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1365 SkPaint paint;
1366 paint.setColor(0x40FFFF00);
1367 surface->getCanvas()->drawRect(r, paint);
1368 }
liyuqian6f163d22016-06-13 12:26:45 -07001369}
1370
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001371void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001372 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001373 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001374}
Jim Van Verth6f449692017-02-14 15:16:46 -05001375
Robert Phillips9882dae2019-03-04 11:00:10 -05001376void Viewer::onPaint(SkSurface* surface) {
1377 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001378
Robert Phillips9882dae2019-03-04 11:00:10 -05001379 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001380
Brian Osmand67e5182017-12-08 16:46:09 -05001381 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001382
1383 if (GrContext* ctx = fWindow->getGrContext()) {
1384 // Clean out cache items that haven't been used in more than 10 seconds.
1385 ctx->performDeferredCleanup(std::chrono::seconds(10));
1386 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001387}
1388
Ben Wagnera1915972018-08-09 15:06:19 -04001389void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001390 if (fCurrentSlide >= 0) {
1391 fSlides[fCurrentSlide]->resize(width, height);
1392 }
Ben Wagnera1915972018-08-09 15:06:19 -04001393}
1394
Florin Malitacefc1b92018-02-19 21:43:47 -05001395SkPoint Viewer::mapEvent(float x, float y) {
1396 const auto m = this->computeMatrix();
1397 SkMatrix inv;
1398
1399 SkAssertResult(m.invert(&inv));
1400
1401 return inv.mapXY(x, y);
1402}
1403
Hal Canaryb1f411a2019-08-29 10:39:22 -04001404bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001405 if (GestureDevice::kMouse == fGestureDevice) {
1406 return false;
1407 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001408
1409 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001410 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001411 fWindow->inval();
1412 return true;
1413 }
1414
liyuqiand3cdbca2016-05-17 12:44:20 -07001415 void* castedOwner = reinterpret_cast<void*>(owner);
1416 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001417 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001418 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001419#if defined(SK_BUILD_FOR_IOS)
1420 // TODO: move IOS swipe detection higher up into the platform code
1421 SkPoint dir;
1422 if (fGesture.isFling(&dir)) {
1423 // swiping left or right
1424 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1425 if (dir.fX < 0) {
1426 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1427 fCurrentSlide + 1 : 0);
1428 } else {
1429 this->setCurrentSlide(fCurrentSlide > 0 ?
1430 fCurrentSlide - 1 : fSlides.count() - 1);
1431 }
1432 }
1433 fGesture.reset();
1434 }
1435#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001436 break;
1437 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001438 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001439 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001440 break;
1441 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001442 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001443 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001444 break;
1445 }
1446 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001447 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001448 fWindow->inval();
1449 return true;
1450}
1451
Hal Canaryb1f411a2019-08-29 10:39:22 -04001452bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001453 if (GestureDevice::kTouch == fGestureDevice) {
1454 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001455 }
Brian Osman16c81a12017-12-20 11:58:34 -05001456
Florin Malitacefc1b92018-02-19 21:43:47 -05001457 const auto slidePt = this->mapEvent(x, y);
1458 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1459 fWindow->inval();
1460 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001461 }
1462
1463 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001464 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001465 fGesture.touchEnd(nullptr);
1466 break;
1467 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001468 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001469 fGesture.touchBegin(nullptr, x, y);
1470 break;
1471 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001472 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001473 fGesture.touchMoved(nullptr, x, y);
1474 break;
1475 }
1476 }
1477 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1478
Hal Canaryb1f411a2019-08-29 10:39:22 -04001479 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001480 fWindow->inval();
1481 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001482 return true;
1483}
1484
Brian Osmana109e392017-02-24 09:49:14 -05001485static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001486 // The gamut image covers a (0.8 x 0.9) shaped region
1487 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001488
1489 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1490 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1491 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001492 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1493 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1494 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001495
Brian Osman535c5e32019-02-09 16:32:58 -05001496 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1497 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1498 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1499 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1500 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001501}
1502
Ben Wagner3627d2e2018-06-26 14:23:20 -04001503static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001504 ImGui::DragCanvas dc(pt);
1505 dc.fillColor(IM_COL32(0, 0, 0, 128));
1506 dc.dragPoint(pt);
1507 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001508}
1509
Brian Osman9bb47cf2018-04-26 15:55:00 -04001510static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001511 ImGui::DragCanvas dc(pts);
1512 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001513
Brian Osman535c5e32019-02-09 16:32:58 -05001514 for (int i = 0; i < 4; ++i) {
1515 dc.dragPoint(pts + i);
1516 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001517
Brian Osman535c5e32019-02-09 16:32:58 -05001518 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1519 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1520 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1521 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001522
Brian Osman535c5e32019-02-09 16:32:58 -05001523 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001524}
1525
Brian Osmand67e5182017-12-08 16:46:09 -05001526void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001527 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1528 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001529 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001530 }
1531
1532 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001533 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1534 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001535 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001536 DisplayParams params = fWindow->getRequestedDisplayParams();
1537 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001538 const GrContext* ctx = fWindow->getGrContext();
1539
Brian Osmana109e392017-02-24 09:49:14 -05001540 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1541 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001542 if (ImGui::CollapsingHeader("Backend")) {
1543 int newBackend = static_cast<int>(fBackendType);
1544 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1545 ImGui::SameLine();
1546 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001547#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1548 ImGui::SameLine();
1549 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1550#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001551#if defined(SK_DAWN)
1552 ImGui::SameLine();
1553 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1554#endif
Brian Osman621491e2017-02-28 15:45:01 -05001555#if defined(SK_VULKAN)
1556 ImGui::SameLine();
1557 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1558#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001559#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001560 ImGui::SameLine();
1561 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1562#endif
Brian Osman621491e2017-02-28 15:45:01 -05001563 if (newBackend != fBackendType) {
1564 fDeferredActions.push_back([=]() {
1565 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1566 });
1567 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001568
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001569 bool* wire = &params.fGrContextOptions.fWireframeMode;
1570 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1571 paramsChanged = true;
1572 }
Brian Salomon99a33902017-03-07 15:16:34 -05001573
Brian Osman28b12522017-03-08 17:10:24 -05001574 if (ctx) {
1575 int sampleCount = fWindow->sampleCount();
1576 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001577 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001578 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1579 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1580 ImGui::RadioButton("16", &sampleCount, 16);
1581
1582 if (sampleCount != params.fMSAASampleCount) {
1583 params.fMSAASampleCount = sampleCount;
1584 paramsChanged = true;
1585 }
1586 }
1587
Ben Wagner37c54032018-04-13 14:30:23 -04001588 int pixelGeometryIdx = 0;
1589 if (fPixelGeometryOverrides) {
1590 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1591 }
1592 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1593 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1594 {
1595 uint32_t flags = params.fSurfaceProps.flags();
1596 if (pixelGeometryIdx == 0) {
1597 fPixelGeometryOverrides = false;
1598 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1599 } else {
1600 fPixelGeometryOverrides = true;
1601 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1602 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1603 }
1604 paramsChanged = true;
1605 }
1606
1607 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1608 if (ImGui::Checkbox("DFT", &useDFT)) {
1609 uint32_t flags = params.fSurfaceProps.flags();
1610 if (useDFT) {
1611 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1612 } else {
1613 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1614 }
1615 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1616 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1617 paramsChanged = true;
1618 }
1619
Brian Osman8a9de3d2017-03-01 14:59:05 -05001620 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001621 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001622 auto prButton = [&](GpuPathRenderers x) {
1623 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001624 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1625 params.fGrContextOptions.fGpuPathRenderers = x;
1626 paramsChanged = true;
1627 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001628 }
1629 };
1630
1631 if (!ctx) {
1632 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001633 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001634 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001635 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001636 prButton(GpuPathRenderers::kStencilAndCover);
1637 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001638 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001639 prButton(GpuPathRenderers::kNone);
1640 } else {
1641 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001642 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001643 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001644 prButton(GpuPathRenderers::kCoverageCounting);
1645 }
Jim Van Verth83010462017-03-16 08:45:39 -04001646 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001647 prButton(GpuPathRenderers::kTessellating);
1648 prButton(GpuPathRenderers::kNone);
1649 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001650 ImGui::TreePop();
1651 }
Brian Osman621491e2017-02-28 15:45:01 -05001652 }
1653
Ben Wagner964571d2019-03-08 12:35:06 -05001654 if (ImGui::CollapsingHeader("Tiling")) {
1655 ImGui::Checkbox("Enable", &fTiled);
1656 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1657 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1658 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1659 }
1660
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001661 if (ImGui::CollapsingHeader("Transform")) {
1662 float zoom = fZoomLevel;
1663 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1664 fZoomLevel = zoom;
1665 this->preTouchMatrixChanged();
1666 paramsChanged = true;
1667 }
1668 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001669 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001670 fRotation = deg;
1671 this->preTouchMatrixChanged();
1672 paramsChanged = true;
1673 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001674 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1675 if (ImGui_DragLocation(&fOffset)) {
1676 this->preTouchMatrixChanged();
1677 paramsChanged = true;
1678 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001679 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1680 this->preTouchMatrixChanged();
1681 paramsChanged = true;
1682 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001683 }
Brian Osman805a7272018-05-02 15:40:20 -04001684 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1685 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1686 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001687 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001688 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001689 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001690 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001691 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001692 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001693 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001694 }
1695
Ben Wagnera580fb32018-04-17 11:16:32 -04001696 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001697 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001698 if (fPaintOverrides.fAntiAlias) {
1699 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001700 }
1701 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001702 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001703 {
1704 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1705 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001706 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001707 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1708 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001709 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001710 fPaintOverrides.fAntiAlias = true;
1711 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001712 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001713 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001714 case SkPaintFields::AntiAliasState::Alias:
1715 break;
1716 case SkPaintFields::AntiAliasState::Normal:
1717 break;
1718 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1719 gSkUseAnalyticAA = true;
1720 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001721 break;
1722 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1723 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001724 break;
1725 }
1726 }
1727 paramsChanged = true;
1728 }
1729
Ben Wagner99a78dc2018-05-09 18:23:51 -04001730 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001731 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001732 bool (SkPaint::* isFlag)() const,
1733 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001734 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001735 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001736 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001737 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001738 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001739 if (ImGui::Combo(label, &itemIndex, items)) {
1740 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001741 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001742 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001743 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001744 (fPaint.*setFlag)(itemIndex == 2);
1745 }
1746 paramsChanged = true;
1747 }
1748 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001749
Ben Wagner99a78dc2018-05-09 18:23:51 -04001750 paintFlag("Dither",
1751 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001752 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001753 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001754
1755 int filterQualityIdx = 0;
1756 if (fPaintOverrides.fFilterQuality) {
1757 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1758 }
1759 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1760 "Default\0None\0Low\0Medium\0High\0\0"))
1761 {
1762 if (filterQualityIdx == 0) {
1763 fPaintOverrides.fFilterQuality = false;
1764 fPaint.setFilterQuality(kNone_SkFilterQuality);
1765 } else {
1766 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1767 fPaintOverrides.fFilterQuality = true;
1768 }
1769 paramsChanged = true;
1770 }
Ben Wagner9613e452019-01-23 10:34:59 -05001771 }
Hal Canary02738a82019-01-21 18:51:32 +00001772
Ben Wagner9613e452019-01-23 10:34:59 -05001773 if (ImGui::CollapsingHeader("Font")) {
1774 int hintingIdx = 0;
1775 if (fFontOverrides.fHinting) {
1776 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1777 }
1778 if (ImGui::Combo("Hinting", &hintingIdx,
1779 "Default\0None\0Slight\0Normal\0Full\0\0"))
1780 {
1781 if (hintingIdx == 0) {
1782 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001783 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001784 } else {
1785 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1786 fFontOverrides.fHinting = true;
1787 }
1788 paramsChanged = true;
1789 }
Hal Canary02738a82019-01-21 18:51:32 +00001790
Ben Wagner9613e452019-01-23 10:34:59 -05001791 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1792 bool SkFontFields::* flag,
1793 bool (SkFont::* isFlag)() const,
1794 void (SkFont::* setFlag)(bool) )
1795 {
1796 int itemIndex = 0;
1797 if (fFontOverrides.*flag) {
1798 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1799 }
1800 if (ImGui::Combo(label, &itemIndex, items)) {
1801 if (itemIndex == 0) {
1802 fFontOverrides.*flag = false;
1803 } else {
1804 fFontOverrides.*flag = true;
1805 (fFont.*setFlag)(itemIndex == 2);
1806 }
1807 paramsChanged = true;
1808 }
1809 };
Hal Canary02738a82019-01-21 18:51:32 +00001810
Ben Wagner9613e452019-01-23 10:34:59 -05001811 fontFlag("Fake Bold Glyphs",
1812 "Default\0No Fake Bold\0Fake Bold\0\0",
1813 &SkFontFields::fEmbolden,
1814 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001815
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001816 fontFlag("Baseline Snapping",
1817 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1818 &SkFontFields::fBaselineSnap,
1819 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1820
Ben Wagner9613e452019-01-23 10:34:59 -05001821 fontFlag("Linear Text",
1822 "Default\0No Linear Text\0Linear Text\0\0",
1823 &SkFontFields::fLinearMetrics,
1824 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001825
Ben Wagner9613e452019-01-23 10:34:59 -05001826 fontFlag("Subpixel Position Glyphs",
1827 "Default\0Pixel Text\0Subpixel Text\0\0",
1828 &SkFontFields::fSubpixel,
1829 &SkFont::isSubpixel, &SkFont::setSubpixel);
1830
1831 fontFlag("Embedded Bitmap Text",
1832 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1833 &SkFontFields::fEmbeddedBitmaps,
1834 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1835
1836 fontFlag("Force Auto-Hinting",
1837 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1838 &SkFontFields::fForceAutoHinting,
1839 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1840
1841 int edgingIdx = 0;
1842 if (fFontOverrides.fEdging) {
1843 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1844 }
1845 if (ImGui::Combo("Edging", &edgingIdx,
1846 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1847 {
1848 if (edgingIdx == 0) {
1849 fFontOverrides.fEdging = false;
1850 fFont.setEdging(SkFont::Edging::kAlias);
1851 } else {
1852 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1853 fFontOverrides.fEdging = true;
1854 }
1855 paramsChanged = true;
1856 }
1857
Ben Wagner15a8d572019-03-21 13:35:44 -04001858 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1859 if (fFontOverrides.fSize) {
1860 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001861 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001862 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001863 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001864 fFontOverrides.fSizeRange[0],
1865 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001866 "%.6f", 2.0f))
1867 {
Mike Reed3ae47332019-01-04 10:11:46 -05001868 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001869 paramsChanged = true;
1870 }
1871 }
1872
1873 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1874 if (fFontOverrides.fScaleX) {
1875 float scaleX = fFont.getScaleX();
1876 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1877 fFont.setScaleX(scaleX);
1878 paramsChanged = true;
1879 }
1880 }
1881
1882 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1883 if (fFontOverrides.fSkewX) {
1884 float skewX = fFont.getSkewX();
1885 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1886 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001887 paramsChanged = true;
1888 }
1889 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001890 }
1891
Mike Reed81f60ec2018-05-15 10:09:52 -04001892 {
1893 SkMetaData controls;
1894 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1895 if (ImGui::CollapsingHeader("Current Slide")) {
1896 SkMetaData::Iter iter(controls);
1897 const char* name;
1898 SkMetaData::Type type;
1899 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001900 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001901 if (type == SkMetaData::kScalar_Type) {
1902 float val[3];
1903 SkASSERT(count == 3);
1904 controls.findScalars(name, &count, val);
1905 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1906 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001907 }
Ben Wagner110c7032019-03-22 17:03:59 -04001908 } else if (type == SkMetaData::kBool_Type) {
1909 bool val;
1910 SkASSERT(count == 1);
1911 controls.findBool(name, &val);
1912 if (ImGui::Checkbox(name, &val)) {
1913 controls.setBool(name, val);
1914 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001915 }
1916 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001917 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001918 }
1919 }
1920 }
1921
Ben Wagner7a3c6742018-04-23 10:01:07 -04001922 if (fShowSlidePicker) {
1923 ImGui::SetNextTreeNodeOpen(true);
1924 }
Brian Osman79086b92017-02-10 13:36:16 -05001925 if (ImGui::CollapsingHeader("Slide")) {
1926 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001927 static ImVector<const char*> filteredSlideNames;
1928 static ImVector<int> filteredSlideIndices;
1929
Brian Osmanfce09c52017-11-14 15:32:20 -05001930 if (fShowSlidePicker) {
1931 ImGui::SetKeyboardFocusHere();
1932 fShowSlidePicker = false;
1933 }
1934
Brian Osman79086b92017-02-10 13:36:16 -05001935 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001936 filteredSlideNames.clear();
1937 filteredSlideIndices.clear();
1938 int filteredIndex = 0;
1939 for (int i = 0; i < fSlides.count(); ++i) {
1940 const char* slideName = fSlides[i]->getName().c_str();
1941 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1942 if (i == fCurrentSlide) {
1943 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001944 }
Brian Osmanf479e422017-11-08 13:11:36 -05001945 filteredSlideNames.push_back(slideName);
1946 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001947 }
Brian Osman79086b92017-02-10 13:36:16 -05001948 }
Brian Osmanf479e422017-11-08 13:11:36 -05001949
Brian Osmanf479e422017-11-08 13:11:36 -05001950 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1951 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001952 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001953 }
1954 }
Brian Osmana109e392017-02-24 09:49:14 -05001955
1956 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001957 ColorMode newMode = fColorMode;
1958 auto cmButton = [&](ColorMode mode, const char* label) {
1959 if (ImGui::RadioButton(label, mode == fColorMode)) {
1960 newMode = mode;
1961 }
1962 };
1963
1964 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001965 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1966 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001967
1968 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001969 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001970 }
1971
1972 // Pick from common gamuts:
1973 int primariesIdx = 4; // Default: Custom
1974 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1975 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1976 primariesIdx = i;
1977 break;
1978 }
1979 }
1980
Brian Osman03115dc2018-11-26 13:55:19 -05001981 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05001982 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001983
Brian Osmana109e392017-02-24 09:49:14 -05001984 if (ImGui::Combo("Primaries", &primariesIdx,
1985 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1986 if (primariesIdx >= 0 && primariesIdx <= 3) {
1987 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1988 }
1989 }
1990
1991 // Allow direct editing of gamut
1992 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1993 }
Brian Osman207d4102019-01-10 09:40:58 -05001994
1995 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04001996 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05001997 if (ImGui::Checkbox("Pause", &isPaused)) {
1998 fAnimTimer.togglePauseResume();
1999 }
Brian Osman707d2022019-01-10 11:27:34 -05002000
2001 float speed = fAnimTimer.getSpeed();
2002 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2003 fAnimTimer.setSpeed(speed);
2004 }
Brian Osman207d4102019-01-10 09:40:58 -05002005 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002006
Brian Osmanfd7657c2019-04-25 11:34:07 -04002007 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2008#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2009 || Window::kANGLE_BackendType == fBackendType
2010#endif
2011 ;
2012
2013 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2014 // than GL, we need to force it on. Just do that on the first frame after the backend
2015 // switch, then resume normal operation.
2016 if (!backendIsGL && !params.fGrContextOptions.fCacheSKSL) {
2017 params.fGrContextOptions.fCacheSKSL = true;
2018 paramsChanged = true;
2019 fPersistentCache.reset();
2020 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002021 // To re-load shaders from the currently active programs, we flush all caches on one
2022 // frame, then set a flag to poll the cache on the next frame.
2023 static bool gLoadPending = false;
2024 if (gLoadPending) {
2025 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2026 int hitCount) {
2027 CachedGLSL& entry(fCachedGLSL.push_back());
2028 entry.fKey = key;
2029 SkMD5 hash;
2030 hash.write(key->bytes(), key->size());
2031 SkMD5::Digest digest = hash.finish();
2032 for (int i = 0; i < 16; ++i) {
2033 entry.fKeyString.appendf("%02x", digest.data[i]);
2034 }
2035
Brian Osmana085a412019-04-25 09:44:43 -04002036 entry.fShaderType = GrPersistentCacheUtils::UnpackCachedShaders(
2037 data.get(), entry.fShader, entry.fInputs, kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002038 };
2039 fCachedGLSL.reset();
2040 fPersistentCache.foreach(collectShaders);
2041 gLoadPending = false;
2042 }
2043
2044 // Defer actually doing the load/save logic so that we can trigger a save when we
2045 // start or finish hovering on a tree node in the list below:
2046 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002047 bool doSave = ImGui::Button("Save");
2048 if (backendIsGL) {
2049 ImGui::SameLine();
2050 if (ImGui::Checkbox("SkSL", &params.fGrContextOptions.fCacheSKSL)) {
2051 paramsChanged = true;
2052 doLoad = true;
2053 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2054 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002055 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002056
2057 ImGui::BeginChild("##ScrollingRegion");
2058 for (auto& entry : fCachedGLSL) {
2059 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2060 bool hovered = ImGui::IsItemHovered();
2061 if (hovered != entry.fHovered) {
2062 // Force a save to patch the highlight shader in/out
2063 entry.fHovered = hovered;
2064 doSave = true;
2065 }
2066 if (inTreeNode) {
2067 // Full width, and a reasonable amount of space for each shader.
2068 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2069 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2070 boxSize);
2071 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2072 boxSize);
2073 ImGui::TreePop();
2074 }
2075 }
2076 ImGui::EndChild();
2077
2078 if (doLoad) {
2079 fPersistentCache.reset();
2080 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2081 gLoadPending = true;
2082 }
2083 if (doSave) {
2084 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002085 auto shaderCaps = ctx->priv().caps()->shaderCaps();
2086 bool sksl = params.fGrContextOptions.fCacheSKSL;
2087
Brian Osman072e6fc2019-06-12 11:35:41 -04002088 SkSL::String highlight;
2089 if (!sksl) {
2090 highlight = shaderCaps->versionDeclString();
2091 if (shaderCaps->usesPrecisionModifiers()) {
2092 highlight.append("precision mediump float;\n");
2093 }
Brian Osman5bee3902019-05-07 09:55:45 -04002094 }
2095 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002096 highlight.appendf("out %s sk_FragColor;\n"
2097 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2098 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002099
2100 fPersistentCache.reset();
2101 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2102 for (auto& entry : fCachedGLSL) {
2103 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2104 if (entry.fHovered) {
2105 entry.fShader[kFragment_GrShaderType] = highlight;
2106 }
2107
Brian Osmana085a412019-04-25 09:44:43 -04002108 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2109 entry.fShader,
2110 entry.fInputs,
2111 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002112 fPersistentCache.store(*entry.fKey, *data);
2113
2114 entry.fShader[kFragment_GrShaderType] = backup;
2115 }
2116 }
2117 }
Brian Osman79086b92017-02-10 13:36:16 -05002118 }
Brian Salomon99a33902017-03-07 15:16:34 -05002119 if (paramsChanged) {
2120 fDeferredActions.push_back([=]() {
2121 fWindow->setRequestedDisplayParams(params);
2122 fWindow->inval();
2123 this->updateTitle();
2124 });
2125 }
Brian Osman79086b92017-02-10 13:36:16 -05002126 ImGui::End();
2127 }
2128
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002129 if (gShaderErrorHandler.fErrors.count()) {
2130 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2131 ImGui::Begin("Shader Errors");
2132 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2133 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
2134 ImGui::TextWrapped("%s", gShaderErrorHandler.fShaders[i].c_str());
2135 }
2136 ImGui::End();
2137 gShaderErrorHandler.reset();
2138 }
2139
Brian Osmanf6877092017-02-13 09:39:57 -05002140 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002141 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2142 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002143 static int zoomFactor = 8;
2144 if (ImGui::Button("<<")) {
2145 zoomFactor = SkTMax(zoomFactor / 2, 4);
2146 }
2147 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2148 if (ImGui::Button(">>")) {
2149 zoomFactor = SkTMin(zoomFactor * 2, 32);
2150 }
Brian Osmanf6877092017-02-13 09:39:57 -05002151
Ben Wagner3627d2e2018-06-26 14:23:20 -04002152 if (!fZoomWindowFixed) {
2153 ImVec2 mousePos = ImGui::GetMousePos();
2154 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2155 }
2156 SkScalar x = fZoomWindowLocation.x();
2157 SkScalar y = fZoomWindowLocation.y();
2158 int xInt = SkScalarRoundToInt(x);
2159 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002160 ImVec2 avail = ImGui::GetContentRegionAvail();
2161
Brian Osmanead517d2017-11-13 15:36:36 -05002162 uint32_t pixel = 0;
2163 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002164 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002165 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002166 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002167 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002168 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002169 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2170 }
2171
Brian Osmand67e5182017-12-08 16:46:09 -05002172 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002173 // Translate so the region of the image that's under the mouse cursor is centered
2174 // in the zoom canvas:
2175 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002176 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2177 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002178 c->drawImage(this->fLastImage, 0, 0);
2179
2180 SkPaint outline;
2181 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002182 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002183 });
Brian Osmanf6877092017-02-13 09:39:57 -05002184 }
2185
2186 ImGui::End();
2187 }
Brian Osman79086b92017-02-10 13:36:16 -05002188}
2189
liyuqian2edb0f42016-07-06 14:11:32 -07002190void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002191 for (int i = 0; i < fDeferredActions.count(); ++i) {
2192 fDeferredActions[i]();
2193 }
2194 fDeferredActions.reset();
2195
Brian Osman56a24812017-12-19 11:15:16 -05002196 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002197 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002198 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002199 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002200
Brian Osman79086b92017-02-10 13:36:16 -05002201 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002202 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2203 // not be visible, though. So we need to redraw if there is at least one visible window, or
2204 // more than one active window. Newly created windows are active but not visible for one frame
2205 // while they determine their layout and sizing.
2206 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2207 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002208 fWindow->inval();
2209 }
jvanverth9f372462016-04-06 06:08:59 -07002210}
liyuqiane5a6cd92016-05-27 08:52:52 -07002211
Florin Malitab632df72018-06-18 21:23:06 -04002212template <typename OptionsFunc>
2213static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2214 OptionsFunc&& optionsFunc) {
2215 writer.beginObject();
2216 {
2217 writer.appendString(kName , name);
2218 writer.appendString(kValue, value);
2219
2220 writer.beginArray(kOptions);
2221 {
2222 optionsFunc(writer);
2223 }
2224 writer.endArray();
2225 }
2226 writer.endObject();
2227}
2228
2229
liyuqiane5a6cd92016-05-27 08:52:52 -07002230void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002231 if (!fWindow) {
2232 return;
2233 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002234 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002235 return; // Surface hasn't been created yet.
2236 }
2237
Florin Malitab632df72018-06-18 21:23:06 -04002238 SkDynamicMemoryWStream memStream;
2239 SkJSONWriter writer(&memStream);
2240 writer.beginArray();
2241
liyuqianb73c24b2016-06-03 08:47:23 -07002242 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002243 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2244 [this](SkJSONWriter& writer) {
2245 for(const auto& slide : fSlides) {
2246 writer.appendString(slide->getName().c_str());
2247 }
2248 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002249
liyuqianb73c24b2016-06-03 08:47:23 -07002250 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002251 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2252 [](SkJSONWriter& writer) {
2253 for (const auto& str : kBackendTypeStrings) {
2254 writer.appendString(str);
2255 }
2256 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002257
csmartdalton578f0642017-02-24 16:04:47 -07002258 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002259 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2260 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2261 [this](SkJSONWriter& writer) {
2262 writer.appendS32(0);
2263
2264 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2265 return;
2266 }
2267
2268 for (int msaa : {4, 8, 16}) {
2269 writer.appendS32(msaa);
2270 }
2271 });
csmartdalton578f0642017-02-24 16:04:47 -07002272
csmartdalton61cd31a2017-02-27 17:00:53 -07002273 // Path renderer state
2274 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002275 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2276 [this](SkJSONWriter& writer) {
2277 const GrContext* ctx = fWindow->getGrContext();
2278 if (!ctx) {
2279 writer.appendString("Software");
2280 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002281 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002282
Florin Malitab632df72018-06-18 21:23:06 -04002283 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2284 if (fWindow->sampleCount() > 1) {
2285 if (caps->shaderCaps()->pathRenderingSupport()) {
2286 writer.appendString(
2287 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2288 }
2289 } else {
2290 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2291 writer.appendString(
2292 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2293 }
2294 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2295 }
2296 writer.appendString(
2297 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2298 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2299 }
2300 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002301
liyuqianb73c24b2016-06-03 08:47:23 -07002302 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002303 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2304 [this](SkJSONWriter& writer) {
2305 writer.appendString(kSoftkeyHint);
2306 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2307 writer.appendString(softkey.c_str());
2308 }
2309 });
liyuqianb73c24b2016-06-03 08:47:23 -07002310
Florin Malitab632df72018-06-18 21:23:06 -04002311 writer.endArray();
2312 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002313
Florin Malitab632df72018-06-18 21:23:06 -04002314 auto data = memStream.detachAsData();
2315
2316 // TODO: would be cool to avoid this copy
2317 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2318
2319 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002320}
2321
2322void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002323 // For those who will add more features to handle the state change in this function:
2324 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2325 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2326 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002327 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002328 for (int i = 0; i < fSlides.count(); ++i) {
2329 if (fSlides[i]->getName().equals(stateValue)) {
2330 this->setCurrentSlide(i);
2331 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002332 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002333 }
Florin Malitaab99c342018-01-16 16:23:03 -05002334
2335 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002336 } else if (stateName.equals(kBackendStateName)) {
2337 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2338 if (stateValue.equals(kBackendTypeStrings[i])) {
2339 if (fBackendType != i) {
2340 fBackendType = (sk_app::Window::BackendType)i;
2341 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002342 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002343 }
2344 break;
2345 }
2346 }
csmartdalton578f0642017-02-24 16:04:47 -07002347 } else if (stateName.equals(kMSAAStateName)) {
2348 DisplayParams params = fWindow->getRequestedDisplayParams();
2349 int sampleCount = atoi(stateValue.c_str());
2350 if (sampleCount != params.fMSAASampleCount) {
2351 params.fMSAASampleCount = sampleCount;
2352 fWindow->setRequestedDisplayParams(params);
2353 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002354 this->updateTitle();
2355 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002356 }
2357 } else if (stateName.equals(kPathRendererStateName)) {
2358 DisplayParams params = fWindow->getRequestedDisplayParams();
2359 for (const auto& pair : gPathRendererNames) {
2360 if (pair.second == stateValue.c_str()) {
2361 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2362 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2363 fWindow->setRequestedDisplayParams(params);
2364 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002365 this->updateTitle();
2366 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002367 }
2368 break;
2369 }
csmartdalton578f0642017-02-24 16:04:47 -07002370 }
liyuqianb73c24b2016-06-03 08:47:23 -07002371 } else if (stateName.equals(kSoftkeyStateName)) {
2372 if (!stateValue.equals(kSoftkeyHint)) {
2373 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002374 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002375 }
liyuqian2edb0f42016-07-06 14:11:32 -07002376 } else if (stateName.equals(kRefreshStateName)) {
2377 // This state is actually NOT in the UI state.
2378 // We use this to allow Android to quickly set bool fRefresh.
2379 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002380 } else {
2381 SkDebugf("Unknown stateName: %s", stateName.c_str());
2382 }
2383}
Brian Osman79086b92017-02-10 13:36:16 -05002384
Hal Canaryb1f411a2019-08-29 10:39:22 -04002385bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002386 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002387}
2388
Hal Canaryb1f411a2019-08-29 10:39:22 -04002389bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002390 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002391 fWindow->inval();
2392 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002393 } else {
2394 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002395 }
Brian Osman79086b92017-02-10 13:36:16 -05002396}