blob: 7ec001bac04092804b4fc8513e04e69d9b1dc3a1 [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 Osman2dd96932016-10-18 15:33:53 -040018#include "SkCommandLineFlags.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040019#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070020#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040021#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050022#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040023#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050024#include "SkImagePriv.h"
Florin Malitab632df72018-06-18 21:23:06 -040025#include "SkJSONWriter.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"
Hal Canaryc640d0d2018-06-13 09:59:02 -040036#include "SkTo.h"
Hal Canary8a001442018-09-19 11:31:27 -040037#include "SlideDir.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"
csmartdalton578f0642017-02-24 16:04:47 -070041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <stdlib.h>
43#include <map>
44
Hal Canary8a001442018-09-19 11:31:27 -040045#include "imgui.h"
Florin Malita3b526b02018-05-25 12:43:51 -040046
Florin Malita87ccf332018-05-04 12:23:24 -040047#if defined(SK_ENABLE_SKOTTIE)
48 #include "SkottieSlide.h"
49#endif
50
Ruiqi Maof5101492018-06-29 14:32:21 -040051#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
52 #include "NIMASlide.h"
53#endif
54
jvanverth34524262016-05-04 13:49:13 -070055using namespace sk_app;
56
csmartdalton61cd31a2017-02-27 17:00:53 -070057static std::map<GpuPathRenderers, std::string> gPathRendererNames;
58
jvanverth9f372462016-04-06 06:08:59 -070059Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070060 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070061}
62
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060063static DEFINE_string(slide, "", "Start on this sample.");
64static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050065
bsalomon6c471f72016-07-26 12:56:32 -070066#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070067# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070068#else
69# define BACKENDS_STR "\"sw\" and \"gl\""
70#endif
71
Brian Osman2dd96932016-10-18 15:33:53 -040072static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070073
Brian Salomonbdecacf2018-02-02 20:32:49 -050074static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070075
Chris Dalton2d18f412018-02-20 13:23:32 -070076DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
77
Brian Osman53136aa2017-07-20 15:43:35 -040078DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050079
Florin Malita38792ce2018-05-08 10:36:18 -040080DEFINE_string2(file, f, "", "Open a single file for viewing.");
81
Brian Salomon194db172017-08-17 14:37:06 -040082const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070083 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040084#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
85 "ANGLE",
86#endif
jvanverth063ece72016-06-17 09:29:14 -070087#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070088 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070089#endif
csmartdalton578f0642017-02-24 16:04:47 -070090 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070091};
92
bsalomon6c471f72016-07-26 12:56:32 -070093static sk_app::Window::BackendType get_backend_type(const char* str) {
94#ifdef SK_VULKAN
95 if (0 == strcmp(str, "vk")) {
96 return sk_app::Window::kVulkan_BackendType;
97 } else
98#endif
Brian Salomon194db172017-08-17 14:37:06 -040099#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
100 if (0 == strcmp(str, "angle")) {
101 return sk_app::Window::kANGLE_BackendType;
102 } else
103#endif
bsalomon6c471f72016-07-26 12:56:32 -0700104 if (0 == strcmp(str, "gl")) {
105 return sk_app::Window::kNativeGL_BackendType;
106 } else if (0 == strcmp(str, "sw")) {
107 return sk_app::Window::kRaster_BackendType;
108 } else {
109 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
110 return sk_app::Window::kRaster_BackendType;
111 }
112}
113
Brian Osmana109e392017-02-24 09:49:14 -0500114static SkColorSpacePrimaries gSrgbPrimaries = {
115 0.64f, 0.33f,
116 0.30f, 0.60f,
117 0.15f, 0.06f,
118 0.3127f, 0.3290f };
119
120static SkColorSpacePrimaries gAdobePrimaries = {
121 0.64f, 0.33f,
122 0.21f, 0.71f,
123 0.15f, 0.06f,
124 0.3127f, 0.3290f };
125
126static SkColorSpacePrimaries gP3Primaries = {
127 0.680f, 0.320f,
128 0.265f, 0.690f,
129 0.150f, 0.060f,
130 0.3127f, 0.3290f };
131
132static SkColorSpacePrimaries gRec2020Primaries = {
133 0.708f, 0.292f,
134 0.170f, 0.797f,
135 0.131f, 0.046f,
136 0.3127f, 0.3290f };
137
138struct NamedPrimaries {
139 const char* fName;
140 SkColorSpacePrimaries* fPrimaries;
141} gNamedPrimaries[] = {
142 { "sRGB", &gSrgbPrimaries },
143 { "AdobeRGB", &gAdobePrimaries },
144 { "P3", &gP3Primaries },
145 { "Rec. 2020", &gRec2020Primaries },
146};
147
148static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
149 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
150}
151
Brian Osman70d2f432017-11-08 09:54:10 -0500152static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
153 // In raster mode, we still use GL for the window.
154 // This lets us render the GUI faster (and correct).
155 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
156}
157
liyuqiane5a6cd92016-05-27 08:52:52 -0700158const char* kName = "name";
159const char* kValue = "value";
160const char* kOptions = "options";
161const char* kSlideStateName = "Slide";
162const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700163const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700164const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700165const char* kSoftkeyStateName = "Softkey";
166const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700167const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700168const char* kON = "ON";
169const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700170const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700171
jvanverth34524262016-05-04 13:49:13 -0700172Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500173 : fCurrentSlide(-1)
174 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500175 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500176 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500177 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500178 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500179 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400180 , fZoomWindowFixed(false)
181 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500182 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400183 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700184 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500185 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500186 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500187 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osmana9549ab2019-01-03 12:43:05 -0500188 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700189 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400190 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400191 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400192 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500193 , fTiled(false)
194 , fDrawTileBoundaries(false)
195 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400196 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700197{
Greg Daniel285db442016-10-14 09:12:53 -0400198 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700199
Brian Osmanf09e35e2017-12-15 14:48:09 -0500200 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500201 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500202 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
203 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
204 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
205 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700206
jvanverth2bb3b6d2016-04-08 07:24:09 -0700207 SkDebugf("Command line arguments: ");
208 for (int i = 1; i < argc; ++i) {
209 SkDebugf("%s ", argv[i]);
210 }
211 SkDebugf("\n");
212
213 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500214#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400215 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500216#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700217
Ben Wagner483c7722018-02-20 17:06:07 -0500218 if (!FLAGS_nativeFonts) {
219 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
220 }
221
Brian Osmanbc8150f2017-07-24 11:38:01 -0400222 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400223 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400224
bsalomon6c471f72016-07-26 12:56:32 -0700225 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700226 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700227
csmartdalton578f0642017-02-24 16:04:47 -0700228 DisplayParams displayParams;
229 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700230 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700231 fWindow->setRequestedDisplayParams(displayParams);
232
Brian Osman56a24812017-12-19 11:15:16 -0500233 // Configure timers
234 fStatsLayer.setActive(false);
235 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
236 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
237 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
238
jvanverth9f372462016-04-06 06:08:59 -0700239 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700240 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500241 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500242 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500243 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700244
brianosman622c8d52016-05-10 06:50:49 -0700245 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500246 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
247 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
248 fWindow->inval();
249 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500250 // Command to jump directly to the slide picker and give it focus
251 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
252 this->fShowImGuiDebugWindow = true;
253 this->fShowSlidePicker = true;
254 fWindow->inval();
255 });
256 // Alias that to Backspace, to match SampleApp
257 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
258 this->fShowImGuiDebugWindow = true;
259 this->fShowSlidePicker = true;
260 fWindow->inval();
261 });
Brian Osman79086b92017-02-10 13:36:16 -0500262 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
263 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
264 fWindow->inval();
265 });
Brian Osmanf6877092017-02-13 09:39:57 -0500266 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
267 this->fShowZoomWindow = !this->fShowZoomWindow;
268 fWindow->inval();
269 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400270 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
271 this->fZoomWindowFixed = !this->fZoomWindowFixed;
272 fWindow->inval();
273 });
brianosman622c8d52016-05-10 06:50:49 -0700274 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500275 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700276 fWindow->inval();
277 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400278 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500279 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400280 this->updateTitle();
281 fWindow->inval();
282 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500283 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500284 switch (fColorMode) {
285 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500286 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500287 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500288 case ColorMode::kColorManaged8888:
289 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500290 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500291 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500292 this->setColorMode(ColorMode::kLegacy);
293 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500294 }
brianosman622c8d52016-05-10 06:50:49 -0700295 });
296 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500297 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700298 });
299 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500300 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700301 });
302 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
303 this->changeZoomLevel(1.f / 32.f);
304 fWindow->inval();
305 });
306 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
307 this->changeZoomLevel(-1.f / 32.f);
308 fWindow->inval();
309 });
jvanverthaf236b52016-05-20 06:01:06 -0700310 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400311 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
312 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500313 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400314#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
315 if (newBackend == sk_app::Window::kVulkan_BackendType) {
316 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
317 sk_app::Window::kBackendTypeCount);
318 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
319 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500320 }
321#endif
Brian Osman621491e2017-02-28 15:45:01 -0500322 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700323 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500324 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
325 fSaveToSKP = true;
326 fWindow->inval();
327 });
Ben Wagner37c54032018-04-13 14:30:23 -0400328 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
329 DisplayParams params = fWindow->getRequestedDisplayParams();
330 uint32_t flags = params.fSurfaceProps.flags();
331 if (!fPixelGeometryOverrides) {
332 fPixelGeometryOverrides = true;
333 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
334 } else {
335 switch (params.fSurfaceProps.pixelGeometry()) {
336 case kUnknown_SkPixelGeometry:
337 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
338 break;
339 case kRGB_H_SkPixelGeometry:
340 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
341 break;
342 case kBGR_H_SkPixelGeometry:
343 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
344 break;
345 case kRGB_V_SkPixelGeometry:
346 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
347 break;
348 case kBGR_V_SkPixelGeometry:
349 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
350 fPixelGeometryOverrides = false;
351 break;
352 }
353 }
354 fWindow->setRequestedDisplayParams(params);
355 this->updateTitle();
356 fWindow->inval();
357 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500358 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500359 if (!fFontOverrides.fHinting) {
360 fFontOverrides.fHinting = true;
361 fFont.setHinting(kNo_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500362 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500363 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500364 case kNo_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500365 fFont.setHinting(kSlight_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500366 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500367 case kSlight_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500368 fFont.setHinting(kNormal_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500369 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500370 case kNormal_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500371 fFont.setHinting(kFull_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500372 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500373 case kFull_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500374 fFont.setHinting(kNo_SkFontHinting);
375 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500376 break;
377 }
378 }
379 this->updateTitle();
380 fWindow->inval();
381 });
382 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
383 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
384 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
385 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
386 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500387 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
388 gSkUseDeltaAA = gSkForceDeltaAA = false;
389 } else {
390 fPaint.setAntiAlias(true);
391 switch (fPaintOverrides.fAntiAlias) {
392 case SkPaintFields::AntiAliasState::Alias:
393 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400394 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
395 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500396 break;
397 case SkPaintFields::AntiAliasState::Normal:
398 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500399 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400400 gSkForceAnalyticAA = false;
401 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500402 break;
403 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
404 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400405 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
406 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500407 break;
408 case SkPaintFields::AntiAliasState::AnalyticAAForced:
409 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
410 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
411 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400412 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500413 break;
414 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
415 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400416 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
417 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500418 break;
419 case SkPaintFields::AntiAliasState::DeltaAAForced:
420 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
421 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
422 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
423 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
424 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
425 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
426 break;
427 }
428 }
429 this->updateTitle();
430 fWindow->inval();
431 });
Ben Wagner37c54032018-04-13 14:30:23 -0400432 fCommands.addCommand('D', "Modes", "DFT", [this]() {
433 DisplayParams params = fWindow->getRequestedDisplayParams();
434 uint32_t flags = params.fSurfaceProps.flags();
435 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
436 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
437 fWindow->setRequestedDisplayParams(params);
438 this->updateTitle();
439 fWindow->inval();
440 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500441 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
442 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
443 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
444 fPaint.setLCDRenderText(false);
445 } else {
446 if (!fPaint.isLCDRenderText()) {
447 fPaint.setLCDRenderText(true);
448 } else {
449 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
450 }
451 }
452 this->updateTitle();
453 fWindow->inval();
454 });
455 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
456 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
457 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
458 fPaint.setSubpixelText(false);
459 } else {
460 if (!fPaint.isSubpixelText()) {
461 fPaint.setSubpixelText(true);
462 } else {
463 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
464 }
465 }
466 this->updateTitle();
467 fWindow->inval();
468 });
Brian Osman805a7272018-05-02 15:40:20 -0400469 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
470 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
471 : kPerspective_Real;
472 this->updateTitle();
473 fWindow->inval();
474 });
475 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
476 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
477 : kPerspective_Off;
478 this->updateTitle();
479 fWindow->inval();
480 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400481 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
482 fZoomUI = !fZoomUI;
483 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
484 fWindow->inval();
485 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500486
jvanverth2bb3b6d2016-04-08 07:24:09 -0700487 // set up slides
488 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500489 if (FLAGS_list) {
490 this->listNames();
491 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700492
Brian Osman9bb47cf2018-04-26 15:55:00 -0400493 fPerspectivePoints[0].set(0, 0);
494 fPerspectivePoints[1].set(1, 0);
495 fPerspectivePoints[2].set(0, 1);
496 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700497 fAnimTimer.run();
498
Hal Canaryc465d132017-12-08 10:21:31 -0500499 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500500 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400501 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500502 }
503 fImGuiGamutPaint.setColor(SK_ColorWHITE);
504 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
505
Brian Osman70d2f432017-11-08 09:54:10 -0500506 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500507 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700508}
509
jvanverth34524262016-05-04 13:49:13 -0700510void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400511 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
512 static const struct {
513 const char* fExtension;
514 const char* fDirName;
515 const SkCommandLineFlags::StringArray& fFlags;
516 const SlideFactory fFactory;
517 } gExternalSlidesInfo[] = {
518 { ".skp", "skp-dir", FLAGS_skps,
519 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
520 return sk_make_sp<SKPSlide>(name, path);}
521 },
522 { ".jpg", "jpg-dir", FLAGS_jpgs,
523 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
524 return sk_make_sp<ImageSlide>(name, path);}
525 },
Florin Malita87ccf332018-05-04 12:23:24 -0400526#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400527 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400528 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
529 return sk_make_sp<SkottieSlide>(name, path);}
530 },
Florin Malita87ccf332018-05-04 12:23:24 -0400531#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400532#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400533 { ".svg", "svg-dir", FLAGS_svgs,
534 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
535 return sk_make_sp<SvgSlide>(name, path);}
536 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400537#endif
Ruiqi Maof5101492018-06-29 14:32:21 -0400538#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
539 { ".nima", "nima-dir", FLAGS_nimas,
540 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
541 return sk_make_sp<NIMASlide>(name, path);}
542 },
543#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400544 };
jvanverthc265a922016-04-08 12:51:45 -0700545
Brian Salomon343553a2018-09-05 15:41:23 -0400546 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700547
Florin Malita0ffa3222018-04-05 14:34:45 -0400548 const auto addSlide = [&](const SkString& name,
549 const SkString& path,
550 const SlideFactory& fact) {
551 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
552 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700553 }
liyuqian6f163d22016-06-13 12:26:45 -0700554
Florin Malita0ffa3222018-04-05 14:34:45 -0400555 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500556 dirSlides.push_back(slide);
557 fSlides.push_back(std::move(slide));
558 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400559 };
Florin Malita76a076b2018-02-15 18:40:48 -0500560
Florin Malita38792ce2018-05-08 10:36:18 -0400561 if (!FLAGS_file.isEmpty()) {
562 // single file mode
563 const SkString file(FLAGS_file[0]);
564
565 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
566 for (const auto& sinfo : gExternalSlidesInfo) {
567 if (file.endsWith(sinfo.fExtension)) {
568 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
569 return;
570 }
571 }
572
573 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
574 } else {
575 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
576 }
577
578 return;
579 }
580
581 // Bisect slide.
582 if (!FLAGS_bisect.isEmpty()) {
583 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
584 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
585 if (FLAGS_bisect.count() >= 2) {
586 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
587 bisect->onChar(*ch);
588 }
589 }
590 fSlides.push_back(std::move(bisect));
591 }
592 }
593
594 // GMs
595 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400596 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
597 std::unique_ptr<skiagm::GM> gm(gmFactory(nullptr));
Florin Malita38792ce2018-05-08 10:36:18 -0400598 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
599 sk_sp<Slide> slide(new GMSlide(gm.release()));
600 fSlides.push_back(std::move(slide));
601 }
Florin Malita38792ce2018-05-08 10:36:18 -0400602 }
603 // reverse gms
604 int numGMs = fSlides.count() - firstGM;
605 for (int i = 0; i < numGMs/2; ++i) {
606 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
607 }
608
609 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400610 for (const SampleFactory factory : SampleRegistry::Range()) {
611 sk_sp<Slide> slide(new SampleSlide(factory));
Florin Malita38792ce2018-05-08 10:36:18 -0400612 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
613 fSlides.push_back(slide);
614 }
Florin Malita38792ce2018-05-08 10:36:18 -0400615 }
616
Florin Malita0ffa3222018-04-05 14:34:45 -0400617 for (const auto& info : gExternalSlidesInfo) {
618 for (const auto& flag : info.fFlags) {
619 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
620 // single file
621 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
622 } else {
623 // directory
624 SkOSFile::Iter it(flag.c_str(), info.fExtension);
625 SkString name;
626 while (it.next(&name)) {
627 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
628 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400629 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400630 if (!dirSlides.empty()) {
631 fSlides.push_back(
632 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
633 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500634 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400635 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400636 }
637 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700638}
639
640
jvanverth34524262016-05-04 13:49:13 -0700641Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700642 fWindow->detach();
643 delete fWindow;
644}
645
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500646struct SkPaintTitleUpdater {
647 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
648 void append(const char* s) {
649 if (fCount == 0) {
650 fTitle->append(" {");
651 } else {
652 fTitle->append(", ");
653 }
654 fTitle->append(s);
655 ++fCount;
656 }
657 void done() {
658 if (fCount > 0) {
659 fTitle->append("}");
660 }
661 }
662 SkString* fTitle;
663 int fCount;
664};
665
brianosman05de2162016-05-06 13:28:57 -0700666void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700667 if (!fWindow) {
668 return;
669 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500670 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700671 return; // Surface hasn't been created yet.
672 }
673
jvanverth34524262016-05-04 13:49:13 -0700674 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700675 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700676
Yuqian Li399b3c22017-08-03 11:08:15 -0400677 if (gSkUseDeltaAA) {
678 if (gSkForceDeltaAA) {
679 title.append(" <FDAA>");
680 } else {
681 title.append(" <DAA>");
682 }
683 } else if (gSkUseAnalyticAA) {
684 if (gSkForceAnalyticAA) {
685 title.append(" <FAAA>");
686 } else {
687 title.append(" <AAA>");
688 }
689 }
690
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500691 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner99a78dc2018-05-09 18:23:51 -0400692 auto paintFlag = [this, &paintTitle](SkPaint::Flags flag, bool (SkPaint::* isFlag)() const,
693 const char* on, const char* off)
694 {
695 if (fPaintOverrides.fFlags & flag) {
696 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500697 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400698 };
699
700 paintFlag(SkPaint::kAntiAlias_Flag, &SkPaint::isAntiAlias, "Antialias", "Alias");
701 paintFlag(SkPaint::kDither_Flag, &SkPaint::isDither, "DITHER", "No Dither");
702 paintFlag(SkPaint::kFakeBoldText_Flag, &SkPaint::isFakeBoldText, "Fake Bold", "No Fake Bold");
703 paintFlag(SkPaint::kLinearText_Flag, &SkPaint::isLinearText, "Linear Text", "Non-Linear Text");
704 paintFlag(SkPaint::kSubpixelText_Flag, &SkPaint::isSubpixelText, "Subpixel Text", "Pixel Text");
705 paintFlag(SkPaint::kLCDRenderText_Flag, &SkPaint::isLCDRenderText, "LCD", "lcd");
706 paintFlag(SkPaint::kEmbeddedBitmapText_Flag, &SkPaint::isEmbeddedBitmapText,
707 "Bitmap Text", "No Bitmap Text");
708 paintFlag(SkPaint::kAutoHinting_Flag, &SkPaint::isAutohinted,
709 "Force Autohint", "No Force Autohint");
Ben Wagner99a78dc2018-05-09 18:23:51 -0400710
Mike Reed3ae47332019-01-04 10:11:46 -0500711 if (fFontOverrides.fHinting) {
712 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500713 case kNo_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500714 paintTitle.append("No Hinting");
715 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500716 case kSlight_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500717 paintTitle.append("Slight Hinting");
718 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500719 case kNormal_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500720 paintTitle.append("Normal Hinting");
721 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500722 case kFull_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500723 paintTitle.append("Full Hinting");
724 break;
725 }
726 }
727 paintTitle.done();
728
Brian Osman92004802017-03-06 11:47:26 -0500729 switch (fColorMode) {
730 case ColorMode::kLegacy:
731 title.append(" Legacy 8888");
732 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500733 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500734 title.append(" ColorManaged 8888");
735 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500736 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500737 title.append(" ColorManaged F16");
738 break;
739 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500740
Brian Osman92004802017-03-06 11:47:26 -0500741 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500742 int curPrimaries = -1;
743 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
744 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
745 curPrimaries = i;
746 break;
747 }
748 }
Brian Osman03115dc2018-11-26 13:55:19 -0500749 title.appendf(" %s Gamma %f",
750 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osmana9549ab2019-01-03 12:43:05 -0500751 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700752 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500753
Ben Wagner37c54032018-04-13 14:30:23 -0400754 const DisplayParams& params = fWindow->getRequestedDisplayParams();
755 if (fPixelGeometryOverrides) {
756 switch (params.fSurfaceProps.pixelGeometry()) {
757 case kUnknown_SkPixelGeometry:
758 title.append( " Flat");
759 break;
760 case kRGB_H_SkPixelGeometry:
761 title.append( " RGB");
762 break;
763 case kBGR_H_SkPixelGeometry:
764 title.append( " BGR");
765 break;
766 case kRGB_V_SkPixelGeometry:
767 title.append( " RGBV");
768 break;
769 case kBGR_V_SkPixelGeometry:
770 title.append( " BGRV");
771 break;
772 }
773 }
774
775 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
776 title.append(" DFT");
777 }
778
csmartdalton578f0642017-02-24 16:04:47 -0700779 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700780 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500781 int msaa = fWindow->sampleCount();
782 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700783 title.appendf(" MSAA: %i", msaa);
784 }
785 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700786
787 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman1994f202018-07-10 11:41:22 -0400788 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700789 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
790 }
791
Brian Osman805a7272018-05-02 15:40:20 -0400792 if (kPerspective_Real == fPerspectiveMode) {
793 title.append(" Perpsective (Real)");
794 } else if (kPerspective_Fake == fPerspectiveMode) {
795 title.append(" Perspective (Fake)");
796 }
797
brianosman05de2162016-05-06 13:28:57 -0700798 fWindow->setTitle(title.c_str());
799}
800
Florin Malitaab99c342018-01-16 16:23:03 -0500801int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500802
803 if (!FLAGS_slide.isEmpty()) {
804 int count = fSlides.count();
805 for (int i = 0; i < count; i++) {
806 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500807 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500808 }
809 }
810
811 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
812 this->listNames();
813 }
814
Florin Malitaab99c342018-01-16 16:23:03 -0500815 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500816}
817
Florin Malitaab99c342018-01-16 16:23:03 -0500818void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500819 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500820 for (const auto& slide : fSlides) {
821 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500822 }
823}
824
Florin Malitaab99c342018-01-16 16:23:03 -0500825void Viewer::setCurrentSlide(int slide) {
826 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700827
Florin Malitaab99c342018-01-16 16:23:03 -0500828 if (slide == fCurrentSlide) {
829 return;
830 }
831
832 if (fCurrentSlide >= 0) {
833 fSlides[fCurrentSlide]->unload();
834 }
835
836 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
837 SkIntToScalar(fWindow->height()));
838 fCurrentSlide = slide;
839 this->setupCurrentSlide();
840}
841
842void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500843 if (fCurrentSlide >= 0) {
844 // prepare dimensions for image slides
845 fGesture.resetTouchState();
846 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700847
Jim Van Verth0848fb02018-01-22 13:39:30 -0500848 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
849 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
850 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400851
Jim Van Verth0848fb02018-01-22 13:39:30 -0500852 // Start with a matrix that scales the slide to the available screen space
853 if (fWindow->scaleContentToFit()) {
854 if (windowRect.width() > 0 && windowRect.height() > 0) {
855 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
856 }
liyuqiane46e4f02016-05-20 07:32:19 -0700857 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500858
859 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400860 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500861
862 this->updateTitle();
863 this->updateUIState();
864
865 fStatsLayer.resetMeasurements();
866
867 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700868 }
jvanverthc265a922016-04-08 12:51:45 -0700869}
870
871#define MAX_ZOOM_LEVEL 8
872#define MIN_ZOOM_LEVEL -8
873
jvanverth34524262016-05-04 13:49:13 -0700874void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700875 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400876 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400877 this->preTouchMatrixChanged();
878}
Yuqian Li755778c2018-03-28 16:23:31 -0400879
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400880void Viewer::preTouchMatrixChanged() {
881 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400882 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
883 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
884 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
885 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
886}
887
Brian Osman805a7272018-05-02 15:40:20 -0400888SkMatrix Viewer::computePerspectiveMatrix() {
889 SkScalar w = fWindow->width(), h = fWindow->height();
890 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
891 SkPoint perspPts[4] = {
892 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
893 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
894 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
895 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
896 };
897 SkMatrix m;
898 m.setPolyToPoly(orthoPts, perspPts, 4);
899 return m;
900}
901
Yuqian Li755778c2018-03-28 16:23:31 -0400902SkMatrix Viewer::computePreTouchMatrix() {
903 SkMatrix m = fDefaultMatrix;
904 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
905 : SK_Scalar1 + fZoomLevel;
Ben Wagner897dfa22018-08-09 15:18:46 -0400906 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -0400907 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400908
909 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
910 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400911
Brian Osman805a7272018-05-02 15:40:20 -0400912 if (kPerspective_Real == fPerspectiveMode) {
913 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400914 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400915 }
916
Yuqian Li755778c2018-03-28 16:23:31 -0400917 return m;
jvanverthc265a922016-04-08 12:51:45 -0700918}
919
liyuqiand3cdbca2016-05-17 12:44:20 -0700920SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400921 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700922 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400923 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700924 return m;
jvanverthc265a922016-04-08 12:51:45 -0700925}
926
Brian Osman621491e2017-02-28 15:45:01 -0500927void Viewer::setBackend(sk_app::Window::BackendType backendType) {
928 fBackendType = backendType;
929
930 fWindow->detach();
931
Brian Osman70d2f432017-11-08 09:54:10 -0500932#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400933 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
934 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500935 DisplayParams params = fWindow->getRequestedDisplayParams();
936 delete fWindow;
937 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500938
Brian Osman70d2f432017-11-08 09:54:10 -0500939 // re-register callbacks
940 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500941 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500942 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500943 fWindow->pushLayer(&fImGuiLayer);
944
Brian Osman70d2f432017-11-08 09:54:10 -0500945 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
946 // will still include our correct sample count. But the re-created fWindow will lose that
947 // information. On Windows, we need to re-create the window when changing sample count,
948 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
949 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
950 // as if we had never changed windows at all).
951 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500952#endif
953
Brian Osman70d2f432017-11-08 09:54:10 -0500954 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500955}
956
Brian Osman92004802017-03-06 11:47:26 -0500957void Viewer::setColorMode(ColorMode colorMode) {
958 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500959 this->updateTitle();
960 fWindow->inval();
961}
962
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500963class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
964public:
Mike Reed3ae47332019-01-04 10:11:46 -0500965 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
966 SkFont* font, Viewer::SkFontFields* ffields)
967 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500968 { }
Ben Wagner41e40472018-09-24 13:01:54 -0400969 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
970 sk_sp<SkTextBlob>* cache) {
971 bool blobWillChange = false;
972 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -0500973 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
974 bool shouldDraw = this->filterFont(&filteredFont);
975 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -0400976 blobWillChange = true;
977 break;
978 }
979 }
980 if (!blobWillChange) {
981 return blob;
982 }
983
984 SkTextBlobBuilder builder;
985 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -0500986 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
987 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -0400988 if (!shouldDraw) {
989 continue;
990 }
991
Mike Reed3ae47332019-01-04 10:11:46 -0500992 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -0500993
Ben Wagner41e40472018-09-24 13:01:54 -0400994 const SkTextBlobBuilder::RunBuffer& runBuffer
995 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -0500996 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -0400997 it.offset().x(),it.offset().y(), it.glyphCount(), it.textSize(), SkString())
998 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -0500999 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001000 it.offset().y(), it.glyphCount(), it.textSize(), SkString())
1001 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001002 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001003 it.glyphCount(), it.textSize(), SkString())
1004 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1005 uint32_t glyphCount = it.glyphCount();
1006 if (it.glyphs()) {
1007 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1008 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1009 }
1010 if (it.pos()) {
1011 size_t posSize = sizeof(decltype(*it.pos()));
1012 uint8_t positioning = it.positioning();
1013 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1014 }
1015 if (it.text()) {
1016 size_t textSize = sizeof(decltype(*it.text()));
1017 uint32_t textCount = it.textSize();
1018 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1019 }
1020 if (it.clusters()) {
1021 size_t clusterSize = sizeof(decltype(*it.clusters()));
1022 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1023 }
1024 }
1025 *cache = builder.make();
1026 return cache->get();
1027 }
1028 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1029 const SkPaint& paint) override {
1030 sk_sp<SkTextBlob> cache;
1031 this->SkPaintFilterCanvas::onDrawTextBlob(
1032 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1033 }
Mike Reed3ae47332019-01-04 10:11:46 -05001034 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
1035 if (fFontOverrides->fTextSize) {
1036 font->writable()->setSize(fFont->getSize());
1037 }
1038 if (fFontOverrides->fHinting) {
1039 font->writable()->setHinting(fFont->getHinting());
1040 }
1041#if 0
1042 if (fFontOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
1043 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1044 }
1045 if (fFontOverrides->fFlags & SkPaint::kFakeBoldText_Flag) {
1046 paint->writable()->setFakeBoldText(fPaint->isFakeBoldText());
1047 }
1048 if (fFontOverrides->fFlags & SkPaint::kLinearText_Flag) {
1049 paint->writable()->setLinearText(fPaint->isLinearText());
1050 }
1051 if (fFontOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
1052 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
1053 }
1054 if (fFontOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
1055 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
1056 }
1057 if (fFontOverrides->fFlags & SkPaint::kEmbeddedBitmapText_Flag) {
1058 paint->writable()->setEmbeddedBitmapText(fPaint->isEmbeddedBitmapText());
1059 }
1060 if (fFontOverrides->fFlags & SkPaint::kAutoHinting_Flag) {
1061 paint->writable()->setAutohinted(fPaint->isAutohinted());
1062 }
1063#endif
1064 return true;
1065 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001066 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001067 if (*paint == nullptr) {
1068 return true;
1069 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001070 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
1071 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1072 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001073 if (fPaintOverrides->fFlags & SkPaint::kDither_Flag) {
1074 paint->writable()->setDither(fPaint->isDither());
1075 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001076 return true;
1077 }
1078 SkPaint* fPaint;
1079 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001080 SkFont* fFont;
1081 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001082};
1083
Brian Osmanf750fbc2017-02-08 10:47:28 -05001084void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001085 SkAutoCanvasRestore autorestore(canvas, false);
1086
Brian Osmanf750fbc2017-02-08 10:47:28 -05001087 // By default, we render directly into the window's surface/canvas
1088 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001089 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001090
Brian Osmane0d4fba2017-03-15 10:24:55 -04001091 // If we're in any of the color managed modes, construct the color space we're going to use
Brian Osman03115dc2018-11-26 13:55:19 -05001092 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001093 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana9549ab2019-01-03 12:43:05 -05001094 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001095 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001096 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001097 }
1098
Brian Osman3ac99cf2017-12-01 11:23:53 -05001099 if (fSaveToSKP) {
1100 SkPictureRecorder recorder;
1101 SkCanvas* recorderCanvas = recorder.beginRecording(
1102 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001103 fSlides[fCurrentSlide]->draw(recorderCanvas);
1104 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1105 SkFILEWStream stream("sample_app.skp");
1106 picture->serialize(&stream);
1107 fSaveToSKP = false;
1108 }
1109
Brian Osmane9ed0f02018-11-26 14:50:05 -05001110 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
1111 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode) ? kRGBA_F16_SkColorType
1112 : kN32_SkColorType;
1113 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1114 canvas->getProps(&props);
1115
1116 auto make_surface = [=](int w, int h) {
1117 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1118 return Window::kRaster_BackendType == this->fBackendType
1119 ? SkSurface::MakeRaster(info, &props)
1120 : canvas->makeSurface(info, &props);
1121 };
1122
Brian Osman03115dc2018-11-26 13:55:19 -05001123 // We need to render offscreen if we're...
1124 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1125 // ... in any raster mode, because the window surface is actually GL
1126 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001127 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001128 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001129 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001130 Window::kRaster_BackendType == fBackendType ||
1131 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001132
Brian Osmane9ed0f02018-11-26 14:50:05 -05001133 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Mike Klein48b64902018-07-25 13:28:44 -04001134 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001135 }
1136
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001137 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001138 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001139 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001140 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001141 if (fTiled) {
1142 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1143 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1144 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1145 SkCanvas* tileCanvas = tileSurface->getCanvas();
1146 SkMatrix m = this->computeMatrix();
1147 for (int y = 0; y < fWindow->height(); y += tileH) {
1148 for (int x = 0; x < fWindow->width(); x += tileW) {
1149 SkAutoCanvasRestore acr(tileCanvas, true);
1150 tileCanvas->translate(-x, -y);
1151 tileCanvas->clear(SK_ColorTRANSPARENT);
1152 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001153 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1154 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001155 fSlides[fCurrentSlide]->draw(&filterCanvas);
1156 tileSurface->draw(slideCanvas, x, y, nullptr);
1157 }
1158 }
1159
1160 // Draw borders between tiles
1161 if (fDrawTileBoundaries) {
1162 SkPaint border;
1163 border.setColor(0x60FF00FF);
1164 border.setStyle(SkPaint::kStroke_Style);
1165 for (int y = 0; y < fWindow->height(); y += tileH) {
1166 for (int x = 0; x < fWindow->width(); x += tileW) {
1167 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1168 }
1169 }
1170 }
1171 } else {
1172 slideCanvas->concat(this->computeMatrix());
1173 if (kPerspective_Real == fPerspectiveMode) {
1174 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1175 }
Mike Reed3ae47332019-01-04 10:11:46 -05001176 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001177 fSlides[fCurrentSlide]->draw(&filterCanvas);
1178 }
Brian Osman56a24812017-12-19 11:15:16 -05001179 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001180 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001181
1182 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001183 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001184 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001185 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001186
1187 // If we rendered offscreen, snap an image and push the results to the window's canvas
1188 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001189 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001190
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001191 SkPaint paint;
1192 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001193 int prePerspectiveCount = canvas->save();
1194 if (kPerspective_Fake == fPerspectiveMode) {
1195 paint.setFilterQuality(kHigh_SkFilterQuality);
1196 canvas->clear(SK_ColorWHITE);
1197 canvas->concat(this->computePerspectiveMatrix());
1198 }
Brian Osman03115dc2018-11-26 13:55:19 -05001199 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001200 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001201 }
liyuqian6f163d22016-06-13 12:26:45 -07001202}
1203
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001204void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001205 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001206 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001207}
Jim Van Verth6f449692017-02-14 15:16:46 -05001208
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001209void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001210 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001211
brianosman622c8d52016-05-10 06:50:49 -07001212 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001213
Brian Osmand67e5182017-12-08 16:46:09 -05001214 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001215
1216 if (GrContext* ctx = fWindow->getGrContext()) {
1217 // Clean out cache items that haven't been used in more than 10 seconds.
1218 ctx->performDeferredCleanup(std::chrono::seconds(10));
1219 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001220}
1221
Ben Wagnera1915972018-08-09 15:06:19 -04001222void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001223 if (fCurrentSlide >= 0) {
1224 fSlides[fCurrentSlide]->resize(width, height);
1225 }
Ben Wagnera1915972018-08-09 15:06:19 -04001226}
1227
Florin Malitacefc1b92018-02-19 21:43:47 -05001228SkPoint Viewer::mapEvent(float x, float y) {
1229 const auto m = this->computeMatrix();
1230 SkMatrix inv;
1231
1232 SkAssertResult(m.invert(&inv));
1233
1234 return inv.mapXY(x, y);
1235}
1236
jvanverth814e38d2016-06-06 08:48:47 -07001237bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001238 if (GestureDevice::kMouse == fGestureDevice) {
1239 return false;
1240 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001241
1242 const auto slidePt = this->mapEvent(x, y);
1243 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1244 fWindow->inval();
1245 return true;
1246 }
1247
liyuqiand3cdbca2016-05-17 12:44:20 -07001248 void* castedOwner = reinterpret_cast<void*>(owner);
1249 switch (state) {
1250 case Window::kUp_InputState: {
1251 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001252#if defined(SK_BUILD_FOR_IOS)
1253 // TODO: move IOS swipe detection higher up into the platform code
1254 SkPoint dir;
1255 if (fGesture.isFling(&dir)) {
1256 // swiping left or right
1257 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1258 if (dir.fX < 0) {
1259 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1260 fCurrentSlide + 1 : 0);
1261 } else {
1262 this->setCurrentSlide(fCurrentSlide > 0 ?
1263 fCurrentSlide - 1 : fSlides.count() - 1);
1264 }
1265 }
1266 fGesture.reset();
1267 }
1268#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001269 break;
1270 }
1271 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001272 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001273 break;
1274 }
1275 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001276 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001277 break;
1278 }
1279 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001280 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001281 fWindow->inval();
1282 return true;
1283}
1284
Brian Osman80fc07e2017-12-08 16:45:43 -05001285bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001286 if (GestureDevice::kTouch == fGestureDevice) {
1287 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001288 }
Brian Osman16c81a12017-12-20 11:58:34 -05001289
Florin Malitacefc1b92018-02-19 21:43:47 -05001290 const auto slidePt = this->mapEvent(x, y);
1291 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1292 fWindow->inval();
1293 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001294 }
1295
1296 switch (state) {
1297 case Window::kUp_InputState: {
1298 fGesture.touchEnd(nullptr);
1299 break;
1300 }
1301 case Window::kDown_InputState: {
1302 fGesture.touchBegin(nullptr, x, y);
1303 break;
1304 }
1305 case Window::kMove_InputState: {
1306 fGesture.touchMoved(nullptr, x, y);
1307 break;
1308 }
1309 }
1310 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1311
1312 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1313 fWindow->inval();
1314 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001315 return true;
1316}
1317
Brian Osmana109e392017-02-24 09:49:14 -05001318static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1319 const ImVec2& pos, const ImVec2& size) {
1320 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1321 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1322
1323 // Invisible 10x10 button
1324 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1325 ImGui::InvisibleButton(label, ImVec2(10, 10));
1326
1327 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1328 ImGuiIO& io = ImGui::GetIO();
1329 // Normalized mouse position, relative to our gamut box
1330 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1331 // Clamp to edge of box, convert back to primary scale
1332 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1333 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1334 }
1335
1336 if (ImGui::IsItemHovered()) {
1337 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1338 }
1339
1340 // Return screen coordinates for the caller. We could just return center here, but we'd have
1341 // one frame of lag during drag.
1342 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1343}
1344
1345static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1346 ImDrawList* drawList = ImGui::GetWindowDrawList();
1347
1348 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1349 // width, and scale the height to maintain aspect ratio.
1350 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1351 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1352 ImVec2 pos = ImGui::GetCursorScreenPos();
1353
1354 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1355 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1356 // Magic numbers are pixel locations of the origin and upper-right corner.
1357 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1358 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001359
1360 // Primary markers
1361 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1362 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1363 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1364 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1365
1366 // Gamut triangle
1367 drawList->AddCircle(r, 5.0f, 0xFF000040);
1368 drawList->AddCircle(g, 5.0f, 0xFF004000);
1369 drawList->AddCircle(b, 5.0f, 0xFF400000);
1370 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1371 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1372
1373 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001374 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1375}
1376
1377static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1378 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1379 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1380 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1381
1382 // Invisible 10x10 button
1383 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1384 ImGui::InvisibleButton(label, ImVec2(10, 10));
1385
1386 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1387 ImGuiIO& io = ImGui::GetIO();
1388 // Normalized mouse position, relative to our gamut box
1389 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1390 // Clamp to edge of box
1391 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1392 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1393 *dragging = true;
1394 }
1395
1396 // Return screen coordinates for the caller. We could just return center here, but we'd have
1397 // one frame of lag during drag.
1398 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1399}
1400
Ben Wagner3627d2e2018-06-26 14:23:20 -04001401static bool ImGui_DragLocation(SkPoint* pt) {
1402 ImDrawList* drawList = ImGui::GetWindowDrawList();
1403
1404 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1405 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1406 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1407 ImVec2 pos = ImGui::GetCursorScreenPos();
1408
1409 // Background rectangle
1410 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1411
1412 // Location marker
1413 bool dragging = false;
1414 ImVec2 tl = ImGui_DragPoint("SL", pt + 0, pos, size, &dragging);
1415 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1416
1417 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1418 ImGui::Spacing();
1419
1420 return dragging;
1421}
1422
Brian Osman9bb47cf2018-04-26 15:55:00 -04001423static bool ImGui_DragQuad(SkPoint* pts) {
1424 ImDrawList* drawList = ImGui::GetWindowDrawList();
1425
1426 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1427 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1428 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1429 ImVec2 pos = ImGui::GetCursorScreenPos();
1430
1431 // Background rectangle
1432 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1433
1434 // Corner markers
1435 bool dragging = false;
1436 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1437 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1438 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1439 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1440
1441 // Draw markers and quad
1442 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1443 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1444 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1445 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1446 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1447 drawList->AddLine(tr, br, 0xFFFFFFFF);
1448 drawList->AddLine(br, bl, 0xFFFFFFFF);
1449 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1450
1451 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1452 ImGui::Spacing();
1453
1454 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001455}
1456
Brian Osmand67e5182017-12-08 16:46:09 -05001457void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001458 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1459 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001460 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001461 }
1462
1463 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001464 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1465 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001466 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001467 DisplayParams params = fWindow->getRequestedDisplayParams();
1468 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001469 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1470 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001471 if (ImGui::CollapsingHeader("Backend")) {
1472 int newBackend = static_cast<int>(fBackendType);
1473 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1474 ImGui::SameLine();
1475 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001476#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1477 ImGui::SameLine();
1478 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1479#endif
Brian Osman621491e2017-02-28 15:45:01 -05001480#if defined(SK_VULKAN)
1481 ImGui::SameLine();
1482 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1483#endif
1484 if (newBackend != fBackendType) {
1485 fDeferredActions.push_back([=]() {
1486 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1487 });
1488 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001489
Brian Salomon99a33902017-03-07 15:16:34 -05001490 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001491 bool* wire = &params.fGrContextOptions.fWireframeMode;
1492 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1493 paramsChanged = true;
1494 }
Brian Salomon99a33902017-03-07 15:16:34 -05001495
Brian Osman28b12522017-03-08 17:10:24 -05001496 if (ctx) {
1497 int sampleCount = fWindow->sampleCount();
1498 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001499 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001500 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1501 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1502 ImGui::RadioButton("16", &sampleCount, 16);
1503
1504 if (sampleCount != params.fMSAASampleCount) {
1505 params.fMSAASampleCount = sampleCount;
1506 paramsChanged = true;
1507 }
1508 }
1509
Ben Wagner37c54032018-04-13 14:30:23 -04001510 int pixelGeometryIdx = 0;
1511 if (fPixelGeometryOverrides) {
1512 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1513 }
1514 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1515 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1516 {
1517 uint32_t flags = params.fSurfaceProps.flags();
1518 if (pixelGeometryIdx == 0) {
1519 fPixelGeometryOverrides = false;
1520 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1521 } else {
1522 fPixelGeometryOverrides = true;
1523 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1524 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1525 }
1526 paramsChanged = true;
1527 }
1528
1529 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1530 if (ImGui::Checkbox("DFT", &useDFT)) {
1531 uint32_t flags = params.fSurfaceProps.flags();
1532 if (useDFT) {
1533 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1534 } else {
1535 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1536 }
1537 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1538 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1539 paramsChanged = true;
1540 }
1541
Brian Osman8a9de3d2017-03-01 14:59:05 -05001542 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001543 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001544 auto prButton = [&](GpuPathRenderers x) {
1545 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001546 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1547 params.fGrContextOptions.fGpuPathRenderers = x;
1548 paramsChanged = true;
1549 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001550 }
1551 };
1552
1553 if (!ctx) {
1554 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001555 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001556 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001557 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001558 prButton(GpuPathRenderers::kStencilAndCover);
1559 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001560 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001561 prButton(GpuPathRenderers::kNone);
1562 } else {
1563 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001564 if (GrCoverageCountingPathRenderer::IsSupported(
1565 *ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001566 prButton(GpuPathRenderers::kCoverageCounting);
1567 }
Jim Van Verth83010462017-03-16 08:45:39 -04001568 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001569 prButton(GpuPathRenderers::kTessellating);
1570 prButton(GpuPathRenderers::kNone);
1571 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001572 ImGui::TreePop();
1573 }
Brian Osman621491e2017-02-28 15:45:01 -05001574 }
1575
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001576 if (ImGui::CollapsingHeader("Transform")) {
1577 float zoom = fZoomLevel;
1578 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1579 fZoomLevel = zoom;
1580 this->preTouchMatrixChanged();
1581 paramsChanged = true;
1582 }
1583 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001584 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001585 fRotation = deg;
1586 this->preTouchMatrixChanged();
1587 paramsChanged = true;
1588 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001589 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1590 if (ImGui_DragLocation(&fOffset)) {
1591 this->preTouchMatrixChanged();
1592 paramsChanged = true;
1593 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001594 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1595 this->preTouchMatrixChanged();
1596 paramsChanged = true;
1597 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001598 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001599 if (ImGui::CollapsingHeader("Tiling")) {
1600 ImGui::Checkbox("Enable", &fTiled);
1601 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1602 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1603 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1604 }
Brian Osman805a7272018-05-02 15:40:20 -04001605 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1606 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1607 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001608 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001609 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001610 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001611 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001612 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001613 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001614 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001615 }
1616
Ben Wagnera580fb32018-04-17 11:16:32 -04001617 if (ImGui::CollapsingHeader("Paint")) {
1618 int hintingIdx = 0;
Mike Reed3ae47332019-01-04 10:11:46 -05001619 if (fFontOverrides.fHinting) {
1620 hintingIdx = static_cast<unsigned>(fFont.getHinting()) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001621 }
1622 if (ImGui::Combo("Hinting", &hintingIdx,
1623 "Default\0None\0Slight\0Normal\0Full\0\0"))
1624 {
1625 if (hintingIdx == 0) {
Mike Reed3ae47332019-01-04 10:11:46 -05001626 fFontOverrides.fHinting = false;
1627 fFont.setHinting(kNo_SkFontHinting);
Ben Wagnera580fb32018-04-17 11:16:32 -04001628 } else {
Mike Reed3ae47332019-01-04 10:11:46 -05001629 fFontOverrides.fHinting = true;
Mike Reed9edbf422018-11-07 19:54:33 -05001630 SkFontHinting hinting = SkTo<SkFontHinting>(hintingIdx - 1);
Mike Reed3ae47332019-01-04 10:11:46 -05001631 fFont.setHinting(hinting);
Ben Wagnera580fb32018-04-17 11:16:32 -04001632 }
1633 paramsChanged = true;
1634 }
1635
1636 int aliasIdx = 0;
1637 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1638 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1639 }
1640 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1641 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1642 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1643 {
1644 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1645 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1646 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1647 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1648 if (aliasIdx == 0) {
1649 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1650 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1651 } else {
1652 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1653 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1654 fPaint.setAntiAlias(aliasIdx > 1);
1655 switch (fPaintOverrides.fAntiAlias) {
1656 case SkPaintFields::AntiAliasState::Alias:
1657 break;
1658 case SkPaintFields::AntiAliasState::Normal:
1659 break;
1660 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1661 gSkUseAnalyticAA = true;
1662 gSkForceAnalyticAA = false;
1663 gSkUseDeltaAA = gSkForceDeltaAA = false;
1664 break;
1665 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1666 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1667 gSkUseDeltaAA = gSkForceDeltaAA = false;
1668 break;
1669 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1670 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1671 gSkUseDeltaAA = true;
1672 gSkForceDeltaAA = false;
1673 break;
1674 case SkPaintFields::AntiAliasState::DeltaAAForced:
1675 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1676 gSkUseDeltaAA = gSkForceDeltaAA = true;
1677 break;
1678 }
1679 }
1680 paramsChanged = true;
1681 }
1682
Ben Wagner99a78dc2018-05-09 18:23:51 -04001683 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
1684 SkPaint::Flags flag,
1685 bool (SkPaint::* isFlag)() const,
1686 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001687 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001688 int itemIndex = 0;
1689 if (fPaintOverrides.fFlags & flag) {
1690 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001691 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001692 if (ImGui::Combo(label, &itemIndex, items)) {
1693 if (itemIndex == 0) {
1694 fPaintOverrides.fFlags &= ~flag;
1695 } else {
1696 fPaintOverrides.fFlags |= flag;
1697 (fPaint.*setFlag)(itemIndex == 2);
1698 }
1699 paramsChanged = true;
1700 }
1701 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001702
Ben Wagner99a78dc2018-05-09 18:23:51 -04001703 paintFlag("Dither",
1704 "Default\0No Dither\0Dither\0\0",
1705 SkPaint::kDither_Flag,
1706 &SkPaint::isDither, &SkPaint::setDither);
Mike Reed3ae47332019-01-04 10:11:46 -05001707#if 0
Ben Wagner99a78dc2018-05-09 18:23:51 -04001708 paintFlag("Fake Bold Glyphs",
1709 "Default\0No Fake Bold\0Fake Bold\0\0",
1710 SkPaint::kFakeBoldText_Flag,
1711 &SkPaint::isFakeBoldText, &SkPaint::setFakeBoldText);
1712
1713 paintFlag("Linear Text",
1714 "Default\0No Linear Text\0Linear Text\0\0",
1715 SkPaint::kLinearText_Flag,
1716 &SkPaint::isLinearText, &SkPaint::setLinearText);
1717
1718 paintFlag("Subpixel Position Glyphs",
1719 "Default\0Pixel Text\0Subpixel Text\0\0",
1720 SkPaint::kSubpixelText_Flag,
1721 &SkPaint::isSubpixelText, &SkPaint::setSubpixelText);
1722
1723 paintFlag("Subpixel Anti-Alias",
1724 "Default\0lcd\0LCD\0\0",
1725 SkPaint::kLCDRenderText_Flag,
1726 &SkPaint::isLCDRenderText, &SkPaint::setLCDRenderText);
1727
1728 paintFlag("Embedded Bitmap Text",
1729 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1730 SkPaint::kEmbeddedBitmapText_Flag,
1731 &SkPaint::isEmbeddedBitmapText, &SkPaint::setEmbeddedBitmapText);
1732
1733 paintFlag("Force Auto-Hinting",
1734 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1735 SkPaint::kAutoHinting_Flag,
1736 &SkPaint::isAutohinted, &SkPaint::setAutohinted);
Mike Reed3ae47332019-01-04 10:11:46 -05001737#endif
1738 ImGui::Checkbox("Override TextSize", &fFontOverrides.fTextSize);
1739 if (fFontOverrides.fTextSize) {
1740 ImGui::DragFloat2("TextRange", fFontOverrides.fTextSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001741 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001742 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001743 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Mike Reed3ae47332019-01-04 10:11:46 -05001744 fFontOverrides.fTextSizeRange[0],
1745 fFontOverrides.fTextSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001746 "%.6f", 2.0f))
1747 {
Mike Reed3ae47332019-01-04 10:11:46 -05001748 fFont.setSize(textSize);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001749 this->preTouchMatrixChanged();
1750 paramsChanged = true;
1751 }
1752 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001753 }
1754
Mike Reed81f60ec2018-05-15 10:09:52 -04001755 {
1756 SkMetaData controls;
1757 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1758 if (ImGui::CollapsingHeader("Current Slide")) {
1759 SkMetaData::Iter iter(controls);
1760 const char* name;
1761 SkMetaData::Type type;
1762 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001763 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001764 if (type == SkMetaData::kScalar_Type) {
1765 float val[3];
1766 SkASSERT(count == 3);
1767 controls.findScalars(name, &count, val);
1768 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1769 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001770 }
1771 }
1772 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001773 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001774 }
1775 }
1776 }
1777
Ben Wagner7a3c6742018-04-23 10:01:07 -04001778 if (fShowSlidePicker) {
1779 ImGui::SetNextTreeNodeOpen(true);
1780 }
Brian Osman79086b92017-02-10 13:36:16 -05001781 if (ImGui::CollapsingHeader("Slide")) {
1782 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001783 static ImVector<const char*> filteredSlideNames;
1784 static ImVector<int> filteredSlideIndices;
1785
Brian Osmanfce09c52017-11-14 15:32:20 -05001786 if (fShowSlidePicker) {
1787 ImGui::SetKeyboardFocusHere();
1788 fShowSlidePicker = false;
1789 }
1790
Brian Osman79086b92017-02-10 13:36:16 -05001791 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001792 filteredSlideNames.clear();
1793 filteredSlideIndices.clear();
1794 int filteredIndex = 0;
1795 for (int i = 0; i < fSlides.count(); ++i) {
1796 const char* slideName = fSlides[i]->getName().c_str();
1797 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1798 if (i == fCurrentSlide) {
1799 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001800 }
Brian Osmanf479e422017-11-08 13:11:36 -05001801 filteredSlideNames.push_back(slideName);
1802 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001803 }
Brian Osman79086b92017-02-10 13:36:16 -05001804 }
Brian Osmanf479e422017-11-08 13:11:36 -05001805
Brian Osmanf479e422017-11-08 13:11:36 -05001806 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1807 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001808 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001809 }
1810 }
Brian Osmana109e392017-02-24 09:49:14 -05001811
1812 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001813 ColorMode newMode = fColorMode;
1814 auto cmButton = [&](ColorMode mode, const char* label) {
1815 if (ImGui::RadioButton(label, mode == fColorMode)) {
1816 newMode = mode;
1817 }
1818 };
1819
1820 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001821 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1822 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001823
1824 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001825 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001826 }
1827
1828 // Pick from common gamuts:
1829 int primariesIdx = 4; // Default: Custom
1830 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1831 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1832 primariesIdx = i;
1833 break;
1834 }
1835 }
1836
Brian Osman03115dc2018-11-26 13:55:19 -05001837 // Let user adjust the gamma
Brian Osmana9549ab2019-01-03 12:43:05 -05001838 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001839
Brian Osmana109e392017-02-24 09:49:14 -05001840 if (ImGui::Combo("Primaries", &primariesIdx,
1841 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1842 if (primariesIdx >= 0 && primariesIdx <= 3) {
1843 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1844 }
1845 }
1846
1847 // Allow direct editing of gamut
1848 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1849 }
Brian Osman79086b92017-02-10 13:36:16 -05001850 }
Brian Salomon99a33902017-03-07 15:16:34 -05001851 if (paramsChanged) {
1852 fDeferredActions.push_back([=]() {
1853 fWindow->setRequestedDisplayParams(params);
1854 fWindow->inval();
1855 this->updateTitle();
1856 });
1857 }
Brian Osman79086b92017-02-10 13:36:16 -05001858 ImGui::End();
1859 }
1860
Brian Osmanf6877092017-02-13 09:39:57 -05001861 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04001862 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
1863 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001864 static int zoomFactor = 8;
1865 if (ImGui::Button("<<")) {
1866 zoomFactor = SkTMax(zoomFactor / 2, 4);
1867 }
1868 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1869 if (ImGui::Button(">>")) {
1870 zoomFactor = SkTMin(zoomFactor * 2, 32);
1871 }
Brian Osmanf6877092017-02-13 09:39:57 -05001872
Ben Wagner3627d2e2018-06-26 14:23:20 -04001873 if (!fZoomWindowFixed) {
1874 ImVec2 mousePos = ImGui::GetMousePos();
1875 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
1876 }
1877 SkScalar x = fZoomWindowLocation.x();
1878 SkScalar y = fZoomWindowLocation.y();
1879 int xInt = SkScalarRoundToInt(x);
1880 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05001881 ImVec2 avail = ImGui::GetContentRegionAvail();
1882
Brian Osmanead517d2017-11-13 15:36:36 -05001883 uint32_t pixel = 0;
1884 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001885 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001886 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001887 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
Ben Wagner3627d2e2018-06-26 14:23:20 -04001888 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05001889 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001890 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1891 }
1892
Brian Osmand67e5182017-12-08 16:46:09 -05001893 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001894 // Translate so the region of the image that's under the mouse cursor is centered
1895 // in the zoom canvas:
1896 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001897 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
1898 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05001899 c->drawImage(this->fLastImage, 0, 0);
1900
1901 SkPaint outline;
1902 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001903 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05001904 });
Brian Osmanf6877092017-02-13 09:39:57 -05001905 }
1906
1907 ImGui::End();
1908 }
Brian Osman79086b92017-02-10 13:36:16 -05001909}
1910
liyuqian2edb0f42016-07-06 14:11:32 -07001911void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001912 for (int i = 0; i < fDeferredActions.count(); ++i) {
1913 fDeferredActions[i]();
1914 }
1915 fDeferredActions.reset();
1916
Brian Osman56a24812017-12-19 11:15:16 -05001917 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001918 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001919 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001920 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001921
Brian Osman79086b92017-02-10 13:36:16 -05001922 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04001923 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
1924 // not be visible, though. So we need to redraw if there is at least one visible window, or
1925 // more than one active window. Newly created windows are active but not visible for one frame
1926 // while they determine their layout and sizing.
1927 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
1928 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07001929 fWindow->inval();
1930 }
jvanverth9f372462016-04-06 06:08:59 -07001931}
liyuqiane5a6cd92016-05-27 08:52:52 -07001932
Florin Malitab632df72018-06-18 21:23:06 -04001933template <typename OptionsFunc>
1934static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
1935 OptionsFunc&& optionsFunc) {
1936 writer.beginObject();
1937 {
1938 writer.appendString(kName , name);
1939 writer.appendString(kValue, value);
1940
1941 writer.beginArray(kOptions);
1942 {
1943 optionsFunc(writer);
1944 }
1945 writer.endArray();
1946 }
1947 writer.endObject();
1948}
1949
1950
liyuqiane5a6cd92016-05-27 08:52:52 -07001951void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001952 if (!fWindow) {
1953 return;
1954 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001955 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001956 return; // Surface hasn't been created yet.
1957 }
1958
Florin Malitab632df72018-06-18 21:23:06 -04001959 SkDynamicMemoryWStream memStream;
1960 SkJSONWriter writer(&memStream);
1961 writer.beginArray();
1962
liyuqianb73c24b2016-06-03 08:47:23 -07001963 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04001964 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
1965 [this](SkJSONWriter& writer) {
1966 for(const auto& slide : fSlides) {
1967 writer.appendString(slide->getName().c_str());
1968 }
1969 });
liyuqiane5a6cd92016-05-27 08:52:52 -07001970
liyuqianb73c24b2016-06-03 08:47:23 -07001971 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04001972 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
1973 [](SkJSONWriter& writer) {
1974 for (const auto& str : kBackendTypeStrings) {
1975 writer.appendString(str);
1976 }
1977 });
liyuqiane5a6cd92016-05-27 08:52:52 -07001978
csmartdalton578f0642017-02-24 16:04:47 -07001979 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04001980 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
1981 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
1982 [this](SkJSONWriter& writer) {
1983 writer.appendS32(0);
1984
1985 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1986 return;
1987 }
1988
1989 for (int msaa : {4, 8, 16}) {
1990 writer.appendS32(msaa);
1991 }
1992 });
csmartdalton578f0642017-02-24 16:04:47 -07001993
csmartdalton61cd31a2017-02-27 17:00:53 -07001994 // Path renderer state
1995 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04001996 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
1997 [this](SkJSONWriter& writer) {
1998 const GrContext* ctx = fWindow->getGrContext();
1999 if (!ctx) {
2000 writer.appendString("Software");
2001 } else {
2002 const auto* caps = ctx->contextPriv().caps();
2003
Florin Malitab632df72018-06-18 21:23:06 -04002004 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2005 if (fWindow->sampleCount() > 1) {
2006 if (caps->shaderCaps()->pathRenderingSupport()) {
2007 writer.appendString(
2008 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2009 }
2010 } else {
2011 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2012 writer.appendString(
2013 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2014 }
2015 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2016 }
2017 writer.appendString(
2018 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2019 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2020 }
2021 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002022
liyuqianb73c24b2016-06-03 08:47:23 -07002023 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002024 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2025 [this](SkJSONWriter& writer) {
2026 writer.appendString(kSoftkeyHint);
2027 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2028 writer.appendString(softkey.c_str());
2029 }
2030 });
liyuqianb73c24b2016-06-03 08:47:23 -07002031
Florin Malitab632df72018-06-18 21:23:06 -04002032 writer.endArray();
2033 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002034
Florin Malitab632df72018-06-18 21:23:06 -04002035 auto data = memStream.detachAsData();
2036
2037 // TODO: would be cool to avoid this copy
2038 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2039
2040 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002041}
2042
2043void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002044 // For those who will add more features to handle the state change in this function:
2045 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2046 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2047 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002048 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002049 for (int i = 0; i < fSlides.count(); ++i) {
2050 if (fSlides[i]->getName().equals(stateValue)) {
2051 this->setCurrentSlide(i);
2052 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002053 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002054 }
Florin Malitaab99c342018-01-16 16:23:03 -05002055
2056 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002057 } else if (stateName.equals(kBackendStateName)) {
2058 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2059 if (stateValue.equals(kBackendTypeStrings[i])) {
2060 if (fBackendType != i) {
2061 fBackendType = (sk_app::Window::BackendType)i;
2062 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002063 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002064 }
2065 break;
2066 }
2067 }
csmartdalton578f0642017-02-24 16:04:47 -07002068 } else if (stateName.equals(kMSAAStateName)) {
2069 DisplayParams params = fWindow->getRequestedDisplayParams();
2070 int sampleCount = atoi(stateValue.c_str());
2071 if (sampleCount != params.fMSAASampleCount) {
2072 params.fMSAASampleCount = sampleCount;
2073 fWindow->setRequestedDisplayParams(params);
2074 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002075 this->updateTitle();
2076 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002077 }
2078 } else if (stateName.equals(kPathRendererStateName)) {
2079 DisplayParams params = fWindow->getRequestedDisplayParams();
2080 for (const auto& pair : gPathRendererNames) {
2081 if (pair.second == stateValue.c_str()) {
2082 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2083 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2084 fWindow->setRequestedDisplayParams(params);
2085 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002086 this->updateTitle();
2087 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002088 }
2089 break;
2090 }
csmartdalton578f0642017-02-24 16:04:47 -07002091 }
liyuqianb73c24b2016-06-03 08:47:23 -07002092 } else if (stateName.equals(kSoftkeyStateName)) {
2093 if (!stateValue.equals(kSoftkeyHint)) {
2094 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002095 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002096 }
liyuqian2edb0f42016-07-06 14:11:32 -07002097 } else if (stateName.equals(kRefreshStateName)) {
2098 // This state is actually NOT in the UI state.
2099 // We use this to allow Android to quickly set bool fRefresh.
2100 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002101 } else {
2102 SkDebugf("Unknown stateName: %s", stateName.c_str());
2103 }
2104}
Brian Osman79086b92017-02-10 13:36:16 -05002105
2106bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002107 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002108}
2109
2110bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002111 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002112 fWindow->inval();
2113 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002114 } else {
2115 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002116 }
Brian Osman79086b92017-02-10 13:36:16 -05002117}