blob: 8aff1cedb0d49dce1f22048631536bf7bfe4ff0c [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"
jvanverth9f372462016-04-06 06:08:59 -070018
csmartdalton61cd31a2017-02-27 17:00:53 -070019#include "GrContext.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070020#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050021#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040022#include "SkColorSpaceXformCanvas.h"
Ben Wagner483c7722018-02-20 17:06:07 -050023#include "SkCommonFlags.h"
Brian Osman2dd96932016-10-18 15:33:53 -040024#include "SkCommandLineFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070025#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040026#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050027#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040028#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050029#include "SkImagePriv.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070030#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050031#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050032#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050033#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040034#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070035#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070036#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050037#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050038#include "SkTestFontMgr.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050039#include "SkThreadedBMPDevice.h"
jvanverth9f372462016-04-06 06:08:59 -070040
Brian Osman79086b92017-02-10 13:36:16 -050041#include "imgui.h"
42
Chris Dalton1a325d22017-07-14 15:17:41 -060043#include "ccpr/GrCoverageCountingPathRenderer.h"
44
csmartdalton578f0642017-02-24 16:04:47 -070045#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070046#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070047
jvanverth34524262016-05-04 13:49:13 -070048using namespace sk_app;
49
csmartdalton61cd31a2017-02-27 17:00:53 -070050static std::map<GpuPathRenderers, std::string> gPathRendererNames;
51
jvanverth9f372462016-04-06 06:08:59 -070052Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070053 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070054}
55
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060056static DEFINE_string(slide, "", "Start on this sample.");
57static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050058
bsalomon6c471f72016-07-26 12:56:32 -070059#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070060# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070061#else
62# define BACKENDS_STR "\"sw\" and \"gl\""
63#endif
64
Brian Osman2dd96932016-10-18 15:33:53 -040065static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070066
Brian Salomonbdecacf2018-02-02 20:32:49 -050067static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070068
Chris Dalton2d18f412018-02-20 13:23:32 -070069DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
70
Brian Osman53136aa2017-07-20 15:43:35 -040071DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050072
Brian Salomon194db172017-08-17 14:37:06 -040073const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070074 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040075#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
76 "ANGLE",
77#endif
jvanverth063ece72016-06-17 09:29:14 -070078#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070079 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070080#endif
csmartdalton578f0642017-02-24 16:04:47 -070081 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070082};
83
bsalomon6c471f72016-07-26 12:56:32 -070084static sk_app::Window::BackendType get_backend_type(const char* str) {
85#ifdef SK_VULKAN
86 if (0 == strcmp(str, "vk")) {
87 return sk_app::Window::kVulkan_BackendType;
88 } else
89#endif
Brian Salomon194db172017-08-17 14:37:06 -040090#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
91 if (0 == strcmp(str, "angle")) {
92 return sk_app::Window::kANGLE_BackendType;
93 } else
94#endif
bsalomon6c471f72016-07-26 12:56:32 -070095 if (0 == strcmp(str, "gl")) {
96 return sk_app::Window::kNativeGL_BackendType;
97 } else if (0 == strcmp(str, "sw")) {
98 return sk_app::Window::kRaster_BackendType;
99 } else {
100 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
101 return sk_app::Window::kRaster_BackendType;
102 }
103}
104
Brian Osmana109e392017-02-24 09:49:14 -0500105static SkColorSpacePrimaries gSrgbPrimaries = {
106 0.64f, 0.33f,
107 0.30f, 0.60f,
108 0.15f, 0.06f,
109 0.3127f, 0.3290f };
110
111static SkColorSpacePrimaries gAdobePrimaries = {
112 0.64f, 0.33f,
113 0.21f, 0.71f,
114 0.15f, 0.06f,
115 0.3127f, 0.3290f };
116
117static SkColorSpacePrimaries gP3Primaries = {
118 0.680f, 0.320f,
119 0.265f, 0.690f,
120 0.150f, 0.060f,
121 0.3127f, 0.3290f };
122
123static SkColorSpacePrimaries gRec2020Primaries = {
124 0.708f, 0.292f,
125 0.170f, 0.797f,
126 0.131f, 0.046f,
127 0.3127f, 0.3290f };
128
129struct NamedPrimaries {
130 const char* fName;
131 SkColorSpacePrimaries* fPrimaries;
132} gNamedPrimaries[] = {
133 { "sRGB", &gSrgbPrimaries },
134 { "AdobeRGB", &gAdobePrimaries },
135 { "P3", &gP3Primaries },
136 { "Rec. 2020", &gRec2020Primaries },
137};
138
139static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
140 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
141}
142
Brian Osman70d2f432017-11-08 09:54:10 -0500143static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
144 // In raster mode, we still use GL for the window.
145 // This lets us render the GUI faster (and correct).
146 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
147}
148
liyuqiane5a6cd92016-05-27 08:52:52 -0700149const char* kName = "name";
150const char* kValue = "value";
151const char* kOptions = "options";
152const char* kSlideStateName = "Slide";
153const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700154const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700155const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700156const char* kSoftkeyStateName = "Softkey";
157const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700158const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700159const char* kON = "ON";
160const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700161const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700162
jvanverth34524262016-05-04 13:49:13 -0700163Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500164 : fCurrentSlide(-1)
165 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500166 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500167 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500168 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500169 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500170 , fShowZoomWindow(false)
171 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700172 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500173 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500174 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500175 // Our UI can only tweak gamma (currently), so start out gamma-only
176 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700177 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400178 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500179 , fTileCnt(0)
180 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700181{
Greg Daniel285db442016-10-14 09:12:53 -0400182 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700183
Brian Osmanf09e35e2017-12-15 14:48:09 -0500184 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
185 gPathRendererNames[GpuPathRenderers::kDefault] =
186 "Default Ganesh Behavior (best path renderer, not including CCPR)";
187 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
188 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
189 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
190 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
191 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
192 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700193
jvanverth2bb3b6d2016-04-08 07:24:09 -0700194 SkDebugf("Command line arguments: ");
195 for (int i = 1; i < argc; ++i) {
196 SkDebugf("%s ", argv[i]);
197 }
198 SkDebugf("\n");
199
200 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500201#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400202 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500203#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700204
Ben Wagner483c7722018-02-20 17:06:07 -0500205 if (!FLAGS_nativeFonts) {
206 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
207 }
208
Brian Osmanbc8150f2017-07-24 11:38:01 -0400209 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400210 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400211
bsalomon6c471f72016-07-26 12:56:32 -0700212 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700213 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700214
csmartdalton578f0642017-02-24 16:04:47 -0700215 DisplayParams displayParams;
216 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700217 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700218 fWindow->setRequestedDisplayParams(displayParams);
219
Brian Osman56a24812017-12-19 11:15:16 -0500220 // Configure timers
221 fStatsLayer.setActive(false);
222 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
223 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
224 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
225
jvanverth9f372462016-04-06 06:08:59 -0700226 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700227 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500228 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500229 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500230 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700231
brianosman622c8d52016-05-10 06:50:49 -0700232 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500233 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
234 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
235 fWindow->inval();
236 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500237 // Command to jump directly to the slide picker and give it focus
238 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
239 this->fShowImGuiDebugWindow = true;
240 this->fShowSlidePicker = true;
241 fWindow->inval();
242 });
243 // Alias that to Backspace, to match SampleApp
244 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
245 this->fShowImGuiDebugWindow = true;
246 this->fShowSlidePicker = true;
247 fWindow->inval();
248 });
Brian Osman79086b92017-02-10 13:36:16 -0500249 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
250 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
251 fWindow->inval();
252 });
Brian Osmanf6877092017-02-13 09:39:57 -0500253 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
254 this->fShowZoomWindow = !this->fShowZoomWindow;
255 fWindow->inval();
256 });
brianosman622c8d52016-05-10 06:50:49 -0700257 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500258 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700259 fWindow->inval();
260 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400261 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500262 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400263 this->updateTitle();
264 fWindow->inval();
265 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500266 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500267 switch (fColorMode) {
268 case ColorMode::kLegacy:
269 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
270 break;
271 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
272 this->setColorMode(ColorMode::kColorManagedSRGB8888);
273 break;
274 case ColorMode::kColorManagedSRGB8888:
275 this->setColorMode(ColorMode::kColorManagedLinearF16);
276 break;
277 case ColorMode::kColorManagedLinearF16:
278 this->setColorMode(ColorMode::kLegacy);
279 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500280 }
brianosman622c8d52016-05-10 06:50:49 -0700281 });
282 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500283 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700284 });
285 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500286 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700287 });
288 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
289 this->changeZoomLevel(1.f / 32.f);
290 fWindow->inval();
291 });
292 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
293 this->changeZoomLevel(-1.f / 32.f);
294 fWindow->inval();
295 });
jvanverthaf236b52016-05-20 06:01:06 -0700296 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400297 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
298 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500299 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400300#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
301 if (newBackend == sk_app::Window::kVulkan_BackendType) {
302 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
303 sk_app::Window::kBackendTypeCount);
304 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
305 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500306 }
307#endif
Brian Osman621491e2017-02-28 15:45:01 -0500308 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700309 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500310 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
311 fTileCnt++;
312 if (fThreadCnt == 0) {
313 this->resetExecutor();
314 }
315 this->updateTitle();
316 fWindow->inval();
317 });
318 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
319 fTileCnt = SkTMax(0, fTileCnt - 1);
320 if (fThreadCnt == 0) {
321 this->resetExecutor();
322 }
323 this->updateTitle();
324 fWindow->inval();
325 });
326 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
327 if (fTileCnt == 0) {
328 return;
329 }
330 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
331 this->resetExecutor();
332 this->updateTitle();
333 fWindow->inval();
334 });
335 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
336 if (fTileCnt == 0) {
337 return;
338 }
339 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
340 this->resetExecutor();
341 this->updateTitle();
342 fWindow->inval();
343 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500344 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
345 fSaveToSKP = true;
346 fWindow->inval();
347 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500348 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
349 if (!fPaintOverrides.fHinting) {
350 fPaintOverrides.fHinting = true;
351 fPaint.setHinting(SkPaint::kNo_Hinting);
352 } else {
353 switch (fPaint.getHinting()) {
354 case SkPaint::kNo_Hinting:
355 fPaint.setHinting(SkPaint::kSlight_Hinting);
356 break;
357 case SkPaint::kSlight_Hinting:
358 fPaint.setHinting(SkPaint::kNormal_Hinting);
359 break;
360 case SkPaint::kNormal_Hinting:
361 fPaint.setHinting(SkPaint::kFull_Hinting);
362 break;
363 case SkPaint::kFull_Hinting:
364 fPaint.setHinting(SkPaint::kNo_Hinting);
365 fPaintOverrides.fHinting = false;
366 break;
367 }
368 }
369 this->updateTitle();
370 fWindow->inval();
371 });
372 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
373 if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) {
374 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
375 fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag;
376 fPaint.setAntiAlias(false);
377 fPaintOverrides.fOriginalSkUseAnalyticAA = gSkUseAnalyticAA;
378 fPaintOverrides.fOriginalSkForceAnalyticAA = gSkForceAnalyticAA;
379 fPaintOverrides.fOriginalSkUseDeltaAA = gSkUseDeltaAA;
380 fPaintOverrides.fOriginalSkForceDeltaAA = gSkForceDeltaAA;
381 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
382 gSkUseDeltaAA = gSkForceDeltaAA = false;
383 } else {
384 fPaint.setAntiAlias(true);
385 switch (fPaintOverrides.fAntiAlias) {
386 case SkPaintFields::AntiAliasState::Alias:
387 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
388 break;
389 case SkPaintFields::AntiAliasState::Normal:
390 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
391 gSkUseDeltaAA = gSkForceDeltaAA = false;
392 gSkUseAnalyticAA = true;
393 break;
394 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
395 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
396 gSkForceAnalyticAA = true;
397 break;
398 case SkPaintFields::AntiAliasState::AnalyticAAForced:
399 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
400 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
401 gSkUseDeltaAA = true;
402 break;
403 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
404 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
405 gSkForceDeltaAA = true;
406 break;
407 case SkPaintFields::AntiAliasState::DeltaAAForced:
408 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
409 fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag;
410 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
411 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
412 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
413 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
414 break;
415 }
416 }
417 this->updateTitle();
418 fWindow->inval();
419 });
420 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
421 if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) {
422 fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag;
423 fPaint.setLCDRenderText(false);
424 } else {
425 if (!fPaint.isLCDRenderText()) {
426 fPaint.setLCDRenderText(true);
427 } else {
428 fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag;
429 }
430 }
431 this->updateTitle();
432 fWindow->inval();
433 });
434 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
435 if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) {
436 fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag;
437 fPaint.setSubpixelText(false);
438 } else {
439 if (!fPaint.isSubpixelText()) {
440 fPaint.setSubpixelText(true);
441 } else {
442 fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag;
443 }
444 }
445 this->updateTitle();
446 fWindow->inval();
447 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500448
jvanverth2bb3b6d2016-04-08 07:24:09 -0700449 // set up slides
450 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500451 if (FLAGS_list) {
452 this->listNames();
453 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700454
djsollen12d62a72016-04-21 07:59:44 -0700455 fAnimTimer.run();
456
Hal Canaryc465d132017-12-08 10:21:31 -0500457 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500458 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400459 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500460 }
461 fImGuiGamutPaint.setColor(SK_ColorWHITE);
462 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
463
Brian Osman70d2f432017-11-08 09:54:10 -0500464 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500465 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700466}
467
jvanverth34524262016-05-04 13:49:13 -0700468void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700469 fAllSlideNames = Json::Value(Json::arrayValue);
470
Chris Dalton2d18f412018-02-20 13:23:32 -0700471 // Bisect slide.
472 if (!FLAGS_bisect.isEmpty()) {
473 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
474 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
475 if (FLAGS_bisect.count() >= 2) {
476 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
477 bisect->onChar(*ch);
478 }
479 }
480 fSlides.push_back(std::move(bisect));
481 }
482 }
483
484 // GMs
485 int firstGM = fSlides.count();
jvanverth2bb3b6d2016-04-08 07:24:09 -0700486 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
487 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400488 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700489
490 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
491 sk_sp<Slide> slide(new GMSlide(gm.release()));
Chris Dalton2d18f412018-02-20 13:23:32 -0700492 fSlides.push_back(std::move(slide));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700493 }
494
495 gms = gms->next();
496 }
Chris Dalton2d18f412018-02-20 13:23:32 -0700497 // reverse gms
498 int numGMs = fSlides.count() - firstGM;
499 for (int i = 0; i < numGMs/2; ++i) {
500 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
jvanverth2bb3b6d2016-04-08 07:24:09 -0700501 }
502
jvanverthc7027ab2016-06-16 09:52:35 -0700503 // samples
504 const SkViewRegister* reg = SkViewRegister::Head();
505 while (reg) {
506 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700507 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
508 fSlides.push_back(slide);
509 }
jvanverthc7027ab2016-06-16 09:52:35 -0700510 reg = reg->next();
511 }
512
jvanverth2bb3b6d2016-04-08 07:24:09 -0700513 // SKPs
514 for (int i = 0; i < FLAGS_skps.count(); i++) {
515 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700516 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
517 continue;
518 }
519
jvanverth2bb3b6d2016-04-08 07:24:09 -0700520 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700521 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700522 if (slide) {
523 fSlides.push_back(slide);
524 }
525 } else {
526 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700527 SkString skpName;
528 while (it.next(&skpName)) {
529 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
530 continue;
531 }
532
533 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
534 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700535 if (slide) {
536 fSlides.push_back(slide);
537 }
538 }
539 }
540 }
liyuqian6f163d22016-06-13 12:26:45 -0700541
542 // JPGs
543 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
544 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
545 SkString jpgName;
546 while (it.next(&jpgName)) {
brianosmane1d20072016-07-12 09:07:33 -0700547 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) {
548 continue;
549 }
550
liyuqian6f163d22016-06-13 12:26:45 -0700551 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
552 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
553 if (slide) {
554 fSlides.push_back(slide);
555 }
556 }
557 }
Florin Malita094ccde2017-12-30 12:27:00 -0500558
559 // JSONs
560 for (const auto& json : FLAGS_jsons) {
Florin Malita76a076b2018-02-15 18:40:48 -0500561 SkTArray<sk_sp<Slide>, true> dirSlides;
562
Florin Malita094ccde2017-12-30 12:27:00 -0500563 SkOSFile::Iter it(json.c_str(), ".json");
564 SkString jsonName;
565 while (it.next(&jsonName)) {
566 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jsonName.c_str())) {
567 continue;
568 }
Florin Malita76a076b2018-02-15 18:40:48 -0500569 auto slide = sk_make_sp<SkottieSlide>(jsonName, SkOSPath::Join(json.c_str(),
570 jsonName.c_str()));
571 dirSlides.push_back(slide);
572 fSlides.push_back(std::move(slide));
573 }
574
575 if (!dirSlides.empty()) {
576 fSlides.push_back(sk_make_sp<SlideDir>(SkStringPrintf("skottie-dir[%s]", json.c_str()),
577 std::move(dirSlides)));
Florin Malita094ccde2017-12-30 12:27:00 -0500578 }
579 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700580}
581
582
jvanverth34524262016-05-04 13:49:13 -0700583Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700584 fWindow->detach();
585 delete fWindow;
586}
587
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500588struct SkPaintTitleUpdater {
589 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
590 void append(const char* s) {
591 if (fCount == 0) {
592 fTitle->append(" {");
593 } else {
594 fTitle->append(", ");
595 }
596 fTitle->append(s);
597 ++fCount;
598 }
599 void done() {
600 if (fCount > 0) {
601 fTitle->append("}");
602 }
603 }
604 SkString* fTitle;
605 int fCount;
606};
607
brianosman05de2162016-05-06 13:28:57 -0700608void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700609 if (!fWindow) {
610 return;
611 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500612 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700613 return; // Surface hasn't been created yet.
614 }
615
jvanverth34524262016-05-04 13:49:13 -0700616 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700617 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700618
Yuqian Li399b3c22017-08-03 11:08:15 -0400619 if (gSkUseDeltaAA) {
620 if (gSkForceDeltaAA) {
621 title.append(" <FDAA>");
622 } else {
623 title.append(" <DAA>");
624 }
625 } else if (gSkUseAnalyticAA) {
626 if (gSkForceAnalyticAA) {
627 title.append(" <FAAA>");
628 } else {
629 title.append(" <AAA>");
630 }
631 }
632
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500633 SkPaintTitleUpdater paintTitle(&title);
634 if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) {
635 if (fPaint.isAntiAlias()) {
636 paintTitle.append("Antialias");
637 } else {
638 paintTitle.append("Alias");
639 }
640 }
641 if (fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag) {
642 if (fPaint.isLCDRenderText()) {
643 paintTitle.append("LCD");
644 } else {
645 paintTitle.append("lcd");
646 }
647 }
648 if (fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag) {
649 if (fPaint.isSubpixelText()) {
650 paintTitle.append("Subpixel Glyphs");
651 } else {
652 paintTitle.append("Pixel Glyphs");
653 }
654 }
655 if (fPaintOverrides.fHinting) {
656 switch (fPaint.getHinting()) {
657 case SkPaint::kNo_Hinting:
658 paintTitle.append("No Hinting");
659 break;
660 case SkPaint::kSlight_Hinting:
661 paintTitle.append("Slight Hinting");
662 break;
663 case SkPaint::kNormal_Hinting:
664 paintTitle.append("Normal Hinting");
665 break;
666 case SkPaint::kFull_Hinting:
667 paintTitle.append("Full Hinting");
668 break;
669 }
670 }
671 paintTitle.done();
672
Yuqian Lib2ba6642017-11-22 12:07:41 -0500673 if (fTileCnt > 0) {
674 title.appendf(" T%d", fTileCnt);
675 if (fThreadCnt > 0) {
676 title.appendf("/%d", fThreadCnt);
677 }
678 }
679
Brian Osman92004802017-03-06 11:47:26 -0500680 switch (fColorMode) {
681 case ColorMode::kLegacy:
682 title.append(" Legacy 8888");
683 break;
684 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
685 title.append(" ColorManaged 8888 (Nonlinear blending)");
686 break;
687 case ColorMode::kColorManagedSRGB8888:
688 title.append(" ColorManaged 8888");
689 break;
690 case ColorMode::kColorManagedLinearF16:
691 title.append(" ColorManaged F16");
692 break;
693 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500694
Brian Osman92004802017-03-06 11:47:26 -0500695 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500696 int curPrimaries = -1;
697 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
698 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
699 curPrimaries = i;
700 break;
701 }
702 }
703 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500704
705 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
706 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
707 }
brianosman05de2162016-05-06 13:28:57 -0700708 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500709
csmartdalton578f0642017-02-24 16:04:47 -0700710 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700711 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500712 int msaa = fWindow->sampleCount();
713 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700714 title.appendf(" MSAA: %i", msaa);
715 }
716 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700717
718 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400719 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700720 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
721 }
722
brianosman05de2162016-05-06 13:28:57 -0700723 fWindow->setTitle(title.c_str());
724}
725
Florin Malitaab99c342018-01-16 16:23:03 -0500726int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500727
728 if (!FLAGS_slide.isEmpty()) {
729 int count = fSlides.count();
730 for (int i = 0; i < count; i++) {
731 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500732 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500733 }
734 }
735
736 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
737 this->listNames();
738 }
739
Florin Malitaab99c342018-01-16 16:23:03 -0500740 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500741}
742
Florin Malitaab99c342018-01-16 16:23:03 -0500743void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500744 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500745 for (const auto& slide : fSlides) {
746 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500747 }
748}
749
Florin Malitaab99c342018-01-16 16:23:03 -0500750void Viewer::setCurrentSlide(int slide) {
751 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700752
Florin Malitaab99c342018-01-16 16:23:03 -0500753 if (slide == fCurrentSlide) {
754 return;
755 }
756
757 if (fCurrentSlide >= 0) {
758 fSlides[fCurrentSlide]->unload();
759 }
760
761 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
762 SkIntToScalar(fWindow->height()));
763 fCurrentSlide = slide;
764 this->setupCurrentSlide();
765}
766
767void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500768 if (fCurrentSlide >= 0) {
769 // prepare dimensions for image slides
770 fGesture.resetTouchState();
771 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700772
Jim Van Verth0848fb02018-01-22 13:39:30 -0500773 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
774 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
775 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400776
Jim Van Verth0848fb02018-01-22 13:39:30 -0500777 // Start with a matrix that scales the slide to the available screen space
778 if (fWindow->scaleContentToFit()) {
779 if (windowRect.width() > 0 && windowRect.height() > 0) {
780 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
781 }
liyuqiane46e4f02016-05-20 07:32:19 -0700782 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500783
784 // Prevent the user from dragging content so far outside the window they can't find it again
785 fGesture.setTransLimit(slideBounds, windowRect, fDefaultMatrix);
786
787 this->updateTitle();
788 this->updateUIState();
789
790 fStatsLayer.resetMeasurements();
791
792 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700793 }
jvanverthc265a922016-04-08 12:51:45 -0700794}
795
796#define MAX_ZOOM_LEVEL 8
797#define MIN_ZOOM_LEVEL -8
798
jvanverth34524262016-05-04 13:49:13 -0700799void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700800 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400801 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
jvanverthc265a922016-04-08 12:51:45 -0700802}
803
liyuqiand3cdbca2016-05-17 12:44:20 -0700804SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700805 SkMatrix m;
jvanverthc265a922016-04-08 12:51:45 -0700806
Brian Osman42bb6ac2017-06-05 08:46:04 -0400807 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
808 : SK_Scalar1 + fZoomLevel;
809 m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700810 m.preConcat(fGesture.globalM());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400811 m.preConcat(fDefaultMatrix);
812 m.preScale(zoomScale, zoomScale);
jvanverthc265a922016-04-08 12:51:45 -0700813
liyuqiand3cdbca2016-05-17 12:44:20 -0700814 return m;
jvanverthc265a922016-04-08 12:51:45 -0700815}
816
Brian Osman621491e2017-02-28 15:45:01 -0500817void Viewer::setBackend(sk_app::Window::BackendType backendType) {
818 fBackendType = backendType;
819
820 fWindow->detach();
821
Brian Osman70d2f432017-11-08 09:54:10 -0500822#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400823 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
824 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500825 DisplayParams params = fWindow->getRequestedDisplayParams();
826 delete fWindow;
827 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500828
Brian Osman70d2f432017-11-08 09:54:10 -0500829 // re-register callbacks
830 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500831 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500832 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500833 fWindow->pushLayer(&fImGuiLayer);
834
Brian Osman70d2f432017-11-08 09:54:10 -0500835 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
836 // will still include our correct sample count. But the re-created fWindow will lose that
837 // information. On Windows, we need to re-create the window when changing sample count,
838 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
839 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
840 // as if we had never changed windows at all).
841 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500842#endif
843
Brian Osman70d2f432017-11-08 09:54:10 -0500844 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500845}
846
Brian Osman92004802017-03-06 11:47:26 -0500847void Viewer::setColorMode(ColorMode colorMode) {
848 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700849
Brian Osmanf750fbc2017-02-08 10:47:28 -0500850 // 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 -0400851 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700852 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500853 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400854 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
855 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500856 if (wasInLegacy != wantLegacy) {
857 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700858 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500859 }
860
861 this->updateTitle();
862 fWindow->inval();
863}
864
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500865class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
866public:
867 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields)
868 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields)
869 { }
870 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
871 if (fPaintOverrides->fHinting) {
872 paint->writable()->setHinting(fPaint->getHinting());
873 }
874 if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) {
875 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
876 }
877 if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) {
878 paint->writable()->setLCDRenderText(fPaint->isLCDRenderText());
879 }
880 if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) {
881 paint->writable()->setSubpixelText(fPaint->isSubpixelText());
882 }
883 return true;
884 }
885 SkPaint* fPaint;
886 Viewer::SkPaintFields* fPaintOverrides;
887};
888
Brian Osmanf750fbc2017-02-08 10:47:28 -0500889void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500890 SkAutoCanvasRestore autorestore(canvas, false);
891
Brian Osmanf750fbc2017-02-08 10:47:28 -0500892 // By default, we render directly into the window's surface/canvas
893 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500894 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700895
Brian Osmane0d4fba2017-03-15 10:24:55 -0400896 // If we're in any of the color managed modes, construct the color space we're going to use
897 sk_sp<SkColorSpace> cs = nullptr;
898 if (ColorMode::kLegacy != fColorMode) {
899 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
900 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400901 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400902 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500903 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
904 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
905 } else {
906 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
907 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400908 }
909
Brian Osman3ac99cf2017-12-01 11:23:53 -0500910 if (fSaveToSKP) {
911 SkPictureRecorder recorder;
912 SkCanvas* recorderCanvas = recorder.beginRecording(
913 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
914 // In xform-canvas mode, record the transformed output
915 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
916 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
917 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
918 recorderCanvas = xformCanvas.get();
919 }
920 fSlides[fCurrentSlide]->draw(recorderCanvas);
921 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
922 SkFILEWStream stream("sample_app.skp");
923 picture->serialize(&stream);
924 fSaveToSKP = false;
925 }
926
Brian Osmane0d4fba2017-03-15 10:24:55 -0400927 // 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 -0500928 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
929 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500930 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -0500931 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
932 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -0500933 if (Window::kRaster_BackendType == fBackendType ||
934 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500935 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400936 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
937 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
938
Brian Osman92004802017-03-06 11:47:26 -0500939 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
940 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400941 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
942 // with a special canvas (below) that has the color space attached
943 sk_sp<SkColorSpace> offscreenColorSpace =
944 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500945 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400946 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500947 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
948 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -0500949 SkPixmap offscreenPixmap;
950 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
951 SkBitmap offscreenBitmap;
952 offscreenBitmap.installPixels(offscreenPixmap);
953 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
954 fThreadCnt, fExecutor.get()));
955 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
956 slideCanvas = threadedCanvas.get();
957 } else {
958 slideCanvas = offscreenSurface->getCanvas();
959 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500960 }
961
Brian Osmane0d4fba2017-03-15 10:24:55 -0400962 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
963 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
964 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
965 slideCanvas = xformCanvas.get();
966 }
967
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500968 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500969 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500970 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500971 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -0500972 fStatsLayer.beginTiming(fPaintTimer);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500973 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides);
974 fSlides[fCurrentSlide]->draw(&filterCanvas);
Brian Osman56a24812017-12-19 11:15:16 -0500975 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500976 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500977
978 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -0500979 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -0500980 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -0500981 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500982
983 // If we rendered offscreen, snap an image and push the results to the window's canvas
984 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500985 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500986
987 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400988 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500989 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400990 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500991 SkPaint paint;
992 paint.setBlendMode(SkBlendMode::kSrc);
993 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -0700994 }
liyuqian6f163d22016-06-13 12:26:45 -0700995}
996
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700997void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -0500998 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700999 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001000}
Jim Van Verth6f449692017-02-14 15:16:46 -05001001
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001002void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001003 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001004
brianosman622c8d52016-05-10 06:50:49 -07001005 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001006
Brian Osmand67e5182017-12-08 16:46:09 -05001007 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -05001008
Brian Osman1df161a2017-02-09 12:10:20 -05001009 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -04001010 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001011}
1012
Florin Malitacefc1b92018-02-19 21:43:47 -05001013SkPoint Viewer::mapEvent(float x, float y) {
1014 const auto m = this->computeMatrix();
1015 SkMatrix inv;
1016
1017 SkAssertResult(m.invert(&inv));
1018
1019 return inv.mapXY(x, y);
1020}
1021
jvanverth814e38d2016-06-06 08:48:47 -07001022bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001023 if (GestureDevice::kMouse == fGestureDevice) {
1024 return false;
1025 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001026
1027 const auto slidePt = this->mapEvent(x, y);
1028 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1029 fWindow->inval();
1030 return true;
1031 }
1032
liyuqiand3cdbca2016-05-17 12:44:20 -07001033 void* castedOwner = reinterpret_cast<void*>(owner);
1034 switch (state) {
1035 case Window::kUp_InputState: {
1036 fGesture.touchEnd(castedOwner);
1037 break;
1038 }
1039 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001040 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001041 break;
1042 }
1043 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001044 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001045 break;
1046 }
1047 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001048 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001049 fWindow->inval();
1050 return true;
1051}
1052
Brian Osman80fc07e2017-12-08 16:45:43 -05001053bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001054 if (GestureDevice::kTouch == fGestureDevice) {
1055 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001056 }
Brian Osman16c81a12017-12-20 11:58:34 -05001057
Florin Malitacefc1b92018-02-19 21:43:47 -05001058 const auto slidePt = this->mapEvent(x, y);
1059 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1060 fWindow->inval();
1061 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001062 }
1063
1064 switch (state) {
1065 case Window::kUp_InputState: {
1066 fGesture.touchEnd(nullptr);
1067 break;
1068 }
1069 case Window::kDown_InputState: {
1070 fGesture.touchBegin(nullptr, x, y);
1071 break;
1072 }
1073 case Window::kMove_InputState: {
1074 fGesture.touchMoved(nullptr, x, y);
1075 break;
1076 }
1077 }
1078 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1079
1080 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1081 fWindow->inval();
1082 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001083 return true;
1084}
1085
Brian Osmana109e392017-02-24 09:49:14 -05001086static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1087 const ImVec2& pos, const ImVec2& size) {
1088 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1089 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1090
1091 // Invisible 10x10 button
1092 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1093 ImGui::InvisibleButton(label, ImVec2(10, 10));
1094
1095 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1096 ImGuiIO& io = ImGui::GetIO();
1097 // Normalized mouse position, relative to our gamut box
1098 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1099 // Clamp to edge of box, convert back to primary scale
1100 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1101 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1102 }
1103
1104 if (ImGui::IsItemHovered()) {
1105 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1106 }
1107
1108 // Return screen coordinates for the caller. We could just return center here, but we'd have
1109 // one frame of lag during drag.
1110 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1111}
1112
1113static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1114 ImDrawList* drawList = ImGui::GetWindowDrawList();
1115
1116 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1117 // width, and scale the height to maintain aspect ratio.
1118 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1119 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1120 ImVec2 pos = ImGui::GetCursorScreenPos();
1121
1122 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1123 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1124 // Magic numbers are pixel locations of the origin and upper-right corner.
1125 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1126 ImVec2(242, 61), ImVec2(1897, 1922));
1127 ImVec2 endPos = ImGui::GetCursorPos();
1128
1129 // Primary markers
1130 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1131 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1132 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1133 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1134
1135 // Gamut triangle
1136 drawList->AddCircle(r, 5.0f, 0xFF000040);
1137 drawList->AddCircle(g, 5.0f, 0xFF004000);
1138 drawList->AddCircle(b, 5.0f, 0xFF400000);
1139 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1140 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1141
1142 // Re-position cursor immediate after the diagram for subsequent controls
1143 ImGui::SetCursorPos(endPos);
1144}
1145
Brian Osmand67e5182017-12-08 16:46:09 -05001146void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001147 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1148 if (fShowImGuiTestWindow) {
1149 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1150 }
1151
1152 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001153 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1154 // always visible, we can end up in a layout feedback loop.
1155 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001156 DisplayParams params = fWindow->getRequestedDisplayParams();
1157 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001158 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1159 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001160 if (ImGui::CollapsingHeader("Backend")) {
1161 int newBackend = static_cast<int>(fBackendType);
1162 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1163 ImGui::SameLine();
1164 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001165#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1166 ImGui::SameLine();
1167 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1168#endif
Brian Osman621491e2017-02-28 15:45:01 -05001169#if defined(SK_VULKAN)
1170 ImGui::SameLine();
1171 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1172#endif
1173 if (newBackend != fBackendType) {
1174 fDeferredActions.push_back([=]() {
1175 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1176 });
1177 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001178
Brian Salomon99a33902017-03-07 15:16:34 -05001179 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001180 bool* wire = &params.fGrContextOptions.fWireframeMode;
1181 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1182 paramsChanged = true;
1183 }
Brian Salomon99a33902017-03-07 15:16:34 -05001184
Brian Osman28b12522017-03-08 17:10:24 -05001185 if (ctx) {
1186 int sampleCount = fWindow->sampleCount();
1187 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001188 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001189 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1190 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1191 ImGui::RadioButton("16", &sampleCount, 16);
1192
1193 if (sampleCount != params.fMSAASampleCount) {
1194 params.fMSAASampleCount = sampleCount;
1195 paramsChanged = true;
1196 }
1197 }
1198
Brian Osman8a9de3d2017-03-01 14:59:05 -05001199 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001200 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001201 auto prButton = [&](GpuPathRenderers x) {
1202 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001203 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1204 params.fGrContextOptions.fGpuPathRenderers = x;
1205 paramsChanged = true;
1206 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001207 }
1208 };
1209
1210 if (!ctx) {
1211 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001212 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001213 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001214 prButton(GpuPathRenderers::kAll);
1215 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1216 prButton(GpuPathRenderers::kStencilAndCover);
1217 }
1218 if (ctx->caps()->sampleShadingSupport()) {
1219 prButton(GpuPathRenderers::kMSAA);
1220 }
1221 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001222 prButton(GpuPathRenderers::kNone);
1223 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001224 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001225 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001226 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1227 prButton(GpuPathRenderers::kCoverageCounting);
1228 }
Jim Van Verth83010462017-03-16 08:45:39 -04001229 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001230 prButton(GpuPathRenderers::kTessellating);
1231 prButton(GpuPathRenderers::kNone);
1232 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001233 ImGui::TreePop();
1234 }
Brian Osman621491e2017-02-28 15:45:01 -05001235 }
1236
Brian Osmanfce09c52017-11-14 15:32:20 -05001237 if (fShowSlidePicker) {
1238 ImGui::SetNextTreeNodeOpen(true);
1239 }
1240
Brian Osman79086b92017-02-10 13:36:16 -05001241 if (ImGui::CollapsingHeader("Slide")) {
1242 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001243 static ImVector<const char*> filteredSlideNames;
1244 static ImVector<int> filteredSlideIndices;
1245
Brian Osmanfce09c52017-11-14 15:32:20 -05001246 if (fShowSlidePicker) {
1247 ImGui::SetKeyboardFocusHere();
1248 fShowSlidePicker = false;
1249 }
1250
Brian Osman79086b92017-02-10 13:36:16 -05001251 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001252 filteredSlideNames.clear();
1253 filteredSlideIndices.clear();
1254 int filteredIndex = 0;
1255 for (int i = 0; i < fSlides.count(); ++i) {
1256 const char* slideName = fSlides[i]->getName().c_str();
1257 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1258 if (i == fCurrentSlide) {
1259 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001260 }
Brian Osmanf479e422017-11-08 13:11:36 -05001261 filteredSlideNames.push_back(slideName);
1262 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001263 }
Brian Osman79086b92017-02-10 13:36:16 -05001264 }
Brian Osmanf479e422017-11-08 13:11:36 -05001265
Brian Osmanf479e422017-11-08 13:11:36 -05001266 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1267 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001268 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001269 }
1270 }
Brian Osmana109e392017-02-24 09:49:14 -05001271
1272 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001273 ColorMode newMode = fColorMode;
1274 auto cmButton = [&](ColorMode mode, const char* label) {
1275 if (ImGui::RadioButton(label, mode == fColorMode)) {
1276 newMode = mode;
1277 }
1278 };
1279
1280 cmButton(ColorMode::kLegacy, "Legacy 8888");
1281 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1282 "Color Managed 8888 (Nonlinear blending)");
1283 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1284 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1285
1286 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001287 // It isn't safe to switch color mode now (in the middle of painting). We might
1288 // tear down the back-end, etc... Defer this change until the next onIdle.
1289 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001290 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001291 });
Brian Osmana109e392017-02-24 09:49:14 -05001292 }
1293
1294 // Pick from common gamuts:
1295 int primariesIdx = 4; // Default: Custom
1296 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1297 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1298 primariesIdx = i;
1299 break;
1300 }
1301 }
1302
Brian Osmanfdab5762017-11-09 10:27:55 -05001303 // When we're in xform canvas mode, we can alter the transfer function, too
1304 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1305 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1306 }
1307
Brian Osmana109e392017-02-24 09:49:14 -05001308 if (ImGui::Combo("Primaries", &primariesIdx,
1309 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1310 if (primariesIdx >= 0 && primariesIdx <= 3) {
1311 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1312 }
1313 }
1314
1315 // Allow direct editing of gamut
1316 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1317 }
Brian Osman79086b92017-02-10 13:36:16 -05001318 }
Brian Salomon99a33902017-03-07 15:16:34 -05001319 if (paramsChanged) {
1320 fDeferredActions.push_back([=]() {
1321 fWindow->setRequestedDisplayParams(params);
1322 fWindow->inval();
1323 this->updateTitle();
1324 });
1325 }
Brian Osman79086b92017-02-10 13:36:16 -05001326 ImGui::End();
1327 }
1328
Brian Osmanf6877092017-02-13 09:39:57 -05001329 if (fShowZoomWindow && fLastImage) {
1330 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001331 static int zoomFactor = 8;
1332 if (ImGui::Button("<<")) {
1333 zoomFactor = SkTMax(zoomFactor / 2, 4);
1334 }
1335 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1336 if (ImGui::Button(">>")) {
1337 zoomFactor = SkTMin(zoomFactor * 2, 32);
1338 }
Brian Osmanf6877092017-02-13 09:39:57 -05001339
Brian Osmanf6877092017-02-13 09:39:57 -05001340 ImVec2 mousePos = ImGui::GetMousePos();
1341 ImVec2 avail = ImGui::GetContentRegionAvail();
1342
Brian Osmanead517d2017-11-13 15:36:36 -05001343 uint32_t pixel = 0;
1344 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1345 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1346 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001347 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1348 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1349 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001350 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1351 }
1352
Brian Osmand67e5182017-12-08 16:46:09 -05001353 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001354 // Translate so the region of the image that's under the mouse cursor is centered
1355 // in the zoom canvas:
1356 c->scale(zoomFactor, zoomFactor);
1357 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1358 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1359 c->drawImage(this->fLastImage, 0, 0);
1360
1361 SkPaint outline;
1362 outline.setStyle(SkPaint::kStroke_Style);
1363 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1364 });
Brian Osmanf6877092017-02-13 09:39:57 -05001365 }
1366
1367 ImGui::End();
1368 }
Brian Osman79086b92017-02-10 13:36:16 -05001369}
1370
liyuqian2edb0f42016-07-06 14:11:32 -07001371void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001372 for (int i = 0; i < fDeferredActions.count(); ++i) {
1373 fDeferredActions[i]();
1374 }
1375 fDeferredActions.reset();
1376
Brian Osman56a24812017-12-19 11:15:16 -05001377 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001378 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001379 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001380 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001381
Brian Osman79086b92017-02-10 13:36:16 -05001382 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001383 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001384 fWindow->inval();
1385 }
jvanverth9f372462016-04-06 06:08:59 -07001386}
liyuqiane5a6cd92016-05-27 08:52:52 -07001387
1388void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001389 if (!fWindow) {
1390 return;
1391 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001392 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001393 return; // Surface hasn't been created yet.
1394 }
1395
liyuqianb73c24b2016-06-03 08:47:23 -07001396 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001397 Json::Value slideState(Json::objectValue);
1398 slideState[kName] = kSlideStateName;
1399 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001400 if (fAllSlideNames.size() == 0) {
1401 for(auto slide : fSlides) {
1402 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1403 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001404 }
liyuqian1f508fd2016-06-07 06:57:40 -07001405 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001406
liyuqianb73c24b2016-06-03 08:47:23 -07001407 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001408 Json::Value backendState(Json::objectValue);
1409 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001410 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001411 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001412 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001413 backendState[kOptions].append(Json::Value(str));
1414 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001415
csmartdalton578f0642017-02-24 16:04:47 -07001416 // MSAA state
1417 Json::Value msaaState(Json::objectValue);
1418 msaaState[kName] = kMSAAStateName;
1419 msaaState[kValue] = fWindow->sampleCount();
1420 msaaState[kOptions] = Json::Value(Json::arrayValue);
1421 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1422 msaaState[kOptions].append(Json::Value(0));
1423 } else {
1424 for (int msaa : {0, 4, 8, 16}) {
1425 msaaState[kOptions].append(Json::Value(msaa));
1426 }
1427 }
1428
csmartdalton61cd31a2017-02-27 17:00:53 -07001429 // Path renderer state
1430 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1431 Json::Value prState(Json::objectValue);
1432 prState[kName] = kPathRendererStateName;
1433 prState[kValue] = gPathRendererNames[pr];
1434 prState[kOptions] = Json::Value(Json::arrayValue);
1435 const GrContext* ctx = fWindow->getGrContext();
1436 if (!ctx) {
1437 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001438 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001439 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001440 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1441 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1442 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1443 }
1444 if (ctx->caps()->sampleShadingSupport()) {
1445 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1446 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001447 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001448 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1449 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001450 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001451 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001452 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1453 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1454 }
Jim Van Verth83010462017-03-16 08:45:39 -04001455 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001456 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001457 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1458 }
1459
liyuqianb73c24b2016-06-03 08:47:23 -07001460 // Softkey state
1461 Json::Value softkeyState(Json::objectValue);
1462 softkeyState[kName] = kSoftkeyStateName;
1463 softkeyState[kValue] = kSoftkeyHint;
1464 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1465 softkeyState[kOptions].append(kSoftkeyHint);
1466 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1467 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1468 }
1469
liyuqian1f508fd2016-06-07 06:57:40 -07001470 // FPS state
1471 Json::Value fpsState(Json::objectValue);
1472 fpsState[kName] = kFpsStateName;
Brian Osman56a24812017-12-19 11:15:16 -05001473 double animTime = fStatsLayer.getLastTime(fAnimateTimer);
1474 double paintTime = fStatsLayer.getLastTime(fPaintTimer);
1475 double flushTime = fStatsLayer.getLastTime(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001476 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
Brian Osman56a24812017-12-19 11:15:16 -05001477 animTime + paintTime + flushTime,
1478 animTime, paintTime, flushTime).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001479 fpsState[kOptions] = Json::Value(Json::arrayValue);
1480
liyuqiane5a6cd92016-05-27 08:52:52 -07001481 Json::Value state(Json::arrayValue);
1482 state.append(slideState);
1483 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001484 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001485 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001486 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001487 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001488
Brian Osmaneff04b52017-11-21 13:18:02 -05001489 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001490}
1491
1492void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001493 // For those who will add more features to handle the state change in this function:
1494 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1495 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1496 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001497 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001498 for (int i = 0; i < fSlides.count(); ++i) {
1499 if (fSlides[i]->getName().equals(stateValue)) {
1500 this->setCurrentSlide(i);
1501 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001502 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001503 }
Florin Malitaab99c342018-01-16 16:23:03 -05001504
1505 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001506 } else if (stateName.equals(kBackendStateName)) {
1507 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1508 if (stateValue.equals(kBackendTypeStrings[i])) {
1509 if (fBackendType != i) {
1510 fBackendType = (sk_app::Window::BackendType)i;
1511 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001512 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001513 }
1514 break;
1515 }
1516 }
csmartdalton578f0642017-02-24 16:04:47 -07001517 } else if (stateName.equals(kMSAAStateName)) {
1518 DisplayParams params = fWindow->getRequestedDisplayParams();
1519 int sampleCount = atoi(stateValue.c_str());
1520 if (sampleCount != params.fMSAASampleCount) {
1521 params.fMSAASampleCount = sampleCount;
1522 fWindow->setRequestedDisplayParams(params);
1523 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001524 this->updateTitle();
1525 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001526 }
1527 } else if (stateName.equals(kPathRendererStateName)) {
1528 DisplayParams params = fWindow->getRequestedDisplayParams();
1529 for (const auto& pair : gPathRendererNames) {
1530 if (pair.second == stateValue.c_str()) {
1531 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1532 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1533 fWindow->setRequestedDisplayParams(params);
1534 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001535 this->updateTitle();
1536 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001537 }
1538 break;
1539 }
csmartdalton578f0642017-02-24 16:04:47 -07001540 }
liyuqianb73c24b2016-06-03 08:47:23 -07001541 } else if (stateName.equals(kSoftkeyStateName)) {
1542 if (!stateValue.equals(kSoftkeyHint)) {
1543 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001544 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001545 }
liyuqian2edb0f42016-07-06 14:11:32 -07001546 } else if (stateName.equals(kRefreshStateName)) {
1547 // This state is actually NOT in the UI state.
1548 // We use this to allow Android to quickly set bool fRefresh.
1549 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001550 } else {
1551 SkDebugf("Unknown stateName: %s", stateName.c_str());
1552 }
1553}
Brian Osman79086b92017-02-10 13:36:16 -05001554
1555bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001556 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001557}
1558
1559bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001560 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001561 fWindow->inval();
1562 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001563 } else {
1564 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001565 }
Brian Osman79086b92017-02-10 13:36:16 -05001566}