blob: 90c4dfc1504dcf93fa914b9528afb5d0fbd70880 [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
jvanverth34524262016-05-04 13:49:13 -07008#include "Viewer.h"
jvanverth9f372462016-04-06 06:08:59 -07009
Chris Dalton2d18f412018-02-20 13:23:32 -070010#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070011#include "GMSlide.h"
liyuqian6f163d22016-06-13 12:26:45 -070012#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050013#include "Resources.h"
jvanverthc7027ab2016-06-16 09:52:35 -070014#include "SampleSlide.h"
Florin Malita54f65c42018-01-16 17:04:30 -050015#include "SkottieSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070016#include "SKPSlide.h"
Florin Malita76a076b2018-02-15 18:40:48 -050017#include "SlideDir.h"
Florin Malitac659c2c2018-04-05 11:57:21 -040018#include "SvgSlide.h"
jvanverth9f372462016-04-06 06:08:59 -070019
csmartdalton61cd31a2017-02-27 17:00:53 -070020#include "GrContext.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070021#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050022#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040023#include "SkColorSpaceXformCanvas.h"
Ben Wagner483c7722018-02-20 17:06:07 -050024#include "SkCommonFlags.h"
Brian Osman2dd96932016-10-18 15:33:53 -040025#include "SkCommandLineFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070026#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040027#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050028#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040029#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050030#include "SkImagePriv.h"
Herb Derbyefe39bc2018-05-01 17:06:20 -040031#include "SkMakeUnique.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070032#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050033#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050034#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050035#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040036#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070037#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070038#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050039#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050040#include "SkTestFontMgr.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050041#include "SkThreadedBMPDevice.h"
jvanverth9f372462016-04-06 06:08:59 -070042
Brian Osman79086b92017-02-10 13:36:16 -050043#include "imgui.h"
44
Chris Dalton1a325d22017-07-14 15:17:41 -060045#include "ccpr/GrCoverageCountingPathRenderer.h"
46
csmartdalton578f0642017-02-24 16:04:47 -070047#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070048#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070049
jvanverth34524262016-05-04 13:49:13 -070050using namespace sk_app;
51
csmartdalton61cd31a2017-02-27 17:00:53 -070052static std::map<GpuPathRenderers, std::string> gPathRendererNames;
53
jvanverth9f372462016-04-06 06:08:59 -070054Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070055 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070056}
57
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060058static DEFINE_string(slide, "", "Start on this sample.");
59static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050060
bsalomon6c471f72016-07-26 12:56:32 -070061#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070062# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070063#else
64# define BACKENDS_STR "\"sw\" and \"gl\""
65#endif
66
Brian Osman2dd96932016-10-18 15:33:53 -040067static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070068
Brian Salomonbdecacf2018-02-02 20:32:49 -050069static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070070
Chris Dalton2d18f412018-02-20 13:23:32 -070071DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
72
Brian Osman53136aa2017-07-20 15:43:35 -040073DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050074
Brian Salomon194db172017-08-17 14:37:06 -040075const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070076 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040077#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
78 "ANGLE",
79#endif
jvanverth063ece72016-06-17 09:29:14 -070080#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070081 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070082#endif
csmartdalton578f0642017-02-24 16:04:47 -070083 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070084};
85
bsalomon6c471f72016-07-26 12:56:32 -070086static sk_app::Window::BackendType get_backend_type(const char* str) {
87#ifdef SK_VULKAN
88 if (0 == strcmp(str, "vk")) {
89 return sk_app::Window::kVulkan_BackendType;
90 } else
91#endif
Brian Salomon194db172017-08-17 14:37:06 -040092#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
93 if (0 == strcmp(str, "angle")) {
94 return sk_app::Window::kANGLE_BackendType;
95 } else
96#endif
bsalomon6c471f72016-07-26 12:56:32 -070097 if (0 == strcmp(str, "gl")) {
98 return sk_app::Window::kNativeGL_BackendType;
99 } else if (0 == strcmp(str, "sw")) {
100 return sk_app::Window::kRaster_BackendType;
101 } else {
102 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
103 return sk_app::Window::kRaster_BackendType;
104 }
105}
106
Brian Osmana109e392017-02-24 09:49:14 -0500107static SkColorSpacePrimaries gSrgbPrimaries = {
108 0.64f, 0.33f,
109 0.30f, 0.60f,
110 0.15f, 0.06f,
111 0.3127f, 0.3290f };
112
113static SkColorSpacePrimaries gAdobePrimaries = {
114 0.64f, 0.33f,
115 0.21f, 0.71f,
116 0.15f, 0.06f,
117 0.3127f, 0.3290f };
118
119static SkColorSpacePrimaries gP3Primaries = {
120 0.680f, 0.320f,
121 0.265f, 0.690f,
122 0.150f, 0.060f,
123 0.3127f, 0.3290f };
124
125static SkColorSpacePrimaries gRec2020Primaries = {
126 0.708f, 0.292f,
127 0.170f, 0.797f,
128 0.131f, 0.046f,
129 0.3127f, 0.3290f };
130
131struct NamedPrimaries {
132 const char* fName;
133 SkColorSpacePrimaries* fPrimaries;
134} gNamedPrimaries[] = {
135 { "sRGB", &gSrgbPrimaries },
136 { "AdobeRGB", &gAdobePrimaries },
137 { "P3", &gP3Primaries },
138 { "Rec. 2020", &gRec2020Primaries },
139};
140
141static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
142 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
143}
144
Brian Osman70d2f432017-11-08 09:54:10 -0500145static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
146 // In raster mode, we still use GL for the window.
147 // This lets us render the GUI faster (and correct).
148 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
149}
150
liyuqiane5a6cd92016-05-27 08:52:52 -0700151const char* kName = "name";
152const char* kValue = "value";
153const char* kOptions = "options";
154const char* kSlideStateName = "Slide";
155const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700156const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700157const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700158const char* kSoftkeyStateName = "Softkey";
159const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700160const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700161const char* kON = "ON";
162const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700163const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700164
jvanverth34524262016-05-04 13:49:13 -0700165Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500166 : fCurrentSlide(-1)
167 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500168 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500169 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500170 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500171 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500172 , fShowZoomWindow(false)
173 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700174 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500175 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500176 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500177 // Our UI can only tweak gamma (currently), so start out gamma-only
178 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700179 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400180 , fRotation(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400181 , fGestureDevice(GestureDevice::kNone)
Brian Osman805a7272018-05-02 15:40:20 -0400182 , fPerspectiveMode(kPerspective_Off)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500183 , fTileCnt(0)
184 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700185{
Greg Daniel285db442016-10-14 09:12:53 -0400186 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700187
Brian Osmanf09e35e2017-12-15 14:48:09 -0500188 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
189 gPathRendererNames[GpuPathRenderers::kDefault] =
190 "Default Ganesh Behavior (best path renderer, not including CCPR)";
191 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500192 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
193 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
194 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
195 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700196
jvanverth2bb3b6d2016-04-08 07:24:09 -0700197 SkDebugf("Command line arguments: ");
198 for (int i = 1; i < argc; ++i) {
199 SkDebugf("%s ", argv[i]);
200 }
201 SkDebugf("\n");
202
203 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500204#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400205 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500206#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700207
Ben Wagner483c7722018-02-20 17:06:07 -0500208 if (!FLAGS_nativeFonts) {
209 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
210 }
211
Brian Osmanbc8150f2017-07-24 11:38:01 -0400212 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400213 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400214
bsalomon6c471f72016-07-26 12:56:32 -0700215 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700216 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700217
csmartdalton578f0642017-02-24 16:04:47 -0700218 DisplayParams displayParams;
219 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700220 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700221 fWindow->setRequestedDisplayParams(displayParams);
222
Brian Osman56a24812017-12-19 11:15:16 -0500223 // Configure timers
224 fStatsLayer.setActive(false);
225 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
226 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
227 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
228
jvanverth9f372462016-04-06 06:08:59 -0700229 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700230 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500231 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500232 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500233 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700234
brianosman622c8d52016-05-10 06:50:49 -0700235 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500236 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
237 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
238 fWindow->inval();
239 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500240 // Command to jump directly to the slide picker and give it focus
241 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
242 this->fShowImGuiDebugWindow = true;
243 this->fShowSlidePicker = true;
244 fWindow->inval();
245 });
246 // Alias that to Backspace, to match SampleApp
247 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
248 this->fShowImGuiDebugWindow = true;
249 this->fShowSlidePicker = true;
250 fWindow->inval();
251 });
Brian Osman79086b92017-02-10 13:36:16 -0500252 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
253 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
254 fWindow->inval();
255 });
Brian Osmanf6877092017-02-13 09:39:57 -0500256 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
257 this->fShowZoomWindow = !this->fShowZoomWindow;
258 fWindow->inval();
259 });
brianosman622c8d52016-05-10 06:50:49 -0700260 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500261 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700262 fWindow->inval();
263 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400264 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500265 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400266 this->updateTitle();
267 fWindow->inval();
268 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500269 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500270 switch (fColorMode) {
271 case ColorMode::kLegacy:
272 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
273 break;
274 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
275 this->setColorMode(ColorMode::kColorManagedSRGB8888);
276 break;
277 case ColorMode::kColorManagedSRGB8888:
278 this->setColorMode(ColorMode::kColorManagedLinearF16);
279 break;
280 case ColorMode::kColorManagedLinearF16:
281 this->setColorMode(ColorMode::kLegacy);
282 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500283 }
brianosman622c8d52016-05-10 06:50:49 -0700284 });
285 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500286 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700287 });
288 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500289 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700290 });
291 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
292 this->changeZoomLevel(1.f / 32.f);
293 fWindow->inval();
294 });
295 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
296 this->changeZoomLevel(-1.f / 32.f);
297 fWindow->inval();
298 });
jvanverthaf236b52016-05-20 06:01:06 -0700299 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400300 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
301 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500302 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400303#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
304 if (newBackend == sk_app::Window::kVulkan_BackendType) {
305 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
306 sk_app::Window::kBackendTypeCount);
307 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
308 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500309 }
310#endif
Brian Osman621491e2017-02-28 15:45:01 -0500311 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700312 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500313 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
314 fTileCnt++;
315 if (fThreadCnt == 0) {
316 this->resetExecutor();
317 }
318 this->updateTitle();
319 fWindow->inval();
320 });
321 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
322 fTileCnt = SkTMax(0, fTileCnt - 1);
323 if (fThreadCnt == 0) {
324 this->resetExecutor();
325 }
326 this->updateTitle();
327 fWindow->inval();
328 });
329 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
330 if (fTileCnt == 0) {
331 return;
332 }
333 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
334 this->resetExecutor();
335 this->updateTitle();
336 fWindow->inval();
337 });
338 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
339 if (fTileCnt == 0) {
340 return;
341 }
342 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
343 this->resetExecutor();
344 this->updateTitle();
345 fWindow->inval();
346 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500347 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
348 fSaveToSKP = true;
349 fWindow->inval();
350 });
Ben Wagner37c54032018-04-13 14:30:23 -0400351 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
352 DisplayParams params = fWindow->getRequestedDisplayParams();
353 uint32_t flags = params.fSurfaceProps.flags();
354 if (!fPixelGeometryOverrides) {
355 fPixelGeometryOverrides = true;
356 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
357 } else {
358 switch (params.fSurfaceProps.pixelGeometry()) {
359 case kUnknown_SkPixelGeometry:
360 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
361 break;
362 case kRGB_H_SkPixelGeometry:
363 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
364 break;
365 case kBGR_H_SkPixelGeometry:
366 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
367 break;
368 case kRGB_V_SkPixelGeometry:
369 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
370 break;
371 case kBGR_V_SkPixelGeometry:
372 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
373 fPixelGeometryOverrides = false;
374 break;
375 }
376 }
377 fWindow->setRequestedDisplayParams(params);
378 this->updateTitle();
379 fWindow->inval();
380 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500381 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
382 if (!fPaintOverrides.fHinting) {
383 fPaintOverrides.fHinting = true;
384 fPaint.setHinting(SkPaint::kNo_Hinting);
385 } else {
386 switch (fPaint.getHinting()) {
387 case SkPaint::kNo_Hinting:
388 fPaint.setHinting(SkPaint::kSlight_Hinting);
389 break;
390 case SkPaint::kSlight_Hinting:
391 fPaint.setHinting(SkPaint::kNormal_Hinting);
392 break;
393 case SkPaint::kNormal_Hinting:
394 fPaint.setHinting(SkPaint::kFull_Hinting);
395 break;
396 case SkPaint::kFull_Hinting:
397 fPaint.setHinting(SkPaint::kNo_Hinting);
398 fPaintOverrides.fHinting = false;
399 break;
400 }
401 }
402 this->updateTitle();
403 fWindow->inval();
404 });
405 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
406 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
407 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
408 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
409 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500410 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
411 gSkUseDeltaAA = gSkForceDeltaAA = false;
412 } else {
413 fPaint.setAntiAlias(true);
414 switch (fPaintOverrides.fAntiAlias) {
415 case SkPaintFields::AntiAliasState::Alias:
416 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400417 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
418 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500419 break;
420 case SkPaintFields::AntiAliasState::Normal:
421 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500422 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400423 gSkForceAnalyticAA = false;
424 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500425 break;
426 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
427 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400428 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
429 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500430 break;
431 case SkPaintFields::AntiAliasState::AnalyticAAForced:
432 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
433 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
434 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400435 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500436 break;
437 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
438 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400439 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
440 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500441 break;
442 case SkPaintFields::AntiAliasState::DeltaAAForced:
443 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
444 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
445 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
446 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
447 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
448 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
449 break;
450 }
451 }
452 this->updateTitle();
453 fWindow->inval();
454 });
Ben Wagner37c54032018-04-13 14:30:23 -0400455 fCommands.addCommand('D', "Modes", "DFT", [this]() {
456 DisplayParams params = fWindow->getRequestedDisplayParams();
457 uint32_t flags = params.fSurfaceProps.flags();
458 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
459 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
460 fWindow->setRequestedDisplayParams(params);
461 this->updateTitle();
462 fWindow->inval();
463 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500464 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
465 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
466 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
467 fPaint.setLCDRenderText(false);
468 } else {
469 if (!fPaint.isLCDRenderText()) {
470 fPaint.setLCDRenderText(true);
471 } else {
472 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
473 }
474 }
475 this->updateTitle();
476 fWindow->inval();
477 });
478 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
479 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
480 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
481 fPaint.setSubpixelText(false);
482 } else {
483 if (!fPaint.isSubpixelText()) {
484 fPaint.setSubpixelText(true);
485 } else {
486 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
487 }
488 }
489 this->updateTitle();
490 fWindow->inval();
491 });
Brian Osman805a7272018-05-02 15:40:20 -0400492 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
493 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
494 : kPerspective_Real;
495 this->updateTitle();
496 fWindow->inval();
497 });
498 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
499 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
500 : kPerspective_Off;
501 this->updateTitle();
502 fWindow->inval();
503 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500504
jvanverth2bb3b6d2016-04-08 07:24:09 -0700505 // set up slides
506 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500507 if (FLAGS_list) {
508 this->listNames();
509 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700510
Brian Osman9bb47cf2018-04-26 15:55:00 -0400511 fPerspectivePoints[0].set(0, 0);
512 fPerspectivePoints[1].set(1, 0);
513 fPerspectivePoints[2].set(0, 1);
514 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700515 fAnimTimer.run();
516
Hal Canaryc465d132017-12-08 10:21:31 -0500517 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500518 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400519 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500520 }
521 fImGuiGamutPaint.setColor(SK_ColorWHITE);
522 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
523
Brian Osman70d2f432017-11-08 09:54:10 -0500524 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500525 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700526}
527
jvanverth34524262016-05-04 13:49:13 -0700528void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700529 fAllSlideNames = Json::Value(Json::arrayValue);
530
Chris Dalton2d18f412018-02-20 13:23:32 -0700531 // Bisect slide.
532 if (!FLAGS_bisect.isEmpty()) {
533 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
534 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
535 if (FLAGS_bisect.count() >= 2) {
536 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
537 bisect->onChar(*ch);
538 }
539 }
540 fSlides.push_back(std::move(bisect));
541 }
542 }
543
544 // GMs
545 int firstGM = fSlides.count();
jvanverth2bb3b6d2016-04-08 07:24:09 -0700546 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
547 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400548 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700549
550 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
551 sk_sp<Slide> slide(new GMSlide(gm.release()));
Chris Dalton2d18f412018-02-20 13:23:32 -0700552 fSlides.push_back(std::move(slide));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700553 }
554
555 gms = gms->next();
556 }
Chris Dalton2d18f412018-02-20 13:23:32 -0700557 // reverse gms
558 int numGMs = fSlides.count() - firstGM;
559 for (int i = 0; i < numGMs/2; ++i) {
560 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
jvanverth2bb3b6d2016-04-08 07:24:09 -0700561 }
562
jvanverthc7027ab2016-06-16 09:52:35 -0700563 // samples
564 const SkViewRegister* reg = SkViewRegister::Head();
565 while (reg) {
566 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700567 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
568 fSlides.push_back(slide);
569 }
jvanverthc7027ab2016-06-16 09:52:35 -0700570 reg = reg->next();
571 }
572
Florin Malita0ffa3222018-04-05 14:34:45 -0400573 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
574 static const struct {
575 const char* fExtension;
576 const char* fDirName;
577 const SkCommandLineFlags::StringArray& fFlags;
578 const SlideFactory fFactory;
579 } gExternalSlidesInfo[] = {
580 { ".skp", "skp-dir", FLAGS_skps,
581 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
582 return sk_make_sp<SKPSlide>(name, path);}
583 },
584 { ".jpg", "jpg-dir", FLAGS_jpgs,
585 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
586 return sk_make_sp<ImageSlide>(name, path);}
587 },
588 { ".json", "skottie-dir", FLAGS_jsons,
589 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
590 return sk_make_sp<SkottieSlide>(name, path);}
591 },
592 { ".svg", "svg-dir", FLAGS_svgs,
593 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
594 return sk_make_sp<SvgSlide>(name, path);}
595 },
596 };
jvanverthc265a922016-04-08 12:51:45 -0700597
Florin Malita0ffa3222018-04-05 14:34:45 -0400598 SkTArray<sk_sp<Slide>, true> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700599
Florin Malita0ffa3222018-04-05 14:34:45 -0400600 const auto addSlide = [&](const SkString& name,
601 const SkString& path,
602 const SlideFactory& fact) {
603 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
604 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700605 }
liyuqian6f163d22016-06-13 12:26:45 -0700606
Florin Malita0ffa3222018-04-05 14:34:45 -0400607 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500608 dirSlides.push_back(slide);
609 fSlides.push_back(std::move(slide));
610 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400611 };
Florin Malita76a076b2018-02-15 18:40:48 -0500612
Florin Malita0ffa3222018-04-05 14:34:45 -0400613 for (const auto& info : gExternalSlidesInfo) {
614 for (const auto& flag : info.fFlags) {
615 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
616 // single file
617 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
618 } else {
619 // directory
620 SkOSFile::Iter it(flag.c_str(), info.fExtension);
621 SkString name;
622 while (it.next(&name)) {
623 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
624 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400625 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400626
627 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);
689 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
690 if (fPaint.isAntiAlias()) {
691 paintTitle.append("Antialias");
692 } else {
693 paintTitle.append("Alias");
694 }
695 }
696 if (fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag) {
697 if (fPaint.isLCDRenderText()) {
698 paintTitle.append("LCD");
699 } else {
700 paintTitle.append("lcd");
701 }
702 }
703 if (fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag) {
704 if (fPaint.isSubpixelText()) {
705 paintTitle.append("Subpixel Glyphs");
706 } else {
707 paintTitle.append("Pixel Glyphs");
708 }
709 }
710 if (fPaintOverrides.fHinting) {
711 switch (fPaint.getHinting()) {
712 case SkPaint::kNo_Hinting:
713 paintTitle.append("No Hinting");
714 break;
715 case SkPaint::kSlight_Hinting:
716 paintTitle.append("Slight Hinting");
717 break;
718 case SkPaint::kNormal_Hinting:
719 paintTitle.append("Normal Hinting");
720 break;
721 case SkPaint::kFull_Hinting:
722 paintTitle.append("Full Hinting");
723 break;
724 }
725 }
726 paintTitle.done();
727
Yuqian Lib2ba6642017-11-22 12:07:41 -0500728 if (fTileCnt > 0) {
729 title.appendf(" T%d", fTileCnt);
730 if (fThreadCnt > 0) {
731 title.appendf("/%d", fThreadCnt);
732 }
733 }
734
Brian Osman92004802017-03-06 11:47:26 -0500735 switch (fColorMode) {
736 case ColorMode::kLegacy:
737 title.append(" Legacy 8888");
738 break;
739 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
740 title.append(" ColorManaged 8888 (Nonlinear blending)");
741 break;
742 case ColorMode::kColorManagedSRGB8888:
743 title.append(" ColorManaged 8888");
744 break;
745 case ColorMode::kColorManagedLinearF16:
746 title.append(" ColorManaged F16");
747 break;
748 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500749
Brian Osman92004802017-03-06 11:47:26 -0500750 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500751 int curPrimaries = -1;
752 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
753 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
754 curPrimaries = i;
755 break;
756 }
757 }
758 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500759
760 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
761 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
762 }
brianosman05de2162016-05-06 13:28:57 -0700763 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500764
Ben Wagner37c54032018-04-13 14:30:23 -0400765 const DisplayParams& params = fWindow->getRequestedDisplayParams();
766 if (fPixelGeometryOverrides) {
767 switch (params.fSurfaceProps.pixelGeometry()) {
768 case kUnknown_SkPixelGeometry:
769 title.append( " Flat");
770 break;
771 case kRGB_H_SkPixelGeometry:
772 title.append( " RGB");
773 break;
774 case kBGR_H_SkPixelGeometry:
775 title.append( " BGR");
776 break;
777 case kRGB_V_SkPixelGeometry:
778 title.append( " RGBV");
779 break;
780 case kBGR_V_SkPixelGeometry:
781 title.append( " BGRV");
782 break;
783 }
784 }
785
786 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
787 title.append(" DFT");
788 }
789
csmartdalton578f0642017-02-24 16:04:47 -0700790 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700791 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500792 int msaa = fWindow->sampleCount();
793 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700794 title.appendf(" MSAA: %i", msaa);
795 }
796 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700797
798 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400799 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700800 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
801 }
802
Brian Osman805a7272018-05-02 15:40:20 -0400803 if (kPerspective_Real == fPerspectiveMode) {
804 title.append(" Perpsective (Real)");
805 } else if (kPerspective_Fake == fPerspectiveMode) {
806 title.append(" Perspective (Fake)");
807 }
808
brianosman05de2162016-05-06 13:28:57 -0700809 fWindow->setTitle(title.c_str());
810}
811
Florin Malitaab99c342018-01-16 16:23:03 -0500812int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500813
814 if (!FLAGS_slide.isEmpty()) {
815 int count = fSlides.count();
816 for (int i = 0; i < count; i++) {
817 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500818 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500819 }
820 }
821
822 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
823 this->listNames();
824 }
825
Florin Malitaab99c342018-01-16 16:23:03 -0500826 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500827}
828
Florin Malitaab99c342018-01-16 16:23:03 -0500829void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500830 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500831 for (const auto& slide : fSlides) {
832 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500833 }
834}
835
Florin Malitaab99c342018-01-16 16:23:03 -0500836void Viewer::setCurrentSlide(int slide) {
837 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700838
Florin Malitaab99c342018-01-16 16:23:03 -0500839 if (slide == fCurrentSlide) {
840 return;
841 }
842
843 if (fCurrentSlide >= 0) {
844 fSlides[fCurrentSlide]->unload();
845 }
846
847 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
848 SkIntToScalar(fWindow->height()));
849 fCurrentSlide = slide;
850 this->setupCurrentSlide();
851}
852
853void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500854 if (fCurrentSlide >= 0) {
855 // prepare dimensions for image slides
856 fGesture.resetTouchState();
857 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700858
Jim Van Verth0848fb02018-01-22 13:39:30 -0500859 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
860 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
861 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400862
Jim Van Verth0848fb02018-01-22 13:39:30 -0500863 // Start with a matrix that scales the slide to the available screen space
864 if (fWindow->scaleContentToFit()) {
865 if (windowRect.width() > 0 && windowRect.height() > 0) {
866 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
867 }
liyuqiane46e4f02016-05-20 07:32:19 -0700868 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500869
870 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400871 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500872
873 this->updateTitle();
874 this->updateUIState();
875
876 fStatsLayer.resetMeasurements();
877
878 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700879 }
jvanverthc265a922016-04-08 12:51:45 -0700880}
881
882#define MAX_ZOOM_LEVEL 8
883#define MIN_ZOOM_LEVEL -8
884
jvanverth34524262016-05-04 13:49:13 -0700885void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700886 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400887 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400888 this->preTouchMatrixChanged();
889}
Yuqian Li755778c2018-03-28 16:23:31 -0400890
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400891void Viewer::preTouchMatrixChanged() {
892 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400893 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
894 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
895 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
896 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
897}
898
Brian Osman805a7272018-05-02 15:40:20 -0400899SkMatrix Viewer::computePerspectiveMatrix() {
900 SkScalar w = fWindow->width(), h = fWindow->height();
901 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
902 SkPoint perspPts[4] = {
903 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
904 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
905 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
906 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
907 };
908 SkMatrix m;
909 m.setPolyToPoly(orthoPts, perspPts, 4);
910 return m;
911}
912
Yuqian Li755778c2018-03-28 16:23:31 -0400913SkMatrix Viewer::computePreTouchMatrix() {
914 SkMatrix m = fDefaultMatrix;
915 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
916 : SK_Scalar1 + fZoomLevel;
917 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400918
919 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
920 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400921
Brian Osman805a7272018-05-02 15:40:20 -0400922 if (kPerspective_Real == fPerspectiveMode) {
923 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400924 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400925 }
926
Yuqian Li755778c2018-03-28 16:23:31 -0400927 return m;
jvanverthc265a922016-04-08 12:51:45 -0700928}
929
liyuqiand3cdbca2016-05-17 12:44:20 -0700930SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400931 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700932 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400933 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700934 return m;
jvanverthc265a922016-04-08 12:51:45 -0700935}
936
Brian Osman621491e2017-02-28 15:45:01 -0500937void Viewer::setBackend(sk_app::Window::BackendType backendType) {
938 fBackendType = backendType;
939
940 fWindow->detach();
941
Brian Osman70d2f432017-11-08 09:54:10 -0500942#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400943 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
944 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500945 DisplayParams params = fWindow->getRequestedDisplayParams();
946 delete fWindow;
947 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500948
Brian Osman70d2f432017-11-08 09:54:10 -0500949 // re-register callbacks
950 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500951 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500952 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500953 fWindow->pushLayer(&fImGuiLayer);
954
Brian Osman70d2f432017-11-08 09:54:10 -0500955 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
956 // will still include our correct sample count. But the re-created fWindow will lose that
957 // information. On Windows, we need to re-create the window when changing sample count,
958 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
959 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
960 // as if we had never changed windows at all).
961 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500962#endif
963
Brian Osman70d2f432017-11-08 09:54:10 -0500964 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500965}
966
Brian Osman92004802017-03-06 11:47:26 -0500967void Viewer::setColorMode(ColorMode colorMode) {
968 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700969
Brian Osmanf750fbc2017-02-08 10:47:28 -0500970 // When we're in color managed mode, we tag our window surface as sRGB. If we've switched into
Brian Osmane0d4fba2017-03-15 10:24:55 -0400971 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700972 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500973 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400974 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
975 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500976 if (wasInLegacy != wantLegacy) {
977 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700978 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500979 }
980
981 this->updateTitle();
982 fWindow->inval();
983}
984
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500985class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
986public:
987 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
988 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
989 { }
990 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -0400991 if (*paint == nullptr) {
992 return true;
993 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500994 if (fPaintOverrides->fHinting) {
995 paint->writable()->setHinting(fPaint->getHinting());
996 }
997 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
998 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
999 }
1000 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
1001 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
1002 }
1003 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
1004 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
1005 }
1006 return true;
1007 }
1008 SkPaint* fPaint;
1009 Viewer::SkPaintFields* fPaintOverrides;
1010};
1011
Brian Osmanf750fbc2017-02-08 10:47:28 -05001012void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001013 SkAutoCanvasRestore autorestore(canvas, false);
1014
Brian Osmanf750fbc2017-02-08 10:47:28 -05001015 // By default, we render directly into the window's surface/canvas
1016 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001017 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001018
Brian Osmane0d4fba2017-03-15 10:24:55 -04001019 // If we're in any of the color managed modes, construct the color space we're going to use
1020 sk_sp<SkColorSpace> cs = nullptr;
1021 if (ColorMode::kLegacy != fColorMode) {
1022 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
1023 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -04001024 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001025 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -05001026 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1027 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
1028 } else {
1029 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
1030 }
Brian Osmane0d4fba2017-03-15 10:24:55 -04001031 }
1032
Brian Osman3ac99cf2017-12-01 11:23:53 -05001033 if (fSaveToSKP) {
1034 SkPictureRecorder recorder;
1035 SkCanvas* recorderCanvas = recorder.beginRecording(
1036 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1037 // In xform-canvas mode, record the transformed output
1038 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1039 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1040 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
1041 recorderCanvas = xformCanvas.get();
1042 }
1043 fSlides[fCurrentSlide]->draw(recorderCanvas);
1044 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1045 SkFILEWStream stream("sample_app.skp");
1046 picture->serialize(&stream);
1047 fSaveToSKP = false;
1048 }
1049
Brian Osmane0d4fba2017-03-15 10:24:55 -04001050 // If we're in F16, or we're zooming, or we're in color correct 8888 and the gamut isn't sRGB,
Brian Osman70d2f432017-11-08 09:54:10 -05001051 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
Brian Osman805a7272018-05-02 15:40:20 -04001052 // because the window surface is actually GL, or we're doing fake perspective.
Brian Osmanf750fbc2017-02-08 10:47:28 -05001053 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -05001054 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -05001055 if (Window::kRaster_BackendType == fBackendType ||
Brian Osman805a7272018-05-02 15:40:20 -04001056 kPerspective_Fake == fPerspectiveMode ||
Brian Osman70d2f432017-11-08 09:54:10 -05001057 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -05001058 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -04001059 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
1060 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
1061
Brian Osman92004802017-03-06 11:47:26 -05001062 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
1063 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001064 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
1065 // with a special canvas (below) that has the color space attached
1066 sk_sp<SkColorSpace> offscreenColorSpace =
1067 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -05001068 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -04001069 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Ben Wagner37c54032018-04-13 14:30:23 -04001070 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1071 canvas->getProps(&props);
1072 offscreenSurface = Window::kRaster_BackendType == fBackendType
1073 ? SkSurface::MakeRaster(info, &props)
1074 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -05001075 SkPixmap offscreenPixmap;
1076 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
1077 SkBitmap offscreenBitmap;
1078 offscreenBitmap.installPixels(offscreenPixmap);
Herb Derbyefe39bc2018-05-01 17:06:20 -04001079 threadedCanvas =
1080 skstd::make_unique<SkCanvas>(
1081 sk_make_sp<SkThreadedBMPDevice>(
1082 offscreenBitmap, fTileCnt, fThreadCnt, fExecutor.get()));
Yuqian Lib2ba6642017-11-22 12:07:41 -05001083 slideCanvas = threadedCanvas.get();
1084 } else {
1085 slideCanvas = offscreenSurface->getCanvas();
1086 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001087 }
1088
Brian Osmane0d4fba2017-03-15 10:24:55 -04001089 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1090 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1091 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
1092 slideCanvas = xformCanvas.get();
1093 }
1094
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001095 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001096 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001097 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -05001098 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001099 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001100 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1101 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001102 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001103 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001104
1105 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001106 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001107 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001108 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001109
1110 // If we rendered offscreen, snap an image and push the results to the window's canvas
1111 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001112 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001113
1114 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -04001115 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001116 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001117 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001118 SkPaint paint;
1119 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001120 int prePerspectiveCount = canvas->save();
1121 if (kPerspective_Fake == fPerspectiveMode) {
1122 paint.setFilterQuality(kHigh_SkFilterQuality);
1123 canvas->clear(SK_ColorWHITE);
1124 canvas->concat(this->computePerspectiveMatrix());
1125 }
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001126 canvas->drawImage(retaggedImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001127 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001128 }
liyuqian6f163d22016-06-13 12:26:45 -07001129}
1130
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001131void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001132 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001133 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001134}
Jim Van Verth6f449692017-02-14 15:16:46 -05001135
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001136void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001137 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001138
brianosman622c8d52016-05-10 06:50:49 -07001139 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001140
Brian Osmand67e5182017-12-08 16:46:09 -05001141 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -05001142
Brian Osman1df161a2017-02-09 12:10:20 -05001143 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -04001144 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001145}
1146
Florin Malitacefc1b92018-02-19 21:43:47 -05001147SkPoint Viewer::mapEvent(float x, float y) {
1148 const auto m = this->computeMatrix();
1149 SkMatrix inv;
1150
1151 SkAssertResult(m.invert(&inv));
1152
1153 return inv.mapXY(x, y);
1154}
1155
jvanverth814e38d2016-06-06 08:48:47 -07001156bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001157 if (GestureDevice::kMouse == fGestureDevice) {
1158 return false;
1159 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001160
1161 const auto slidePt = this->mapEvent(x, y);
1162 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1163 fWindow->inval();
1164 return true;
1165 }
1166
liyuqiand3cdbca2016-05-17 12:44:20 -07001167 void* castedOwner = reinterpret_cast<void*>(owner);
1168 switch (state) {
1169 case Window::kUp_InputState: {
1170 fGesture.touchEnd(castedOwner);
1171 break;
1172 }
1173 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001174 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001175 break;
1176 }
1177 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001178 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001179 break;
1180 }
1181 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001182 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001183 fWindow->inval();
1184 return true;
1185}
1186
Brian Osman80fc07e2017-12-08 16:45:43 -05001187bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001188 if (GestureDevice::kTouch == fGestureDevice) {
1189 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001190 }
Brian Osman16c81a12017-12-20 11:58:34 -05001191
Florin Malitacefc1b92018-02-19 21:43:47 -05001192 const auto slidePt = this->mapEvent(x, y);
1193 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1194 fWindow->inval();
1195 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001196 }
1197
1198 switch (state) {
1199 case Window::kUp_InputState: {
1200 fGesture.touchEnd(nullptr);
1201 break;
1202 }
1203 case Window::kDown_InputState: {
1204 fGesture.touchBegin(nullptr, x, y);
1205 break;
1206 }
1207 case Window::kMove_InputState: {
1208 fGesture.touchMoved(nullptr, x, y);
1209 break;
1210 }
1211 }
1212 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1213
1214 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1215 fWindow->inval();
1216 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001217 return true;
1218}
1219
Brian Osmana109e392017-02-24 09:49:14 -05001220static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1221 const ImVec2& pos, const ImVec2& size) {
1222 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1223 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1224
1225 // Invisible 10x10 button
1226 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1227 ImGui::InvisibleButton(label, ImVec2(10, 10));
1228
1229 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1230 ImGuiIO& io = ImGui::GetIO();
1231 // Normalized mouse position, relative to our gamut box
1232 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1233 // Clamp to edge of box, convert back to primary scale
1234 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1235 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1236 }
1237
1238 if (ImGui::IsItemHovered()) {
1239 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1240 }
1241
1242 // Return screen coordinates for the caller. We could just return center here, but we'd have
1243 // one frame of lag during drag.
1244 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1245}
1246
1247static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1248 ImDrawList* drawList = ImGui::GetWindowDrawList();
1249
1250 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1251 // width, and scale the height to maintain aspect ratio.
1252 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1253 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1254 ImVec2 pos = ImGui::GetCursorScreenPos();
1255
1256 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1257 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1258 // Magic numbers are pixel locations of the origin and upper-right corner.
1259 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1260 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001261
1262 // Primary markers
1263 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1264 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1265 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1266 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1267
1268 // Gamut triangle
1269 drawList->AddCircle(r, 5.0f, 0xFF000040);
1270 drawList->AddCircle(g, 5.0f, 0xFF004000);
1271 drawList->AddCircle(b, 5.0f, 0xFF400000);
1272 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1273 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1274
1275 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001276 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1277}
1278
1279static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1280 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1281 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1282 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1283
1284 // Invisible 10x10 button
1285 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1286 ImGui::InvisibleButton(label, ImVec2(10, 10));
1287
1288 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1289 ImGuiIO& io = ImGui::GetIO();
1290 // Normalized mouse position, relative to our gamut box
1291 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1292 // Clamp to edge of box
1293 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1294 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1295 *dragging = true;
1296 }
1297
1298 // Return screen coordinates for the caller. We could just return center here, but we'd have
1299 // one frame of lag during drag.
1300 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1301}
1302
1303static bool ImGui_DragQuad(SkPoint* pts) {
1304 ImDrawList* drawList = ImGui::GetWindowDrawList();
1305
1306 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1307 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1308 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1309 ImVec2 pos = ImGui::GetCursorScreenPos();
1310
1311 // Background rectangle
1312 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1313
1314 // Corner markers
1315 bool dragging = false;
1316 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1317 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1318 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1319 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1320
1321 // Draw markers and quad
1322 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1323 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1324 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1325 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1326 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1327 drawList->AddLine(tr, br, 0xFFFFFFFF);
1328 drawList->AddLine(br, bl, 0xFFFFFFFF);
1329 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1330
1331 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1332 ImGui::Spacing();
1333
1334 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001335}
1336
Brian Osmand67e5182017-12-08 16:46:09 -05001337void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001338 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1339 if (fShowImGuiTestWindow) {
1340 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1341 }
1342
1343 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001344 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1345 // always visible, we can end up in a layout feedback loop.
1346 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001347 DisplayParams params = fWindow->getRequestedDisplayParams();
1348 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001349 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1350 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001351 if (ImGui::CollapsingHeader("Backend")) {
1352 int newBackend = static_cast<int>(fBackendType);
1353 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1354 ImGui::SameLine();
1355 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001356#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1357 ImGui::SameLine();
1358 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1359#endif
Brian Osman621491e2017-02-28 15:45:01 -05001360#if defined(SK_VULKAN)
1361 ImGui::SameLine();
1362 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1363#endif
1364 if (newBackend != fBackendType) {
1365 fDeferredActions.push_back([=]() {
1366 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1367 });
1368 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001369
Brian Salomon99a33902017-03-07 15:16:34 -05001370 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001371 bool* wire = &params.fGrContextOptions.fWireframeMode;
1372 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1373 paramsChanged = true;
1374 }
Brian Salomon99a33902017-03-07 15:16:34 -05001375
Brian Osman28b12522017-03-08 17:10:24 -05001376 if (ctx) {
1377 int sampleCount = fWindow->sampleCount();
1378 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001379 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001380 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1381 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1382 ImGui::RadioButton("16", &sampleCount, 16);
1383
1384 if (sampleCount != params.fMSAASampleCount) {
1385 params.fMSAASampleCount = sampleCount;
1386 paramsChanged = true;
1387 }
1388 }
1389
Ben Wagner37c54032018-04-13 14:30:23 -04001390 int pixelGeometryIdx = 0;
1391 if (fPixelGeometryOverrides) {
1392 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1393 }
1394 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1395 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1396 {
1397 uint32_t flags = params.fSurfaceProps.flags();
1398 if (pixelGeometryIdx == 0) {
1399 fPixelGeometryOverrides = false;
1400 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1401 } else {
1402 fPixelGeometryOverrides = true;
1403 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1404 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1405 }
1406 paramsChanged = true;
1407 }
1408
1409 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1410 if (ImGui::Checkbox("DFT", &useDFT)) {
1411 uint32_t flags = params.fSurfaceProps.flags();
1412 if (useDFT) {
1413 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1414 } else {
1415 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1416 }
1417 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1418 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1419 paramsChanged = true;
1420 }
1421
Brian Osman8a9de3d2017-03-01 14:59:05 -05001422 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001423 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001424 auto prButton = [&](GpuPathRenderers x) {
1425 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001426 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1427 params.fGrContextOptions.fGpuPathRenderers = x;
1428 paramsChanged = true;
1429 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001430 }
1431 };
1432
1433 if (!ctx) {
1434 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001435 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001436 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001437 prButton(GpuPathRenderers::kAll);
1438 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1439 prButton(GpuPathRenderers::kStencilAndCover);
1440 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001441 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001442 prButton(GpuPathRenderers::kNone);
1443 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001444 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001445 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001446 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1447 prButton(GpuPathRenderers::kCoverageCounting);
1448 }
Jim Van Verth83010462017-03-16 08:45:39 -04001449 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001450 prButton(GpuPathRenderers::kTessellating);
1451 prButton(GpuPathRenderers::kNone);
1452 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001453 ImGui::TreePop();
1454 }
Brian Osman621491e2017-02-28 15:45:01 -05001455 }
1456
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001457 if (ImGui::CollapsingHeader("Transform")) {
1458 float zoom = fZoomLevel;
1459 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1460 fZoomLevel = zoom;
1461 this->preTouchMatrixChanged();
1462 paramsChanged = true;
1463 }
1464 float deg = fRotation;
1465 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.0f deg")) {
1466 fRotation = deg;
1467 this->preTouchMatrixChanged();
1468 paramsChanged = true;
1469 }
Brian Osman805a7272018-05-02 15:40:20 -04001470 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1471 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1472 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001473 this->preTouchMatrixChanged();
Brian Osman805a7272018-05-02 15:40:20 -04001474 this->updateTitle();
Brian Osman9bb47cf2018-04-26 15:55:00 -04001475 }
1476 if (ImGui_DragQuad(fPerspectivePoints)) {
1477 this->preTouchMatrixChanged();
1478 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001479 }
1480
Ben Wagnera580fb32018-04-17 11:16:32 -04001481 if (ImGui::CollapsingHeader("Paint")) {
1482 int hintingIdx = 0;
1483 if (fPaintOverrides.fHinting) {
1484 hintingIdx = fPaint.getHinting() + 1;
1485 }
1486 if (ImGui::Combo("Hinting", &hintingIdx,
1487 "Default\0None\0Slight\0Normal\0Full\0\0"))
1488 {
1489 if (hintingIdx == 0) {
1490 fPaintOverrides.fHinting = false;
1491 fPaint.setHinting(SkPaint::kNo_Hinting);
1492 } else {
1493 fPaintOverrides.fHinting = true;
1494 SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1);
1495 fPaint.setHinting(hinting);
1496 }
1497 paramsChanged = true;
1498 }
1499
1500 int aliasIdx = 0;
1501 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1502 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1503 }
1504 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1505 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1506 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1507 {
1508 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1509 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1510 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1511 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1512 if (aliasIdx == 0) {
1513 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1514 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1515 } else {
1516 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1517 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1518 fPaint.setAntiAlias(aliasIdx > 1);
1519 switch (fPaintOverrides.fAntiAlias) {
1520 case SkPaintFields::AntiAliasState::Alias:
1521 break;
1522 case SkPaintFields::AntiAliasState::Normal:
1523 break;
1524 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1525 gSkUseAnalyticAA = true;
1526 gSkForceAnalyticAA = false;
1527 gSkUseDeltaAA = gSkForceDeltaAA = false;
1528 break;
1529 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1530 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1531 gSkUseDeltaAA = gSkForceDeltaAA = false;
1532 break;
1533 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1534 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1535 gSkUseDeltaAA = true;
1536 gSkForceDeltaAA = false;
1537 break;
1538 case SkPaintFields::AntiAliasState::DeltaAAForced:
1539 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1540 gSkUseDeltaAA = gSkForceDeltaAA = true;
1541 break;
1542 }
1543 }
1544 paramsChanged = true;
1545 }
1546
1547 int subpixelAAIdx = 0;
1548 if (fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag) {
1549 subpixelAAIdx = fPaint.isLCDRenderText() ? 2 : 1;
1550 }
1551 if (ImGui::Combo("Subpixel Anti-Alias", &subpixelAAIdx,
1552 "Default\0lcd\0LCD\0\0"))
1553 {
1554 if (subpixelAAIdx == 0) {
1555 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
1556 } else {
1557 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
1558 fPaint.setLCDRenderText(subpixelAAIdx == 2);
1559 }
1560 paramsChanged = true;
1561 }
1562
1563 int subpixelPositionIdx = 0;
1564 if (fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag) {
1565 subpixelPositionIdx = fPaint.isSubpixelText() ? 2 : 1;
1566 }
1567 if (ImGui::Combo("Subpixel Position Glyphs", &subpixelPositionIdx,
1568 "Default\0Pixel Glyphs\0Subpixel Glyphs\0\0"))
1569 {
1570 if (subpixelPositionIdx == 0) {
1571 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
1572 } else {
1573 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
1574 fPaint.setSubpixelText(subpixelPositionIdx == 2);
1575 }
1576 paramsChanged = true;
1577 }
1578 }
1579
Ben Wagner7a3c6742018-04-23 10:01:07 -04001580 if (fShowSlidePicker) {
1581 ImGui::SetNextTreeNodeOpen(true);
1582 }
Brian Osman79086b92017-02-10 13:36:16 -05001583 if (ImGui::CollapsingHeader("Slide")) {
1584 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001585 static ImVector<const char*> filteredSlideNames;
1586 static ImVector<int> filteredSlideIndices;
1587
Brian Osmanfce09c52017-11-14 15:32:20 -05001588 if (fShowSlidePicker) {
1589 ImGui::SetKeyboardFocusHere();
1590 fShowSlidePicker = false;
1591 }
1592
Brian Osman79086b92017-02-10 13:36:16 -05001593 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001594 filteredSlideNames.clear();
1595 filteredSlideIndices.clear();
1596 int filteredIndex = 0;
1597 for (int i = 0; i < fSlides.count(); ++i) {
1598 const char* slideName = fSlides[i]->getName().c_str();
1599 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1600 if (i == fCurrentSlide) {
1601 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001602 }
Brian Osmanf479e422017-11-08 13:11:36 -05001603 filteredSlideNames.push_back(slideName);
1604 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001605 }
Brian Osman79086b92017-02-10 13:36:16 -05001606 }
Brian Osmanf479e422017-11-08 13:11:36 -05001607
Brian Osmanf479e422017-11-08 13:11:36 -05001608 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1609 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001610 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001611 }
1612 }
Brian Osmana109e392017-02-24 09:49:14 -05001613
1614 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001615 ColorMode newMode = fColorMode;
1616 auto cmButton = [&](ColorMode mode, const char* label) {
1617 if (ImGui::RadioButton(label, mode == fColorMode)) {
1618 newMode = mode;
1619 }
1620 };
1621
1622 cmButton(ColorMode::kLegacy, "Legacy 8888");
1623 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1624 "Color Managed 8888 (Nonlinear blending)");
1625 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1626 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1627
1628 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001629 // It isn't safe to switch color mode now (in the middle of painting). We might
1630 // tear down the back-end, etc... Defer this change until the next onIdle.
1631 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001632 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001633 });
Brian Osmana109e392017-02-24 09:49:14 -05001634 }
1635
1636 // Pick from common gamuts:
1637 int primariesIdx = 4; // Default: Custom
1638 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1639 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1640 primariesIdx = i;
1641 break;
1642 }
1643 }
1644
Brian Osmanfdab5762017-11-09 10:27:55 -05001645 // When we're in xform canvas mode, we can alter the transfer function, too
1646 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1647 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1648 }
1649
Brian Osmana109e392017-02-24 09:49:14 -05001650 if (ImGui::Combo("Primaries", &primariesIdx,
1651 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1652 if (primariesIdx >= 0 && primariesIdx <= 3) {
1653 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1654 }
1655 }
1656
1657 // Allow direct editing of gamut
1658 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1659 }
Brian Osman79086b92017-02-10 13:36:16 -05001660 }
Brian Salomon99a33902017-03-07 15:16:34 -05001661 if (paramsChanged) {
1662 fDeferredActions.push_back([=]() {
1663 fWindow->setRequestedDisplayParams(params);
1664 fWindow->inval();
1665 this->updateTitle();
1666 });
1667 }
Brian Osman79086b92017-02-10 13:36:16 -05001668 ImGui::End();
1669 }
1670
Brian Osmanf6877092017-02-13 09:39:57 -05001671 if (fShowZoomWindow && fLastImage) {
1672 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001673 static int zoomFactor = 8;
1674 if (ImGui::Button("<<")) {
1675 zoomFactor = SkTMax(zoomFactor / 2, 4);
1676 }
1677 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1678 if (ImGui::Button(">>")) {
1679 zoomFactor = SkTMin(zoomFactor * 2, 32);
1680 }
Brian Osmanf6877092017-02-13 09:39:57 -05001681
Brian Osmanf6877092017-02-13 09:39:57 -05001682 ImVec2 mousePos = ImGui::GetMousePos();
1683 ImVec2 avail = ImGui::GetContentRegionAvail();
1684
Brian Osmanead517d2017-11-13 15:36:36 -05001685 uint32_t pixel = 0;
1686 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1687 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1688 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001689 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1690 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1691 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001692 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1693 }
1694
Brian Osmand67e5182017-12-08 16:46:09 -05001695 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001696 // Translate so the region of the image that's under the mouse cursor is centered
1697 // in the zoom canvas:
1698 c->scale(zoomFactor, zoomFactor);
1699 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1700 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1701 c->drawImage(this->fLastImage, 0, 0);
1702
1703 SkPaint outline;
1704 outline.setStyle(SkPaint::kStroke_Style);
1705 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1706 });
Brian Osmanf6877092017-02-13 09:39:57 -05001707 }
1708
1709 ImGui::End();
1710 }
Brian Osman79086b92017-02-10 13:36:16 -05001711}
1712
liyuqian2edb0f42016-07-06 14:11:32 -07001713void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001714 for (int i = 0; i < fDeferredActions.count(); ++i) {
1715 fDeferredActions[i]();
1716 }
1717 fDeferredActions.reset();
1718
Brian Osman56a24812017-12-19 11:15:16 -05001719 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001720 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001721 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001722 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001723
Brian Osman79086b92017-02-10 13:36:16 -05001724 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001725 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001726 fWindow->inval();
1727 }
jvanverth9f372462016-04-06 06:08:59 -07001728}
liyuqiane5a6cd92016-05-27 08:52:52 -07001729
1730void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001731 if (!fWindow) {
1732 return;
1733 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001734 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001735 return; // Surface hasn't been created yet.
1736 }
1737
liyuqianb73c24b2016-06-03 08:47:23 -07001738 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001739 Json::Value slideState(Json::objectValue);
1740 slideState[kName] = kSlideStateName;
1741 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001742 if (fAllSlideNames.size() == 0) {
1743 for(auto slide : fSlides) {
1744 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1745 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001746 }
liyuqian1f508fd2016-06-07 06:57:40 -07001747 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001748
liyuqianb73c24b2016-06-03 08:47:23 -07001749 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001750 Json::Value backendState(Json::objectValue);
1751 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001752 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001753 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001754 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001755 backendState[kOptions].append(Json::Value(str));
1756 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001757
csmartdalton578f0642017-02-24 16:04:47 -07001758 // MSAA state
1759 Json::Value msaaState(Json::objectValue);
1760 msaaState[kName] = kMSAAStateName;
1761 msaaState[kValue] = fWindow->sampleCount();
1762 msaaState[kOptions] = Json::Value(Json::arrayValue);
1763 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1764 msaaState[kOptions].append(Json::Value(0));
1765 } else {
1766 for (int msaa : {0, 4, 8, 16}) {
1767 msaaState[kOptions].append(Json::Value(msaa));
1768 }
1769 }
1770
csmartdalton61cd31a2017-02-27 17:00:53 -07001771 // Path renderer state
1772 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1773 Json::Value prState(Json::objectValue);
1774 prState[kName] = kPathRendererStateName;
1775 prState[kValue] = gPathRendererNames[pr];
1776 prState[kOptions] = Json::Value(Json::arrayValue);
1777 const GrContext* ctx = fWindow->getGrContext();
1778 if (!ctx) {
1779 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001780 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001781 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001782 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1783 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1784 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1785 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001786 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001787 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1788 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001789 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001790 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001791 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1792 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1793 }
Jim Van Verth83010462017-03-16 08:45:39 -04001794 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001795 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001796 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1797 }
1798
liyuqianb73c24b2016-06-03 08:47:23 -07001799 // Softkey state
1800 Json::Value softkeyState(Json::objectValue);
1801 softkeyState[kName] = kSoftkeyStateName;
1802 softkeyState[kValue] = kSoftkeyHint;
1803 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1804 softkeyState[kOptions].append(kSoftkeyHint);
1805 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1806 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1807 }
1808
liyuqian1f508fd2016-06-07 06:57:40 -07001809 // FPS state
1810 Json::Value fpsState(Json::objectValue);
1811 fpsState[kName] = kFpsStateName;
Brian Osman56a24812017-12-19 11:15:16 -05001812 double animTime = fStatsLayer.getLastTime(fAnimateTimer);
1813 double paintTime = fStatsLayer.getLastTime(fPaintTimer);
1814 double flushTime = fStatsLayer.getLastTime(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001815 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
Brian Osman56a24812017-12-19 11:15:16 -05001816 animTime + paintTime + flushTime,
1817 animTime, paintTime, flushTime).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001818 fpsState[kOptions] = Json::Value(Json::arrayValue);
1819
liyuqiane5a6cd92016-05-27 08:52:52 -07001820 Json::Value state(Json::arrayValue);
1821 state.append(slideState);
1822 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001823 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001824 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001825 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001826 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001827
Brian Osmaneff04b52017-11-21 13:18:02 -05001828 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001829}
1830
1831void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001832 // For those who will add more features to handle the state change in this function:
1833 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1834 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1835 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001836 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001837 for (int i = 0; i < fSlides.count(); ++i) {
1838 if (fSlides[i]->getName().equals(stateValue)) {
1839 this->setCurrentSlide(i);
1840 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001841 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001842 }
Florin Malitaab99c342018-01-16 16:23:03 -05001843
1844 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001845 } else if (stateName.equals(kBackendStateName)) {
1846 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1847 if (stateValue.equals(kBackendTypeStrings[i])) {
1848 if (fBackendType != i) {
1849 fBackendType = (sk_app::Window::BackendType)i;
1850 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001851 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001852 }
1853 break;
1854 }
1855 }
csmartdalton578f0642017-02-24 16:04:47 -07001856 } else if (stateName.equals(kMSAAStateName)) {
1857 DisplayParams params = fWindow->getRequestedDisplayParams();
1858 int sampleCount = atoi(stateValue.c_str());
1859 if (sampleCount != params.fMSAASampleCount) {
1860 params.fMSAASampleCount = sampleCount;
1861 fWindow->setRequestedDisplayParams(params);
1862 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001863 this->updateTitle();
1864 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001865 }
1866 } else if (stateName.equals(kPathRendererStateName)) {
1867 DisplayParams params = fWindow->getRequestedDisplayParams();
1868 for (const auto& pair : gPathRendererNames) {
1869 if (pair.second == stateValue.c_str()) {
1870 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1871 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1872 fWindow->setRequestedDisplayParams(params);
1873 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001874 this->updateTitle();
1875 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001876 }
1877 break;
1878 }
csmartdalton578f0642017-02-24 16:04:47 -07001879 }
liyuqianb73c24b2016-06-03 08:47:23 -07001880 } else if (stateName.equals(kSoftkeyStateName)) {
1881 if (!stateValue.equals(kSoftkeyHint)) {
1882 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001883 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001884 }
liyuqian2edb0f42016-07-06 14:11:32 -07001885 } else if (stateName.equals(kRefreshStateName)) {
1886 // This state is actually NOT in the UI state.
1887 // We use this to allow Android to quickly set bool fRefresh.
1888 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001889 } else {
1890 SkDebugf("Unknown stateName: %s", stateName.c_str());
1891 }
1892}
Brian Osman79086b92017-02-10 13:36:16 -05001893
1894bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001895 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001896}
1897
1898bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001899 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001900 fWindow->inval();
1901 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001902 } else {
1903 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001904 }
Brian Osman79086b92017-02-10 13:36:16 -05001905}