blob: 7695aab05aef9f96850ce1014d8541468d662abb [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"
jvanverth2bb3b6d2016-04-08 07:24:09 -070031#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050032#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050033#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050034#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040035#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070036#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070037#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050038#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050039#include "SkTestFontMgr.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050040#include "SkThreadedBMPDevice.h"
jvanverth9f372462016-04-06 06:08:59 -070041
Brian Osman79086b92017-02-10 13:36:16 -050042#include "imgui.h"
43
Chris Dalton1a325d22017-07-14 15:17:41 -060044#include "ccpr/GrCoverageCountingPathRenderer.h"
45
csmartdalton578f0642017-02-24 16:04:47 -070046#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070047#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070048
jvanverth34524262016-05-04 13:49:13 -070049using namespace sk_app;
50
csmartdalton61cd31a2017-02-27 17:00:53 -070051static std::map<GpuPathRenderers, std::string> gPathRendererNames;
52
jvanverth9f372462016-04-06 06:08:59 -070053Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070054 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070055}
56
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060057static DEFINE_string(slide, "", "Start on this sample.");
58static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050059
bsalomon6c471f72016-07-26 12:56:32 -070060#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070061# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070062#else
63# define BACKENDS_STR "\"sw\" and \"gl\""
64#endif
65
Brian Osman2dd96932016-10-18 15:33:53 -040066static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070067
Brian Salomonbdecacf2018-02-02 20:32:49 -050068static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070069
Chris Dalton2d18f412018-02-20 13:23:32 -070070DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
71
Brian Osman53136aa2017-07-20 15:43:35 -040072DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050073
Brian Salomon194db172017-08-17 14:37:06 -040074const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070075 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040076#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
77 "ANGLE",
78#endif
jvanverth063ece72016-06-17 09:29:14 -070079#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070080 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070081#endif
csmartdalton578f0642017-02-24 16:04:47 -070082 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070083};
84
bsalomon6c471f72016-07-26 12:56:32 -070085static sk_app::Window::BackendType get_backend_type(const char* str) {
86#ifdef SK_VULKAN
87 if (0 == strcmp(str, "vk")) {
88 return sk_app::Window::kVulkan_BackendType;
89 } else
90#endif
Brian Salomon194db172017-08-17 14:37:06 -040091#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
92 if (0 == strcmp(str, "angle")) {
93 return sk_app::Window::kANGLE_BackendType;
94 } else
95#endif
bsalomon6c471f72016-07-26 12:56:32 -070096 if (0 == strcmp(str, "gl")) {
97 return sk_app::Window::kNativeGL_BackendType;
98 } else if (0 == strcmp(str, "sw")) {
99 return sk_app::Window::kRaster_BackendType;
100 } else {
101 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
102 return sk_app::Window::kRaster_BackendType;
103 }
104}
105
Brian Osmana109e392017-02-24 09:49:14 -0500106static SkColorSpacePrimaries gSrgbPrimaries = {
107 0.64f, 0.33f,
108 0.30f, 0.60f,
109 0.15f, 0.06f,
110 0.3127f, 0.3290f };
111
112static SkColorSpacePrimaries gAdobePrimaries = {
113 0.64f, 0.33f,
114 0.21f, 0.71f,
115 0.15f, 0.06f,
116 0.3127f, 0.3290f };
117
118static SkColorSpacePrimaries gP3Primaries = {
119 0.680f, 0.320f,
120 0.265f, 0.690f,
121 0.150f, 0.060f,
122 0.3127f, 0.3290f };
123
124static SkColorSpacePrimaries gRec2020Primaries = {
125 0.708f, 0.292f,
126 0.170f, 0.797f,
127 0.131f, 0.046f,
128 0.3127f, 0.3290f };
129
130struct NamedPrimaries {
131 const char* fName;
132 SkColorSpacePrimaries* fPrimaries;
133} gNamedPrimaries[] = {
134 { "sRGB", &gSrgbPrimaries },
135 { "AdobeRGB", &gAdobePrimaries },
136 { "P3", &gP3Primaries },
137 { "Rec. 2020", &gRec2020Primaries },
138};
139
140static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
141 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
142}
143
Brian Osman70d2f432017-11-08 09:54:10 -0500144static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
145 // In raster mode, we still use GL for the window.
146 // This lets us render the GUI faster (and correct).
147 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
148}
149
liyuqiane5a6cd92016-05-27 08:52:52 -0700150const char* kName = "name";
151const char* kValue = "value";
152const char* kOptions = "options";
153const char* kSlideStateName = "Slide";
154const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700155const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700156const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700157const char* kSoftkeyStateName = "Softkey";
158const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700159const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700160const char* kON = "ON";
161const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700162const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700163
jvanverth34524262016-05-04 13:49:13 -0700164Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500165 : fCurrentSlide(-1)
166 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500167 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500168 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500169 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500170 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500171 , fShowZoomWindow(false)
172 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700173 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500174 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500175 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500176 // Our UI can only tweak gamma (currently), so start out gamma-only
177 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700178 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400179 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500180 , fTileCnt(0)
181 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700182{
Greg Daniel285db442016-10-14 09:12:53 -0400183 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700184
Brian Osmanf09e35e2017-12-15 14:48:09 -0500185 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
186 gPathRendererNames[GpuPathRenderers::kDefault] =
187 "Default Ganesh Behavior (best path renderer, not including CCPR)";
188 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
189 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
190 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
191 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
192 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
193 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700194
jvanverth2bb3b6d2016-04-08 07:24:09 -0700195 SkDebugf("Command line arguments: ");
196 for (int i = 1; i < argc; ++i) {
197 SkDebugf("%s ", argv[i]);
198 }
199 SkDebugf("\n");
200
201 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500202#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400203 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500204#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700205
Ben Wagner483c7722018-02-20 17:06:07 -0500206 if (!FLAGS_nativeFonts) {
207 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
208 }
209
Brian Osmanbc8150f2017-07-24 11:38:01 -0400210 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400211 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400212
bsalomon6c471f72016-07-26 12:56:32 -0700213 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700214 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700215
csmartdalton578f0642017-02-24 16:04:47 -0700216 DisplayParams displayParams;
217 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700218 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700219 fWindow->setRequestedDisplayParams(displayParams);
220
Brian Osman56a24812017-12-19 11:15:16 -0500221 // Configure timers
222 fStatsLayer.setActive(false);
223 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
224 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
225 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
226
jvanverth9f372462016-04-06 06:08:59 -0700227 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700228 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500229 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500230 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500231 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700232
brianosman622c8d52016-05-10 06:50:49 -0700233 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500234 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
235 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
236 fWindow->inval();
237 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500238 // Command to jump directly to the slide picker and give it focus
239 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
240 this->fShowImGuiDebugWindow = true;
241 this->fShowSlidePicker = true;
242 fWindow->inval();
243 });
244 // Alias that to Backspace, to match SampleApp
245 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
246 this->fShowImGuiDebugWindow = true;
247 this->fShowSlidePicker = true;
248 fWindow->inval();
249 });
Brian Osman79086b92017-02-10 13:36:16 -0500250 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
251 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
252 fWindow->inval();
253 });
Brian Osmanf6877092017-02-13 09:39:57 -0500254 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
255 this->fShowZoomWindow = !this->fShowZoomWindow;
256 fWindow->inval();
257 });
brianosman622c8d52016-05-10 06:50:49 -0700258 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500259 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700260 fWindow->inval();
261 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400262 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500263 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400264 this->updateTitle();
265 fWindow->inval();
266 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500267 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500268 switch (fColorMode) {
269 case ColorMode::kLegacy:
270 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
271 break;
272 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
273 this->setColorMode(ColorMode::kColorManagedSRGB8888);
274 break;
275 case ColorMode::kColorManagedSRGB8888:
276 this->setColorMode(ColorMode::kColorManagedLinearF16);
277 break;
278 case ColorMode::kColorManagedLinearF16:
279 this->setColorMode(ColorMode::kLegacy);
280 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500281 }
brianosman622c8d52016-05-10 06:50:49 -0700282 });
283 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500284 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700285 });
286 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500287 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700288 });
289 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
290 this->changeZoomLevel(1.f / 32.f);
291 fWindow->inval();
292 });
293 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
294 this->changeZoomLevel(-1.f / 32.f);
295 fWindow->inval();
296 });
jvanverthaf236b52016-05-20 06:01:06 -0700297 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400298 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
299 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500300 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400301#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
302 if (newBackend == sk_app::Window::kVulkan_BackendType) {
303 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
304 sk_app::Window::kBackendTypeCount);
305 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
306 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500307 }
308#endif
Brian Osman621491e2017-02-28 15:45:01 -0500309 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700310 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500311 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
312 fTileCnt++;
313 if (fThreadCnt == 0) {
314 this->resetExecutor();
315 }
316 this->updateTitle();
317 fWindow->inval();
318 });
319 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
320 fTileCnt = SkTMax(0, fTileCnt - 1);
321 if (fThreadCnt == 0) {
322 this->resetExecutor();
323 }
324 this->updateTitle();
325 fWindow->inval();
326 });
327 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
328 if (fTileCnt == 0) {
329 return;
330 }
331 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
332 this->resetExecutor();
333 this->updateTitle();
334 fWindow->inval();
335 });
336 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
337 if (fTileCnt == 0) {
338 return;
339 }
340 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
341 this->resetExecutor();
342 this->updateTitle();
343 fWindow->inval();
344 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500345 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
346 fSaveToSKP = true;
347 fWindow->inval();
348 });
Ben Wagner37c54032018-04-13 14:30:23 -0400349 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
350 DisplayParams params = fWindow->getRequestedDisplayParams();
351 uint32_t flags = params.fSurfaceProps.flags();
352 if (!fPixelGeometryOverrides) {
353 fPixelGeometryOverrides = true;
354 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
355 } else {
356 switch (params.fSurfaceProps.pixelGeometry()) {
357 case kUnknown_SkPixelGeometry:
358 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
359 break;
360 case kRGB_H_SkPixelGeometry:
361 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
362 break;
363 case kBGR_H_SkPixelGeometry:
364 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
365 break;
366 case kRGB_V_SkPixelGeometry:
367 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
368 break;
369 case kBGR_V_SkPixelGeometry:
370 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
371 fPixelGeometryOverrides = false;
372 break;
373 }
374 }
375 fWindow->setRequestedDisplayParams(params);
376 this->updateTitle();
377 fWindow->inval();
378 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500379 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
380 if (!fPaintOverrides.fHinting) {
381 fPaintOverrides.fHinting = true;
382 fPaint.setHinting(SkPaint::kNo_Hinting);
383 } else {
384 switch (fPaint.getHinting()) {
385 case SkPaint::kNo_Hinting:
386 fPaint.setHinting(SkPaint::kSlight_Hinting);
387 break;
388 case SkPaint::kSlight_Hinting:
389 fPaint.setHinting(SkPaint::kNormal_Hinting);
390 break;
391 case SkPaint::kNormal_Hinting:
392 fPaint.setHinting(SkPaint::kFull_Hinting);
393 break;
394 case SkPaint::kFull_Hinting:
395 fPaint.setHinting(SkPaint::kNo_Hinting);
396 fPaintOverrides.fHinting = false;
397 break;
398 }
399 }
400 this->updateTitle();
401 fWindow->inval();
402 });
403 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
404 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
405 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
406 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
407 fPaint.setAntiAlias(false);
408 fPaintOverrides.fOriginalSkUseAnalyticAA = gSkUseAnalyticAA;
409 fPaintOverrides.fOriginalSkForceAnalyticAA = gSkForceAnalyticAA;
410 fPaintOverrides.fOriginalSkUseDeltaAA = gSkUseDeltaAA;
411 fPaintOverrides.fOriginalSkForceDeltaAA = gSkForceDeltaAA;
412 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
413 gSkUseDeltaAA = gSkForceDeltaAA = false;
414 } else {
415 fPaint.setAntiAlias(true);
416 switch (fPaintOverrides.fAntiAlias) {
417 case SkPaintFields::AntiAliasState::Alias:
418 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
419 break;
420 case SkPaintFields::AntiAliasState::Normal:
421 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
422 gSkUseDeltaAA = gSkForceDeltaAA = false;
423 gSkUseAnalyticAA = true;
424 break;
425 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
426 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
427 gSkForceAnalyticAA = true;
428 break;
429 case SkPaintFields::AntiAliasState::AnalyticAAForced:
430 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
431 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
432 gSkUseDeltaAA = true;
433 break;
434 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
435 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
436 gSkForceDeltaAA = true;
437 break;
438 case SkPaintFields::AntiAliasState::DeltaAAForced:
439 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
440 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
441 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
442 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
443 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
444 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
445 break;
446 }
447 }
448 this->updateTitle();
449 fWindow->inval();
450 });
Ben Wagner37c54032018-04-13 14:30:23 -0400451 fCommands.addCommand('D', "Modes", "DFT", [this]() {
452 DisplayParams params = fWindow->getRequestedDisplayParams();
453 uint32_t flags = params.fSurfaceProps.flags();
454 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
455 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
456 fWindow->setRequestedDisplayParams(params);
457 this->updateTitle();
458 fWindow->inval();
459 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500460 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
461 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
462 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
463 fPaint.setLCDRenderText(false);
464 } else {
465 if (!fPaint.isLCDRenderText()) {
466 fPaint.setLCDRenderText(true);
467 } else {
468 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
469 }
470 }
471 this->updateTitle();
472 fWindow->inval();
473 });
474 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
475 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
476 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
477 fPaint.setSubpixelText(false);
478 } else {
479 if (!fPaint.isSubpixelText()) {
480 fPaint.setSubpixelText(true);
481 } else {
482 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
483 }
484 }
485 this->updateTitle();
486 fWindow->inval();
487 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500488
jvanverth2bb3b6d2016-04-08 07:24:09 -0700489 // set up slides
490 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500491 if (FLAGS_list) {
492 this->listNames();
493 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700494
djsollen12d62a72016-04-21 07:59:44 -0700495 fAnimTimer.run();
496
Hal Canaryc465d132017-12-08 10:21:31 -0500497 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500498 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400499 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500500 }
501 fImGuiGamutPaint.setColor(SK_ColorWHITE);
502 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
503
Brian Osman70d2f432017-11-08 09:54:10 -0500504 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500505 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700506}
507
jvanverth34524262016-05-04 13:49:13 -0700508void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700509 fAllSlideNames = Json::Value(Json::arrayValue);
510
Chris Dalton2d18f412018-02-20 13:23:32 -0700511 // Bisect slide.
512 if (!FLAGS_bisect.isEmpty()) {
513 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
514 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
515 if (FLAGS_bisect.count() >= 2) {
516 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
517 bisect->onChar(*ch);
518 }
519 }
520 fSlides.push_back(std::move(bisect));
521 }
522 }
523
524 // GMs
525 int firstGM = fSlides.count();
jvanverth2bb3b6d2016-04-08 07:24:09 -0700526 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
527 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400528 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700529
530 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
531 sk_sp<Slide> slide(new GMSlide(gm.release()));
Chris Dalton2d18f412018-02-20 13:23:32 -0700532 fSlides.push_back(std::move(slide));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700533 }
534
535 gms = gms->next();
536 }
Chris Dalton2d18f412018-02-20 13:23:32 -0700537 // reverse gms
538 int numGMs = fSlides.count() - firstGM;
539 for (int i = 0; i < numGMs/2; ++i) {
540 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
jvanverth2bb3b6d2016-04-08 07:24:09 -0700541 }
542
jvanverthc7027ab2016-06-16 09:52:35 -0700543 // samples
544 const SkViewRegister* reg = SkViewRegister::Head();
545 while (reg) {
546 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700547 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
548 fSlides.push_back(slide);
549 }
jvanverthc7027ab2016-06-16 09:52:35 -0700550 reg = reg->next();
551 }
552
Florin Malita0ffa3222018-04-05 14:34:45 -0400553 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
554 static const struct {
555 const char* fExtension;
556 const char* fDirName;
557 const SkCommandLineFlags::StringArray& fFlags;
558 const SlideFactory fFactory;
559 } gExternalSlidesInfo[] = {
560 { ".skp", "skp-dir", FLAGS_skps,
561 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
562 return sk_make_sp<SKPSlide>(name, path);}
563 },
564 { ".jpg", "jpg-dir", FLAGS_jpgs,
565 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
566 return sk_make_sp<ImageSlide>(name, path);}
567 },
568 { ".json", "skottie-dir", FLAGS_jsons,
569 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
570 return sk_make_sp<SkottieSlide>(name, path);}
571 },
572 { ".svg", "svg-dir", FLAGS_svgs,
573 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
574 return sk_make_sp<SvgSlide>(name, path);}
575 },
576 };
jvanverthc265a922016-04-08 12:51:45 -0700577
Florin Malita0ffa3222018-04-05 14:34:45 -0400578 SkTArray<sk_sp<Slide>, true> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700579
Florin Malita0ffa3222018-04-05 14:34:45 -0400580 const auto addSlide = [&](const SkString& name,
581 const SkString& path,
582 const SlideFactory& fact) {
583 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
584 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700585 }
liyuqian6f163d22016-06-13 12:26:45 -0700586
Florin Malita0ffa3222018-04-05 14:34:45 -0400587 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500588 dirSlides.push_back(slide);
589 fSlides.push_back(std::move(slide));
590 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400591 };
Florin Malita76a076b2018-02-15 18:40:48 -0500592
Florin Malita0ffa3222018-04-05 14:34:45 -0400593 for (const auto& info : gExternalSlidesInfo) {
594 for (const auto& flag : info.fFlags) {
595 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
596 // single file
597 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
598 } else {
599 // directory
600 SkOSFile::Iter it(flag.c_str(), info.fExtension);
601 SkString name;
602 while (it.next(&name)) {
603 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
604 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400605 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400606
607 if (!dirSlides.empty()) {
608 fSlides.push_back(
609 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
610 std::move(dirSlides)));
611 dirSlides.reset();
612 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400613 }
614 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700615}
616
617
jvanverth34524262016-05-04 13:49:13 -0700618Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700619 fWindow->detach();
620 delete fWindow;
621}
622
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500623struct SkPaintTitleUpdater {
624 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
625 void append(const char* s) {
626 if (fCount == 0) {
627 fTitle->append(" {");
628 } else {
629 fTitle->append(", ");
630 }
631 fTitle->append(s);
632 ++fCount;
633 }
634 void done() {
635 if (fCount > 0) {
636 fTitle->append("}");
637 }
638 }
639 SkString* fTitle;
640 int fCount;
641};
642
brianosman05de2162016-05-06 13:28:57 -0700643void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700644 if (!fWindow) {
645 return;
646 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500647 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700648 return; // Surface hasn't been created yet.
649 }
650
jvanverth34524262016-05-04 13:49:13 -0700651 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700652 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700653
Yuqian Li399b3c22017-08-03 11:08:15 -0400654 if (gSkUseDeltaAA) {
655 if (gSkForceDeltaAA) {
656 title.append(" <FDAA>");
657 } else {
658 title.append(" <DAA>");
659 }
660 } else if (gSkUseAnalyticAA) {
661 if (gSkForceAnalyticAA) {
662 title.append(" <FAAA>");
663 } else {
664 title.append(" <AAA>");
665 }
666 }
667
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500668 SkPaintTitleUpdater paintTitle(&title);
669 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
670 if (fPaint.isAntiAlias()) {
671 paintTitle.append("Antialias");
672 } else {
673 paintTitle.append("Alias");
674 }
675 }
676 if (fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag) {
677 if (fPaint.isLCDRenderText()) {
678 paintTitle.append("LCD");
679 } else {
680 paintTitle.append("lcd");
681 }
682 }
683 if (fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag) {
684 if (fPaint.isSubpixelText()) {
685 paintTitle.append("Subpixel Glyphs");
686 } else {
687 paintTitle.append("Pixel Glyphs");
688 }
689 }
690 if (fPaintOverrides.fHinting) {
691 switch (fPaint.getHinting()) {
692 case SkPaint::kNo_Hinting:
693 paintTitle.append("No Hinting");
694 break;
695 case SkPaint::kSlight_Hinting:
696 paintTitle.append("Slight Hinting");
697 break;
698 case SkPaint::kNormal_Hinting:
699 paintTitle.append("Normal Hinting");
700 break;
701 case SkPaint::kFull_Hinting:
702 paintTitle.append("Full Hinting");
703 break;
704 }
705 }
706 paintTitle.done();
707
Yuqian Lib2ba6642017-11-22 12:07:41 -0500708 if (fTileCnt > 0) {
709 title.appendf(" T%d", fTileCnt);
710 if (fThreadCnt > 0) {
711 title.appendf("/%d", fThreadCnt);
712 }
713 }
714
Brian Osman92004802017-03-06 11:47:26 -0500715 switch (fColorMode) {
716 case ColorMode::kLegacy:
717 title.append(" Legacy 8888");
718 break;
719 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
720 title.append(" ColorManaged 8888 (Nonlinear blending)");
721 break;
722 case ColorMode::kColorManagedSRGB8888:
723 title.append(" ColorManaged 8888");
724 break;
725 case ColorMode::kColorManagedLinearF16:
726 title.append(" ColorManaged F16");
727 break;
728 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500729
Brian Osman92004802017-03-06 11:47:26 -0500730 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500731 int curPrimaries = -1;
732 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
733 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
734 curPrimaries = i;
735 break;
736 }
737 }
738 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500739
740 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
741 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
742 }
brianosman05de2162016-05-06 13:28:57 -0700743 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500744
Ben Wagner37c54032018-04-13 14:30:23 -0400745 const DisplayParams& params = fWindow->getRequestedDisplayParams();
746 if (fPixelGeometryOverrides) {
747 switch (params.fSurfaceProps.pixelGeometry()) {
748 case kUnknown_SkPixelGeometry:
749 title.append( " Flat");
750 break;
751 case kRGB_H_SkPixelGeometry:
752 title.append( " RGB");
753 break;
754 case kBGR_H_SkPixelGeometry:
755 title.append( " BGR");
756 break;
757 case kRGB_V_SkPixelGeometry:
758 title.append( " RGBV");
759 break;
760 case kBGR_V_SkPixelGeometry:
761 title.append( " BGRV");
762 break;
763 }
764 }
765
766 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
767 title.append(" DFT");
768 }
769
csmartdalton578f0642017-02-24 16:04:47 -0700770 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700771 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500772 int msaa = fWindow->sampleCount();
773 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700774 title.appendf(" MSAA: %i", msaa);
775 }
776 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700777
778 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400779 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700780 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
781 }
782
brianosman05de2162016-05-06 13:28:57 -0700783 fWindow->setTitle(title.c_str());
784}
785
Florin Malitaab99c342018-01-16 16:23:03 -0500786int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500787
788 if (!FLAGS_slide.isEmpty()) {
789 int count = fSlides.count();
790 for (int i = 0; i < count; i++) {
791 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500792 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500793 }
794 }
795
796 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
797 this->listNames();
798 }
799
Florin Malitaab99c342018-01-16 16:23:03 -0500800 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500801}
802
Florin Malitaab99c342018-01-16 16:23:03 -0500803void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500804 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500805 for (const auto& slide : fSlides) {
806 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500807 }
808}
809
Florin Malitaab99c342018-01-16 16:23:03 -0500810void Viewer::setCurrentSlide(int slide) {
811 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700812
Florin Malitaab99c342018-01-16 16:23:03 -0500813 if (slide == fCurrentSlide) {
814 return;
815 }
816
817 if (fCurrentSlide >= 0) {
818 fSlides[fCurrentSlide]->unload();
819 }
820
821 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
822 SkIntToScalar(fWindow->height()));
823 fCurrentSlide = slide;
824 this->setupCurrentSlide();
825}
826
827void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500828 if (fCurrentSlide >= 0) {
829 // prepare dimensions for image slides
830 fGesture.resetTouchState();
831 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700832
Jim Van Verth0848fb02018-01-22 13:39:30 -0500833 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
834 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
835 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400836
Jim Van Verth0848fb02018-01-22 13:39:30 -0500837 // Start with a matrix that scales the slide to the available screen space
838 if (fWindow->scaleContentToFit()) {
839 if (windowRect.width() > 0 && windowRect.height() > 0) {
840 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
841 }
liyuqiane46e4f02016-05-20 07:32:19 -0700842 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500843
844 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400845 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500846
847 this->updateTitle();
848 this->updateUIState();
849
850 fStatsLayer.resetMeasurements();
851
852 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700853 }
jvanverthc265a922016-04-08 12:51:45 -0700854}
855
856#define MAX_ZOOM_LEVEL 8
857#define MIN_ZOOM_LEVEL -8
858
jvanverth34524262016-05-04 13:49:13 -0700859void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700860 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400861 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Yuqian Li755778c2018-03-28 16:23:31 -0400862
863 // Update the trans limit as the zoom level changes.
864 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
865 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
866 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
867 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
868}
869
870SkMatrix Viewer::computePreTouchMatrix() {
871 SkMatrix m = fDefaultMatrix;
872 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
873 : SK_Scalar1 + fZoomLevel;
874 m.preScale(zoomScale, zoomScale);
875 return m;
jvanverthc265a922016-04-08 12:51:45 -0700876}
877
liyuqiand3cdbca2016-05-17 12:44:20 -0700878SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400879 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700880 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400881 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700882 return m;
jvanverthc265a922016-04-08 12:51:45 -0700883}
884
Brian Osman621491e2017-02-28 15:45:01 -0500885void Viewer::setBackend(sk_app::Window::BackendType backendType) {
886 fBackendType = backendType;
887
888 fWindow->detach();
889
Brian Osman70d2f432017-11-08 09:54:10 -0500890#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400891 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
892 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500893 DisplayParams params = fWindow->getRequestedDisplayParams();
894 delete fWindow;
895 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500896
Brian Osman70d2f432017-11-08 09:54:10 -0500897 // re-register callbacks
898 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500899 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500900 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500901 fWindow->pushLayer(&fImGuiLayer);
902
Brian Osman70d2f432017-11-08 09:54:10 -0500903 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
904 // will still include our correct sample count. But the re-created fWindow will lose that
905 // information. On Windows, we need to re-create the window when changing sample count,
906 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
907 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
908 // as if we had never changed windows at all).
909 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500910#endif
911
Brian Osman70d2f432017-11-08 09:54:10 -0500912 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500913}
914
Brian Osman92004802017-03-06 11:47:26 -0500915void Viewer::setColorMode(ColorMode colorMode) {
916 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700917
Brian Osmanf750fbc2017-02-08 10:47:28 -0500918 // 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 -0400919 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700920 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500921 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400922 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
923 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500924 if (wasInLegacy != wantLegacy) {
925 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700926 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500927 }
928
929 this->updateTitle();
930 fWindow->inval();
931}
932
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500933class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
934public:
935 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
936 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
937 { }
938 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -0400939 if (*paint == nullptr) {
940 return true;
941 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500942 if (fPaintOverrides->fHinting) {
943 paint->writable()->setHinting(fPaint->getHinting());
944 }
945 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
946 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
947 }
948 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
949 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
950 }
951 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
952 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
953 }
954 return true;
955 }
956 SkPaint* fPaint;
957 Viewer::SkPaintFields* fPaintOverrides;
958};
959
Brian Osmanf750fbc2017-02-08 10:47:28 -0500960void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500961 SkAutoCanvasRestore autorestore(canvas, false);
962
Brian Osmanf750fbc2017-02-08 10:47:28 -0500963 // By default, we render directly into the window's surface/canvas
964 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500965 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700966
Brian Osmane0d4fba2017-03-15 10:24:55 -0400967 // If we're in any of the color managed modes, construct the color space we're going to use
968 sk_sp<SkColorSpace> cs = nullptr;
969 if (ColorMode::kLegacy != fColorMode) {
970 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
971 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400972 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400973 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500974 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
975 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
976 } else {
977 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
978 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400979 }
980
Brian Osman3ac99cf2017-12-01 11:23:53 -0500981 if (fSaveToSKP) {
982 SkPictureRecorder recorder;
983 SkCanvas* recorderCanvas = recorder.beginRecording(
984 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
985 // In xform-canvas mode, record the transformed output
986 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
987 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
988 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
989 recorderCanvas = xformCanvas.get();
990 }
991 fSlides[fCurrentSlide]->draw(recorderCanvas);
992 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
993 SkFILEWStream stream("sample_app.skp");
994 picture->serialize(&stream);
995 fSaveToSKP = false;
996 }
997
Brian Osmane0d4fba2017-03-15 10:24:55 -0400998 // 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 -0500999 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
1000 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -05001001 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -05001002 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
1003 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -05001004 if (Window::kRaster_BackendType == fBackendType ||
1005 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -05001006 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -04001007 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
1008 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
1009
Brian Osman92004802017-03-06 11:47:26 -05001010 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
1011 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001012 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
1013 // with a special canvas (below) that has the color space attached
1014 sk_sp<SkColorSpace> offscreenColorSpace =
1015 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -05001016 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -04001017 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Ben Wagner37c54032018-04-13 14:30:23 -04001018 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1019 canvas->getProps(&props);
1020 offscreenSurface = Window::kRaster_BackendType == fBackendType
1021 ? SkSurface::MakeRaster(info, &props)
1022 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -05001023 SkPixmap offscreenPixmap;
1024 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
1025 SkBitmap offscreenBitmap;
1026 offscreenBitmap.installPixels(offscreenPixmap);
1027 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
1028 fThreadCnt, fExecutor.get()));
1029 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
1030 slideCanvas = threadedCanvas.get();
1031 } else {
1032 slideCanvas = offscreenSurface->getCanvas();
1033 }
Brian Osmanf750fbc2017-02-08 10:47:28 -05001034 }
1035
Brian Osmane0d4fba2017-03-15 10:24:55 -04001036 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
1037 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1038 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
1039 slideCanvas = xformCanvas.get();
1040 }
1041
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001042 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001043 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001044 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -05001045 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001046 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001047 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
1048 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -05001049 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001050 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001051
1052 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001053 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001054 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001055 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001056
1057 // If we rendered offscreen, snap an image and push the results to the window's canvas
1058 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001059 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001060
1061 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -04001062 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -05001063 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -04001064 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001065 SkPaint paint;
1066 paint.setBlendMode(SkBlendMode::kSrc);
1067 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -07001068 }
liyuqian6f163d22016-06-13 12:26:45 -07001069}
1070
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001071void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001072 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001073 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001074}
Jim Van Verth6f449692017-02-14 15:16:46 -05001075
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001076void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001077 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001078
brianosman622c8d52016-05-10 06:50:49 -07001079 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001080
Brian Osmand67e5182017-12-08 16:46:09 -05001081 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -05001082
Brian Osman1df161a2017-02-09 12:10:20 -05001083 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -04001084 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001085}
1086
Florin Malitacefc1b92018-02-19 21:43:47 -05001087SkPoint Viewer::mapEvent(float x, float y) {
1088 const auto m = this->computeMatrix();
1089 SkMatrix inv;
1090
1091 SkAssertResult(m.invert(&inv));
1092
1093 return inv.mapXY(x, y);
1094}
1095
jvanverth814e38d2016-06-06 08:48:47 -07001096bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001097 if (GestureDevice::kMouse == fGestureDevice) {
1098 return false;
1099 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001100
1101 const auto slidePt = this->mapEvent(x, y);
1102 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1103 fWindow->inval();
1104 return true;
1105 }
1106
liyuqiand3cdbca2016-05-17 12:44:20 -07001107 void* castedOwner = reinterpret_cast<void*>(owner);
1108 switch (state) {
1109 case Window::kUp_InputState: {
1110 fGesture.touchEnd(castedOwner);
1111 break;
1112 }
1113 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001114 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001115 break;
1116 }
1117 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001118 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001119 break;
1120 }
1121 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001122 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001123 fWindow->inval();
1124 return true;
1125}
1126
Brian Osman80fc07e2017-12-08 16:45:43 -05001127bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001128 if (GestureDevice::kTouch == fGestureDevice) {
1129 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001130 }
Brian Osman16c81a12017-12-20 11:58:34 -05001131
Florin Malitacefc1b92018-02-19 21:43:47 -05001132 const auto slidePt = this->mapEvent(x, y);
1133 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1134 fWindow->inval();
1135 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001136 }
1137
1138 switch (state) {
1139 case Window::kUp_InputState: {
1140 fGesture.touchEnd(nullptr);
1141 break;
1142 }
1143 case Window::kDown_InputState: {
1144 fGesture.touchBegin(nullptr, x, y);
1145 break;
1146 }
1147 case Window::kMove_InputState: {
1148 fGesture.touchMoved(nullptr, x, y);
1149 break;
1150 }
1151 }
1152 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1153
1154 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1155 fWindow->inval();
1156 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001157 return true;
1158}
1159
Brian Osmana109e392017-02-24 09:49:14 -05001160static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1161 const ImVec2& pos, const ImVec2& size) {
1162 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1163 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1164
1165 // Invisible 10x10 button
1166 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1167 ImGui::InvisibleButton(label, ImVec2(10, 10));
1168
1169 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1170 ImGuiIO& io = ImGui::GetIO();
1171 // Normalized mouse position, relative to our gamut box
1172 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1173 // Clamp to edge of box, convert back to primary scale
1174 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1175 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1176 }
1177
1178 if (ImGui::IsItemHovered()) {
1179 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1180 }
1181
1182 // Return screen coordinates for the caller. We could just return center here, but we'd have
1183 // one frame of lag during drag.
1184 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1185}
1186
1187static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1188 ImDrawList* drawList = ImGui::GetWindowDrawList();
1189
1190 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1191 // width, and scale the height to maintain aspect ratio.
1192 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1193 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1194 ImVec2 pos = ImGui::GetCursorScreenPos();
1195
1196 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1197 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1198 // Magic numbers are pixel locations of the origin and upper-right corner.
1199 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1200 ImVec2(242, 61), ImVec2(1897, 1922));
1201 ImVec2 endPos = ImGui::GetCursorPos();
1202
1203 // Primary markers
1204 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1205 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1206 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1207 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1208
1209 // Gamut triangle
1210 drawList->AddCircle(r, 5.0f, 0xFF000040);
1211 drawList->AddCircle(g, 5.0f, 0xFF004000);
1212 drawList->AddCircle(b, 5.0f, 0xFF400000);
1213 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1214 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1215
1216 // Re-position cursor immediate after the diagram for subsequent controls
1217 ImGui::SetCursorPos(endPos);
1218}
1219
Brian Osmand67e5182017-12-08 16:46:09 -05001220void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001221 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1222 if (fShowImGuiTestWindow) {
1223 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1224 }
1225
1226 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001227 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1228 // always visible, we can end up in a layout feedback loop.
1229 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001230 DisplayParams params = fWindow->getRequestedDisplayParams();
1231 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001232 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1233 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001234 if (ImGui::CollapsingHeader("Backend")) {
1235 int newBackend = static_cast<int>(fBackendType);
1236 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1237 ImGui::SameLine();
1238 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001239#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1240 ImGui::SameLine();
1241 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1242#endif
Brian Osman621491e2017-02-28 15:45:01 -05001243#if defined(SK_VULKAN)
1244 ImGui::SameLine();
1245 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1246#endif
1247 if (newBackend != fBackendType) {
1248 fDeferredActions.push_back([=]() {
1249 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1250 });
1251 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001252
Brian Salomon99a33902017-03-07 15:16:34 -05001253 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001254 bool* wire = &params.fGrContextOptions.fWireframeMode;
1255 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1256 paramsChanged = true;
1257 }
Brian Salomon99a33902017-03-07 15:16:34 -05001258
Brian Osman28b12522017-03-08 17:10:24 -05001259 if (ctx) {
1260 int sampleCount = fWindow->sampleCount();
1261 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001262 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001263 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1264 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1265 ImGui::RadioButton("16", &sampleCount, 16);
1266
1267 if (sampleCount != params.fMSAASampleCount) {
1268 params.fMSAASampleCount = sampleCount;
1269 paramsChanged = true;
1270 }
1271 }
1272
Ben Wagner37c54032018-04-13 14:30:23 -04001273 int pixelGeometryIdx = 0;
1274 if (fPixelGeometryOverrides) {
1275 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1276 }
1277 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1278 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1279 {
1280 uint32_t flags = params.fSurfaceProps.flags();
1281 if (pixelGeometryIdx == 0) {
1282 fPixelGeometryOverrides = false;
1283 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1284 } else {
1285 fPixelGeometryOverrides = true;
1286 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1287 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1288 }
1289 paramsChanged = true;
1290 }
1291
1292 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1293 if (ImGui::Checkbox("DFT", &useDFT)) {
1294 uint32_t flags = params.fSurfaceProps.flags();
1295 if (useDFT) {
1296 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1297 } else {
1298 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1299 }
1300 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1301 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1302 paramsChanged = true;
1303 }
1304
Brian Osman8a9de3d2017-03-01 14:59:05 -05001305 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001306 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001307 auto prButton = [&](GpuPathRenderers x) {
1308 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001309 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1310 params.fGrContextOptions.fGpuPathRenderers = x;
1311 paramsChanged = true;
1312 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001313 }
1314 };
1315
1316 if (!ctx) {
1317 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001318 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001319 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001320 prButton(GpuPathRenderers::kAll);
1321 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1322 prButton(GpuPathRenderers::kStencilAndCover);
1323 }
1324 if (ctx->caps()->sampleShadingSupport()) {
1325 prButton(GpuPathRenderers::kMSAA);
1326 }
1327 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001328 prButton(GpuPathRenderers::kNone);
1329 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001330 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001331 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001332 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1333 prButton(GpuPathRenderers::kCoverageCounting);
1334 }
Jim Van Verth83010462017-03-16 08:45:39 -04001335 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001336 prButton(GpuPathRenderers::kTessellating);
1337 prButton(GpuPathRenderers::kNone);
1338 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001339 ImGui::TreePop();
1340 }
Brian Osman621491e2017-02-28 15:45:01 -05001341 }
1342
Brian Osmanfce09c52017-11-14 15:32:20 -05001343 if (fShowSlidePicker) {
1344 ImGui::SetNextTreeNodeOpen(true);
1345 }
1346
Brian Osman79086b92017-02-10 13:36:16 -05001347 if (ImGui::CollapsingHeader("Slide")) {
1348 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001349 static ImVector<const char*> filteredSlideNames;
1350 static ImVector<int> filteredSlideIndices;
1351
Brian Osmanfce09c52017-11-14 15:32:20 -05001352 if (fShowSlidePicker) {
1353 ImGui::SetKeyboardFocusHere();
1354 fShowSlidePicker = false;
1355 }
1356
Brian Osman79086b92017-02-10 13:36:16 -05001357 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001358 filteredSlideNames.clear();
1359 filteredSlideIndices.clear();
1360 int filteredIndex = 0;
1361 for (int i = 0; i < fSlides.count(); ++i) {
1362 const char* slideName = fSlides[i]->getName().c_str();
1363 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1364 if (i == fCurrentSlide) {
1365 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001366 }
Brian Osmanf479e422017-11-08 13:11:36 -05001367 filteredSlideNames.push_back(slideName);
1368 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001369 }
Brian Osman79086b92017-02-10 13:36:16 -05001370 }
Brian Osmanf479e422017-11-08 13:11:36 -05001371
Brian Osmanf479e422017-11-08 13:11:36 -05001372 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1373 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001374 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001375 }
1376 }
Brian Osmana109e392017-02-24 09:49:14 -05001377
1378 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001379 ColorMode newMode = fColorMode;
1380 auto cmButton = [&](ColorMode mode, const char* label) {
1381 if (ImGui::RadioButton(label, mode == fColorMode)) {
1382 newMode = mode;
1383 }
1384 };
1385
1386 cmButton(ColorMode::kLegacy, "Legacy 8888");
1387 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1388 "Color Managed 8888 (Nonlinear blending)");
1389 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1390 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1391
1392 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001393 // It isn't safe to switch color mode now (in the middle of painting). We might
1394 // tear down the back-end, etc... Defer this change until the next onIdle.
1395 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001396 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001397 });
Brian Osmana109e392017-02-24 09:49:14 -05001398 }
1399
1400 // Pick from common gamuts:
1401 int primariesIdx = 4; // Default: Custom
1402 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1403 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1404 primariesIdx = i;
1405 break;
1406 }
1407 }
1408
Brian Osmanfdab5762017-11-09 10:27:55 -05001409 // When we're in xform canvas mode, we can alter the transfer function, too
1410 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1411 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1412 }
1413
Brian Osmana109e392017-02-24 09:49:14 -05001414 if (ImGui::Combo("Primaries", &primariesIdx,
1415 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1416 if (primariesIdx >= 0 && primariesIdx <= 3) {
1417 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1418 }
1419 }
1420
1421 // Allow direct editing of gamut
1422 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1423 }
Brian Osman79086b92017-02-10 13:36:16 -05001424 }
Brian Salomon99a33902017-03-07 15:16:34 -05001425 if (paramsChanged) {
1426 fDeferredActions.push_back([=]() {
1427 fWindow->setRequestedDisplayParams(params);
1428 fWindow->inval();
1429 this->updateTitle();
1430 });
1431 }
Brian Osman79086b92017-02-10 13:36:16 -05001432 ImGui::End();
1433 }
1434
Brian Osmanf6877092017-02-13 09:39:57 -05001435 if (fShowZoomWindow && fLastImage) {
1436 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001437 static int zoomFactor = 8;
1438 if (ImGui::Button("<<")) {
1439 zoomFactor = SkTMax(zoomFactor / 2, 4);
1440 }
1441 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1442 if (ImGui::Button(">>")) {
1443 zoomFactor = SkTMin(zoomFactor * 2, 32);
1444 }
Brian Osmanf6877092017-02-13 09:39:57 -05001445
Brian Osmanf6877092017-02-13 09:39:57 -05001446 ImVec2 mousePos = ImGui::GetMousePos();
1447 ImVec2 avail = ImGui::GetContentRegionAvail();
1448
Brian Osmanead517d2017-11-13 15:36:36 -05001449 uint32_t pixel = 0;
1450 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1451 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1452 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001453 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1454 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1455 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001456 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1457 }
1458
Brian Osmand67e5182017-12-08 16:46:09 -05001459 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001460 // Translate so the region of the image that's under the mouse cursor is centered
1461 // in the zoom canvas:
1462 c->scale(zoomFactor, zoomFactor);
1463 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1464 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1465 c->drawImage(this->fLastImage, 0, 0);
1466
1467 SkPaint outline;
1468 outline.setStyle(SkPaint::kStroke_Style);
1469 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1470 });
Brian Osmanf6877092017-02-13 09:39:57 -05001471 }
1472
1473 ImGui::End();
1474 }
Brian Osman79086b92017-02-10 13:36:16 -05001475}
1476
liyuqian2edb0f42016-07-06 14:11:32 -07001477void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001478 for (int i = 0; i < fDeferredActions.count(); ++i) {
1479 fDeferredActions[i]();
1480 }
1481 fDeferredActions.reset();
1482
Brian Osman56a24812017-12-19 11:15:16 -05001483 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001484 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001485 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001486 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001487
Brian Osman79086b92017-02-10 13:36:16 -05001488 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001489 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001490 fWindow->inval();
1491 }
jvanverth9f372462016-04-06 06:08:59 -07001492}
liyuqiane5a6cd92016-05-27 08:52:52 -07001493
1494void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001495 if (!fWindow) {
1496 return;
1497 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001498 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001499 return; // Surface hasn't been created yet.
1500 }
1501
liyuqianb73c24b2016-06-03 08:47:23 -07001502 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001503 Json::Value slideState(Json::objectValue);
1504 slideState[kName] = kSlideStateName;
1505 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001506 if (fAllSlideNames.size() == 0) {
1507 for(auto slide : fSlides) {
1508 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1509 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001510 }
liyuqian1f508fd2016-06-07 06:57:40 -07001511 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001512
liyuqianb73c24b2016-06-03 08:47:23 -07001513 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001514 Json::Value backendState(Json::objectValue);
1515 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001516 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001517 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001518 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001519 backendState[kOptions].append(Json::Value(str));
1520 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001521
csmartdalton578f0642017-02-24 16:04:47 -07001522 // MSAA state
1523 Json::Value msaaState(Json::objectValue);
1524 msaaState[kName] = kMSAAStateName;
1525 msaaState[kValue] = fWindow->sampleCount();
1526 msaaState[kOptions] = Json::Value(Json::arrayValue);
1527 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1528 msaaState[kOptions].append(Json::Value(0));
1529 } else {
1530 for (int msaa : {0, 4, 8, 16}) {
1531 msaaState[kOptions].append(Json::Value(msaa));
1532 }
1533 }
1534
csmartdalton61cd31a2017-02-27 17:00:53 -07001535 // Path renderer state
1536 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1537 Json::Value prState(Json::objectValue);
1538 prState[kName] = kPathRendererStateName;
1539 prState[kValue] = gPathRendererNames[pr];
1540 prState[kOptions] = Json::Value(Json::arrayValue);
1541 const GrContext* ctx = fWindow->getGrContext();
1542 if (!ctx) {
1543 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001544 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001545 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001546 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1547 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1548 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1549 }
1550 if (ctx->caps()->sampleShadingSupport()) {
1551 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1552 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001553 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001554 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1555 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001556 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001557 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001558 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1559 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1560 }
Jim Van Verth83010462017-03-16 08:45:39 -04001561 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001562 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001563 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1564 }
1565
liyuqianb73c24b2016-06-03 08:47:23 -07001566 // Softkey state
1567 Json::Value softkeyState(Json::objectValue);
1568 softkeyState[kName] = kSoftkeyStateName;
1569 softkeyState[kValue] = kSoftkeyHint;
1570 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1571 softkeyState[kOptions].append(kSoftkeyHint);
1572 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1573 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1574 }
1575
liyuqian1f508fd2016-06-07 06:57:40 -07001576 // FPS state
1577 Json::Value fpsState(Json::objectValue);
1578 fpsState[kName] = kFpsStateName;
Brian Osman56a24812017-12-19 11:15:16 -05001579 double animTime = fStatsLayer.getLastTime(fAnimateTimer);
1580 double paintTime = fStatsLayer.getLastTime(fPaintTimer);
1581 double flushTime = fStatsLayer.getLastTime(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001582 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
Brian Osman56a24812017-12-19 11:15:16 -05001583 animTime + paintTime + flushTime,
1584 animTime, paintTime, flushTime).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001585 fpsState[kOptions] = Json::Value(Json::arrayValue);
1586
liyuqiane5a6cd92016-05-27 08:52:52 -07001587 Json::Value state(Json::arrayValue);
1588 state.append(slideState);
1589 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001590 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001591 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001592 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001593 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001594
Brian Osmaneff04b52017-11-21 13:18:02 -05001595 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001596}
1597
1598void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001599 // For those who will add more features to handle the state change in this function:
1600 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1601 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1602 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001603 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001604 for (int i = 0; i < fSlides.count(); ++i) {
1605 if (fSlides[i]->getName().equals(stateValue)) {
1606 this->setCurrentSlide(i);
1607 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001608 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001609 }
Florin Malitaab99c342018-01-16 16:23:03 -05001610
1611 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001612 } else if (stateName.equals(kBackendStateName)) {
1613 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1614 if (stateValue.equals(kBackendTypeStrings[i])) {
1615 if (fBackendType != i) {
1616 fBackendType = (sk_app::Window::BackendType)i;
1617 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001618 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001619 }
1620 break;
1621 }
1622 }
csmartdalton578f0642017-02-24 16:04:47 -07001623 } else if (stateName.equals(kMSAAStateName)) {
1624 DisplayParams params = fWindow->getRequestedDisplayParams();
1625 int sampleCount = atoi(stateValue.c_str());
1626 if (sampleCount != params.fMSAASampleCount) {
1627 params.fMSAASampleCount = sampleCount;
1628 fWindow->setRequestedDisplayParams(params);
1629 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001630 this->updateTitle();
1631 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001632 }
1633 } else if (stateName.equals(kPathRendererStateName)) {
1634 DisplayParams params = fWindow->getRequestedDisplayParams();
1635 for (const auto& pair : gPathRendererNames) {
1636 if (pair.second == stateValue.c_str()) {
1637 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1638 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1639 fWindow->setRequestedDisplayParams(params);
1640 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001641 this->updateTitle();
1642 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001643 }
1644 break;
1645 }
csmartdalton578f0642017-02-24 16:04:47 -07001646 }
liyuqianb73c24b2016-06-03 08:47:23 -07001647 } else if (stateName.equals(kSoftkeyStateName)) {
1648 if (!stateValue.equals(kSoftkeyHint)) {
1649 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001650 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001651 }
liyuqian2edb0f42016-07-06 14:11:32 -07001652 } else if (stateName.equals(kRefreshStateName)) {
1653 // This state is actually NOT in the UI state.
1654 // We use this to allow Android to quickly set bool fRefresh.
1655 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001656 } else {
1657 SkDebugf("Unknown stateName: %s", stateName.c_str());
1658 }
1659}
Brian Osman79086b92017-02-10 13:36:16 -05001660
1661bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001662 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001663}
1664
1665bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001666 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001667 fWindow->inval();
1668 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001669 } else {
1670 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001671 }
Brian Osman79086b92017-02-10 13:36:16 -05001672}