blob: abc30c9505f744a0697798eb85e77c4318e2a775 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Chris Dalton2d18f412018-02-20 13:23:32 -07008#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -07009#include "GMSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040010#include "GrContext.h"
11#include "GrContextPriv.h"
liyuqian6f163d22016-06-13 12:26:45 -070012#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050013#include "Resources.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070014#include "SKPSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040015#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070016#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050017#include "SkColorSpacePriv.h"
Brian Osman2dd96932016-10-18 15:33:53 -040018#include "SkCommandLineFlags.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040019#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070020#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040021#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050022#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040023#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050024#include "SkImagePriv.h"
Florin Malitab632df72018-06-18 21:23:06 -040025#include "SkJSONWriter.h"
Herb Derbyefe39bc2018-05-01 17:06:20 -040026#include "SkMakeUnique.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070027#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050028#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050029#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050030#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040031#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070032#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070033#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050034#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050035#include "SkTestFontMgr.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040036#include "SkTo.h"
Hal Canary8a001442018-09-19 11:31:27 -040037#include "SlideDir.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040038#include "SvgSlide.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040039#include "Viewer.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060040#include "ccpr/GrCoverageCountingPathRenderer.h"
csmartdalton578f0642017-02-24 16:04:47 -070041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <stdlib.h>
43#include <map>
44
Hal Canary8a001442018-09-19 11:31:27 -040045#include "imgui.h"
Florin Malita3b526b02018-05-25 12:43:51 -040046
Florin Malita87ccf332018-05-04 12:23:24 -040047#if defined(SK_ENABLE_SKOTTIE)
48 #include "SkottieSlide.h"
49#endif
50
Ruiqi Maof5101492018-06-29 14:32:21 -040051#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
52 #include "NIMASlide.h"
53#endif
54
jvanverth34524262016-05-04 13:49:13 -070055using namespace sk_app;
56
csmartdalton61cd31a2017-02-27 17:00:53 -070057static std::map<GpuPathRenderers, std::string> gPathRendererNames;
58
jvanverth9f372462016-04-06 06:08:59 -070059Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070060 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070061}
62
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060063static DEFINE_string(slide, "", "Start on this sample.");
64static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050065
bsalomon6c471f72016-07-26 12:56:32 -070066#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070067# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070068#else
69# define BACKENDS_STR "\"sw\" and \"gl\""
70#endif
71
Brian Osman2dd96932016-10-18 15:33:53 -040072static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070073
Brian Salomonbdecacf2018-02-02 20:32:49 -050074static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070075
Chris Dalton2d18f412018-02-20 13:23:32 -070076DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
77
Brian Osman53136aa2017-07-20 15:43:35 -040078DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050079
Florin Malita38792ce2018-05-08 10:36:18 -040080DEFINE_string2(file, f, "", "Open a single file for viewing.");
81
Brian Salomon194db172017-08-17 14:37:06 -040082const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070083 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040084#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
85 "ANGLE",
86#endif
jvanverth063ece72016-06-17 09:29:14 -070087#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070088 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070089#endif
csmartdalton578f0642017-02-24 16:04:47 -070090 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070091};
92
bsalomon6c471f72016-07-26 12:56:32 -070093static sk_app::Window::BackendType get_backend_type(const char* str) {
94#ifdef SK_VULKAN
95 if (0 == strcmp(str, "vk")) {
96 return sk_app::Window::kVulkan_BackendType;
97 } else
98#endif
Brian Salomon194db172017-08-17 14:37:06 -040099#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
100 if (0 == strcmp(str, "angle")) {
101 return sk_app::Window::kANGLE_BackendType;
102 } else
103#endif
bsalomon6c471f72016-07-26 12:56:32 -0700104 if (0 == strcmp(str, "gl")) {
105 return sk_app::Window::kNativeGL_BackendType;
106 } else if (0 == strcmp(str, "sw")) {
107 return sk_app::Window::kRaster_BackendType;
108 } else {
109 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
110 return sk_app::Window::kRaster_BackendType;
111 }
112}
113
Brian Osmana109e392017-02-24 09:49:14 -0500114static SkColorSpacePrimaries gSrgbPrimaries = {
115 0.64f, 0.33f,
116 0.30f, 0.60f,
117 0.15f, 0.06f,
118 0.3127f, 0.3290f };
119
120static SkColorSpacePrimaries gAdobePrimaries = {
121 0.64f, 0.33f,
122 0.21f, 0.71f,
123 0.15f, 0.06f,
124 0.3127f, 0.3290f };
125
126static SkColorSpacePrimaries gP3Primaries = {
127 0.680f, 0.320f,
128 0.265f, 0.690f,
129 0.150f, 0.060f,
130 0.3127f, 0.3290f };
131
132static SkColorSpacePrimaries gRec2020Primaries = {
133 0.708f, 0.292f,
134 0.170f, 0.797f,
135 0.131f, 0.046f,
136 0.3127f, 0.3290f };
137
138struct NamedPrimaries {
139 const char* fName;
140 SkColorSpacePrimaries* fPrimaries;
141} gNamedPrimaries[] = {
142 { "sRGB", &gSrgbPrimaries },
143 { "AdobeRGB", &gAdobePrimaries },
144 { "P3", &gP3Primaries },
145 { "Rec. 2020", &gRec2020Primaries },
146};
147
148static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
149 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
150}
151
Brian Osman70d2f432017-11-08 09:54:10 -0500152static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
153 // In raster mode, we still use GL for the window.
154 // This lets us render the GUI faster (and correct).
155 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
156}
157
liyuqiane5a6cd92016-05-27 08:52:52 -0700158const char* kName = "name";
159const char* kValue = "value";
160const char* kOptions = "options";
161const char* kSlideStateName = "Slide";
162const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700163const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700164const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700165const char* kSoftkeyStateName = "Softkey";
166const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700167const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700168const char* kON = "ON";
169const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700170const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700171
jvanverth34524262016-05-04 13:49:13 -0700172Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500173 : fCurrentSlide(-1)
174 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500175 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500176 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500177 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500178 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500179 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400180 , fZoomWindowFixed(false)
181 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500182 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400183 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700184 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500185 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500186 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500187 // Our UI can only tweak gamma (currently), so start out gamma-only
188 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700189 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400190 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400191 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400192 , fGestureDevice(GestureDevice::kNone)
Brian Osman805a7272018-05-02 15:40:20 -0400193 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700194{
Greg Daniel285db442016-10-14 09:12:53 -0400195 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700196
Brian Osmanf09e35e2017-12-15 14:48:09 -0500197 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500198 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500199 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
200 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
201 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
202 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700203
jvanverth2bb3b6d2016-04-08 07:24:09 -0700204 SkDebugf("Command line arguments: ");
205 for (int i = 1; i < argc; ++i) {
206 SkDebugf("%s ", argv[i]);
207 }
208 SkDebugf("\n");
209
210 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500211#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400212 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500213#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700214
Ben Wagner483c7722018-02-20 17:06:07 -0500215 if (!FLAGS_nativeFonts) {
216 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
217 }
218
Brian Osmanbc8150f2017-07-24 11:38:01 -0400219 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400220 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400221
bsalomon6c471f72016-07-26 12:56:32 -0700222 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700223 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700224
csmartdalton578f0642017-02-24 16:04:47 -0700225 DisplayParams displayParams;
226 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700227 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700228 fWindow->setRequestedDisplayParams(displayParams);
229
Brian Osman56a24812017-12-19 11:15:16 -0500230 // Configure timers
231 fStatsLayer.setActive(false);
232 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
233 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
234 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
235
jvanverth9f372462016-04-06 06:08:59 -0700236 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700237 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500238 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500239 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500240 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700241
brianosman622c8d52016-05-10 06:50:49 -0700242 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500243 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
244 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
245 fWindow->inval();
246 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500247 // Command to jump directly to the slide picker and give it focus
248 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
249 this->fShowImGuiDebugWindow = true;
250 this->fShowSlidePicker = true;
251 fWindow->inval();
252 });
253 // Alias that to Backspace, to match SampleApp
254 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
255 this->fShowImGuiDebugWindow = true;
256 this->fShowSlidePicker = true;
257 fWindow->inval();
258 });
Brian Osman79086b92017-02-10 13:36:16 -0500259 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
260 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
261 fWindow->inval();
262 });
Brian Osmanf6877092017-02-13 09:39:57 -0500263 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
264 this->fShowZoomWindow = !this->fShowZoomWindow;
265 fWindow->inval();
266 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400267 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
268 this->fZoomWindowFixed = !this->fZoomWindowFixed;
269 fWindow->inval();
270 });
brianosman622c8d52016-05-10 06:50:49 -0700271 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500272 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700273 fWindow->inval();
274 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400275 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500276 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400277 this->updateTitle();
278 fWindow->inval();
279 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500280 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500281 switch (fColorMode) {
282 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500283 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500284 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500285 case ColorMode::kColorManaged8888:
286 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500287 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500288 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500289 this->setColorMode(ColorMode::kLegacy);
290 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500291 }
brianosman622c8d52016-05-10 06:50:49 -0700292 });
293 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500294 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700295 });
296 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500297 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700298 });
299 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
300 this->changeZoomLevel(1.f / 32.f);
301 fWindow->inval();
302 });
303 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
304 this->changeZoomLevel(-1.f / 32.f);
305 fWindow->inval();
306 });
jvanverthaf236b52016-05-20 06:01:06 -0700307 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400308 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
309 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500310 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400311#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
312 if (newBackend == sk_app::Window::kVulkan_BackendType) {
313 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
314 sk_app::Window::kBackendTypeCount);
315 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
316 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500317 }
318#endif
Brian Osman621491e2017-02-28 15:45:01 -0500319 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700320 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500321 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
322 fSaveToSKP = true;
323 fWindow->inval();
324 });
Ben Wagner37c54032018-04-13 14:30:23 -0400325 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
326 DisplayParams params = fWindow->getRequestedDisplayParams();
327 uint32_t flags = params.fSurfaceProps.flags();
328 if (!fPixelGeometryOverrides) {
329 fPixelGeometryOverrides = true;
330 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
331 } else {
332 switch (params.fSurfaceProps.pixelGeometry()) {
333 case kUnknown_SkPixelGeometry:
334 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
335 break;
336 case kRGB_H_SkPixelGeometry:
337 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
338 break;
339 case kBGR_H_SkPixelGeometry:
340 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
341 break;
342 case kRGB_V_SkPixelGeometry:
343 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
344 break;
345 case kBGR_V_SkPixelGeometry:
346 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
347 fPixelGeometryOverrides = false;
348 break;
349 }
350 }
351 fWindow->setRequestedDisplayParams(params);
352 this->updateTitle();
353 fWindow->inval();
354 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500355 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
356 if (!fPaintOverrides.fHinting) {
357 fPaintOverrides.fHinting = true;
Mike Reed9edbf422018-11-07 19:54:33 -0500358 fPaint.setHinting(kNo_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500359 } else {
Mike Reed9edbf422018-11-07 19:54:33 -0500360 switch ((SkFontHinting)fPaint.getHinting()) {
361 case kNo_SkFontHinting:
362 fPaint.setHinting(kSlight_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500363 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500364 case kSlight_SkFontHinting:
365 fPaint.setHinting(kNormal_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500366 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500367 case kNormal_SkFontHinting:
368 fPaint.setHinting(kFull_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500369 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500370 case kFull_SkFontHinting:
371 fPaint.setHinting(kNo_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500372 fPaintOverrides.fHinting = false;
373 break;
374 }
375 }
376 this->updateTitle();
377 fWindow->inval();
378 });
379 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
380 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
381 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
382 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
383 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500384 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
385 gSkUseDeltaAA = gSkForceDeltaAA = false;
386 } else {
387 fPaint.setAntiAlias(true);
388 switch (fPaintOverrides.fAntiAlias) {
389 case SkPaintFields::AntiAliasState::Alias:
390 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400391 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
392 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500393 break;
394 case SkPaintFields::AntiAliasState::Normal:
395 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500396 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400397 gSkForceAnalyticAA = false;
398 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500399 break;
400 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
401 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400402 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
403 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500404 break;
405 case SkPaintFields::AntiAliasState::AnalyticAAForced:
406 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
407 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
408 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400409 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500410 break;
411 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
412 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400413 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
414 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500415 break;
416 case SkPaintFields::AntiAliasState::DeltaAAForced:
417 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
418 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
419 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
420 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
421 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
422 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
423 break;
424 }
425 }
426 this->updateTitle();
427 fWindow->inval();
428 });
Ben Wagner37c54032018-04-13 14:30:23 -0400429 fCommands.addCommand('D', "Modes", "DFT", [this]() {
430 DisplayParams params = fWindow->getRequestedDisplayParams();
431 uint32_t flags = params.fSurfaceProps.flags();
432 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
433 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
434 fWindow->setRequestedDisplayParams(params);
435 this->updateTitle();
436 fWindow->inval();
437 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500438 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
439 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
440 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
441 fPaint.setLCDRenderText(false);
442 } else {
443 if (!fPaint.isLCDRenderText()) {
444 fPaint.setLCDRenderText(true);
445 } else {
446 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
447 }
448 }
449 this->updateTitle();
450 fWindow->inval();
451 });
452 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
453 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
454 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
455 fPaint.setSubpixelText(false);
456 } else {
457 if (!fPaint.isSubpixelText()) {
458 fPaint.setSubpixelText(true);
459 } else {
460 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
461 }
462 }
463 this->updateTitle();
464 fWindow->inval();
465 });
Brian Osman805a7272018-05-02 15:40:20 -0400466 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
467 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
468 : kPerspective_Real;
469 this->updateTitle();
470 fWindow->inval();
471 });
472 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
473 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
474 : kPerspective_Off;
475 this->updateTitle();
476 fWindow->inval();
477 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400478 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
479 fZoomUI = !fZoomUI;
480 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
481 fWindow->inval();
482 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500483
jvanverth2bb3b6d2016-04-08 07:24:09 -0700484 // set up slides
485 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500486 if (FLAGS_list) {
487 this->listNames();
488 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700489
Brian Osman9bb47cf2018-04-26 15:55:00 -0400490 fPerspectivePoints[0].set(0, 0);
491 fPerspectivePoints[1].set(1, 0);
492 fPerspectivePoints[2].set(0, 1);
493 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700494 fAnimTimer.run();
495
Hal Canaryc465d132017-12-08 10:21:31 -0500496 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500497 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400498 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500499 }
500 fImGuiGamutPaint.setColor(SK_ColorWHITE);
501 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
502
Brian Osman70d2f432017-11-08 09:54:10 -0500503 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500504 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700505}
506
jvanverth34524262016-05-04 13:49:13 -0700507void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400508 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
509 static const struct {
510 const char* fExtension;
511 const char* fDirName;
512 const SkCommandLineFlags::StringArray& fFlags;
513 const SlideFactory fFactory;
514 } gExternalSlidesInfo[] = {
515 { ".skp", "skp-dir", FLAGS_skps,
516 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
517 return sk_make_sp<SKPSlide>(name, path);}
518 },
519 { ".jpg", "jpg-dir", FLAGS_jpgs,
520 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
521 return sk_make_sp<ImageSlide>(name, path);}
522 },
Florin Malita87ccf332018-05-04 12:23:24 -0400523#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400524 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400525 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
526 return sk_make_sp<SkottieSlide>(name, path);}
527 },
Florin Malita87ccf332018-05-04 12:23:24 -0400528#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400529#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400530 { ".svg", "svg-dir", FLAGS_svgs,
531 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
532 return sk_make_sp<SvgSlide>(name, path);}
533 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400534#endif
Ruiqi Maof5101492018-06-29 14:32:21 -0400535#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
536 { ".nima", "nima-dir", FLAGS_nimas,
537 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
538 return sk_make_sp<NIMASlide>(name, path);}
539 },
540#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400541 };
jvanverthc265a922016-04-08 12:51:45 -0700542
Brian Salomon343553a2018-09-05 15:41:23 -0400543 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700544
Florin Malita0ffa3222018-04-05 14:34:45 -0400545 const auto addSlide = [&](const SkString& name,
546 const SkString& path,
547 const SlideFactory& fact) {
548 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
549 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700550 }
liyuqian6f163d22016-06-13 12:26:45 -0700551
Florin Malita0ffa3222018-04-05 14:34:45 -0400552 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500553 dirSlides.push_back(slide);
554 fSlides.push_back(std::move(slide));
555 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400556 };
Florin Malita76a076b2018-02-15 18:40:48 -0500557
Florin Malita38792ce2018-05-08 10:36:18 -0400558 if (!FLAGS_file.isEmpty()) {
559 // single file mode
560 const SkString file(FLAGS_file[0]);
561
562 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
563 for (const auto& sinfo : gExternalSlidesInfo) {
564 if (file.endsWith(sinfo.fExtension)) {
565 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
566 return;
567 }
568 }
569
570 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
571 } else {
572 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
573 }
574
575 return;
576 }
577
578 // Bisect slide.
579 if (!FLAGS_bisect.isEmpty()) {
580 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
581 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
582 if (FLAGS_bisect.count() >= 2) {
583 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
584 bisect->onChar(*ch);
585 }
586 }
587 fSlides.push_back(std::move(bisect));
588 }
589 }
590
591 // GMs
592 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400593 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
594 std::unique_ptr<skiagm::GM> gm(gmFactory(nullptr));
Florin Malita38792ce2018-05-08 10:36:18 -0400595 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
596 sk_sp<Slide> slide(new GMSlide(gm.release()));
597 fSlides.push_back(std::move(slide));
598 }
Florin Malita38792ce2018-05-08 10:36:18 -0400599 }
600 // reverse gms
601 int numGMs = fSlides.count() - firstGM;
602 for (int i = 0; i < numGMs/2; ++i) {
603 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
604 }
605
606 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400607 for (const SampleFactory factory : SampleRegistry::Range()) {
608 sk_sp<Slide> slide(new SampleSlide(factory));
Florin Malita38792ce2018-05-08 10:36:18 -0400609 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
610 fSlides.push_back(slide);
611 }
Florin Malita38792ce2018-05-08 10:36:18 -0400612 }
613
Florin Malita0ffa3222018-04-05 14:34:45 -0400614 for (const auto& info : gExternalSlidesInfo) {
615 for (const auto& flag : info.fFlags) {
616 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
617 // single file
618 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
619 } else {
620 // directory
621 SkOSFile::Iter it(flag.c_str(), info.fExtension);
622 SkString name;
623 while (it.next(&name)) {
624 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
625 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400626 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400627 if (!dirSlides.empty()) {
628 fSlides.push_back(
629 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
630 std::move(dirSlides)));
631 dirSlides.reset();
632 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400633 }
634 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700635}
636
637
jvanverth34524262016-05-04 13:49:13 -0700638Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700639 fWindow->detach();
640 delete fWindow;
641}
642
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500643struct SkPaintTitleUpdater {
644 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
645 void append(const char* s) {
646 if (fCount == 0) {
647 fTitle->append(" {");
648 } else {
649 fTitle->append(", ");
650 }
651 fTitle->append(s);
652 ++fCount;
653 }
654 void done() {
655 if (fCount > 0) {
656 fTitle->append("}");
657 }
658 }
659 SkString* fTitle;
660 int fCount;
661};
662
brianosman05de2162016-05-06 13:28:57 -0700663void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700664 if (!fWindow) {
665 return;
666 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500667 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700668 return; // Surface hasn't been created yet.
669 }
670
jvanverth34524262016-05-04 13:49:13 -0700671 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700672 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700673
Yuqian Li399b3c22017-08-03 11:08:15 -0400674 if (gSkUseDeltaAA) {
675 if (gSkForceDeltaAA) {
676 title.append(" <FDAA>");
677 } else {
678 title.append(" <DAA>");
679 }
680 } else if (gSkUseAnalyticAA) {
681 if (gSkForceAnalyticAA) {
682 title.append(" <FAAA>");
683 } else {
684 title.append(" <AAA>");
685 }
686 }
687
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500688 SkPaintTitleUpdater paintTitle(&title);
Ben Wagner99a78dc2018-05-09 18:23:51 -0400689 auto paintFlag = [this, &paintTitle](SkPaint::Flags flag, bool (SkPaint::* isFlag)() const,
690 const char* on, const char* off)
691 {
692 if (fPaintOverrides.fFlags & flag) {
693 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500694 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400695 };
696
697 paintFlag(SkPaint::kAntiAlias_Flag, &SkPaint::isAntiAlias, "Antialias", "Alias");
698 paintFlag(SkPaint::kDither_Flag, &SkPaint::isDither, "DITHER", "No Dither");
699 paintFlag(SkPaint::kFakeBoldText_Flag, &SkPaint::isFakeBoldText, "Fake Bold", "No Fake Bold");
700 paintFlag(SkPaint::kLinearText_Flag, &SkPaint::isLinearText, "Linear Text", "Non-Linear Text");
701 paintFlag(SkPaint::kSubpixelText_Flag, &SkPaint::isSubpixelText, "Subpixel Text", "Pixel Text");
702 paintFlag(SkPaint::kLCDRenderText_Flag, &SkPaint::isLCDRenderText, "LCD", "lcd");
703 paintFlag(SkPaint::kEmbeddedBitmapText_Flag, &SkPaint::isEmbeddedBitmapText,
704 "Bitmap Text", "No Bitmap Text");
705 paintFlag(SkPaint::kAutoHinting_Flag, &SkPaint::isAutohinted,
706 "Force Autohint", "No Force Autohint");
Ben Wagner99a78dc2018-05-09 18:23:51 -0400707
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500708 if (fPaintOverrides.fHinting) {
Mike Reed9edbf422018-11-07 19:54:33 -0500709 switch ((SkFontHinting)fPaint.getHinting()) {
710 case kNo_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500711 paintTitle.append("No Hinting");
712 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500713 case kSlight_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500714 paintTitle.append("Slight Hinting");
715 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500716 case kNormal_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500717 paintTitle.append("Normal Hinting");
718 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500719 case kFull_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500720 paintTitle.append("Full Hinting");
721 break;
722 }
723 }
724 paintTitle.done();
725
Brian Osman92004802017-03-06 11:47:26 -0500726 switch (fColorMode) {
727 case ColorMode::kLegacy:
728 title.append(" Legacy 8888");
729 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500730 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500731 title.append(" ColorManaged 8888");
732 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500733 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500734 title.append(" ColorManaged F16");
735 break;
736 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500737
Brian Osman92004802017-03-06 11:47:26 -0500738 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500739 int curPrimaries = -1;
740 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
741 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
742 curPrimaries = i;
743 break;
744 }
745 }
Brian Osman03115dc2018-11-26 13:55:19 -0500746 title.appendf(" %s Gamma %f",
747 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
748 fColorSpaceTransferFn.fG);
brianosman05de2162016-05-06 13:28:57 -0700749 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500750
Ben Wagner37c54032018-04-13 14:30:23 -0400751 const DisplayParams& params = fWindow->getRequestedDisplayParams();
752 if (fPixelGeometryOverrides) {
753 switch (params.fSurfaceProps.pixelGeometry()) {
754 case kUnknown_SkPixelGeometry:
755 title.append( " Flat");
756 break;
757 case kRGB_H_SkPixelGeometry:
758 title.append( " RGB");
759 break;
760 case kBGR_H_SkPixelGeometry:
761 title.append( " BGR");
762 break;
763 case kRGB_V_SkPixelGeometry:
764 title.append( " RGBV");
765 break;
766 case kBGR_V_SkPixelGeometry:
767 title.append( " BGRV");
768 break;
769 }
770 }
771
772 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
773 title.append(" DFT");
774 }
775
csmartdalton578f0642017-02-24 16:04:47 -0700776 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700777 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500778 int msaa = fWindow->sampleCount();
779 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700780 title.appendf(" MSAA: %i", msaa);
781 }
782 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700783
784 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman1994f202018-07-10 11:41:22 -0400785 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700786 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
787 }
788
Brian Osman805a7272018-05-02 15:40:20 -0400789 if (kPerspective_Real == fPerspectiveMode) {
790 title.append(" Perpsective (Real)");
791 } else if (kPerspective_Fake == fPerspectiveMode) {
792 title.append(" Perspective (Fake)");
793 }
794
brianosman05de2162016-05-06 13:28:57 -0700795 fWindow->setTitle(title.c_str());
796}
797
Florin Malitaab99c342018-01-16 16:23:03 -0500798int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500799
800 if (!FLAGS_slide.isEmpty()) {
801 int count = fSlides.count();
802 for (int i = 0; i < count; i++) {
803 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500804 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500805 }
806 }
807
808 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
809 this->listNames();
810 }
811
Florin Malitaab99c342018-01-16 16:23:03 -0500812 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500813}
814
Florin Malitaab99c342018-01-16 16:23:03 -0500815void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500816 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500817 for (const auto& slide : fSlides) {
818 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500819 }
820}
821
Florin Malitaab99c342018-01-16 16:23:03 -0500822void Viewer::setCurrentSlide(int slide) {
823 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700824
Florin Malitaab99c342018-01-16 16:23:03 -0500825 if (slide == fCurrentSlide) {
826 return;
827 }
828
829 if (fCurrentSlide >= 0) {
830 fSlides[fCurrentSlide]->unload();
831 }
832
833 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
834 SkIntToScalar(fWindow->height()));
835 fCurrentSlide = slide;
836 this->setupCurrentSlide();
837}
838
839void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500840 if (fCurrentSlide >= 0) {
841 // prepare dimensions for image slides
842 fGesture.resetTouchState();
843 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700844
Jim Van Verth0848fb02018-01-22 13:39:30 -0500845 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
846 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
847 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400848
Jim Van Verth0848fb02018-01-22 13:39:30 -0500849 // Start with a matrix that scales the slide to the available screen space
850 if (fWindow->scaleContentToFit()) {
851 if (windowRect.width() > 0 && windowRect.height() > 0) {
852 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
853 }
liyuqiane46e4f02016-05-20 07:32:19 -0700854 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500855
856 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400857 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500858
859 this->updateTitle();
860 this->updateUIState();
861
862 fStatsLayer.resetMeasurements();
863
864 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700865 }
jvanverthc265a922016-04-08 12:51:45 -0700866}
867
868#define MAX_ZOOM_LEVEL 8
869#define MIN_ZOOM_LEVEL -8
870
jvanverth34524262016-05-04 13:49:13 -0700871void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700872 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400873 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400874 this->preTouchMatrixChanged();
875}
Yuqian Li755778c2018-03-28 16:23:31 -0400876
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400877void Viewer::preTouchMatrixChanged() {
878 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400879 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
880 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
881 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
882 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
883}
884
Brian Osman805a7272018-05-02 15:40:20 -0400885SkMatrix Viewer::computePerspectiveMatrix() {
886 SkScalar w = fWindow->width(), h = fWindow->height();
887 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
888 SkPoint perspPts[4] = {
889 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
890 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
891 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
892 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
893 };
894 SkMatrix m;
895 m.setPolyToPoly(orthoPts, perspPts, 4);
896 return m;
897}
898
Yuqian Li755778c2018-03-28 16:23:31 -0400899SkMatrix Viewer::computePreTouchMatrix() {
900 SkMatrix m = fDefaultMatrix;
901 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
902 : SK_Scalar1 + fZoomLevel;
Ben Wagner897dfa22018-08-09 15:18:46 -0400903 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -0400904 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400905
906 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
907 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400908
Brian Osman805a7272018-05-02 15:40:20 -0400909 if (kPerspective_Real == fPerspectiveMode) {
910 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400911 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400912 }
913
Yuqian Li755778c2018-03-28 16:23:31 -0400914 return m;
jvanverthc265a922016-04-08 12:51:45 -0700915}
916
liyuqiand3cdbca2016-05-17 12:44:20 -0700917SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400918 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700919 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400920 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700921 return m;
jvanverthc265a922016-04-08 12:51:45 -0700922}
923
Brian Osman621491e2017-02-28 15:45:01 -0500924void Viewer::setBackend(sk_app::Window::BackendType backendType) {
925 fBackendType = backendType;
926
927 fWindow->detach();
928
Brian Osman70d2f432017-11-08 09:54:10 -0500929#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400930 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
931 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500932 DisplayParams params = fWindow->getRequestedDisplayParams();
933 delete fWindow;
934 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500935
Brian Osman70d2f432017-11-08 09:54:10 -0500936 // re-register callbacks
937 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500938 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500939 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500940 fWindow->pushLayer(&fImGuiLayer);
941
Brian Osman70d2f432017-11-08 09:54:10 -0500942 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
943 // will still include our correct sample count. But the re-created fWindow will lose that
944 // information. On Windows, we need to re-create the window when changing sample count,
945 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
946 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
947 // as if we had never changed windows at all).
948 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500949#endif
950
Brian Osman70d2f432017-11-08 09:54:10 -0500951 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500952}
953
Brian Osman92004802017-03-06 11:47:26 -0500954void Viewer::setColorMode(ColorMode colorMode) {
955 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500956 this->updateTitle();
957 fWindow->inval();
958}
959
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500960class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
961public:
962 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
963 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
964 { }
Ben Wagner41e40472018-09-24 13:01:54 -0400965 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
966 sk_sp<SkTextBlob>* cache) {
967 bool blobWillChange = false;
968 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
969 SkPaint blobPaint = paint;
970 it.applyFontToPaint(&blobPaint);
971 SkTCopyOnFirstWrite<SkPaint> filteredPaint(blobPaint);
972 bool shouldDraw = this->onFilter(&filteredPaint, kTextBlob_Type);
973 if (blobPaint != *filteredPaint || !shouldDraw) {
974 blobWillChange = true;
975 break;
976 }
977 }
978 if (!blobWillChange) {
979 return blob;
980 }
981
982 SkTextBlobBuilder builder;
983 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
984 SkPaint blobPaint = paint;
985 it.applyFontToPaint(&blobPaint);
986 SkTCopyOnFirstWrite<SkPaint> filteredPaint(blobPaint);
987 bool shouldDraw = this->onFilter(&filteredPaint, kTextBlob_Type);
988 if (!shouldDraw) {
989 continue;
990 }
991
992 const SkTextBlobBuilder::RunBuffer& runBuffer
993 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
994 ? SkTextBlobBuilderPriv::AllocRunText(&builder, *filteredPaint,
995 it.offset().x(),it.offset().y(), it.glyphCount(), it.textSize(), SkString())
996 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
997 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, *filteredPaint,
998 it.offset().y(), it.glyphCount(), it.textSize(), SkString())
999 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
1000 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, *filteredPaint,
1001 it.glyphCount(), it.textSize(), SkString())
1002 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1003 uint32_t glyphCount = it.glyphCount();
1004 if (it.glyphs()) {
1005 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1006 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1007 }
1008 if (it.pos()) {
1009 size_t posSize = sizeof(decltype(*it.pos()));
1010 uint8_t positioning = it.positioning();
1011 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1012 }
1013 if (it.text()) {
1014 size_t textSize = sizeof(decltype(*it.text()));
1015 uint32_t textCount = it.textSize();
1016 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1017 }
1018 if (it.clusters()) {
1019 size_t clusterSize = sizeof(decltype(*it.clusters()));
1020 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1021 }
1022 }
1023 *cache = builder.make();
1024 return cache->get();
1025 }
1026 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1027 const SkPaint& paint) override {
1028 sk_sp<SkTextBlob> cache;
1029 this->SkPaintFilterCanvas::onDrawTextBlob(
1030 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1031 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001032 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001033 if (*paint == nullptr) {
1034 return true;
1035 }
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001036 if (fPaintOverrides->fTextSize) {
1037 paint->writable()->setTextSize(fPaint->getTextSize());
1038 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001039 if (fPaintOverrides->fHinting) {
1040 paint->writable()->setHinting(fPaint->getHinting());
1041 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001042
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001043 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
1044 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1045 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001046 if (fPaintOverrides->fFlags & SkPaint::kDither_Flag) {
1047 paint->writable()->setDither(fPaint->isDither());
1048 }
1049 if (fPaintOverrides->fFlags & SkPaint::kFakeBoldText_Flag) {
1050 paint->writable()->setFakeBoldText(fPaint->isFakeBoldText());
1051 }
1052 if (fPaintOverrides->fFlags & SkPaint::kLinearText_Flag) {
1053 paint->writable()->setLinearText(fPaint->isLinearText());
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001054 }
1055 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
1056 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
1057 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001058 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
1059 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
1060 }
1061 if (fPaintOverrides->fFlags & SkPaint::kEmbeddedBitmapText_Flag) {
1062 paint->writable()->setEmbeddedBitmapText(fPaint->isEmbeddedBitmapText());
1063 }
1064 if (fPaintOverrides->fFlags & SkPaint::kAutoHinting_Flag) {
1065 paint->writable()->setAutohinted(fPaint->isAutohinted());
1066 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001067
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001068 return true;
1069 }
1070 SkPaint* fPaint;
1071 Viewer::SkPaintFields* fPaintOverrides;
1072};
1073
Brian Osmanf750fbc2017-02-08 10:47:28 -05001074void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001075 SkAutoCanvasRestore autorestore(canvas, false);
1076
Brian Osmanf750fbc2017-02-08 10:47:28 -05001077 // By default, we render directly into the window's surface/canvas
1078 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001079 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001080
Brian Osmane0d4fba2017-03-15 10:24:55 -04001081 // If we're in any of the color managed modes, construct the color space we're going to use
Brian Osman03115dc2018-11-26 13:55:19 -05001082 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001083 if (ColorMode::kLegacy != fColorMode) {
Mike Klein4429a4f2018-10-04 09:06:00 -04001084 SkMatrix44 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001085 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001086 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001087 }
1088
Brian Osman3ac99cf2017-12-01 11:23:53 -05001089 if (fSaveToSKP) {
1090 SkPictureRecorder recorder;
1091 SkCanvas* recorderCanvas = recorder.beginRecording(
1092 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001093 fSlides[fCurrentSlide]->draw(recorderCanvas);
1094 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1095 SkFILEWStream stream("sample_app.skp");
1096 picture->serialize(&stream);
1097 fSaveToSKP = false;
1098 }
1099
Brian Osman03115dc2018-11-26 13:55:19 -05001100 // We need to render offscreen if we're...
1101 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1102 // ... in any raster mode, because the window surface is actually GL
1103 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001104 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001105 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001106 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001107 Window::kRaster_BackendType == fBackendType ||
1108 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001109
Brian Osman03115dc2018-11-26 13:55:19 -05001110 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode)
Brian Osman92004802017-03-06 11:47:26 -05001111 ? kRGBA_F16_SkColorType : kN32_SkColorType;
1112 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osman03115dc2018-11-26 13:55:19 -05001113 kPremul_SkAlphaType, colorSpace);
Ben Wagner37c54032018-04-13 14:30:23 -04001114 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1115 canvas->getProps(&props);
1116 offscreenSurface = Window::kRaster_BackendType == fBackendType
1117 ? SkSurface::MakeRaster(info, &props)
1118 : canvas->makeSurface(info);
Mike Klein48b64902018-07-25 13:28:44 -04001119 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001120 }
1121
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001122 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001123 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001124 slideCanvas->concat(computeMatrix());
Brian Osmane19e23f2018-05-22 16:56:40 -04001125 if (kPerspective_Real == fPerspectiveMode) {
1126 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1127 }
Brian Osman1df161a2017-02-09 12:10:20 -05001128 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001129 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001130 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1131 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001132 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001133 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001134
1135 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001136 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001137 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001138 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001139
1140 // If we rendered offscreen, snap an image and push the results to the window's canvas
1141 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001142 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001143
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001144 SkPaint paint;
1145 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001146 int prePerspectiveCount = canvas->save();
1147 if (kPerspective_Fake == fPerspectiveMode) {
1148 paint.setFilterQuality(kHigh_SkFilterQuality);
1149 canvas->clear(SK_ColorWHITE);
1150 canvas->concat(this->computePerspectiveMatrix());
1151 }
Brian Osman03115dc2018-11-26 13:55:19 -05001152 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001153 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001154 }
liyuqian6f163d22016-06-13 12:26:45 -07001155}
1156
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001157void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001158 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001159 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001160}
Jim Van Verth6f449692017-02-14 15:16:46 -05001161
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001162void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001163 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001164
brianosman622c8d52016-05-10 06:50:49 -07001165 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001166
Brian Osmand67e5182017-12-08 16:46:09 -05001167 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001168
1169 if (GrContext* ctx = fWindow->getGrContext()) {
1170 // Clean out cache items that haven't been used in more than 10 seconds.
1171 ctx->performDeferredCleanup(std::chrono::seconds(10));
1172 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001173}
1174
Ben Wagnera1915972018-08-09 15:06:19 -04001175void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001176 if (fCurrentSlide >= 0) {
1177 fSlides[fCurrentSlide]->resize(width, height);
1178 }
Ben Wagnera1915972018-08-09 15:06:19 -04001179}
1180
Florin Malitacefc1b92018-02-19 21:43:47 -05001181SkPoint Viewer::mapEvent(float x, float y) {
1182 const auto m = this->computeMatrix();
1183 SkMatrix inv;
1184
1185 SkAssertResult(m.invert(&inv));
1186
1187 return inv.mapXY(x, y);
1188}
1189
jvanverth814e38d2016-06-06 08:48:47 -07001190bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001191 if (GestureDevice::kMouse == fGestureDevice) {
1192 return false;
1193 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001194
1195 const auto slidePt = this->mapEvent(x, y);
1196 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1197 fWindow->inval();
1198 return true;
1199 }
1200
liyuqiand3cdbca2016-05-17 12:44:20 -07001201 void* castedOwner = reinterpret_cast<void*>(owner);
1202 switch (state) {
1203 case Window::kUp_InputState: {
1204 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001205#if defined(SK_BUILD_FOR_IOS)
1206 // TODO: move IOS swipe detection higher up into the platform code
1207 SkPoint dir;
1208 if (fGesture.isFling(&dir)) {
1209 // swiping left or right
1210 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1211 if (dir.fX < 0) {
1212 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1213 fCurrentSlide + 1 : 0);
1214 } else {
1215 this->setCurrentSlide(fCurrentSlide > 0 ?
1216 fCurrentSlide - 1 : fSlides.count() - 1);
1217 }
1218 }
1219 fGesture.reset();
1220 }
1221#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001222 break;
1223 }
1224 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001225 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001226 break;
1227 }
1228 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001229 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001230 break;
1231 }
1232 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001233 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001234 fWindow->inval();
1235 return true;
1236}
1237
Brian Osman80fc07e2017-12-08 16:45:43 -05001238bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001239 if (GestureDevice::kTouch == fGestureDevice) {
1240 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001241 }
Brian Osman16c81a12017-12-20 11:58:34 -05001242
Florin Malitacefc1b92018-02-19 21:43:47 -05001243 const auto slidePt = this->mapEvent(x, y);
1244 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1245 fWindow->inval();
1246 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001247 }
1248
1249 switch (state) {
1250 case Window::kUp_InputState: {
1251 fGesture.touchEnd(nullptr);
1252 break;
1253 }
1254 case Window::kDown_InputState: {
1255 fGesture.touchBegin(nullptr, x, y);
1256 break;
1257 }
1258 case Window::kMove_InputState: {
1259 fGesture.touchMoved(nullptr, x, y);
1260 break;
1261 }
1262 }
1263 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1264
1265 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1266 fWindow->inval();
1267 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001268 return true;
1269}
1270
Brian Osmana109e392017-02-24 09:49:14 -05001271static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1272 const ImVec2& pos, const ImVec2& size) {
1273 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1274 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1275
1276 // Invisible 10x10 button
1277 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1278 ImGui::InvisibleButton(label, ImVec2(10, 10));
1279
1280 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1281 ImGuiIO& io = ImGui::GetIO();
1282 // Normalized mouse position, relative to our gamut box
1283 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1284 // Clamp to edge of box, convert back to primary scale
1285 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1286 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1287 }
1288
1289 if (ImGui::IsItemHovered()) {
1290 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1291 }
1292
1293 // Return screen coordinates for the caller. We could just return center here, but we'd have
1294 // one frame of lag during drag.
1295 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1296}
1297
1298static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1299 ImDrawList* drawList = ImGui::GetWindowDrawList();
1300
1301 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1302 // width, and scale the height to maintain aspect ratio.
1303 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1304 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1305 ImVec2 pos = ImGui::GetCursorScreenPos();
1306
1307 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1308 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1309 // Magic numbers are pixel locations of the origin and upper-right corner.
1310 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1311 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001312
1313 // Primary markers
1314 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1315 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1316 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1317 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1318
1319 // Gamut triangle
1320 drawList->AddCircle(r, 5.0f, 0xFF000040);
1321 drawList->AddCircle(g, 5.0f, 0xFF004000);
1322 drawList->AddCircle(b, 5.0f, 0xFF400000);
1323 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1324 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1325
1326 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001327 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1328}
1329
1330static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1331 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1332 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1333 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1334
1335 // Invisible 10x10 button
1336 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1337 ImGui::InvisibleButton(label, ImVec2(10, 10));
1338
1339 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1340 ImGuiIO& io = ImGui::GetIO();
1341 // Normalized mouse position, relative to our gamut box
1342 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1343 // Clamp to edge of box
1344 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1345 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1346 *dragging = true;
1347 }
1348
1349 // Return screen coordinates for the caller. We could just return center here, but we'd have
1350 // one frame of lag during drag.
1351 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1352}
1353
Ben Wagner3627d2e2018-06-26 14:23:20 -04001354static bool ImGui_DragLocation(SkPoint* pt) {
1355 ImDrawList* drawList = ImGui::GetWindowDrawList();
1356
1357 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1358 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1359 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1360 ImVec2 pos = ImGui::GetCursorScreenPos();
1361
1362 // Background rectangle
1363 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1364
1365 // Location marker
1366 bool dragging = false;
1367 ImVec2 tl = ImGui_DragPoint("SL", pt + 0, pos, size, &dragging);
1368 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1369
1370 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1371 ImGui::Spacing();
1372
1373 return dragging;
1374}
1375
Brian Osman9bb47cf2018-04-26 15:55:00 -04001376static bool ImGui_DragQuad(SkPoint* pts) {
1377 ImDrawList* drawList = ImGui::GetWindowDrawList();
1378
1379 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1380 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1381 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1382 ImVec2 pos = ImGui::GetCursorScreenPos();
1383
1384 // Background rectangle
1385 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1386
1387 // Corner markers
1388 bool dragging = false;
1389 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1390 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1391 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1392 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1393
1394 // Draw markers and quad
1395 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1396 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1397 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1398 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1399 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1400 drawList->AddLine(tr, br, 0xFFFFFFFF);
1401 drawList->AddLine(br, bl, 0xFFFFFFFF);
1402 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1403
1404 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1405 ImGui::Spacing();
1406
1407 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001408}
1409
Brian Osmand67e5182017-12-08 16:46:09 -05001410void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001411 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1412 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001413 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001414 }
1415
1416 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001417 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1418 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001419 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001420 DisplayParams params = fWindow->getRequestedDisplayParams();
1421 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001422 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1423 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001424 if (ImGui::CollapsingHeader("Backend")) {
1425 int newBackend = static_cast<int>(fBackendType);
1426 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1427 ImGui::SameLine();
1428 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001429#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1430 ImGui::SameLine();
1431 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1432#endif
Brian Osman621491e2017-02-28 15:45:01 -05001433#if defined(SK_VULKAN)
1434 ImGui::SameLine();
1435 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1436#endif
1437 if (newBackend != fBackendType) {
1438 fDeferredActions.push_back([=]() {
1439 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1440 });
1441 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001442
Brian Salomon99a33902017-03-07 15:16:34 -05001443 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001444 bool* wire = &params.fGrContextOptions.fWireframeMode;
1445 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1446 paramsChanged = true;
1447 }
Brian Salomon99a33902017-03-07 15:16:34 -05001448
Brian Osman28b12522017-03-08 17:10:24 -05001449 if (ctx) {
1450 int sampleCount = fWindow->sampleCount();
1451 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001452 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001453 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1454 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1455 ImGui::RadioButton("16", &sampleCount, 16);
1456
1457 if (sampleCount != params.fMSAASampleCount) {
1458 params.fMSAASampleCount = sampleCount;
1459 paramsChanged = true;
1460 }
1461 }
1462
Ben Wagner37c54032018-04-13 14:30:23 -04001463 int pixelGeometryIdx = 0;
1464 if (fPixelGeometryOverrides) {
1465 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1466 }
1467 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1468 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1469 {
1470 uint32_t flags = params.fSurfaceProps.flags();
1471 if (pixelGeometryIdx == 0) {
1472 fPixelGeometryOverrides = false;
1473 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1474 } else {
1475 fPixelGeometryOverrides = true;
1476 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1477 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1478 }
1479 paramsChanged = true;
1480 }
1481
1482 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1483 if (ImGui::Checkbox("DFT", &useDFT)) {
1484 uint32_t flags = params.fSurfaceProps.flags();
1485 if (useDFT) {
1486 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1487 } else {
1488 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1489 }
1490 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1491 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1492 paramsChanged = true;
1493 }
1494
Brian Osman8a9de3d2017-03-01 14:59:05 -05001495 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001496 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001497 auto prButton = [&](GpuPathRenderers x) {
1498 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001499 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1500 params.fGrContextOptions.fGpuPathRenderers = x;
1501 paramsChanged = true;
1502 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001503 }
1504 };
1505
1506 if (!ctx) {
1507 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001508 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001509 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001510 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001511 prButton(GpuPathRenderers::kStencilAndCover);
1512 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001513 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001514 prButton(GpuPathRenderers::kNone);
1515 } else {
1516 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001517 if (GrCoverageCountingPathRenderer::IsSupported(
1518 *ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001519 prButton(GpuPathRenderers::kCoverageCounting);
1520 }
Jim Van Verth83010462017-03-16 08:45:39 -04001521 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001522 prButton(GpuPathRenderers::kTessellating);
1523 prButton(GpuPathRenderers::kNone);
1524 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001525 ImGui::TreePop();
1526 }
Brian Osman621491e2017-02-28 15:45:01 -05001527 }
1528
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001529 if (ImGui::CollapsingHeader("Transform")) {
1530 float zoom = fZoomLevel;
1531 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1532 fZoomLevel = zoom;
1533 this->preTouchMatrixChanged();
1534 paramsChanged = true;
1535 }
1536 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001537 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001538 fRotation = deg;
1539 this->preTouchMatrixChanged();
1540 paramsChanged = true;
1541 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001542 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1543 if (ImGui_DragLocation(&fOffset)) {
1544 this->preTouchMatrixChanged();
1545 paramsChanged = true;
1546 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001547 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1548 this->preTouchMatrixChanged();
1549 paramsChanged = true;
1550 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001551 }
Brian Osman805a7272018-05-02 15:40:20 -04001552 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1553 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1554 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001555 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001556 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001557 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001558 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001559 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001560 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001561 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001562 }
1563
Ben Wagnera580fb32018-04-17 11:16:32 -04001564 if (ImGui::CollapsingHeader("Paint")) {
1565 int hintingIdx = 0;
1566 if (fPaintOverrides.fHinting) {
Mike Reed9edbf422018-11-07 19:54:33 -05001567 hintingIdx = static_cast<unsigned>(fPaint.getHinting()) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001568 }
1569 if (ImGui::Combo("Hinting", &hintingIdx,
1570 "Default\0None\0Slight\0Normal\0Full\0\0"))
1571 {
1572 if (hintingIdx == 0) {
1573 fPaintOverrides.fHinting = false;
Mike Reed9edbf422018-11-07 19:54:33 -05001574 fPaint.setHinting(kNo_SkFontHinting);
Ben Wagnera580fb32018-04-17 11:16:32 -04001575 } else {
1576 fPaintOverrides.fHinting = true;
Mike Reed9edbf422018-11-07 19:54:33 -05001577 SkFontHinting hinting = SkTo<SkFontHinting>(hintingIdx - 1);
Ben Wagnera580fb32018-04-17 11:16:32 -04001578 fPaint.setHinting(hinting);
1579 }
1580 paramsChanged = true;
1581 }
1582
1583 int aliasIdx = 0;
1584 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1585 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1586 }
1587 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1588 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1589 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1590 {
1591 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1592 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1593 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1594 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1595 if (aliasIdx == 0) {
1596 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1597 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1598 } else {
1599 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1600 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1601 fPaint.setAntiAlias(aliasIdx > 1);
1602 switch (fPaintOverrides.fAntiAlias) {
1603 case SkPaintFields::AntiAliasState::Alias:
1604 break;
1605 case SkPaintFields::AntiAliasState::Normal:
1606 break;
1607 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1608 gSkUseAnalyticAA = true;
1609 gSkForceAnalyticAA = false;
1610 gSkUseDeltaAA = gSkForceDeltaAA = false;
1611 break;
1612 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1613 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1614 gSkUseDeltaAA = gSkForceDeltaAA = false;
1615 break;
1616 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1617 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1618 gSkUseDeltaAA = true;
1619 gSkForceDeltaAA = false;
1620 break;
1621 case SkPaintFields::AntiAliasState::DeltaAAForced:
1622 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1623 gSkUseDeltaAA = gSkForceDeltaAA = true;
1624 break;
1625 }
1626 }
1627 paramsChanged = true;
1628 }
1629
Ben Wagner99a78dc2018-05-09 18:23:51 -04001630 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
1631 SkPaint::Flags flag,
1632 bool (SkPaint::* isFlag)() const,
1633 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001634 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001635 int itemIndex = 0;
1636 if (fPaintOverrides.fFlags & flag) {
1637 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001638 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001639 if (ImGui::Combo(label, &itemIndex, items)) {
1640 if (itemIndex == 0) {
1641 fPaintOverrides.fFlags &= ~flag;
1642 } else {
1643 fPaintOverrides.fFlags |= flag;
1644 (fPaint.*setFlag)(itemIndex == 2);
1645 }
1646 paramsChanged = true;
1647 }
1648 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001649
Ben Wagner99a78dc2018-05-09 18:23:51 -04001650 paintFlag("Dither",
1651 "Default\0No Dither\0Dither\0\0",
1652 SkPaint::kDither_Flag,
1653 &SkPaint::isDither, &SkPaint::setDither);
1654
1655 paintFlag("Fake Bold Glyphs",
1656 "Default\0No Fake Bold\0Fake Bold\0\0",
1657 SkPaint::kFakeBoldText_Flag,
1658 &SkPaint::isFakeBoldText, &SkPaint::setFakeBoldText);
1659
1660 paintFlag("Linear Text",
1661 "Default\0No Linear Text\0Linear Text\0\0",
1662 SkPaint::kLinearText_Flag,
1663 &SkPaint::isLinearText, &SkPaint::setLinearText);
1664
1665 paintFlag("Subpixel Position Glyphs",
1666 "Default\0Pixel Text\0Subpixel Text\0\0",
1667 SkPaint::kSubpixelText_Flag,
1668 &SkPaint::isSubpixelText, &SkPaint::setSubpixelText);
1669
1670 paintFlag("Subpixel Anti-Alias",
1671 "Default\0lcd\0LCD\0\0",
1672 SkPaint::kLCDRenderText_Flag,
1673 &SkPaint::isLCDRenderText, &SkPaint::setLCDRenderText);
1674
1675 paintFlag("Embedded Bitmap Text",
1676 "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0",
1677 SkPaint::kEmbeddedBitmapText_Flag,
1678 &SkPaint::isEmbeddedBitmapText, &SkPaint::setEmbeddedBitmapText);
1679
1680 paintFlag("Force Auto-Hinting",
1681 "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0",
1682 SkPaint::kAutoHinting_Flag,
1683 &SkPaint::isAutohinted, &SkPaint::setAutohinted);
1684
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001685 ImGui::Checkbox("Override TextSize", &fPaintOverrides.fTextSize);
1686 if (fPaintOverrides.fTextSize) {
1687 ImGui::DragFloat2("TextRange", fPaintOverrides.fTextSizeRange,
1688 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
1689 float textSize = fPaint.getTextSize();
1690 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
1691 fPaintOverrides.fTextSizeRange[0],
1692 fPaintOverrides.fTextSizeRange[1],
1693 "%.6f", 2.0f))
1694 {
1695 fPaint.setTextSize(textSize);
1696 this->preTouchMatrixChanged();
1697 paramsChanged = true;
1698 }
1699 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001700 }
1701
Mike Reed81f60ec2018-05-15 10:09:52 -04001702 {
1703 SkMetaData controls;
1704 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1705 if (ImGui::CollapsingHeader("Current Slide")) {
1706 SkMetaData::Iter iter(controls);
1707 const char* name;
1708 SkMetaData::Type type;
1709 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001710 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001711 if (type == SkMetaData::kScalar_Type) {
1712 float val[3];
1713 SkASSERT(count == 3);
1714 controls.findScalars(name, &count, val);
1715 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1716 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001717 }
1718 }
1719 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001720 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001721 }
1722 }
1723 }
1724
Ben Wagner7a3c6742018-04-23 10:01:07 -04001725 if (fShowSlidePicker) {
1726 ImGui::SetNextTreeNodeOpen(true);
1727 }
Brian Osman79086b92017-02-10 13:36:16 -05001728 if (ImGui::CollapsingHeader("Slide")) {
1729 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001730 static ImVector<const char*> filteredSlideNames;
1731 static ImVector<int> filteredSlideIndices;
1732
Brian Osmanfce09c52017-11-14 15:32:20 -05001733 if (fShowSlidePicker) {
1734 ImGui::SetKeyboardFocusHere();
1735 fShowSlidePicker = false;
1736 }
1737
Brian Osman79086b92017-02-10 13:36:16 -05001738 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001739 filteredSlideNames.clear();
1740 filteredSlideIndices.clear();
1741 int filteredIndex = 0;
1742 for (int i = 0; i < fSlides.count(); ++i) {
1743 const char* slideName = fSlides[i]->getName().c_str();
1744 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1745 if (i == fCurrentSlide) {
1746 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001747 }
Brian Osmanf479e422017-11-08 13:11:36 -05001748 filteredSlideNames.push_back(slideName);
1749 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001750 }
Brian Osman79086b92017-02-10 13:36:16 -05001751 }
Brian Osmanf479e422017-11-08 13:11:36 -05001752
Brian Osmanf479e422017-11-08 13:11:36 -05001753 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1754 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001755 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001756 }
1757 }
Brian Osmana109e392017-02-24 09:49:14 -05001758
1759 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001760 ColorMode newMode = fColorMode;
1761 auto cmButton = [&](ColorMode mode, const char* label) {
1762 if (ImGui::RadioButton(label, mode == fColorMode)) {
1763 newMode = mode;
1764 }
1765 };
1766
1767 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001768 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1769 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001770
1771 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001772 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001773 }
1774
1775 // Pick from common gamuts:
1776 int primariesIdx = 4; // Default: Custom
1777 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1778 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1779 primariesIdx = i;
1780 break;
1781 }
1782 }
1783
Brian Osman03115dc2018-11-26 13:55:19 -05001784 // Let user adjust the gamma
1785 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001786
Brian Osmana109e392017-02-24 09:49:14 -05001787 if (ImGui::Combo("Primaries", &primariesIdx,
1788 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1789 if (primariesIdx >= 0 && primariesIdx <= 3) {
1790 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1791 }
1792 }
1793
1794 // Allow direct editing of gamut
1795 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1796 }
Brian Osman79086b92017-02-10 13:36:16 -05001797 }
Brian Salomon99a33902017-03-07 15:16:34 -05001798 if (paramsChanged) {
1799 fDeferredActions.push_back([=]() {
1800 fWindow->setRequestedDisplayParams(params);
1801 fWindow->inval();
1802 this->updateTitle();
1803 });
1804 }
Brian Osman79086b92017-02-10 13:36:16 -05001805 ImGui::End();
1806 }
1807
Brian Osmanf6877092017-02-13 09:39:57 -05001808 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04001809 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
1810 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001811 static int zoomFactor = 8;
1812 if (ImGui::Button("<<")) {
1813 zoomFactor = SkTMax(zoomFactor / 2, 4);
1814 }
1815 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1816 if (ImGui::Button(">>")) {
1817 zoomFactor = SkTMin(zoomFactor * 2, 32);
1818 }
Brian Osmanf6877092017-02-13 09:39:57 -05001819
Ben Wagner3627d2e2018-06-26 14:23:20 -04001820 if (!fZoomWindowFixed) {
1821 ImVec2 mousePos = ImGui::GetMousePos();
1822 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
1823 }
1824 SkScalar x = fZoomWindowLocation.x();
1825 SkScalar y = fZoomWindowLocation.y();
1826 int xInt = SkScalarRoundToInt(x);
1827 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05001828 ImVec2 avail = ImGui::GetContentRegionAvail();
1829
Brian Osmanead517d2017-11-13 15:36:36 -05001830 uint32_t pixel = 0;
1831 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001832 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001833 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001834 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
Ben Wagner3627d2e2018-06-26 14:23:20 -04001835 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05001836 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001837 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1838 }
1839
Brian Osmand67e5182017-12-08 16:46:09 -05001840 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001841 // Translate so the region of the image that's under the mouse cursor is centered
1842 // in the zoom canvas:
1843 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001844 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
1845 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05001846 c->drawImage(this->fLastImage, 0, 0);
1847
1848 SkPaint outline;
1849 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001850 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05001851 });
Brian Osmanf6877092017-02-13 09:39:57 -05001852 }
1853
1854 ImGui::End();
1855 }
Brian Osman79086b92017-02-10 13:36:16 -05001856}
1857
liyuqian2edb0f42016-07-06 14:11:32 -07001858void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001859 for (int i = 0; i < fDeferredActions.count(); ++i) {
1860 fDeferredActions[i]();
1861 }
1862 fDeferredActions.reset();
1863
Brian Osman56a24812017-12-19 11:15:16 -05001864 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001865 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001866 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001867 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001868
Brian Osman79086b92017-02-10 13:36:16 -05001869 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04001870 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
1871 // not be visible, though. So we need to redraw if there is at least one visible window, or
1872 // more than one active window. Newly created windows are active but not visible for one frame
1873 // while they determine their layout and sizing.
1874 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
1875 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07001876 fWindow->inval();
1877 }
jvanverth9f372462016-04-06 06:08:59 -07001878}
liyuqiane5a6cd92016-05-27 08:52:52 -07001879
Florin Malitab632df72018-06-18 21:23:06 -04001880template <typename OptionsFunc>
1881static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
1882 OptionsFunc&& optionsFunc) {
1883 writer.beginObject();
1884 {
1885 writer.appendString(kName , name);
1886 writer.appendString(kValue, value);
1887
1888 writer.beginArray(kOptions);
1889 {
1890 optionsFunc(writer);
1891 }
1892 writer.endArray();
1893 }
1894 writer.endObject();
1895}
1896
1897
liyuqiane5a6cd92016-05-27 08:52:52 -07001898void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001899 if (!fWindow) {
1900 return;
1901 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001902 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001903 return; // Surface hasn't been created yet.
1904 }
1905
Florin Malitab632df72018-06-18 21:23:06 -04001906 SkDynamicMemoryWStream memStream;
1907 SkJSONWriter writer(&memStream);
1908 writer.beginArray();
1909
liyuqianb73c24b2016-06-03 08:47:23 -07001910 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04001911 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
1912 [this](SkJSONWriter& writer) {
1913 for(const auto& slide : fSlides) {
1914 writer.appendString(slide->getName().c_str());
1915 }
1916 });
liyuqiane5a6cd92016-05-27 08:52:52 -07001917
liyuqianb73c24b2016-06-03 08:47:23 -07001918 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04001919 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
1920 [](SkJSONWriter& writer) {
1921 for (const auto& str : kBackendTypeStrings) {
1922 writer.appendString(str);
1923 }
1924 });
liyuqiane5a6cd92016-05-27 08:52:52 -07001925
csmartdalton578f0642017-02-24 16:04:47 -07001926 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04001927 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
1928 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
1929 [this](SkJSONWriter& writer) {
1930 writer.appendS32(0);
1931
1932 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1933 return;
1934 }
1935
1936 for (int msaa : {4, 8, 16}) {
1937 writer.appendS32(msaa);
1938 }
1939 });
csmartdalton578f0642017-02-24 16:04:47 -07001940
csmartdalton61cd31a2017-02-27 17:00:53 -07001941 // Path renderer state
1942 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04001943 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
1944 [this](SkJSONWriter& writer) {
1945 const GrContext* ctx = fWindow->getGrContext();
1946 if (!ctx) {
1947 writer.appendString("Software");
1948 } else {
1949 const auto* caps = ctx->contextPriv().caps();
1950
Florin Malitab632df72018-06-18 21:23:06 -04001951 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
1952 if (fWindow->sampleCount() > 1) {
1953 if (caps->shaderCaps()->pathRenderingSupport()) {
1954 writer.appendString(
1955 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
1956 }
1957 } else {
1958 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1959 writer.appendString(
1960 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
1961 }
1962 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
1963 }
1964 writer.appendString(
1965 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
1966 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
1967 }
1968 });
csmartdalton61cd31a2017-02-27 17:00:53 -07001969
liyuqianb73c24b2016-06-03 08:47:23 -07001970 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04001971 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
1972 [this](SkJSONWriter& writer) {
1973 writer.appendString(kSoftkeyHint);
1974 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1975 writer.appendString(softkey.c_str());
1976 }
1977 });
liyuqianb73c24b2016-06-03 08:47:23 -07001978
Florin Malitab632df72018-06-18 21:23:06 -04001979 writer.endArray();
1980 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07001981
Florin Malitab632df72018-06-18 21:23:06 -04001982 auto data = memStream.detachAsData();
1983
1984 // TODO: would be cool to avoid this copy
1985 const SkString cstring(static_cast<const char*>(data->data()), data->size());
1986
1987 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001988}
1989
1990void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001991 // For those who will add more features to handle the state change in this function:
1992 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1993 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1994 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001995 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001996 for (int i = 0; i < fSlides.count(); ++i) {
1997 if (fSlides[i]->getName().equals(stateValue)) {
1998 this->setCurrentSlide(i);
1999 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002000 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002001 }
Florin Malitaab99c342018-01-16 16:23:03 -05002002
2003 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002004 } else if (stateName.equals(kBackendStateName)) {
2005 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2006 if (stateValue.equals(kBackendTypeStrings[i])) {
2007 if (fBackendType != i) {
2008 fBackendType = (sk_app::Window::BackendType)i;
2009 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002010 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002011 }
2012 break;
2013 }
2014 }
csmartdalton578f0642017-02-24 16:04:47 -07002015 } else if (stateName.equals(kMSAAStateName)) {
2016 DisplayParams params = fWindow->getRequestedDisplayParams();
2017 int sampleCount = atoi(stateValue.c_str());
2018 if (sampleCount != params.fMSAASampleCount) {
2019 params.fMSAASampleCount = sampleCount;
2020 fWindow->setRequestedDisplayParams(params);
2021 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002022 this->updateTitle();
2023 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002024 }
2025 } else if (stateName.equals(kPathRendererStateName)) {
2026 DisplayParams params = fWindow->getRequestedDisplayParams();
2027 for (const auto& pair : gPathRendererNames) {
2028 if (pair.second == stateValue.c_str()) {
2029 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2030 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2031 fWindow->setRequestedDisplayParams(params);
2032 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002033 this->updateTitle();
2034 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002035 }
2036 break;
2037 }
csmartdalton578f0642017-02-24 16:04:47 -07002038 }
liyuqianb73c24b2016-06-03 08:47:23 -07002039 } else if (stateName.equals(kSoftkeyStateName)) {
2040 if (!stateValue.equals(kSoftkeyHint)) {
2041 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002042 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002043 }
liyuqian2edb0f42016-07-06 14:11:32 -07002044 } else if (stateName.equals(kRefreshStateName)) {
2045 // This state is actually NOT in the UI state.
2046 // We use this to allow Android to quickly set bool fRefresh.
2047 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002048 } else {
2049 SkDebugf("Unknown stateName: %s", stateName.c_str());
2050 }
2051}
Brian Osman79086b92017-02-10 13:36:16 -05002052
2053bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002054 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002055}
2056
2057bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002058 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002059 fWindow->inval();
2060 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002061 } else {
2062 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002063 }
Brian Osman79086b92017-02-10 13:36:16 -05002064}