blob: 75730caff862dcb48e8446ccc55c7af60e5644d5 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Chris Dalton2d18f412018-02-20 13:23:32 -07008#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -07009#include "GMSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040010#include "GrContext.h"
11#include "GrContextPriv.h"
liyuqian6f163d22016-06-13 12:26:45 -070012#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050013#include "Resources.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070014#include "SKPSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040015#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070016#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050017#include "SkColorSpacePriv.h"
Brian Osman2dd96932016-10-18 15:33:53 -040018#include "SkCommandLineFlags.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040019#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070020#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040021#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050022#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040023#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050024#include "SkImagePriv.h"
Florin Malitab632df72018-06-18 21:23:06 -040025#include "SkJSONWriter.h"
Herb Derbyefe39bc2018-05-01 17:06:20 -040026#include "SkMakeUnique.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070027#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050028#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050029#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050030#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040031#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070032#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070033#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050034#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050035#include "SkTestFontMgr.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040036#include "SkTo.h"
Hal Canary8a001442018-09-19 11:31:27 -040037#include "SlideDir.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040038#include "SvgSlide.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040039#include "Viewer.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060040#include "ccpr/GrCoverageCountingPathRenderer.h"
csmartdalton578f0642017-02-24 16:04:47 -070041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <stdlib.h>
43#include <map>
44
Hal Canary8a001442018-09-19 11:31:27 -040045#include "imgui.h"
Florin Malita3b526b02018-05-25 12:43:51 -040046
Florin Malita87ccf332018-05-04 12:23:24 -040047#if defined(SK_ENABLE_SKOTTIE)
48 #include "SkottieSlide.h"
49#endif
50
Ruiqi Maof5101492018-06-29 14:32:21 -040051#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
52 #include "NIMASlide.h"
53#endif
54
jvanverth34524262016-05-04 13:49:13 -070055using namespace sk_app;
56
csmartdalton61cd31a2017-02-27 17:00:53 -070057static std::map<GpuPathRenderers, std::string> gPathRendererNames;
58
jvanverth9f372462016-04-06 06:08:59 -070059Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070060 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070061}
62
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060063static DEFINE_string(slide, "", "Start on this sample.");
64static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050065
bsalomon6c471f72016-07-26 12:56:32 -070066#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070067# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070068#else
69# define BACKENDS_STR "\"sw\" and \"gl\""
70#endif
71
Brian Osman2dd96932016-10-18 15:33:53 -040072static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070073
Brian Salomonbdecacf2018-02-02 20:32:49 -050074static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070075
Chris Dalton2d18f412018-02-20 13:23:32 -070076DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
77
Brian Osman53136aa2017-07-20 15:43:35 -040078DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050079
Florin Malita38792ce2018-05-08 10:36:18 -040080DEFINE_string2(file, f, "", "Open a single file for viewing.");
81
Brian Salomon194db172017-08-17 14:37:06 -040082const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070083 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040084#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
85 "ANGLE",
86#endif
jvanverth063ece72016-06-17 09:29:14 -070087#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070088 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070089#endif
csmartdalton578f0642017-02-24 16:04:47 -070090 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070091};
92
bsalomon6c471f72016-07-26 12:56:32 -070093static sk_app::Window::BackendType get_backend_type(const char* str) {
94#ifdef SK_VULKAN
95 if (0 == strcmp(str, "vk")) {
96 return sk_app::Window::kVulkan_BackendType;
97 } else
98#endif
Brian Salomon194db172017-08-17 14:37:06 -040099#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
100 if (0 == strcmp(str, "angle")) {
101 return sk_app::Window::kANGLE_BackendType;
102 } else
103#endif
bsalomon6c471f72016-07-26 12:56:32 -0700104 if (0 == strcmp(str, "gl")) {
105 return sk_app::Window::kNativeGL_BackendType;
106 } else if (0 == strcmp(str, "sw")) {
107 return sk_app::Window::kRaster_BackendType;
108 } else {
109 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
110 return sk_app::Window::kRaster_BackendType;
111 }
112}
113
Brian Osmana109e392017-02-24 09:49:14 -0500114static SkColorSpacePrimaries gSrgbPrimaries = {
115 0.64f, 0.33f,
116 0.30f, 0.60f,
117 0.15f, 0.06f,
118 0.3127f, 0.3290f };
119
120static SkColorSpacePrimaries gAdobePrimaries = {
121 0.64f, 0.33f,
122 0.21f, 0.71f,
123 0.15f, 0.06f,
124 0.3127f, 0.3290f };
125
126static SkColorSpacePrimaries gP3Primaries = {
127 0.680f, 0.320f,
128 0.265f, 0.690f,
129 0.150f, 0.060f,
130 0.3127f, 0.3290f };
131
132static SkColorSpacePrimaries gRec2020Primaries = {
133 0.708f, 0.292f,
134 0.170f, 0.797f,
135 0.131f, 0.046f,
136 0.3127f, 0.3290f };
137
138struct NamedPrimaries {
139 const char* fName;
140 SkColorSpacePrimaries* fPrimaries;
141} gNamedPrimaries[] = {
142 { "sRGB", &gSrgbPrimaries },
143 { "AdobeRGB", &gAdobePrimaries },
144 { "P3", &gP3Primaries },
145 { "Rec. 2020", &gRec2020Primaries },
146};
147
148static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
149 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
150}
151
Brian Osman70d2f432017-11-08 09:54:10 -0500152static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
153 // In raster mode, we still use GL for the window.
154 // This lets us render the GUI faster (and correct).
155 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
156}
157
liyuqiane5a6cd92016-05-27 08:52:52 -0700158const char* kName = "name";
159const char* kValue = "value";
160const char* kOptions = "options";
161const char* kSlideStateName = "Slide";
162const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700163const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700164const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700165const char* kSoftkeyStateName = "Softkey";
166const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700167const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700168const char* kON = "ON";
169const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700170const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700171
jvanverth34524262016-05-04 13:49:13 -0700172Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500173 : fCurrentSlide(-1)
174 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500175 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500176 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500177 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500178 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500179 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400180 , fZoomWindowFixed(false)
181 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500182 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400183 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700184 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500185 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500186 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500187 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500188 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700189 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400190 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400191 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400192 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500193 , fTiled(false)
194 , fDrawTileBoundaries(false)
195 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400196 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700197{
Greg Daniel285db442016-10-14 09:12:53 -0400198 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700199
Brian Osmanf09e35e2017-12-15 14:48:09 -0500200 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500201 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500202 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
203 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
204 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
205 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700206
jvanverth2bb3b6d2016-04-08 07:24:09 -0700207 SkDebugf("Command line arguments: ");
208 for (int i = 1; i < argc; ++i) {
209 SkDebugf("%s ", argv[i]);
210 }
211 SkDebugf("\n");
212
213 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500214#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400215 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500216#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700217
Ben Wagner483c7722018-02-20 17:06:07 -0500218 if (!FLAGS_nativeFonts) {
219 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
220 }
221
Brian Osmanbc8150f2017-07-24 11:38:01 -0400222 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400223 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400224
bsalomon6c471f72016-07-26 12:56:32 -0700225 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700226 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700227
csmartdalton578f0642017-02-24 16:04:47 -0700228 DisplayParams displayParams;
229 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700230 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700231 fWindow->setRequestedDisplayParams(displayParams);
232
Brian Osman56a24812017-12-19 11:15:16 -0500233 // Configure timers
234 fStatsLayer.setActive(false);
235 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
236 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
237 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
238
jvanverth9f372462016-04-06 06:08:59 -0700239 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700240 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500241 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500242 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500243 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700244
brianosman622c8d52016-05-10 06:50:49 -0700245 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500246 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
247 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
248 fWindow->inval();
249 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500250 // Command to jump directly to the slide picker and give it focus
251 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
252 this->fShowImGuiDebugWindow = true;
253 this->fShowSlidePicker = true;
254 fWindow->inval();
255 });
256 // Alias that to Backspace, to match SampleApp
257 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
258 this->fShowImGuiDebugWindow = true;
259 this->fShowSlidePicker = true;
260 fWindow->inval();
261 });
Brian Osman79086b92017-02-10 13:36:16 -0500262 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
263 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
264 fWindow->inval();
265 });
Brian Osmanf6877092017-02-13 09:39:57 -0500266 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
267 this->fShowZoomWindow = !this->fShowZoomWindow;
268 fWindow->inval();
269 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400270 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
271 this->fZoomWindowFixed = !this->fZoomWindowFixed;
272 fWindow->inval();
273 });
brianosman622c8d52016-05-10 06:50:49 -0700274 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500275 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700276 fWindow->inval();
277 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400278 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500279 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400280 this->updateTitle();
281 fWindow->inval();
282 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500283 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500284 switch (fColorMode) {
285 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500286 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500287 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500288 case ColorMode::kColorManaged8888:
289 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500290 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500291 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500292 this->setColorMode(ColorMode::kLegacy);
293 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500294 }
brianosman622c8d52016-05-10 06:50:49 -0700295 });
296 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500297 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700298 });
299 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500300 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700301 });
302 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
303 this->changeZoomLevel(1.f / 32.f);
304 fWindow->inval();
305 });
306 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
307 this->changeZoomLevel(-1.f / 32.f);
308 fWindow->inval();
309 });
jvanverthaf236b52016-05-20 06:01:06 -0700310 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400311 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
312 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500313 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400314#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
315 if (newBackend == sk_app::Window::kVulkan_BackendType) {
316 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
317 sk_app::Window::kBackendTypeCount);
318 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
319 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500320 }
321#endif
Brian Osman621491e2017-02-28 15:45:01 -0500322 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700323 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500324 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
325 fSaveToSKP = true;
326 fWindow->inval();
327 });
Ben Wagner37c54032018-04-13 14:30:23 -0400328 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
329 DisplayParams params = fWindow->getRequestedDisplayParams();
330 uint32_t flags = params.fSurfaceProps.flags();
331 if (!fPixelGeometryOverrides) {
332 fPixelGeometryOverrides = true;
333 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
334 } else {
335 switch (params.fSurfaceProps.pixelGeometry()) {
336 case kUnknown_SkPixelGeometry:
337 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
338 break;
339 case kRGB_H_SkPixelGeometry:
340 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
341 break;
342 case kBGR_H_SkPixelGeometry:
343 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
344 break;
345 case kRGB_V_SkPixelGeometry:
346 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
347 break;
348 case kBGR_V_SkPixelGeometry:
349 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
350 fPixelGeometryOverrides = false;
351 break;
352 }
353 }
354 fWindow->setRequestedDisplayParams(params);
355 this->updateTitle();
356 fWindow->inval();
357 });
Ben Wagner9613e452019-01-23 10:34:59 -0500358 fCommands.addCommand('H', "Font", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500359 if (!fFontOverrides.fHinting) {
360 fFontOverrides.fHinting = true;
361 fFont.setHinting(kNo_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500362 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500363 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500364 case kNo_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500365 fFont.setHinting(kSlight_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500366 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500367 case kSlight_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500368 fFont.setHinting(kNormal_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500369 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500370 case kNormal_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500371 fFont.setHinting(kFull_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500372 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500373 case kFull_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500374 fFont.setHinting(kNo_SkFontHinting);
375 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500376 break;
377 }
378 }
379 this->updateTitle();
380 fWindow->inval();
381 });
382 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Ben Wagner9613e452019-01-23 10:34:59 -0500383 if (!fPaintOverrides.fAntiAlias) {
384 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
385 fPaintOverrides.fAntiAlias = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500386 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500387 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
388 gSkUseDeltaAA = gSkForceDeltaAA = false;
389 } else {
390 fPaint.setAntiAlias(true);
Ben Wagner9613e452019-01-23 10:34:59 -0500391 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500392 case SkPaintFields::AntiAliasState::Alias:
Ben Wagner9613e452019-01-23 10:34:59 -0500393 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400394 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
395 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500396 break;
397 case SkPaintFields::AntiAliasState::Normal:
Ben Wagner9613e452019-01-23 10:34:59 -0500398 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500399 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400400 gSkForceAnalyticAA = false;
401 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500402 break;
403 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500404 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400405 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
406 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500407 break;
408 case SkPaintFields::AntiAliasState::AnalyticAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500409 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::DeltaAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500410 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
411 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400412 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500413 break;
414 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
Ben Wagner9613e452019-01-23 10:34:59 -0500415 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400416 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
417 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500418 break;
419 case SkPaintFields::AntiAliasState::DeltaAAForced:
Ben Wagner9613e452019-01-23 10:34:59 -0500420 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
421 fPaintOverrides.fAntiAlias = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500422 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
423 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
424 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
425 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
426 break;
427 }
428 }
429 this->updateTitle();
430 fWindow->inval();
431 });
Ben Wagner37c54032018-04-13 14:30:23 -0400432 fCommands.addCommand('D', "Modes", "DFT", [this]() {
433 DisplayParams params = fWindow->getRequestedDisplayParams();
434 uint32_t flags = params.fSurfaceProps.flags();
435 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
436 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
437 fWindow->setRequestedDisplayParams(params);
438 this->updateTitle();
439 fWindow->inval();
440 });
Ben Wagner9613e452019-01-23 10:34:59 -0500441 fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500442 if (!fFontOverrides.fEdging) {
443 fFontOverrides.fEdging = true;
444 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500445 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500446 switch (fFont.getEdging()) {
447 case SkFont::Edging::kAlias:
448 fFont.setEdging(SkFont::Edging::kAntiAlias);
449 break;
450 case SkFont::Edging::kAntiAlias:
451 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
452 break;
453 case SkFont::Edging::kSubpixelAntiAlias:
454 fFont.setEdging(SkFont::Edging::kAlias);
455 fFontOverrides.fEdging = false;
456 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500457 }
458 }
459 this->updateTitle();
460 fWindow->inval();
461 });
Ben Wagner9613e452019-01-23 10:34:59 -0500462 fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500463 if (!fFontOverrides.fSubpixel) {
464 fFontOverrides.fSubpixel = true;
465 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500466 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500467 if (!fFont.isSubpixel()) {
468 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500469 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500470 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500471 }
472 }
473 this->updateTitle();
474 fWindow->inval();
475 });
Brian Osman805a7272018-05-02 15:40:20 -0400476 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
477 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
478 : kPerspective_Real;
479 this->updateTitle();
480 fWindow->inval();
481 });
482 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
483 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
484 : kPerspective_Off;
485 this->updateTitle();
486 fWindow->inval();
487 });
Brian Osman207d4102019-01-10 09:40:58 -0500488 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
489 fAnimTimer.togglePauseResume();
490 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400491 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
492 fZoomUI = !fZoomUI;
493 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
494 fWindow->inval();
495 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500496
jvanverth2bb3b6d2016-04-08 07:24:09 -0700497 // set up slides
498 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500499 if (FLAGS_list) {
500 this->listNames();
501 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700502
Brian Osman9bb47cf2018-04-26 15:55:00 -0400503 fPerspectivePoints[0].set(0, 0);
504 fPerspectivePoints[1].set(1, 0);
505 fPerspectivePoints[2].set(0, 1);
506 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700507 fAnimTimer.run();
508
Hal Canaryc465d132017-12-08 10:21:31 -0500509 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500510 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400511 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500512 }
513 fImGuiGamutPaint.setColor(SK_ColorWHITE);
514 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
515
Brian Osman70d2f432017-11-08 09:54:10 -0500516 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500517 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700518}
519
jvanverth34524262016-05-04 13:49:13 -0700520void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400521 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
522 static const struct {
523 const char* fExtension;
524 const char* fDirName;
525 const SkCommandLineFlags::StringArray& fFlags;
526 const SlideFactory fFactory;
527 } gExternalSlidesInfo[] = {
528 { ".skp", "skp-dir", FLAGS_skps,
529 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
530 return sk_make_sp<SKPSlide>(name, path);}
531 },
532 { ".jpg", "jpg-dir", FLAGS_jpgs,
533 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
534 return sk_make_sp<ImageSlide>(name, path);}
535 },
Florin Malita87ccf332018-05-04 12:23:24 -0400536#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400537 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400538 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
539 return sk_make_sp<SkottieSlide>(name, path);}
540 },
Florin Malita87ccf332018-05-04 12:23:24 -0400541#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400542#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400543 { ".svg", "svg-dir", FLAGS_svgs,
544 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
545 return sk_make_sp<SvgSlide>(name, path);}
546 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400547#endif
Ruiqi Maof5101492018-06-29 14:32:21 -0400548#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
549 { ".nima", "nima-dir", FLAGS_nimas,
550 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
551 return sk_make_sp<NIMASlide>(name, path);}
552 },
553#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400554 };
jvanverthc265a922016-04-08 12:51:45 -0700555
Brian Salomon343553a2018-09-05 15:41:23 -0400556 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700557
Florin Malita0ffa3222018-04-05 14:34:45 -0400558 const auto addSlide = [&](const SkString& name,
559 const SkString& path,
560 const SlideFactory& fact) {
561 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
562 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700563 }
liyuqian6f163d22016-06-13 12:26:45 -0700564
Florin Malita0ffa3222018-04-05 14:34:45 -0400565 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500566 dirSlides.push_back(slide);
567 fSlides.push_back(std::move(slide));
568 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400569 };
Florin Malita76a076b2018-02-15 18:40:48 -0500570
Florin Malita38792ce2018-05-08 10:36:18 -0400571 if (!FLAGS_file.isEmpty()) {
572 // single file mode
573 const SkString file(FLAGS_file[0]);
574
575 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
576 for (const auto& sinfo : gExternalSlidesInfo) {
577 if (file.endsWith(sinfo.fExtension)) {
578 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
579 return;
580 }
581 }
582
583 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
584 } else {
585 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
586 }
587
588 return;
589 }
590
591 // Bisect slide.
592 if (!FLAGS_bisect.isEmpty()) {
593 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
594 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
595 if (FLAGS_bisect.count() >= 2) {
596 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
597 bisect->onChar(*ch);
598 }
599 }
600 fSlides.push_back(std::move(bisect));
601 }
602 }
603
604 // GMs
605 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400606 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
607 std::unique_ptr<skiagm::GM> gm(gmFactory(nullptr));
Florin Malita38792ce2018-05-08 10:36:18 -0400608 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
609 sk_sp<Slide> slide(new GMSlide(gm.release()));
610 fSlides.push_back(std::move(slide));
611 }
Florin Malita38792ce2018-05-08 10:36:18 -0400612 }
613 // reverse gms
614 int numGMs = fSlides.count() - firstGM;
615 for (int i = 0; i < numGMs/2; ++i) {
616 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
617 }
618
619 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400620 for (const SampleFactory factory : SampleRegistry::Range()) {
621 sk_sp<Slide> slide(new SampleSlide(factory));
Florin Malita38792ce2018-05-08 10:36:18 -0400622 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
623 fSlides.push_back(slide);
624 }
Florin Malita38792ce2018-05-08 10:36:18 -0400625 }
626
Florin Malita0ffa3222018-04-05 14:34:45 -0400627 for (const auto& info : gExternalSlidesInfo) {
628 for (const auto& flag : info.fFlags) {
629 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
630 // single file
631 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
632 } else {
633 // directory
634 SkOSFile::Iter it(flag.c_str(), info.fExtension);
635 SkString name;
636 while (it.next(&name)) {
637 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
638 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400639 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400640 if (!dirSlides.empty()) {
641 fSlides.push_back(
642 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
643 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500644 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400645 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400646 }
647 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700648}
649
650
jvanverth34524262016-05-04 13:49:13 -0700651Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700652 fWindow->detach();
653 delete fWindow;
654}
655
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500656struct SkPaintTitleUpdater {
657 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
658 void append(const char* s) {
659 if (fCount == 0) {
660 fTitle->append(" {");
661 } else {
662 fTitle->append(", ");
663 }
664 fTitle->append(s);
665 ++fCount;
666 }
667 void done() {
668 if (fCount > 0) {
669 fTitle->append("}");
670 }
671 }
672 SkString* fTitle;
673 int fCount;
674};
675
brianosman05de2162016-05-06 13:28:57 -0700676void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700677 if (!fWindow) {
678 return;
679 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500680 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700681 return; // Surface hasn't been created yet.
682 }
683
jvanverth34524262016-05-04 13:49:13 -0700684 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700685 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700686
Yuqian Li399b3c22017-08-03 11:08:15 -0400687 if (gSkUseDeltaAA) {
688 if (gSkForceDeltaAA) {
689 title.append(" <FDAA>");
690 } else {
691 title.append(" <DAA>");
692 }
693 } else if (gSkUseAnalyticAA) {
694 if (gSkForceAnalyticAA) {
695 title.append(" <FAAA>");
696 } else {
697 title.append(" <AAA>");
698 }
699 }
700
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500701 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner9613e452019-01-23 10:34:59 -0500702 auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag,
703 bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400704 const char* on, const char* off)
705 {
Ben Wagner9613e452019-01-23 10:34:59 -0500706 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -0400707 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500708 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400709 };
710
Ben Wagner9613e452019-01-23 10:34:59 -0500711 auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const,
712 const char* on, const char* off)
713 {
714 if (fFontOverrides.*flag) {
715 paintTitle.append((fFont.*isFlag)() ? on : off);
716 }
717 };
718
719 paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias");
720 paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither");
721
722 fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting,
723 "Force Autohint", "No Force Autohint");
724 fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold");
725 fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics,
726 "Linear Metrics", "Non-Linear Metrics");
727 fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps,
728 "Bitmap Text", "No Bitmap Text");
729 fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text");
730
731 if (fFontOverrides.fEdging) {
732 switch (fFont.getEdging()) {
733 case SkFont::Edging::kAlias:
734 paintTitle.append("Alias Text");
735 break;
736 case SkFont::Edging::kAntiAlias:
737 paintTitle.append("Antialias Text");
738 break;
739 case SkFont::Edging::kSubpixelAntiAlias:
740 paintTitle.append("Subpixel Antialias Text");
741 break;
742 }
743 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400744
Mike Reed3ae47332019-01-04 10:11:46 -0500745 if (fFontOverrides.fHinting) {
746 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500747 case kNo_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500748 paintTitle.append("No Hinting");
749 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500750 case kSlight_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500751 paintTitle.append("Slight Hinting");
752 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500753 case kNormal_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500754 paintTitle.append("Normal Hinting");
755 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500756 case kFull_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500757 paintTitle.append("Full Hinting");
758 break;
759 }
760 }
761 paintTitle.done();
762
Brian Osman92004802017-03-06 11:47:26 -0500763 switch (fColorMode) {
764 case ColorMode::kLegacy:
765 title.append(" Legacy 8888");
766 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500767 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500768 title.append(" ColorManaged 8888");
769 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500770 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500771 title.append(" ColorManaged F16");
772 break;
773 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500774
Brian Osman92004802017-03-06 11:47:26 -0500775 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500776 int curPrimaries = -1;
777 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
778 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
779 curPrimaries = i;
780 break;
781 }
782 }
Brian Osman03115dc2018-11-26 13:55:19 -0500783 title.appendf(" %s Gamma %f",
784 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500785 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700786 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500787
Ben Wagner37c54032018-04-13 14:30:23 -0400788 const DisplayParams& params = fWindow->getRequestedDisplayParams();
789 if (fPixelGeometryOverrides) {
790 switch (params.fSurfaceProps.pixelGeometry()) {
791 case kUnknown_SkPixelGeometry:
792 title.append( " Flat");
793 break;
794 case kRGB_H_SkPixelGeometry:
795 title.append( " RGB");
796 break;
797 case kBGR_H_SkPixelGeometry:
798 title.append( " BGR");
799 break;
800 case kRGB_V_SkPixelGeometry:
801 title.append( " RGBV");
802 break;
803 case kBGR_V_SkPixelGeometry:
804 title.append( " BGRV");
805 break;
806 }
807 }
808
809 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
810 title.append(" DFT");
811 }
812
csmartdalton578f0642017-02-24 16:04:47 -0700813 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700814 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500815 int msaa = fWindow->sampleCount();
816 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700817 title.appendf(" MSAA: %i", msaa);
818 }
819 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700820
821 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman1994f202018-07-10 11:41:22 -0400822 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700823 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
824 }
825
Brian Osman805a7272018-05-02 15:40:20 -0400826 if (kPerspective_Real == fPerspectiveMode) {
827 title.append(" Perpsective (Real)");
828 } else if (kPerspective_Fake == fPerspectiveMode) {
829 title.append(" Perspective (Fake)");
830 }
831
brianosman05de2162016-05-06 13:28:57 -0700832 fWindow->setTitle(title.c_str());
833}
834
Florin Malitaab99c342018-01-16 16:23:03 -0500835int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500836
837 if (!FLAGS_slide.isEmpty()) {
838 int count = fSlides.count();
839 for (int i = 0; i < count; i++) {
840 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500841 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500842 }
843 }
844
845 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
846 this->listNames();
847 }
848
Florin Malitaab99c342018-01-16 16:23:03 -0500849 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500850}
851
Florin Malitaab99c342018-01-16 16:23:03 -0500852void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500853 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500854 for (const auto& slide : fSlides) {
855 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500856 }
857}
858
Florin Malitaab99c342018-01-16 16:23:03 -0500859void Viewer::setCurrentSlide(int slide) {
860 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700861
Florin Malitaab99c342018-01-16 16:23:03 -0500862 if (slide == fCurrentSlide) {
863 return;
864 }
865
866 if (fCurrentSlide >= 0) {
867 fSlides[fCurrentSlide]->unload();
868 }
869
870 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
871 SkIntToScalar(fWindow->height()));
872 fCurrentSlide = slide;
873 this->setupCurrentSlide();
874}
875
876void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500877 if (fCurrentSlide >= 0) {
878 // prepare dimensions for image slides
879 fGesture.resetTouchState();
880 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700881
Jim Van Verth0848fb02018-01-22 13:39:30 -0500882 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
883 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
884 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400885
Jim Van Verth0848fb02018-01-22 13:39:30 -0500886 // Start with a matrix that scales the slide to the available screen space
887 if (fWindow->scaleContentToFit()) {
888 if (windowRect.width() > 0 && windowRect.height() > 0) {
889 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
890 }
liyuqiane46e4f02016-05-20 07:32:19 -0700891 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500892
893 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400894 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500895
896 this->updateTitle();
897 this->updateUIState();
898
899 fStatsLayer.resetMeasurements();
900
901 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700902 }
jvanverthc265a922016-04-08 12:51:45 -0700903}
904
905#define MAX_ZOOM_LEVEL 8
906#define MIN_ZOOM_LEVEL -8
907
jvanverth34524262016-05-04 13:49:13 -0700908void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700909 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400910 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400911 this->preTouchMatrixChanged();
912}
Yuqian Li755778c2018-03-28 16:23:31 -0400913
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400914void Viewer::preTouchMatrixChanged() {
915 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400916 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
917 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
918 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
919 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
920}
921
Brian Osman805a7272018-05-02 15:40:20 -0400922SkMatrix Viewer::computePerspectiveMatrix() {
923 SkScalar w = fWindow->width(), h = fWindow->height();
924 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
925 SkPoint perspPts[4] = {
926 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
927 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
928 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
929 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
930 };
931 SkMatrix m;
932 m.setPolyToPoly(orthoPts, perspPts, 4);
933 return m;
934}
935
Yuqian Li755778c2018-03-28 16:23:31 -0400936SkMatrix Viewer::computePreTouchMatrix() {
937 SkMatrix m = fDefaultMatrix;
938 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
939 : SK_Scalar1 + fZoomLevel;
Ben Wagner897dfa22018-08-09 15:18:46 -0400940 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -0400941 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400942
943 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
944 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400945
Brian Osman805a7272018-05-02 15:40:20 -0400946 if (kPerspective_Real == fPerspectiveMode) {
947 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400948 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400949 }
950
Yuqian Li755778c2018-03-28 16:23:31 -0400951 return m;
jvanverthc265a922016-04-08 12:51:45 -0700952}
953
liyuqiand3cdbca2016-05-17 12:44:20 -0700954SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400955 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700956 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400957 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700958 return m;
jvanverthc265a922016-04-08 12:51:45 -0700959}
960
Brian Osman621491e2017-02-28 15:45:01 -0500961void Viewer::setBackend(sk_app::Window::BackendType backendType) {
962 fBackendType = backendType;
963
964 fWindow->detach();
965
Brian Osman70d2f432017-11-08 09:54:10 -0500966#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400967 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
968 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500969 DisplayParams params = fWindow->getRequestedDisplayParams();
970 delete fWindow;
971 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500972
Brian Osman70d2f432017-11-08 09:54:10 -0500973 // re-register callbacks
974 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500975 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500976 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500977 fWindow->pushLayer(&fImGuiLayer);
978
Brian Osman70d2f432017-11-08 09:54:10 -0500979 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
980 // will still include our correct sample count. But the re-created fWindow will lose that
981 // information. On Windows, we need to re-create the window when changing sample count,
982 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
983 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
984 // as if we had never changed windows at all).
985 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500986#endif
987
Brian Osman70d2f432017-11-08 09:54:10 -0500988 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500989}
990
Brian Osman92004802017-03-06 11:47:26 -0500991void Viewer::setColorMode(ColorMode colorMode) {
992 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500993 this->updateTitle();
994 fWindow->inval();
995}
996
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500997class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
998public:
Mike Reed3ae47332019-01-04 10:11:46 -0500999 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
1000 SkFont* font, Viewer::SkFontFields* ffields)
1001 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001002 { }
Ben Wagner41e40472018-09-24 13:01:54 -04001003 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
1004 sk_sp<SkTextBlob>* cache) {
1005 bool blobWillChange = false;
1006 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001007 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1008 bool shouldDraw = this->filterFont(&filteredFont);
1009 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -04001010 blobWillChange = true;
1011 break;
1012 }
1013 }
1014 if (!blobWillChange) {
1015 return blob;
1016 }
1017
1018 SkTextBlobBuilder builder;
1019 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -05001020 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
1021 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -04001022 if (!shouldDraw) {
1023 continue;
1024 }
1025
Mike Reed3ae47332019-01-04 10:11:46 -05001026 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001027
Ben Wagner41e40472018-09-24 13:01:54 -04001028 const SkTextBlobBuilder::RunBuffer& runBuffer
1029 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001030 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001031 it.offset().x(),it.offset().y(), it.glyphCount(), it.textSize(), SkString())
1032 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001033 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001034 it.offset().y(), it.glyphCount(), it.textSize(), SkString())
1035 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001036 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001037 it.glyphCount(), it.textSize(), SkString())
1038 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1039 uint32_t glyphCount = it.glyphCount();
1040 if (it.glyphs()) {
1041 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1042 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1043 }
1044 if (it.pos()) {
1045 size_t posSize = sizeof(decltype(*it.pos()));
1046 uint8_t positioning = it.positioning();
1047 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1048 }
1049 if (it.text()) {
1050 size_t textSize = sizeof(decltype(*it.text()));
1051 uint32_t textCount = it.textSize();
1052 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1053 }
1054 if (it.clusters()) {
1055 size_t clusterSize = sizeof(decltype(*it.clusters()));
1056 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1057 }
1058 }
1059 *cache = builder.make();
1060 return cache->get();
1061 }
1062 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1063 const SkPaint& paint) override {
1064 sk_sp<SkTextBlob> cache;
1065 this->SkPaintFilterCanvas::onDrawTextBlob(
1066 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1067 }
Mike Reed3ae47332019-01-04 10:11:46 -05001068 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
1069 if (fFontOverrides->fTextSize) {
1070 font->writable()->setSize(fFont->getSize());
1071 }
1072 if (fFontOverrides->fHinting) {
1073 font->writable()->setHinting(fFont->getHinting());
1074 }
Ben Wagner9613e452019-01-23 10:34:59 -05001075 if (fFontOverrides->fEdging) {
1076 font->writable()->setEdging(fFont->getEdging());
Hal Canary02738a82019-01-21 18:51:32 +00001077 }
Ben Wagner9613e452019-01-23 10:34:59 -05001078 if (fFontOverrides->fEmbolden) {
1079 font->writable()->setEmbolden(fFont->isEmbolden());
Hal Canary02738a82019-01-21 18:51:32 +00001080 }
Ben Wagner9613e452019-01-23 10:34:59 -05001081 if (fFontOverrides->fLinearMetrics) {
1082 font->writable()->setLinearMetrics(fFont->isLinearMetrics());
Hal Canary02738a82019-01-21 18:51:32 +00001083 }
Ben Wagner9613e452019-01-23 10:34:59 -05001084 if (fFontOverrides->fSubpixel) {
1085 font->writable()->setSubpixel(fFont->isSubpixel());
Hal Canary02738a82019-01-21 18:51:32 +00001086 }
Ben Wagner9613e452019-01-23 10:34:59 -05001087 if (fFontOverrides->fEmbeddedBitmaps) {
1088 font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps());
Hal Canary02738a82019-01-21 18:51:32 +00001089 }
Ben Wagner9613e452019-01-23 10:34:59 -05001090 if (fFontOverrides->fForceAutoHinting) {
1091 font->writable()->setForceAutoHinting(fFont->isForceAutoHinting());
Hal Canary02738a82019-01-21 18:51:32 +00001092 }
Ben Wagner9613e452019-01-23 10:34:59 -05001093
Mike Reed3ae47332019-01-04 10:11:46 -05001094 return true;
1095 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001096 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001097 if (*paint == nullptr) {
1098 return true;
1099 }
Ben Wagner9613e452019-01-23 10:34:59 -05001100 if (fPaintOverrides->fAntiAlias) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001101 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1102 }
Ben Wagner9613e452019-01-23 10:34:59 -05001103 if (fPaintOverrides->fDither) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001104 paint->writable()->setDither(fPaint->isDither());
1105 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001106 return true;
1107 }
1108 SkPaint* fPaint;
1109 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001110 SkFont* fFont;
1111 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001112};
1113
Brian Osmanf750fbc2017-02-08 10:47:28 -05001114void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001115 SkAutoCanvasRestore autorestore(canvas, false);
1116
Brian Osmanf750fbc2017-02-08 10:47:28 -05001117 // By default, we render directly into the window's surface/canvas
1118 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001119 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001120
Brian Osmane0d4fba2017-03-15 10:24:55 -04001121 // 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 -05001122 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001123 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001124 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001125 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001126 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001127 }
1128
Brian Osman3ac99cf2017-12-01 11:23:53 -05001129 if (fSaveToSKP) {
1130 SkPictureRecorder recorder;
1131 SkCanvas* recorderCanvas = recorder.beginRecording(
1132 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001133 fSlides[fCurrentSlide]->draw(recorderCanvas);
1134 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1135 SkFILEWStream stream("sample_app.skp");
1136 picture->serialize(&stream);
1137 fSaveToSKP = false;
1138 }
1139
Brian Osmane9ed0f02018-11-26 14:50:05 -05001140 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
1141 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode) ? kRGBA_F16_SkColorType
1142 : kN32_SkColorType;
1143 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1144 canvas->getProps(&props);
1145
1146 auto make_surface = [=](int w, int h) {
1147 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1148 return Window::kRaster_BackendType == this->fBackendType
1149 ? SkSurface::MakeRaster(info, &props)
1150 : canvas->makeSurface(info, &props);
1151 };
1152
Brian Osman03115dc2018-11-26 13:55:19 -05001153 // We need to render offscreen if we're...
1154 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1155 // ... in any raster mode, because the window surface is actually GL
1156 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001157 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001158 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001159 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001160 Window::kRaster_BackendType == fBackendType ||
1161 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001162
Brian Osmane9ed0f02018-11-26 14:50:05 -05001163 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Mike Klein48b64902018-07-25 13:28:44 -04001164 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001165 }
1166
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001167 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001168 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001169 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001170 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001171 if (fTiled) {
1172 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1173 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1174 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1175 SkCanvas* tileCanvas = tileSurface->getCanvas();
1176 SkMatrix m = this->computeMatrix();
1177 for (int y = 0; y < fWindow->height(); y += tileH) {
1178 for (int x = 0; x < fWindow->width(); x += tileW) {
1179 SkAutoCanvasRestore acr(tileCanvas, true);
1180 tileCanvas->translate(-x, -y);
1181 tileCanvas->clear(SK_ColorTRANSPARENT);
1182 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001183 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1184 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001185 fSlides[fCurrentSlide]->draw(&filterCanvas);
1186 tileSurface->draw(slideCanvas, x, y, nullptr);
1187 }
1188 }
1189
1190 // Draw borders between tiles
1191 if (fDrawTileBoundaries) {
1192 SkPaint border;
1193 border.setColor(0x60FF00FF);
1194 border.setStyle(SkPaint::kStroke_Style);
1195 for (int y = 0; y < fWindow->height(); y += tileH) {
1196 for (int x = 0; x < fWindow->width(); x += tileW) {
1197 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1198 }
1199 }
1200 }
1201 } else {
1202 slideCanvas->concat(this->computeMatrix());
1203 if (kPerspective_Real == fPerspectiveMode) {
1204 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1205 }
Mike Reed3ae47332019-01-04 10:11:46 -05001206 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001207 fSlides[fCurrentSlide]->draw(&filterCanvas);
1208 }
Brian Osman56a24812017-12-19 11:15:16 -05001209 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001210 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001211
1212 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001213 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001214 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001215 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001216
1217 // If we rendered offscreen, snap an image and push the results to the window's canvas
1218 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001219 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001220
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001221 SkPaint paint;
1222 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001223 int prePerspectiveCount = canvas->save();
1224 if (kPerspective_Fake == fPerspectiveMode) {
1225 paint.setFilterQuality(kHigh_SkFilterQuality);
1226 canvas->clear(SK_ColorWHITE);
1227 canvas->concat(this->computePerspectiveMatrix());
1228 }
Brian Osman03115dc2018-11-26 13:55:19 -05001229 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001230 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001231 }
liyuqian6f163d22016-06-13 12:26:45 -07001232}
1233
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001234void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001235 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001236 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001237}
Jim Van Verth6f449692017-02-14 15:16:46 -05001238
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001239void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001240 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001241
brianosman622c8d52016-05-10 06:50:49 -07001242 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001243
Brian Osmand67e5182017-12-08 16:46:09 -05001244 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001245
1246 if (GrContext* ctx = fWindow->getGrContext()) {
1247 // Clean out cache items that haven't been used in more than 10 seconds.
1248 ctx->performDeferredCleanup(std::chrono::seconds(10));
1249 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001250}
1251
Ben Wagnera1915972018-08-09 15:06:19 -04001252void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001253 if (fCurrentSlide >= 0) {
1254 fSlides[fCurrentSlide]->resize(width, height);
1255 }
Ben Wagnera1915972018-08-09 15:06:19 -04001256}
1257
Florin Malitacefc1b92018-02-19 21:43:47 -05001258SkPoint Viewer::mapEvent(float x, float y) {
1259 const auto m = this->computeMatrix();
1260 SkMatrix inv;
1261
1262 SkAssertResult(m.invert(&inv));
1263
1264 return inv.mapXY(x, y);
1265}
1266
jvanverth814e38d2016-06-06 08:48:47 -07001267bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001268 if (GestureDevice::kMouse == fGestureDevice) {
1269 return false;
1270 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001271
1272 const auto slidePt = this->mapEvent(x, y);
1273 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1274 fWindow->inval();
1275 return true;
1276 }
1277
liyuqiand3cdbca2016-05-17 12:44:20 -07001278 void* castedOwner = reinterpret_cast<void*>(owner);
1279 switch (state) {
1280 case Window::kUp_InputState: {
1281 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001282#if defined(SK_BUILD_FOR_IOS)
1283 // TODO: move IOS swipe detection higher up into the platform code
1284 SkPoint dir;
1285 if (fGesture.isFling(&dir)) {
1286 // swiping left or right
1287 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1288 if (dir.fX < 0) {
1289 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1290 fCurrentSlide + 1 : 0);
1291 } else {
1292 this->setCurrentSlide(fCurrentSlide > 0 ?
1293 fCurrentSlide - 1 : fSlides.count() - 1);
1294 }
1295 }
1296 fGesture.reset();
1297 }
1298#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001299 break;
1300 }
1301 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001302 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001303 break;
1304 }
1305 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001306 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001307 break;
1308 }
1309 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001310 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001311 fWindow->inval();
1312 return true;
1313}
1314
Brian Osman80fc07e2017-12-08 16:45:43 -05001315bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001316 if (GestureDevice::kTouch == fGestureDevice) {
1317 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001318 }
Brian Osman16c81a12017-12-20 11:58:34 -05001319
Florin Malitacefc1b92018-02-19 21:43:47 -05001320 const auto slidePt = this->mapEvent(x, y);
1321 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1322 fWindow->inval();
1323 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001324 }
1325
1326 switch (state) {
1327 case Window::kUp_InputState: {
1328 fGesture.touchEnd(nullptr);
1329 break;
1330 }
1331 case Window::kDown_InputState: {
1332 fGesture.touchBegin(nullptr, x, y);
1333 break;
1334 }
1335 case Window::kMove_InputState: {
1336 fGesture.touchMoved(nullptr, x, y);
1337 break;
1338 }
1339 }
1340 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1341
1342 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1343 fWindow->inval();
1344 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001345 return true;
1346}
1347
Brian Osmana109e392017-02-24 09:49:14 -05001348static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1349 const ImVec2& pos, const ImVec2& size) {
1350 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1351 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1352
1353 // Invisible 10x10 button
1354 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1355 ImGui::InvisibleButton(label, ImVec2(10, 10));
1356
1357 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1358 ImGuiIO& io = ImGui::GetIO();
1359 // Normalized mouse position, relative to our gamut box
1360 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1361 // Clamp to edge of box, convert back to primary scale
1362 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1363 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1364 }
1365
1366 if (ImGui::IsItemHovered()) {
1367 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1368 }
1369
1370 // Return screen coordinates for the caller. We could just return center here, but we'd have
1371 // one frame of lag during drag.
1372 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1373}
1374
1375static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1376 ImDrawList* drawList = ImGui::GetWindowDrawList();
1377
1378 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1379 // width, and scale the height to maintain aspect ratio.
1380 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1381 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1382 ImVec2 pos = ImGui::GetCursorScreenPos();
1383
1384 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1385 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1386 // Magic numbers are pixel locations of the origin and upper-right corner.
1387 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1388 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001389
1390 // Primary markers
1391 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1392 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1393 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1394 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1395
1396 // Gamut triangle
1397 drawList->AddCircle(r, 5.0f, 0xFF000040);
1398 drawList->AddCircle(g, 5.0f, 0xFF004000);
1399 drawList->AddCircle(b, 5.0f, 0xFF400000);
1400 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1401 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1402
1403 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001404 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1405}
1406
1407static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1408 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1409 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1410 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1411
1412 // Invisible 10x10 button
1413 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1414 ImGui::InvisibleButton(label, ImVec2(10, 10));
1415
1416 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1417 ImGuiIO& io = ImGui::GetIO();
1418 // Normalized mouse position, relative to our gamut box
1419 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1420 // Clamp to edge of box
1421 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1422 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1423 *dragging = true;
1424 }
1425
1426 // Return screen coordinates for the caller. We could just return center here, but we'd have
1427 // one frame of lag during drag.
1428 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1429}
1430
Ben Wagner3627d2e2018-06-26 14:23:20 -04001431static bool ImGui_DragLocation(SkPoint* pt) {
1432 ImDrawList* drawList = ImGui::GetWindowDrawList();
1433
1434 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1435 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1436 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1437 ImVec2 pos = ImGui::GetCursorScreenPos();
1438
1439 // Background rectangle
1440 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1441
1442 // Location marker
1443 bool dragging = false;
1444 ImVec2 tl = ImGui_DragPoint("SL", pt + 0, pos, size, &dragging);
1445 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1446
1447 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1448 ImGui::Spacing();
1449
1450 return dragging;
1451}
1452
Brian Osman9bb47cf2018-04-26 15:55:00 -04001453static bool ImGui_DragQuad(SkPoint* pts) {
1454 ImDrawList* drawList = ImGui::GetWindowDrawList();
1455
1456 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1457 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1458 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1459 ImVec2 pos = ImGui::GetCursorScreenPos();
1460
1461 // Background rectangle
1462 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1463
1464 // Corner markers
1465 bool dragging = false;
1466 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1467 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1468 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1469 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1470
1471 // Draw markers and quad
1472 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1473 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1474 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1475 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1476 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1477 drawList->AddLine(tr, br, 0xFFFFFFFF);
1478 drawList->AddLine(br, bl, 0xFFFFFFFF);
1479 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1480
1481 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1482 ImGui::Spacing();
1483
1484 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001485}
1486
Brian Osmand67e5182017-12-08 16:46:09 -05001487void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001488 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1489 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001490 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001491 }
1492
1493 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001494 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1495 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001496 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001497 DisplayParams params = fWindow->getRequestedDisplayParams();
1498 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001499 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1500 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001501 if (ImGui::CollapsingHeader("Backend")) {
1502 int newBackend = static_cast<int>(fBackendType);
1503 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1504 ImGui::SameLine();
1505 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001506#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1507 ImGui::SameLine();
1508 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1509#endif
Brian Osman621491e2017-02-28 15:45:01 -05001510#if defined(SK_VULKAN)
1511 ImGui::SameLine();
1512 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1513#endif
1514 if (newBackend != fBackendType) {
1515 fDeferredActions.push_back([=]() {
1516 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1517 });
1518 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001519
Brian Salomon99a33902017-03-07 15:16:34 -05001520 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001521 bool* wire = &params.fGrContextOptions.fWireframeMode;
1522 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1523 paramsChanged = true;
1524 }
Brian Salomon99a33902017-03-07 15:16:34 -05001525
Brian Osman28b12522017-03-08 17:10:24 -05001526 if (ctx) {
1527 int sampleCount = fWindow->sampleCount();
1528 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001529 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001530 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1531 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1532 ImGui::RadioButton("16", &sampleCount, 16);
1533
1534 if (sampleCount != params.fMSAASampleCount) {
1535 params.fMSAASampleCount = sampleCount;
1536 paramsChanged = true;
1537 }
1538 }
1539
Ben Wagner37c54032018-04-13 14:30:23 -04001540 int pixelGeometryIdx = 0;
1541 if (fPixelGeometryOverrides) {
1542 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1543 }
1544 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1545 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1546 {
1547 uint32_t flags = params.fSurfaceProps.flags();
1548 if (pixelGeometryIdx == 0) {
1549 fPixelGeometryOverrides = false;
1550 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1551 } else {
1552 fPixelGeometryOverrides = true;
1553 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1554 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1555 }
1556 paramsChanged = true;
1557 }
1558
1559 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1560 if (ImGui::Checkbox("DFT", &useDFT)) {
1561 uint32_t flags = params.fSurfaceProps.flags();
1562 if (useDFT) {
1563 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1564 } else {
1565 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1566 }
1567 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1568 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1569 paramsChanged = true;
1570 }
1571
Brian Osman8a9de3d2017-03-01 14:59:05 -05001572 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001573 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001574 auto prButton = [&](GpuPathRenderers x) {
1575 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001576 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1577 params.fGrContextOptions.fGpuPathRenderers = x;
1578 paramsChanged = true;
1579 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001580 }
1581 };
1582
1583 if (!ctx) {
1584 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001585 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001586 prButton(GpuPathRenderers::kAll);
Robert Phillips9da87e02019-02-04 13:26:26 -05001587 if (ctx->priv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001588 prButton(GpuPathRenderers::kStencilAndCover);
1589 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001590 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001591 prButton(GpuPathRenderers::kNone);
1592 } else {
1593 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001594 if (GrCoverageCountingPathRenderer::IsSupported(
Robert Phillips9da87e02019-02-04 13:26:26 -05001595 *ctx->priv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001596 prButton(GpuPathRenderers::kCoverageCounting);
1597 }
Jim Van Verth83010462017-03-16 08:45:39 -04001598 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001599 prButton(GpuPathRenderers::kTessellating);
1600 prButton(GpuPathRenderers::kNone);
1601 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001602 ImGui::TreePop();
1603 }
Brian Osman621491e2017-02-28 15:45:01 -05001604 }
1605
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001606 if (ImGui::CollapsingHeader("Transform")) {
1607 float zoom = fZoomLevel;
1608 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1609 fZoomLevel = zoom;
1610 this->preTouchMatrixChanged();
1611 paramsChanged = true;
1612 }
1613 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001614 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001615 fRotation = deg;
1616 this->preTouchMatrixChanged();
1617 paramsChanged = true;
1618 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001619 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1620 if (ImGui_DragLocation(&fOffset)) {
1621 this->preTouchMatrixChanged();
1622 paramsChanged = true;
1623 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001624 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1625 this->preTouchMatrixChanged();
1626 paramsChanged = true;
1627 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001628 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001629 if (ImGui::CollapsingHeader("Tiling")) {
1630 ImGui::Checkbox("Enable", &fTiled);
1631 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1632 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1633 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1634 }
Brian Osman805a7272018-05-02 15:40:20 -04001635 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1636 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1637 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001638 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001639 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001640 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001641 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001642 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001643 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001644 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001645 }
1646
Ben Wagnera580fb32018-04-17 11:16:32 -04001647 if (ImGui::CollapsingHeader("Paint")) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001648 int aliasIdx = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001649 if (fPaintOverrides.fAntiAlias) {
1650 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAliasState) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001651 }
1652 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1653 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1654 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1655 {
1656 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1657 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1658 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1659 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1660 if (aliasIdx == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001661 fPaintOverrides.fAntiAliasState = SkPaintFields::AntiAliasState::Alias;
1662 fPaintOverrides.fAntiAlias = false;
Ben Wagnera580fb32018-04-17 11:16:32 -04001663 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001664 fPaintOverrides.fAntiAlias = true;
1665 fPaintOverrides.fAntiAliasState = SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001666 fPaint.setAntiAlias(aliasIdx > 1);
Ben Wagner9613e452019-01-23 10:34:59 -05001667 switch (fPaintOverrides.fAntiAliasState) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001668 case SkPaintFields::AntiAliasState::Alias:
1669 break;
1670 case SkPaintFields::AntiAliasState::Normal:
1671 break;
1672 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1673 gSkUseAnalyticAA = true;
1674 gSkForceAnalyticAA = false;
1675 gSkUseDeltaAA = gSkForceDeltaAA = false;
1676 break;
1677 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1678 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1679 gSkUseDeltaAA = gSkForceDeltaAA = false;
1680 break;
1681 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1682 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1683 gSkUseDeltaAA = true;
1684 gSkForceDeltaAA = false;
1685 break;
1686 case SkPaintFields::AntiAliasState::DeltaAAForced:
1687 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1688 gSkUseDeltaAA = gSkForceDeltaAA = true;
1689 break;
1690 }
1691 }
1692 paramsChanged = true;
1693 }
1694
Ben Wagner99a78dc2018-05-09 18:23:51 -04001695 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Ben Wagner9613e452019-01-23 10:34:59 -05001696 bool SkPaintFields::* flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001697 bool (SkPaint::* isFlag)() const,
1698 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001699 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001700 int itemIndex = 0;
Ben Wagner9613e452019-01-23 10:34:59 -05001701 if (fPaintOverrides.*flag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001702 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001703 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001704 if (ImGui::Combo(label, &itemIndex, items)) {
1705 if (itemIndex == 0) {
Ben Wagner9613e452019-01-23 10:34:59 -05001706 fPaintOverrides.*flag = false;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001707 } else {
Ben Wagner9613e452019-01-23 10:34:59 -05001708 fPaintOverrides.*flag = true;
Ben Wagner99a78dc2018-05-09 18:23:51 -04001709 (fPaint.*setFlag)(itemIndex == 2);
1710 }
1711 paramsChanged = true;
1712 }
1713 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001714
Ben Wagner99a78dc2018-05-09 18:23:51 -04001715 paintFlag("Dither",
1716 "Default\0No Dither\0Dither\0\0",
Ben Wagner9613e452019-01-23 10:34:59 -05001717 &SkPaintFields::fDither,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001718 &SkPaint::isDither, &SkPaint::setDither);
Ben Wagner9613e452019-01-23 10:34:59 -05001719 }
Hal Canary02738a82019-01-21 18:51:32 +00001720
Ben Wagner9613e452019-01-23 10:34:59 -05001721 if (ImGui::CollapsingHeader("Font")) {
1722 int hintingIdx = 0;
1723 if (fFontOverrides.fHinting) {
1724 hintingIdx = SkTo<int>(fFont.getHinting()) + 1;
1725 }
1726 if (ImGui::Combo("Hinting", &hintingIdx,
1727 "Default\0None\0Slight\0Normal\0Full\0\0"))
1728 {
1729 if (hintingIdx == 0) {
1730 fFontOverrides.fHinting = false;
1731 fFont.setHinting(kNo_SkFontHinting);
1732 } else {
1733 fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1));
1734 fFontOverrides.fHinting = true;
1735 }
1736 paramsChanged = true;
1737 }
Hal Canary02738a82019-01-21 18:51:32 +00001738
Ben Wagner9613e452019-01-23 10:34:59 -05001739 auto fontFlag = [this, &paramsChanged](const char* label, const char* items,
1740 bool SkFontFields::* flag,
1741 bool (SkFont::* isFlag)() const,
1742 void (SkFont::* setFlag)(bool) )
1743 {
1744 int itemIndex = 0;
1745 if (fFontOverrides.*flag) {
1746 itemIndex = (fFont.*isFlag)() ? 2 : 1;
1747 }
1748 if (ImGui::Combo(label, &itemIndex, items)) {
1749 if (itemIndex == 0) {
1750 fFontOverrides.*flag = false;
1751 } else {
1752 fFontOverrides.*flag = true;
1753 (fFont.*setFlag)(itemIndex == 2);
1754 }
1755 paramsChanged = true;
1756 }
1757 };
Hal Canary02738a82019-01-21 18:51:32 +00001758
Ben Wagner9613e452019-01-23 10:34:59 -05001759 fontFlag("Fake Bold Glyphs",
1760 "Default\0No Fake Bold\0Fake Bold\0\0",
1761 &SkFontFields::fEmbolden,
1762 &SkFont::isEmbolden, &SkFont::setEmbolden);
Hal Canary02738a82019-01-21 18:51:32 +00001763
Ben Wagner9613e452019-01-23 10:34:59 -05001764 fontFlag("Linear Text",
1765 "Default\0No Linear Text\0Linear Text\0\0",
1766 &SkFontFields::fLinearMetrics,
1767 &SkFont::isLinearMetrics, &SkFont::setLinearMetrics);
Hal Canary02738a82019-01-21 18:51:32 +00001768
Ben Wagner9613e452019-01-23 10:34:59 -05001769 fontFlag("Subpixel Position Glyphs",
1770 "Default\0Pixel Text\0Subpixel Text\0\0",
1771 &SkFontFields::fSubpixel,
1772 &SkFont::isSubpixel, &SkFont::setSubpixel);
1773
1774 fontFlag("Embedded Bitmap Text",
1775 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1776 &SkFontFields::fEmbeddedBitmaps,
1777 &SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps);
1778
1779 fontFlag("Force Auto-Hinting",
1780 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1781 &SkFontFields::fForceAutoHinting,
1782 &SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting);
1783
1784 int edgingIdx = 0;
1785 if (fFontOverrides.fEdging) {
1786 edgingIdx = SkTo<int>(fFont.getEdging()) + 1;
1787 }
1788 if (ImGui::Combo("Edging", &edgingIdx,
1789 "Default\0Alias\0Antialias\0Subpixel Antialias\0\0"))
1790 {
1791 if (edgingIdx == 0) {
1792 fFontOverrides.fEdging = false;
1793 fFont.setEdging(SkFont::Edging::kAlias);
1794 } else {
1795 fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1));
1796 fFontOverrides.fEdging = true;
1797 }
1798 paramsChanged = true;
1799 }
1800
Mike Reed3ae47332019-01-04 10:11:46 -05001801 ImGui::Checkbox("Override TextSize", &fFontOverrides.fTextSize);
1802 if (fFontOverrides.fTextSize) {
1803 ImGui::DragFloat2("TextRange", fFontOverrides.fTextSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001804 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001805 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001806 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Mike Reed3ae47332019-01-04 10:11:46 -05001807 fFontOverrides.fTextSizeRange[0],
1808 fFontOverrides.fTextSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001809 "%.6f", 2.0f))
1810 {
Mike Reed3ae47332019-01-04 10:11:46 -05001811 fFont.setSize(textSize);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001812 this->preTouchMatrixChanged();
1813 paramsChanged = true;
1814 }
1815 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001816 }
1817
Mike Reed81f60ec2018-05-15 10:09:52 -04001818 {
1819 SkMetaData controls;
1820 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1821 if (ImGui::CollapsingHeader("Current Slide")) {
1822 SkMetaData::Iter iter(controls);
1823 const char* name;
1824 SkMetaData::Type type;
1825 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001826 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001827 if (type == SkMetaData::kScalar_Type) {
1828 float val[3];
1829 SkASSERT(count == 3);
1830 controls.findScalars(name, &count, val);
1831 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1832 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001833 }
1834 }
1835 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001836 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001837 }
1838 }
1839 }
1840
Ben Wagner7a3c6742018-04-23 10:01:07 -04001841 if (fShowSlidePicker) {
1842 ImGui::SetNextTreeNodeOpen(true);
1843 }
Brian Osman79086b92017-02-10 13:36:16 -05001844 if (ImGui::CollapsingHeader("Slide")) {
1845 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001846 static ImVector<const char*> filteredSlideNames;
1847 static ImVector<int> filteredSlideIndices;
1848
Brian Osmanfce09c52017-11-14 15:32:20 -05001849 if (fShowSlidePicker) {
1850 ImGui::SetKeyboardFocusHere();
1851 fShowSlidePicker = false;
1852 }
1853
Brian Osman79086b92017-02-10 13:36:16 -05001854 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001855 filteredSlideNames.clear();
1856 filteredSlideIndices.clear();
1857 int filteredIndex = 0;
1858 for (int i = 0; i < fSlides.count(); ++i) {
1859 const char* slideName = fSlides[i]->getName().c_str();
1860 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1861 if (i == fCurrentSlide) {
1862 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001863 }
Brian Osmanf479e422017-11-08 13:11:36 -05001864 filteredSlideNames.push_back(slideName);
1865 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001866 }
Brian Osman79086b92017-02-10 13:36:16 -05001867 }
Brian Osmanf479e422017-11-08 13:11:36 -05001868
Brian Osmanf479e422017-11-08 13:11:36 -05001869 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1870 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001871 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001872 }
1873 }
Brian Osmana109e392017-02-24 09:49:14 -05001874
1875 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001876 ColorMode newMode = fColorMode;
1877 auto cmButton = [&](ColorMode mode, const char* label) {
1878 if (ImGui::RadioButton(label, mode == fColorMode)) {
1879 newMode = mode;
1880 }
1881 };
1882
1883 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001884 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1885 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001886
1887 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001888 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001889 }
1890
1891 // Pick from common gamuts:
1892 int primariesIdx = 4; // Default: Custom
1893 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1894 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1895 primariesIdx = i;
1896 break;
1897 }
1898 }
1899
Brian Osman03115dc2018-11-26 13:55:19 -05001900 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05001901 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001902
Brian Osmana109e392017-02-24 09:49:14 -05001903 if (ImGui::Combo("Primaries", &primariesIdx,
1904 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1905 if (primariesIdx >= 0 && primariesIdx <= 3) {
1906 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1907 }
1908 }
1909
1910 // Allow direct editing of gamut
1911 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1912 }
Brian Osman207d4102019-01-10 09:40:58 -05001913
1914 if (ImGui::CollapsingHeader("Animation")) {
1915 bool isPaused = fAnimTimer.isPaused();
1916 if (ImGui::Checkbox("Pause", &isPaused)) {
1917 fAnimTimer.togglePauseResume();
1918 }
Brian Osman707d2022019-01-10 11:27:34 -05001919
1920 float speed = fAnimTimer.getSpeed();
1921 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
1922 fAnimTimer.setSpeed(speed);
1923 }
Brian Osman207d4102019-01-10 09:40:58 -05001924 }
Brian Osman79086b92017-02-10 13:36:16 -05001925 }
Brian Salomon99a33902017-03-07 15:16:34 -05001926 if (paramsChanged) {
1927 fDeferredActions.push_back([=]() {
1928 fWindow->setRequestedDisplayParams(params);
1929 fWindow->inval();
1930 this->updateTitle();
1931 });
1932 }
Brian Osman79086b92017-02-10 13:36:16 -05001933 ImGui::End();
1934 }
1935
Brian Osmanf6877092017-02-13 09:39:57 -05001936 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04001937 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
1938 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001939 static int zoomFactor = 8;
1940 if (ImGui::Button("<<")) {
1941 zoomFactor = SkTMax(zoomFactor / 2, 4);
1942 }
1943 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1944 if (ImGui::Button(">>")) {
1945 zoomFactor = SkTMin(zoomFactor * 2, 32);
1946 }
Brian Osmanf6877092017-02-13 09:39:57 -05001947
Ben Wagner3627d2e2018-06-26 14:23:20 -04001948 if (!fZoomWindowFixed) {
1949 ImVec2 mousePos = ImGui::GetMousePos();
1950 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
1951 }
1952 SkScalar x = fZoomWindowLocation.x();
1953 SkScalar y = fZoomWindowLocation.y();
1954 int xInt = SkScalarRoundToInt(x);
1955 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05001956 ImVec2 avail = ImGui::GetContentRegionAvail();
1957
Brian Osmanead517d2017-11-13 15:36:36 -05001958 uint32_t pixel = 0;
1959 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001960 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001961 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001962 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
Ben Wagner3627d2e2018-06-26 14:23:20 -04001963 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05001964 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001965 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1966 }
1967
Brian Osmand67e5182017-12-08 16:46:09 -05001968 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001969 // Translate so the region of the image that's under the mouse cursor is centered
1970 // in the zoom canvas:
1971 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001972 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
1973 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05001974 c->drawImage(this->fLastImage, 0, 0);
1975
1976 SkPaint outline;
1977 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001978 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05001979 });
Brian Osmanf6877092017-02-13 09:39:57 -05001980 }
1981
1982 ImGui::End();
1983 }
Brian Osman79086b92017-02-10 13:36:16 -05001984}
1985
liyuqian2edb0f42016-07-06 14:11:32 -07001986void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001987 for (int i = 0; i < fDeferredActions.count(); ++i) {
1988 fDeferredActions[i]();
1989 }
1990 fDeferredActions.reset();
1991
Brian Osman56a24812017-12-19 11:15:16 -05001992 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001993 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001994 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001995 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001996
Brian Osman79086b92017-02-10 13:36:16 -05001997 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04001998 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
1999 // not be visible, though. So we need to redraw if there is at least one visible window, or
2000 // more than one active window. Newly created windows are active but not visible for one frame
2001 // while they determine their layout and sizing.
2002 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
2003 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07002004 fWindow->inval();
2005 }
jvanverth9f372462016-04-06 06:08:59 -07002006}
liyuqiane5a6cd92016-05-27 08:52:52 -07002007
Florin Malitab632df72018-06-18 21:23:06 -04002008template <typename OptionsFunc>
2009static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
2010 OptionsFunc&& optionsFunc) {
2011 writer.beginObject();
2012 {
2013 writer.appendString(kName , name);
2014 writer.appendString(kValue, value);
2015
2016 writer.beginArray(kOptions);
2017 {
2018 optionsFunc(writer);
2019 }
2020 writer.endArray();
2021 }
2022 writer.endObject();
2023}
2024
2025
liyuqiane5a6cd92016-05-27 08:52:52 -07002026void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07002027 if (!fWindow) {
2028 return;
2029 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002030 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07002031 return; // Surface hasn't been created yet.
2032 }
2033
Florin Malitab632df72018-06-18 21:23:06 -04002034 SkDynamicMemoryWStream memStream;
2035 SkJSONWriter writer(&memStream);
2036 writer.beginArray();
2037
liyuqianb73c24b2016-06-03 08:47:23 -07002038 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04002039 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
2040 [this](SkJSONWriter& writer) {
2041 for(const auto& slide : fSlides) {
2042 writer.appendString(slide->getName().c_str());
2043 }
2044 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002045
liyuqianb73c24b2016-06-03 08:47:23 -07002046 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04002047 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
2048 [](SkJSONWriter& writer) {
2049 for (const auto& str : kBackendTypeStrings) {
2050 writer.appendString(str);
2051 }
2052 });
liyuqiane5a6cd92016-05-27 08:52:52 -07002053
csmartdalton578f0642017-02-24 16:04:47 -07002054 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04002055 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
2056 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
2057 [this](SkJSONWriter& writer) {
2058 writer.appendS32(0);
2059
2060 if (sk_app::Window::kRaster_BackendType == fBackendType) {
2061 return;
2062 }
2063
2064 for (int msaa : {4, 8, 16}) {
2065 writer.appendS32(msaa);
2066 }
2067 });
csmartdalton578f0642017-02-24 16:04:47 -07002068
csmartdalton61cd31a2017-02-27 17:00:53 -07002069 // Path renderer state
2070 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002071 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2072 [this](SkJSONWriter& writer) {
2073 const GrContext* ctx = fWindow->getGrContext();
2074 if (!ctx) {
2075 writer.appendString("Software");
2076 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -05002077 const auto* caps = ctx->priv().caps();
Florin Malitab632df72018-06-18 21:23:06 -04002078
Florin Malitab632df72018-06-18 21:23:06 -04002079 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2080 if (fWindow->sampleCount() > 1) {
2081 if (caps->shaderCaps()->pathRenderingSupport()) {
2082 writer.appendString(
2083 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2084 }
2085 } else {
2086 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2087 writer.appendString(
2088 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2089 }
2090 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2091 }
2092 writer.appendString(
2093 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2094 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2095 }
2096 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002097
liyuqianb73c24b2016-06-03 08:47:23 -07002098 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002099 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2100 [this](SkJSONWriter& writer) {
2101 writer.appendString(kSoftkeyHint);
2102 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2103 writer.appendString(softkey.c_str());
2104 }
2105 });
liyuqianb73c24b2016-06-03 08:47:23 -07002106
Florin Malitab632df72018-06-18 21:23:06 -04002107 writer.endArray();
2108 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002109
Florin Malitab632df72018-06-18 21:23:06 -04002110 auto data = memStream.detachAsData();
2111
2112 // TODO: would be cool to avoid this copy
2113 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2114
2115 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002116}
2117
2118void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002119 // For those who will add more features to handle the state change in this function:
2120 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2121 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2122 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002123 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002124 for (int i = 0; i < fSlides.count(); ++i) {
2125 if (fSlides[i]->getName().equals(stateValue)) {
2126 this->setCurrentSlide(i);
2127 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002128 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002129 }
Florin Malitaab99c342018-01-16 16:23:03 -05002130
2131 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002132 } else if (stateName.equals(kBackendStateName)) {
2133 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2134 if (stateValue.equals(kBackendTypeStrings[i])) {
2135 if (fBackendType != i) {
2136 fBackendType = (sk_app::Window::BackendType)i;
2137 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002138 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002139 }
2140 break;
2141 }
2142 }
csmartdalton578f0642017-02-24 16:04:47 -07002143 } else if (stateName.equals(kMSAAStateName)) {
2144 DisplayParams params = fWindow->getRequestedDisplayParams();
2145 int sampleCount = atoi(stateValue.c_str());
2146 if (sampleCount != params.fMSAASampleCount) {
2147 params.fMSAASampleCount = sampleCount;
2148 fWindow->setRequestedDisplayParams(params);
2149 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002150 this->updateTitle();
2151 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002152 }
2153 } else if (stateName.equals(kPathRendererStateName)) {
2154 DisplayParams params = fWindow->getRequestedDisplayParams();
2155 for (const auto& pair : gPathRendererNames) {
2156 if (pair.second == stateValue.c_str()) {
2157 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2158 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2159 fWindow->setRequestedDisplayParams(params);
2160 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002161 this->updateTitle();
2162 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002163 }
2164 break;
2165 }
csmartdalton578f0642017-02-24 16:04:47 -07002166 }
liyuqianb73c24b2016-06-03 08:47:23 -07002167 } else if (stateName.equals(kSoftkeyStateName)) {
2168 if (!stateValue.equals(kSoftkeyHint)) {
2169 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002170 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002171 }
liyuqian2edb0f42016-07-06 14:11:32 -07002172 } else if (stateName.equals(kRefreshStateName)) {
2173 // This state is actually NOT in the UI state.
2174 // We use this to allow Android to quickly set bool fRefresh.
2175 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002176 } else {
2177 SkDebugf("Unknown stateName: %s", stateName.c_str());
2178 }
2179}
Brian Osman79086b92017-02-10 13:36:16 -05002180
2181bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002182 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002183}
2184
2185bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002186 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002187 fWindow->inval();
2188 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002189 } else {
2190 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002191 }
Brian Osman79086b92017-02-10 13:36:16 -05002192}