blob: edf37cd7a087d412f0dce06fd0ec672844182bdf [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
jvanverth34524262016-05-04 13:49:13 -07008#include "Viewer.h"
jvanverth9f372462016-04-06 06:08:59 -07009
jvanverth2bb3b6d2016-04-08 07:24:09 -070010#include "GMSlide.h"
liyuqian6f163d22016-06-13 12:26:45 -070011#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050012#include "Resources.h"
jvanverthc7027ab2016-06-16 09:52:35 -070013#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070014#include "SKPSlide.h"
jvanverth9f372462016-04-06 06:08:59 -070015
csmartdalton61cd31a2017-02-27 17:00:53 -070016#include "GrContext.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070017#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050018#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040019#include "SkColorSpaceXformCanvas.h"
Brian Osman2dd96932016-10-18 15:33:53 -040020#include "SkCommandLineFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070021#include "SkCommonFlagsGpu.h"
Brian Osman53136aa2017-07-20 15:43:35 -040022#include "SkEventTracingPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040023#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050024#include "SkImagePriv.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070025#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050026#include "SkOSPath.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050027#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040028#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070029#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070030#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050031#include "SkTaskGroup.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050032#include "SkThreadedBMPDevice.h"
liyuqian2edb0f42016-07-06 14:11:32 -070033#include "SkTime.h"
jvanverth9f372462016-04-06 06:08:59 -070034
Brian Osman79086b92017-02-10 13:36:16 -050035#include "imgui.h"
36
Chris Dalton1a325d22017-07-14 15:17:41 -060037#include "ccpr/GrCoverageCountingPathRenderer.h"
38
csmartdalton578f0642017-02-24 16:04:47 -070039#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070040#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070041
jvanverth34524262016-05-04 13:49:13 -070042using namespace sk_app;
43
csmartdalton61cd31a2017-02-27 17:00:53 -070044static std::map<GpuPathRenderers, std::string> gPathRendererNames;
45
jvanverth9f372462016-04-06 06:08:59 -070046Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070047 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070048}
49
Brian Osman2dd96932016-10-18 15:33:53 -040050static DEFINE_string2(match, m, nullptr,
jvanverth2bb3b6d2016-04-08 07:24:09 -070051 "[~][^]substring[$] [...] of bench name to run.\n"
52 "Multiple matches may be separated by spaces.\n"
53 "~ causes a matching bench to always be skipped\n"
54 "^ requires the start of the bench to match\n"
55 "$ requires the end of the bench to match\n"
56 "^ and $ requires an exact match\n"
57 "If a bench does not match any list entry,\n"
58 "it is skipped unless some list entry starts with ~");
bsalomon6c471f72016-07-26 12:56:32 -070059
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060060static DEFINE_string(slide, "", "Start on this sample.");
61static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050062
bsalomon6c471f72016-07-26 12:56:32 -070063#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070064# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070065#else
66# define BACKENDS_STR "\"sw\" and \"gl\""
67#endif
68
liyuqian71491dc2016-06-09 12:02:34 -070069#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -040070static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
71static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070072#else
Brian Osman2dd96932016-10-18 15:33:53 -040073static DEFINE_string(skps, "skps", "Directory to read skps from.");
74static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070075#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -070076
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
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060079static DEFINE_int32(msaa, 0, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070080
Brian Osman53136aa2017-07-20 15:43:35 -040081DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050082
Brian Salomon194db172017-08-17 14:37:06 -040083const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070084 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040085#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
86 "ANGLE",
87#endif
jvanverth063ece72016-06-17 09:29:14 -070088#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070089 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070090#endif
csmartdalton578f0642017-02-24 16:04:47 -070091 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070092};
93
bsalomon6c471f72016-07-26 12:56:32 -070094static sk_app::Window::BackendType get_backend_type(const char* str) {
95#ifdef SK_VULKAN
96 if (0 == strcmp(str, "vk")) {
97 return sk_app::Window::kVulkan_BackendType;
98 } else
99#endif
Brian Salomon194db172017-08-17 14:37:06 -0400100#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
101 if (0 == strcmp(str, "angle")) {
102 return sk_app::Window::kANGLE_BackendType;
103 } else
104#endif
bsalomon6c471f72016-07-26 12:56:32 -0700105 if (0 == strcmp(str, "gl")) {
106 return sk_app::Window::kNativeGL_BackendType;
107 } else if (0 == strcmp(str, "sw")) {
108 return sk_app::Window::kRaster_BackendType;
109 } else {
110 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
111 return sk_app::Window::kRaster_BackendType;
112 }
113}
114
Brian Osmana109e392017-02-24 09:49:14 -0500115static SkColorSpacePrimaries gSrgbPrimaries = {
116 0.64f, 0.33f,
117 0.30f, 0.60f,
118 0.15f, 0.06f,
119 0.3127f, 0.3290f };
120
121static SkColorSpacePrimaries gAdobePrimaries = {
122 0.64f, 0.33f,
123 0.21f, 0.71f,
124 0.15f, 0.06f,
125 0.3127f, 0.3290f };
126
127static SkColorSpacePrimaries gP3Primaries = {
128 0.680f, 0.320f,
129 0.265f, 0.690f,
130 0.150f, 0.060f,
131 0.3127f, 0.3290f };
132
133static SkColorSpacePrimaries gRec2020Primaries = {
134 0.708f, 0.292f,
135 0.170f, 0.797f,
136 0.131f, 0.046f,
137 0.3127f, 0.3290f };
138
139struct NamedPrimaries {
140 const char* fName;
141 SkColorSpacePrimaries* fPrimaries;
142} gNamedPrimaries[] = {
143 { "sRGB", &gSrgbPrimaries },
144 { "AdobeRGB", &gAdobePrimaries },
145 { "P3", &gP3Primaries },
146 { "Rec. 2020", &gRec2020Primaries },
147};
148
149static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
150 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
151}
152
Brian Osman70d2f432017-11-08 09:54:10 -0500153static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
154 // In raster mode, we still use GL for the window.
155 // This lets us render the GUI faster (and correct).
156 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
157}
158
liyuqiane5a6cd92016-05-27 08:52:52 -0700159const char* kName = "name";
160const char* kValue = "value";
161const char* kOptions = "options";
162const char* kSlideStateName = "Slide";
163const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700164const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700165const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700166const char* kSoftkeyStateName = "Softkey";
167const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700168const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700169const char* kON = "ON";
170const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700171const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700172
jvanverth34524262016-05-04 13:49:13 -0700173Viewer::Viewer(int argc, char** argv, void* platformData)
jvanverthc265a922016-04-08 12:51:45 -0700174 : fCurrentMeasurement(0)
Jim Van Verth90dcce52017-11-03 13:36:07 -0400175 , fCumulativeMeasurementTime(0)
176 , fCumulativeMeasurementCount(0)
jvanverthc265a922016-04-08 12:51:45 -0700177 , fDisplayStats(false)
liyuqian2edb0f42016-07-06 14:11:32 -0700178 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500179 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500180 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500181 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500182 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500183 , fShowZoomWindow(false)
184 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700185 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500186 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500187 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500188 // Our UI can only tweak gamma (currently), so start out gamma-only
189 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700190 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400191 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500192 , fTileCnt(0)
193 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700194{
Greg Daniel285db442016-10-14 09:12:53 -0400195 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700196
Brian Osmanf09e35e2017-12-15 14:48:09 -0500197 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
198 gPathRendererNames[GpuPathRenderers::kDefault] =
199 "Default Ganesh Behavior (best path renderer, not including CCPR)";
200 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
201 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
202 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
203 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
204 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
205 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700206
Brian Osman1df161a2017-02-09 12:10:20 -0500207 memset(fPaintTimes, 0, sizeof(fPaintTimes));
208 memset(fFlushTimes, 0, sizeof(fFlushTimes));
209 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
jvanverth9f372462016-04-06 06:08:59 -0700210
jvanverth2bb3b6d2016-04-08 07:24:09 -0700211 SkDebugf("Command line arguments: ");
212 for (int i = 1; i < argc; ++i) {
213 SkDebugf("%s ", argv[i]);
214 }
215 SkDebugf("\n");
216
217 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500218#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400219 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500220#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700221
Brian Osmanbc8150f2017-07-24 11:38:01 -0400222 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400223 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400224
bsalomon6c471f72016-07-26 12:56:32 -0700225 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700226 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700227
csmartdalton578f0642017-02-24 16:04:47 -0700228 DisplayParams displayParams;
229 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700230 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700231 fWindow->setRequestedDisplayParams(displayParams);
232
jvanverth9f372462016-04-06 06:08:59 -0700233 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700234 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500235 fWindow->pushLayer(this);
Brian Osmand67e5182017-12-08 16:46:09 -0500236 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700237
brianosman622c8d52016-05-10 06:50:49 -0700238 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500239 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
240 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
241 fWindow->inval();
242 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500243 // Command to jump directly to the slide picker and give it focus
244 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
245 this->fShowImGuiDebugWindow = true;
246 this->fShowSlidePicker = true;
247 fWindow->inval();
248 });
249 // Alias that to Backspace, to match SampleApp
250 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
251 this->fShowImGuiDebugWindow = true;
252 this->fShowSlidePicker = true;
253 fWindow->inval();
254 });
Brian Osman79086b92017-02-10 13:36:16 -0500255 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
256 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
257 fWindow->inval();
258 });
Brian Osmanf6877092017-02-13 09:39:57 -0500259 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
260 this->fShowZoomWindow = !this->fShowZoomWindow;
261 fWindow->inval();
262 });
brianosman622c8d52016-05-10 06:50:49 -0700263 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
264 this->fDisplayStats = !this->fDisplayStats;
265 fWindow->inval();
266 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400267 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
268 this->resetMeasurements();
269 this->updateTitle();
270 fWindow->inval();
271 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500272 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500273 switch (fColorMode) {
274 case ColorMode::kLegacy:
275 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
276 break;
277 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
278 this->setColorMode(ColorMode::kColorManagedSRGB8888);
279 break;
280 case ColorMode::kColorManagedSRGB8888:
281 this->setColorMode(ColorMode::kColorManagedLinearF16);
282 break;
283 case ColorMode::kColorManagedLinearF16:
284 this->setColorMode(ColorMode::kLegacy);
285 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500286 }
brianosman622c8d52016-05-10 06:50:49 -0700287 });
288 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
289 int previousSlide = fCurrentSlide;
290 fCurrentSlide++;
291 if (fCurrentSlide >= fSlides.count()) {
292 fCurrentSlide = 0;
293 }
294 this->setupCurrentSlide(previousSlide);
295 });
296 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
297 int previousSlide = fCurrentSlide;
298 fCurrentSlide--;
299 if (fCurrentSlide < 0) {
300 fCurrentSlide = fSlides.count() - 1;
301 }
302 this->setupCurrentSlide(previousSlide);
303 });
304 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
305 this->changeZoomLevel(1.f / 32.f);
306 fWindow->inval();
307 });
308 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
309 this->changeZoomLevel(-1.f / 32.f);
310 fWindow->inval();
311 });
jvanverthaf236b52016-05-20 06:01:06 -0700312 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400313 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
314 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500315 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400316#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
317 if (newBackend == sk_app::Window::kVulkan_BackendType) {
318 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
319 sk_app::Window::kBackendTypeCount);
320 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
321 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500322 }
323#endif
Brian Osman621491e2017-02-28 15:45:01 -0500324 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700325 });
brianosman622c8d52016-05-10 06:50:49 -0700326
Yuqian Lib2ba6642017-11-22 12:07:41 -0500327 fCommands.addCommand('A', "AA", "Toggle analytic AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400328 if (!gSkUseAnalyticAA) {
329 gSkUseAnalyticAA = true;
330 } else if (!gSkForceAnalyticAA) {
331 gSkForceAnalyticAA = true;
332 } else {
333 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
334 }
335 this->updateTitle();
336 fWindow->inval();
337 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500338 fCommands.addCommand('D', "AA", "Toggle delta AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400339 if (!gSkUseDeltaAA) {
340 gSkUseDeltaAA = true;
341 } else if (!gSkForceDeltaAA) {
342 gSkForceDeltaAA = true;
343 } else {
344 gSkUseDeltaAA = gSkForceDeltaAA = false;
345 }
346 this->updateTitle();
347 fWindow->inval();
348 });
349
Yuqian Lib2ba6642017-11-22 12:07:41 -0500350 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
351 fTileCnt++;
352 if (fThreadCnt == 0) {
353 this->resetExecutor();
354 }
355 this->updateTitle();
356 fWindow->inval();
357 });
358 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
359 fTileCnt = SkTMax(0, fTileCnt - 1);
360 if (fThreadCnt == 0) {
361 this->resetExecutor();
362 }
363 this->updateTitle();
364 fWindow->inval();
365 });
366 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
367 if (fTileCnt == 0) {
368 return;
369 }
370 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
371 this->resetExecutor();
372 this->updateTitle();
373 fWindow->inval();
374 });
375 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
376 if (fTileCnt == 0) {
377 return;
378 }
379 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
380 this->resetExecutor();
381 this->updateTitle();
382 fWindow->inval();
383 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500384 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
385 fSaveToSKP = true;
386 fWindow->inval();
387 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500388
jvanverth2bb3b6d2016-04-08 07:24:09 -0700389 // set up slides
390 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500391 this->setStartupSlide();
392 if (FLAGS_list) {
393 this->listNames();
394 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700395
djsollen12d62a72016-04-21 07:59:44 -0700396 fAnimTimer.run();
397
Hal Canaryc465d132017-12-08 10:21:31 -0500398 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500399 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400400 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500401 }
402 fImGuiGamutPaint.setColor(SK_ColorWHITE);
403 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
404
Brian Osman70d2f432017-11-08 09:54:10 -0500405 fWindow->attach(backend_type_for_window(fBackendType));
jvanverth9f372462016-04-06 06:08:59 -0700406}
407
jvanverth34524262016-05-04 13:49:13 -0700408void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700409 fAllSlideNames = Json::Value(Json::arrayValue);
410
jvanverth2bb3b6d2016-04-08 07:24:09 -0700411 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
412 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400413 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700414
415 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
416 sk_sp<Slide> slide(new GMSlide(gm.release()));
417 fSlides.push_back(slide);
418 }
419
420 gms = gms->next();
421 }
422
423 // reverse array
424 for (int i = 0; i < fSlides.count()/2; ++i) {
425 sk_sp<Slide> temp = fSlides[i];
426 fSlides[i] = fSlides[fSlides.count() - i - 1];
427 fSlides[fSlides.count() - i - 1] = temp;
428 }
429
jvanverthc7027ab2016-06-16 09:52:35 -0700430 // samples
431 const SkViewRegister* reg = SkViewRegister::Head();
432 while (reg) {
433 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700434 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
435 fSlides.push_back(slide);
436 }
jvanverthc7027ab2016-06-16 09:52:35 -0700437 reg = reg->next();
438 }
439
jvanverth2bb3b6d2016-04-08 07:24:09 -0700440 // SKPs
441 for (int i = 0; i < FLAGS_skps.count(); i++) {
442 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700443 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
444 continue;
445 }
446
jvanverth2bb3b6d2016-04-08 07:24:09 -0700447 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700448 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700449 if (slide) {
450 fSlides.push_back(slide);
451 }
452 } else {
453 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700454 SkString skpName;
455 while (it.next(&skpName)) {
456 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
457 continue;
458 }
459
460 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
461 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700462 if (slide) {
463 fSlides.push_back(slide);
464 }
465 }
466 }
467 }
liyuqian6f163d22016-06-13 12:26:45 -0700468
469 // JPGs
470 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
471 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
472 SkString jpgName;
473 while (it.next(&jpgName)) {
brianosmane1d20072016-07-12 09:07:33 -0700474 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) {
475 continue;
476 }
477
liyuqian6f163d22016-06-13 12:26:45 -0700478 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
479 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
480 if (slide) {
481 fSlides.push_back(slide);
482 }
483 }
484 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700485}
486
487
jvanverth34524262016-05-04 13:49:13 -0700488Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700489 fWindow->detach();
490 delete fWindow;
491}
492
brianosman05de2162016-05-06 13:28:57 -0700493void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700494 if (!fWindow) {
495 return;
496 }
497 if (fWindow->sampleCount() < 0) {
498 return; // Surface hasn't been created yet.
499 }
500
jvanverth34524262016-05-04 13:49:13 -0700501 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700502 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700503
Yuqian Li399b3c22017-08-03 11:08:15 -0400504 if (gSkUseDeltaAA) {
505 if (gSkForceDeltaAA) {
506 title.append(" <FDAA>");
507 } else {
508 title.append(" <DAA>");
509 }
510 } else if (gSkUseAnalyticAA) {
511 if (gSkForceAnalyticAA) {
512 title.append(" <FAAA>");
513 } else {
514 title.append(" <AAA>");
515 }
516 }
517
Yuqian Lib2ba6642017-11-22 12:07:41 -0500518 if (fTileCnt > 0) {
519 title.appendf(" T%d", fTileCnt);
520 if (fThreadCnt > 0) {
521 title.appendf("/%d", fThreadCnt);
522 }
523 }
524
Brian Osman92004802017-03-06 11:47:26 -0500525 switch (fColorMode) {
526 case ColorMode::kLegacy:
527 title.append(" Legacy 8888");
528 break;
529 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
530 title.append(" ColorManaged 8888 (Nonlinear blending)");
531 break;
532 case ColorMode::kColorManagedSRGB8888:
533 title.append(" ColorManaged 8888");
534 break;
535 case ColorMode::kColorManagedLinearF16:
536 title.append(" ColorManaged F16");
537 break;
538 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500539
Brian Osman92004802017-03-06 11:47:26 -0500540 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500541 int curPrimaries = -1;
542 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
543 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
544 curPrimaries = i;
545 break;
546 }
547 }
548 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500549
550 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
551 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
552 }
brianosman05de2162016-05-06 13:28:57 -0700553 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500554
csmartdalton578f0642017-02-24 16:04:47 -0700555 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700556 title.append(kBackendTypeStrings[fBackendType]);
csmartdalton578f0642017-02-24 16:04:47 -0700557 if (int msaa = fWindow->sampleCount()) {
558 title.appendf(" MSAA: %i", msaa);
559 }
560 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700561
562 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400563 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700564 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
565 }
566
brianosman05de2162016-05-06 13:28:57 -0700567 fWindow->setTitle(title.c_str());
568}
569
Jim Van Verth6f449692017-02-14 15:16:46 -0500570void Viewer::setStartupSlide() {
571
572 if (!FLAGS_slide.isEmpty()) {
573 int count = fSlides.count();
574 for (int i = 0; i < count; i++) {
575 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
576 fCurrentSlide = i;
577 return;
578 }
579 }
580
581 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
582 this->listNames();
583 }
584
585 fCurrentSlide = 0;
586}
587
588void Viewer::listNames() {
589 int count = fSlides.count();
590 SkDebugf("All Slides:\n");
591 for (int i = 0; i < count; i++) {
592 SkDebugf(" %s\n", fSlides[i]->getName().c_str());
593 }
594}
595
Jim Van Verth90dcce52017-11-03 13:36:07 -0400596void Viewer::resetMeasurements() {
597 memset(fPaintTimes, 0, sizeof(fPaintTimes));
598 memset(fFlushTimes, 0, sizeof(fFlushTimes));
599 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
600 fCurrentMeasurement = 0;
601 fCumulativeMeasurementTime = 0;
602 fCumulativeMeasurementCount = 0;
603}
604
brianosman05de2162016-05-06 13:28:57 -0700605void Viewer::setupCurrentSlide(int previousSlide) {
liyuqiane5a6cd92016-05-27 08:52:52 -0700606 if (fCurrentSlide == previousSlide) {
607 return; // no change; do nothing
608 }
liyuqian6f163d22016-06-13 12:26:45 -0700609 // prepare dimensions for image slides
jvanverthc7027ab2016-06-16 09:52:35 -0700610 fSlides[fCurrentSlide]->load(SkIntToScalar(fWindow->width()), SkIntToScalar(fWindow->height()));
liyuqian6f163d22016-06-13 12:26:45 -0700611
liyuqiane46e4f02016-05-20 07:32:19 -0700612 fGesture.reset();
613 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700614
Brian Osman42bb6ac2017-06-05 08:46:04 -0400615 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
616 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
617 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
618
619 // Start with a matrix that scales the slide to the available screen space
620 if (fWindow->scaleContentToFit()) {
621 if (windowRect.width() > 0 && windowRect.height() > 0) {
622 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
liyuqiane46e4f02016-05-20 07:32:19 -0700623 }
624 }
625
Brian Osman42bb6ac2017-06-05 08:46:04 -0400626 // Prevent the user from dragging content so far outside the window they can't find it again
627 fGesture.setTransLimit(slideBounds, windowRect, fDefaultMatrix);
liyuqiane46e4f02016-05-20 07:32:19 -0700628
brianosman05de2162016-05-06 13:28:57 -0700629 this->updateTitle();
liyuqiane5a6cd92016-05-27 08:52:52 -0700630 this->updateUIState();
jvanverthc265a922016-04-08 12:51:45 -0700631 if (previousSlide >= 0) {
632 fSlides[previousSlide]->unload();
633 }
Jim Van Verth90dcce52017-11-03 13:36:07 -0400634
635 this->resetMeasurements();
636
jvanverthc265a922016-04-08 12:51:45 -0700637 fWindow->inval();
638}
639
640#define MAX_ZOOM_LEVEL 8
641#define MIN_ZOOM_LEVEL -8
642
jvanverth34524262016-05-04 13:49:13 -0700643void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700644 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400645 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
jvanverthc265a922016-04-08 12:51:45 -0700646}
647
liyuqiand3cdbca2016-05-17 12:44:20 -0700648SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700649 SkMatrix m;
jvanverthc265a922016-04-08 12:51:45 -0700650
Brian Osman42bb6ac2017-06-05 08:46:04 -0400651 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
652 : SK_Scalar1 + fZoomLevel;
653 m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700654 m.preConcat(fGesture.globalM());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400655 m.preConcat(fDefaultMatrix);
656 m.preScale(zoomScale, zoomScale);
jvanverthc265a922016-04-08 12:51:45 -0700657
liyuqiand3cdbca2016-05-17 12:44:20 -0700658 return m;
jvanverthc265a922016-04-08 12:51:45 -0700659}
660
Brian Osman621491e2017-02-28 15:45:01 -0500661void Viewer::setBackend(sk_app::Window::BackendType backendType) {
662 fBackendType = backendType;
663
664 fWindow->detach();
665
Brian Osman70d2f432017-11-08 09:54:10 -0500666#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400667 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
668 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500669 DisplayParams params = fWindow->getRequestedDisplayParams();
670 delete fWindow;
671 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500672
Brian Osman70d2f432017-11-08 09:54:10 -0500673 // re-register callbacks
674 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500675 fWindow->pushLayer(this);
Brian Osmand67e5182017-12-08 16:46:09 -0500676 fWindow->pushLayer(&fImGuiLayer);
677
Brian Osman70d2f432017-11-08 09:54:10 -0500678 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
679 // will still include our correct sample count. But the re-created fWindow will lose that
680 // information. On Windows, we need to re-create the window when changing sample count,
681 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
682 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
683 // as if we had never changed windows at all).
684 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500685#endif
686
Brian Osman70d2f432017-11-08 09:54:10 -0500687 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500688}
689
Brian Osman92004802017-03-06 11:47:26 -0500690void Viewer::setColorMode(ColorMode colorMode) {
691 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700692
Brian Osmanf750fbc2017-02-08 10:47:28 -0500693 // When we're in color managed mode, we tag our window surface as sRGB. If we've switched into
Brian Osmane0d4fba2017-03-15 10:24:55 -0400694 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700695 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500696 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400697 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
698 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500699 if (wasInLegacy != wantLegacy) {
700 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700701 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500702 }
703
704 this->updateTitle();
705 fWindow->inval();
706}
707
708void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500709 SkAutoCanvasRestore autorestore(canvas, false);
710
Brian Osmanf750fbc2017-02-08 10:47:28 -0500711 // By default, we render directly into the window's surface/canvas
712 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500713 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700714
Brian Osmane0d4fba2017-03-15 10:24:55 -0400715 // If we're in any of the color managed modes, construct the color space we're going to use
716 sk_sp<SkColorSpace> cs = nullptr;
717 if (ColorMode::kLegacy != fColorMode) {
718 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
719 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400720 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400721 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500722 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
723 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
724 } else {
725 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
726 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400727 }
728
Brian Osman3ac99cf2017-12-01 11:23:53 -0500729 if (fSaveToSKP) {
730 SkPictureRecorder recorder;
731 SkCanvas* recorderCanvas = recorder.beginRecording(
732 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
733 // In xform-canvas mode, record the transformed output
734 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
735 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
736 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
737 recorderCanvas = xformCanvas.get();
738 }
739 fSlides[fCurrentSlide]->draw(recorderCanvas);
740 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
741 SkFILEWStream stream("sample_app.skp");
742 picture->serialize(&stream);
743 fSaveToSKP = false;
744 }
745
Brian Osmane0d4fba2017-03-15 10:24:55 -0400746 // If we're in F16, or we're zooming, or we're in color correct 8888 and the gamut isn't sRGB,
Brian Osman70d2f432017-11-08 09:54:10 -0500747 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
748 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500749 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -0500750 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
751 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -0500752 if (Window::kRaster_BackendType == fBackendType ||
753 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500754 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400755 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
756 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
757
Brian Osman92004802017-03-06 11:47:26 -0500758 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
759 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400760 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
761 // with a special canvas (below) that has the color space attached
762 sk_sp<SkColorSpace> offscreenColorSpace =
763 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500764 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400765 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500766 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
767 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -0500768 SkPixmap offscreenPixmap;
769 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
770 SkBitmap offscreenBitmap;
771 offscreenBitmap.installPixels(offscreenPixmap);
772 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
773 fThreadCnt, fExecutor.get()));
774 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
775 slideCanvas = threadedCanvas.get();
776 } else {
777 slideCanvas = offscreenSurface->getCanvas();
778 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500779 }
780
Brian Osmane0d4fba2017-03-15 10:24:55 -0400781 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
782 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
783 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
784 slideCanvas = xformCanvas.get();
785 }
786
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500787 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500788 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500789 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500790 // Time the painting logic of the slide
791 double startTime = SkTime::GetMSecs();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500792 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osman1df161a2017-02-09 12:10:20 -0500793 fPaintTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500794 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500795
796 // Force a flush so we can time that, too
797 startTime = SkTime::GetMSecs();
798 slideCanvas->flush();
799 fFlushTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500800
801 // If we rendered offscreen, snap an image and push the results to the window's canvas
802 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500803 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500804
805 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400806 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500807 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400808 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500809 SkPaint paint;
810 paint.setBlendMode(SkBlendMode::kSrc);
811 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -0700812 }
liyuqian6f163d22016-06-13 12:26:45 -0700813}
814
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700815void Viewer::onBackendCreated() {
816 this->updateTitle();
817 this->updateUIState();
818 this->setupCurrentSlide(-1);
Jim Van Verth90dcce52017-11-03 13:36:07 -0400819 this->resetMeasurements();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700820 fWindow->show();
821 fWindow->inval();
822}
Jim Van Verth6f449692017-02-14 15:16:46 -0500823
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700824void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -0400825 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -0700826
Brian Osman1df161a2017-02-09 12:10:20 -0500827 // Advance our timing bookkeeping
Jim Van Verth90dcce52017-11-03 13:36:07 -0400828 fCumulativeMeasurementTime += fAnimateTimes[fCurrentMeasurement] +
829 fPaintTimes[fCurrentMeasurement] +
830 fFlushTimes[fCurrentMeasurement];
831 fCumulativeMeasurementCount++;
Brian Osman1df161a2017-02-09 12:10:20 -0500832 fCurrentMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1);
833 SkASSERT(fCurrentMeasurement < kMeasurementCount);
834
835 // Draw any overlays or UI that we don't want timed
jvanverthc265a922016-04-08 12:51:45 -0700836 if (fDisplayStats) {
837 drawStats(canvas);
838 }
brianosman622c8d52016-05-10 06:50:49 -0700839 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -0700840
Brian Osmand67e5182017-12-08 16:46:09 -0500841 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -0500842
Brian Osman1df161a2017-02-09 12:10:20 -0500843 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -0400844 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700845}
846
jvanverth814e38d2016-06-06 08:48:47 -0700847bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400848 if (GestureDevice::kMouse == fGestureDevice) {
849 return false;
850 }
liyuqiand3cdbca2016-05-17 12:44:20 -0700851 void* castedOwner = reinterpret_cast<void*>(owner);
852 switch (state) {
853 case Window::kUp_InputState: {
854 fGesture.touchEnd(castedOwner);
855 break;
856 }
857 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400858 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700859 break;
860 }
861 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400862 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700863 break;
864 }
865 }
Brian Osmanb53f48c2017-06-07 10:00:30 -0400866 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -0700867 fWindow->inval();
868 return true;
869}
870
Brian Osman80fc07e2017-12-08 16:45:43 -0500871bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -0500872 if (!fSlides[fCurrentSlide]->onMouse(x, y, state, modifiers)) {
873 if (GestureDevice::kTouch == fGestureDevice) {
874 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -0500875 }
Brian Osmand67e5182017-12-08 16:46:09 -0500876 switch (state) {
877 case Window::kUp_InputState: {
878 fGesture.touchEnd(nullptr);
879 break;
880 }
881 case Window::kDown_InputState: {
882 fGesture.touchBegin(nullptr, x, y);
883 break;
884 }
885 case Window::kMove_InputState: {
886 fGesture.touchMoved(nullptr, x, y);
887 break;
888 }
889 }
890 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
Brian Osman80fc07e2017-12-08 16:45:43 -0500891 }
Brian Osmand67e5182017-12-08 16:46:09 -0500892 fWindow->inval();
Jim Van Verthe7705782017-05-04 14:00:59 -0400893 return true;
894}
895
jvanverth34524262016-05-04 13:49:13 -0700896void Viewer::drawStats(SkCanvas* canvas) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700897 static const float kPixelPerMS = 2.0f;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500898 static const int kDisplayWidth = 192;
899 static const int kGraphHeight = 100;
900 static const int kTextHeight = 60;
901 static const int kDisplayHeight = kGraphHeight + kTextHeight;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700902 static const int kDisplayPadding = 10;
903 static const int kGraphPadding = 3;
904 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
905
Mike Reed3661bc92017-02-22 13:21:42 -0500906 SkISize canvasSize = canvas->getBaseLayerSize();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700907 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
908 SkIntToScalar(kDisplayPadding),
909 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
910 SkPaint paint;
911 canvas->save();
912
jvanverth3d6ed3a2016-04-07 11:09:51 -0700913 paint.setColor(SK_ColorBLACK);
914 canvas->drawRect(rect, paint);
915 // draw the 16ms line
916 paint.setColor(SK_ColorLTGRAY);
917 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
918 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
919 paint.setColor(SK_ColorRED);
920 paint.setStyle(SkPaint::kStroke_Style);
921 canvas->drawRect(rect, paint);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500922 paint.setStyle(SkPaint::kFill_Style);
jvanverth3d6ed3a2016-04-07 11:09:51 -0700923
924 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500925 const int xStep = 3;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700926 int i = fCurrentMeasurement;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500927 double ms = 0;
928 double animateMS = 0;
929 double paintMS = 0;
930 double flushMS = 0;
931 int count = 0;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700932 do {
Brian Osman1df161a2017-02-09 12:10:20 -0500933 // Round to nearest values
934 int animateHeight = (int)(fAnimateTimes[i] * kPixelPerMS + 0.5);
935 int paintHeight = (int)(fPaintTimes[i] * kPixelPerMS + 0.5);
936 int flushHeight = (int)(fFlushTimes[i] * kPixelPerMS + 0.5);
937 int startY = SkScalarTruncToInt(rect.fBottom);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500938 int endY = SkTMax(startY - flushHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -0500939 paint.setColor(SK_ColorRED);
940 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
941 SkIntToScalar(x), SkIntToScalar(endY), paint);
942 startY = endY;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500943 endY = SkTMax(startY - paintHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -0500944 paint.setColor(SK_ColorGREEN);
945 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
946 SkIntToScalar(x), SkIntToScalar(endY), paint);
947 startY = endY;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500948 endY = SkTMax(startY - animateHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -0500949 paint.setColor(SK_ColorMAGENTA);
jvanverth3d6ed3a2016-04-07 11:09:51 -0700950 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
951 SkIntToScalar(x), SkIntToScalar(endY), paint);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500952
953 double inc = fAnimateTimes[i] + fPaintTimes[i] + fFlushTimes[i];
954 if (inc > 0) {
955 ms += inc;
956 animateMS += fAnimateTimes[i];
957 paintMS += fPaintTimes[i];
958 flushMS += fFlushTimes[i];
959 ++count;
960 }
961
jvanverth3d6ed3a2016-04-07 11:09:51 -0700962 i++;
963 i &= (kMeasurementCount - 1); // fast mod
964 x += xStep;
965 } while (i != fCurrentMeasurement);
jvanverth9f372462016-04-06 06:08:59 -0700966
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500967 paint.setTextSize(16);
968 SkString mainString;
969 mainString.appendf("%4.3f ms -> %4.3f ms", ms / SkTMax(1, count),
970 fCumulativeMeasurementTime / SkTMax(1, fCumulativeMeasurementCount));
971 paint.setColor(SK_ColorWHITE);
972 canvas->drawString(mainString.c_str(), rect.fLeft+3, rect.fTop + 14, paint);
973
974 SkString animateString;
975 animateString.appendf("Animate: %4.3f ms", animateMS / SkTMax(1, count));
976 paint.setColor(0xffff66ff); // pure magenta is hard to read
977 canvas->drawString(animateString.c_str(), rect.fLeft+3, rect.fTop + 28, paint);
978
979 SkString paintString;
980 paintString.appendf("Paint: %4.3f ms", paintMS / SkTMax(1, count));
981 paint.setColor(SK_ColorGREEN);
982 canvas->drawString(paintString.c_str(), rect.fLeft+3, rect.fTop + 42, paint);
983
984 SkString flushString;
985 flushString.appendf("Flush: %4.3f ms", flushMS / SkTMax(1, count));
986 paint.setColor(0xffff6666); // pure red is hard to read
987 canvas->drawString(flushString.c_str(), rect.fLeft+3, rect.fTop + 56, paint);
988
jvanverth9f372462016-04-06 06:08:59 -0700989 canvas->restore();
990}
991
Brian Osmana109e392017-02-24 09:49:14 -0500992static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
993 const ImVec2& pos, const ImVec2& size) {
994 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
995 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
996
997 // Invisible 10x10 button
998 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
999 ImGui::InvisibleButton(label, ImVec2(10, 10));
1000
1001 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1002 ImGuiIO& io = ImGui::GetIO();
1003 // Normalized mouse position, relative to our gamut box
1004 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1005 // Clamp to edge of box, convert back to primary scale
1006 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1007 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1008 }
1009
1010 if (ImGui::IsItemHovered()) {
1011 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1012 }
1013
1014 // Return screen coordinates for the caller. We could just return center here, but we'd have
1015 // one frame of lag during drag.
1016 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1017}
1018
1019static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1020 ImDrawList* drawList = ImGui::GetWindowDrawList();
1021
1022 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1023 // width, and scale the height to maintain aspect ratio.
1024 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1025 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1026 ImVec2 pos = ImGui::GetCursorScreenPos();
1027
1028 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1029 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1030 // Magic numbers are pixel locations of the origin and upper-right corner.
1031 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1032 ImVec2(242, 61), ImVec2(1897, 1922));
1033 ImVec2 endPos = ImGui::GetCursorPos();
1034
1035 // Primary markers
1036 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1037 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1038 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1039 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1040
1041 // Gamut triangle
1042 drawList->AddCircle(r, 5.0f, 0xFF000040);
1043 drawList->AddCircle(g, 5.0f, 0xFF004000);
1044 drawList->AddCircle(b, 5.0f, 0xFF400000);
1045 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1046 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1047
1048 // Re-position cursor immediate after the diagram for subsequent controls
1049 ImGui::SetCursorPos(endPos);
1050}
1051
Brian Osmand67e5182017-12-08 16:46:09 -05001052void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001053 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1054 if (fShowImGuiTestWindow) {
1055 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1056 }
1057
1058 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001059 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1060 // always visible, we can end up in a layout feedback loop.
1061 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001062 DisplayParams params = fWindow->getRequestedDisplayParams();
1063 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001064 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1065 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001066 if (ImGui::CollapsingHeader("Backend")) {
1067 int newBackend = static_cast<int>(fBackendType);
1068 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1069 ImGui::SameLine();
1070 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001071#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1072 ImGui::SameLine();
1073 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1074#endif
Brian Osman621491e2017-02-28 15:45:01 -05001075#if defined(SK_VULKAN)
1076 ImGui::SameLine();
1077 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1078#endif
1079 if (newBackend != fBackendType) {
1080 fDeferredActions.push_back([=]() {
1081 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1082 });
1083 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001084
Brian Salomon99a33902017-03-07 15:16:34 -05001085 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001086 bool* wire = &params.fGrContextOptions.fWireframeMode;
1087 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1088 paramsChanged = true;
1089 }
Brian Salomon99a33902017-03-07 15:16:34 -05001090
Brian Osman28b12522017-03-08 17:10:24 -05001091 if (ctx) {
1092 int sampleCount = fWindow->sampleCount();
1093 ImGui::Text("MSAA: "); ImGui::SameLine();
1094 ImGui::RadioButton("0", &sampleCount, 0); ImGui::SameLine();
1095 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1096 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1097 ImGui::RadioButton("16", &sampleCount, 16);
1098
1099 if (sampleCount != params.fMSAASampleCount) {
1100 params.fMSAASampleCount = sampleCount;
1101 paramsChanged = true;
1102 }
1103 }
1104
Brian Osman8a9de3d2017-03-01 14:59:05 -05001105 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001106 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001107 auto prButton = [&](GpuPathRenderers x) {
1108 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001109 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1110 params.fGrContextOptions.fGpuPathRenderers = x;
1111 paramsChanged = true;
1112 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001113 }
1114 };
1115
1116 if (!ctx) {
1117 ImGui::RadioButton("Software", true);
1118 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001119 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001120 prButton(GpuPathRenderers::kAll);
1121 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1122 prButton(GpuPathRenderers::kStencilAndCover);
1123 }
1124 if (ctx->caps()->sampleShadingSupport()) {
1125 prButton(GpuPathRenderers::kMSAA);
1126 }
1127 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001128 prButton(GpuPathRenderers::kNone);
1129 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001130 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001131 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001132 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1133 prButton(GpuPathRenderers::kCoverageCounting);
1134 }
Jim Van Verth83010462017-03-16 08:45:39 -04001135 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001136 prButton(GpuPathRenderers::kTessellating);
1137 prButton(GpuPathRenderers::kNone);
1138 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001139 ImGui::TreePop();
1140 }
Brian Osman621491e2017-02-28 15:45:01 -05001141 }
1142
Brian Osmanfce09c52017-11-14 15:32:20 -05001143 if (fShowSlidePicker) {
1144 ImGui::SetNextTreeNodeOpen(true);
1145 }
1146
Brian Osman79086b92017-02-10 13:36:16 -05001147 if (ImGui::CollapsingHeader("Slide")) {
1148 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001149 static ImVector<const char*> filteredSlideNames;
1150 static ImVector<int> filteredSlideIndices;
1151
Brian Osmanfce09c52017-11-14 15:32:20 -05001152 if (fShowSlidePicker) {
1153 ImGui::SetKeyboardFocusHere();
1154 fShowSlidePicker = false;
1155 }
1156
Brian Osman79086b92017-02-10 13:36:16 -05001157 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001158 filteredSlideNames.clear();
1159 filteredSlideIndices.clear();
1160 int filteredIndex = 0;
1161 for (int i = 0; i < fSlides.count(); ++i) {
1162 const char* slideName = fSlides[i]->getName().c_str();
1163 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1164 if (i == fCurrentSlide) {
1165 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001166 }
Brian Osmanf479e422017-11-08 13:11:36 -05001167 filteredSlideNames.push_back(slideName);
1168 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001169 }
Brian Osman79086b92017-02-10 13:36:16 -05001170 }
Brian Osmanf479e422017-11-08 13:11:36 -05001171
1172 int previousSlide = fCurrentSlide;
1173 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1174 filteredSlideNames.size(), 20)) {
1175 fCurrentSlide = filteredSlideIndices[filteredIndex];
1176 setupCurrentSlide(previousSlide);
Brian Osman79086b92017-02-10 13:36:16 -05001177 }
1178 }
Brian Osmana109e392017-02-24 09:49:14 -05001179
1180 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001181 ColorMode newMode = fColorMode;
1182 auto cmButton = [&](ColorMode mode, const char* label) {
1183 if (ImGui::RadioButton(label, mode == fColorMode)) {
1184 newMode = mode;
1185 }
1186 };
1187
1188 cmButton(ColorMode::kLegacy, "Legacy 8888");
1189 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1190 "Color Managed 8888 (Nonlinear blending)");
1191 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1192 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1193
1194 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001195 // It isn't safe to switch color mode now (in the middle of painting). We might
1196 // tear down the back-end, etc... Defer this change until the next onIdle.
1197 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001198 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001199 });
Brian Osmana109e392017-02-24 09:49:14 -05001200 }
1201
1202 // Pick from common gamuts:
1203 int primariesIdx = 4; // Default: Custom
1204 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1205 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1206 primariesIdx = i;
1207 break;
1208 }
1209 }
1210
Brian Osmanfdab5762017-11-09 10:27:55 -05001211 // When we're in xform canvas mode, we can alter the transfer function, too
1212 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1213 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1214 }
1215
Brian Osmana109e392017-02-24 09:49:14 -05001216 if (ImGui::Combo("Primaries", &primariesIdx,
1217 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1218 if (primariesIdx >= 0 && primariesIdx <= 3) {
1219 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1220 }
1221 }
1222
1223 // Allow direct editing of gamut
1224 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1225 }
Brian Osman79086b92017-02-10 13:36:16 -05001226 }
Brian Salomon99a33902017-03-07 15:16:34 -05001227 if (paramsChanged) {
1228 fDeferredActions.push_back([=]() {
1229 fWindow->setRequestedDisplayParams(params);
1230 fWindow->inval();
1231 this->updateTitle();
1232 });
1233 }
Brian Osman79086b92017-02-10 13:36:16 -05001234 ImGui::End();
1235 }
1236
Brian Osmanf6877092017-02-13 09:39:57 -05001237 if (fShowZoomWindow && fLastImage) {
1238 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001239 static int zoomFactor = 8;
1240 if (ImGui::Button("<<")) {
1241 zoomFactor = SkTMax(zoomFactor / 2, 4);
1242 }
1243 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1244 if (ImGui::Button(">>")) {
1245 zoomFactor = SkTMin(zoomFactor * 2, 32);
1246 }
Brian Osmanf6877092017-02-13 09:39:57 -05001247
Brian Osmanf6877092017-02-13 09:39:57 -05001248 ImVec2 mousePos = ImGui::GetMousePos();
1249 ImVec2 avail = ImGui::GetContentRegionAvail();
1250
Brian Osmanead517d2017-11-13 15:36:36 -05001251 uint32_t pixel = 0;
1252 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1253 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1254 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001255 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1256 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1257 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001258 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1259 }
1260
Brian Osmand67e5182017-12-08 16:46:09 -05001261 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001262 // Translate so the region of the image that's under the mouse cursor is centered
1263 // in the zoom canvas:
1264 c->scale(zoomFactor, zoomFactor);
1265 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1266 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1267 c->drawImage(this->fLastImage, 0, 0);
1268
1269 SkPaint outline;
1270 outline.setStyle(SkPaint::kStroke_Style);
1271 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1272 });
Brian Osmanf6877092017-02-13 09:39:57 -05001273 }
1274
1275 ImGui::End();
1276 }
Brian Osman79086b92017-02-10 13:36:16 -05001277}
1278
liyuqian2edb0f42016-07-06 14:11:32 -07001279void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001280 for (int i = 0; i < fDeferredActions.count(); ++i) {
1281 fDeferredActions[i]();
1282 }
1283 fDeferredActions.reset();
1284
Brian Osman1df161a2017-02-09 12:10:20 -05001285 double startTime = SkTime::GetMSecs();
jvanverthc265a922016-04-08 12:51:45 -07001286 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001287 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
1288 fAnimateTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
1289
Brian Osman79086b92017-02-10 13:36:16 -05001290 ImGuiIO& io = ImGui::GetIO();
1291 if (animateWantsInval || fDisplayStats || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001292 fWindow->inval();
1293 }
jvanverth9f372462016-04-06 06:08:59 -07001294}
liyuqiane5a6cd92016-05-27 08:52:52 -07001295
1296void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001297 if (!fWindow) {
1298 return;
1299 }
1300 if (fWindow->sampleCount() < 0) {
1301 return; // Surface hasn't been created yet.
1302 }
1303
liyuqianb73c24b2016-06-03 08:47:23 -07001304 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001305 Json::Value slideState(Json::objectValue);
1306 slideState[kName] = kSlideStateName;
1307 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001308 if (fAllSlideNames.size() == 0) {
1309 for(auto slide : fSlides) {
1310 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1311 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001312 }
liyuqian1f508fd2016-06-07 06:57:40 -07001313 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001314
liyuqianb73c24b2016-06-03 08:47:23 -07001315 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001316 Json::Value backendState(Json::objectValue);
1317 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001318 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001319 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001320 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001321 backendState[kOptions].append(Json::Value(str));
1322 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001323
csmartdalton578f0642017-02-24 16:04:47 -07001324 // MSAA state
1325 Json::Value msaaState(Json::objectValue);
1326 msaaState[kName] = kMSAAStateName;
1327 msaaState[kValue] = fWindow->sampleCount();
1328 msaaState[kOptions] = Json::Value(Json::arrayValue);
1329 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1330 msaaState[kOptions].append(Json::Value(0));
1331 } else {
1332 for (int msaa : {0, 4, 8, 16}) {
1333 msaaState[kOptions].append(Json::Value(msaa));
1334 }
1335 }
1336
csmartdalton61cd31a2017-02-27 17:00:53 -07001337 // Path renderer state
1338 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1339 Json::Value prState(Json::objectValue);
1340 prState[kName] = kPathRendererStateName;
1341 prState[kValue] = gPathRendererNames[pr];
1342 prState[kOptions] = Json::Value(Json::arrayValue);
1343 const GrContext* ctx = fWindow->getGrContext();
1344 if (!ctx) {
1345 prState[kOptions].append("Software");
1346 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001347 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001348 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1349 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1350 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1351 }
1352 if (ctx->caps()->sampleShadingSupport()) {
1353 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1354 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001355 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001356 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1357 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001358 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001359 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001360 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1361 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1362 }
Jim Van Verth83010462017-03-16 08:45:39 -04001363 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001364 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001365 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1366 }
1367
liyuqianb73c24b2016-06-03 08:47:23 -07001368 // Softkey state
1369 Json::Value softkeyState(Json::objectValue);
1370 softkeyState[kName] = kSoftkeyStateName;
1371 softkeyState[kValue] = kSoftkeyHint;
1372 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1373 softkeyState[kOptions].append(kSoftkeyHint);
1374 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1375 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1376 }
1377
liyuqian1f508fd2016-06-07 06:57:40 -07001378 // FPS state
1379 Json::Value fpsState(Json::objectValue);
1380 fpsState[kName] = kFpsStateName;
Brian Osman1df161a2017-02-09 12:10:20 -05001381 int idx = (fCurrentMeasurement + (kMeasurementCount - 1)) & (kMeasurementCount - 1);
1382 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
1383 fAnimateTimes[idx] + fPaintTimes[idx] + fFlushTimes[idx],
1384 fAnimateTimes[idx],
1385 fPaintTimes[idx],
1386 fFlushTimes[idx]).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001387 fpsState[kOptions] = Json::Value(Json::arrayValue);
1388
liyuqiane5a6cd92016-05-27 08:52:52 -07001389 Json::Value state(Json::arrayValue);
1390 state.append(slideState);
1391 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001392 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001393 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001394 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001395 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001396
Brian Osmaneff04b52017-11-21 13:18:02 -05001397 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001398}
1399
1400void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001401 // For those who will add more features to handle the state change in this function:
1402 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1403 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1404 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001405 if (stateName.equals(kSlideStateName)) {
1406 int previousSlide = fCurrentSlide;
1407 fCurrentSlide = 0;
1408 for(auto slide : fSlides) {
1409 if (slide->getName().equals(stateValue)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001410 this->setupCurrentSlide(previousSlide);
liyuqiane5a6cd92016-05-27 08:52:52 -07001411 break;
1412 }
1413 fCurrentSlide++;
1414 }
1415 if (fCurrentSlide >= fSlides.count()) {
1416 fCurrentSlide = previousSlide;
1417 SkDebugf("Slide not found: %s", stateValue.c_str());
1418 }
liyuqian6cb70252016-06-02 12:16:25 -07001419 } else if (stateName.equals(kBackendStateName)) {
1420 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1421 if (stateValue.equals(kBackendTypeStrings[i])) {
1422 if (fBackendType != i) {
1423 fBackendType = (sk_app::Window::BackendType)i;
1424 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001425 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001426 }
1427 break;
1428 }
1429 }
csmartdalton578f0642017-02-24 16:04:47 -07001430 } else if (stateName.equals(kMSAAStateName)) {
1431 DisplayParams params = fWindow->getRequestedDisplayParams();
1432 int sampleCount = atoi(stateValue.c_str());
1433 if (sampleCount != params.fMSAASampleCount) {
1434 params.fMSAASampleCount = sampleCount;
1435 fWindow->setRequestedDisplayParams(params);
1436 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001437 this->updateTitle();
1438 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001439 }
1440 } else if (stateName.equals(kPathRendererStateName)) {
1441 DisplayParams params = fWindow->getRequestedDisplayParams();
1442 for (const auto& pair : gPathRendererNames) {
1443 if (pair.second == stateValue.c_str()) {
1444 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1445 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1446 fWindow->setRequestedDisplayParams(params);
1447 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001448 this->updateTitle();
1449 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001450 }
1451 break;
1452 }
csmartdalton578f0642017-02-24 16:04:47 -07001453 }
liyuqianb73c24b2016-06-03 08:47:23 -07001454 } else if (stateName.equals(kSoftkeyStateName)) {
1455 if (!stateValue.equals(kSoftkeyHint)) {
1456 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001457 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001458 }
liyuqian2edb0f42016-07-06 14:11:32 -07001459 } else if (stateName.equals(kRefreshStateName)) {
1460 // This state is actually NOT in the UI state.
1461 // We use this to allow Android to quickly set bool fRefresh.
1462 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001463 } else {
1464 SkDebugf("Unknown stateName: %s", stateName.c_str());
1465 }
1466}
Brian Osman79086b92017-02-10 13:36:16 -05001467
1468bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001469 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001470}
1471
1472bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001473 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001474 fWindow->inval();
1475 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001476 } else {
1477 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001478 }
Brian Osman79086b92017-02-10 13:36:16 -05001479}