blob: 945b07f19b48394b7bea600fedc32334bc189c3f [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\""
Jim Van Verthbe39f712019-02-08 15:36:14 -050068#elif defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
69# define BACKENDS_STR "\"sw\", \"gl\", and \"mtl\""
bsalomon6c471f72016-07-26 12:56:32 -070070#else
71# define BACKENDS_STR "\"sw\" and \"gl\""
72#endif
73
Brian Osman2dd96932016-10-18 15:33:53 -040074static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070075
Brian Salomonbdecacf2018-02-02 20:32:49 -050076static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070077
Chris Dalton2d18f412018-02-20 13:23:32 -070078DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
79
Brian Osman53136aa2017-07-20 15:43:35 -040080DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050081
Florin Malita38792ce2018-05-08 10:36:18 -040082DEFINE_string2(file, f, "", "Open a single file for viewing.");
83
Brian Salomon194db172017-08-17 14:37:06 -040084const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070085 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040086#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
87 "ANGLE",
88#endif
jvanverth063ece72016-06-17 09:29:14 -070089#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070090 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070091#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -050092#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
93 "Metal",
94#endif
csmartdalton578f0642017-02-24 16:04:47 -070095 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070096};
97
bsalomon6c471f72016-07-26 12:56:32 -070098static sk_app::Window::BackendType get_backend_type(const char* str) {
99#ifdef SK_VULKAN
100 if (0 == strcmp(str, "vk")) {
101 return sk_app::Window::kVulkan_BackendType;
102 } else
103#endif
Brian Salomon194db172017-08-17 14:37:06 -0400104#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
105 if (0 == strcmp(str, "angle")) {
106 return sk_app::Window::kANGLE_BackendType;
107 } else
108#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -0500109#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
110 if (0 == strcmp(str, "mtl")) {
111 return sk_app::Window::kMetal_BackendType;
112 } else
113#endif
bsalomon6c471f72016-07-26 12:56:32 -0700114 if (0 == strcmp(str, "gl")) {
115 return sk_app::Window::kNativeGL_BackendType;
116 } else if (0 == strcmp(str, "sw")) {
117 return sk_app::Window::kRaster_BackendType;
118 } else {
119 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
120 return sk_app::Window::kRaster_BackendType;
121 }
122}
123
Brian Osmana109e392017-02-24 09:49:14 -0500124static SkColorSpacePrimaries gSrgbPrimaries = {
125 0.64f, 0.33f,
126 0.30f, 0.60f,
127 0.15f, 0.06f,
128 0.3127f, 0.3290f };
129
130static SkColorSpacePrimaries gAdobePrimaries = {
131 0.64f, 0.33f,
132 0.21f, 0.71f,
133 0.15f, 0.06f,
134 0.3127f, 0.3290f };
135
136static SkColorSpacePrimaries gP3Primaries = {
137 0.680f, 0.320f,
138 0.265f, 0.690f,
139 0.150f, 0.060f,
140 0.3127f, 0.3290f };
141
142static SkColorSpacePrimaries gRec2020Primaries = {
143 0.708f, 0.292f,
144 0.170f, 0.797f,
145 0.131f, 0.046f,
146 0.3127f, 0.3290f };
147
148struct NamedPrimaries {
149 const char* fName;
150 SkColorSpacePrimaries* fPrimaries;
151} gNamedPrimaries[] = {
152 { "sRGB", &gSrgbPrimaries },
153 { "AdobeRGB", &gAdobePrimaries },
154 { "P3", &gP3Primaries },
155 { "Rec. 2020", &gRec2020Primaries },
156};
157
158static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
159 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
160}
161
Brian Osman70d2f432017-11-08 09:54:10 -0500162static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
163 // In raster mode, we still use GL for the window.
164 // This lets us render the GUI faster (and correct).
165 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
166}
167
liyuqiane5a6cd92016-05-27 08:52:52 -0700168const char* kName = "name";
169const char* kValue = "value";
170const char* kOptions = "options";
171const char* kSlideStateName = "Slide";
172const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700173const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700174const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700175const char* kSoftkeyStateName = "Softkey";
176const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700177const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700178const char* kON = "ON";
179const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700180const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700181
jvanverth34524262016-05-04 13:49:13 -0700182Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500183 : fCurrentSlide(-1)
184 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500185 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500186 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500187 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500188 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500189 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400190 , fZoomWindowFixed(false)
191 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500192 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400193 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700194 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500195 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500196 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500197 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500198 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700199 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400200 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400201 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400202 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500203 , fTiled(false)
204 , fDrawTileBoundaries(false)
205 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400206 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700207{
Greg Daniel285db442016-10-14 09:12:53 -0400208 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700209
Brian Osmanf09e35e2017-12-15 14:48:09 -0500210 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500211 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500212 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
213 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
214 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
215 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700216
jvanverth2bb3b6d2016-04-08 07:24:09 -0700217 SkDebugf("Command line arguments: ");
218 for (int i = 1; i < argc; ++i) {
219 SkDebugf("%s ", argv[i]);
220 }
221 SkDebugf("\n");
222
223 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500224#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400225 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500226#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700227
Ben Wagner483c7722018-02-20 17:06:07 -0500228 if (!FLAGS_nativeFonts) {
229 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
230 }
231
Brian Osmanbc8150f2017-07-24 11:38:01 -0400232 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400233 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400234
bsalomon6c471f72016-07-26 12:56:32 -0700235 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700236 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700237
csmartdalton578f0642017-02-24 16:04:47 -0700238 DisplayParams displayParams;
239 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700240 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700241 fWindow->setRequestedDisplayParams(displayParams);
242
Brian Osman56a24812017-12-19 11:15:16 -0500243 // Configure timers
244 fStatsLayer.setActive(false);
245 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
246 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
247 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
248
jvanverth9f372462016-04-06 06:08:59 -0700249 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700250 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500251 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500252 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500253 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700254
brianosman622c8d52016-05-10 06:50:49 -0700255 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500256 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
257 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
258 fWindow->inval();
259 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500260 // Command to jump directly to the slide picker and give it focus
261 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
262 this->fShowImGuiDebugWindow = true;
263 this->fShowSlidePicker = true;
264 fWindow->inval();
265 });
266 // Alias that to Backspace, to match SampleApp
267 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
268 this->fShowImGuiDebugWindow = true;
269 this->fShowSlidePicker = true;
270 fWindow->inval();
271 });
Brian Osman79086b92017-02-10 13:36:16 -0500272 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
273 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
274 fWindow->inval();
275 });
Brian Osmanf6877092017-02-13 09:39:57 -0500276 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
277 this->fShowZoomWindow = !this->fShowZoomWindow;
278 fWindow->inval();
279 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400280 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
281 this->fZoomWindowFixed = !this->fZoomWindowFixed;
282 fWindow->inval();
283 });
brianosman622c8d52016-05-10 06:50:49 -0700284 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500285 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700286 fWindow->inval();
287 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400288 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500289 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400290 this->updateTitle();
291 fWindow->inval();
292 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500293 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500294 switch (fColorMode) {
295 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500296 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500297 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500298 case ColorMode::kColorManaged8888:
299 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500300 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500301 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500302 this->setColorMode(ColorMode::kLegacy);
303 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500304 }
brianosman622c8d52016-05-10 06:50:49 -0700305 });
306 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500307 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700308 });
309 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500310 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700311 });
312 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
313 this->changeZoomLevel(1.f / 32.f);
314 fWindow->inval();
315 });
316 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
317 this->changeZoomLevel(-1.f / 32.f);
318 fWindow->inval();
319 });
jvanverthaf236b52016-05-20 06:01:06 -0700320 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400321 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
322 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500323 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400324#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
325 if (newBackend == sk_app::Window::kVulkan_BackendType) {
326 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
327 sk_app::Window::kBackendTypeCount);
328 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
329 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500330 }
331#endif
Brian Osman621491e2017-02-28 15:45:01 -0500332 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700333 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500334 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
335 fSaveToSKP = true;
336 fWindow->inval();
337 });
Ben Wagner37c54032018-04-13 14:30:23 -0400338 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
339 DisplayParams params = fWindow->getRequestedDisplayParams();
340 uint32_t flags = params.fSurfaceProps.flags();
341 if (!fPixelGeometryOverrides) {
342 fPixelGeometryOverrides = true;
343 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
344 } else {
345 switch (params.fSurfaceProps.pixelGeometry()) {
346 case kUnknown_SkPixelGeometry:
347 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
348 break;
349 case kRGB_H_SkPixelGeometry:
350 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
351 break;
352 case kBGR_H_SkPixelGeometry:
353 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
354 break;
355 case kRGB_V_SkPixelGeometry:
356 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
357 break;
358 case kBGR_V_SkPixelGeometry:
359 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
360 fPixelGeometryOverrides = false;
361 break;
362 }
363 }
364 fWindow->setRequestedDisplayParams(params);
365 this->updateTitle();
366 fWindow->inval();
367 });
Ben Wagner9613e452019-01-23 10:34:59 -0500368 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500369 if (!fFontOverrides.fHinting) {
370 fFontOverrides.fHinting = true;
371 fFont.setHinting(kNo_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500372 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500373 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500374 case kNo_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500375 fFont.setHinting(kSlight_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500376 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500377 case kSlight_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500378 fFont.setHinting(kNormal_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500379 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500380 case kNormal_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500381 fFont.setHinting(kFull_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500382 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500383 case kFull_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500384 fFont.setHinting(kNo_SkFontHinting);
385 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500386 break;
387 }
388 }
389 this->updateTitle();
390 fWindow->inval();
391 });
392 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500393 if (!fPaintOverrides.fAntiAlias) {
394 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
395 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500396 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500397 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
398 gSkUseDeltaAA = gSkForceDeltaAA = false;
399 } else {
400 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500401 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500402 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500403 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400404 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
405 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500406 break;
407 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500408 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500409 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400410 gSkForceAnalyticAA = false;
411 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500412 break;
413 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500414 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400415 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
416 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500417 break;
418 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500419 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::DeltaAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500420 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
421 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400422 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500423 break;
424 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500425 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400426 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
427 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500428 break;
429 case SkPaintFields::AntiAliasState::DeltaAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500430 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
431 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500432 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
433 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
434 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
435 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
436 break;
437 }
438 }
439 this->updateTitle();
440 fWindow->inval();
441 });
Ben Wagner37c54032018-04-13 14:30:23 -0400442 fCommands.addCommand('D', "Modes", "DFT", [this]() {
443 DisplayParams params = fWindow->getRequestedDisplayParams();
444 uint32_t flags = params.fSurfaceProps.flags();
445 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
446 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
447 fWindow->setRequestedDisplayParams(params);
448 this->updateTitle();
449 fWindow->inval();
450 });
Ben Wagner9613e452019-01-23 10:34:59 -0500451 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500452 if (!fFontOverrides.fEdging) {
453 fFontOverrides.fEdging = true;
454 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500455 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500456 switch (fFont.getEdging()) {
457 case SkFont::Edging::kAlias:
458 fFont.setEdging(SkFont::Edging::kAntiAlias);
459 break;
460 case SkFont::Edging::kAntiAlias:
461 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
462 break;
463 case SkFont::Edging::kSubpixelAntiAlias:
464 fFont.setEdging(SkFont::Edging::kAlias);
465 fFontOverrides.fEdging = false;
466 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500467 }
468 }
469 this->updateTitle();
470 fWindow->inval();
471 });
Ben Wagner9613e452019-01-23 10:34:59 -0500472 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500473 if (!fFontOverrides.fSubpixel) {
474 fFontOverrides.fSubpixel = true;
475 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500476 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500477 if (!fFont.isSubpixel()) {
478 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500479 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500480 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500481 }
482 }
483 this->updateTitle();
484 fWindow->inval();
485 });
Brian Osman805a7272018-05-02 15:40:20 -0400486 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
487 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
488 : kPerspective_Real;
489 this->updateTitle();
490 fWindow->inval();
491 });
492 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
493 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
494 : kPerspective_Off;
495 this->updateTitle();
496 fWindow->inval();
497 });
Brian Osman207d4102019-01-10 09:40:58 -0500498 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
499 fAnimTimer.togglePauseResume();
500 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400501 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
502 fZoomUI = !fZoomUI;
503 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
504 fWindow->inval();
505 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500506
jvanverth2bb3b6d2016-04-08 07:24:09 -0700507 // set up slides
508 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500509 if (FLAGS_list) {
510 this->listNames();
511 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700512
Brian Osman9bb47cf2018-04-26 15:55:00 -0400513 fPerspectivePoints[0].set(0, 0);
514 fPerspectivePoints[1].set(1, 0);
515 fPerspectivePoints[2].set(0, 1);
516 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700517 fAnimTimer.run();
518
Hal Canaryc465d132017-12-08 10:21:31 -0500519 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500520 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400521 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500522 }
523 fImGuiGamutPaint.setColor(SK_ColorWHITE);
524 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
525
Brian Osman70d2f432017-11-08 09:54:10 -0500526 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500527 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700528}
529
jvanverth34524262016-05-04 13:49:13 -0700530void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400531 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
532 static const struct {
533 const char* fExtension;
534 const char* fDirName;
535 const SkCommandLineFlags::StringArray& fFlags;
536 const SlideFactory fFactory;
537 } gExternalSlidesInfo[] = {
538 { ".skp", "skp-dir", FLAGS_skps,
539 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
540 return sk_make_sp<SKPSlide>(name, path);}
541 },
542 { ".jpg", "jpg-dir", FLAGS_jpgs,
543 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
544 return sk_make_sp<ImageSlide>(name, path);}
545 },
Florin Malita87ccf332018-05-04 12:23:24 -0400546#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400547 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400548 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
549 return sk_make_sp<SkottieSlide>(name, path);}
550 },
Florin Malita87ccf332018-05-04 12:23:24 -0400551#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400552#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400553 { ".svg", "svg-dir", FLAGS_svgs,
554 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
555 return sk_make_sp<SvgSlide>(name, path);}
556 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400557#endif
Ruiqi Maof5101492018-06-29 14:32:21 -0400558#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
559 { ".nima", "nima-dir", FLAGS_nimas,
560 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
561 return sk_make_sp<NIMASlide>(name, path);}
562 },
563#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400564 };
jvanverthc265a922016-04-08 12:51:45 -0700565
Brian Salomon343553a2018-09-05 15:41:23 -0400566 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700567
Florin Malita0ffa3222018-04-05 14:34:45 -0400568 const auto addSlide = [&](const SkString& name,
569 const SkString& path,
570 const SlideFactory& fact) {
571 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
572 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700573 }
liyuqian6f163d22016-06-13 12:26:45 -0700574
Florin Malita0ffa3222018-04-05 14:34:45 -0400575 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500576 dirSlides.push_back(slide);
577 fSlides.push_back(std::move(slide));
578 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400579 };
Florin Malita76a076b2018-02-15 18:40:48 -0500580
Florin Malita38792ce2018-05-08 10:36:18 -0400581 if (!FLAGS_file.isEmpty()) {
582 // single file mode
583 const SkString file(FLAGS_file[0]);
584
585 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
586 for (const auto& sinfo : gExternalSlidesInfo) {
587 if (file.endsWith(sinfo.fExtension)) {
588 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
589 return;
590 }
591 }
592
593 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
594 } else {
595 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
596 }
597
598 return;
599 }
600
601 // Bisect slide.
602 if (!FLAGS_bisect.isEmpty()) {
603 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
604 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
605 if (FLAGS_bisect.count() >= 2) {
606 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
607 bisect->onChar(*ch);
608 }
609 }
610 fSlides.push_back(std::move(bisect));
611 }
612 }
613
614 // GMs
615 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400616 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
617 std::unique_ptr<skiagm::GM> gm(gmFactory(nullptr));
Florin Malita38792ce2018-05-08 10:36:18 -0400618 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
619 sk_sp<Slide> slide(new GMSlide(gm.release()));
620 fSlides.push_back(std::move(slide));
621 }
Florin Malita38792ce2018-05-08 10:36:18 -0400622 }
623 // reverse gms
624 int numGMs = fSlides.count() - firstGM;
625 for (int i = 0; i < numGMs/2; ++i) {
626 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
627 }
628
629 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400630 for (const SampleFactory factory : SampleRegistry::Range()) {
631 sk_sp<Slide> slide(new SampleSlide(factory));
Florin Malita38792ce2018-05-08 10:36:18 -0400632 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
633 fSlides.push_back(slide);
634 }
Florin Malita38792ce2018-05-08 10:36:18 -0400635 }
636
Florin Malita0ffa3222018-04-05 14:34:45 -0400637 for (const auto& info : gExternalSlidesInfo) {
638 for (const auto& flag : info.fFlags) {
639 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
640 // single file
641 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
642 } else {
643 // directory
644 SkOSFile::Iter it(flag.c_str(), info.fExtension);
645 SkString name;
646 while (it.next(&name)) {
647 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
648 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400649 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400650 if (!dirSlides.empty()) {
651 fSlides.push_back(
652 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
653 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500654 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400655 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400656 }
657 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700658}
659
660
jvanverth34524262016-05-04 13:49:13 -0700661Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700662 fWindow->detach();
663 delete fWindow;
664}
665
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500666struct SkPaintTitleUpdater {
667 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
668 void append(const char* s) {
669 if (fCount == 0) {
670 fTitle->append(" {");
671 } else {
672 fTitle->append(", ");
673 }
674 fTitle->append(s);
675 ++fCount;
676 }
677 void done() {
678 if (fCount > 0) {
679 fTitle->append("}");
680 }
681 }
682 SkString* fTitle;
683 int fCount;
684};
685
brianosman05de2162016-05-06 13:28:57 -0700686void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700687 if (!fWindow) {
688 return;
689 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500690 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700691 return; // Surface hasn't been created yet.
692 }
693
jvanverth34524262016-05-04 13:49:13 -0700694 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700695 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700696
Yuqian Li399b3c22017-08-03 11:08:15 -0400697 if (gSkUseDeltaAA) {
698 if (gSkForceDeltaAA) {
699 title.append(" <FDAA>");
700 } else {
701 title.append(" <DAA>");
702 }
703 } else if (gSkUseAnalyticAA) {
704 if (gSkForceAnalyticAA) {
705 title.append(" <FAAA>");
706 } else {
707 title.append(" <AAA>");
708 }
709 }
710
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500711 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500712 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
713 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400714 const char* on, const char* off)
715 {
Ben Wagner9613e452019-01-23 10:34:59 -0500716 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400717 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500718 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400719 };
720
Ben Wagner9613e452019-01-23 10:34:59 -0500721 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
722 const char* on, const char* off)
723 {
724 if (fFontOverrides.*flag) {
725 paintTitle.append((fFont.*isFlag)() ? on : off);
726 }
727 };
728
729 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
730 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
731
732 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
733 "Force Autohint", "No Force Autohint");
734 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
735 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
736 "Linear Metrics", "Non-Linear Metrics");
737 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
738 "Bitmap Text", "No Bitmap Text");
739 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
740
741 if (fFontOverrides.fEdging) {
742 switch (fFont.getEdging()) {
743 case SkFont::Edging::kAlias:
744 paintTitle.append("Alias Text");
745 break;
746 case SkFont::Edging::kAntiAlias:
747 paintTitle.append("Antialias Text");
748 break;
749 case SkFont::Edging::kSubpixelAntiAlias:
750 paintTitle.append("Subpixel Antialias Text");
751 break;
752 }
753 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400754
Mike Reed3ae47332019-01-04 10:11:46 -0500755 if (fFontOverrides.fHinting) {
756 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500757 case kNo_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500758 paintTitle.append("No Hinting");
759 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500760 case kSlight_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500761 paintTitle.append("Slight Hinting");
762 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500763 case kNormal_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500764 paintTitle.append("Normal Hinting");
765 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500766 case kFull_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500767 paintTitle.append("Full Hinting");
768 break;
769 }
770 }
771 paintTitle.done();
772
Brian Osman92004802017-03-06 11:47:26 -0500773 switch (fColorMode) {
774 case ColorMode::kLegacy:
775 title.append(" Legacy 8888");
776 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500777 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500778 title.append(" ColorManaged 8888");
779 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500780 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500781 title.append(" ColorManaged F16");
782 break;
783 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500784
Brian Osman92004802017-03-06 11:47:26 -0500785 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500786 int curPrimaries = -1;
787 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
788 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
789 curPrimaries = i;
790 break;
791 }
792 }
Brian Osman03115dc2018-11-26 13:55:19 -0500793 title.appendf(" %s Gamma %f",
794 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500795 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700796 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500797
Ben Wagner37c54032018-04-13 14:30:23 -0400798 const DisplayParams& params = fWindow->getRequestedDisplayParams();
799 if (fPixelGeometryOverrides) {
800 switch (params.fSurfaceProps.pixelGeometry()) {
801 case kUnknown_SkPixelGeometry:
802 title.append( " Flat");
803 break;
804 case kRGB_H_SkPixelGeometry:
805 title.append( " RGB");
806 break;
807 case kBGR_H_SkPixelGeometry:
808 title.append( " BGR");
809 break;
810 case kRGB_V_SkPixelGeometry:
811 title.append( " RGBV");
812 break;
813 case kBGR_V_SkPixelGeometry:
814 title.append( " BGRV");
815 break;
816 }
817 }
818
819 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
820 title.append(" DFT");
821 }
822
csmartdalton578f0642017-02-24 16:04:47 -0700823 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700824 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500825 int msaa = fWindow->sampleCount();
826 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700827 title.appendf(" MSAA: %i", msaa);
828 }
829 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700830
831 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman1994f202018-07-10 11:41:22 -0400832 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700833 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
834 }
835
Brian Osman805a7272018-05-02 15:40:20 -0400836 if (kPerspective_Real == fPerspectiveMode) {
837 title.append(" Perpsective (Real)");
838 } else if (kPerspective_Fake == fPerspectiveMode) {
839 title.append(" Perspective (Fake)");
840 }
841
brianosman05de2162016-05-06 13:28:57 -0700842 fWindow->setTitle(title.c_str());
843}
844
Florin Malitaab99c342018-01-16 16:23:03 -0500845int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500846
847 if (!FLAGS_slide.isEmpty()) {
848 int count = fSlides.count();
849 for (int i = 0; i < count; i++) {
850 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500851 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500852 }
853 }
854
855 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
856 this->listNames();
857 }
858
Florin Malitaab99c342018-01-16 16:23:03 -0500859 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500860}
861
Florin Malitaab99c342018-01-16 16:23:03 -0500862void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500863 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500864 for (const auto& slide : fSlides) {
865 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500866 }
867}
868
Florin Malitaab99c342018-01-16 16:23:03 -0500869void Viewer::setCurrentSlide(int slide) {
870 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700871
Florin Malitaab99c342018-01-16 16:23:03 -0500872 if (slide == fCurrentSlide) {
873 return;
874 }
875
876 if (fCurrentSlide >= 0) {
877 fSlides[fCurrentSlide]->unload();
878 }
879
880 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
881 SkIntToScalar(fWindow->height()));
882 fCurrentSlide = slide;
883 this->setupCurrentSlide();
884}
885
886void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500887 if (fCurrentSlide >= 0) {
888 // prepare dimensions for image slides
889 fGesture.resetTouchState();
890 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700891
Jim Van Verth0848fb02018-01-22 13:39:30 -0500892 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
893 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
894 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400895
Jim Van Verth0848fb02018-01-22 13:39:30 -0500896 // Start with a matrix that scales the slide to the available screen space
897 if (fWindow->scaleContentToFit()) {
898 if (windowRect.width() > 0 && windowRect.height() > 0) {
899 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
900 }
liyuqiane46e4f02016-05-20 07:32:19 -0700901 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500902
903 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400904 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500905
906 this->updateTitle();
907 this->updateUIState();
908
909 fStatsLayer.resetMeasurements();
910
911 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700912 }
jvanverthc265a922016-04-08 12:51:45 -0700913}
914
915#define MAX_ZOOM_LEVEL 8
916#define MIN_ZOOM_LEVEL -8
917
jvanverth34524262016-05-04 13:49:13 -0700918void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700919 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400920 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400921 this->preTouchMatrixChanged();
922}
Yuqian Li755778c2018-03-28 16:23:31 -0400923
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400924void Viewer::preTouchMatrixChanged() {
925 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400926 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
927 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
928 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
929 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
930}
931
Brian Osman805a7272018-05-02 15:40:20 -0400932SkMatrix Viewer::computePerspectiveMatrix() {
933 SkScalar w = fWindow->width(), h = fWindow->height();
934 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
935 SkPoint perspPts[4] = {
936 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
937 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
938 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
939 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
940 };
941 SkMatrix m;
942 m.setPolyToPoly(orthoPts, perspPts, 4);
943 return m;
944}
945
Yuqian Li755778c2018-03-28 16:23:31 -0400946SkMatrix Viewer::computePreTouchMatrix() {
947 SkMatrix m = fDefaultMatrix;
948 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
949 : SK_Scalar1 + fZoomLevel;
Ben Wagner897dfa22018-08-09 15:18:46 -0400950 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -0400951 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400952
953 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
954 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400955
Brian Osman805a7272018-05-02 15:40:20 -0400956 if (kPerspective_Real == fPerspectiveMode) {
957 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400958 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400959 }
960
Yuqian Li755778c2018-03-28 16:23:31 -0400961 return m;
jvanverthc265a922016-04-08 12:51:45 -0700962}
963
liyuqiand3cdbca2016-05-17 12:44:20 -0700964SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400965 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700966 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400967 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700968 return m;
jvanverthc265a922016-04-08 12:51:45 -0700969}
970
Brian Osman621491e2017-02-28 15:45:01 -0500971void Viewer::setBackend(sk_app::Window::BackendType backendType) {
972 fBackendType = backendType;
973
974 fWindow->detach();
975
Brian Osman70d2f432017-11-08 09:54:10 -0500976#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400977 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
978 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500979 DisplayParams params = fWindow->getRequestedDisplayParams();
980 delete fWindow;
981 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500982
Brian Osman70d2f432017-11-08 09:54:10 -0500983 // re-register callbacks
984 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500985 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500986 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500987 fWindow->pushLayer(&fImGuiLayer);
988
Brian Osman70d2f432017-11-08 09:54:10 -0500989 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
990 // will still include our correct sample count. But the re-created fWindow will lose that
991 // information. On Windows, we need to re-create the window when changing sample count,
992 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
993 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
994 // as if we had never changed windows at all).
995 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500996#endif
997
Brian Osman70d2f432017-11-08 09:54:10 -0500998 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500999}
1000
Brian Osman92004802017-03-06 11:47:26 -05001001void Viewer::setColorMode(ColorMode colorMode) {
1002 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -05001003 this->updateTitle();
1004 fWindow->inval();
1005}
1006
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001007class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
1008public:
Mike Reed3ae47332019-01-04 10:11:46 -05001009 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1010 SkFont* font, Viewer::SkFontFields* ffields)
1011 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001012 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001013 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1014 sk_sp<SkTextBlob>* cache) {
1015 bool blobWillChange = false;
1016 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001017 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1018 bool shouldDraw = this->filterFont(&filteredFont);
1019 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001020 blobWillChange = true;
1021 break;
1022 }
1023 }
1024 if (!blobWillChange) {
1025 return blob;
1026 }
1027
1028 SkTextBlobBuilder builder;
1029 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001030 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1031 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001032 if (!shouldDraw) {
1033 continue;
1034 }
1035
Mike Reed3ae47332019-01-04 10:11:46 -05001036 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001037
Ben Wagner41e40472018-09-24 13:01:54 -04001038 const SkTextBlobBuilder::RunBuffer& runBuffer
1039 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001040 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001041 it.offset().x(),it.offset().y(), it.glyphCount(), it.textSize(), SkString())
1042 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001043 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001044 it.offset().y(), it.glyphCount(), it.textSize(), SkString())
1045 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001046 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001047 it.glyphCount(), it.textSize(), SkString())
1048 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1049 uint32_t glyphCount = it.glyphCount();
1050 if (it.glyphs()) {
1051 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1052 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1053 }
1054 if (it.pos()) {
1055 size_t posSize = sizeof(decltype(*it.pos()));
1056 uint8_t positioning = it.positioning();
1057 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1058 }
1059 if (it.text()) {
1060 size_t textSize = sizeof(decltype(*it.text()));
1061 uint32_t textCount = it.textSize();
1062 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1063 }
1064 if (it.clusters()) {
1065 size_t clusterSize = sizeof(decltype(*it.clusters()));
1066 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1067 }
1068 }
1069 *cache = builder.make();
1070 return cache->get();
1071 }
1072 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1073 const SkPaint& paint) override {
1074 sk_sp<SkTextBlob> cache;
1075 this->SkPaintFilterCanvas::onDrawTextBlob(
1076 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1077 }
Mike Reed3ae47332019-01-04 10:11:46 -05001078 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
1079 if (fFontOverrides->fTextSize) {
1080 font->writable()->setSize(fFont->getSize());
1081 }
1082 if (fFontOverrides->fHinting) {
1083 font->writable()->setHinting(fFont->getHinting());
1084 }
Ben Wagner9613e452019-01-23 10:34:59 -05001085 if (fFontOverrides->fEdging) {
1086 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001087 }
Ben Wagner9613e452019-01-23 10:34:59 -05001088 if (fFontOverrides->fEmbolden) {
1089 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001090 }
Ben Wagner9613e452019-01-23 10:34:59 -05001091 if (fFontOverrides->fLinearMetrics) {
1092 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001093 }
Ben Wagner9613e452019-01-23 10:34:59 -05001094 if (fFontOverrides->fSubpixel) {
1095 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001096 }
Ben Wagner9613e452019-01-23 10:34:59 -05001097 if (fFontOverrides->fEmbeddedBitmaps) {
1098 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001099 }
Ben Wagner9613e452019-01-23 10:34:59 -05001100 if (fFontOverrides->fForceAutoHinting) {
1101 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001102 }
Ben Wagner9613e452019-01-23 10:34:59 -05001103
Mike Reed3ae47332019-01-04 10:11:46 -05001104 return true;
1105 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001106 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001107 if (*paint == nullptr) {
1108 return true;
1109 }
Ben Wagner9613e452019-01-23 10:34:59 -05001110 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001111 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1112 }
Ben Wagner9613e452019-01-23 10:34:59 -05001113 if (fPaintOverrides->fDither) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001114 paint->writable()->setDither(fPaint->isDither());
1115 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001116 return true;
1117 }
1118 SkPaint* fPaint;
1119 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001120 SkFont* fFont;
1121 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001122};
1123
Brian Osmanf750fbc2017-02-08 10:47:28 -05001124void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001125 SkAutoCanvasRestore autorestore(canvas, false);
1126
Brian Osmanf750fbc2017-02-08 10:47:28 -05001127 // By default, we render directly into the window's surface/canvas
1128 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001129 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001130
Brian Osmane0d4fba2017-03-15 10:24:55 -04001131 // 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 -05001132 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001133 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001134 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001135 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001136 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001137 }
1138
Brian Osman3ac99cf2017-12-01 11:23:53 -05001139 if (fSaveToSKP) {
1140 SkPictureRecorder recorder;
1141 SkCanvas* recorderCanvas = recorder.beginRecording(
1142 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001143 fSlides[fCurrentSlide]->draw(recorderCanvas);
1144 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1145 SkFILEWStream stream("sample_app.skp");
1146 picture->serialize(&stream);
1147 fSaveToSKP = false;
1148 }
1149
Brian Osmane9ed0f02018-11-26 14:50:05 -05001150 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
1151 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode) ? kRGBA_F16_SkColorType
1152 : kN32_SkColorType;
1153 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1154 canvas->getProps(&props);
1155
1156 auto make_surface = [=](int w, int h) {
1157 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1158 return Window::kRaster_BackendType == this->fBackendType
1159 ? SkSurface::MakeRaster(info, &props)
1160 : canvas->makeSurface(info, &props);
1161 };
1162
Brian Osman03115dc2018-11-26 13:55:19 -05001163 // We need to render offscreen if we're...
1164 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1165 // ... in any raster mode, because the window surface is actually GL
1166 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001167 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001168 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001169 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001170 Window::kRaster_BackendType == fBackendType ||
1171 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001172
Brian Osmane9ed0f02018-11-26 14:50:05 -05001173 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Mike Klein48b64902018-07-25 13:28:44 -04001174 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001175 }
1176
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001177 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001178 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001179 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001180 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001181 if (fTiled) {
1182 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1183 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1184 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1185 SkCanvas* tileCanvas = tileSurface->getCanvas();
1186 SkMatrix m = this->computeMatrix();
1187 for (int y = 0; y < fWindow->height(); y += tileH) {
1188 for (int x = 0; x < fWindow->width(); x += tileW) {
1189 SkAutoCanvasRestore acr(tileCanvas, true);
1190 tileCanvas->translate(-x, -y);
1191 tileCanvas->clear(SK_ColorTRANSPARENT);
1192 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001193 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1194 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001195 fSlides[fCurrentSlide]->draw(&filterCanvas);
1196 tileSurface->draw(slideCanvas, x, y, nullptr);
1197 }
1198 }
1199
1200 // Draw borders between tiles
1201 if (fDrawTileBoundaries) {
1202 SkPaint border;
1203 border.setColor(0x60FF00FF);
1204 border.setStyle(SkPaint::kStroke_Style);
1205 for (int y = 0; y < fWindow->height(); y += tileH) {
1206 for (int x = 0; x < fWindow->width(); x += tileW) {
1207 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1208 }
1209 }
1210 }
1211 } else {
1212 slideCanvas->concat(this->computeMatrix());
1213 if (kPerspective_Real == fPerspectiveMode) {
1214 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1215 }
Mike Reed3ae47332019-01-04 10:11:46 -05001216 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001217 fSlides[fCurrentSlide]->draw(&filterCanvas);
1218 }
Brian Osman56a24812017-12-19 11:15:16 -05001219 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001220 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001221
1222 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001223 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001224 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001225 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001226
1227 // If we rendered offscreen, snap an image and push the results to the window's canvas
1228 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001229 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001230
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001231 SkPaint paint;
1232 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001233 int prePerspectiveCount = canvas->save();
1234 if (kPerspective_Fake == fPerspectiveMode) {
1235 paint.setFilterQuality(kHigh_SkFilterQuality);
1236 canvas->clear(SK_ColorWHITE);
1237 canvas->concat(this->computePerspectiveMatrix());
1238 }
Brian Osman03115dc2018-11-26 13:55:19 -05001239 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001240 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001241 }
liyuqian6f163d22016-06-13 12:26:45 -07001242}
1243
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001244void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001245 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001246 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001247}
Jim Van Verth6f449692017-02-14 15:16:46 -05001248
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001249void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001250 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001251
brianosman622c8d52016-05-10 06:50:49 -07001252 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001253
Brian Osmand67e5182017-12-08 16:46:09 -05001254 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001255
1256 if (GrContext* ctx = fWindow->getGrContext()) {
1257 // Clean out cache items that haven't been used in more than 10 seconds.
1258 ctx->performDeferredCleanup(std::chrono::seconds(10));
1259 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001260}
1261
Ben Wagnera1915972018-08-09 15:06:19 -04001262void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001263 if (fCurrentSlide >= 0) {
1264 fSlides[fCurrentSlide]->resize(width, height);
1265 }
Ben Wagnera1915972018-08-09 15:06:19 -04001266}
1267
Florin Malitacefc1b92018-02-19 21:43:47 -05001268SkPoint Viewer::mapEvent(float x, float y) {
1269 const auto m = this->computeMatrix();
1270 SkMatrix inv;
1271
1272 SkAssertResult(m.invert(&inv));
1273
1274 return inv.mapXY(x, y);
1275}
1276
jvanverth814e38d2016-06-06 08:48:47 -07001277bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001278 if (GestureDevice::kMouse == fGestureDevice) {
1279 return false;
1280 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001281
1282 const auto slidePt = this->mapEvent(x, y);
1283 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1284 fWindow->inval();
1285 return true;
1286 }
1287
liyuqiand3cdbca2016-05-17 12:44:20 -07001288 void* castedOwner = reinterpret_cast<void*>(owner);
1289 switch (state) {
1290 case Window::kUp_InputState: {
1291 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001292#if defined(SK_BUILD_FOR_IOS)
1293 // TODO: move IOS swipe detection higher up into the platform code
1294 SkPoint dir;
1295 if (fGesture.isFling(&dir)) {
1296 // swiping left or right
1297 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1298 if (dir.fX < 0) {
1299 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1300 fCurrentSlide + 1 : 0);
1301 } else {
1302 this->setCurrentSlide(fCurrentSlide > 0 ?
1303 fCurrentSlide - 1 : fSlides.count() - 1);
1304 }
1305 }
1306 fGesture.reset();
1307 }
1308#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001309 break;
1310 }
1311 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001312 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001313 break;
1314 }
1315 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001316 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001317 break;
1318 }
1319 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001320 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001321 fWindow->inval();
1322 return true;
1323}
1324
Brian Osman80fc07e2017-12-08 16:45:43 -05001325bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001326 if (GestureDevice::kTouch == fGestureDevice) {
1327 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001328 }
Brian Osman16c81a12017-12-20 11:58:34 -05001329
Florin Malitacefc1b92018-02-19 21:43:47 -05001330 const auto slidePt = this->mapEvent(x, y);
1331 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1332 fWindow->inval();
1333 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001334 }
1335
1336 switch (state) {
1337 case Window::kUp_InputState: {
1338 fGesture.touchEnd(nullptr);
1339 break;
1340 }
1341 case Window::kDown_InputState: {
1342 fGesture.touchBegin(nullptr, x, y);
1343 break;
1344 }
1345 case Window::kMove_InputState: {
1346 fGesture.touchMoved(nullptr, x, y);
1347 break;
1348 }
1349 }
1350 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1351
1352 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1353 fWindow->inval();
1354 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001355 return true;
1356}
1357
Brian Osmana109e392017-02-24 09:49:14 -05001358static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1359 const ImVec2& pos, const ImVec2& size) {
1360 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1361 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1362
1363 // Invisible 10x10 button
1364 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1365 ImGui::InvisibleButton(label, ImVec2(10, 10));
1366
1367 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1368 ImGuiIO& io = ImGui::GetIO();
1369 // Normalized mouse position, relative to our gamut box
1370 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1371 // Clamp to edge of box, convert back to primary scale
1372 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1373 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1374 }
1375
1376 if (ImGui::IsItemHovered()) {
1377 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1378 }
1379
1380 // Return screen coordinates for the caller. We could just return center here, but we'd have
1381 // one frame of lag during drag.
1382 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1383}
1384
1385static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1386 ImDrawList* drawList = ImGui::GetWindowDrawList();
1387
1388 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1389 // width, and scale the height to maintain aspect ratio.
1390 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1391 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1392 ImVec2 pos = ImGui::GetCursorScreenPos();
1393
1394 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1395 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1396 // Magic numbers are pixel locations of the origin and upper-right corner.
1397 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1398 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001399
1400 // Primary markers
1401 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1402 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1403 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1404 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1405
1406 // Gamut triangle
1407 drawList->AddCircle(r, 5.0f, 0xFF000040);
1408 drawList->AddCircle(g, 5.0f, 0xFF004000);
1409 drawList->AddCircle(b, 5.0f, 0xFF400000);
1410 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1411 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1412
1413 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001414 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1415}
1416
1417static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1418 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1419 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1420 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1421
1422 // Invisible 10x10 button
1423 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1424 ImGui::InvisibleButton(label, ImVec2(10, 10));
1425
1426 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1427 ImGuiIO& io = ImGui::GetIO();
1428 // Normalized mouse position, relative to our gamut box
1429 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1430 // Clamp to edge of box
1431 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1432 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1433 *dragging = true;
1434 }
1435
1436 // Return screen coordinates for the caller. We could just return center here, but we'd have
1437 // one frame of lag during drag.
1438 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1439}
1440
Ben Wagner3627d2e2018-06-26 14:23:20 -04001441static bool ImGui_DragLocation(SkPoint* pt) {
1442 ImDrawList* drawList = ImGui::GetWindowDrawList();
1443
1444 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1445 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1446 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1447 ImVec2 pos = ImGui::GetCursorScreenPos();
1448
1449 // Background rectangle
1450 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1451
1452 // Location marker
1453 bool dragging = false;
1454 ImVec2 tl = ImGui_DragPoint("SL", pt + 0, pos, size, &dragging);
1455 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1456
1457 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1458 ImGui::Spacing();
1459
1460 return dragging;
1461}
1462
Brian Osman9bb47cf2018-04-26 15:55:00 -04001463static bool ImGui_DragQuad(SkPoint* pts) {
1464 ImDrawList* drawList = ImGui::GetWindowDrawList();
1465
1466 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1467 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1468 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1469 ImVec2 pos = ImGui::GetCursorScreenPos();
1470
1471 // Background rectangle
1472 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1473
1474 // Corner markers
1475 bool dragging = false;
1476 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1477 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1478 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1479 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1480
1481 // Draw markers and quad
1482 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1483 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1484 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1485 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1486 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1487 drawList->AddLine(tr, br, 0xFFFFFFFF);
1488 drawList->AddLine(br, bl, 0xFFFFFFFF);
1489 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1490
1491 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1492 ImGui::Spacing();
1493
1494 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001495}
1496
Brian Osmand67e5182017-12-08 16:46:09 -05001497void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001498 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1499 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001500 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001501 }
1502
1503 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001504 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1505 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001506 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001507 DisplayParams params = fWindow->getRequestedDisplayParams();
1508 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001509 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1510 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001511 if (ImGui::CollapsingHeader("Backend")) {
1512 int newBackend = static_cast<int>(fBackendType);
1513 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1514 ImGui::SameLine();
1515 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001516#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1517 ImGui::SameLine();
1518 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1519#endif
Brian Osman621491e2017-02-28 15:45:01 -05001520#if defined(SK_VULKAN)
1521 ImGui::SameLine();
1522 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1523#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -05001524#if defined(SK_METAL) && defined(SK_BUILD_FOR_MAC)
1525 ImGui::SameLine();
1526 ImGui::RadioButton("Metal", &newBackend, sk_app::Window::kMetal_BackendType);
1527#endif
Brian Osman621491e2017-02-28 15:45:01 -05001528 if (newBackend != fBackendType) {
1529 fDeferredActions.push_back([=]() {
1530 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1531 });
1532 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001533
Brian Salomon99a33902017-03-07 15:16:34 -05001534 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001535 bool* wire = &params.fGrContextOptions.fWireframeMode;
1536 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1537 paramsChanged = true;
1538 }
Brian Salomon99a33902017-03-07 15:16:34 -05001539
Brian Osman28b12522017-03-08 17:10:24 -05001540 if (ctx) {
1541 int sampleCount = fWindow->sampleCount();
1542 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001543 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001544 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1545 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1546 ImGui::RadioButton("16", &sampleCount, 16);
1547
1548 if (sampleCount != params.fMSAASampleCount) {
1549 params.fMSAASampleCount = sampleCount;
1550 paramsChanged = true;
1551 }
1552 }
1553
Ben Wagner37c54032018-04-13 14:30:23 -04001554 int pixelGeometryIdx = 0;
1555 if (fPixelGeometryOverrides) {
1556 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1557 }
1558 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1559 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1560 {
1561 uint32_t flags = params.fSurfaceProps.flags();
1562 if (pixelGeometryIdx == 0) {
1563 fPixelGeometryOverrides = false;
1564 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1565 } else {
1566 fPixelGeometryOverrides = true;
1567 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1568 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1569 }
1570 paramsChanged = true;
1571 }
1572
1573 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1574 if (ImGui::Checkbox("DFT", &useDFT)) {
1575 uint32_t flags = params.fSurfaceProps.flags();
1576 if (useDFT) {
1577 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1578 } else {
1579 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1580 }
1581 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1582 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1583 paramsChanged = true;
1584 }
1585
Brian Osman8a9de3d2017-03-01 14:59:05 -05001586 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001587 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001588 auto prButton = [&](GpuPathRenderers x) {
1589 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001590 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1591 params.fGrContextOptions.fGpuPathRenderers = x;
1592 paramsChanged = true;
1593 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001594 }
1595 };
1596
1597 if (!ctx) {
1598 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001599 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001600 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001601 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001602 prButton(GpuPathRenderers::kStencilAndCover);
1603 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001604 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001605 prButton(GpuPathRenderers::kNone);
1606 } else {
1607 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001608 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001609 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001610 prButton(GpuPathRenderers::kCoverageCounting);
1611 }
Jim Van Verth83010462017-03-16 08:45:39 -04001612 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001613 prButton(GpuPathRenderers::kTessellating);
1614 prButton(GpuPathRenderers::kNone);
1615 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001616 ImGui::TreePop();
1617 }
Brian Osman621491e2017-02-28 15:45:01 -05001618 }
1619
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001620 if (ImGui::CollapsingHeader("Transform")) {
1621 float zoom = fZoomLevel;
1622 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1623 fZoomLevel = zoom;
1624 this->preTouchMatrixChanged();
1625 paramsChanged = true;
1626 }
1627 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001628 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001629 fRotation = deg;
1630 this->preTouchMatrixChanged();
1631 paramsChanged = true;
1632 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001633 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1634 if (ImGui_DragLocation(&fOffset)) {
1635 this->preTouchMatrixChanged();
1636 paramsChanged = true;
1637 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001638 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1639 this->preTouchMatrixChanged();
1640 paramsChanged = true;
1641 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001642 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001643 if (ImGui::CollapsingHeader("Tiling")) {
1644 ImGui::Checkbox("Enable", &fTiled);
1645 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1646 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1647 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1648 }
Brian Osman805a7272018-05-02 15:40:20 -04001649 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1650 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1651 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001652 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001653 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001654 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001655 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001656 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001657 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001658 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001659 }
1660
Ben Wagnera580fb32018-04-17 11:16:32 -04001661 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001662 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001663 if (fPaintOverrides.fAntiAlias) {
1664 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001665 }
1666 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1667 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1668 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1669 {
1670 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1671 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1672 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1673 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1674 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001675 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1676 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001677 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001678 fPaintOverrides.fAntiAlias = true;
1679 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001680 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001681 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001682 case SkPaintFields::AntiAliasState::Alias:
1683 break;
1684 case SkPaintFields::AntiAliasState::Normal:
1685 break;
1686 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1687 gSkUseAnalyticAA = true;
1688 gSkForceAnalyticAA = false;
1689 gSkUseDeltaAA = gSkForceDeltaAA = false;
1690 break;
1691 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1692 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1693 gSkUseDeltaAA = gSkForceDeltaAA = false;
1694 break;
1695 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1696 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1697 gSkUseDeltaAA = true;
1698 gSkForceDeltaAA = false;
1699 break;
1700 case SkPaintFields::AntiAliasState::DeltaAAForced:
1701 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1702 gSkUseDeltaAA = gSkForceDeltaAA = true;
1703 break;
1704 }
1705 }
1706 paramsChanged = true;
1707 }
1708
Ben Wagner99a78dc2018-05-09 18:23:51 -04001709 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001710 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001711 bool (SkPaint::* isFlag)() const,
1712 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001713 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001714 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001715 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001716 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001717 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001718 if (ImGui::Combo(label, &itemIndex, items)) {
1719 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001720 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001721 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001722 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001723 (fPaint.*setFlag)(itemIndex == 2);
1724 }
1725 paramsChanged = true;
1726 }
1727 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001728
Ben Wagner99a78dc2018-05-09 18:23:51 -04001729 paintFlag("Dither",
1730 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001731 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001732 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagner9613e452019-01-23 10:34:59 -05001733 }
Hal Canary02738a82019-01-21 18:51:32 +00001734
Ben Wagner9613e452019-01-23 10:34:59 -05001735 if (ImGui::CollapsingHeader("Font")) {
1736 int hintingIdx = 0;
1737 if (fFontOverrides.fHinting) {
1738 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1739 }
1740 if (ImGui::Combo("Hinting", &hintingIdx,
1741 "Default\0None\0Slight\0Normal\0Full\0\0"))
1742 {
1743 if (hintingIdx == 0) {
1744 fFontOverrides.fHinting = false;
1745 fFont.setHinting(kNo_SkFontHinting);
1746 } else {
1747 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1748 fFontOverrides.fHinting = true;
1749 }
1750 paramsChanged = true;
1751 }
Hal Canary02738a82019-01-21 18:51:32 +00001752
Ben Wagner9613e452019-01-23 10:34:59 -05001753 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1754 bool SkFontFields::* flag,
1755 bool (SkFont::* isFlag)() const,
1756 void (SkFont::* setFlag)(bool) )
1757 {
1758 int itemIndex = 0;
1759 if (fFontOverrides.*flag) {
1760 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1761 }
1762 if (ImGui::Combo(label, &itemIndex, items)) {
1763 if (itemIndex == 0) {
1764 fFontOverrides.*flag = false;
1765 } else {
1766 fFontOverrides.*flag = true;
1767 (fFont.*setFlag)(itemIndex == 2);
1768 }
1769 paramsChanged = true;
1770 }
1771 };
Hal Canary02738a82019-01-21 18:51:32 +00001772
Ben Wagner9613e452019-01-23 10:34:59 -05001773 fontFlag("Fake Bold Glyphs",
1774 "Default\0No Fake Bold\0Fake Bold\0\0",
1775 &SkFontFields::fEmbolden,
1776 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001777
Ben Wagner9613e452019-01-23 10:34:59 -05001778 fontFlag("Linear Text",
1779 "Default\0No Linear Text\0Linear Text\0\0",
1780 &SkFontFields::fLinearMetrics,
1781 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001782
Ben Wagner9613e452019-01-23 10:34:59 -05001783 fontFlag("Subpixel Position Glyphs",
1784 "Default\0Pixel Text\0Subpixel Text\0\0",
1785 &SkFontFields::fSubpixel,
1786 &SkFont::isSubpixel, &SkFont::setSubpixel);
1787
1788 fontFlag("Embedded Bitmap Text",
1789 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1790 &SkFontFields::fEmbeddedBitmaps,
1791 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1792
1793 fontFlag("Force Auto-Hinting",
1794 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1795 &SkFontFields::fForceAutoHinting,
1796 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1797
1798 int edgingIdx = 0;
1799 if (fFontOverrides.fEdging) {
1800 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1801 }
1802 if (ImGui::Combo("Edging", &edgingIdx,
1803 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1804 {
1805 if (edgingIdx == 0) {
1806 fFontOverrides.fEdging = false;
1807 fFont.setEdging(SkFont::Edging::kAlias);
1808 } else {
1809 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1810 fFontOverrides.fEdging = true;
1811 }
1812 paramsChanged = true;
1813 }
1814
Mike Reed3ae47332019-01-04 10:11:46 -05001815 ImGui::Checkbox("Override TextSize", &fFontOverrides.fTextSize);
1816 if (fFontOverrides.fTextSize) {
1817 ImGui::DragFloat2("TextRange", fFontOverrides.fTextSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001818 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001819 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001820 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Mike Reed3ae47332019-01-04 10:11:46 -05001821 fFontOverrides.fTextSizeRange[0],
1822 fFontOverrides.fTextSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001823 "%.6f", 2.0f))
1824 {
Mike Reed3ae47332019-01-04 10:11:46 -05001825 fFont.setSize(textSize);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001826 this->preTouchMatrixChanged();
1827 paramsChanged = true;
1828 }
1829 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001830 }
1831
Mike Reed81f60ec2018-05-15 10:09:52 -04001832 {
1833 SkMetaData controls;
1834 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1835 if (ImGui::CollapsingHeader("Current Slide")) {
1836 SkMetaData::Iter iter(controls);
1837 const char* name;
1838 SkMetaData::Type type;
1839 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001840 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001841 if (type == SkMetaData::kScalar_Type) {
1842 float val[3];
1843 SkASSERT(count == 3);
1844 controls.findScalars(name, &count, val);
1845 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1846 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001847 }
1848 }
1849 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001850 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001851 }
1852 }
1853 }
1854
Ben Wagner7a3c6742018-04-23 10:01:07 -04001855 if (fShowSlidePicker) {
1856 ImGui::SetNextTreeNodeOpen(true);
1857 }
Brian Osman79086b92017-02-10 13:36:16 -05001858 if (ImGui::CollapsingHeader("Slide")) {
1859 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001860 static ImVector<const char*> filteredSlideNames;
1861 static ImVector<int> filteredSlideIndices;
1862
Brian Osmanfce09c52017-11-14 15:32:20 -05001863 if (fShowSlidePicker) {
1864 ImGui::SetKeyboardFocusHere();
1865 fShowSlidePicker = false;
1866 }
1867
Brian Osman79086b92017-02-10 13:36:16 -05001868 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001869 filteredSlideNames.clear();
1870 filteredSlideIndices.clear();
1871 int filteredIndex = 0;
1872 for (int i = 0; i < fSlides.count(); ++i) {
1873 const char* slideName = fSlides[i]->getName().c_str();
1874 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1875 if (i == fCurrentSlide) {
1876 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001877 }
Brian Osmanf479e422017-11-08 13:11:36 -05001878 filteredSlideNames.push_back(slideName);
1879 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001880 }
Brian Osman79086b92017-02-10 13:36:16 -05001881 }
Brian Osmanf479e422017-11-08 13:11:36 -05001882
Brian Osmanf479e422017-11-08 13:11:36 -05001883 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1884 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001885 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001886 }
1887 }
Brian Osmana109e392017-02-24 09:49:14 -05001888
1889 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001890 ColorMode newMode = fColorMode;
1891 auto cmButton = [&](ColorMode mode, const char* label) {
1892 if (ImGui::RadioButton(label, mode == fColorMode)) {
1893 newMode = mode;
1894 }
1895 };
1896
1897 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001898 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1899 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001900
1901 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001902 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001903 }
1904
1905 // Pick from common gamuts:
1906 int primariesIdx = 4; // Default: Custom
1907 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1908 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1909 primariesIdx = i;
1910 break;
1911 }
1912 }
1913
Brian Osman03115dc2018-11-26 13:55:19 -05001914 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05001915 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001916
Brian Osmana109e392017-02-24 09:49:14 -05001917 if (ImGui::Combo("Primaries", &primariesIdx,
1918 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1919 if (primariesIdx >= 0 && primariesIdx <= 3) {
1920 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1921 }
1922 }
1923
1924 // Allow direct editing of gamut
1925 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1926 }
Brian Osman207d4102019-01-10 09:40:58 -05001927
1928 if (ImGui::CollapsingHeader("Animation")) {
1929 bool isPaused = fAnimTimer.isPaused();
1930 if (ImGui::Checkbox("Pause", &isPaused)) {
1931 fAnimTimer.togglePauseResume();
1932 }
Brian Osman707d2022019-01-10 11:27:34 -05001933
1934 float speed = fAnimTimer.getSpeed();
1935 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
1936 fAnimTimer.setSpeed(speed);
1937 }
Brian Osman207d4102019-01-10 09:40:58 -05001938 }
Brian Osman79086b92017-02-10 13:36:16 -05001939 }
Brian Salomon99a33902017-03-07 15:16:34 -05001940 if (paramsChanged) {
1941 fDeferredActions.push_back([=]() {
1942 fWindow->setRequestedDisplayParams(params);
1943 fWindow->inval();
1944 this->updateTitle();
1945 });
1946 }
Brian Osman79086b92017-02-10 13:36:16 -05001947 ImGui::End();
1948 }
1949
Brian Osmanf6877092017-02-13 09:39:57 -05001950 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04001951 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
1952 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001953 static int zoomFactor = 8;
1954 if (ImGui::Button("<<")) {
1955 zoomFactor = SkTMax(zoomFactor / 2, 4);
1956 }
1957 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1958 if (ImGui::Button(">>")) {
1959 zoomFactor = SkTMin(zoomFactor * 2, 32);
1960 }
Brian Osmanf6877092017-02-13 09:39:57 -05001961
Ben Wagner3627d2e2018-06-26 14:23:20 -04001962 if (!fZoomWindowFixed) {
1963 ImVec2 mousePos = ImGui::GetMousePos();
1964 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
1965 }
1966 SkScalar x = fZoomWindowLocation.x();
1967 SkScalar y = fZoomWindowLocation.y();
1968 int xInt = SkScalarRoundToInt(x);
1969 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05001970 ImVec2 avail = ImGui::GetContentRegionAvail();
1971
Brian Osmanead517d2017-11-13 15:36:36 -05001972 uint32_t pixel = 0;
1973 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001974 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001975 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001976 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
Ben Wagner3627d2e2018-06-26 14:23:20 -04001977 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05001978 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001979 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1980 }
1981
Brian Osmand67e5182017-12-08 16:46:09 -05001982 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001983 // Translate so the region of the image that's under the mouse cursor is centered
1984 // in the zoom canvas:
1985 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001986 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
1987 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05001988 c->drawImage(this->fLastImage, 0, 0);
1989
1990 SkPaint outline;
1991 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001992 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05001993 });
Brian Osmanf6877092017-02-13 09:39:57 -05001994 }
1995
1996 ImGui::End();
1997 }
Brian Osman79086b92017-02-10 13:36:16 -05001998}
1999
liyuqian2edb0f42016-07-06 14:11:32 -07002000void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05002001 for (int i = 0; i < fDeferredActions.count(); ++i) {
2002 fDeferredActions[i]();
2003 }
2004 fDeferredActions.reset();
2005
Brian Osman56a24812017-12-19 11:15:16 -05002006 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07002007 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05002008 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05002009 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05002010
Brian Osman79086b92017-02-10 13:36:16 -05002011 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04002012 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
2013 // not be visible, though. So we need to redraw if there is at least one visible window, or
2014 // more than one active window. Newly created windows are active but not visible for one frame
2015 // while they determine their layout and sizing.
2016 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2017 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002018 fWindow->inval();
2019 }
jvanverth9f372462016-04-06 06:08:59 -07002020}
liyuqiane5a6cd92016-05-27 08:52:52 -07002021
Florin Malitab632df72018-06-18 21:23:06 -04002022template <typename OptionsFunc>
2023static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2024 OptionsFunc&& optionsFunc) {
2025 writer.beginObject();
2026 {
2027 writer.appendString(kName , name);
2028 writer.appendString(kValue, value);
2029
2030 writer.beginArray(kOptions);
2031 {
2032 optionsFunc(writer);
2033 }
2034 writer.endArray();
2035 }
2036 writer.endObject();
2037}
2038
2039
liyuqiane5a6cd92016-05-27 08:52:52 -07002040void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002041 if (!fWindow) {
2042 return;
2043 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002044 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002045 return; // Surface hasn't been created yet.
2046 }
2047
Florin Malitab632df72018-06-18 21:23:06 -04002048 SkDynamicMemoryWStream memStream;
2049 SkJSONWriter writer(&memStream);
2050 writer.beginArray();
2051
liyuqianb73c24b2016-06-03 08:47:23 -07002052 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002053 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2054 [this](SkJSONWriter& writer) {
2055 for(const auto& slide : fSlides) {
2056 writer.appendString(slide->getName().c_str());
2057 }
2058 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002059
liyuqianb73c24b2016-06-03 08:47:23 -07002060 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002061 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2062 [](SkJSONWriter& writer) {
2063 for (const auto& str : kBackendTypeStrings) {
2064 writer.appendString(str);
2065 }
2066 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002067
csmartdalton578f0642017-02-24 16:04:47 -07002068 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002069 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2070 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2071 [this](SkJSONWriter& writer) {
2072 writer.appendS32(0);
2073
2074 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2075 return;
2076 }
2077
2078 for (int msaa : {4, 8, 16}) {
2079 writer.appendS32(msaa);
2080 }
2081 });
csmartdalton578f0642017-02-24 16:04:47 -07002082
csmartdalton61cd31a2017-02-27 17:00:53 -07002083 // Path renderer state
2084 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002085 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2086 [this](SkJSONWriter& writer) {
2087 const GrContext* ctx = fWindow->getGrContext();
2088 if (!ctx) {
2089 writer.appendString("Software");
2090 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002091 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002092
Florin Malitab632df72018-06-18 21:23:06 -04002093 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2094 if (fWindow->sampleCount() > 1) {
2095 if (caps->shaderCaps()->pathRenderingSupport()) {
2096 writer.appendString(
2097 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2098 }
2099 } else {
2100 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2101 writer.appendString(
2102 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2103 }
2104 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2105 }
2106 writer.appendString(
2107 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2108 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2109 }
2110 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002111
liyuqianb73c24b2016-06-03 08:47:23 -07002112 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002113 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2114 [this](SkJSONWriter& writer) {
2115 writer.appendString(kSoftkeyHint);
2116 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2117 writer.appendString(softkey.c_str());
2118 }
2119 });
liyuqianb73c24b2016-06-03 08:47:23 -07002120
Florin Malitab632df72018-06-18 21:23:06 -04002121 writer.endArray();
2122 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002123
Florin Malitab632df72018-06-18 21:23:06 -04002124 auto data = memStream.detachAsData();
2125
2126 // TODO: would be cool to avoid this copy
2127 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2128
2129 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002130}
2131
2132void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002133 // For those who will add more features to handle the state change in this function:
2134 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2135 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2136 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002137 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002138 for (int i = 0; i < fSlides.count(); ++i) {
2139 if (fSlides[i]->getName().equals(stateValue)) {
2140 this->setCurrentSlide(i);
2141 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002142 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002143 }
Florin Malitaab99c342018-01-16 16:23:03 -05002144
2145 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002146 } else if (stateName.equals(kBackendStateName)) {
2147 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2148 if (stateValue.equals(kBackendTypeStrings[i])) {
2149 if (fBackendType != i) {
2150 fBackendType = (sk_app::Window::BackendType)i;
2151 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002152 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002153 }
2154 break;
2155 }
2156 }
csmartdalton578f0642017-02-24 16:04:47 -07002157 } else if (stateName.equals(kMSAAStateName)) {
2158 DisplayParams params = fWindow->getRequestedDisplayParams();
2159 int sampleCount = atoi(stateValue.c_str());
2160 if (sampleCount != params.fMSAASampleCount) {
2161 params.fMSAASampleCount = sampleCount;
2162 fWindow->setRequestedDisplayParams(params);
2163 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002164 this->updateTitle();
2165 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002166 }
2167 } else if (stateName.equals(kPathRendererStateName)) {
2168 DisplayParams params = fWindow->getRequestedDisplayParams();
2169 for (const auto& pair : gPathRendererNames) {
2170 if (pair.second == stateValue.c_str()) {
2171 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2172 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2173 fWindow->setRequestedDisplayParams(params);
2174 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002175 this->updateTitle();
2176 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002177 }
2178 break;
2179 }
csmartdalton578f0642017-02-24 16:04:47 -07002180 }
liyuqianb73c24b2016-06-03 08:47:23 -07002181 } else if (stateName.equals(kSoftkeyStateName)) {
2182 if (!stateValue.equals(kSoftkeyHint)) {
2183 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002184 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002185 }
liyuqian2edb0f42016-07-06 14:11:32 -07002186 } else if (stateName.equals(kRefreshStateName)) {
2187 // This state is actually NOT in the UI state.
2188 // We use this to allow Android to quickly set bool fRefresh.
2189 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002190 } else {
2191 SkDebugf("Unknown stateName: %s", stateName.c_str());
2192 }
2193}
Brian Osman79086b92017-02-10 13:36:16 -05002194
2195bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002196 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002197}
2198
2199bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002200 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002201 fWindow->inval();
2202 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002203 } else {
2204 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002205 }
Brian Osman79086b92017-02-10 13:36:16 -05002206}