blob: a1b7baac6bfa6db2eacbd9ec91d424749db3ba59 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
jvanverth34524262016-05-04 13:49:13 -07008#include "Viewer.h"
jvanverth9f372462016-04-06 06:08:59 -07009
jvanverth2bb3b6d2016-04-08 07:24:09 -070010#include "GMSlide.h"
liyuqian6f163d22016-06-13 12:26:45 -070011#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050012#include "Resources.h"
jvanverthc7027ab2016-06-16 09:52:35 -070013#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070014#include "SKPSlide.h"
jvanverth9f372462016-04-06 06:08:59 -070015
csmartdalton61cd31a2017-02-27 17:00:53 -070016#include "GrContext.h"
Greg Daniel285db442016-10-14 09:12:53 -040017#include "SkATrace.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070018#include "SkCanvas.h"
Brian Osmana109e392017-02-24 09:49:14 -050019#include "SkColorSpace_Base.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050020#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040021#include "SkColorSpaceXformCanvas.h"
Brian Osman2dd96932016-10-18 15:33:53 -040022#include "SkCommandLineFlags.h"
Brian Osmanf9810662017-08-30 10:02:10 -040023#include "SkCommonFlagsGpuThreads.h"
csmartdalton008b9d82017-02-22 12:00:42 -070024#include "SkCommonFlagsPathRenderer.h"
liyuqian74959a12016-06-16 14:10:34 -070025#include "SkDashPathEffect.h"
Brian Osman53136aa2017-07-20 15:43:35 -040026#include "SkEventTracingPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040027#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050028#include "SkImagePriv.h"
liyuqian6f163d22016-06-13 12:26:45 -070029#include "SkMetaData.h"
csmartdalton61cd31a2017-02-27 17:00:53 -070030#include "SkOnce.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070031#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050032#include "SkOSPath.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050033#include "SkPictureRecorder.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070034#include "SkRandom.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040035#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070036#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070037#include "SkSurface.h"
Brian Osman79086b92017-02-10 13:36:16 -050038#include "SkSwizzle.h"
csmartdalton29d87152017-02-10 17:05:14 -050039#include "SkTaskGroup.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050040#include "SkThreadedBMPDevice.h"
liyuqian2edb0f42016-07-06 14:11:32 -070041#include "SkTime.h"
Mike Reed887cdf12017-04-03 11:11:09 -040042#include "SkVertices.h"
jvanverth9f372462016-04-06 06:08:59 -070043
Brian Osman79086b92017-02-10 13:36:16 -050044#include "imgui.h"
45
Chris Dalton1a325d22017-07-14 15:17:41 -060046#include "ccpr/GrCoverageCountingPathRenderer.h"
47
csmartdalton578f0642017-02-24 16:04:47 -070048#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070049#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070050
jvanverth34524262016-05-04 13:49:13 -070051using namespace sk_app;
52
csmartdalton61cd31a2017-02-27 17:00:53 -070053static std::map<GpuPathRenderers, std::string> gPathRendererNames;
54
jvanverth9f372462016-04-06 06:08:59 -070055Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070056 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070057}
58
Brian Osman2dd96932016-10-18 15:33:53 -040059static DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
Brian Osman16adfa32016-10-18 14:42:44 -040060
Brian Osman2dd96932016-10-18 15:33:53 -040061static DEFINE_string2(match, m, nullptr,
jvanverth2bb3b6d2016-04-08 07:24:09 -070062 "[~][^]substring[$] [...] of bench name to run.\n"
63 "Multiple matches may be separated by spaces.\n"
64 "~ causes a matching bench to always be skipped\n"
65 "^ requires the start of the bench to match\n"
66 "$ requires the end of the bench to match\n"
67 "^ and $ requires an exact match\n"
68 "If a bench does not match any list entry,\n"
69 "it is skipped unless some list entry starts with ~");
bsalomon6c471f72016-07-26 12:56:32 -070070
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060071static DEFINE_string(slide, "", "Start on this sample.");
72static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050073
bsalomon6c471f72016-07-26 12:56:32 -070074#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070075# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070076#else
77# define BACKENDS_STR "\"sw\" and \"gl\""
78#endif
79
liyuqian71491dc2016-06-09 12:02:34 -070080#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -040081static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
82static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070083#else
Brian Osman2dd96932016-10-18 15:33:53 -040084static DEFINE_string(skps, "skps", "Directory to read skps from.");
85static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070086#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -070087
Brian Osman2dd96932016-10-18 15:33:53 -040088static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070089
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060090static DEFINE_int32(msaa, 0, "Number of subpixel samples. 0 for no HW antialiasing.");
91static DEFINE_bool(cachePathMasks, true, "Allows path mask textures to be cached in GPU configs.");
csmartdalton008b9d82017-02-22 12:00:42 -070092DEFINE_pathrenderer_flag;
93
Brian Osman53136aa2017-07-20 15:43:35 -040094DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050095
Brian Salomon194db172017-08-17 14:37:06 -040096const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070097 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040098#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
99 "ANGLE",
100#endif
jvanverth063ece72016-06-17 09:29:14 -0700101#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700102 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700103#endif
csmartdalton578f0642017-02-24 16:04:47 -0700104 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700105};
106
bsalomon6c471f72016-07-26 12:56:32 -0700107static sk_app::Window::BackendType get_backend_type(const char* str) {
108#ifdef SK_VULKAN
109 if (0 == strcmp(str, "vk")) {
110 return sk_app::Window::kVulkan_BackendType;
111 } else
112#endif
Brian Salomon194db172017-08-17 14:37:06 -0400113#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
114 if (0 == strcmp(str, "angle")) {
115 return sk_app::Window::kANGLE_BackendType;
116 } else
117#endif
bsalomon6c471f72016-07-26 12:56:32 -0700118 if (0 == strcmp(str, "gl")) {
119 return sk_app::Window::kNativeGL_BackendType;
120 } else if (0 == strcmp(str, "sw")) {
121 return sk_app::Window::kRaster_BackendType;
122 } else {
123 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
124 return sk_app::Window::kRaster_BackendType;
125 }
126}
127
Brian Osmana109e392017-02-24 09:49:14 -0500128static SkColorSpacePrimaries gSrgbPrimaries = {
129 0.64f, 0.33f,
130 0.30f, 0.60f,
131 0.15f, 0.06f,
132 0.3127f, 0.3290f };
133
134static SkColorSpacePrimaries gAdobePrimaries = {
135 0.64f, 0.33f,
136 0.21f, 0.71f,
137 0.15f, 0.06f,
138 0.3127f, 0.3290f };
139
140static SkColorSpacePrimaries gP3Primaries = {
141 0.680f, 0.320f,
142 0.265f, 0.690f,
143 0.150f, 0.060f,
144 0.3127f, 0.3290f };
145
146static SkColorSpacePrimaries gRec2020Primaries = {
147 0.708f, 0.292f,
148 0.170f, 0.797f,
149 0.131f, 0.046f,
150 0.3127f, 0.3290f };
151
152struct NamedPrimaries {
153 const char* fName;
154 SkColorSpacePrimaries* fPrimaries;
155} gNamedPrimaries[] = {
156 { "sRGB", &gSrgbPrimaries },
157 { "AdobeRGB", &gAdobePrimaries },
158 { "P3", &gP3Primaries },
159 { "Rec. 2020", &gRec2020Primaries },
160};
161
162static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
163 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
164}
165
Brian Osman70d2f432017-11-08 09:54:10 -0500166static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
167 // In raster mode, we still use GL for the window.
168 // This lets us render the GUI faster (and correct).
169 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
170}
171
liyuqiane5a6cd92016-05-27 08:52:52 -0700172const char* kName = "name";
173const char* kValue = "value";
174const char* kOptions = "options";
175const char* kSlideStateName = "Slide";
176const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700177const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700178const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700179const char* kSoftkeyStateName = "Softkey";
180const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700181const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700182const char* kON = "ON";
183const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700184const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700185
jvanverth34524262016-05-04 13:49:13 -0700186Viewer::Viewer(int argc, char** argv, void* platformData)
jvanverthc265a922016-04-08 12:51:45 -0700187 : fCurrentMeasurement(0)
Jim Van Verth90dcce52017-11-03 13:36:07 -0400188 , fCumulativeMeasurementTime(0)
189 , fCumulativeMeasurementCount(0)
jvanverthc265a922016-04-08 12:51:45 -0700190 , fDisplayStats(false)
liyuqian2edb0f42016-07-06 14:11:32 -0700191 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500192 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500193 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500194 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500195 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500196 , fShowZoomWindow(false)
197 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700198 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500199 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500200 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500201 // Our UI can only tweak gamma (currently), so start out gamma-only
202 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700203 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400204 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500205 , fTileCnt(0)
206 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700207{
Greg Daniel285db442016-10-14 09:12:53 -0400208 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700209
210 static SkOnce initPathRendererNames;
211 initPathRendererNames([]() {
Brian Osman8b0f2652017-08-29 15:18:34 -0400212 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
213 gPathRendererNames[GpuPathRenderers::kDefault] =
214 "Default Ganesh Behavior (best path renderer, not including CCPR)";
csmartdalton61cd31a2017-02-27 17:00:53 -0700215 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
216 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
Jim Van Verth83010462017-03-16 08:45:39 -0400217 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Dalton1a325d22017-07-14 15:17:41 -0600218 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
Brian Osman8a9de3d2017-03-01 14:59:05 -0500219 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
csmartdalton61cd31a2017-02-27 17:00:53 -0700220 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
221 });
222
Brian Osman1df161a2017-02-09 12:10:20 -0500223 memset(fPaintTimes, 0, sizeof(fPaintTimes));
224 memset(fFlushTimes, 0, sizeof(fFlushTimes));
225 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
jvanverth9f372462016-04-06 06:08:59 -0700226
jvanverth2bb3b6d2016-04-08 07:24:09 -0700227 SkDebugf("Command line arguments: ");
228 for (int i = 1; i < argc; ++i) {
229 SkDebugf("%s ", argv[i]);
230 }
231 SkDebugf("\n");
232
233 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500234#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400235 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500236#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700237
Brian Osmanbc8150f2017-07-24 11:38:01 -0400238 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400239 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400240
bsalomon6c471f72016-07-26 12:56:32 -0700241 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700242 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700243
csmartdalton578f0642017-02-24 16:04:47 -0700244 DisplayParams displayParams;
245 displayParams.fMSAASampleCount = FLAGS_msaa;
csmartdalton61cd31a2017-02-27 17:00:53 -0700246 displayParams.fGrContextOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600247 displayParams.fGrContextOptions.fAllowPathMaskCaching = FLAGS_cachePathMasks;
Brian Osmanf9810662017-08-30 10:02:10 -0400248 displayParams.fGrContextOptions.fExecutor = GpuExecutorForTools();
csmartdalton578f0642017-02-24 16:04:47 -0700249 fWindow->setRequestedDisplayParams(displayParams);
250
jvanverth9f372462016-04-06 06:08:59 -0700251 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700252 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500253 fWindow->pushLayer(this);
jvanverth9f372462016-04-06 06:08:59 -0700254
brianosman622c8d52016-05-10 06:50:49 -0700255 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500256 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
257 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
258 fWindow->inval();
259 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500260 // Command to jump directly to the slide picker and give it focus
261 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
262 this->fShowImGuiDebugWindow = true;
263 this->fShowSlidePicker = true;
264 fWindow->inval();
265 });
266 // Alias that to Backspace, to match SampleApp
267 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
268 this->fShowImGuiDebugWindow = true;
269 this->fShowSlidePicker = true;
270 fWindow->inval();
271 });
Brian Osman79086b92017-02-10 13:36:16 -0500272 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
273 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
274 fWindow->inval();
275 });
Brian Osmanf6877092017-02-13 09:39:57 -0500276 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
277 this->fShowZoomWindow = !this->fShowZoomWindow;
278 fWindow->inval();
279 });
brianosman622c8d52016-05-10 06:50:49 -0700280 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
281 this->fDisplayStats = !this->fDisplayStats;
282 fWindow->inval();
283 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400284 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
285 this->resetMeasurements();
286 this->updateTitle();
287 fWindow->inval();
288 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500289 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500290 switch (fColorMode) {
291 case ColorMode::kLegacy:
292 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
293 break;
294 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
295 this->setColorMode(ColorMode::kColorManagedSRGB8888);
296 break;
297 case ColorMode::kColorManagedSRGB8888:
298 this->setColorMode(ColorMode::kColorManagedLinearF16);
299 break;
300 case ColorMode::kColorManagedLinearF16:
301 this->setColorMode(ColorMode::kLegacy);
302 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500303 }
brianosman622c8d52016-05-10 06:50:49 -0700304 });
305 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
306 int previousSlide = fCurrentSlide;
307 fCurrentSlide++;
308 if (fCurrentSlide >= fSlides.count()) {
309 fCurrentSlide = 0;
310 }
311 this->setupCurrentSlide(previousSlide);
312 });
313 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
314 int previousSlide = fCurrentSlide;
315 fCurrentSlide--;
316 if (fCurrentSlide < 0) {
317 fCurrentSlide = fSlides.count() - 1;
318 }
319 this->setupCurrentSlide(previousSlide);
320 });
321 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
322 this->changeZoomLevel(1.f / 32.f);
323 fWindow->inval();
324 });
325 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
326 this->changeZoomLevel(-1.f / 32.f);
327 fWindow->inval();
328 });
jvanverthaf236b52016-05-20 06:01:06 -0700329 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400330 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
331 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500332 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400333#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
334 if (newBackend == sk_app::Window::kVulkan_BackendType) {
335 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
336 sk_app::Window::kBackendTypeCount);
337 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
338 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500339 }
340#endif
Brian Osman621491e2017-02-28 15:45:01 -0500341 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700342 });
brianosman622c8d52016-05-10 06:50:49 -0700343
Yuqian Lib2ba6642017-11-22 12:07:41 -0500344 fCommands.addCommand('A', "AA", "Toggle analytic AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400345 if (!gSkUseAnalyticAA) {
346 gSkUseAnalyticAA = true;
347 } else if (!gSkForceAnalyticAA) {
348 gSkForceAnalyticAA = true;
349 } else {
350 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
351 }
352 this->updateTitle();
353 fWindow->inval();
354 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500355 fCommands.addCommand('D', "AA", "Toggle delta AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400356 if (!gSkUseDeltaAA) {
357 gSkUseDeltaAA = true;
358 } else if (!gSkForceDeltaAA) {
359 gSkForceDeltaAA = true;
360 } else {
361 gSkUseDeltaAA = gSkForceDeltaAA = false;
362 }
363 this->updateTitle();
364 fWindow->inval();
365 });
366
Yuqian Lib2ba6642017-11-22 12:07:41 -0500367 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
368 fTileCnt++;
369 if (fThreadCnt == 0) {
370 this->resetExecutor();
371 }
372 this->updateTitle();
373 fWindow->inval();
374 });
375 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
376 fTileCnt = SkTMax(0, fTileCnt - 1);
377 if (fThreadCnt == 0) {
378 this->resetExecutor();
379 }
380 this->updateTitle();
381 fWindow->inval();
382 });
383 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
384 if (fTileCnt == 0) {
385 return;
386 }
387 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
388 this->resetExecutor();
389 this->updateTitle();
390 fWindow->inval();
391 });
392 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
393 if (fTileCnt == 0) {
394 return;
395 }
396 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
397 this->resetExecutor();
398 this->updateTitle();
399 fWindow->inval();
400 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500401 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
402 fSaveToSKP = true;
403 fWindow->inval();
404 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500405
jvanverth2bb3b6d2016-04-08 07:24:09 -0700406 // set up slides
407 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500408 this->setStartupSlide();
409 if (FLAGS_list) {
410 this->listNames();
411 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700412
djsollen12d62a72016-04-21 07:59:44 -0700413 fAnimTimer.run();
414
Brian Osman79086b92017-02-10 13:36:16 -0500415 // ImGui initialization:
416 ImGuiIO& io = ImGui::GetIO();
417 io.DisplaySize.x = static_cast<float>(fWindow->width());
418 io.DisplaySize.y = static_cast<float>(fWindow->height());
419
420 // Keymap...
421 io.KeyMap[ImGuiKey_Tab] = (int)Window::Key::kTab;
422 io.KeyMap[ImGuiKey_LeftArrow] = (int)Window::Key::kLeft;
423 io.KeyMap[ImGuiKey_RightArrow] = (int)Window::Key::kRight;
424 io.KeyMap[ImGuiKey_UpArrow] = (int)Window::Key::kUp;
425 io.KeyMap[ImGuiKey_DownArrow] = (int)Window::Key::kDown;
426 io.KeyMap[ImGuiKey_PageUp] = (int)Window::Key::kPageUp;
427 io.KeyMap[ImGuiKey_PageDown] = (int)Window::Key::kPageDown;
428 io.KeyMap[ImGuiKey_Home] = (int)Window::Key::kHome;
429 io.KeyMap[ImGuiKey_End] = (int)Window::Key::kEnd;
430 io.KeyMap[ImGuiKey_Delete] = (int)Window::Key::kDelete;
431 io.KeyMap[ImGuiKey_Backspace] = (int)Window::Key::kBack;
432 io.KeyMap[ImGuiKey_Enter] = (int)Window::Key::kOK;
433 io.KeyMap[ImGuiKey_Escape] = (int)Window::Key::kEscape;
434 io.KeyMap[ImGuiKey_A] = (int)Window::Key::kA;
435 io.KeyMap[ImGuiKey_C] = (int)Window::Key::kC;
436 io.KeyMap[ImGuiKey_V] = (int)Window::Key::kV;
437 io.KeyMap[ImGuiKey_X] = (int)Window::Key::kX;
438 io.KeyMap[ImGuiKey_Y] = (int)Window::Key::kY;
439 io.KeyMap[ImGuiKey_Z] = (int)Window::Key::kZ;
440
441 int w, h;
442 unsigned char* pixels;
443 io.Fonts->GetTexDataAsAlpha8(&pixels, &w, &h);
444 SkImageInfo info = SkImageInfo::MakeA8(w, h);
445 SkPixmap pmap(info, pixels, info.minRowBytes());
Brian Osmanf6877092017-02-13 09:39:57 -0500446 SkMatrix localMatrix = SkMatrix::MakeScale(1.0f / w, 1.0f / h);
447 auto fontImage = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
Mike Reed0acd7952017-04-28 11:12:19 -0400448 auto fontShader = fontImage->makeShader(&localMatrix);
Brian Osmanf6877092017-02-13 09:39:57 -0500449 fImGuiFontPaint.setShader(fontShader);
450 fImGuiFontPaint.setColor(SK_ColorWHITE);
451 fImGuiFontPaint.setFilterQuality(kLow_SkFilterQuality);
452 io.Fonts->TexID = &fImGuiFontPaint;
Brian Osman79086b92017-02-10 13:36:16 -0500453
Hal Canaryc465d132017-12-08 10:21:31 -0500454 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500455 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400456 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500457 }
458 fImGuiGamutPaint.setColor(SK_ColorWHITE);
459 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
460
Brian Osman70d2f432017-11-08 09:54:10 -0500461 fWindow->attach(backend_type_for_window(fBackendType));
jvanverth9f372462016-04-06 06:08:59 -0700462}
463
jvanverth34524262016-05-04 13:49:13 -0700464void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700465 fAllSlideNames = Json::Value(Json::arrayValue);
466
jvanverth2bb3b6d2016-04-08 07:24:09 -0700467 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
468 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400469 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700470
471 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
472 sk_sp<Slide> slide(new GMSlide(gm.release()));
473 fSlides.push_back(slide);
474 }
475
476 gms = gms->next();
477 }
478
479 // reverse array
480 for (int i = 0; i < fSlides.count()/2; ++i) {
481 sk_sp<Slide> temp = fSlides[i];
482 fSlides[i] = fSlides[fSlides.count() - i - 1];
483 fSlides[fSlides.count() - i - 1] = temp;
484 }
485
jvanverthc7027ab2016-06-16 09:52:35 -0700486 // samples
487 const SkViewRegister* reg = SkViewRegister::Head();
488 while (reg) {
489 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700490 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
491 fSlides.push_back(slide);
492 }
jvanverthc7027ab2016-06-16 09:52:35 -0700493 reg = reg->next();
494 }
495
jvanverth2bb3b6d2016-04-08 07:24:09 -0700496 // SKPs
497 for (int i = 0; i < FLAGS_skps.count(); i++) {
498 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700499 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
500 continue;
501 }
502
jvanverth2bb3b6d2016-04-08 07:24:09 -0700503 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700504 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700505 if (slide) {
506 fSlides.push_back(slide);
507 }
508 } else {
509 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700510 SkString skpName;
511 while (it.next(&skpName)) {
512 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
513 continue;
514 }
515
516 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
517 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700518 if (slide) {
519 fSlides.push_back(slide);
520 }
521 }
522 }
523 }
liyuqian6f163d22016-06-13 12:26:45 -0700524
525 // JPGs
526 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
527 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
528 SkString jpgName;
529 while (it.next(&jpgName)) {
brianosmane1d20072016-07-12 09:07:33 -0700530 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) {
531 continue;
532 }
533
liyuqian6f163d22016-06-13 12:26:45 -0700534 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
535 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
536 if (slide) {
537 fSlides.push_back(slide);
538 }
539 }
540 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700541}
542
543
jvanverth34524262016-05-04 13:49:13 -0700544Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700545 fWindow->detach();
546 delete fWindow;
547}
548
brianosman05de2162016-05-06 13:28:57 -0700549void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700550 if (!fWindow) {
551 return;
552 }
553 if (fWindow->sampleCount() < 0) {
554 return; // Surface hasn't been created yet.
555 }
556
jvanverth34524262016-05-04 13:49:13 -0700557 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700558 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700559
Yuqian Li399b3c22017-08-03 11:08:15 -0400560 if (gSkUseDeltaAA) {
561 if (gSkForceDeltaAA) {
562 title.append(" <FDAA>");
563 } else {
564 title.append(" <DAA>");
565 }
566 } else if (gSkUseAnalyticAA) {
567 if (gSkForceAnalyticAA) {
568 title.append(" <FAAA>");
569 } else {
570 title.append(" <AAA>");
571 }
572 }
573
Yuqian Lib2ba6642017-11-22 12:07:41 -0500574 if (fTileCnt > 0) {
575 title.appendf(" T%d", fTileCnt);
576 if (fThreadCnt > 0) {
577 title.appendf("/%d", fThreadCnt);
578 }
579 }
580
Brian Osman92004802017-03-06 11:47:26 -0500581 switch (fColorMode) {
582 case ColorMode::kLegacy:
583 title.append(" Legacy 8888");
584 break;
585 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
586 title.append(" ColorManaged 8888 (Nonlinear blending)");
587 break;
588 case ColorMode::kColorManagedSRGB8888:
589 title.append(" ColorManaged 8888");
590 break;
591 case ColorMode::kColorManagedLinearF16:
592 title.append(" ColorManaged F16");
593 break;
594 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500595
Brian Osman92004802017-03-06 11:47:26 -0500596 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500597 int curPrimaries = -1;
598 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
599 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
600 curPrimaries = i;
601 break;
602 }
603 }
604 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500605
606 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
607 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
608 }
brianosman05de2162016-05-06 13:28:57 -0700609 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500610
csmartdalton578f0642017-02-24 16:04:47 -0700611 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700612 title.append(kBackendTypeStrings[fBackendType]);
csmartdalton578f0642017-02-24 16:04:47 -0700613 if (int msaa = fWindow->sampleCount()) {
614 title.appendf(" MSAA: %i", msaa);
615 }
616 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700617
618 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400619 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700620 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
621 }
622
brianosman05de2162016-05-06 13:28:57 -0700623 fWindow->setTitle(title.c_str());
624}
625
Jim Van Verth6f449692017-02-14 15:16:46 -0500626void Viewer::setStartupSlide() {
627
628 if (!FLAGS_slide.isEmpty()) {
629 int count = fSlides.count();
630 for (int i = 0; i < count; i++) {
631 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
632 fCurrentSlide = i;
633 return;
634 }
635 }
636
637 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
638 this->listNames();
639 }
640
641 fCurrentSlide = 0;
642}
643
644void Viewer::listNames() {
645 int count = fSlides.count();
646 SkDebugf("All Slides:\n");
647 for (int i = 0; i < count; i++) {
648 SkDebugf(" %s\n", fSlides[i]->getName().c_str());
649 }
650}
651
Jim Van Verth90dcce52017-11-03 13:36:07 -0400652void Viewer::resetMeasurements() {
653 memset(fPaintTimes, 0, sizeof(fPaintTimes));
654 memset(fFlushTimes, 0, sizeof(fFlushTimes));
655 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
656 fCurrentMeasurement = 0;
657 fCumulativeMeasurementTime = 0;
658 fCumulativeMeasurementCount = 0;
659}
660
brianosman05de2162016-05-06 13:28:57 -0700661void Viewer::setupCurrentSlide(int previousSlide) {
liyuqiane5a6cd92016-05-27 08:52:52 -0700662 if (fCurrentSlide == previousSlide) {
663 return; // no change; do nothing
664 }
liyuqian6f163d22016-06-13 12:26:45 -0700665 // prepare dimensions for image slides
jvanverthc7027ab2016-06-16 09:52:35 -0700666 fSlides[fCurrentSlide]->load(SkIntToScalar(fWindow->width()), SkIntToScalar(fWindow->height()));
liyuqian6f163d22016-06-13 12:26:45 -0700667
liyuqiane46e4f02016-05-20 07:32:19 -0700668 fGesture.reset();
669 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700670
Brian Osman42bb6ac2017-06-05 08:46:04 -0400671 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
672 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
673 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
674
675 // Start with a matrix that scales the slide to the available screen space
676 if (fWindow->scaleContentToFit()) {
677 if (windowRect.width() > 0 && windowRect.height() > 0) {
678 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
liyuqiane46e4f02016-05-20 07:32:19 -0700679 }
680 }
681
Brian Osman42bb6ac2017-06-05 08:46:04 -0400682 // Prevent the user from dragging content so far outside the window they can't find it again
683 fGesture.setTransLimit(slideBounds, windowRect, fDefaultMatrix);
liyuqiane46e4f02016-05-20 07:32:19 -0700684
brianosman05de2162016-05-06 13:28:57 -0700685 this->updateTitle();
liyuqiane5a6cd92016-05-27 08:52:52 -0700686 this->updateUIState();
jvanverthc265a922016-04-08 12:51:45 -0700687 if (previousSlide >= 0) {
688 fSlides[previousSlide]->unload();
689 }
Jim Van Verth90dcce52017-11-03 13:36:07 -0400690
691 this->resetMeasurements();
692
jvanverthc265a922016-04-08 12:51:45 -0700693 fWindow->inval();
694}
695
696#define MAX_ZOOM_LEVEL 8
697#define MIN_ZOOM_LEVEL -8
698
jvanverth34524262016-05-04 13:49:13 -0700699void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700700 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400701 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
jvanverthc265a922016-04-08 12:51:45 -0700702}
703
liyuqiand3cdbca2016-05-17 12:44:20 -0700704SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700705 SkMatrix m;
jvanverthc265a922016-04-08 12:51:45 -0700706
Brian Osman42bb6ac2017-06-05 08:46:04 -0400707 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
708 : SK_Scalar1 + fZoomLevel;
709 m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700710 m.preConcat(fGesture.globalM());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400711 m.preConcat(fDefaultMatrix);
712 m.preScale(zoomScale, zoomScale);
jvanverthc265a922016-04-08 12:51:45 -0700713
liyuqiand3cdbca2016-05-17 12:44:20 -0700714 return m;
jvanverthc265a922016-04-08 12:51:45 -0700715}
716
Brian Osman621491e2017-02-28 15:45:01 -0500717void Viewer::setBackend(sk_app::Window::BackendType backendType) {
718 fBackendType = backendType;
719
720 fWindow->detach();
721
Brian Osman70d2f432017-11-08 09:54:10 -0500722#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400723 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
724 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500725 DisplayParams params = fWindow->getRequestedDisplayParams();
726 delete fWindow;
727 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500728
Brian Osman70d2f432017-11-08 09:54:10 -0500729 // re-register callbacks
730 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500731 fWindow->pushLayer(this);
Brian Osman70d2f432017-11-08 09:54:10 -0500732 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
733 // will still include our correct sample count. But the re-created fWindow will lose that
734 // information. On Windows, we need to re-create the window when changing sample count,
735 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
736 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
737 // as if we had never changed windows at all).
738 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500739#endif
740
Brian Osman70d2f432017-11-08 09:54:10 -0500741 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500742}
743
Brian Osman92004802017-03-06 11:47:26 -0500744void Viewer::setColorMode(ColorMode colorMode) {
745 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700746
Brian Osmanf750fbc2017-02-08 10:47:28 -0500747 // 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 -0400748 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700749 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500750 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400751 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
752 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500753 if (wasInLegacy != wantLegacy) {
754 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700755 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500756 }
757
758 this->updateTitle();
759 fWindow->inval();
760}
761
762void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500763 SkAutoCanvasRestore autorestore(canvas, false);
764
Brian Osmanf750fbc2017-02-08 10:47:28 -0500765 // By default, we render directly into the window's surface/canvas
766 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500767 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700768
Brian Osmane0d4fba2017-03-15 10:24:55 -0400769 // If we're in any of the color managed modes, construct the color space we're going to use
770 sk_sp<SkColorSpace> cs = nullptr;
771 if (ColorMode::kLegacy != fColorMode) {
772 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
773 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400774 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400775 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500776 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
777 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
778 } else {
779 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
780 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400781 }
782
Brian Osman3ac99cf2017-12-01 11:23:53 -0500783 if (fSaveToSKP) {
784 SkPictureRecorder recorder;
785 SkCanvas* recorderCanvas = recorder.beginRecording(
786 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
787 // In xform-canvas mode, record the transformed output
788 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
789 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
790 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
791 recorderCanvas = xformCanvas.get();
792 }
793 fSlides[fCurrentSlide]->draw(recorderCanvas);
794 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
795 SkFILEWStream stream("sample_app.skp");
796 picture->serialize(&stream);
797 fSaveToSKP = false;
798 }
799
Brian Osmane0d4fba2017-03-15 10:24:55 -0400800 // 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 -0500801 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
802 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500803 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -0500804 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
805 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -0500806 if (Window::kRaster_BackendType == fBackendType ||
807 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500808 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400809 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
810 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
811
Brian Osman92004802017-03-06 11:47:26 -0500812 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
813 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400814 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
815 // with a special canvas (below) that has the color space attached
816 sk_sp<SkColorSpace> offscreenColorSpace =
817 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500818 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400819 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500820 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
821 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -0500822 SkPixmap offscreenPixmap;
823 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
824 SkBitmap offscreenBitmap;
825 offscreenBitmap.installPixels(offscreenPixmap);
826 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
827 fThreadCnt, fExecutor.get()));
828 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
829 slideCanvas = threadedCanvas.get();
830 } else {
831 slideCanvas = offscreenSurface->getCanvas();
832 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500833 }
834
Brian Osmane0d4fba2017-03-15 10:24:55 -0400835 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
836 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
837 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
838 slideCanvas = xformCanvas.get();
839 }
840
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500841 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500842 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500843 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500844 // Time the painting logic of the slide
845 double startTime = SkTime::GetMSecs();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500846 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osman1df161a2017-02-09 12:10:20 -0500847 fPaintTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500848 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500849
850 // Force a flush so we can time that, too
851 startTime = SkTime::GetMSecs();
852 slideCanvas->flush();
853 fFlushTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500854
855 // If we rendered offscreen, snap an image and push the results to the window's canvas
856 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500857 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500858
859 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400860 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500861 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400862 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500863 SkPaint paint;
864 paint.setBlendMode(SkBlendMode::kSrc);
865 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -0700866 }
liyuqian6f163d22016-06-13 12:26:45 -0700867}
868
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700869void Viewer::onBackendCreated() {
870 this->updateTitle();
871 this->updateUIState();
872 this->setupCurrentSlide(-1);
Jim Van Verth90dcce52017-11-03 13:36:07 -0400873 this->resetMeasurements();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700874 fWindow->show();
875 fWindow->inval();
876}
Jim Van Verth6f449692017-02-14 15:16:46 -0500877
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700878void Viewer::onPaint(SkCanvas* canvas) {
Brian Osman79086b92017-02-10 13:36:16 -0500879 // Update ImGui input
880 ImGuiIO& io = ImGui::GetIO();
881 io.DeltaTime = 1.0f / 60.0f;
882 io.DisplaySize.x = static_cast<float>(fWindow->width());
883 io.DisplaySize.y = static_cast<float>(fWindow->height());
884
885 io.KeyAlt = io.KeysDown[static_cast<int>(Window::Key::kOption)];
886 io.KeyCtrl = io.KeysDown[static_cast<int>(Window::Key::kCtrl)];
887 io.KeyShift = io.KeysDown[static_cast<int>(Window::Key::kShift)];
888
889 ImGui::NewFrame();
890
Jim Van Verth90dcce52017-11-03 13:36:07 -0400891 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -0700892
Brian Osman1df161a2017-02-09 12:10:20 -0500893 // Advance our timing bookkeeping
Jim Van Verth90dcce52017-11-03 13:36:07 -0400894 fCumulativeMeasurementTime += fAnimateTimes[fCurrentMeasurement] +
895 fPaintTimes[fCurrentMeasurement] +
896 fFlushTimes[fCurrentMeasurement];
897 fCumulativeMeasurementCount++;
Brian Osman1df161a2017-02-09 12:10:20 -0500898 fCurrentMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1);
899 SkASSERT(fCurrentMeasurement < kMeasurementCount);
900
901 // Draw any overlays or UI that we don't want timed
jvanverthc265a922016-04-08 12:51:45 -0700902 if (fDisplayStats) {
903 drawStats(canvas);
904 }
brianosman622c8d52016-05-10 06:50:49 -0700905 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -0700906
Jim Van Verth90dcce52017-11-03 13:36:07 -0400907 this->drawImGui(canvas);
Brian Osman79086b92017-02-10 13:36:16 -0500908
Brian Osman1df161a2017-02-09 12:10:20 -0500909 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -0400910 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700911}
912
jvanverth814e38d2016-06-06 08:48:47 -0700913bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400914 if (GestureDevice::kMouse == fGestureDevice) {
915 return false;
916 }
liyuqiand3cdbca2016-05-17 12:44:20 -0700917 void* castedOwner = reinterpret_cast<void*>(owner);
918 switch (state) {
919 case Window::kUp_InputState: {
920 fGesture.touchEnd(castedOwner);
921 break;
922 }
923 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400924 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700925 break;
926 }
927 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400928 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700929 break;
930 }
931 }
Brian Osmanb53f48c2017-06-07 10:00:30 -0400932 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -0700933 fWindow->inval();
934 return true;
935}
936
Brian Osman80fc07e2017-12-08 16:45:43 -0500937bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
938 ImGuiIO& io = ImGui::GetIO();
939 io.MousePos.x = static_cast<float>(x);
940 io.MousePos.y = static_cast<float>(y);
941 if (Window::kDown_InputState == state) {
942 io.MouseDown[0] = true;
943 } else if (Window::kUp_InputState == state) {
944 io.MouseDown[0] = false;
Brian Osmanb53f48c2017-06-07 10:00:30 -0400945 }
Brian Osman80fc07e2017-12-08 16:45:43 -0500946 if (io.WantCaptureMouse) {
947 return true;
948 } else {
949 if (!fSlides[fCurrentSlide]->onMouse(x, y, state, modifiers)) {
950 if (GestureDevice::kTouch == fGestureDevice) {
951 return false;
952 }
953 switch (state) {
954 case Window::kUp_InputState: {
955 fGesture.touchEnd(nullptr);
956 break;
957 }
958 case Window::kDown_InputState: {
959 fGesture.touchBegin(nullptr, x, y);
960 break;
961 }
962 case Window::kMove_InputState: {
963 fGesture.touchMoved(nullptr, x, y);
964 break;
965 }
966 }
967 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
968 }
969 fWindow->inval();
970 return true;
971 }
972}
973
974bool Viewer::onMouseWheel(float delta, uint32_t modifiers) {
975 ImGuiIO& io = ImGui::GetIO();
976 io.MouseWheel += delta;
Jim Van Verthe7705782017-05-04 14:00:59 -0400977 return true;
978}
979
jvanverth34524262016-05-04 13:49:13 -0700980void Viewer::drawStats(SkCanvas* canvas) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700981 static const float kPixelPerMS = 2.0f;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500982 static const int kDisplayWidth = 192;
983 static const int kGraphHeight = 100;
984 static const int kTextHeight = 60;
985 static const int kDisplayHeight = kGraphHeight + kTextHeight;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700986 static const int kDisplayPadding = 10;
987 static const int kGraphPadding = 3;
988 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
989
Mike Reed3661bc92017-02-22 13:21:42 -0500990 SkISize canvasSize = canvas->getBaseLayerSize();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700991 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
992 SkIntToScalar(kDisplayPadding),
993 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
994 SkPaint paint;
995 canvas->save();
996
jvanverth3d6ed3a2016-04-07 11:09:51 -0700997 paint.setColor(SK_ColorBLACK);
998 canvas->drawRect(rect, paint);
999 // draw the 16ms line
1000 paint.setColor(SK_ColorLTGRAY);
1001 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
1002 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
1003 paint.setColor(SK_ColorRED);
1004 paint.setStyle(SkPaint::kStroke_Style);
1005 canvas->drawRect(rect, paint);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001006 paint.setStyle(SkPaint::kFill_Style);
jvanverth3d6ed3a2016-04-07 11:09:51 -07001007
1008 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001009 const int xStep = 3;
jvanverth3d6ed3a2016-04-07 11:09:51 -07001010 int i = fCurrentMeasurement;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001011 double ms = 0;
1012 double animateMS = 0;
1013 double paintMS = 0;
1014 double flushMS = 0;
1015 int count = 0;
jvanverth3d6ed3a2016-04-07 11:09:51 -07001016 do {
Brian Osman1df161a2017-02-09 12:10:20 -05001017 // Round to nearest values
1018 int animateHeight = (int)(fAnimateTimes[i] * kPixelPerMS + 0.5);
1019 int paintHeight = (int)(fPaintTimes[i] * kPixelPerMS + 0.5);
1020 int flushHeight = (int)(fFlushTimes[i] * kPixelPerMS + 0.5);
1021 int startY = SkScalarTruncToInt(rect.fBottom);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001022 int endY = SkTMax(startY - flushHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -05001023 paint.setColor(SK_ColorRED);
1024 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
1025 SkIntToScalar(x), SkIntToScalar(endY), paint);
1026 startY = endY;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001027 endY = SkTMax(startY - paintHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -05001028 paint.setColor(SK_ColorGREEN);
1029 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
1030 SkIntToScalar(x), SkIntToScalar(endY), paint);
1031 startY = endY;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001032 endY = SkTMax(startY - animateHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -05001033 paint.setColor(SK_ColorMAGENTA);
jvanverth3d6ed3a2016-04-07 11:09:51 -07001034 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
1035 SkIntToScalar(x), SkIntToScalar(endY), paint);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001036
1037 double inc = fAnimateTimes[i] + fPaintTimes[i] + fFlushTimes[i];
1038 if (inc > 0) {
1039 ms += inc;
1040 animateMS += fAnimateTimes[i];
1041 paintMS += fPaintTimes[i];
1042 flushMS += fFlushTimes[i];
1043 ++count;
1044 }
1045
jvanverth3d6ed3a2016-04-07 11:09:51 -07001046 i++;
1047 i &= (kMeasurementCount - 1); // fast mod
1048 x += xStep;
1049 } while (i != fCurrentMeasurement);
jvanverth9f372462016-04-06 06:08:59 -07001050
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001051 paint.setTextSize(16);
1052 SkString mainString;
1053 mainString.appendf("%4.3f ms -> %4.3f ms", ms / SkTMax(1, count),
1054 fCumulativeMeasurementTime / SkTMax(1, fCumulativeMeasurementCount));
1055 paint.setColor(SK_ColorWHITE);
1056 canvas->drawString(mainString.c_str(), rect.fLeft+3, rect.fTop + 14, paint);
1057
1058 SkString animateString;
1059 animateString.appendf("Animate: %4.3f ms", animateMS / SkTMax(1, count));
1060 paint.setColor(0xffff66ff); // pure magenta is hard to read
1061 canvas->drawString(animateString.c_str(), rect.fLeft+3, rect.fTop + 28, paint);
1062
1063 SkString paintString;
1064 paintString.appendf("Paint: %4.3f ms", paintMS / SkTMax(1, count));
1065 paint.setColor(SK_ColorGREEN);
1066 canvas->drawString(paintString.c_str(), rect.fLeft+3, rect.fTop + 42, paint);
1067
1068 SkString flushString;
1069 flushString.appendf("Flush: %4.3f ms", flushMS / SkTMax(1, count));
1070 paint.setColor(0xffff6666); // pure red is hard to read
1071 canvas->drawString(flushString.c_str(), rect.fLeft+3, rect.fTop + 56, paint);
1072
jvanverth9f372462016-04-06 06:08:59 -07001073 canvas->restore();
1074}
1075
Brian Osmana109e392017-02-24 09:49:14 -05001076static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1077 const ImVec2& pos, const ImVec2& size) {
1078 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1079 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1080
1081 // Invisible 10x10 button
1082 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1083 ImGui::InvisibleButton(label, ImVec2(10, 10));
1084
1085 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1086 ImGuiIO& io = ImGui::GetIO();
1087 // Normalized mouse position, relative to our gamut box
1088 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1089 // Clamp to edge of box, convert back to primary scale
1090 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1091 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1092 }
1093
1094 if (ImGui::IsItemHovered()) {
1095 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1096 }
1097
1098 // Return screen coordinates for the caller. We could just return center here, but we'd have
1099 // one frame of lag during drag.
1100 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1101}
1102
1103static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1104 ImDrawList* drawList = ImGui::GetWindowDrawList();
1105
1106 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1107 // width, and scale the height to maintain aspect ratio.
1108 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1109 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1110 ImVec2 pos = ImGui::GetCursorScreenPos();
1111
1112 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1113 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1114 // Magic numbers are pixel locations of the origin and upper-right corner.
1115 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1116 ImVec2(242, 61), ImVec2(1897, 1922));
1117 ImVec2 endPos = ImGui::GetCursorPos();
1118
1119 // Primary markers
1120 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1121 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1122 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1123 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1124
1125 // Gamut triangle
1126 drawList->AddCircle(r, 5.0f, 0xFF000040);
1127 drawList->AddCircle(g, 5.0f, 0xFF004000);
1128 drawList->AddCircle(b, 5.0f, 0xFF400000);
1129 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1130 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1131
1132 // Re-position cursor immediate after the diagram for subsequent controls
1133 ImGui::SetCursorPos(endPos);
1134}
1135
Brian Osmanead517d2017-11-13 15:36:36 -05001136typedef std::function<void(SkCanvas*)> CustomGuiPainter;
1137static SkTArray<CustomGuiPainter> gCustomGuiPainters;
1138
1139static void ImGui_Skia_Callback(const ImVec2& size, CustomGuiPainter painter) {
1140 intptr_t painterIndex = gCustomGuiPainters.count();
1141 gCustomGuiPainters.push_back(painter);
1142 ImGui::Image((ImTextureID)painterIndex, size);
1143}
1144
Brian Osman79086b92017-02-10 13:36:16 -05001145void Viewer::drawImGui(SkCanvas* canvas) {
1146 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1147 if (fShowImGuiTestWindow) {
1148 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1149 }
1150
1151 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001152 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1153 // always visible, we can end up in a layout feedback loop.
1154 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001155 DisplayParams params = fWindow->getRequestedDisplayParams();
1156 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001157 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1158 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001159 if (ImGui::CollapsingHeader("Backend")) {
1160 int newBackend = static_cast<int>(fBackendType);
1161 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1162 ImGui::SameLine();
1163 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001164#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1165 ImGui::SameLine();
1166 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1167#endif
Brian Osman621491e2017-02-28 15:45:01 -05001168#if defined(SK_VULKAN)
1169 ImGui::SameLine();
1170 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1171#endif
1172 if (newBackend != fBackendType) {
1173 fDeferredActions.push_back([=]() {
1174 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1175 });
1176 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001177
Brian Salomon99a33902017-03-07 15:16:34 -05001178 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001179 bool* wire = &params.fGrContextOptions.fWireframeMode;
1180 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1181 paramsChanged = true;
1182 }
Brian Salomon99a33902017-03-07 15:16:34 -05001183
Brian Osman28b12522017-03-08 17:10:24 -05001184 if (ctx) {
1185 int sampleCount = fWindow->sampleCount();
1186 ImGui::Text("MSAA: "); ImGui::SameLine();
1187 ImGui::RadioButton("0", &sampleCount, 0); ImGui::SameLine();
1188 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1189 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1190 ImGui::RadioButton("16", &sampleCount, 16);
1191
1192 if (sampleCount != params.fMSAASampleCount) {
1193 params.fMSAASampleCount = sampleCount;
1194 paramsChanged = true;
1195 }
1196 }
1197
Brian Osman8a9de3d2017-03-01 14:59:05 -05001198 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001199 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001200 auto prButton = [&](GpuPathRenderers x) {
1201 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001202 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1203 params.fGrContextOptions.fGpuPathRenderers = x;
1204 paramsChanged = true;
1205 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001206 }
1207 };
1208
1209 if (!ctx) {
1210 ImGui::RadioButton("Software", true);
1211 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001212 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001213 prButton(GpuPathRenderers::kAll);
1214 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1215 prButton(GpuPathRenderers::kStencilAndCover);
1216 }
1217 if (ctx->caps()->sampleShadingSupport()) {
1218 prButton(GpuPathRenderers::kMSAA);
1219 }
1220 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001221 prButton(GpuPathRenderers::kNone);
1222 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001223 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001224 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001225 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1226 prButton(GpuPathRenderers::kCoverageCounting);
1227 }
Jim Van Verth83010462017-03-16 08:45:39 -04001228 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001229 prButton(GpuPathRenderers::kTessellating);
1230 prButton(GpuPathRenderers::kNone);
1231 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001232 ImGui::TreePop();
1233 }
Brian Osman621491e2017-02-28 15:45:01 -05001234 }
1235
Brian Osmanfce09c52017-11-14 15:32:20 -05001236 if (fShowSlidePicker) {
1237 ImGui::SetNextTreeNodeOpen(true);
1238 }
1239
Brian Osman79086b92017-02-10 13:36:16 -05001240 if (ImGui::CollapsingHeader("Slide")) {
1241 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001242 static ImVector<const char*> filteredSlideNames;
1243 static ImVector<int> filteredSlideIndices;
1244
Brian Osmanfce09c52017-11-14 15:32:20 -05001245 if (fShowSlidePicker) {
1246 ImGui::SetKeyboardFocusHere();
1247 fShowSlidePicker = false;
1248 }
1249
Brian Osman79086b92017-02-10 13:36:16 -05001250 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001251 filteredSlideNames.clear();
1252 filteredSlideIndices.clear();
1253 int filteredIndex = 0;
1254 for (int i = 0; i < fSlides.count(); ++i) {
1255 const char* slideName = fSlides[i]->getName().c_str();
1256 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1257 if (i == fCurrentSlide) {
1258 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001259 }
Brian Osmanf479e422017-11-08 13:11:36 -05001260 filteredSlideNames.push_back(slideName);
1261 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001262 }
Brian Osman79086b92017-02-10 13:36:16 -05001263 }
Brian Osmanf479e422017-11-08 13:11:36 -05001264
1265 int previousSlide = fCurrentSlide;
1266 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1267 filteredSlideNames.size(), 20)) {
1268 fCurrentSlide = filteredSlideIndices[filteredIndex];
1269 setupCurrentSlide(previousSlide);
Brian Osman79086b92017-02-10 13:36:16 -05001270 }
1271 }
Brian Osmana109e392017-02-24 09:49:14 -05001272
1273 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001274 ColorMode newMode = fColorMode;
1275 auto cmButton = [&](ColorMode mode, const char* label) {
1276 if (ImGui::RadioButton(label, mode == fColorMode)) {
1277 newMode = mode;
1278 }
1279 };
1280
1281 cmButton(ColorMode::kLegacy, "Legacy 8888");
1282 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1283 "Color Managed 8888 (Nonlinear blending)");
1284 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1285 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1286
1287 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001288 // It isn't safe to switch color mode now (in the middle of painting). We might
1289 // tear down the back-end, etc... Defer this change until the next onIdle.
1290 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001291 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001292 });
Brian Osmana109e392017-02-24 09:49:14 -05001293 }
1294
1295 // Pick from common gamuts:
1296 int primariesIdx = 4; // Default: Custom
1297 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1298 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1299 primariesIdx = i;
1300 break;
1301 }
1302 }
1303
Brian Osmanfdab5762017-11-09 10:27:55 -05001304 // When we're in xform canvas mode, we can alter the transfer function, too
1305 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1306 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1307 }
1308
Brian Osmana109e392017-02-24 09:49:14 -05001309 if (ImGui::Combo("Primaries", &primariesIdx,
1310 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1311 if (primariesIdx >= 0 && primariesIdx <= 3) {
1312 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1313 }
1314 }
1315
1316 // Allow direct editing of gamut
1317 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1318 }
Brian Osman79086b92017-02-10 13:36:16 -05001319 }
Brian Salomon99a33902017-03-07 15:16:34 -05001320 if (paramsChanged) {
1321 fDeferredActions.push_back([=]() {
1322 fWindow->setRequestedDisplayParams(params);
1323 fWindow->inval();
1324 this->updateTitle();
1325 });
1326 }
Brian Osman79086b92017-02-10 13:36:16 -05001327 ImGui::End();
1328 }
1329
Brian Osmanf6877092017-02-13 09:39:57 -05001330 if (fShowZoomWindow && fLastImage) {
1331 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001332 static int zoomFactor = 8;
1333 if (ImGui::Button("<<")) {
1334 zoomFactor = SkTMax(zoomFactor / 2, 4);
1335 }
1336 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1337 if (ImGui::Button(">>")) {
1338 zoomFactor = SkTMin(zoomFactor * 2, 32);
1339 }
Brian Osmanf6877092017-02-13 09:39:57 -05001340
Brian Osmanf6877092017-02-13 09:39:57 -05001341 ImVec2 mousePos = ImGui::GetMousePos();
1342 ImVec2 avail = ImGui::GetContentRegionAvail();
1343
Brian Osmanead517d2017-11-13 15:36:36 -05001344 uint32_t pixel = 0;
1345 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1346 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1347 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001348 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1349 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1350 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001351 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1352 }
1353
1354 ImGui_Skia_Callback(avail, [=](SkCanvas* c) {
1355 // Translate so the region of the image that's under the mouse cursor is centered
1356 // in the zoom canvas:
1357 c->scale(zoomFactor, zoomFactor);
1358 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1359 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1360 c->drawImage(this->fLastImage, 0, 0);
1361
1362 SkPaint outline;
1363 outline.setStyle(SkPaint::kStroke_Style);
1364 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1365 });
Brian Osmanf6877092017-02-13 09:39:57 -05001366 }
1367
1368 ImGui::End();
1369 }
1370
Brian Osman79086b92017-02-10 13:36:16 -05001371 // This causes ImGui to rebuild vertex/index data based on all immediate-mode commands
1372 // (widgets, etc...) that have been issued
1373 ImGui::Render();
1374
1375 // Then we fetch the most recent data, and convert it so we can render with Skia
1376 const ImDrawData* drawData = ImGui::GetDrawData();
1377 SkTDArray<SkPoint> pos;
1378 SkTDArray<SkPoint> uv;
1379 SkTDArray<SkColor> color;
Brian Osman79086b92017-02-10 13:36:16 -05001380
1381 for (int i = 0; i < drawData->CmdListsCount; ++i) {
1382 const ImDrawList* drawList = drawData->CmdLists[i];
1383
1384 // De-interleave all vertex data (sigh), convert to Skia types
1385 pos.rewind(); uv.rewind(); color.rewind();
1386 for (int i = 0; i < drawList->VtxBuffer.size(); ++i) {
1387 const ImDrawVert& vert = drawList->VtxBuffer[i];
1388 pos.push(SkPoint::Make(vert.pos.x, vert.pos.y));
1389 uv.push(SkPoint::Make(vert.uv.x, vert.uv.y));
1390 color.push(vert.col);
1391 }
1392 // ImGui colors are RGBA
1393 SkSwapRB(color.begin(), color.begin(), color.count());
1394
1395 int indexOffset = 0;
1396
1397 // Draw everything with canvas.drawVertices...
1398 for (int j = 0; j < drawList->CmdBuffer.size(); ++j) {
1399 const ImDrawCmd* drawCmd = &drawList->CmdBuffer[j];
1400
Brian Osmanead517d2017-11-13 15:36:36 -05001401 SkAutoCanvasRestore acr(canvas, true);
1402
Brian Osman79086b92017-02-10 13:36:16 -05001403 // TODO: Find min/max index for each draw, so we know how many vertices (sigh)
1404 if (drawCmd->UserCallback) {
1405 drawCmd->UserCallback(drawList, drawCmd);
1406 } else {
Brian Osmanead517d2017-11-13 15:36:36 -05001407 intptr_t idIndex = (intptr_t)drawCmd->TextureId;
1408 if (idIndex < gCustomGuiPainters.count()) {
1409 // Small image IDs are actually indices into a list of callbacks. We directly
1410 // examing the vertex data to deduce the image rectangle, then reconfigure the
1411 // canvas to be clipped and translated so that the callback code gets to use
1412 // Skia to render a widget in the middle of an ImGui panel.
1413 ImDrawIdx rectIndex = drawList->IdxBuffer[indexOffset];
1414 SkPoint tl = pos[rectIndex], br = pos[rectIndex + 2];
1415 canvas->clipRect(SkRect::MakeLTRB(tl.fX, tl.fY, br.fX, br.fY));
1416 canvas->translate(tl.fX, tl.fY);
1417 gCustomGuiPainters[idIndex](canvas);
1418 } else {
1419 SkPaint* paint = static_cast<SkPaint*>(drawCmd->TextureId);
1420 SkASSERT(paint);
Brian Osmanf6877092017-02-13 09:39:57 -05001421
Brian Osmanead517d2017-11-13 15:36:36 -05001422 canvas->clipRect(SkRect::MakeLTRB(drawCmd->ClipRect.x, drawCmd->ClipRect.y,
1423 drawCmd->ClipRect.z, drawCmd->ClipRect.w));
1424 auto vertices = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode,
1425 drawList->VtxBuffer.size(),
1426 pos.begin(), uv.begin(), color.begin(),
1427 drawCmd->ElemCount,
1428 drawList->IdxBuffer.begin() + indexOffset);
1429 canvas->drawVertices(vertices, SkBlendMode::kModulate, *paint);
1430 indexOffset += drawCmd->ElemCount;
1431 }
Brian Osman79086b92017-02-10 13:36:16 -05001432 }
1433 }
1434 }
Brian Osmanead517d2017-11-13 15:36:36 -05001435
1436 gCustomGuiPainters.reset();
Brian Osman79086b92017-02-10 13:36:16 -05001437}
1438
liyuqian2edb0f42016-07-06 14:11:32 -07001439void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001440 for (int i = 0; i < fDeferredActions.count(); ++i) {
1441 fDeferredActions[i]();
1442 }
1443 fDeferredActions.reset();
1444
Brian Osman1df161a2017-02-09 12:10:20 -05001445 double startTime = SkTime::GetMSecs();
jvanverthc265a922016-04-08 12:51:45 -07001446 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001447 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
1448 fAnimateTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
1449
Brian Osman79086b92017-02-10 13:36:16 -05001450 ImGuiIO& io = ImGui::GetIO();
1451 if (animateWantsInval || fDisplayStats || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001452 fWindow->inval();
1453 }
jvanverth9f372462016-04-06 06:08:59 -07001454}
liyuqiane5a6cd92016-05-27 08:52:52 -07001455
1456void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001457 if (!fWindow) {
1458 return;
1459 }
1460 if (fWindow->sampleCount() < 0) {
1461 return; // Surface hasn't been created yet.
1462 }
1463
liyuqianb73c24b2016-06-03 08:47:23 -07001464 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001465 Json::Value slideState(Json::objectValue);
1466 slideState[kName] = kSlideStateName;
1467 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001468 if (fAllSlideNames.size() == 0) {
1469 for(auto slide : fSlides) {
1470 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1471 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001472 }
liyuqian1f508fd2016-06-07 06:57:40 -07001473 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001474
liyuqianb73c24b2016-06-03 08:47:23 -07001475 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001476 Json::Value backendState(Json::objectValue);
1477 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001478 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001479 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001480 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001481 backendState[kOptions].append(Json::Value(str));
1482 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001483
csmartdalton578f0642017-02-24 16:04:47 -07001484 // MSAA state
1485 Json::Value msaaState(Json::objectValue);
1486 msaaState[kName] = kMSAAStateName;
1487 msaaState[kValue] = fWindow->sampleCount();
1488 msaaState[kOptions] = Json::Value(Json::arrayValue);
1489 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1490 msaaState[kOptions].append(Json::Value(0));
1491 } else {
1492 for (int msaa : {0, 4, 8, 16}) {
1493 msaaState[kOptions].append(Json::Value(msaa));
1494 }
1495 }
1496
csmartdalton61cd31a2017-02-27 17:00:53 -07001497 // Path renderer state
1498 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1499 Json::Value prState(Json::objectValue);
1500 prState[kName] = kPathRendererStateName;
1501 prState[kValue] = gPathRendererNames[pr];
1502 prState[kOptions] = Json::Value(Json::arrayValue);
1503 const GrContext* ctx = fWindow->getGrContext();
1504 if (!ctx) {
1505 prState[kOptions].append("Software");
1506 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001507 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001508 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1509 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1510 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1511 }
1512 if (ctx->caps()->sampleShadingSupport()) {
1513 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1514 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001515 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001516 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1517 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001518 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001519 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001520 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1521 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1522 }
Jim Van Verth83010462017-03-16 08:45:39 -04001523 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001524 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001525 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1526 }
1527
liyuqianb73c24b2016-06-03 08:47:23 -07001528 // Softkey state
1529 Json::Value softkeyState(Json::objectValue);
1530 softkeyState[kName] = kSoftkeyStateName;
1531 softkeyState[kValue] = kSoftkeyHint;
1532 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1533 softkeyState[kOptions].append(kSoftkeyHint);
1534 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1535 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1536 }
1537
liyuqian1f508fd2016-06-07 06:57:40 -07001538 // FPS state
1539 Json::Value fpsState(Json::objectValue);
1540 fpsState[kName] = kFpsStateName;
Brian Osman1df161a2017-02-09 12:10:20 -05001541 int idx = (fCurrentMeasurement + (kMeasurementCount - 1)) & (kMeasurementCount - 1);
1542 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
1543 fAnimateTimes[idx] + fPaintTimes[idx] + fFlushTimes[idx],
1544 fAnimateTimes[idx],
1545 fPaintTimes[idx],
1546 fFlushTimes[idx]).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001547 fpsState[kOptions] = Json::Value(Json::arrayValue);
1548
liyuqiane5a6cd92016-05-27 08:52:52 -07001549 Json::Value state(Json::arrayValue);
1550 state.append(slideState);
1551 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001552 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001553 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001554 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001555 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001556
Brian Osmaneff04b52017-11-21 13:18:02 -05001557 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001558}
1559
1560void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001561 // For those who will add more features to handle the state change in this function:
1562 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1563 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1564 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001565 if (stateName.equals(kSlideStateName)) {
1566 int previousSlide = fCurrentSlide;
1567 fCurrentSlide = 0;
1568 for(auto slide : fSlides) {
1569 if (slide->getName().equals(stateValue)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001570 this->setupCurrentSlide(previousSlide);
liyuqiane5a6cd92016-05-27 08:52:52 -07001571 break;
1572 }
1573 fCurrentSlide++;
1574 }
1575 if (fCurrentSlide >= fSlides.count()) {
1576 fCurrentSlide = previousSlide;
1577 SkDebugf("Slide not found: %s", stateValue.c_str());
1578 }
liyuqian6cb70252016-06-02 12:16:25 -07001579 } else if (stateName.equals(kBackendStateName)) {
1580 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1581 if (stateValue.equals(kBackendTypeStrings[i])) {
1582 if (fBackendType != i) {
1583 fBackendType = (sk_app::Window::BackendType)i;
1584 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001585 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001586 }
1587 break;
1588 }
1589 }
csmartdalton578f0642017-02-24 16:04:47 -07001590 } else if (stateName.equals(kMSAAStateName)) {
1591 DisplayParams params = fWindow->getRequestedDisplayParams();
1592 int sampleCount = atoi(stateValue.c_str());
1593 if (sampleCount != params.fMSAASampleCount) {
1594 params.fMSAASampleCount = sampleCount;
1595 fWindow->setRequestedDisplayParams(params);
1596 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001597 this->updateTitle();
1598 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001599 }
1600 } else if (stateName.equals(kPathRendererStateName)) {
1601 DisplayParams params = fWindow->getRequestedDisplayParams();
1602 for (const auto& pair : gPathRendererNames) {
1603 if (pair.second == stateValue.c_str()) {
1604 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1605 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1606 fWindow->setRequestedDisplayParams(params);
1607 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001608 this->updateTitle();
1609 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001610 }
1611 break;
1612 }
csmartdalton578f0642017-02-24 16:04:47 -07001613 }
liyuqianb73c24b2016-06-03 08:47:23 -07001614 } else if (stateName.equals(kSoftkeyStateName)) {
1615 if (!stateValue.equals(kSoftkeyHint)) {
1616 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001617 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001618 }
liyuqian2edb0f42016-07-06 14:11:32 -07001619 } else if (stateName.equals(kRefreshStateName)) {
1620 // This state is actually NOT in the UI state.
1621 // We use this to allow Android to quickly set bool fRefresh.
1622 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001623 } else {
1624 SkDebugf("Unknown stateName: %s", stateName.c_str());
1625 }
1626}
Brian Osman79086b92017-02-10 13:36:16 -05001627
1628bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osman80fc07e2017-12-08 16:45:43 -05001629 ImGuiIO& io = ImGui::GetIO();
1630 io.KeysDown[static_cast<int>(key)] = (Window::kDown_InputState == state);
1631
1632 if (io.WantCaptureKeyboard) {
1633 return true;
1634 } else {
1635 return fCommands.onKey(key, state, modifiers);
1636 }
Brian Osman79086b92017-02-10 13:36:16 -05001637}
1638
1639bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osman80fc07e2017-12-08 16:45:43 -05001640 ImGuiIO& io = ImGui::GetIO();
1641 if (io.WantTextInput) {
1642 if (c > 0 && c < 0x10000) {
1643 io.AddInputCharacter(c);
1644 }
1645 return true;
1646 } else if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001647 fWindow->inval();
1648 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001649 } else {
1650 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001651 }
Brian Osman79086b92017-02-10 13:36:16 -05001652}