blob: 7d69567a293c869567a64b74d0c0eb630fcd2ad2 [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 Malita54f65c42018-01-16 17:04:30 -050014#include "SkottieSlide.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
Brian Salomonbdecacf2018-02-02 20:32:49 -050081static DEFINE_int32(msaa, 1, "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)
Florin Malitaab99c342018-01-16 16:23:03 -0500176 : fCurrentSlide(-1)
177 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500178 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500179 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500180 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500181 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500182 , fShowZoomWindow(false)
183 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700184 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500185 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500186 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500187 // Our UI can only tweak gamma (currently), so start out gamma-only
188 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700189 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400190 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500191 , fTileCnt(0)
192 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700193{
Greg Daniel285db442016-10-14 09:12:53 -0400194 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700195
Brian Osmanf09e35e2017-12-15 14:48:09 -0500196 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
197 gPathRendererNames[GpuPathRenderers::kDefault] =
198 "Default Ganesh Behavior (best path renderer, not including CCPR)";
199 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
200 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
201 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
202 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
203 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
204 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
csmartdalton61cd31a2017-02-27 17:00:53 -0700205
jvanverth2bb3b6d2016-04-08 07:24:09 -0700206 SkDebugf("Command line arguments: ");
207 for (int i = 1; i < argc; ++i) {
208 SkDebugf("%s ", argv[i]);
209 }
210 SkDebugf("\n");
211
212 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500213#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400214 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500215#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700216
Brian Osmanbc8150f2017-07-24 11:38:01 -0400217 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400218 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400219
bsalomon6c471f72016-07-26 12:56:32 -0700220 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700221 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700222
csmartdalton578f0642017-02-24 16:04:47 -0700223 DisplayParams displayParams;
224 displayParams.fMSAASampleCount = FLAGS_msaa;
Chris Dalton040238b2017-12-18 14:22:34 -0700225 SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
csmartdalton578f0642017-02-24 16:04:47 -0700226 fWindow->setRequestedDisplayParams(displayParams);
227
Brian Osman56a24812017-12-19 11:15:16 -0500228 // Configure timers
229 fStatsLayer.setActive(false);
230 fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff);
231 fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN);
232 fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666);
233
jvanverth9f372462016-04-06 06:08:59 -0700234 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700235 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500236 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500237 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500238 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700239
brianosman622c8d52016-05-10 06:50:49 -0700240 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500241 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
242 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
243 fWindow->inval();
244 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500245 // Command to jump directly to the slide picker and give it focus
246 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
247 this->fShowImGuiDebugWindow = true;
248 this->fShowSlidePicker = true;
249 fWindow->inval();
250 });
251 // Alias that to Backspace, to match SampleApp
252 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
253 this->fShowImGuiDebugWindow = true;
254 this->fShowSlidePicker = true;
255 fWindow->inval();
256 });
Brian Osman79086b92017-02-10 13:36:16 -0500257 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
258 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
259 fWindow->inval();
260 });
Brian Osmanf6877092017-02-13 09:39:57 -0500261 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
262 this->fShowZoomWindow = !this->fShowZoomWindow;
263 fWindow->inval();
264 });
brianosman622c8d52016-05-10 06:50:49 -0700265 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500266 fStatsLayer.setActive(!fStatsLayer.getActive());
brianosman622c8d52016-05-10 06:50:49 -0700267 fWindow->inval();
268 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400269 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
Brian Osman56a24812017-12-19 11:15:16 -0500270 fStatsLayer.resetMeasurements();
Jim Van Verth90dcce52017-11-03 13:36:07 -0400271 this->updateTitle();
272 fWindow->inval();
273 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500274 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500275 switch (fColorMode) {
276 case ColorMode::kLegacy:
277 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
278 break;
279 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
280 this->setColorMode(ColorMode::kColorManagedSRGB8888);
281 break;
282 case ColorMode::kColorManagedSRGB8888:
283 this->setColorMode(ColorMode::kColorManagedLinearF16);
284 break;
285 case ColorMode::kColorManagedLinearF16:
286 this->setColorMode(ColorMode::kLegacy);
287 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500288 }
brianosman622c8d52016-05-10 06:50:49 -0700289 });
290 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500291 this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0);
brianosman622c8d52016-05-10 06:50:49 -0700292 });
293 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
Florin Malitaab99c342018-01-16 16:23:03 -0500294 this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1);
brianosman622c8d52016-05-10 06:50:49 -0700295 });
296 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
297 this->changeZoomLevel(1.f / 32.f);
298 fWindow->inval();
299 });
300 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
301 this->changeZoomLevel(-1.f / 32.f);
302 fWindow->inval();
303 });
jvanverthaf236b52016-05-20 06:01:06 -0700304 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400305 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
306 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500307 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400308#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
309 if (newBackend == sk_app::Window::kVulkan_BackendType) {
310 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
311 sk_app::Window::kBackendTypeCount);
312 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
313 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500314 }
315#endif
Brian Osman621491e2017-02-28 15:45:01 -0500316 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700317 });
brianosman622c8d52016-05-10 06:50:49 -0700318
Yuqian Lib2ba6642017-11-22 12:07:41 -0500319 fCommands.addCommand('A', "AA", "Toggle analytic AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400320 if (!gSkUseAnalyticAA) {
321 gSkUseAnalyticAA = true;
322 } else if (!gSkForceAnalyticAA) {
323 gSkForceAnalyticAA = true;
324 } else {
325 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
326 }
327 this->updateTitle();
328 fWindow->inval();
329 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500330 fCommands.addCommand('D', "AA", "Toggle delta AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400331 if (!gSkUseDeltaAA) {
332 gSkUseDeltaAA = true;
333 } else if (!gSkForceDeltaAA) {
334 gSkForceDeltaAA = true;
335 } else {
336 gSkUseDeltaAA = gSkForceDeltaAA = false;
337 }
338 this->updateTitle();
339 fWindow->inval();
340 });
341
Yuqian Lib2ba6642017-11-22 12:07:41 -0500342 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
343 fTileCnt++;
344 if (fThreadCnt == 0) {
345 this->resetExecutor();
346 }
347 this->updateTitle();
348 fWindow->inval();
349 });
350 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
351 fTileCnt = SkTMax(0, fTileCnt - 1);
352 if (fThreadCnt == 0) {
353 this->resetExecutor();
354 }
355 this->updateTitle();
356 fWindow->inval();
357 });
358 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
359 if (fTileCnt == 0) {
360 return;
361 }
362 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
363 this->resetExecutor();
364 this->updateTitle();
365 fWindow->inval();
366 });
367 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
368 if (fTileCnt == 0) {
369 return;
370 }
371 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
372 this->resetExecutor();
373 this->updateTitle();
374 fWindow->inval();
375 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500376 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
377 fSaveToSKP = true;
378 fWindow->inval();
379 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500380
jvanverth2bb3b6d2016-04-08 07:24:09 -0700381 // set up slides
382 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500383 if (FLAGS_list) {
384 this->listNames();
385 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700386
djsollen12d62a72016-04-21 07:59:44 -0700387 fAnimTimer.run();
388
Hal Canaryc465d132017-12-08 10:21:31 -0500389 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500390 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400391 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500392 }
393 fImGuiGamutPaint.setColor(SK_ColorWHITE);
394 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
395
Brian Osman70d2f432017-11-08 09:54:10 -0500396 fWindow->attach(backend_type_for_window(fBackendType));
Jim Van Verth0848fb02018-01-22 13:39:30 -0500397 this->setCurrentSlide(this->startupSlide());
jvanverth9f372462016-04-06 06:08:59 -0700398}
399
jvanverth34524262016-05-04 13:49:13 -0700400void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700401 fAllSlideNames = Json::Value(Json::arrayValue);
402
jvanverth2bb3b6d2016-04-08 07:24:09 -0700403 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
404 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400405 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700406
407 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
408 sk_sp<Slide> slide(new GMSlide(gm.release()));
409 fSlides.push_back(slide);
410 }
411
412 gms = gms->next();
413 }
414
415 // reverse array
416 for (int i = 0; i < fSlides.count()/2; ++i) {
417 sk_sp<Slide> temp = fSlides[i];
418 fSlides[i] = fSlides[fSlides.count() - i - 1];
419 fSlides[fSlides.count() - i - 1] = temp;
420 }
421
jvanverthc7027ab2016-06-16 09:52:35 -0700422 // samples
423 const SkViewRegister* reg = SkViewRegister::Head();
424 while (reg) {
425 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700426 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
427 fSlides.push_back(slide);
428 }
jvanverthc7027ab2016-06-16 09:52:35 -0700429 reg = reg->next();
430 }
431
jvanverth2bb3b6d2016-04-08 07:24:09 -0700432 // SKPs
433 for (int i = 0; i < FLAGS_skps.count(); i++) {
434 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700435 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
436 continue;
437 }
438
jvanverth2bb3b6d2016-04-08 07:24:09 -0700439 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700440 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700441 if (slide) {
442 fSlides.push_back(slide);
443 }
444 } else {
445 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700446 SkString skpName;
447 while (it.next(&skpName)) {
448 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
449 continue;
450 }
451
452 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
453 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700454 if (slide) {
455 fSlides.push_back(slide);
456 }
457 }
458 }
459 }
liyuqian6f163d22016-06-13 12:26:45 -0700460
461 // JPGs
462 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
463 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
464 SkString jpgName;
465 while (it.next(&jpgName)) {
brianosmane1d20072016-07-12 09:07:33 -0700466 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) {
467 continue;
468 }
469
liyuqian6f163d22016-06-13 12:26:45 -0700470 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
471 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
472 if (slide) {
473 fSlides.push_back(slide);
474 }
475 }
476 }
Florin Malita094ccde2017-12-30 12:27:00 -0500477
478 // JSONs
479 for (const auto& json : FLAGS_jsons) {
Florin Malita54f65c42018-01-16 17:04:30 -0500480 fSlides.push_back(sk_make_sp<SkottieSlide2>(json));
Mike Reed29859872018-01-08 08:25:27 -0500481
Florin Malita094ccde2017-12-30 12:27:00 -0500482 SkOSFile::Iter it(json.c_str(), ".json");
483 SkString jsonName;
484 while (it.next(&jsonName)) {
485 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jsonName.c_str())) {
486 continue;
487 }
Florin Malita54f65c42018-01-16 17:04:30 -0500488 fSlides.push_back(sk_make_sp<SkottieSlide>(jsonName, SkOSPath::Join(json.c_str(),
489 jsonName.c_str())));
Florin Malita094ccde2017-12-30 12:27:00 -0500490 }
491 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700492}
493
494
jvanverth34524262016-05-04 13:49:13 -0700495Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700496 fWindow->detach();
497 delete fWindow;
498}
499
brianosman05de2162016-05-06 13:28:57 -0700500void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700501 if (!fWindow) {
502 return;
503 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500504 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700505 return; // Surface hasn't been created yet.
506 }
507
jvanverth34524262016-05-04 13:49:13 -0700508 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700509 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700510
Yuqian Li399b3c22017-08-03 11:08:15 -0400511 if (gSkUseDeltaAA) {
512 if (gSkForceDeltaAA) {
513 title.append(" <FDAA>");
514 } else {
515 title.append(" <DAA>");
516 }
517 } else if (gSkUseAnalyticAA) {
518 if (gSkForceAnalyticAA) {
519 title.append(" <FAAA>");
520 } else {
521 title.append(" <AAA>");
522 }
523 }
524
Yuqian Lib2ba6642017-11-22 12:07:41 -0500525 if (fTileCnt > 0) {
526 title.appendf(" T%d", fTileCnt);
527 if (fThreadCnt > 0) {
528 title.appendf("/%d", fThreadCnt);
529 }
530 }
531
Brian Osman92004802017-03-06 11:47:26 -0500532 switch (fColorMode) {
533 case ColorMode::kLegacy:
534 title.append(" Legacy 8888");
535 break;
536 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
537 title.append(" ColorManaged 8888 (Nonlinear blending)");
538 break;
539 case ColorMode::kColorManagedSRGB8888:
540 title.append(" ColorManaged 8888");
541 break;
542 case ColorMode::kColorManagedLinearF16:
543 title.append(" ColorManaged F16");
544 break;
545 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500546
Brian Osman92004802017-03-06 11:47:26 -0500547 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500548 int curPrimaries = -1;
549 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
550 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
551 curPrimaries = i;
552 break;
553 }
554 }
555 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500556
557 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
558 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
559 }
brianosman05de2162016-05-06 13:28:57 -0700560 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500561
csmartdalton578f0642017-02-24 16:04:47 -0700562 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700563 title.append(kBackendTypeStrings[fBackendType]);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500564 int msaa = fWindow->sampleCount();
565 if (msaa > 1) {
csmartdalton578f0642017-02-24 16:04:47 -0700566 title.appendf(" MSAA: %i", msaa);
567 }
568 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700569
570 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400571 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700572 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
573 }
574
brianosman05de2162016-05-06 13:28:57 -0700575 fWindow->setTitle(title.c_str());
576}
577
Florin Malitaab99c342018-01-16 16:23:03 -0500578int Viewer::startupSlide() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500579
580 if (!FLAGS_slide.isEmpty()) {
581 int count = fSlides.count();
582 for (int i = 0; i < count; i++) {
583 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
Florin Malitaab99c342018-01-16 16:23:03 -0500584 return i;
Jim Van Verth6f449692017-02-14 15:16:46 -0500585 }
586 }
587
588 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
589 this->listNames();
590 }
591
Florin Malitaab99c342018-01-16 16:23:03 -0500592 return 0;
Jim Van Verth6f449692017-02-14 15:16:46 -0500593}
594
Florin Malitaab99c342018-01-16 16:23:03 -0500595void Viewer::listNames() const {
Jim Van Verth6f449692017-02-14 15:16:46 -0500596 SkDebugf("All Slides:\n");
Florin Malitaab99c342018-01-16 16:23:03 -0500597 for (const auto& slide : fSlides) {
598 SkDebugf(" %s\n", slide->getName().c_str());
Jim Van Verth6f449692017-02-14 15:16:46 -0500599 }
600}
601
Florin Malitaab99c342018-01-16 16:23:03 -0500602void Viewer::setCurrentSlide(int slide) {
603 SkASSERT(slide >= 0 && slide < fSlides.count());
liyuqian6f163d22016-06-13 12:26:45 -0700604
Florin Malitaab99c342018-01-16 16:23:03 -0500605 if (slide == fCurrentSlide) {
606 return;
607 }
608
609 if (fCurrentSlide >= 0) {
610 fSlides[fCurrentSlide]->unload();
611 }
612
613 fSlides[slide]->load(SkIntToScalar(fWindow->width()),
614 SkIntToScalar(fWindow->height()));
615 fCurrentSlide = slide;
616 this->setupCurrentSlide();
617}
618
619void Viewer::setupCurrentSlide() {
Jim Van Verth0848fb02018-01-22 13:39:30 -0500620 if (fCurrentSlide >= 0) {
621 // prepare dimensions for image slides
622 fGesture.resetTouchState();
623 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700624
Jim Van Verth0848fb02018-01-22 13:39:30 -0500625 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
626 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
627 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400628
Jim Van Verth0848fb02018-01-22 13:39:30 -0500629 // Start with a matrix that scales the slide to the available screen space
630 if (fWindow->scaleContentToFit()) {
631 if (windowRect.width() > 0 && windowRect.height() > 0) {
632 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
633 }
liyuqiane46e4f02016-05-20 07:32:19 -0700634 }
Jim Van Verth0848fb02018-01-22 13:39:30 -0500635
636 // Prevent the user from dragging content so far outside the window they can't find it again
637 fGesture.setTransLimit(slideBounds, windowRect, fDefaultMatrix);
638
639 this->updateTitle();
640 this->updateUIState();
641
642 fStatsLayer.resetMeasurements();
643
644 fWindow->inval();
liyuqiane46e4f02016-05-20 07:32:19 -0700645 }
jvanverthc265a922016-04-08 12:51:45 -0700646}
647
648#define MAX_ZOOM_LEVEL 8
649#define MIN_ZOOM_LEVEL -8
650
jvanverth34524262016-05-04 13:49:13 -0700651void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700652 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400653 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
jvanverthc265a922016-04-08 12:51:45 -0700654}
655
liyuqiand3cdbca2016-05-17 12:44:20 -0700656SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700657 SkMatrix m;
jvanverthc265a922016-04-08 12:51:45 -0700658
Brian Osman42bb6ac2017-06-05 08:46:04 -0400659 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
660 : SK_Scalar1 + fZoomLevel;
661 m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700662 m.preConcat(fGesture.globalM());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400663 m.preConcat(fDefaultMatrix);
664 m.preScale(zoomScale, zoomScale);
jvanverthc265a922016-04-08 12:51:45 -0700665
liyuqiand3cdbca2016-05-17 12:44:20 -0700666 return m;
jvanverthc265a922016-04-08 12:51:45 -0700667}
668
Brian Osman621491e2017-02-28 15:45:01 -0500669void Viewer::setBackend(sk_app::Window::BackendType backendType) {
670 fBackendType = backendType;
671
672 fWindow->detach();
673
Brian Osman70d2f432017-11-08 09:54:10 -0500674#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400675 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
676 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500677 DisplayParams params = fWindow->getRequestedDisplayParams();
678 delete fWindow;
679 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500680
Brian Osman70d2f432017-11-08 09:54:10 -0500681 // re-register callbacks
682 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500683 fWindow->pushLayer(this);
Brian Osman56a24812017-12-19 11:15:16 -0500684 fWindow->pushLayer(&fStatsLayer);
Brian Osmand67e5182017-12-08 16:46:09 -0500685 fWindow->pushLayer(&fImGuiLayer);
686
Brian Osman70d2f432017-11-08 09:54:10 -0500687 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
688 // will still include our correct sample count. But the re-created fWindow will lose that
689 // information. On Windows, we need to re-create the window when changing sample count,
690 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
691 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
692 // as if we had never changed windows at all).
693 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500694#endif
695
Brian Osman70d2f432017-11-08 09:54:10 -0500696 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500697}
698
Brian Osman92004802017-03-06 11:47:26 -0500699void Viewer::setColorMode(ColorMode colorMode) {
700 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700701
Brian Osmanf750fbc2017-02-08 10:47:28 -0500702 // 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 -0400703 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700704 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500705 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400706 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
707 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500708 if (wasInLegacy != wantLegacy) {
709 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700710 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500711 }
712
713 this->updateTitle();
714 fWindow->inval();
715}
716
717void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500718 SkAutoCanvasRestore autorestore(canvas, false);
719
Brian Osmanf750fbc2017-02-08 10:47:28 -0500720 // By default, we render directly into the window's surface/canvas
721 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500722 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700723
Brian Osmane0d4fba2017-03-15 10:24:55 -0400724 // If we're in any of the color managed modes, construct the color space we're going to use
725 sk_sp<SkColorSpace> cs = nullptr;
726 if (ColorMode::kLegacy != fColorMode) {
727 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
728 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400729 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400730 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500731 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
732 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
733 } else {
734 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
735 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400736 }
737
Brian Osman3ac99cf2017-12-01 11:23:53 -0500738 if (fSaveToSKP) {
739 SkPictureRecorder recorder;
740 SkCanvas* recorderCanvas = recorder.beginRecording(
741 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
742 // In xform-canvas mode, record the transformed output
743 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
744 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
745 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
746 recorderCanvas = xformCanvas.get();
747 }
748 fSlides[fCurrentSlide]->draw(recorderCanvas);
749 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
750 SkFILEWStream stream("sample_app.skp");
751 picture->serialize(&stream);
752 fSaveToSKP = false;
753 }
754
Brian Osmane0d4fba2017-03-15 10:24:55 -0400755 // 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 -0500756 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
757 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500758 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -0500759 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
760 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -0500761 if (Window::kRaster_BackendType == fBackendType ||
762 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500763 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400764 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
765 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
766
Brian Osman92004802017-03-06 11:47:26 -0500767 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
768 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400769 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
770 // with a special canvas (below) that has the color space attached
771 sk_sp<SkColorSpace> offscreenColorSpace =
772 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500773 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400774 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500775 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
776 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -0500777 SkPixmap offscreenPixmap;
778 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
779 SkBitmap offscreenBitmap;
780 offscreenBitmap.installPixels(offscreenPixmap);
781 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
782 fThreadCnt, fExecutor.get()));
783 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
784 slideCanvas = threadedCanvas.get();
785 } else {
786 slideCanvas = offscreenSurface->getCanvas();
787 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500788 }
789
Brian Osmane0d4fba2017-03-15 10:24:55 -0400790 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
791 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
792 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
793 slideCanvas = xformCanvas.get();
794 }
795
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500796 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500797 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500798 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500799 // Time the painting logic of the slide
Brian Osman56a24812017-12-19 11:15:16 -0500800 fStatsLayer.beginTiming(fPaintTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500801 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osman56a24812017-12-19 11:15:16 -0500802 fStatsLayer.endTiming(fPaintTimer);
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500803 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500804
805 // Force a flush so we can time that, too
Brian Osman56a24812017-12-19 11:15:16 -0500806 fStatsLayer.beginTiming(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -0500807 slideCanvas->flush();
Brian Osman56a24812017-12-19 11:15:16 -0500808 fStatsLayer.endTiming(fFlushTimer);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500809
810 // If we rendered offscreen, snap an image and push the results to the window's canvas
811 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500812 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500813
814 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400815 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500816 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400817 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500818 SkPaint paint;
819 paint.setBlendMode(SkBlendMode::kSrc);
820 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -0700821 }
liyuqian6f163d22016-06-13 12:26:45 -0700822}
823
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700824void Viewer::onBackendCreated() {
Florin Malitaab99c342018-01-16 16:23:03 -0500825 this->setupCurrentSlide();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700826 fWindow->show();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700827}
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
brianosman622c8d52016-05-10 06:50:49 -0700832 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -0700833
Brian Osmand67e5182017-12-08 16:46:09 -0500834 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -0500835
Brian Osman1df161a2017-02-09 12:10:20 -0500836 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -0400837 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700838}
839
jvanverth814e38d2016-06-06 08:48:47 -0700840bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400841 if (GestureDevice::kMouse == fGestureDevice) {
842 return false;
843 }
liyuqiand3cdbca2016-05-17 12:44:20 -0700844 void* castedOwner = reinterpret_cast<void*>(owner);
845 switch (state) {
846 case Window::kUp_InputState: {
847 fGesture.touchEnd(castedOwner);
848 break;
849 }
850 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400851 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700852 break;
853 }
854 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400855 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700856 break;
857 }
858 }
Brian Osmanb53f48c2017-06-07 10:00:30 -0400859 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -0700860 fWindow->inval();
861 return true;
862}
863
Brian Osman80fc07e2017-12-08 16:45:43 -0500864bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osman16c81a12017-12-20 11:58:34 -0500865 if (GestureDevice::kTouch == fGestureDevice) {
866 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -0500867 }
Brian Osman16c81a12017-12-20 11:58:34 -0500868
Florin Malita8deb2952018-02-01 14:16:54 -0500869 const auto slideMatrix = this->computeMatrix();
870 SkMatrix slideInvMatrix;
871 if (slideMatrix.invert(&slideInvMatrix)) {
872 SkPoint slideMouse = SkPoint::Make(x, y);
873 slideInvMatrix.mapPoints(&slideMouse, 1);
874 if (fSlides[fCurrentSlide]->onMouse(slideMouse.x(), slideMouse.y(), state, modifiers)) {
875 fWindow->inval();
876 return true;
877 }
Brian Osman16c81a12017-12-20 11:58:34 -0500878 }
879
880 switch (state) {
881 case Window::kUp_InputState: {
882 fGesture.touchEnd(nullptr);
883 break;
884 }
885 case Window::kDown_InputState: {
886 fGesture.touchBegin(nullptr, x, y);
887 break;
888 }
889 case Window::kMove_InputState: {
890 fGesture.touchMoved(nullptr, x, y);
891 break;
892 }
893 }
894 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
895
896 if (state != Window::kMove_InputState || fGesture.isBeingTouched()) {
897 fWindow->inval();
898 }
Jim Van Verthe7705782017-05-04 14:00:59 -0400899 return true;
900}
901
Brian Osmana109e392017-02-24 09:49:14 -0500902static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
903 const ImVec2& pos, const ImVec2& size) {
904 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
905 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
906
907 // Invisible 10x10 button
908 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
909 ImGui::InvisibleButton(label, ImVec2(10, 10));
910
911 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
912 ImGuiIO& io = ImGui::GetIO();
913 // Normalized mouse position, relative to our gamut box
914 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
915 // Clamp to edge of box, convert back to primary scale
916 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
917 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
918 }
919
920 if (ImGui::IsItemHovered()) {
921 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
922 }
923
924 // Return screen coordinates for the caller. We could just return center here, but we'd have
925 // one frame of lag during drag.
926 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
927}
928
929static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
930 ImDrawList* drawList = ImGui::GetWindowDrawList();
931
932 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
933 // width, and scale the height to maintain aspect ratio.
934 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
935 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
936 ImVec2 pos = ImGui::GetCursorScreenPos();
937
938 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
939 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
940 // Magic numbers are pixel locations of the origin and upper-right corner.
941 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
942 ImVec2(242, 61), ImVec2(1897, 1922));
943 ImVec2 endPos = ImGui::GetCursorPos();
944
945 // Primary markers
946 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
947 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
948 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
949 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
950
951 // Gamut triangle
952 drawList->AddCircle(r, 5.0f, 0xFF000040);
953 drawList->AddCircle(g, 5.0f, 0xFF004000);
954 drawList->AddCircle(b, 5.0f, 0xFF400000);
955 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
956 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
957
958 // Re-position cursor immediate after the diagram for subsequent controls
959 ImGui::SetCursorPos(endPos);
960}
961
Brian Osmand67e5182017-12-08 16:46:09 -0500962void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -0500963 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
964 if (fShowImGuiTestWindow) {
965 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
966 }
967
968 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -0500969 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
970 // always visible, we can end up in a layout feedback loop.
971 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -0500972 DisplayParams params = fWindow->getRequestedDisplayParams();
973 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -0500974 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
975 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -0500976 if (ImGui::CollapsingHeader("Backend")) {
977 int newBackend = static_cast<int>(fBackendType);
978 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
979 ImGui::SameLine();
980 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -0400981#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
982 ImGui::SameLine();
983 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
984#endif
Brian Osman621491e2017-02-28 15:45:01 -0500985#if defined(SK_VULKAN)
986 ImGui::SameLine();
987 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
988#endif
989 if (newBackend != fBackendType) {
990 fDeferredActions.push_back([=]() {
991 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
992 });
993 }
Brian Osman8a9de3d2017-03-01 14:59:05 -0500994
Brian Salomon99a33902017-03-07 15:16:34 -0500995 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400996 bool* wire = &params.fGrContextOptions.fWireframeMode;
997 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
998 paramsChanged = true;
999 }
Brian Salomon99a33902017-03-07 15:16:34 -05001000
Brian Osman28b12522017-03-08 17:10:24 -05001001 if (ctx) {
1002 int sampleCount = fWindow->sampleCount();
1003 ImGui::Text("MSAA: "); ImGui::SameLine();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001004 ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine();
Brian Osman28b12522017-03-08 17:10:24 -05001005 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1006 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1007 ImGui::RadioButton("16", &sampleCount, 16);
1008
1009 if (sampleCount != params.fMSAASampleCount) {
1010 params.fMSAASampleCount = sampleCount;
1011 paramsChanged = true;
1012 }
1013 }
1014
Brian Osman8a9de3d2017-03-01 14:59:05 -05001015 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001016 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001017 auto prButton = [&](GpuPathRenderers x) {
1018 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001019 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1020 params.fGrContextOptions.fGpuPathRenderers = x;
1021 paramsChanged = true;
1022 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001023 }
1024 };
1025
1026 if (!ctx) {
1027 ImGui::RadioButton("Software", true);
Brian Salomonbdecacf2018-02-02 20:32:49 -05001028 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001029 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001030 prButton(GpuPathRenderers::kAll);
1031 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1032 prButton(GpuPathRenderers::kStencilAndCover);
1033 }
1034 if (ctx->caps()->sampleShadingSupport()) {
1035 prButton(GpuPathRenderers::kMSAA);
1036 }
1037 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001038 prButton(GpuPathRenderers::kNone);
1039 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001040 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001041 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001042 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1043 prButton(GpuPathRenderers::kCoverageCounting);
1044 }
Jim Van Verth83010462017-03-16 08:45:39 -04001045 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001046 prButton(GpuPathRenderers::kTessellating);
1047 prButton(GpuPathRenderers::kNone);
1048 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001049 ImGui::TreePop();
1050 }
Brian Osman621491e2017-02-28 15:45:01 -05001051 }
1052
Brian Osmanfce09c52017-11-14 15:32:20 -05001053 if (fShowSlidePicker) {
1054 ImGui::SetNextTreeNodeOpen(true);
1055 }
1056
Brian Osman79086b92017-02-10 13:36:16 -05001057 if (ImGui::CollapsingHeader("Slide")) {
1058 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001059 static ImVector<const char*> filteredSlideNames;
1060 static ImVector<int> filteredSlideIndices;
1061
Brian Osmanfce09c52017-11-14 15:32:20 -05001062 if (fShowSlidePicker) {
1063 ImGui::SetKeyboardFocusHere();
1064 fShowSlidePicker = false;
1065 }
1066
Brian Osman79086b92017-02-10 13:36:16 -05001067 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001068 filteredSlideNames.clear();
1069 filteredSlideIndices.clear();
1070 int filteredIndex = 0;
1071 for (int i = 0; i < fSlides.count(); ++i) {
1072 const char* slideName = fSlides[i]->getName().c_str();
1073 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1074 if (i == fCurrentSlide) {
1075 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001076 }
Brian Osmanf479e422017-11-08 13:11:36 -05001077 filteredSlideNames.push_back(slideName);
1078 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001079 }
Brian Osman79086b92017-02-10 13:36:16 -05001080 }
Brian Osmanf479e422017-11-08 13:11:36 -05001081
Brian Osmanf479e422017-11-08 13:11:36 -05001082 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1083 filteredSlideNames.size(), 20)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001084 this->setCurrentSlide(filteredSlideIndices[filteredIndex]);
Brian Osman79086b92017-02-10 13:36:16 -05001085 }
1086 }
Brian Osmana109e392017-02-24 09:49:14 -05001087
1088 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001089 ColorMode newMode = fColorMode;
1090 auto cmButton = [&](ColorMode mode, const char* label) {
1091 if (ImGui::RadioButton(label, mode == fColorMode)) {
1092 newMode = mode;
1093 }
1094 };
1095
1096 cmButton(ColorMode::kLegacy, "Legacy 8888");
1097 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1098 "Color Managed 8888 (Nonlinear blending)");
1099 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1100 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1101
1102 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001103 // It isn't safe to switch color mode now (in the middle of painting). We might
1104 // tear down the back-end, etc... Defer this change until the next onIdle.
1105 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001106 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001107 });
Brian Osmana109e392017-02-24 09:49:14 -05001108 }
1109
1110 // Pick from common gamuts:
1111 int primariesIdx = 4; // Default: Custom
1112 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1113 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1114 primariesIdx = i;
1115 break;
1116 }
1117 }
1118
Brian Osmanfdab5762017-11-09 10:27:55 -05001119 // When we're in xform canvas mode, we can alter the transfer function, too
1120 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1121 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1122 }
1123
Brian Osmana109e392017-02-24 09:49:14 -05001124 if (ImGui::Combo("Primaries", &primariesIdx,
1125 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1126 if (primariesIdx >= 0 && primariesIdx <= 3) {
1127 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1128 }
1129 }
1130
1131 // Allow direct editing of gamut
1132 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1133 }
Brian Osman79086b92017-02-10 13:36:16 -05001134 }
Brian Salomon99a33902017-03-07 15:16:34 -05001135 if (paramsChanged) {
1136 fDeferredActions.push_back([=]() {
1137 fWindow->setRequestedDisplayParams(params);
1138 fWindow->inval();
1139 this->updateTitle();
1140 });
1141 }
Brian Osman79086b92017-02-10 13:36:16 -05001142 ImGui::End();
1143 }
1144
Brian Osmanf6877092017-02-13 09:39:57 -05001145 if (fShowZoomWindow && fLastImage) {
1146 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001147 static int zoomFactor = 8;
1148 if (ImGui::Button("<<")) {
1149 zoomFactor = SkTMax(zoomFactor / 2, 4);
1150 }
1151 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1152 if (ImGui::Button(">>")) {
1153 zoomFactor = SkTMin(zoomFactor * 2, 32);
1154 }
Brian Osmanf6877092017-02-13 09:39:57 -05001155
Brian Osmanf6877092017-02-13 09:39:57 -05001156 ImVec2 mousePos = ImGui::GetMousePos();
1157 ImVec2 avail = ImGui::GetContentRegionAvail();
1158
Brian Osmanead517d2017-11-13 15:36:36 -05001159 uint32_t pixel = 0;
1160 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1161 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1162 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001163 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1164 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1165 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001166 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1167 }
1168
Brian Osmand67e5182017-12-08 16:46:09 -05001169 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001170 // Translate so the region of the image that's under the mouse cursor is centered
1171 // in the zoom canvas:
1172 c->scale(zoomFactor, zoomFactor);
1173 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1174 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1175 c->drawImage(this->fLastImage, 0, 0);
1176
1177 SkPaint outline;
1178 outline.setStyle(SkPaint::kStroke_Style);
1179 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1180 });
Brian Osmanf6877092017-02-13 09:39:57 -05001181 }
1182
1183 ImGui::End();
1184 }
Brian Osman79086b92017-02-10 13:36:16 -05001185}
1186
liyuqian2edb0f42016-07-06 14:11:32 -07001187void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001188 for (int i = 0; i < fDeferredActions.count(); ++i) {
1189 fDeferredActions[i]();
1190 }
1191 fDeferredActions.reset();
1192
Brian Osman56a24812017-12-19 11:15:16 -05001193 fStatsLayer.beginTiming(fAnimateTimer);
jvanverthc265a922016-04-08 12:51:45 -07001194 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001195 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
Brian Osman56a24812017-12-19 11:15:16 -05001196 fStatsLayer.endTiming(fAnimateTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001197
Brian Osman79086b92017-02-10 13:36:16 -05001198 ImGuiIO& io = ImGui::GetIO();
Brian Osman56a24812017-12-19 11:15:16 -05001199 if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001200 fWindow->inval();
1201 }
jvanverth9f372462016-04-06 06:08:59 -07001202}
liyuqiane5a6cd92016-05-27 08:52:52 -07001203
1204void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001205 if (!fWindow) {
1206 return;
1207 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05001208 if (fWindow->sampleCount() < 1) {
csmartdalton578f0642017-02-24 16:04:47 -07001209 return; // Surface hasn't been created yet.
1210 }
1211
liyuqianb73c24b2016-06-03 08:47:23 -07001212 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001213 Json::Value slideState(Json::objectValue);
1214 slideState[kName] = kSlideStateName;
1215 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001216 if (fAllSlideNames.size() == 0) {
1217 for(auto slide : fSlides) {
1218 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1219 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001220 }
liyuqian1f508fd2016-06-07 06:57:40 -07001221 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001222
liyuqianb73c24b2016-06-03 08:47:23 -07001223 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001224 Json::Value backendState(Json::objectValue);
1225 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001226 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001227 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001228 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001229 backendState[kOptions].append(Json::Value(str));
1230 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001231
csmartdalton578f0642017-02-24 16:04:47 -07001232 // MSAA state
1233 Json::Value msaaState(Json::objectValue);
1234 msaaState[kName] = kMSAAStateName;
1235 msaaState[kValue] = fWindow->sampleCount();
1236 msaaState[kOptions] = Json::Value(Json::arrayValue);
1237 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1238 msaaState[kOptions].append(Json::Value(0));
1239 } else {
1240 for (int msaa : {0, 4, 8, 16}) {
1241 msaaState[kOptions].append(Json::Value(msaa));
1242 }
1243 }
1244
csmartdalton61cd31a2017-02-27 17:00:53 -07001245 // Path renderer state
1246 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1247 Json::Value prState(Json::objectValue);
1248 prState[kName] = kPathRendererStateName;
1249 prState[kValue] = gPathRendererNames[pr];
1250 prState[kOptions] = Json::Value(Json::arrayValue);
1251 const GrContext* ctx = fWindow->getGrContext();
1252 if (!ctx) {
1253 prState[kOptions].append("Software");
Brian Salomonbdecacf2018-02-02 20:32:49 -05001254 } else if (fWindow->sampleCount() > 1) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001255 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001256 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1257 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1258 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1259 }
1260 if (ctx->caps()->sampleShadingSupport()) {
1261 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1262 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001263 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001264 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1265 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001266 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001267 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001268 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1269 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1270 }
Jim Van Verth83010462017-03-16 08:45:39 -04001271 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001272 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001273 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1274 }
1275
liyuqianb73c24b2016-06-03 08:47:23 -07001276 // Softkey state
1277 Json::Value softkeyState(Json::objectValue);
1278 softkeyState[kName] = kSoftkeyStateName;
1279 softkeyState[kValue] = kSoftkeyHint;
1280 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1281 softkeyState[kOptions].append(kSoftkeyHint);
1282 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1283 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1284 }
1285
liyuqian1f508fd2016-06-07 06:57:40 -07001286 // FPS state
1287 Json::Value fpsState(Json::objectValue);
1288 fpsState[kName] = kFpsStateName;
Brian Osman56a24812017-12-19 11:15:16 -05001289 double animTime = fStatsLayer.getLastTime(fAnimateTimer);
1290 double paintTime = fStatsLayer.getLastTime(fPaintTimer);
1291 double flushTime = fStatsLayer.getLastTime(fFlushTimer);
Brian Osman1df161a2017-02-09 12:10:20 -05001292 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
Brian Osman56a24812017-12-19 11:15:16 -05001293 animTime + paintTime + flushTime,
1294 animTime, paintTime, flushTime).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001295 fpsState[kOptions] = Json::Value(Json::arrayValue);
1296
liyuqiane5a6cd92016-05-27 08:52:52 -07001297 Json::Value state(Json::arrayValue);
1298 state.append(slideState);
1299 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001300 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001301 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001302 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001303 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001304
Brian Osmaneff04b52017-11-21 13:18:02 -05001305 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001306}
1307
1308void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001309 // For those who will add more features to handle the state change in this function:
1310 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1311 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1312 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001313 if (stateName.equals(kSlideStateName)) {
Florin Malitaab99c342018-01-16 16:23:03 -05001314 for (int i = 0; i < fSlides.count(); ++i) {
1315 if (fSlides[i]->getName().equals(stateValue)) {
1316 this->setCurrentSlide(i);
1317 return;
liyuqiane5a6cd92016-05-27 08:52:52 -07001318 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001319 }
Florin Malitaab99c342018-01-16 16:23:03 -05001320
1321 SkDebugf("Slide not found: %s", stateValue.c_str());
liyuqian6cb70252016-06-02 12:16:25 -07001322 } else if (stateName.equals(kBackendStateName)) {
1323 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1324 if (stateValue.equals(kBackendTypeStrings[i])) {
1325 if (fBackendType != i) {
1326 fBackendType = (sk_app::Window::BackendType)i;
1327 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001328 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001329 }
1330 break;
1331 }
1332 }
csmartdalton578f0642017-02-24 16:04:47 -07001333 } else if (stateName.equals(kMSAAStateName)) {
1334 DisplayParams params = fWindow->getRequestedDisplayParams();
1335 int sampleCount = atoi(stateValue.c_str());
1336 if (sampleCount != params.fMSAASampleCount) {
1337 params.fMSAASampleCount = sampleCount;
1338 fWindow->setRequestedDisplayParams(params);
1339 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001340 this->updateTitle();
1341 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001342 }
1343 } else if (stateName.equals(kPathRendererStateName)) {
1344 DisplayParams params = fWindow->getRequestedDisplayParams();
1345 for (const auto& pair : gPathRendererNames) {
1346 if (pair.second == stateValue.c_str()) {
1347 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1348 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1349 fWindow->setRequestedDisplayParams(params);
1350 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001351 this->updateTitle();
1352 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001353 }
1354 break;
1355 }
csmartdalton578f0642017-02-24 16:04:47 -07001356 }
liyuqianb73c24b2016-06-03 08:47:23 -07001357 } else if (stateName.equals(kSoftkeyStateName)) {
1358 if (!stateValue.equals(kSoftkeyHint)) {
1359 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001360 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001361 }
liyuqian2edb0f42016-07-06 14:11:32 -07001362 } else if (stateName.equals(kRefreshStateName)) {
1363 // This state is actually NOT in the UI state.
1364 // We use this to allow Android to quickly set bool fRefresh.
1365 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001366 } else {
1367 SkDebugf("Unknown stateName: %s", stateName.c_str());
1368 }
1369}
Brian Osman79086b92017-02-10 13:36:16 -05001370
1371bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001372 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001373}
1374
1375bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001376 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001377 fWindow->inval();
1378 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001379 } else {
1380 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001381 }
Brian Osman79086b92017-02-10 13:36:16 -05001382}