blob: a01845d0f64731e4d67563deae7d1fb85e99a264 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkGraphics.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkSurface.h"
14#include "include/gpu/GrContext.h"
15#include "include/private/SkTo.h"
16#include "include/utils/SkPaintFilterCanvas.h"
17#include "src/core/SkColorSpacePriv.h"
18#include "src/core/SkImagePriv.h"
19#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkOSFile.h"
21#include "src/core/SkScan.h"
22#include "src/core/SkTaskGroup.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrGpu.h"
25#include "src/gpu/GrPersistentCacheUtils.h"
Chris Dalton77912982019-12-16 11:18:13 -070026#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#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
Jim Van Verth7b558182019-11-14 16:47:01 -0500132static DEFINE_bool(redraw, false, "Toggle continuous redraw.");
133
Mike Kleinc6142d82019-03-25 10:54:59 -0500134
Brian Salomon194db172017-08-17 14:37:06 -0400135const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700136 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400137#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
138 "ANGLE",
139#endif
Stephen Whitea800ec92019-08-02 15:04:52 -0400140#ifdef SK_DAWN
141 "Dawn",
142#endif
jvanverth063ece72016-06-17 09:29:14 -0700143#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700144 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700145#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400146#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -0500147 "Metal",
148#endif
csmartdalton578f0642017-02-24 16:04:47 -0700149 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700150};
151
bsalomon6c471f72016-07-26 12:56:32 -0700152static sk_app::Window::BackendType get_backend_type(const char* str) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400153#ifdef SK_DAWN
154 if (0 == strcmp(str, "dawn")) {
155 return sk_app::Window::kDawn_BackendType;
156 } else
157#endif
bsalomon6c471f72016-07-26 12:56:32 -0700158#ifdef SK_VULKAN
159 if (0 == strcmp(str, "vk")) {
160 return sk_app::Window::kVulkan_BackendType;
161 } else
162#endif
Brian Salomon194db172017-08-17 14:37:06 -0400163#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
164 if (0 == strcmp(str, "angle")) {
165 return sk_app::Window::kANGLE_BackendType;
166 } else
167#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -0400168#ifdef SK_METAL
169 if (0 == strcmp(str, "mtl")) {
170 return sk_app::Window::kMetal_BackendType;
171 } else
Jim Van Verthbe39f712019-02-08 15:36:14 -0500172#endif
bsalomon6c471f72016-07-26 12:56:32 -0700173 if (0 == strcmp(str, "gl")) {
174 return sk_app::Window::kNativeGL_BackendType;
175 } else if (0 == strcmp(str, "sw")) {
176 return sk_app::Window::kRaster_BackendType;
177 } else {
178 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
179 return sk_app::Window::kRaster_BackendType;
180 }
181}
182
Brian Osmana109e392017-02-24 09:49:14 -0500183static SkColorSpacePrimaries gSrgbPrimaries = {
184 0.64f, 0.33f,
185 0.30f, 0.60f,
186 0.15f, 0.06f,
187 0.3127f, 0.3290f };
188
189static SkColorSpacePrimaries gAdobePrimaries = {
190 0.64f, 0.33f,
191 0.21f, 0.71f,
192 0.15f, 0.06f,
193 0.3127f, 0.3290f };
194
195static SkColorSpacePrimaries gP3Primaries = {
196 0.680f, 0.320f,
197 0.265f, 0.690f,
198 0.150f, 0.060f,
199 0.3127f, 0.3290f };
200
201static SkColorSpacePrimaries gRec2020Primaries = {
202 0.708f, 0.292f,
203 0.170f, 0.797f,
204 0.131f, 0.046f,
205 0.3127f, 0.3290f };
206
207struct NamedPrimaries {
208 const char* fName;
209 SkColorSpacePrimaries* fPrimaries;
210} gNamedPrimaries[] = {
211 { "sRGB", &gSrgbPrimaries },
212 { "AdobeRGB", &gAdobePrimaries },
213 { "P3", &gP3Primaries },
214 { "Rec. 2020", &gRec2020Primaries },
215};
216
217static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
218 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
219}
220
Brian Osman70d2f432017-11-08 09:54:10 -0500221static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
222 // In raster mode, we still use GL for the window.
223 // This lets us render the GUI faster (and correct).
224 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
225}
226
Jim Van Verth74826c82019-03-01 14:37:30 -0500227class NullSlide : public Slide {
228 SkISize getDimensions() const override {
229 return SkISize::Make(640, 480);
230 }
231
232 void draw(SkCanvas* canvas) override {
233 canvas->clear(0xffff11ff);
234 }
235};
236
liyuqiane5a6cd92016-05-27 08:52:52 -0700237const char* kName = "name";
238const char* kValue = "value";
239const char* kOptions = "options";
240const char* kSlideStateName = "Slide";
241const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700242const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700243const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700244const char* kSoftkeyStateName = "Softkey";
245const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700246const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700247const char* kON = "ON";
248const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700249const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700250
jvanverth34524262016-05-04 13:49:13 -0700251Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500252 : fCurrentSlide(-1)
253 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500254 , fSaveToSKP(false)
Mike Reed376d8122019-03-14 11:39:02 -0400255 , fShowSlideDimensions(false)
Brian Osman79086b92017-02-10 13:36:16 -0500256 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500257 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500258 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500259 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400260 , fZoomWindowFixed(false)
261 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500262 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400263 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700264 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500265 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500266 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500267 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500268 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700269 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400270 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400271 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400272 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500273 , fTiled(false)
274 , fDrawTileBoundaries(false)
275 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400276 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700277{
Greg Daniel285db442016-10-14 09:12:53 -0400278 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700279
Brian Osmanf09e35e2017-12-15 14:48:09 -0500280 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500281 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500282 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Daltonc3318f02019-07-19 14:20:53 -0600283 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "CCPR";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500284 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
285 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700286
jvanverth2bb3b6d2016-04-08 07:24:09 -0700287 SkDebugf("Command line arguments: ");
288 for (int i = 1; i < argc; ++i) {
289 SkDebugf("%s ", argv[i]);
290 }
291 SkDebugf("\n");
292
Mike Klein88544fb2019-03-20 10:50:33 -0500293 CommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500294#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400295 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500296#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700297
Mike Klein19cc0f62019-03-22 15:30:07 -0500298 ToolUtils::SetDefaultFontMgr();
Ben Wagner483c7722018-02-20 17:06:07 -0500299
Brian Osmanbc8150f2017-07-24 11:38:01 -0400300 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400301 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400302
bsalomon6c471f72016-07-26 12:56:32 -0700303 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700304 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700305
csmartdalton578f0642017-02-24 16:04:47 -0700306 DisplayParams displayParams;
307 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700308 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
Brian Osman0b8bb882019-04-12 11:47:19 -0400309 displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
Brian Osmana66081d2019-09-03 14:59:26 -0400310 displayParams.fGrContextOptions.fShaderCacheStrategy =
311 GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400312 displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
313 displayParams.fGrContextOptions.fSuppressPrints = true;
Chris Daltona1638a52019-06-24 11:54:24 -0600314 displayParams.fGrContextOptions.fInternalMultisampleCount = FLAGS_internalSamples;
csmartdalton578f0642017-02-24 16:04:47 -0700315 fWindow->setRequestedDisplayParams(displayParams);
Jim Van Verth7b558182019-11-14 16:47:01 -0500316 fRefresh = FLAGS_redraw;
csmartdalton578f0642017-02-24 16:04:47 -0700317
Brian Osman56a24812017-12-19 11:15:16 -0500318 // Configure timers
319 fStatsLayer.setActive(false);
320 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
321 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
322 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
323
jvanverth9f372462016-04-06 06:08:59 -0700324 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700325 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500326 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500327 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500328 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700329
brianosman622c8d52016-05-10 06:50:49 -0700330 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500331 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
332 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
333 fWindow->inval();
334 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500335 // Command to jump directly to the slide picker and give it focus
336 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
337 this->fShowImGuiDebugWindow = true;
338 this->fShowSlidePicker = true;
339 fWindow->inval();
340 });
341 // Alias that to Backspace, to match SampleApp
Hal Canaryb1f411a2019-08-29 10:39:22 -0400342 fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
Brian Osmanfce09c52017-11-14 15:32:20 -0500343 this->fShowImGuiDebugWindow = true;
344 this->fShowSlidePicker = true;
345 fWindow->inval();
346 });
Brian Osman79086b92017-02-10 13:36:16 -0500347 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
348 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
349 fWindow->inval();
350 });
Brian Osmanf6877092017-02-13 09:39:57 -0500351 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
352 this->fShowZoomWindow = !this->fShowZoomWindow;
353 fWindow->inval();
354 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400355 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
356 this->fZoomWindowFixed = !this->fZoomWindowFixed;
357 fWindow->inval();
358 });
Greg Danield0794cc2019-03-27 16:23:26 -0400359 fCommands.addCommand('v', "VSync", "Toggle vsync on/off", [this]() {
360 DisplayParams params = fWindow->getRequestedDisplayParams();
361 params.fDisableVsync = !params.fDisableVsync;
362 fWindow->setRequestedDisplayParams(params);
363 this->updateTitle();
364 fWindow->inval();
365 });
Mike Reedf702ed42019-07-22 17:00:49 -0400366 fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() {
367 fRefresh = !fRefresh;
368 fWindow->inval();
369 });
brianosman622c8d52016-05-10 06:50:49 -0700370 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500371 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700372 fWindow->inval();
373 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400374 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500375 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400376 this->updateTitle();
377 fWindow->inval();
378 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500379 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500380 switch (fColorMode) {
381 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500382 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500383 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500384 case ColorMode::kColorManaged8888:
385 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500386 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500387 case ColorMode::kColorManagedF16:
Brian Salomon8391bac2019-09-18 11:22:44 -0400388 this->setColorMode(ColorMode::kColorManagedF16Norm);
389 break;
390 case ColorMode::kColorManagedF16Norm:
Brian Osman92004802017-03-06 11:47:26 -0500391 this->setColorMode(ColorMode::kLegacy);
392 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500393 }
brianosman622c8d52016-05-10 06:50:49 -0700394 });
Chris Dalton1215cda2019-12-17 21:44:04 -0700395 fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() {
396 DisplayParams params = fWindow->getRequestedDisplayParams();
397 params.fGrContextOptions.fWireframeMode = !params.fGrContextOptions.fWireframeMode;
398 fWindow->setRequestedDisplayParams(params);
399 fWindow->inval();
400 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400401 fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500402 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700403 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400404 fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500405 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700406 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400407 fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700408 this->changeZoomLevel(1.f / 32.f);
409 fWindow->inval();
410 });
Hal Canaryb1f411a2019-08-29 10:39:22 -0400411 fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
brianosman622c8d52016-05-10 06:50:49 -0700412 this->changeZoomLevel(-1.f / 32.f);
413 fWindow->inval();
414 });
jvanverthaf236b52016-05-20 06:01:06 -0700415 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400416 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
417 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500418 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400419#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
420 if (newBackend == sk_app::Window::kVulkan_BackendType) {
421 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
422 sk_app::Window::kBackendTypeCount);
423 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
424 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500425 }
426#endif
Brian Osman621491e2017-02-28 15:45:01 -0500427 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700428 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500429 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
430 fSaveToSKP = true;
431 fWindow->inval();
432 });
Mike Reed376d8122019-03-14 11:39:02 -0400433 fCommands.addCommand('&', "Overlays", "Show slide dimensios", [this]() {
434 fShowSlideDimensions = !fShowSlideDimensions;
435 fWindow->inval();
436 });
Ben Wagner37c54032018-04-13 14:30:23 -0400437 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
438 DisplayParams params = fWindow->getRequestedDisplayParams();
439 uint32_t flags = params.fSurfaceProps.flags();
440 if (!fPixelGeometryOverrides) {
441 fPixelGeometryOverrides = true;
442 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
443 } else {
444 switch (params.fSurfaceProps.pixelGeometry()) {
445 case kUnknown_SkPixelGeometry:
446 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
447 break;
448 case kRGB_H_SkPixelGeometry:
449 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
450 break;
451 case kBGR_H_SkPixelGeometry:
452 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
453 break;
454 case kRGB_V_SkPixelGeometry:
455 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
456 break;
457 case kBGR_V_SkPixelGeometry:
458 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
459 fPixelGeometryOverrides = false;
460 break;
461 }
462 }
463 fWindow->setRequestedDisplayParams(params);
464 this->updateTitle();
465 fWindow->inval();
466 });
Ben Wagner9613e452019-01-23 10:34:59 -0500467 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500468 if (!fFontOverrides.fHinting) {
469 fFontOverrides.fHinting = true;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400470 fFont.setHinting(SkFontHinting::kNone);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500471 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500472 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400473 case SkFontHinting::kNone:
474 fFont.setHinting(SkFontHinting::kSlight);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500475 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400476 case SkFontHinting::kSlight:
477 fFont.setHinting(SkFontHinting::kNormal);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500478 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400479 case SkFontHinting::kNormal:
480 fFont.setHinting(SkFontHinting::kFull);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500481 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400482 case SkFontHinting::kFull:
483 fFont.setHinting(SkFontHinting::kNone);
Mike Reed3ae47332019-01-04 10:11:46 -0500484 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500485 break;
486 }
487 }
488 this->updateTitle();
489 fWindow->inval();
490 });
491 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500492 if (!fPaintOverrides.fAntiAlias) {
493 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
494 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500495 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500496 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500497 } else {
498 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500499 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500500 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500501 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400502 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500503 break;
504 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500505 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500506 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400507 gSkForceAnalyticAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500508 break;
509 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500510 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400511 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500512 break;
513 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500514 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
515 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500516 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
517 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500518 break;
519 }
520 }
521 this->updateTitle();
522 fWindow->inval();
523 });
Ben Wagner37c54032018-04-13 14:30:23 -0400524 fCommands.addCommand('D', "Modes", "DFT", [this]() {
525 DisplayParams params = fWindow->getRequestedDisplayParams();
526 uint32_t flags = params.fSurfaceProps.flags();
527 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
528 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
529 fWindow->setRequestedDisplayParams(params);
530 this->updateTitle();
531 fWindow->inval();
532 });
Ben Wagner9613e452019-01-23 10:34:59 -0500533 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500534 if (!fFontOverrides.fEdging) {
535 fFontOverrides.fEdging = true;
536 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500537 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500538 switch (fFont.getEdging()) {
539 case SkFont::Edging::kAlias:
540 fFont.setEdging(SkFont::Edging::kAntiAlias);
541 break;
542 case SkFont::Edging::kAntiAlias:
543 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
544 break;
545 case SkFont::Edging::kSubpixelAntiAlias:
546 fFont.setEdging(SkFont::Edging::kAlias);
547 fFontOverrides.fEdging = false;
548 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500549 }
550 }
551 this->updateTitle();
552 fWindow->inval();
553 });
Ben Wagner9613e452019-01-23 10:34:59 -0500554 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500555 if (!fFontOverrides.fSubpixel) {
556 fFontOverrides.fSubpixel = true;
557 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500558 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500559 if (!fFont.isSubpixel()) {
560 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500561 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500562 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500563 }
564 }
565 this->updateTitle();
566 fWindow->inval();
567 });
Ben Wagner54aa8842019-08-27 16:20:39 -0400568 fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() {
569 if (!fFontOverrides.fBaselineSnap) {
570 fFontOverrides.fBaselineSnap = true;
571 fFont.setBaselineSnap(false);
572 } else {
573 if (!fFont.isBaselineSnap()) {
574 fFont.setBaselineSnap(true);
575 } else {
576 fFontOverrides.fBaselineSnap = false;
577 }
578 }
579 this->updateTitle();
580 fWindow->inval();
581 });
Brian Osman805a7272018-05-02 15:40:20 -0400582 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
583 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
584 : kPerspective_Real;
585 this->updateTitle();
586 fWindow->inval();
587 });
588 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
589 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
590 : kPerspective_Off;
591 this->updateTitle();
592 fWindow->inval();
593 });
Brian Osman207d4102019-01-10 09:40:58 -0500594 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
595 fAnimTimer.togglePauseResume();
596 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400597 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
598 fZoomUI = !fZoomUI;
599 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
600 fWindow->inval();
601 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500602
jvanverth2bb3b6d2016-04-08 07:24:09 -0700603 // set up slides
604 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500605 if (FLAGS_list) {
606 this->listNames();
607 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700608
Brian Osman9bb47cf2018-04-26 15:55:00 -0400609 fPerspectivePoints[0].set(0, 0);
610 fPerspectivePoints[1].set(1, 0);
611 fPerspectivePoints[2].set(0, 1);
612 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700613 fAnimTimer.run();
614
Hal Canaryc465d132017-12-08 10:21:31 -0500615 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500616 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400617 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500618 }
619 fImGuiGamutPaint.setColor(SK_ColorWHITE);
620 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
621
jongdeok.kim804f17e2019-02-26 14:39:23 +0900622 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth74826c82019-03-01 14:37:30 -0500623 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700624}
625
jvanverth34524262016-05-04 13:49:13 -0700626void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400627 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
628 static const struct {
629 const char* fExtension;
630 const char* fDirName;
Mike Klein88544fb2019-03-20 10:50:33 -0500631 const CommandLineFlags::StringArray& fFlags;
Florin Malita0ffa3222018-04-05 14:34:45 -0400632 const SlideFactory fFactory;
633 } gExternalSlidesInfo[] = {
634 { ".skp", "skp-dir", FLAGS_skps,
635 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
636 return sk_make_sp<SKPSlide>(name, path);}
637 },
638 { ".jpg", "jpg-dir", FLAGS_jpgs,
639 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
640 return sk_make_sp<ImageSlide>(name, path);}
641 },
Florin Malita87ccf332018-05-04 12:23:24 -0400642#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400643 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400644 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
645 return sk_make_sp<SkottieSlide>(name, path);}
646 },
Florin Malita87ccf332018-05-04 12:23:24 -0400647#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400648#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400649 { ".svg", "svg-dir", FLAGS_svgs,
650 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
651 return sk_make_sp<SvgSlide>(name, path);}
652 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400653#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400654 };
jvanverthc265a922016-04-08 12:51:45 -0700655
Brian Salomon343553a2018-09-05 15:41:23 -0400656 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700657
Mike Klein88544fb2019-03-20 10:50:33 -0500658 const auto addSlide =
659 [&](const SkString& name, const SkString& path, const SlideFactory& fact) {
660 if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
661 return;
662 }
liyuqian6f163d22016-06-13 12:26:45 -0700663
Mike Klein88544fb2019-03-20 10:50:33 -0500664 if (auto slide = fact(name, path)) {
665 dirSlides.push_back(slide);
666 fSlides.push_back(std::move(slide));
667 }
668 };
Florin Malita76a076b2018-02-15 18:40:48 -0500669
Florin Malita38792ce2018-05-08 10:36:18 -0400670 if (!FLAGS_file.isEmpty()) {
671 // single file mode
672 const SkString file(FLAGS_file[0]);
673
674 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
675 for (const auto& sinfo : gExternalSlidesInfo) {
676 if (file.endsWith(sinfo.fExtension)) {
677 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
678 return;
679 }
680 }
681
682 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
683 } else {
684 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
685 }
686
687 return;
688 }
689
690 // Bisect slide.
691 if (!FLAGS_bisect.isEmpty()) {
692 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
Mike Klein88544fb2019-03-20 10:50:33 -0500693 if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400694 if (FLAGS_bisect.count() >= 2) {
695 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
696 bisect->onChar(*ch);
697 }
698 }
699 fSlides.push_back(std::move(bisect));
700 }
701 }
702
703 // GMs
704 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400705 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
Ben Wagner406ff502019-08-12 16:39:24 -0400706 std::unique_ptr<skiagm::GM> gm = gmFactory();
Mike Klein88544fb2019-03-20 10:50:33 -0500707 if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
Ben Wagner406ff502019-08-12 16:39:24 -0400708 sk_sp<Slide> slide(new GMSlide(std::move(gm)));
Florin Malita38792ce2018-05-08 10:36:18 -0400709 fSlides.push_back(std::move(slide));
710 }
Florin Malita38792ce2018-05-08 10:36:18 -0400711 }
712 // reverse gms
713 int numGMs = fSlides.count() - firstGM;
714 for (int i = 0; i < numGMs/2; ++i) {
715 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
716 }
717
718 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400719 for (const SampleFactory factory : SampleRegistry::Range()) {
720 sk_sp<Slide> slide(new SampleSlide(factory));
Mike Klein88544fb2019-03-20 10:50:33 -0500721 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Florin Malita38792ce2018-05-08 10:36:18 -0400722 fSlides.push_back(slide);
723 }
Florin Malita38792ce2018-05-08 10:36:18 -0400724 }
725
Brian Osman7c979f52019-02-12 13:27:51 -0500726 // Particle demo
727 {
728 // TODO: Convert this to a sample
729 sk_sp<Slide> slide(new ParticlesSlide());
Mike Klein88544fb2019-03-20 10:50:33 -0500730 if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
Brian Osman7c979f52019-02-12 13:27:51 -0500731 fSlides.push_back(std::move(slide));
732 }
733 }
734
Florin Malita0ffa3222018-04-05 14:34:45 -0400735 for (const auto& info : gExternalSlidesInfo) {
736 for (const auto& flag : info.fFlags) {
737 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
738 // single file
739 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
740 } else {
741 // directory
742 SkOSFile::Iter it(flag.c_str(), info.fExtension);
743 SkString name;
744 while (it.next(&name)) {
745 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
746 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400747 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400748 if (!dirSlides.empty()) {
749 fSlides.push_back(
750 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
751 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500752 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400753 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400754 }
755 }
Jim Van Verth74826c82019-03-01 14:37:30 -0500756
757 if (!fSlides.count()) {
758 sk_sp<Slide> slide(new NullSlide());
759 fSlides.push_back(std::move(slide));
760 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700761}
762
763
jvanverth34524262016-05-04 13:49:13 -0700764Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700765 fWindow->detach();
766 delete fWindow;
767}
768
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500769struct SkPaintTitleUpdater {
770 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
771 void append(const char* s) {
772 if (fCount == 0) {
773 fTitle->append(" {");
774 } else {
775 fTitle->append(", ");
776 }
777 fTitle->append(s);
778 ++fCount;
779 }
780 void done() {
781 if (fCount > 0) {
782 fTitle->append("}");
783 }
784 }
785 SkString* fTitle;
786 int fCount;
787};
788
brianosman05de2162016-05-06 13:28:57 -0700789void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700790 if (!fWindow) {
791 return;
792 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500793 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700794 return; // Surface hasn't been created yet.
795 }
796
jvanverth34524262016-05-04 13:49:13 -0700797 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700798 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700799
Mike Kleine5acd752019-03-22 09:57:16 -0500800 if (gSkUseAnalyticAA) {
Yuqian Li399b3c22017-08-03 11:08:15 -0400801 if (gSkForceAnalyticAA) {
802 title.append(" <FAAA>");
803 } else {
804 title.append(" <AAA>");
805 }
806 }
807
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500808 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500809 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
810 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400811 const char* on, const char* off)
812 {
Ben Wagner9613e452019-01-23 10:34:59 -0500813 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400814 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500815 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400816 };
817
Ben Wagner9613e452019-01-23 10:34:59 -0500818 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
819 const char* on, const char* off)
820 {
821 if (fFontOverrides.*flag) {
822 paintTitle.append((fFont.*isFlag)() ? on : off);
823 }
824 };
825
826 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
827 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagnerd10a78f2019-03-07 13:14:26 -0500828 if (fPaintOverrides.fFilterQuality) {
829 switch (fPaint.getFilterQuality()) {
830 case kNone_SkFilterQuality:
831 paintTitle.append("NoFilter");
832 break;
833 case kLow_SkFilterQuality:
834 paintTitle.append("LowFilter");
835 break;
836 case kMedium_SkFilterQuality:
837 paintTitle.append("MediumFilter");
838 break;
839 case kHigh_SkFilterQuality:
840 paintTitle.append("HighFilter");
841 break;
842 }
843 }
Ben Wagner9613e452019-01-23 10:34:59 -0500844
845 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
846 "Force Autohint", "No Force Autohint");
847 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
Ben Wagnerc17de1d2019-08-26 16:59:09 -0400848 fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap");
Ben Wagner9613e452019-01-23 10:34:59 -0500849 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
850 "Linear Metrics", "Non-Linear Metrics");
851 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
852 "Bitmap Text", "No Bitmap Text");
853 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
854
855 if (fFontOverrides.fEdging) {
856 switch (fFont.getEdging()) {
857 case SkFont::Edging::kAlias:
858 paintTitle.append("Alias Text");
859 break;
860 case SkFont::Edging::kAntiAlias:
861 paintTitle.append("Antialias Text");
862 break;
863 case SkFont::Edging::kSubpixelAntiAlias:
864 paintTitle.append("Subpixel Antialias Text");
865 break;
866 }
867 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400868
Mike Reed3ae47332019-01-04 10:11:46 -0500869 if (fFontOverrides.fHinting) {
870 switch (fFont.getHinting()) {
Ben Wagner5785e4a2019-05-07 16:50:29 -0400871 case SkFontHinting::kNone:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500872 paintTitle.append("No Hinting");
873 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400874 case SkFontHinting::kSlight:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500875 paintTitle.append("Slight Hinting");
876 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400877 case SkFontHinting::kNormal:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500878 paintTitle.append("Normal Hinting");
879 break;
Ben Wagner5785e4a2019-05-07 16:50:29 -0400880 case SkFontHinting::kFull:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500881 paintTitle.append("Full Hinting");
882 break;
883 }
884 }
885 paintTitle.done();
886
Brian Osman92004802017-03-06 11:47:26 -0500887 switch (fColorMode) {
888 case ColorMode::kLegacy:
889 title.append(" Legacy 8888");
890 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500891 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500892 title.append(" ColorManaged 8888");
893 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500894 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500895 title.append(" ColorManaged F16");
896 break;
Brian Salomon8391bac2019-09-18 11:22:44 -0400897 case ColorMode::kColorManagedF16Norm:
898 title.append(" ColorManaged F16 Norm");
899 break;
Brian Osman92004802017-03-06 11:47:26 -0500900 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500901
Brian Osman92004802017-03-06 11:47:26 -0500902 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500903 int curPrimaries = -1;
904 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
905 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
906 curPrimaries = i;
907 break;
908 }
909 }
Brian Osman03115dc2018-11-26 13:55:19 -0500910 title.appendf(" %s Gamma %f",
911 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500912 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700913 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500914
Ben Wagner37c54032018-04-13 14:30:23 -0400915 const DisplayParams& params = fWindow->getRequestedDisplayParams();
916 if (fPixelGeometryOverrides) {
917 switch (params.fSurfaceProps.pixelGeometry()) {
918 case kUnknown_SkPixelGeometry:
919 title.append( " Flat");
920 break;
921 case kRGB_H_SkPixelGeometry:
922 title.append( " RGB");
923 break;
924 case kBGR_H_SkPixelGeometry:
925 title.append( " BGR");
926 break;
927 case kRGB_V_SkPixelGeometry:
928 title.append( " RGBV");
929 break;
930 case kBGR_V_SkPixelGeometry:
931 title.append( " BGRV");
932 break;
933 }
934 }
935
936 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
937 title.append(" DFT");
938 }
939
csmartdalton578f0642017-02-24 16:04:47 -0700940 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700941 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500942 int msaa = fWindow->sampleCount();
943 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700944 title.appendf(" MSAA: %i", msaa);
945 }
946 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700947
948 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Chris Daltona8fbeba2019-03-30 00:31:23 -0600949 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700950 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
951 }
952
Brian Osman805a7272018-05-02 15:40:20 -0400953 if (kPerspective_Real == fPerspectiveMode) {
954 title.append(" Perpsective (Real)");
955 } else if (kPerspective_Fake == fPerspectiveMode) {
956 title.append(" Perspective (Fake)");
957 }
958
brianosman05de2162016-05-06 13:28:57 -0700959 fWindow->setTitle(title.c_str());
960}
961
Florin Malitaab99c342018-01-16 16:23:03 -0500962int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500963
964 if (!FLAGS_slide.isEmpty()) {
965 int count = fSlides.count();
966 for (int i = 0; i < count; i++) {
967 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500968 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500969 }
970 }
971
972 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
973 this->listNames();
974 }
975
Florin Malitaab99c342018-01-16 16:23:03 -0500976 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500977}
978
Florin Malitaab99c342018-01-16 16:23:03 -0500979void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500980 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500981 for (const auto& slide : fSlides) {
982 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500983 }
984}
985
Florin Malitaab99c342018-01-16 16:23:03 -0500986void Viewer::setCurrentSlide(int slide) {
987 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700988
Florin Malitaab99c342018-01-16 16:23:03 -0500989 if (slide == fCurrentSlide) {
990 return;
991 }
992
993 if (fCurrentSlide >= 0) {
994 fSlides[fCurrentSlide]->unload();
995 }
996
997 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
998 SkIntToScalar(fWindow->height()));
999 fCurrentSlide = slide;
1000 this->setupCurrentSlide();
1001}
1002
1003void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -05001004 if (fCurrentSlide >= 0) {
1005 // prepare dimensions for image slides
1006 fGesture.resetTouchState();
1007 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -07001008
Jim Van Verth0848fb02018-01-22 13:39:30 -05001009 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1010 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1011 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -04001012
Jim Van Verth0848fb02018-01-22 13:39:30 -05001013 // Start with a matrix that scales the slide to the available screen space
1014 if (fWindow->scaleContentToFit()) {
1015 if (windowRect.width() > 0 && windowRect.height() > 0) {
1016 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
1017 }
liyuqiane46e4f02016-05-20 07:32:19 -07001018 }
Jim Van Verth0848fb02018-01-22 13:39:30 -05001019
1020 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -04001021 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -05001022
1023 this->updateTitle();
1024 this->updateUIState();
1025
1026 fStatsLayer.resetMeasurements();
1027
1028 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -07001029 }
jvanverthc265a922016-04-08 12:51:45 -07001030}
1031
1032#define MAX_ZOOM_LEVEL 8
1033#define MIN_ZOOM_LEVEL -8
1034
jvanverth34524262016-05-04 13:49:13 -07001035void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -07001036 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -04001037 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001038 this->preTouchMatrixChanged();
1039}
Yuqian Li755778c2018-03-28 16:23:31 -04001040
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001041void Viewer::preTouchMatrixChanged() {
1042 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -04001043 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1044 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
1045 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
1046 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
1047}
1048
Brian Osman805a7272018-05-02 15:40:20 -04001049SkMatrix Viewer::computePerspectiveMatrix() {
1050 SkScalar w = fWindow->width(), h = fWindow->height();
1051 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
1052 SkPoint perspPts[4] = {
1053 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
1054 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
1055 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
1056 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
1057 };
1058 SkMatrix m;
1059 m.setPolyToPoly(orthoPts, perspPts, 4);
1060 return m;
1061}
1062
Yuqian Li755778c2018-03-28 16:23:31 -04001063SkMatrix Viewer::computePreTouchMatrix() {
1064 SkMatrix m = fDefaultMatrix;
Ben Wagnercc8eb862019-03-21 16:50:22 -04001065
1066 SkScalar zoomScale = exp(fZoomLevel);
Ben Wagner897dfa22018-08-09 15:18:46 -04001067 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -04001068 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001069
1070 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
1071 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001072
Brian Osman805a7272018-05-02 15:40:20 -04001073 if (kPerspective_Real == fPerspectiveMode) {
1074 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -04001075 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001076 }
1077
Yuqian Li755778c2018-03-28 16:23:31 -04001078 return m;
jvanverthc265a922016-04-08 12:51:45 -07001079}
1080
liyuqiand3cdbca2016-05-17 12:44:20 -07001081SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -04001082 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -07001083 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -04001084 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -07001085 return m;
jvanverthc265a922016-04-08 12:51:45 -07001086}
1087
Brian Osman621491e2017-02-28 15:45:01 -05001088void Viewer::setBackend(sk_app::Window::BackendType backendType) {
Brian Osman5bee3902019-05-07 09:55:45 -04001089 fPersistentCache.reset();
1090 fCachedGLSL.reset();
Brian Osman621491e2017-02-28 15:45:01 -05001091 fBackendType = backendType;
1092
1093 fWindow->detach();
1094
Brian Osman70d2f432017-11-08 09:54:10 -05001095#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -04001096 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
1097 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -05001098 DisplayParams params = fWindow->getRequestedDisplayParams();
1099 delete fWindow;
1100 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -05001101
Brian Osman70d2f432017-11-08 09:54:10 -05001102 // re-register callbacks
1103 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -05001104 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -05001105 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -05001106 fWindow->pushLayer(&fImGuiLayer);
1107
Brian Osman70d2f432017-11-08 09:54:10 -05001108 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
1109 // will still include our correct sample count. But the re-created fWindow will lose that
1110 // information. On Windows, we need to re-create the window when changing sample count,
1111 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
1112 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
1113 // as if we had never changed windows at all).
1114 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -05001115#endif
1116
Brian Osman70d2f432017-11-08 09:54:10 -05001117 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -05001118}
1119
Brian Osman92004802017-03-06 11:47:26 -05001120void Viewer::setColorMode(ColorMode colorMode) {
1121 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001122 this->updateTitle();
1123 fWindow->inval();
1124}
1125
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001126class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1127public:
Mike Reed3ae47332019-01-04 10:11:46 -05001128 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1129 SkFont* font, Viewer::SkFontFields* ffields)
1130 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001131 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001132 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1133 sk_sp<SkTextBlob>* cache) {
1134 bool blobWillChange = false;
1135 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001136 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1137 bool shouldDraw = this->filterFont(&filteredFont);
1138 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001139 blobWillChange = true;
1140 break;
1141 }
1142 }
1143 if (!blobWillChange) {
1144 return blob;
1145 }
1146
1147 SkTextBlobBuilder builder;
1148 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001149 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1150 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001151 if (!shouldDraw) {
1152 continue;
1153 }
1154
Mike Reed3ae47332019-01-04 10:11:46 -05001155 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001156
Ben Wagner41e40472018-09-24 13:01:54 -04001157 const SkTextBlobBuilder::RunBuffer& runBuffer
1158 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001159 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001160 it.glyphCount(), it.offset().x(),it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001161 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001162 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner0bb2db12019-03-06 18:19:08 -05001163 it.glyphCount(), it.offset().y(), it.textSize(), SkString())
Ben Wagner41e40472018-09-24 13:01:54 -04001164 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001165 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001166 it.glyphCount(), it.textSize(), SkString())
1167 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1168 uint32_t glyphCount = it.glyphCount();
1169 if (it.glyphs()) {
1170 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1171 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1172 }
1173 if (it.pos()) {
1174 size_t posSize = sizeof(decltype(*it.pos()));
1175 uint8_t positioning = it.positioning();
1176 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1177 }
1178 if (it.text()) {
1179 size_t textSize = sizeof(decltype(*it.text()));
1180 uint32_t textCount = it.textSize();
1181 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1182 }
1183 if (it.clusters()) {
1184 size_t clusterSize = sizeof(decltype(*it.clusters()));
1185 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1186 }
1187 }
1188 *cache = builder.make();
1189 return cache->get();
1190 }
1191 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1192 const SkPaint& paint) override {
1193 sk_sp<SkTextBlob> cache;
1194 this->SkPaintFilterCanvas::onDrawTextBlob(
1195 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1196 }
Mike Reed3ae47332019-01-04 10:11:46 -05001197 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
Ben Wagner15a8d572019-03-21 13:35:44 -04001198 if (fFontOverrides->fSize) {
Mike Reed3ae47332019-01-04 10:11:46 -05001199 font->writable()->setSize(fFont->getSize());
1200 }
Ben Wagner15a8d572019-03-21 13:35:44 -04001201 if (fFontOverrides->fScaleX) {
1202 font->writable()->setScaleX(fFont->getScaleX());
1203 }
1204 if (fFontOverrides->fSkewX) {
1205 font->writable()->setSkewX(fFont->getSkewX());
1206 }
Mike Reed3ae47332019-01-04 10:11:46 -05001207 if (fFontOverrides->fHinting) {
1208 font->writable()->setHinting(fFont->getHinting());
1209 }
Ben Wagner9613e452019-01-23 10:34:59 -05001210 if (fFontOverrides->fEdging) {
1211 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001212 }
Ben Wagner9613e452019-01-23 10:34:59 -05001213 if (fFontOverrides->fEmbolden) {
1214 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001215 }
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001216 if (fFontOverrides->fBaselineSnap) {
1217 font->writable()->setBaselineSnap(fFont->isBaselineSnap());
1218 }
Ben Wagner9613e452019-01-23 10:34:59 -05001219 if (fFontOverrides->fLinearMetrics) {
1220 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001221 }
Ben Wagner9613e452019-01-23 10:34:59 -05001222 if (fFontOverrides->fSubpixel) {
1223 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001224 }
Ben Wagner9613e452019-01-23 10:34:59 -05001225 if (fFontOverrides->fEmbeddedBitmaps) {
1226 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001227 }
Ben Wagner9613e452019-01-23 10:34:59 -05001228 if (fFontOverrides->fForceAutoHinting) {
1229 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001230 }
Ben Wagner9613e452019-01-23 10:34:59 -05001231
Mike Reed3ae47332019-01-04 10:11:46 -05001232 return true;
1233 }
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001234 bool onFilter(SkPaint& paint) const override {
Ben Wagner9613e452019-01-23 10:34:59 -05001235 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001236 paint.setAntiAlias(fPaint->isAntiAlias());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001237 }
Ben Wagner9613e452019-01-23 10:34:59 -05001238 if (fPaintOverrides->fDither) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001239 paint.setDither(fPaint->isDither());
Ben Wagner99a78dc2018-05-09 18:23:51 -04001240 }
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001241 if (fPaintOverrides->fFilterQuality) {
Ben Wagnerf55fa0d2018-08-27 18:11:57 -04001242 paint.setFilterQuality(fPaint->getFilterQuality());
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001243 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001244 return true;
1245 }
1246 SkPaint* fPaint;
1247 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001248 SkFont* fFont;
1249 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001250};
1251
Robert Phillips9882dae2019-03-04 11:00:10 -05001252void Viewer::drawSlide(SkSurface* surface) {
Jim Van Verth74826c82019-03-01 14:37:30 -05001253 if (fCurrentSlide < 0) {
1254 return;
1255 }
1256
Robert Phillips9882dae2019-03-04 11:00:10 -05001257 SkAutoCanvasRestore autorestore(surface->getCanvas(), false);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001258
Brian Osmanf750fbc2017-02-08 10:47:28 -05001259 // By default, we render directly into the window's surface/canvas
Robert Phillips9882dae2019-03-04 11:00:10 -05001260 SkSurface* slideSurface = surface;
1261 SkCanvas* slideCanvas = surface->getCanvas();
Brian Osmanf6877092017-02-13 09:39:57 -05001262 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001263
Brian Osmane0d4fba2017-03-15 10:24:55 -04001264 // 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 -05001265 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001266 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001267 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001268 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001269 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001270 }
1271
Brian Osman3ac99cf2017-12-01 11:23:53 -05001272 if (fSaveToSKP) {
1273 SkPictureRecorder recorder;
1274 SkCanvas* recorderCanvas = recorder.beginRecording(
1275 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001276 fSlides[fCurrentSlide]->draw(recorderCanvas);
1277 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1278 SkFILEWStream stream("sample_app.skp");
1279 picture->serialize(&stream);
1280 fSaveToSKP = false;
1281 }
1282
Brian Osmane9ed0f02018-11-26 14:50:05 -05001283 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
Brian Salomon8391bac2019-09-18 11:22:44 -04001284 SkColorType colorType;
1285 switch (fColorMode) {
1286 case ColorMode::kLegacy:
1287 case ColorMode::kColorManaged8888:
1288 colorType = kN32_SkColorType;
1289 break;
1290 case ColorMode::kColorManagedF16:
1291 colorType = kRGBA_F16_SkColorType;
1292 break;
1293 case ColorMode::kColorManagedF16Norm:
1294 colorType = kRGBA_F16Norm_SkColorType;
1295 break;
1296 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001297
1298 auto make_surface = [=](int w, int h) {
Robert Phillips9882dae2019-03-04 11:00:10 -05001299 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1300 slideCanvas->getProps(&props);
1301
Brian Osmane9ed0f02018-11-26 14:50:05 -05001302 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1303 return Window::kRaster_BackendType == this->fBackendType
1304 ? SkSurface::MakeRaster(info, &props)
Robert Phillips9882dae2019-03-04 11:00:10 -05001305 : slideCanvas->makeSurface(info, &props);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001306 };
1307
Brian Osman03115dc2018-11-26 13:55:19 -05001308 // We need to render offscreen if we're...
1309 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1310 // ... in any raster mode, because the window surface is actually GL
1311 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001312 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001313 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001314 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001315 Window::kRaster_BackendType == fBackendType ||
1316 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001317
Brian Osmane9ed0f02018-11-26 14:50:05 -05001318 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Robert Phillips9882dae2019-03-04 11:00:10 -05001319 slideSurface = offscreenSurface.get();
Mike Klein48b64902018-07-25 13:28:44 -04001320 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001321 }
1322
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001323 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001324 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001325 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001326 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001327 if (fTiled) {
1328 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1329 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1330 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1331 SkCanvas* tileCanvas = tileSurface->getCanvas();
1332 SkMatrix m = this->computeMatrix();
1333 for (int y = 0; y < fWindow->height(); y += tileH) {
1334 for (int x = 0; x < fWindow->width(); x += tileW) {
1335 SkAutoCanvasRestore acr(tileCanvas, true);
1336 tileCanvas->translate(-x, -y);
1337 tileCanvas->clear(SK_ColorTRANSPARENT);
1338 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001339 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1340 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001341 fSlides[fCurrentSlide]->draw(&filterCanvas);
1342 tileSurface->draw(slideCanvas, x, y, nullptr);
1343 }
1344 }
1345
1346 // Draw borders between tiles
1347 if (fDrawTileBoundaries) {
1348 SkPaint border;
1349 border.setColor(0x60FF00FF);
1350 border.setStyle(SkPaint::kStroke_Style);
1351 for (int y = 0; y < fWindow->height(); y += tileH) {
1352 for (int x = 0; x < fWindow->width(); x += tileW) {
1353 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1354 }
1355 }
1356 }
1357 } else {
1358 slideCanvas->concat(this->computeMatrix());
1359 if (kPerspective_Real == fPerspectiveMode) {
1360 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1361 }
Mike Reed3ae47332019-01-04 10:11:46 -05001362 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001363 fSlides[fCurrentSlide]->draw(&filterCanvas);
1364 }
Brian Osman56a24812017-12-19 11:15:16 -05001365 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001366 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001367
1368 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001369 fStatsLayer.beginTiming(fFlushTimer);
Robert Phillips9882dae2019-03-04 11:00:10 -05001370 slideSurface->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001371 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001372
1373 // If we rendered offscreen, snap an image and push the results to the window's canvas
1374 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001375 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001376
Robert Phillips9882dae2019-03-04 11:00:10 -05001377 SkCanvas* canvas = surface->getCanvas();
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001378 SkPaint paint;
1379 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001380 int prePerspectiveCount = canvas->save();
1381 if (kPerspective_Fake == fPerspectiveMode) {
1382 paint.setFilterQuality(kHigh_SkFilterQuality);
1383 canvas->clear(SK_ColorWHITE);
1384 canvas->concat(this->computePerspectiveMatrix());
1385 }
Brian Osman03115dc2018-11-26 13:55:19 -05001386 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001387 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001388 }
Mike Reed376d8122019-03-14 11:39:02 -04001389
1390 if (fShowSlideDimensions) {
1391 SkRect r = SkRect::Make(fSlides[fCurrentSlide]->getDimensions());
1392 SkPaint paint;
1393 paint.setColor(0x40FFFF00);
1394 surface->getCanvas()->drawRect(r, paint);
1395 }
liyuqian6f163d22016-06-13 12:26:45 -07001396}
1397
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001398void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001399 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001400 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001401}
Jim Van Verth6f449692017-02-14 15:16:46 -05001402
Robert Phillips9882dae2019-03-04 11:00:10 -05001403void Viewer::onPaint(SkSurface* surface) {
1404 this->drawSlide(surface);
jvanverthc265a922016-04-08 12:51:45 -07001405
Robert Phillips9882dae2019-03-04 11:00:10 -05001406 fCommands.drawHelp(surface->getCanvas());
liyuqian2edb0f42016-07-06 14:11:32 -07001407
Brian Osmand67e5182017-12-08 16:46:09 -05001408 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001409
1410 if (GrContext* ctx = fWindow->getGrContext()) {
1411 // Clean out cache items that haven't been used in more than 10 seconds.
1412 ctx->performDeferredCleanup(std::chrono::seconds(10));
1413 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001414}
1415
Ben Wagnera1915972018-08-09 15:06:19 -04001416void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001417 if (fCurrentSlide >= 0) {
1418 fSlides[fCurrentSlide]->resize(width, height);
1419 }
Ben Wagnera1915972018-08-09 15:06:19 -04001420}
1421
Florin Malitacefc1b92018-02-19 21:43:47 -05001422SkPoint Viewer::mapEvent(float x, float y) {
1423 const auto m = this->computeMatrix();
1424 SkMatrix inv;
1425
1426 SkAssertResult(m.invert(&inv));
1427
1428 return inv.mapXY(x, y);
1429}
1430
Hal Canaryb1f411a2019-08-29 10:39:22 -04001431bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001432 if (GestureDevice::kMouse == fGestureDevice) {
1433 return false;
1434 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001435
1436 const auto slidePt = this->mapEvent(x, y);
Hal Canaryb1f411a2019-08-29 10:39:22 -04001437 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) {
Florin Malitacefc1b92018-02-19 21:43:47 -05001438 fWindow->inval();
1439 return true;
1440 }
1441
liyuqiand3cdbca2016-05-17 12:44:20 -07001442 void* castedOwner = reinterpret_cast<void*>(owner);
1443 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001444 case skui::InputState::kUp: {
liyuqiand3cdbca2016-05-17 12:44:20 -07001445 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001446#if defined(SK_BUILD_FOR_IOS)
1447 // TODO: move IOS swipe detection higher up into the platform code
1448 SkPoint dir;
1449 if (fGesture.isFling(&dir)) {
1450 // swiping left or right
1451 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1452 if (dir.fX < 0) {
1453 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1454 fCurrentSlide + 1 : 0);
1455 } else {
1456 this->setCurrentSlide(fCurrentSlide > 0 ?
1457 fCurrentSlide - 1 : fSlides.count() - 1);
1458 }
1459 }
1460 fGesture.reset();
1461 }
1462#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001463 break;
1464 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001465 case skui::InputState::kDown: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001466 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001467 break;
1468 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001469 case skui::InputState::kMove: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001470 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001471 break;
1472 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001473 default: {
1474 // kLeft and kRight are only for swipes
1475 SkASSERT(false);
1476 break;
1477 }
liyuqiand3cdbca2016-05-17 12:44:20 -07001478 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001479 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001480 fWindow->inval();
1481 return true;
1482}
1483
Hal Canaryb1f411a2019-08-29 10:39:22 -04001484bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001485 if (GestureDevice::kTouch == fGestureDevice) {
1486 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001487 }
Brian Osman16c81a12017-12-20 11:58:34 -05001488
Florin Malitacefc1b92018-02-19 21:43:47 -05001489 const auto slidePt = this->mapEvent(x, y);
1490 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1491 fWindow->inval();
1492 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001493 }
1494
1495 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -04001496 case skui::InputState::kUp: {
Brian Osman16c81a12017-12-20 11:58:34 -05001497 fGesture.touchEnd(nullptr);
1498 break;
1499 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001500 case skui::InputState::kDown: {
Brian Osman16c81a12017-12-20 11:58:34 -05001501 fGesture.touchBegin(nullptr, x, y);
1502 break;
1503 }
Hal Canaryb1f411a2019-08-29 10:39:22 -04001504 case skui::InputState::kMove: {
Brian Osman16c81a12017-12-20 11:58:34 -05001505 fGesture.touchMoved(nullptr, x, y);
1506 break;
1507 }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001508 default: {
1509 SkASSERT(false); // shouldn't see kRight or kLeft here
1510 break;
1511 }
Brian Osman16c81a12017-12-20 11:58:34 -05001512 }
1513 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1514
Hal Canaryb1f411a2019-08-29 10:39:22 -04001515 if (state != skui::InputState::kMove || fGesture.isBeingTouched()) {
Brian Osman16c81a12017-12-20 11:58:34 -05001516 fWindow->inval();
1517 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001518 return true;
1519}
1520
Jim Van Verthd0cf5da2019-09-09 16:53:39 -04001521bool Viewer::onFling(skui::InputState state) {
1522 if (skui::InputState::kRight == state) {
1523 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
1524 return true;
1525 } else if (skui::InputState::kLeft == state) {
1526 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
1527 return true;
1528 }
1529 return false;
1530}
1531
1532bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) {
1533 switch (state) {
1534 case skui::InputState::kDown:
1535 fGesture.startZoom();
1536 return true;
1537 break;
1538 case skui::InputState::kMove:
1539 fGesture.updateZoom(scale, x, y, x, y);
1540 return true;
1541 break;
1542 case skui::InputState::kUp:
1543 fGesture.endZoom();
1544 return true;
1545 break;
1546 default:
1547 SkASSERT(false);
1548 break;
1549 }
1550
1551 return false;
1552}
1553
Brian Osmana109e392017-02-24 09:49:14 -05001554static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
Brian Osman535c5e32019-02-09 16:32:58 -05001555 // The gamut image covers a (0.8 x 0.9) shaped region
1556 ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f });
Brian Osmana109e392017-02-24 09:49:14 -05001557
1558 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1559 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1560 // Magic numbers are pixel locations of the origin and upper-right corner.
Brian Osman535c5e32019-02-09 16:32:58 -05001561 dc.fDrawList->AddImage(gamutPaint, dc.fPos,
1562 ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y),
1563 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001564
Brian Osman535c5e32019-02-09 16:32:58 -05001565 dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040);
1566 dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000);
1567 dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000);
1568 dc.dragPoint((SkPoint*)(&primaries->fWX), true);
1569 dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001570}
1571
Ben Wagner3627d2e2018-06-26 14:23:20 -04001572static bool ImGui_DragLocation(SkPoint* pt) {
Brian Osman535c5e32019-02-09 16:32:58 -05001573 ImGui::DragCanvas dc(pt);
1574 dc.fillColor(IM_COL32(0, 0, 0, 128));
1575 dc.dragPoint(pt);
1576 return dc.fDragging;
Ben Wagner3627d2e2018-06-26 14:23:20 -04001577}
1578
Brian Osman9bb47cf2018-04-26 15:55:00 -04001579static bool ImGui_DragQuad(SkPoint* pts) {
Brian Osman535c5e32019-02-09 16:32:58 -05001580 ImGui::DragCanvas dc(pts);
1581 dc.fillColor(IM_COL32(0, 0, 0, 128));
Brian Osman9bb47cf2018-04-26 15:55:00 -04001582
Brian Osman535c5e32019-02-09 16:32:58 -05001583 for (int i = 0; i < 4; ++i) {
1584 dc.dragPoint(pts + i);
1585 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001586
Brian Osman535c5e32019-02-09 16:32:58 -05001587 dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF);
1588 dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF);
1589 dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF);
1590 dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001591
Brian Osman535c5e32019-02-09 16:32:58 -05001592 return dc.fDragging;
Brian Osmana109e392017-02-24 09:49:14 -05001593}
1594
Brian Osmand67e5182017-12-08 16:46:09 -05001595void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001596 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1597 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001598 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001599 }
1600
1601 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001602 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1603 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001604 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001605 DisplayParams params = fWindow->getRequestedDisplayParams();
1606 bool paramsChanged = false;
Brian Osman0b8bb882019-04-12 11:47:19 -04001607 const GrContext* ctx = fWindow->getGrContext();
1608
Brian Osmana109e392017-02-24 09:49:14 -05001609 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1610 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001611 if (ImGui::CollapsingHeader("Backend")) {
1612 int newBackend = static_cast<int>(fBackendType);
1613 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1614 ImGui::SameLine();
1615 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001616#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1617 ImGui::SameLine();
1618 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1619#endif
Stephen Whitea800ec92019-08-02 15:04:52 -04001620#if defined(SK_DAWN)
1621 ImGui::SameLine();
1622 ImGui::RadioButton("Dawn", &newBackend, sk_app::Window::kDawn_BackendType);
1623#endif
Brian Osman621491e2017-02-28 15:45:01 -05001624#if defined(SK_VULKAN)
1625 ImGui::SameLine();
1626 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1627#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -04001628#if defined(SK_METAL)
Jim Van Verthbe39f712019-02-08 15:36:14 -05001629 ImGui::SameLine();
1630 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1631#endif
Brian Osman621491e2017-02-28 15:45:01 -05001632 if (newBackend != fBackendType) {
1633 fDeferredActions.push_back([=]() {
1634 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1635 });
1636 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001637
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001638 bool* wire = &params.fGrContextOptions.fWireframeMode;
1639 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1640 paramsChanged = true;
1641 }
Brian Salomon99a33902017-03-07 15:16:34 -05001642
Brian Osman28b12522017-03-08 17:10:24 -05001643 if (ctx) {
1644 int sampleCount = fWindow->sampleCount();
1645 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001646 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001647 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1648 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1649 ImGui::RadioButton("16", &sampleCount, 16);
1650
1651 if (sampleCount != params.fMSAASampleCount) {
1652 params.fMSAASampleCount = sampleCount;
1653 paramsChanged = true;
1654 }
1655 }
1656
Ben Wagner37c54032018-04-13 14:30:23 -04001657 int pixelGeometryIdx = 0;
1658 if (fPixelGeometryOverrides) {
1659 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1660 }
1661 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1662 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1663 {
1664 uint32_t flags = params.fSurfaceProps.flags();
1665 if (pixelGeometryIdx == 0) {
1666 fPixelGeometryOverrides = false;
1667 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1668 } else {
1669 fPixelGeometryOverrides = true;
1670 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1671 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1672 }
1673 paramsChanged = true;
1674 }
1675
1676 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1677 if (ImGui::Checkbox("DFT", &useDFT)) {
1678 uint32_t flags = params.fSurfaceProps.flags();
1679 if (useDFT) {
1680 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1681 } else {
1682 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1683 }
1684 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1685 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1686 paramsChanged = true;
1687 }
1688
Brian Osman8a9de3d2017-03-01 14:59:05 -05001689 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001690 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001691 auto prButton = [&](GpuPathRenderers x) {
1692 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001693 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1694 params.fGrContextOptions.fGpuPathRenderers = x;
1695 paramsChanged = true;
1696 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001697 }
1698 };
1699
1700 if (!ctx) {
1701 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001702 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001703 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001704 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001705 prButton(GpuPathRenderers::kStencilAndCover);
1706 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001707 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001708 prButton(GpuPathRenderers::kNone);
1709 } else {
1710 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001711 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001712 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001713 prButton(GpuPathRenderers::kCoverageCounting);
1714 }
Jim Van Verth83010462017-03-16 08:45:39 -04001715 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001716 prButton(GpuPathRenderers::kTessellating);
1717 prButton(GpuPathRenderers::kNone);
1718 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001719 ImGui::TreePop();
1720 }
Brian Osman621491e2017-02-28 15:45:01 -05001721 }
1722
Ben Wagner964571d2019-03-08 12:35:06 -05001723 if (ImGui::CollapsingHeader("Tiling")) {
1724 ImGui::Checkbox("Enable", &fTiled);
1725 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1726 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1727 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1728 }
1729
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001730 if (ImGui::CollapsingHeader("Transform")) {
1731 float zoom = fZoomLevel;
1732 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1733 fZoomLevel = zoom;
1734 this->preTouchMatrixChanged();
1735 paramsChanged = true;
1736 }
1737 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001738 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001739 fRotation = deg;
1740 this->preTouchMatrixChanged();
1741 paramsChanged = true;
1742 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001743 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1744 if (ImGui_DragLocation(&fOffset)) {
1745 this->preTouchMatrixChanged();
1746 paramsChanged = true;
1747 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001748 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1749 this->preTouchMatrixChanged();
1750 paramsChanged = true;
1751 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001752 }
Brian Osman805a7272018-05-02 15:40:20 -04001753 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1754 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1755 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001756 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001757 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001758 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001759 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001760 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001761 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001762 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001763 }
1764
Ben Wagnera580fb32018-04-17 11:16:32 -04001765 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001766 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001767 if (fPaintOverrides.fAntiAlias) {
1768 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001769 }
1770 if (ImGui::Combo("Anti-Alias", &aliasIdx,
Mike Kleine5acd752019-03-22 09:57:16 -05001771 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0\0"))
Ben Wagnera580fb32018-04-17 11:16:32 -04001772 {
1773 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1774 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
Ben Wagnera580fb32018-04-17 11:16:32 -04001775 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001776 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1777 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001778 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001779 fPaintOverrides.fAntiAlias = true;
1780 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001781 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001782 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001783 case SkPaintFields::AntiAliasState::Alias:
1784 break;
1785 case SkPaintFields::AntiAliasState::Normal:
1786 break;
1787 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1788 gSkUseAnalyticAA = true;
1789 gSkForceAnalyticAA = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001790 break;
1791 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1792 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -04001793 break;
1794 }
1795 }
1796 paramsChanged = true;
1797 }
1798
Ben Wagner99a78dc2018-05-09 18:23:51 -04001799 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001800 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001801 bool (SkPaint::* isFlag)() const,
1802 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001803 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001804 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001805 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001806 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001807 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001808 if (ImGui::Combo(label, &itemIndex, items)) {
1809 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001810 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001811 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001812 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001813 (fPaint.*setFlag)(itemIndex == 2);
1814 }
1815 paramsChanged = true;
1816 }
1817 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001818
Ben Wagner99a78dc2018-05-09 18:23:51 -04001819 paintFlag("Dither",
1820 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001821 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001822 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagnerd10a78f2019-03-07 13:14:26 -05001823
1824 int filterQualityIdx = 0;
1825 if (fPaintOverrides.fFilterQuality) {
1826 filterQualityIdx = SkTo<int>(fPaint.getFilterQuality()) + 1;
1827 }
1828 if (ImGui::Combo("Filter Quality", &filterQualityIdx,
1829 "Default\0None\0Low\0Medium\0High\0\0"))
1830 {
1831 if (filterQualityIdx == 0) {
1832 fPaintOverrides.fFilterQuality = false;
1833 fPaint.setFilterQuality(kNone_SkFilterQuality);
1834 } else {
1835 fPaint.setFilterQuality(SkTo<SkFilterQuality>(filterQualityIdx - 1));
1836 fPaintOverrides.fFilterQuality = true;
1837 }
1838 paramsChanged = true;
1839 }
Ben Wagner9613e452019-01-23 10:34:59 -05001840 }
Hal Canary02738a82019-01-21 18:51:32 +00001841
Ben Wagner9613e452019-01-23 10:34:59 -05001842 if (ImGui::CollapsingHeader("Font")) {
1843 int hintingIdx = 0;
1844 if (fFontOverrides.fHinting) {
1845 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1846 }
1847 if (ImGui::Combo("Hinting", &hintingIdx,
1848 "Default\0None\0Slight\0Normal\0Full\0\0"))
1849 {
1850 if (hintingIdx == 0) {
1851 fFontOverrides.fHinting = false;
Ben Wagner5785e4a2019-05-07 16:50:29 -04001852 fFont.setHinting(SkFontHinting::kNone);
Ben Wagner9613e452019-01-23 10:34:59 -05001853 } else {
1854 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1855 fFontOverrides.fHinting = true;
1856 }
1857 paramsChanged = true;
1858 }
Hal Canary02738a82019-01-21 18:51:32 +00001859
Ben Wagner9613e452019-01-23 10:34:59 -05001860 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1861 bool SkFontFields::* flag,
1862 bool (SkFont::* isFlag)() const,
1863 void (SkFont::* setFlag)(bool) )
1864 {
1865 int itemIndex = 0;
1866 if (fFontOverrides.*flag) {
1867 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1868 }
1869 if (ImGui::Combo(label, &itemIndex, items)) {
1870 if (itemIndex == 0) {
1871 fFontOverrides.*flag = false;
1872 } else {
1873 fFontOverrides.*flag = true;
1874 (fFont.*setFlag)(itemIndex == 2);
1875 }
1876 paramsChanged = true;
1877 }
1878 };
Hal Canary02738a82019-01-21 18:51:32 +00001879
Ben Wagner9613e452019-01-23 10:34:59 -05001880 fontFlag("Fake Bold Glyphs",
1881 "Default\0No Fake Bold\0Fake Bold\0\0",
1882 &SkFontFields::fEmbolden,
1883 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001884
Ben Wagnerc17de1d2019-08-26 16:59:09 -04001885 fontFlag("Baseline Snapping",
1886 "Default\0No Baseline Snapping\0Baseline Snapping\0\0",
1887 &SkFontFields::fBaselineSnap,
1888 &SkFont::isBaselineSnap, &SkFont::setBaselineSnap);
1889
Ben Wagner9613e452019-01-23 10:34:59 -05001890 fontFlag("Linear Text",
1891 "Default\0No Linear Text\0Linear Text\0\0",
1892 &SkFontFields::fLinearMetrics,
1893 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001894
Ben Wagner9613e452019-01-23 10:34:59 -05001895 fontFlag("Subpixel Position Glyphs",
1896 "Default\0Pixel Text\0Subpixel Text\0\0",
1897 &SkFontFields::fSubpixel,
1898 &SkFont::isSubpixel, &SkFont::setSubpixel);
1899
1900 fontFlag("Embedded Bitmap Text",
1901 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1902 &SkFontFields::fEmbeddedBitmaps,
1903 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1904
1905 fontFlag("Force Auto-Hinting",
1906 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1907 &SkFontFields::fForceAutoHinting,
1908 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1909
1910 int edgingIdx = 0;
1911 if (fFontOverrides.fEdging) {
1912 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1913 }
1914 if (ImGui::Combo("Edging", &edgingIdx,
1915 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1916 {
1917 if (edgingIdx == 0) {
1918 fFontOverrides.fEdging = false;
1919 fFont.setEdging(SkFont::Edging::kAlias);
1920 } else {
1921 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1922 fFontOverrides.fEdging = true;
1923 }
1924 paramsChanged = true;
1925 }
1926
Ben Wagner15a8d572019-03-21 13:35:44 -04001927 ImGui::Checkbox("Override Size", &fFontOverrides.fSize);
1928 if (fFontOverrides.fSize) {
1929 ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001930 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001931 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001932 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Ben Wagner15a8d572019-03-21 13:35:44 -04001933 fFontOverrides.fSizeRange[0],
1934 fFontOverrides.fSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001935 "%.6f", 2.0f))
1936 {
Mike Reed3ae47332019-01-04 10:11:46 -05001937 fFont.setSize(textSize);
Ben Wagner15a8d572019-03-21 13:35:44 -04001938 paramsChanged = true;
1939 }
1940 }
1941
1942 ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX);
1943 if (fFontOverrides.fScaleX) {
1944 float scaleX = fFont.getScaleX();
1945 if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1946 fFont.setScaleX(scaleX);
1947 paramsChanged = true;
1948 }
1949 }
1950
1951 ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX);
1952 if (fFontOverrides.fSkewX) {
1953 float skewX = fFont.getSkewX();
1954 if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1955 fFont.setSkewX(skewX);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001956 paramsChanged = true;
1957 }
1958 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001959 }
1960
Mike Reed81f60ec2018-05-15 10:09:52 -04001961 {
1962 SkMetaData controls;
1963 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1964 if (ImGui::CollapsingHeader("Current Slide")) {
1965 SkMetaData::Iter iter(controls);
1966 const char* name;
1967 SkMetaData::Type type;
1968 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001969 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001970 if (type == SkMetaData::kScalar_Type) {
1971 float val[3];
1972 SkASSERT(count == 3);
1973 controls.findScalars(name, &count, val);
1974 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1975 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001976 }
Ben Wagner110c7032019-03-22 17:03:59 -04001977 } else if (type == SkMetaData::kBool_Type) {
1978 bool val;
1979 SkASSERT(count == 1);
1980 controls.findBool(name, &val);
1981 if (ImGui::Checkbox(name, &val)) {
1982 controls.setBool(name, val);
1983 }
Mike Reed81f60ec2018-05-15 10:09:52 -04001984 }
1985 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001986 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001987 }
1988 }
1989 }
1990
Ben Wagner7a3c6742018-04-23 10:01:07 -04001991 if (fShowSlidePicker) {
1992 ImGui::SetNextTreeNodeOpen(true);
1993 }
Brian Osman79086b92017-02-10 13:36:16 -05001994 if (ImGui::CollapsingHeader("Slide")) {
1995 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001996 static ImVector<const char*> filteredSlideNames;
1997 static ImVector<int> filteredSlideIndices;
1998
Brian Osmanfce09c52017-11-14 15:32:20 -05001999 if (fShowSlidePicker) {
2000 ImGui::SetKeyboardFocusHere();
2001 fShowSlidePicker = false;
2002 }
2003
Brian Osman79086b92017-02-10 13:36:16 -05002004 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05002005 filteredSlideNames.clear();
2006 filteredSlideIndices.clear();
2007 int filteredIndex = 0;
2008 for (int i = 0; i < fSlides.count(); ++i) {
2009 const char* slideName = fSlides[i]->getName().c_str();
2010 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
2011 if (i == fCurrentSlide) {
2012 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05002013 }
Brian Osmanf479e422017-11-08 13:11:36 -05002014 filteredSlideNames.push_back(slideName);
2015 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05002016 }
Brian Osman79086b92017-02-10 13:36:16 -05002017 }
Brian Osmanf479e422017-11-08 13:11:36 -05002018
Brian Osmanf479e422017-11-08 13:11:36 -05002019 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
2020 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002021 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05002022 }
2023 }
Brian Osmana109e392017-02-24 09:49:14 -05002024
2025 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05002026 ColorMode newMode = fColorMode;
2027 auto cmButton = [&](ColorMode mode, const char* label) {
2028 if (ImGui::RadioButton(label, mode == fColorMode)) {
2029 newMode = mode;
2030 }
2031 };
2032
2033 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05002034 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
2035 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Salomon8391bac2019-09-18 11:22:44 -04002036 cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm");
Brian Osman92004802017-03-06 11:47:26 -05002037
2038 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05002039 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05002040 }
2041
2042 // Pick from common gamuts:
2043 int primariesIdx = 4; // Default: Custom
2044 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
2045 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
2046 primariesIdx = i;
2047 break;
2048 }
2049 }
2050
Brian Osman03115dc2018-11-26 13:55:19 -05002051 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05002052 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05002053
Brian Osmana109e392017-02-24 09:49:14 -05002054 if (ImGui::Combo("Primaries", &primariesIdx,
2055 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
2056 if (primariesIdx >= 0 && primariesIdx <= 3) {
2057 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
2058 }
2059 }
2060
2061 // Allow direct editing of gamut
2062 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
2063 }
Brian Osman207d4102019-01-10 09:40:58 -05002064
2065 if (ImGui::CollapsingHeader("Animation")) {
Hal Canary41248072019-07-11 16:32:53 -04002066 bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state();
Brian Osman207d4102019-01-10 09:40:58 -05002067 if (ImGui::Checkbox("Pause", &isPaused)) {
2068 fAnimTimer.togglePauseResume();
2069 }
Brian Osman707d2022019-01-10 11:27:34 -05002070
2071 float speed = fAnimTimer.getSpeed();
2072 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
2073 fAnimTimer.setSpeed(speed);
2074 }
Brian Osman207d4102019-01-10 09:40:58 -05002075 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002076
Brian Osmanfd7657c2019-04-25 11:34:07 -04002077 bool backendIsGL = Window::kNativeGL_BackendType == fBackendType
2078#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
2079 || Window::kANGLE_BackendType == fBackendType
2080#endif
2081 ;
2082
2083 // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
2084 // than GL, we need to force it on. Just do that on the first frame after the backend
2085 // switch, then resume normal operation.
Brian Osmana66081d2019-09-03 14:59:26 -04002086 if (!backendIsGL &&
2087 params.fGrContextOptions.fShaderCacheStrategy !=
2088 GrContextOptions::ShaderCacheStrategy::kSkSL) {
2089 params.fGrContextOptions.fShaderCacheStrategy =
2090 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002091 paramsChanged = true;
2092 fPersistentCache.reset();
2093 } else if (ImGui::CollapsingHeader("Shaders")) {
Brian Osman0b8bb882019-04-12 11:47:19 -04002094 // To re-load shaders from the currently active programs, we flush all caches on one
2095 // frame, then set a flag to poll the cache on the next frame.
2096 static bool gLoadPending = false;
2097 if (gLoadPending) {
2098 auto collectShaders = [this](sk_sp<const SkData> key, sk_sp<SkData> data,
2099 int hitCount) {
2100 CachedGLSL& entry(fCachedGLSL.push_back());
2101 entry.fKey = key;
2102 SkMD5 hash;
2103 hash.write(key->bytes(), key->size());
2104 SkMD5::Digest digest = hash.finish();
2105 for (int i = 0; i < 16; ++i) {
2106 entry.fKeyString.appendf("%02x", digest.data[i]);
2107 }
2108
Brian Osmana66081d2019-09-03 14:59:26 -04002109 SkReader32 reader(data->data(), data->size());
2110 entry.fShaderType = reader.readU32();
2111 GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
2112 entry.fInputs,
2113 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002114 };
2115 fCachedGLSL.reset();
2116 fPersistentCache.foreach(collectShaders);
2117 gLoadPending = false;
2118 }
2119
2120 // Defer actually doing the load/save logic so that we can trigger a save when we
2121 // start or finish hovering on a tree node in the list below:
2122 bool doLoad = ImGui::Button("Load"); ImGui::SameLine();
Brian Osmanfd7657c2019-04-25 11:34:07 -04002123 bool doSave = ImGui::Button("Save");
2124 if (backendIsGL) {
2125 ImGui::SameLine();
Brian Osmana66081d2019-09-03 14:59:26 -04002126 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2127 GrContextOptions::ShaderCacheStrategy::kSkSL;
2128 if (ImGui::Checkbox("SkSL", &sksl)) {
2129 params.fGrContextOptions.fShaderCacheStrategy = sksl
2130 ? GrContextOptions::ShaderCacheStrategy::kSkSL
2131 : GrContextOptions::ShaderCacheStrategy::kBackendSource;
Brian Osmanfd7657c2019-04-25 11:34:07 -04002132 paramsChanged = true;
2133 doLoad = true;
2134 fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
2135 }
Brian Osmancbc33b82019-04-19 14:16:19 -04002136 }
Brian Osman0b8bb882019-04-12 11:47:19 -04002137
2138 ImGui::BeginChild("##ScrollingRegion");
2139 for (auto& entry : fCachedGLSL) {
2140 bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str());
2141 bool hovered = ImGui::IsItemHovered();
2142 if (hovered != entry.fHovered) {
2143 // Force a save to patch the highlight shader in/out
2144 entry.fHovered = hovered;
2145 doSave = true;
2146 }
2147 if (inTreeNode) {
2148 // Full width, and a reasonable amount of space for each shader.
2149 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 20.0f);
2150 ImGui::InputTextMultiline("##VP", &entry.fShader[kVertex_GrShaderType],
2151 boxSize);
2152 ImGui::InputTextMultiline("##FP", &entry.fShader[kFragment_GrShaderType],
2153 boxSize);
2154 ImGui::TreePop();
2155 }
2156 }
2157 ImGui::EndChild();
2158
2159 if (doLoad) {
2160 fPersistentCache.reset();
2161 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2162 gLoadPending = true;
2163 }
2164 if (doSave) {
2165 // The hovered item (if any) gets a special shader to make it identifiable
Brian Osman5bee3902019-05-07 09:55:45 -04002166 auto shaderCaps = ctx->priv().caps()->shaderCaps();
Brian Osmana66081d2019-09-03 14:59:26 -04002167 bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
2168 GrContextOptions::ShaderCacheStrategy::kSkSL;
Brian Osman5bee3902019-05-07 09:55:45 -04002169
Brian Osman072e6fc2019-06-12 11:35:41 -04002170 SkSL::String highlight;
2171 if (!sksl) {
2172 highlight = shaderCaps->versionDeclString();
2173 if (shaderCaps->usesPrecisionModifiers()) {
2174 highlight.append("precision mediump float;\n");
2175 }
Brian Osman5bee3902019-05-07 09:55:45 -04002176 }
2177 const char* f4Type = sksl ? "half4" : "vec4";
Brian Osmancbc33b82019-04-19 14:16:19 -04002178 highlight.appendf("out %s sk_FragColor;\n"
2179 "void main() { sk_FragColor = %s(1, 0, 1, 0.5); }",
2180 f4Type, f4Type);
Brian Osman0b8bb882019-04-12 11:47:19 -04002181
2182 fPersistentCache.reset();
2183 fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting();
2184 for (auto& entry : fCachedGLSL) {
2185 SkSL::String backup = entry.fShader[kFragment_GrShaderType];
2186 if (entry.fHovered) {
2187 entry.fShader[kFragment_GrShaderType] = highlight;
2188 }
2189
Brian Osmana085a412019-04-25 09:44:43 -04002190 auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType,
2191 entry.fShader,
2192 entry.fInputs,
Brian Osman4524e842019-09-24 16:03:41 -04002193 kGrShaderTypeCount);
Brian Osman0b8bb882019-04-12 11:47:19 -04002194 fPersistentCache.store(*entry.fKey, *data);
2195
2196 entry.fShader[kFragment_GrShaderType] = backup;
2197 }
2198 }
2199 }
Brian Osman79086b92017-02-10 13:36:16 -05002200 }
Brian Salomon99a33902017-03-07 15:16:34 -05002201 if (paramsChanged) {
2202 fDeferredActions.push_back([=]() {
2203 fWindow->setRequestedDisplayParams(params);
2204 fWindow->inval();
2205 this->updateTitle();
2206 });
2207 }
Brian Osman79086b92017-02-10 13:36:16 -05002208 ImGui::End();
2209 }
2210
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002211 if (gShaderErrorHandler.fErrors.count()) {
2212 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
2213 ImGui::Begin("Shader Errors");
2214 for (int i = 0; i < gShaderErrorHandler.fErrors.count(); ++i) {
2215 ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str());
Chris Dalton77912982019-12-16 11:18:13 -07002216 SkSL::String sksl(gShaderErrorHandler.fShaders[i].c_str());
2217 GrShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) {
2218 ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText);
2219 });
Brian Osman5e7fbfd2019-05-03 13:13:35 -04002220 }
2221 ImGui::End();
2222 gShaderErrorHandler.reset();
2223 }
2224
Brian Osmanf6877092017-02-13 09:39:57 -05002225 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04002226 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
2227 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002228 static int zoomFactor = 8;
2229 if (ImGui::Button("<<")) {
2230 zoomFactor = SkTMax(zoomFactor / 2, 4);
2231 }
2232 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
2233 if (ImGui::Button(">>")) {
2234 zoomFactor = SkTMin(zoomFactor * 2, 32);
2235 }
Brian Osmanf6877092017-02-13 09:39:57 -05002236
Ben Wagner3627d2e2018-06-26 14:23:20 -04002237 if (!fZoomWindowFixed) {
2238 ImVec2 mousePos = ImGui::GetMousePos();
2239 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
2240 }
2241 SkScalar x = fZoomWindowLocation.x();
2242 SkScalar y = fZoomWindowLocation.y();
2243 int xInt = SkScalarRoundToInt(x);
2244 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05002245 ImVec2 avail = ImGui::GetContentRegionAvail();
2246
Brian Osmanead517d2017-11-13 15:36:36 -05002247 uint32_t pixel = 0;
2248 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002249 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05002250 ImGui::SameLine();
Brian Osman22eeb3c2019-02-20 10:13:06 -05002251 ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X",
Ben Wagner3627d2e2018-06-26 14:23:20 -04002252 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05002253 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05002254 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
2255 }
2256
Brian Osmand67e5182017-12-08 16:46:09 -05002257 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05002258 // Translate so the region of the image that's under the mouse cursor is centered
2259 // in the zoom canvas:
2260 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002261 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
2262 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05002263 c->drawImage(this->fLastImage, 0, 0);
2264
2265 SkPaint outline;
2266 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04002267 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05002268 });
Brian Osmanf6877092017-02-13 09:39:57 -05002269 }
2270
2271 ImGui::End();
2272 }
Brian Osman79086b92017-02-10 13:36:16 -05002273}
2274
liyuqian2edb0f42016-07-06 14:11:32 -07002275void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002276 for (int i = 0; i < fDeferredActions.count(); ++i) {
2277 fDeferredActions[i]();
2278 }
2279 fDeferredActions.reset();
2280
Brian Osman56a24812017-12-19 11:15:16 -05002281 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002282 fAnimTimer.updateTime();
Hal Canary41248072019-07-11 16:32:53 -04002283 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos());
Brian Osman56a24812017-12-19 11:15:16 -05002284 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002285
Brian Osman79086b92017-02-10 13:36:16 -05002286 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002287 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2288 // not be visible, though. So we need to redraw if there is at least one visible window, or
2289 // more than one active window. Newly created windows are active but not visible for one frame
2290 // while they determine their layout and sizing.
2291 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2292 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002293 fWindow->inval();
2294 }
jvanverth9f372462016-04-06 06:08:59 -07002295}
liyuqiane5a6cd92016-05-27 08:52:52 -07002296
Florin Malitab632df72018-06-18 21:23:06 -04002297template <typename OptionsFunc>
2298static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2299 OptionsFunc&& optionsFunc) {
2300 writer.beginObject();
2301 {
2302 writer.appendString(kName , name);
2303 writer.appendString(kValue, value);
2304
2305 writer.beginArray(kOptions);
2306 {
2307 optionsFunc(writer);
2308 }
2309 writer.endArray();
2310 }
2311 writer.endObject();
2312}
2313
2314
liyuqiane5a6cd92016-05-27 08:52:52 -07002315void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002316 if (!fWindow) {
2317 return;
2318 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002319 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002320 return; // Surface hasn't been created yet.
2321 }
2322
Florin Malitab632df72018-06-18 21:23:06 -04002323 SkDynamicMemoryWStream memStream;
2324 SkJSONWriter writer(&memStream);
2325 writer.beginArray();
2326
liyuqianb73c24b2016-06-03 08:47:23 -07002327 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002328 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2329 [this](SkJSONWriter& writer) {
2330 for(const auto& slide : fSlides) {
2331 writer.appendString(slide->getName().c_str());
2332 }
2333 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002334
liyuqianb73c24b2016-06-03 08:47:23 -07002335 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002336 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2337 [](SkJSONWriter& writer) {
2338 for (const auto& str : kBackendTypeStrings) {
2339 writer.appendString(str);
2340 }
2341 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002342
csmartdalton578f0642017-02-24 16:04:47 -07002343 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002344 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2345 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2346 [this](SkJSONWriter& writer) {
2347 writer.appendS32(0);
2348
2349 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2350 return;
2351 }
2352
2353 for (int msaa : {4, 8, 16}) {
2354 writer.appendS32(msaa);
2355 }
2356 });
csmartdalton578f0642017-02-24 16:04:47 -07002357
csmartdalton61cd31a2017-02-27 17:00:53 -07002358 // Path renderer state
2359 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002360 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2361 [this](SkJSONWriter& writer) {
2362 const GrContext* ctx = fWindow->getGrContext();
2363 if (!ctx) {
2364 writer.appendString("Software");
2365 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002366 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002367
Florin Malitab632df72018-06-18 21:23:06 -04002368 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2369 if (fWindow->sampleCount() > 1) {
2370 if (caps->shaderCaps()->pathRenderingSupport()) {
2371 writer.appendString(
2372 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2373 }
2374 } else {
2375 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2376 writer.appendString(
2377 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2378 }
2379 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2380 }
2381 writer.appendString(
2382 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2383 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2384 }
2385 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002386
liyuqianb73c24b2016-06-03 08:47:23 -07002387 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002388 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2389 [this](SkJSONWriter& writer) {
2390 writer.appendString(kSoftkeyHint);
2391 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2392 writer.appendString(softkey.c_str());
2393 }
2394 });
liyuqianb73c24b2016-06-03 08:47:23 -07002395
Florin Malitab632df72018-06-18 21:23:06 -04002396 writer.endArray();
2397 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002398
Florin Malitab632df72018-06-18 21:23:06 -04002399 auto data = memStream.detachAsData();
2400
2401 // TODO: would be cool to avoid this copy
2402 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2403
2404 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002405}
2406
2407void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002408 // For those who will add more features to handle the state change in this function:
2409 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2410 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2411 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002412 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002413 for (int i = 0; i < fSlides.count(); ++i) {
2414 if (fSlides[i]->getName().equals(stateValue)) {
2415 this->setCurrentSlide(i);
2416 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002417 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002418 }
Florin Malitaab99c342018-01-16 16:23:03 -05002419
2420 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002421 } else if (stateName.equals(kBackendStateName)) {
2422 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2423 if (stateValue.equals(kBackendTypeStrings[i])) {
2424 if (fBackendType != i) {
2425 fBackendType = (sk_app::Window::BackendType)i;
2426 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002427 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002428 }
2429 break;
2430 }
2431 }
csmartdalton578f0642017-02-24 16:04:47 -07002432 } else if (stateName.equals(kMSAAStateName)) {
2433 DisplayParams params = fWindow->getRequestedDisplayParams();
2434 int sampleCount = atoi(stateValue.c_str());
2435 if (sampleCount != params.fMSAASampleCount) {
2436 params.fMSAASampleCount = sampleCount;
2437 fWindow->setRequestedDisplayParams(params);
2438 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002439 this->updateTitle();
2440 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002441 }
2442 } else if (stateName.equals(kPathRendererStateName)) {
2443 DisplayParams params = fWindow->getRequestedDisplayParams();
2444 for (const auto& pair : gPathRendererNames) {
2445 if (pair.second == stateValue.c_str()) {
2446 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2447 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2448 fWindow->setRequestedDisplayParams(params);
2449 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002450 this->updateTitle();
2451 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002452 }
2453 break;
2454 }
csmartdalton578f0642017-02-24 16:04:47 -07002455 }
liyuqianb73c24b2016-06-03 08:47:23 -07002456 } else if (stateName.equals(kSoftkeyStateName)) {
2457 if (!stateValue.equals(kSoftkeyHint)) {
2458 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002459 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002460 }
liyuqian2edb0f42016-07-06 14:11:32 -07002461 } else if (stateName.equals(kRefreshStateName)) {
2462 // This state is actually NOT in the UI state.
2463 // We use this to allow Android to quickly set bool fRefresh.
2464 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002465 } else {
2466 SkDebugf("Unknown stateName: %s", stateName.c_str());
2467 }
2468}
Brian Osman79086b92017-02-10 13:36:16 -05002469
Hal Canaryb1f411a2019-08-29 10:39:22 -04002470bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002471 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002472}
2473
Hal Canaryb1f411a2019-08-29 10:39:22 -04002474bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002475 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002476 fWindow->inval();
2477 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002478 } else {
2479 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002480 }
Brian Osman79086b92017-02-10 13:36:16 -05002481}