blob: 185868266fb8aad41c4dff4ecda906ab42dd9e04 [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
jvanverth34524262016-05-04 13:49:13 -07008#include "Viewer.h"
jvanverth9f372462016-04-06 06:08:59 -07009
Chris Dalton2d18f412018-02-20 13:23:32 -070010#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070011#include "GMSlide.h"
liyuqian6f163d22016-06-13 12:26:45 -070012#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050013#include "Resources.h"
jvanverthc7027ab2016-06-16 09:52:35 -070014#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070015#include "SKPSlide.h"
Florin Malita76a076b2018-02-15 18:40:48 -050016#include "SlideDir.h"
Florin Malitac659c2c2018-04-05 11:57:21 -040017#include "SvgSlide.h"
jvanverth9f372462016-04-06 06:08:59 -070018
csmartdalton61cd31a2017-02-27 17:00:53 -070019#include "GrContext.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070020#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050021#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040022#include "SkColorSpaceXformCanvas.h"
Ben Wagner483c7722018-02-20 17:06:07 -050023#include "SkCommonFlags.h"
Brian Osman2dd96932016-10-18 15:33:53 -040024#include "SkCommandLineFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070025#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040026#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050027#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040028#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050029#include "SkImagePriv.h"
Herb Derbyefe39bc2018-05-01 17:06:20 -040030#include "SkMakeUnique.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070031#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050032#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050033#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050034#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040035#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070036#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070037#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050038#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050039#include "SkTestFontMgr.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050040#include "SkThreadedBMPDevice.h"
jvanverth9f372462016-04-06 06:08:59 -070041
Brian Osman79086b92017-02-10 13:36:16 -050042#include "imgui.h"
43
Chris Dalton1a325d22017-07-14 15:17:41 -060044#include "ccpr/GrCoverageCountingPathRenderer.h"
45
csmartdalton578f0642017-02-24 16:04:47 -070046#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070047#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070048
Florin Malita87ccf332018-05-04 12:23:24 -040049#if defined(SK_ENABLE_SKOTTIE)
50 #include "SkottieSlide.h"
51#endif
52
jvanverth34524262016-05-04 13:49:13 -070053using namespace sk_app;
54
csmartdalton61cd31a2017-02-27 17:00:53 -070055static std::map<GpuPathRenderers, std::string> gPathRendererNames;
56
jvanverth9f372462016-04-06 06:08:59 -070057Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070058 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070059}
60
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060061static DEFINE_string(slide, "", "Start on this sample.");
62static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050063
bsalomon6c471f72016-07-26 12:56:32 -070064#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070065# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070066#else
67# define BACKENDS_STR "\"sw\" and \"gl\""
68#endif
69
Brian Osman2dd96932016-10-18 15:33:53 -040070static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070071
Brian Salomonbdecacf2018-02-02 20:32:49 -050072static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070073
Chris Dalton2d18f412018-02-20 13:23:32 -070074DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
75
Brian Osman53136aa2017-07-20 15:43:35 -040076DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050077
Florin Malita38792ce2018-05-08 10:36:18 -040078DEFINE_string2(file, f, "", "Open a single file for viewing.");
79
Brian Salomon194db172017-08-17 14:37:06 -040080const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070081 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040082#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
83 "ANGLE",
84#endif
jvanverth063ece72016-06-17 09:29:14 -070085#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070086 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070087#endif
csmartdalton578f0642017-02-24 16:04:47 -070088 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070089};
90
bsalomon6c471f72016-07-26 12:56:32 -070091static sk_app::Window::BackendType get_backend_type(const char* str) {
92#ifdef SK_VULKAN
93 if (0 == strcmp(str, "vk")) {
94 return sk_app::Window::kVulkan_BackendType;
95 } else
96#endif
Brian Salomon194db172017-08-17 14:37:06 -040097#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
98 if (0 == strcmp(str, "angle")) {
99 return sk_app::Window::kANGLE_BackendType;
100 } else
101#endif
bsalomon6c471f72016-07-26 12:56:32 -0700102 if (0 == strcmp(str, "gl")) {
103 return sk_app::Window::kNativeGL_BackendType;
104 } else if (0 == strcmp(str, "sw")) {
105 return sk_app::Window::kRaster_BackendType;
106 } else {
107 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
108 return sk_app::Window::kRaster_BackendType;
109 }
110}
111
Brian Osmana109e392017-02-24 09:49:14 -0500112static SkColorSpacePrimaries gSrgbPrimaries = {
113 0.64f, 0.33f,
114 0.30f, 0.60f,
115 0.15f, 0.06f,
116 0.3127f, 0.3290f };
117
118static SkColorSpacePrimaries gAdobePrimaries = {
119 0.64f, 0.33f,
120 0.21f, 0.71f,
121 0.15f, 0.06f,
122 0.3127f, 0.3290f };
123
124static SkColorSpacePrimaries gP3Primaries = {
125 0.680f, 0.320f,
126 0.265f, 0.690f,
127 0.150f, 0.060f,
128 0.3127f, 0.3290f };
129
130static SkColorSpacePrimaries gRec2020Primaries = {
131 0.708f, 0.292f,
132 0.170f, 0.797f,
133 0.131f, 0.046f,
134 0.3127f, 0.3290f };
135
136struct NamedPrimaries {
137 const char* fName;
138 SkColorSpacePrimaries* fPrimaries;
139} gNamedPrimaries[] = {
140 { "sRGB", &gSrgbPrimaries },
141 { "AdobeRGB", &gAdobePrimaries },
142 { "P3", &gP3Primaries },
143 { "Rec. 2020", &gRec2020Primaries },
144};
145
146static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
147 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
148}
149
Brian Osman70d2f432017-11-08 09:54:10 -0500150static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
151 // In raster mode, we still use GL for the window.
152 // This lets us render the GUI faster (and correct).
153 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
154}
155
liyuqiane5a6cd92016-05-27 08:52:52 -0700156const char* kName = "name";
157const char* kValue = "value";
158const char* kOptions = "options";
159const char* kSlideStateName = "Slide";
160const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700161const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700162const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700163const char* kSoftkeyStateName = "Softkey";
164const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700165const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700166const char* kON = "ON";
167const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700168const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700169
jvanverth34524262016-05-04 13:49:13 -0700170Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500171 : fCurrentSlide(-1)
172 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500173 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500174 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500175 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500176 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500177 , fShowZoomWindow(false)
178 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700179 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500180 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500181 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500182 // Our UI can only tweak gamma (currently), so start out gamma-only
183 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700184 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400185 , fRotation(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400186 , fGestureDevice(GestureDevice::kNone)
Brian Osman805a7272018-05-02 15:40:20 -0400187 , fPerspectiveMode(kPerspective_Off)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500188 , fTileCnt(0)
189 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700190{
Greg Daniel285db442016-10-14 09:12:53 -0400191 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700192
Brian Osmanf09e35e2017-12-15 14:48:09 -0500193 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
194 gPathRendererNames[GpuPathRenderers::kDefault] =
195 "Default Ganesh Behavior (best path renderer, not including CCPR)";
196 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500197 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
198 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
199 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
200 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700201
jvanverth2bb3b6d2016-04-08 07:24:09 -0700202 SkDebugf("Command line arguments: ");
203 for (int i = 1; i < argc; ++i) {
204 SkDebugf("%s ", argv[i]);
205 }
206 SkDebugf("\n");
207
208 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500209#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400210 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500211#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700212
Ben Wagner483c7722018-02-20 17:06:07 -0500213 if (!FLAGS_nativeFonts) {
214 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
215 }
216
Brian Osmanbc8150f2017-07-24 11:38:01 -0400217 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400218 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400219
bsalomon6c471f72016-07-26 12:56:32 -0700220 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700221 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700222
csmartdalton578f0642017-02-24 16:04:47 -0700223 DisplayParams displayParams;
224 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700225 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700226 fWindow->setRequestedDisplayParams(displayParams);
227
Brian Osman56a24812017-12-19 11:15:16 -0500228 // Configure timers
229 fStatsLayer.setActive(false);
230 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
231 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
232 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
233
jvanverth9f372462016-04-06 06:08:59 -0700234 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700235 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500236 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500237 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500238 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700239
brianosman622c8d52016-05-10 06:50:49 -0700240 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500241 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
242 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
243 fWindow->inval();
244 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500245 // Command to jump directly to the slide picker and give it focus
246 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
247 this->fShowImGuiDebugWindow = true;
248 this->fShowSlidePicker = true;
249 fWindow->inval();
250 });
251 // Alias that to Backspace, to match SampleApp
252 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
253 this->fShowImGuiDebugWindow = true;
254 this->fShowSlidePicker = true;
255 fWindow->inval();
256 });
Brian Osman79086b92017-02-10 13:36:16 -0500257 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
258 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
259 fWindow->inval();
260 });
Brian Osmanf6877092017-02-13 09:39:57 -0500261 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
262 this->fShowZoomWindow = !this->fShowZoomWindow;
263 fWindow->inval();
264 });
brianosman622c8d52016-05-10 06:50:49 -0700265 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500266 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700267 fWindow->inval();
268 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400269 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500270 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400271 this->updateTitle();
272 fWindow->inval();
273 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500274 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500275 switch (fColorMode) {
276 case ColorMode::kLegacy:
277 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
278 break;
279 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
280 this->setColorMode(ColorMode::kColorManagedSRGB8888);
281 break;
282 case ColorMode::kColorManagedSRGB8888:
283 this->setColorMode(ColorMode::kColorManagedLinearF16);
284 break;
285 case ColorMode::kColorManagedLinearF16:
286 this->setColorMode(ColorMode::kLegacy);
287 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500288 }
brianosman622c8d52016-05-10 06:50:49 -0700289 });
290 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500291 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700292 });
293 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500294 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700295 });
296 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
297 this->changeZoomLevel(1.f / 32.f);
298 fWindow->inval();
299 });
300 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
301 this->changeZoomLevel(-1.f / 32.f);
302 fWindow->inval();
303 });
jvanverthaf236b52016-05-20 06:01:06 -0700304 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400305 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
306 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500307 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400308#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
309 if (newBackend == sk_app::Window::kVulkan_BackendType) {
310 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
311 sk_app::Window::kBackendTypeCount);
312 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
313 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500314 }
315#endif
Brian Osman621491e2017-02-28 15:45:01 -0500316 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700317 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500318 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
319 fTileCnt++;
320 if (fThreadCnt == 0) {
321 this->resetExecutor();
322 }
323 this->updateTitle();
324 fWindow->inval();
325 });
326 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
327 fTileCnt = SkTMax(0, fTileCnt - 1);
328 if (fThreadCnt == 0) {
329 this->resetExecutor();
330 }
331 this->updateTitle();
332 fWindow->inval();
333 });
334 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
335 if (fTileCnt == 0) {
336 return;
337 }
338 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
339 this->resetExecutor();
340 this->updateTitle();
341 fWindow->inval();
342 });
343 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
344 if (fTileCnt == 0) {
345 return;
346 }
347 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
348 this->resetExecutor();
349 this->updateTitle();
350 fWindow->inval();
351 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500352 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
353 fSaveToSKP = true;
354 fWindow->inval();
355 });
Ben Wagner37c54032018-04-13 14:30:23 -0400356 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
357 DisplayParams params = fWindow->getRequestedDisplayParams();
358 uint32_t flags = params.fSurfaceProps.flags();
359 if (!fPixelGeometryOverrides) {
360 fPixelGeometryOverrides = true;
361 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
362 } else {
363 switch (params.fSurfaceProps.pixelGeometry()) {
364 case kUnknown_SkPixelGeometry:
365 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
366 break;
367 case kRGB_H_SkPixelGeometry:
368 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
369 break;
370 case kBGR_H_SkPixelGeometry:
371 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
372 break;
373 case kRGB_V_SkPixelGeometry:
374 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
375 break;
376 case kBGR_V_SkPixelGeometry:
377 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
378 fPixelGeometryOverrides = false;
379 break;
380 }
381 }
382 fWindow->setRequestedDisplayParams(params);
383 this->updateTitle();
384 fWindow->inval();
385 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500386 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
387 if (!fPaintOverrides.fHinting) {
388 fPaintOverrides.fHinting = true;
389 fPaint.setHinting(SkPaint::kNo_Hinting);
390 } else {
391 switch (fPaint.getHinting()) {
392 case SkPaint::kNo_Hinting:
393 fPaint.setHinting(SkPaint::kSlight_Hinting);
394 break;
395 case SkPaint::kSlight_Hinting:
396 fPaint.setHinting(SkPaint::kNormal_Hinting);
397 break;
398 case SkPaint::kNormal_Hinting:
399 fPaint.setHinting(SkPaint::kFull_Hinting);
400 break;
401 case SkPaint::kFull_Hinting:
402 fPaint.setHinting(SkPaint::kNo_Hinting);
403 fPaintOverrides.fHinting = false;
404 break;
405 }
406 }
407 this->updateTitle();
408 fWindow->inval();
409 });
410 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
411 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
412 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
413 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
414 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500415 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
416 gSkUseDeltaAA = gSkForceDeltaAA = false;
417 } else {
418 fPaint.setAntiAlias(true);
419 switch (fPaintOverrides.fAntiAlias) {
420 case SkPaintFields::AntiAliasState::Alias:
421 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400422 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
423 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500424 break;
425 case SkPaintFields::AntiAliasState::Normal:
426 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500427 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400428 gSkForceAnalyticAA = false;
429 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500430 break;
431 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
432 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400433 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
434 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500435 break;
436 case SkPaintFields::AntiAliasState::AnalyticAAForced:
437 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
438 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
439 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400440 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500441 break;
442 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
443 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400444 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
445 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500446 break;
447 case SkPaintFields::AntiAliasState::DeltaAAForced:
448 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
449 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
450 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
451 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
452 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
453 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
454 break;
455 }
456 }
457 this->updateTitle();
458 fWindow->inval();
459 });
Ben Wagner37c54032018-04-13 14:30:23 -0400460 fCommands.addCommand('D', "Modes", "DFT", [this]() {
461 DisplayParams params = fWindow->getRequestedDisplayParams();
462 uint32_t flags = params.fSurfaceProps.flags();
463 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
464 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
465 fWindow->setRequestedDisplayParams(params);
466 this->updateTitle();
467 fWindow->inval();
468 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500469 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
470 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
471 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
472 fPaint.setLCDRenderText(false);
473 } else {
474 if (!fPaint.isLCDRenderText()) {
475 fPaint.setLCDRenderText(true);
476 } else {
477 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
478 }
479 }
480 this->updateTitle();
481 fWindow->inval();
482 });
483 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
484 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
485 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
486 fPaint.setSubpixelText(false);
487 } else {
488 if (!fPaint.isSubpixelText()) {
489 fPaint.setSubpixelText(true);
490 } else {
491 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
492 }
493 }
494 this->updateTitle();
495 fWindow->inval();
496 });
Brian Osman805a7272018-05-02 15:40:20 -0400497 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
498 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
499 : kPerspective_Real;
500 this->updateTitle();
501 fWindow->inval();
502 });
503 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
504 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
505 : kPerspective_Off;
506 this->updateTitle();
507 fWindow->inval();
508 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500509
jvanverth2bb3b6d2016-04-08 07:24:09 -0700510 // set up slides
511 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500512 if (FLAGS_list) {
513 this->listNames();
514 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700515
Brian Osman9bb47cf2018-04-26 15:55:00 -0400516 fPerspectivePoints[0].set(0, 0);
517 fPerspectivePoints[1].set(1, 0);
518 fPerspectivePoints[2].set(0, 1);
519 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700520 fAnimTimer.run();
521
Hal Canaryc465d132017-12-08 10:21:31 -0500522 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500523 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400524 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500525 }
526 fImGuiGamutPaint.setColor(SK_ColorWHITE);
527 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
528
Brian Osman70d2f432017-11-08 09:54:10 -0500529 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500530 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700531}
532
jvanverth34524262016-05-04 13:49:13 -0700533void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700534 fAllSlideNames = Json::Value(Json::arrayValue);
535
Florin Malita0ffa3222018-04-05 14:34:45 -0400536 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
537 static const struct {
538 const char* fExtension;
539 const char* fDirName;
540 const SkCommandLineFlags::StringArray& fFlags;
541 const SlideFactory fFactory;
542 } gExternalSlidesInfo[] = {
543 { ".skp", "skp-dir", FLAGS_skps,
544 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
545 return sk_make_sp<SKPSlide>(name, path);}
546 },
547 { ".jpg", "jpg-dir", FLAGS_jpgs,
548 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
549 return sk_make_sp<ImageSlide>(name, path);}
550 },
Florin Malita87ccf332018-05-04 12:23:24 -0400551#if defined(SK_ENABLE_SKOTTIE)
Florin Malita0ffa3222018-04-05 14:34:45 -0400552 { ".json", "skottie-dir", FLAGS_jsons,
553 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
554 return sk_make_sp<SkottieSlide>(name, path);}
555 },
Florin Malita87ccf332018-05-04 12:23:24 -0400556#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400557 { ".svg", "svg-dir", FLAGS_svgs,
558 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
559 return sk_make_sp<SvgSlide>(name, path);}
560 },
561 };
jvanverthc265a922016-04-08 12:51:45 -0700562
Florin Malita0ffa3222018-04-05 14:34:45 -0400563 SkTArray<sk_sp<Slide>, true> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700564
Florin Malita0ffa3222018-04-05 14:34:45 -0400565 const auto addSlide = [&](const SkString& name,
566 const SkString& path,
567 const SlideFactory& fact) {
568 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
569 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700570 }
liyuqian6f163d22016-06-13 12:26:45 -0700571
Florin Malita0ffa3222018-04-05 14:34:45 -0400572 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500573 dirSlides.push_back(slide);
574 fSlides.push_back(std::move(slide));
575 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400576 };
Florin Malita76a076b2018-02-15 18:40:48 -0500577
Florin Malita38792ce2018-05-08 10:36:18 -0400578 if (!FLAGS_file.isEmpty()) {
579 // single file mode
580 const SkString file(FLAGS_file[0]);
581
582 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
583 for (const auto& sinfo : gExternalSlidesInfo) {
584 if (file.endsWith(sinfo.fExtension)) {
585 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
586 return;
587 }
588 }
589
590 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
591 } else {
592 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
593 }
594
595 return;
596 }
597
598 // Bisect slide.
599 if (!FLAGS_bisect.isEmpty()) {
600 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
601 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
602 if (FLAGS_bisect.count() >= 2) {
603 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
604 bisect->onChar(*ch);
605 }
606 }
607 fSlides.push_back(std::move(bisect));
608 }
609 }
610
611 // GMs
612 int firstGM = fSlides.count();
613 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
614 while (gms) {
615 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
616
617 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
618 sk_sp<Slide> slide(new GMSlide(gm.release()));
619 fSlides.push_back(std::move(slide));
620 }
621
622 gms = gms->next();
623 }
624 // reverse gms
625 int numGMs = fSlides.count() - firstGM;
626 for (int i = 0; i < numGMs/2; ++i) {
627 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
628 }
629
630 // samples
631 const SkViewRegister* reg = SkViewRegister::Head();
632 while (reg) {
633 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
634 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
635 fSlides.push_back(slide);
636 }
637 reg = reg->next();
638 }
639
Florin Malita0ffa3222018-04-05 14:34:45 -0400640 for (const auto& info : gExternalSlidesInfo) {
641 for (const auto& flag : info.fFlags) {
642 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
643 // single file
644 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
645 } else {
646 // directory
647 SkOSFile::Iter it(flag.c_str(), info.fExtension);
648 SkString name;
649 while (it.next(&name)) {
650 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
651 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400652 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400653
654 if (!dirSlides.empty()) {
655 fSlides.push_back(
656 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
657 std::move(dirSlides)));
658 dirSlides.reset();
659 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400660 }
661 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700662}
663
664
jvanverth34524262016-05-04 13:49:13 -0700665Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700666 fWindow->detach();
667 delete fWindow;
668}
669
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500670struct SkPaintTitleUpdater {
671 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
672 void append(const char* s) {
673 if (fCount == 0) {
674 fTitle->append(" {");
675 } else {
676 fTitle->append(", ");
677 }
678 fTitle->append(s);
679 ++fCount;
680 }
681 void done() {
682 if (fCount > 0) {
683 fTitle->append("}");
684 }
685 }
686 SkString* fTitle;
687 int fCount;
688};
689
brianosman05de2162016-05-06 13:28:57 -0700690void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700691 if (!fWindow) {
692 return;
693 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500694 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700695 return; // Surface hasn't been created yet.
696 }
697
jvanverth34524262016-05-04 13:49:13 -0700698 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700699 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700700
Yuqian Li399b3c22017-08-03 11:08:15 -0400701 if (gSkUseDeltaAA) {
702 if (gSkForceDeltaAA) {
703 title.append(" <FDAA>");
704 } else {
705 title.append(" <DAA>");
706 }
707 } else if (gSkUseAnalyticAA) {
708 if (gSkForceAnalyticAA) {
709 title.append(" <FAAA>");
710 } else {
711 title.append(" <AAA>");
712 }
713 }
714
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500715 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner99a78dc2018-05-09 18:23:51 -0400716 auto paintFlag = [this, &paintTitle](SkPaint::Flags flag, bool (SkPaint::* isFlag)() const,
717 const char* on, const char* off)
718 {
719 if (fPaintOverrides.fFlags & flag) {
720 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500721 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400722 };
723
724 paintFlag(SkPaint::kAntiAlias_Flag, &SkPaint::isAntiAlias, "Antialias", "Alias");
725 paintFlag(SkPaint::kDither_Flag, &SkPaint::isDither, "DITHER", "No Dither");
726 paintFlag(SkPaint::kFakeBoldText_Flag, &SkPaint::isFakeBoldText, "Fake Bold", "No Fake Bold");
727 paintFlag(SkPaint::kLinearText_Flag, &SkPaint::isLinearText, "Linear Text", "Non-Linear Text");
728 paintFlag(SkPaint::kSubpixelText_Flag, &SkPaint::isSubpixelText, "Subpixel Text", "Pixel Text");
729 paintFlag(SkPaint::kLCDRenderText_Flag, &SkPaint::isLCDRenderText, "LCD", "lcd");
730 paintFlag(SkPaint::kEmbeddedBitmapText_Flag, &SkPaint::isEmbeddedBitmapText,
731 "Bitmap Text", "No Bitmap Text");
732 paintFlag(SkPaint::kAutoHinting_Flag, &SkPaint::isAutohinted,
733 "Force Autohint", "No Force Autohint");
734 paintFlag(SkPaint::kVerticalText_Flag, &SkPaint::isVerticalText,
735 "Vertical Text", "No Vertical Text");
736
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500737 if (fPaintOverrides.fHinting) {
738 switch (fPaint.getHinting()) {
739 case SkPaint::kNo_Hinting:
740 paintTitle.append("No Hinting");
741 break;
742 case SkPaint::kSlight_Hinting:
743 paintTitle.append("Slight Hinting");
744 break;
745 case SkPaint::kNormal_Hinting:
746 paintTitle.append("Normal Hinting");
747 break;
748 case SkPaint::kFull_Hinting:
749 paintTitle.append("Full Hinting");
750 break;
751 }
752 }
753 paintTitle.done();
754
Yuqian Lib2ba6642017-11-22 12:07:41 -0500755 if (fTileCnt > 0) {
756 title.appendf(" T%d", fTileCnt);
757 if (fThreadCnt > 0) {
758 title.appendf("/%d", fThreadCnt);
759 }
760 }
761
Brian Osman92004802017-03-06 11:47:26 -0500762 switch (fColorMode) {
763 case ColorMode::kLegacy:
764 title.append(" Legacy 8888");
765 break;
766 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
767 title.append(" ColorManaged 8888 (Nonlinear blending)");
768 break;
769 case ColorMode::kColorManagedSRGB8888:
770 title.append(" ColorManaged 8888");
771 break;
772 case ColorMode::kColorManagedLinearF16:
773 title.append(" ColorManaged F16");
774 break;
775 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500776
Brian Osman92004802017-03-06 11:47:26 -0500777 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500778 int curPrimaries = -1;
779 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
780 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
781 curPrimaries = i;
782 break;
783 }
784 }
785 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500786
787 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
788 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
789 }
brianosman05de2162016-05-06 13:28:57 -0700790 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500791
Ben Wagner37c54032018-04-13 14:30:23 -0400792 const DisplayParams& params = fWindow->getRequestedDisplayParams();
793 if (fPixelGeometryOverrides) {
794 switch (params.fSurfaceProps.pixelGeometry()) {
795 case kUnknown_SkPixelGeometry:
796 title.append( " Flat");
797 break;
798 case kRGB_H_SkPixelGeometry:
799 title.append( " RGB");
800 break;
801 case kBGR_H_SkPixelGeometry:
802 title.append( " BGR");
803 break;
804 case kRGB_V_SkPixelGeometry:
805 title.append( " RGBV");
806 break;
807 case kBGR_V_SkPixelGeometry:
808 title.append( " BGRV");
809 break;
810 }
811 }
812
813 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
814 title.append(" DFT");
815 }
816
csmartdalton578f0642017-02-24 16:04:47 -0700817 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700818 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500819 int msaa = fWindow->sampleCount();
820 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700821 title.appendf(" MSAA: %i", msaa);
822 }
823 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700824
825 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400826 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700827 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
828 }
829
Brian Osman805a7272018-05-02 15:40:20 -0400830 if (kPerspective_Real == fPerspectiveMode) {
831 title.append(" Perpsective (Real)");
832 } else if (kPerspective_Fake == fPerspectiveMode) {
833 title.append(" Perspective (Fake)");
834 }
835
brianosman05de2162016-05-06 13:28:57 -0700836 fWindow->setTitle(title.c_str());
837}
838
Florin Malitaab99c342018-01-16 16:23:03 -0500839int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500840
841 if (!FLAGS_slide.isEmpty()) {
842 int count = fSlides.count();
843 for (int i = 0; i < count; i++) {
844 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500845 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500846 }
847 }
848
849 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
850 this->listNames();
851 }
852
Florin Malitaab99c342018-01-16 16:23:03 -0500853 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500854}
855
Florin Malitaab99c342018-01-16 16:23:03 -0500856void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500857 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500858 for (const auto& slide : fSlides) {
859 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500860 }
861}
862
Florin Malitaab99c342018-01-16 16:23:03 -0500863void Viewer::setCurrentSlide(int slide) {
864 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700865
Florin Malitaab99c342018-01-16 16:23:03 -0500866 if (slide == fCurrentSlide) {
867 return;
868 }
869
870 if (fCurrentSlide >= 0) {
871 fSlides[fCurrentSlide]->unload();
872 }
873
874 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
875 SkIntToScalar(fWindow->height()));
876 fCurrentSlide = slide;
877 this->setupCurrentSlide();
878}
879
880void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500881 if (fCurrentSlide >= 0) {
882 // prepare dimensions for image slides
883 fGesture.resetTouchState();
884 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700885
Jim Van Verth0848fb02018-01-22 13:39:30 -0500886 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
887 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
888 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400889
Jim Van Verth0848fb02018-01-22 13:39:30 -0500890 // Start with a matrix that scales the slide to the available screen space
891 if (fWindow->scaleContentToFit()) {
892 if (windowRect.width() > 0 && windowRect.height() > 0) {
893 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
894 }
liyuqiane46e4f02016-05-20 07:32:19 -0700895 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500896
897 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400898 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500899
900 this->updateTitle();
901 this->updateUIState();
902
903 fStatsLayer.resetMeasurements();
904
905 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700906 }
jvanverthc265a922016-04-08 12:51:45 -0700907}
908
909#define MAX_ZOOM_LEVEL 8
910#define MIN_ZOOM_LEVEL -8
911
jvanverth34524262016-05-04 13:49:13 -0700912void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700913 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400914 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400915 this->preTouchMatrixChanged();
916}
Yuqian Li755778c2018-03-28 16:23:31 -0400917
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400918void Viewer::preTouchMatrixChanged() {
919 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400920 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
921 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
922 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
923 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
924}
925
Brian Osman805a7272018-05-02 15:40:20 -0400926SkMatrix Viewer::computePerspectiveMatrix() {
927 SkScalar w = fWindow->width(), h = fWindow->height();
928 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
929 SkPoint perspPts[4] = {
930 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
931 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
932 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
933 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
934 };
935 SkMatrix m;
936 m.setPolyToPoly(orthoPts, perspPts, 4);
937 return m;
938}
939
Yuqian Li755778c2018-03-28 16:23:31 -0400940SkMatrix Viewer::computePreTouchMatrix() {
941 SkMatrix m = fDefaultMatrix;
942 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
943 : SK_Scalar1 + fZoomLevel;
944 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400945
946 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
947 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400948
Brian Osman805a7272018-05-02 15:40:20 -0400949 if (kPerspective_Real == fPerspectiveMode) {
950 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400951 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400952 }
953
Yuqian Li755778c2018-03-28 16:23:31 -0400954 return m;
jvanverthc265a922016-04-08 12:51:45 -0700955}
956
liyuqiand3cdbca2016-05-17 12:44:20 -0700957SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400958 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700959 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400960 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700961 return m;
jvanverthc265a922016-04-08 12:51:45 -0700962}
963
Brian Osman621491e2017-02-28 15:45:01 -0500964void Viewer::setBackend(sk_app::Window::BackendType backendType) {
965 fBackendType = backendType;
966
967 fWindow->detach();
968
Brian Osman70d2f432017-11-08 09:54:10 -0500969#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400970 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
971 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500972 DisplayParams params = fWindow->getRequestedDisplayParams();
973 delete fWindow;
974 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500975
Brian Osman70d2f432017-11-08 09:54:10 -0500976 // re-register callbacks
977 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500978 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500979 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500980 fWindow->pushLayer(&fImGuiLayer);
981
Brian Osman70d2f432017-11-08 09:54:10 -0500982 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
983 // will still include our correct sample count. But the re-created fWindow will lose that
984 // information. On Windows, we need to re-create the window when changing sample count,
985 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
986 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
987 // as if we had never changed windows at all).
988 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500989#endif
990
Brian Osman70d2f432017-11-08 09:54:10 -0500991 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500992}
993
Brian Osman92004802017-03-06 11:47:26 -0500994void Viewer::setColorMode(ColorMode colorMode) {
995 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700996
Brian Osmanf750fbc2017-02-08 10:47:28 -0500997 // 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 -0400998 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700999 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -05001000 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001001 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
1002 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -05001003 if (wasInLegacy != wantLegacy) {
1004 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -07001005 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001006 }
1007
1008 this->updateTitle();
1009 fWindow->inval();
1010}
1011
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001012class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1013public:
1014 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
1015 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
1016 { }
1017 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001018 if (*paint == nullptr) {
1019 return true;
1020 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001021 if (fPaintOverrides->fHinting) {
1022 paint->writable()->setHinting(fPaint->getHinting());
1023 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001024
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001025 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
1026 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1027 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001028 if (fPaintOverrides->fFlags & SkPaint::kDither_Flag) {
1029 paint->writable()->setDither(fPaint->isDither());
1030 }
1031 if (fPaintOverrides->fFlags & SkPaint::kFakeBoldText_Flag) {
1032 paint->writable()->setFakeBoldText(fPaint->isFakeBoldText());
1033 }
1034 if (fPaintOverrides->fFlags & SkPaint::kLinearText_Flag) {
1035 paint->writable()->setLinearText(fPaint->isLinearText());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001036 }
1037 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
1038 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
1039 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001040 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
1041 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
1042 }
1043 if (fPaintOverrides->fFlags & SkPaint::kEmbeddedBitmapText_Flag) {
1044 paint->writable()->setEmbeddedBitmapText(fPaint->isEmbeddedBitmapText());
1045 }
1046 if (fPaintOverrides->fFlags & SkPaint::kAutoHinting_Flag) {
1047 paint->writable()->setAutohinted(fPaint->isAutohinted());
1048 }
1049 if (fPaintOverrides->fFlags & SkPaint::kVerticalText_Flag) {
1050 paint->writable()->setVerticalText(fPaint->isVerticalText());
1051 }
1052
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001053 return true;
1054 }
1055 SkPaint* fPaint;
1056 Viewer::SkPaintFields* fPaintOverrides;
1057};
1058
Brian Osmanf750fbc2017-02-08 10:47:28 -05001059void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001060 SkAutoCanvasRestore autorestore(canvas, false);
1061
Brian Osmanf750fbc2017-02-08 10:47:28 -05001062 // By default, we render directly into the window's surface/canvas
1063 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001064 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001065
Brian Osmane0d4fba2017-03-15 10:24:55 -04001066 // If we're in any of the color managed modes, construct the color space we're going to use
1067 sk_sp<SkColorSpace> cs = nullptr;
1068 if (ColorMode::kLegacy != fColorMode) {
1069 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
1070 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -04001071 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001072 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -05001073 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1074 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
1075 } else {
1076 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
1077 }
Brian Osmane0d4fba2017-03-15 10:24:55 -04001078 }
1079
Brian Osman3ac99cf2017-12-01 11:23:53 -05001080 if (fSaveToSKP) {
1081 SkPictureRecorder recorder;
1082 SkCanvas* recorderCanvas = recorder.beginRecording(
1083 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1084 // In xform-canvas mode, record the transformed output
1085 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1086 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1087 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
1088 recorderCanvas = xformCanvas.get();
1089 }
1090 fSlides[fCurrentSlide]->draw(recorderCanvas);
1091 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1092 SkFILEWStream stream("sample_app.skp");
1093 picture->serialize(&stream);
1094 fSaveToSKP = false;
1095 }
1096
Brian Osmane0d4fba2017-03-15 10:24:55 -04001097 // 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 -05001098 // 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 -04001099 // because the window surface is actually GL, or we're doing fake perspective.
Brian Osmanf750fbc2017-02-08 10:47:28 -05001100 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -05001101 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -05001102 if (Window::kRaster_BackendType == fBackendType ||
Brian Osman805a7272018-05-02 15:40:20 -04001103 kPerspective_Fake == fPerspectiveMode ||
Brian Osman70d2f432017-11-08 09:54:10 -05001104 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -05001105 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -04001106 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
1107 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
1108
Brian Osman92004802017-03-06 11:47:26 -05001109 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
1110 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001111 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
1112 // with a special canvas (below) that has the color space attached
1113 sk_sp<SkColorSpace> offscreenColorSpace =
1114 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -05001115 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -04001116 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Ben Wagner37c54032018-04-13 14:30:23 -04001117 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1118 canvas->getProps(&props);
1119 offscreenSurface = Window::kRaster_BackendType == fBackendType
1120 ? SkSurface::MakeRaster(info, &props)
1121 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -05001122 SkPixmap offscreenPixmap;
1123 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
1124 SkBitmap offscreenBitmap;
1125 offscreenBitmap.installPixels(offscreenPixmap);
Herb Derbyefe39bc2018-05-01 17:06:20 -04001126 threadedCanvas =
1127 skstd::make_unique<SkCanvas>(
1128 sk_make_sp<SkThreadedBMPDevice>(
1129 offscreenBitmap, fTileCnt, fThreadCnt, fExecutor.get()));
Yuqian Lib2ba6642017-11-22 12:07:41 -05001130 slideCanvas = threadedCanvas.get();
1131 } else {
1132 slideCanvas = offscreenSurface->getCanvas();
1133 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001134 }
1135
Brian Osmane0d4fba2017-03-15 10:24:55 -04001136 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1137 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1138 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
1139 slideCanvas = xformCanvas.get();
1140 }
1141
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001142 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001143 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001144 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -05001145 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001146 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001147 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1148 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001149 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001150 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001151
1152 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001153 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001154 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001155 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001156
1157 // If we rendered offscreen, snap an image and push the results to the window's canvas
1158 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001159 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001160
1161 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -04001162 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001163 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001164 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001165 SkPaint paint;
1166 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001167 int prePerspectiveCount = canvas->save();
1168 if (kPerspective_Fake == fPerspectiveMode) {
1169 paint.setFilterQuality(kHigh_SkFilterQuality);
1170 canvas->clear(SK_ColorWHITE);
1171 canvas->concat(this->computePerspectiveMatrix());
1172 }
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001173 canvas->drawImage(retaggedImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001174 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001175 }
liyuqian6f163d22016-06-13 12:26:45 -07001176}
1177
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001178void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001179 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001180 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001181}
Jim Van Verth6f449692017-02-14 15:16:46 -05001182
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001183void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001184 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001185
brianosman622c8d52016-05-10 06:50:49 -07001186 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001187
Brian Osmand67e5182017-12-08 16:46:09 -05001188 this->drawImGui();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001189}
1190
Florin Malitacefc1b92018-02-19 21:43:47 -05001191SkPoint Viewer::mapEvent(float x, float y) {
1192 const auto m = this->computeMatrix();
1193 SkMatrix inv;
1194
1195 SkAssertResult(m.invert(&inv));
1196
1197 return inv.mapXY(x, y);
1198}
1199
jvanverth814e38d2016-06-06 08:48:47 -07001200bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001201 if (GestureDevice::kMouse == fGestureDevice) {
1202 return false;
1203 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001204
1205 const auto slidePt = this->mapEvent(x, y);
1206 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1207 fWindow->inval();
1208 return true;
1209 }
1210
liyuqiand3cdbca2016-05-17 12:44:20 -07001211 void* castedOwner = reinterpret_cast<void*>(owner);
1212 switch (state) {
1213 case Window::kUp_InputState: {
1214 fGesture.touchEnd(castedOwner);
1215 break;
1216 }
1217 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001218 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001219 break;
1220 }
1221 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001222 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001223 break;
1224 }
1225 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001226 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001227 fWindow->inval();
1228 return true;
1229}
1230
Brian Osman80fc07e2017-12-08 16:45:43 -05001231bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001232 if (GestureDevice::kTouch == fGestureDevice) {
1233 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001234 }
Brian Osman16c81a12017-12-20 11:58:34 -05001235
Florin Malitacefc1b92018-02-19 21:43:47 -05001236 const auto slidePt = this->mapEvent(x, y);
1237 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1238 fWindow->inval();
1239 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001240 }
1241
1242 switch (state) {
1243 case Window::kUp_InputState: {
1244 fGesture.touchEnd(nullptr);
1245 break;
1246 }
1247 case Window::kDown_InputState: {
1248 fGesture.touchBegin(nullptr, x, y);
1249 break;
1250 }
1251 case Window::kMove_InputState: {
1252 fGesture.touchMoved(nullptr, x, y);
1253 break;
1254 }
1255 }
1256 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1257
1258 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1259 fWindow->inval();
1260 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001261 return true;
1262}
1263
Brian Osmana109e392017-02-24 09:49:14 -05001264static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1265 const ImVec2& pos, const ImVec2& size) {
1266 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1267 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1268
1269 // Invisible 10x10 button
1270 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1271 ImGui::InvisibleButton(label, ImVec2(10, 10));
1272
1273 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1274 ImGuiIO& io = ImGui::GetIO();
1275 // Normalized mouse position, relative to our gamut box
1276 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1277 // Clamp to edge of box, convert back to primary scale
1278 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1279 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1280 }
1281
1282 if (ImGui::IsItemHovered()) {
1283 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1284 }
1285
1286 // Return screen coordinates for the caller. We could just return center here, but we'd have
1287 // one frame of lag during drag.
1288 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1289}
1290
1291static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1292 ImDrawList* drawList = ImGui::GetWindowDrawList();
1293
1294 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1295 // width, and scale the height to maintain aspect ratio.
1296 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1297 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1298 ImVec2 pos = ImGui::GetCursorScreenPos();
1299
1300 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1301 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1302 // Magic numbers are pixel locations of the origin and upper-right corner.
1303 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1304 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001305
1306 // Primary markers
1307 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1308 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1309 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1310 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1311
1312 // Gamut triangle
1313 drawList->AddCircle(r, 5.0f, 0xFF000040);
1314 drawList->AddCircle(g, 5.0f, 0xFF004000);
1315 drawList->AddCircle(b, 5.0f, 0xFF400000);
1316 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1317 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1318
1319 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001320 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1321}
1322
1323static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1324 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1325 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1326 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1327
1328 // Invisible 10x10 button
1329 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1330 ImGui::InvisibleButton(label, ImVec2(10, 10));
1331
1332 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1333 ImGuiIO& io = ImGui::GetIO();
1334 // Normalized mouse position, relative to our gamut box
1335 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1336 // Clamp to edge of box
1337 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1338 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1339 *dragging = true;
1340 }
1341
1342 // Return screen coordinates for the caller. We could just return center here, but we'd have
1343 // one frame of lag during drag.
1344 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1345}
1346
1347static bool ImGui_DragQuad(SkPoint* pts) {
1348 ImDrawList* drawList = ImGui::GetWindowDrawList();
1349
1350 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1351 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1352 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1353 ImVec2 pos = ImGui::GetCursorScreenPos();
1354
1355 // Background rectangle
1356 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1357
1358 // Corner markers
1359 bool dragging = false;
1360 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1361 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1362 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1363 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1364
1365 // Draw markers and quad
1366 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1367 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1368 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1369 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1370 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1371 drawList->AddLine(tr, br, 0xFFFFFFFF);
1372 drawList->AddLine(br, bl, 0xFFFFFFFF);
1373 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1374
1375 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1376 ImGui::Spacing();
1377
1378 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001379}
1380
Brian Osmand67e5182017-12-08 16:46:09 -05001381void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001382 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1383 if (fShowImGuiTestWindow) {
1384 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1385 }
1386
1387 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001388 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1389 // always visible, we can end up in a layout feedback loop.
1390 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001391 DisplayParams params = fWindow->getRequestedDisplayParams();
1392 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001393 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1394 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001395 if (ImGui::CollapsingHeader("Backend")) {
1396 int newBackend = static_cast<int>(fBackendType);
1397 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1398 ImGui::SameLine();
1399 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001400#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1401 ImGui::SameLine();
1402 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1403#endif
Brian Osman621491e2017-02-28 15:45:01 -05001404#if defined(SK_VULKAN)
1405 ImGui::SameLine();
1406 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1407#endif
1408 if (newBackend != fBackendType) {
1409 fDeferredActions.push_back([=]() {
1410 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1411 });
1412 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001413
Brian Salomon99a33902017-03-07 15:16:34 -05001414 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001415 bool* wire = &params.fGrContextOptions.fWireframeMode;
1416 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1417 paramsChanged = true;
1418 }
Brian Salomon99a33902017-03-07 15:16:34 -05001419
Brian Osman28b12522017-03-08 17:10:24 -05001420 if (ctx) {
1421 int sampleCount = fWindow->sampleCount();
1422 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001423 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001424 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1425 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1426 ImGui::RadioButton("16", &sampleCount, 16);
1427
1428 if (sampleCount != params.fMSAASampleCount) {
1429 params.fMSAASampleCount = sampleCount;
1430 paramsChanged = true;
1431 }
1432 }
1433
Ben Wagner37c54032018-04-13 14:30:23 -04001434 int pixelGeometryIdx = 0;
1435 if (fPixelGeometryOverrides) {
1436 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1437 }
1438 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1439 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1440 {
1441 uint32_t flags = params.fSurfaceProps.flags();
1442 if (pixelGeometryIdx == 0) {
1443 fPixelGeometryOverrides = false;
1444 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1445 } else {
1446 fPixelGeometryOverrides = true;
1447 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1448 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1449 }
1450 paramsChanged = true;
1451 }
1452
1453 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1454 if (ImGui::Checkbox("DFT", &useDFT)) {
1455 uint32_t flags = params.fSurfaceProps.flags();
1456 if (useDFT) {
1457 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1458 } else {
1459 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1460 }
1461 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1462 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1463 paramsChanged = true;
1464 }
1465
Brian Osman8a9de3d2017-03-01 14:59:05 -05001466 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001467 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001468 auto prButton = [&](GpuPathRenderers x) {
1469 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001470 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1471 params.fGrContextOptions.fGpuPathRenderers = x;
1472 paramsChanged = true;
1473 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001474 }
1475 };
1476
1477 if (!ctx) {
1478 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001479 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001480 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001481 prButton(GpuPathRenderers::kAll);
1482 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1483 prButton(GpuPathRenderers::kStencilAndCover);
1484 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001485 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001486 prButton(GpuPathRenderers::kNone);
1487 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001488 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001489 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001490 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1491 prButton(GpuPathRenderers::kCoverageCounting);
1492 }
Jim Van Verth83010462017-03-16 08:45:39 -04001493 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001494 prButton(GpuPathRenderers::kTessellating);
1495 prButton(GpuPathRenderers::kNone);
1496 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001497 ImGui::TreePop();
1498 }
Brian Osman621491e2017-02-28 15:45:01 -05001499 }
1500
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001501 if (ImGui::CollapsingHeader("Transform")) {
1502 float zoom = fZoomLevel;
1503 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1504 fZoomLevel = zoom;
1505 this->preTouchMatrixChanged();
1506 paramsChanged = true;
1507 }
1508 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001509 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001510 fRotation = deg;
1511 this->preTouchMatrixChanged();
1512 paramsChanged = true;
1513 }
Brian Osman805a7272018-05-02 15:40:20 -04001514 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1515 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1516 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001517 this->preTouchMatrixChanged();
Brian Osman805a7272018-05-02 15:40:20 -04001518 this->updateTitle();
Brian Osman9bb47cf2018-04-26 15:55:00 -04001519 }
1520 if (ImGui_DragQuad(fPerspectivePoints)) {
1521 this->preTouchMatrixChanged();
1522 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001523 }
1524
Ben Wagnera580fb32018-04-17 11:16:32 -04001525 if (ImGui::CollapsingHeader("Paint")) {
1526 int hintingIdx = 0;
1527 if (fPaintOverrides.fHinting) {
1528 hintingIdx = fPaint.getHinting() + 1;
1529 }
1530 if (ImGui::Combo("Hinting", &hintingIdx,
1531 "Default\0None\0Slight\0Normal\0Full\0\0"))
1532 {
1533 if (hintingIdx == 0) {
1534 fPaintOverrides.fHinting = false;
1535 fPaint.setHinting(SkPaint::kNo_Hinting);
1536 } else {
1537 fPaintOverrides.fHinting = true;
1538 SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1);
1539 fPaint.setHinting(hinting);
1540 }
1541 paramsChanged = true;
1542 }
1543
1544 int aliasIdx = 0;
1545 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1546 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1547 }
1548 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1549 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1550 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1551 {
1552 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1553 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1554 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1555 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1556 if (aliasIdx == 0) {
1557 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1558 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1559 } else {
1560 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1561 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1562 fPaint.setAntiAlias(aliasIdx > 1);
1563 switch (fPaintOverrides.fAntiAlias) {
1564 case SkPaintFields::AntiAliasState::Alias:
1565 break;
1566 case SkPaintFields::AntiAliasState::Normal:
1567 break;
1568 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1569 gSkUseAnalyticAA = true;
1570 gSkForceAnalyticAA = false;
1571 gSkUseDeltaAA = gSkForceDeltaAA = false;
1572 break;
1573 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1574 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1575 gSkUseDeltaAA = gSkForceDeltaAA = false;
1576 break;
1577 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1578 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1579 gSkUseDeltaAA = true;
1580 gSkForceDeltaAA = false;
1581 break;
1582 case SkPaintFields::AntiAliasState::DeltaAAForced:
1583 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1584 gSkUseDeltaAA = gSkForceDeltaAA = true;
1585 break;
1586 }
1587 }
1588 paramsChanged = true;
1589 }
1590
Ben Wagner99a78dc2018-05-09 18:23:51 -04001591 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
1592 SkPaint::Flags flag,
1593 bool (SkPaint::* isFlag)() const,
1594 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001595 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001596 int itemIndex = 0;
1597 if (fPaintOverrides.fFlags & flag) {
1598 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001599 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001600 if (ImGui::Combo(label, &itemIndex, items)) {
1601 if (itemIndex == 0) {
1602 fPaintOverrides.fFlags &= ~flag;
1603 } else {
1604 fPaintOverrides.fFlags |= flag;
1605 (fPaint.*setFlag)(itemIndex == 2);
1606 }
1607 paramsChanged = true;
1608 }
1609 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001610
Ben Wagner99a78dc2018-05-09 18:23:51 -04001611 paintFlag("Dither",
1612 "Default\0No Dither\0Dither\0\0",
1613 SkPaint::kDither_Flag,
1614 &SkPaint::isDither, &SkPaint::setDither);
1615
1616 paintFlag("Fake Bold Glyphs",
1617 "Default\0No Fake Bold\0Fake Bold\0\0",
1618 SkPaint::kFakeBoldText_Flag,
1619 &SkPaint::isFakeBoldText, &SkPaint::setFakeBoldText);
1620
1621 paintFlag("Linear Text",
1622 "Default\0No Linear Text\0Linear Text\0\0",
1623 SkPaint::kLinearText_Flag,
1624 &SkPaint::isLinearText, &SkPaint::setLinearText);
1625
1626 paintFlag("Subpixel Position Glyphs",
1627 "Default\0Pixel Text\0Subpixel Text\0\0",
1628 SkPaint::kSubpixelText_Flag,
1629 &SkPaint::isSubpixelText, &SkPaint::setSubpixelText);
1630
1631 paintFlag("Subpixel Anti-Alias",
1632 "Default\0lcd\0LCD\0\0",
1633 SkPaint::kLCDRenderText_Flag,
1634 &SkPaint::isLCDRenderText, &SkPaint::setLCDRenderText);
1635
1636 paintFlag("Embedded Bitmap Text",
1637 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1638 SkPaint::kEmbeddedBitmapText_Flag,
1639 &SkPaint::isEmbeddedBitmapText, &SkPaint::setEmbeddedBitmapText);
1640
1641 paintFlag("Force Auto-Hinting",
1642 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1643 SkPaint::kAutoHinting_Flag,
1644 &SkPaint::isAutohinted, &SkPaint::setAutohinted);
1645
1646 paintFlag("Vertical Text",
1647 "Default\0No Vertical Text\0Vertical Text\0\0",
1648 SkPaint::kVerticalText_Flag,
1649 &SkPaint::isVerticalText, &SkPaint::setVerticalText);
Ben Wagnera580fb32018-04-17 11:16:32 -04001650 }
1651
Ben Wagner7a3c6742018-04-23 10:01:07 -04001652 if (fShowSlidePicker) {
1653 ImGui::SetNextTreeNodeOpen(true);
1654 }
Brian Osman79086b92017-02-10 13:36:16 -05001655 if (ImGui::CollapsingHeader("Slide")) {
1656 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001657 static ImVector<const char*> filteredSlideNames;
1658 static ImVector<int> filteredSlideIndices;
1659
Brian Osmanfce09c52017-11-14 15:32:20 -05001660 if (fShowSlidePicker) {
1661 ImGui::SetKeyboardFocusHere();
1662 fShowSlidePicker = false;
1663 }
1664
Brian Osman79086b92017-02-10 13:36:16 -05001665 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001666 filteredSlideNames.clear();
1667 filteredSlideIndices.clear();
1668 int filteredIndex = 0;
1669 for (int i = 0; i < fSlides.count(); ++i) {
1670 const char* slideName = fSlides[i]->getName().c_str();
1671 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1672 if (i == fCurrentSlide) {
1673 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001674 }
Brian Osmanf479e422017-11-08 13:11:36 -05001675 filteredSlideNames.push_back(slideName);
1676 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001677 }
Brian Osman79086b92017-02-10 13:36:16 -05001678 }
Brian Osmanf479e422017-11-08 13:11:36 -05001679
Brian Osmanf479e422017-11-08 13:11:36 -05001680 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1681 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001682 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001683 }
1684 }
Brian Osmana109e392017-02-24 09:49:14 -05001685
1686 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001687 ColorMode newMode = fColorMode;
1688 auto cmButton = [&](ColorMode mode, const char* label) {
1689 if (ImGui::RadioButton(label, mode == fColorMode)) {
1690 newMode = mode;
1691 }
1692 };
1693
1694 cmButton(ColorMode::kLegacy, "Legacy 8888");
1695 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1696 "Color Managed 8888 (Nonlinear blending)");
1697 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1698 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1699
1700 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001701 // It isn't safe to switch color mode now (in the middle of painting). We might
1702 // tear down the back-end, etc... Defer this change until the next onIdle.
1703 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001704 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001705 });
Brian Osmana109e392017-02-24 09:49:14 -05001706 }
1707
1708 // Pick from common gamuts:
1709 int primariesIdx = 4; // Default: Custom
1710 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1711 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1712 primariesIdx = i;
1713 break;
1714 }
1715 }
1716
Brian Osmanfdab5762017-11-09 10:27:55 -05001717 // When we're in xform canvas mode, we can alter the transfer function, too
1718 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1719 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1720 }
1721
Brian Osmana109e392017-02-24 09:49:14 -05001722 if (ImGui::Combo("Primaries", &primariesIdx,
1723 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1724 if (primariesIdx >= 0 && primariesIdx <= 3) {
1725 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1726 }
1727 }
1728
1729 // Allow direct editing of gamut
1730 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1731 }
Brian Osman79086b92017-02-10 13:36:16 -05001732 }
Brian Salomon99a33902017-03-07 15:16:34 -05001733 if (paramsChanged) {
1734 fDeferredActions.push_back([=]() {
1735 fWindow->setRequestedDisplayParams(params);
1736 fWindow->inval();
1737 this->updateTitle();
1738 });
1739 }
Brian Osman79086b92017-02-10 13:36:16 -05001740 ImGui::End();
1741 }
1742
Brian Osmanf6877092017-02-13 09:39:57 -05001743 if (fShowZoomWindow && fLastImage) {
1744 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001745 static int zoomFactor = 8;
1746 if (ImGui::Button("<<")) {
1747 zoomFactor = SkTMax(zoomFactor / 2, 4);
1748 }
1749 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1750 if (ImGui::Button(">>")) {
1751 zoomFactor = SkTMin(zoomFactor * 2, 32);
1752 }
Brian Osmanf6877092017-02-13 09:39:57 -05001753
Brian Osmanf6877092017-02-13 09:39:57 -05001754 ImVec2 mousePos = ImGui::GetMousePos();
1755 ImVec2 avail = ImGui::GetContentRegionAvail();
1756
Brian Osmanead517d2017-11-13 15:36:36 -05001757 uint32_t pixel = 0;
1758 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1759 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1760 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001761 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1762 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1763 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001764 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1765 }
1766
Brian Osmand67e5182017-12-08 16:46:09 -05001767 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001768 // Translate so the region of the image that's under the mouse cursor is centered
1769 // in the zoom canvas:
1770 c->scale(zoomFactor, zoomFactor);
1771 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1772 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1773 c->drawImage(this->fLastImage, 0, 0);
1774
1775 SkPaint outline;
1776 outline.setStyle(SkPaint::kStroke_Style);
1777 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1778 });
Brian Osmanf6877092017-02-13 09:39:57 -05001779 }
1780
1781 ImGui::End();
1782 }
Brian Osman79086b92017-02-10 13:36:16 -05001783}
1784
liyuqian2edb0f42016-07-06 14:11:32 -07001785void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001786 for (int i = 0; i < fDeferredActions.count(); ++i) {
1787 fDeferredActions[i]();
1788 }
1789 fDeferredActions.reset();
1790
Brian Osman56a24812017-12-19 11:15:16 -05001791 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001792 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001793 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001794 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001795
Brian Osman79086b92017-02-10 13:36:16 -05001796 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001797 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001798 fWindow->inval();
1799 }
jvanverth9f372462016-04-06 06:08:59 -07001800}
liyuqiane5a6cd92016-05-27 08:52:52 -07001801
1802void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001803 if (!fWindow) {
1804 return;
1805 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001806 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001807 return; // Surface hasn't been created yet.
1808 }
1809
liyuqianb73c24b2016-06-03 08:47:23 -07001810 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001811 Json::Value slideState(Json::objectValue);
1812 slideState[kName] = kSlideStateName;
1813 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001814 if (fAllSlideNames.size() == 0) {
1815 for(auto slide : fSlides) {
1816 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1817 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001818 }
liyuqian1f508fd2016-06-07 06:57:40 -07001819 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001820
liyuqianb73c24b2016-06-03 08:47:23 -07001821 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001822 Json::Value backendState(Json::objectValue);
1823 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001824 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001825 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001826 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001827 backendState[kOptions].append(Json::Value(str));
1828 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001829
csmartdalton578f0642017-02-24 16:04:47 -07001830 // MSAA state
1831 Json::Value msaaState(Json::objectValue);
1832 msaaState[kName] = kMSAAStateName;
1833 msaaState[kValue] = fWindow->sampleCount();
1834 msaaState[kOptions] = Json::Value(Json::arrayValue);
1835 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1836 msaaState[kOptions].append(Json::Value(0));
1837 } else {
1838 for (int msaa : {0, 4, 8, 16}) {
1839 msaaState[kOptions].append(Json::Value(msaa));
1840 }
1841 }
1842
csmartdalton61cd31a2017-02-27 17:00:53 -07001843 // Path renderer state
1844 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1845 Json::Value prState(Json::objectValue);
1846 prState[kName] = kPathRendererStateName;
1847 prState[kValue] = gPathRendererNames[pr];
1848 prState[kOptions] = Json::Value(Json::arrayValue);
1849 const GrContext* ctx = fWindow->getGrContext();
1850 if (!ctx) {
1851 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001852 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001853 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001854 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1855 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1856 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1857 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001858 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001859 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1860 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001861 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001862 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001863 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1864 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1865 }
Jim Van Verth83010462017-03-16 08:45:39 -04001866 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001867 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001868 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1869 }
1870
liyuqianb73c24b2016-06-03 08:47:23 -07001871 // Softkey state
1872 Json::Value softkeyState(Json::objectValue);
1873 softkeyState[kName] = kSoftkeyStateName;
1874 softkeyState[kValue] = kSoftkeyHint;
1875 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1876 softkeyState[kOptions].append(kSoftkeyHint);
1877 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1878 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1879 }
1880
liyuqiane5a6cd92016-05-27 08:52:52 -07001881 Json::Value state(Json::arrayValue);
1882 state.append(slideState);
1883 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001884 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001885 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001886 state.append(softkeyState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001887
Brian Osmaneff04b52017-11-21 13:18:02 -05001888 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001889}
1890
1891void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001892 // For those who will add more features to handle the state change in this function:
1893 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1894 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1895 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001896 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001897 for (int i = 0; i < fSlides.count(); ++i) {
1898 if (fSlides[i]->getName().equals(stateValue)) {
1899 this->setCurrentSlide(i);
1900 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001901 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001902 }
Florin Malitaab99c342018-01-16 16:23:03 -05001903
1904 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001905 } else if (stateName.equals(kBackendStateName)) {
1906 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1907 if (stateValue.equals(kBackendTypeStrings[i])) {
1908 if (fBackendType != i) {
1909 fBackendType = (sk_app::Window::BackendType)i;
1910 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001911 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001912 }
1913 break;
1914 }
1915 }
csmartdalton578f0642017-02-24 16:04:47 -07001916 } else if (stateName.equals(kMSAAStateName)) {
1917 DisplayParams params = fWindow->getRequestedDisplayParams();
1918 int sampleCount = atoi(stateValue.c_str());
1919 if (sampleCount != params.fMSAASampleCount) {
1920 params.fMSAASampleCount = sampleCount;
1921 fWindow->setRequestedDisplayParams(params);
1922 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001923 this->updateTitle();
1924 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001925 }
1926 } else if (stateName.equals(kPathRendererStateName)) {
1927 DisplayParams params = fWindow->getRequestedDisplayParams();
1928 for (const auto& pair : gPathRendererNames) {
1929 if (pair.second == stateValue.c_str()) {
1930 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1931 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1932 fWindow->setRequestedDisplayParams(params);
1933 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001934 this->updateTitle();
1935 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001936 }
1937 break;
1938 }
csmartdalton578f0642017-02-24 16:04:47 -07001939 }
liyuqianb73c24b2016-06-03 08:47:23 -07001940 } else if (stateName.equals(kSoftkeyStateName)) {
1941 if (!stateValue.equals(kSoftkeyHint)) {
1942 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001943 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001944 }
liyuqian2edb0f42016-07-06 14:11:32 -07001945 } else if (stateName.equals(kRefreshStateName)) {
1946 // This state is actually NOT in the UI state.
1947 // We use this to allow Android to quickly set bool fRefresh.
1948 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001949 } else {
1950 SkDebugf("Unknown stateName: %s", stateName.c_str());
1951 }
1952}
Brian Osman79086b92017-02-10 13:36:16 -05001953
1954bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001955 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001956}
1957
1958bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001959 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001960 fWindow->inval();
1961 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001962 } else {
1963 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001964 }
Brian Osman79086b92017-02-10 13:36:16 -05001965}