blob: d6d1fb3f342a42444e84d5c9266ff018d0d9893d [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
jvanverth34524262016-05-04 13:49:13 -07008#include "Viewer.h"
jvanverth9f372462016-04-06 06:08:59 -07009
Chris Dalton2d18f412018-02-20 13:23:32 -070010#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070011#include "GMSlide.h"
liyuqian6f163d22016-06-13 12:26:45 -070012#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050013#include "Resources.h"
jvanverthc7027ab2016-06-16 09:52:35 -070014#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070015#include "SKPSlide.h"
Florin Malita76a076b2018-02-15 18:40:48 -050016#include "SlideDir.h"
Florin Malitac659c2c2018-04-05 11:57:21 -040017#include "SvgSlide.h"
jvanverth9f372462016-04-06 06:08:59 -070018
csmartdalton61cd31a2017-02-27 17:00:53 -070019#include "GrContext.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070020#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050021#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040022#include "SkColorSpaceXformCanvas.h"
Ben Wagner483c7722018-02-20 17:06:07 -050023#include "SkCommonFlags.h"
Brian Osman2dd96932016-10-18 15:33:53 -040024#include "SkCommandLineFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070025#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040026#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050027#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040028#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050029#include "SkImagePriv.h"
Herb Derbyefe39bc2018-05-01 17:06:20 -040030#include "SkMakeUnique.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070031#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050032#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050033#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050034#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040035#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070036#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070037#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050038#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050039#include "SkTestFontMgr.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050040#include "SkThreadedBMPDevice.h"
jvanverth9f372462016-04-06 06:08:59 -070041
Brian Osman79086b92017-02-10 13:36:16 -050042#include "imgui.h"
43
Chris Dalton1a325d22017-07-14 15:17:41 -060044#include "ccpr/GrCoverageCountingPathRenderer.h"
45
csmartdalton578f0642017-02-24 16:04:47 -070046#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070047#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070048
Florin Malita87ccf332018-05-04 12:23:24 -040049#if defined(SK_ENABLE_SKOTTIE)
50 #include "SkottieSlide.h"
51#endif
52
jvanverth34524262016-05-04 13:49:13 -070053using namespace sk_app;
54
csmartdalton61cd31a2017-02-27 17:00:53 -070055static std::map<GpuPathRenderers, std::string> gPathRendererNames;
56
jvanverth9f372462016-04-06 06:08:59 -070057Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070058 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070059}
60
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060061static DEFINE_string(slide, "", "Start on this sample.");
62static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050063
bsalomon6c471f72016-07-26 12:56:32 -070064#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070065# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070066#else
67# define BACKENDS_STR "\"sw\" and \"gl\""
68#endif
69
Brian Osman2dd96932016-10-18 15:33:53 -040070static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070071
Brian Salomonbdecacf2018-02-02 20:32:49 -050072static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070073
Chris Dalton2d18f412018-02-20 13:23:32 -070074DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
75
Brian Osman53136aa2017-07-20 15:43:35 -040076DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050077
Florin Malita38792ce2018-05-08 10:36:18 -040078DEFINE_string2(file, f, "", "Open a single file for viewing.");
79
Brian Salomon194db172017-08-17 14:37:06 -040080const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070081 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040082#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
83 "ANGLE",
84#endif
jvanverth063ece72016-06-17 09:29:14 -070085#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070086 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070087#endif
csmartdalton578f0642017-02-24 16:04:47 -070088 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070089};
90
bsalomon6c471f72016-07-26 12:56:32 -070091static sk_app::Window::BackendType get_backend_type(const char* str) {
92#ifdef SK_VULKAN
93 if (0 == strcmp(str, "vk")) {
94 return sk_app::Window::kVulkan_BackendType;
95 } else
96#endif
Brian Salomon194db172017-08-17 14:37:06 -040097#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
98 if (0 == strcmp(str, "angle")) {
99 return sk_app::Window::kANGLE_BackendType;
100 } else
101#endif
bsalomon6c471f72016-07-26 12:56:32 -0700102 if (0 == strcmp(str, "gl")) {
103 return sk_app::Window::kNativeGL_BackendType;
104 } else if (0 == strcmp(str, "sw")) {
105 return sk_app::Window::kRaster_BackendType;
106 } else {
107 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
108 return sk_app::Window::kRaster_BackendType;
109 }
110}
111
Brian Osmana109e392017-02-24 09:49:14 -0500112static SkColorSpacePrimaries gSrgbPrimaries = {
113 0.64f, 0.33f,
114 0.30f, 0.60f,
115 0.15f, 0.06f,
116 0.3127f, 0.3290f };
117
118static SkColorSpacePrimaries gAdobePrimaries = {
119 0.64f, 0.33f,
120 0.21f, 0.71f,
121 0.15f, 0.06f,
122 0.3127f, 0.3290f };
123
124static SkColorSpacePrimaries gP3Primaries = {
125 0.680f, 0.320f,
126 0.265f, 0.690f,
127 0.150f, 0.060f,
128 0.3127f, 0.3290f };
129
130static SkColorSpacePrimaries gRec2020Primaries = {
131 0.708f, 0.292f,
132 0.170f, 0.797f,
133 0.131f, 0.046f,
134 0.3127f, 0.3290f };
135
136struct NamedPrimaries {
137 const char* fName;
138 SkColorSpacePrimaries* fPrimaries;
139} gNamedPrimaries[] = {
140 { "sRGB", &gSrgbPrimaries },
141 { "AdobeRGB", &gAdobePrimaries },
142 { "P3", &gP3Primaries },
143 { "Rec. 2020", &gRec2020Primaries },
144};
145
146static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
147 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
148}
149
Brian Osman70d2f432017-11-08 09:54:10 -0500150static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
151 // In raster mode, we still use GL for the window.
152 // This lets us render the GUI faster (and correct).
153 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
154}
155
liyuqiane5a6cd92016-05-27 08:52:52 -0700156const char* kName = "name";
157const char* kValue = "value";
158const char* kOptions = "options";
159const char* kSlideStateName = "Slide";
160const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700161const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700162const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700163const char* kSoftkeyStateName = "Softkey";
164const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700165const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700166const char* kON = "ON";
167const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700168const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700169
jvanverth34524262016-05-04 13:49:13 -0700170Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500171 : fCurrentSlide(-1)
172 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500173 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500174 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500175 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500176 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500177 , fShowZoomWindow(false)
178 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700179 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500180 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500181 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500182 // Our UI can only tweak gamma (currently), so start out gamma-only
183 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700184 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400185 , fRotation(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400186 , fGestureDevice(GestureDevice::kNone)
Brian Osman805a7272018-05-02 15:40:20 -0400187 , fPerspectiveMode(kPerspective_Off)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500188 , fTileCnt(0)
189 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700190{
Greg Daniel285db442016-10-14 09:12:53 -0400191 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700192
Brian Osmanf09e35e2017-12-15 14:48:09 -0500193 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
194 gPathRendererNames[GpuPathRenderers::kDefault] =
195 "Default Ganesh Behavior (best path renderer, not including CCPR)";
196 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500197 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
198 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
199 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
200 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700201
jvanverth2bb3b6d2016-04-08 07:24:09 -0700202 SkDebugf("Command line arguments: ");
203 for (int i = 1; i < argc; ++i) {
204 SkDebugf("%s ", argv[i]);
205 }
206 SkDebugf("\n");
207
208 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500209#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400210 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500211#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700212
Ben Wagner483c7722018-02-20 17:06:07 -0500213 if (!FLAGS_nativeFonts) {
214 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
215 }
216
Brian Osmanbc8150f2017-07-24 11:38:01 -0400217 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400218 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400219
bsalomon6c471f72016-07-26 12:56:32 -0700220 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700221 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700222
csmartdalton578f0642017-02-24 16:04:47 -0700223 DisplayParams displayParams;
224 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700225 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700226 fWindow->setRequestedDisplayParams(displayParams);
227
Brian Osman56a24812017-12-19 11:15:16 -0500228 // Configure timers
229 fStatsLayer.setActive(false);
230 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
231 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
232 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
233
jvanverth9f372462016-04-06 06:08:59 -0700234 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700235 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500236 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500237 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500238 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700239
brianosman622c8d52016-05-10 06:50:49 -0700240 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500241 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
242 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
243 fWindow->inval();
244 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500245 // Command to jump directly to the slide picker and give it focus
246 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
247 this->fShowImGuiDebugWindow = true;
248 this->fShowSlidePicker = true;
249 fWindow->inval();
250 });
251 // Alias that to Backspace, to match SampleApp
252 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
253 this->fShowImGuiDebugWindow = true;
254 this->fShowSlidePicker = true;
255 fWindow->inval();
256 });
Brian Osman79086b92017-02-10 13:36:16 -0500257 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
258 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
259 fWindow->inval();
260 });
Brian Osmanf6877092017-02-13 09:39:57 -0500261 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
262 this->fShowZoomWindow = !this->fShowZoomWindow;
263 fWindow->inval();
264 });
brianosman622c8d52016-05-10 06:50:49 -0700265 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500266 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700267 fWindow->inval();
268 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400269 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500270 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400271 this->updateTitle();
272 fWindow->inval();
273 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500274 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500275 switch (fColorMode) {
276 case ColorMode::kLegacy:
277 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
278 break;
279 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
280 this->setColorMode(ColorMode::kColorManagedSRGB8888);
281 break;
282 case ColorMode::kColorManagedSRGB8888:
283 this->setColorMode(ColorMode::kColorManagedLinearF16);
284 break;
285 case ColorMode::kColorManagedLinearF16:
286 this->setColorMode(ColorMode::kLegacy);
287 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500288 }
brianosman622c8d52016-05-10 06:50:49 -0700289 });
290 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500291 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700292 });
293 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500294 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700295 });
296 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
297 this->changeZoomLevel(1.f / 32.f);
298 fWindow->inval();
299 });
300 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
301 this->changeZoomLevel(-1.f / 32.f);
302 fWindow->inval();
303 });
jvanverthaf236b52016-05-20 06:01:06 -0700304 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400305 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
306 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500307 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400308#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
309 if (newBackend == sk_app::Window::kVulkan_BackendType) {
310 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
311 sk_app::Window::kBackendTypeCount);
312 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
313 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500314 }
315#endif
Brian Osman621491e2017-02-28 15:45:01 -0500316 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700317 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500318 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
319 fTileCnt++;
320 if (fThreadCnt == 0) {
321 this->resetExecutor();
322 }
323 this->updateTitle();
324 fWindow->inval();
325 });
326 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
327 fTileCnt = SkTMax(0, fTileCnt - 1);
328 if (fThreadCnt == 0) {
329 this->resetExecutor();
330 }
331 this->updateTitle();
332 fWindow->inval();
333 });
334 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
335 if (fTileCnt == 0) {
336 return;
337 }
338 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
339 this->resetExecutor();
340 this->updateTitle();
341 fWindow->inval();
342 });
343 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
344 if (fTileCnt == 0) {
345 return;
346 }
347 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
348 this->resetExecutor();
349 this->updateTitle();
350 fWindow->inval();
351 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500352 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
353 fSaveToSKP = true;
354 fWindow->inval();
355 });
Ben Wagner37c54032018-04-13 14:30:23 -0400356 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
357 DisplayParams params = fWindow->getRequestedDisplayParams();
358 uint32_t flags = params.fSurfaceProps.flags();
359 if (!fPixelGeometryOverrides) {
360 fPixelGeometryOverrides = true;
361 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
362 } else {
363 switch (params.fSurfaceProps.pixelGeometry()) {
364 case kUnknown_SkPixelGeometry:
365 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
366 break;
367 case kRGB_H_SkPixelGeometry:
368 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
369 break;
370 case kBGR_H_SkPixelGeometry:
371 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
372 break;
373 case kRGB_V_SkPixelGeometry:
374 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
375 break;
376 case kBGR_V_SkPixelGeometry:
377 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
378 fPixelGeometryOverrides = false;
379 break;
380 }
381 }
382 fWindow->setRequestedDisplayParams(params);
383 this->updateTitle();
384 fWindow->inval();
385 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500386 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
387 if (!fPaintOverrides.fHinting) {
388 fPaintOverrides.fHinting = true;
389 fPaint.setHinting(SkPaint::kNo_Hinting);
390 } else {
391 switch (fPaint.getHinting()) {
392 case SkPaint::kNo_Hinting:
393 fPaint.setHinting(SkPaint::kSlight_Hinting);
394 break;
395 case SkPaint::kSlight_Hinting:
396 fPaint.setHinting(SkPaint::kNormal_Hinting);
397 break;
398 case SkPaint::kNormal_Hinting:
399 fPaint.setHinting(SkPaint::kFull_Hinting);
400 break;
401 case SkPaint::kFull_Hinting:
402 fPaint.setHinting(SkPaint::kNo_Hinting);
403 fPaintOverrides.fHinting = false;
404 break;
405 }
406 }
407 this->updateTitle();
408 fWindow->inval();
409 });
410 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
411 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
412 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
413 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
414 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500415 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
416 gSkUseDeltaAA = gSkForceDeltaAA = false;
417 } else {
418 fPaint.setAntiAlias(true);
419 switch (fPaintOverrides.fAntiAlias) {
420 case SkPaintFields::AntiAliasState::Alias:
421 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400422 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
423 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500424 break;
425 case SkPaintFields::AntiAliasState::Normal:
426 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500427 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400428 gSkForceAnalyticAA = false;
429 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500430 break;
431 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
432 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400433 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
434 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500435 break;
436 case SkPaintFields::AntiAliasState::AnalyticAAForced:
437 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
438 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
439 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400440 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500441 break;
442 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
443 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400444 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
445 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500446 break;
447 case SkPaintFields::AntiAliasState::DeltaAAForced:
448 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
449 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
450 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
451 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
452 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
453 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
454 break;
455 }
456 }
457 this->updateTitle();
458 fWindow->inval();
459 });
Ben Wagner37c54032018-04-13 14:30:23 -0400460 fCommands.addCommand('D', "Modes", "DFT", [this]() {
461 DisplayParams params = fWindow->getRequestedDisplayParams();
462 uint32_t flags = params.fSurfaceProps.flags();
463 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
464 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
465 fWindow->setRequestedDisplayParams(params);
466 this->updateTitle();
467 fWindow->inval();
468 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500469 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
470 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
471 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
472 fPaint.setLCDRenderText(false);
473 } else {
474 if (!fPaint.isLCDRenderText()) {
475 fPaint.setLCDRenderText(true);
476 } else {
477 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
478 }
479 }
480 this->updateTitle();
481 fWindow->inval();
482 });
483 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
484 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
485 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
486 fPaint.setSubpixelText(false);
487 } else {
488 if (!fPaint.isSubpixelText()) {
489 fPaint.setSubpixelText(true);
490 } else {
491 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
492 }
493 }
494 this->updateTitle();
495 fWindow->inval();
496 });
Brian Osman805a7272018-05-02 15:40:20 -0400497 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
498 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
499 : kPerspective_Real;
500 this->updateTitle();
501 fWindow->inval();
502 });
503 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
504 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
505 : kPerspective_Off;
506 this->updateTitle();
507 fWindow->inval();
508 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500509
jvanverth2bb3b6d2016-04-08 07:24:09 -0700510 // set up slides
511 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500512 if (FLAGS_list) {
513 this->listNames();
514 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700515
Brian Osman9bb47cf2018-04-26 15:55:00 -0400516 fPerspectivePoints[0].set(0, 0);
517 fPerspectivePoints[1].set(1, 0);
518 fPerspectivePoints[2].set(0, 1);
519 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700520 fAnimTimer.run();
521
Hal Canaryc465d132017-12-08 10:21:31 -0500522 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500523 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400524 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500525 }
526 fImGuiGamutPaint.setColor(SK_ColorWHITE);
527 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
528
Brian Osman70d2f432017-11-08 09:54:10 -0500529 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500530 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700531}
532
jvanverth34524262016-05-04 13:49:13 -0700533void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700534 fAllSlideNames = Json::Value(Json::arrayValue);
535
Florin Malita0ffa3222018-04-05 14:34:45 -0400536 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
537 static const struct {
538 const char* fExtension;
539 const char* fDirName;
540 const SkCommandLineFlags::StringArray& fFlags;
541 const SlideFactory fFactory;
542 } gExternalSlidesInfo[] = {
543 { ".skp", "skp-dir", FLAGS_skps,
544 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
545 return sk_make_sp<SKPSlide>(name, path);}
546 },
547 { ".jpg", "jpg-dir", FLAGS_jpgs,
548 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
549 return sk_make_sp<ImageSlide>(name, path);}
550 },
Florin Malita87ccf332018-05-04 12:23:24 -0400551#if defined(SK_ENABLE_SKOTTIE)
Florin Malita0ffa3222018-04-05 14:34:45 -0400552 { ".json", "skottie-dir", FLAGS_jsons,
553 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
554 return sk_make_sp<SkottieSlide>(name, path);}
555 },
Florin Malita87ccf332018-05-04 12:23:24 -0400556#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400557 { ".svg", "svg-dir", FLAGS_svgs,
558 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
559 return sk_make_sp<SvgSlide>(name, path);}
560 },
561 };
jvanverthc265a922016-04-08 12:51:45 -0700562
Florin Malita0ffa3222018-04-05 14:34:45 -0400563 SkTArray<sk_sp<Slide>, true> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700564
Florin Malita0ffa3222018-04-05 14:34:45 -0400565 const auto addSlide = [&](const SkString& name,
566 const SkString& path,
567 const SlideFactory& fact) {
568 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
569 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700570 }
liyuqian6f163d22016-06-13 12:26:45 -0700571
Florin Malita0ffa3222018-04-05 14:34:45 -0400572 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500573 dirSlides.push_back(slide);
574 fSlides.push_back(std::move(slide));
575 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400576 };
Florin Malita76a076b2018-02-15 18:40:48 -0500577
Florin Malita38792ce2018-05-08 10:36:18 -0400578 if (!FLAGS_file.isEmpty()) {
579 // single file mode
580 const SkString file(FLAGS_file[0]);
581
582 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
583 for (const auto& sinfo : gExternalSlidesInfo) {
584 if (file.endsWith(sinfo.fExtension)) {
585 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
586 return;
587 }
588 }
589
590 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
591 } else {
592 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
593 }
594
595 return;
596 }
597
598 // Bisect slide.
599 if (!FLAGS_bisect.isEmpty()) {
600 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
601 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
602 if (FLAGS_bisect.count() >= 2) {
603 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
604 bisect->onChar(*ch);
605 }
606 }
607 fSlides.push_back(std::move(bisect));
608 }
609 }
610
611 // GMs
612 int firstGM = fSlides.count();
613 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
614 while (gms) {
615 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
616
617 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
618 sk_sp<Slide> slide(new GMSlide(gm.release()));
619 fSlides.push_back(std::move(slide));
620 }
621
622 gms = gms->next();
623 }
624 // reverse gms
625 int numGMs = fSlides.count() - firstGM;
626 for (int i = 0; i < numGMs/2; ++i) {
627 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
628 }
629
630 // samples
631 const SkViewRegister* reg = SkViewRegister::Head();
632 while (reg) {
633 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
634 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
635 fSlides.push_back(slide);
636 }
637 reg = reg->next();
638 }
639
Florin Malita0ffa3222018-04-05 14:34:45 -0400640 for (const auto& info : gExternalSlidesInfo) {
641 for (const auto& flag : info.fFlags) {
642 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
643 // single file
644 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
645 } else {
646 // directory
647 SkOSFile::Iter it(flag.c_str(), info.fExtension);
648 SkString name;
649 while (it.next(&name)) {
650 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
651 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400652 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400653
654 if (!dirSlides.empty()) {
655 fSlides.push_back(
656 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
657 std::move(dirSlides)));
658 dirSlides.reset();
659 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400660 }
661 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700662}
663
664
jvanverth34524262016-05-04 13:49:13 -0700665Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700666 fWindow->detach();
667 delete fWindow;
668}
669
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500670struct SkPaintTitleUpdater {
671 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
672 void append(const char* s) {
673 if (fCount == 0) {
674 fTitle->append(" {");
675 } else {
676 fTitle->append(", ");
677 }
678 fTitle->append(s);
679 ++fCount;
680 }
681 void done() {
682 if (fCount > 0) {
683 fTitle->append("}");
684 }
685 }
686 SkString* fTitle;
687 int fCount;
688};
689
brianosman05de2162016-05-06 13:28:57 -0700690void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700691 if (!fWindow) {
692 return;
693 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500694 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700695 return; // Surface hasn't been created yet.
696 }
697
jvanverth34524262016-05-04 13:49:13 -0700698 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700699 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700700
Yuqian Li399b3c22017-08-03 11:08:15 -0400701 if (gSkUseDeltaAA) {
702 if (gSkForceDeltaAA) {
703 title.append(" <FDAA>");
704 } else {
705 title.append(" <DAA>");
706 }
707 } else if (gSkUseAnalyticAA) {
708 if (gSkForceAnalyticAA) {
709 title.append(" <FAAA>");
710 } else {
711 title.append(" <AAA>");
712 }
713 }
714
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500715 SkPaintTitleUpdater paintTitle(&title);
716 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
717 if (fPaint.isAntiAlias()) {
718 paintTitle.append("Antialias");
719 } else {
720 paintTitle.append("Alias");
721 }
722 }
723 if (fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag) {
724 if (fPaint.isLCDRenderText()) {
725 paintTitle.append("LCD");
726 } else {
727 paintTitle.append("lcd");
728 }
729 }
730 if (fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag) {
731 if (fPaint.isSubpixelText()) {
732 paintTitle.append("Subpixel Glyphs");
733 } else {
734 paintTitle.append("Pixel Glyphs");
735 }
736 }
737 if (fPaintOverrides.fHinting) {
738 switch (fPaint.getHinting()) {
739 case SkPaint::kNo_Hinting:
740 paintTitle.append("No Hinting");
741 break;
742 case SkPaint::kSlight_Hinting:
743 paintTitle.append("Slight Hinting");
744 break;
745 case SkPaint::kNormal_Hinting:
746 paintTitle.append("Normal Hinting");
747 break;
748 case SkPaint::kFull_Hinting:
749 paintTitle.append("Full Hinting");
750 break;
751 }
752 }
753 paintTitle.done();
754
Yuqian Lib2ba6642017-11-22 12:07:41 -0500755 if (fTileCnt > 0) {
756 title.appendf(" T%d", fTileCnt);
757 if (fThreadCnt > 0) {
758 title.appendf("/%d", fThreadCnt);
759 }
760 }
761
Brian Osman92004802017-03-06 11:47:26 -0500762 switch (fColorMode) {
763 case ColorMode::kLegacy:
764 title.append(" Legacy 8888");
765 break;
766 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
767 title.append(" ColorManaged 8888 (Nonlinear blending)");
768 break;
769 case ColorMode::kColorManagedSRGB8888:
770 title.append(" ColorManaged 8888");
771 break;
772 case ColorMode::kColorManagedLinearF16:
773 title.append(" ColorManaged F16");
774 break;
775 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500776
Brian Osman92004802017-03-06 11:47:26 -0500777 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500778 int curPrimaries = -1;
779 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
780 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
781 curPrimaries = i;
782 break;
783 }
784 }
785 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500786
787 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
788 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
789 }
brianosman05de2162016-05-06 13:28:57 -0700790 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500791
Ben Wagner37c54032018-04-13 14:30:23 -0400792 const DisplayParams& params = fWindow->getRequestedDisplayParams();
793 if (fPixelGeometryOverrides) {
794 switch (params.fSurfaceProps.pixelGeometry()) {
795 case kUnknown_SkPixelGeometry:
796 title.append( " Flat");
797 break;
798 case kRGB_H_SkPixelGeometry:
799 title.append( " RGB");
800 break;
801 case kBGR_H_SkPixelGeometry:
802 title.append( " BGR");
803 break;
804 case kRGB_V_SkPixelGeometry:
805 title.append( " RGBV");
806 break;
807 case kBGR_V_SkPixelGeometry:
808 title.append( " BGRV");
809 break;
810 }
811 }
812
813 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
814 title.append(" DFT");
815 }
816
csmartdalton578f0642017-02-24 16:04:47 -0700817 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700818 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500819 int msaa = fWindow->sampleCount();
820 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700821 title.appendf(" MSAA: %i", msaa);
822 }
823 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700824
825 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400826 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700827 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
828 }
829
Brian Osman805a7272018-05-02 15:40:20 -0400830 if (kPerspective_Real == fPerspectiveMode) {
831 title.append(" Perpsective (Real)");
832 } else if (kPerspective_Fake == fPerspectiveMode) {
833 title.append(" Perspective (Fake)");
834 }
835
brianosman05de2162016-05-06 13:28:57 -0700836 fWindow->setTitle(title.c_str());
837}
838
Florin Malitaab99c342018-01-16 16:23:03 -0500839int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500840
841 if (!FLAGS_slide.isEmpty()) {
842 int count = fSlides.count();
843 for (int i = 0; i < count; i++) {
844 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500845 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500846 }
847 }
848
849 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
850 this->listNames();
851 }
852
Florin Malitaab99c342018-01-16 16:23:03 -0500853 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500854}
855
Florin Malitaab99c342018-01-16 16:23:03 -0500856void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500857 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500858 for (const auto& slide : fSlides) {
859 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500860 }
861}
862
Florin Malitaab99c342018-01-16 16:23:03 -0500863void Viewer::setCurrentSlide(int slide) {
864 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700865
Florin Malitaab99c342018-01-16 16:23:03 -0500866 if (slide == fCurrentSlide) {
867 return;
868 }
869
870 if (fCurrentSlide >= 0) {
871 fSlides[fCurrentSlide]->unload();
872 }
873
874 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
875 SkIntToScalar(fWindow->height()));
876 fCurrentSlide = slide;
877 this->setupCurrentSlide();
878}
879
880void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500881 if (fCurrentSlide >= 0) {
882 // prepare dimensions for image slides
883 fGesture.resetTouchState();
884 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700885
Jim Van Verth0848fb02018-01-22 13:39:30 -0500886 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
887 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
888 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400889
Jim Van Verth0848fb02018-01-22 13:39:30 -0500890 // Start with a matrix that scales the slide to the available screen space
891 if (fWindow->scaleContentToFit()) {
892 if (windowRect.width() > 0 && windowRect.height() > 0) {
893 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
894 }
liyuqiane46e4f02016-05-20 07:32:19 -0700895 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500896
897 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400898 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500899
900 this->updateTitle();
901 this->updateUIState();
902
903 fStatsLayer.resetMeasurements();
904
905 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700906 }
jvanverthc265a922016-04-08 12:51:45 -0700907}
908
909#define MAX_ZOOM_LEVEL 8
910#define MIN_ZOOM_LEVEL -8
911
jvanverth34524262016-05-04 13:49:13 -0700912void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700913 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400914 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400915 this->preTouchMatrixChanged();
916}
Yuqian Li755778c2018-03-28 16:23:31 -0400917
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400918void Viewer::preTouchMatrixChanged() {
919 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400920 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
921 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
922 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
923 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
924}
925
Brian Osman805a7272018-05-02 15:40:20 -0400926SkMatrix Viewer::computePerspectiveMatrix() {
927 SkScalar w = fWindow->width(), h = fWindow->height();
928 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
929 SkPoint perspPts[4] = {
930 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
931 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
932 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
933 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
934 };
935 SkMatrix m;
936 m.setPolyToPoly(orthoPts, perspPts, 4);
937 return m;
938}
939
Yuqian Li755778c2018-03-28 16:23:31 -0400940SkMatrix Viewer::computePreTouchMatrix() {
941 SkMatrix m = fDefaultMatrix;
942 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
943 : SK_Scalar1 + fZoomLevel;
944 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400945
946 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
947 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400948
Brian Osman805a7272018-05-02 15:40:20 -0400949 if (kPerspective_Real == fPerspectiveMode) {
950 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400951 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400952 }
953
Yuqian Li755778c2018-03-28 16:23:31 -0400954 return m;
jvanverthc265a922016-04-08 12:51:45 -0700955}
956
liyuqiand3cdbca2016-05-17 12:44:20 -0700957SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400958 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700959 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400960 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700961 return m;
jvanverthc265a922016-04-08 12:51:45 -0700962}
963
Brian Osman621491e2017-02-28 15:45:01 -0500964void Viewer::setBackend(sk_app::Window::BackendType backendType) {
965 fBackendType = backendType;
966
967 fWindow->detach();
968
Brian Osman70d2f432017-11-08 09:54:10 -0500969#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400970 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
971 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500972 DisplayParams params = fWindow->getRequestedDisplayParams();
973 delete fWindow;
974 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500975
Brian Osman70d2f432017-11-08 09:54:10 -0500976 // re-register callbacks
977 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500978 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500979 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500980 fWindow->pushLayer(&fImGuiLayer);
981
Brian Osman70d2f432017-11-08 09:54:10 -0500982 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
983 // will still include our correct sample count. But the re-created fWindow will lose that
984 // information. On Windows, we need to re-create the window when changing sample count,
985 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
986 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
987 // as if we had never changed windows at all).
988 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500989#endif
990
Brian Osman70d2f432017-11-08 09:54:10 -0500991 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500992}
993
Brian Osman92004802017-03-06 11:47:26 -0500994void Viewer::setColorMode(ColorMode colorMode) {
995 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700996
Brian Osmanf750fbc2017-02-08 10:47:28 -0500997 // When we're in color managed mode, we tag our window surface as sRGB. If we've switched into
Brian Osmane0d4fba2017-03-15 10:24:55 -0400998 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700999 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -05001000 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001001 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
1002 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -05001003 if (wasInLegacy != wantLegacy) {
1004 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -07001005 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001006 }
1007
1008 this->updateTitle();
1009 fWindow->inval();
1010}
1011
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001012class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1013public:
1014 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
1015 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
1016 { }
1017 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001018 if (*paint == nullptr) {
1019 return true;
1020 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001021 if (fPaintOverrides->fHinting) {
1022 paint->writable()->setHinting(fPaint->getHinting());
1023 }
1024 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
1025 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1026 }
1027 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
1028 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
1029 }
1030 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
1031 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
1032 }
1033 return true;
1034 }
1035 SkPaint* fPaint;
1036 Viewer::SkPaintFields* fPaintOverrides;
1037};
1038
Brian Osmanf750fbc2017-02-08 10:47:28 -05001039void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001040 SkAutoCanvasRestore autorestore(canvas, false);
1041
Brian Osmanf750fbc2017-02-08 10:47:28 -05001042 // By default, we render directly into the window's surface/canvas
1043 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001044 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001045
Brian Osmane0d4fba2017-03-15 10:24:55 -04001046 // If we're in any of the color managed modes, construct the color space we're going to use
1047 sk_sp<SkColorSpace> cs = nullptr;
1048 if (ColorMode::kLegacy != fColorMode) {
1049 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
1050 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -04001051 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001052 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -05001053 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1054 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
1055 } else {
1056 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
1057 }
Brian Osmane0d4fba2017-03-15 10:24:55 -04001058 }
1059
Brian Osman3ac99cf2017-12-01 11:23:53 -05001060 if (fSaveToSKP) {
1061 SkPictureRecorder recorder;
1062 SkCanvas* recorderCanvas = recorder.beginRecording(
1063 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1064 // In xform-canvas mode, record the transformed output
1065 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1066 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1067 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
1068 recorderCanvas = xformCanvas.get();
1069 }
1070 fSlides[fCurrentSlide]->draw(recorderCanvas);
1071 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1072 SkFILEWStream stream("sample_app.skp");
1073 picture->serialize(&stream);
1074 fSaveToSKP = false;
1075 }
1076
Brian Osmane0d4fba2017-03-15 10:24:55 -04001077 // 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 -05001078 // 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 -04001079 // because the window surface is actually GL, or we're doing fake perspective.
Brian Osmanf750fbc2017-02-08 10:47:28 -05001080 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -05001081 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -05001082 if (Window::kRaster_BackendType == fBackendType ||
Brian Osman805a7272018-05-02 15:40:20 -04001083 kPerspective_Fake == fPerspectiveMode ||
Brian Osman70d2f432017-11-08 09:54:10 -05001084 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -05001085 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -04001086 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
1087 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
1088
Brian Osman92004802017-03-06 11:47:26 -05001089 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
1090 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001091 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
1092 // with a special canvas (below) that has the color space attached
1093 sk_sp<SkColorSpace> offscreenColorSpace =
1094 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -05001095 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -04001096 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Ben Wagner37c54032018-04-13 14:30:23 -04001097 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1098 canvas->getProps(&props);
1099 offscreenSurface = Window::kRaster_BackendType == fBackendType
1100 ? SkSurface::MakeRaster(info, &props)
1101 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -05001102 SkPixmap offscreenPixmap;
1103 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
1104 SkBitmap offscreenBitmap;
1105 offscreenBitmap.installPixels(offscreenPixmap);
Herb Derbyefe39bc2018-05-01 17:06:20 -04001106 threadedCanvas =
1107 skstd::make_unique<SkCanvas>(
1108 sk_make_sp<SkThreadedBMPDevice>(
1109 offscreenBitmap, fTileCnt, fThreadCnt, fExecutor.get()));
Yuqian Lib2ba6642017-11-22 12:07:41 -05001110 slideCanvas = threadedCanvas.get();
1111 } else {
1112 slideCanvas = offscreenSurface->getCanvas();
1113 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001114 }
1115
Brian Osmane0d4fba2017-03-15 10:24:55 -04001116 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1117 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1118 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
1119 slideCanvas = xformCanvas.get();
1120 }
1121
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001122 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001123 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001124 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -05001125 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001126 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001127 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1128 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001129 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001130 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001131
1132 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001133 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001134 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001135 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001136
1137 // If we rendered offscreen, snap an image and push the results to the window's canvas
1138 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001139 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001140
1141 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -04001142 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001143 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001144 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001145 SkPaint paint;
1146 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001147 int prePerspectiveCount = canvas->save();
1148 if (kPerspective_Fake == fPerspectiveMode) {
1149 paint.setFilterQuality(kHigh_SkFilterQuality);
1150 canvas->clear(SK_ColorWHITE);
1151 canvas->concat(this->computePerspectiveMatrix());
1152 }
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001153 canvas->drawImage(retaggedImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001154 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001155 }
liyuqian6f163d22016-06-13 12:26:45 -07001156}
1157
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001158void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001159 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001160 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001161}
Jim Van Verth6f449692017-02-14 15:16:46 -05001162
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001163void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001164 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001165
brianosman622c8d52016-05-10 06:50:49 -07001166 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001167
Brian Osmand67e5182017-12-08 16:46:09 -05001168 this->drawImGui();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001169}
1170
Florin Malitacefc1b92018-02-19 21:43:47 -05001171SkPoint Viewer::mapEvent(float x, float y) {
1172 const auto m = this->computeMatrix();
1173 SkMatrix inv;
1174
1175 SkAssertResult(m.invert(&inv));
1176
1177 return inv.mapXY(x, y);
1178}
1179
jvanverth814e38d2016-06-06 08:48:47 -07001180bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001181 if (GestureDevice::kMouse == fGestureDevice) {
1182 return false;
1183 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001184
1185 const auto slidePt = this->mapEvent(x, y);
1186 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1187 fWindow->inval();
1188 return true;
1189 }
1190
liyuqiand3cdbca2016-05-17 12:44:20 -07001191 void* castedOwner = reinterpret_cast<void*>(owner);
1192 switch (state) {
1193 case Window::kUp_InputState: {
1194 fGesture.touchEnd(castedOwner);
1195 break;
1196 }
1197 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001198 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001199 break;
1200 }
1201 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001202 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001203 break;
1204 }
1205 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001206 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001207 fWindow->inval();
1208 return true;
1209}
1210
Brian Osman80fc07e2017-12-08 16:45:43 -05001211bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001212 if (GestureDevice::kTouch == fGestureDevice) {
1213 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001214 }
Brian Osman16c81a12017-12-20 11:58:34 -05001215
Florin Malitacefc1b92018-02-19 21:43:47 -05001216 const auto slidePt = this->mapEvent(x, y);
1217 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1218 fWindow->inval();
1219 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001220 }
1221
1222 switch (state) {
1223 case Window::kUp_InputState: {
1224 fGesture.touchEnd(nullptr);
1225 break;
1226 }
1227 case Window::kDown_InputState: {
1228 fGesture.touchBegin(nullptr, x, y);
1229 break;
1230 }
1231 case Window::kMove_InputState: {
1232 fGesture.touchMoved(nullptr, x, y);
1233 break;
1234 }
1235 }
1236 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1237
1238 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1239 fWindow->inval();
1240 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001241 return true;
1242}
1243
Brian Osmana109e392017-02-24 09:49:14 -05001244static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1245 const ImVec2& pos, const ImVec2& size) {
1246 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1247 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1248
1249 // Invisible 10x10 button
1250 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1251 ImGui::InvisibleButton(label, ImVec2(10, 10));
1252
1253 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1254 ImGuiIO& io = ImGui::GetIO();
1255 // Normalized mouse position, relative to our gamut box
1256 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1257 // Clamp to edge of box, convert back to primary scale
1258 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1259 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1260 }
1261
1262 if (ImGui::IsItemHovered()) {
1263 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1264 }
1265
1266 // Return screen coordinates for the caller. We could just return center here, but we'd have
1267 // one frame of lag during drag.
1268 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1269}
1270
1271static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1272 ImDrawList* drawList = ImGui::GetWindowDrawList();
1273
1274 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1275 // width, and scale the height to maintain aspect ratio.
1276 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1277 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1278 ImVec2 pos = ImGui::GetCursorScreenPos();
1279
1280 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1281 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1282 // Magic numbers are pixel locations of the origin and upper-right corner.
1283 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1284 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001285
1286 // Primary markers
1287 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1288 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1289 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1290 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1291
1292 // Gamut triangle
1293 drawList->AddCircle(r, 5.0f, 0xFF000040);
1294 drawList->AddCircle(g, 5.0f, 0xFF004000);
1295 drawList->AddCircle(b, 5.0f, 0xFF400000);
1296 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1297 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1298
1299 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001300 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1301}
1302
1303static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1304 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1305 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1306 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1307
1308 // Invisible 10x10 button
1309 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1310 ImGui::InvisibleButton(label, ImVec2(10, 10));
1311
1312 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1313 ImGuiIO& io = ImGui::GetIO();
1314 // Normalized mouse position, relative to our gamut box
1315 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1316 // Clamp to edge of box
1317 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1318 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1319 *dragging = true;
1320 }
1321
1322 // Return screen coordinates for the caller. We could just return center here, but we'd have
1323 // one frame of lag during drag.
1324 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1325}
1326
1327static bool ImGui_DragQuad(SkPoint* pts) {
1328 ImDrawList* drawList = ImGui::GetWindowDrawList();
1329
1330 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1331 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1332 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1333 ImVec2 pos = ImGui::GetCursorScreenPos();
1334
1335 // Background rectangle
1336 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1337
1338 // Corner markers
1339 bool dragging = false;
1340 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1341 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1342 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1343 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1344
1345 // Draw markers and quad
1346 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1347 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1348 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1349 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1350 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1351 drawList->AddLine(tr, br, 0xFFFFFFFF);
1352 drawList->AddLine(br, bl, 0xFFFFFFFF);
1353 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1354
1355 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1356 ImGui::Spacing();
1357
1358 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001359}
1360
Brian Osmand67e5182017-12-08 16:46:09 -05001361void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001362 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1363 if (fShowImGuiTestWindow) {
1364 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1365 }
1366
1367 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001368 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1369 // always visible, we can end up in a layout feedback loop.
1370 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001371 DisplayParams params = fWindow->getRequestedDisplayParams();
1372 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001373 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1374 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001375 if (ImGui::CollapsingHeader("Backend")) {
1376 int newBackend = static_cast<int>(fBackendType);
1377 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1378 ImGui::SameLine();
1379 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001380#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1381 ImGui::SameLine();
1382 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1383#endif
Brian Osman621491e2017-02-28 15:45:01 -05001384#if defined(SK_VULKAN)
1385 ImGui::SameLine();
1386 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1387#endif
1388 if (newBackend != fBackendType) {
1389 fDeferredActions.push_back([=]() {
1390 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1391 });
1392 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001393
Brian Salomon99a33902017-03-07 15:16:34 -05001394 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001395 bool* wire = &params.fGrContextOptions.fWireframeMode;
1396 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1397 paramsChanged = true;
1398 }
Brian Salomon99a33902017-03-07 15:16:34 -05001399
Brian Osman28b12522017-03-08 17:10:24 -05001400 if (ctx) {
1401 int sampleCount = fWindow->sampleCount();
1402 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001403 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001404 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1405 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1406 ImGui::RadioButton("16", &sampleCount, 16);
1407
1408 if (sampleCount != params.fMSAASampleCount) {
1409 params.fMSAASampleCount = sampleCount;
1410 paramsChanged = true;
1411 }
1412 }
1413
Ben Wagner37c54032018-04-13 14:30:23 -04001414 int pixelGeometryIdx = 0;
1415 if (fPixelGeometryOverrides) {
1416 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1417 }
1418 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1419 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1420 {
1421 uint32_t flags = params.fSurfaceProps.flags();
1422 if (pixelGeometryIdx == 0) {
1423 fPixelGeometryOverrides = false;
1424 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1425 } else {
1426 fPixelGeometryOverrides = true;
1427 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1428 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1429 }
1430 paramsChanged = true;
1431 }
1432
1433 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1434 if (ImGui::Checkbox("DFT", &useDFT)) {
1435 uint32_t flags = params.fSurfaceProps.flags();
1436 if (useDFT) {
1437 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1438 } else {
1439 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1440 }
1441 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1442 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1443 paramsChanged = true;
1444 }
1445
Brian Osman8a9de3d2017-03-01 14:59:05 -05001446 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001447 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001448 auto prButton = [&](GpuPathRenderers x) {
1449 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001450 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1451 params.fGrContextOptions.fGpuPathRenderers = x;
1452 paramsChanged = true;
1453 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001454 }
1455 };
1456
1457 if (!ctx) {
1458 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001459 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001460 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001461 prButton(GpuPathRenderers::kAll);
1462 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1463 prButton(GpuPathRenderers::kStencilAndCover);
1464 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001465 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001466 prButton(GpuPathRenderers::kNone);
1467 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001468 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001469 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001470 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1471 prButton(GpuPathRenderers::kCoverageCounting);
1472 }
Jim Van Verth83010462017-03-16 08:45:39 -04001473 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001474 prButton(GpuPathRenderers::kTessellating);
1475 prButton(GpuPathRenderers::kNone);
1476 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001477 ImGui::TreePop();
1478 }
Brian Osman621491e2017-02-28 15:45:01 -05001479 }
1480
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001481 if (ImGui::CollapsingHeader("Transform")) {
1482 float zoom = fZoomLevel;
1483 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1484 fZoomLevel = zoom;
1485 this->preTouchMatrixChanged();
1486 paramsChanged = true;
1487 }
1488 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001489 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001490 fRotation = deg;
1491 this->preTouchMatrixChanged();
1492 paramsChanged = true;
1493 }
Brian Osman805a7272018-05-02 15:40:20 -04001494 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1495 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1496 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001497 this->preTouchMatrixChanged();
Brian Osman805a7272018-05-02 15:40:20 -04001498 this->updateTitle();
Brian Osman9bb47cf2018-04-26 15:55:00 -04001499 }
1500 if (ImGui_DragQuad(fPerspectivePoints)) {
1501 this->preTouchMatrixChanged();
1502 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001503 }
1504
Ben Wagnera580fb32018-04-17 11:16:32 -04001505 if (ImGui::CollapsingHeader("Paint")) {
1506 int hintingIdx = 0;
1507 if (fPaintOverrides.fHinting) {
1508 hintingIdx = fPaint.getHinting() + 1;
1509 }
1510 if (ImGui::Combo("Hinting", &hintingIdx,
1511 "Default\0None\0Slight\0Normal\0Full\0\0"))
1512 {
1513 if (hintingIdx == 0) {
1514 fPaintOverrides.fHinting = false;
1515 fPaint.setHinting(SkPaint::kNo_Hinting);
1516 } else {
1517 fPaintOverrides.fHinting = true;
1518 SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1);
1519 fPaint.setHinting(hinting);
1520 }
1521 paramsChanged = true;
1522 }
1523
1524 int aliasIdx = 0;
1525 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1526 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1527 }
1528 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1529 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1530 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1531 {
1532 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1533 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1534 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1535 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1536 if (aliasIdx == 0) {
1537 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1538 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1539 } else {
1540 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1541 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1542 fPaint.setAntiAlias(aliasIdx > 1);
1543 switch (fPaintOverrides.fAntiAlias) {
1544 case SkPaintFields::AntiAliasState::Alias:
1545 break;
1546 case SkPaintFields::AntiAliasState::Normal:
1547 break;
1548 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1549 gSkUseAnalyticAA = true;
1550 gSkForceAnalyticAA = false;
1551 gSkUseDeltaAA = gSkForceDeltaAA = false;
1552 break;
1553 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1554 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1555 gSkUseDeltaAA = gSkForceDeltaAA = false;
1556 break;
1557 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1558 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1559 gSkUseDeltaAA = true;
1560 gSkForceDeltaAA = false;
1561 break;
1562 case SkPaintFields::AntiAliasState::DeltaAAForced:
1563 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1564 gSkUseDeltaAA = gSkForceDeltaAA = true;
1565 break;
1566 }
1567 }
1568 paramsChanged = true;
1569 }
1570
1571 int subpixelAAIdx = 0;
1572 if (fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag) {
1573 subpixelAAIdx = fPaint.isLCDRenderText() ? 2 : 1;
1574 }
1575 if (ImGui::Combo("Subpixel Anti-Alias", &subpixelAAIdx,
1576 "Default\0lcd\0LCD\0\0"))
1577 {
1578 if (subpixelAAIdx == 0) {
1579 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
1580 } else {
1581 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
1582 fPaint.setLCDRenderText(subpixelAAIdx == 2);
1583 }
1584 paramsChanged = true;
1585 }
1586
1587 int subpixelPositionIdx = 0;
1588 if (fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag) {
1589 subpixelPositionIdx = fPaint.isSubpixelText() ? 2 : 1;
1590 }
1591 if (ImGui::Combo("Subpixel Position Glyphs", &subpixelPositionIdx,
1592 "Default\0Pixel Glyphs\0Subpixel Glyphs\0\0"))
1593 {
1594 if (subpixelPositionIdx == 0) {
1595 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
1596 } else {
1597 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
1598 fPaint.setSubpixelText(subpixelPositionIdx == 2);
1599 }
1600 paramsChanged = true;
1601 }
1602 }
1603
Ben Wagner7a3c6742018-04-23 10:01:07 -04001604 if (fShowSlidePicker) {
1605 ImGui::SetNextTreeNodeOpen(true);
1606 }
Brian Osman79086b92017-02-10 13:36:16 -05001607 if (ImGui::CollapsingHeader("Slide")) {
1608 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001609 static ImVector<const char*> filteredSlideNames;
1610 static ImVector<int> filteredSlideIndices;
1611
Brian Osmanfce09c52017-11-14 15:32:20 -05001612 if (fShowSlidePicker) {
1613 ImGui::SetKeyboardFocusHere();
1614 fShowSlidePicker = false;
1615 }
1616
Brian Osman79086b92017-02-10 13:36:16 -05001617 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001618 filteredSlideNames.clear();
1619 filteredSlideIndices.clear();
1620 int filteredIndex = 0;
1621 for (int i = 0; i < fSlides.count(); ++i) {
1622 const char* slideName = fSlides[i]->getName().c_str();
1623 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1624 if (i == fCurrentSlide) {
1625 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001626 }
Brian Osmanf479e422017-11-08 13:11:36 -05001627 filteredSlideNames.push_back(slideName);
1628 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001629 }
Brian Osman79086b92017-02-10 13:36:16 -05001630 }
Brian Osmanf479e422017-11-08 13:11:36 -05001631
Brian Osmanf479e422017-11-08 13:11:36 -05001632 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1633 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001634 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001635 }
1636 }
Brian Osmana109e392017-02-24 09:49:14 -05001637
1638 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001639 ColorMode newMode = fColorMode;
1640 auto cmButton = [&](ColorMode mode, const char* label) {
1641 if (ImGui::RadioButton(label, mode == fColorMode)) {
1642 newMode = mode;
1643 }
1644 };
1645
1646 cmButton(ColorMode::kLegacy, "Legacy 8888");
1647 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1648 "Color Managed 8888 (Nonlinear blending)");
1649 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1650 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1651
1652 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001653 // It isn't safe to switch color mode now (in the middle of painting). We might
1654 // tear down the back-end, etc... Defer this change until the next onIdle.
1655 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001656 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001657 });
Brian Osmana109e392017-02-24 09:49:14 -05001658 }
1659
1660 // Pick from common gamuts:
1661 int primariesIdx = 4; // Default: Custom
1662 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1663 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1664 primariesIdx = i;
1665 break;
1666 }
1667 }
1668
Brian Osmanfdab5762017-11-09 10:27:55 -05001669 // When we're in xform canvas mode, we can alter the transfer function, too
1670 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1671 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1672 }
1673
Brian Osmana109e392017-02-24 09:49:14 -05001674 if (ImGui::Combo("Primaries", &primariesIdx,
1675 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1676 if (primariesIdx >= 0 && primariesIdx <= 3) {
1677 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1678 }
1679 }
1680
1681 // Allow direct editing of gamut
1682 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1683 }
Brian Osman79086b92017-02-10 13:36:16 -05001684 }
Brian Salomon99a33902017-03-07 15:16:34 -05001685 if (paramsChanged) {
1686 fDeferredActions.push_back([=]() {
1687 fWindow->setRequestedDisplayParams(params);
1688 fWindow->inval();
1689 this->updateTitle();
1690 });
1691 }
Brian Osman79086b92017-02-10 13:36:16 -05001692 ImGui::End();
1693 }
1694
Brian Osmanf6877092017-02-13 09:39:57 -05001695 if (fShowZoomWindow && fLastImage) {
1696 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001697 static int zoomFactor = 8;
1698 if (ImGui::Button("<<")) {
1699 zoomFactor = SkTMax(zoomFactor / 2, 4);
1700 }
1701 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1702 if (ImGui::Button(">>")) {
1703 zoomFactor = SkTMin(zoomFactor * 2, 32);
1704 }
Brian Osmanf6877092017-02-13 09:39:57 -05001705
Brian Osmanf6877092017-02-13 09:39:57 -05001706 ImVec2 mousePos = ImGui::GetMousePos();
1707 ImVec2 avail = ImGui::GetContentRegionAvail();
1708
Brian Osmanead517d2017-11-13 15:36:36 -05001709 uint32_t pixel = 0;
1710 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1711 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1712 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001713 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1714 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1715 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001716 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1717 }
1718
Brian Osmand67e5182017-12-08 16:46:09 -05001719 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001720 // Translate so the region of the image that's under the mouse cursor is centered
1721 // in the zoom canvas:
1722 c->scale(zoomFactor, zoomFactor);
1723 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1724 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1725 c->drawImage(this->fLastImage, 0, 0);
1726
1727 SkPaint outline;
1728 outline.setStyle(SkPaint::kStroke_Style);
1729 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1730 });
Brian Osmanf6877092017-02-13 09:39:57 -05001731 }
1732
1733 ImGui::End();
1734 }
Brian Osman79086b92017-02-10 13:36:16 -05001735}
1736
liyuqian2edb0f42016-07-06 14:11:32 -07001737void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001738 for (int i = 0; i < fDeferredActions.count(); ++i) {
1739 fDeferredActions[i]();
1740 }
1741 fDeferredActions.reset();
1742
Brian Osman56a24812017-12-19 11:15:16 -05001743 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001744 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001745 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001746 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001747
Brian Osman79086b92017-02-10 13:36:16 -05001748 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001749 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001750 fWindow->inval();
1751 }
jvanverth9f372462016-04-06 06:08:59 -07001752}
liyuqiane5a6cd92016-05-27 08:52:52 -07001753
1754void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001755 if (!fWindow) {
1756 return;
1757 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001758 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001759 return; // Surface hasn't been created yet.
1760 }
1761
liyuqianb73c24b2016-06-03 08:47:23 -07001762 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001763 Json::Value slideState(Json::objectValue);
1764 slideState[kName] = kSlideStateName;
1765 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001766 if (fAllSlideNames.size() == 0) {
1767 for(auto slide : fSlides) {
1768 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1769 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001770 }
liyuqian1f508fd2016-06-07 06:57:40 -07001771 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001772
liyuqianb73c24b2016-06-03 08:47:23 -07001773 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001774 Json::Value backendState(Json::objectValue);
1775 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001776 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001777 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001778 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001779 backendState[kOptions].append(Json::Value(str));
1780 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001781
csmartdalton578f0642017-02-24 16:04:47 -07001782 // MSAA state
1783 Json::Value msaaState(Json::objectValue);
1784 msaaState[kName] = kMSAAStateName;
1785 msaaState[kValue] = fWindow->sampleCount();
1786 msaaState[kOptions] = Json::Value(Json::arrayValue);
1787 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1788 msaaState[kOptions].append(Json::Value(0));
1789 } else {
1790 for (int msaa : {0, 4, 8, 16}) {
1791 msaaState[kOptions].append(Json::Value(msaa));
1792 }
1793 }
1794
csmartdalton61cd31a2017-02-27 17:00:53 -07001795 // Path renderer state
1796 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1797 Json::Value prState(Json::objectValue);
1798 prState[kName] = kPathRendererStateName;
1799 prState[kValue] = gPathRendererNames[pr];
1800 prState[kOptions] = Json::Value(Json::arrayValue);
1801 const GrContext* ctx = fWindow->getGrContext();
1802 if (!ctx) {
1803 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001804 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001805 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001806 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1807 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1808 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1809 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001810 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001811 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1812 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001813 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001814 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001815 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1816 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1817 }
Jim Van Verth83010462017-03-16 08:45:39 -04001818 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001819 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001820 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1821 }
1822
liyuqianb73c24b2016-06-03 08:47:23 -07001823 // Softkey state
1824 Json::Value softkeyState(Json::objectValue);
1825 softkeyState[kName] = kSoftkeyStateName;
1826 softkeyState[kValue] = kSoftkeyHint;
1827 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1828 softkeyState[kOptions].append(kSoftkeyHint);
1829 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1830 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1831 }
1832
liyuqiane5a6cd92016-05-27 08:52:52 -07001833 Json::Value state(Json::arrayValue);
1834 state.append(slideState);
1835 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001836 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001837 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001838 state.append(softkeyState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001839
Brian Osmaneff04b52017-11-21 13:18:02 -05001840 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001841}
1842
1843void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001844 // For those who will add more features to handle the state change in this function:
1845 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1846 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1847 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001848 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001849 for (int i = 0; i < fSlides.count(); ++i) {
1850 if (fSlides[i]->getName().equals(stateValue)) {
1851 this->setCurrentSlide(i);
1852 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001853 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001854 }
Florin Malitaab99c342018-01-16 16:23:03 -05001855
1856 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001857 } else if (stateName.equals(kBackendStateName)) {
1858 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1859 if (stateValue.equals(kBackendTypeStrings[i])) {
1860 if (fBackendType != i) {
1861 fBackendType = (sk_app::Window::BackendType)i;
1862 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001863 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001864 }
1865 break;
1866 }
1867 }
csmartdalton578f0642017-02-24 16:04:47 -07001868 } else if (stateName.equals(kMSAAStateName)) {
1869 DisplayParams params = fWindow->getRequestedDisplayParams();
1870 int sampleCount = atoi(stateValue.c_str());
1871 if (sampleCount != params.fMSAASampleCount) {
1872 params.fMSAASampleCount = sampleCount;
1873 fWindow->setRequestedDisplayParams(params);
1874 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001875 this->updateTitle();
1876 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001877 }
1878 } else if (stateName.equals(kPathRendererStateName)) {
1879 DisplayParams params = fWindow->getRequestedDisplayParams();
1880 for (const auto& pair : gPathRendererNames) {
1881 if (pair.second == stateValue.c_str()) {
1882 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1883 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1884 fWindow->setRequestedDisplayParams(params);
1885 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001886 this->updateTitle();
1887 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001888 }
1889 break;
1890 }
csmartdalton578f0642017-02-24 16:04:47 -07001891 }
liyuqianb73c24b2016-06-03 08:47:23 -07001892 } else if (stateName.equals(kSoftkeyStateName)) {
1893 if (!stateValue.equals(kSoftkeyHint)) {
1894 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001895 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001896 }
liyuqian2edb0f42016-07-06 14:11:32 -07001897 } else if (stateName.equals(kRefreshStateName)) {
1898 // This state is actually NOT in the UI state.
1899 // We use this to allow Android to quickly set bool fRefresh.
1900 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001901 } else {
1902 SkDebugf("Unknown stateName: %s", stateName.c_str());
1903 }
1904}
Brian Osman79086b92017-02-10 13:36:16 -05001905
1906bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001907 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001908}
1909
1910bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001911 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001912 fWindow->inval();
1913 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001914 } else {
1915 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001916 }
Brian Osman79086b92017-02-10 13:36:16 -05001917}