blob: 6d8e3b3c95cc630c2151171bf218a6ec84b2c5d7 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Chris Dalton2d18f412018-02-20 13:23:32 -07008#include "BisectSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -07009#include "GMSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040010#include "GrContext.h"
11#include "GrContextPriv.h"
liyuqian6f163d22016-06-13 12:26:45 -070012#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050013#include "Resources.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070014#include "SKPSlide.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040015#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070016#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050017#include "SkColorSpacePriv.h"
Brian Osman2dd96932016-10-18 15:33:53 -040018#include "SkCommandLineFlags.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040019#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070020#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040021#include "SkEventTracingPriv.h"
Ben Wagner483c7722018-02-20 17:06:07 -050022#include "SkFontMgrPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040023#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050024#include "SkImagePriv.h"
Florin Malitab632df72018-06-18 21:23:06 -040025#include "SkJSONWriter.h"
Herb Derbyefe39bc2018-05-01 17:06:20 -040026#include "SkMakeUnique.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070027#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050028#include "SkOSPath.h"
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050029#include "SkPaintFilterCanvas.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050030#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040031#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070032#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070033#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050034#include "SkTaskGroup.h"
Ben Wagner483c7722018-02-20 17:06:07 -050035#include "SkTestFontMgr.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040036#include "SkTo.h"
Hal Canary8a001442018-09-19 11:31:27 -040037#include "SlideDir.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040038#include "SvgSlide.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040039#include "Viewer.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060040#include "ccpr/GrCoverageCountingPathRenderer.h"
csmartdalton578f0642017-02-24 16:04:47 -070041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <stdlib.h>
43#include <map>
44
Hal Canary8a001442018-09-19 11:31:27 -040045#include "imgui.h"
Florin Malita3b526b02018-05-25 12:43:51 -040046
Florin Malita87ccf332018-05-04 12:23:24 -040047#if defined(SK_ENABLE_SKOTTIE)
48 #include "SkottieSlide.h"
49#endif
50
Ruiqi Maof5101492018-06-29 14:32:21 -040051#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
52 #include "NIMASlide.h"
53#endif
54
Mike Reed5d052da2019-01-21 10:40:43 -050055enum PaintFlags {
56 kAntiAlias_PaintFlag = 1 << 0,
57 kDither_PaintFlag = 1 << 1,
58};
59
jvanverth34524262016-05-04 13:49:13 -070060using namespace sk_app;
61
csmartdalton61cd31a2017-02-27 17:00:53 -070062static std::map<GpuPathRenderers, std::string> gPathRendererNames;
63
jvanverth9f372462016-04-06 06:08:59 -070064Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070065 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070066}
67
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060068static DEFINE_string(slide, "", "Start on this sample.");
69static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050070
bsalomon6c471f72016-07-26 12:56:32 -070071#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070072# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070073#else
74# define BACKENDS_STR "\"sw\" and \"gl\""
75#endif
76
Brian Osman2dd96932016-10-18 15:33:53 -040077static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070078
Brian Salomonbdecacf2018-02-02 20:32:49 -050079static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070080
Chris Dalton2d18f412018-02-20 13:23:32 -070081DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect.");
82
Brian Osman53136aa2017-07-20 15:43:35 -040083DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050084
Florin Malita38792ce2018-05-08 10:36:18 -040085DEFINE_string2(file, f, "", "Open a single file for viewing.");
86
Brian Salomon194db172017-08-17 14:37:06 -040087const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070088 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040089#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
90 "ANGLE",
91#endif
jvanverth063ece72016-06-17 09:29:14 -070092#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070093 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070094#endif
csmartdalton578f0642017-02-24 16:04:47 -070095 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070096};
97
bsalomon6c471f72016-07-26 12:56:32 -070098static sk_app::Window::BackendType get_backend_type(const char* str) {
99#ifdef SK_VULKAN
100 if (0 == strcmp(str, "vk")) {
101 return sk_app::Window::kVulkan_BackendType;
102 } else
103#endif
Brian Salomon194db172017-08-17 14:37:06 -0400104#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
105 if (0 == strcmp(str, "angle")) {
106 return sk_app::Window::kANGLE_BackendType;
107 } else
108#endif
bsalomon6c471f72016-07-26 12:56:32 -0700109 if (0 == strcmp(str, "gl")) {
110 return sk_app::Window::kNativeGL_BackendType;
111 } else if (0 == strcmp(str, "sw")) {
112 return sk_app::Window::kRaster_BackendType;
113 } else {
114 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
115 return sk_app::Window::kRaster_BackendType;
116 }
117}
118
Brian Osmana109e392017-02-24 09:49:14 -0500119static SkColorSpacePrimaries gSrgbPrimaries = {
120 0.64f, 0.33f,
121 0.30f, 0.60f,
122 0.15f, 0.06f,
123 0.3127f, 0.3290f };
124
125static SkColorSpacePrimaries gAdobePrimaries = {
126 0.64f, 0.33f,
127 0.21f, 0.71f,
128 0.15f, 0.06f,
129 0.3127f, 0.3290f };
130
131static SkColorSpacePrimaries gP3Primaries = {
132 0.680f, 0.320f,
133 0.265f, 0.690f,
134 0.150f, 0.060f,
135 0.3127f, 0.3290f };
136
137static SkColorSpacePrimaries gRec2020Primaries = {
138 0.708f, 0.292f,
139 0.170f, 0.797f,
140 0.131f, 0.046f,
141 0.3127f, 0.3290f };
142
143struct NamedPrimaries {
144 const char* fName;
145 SkColorSpacePrimaries* fPrimaries;
146} gNamedPrimaries[] = {
147 { "sRGB", &gSrgbPrimaries },
148 { "AdobeRGB", &gAdobePrimaries },
149 { "P3", &gP3Primaries },
150 { "Rec. 2020", &gRec2020Primaries },
151};
152
153static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
154 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
155}
156
Brian Osman70d2f432017-11-08 09:54:10 -0500157static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
158 // In raster mode, we still use GL for the window.
159 // This lets us render the GUI faster (and correct).
160 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
161}
162
liyuqiane5a6cd92016-05-27 08:52:52 -0700163const char* kName = "name";
164const char* kValue = "value";
165const char* kOptions = "options";
166const char* kSlideStateName = "Slide";
167const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700168const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700169const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700170const char* kSoftkeyStateName = "Softkey";
171const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700172const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700173const char* kON = "ON";
174const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700175const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700176
jvanverth34524262016-05-04 13:49:13 -0700177Viewer::Viewer(int argc, char** argv, void* platformData)
Florin Malitaab99c342018-01-16 16:23:03 -0500178 : fCurrentSlide(-1)
179 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500180 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500181 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500182 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500183 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500184 , fShowZoomWindow(false)
Ben Wagner3627d2e2018-06-26 14:23:20 -0400185 , fZoomWindowFixed(false)
186 , fZoomWindowLocation{0.0f, 0.0f}
Brian Osmanf6877092017-02-13 09:39:57 -0500187 , fLastImage(nullptr)
Brian Osmanb63f6002018-07-24 18:01:53 -0400188 , fZoomUI(false)
jvanverth063ece72016-06-17 09:29:14 -0700189 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500190 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500191 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500192 // Our UI can only tweak gamma (currently), so start out gamma-only
Brian Osman82ebe042019-01-04 17:03:00 -0500193 , fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700194 , fZoomLevel(0.0f)
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400195 , fRotation(0.0f)
Ben Wagner897dfa22018-08-09 15:18:46 -0400196 , fOffset{0.5f, 0.5f}
Brian Osmanb53f48c2017-06-07 10:00:30 -0400197 , fGestureDevice(GestureDevice::kNone)
Brian Osmane9ed0f02018-11-26 14:50:05 -0500198 , fTiled(false)
199 , fDrawTileBoundaries(false)
200 , fTileScale{0.25f, 0.25f}
Brian Osman805a7272018-05-02 15:40:20 -0400201 , fPerspectiveMode(kPerspective_Off)
jvanverthc265a922016-04-08 12:51:45 -0700202{
Greg Daniel285db442016-10-14 09:12:53 -0400203 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700204
Brian Osmanf09e35e2017-12-15 14:48:09 -0500205 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500206 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
Brian Osmanf09e35e2017-12-15 14:48:09 -0500207 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
208 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
209 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
210 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700211
jvanverth2bb3b6d2016-04-08 07:24:09 -0700212 SkDebugf("Command line arguments: ");
213 for (int i = 1; i < argc; ++i) {
214 SkDebugf("%s ", argv[i]);
215 }
216 SkDebugf("\n");
217
218 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500219#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400220 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500221#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700222
Ben Wagner483c7722018-02-20 17:06:07 -0500223 if (!FLAGS_nativeFonts) {
224 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
225 }
226
Brian Osmanbc8150f2017-07-24 11:38:01 -0400227 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400228 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400229
bsalomon6c471f72016-07-26 12:56:32 -0700230 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700231 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700232
csmartdalton578f0642017-02-24 16:04:47 -0700233 DisplayParams displayParams;
234 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700235 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700236 fWindow->setRequestedDisplayParams(displayParams);
237
Brian Osman56a24812017-12-19 11:15:16 -0500238 // Configure timers
239 fStatsLayer.setActive(false);
240 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
241 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
242 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
243
jvanverth9f372462016-04-06 06:08:59 -0700244 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700245 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500246 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500247 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500248 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700249
brianosman622c8d52016-05-10 06:50:49 -0700250 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500251 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
252 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
253 fWindow->inval();
254 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500255 // Command to jump directly to the slide picker and give it focus
256 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
257 this->fShowImGuiDebugWindow = true;
258 this->fShowSlidePicker = true;
259 fWindow->inval();
260 });
261 // Alias that to Backspace, to match SampleApp
262 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
263 this->fShowImGuiDebugWindow = true;
264 this->fShowSlidePicker = true;
265 fWindow->inval();
266 });
Brian Osman79086b92017-02-10 13:36:16 -0500267 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
268 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
269 fWindow->inval();
270 });
Brian Osmanf6877092017-02-13 09:39:57 -0500271 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
272 this->fShowZoomWindow = !this->fShowZoomWindow;
273 fWindow->inval();
274 });
Ben Wagner3627d2e2018-06-26 14:23:20 -0400275 fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() {
276 this->fZoomWindowFixed = !this->fZoomWindowFixed;
277 fWindow->inval();
278 });
brianosman622c8d52016-05-10 06:50:49 -0700279 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500280 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700281 fWindow->inval();
282 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400283 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500284 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400285 this->updateTitle();
286 fWindow->inval();
287 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500288 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500289 switch (fColorMode) {
290 case ColorMode::kLegacy:
Brian Osman03115dc2018-11-26 13:55:19 -0500291 this->setColorMode(ColorMode::kColorManaged8888);
Brian Osman92004802017-03-06 11:47:26 -0500292 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500293 case ColorMode::kColorManaged8888:
294 this->setColorMode(ColorMode::kColorManagedF16);
Brian Osman92004802017-03-06 11:47:26 -0500295 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500296 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500297 this->setColorMode(ColorMode::kLegacy);
298 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500299 }
brianosman622c8d52016-05-10 06:50:49 -0700300 });
301 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500302 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700303 });
304 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500305 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700306 });
307 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
308 this->changeZoomLevel(1.f / 32.f);
309 fWindow->inval();
310 });
311 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
312 this->changeZoomLevel(-1.f / 32.f);
313 fWindow->inval();
314 });
jvanverthaf236b52016-05-20 06:01:06 -0700315 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400316 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
317 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500318 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400319#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
320 if (newBackend == sk_app::Window::kVulkan_BackendType) {
321 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
322 sk_app::Window::kBackendTypeCount);
323 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
324 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500325 }
326#endif
Brian Osman621491e2017-02-28 15:45:01 -0500327 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700328 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500329 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
330 fSaveToSKP = true;
331 fWindow->inval();
332 });
Ben Wagner37c54032018-04-13 14:30:23 -0400333 fCommands.addCommand('G', "Modes", "Geometry", [this]() {
334 DisplayParams params = fWindow->getRequestedDisplayParams();
335 uint32_t flags = params.fSurfaceProps.flags();
336 if (!fPixelGeometryOverrides) {
337 fPixelGeometryOverrides = true;
338 params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
339 } else {
340 switch (params.fSurfaceProps.pixelGeometry()) {
341 case kUnknown_SkPixelGeometry:
342 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry);
343 break;
344 case kRGB_H_SkPixelGeometry:
345 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry);
346 break;
347 case kBGR_H_SkPixelGeometry:
348 params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry);
349 break;
350 case kRGB_V_SkPixelGeometry:
351 params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry);
352 break;
353 case kBGR_V_SkPixelGeometry:
354 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
355 fPixelGeometryOverrides = false;
356 break;
357 }
358 }
359 fWindow->setRequestedDisplayParams(params);
360 this->updateTitle();
361 fWindow->inval();
362 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500363 fCommands.addCommand('H', "Paint", "Hinting mode", [this]() {
Mike Reed3ae47332019-01-04 10:11:46 -0500364 if (!fFontOverrides.fHinting) {
365 fFontOverrides.fHinting = true;
366 fFont.setHinting(kNo_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500367 } else {
Mike Reed3ae47332019-01-04 10:11:46 -0500368 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500369 case kNo_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500370 fFont.setHinting(kSlight_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500371 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500372 case kSlight_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500373 fFont.setHinting(kNormal_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500374 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500375 case kNormal_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500376 fFont.setHinting(kFull_SkFontHinting);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500377 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500378 case kFull_SkFontHinting:
Mike Reed3ae47332019-01-04 10:11:46 -0500379 fFont.setHinting(kNo_SkFontHinting);
380 fFontOverrides.fHinting = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500381 break;
382 }
383 }
384 this->updateTitle();
385 fWindow->inval();
386 });
387 fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() {
Mike Reed5d052da2019-01-21 10:40:43 -0500388 if (!(fPaintOverrides.fFlags & kAntiAlias_PaintFlag)) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500389 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
Mike Reed5d052da2019-01-21 10:40:43 -0500390 fPaintOverrides.fFlags |= kAntiAlias_PaintFlag;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500391 fPaint.setAntiAlias(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500392 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
393 gSkUseDeltaAA = gSkForceDeltaAA = false;
394 } else {
395 fPaint.setAntiAlias(true);
396 switch (fPaintOverrides.fAntiAlias) {
397 case SkPaintFields::AntiAliasState::Alias:
398 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal;
Ben Wagnera580fb32018-04-17 11:16:32 -0400399 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
400 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500401 break;
402 case SkPaintFields::AntiAliasState::Normal:
403 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500404 gSkUseAnalyticAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400405 gSkForceAnalyticAA = false;
406 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500407 break;
408 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
409 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400410 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
411 gSkUseDeltaAA = gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500412 break;
413 case SkPaintFields::AntiAliasState::AnalyticAAForced:
414 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled;
415 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
416 gSkUseDeltaAA = true;
Ben Wagnera580fb32018-04-17 11:16:32 -0400417 gSkForceDeltaAA = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500418 break;
419 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
420 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced;
Ben Wagnera580fb32018-04-17 11:16:32 -0400421 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
422 gSkUseDeltaAA = gSkForceDeltaAA = true;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500423 break;
424 case SkPaintFields::AntiAliasState::DeltaAAForced:
425 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
Mike Reed5d052da2019-01-21 10:40:43 -0500426 fPaintOverrides.fFlags &= ~kAntiAlias_PaintFlag;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500427 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
428 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
429 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
430 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
431 break;
432 }
433 }
434 this->updateTitle();
435 fWindow->inval();
436 });
Ben Wagner37c54032018-04-13 14:30:23 -0400437 fCommands.addCommand('D', "Modes", "DFT", [this]() {
438 DisplayParams params = fWindow->getRequestedDisplayParams();
439 uint32_t flags = params.fSurfaceProps.flags();
440 flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
441 params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry());
442 fWindow->setRequestedDisplayParams(params);
443 this->updateTitle();
444 fWindow->inval();
445 });
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500446 fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500447 if (!fFontOverrides.fEdging) {
448 fFontOverrides.fEdging = true;
449 fFont.setEdging(SkFont::Edging::kAlias);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500450 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500451 switch (fFont.getEdging()) {
452 case SkFont::Edging::kAlias:
453 fFont.setEdging(SkFont::Edging::kAntiAlias);
454 break;
455 case SkFont::Edging::kAntiAlias:
456 fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
457 break;
458 case SkFont::Edging::kSubpixelAntiAlias:
459 fFont.setEdging(SkFont::Edging::kAlias);
460 fFontOverrides.fEdging = false;
461 break;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500462 }
463 }
464 this->updateTitle();
465 fWindow->inval();
466 });
467 fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500468 if (!fFontOverrides.fSubpixel) {
469 fFontOverrides.fSubpixel = true;
470 fFont.setSubpixel(false);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500471 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500472 if (!fFont.isSubpixel()) {
473 fFont.setSubpixel(true);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500474 } else {
Mike Reede5f9cfa2019-01-10 13:55:35 -0500475 fFontOverrides.fSubpixel = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500476 }
477 }
478 this->updateTitle();
479 fWindow->inval();
480 });
Brian Osman805a7272018-05-02 15:40:20 -0400481 fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() {
482 fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake
483 : kPerspective_Real;
484 this->updateTitle();
485 fWindow->inval();
486 });
487 fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() {
488 fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real
489 : kPerspective_Off;
490 this->updateTitle();
491 fWindow->inval();
492 });
Brian Osman207d4102019-01-10 09:40:58 -0500493 fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() {
494 fAnimTimer.togglePauseResume();
495 });
Brian Osmanb63f6002018-07-24 18:01:53 -0400496 fCommands.addCommand('u', "GUI", "Zoom UI", [this]() {
497 fZoomUI = !fZoomUI;
498 fStatsLayer.setDisplayScale(fZoomUI ? 2.0f : 1.0f);
499 fWindow->inval();
500 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500501
jvanverth2bb3b6d2016-04-08 07:24:09 -0700502 // set up slides
503 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500504 if (FLAGS_list) {
505 this->listNames();
506 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700507
Brian Osman9bb47cf2018-04-26 15:55:00 -0400508 fPerspectivePoints[0].set(0, 0);
509 fPerspectivePoints[1].set(1, 0);
510 fPerspectivePoints[2].set(0, 1);
511 fPerspectivePoints[3].set(1, 1);
djsollen12d62a72016-04-21 07:59:44 -0700512 fAnimTimer.run();
513
Hal Canaryc465d132017-12-08 10:21:31 -0500514 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500515 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400516 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500517 }
518 fImGuiGamutPaint.setColor(SK_ColorWHITE);
519 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
520
Brian Osman70d2f432017-11-08 09:54:10 -0500521 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500522 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700523}
524
jvanverth34524262016-05-04 13:49:13 -0700525void Viewer::initSlides() {
Florin Malita0ffa3222018-04-05 14:34:45 -0400526 using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path);
527 static const struct {
528 const char* fExtension;
529 const char* fDirName;
530 const SkCommandLineFlags::StringArray& fFlags;
531 const SlideFactory fFactory;
532 } gExternalSlidesInfo[] = {
533 { ".skp", "skp-dir", FLAGS_skps,
534 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
535 return sk_make_sp<SKPSlide>(name, path);}
536 },
537 { ".jpg", "jpg-dir", FLAGS_jpgs,
538 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
539 return sk_make_sp<ImageSlide>(name, path);}
540 },
Florin Malita87ccf332018-05-04 12:23:24 -0400541#if defined(SK_ENABLE_SKOTTIE)
Eric Boren8c172ba2018-07-19 13:27:49 -0400542 { ".json", "skottie-dir", FLAGS_lotties,
Florin Malita0ffa3222018-04-05 14:34:45 -0400543 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
544 return sk_make_sp<SkottieSlide>(name, path);}
545 },
Florin Malita87ccf332018-05-04 12:23:24 -0400546#endif
Florin Malita5d3ff432018-07-31 16:38:43 -0400547#if defined(SK_XML)
Florin Malita0ffa3222018-04-05 14:34:45 -0400548 { ".svg", "svg-dir", FLAGS_svgs,
549 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
550 return sk_make_sp<SvgSlide>(name, path);}
551 },
Florin Malita5d3ff432018-07-31 16:38:43 -0400552#endif
Ruiqi Maof5101492018-06-29 14:32:21 -0400553#if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__))
554 { ".nima", "nima-dir", FLAGS_nimas,
555 [](const SkString& name, const SkString& path) -> sk_sp<Slide> {
556 return sk_make_sp<NIMASlide>(name, path);}
557 },
558#endif
Florin Malita0ffa3222018-04-05 14:34:45 -0400559 };
jvanverthc265a922016-04-08 12:51:45 -0700560
Brian Salomon343553a2018-09-05 15:41:23 -0400561 SkTArray<sk_sp<Slide>> dirSlides;
jvanverthc265a922016-04-08 12:51:45 -0700562
Florin Malita0ffa3222018-04-05 14:34:45 -0400563 const auto addSlide = [&](const SkString& name,
564 const SkString& path,
565 const SlideFactory& fact) {
566 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) {
567 return;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700568 }
liyuqian6f163d22016-06-13 12:26:45 -0700569
Florin Malita0ffa3222018-04-05 14:34:45 -0400570 if (auto slide = fact(name, path)) {
Florin Malita76a076b2018-02-15 18:40:48 -0500571 dirSlides.push_back(slide);
572 fSlides.push_back(std::move(slide));
573 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400574 };
Florin Malita76a076b2018-02-15 18:40:48 -0500575
Florin Malita38792ce2018-05-08 10:36:18 -0400576 if (!FLAGS_file.isEmpty()) {
577 // single file mode
578 const SkString file(FLAGS_file[0]);
579
580 if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) {
581 for (const auto& sinfo : gExternalSlidesInfo) {
582 if (file.endsWith(sinfo.fExtension)) {
583 addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory);
584 return;
585 }
586 }
587
588 fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str());
589 } else {
590 fprintf(stderr, "Cannot read \"%s\"\n", file.c_str());
591 }
592
593 return;
594 }
595
596 // Bisect slide.
597 if (!FLAGS_bisect.isEmpty()) {
598 sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]);
599 if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) {
600 if (FLAGS_bisect.count() >= 2) {
601 for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) {
602 bisect->onChar(*ch);
603 }
604 }
605 fSlides.push_back(std::move(bisect));
606 }
607 }
608
609 // GMs
610 int firstGM = fSlides.count();
Hal Canary972eba32018-07-30 17:07:07 -0400611 for (skiagm::GMFactory gmFactory : skiagm::GMRegistry::Range()) {
612 std::unique_ptr<skiagm::GM> gm(gmFactory(nullptr));
Florin Malita38792ce2018-05-08 10:36:18 -0400613 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
614 sk_sp<Slide> slide(new GMSlide(gm.release()));
615 fSlides.push_back(std::move(slide));
616 }
Florin Malita38792ce2018-05-08 10:36:18 -0400617 }
618 // reverse gms
619 int numGMs = fSlides.count() - firstGM;
620 for (int i = 0; i < numGMs/2; ++i) {
621 std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]);
622 }
623
624 // samples
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400625 for (const SampleFactory factory : SampleRegistry::Range()) {
626 sk_sp<Slide> slide(new SampleSlide(factory));
Florin Malita38792ce2018-05-08 10:36:18 -0400627 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
628 fSlides.push_back(slide);
629 }
Florin Malita38792ce2018-05-08 10:36:18 -0400630 }
631
Florin Malita0ffa3222018-04-05 14:34:45 -0400632 for (const auto& info : gExternalSlidesInfo) {
633 for (const auto& flag : info.fFlags) {
634 if (SkStrEndsWith(flag.c_str(), info.fExtension)) {
635 // single file
636 addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory);
637 } else {
638 // directory
639 SkOSFile::Iter it(flag.c_str(), info.fExtension);
640 SkString name;
641 while (it.next(&name)) {
642 addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory);
643 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400644 }
Florin Malita0ffa3222018-04-05 14:34:45 -0400645 if (!dirSlides.empty()) {
646 fSlides.push_back(
647 sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()),
648 std::move(dirSlides)));
Mike Klein16885072018-12-11 09:54:31 -0500649 dirSlides.reset(); // NOLINT(bugprone-use-after-move)
Florin Malita0ffa3222018-04-05 14:34:45 -0400650 }
Florin Malitac659c2c2018-04-05 11:57:21 -0400651 }
652 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700653}
654
655
jvanverth34524262016-05-04 13:49:13 -0700656Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700657 fWindow->detach();
658 delete fWindow;
659}
660
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500661struct SkPaintTitleUpdater {
662 SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {}
663 void append(const char* s) {
664 if (fCount == 0) {
665 fTitle->append(" {");
666 } else {
667 fTitle->append(", ");
668 }
669 fTitle->append(s);
670 ++fCount;
671 }
672 void done() {
673 if (fCount > 0) {
674 fTitle->append("}");
675 }
676 }
677 SkString* fTitle;
678 int fCount;
679};
680
brianosman05de2162016-05-06 13:28:57 -0700681void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700682 if (!fWindow) {
683 return;
684 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500685 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700686 return; // Surface hasn't been created yet.
687 }
688
jvanverth34524262016-05-04 13:49:13 -0700689 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700690 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700691
Yuqian Li399b3c22017-08-03 11:08:15 -0400692 if (gSkUseDeltaAA) {
693 if (gSkForceDeltaAA) {
694 title.append(" <FDAA>");
695 } else {
696 title.append(" <DAA>");
697 }
698 } else if (gSkUseAnalyticAA) {
699 if (gSkForceAnalyticAA) {
700 title.append(" <FAAA>");
701 } else {
702 title.append(" <AAA>");
703 }
704 }
705
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500706 SkPaintTitleUpdater paintTitle(&title);
Mike Reed5d052da2019-01-21 10:40:43 -0500707 auto paintFlag = [this, &paintTitle](PaintFlags flag, bool (SkPaint::* isFlag)() const,
Ben Wagner99a78dc2018-05-09 18:23:51 -0400708 const char* on, const char* off)
709 {
710 if (fPaintOverrides.fFlags & flag) {
711 paintTitle.append((fPaint.*isFlag)() ? on : off);
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500712 }
Ben Wagner99a78dc2018-05-09 18:23:51 -0400713 };
714
Mike Reed5d052da2019-01-21 10:40:43 -0500715 paintFlag(kAntiAlias_PaintFlag, &SkPaint::isAntiAlias, "Antialias", "Alias");
716 paintFlag(kAntiAlias_PaintFlag, &SkPaint::isDither, "DITHER", "No Dither");
Ben Wagner99a78dc2018-05-09 18:23:51 -0400717
Mike Reed3ae47332019-01-04 10:11:46 -0500718 if (fFontOverrides.fHinting) {
719 switch (fFont.getHinting()) {
Mike Reed9edbf422018-11-07 19:54:33 -0500720 case kNo_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500721 paintTitle.append("No Hinting");
722 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500723 case kSlight_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500724 paintTitle.append("Slight Hinting");
725 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500726 case kNormal_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500727 paintTitle.append("Normal Hinting");
728 break;
Mike Reed9edbf422018-11-07 19:54:33 -0500729 case kFull_SkFontHinting:
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500730 paintTitle.append("Full Hinting");
731 break;
732 }
733 }
734 paintTitle.done();
735
Brian Osman92004802017-03-06 11:47:26 -0500736 switch (fColorMode) {
737 case ColorMode::kLegacy:
738 title.append(" Legacy 8888");
739 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500740 case ColorMode::kColorManaged8888:
Brian Osman92004802017-03-06 11:47:26 -0500741 title.append(" ColorManaged 8888");
742 break;
Brian Osman03115dc2018-11-26 13:55:19 -0500743 case ColorMode::kColorManagedF16:
Brian Osman92004802017-03-06 11:47:26 -0500744 title.append(" ColorManaged F16");
745 break;
746 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500747
Brian Osman92004802017-03-06 11:47:26 -0500748 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500749 int curPrimaries = -1;
750 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
751 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
752 curPrimaries = i;
753 break;
754 }
755 }
Brian Osman03115dc2018-11-26 13:55:19 -0500756 title.appendf(" %s Gamma %f",
757 curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom",
Brian Osman82ebe042019-01-04 17:03:00 -0500758 fColorSpaceTransferFn.g);
brianosman05de2162016-05-06 13:28:57 -0700759 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500760
Ben Wagner37c54032018-04-13 14:30:23 -0400761 const DisplayParams& params = fWindow->getRequestedDisplayParams();
762 if (fPixelGeometryOverrides) {
763 switch (params.fSurfaceProps.pixelGeometry()) {
764 case kUnknown_SkPixelGeometry:
765 title.append( " Flat");
766 break;
767 case kRGB_H_SkPixelGeometry:
768 title.append( " RGB");
769 break;
770 case kBGR_H_SkPixelGeometry:
771 title.append( " BGR");
772 break;
773 case kRGB_V_SkPixelGeometry:
774 title.append( " RGBV");
775 break;
776 case kBGR_V_SkPixelGeometry:
777 title.append( " BGRV");
778 break;
779 }
780 }
781
782 if (params.fSurfaceProps.isUseDeviceIndependentFonts()) {
783 title.append(" DFT");
784 }
785
csmartdalton578f0642017-02-24 16:04:47 -0700786 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700787 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500788 int msaa = fWindow->sampleCount();
789 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700790 title.appendf(" MSAA: %i", msaa);
791 }
792 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700793
794 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman1994f202018-07-10 11:41:22 -0400795 if (GpuPathRenderers::kAll != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700796 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
797 }
798
Brian Osman805a7272018-05-02 15:40:20 -0400799 if (kPerspective_Real == fPerspectiveMode) {
800 title.append(" Perpsective (Real)");
801 } else if (kPerspective_Fake == fPerspectiveMode) {
802 title.append(" Perspective (Fake)");
803 }
804
brianosman05de2162016-05-06 13:28:57 -0700805 fWindow->setTitle(title.c_str());
806}
807
Florin Malitaab99c342018-01-16 16:23:03 -0500808int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500809
810 if (!FLAGS_slide.isEmpty()) {
811 int count = fSlides.count();
812 for (int i = 0; i < count; i++) {
813 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500814 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500815 }
816 }
817
818 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
819 this->listNames();
820 }
821
Florin Malitaab99c342018-01-16 16:23:03 -0500822 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500823}
824
Florin Malitaab99c342018-01-16 16:23:03 -0500825void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500826 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500827 for (const auto& slide : fSlides) {
828 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500829 }
830}
831
Florin Malitaab99c342018-01-16 16:23:03 -0500832void Viewer::setCurrentSlide(int slide) {
833 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700834
Florin Malitaab99c342018-01-16 16:23:03 -0500835 if (slide == fCurrentSlide) {
836 return;
837 }
838
839 if (fCurrentSlide >= 0) {
840 fSlides[fCurrentSlide]->unload();
841 }
842
843 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
844 SkIntToScalar(fWindow->height()));
845 fCurrentSlide = slide;
846 this->setupCurrentSlide();
847}
848
849void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500850 if (fCurrentSlide >= 0) {
851 // prepare dimensions for image slides
852 fGesture.resetTouchState();
853 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700854
Jim Van Verth0848fb02018-01-22 13:39:30 -0500855 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
856 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
857 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400858
Jim Van Verth0848fb02018-01-22 13:39:30 -0500859 // Start with a matrix that scales the slide to the available screen space
860 if (fWindow->scaleContentToFit()) {
861 if (windowRect.width() > 0 && windowRect.height() > 0) {
862 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
863 }
liyuqiane46e4f02016-05-20 07:32:19 -0700864 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500865
866 // Prevent the user from dragging content so far outside the window they can't find it again
Yuqian Li755778c2018-03-28 16:23:31 -0400867 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
Jim Van Verth0848fb02018-01-22 13:39:30 -0500868
869 this->updateTitle();
870 this->updateUIState();
871
872 fStatsLayer.resetMeasurements();
873
874 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700875 }
jvanverthc265a922016-04-08 12:51:45 -0700876}
877
878#define MAX_ZOOM_LEVEL 8
879#define MIN_ZOOM_LEVEL -8
880
jvanverth34524262016-05-04 13:49:13 -0700881void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700882 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400883 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400884 this->preTouchMatrixChanged();
885}
Yuqian Li755778c2018-03-28 16:23:31 -0400886
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400887void Viewer::preTouchMatrixChanged() {
888 // Update the trans limit as the transform changes.
Yuqian Li755778c2018-03-28 16:23:31 -0400889 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
890 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
891 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
892 fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix());
893}
894
Brian Osman805a7272018-05-02 15:40:20 -0400895SkMatrix Viewer::computePerspectiveMatrix() {
896 SkScalar w = fWindow->width(), h = fWindow->height();
897 SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } };
898 SkPoint perspPts[4] = {
899 { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h },
900 { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h },
901 { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h },
902 { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h }
903 };
904 SkMatrix m;
905 m.setPolyToPoly(orthoPts, perspPts, 4);
906 return m;
907}
908
Yuqian Li755778c2018-03-28 16:23:31 -0400909SkMatrix Viewer::computePreTouchMatrix() {
910 SkMatrix m = fDefaultMatrix;
911 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
912 : SK_Scalar1 + fZoomLevel;
Ben Wagner897dfa22018-08-09 15:18:46 -0400913 m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f);
Yuqian Li755778c2018-03-28 16:23:31 -0400914 m.preScale(zoomScale, zoomScale);
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400915
916 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
917 m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400918
Brian Osman805a7272018-05-02 15:40:20 -0400919 if (kPerspective_Real == fPerspectiveMode) {
920 SkMatrix persp = this->computePerspectiveMatrix();
Brian Osmanbdaf97b2018-04-26 16:22:42 -0400921 m.postConcat(persp);
Brian Osman9bb47cf2018-04-26 15:55:00 -0400922 }
923
Yuqian Li755778c2018-03-28 16:23:31 -0400924 return m;
jvanverthc265a922016-04-08 12:51:45 -0700925}
926
liyuqiand3cdbca2016-05-17 12:44:20 -0700927SkMatrix Viewer::computeMatrix() {
Yuqian Li755778c2018-03-28 16:23:31 -0400928 SkMatrix m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700929 m.preConcat(fGesture.globalM());
Yuqian Li755778c2018-03-28 16:23:31 -0400930 m.preConcat(this->computePreTouchMatrix());
liyuqiand3cdbca2016-05-17 12:44:20 -0700931 return m;
jvanverthc265a922016-04-08 12:51:45 -0700932}
933
Brian Osman621491e2017-02-28 15:45:01 -0500934void Viewer::setBackend(sk_app::Window::BackendType backendType) {
935 fBackendType = backendType;
936
937 fWindow->detach();
938
Brian Osman70d2f432017-11-08 09:54:10 -0500939#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400940 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
941 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500942 DisplayParams params = fWindow->getRequestedDisplayParams();
943 delete fWindow;
944 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500945
Brian Osman70d2f432017-11-08 09:54:10 -0500946 // re-register callbacks
947 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500948 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500949 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500950 fWindow->pushLayer(&fImGuiLayer);
951
Brian Osman70d2f432017-11-08 09:54:10 -0500952 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
953 // will still include our correct sample count. But the re-created fWindow will lose that
954 // information. On Windows, we need to re-create the window when changing sample count,
955 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
956 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
957 // as if we had never changed windows at all).
958 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500959#endif
960
Brian Osman70d2f432017-11-08 09:54:10 -0500961 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500962}
963
Brian Osman92004802017-03-06 11:47:26 -0500964void Viewer::setColorMode(ColorMode colorMode) {
965 fColorMode = colorMode;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500966 this->updateTitle();
967 fWindow->inval();
968}
969
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500970class OveridePaintFilterCanvas : public SkPaintFilterCanvas {
971public:
Mike Reed3ae47332019-01-04 10:11:46 -0500972 OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* pfields,
973 SkFont* font, Viewer::SkFontFields* ffields)
974 : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(pfields), fFont(font), fFontOverrides(ffields)
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500975 { }
Ben Wagner41e40472018-09-24 13:01:54 -0400976 const SkTextBlob* filterTextBlob(const SkPaint& paint, const SkTextBlob* blob,
977 sk_sp<SkTextBlob>* cache) {
978 bool blobWillChange = false;
979 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -0500980 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
981 bool shouldDraw = this->filterFont(&filteredFont);
982 if (it.font() != *filteredFont || !shouldDraw) {
Ben Wagner41e40472018-09-24 13:01:54 -0400983 blobWillChange = true;
984 break;
985 }
986 }
987 if (!blobWillChange) {
988 return blob;
989 }
990
991 SkTextBlobBuilder builder;
992 for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) {
Mike Reed3ae47332019-01-04 10:11:46 -0500993 SkTCopyOnFirstWrite<SkFont> filteredFont(it.font());
994 bool shouldDraw = this->filterFont(&filteredFont);
Ben Wagner41e40472018-09-24 13:01:54 -0400995 if (!shouldDraw) {
996 continue;
997 }
998
Mike Reed3ae47332019-01-04 10:11:46 -0500999 SkFont font = *filteredFont;
Mike Reed6d595682018-12-05 17:28:14 -05001000
Ben Wagner41e40472018-09-24 13:01:54 -04001001 const SkTextBlobBuilder::RunBuffer& runBuffer
1002 = it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001003 ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001004 it.offset().x(),it.offset().y(), it.glyphCount(), it.textSize(), SkString())
1005 : it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001006 ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001007 it.offset().y(), it.glyphCount(), it.textSize(), SkString())
1008 : it.positioning() == SkTextBlobRunIterator::kFull_Positioning
Mike Reed6d595682018-12-05 17:28:14 -05001009 ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
Ben Wagner41e40472018-09-24 13:01:54 -04001010 it.glyphCount(), it.textSize(), SkString())
1011 : (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
1012 uint32_t glyphCount = it.glyphCount();
1013 if (it.glyphs()) {
1014 size_t glyphSize = sizeof(decltype(*it.glyphs()));
1015 memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize);
1016 }
1017 if (it.pos()) {
1018 size_t posSize = sizeof(decltype(*it.pos()));
1019 uint8_t positioning = it.positioning();
1020 memcpy(runBuffer.pos, it.pos(), glyphCount * positioning * posSize);
1021 }
1022 if (it.text()) {
1023 size_t textSize = sizeof(decltype(*it.text()));
1024 uint32_t textCount = it.textSize();
1025 memcpy(runBuffer.utf8text, it.text(), textCount * textSize);
1026 }
1027 if (it.clusters()) {
1028 size_t clusterSize = sizeof(decltype(*it.clusters()));
1029 memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize);
1030 }
1031 }
1032 *cache = builder.make();
1033 return cache->get();
1034 }
1035 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1036 const SkPaint& paint) override {
1037 sk_sp<SkTextBlob> cache;
1038 this->SkPaintFilterCanvas::onDrawTextBlob(
1039 this->filterTextBlob(paint, blob, &cache), x, y, paint);
1040 }
Mike Reed3ae47332019-01-04 10:11:46 -05001041 bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const {
1042 if (fFontOverrides->fTextSize) {
1043 font->writable()->setSize(fFont->getSize());
1044 }
1045 if (fFontOverrides->fHinting) {
1046 font->writable()->setHinting(fFont->getHinting());
1047 }
Mike Reed3ae47332019-01-04 10:11:46 -05001048 return true;
1049 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001050 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
Ben Wagneraa5da732018-03-28 13:36:02 -04001051 if (*paint == nullptr) {
1052 return true;
1053 }
Mike Reed5d052da2019-01-21 10:40:43 -05001054 if (fPaintOverrides->fFlags & kAntiAlias_PaintFlag) {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001055 paint->writable()->setAntiAlias(fPaint->isAntiAlias());
1056 }
Mike Reed5d052da2019-01-21 10:40:43 -05001057 if (fPaintOverrides->fFlags & kDither_PaintFlag) {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001058 paint->writable()->setDither(fPaint->isDither());
1059 }
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001060 return true;
1061 }
1062 SkPaint* fPaint;
1063 Viewer::SkPaintFields* fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -05001064 SkFont* fFont;
1065 Viewer::SkFontFields* fFontOverrides;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -05001066};
1067
Brian Osmanf750fbc2017-02-08 10:47:28 -05001068void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001069 SkAutoCanvasRestore autorestore(canvas, false);
1070
Brian Osmanf750fbc2017-02-08 10:47:28 -05001071 // By default, we render directly into the window's surface/canvas
1072 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -05001073 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -07001074
Brian Osmane0d4fba2017-03-15 10:24:55 -04001075 // If we're in any of the color managed modes, construct the color space we're going to use
Brian Osman03115dc2018-11-26 13:55:19 -05001076 sk_sp<SkColorSpace> colorSpace = nullptr;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001077 if (ColorMode::kLegacy != fColorMode) {
Brian Osman82ebe042019-01-04 17:03:00 -05001078 skcms_Matrix3x3 toXYZ;
Brian Osmane0d4fba2017-03-15 10:24:55 -04001079 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osman03115dc2018-11-26 13:55:19 -05001080 colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
Brian Osmane0d4fba2017-03-15 10:24:55 -04001081 }
1082
Brian Osman3ac99cf2017-12-01 11:23:53 -05001083 if (fSaveToSKP) {
1084 SkPictureRecorder recorder;
1085 SkCanvas* recorderCanvas = recorder.beginRecording(
1086 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
Brian Osman3ac99cf2017-12-01 11:23:53 -05001087 fSlides[fCurrentSlide]->draw(recorderCanvas);
1088 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
1089 SkFILEWStream stream("sample_app.skp");
1090 picture->serialize(&stream);
1091 fSaveToSKP = false;
1092 }
1093
Brian Osmane9ed0f02018-11-26 14:50:05 -05001094 // Grab some things we'll need to make surfaces (for tiling or general offscreen rendering)
1095 SkColorType colorType = (ColorMode::kColorManagedF16 == fColorMode) ? kRGBA_F16_SkColorType
1096 : kN32_SkColorType;
1097 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
1098 canvas->getProps(&props);
1099
1100 auto make_surface = [=](int w, int h) {
1101 SkImageInfo info = SkImageInfo::Make(w, h, colorType, kPremul_SkAlphaType, colorSpace);
1102 return Window::kRaster_BackendType == this->fBackendType
1103 ? SkSurface::MakeRaster(info, &props)
1104 : canvas->makeSurface(info, &props);
1105 };
1106
Brian Osman03115dc2018-11-26 13:55:19 -05001107 // We need to render offscreen if we're...
1108 // ... in fake perspective or zooming (so we have a snapped copy of the results)
1109 // ... in any raster mode, because the window surface is actually GL
1110 // ... in any color managed mode, because we always make the window surface with no color space
Brian Osmanf750fbc2017-02-08 10:47:28 -05001111 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman03115dc2018-11-26 13:55:19 -05001112 if (kPerspective_Fake == fPerspectiveMode ||
Brian Osman92004802017-03-06 11:47:26 -05001113 fShowZoomWindow ||
Brian Osman03115dc2018-11-26 13:55:19 -05001114 Window::kRaster_BackendType == fBackendType ||
1115 colorSpace != nullptr) {
Brian Osmane0d4fba2017-03-15 10:24:55 -04001116
Brian Osmane9ed0f02018-11-26 14:50:05 -05001117 offscreenSurface = make_surface(fWindow->width(), fWindow->height());
Mike Klein48b64902018-07-25 13:28:44 -04001118 slideCanvas = offscreenSurface->getCanvas();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001119 }
1120
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001121 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001122 slideCanvas->clear(SK_ColorWHITE);
Brian Osman1df161a2017-02-09 12:10:20 -05001123 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -05001124 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001125 if (fTiled) {
1126 int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width());
1127 int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height());
1128 sk_sp<SkSurface> tileSurface = make_surface(tileW, tileH);
1129 SkCanvas* tileCanvas = tileSurface->getCanvas();
1130 SkMatrix m = this->computeMatrix();
1131 for (int y = 0; y < fWindow->height(); y += tileH) {
1132 for (int x = 0; x < fWindow->width(); x += tileW) {
1133 SkAutoCanvasRestore acr(tileCanvas, true);
1134 tileCanvas->translate(-x, -y);
1135 tileCanvas->clear(SK_ColorTRANSPARENT);
1136 tileCanvas->concat(m);
Mike Reed3ae47332019-01-04 10:11:46 -05001137 OveridePaintFilterCanvas filterCanvas(tileCanvas, &fPaint, &fPaintOverrides,
1138 &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001139 fSlides[fCurrentSlide]->draw(&filterCanvas);
1140 tileSurface->draw(slideCanvas, x, y, nullptr);
1141 }
1142 }
1143
1144 // Draw borders between tiles
1145 if (fDrawTileBoundaries) {
1146 SkPaint border;
1147 border.setColor(0x60FF00FF);
1148 border.setStyle(SkPaint::kStroke_Style);
1149 for (int y = 0; y < fWindow->height(); y += tileH) {
1150 for (int x = 0; x < fWindow->width(); x += tileW) {
1151 slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border);
1152 }
1153 }
1154 }
1155 } else {
1156 slideCanvas->concat(this->computeMatrix());
1157 if (kPerspective_Real == fPerspectiveMode) {
1158 slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height()));
1159 }
Mike Reed3ae47332019-01-04 10:11:46 -05001160 OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides, &fFont, &fFontOverrides);
Brian Osmane9ed0f02018-11-26 14:50:05 -05001161 fSlides[fCurrentSlide]->draw(&filterCanvas);
1162 }
Brian Osman56a24812017-12-19 11:15:16 -05001163 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001164 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -05001165
1166 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -05001167 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001168 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -05001169 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -05001170
1171 // If we rendered offscreen, snap an image and push the results to the window's canvas
1172 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -05001173 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -05001174
Brian Salomonbf52e3d2017-02-22 15:21:11 -05001175 SkPaint paint;
1176 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman805a7272018-05-02 15:40:20 -04001177 int prePerspectiveCount = canvas->save();
1178 if (kPerspective_Fake == fPerspectiveMode) {
1179 paint.setFilterQuality(kHigh_SkFilterQuality);
1180 canvas->clear(SK_ColorWHITE);
1181 canvas->concat(this->computePerspectiveMatrix());
1182 }
Brian Osman03115dc2018-11-26 13:55:19 -05001183 canvas->drawImage(fLastImage, 0, 0, &paint);
Brian Osman805a7272018-05-02 15:40:20 -04001184 canvas->restoreToCount(prePerspectiveCount);
liyuqian74959a12016-06-16 14:10:34 -07001185 }
liyuqian6f163d22016-06-13 12:26:45 -07001186}
1187
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001188void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -05001189 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001190 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001191}
Jim Van Verth6f449692017-02-14 15:16:46 -05001192
Christopher Dalton443ec1b2017-02-24 13:22:53 -07001193void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -04001194 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -07001195
brianosman622c8d52016-05-10 06:50:49 -07001196 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -07001197
Brian Osmand67e5182017-12-08 16:46:09 -05001198 this->drawImGui();
Chris Dalton89305752018-11-01 10:52:34 -06001199
1200 if (GrContext* ctx = fWindow->getGrContext()) {
1201 // Clean out cache items that haven't been used in more than 10 seconds.
1202 ctx->performDeferredCleanup(std::chrono::seconds(10));
1203 }
jvanverth3d6ed3a2016-04-07 11:09:51 -07001204}
1205
Ben Wagnera1915972018-08-09 15:06:19 -04001206void Viewer::onResize(int width, int height) {
Jim Van Verthb35c6552018-08-13 10:42:17 -04001207 if (fCurrentSlide >= 0) {
1208 fSlides[fCurrentSlide]->resize(width, height);
1209 }
Ben Wagnera1915972018-08-09 15:06:19 -04001210}
1211
Florin Malitacefc1b92018-02-19 21:43:47 -05001212SkPoint Viewer::mapEvent(float x, float y) {
1213 const auto m = this->computeMatrix();
1214 SkMatrix inv;
1215
1216 SkAssertResult(m.invert(&inv));
1217
1218 return inv.mapXY(x, y);
1219}
1220
jvanverth814e38d2016-06-06 08:48:47 -07001221bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -04001222 if (GestureDevice::kMouse == fGestureDevice) {
1223 return false;
1224 }
Florin Malitacefc1b92018-02-19 21:43:47 -05001225
1226 const auto slidePt = this->mapEvent(x, y);
1227 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) {
1228 fWindow->inval();
1229 return true;
1230 }
1231
liyuqiand3cdbca2016-05-17 12:44:20 -07001232 void* castedOwner = reinterpret_cast<void*>(owner);
1233 switch (state) {
1234 case Window::kUp_InputState: {
1235 fGesture.touchEnd(castedOwner);
Jim Van Verth234e5a22018-07-23 13:46:01 -04001236#if defined(SK_BUILD_FOR_IOS)
1237 // TODO: move IOS swipe detection higher up into the platform code
1238 SkPoint dir;
1239 if (fGesture.isFling(&dir)) {
1240 // swiping left or right
1241 if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) {
1242 if (dir.fX < 0) {
1243 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ?
1244 fCurrentSlide + 1 : 0);
1245 } else {
1246 this->setCurrentSlide(fCurrentSlide > 0 ?
1247 fCurrentSlide - 1 : fSlides.count() - 1);
1248 }
1249 }
1250 fGesture.reset();
1251 }
1252#endif
liyuqiand3cdbca2016-05-17 12:44:20 -07001253 break;
1254 }
1255 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001256 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001257 break;
1258 }
1259 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -04001260 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -07001261 break;
1262 }
1263 }
Brian Osmanb53f48c2017-06-07 10:00:30 -04001264 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -07001265 fWindow->inval();
1266 return true;
1267}
1268
Brian Osman80fc07e2017-12-08 16:45:43 -05001269bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -05001270 if (GestureDevice::kTouch == fGestureDevice) {
1271 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -05001272 }
Brian Osman16c81a12017-12-20 11:58:34 -05001273
Florin Malitacefc1b92018-02-19 21:43:47 -05001274 const auto slidePt = this->mapEvent(x, y);
1275 if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) {
1276 fWindow->inval();
1277 return true;
Brian Osman16c81a12017-12-20 11:58:34 -05001278 }
1279
1280 switch (state) {
1281 case Window::kUp_InputState: {
1282 fGesture.touchEnd(nullptr);
1283 break;
1284 }
1285 case Window::kDown_InputState: {
1286 fGesture.touchBegin(nullptr, x, y);
1287 break;
1288 }
1289 case Window::kMove_InputState: {
1290 fGesture.touchMoved(nullptr, x, y);
1291 break;
1292 }
1293 }
1294 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
1295
1296 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
1297 fWindow->inval();
1298 }
Jim Van Verthe7705782017-05-04 14:00:59 -04001299 return true;
1300}
1301
Brian Osmana109e392017-02-24 09:49:14 -05001302static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1303 const ImVec2& pos, const ImVec2& size) {
1304 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1305 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1306
1307 // Invisible 10x10 button
1308 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1309 ImGui::InvisibleButton(label, ImVec2(10, 10));
1310
1311 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1312 ImGuiIO& io = ImGui::GetIO();
1313 // Normalized mouse position, relative to our gamut box
1314 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1315 // Clamp to edge of box, convert back to primary scale
1316 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1317 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1318 }
1319
1320 if (ImGui::IsItemHovered()) {
1321 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1322 }
1323
1324 // Return screen coordinates for the caller. We could just return center here, but we'd have
1325 // one frame of lag during drag.
1326 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1327}
1328
1329static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1330 ImDrawList* drawList = ImGui::GetWindowDrawList();
1331
1332 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1333 // width, and scale the height to maintain aspect ratio.
1334 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1335 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1336 ImVec2 pos = ImGui::GetCursorScreenPos();
1337
1338 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1339 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1340 // Magic numbers are pixel locations of the origin and upper-right corner.
1341 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1342 ImVec2(242, 61), ImVec2(1897, 1922));
Brian Osmana109e392017-02-24 09:49:14 -05001343
1344 // Primary markers
1345 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1346 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1347 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1348 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1349
1350 // Gamut triangle
1351 drawList->AddCircle(r, 5.0f, 0xFF000040);
1352 drawList->AddCircle(g, 5.0f, 0xFF004000);
1353 drawList->AddCircle(b, 5.0f, 0xFF400000);
1354 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1355 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1356
1357 // Re-position cursor immediate after the diagram for subsequent controls
Brian Osman9bb47cf2018-04-26 15:55:00 -04001358 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1359}
1360
1361static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p,
1362 const ImVec2& pos, const ImVec2& size, bool* dragging) {
1363 // Transform points ([0, 0] - [1.0, 1.0]) to screen coords
1364 ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1365
1366 // Invisible 10x10 button
1367 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1368 ImGui::InvisibleButton(label, ImVec2(10, 10));
1369
1370 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1371 ImGuiIO& io = ImGui::GetIO();
1372 // Normalized mouse position, relative to our gamut box
1373 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1374 // Clamp to edge of box
1375 p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f);
1376 p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f);
1377 *dragging = true;
1378 }
1379
1380 // Return screen coordinates for the caller. We could just return center here, but we'd have
1381 // one frame of lag during drag.
1382 return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y);
1383}
1384
Ben Wagner3627d2e2018-06-26 14:23:20 -04001385static bool ImGui_DragLocation(SkPoint* pt) {
1386 ImDrawList* drawList = ImGui::GetWindowDrawList();
1387
1388 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1389 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1390 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1391 ImVec2 pos = ImGui::GetCursorScreenPos();
1392
1393 // Background rectangle
1394 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1395
1396 // Location marker
1397 bool dragging = false;
1398 ImVec2 tl = ImGui_DragPoint("SL", pt + 0, pos, size, &dragging);
1399 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1400
1401 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1402 ImGui::Spacing();
1403
1404 return dragging;
1405}
1406
Brian Osman9bb47cf2018-04-26 15:55:00 -04001407static bool ImGui_DragQuad(SkPoint* pts) {
1408 ImDrawList* drawList = ImGui::GetWindowDrawList();
1409
1410 // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio.
1411 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1412 ImVec2 size = ImVec2(canvasWidth, canvasWidth);
1413 ImVec2 pos = ImGui::GetCursorScreenPos();
1414
1415 // Background rectangle
1416 drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128));
1417
1418 // Corner markers
1419 bool dragging = false;
1420 ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging);
1421 ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging);
1422 ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging);
1423 ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging);
1424
1425 // Draw markers and quad
1426 drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF);
1427 drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF);
1428 drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF);
1429 drawList->AddCircle(br, 5.0f, 0xFFFFFFFF);
1430 drawList->AddLine(tl, tr, 0xFFFFFFFF);
1431 drawList->AddLine(tr, br, 0xFFFFFFFF);
1432 drawList->AddLine(br, bl, 0xFFFFFFFF);
1433 drawList->AddLine(bl, tl, 0xFFFFFFFF);
1434
1435 ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y));
1436 ImGui::Spacing();
1437
1438 return dragging;
Brian Osmana109e392017-02-24 09:49:14 -05001439}
1440
Brian Osmand67e5182017-12-08 16:46:09 -05001441void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001442 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1443 if (fShowImGuiTestWindow) {
Brian Osman7197e052018-06-29 14:30:48 -04001444 ImGui::ShowDemoWindow(&fShowImGuiTestWindow);
Brian Osman79086b92017-02-10 13:36:16 -05001445 }
1446
1447 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001448 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1449 // always visible, we can end up in a layout feedback loop.
Brian Osman7197e052018-06-29 14:30:48 -04001450 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001451 DisplayParams params = fWindow->getRequestedDisplayParams();
1452 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001453 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1454 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001455 if (ImGui::CollapsingHeader("Backend")) {
1456 int newBackend = static_cast<int>(fBackendType);
1457 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1458 ImGui::SameLine();
1459 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001460#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1461 ImGui::SameLine();
1462 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1463#endif
Brian Osman621491e2017-02-28 15:45:01 -05001464#if defined(SK_VULKAN)
1465 ImGui::SameLine();
1466 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1467#endif
1468 if (newBackend != fBackendType) {
1469 fDeferredActions.push_back([=]() {
1470 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1471 });
1472 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001473
Brian Salomon99a33902017-03-07 15:16:34 -05001474 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001475 bool* wire = &params.fGrContextOptions.fWireframeMode;
1476 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1477 paramsChanged = true;
1478 }
Brian Salomon99a33902017-03-07 15:16:34 -05001479
Brian Osman28b12522017-03-08 17:10:24 -05001480 if (ctx) {
1481 int sampleCount = fWindow->sampleCount();
1482 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001483 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001484 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1485 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1486 ImGui::RadioButton("16", &sampleCount, 16);
1487
1488 if (sampleCount != params.fMSAASampleCount) {
1489 params.fMSAASampleCount = sampleCount;
1490 paramsChanged = true;
1491 }
1492 }
1493
Ben Wagner37c54032018-04-13 14:30:23 -04001494 int pixelGeometryIdx = 0;
1495 if (fPixelGeometryOverrides) {
1496 pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1;
1497 }
1498 if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx,
1499 "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0"))
1500 {
1501 uint32_t flags = params.fSurfaceProps.flags();
1502 if (pixelGeometryIdx == 0) {
1503 fPixelGeometryOverrides = false;
1504 params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1505 } else {
1506 fPixelGeometryOverrides = true;
1507 SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1);
1508 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1509 }
1510 paramsChanged = true;
1511 }
1512
1513 bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts();
1514 if (ImGui::Checkbox("DFT", &useDFT)) {
1515 uint32_t flags = params.fSurfaceProps.flags();
1516 if (useDFT) {
1517 flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1518 } else {
1519 flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
1520 }
1521 SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry();
1522 params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry);
1523 paramsChanged = true;
1524 }
1525
Brian Osman8a9de3d2017-03-01 14:59:05 -05001526 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001527 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001528 auto prButton = [&](GpuPathRenderers x) {
1529 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001530 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1531 params.fGrContextOptions.fGpuPathRenderers = x;
1532 paramsChanged = true;
1533 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001534 }
1535 };
1536
1537 if (!ctx) {
1538 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001539 } else if (fWindow->sampleCount() > 1) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001540 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001541 if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001542 prButton(GpuPathRenderers::kStencilAndCover);
1543 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001544 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001545 prButton(GpuPathRenderers::kNone);
1546 } else {
1547 prButton(GpuPathRenderers::kAll);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001548 if (GrCoverageCountingPathRenderer::IsSupported(
1549 *ctx->contextPriv().caps())) {
Chris Dalton1a325d22017-07-14 15:17:41 -06001550 prButton(GpuPathRenderers::kCoverageCounting);
1551 }
Jim Van Verth83010462017-03-16 08:45:39 -04001552 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001553 prButton(GpuPathRenderers::kTessellating);
1554 prButton(GpuPathRenderers::kNone);
1555 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001556 ImGui::TreePop();
1557 }
Brian Osman621491e2017-02-28 15:45:01 -05001558 }
1559
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001560 if (ImGui::CollapsingHeader("Transform")) {
1561 float zoom = fZoomLevel;
1562 if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) {
1563 fZoomLevel = zoom;
1564 this->preTouchMatrixChanged();
1565 paramsChanged = true;
1566 }
1567 float deg = fRotation;
Ben Wagnercb139352018-05-04 10:33:04 -04001568 if (ImGui::SliderFloat("Rotate", &deg, -30, 360, "%.3f deg")) {
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001569 fRotation = deg;
1570 this->preTouchMatrixChanged();
1571 paramsChanged = true;
1572 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001573 if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) {
1574 if (ImGui_DragLocation(&fOffset)) {
1575 this->preTouchMatrixChanged();
1576 paramsChanged = true;
1577 }
Ben Wagner897dfa22018-08-09 15:18:46 -04001578 } else if (fOffset != SkVector{0.5f, 0.5f}) {
1579 this->preTouchMatrixChanged();
1580 paramsChanged = true;
1581 fOffset = {0.5f, 0.5f};
Ben Wagner3627d2e2018-06-26 14:23:20 -04001582 }
Brian Osmane9ed0f02018-11-26 14:50:05 -05001583 if (ImGui::CollapsingHeader("Tiling")) {
1584 ImGui::Checkbox("Enable", &fTiled);
1585 ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries);
1586 ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f);
1587 ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f);
1588 }
Brian Osman805a7272018-05-02 15:40:20 -04001589 int perspectiveMode = static_cast<int>(fPerspectiveMode);
1590 if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) {
1591 fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode);
Brian Osman9bb47cf2018-04-26 15:55:00 -04001592 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001593 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001594 }
Ben Wagner3627d2e2018-06-26 14:23:20 -04001595 if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) {
Brian Osman9bb47cf2018-04-26 15:55:00 -04001596 this->preTouchMatrixChanged();
Ben Wagner3627d2e2018-06-26 14:23:20 -04001597 paramsChanged = true;
Brian Osman9bb47cf2018-04-26 15:55:00 -04001598 }
Ben Wagnerd02a74d2018-04-23 12:55:06 -04001599 }
1600
Ben Wagnera580fb32018-04-17 11:16:32 -04001601 if (ImGui::CollapsingHeader("Paint")) {
1602 int hintingIdx = 0;
Mike Reed3ae47332019-01-04 10:11:46 -05001603 if (fFontOverrides.fHinting) {
1604 hintingIdx = static_cast<unsigned>(fFont.getHinting()) + 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001605 }
1606 if (ImGui::Combo("Hinting", &hintingIdx,
1607 "Default\0None\0Slight\0Normal\0Full\0\0"))
1608 {
1609 if (hintingIdx == 0) {
Mike Reed3ae47332019-01-04 10:11:46 -05001610 fFontOverrides.fHinting = false;
1611 fFont.setHinting(kNo_SkFontHinting);
Ben Wagnera580fb32018-04-17 11:16:32 -04001612 } else {
Mike Reed3ae47332019-01-04 10:11:46 -05001613 fFontOverrides.fHinting = true;
Mike Reed9edbf422018-11-07 19:54:33 -05001614 SkFontHinting hinting = SkTo<SkFontHinting>(hintingIdx - 1);
Mike Reed3ae47332019-01-04 10:11:46 -05001615 fFont.setHinting(hinting);
Ben Wagnera580fb32018-04-17 11:16:32 -04001616 }
1617 paramsChanged = true;
1618 }
1619
1620 int aliasIdx = 0;
Mike Reed5d052da2019-01-21 10:40:43 -05001621 if (fPaintOverrides.fFlags & kAntiAlias_PaintFlag) {
Ben Wagnera580fb32018-04-17 11:16:32 -04001622 aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1;
1623 }
1624 if (ImGui::Combo("Anti-Alias", &aliasIdx,
1625 "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0"
1626 "DeltaAAEnabled\0DeltaAAForced\0\0"))
1627 {
1628 gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA;
1629 gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA;
1630 gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA;
1631 gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA;
1632 if (aliasIdx == 0) {
1633 fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias;
Mike Reed5d052da2019-01-21 10:40:43 -05001634 fPaintOverrides.fFlags &= ~kAntiAlias_PaintFlag;
Ben Wagnera580fb32018-04-17 11:16:32 -04001635 } else {
Mike Reed5d052da2019-01-21 10:40:43 -05001636 fPaintOverrides.fFlags |= kAntiAlias_PaintFlag;
Ben Wagnera580fb32018-04-17 11:16:32 -04001637 fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1);
1638 fPaint.setAntiAlias(aliasIdx > 1);
1639 switch (fPaintOverrides.fAntiAlias) {
1640 case SkPaintFields::AntiAliasState::Alias:
1641 break;
1642 case SkPaintFields::AntiAliasState::Normal:
1643 break;
1644 case SkPaintFields::AntiAliasState::AnalyticAAEnabled:
1645 gSkUseAnalyticAA = true;
1646 gSkForceAnalyticAA = false;
1647 gSkUseDeltaAA = gSkForceDeltaAA = false;
1648 break;
1649 case SkPaintFields::AntiAliasState::AnalyticAAForced:
1650 gSkUseAnalyticAA = gSkForceAnalyticAA = true;
1651 gSkUseDeltaAA = gSkForceDeltaAA = false;
1652 break;
1653 case SkPaintFields::AntiAliasState::DeltaAAEnabled:
1654 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1655 gSkUseDeltaAA = true;
1656 gSkForceDeltaAA = false;
1657 break;
1658 case SkPaintFields::AntiAliasState::DeltaAAForced:
1659 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
1660 gSkUseDeltaAA = gSkForceDeltaAA = true;
1661 break;
1662 }
1663 }
1664 paramsChanged = true;
1665 }
1666
Ben Wagner99a78dc2018-05-09 18:23:51 -04001667 auto paintFlag = [this, &paramsChanged](const char* label, const char* items,
Mike Reed5d052da2019-01-21 10:40:43 -05001668 PaintFlags flag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001669 bool (SkPaint::* isFlag)() const,
1670 void (SkPaint::* setFlag)(bool) )
Ben Wagnera580fb32018-04-17 11:16:32 -04001671 {
Ben Wagner99a78dc2018-05-09 18:23:51 -04001672 int itemIndex = 0;
1673 if (fPaintOverrides.fFlags & flag) {
1674 itemIndex = (fPaint.*isFlag)() ? 2 : 1;
Ben Wagnera580fb32018-04-17 11:16:32 -04001675 }
Ben Wagner99a78dc2018-05-09 18:23:51 -04001676 if (ImGui::Combo(label, &itemIndex, items)) {
1677 if (itemIndex == 0) {
1678 fPaintOverrides.fFlags &= ~flag;
1679 } else {
1680 fPaintOverrides.fFlags |= flag;
1681 (fPaint.*setFlag)(itemIndex == 2);
1682 }
1683 paramsChanged = true;
1684 }
1685 };
Ben Wagnera580fb32018-04-17 11:16:32 -04001686
Ben Wagner99a78dc2018-05-09 18:23:51 -04001687 paintFlag("Dither",
1688 "Default\0No Dither\0Dither\0\0",
Mike Reed5d052da2019-01-21 10:40:43 -05001689 kDither_PaintFlag,
Ben Wagner99a78dc2018-05-09 18:23:51 -04001690 &SkPaint::isDither, &SkPaint::setDither);
Mike Reed3ae47332019-01-04 10:11:46 -05001691 ImGui::Checkbox("Override TextSize", &fFontOverrides.fTextSize);
1692 if (fFontOverrides.fTextSize) {
1693 ImGui::DragFloat2("TextRange", fFontOverrides.fTextSizeRange,
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001694 0.001f, -10.0f, 300.0f, "%.6f", 2.0f);
Mike Reed3ae47332019-01-04 10:11:46 -05001695 float textSize = fFont.getSize();
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001696 if (ImGui::DragFloat("TextSize", &textSize, 0.001f,
Mike Reed3ae47332019-01-04 10:11:46 -05001697 fFontOverrides.fTextSizeRange[0],
1698 fFontOverrides.fTextSizeRange[1],
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001699 "%.6f", 2.0f))
1700 {
Mike Reed3ae47332019-01-04 10:11:46 -05001701 fFont.setSize(textSize);
Ben Wagnerd2ae4df2018-06-07 17:54:07 -04001702 this->preTouchMatrixChanged();
1703 paramsChanged = true;
1704 }
1705 }
Ben Wagnera580fb32018-04-17 11:16:32 -04001706 }
1707
Mike Reed81f60ec2018-05-15 10:09:52 -04001708 {
1709 SkMetaData controls;
1710 if (fSlides[fCurrentSlide]->onGetControls(&controls)) {
1711 if (ImGui::CollapsingHeader("Current Slide")) {
1712 SkMetaData::Iter iter(controls);
1713 const char* name;
1714 SkMetaData::Type type;
1715 int count;
Brian Osman61fb4bb2018-08-03 11:14:02 -04001716 while ((name = iter.next(&type, &count)) != nullptr) {
Mike Reed81f60ec2018-05-15 10:09:52 -04001717 if (type == SkMetaData::kScalar_Type) {
1718 float val[3];
1719 SkASSERT(count == 3);
1720 controls.findScalars(name, &count, val);
1721 if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) {
1722 controls.setScalars(name, 3, val);
Mike Reed81f60ec2018-05-15 10:09:52 -04001723 }
1724 }
1725 }
Brian Osman61fb4bb2018-08-03 11:14:02 -04001726 fSlides[fCurrentSlide]->onSetControls(controls);
Mike Reed81f60ec2018-05-15 10:09:52 -04001727 }
1728 }
1729 }
1730
Ben Wagner7a3c6742018-04-23 10:01:07 -04001731 if (fShowSlidePicker) {
1732 ImGui::SetNextTreeNodeOpen(true);
1733 }
Brian Osman79086b92017-02-10 13:36:16 -05001734 if (ImGui::CollapsingHeader("Slide")) {
1735 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001736 static ImVector<const char*> filteredSlideNames;
1737 static ImVector<int> filteredSlideIndices;
1738
Brian Osmanfce09c52017-11-14 15:32:20 -05001739 if (fShowSlidePicker) {
1740 ImGui::SetKeyboardFocusHere();
1741 fShowSlidePicker = false;
1742 }
1743
Brian Osman79086b92017-02-10 13:36:16 -05001744 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001745 filteredSlideNames.clear();
1746 filteredSlideIndices.clear();
1747 int filteredIndex = 0;
1748 for (int i = 0; i < fSlides.count(); ++i) {
1749 const char* slideName = fSlides[i]->getName().c_str();
1750 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1751 if (i == fCurrentSlide) {
1752 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001753 }
Brian Osmanf479e422017-11-08 13:11:36 -05001754 filteredSlideNames.push_back(slideName);
1755 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001756 }
Brian Osman79086b92017-02-10 13:36:16 -05001757 }
Brian Osmanf479e422017-11-08 13:11:36 -05001758
Brian Osmanf479e422017-11-08 13:11:36 -05001759 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1760 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001761 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001762 }
1763 }
Brian Osmana109e392017-02-24 09:49:14 -05001764
1765 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001766 ColorMode newMode = fColorMode;
1767 auto cmButton = [&](ColorMode mode, const char* label) {
1768 if (ImGui::RadioButton(label, mode == fColorMode)) {
1769 newMode = mode;
1770 }
1771 };
1772
1773 cmButton(ColorMode::kLegacy, "Legacy 8888");
Brian Osman03115dc2018-11-26 13:55:19 -05001774 cmButton(ColorMode::kColorManaged8888, "Color Managed 8888");
1775 cmButton(ColorMode::kColorManagedF16, "Color Managed F16");
Brian Osman92004802017-03-06 11:47:26 -05001776
1777 if (newMode != fColorMode) {
Brian Osman03115dc2018-11-26 13:55:19 -05001778 this->setColorMode(newMode);
Brian Osmana109e392017-02-24 09:49:14 -05001779 }
1780
1781 // Pick from common gamuts:
1782 int primariesIdx = 4; // Default: Custom
1783 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1784 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1785 primariesIdx = i;
1786 break;
1787 }
1788 }
1789
Brian Osman03115dc2018-11-26 13:55:19 -05001790 // Let user adjust the gamma
Brian Osman82ebe042019-01-04 17:03:00 -05001791 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f);
Brian Osmanfdab5762017-11-09 10:27:55 -05001792
Brian Osmana109e392017-02-24 09:49:14 -05001793 if (ImGui::Combo("Primaries", &primariesIdx,
1794 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1795 if (primariesIdx >= 0 && primariesIdx <= 3) {
1796 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1797 }
1798 }
1799
1800 // Allow direct editing of gamut
1801 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1802 }
Brian Osman207d4102019-01-10 09:40:58 -05001803
1804 if (ImGui::CollapsingHeader("Animation")) {
1805 bool isPaused = fAnimTimer.isPaused();
1806 if (ImGui::Checkbox("Pause", &isPaused)) {
1807 fAnimTimer.togglePauseResume();
1808 }
Brian Osman707d2022019-01-10 11:27:34 -05001809
1810 float speed = fAnimTimer.getSpeed();
1811 if (ImGui::DragFloat("Speed", &speed, 0.1f)) {
1812 fAnimTimer.setSpeed(speed);
1813 }
Brian Osman207d4102019-01-10 09:40:58 -05001814 }
Brian Osman79086b92017-02-10 13:36:16 -05001815 }
Brian Salomon99a33902017-03-07 15:16:34 -05001816 if (paramsChanged) {
1817 fDeferredActions.push_back([=]() {
1818 fWindow->setRequestedDisplayParams(params);
1819 fWindow->inval();
1820 this->updateTitle();
1821 });
1822 }
Brian Osman79086b92017-02-10 13:36:16 -05001823 ImGui::End();
1824 }
1825
Brian Osmanf6877092017-02-13 09:39:57 -05001826 if (fShowZoomWindow && fLastImage) {
Brian Osman7197e052018-06-29 14:30:48 -04001827 ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
1828 if (ImGui::Begin("Zoom", &fShowZoomWindow)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001829 static int zoomFactor = 8;
1830 if (ImGui::Button("<<")) {
1831 zoomFactor = SkTMax(zoomFactor / 2, 4);
1832 }
1833 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1834 if (ImGui::Button(">>")) {
1835 zoomFactor = SkTMin(zoomFactor * 2, 32);
1836 }
Brian Osmanf6877092017-02-13 09:39:57 -05001837
Ben Wagner3627d2e2018-06-26 14:23:20 -04001838 if (!fZoomWindowFixed) {
1839 ImVec2 mousePos = ImGui::GetMousePos();
1840 fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y);
1841 }
1842 SkScalar x = fZoomWindowLocation.x();
1843 SkScalar y = fZoomWindowLocation.y();
1844 int xInt = SkScalarRoundToInt(x);
1845 int yInt = SkScalarRoundToInt(y);
Brian Osmanf6877092017-02-13 09:39:57 -05001846 ImVec2 avail = ImGui::GetContentRegionAvail();
1847
Brian Osmanead517d2017-11-13 15:36:36 -05001848 uint32_t pixel = 0;
1849 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001850 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) {
Brian Osmanead517d2017-11-13 15:36:36 -05001851 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001852 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
Ben Wagner3627d2e2018-06-26 14:23:20 -04001853 xInt, yInt,
Brian Osman07b56b22017-11-21 14:59:31 -05001854 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001855 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1856 }
1857
Brian Osmand67e5182017-12-08 16:46:09 -05001858 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001859 // Translate so the region of the image that's under the mouse cursor is centered
1860 // in the zoom canvas:
1861 c->scale(zoomFactor, zoomFactor);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001862 c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f,
1863 avail.y * 0.5f / zoomFactor - y - 0.5f);
Brian Osmanead517d2017-11-13 15:36:36 -05001864 c->drawImage(this->fLastImage, 0, 0);
1865
1866 SkPaint outline;
1867 outline.setStyle(SkPaint::kStroke_Style);
Ben Wagner3627d2e2018-06-26 14:23:20 -04001868 c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline);
Brian Osmanead517d2017-11-13 15:36:36 -05001869 });
Brian Osmanf6877092017-02-13 09:39:57 -05001870 }
1871
1872 ImGui::End();
1873 }
Brian Osman79086b92017-02-10 13:36:16 -05001874}
1875
liyuqian2edb0f42016-07-06 14:11:32 -07001876void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001877 for (int i = 0; i < fDeferredActions.count(); ++i) {
1878 fDeferredActions[i]();
1879 }
1880 fDeferredActions.reset();
1881
Brian Osman56a24812017-12-19 11:15:16 -05001882 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001883 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001884 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001885 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001886
Brian Osman79086b92017-02-10 13:36:16 -05001887 ImGuiIO& io = ImGui::GetIO();
Brian Osmanffee60f2018-08-03 13:03:19 -04001888 // ImGui always has at least one "active" window, which is the default "Debug" window. It may
1889 // not be visible, though. So we need to redraw if there is at least one visible window, or
1890 // more than one active window. Newly created windows are active but not visible for one frame
1891 // while they determine their layout and sizing.
1892 if (animateWantsInval || fStatsLayer.getActive() || fRefresh ||
1893 io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) {
jvanverthc265a922016-04-08 12:51:45 -07001894 fWindow->inval();
1895 }
jvanverth9f372462016-04-06 06:08:59 -07001896}
liyuqiane5a6cd92016-05-27 08:52:52 -07001897
Florin Malitab632df72018-06-18 21:23:06 -04001898template <typename OptionsFunc>
1899static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value,
1900 OptionsFunc&& optionsFunc) {
1901 writer.beginObject();
1902 {
1903 writer.appendString(kName , name);
1904 writer.appendString(kValue, value);
1905
1906 writer.beginArray(kOptions);
1907 {
1908 optionsFunc(writer);
1909 }
1910 writer.endArray();
1911 }
1912 writer.endObject();
1913}
1914
1915
liyuqiane5a6cd92016-05-27 08:52:52 -07001916void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001917 if (!fWindow) {
1918 return;
1919 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001920 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001921 return; // Surface hasn't been created yet.
1922 }
1923
Florin Malitab632df72018-06-18 21:23:06 -04001924 SkDynamicMemoryWStream memStream;
1925 SkJSONWriter writer(&memStream);
1926 writer.beginArray();
1927
liyuqianb73c24b2016-06-03 08:47:23 -07001928 // Slide state
Florin Malitab632df72018-06-18 21:23:06 -04001929 WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(),
1930 [this](SkJSONWriter& writer) {
1931 for(const auto& slide : fSlides) {
1932 writer.appendString(slide->getName().c_str());
1933 }
1934 });
liyuqiane5a6cd92016-05-27 08:52:52 -07001935
liyuqianb73c24b2016-06-03 08:47:23 -07001936 // Backend state
Florin Malitab632df72018-06-18 21:23:06 -04001937 WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType],
1938 [](SkJSONWriter& writer) {
1939 for (const auto& str : kBackendTypeStrings) {
1940 writer.appendString(str);
1941 }
1942 });
liyuqiane5a6cd92016-05-27 08:52:52 -07001943
csmartdalton578f0642017-02-24 16:04:47 -07001944 // MSAA state
Florin Malitab632df72018-06-18 21:23:06 -04001945 const auto countString = SkStringPrintf("%d", fWindow->sampleCount());
1946 WriteStateObject(writer, kMSAAStateName, countString.c_str(),
1947 [this](SkJSONWriter& writer) {
1948 writer.appendS32(0);
1949
1950 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1951 return;
1952 }
1953
1954 for (int msaa : {4, 8, 16}) {
1955 writer.appendS32(msaa);
1956 }
1957 });
csmartdalton578f0642017-02-24 16:04:47 -07001958
csmartdalton61cd31a2017-02-27 17:00:53 -07001959 // Path renderer state
1960 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Florin Malitab632df72018-06-18 21:23:06 -04001961 WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(),
1962 [this](SkJSONWriter& writer) {
1963 const GrContext* ctx = fWindow->getGrContext();
1964 if (!ctx) {
1965 writer.appendString("Software");
1966 } else {
1967 const auto* caps = ctx->contextPriv().caps();
1968
Florin Malitab632df72018-06-18 21:23:06 -04001969 writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str());
1970 if (fWindow->sampleCount() > 1) {
1971 if (caps->shaderCaps()->pathRenderingSupport()) {
1972 writer.appendString(
1973 gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str());
1974 }
1975 } else {
1976 if(GrCoverageCountingPathRenderer::IsSupported(*caps)) {
1977 writer.appendString(
1978 gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str());
1979 }
1980 writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str());
1981 }
1982 writer.appendString(
1983 gPathRendererNames[GpuPathRenderers::kTessellating].c_str());
1984 writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str());
1985 }
1986 });
csmartdalton61cd31a2017-02-27 17:00:53 -07001987
liyuqianb73c24b2016-06-03 08:47:23 -07001988 // Softkey state
Florin Malitab632df72018-06-18 21:23:06 -04001989 WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint,
1990 [this](SkJSONWriter& writer) {
1991 writer.appendString(kSoftkeyHint);
1992 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1993 writer.appendString(softkey.c_str());
1994 }
1995 });
liyuqianb73c24b2016-06-03 08:47:23 -07001996
Florin Malitab632df72018-06-18 21:23:06 -04001997 writer.endArray();
1998 writer.flush();
liyuqiane5a6cd92016-05-27 08:52:52 -07001999
Florin Malitab632df72018-06-18 21:23:06 -04002000 auto data = memStream.detachAsData();
2001
2002 // TODO: would be cool to avoid this copy
2003 const SkString cstring(static_cast<const char*>(data->data()), data->size());
2004
2005 fWindow->setUIState(cstring.c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07002006}
2007
2008void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07002009 // For those who will add more features to handle the state change in this function:
2010 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
2011 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
2012 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07002013 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05002014 for (int i = 0; i < fSlides.count(); ++i) {
2015 if (fSlides[i]->getName().equals(stateValue)) {
2016 this->setCurrentSlide(i);
2017 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07002018 }
liyuqiane5a6cd92016-05-27 08:52:52 -07002019 }
Florin Malitaab99c342018-01-16 16:23:03 -05002020
2021 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07002022 } else if (stateName.equals(kBackendStateName)) {
2023 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
2024 if (stateValue.equals(kBackendTypeStrings[i])) {
2025 if (fBackendType != i) {
2026 fBackendType = (sk_app::Window::BackendType)i;
2027 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05002028 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07002029 }
2030 break;
2031 }
2032 }
csmartdalton578f0642017-02-24 16:04:47 -07002033 } else if (stateName.equals(kMSAAStateName)) {
2034 DisplayParams params = fWindow->getRequestedDisplayParams();
2035 int sampleCount = atoi(stateValue.c_str());
2036 if (sampleCount != params.fMSAASampleCount) {
2037 params.fMSAASampleCount = sampleCount;
2038 fWindow->setRequestedDisplayParams(params);
2039 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002040 this->updateTitle();
2041 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002042 }
2043 } else if (stateName.equals(kPathRendererStateName)) {
2044 DisplayParams params = fWindow->getRequestedDisplayParams();
2045 for (const auto& pair : gPathRendererNames) {
2046 if (pair.second == stateValue.c_str()) {
2047 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
2048 params.fGrContextOptions.fGpuPathRenderers = pair.first;
2049 fWindow->setRequestedDisplayParams(params);
2050 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05002051 this->updateTitle();
2052 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07002053 }
2054 break;
2055 }
csmartdalton578f0642017-02-24 16:04:47 -07002056 }
liyuqianb73c24b2016-06-03 08:47:23 -07002057 } else if (stateName.equals(kSoftkeyStateName)) {
2058 if (!stateValue.equals(kSoftkeyHint)) {
2059 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05002060 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07002061 }
liyuqian2edb0f42016-07-06 14:11:32 -07002062 } else if (stateName.equals(kRefreshStateName)) {
2063 // This state is actually NOT in the UI state.
2064 // We use this to allow Android to quickly set bool fRefresh.
2065 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07002066 } else {
2067 SkDebugf("Unknown stateName: %s", stateName.c_str());
2068 }
2069}
Brian Osman79086b92017-02-10 13:36:16 -05002070
2071bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002072 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05002073}
2074
2075bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05002076 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05002077 fWindow->inval();
2078 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05002079 } else {
2080 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05002081 }
Brian Osman79086b92017-02-10 13:36:16 -05002082}