blob: 5d8b431e93ef37128556580844b2b356bc0b7a55 [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 Osman9bb47cf2018-04-26 15:55:00 -0400182 , fPerspective(false)
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 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500492
jvanverth2bb3b6d2016-04-08 07:24:09 -0700493 // set up slides
494 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500495 if (FLAGS_list) {
496 this->listNames();
497 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700498
Brian Osman9bb47cf2018-04-26 15:55:00 -0400499 fPerspectivePoints[0].set(0, 0);
500 fPerspectivePoints[1].set(1, 0);
501 fPerspectivePoints[2].set(0, 1);
502 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700503 fAnimTimer.run();
504
Hal Canaryc465d132017-12-08 10:21:31 -0500505 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500506 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400507 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500508 }
509 fImGuiGamutPaint.setColor(SK_ColorWHITE);
510 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
511
Brian Osman70d2f432017-11-08 09:54:10 -0500512 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500513 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700514}
515
jvanverth34524262016-05-04 13:49:13 -0700516void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700517 fAllSlideNames = Json::Value(Json::arrayValue);
518
Chris Dalton2d18f412018-02-20 13:23:32 -0700519 // Bisect slide.
520 if (!FLAGS_bisect.isEmpty()) {
521 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
522 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
523 if (FLAGS_bisect.count() >= 2) {
524 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
525 bisect->onChar(*ch);
526 }
527 }
528 fSlides.push_back(std::move(bisect));
529 }
530 }
531
532 // GMs
533 int firstGM = fSlides.count();
jvanverth2bb3b6d2016-04-08 07:24:09 -0700534 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
535 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400536 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700537
538 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
539 sk_sp<Slide> slide(new GMSlide(gm.release()));
Chris Dalton2d18f412018-02-20 13:23:32 -0700540 fSlides.push_back(std::move(slide));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700541 }
542
543 gms = gms->next();
544 }
Chris Dalton2d18f412018-02-20 13:23:32 -0700545 // reverse gms
546 int numGMs = fSlides.count() - firstGM;
547 for (int i = 0; i < numGMs/2; ++i) {
548 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
jvanverth2bb3b6d2016-04-08 07:24:09 -0700549 }
550
jvanverthc7027ab2016-06-16 09:52:35 -0700551 // samples
552 const SkViewRegister* reg = SkViewRegister::Head();
553 while (reg) {
554 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700555 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
556 fSlides.push_back(slide);
557 }
jvanverthc7027ab2016-06-16 09:52:35 -0700558 reg = reg->next();
559 }
560
Florin Malita0ffa3222018-04-05 14:34:45 -0400561 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
562 static const struct {
563 const char* fExtension;
564 const char* fDirName;
565 const SkCommandLineFlags::StringArray& fFlags;
566 const SlideFactory fFactory;
567 } gExternalSlidesInfo[] = {
568 { ".skp", "skp-dir", FLAGS_skps,
569 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
570 return sk_make_sp<SKPSlide>(name, path);}
571 },
572 { ".jpg", "jpg-dir", FLAGS_jpgs,
573 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
574 return sk_make_sp<ImageSlide>(name, path);}
575 },
576 { ".json", "skottie-dir", FLAGS_jsons,
577 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
578 return sk_make_sp<SkottieSlide>(name, path);}
579 },
580 { ".svg", "svg-dir", FLAGS_svgs,
581 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
582 return sk_make_sp<SvgSlide>(name, path);}
583 },
584 };
jvanverthc265a922016-04-08 12:51:45 -0700585
Florin Malita0ffa3222018-04-05 14:34:45 -0400586 SkTArray<sk_sp<Slide>, true> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700587
Florin Malita0ffa3222018-04-05 14:34:45 -0400588 const auto addSlide = [&](const SkString& name,
589 const SkString& path,
590 const SlideFactory& fact) {
591 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
592 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700593 }
liyuqian6f163d22016-06-13 12:26:45 -0700594
Florin Malita0ffa3222018-04-05 14:34:45 -0400595 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500596 dirSlides.push_back(slide);
597 fSlides.push_back(std::move(slide));
598 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400599 };
Florin Malita76a076b2018-02-15 18:40:48 -0500600
Florin Malita0ffa3222018-04-05 14:34:45 -0400601 for (const auto& info : gExternalSlidesInfo) {
602 for (const auto& flag : info.fFlags) {
603 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
604 // single file
605 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
606 } else {
607 // directory
608 SkOSFile::Iter it(flag.c_str(), info.fExtension);
609 SkString name;
610 while (it.next(&name)) {
611 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
612 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400613 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400614
615 if (!dirSlides.empty()) {
616 fSlides.push_back(
617 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
618 std::move(dirSlides)));
619 dirSlides.reset();
620 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400621 }
622 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700623}
624
625
jvanverth34524262016-05-04 13:49:13 -0700626Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700627 fWindow->detach();
628 delete fWindow;
629}
630
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500631struct SkPaintTitleUpdater {
632 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
633 void append(const char* s) {
634 if (fCount == 0) {
635 fTitle->append(" {");
636 } else {
637 fTitle->append(", ");
638 }
639 fTitle->append(s);
640 ++fCount;
641 }
642 void done() {
643 if (fCount > 0) {
644 fTitle->append("}");
645 }
646 }
647 SkString* fTitle;
648 int fCount;
649};
650
brianosman05de2162016-05-06 13:28:57 -0700651void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700652 if (!fWindow) {
653 return;
654 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500655 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700656 return; // Surface hasn't been created yet.
657 }
658
jvanverth34524262016-05-04 13:49:13 -0700659 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700660 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700661
Yuqian Li399b3c22017-08-03 11:08:15 -0400662 if (gSkUseDeltaAA) {
663 if (gSkForceDeltaAA) {
664 title.append(" <FDAA>");
665 } else {
666 title.append(" <DAA>");
667 }
668 } else if (gSkUseAnalyticAA) {
669 if (gSkForceAnalyticAA) {
670 title.append(" <FAAA>");
671 } else {
672 title.append(" <AAA>");
673 }
674 }
675
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500676 SkPaintTitleUpdater paintTitle(&title);
677 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
678 if (fPaint.isAntiAlias()) {
679 paintTitle.append("Antialias");
680 } else {
681 paintTitle.append("Alias");
682 }
683 }
684 if (fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag) {
685 if (fPaint.isLCDRenderText()) {
686 paintTitle.append("LCD");
687 } else {
688 paintTitle.append("lcd");
689 }
690 }
691 if (fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag) {
692 if (fPaint.isSubpixelText()) {
693 paintTitle.append("Subpixel Glyphs");
694 } else {
695 paintTitle.append("Pixel Glyphs");
696 }
697 }
698 if (fPaintOverrides.fHinting) {
699 switch (fPaint.getHinting()) {
700 case SkPaint::kNo_Hinting:
701 paintTitle.append("No Hinting");
702 break;
703 case SkPaint::kSlight_Hinting:
704 paintTitle.append("Slight Hinting");
705 break;
706 case SkPaint::kNormal_Hinting:
707 paintTitle.append("Normal Hinting");
708 break;
709 case SkPaint::kFull_Hinting:
710 paintTitle.append("Full Hinting");
711 break;
712 }
713 }
714 paintTitle.done();
715
Yuqian Lib2ba6642017-11-22 12:07:41 -0500716 if (fTileCnt > 0) {
717 title.appendf(" T%d", fTileCnt);
718 if (fThreadCnt > 0) {
719 title.appendf("/%d", fThreadCnt);
720 }
721 }
722
Brian Osman92004802017-03-06 11:47:26 -0500723 switch (fColorMode) {
724 case ColorMode::kLegacy:
725 title.append(" Legacy 8888");
726 break;
727 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
728 title.append(" ColorManaged 8888 (Nonlinear blending)");
729 break;
730 case ColorMode::kColorManagedSRGB8888:
731 title.append(" ColorManaged 8888");
732 break;
733 case ColorMode::kColorManagedLinearF16:
734 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 }
746 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500747
748 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
749 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
750 }
brianosman05de2162016-05-06 13:28:57 -0700751 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500752
Ben Wagner37c54032018-04-13 14:30:23 -0400753 const DisplayParams& params = fWindow->getRequestedDisplayParams();
754 if (fPixelGeometryOverrides) {
755 switch (params.fSurfaceProps.pixelGeometry()) {
756 case kUnknown_SkPixelGeometry:
757 title.append( " Flat");
758 break;
759 case kRGB_H_SkPixelGeometry:
760 title.append( " RGB");
761 break;
762 case kBGR_H_SkPixelGeometry:
763 title.append( " BGR");
764 break;
765 case kRGB_V_SkPixelGeometry:
766 title.append( " RGBV");
767 break;
768 case kBGR_V_SkPixelGeometry:
769 title.append( " BGRV");
770 break;
771 }
772 }
773
774 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
775 title.append(" DFT");
776 }
777
csmartdalton578f0642017-02-24 16:04:47 -0700778 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700779 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500780 int msaa = fWindow->sampleCount();
781 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700782 title.appendf(" MSAA: %i", msaa);
783 }
784 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700785
786 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400787 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700788 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
789 }
790
brianosman05de2162016-05-06 13:28:57 -0700791 fWindow->setTitle(title.c_str());
792}
793
Florin Malitaab99c342018-01-16 16:23:03 -0500794int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500795
796 if (!FLAGS_slide.isEmpty()) {
797 int count = fSlides.count();
798 for (int i = 0; i < count; i++) {
799 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500800 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500801 }
802 }
803
804 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
805 this->listNames();
806 }
807
Florin Malitaab99c342018-01-16 16:23:03 -0500808 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500809}
810
Florin Malitaab99c342018-01-16 16:23:03 -0500811void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500812 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500813 for (const auto& slide : fSlides) {
814 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500815 }
816}
817
Florin Malitaab99c342018-01-16 16:23:03 -0500818void Viewer::setCurrentSlide(int slide) {
819 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700820
Florin Malitaab99c342018-01-16 16:23:03 -0500821 if (slide == fCurrentSlide) {
822 return;
823 }
824
825 if (fCurrentSlide >= 0) {
826 fSlides[fCurrentSlide]->unload();
827 }
828
829 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
830 SkIntToScalar(fWindow->height()));
831 fCurrentSlide = slide;
832 this->setupCurrentSlide();
833}
834
835void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500836 if (fCurrentSlide >= 0) {
837 // prepare dimensions for image slides
838 fGesture.resetTouchState();
839 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700840
Jim Van Verth0848fb02018-01-22 13:39:30 -0500841 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
842 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
843 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400844
Jim Van Verth0848fb02018-01-22 13:39:30 -0500845 // Start with a matrix that scales the slide to the available screen space
846 if (fWindow->scaleContentToFit()) {
847 if (windowRect.width() > 0 && windowRect.height() > 0) {
848 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
849 }
liyuqiane46e4f02016-05-20 07:32:19 -0700850 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500851
852 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400853 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500854
855 this->updateTitle();
856 this->updateUIState();
857
858 fStatsLayer.resetMeasurements();
859
860 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700861 }
jvanverthc265a922016-04-08 12:51:45 -0700862}
863
864#define MAX_ZOOM_LEVEL 8
865#define MIN_ZOOM_LEVEL -8
866
jvanverth34524262016-05-04 13:49:13 -0700867void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700868 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400869 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400870 this->preTouchMatrixChanged();
871}
Yuqian Li755778c2018-03-28 16:23:31 -0400872
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400873void Viewer::preTouchMatrixChanged() {
874 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400875 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
876 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
877 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
878 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
879}
880
881SkMatrix Viewer::computePreTouchMatrix() {
882 SkMatrix m = fDefaultMatrix;
883 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
884 : SK_Scalar1 + fZoomLevel;
885 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400886
887 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
888 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400889
890 if (fPerspective) {
891 SkScalar w = fWindow->width(), h = fWindow->height();
892 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
893 SkPoint perspPts[4] = {
894 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
895 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
896 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
897 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
898 };
899 SkMatrix persp;
900 persp.setPolyToPoly(orthoPts, perspPts, 4);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400901 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400902 }
903
Yuqian Li755778c2018-03-28 16:23:31 -0400904 return m;
jvanverthc265a922016-04-08 12:51:45 -0700905}
906
liyuqiand3cdbca2016-05-17 12:44:20 -0700907SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400908 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700909 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400910 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700911 return m;
jvanverthc265a922016-04-08 12:51:45 -0700912}
913
Brian Osman621491e2017-02-28 15:45:01 -0500914void Viewer::setBackend(sk_app::Window::BackendType backendType) {
915 fBackendType = backendType;
916
917 fWindow->detach();
918
Brian Osman70d2f432017-11-08 09:54:10 -0500919#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400920 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
921 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500922 DisplayParams params = fWindow->getRequestedDisplayParams();
923 delete fWindow;
924 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500925
Brian Osman70d2f432017-11-08 09:54:10 -0500926 // re-register callbacks
927 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500928 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500929 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500930 fWindow->pushLayer(&fImGuiLayer);
931
Brian Osman70d2f432017-11-08 09:54:10 -0500932 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
933 // will still include our correct sample count. But the re-created fWindow will lose that
934 // information. On Windows, we need to re-create the window when changing sample count,
935 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
936 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
937 // as if we had never changed windows at all).
938 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500939#endif
940
Brian Osman70d2f432017-11-08 09:54:10 -0500941 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500942}
943
Brian Osman92004802017-03-06 11:47:26 -0500944void Viewer::setColorMode(ColorMode colorMode) {
945 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700946
Brian Osmanf750fbc2017-02-08 10:47:28 -0500947 // 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 -0400948 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700949 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500950 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400951 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
952 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500953 if (wasInLegacy != wantLegacy) {
954 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700955 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500956 }
957
958 this->updateTitle();
959 fWindow->inval();
960}
961
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500962class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
963public:
964 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
965 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
966 { }
967 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -0400968 if (*paint == nullptr) {
969 return true;
970 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500971 if (fPaintOverrides->fHinting) {
972 paint->writable()->setHinting(fPaint->getHinting());
973 }
974 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
975 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
976 }
977 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
978 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
979 }
980 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
981 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
982 }
983 return true;
984 }
985 SkPaint* fPaint;
986 Viewer::SkPaintFields* fPaintOverrides;
987};
988
Brian Osmanf750fbc2017-02-08 10:47:28 -0500989void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500990 SkAutoCanvasRestore autorestore(canvas, false);
991
Brian Osmanf750fbc2017-02-08 10:47:28 -0500992 // By default, we render directly into the window's surface/canvas
993 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500994 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700995
Brian Osmane0d4fba2017-03-15 10:24:55 -0400996 // If we're in any of the color managed modes, construct the color space we're going to use
997 sk_sp<SkColorSpace> cs = nullptr;
998 if (ColorMode::kLegacy != fColorMode) {
999 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
1000 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -04001001 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001002 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -05001003 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1004 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
1005 } else {
1006 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
1007 }
Brian Osmane0d4fba2017-03-15 10:24:55 -04001008 }
1009
Brian Osman3ac99cf2017-12-01 11:23:53 -05001010 if (fSaveToSKP) {
1011 SkPictureRecorder recorder;
1012 SkCanvas* recorderCanvas = recorder.beginRecording(
1013 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
1014 // In xform-canvas mode, record the transformed output
1015 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1016 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1017 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
1018 recorderCanvas = xformCanvas.get();
1019 }
1020 fSlides[fCurrentSlide]->draw(recorderCanvas);
1021 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1022 SkFILEWStream stream("sample_app.skp");
1023 picture->serialize(&stream);
1024 fSaveToSKP = false;
1025 }
1026
Brian Osmane0d4fba2017-03-15 10:24:55 -04001027 // 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 -05001028 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
1029 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -05001030 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -05001031 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -05001032 if (Window::kRaster_BackendType == fBackendType ||
1033 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -05001034 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -04001035 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
1036 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
1037
Brian Osman92004802017-03-06 11:47:26 -05001038 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
1039 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001040 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
1041 // with a special canvas (below) that has the color space attached
1042 sk_sp<SkColorSpace> offscreenColorSpace =
1043 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -05001044 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -04001045 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Ben Wagner37c54032018-04-13 14:30:23 -04001046 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1047 canvas->getProps(&props);
1048 offscreenSurface = Window::kRaster_BackendType == fBackendType
1049 ? SkSurface::MakeRaster(info, &props)
1050 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -05001051 SkPixmap offscreenPixmap;
1052 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
1053 SkBitmap offscreenBitmap;
1054 offscreenBitmap.installPixels(offscreenPixmap);
Herb Derbyefe39bc2018-05-01 17:06:20 -04001055 threadedCanvas =
1056 skstd::make_unique<SkCanvas>(
1057 sk_make_sp<SkThreadedBMPDevice>(
1058 offscreenBitmap, fTileCnt, fThreadCnt, fExecutor.get()));
Yuqian Lib2ba6642017-11-22 12:07:41 -05001059 slideCanvas = threadedCanvas.get();
1060 } else {
1061 slideCanvas = offscreenSurface->getCanvas();
1062 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001063 }
1064
Brian Osmane0d4fba2017-03-15 10:24:55 -04001065 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1066 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1067 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
1068 slideCanvas = xformCanvas.get();
1069 }
1070
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001071 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001072 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001073 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -05001074 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001075 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001076 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1077 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001078 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001079 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001080
1081 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001082 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001083 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001084 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001085
1086 // If we rendered offscreen, snap an image and push the results to the window's canvas
1087 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001088 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001089
1090 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -04001091 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001092 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001093 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001094 SkPaint paint;
1095 paint.setBlendMode(SkBlendMode::kSrc);
1096 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -07001097 }
liyuqian6f163d22016-06-13 12:26:45 -07001098}
1099
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001100void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001101 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001102 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001103}
Jim Van Verth6f449692017-02-14 15:16:46 -05001104
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001105void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001106 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001107
brianosman622c8d52016-05-10 06:50:49 -07001108 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001109
Brian Osmand67e5182017-12-08 16:46:09 -05001110 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -05001111
Brian Osman1df161a2017-02-09 12:10:20 -05001112 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -04001113 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001114}
1115
Florin Malitacefc1b92018-02-19 21:43:47 -05001116SkPoint Viewer::mapEvent(float x, float y) {
1117 const auto m = this->computeMatrix();
1118 SkMatrix inv;
1119
1120 SkAssertResult(m.invert(&inv));
1121
1122 return inv.mapXY(x, y);
1123}
1124
jvanverth814e38d2016-06-06 08:48:47 -07001125bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001126 if (GestureDevice::kMouse == fGestureDevice) {
1127 return false;
1128 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001129
1130 const auto slidePt = this->mapEvent(x, y);
1131 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1132 fWindow->inval();
1133 return true;
1134 }
1135
liyuqiand3cdbca2016-05-17 12:44:20 -07001136 void* castedOwner = reinterpret_cast<void*>(owner);
1137 switch (state) {
1138 case Window::kUp_InputState: {
1139 fGesture.touchEnd(castedOwner);
1140 break;
1141 }
1142 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001143 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001144 break;
1145 }
1146 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001147 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001148 break;
1149 }
1150 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001151 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001152 fWindow->inval();
1153 return true;
1154}
1155
Brian Osman80fc07e2017-12-08 16:45:43 -05001156bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001157 if (GestureDevice::kTouch == fGestureDevice) {
1158 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001159 }
Brian Osman16c81a12017-12-20 11:58:34 -05001160
Florin Malitacefc1b92018-02-19 21:43:47 -05001161 const auto slidePt = this->mapEvent(x, y);
1162 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1163 fWindow->inval();
1164 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001165 }
1166
1167 switch (state) {
1168 case Window::kUp_InputState: {
1169 fGesture.touchEnd(nullptr);
1170 break;
1171 }
1172 case Window::kDown_InputState: {
1173 fGesture.touchBegin(nullptr, x, y);
1174 break;
1175 }
1176 case Window::kMove_InputState: {
1177 fGesture.touchMoved(nullptr, x, y);
1178 break;
1179 }
1180 }
1181 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1182
1183 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1184 fWindow->inval();
1185 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001186 return true;
1187}
1188
Brian Osmana109e392017-02-24 09:49:14 -05001189static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1190 const ImVec2& pos, const ImVec2& size) {
1191 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1192 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1193
1194 // Invisible 10x10 button
1195 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1196 ImGui::InvisibleButton(label, ImVec2(10, 10));
1197
1198 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1199 ImGuiIO& io = ImGui::GetIO();
1200 // Normalized mouse position, relative to our gamut box
1201 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1202 // Clamp to edge of box, convert back to primary scale
1203 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1204 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1205 }
1206
1207 if (ImGui::IsItemHovered()) {
1208 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1209 }
1210
1211 // Return screen coordinates for the caller. We could just return center here, but we'd have
1212 // one frame of lag during drag.
1213 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1214}
1215
1216static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1217 ImDrawList* drawList = ImGui::GetWindowDrawList();
1218
1219 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1220 // width, and scale the height to maintain aspect ratio.
1221 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1222 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1223 ImVec2 pos = ImGui::GetCursorScreenPos();
1224
1225 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1226 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1227 // Magic numbers are pixel locations of the origin and upper-right corner.
1228 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1229 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001230
1231 // Primary markers
1232 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1233 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1234 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1235 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1236
1237 // Gamut triangle
1238 drawList->AddCircle(r, 5.0f, 0xFF000040);
1239 drawList->AddCircle(g, 5.0f, 0xFF004000);
1240 drawList->AddCircle(b, 5.0f, 0xFF400000);
1241 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1242 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1243
1244 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001245 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1246}
1247
1248static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1249 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1250 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1251 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1252
1253 // Invisible 10x10 button
1254 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1255 ImGui::InvisibleButton(label, ImVec2(10, 10));
1256
1257 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1258 ImGuiIO& io = ImGui::GetIO();
1259 // Normalized mouse position, relative to our gamut box
1260 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1261 // Clamp to edge of box
1262 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1263 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1264 *dragging = true;
1265 }
1266
1267 // Return screen coordinates for the caller. We could just return center here, but we'd have
1268 // one frame of lag during drag.
1269 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1270}
1271
1272static bool ImGui_DragQuad(SkPoint* pts) {
1273 ImDrawList* drawList = ImGui::GetWindowDrawList();
1274
1275 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1276 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1277 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1278 ImVec2 pos = ImGui::GetCursorScreenPos();
1279
1280 // Background rectangle
1281 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1282
1283 // Corner markers
1284 bool dragging = false;
1285 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1286 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1287 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1288 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1289
1290 // Draw markers and quad
1291 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1292 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1293 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1294 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1295 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1296 drawList->AddLine(tr, br, 0xFFFFFFFF);
1297 drawList->AddLine(br, bl, 0xFFFFFFFF);
1298 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1299
1300 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1301 ImGui::Spacing();
1302
1303 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001304}
1305
Brian Osmand67e5182017-12-08 16:46:09 -05001306void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001307 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1308 if (fShowImGuiTestWindow) {
1309 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1310 }
1311
1312 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001313 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1314 // always visible, we can end up in a layout feedback loop.
1315 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001316 DisplayParams params = fWindow->getRequestedDisplayParams();
1317 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001318 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1319 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001320 if (ImGui::CollapsingHeader("Backend")) {
1321 int newBackend = static_cast<int>(fBackendType);
1322 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1323 ImGui::SameLine();
1324 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001325#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1326 ImGui::SameLine();
1327 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1328#endif
Brian Osman621491e2017-02-28 15:45:01 -05001329#if defined(SK_VULKAN)
1330 ImGui::SameLine();
1331 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1332#endif
1333 if (newBackend != fBackendType) {
1334 fDeferredActions.push_back([=]() {
1335 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1336 });
1337 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001338
Brian Salomon99a33902017-03-07 15:16:34 -05001339 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001340 bool* wire = &params.fGrContextOptions.fWireframeMode;
1341 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1342 paramsChanged = true;
1343 }
Brian Salomon99a33902017-03-07 15:16:34 -05001344
Brian Osman28b12522017-03-08 17:10:24 -05001345 if (ctx) {
1346 int sampleCount = fWindow->sampleCount();
1347 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001348 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001349 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1350 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1351 ImGui::RadioButton("16", &sampleCount, 16);
1352
1353 if (sampleCount != params.fMSAASampleCount) {
1354 params.fMSAASampleCount = sampleCount;
1355 paramsChanged = true;
1356 }
1357 }
1358
Ben Wagner37c54032018-04-13 14:30:23 -04001359 int pixelGeometryIdx = 0;
1360 if (fPixelGeometryOverrides) {
1361 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1362 }
1363 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1364 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1365 {
1366 uint32_t flags = params.fSurfaceProps.flags();
1367 if (pixelGeometryIdx == 0) {
1368 fPixelGeometryOverrides = false;
1369 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1370 } else {
1371 fPixelGeometryOverrides = true;
1372 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1373 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1374 }
1375 paramsChanged = true;
1376 }
1377
1378 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1379 if (ImGui::Checkbox("DFT", &useDFT)) {
1380 uint32_t flags = params.fSurfaceProps.flags();
1381 if (useDFT) {
1382 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1383 } else {
1384 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1385 }
1386 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1387 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1388 paramsChanged = true;
1389 }
1390
Brian Osman8a9de3d2017-03-01 14:59:05 -05001391 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001392 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001393 auto prButton = [&](GpuPathRenderers x) {
1394 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001395 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1396 params.fGrContextOptions.fGpuPathRenderers = x;
1397 paramsChanged = true;
1398 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001399 }
1400 };
1401
1402 if (!ctx) {
1403 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001404 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001405 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001406 prButton(GpuPathRenderers::kAll);
1407 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1408 prButton(GpuPathRenderers::kStencilAndCover);
1409 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001410 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001411 prButton(GpuPathRenderers::kNone);
1412 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001413 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001414 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001415 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1416 prButton(GpuPathRenderers::kCoverageCounting);
1417 }
Jim Van Verth83010462017-03-16 08:45:39 -04001418 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001419 prButton(GpuPathRenderers::kTessellating);
1420 prButton(GpuPathRenderers::kNone);
1421 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001422 ImGui::TreePop();
1423 }
Brian Osman621491e2017-02-28 15:45:01 -05001424 }
1425
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001426 if (ImGui::CollapsingHeader("Transform")) {
1427 float zoom = fZoomLevel;
1428 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1429 fZoomLevel = zoom;
1430 this->preTouchMatrixChanged();
1431 paramsChanged = true;
1432 }
1433 float deg = fRotation;
1434 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.0f deg")) {
1435 fRotation = deg;
1436 this->preTouchMatrixChanged();
1437 paramsChanged = true;
1438 }
Brian Osman9bb47cf2018-04-26 15:55:00 -04001439 if (ImGui::Checkbox("Perspective", &fPerspective)) {
1440 this->preTouchMatrixChanged();
1441 }
1442 if (ImGui_DragQuad(fPerspectivePoints)) {
1443 this->preTouchMatrixChanged();
1444 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001445 }
1446
Ben Wagnera580fb32018-04-17 11:16:32 -04001447 if (ImGui::CollapsingHeader("Paint")) {
1448 int hintingIdx = 0;
1449 if (fPaintOverrides.fHinting) {
1450 hintingIdx = fPaint.getHinting() + 1;
1451 }
1452 if (ImGui::Combo("Hinting", &hintingIdx,
1453 "Default\0None\0Slight\0Normal\0Full\0\0"))
1454 {
1455 if (hintingIdx == 0) {
1456 fPaintOverrides.fHinting = false;
1457 fPaint.setHinting(SkPaint::kNo_Hinting);
1458 } else {
1459 fPaintOverrides.fHinting = true;
1460 SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1);
1461 fPaint.setHinting(hinting);
1462 }
1463 paramsChanged = true;
1464 }
1465
1466 int aliasIdx = 0;
1467 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
1468 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1469 }
1470 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1471 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1472 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1473 {
1474 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1475 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1476 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1477 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1478 if (aliasIdx == 0) {
1479 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
1480 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
1481 } else {
1482 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
1483 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1484 fPaint.setAntiAlias(aliasIdx > 1);
1485 switch (fPaintOverrides.fAntiAlias) {
1486 case SkPaintFields::AntiAliasState::Alias:
1487 break;
1488 case SkPaintFields::AntiAliasState::Normal:
1489 break;
1490 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1491 gSkUseAnalyticAA = true;
1492 gSkForceAnalyticAA = false;
1493 gSkUseDeltaAA = gSkForceDeltaAA = false;
1494 break;
1495 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1496 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1497 gSkUseDeltaAA = gSkForceDeltaAA = false;
1498 break;
1499 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1500 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1501 gSkUseDeltaAA = true;
1502 gSkForceDeltaAA = false;
1503 break;
1504 case SkPaintFields::AntiAliasState::DeltaAAForced:
1505 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1506 gSkUseDeltaAA = gSkForceDeltaAA = true;
1507 break;
1508 }
1509 }
1510 paramsChanged = true;
1511 }
1512
1513 int subpixelAAIdx = 0;
1514 if (fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag) {
1515 subpixelAAIdx = fPaint.isLCDRenderText() ? 2 : 1;
1516 }
1517 if (ImGui::Combo("Subpixel Anti-Alias", &subpixelAAIdx,
1518 "Default\0lcd\0LCD\0\0"))
1519 {
1520 if (subpixelAAIdx == 0) {
1521 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
1522 } else {
1523 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
1524 fPaint.setLCDRenderText(subpixelAAIdx == 2);
1525 }
1526 paramsChanged = true;
1527 }
1528
1529 int subpixelPositionIdx = 0;
1530 if (fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag) {
1531 subpixelPositionIdx = fPaint.isSubpixelText() ? 2 : 1;
1532 }
1533 if (ImGui::Combo("Subpixel Position Glyphs", &subpixelPositionIdx,
1534 "Default\0Pixel Glyphs\0Subpixel Glyphs\0\0"))
1535 {
1536 if (subpixelPositionIdx == 0) {
1537 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
1538 } else {
1539 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
1540 fPaint.setSubpixelText(subpixelPositionIdx == 2);
1541 }
1542 paramsChanged = true;
1543 }
1544 }
1545
Ben Wagner7a3c6742018-04-23 10:01:07 -04001546 if (fShowSlidePicker) {
1547 ImGui::SetNextTreeNodeOpen(true);
1548 }
Brian Osman79086b92017-02-10 13:36:16 -05001549 if (ImGui::CollapsingHeader("Slide")) {
1550 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001551 static ImVector<const char*> filteredSlideNames;
1552 static ImVector<int> filteredSlideIndices;
1553
Brian Osmanfce09c52017-11-14 15:32:20 -05001554 if (fShowSlidePicker) {
1555 ImGui::SetKeyboardFocusHere();
1556 fShowSlidePicker = false;
1557 }
1558
Brian Osman79086b92017-02-10 13:36:16 -05001559 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001560 filteredSlideNames.clear();
1561 filteredSlideIndices.clear();
1562 int filteredIndex = 0;
1563 for (int i = 0; i < fSlides.count(); ++i) {
1564 const char* slideName = fSlides[i]->getName().c_str();
1565 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1566 if (i == fCurrentSlide) {
1567 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001568 }
Brian Osmanf479e422017-11-08 13:11:36 -05001569 filteredSlideNames.push_back(slideName);
1570 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001571 }
Brian Osman79086b92017-02-10 13:36:16 -05001572 }
Brian Osmanf479e422017-11-08 13:11:36 -05001573
Brian Osmanf479e422017-11-08 13:11:36 -05001574 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1575 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001576 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001577 }
1578 }
Brian Osmana109e392017-02-24 09:49:14 -05001579
1580 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001581 ColorMode newMode = fColorMode;
1582 auto cmButton = [&](ColorMode mode, const char* label) {
1583 if (ImGui::RadioButton(label, mode == fColorMode)) {
1584 newMode = mode;
1585 }
1586 };
1587
1588 cmButton(ColorMode::kLegacy, "Legacy 8888");
1589 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1590 "Color Managed 8888 (Nonlinear blending)");
1591 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1592 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1593
1594 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001595 // It isn't safe to switch color mode now (in the middle of painting). We might
1596 // tear down the back-end, etc... Defer this change until the next onIdle.
1597 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001598 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001599 });
Brian Osmana109e392017-02-24 09:49:14 -05001600 }
1601
1602 // Pick from common gamuts:
1603 int primariesIdx = 4; // Default: Custom
1604 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1605 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1606 primariesIdx = i;
1607 break;
1608 }
1609 }
1610
Brian Osmanfdab5762017-11-09 10:27:55 -05001611 // When we're in xform canvas mode, we can alter the transfer function, too
1612 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1613 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1614 }
1615
Brian Osmana109e392017-02-24 09:49:14 -05001616 if (ImGui::Combo("Primaries", &primariesIdx,
1617 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1618 if (primariesIdx >= 0 && primariesIdx <= 3) {
1619 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1620 }
1621 }
1622
1623 // Allow direct editing of gamut
1624 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1625 }
Brian Osman79086b92017-02-10 13:36:16 -05001626 }
Brian Salomon99a33902017-03-07 15:16:34 -05001627 if (paramsChanged) {
1628 fDeferredActions.push_back([=]() {
1629 fWindow->setRequestedDisplayParams(params);
1630 fWindow->inval();
1631 this->updateTitle();
1632 });
1633 }
Brian Osman79086b92017-02-10 13:36:16 -05001634 ImGui::End();
1635 }
1636
Brian Osmanf6877092017-02-13 09:39:57 -05001637 if (fShowZoomWindow && fLastImage) {
1638 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001639 static int zoomFactor = 8;
1640 if (ImGui::Button("<<")) {
1641 zoomFactor = SkTMax(zoomFactor / 2, 4);
1642 }
1643 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1644 if (ImGui::Button(">>")) {
1645 zoomFactor = SkTMin(zoomFactor * 2, 32);
1646 }
Brian Osmanf6877092017-02-13 09:39:57 -05001647
Brian Osmanf6877092017-02-13 09:39:57 -05001648 ImVec2 mousePos = ImGui::GetMousePos();
1649 ImVec2 avail = ImGui::GetContentRegionAvail();
1650
Brian Osmanead517d2017-11-13 15:36:36 -05001651 uint32_t pixel = 0;
1652 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1653 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1654 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001655 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1656 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1657 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001658 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1659 }
1660
Brian Osmand67e5182017-12-08 16:46:09 -05001661 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001662 // Translate so the region of the image that's under the mouse cursor is centered
1663 // in the zoom canvas:
1664 c->scale(zoomFactor, zoomFactor);
1665 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1666 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1667 c->drawImage(this->fLastImage, 0, 0);
1668
1669 SkPaint outline;
1670 outline.setStyle(SkPaint::kStroke_Style);
1671 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1672 });
Brian Osmanf6877092017-02-13 09:39:57 -05001673 }
1674
1675 ImGui::End();
1676 }
Brian Osman79086b92017-02-10 13:36:16 -05001677}
1678
liyuqian2edb0f42016-07-06 14:11:32 -07001679void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001680 for (int i = 0; i < fDeferredActions.count(); ++i) {
1681 fDeferredActions[i]();
1682 }
1683 fDeferredActions.reset();
1684
Brian Osman56a24812017-12-19 11:15:16 -05001685 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001686 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001687 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001688 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001689
Brian Osman79086b92017-02-10 13:36:16 -05001690 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001691 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001692 fWindow->inval();
1693 }
jvanverth9f372462016-04-06 06:08:59 -07001694}
liyuqiane5a6cd92016-05-27 08:52:52 -07001695
1696void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001697 if (!fWindow) {
1698 return;
1699 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001700 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001701 return; // Surface hasn't been created yet.
1702 }
1703
liyuqianb73c24b2016-06-03 08:47:23 -07001704 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001705 Json::Value slideState(Json::objectValue);
1706 slideState[kName] = kSlideStateName;
1707 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001708 if (fAllSlideNames.size() == 0) {
1709 for(auto slide : fSlides) {
1710 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1711 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001712 }
liyuqian1f508fd2016-06-07 06:57:40 -07001713 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001714
liyuqianb73c24b2016-06-03 08:47:23 -07001715 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001716 Json::Value backendState(Json::objectValue);
1717 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001718 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001719 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001720 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001721 backendState[kOptions].append(Json::Value(str));
1722 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001723
csmartdalton578f0642017-02-24 16:04:47 -07001724 // MSAA state
1725 Json::Value msaaState(Json::objectValue);
1726 msaaState[kName] = kMSAAStateName;
1727 msaaState[kValue] = fWindow->sampleCount();
1728 msaaState[kOptions] = Json::Value(Json::arrayValue);
1729 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1730 msaaState[kOptions].append(Json::Value(0));
1731 } else {
1732 for (int msaa : {0, 4, 8, 16}) {
1733 msaaState[kOptions].append(Json::Value(msaa));
1734 }
1735 }
1736
csmartdalton61cd31a2017-02-27 17:00:53 -07001737 // Path renderer state
1738 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1739 Json::Value prState(Json::objectValue);
1740 prState[kName] = kPathRendererStateName;
1741 prState[kValue] = gPathRendererNames[pr];
1742 prState[kOptions] = Json::Value(Json::arrayValue);
1743 const GrContext* ctx = fWindow->getGrContext();
1744 if (!ctx) {
1745 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001746 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001747 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001748 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1749 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1750 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1751 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001752 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001753 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1754 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001755 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001756 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001757 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1758 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1759 }
Jim Van Verth83010462017-03-16 08:45:39 -04001760 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001761 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001762 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1763 }
1764
liyuqianb73c24b2016-06-03 08:47:23 -07001765 // Softkey state
1766 Json::Value softkeyState(Json::objectValue);
1767 softkeyState[kName] = kSoftkeyStateName;
1768 softkeyState[kValue] = kSoftkeyHint;
1769 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1770 softkeyState[kOptions].append(kSoftkeyHint);
1771 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1772 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1773 }
1774
liyuqian1f508fd2016-06-07 06:57:40 -07001775 // FPS state
1776 Json::Value fpsState(Json::objectValue);
1777 fpsState[kName] = kFpsStateName;
Brian Osman56a24812017-12-19 11:15:16 -05001778 double animTime = fStatsLayer.getLastTime(fAnimateTimer);
1779 double paintTime = fStatsLayer.getLastTime(fPaintTimer);
1780 double flushTime = fStatsLayer.getLastTime(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001781 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
Brian Osman56a24812017-12-19 11:15:16 -05001782 animTime + paintTime + flushTime,
1783 animTime, paintTime, flushTime).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001784 fpsState[kOptions] = Json::Value(Json::arrayValue);
1785
liyuqiane5a6cd92016-05-27 08:52:52 -07001786 Json::Value state(Json::arrayValue);
1787 state.append(slideState);
1788 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001789 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001790 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001791 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001792 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001793
Brian Osmaneff04b52017-11-21 13:18:02 -05001794 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001795}
1796
1797void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001798 // For those who will add more features to handle the state change in this function:
1799 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1800 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1801 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001802 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001803 for (int i = 0; i < fSlides.count(); ++i) {
1804 if (fSlides[i]->getName().equals(stateValue)) {
1805 this->setCurrentSlide(i);
1806 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001807 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001808 }
Florin Malitaab99c342018-01-16 16:23:03 -05001809
1810 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001811 } else if (stateName.equals(kBackendStateName)) {
1812 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1813 if (stateValue.equals(kBackendTypeStrings[i])) {
1814 if (fBackendType != i) {
1815 fBackendType = (sk_app::Window::BackendType)i;
1816 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001817 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001818 }
1819 break;
1820 }
1821 }
csmartdalton578f0642017-02-24 16:04:47 -07001822 } else if (stateName.equals(kMSAAStateName)) {
1823 DisplayParams params = fWindow->getRequestedDisplayParams();
1824 int sampleCount = atoi(stateValue.c_str());
1825 if (sampleCount != params.fMSAASampleCount) {
1826 params.fMSAASampleCount = sampleCount;
1827 fWindow->setRequestedDisplayParams(params);
1828 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001829 this->updateTitle();
1830 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001831 }
1832 } else if (stateName.equals(kPathRendererStateName)) {
1833 DisplayParams params = fWindow->getRequestedDisplayParams();
1834 for (const auto& pair : gPathRendererNames) {
1835 if (pair.second == stateValue.c_str()) {
1836 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1837 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1838 fWindow->setRequestedDisplayParams(params);
1839 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001840 this->updateTitle();
1841 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001842 }
1843 break;
1844 }
csmartdalton578f0642017-02-24 16:04:47 -07001845 }
liyuqianb73c24b2016-06-03 08:47:23 -07001846 } else if (stateName.equals(kSoftkeyStateName)) {
1847 if (!stateValue.equals(kSoftkeyHint)) {
1848 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001849 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001850 }
liyuqian2edb0f42016-07-06 14:11:32 -07001851 } else if (stateName.equals(kRefreshStateName)) {
1852 // This state is actually NOT in the UI state.
1853 // We use this to allow Android to quickly set bool fRefresh.
1854 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001855 } else {
1856 SkDebugf("Unknown stateName: %s", stateName.c_str());
1857 }
1858}
Brian Osman79086b92017-02-10 13:36:16 -05001859
1860bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001861 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001862}
1863
1864bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001865 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001866 fWindow->inval();
1867 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001868 } else {
1869 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001870 }
Brian Osman79086b92017-02-10 13:36:16 -05001871}