blob: 0baa409acd1689eaa662a5f9905fed2debbb4e45 [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"
jvanverth9f372462016-04-06 06:08:59 -070033
Brian Osman79086b92017-02-10 13:36:16 -050034#include "imgui.h"
35
Chris Dalton1a325d22017-07-14 15:17:41 -060036#include "ccpr/GrCoverageCountingPathRenderer.h"
37
csmartdalton578f0642017-02-24 16:04:47 -070038#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070039#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070040
jvanverth34524262016-05-04 13:49:13 -070041using namespace sk_app;
42
csmartdalton61cd31a2017-02-27 17:00:53 -070043static std::map<GpuPathRenderers, std::string> gPathRendererNames;
44
jvanverth9f372462016-04-06 06:08:59 -070045Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070046 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070047}
48
Brian Osman2dd96932016-10-18 15:33:53 -040049static DEFINE_string2(match, m, nullptr,
jvanverth2bb3b6d2016-04-08 07:24:09 -070050 "[~][^]substring[$] [...] of bench name to run.\n"
51 "Multiple matches may be separated by spaces.\n"
52 "~ causes a matching bench to always be skipped\n"
53 "^ requires the start of the bench to match\n"
54 "$ requires the end of the bench to match\n"
55 "^ and $ requires an exact match\n"
56 "If a bench does not match any list entry,\n"
57 "it is skipped unless some list entry starts with ~");
bsalomon6c471f72016-07-26 12:56:32 -070058
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060059static DEFINE_string(slide, "", "Start on this sample.");
60static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050061
bsalomon6c471f72016-07-26 12:56:32 -070062#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070063# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070064#else
65# define BACKENDS_STR "\"sw\" and \"gl\""
66#endif
67
liyuqian71491dc2016-06-09 12:02:34 -070068#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -040069static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
70static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070071#else
Brian Osman2dd96932016-10-18 15:33:53 -040072static DEFINE_string(skps, "skps", "Directory to read skps from.");
73static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070074#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -070075
Brian Osman2dd96932016-10-18 15:33:53 -040076static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070077
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060078static DEFINE_int32(msaa, 0, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070079
Brian Osman53136aa2017-07-20 15:43:35 -040080DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050081
Brian Salomon194db172017-08-17 14:37:06 -040082const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070083 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040084#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
85 "ANGLE",
86#endif
jvanverth063ece72016-06-17 09:29:14 -070087#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070088 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070089#endif
csmartdalton578f0642017-02-24 16:04:47 -070090 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070091};
92
bsalomon6c471f72016-07-26 12:56:32 -070093static sk_app::Window::BackendType get_backend_type(const char* str) {
94#ifdef SK_VULKAN
95 if (0 == strcmp(str, "vk")) {
96 return sk_app::Window::kVulkan_BackendType;
97 } else
98#endif
Brian Salomon194db172017-08-17 14:37:06 -040099#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
100 if (0 == strcmp(str, "angle")) {
101 return sk_app::Window::kANGLE_BackendType;
102 } else
103#endif
bsalomon6c471f72016-07-26 12:56:32 -0700104 if (0 == strcmp(str, "gl")) {
105 return sk_app::Window::kNativeGL_BackendType;
106 } else if (0 == strcmp(str, "sw")) {
107 return sk_app::Window::kRaster_BackendType;
108 } else {
109 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
110 return sk_app::Window::kRaster_BackendType;
111 }
112}
113
Brian Osmana109e392017-02-24 09:49:14 -0500114static SkColorSpacePrimaries gSrgbPrimaries = {
115 0.64f, 0.33f,
116 0.30f, 0.60f,
117 0.15f, 0.06f,
118 0.3127f, 0.3290f };
119
120static SkColorSpacePrimaries gAdobePrimaries = {
121 0.64f, 0.33f,
122 0.21f, 0.71f,
123 0.15f, 0.06f,
124 0.3127f, 0.3290f };
125
126static SkColorSpacePrimaries gP3Primaries = {
127 0.680f, 0.320f,
128 0.265f, 0.690f,
129 0.150f, 0.060f,
130 0.3127f, 0.3290f };
131
132static SkColorSpacePrimaries gRec2020Primaries = {
133 0.708f, 0.292f,
134 0.170f, 0.797f,
135 0.131f, 0.046f,
136 0.3127f, 0.3290f };
137
138struct NamedPrimaries {
139 const char* fName;
140 SkColorSpacePrimaries* fPrimaries;
141} gNamedPrimaries[] = {
142 { "sRGB", &gSrgbPrimaries },
143 { "AdobeRGB", &gAdobePrimaries },
144 { "P3", &gP3Primaries },
145 { "Rec. 2020", &gRec2020Primaries },
146};
147
148static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
149 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
150}
151
Brian Osman70d2f432017-11-08 09:54:10 -0500152static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
153 // In raster mode, we still use GL for the window.
154 // This lets us render the GUI faster (and correct).
155 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
156}
157
liyuqiane5a6cd92016-05-27 08:52:52 -0700158const char* kName = "name";
159const char* kValue = "value";
160const char* kOptions = "options";
161const char* kSlideStateName = "Slide";
162const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700163const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700164const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700165const char* kSoftkeyStateName = "Softkey";
166const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700167const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700168const char* kON = "ON";
169const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700170const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700171
jvanverth34524262016-05-04 13:49:13 -0700172Viewer::Viewer(int argc, char** argv, void* platformData)
Brian Osman56a24812017-12-19 11:15:16 -0500173 : fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500174 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500175 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500176 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500177 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500178 , fShowZoomWindow(false)
179 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700180 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500181 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500182 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500183 // Our UI can only tweak gamma (currently), so start out gamma-only
184 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700185 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400186 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500187 , fTileCnt(0)
188 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700189{
Greg Daniel285db442016-10-14 09:12:53 -0400190 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700191
Brian Osmanf09e35e2017-12-15 14:48:09 -0500192 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
193 gPathRendererNames[GpuPathRenderers::kDefault] =
194 "Default Ganesh Behavior (best path renderer, not including CCPR)";
195 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
196 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
197 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
198 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
199 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
200 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700201
jvanverth2bb3b6d2016-04-08 07:24:09 -0700202 SkDebugf("Command line arguments: ");
203 for (int i = 1; i < argc; ++i) {
204 SkDebugf("%s ", argv[i]);
205 }
206 SkDebugf("\n");
207
208 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500209#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400210 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500211#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700212
Brian Osmanbc8150f2017-07-24 11:38:01 -0400213 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400214 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400215
bsalomon6c471f72016-07-26 12:56:32 -0700216 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700217 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700218
csmartdalton578f0642017-02-24 16:04:47 -0700219 DisplayParams displayParams;
220 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700221 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700222 fWindow->setRequestedDisplayParams(displayParams);
223
Brian Osman56a24812017-12-19 11:15:16 -0500224 // Configure timers
225 fStatsLayer.setActive(false);
226 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
227 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
228 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
229
jvanverth9f372462016-04-06 06:08:59 -0700230 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700231 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500232 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500233 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500234 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700235
brianosman622c8d52016-05-10 06:50:49 -0700236 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500237 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
238 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
239 fWindow->inval();
240 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500241 // Command to jump directly to the slide picker and give it focus
242 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
243 this->fShowImGuiDebugWindow = true;
244 this->fShowSlidePicker = true;
245 fWindow->inval();
246 });
247 // Alias that to Backspace, to match SampleApp
248 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
249 this->fShowImGuiDebugWindow = true;
250 this->fShowSlidePicker = true;
251 fWindow->inval();
252 });
Brian Osman79086b92017-02-10 13:36:16 -0500253 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
254 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
255 fWindow->inval();
256 });
Brian Osmanf6877092017-02-13 09:39:57 -0500257 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
258 this->fShowZoomWindow = !this->fShowZoomWindow;
259 fWindow->inval();
260 });
brianosman622c8d52016-05-10 06:50:49 -0700261 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500262 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700263 fWindow->inval();
264 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400265 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500266 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400267 this->updateTitle();
268 fWindow->inval();
269 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500270 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500271 switch (fColorMode) {
272 case ColorMode::kLegacy:
273 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
274 break;
275 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
276 this->setColorMode(ColorMode::kColorManagedSRGB8888);
277 break;
278 case ColorMode::kColorManagedSRGB8888:
279 this->setColorMode(ColorMode::kColorManagedLinearF16);
280 break;
281 case ColorMode::kColorManagedLinearF16:
282 this->setColorMode(ColorMode::kLegacy);
283 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500284 }
brianosman622c8d52016-05-10 06:50:49 -0700285 });
286 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
287 int previousSlide = fCurrentSlide;
288 fCurrentSlide++;
289 if (fCurrentSlide >= fSlides.count()) {
290 fCurrentSlide = 0;
291 }
292 this->setupCurrentSlide(previousSlide);
293 });
294 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
295 int previousSlide = fCurrentSlide;
296 fCurrentSlide--;
297 if (fCurrentSlide < 0) {
298 fCurrentSlide = fSlides.count() - 1;
299 }
300 this->setupCurrentSlide(previousSlide);
301 });
302 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
303 this->changeZoomLevel(1.f / 32.f);
304 fWindow->inval();
305 });
306 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
307 this->changeZoomLevel(-1.f / 32.f);
308 fWindow->inval();
309 });
jvanverthaf236b52016-05-20 06:01:06 -0700310 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400311 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
312 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500313 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400314#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
315 if (newBackend == sk_app::Window::kVulkan_BackendType) {
316 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
317 sk_app::Window::kBackendTypeCount);
318 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
319 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500320 }
321#endif
Brian Osman621491e2017-02-28 15:45:01 -0500322 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700323 });
brianosman622c8d52016-05-10 06:50:49 -0700324
Yuqian Lib2ba6642017-11-22 12:07:41 -0500325 fCommands.addCommand('A', "AA", "Toggle analytic AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400326 if (!gSkUseAnalyticAA) {
327 gSkUseAnalyticAA = true;
328 } else if (!gSkForceAnalyticAA) {
329 gSkForceAnalyticAA = true;
330 } else {
331 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
332 }
333 this->updateTitle();
334 fWindow->inval();
335 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500336 fCommands.addCommand('D', "AA", "Toggle delta AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400337 if (!gSkUseDeltaAA) {
338 gSkUseDeltaAA = true;
339 } else if (!gSkForceDeltaAA) {
340 gSkForceDeltaAA = true;
341 } else {
342 gSkUseDeltaAA = gSkForceDeltaAA = false;
343 }
344 this->updateTitle();
345 fWindow->inval();
346 });
347
Yuqian Lib2ba6642017-11-22 12:07:41 -0500348 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
349 fTileCnt++;
350 if (fThreadCnt == 0) {
351 this->resetExecutor();
352 }
353 this->updateTitle();
354 fWindow->inval();
355 });
356 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
357 fTileCnt = SkTMax(0, fTileCnt - 1);
358 if (fThreadCnt == 0) {
359 this->resetExecutor();
360 }
361 this->updateTitle();
362 fWindow->inval();
363 });
364 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
365 if (fTileCnt == 0) {
366 return;
367 }
368 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
369 this->resetExecutor();
370 this->updateTitle();
371 fWindow->inval();
372 });
373 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
374 if (fTileCnt == 0) {
375 return;
376 }
377 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
378 this->resetExecutor();
379 this->updateTitle();
380 fWindow->inval();
381 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500382 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
383 fSaveToSKP = true;
384 fWindow->inval();
385 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500386
jvanverth2bb3b6d2016-04-08 07:24:09 -0700387 // set up slides
388 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500389 this->setStartupSlide();
390 if (FLAGS_list) {
391 this->listNames();
392 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700393
djsollen12d62a72016-04-21 07:59:44 -0700394 fAnimTimer.run();
395
Hal Canaryc465d132017-12-08 10:21:31 -0500396 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500397 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400398 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500399 }
400 fImGuiGamutPaint.setColor(SK_ColorWHITE);
401 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
402
Brian Osman70d2f432017-11-08 09:54:10 -0500403 fWindow->attach(backend_type_for_window(fBackendType));
jvanverth9f372462016-04-06 06:08:59 -0700404}
405
jvanverth34524262016-05-04 13:49:13 -0700406void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700407 fAllSlideNames = Json::Value(Json::arrayValue);
408
jvanverth2bb3b6d2016-04-08 07:24:09 -0700409 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
410 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400411 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700412
413 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
414 sk_sp<Slide> slide(new GMSlide(gm.release()));
415 fSlides.push_back(slide);
416 }
417
418 gms = gms->next();
419 }
420
421 // reverse array
422 for (int i = 0; i < fSlides.count()/2; ++i) {
423 sk_sp<Slide> temp = fSlides[i];
424 fSlides[i] = fSlides[fSlides.count() - i - 1];
425 fSlides[fSlides.count() - i - 1] = temp;
426 }
427
jvanverthc7027ab2016-06-16 09:52:35 -0700428 // samples
429 const SkViewRegister* reg = SkViewRegister::Head();
430 while (reg) {
431 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700432 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
433 fSlides.push_back(slide);
434 }
jvanverthc7027ab2016-06-16 09:52:35 -0700435 reg = reg->next();
436 }
437
jvanverth2bb3b6d2016-04-08 07:24:09 -0700438 // SKPs
439 for (int i = 0; i < FLAGS_skps.count(); i++) {
440 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700441 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
442 continue;
443 }
444
jvanverth2bb3b6d2016-04-08 07:24:09 -0700445 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700446 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700447 if (slide) {
448 fSlides.push_back(slide);
449 }
450 } else {
451 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700452 SkString skpName;
453 while (it.next(&skpName)) {
454 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
455 continue;
456 }
457
458 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
459 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700460 if (slide) {
461 fSlides.push_back(slide);
462 }
463 }
464 }
465 }
liyuqian6f163d22016-06-13 12:26:45 -0700466
467 // JPGs
468 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
469 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
470 SkString jpgName;
471 while (it.next(&jpgName)) {
brianosmane1d20072016-07-12 09:07:33 -0700472 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) {
473 continue;
474 }
475
liyuqian6f163d22016-06-13 12:26:45 -0700476 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
477 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
478 if (slide) {
479 fSlides.push_back(slide);
480 }
481 }
482 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700483}
484
485
jvanverth34524262016-05-04 13:49:13 -0700486Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700487 fWindow->detach();
488 delete fWindow;
489}
490
brianosman05de2162016-05-06 13:28:57 -0700491void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700492 if (!fWindow) {
493 return;
494 }
495 if (fWindow->sampleCount() < 0) {
496 return; // Surface hasn't been created yet.
497 }
498
jvanverth34524262016-05-04 13:49:13 -0700499 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700500 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700501
Yuqian Li399b3c22017-08-03 11:08:15 -0400502 if (gSkUseDeltaAA) {
503 if (gSkForceDeltaAA) {
504 title.append(" <FDAA>");
505 } else {
506 title.append(" <DAA>");
507 }
508 } else if (gSkUseAnalyticAA) {
509 if (gSkForceAnalyticAA) {
510 title.append(" <FAAA>");
511 } else {
512 title.append(" <AAA>");
513 }
514 }
515
Yuqian Lib2ba6642017-11-22 12:07:41 -0500516 if (fTileCnt > 0) {
517 title.appendf(" T%d", fTileCnt);
518 if (fThreadCnt > 0) {
519 title.appendf("/%d", fThreadCnt);
520 }
521 }
522
Brian Osman92004802017-03-06 11:47:26 -0500523 switch (fColorMode) {
524 case ColorMode::kLegacy:
525 title.append(" Legacy 8888");
526 break;
527 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
528 title.append(" ColorManaged 8888 (Nonlinear blending)");
529 break;
530 case ColorMode::kColorManagedSRGB8888:
531 title.append(" ColorManaged 8888");
532 break;
533 case ColorMode::kColorManagedLinearF16:
534 title.append(" ColorManaged F16");
535 break;
536 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500537
Brian Osman92004802017-03-06 11:47:26 -0500538 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500539 int curPrimaries = -1;
540 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
541 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
542 curPrimaries = i;
543 break;
544 }
545 }
546 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500547
548 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
549 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
550 }
brianosman05de2162016-05-06 13:28:57 -0700551 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500552
csmartdalton578f0642017-02-24 16:04:47 -0700553 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700554 title.append(kBackendTypeStrings[fBackendType]);
csmartdalton578f0642017-02-24 16:04:47 -0700555 if (int msaa = fWindow->sampleCount()) {
556 title.appendf(" MSAA: %i", msaa);
557 }
558 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700559
560 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400561 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700562 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
563 }
564
brianosman05de2162016-05-06 13:28:57 -0700565 fWindow->setTitle(title.c_str());
566}
567
Jim Van Verth6f449692017-02-14 15:16:46 -0500568void Viewer::setStartupSlide() {
569
570 if (!FLAGS_slide.isEmpty()) {
571 int count = fSlides.count();
572 for (int i = 0; i < count; i++) {
573 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
574 fCurrentSlide = i;
575 return;
576 }
577 }
578
579 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
580 this->listNames();
581 }
582
583 fCurrentSlide = 0;
584}
585
586void Viewer::listNames() {
587 int count = fSlides.count();
588 SkDebugf("All Slides:\n");
589 for (int i = 0; i < count; i++) {
590 SkDebugf(" %s\n", fSlides[i]->getName().c_str());
591 }
592}
593
brianosman05de2162016-05-06 13:28:57 -0700594void Viewer::setupCurrentSlide(int previousSlide) {
liyuqiane5a6cd92016-05-27 08:52:52 -0700595 if (fCurrentSlide == previousSlide) {
596 return; // no change; do nothing
597 }
liyuqian6f163d22016-06-13 12:26:45 -0700598 // prepare dimensions for image slides
jvanverthc7027ab2016-06-16 09:52:35 -0700599 fSlides[fCurrentSlide]->load(SkIntToScalar(fWindow->width()), SkIntToScalar(fWindow->height()));
liyuqian6f163d22016-06-13 12:26:45 -0700600
liyuqiane46e4f02016-05-20 07:32:19 -0700601 fGesture.reset();
602 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700603
Brian Osman42bb6ac2017-06-05 08:46:04 -0400604 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
605 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
606 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
607
608 // Start with a matrix that scales the slide to the available screen space
609 if (fWindow->scaleContentToFit()) {
610 if (windowRect.width() > 0 && windowRect.height() > 0) {
611 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
liyuqiane46e4f02016-05-20 07:32:19 -0700612 }
613 }
614
Brian Osman42bb6ac2017-06-05 08:46:04 -0400615 // Prevent the user from dragging content so far outside the window they can't find it again
616 fGesture.setTransLimit(slideBounds, windowRect, fDefaultMatrix);
liyuqiane46e4f02016-05-20 07:32:19 -0700617
brianosman05de2162016-05-06 13:28:57 -0700618 this->updateTitle();
liyuqiane5a6cd92016-05-27 08:52:52 -0700619 this->updateUIState();
jvanverthc265a922016-04-08 12:51:45 -0700620 if (previousSlide >= 0) {
621 fSlides[previousSlide]->unload();
622 }
Jim Van Verth90dcce52017-11-03 13:36:07 -0400623
Brian Osman56a24812017-12-19 11:15:16 -0500624 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400625
jvanverthc265a922016-04-08 12:51:45 -0700626 fWindow->inval();
627}
628
629#define MAX_ZOOM_LEVEL 8
630#define MIN_ZOOM_LEVEL -8
631
jvanverth34524262016-05-04 13:49:13 -0700632void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700633 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400634 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
jvanverthc265a922016-04-08 12:51:45 -0700635}
636
liyuqiand3cdbca2016-05-17 12:44:20 -0700637SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700638 SkMatrix m;
jvanverthc265a922016-04-08 12:51:45 -0700639
Brian Osman42bb6ac2017-06-05 08:46:04 -0400640 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
641 : SK_Scalar1 + fZoomLevel;
642 m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700643 m.preConcat(fGesture.globalM());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400644 m.preConcat(fDefaultMatrix);
645 m.preScale(zoomScale, zoomScale);
jvanverthc265a922016-04-08 12:51:45 -0700646
liyuqiand3cdbca2016-05-17 12:44:20 -0700647 return m;
jvanverthc265a922016-04-08 12:51:45 -0700648}
649
Brian Osman621491e2017-02-28 15:45:01 -0500650void Viewer::setBackend(sk_app::Window::BackendType backendType) {
651 fBackendType = backendType;
652
653 fWindow->detach();
654
Brian Osman70d2f432017-11-08 09:54:10 -0500655#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400656 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
657 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500658 DisplayParams params = fWindow->getRequestedDisplayParams();
659 delete fWindow;
660 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500661
Brian Osman70d2f432017-11-08 09:54:10 -0500662 // re-register callbacks
663 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500664 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500665 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500666 fWindow->pushLayer(&fImGuiLayer);
667
Brian Osman70d2f432017-11-08 09:54:10 -0500668 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
669 // will still include our correct sample count. But the re-created fWindow will lose that
670 // information. On Windows, we need to re-create the window when changing sample count,
671 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
672 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
673 // as if we had never changed windows at all).
674 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500675#endif
676
Brian Osman70d2f432017-11-08 09:54:10 -0500677 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500678}
679
Brian Osman92004802017-03-06 11:47:26 -0500680void Viewer::setColorMode(ColorMode colorMode) {
681 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700682
Brian Osmanf750fbc2017-02-08 10:47:28 -0500683 // 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 -0400684 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700685 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500686 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400687 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
688 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500689 if (wasInLegacy != wantLegacy) {
690 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700691 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500692 }
693
694 this->updateTitle();
695 fWindow->inval();
696}
697
698void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500699 SkAutoCanvasRestore autorestore(canvas, false);
700
Brian Osmanf750fbc2017-02-08 10:47:28 -0500701 // By default, we render directly into the window's surface/canvas
702 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500703 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700704
Brian Osmane0d4fba2017-03-15 10:24:55 -0400705 // If we're in any of the color managed modes, construct the color space we're going to use
706 sk_sp<SkColorSpace> cs = nullptr;
707 if (ColorMode::kLegacy != fColorMode) {
708 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
709 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400710 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400711 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500712 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
713 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
714 } else {
715 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
716 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400717 }
718
Brian Osman3ac99cf2017-12-01 11:23:53 -0500719 if (fSaveToSKP) {
720 SkPictureRecorder recorder;
721 SkCanvas* recorderCanvas = recorder.beginRecording(
722 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
723 // In xform-canvas mode, record the transformed output
724 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
725 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
726 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
727 recorderCanvas = xformCanvas.get();
728 }
729 fSlides[fCurrentSlide]->draw(recorderCanvas);
730 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
731 SkFILEWStream stream("sample_app.skp");
732 picture->serialize(&stream);
733 fSaveToSKP = false;
734 }
735
Brian Osmane0d4fba2017-03-15 10:24:55 -0400736 // 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 -0500737 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
738 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500739 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -0500740 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
741 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -0500742 if (Window::kRaster_BackendType == fBackendType ||
743 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500744 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400745 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
746 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
747
Brian Osman92004802017-03-06 11:47:26 -0500748 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
749 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400750 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
751 // with a special canvas (below) that has the color space attached
752 sk_sp<SkColorSpace> offscreenColorSpace =
753 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500754 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400755 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500756 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
757 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -0500758 SkPixmap offscreenPixmap;
759 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
760 SkBitmap offscreenBitmap;
761 offscreenBitmap.installPixels(offscreenPixmap);
762 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
763 fThreadCnt, fExecutor.get()));
764 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
765 slideCanvas = threadedCanvas.get();
766 } else {
767 slideCanvas = offscreenSurface->getCanvas();
768 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500769 }
770
Brian Osmane0d4fba2017-03-15 10:24:55 -0400771 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
772 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
773 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
774 slideCanvas = xformCanvas.get();
775 }
776
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500777 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500778 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500779 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500780 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -0500781 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500782 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osman56a24812017-12-19 11:15:16 -0500783 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500784 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500785
786 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -0500787 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -0500788 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -0500789 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500790
791 // If we rendered offscreen, snap an image and push the results to the window's canvas
792 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500793 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500794
795 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400796 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500797 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400798 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500799 SkPaint paint;
800 paint.setBlendMode(SkBlendMode::kSrc);
801 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -0700802 }
liyuqian6f163d22016-06-13 12:26:45 -0700803}
804
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700805void Viewer::onBackendCreated() {
806 this->updateTitle();
807 this->updateUIState();
808 this->setupCurrentSlide(-1);
Brian Osman56a24812017-12-19 11:15:16 -0500809 fStatsLayer.resetMeasurements();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700810 fWindow->show();
811 fWindow->inval();
812}
Jim Van Verth6f449692017-02-14 15:16:46 -0500813
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700814void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -0400815 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -0700816
brianosman622c8d52016-05-10 06:50:49 -0700817 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -0700818
Brian Osmand67e5182017-12-08 16:46:09 -0500819 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -0500820
Brian Osman1df161a2017-02-09 12:10:20 -0500821 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -0400822 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700823}
824
jvanverth814e38d2016-06-06 08:48:47 -0700825bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400826 if (GestureDevice::kMouse == fGestureDevice) {
827 return false;
828 }
liyuqiand3cdbca2016-05-17 12:44:20 -0700829 void* castedOwner = reinterpret_cast<void*>(owner);
830 switch (state) {
831 case Window::kUp_InputState: {
832 fGesture.touchEnd(castedOwner);
833 break;
834 }
835 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400836 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700837 break;
838 }
839 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400840 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700841 break;
842 }
843 }
Brian Osmanb53f48c2017-06-07 10:00:30 -0400844 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -0700845 fWindow->inval();
846 return true;
847}
848
Brian Osman80fc07e2017-12-08 16:45:43 -0500849bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -0500850 if (!fSlides[fCurrentSlide]->onMouse(x, y, state, modifiers)) {
851 if (GestureDevice::kTouch == fGestureDevice) {
852 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -0500853 }
Brian Osmand67e5182017-12-08 16:46:09 -0500854 switch (state) {
855 case Window::kUp_InputState: {
856 fGesture.touchEnd(nullptr);
857 break;
858 }
859 case Window::kDown_InputState: {
860 fGesture.touchBegin(nullptr, x, y);
861 break;
862 }
863 case Window::kMove_InputState: {
864 fGesture.touchMoved(nullptr, x, y);
865 break;
866 }
867 }
868 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
Brian Osman80fc07e2017-12-08 16:45:43 -0500869 }
Brian Osmand67e5182017-12-08 16:46:09 -0500870 fWindow->inval();
Jim Van Verthe7705782017-05-04 14:00:59 -0400871 return true;
872}
873
Brian Osmana109e392017-02-24 09:49:14 -0500874static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
875 const ImVec2& pos, const ImVec2& size) {
876 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
877 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
878
879 // Invisible 10x10 button
880 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
881 ImGui::InvisibleButton(label, ImVec2(10, 10));
882
883 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
884 ImGuiIO& io = ImGui::GetIO();
885 // Normalized mouse position, relative to our gamut box
886 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
887 // Clamp to edge of box, convert back to primary scale
888 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
889 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
890 }
891
892 if (ImGui::IsItemHovered()) {
893 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
894 }
895
896 // Return screen coordinates for the caller. We could just return center here, but we'd have
897 // one frame of lag during drag.
898 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
899}
900
901static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
902 ImDrawList* drawList = ImGui::GetWindowDrawList();
903
904 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
905 // width, and scale the height to maintain aspect ratio.
906 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
907 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
908 ImVec2 pos = ImGui::GetCursorScreenPos();
909
910 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
911 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
912 // Magic numbers are pixel locations of the origin and upper-right corner.
913 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
914 ImVec2(242, 61), ImVec2(1897, 1922));
915 ImVec2 endPos = ImGui::GetCursorPos();
916
917 // Primary markers
918 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
919 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
920 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
921 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
922
923 // Gamut triangle
924 drawList->AddCircle(r, 5.0f, 0xFF000040);
925 drawList->AddCircle(g, 5.0f, 0xFF004000);
926 drawList->AddCircle(b, 5.0f, 0xFF400000);
927 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
928 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
929
930 // Re-position cursor immediate after the diagram for subsequent controls
931 ImGui::SetCursorPos(endPos);
932}
933
Brian Osmand67e5182017-12-08 16:46:09 -0500934void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -0500935 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
936 if (fShowImGuiTestWindow) {
937 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
938 }
939
940 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -0500941 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
942 // always visible, we can end up in a layout feedback loop.
943 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -0500944 DisplayParams params = fWindow->getRequestedDisplayParams();
945 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -0500946 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
947 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -0500948 if (ImGui::CollapsingHeader("Backend")) {
949 int newBackend = static_cast<int>(fBackendType);
950 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
951 ImGui::SameLine();
952 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -0400953#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
954 ImGui::SameLine();
955 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
956#endif
Brian Osman621491e2017-02-28 15:45:01 -0500957#if defined(SK_VULKAN)
958 ImGui::SameLine();
959 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
960#endif
961 if (newBackend != fBackendType) {
962 fDeferredActions.push_back([=]() {
963 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
964 });
965 }
Brian Osman8a9de3d2017-03-01 14:59:05 -0500966
Brian Salomon99a33902017-03-07 15:16:34 -0500967 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400968 bool* wire = &params.fGrContextOptions.fWireframeMode;
969 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
970 paramsChanged = true;
971 }
Brian Salomon99a33902017-03-07 15:16:34 -0500972
Brian Osman28b12522017-03-08 17:10:24 -0500973 if (ctx) {
974 int sampleCount = fWindow->sampleCount();
975 ImGui::Text("MSAA: "); ImGui::SameLine();
976 ImGui::RadioButton("0", &sampleCount, 0); ImGui::SameLine();
977 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
978 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
979 ImGui::RadioButton("16", &sampleCount, 16);
980
981 if (sampleCount != params.fMSAASampleCount) {
982 params.fMSAASampleCount = sampleCount;
983 paramsChanged = true;
984 }
985 }
986
Brian Osman8a9de3d2017-03-01 14:59:05 -0500987 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -0500988 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -0500989 auto prButton = [&](GpuPathRenderers x) {
990 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -0500991 if (x != params.fGrContextOptions.fGpuPathRenderers) {
992 params.fGrContextOptions.fGpuPathRenderers = x;
993 paramsChanged = true;
994 }
Brian Osman8a9de3d2017-03-01 14:59:05 -0500995 }
996 };
997
998 if (!ctx) {
999 ImGui::RadioButton("Software", true);
1000 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001001 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001002 prButton(GpuPathRenderers::kAll);
1003 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1004 prButton(GpuPathRenderers::kStencilAndCover);
1005 }
1006 if (ctx->caps()->sampleShadingSupport()) {
1007 prButton(GpuPathRenderers::kMSAA);
1008 }
1009 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001010 prButton(GpuPathRenderers::kNone);
1011 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001012 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001013 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001014 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1015 prButton(GpuPathRenderers::kCoverageCounting);
1016 }
Jim Van Verth83010462017-03-16 08:45:39 -04001017 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001018 prButton(GpuPathRenderers::kTessellating);
1019 prButton(GpuPathRenderers::kNone);
1020 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001021 ImGui::TreePop();
1022 }
Brian Osman621491e2017-02-28 15:45:01 -05001023 }
1024
Brian Osmanfce09c52017-11-14 15:32:20 -05001025 if (fShowSlidePicker) {
1026 ImGui::SetNextTreeNodeOpen(true);
1027 }
1028
Brian Osman79086b92017-02-10 13:36:16 -05001029 if (ImGui::CollapsingHeader("Slide")) {
1030 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001031 static ImVector<const char*> filteredSlideNames;
1032 static ImVector<int> filteredSlideIndices;
1033
Brian Osmanfce09c52017-11-14 15:32:20 -05001034 if (fShowSlidePicker) {
1035 ImGui::SetKeyboardFocusHere();
1036 fShowSlidePicker = false;
1037 }
1038
Brian Osman79086b92017-02-10 13:36:16 -05001039 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001040 filteredSlideNames.clear();
1041 filteredSlideIndices.clear();
1042 int filteredIndex = 0;
1043 for (int i = 0; i < fSlides.count(); ++i) {
1044 const char* slideName = fSlides[i]->getName().c_str();
1045 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1046 if (i == fCurrentSlide) {
1047 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001048 }
Brian Osmanf479e422017-11-08 13:11:36 -05001049 filteredSlideNames.push_back(slideName);
1050 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001051 }
Brian Osman79086b92017-02-10 13:36:16 -05001052 }
Brian Osmanf479e422017-11-08 13:11:36 -05001053
1054 int previousSlide = fCurrentSlide;
1055 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1056 filteredSlideNames.size(), 20)) {
1057 fCurrentSlide = filteredSlideIndices[filteredIndex];
1058 setupCurrentSlide(previousSlide);
Brian Osman79086b92017-02-10 13:36:16 -05001059 }
1060 }
Brian Osmana109e392017-02-24 09:49:14 -05001061
1062 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001063 ColorMode newMode = fColorMode;
1064 auto cmButton = [&](ColorMode mode, const char* label) {
1065 if (ImGui::RadioButton(label, mode == fColorMode)) {
1066 newMode = mode;
1067 }
1068 };
1069
1070 cmButton(ColorMode::kLegacy, "Legacy 8888");
1071 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1072 "Color Managed 8888 (Nonlinear blending)");
1073 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1074 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1075
1076 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001077 // It isn't safe to switch color mode now (in the middle of painting). We might
1078 // tear down the back-end, etc... Defer this change until the next onIdle.
1079 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001080 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001081 });
Brian Osmana109e392017-02-24 09:49:14 -05001082 }
1083
1084 // Pick from common gamuts:
1085 int primariesIdx = 4; // Default: Custom
1086 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1087 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1088 primariesIdx = i;
1089 break;
1090 }
1091 }
1092
Brian Osmanfdab5762017-11-09 10:27:55 -05001093 // When we're in xform canvas mode, we can alter the transfer function, too
1094 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1095 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1096 }
1097
Brian Osmana109e392017-02-24 09:49:14 -05001098 if (ImGui::Combo("Primaries", &primariesIdx,
1099 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1100 if (primariesIdx >= 0 && primariesIdx <= 3) {
1101 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1102 }
1103 }
1104
1105 // Allow direct editing of gamut
1106 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1107 }
Brian Osman79086b92017-02-10 13:36:16 -05001108 }
Brian Salomon99a33902017-03-07 15:16:34 -05001109 if (paramsChanged) {
1110 fDeferredActions.push_back([=]() {
1111 fWindow->setRequestedDisplayParams(params);
1112 fWindow->inval();
1113 this->updateTitle();
1114 });
1115 }
Brian Osman79086b92017-02-10 13:36:16 -05001116 ImGui::End();
1117 }
1118
Brian Osmanf6877092017-02-13 09:39:57 -05001119 if (fShowZoomWindow && fLastImage) {
1120 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001121 static int zoomFactor = 8;
1122 if (ImGui::Button("<<")) {
1123 zoomFactor = SkTMax(zoomFactor / 2, 4);
1124 }
1125 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1126 if (ImGui::Button(">>")) {
1127 zoomFactor = SkTMin(zoomFactor * 2, 32);
1128 }
Brian Osmanf6877092017-02-13 09:39:57 -05001129
Brian Osmanf6877092017-02-13 09:39:57 -05001130 ImVec2 mousePos = ImGui::GetMousePos();
1131 ImVec2 avail = ImGui::GetContentRegionAvail();
1132
Brian Osmanead517d2017-11-13 15:36:36 -05001133 uint32_t pixel = 0;
1134 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1135 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1136 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001137 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1138 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1139 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001140 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1141 }
1142
Brian Osmand67e5182017-12-08 16:46:09 -05001143 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001144 // Translate so the region of the image that's under the mouse cursor is centered
1145 // in the zoom canvas:
1146 c->scale(zoomFactor, zoomFactor);
1147 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1148 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1149 c->drawImage(this->fLastImage, 0, 0);
1150
1151 SkPaint outline;
1152 outline.setStyle(SkPaint::kStroke_Style);
1153 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1154 });
Brian Osmanf6877092017-02-13 09:39:57 -05001155 }
1156
1157 ImGui::End();
1158 }
Brian Osman79086b92017-02-10 13:36:16 -05001159}
1160
liyuqian2edb0f42016-07-06 14:11:32 -07001161void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001162 for (int i = 0; i < fDeferredActions.count(); ++i) {
1163 fDeferredActions[i]();
1164 }
1165 fDeferredActions.reset();
1166
Brian Osman56a24812017-12-19 11:15:16 -05001167 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001168 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001169 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001170 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001171
Brian Osman79086b92017-02-10 13:36:16 -05001172 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001173 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001174 fWindow->inval();
1175 }
jvanverth9f372462016-04-06 06:08:59 -07001176}
liyuqiane5a6cd92016-05-27 08:52:52 -07001177
1178void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001179 if (!fWindow) {
1180 return;
1181 }
1182 if (fWindow->sampleCount() < 0) {
1183 return; // Surface hasn't been created yet.
1184 }
1185
liyuqianb73c24b2016-06-03 08:47:23 -07001186 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001187 Json::Value slideState(Json::objectValue);
1188 slideState[kName] = kSlideStateName;
1189 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001190 if (fAllSlideNames.size() == 0) {
1191 for(auto slide : fSlides) {
1192 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1193 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001194 }
liyuqian1f508fd2016-06-07 06:57:40 -07001195 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001196
liyuqianb73c24b2016-06-03 08:47:23 -07001197 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001198 Json::Value backendState(Json::objectValue);
1199 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001200 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001201 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001202 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001203 backendState[kOptions].append(Json::Value(str));
1204 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001205
csmartdalton578f0642017-02-24 16:04:47 -07001206 // MSAA state
1207 Json::Value msaaState(Json::objectValue);
1208 msaaState[kName] = kMSAAStateName;
1209 msaaState[kValue] = fWindow->sampleCount();
1210 msaaState[kOptions] = Json::Value(Json::arrayValue);
1211 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1212 msaaState[kOptions].append(Json::Value(0));
1213 } else {
1214 for (int msaa : {0, 4, 8, 16}) {
1215 msaaState[kOptions].append(Json::Value(msaa));
1216 }
1217 }
1218
csmartdalton61cd31a2017-02-27 17:00:53 -07001219 // Path renderer state
1220 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1221 Json::Value prState(Json::objectValue);
1222 prState[kName] = kPathRendererStateName;
1223 prState[kValue] = gPathRendererNames[pr];
1224 prState[kOptions] = Json::Value(Json::arrayValue);
1225 const GrContext* ctx = fWindow->getGrContext();
1226 if (!ctx) {
1227 prState[kOptions].append("Software");
1228 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001229 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001230 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1231 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1232 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1233 }
1234 if (ctx->caps()->sampleShadingSupport()) {
1235 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1236 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001237 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001238 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1239 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001240 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001241 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001242 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1243 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1244 }
Jim Van Verth83010462017-03-16 08:45:39 -04001245 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001246 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001247 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1248 }
1249
liyuqianb73c24b2016-06-03 08:47:23 -07001250 // Softkey state
1251 Json::Value softkeyState(Json::objectValue);
1252 softkeyState[kName] = kSoftkeyStateName;
1253 softkeyState[kValue] = kSoftkeyHint;
1254 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1255 softkeyState[kOptions].append(kSoftkeyHint);
1256 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1257 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1258 }
1259
liyuqian1f508fd2016-06-07 06:57:40 -07001260 // FPS state
1261 Json::Value fpsState(Json::objectValue);
1262 fpsState[kName] = kFpsStateName;
Brian Osman56a24812017-12-19 11:15:16 -05001263 double animTime = fStatsLayer.getLastTime(fAnimateTimer);
1264 double paintTime = fStatsLayer.getLastTime(fPaintTimer);
1265 double flushTime = fStatsLayer.getLastTime(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001266 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
Brian Osman56a24812017-12-19 11:15:16 -05001267 animTime + paintTime + flushTime,
1268 animTime, paintTime, flushTime).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001269 fpsState[kOptions] = Json::Value(Json::arrayValue);
1270
liyuqiane5a6cd92016-05-27 08:52:52 -07001271 Json::Value state(Json::arrayValue);
1272 state.append(slideState);
1273 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001274 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001275 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001276 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001277 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001278
Brian Osmaneff04b52017-11-21 13:18:02 -05001279 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001280}
1281
1282void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001283 // For those who will add more features to handle the state change in this function:
1284 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1285 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1286 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001287 if (stateName.equals(kSlideStateName)) {
1288 int previousSlide = fCurrentSlide;
1289 fCurrentSlide = 0;
1290 for(auto slide : fSlides) {
1291 if (slide->getName().equals(stateValue)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001292 this->setupCurrentSlide(previousSlide);
liyuqiane5a6cd92016-05-27 08:52:52 -07001293 break;
1294 }
1295 fCurrentSlide++;
1296 }
1297 if (fCurrentSlide >= fSlides.count()) {
1298 fCurrentSlide = previousSlide;
1299 SkDebugf("Slide not found: %s", stateValue.c_str());
1300 }
liyuqian6cb70252016-06-02 12:16:25 -07001301 } else if (stateName.equals(kBackendStateName)) {
1302 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1303 if (stateValue.equals(kBackendTypeStrings[i])) {
1304 if (fBackendType != i) {
1305 fBackendType = (sk_app::Window::BackendType)i;
1306 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001307 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001308 }
1309 break;
1310 }
1311 }
csmartdalton578f0642017-02-24 16:04:47 -07001312 } else if (stateName.equals(kMSAAStateName)) {
1313 DisplayParams params = fWindow->getRequestedDisplayParams();
1314 int sampleCount = atoi(stateValue.c_str());
1315 if (sampleCount != params.fMSAASampleCount) {
1316 params.fMSAASampleCount = sampleCount;
1317 fWindow->setRequestedDisplayParams(params);
1318 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001319 this->updateTitle();
1320 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001321 }
1322 } else if (stateName.equals(kPathRendererStateName)) {
1323 DisplayParams params = fWindow->getRequestedDisplayParams();
1324 for (const auto& pair : gPathRendererNames) {
1325 if (pair.second == stateValue.c_str()) {
1326 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1327 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1328 fWindow->setRequestedDisplayParams(params);
1329 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001330 this->updateTitle();
1331 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001332 }
1333 break;
1334 }
csmartdalton578f0642017-02-24 16:04:47 -07001335 }
liyuqianb73c24b2016-06-03 08:47:23 -07001336 } else if (stateName.equals(kSoftkeyStateName)) {
1337 if (!stateValue.equals(kSoftkeyHint)) {
1338 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001339 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001340 }
liyuqian2edb0f42016-07-06 14:11:32 -07001341 } else if (stateName.equals(kRefreshStateName)) {
1342 // This state is actually NOT in the UI state.
1343 // We use this to allow Android to quickly set bool fRefresh.
1344 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001345 } else {
1346 SkDebugf("Unknown stateName: %s", stateName.c_str());
1347 }
1348}
Brian Osman79086b92017-02-10 13:36:16 -05001349
1350bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001351 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001352}
1353
1354bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001355 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001356 fWindow->inval();
1357 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001358 } else {
1359 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001360 }
Brian Osman79086b92017-02-10 13:36:16 -05001361}