blob: cfb8c61adde32b22c4b5d0116093d5669c767f71 [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"
Brian Salomonc7fe0f72018-05-11 10:14:21 -04009#include <stdlib.h>
10#include <map>
Chris Dalton2d18f412018-02-20 13:23:32 -070011#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070012#include "GMSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040013#include "GrContext.h"
14#include "GrContextPriv.h"
liyuqian6f163d22016-06-13 12:26:45 -070015#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050016#include "Resources.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070017#include "SKPSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040018#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070019#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050020#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040021#include "SkColorSpaceXformCanvas.h"
Brian Osman2dd96932016-10-18 15:33:53 -040022#include "SkCommandLineFlags.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040023#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070024#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040025#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050026#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040027#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050028#include "SkImagePriv.h"
Herb Derbyefe39bc2018-05-01 17:06:20 -040029#include "SkMakeUnique.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070030#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050031#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050032#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050033#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040034#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070035#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070036#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050037#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050038#include "SkTestFontMgr.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050039#include "SkThreadedBMPDevice.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040040#include "SlideDir.h"
41#include "SvgSlide.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060042#include "ccpr/GrCoverageCountingPathRenderer.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040043#include "imgui.h"
csmartdalton578f0642017-02-24 16:04:47 -070044
Florin Malita87ccf332018-05-04 12:23:24 -040045#if defined(SK_ENABLE_SKOTTIE)
46 #include "SkottieSlide.h"
47#endif
48
jvanverth34524262016-05-04 13:49:13 -070049using namespace sk_app;
50
csmartdalton61cd31a2017-02-27 17:00:53 -070051static std::map<GpuPathRenderers, std::string> gPathRendererNames;
52
jvanverth9f372462016-04-06 06:08:59 -070053Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070054 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070055}
56
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060057static DEFINE_string(slide, "", "Start on this sample.");
58static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050059
bsalomon6c471f72016-07-26 12:56:32 -070060#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070061# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070062#else
63# define BACKENDS_STR "\"sw\" and \"gl\""
64#endif
65
Brian Osman2dd96932016-10-18 15:33:53 -040066static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070067
Brian Salomonbdecacf2018-02-02 20:32:49 -050068static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070069
Chris Dalton2d18f412018-02-20 13:23:32 -070070DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
71
Brian Osman53136aa2017-07-20 15:43:35 -040072DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050073
Florin Malita38792ce2018-05-08 10:36:18 -040074DEFINE_string2(file, f, "", "Open a single file for viewing.");
75
Brian Salomon194db172017-08-17 14:37:06 -040076const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070077 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040078#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
79 "ANGLE",
80#endif
jvanverth063ece72016-06-17 09:29:14 -070081#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070082 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070083#endif
csmartdalton578f0642017-02-24 16:04:47 -070084 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070085};
86
bsalomon6c471f72016-07-26 12:56:32 -070087static sk_app::Window::BackendType get_backend_type(const char* str) {
88#ifdef SK_VULKAN
89 if (0 == strcmp(str, "vk")) {
90 return sk_app::Window::kVulkan_BackendType;
91 } else
92#endif
Brian Salomon194db172017-08-17 14:37:06 -040093#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
94 if (0 == strcmp(str, "angle")) {
95 return sk_app::Window::kANGLE_BackendType;
96 } else
97#endif
bsalomon6c471f72016-07-26 12:56:32 -070098 if (0 == strcmp(str, "gl")) {
99 return sk_app::Window::kNativeGL_BackendType;
100 } else if (0 == strcmp(str, "sw")) {
101 return sk_app::Window::kRaster_BackendType;
102 } else {
103 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
104 return sk_app::Window::kRaster_BackendType;
105 }
106}
107
Brian Osmana109e392017-02-24 09:49:14 -0500108static SkColorSpacePrimaries gSrgbPrimaries = {
109 0.64f, 0.33f,
110 0.30f, 0.60f,
111 0.15f, 0.06f,
112 0.3127f, 0.3290f };
113
114static SkColorSpacePrimaries gAdobePrimaries = {
115 0.64f, 0.33f,
116 0.21f, 0.71f,
117 0.15f, 0.06f,
118 0.3127f, 0.3290f };
119
120static SkColorSpacePrimaries gP3Primaries = {
121 0.680f, 0.320f,
122 0.265f, 0.690f,
123 0.150f, 0.060f,
124 0.3127f, 0.3290f };
125
126static SkColorSpacePrimaries gRec2020Primaries = {
127 0.708f, 0.292f,
128 0.170f, 0.797f,
129 0.131f, 0.046f,
130 0.3127f, 0.3290f };
131
132struct NamedPrimaries {
133 const char* fName;
134 SkColorSpacePrimaries* fPrimaries;
135} gNamedPrimaries[] = {
136 { "sRGB", &gSrgbPrimaries },
137 { "AdobeRGB", &gAdobePrimaries },
138 { "P3", &gP3Primaries },
139 { "Rec. 2020", &gRec2020Primaries },
140};
141
142static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
143 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
144}
145
Brian Osman70d2f432017-11-08 09:54:10 -0500146static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
147 // In raster mode, we still use GL for the window.
148 // This lets us render the GUI faster (and correct).
149 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
150}
151
liyuqiane5a6cd92016-05-27 08:52:52 -0700152const char* kName = "name";
153const char* kValue = "value";
154const char* kOptions = "options";
155const char* kSlideStateName = "Slide";
156const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700157const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700158const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700159const char* kSoftkeyStateName = "Softkey";
160const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700161const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700162const char* kON = "ON";
163const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700164const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700165
jvanverth34524262016-05-04 13:49:13 -0700166Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500167 : fCurrentSlide(-1)
168 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500169 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500170 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500171 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500172 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500173 , fShowZoomWindow(false)
174 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700175 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500176 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500177 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500178 // Our UI can only tweak gamma (currently), so start out gamma-only
179 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700180 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400181 , fRotation(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400182 , fGestureDevice(GestureDevice::kNone)
Brian Osman805a7272018-05-02 15:40:20 -0400183 , fPerspectiveMode(kPerspective_Off)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500184 , fTileCnt(0)
185 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700186{
Greg Daniel285db442016-10-14 09:12:53 -0400187 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700188
Brian Osmanf09e35e2017-12-15 14:48:09 -0500189 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
190 gPathRendererNames[GpuPathRenderers::kDefault] =
191 "Default Ganesh Behavior (best path renderer, not including CCPR)";
192 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500193 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
194 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
195 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
196 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700197
jvanverth2bb3b6d2016-04-08 07:24:09 -0700198 SkDebugf("Command line arguments: ");
199 for (int i = 1; i < argc; ++i) {
200 SkDebugf("%s ", argv[i]);
201 }
202 SkDebugf("\n");
203
204 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500205#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400206 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500207#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700208
Ben Wagner483c7722018-02-20 17:06:07 -0500209 if (!FLAGS_nativeFonts) {
210 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
211 }
212
Brian Osmanbc8150f2017-07-24 11:38:01 -0400213 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400214 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400215
bsalomon6c471f72016-07-26 12:56:32 -0700216 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700217 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700218
csmartdalton578f0642017-02-24 16:04:47 -0700219 DisplayParams displayParams;
220 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700221 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700222 fWindow->setRequestedDisplayParams(displayParams);
223
Brian Osman56a24812017-12-19 11:15:16 -0500224 // Configure timers
225 fStatsLayer.setActive(false);
226 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
227 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
228 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
229
jvanverth9f372462016-04-06 06:08:59 -0700230 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700231 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500232 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500233 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500234 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700235
brianosman622c8d52016-05-10 06:50:49 -0700236 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500237 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
238 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
239 fWindow->inval();
240 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500241 // Command to jump directly to the slide picker and give it focus
242 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
243 this->fShowImGuiDebugWindow = true;
244 this->fShowSlidePicker = true;
245 fWindow->inval();
246 });
247 // Alias that to Backspace, to match SampleApp
248 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
249 this->fShowImGuiDebugWindow = true;
250 this->fShowSlidePicker = true;
251 fWindow->inval();
252 });
Brian Osman79086b92017-02-10 13:36:16 -0500253 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
254 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
255 fWindow->inval();
256 });
Brian Osmanf6877092017-02-13 09:39:57 -0500257 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
258 this->fShowZoomWindow = !this->fShowZoomWindow;
259 fWindow->inval();
260 });
brianosman622c8d52016-05-10 06:50:49 -0700261 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500262 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700263 fWindow->inval();
264 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400265 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500266 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400267 this->updateTitle();
268 fWindow->inval();
269 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500270 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500271 switch (fColorMode) {
272 case ColorMode::kLegacy:
273 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
274 break;
275 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
276 this->setColorMode(ColorMode::kColorManagedSRGB8888);
277 break;
278 case ColorMode::kColorManagedSRGB8888:
279 this->setColorMode(ColorMode::kColorManagedLinearF16);
280 break;
281 case ColorMode::kColorManagedLinearF16:
282 this->setColorMode(ColorMode::kLegacy);
283 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500284 }
brianosman622c8d52016-05-10 06:50:49 -0700285 });
286 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500287 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700288 });
289 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500290 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700291 });
292 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
293 this->changeZoomLevel(1.f / 32.f);
294 fWindow->inval();
295 });
296 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
297 this->changeZoomLevel(-1.f / 32.f);
298 fWindow->inval();
299 });
jvanverthaf236b52016-05-20 06:01:06 -0700300 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400301 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
302 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500303 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400304#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
305 if (newBackend == sk_app::Window::kVulkan_BackendType) {
306 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
307 sk_app::Window::kBackendTypeCount);
308 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
309 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500310 }
311#endif
Brian Osman621491e2017-02-28 15:45:01 -0500312 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700313 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500314 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
315 fTileCnt++;
316 if (fThreadCnt == 0) {
317 this->resetExecutor();
318 }
319 this->updateTitle();
320 fWindow->inval();
321 });
322 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
323 fTileCnt = SkTMax(0, fTileCnt - 1);
324 if (fThreadCnt == 0) {
325 this->resetExecutor();
326 }
327 this->updateTitle();
328 fWindow->inval();
329 });
330 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
331 if (fTileCnt == 0) {
332 return;
333 }
334 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
335 this->resetExecutor();
336 this->updateTitle();
337 fWindow->inval();
338 });
339 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
340 if (fTileCnt == 0) {
341 return;
342 }
343 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
344 this->resetExecutor();
345 this->updateTitle();
346 fWindow->inval();
347 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500348 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
349 fSaveToSKP = true;
350 fWindow->inval();
351 });
Ben Wagner37c54032018-04-13 14:30:23 -0400352 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
353 DisplayParams params = fWindow->getRequestedDisplayParams();
354 uint32_t flags = params.fSurfaceProps.flags();
355 if (!fPixelGeometryOverrides) {
356 fPixelGeometryOverrides = true;
357 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
358 } else {
359 switch (params.fSurfaceProps.pixelGeometry()) {
360 case kUnknown_SkPixelGeometry:
361 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
362 break;
363 case kRGB_H_SkPixelGeometry:
364 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
365 break;
366 case kBGR_H_SkPixelGeometry:
367 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
368 break;
369 case kRGB_V_SkPixelGeometry:
370 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
371 break;
372 case kBGR_V_SkPixelGeometry:
373 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
374 fPixelGeometryOverrides = false;
375 break;
376 }
377 }
378 fWindow->setRequestedDisplayParams(params);
379 this->updateTitle();
380 fWindow->inval();
381 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500382 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
383 if (!fPaintOverrides.fHinting) {
384 fPaintOverrides.fHinting = true;
385 fPaint.setHinting(SkPaint::kNo_Hinting);
386 } else {
387 switch (fPaint.getHinting()) {
388 case SkPaint::kNo_Hinting:
389 fPaint.setHinting(SkPaint::kSlight_Hinting);
390 break;
391 case SkPaint::kSlight_Hinting:
392 fPaint.setHinting(SkPaint::kNormal_Hinting);
393 break;
394 case SkPaint::kNormal_Hinting:
395 fPaint.setHinting(SkPaint::kFull_Hinting);
396 break;
397 case SkPaint::kFull_Hinting:
398 fPaint.setHinting(SkPaint::kNo_Hinting);
399 fPaintOverrides.fHinting = false;
400 break;
401 }
402 }
403 this->updateTitle();
404 fWindow->inval();
405 });
406 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
407 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
408 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
409 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
410 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500411 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
412 gSkUseDeltaAA = gSkForceDeltaAA = false;
413 } else {
414 fPaint.setAntiAlias(true);
415 switch (fPaintOverrides.fAntiAlias) {
416 case SkPaintFields::AntiAliasState::Alias:
417 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400418 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
419 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500420 break;
421 case SkPaintFields::AntiAliasState::Normal:
422 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500423 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400424 gSkForceAnalyticAA = false;
425 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500426 break;
427 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
428 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400429 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
430 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500431 break;
432 case SkPaintFields::AntiAliasState::AnalyticAAForced:
433 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
434 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
435 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400436 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500437 break;
438 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
439 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400440 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
441 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500442 break;
443 case SkPaintFields::AntiAliasState::DeltaAAForced:
444 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
445 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
446 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
447 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
448 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
449 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
450 break;
451 }
452 }
453 this->updateTitle();
454 fWindow->inval();
455 });
Ben Wagner37c54032018-04-13 14:30:23 -0400456 fCommands.addCommand('D', "Modes", "DFT", [this]() {
457 DisplayParams params = fWindow->getRequestedDisplayParams();
458 uint32_t flags = params.fSurfaceProps.flags();
459 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
460 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
461 fWindow->setRequestedDisplayParams(params);
462 this->updateTitle();
463 fWindow->inval();
464 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500465 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
466 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
467 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
468 fPaint.setLCDRenderText(false);
469 } else {
470 if (!fPaint.isLCDRenderText()) {
471 fPaint.setLCDRenderText(true);
472 } else {
473 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
474 }
475 }
476 this->updateTitle();
477 fWindow->inval();
478 });
479 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
480 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
481 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
482 fPaint.setSubpixelText(false);
483 } else {
484 if (!fPaint.isSubpixelText()) {
485 fPaint.setSubpixelText(true);
486 } else {
487 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
488 }
489 }
490 this->updateTitle();
491 fWindow->inval();
492 });
Brian Osman805a7272018-05-02 15:40:20 -0400493 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
494 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
495 : kPerspective_Real;
496 this->updateTitle();
497 fWindow->inval();
498 });
499 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
500 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
501 : kPerspective_Off;
502 this->updateTitle();
503 fWindow->inval();
504 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500505
jvanverth2bb3b6d2016-04-08 07:24:09 -0700506 // set up slides
507 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500508 if (FLAGS_list) {
509 this->listNames();
510 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700511
Brian Osman9bb47cf2018-04-26 15:55:00 -0400512 fPerspectivePoints[0].set(0, 0);
513 fPerspectivePoints[1].set(1, 0);
514 fPerspectivePoints[2].set(0, 1);
515 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700516 fAnimTimer.run();
517
Hal Canaryc465d132017-12-08 10:21:31 -0500518 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500519 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400520 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500521 }
522 fImGuiGamutPaint.setColor(SK_ColorWHITE);
523 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
524
Brian Osman70d2f432017-11-08 09:54:10 -0500525 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500526 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700527}
528
jvanverth34524262016-05-04 13:49:13 -0700529void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700530 fAllSlideNames = Json::Value(Json::arrayValue);
531
Florin Malita0ffa3222018-04-05 14:34:45 -0400532 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
533 static const struct {
534 const char* fExtension;
535 const char* fDirName;
536 const SkCommandLineFlags::StringArray& fFlags;
537 const SlideFactory fFactory;
538 } gExternalSlidesInfo[] = {
539 { ".skp", "skp-dir", FLAGS_skps,
540 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
541 return sk_make_sp<SKPSlide>(name, path);}
542 },
543 { ".jpg", "jpg-dir", FLAGS_jpgs,
544 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
545 return sk_make_sp<ImageSlide>(name, path);}
546 },
Florin Malita87ccf332018-05-04 12:23:24 -0400547#if defined(SK_ENABLE_SKOTTIE)
Florin Malita0ffa3222018-04-05 14:34:45 -0400548 { ".json", "skottie-dir", FLAGS_jsons,
549 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
550 return sk_make_sp<SkottieSlide>(name, path);}
551 },
Florin Malita87ccf332018-05-04 12:23:24 -0400552#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400553 { ".svg", "svg-dir", FLAGS_svgs,
554 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
555 return sk_make_sp<SvgSlide>(name, path);}
556 },
557 };
jvanverthc265a922016-04-08 12:51:45 -0700558
Florin Malita0ffa3222018-04-05 14:34:45 -0400559 SkTArray<sk_sp<Slide>, true> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700560
Florin Malita0ffa3222018-04-05 14:34:45 -0400561 const auto addSlide = [&](const SkString& name,
562 const SkString& path,
563 const SlideFactory& fact) {
564 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
565 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700566 }
liyuqian6f163d22016-06-13 12:26:45 -0700567
Florin Malita0ffa3222018-04-05 14:34:45 -0400568 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500569 dirSlides.push_back(slide);
570 fSlides.push_back(std::move(slide));
571 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400572 };
Florin Malita76a076b2018-02-15 18:40:48 -0500573
Florin Malita38792ce2018-05-08 10:36:18 -0400574 if (!FLAGS_file.isEmpty()) {
575 // single file mode
576 const SkString file(FLAGS_file[0]);
577
578 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
579 for (const auto& sinfo : gExternalSlidesInfo) {
580 if (file.endsWith(sinfo.fExtension)) {
581 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
582 return;
583 }
584 }
585
586 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
587 } else {
588 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
589 }
590
591 return;
592 }
593
594 // Bisect slide.
595 if (!FLAGS_bisect.isEmpty()) {
596 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
597 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
598 if (FLAGS_bisect.count() >= 2) {
599 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
600 bisect->onChar(*ch);
601 }
602 }
603 fSlides.push_back(std::move(bisect));
604 }
605 }
606
607 // GMs
608 int firstGM = fSlides.count();
609 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
610 while (gms) {
611 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
612
613 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
614 sk_sp<Slide> slide(new GMSlide(gm.release()));
615 fSlides.push_back(std::move(slide));
616 }
617
618 gms = gms->next();
619 }
620 // reverse gms
621 int numGMs = fSlides.count() - firstGM;
622 for (int i = 0; i < numGMs/2; ++i) {
623 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
624 }
625
626 // samples
627 const SkViewRegister* reg = SkViewRegister::Head();
628 while (reg) {
629 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
630 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
631 fSlides.push_back(slide);
632 }
633 reg = reg->next();
634 }
635
Florin Malita0ffa3222018-04-05 14:34:45 -0400636 for (const auto& info : gExternalSlidesInfo) {
637 for (const auto& flag : info.fFlags) {
638 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
639 // single file
640 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
641 } else {
642 // directory
643 SkOSFile::Iter it(flag.c_str(), info.fExtension);
644 SkString name;
645 while (it.next(&name)) {
646 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
647 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400648 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400649
650 if (!dirSlides.empty()) {
651 fSlides.push_back(
652 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
653 std::move(dirSlides)));
654 dirSlides.reset();
655 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400656 }
657 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700658}
659
660
jvanverth34524262016-05-04 13:49:13 -0700661Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700662 fWindow->detach();
663 delete fWindow;
664}
665
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500666struct SkPaintTitleUpdater {
667 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
668 void append(const char* s) {
669 if (fCount == 0) {
670 fTitle->append(" {");
671 } else {
672 fTitle->append(", ");
673 }
674 fTitle->append(s);
675 ++fCount;
676 }
677 void done() {
678 if (fCount > 0) {
679 fTitle->append("}");
680 }
681 }
682 SkString* fTitle;
683 int fCount;
684};
685
brianosman05de2162016-05-06 13:28:57 -0700686void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700687 if (!fWindow) {
688 return;
689 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500690 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700691 return; // Surface hasn't been created yet.
692 }
693
jvanverth34524262016-05-04 13:49:13 -0700694 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700695 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700696
Yuqian Li399b3c22017-08-03 11:08:15 -0400697 if (gSkUseDeltaAA) {
698 if (gSkForceDeltaAA) {
699 title.append(" <FDAA>");
700 } else {
701 title.append(" <DAA>");
702 }
703 } else if (gSkUseAnalyticAA) {
704 if (gSkForceAnalyticAA) {
705 title.append(" <FAAA>");
706 } else {
707 title.append(" <AAA>");
708 }
709 }
710
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500711 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner99a78dc2018-05-09 18:23:51 -0400712 auto paintFlag = [this, &paintTitle](SkPaint::Flags flag, bool (SkPaint::* isFlag)() const,
713 const char* on, const char* off)
714 {
715 if (fPaintOverrides.fFlags & flag) {
716 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500717 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400718 };
719
720 paintFlag(SkPaint::kAntiAlias_Flag, &SkPaint::isAntiAlias, "Antialias", "Alias");
721 paintFlag(SkPaint::kDither_Flag, &SkPaint::isDither, "DITHER", "No Dither");
722 paintFlag(SkPaint::kFakeBoldText_Flag, &SkPaint::isFakeBoldText, "Fake Bold", "No Fake Bold");
723 paintFlag(SkPaint::kLinearText_Flag, &SkPaint::isLinearText, "Linear Text", "Non-Linear Text");
724 paintFlag(SkPaint::kSubpixelText_Flag, &SkPaint::isSubpixelText, "Subpixel Text", "Pixel Text");
725 paintFlag(SkPaint::kLCDRenderText_Flag, &SkPaint::isLCDRenderText, "LCD", "lcd");
726 paintFlag(SkPaint::kEmbeddedBitmapText_Flag, &SkPaint::isEmbeddedBitmapText,
727 "Bitmap Text", "No Bitmap Text");
728 paintFlag(SkPaint::kAutoHinting_Flag, &SkPaint::isAutohinted,
729 "Force Autohint", "No Force Autohint");
730 paintFlag(SkPaint::kVerticalText_Flag, &SkPaint::isVerticalText,
731 "Vertical Text", "No Vertical Text");
732
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500733 if (fPaintOverrides.fHinting) {
734 switch (fPaint.getHinting()) {
735 case SkPaint::kNo_Hinting:
736 paintTitle.append("No Hinting");
737 break;
738 case SkPaint::kSlight_Hinting:
739 paintTitle.append("Slight Hinting");
740 break;
741 case SkPaint::kNormal_Hinting:
742 paintTitle.append("Normal Hinting");
743 break;
744 case SkPaint::kFull_Hinting:
745 paintTitle.append("Full Hinting");
746 break;
747 }
748 }
749 paintTitle.done();
750
Yuqian Lib2ba6642017-11-22 12:07:41 -0500751 if (fTileCnt > 0) {
752 title.appendf(" T%d", fTileCnt);
753 if (fThreadCnt > 0) {
754 title.appendf("/%d", fThreadCnt);
755 }
756 }
757
Brian Osman92004802017-03-06 11:47:26 -0500758 switch (fColorMode) {
759 case ColorMode::kLegacy:
760 title.append(" Legacy 8888");
761 break;
762 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
763 title.append(" ColorManaged 8888 (Nonlinear blending)");
764 break;
765 case ColorMode::kColorManagedSRGB8888:
766 title.append(" ColorManaged 8888");
767 break;
768 case ColorMode::kColorManagedLinearF16:
769 title.append(" ColorManaged F16");
770 break;
771 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500772
Brian Osman92004802017-03-06 11:47:26 -0500773 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500774 int curPrimaries = -1;
775 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
776 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
777 curPrimaries = i;
778 break;
779 }
780 }
781 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500782
783 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
784 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
785 }
brianosman05de2162016-05-06 13:28:57 -0700786 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500787
Ben Wagner37c54032018-04-13 14:30:23 -0400788 const DisplayParams& params = fWindow->getRequestedDisplayParams();
789 if (fPixelGeometryOverrides) {
790 switch (params.fSurfaceProps.pixelGeometry()) {
791 case kUnknown_SkPixelGeometry:
792 title.append( " Flat");
793 break;
794 case kRGB_H_SkPixelGeometry:
795 title.append( " RGB");
796 break;
797 case kBGR_H_SkPixelGeometry:
798 title.append( " BGR");
799 break;
800 case kRGB_V_SkPixelGeometry:
801 title.append( " RGBV");
802 break;
803 case kBGR_V_SkPixelGeometry:
804 title.append( " BGRV");
805 break;
806 }
807 }
808
809 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
810 title.append(" DFT");
811 }
812
csmartdalton578f0642017-02-24 16:04:47 -0700813 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700814 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500815 int msaa = fWindow->sampleCount();
816 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700817 title.appendf(" MSAA: %i", msaa);
818 }
819 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700820
821 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400822 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700823 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
824 }
825
Brian Osman805a7272018-05-02 15:40:20 -0400826 if (kPerspective_Real == fPerspectiveMode) {
827 title.append(" Perpsective (Real)");
828 } else if (kPerspective_Fake == fPerspectiveMode) {
829 title.append(" Perspective (Fake)");
830 }
831
brianosman05de2162016-05-06 13:28:57 -0700832 fWindow->setTitle(title.c_str());
833}
834
Florin Malitaab99c342018-01-16 16:23:03 -0500835int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500836
837 if (!FLAGS_slide.isEmpty()) {
838 int count = fSlides.count();
839 for (int i = 0; i < count; i++) {
840 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500841 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500842 }
843 }
844
845 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
846 this->listNames();
847 }
848
Florin Malitaab99c342018-01-16 16:23:03 -0500849 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500850}
851
Florin Malitaab99c342018-01-16 16:23:03 -0500852void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500853 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500854 for (const auto& slide : fSlides) {
855 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500856 }
857}
858
Florin Malitaab99c342018-01-16 16:23:03 -0500859void Viewer::setCurrentSlide(int slide) {
860 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700861
Florin Malitaab99c342018-01-16 16:23:03 -0500862 if (slide == fCurrentSlide) {
863 return;
864 }
865
866 if (fCurrentSlide >= 0) {
867 fSlides[fCurrentSlide]->unload();
868 }
869
870 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
871 SkIntToScalar(fWindow->height()));
872 fCurrentSlide = slide;
873 this->setupCurrentSlide();
874}
875
876void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500877 if (fCurrentSlide >= 0) {
878 // prepare dimensions for image slides
879 fGesture.resetTouchState();
880 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700881
Jim Van Verth0848fb02018-01-22 13:39:30 -0500882 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
883 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
884 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400885
Jim Van Verth0848fb02018-01-22 13:39:30 -0500886 // Start with a matrix that scales the slide to the available screen space
887 if (fWindow->scaleContentToFit()) {
888 if (windowRect.width() > 0 && windowRect.height() > 0) {
889 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
890 }
liyuqiane46e4f02016-05-20 07:32:19 -0700891 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500892
893 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400894 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500895
896 this->updateTitle();
897 this->updateUIState();
898
899 fStatsLayer.resetMeasurements();
900
901 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700902 }
jvanverthc265a922016-04-08 12:51:45 -0700903}
904
905#define MAX_ZOOM_LEVEL 8
906#define MIN_ZOOM_LEVEL -8
907
jvanverth34524262016-05-04 13:49:13 -0700908void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700909 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400910 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400911 this->preTouchMatrixChanged();
912}
Yuqian Li755778c2018-03-28 16:23:31 -0400913
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400914void Viewer::preTouchMatrixChanged() {
915 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400916 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
917 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
918 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
919 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
920}
921
Brian Osman805a7272018-05-02 15:40:20 -0400922SkMatrix Viewer::computePerspectiveMatrix() {
923 SkScalar w = fWindow->width(), h = fWindow->height();
924 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
925 SkPoint perspPts[4] = {
926 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
927 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
928 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
929 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
930 };
931 SkMatrix m;
932 m.setPolyToPoly(orthoPts, perspPts, 4);
933 return m;
934}
935
Yuqian Li755778c2018-03-28 16:23:31 -0400936SkMatrix Viewer::computePreTouchMatrix() {
937 SkMatrix m = fDefaultMatrix;
938 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
939 : SK_Scalar1 + fZoomLevel;
940 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400941
942 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
943 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400944
Brian Osman805a7272018-05-02 15:40:20 -0400945 if (kPerspective_Real == fPerspectiveMode) {
946 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400947 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400948 }
949
Yuqian Li755778c2018-03-28 16:23:31 -0400950 return m;
jvanverthc265a922016-04-08 12:51:45 -0700951}
952
liyuqiand3cdbca2016-05-17 12:44:20 -0700953SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400954 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700955 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400956 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700957 return m;
jvanverthc265a922016-04-08 12:51:45 -0700958}
959
Brian Osman621491e2017-02-28 15:45:01 -0500960void Viewer::setBackend(sk_app::Window::BackendType backendType) {
961 fBackendType = backendType;
962
963 fWindow->detach();
964
Brian Osman70d2f432017-11-08 09:54:10 -0500965#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400966 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
967 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500968 DisplayParams params = fWindow->getRequestedDisplayParams();
969 delete fWindow;
970 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500971
Brian Osman70d2f432017-11-08 09:54:10 -0500972 // re-register callbacks
973 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500974 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500975 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500976 fWindow->pushLayer(&fImGuiLayer);
977
Brian Osman70d2f432017-11-08 09:54:10 -0500978 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
979 // will still include our correct sample count. But the re-created fWindow will lose that
980 // information. On Windows, we need to re-create the window when changing sample count,
981 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
982 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
983 // as if we had never changed windows at all).
984 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500985#endif
986
Brian Osman70d2f432017-11-08 09:54:10 -0500987 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500988}
989
Brian Osman92004802017-03-06 11:47:26 -0500990void Viewer::setColorMode(ColorMode colorMode) {
991 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700992
Brian Osmanf750fbc2017-02-08 10:47:28 -0500993 // 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 -0400994 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700995 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500996 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400997 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
998 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500999 if (wasInLegacy != wantLegacy) {
1000 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -07001001 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001002 }
1003
1004 this->updateTitle();
1005 fWindow->inval();
1006}
1007
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001008class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1009public:
1010 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
1011 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
1012 { }
1013 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001014 if (*paint == nullptr) {
1015 return true;
1016 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001017 if (fPaintOverrides->fHinting) {
1018 paint->writable()->setHinting(fPaint->getHinting());
1019 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001020
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001021 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
1022 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1023 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001024 if (fPaintOverrides->fFlags & SkPaint::kDither_Flag) {
1025 paint->writable()->setDither(fPaint->isDither());
1026 }
1027 if (fPaintOverrides->fFlags & SkPaint::kFakeBoldText_Flag) {
1028 paint->writable()->setFakeBoldText(fPaint->isFakeBoldText());
1029 }
1030 if (fPaintOverrides->fFlags & SkPaint::kLinearText_Flag) {
1031 paint->writable()->setLinearText(fPaint->isLinearText());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001032 }
1033 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
1034 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
1035 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001036 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
1037 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
1038 }
1039 if (fPaintOverrides->fFlags & SkPaint::kEmbeddedBitmapText_Flag) {
1040 paint->writable()->setEmbeddedBitmapText(fPaint->isEmbeddedBitmapText());
1041 }
1042 if (fPaintOverrides->fFlags & SkPaint::kAutoHinting_Flag) {
1043 paint->writable()->setAutohinted(fPaint->isAutohinted());
1044 }
1045 if (fPaintOverrides->fFlags & SkPaint::kVerticalText_Flag) {
1046 paint->writable()->setVerticalText(fPaint->isVerticalText());
1047 }
1048
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001049 return true;
1050 }
1051 SkPaint* fPaint;
1052 Viewer::SkPaintFields* fPaintOverrides;
1053};
1054
Brian Osmanf750fbc2017-02-08 10:47:28 -05001055void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001056 SkAutoCanvasRestore autorestore(canvas, false);
1057
Brian Osmanf750fbc2017-02-08 10:47:28 -05001058 // By default, we render directly into the window's surface/canvas
1059 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001060 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001061
Brian Osmane0d4fba2017-03-15 10:24:55 -04001062 // If we're in any of the color managed modes, construct the color space we're going to use
1063 sk_sp<SkColorSpace> cs = nullptr;
1064 if (ColorMode::kLegacy != fColorMode) {
1065 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
1066 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -04001067 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001068 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -05001069 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1070 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
1071 } else {
1072 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
1073 }
Brian Osmane0d4fba2017-03-15 10:24:55 -04001074 }
1075
Brian Osman3ac99cf2017-12-01 11:23:53 -05001076 if (fSaveToSKP) {
1077 SkPictureRecorder recorder;
1078 SkCanvas* recorderCanvas = recorder.beginRecording(
1079 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1080 // In xform-canvas mode, record the transformed output
1081 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1082 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1083 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
1084 recorderCanvas = xformCanvas.get();
1085 }
1086 fSlides[fCurrentSlide]->draw(recorderCanvas);
1087 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1088 SkFILEWStream stream("sample_app.skp");
1089 picture->serialize(&stream);
1090 fSaveToSKP = false;
1091 }
1092
Brian Osmane0d4fba2017-03-15 10:24:55 -04001093 // 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 -05001094 // 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 -04001095 // because the window surface is actually GL, or we're doing fake perspective.
Brian Osmanf750fbc2017-02-08 10:47:28 -05001096 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -05001097 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -05001098 if (Window::kRaster_BackendType == fBackendType ||
Brian Osman805a7272018-05-02 15:40:20 -04001099 kPerspective_Fake == fPerspectiveMode ||
Brian Osman70d2f432017-11-08 09:54:10 -05001100 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -05001101 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -04001102 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
1103 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
1104
Brian Osman92004802017-03-06 11:47:26 -05001105 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
1106 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001107 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
1108 // with a special canvas (below) that has the color space attached
1109 sk_sp<SkColorSpace> offscreenColorSpace =
1110 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -05001111 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -04001112 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Ben Wagner37c54032018-04-13 14:30:23 -04001113 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1114 canvas->getProps(&props);
1115 offscreenSurface = Window::kRaster_BackendType == fBackendType
1116 ? SkSurface::MakeRaster(info, &props)
1117 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -05001118 SkPixmap offscreenPixmap;
1119 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
1120 SkBitmap offscreenBitmap;
1121 offscreenBitmap.installPixels(offscreenPixmap);
Herb Derbyefe39bc2018-05-01 17:06:20 -04001122 threadedCanvas =
1123 skstd::make_unique<SkCanvas>(
1124 sk_make_sp<SkThreadedBMPDevice>(
1125 offscreenBitmap, fTileCnt, fThreadCnt, fExecutor.get()));
Yuqian Lib2ba6642017-11-22 12:07:41 -05001126 slideCanvas = threadedCanvas.get();
1127 } else {
1128 slideCanvas = offscreenSurface->getCanvas();
1129 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001130 }
1131
Brian Osmane0d4fba2017-03-15 10:24:55 -04001132 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1133 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1134 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
1135 slideCanvas = xformCanvas.get();
1136 }
1137
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001138 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001139 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001140 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -05001141 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001142 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001143 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1144 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001145 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001146 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001147
1148 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001149 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001150 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001151 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001152
1153 // If we rendered offscreen, snap an image and push the results to the window's canvas
1154 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001155 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001156
1157 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -04001158 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001159 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001160 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001161 SkPaint paint;
1162 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001163 int prePerspectiveCount = canvas->save();
1164 if (kPerspective_Fake == fPerspectiveMode) {
1165 paint.setFilterQuality(kHigh_SkFilterQuality);
1166 canvas->clear(SK_ColorWHITE);
1167 canvas->concat(this->computePerspectiveMatrix());
1168 }
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001169 canvas->drawImage(retaggedImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001170 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001171 }
liyuqian6f163d22016-06-13 12:26:45 -07001172}
1173
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001174void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001175 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001176 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001177}
Jim Van Verth6f449692017-02-14 15:16:46 -05001178
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001179void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001180 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001181
brianosman622c8d52016-05-10 06:50:49 -07001182 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001183
Brian Osmand67e5182017-12-08 16:46:09 -05001184 this->drawImGui();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001185}
1186
Florin Malitacefc1b92018-02-19 21:43:47 -05001187SkPoint Viewer::mapEvent(float x, float y) {
1188 const auto m = this->computeMatrix();
1189 SkMatrix inv;
1190
1191 SkAssertResult(m.invert(&inv));
1192
1193 return inv.mapXY(x, y);
1194}
1195
jvanverth814e38d2016-06-06 08:48:47 -07001196bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001197 if (GestureDevice::kMouse == fGestureDevice) {
1198 return false;
1199 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001200
1201 const auto slidePt = this->mapEvent(x, y);
1202 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1203 fWindow->inval();
1204 return true;
1205 }
1206
liyuqiand3cdbca2016-05-17 12:44:20 -07001207 void* castedOwner = reinterpret_cast<void*>(owner);
1208 switch (state) {
1209 case Window::kUp_InputState: {
1210 fGesture.touchEnd(castedOwner);
1211 break;
1212 }
1213 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001214 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001215 break;
1216 }
1217 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001218 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001219 break;
1220 }
1221 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001222 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001223 fWindow->inval();
1224 return true;
1225}
1226
Brian Osman80fc07e2017-12-08 16:45:43 -05001227bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001228 if (GestureDevice::kTouch == fGestureDevice) {
1229 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001230 }
Brian Osman16c81a12017-12-20 11:58:34 -05001231
Florin Malitacefc1b92018-02-19 21:43:47 -05001232 const auto slidePt = this->mapEvent(x, y);
1233 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1234 fWindow->inval();
1235 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001236 }
1237
1238 switch (state) {
1239 case Window::kUp_InputState: {
1240 fGesture.touchEnd(nullptr);
1241 break;
1242 }
1243 case Window::kDown_InputState: {
1244 fGesture.touchBegin(nullptr, x, y);
1245 break;
1246 }
1247 case Window::kMove_InputState: {
1248 fGesture.touchMoved(nullptr, x, y);
1249 break;
1250 }
1251 }
1252 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1253
1254 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1255 fWindow->inval();
1256 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001257 return true;
1258}
1259
Brian Osmana109e392017-02-24 09:49:14 -05001260static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1261 const ImVec2& pos, const ImVec2& size) {
1262 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1263 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1264
1265 // Invisible 10x10 button
1266 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1267 ImGui::InvisibleButton(label, ImVec2(10, 10));
1268
1269 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1270 ImGuiIO& io = ImGui::GetIO();
1271 // Normalized mouse position, relative to our gamut box
1272 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1273 // Clamp to edge of box, convert back to primary scale
1274 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1275 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1276 }
1277
1278 if (ImGui::IsItemHovered()) {
1279 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1280 }
1281
1282 // Return screen coordinates for the caller. We could just return center here, but we'd have
1283 // one frame of lag during drag.
1284 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1285}
1286
1287static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1288 ImDrawList* drawList = ImGui::GetWindowDrawList();
1289
1290 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1291 // width, and scale the height to maintain aspect ratio.
1292 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1293 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1294 ImVec2 pos = ImGui::GetCursorScreenPos();
1295
1296 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1297 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1298 // Magic numbers are pixel locations of the origin and upper-right corner.
1299 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1300 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001301
1302 // Primary markers
1303 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1304 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1305 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1306 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1307
1308 // Gamut triangle
1309 drawList->AddCircle(r, 5.0f, 0xFF000040);
1310 drawList->AddCircle(g, 5.0f, 0xFF004000);
1311 drawList->AddCircle(b, 5.0f, 0xFF400000);
1312 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1313 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1314
1315 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001316 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1317}
1318
1319static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1320 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1321 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1322 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1323
1324 // Invisible 10x10 button
1325 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1326 ImGui::InvisibleButton(label, ImVec2(10, 10));
1327
1328 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1329 ImGuiIO& io = ImGui::GetIO();
1330 // Normalized mouse position, relative to our gamut box
1331 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1332 // Clamp to edge of box
1333 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1334 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1335 *dragging = true;
1336 }
1337
1338 // Return screen coordinates for the caller. We could just return center here, but we'd have
1339 // one frame of lag during drag.
1340 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1341}
1342
1343static bool ImGui_DragQuad(SkPoint* pts) {
1344 ImDrawList* drawList = ImGui::GetWindowDrawList();
1345
1346 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1347 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1348 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1349 ImVec2 pos = ImGui::GetCursorScreenPos();
1350
1351 // Background rectangle
1352 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1353
1354 // Corner markers
1355 bool dragging = false;
1356 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1357 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1358 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1359 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1360
1361 // Draw markers and quad
1362 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1363 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1364 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1365 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1366 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1367 drawList->AddLine(tr, br, 0xFFFFFFFF);
1368 drawList->AddLine(br, bl, 0xFFFFFFFF);
1369 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1370
1371 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1372 ImGui::Spacing();
1373
1374 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001375}
1376
Brian Osmand67e5182017-12-08 16:46:09 -05001377void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001378 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1379 if (fShowImGuiTestWindow) {
1380 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1381 }
1382
1383 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001384 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1385 // always visible, we can end up in a layout feedback loop.
1386 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001387 DisplayParams params = fWindow->getRequestedDisplayParams();
1388 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001389 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1390 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001391 if (ImGui::CollapsingHeader("Backend")) {
1392 int newBackend = static_cast<int>(fBackendType);
1393 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1394 ImGui::SameLine();
1395 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001396#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1397 ImGui::SameLine();
1398 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1399#endif
Brian Osman621491e2017-02-28 15:45:01 -05001400#if defined(SK_VULKAN)
1401 ImGui::SameLine();
1402 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1403#endif
1404 if (newBackend != fBackendType) {
1405 fDeferredActions.push_back([=]() {
1406 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1407 });
1408 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001409
Brian Salomon99a33902017-03-07 15:16:34 -05001410 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001411 bool* wire = &params.fGrContextOptions.fWireframeMode;
1412 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1413 paramsChanged = true;
1414 }
Brian Salomon99a33902017-03-07 15:16:34 -05001415
Brian Osman28b12522017-03-08 17:10:24 -05001416 if (ctx) {
1417 int sampleCount = fWindow->sampleCount();
1418 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001419 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001420 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1421 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1422 ImGui::RadioButton("16", &sampleCount, 16);
1423
1424 if (sampleCount != params.fMSAASampleCount) {
1425 params.fMSAASampleCount = sampleCount;
1426 paramsChanged = true;
1427 }
1428 }
1429
Ben Wagner37c54032018-04-13 14:30:23 -04001430 int pixelGeometryIdx = 0;
1431 if (fPixelGeometryOverrides) {
1432 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1433 }
1434 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1435 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1436 {
1437 uint32_t flags = params.fSurfaceProps.flags();
1438 if (pixelGeometryIdx == 0) {
1439 fPixelGeometryOverrides = false;
1440 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1441 } else {
1442 fPixelGeometryOverrides = true;
1443 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1444 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1445 }
1446 paramsChanged = true;
1447 }
1448
1449 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1450 if (ImGui::Checkbox("DFT", &useDFT)) {
1451 uint32_t flags = params.fSurfaceProps.flags();
1452 if (useDFT) {
1453 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1454 } else {
1455 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1456 }
1457 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1458 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1459 paramsChanged = true;
1460 }
1461
Brian Osman8a9de3d2017-03-01 14:59:05 -05001462 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001463 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001464 auto prButton = [&](GpuPathRenderers x) {
1465 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001466 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1467 params.fGrContextOptions.fGpuPathRenderers = x;
1468 paramsChanged = true;
1469 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001470 }
1471 };
1472
1473 if (!ctx) {
1474 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001475 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001476 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001477 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001478 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001479 prButton(GpuPathRenderers::kStencilAndCover);
1480 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001481 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001482 prButton(GpuPathRenderers::kNone);
1483 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001484 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001485 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001486 if (GrCoverageCountingPathRenderer::IsSupported(
1487 *ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001488 prButton(GpuPathRenderers::kCoverageCounting);
1489 }
Jim Van Verth83010462017-03-16 08:45:39 -04001490 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001491 prButton(GpuPathRenderers::kTessellating);
1492 prButton(GpuPathRenderers::kNone);
1493 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001494 ImGui::TreePop();
1495 }
Brian Osman621491e2017-02-28 15:45:01 -05001496 }
1497
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001498 if (ImGui::CollapsingHeader("Transform")) {
1499 float zoom = fZoomLevel;
1500 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1501 fZoomLevel = zoom;
1502 this->preTouchMatrixChanged();
1503 paramsChanged = true;
1504 }
1505 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001506 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001507 fRotation = deg;
1508 this->preTouchMatrixChanged();
1509 paramsChanged = true;
1510 }
Brian Osman805a7272018-05-02 15:40:20 -04001511 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1512 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1513 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001514 this->preTouchMatrixChanged();
Brian Osman805a7272018-05-02 15:40:20 -04001515 this->updateTitle();
Brian Osman9bb47cf2018-04-26 15:55:00 -04001516 }
1517 if (ImGui_DragQuad(fPerspectivePoints)) {
1518 this->preTouchMatrixChanged();
1519 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001520 }
1521
Ben Wagnera580fb32018-04-17 11:16:32 -04001522 if (ImGui::CollapsingHeader("Paint")) {
1523 int hintingIdx = 0;
1524 if (fPaintOverrides.fHinting) {
1525 hintingIdx = fPaint.getHinting() + 1;
1526 }
1527 if (ImGui::Combo("Hinting", &hintingIdx,
1528 "Default\0None\0Slight\0Normal\0Full\0\0"))
1529 {
1530 if (hintingIdx == 0) {
1531 fPaintOverrides.fHinting = false;
1532 fPaint.setHinting(SkPaint::kNo_Hinting);
1533 } else {
1534 fPaintOverrides.fHinting = true;
1535 SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1);
1536 fPaint.setHinting(hinting);
1537 }
1538 paramsChanged = true;
1539 }
1540
1541 int aliasIdx = 0;
1542 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1543 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1544 }
1545 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1546 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1547 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1548 {
1549 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1550 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1551 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1552 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1553 if (aliasIdx == 0) {
1554 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1555 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1556 } else {
1557 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1558 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1559 fPaint.setAntiAlias(aliasIdx > 1);
1560 switch (fPaintOverrides.fAntiAlias) {
1561 case SkPaintFields::AntiAliasState::Alias:
1562 break;
1563 case SkPaintFields::AntiAliasState::Normal:
1564 break;
1565 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1566 gSkUseAnalyticAA = true;
1567 gSkForceAnalyticAA = false;
1568 gSkUseDeltaAA = gSkForceDeltaAA = false;
1569 break;
1570 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1571 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1572 gSkUseDeltaAA = gSkForceDeltaAA = false;
1573 break;
1574 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1575 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1576 gSkUseDeltaAA = true;
1577 gSkForceDeltaAA = false;
1578 break;
1579 case SkPaintFields::AntiAliasState::DeltaAAForced:
1580 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1581 gSkUseDeltaAA = gSkForceDeltaAA = true;
1582 break;
1583 }
1584 }
1585 paramsChanged = true;
1586 }
1587
Ben Wagner99a78dc2018-05-09 18:23:51 -04001588 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
1589 SkPaint::Flags flag,
1590 bool (SkPaint::* isFlag)() const,
1591 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001592 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001593 int itemIndex = 0;
1594 if (fPaintOverrides.fFlags & flag) {
1595 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001596 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001597 if (ImGui::Combo(label, &itemIndex, items)) {
1598 if (itemIndex == 0) {
1599 fPaintOverrides.fFlags &= ~flag;
1600 } else {
1601 fPaintOverrides.fFlags |= flag;
1602 (fPaint.*setFlag)(itemIndex == 2);
1603 }
1604 paramsChanged = true;
1605 }
1606 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001607
Ben Wagner99a78dc2018-05-09 18:23:51 -04001608 paintFlag("Dither",
1609 "Default\0No Dither\0Dither\0\0",
1610 SkPaint::kDither_Flag,
1611 &SkPaint::isDither, &SkPaint::setDither);
1612
1613 paintFlag("Fake Bold Glyphs",
1614 "Default\0No Fake Bold\0Fake Bold\0\0",
1615 SkPaint::kFakeBoldText_Flag,
1616 &SkPaint::isFakeBoldText, &SkPaint::setFakeBoldText);
1617
1618 paintFlag("Linear Text",
1619 "Default\0No Linear Text\0Linear Text\0\0",
1620 SkPaint::kLinearText_Flag,
1621 &SkPaint::isLinearText, &SkPaint::setLinearText);
1622
1623 paintFlag("Subpixel Position Glyphs",
1624 "Default\0Pixel Text\0Subpixel Text\0\0",
1625 SkPaint::kSubpixelText_Flag,
1626 &SkPaint::isSubpixelText, &SkPaint::setSubpixelText);
1627
1628 paintFlag("Subpixel Anti-Alias",
1629 "Default\0lcd\0LCD\0\0",
1630 SkPaint::kLCDRenderText_Flag,
1631 &SkPaint::isLCDRenderText, &SkPaint::setLCDRenderText);
1632
1633 paintFlag("Embedded Bitmap Text",
1634 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1635 SkPaint::kEmbeddedBitmapText_Flag,
1636 &SkPaint::isEmbeddedBitmapText, &SkPaint::setEmbeddedBitmapText);
1637
1638 paintFlag("Force Auto-Hinting",
1639 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1640 SkPaint::kAutoHinting_Flag,
1641 &SkPaint::isAutohinted, &SkPaint::setAutohinted);
1642
1643 paintFlag("Vertical Text",
1644 "Default\0No Vertical Text\0Vertical Text\0\0",
1645 SkPaint::kVerticalText_Flag,
1646 &SkPaint::isVerticalText, &SkPaint::setVerticalText);
Ben Wagnera580fb32018-04-17 11:16:32 -04001647 }
1648
Ben Wagner7a3c6742018-04-23 10:01:07 -04001649 if (fShowSlidePicker) {
1650 ImGui::SetNextTreeNodeOpen(true);
1651 }
Brian Osman79086b92017-02-10 13:36:16 -05001652 if (ImGui::CollapsingHeader("Slide")) {
1653 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001654 static ImVector<const char*> filteredSlideNames;
1655 static ImVector<int> filteredSlideIndices;
1656
Brian Osmanfce09c52017-11-14 15:32:20 -05001657 if (fShowSlidePicker) {
1658 ImGui::SetKeyboardFocusHere();
1659 fShowSlidePicker = false;
1660 }
1661
Brian Osman79086b92017-02-10 13:36:16 -05001662 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001663 filteredSlideNames.clear();
1664 filteredSlideIndices.clear();
1665 int filteredIndex = 0;
1666 for (int i = 0; i < fSlides.count(); ++i) {
1667 const char* slideName = fSlides[i]->getName().c_str();
1668 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1669 if (i == fCurrentSlide) {
1670 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001671 }
Brian Osmanf479e422017-11-08 13:11:36 -05001672 filteredSlideNames.push_back(slideName);
1673 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001674 }
Brian Osman79086b92017-02-10 13:36:16 -05001675 }
Brian Osmanf479e422017-11-08 13:11:36 -05001676
Brian Osmanf479e422017-11-08 13:11:36 -05001677 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1678 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001679 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001680 }
1681 }
Brian Osmana109e392017-02-24 09:49:14 -05001682
1683 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001684 ColorMode newMode = fColorMode;
1685 auto cmButton = [&](ColorMode mode, const char* label) {
1686 if (ImGui::RadioButton(label, mode == fColorMode)) {
1687 newMode = mode;
1688 }
1689 };
1690
1691 cmButton(ColorMode::kLegacy, "Legacy 8888");
1692 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1693 "Color Managed 8888 (Nonlinear blending)");
1694 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1695 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1696
1697 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001698 // It isn't safe to switch color mode now (in the middle of painting). We might
1699 // tear down the back-end, etc... Defer this change until the next onIdle.
1700 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001701 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001702 });
Brian Osmana109e392017-02-24 09:49:14 -05001703 }
1704
1705 // Pick from common gamuts:
1706 int primariesIdx = 4; // Default: Custom
1707 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1708 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1709 primariesIdx = i;
1710 break;
1711 }
1712 }
1713
Brian Osmanfdab5762017-11-09 10:27:55 -05001714 // When we're in xform canvas mode, we can alter the transfer function, too
1715 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1716 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1717 }
1718
Brian Osmana109e392017-02-24 09:49:14 -05001719 if (ImGui::Combo("Primaries", &primariesIdx,
1720 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1721 if (primariesIdx >= 0 && primariesIdx <= 3) {
1722 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1723 }
1724 }
1725
1726 // Allow direct editing of gamut
1727 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1728 }
Brian Osman79086b92017-02-10 13:36:16 -05001729 }
Brian Salomon99a33902017-03-07 15:16:34 -05001730 if (paramsChanged) {
1731 fDeferredActions.push_back([=]() {
1732 fWindow->setRequestedDisplayParams(params);
1733 fWindow->inval();
1734 this->updateTitle();
1735 });
1736 }
Brian Osman79086b92017-02-10 13:36:16 -05001737 ImGui::End();
1738 }
1739
Brian Osmanf6877092017-02-13 09:39:57 -05001740 if (fShowZoomWindow && fLastImage) {
1741 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001742 static int zoomFactor = 8;
1743 if (ImGui::Button("<<")) {
1744 zoomFactor = SkTMax(zoomFactor / 2, 4);
1745 }
1746 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1747 if (ImGui::Button(">>")) {
1748 zoomFactor = SkTMin(zoomFactor * 2, 32);
1749 }
Brian Osmanf6877092017-02-13 09:39:57 -05001750
Brian Osmanf6877092017-02-13 09:39:57 -05001751 ImVec2 mousePos = ImGui::GetMousePos();
1752 ImVec2 avail = ImGui::GetContentRegionAvail();
1753
Brian Osmanead517d2017-11-13 15:36:36 -05001754 uint32_t pixel = 0;
1755 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1756 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1757 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001758 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1759 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1760 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001761 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1762 }
1763
Brian Osmand67e5182017-12-08 16:46:09 -05001764 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001765 // Translate so the region of the image that's under the mouse cursor is centered
1766 // in the zoom canvas:
1767 c->scale(zoomFactor, zoomFactor);
1768 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1769 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1770 c->drawImage(this->fLastImage, 0, 0);
1771
1772 SkPaint outline;
1773 outline.setStyle(SkPaint::kStroke_Style);
1774 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1775 });
Brian Osmanf6877092017-02-13 09:39:57 -05001776 }
1777
1778 ImGui::End();
1779 }
Brian Osman79086b92017-02-10 13:36:16 -05001780}
1781
liyuqian2edb0f42016-07-06 14:11:32 -07001782void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001783 for (int i = 0; i < fDeferredActions.count(); ++i) {
1784 fDeferredActions[i]();
1785 }
1786 fDeferredActions.reset();
1787
Brian Osman56a24812017-12-19 11:15:16 -05001788 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001789 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001790 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001791 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001792
Brian Osman79086b92017-02-10 13:36:16 -05001793 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001794 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001795 fWindow->inval();
1796 }
jvanverth9f372462016-04-06 06:08:59 -07001797}
liyuqiane5a6cd92016-05-27 08:52:52 -07001798
1799void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001800 if (!fWindow) {
1801 return;
1802 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001803 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001804 return; // Surface hasn't been created yet.
1805 }
1806
liyuqianb73c24b2016-06-03 08:47:23 -07001807 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001808 Json::Value slideState(Json::objectValue);
1809 slideState[kName] = kSlideStateName;
1810 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001811 if (fAllSlideNames.size() == 0) {
1812 for(auto slide : fSlides) {
1813 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1814 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001815 }
liyuqian1f508fd2016-06-07 06:57:40 -07001816 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001817
liyuqianb73c24b2016-06-03 08:47:23 -07001818 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001819 Json::Value backendState(Json::objectValue);
1820 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001821 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001822 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001823 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001824 backendState[kOptions].append(Json::Value(str));
1825 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001826
csmartdalton578f0642017-02-24 16:04:47 -07001827 // MSAA state
1828 Json::Value msaaState(Json::objectValue);
1829 msaaState[kName] = kMSAAStateName;
1830 msaaState[kValue] = fWindow->sampleCount();
1831 msaaState[kOptions] = Json::Value(Json::arrayValue);
1832 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1833 msaaState[kOptions].append(Json::Value(0));
1834 } else {
1835 for (int msaa : {0, 4, 8, 16}) {
1836 msaaState[kOptions].append(Json::Value(msaa));
1837 }
1838 }
1839
csmartdalton61cd31a2017-02-27 17:00:53 -07001840 // Path renderer state
1841 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1842 Json::Value prState(Json::objectValue);
1843 prState[kName] = kPathRendererStateName;
1844 prState[kValue] = gPathRendererNames[pr];
1845 prState[kOptions] = Json::Value(Json::arrayValue);
1846 const GrContext* ctx = fWindow->getGrContext();
1847 if (!ctx) {
1848 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001849 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001850 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001851 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001852 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001853 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1854 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001855 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001856 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1857 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001858 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001859 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001860 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001861 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1862 }
Jim Van Verth83010462017-03-16 08:45:39 -04001863 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001864 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001865 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1866 }
1867
liyuqianb73c24b2016-06-03 08:47:23 -07001868 // Softkey state
1869 Json::Value softkeyState(Json::objectValue);
1870 softkeyState[kName] = kSoftkeyStateName;
1871 softkeyState[kValue] = kSoftkeyHint;
1872 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1873 softkeyState[kOptions].append(kSoftkeyHint);
1874 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1875 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1876 }
1877
liyuqiane5a6cd92016-05-27 08:52:52 -07001878 Json::Value state(Json::arrayValue);
1879 state.append(slideState);
1880 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001881 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001882 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001883 state.append(softkeyState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001884
Brian Osmaneff04b52017-11-21 13:18:02 -05001885 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001886}
1887
1888void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001889 // For those who will add more features to handle the state change in this function:
1890 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1891 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1892 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001893 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001894 for (int i = 0; i < fSlides.count(); ++i) {
1895 if (fSlides[i]->getName().equals(stateValue)) {
1896 this->setCurrentSlide(i);
1897 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001898 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001899 }
Florin Malitaab99c342018-01-16 16:23:03 -05001900
1901 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001902 } else if (stateName.equals(kBackendStateName)) {
1903 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1904 if (stateValue.equals(kBackendTypeStrings[i])) {
1905 if (fBackendType != i) {
1906 fBackendType = (sk_app::Window::BackendType)i;
1907 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001908 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001909 }
1910 break;
1911 }
1912 }
csmartdalton578f0642017-02-24 16:04:47 -07001913 } else if (stateName.equals(kMSAAStateName)) {
1914 DisplayParams params = fWindow->getRequestedDisplayParams();
1915 int sampleCount = atoi(stateValue.c_str());
1916 if (sampleCount != params.fMSAASampleCount) {
1917 params.fMSAASampleCount = sampleCount;
1918 fWindow->setRequestedDisplayParams(params);
1919 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001920 this->updateTitle();
1921 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001922 }
1923 } else if (stateName.equals(kPathRendererStateName)) {
1924 DisplayParams params = fWindow->getRequestedDisplayParams();
1925 for (const auto& pair : gPathRendererNames) {
1926 if (pair.second == stateValue.c_str()) {
1927 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1928 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1929 fWindow->setRequestedDisplayParams(params);
1930 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001931 this->updateTitle();
1932 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001933 }
1934 break;
1935 }
csmartdalton578f0642017-02-24 16:04:47 -07001936 }
liyuqianb73c24b2016-06-03 08:47:23 -07001937 } else if (stateName.equals(kSoftkeyStateName)) {
1938 if (!stateValue.equals(kSoftkeyHint)) {
1939 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001940 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001941 }
liyuqian2edb0f42016-07-06 14:11:32 -07001942 } else if (stateName.equals(kRefreshStateName)) {
1943 // This state is actually NOT in the UI state.
1944 // We use this to allow Android to quickly set bool fRefresh.
1945 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001946 } else {
1947 SkDebugf("Unknown stateName: %s", stateName.c_str());
1948 }
1949}
Brian Osman79086b92017-02-10 13:36:16 -05001950
1951bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001952 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001953}
1954
1955bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001956 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001957 fWindow->inval();
1958 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001959 } else {
1960 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001961 }
Brian Osman79086b92017-02-10 13:36:16 -05001962}