blob: 65280bc4af9aa8e9e88a4dc54f2792d5e7f441db [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
Hal Canaryfdcfb8b2018-06-13 09:42:32 -04008#include "Viewer.h"
9#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 "SvgSlide.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060041#include "ccpr/GrCoverageCountingPathRenderer.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040042#include "imgui.h"
csmartdalton578f0642017-02-24 16:04:47 -070043
Florin Malita3b526b02018-05-25 12:43:51 -040044#if defined(SK_HAS_SKSG)
45 #include "SlideDir.h"
46#endif
47
Florin Malita87ccf332018-05-04 12:23:24 -040048#if defined(SK_ENABLE_SKOTTIE)
49 #include "SkottieSlide.h"
50#endif
51
jvanverth34524262016-05-04 13:49:13 -070052using namespace sk_app;
53
csmartdalton61cd31a2017-02-27 17:00:53 -070054static std::map<GpuPathRenderers, std::string> gPathRendererNames;
55
jvanverth9f372462016-04-06 06:08:59 -070056Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070057 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070058}
59
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060060static DEFINE_string(slide, "", "Start on this sample.");
61static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050062
bsalomon6c471f72016-07-26 12:56:32 -070063#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070064# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070065#else
66# define BACKENDS_STR "\"sw\" and \"gl\""
67#endif
68
Brian Osman2dd96932016-10-18 15:33:53 -040069static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070070
Brian Salomonbdecacf2018-02-02 20:32:49 -050071static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070072
Chris Dalton2d18f412018-02-20 13:23:32 -070073DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
74
Brian Osman53136aa2017-07-20 15:43:35 -040075DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050076
Florin Malita38792ce2018-05-08 10:36:18 -040077DEFINE_string2(file, f, "", "Open a single file for viewing.");
78
Brian Salomon194db172017-08-17 14:37:06 -040079const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070080 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040081#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
82 "ANGLE",
83#endif
jvanverth063ece72016-06-17 09:29:14 -070084#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070085 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070086#endif
csmartdalton578f0642017-02-24 16:04:47 -070087 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070088};
89
bsalomon6c471f72016-07-26 12:56:32 -070090static sk_app::Window::BackendType get_backend_type(const char* str) {
91#ifdef SK_VULKAN
92 if (0 == strcmp(str, "vk")) {
93 return sk_app::Window::kVulkan_BackendType;
94 } else
95#endif
Brian Salomon194db172017-08-17 14:37:06 -040096#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
97 if (0 == strcmp(str, "angle")) {
98 return sk_app::Window::kANGLE_BackendType;
99 } else
100#endif
bsalomon6c471f72016-07-26 12:56:32 -0700101 if (0 == strcmp(str, "gl")) {
102 return sk_app::Window::kNativeGL_BackendType;
103 } else if (0 == strcmp(str, "sw")) {
104 return sk_app::Window::kRaster_BackendType;
105 } else {
106 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
107 return sk_app::Window::kRaster_BackendType;
108 }
109}
110
Brian Osmana109e392017-02-24 09:49:14 -0500111static SkColorSpacePrimaries gSrgbPrimaries = {
112 0.64f, 0.33f,
113 0.30f, 0.60f,
114 0.15f, 0.06f,
115 0.3127f, 0.3290f };
116
117static SkColorSpacePrimaries gAdobePrimaries = {
118 0.64f, 0.33f,
119 0.21f, 0.71f,
120 0.15f, 0.06f,
121 0.3127f, 0.3290f };
122
123static SkColorSpacePrimaries gP3Primaries = {
124 0.680f, 0.320f,
125 0.265f, 0.690f,
126 0.150f, 0.060f,
127 0.3127f, 0.3290f };
128
129static SkColorSpacePrimaries gRec2020Primaries = {
130 0.708f, 0.292f,
131 0.170f, 0.797f,
132 0.131f, 0.046f,
133 0.3127f, 0.3290f };
134
135struct NamedPrimaries {
136 const char* fName;
137 SkColorSpacePrimaries* fPrimaries;
138} gNamedPrimaries[] = {
139 { "sRGB", &gSrgbPrimaries },
140 { "AdobeRGB", &gAdobePrimaries },
141 { "P3", &gP3Primaries },
142 { "Rec. 2020", &gRec2020Primaries },
143};
144
145static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
146 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
147}
148
Brian Osman70d2f432017-11-08 09:54:10 -0500149static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
150 // In raster mode, we still use GL for the window.
151 // This lets us render the GUI faster (and correct).
152 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
153}
154
liyuqiane5a6cd92016-05-27 08:52:52 -0700155const char* kName = "name";
156const char* kValue = "value";
157const char* kOptions = "options";
158const char* kSlideStateName = "Slide";
159const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700160const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700161const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700162const char* kSoftkeyStateName = "Softkey";
163const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700164const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700165const char* kON = "ON";
166const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700167const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700168
jvanverth34524262016-05-04 13:49:13 -0700169Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500170 : fCurrentSlide(-1)
171 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500172 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500173 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500174 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500175 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500176 , fShowZoomWindow(false)
177 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700178 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500179 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500180 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500181 // Our UI can only tweak gamma (currently), so start out gamma-only
182 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700183 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400184 , fRotation(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400185 , fGestureDevice(GestureDevice::kNone)
Brian Osman805a7272018-05-02 15:40:20 -0400186 , fPerspectiveMode(kPerspective_Off)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500187 , fTileCnt(0)
188 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700189{
Greg Daniel285db442016-10-14 09:12:53 -0400190 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700191
Brian Osmanf09e35e2017-12-15 14:48:09 -0500192 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
193 gPathRendererNames[GpuPathRenderers::kDefault] =
194 "Default Ganesh Behavior (best path renderer, not including CCPR)";
195 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500196 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
197 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
198 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
199 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700200
jvanverth2bb3b6d2016-04-08 07:24:09 -0700201 SkDebugf("Command line arguments: ");
202 for (int i = 1; i < argc; ++i) {
203 SkDebugf("%s ", argv[i]);
204 }
205 SkDebugf("\n");
206
207 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500208#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400209 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500210#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700211
Ben Wagner483c7722018-02-20 17:06:07 -0500212 if (!FLAGS_nativeFonts) {
213 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
214 }
215
Brian Osmanbc8150f2017-07-24 11:38:01 -0400216 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400217 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400218
bsalomon6c471f72016-07-26 12:56:32 -0700219 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700220 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700221
csmartdalton578f0642017-02-24 16:04:47 -0700222 DisplayParams displayParams;
223 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700224 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700225 fWindow->setRequestedDisplayParams(displayParams);
226
Brian Osman56a24812017-12-19 11:15:16 -0500227 // Configure timers
228 fStatsLayer.setActive(false);
229 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
230 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
231 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
232
jvanverth9f372462016-04-06 06:08:59 -0700233 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700234 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500235 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500236 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500237 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700238
brianosman622c8d52016-05-10 06:50:49 -0700239 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500240 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
241 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
242 fWindow->inval();
243 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500244 // Command to jump directly to the slide picker and give it focus
245 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
246 this->fShowImGuiDebugWindow = true;
247 this->fShowSlidePicker = true;
248 fWindow->inval();
249 });
250 // Alias that to Backspace, to match SampleApp
251 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
252 this->fShowImGuiDebugWindow = true;
253 this->fShowSlidePicker = true;
254 fWindow->inval();
255 });
Brian Osman79086b92017-02-10 13:36:16 -0500256 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
257 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
258 fWindow->inval();
259 });
Brian Osmanf6877092017-02-13 09:39:57 -0500260 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
261 this->fShowZoomWindow = !this->fShowZoomWindow;
262 fWindow->inval();
263 });
brianosman622c8d52016-05-10 06:50:49 -0700264 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500265 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700266 fWindow->inval();
267 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400268 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500269 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400270 this->updateTitle();
271 fWindow->inval();
272 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500273 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500274 switch (fColorMode) {
275 case ColorMode::kLegacy:
276 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
277 break;
278 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
279 this->setColorMode(ColorMode::kColorManagedSRGB8888);
280 break;
281 case ColorMode::kColorManagedSRGB8888:
282 this->setColorMode(ColorMode::kColorManagedLinearF16);
283 break;
284 case ColorMode::kColorManagedLinearF16:
285 this->setColorMode(ColorMode::kLegacy);
286 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500287 }
brianosman622c8d52016-05-10 06:50:49 -0700288 });
289 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500290 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700291 });
292 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500293 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700294 });
295 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
296 this->changeZoomLevel(1.f / 32.f);
297 fWindow->inval();
298 });
299 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
300 this->changeZoomLevel(-1.f / 32.f);
301 fWindow->inval();
302 });
jvanverthaf236b52016-05-20 06:01:06 -0700303 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400304 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
305 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500306 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400307#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
308 if (newBackend == sk_app::Window::kVulkan_BackendType) {
309 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
310 sk_app::Window::kBackendTypeCount);
311 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
312 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500313 }
314#endif
Brian Osman621491e2017-02-28 15:45:01 -0500315 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700316 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500317 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
318 fTileCnt++;
319 if (fThreadCnt == 0) {
320 this->resetExecutor();
321 }
322 this->updateTitle();
323 fWindow->inval();
324 });
325 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
326 fTileCnt = SkTMax(0, fTileCnt - 1);
327 if (fThreadCnt == 0) {
328 this->resetExecutor();
329 }
330 this->updateTitle();
331 fWindow->inval();
332 });
333 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
334 if (fTileCnt == 0) {
335 return;
336 }
337 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
338 this->resetExecutor();
339 this->updateTitle();
340 fWindow->inval();
341 });
342 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
343 if (fTileCnt == 0) {
344 return;
345 }
346 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
347 this->resetExecutor();
348 this->updateTitle();
349 fWindow->inval();
350 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500351 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
352 fSaveToSKP = true;
353 fWindow->inval();
354 });
Ben Wagner37c54032018-04-13 14:30:23 -0400355 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
356 DisplayParams params = fWindow->getRequestedDisplayParams();
357 uint32_t flags = params.fSurfaceProps.flags();
358 if (!fPixelGeometryOverrides) {
359 fPixelGeometryOverrides = true;
360 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
361 } else {
362 switch (params.fSurfaceProps.pixelGeometry()) {
363 case kUnknown_SkPixelGeometry:
364 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
365 break;
366 case kRGB_H_SkPixelGeometry:
367 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
368 break;
369 case kBGR_H_SkPixelGeometry:
370 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
371 break;
372 case kRGB_V_SkPixelGeometry:
373 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
374 break;
375 case kBGR_V_SkPixelGeometry:
376 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
377 fPixelGeometryOverrides = false;
378 break;
379 }
380 }
381 fWindow->setRequestedDisplayParams(params);
382 this->updateTitle();
383 fWindow->inval();
384 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500385 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
386 if (!fPaintOverrides.fHinting) {
387 fPaintOverrides.fHinting = true;
388 fPaint.setHinting(SkPaint::kNo_Hinting);
389 } else {
390 switch (fPaint.getHinting()) {
391 case SkPaint::kNo_Hinting:
392 fPaint.setHinting(SkPaint::kSlight_Hinting);
393 break;
394 case SkPaint::kSlight_Hinting:
395 fPaint.setHinting(SkPaint::kNormal_Hinting);
396 break;
397 case SkPaint::kNormal_Hinting:
398 fPaint.setHinting(SkPaint::kFull_Hinting);
399 break;
400 case SkPaint::kFull_Hinting:
401 fPaint.setHinting(SkPaint::kNo_Hinting);
402 fPaintOverrides.fHinting = false;
403 break;
404 }
405 }
406 this->updateTitle();
407 fWindow->inval();
408 });
409 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
410 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
411 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
412 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
413 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500414 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
415 gSkUseDeltaAA = gSkForceDeltaAA = false;
416 } else {
417 fPaint.setAntiAlias(true);
418 switch (fPaintOverrides.fAntiAlias) {
419 case SkPaintFields::AntiAliasState::Alias:
420 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400421 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
422 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500423 break;
424 case SkPaintFields::AntiAliasState::Normal:
425 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500426 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400427 gSkForceAnalyticAA = false;
428 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500429 break;
430 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
431 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400432 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
433 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500434 break;
435 case SkPaintFields::AntiAliasState::AnalyticAAForced:
436 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
437 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
438 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400439 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500440 break;
441 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
442 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400443 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
444 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500445 break;
446 case SkPaintFields::AntiAliasState::DeltaAAForced:
447 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
448 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
449 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
450 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
451 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
452 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
453 break;
454 }
455 }
456 this->updateTitle();
457 fWindow->inval();
458 });
Ben Wagner37c54032018-04-13 14:30:23 -0400459 fCommands.addCommand('D', "Modes", "DFT", [this]() {
460 DisplayParams params = fWindow->getRequestedDisplayParams();
461 uint32_t flags = params.fSurfaceProps.flags();
462 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
463 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
464 fWindow->setRequestedDisplayParams(params);
465 this->updateTitle();
466 fWindow->inval();
467 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500468 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
469 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
470 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
471 fPaint.setLCDRenderText(false);
472 } else {
473 if (!fPaint.isLCDRenderText()) {
474 fPaint.setLCDRenderText(true);
475 } else {
476 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
477 }
478 }
479 this->updateTitle();
480 fWindow->inval();
481 });
482 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
483 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
484 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
485 fPaint.setSubpixelText(false);
486 } else {
487 if (!fPaint.isSubpixelText()) {
488 fPaint.setSubpixelText(true);
489 } else {
490 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
491 }
492 }
493 this->updateTitle();
494 fWindow->inval();
495 });
Brian Osman805a7272018-05-02 15:40:20 -0400496 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
497 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
498 : kPerspective_Real;
499 this->updateTitle();
500 fWindow->inval();
501 });
502 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
503 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
504 : kPerspective_Off;
505 this->updateTitle();
506 fWindow->inval();
507 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500508
jvanverth2bb3b6d2016-04-08 07:24:09 -0700509 // set up slides
510 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500511 if (FLAGS_list) {
512 this->listNames();
513 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700514
Brian Osman9bb47cf2018-04-26 15:55:00 -0400515 fPerspectivePoints[0].set(0, 0);
516 fPerspectivePoints[1].set(1, 0);
517 fPerspectivePoints[2].set(0, 1);
518 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700519 fAnimTimer.run();
520
Hal Canaryc465d132017-12-08 10:21:31 -0500521 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500522 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400523 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500524 }
525 fImGuiGamutPaint.setColor(SK_ColorWHITE);
526 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
527
Brian Osman70d2f432017-11-08 09:54:10 -0500528 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500529 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700530}
531
jvanverth34524262016-05-04 13:49:13 -0700532void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700533 fAllSlideNames = Json::Value(Json::arrayValue);
534
Florin Malita0ffa3222018-04-05 14:34:45 -0400535 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
536 static const struct {
537 const char* fExtension;
538 const char* fDirName;
539 const SkCommandLineFlags::StringArray& fFlags;
540 const SlideFactory fFactory;
541 } gExternalSlidesInfo[] = {
542 { ".skp", "skp-dir", FLAGS_skps,
543 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
544 return sk_make_sp<SKPSlide>(name, path);}
545 },
546 { ".jpg", "jpg-dir", FLAGS_jpgs,
547 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
548 return sk_make_sp<ImageSlide>(name, path);}
549 },
Florin Malita87ccf332018-05-04 12:23:24 -0400550#if defined(SK_ENABLE_SKOTTIE)
Florin Malita0ffa3222018-04-05 14:34:45 -0400551 { ".json", "skottie-dir", FLAGS_jsons,
552 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
553 return sk_make_sp<SkottieSlide>(name, path);}
554 },
Florin Malita87ccf332018-05-04 12:23:24 -0400555#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400556 { ".svg", "svg-dir", FLAGS_svgs,
557 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
558 return sk_make_sp<SvgSlide>(name, path);}
559 },
560 };
jvanverthc265a922016-04-08 12:51:45 -0700561
Florin Malita0ffa3222018-04-05 14:34:45 -0400562 SkTArray<sk_sp<Slide>, true> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700563
Florin Malita0ffa3222018-04-05 14:34:45 -0400564 const auto addSlide = [&](const SkString& name,
565 const SkString& path,
566 const SlideFactory& fact) {
567 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
568 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700569 }
liyuqian6f163d22016-06-13 12:26:45 -0700570
Florin Malita0ffa3222018-04-05 14:34:45 -0400571 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500572 dirSlides.push_back(slide);
573 fSlides.push_back(std::move(slide));
574 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400575 };
Florin Malita76a076b2018-02-15 18:40:48 -0500576
Florin Malita38792ce2018-05-08 10:36:18 -0400577 if (!FLAGS_file.isEmpty()) {
578 // single file mode
579 const SkString file(FLAGS_file[0]);
580
581 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
582 for (const auto& sinfo : gExternalSlidesInfo) {
583 if (file.endsWith(sinfo.fExtension)) {
584 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
585 return;
586 }
587 }
588
589 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
590 } else {
591 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
592 }
593
594 return;
595 }
596
597 // Bisect slide.
598 if (!FLAGS_bisect.isEmpty()) {
599 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
600 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
601 if (FLAGS_bisect.count() >= 2) {
602 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
603 bisect->onChar(*ch);
604 }
605 }
606 fSlides.push_back(std::move(bisect));
607 }
608 }
609
610 // GMs
611 int firstGM = fSlides.count();
612 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
613 while (gms) {
614 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
615
616 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
617 sk_sp<Slide> slide(new GMSlide(gm.release()));
618 fSlides.push_back(std::move(slide));
619 }
620
621 gms = gms->next();
622 }
623 // reverse gms
624 int numGMs = fSlides.count() - firstGM;
625 for (int i = 0; i < numGMs/2; ++i) {
626 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
627 }
628
629 // samples
630 const SkViewRegister* reg = SkViewRegister::Head();
631 while (reg) {
632 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
633 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
634 fSlides.push_back(slide);
635 }
636 reg = reg->next();
637 }
638
Florin Malita0ffa3222018-04-05 14:34:45 -0400639 for (const auto& info : gExternalSlidesInfo) {
640 for (const auto& flag : info.fFlags) {
641 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
642 // single file
643 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
644 } else {
645 // directory
646 SkOSFile::Iter it(flag.c_str(), info.fExtension);
647 SkString name;
648 while (it.next(&name)) {
649 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
650 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400651 }
Florin Malita3b526b02018-05-25 12:43:51 -0400652#if defined(SK_HAS_SKSG)
Florin Malita0ffa3222018-04-05 14:34:45 -0400653 if (!dirSlides.empty()) {
654 fSlides.push_back(
655 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
656 std::move(dirSlides)));
657 dirSlides.reset();
658 }
Florin Malita3b526b02018-05-25 12:43:51 -0400659#endif
Florin Malitac659c2c2018-04-05 11:57:21 -0400660 }
661 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700662}
663
664
jvanverth34524262016-05-04 13:49:13 -0700665Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700666 fWindow->detach();
667 delete fWindow;
668}
669
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500670struct SkPaintTitleUpdater {
671 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
672 void append(const char* s) {
673 if (fCount == 0) {
674 fTitle->append(" {");
675 } else {
676 fTitle->append(", ");
677 }
678 fTitle->append(s);
679 ++fCount;
680 }
681 void done() {
682 if (fCount > 0) {
683 fTitle->append("}");
684 }
685 }
686 SkString* fTitle;
687 int fCount;
688};
689
brianosman05de2162016-05-06 13:28:57 -0700690void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700691 if (!fWindow) {
692 return;
693 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500694 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700695 return; // Surface hasn't been created yet.
696 }
697
jvanverth34524262016-05-04 13:49:13 -0700698 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700699 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700700
Yuqian Li399b3c22017-08-03 11:08:15 -0400701 if (gSkUseDeltaAA) {
702 if (gSkForceDeltaAA) {
703 title.append(" <FDAA>");
704 } else {
705 title.append(" <DAA>");
706 }
707 } else if (gSkUseAnalyticAA) {
708 if (gSkForceAnalyticAA) {
709 title.append(" <FAAA>");
710 } else {
711 title.append(" <AAA>");
712 }
713 }
714
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500715 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner99a78dc2018-05-09 18:23:51 -0400716 auto paintFlag = [this, &paintTitle](SkPaint::Flags flag, bool (SkPaint::* isFlag)() const,
717 const char* on, const char* off)
718 {
719 if (fPaintOverrides.fFlags & flag) {
720 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500721 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400722 };
723
724 paintFlag(SkPaint::kAntiAlias_Flag, &SkPaint::isAntiAlias, "Antialias", "Alias");
725 paintFlag(SkPaint::kDither_Flag, &SkPaint::isDither, "DITHER", "No Dither");
726 paintFlag(SkPaint::kFakeBoldText_Flag, &SkPaint::isFakeBoldText, "Fake Bold", "No Fake Bold");
727 paintFlag(SkPaint::kLinearText_Flag, &SkPaint::isLinearText, "Linear Text", "Non-Linear Text");
728 paintFlag(SkPaint::kSubpixelText_Flag, &SkPaint::isSubpixelText, "Subpixel Text", "Pixel Text");
729 paintFlag(SkPaint::kLCDRenderText_Flag, &SkPaint::isLCDRenderText, "LCD", "lcd");
730 paintFlag(SkPaint::kEmbeddedBitmapText_Flag, &SkPaint::isEmbeddedBitmapText,
731 "Bitmap Text", "No Bitmap Text");
732 paintFlag(SkPaint::kAutoHinting_Flag, &SkPaint::isAutohinted,
733 "Force Autohint", "No Force Autohint");
734 paintFlag(SkPaint::kVerticalText_Flag, &SkPaint::isVerticalText,
735 "Vertical Text", "No Vertical Text");
736
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500737 if (fPaintOverrides.fHinting) {
738 switch (fPaint.getHinting()) {
739 case SkPaint::kNo_Hinting:
740 paintTitle.append("No Hinting");
741 break;
742 case SkPaint::kSlight_Hinting:
743 paintTitle.append("Slight Hinting");
744 break;
745 case SkPaint::kNormal_Hinting:
746 paintTitle.append("Normal Hinting");
747 break;
748 case SkPaint::kFull_Hinting:
749 paintTitle.append("Full Hinting");
750 break;
751 }
752 }
753 paintTitle.done();
754
Yuqian Lib2ba6642017-11-22 12:07:41 -0500755 if (fTileCnt > 0) {
756 title.appendf(" T%d", fTileCnt);
757 if (fThreadCnt > 0) {
758 title.appendf("/%d", fThreadCnt);
759 }
760 }
761
Brian Osman92004802017-03-06 11:47:26 -0500762 switch (fColorMode) {
763 case ColorMode::kLegacy:
764 title.append(" Legacy 8888");
765 break;
766 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
767 title.append(" ColorManaged 8888 (Nonlinear blending)");
768 break;
769 case ColorMode::kColorManagedSRGB8888:
770 title.append(" ColorManaged 8888");
771 break;
772 case ColorMode::kColorManagedLinearF16:
773 title.append(" ColorManaged F16");
774 break;
775 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500776
Brian Osman92004802017-03-06 11:47:26 -0500777 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500778 int curPrimaries = -1;
779 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
780 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
781 curPrimaries = i;
782 break;
783 }
784 }
785 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500786
787 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
788 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
789 }
brianosman05de2162016-05-06 13:28:57 -0700790 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500791
Ben Wagner37c54032018-04-13 14:30:23 -0400792 const DisplayParams& params = fWindow->getRequestedDisplayParams();
793 if (fPixelGeometryOverrides) {
794 switch (params.fSurfaceProps.pixelGeometry()) {
795 case kUnknown_SkPixelGeometry:
796 title.append( " Flat");
797 break;
798 case kRGB_H_SkPixelGeometry:
799 title.append( " RGB");
800 break;
801 case kBGR_H_SkPixelGeometry:
802 title.append( " BGR");
803 break;
804 case kRGB_V_SkPixelGeometry:
805 title.append( " RGBV");
806 break;
807 case kBGR_V_SkPixelGeometry:
808 title.append( " BGRV");
809 break;
810 }
811 }
812
813 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
814 title.append(" DFT");
815 }
816
csmartdalton578f0642017-02-24 16:04:47 -0700817 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700818 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500819 int msaa = fWindow->sampleCount();
820 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700821 title.appendf(" MSAA: %i", msaa);
822 }
823 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700824
825 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400826 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700827 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
828 }
829
Brian Osman805a7272018-05-02 15:40:20 -0400830 if (kPerspective_Real == fPerspectiveMode) {
831 title.append(" Perpsective (Real)");
832 } else if (kPerspective_Fake == fPerspectiveMode) {
833 title.append(" Perspective (Fake)");
834 }
835
brianosman05de2162016-05-06 13:28:57 -0700836 fWindow->setTitle(title.c_str());
837}
838
Florin Malitaab99c342018-01-16 16:23:03 -0500839int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500840
841 if (!FLAGS_slide.isEmpty()) {
842 int count = fSlides.count();
843 for (int i = 0; i < count; i++) {
844 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500845 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500846 }
847 }
848
849 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
850 this->listNames();
851 }
852
Florin Malitaab99c342018-01-16 16:23:03 -0500853 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500854}
855
Florin Malitaab99c342018-01-16 16:23:03 -0500856void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500857 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500858 for (const auto& slide : fSlides) {
859 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500860 }
861}
862
Florin Malitaab99c342018-01-16 16:23:03 -0500863void Viewer::setCurrentSlide(int slide) {
864 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700865
Florin Malitaab99c342018-01-16 16:23:03 -0500866 if (slide == fCurrentSlide) {
867 return;
868 }
869
870 if (fCurrentSlide >= 0) {
871 fSlides[fCurrentSlide]->unload();
872 }
873
874 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
875 SkIntToScalar(fWindow->height()));
876 fCurrentSlide = slide;
877 this->setupCurrentSlide();
878}
879
880void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500881 if (fCurrentSlide >= 0) {
882 // prepare dimensions for image slides
883 fGesture.resetTouchState();
884 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700885
Jim Van Verth0848fb02018-01-22 13:39:30 -0500886 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
887 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
888 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400889
Jim Van Verth0848fb02018-01-22 13:39:30 -0500890 // Start with a matrix that scales the slide to the available screen space
891 if (fWindow->scaleContentToFit()) {
892 if (windowRect.width() > 0 && windowRect.height() > 0) {
893 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
894 }
liyuqiane46e4f02016-05-20 07:32:19 -0700895 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500896
897 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400898 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500899
900 this->updateTitle();
901 this->updateUIState();
902
903 fStatsLayer.resetMeasurements();
904
905 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700906 }
jvanverthc265a922016-04-08 12:51:45 -0700907}
908
909#define MAX_ZOOM_LEVEL 8
910#define MIN_ZOOM_LEVEL -8
911
jvanverth34524262016-05-04 13:49:13 -0700912void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700913 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400914 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400915 this->preTouchMatrixChanged();
916}
Yuqian Li755778c2018-03-28 16:23:31 -0400917
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400918void Viewer::preTouchMatrixChanged() {
919 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400920 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
921 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
922 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
923 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
924}
925
Brian Osman805a7272018-05-02 15:40:20 -0400926SkMatrix Viewer::computePerspectiveMatrix() {
927 SkScalar w = fWindow->width(), h = fWindow->height();
928 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
929 SkPoint perspPts[4] = {
930 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
931 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
932 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
933 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
934 };
935 SkMatrix m;
936 m.setPolyToPoly(orthoPts, perspPts, 4);
937 return m;
938}
939
Yuqian Li755778c2018-03-28 16:23:31 -0400940SkMatrix Viewer::computePreTouchMatrix() {
941 SkMatrix m = fDefaultMatrix;
942 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
943 : SK_Scalar1 + fZoomLevel;
944 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400945
946 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
947 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400948
Brian Osman805a7272018-05-02 15:40:20 -0400949 if (kPerspective_Real == fPerspectiveMode) {
950 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400951 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400952 }
953
Yuqian Li755778c2018-03-28 16:23:31 -0400954 return m;
jvanverthc265a922016-04-08 12:51:45 -0700955}
956
liyuqiand3cdbca2016-05-17 12:44:20 -0700957SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400958 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700959 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400960 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700961 return m;
jvanverthc265a922016-04-08 12:51:45 -0700962}
963
Brian Osman621491e2017-02-28 15:45:01 -0500964void Viewer::setBackend(sk_app::Window::BackendType backendType) {
965 fBackendType = backendType;
966
967 fWindow->detach();
968
Brian Osman70d2f432017-11-08 09:54:10 -0500969#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400970 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
971 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500972 DisplayParams params = fWindow->getRequestedDisplayParams();
973 delete fWindow;
974 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500975
Brian Osman70d2f432017-11-08 09:54:10 -0500976 // re-register callbacks
977 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500978 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500979 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500980 fWindow->pushLayer(&fImGuiLayer);
981
Brian Osman70d2f432017-11-08 09:54:10 -0500982 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
983 // will still include our correct sample count. But the re-created fWindow will lose that
984 // information. On Windows, we need to re-create the window when changing sample count,
985 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
986 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
987 // as if we had never changed windows at all).
988 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500989#endif
990
Brian Osman70d2f432017-11-08 09:54:10 -0500991 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500992}
993
Brian Osman92004802017-03-06 11:47:26 -0500994void Viewer::setColorMode(ColorMode colorMode) {
995 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700996
Brian Osmanf750fbc2017-02-08 10:47:28 -0500997 // When we're in color managed mode, we tag our window surface as sRGB. If we've switched into
Brian Osmane0d4fba2017-03-15 10:24:55 -0400998 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700999 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -05001000 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001001 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
1002 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -05001003 if (wasInLegacy != wantLegacy) {
1004 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -07001005 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001006 }
1007
1008 this->updateTitle();
1009 fWindow->inval();
1010}
1011
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001012class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1013public:
1014 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
1015 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
1016 { }
1017 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001018 if (*paint == nullptr) {
1019 return true;
1020 }
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001021 if (fPaintOverrides->fTextSize) {
1022 paint->writable()->setTextSize(fPaint->getTextSize());
1023 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001024 if (fPaintOverrides->fHinting) {
1025 paint->writable()->setHinting(fPaint->getHinting());
1026 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001027
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001028 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
1029 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1030 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001031 if (fPaintOverrides->fFlags & SkPaint::kDither_Flag) {
1032 paint->writable()->setDither(fPaint->isDither());
1033 }
1034 if (fPaintOverrides->fFlags & SkPaint::kFakeBoldText_Flag) {
1035 paint->writable()->setFakeBoldText(fPaint->isFakeBoldText());
1036 }
1037 if (fPaintOverrides->fFlags & SkPaint::kLinearText_Flag) {
1038 paint->writable()->setLinearText(fPaint->isLinearText());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001039 }
1040 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
1041 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
1042 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001043 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
1044 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
1045 }
1046 if (fPaintOverrides->fFlags & SkPaint::kEmbeddedBitmapText_Flag) {
1047 paint->writable()->setEmbeddedBitmapText(fPaint->isEmbeddedBitmapText());
1048 }
1049 if (fPaintOverrides->fFlags & SkPaint::kAutoHinting_Flag) {
1050 paint->writable()->setAutohinted(fPaint->isAutohinted());
1051 }
1052 if (fPaintOverrides->fFlags & SkPaint::kVerticalText_Flag) {
1053 paint->writable()->setVerticalText(fPaint->isVerticalText());
1054 }
1055
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001056 return true;
1057 }
1058 SkPaint* fPaint;
1059 Viewer::SkPaintFields* fPaintOverrides;
1060};
1061
Brian Osmanf750fbc2017-02-08 10:47:28 -05001062void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001063 SkAutoCanvasRestore autorestore(canvas, false);
1064
Brian Osmanf750fbc2017-02-08 10:47:28 -05001065 // By default, we render directly into the window's surface/canvas
1066 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001067 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001068
Brian Osmane0d4fba2017-03-15 10:24:55 -04001069 // If we're in any of the color managed modes, construct the color space we're going to use
1070 sk_sp<SkColorSpace> cs = nullptr;
1071 if (ColorMode::kLegacy != fColorMode) {
1072 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
1073 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -04001074 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001075 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -05001076 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1077 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
1078 } else {
1079 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
1080 }
Brian Osmane0d4fba2017-03-15 10:24:55 -04001081 }
1082
Brian Osman3ac99cf2017-12-01 11:23:53 -05001083 if (fSaveToSKP) {
1084 SkPictureRecorder recorder;
1085 SkCanvas* recorderCanvas = recorder.beginRecording(
1086 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1087 // In xform-canvas mode, record the transformed output
1088 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1089 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1090 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
1091 recorderCanvas = xformCanvas.get();
1092 }
1093 fSlides[fCurrentSlide]->draw(recorderCanvas);
1094 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1095 SkFILEWStream stream("sample_app.skp");
1096 picture->serialize(&stream);
1097 fSaveToSKP = false;
1098 }
1099
Brian Osmane0d4fba2017-03-15 10:24:55 -04001100 // 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 -05001101 // 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 -04001102 // because the window surface is actually GL, or we're doing fake perspective.
Brian Osmanf750fbc2017-02-08 10:47:28 -05001103 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -05001104 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -05001105 if (Window::kRaster_BackendType == fBackendType ||
Brian Osman805a7272018-05-02 15:40:20 -04001106 kPerspective_Fake == fPerspectiveMode ||
Brian Osman70d2f432017-11-08 09:54:10 -05001107 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -05001108 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -04001109 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
1110 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
1111
Brian Osman92004802017-03-06 11:47:26 -05001112 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
1113 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001114 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
1115 // with a special canvas (below) that has the color space attached
1116 sk_sp<SkColorSpace> offscreenColorSpace =
1117 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -05001118 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -04001119 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Ben Wagner37c54032018-04-13 14:30:23 -04001120 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1121 canvas->getProps(&props);
1122 offscreenSurface = Window::kRaster_BackendType == fBackendType
1123 ? SkSurface::MakeRaster(info, &props)
1124 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -05001125 SkPixmap offscreenPixmap;
1126 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
1127 SkBitmap offscreenBitmap;
1128 offscreenBitmap.installPixels(offscreenPixmap);
Herb Derbyefe39bc2018-05-01 17:06:20 -04001129 threadedCanvas =
1130 skstd::make_unique<SkCanvas>(
1131 sk_make_sp<SkThreadedBMPDevice>(
1132 offscreenBitmap, fTileCnt, fThreadCnt, fExecutor.get()));
Yuqian Lib2ba6642017-11-22 12:07:41 -05001133 slideCanvas = threadedCanvas.get();
1134 } else {
1135 slideCanvas = offscreenSurface->getCanvas();
1136 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001137 }
1138
Brian Osmane0d4fba2017-03-15 10:24:55 -04001139 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1140 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1141 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
1142 slideCanvas = xformCanvas.get();
1143 }
1144
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001145 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001146 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001147 slideCanvas->concat(computeMatrix());
Brian Osmane19e23f2018-05-22 16:56:40 -04001148 if (kPerspective_Real == fPerspectiveMode) {
1149 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1150 }
Brian Osman1df161a2017-02-09 12:10:20 -05001151 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001152 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001153 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1154 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001155 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001156 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001157
1158 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001159 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001160 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001161 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001162
1163 // If we rendered offscreen, snap an image and push the results to the window's canvas
1164 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001165 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001166
1167 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -04001168 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001169 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001170 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001171 SkPaint paint;
1172 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001173 int prePerspectiveCount = canvas->save();
1174 if (kPerspective_Fake == fPerspectiveMode) {
1175 paint.setFilterQuality(kHigh_SkFilterQuality);
1176 canvas->clear(SK_ColorWHITE);
1177 canvas->concat(this->computePerspectiveMatrix());
1178 }
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001179 canvas->drawImage(retaggedImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001180 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001181 }
liyuqian6f163d22016-06-13 12:26:45 -07001182}
1183
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001184void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001185 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001186 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001187}
Jim Van Verth6f449692017-02-14 15:16:46 -05001188
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001189void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001190 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001191
brianosman622c8d52016-05-10 06:50:49 -07001192 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001193
Brian Osmand67e5182017-12-08 16:46:09 -05001194 this->drawImGui();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001195}
1196
Florin Malitacefc1b92018-02-19 21:43:47 -05001197SkPoint Viewer::mapEvent(float x, float y) {
1198 const auto m = this->computeMatrix();
1199 SkMatrix inv;
1200
1201 SkAssertResult(m.invert(&inv));
1202
1203 return inv.mapXY(x, y);
1204}
1205
jvanverth814e38d2016-06-06 08:48:47 -07001206bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001207 if (GestureDevice::kMouse == fGestureDevice) {
1208 return false;
1209 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001210
1211 const auto slidePt = this->mapEvent(x, y);
1212 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1213 fWindow->inval();
1214 return true;
1215 }
1216
liyuqiand3cdbca2016-05-17 12:44:20 -07001217 void* castedOwner = reinterpret_cast<void*>(owner);
1218 switch (state) {
1219 case Window::kUp_InputState: {
1220 fGesture.touchEnd(castedOwner);
1221 break;
1222 }
1223 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001224 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001225 break;
1226 }
1227 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001228 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001229 break;
1230 }
1231 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001232 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001233 fWindow->inval();
1234 return true;
1235}
1236
Brian Osman80fc07e2017-12-08 16:45:43 -05001237bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001238 if (GestureDevice::kTouch == fGestureDevice) {
1239 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001240 }
Brian Osman16c81a12017-12-20 11:58:34 -05001241
Florin Malitacefc1b92018-02-19 21:43:47 -05001242 const auto slidePt = this->mapEvent(x, y);
1243 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1244 fWindow->inval();
1245 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001246 }
1247
1248 switch (state) {
1249 case Window::kUp_InputState: {
1250 fGesture.touchEnd(nullptr);
1251 break;
1252 }
1253 case Window::kDown_InputState: {
1254 fGesture.touchBegin(nullptr, x, y);
1255 break;
1256 }
1257 case Window::kMove_InputState: {
1258 fGesture.touchMoved(nullptr, x, y);
1259 break;
1260 }
1261 }
1262 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1263
1264 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1265 fWindow->inval();
1266 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001267 return true;
1268}
1269
Brian Osmana109e392017-02-24 09:49:14 -05001270static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1271 const ImVec2& pos, const ImVec2& size) {
1272 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1273 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1274
1275 // Invisible 10x10 button
1276 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1277 ImGui::InvisibleButton(label, ImVec2(10, 10));
1278
1279 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1280 ImGuiIO& io = ImGui::GetIO();
1281 // Normalized mouse position, relative to our gamut box
1282 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1283 // Clamp to edge of box, convert back to primary scale
1284 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1285 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1286 }
1287
1288 if (ImGui::IsItemHovered()) {
1289 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1290 }
1291
1292 // Return screen coordinates for the caller. We could just return center here, but we'd have
1293 // one frame of lag during drag.
1294 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1295}
1296
1297static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1298 ImDrawList* drawList = ImGui::GetWindowDrawList();
1299
1300 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1301 // width, and scale the height to maintain aspect ratio.
1302 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1303 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1304 ImVec2 pos = ImGui::GetCursorScreenPos();
1305
1306 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1307 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1308 // Magic numbers are pixel locations of the origin and upper-right corner.
1309 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1310 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001311
1312 // Primary markers
1313 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1314 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1315 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1316 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1317
1318 // Gamut triangle
1319 drawList->AddCircle(r, 5.0f, 0xFF000040);
1320 drawList->AddCircle(g, 5.0f, 0xFF004000);
1321 drawList->AddCircle(b, 5.0f, 0xFF400000);
1322 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1323 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1324
1325 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001326 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1327}
1328
1329static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1330 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1331 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1332 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1333
1334 // Invisible 10x10 button
1335 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1336 ImGui::InvisibleButton(label, ImVec2(10, 10));
1337
1338 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1339 ImGuiIO& io = ImGui::GetIO();
1340 // Normalized mouse position, relative to our gamut box
1341 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1342 // Clamp to edge of box
1343 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1344 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1345 *dragging = true;
1346 }
1347
1348 // Return screen coordinates for the caller. We could just return center here, but we'd have
1349 // one frame of lag during drag.
1350 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1351}
1352
1353static bool ImGui_DragQuad(SkPoint* pts) {
1354 ImDrawList* drawList = ImGui::GetWindowDrawList();
1355
1356 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1357 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1358 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1359 ImVec2 pos = ImGui::GetCursorScreenPos();
1360
1361 // Background rectangle
1362 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1363
1364 // Corner markers
1365 bool dragging = false;
1366 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1367 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1368 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1369 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1370
1371 // Draw markers and quad
1372 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1373 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1374 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1375 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1376 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1377 drawList->AddLine(tr, br, 0xFFFFFFFF);
1378 drawList->AddLine(br, bl, 0xFFFFFFFF);
1379 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1380
1381 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1382 ImGui::Spacing();
1383
1384 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001385}
1386
Brian Osmand67e5182017-12-08 16:46:09 -05001387void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001388 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1389 if (fShowImGuiTestWindow) {
1390 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1391 }
1392
1393 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001394 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1395 // always visible, we can end up in a layout feedback loop.
1396 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001397 DisplayParams params = fWindow->getRequestedDisplayParams();
1398 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001399 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1400 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001401 if (ImGui::CollapsingHeader("Backend")) {
1402 int newBackend = static_cast<int>(fBackendType);
1403 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1404 ImGui::SameLine();
1405 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001406#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1407 ImGui::SameLine();
1408 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1409#endif
Brian Osman621491e2017-02-28 15:45:01 -05001410#if defined(SK_VULKAN)
1411 ImGui::SameLine();
1412 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1413#endif
1414 if (newBackend != fBackendType) {
1415 fDeferredActions.push_back([=]() {
1416 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1417 });
1418 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001419
Brian Salomon99a33902017-03-07 15:16:34 -05001420 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001421 bool* wire = &params.fGrContextOptions.fWireframeMode;
1422 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1423 paramsChanged = true;
1424 }
Brian Salomon99a33902017-03-07 15:16:34 -05001425
Brian Osman28b12522017-03-08 17:10:24 -05001426 if (ctx) {
1427 int sampleCount = fWindow->sampleCount();
1428 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001429 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001430 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1431 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1432 ImGui::RadioButton("16", &sampleCount, 16);
1433
1434 if (sampleCount != params.fMSAASampleCount) {
1435 params.fMSAASampleCount = sampleCount;
1436 paramsChanged = true;
1437 }
1438 }
1439
Ben Wagner37c54032018-04-13 14:30:23 -04001440 int pixelGeometryIdx = 0;
1441 if (fPixelGeometryOverrides) {
1442 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1443 }
1444 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1445 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1446 {
1447 uint32_t flags = params.fSurfaceProps.flags();
1448 if (pixelGeometryIdx == 0) {
1449 fPixelGeometryOverrides = false;
1450 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1451 } else {
1452 fPixelGeometryOverrides = true;
1453 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1454 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1455 }
1456 paramsChanged = true;
1457 }
1458
1459 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1460 if (ImGui::Checkbox("DFT", &useDFT)) {
1461 uint32_t flags = params.fSurfaceProps.flags();
1462 if (useDFT) {
1463 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1464 } else {
1465 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1466 }
1467 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1468 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1469 paramsChanged = true;
1470 }
1471
Brian Osman8a9de3d2017-03-01 14:59:05 -05001472 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001473 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001474 auto prButton = [&](GpuPathRenderers x) {
1475 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001476 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1477 params.fGrContextOptions.fGpuPathRenderers = x;
1478 paramsChanged = true;
1479 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001480 }
1481 };
1482
1483 if (!ctx) {
1484 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001485 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001486 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001487 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001488 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001489 prButton(GpuPathRenderers::kStencilAndCover);
1490 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001491 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001492 prButton(GpuPathRenderers::kNone);
1493 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001494 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001495 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001496 if (GrCoverageCountingPathRenderer::IsSupported(
1497 *ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001498 prButton(GpuPathRenderers::kCoverageCounting);
1499 }
Jim Van Verth83010462017-03-16 08:45:39 -04001500 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001501 prButton(GpuPathRenderers::kTessellating);
1502 prButton(GpuPathRenderers::kNone);
1503 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001504 ImGui::TreePop();
1505 }
Brian Osman621491e2017-02-28 15:45:01 -05001506 }
1507
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001508 if (ImGui::CollapsingHeader("Transform")) {
1509 float zoom = fZoomLevel;
1510 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1511 fZoomLevel = zoom;
1512 this->preTouchMatrixChanged();
1513 paramsChanged = true;
1514 }
1515 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001516 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001517 fRotation = deg;
1518 this->preTouchMatrixChanged();
1519 paramsChanged = true;
1520 }
Brian Osman805a7272018-05-02 15:40:20 -04001521 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1522 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1523 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001524 this->preTouchMatrixChanged();
Brian Osman805a7272018-05-02 15:40:20 -04001525 this->updateTitle();
Brian Osman9bb47cf2018-04-26 15:55:00 -04001526 }
1527 if (ImGui_DragQuad(fPerspectivePoints)) {
1528 this->preTouchMatrixChanged();
1529 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001530 }
1531
Ben Wagnera580fb32018-04-17 11:16:32 -04001532 if (ImGui::CollapsingHeader("Paint")) {
1533 int hintingIdx = 0;
1534 if (fPaintOverrides.fHinting) {
1535 hintingIdx = fPaint.getHinting() + 1;
1536 }
1537 if (ImGui::Combo("Hinting", &hintingIdx,
1538 "Default\0None\0Slight\0Normal\0Full\0\0"))
1539 {
1540 if (hintingIdx == 0) {
1541 fPaintOverrides.fHinting = false;
1542 fPaint.setHinting(SkPaint::kNo_Hinting);
1543 } else {
1544 fPaintOverrides.fHinting = true;
1545 SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1);
1546 fPaint.setHinting(hinting);
1547 }
1548 paramsChanged = true;
1549 }
1550
1551 int aliasIdx = 0;
1552 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1553 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1554 }
1555 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1556 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1557 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1558 {
1559 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1560 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1561 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1562 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1563 if (aliasIdx == 0) {
1564 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1565 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1566 } else {
1567 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1568 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1569 fPaint.setAntiAlias(aliasIdx > 1);
1570 switch (fPaintOverrides.fAntiAlias) {
1571 case SkPaintFields::AntiAliasState::Alias:
1572 break;
1573 case SkPaintFields::AntiAliasState::Normal:
1574 break;
1575 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1576 gSkUseAnalyticAA = true;
1577 gSkForceAnalyticAA = false;
1578 gSkUseDeltaAA = gSkForceDeltaAA = false;
1579 break;
1580 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1581 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1582 gSkUseDeltaAA = gSkForceDeltaAA = false;
1583 break;
1584 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1585 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1586 gSkUseDeltaAA = true;
1587 gSkForceDeltaAA = false;
1588 break;
1589 case SkPaintFields::AntiAliasState::DeltaAAForced:
1590 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1591 gSkUseDeltaAA = gSkForceDeltaAA = true;
1592 break;
1593 }
1594 }
1595 paramsChanged = true;
1596 }
1597
Ben Wagner99a78dc2018-05-09 18:23:51 -04001598 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
1599 SkPaint::Flags flag,
1600 bool (SkPaint::* isFlag)() const,
1601 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001602 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001603 int itemIndex = 0;
1604 if (fPaintOverrides.fFlags & flag) {
1605 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001606 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001607 if (ImGui::Combo(label, &itemIndex, items)) {
1608 if (itemIndex == 0) {
1609 fPaintOverrides.fFlags &= ~flag;
1610 } else {
1611 fPaintOverrides.fFlags |= flag;
1612 (fPaint.*setFlag)(itemIndex == 2);
1613 }
1614 paramsChanged = true;
1615 }
1616 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001617
Ben Wagner99a78dc2018-05-09 18:23:51 -04001618 paintFlag("Dither",
1619 "Default\0No Dither\0Dither\0\0",
1620 SkPaint::kDither_Flag,
1621 &SkPaint::isDither, &SkPaint::setDither);
1622
1623 paintFlag("Fake Bold Glyphs",
1624 "Default\0No Fake Bold\0Fake Bold\0\0",
1625 SkPaint::kFakeBoldText_Flag,
1626 &SkPaint::isFakeBoldText, &SkPaint::setFakeBoldText);
1627
1628 paintFlag("Linear Text",
1629 "Default\0No Linear Text\0Linear Text\0\0",
1630 SkPaint::kLinearText_Flag,
1631 &SkPaint::isLinearText, &SkPaint::setLinearText);
1632
1633 paintFlag("Subpixel Position Glyphs",
1634 "Default\0Pixel Text\0Subpixel Text\0\0",
1635 SkPaint::kSubpixelText_Flag,
1636 &SkPaint::isSubpixelText, &SkPaint::setSubpixelText);
1637
1638 paintFlag("Subpixel Anti-Alias",
1639 "Default\0lcd\0LCD\0\0",
1640 SkPaint::kLCDRenderText_Flag,
1641 &SkPaint::isLCDRenderText, &SkPaint::setLCDRenderText);
1642
1643 paintFlag("Embedded Bitmap Text",
1644 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1645 SkPaint::kEmbeddedBitmapText_Flag,
1646 &SkPaint::isEmbeddedBitmapText, &SkPaint::setEmbeddedBitmapText);
1647
1648 paintFlag("Force Auto-Hinting",
1649 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1650 SkPaint::kAutoHinting_Flag,
1651 &SkPaint::isAutohinted, &SkPaint::setAutohinted);
1652
1653 paintFlag("Vertical Text",
1654 "Default\0No Vertical Text\0Vertical Text\0\0",
1655 SkPaint::kVerticalText_Flag,
1656 &SkPaint::isVerticalText, &SkPaint::setVerticalText);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001657
1658 ImGui::Checkbox("Override TextSize", &fPaintOverrides.fTextSize);
1659 if (fPaintOverrides.fTextSize) {
1660 ImGui::DragFloat2("TextRange", fPaintOverrides.fTextSizeRange,
1661 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
1662 float textSize = fPaint.getTextSize();
1663 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
1664 fPaintOverrides.fTextSizeRange[0],
1665 fPaintOverrides.fTextSizeRange[1],
1666 "%.6f", 2.0f))
1667 {
1668 fPaint.setTextSize(textSize);
1669 this->preTouchMatrixChanged();
1670 paramsChanged = true;
1671 }
1672 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001673 }
1674
Mike Reed81f60ec2018-05-15 10:09:52 -04001675 {
1676 SkMetaData controls;
1677 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1678 if (ImGui::CollapsingHeader("Current Slide")) {
1679 SkMetaData::Iter iter(controls);
1680 const char* name;
1681 SkMetaData::Type type;
1682 int count;
1683 bool found = false;
1684 while ((name = iter.next(&type, &count)) != nullptr && found == false) {
1685 if (type == SkMetaData::kScalar_Type) {
1686 float val[3];
1687 SkASSERT(count == 3);
1688 controls.findScalars(name, &count, val);
1689 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1690 controls.setScalars(name, 3, val);
1691 fSlides[fCurrentSlide]->onSetControls(controls);
1692 found = paramsChanged = true;
1693 }
1694 }
1695 }
1696 }
1697 }
1698 }
1699
Ben Wagner7a3c6742018-04-23 10:01:07 -04001700 if (fShowSlidePicker) {
1701 ImGui::SetNextTreeNodeOpen(true);
1702 }
Brian Osman79086b92017-02-10 13:36:16 -05001703 if (ImGui::CollapsingHeader("Slide")) {
1704 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001705 static ImVector<const char*> filteredSlideNames;
1706 static ImVector<int> filteredSlideIndices;
1707
Brian Osmanfce09c52017-11-14 15:32:20 -05001708 if (fShowSlidePicker) {
1709 ImGui::SetKeyboardFocusHere();
1710 fShowSlidePicker = false;
1711 }
1712
Brian Osman79086b92017-02-10 13:36:16 -05001713 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001714 filteredSlideNames.clear();
1715 filteredSlideIndices.clear();
1716 int filteredIndex = 0;
1717 for (int i = 0; i < fSlides.count(); ++i) {
1718 const char* slideName = fSlides[i]->getName().c_str();
1719 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1720 if (i == fCurrentSlide) {
1721 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001722 }
Brian Osmanf479e422017-11-08 13:11:36 -05001723 filteredSlideNames.push_back(slideName);
1724 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001725 }
Brian Osman79086b92017-02-10 13:36:16 -05001726 }
Brian Osmanf479e422017-11-08 13:11:36 -05001727
Brian Osmanf479e422017-11-08 13:11:36 -05001728 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1729 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001730 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001731 }
1732 }
Brian Osmana109e392017-02-24 09:49:14 -05001733
1734 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001735 ColorMode newMode = fColorMode;
1736 auto cmButton = [&](ColorMode mode, const char* label) {
1737 if (ImGui::RadioButton(label, mode == fColorMode)) {
1738 newMode = mode;
1739 }
1740 };
1741
1742 cmButton(ColorMode::kLegacy, "Legacy 8888");
1743 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1744 "Color Managed 8888 (Nonlinear blending)");
1745 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1746 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1747
1748 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001749 // It isn't safe to switch color mode now (in the middle of painting). We might
1750 // tear down the back-end, etc... Defer this change until the next onIdle.
1751 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001752 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001753 });
Brian Osmana109e392017-02-24 09:49:14 -05001754 }
1755
1756 // Pick from common gamuts:
1757 int primariesIdx = 4; // Default: Custom
1758 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1759 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1760 primariesIdx = i;
1761 break;
1762 }
1763 }
1764
Brian Osmanfdab5762017-11-09 10:27:55 -05001765 // When we're in xform canvas mode, we can alter the transfer function, too
1766 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1767 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1768 }
1769
Brian Osmana109e392017-02-24 09:49:14 -05001770 if (ImGui::Combo("Primaries", &primariesIdx,
1771 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1772 if (primariesIdx >= 0 && primariesIdx <= 3) {
1773 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1774 }
1775 }
1776
1777 // Allow direct editing of gamut
1778 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1779 }
Brian Osman79086b92017-02-10 13:36:16 -05001780 }
Brian Salomon99a33902017-03-07 15:16:34 -05001781 if (paramsChanged) {
1782 fDeferredActions.push_back([=]() {
1783 fWindow->setRequestedDisplayParams(params);
1784 fWindow->inval();
1785 this->updateTitle();
1786 });
1787 }
Brian Osman79086b92017-02-10 13:36:16 -05001788 ImGui::End();
1789 }
1790
Brian Osmanf6877092017-02-13 09:39:57 -05001791 if (fShowZoomWindow && fLastImage) {
1792 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001793 static int zoomFactor = 8;
1794 if (ImGui::Button("<<")) {
1795 zoomFactor = SkTMax(zoomFactor / 2, 4);
1796 }
1797 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1798 if (ImGui::Button(">>")) {
1799 zoomFactor = SkTMin(zoomFactor * 2, 32);
1800 }
Brian Osmanf6877092017-02-13 09:39:57 -05001801
Brian Osmanf6877092017-02-13 09:39:57 -05001802 ImVec2 mousePos = ImGui::GetMousePos();
1803 ImVec2 avail = ImGui::GetContentRegionAvail();
1804
Brian Osmanead517d2017-11-13 15:36:36 -05001805 uint32_t pixel = 0;
1806 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1807 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1808 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001809 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1810 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1811 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001812 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1813 }
1814
Brian Osmand67e5182017-12-08 16:46:09 -05001815 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001816 // Translate so the region of the image that's under the mouse cursor is centered
1817 // in the zoom canvas:
1818 c->scale(zoomFactor, zoomFactor);
1819 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1820 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1821 c->drawImage(this->fLastImage, 0, 0);
1822
1823 SkPaint outline;
1824 outline.setStyle(SkPaint::kStroke_Style);
1825 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1826 });
Brian Osmanf6877092017-02-13 09:39:57 -05001827 }
1828
1829 ImGui::End();
1830 }
Brian Osman79086b92017-02-10 13:36:16 -05001831}
1832
liyuqian2edb0f42016-07-06 14:11:32 -07001833void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001834 for (int i = 0; i < fDeferredActions.count(); ++i) {
1835 fDeferredActions[i]();
1836 }
1837 fDeferredActions.reset();
1838
Brian Osman56a24812017-12-19 11:15:16 -05001839 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001840 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001841 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001842 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001843
Brian Osman79086b92017-02-10 13:36:16 -05001844 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001845 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001846 fWindow->inval();
1847 }
jvanverth9f372462016-04-06 06:08:59 -07001848}
liyuqiane5a6cd92016-05-27 08:52:52 -07001849
1850void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001851 if (!fWindow) {
1852 return;
1853 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001854 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001855 return; // Surface hasn't been created yet.
1856 }
1857
liyuqianb73c24b2016-06-03 08:47:23 -07001858 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001859 Json::Value slideState(Json::objectValue);
1860 slideState[kName] = kSlideStateName;
1861 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001862 if (fAllSlideNames.size() == 0) {
1863 for(auto slide : fSlides) {
1864 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1865 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001866 }
liyuqian1f508fd2016-06-07 06:57:40 -07001867 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001868
liyuqianb73c24b2016-06-03 08:47:23 -07001869 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001870 Json::Value backendState(Json::objectValue);
1871 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001872 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001873 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001874 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001875 backendState[kOptions].append(Json::Value(str));
1876 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001877
csmartdalton578f0642017-02-24 16:04:47 -07001878 // MSAA state
1879 Json::Value msaaState(Json::objectValue);
1880 msaaState[kName] = kMSAAStateName;
1881 msaaState[kValue] = fWindow->sampleCount();
1882 msaaState[kOptions] = Json::Value(Json::arrayValue);
1883 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1884 msaaState[kOptions].append(Json::Value(0));
1885 } else {
1886 for (int msaa : {0, 4, 8, 16}) {
1887 msaaState[kOptions].append(Json::Value(msaa));
1888 }
1889 }
1890
csmartdalton61cd31a2017-02-27 17:00:53 -07001891 // Path renderer state
1892 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1893 Json::Value prState(Json::objectValue);
1894 prState[kName] = kPathRendererStateName;
1895 prState[kValue] = gPathRendererNames[pr];
1896 prState[kOptions] = Json::Value(Json::arrayValue);
1897 const GrContext* ctx = fWindow->getGrContext();
1898 if (!ctx) {
1899 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001900 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001901 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001902 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001903 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001904 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1905 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001906 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001907 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1908 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001909 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001910 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001911 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001912 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1913 }
Jim Van Verth83010462017-03-16 08:45:39 -04001914 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001915 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001916 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1917 }
1918
liyuqianb73c24b2016-06-03 08:47:23 -07001919 // Softkey state
1920 Json::Value softkeyState(Json::objectValue);
1921 softkeyState[kName] = kSoftkeyStateName;
1922 softkeyState[kValue] = kSoftkeyHint;
1923 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1924 softkeyState[kOptions].append(kSoftkeyHint);
1925 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1926 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1927 }
1928
liyuqiane5a6cd92016-05-27 08:52:52 -07001929 Json::Value state(Json::arrayValue);
1930 state.append(slideState);
1931 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001932 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001933 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001934 state.append(softkeyState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001935
Brian Osmaneff04b52017-11-21 13:18:02 -05001936 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001937}
1938
1939void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001940 // For those who will add more features to handle the state change in this function:
1941 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1942 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1943 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001944 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001945 for (int i = 0; i < fSlides.count(); ++i) {
1946 if (fSlides[i]->getName().equals(stateValue)) {
1947 this->setCurrentSlide(i);
1948 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001949 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001950 }
Florin Malitaab99c342018-01-16 16:23:03 -05001951
1952 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001953 } else if (stateName.equals(kBackendStateName)) {
1954 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1955 if (stateValue.equals(kBackendTypeStrings[i])) {
1956 if (fBackendType != i) {
1957 fBackendType = (sk_app::Window::BackendType)i;
1958 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001959 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001960 }
1961 break;
1962 }
1963 }
csmartdalton578f0642017-02-24 16:04:47 -07001964 } else if (stateName.equals(kMSAAStateName)) {
1965 DisplayParams params = fWindow->getRequestedDisplayParams();
1966 int sampleCount = atoi(stateValue.c_str());
1967 if (sampleCount != params.fMSAASampleCount) {
1968 params.fMSAASampleCount = sampleCount;
1969 fWindow->setRequestedDisplayParams(params);
1970 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001971 this->updateTitle();
1972 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001973 }
1974 } else if (stateName.equals(kPathRendererStateName)) {
1975 DisplayParams params = fWindow->getRequestedDisplayParams();
1976 for (const auto& pair : gPathRendererNames) {
1977 if (pair.second == stateValue.c_str()) {
1978 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1979 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1980 fWindow->setRequestedDisplayParams(params);
1981 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001982 this->updateTitle();
1983 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001984 }
1985 break;
1986 }
csmartdalton578f0642017-02-24 16:04:47 -07001987 }
liyuqianb73c24b2016-06-03 08:47:23 -07001988 } else if (stateName.equals(kSoftkeyStateName)) {
1989 if (!stateValue.equals(kSoftkeyHint)) {
1990 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001991 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001992 }
liyuqian2edb0f42016-07-06 14:11:32 -07001993 } else if (stateName.equals(kRefreshStateName)) {
1994 // This state is actually NOT in the UI state.
1995 // We use this to allow Android to quickly set bool fRefresh.
1996 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001997 } else {
1998 SkDebugf("Unknown stateName: %s", stateName.c_str());
1999 }
2000}
Brian Osman79086b92017-02-10 13:36:16 -05002001
2002bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002003 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002004}
2005
2006bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002007 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002008 fWindow->inval();
2009 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002010 } else {
2011 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002012 }
Brian Osman79086b92017-02-10 13:36:16 -05002013}