blob: bf779e92477eb884ebabdf8e2e78695a25f44a2e [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Chris Dalton2d18f412018-02-20 13:23:32 -07008#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -07009#include "GMSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040010#include "GrContext.h"
11#include "GrContextPriv.h"
liyuqian6f163d22016-06-13 12:26:45 -070012#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050013#include "Resources.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070014#include "SKPSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040015#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070016#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050017#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040018#include "SkColorSpaceXformCanvas.h"
Brian Osman2dd96932016-10-18 15:33:53 -040019#include "SkCommandLineFlags.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040020#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070021#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040022#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050023#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040024#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050025#include "SkImagePriv.h"
Florin Malitab632df72018-06-18 21:23:06 -040026#include "SkJSONWriter.h"
Herb Derbyefe39bc2018-05-01 17:06:20 -040027#include "SkMakeUnique.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070028#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050029#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050030#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050031#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040032#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070033#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070034#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050035#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050036#include "SkTestFontMgr.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040037#include "SkTo.h"
Hal Canary8a001442018-09-19 11:31:27 -040038#include "SlideDir.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040039#include "SvgSlide.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040040#include "Viewer.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060041#include "ccpr/GrCoverageCountingPathRenderer.h"
csmartdalton578f0642017-02-24 16:04:47 -070042
Hal Canaryc640d0d2018-06-13 09:59:02 -040043#include <stdlib.h>
44#include <map>
45
Hal Canary8a001442018-09-19 11:31:27 -040046#include "imgui.h"
Florin Malita3b526b02018-05-25 12:43:51 -040047
Florin Malita87ccf332018-05-04 12:23:24 -040048#if defined(SK_ENABLE_SKOTTIE)
49 #include "SkottieSlide.h"
50#endif
51
Ruiqi Maof5101492018-06-29 14:32:21 -040052#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
53 #include "NIMASlide.h"
54#endif
55
jvanverth34524262016-05-04 13:49:13 -070056using namespace sk_app;
57
csmartdalton61cd31a2017-02-27 17:00:53 -070058static std::map<GpuPathRenderers, std::string> gPathRendererNames;
59
jvanverth9f372462016-04-06 06:08:59 -070060Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070061 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070062}
63
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060064static DEFINE_string(slide, "", "Start on this sample.");
65static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050066
bsalomon6c471f72016-07-26 12:56:32 -070067#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070068# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070069#else
70# define BACKENDS_STR "\"sw\" and \"gl\""
71#endif
72
Brian Osman2dd96932016-10-18 15:33:53 -040073static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070074
Brian Salomonbdecacf2018-02-02 20:32:49 -050075static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070076
Chris Dalton2d18f412018-02-20 13:23:32 -070077DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
78
Brian Osman53136aa2017-07-20 15:43:35 -040079DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050080
Florin Malita38792ce2018-05-08 10:36:18 -040081DEFINE_string2(file, f, "", "Open a single file for viewing.");
82
Brian Salomon194db172017-08-17 14:37:06 -040083const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070084 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040085#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
86 "ANGLE",
87#endif
jvanverth063ece72016-06-17 09:29:14 -070088#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070089 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070090#endif
csmartdalton578f0642017-02-24 16:04:47 -070091 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070092};
93
bsalomon6c471f72016-07-26 12:56:32 -070094static sk_app::Window::BackendType get_backend_type(const char* str) {
95#ifdef SK_VULKAN
96 if (0 == strcmp(str, "vk")) {
97 return sk_app::Window::kVulkan_BackendType;
98 } else
99#endif
Brian Salomon194db172017-08-17 14:37:06 -0400100#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
101 if (0 == strcmp(str, "angle")) {
102 return sk_app::Window::kANGLE_BackendType;
103 } else
104#endif
bsalomon6c471f72016-07-26 12:56:32 -0700105 if (0 == strcmp(str, "gl")) {
106 return sk_app::Window::kNativeGL_BackendType;
107 } else if (0 == strcmp(str, "sw")) {
108 return sk_app::Window::kRaster_BackendType;
109 } else {
110 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
111 return sk_app::Window::kRaster_BackendType;
112 }
113}
114
Brian Osmana109e392017-02-24 09:49:14 -0500115static SkColorSpacePrimaries gSrgbPrimaries = {
116 0.64f, 0.33f,
117 0.30f, 0.60f,
118 0.15f, 0.06f,
119 0.3127f, 0.3290f };
120
121static SkColorSpacePrimaries gAdobePrimaries = {
122 0.64f, 0.33f,
123 0.21f, 0.71f,
124 0.15f, 0.06f,
125 0.3127f, 0.3290f };
126
127static SkColorSpacePrimaries gP3Primaries = {
128 0.680f, 0.320f,
129 0.265f, 0.690f,
130 0.150f, 0.060f,
131 0.3127f, 0.3290f };
132
133static SkColorSpacePrimaries gRec2020Primaries = {
134 0.708f, 0.292f,
135 0.170f, 0.797f,
136 0.131f, 0.046f,
137 0.3127f, 0.3290f };
138
139struct NamedPrimaries {
140 const char* fName;
141 SkColorSpacePrimaries* fPrimaries;
142} gNamedPrimaries[] = {
143 { "sRGB", &gSrgbPrimaries },
144 { "AdobeRGB", &gAdobePrimaries },
145 { "P3", &gP3Primaries },
146 { "Rec. 2020", &gRec2020Primaries },
147};
148
149static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
150 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
151}
152
Brian Osman70d2f432017-11-08 09:54:10 -0500153static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
154 // In raster mode, we still use GL for the window.
155 // This lets us render the GUI faster (and correct).
156 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
157}
158
liyuqiane5a6cd92016-05-27 08:52:52 -0700159const char* kName = "name";
160const char* kValue = "value";
161const char* kOptions = "options";
162const char* kSlideStateName = "Slide";
163const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700164const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700165const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700166const char* kSoftkeyStateName = "Softkey";
167const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700168const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700169const char* kON = "ON";
170const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700171const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700172
jvanverth34524262016-05-04 13:49:13 -0700173Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500174 : fCurrentSlide(-1)
175 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500176 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500177 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500178 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500179 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500180 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400181 , fZoomWindowFixed(false)
182 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500183 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400184 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700185 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500186 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500187 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500188 // Our UI can only tweak gamma (currently), so start out gamma-only
189 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700190 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400191 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400192 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400193 , fGestureDevice(GestureDevice::kNone)
Brian Osman805a7272018-05-02 15:40:20 -0400194 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700195{
Greg Daniel285db442016-10-14 09:12:53 -0400196 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700197
Brian Osmanf09e35e2017-12-15 14:48:09 -0500198 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500199 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500200 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
201 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
202 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
203 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700204
jvanverth2bb3b6d2016-04-08 07:24:09 -0700205 SkDebugf("Command line arguments: ");
206 for (int i = 1; i < argc; ++i) {
207 SkDebugf("%s ", argv[i]);
208 }
209 SkDebugf("\n");
210
211 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500212#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400213 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500214#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700215
Ben Wagner483c7722018-02-20 17:06:07 -0500216 if (!FLAGS_nativeFonts) {
217 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
218 }
219
Brian Osmanbc8150f2017-07-24 11:38:01 -0400220 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400221 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400222
bsalomon6c471f72016-07-26 12:56:32 -0700223 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700224 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700225
csmartdalton578f0642017-02-24 16:04:47 -0700226 DisplayParams displayParams;
227 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700228 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700229 fWindow->setRequestedDisplayParams(displayParams);
230
Brian Osman56a24812017-12-19 11:15:16 -0500231 // Configure timers
232 fStatsLayer.setActive(false);
233 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
234 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
235 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
236
jvanverth9f372462016-04-06 06:08:59 -0700237 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700238 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500239 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500240 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500241 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700242
brianosman622c8d52016-05-10 06:50:49 -0700243 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500244 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
245 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
246 fWindow->inval();
247 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500248 // Command to jump directly to the slide picker and give it focus
249 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
250 this->fShowImGuiDebugWindow = true;
251 this->fShowSlidePicker = true;
252 fWindow->inval();
253 });
254 // Alias that to Backspace, to match SampleApp
255 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
256 this->fShowImGuiDebugWindow = true;
257 this->fShowSlidePicker = true;
258 fWindow->inval();
259 });
Brian Osman79086b92017-02-10 13:36:16 -0500260 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
261 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
262 fWindow->inval();
263 });
Brian Osmanf6877092017-02-13 09:39:57 -0500264 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
265 this->fShowZoomWindow = !this->fShowZoomWindow;
266 fWindow->inval();
267 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400268 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
269 this->fZoomWindowFixed = !this->fZoomWindowFixed;
270 fWindow->inval();
271 });
brianosman622c8d52016-05-10 06:50:49 -0700272 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500273 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700274 fWindow->inval();
275 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400276 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500277 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400278 this->updateTitle();
279 fWindow->inval();
280 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500281 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500282 switch (fColorMode) {
283 case ColorMode::kLegacy:
284 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
285 break;
286 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
287 this->setColorMode(ColorMode::kColorManagedSRGB8888);
288 break;
289 case ColorMode::kColorManagedSRGB8888:
290 this->setColorMode(ColorMode::kColorManagedLinearF16);
291 break;
292 case ColorMode::kColorManagedLinearF16:
293 this->setColorMode(ColorMode::kLegacy);
294 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500295 }
brianosman622c8d52016-05-10 06:50:49 -0700296 });
297 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500298 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700299 });
300 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500301 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700302 });
303 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
304 this->changeZoomLevel(1.f / 32.f);
305 fWindow->inval();
306 });
307 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
308 this->changeZoomLevel(-1.f / 32.f);
309 fWindow->inval();
310 });
jvanverthaf236b52016-05-20 06:01:06 -0700311 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400312 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
313 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500314 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400315#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
316 if (newBackend == sk_app::Window::kVulkan_BackendType) {
317 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
318 sk_app::Window::kBackendTypeCount);
319 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
320 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500321 }
322#endif
Brian Osman621491e2017-02-28 15:45:01 -0500323 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700324 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500325 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
326 fSaveToSKP = true;
327 fWindow->inval();
328 });
Ben Wagner37c54032018-04-13 14:30:23 -0400329 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
330 DisplayParams params = fWindow->getRequestedDisplayParams();
331 uint32_t flags = params.fSurfaceProps.flags();
332 if (!fPixelGeometryOverrides) {
333 fPixelGeometryOverrides = true;
334 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
335 } else {
336 switch (params.fSurfaceProps.pixelGeometry()) {
337 case kUnknown_SkPixelGeometry:
338 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
339 break;
340 case kRGB_H_SkPixelGeometry:
341 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
342 break;
343 case kBGR_H_SkPixelGeometry:
344 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
345 break;
346 case kRGB_V_SkPixelGeometry:
347 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
348 break;
349 case kBGR_V_SkPixelGeometry:
350 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
351 fPixelGeometryOverrides = false;
352 break;
353 }
354 }
355 fWindow->setRequestedDisplayParams(params);
356 this->updateTitle();
357 fWindow->inval();
358 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500359 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
360 if (!fPaintOverrides.fHinting) {
361 fPaintOverrides.fHinting = true;
Mike Klein8e12cc62018-11-07 23:56:21 +0000362 fPaint.setHinting(SkPaint::kNo_Hinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500363 } else {
Mike Klein8e12cc62018-11-07 23:56:21 +0000364 switch (fPaint.getHinting()) {
365 case SkPaint::kNo_Hinting:
366 fPaint.setHinting(SkPaint::kSlight_Hinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500367 break;
Mike Klein8e12cc62018-11-07 23:56:21 +0000368 case SkPaint::kSlight_Hinting:
369 fPaint.setHinting(SkPaint::kNormal_Hinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500370 break;
Mike Klein8e12cc62018-11-07 23:56:21 +0000371 case SkPaint::kNormal_Hinting:
372 fPaint.setHinting(SkPaint::kFull_Hinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500373 break;
Mike Klein8e12cc62018-11-07 23:56:21 +0000374 case SkPaint::kFull_Hinting:
375 fPaint.setHinting(SkPaint::kNo_Hinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500376 fPaintOverrides.fHinting = false;
377 break;
378 }
379 }
380 this->updateTitle();
381 fWindow->inval();
382 });
383 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
384 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
385 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
386 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
387 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500388 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
389 gSkUseDeltaAA = gSkForceDeltaAA = false;
390 } else {
391 fPaint.setAntiAlias(true);
392 switch (fPaintOverrides.fAntiAlias) {
393 case SkPaintFields::AntiAliasState::Alias:
394 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400395 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
396 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500397 break;
398 case SkPaintFields::AntiAliasState::Normal:
399 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500400 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400401 gSkForceAnalyticAA = false;
402 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500403 break;
404 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
405 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400406 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
407 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500408 break;
409 case SkPaintFields::AntiAliasState::AnalyticAAForced:
410 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
411 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
412 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400413 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500414 break;
415 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
416 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400417 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
418 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500419 break;
420 case SkPaintFields::AntiAliasState::DeltaAAForced:
421 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
422 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
423 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
424 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
425 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
426 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
427 break;
428 }
429 }
430 this->updateTitle();
431 fWindow->inval();
432 });
Ben Wagner37c54032018-04-13 14:30:23 -0400433 fCommands.addCommand('D', "Modes", "DFT", [this]() {
434 DisplayParams params = fWindow->getRequestedDisplayParams();
435 uint32_t flags = params.fSurfaceProps.flags();
436 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
437 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
438 fWindow->setRequestedDisplayParams(params);
439 this->updateTitle();
440 fWindow->inval();
441 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500442 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
443 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
444 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
445 fPaint.setLCDRenderText(false);
446 } else {
447 if (!fPaint.isLCDRenderText()) {
448 fPaint.setLCDRenderText(true);
449 } else {
450 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
451 }
452 }
453 this->updateTitle();
454 fWindow->inval();
455 });
456 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
457 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
458 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
459 fPaint.setSubpixelText(false);
460 } else {
461 if (!fPaint.isSubpixelText()) {
462 fPaint.setSubpixelText(true);
463 } else {
464 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
465 }
466 }
467 this->updateTitle();
468 fWindow->inval();
469 });
Brian Osman805a7272018-05-02 15:40:20 -0400470 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
471 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
472 : kPerspective_Real;
473 this->updateTitle();
474 fWindow->inval();
475 });
476 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
477 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
478 : kPerspective_Off;
479 this->updateTitle();
480 fWindow->inval();
481 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400482 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
483 fZoomUI = !fZoomUI;
484 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
485 fWindow->inval();
486 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500487
jvanverth2bb3b6d2016-04-08 07:24:09 -0700488 // set up slides
489 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500490 if (FLAGS_list) {
491 this->listNames();
492 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700493
Brian Osman9bb47cf2018-04-26 15:55:00 -0400494 fPerspectivePoints[0].set(0, 0);
495 fPerspectivePoints[1].set(1, 0);
496 fPerspectivePoints[2].set(0, 1);
497 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700498 fAnimTimer.run();
499
Hal Canaryc465d132017-12-08 10:21:31 -0500500 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500501 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400502 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500503 }
504 fImGuiGamutPaint.setColor(SK_ColorWHITE);
505 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
506
Brian Osman70d2f432017-11-08 09:54:10 -0500507 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500508 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700509}
510
jvanverth34524262016-05-04 13:49:13 -0700511void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400512 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
513 static const struct {
514 const char* fExtension;
515 const char* fDirName;
516 const SkCommandLineFlags::StringArray& fFlags;
517 const SlideFactory fFactory;
518 } gExternalSlidesInfo[] = {
519 { ".skp", "skp-dir", FLAGS_skps,
520 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
521 return sk_make_sp<SKPSlide>(name, path);}
522 },
523 { ".jpg", "jpg-dir", FLAGS_jpgs,
524 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
525 return sk_make_sp<ImageSlide>(name, path);}
526 },
Florin Malita87ccf332018-05-04 12:23:24 -0400527#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400528 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400529 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
530 return sk_make_sp<SkottieSlide>(name, path);}
531 },
Florin Malita87ccf332018-05-04 12:23:24 -0400532#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400533#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400534 { ".svg", "svg-dir", FLAGS_svgs,
535 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
536 return sk_make_sp<SvgSlide>(name, path);}
537 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400538#endif
Ruiqi Maof5101492018-06-29 14:32:21 -0400539#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
540 { ".nima", "nima-dir", FLAGS_nimas,
541 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
542 return sk_make_sp<NIMASlide>(name, path);}
543 },
544#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400545 };
jvanverthc265a922016-04-08 12:51:45 -0700546
Brian Salomon343553a2018-09-05 15:41:23 -0400547 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700548
Florin Malita0ffa3222018-04-05 14:34:45 -0400549 const auto addSlide = [&](const SkString& name,
550 const SkString& path,
551 const SlideFactory& fact) {
552 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
553 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700554 }
liyuqian6f163d22016-06-13 12:26:45 -0700555
Florin Malita0ffa3222018-04-05 14:34:45 -0400556 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500557 dirSlides.push_back(slide);
558 fSlides.push_back(std::move(slide));
559 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400560 };
Florin Malita76a076b2018-02-15 18:40:48 -0500561
Florin Malita38792ce2018-05-08 10:36:18 -0400562 if (!FLAGS_file.isEmpty()) {
563 // single file mode
564 const SkString file(FLAGS_file[0]);
565
566 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
567 for (const auto& sinfo : gExternalSlidesInfo) {
568 if (file.endsWith(sinfo.fExtension)) {
569 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
570 return;
571 }
572 }
573
574 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
575 } else {
576 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
577 }
578
579 return;
580 }
581
582 // Bisect slide.
583 if (!FLAGS_bisect.isEmpty()) {
584 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
585 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
586 if (FLAGS_bisect.count() >= 2) {
587 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
588 bisect->onChar(*ch);
589 }
590 }
591 fSlides.push_back(std::move(bisect));
592 }
593 }
594
595 // GMs
596 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400597 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
598 std::unique_ptr<skiagm::GM> gm(gmFactory(nullptr));
Florin Malita38792ce2018-05-08 10:36:18 -0400599 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
600 sk_sp<Slide> slide(new GMSlide(gm.release()));
601 fSlides.push_back(std::move(slide));
602 }
Florin Malita38792ce2018-05-08 10:36:18 -0400603 }
604 // reverse gms
605 int numGMs = fSlides.count() - firstGM;
606 for (int i = 0; i < numGMs/2; ++i) {
607 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
608 }
609
610 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400611 for (const SampleFactory factory : SampleRegistry::Range()) {
612 sk_sp<Slide> slide(new SampleSlide(factory));
Florin Malita38792ce2018-05-08 10:36:18 -0400613 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
614 fSlides.push_back(slide);
615 }
Florin Malita38792ce2018-05-08 10:36:18 -0400616 }
617
Florin Malita0ffa3222018-04-05 14:34:45 -0400618 for (const auto& info : gExternalSlidesInfo) {
619 for (const auto& flag : info.fFlags) {
620 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
621 // single file
622 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
623 } else {
624 // directory
625 SkOSFile::Iter it(flag.c_str(), info.fExtension);
626 SkString name;
627 while (it.next(&name)) {
628 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
629 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400630 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400631 if (!dirSlides.empty()) {
632 fSlides.push_back(
633 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
634 std::move(dirSlides)));
635 dirSlides.reset();
636 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400637 }
638 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700639}
640
641
jvanverth34524262016-05-04 13:49:13 -0700642Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700643 fWindow->detach();
644 delete fWindow;
645}
646
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500647struct SkPaintTitleUpdater {
648 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
649 void append(const char* s) {
650 if (fCount == 0) {
651 fTitle->append(" {");
652 } else {
653 fTitle->append(", ");
654 }
655 fTitle->append(s);
656 ++fCount;
657 }
658 void done() {
659 if (fCount > 0) {
660 fTitle->append("}");
661 }
662 }
663 SkString* fTitle;
664 int fCount;
665};
666
brianosman05de2162016-05-06 13:28:57 -0700667void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700668 if (!fWindow) {
669 return;
670 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500671 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700672 return; // Surface hasn't been created yet.
673 }
674
jvanverth34524262016-05-04 13:49:13 -0700675 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700676 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700677
Yuqian Li399b3c22017-08-03 11:08:15 -0400678 if (gSkUseDeltaAA) {
679 if (gSkForceDeltaAA) {
680 title.append(" <FDAA>");
681 } else {
682 title.append(" <DAA>");
683 }
684 } else if (gSkUseAnalyticAA) {
685 if (gSkForceAnalyticAA) {
686 title.append(" <FAAA>");
687 } else {
688 title.append(" <AAA>");
689 }
690 }
691
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500692 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner99a78dc2018-05-09 18:23:51 -0400693 auto paintFlag = [this, &paintTitle](SkPaint::Flags flag, bool (SkPaint::* isFlag)() const,
694 const char* on, const char* off)
695 {
696 if (fPaintOverrides.fFlags & flag) {
697 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500698 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400699 };
700
701 paintFlag(SkPaint::kAntiAlias_Flag, &SkPaint::isAntiAlias, "Antialias", "Alias");
702 paintFlag(SkPaint::kDither_Flag, &SkPaint::isDither, "DITHER", "No Dither");
703 paintFlag(SkPaint::kFakeBoldText_Flag, &SkPaint::isFakeBoldText, "Fake Bold", "No Fake Bold");
704 paintFlag(SkPaint::kLinearText_Flag, &SkPaint::isLinearText, "Linear Text", "Non-Linear Text");
705 paintFlag(SkPaint::kSubpixelText_Flag, &SkPaint::isSubpixelText, "Subpixel Text", "Pixel Text");
706 paintFlag(SkPaint::kLCDRenderText_Flag, &SkPaint::isLCDRenderText, "LCD", "lcd");
707 paintFlag(SkPaint::kEmbeddedBitmapText_Flag, &SkPaint::isEmbeddedBitmapText,
708 "Bitmap Text", "No Bitmap Text");
709 paintFlag(SkPaint::kAutoHinting_Flag, &SkPaint::isAutohinted,
710 "Force Autohint", "No Force Autohint");
Ben Wagner99a78dc2018-05-09 18:23:51 -0400711
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500712 if (fPaintOverrides.fHinting) {
Mike Klein8e12cc62018-11-07 23:56:21 +0000713 switch (fPaint.getHinting()) {
714 case SkPaint::kNo_Hinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500715 paintTitle.append("No Hinting");
716 break;
Mike Klein8e12cc62018-11-07 23:56:21 +0000717 case SkPaint::kSlight_Hinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500718 paintTitle.append("Slight Hinting");
719 break;
Mike Klein8e12cc62018-11-07 23:56:21 +0000720 case SkPaint::kNormal_Hinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500721 paintTitle.append("Normal Hinting");
722 break;
Mike Klein8e12cc62018-11-07 23:56:21 +0000723 case SkPaint::kFull_Hinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500724 paintTitle.append("Full Hinting");
725 break;
726 }
727 }
728 paintTitle.done();
729
Brian Osman92004802017-03-06 11:47:26 -0500730 switch (fColorMode) {
731 case ColorMode::kLegacy:
732 title.append(" Legacy 8888");
733 break;
734 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
735 title.append(" ColorManaged 8888 (Nonlinear blending)");
736 break;
737 case ColorMode::kColorManagedSRGB8888:
738 title.append(" ColorManaged 8888");
739 break;
740 case ColorMode::kColorManagedLinearF16:
741 title.append(" ColorManaged F16");
742 break;
743 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500744
Brian Osman92004802017-03-06 11:47:26 -0500745 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500746 int curPrimaries = -1;
747 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
748 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
749 curPrimaries = i;
750 break;
751 }
752 }
753 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500754
755 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
756 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
757 }
brianosman05de2162016-05-06 13:28:57 -0700758 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500759
Ben Wagner37c54032018-04-13 14:30:23 -0400760 const DisplayParams& params = fWindow->getRequestedDisplayParams();
761 if (fPixelGeometryOverrides) {
762 switch (params.fSurfaceProps.pixelGeometry()) {
763 case kUnknown_SkPixelGeometry:
764 title.append( " Flat");
765 break;
766 case kRGB_H_SkPixelGeometry:
767 title.append( " RGB");
768 break;
769 case kBGR_H_SkPixelGeometry:
770 title.append( " BGR");
771 break;
772 case kRGB_V_SkPixelGeometry:
773 title.append( " RGBV");
774 break;
775 case kBGR_V_SkPixelGeometry:
776 title.append( " BGRV");
777 break;
778 }
779 }
780
781 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
782 title.append(" DFT");
783 }
784
csmartdalton578f0642017-02-24 16:04:47 -0700785 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700786 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500787 int msaa = fWindow->sampleCount();
788 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700789 title.appendf(" MSAA: %i", msaa);
790 }
791 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700792
793 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman1994f202018-07-10 11:41:22 -0400794 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700795 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
796 }
797
Brian Osman805a7272018-05-02 15:40:20 -0400798 if (kPerspective_Real == fPerspectiveMode) {
799 title.append(" Perpsective (Real)");
800 } else if (kPerspective_Fake == fPerspectiveMode) {
801 title.append(" Perspective (Fake)");
802 }
803
brianosman05de2162016-05-06 13:28:57 -0700804 fWindow->setTitle(title.c_str());
805}
806
Florin Malitaab99c342018-01-16 16:23:03 -0500807int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500808
809 if (!FLAGS_slide.isEmpty()) {
810 int count = fSlides.count();
811 for (int i = 0; i < count; i++) {
812 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500813 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500814 }
815 }
816
817 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
818 this->listNames();
819 }
820
Florin Malitaab99c342018-01-16 16:23:03 -0500821 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500822}
823
Florin Malitaab99c342018-01-16 16:23:03 -0500824void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500825 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500826 for (const auto& slide : fSlides) {
827 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500828 }
829}
830
Florin Malitaab99c342018-01-16 16:23:03 -0500831void Viewer::setCurrentSlide(int slide) {
832 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700833
Florin Malitaab99c342018-01-16 16:23:03 -0500834 if (slide == fCurrentSlide) {
835 return;
836 }
837
838 if (fCurrentSlide >= 0) {
839 fSlides[fCurrentSlide]->unload();
840 }
841
842 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
843 SkIntToScalar(fWindow->height()));
844 fCurrentSlide = slide;
845 this->setupCurrentSlide();
846}
847
848void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500849 if (fCurrentSlide >= 0) {
850 // prepare dimensions for image slides
851 fGesture.resetTouchState();
852 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700853
Jim Van Verth0848fb02018-01-22 13:39:30 -0500854 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
855 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
856 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400857
Jim Van Verth0848fb02018-01-22 13:39:30 -0500858 // Start with a matrix that scales the slide to the available screen space
859 if (fWindow->scaleContentToFit()) {
860 if (windowRect.width() > 0 && windowRect.height() > 0) {
861 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
862 }
liyuqiane46e4f02016-05-20 07:32:19 -0700863 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500864
865 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400866 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500867
868 this->updateTitle();
869 this->updateUIState();
870
871 fStatsLayer.resetMeasurements();
872
873 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700874 }
jvanverthc265a922016-04-08 12:51:45 -0700875}
876
877#define MAX_ZOOM_LEVEL 8
878#define MIN_ZOOM_LEVEL -8
879
jvanverth34524262016-05-04 13:49:13 -0700880void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700881 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400882 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400883 this->preTouchMatrixChanged();
884}
Yuqian Li755778c2018-03-28 16:23:31 -0400885
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400886void Viewer::preTouchMatrixChanged() {
887 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400888 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
889 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
890 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
891 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
892}
893
Brian Osman805a7272018-05-02 15:40:20 -0400894SkMatrix Viewer::computePerspectiveMatrix() {
895 SkScalar w = fWindow->width(), h = fWindow->height();
896 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
897 SkPoint perspPts[4] = {
898 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
899 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
900 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
901 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
902 };
903 SkMatrix m;
904 m.setPolyToPoly(orthoPts, perspPts, 4);
905 return m;
906}
907
Yuqian Li755778c2018-03-28 16:23:31 -0400908SkMatrix Viewer::computePreTouchMatrix() {
909 SkMatrix m = fDefaultMatrix;
910 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
911 : SK_Scalar1 + fZoomLevel;
Ben Wagner897dfa22018-08-09 15:18:46 -0400912 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -0400913 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400914
915 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
916 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400917
Brian Osman805a7272018-05-02 15:40:20 -0400918 if (kPerspective_Real == fPerspectiveMode) {
919 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400920 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400921 }
922
Yuqian Li755778c2018-03-28 16:23:31 -0400923 return m;
jvanverthc265a922016-04-08 12:51:45 -0700924}
925
liyuqiand3cdbca2016-05-17 12:44:20 -0700926SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400927 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700928 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400929 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700930 return m;
jvanverthc265a922016-04-08 12:51:45 -0700931}
932
Brian Osman621491e2017-02-28 15:45:01 -0500933void Viewer::setBackend(sk_app::Window::BackendType backendType) {
934 fBackendType = backendType;
935
936 fWindow->detach();
937
Brian Osman70d2f432017-11-08 09:54:10 -0500938#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400939 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
940 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500941 DisplayParams params = fWindow->getRequestedDisplayParams();
942 delete fWindow;
943 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500944
Brian Osman70d2f432017-11-08 09:54:10 -0500945 // re-register callbacks
946 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500947 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500948 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500949 fWindow->pushLayer(&fImGuiLayer);
950
Brian Osman70d2f432017-11-08 09:54:10 -0500951 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
952 // will still include our correct sample count. But the re-created fWindow will lose that
953 // information. On Windows, we need to re-create the window when changing sample count,
954 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
955 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
956 // as if we had never changed windows at all).
957 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500958#endif
959
Brian Osman70d2f432017-11-08 09:54:10 -0500960 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500961}
962
Brian Osman92004802017-03-06 11:47:26 -0500963void Viewer::setColorMode(ColorMode colorMode) {
964 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700965
Brian Osmanf750fbc2017-02-08 10:47:28 -0500966 // When we're in color managed mode, we tag our window surface as sRGB. If we've switched into
Brian Osmane0d4fba2017-03-15 10:24:55 -0400967 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700968 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500969 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400970 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
971 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500972 if (wasInLegacy != wantLegacy) {
973 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700974 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500975 }
976
977 this->updateTitle();
978 fWindow->inval();
979}
980
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500981class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
982public:
983 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
984 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
985 { }
Ben Wagner41e40472018-09-24 13:01:54 -0400986 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
987 sk_sp<SkTextBlob>* cache) {
988 bool blobWillChange = false;
989 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
990 SkPaint blobPaint = paint;
991 it.applyFontToPaint(&blobPaint);
992 SkTCopyOnFirstWrite<SkPaint> filteredPaint(blobPaint);
993 bool shouldDraw = this->onFilter(&filteredPaint, kTextBlob_Type);
994 if (blobPaint != *filteredPaint || !shouldDraw) {
995 blobWillChange = true;
996 break;
997 }
998 }
999 if (!blobWillChange) {
1000 return blob;
1001 }
1002
1003 SkTextBlobBuilder builder;
1004 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
1005 SkPaint blobPaint = paint;
1006 it.applyFontToPaint(&blobPaint);
1007 SkTCopyOnFirstWrite<SkPaint> filteredPaint(blobPaint);
1008 bool shouldDraw = this->onFilter(&filteredPaint, kTextBlob_Type);
1009 if (!shouldDraw) {
1010 continue;
1011 }
1012
1013 const SkTextBlobBuilder::RunBuffer& runBuffer
1014 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
1015 ? SkTextBlobBuilderPriv::AllocRunText(&builder, *filteredPaint,
1016 it.offset().x(),it.offset().y(), it.glyphCount(), it.textSize(), SkString())
1017 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
1018 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, *filteredPaint,
1019 it.offset().y(), it.glyphCount(), it.textSize(), SkString())
1020 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
1021 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, *filteredPaint,
1022 it.glyphCount(), it.textSize(), SkString())
1023 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1024 uint32_t glyphCount = it.glyphCount();
1025 if (it.glyphs()) {
1026 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1027 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1028 }
1029 if (it.pos()) {
1030 size_t posSize = sizeof(decltype(*it.pos()));
1031 uint8_t positioning = it.positioning();
1032 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1033 }
1034 if (it.text()) {
1035 size_t textSize = sizeof(decltype(*it.text()));
1036 uint32_t textCount = it.textSize();
1037 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1038 }
1039 if (it.clusters()) {
1040 size_t clusterSize = sizeof(decltype(*it.clusters()));
1041 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1042 }
1043 }
1044 *cache = builder.make();
1045 return cache->get();
1046 }
1047 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1048 const SkPaint& paint) override {
1049 sk_sp<SkTextBlob> cache;
1050 this->SkPaintFilterCanvas::onDrawTextBlob(
1051 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1052 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001053 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001054 if (*paint == nullptr) {
1055 return true;
1056 }
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001057 if (fPaintOverrides->fTextSize) {
1058 paint->writable()->setTextSize(fPaint->getTextSize());
1059 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001060 if (fPaintOverrides->fHinting) {
1061 paint->writable()->setHinting(fPaint->getHinting());
1062 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001063
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001064 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
1065 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1066 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001067 if (fPaintOverrides->fFlags & SkPaint::kDither_Flag) {
1068 paint->writable()->setDither(fPaint->isDither());
1069 }
1070 if (fPaintOverrides->fFlags & SkPaint::kFakeBoldText_Flag) {
1071 paint->writable()->setFakeBoldText(fPaint->isFakeBoldText());
1072 }
1073 if (fPaintOverrides->fFlags & SkPaint::kLinearText_Flag) {
1074 paint->writable()->setLinearText(fPaint->isLinearText());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001075 }
1076 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
1077 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
1078 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001079 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
1080 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
1081 }
1082 if (fPaintOverrides->fFlags & SkPaint::kEmbeddedBitmapText_Flag) {
1083 paint->writable()->setEmbeddedBitmapText(fPaint->isEmbeddedBitmapText());
1084 }
1085 if (fPaintOverrides->fFlags & SkPaint::kAutoHinting_Flag) {
1086 paint->writable()->setAutohinted(fPaint->isAutohinted());
1087 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001088
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001089 return true;
1090 }
1091 SkPaint* fPaint;
1092 Viewer::SkPaintFields* fPaintOverrides;
1093};
1094
Brian Osmanf750fbc2017-02-08 10:47:28 -05001095void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001096 SkAutoCanvasRestore autorestore(canvas, false);
1097
Brian Osmanf750fbc2017-02-08 10:47:28 -05001098 // By default, we render directly into the window's surface/canvas
1099 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001100 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001101
Brian Osmane0d4fba2017-03-15 10:24:55 -04001102 // If we're in any of the color managed modes, construct the color space we're going to use
1103 sk_sp<SkColorSpace> cs = nullptr;
1104 if (ColorMode::kLegacy != fColorMode) {
1105 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
1106 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Klein4429a4f2018-10-04 09:06:00 -04001107 SkMatrix44 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001108 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -05001109 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1110 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
1111 } else {
1112 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
1113 }
Brian Osmane0d4fba2017-03-15 10:24:55 -04001114 }
1115
Brian Osman3ac99cf2017-12-01 11:23:53 -05001116 if (fSaveToSKP) {
1117 SkPictureRecorder recorder;
1118 SkCanvas* recorderCanvas = recorder.beginRecording(
1119 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1120 // In xform-canvas mode, record the transformed output
1121 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1122 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1123 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
1124 recorderCanvas = xformCanvas.get();
1125 }
1126 fSlides[fCurrentSlide]->draw(recorderCanvas);
1127 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1128 SkFILEWStream stream("sample_app.skp");
1129 picture->serialize(&stream);
1130 fSaveToSKP = false;
1131 }
1132
Brian Osmane0d4fba2017-03-15 10:24:55 -04001133 // If we're in F16, or we're zooming, or we're in color correct 8888 and the gamut isn't sRGB,
Brian Osman70d2f432017-11-08 09:54:10 -05001134 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
Brian Osman805a7272018-05-02 15:40:20 -04001135 // because the window surface is actually GL, or we're doing fake perspective.
Brian Osmanf750fbc2017-02-08 10:47:28 -05001136 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -05001137 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -05001138 if (Window::kRaster_BackendType == fBackendType ||
Brian Osman805a7272018-05-02 15:40:20 -04001139 kPerspective_Fake == fPerspectiveMode ||
Brian Osman70d2f432017-11-08 09:54:10 -05001140 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -05001141 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -04001142 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
1143 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
1144
Brian Osman92004802017-03-06 11:47:26 -05001145 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
1146 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001147 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
1148 // with a special canvas (below) that has the color space attached
1149 sk_sp<SkColorSpace> offscreenColorSpace =
1150 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -05001151 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -04001152 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Ben Wagner37c54032018-04-13 14:30:23 -04001153 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1154 canvas->getProps(&props);
1155 offscreenSurface = Window::kRaster_BackendType == fBackendType
1156 ? SkSurface::MakeRaster(info, &props)
1157 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -05001158 SkPixmap offscreenPixmap;
Mike Klein48b64902018-07-25 13:28:44 -04001159 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001160 }
1161
Brian Osmane0d4fba2017-03-15 10:24:55 -04001162 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1163 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1164 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
1165 slideCanvas = xformCanvas.get();
1166 }
1167
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001168 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001169 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001170 slideCanvas->concat(computeMatrix());
Brian Osmane19e23f2018-05-22 16:56:40 -04001171 if (kPerspective_Real == fPerspectiveMode) {
1172 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1173 }
Brian Osman1df161a2017-02-09 12:10:20 -05001174 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001175 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001176 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1177 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001178 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001179 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001180
1181 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001182 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001183 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001184 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001185
1186 // If we rendered offscreen, snap an image and push the results to the window's canvas
1187 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001188 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001189
1190 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -04001191 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001192 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001193 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001194 SkPaint paint;
1195 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001196 int prePerspectiveCount = canvas->save();
1197 if (kPerspective_Fake == fPerspectiveMode) {
1198 paint.setFilterQuality(kHigh_SkFilterQuality);
1199 canvas->clear(SK_ColorWHITE);
1200 canvas->concat(this->computePerspectiveMatrix());
1201 }
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001202 canvas->drawImage(retaggedImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001203 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001204 }
liyuqian6f163d22016-06-13 12:26:45 -07001205}
1206
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001207void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001208 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001209 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001210}
Jim Van Verth6f449692017-02-14 15:16:46 -05001211
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001212void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001213 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001214
brianosman622c8d52016-05-10 06:50:49 -07001215 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001216
Brian Osmand67e5182017-12-08 16:46:09 -05001217 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001218
1219 if (GrContext* ctx = fWindow->getGrContext()) {
1220 // Clean out cache items that haven't been used in more than 10 seconds.
1221 ctx->performDeferredCleanup(std::chrono::seconds(10));
1222 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001223}
1224
Ben Wagnera1915972018-08-09 15:06:19 -04001225void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001226 if (fCurrentSlide >= 0) {
1227 fSlides[fCurrentSlide]->resize(width, height);
1228 }
Ben Wagnera1915972018-08-09 15:06:19 -04001229}
1230
Florin Malitacefc1b92018-02-19 21:43:47 -05001231SkPoint Viewer::mapEvent(float x, float y) {
1232 const auto m = this->computeMatrix();
1233 SkMatrix inv;
1234
1235 SkAssertResult(m.invert(&inv));
1236
1237 return inv.mapXY(x, y);
1238}
1239
jvanverth814e38d2016-06-06 08:48:47 -07001240bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001241 if (GestureDevice::kMouse == fGestureDevice) {
1242 return false;
1243 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001244
1245 const auto slidePt = this->mapEvent(x, y);
1246 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1247 fWindow->inval();
1248 return true;
1249 }
1250
liyuqiand3cdbca2016-05-17 12:44:20 -07001251 void* castedOwner = reinterpret_cast<void*>(owner);
1252 switch (state) {
1253 case Window::kUp_InputState: {
1254 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001255#if defined(SK_BUILD_FOR_IOS)
1256 // TODO: move IOS swipe detection higher up into the platform code
1257 SkPoint dir;
1258 if (fGesture.isFling(&dir)) {
1259 // swiping left or right
1260 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1261 if (dir.fX < 0) {
1262 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1263 fCurrentSlide + 1 : 0);
1264 } else {
1265 this->setCurrentSlide(fCurrentSlide > 0 ?
1266 fCurrentSlide - 1 : fSlides.count() - 1);
1267 }
1268 }
1269 fGesture.reset();
1270 }
1271#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001272 break;
1273 }
1274 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001275 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001276 break;
1277 }
1278 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001279 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001280 break;
1281 }
1282 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001283 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001284 fWindow->inval();
1285 return true;
1286}
1287
Brian Osman80fc07e2017-12-08 16:45:43 -05001288bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001289 if (GestureDevice::kTouch == fGestureDevice) {
1290 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001291 }
Brian Osman16c81a12017-12-20 11:58:34 -05001292
Florin Malitacefc1b92018-02-19 21:43:47 -05001293 const auto slidePt = this->mapEvent(x, y);
1294 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1295 fWindow->inval();
1296 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001297 }
1298
1299 switch (state) {
1300 case Window::kUp_InputState: {
1301 fGesture.touchEnd(nullptr);
1302 break;
1303 }
1304 case Window::kDown_InputState: {
1305 fGesture.touchBegin(nullptr, x, y);
1306 break;
1307 }
1308 case Window::kMove_InputState: {
1309 fGesture.touchMoved(nullptr, x, y);
1310 break;
1311 }
1312 }
1313 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1314
1315 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1316 fWindow->inval();
1317 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001318 return true;
1319}
1320
Brian Osmana109e392017-02-24 09:49:14 -05001321static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1322 const ImVec2& pos, const ImVec2& size) {
1323 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1324 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1325
1326 // Invisible 10x10 button
1327 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1328 ImGui::InvisibleButton(label, ImVec2(10, 10));
1329
1330 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1331 ImGuiIO& io = ImGui::GetIO();
1332 // Normalized mouse position, relative to our gamut box
1333 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1334 // Clamp to edge of box, convert back to primary scale
1335 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1336 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1337 }
1338
1339 if (ImGui::IsItemHovered()) {
1340 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1341 }
1342
1343 // Return screen coordinates for the caller. We could just return center here, but we'd have
1344 // one frame of lag during drag.
1345 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1346}
1347
1348static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1349 ImDrawList* drawList = ImGui::GetWindowDrawList();
1350
1351 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1352 // width, and scale the height to maintain aspect ratio.
1353 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1354 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1355 ImVec2 pos = ImGui::GetCursorScreenPos();
1356
1357 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1358 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1359 // Magic numbers are pixel locations of the origin and upper-right corner.
1360 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1361 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001362
1363 // Primary markers
1364 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1365 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1366 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1367 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1368
1369 // Gamut triangle
1370 drawList->AddCircle(r, 5.0f, 0xFF000040);
1371 drawList->AddCircle(g, 5.0f, 0xFF004000);
1372 drawList->AddCircle(b, 5.0f, 0xFF400000);
1373 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1374 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1375
1376 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001377 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1378}
1379
1380static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1381 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1382 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1383 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1384
1385 // Invisible 10x10 button
1386 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1387 ImGui::InvisibleButton(label, ImVec2(10, 10));
1388
1389 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1390 ImGuiIO& io = ImGui::GetIO();
1391 // Normalized mouse position, relative to our gamut box
1392 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1393 // Clamp to edge of box
1394 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1395 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1396 *dragging = true;
1397 }
1398
1399 // Return screen coordinates for the caller. We could just return center here, but we'd have
1400 // one frame of lag during drag.
1401 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1402}
1403
Ben Wagner3627d2e2018-06-26 14:23:20 -04001404static bool ImGui_DragLocation(SkPoint* pt) {
1405 ImDrawList* drawList = ImGui::GetWindowDrawList();
1406
1407 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1408 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1409 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1410 ImVec2 pos = ImGui::GetCursorScreenPos();
1411
1412 // Background rectangle
1413 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1414
1415 // Location marker
1416 bool dragging = false;
1417 ImVec2 tl = ImGui_DragPoint("SL", pt + 0, pos, size, &dragging);
1418 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1419
1420 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1421 ImGui::Spacing();
1422
1423 return dragging;
1424}
1425
Brian Osman9bb47cf2018-04-26 15:55:00 -04001426static bool ImGui_DragQuad(SkPoint* pts) {
1427 ImDrawList* drawList = ImGui::GetWindowDrawList();
1428
1429 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1430 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1431 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1432 ImVec2 pos = ImGui::GetCursorScreenPos();
1433
1434 // Background rectangle
1435 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1436
1437 // Corner markers
1438 bool dragging = false;
1439 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1440 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1441 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1442 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1443
1444 // Draw markers and quad
1445 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1446 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1447 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1448 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1449 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1450 drawList->AddLine(tr, br, 0xFFFFFFFF);
1451 drawList->AddLine(br, bl, 0xFFFFFFFF);
1452 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1453
1454 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1455 ImGui::Spacing();
1456
1457 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001458}
1459
Brian Osmand67e5182017-12-08 16:46:09 -05001460void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001461 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1462 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001463 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001464 }
1465
1466 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001467 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1468 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001469 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001470 DisplayParams params = fWindow->getRequestedDisplayParams();
1471 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001472 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1473 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001474 if (ImGui::CollapsingHeader("Backend")) {
1475 int newBackend = static_cast<int>(fBackendType);
1476 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1477 ImGui::SameLine();
1478 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001479#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1480 ImGui::SameLine();
1481 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1482#endif
Brian Osman621491e2017-02-28 15:45:01 -05001483#if defined(SK_VULKAN)
1484 ImGui::SameLine();
1485 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1486#endif
1487 if (newBackend != fBackendType) {
1488 fDeferredActions.push_back([=]() {
1489 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1490 });
1491 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001492
Brian Salomon99a33902017-03-07 15:16:34 -05001493 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001494 bool* wire = &params.fGrContextOptions.fWireframeMode;
1495 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1496 paramsChanged = true;
1497 }
Brian Salomon99a33902017-03-07 15:16:34 -05001498
Brian Osman28b12522017-03-08 17:10:24 -05001499 if (ctx) {
1500 int sampleCount = fWindow->sampleCount();
1501 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001502 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001503 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1504 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1505 ImGui::RadioButton("16", &sampleCount, 16);
1506
1507 if (sampleCount != params.fMSAASampleCount) {
1508 params.fMSAASampleCount = sampleCount;
1509 paramsChanged = true;
1510 }
1511 }
1512
Ben Wagner37c54032018-04-13 14:30:23 -04001513 int pixelGeometryIdx = 0;
1514 if (fPixelGeometryOverrides) {
1515 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1516 }
1517 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1518 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1519 {
1520 uint32_t flags = params.fSurfaceProps.flags();
1521 if (pixelGeometryIdx == 0) {
1522 fPixelGeometryOverrides = false;
1523 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1524 } else {
1525 fPixelGeometryOverrides = true;
1526 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1527 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1528 }
1529 paramsChanged = true;
1530 }
1531
1532 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1533 if (ImGui::Checkbox("DFT", &useDFT)) {
1534 uint32_t flags = params.fSurfaceProps.flags();
1535 if (useDFT) {
1536 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1537 } else {
1538 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1539 }
1540 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1541 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1542 paramsChanged = true;
1543 }
1544
Brian Osman8a9de3d2017-03-01 14:59:05 -05001545 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001546 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001547 auto prButton = [&](GpuPathRenderers x) {
1548 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001549 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1550 params.fGrContextOptions.fGpuPathRenderers = x;
1551 paramsChanged = true;
1552 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001553 }
1554 };
1555
1556 if (!ctx) {
1557 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001558 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001559 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001560 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001561 prButton(GpuPathRenderers::kStencilAndCover);
1562 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001563 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001564 prButton(GpuPathRenderers::kNone);
1565 } else {
1566 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001567 if (GrCoverageCountingPathRenderer::IsSupported(
1568 *ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001569 prButton(GpuPathRenderers::kCoverageCounting);
1570 }
Jim Van Verth83010462017-03-16 08:45:39 -04001571 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001572 prButton(GpuPathRenderers::kTessellating);
1573 prButton(GpuPathRenderers::kNone);
1574 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001575 ImGui::TreePop();
1576 }
Brian Osman621491e2017-02-28 15:45:01 -05001577 }
1578
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001579 if (ImGui::CollapsingHeader("Transform")) {
1580 float zoom = fZoomLevel;
1581 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1582 fZoomLevel = zoom;
1583 this->preTouchMatrixChanged();
1584 paramsChanged = true;
1585 }
1586 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001587 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001588 fRotation = deg;
1589 this->preTouchMatrixChanged();
1590 paramsChanged = true;
1591 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001592 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1593 if (ImGui_DragLocation(&fOffset)) {
1594 this->preTouchMatrixChanged();
1595 paramsChanged = true;
1596 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001597 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1598 this->preTouchMatrixChanged();
1599 paramsChanged = true;
1600 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001601 }
Brian Osman805a7272018-05-02 15:40:20 -04001602 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1603 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1604 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001605 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001606 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001607 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001608 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001609 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001610 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001611 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001612 }
1613
Ben Wagnera580fb32018-04-17 11:16:32 -04001614 if (ImGui::CollapsingHeader("Paint")) {
1615 int hintingIdx = 0;
1616 if (fPaintOverrides.fHinting) {
Mike Klein8e12cc62018-11-07 23:56:21 +00001617 hintingIdx = fPaint.getHinting() + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001618 }
1619 if (ImGui::Combo("Hinting", &hintingIdx,
1620 "Default\0None\0Slight\0Normal\0Full\0\0"))
1621 {
1622 if (hintingIdx == 0) {
1623 fPaintOverrides.fHinting = false;
Mike Klein8e12cc62018-11-07 23:56:21 +00001624 fPaint.setHinting(SkPaint::kNo_Hinting);
Ben Wagnera580fb32018-04-17 11:16:32 -04001625 } else {
1626 fPaintOverrides.fHinting = true;
Mike Klein8e12cc62018-11-07 23:56:21 +00001627 SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001628 fPaint.setHinting(hinting);
1629 }
1630 paramsChanged = true;
1631 }
1632
1633 int aliasIdx = 0;
1634 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1635 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1636 }
1637 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1638 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1639 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1640 {
1641 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1642 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1643 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1644 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1645 if (aliasIdx == 0) {
1646 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1647 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1648 } else {
1649 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1650 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1651 fPaint.setAntiAlias(aliasIdx > 1);
1652 switch (fPaintOverrides.fAntiAlias) {
1653 case SkPaintFields::AntiAliasState::Alias:
1654 break;
1655 case SkPaintFields::AntiAliasState::Normal:
1656 break;
1657 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1658 gSkUseAnalyticAA = true;
1659 gSkForceAnalyticAA = false;
1660 gSkUseDeltaAA = gSkForceDeltaAA = false;
1661 break;
1662 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1663 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1664 gSkUseDeltaAA = gSkForceDeltaAA = false;
1665 break;
1666 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1667 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1668 gSkUseDeltaAA = true;
1669 gSkForceDeltaAA = false;
1670 break;
1671 case SkPaintFields::AntiAliasState::DeltaAAForced:
1672 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1673 gSkUseDeltaAA = gSkForceDeltaAA = true;
1674 break;
1675 }
1676 }
1677 paramsChanged = true;
1678 }
1679
Ben Wagner99a78dc2018-05-09 18:23:51 -04001680 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
1681 SkPaint::Flags flag,
1682 bool (SkPaint::* isFlag)() const,
1683 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001684 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001685 int itemIndex = 0;
1686 if (fPaintOverrides.fFlags & flag) {
1687 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001688 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001689 if (ImGui::Combo(label, &itemIndex, items)) {
1690 if (itemIndex == 0) {
1691 fPaintOverrides.fFlags &= ~flag;
1692 } else {
1693 fPaintOverrides.fFlags |= flag;
1694 (fPaint.*setFlag)(itemIndex == 2);
1695 }
1696 paramsChanged = true;
1697 }
1698 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001699
Ben Wagner99a78dc2018-05-09 18:23:51 -04001700 paintFlag("Dither",
1701 "Default\0No Dither\0Dither\0\0",
1702 SkPaint::kDither_Flag,
1703 &SkPaint::isDither, &SkPaint::setDither);
1704
1705 paintFlag("Fake Bold Glyphs",
1706 "Default\0No Fake Bold\0Fake Bold\0\0",
1707 SkPaint::kFakeBoldText_Flag,
1708 &SkPaint::isFakeBoldText, &SkPaint::setFakeBoldText);
1709
1710 paintFlag("Linear Text",
1711 "Default\0No Linear Text\0Linear Text\0\0",
1712 SkPaint::kLinearText_Flag,
1713 &SkPaint::isLinearText, &SkPaint::setLinearText);
1714
1715 paintFlag("Subpixel Position Glyphs",
1716 "Default\0Pixel Text\0Subpixel Text\0\0",
1717 SkPaint::kSubpixelText_Flag,
1718 &SkPaint::isSubpixelText, &SkPaint::setSubpixelText);
1719
1720 paintFlag("Subpixel Anti-Alias",
1721 "Default\0lcd\0LCD\0\0",
1722 SkPaint::kLCDRenderText_Flag,
1723 &SkPaint::isLCDRenderText, &SkPaint::setLCDRenderText);
1724
1725 paintFlag("Embedded Bitmap Text",
1726 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1727 SkPaint::kEmbeddedBitmapText_Flag,
1728 &SkPaint::isEmbeddedBitmapText, &SkPaint::setEmbeddedBitmapText);
1729
1730 paintFlag("Force Auto-Hinting",
1731 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1732 SkPaint::kAutoHinting_Flag,
1733 &SkPaint::isAutohinted, &SkPaint::setAutohinted);
1734
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001735 ImGui::Checkbox("Override TextSize", &fPaintOverrides.fTextSize);
1736 if (fPaintOverrides.fTextSize) {
1737 ImGui::DragFloat2("TextRange", fPaintOverrides.fTextSizeRange,
1738 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
1739 float textSize = fPaint.getTextSize();
1740 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
1741 fPaintOverrides.fTextSizeRange[0],
1742 fPaintOverrides.fTextSizeRange[1],
1743 "%.6f", 2.0f))
1744 {
1745 fPaint.setTextSize(textSize);
1746 this->preTouchMatrixChanged();
1747 paramsChanged = true;
1748 }
1749 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001750 }
1751
Mike Reed81f60ec2018-05-15 10:09:52 -04001752 {
1753 SkMetaData controls;
1754 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1755 if (ImGui::CollapsingHeader("Current Slide")) {
1756 SkMetaData::Iter iter(controls);
1757 const char* name;
1758 SkMetaData::Type type;
1759 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001760 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001761 if (type == SkMetaData::kScalar_Type) {
1762 float val[3];
1763 SkASSERT(count == 3);
1764 controls.findScalars(name, &count, val);
1765 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1766 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001767 }
1768 }
1769 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001770 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001771 }
1772 }
1773 }
1774
Ben Wagner7a3c6742018-04-23 10:01:07 -04001775 if (fShowSlidePicker) {
1776 ImGui::SetNextTreeNodeOpen(true);
1777 }
Brian Osman79086b92017-02-10 13:36:16 -05001778 if (ImGui::CollapsingHeader("Slide")) {
1779 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001780 static ImVector<const char*> filteredSlideNames;
1781 static ImVector<int> filteredSlideIndices;
1782
Brian Osmanfce09c52017-11-14 15:32:20 -05001783 if (fShowSlidePicker) {
1784 ImGui::SetKeyboardFocusHere();
1785 fShowSlidePicker = false;
1786 }
1787
Brian Osman79086b92017-02-10 13:36:16 -05001788 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001789 filteredSlideNames.clear();
1790 filteredSlideIndices.clear();
1791 int filteredIndex = 0;
1792 for (int i = 0; i < fSlides.count(); ++i) {
1793 const char* slideName = fSlides[i]->getName().c_str();
1794 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1795 if (i == fCurrentSlide) {
1796 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001797 }
Brian Osmanf479e422017-11-08 13:11:36 -05001798 filteredSlideNames.push_back(slideName);
1799 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001800 }
Brian Osman79086b92017-02-10 13:36:16 -05001801 }
Brian Osmanf479e422017-11-08 13:11:36 -05001802
Brian Osmanf479e422017-11-08 13:11:36 -05001803 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1804 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001805 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001806 }
1807 }
Brian Osmana109e392017-02-24 09:49:14 -05001808
1809 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001810 ColorMode newMode = fColorMode;
1811 auto cmButton = [&](ColorMode mode, const char* label) {
1812 if (ImGui::RadioButton(label, mode == fColorMode)) {
1813 newMode = mode;
1814 }
1815 };
1816
1817 cmButton(ColorMode::kLegacy, "Legacy 8888");
1818 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1819 "Color Managed 8888 (Nonlinear blending)");
1820 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1821 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1822
1823 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001824 // It isn't safe to switch color mode now (in the middle of painting). We might
1825 // tear down the back-end, etc... Defer this change until the next onIdle.
1826 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001827 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001828 });
Brian Osmana109e392017-02-24 09:49:14 -05001829 }
1830
1831 // Pick from common gamuts:
1832 int primariesIdx = 4; // Default: Custom
1833 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1834 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1835 primariesIdx = i;
1836 break;
1837 }
1838 }
1839
Brian Osmanfdab5762017-11-09 10:27:55 -05001840 // When we're in xform canvas mode, we can alter the transfer function, too
1841 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1842 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1843 }
1844
Brian Osmana109e392017-02-24 09:49:14 -05001845 if (ImGui::Combo("Primaries", &primariesIdx,
1846 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1847 if (primariesIdx >= 0 && primariesIdx <= 3) {
1848 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1849 }
1850 }
1851
1852 // Allow direct editing of gamut
1853 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1854 }
Brian Osman79086b92017-02-10 13:36:16 -05001855 }
Brian Salomon99a33902017-03-07 15:16:34 -05001856 if (paramsChanged) {
1857 fDeferredActions.push_back([=]() {
1858 fWindow->setRequestedDisplayParams(params);
1859 fWindow->inval();
1860 this->updateTitle();
1861 });
1862 }
Brian Osman79086b92017-02-10 13:36:16 -05001863 ImGui::End();
1864 }
1865
Brian Osmanf6877092017-02-13 09:39:57 -05001866 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04001867 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
1868 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001869 static int zoomFactor = 8;
1870 if (ImGui::Button("<<")) {
1871 zoomFactor = SkTMax(zoomFactor / 2, 4);
1872 }
1873 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1874 if (ImGui::Button(">>")) {
1875 zoomFactor = SkTMin(zoomFactor * 2, 32);
1876 }
Brian Osmanf6877092017-02-13 09:39:57 -05001877
Ben Wagner3627d2e2018-06-26 14:23:20 -04001878 if (!fZoomWindowFixed) {
1879 ImVec2 mousePos = ImGui::GetMousePos();
1880 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
1881 }
1882 SkScalar x = fZoomWindowLocation.x();
1883 SkScalar y = fZoomWindowLocation.y();
1884 int xInt = SkScalarRoundToInt(x);
1885 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05001886 ImVec2 avail = ImGui::GetContentRegionAvail();
1887
Brian Osmanead517d2017-11-13 15:36:36 -05001888 uint32_t pixel = 0;
1889 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001890 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001891 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001892 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
Ben Wagner3627d2e2018-06-26 14:23:20 -04001893 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05001894 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001895 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1896 }
1897
Brian Osmand67e5182017-12-08 16:46:09 -05001898 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001899 // Translate so the region of the image that's under the mouse cursor is centered
1900 // in the zoom canvas:
1901 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001902 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
1903 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05001904 c->drawImage(this->fLastImage, 0, 0);
1905
1906 SkPaint outline;
1907 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001908 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05001909 });
Brian Osmanf6877092017-02-13 09:39:57 -05001910 }
1911
1912 ImGui::End();
1913 }
Brian Osman79086b92017-02-10 13:36:16 -05001914}
1915
liyuqian2edb0f42016-07-06 14:11:32 -07001916void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001917 for (int i = 0; i < fDeferredActions.count(); ++i) {
1918 fDeferredActions[i]();
1919 }
1920 fDeferredActions.reset();
1921
Brian Osman56a24812017-12-19 11:15:16 -05001922 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001923 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001924 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001925 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001926
Brian Osman79086b92017-02-10 13:36:16 -05001927 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04001928 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
1929 // not be visible, though. So we need to redraw if there is at least one visible window, or
1930 // more than one active window. Newly created windows are active but not visible for one frame
1931 // while they determine their layout and sizing.
1932 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
1933 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07001934 fWindow->inval();
1935 }
jvanverth9f372462016-04-06 06:08:59 -07001936}
liyuqiane5a6cd92016-05-27 08:52:52 -07001937
Florin Malitab632df72018-06-18 21:23:06 -04001938template <typename OptionsFunc>
1939static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
1940 OptionsFunc&& optionsFunc) {
1941 writer.beginObject();
1942 {
1943 writer.appendString(kName , name);
1944 writer.appendString(kValue, value);
1945
1946 writer.beginArray(kOptions);
1947 {
1948 optionsFunc(writer);
1949 }
1950 writer.endArray();
1951 }
1952 writer.endObject();
1953}
1954
1955
liyuqiane5a6cd92016-05-27 08:52:52 -07001956void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001957 if (!fWindow) {
1958 return;
1959 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001960 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001961 return; // Surface hasn't been created yet.
1962 }
1963
Florin Malitab632df72018-06-18 21:23:06 -04001964 SkDynamicMemoryWStream memStream;
1965 SkJSONWriter writer(&memStream);
1966 writer.beginArray();
1967
liyuqianb73c24b2016-06-03 08:47:23 -07001968 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04001969 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
1970 [this](SkJSONWriter& writer) {
1971 for(const auto& slide : fSlides) {
1972 writer.appendString(slide->getName().c_str());
1973 }
1974 });
liyuqiane5a6cd92016-05-27 08:52:52 -07001975
liyuqianb73c24b2016-06-03 08:47:23 -07001976 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04001977 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
1978 [](SkJSONWriter& writer) {
1979 for (const auto& str : kBackendTypeStrings) {
1980 writer.appendString(str);
1981 }
1982 });
liyuqiane5a6cd92016-05-27 08:52:52 -07001983
csmartdalton578f0642017-02-24 16:04:47 -07001984 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04001985 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
1986 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
1987 [this](SkJSONWriter& writer) {
1988 writer.appendS32(0);
1989
1990 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1991 return;
1992 }
1993
1994 for (int msaa : {4, 8, 16}) {
1995 writer.appendS32(msaa);
1996 }
1997 });
csmartdalton578f0642017-02-24 16:04:47 -07001998
csmartdalton61cd31a2017-02-27 17:00:53 -07001999 // Path renderer state
2000 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04002001 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
2002 [this](SkJSONWriter& writer) {
2003 const GrContext* ctx = fWindow->getGrContext();
2004 if (!ctx) {
2005 writer.appendString("Software");
2006 } else {
2007 const auto* caps = ctx->contextPriv().caps();
2008
Florin Malitab632df72018-06-18 21:23:06 -04002009 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
2010 if (fWindow->sampleCount() > 1) {
2011 if (caps->shaderCaps()->pathRenderingSupport()) {
2012 writer.appendString(
2013 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
2014 }
2015 } else {
2016 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
2017 writer.appendString(
2018 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
2019 }
2020 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
2021 }
2022 writer.appendString(
2023 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
2024 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
2025 }
2026 });
csmartdalton61cd31a2017-02-27 17:00:53 -07002027
liyuqianb73c24b2016-06-03 08:47:23 -07002028 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04002029 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
2030 [this](SkJSONWriter& writer) {
2031 writer.appendString(kSoftkeyHint);
2032 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
2033 writer.appendString(softkey.c_str());
2034 }
2035 });
liyuqianb73c24b2016-06-03 08:47:23 -07002036
Florin Malitab632df72018-06-18 21:23:06 -04002037 writer.endArray();
2038 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07002039
Florin Malitab632df72018-06-18 21:23:06 -04002040 auto data = memStream.detachAsData();
2041
2042 // TODO: would be cool to avoid this copy
2043 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2044
2045 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002046}
2047
2048void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002049 // For those who will add more features to handle the state change in this function:
2050 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2051 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2052 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002053 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002054 for (int i = 0; i < fSlides.count(); ++i) {
2055 if (fSlides[i]->getName().equals(stateValue)) {
2056 this->setCurrentSlide(i);
2057 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002058 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002059 }
Florin Malitaab99c342018-01-16 16:23:03 -05002060
2061 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002062 } else if (stateName.equals(kBackendStateName)) {
2063 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2064 if (stateValue.equals(kBackendTypeStrings[i])) {
2065 if (fBackendType != i) {
2066 fBackendType = (sk_app::Window::BackendType)i;
2067 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002068 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002069 }
2070 break;
2071 }
2072 }
csmartdalton578f0642017-02-24 16:04:47 -07002073 } else if (stateName.equals(kMSAAStateName)) {
2074 DisplayParams params = fWindow->getRequestedDisplayParams();
2075 int sampleCount = atoi(stateValue.c_str());
2076 if (sampleCount != params.fMSAASampleCount) {
2077 params.fMSAASampleCount = sampleCount;
2078 fWindow->setRequestedDisplayParams(params);
2079 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002080 this->updateTitle();
2081 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002082 }
2083 } else if (stateName.equals(kPathRendererStateName)) {
2084 DisplayParams params = fWindow->getRequestedDisplayParams();
2085 for (const auto& pair : gPathRendererNames) {
2086 if (pair.second == stateValue.c_str()) {
2087 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2088 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2089 fWindow->setRequestedDisplayParams(params);
2090 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002091 this->updateTitle();
2092 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002093 }
2094 break;
2095 }
csmartdalton578f0642017-02-24 16:04:47 -07002096 }
liyuqianb73c24b2016-06-03 08:47:23 -07002097 } else if (stateName.equals(kSoftkeyStateName)) {
2098 if (!stateValue.equals(kSoftkeyHint)) {
2099 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002100 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002101 }
liyuqian2edb0f42016-07-06 14:11:32 -07002102 } else if (stateName.equals(kRefreshStateName)) {
2103 // This state is actually NOT in the UI state.
2104 // We use this to allow Android to quickly set bool fRefresh.
2105 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002106 } else {
2107 SkDebugf("Unknown stateName: %s", stateName.c_str());
2108 }
2109}
Brian Osman79086b92017-02-10 13:36:16 -05002110
2111bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002112 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002113}
2114
2115bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002116 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002117 fWindow->inval();
2118 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002119 } else {
2120 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002121 }
Brian Osman79086b92017-02-10 13:36:16 -05002122}