blob: b5a9084645220be55d6cb2d460aa9c0f4b34d0f5 [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
Chris Dalton2d18f412018-02-20 13:23:32 -07008#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -07009#include "GMSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040010#include "GrContext.h"
11#include "GrContextPriv.h"
liyuqian6f163d22016-06-13 12:26:45 -070012#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050013#include "Resources.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070014#include "SKPSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040015#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070016#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050017#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040018#include "SkColorSpaceXformCanvas.h"
Brian Osman2dd96932016-10-18 15:33:53 -040019#include "SkCommandLineFlags.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040020#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070021#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040022#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050023#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040024#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050025#include "SkImagePriv.h"
Florin Malitab632df72018-06-18 21:23:06 -040026#include "SkJSONWriter.h"
Herb Derbyefe39bc2018-05-01 17:06:20 -040027#include "SkMakeUnique.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070028#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050029#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050030#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050031#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040032#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070033#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070034#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050035#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050036#include "SkTestFontMgr.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040037#include "SkTo.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040038#include "SvgSlide.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040039#include "Viewer.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060040#include "ccpr/GrCoverageCountingPathRenderer.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040041#include "imgui.h"
csmartdalton578f0642017-02-24 16:04:47 -070042
Hal Canaryc640d0d2018-06-13 09:59:02 -040043#include <stdlib.h>
44#include <map>
45
Florin Malita3b526b02018-05-25 12:43:51 -040046#if defined(SK_HAS_SKSG)
47 #include "SlideDir.h"
48#endif
49
Florin Malita87ccf332018-05-04 12:23:24 -040050#if defined(SK_ENABLE_SKOTTIE)
51 #include "SkottieSlide.h"
52#endif
53
Ruiqi Maof5101492018-06-29 14:32:21 -040054#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
55 #include "NIMASlide.h"
56#endif
57
jvanverth34524262016-05-04 13:49:13 -070058using namespace sk_app;
59
csmartdalton61cd31a2017-02-27 17:00:53 -070060static std::map<GpuPathRenderers, std::string> gPathRendererNames;
61
jvanverth9f372462016-04-06 06:08:59 -070062Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070063 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070064}
65
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060066static DEFINE_string(slide, "", "Start on this sample.");
67static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050068
bsalomon6c471f72016-07-26 12:56:32 -070069#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070070# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070071#else
72# define BACKENDS_STR "\"sw\" and \"gl\""
73#endif
74
Brian Osman2dd96932016-10-18 15:33:53 -040075static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070076
Brian Salomonbdecacf2018-02-02 20:32:49 -050077static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070078
Chris Dalton2d18f412018-02-20 13:23:32 -070079DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
80
Brian Osman53136aa2017-07-20 15:43:35 -040081DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050082
Florin Malita38792ce2018-05-08 10:36:18 -040083DEFINE_string2(file, f, "", "Open a single file for viewing.");
84
Brian Salomon194db172017-08-17 14:37:06 -040085const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070086 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040087#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
88 "ANGLE",
89#endif
jvanverth063ece72016-06-17 09:29:14 -070090#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070091 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070092#endif
csmartdalton578f0642017-02-24 16:04:47 -070093 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070094};
95
bsalomon6c471f72016-07-26 12:56:32 -070096static sk_app::Window::BackendType get_backend_type(const char* str) {
97#ifdef SK_VULKAN
98 if (0 == strcmp(str, "vk")) {
99 return sk_app::Window::kVulkan_BackendType;
100 } else
101#endif
Brian Salomon194db172017-08-17 14:37:06 -0400102#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
103 if (0 == strcmp(str, "angle")) {
104 return sk_app::Window::kANGLE_BackendType;
105 } else
106#endif
bsalomon6c471f72016-07-26 12:56:32 -0700107 if (0 == strcmp(str, "gl")) {
108 return sk_app::Window::kNativeGL_BackendType;
109 } else if (0 == strcmp(str, "sw")) {
110 return sk_app::Window::kRaster_BackendType;
111 } else {
112 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
113 return sk_app::Window::kRaster_BackendType;
114 }
115}
116
Brian Osmana109e392017-02-24 09:49:14 -0500117static SkColorSpacePrimaries gSrgbPrimaries = {
118 0.64f, 0.33f,
119 0.30f, 0.60f,
120 0.15f, 0.06f,
121 0.3127f, 0.3290f };
122
123static SkColorSpacePrimaries gAdobePrimaries = {
124 0.64f, 0.33f,
125 0.21f, 0.71f,
126 0.15f, 0.06f,
127 0.3127f, 0.3290f };
128
129static SkColorSpacePrimaries gP3Primaries = {
130 0.680f, 0.320f,
131 0.265f, 0.690f,
132 0.150f, 0.060f,
133 0.3127f, 0.3290f };
134
135static SkColorSpacePrimaries gRec2020Primaries = {
136 0.708f, 0.292f,
137 0.170f, 0.797f,
138 0.131f, 0.046f,
139 0.3127f, 0.3290f };
140
141struct NamedPrimaries {
142 const char* fName;
143 SkColorSpacePrimaries* fPrimaries;
144} gNamedPrimaries[] = {
145 { "sRGB", &gSrgbPrimaries },
146 { "AdobeRGB", &gAdobePrimaries },
147 { "P3", &gP3Primaries },
148 { "Rec. 2020", &gRec2020Primaries },
149};
150
151static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
152 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
153}
154
Brian Osman70d2f432017-11-08 09:54:10 -0500155static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
156 // In raster mode, we still use GL for the window.
157 // This lets us render the GUI faster (and correct).
158 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
159}
160
liyuqiane5a6cd92016-05-27 08:52:52 -0700161const char* kName = "name";
162const char* kValue = "value";
163const char* kOptions = "options";
164const char* kSlideStateName = "Slide";
165const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700166const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700167const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700168const char* kSoftkeyStateName = "Softkey";
169const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700170const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700171const char* kON = "ON";
172const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700173const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700174
jvanverth34524262016-05-04 13:49:13 -0700175Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500176 : fCurrentSlide(-1)
177 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500178 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500179 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500180 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500181 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500182 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400183 , fZoomWindowFixed(false)
184 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500185 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400186 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700187 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500188 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500189 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500190 // Our UI can only tweak gamma (currently), so start out gamma-only
191 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700192 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400193 , fRotation(0.0f)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400194 , fOffset{0.0f, 0.0f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400195 , fGestureDevice(GestureDevice::kNone)
Brian Osman805a7272018-05-02 15:40:20 -0400196 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700197{
Greg Daniel285db442016-10-14 09:12:53 -0400198 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700199
Brian Osmanf09e35e2017-12-15 14:48:09 -0500200 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500201 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500202 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
203 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
204 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
205 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700206
jvanverth2bb3b6d2016-04-08 07:24:09 -0700207 SkDebugf("Command line arguments: ");
208 for (int i = 1; i < argc; ++i) {
209 SkDebugf("%s ", argv[i]);
210 }
211 SkDebugf("\n");
212
213 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500214#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400215 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500216#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700217
Ben Wagner483c7722018-02-20 17:06:07 -0500218 if (!FLAGS_nativeFonts) {
219 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
220 }
221
Brian Osmanbc8150f2017-07-24 11:38:01 -0400222 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400223 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400224
bsalomon6c471f72016-07-26 12:56:32 -0700225 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700226 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700227
csmartdalton578f0642017-02-24 16:04:47 -0700228 DisplayParams displayParams;
229 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700230 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700231 fWindow->setRequestedDisplayParams(displayParams);
232
Brian Osman56a24812017-12-19 11:15:16 -0500233 // Configure timers
234 fStatsLayer.setActive(false);
235 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
236 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
237 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
238
jvanverth9f372462016-04-06 06:08:59 -0700239 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700240 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500241 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500242 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500243 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700244
brianosman622c8d52016-05-10 06:50:49 -0700245 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500246 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
247 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
248 fWindow->inval();
249 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500250 // Command to jump directly to the slide picker and give it focus
251 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
252 this->fShowImGuiDebugWindow = true;
253 this->fShowSlidePicker = true;
254 fWindow->inval();
255 });
256 // Alias that to Backspace, to match SampleApp
257 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
258 this->fShowImGuiDebugWindow = true;
259 this->fShowSlidePicker = true;
260 fWindow->inval();
261 });
Brian Osman79086b92017-02-10 13:36:16 -0500262 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
263 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
264 fWindow->inval();
265 });
Brian Osmanf6877092017-02-13 09:39:57 -0500266 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
267 this->fShowZoomWindow = !this->fShowZoomWindow;
268 fWindow->inval();
269 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400270 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
271 this->fZoomWindowFixed = !this->fZoomWindowFixed;
272 fWindow->inval();
273 });
brianosman622c8d52016-05-10 06:50:49 -0700274 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500275 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700276 fWindow->inval();
277 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400278 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500279 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400280 this->updateTitle();
281 fWindow->inval();
282 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500283 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500284 switch (fColorMode) {
285 case ColorMode::kLegacy:
286 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
287 break;
288 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
289 this->setColorMode(ColorMode::kColorManagedSRGB8888);
290 break;
291 case ColorMode::kColorManagedSRGB8888:
292 this->setColorMode(ColorMode::kColorManagedLinearF16);
293 break;
294 case ColorMode::kColorManagedLinearF16:
295 this->setColorMode(ColorMode::kLegacy);
296 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500297 }
brianosman622c8d52016-05-10 06:50:49 -0700298 });
299 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500300 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700301 });
302 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500303 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700304 });
305 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
306 this->changeZoomLevel(1.f / 32.f);
307 fWindow->inval();
308 });
309 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
310 this->changeZoomLevel(-1.f / 32.f);
311 fWindow->inval();
312 });
jvanverthaf236b52016-05-20 06:01:06 -0700313 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400314 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
315 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500316 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400317#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
318 if (newBackend == sk_app::Window::kVulkan_BackendType) {
319 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
320 sk_app::Window::kBackendTypeCount);
321 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
322 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500323 }
324#endif
Brian Osman621491e2017-02-28 15:45:01 -0500325 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700326 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500327 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
328 fSaveToSKP = true;
329 fWindow->inval();
330 });
Ben Wagner37c54032018-04-13 14:30:23 -0400331 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
332 DisplayParams params = fWindow->getRequestedDisplayParams();
333 uint32_t flags = params.fSurfaceProps.flags();
334 if (!fPixelGeometryOverrides) {
335 fPixelGeometryOverrides = true;
336 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
337 } else {
338 switch (params.fSurfaceProps.pixelGeometry()) {
339 case kUnknown_SkPixelGeometry:
340 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
341 break;
342 case kRGB_H_SkPixelGeometry:
343 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
344 break;
345 case kBGR_H_SkPixelGeometry:
346 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
347 break;
348 case kRGB_V_SkPixelGeometry:
349 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
350 break;
351 case kBGR_V_SkPixelGeometry:
352 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
353 fPixelGeometryOverrides = false;
354 break;
355 }
356 }
357 fWindow->setRequestedDisplayParams(params);
358 this->updateTitle();
359 fWindow->inval();
360 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500361 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
362 if (!fPaintOverrides.fHinting) {
363 fPaintOverrides.fHinting = true;
364 fPaint.setHinting(SkPaint::kNo_Hinting);
365 } else {
366 switch (fPaint.getHinting()) {
367 case SkPaint::kNo_Hinting:
368 fPaint.setHinting(SkPaint::kSlight_Hinting);
369 break;
370 case SkPaint::kSlight_Hinting:
371 fPaint.setHinting(SkPaint::kNormal_Hinting);
372 break;
373 case SkPaint::kNormal_Hinting:
374 fPaint.setHinting(SkPaint::kFull_Hinting);
375 break;
376 case SkPaint::kFull_Hinting:
377 fPaint.setHinting(SkPaint::kNo_Hinting);
378 fPaintOverrides.fHinting = false;
379 break;
380 }
381 }
382 this->updateTitle();
383 fWindow->inval();
384 });
385 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
386 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
387 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
388 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
389 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500390 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
391 gSkUseDeltaAA = gSkForceDeltaAA = false;
392 } else {
393 fPaint.setAntiAlias(true);
394 switch (fPaintOverrides.fAntiAlias) {
395 case SkPaintFields::AntiAliasState::Alias:
396 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400397 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
398 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500399 break;
400 case SkPaintFields::AntiAliasState::Normal:
401 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500402 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400403 gSkForceAnalyticAA = false;
404 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500405 break;
406 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
407 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400408 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
409 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500410 break;
411 case SkPaintFields::AntiAliasState::AnalyticAAForced:
412 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
413 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
414 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400415 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500416 break;
417 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
418 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400419 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
420 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500421 break;
422 case SkPaintFields::AntiAliasState::DeltaAAForced:
423 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
424 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
425 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
426 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
427 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
428 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
429 break;
430 }
431 }
432 this->updateTitle();
433 fWindow->inval();
434 });
Ben Wagner37c54032018-04-13 14:30:23 -0400435 fCommands.addCommand('D', "Modes", "DFT", [this]() {
436 DisplayParams params = fWindow->getRequestedDisplayParams();
437 uint32_t flags = params.fSurfaceProps.flags();
438 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
439 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
440 fWindow->setRequestedDisplayParams(params);
441 this->updateTitle();
442 fWindow->inval();
443 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500444 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
445 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
446 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
447 fPaint.setLCDRenderText(false);
448 } else {
449 if (!fPaint.isLCDRenderText()) {
450 fPaint.setLCDRenderText(true);
451 } else {
452 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
453 }
454 }
455 this->updateTitle();
456 fWindow->inval();
457 });
458 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
459 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
460 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
461 fPaint.setSubpixelText(false);
462 } else {
463 if (!fPaint.isSubpixelText()) {
464 fPaint.setSubpixelText(true);
465 } else {
466 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
467 }
468 }
469 this->updateTitle();
470 fWindow->inval();
471 });
Brian Osman805a7272018-05-02 15:40:20 -0400472 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
473 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
474 : kPerspective_Real;
475 this->updateTitle();
476 fWindow->inval();
477 });
478 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
479 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
480 : kPerspective_Off;
481 this->updateTitle();
482 fWindow->inval();
483 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400484 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
485 fZoomUI = !fZoomUI;
486 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
487 fWindow->inval();
488 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500489
jvanverth2bb3b6d2016-04-08 07:24:09 -0700490 // set up slides
491 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500492 if (FLAGS_list) {
493 this->listNames();
494 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700495
Brian Osman9bb47cf2018-04-26 15:55:00 -0400496 fPerspectivePoints[0].set(0, 0);
497 fPerspectivePoints[1].set(1, 0);
498 fPerspectivePoints[2].set(0, 1);
499 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700500 fAnimTimer.run();
501
Hal Canaryc465d132017-12-08 10:21:31 -0500502 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500503 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400504 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500505 }
506 fImGuiGamutPaint.setColor(SK_ColorWHITE);
507 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
508
Brian Osman70d2f432017-11-08 09:54:10 -0500509 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500510 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700511}
512
jvanverth34524262016-05-04 13:49:13 -0700513void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400514 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
515 static const struct {
516 const char* fExtension;
517 const char* fDirName;
518 const SkCommandLineFlags::StringArray& fFlags;
519 const SlideFactory fFactory;
520 } gExternalSlidesInfo[] = {
521 { ".skp", "skp-dir", FLAGS_skps,
522 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
523 return sk_make_sp<SKPSlide>(name, path);}
524 },
525 { ".jpg", "jpg-dir", FLAGS_jpgs,
526 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
527 return sk_make_sp<ImageSlide>(name, path);}
528 },
Florin Malita87ccf332018-05-04 12:23:24 -0400529#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400530 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400531 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
532 return sk_make_sp<SkottieSlide>(name, path);}
533 },
Florin Malita87ccf332018-05-04 12:23:24 -0400534#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400535 { ".svg", "svg-dir", FLAGS_svgs,
536 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
537 return sk_make_sp<SvgSlide>(name, path);}
538 },
Ruiqi Maof5101492018-06-29 14:32:21 -0400539#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
540 { ".nima", "nima-dir", FLAGS_nimas,
541 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
542 return sk_make_sp<NIMASlide>(name, path);}
543 },
544#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400545 };
jvanverthc265a922016-04-08 12:51:45 -0700546
Florin Malita0ffa3222018-04-05 14:34:45 -0400547 SkTArray<sk_sp<Slide>, true> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700548
Florin Malita0ffa3222018-04-05 14:34:45 -0400549 const auto addSlide = [&](const SkString& name,
550 const SkString& path,
551 const SlideFactory& fact) {
552 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
553 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700554 }
liyuqian6f163d22016-06-13 12:26:45 -0700555
Florin Malita0ffa3222018-04-05 14:34:45 -0400556 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500557 dirSlides.push_back(slide);
558 fSlides.push_back(std::move(slide));
559 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400560 };
Florin Malita76a076b2018-02-15 18:40:48 -0500561
Florin Malita38792ce2018-05-08 10:36:18 -0400562 if (!FLAGS_file.isEmpty()) {
563 // single file mode
564 const SkString file(FLAGS_file[0]);
565
566 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
567 for (const auto& sinfo : gExternalSlidesInfo) {
568 if (file.endsWith(sinfo.fExtension)) {
569 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
570 return;
571 }
572 }
573
574 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
575 } else {
576 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
577 }
578
579 return;
580 }
581
582 // Bisect slide.
583 if (!FLAGS_bisect.isEmpty()) {
584 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
585 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
586 if (FLAGS_bisect.count() >= 2) {
587 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
588 bisect->onChar(*ch);
589 }
590 }
591 fSlides.push_back(std::move(bisect));
592 }
593 }
594
595 // GMs
596 int firstGM = fSlides.count();
597 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
598 while (gms) {
599 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
600
601 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
602 sk_sp<Slide> slide(new GMSlide(gm.release()));
603 fSlides.push_back(std::move(slide));
604 }
605
606 gms = gms->next();
607 }
608 // reverse gms
609 int numGMs = fSlides.count() - firstGM;
610 for (int i = 0; i < numGMs/2; ++i) {
611 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
612 }
613
614 // samples
615 const SkViewRegister* reg = SkViewRegister::Head();
616 while (reg) {
617 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
618 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
619 fSlides.push_back(slide);
620 }
621 reg = reg->next();
622 }
623
Florin Malita0ffa3222018-04-05 14:34:45 -0400624 for (const auto& info : gExternalSlidesInfo) {
625 for (const auto& flag : info.fFlags) {
626 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
627 // single file
628 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
629 } else {
630 // directory
631 SkOSFile::Iter it(flag.c_str(), info.fExtension);
632 SkString name;
633 while (it.next(&name)) {
634 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
635 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400636 }
Florin Malita3b526b02018-05-25 12:43:51 -0400637#if defined(SK_HAS_SKSG)
Florin Malita0ffa3222018-04-05 14:34:45 -0400638 if (!dirSlides.empty()) {
639 fSlides.push_back(
640 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
641 std::move(dirSlides)));
642 dirSlides.reset();
643 }
Florin Malita3b526b02018-05-25 12:43:51 -0400644#endif
Florin Malitac659c2c2018-04-05 11:57:21 -0400645 }
646 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700647}
648
649
jvanverth34524262016-05-04 13:49:13 -0700650Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700651 fWindow->detach();
652 delete fWindow;
653}
654
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500655struct SkPaintTitleUpdater {
656 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
657 void append(const char* s) {
658 if (fCount == 0) {
659 fTitle->append(" {");
660 } else {
661 fTitle->append(", ");
662 }
663 fTitle->append(s);
664 ++fCount;
665 }
666 void done() {
667 if (fCount > 0) {
668 fTitle->append("}");
669 }
670 }
671 SkString* fTitle;
672 int fCount;
673};
674
brianosman05de2162016-05-06 13:28:57 -0700675void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700676 if (!fWindow) {
677 return;
678 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500679 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700680 return; // Surface hasn't been created yet.
681 }
682
jvanverth34524262016-05-04 13:49:13 -0700683 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700684 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700685
Yuqian Li399b3c22017-08-03 11:08:15 -0400686 if (gSkUseDeltaAA) {
687 if (gSkForceDeltaAA) {
688 title.append(" <FDAA>");
689 } else {
690 title.append(" <DAA>");
691 }
692 } else if (gSkUseAnalyticAA) {
693 if (gSkForceAnalyticAA) {
694 title.append(" <FAAA>");
695 } else {
696 title.append(" <AAA>");
697 }
698 }
699
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500700 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner99a78dc2018-05-09 18:23:51 -0400701 auto paintFlag = [this, &paintTitle](SkPaint::Flags flag, bool (SkPaint::* isFlag)() const,
702 const char* on, const char* off)
703 {
704 if (fPaintOverrides.fFlags & flag) {
705 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500706 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400707 };
708
709 paintFlag(SkPaint::kAntiAlias_Flag, &SkPaint::isAntiAlias, "Antialias", "Alias");
710 paintFlag(SkPaint::kDither_Flag, &SkPaint::isDither, "DITHER", "No Dither");
711 paintFlag(SkPaint::kFakeBoldText_Flag, &SkPaint::isFakeBoldText, "Fake Bold", "No Fake Bold");
712 paintFlag(SkPaint::kLinearText_Flag, &SkPaint::isLinearText, "Linear Text", "Non-Linear Text");
713 paintFlag(SkPaint::kSubpixelText_Flag, &SkPaint::isSubpixelText, "Subpixel Text", "Pixel Text");
714 paintFlag(SkPaint::kLCDRenderText_Flag, &SkPaint::isLCDRenderText, "LCD", "lcd");
715 paintFlag(SkPaint::kEmbeddedBitmapText_Flag, &SkPaint::isEmbeddedBitmapText,
716 "Bitmap Text", "No Bitmap Text");
717 paintFlag(SkPaint::kAutoHinting_Flag, &SkPaint::isAutohinted,
718 "Force Autohint", "No Force Autohint");
719 paintFlag(SkPaint::kVerticalText_Flag, &SkPaint::isVerticalText,
720 "Vertical Text", "No Vertical Text");
721
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500722 if (fPaintOverrides.fHinting) {
723 switch (fPaint.getHinting()) {
724 case SkPaint::kNo_Hinting:
725 paintTitle.append("No Hinting");
726 break;
727 case SkPaint::kSlight_Hinting:
728 paintTitle.append("Slight Hinting");
729 break;
730 case SkPaint::kNormal_Hinting:
731 paintTitle.append("Normal Hinting");
732 break;
733 case SkPaint::kFull_Hinting:
734 paintTitle.append("Full Hinting");
735 break;
736 }
737 }
738 paintTitle.done();
739
Brian Osman92004802017-03-06 11:47:26 -0500740 switch (fColorMode) {
741 case ColorMode::kLegacy:
742 title.append(" Legacy 8888");
743 break;
744 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
745 title.append(" ColorManaged 8888 (Nonlinear blending)");
746 break;
747 case ColorMode::kColorManagedSRGB8888:
748 title.append(" ColorManaged 8888");
749 break;
750 case ColorMode::kColorManagedLinearF16:
751 title.append(" ColorManaged F16");
752 break;
753 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500754
Brian Osman92004802017-03-06 11:47:26 -0500755 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500756 int curPrimaries = -1;
757 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
758 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
759 curPrimaries = i;
760 break;
761 }
762 }
763 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500764
765 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
766 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
767 }
brianosman05de2162016-05-06 13:28:57 -0700768 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500769
Ben Wagner37c54032018-04-13 14:30:23 -0400770 const DisplayParams& params = fWindow->getRequestedDisplayParams();
771 if (fPixelGeometryOverrides) {
772 switch (params.fSurfaceProps.pixelGeometry()) {
773 case kUnknown_SkPixelGeometry:
774 title.append( " Flat");
775 break;
776 case kRGB_H_SkPixelGeometry:
777 title.append( " RGB");
778 break;
779 case kBGR_H_SkPixelGeometry:
780 title.append( " BGR");
781 break;
782 case kRGB_V_SkPixelGeometry:
783 title.append( " RGBV");
784 break;
785 case kBGR_V_SkPixelGeometry:
786 title.append( " BGRV");
787 break;
788 }
789 }
790
791 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
792 title.append(" DFT");
793 }
794
csmartdalton578f0642017-02-24 16:04:47 -0700795 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700796 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500797 int msaa = fWindow->sampleCount();
798 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700799 title.appendf(" MSAA: %i", msaa);
800 }
801 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700802
803 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman1994f202018-07-10 11:41:22 -0400804 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700805 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
806 }
807
Brian Osman805a7272018-05-02 15:40:20 -0400808 if (kPerspective_Real == fPerspectiveMode) {
809 title.append(" Perpsective (Real)");
810 } else if (kPerspective_Fake == fPerspectiveMode) {
811 title.append(" Perspective (Fake)");
812 }
813
brianosman05de2162016-05-06 13:28:57 -0700814 fWindow->setTitle(title.c_str());
815}
816
Florin Malitaab99c342018-01-16 16:23:03 -0500817int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500818
819 if (!FLAGS_slide.isEmpty()) {
820 int count = fSlides.count();
821 for (int i = 0; i < count; i++) {
822 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500823 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500824 }
825 }
826
827 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
828 this->listNames();
829 }
830
Florin Malitaab99c342018-01-16 16:23:03 -0500831 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500832}
833
Florin Malitaab99c342018-01-16 16:23:03 -0500834void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500835 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500836 for (const auto& slide : fSlides) {
837 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500838 }
839}
840
Florin Malitaab99c342018-01-16 16:23:03 -0500841void Viewer::setCurrentSlide(int slide) {
842 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700843
Florin Malitaab99c342018-01-16 16:23:03 -0500844 if (slide == fCurrentSlide) {
845 return;
846 }
847
848 if (fCurrentSlide >= 0) {
849 fSlides[fCurrentSlide]->unload();
850 }
851
852 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
853 SkIntToScalar(fWindow->height()));
854 fCurrentSlide = slide;
855 this->setupCurrentSlide();
856}
857
858void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500859 if (fCurrentSlide >= 0) {
860 // prepare dimensions for image slides
861 fGesture.resetTouchState();
862 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700863
Jim Van Verth0848fb02018-01-22 13:39:30 -0500864 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
865 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
866 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400867
Jim Van Verth0848fb02018-01-22 13:39:30 -0500868 // Start with a matrix that scales the slide to the available screen space
869 if (fWindow->scaleContentToFit()) {
870 if (windowRect.width() > 0 && windowRect.height() > 0) {
871 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
872 }
liyuqiane46e4f02016-05-20 07:32:19 -0700873 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500874
875 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400876 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500877
878 this->updateTitle();
879 this->updateUIState();
880
881 fStatsLayer.resetMeasurements();
882
883 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700884 }
jvanverthc265a922016-04-08 12:51:45 -0700885}
886
887#define MAX_ZOOM_LEVEL 8
888#define MIN_ZOOM_LEVEL -8
889
jvanverth34524262016-05-04 13:49:13 -0700890void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700891 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400892 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400893 this->preTouchMatrixChanged();
894}
Yuqian Li755778c2018-03-28 16:23:31 -0400895
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400896void Viewer::preTouchMatrixChanged() {
897 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400898 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
899 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
900 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
901 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
902}
903
Brian Osman805a7272018-05-02 15:40:20 -0400904SkMatrix Viewer::computePerspectiveMatrix() {
905 SkScalar w = fWindow->width(), h = fWindow->height();
906 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
907 SkPoint perspPts[4] = {
908 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
909 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
910 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
911 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
912 };
913 SkMatrix m;
914 m.setPolyToPoly(orthoPts, perspPts, 4);
915 return m;
916}
917
Yuqian Li755778c2018-03-28 16:23:31 -0400918SkMatrix Viewer::computePreTouchMatrix() {
919 SkMatrix m = fDefaultMatrix;
920 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
921 : SK_Scalar1 + fZoomLevel;
Ben Wagner3627d2e2018-06-26 14:23:20 -0400922 m.preTranslate(fOffset.x(), fOffset.y());
Yuqian Li755778c2018-03-28 16:23:31 -0400923 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400924
925 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
926 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400927
Brian Osman805a7272018-05-02 15:40:20 -0400928 if (kPerspective_Real == fPerspectiveMode) {
929 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400930 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400931 }
932
Yuqian Li755778c2018-03-28 16:23:31 -0400933 return m;
jvanverthc265a922016-04-08 12:51:45 -0700934}
935
liyuqiand3cdbca2016-05-17 12:44:20 -0700936SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400937 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700938 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400939 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700940 return m;
jvanverthc265a922016-04-08 12:51:45 -0700941}
942
Brian Osman621491e2017-02-28 15:45:01 -0500943void Viewer::setBackend(sk_app::Window::BackendType backendType) {
944 fBackendType = backendType;
945
946 fWindow->detach();
947
Brian Osman70d2f432017-11-08 09:54:10 -0500948#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400949 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
950 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500951 DisplayParams params = fWindow->getRequestedDisplayParams();
952 delete fWindow;
953 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500954
Brian Osman70d2f432017-11-08 09:54:10 -0500955 // re-register callbacks
956 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500957 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500958 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500959 fWindow->pushLayer(&fImGuiLayer);
960
Brian Osman70d2f432017-11-08 09:54:10 -0500961 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
962 // will still include our correct sample count. But the re-created fWindow will lose that
963 // information. On Windows, we need to re-create the window when changing sample count,
964 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
965 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
966 // as if we had never changed windows at all).
967 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500968#endif
969
Brian Osman70d2f432017-11-08 09:54:10 -0500970 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500971}
972
Brian Osman92004802017-03-06 11:47:26 -0500973void Viewer::setColorMode(ColorMode colorMode) {
974 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700975
Brian Osmanf750fbc2017-02-08 10:47:28 -0500976 // When we're in color managed mode, we tag our window surface as sRGB. If we've switched into
Brian Osmane0d4fba2017-03-15 10:24:55 -0400977 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700978 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500979 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400980 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
981 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500982 if (wasInLegacy != wantLegacy) {
983 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700984 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500985 }
986
987 this->updateTitle();
988 fWindow->inval();
989}
990
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500991class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
992public:
993 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
994 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
995 { }
996 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -0400997 if (*paint == nullptr) {
998 return true;
999 }
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001000 if (fPaintOverrides->fTextSize) {
1001 paint->writable()->setTextSize(fPaint->getTextSize());
1002 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001003 if (fPaintOverrides->fHinting) {
1004 paint->writable()->setHinting(fPaint->getHinting());
1005 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001006
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001007 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
1008 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1009 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001010 if (fPaintOverrides->fFlags & SkPaint::kDither_Flag) {
1011 paint->writable()->setDither(fPaint->isDither());
1012 }
1013 if (fPaintOverrides->fFlags & SkPaint::kFakeBoldText_Flag) {
1014 paint->writable()->setFakeBoldText(fPaint->isFakeBoldText());
1015 }
1016 if (fPaintOverrides->fFlags & SkPaint::kLinearText_Flag) {
1017 paint->writable()->setLinearText(fPaint->isLinearText());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001018 }
1019 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
1020 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
1021 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001022 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
1023 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
1024 }
1025 if (fPaintOverrides->fFlags & SkPaint::kEmbeddedBitmapText_Flag) {
1026 paint->writable()->setEmbeddedBitmapText(fPaint->isEmbeddedBitmapText());
1027 }
1028 if (fPaintOverrides->fFlags & SkPaint::kAutoHinting_Flag) {
1029 paint->writable()->setAutohinted(fPaint->isAutohinted());
1030 }
1031 if (fPaintOverrides->fFlags & SkPaint::kVerticalText_Flag) {
1032 paint->writable()->setVerticalText(fPaint->isVerticalText());
1033 }
1034
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001035 return true;
1036 }
1037 SkPaint* fPaint;
1038 Viewer::SkPaintFields* fPaintOverrides;
1039};
1040
Brian Osmanf750fbc2017-02-08 10:47:28 -05001041void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001042 SkAutoCanvasRestore autorestore(canvas, false);
1043
Brian Osmanf750fbc2017-02-08 10:47:28 -05001044 // By default, we render directly into the window's surface/canvas
1045 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001046 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001047
Brian Osmane0d4fba2017-03-15 10:24:55 -04001048 // If we're in any of the color managed modes, construct the color space we're going to use
1049 sk_sp<SkColorSpace> cs = nullptr;
1050 if (ColorMode::kLegacy != fColorMode) {
1051 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
1052 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -04001053 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001054 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -05001055 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1056 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
1057 } else {
1058 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
1059 }
Brian Osmane0d4fba2017-03-15 10:24:55 -04001060 }
1061
Brian Osman3ac99cf2017-12-01 11:23:53 -05001062 if (fSaveToSKP) {
1063 SkPictureRecorder recorder;
1064 SkCanvas* recorderCanvas = recorder.beginRecording(
1065 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1066 // In xform-canvas mode, record the transformed output
1067 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1068 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1069 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
1070 recorderCanvas = xformCanvas.get();
1071 }
1072 fSlides[fCurrentSlide]->draw(recorderCanvas);
1073 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1074 SkFILEWStream stream("sample_app.skp");
1075 picture->serialize(&stream);
1076 fSaveToSKP = false;
1077 }
1078
Brian Osmane0d4fba2017-03-15 10:24:55 -04001079 // If we're in F16, or we're zooming, or we're in color correct 8888 and the gamut isn't sRGB,
Brian Osman70d2f432017-11-08 09:54:10 -05001080 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
Brian Osman805a7272018-05-02 15:40:20 -04001081 // because the window surface is actually GL, or we're doing fake perspective.
Brian Osmanf750fbc2017-02-08 10:47:28 -05001082 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -05001083 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -05001084 if (Window::kRaster_BackendType == fBackendType ||
Brian Osman805a7272018-05-02 15:40:20 -04001085 kPerspective_Fake == fPerspectiveMode ||
Brian Osman70d2f432017-11-08 09:54:10 -05001086 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -05001087 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -04001088 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
1089 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
1090
Brian Osman92004802017-03-06 11:47:26 -05001091 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
1092 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001093 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
1094 // with a special canvas (below) that has the color space attached
1095 sk_sp<SkColorSpace> offscreenColorSpace =
1096 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -05001097 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -04001098 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Ben Wagner37c54032018-04-13 14:30:23 -04001099 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1100 canvas->getProps(&props);
1101 offscreenSurface = Window::kRaster_BackendType == fBackendType
1102 ? SkSurface::MakeRaster(info, &props)
1103 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -05001104 SkPixmap offscreenPixmap;
Mike Klein48b64902018-07-25 13:28:44 -04001105 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001106 }
1107
Brian Osmane0d4fba2017-03-15 10:24:55 -04001108 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1109 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1110 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
1111 slideCanvas = xformCanvas.get();
1112 }
1113
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001114 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001115 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001116 slideCanvas->concat(computeMatrix());
Brian Osmane19e23f2018-05-22 16:56:40 -04001117 if (kPerspective_Real == fPerspectiveMode) {
1118 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1119 }
Brian Osman1df161a2017-02-09 12:10:20 -05001120 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001121 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001122 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1123 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001124 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001125 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001126
1127 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001128 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001129 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001130 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001131
1132 // If we rendered offscreen, snap an image and push the results to the window's canvas
1133 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001134 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001135
1136 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -04001137 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001138 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001139 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001140 SkPaint paint;
1141 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001142 int prePerspectiveCount = canvas->save();
1143 if (kPerspective_Fake == fPerspectiveMode) {
1144 paint.setFilterQuality(kHigh_SkFilterQuality);
1145 canvas->clear(SK_ColorWHITE);
1146 canvas->concat(this->computePerspectiveMatrix());
1147 }
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001148 canvas->drawImage(retaggedImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001149 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001150 }
liyuqian6f163d22016-06-13 12:26:45 -07001151}
1152
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001153void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001154 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001155 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001156}
Jim Van Verth6f449692017-02-14 15:16:46 -05001157
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001158void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001159 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001160
brianosman622c8d52016-05-10 06:50:49 -07001161 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001162
Brian Osmand67e5182017-12-08 16:46:09 -05001163 this->drawImGui();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001164}
1165
Florin Malitacefc1b92018-02-19 21:43:47 -05001166SkPoint Viewer::mapEvent(float x, float y) {
1167 const auto m = this->computeMatrix();
1168 SkMatrix inv;
1169
1170 SkAssertResult(m.invert(&inv));
1171
1172 return inv.mapXY(x, y);
1173}
1174
jvanverth814e38d2016-06-06 08:48:47 -07001175bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001176 if (GestureDevice::kMouse == fGestureDevice) {
1177 return false;
1178 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001179
1180 const auto slidePt = this->mapEvent(x, y);
1181 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1182 fWindow->inval();
1183 return true;
1184 }
1185
liyuqiand3cdbca2016-05-17 12:44:20 -07001186 void* castedOwner = reinterpret_cast<void*>(owner);
1187 switch (state) {
1188 case Window::kUp_InputState: {
1189 fGesture.touchEnd(castedOwner);
1190 break;
1191 }
1192 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001193 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001194 break;
1195 }
1196 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001197 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001198 break;
1199 }
1200 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001201 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001202 fWindow->inval();
1203 return true;
1204}
1205
Brian Osman80fc07e2017-12-08 16:45:43 -05001206bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001207 if (GestureDevice::kTouch == fGestureDevice) {
1208 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001209 }
Brian Osman16c81a12017-12-20 11:58:34 -05001210
Florin Malitacefc1b92018-02-19 21:43:47 -05001211 const auto slidePt = this->mapEvent(x, y);
1212 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1213 fWindow->inval();
1214 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001215 }
1216
1217 switch (state) {
1218 case Window::kUp_InputState: {
1219 fGesture.touchEnd(nullptr);
1220 break;
1221 }
1222 case Window::kDown_InputState: {
1223 fGesture.touchBegin(nullptr, x, y);
1224 break;
1225 }
1226 case Window::kMove_InputState: {
1227 fGesture.touchMoved(nullptr, x, y);
1228 break;
1229 }
1230 }
1231 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1232
1233 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1234 fWindow->inval();
1235 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001236 return true;
1237}
1238
Brian Osmana109e392017-02-24 09:49:14 -05001239static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1240 const ImVec2& pos, const ImVec2& size) {
1241 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1242 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1243
1244 // Invisible 10x10 button
1245 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1246 ImGui::InvisibleButton(label, ImVec2(10, 10));
1247
1248 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1249 ImGuiIO& io = ImGui::GetIO();
1250 // Normalized mouse position, relative to our gamut box
1251 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1252 // Clamp to edge of box, convert back to primary scale
1253 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1254 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1255 }
1256
1257 if (ImGui::IsItemHovered()) {
1258 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1259 }
1260
1261 // Return screen coordinates for the caller. We could just return center here, but we'd have
1262 // one frame of lag during drag.
1263 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1264}
1265
1266static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1267 ImDrawList* drawList = ImGui::GetWindowDrawList();
1268
1269 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1270 // width, and scale the height to maintain aspect ratio.
1271 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1272 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1273 ImVec2 pos = ImGui::GetCursorScreenPos();
1274
1275 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1276 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1277 // Magic numbers are pixel locations of the origin and upper-right corner.
1278 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1279 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001280
1281 // Primary markers
1282 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1283 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1284 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1285 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1286
1287 // Gamut triangle
1288 drawList->AddCircle(r, 5.0f, 0xFF000040);
1289 drawList->AddCircle(g, 5.0f, 0xFF004000);
1290 drawList->AddCircle(b, 5.0f, 0xFF400000);
1291 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1292 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1293
1294 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001295 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1296}
1297
1298static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1299 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1300 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1301 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1302
1303 // Invisible 10x10 button
1304 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1305 ImGui::InvisibleButton(label, ImVec2(10, 10));
1306
1307 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1308 ImGuiIO& io = ImGui::GetIO();
1309 // Normalized mouse position, relative to our gamut box
1310 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1311 // Clamp to edge of box
1312 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1313 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1314 *dragging = true;
1315 }
1316
1317 // Return screen coordinates for the caller. We could just return center here, but we'd have
1318 // one frame of lag during drag.
1319 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1320}
1321
Ben Wagner3627d2e2018-06-26 14:23:20 -04001322static bool ImGui_DragLocation(SkPoint* pt) {
1323 ImDrawList* drawList = ImGui::GetWindowDrawList();
1324
1325 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1326 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1327 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1328 ImVec2 pos = ImGui::GetCursorScreenPos();
1329
1330 // Background rectangle
1331 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1332
1333 // Location marker
1334 bool dragging = false;
1335 ImVec2 tl = ImGui_DragPoint("SL", pt + 0, pos, size, &dragging);
1336 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1337
1338 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1339 ImGui::Spacing();
1340
1341 return dragging;
1342}
1343
Brian Osman9bb47cf2018-04-26 15:55:00 -04001344static bool ImGui_DragQuad(SkPoint* pts) {
1345 ImDrawList* drawList = ImGui::GetWindowDrawList();
1346
1347 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1348 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1349 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1350 ImVec2 pos = ImGui::GetCursorScreenPos();
1351
1352 // Background rectangle
1353 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1354
1355 // Corner markers
1356 bool dragging = false;
1357 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1358 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1359 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1360 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1361
1362 // Draw markers and quad
1363 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1364 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1365 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1366 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1367 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1368 drawList->AddLine(tr, br, 0xFFFFFFFF);
1369 drawList->AddLine(br, bl, 0xFFFFFFFF);
1370 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1371
1372 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1373 ImGui::Spacing();
1374
1375 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001376}
1377
Brian Osmand67e5182017-12-08 16:46:09 -05001378void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001379 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1380 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001381 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001382 }
1383
1384 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001385 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1386 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001387 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001388 DisplayParams params = fWindow->getRequestedDisplayParams();
1389 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001390 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1391 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001392 if (ImGui::CollapsingHeader("Backend")) {
1393 int newBackend = static_cast<int>(fBackendType);
1394 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1395 ImGui::SameLine();
1396 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001397#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1398 ImGui::SameLine();
1399 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1400#endif
Brian Osman621491e2017-02-28 15:45:01 -05001401#if defined(SK_VULKAN)
1402 ImGui::SameLine();
1403 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1404#endif
1405 if (newBackend != fBackendType) {
1406 fDeferredActions.push_back([=]() {
1407 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1408 });
1409 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001410
Brian Salomon99a33902017-03-07 15:16:34 -05001411 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001412 bool* wire = &params.fGrContextOptions.fWireframeMode;
1413 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1414 paramsChanged = true;
1415 }
Brian Salomon99a33902017-03-07 15:16:34 -05001416
Brian Osman28b12522017-03-08 17:10:24 -05001417 if (ctx) {
1418 int sampleCount = fWindow->sampleCount();
1419 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001420 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001421 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1422 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1423 ImGui::RadioButton("16", &sampleCount, 16);
1424
1425 if (sampleCount != params.fMSAASampleCount) {
1426 params.fMSAASampleCount = sampleCount;
1427 paramsChanged = true;
1428 }
1429 }
1430
Ben Wagner37c54032018-04-13 14:30:23 -04001431 int pixelGeometryIdx = 0;
1432 if (fPixelGeometryOverrides) {
1433 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1434 }
1435 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1436 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1437 {
1438 uint32_t flags = params.fSurfaceProps.flags();
1439 if (pixelGeometryIdx == 0) {
1440 fPixelGeometryOverrides = false;
1441 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1442 } else {
1443 fPixelGeometryOverrides = true;
1444 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1445 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1446 }
1447 paramsChanged = true;
1448 }
1449
1450 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1451 if (ImGui::Checkbox("DFT", &useDFT)) {
1452 uint32_t flags = params.fSurfaceProps.flags();
1453 if (useDFT) {
1454 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1455 } else {
1456 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1457 }
1458 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1459 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1460 paramsChanged = true;
1461 }
1462
Brian Osman8a9de3d2017-03-01 14:59:05 -05001463 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001464 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001465 auto prButton = [&](GpuPathRenderers x) {
1466 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001467 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1468 params.fGrContextOptions.fGpuPathRenderers = x;
1469 paramsChanged = true;
1470 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001471 }
1472 };
1473
1474 if (!ctx) {
1475 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001476 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001477 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001478 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001479 prButton(GpuPathRenderers::kStencilAndCover);
1480 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001481 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001482 prButton(GpuPathRenderers::kNone);
1483 } else {
1484 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001485 if (GrCoverageCountingPathRenderer::IsSupported(
1486 *ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001487 prButton(GpuPathRenderers::kCoverageCounting);
1488 }
Jim Van Verth83010462017-03-16 08:45:39 -04001489 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001490 prButton(GpuPathRenderers::kTessellating);
1491 prButton(GpuPathRenderers::kNone);
1492 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001493 ImGui::TreePop();
1494 }
Brian Osman621491e2017-02-28 15:45:01 -05001495 }
1496
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001497 if (ImGui::CollapsingHeader("Transform")) {
1498 float zoom = fZoomLevel;
1499 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1500 fZoomLevel = zoom;
1501 this->preTouchMatrixChanged();
1502 paramsChanged = true;
1503 }
1504 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001505 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001506 fRotation = deg;
1507 this->preTouchMatrixChanged();
1508 paramsChanged = true;
1509 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001510 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1511 if (ImGui_DragLocation(&fOffset)) {
1512 this->preTouchMatrixChanged();
1513 paramsChanged = true;
1514 }
1515 }
Brian Osman805a7272018-05-02 15:40:20 -04001516 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1517 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1518 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001519 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001520 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001521 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001522 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001523 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001524 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001525 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001526 }
1527
Ben Wagnera580fb32018-04-17 11:16:32 -04001528 if (ImGui::CollapsingHeader("Paint")) {
1529 int hintingIdx = 0;
1530 if (fPaintOverrides.fHinting) {
1531 hintingIdx = fPaint.getHinting() + 1;
1532 }
1533 if (ImGui::Combo("Hinting", &hintingIdx,
1534 "Default\0None\0Slight\0Normal\0Full\0\0"))
1535 {
1536 if (hintingIdx == 0) {
1537 fPaintOverrides.fHinting = false;
1538 fPaint.setHinting(SkPaint::kNo_Hinting);
1539 } else {
1540 fPaintOverrides.fHinting = true;
1541 SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1);
1542 fPaint.setHinting(hinting);
1543 }
1544 paramsChanged = true;
1545 }
1546
1547 int aliasIdx = 0;
1548 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1549 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1550 }
1551 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1552 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1553 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1554 {
1555 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1556 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1557 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1558 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1559 if (aliasIdx == 0) {
1560 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1561 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1562 } else {
1563 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1564 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1565 fPaint.setAntiAlias(aliasIdx > 1);
1566 switch (fPaintOverrides.fAntiAlias) {
1567 case SkPaintFields::AntiAliasState::Alias:
1568 break;
1569 case SkPaintFields::AntiAliasState::Normal:
1570 break;
1571 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1572 gSkUseAnalyticAA = true;
1573 gSkForceAnalyticAA = false;
1574 gSkUseDeltaAA = gSkForceDeltaAA = false;
1575 break;
1576 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1577 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1578 gSkUseDeltaAA = gSkForceDeltaAA = false;
1579 break;
1580 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1581 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1582 gSkUseDeltaAA = true;
1583 gSkForceDeltaAA = false;
1584 break;
1585 case SkPaintFields::AntiAliasState::DeltaAAForced:
1586 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1587 gSkUseDeltaAA = gSkForceDeltaAA = true;
1588 break;
1589 }
1590 }
1591 paramsChanged = true;
1592 }
1593
Ben Wagner99a78dc2018-05-09 18:23:51 -04001594 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
1595 SkPaint::Flags flag,
1596 bool (SkPaint::* isFlag)() const,
1597 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001598 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001599 int itemIndex = 0;
1600 if (fPaintOverrides.fFlags & flag) {
1601 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001602 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001603 if (ImGui::Combo(label, &itemIndex, items)) {
1604 if (itemIndex == 0) {
1605 fPaintOverrides.fFlags &= ~flag;
1606 } else {
1607 fPaintOverrides.fFlags |= flag;
1608 (fPaint.*setFlag)(itemIndex == 2);
1609 }
1610 paramsChanged = true;
1611 }
1612 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001613
Ben Wagner99a78dc2018-05-09 18:23:51 -04001614 paintFlag("Dither",
1615 "Default\0No Dither\0Dither\0\0",
1616 SkPaint::kDither_Flag,
1617 &SkPaint::isDither, &SkPaint::setDither);
1618
1619 paintFlag("Fake Bold Glyphs",
1620 "Default\0No Fake Bold\0Fake Bold\0\0",
1621 SkPaint::kFakeBoldText_Flag,
1622 &SkPaint::isFakeBoldText, &SkPaint::setFakeBoldText);
1623
1624 paintFlag("Linear Text",
1625 "Default\0No Linear Text\0Linear Text\0\0",
1626 SkPaint::kLinearText_Flag,
1627 &SkPaint::isLinearText, &SkPaint::setLinearText);
1628
1629 paintFlag("Subpixel Position Glyphs",
1630 "Default\0Pixel Text\0Subpixel Text\0\0",
1631 SkPaint::kSubpixelText_Flag,
1632 &SkPaint::isSubpixelText, &SkPaint::setSubpixelText);
1633
1634 paintFlag("Subpixel Anti-Alias",
1635 "Default\0lcd\0LCD\0\0",
1636 SkPaint::kLCDRenderText_Flag,
1637 &SkPaint::isLCDRenderText, &SkPaint::setLCDRenderText);
1638
1639 paintFlag("Embedded Bitmap Text",
1640 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1641 SkPaint::kEmbeddedBitmapText_Flag,
1642 &SkPaint::isEmbeddedBitmapText, &SkPaint::setEmbeddedBitmapText);
1643
1644 paintFlag("Force Auto-Hinting",
1645 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1646 SkPaint::kAutoHinting_Flag,
1647 &SkPaint::isAutohinted, &SkPaint::setAutohinted);
1648
1649 paintFlag("Vertical Text",
1650 "Default\0No Vertical Text\0Vertical Text\0\0",
1651 SkPaint::kVerticalText_Flag,
1652 &SkPaint::isVerticalText, &SkPaint::setVerticalText);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001653
1654 ImGui::Checkbox("Override TextSize", &fPaintOverrides.fTextSize);
1655 if (fPaintOverrides.fTextSize) {
1656 ImGui::DragFloat2("TextRange", fPaintOverrides.fTextSizeRange,
1657 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
1658 float textSize = fPaint.getTextSize();
1659 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
1660 fPaintOverrides.fTextSizeRange[0],
1661 fPaintOverrides.fTextSizeRange[1],
1662 "%.6f", 2.0f))
1663 {
1664 fPaint.setTextSize(textSize);
1665 this->preTouchMatrixChanged();
1666 paramsChanged = true;
1667 }
1668 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001669 }
1670
Mike Reed81f60ec2018-05-15 10:09:52 -04001671 {
1672 SkMetaData controls;
1673 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1674 if (ImGui::CollapsingHeader("Current Slide")) {
1675 SkMetaData::Iter iter(controls);
1676 const char* name;
1677 SkMetaData::Type type;
1678 int count;
1679 bool found = false;
1680 while ((name = iter.next(&type, &count)) != nullptr && found == false) {
1681 if (type == SkMetaData::kScalar_Type) {
1682 float val[3];
1683 SkASSERT(count == 3);
1684 controls.findScalars(name, &count, val);
1685 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1686 controls.setScalars(name, 3, val);
1687 fSlides[fCurrentSlide]->onSetControls(controls);
1688 found = paramsChanged = true;
1689 }
1690 }
1691 }
1692 }
1693 }
1694 }
1695
Ben Wagner7a3c6742018-04-23 10:01:07 -04001696 if (fShowSlidePicker) {
1697 ImGui::SetNextTreeNodeOpen(true);
1698 }
Brian Osman79086b92017-02-10 13:36:16 -05001699 if (ImGui::CollapsingHeader("Slide")) {
1700 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001701 static ImVector<const char*> filteredSlideNames;
1702 static ImVector<int> filteredSlideIndices;
1703
Brian Osmanfce09c52017-11-14 15:32:20 -05001704 if (fShowSlidePicker) {
1705 ImGui::SetKeyboardFocusHere();
1706 fShowSlidePicker = false;
1707 }
1708
Brian Osman79086b92017-02-10 13:36:16 -05001709 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001710 filteredSlideNames.clear();
1711 filteredSlideIndices.clear();
1712 int filteredIndex = 0;
1713 for (int i = 0; i < fSlides.count(); ++i) {
1714 const char* slideName = fSlides[i]->getName().c_str();
1715 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1716 if (i == fCurrentSlide) {
1717 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001718 }
Brian Osmanf479e422017-11-08 13:11:36 -05001719 filteredSlideNames.push_back(slideName);
1720 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001721 }
Brian Osman79086b92017-02-10 13:36:16 -05001722 }
Brian Osmanf479e422017-11-08 13:11:36 -05001723
Brian Osmanf479e422017-11-08 13:11:36 -05001724 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1725 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001726 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001727 }
1728 }
Brian Osmana109e392017-02-24 09:49:14 -05001729
1730 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001731 ColorMode newMode = fColorMode;
1732 auto cmButton = [&](ColorMode mode, const char* label) {
1733 if (ImGui::RadioButton(label, mode == fColorMode)) {
1734 newMode = mode;
1735 }
1736 };
1737
1738 cmButton(ColorMode::kLegacy, "Legacy 8888");
1739 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1740 "Color Managed 8888 (Nonlinear blending)");
1741 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1742 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1743
1744 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001745 // It isn't safe to switch color mode now (in the middle of painting). We might
1746 // tear down the back-end, etc... Defer this change until the next onIdle.
1747 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001748 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001749 });
Brian Osmana109e392017-02-24 09:49:14 -05001750 }
1751
1752 // Pick from common gamuts:
1753 int primariesIdx = 4; // Default: Custom
1754 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1755 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1756 primariesIdx = i;
1757 break;
1758 }
1759 }
1760
Brian Osmanfdab5762017-11-09 10:27:55 -05001761 // When we're in xform canvas mode, we can alter the transfer function, too
1762 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1763 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1764 }
1765
Brian Osmana109e392017-02-24 09:49:14 -05001766 if (ImGui::Combo("Primaries", &primariesIdx,
1767 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1768 if (primariesIdx >= 0 && primariesIdx <= 3) {
1769 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1770 }
1771 }
1772
1773 // Allow direct editing of gamut
1774 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1775 }
Brian Osman79086b92017-02-10 13:36:16 -05001776 }
Brian Salomon99a33902017-03-07 15:16:34 -05001777 if (paramsChanged) {
1778 fDeferredActions.push_back([=]() {
1779 fWindow->setRequestedDisplayParams(params);
1780 fWindow->inval();
1781 this->updateTitle();
1782 });
1783 }
Brian Osman79086b92017-02-10 13:36:16 -05001784 ImGui::End();
1785 }
1786
Brian Osmanf6877092017-02-13 09:39:57 -05001787 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04001788 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
1789 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001790 static int zoomFactor = 8;
1791 if (ImGui::Button("<<")) {
1792 zoomFactor = SkTMax(zoomFactor / 2, 4);
1793 }
1794 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1795 if (ImGui::Button(">>")) {
1796 zoomFactor = SkTMin(zoomFactor * 2, 32);
1797 }
Brian Osmanf6877092017-02-13 09:39:57 -05001798
Ben Wagner3627d2e2018-06-26 14:23:20 -04001799 if (!fZoomWindowFixed) {
1800 ImVec2 mousePos = ImGui::GetMousePos();
1801 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
1802 }
1803 SkScalar x = fZoomWindowLocation.x();
1804 SkScalar y = fZoomWindowLocation.y();
1805 int xInt = SkScalarRoundToInt(x);
1806 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05001807 ImVec2 avail = ImGui::GetContentRegionAvail();
1808
Brian Osmanead517d2017-11-13 15:36:36 -05001809 uint32_t pixel = 0;
1810 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001811 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001812 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001813 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
Ben Wagner3627d2e2018-06-26 14:23:20 -04001814 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05001815 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001816 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1817 }
1818
Brian Osmand67e5182017-12-08 16:46:09 -05001819 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001820 // Translate so the region of the image that's under the mouse cursor is centered
1821 // in the zoom canvas:
1822 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001823 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
1824 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05001825 c->drawImage(this->fLastImage, 0, 0);
1826
1827 SkPaint outline;
1828 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001829 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05001830 });
Brian Osmanf6877092017-02-13 09:39:57 -05001831 }
1832
1833 ImGui::End();
1834 }
Brian Osman79086b92017-02-10 13:36:16 -05001835}
1836
liyuqian2edb0f42016-07-06 14:11:32 -07001837void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001838 for (int i = 0; i < fDeferredActions.count(); ++i) {
1839 fDeferredActions[i]();
1840 }
1841 fDeferredActions.reset();
1842
Brian Osman56a24812017-12-19 11:15:16 -05001843 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001844 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001845 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001846 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001847
Brian Osman79086b92017-02-10 13:36:16 -05001848 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001849 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001850 fWindow->inval();
1851 }
jvanverth9f372462016-04-06 06:08:59 -07001852}
liyuqiane5a6cd92016-05-27 08:52:52 -07001853
Florin Malitab632df72018-06-18 21:23:06 -04001854template <typename OptionsFunc>
1855static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
1856 OptionsFunc&& optionsFunc) {
1857 writer.beginObject();
1858 {
1859 writer.appendString(kName , name);
1860 writer.appendString(kValue, value);
1861
1862 writer.beginArray(kOptions);
1863 {
1864 optionsFunc(writer);
1865 }
1866 writer.endArray();
1867 }
1868 writer.endObject();
1869}
1870
1871
liyuqiane5a6cd92016-05-27 08:52:52 -07001872void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001873 if (!fWindow) {
1874 return;
1875 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001876 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001877 return; // Surface hasn't been created yet.
1878 }
1879
Florin Malitab632df72018-06-18 21:23:06 -04001880 SkDynamicMemoryWStream memStream;
1881 SkJSONWriter writer(&memStream);
1882 writer.beginArray();
1883
liyuqianb73c24b2016-06-03 08:47:23 -07001884 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04001885 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
1886 [this](SkJSONWriter& writer) {
1887 for(const auto& slide : fSlides) {
1888 writer.appendString(slide->getName().c_str());
1889 }
1890 });
liyuqiane5a6cd92016-05-27 08:52:52 -07001891
liyuqianb73c24b2016-06-03 08:47:23 -07001892 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04001893 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
1894 [](SkJSONWriter& writer) {
1895 for (const auto& str : kBackendTypeStrings) {
1896 writer.appendString(str);
1897 }
1898 });
liyuqiane5a6cd92016-05-27 08:52:52 -07001899
csmartdalton578f0642017-02-24 16:04:47 -07001900 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04001901 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
1902 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
1903 [this](SkJSONWriter& writer) {
1904 writer.appendS32(0);
1905
1906 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1907 return;
1908 }
1909
1910 for (int msaa : {4, 8, 16}) {
1911 writer.appendS32(msaa);
1912 }
1913 });
csmartdalton578f0642017-02-24 16:04:47 -07001914
csmartdalton61cd31a2017-02-27 17:00:53 -07001915 // Path renderer state
1916 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04001917 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
1918 [this](SkJSONWriter& writer) {
1919 const GrContext* ctx = fWindow->getGrContext();
1920 if (!ctx) {
1921 writer.appendString("Software");
1922 } else {
1923 const auto* caps = ctx->contextPriv().caps();
1924
Florin Malitab632df72018-06-18 21:23:06 -04001925 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
1926 if (fWindow->sampleCount() > 1) {
1927 if (caps->shaderCaps()->pathRenderingSupport()) {
1928 writer.appendString(
1929 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
1930 }
1931 } else {
1932 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1933 writer.appendString(
1934 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
1935 }
1936 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
1937 }
1938 writer.appendString(
1939 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
1940 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
1941 }
1942 });
csmartdalton61cd31a2017-02-27 17:00:53 -07001943
liyuqianb73c24b2016-06-03 08:47:23 -07001944 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04001945 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
1946 [this](SkJSONWriter& writer) {
1947 writer.appendString(kSoftkeyHint);
1948 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1949 writer.appendString(softkey.c_str());
1950 }
1951 });
liyuqianb73c24b2016-06-03 08:47:23 -07001952
Florin Malitab632df72018-06-18 21:23:06 -04001953 writer.endArray();
1954 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07001955
Florin Malitab632df72018-06-18 21:23:06 -04001956 auto data = memStream.detachAsData();
1957
1958 // TODO: would be cool to avoid this copy
1959 const SkString cstring(static_cast<const char*>(data->data()), data->size());
1960
1961 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001962}
1963
1964void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001965 // For those who will add more features to handle the state change in this function:
1966 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1967 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1968 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001969 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001970 for (int i = 0; i < fSlides.count(); ++i) {
1971 if (fSlides[i]->getName().equals(stateValue)) {
1972 this->setCurrentSlide(i);
1973 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001974 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001975 }
Florin Malitaab99c342018-01-16 16:23:03 -05001976
1977 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001978 } else if (stateName.equals(kBackendStateName)) {
1979 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1980 if (stateValue.equals(kBackendTypeStrings[i])) {
1981 if (fBackendType != i) {
1982 fBackendType = (sk_app::Window::BackendType)i;
1983 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001984 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001985 }
1986 break;
1987 }
1988 }
csmartdalton578f0642017-02-24 16:04:47 -07001989 } else if (stateName.equals(kMSAAStateName)) {
1990 DisplayParams params = fWindow->getRequestedDisplayParams();
1991 int sampleCount = atoi(stateValue.c_str());
1992 if (sampleCount != params.fMSAASampleCount) {
1993 params.fMSAASampleCount = sampleCount;
1994 fWindow->setRequestedDisplayParams(params);
1995 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001996 this->updateTitle();
1997 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001998 }
1999 } else if (stateName.equals(kPathRendererStateName)) {
2000 DisplayParams params = fWindow->getRequestedDisplayParams();
2001 for (const auto& pair : gPathRendererNames) {
2002 if (pair.second == stateValue.c_str()) {
2003 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2004 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2005 fWindow->setRequestedDisplayParams(params);
2006 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002007 this->updateTitle();
2008 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002009 }
2010 break;
2011 }
csmartdalton578f0642017-02-24 16:04:47 -07002012 }
liyuqianb73c24b2016-06-03 08:47:23 -07002013 } else if (stateName.equals(kSoftkeyStateName)) {
2014 if (!stateValue.equals(kSoftkeyHint)) {
2015 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002016 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002017 }
liyuqian2edb0f42016-07-06 14:11:32 -07002018 } else if (stateName.equals(kRefreshStateName)) {
2019 // This state is actually NOT in the UI state.
2020 // We use this to allow Android to quickly set bool fRefresh.
2021 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002022 } else {
2023 SkDebugf("Unknown stateName: %s", stateName.c_str());
2024 }
2025}
Brian Osman79086b92017-02-10 13:36:16 -05002026
2027bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002028 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002029}
2030
2031bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002032 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002033 fWindow->inval();
2034 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002035 } else {
2036 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002037 }
Brian Osman79086b92017-02-10 13:36:16 -05002038}