blob: 19ecd42894f48993786e1956be78d5ad53c2d986 [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"
Brian Osmanf9810662017-08-30 10:02:10 -040021#include "SkCommonFlagsGpuThreads.h"
csmartdalton008b9d82017-02-22 12:00:42 -070022#include "SkCommonFlagsPathRenderer.h"
Brian Osman53136aa2017-07-20 15:43:35 -040023#include "SkEventTracingPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040024#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050025#include "SkImagePriv.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070026#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050027#include "SkOSPath.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050028#include "SkPictureRecorder.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040029#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070030#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070031#include "SkSurface.h"
csmartdalton29d87152017-02-10 17:05:14 -050032#include "SkTaskGroup.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050033#include "SkThreadedBMPDevice.h"
liyuqian2edb0f42016-07-06 14:11:32 -070034#include "SkTime.h"
jvanverth9f372462016-04-06 06:08:59 -070035
Brian Osman79086b92017-02-10 13:36:16 -050036#include "imgui.h"
37
Chris Dalton1a325d22017-07-14 15:17:41 -060038#include "ccpr/GrCoverageCountingPathRenderer.h"
39
csmartdalton578f0642017-02-24 16:04:47 -070040#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070041#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070042
jvanverth34524262016-05-04 13:49:13 -070043using namespace sk_app;
44
csmartdalton61cd31a2017-02-27 17:00:53 -070045static std::map<GpuPathRenderers, std::string> gPathRendererNames;
46
jvanverth9f372462016-04-06 06:08:59 -070047Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070048 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070049}
50
Brian Osman2dd96932016-10-18 15:33:53 -040051static DEFINE_string2(match, m, nullptr,
jvanverth2bb3b6d2016-04-08 07:24:09 -070052 "[~][^]substring[$] [...] of bench name to run.\n"
53 "Multiple matches may be separated by spaces.\n"
54 "~ causes a matching bench to always be skipped\n"
55 "^ requires the start of the bench to match\n"
56 "$ requires the end of the bench to match\n"
57 "^ and $ requires an exact match\n"
58 "If a bench does not match any list entry,\n"
59 "it is skipped unless some list entry starts with ~");
bsalomon6c471f72016-07-26 12:56:32 -070060
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060061static DEFINE_string(slide, "", "Start on this sample.");
62static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050063
bsalomon6c471f72016-07-26 12:56:32 -070064#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070065# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070066#else
67# define BACKENDS_STR "\"sw\" and \"gl\""
68#endif
69
liyuqian71491dc2016-06-09 12:02:34 -070070#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -040071static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
72static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070073#else
Brian Osman2dd96932016-10-18 15:33:53 -040074static DEFINE_string(skps, "skps", "Directory to read skps from.");
75static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070076#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -070077
Brian Osman2dd96932016-10-18 15:33:53 -040078static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070079
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060080static DEFINE_int32(msaa, 0, "Number of subpixel samples. 0 for no HW antialiasing.");
81static DEFINE_bool(cachePathMasks, true, "Allows path mask textures to be cached in GPU configs.");
csmartdalton008b9d82017-02-22 12:00:42 -070082DEFINE_pathrenderer_flag;
83
Brian Osman53136aa2017-07-20 15:43:35 -040084DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050085
Brian Salomon194db172017-08-17 14:37:06 -040086const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070087 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040088#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
89 "ANGLE",
90#endif
jvanverth063ece72016-06-17 09:29:14 -070091#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070092 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070093#endif
csmartdalton578f0642017-02-24 16:04:47 -070094 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070095};
96
bsalomon6c471f72016-07-26 12:56:32 -070097static sk_app::Window::BackendType get_backend_type(const char* str) {
98#ifdef SK_VULKAN
99 if (0 == strcmp(str, "vk")) {
100 return sk_app::Window::kVulkan_BackendType;
101 } else
102#endif
Brian Salomon194db172017-08-17 14:37:06 -0400103#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
104 if (0 == strcmp(str, "angle")) {
105 return sk_app::Window::kANGLE_BackendType;
106 } else
107#endif
bsalomon6c471f72016-07-26 12:56:32 -0700108 if (0 == strcmp(str, "gl")) {
109 return sk_app::Window::kNativeGL_BackendType;
110 } else if (0 == strcmp(str, "sw")) {
111 return sk_app::Window::kRaster_BackendType;
112 } else {
113 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
114 return sk_app::Window::kRaster_BackendType;
115 }
116}
117
Brian Osmana109e392017-02-24 09:49:14 -0500118static SkColorSpacePrimaries gSrgbPrimaries = {
119 0.64f, 0.33f,
120 0.30f, 0.60f,
121 0.15f, 0.06f,
122 0.3127f, 0.3290f };
123
124static SkColorSpacePrimaries gAdobePrimaries = {
125 0.64f, 0.33f,
126 0.21f, 0.71f,
127 0.15f, 0.06f,
128 0.3127f, 0.3290f };
129
130static SkColorSpacePrimaries gP3Primaries = {
131 0.680f, 0.320f,
132 0.265f, 0.690f,
133 0.150f, 0.060f,
134 0.3127f, 0.3290f };
135
136static SkColorSpacePrimaries gRec2020Primaries = {
137 0.708f, 0.292f,
138 0.170f, 0.797f,
139 0.131f, 0.046f,
140 0.3127f, 0.3290f };
141
142struct NamedPrimaries {
143 const char* fName;
144 SkColorSpacePrimaries* fPrimaries;
145} gNamedPrimaries[] = {
146 { "sRGB", &gSrgbPrimaries },
147 { "AdobeRGB", &gAdobePrimaries },
148 { "P3", &gP3Primaries },
149 { "Rec. 2020", &gRec2020Primaries },
150};
151
152static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
153 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
154}
155
Brian Osman70d2f432017-11-08 09:54:10 -0500156static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
157 // In raster mode, we still use GL for the window.
158 // This lets us render the GUI faster (and correct).
159 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
160}
161
liyuqiane5a6cd92016-05-27 08:52:52 -0700162const char* kName = "name";
163const char* kValue = "value";
164const char* kOptions = "options";
165const char* kSlideStateName = "Slide";
166const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700167const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700168const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700169const char* kSoftkeyStateName = "Softkey";
170const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700171const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700172const char* kON = "ON";
173const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700174const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700175
jvanverth34524262016-05-04 13:49:13 -0700176Viewer::Viewer(int argc, char** argv, void* platformData)
jvanverthc265a922016-04-08 12:51:45 -0700177 : fCurrentMeasurement(0)
Jim Van Verth90dcce52017-11-03 13:36:07 -0400178 , fCumulativeMeasurementTime(0)
179 , fCumulativeMeasurementCount(0)
jvanverthc265a922016-04-08 12:51:45 -0700180 , fDisplayStats(false)
liyuqian2edb0f42016-07-06 14:11:32 -0700181 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500182 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500183 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500184 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500185 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500186 , fShowZoomWindow(false)
187 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700188 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500189 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500190 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500191 // Our UI can only tweak gamma (currently), so start out gamma-only
192 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700193 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400194 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500195 , fTileCnt(0)
196 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700197{
Greg Daniel285db442016-10-14 09:12:53 -0400198 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700199
Brian Osmanf09e35e2017-12-15 14:48:09 -0500200 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
201 gPathRendererNames[GpuPathRenderers::kDefault] =
202 "Default Ganesh Behavior (best path renderer, not including CCPR)";
203 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
204 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
205 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
206 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
207 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
208 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700209
Brian Osman1df161a2017-02-09 12:10:20 -0500210 memset(fPaintTimes, 0, sizeof(fPaintTimes));
211 memset(fFlushTimes, 0, sizeof(fFlushTimes));
212 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
jvanverth9f372462016-04-06 06:08:59 -0700213
jvanverth2bb3b6d2016-04-08 07:24:09 -0700214 SkDebugf("Command line arguments: ");
215 for (int i = 1; i < argc; ++i) {
216 SkDebugf("%s ", argv[i]);
217 }
218 SkDebugf("\n");
219
220 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500221#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400222 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500223#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700224
Brian Osmanbc8150f2017-07-24 11:38:01 -0400225 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400226 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400227
bsalomon6c471f72016-07-26 12:56:32 -0700228 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700229 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700230
csmartdalton578f0642017-02-24 16:04:47 -0700231 DisplayParams displayParams;
232 displayParams.fMSAASampleCount = FLAGS_msaa;
csmartdalton61cd31a2017-02-27 17:00:53 -0700233 displayParams.fGrContextOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600234 displayParams.fGrContextOptions.fAllowPathMaskCaching = FLAGS_cachePathMasks;
Brian Osmanf9810662017-08-30 10:02:10 -0400235 displayParams.fGrContextOptions.fExecutor = GpuExecutorForTools();
csmartdalton578f0642017-02-24 16:04:47 -0700236 fWindow->setRequestedDisplayParams(displayParams);
237
jvanverth9f372462016-04-06 06:08:59 -0700238 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700239 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500240 fWindow->pushLayer(this);
Brian Osmand67e5182017-12-08 16:46:09 -0500241 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700242
brianosman622c8d52016-05-10 06:50:49 -0700243 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500244 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
245 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
246 fWindow->inval();
247 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500248 // Command to jump directly to the slide picker and give it focus
249 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
250 this->fShowImGuiDebugWindow = true;
251 this->fShowSlidePicker = true;
252 fWindow->inval();
253 });
254 // Alias that to Backspace, to match SampleApp
255 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
256 this->fShowImGuiDebugWindow = true;
257 this->fShowSlidePicker = true;
258 fWindow->inval();
259 });
Brian Osman79086b92017-02-10 13:36:16 -0500260 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
261 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
262 fWindow->inval();
263 });
Brian Osmanf6877092017-02-13 09:39:57 -0500264 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
265 this->fShowZoomWindow = !this->fShowZoomWindow;
266 fWindow->inval();
267 });
brianosman622c8d52016-05-10 06:50:49 -0700268 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
269 this->fDisplayStats = !this->fDisplayStats;
270 fWindow->inval();
271 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400272 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
273 this->resetMeasurements();
274 this->updateTitle();
275 fWindow->inval();
276 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500277 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500278 switch (fColorMode) {
279 case ColorMode::kLegacy:
280 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
281 break;
282 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
283 this->setColorMode(ColorMode::kColorManagedSRGB8888);
284 break;
285 case ColorMode::kColorManagedSRGB8888:
286 this->setColorMode(ColorMode::kColorManagedLinearF16);
287 break;
288 case ColorMode::kColorManagedLinearF16:
289 this->setColorMode(ColorMode::kLegacy);
290 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500291 }
brianosman622c8d52016-05-10 06:50:49 -0700292 });
293 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
294 int previousSlide = fCurrentSlide;
295 fCurrentSlide++;
296 if (fCurrentSlide >= fSlides.count()) {
297 fCurrentSlide = 0;
298 }
299 this->setupCurrentSlide(previousSlide);
300 });
301 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
302 int previousSlide = fCurrentSlide;
303 fCurrentSlide--;
304 if (fCurrentSlide < 0) {
305 fCurrentSlide = fSlides.count() - 1;
306 }
307 this->setupCurrentSlide(previousSlide);
308 });
309 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
310 this->changeZoomLevel(1.f / 32.f);
311 fWindow->inval();
312 });
313 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
314 this->changeZoomLevel(-1.f / 32.f);
315 fWindow->inval();
316 });
jvanverthaf236b52016-05-20 06:01:06 -0700317 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400318 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
319 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500320 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400321#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
322 if (newBackend == sk_app::Window::kVulkan_BackendType) {
323 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
324 sk_app::Window::kBackendTypeCount);
325 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
326 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500327 }
328#endif
Brian Osman621491e2017-02-28 15:45:01 -0500329 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700330 });
brianosman622c8d52016-05-10 06:50:49 -0700331
Yuqian Lib2ba6642017-11-22 12:07:41 -0500332 fCommands.addCommand('A', "AA", "Toggle analytic AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400333 if (!gSkUseAnalyticAA) {
334 gSkUseAnalyticAA = true;
335 } else if (!gSkForceAnalyticAA) {
336 gSkForceAnalyticAA = true;
337 } else {
338 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
339 }
340 this->updateTitle();
341 fWindow->inval();
342 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500343 fCommands.addCommand('D', "AA", "Toggle delta AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400344 if (!gSkUseDeltaAA) {
345 gSkUseDeltaAA = true;
346 } else if (!gSkForceDeltaAA) {
347 gSkForceDeltaAA = true;
348 } else {
349 gSkUseDeltaAA = gSkForceDeltaAA = false;
350 }
351 this->updateTitle();
352 fWindow->inval();
353 });
354
Yuqian Lib2ba6642017-11-22 12:07:41 -0500355 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
356 fTileCnt++;
357 if (fThreadCnt == 0) {
358 this->resetExecutor();
359 }
360 this->updateTitle();
361 fWindow->inval();
362 });
363 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
364 fTileCnt = SkTMax(0, fTileCnt - 1);
365 if (fThreadCnt == 0) {
366 this->resetExecutor();
367 }
368 this->updateTitle();
369 fWindow->inval();
370 });
371 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
372 if (fTileCnt == 0) {
373 return;
374 }
375 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
376 this->resetExecutor();
377 this->updateTitle();
378 fWindow->inval();
379 });
380 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
381 if (fTileCnt == 0) {
382 return;
383 }
384 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
385 this->resetExecutor();
386 this->updateTitle();
387 fWindow->inval();
388 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500389 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
390 fSaveToSKP = true;
391 fWindow->inval();
392 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500393
jvanverth2bb3b6d2016-04-08 07:24:09 -0700394 // set up slides
395 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500396 this->setStartupSlide();
397 if (FLAGS_list) {
398 this->listNames();
399 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700400
djsollen12d62a72016-04-21 07:59:44 -0700401 fAnimTimer.run();
402
Hal Canaryc465d132017-12-08 10:21:31 -0500403 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500404 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400405 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500406 }
407 fImGuiGamutPaint.setColor(SK_ColorWHITE);
408 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
409
Brian Osman70d2f432017-11-08 09:54:10 -0500410 fWindow->attach(backend_type_for_window(fBackendType));
jvanverth9f372462016-04-06 06:08:59 -0700411}
412
jvanverth34524262016-05-04 13:49:13 -0700413void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700414 fAllSlideNames = Json::Value(Json::arrayValue);
415
jvanverth2bb3b6d2016-04-08 07:24:09 -0700416 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
417 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400418 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700419
420 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
421 sk_sp<Slide> slide(new GMSlide(gm.release()));
422 fSlides.push_back(slide);
423 }
424
425 gms = gms->next();
426 }
427
428 // reverse array
429 for (int i = 0; i < fSlides.count()/2; ++i) {
430 sk_sp<Slide> temp = fSlides[i];
431 fSlides[i] = fSlides[fSlides.count() - i - 1];
432 fSlides[fSlides.count() - i - 1] = temp;
433 }
434
jvanverthc7027ab2016-06-16 09:52:35 -0700435 // samples
436 const SkViewRegister* reg = SkViewRegister::Head();
437 while (reg) {
438 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700439 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
440 fSlides.push_back(slide);
441 }
jvanverthc7027ab2016-06-16 09:52:35 -0700442 reg = reg->next();
443 }
444
jvanverth2bb3b6d2016-04-08 07:24:09 -0700445 // SKPs
446 for (int i = 0; i < FLAGS_skps.count(); i++) {
447 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700448 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
449 continue;
450 }
451
jvanverth2bb3b6d2016-04-08 07:24:09 -0700452 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700453 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700454 if (slide) {
455 fSlides.push_back(slide);
456 }
457 } else {
458 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700459 SkString skpName;
460 while (it.next(&skpName)) {
461 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
462 continue;
463 }
464
465 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
466 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700467 if (slide) {
468 fSlides.push_back(slide);
469 }
470 }
471 }
472 }
liyuqian6f163d22016-06-13 12:26:45 -0700473
474 // JPGs
475 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
476 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
477 SkString jpgName;
478 while (it.next(&jpgName)) {
brianosmane1d20072016-07-12 09:07:33 -0700479 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) {
480 continue;
481 }
482
liyuqian6f163d22016-06-13 12:26:45 -0700483 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
484 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
485 if (slide) {
486 fSlides.push_back(slide);
487 }
488 }
489 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700490}
491
492
jvanverth34524262016-05-04 13:49:13 -0700493Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700494 fWindow->detach();
495 delete fWindow;
496}
497
brianosman05de2162016-05-06 13:28:57 -0700498void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700499 if (!fWindow) {
500 return;
501 }
502 if (fWindow->sampleCount() < 0) {
503 return; // Surface hasn't been created yet.
504 }
505
jvanverth34524262016-05-04 13:49:13 -0700506 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700507 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700508
Yuqian Li399b3c22017-08-03 11:08:15 -0400509 if (gSkUseDeltaAA) {
510 if (gSkForceDeltaAA) {
511 title.append(" <FDAA>");
512 } else {
513 title.append(" <DAA>");
514 }
515 } else if (gSkUseAnalyticAA) {
516 if (gSkForceAnalyticAA) {
517 title.append(" <FAAA>");
518 } else {
519 title.append(" <AAA>");
520 }
521 }
522
Yuqian Lib2ba6642017-11-22 12:07:41 -0500523 if (fTileCnt > 0) {
524 title.appendf(" T%d", fTileCnt);
525 if (fThreadCnt > 0) {
526 title.appendf("/%d", fThreadCnt);
527 }
528 }
529
Brian Osman92004802017-03-06 11:47:26 -0500530 switch (fColorMode) {
531 case ColorMode::kLegacy:
532 title.append(" Legacy 8888");
533 break;
534 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
535 title.append(" ColorManaged 8888 (Nonlinear blending)");
536 break;
537 case ColorMode::kColorManagedSRGB8888:
538 title.append(" ColorManaged 8888");
539 break;
540 case ColorMode::kColorManagedLinearF16:
541 title.append(" ColorManaged F16");
542 break;
543 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500544
Brian Osman92004802017-03-06 11:47:26 -0500545 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500546 int curPrimaries = -1;
547 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
548 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
549 curPrimaries = i;
550 break;
551 }
552 }
553 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500554
555 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
556 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
557 }
brianosman05de2162016-05-06 13:28:57 -0700558 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500559
csmartdalton578f0642017-02-24 16:04:47 -0700560 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700561 title.append(kBackendTypeStrings[fBackendType]);
csmartdalton578f0642017-02-24 16:04:47 -0700562 if (int msaa = fWindow->sampleCount()) {
563 title.appendf(" MSAA: %i", msaa);
564 }
565 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700566
567 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400568 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700569 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
570 }
571
brianosman05de2162016-05-06 13:28:57 -0700572 fWindow->setTitle(title.c_str());
573}
574
Jim Van Verth6f449692017-02-14 15:16:46 -0500575void Viewer::setStartupSlide() {
576
577 if (!FLAGS_slide.isEmpty()) {
578 int count = fSlides.count();
579 for (int i = 0; i < count; i++) {
580 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
581 fCurrentSlide = i;
582 return;
583 }
584 }
585
586 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
587 this->listNames();
588 }
589
590 fCurrentSlide = 0;
591}
592
593void Viewer::listNames() {
594 int count = fSlides.count();
595 SkDebugf("All Slides:\n");
596 for (int i = 0; i < count; i++) {
597 SkDebugf(" %s\n", fSlides[i]->getName().c_str());
598 }
599}
600
Jim Van Verth90dcce52017-11-03 13:36:07 -0400601void Viewer::resetMeasurements() {
602 memset(fPaintTimes, 0, sizeof(fPaintTimes));
603 memset(fFlushTimes, 0, sizeof(fFlushTimes));
604 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
605 fCurrentMeasurement = 0;
606 fCumulativeMeasurementTime = 0;
607 fCumulativeMeasurementCount = 0;
608}
609
brianosman05de2162016-05-06 13:28:57 -0700610void Viewer::setupCurrentSlide(int previousSlide) {
liyuqiane5a6cd92016-05-27 08:52:52 -0700611 if (fCurrentSlide == previousSlide) {
612 return; // no change; do nothing
613 }
liyuqian6f163d22016-06-13 12:26:45 -0700614 // prepare dimensions for image slides
jvanverthc7027ab2016-06-16 09:52:35 -0700615 fSlides[fCurrentSlide]->load(SkIntToScalar(fWindow->width()), SkIntToScalar(fWindow->height()));
liyuqian6f163d22016-06-13 12:26:45 -0700616
liyuqiane46e4f02016-05-20 07:32:19 -0700617 fGesture.reset();
618 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700619
Brian Osman42bb6ac2017-06-05 08:46:04 -0400620 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
621 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
622 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
623
624 // Start with a matrix that scales the slide to the available screen space
625 if (fWindow->scaleContentToFit()) {
626 if (windowRect.width() > 0 && windowRect.height() > 0) {
627 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
liyuqiane46e4f02016-05-20 07:32:19 -0700628 }
629 }
630
Brian Osman42bb6ac2017-06-05 08:46:04 -0400631 // Prevent the user from dragging content so far outside the window they can't find it again
632 fGesture.setTransLimit(slideBounds, windowRect, fDefaultMatrix);
liyuqiane46e4f02016-05-20 07:32:19 -0700633
brianosman05de2162016-05-06 13:28:57 -0700634 this->updateTitle();
liyuqiane5a6cd92016-05-27 08:52:52 -0700635 this->updateUIState();
jvanverthc265a922016-04-08 12:51:45 -0700636 if (previousSlide >= 0) {
637 fSlides[previousSlide]->unload();
638 }
Jim Van Verth90dcce52017-11-03 13:36:07 -0400639
640 this->resetMeasurements();
641
jvanverthc265a922016-04-08 12:51:45 -0700642 fWindow->inval();
643}
644
645#define MAX_ZOOM_LEVEL 8
646#define MIN_ZOOM_LEVEL -8
647
jvanverth34524262016-05-04 13:49:13 -0700648void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700649 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400650 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
jvanverthc265a922016-04-08 12:51:45 -0700651}
652
liyuqiand3cdbca2016-05-17 12:44:20 -0700653SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700654 SkMatrix m;
jvanverthc265a922016-04-08 12:51:45 -0700655
Brian Osman42bb6ac2017-06-05 08:46:04 -0400656 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
657 : SK_Scalar1 + fZoomLevel;
658 m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700659 m.preConcat(fGesture.globalM());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400660 m.preConcat(fDefaultMatrix);
661 m.preScale(zoomScale, zoomScale);
jvanverthc265a922016-04-08 12:51:45 -0700662
liyuqiand3cdbca2016-05-17 12:44:20 -0700663 return m;
jvanverthc265a922016-04-08 12:51:45 -0700664}
665
Brian Osman621491e2017-02-28 15:45:01 -0500666void Viewer::setBackend(sk_app::Window::BackendType backendType) {
667 fBackendType = backendType;
668
669 fWindow->detach();
670
Brian Osman70d2f432017-11-08 09:54:10 -0500671#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400672 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
673 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500674 DisplayParams params = fWindow->getRequestedDisplayParams();
675 delete fWindow;
676 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500677
Brian Osman70d2f432017-11-08 09:54:10 -0500678 // re-register callbacks
679 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500680 fWindow->pushLayer(this);
Brian Osmand67e5182017-12-08 16:46:09 -0500681 fWindow->pushLayer(&fImGuiLayer);
682
Brian Osman70d2f432017-11-08 09:54:10 -0500683 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
684 // will still include our correct sample count. But the re-created fWindow will lose that
685 // information. On Windows, we need to re-create the window when changing sample count,
686 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
687 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
688 // as if we had never changed windows at all).
689 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500690#endif
691
Brian Osman70d2f432017-11-08 09:54:10 -0500692 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500693}
694
Brian Osman92004802017-03-06 11:47:26 -0500695void Viewer::setColorMode(ColorMode colorMode) {
696 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700697
Brian Osmanf750fbc2017-02-08 10:47:28 -0500698 // 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 -0400699 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700700 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500701 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400702 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
703 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500704 if (wasInLegacy != wantLegacy) {
705 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700706 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500707 }
708
709 this->updateTitle();
710 fWindow->inval();
711}
712
713void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500714 SkAutoCanvasRestore autorestore(canvas, false);
715
Brian Osmanf750fbc2017-02-08 10:47:28 -0500716 // By default, we render directly into the window's surface/canvas
717 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500718 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700719
Brian Osmane0d4fba2017-03-15 10:24:55 -0400720 // If we're in any of the color managed modes, construct the color space we're going to use
721 sk_sp<SkColorSpace> cs = nullptr;
722 if (ColorMode::kLegacy != fColorMode) {
723 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
724 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400725 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400726 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500727 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
728 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
729 } else {
730 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
731 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400732 }
733
Brian Osman3ac99cf2017-12-01 11:23:53 -0500734 if (fSaveToSKP) {
735 SkPictureRecorder recorder;
736 SkCanvas* recorderCanvas = recorder.beginRecording(
737 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
738 // In xform-canvas mode, record the transformed output
739 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
740 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
741 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
742 recorderCanvas = xformCanvas.get();
743 }
744 fSlides[fCurrentSlide]->draw(recorderCanvas);
745 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
746 SkFILEWStream stream("sample_app.skp");
747 picture->serialize(&stream);
748 fSaveToSKP = false;
749 }
750
Brian Osmane0d4fba2017-03-15 10:24:55 -0400751 // 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 -0500752 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
753 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500754 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -0500755 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
756 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -0500757 if (Window::kRaster_BackendType == fBackendType ||
758 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500759 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400760 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
761 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
762
Brian Osman92004802017-03-06 11:47:26 -0500763 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
764 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400765 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
766 // with a special canvas (below) that has the color space attached
767 sk_sp<SkColorSpace> offscreenColorSpace =
768 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500769 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400770 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500771 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
772 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -0500773 SkPixmap offscreenPixmap;
774 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
775 SkBitmap offscreenBitmap;
776 offscreenBitmap.installPixels(offscreenPixmap);
777 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
778 fThreadCnt, fExecutor.get()));
779 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
780 slideCanvas = threadedCanvas.get();
781 } else {
782 slideCanvas = offscreenSurface->getCanvas();
783 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500784 }
785
Brian Osmane0d4fba2017-03-15 10:24:55 -0400786 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
787 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
788 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
789 slideCanvas = xformCanvas.get();
790 }
791
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500792 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500793 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500794 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500795 // Time the painting logic of the slide
796 double startTime = SkTime::GetMSecs();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500797 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osman1df161a2017-02-09 12:10:20 -0500798 fPaintTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500799 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500800
801 // Force a flush so we can time that, too
802 startTime = SkTime::GetMSecs();
803 slideCanvas->flush();
804 fFlushTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500805
806 // If we rendered offscreen, snap an image and push the results to the window's canvas
807 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500808 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500809
810 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400811 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500812 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400813 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500814 SkPaint paint;
815 paint.setBlendMode(SkBlendMode::kSrc);
816 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -0700817 }
liyuqian6f163d22016-06-13 12:26:45 -0700818}
819
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700820void Viewer::onBackendCreated() {
821 this->updateTitle();
822 this->updateUIState();
823 this->setupCurrentSlide(-1);
Jim Van Verth90dcce52017-11-03 13:36:07 -0400824 this->resetMeasurements();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700825 fWindow->show();
826 fWindow->inval();
827}
Jim Van Verth6f449692017-02-14 15:16:46 -0500828
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700829void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -0400830 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -0700831
Brian Osman1df161a2017-02-09 12:10:20 -0500832 // Advance our timing bookkeeping
Jim Van Verth90dcce52017-11-03 13:36:07 -0400833 fCumulativeMeasurementTime += fAnimateTimes[fCurrentMeasurement] +
834 fPaintTimes[fCurrentMeasurement] +
835 fFlushTimes[fCurrentMeasurement];
836 fCumulativeMeasurementCount++;
Brian Osman1df161a2017-02-09 12:10:20 -0500837 fCurrentMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1);
838 SkASSERT(fCurrentMeasurement < kMeasurementCount);
839
840 // Draw any overlays or UI that we don't want timed
jvanverthc265a922016-04-08 12:51:45 -0700841 if (fDisplayStats) {
842 drawStats(canvas);
843 }
brianosman622c8d52016-05-10 06:50:49 -0700844 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -0700845
Brian Osmand67e5182017-12-08 16:46:09 -0500846 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -0500847
Brian Osman1df161a2017-02-09 12:10:20 -0500848 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -0400849 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700850}
851
jvanverth814e38d2016-06-06 08:48:47 -0700852bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400853 if (GestureDevice::kMouse == fGestureDevice) {
854 return false;
855 }
liyuqiand3cdbca2016-05-17 12:44:20 -0700856 void* castedOwner = reinterpret_cast<void*>(owner);
857 switch (state) {
858 case Window::kUp_InputState: {
859 fGesture.touchEnd(castedOwner);
860 break;
861 }
862 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400863 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700864 break;
865 }
866 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400867 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700868 break;
869 }
870 }
Brian Osmanb53f48c2017-06-07 10:00:30 -0400871 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -0700872 fWindow->inval();
873 return true;
874}
875
Brian Osman80fc07e2017-12-08 16:45:43 -0500876bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -0500877 if (!fSlides[fCurrentSlide]->onMouse(x, y, state, modifiers)) {
878 if (GestureDevice::kTouch == fGestureDevice) {
879 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -0500880 }
Brian Osmand67e5182017-12-08 16:46:09 -0500881 switch (state) {
882 case Window::kUp_InputState: {
883 fGesture.touchEnd(nullptr);
884 break;
885 }
886 case Window::kDown_InputState: {
887 fGesture.touchBegin(nullptr, x, y);
888 break;
889 }
890 case Window::kMove_InputState: {
891 fGesture.touchMoved(nullptr, x, y);
892 break;
893 }
894 }
895 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
Brian Osman80fc07e2017-12-08 16:45:43 -0500896 }
Brian Osmand67e5182017-12-08 16:46:09 -0500897 fWindow->inval();
Jim Van Verthe7705782017-05-04 14:00:59 -0400898 return true;
899}
900
jvanverth34524262016-05-04 13:49:13 -0700901void Viewer::drawStats(SkCanvas* canvas) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700902 static const float kPixelPerMS = 2.0f;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500903 static const int kDisplayWidth = 192;
904 static const int kGraphHeight = 100;
905 static const int kTextHeight = 60;
906 static const int kDisplayHeight = kGraphHeight + kTextHeight;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700907 static const int kDisplayPadding = 10;
908 static const int kGraphPadding = 3;
909 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
910
Mike Reed3661bc92017-02-22 13:21:42 -0500911 SkISize canvasSize = canvas->getBaseLayerSize();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700912 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
913 SkIntToScalar(kDisplayPadding),
914 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
915 SkPaint paint;
916 canvas->save();
917
jvanverth3d6ed3a2016-04-07 11:09:51 -0700918 paint.setColor(SK_ColorBLACK);
919 canvas->drawRect(rect, paint);
920 // draw the 16ms line
921 paint.setColor(SK_ColorLTGRAY);
922 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
923 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
924 paint.setColor(SK_ColorRED);
925 paint.setStyle(SkPaint::kStroke_Style);
926 canvas->drawRect(rect, paint);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500927 paint.setStyle(SkPaint::kFill_Style);
jvanverth3d6ed3a2016-04-07 11:09:51 -0700928
929 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500930 const int xStep = 3;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700931 int i = fCurrentMeasurement;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500932 double ms = 0;
933 double animateMS = 0;
934 double paintMS = 0;
935 double flushMS = 0;
936 int count = 0;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700937 do {
Brian Osman1df161a2017-02-09 12:10:20 -0500938 // Round to nearest values
939 int animateHeight = (int)(fAnimateTimes[i] * kPixelPerMS + 0.5);
940 int paintHeight = (int)(fPaintTimes[i] * kPixelPerMS + 0.5);
941 int flushHeight = (int)(fFlushTimes[i] * kPixelPerMS + 0.5);
942 int startY = SkScalarTruncToInt(rect.fBottom);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500943 int endY = SkTMax(startY - flushHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -0500944 paint.setColor(SK_ColorRED);
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 - paintHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -0500949 paint.setColor(SK_ColorGREEN);
950 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
951 SkIntToScalar(x), SkIntToScalar(endY), paint);
952 startY = endY;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500953 endY = SkTMax(startY - animateHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -0500954 paint.setColor(SK_ColorMAGENTA);
jvanverth3d6ed3a2016-04-07 11:09:51 -0700955 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
956 SkIntToScalar(x), SkIntToScalar(endY), paint);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500957
958 double inc = fAnimateTimes[i] + fPaintTimes[i] + fFlushTimes[i];
959 if (inc > 0) {
960 ms += inc;
961 animateMS += fAnimateTimes[i];
962 paintMS += fPaintTimes[i];
963 flushMS += fFlushTimes[i];
964 ++count;
965 }
966
jvanverth3d6ed3a2016-04-07 11:09:51 -0700967 i++;
968 i &= (kMeasurementCount - 1); // fast mod
969 x += xStep;
970 } while (i != fCurrentMeasurement);
jvanverth9f372462016-04-06 06:08:59 -0700971
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500972 paint.setTextSize(16);
973 SkString mainString;
974 mainString.appendf("%4.3f ms -> %4.3f ms", ms / SkTMax(1, count),
975 fCumulativeMeasurementTime / SkTMax(1, fCumulativeMeasurementCount));
976 paint.setColor(SK_ColorWHITE);
977 canvas->drawString(mainString.c_str(), rect.fLeft+3, rect.fTop + 14, paint);
978
979 SkString animateString;
980 animateString.appendf("Animate: %4.3f ms", animateMS / SkTMax(1, count));
981 paint.setColor(0xffff66ff); // pure magenta is hard to read
982 canvas->drawString(animateString.c_str(), rect.fLeft+3, rect.fTop + 28, paint);
983
984 SkString paintString;
985 paintString.appendf("Paint: %4.3f ms", paintMS / SkTMax(1, count));
986 paint.setColor(SK_ColorGREEN);
987 canvas->drawString(paintString.c_str(), rect.fLeft+3, rect.fTop + 42, paint);
988
989 SkString flushString;
990 flushString.appendf("Flush: %4.3f ms", flushMS / SkTMax(1, count));
991 paint.setColor(0xffff6666); // pure red is hard to read
992 canvas->drawString(flushString.c_str(), rect.fLeft+3, rect.fTop + 56, paint);
993
jvanverth9f372462016-04-06 06:08:59 -0700994 canvas->restore();
995}
996
Brian Osmana109e392017-02-24 09:49:14 -0500997static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
998 const ImVec2& pos, const ImVec2& size) {
999 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1000 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1001
1002 // Invisible 10x10 button
1003 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1004 ImGui::InvisibleButton(label, ImVec2(10, 10));
1005
1006 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1007 ImGuiIO& io = ImGui::GetIO();
1008 // Normalized mouse position, relative to our gamut box
1009 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1010 // Clamp to edge of box, convert back to primary scale
1011 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1012 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1013 }
1014
1015 if (ImGui::IsItemHovered()) {
1016 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1017 }
1018
1019 // Return screen coordinates for the caller. We could just return center here, but we'd have
1020 // one frame of lag during drag.
1021 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1022}
1023
1024static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1025 ImDrawList* drawList = ImGui::GetWindowDrawList();
1026
1027 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1028 // width, and scale the height to maintain aspect ratio.
1029 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1030 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1031 ImVec2 pos = ImGui::GetCursorScreenPos();
1032
1033 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1034 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1035 // Magic numbers are pixel locations of the origin and upper-right corner.
1036 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1037 ImVec2(242, 61), ImVec2(1897, 1922));
1038 ImVec2 endPos = ImGui::GetCursorPos();
1039
1040 // Primary markers
1041 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1042 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1043 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1044 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1045
1046 // Gamut triangle
1047 drawList->AddCircle(r, 5.0f, 0xFF000040);
1048 drawList->AddCircle(g, 5.0f, 0xFF004000);
1049 drawList->AddCircle(b, 5.0f, 0xFF400000);
1050 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1051 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1052
1053 // Re-position cursor immediate after the diagram for subsequent controls
1054 ImGui::SetCursorPos(endPos);
1055}
1056
Brian Osmand67e5182017-12-08 16:46:09 -05001057void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001058 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1059 if (fShowImGuiTestWindow) {
1060 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1061 }
1062
1063 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001064 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1065 // always visible, we can end up in a layout feedback loop.
1066 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001067 DisplayParams params = fWindow->getRequestedDisplayParams();
1068 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001069 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1070 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001071 if (ImGui::CollapsingHeader("Backend")) {
1072 int newBackend = static_cast<int>(fBackendType);
1073 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1074 ImGui::SameLine();
1075 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001076#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1077 ImGui::SameLine();
1078 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1079#endif
Brian Osman621491e2017-02-28 15:45:01 -05001080#if defined(SK_VULKAN)
1081 ImGui::SameLine();
1082 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1083#endif
1084 if (newBackend != fBackendType) {
1085 fDeferredActions.push_back([=]() {
1086 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1087 });
1088 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001089
Brian Salomon99a33902017-03-07 15:16:34 -05001090 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001091 bool* wire = &params.fGrContextOptions.fWireframeMode;
1092 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1093 paramsChanged = true;
1094 }
Brian Salomon99a33902017-03-07 15:16:34 -05001095
Brian Osman28b12522017-03-08 17:10:24 -05001096 if (ctx) {
1097 int sampleCount = fWindow->sampleCount();
1098 ImGui::Text("MSAA: "); ImGui::SameLine();
1099 ImGui::RadioButton("0", &sampleCount, 0); ImGui::SameLine();
1100 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1101 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1102 ImGui::RadioButton("16", &sampleCount, 16);
1103
1104 if (sampleCount != params.fMSAASampleCount) {
1105 params.fMSAASampleCount = sampleCount;
1106 paramsChanged = true;
1107 }
1108 }
1109
Brian Osman8a9de3d2017-03-01 14:59:05 -05001110 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001111 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001112 auto prButton = [&](GpuPathRenderers x) {
1113 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001114 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1115 params.fGrContextOptions.fGpuPathRenderers = x;
1116 paramsChanged = true;
1117 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001118 }
1119 };
1120
1121 if (!ctx) {
1122 ImGui::RadioButton("Software", true);
1123 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001124 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001125 prButton(GpuPathRenderers::kAll);
1126 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1127 prButton(GpuPathRenderers::kStencilAndCover);
1128 }
1129 if (ctx->caps()->sampleShadingSupport()) {
1130 prButton(GpuPathRenderers::kMSAA);
1131 }
1132 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001133 prButton(GpuPathRenderers::kNone);
1134 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001135 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001136 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001137 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1138 prButton(GpuPathRenderers::kCoverageCounting);
1139 }
Jim Van Verth83010462017-03-16 08:45:39 -04001140 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001141 prButton(GpuPathRenderers::kTessellating);
1142 prButton(GpuPathRenderers::kNone);
1143 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001144 ImGui::TreePop();
1145 }
Brian Osman621491e2017-02-28 15:45:01 -05001146 }
1147
Brian Osmanfce09c52017-11-14 15:32:20 -05001148 if (fShowSlidePicker) {
1149 ImGui::SetNextTreeNodeOpen(true);
1150 }
1151
Brian Osman79086b92017-02-10 13:36:16 -05001152 if (ImGui::CollapsingHeader("Slide")) {
1153 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001154 static ImVector<const char*> filteredSlideNames;
1155 static ImVector<int> filteredSlideIndices;
1156
Brian Osmanfce09c52017-11-14 15:32:20 -05001157 if (fShowSlidePicker) {
1158 ImGui::SetKeyboardFocusHere();
1159 fShowSlidePicker = false;
1160 }
1161
Brian Osman79086b92017-02-10 13:36:16 -05001162 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001163 filteredSlideNames.clear();
1164 filteredSlideIndices.clear();
1165 int filteredIndex = 0;
1166 for (int i = 0; i < fSlides.count(); ++i) {
1167 const char* slideName = fSlides[i]->getName().c_str();
1168 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1169 if (i == fCurrentSlide) {
1170 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001171 }
Brian Osmanf479e422017-11-08 13:11:36 -05001172 filteredSlideNames.push_back(slideName);
1173 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001174 }
Brian Osman79086b92017-02-10 13:36:16 -05001175 }
Brian Osmanf479e422017-11-08 13:11:36 -05001176
1177 int previousSlide = fCurrentSlide;
1178 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1179 filteredSlideNames.size(), 20)) {
1180 fCurrentSlide = filteredSlideIndices[filteredIndex];
1181 setupCurrentSlide(previousSlide);
Brian Osman79086b92017-02-10 13:36:16 -05001182 }
1183 }
Brian Osmana109e392017-02-24 09:49:14 -05001184
1185 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001186 ColorMode newMode = fColorMode;
1187 auto cmButton = [&](ColorMode mode, const char* label) {
1188 if (ImGui::RadioButton(label, mode == fColorMode)) {
1189 newMode = mode;
1190 }
1191 };
1192
1193 cmButton(ColorMode::kLegacy, "Legacy 8888");
1194 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1195 "Color Managed 8888 (Nonlinear blending)");
1196 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1197 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1198
1199 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001200 // It isn't safe to switch color mode now (in the middle of painting). We might
1201 // tear down the back-end, etc... Defer this change until the next onIdle.
1202 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001203 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001204 });
Brian Osmana109e392017-02-24 09:49:14 -05001205 }
1206
1207 // Pick from common gamuts:
1208 int primariesIdx = 4; // Default: Custom
1209 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1210 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1211 primariesIdx = i;
1212 break;
1213 }
1214 }
1215
Brian Osmanfdab5762017-11-09 10:27:55 -05001216 // When we're in xform canvas mode, we can alter the transfer function, too
1217 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1218 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1219 }
1220
Brian Osmana109e392017-02-24 09:49:14 -05001221 if (ImGui::Combo("Primaries", &primariesIdx,
1222 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1223 if (primariesIdx >= 0 && primariesIdx <= 3) {
1224 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1225 }
1226 }
1227
1228 // Allow direct editing of gamut
1229 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1230 }
Brian Osman79086b92017-02-10 13:36:16 -05001231 }
Brian Salomon99a33902017-03-07 15:16:34 -05001232 if (paramsChanged) {
1233 fDeferredActions.push_back([=]() {
1234 fWindow->setRequestedDisplayParams(params);
1235 fWindow->inval();
1236 this->updateTitle();
1237 });
1238 }
Brian Osman79086b92017-02-10 13:36:16 -05001239 ImGui::End();
1240 }
1241
Brian Osmanf6877092017-02-13 09:39:57 -05001242 if (fShowZoomWindow && fLastImage) {
1243 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001244 static int zoomFactor = 8;
1245 if (ImGui::Button("<<")) {
1246 zoomFactor = SkTMax(zoomFactor / 2, 4);
1247 }
1248 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1249 if (ImGui::Button(">>")) {
1250 zoomFactor = SkTMin(zoomFactor * 2, 32);
1251 }
Brian Osmanf6877092017-02-13 09:39:57 -05001252
Brian Osmanf6877092017-02-13 09:39:57 -05001253 ImVec2 mousePos = ImGui::GetMousePos();
1254 ImVec2 avail = ImGui::GetContentRegionAvail();
1255
Brian Osmanead517d2017-11-13 15:36:36 -05001256 uint32_t pixel = 0;
1257 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1258 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1259 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001260 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1261 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1262 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001263 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1264 }
1265
Brian Osmand67e5182017-12-08 16:46:09 -05001266 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001267 // Translate so the region of the image that's under the mouse cursor is centered
1268 // in the zoom canvas:
1269 c->scale(zoomFactor, zoomFactor);
1270 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1271 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1272 c->drawImage(this->fLastImage, 0, 0);
1273
1274 SkPaint outline;
1275 outline.setStyle(SkPaint::kStroke_Style);
1276 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1277 });
Brian Osmanf6877092017-02-13 09:39:57 -05001278 }
1279
1280 ImGui::End();
1281 }
Brian Osman79086b92017-02-10 13:36:16 -05001282}
1283
liyuqian2edb0f42016-07-06 14:11:32 -07001284void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001285 for (int i = 0; i < fDeferredActions.count(); ++i) {
1286 fDeferredActions[i]();
1287 }
1288 fDeferredActions.reset();
1289
Brian Osman1df161a2017-02-09 12:10:20 -05001290 double startTime = SkTime::GetMSecs();
jvanverthc265a922016-04-08 12:51:45 -07001291 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001292 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
1293 fAnimateTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
1294
Brian Osman79086b92017-02-10 13:36:16 -05001295 ImGuiIO& io = ImGui::GetIO();
1296 if (animateWantsInval || fDisplayStats || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001297 fWindow->inval();
1298 }
jvanverth9f372462016-04-06 06:08:59 -07001299}
liyuqiane5a6cd92016-05-27 08:52:52 -07001300
1301void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001302 if (!fWindow) {
1303 return;
1304 }
1305 if (fWindow->sampleCount() < 0) {
1306 return; // Surface hasn't been created yet.
1307 }
1308
liyuqianb73c24b2016-06-03 08:47:23 -07001309 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001310 Json::Value slideState(Json::objectValue);
1311 slideState[kName] = kSlideStateName;
1312 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001313 if (fAllSlideNames.size() == 0) {
1314 for(auto slide : fSlides) {
1315 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1316 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001317 }
liyuqian1f508fd2016-06-07 06:57:40 -07001318 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001319
liyuqianb73c24b2016-06-03 08:47:23 -07001320 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001321 Json::Value backendState(Json::objectValue);
1322 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001323 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001324 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001325 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001326 backendState[kOptions].append(Json::Value(str));
1327 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001328
csmartdalton578f0642017-02-24 16:04:47 -07001329 // MSAA state
1330 Json::Value msaaState(Json::objectValue);
1331 msaaState[kName] = kMSAAStateName;
1332 msaaState[kValue] = fWindow->sampleCount();
1333 msaaState[kOptions] = Json::Value(Json::arrayValue);
1334 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1335 msaaState[kOptions].append(Json::Value(0));
1336 } else {
1337 for (int msaa : {0, 4, 8, 16}) {
1338 msaaState[kOptions].append(Json::Value(msaa));
1339 }
1340 }
1341
csmartdalton61cd31a2017-02-27 17:00:53 -07001342 // Path renderer state
1343 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1344 Json::Value prState(Json::objectValue);
1345 prState[kName] = kPathRendererStateName;
1346 prState[kValue] = gPathRendererNames[pr];
1347 prState[kOptions] = Json::Value(Json::arrayValue);
1348 const GrContext* ctx = fWindow->getGrContext();
1349 if (!ctx) {
1350 prState[kOptions].append("Software");
1351 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001352 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001353 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1354 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1355 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1356 }
1357 if (ctx->caps()->sampleShadingSupport()) {
1358 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1359 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001360 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001361 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1362 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001363 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001364 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001365 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1366 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1367 }
Jim Van Verth83010462017-03-16 08:45:39 -04001368 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001369 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001370 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1371 }
1372
liyuqianb73c24b2016-06-03 08:47:23 -07001373 // Softkey state
1374 Json::Value softkeyState(Json::objectValue);
1375 softkeyState[kName] = kSoftkeyStateName;
1376 softkeyState[kValue] = kSoftkeyHint;
1377 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1378 softkeyState[kOptions].append(kSoftkeyHint);
1379 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1380 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1381 }
1382
liyuqian1f508fd2016-06-07 06:57:40 -07001383 // FPS state
1384 Json::Value fpsState(Json::objectValue);
1385 fpsState[kName] = kFpsStateName;
Brian Osman1df161a2017-02-09 12:10:20 -05001386 int idx = (fCurrentMeasurement + (kMeasurementCount - 1)) & (kMeasurementCount - 1);
1387 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
1388 fAnimateTimes[idx] + fPaintTimes[idx] + fFlushTimes[idx],
1389 fAnimateTimes[idx],
1390 fPaintTimes[idx],
1391 fFlushTimes[idx]).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001392 fpsState[kOptions] = Json::Value(Json::arrayValue);
1393
liyuqiane5a6cd92016-05-27 08:52:52 -07001394 Json::Value state(Json::arrayValue);
1395 state.append(slideState);
1396 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001397 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001398 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001399 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001400 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001401
Brian Osmaneff04b52017-11-21 13:18:02 -05001402 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001403}
1404
1405void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001406 // For those who will add more features to handle the state change in this function:
1407 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1408 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1409 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001410 if (stateName.equals(kSlideStateName)) {
1411 int previousSlide = fCurrentSlide;
1412 fCurrentSlide = 0;
1413 for(auto slide : fSlides) {
1414 if (slide->getName().equals(stateValue)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001415 this->setupCurrentSlide(previousSlide);
liyuqiane5a6cd92016-05-27 08:52:52 -07001416 break;
1417 }
1418 fCurrentSlide++;
1419 }
1420 if (fCurrentSlide >= fSlides.count()) {
1421 fCurrentSlide = previousSlide;
1422 SkDebugf("Slide not found: %s", stateValue.c_str());
1423 }
liyuqian6cb70252016-06-02 12:16:25 -07001424 } else if (stateName.equals(kBackendStateName)) {
1425 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1426 if (stateValue.equals(kBackendTypeStrings[i])) {
1427 if (fBackendType != i) {
1428 fBackendType = (sk_app::Window::BackendType)i;
1429 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001430 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001431 }
1432 break;
1433 }
1434 }
csmartdalton578f0642017-02-24 16:04:47 -07001435 } else if (stateName.equals(kMSAAStateName)) {
1436 DisplayParams params = fWindow->getRequestedDisplayParams();
1437 int sampleCount = atoi(stateValue.c_str());
1438 if (sampleCount != params.fMSAASampleCount) {
1439 params.fMSAASampleCount = sampleCount;
1440 fWindow->setRequestedDisplayParams(params);
1441 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001442 this->updateTitle();
1443 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001444 }
1445 } else if (stateName.equals(kPathRendererStateName)) {
1446 DisplayParams params = fWindow->getRequestedDisplayParams();
1447 for (const auto& pair : gPathRendererNames) {
1448 if (pair.second == stateValue.c_str()) {
1449 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1450 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1451 fWindow->setRequestedDisplayParams(params);
1452 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001453 this->updateTitle();
1454 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001455 }
1456 break;
1457 }
csmartdalton578f0642017-02-24 16:04:47 -07001458 }
liyuqianb73c24b2016-06-03 08:47:23 -07001459 } else if (stateName.equals(kSoftkeyStateName)) {
1460 if (!stateValue.equals(kSoftkeyHint)) {
1461 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001462 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001463 }
liyuqian2edb0f42016-07-06 14:11:32 -07001464 } else if (stateName.equals(kRefreshStateName)) {
1465 // This state is actually NOT in the UI state.
1466 // We use this to allow Android to quickly set bool fRefresh.
1467 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001468 } else {
1469 SkDebugf("Unknown stateName: %s", stateName.c_str());
1470 }
1471}
Brian Osman79086b92017-02-10 13:36:16 -05001472
1473bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001474 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001475}
1476
1477bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001478 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001479 fWindow->inval();
1480 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001481 } else {
1482 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001483 }
Brian Osman79086b92017-02-10 13:36:16 -05001484}