blob: 55a025c1ed9ed1cd0a4faa6bb3ee0aff5c954b1e [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"
Florin Malita094ccde2017-12-30 12:27:00 -050014#include "SkottySlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070015#include "SKPSlide.h"
jvanverth9f372462016-04-06 06:08:59 -070016
csmartdalton61cd31a2017-02-27 17:00:53 -070017#include "GrContext.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070018#include "SkCanvas.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050019#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040020#include "SkColorSpaceXformCanvas.h"
Brian Osman2dd96932016-10-18 15:33:53 -040021#include "SkCommandLineFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070022#include "SkCommonFlagsGpu.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"
jvanverth9f372462016-04-06 06:08:59 -070034
Brian Osman79086b92017-02-10 13:36:16 -050035#include "imgui.h"
36
Chris Dalton1a325d22017-07-14 15:17:41 -060037#include "ccpr/GrCoverageCountingPathRenderer.h"
38
csmartdalton578f0642017-02-24 16:04:47 -070039#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070040#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070041
jvanverth34524262016-05-04 13:49:13 -070042using namespace sk_app;
43
csmartdalton61cd31a2017-02-27 17:00:53 -070044static std::map<GpuPathRenderers, std::string> gPathRendererNames;
45
jvanverth9f372462016-04-06 06:08:59 -070046Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070047 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070048}
49
Brian Osman2dd96932016-10-18 15:33:53 -040050static DEFINE_string2(match, m, nullptr,
jvanverth2bb3b6d2016-04-08 07:24:09 -070051 "[~][^]substring[$] [...] of bench name to run.\n"
52 "Multiple matches may be separated by spaces.\n"
53 "~ causes a matching bench to always be skipped\n"
54 "^ requires the start of the bench to match\n"
55 "$ requires the end of the bench to match\n"
56 "^ and $ requires an exact match\n"
57 "If a bench does not match any list entry,\n"
58 "it is skipped unless some list entry starts with ~");
bsalomon6c471f72016-07-26 12:56:32 -070059
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060060static DEFINE_string(slide, "", "Start on this sample.");
61static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050062
bsalomon6c471f72016-07-26 12:56:32 -070063#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070064# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070065#else
66# define BACKENDS_STR "\"sw\" and \"gl\""
67#endif
68
liyuqian71491dc2016-06-09 12:02:34 -070069#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -040070static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
71static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
Florin Malita094ccde2017-12-30 12:27:00 -050072static DEFINE_string(jsons, "/data/local/tmp/jsons", "Directory to read (Bodymovin) jsons 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.");
Florin Malita094ccde2017-12-30 12:27:00 -050076static DEFINE_string(jsons, "jsons", "Directory to read (Bodymovin) jsons from.");
liyuqian71491dc2016-06-09 12:02:34 -070077#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -070078
Brian Osman2dd96932016-10-18 15:33:53 -040079static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070080
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060081static DEFINE_int32(msaa, 0, "Number of subpixel samples. 0 for no HW antialiasing.");
csmartdalton008b9d82017-02-22 12:00:42 -070082
Brian Osman53136aa2017-07-20 15:43:35 -040083DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050084
Brian Salomon194db172017-08-17 14:37:06 -040085const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070086 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040087#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
88 "ANGLE",
89#endif
jvanverth063ece72016-06-17 09:29:14 -070090#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -070091 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -070092#endif
csmartdalton578f0642017-02-24 16:04:47 -070093 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -070094};
95
bsalomon6c471f72016-07-26 12:56:32 -070096static sk_app::Window::BackendType get_backend_type(const char* str) {
97#ifdef SK_VULKAN
98 if (0 == strcmp(str, "vk")) {
99 return sk_app::Window::kVulkan_BackendType;
100 } else
101#endif
Brian Salomon194db172017-08-17 14:37:06 -0400102#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
103 if (0 == strcmp(str, "angle")) {
104 return sk_app::Window::kANGLE_BackendType;
105 } else
106#endif
bsalomon6c471f72016-07-26 12:56:32 -0700107 if (0 == strcmp(str, "gl")) {
108 return sk_app::Window::kNativeGL_BackendType;
109 } else if (0 == strcmp(str, "sw")) {
110 return sk_app::Window::kRaster_BackendType;
111 } else {
112 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
113 return sk_app::Window::kRaster_BackendType;
114 }
115}
116
Brian Osmana109e392017-02-24 09:49:14 -0500117static SkColorSpacePrimaries gSrgbPrimaries = {
118 0.64f, 0.33f,
119 0.30f, 0.60f,
120 0.15f, 0.06f,
121 0.3127f, 0.3290f };
122
123static SkColorSpacePrimaries gAdobePrimaries = {
124 0.64f, 0.33f,
125 0.21f, 0.71f,
126 0.15f, 0.06f,
127 0.3127f, 0.3290f };
128
129static SkColorSpacePrimaries gP3Primaries = {
130 0.680f, 0.320f,
131 0.265f, 0.690f,
132 0.150f, 0.060f,
133 0.3127f, 0.3290f };
134
135static SkColorSpacePrimaries gRec2020Primaries = {
136 0.708f, 0.292f,
137 0.170f, 0.797f,
138 0.131f, 0.046f,
139 0.3127f, 0.3290f };
140
141struct NamedPrimaries {
142 const char* fName;
143 SkColorSpacePrimaries* fPrimaries;
144} gNamedPrimaries[] = {
145 { "sRGB", &gSrgbPrimaries },
146 { "AdobeRGB", &gAdobePrimaries },
147 { "P3", &gP3Primaries },
148 { "Rec. 2020", &gRec2020Primaries },
149};
150
151static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
152 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
153}
154
Brian Osman70d2f432017-11-08 09:54:10 -0500155static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
156 // In raster mode, we still use GL for the window.
157 // This lets us render the GUI faster (and correct).
158 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
159}
160
liyuqiane5a6cd92016-05-27 08:52:52 -0700161const char* kName = "name";
162const char* kValue = "value";
163const char* kOptions = "options";
164const char* kSlideStateName = "Slide";
165const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700166const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700167const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700168const char* kSoftkeyStateName = "Softkey";
169const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700170const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700171const char* kON = "ON";
172const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700173const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700174
jvanverth34524262016-05-04 13:49:13 -0700175Viewer::Viewer(int argc, char** argv, void* platformData)
Brian Osman56a24812017-12-19 11:15:16 -0500176 : fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500177 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500178 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500179 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500180 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500181 , fShowZoomWindow(false)
182 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700183 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500184 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500185 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500186 // Our UI can only tweak gamma (currently), so start out gamma-only
187 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700188 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400189 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500190 , fTileCnt(0)
191 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700192{
Greg Daniel285db442016-10-14 09:12:53 -0400193 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700194
Brian Osmanf09e35e2017-12-15 14:48:09 -0500195 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
196 gPathRendererNames[GpuPathRenderers::kDefault] =
197 "Default Ganesh Behavior (best path renderer, not including CCPR)";
198 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
199 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
200 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
201 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
202 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
203 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700204
jvanverth2bb3b6d2016-04-08 07:24:09 -0700205 SkDebugf("Command line arguments: ");
206 for (int i = 1; i < argc; ++i) {
207 SkDebugf("%s ", argv[i]);
208 }
209 SkDebugf("\n");
210
211 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500212#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400213 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500214#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700215
Brian Osmanbc8150f2017-07-24 11:38:01 -0400216 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400217 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400218
bsalomon6c471f72016-07-26 12:56:32 -0700219 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700220 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700221
csmartdalton578f0642017-02-24 16:04:47 -0700222 DisplayParams displayParams;
223 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700224 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700225 fWindow->setRequestedDisplayParams(displayParams);
226
Brian Osman56a24812017-12-19 11:15:16 -0500227 // Configure timers
228 fStatsLayer.setActive(false);
229 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
230 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
231 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
232
jvanverth9f372462016-04-06 06:08:59 -0700233 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700234 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500235 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500236 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500237 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700238
brianosman622c8d52016-05-10 06:50:49 -0700239 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500240 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
241 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
242 fWindow->inval();
243 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500244 // Command to jump directly to the slide picker and give it focus
245 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
246 this->fShowImGuiDebugWindow = true;
247 this->fShowSlidePicker = true;
248 fWindow->inval();
249 });
250 // Alias that to Backspace, to match SampleApp
251 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
252 this->fShowImGuiDebugWindow = true;
253 this->fShowSlidePicker = true;
254 fWindow->inval();
255 });
Brian Osman79086b92017-02-10 13:36:16 -0500256 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
257 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
258 fWindow->inval();
259 });
Brian Osmanf6877092017-02-13 09:39:57 -0500260 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
261 this->fShowZoomWindow = !this->fShowZoomWindow;
262 fWindow->inval();
263 });
brianosman622c8d52016-05-10 06:50:49 -0700264 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500265 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700266 fWindow->inval();
267 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400268 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500269 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400270 this->updateTitle();
271 fWindow->inval();
272 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500273 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500274 switch (fColorMode) {
275 case ColorMode::kLegacy:
276 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
277 break;
278 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
279 this->setColorMode(ColorMode::kColorManagedSRGB8888);
280 break;
281 case ColorMode::kColorManagedSRGB8888:
282 this->setColorMode(ColorMode::kColorManagedLinearF16);
283 break;
284 case ColorMode::kColorManagedLinearF16:
285 this->setColorMode(ColorMode::kLegacy);
286 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500287 }
brianosman622c8d52016-05-10 06:50:49 -0700288 });
289 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
290 int previousSlide = fCurrentSlide;
291 fCurrentSlide++;
292 if (fCurrentSlide >= fSlides.count()) {
293 fCurrentSlide = 0;
294 }
295 this->setupCurrentSlide(previousSlide);
296 });
297 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
298 int previousSlide = fCurrentSlide;
299 fCurrentSlide--;
300 if (fCurrentSlide < 0) {
301 fCurrentSlide = fSlides.count() - 1;
302 }
303 this->setupCurrentSlide(previousSlide);
304 });
305 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
306 this->changeZoomLevel(1.f / 32.f);
307 fWindow->inval();
308 });
309 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
310 this->changeZoomLevel(-1.f / 32.f);
311 fWindow->inval();
312 });
jvanverthaf236b52016-05-20 06:01:06 -0700313 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400314 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
315 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500316 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400317#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
318 if (newBackend == sk_app::Window::kVulkan_BackendType) {
319 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
320 sk_app::Window::kBackendTypeCount);
321 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
322 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500323 }
324#endif
Brian Osman621491e2017-02-28 15:45:01 -0500325 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700326 });
brianosman622c8d52016-05-10 06:50:49 -0700327
Yuqian Lib2ba6642017-11-22 12:07:41 -0500328 fCommands.addCommand('A', "AA", "Toggle analytic AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400329 if (!gSkUseAnalyticAA) {
330 gSkUseAnalyticAA = true;
331 } else if (!gSkForceAnalyticAA) {
332 gSkForceAnalyticAA = true;
333 } else {
334 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
335 }
336 this->updateTitle();
337 fWindow->inval();
338 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500339 fCommands.addCommand('D', "AA", "Toggle delta AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400340 if (!gSkUseDeltaAA) {
341 gSkUseDeltaAA = true;
342 } else if (!gSkForceDeltaAA) {
343 gSkForceDeltaAA = true;
344 } else {
345 gSkUseDeltaAA = gSkForceDeltaAA = false;
346 }
347 this->updateTitle();
348 fWindow->inval();
349 });
350
Yuqian Lib2ba6642017-11-22 12:07:41 -0500351 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
352 fTileCnt++;
353 if (fThreadCnt == 0) {
354 this->resetExecutor();
355 }
356 this->updateTitle();
357 fWindow->inval();
358 });
359 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
360 fTileCnt = SkTMax(0, fTileCnt - 1);
361 if (fThreadCnt == 0) {
362 this->resetExecutor();
363 }
364 this->updateTitle();
365 fWindow->inval();
366 });
367 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
368 if (fTileCnt == 0) {
369 return;
370 }
371 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
372 this->resetExecutor();
373 this->updateTitle();
374 fWindow->inval();
375 });
376 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
377 if (fTileCnt == 0) {
378 return;
379 }
380 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
381 this->resetExecutor();
382 this->updateTitle();
383 fWindow->inval();
384 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500385 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
386 fSaveToSKP = true;
387 fWindow->inval();
388 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500389
jvanverth2bb3b6d2016-04-08 07:24:09 -0700390 // set up slides
391 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500392 this->setStartupSlide();
393 if (FLAGS_list) {
394 this->listNames();
395 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700396
djsollen12d62a72016-04-21 07:59:44 -0700397 fAnimTimer.run();
398
Hal Canaryc465d132017-12-08 10:21:31 -0500399 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500400 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400401 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500402 }
403 fImGuiGamutPaint.setColor(SK_ColorWHITE);
404 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
405
Brian Osman70d2f432017-11-08 09:54:10 -0500406 fWindow->attach(backend_type_for_window(fBackendType));
jvanverth9f372462016-04-06 06:08:59 -0700407}
408
jvanverth34524262016-05-04 13:49:13 -0700409void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700410 fAllSlideNames = Json::Value(Json::arrayValue);
411
jvanverth2bb3b6d2016-04-08 07:24:09 -0700412 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
413 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400414 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700415
416 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
417 sk_sp<Slide> slide(new GMSlide(gm.release()));
418 fSlides.push_back(slide);
419 }
420
421 gms = gms->next();
422 }
423
424 // reverse array
425 for (int i = 0; i < fSlides.count()/2; ++i) {
426 sk_sp<Slide> temp = fSlides[i];
427 fSlides[i] = fSlides[fSlides.count() - i - 1];
428 fSlides[fSlides.count() - i - 1] = temp;
429 }
430
jvanverthc7027ab2016-06-16 09:52:35 -0700431 // samples
432 const SkViewRegister* reg = SkViewRegister::Head();
433 while (reg) {
434 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700435 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
436 fSlides.push_back(slide);
437 }
jvanverthc7027ab2016-06-16 09:52:35 -0700438 reg = reg->next();
439 }
440
jvanverth2bb3b6d2016-04-08 07:24:09 -0700441 // SKPs
442 for (int i = 0; i < FLAGS_skps.count(); i++) {
443 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700444 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
445 continue;
446 }
447
jvanverth2bb3b6d2016-04-08 07:24:09 -0700448 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700449 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700450 if (slide) {
451 fSlides.push_back(slide);
452 }
453 } else {
454 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700455 SkString skpName;
456 while (it.next(&skpName)) {
457 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
458 continue;
459 }
460
461 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
462 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700463 if (slide) {
464 fSlides.push_back(slide);
465 }
466 }
467 }
468 }
liyuqian6f163d22016-06-13 12:26:45 -0700469
470 // JPGs
471 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
472 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
473 SkString jpgName;
474 while (it.next(&jpgName)) {
brianosmane1d20072016-07-12 09:07:33 -0700475 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) {
476 continue;
477 }
478
liyuqian6f163d22016-06-13 12:26:45 -0700479 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
480 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
481 if (slide) {
482 fSlides.push_back(slide);
483 }
484 }
485 }
Florin Malita094ccde2017-12-30 12:27:00 -0500486
487 // JSONs
488 for (const auto& json : FLAGS_jsons) {
489 SkOSFile::Iter it(json.c_str(), ".json");
490 SkString jsonName;
491 while (it.next(&jsonName)) {
492 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jsonName.c_str())) {
493 continue;
494 }
495 fSlides.push_back(sk_make_sp<SkottySlide>(jsonName, SkOSPath::Join(json.c_str(),
496 jsonName.c_str())));
497 }
498 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700499}
500
501
jvanverth34524262016-05-04 13:49:13 -0700502Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700503 fWindow->detach();
504 delete fWindow;
505}
506
brianosman05de2162016-05-06 13:28:57 -0700507void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700508 if (!fWindow) {
509 return;
510 }
511 if (fWindow->sampleCount() < 0) {
512 return; // Surface hasn't been created yet.
513 }
514
jvanverth34524262016-05-04 13:49:13 -0700515 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700516 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700517
Yuqian Li399b3c22017-08-03 11:08:15 -0400518 if (gSkUseDeltaAA) {
519 if (gSkForceDeltaAA) {
520 title.append(" <FDAA>");
521 } else {
522 title.append(" <DAA>");
523 }
524 } else if (gSkUseAnalyticAA) {
525 if (gSkForceAnalyticAA) {
526 title.append(" <FAAA>");
527 } else {
528 title.append(" <AAA>");
529 }
530 }
531
Yuqian Lib2ba6642017-11-22 12:07:41 -0500532 if (fTileCnt > 0) {
533 title.appendf(" T%d", fTileCnt);
534 if (fThreadCnt > 0) {
535 title.appendf("/%d", fThreadCnt);
536 }
537 }
538
Brian Osman92004802017-03-06 11:47:26 -0500539 switch (fColorMode) {
540 case ColorMode::kLegacy:
541 title.append(" Legacy 8888");
542 break;
543 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
544 title.append(" ColorManaged 8888 (Nonlinear blending)");
545 break;
546 case ColorMode::kColorManagedSRGB8888:
547 title.append(" ColorManaged 8888");
548 break;
549 case ColorMode::kColorManagedLinearF16:
550 title.append(" ColorManaged F16");
551 break;
552 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500553
Brian Osman92004802017-03-06 11:47:26 -0500554 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500555 int curPrimaries = -1;
556 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
557 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
558 curPrimaries = i;
559 break;
560 }
561 }
562 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500563
564 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
565 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
566 }
brianosman05de2162016-05-06 13:28:57 -0700567 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500568
csmartdalton578f0642017-02-24 16:04:47 -0700569 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700570 title.append(kBackendTypeStrings[fBackendType]);
csmartdalton578f0642017-02-24 16:04:47 -0700571 if (int msaa = fWindow->sampleCount()) {
572 title.appendf(" MSAA: %i", msaa);
573 }
574 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700575
576 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400577 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700578 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
579 }
580
brianosman05de2162016-05-06 13:28:57 -0700581 fWindow->setTitle(title.c_str());
582}
583
Jim Van Verth6f449692017-02-14 15:16:46 -0500584void Viewer::setStartupSlide() {
585
586 if (!FLAGS_slide.isEmpty()) {
587 int count = fSlides.count();
588 for (int i = 0; i < count; i++) {
589 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
590 fCurrentSlide = i;
591 return;
592 }
593 }
594
595 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
596 this->listNames();
597 }
598
599 fCurrentSlide = 0;
600}
601
602void Viewer::listNames() {
603 int count = fSlides.count();
604 SkDebugf("All Slides:\n");
605 for (int i = 0; i < count; i++) {
606 SkDebugf(" %s\n", fSlides[i]->getName().c_str());
607 }
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
Brian Osman56a24812017-12-19 11:15:16 -0500640 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400641
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 Osman56a24812017-12-19 11:15:16 -0500681 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500682 fWindow->pushLayer(&fImGuiLayer);
683
Brian Osman70d2f432017-11-08 09:54:10 -0500684 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
685 // will still include our correct sample count. But the re-created fWindow will lose that
686 // information. On Windows, we need to re-create the window when changing sample count,
687 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
688 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
689 // as if we had never changed windows at all).
690 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500691#endif
692
Brian Osman70d2f432017-11-08 09:54:10 -0500693 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500694}
695
Brian Osman92004802017-03-06 11:47:26 -0500696void Viewer::setColorMode(ColorMode colorMode) {
697 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700698
Brian Osmanf750fbc2017-02-08 10:47:28 -0500699 // 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 -0400700 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700701 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500702 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400703 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
704 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500705 if (wasInLegacy != wantLegacy) {
706 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700707 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500708 }
709
710 this->updateTitle();
711 fWindow->inval();
712}
713
714void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500715 SkAutoCanvasRestore autorestore(canvas, false);
716
Brian Osmanf750fbc2017-02-08 10:47:28 -0500717 // By default, we render directly into the window's surface/canvas
718 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500719 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700720
Brian Osmane0d4fba2017-03-15 10:24:55 -0400721 // If we're in any of the color managed modes, construct the color space we're going to use
722 sk_sp<SkColorSpace> cs = nullptr;
723 if (ColorMode::kLegacy != fColorMode) {
724 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
725 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400726 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400727 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500728 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
729 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
730 } else {
731 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
732 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400733 }
734
Brian Osman3ac99cf2017-12-01 11:23:53 -0500735 if (fSaveToSKP) {
736 SkPictureRecorder recorder;
737 SkCanvas* recorderCanvas = recorder.beginRecording(
738 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
739 // In xform-canvas mode, record the transformed output
740 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
741 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
742 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
743 recorderCanvas = xformCanvas.get();
744 }
745 fSlides[fCurrentSlide]->draw(recorderCanvas);
746 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
747 SkFILEWStream stream("sample_app.skp");
748 picture->serialize(&stream);
749 fSaveToSKP = false;
750 }
751
Brian Osmane0d4fba2017-03-15 10:24:55 -0400752 // 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 -0500753 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
754 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500755 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -0500756 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
757 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -0500758 if (Window::kRaster_BackendType == fBackendType ||
759 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500760 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400761 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
762 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
763
Brian Osman92004802017-03-06 11:47:26 -0500764 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
765 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400766 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
767 // with a special canvas (below) that has the color space attached
768 sk_sp<SkColorSpace> offscreenColorSpace =
769 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500770 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400771 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500772 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
773 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -0500774 SkPixmap offscreenPixmap;
775 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
776 SkBitmap offscreenBitmap;
777 offscreenBitmap.installPixels(offscreenPixmap);
778 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
779 fThreadCnt, fExecutor.get()));
780 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
781 slideCanvas = threadedCanvas.get();
782 } else {
783 slideCanvas = offscreenSurface->getCanvas();
784 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500785 }
786
Brian Osmane0d4fba2017-03-15 10:24:55 -0400787 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
788 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
789 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
790 slideCanvas = xformCanvas.get();
791 }
792
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500793 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500794 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500795 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500796 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -0500797 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500798 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osman56a24812017-12-19 11:15:16 -0500799 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500800 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500801
802 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -0500803 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -0500804 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -0500805 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500806
807 // If we rendered offscreen, snap an image and push the results to the window's canvas
808 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500809 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500810
811 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400812 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500813 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400814 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500815 SkPaint paint;
816 paint.setBlendMode(SkBlendMode::kSrc);
817 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -0700818 }
liyuqian6f163d22016-06-13 12:26:45 -0700819}
820
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700821void Viewer::onBackendCreated() {
822 this->updateTitle();
823 this->updateUIState();
824 this->setupCurrentSlide(-1);
Brian Osman56a24812017-12-19 11:15:16 -0500825 fStatsLayer.resetMeasurements();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700826 fWindow->show();
827 fWindow->inval();
828}
Jim Van Verth6f449692017-02-14 15:16:46 -0500829
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700830void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -0400831 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -0700832
brianosman622c8d52016-05-10 06:50:49 -0700833 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -0700834
Brian Osmand67e5182017-12-08 16:46:09 -0500835 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -0500836
Brian Osman1df161a2017-02-09 12:10:20 -0500837 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -0400838 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700839}
840
jvanverth814e38d2016-06-06 08:48:47 -0700841bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400842 if (GestureDevice::kMouse == fGestureDevice) {
843 return false;
844 }
liyuqiand3cdbca2016-05-17 12:44:20 -0700845 void* castedOwner = reinterpret_cast<void*>(owner);
846 switch (state) {
847 case Window::kUp_InputState: {
848 fGesture.touchEnd(castedOwner);
849 break;
850 }
851 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400852 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700853 break;
854 }
855 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400856 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700857 break;
858 }
859 }
Brian Osmanb53f48c2017-06-07 10:00:30 -0400860 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -0700861 fWindow->inval();
862 return true;
863}
864
Brian Osman80fc07e2017-12-08 16:45:43 -0500865bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -0500866 if (GestureDevice::kTouch == fGestureDevice) {
867 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -0500868 }
Brian Osman16c81a12017-12-20 11:58:34 -0500869
870 if (fSlides[fCurrentSlide]->onMouse(x, y, state, modifiers)) {
871 fWindow->inval();
872 return true;
873 }
874
875 switch (state) {
876 case Window::kUp_InputState: {
877 fGesture.touchEnd(nullptr);
878 break;
879 }
880 case Window::kDown_InputState: {
881 fGesture.touchBegin(nullptr, x, y);
882 break;
883 }
884 case Window::kMove_InputState: {
885 fGesture.touchMoved(nullptr, x, y);
886 break;
887 }
888 }
889 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
890
891 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
892 fWindow->inval();
893 }
Jim Van Verthe7705782017-05-04 14:00:59 -0400894 return true;
895}
896
Brian Osmana109e392017-02-24 09:49:14 -0500897static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
898 const ImVec2& pos, const ImVec2& size) {
899 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
900 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
901
902 // Invisible 10x10 button
903 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
904 ImGui::InvisibleButton(label, ImVec2(10, 10));
905
906 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
907 ImGuiIO& io = ImGui::GetIO();
908 // Normalized mouse position, relative to our gamut box
909 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
910 // Clamp to edge of box, convert back to primary scale
911 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
912 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
913 }
914
915 if (ImGui::IsItemHovered()) {
916 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
917 }
918
919 // Return screen coordinates for the caller. We could just return center here, but we'd have
920 // one frame of lag during drag.
921 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
922}
923
924static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
925 ImDrawList* drawList = ImGui::GetWindowDrawList();
926
927 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
928 // width, and scale the height to maintain aspect ratio.
929 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
930 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
931 ImVec2 pos = ImGui::GetCursorScreenPos();
932
933 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
934 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
935 // Magic numbers are pixel locations of the origin and upper-right corner.
936 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
937 ImVec2(242, 61), ImVec2(1897, 1922));
938 ImVec2 endPos = ImGui::GetCursorPos();
939
940 // Primary markers
941 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
942 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
943 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
944 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
945
946 // Gamut triangle
947 drawList->AddCircle(r, 5.0f, 0xFF000040);
948 drawList->AddCircle(g, 5.0f, 0xFF004000);
949 drawList->AddCircle(b, 5.0f, 0xFF400000);
950 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
951 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
952
953 // Re-position cursor immediate after the diagram for subsequent controls
954 ImGui::SetCursorPos(endPos);
955}
956
Brian Osmand67e5182017-12-08 16:46:09 -0500957void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -0500958 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
959 if (fShowImGuiTestWindow) {
960 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
961 }
962
963 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -0500964 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
965 // always visible, we can end up in a layout feedback loop.
966 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -0500967 DisplayParams params = fWindow->getRequestedDisplayParams();
968 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -0500969 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
970 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -0500971 if (ImGui::CollapsingHeader("Backend")) {
972 int newBackend = static_cast<int>(fBackendType);
973 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
974 ImGui::SameLine();
975 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -0400976#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
977 ImGui::SameLine();
978 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
979#endif
Brian Osman621491e2017-02-28 15:45:01 -0500980#if defined(SK_VULKAN)
981 ImGui::SameLine();
982 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
983#endif
984 if (newBackend != fBackendType) {
985 fDeferredActions.push_back([=]() {
986 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
987 });
988 }
Brian Osman8a9de3d2017-03-01 14:59:05 -0500989
Brian Salomon99a33902017-03-07 15:16:34 -0500990 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400991 bool* wire = &params.fGrContextOptions.fWireframeMode;
992 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
993 paramsChanged = true;
994 }
Brian Salomon99a33902017-03-07 15:16:34 -0500995
Brian Osman28b12522017-03-08 17:10:24 -0500996 if (ctx) {
997 int sampleCount = fWindow->sampleCount();
998 ImGui::Text("MSAA: "); ImGui::SameLine();
999 ImGui::RadioButton("0", &sampleCount, 0); ImGui::SameLine();
1000 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1001 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1002 ImGui::RadioButton("16", &sampleCount, 16);
1003
1004 if (sampleCount != params.fMSAASampleCount) {
1005 params.fMSAASampleCount = sampleCount;
1006 paramsChanged = true;
1007 }
1008 }
1009
Brian Osman8a9de3d2017-03-01 14:59:05 -05001010 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001011 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001012 auto prButton = [&](GpuPathRenderers x) {
1013 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001014 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1015 params.fGrContextOptions.fGpuPathRenderers = x;
1016 paramsChanged = true;
1017 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001018 }
1019 };
1020
1021 if (!ctx) {
1022 ImGui::RadioButton("Software", true);
1023 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001024 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001025 prButton(GpuPathRenderers::kAll);
1026 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1027 prButton(GpuPathRenderers::kStencilAndCover);
1028 }
1029 if (ctx->caps()->sampleShadingSupport()) {
1030 prButton(GpuPathRenderers::kMSAA);
1031 }
1032 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001033 prButton(GpuPathRenderers::kNone);
1034 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001035 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001036 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001037 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1038 prButton(GpuPathRenderers::kCoverageCounting);
1039 }
Jim Van Verth83010462017-03-16 08:45:39 -04001040 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001041 prButton(GpuPathRenderers::kTessellating);
1042 prButton(GpuPathRenderers::kNone);
1043 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001044 ImGui::TreePop();
1045 }
Brian Osman621491e2017-02-28 15:45:01 -05001046 }
1047
Brian Osmanfce09c52017-11-14 15:32:20 -05001048 if (fShowSlidePicker) {
1049 ImGui::SetNextTreeNodeOpen(true);
1050 }
1051
Brian Osman79086b92017-02-10 13:36:16 -05001052 if (ImGui::CollapsingHeader("Slide")) {
1053 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001054 static ImVector<const char*> filteredSlideNames;
1055 static ImVector<int> filteredSlideIndices;
1056
Brian Osmanfce09c52017-11-14 15:32:20 -05001057 if (fShowSlidePicker) {
1058 ImGui::SetKeyboardFocusHere();
1059 fShowSlidePicker = false;
1060 }
1061
Brian Osman79086b92017-02-10 13:36:16 -05001062 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001063 filteredSlideNames.clear();
1064 filteredSlideIndices.clear();
1065 int filteredIndex = 0;
1066 for (int i = 0; i < fSlides.count(); ++i) {
1067 const char* slideName = fSlides[i]->getName().c_str();
1068 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1069 if (i == fCurrentSlide) {
1070 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001071 }
Brian Osmanf479e422017-11-08 13:11:36 -05001072 filteredSlideNames.push_back(slideName);
1073 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001074 }
Brian Osman79086b92017-02-10 13:36:16 -05001075 }
Brian Osmanf479e422017-11-08 13:11:36 -05001076
1077 int previousSlide = fCurrentSlide;
1078 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1079 filteredSlideNames.size(), 20)) {
1080 fCurrentSlide = filteredSlideIndices[filteredIndex];
1081 setupCurrentSlide(previousSlide);
Brian Osman79086b92017-02-10 13:36:16 -05001082 }
1083 }
Brian Osmana109e392017-02-24 09:49:14 -05001084
1085 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001086 ColorMode newMode = fColorMode;
1087 auto cmButton = [&](ColorMode mode, const char* label) {
1088 if (ImGui::RadioButton(label, mode == fColorMode)) {
1089 newMode = mode;
1090 }
1091 };
1092
1093 cmButton(ColorMode::kLegacy, "Legacy 8888");
1094 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1095 "Color Managed 8888 (Nonlinear blending)");
1096 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1097 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1098
1099 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001100 // It isn't safe to switch color mode now (in the middle of painting). We might
1101 // tear down the back-end, etc... Defer this change until the next onIdle.
1102 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001103 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001104 });
Brian Osmana109e392017-02-24 09:49:14 -05001105 }
1106
1107 // Pick from common gamuts:
1108 int primariesIdx = 4; // Default: Custom
1109 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1110 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1111 primariesIdx = i;
1112 break;
1113 }
1114 }
1115
Brian Osmanfdab5762017-11-09 10:27:55 -05001116 // When we're in xform canvas mode, we can alter the transfer function, too
1117 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1118 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1119 }
1120
Brian Osmana109e392017-02-24 09:49:14 -05001121 if (ImGui::Combo("Primaries", &primariesIdx,
1122 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1123 if (primariesIdx >= 0 && primariesIdx <= 3) {
1124 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1125 }
1126 }
1127
1128 // Allow direct editing of gamut
1129 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1130 }
Brian Osman79086b92017-02-10 13:36:16 -05001131 }
Brian Salomon99a33902017-03-07 15:16:34 -05001132 if (paramsChanged) {
1133 fDeferredActions.push_back([=]() {
1134 fWindow->setRequestedDisplayParams(params);
1135 fWindow->inval();
1136 this->updateTitle();
1137 });
1138 }
Brian Osman79086b92017-02-10 13:36:16 -05001139 ImGui::End();
1140 }
1141
Brian Osmanf6877092017-02-13 09:39:57 -05001142 if (fShowZoomWindow && fLastImage) {
1143 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001144 static int zoomFactor = 8;
1145 if (ImGui::Button("<<")) {
1146 zoomFactor = SkTMax(zoomFactor / 2, 4);
1147 }
1148 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1149 if (ImGui::Button(">>")) {
1150 zoomFactor = SkTMin(zoomFactor * 2, 32);
1151 }
Brian Osmanf6877092017-02-13 09:39:57 -05001152
Brian Osmanf6877092017-02-13 09:39:57 -05001153 ImVec2 mousePos = ImGui::GetMousePos();
1154 ImVec2 avail = ImGui::GetContentRegionAvail();
1155
Brian Osmanead517d2017-11-13 15:36:36 -05001156 uint32_t pixel = 0;
1157 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1158 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1159 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001160 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1161 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1162 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001163 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1164 }
1165
Brian Osmand67e5182017-12-08 16:46:09 -05001166 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001167 // Translate so the region of the image that's under the mouse cursor is centered
1168 // in the zoom canvas:
1169 c->scale(zoomFactor, zoomFactor);
1170 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1171 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1172 c->drawImage(this->fLastImage, 0, 0);
1173
1174 SkPaint outline;
1175 outline.setStyle(SkPaint::kStroke_Style);
1176 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1177 });
Brian Osmanf6877092017-02-13 09:39:57 -05001178 }
1179
1180 ImGui::End();
1181 }
Brian Osman79086b92017-02-10 13:36:16 -05001182}
1183
liyuqian2edb0f42016-07-06 14:11:32 -07001184void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001185 for (int i = 0; i < fDeferredActions.count(); ++i) {
1186 fDeferredActions[i]();
1187 }
1188 fDeferredActions.reset();
1189
Brian Osman56a24812017-12-19 11:15:16 -05001190 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001191 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001192 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001193 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001194
Brian Osman79086b92017-02-10 13:36:16 -05001195 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001196 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001197 fWindow->inval();
1198 }
jvanverth9f372462016-04-06 06:08:59 -07001199}
liyuqiane5a6cd92016-05-27 08:52:52 -07001200
1201void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001202 if (!fWindow) {
1203 return;
1204 }
1205 if (fWindow->sampleCount() < 0) {
1206 return; // Surface hasn't been created yet.
1207 }
1208
liyuqianb73c24b2016-06-03 08:47:23 -07001209 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001210 Json::Value slideState(Json::objectValue);
1211 slideState[kName] = kSlideStateName;
1212 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001213 if (fAllSlideNames.size() == 0) {
1214 for(auto slide : fSlides) {
1215 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1216 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001217 }
liyuqian1f508fd2016-06-07 06:57:40 -07001218 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001219
liyuqianb73c24b2016-06-03 08:47:23 -07001220 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001221 Json::Value backendState(Json::objectValue);
1222 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001223 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001224 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001225 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001226 backendState[kOptions].append(Json::Value(str));
1227 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001228
csmartdalton578f0642017-02-24 16:04:47 -07001229 // MSAA state
1230 Json::Value msaaState(Json::objectValue);
1231 msaaState[kName] = kMSAAStateName;
1232 msaaState[kValue] = fWindow->sampleCount();
1233 msaaState[kOptions] = Json::Value(Json::arrayValue);
1234 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1235 msaaState[kOptions].append(Json::Value(0));
1236 } else {
1237 for (int msaa : {0, 4, 8, 16}) {
1238 msaaState[kOptions].append(Json::Value(msaa));
1239 }
1240 }
1241
csmartdalton61cd31a2017-02-27 17:00:53 -07001242 // Path renderer state
1243 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1244 Json::Value prState(Json::objectValue);
1245 prState[kName] = kPathRendererStateName;
1246 prState[kValue] = gPathRendererNames[pr];
1247 prState[kOptions] = Json::Value(Json::arrayValue);
1248 const GrContext* ctx = fWindow->getGrContext();
1249 if (!ctx) {
1250 prState[kOptions].append("Software");
1251 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001252 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001253 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1254 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1255 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1256 }
1257 if (ctx->caps()->sampleShadingSupport()) {
1258 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1259 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001260 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001261 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1262 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001263 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001264 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001265 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1266 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1267 }
Jim Van Verth83010462017-03-16 08:45:39 -04001268 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001269 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001270 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1271 }
1272
liyuqianb73c24b2016-06-03 08:47:23 -07001273 // Softkey state
1274 Json::Value softkeyState(Json::objectValue);
1275 softkeyState[kName] = kSoftkeyStateName;
1276 softkeyState[kValue] = kSoftkeyHint;
1277 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1278 softkeyState[kOptions].append(kSoftkeyHint);
1279 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1280 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1281 }
1282
liyuqian1f508fd2016-06-07 06:57:40 -07001283 // FPS state
1284 Json::Value fpsState(Json::objectValue);
1285 fpsState[kName] = kFpsStateName;
Brian Osman56a24812017-12-19 11:15:16 -05001286 double animTime = fStatsLayer.getLastTime(fAnimateTimer);
1287 double paintTime = fStatsLayer.getLastTime(fPaintTimer);
1288 double flushTime = fStatsLayer.getLastTime(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001289 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
Brian Osman56a24812017-12-19 11:15:16 -05001290 animTime + paintTime + flushTime,
1291 animTime, paintTime, flushTime).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001292 fpsState[kOptions] = Json::Value(Json::arrayValue);
1293
liyuqiane5a6cd92016-05-27 08:52:52 -07001294 Json::Value state(Json::arrayValue);
1295 state.append(slideState);
1296 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001297 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001298 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001299 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001300 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001301
Brian Osmaneff04b52017-11-21 13:18:02 -05001302 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001303}
1304
1305void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001306 // For those who will add more features to handle the state change in this function:
1307 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1308 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1309 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001310 if (stateName.equals(kSlideStateName)) {
1311 int previousSlide = fCurrentSlide;
1312 fCurrentSlide = 0;
1313 for(auto slide : fSlides) {
1314 if (slide->getName().equals(stateValue)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001315 this->setupCurrentSlide(previousSlide);
liyuqiane5a6cd92016-05-27 08:52:52 -07001316 break;
1317 }
1318 fCurrentSlide++;
1319 }
1320 if (fCurrentSlide >= fSlides.count()) {
1321 fCurrentSlide = previousSlide;
1322 SkDebugf("Slide not found: %s", stateValue.c_str());
1323 }
liyuqian6cb70252016-06-02 12:16:25 -07001324 } else if (stateName.equals(kBackendStateName)) {
1325 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1326 if (stateValue.equals(kBackendTypeStrings[i])) {
1327 if (fBackendType != i) {
1328 fBackendType = (sk_app::Window::BackendType)i;
1329 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001330 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001331 }
1332 break;
1333 }
1334 }
csmartdalton578f0642017-02-24 16:04:47 -07001335 } else if (stateName.equals(kMSAAStateName)) {
1336 DisplayParams params = fWindow->getRequestedDisplayParams();
1337 int sampleCount = atoi(stateValue.c_str());
1338 if (sampleCount != params.fMSAASampleCount) {
1339 params.fMSAASampleCount = sampleCount;
1340 fWindow->setRequestedDisplayParams(params);
1341 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001342 this->updateTitle();
1343 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001344 }
1345 } else if (stateName.equals(kPathRendererStateName)) {
1346 DisplayParams params = fWindow->getRequestedDisplayParams();
1347 for (const auto& pair : gPathRendererNames) {
1348 if (pair.second == stateValue.c_str()) {
1349 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1350 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1351 fWindow->setRequestedDisplayParams(params);
1352 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001353 this->updateTitle();
1354 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001355 }
1356 break;
1357 }
csmartdalton578f0642017-02-24 16:04:47 -07001358 }
liyuqianb73c24b2016-06-03 08:47:23 -07001359 } else if (stateName.equals(kSoftkeyStateName)) {
1360 if (!stateValue.equals(kSoftkeyHint)) {
1361 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001362 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001363 }
liyuqian2edb0f42016-07-06 14:11:32 -07001364 } else if (stateName.equals(kRefreshStateName)) {
1365 // This state is actually NOT in the UI state.
1366 // We use this to allow Android to quickly set bool fRefresh.
1367 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001368 } else {
1369 SkDebugf("Unknown stateName: %s", stateName.c_str());
1370 }
1371}
Brian Osman79086b92017-02-10 13:36:16 -05001372
1373bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001374 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001375}
1376
1377bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001378 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001379 fWindow->inval();
1380 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001381 } else {
1382 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001383 }
Brian Osman79086b92017-02-10 13:36:16 -05001384}