blob: 5a5489e67da3cc28dd2fc7349bbaa62bae248f8c [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Chris Dalton2d18f412018-02-20 13:23:32 -07008#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -07009#include "GMSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040010#include "GrContext.h"
11#include "GrContextPriv.h"
liyuqian6f163d22016-06-13 12:26:45 -070012#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050013#include "Resources.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070014#include "SKPSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040015#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070016#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050017#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040018#include "SkColorSpaceXformCanvas.h"
Brian Osman2dd96932016-10-18 15:33:53 -040019#include "SkCommandLineFlags.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040020#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070021#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040022#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050023#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040024#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050025#include "SkImagePriv.h"
Herb Derbyefe39bc2018-05-01 17:06:20 -040026#include "SkMakeUnique.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070027#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050028#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050029#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050030#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040031#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070032#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070033#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050034#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050035#include "SkTestFontMgr.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050036#include "SkThreadedBMPDevice.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040037#include "SkTo.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040038#include "SvgSlide.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040039#include "Viewer.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060040#include "ccpr/GrCoverageCountingPathRenderer.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040041#include "imgui.h"
csmartdalton578f0642017-02-24 16:04:47 -070042
Hal Canaryc640d0d2018-06-13 09:59:02 -040043#include <stdlib.h>
44#include <map>
45
Florin Malita3b526b02018-05-25 12:43:51 -040046#if defined(SK_HAS_SKSG)
47 #include "SlideDir.h"
48#endif
49
Florin Malita87ccf332018-05-04 12:23:24 -040050#if defined(SK_ENABLE_SKOTTIE)
51 #include "SkottieSlide.h"
52#endif
53
jvanverth34524262016-05-04 13:49:13 -070054using namespace sk_app;
55
csmartdalton61cd31a2017-02-27 17:00:53 -070056static std::map<GpuPathRenderers, std::string> gPathRendererNames;
57
jvanverth9f372462016-04-06 06:08:59 -070058Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070059 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070060}
61
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060062static DEFINE_string(slide, "", "Start on this sample.");
63static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050064
bsalomon6c471f72016-07-26 12:56:32 -070065#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070066# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070067#else
68# define BACKENDS_STR "\"sw\" and \"gl\""
69#endif
70
Brian Osman2dd96932016-10-18 15:33:53 -040071static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070072
Brian Salomonbdecacf2018-02-02 20:32:49 -050073static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070074
Chris Dalton2d18f412018-02-20 13:23:32 -070075DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
76
Brian Osman53136aa2017-07-20 15:43:35 -040077DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050078
Florin Malita38792ce2018-05-08 10:36:18 -040079DEFINE_string2(file, f, "", "Open a single file for viewing.");
80
Brian Salomon194db172017-08-17 14:37:06 -040081const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070082 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040083#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
84 "ANGLE",
85#endif
jvanverth063ece72016-06-17 09:29:14 -070086#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070087 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070088#endif
csmartdalton578f0642017-02-24 16:04:47 -070089 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070090};
91
bsalomon6c471f72016-07-26 12:56:32 -070092static sk_app::Window::BackendType get_backend_type(const char* str) {
93#ifdef SK_VULKAN
94 if (0 == strcmp(str, "vk")) {
95 return sk_app::Window::kVulkan_BackendType;
96 } else
97#endif
Brian Salomon194db172017-08-17 14:37:06 -040098#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
99 if (0 == strcmp(str, "angle")) {
100 return sk_app::Window::kANGLE_BackendType;
101 } else
102#endif
bsalomon6c471f72016-07-26 12:56:32 -0700103 if (0 == strcmp(str, "gl")) {
104 return sk_app::Window::kNativeGL_BackendType;
105 } else if (0 == strcmp(str, "sw")) {
106 return sk_app::Window::kRaster_BackendType;
107 } else {
108 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
109 return sk_app::Window::kRaster_BackendType;
110 }
111}
112
Brian Osmana109e392017-02-24 09:49:14 -0500113static SkColorSpacePrimaries gSrgbPrimaries = {
114 0.64f, 0.33f,
115 0.30f, 0.60f,
116 0.15f, 0.06f,
117 0.3127f, 0.3290f };
118
119static SkColorSpacePrimaries gAdobePrimaries = {
120 0.64f, 0.33f,
121 0.21f, 0.71f,
122 0.15f, 0.06f,
123 0.3127f, 0.3290f };
124
125static SkColorSpacePrimaries gP3Primaries = {
126 0.680f, 0.320f,
127 0.265f, 0.690f,
128 0.150f, 0.060f,
129 0.3127f, 0.3290f };
130
131static SkColorSpacePrimaries gRec2020Primaries = {
132 0.708f, 0.292f,
133 0.170f, 0.797f,
134 0.131f, 0.046f,
135 0.3127f, 0.3290f };
136
137struct NamedPrimaries {
138 const char* fName;
139 SkColorSpacePrimaries* fPrimaries;
140} gNamedPrimaries[] = {
141 { "sRGB", &gSrgbPrimaries },
142 { "AdobeRGB", &gAdobePrimaries },
143 { "P3", &gP3Primaries },
144 { "Rec. 2020", &gRec2020Primaries },
145};
146
147static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
148 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
149}
150
Brian Osman70d2f432017-11-08 09:54:10 -0500151static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
152 // In raster mode, we still use GL for the window.
153 // This lets us render the GUI faster (and correct).
154 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
155}
156
liyuqiane5a6cd92016-05-27 08:52:52 -0700157const char* kName = "name";
158const char* kValue = "value";
159const char* kOptions = "options";
160const char* kSlideStateName = "Slide";
161const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700162const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700163const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700164const char* kSoftkeyStateName = "Softkey";
165const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700166const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700167const char* kON = "ON";
168const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700169const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700170
jvanverth34524262016-05-04 13:49:13 -0700171Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500172 : fCurrentSlide(-1)
173 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500174 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500175 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500176 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500177 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500178 , fShowZoomWindow(false)
179 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700180 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500181 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500182 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500183 // Our UI can only tweak gamma (currently), so start out gamma-only
184 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700185 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400186 , fRotation(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400187 , fGestureDevice(GestureDevice::kNone)
Brian Osman805a7272018-05-02 15:40:20 -0400188 , fPerspectiveMode(kPerspective_Off)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500189 , fTileCnt(0)
190 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700191{
Greg Daniel285db442016-10-14 09:12:53 -0400192 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700193
Brian Osmanf09e35e2017-12-15 14:48:09 -0500194 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
195 gPathRendererNames[GpuPathRenderers::kDefault] =
196 "Default Ganesh Behavior (best path renderer, not including CCPR)";
197 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500198 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
199 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
200 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
201 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700202
jvanverth2bb3b6d2016-04-08 07:24:09 -0700203 SkDebugf("Command line arguments: ");
204 for (int i = 1; i < argc; ++i) {
205 SkDebugf("%s ", argv[i]);
206 }
207 SkDebugf("\n");
208
209 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500210#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400211 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500212#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700213
Ben Wagner483c7722018-02-20 17:06:07 -0500214 if (!FLAGS_nativeFonts) {
215 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
216 }
217
Brian Osmanbc8150f2017-07-24 11:38:01 -0400218 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400219 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400220
bsalomon6c471f72016-07-26 12:56:32 -0700221 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700222 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700223
csmartdalton578f0642017-02-24 16:04:47 -0700224 DisplayParams displayParams;
225 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700226 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700227 fWindow->setRequestedDisplayParams(displayParams);
228
Brian Osman56a24812017-12-19 11:15:16 -0500229 // Configure timers
230 fStatsLayer.setActive(false);
231 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
232 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
233 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
234
jvanverth9f372462016-04-06 06:08:59 -0700235 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700236 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500237 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500238 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500239 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700240
brianosman622c8d52016-05-10 06:50:49 -0700241 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500242 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
243 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
244 fWindow->inval();
245 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500246 // Command to jump directly to the slide picker and give it focus
247 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
248 this->fShowImGuiDebugWindow = true;
249 this->fShowSlidePicker = true;
250 fWindow->inval();
251 });
252 // Alias that to Backspace, to match SampleApp
253 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
254 this->fShowImGuiDebugWindow = true;
255 this->fShowSlidePicker = true;
256 fWindow->inval();
257 });
Brian Osman79086b92017-02-10 13:36:16 -0500258 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
259 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
260 fWindow->inval();
261 });
Brian Osmanf6877092017-02-13 09:39:57 -0500262 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
263 this->fShowZoomWindow = !this->fShowZoomWindow;
264 fWindow->inval();
265 });
brianosman622c8d52016-05-10 06:50:49 -0700266 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500267 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700268 fWindow->inval();
269 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400270 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500271 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400272 this->updateTitle();
273 fWindow->inval();
274 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500275 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500276 switch (fColorMode) {
277 case ColorMode::kLegacy:
278 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
279 break;
280 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
281 this->setColorMode(ColorMode::kColorManagedSRGB8888);
282 break;
283 case ColorMode::kColorManagedSRGB8888:
284 this->setColorMode(ColorMode::kColorManagedLinearF16);
285 break;
286 case ColorMode::kColorManagedLinearF16:
287 this->setColorMode(ColorMode::kLegacy);
288 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500289 }
brianosman622c8d52016-05-10 06:50:49 -0700290 });
291 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500292 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700293 });
294 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500295 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700296 });
297 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
298 this->changeZoomLevel(1.f / 32.f);
299 fWindow->inval();
300 });
301 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
302 this->changeZoomLevel(-1.f / 32.f);
303 fWindow->inval();
304 });
jvanverthaf236b52016-05-20 06:01:06 -0700305 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400306 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
307 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500308 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400309#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
310 if (newBackend == sk_app::Window::kVulkan_BackendType) {
311 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
312 sk_app::Window::kBackendTypeCount);
313 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
314 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500315 }
316#endif
Brian Osman621491e2017-02-28 15:45:01 -0500317 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700318 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500319 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
320 fTileCnt++;
321 if (fThreadCnt == 0) {
322 this->resetExecutor();
323 }
324 this->updateTitle();
325 fWindow->inval();
326 });
327 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
328 fTileCnt = SkTMax(0, fTileCnt - 1);
329 if (fThreadCnt == 0) {
330 this->resetExecutor();
331 }
332 this->updateTitle();
333 fWindow->inval();
334 });
335 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
336 if (fTileCnt == 0) {
337 return;
338 }
339 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
340 this->resetExecutor();
341 this->updateTitle();
342 fWindow->inval();
343 });
344 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
345 if (fTileCnt == 0) {
346 return;
347 }
348 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
349 this->resetExecutor();
350 this->updateTitle();
351 fWindow->inval();
352 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500353 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
354 fSaveToSKP = true;
355 fWindow->inval();
356 });
Ben Wagner37c54032018-04-13 14:30:23 -0400357 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
358 DisplayParams params = fWindow->getRequestedDisplayParams();
359 uint32_t flags = params.fSurfaceProps.flags();
360 if (!fPixelGeometryOverrides) {
361 fPixelGeometryOverrides = true;
362 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
363 } else {
364 switch (params.fSurfaceProps.pixelGeometry()) {
365 case kUnknown_SkPixelGeometry:
366 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
367 break;
368 case kRGB_H_SkPixelGeometry:
369 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
370 break;
371 case kBGR_H_SkPixelGeometry:
372 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
373 break;
374 case kRGB_V_SkPixelGeometry:
375 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
376 break;
377 case kBGR_V_SkPixelGeometry:
378 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
379 fPixelGeometryOverrides = false;
380 break;
381 }
382 }
383 fWindow->setRequestedDisplayParams(params);
384 this->updateTitle();
385 fWindow->inval();
386 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500387 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
388 if (!fPaintOverrides.fHinting) {
389 fPaintOverrides.fHinting = true;
390 fPaint.setHinting(SkPaint::kNo_Hinting);
391 } else {
392 switch (fPaint.getHinting()) {
393 case SkPaint::kNo_Hinting:
394 fPaint.setHinting(SkPaint::kSlight_Hinting);
395 break;
396 case SkPaint::kSlight_Hinting:
397 fPaint.setHinting(SkPaint::kNormal_Hinting);
398 break;
399 case SkPaint::kNormal_Hinting:
400 fPaint.setHinting(SkPaint::kFull_Hinting);
401 break;
402 case SkPaint::kFull_Hinting:
403 fPaint.setHinting(SkPaint::kNo_Hinting);
404 fPaintOverrides.fHinting = false;
405 break;
406 }
407 }
408 this->updateTitle();
409 fWindow->inval();
410 });
411 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
412 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
413 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
414 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
415 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500416 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
417 gSkUseDeltaAA = gSkForceDeltaAA = false;
418 } else {
419 fPaint.setAntiAlias(true);
420 switch (fPaintOverrides.fAntiAlias) {
421 case SkPaintFields::AntiAliasState::Alias:
422 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400423 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
424 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500425 break;
426 case SkPaintFields::AntiAliasState::Normal:
427 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500428 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400429 gSkForceAnalyticAA = false;
430 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500431 break;
432 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
433 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400434 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
435 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500436 break;
437 case SkPaintFields::AntiAliasState::AnalyticAAForced:
438 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
439 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
440 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400441 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500442 break;
443 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
444 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400445 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
446 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500447 break;
448 case SkPaintFields::AntiAliasState::DeltaAAForced:
449 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
450 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
451 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
452 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
453 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
454 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
455 break;
456 }
457 }
458 this->updateTitle();
459 fWindow->inval();
460 });
Ben Wagner37c54032018-04-13 14:30:23 -0400461 fCommands.addCommand('D', "Modes", "DFT", [this]() {
462 DisplayParams params = fWindow->getRequestedDisplayParams();
463 uint32_t flags = params.fSurfaceProps.flags();
464 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
465 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
466 fWindow->setRequestedDisplayParams(params);
467 this->updateTitle();
468 fWindow->inval();
469 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500470 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
471 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
472 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
473 fPaint.setLCDRenderText(false);
474 } else {
475 if (!fPaint.isLCDRenderText()) {
476 fPaint.setLCDRenderText(true);
477 } else {
478 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
479 }
480 }
481 this->updateTitle();
482 fWindow->inval();
483 });
484 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
485 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
486 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
487 fPaint.setSubpixelText(false);
488 } else {
489 if (!fPaint.isSubpixelText()) {
490 fPaint.setSubpixelText(true);
491 } else {
492 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
493 }
494 }
495 this->updateTitle();
496 fWindow->inval();
497 });
Brian Osman805a7272018-05-02 15:40:20 -0400498 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
499 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
500 : kPerspective_Real;
501 this->updateTitle();
502 fWindow->inval();
503 });
504 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
505 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
506 : kPerspective_Off;
507 this->updateTitle();
508 fWindow->inval();
509 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500510
jvanverth2bb3b6d2016-04-08 07:24:09 -0700511 // set up slides
512 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500513 if (FLAGS_list) {
514 this->listNames();
515 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700516
Brian Osman9bb47cf2018-04-26 15:55:00 -0400517 fPerspectivePoints[0].set(0, 0);
518 fPerspectivePoints[1].set(1, 0);
519 fPerspectivePoints[2].set(0, 1);
520 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700521 fAnimTimer.run();
522
Hal Canaryc465d132017-12-08 10:21:31 -0500523 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500524 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400525 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500526 }
527 fImGuiGamutPaint.setColor(SK_ColorWHITE);
528 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
529
Brian Osman70d2f432017-11-08 09:54:10 -0500530 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500531 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700532}
533
jvanverth34524262016-05-04 13:49:13 -0700534void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700535 fAllSlideNames = Json::Value(Json::arrayValue);
536
Florin Malita0ffa3222018-04-05 14:34:45 -0400537 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
538 static const struct {
539 const char* fExtension;
540 const char* fDirName;
541 const SkCommandLineFlags::StringArray& fFlags;
542 const SlideFactory fFactory;
543 } gExternalSlidesInfo[] = {
544 { ".skp", "skp-dir", FLAGS_skps,
545 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
546 return sk_make_sp<SKPSlide>(name, path);}
547 },
548 { ".jpg", "jpg-dir", FLAGS_jpgs,
549 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
550 return sk_make_sp<ImageSlide>(name, path);}
551 },
Florin Malita87ccf332018-05-04 12:23:24 -0400552#if defined(SK_ENABLE_SKOTTIE)
Florin Malita0ffa3222018-04-05 14:34:45 -0400553 { ".json", "skottie-dir", FLAGS_jsons,
554 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
555 return sk_make_sp<SkottieSlide>(name, path);}
556 },
Florin Malita87ccf332018-05-04 12:23:24 -0400557#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400558 { ".svg", "svg-dir", FLAGS_svgs,
559 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
560 return sk_make_sp<SvgSlide>(name, path);}
561 },
562 };
jvanverthc265a922016-04-08 12:51:45 -0700563
Florin Malita0ffa3222018-04-05 14:34:45 -0400564 SkTArray<sk_sp<Slide>, true> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700565
Florin Malita0ffa3222018-04-05 14:34:45 -0400566 const auto addSlide = [&](const SkString& name,
567 const SkString& path,
568 const SlideFactory& fact) {
569 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
570 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700571 }
liyuqian6f163d22016-06-13 12:26:45 -0700572
Florin Malita0ffa3222018-04-05 14:34:45 -0400573 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500574 dirSlides.push_back(slide);
575 fSlides.push_back(std::move(slide));
576 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400577 };
Florin Malita76a076b2018-02-15 18:40:48 -0500578
Florin Malita38792ce2018-05-08 10:36:18 -0400579 if (!FLAGS_file.isEmpty()) {
580 // single file mode
581 const SkString file(FLAGS_file[0]);
582
583 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
584 for (const auto& sinfo : gExternalSlidesInfo) {
585 if (file.endsWith(sinfo.fExtension)) {
586 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
587 return;
588 }
589 }
590
591 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
592 } else {
593 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
594 }
595
596 return;
597 }
598
599 // Bisect slide.
600 if (!FLAGS_bisect.isEmpty()) {
601 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
602 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
603 if (FLAGS_bisect.count() >= 2) {
604 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
605 bisect->onChar(*ch);
606 }
607 }
608 fSlides.push_back(std::move(bisect));
609 }
610 }
611
612 // GMs
613 int firstGM = fSlides.count();
614 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
615 while (gms) {
616 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
617
618 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
619 sk_sp<Slide> slide(new GMSlide(gm.release()));
620 fSlides.push_back(std::move(slide));
621 }
622
623 gms = gms->next();
624 }
625 // reverse gms
626 int numGMs = fSlides.count() - firstGM;
627 for (int i = 0; i < numGMs/2; ++i) {
628 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
629 }
630
631 // samples
632 const SkViewRegister* reg = SkViewRegister::Head();
633 while (reg) {
634 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
635 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
636 fSlides.push_back(slide);
637 }
638 reg = reg->next();
639 }
640
Florin Malita0ffa3222018-04-05 14:34:45 -0400641 for (const auto& info : gExternalSlidesInfo) {
642 for (const auto& flag : info.fFlags) {
643 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
644 // single file
645 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
646 } else {
647 // directory
648 SkOSFile::Iter it(flag.c_str(), info.fExtension);
649 SkString name;
650 while (it.next(&name)) {
651 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
652 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400653 }
Florin Malita3b526b02018-05-25 12:43:51 -0400654#if defined(SK_HAS_SKSG)
Florin Malita0ffa3222018-04-05 14:34:45 -0400655 if (!dirSlides.empty()) {
656 fSlides.push_back(
657 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
658 std::move(dirSlides)));
659 dirSlides.reset();
660 }
Florin Malita3b526b02018-05-25 12:43:51 -0400661#endif
Florin Malitac659c2c2018-04-05 11:57:21 -0400662 }
663 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700664}
665
666
jvanverth34524262016-05-04 13:49:13 -0700667Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700668 fWindow->detach();
669 delete fWindow;
670}
671
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500672struct SkPaintTitleUpdater {
673 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
674 void append(const char* s) {
675 if (fCount == 0) {
676 fTitle->append(" {");
677 } else {
678 fTitle->append(", ");
679 }
680 fTitle->append(s);
681 ++fCount;
682 }
683 void done() {
684 if (fCount > 0) {
685 fTitle->append("}");
686 }
687 }
688 SkString* fTitle;
689 int fCount;
690};
691
brianosman05de2162016-05-06 13:28:57 -0700692void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700693 if (!fWindow) {
694 return;
695 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500696 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700697 return; // Surface hasn't been created yet.
698 }
699
jvanverth34524262016-05-04 13:49:13 -0700700 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700701 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700702
Yuqian Li399b3c22017-08-03 11:08:15 -0400703 if (gSkUseDeltaAA) {
704 if (gSkForceDeltaAA) {
705 title.append(" <FDAA>");
706 } else {
707 title.append(" <DAA>");
708 }
709 } else if (gSkUseAnalyticAA) {
710 if (gSkForceAnalyticAA) {
711 title.append(" <FAAA>");
712 } else {
713 title.append(" <AAA>");
714 }
715 }
716
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500717 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner99a78dc2018-05-09 18:23:51 -0400718 auto paintFlag = [this, &paintTitle](SkPaint::Flags flag, bool (SkPaint::* isFlag)() const,
719 const char* on, const char* off)
720 {
721 if (fPaintOverrides.fFlags & flag) {
722 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500723 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400724 };
725
726 paintFlag(SkPaint::kAntiAlias_Flag, &SkPaint::isAntiAlias, "Antialias", "Alias");
727 paintFlag(SkPaint::kDither_Flag, &SkPaint::isDither, "DITHER", "No Dither");
728 paintFlag(SkPaint::kFakeBoldText_Flag, &SkPaint::isFakeBoldText, "Fake Bold", "No Fake Bold");
729 paintFlag(SkPaint::kLinearText_Flag, &SkPaint::isLinearText, "Linear Text", "Non-Linear Text");
730 paintFlag(SkPaint::kSubpixelText_Flag, &SkPaint::isSubpixelText, "Subpixel Text", "Pixel Text");
731 paintFlag(SkPaint::kLCDRenderText_Flag, &SkPaint::isLCDRenderText, "LCD", "lcd");
732 paintFlag(SkPaint::kEmbeddedBitmapText_Flag, &SkPaint::isEmbeddedBitmapText,
733 "Bitmap Text", "No Bitmap Text");
734 paintFlag(SkPaint::kAutoHinting_Flag, &SkPaint::isAutohinted,
735 "Force Autohint", "No Force Autohint");
736 paintFlag(SkPaint::kVerticalText_Flag, &SkPaint::isVerticalText,
737 "Vertical Text", "No Vertical Text");
738
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500739 if (fPaintOverrides.fHinting) {
740 switch (fPaint.getHinting()) {
741 case SkPaint::kNo_Hinting:
742 paintTitle.append("No Hinting");
743 break;
744 case SkPaint::kSlight_Hinting:
745 paintTitle.append("Slight Hinting");
746 break;
747 case SkPaint::kNormal_Hinting:
748 paintTitle.append("Normal Hinting");
749 break;
750 case SkPaint::kFull_Hinting:
751 paintTitle.append("Full Hinting");
752 break;
753 }
754 }
755 paintTitle.done();
756
Yuqian Lib2ba6642017-11-22 12:07:41 -0500757 if (fTileCnt > 0) {
758 title.appendf(" T%d", fTileCnt);
759 if (fThreadCnt > 0) {
760 title.appendf("/%d", fThreadCnt);
761 }
762 }
763
Brian Osman92004802017-03-06 11:47:26 -0500764 switch (fColorMode) {
765 case ColorMode::kLegacy:
766 title.append(" Legacy 8888");
767 break;
768 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
769 title.append(" ColorManaged 8888 (Nonlinear blending)");
770 break;
771 case ColorMode::kColorManagedSRGB8888:
772 title.append(" ColorManaged 8888");
773 break;
774 case ColorMode::kColorManagedLinearF16:
775 title.append(" ColorManaged F16");
776 break;
777 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500778
Brian Osman92004802017-03-06 11:47:26 -0500779 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500780 int curPrimaries = -1;
781 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
782 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
783 curPrimaries = i;
784 break;
785 }
786 }
787 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500788
789 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
790 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
791 }
brianosman05de2162016-05-06 13:28:57 -0700792 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500793
Ben Wagner37c54032018-04-13 14:30:23 -0400794 const DisplayParams& params = fWindow->getRequestedDisplayParams();
795 if (fPixelGeometryOverrides) {
796 switch (params.fSurfaceProps.pixelGeometry()) {
797 case kUnknown_SkPixelGeometry:
798 title.append( " Flat");
799 break;
800 case kRGB_H_SkPixelGeometry:
801 title.append( " RGB");
802 break;
803 case kBGR_H_SkPixelGeometry:
804 title.append( " BGR");
805 break;
806 case kRGB_V_SkPixelGeometry:
807 title.append( " RGBV");
808 break;
809 case kBGR_V_SkPixelGeometry:
810 title.append( " BGRV");
811 break;
812 }
813 }
814
815 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
816 title.append(" DFT");
817 }
818
csmartdalton578f0642017-02-24 16:04:47 -0700819 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700820 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500821 int msaa = fWindow->sampleCount();
822 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700823 title.appendf(" MSAA: %i", msaa);
824 }
825 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700826
827 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400828 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700829 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
830 }
831
Brian Osman805a7272018-05-02 15:40:20 -0400832 if (kPerspective_Real == fPerspectiveMode) {
833 title.append(" Perpsective (Real)");
834 } else if (kPerspective_Fake == fPerspectiveMode) {
835 title.append(" Perspective (Fake)");
836 }
837
brianosman05de2162016-05-06 13:28:57 -0700838 fWindow->setTitle(title.c_str());
839}
840
Florin Malitaab99c342018-01-16 16:23:03 -0500841int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500842
843 if (!FLAGS_slide.isEmpty()) {
844 int count = fSlides.count();
845 for (int i = 0; i < count; i++) {
846 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500847 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500848 }
849 }
850
851 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
852 this->listNames();
853 }
854
Florin Malitaab99c342018-01-16 16:23:03 -0500855 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500856}
857
Florin Malitaab99c342018-01-16 16:23:03 -0500858void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500859 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500860 for (const auto& slide : fSlides) {
861 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500862 }
863}
864
Florin Malitaab99c342018-01-16 16:23:03 -0500865void Viewer::setCurrentSlide(int slide) {
866 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700867
Florin Malitaab99c342018-01-16 16:23:03 -0500868 if (slide == fCurrentSlide) {
869 return;
870 }
871
872 if (fCurrentSlide >= 0) {
873 fSlides[fCurrentSlide]->unload();
874 }
875
876 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
877 SkIntToScalar(fWindow->height()));
878 fCurrentSlide = slide;
879 this->setupCurrentSlide();
880}
881
882void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500883 if (fCurrentSlide >= 0) {
884 // prepare dimensions for image slides
885 fGesture.resetTouchState();
886 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700887
Jim Van Verth0848fb02018-01-22 13:39:30 -0500888 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
889 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
890 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400891
Jim Van Verth0848fb02018-01-22 13:39:30 -0500892 // Start with a matrix that scales the slide to the available screen space
893 if (fWindow->scaleContentToFit()) {
894 if (windowRect.width() > 0 && windowRect.height() > 0) {
895 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
896 }
liyuqiane46e4f02016-05-20 07:32:19 -0700897 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500898
899 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400900 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500901
902 this->updateTitle();
903 this->updateUIState();
904
905 fStatsLayer.resetMeasurements();
906
907 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700908 }
jvanverthc265a922016-04-08 12:51:45 -0700909}
910
911#define MAX_ZOOM_LEVEL 8
912#define MIN_ZOOM_LEVEL -8
913
jvanverth34524262016-05-04 13:49:13 -0700914void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700915 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400916 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400917 this->preTouchMatrixChanged();
918}
Yuqian Li755778c2018-03-28 16:23:31 -0400919
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400920void Viewer::preTouchMatrixChanged() {
921 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400922 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
923 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
924 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
925 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
926}
927
Brian Osman805a7272018-05-02 15:40:20 -0400928SkMatrix Viewer::computePerspectiveMatrix() {
929 SkScalar w = fWindow->width(), h = fWindow->height();
930 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
931 SkPoint perspPts[4] = {
932 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
933 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
934 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
935 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
936 };
937 SkMatrix m;
938 m.setPolyToPoly(orthoPts, perspPts, 4);
939 return m;
940}
941
Yuqian Li755778c2018-03-28 16:23:31 -0400942SkMatrix Viewer::computePreTouchMatrix() {
943 SkMatrix m = fDefaultMatrix;
944 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
945 : SK_Scalar1 + fZoomLevel;
946 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400947
948 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
949 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400950
Brian Osman805a7272018-05-02 15:40:20 -0400951 if (kPerspective_Real == fPerspectiveMode) {
952 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400953 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400954 }
955
Yuqian Li755778c2018-03-28 16:23:31 -0400956 return m;
jvanverthc265a922016-04-08 12:51:45 -0700957}
958
liyuqiand3cdbca2016-05-17 12:44:20 -0700959SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400960 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700961 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400962 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700963 return m;
jvanverthc265a922016-04-08 12:51:45 -0700964}
965
Brian Osman621491e2017-02-28 15:45:01 -0500966void Viewer::setBackend(sk_app::Window::BackendType backendType) {
967 fBackendType = backendType;
968
969 fWindow->detach();
970
Brian Osman70d2f432017-11-08 09:54:10 -0500971#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400972 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
973 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500974 DisplayParams params = fWindow->getRequestedDisplayParams();
975 delete fWindow;
976 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500977
Brian Osman70d2f432017-11-08 09:54:10 -0500978 // re-register callbacks
979 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500980 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500981 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500982 fWindow->pushLayer(&fImGuiLayer);
983
Brian Osman70d2f432017-11-08 09:54:10 -0500984 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
985 // will still include our correct sample count. But the re-created fWindow will lose that
986 // information. On Windows, we need to re-create the window when changing sample count,
987 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
988 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
989 // as if we had never changed windows at all).
990 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500991#endif
992
Brian Osman70d2f432017-11-08 09:54:10 -0500993 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500994}
995
Brian Osman92004802017-03-06 11:47:26 -0500996void Viewer::setColorMode(ColorMode colorMode) {
997 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700998
Brian Osmanf750fbc2017-02-08 10:47:28 -0500999 // 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 -04001000 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -07001001 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -05001002 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001003 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
1004 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -05001005 if (wasInLegacy != wantLegacy) {
1006 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -07001007 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001008 }
1009
1010 this->updateTitle();
1011 fWindow->inval();
1012}
1013
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001014class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1015public:
1016 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
1017 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
1018 { }
1019 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001020 if (*paint == nullptr) {
1021 return true;
1022 }
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001023 if (fPaintOverrides->fTextSize) {
1024 paint->writable()->setTextSize(fPaint->getTextSize());
1025 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001026 if (fPaintOverrides->fHinting) {
1027 paint->writable()->setHinting(fPaint->getHinting());
1028 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001029
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001030 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
1031 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1032 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001033 if (fPaintOverrides->fFlags & SkPaint::kDither_Flag) {
1034 paint->writable()->setDither(fPaint->isDither());
1035 }
1036 if (fPaintOverrides->fFlags & SkPaint::kFakeBoldText_Flag) {
1037 paint->writable()->setFakeBoldText(fPaint->isFakeBoldText());
1038 }
1039 if (fPaintOverrides->fFlags & SkPaint::kLinearText_Flag) {
1040 paint->writable()->setLinearText(fPaint->isLinearText());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001041 }
1042 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
1043 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
1044 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001045 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
1046 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
1047 }
1048 if (fPaintOverrides->fFlags & SkPaint::kEmbeddedBitmapText_Flag) {
1049 paint->writable()->setEmbeddedBitmapText(fPaint->isEmbeddedBitmapText());
1050 }
1051 if (fPaintOverrides->fFlags & SkPaint::kAutoHinting_Flag) {
1052 paint->writable()->setAutohinted(fPaint->isAutohinted());
1053 }
1054 if (fPaintOverrides->fFlags & SkPaint::kVerticalText_Flag) {
1055 paint->writable()->setVerticalText(fPaint->isVerticalText());
1056 }
1057
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001058 return true;
1059 }
1060 SkPaint* fPaint;
1061 Viewer::SkPaintFields* fPaintOverrides;
1062};
1063
Brian Osmanf750fbc2017-02-08 10:47:28 -05001064void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001065 SkAutoCanvasRestore autorestore(canvas, false);
1066
Brian Osmanf750fbc2017-02-08 10:47:28 -05001067 // By default, we render directly into the window's surface/canvas
1068 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001069 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001070
Brian Osmane0d4fba2017-03-15 10:24:55 -04001071 // If we're in any of the color managed modes, construct the color space we're going to use
1072 sk_sp<SkColorSpace> cs = nullptr;
1073 if (ColorMode::kLegacy != fColorMode) {
1074 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
1075 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -04001076 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001077 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -05001078 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1079 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
1080 } else {
1081 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
1082 }
Brian Osmane0d4fba2017-03-15 10:24:55 -04001083 }
1084
Brian Osman3ac99cf2017-12-01 11:23:53 -05001085 if (fSaveToSKP) {
1086 SkPictureRecorder recorder;
1087 SkCanvas* recorderCanvas = recorder.beginRecording(
1088 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1089 // In xform-canvas mode, record the transformed output
1090 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1091 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1092 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
1093 recorderCanvas = xformCanvas.get();
1094 }
1095 fSlides[fCurrentSlide]->draw(recorderCanvas);
1096 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1097 SkFILEWStream stream("sample_app.skp");
1098 picture->serialize(&stream);
1099 fSaveToSKP = false;
1100 }
1101
Brian Osmane0d4fba2017-03-15 10:24:55 -04001102 // 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 -05001103 // 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 -04001104 // because the window surface is actually GL, or we're doing fake perspective.
Brian Osmanf750fbc2017-02-08 10:47:28 -05001105 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -05001106 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -05001107 if (Window::kRaster_BackendType == fBackendType ||
Brian Osman805a7272018-05-02 15:40:20 -04001108 kPerspective_Fake == fPerspectiveMode ||
Brian Osman70d2f432017-11-08 09:54:10 -05001109 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -05001110 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -04001111 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
1112 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
1113
Brian Osman92004802017-03-06 11:47:26 -05001114 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
1115 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001116 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
1117 // with a special canvas (below) that has the color space attached
1118 sk_sp<SkColorSpace> offscreenColorSpace =
1119 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -05001120 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -04001121 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Ben Wagner37c54032018-04-13 14:30:23 -04001122 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1123 canvas->getProps(&props);
1124 offscreenSurface = Window::kRaster_BackendType == fBackendType
1125 ? SkSurface::MakeRaster(info, &props)
1126 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -05001127 SkPixmap offscreenPixmap;
1128 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
1129 SkBitmap offscreenBitmap;
1130 offscreenBitmap.installPixels(offscreenPixmap);
Herb Derbyefe39bc2018-05-01 17:06:20 -04001131 threadedCanvas =
1132 skstd::make_unique<SkCanvas>(
1133 sk_make_sp<SkThreadedBMPDevice>(
1134 offscreenBitmap, fTileCnt, fThreadCnt, fExecutor.get()));
Yuqian Lib2ba6642017-11-22 12:07:41 -05001135 slideCanvas = threadedCanvas.get();
1136 } else {
1137 slideCanvas = offscreenSurface->getCanvas();
1138 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001139 }
1140
Brian Osmane0d4fba2017-03-15 10:24:55 -04001141 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1142 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1143 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
1144 slideCanvas = xformCanvas.get();
1145 }
1146
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001147 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001148 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001149 slideCanvas->concat(computeMatrix());
Brian Osmane19e23f2018-05-22 16:56:40 -04001150 if (kPerspective_Real == fPerspectiveMode) {
1151 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1152 }
Brian Osman1df161a2017-02-09 12:10:20 -05001153 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001154 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001155 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1156 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001157 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001158 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001159
1160 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001161 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001162 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001163 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001164
1165 // If we rendered offscreen, snap an image and push the results to the window's canvas
1166 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001167 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001168
1169 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -04001170 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001171 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001172 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001173 SkPaint paint;
1174 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001175 int prePerspectiveCount = canvas->save();
1176 if (kPerspective_Fake == fPerspectiveMode) {
1177 paint.setFilterQuality(kHigh_SkFilterQuality);
1178 canvas->clear(SK_ColorWHITE);
1179 canvas->concat(this->computePerspectiveMatrix());
1180 }
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001181 canvas->drawImage(retaggedImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001182 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001183 }
liyuqian6f163d22016-06-13 12:26:45 -07001184}
1185
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001186void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001187 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001188 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001189}
Jim Van Verth6f449692017-02-14 15:16:46 -05001190
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001191void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001192 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001193
brianosman622c8d52016-05-10 06:50:49 -07001194 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001195
Brian Osmand67e5182017-12-08 16:46:09 -05001196 this->drawImGui();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001197}
1198
Florin Malitacefc1b92018-02-19 21:43:47 -05001199SkPoint Viewer::mapEvent(float x, float y) {
1200 const auto m = this->computeMatrix();
1201 SkMatrix inv;
1202
1203 SkAssertResult(m.invert(&inv));
1204
1205 return inv.mapXY(x, y);
1206}
1207
jvanverth814e38d2016-06-06 08:48:47 -07001208bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001209 if (GestureDevice::kMouse == fGestureDevice) {
1210 return false;
1211 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001212
1213 const auto slidePt = this->mapEvent(x, y);
1214 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1215 fWindow->inval();
1216 return true;
1217 }
1218
liyuqiand3cdbca2016-05-17 12:44:20 -07001219 void* castedOwner = reinterpret_cast<void*>(owner);
1220 switch (state) {
1221 case Window::kUp_InputState: {
1222 fGesture.touchEnd(castedOwner);
1223 break;
1224 }
1225 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001226 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001227 break;
1228 }
1229 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001230 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001231 break;
1232 }
1233 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001234 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001235 fWindow->inval();
1236 return true;
1237}
1238
Brian Osman80fc07e2017-12-08 16:45:43 -05001239bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001240 if (GestureDevice::kTouch == fGestureDevice) {
1241 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001242 }
Brian Osman16c81a12017-12-20 11:58:34 -05001243
Florin Malitacefc1b92018-02-19 21:43:47 -05001244 const auto slidePt = this->mapEvent(x, y);
1245 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1246 fWindow->inval();
1247 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001248 }
1249
1250 switch (state) {
1251 case Window::kUp_InputState: {
1252 fGesture.touchEnd(nullptr);
1253 break;
1254 }
1255 case Window::kDown_InputState: {
1256 fGesture.touchBegin(nullptr, x, y);
1257 break;
1258 }
1259 case Window::kMove_InputState: {
1260 fGesture.touchMoved(nullptr, x, y);
1261 break;
1262 }
1263 }
1264 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1265
1266 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1267 fWindow->inval();
1268 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001269 return true;
1270}
1271
Brian Osmana109e392017-02-24 09:49:14 -05001272static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1273 const ImVec2& pos, const ImVec2& size) {
1274 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1275 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1276
1277 // Invisible 10x10 button
1278 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1279 ImGui::InvisibleButton(label, ImVec2(10, 10));
1280
1281 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1282 ImGuiIO& io = ImGui::GetIO();
1283 // Normalized mouse position, relative to our gamut box
1284 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1285 // Clamp to edge of box, convert back to primary scale
1286 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1287 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1288 }
1289
1290 if (ImGui::IsItemHovered()) {
1291 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1292 }
1293
1294 // Return screen coordinates for the caller. We could just return center here, but we'd have
1295 // one frame of lag during drag.
1296 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1297}
1298
1299static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1300 ImDrawList* drawList = ImGui::GetWindowDrawList();
1301
1302 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1303 // width, and scale the height to maintain aspect ratio.
1304 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1305 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1306 ImVec2 pos = ImGui::GetCursorScreenPos();
1307
1308 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1309 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1310 // Magic numbers are pixel locations of the origin and upper-right corner.
1311 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1312 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001313
1314 // Primary markers
1315 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1316 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1317 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1318 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1319
1320 // Gamut triangle
1321 drawList->AddCircle(r, 5.0f, 0xFF000040);
1322 drawList->AddCircle(g, 5.0f, 0xFF004000);
1323 drawList->AddCircle(b, 5.0f, 0xFF400000);
1324 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1325 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1326
1327 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001328 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1329}
1330
1331static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1332 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1333 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1334 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1335
1336 // Invisible 10x10 button
1337 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1338 ImGui::InvisibleButton(label, ImVec2(10, 10));
1339
1340 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1341 ImGuiIO& io = ImGui::GetIO();
1342 // Normalized mouse position, relative to our gamut box
1343 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1344 // Clamp to edge of box
1345 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1346 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1347 *dragging = true;
1348 }
1349
1350 // Return screen coordinates for the caller. We could just return center here, but we'd have
1351 // one frame of lag during drag.
1352 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1353}
1354
1355static bool ImGui_DragQuad(SkPoint* pts) {
1356 ImDrawList* drawList = ImGui::GetWindowDrawList();
1357
1358 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1359 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1360 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1361 ImVec2 pos = ImGui::GetCursorScreenPos();
1362
1363 // Background rectangle
1364 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1365
1366 // Corner markers
1367 bool dragging = false;
1368 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1369 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1370 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1371 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1372
1373 // Draw markers and quad
1374 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1375 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1376 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1377 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1378 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1379 drawList->AddLine(tr, br, 0xFFFFFFFF);
1380 drawList->AddLine(br, bl, 0xFFFFFFFF);
1381 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1382
1383 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1384 ImGui::Spacing();
1385
1386 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001387}
1388
Brian Osmand67e5182017-12-08 16:46:09 -05001389void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001390 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1391 if (fShowImGuiTestWindow) {
1392 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1393 }
1394
1395 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001396 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1397 // always visible, we can end up in a layout feedback loop.
1398 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001399 DisplayParams params = fWindow->getRequestedDisplayParams();
1400 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001401 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1402 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001403 if (ImGui::CollapsingHeader("Backend")) {
1404 int newBackend = static_cast<int>(fBackendType);
1405 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1406 ImGui::SameLine();
1407 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001408#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1409 ImGui::SameLine();
1410 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1411#endif
Brian Osman621491e2017-02-28 15:45:01 -05001412#if defined(SK_VULKAN)
1413 ImGui::SameLine();
1414 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1415#endif
1416 if (newBackend != fBackendType) {
1417 fDeferredActions.push_back([=]() {
1418 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1419 });
1420 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001421
Brian Salomon99a33902017-03-07 15:16:34 -05001422 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001423 bool* wire = &params.fGrContextOptions.fWireframeMode;
1424 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1425 paramsChanged = true;
1426 }
Brian Salomon99a33902017-03-07 15:16:34 -05001427
Brian Osman28b12522017-03-08 17:10:24 -05001428 if (ctx) {
1429 int sampleCount = fWindow->sampleCount();
1430 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001431 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001432 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1433 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1434 ImGui::RadioButton("16", &sampleCount, 16);
1435
1436 if (sampleCount != params.fMSAASampleCount) {
1437 params.fMSAASampleCount = sampleCount;
1438 paramsChanged = true;
1439 }
1440 }
1441
Ben Wagner37c54032018-04-13 14:30:23 -04001442 int pixelGeometryIdx = 0;
1443 if (fPixelGeometryOverrides) {
1444 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1445 }
1446 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1447 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1448 {
1449 uint32_t flags = params.fSurfaceProps.flags();
1450 if (pixelGeometryIdx == 0) {
1451 fPixelGeometryOverrides = false;
1452 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1453 } else {
1454 fPixelGeometryOverrides = true;
1455 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1456 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1457 }
1458 paramsChanged = true;
1459 }
1460
1461 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1462 if (ImGui::Checkbox("DFT", &useDFT)) {
1463 uint32_t flags = params.fSurfaceProps.flags();
1464 if (useDFT) {
1465 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1466 } else {
1467 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1468 }
1469 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1470 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1471 paramsChanged = true;
1472 }
1473
Brian Osman8a9de3d2017-03-01 14:59:05 -05001474 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001475 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001476 auto prButton = [&](GpuPathRenderers x) {
1477 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001478 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1479 params.fGrContextOptions.fGpuPathRenderers = x;
1480 paramsChanged = true;
1481 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001482 }
1483 };
1484
1485 if (!ctx) {
1486 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001487 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001488 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001489 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001490 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001491 prButton(GpuPathRenderers::kStencilAndCover);
1492 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001493 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001494 prButton(GpuPathRenderers::kNone);
1495 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001496 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001497 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001498 if (GrCoverageCountingPathRenderer::IsSupported(
1499 *ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001500 prButton(GpuPathRenderers::kCoverageCounting);
1501 }
Jim Van Verth83010462017-03-16 08:45:39 -04001502 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001503 prButton(GpuPathRenderers::kTessellating);
1504 prButton(GpuPathRenderers::kNone);
1505 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001506 ImGui::TreePop();
1507 }
Brian Osman621491e2017-02-28 15:45:01 -05001508 }
1509
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001510 if (ImGui::CollapsingHeader("Transform")) {
1511 float zoom = fZoomLevel;
1512 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1513 fZoomLevel = zoom;
1514 this->preTouchMatrixChanged();
1515 paramsChanged = true;
1516 }
1517 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001518 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001519 fRotation = deg;
1520 this->preTouchMatrixChanged();
1521 paramsChanged = true;
1522 }
Brian Osman805a7272018-05-02 15:40:20 -04001523 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1524 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1525 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001526 this->preTouchMatrixChanged();
Brian Osman805a7272018-05-02 15:40:20 -04001527 this->updateTitle();
Brian Osman9bb47cf2018-04-26 15:55:00 -04001528 }
1529 if (ImGui_DragQuad(fPerspectivePoints)) {
1530 this->preTouchMatrixChanged();
1531 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001532 }
1533
Ben Wagnera580fb32018-04-17 11:16:32 -04001534 if (ImGui::CollapsingHeader("Paint")) {
1535 int hintingIdx = 0;
1536 if (fPaintOverrides.fHinting) {
1537 hintingIdx = fPaint.getHinting() + 1;
1538 }
1539 if (ImGui::Combo("Hinting", &hintingIdx,
1540 "Default\0None\0Slight\0Normal\0Full\0\0"))
1541 {
1542 if (hintingIdx == 0) {
1543 fPaintOverrides.fHinting = false;
1544 fPaint.setHinting(SkPaint::kNo_Hinting);
1545 } else {
1546 fPaintOverrides.fHinting = true;
1547 SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1);
1548 fPaint.setHinting(hinting);
1549 }
1550 paramsChanged = true;
1551 }
1552
1553 int aliasIdx = 0;
1554 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1555 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1556 }
1557 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1558 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1559 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1560 {
1561 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1562 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1563 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1564 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1565 if (aliasIdx == 0) {
1566 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1567 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1568 } else {
1569 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1570 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1571 fPaint.setAntiAlias(aliasIdx > 1);
1572 switch (fPaintOverrides.fAntiAlias) {
1573 case SkPaintFields::AntiAliasState::Alias:
1574 break;
1575 case SkPaintFields::AntiAliasState::Normal:
1576 break;
1577 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1578 gSkUseAnalyticAA = true;
1579 gSkForceAnalyticAA = false;
1580 gSkUseDeltaAA = gSkForceDeltaAA = false;
1581 break;
1582 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1583 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1584 gSkUseDeltaAA = gSkForceDeltaAA = false;
1585 break;
1586 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1587 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1588 gSkUseDeltaAA = true;
1589 gSkForceDeltaAA = false;
1590 break;
1591 case SkPaintFields::AntiAliasState::DeltaAAForced:
1592 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1593 gSkUseDeltaAA = gSkForceDeltaAA = true;
1594 break;
1595 }
1596 }
1597 paramsChanged = true;
1598 }
1599
Ben Wagner99a78dc2018-05-09 18:23:51 -04001600 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
1601 SkPaint::Flags flag,
1602 bool (SkPaint::* isFlag)() const,
1603 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001604 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001605 int itemIndex = 0;
1606 if (fPaintOverrides.fFlags & flag) {
1607 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001608 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001609 if (ImGui::Combo(label, &itemIndex, items)) {
1610 if (itemIndex == 0) {
1611 fPaintOverrides.fFlags &= ~flag;
1612 } else {
1613 fPaintOverrides.fFlags |= flag;
1614 (fPaint.*setFlag)(itemIndex == 2);
1615 }
1616 paramsChanged = true;
1617 }
1618 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001619
Ben Wagner99a78dc2018-05-09 18:23:51 -04001620 paintFlag("Dither",
1621 "Default\0No Dither\0Dither\0\0",
1622 SkPaint::kDither_Flag,
1623 &SkPaint::isDither, &SkPaint::setDither);
1624
1625 paintFlag("Fake Bold Glyphs",
1626 "Default\0No Fake Bold\0Fake Bold\0\0",
1627 SkPaint::kFakeBoldText_Flag,
1628 &SkPaint::isFakeBoldText, &SkPaint::setFakeBoldText);
1629
1630 paintFlag("Linear Text",
1631 "Default\0No Linear Text\0Linear Text\0\0",
1632 SkPaint::kLinearText_Flag,
1633 &SkPaint::isLinearText, &SkPaint::setLinearText);
1634
1635 paintFlag("Subpixel Position Glyphs",
1636 "Default\0Pixel Text\0Subpixel Text\0\0",
1637 SkPaint::kSubpixelText_Flag,
1638 &SkPaint::isSubpixelText, &SkPaint::setSubpixelText);
1639
1640 paintFlag("Subpixel Anti-Alias",
1641 "Default\0lcd\0LCD\0\0",
1642 SkPaint::kLCDRenderText_Flag,
1643 &SkPaint::isLCDRenderText, &SkPaint::setLCDRenderText);
1644
1645 paintFlag("Embedded Bitmap Text",
1646 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1647 SkPaint::kEmbeddedBitmapText_Flag,
1648 &SkPaint::isEmbeddedBitmapText, &SkPaint::setEmbeddedBitmapText);
1649
1650 paintFlag("Force Auto-Hinting",
1651 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1652 SkPaint::kAutoHinting_Flag,
1653 &SkPaint::isAutohinted, &SkPaint::setAutohinted);
1654
1655 paintFlag("Vertical Text",
1656 "Default\0No Vertical Text\0Vertical Text\0\0",
1657 SkPaint::kVerticalText_Flag,
1658 &SkPaint::isVerticalText, &SkPaint::setVerticalText);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001659
1660 ImGui::Checkbox("Override TextSize", &fPaintOverrides.fTextSize);
1661 if (fPaintOverrides.fTextSize) {
1662 ImGui::DragFloat2("TextRange", fPaintOverrides.fTextSizeRange,
1663 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
1664 float textSize = fPaint.getTextSize();
1665 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
1666 fPaintOverrides.fTextSizeRange[0],
1667 fPaintOverrides.fTextSizeRange[1],
1668 "%.6f", 2.0f))
1669 {
1670 fPaint.setTextSize(textSize);
1671 this->preTouchMatrixChanged();
1672 paramsChanged = true;
1673 }
1674 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001675 }
1676
Mike Reed81f60ec2018-05-15 10:09:52 -04001677 {
1678 SkMetaData controls;
1679 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1680 if (ImGui::CollapsingHeader("Current Slide")) {
1681 SkMetaData::Iter iter(controls);
1682 const char* name;
1683 SkMetaData::Type type;
1684 int count;
1685 bool found = false;
1686 while ((name = iter.next(&type, &count)) != nullptr && found == false) {
1687 if (type == SkMetaData::kScalar_Type) {
1688 float val[3];
1689 SkASSERT(count == 3);
1690 controls.findScalars(name, &count, val);
1691 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1692 controls.setScalars(name, 3, val);
1693 fSlides[fCurrentSlide]->onSetControls(controls);
1694 found = paramsChanged = true;
1695 }
1696 }
1697 }
1698 }
1699 }
1700 }
1701
Ben Wagner7a3c6742018-04-23 10:01:07 -04001702 if (fShowSlidePicker) {
1703 ImGui::SetNextTreeNodeOpen(true);
1704 }
Brian Osman79086b92017-02-10 13:36:16 -05001705 if (ImGui::CollapsingHeader("Slide")) {
1706 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001707 static ImVector<const char*> filteredSlideNames;
1708 static ImVector<int> filteredSlideIndices;
1709
Brian Osmanfce09c52017-11-14 15:32:20 -05001710 if (fShowSlidePicker) {
1711 ImGui::SetKeyboardFocusHere();
1712 fShowSlidePicker = false;
1713 }
1714
Brian Osman79086b92017-02-10 13:36:16 -05001715 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001716 filteredSlideNames.clear();
1717 filteredSlideIndices.clear();
1718 int filteredIndex = 0;
1719 for (int i = 0; i < fSlides.count(); ++i) {
1720 const char* slideName = fSlides[i]->getName().c_str();
1721 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1722 if (i == fCurrentSlide) {
1723 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001724 }
Brian Osmanf479e422017-11-08 13:11:36 -05001725 filteredSlideNames.push_back(slideName);
1726 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001727 }
Brian Osman79086b92017-02-10 13:36:16 -05001728 }
Brian Osmanf479e422017-11-08 13:11:36 -05001729
Brian Osmanf479e422017-11-08 13:11:36 -05001730 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1731 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001732 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001733 }
1734 }
Brian Osmana109e392017-02-24 09:49:14 -05001735
1736 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001737 ColorMode newMode = fColorMode;
1738 auto cmButton = [&](ColorMode mode, const char* label) {
1739 if (ImGui::RadioButton(label, mode == fColorMode)) {
1740 newMode = mode;
1741 }
1742 };
1743
1744 cmButton(ColorMode::kLegacy, "Legacy 8888");
1745 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1746 "Color Managed 8888 (Nonlinear blending)");
1747 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1748 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1749
1750 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001751 // It isn't safe to switch color mode now (in the middle of painting). We might
1752 // tear down the back-end, etc... Defer this change until the next onIdle.
1753 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001754 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001755 });
Brian Osmana109e392017-02-24 09:49:14 -05001756 }
1757
1758 // Pick from common gamuts:
1759 int primariesIdx = 4; // Default: Custom
1760 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1761 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1762 primariesIdx = i;
1763 break;
1764 }
1765 }
1766
Brian Osmanfdab5762017-11-09 10:27:55 -05001767 // When we're in xform canvas mode, we can alter the transfer function, too
1768 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1769 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1770 }
1771
Brian Osmana109e392017-02-24 09:49:14 -05001772 if (ImGui::Combo("Primaries", &primariesIdx,
1773 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1774 if (primariesIdx >= 0 && primariesIdx <= 3) {
1775 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1776 }
1777 }
1778
1779 // Allow direct editing of gamut
1780 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1781 }
Brian Osman79086b92017-02-10 13:36:16 -05001782 }
Brian Salomon99a33902017-03-07 15:16:34 -05001783 if (paramsChanged) {
1784 fDeferredActions.push_back([=]() {
1785 fWindow->setRequestedDisplayParams(params);
1786 fWindow->inval();
1787 this->updateTitle();
1788 });
1789 }
Brian Osman79086b92017-02-10 13:36:16 -05001790 ImGui::End();
1791 }
1792
Brian Osmanf6877092017-02-13 09:39:57 -05001793 if (fShowZoomWindow && fLastImage) {
1794 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001795 static int zoomFactor = 8;
1796 if (ImGui::Button("<<")) {
1797 zoomFactor = SkTMax(zoomFactor / 2, 4);
1798 }
1799 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1800 if (ImGui::Button(">>")) {
1801 zoomFactor = SkTMin(zoomFactor * 2, 32);
1802 }
Brian Osmanf6877092017-02-13 09:39:57 -05001803
Brian Osmanf6877092017-02-13 09:39:57 -05001804 ImVec2 mousePos = ImGui::GetMousePos();
1805 ImVec2 avail = ImGui::GetContentRegionAvail();
1806
Brian Osmanead517d2017-11-13 15:36:36 -05001807 uint32_t pixel = 0;
1808 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1809 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1810 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001811 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1812 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1813 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001814 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1815 }
1816
Brian Osmand67e5182017-12-08 16:46:09 -05001817 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001818 // Translate so the region of the image that's under the mouse cursor is centered
1819 // in the zoom canvas:
1820 c->scale(zoomFactor, zoomFactor);
1821 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1822 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1823 c->drawImage(this->fLastImage, 0, 0);
1824
1825 SkPaint outline;
1826 outline.setStyle(SkPaint::kStroke_Style);
1827 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1828 });
Brian Osmanf6877092017-02-13 09:39:57 -05001829 }
1830
1831 ImGui::End();
1832 }
Brian Osman79086b92017-02-10 13:36:16 -05001833}
1834
liyuqian2edb0f42016-07-06 14:11:32 -07001835void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001836 for (int i = 0; i < fDeferredActions.count(); ++i) {
1837 fDeferredActions[i]();
1838 }
1839 fDeferredActions.reset();
1840
Brian Osman56a24812017-12-19 11:15:16 -05001841 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001842 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001843 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001844 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001845
Brian Osman79086b92017-02-10 13:36:16 -05001846 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001847 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001848 fWindow->inval();
1849 }
jvanverth9f372462016-04-06 06:08:59 -07001850}
liyuqiane5a6cd92016-05-27 08:52:52 -07001851
1852void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001853 if (!fWindow) {
1854 return;
1855 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001856 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001857 return; // Surface hasn't been created yet.
1858 }
1859
liyuqianb73c24b2016-06-03 08:47:23 -07001860 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001861 Json::Value slideState(Json::objectValue);
1862 slideState[kName] = kSlideStateName;
1863 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001864 if (fAllSlideNames.size() == 0) {
1865 for(auto slide : fSlides) {
1866 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1867 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001868 }
liyuqian1f508fd2016-06-07 06:57:40 -07001869 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001870
liyuqianb73c24b2016-06-03 08:47:23 -07001871 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001872 Json::Value backendState(Json::objectValue);
1873 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001874 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001875 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001876 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001877 backendState[kOptions].append(Json::Value(str));
1878 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001879
csmartdalton578f0642017-02-24 16:04:47 -07001880 // MSAA state
1881 Json::Value msaaState(Json::objectValue);
1882 msaaState[kName] = kMSAAStateName;
1883 msaaState[kValue] = fWindow->sampleCount();
1884 msaaState[kOptions] = Json::Value(Json::arrayValue);
1885 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1886 msaaState[kOptions].append(Json::Value(0));
1887 } else {
1888 for (int msaa : {0, 4, 8, 16}) {
1889 msaaState[kOptions].append(Json::Value(msaa));
1890 }
1891 }
1892
csmartdalton61cd31a2017-02-27 17:00:53 -07001893 // Path renderer state
1894 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1895 Json::Value prState(Json::objectValue);
1896 prState[kName] = kPathRendererStateName;
1897 prState[kValue] = gPathRendererNames[pr];
1898 prState[kOptions] = Json::Value(Json::arrayValue);
1899 const GrContext* ctx = fWindow->getGrContext();
1900 if (!ctx) {
1901 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001902 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001903 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001904 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001905 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
csmartdalton61cd31a2017-02-27 17:00:53 -07001906 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1907 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001908 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001909 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1910 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001911 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001912 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001913 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001914 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1915 }
Jim Van Verth83010462017-03-16 08:45:39 -04001916 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001917 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001918 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1919 }
1920
liyuqianb73c24b2016-06-03 08:47:23 -07001921 // Softkey state
1922 Json::Value softkeyState(Json::objectValue);
1923 softkeyState[kName] = kSoftkeyStateName;
1924 softkeyState[kValue] = kSoftkeyHint;
1925 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1926 softkeyState[kOptions].append(kSoftkeyHint);
1927 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1928 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1929 }
1930
liyuqiane5a6cd92016-05-27 08:52:52 -07001931 Json::Value state(Json::arrayValue);
1932 state.append(slideState);
1933 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001934 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001935 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001936 state.append(softkeyState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001937
Brian Osmaneff04b52017-11-21 13:18:02 -05001938 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001939}
1940
1941void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001942 // For those who will add more features to handle the state change in this function:
1943 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1944 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1945 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001946 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001947 for (int i = 0; i < fSlides.count(); ++i) {
1948 if (fSlides[i]->getName().equals(stateValue)) {
1949 this->setCurrentSlide(i);
1950 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001951 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001952 }
Florin Malitaab99c342018-01-16 16:23:03 -05001953
1954 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001955 } else if (stateName.equals(kBackendStateName)) {
1956 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1957 if (stateValue.equals(kBackendTypeStrings[i])) {
1958 if (fBackendType != i) {
1959 fBackendType = (sk_app::Window::BackendType)i;
1960 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001961 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001962 }
1963 break;
1964 }
1965 }
csmartdalton578f0642017-02-24 16:04:47 -07001966 } else if (stateName.equals(kMSAAStateName)) {
1967 DisplayParams params = fWindow->getRequestedDisplayParams();
1968 int sampleCount = atoi(stateValue.c_str());
1969 if (sampleCount != params.fMSAASampleCount) {
1970 params.fMSAASampleCount = sampleCount;
1971 fWindow->setRequestedDisplayParams(params);
1972 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001973 this->updateTitle();
1974 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001975 }
1976 } else if (stateName.equals(kPathRendererStateName)) {
1977 DisplayParams params = fWindow->getRequestedDisplayParams();
1978 for (const auto& pair : gPathRendererNames) {
1979 if (pair.second == stateValue.c_str()) {
1980 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1981 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1982 fWindow->setRequestedDisplayParams(params);
1983 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001984 this->updateTitle();
1985 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001986 }
1987 break;
1988 }
csmartdalton578f0642017-02-24 16:04:47 -07001989 }
liyuqianb73c24b2016-06-03 08:47:23 -07001990 } else if (stateName.equals(kSoftkeyStateName)) {
1991 if (!stateValue.equals(kSoftkeyHint)) {
1992 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001993 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001994 }
liyuqian2edb0f42016-07-06 14:11:32 -07001995 } else if (stateName.equals(kRefreshStateName)) {
1996 // This state is actually NOT in the UI state.
1997 // We use this to allow Android to quickly set bool fRefresh.
1998 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001999 } else {
2000 SkDebugf("Unknown stateName: %s", stateName.c_str());
2001 }
2002}
Brian Osman79086b92017-02-10 13:36:16 -05002003
2004bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002005 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002006}
2007
2008bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002009 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002010 fWindow->inval();
2011 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002012 } else {
2013 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002014 }
Brian Osman79086b92017-02-10 13:36:16 -05002015}