blob: 297501454f461b271bc1595681d524502c7930f2 [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"
jvanverth2bb3b6d2016-04-08 07:24:09 -070033#include "SkRandom.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040034#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070035#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070036#include "SkSurface.h"
Brian Osman79086b92017-02-10 13:36:16 -050037#include "SkSwizzle.h"
csmartdalton29d87152017-02-10 17:05:14 -050038#include "SkTaskGroup.h"
liyuqian2edb0f42016-07-06 14:11:32 -070039#include "SkTime.h"
Mike Reed887cdf12017-04-03 11:11:09 -040040#include "SkVertices.h"
jvanverth9f372462016-04-06 06:08:59 -070041
Brian Osman79086b92017-02-10 13:36:16 -050042#include "imgui.h"
43
Chris Dalton1a325d22017-07-14 15:17:41 -060044#include "ccpr/GrCoverageCountingPathRenderer.h"
45
csmartdalton578f0642017-02-24 16:04:47 -070046#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070047#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070048
jvanverth34524262016-05-04 13:49:13 -070049using namespace sk_app;
50
csmartdalton61cd31a2017-02-27 17:00:53 -070051static std::map<GpuPathRenderers, std::string> gPathRendererNames;
52
jvanverth9f372462016-04-06 06:08:59 -070053Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070054 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070055}
56
Christopher Dalton443ec1b2017-02-24 13:22:53 -070057static void on_backend_created_func(void* userData) {
58 Viewer* vv = reinterpret_cast<Viewer*>(userData);
59
60 return vv->onBackendCreated();
61}
62
jvanverth9f372462016-04-06 06:08:59 -070063static void on_paint_handler(SkCanvas* canvas, void* userData) {
jvanverth34524262016-05-04 13:49:13 -070064 Viewer* vv = reinterpret_cast<Viewer*>(userData);
jvanverth9f372462016-04-06 06:08:59 -070065
66 return vv->onPaint(canvas);
67}
68
jvanverth814e38d2016-06-06 08:48:47 -070069static bool on_touch_handler(intptr_t owner, Window::InputState state, float x, float y, void* userData)
liyuqiand3cdbca2016-05-17 12:44:20 -070070{
71 Viewer* viewer = reinterpret_cast<Viewer*>(userData);
72
73 return viewer->onTouch(owner, state, x, y);
74}
75
liyuqiane5a6cd92016-05-27 08:52:52 -070076static void on_ui_state_changed_handler(const SkString& stateName, const SkString& stateValue, void* userData) {
77 Viewer* viewer = reinterpret_cast<Viewer*>(userData);
78
79 return viewer->onUIStateChanged(stateName, stateValue);
80}
81
Brian Osman79086b92017-02-10 13:36:16 -050082static bool on_mouse_handler(int x, int y, Window::InputState state, uint32_t modifiers,
83 void* userData) {
84 ImGuiIO& io = ImGui::GetIO();
85 io.MousePos.x = static_cast<float>(x);
86 io.MousePos.y = static_cast<float>(y);
87 if (Window::kDown_InputState == state) {
88 io.MouseDown[0] = true;
89 } else if (Window::kUp_InputState == state) {
90 io.MouseDown[0] = false;
91 }
Jim Van Verthe7705782017-05-04 14:00:59 -040092 if (io.WantCaptureMouse) {
93 return true;
94 } else {
95 Viewer* viewer = reinterpret_cast<Viewer*>(userData);
96 return viewer->onMouse(x, y, state, modifiers);
97 }
Brian Osman79086b92017-02-10 13:36:16 -050098}
99
100static bool on_mouse_wheel_handler(float delta, uint32_t modifiers, void* userData) {
101 ImGuiIO& io = ImGui::GetIO();
102 io.MouseWheel += delta;
103 return true;
104}
105
106static bool on_key_handler(Window::Key key, Window::InputState state, uint32_t modifiers,
107 void* userData) {
108 ImGuiIO& io = ImGui::GetIO();
109 io.KeysDown[static_cast<int>(key)] = (Window::kDown_InputState == state);
110
111 if (io.WantCaptureKeyboard) {
112 return true;
113 } else {
114 Viewer* viewer = reinterpret_cast<Viewer*>(userData);
115 return viewer->onKey(key, state, modifiers);
116 }
117}
118
119static bool on_char_handler(SkUnichar c, uint32_t modifiers, void* userData) {
120 ImGuiIO& io = ImGui::GetIO();
121 if (io.WantTextInput) {
122 if (c > 0 && c < 0x10000) {
123 io.AddInputCharacter(c);
124 }
125 return true;
126 } else {
127 Viewer* viewer = reinterpret_cast<Viewer*>(userData);
128 return viewer->onChar(c, modifiers);
129 }
130}
131
Brian Osman2dd96932016-10-18 15:33:53 -0400132static DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
Brian Osman16adfa32016-10-18 14:42:44 -0400133
Brian Osman2dd96932016-10-18 15:33:53 -0400134static DEFINE_string2(match, m, nullptr,
jvanverth2bb3b6d2016-04-08 07:24:09 -0700135 "[~][^]substring[$] [...] of bench name to run.\n"
136 "Multiple matches may be separated by spaces.\n"
137 "~ causes a matching bench to always be skipped\n"
138 "^ requires the start of the bench to match\n"
139 "$ requires the end of the bench to match\n"
140 "^ and $ requires an exact match\n"
141 "If a bench does not match any list entry,\n"
142 "it is skipped unless some list entry starts with ~");
bsalomon6c471f72016-07-26 12:56:32 -0700143
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600144static DEFINE_string(slide, "", "Start on this sample.");
145static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -0500146
bsalomon6c471f72016-07-26 12:56:32 -0700147#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -0700148# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -0700149#else
150# define BACKENDS_STR "\"sw\" and \"gl\""
151#endif
152
liyuqian71491dc2016-06-09 12:02:34 -0700153#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400154static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
155static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -0700156#else
Brian Osman2dd96932016-10-18 15:33:53 -0400157static DEFINE_string(skps, "skps", "Directory to read skps from.");
158static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -0700159#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700160
Brian Osman2dd96932016-10-18 15:33:53 -0400161static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -0700162
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600163static DEFINE_int32(msaa, 0, "Number of subpixel samples. 0 for no HW antialiasing.");
164static DEFINE_bool(cachePathMasks, true, "Allows path mask textures to be cached in GPU configs.");
csmartdalton008b9d82017-02-22 12:00:42 -0700165DEFINE_pathrenderer_flag;
166
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600167static DEFINE_bool(instancedRendering, false, "Enable instanced rendering on GPU backends.");
Brian Osman53136aa2017-07-20 15:43:35 -0400168DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -0500169
Brian Salomon194db172017-08-17 14:37:06 -0400170const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -0700171 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -0400172#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
173 "ANGLE",
174#endif
jvanverth063ece72016-06-17 09:29:14 -0700175#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700176 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700177#endif
csmartdalton578f0642017-02-24 16:04:47 -0700178 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700179};
180
bsalomon6c471f72016-07-26 12:56:32 -0700181static sk_app::Window::BackendType get_backend_type(const char* str) {
182#ifdef SK_VULKAN
183 if (0 == strcmp(str, "vk")) {
184 return sk_app::Window::kVulkan_BackendType;
185 } else
186#endif
Brian Salomon194db172017-08-17 14:37:06 -0400187#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
188 if (0 == strcmp(str, "angle")) {
189 return sk_app::Window::kANGLE_BackendType;
190 } else
191#endif
bsalomon6c471f72016-07-26 12:56:32 -0700192 if (0 == strcmp(str, "gl")) {
193 return sk_app::Window::kNativeGL_BackendType;
194 } else if (0 == strcmp(str, "sw")) {
195 return sk_app::Window::kRaster_BackendType;
196 } else {
197 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
198 return sk_app::Window::kRaster_BackendType;
199 }
200}
201
Brian Osmana109e392017-02-24 09:49:14 -0500202static SkColorSpacePrimaries gSrgbPrimaries = {
203 0.64f, 0.33f,
204 0.30f, 0.60f,
205 0.15f, 0.06f,
206 0.3127f, 0.3290f };
207
208static SkColorSpacePrimaries gAdobePrimaries = {
209 0.64f, 0.33f,
210 0.21f, 0.71f,
211 0.15f, 0.06f,
212 0.3127f, 0.3290f };
213
214static SkColorSpacePrimaries gP3Primaries = {
215 0.680f, 0.320f,
216 0.265f, 0.690f,
217 0.150f, 0.060f,
218 0.3127f, 0.3290f };
219
220static SkColorSpacePrimaries gRec2020Primaries = {
221 0.708f, 0.292f,
222 0.170f, 0.797f,
223 0.131f, 0.046f,
224 0.3127f, 0.3290f };
225
226struct NamedPrimaries {
227 const char* fName;
228 SkColorSpacePrimaries* fPrimaries;
229} gNamedPrimaries[] = {
230 { "sRGB", &gSrgbPrimaries },
231 { "AdobeRGB", &gAdobePrimaries },
232 { "P3", &gP3Primaries },
233 { "Rec. 2020", &gRec2020Primaries },
234};
235
236static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
237 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
238}
239
Brian Osman70d2f432017-11-08 09:54:10 -0500240static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
241 // In raster mode, we still use GL for the window.
242 // This lets us render the GUI faster (and correct).
243 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
244}
245
liyuqiane5a6cd92016-05-27 08:52:52 -0700246const char* kName = "name";
247const char* kValue = "value";
248const char* kOptions = "options";
249const char* kSlideStateName = "Slide";
250const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700251const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700252const char* kPathRendererStateName = "Path renderer";
Brian Salomon99a33902017-03-07 15:16:34 -0500253const char* kInstancedRenderingStateName = "Instanced rendering";
liyuqianb73c24b2016-06-03 08:47:23 -0700254const char* kSoftkeyStateName = "Softkey";
255const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700256const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700257const char* kON = "ON";
258const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700259const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700260
jvanverth34524262016-05-04 13:49:13 -0700261Viewer::Viewer(int argc, char** argv, void* platformData)
jvanverthc265a922016-04-08 12:51:45 -0700262 : fCurrentMeasurement(0)
Jim Van Verth90dcce52017-11-03 13:36:07 -0400263 , fCumulativeMeasurementTime(0)
264 , fCumulativeMeasurementCount(0)
jvanverthc265a922016-04-08 12:51:45 -0700265 , fDisplayStats(false)
liyuqian2edb0f42016-07-06 14:11:32 -0700266 , fRefresh(false)
Brian Osman79086b92017-02-10 13:36:16 -0500267 , fShowImGuiDebugWindow(false)
268 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500269 , fShowZoomWindow(false)
270 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700271 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500272 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500273 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500274 // Our UI can only tweak gamma (currently), so start out gamma-only
275 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700276 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400277 , fGestureDevice(GestureDevice::kNone)
jvanverthc265a922016-04-08 12:51:45 -0700278{
Greg Daniel285db442016-10-14 09:12:53 -0400279 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700280
281 static SkOnce initPathRendererNames;
282 initPathRendererNames([]() {
Brian Osman8b0f2652017-08-29 15:18:34 -0400283 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
284 gPathRendererNames[GpuPathRenderers::kDefault] =
285 "Default Ganesh Behavior (best path renderer, not including CCPR)";
csmartdalton61cd31a2017-02-27 17:00:53 -0700286 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
287 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
Jim Van Verth83010462017-03-16 08:45:39 -0400288 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Dalton1a325d22017-07-14 15:17:41 -0600289 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
Brian Osman8a9de3d2017-03-01 14:59:05 -0500290 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
csmartdalton61cd31a2017-02-27 17:00:53 -0700291 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
292 });
293
Brian Osman1df161a2017-02-09 12:10:20 -0500294 memset(fPaintTimes, 0, sizeof(fPaintTimes));
295 memset(fFlushTimes, 0, sizeof(fFlushTimes));
296 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
jvanverth9f372462016-04-06 06:08:59 -0700297
jvanverth2bb3b6d2016-04-08 07:24:09 -0700298 SkDebugf("Command line arguments: ");
299 for (int i = 1; i < argc; ++i) {
300 SkDebugf("%s ", argv[i]);
301 }
302 SkDebugf("\n");
303
304 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500305#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400306 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500307#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700308
Brian Osmanbc8150f2017-07-24 11:38:01 -0400309 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400310 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400311
bsalomon6c471f72016-07-26 12:56:32 -0700312 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700313 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700314
csmartdalton578f0642017-02-24 16:04:47 -0700315 DisplayParams displayParams;
316 displayParams.fMSAASampleCount = FLAGS_msaa;
Brian Salomon41eac792017-03-08 14:03:56 -0500317 displayParams.fGrContextOptions.fEnableInstancedRendering = FLAGS_instancedRendering;
csmartdalton61cd31a2017-02-27 17:00:53 -0700318 displayParams.fGrContextOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600319 displayParams.fGrContextOptions.fAllowPathMaskCaching = FLAGS_cachePathMasks;
Brian Osmanf9810662017-08-30 10:02:10 -0400320 displayParams.fGrContextOptions.fExecutor = GpuExecutorForTools();
csmartdalton578f0642017-02-24 16:04:47 -0700321 fWindow->setRequestedDisplayParams(displayParams);
322
jvanverth9f372462016-04-06 06:08:59 -0700323 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700324 fCommands.attach(fWindow);
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700325 fWindow->registerBackendCreatedFunc(on_backend_created_func, this);
jvanverth9f372462016-04-06 06:08:59 -0700326 fWindow->registerPaintFunc(on_paint_handler, this);
liyuqiand3cdbca2016-05-17 12:44:20 -0700327 fWindow->registerTouchFunc(on_touch_handler, this);
liyuqiane5a6cd92016-05-27 08:52:52 -0700328 fWindow->registerUIStateChangedFunc(on_ui_state_changed_handler, this);
Brian Osman79086b92017-02-10 13:36:16 -0500329 fWindow->registerMouseFunc(on_mouse_handler, this);
330 fWindow->registerMouseWheelFunc(on_mouse_wheel_handler, this);
331 fWindow->registerKeyFunc(on_key_handler, this);
332 fWindow->registerCharFunc(on_char_handler, this);
jvanverth9f372462016-04-06 06:08:59 -0700333
brianosman622c8d52016-05-10 06:50:49 -0700334 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500335 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
336 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
337 fWindow->inval();
338 });
339 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
340 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
341 fWindow->inval();
342 });
Brian Osmanf6877092017-02-13 09:39:57 -0500343 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
344 this->fShowZoomWindow = !this->fShowZoomWindow;
345 fWindow->inval();
346 });
brianosman622c8d52016-05-10 06:50:49 -0700347 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
348 this->fDisplayStats = !this->fDisplayStats;
349 fWindow->inval();
350 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400351 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
352 this->resetMeasurements();
353 this->updateTitle();
354 fWindow->inval();
355 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500356 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500357 switch (fColorMode) {
358 case ColorMode::kLegacy:
359 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
360 break;
361 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
362 this->setColorMode(ColorMode::kColorManagedSRGB8888);
363 break;
364 case ColorMode::kColorManagedSRGB8888:
365 this->setColorMode(ColorMode::kColorManagedLinearF16);
366 break;
367 case ColorMode::kColorManagedLinearF16:
368 this->setColorMode(ColorMode::kLegacy);
369 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500370 }
brianosman622c8d52016-05-10 06:50:49 -0700371 });
372 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
373 int previousSlide = fCurrentSlide;
374 fCurrentSlide++;
375 if (fCurrentSlide >= fSlides.count()) {
376 fCurrentSlide = 0;
377 }
378 this->setupCurrentSlide(previousSlide);
379 });
380 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
381 int previousSlide = fCurrentSlide;
382 fCurrentSlide--;
383 if (fCurrentSlide < 0) {
384 fCurrentSlide = fSlides.count() - 1;
385 }
386 this->setupCurrentSlide(previousSlide);
387 });
388 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
389 this->changeZoomLevel(1.f / 32.f);
390 fWindow->inval();
391 });
392 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
393 this->changeZoomLevel(-1.f / 32.f);
394 fWindow->inval();
395 });
jvanverthaf236b52016-05-20 06:01:06 -0700396 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400397 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
398 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500399 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400400#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
401 if (newBackend == sk_app::Window::kVulkan_BackendType) {
402 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
403 sk_app::Window::kBackendTypeCount);
404 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
405 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500406 }
407#endif
Brian Osman621491e2017-02-28 15:45:01 -0500408 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700409 });
brianosman622c8d52016-05-10 06:50:49 -0700410
Yuqian Li399b3c22017-08-03 11:08:15 -0400411 fCommands.addCommand('A', "AAA", "Toggle analytic AA", [this]() {
412 if (!gSkUseAnalyticAA) {
413 gSkUseAnalyticAA = true;
414 } else if (!gSkForceAnalyticAA) {
415 gSkForceAnalyticAA = true;
416 } else {
417 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
418 }
419 this->updateTitle();
420 fWindow->inval();
421 });
422 fCommands.addCommand('D', "DAA", "Toggle delta AA", [this]() {
423 if (!gSkUseDeltaAA) {
424 gSkUseDeltaAA = true;
425 } else if (!gSkForceDeltaAA) {
426 gSkForceDeltaAA = true;
427 } else {
428 gSkUseDeltaAA = gSkForceDeltaAA = false;
429 }
430 this->updateTitle();
431 fWindow->inval();
432 });
433
jvanverth2bb3b6d2016-04-08 07:24:09 -0700434 // set up slides
435 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500436 this->setStartupSlide();
437 if (FLAGS_list) {
438 this->listNames();
439 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700440
djsollen12d62a72016-04-21 07:59:44 -0700441 fAnimTimer.run();
442
Brian Osman79086b92017-02-10 13:36:16 -0500443 // ImGui initialization:
444 ImGuiIO& io = ImGui::GetIO();
445 io.DisplaySize.x = static_cast<float>(fWindow->width());
446 io.DisplaySize.y = static_cast<float>(fWindow->height());
447
448 // Keymap...
449 io.KeyMap[ImGuiKey_Tab] = (int)Window::Key::kTab;
450 io.KeyMap[ImGuiKey_LeftArrow] = (int)Window::Key::kLeft;
451 io.KeyMap[ImGuiKey_RightArrow] = (int)Window::Key::kRight;
452 io.KeyMap[ImGuiKey_UpArrow] = (int)Window::Key::kUp;
453 io.KeyMap[ImGuiKey_DownArrow] = (int)Window::Key::kDown;
454 io.KeyMap[ImGuiKey_PageUp] = (int)Window::Key::kPageUp;
455 io.KeyMap[ImGuiKey_PageDown] = (int)Window::Key::kPageDown;
456 io.KeyMap[ImGuiKey_Home] = (int)Window::Key::kHome;
457 io.KeyMap[ImGuiKey_End] = (int)Window::Key::kEnd;
458 io.KeyMap[ImGuiKey_Delete] = (int)Window::Key::kDelete;
459 io.KeyMap[ImGuiKey_Backspace] = (int)Window::Key::kBack;
460 io.KeyMap[ImGuiKey_Enter] = (int)Window::Key::kOK;
461 io.KeyMap[ImGuiKey_Escape] = (int)Window::Key::kEscape;
462 io.KeyMap[ImGuiKey_A] = (int)Window::Key::kA;
463 io.KeyMap[ImGuiKey_C] = (int)Window::Key::kC;
464 io.KeyMap[ImGuiKey_V] = (int)Window::Key::kV;
465 io.KeyMap[ImGuiKey_X] = (int)Window::Key::kX;
466 io.KeyMap[ImGuiKey_Y] = (int)Window::Key::kY;
467 io.KeyMap[ImGuiKey_Z] = (int)Window::Key::kZ;
468
469 int w, h;
470 unsigned char* pixels;
471 io.Fonts->GetTexDataAsAlpha8(&pixels, &w, &h);
472 SkImageInfo info = SkImageInfo::MakeA8(w, h);
473 SkPixmap pmap(info, pixels, info.minRowBytes());
Brian Osmanf6877092017-02-13 09:39:57 -0500474 SkMatrix localMatrix = SkMatrix::MakeScale(1.0f / w, 1.0f / h);
475 auto fontImage = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
Mike Reed0acd7952017-04-28 11:12:19 -0400476 auto fontShader = fontImage->makeShader(&localMatrix);
Brian Osmanf6877092017-02-13 09:39:57 -0500477 fImGuiFontPaint.setShader(fontShader);
478 fImGuiFontPaint.setColor(SK_ColorWHITE);
479 fImGuiFontPaint.setFilterQuality(kLow_SkFilterQuality);
480 io.Fonts->TexID = &fImGuiFontPaint;
Brian Osman79086b92017-02-10 13:36:16 -0500481
Brian Osmana109e392017-02-24 09:49:14 -0500482 auto gamutImage = GetResourceAsImage("gamut.png");
483 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400484 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500485 }
486 fImGuiGamutPaint.setColor(SK_ColorWHITE);
487 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
488
Brian Osman70d2f432017-11-08 09:54:10 -0500489 fWindow->attach(backend_type_for_window(fBackendType));
jvanverth9f372462016-04-06 06:08:59 -0700490}
491
jvanverth34524262016-05-04 13:49:13 -0700492void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700493 fAllSlideNames = Json::Value(Json::arrayValue);
494
jvanverth2bb3b6d2016-04-08 07:24:09 -0700495 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
496 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400497 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700498
499 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
500 sk_sp<Slide> slide(new GMSlide(gm.release()));
501 fSlides.push_back(slide);
502 }
503
504 gms = gms->next();
505 }
506
507 // reverse array
508 for (int i = 0; i < fSlides.count()/2; ++i) {
509 sk_sp<Slide> temp = fSlides[i];
510 fSlides[i] = fSlides[fSlides.count() - i - 1];
511 fSlides[fSlides.count() - i - 1] = temp;
512 }
513
jvanverthc7027ab2016-06-16 09:52:35 -0700514 // samples
515 const SkViewRegister* reg = SkViewRegister::Head();
516 while (reg) {
517 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700518 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
519 fSlides.push_back(slide);
520 }
jvanverthc7027ab2016-06-16 09:52:35 -0700521 reg = reg->next();
522 }
523
jvanverth2bb3b6d2016-04-08 07:24:09 -0700524 // SKPs
525 for (int i = 0; i < FLAGS_skps.count(); i++) {
526 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700527 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
528 continue;
529 }
530
jvanverth2bb3b6d2016-04-08 07:24:09 -0700531 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700532 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700533 if (slide) {
534 fSlides.push_back(slide);
535 }
536 } else {
537 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700538 SkString skpName;
539 while (it.next(&skpName)) {
540 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
541 continue;
542 }
543
544 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
545 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700546 if (slide) {
547 fSlides.push_back(slide);
548 }
549 }
550 }
551 }
liyuqian6f163d22016-06-13 12:26:45 -0700552
553 // JPGs
554 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
555 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
556 SkString jpgName;
557 while (it.next(&jpgName)) {
brianosmane1d20072016-07-12 09:07:33 -0700558 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) {
559 continue;
560 }
561
liyuqian6f163d22016-06-13 12:26:45 -0700562 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
563 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
564 if (slide) {
565 fSlides.push_back(slide);
566 }
567 }
568 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700569}
570
571
jvanverth34524262016-05-04 13:49:13 -0700572Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700573 fWindow->detach();
574 delete fWindow;
575}
576
brianosman05de2162016-05-06 13:28:57 -0700577void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700578 if (!fWindow) {
579 return;
580 }
581 if (fWindow->sampleCount() < 0) {
582 return; // Surface hasn't been created yet.
583 }
584
jvanverth34524262016-05-04 13:49:13 -0700585 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700586 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700587
Yuqian Li399b3c22017-08-03 11:08:15 -0400588 if (gSkUseDeltaAA) {
589 if (gSkForceDeltaAA) {
590 title.append(" <FDAA>");
591 } else {
592 title.append(" <DAA>");
593 }
594 } else if (gSkUseAnalyticAA) {
595 if (gSkForceAnalyticAA) {
596 title.append(" <FAAA>");
597 } else {
598 title.append(" <AAA>");
599 }
600 }
601
Brian Osman92004802017-03-06 11:47:26 -0500602 switch (fColorMode) {
603 case ColorMode::kLegacy:
604 title.append(" Legacy 8888");
605 break;
606 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
607 title.append(" ColorManaged 8888 (Nonlinear blending)");
608 break;
609 case ColorMode::kColorManagedSRGB8888:
610 title.append(" ColorManaged 8888");
611 break;
612 case ColorMode::kColorManagedLinearF16:
613 title.append(" ColorManaged F16");
614 break;
615 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500616
Brian Osman92004802017-03-06 11:47:26 -0500617 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500618 int curPrimaries = -1;
619 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
620 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
621 curPrimaries = i;
622 break;
623 }
624 }
625 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500626
627 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
628 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
629 }
brianosman05de2162016-05-06 13:28:57 -0700630 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500631
Jim Van Verth90dcce52017-11-03 13:36:07 -0400632 if (fDisplayStats) {
633 double ms = 0;
634 int count = 0;
635 int i = (fCurrentMeasurement + kMeasurementCount - 1) & (kMeasurementCount - 1);
636 do {
637 double inc = fAnimateTimes[i] + fPaintTimes[i] + fFlushTimes[i];
638 if (inc <= 0) {
639 break;
640 }
641 ms += inc;
642 ++count;
643 i = (i + kMeasurementCount - 1) & (kMeasurementCount - 1);
644 } while (i != fCurrentMeasurement);
645
646 title.appendf(" %8.4f ms -> %4.4f ms", ms / SkTMax(1, count),
647 fCumulativeMeasurementTime / SkTMax(1, fCumulativeMeasurementCount));
648 }
649
csmartdalton578f0642017-02-24 16:04:47 -0700650 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700651 title.append(kBackendTypeStrings[fBackendType]);
csmartdalton578f0642017-02-24 16:04:47 -0700652 if (int msaa = fWindow->sampleCount()) {
653 title.appendf(" MSAA: %i", msaa);
654 }
655 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700656
657 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400658 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700659 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
660 }
661
brianosman05de2162016-05-06 13:28:57 -0700662 fWindow->setTitle(title.c_str());
663}
664
Jim Van Verth6f449692017-02-14 15:16:46 -0500665void Viewer::setStartupSlide() {
666
667 if (!FLAGS_slide.isEmpty()) {
668 int count = fSlides.count();
669 for (int i = 0; i < count; i++) {
670 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
671 fCurrentSlide = i;
672 return;
673 }
674 }
675
676 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
677 this->listNames();
678 }
679
680 fCurrentSlide = 0;
681}
682
683void Viewer::listNames() {
684 int count = fSlides.count();
685 SkDebugf("All Slides:\n");
686 for (int i = 0; i < count; i++) {
687 SkDebugf(" %s\n", fSlides[i]->getName().c_str());
688 }
689}
690
Jim Van Verth90dcce52017-11-03 13:36:07 -0400691void Viewer::resetMeasurements() {
692 memset(fPaintTimes, 0, sizeof(fPaintTimes));
693 memset(fFlushTimes, 0, sizeof(fFlushTimes));
694 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
695 fCurrentMeasurement = 0;
696 fCumulativeMeasurementTime = 0;
697 fCumulativeMeasurementCount = 0;
698}
699
brianosman05de2162016-05-06 13:28:57 -0700700void Viewer::setupCurrentSlide(int previousSlide) {
liyuqiane5a6cd92016-05-27 08:52:52 -0700701 if (fCurrentSlide == previousSlide) {
702 return; // no change; do nothing
703 }
liyuqian6f163d22016-06-13 12:26:45 -0700704 // prepare dimensions for image slides
jvanverthc7027ab2016-06-16 09:52:35 -0700705 fSlides[fCurrentSlide]->load(SkIntToScalar(fWindow->width()), SkIntToScalar(fWindow->height()));
liyuqian6f163d22016-06-13 12:26:45 -0700706
liyuqiane46e4f02016-05-20 07:32:19 -0700707 fGesture.reset();
708 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700709
Brian Osman42bb6ac2017-06-05 08:46:04 -0400710 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
711 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
712 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
713
714 // Start with a matrix that scales the slide to the available screen space
715 if (fWindow->scaleContentToFit()) {
716 if (windowRect.width() > 0 && windowRect.height() > 0) {
717 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
liyuqiane46e4f02016-05-20 07:32:19 -0700718 }
719 }
720
Brian Osman42bb6ac2017-06-05 08:46:04 -0400721 // Prevent the user from dragging content so far outside the window they can't find it again
722 fGesture.setTransLimit(slideBounds, windowRect, fDefaultMatrix);
liyuqiane46e4f02016-05-20 07:32:19 -0700723
brianosman05de2162016-05-06 13:28:57 -0700724 this->updateTitle();
liyuqiane5a6cd92016-05-27 08:52:52 -0700725 this->updateUIState();
jvanverthc265a922016-04-08 12:51:45 -0700726 if (previousSlide >= 0) {
727 fSlides[previousSlide]->unload();
728 }
Jim Van Verth90dcce52017-11-03 13:36:07 -0400729
730 this->resetMeasurements();
731
jvanverthc265a922016-04-08 12:51:45 -0700732 fWindow->inval();
733}
734
735#define MAX_ZOOM_LEVEL 8
736#define MIN_ZOOM_LEVEL -8
737
jvanverth34524262016-05-04 13:49:13 -0700738void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700739 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400740 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
jvanverthc265a922016-04-08 12:51:45 -0700741}
742
liyuqiand3cdbca2016-05-17 12:44:20 -0700743SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700744 SkMatrix m;
jvanverthc265a922016-04-08 12:51:45 -0700745
Brian Osman42bb6ac2017-06-05 08:46:04 -0400746 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
747 : SK_Scalar1 + fZoomLevel;
748 m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700749 m.preConcat(fGesture.globalM());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400750 m.preConcat(fDefaultMatrix);
751 m.preScale(zoomScale, zoomScale);
jvanverthc265a922016-04-08 12:51:45 -0700752
liyuqiand3cdbca2016-05-17 12:44:20 -0700753 return m;
jvanverthc265a922016-04-08 12:51:45 -0700754}
755
Brian Osman621491e2017-02-28 15:45:01 -0500756void Viewer::setBackend(sk_app::Window::BackendType backendType) {
757 fBackendType = backendType;
758
759 fWindow->detach();
760
Brian Osman70d2f432017-11-08 09:54:10 -0500761#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400762 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
763 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500764 DisplayParams params = fWindow->getRequestedDisplayParams();
765 delete fWindow;
766 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500767
Brian Osman70d2f432017-11-08 09:54:10 -0500768 // re-register callbacks
769 fCommands.attach(fWindow);
770 fWindow->registerBackendCreatedFunc(on_backend_created_func, this);
771 fWindow->registerPaintFunc(on_paint_handler, this);
772 fWindow->registerTouchFunc(on_touch_handler, this);
773 fWindow->registerUIStateChangedFunc(on_ui_state_changed_handler, this);
774 fWindow->registerMouseFunc(on_mouse_handler, this);
775 fWindow->registerMouseWheelFunc(on_mouse_wheel_handler, this);
776 fWindow->registerKeyFunc(on_key_handler, this);
777 fWindow->registerCharFunc(on_char_handler, this);
778 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
779 // will still include our correct sample count. But the re-created fWindow will lose that
780 // information. On Windows, we need to re-create the window when changing sample count,
781 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
782 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
783 // as if we had never changed windows at all).
784 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500785#endif
786
Brian Osman70d2f432017-11-08 09:54:10 -0500787 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500788}
789
Brian Osman92004802017-03-06 11:47:26 -0500790void Viewer::setColorMode(ColorMode colorMode) {
791 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700792
Brian Osmanf750fbc2017-02-08 10:47:28 -0500793 // 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 -0400794 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700795 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500796 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400797 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
798 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500799 if (wasInLegacy != wantLegacy) {
800 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700801 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500802 }
803
804 this->updateTitle();
805 fWindow->inval();
806}
807
808void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500809 SkAutoCanvasRestore autorestore(canvas, false);
810
Brian Osmanf750fbc2017-02-08 10:47:28 -0500811 // By default, we render directly into the window's surface/canvas
812 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500813 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700814
Brian Osmane0d4fba2017-03-15 10:24:55 -0400815 // If we're in any of the color managed modes, construct the color space we're going to use
816 sk_sp<SkColorSpace> cs = nullptr;
817 if (ColorMode::kLegacy != fColorMode) {
818 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
819 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400820 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400821 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500822 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
823 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
824 } else {
825 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
826 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400827 }
828
829 // 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 -0500830 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
831 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500832 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman70d2f432017-11-08 09:54:10 -0500833 if (Window::kRaster_BackendType == fBackendType ||
834 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500835 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400836 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
837 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
838
Brian Osman92004802017-03-06 11:47:26 -0500839 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
840 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400841 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
842 // with a special canvas (below) that has the color space attached
843 sk_sp<SkColorSpace> offscreenColorSpace =
844 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500845 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400846 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500847 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
848 : canvas->makeSurface(info);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500849 slideCanvas = offscreenSurface->getCanvas();
850 }
851
Brian Osmane0d4fba2017-03-15 10:24:55 -0400852 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
853 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
854 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
855 slideCanvas = xformCanvas.get();
856 }
857
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500858 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500859 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500860 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500861 // Time the painting logic of the slide
862 double startTime = SkTime::GetMSecs();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500863 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osman1df161a2017-02-09 12:10:20 -0500864 fPaintTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500865 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500866
867 // Force a flush so we can time that, too
868 startTime = SkTime::GetMSecs();
869 slideCanvas->flush();
870 fFlushTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500871
872 // If we rendered offscreen, snap an image and push the results to the window's canvas
873 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500874 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500875
876 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400877 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500878 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400879 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500880 SkPaint paint;
881 paint.setBlendMode(SkBlendMode::kSrc);
882 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -0700883 }
liyuqian6f163d22016-06-13 12:26:45 -0700884}
885
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700886void Viewer::onBackendCreated() {
887 this->updateTitle();
888 this->updateUIState();
889 this->setupCurrentSlide(-1);
Jim Van Verth90dcce52017-11-03 13:36:07 -0400890 this->resetMeasurements();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700891 fWindow->show();
892 fWindow->inval();
893}
Jim Van Verth6f449692017-02-14 15:16:46 -0500894
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700895void Viewer::onPaint(SkCanvas* canvas) {
Brian Osman79086b92017-02-10 13:36:16 -0500896 // Update ImGui input
897 ImGuiIO& io = ImGui::GetIO();
898 io.DeltaTime = 1.0f / 60.0f;
899 io.DisplaySize.x = static_cast<float>(fWindow->width());
900 io.DisplaySize.y = static_cast<float>(fWindow->height());
901
902 io.KeyAlt = io.KeysDown[static_cast<int>(Window::Key::kOption)];
903 io.KeyCtrl = io.KeysDown[static_cast<int>(Window::Key::kCtrl)];
904 io.KeyShift = io.KeysDown[static_cast<int>(Window::Key::kShift)];
905
906 ImGui::NewFrame();
907
Jim Van Verth90dcce52017-11-03 13:36:07 -0400908 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -0700909
Brian Osman1df161a2017-02-09 12:10:20 -0500910 // Advance our timing bookkeeping
Jim Van Verth90dcce52017-11-03 13:36:07 -0400911 fCumulativeMeasurementTime += fAnimateTimes[fCurrentMeasurement] +
912 fPaintTimes[fCurrentMeasurement] +
913 fFlushTimes[fCurrentMeasurement];
914 fCumulativeMeasurementCount++;
Brian Osman1df161a2017-02-09 12:10:20 -0500915 fCurrentMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1);
916 SkASSERT(fCurrentMeasurement < kMeasurementCount);
917
918 // Draw any overlays or UI that we don't want timed
jvanverthc265a922016-04-08 12:51:45 -0700919 if (fDisplayStats) {
920 drawStats(canvas);
Jim Van Verth90dcce52017-11-03 13:36:07 -0400921 this->updateTitle();
jvanverthc265a922016-04-08 12:51:45 -0700922 }
brianosman622c8d52016-05-10 06:50:49 -0700923 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -0700924
Jim Van Verth90dcce52017-11-03 13:36:07 -0400925 this->drawImGui(canvas);
Brian Osman79086b92017-02-10 13:36:16 -0500926
Brian Osman1df161a2017-02-09 12:10:20 -0500927 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -0400928 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700929}
930
jvanverth814e38d2016-06-06 08:48:47 -0700931bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400932 if (GestureDevice::kMouse == fGestureDevice) {
933 return false;
934 }
liyuqiand3cdbca2016-05-17 12:44:20 -0700935 void* castedOwner = reinterpret_cast<void*>(owner);
936 switch (state) {
937 case Window::kUp_InputState: {
938 fGesture.touchEnd(castedOwner);
939 break;
940 }
941 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400942 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700943 break;
944 }
945 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400946 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700947 break;
948 }
949 }
Brian Osmanb53f48c2017-06-07 10:00:30 -0400950 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -0700951 fWindow->inval();
952 return true;
953}
954
Jim Van Verthe7705782017-05-04 14:00:59 -0400955bool Viewer::onMouse(float x, float y, Window::InputState state, uint32_t modifiers) {
Jim Van Verthc9e7f9c2017-11-02 09:46:49 -0400956 if (!fSlides[fCurrentSlide]->onMouse(x, y, state, modifiers)) {
957 if (GestureDevice::kTouch == fGestureDevice) {
958 return false;
959 }
960 switch (state) {
961 case Window::kUp_InputState: {
962 fGesture.touchEnd(nullptr);
963 break;
964 }
965 case Window::kDown_InputState: {
966 fGesture.touchBegin(nullptr, x, y);
967 break;
968 }
969 case Window::kMove_InputState: {
970 fGesture.touchMoved(nullptr, x, y);
971 break;
972 }
973 }
974 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
Brian Osmanb53f48c2017-06-07 10:00:30 -0400975 }
Jim Van Verthe7705782017-05-04 14:00:59 -0400976 fWindow->inval();
977 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;
982 static const int kDisplayWidth = 130;
983 static const int kDisplayHeight = 100;
984 static const int kDisplayPadding = 10;
985 static const int kGraphPadding = 3;
986 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
987
Mike Reed3661bc92017-02-22 13:21:42 -0500988 SkISize canvasSize = canvas->getBaseLayerSize();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700989 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
990 SkIntToScalar(kDisplayPadding),
991 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
992 SkPaint paint;
993 canvas->save();
994
995 canvas->clipRect(rect);
996 paint.setColor(SK_ColorBLACK);
997 canvas->drawRect(rect, paint);
998 // draw the 16ms line
999 paint.setColor(SK_ColorLTGRAY);
1000 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
1001 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
1002 paint.setColor(SK_ColorRED);
1003 paint.setStyle(SkPaint::kStroke_Style);
1004 canvas->drawRect(rect, paint);
1005
1006 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
1007 const int xStep = 2;
jvanverth3d6ed3a2016-04-07 11:09:51 -07001008 int i = fCurrentMeasurement;
1009 do {
Brian Osman1df161a2017-02-09 12:10:20 -05001010 // Round to nearest values
1011 int animateHeight = (int)(fAnimateTimes[i] * kPixelPerMS + 0.5);
1012 int paintHeight = (int)(fPaintTimes[i] * kPixelPerMS + 0.5);
1013 int flushHeight = (int)(fFlushTimes[i] * kPixelPerMS + 0.5);
1014 int startY = SkScalarTruncToInt(rect.fBottom);
1015 int endY = startY - flushHeight;
1016 paint.setColor(SK_ColorRED);
1017 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
1018 SkIntToScalar(x), SkIntToScalar(endY), paint);
1019 startY = endY;
1020 endY = startY - paintHeight;
1021 paint.setColor(SK_ColorGREEN);
1022 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
1023 SkIntToScalar(x), SkIntToScalar(endY), paint);
1024 startY = endY;
1025 endY = startY - animateHeight;
1026 paint.setColor(SK_ColorMAGENTA);
jvanverth3d6ed3a2016-04-07 11:09:51 -07001027 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
1028 SkIntToScalar(x), SkIntToScalar(endY), paint);
1029 i++;
1030 i &= (kMeasurementCount - 1); // fast mod
1031 x += xStep;
1032 } while (i != fCurrentMeasurement);
jvanverth9f372462016-04-06 06:08:59 -07001033
1034 canvas->restore();
1035}
1036
Brian Osmana109e392017-02-24 09:49:14 -05001037static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1038 const ImVec2& pos, const ImVec2& size) {
1039 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1040 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1041
1042 // Invisible 10x10 button
1043 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1044 ImGui::InvisibleButton(label, ImVec2(10, 10));
1045
1046 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1047 ImGuiIO& io = ImGui::GetIO();
1048 // Normalized mouse position, relative to our gamut box
1049 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1050 // Clamp to edge of box, convert back to primary scale
1051 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1052 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1053 }
1054
1055 if (ImGui::IsItemHovered()) {
1056 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1057 }
1058
1059 // Return screen coordinates for the caller. We could just return center here, but we'd have
1060 // one frame of lag during drag.
1061 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1062}
1063
1064static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1065 ImDrawList* drawList = ImGui::GetWindowDrawList();
1066
1067 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1068 // width, and scale the height to maintain aspect ratio.
1069 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1070 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1071 ImVec2 pos = ImGui::GetCursorScreenPos();
1072
1073 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1074 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1075 // Magic numbers are pixel locations of the origin and upper-right corner.
1076 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1077 ImVec2(242, 61), ImVec2(1897, 1922));
1078 ImVec2 endPos = ImGui::GetCursorPos();
1079
1080 // Primary markers
1081 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1082 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1083 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1084 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1085
1086 // Gamut triangle
1087 drawList->AddCircle(r, 5.0f, 0xFF000040);
1088 drawList->AddCircle(g, 5.0f, 0xFF004000);
1089 drawList->AddCircle(b, 5.0f, 0xFF400000);
1090 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1091 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1092
1093 // Re-position cursor immediate after the diagram for subsequent controls
1094 ImGui::SetCursorPos(endPos);
1095}
1096
Brian Osman79086b92017-02-10 13:36:16 -05001097void Viewer::drawImGui(SkCanvas* canvas) {
1098 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1099 if (fShowImGuiTestWindow) {
1100 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1101 }
1102
1103 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001104 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1105 // always visible, we can end up in a layout feedback loop.
1106 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001107 DisplayParams params = fWindow->getRequestedDisplayParams();
1108 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001109 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1110 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001111 if (ImGui::CollapsingHeader("Backend")) {
1112 int newBackend = static_cast<int>(fBackendType);
1113 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1114 ImGui::SameLine();
1115 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001116#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1117 ImGui::SameLine();
1118 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1119#endif
Brian Osman621491e2017-02-28 15:45:01 -05001120#if defined(SK_VULKAN)
1121 ImGui::SameLine();
1122 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1123#endif
1124 if (newBackend != fBackendType) {
1125 fDeferredActions.push_back([=]() {
1126 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1127 });
1128 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001129
Brian Salomon99a33902017-03-07 15:16:34 -05001130 const GrContext* ctx = fWindow->getGrContext();
1131 bool* inst = &params.fGrContextOptions.fEnableInstancedRendering;
1132 if (ctx && ImGui::Checkbox("Instanced Rendering", inst)) {
1133 paramsChanged = true;
1134 }
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001135 bool* wire = &params.fGrContextOptions.fWireframeMode;
1136 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1137 paramsChanged = true;
1138 }
Brian Salomon99a33902017-03-07 15:16:34 -05001139
Brian Osman28b12522017-03-08 17:10:24 -05001140 if (ctx) {
1141 int sampleCount = fWindow->sampleCount();
1142 ImGui::Text("MSAA: "); ImGui::SameLine();
1143 ImGui::RadioButton("0", &sampleCount, 0); ImGui::SameLine();
1144 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1145 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1146 ImGui::RadioButton("16", &sampleCount, 16);
1147
1148 if (sampleCount != params.fMSAASampleCount) {
1149 params.fMSAASampleCount = sampleCount;
1150 paramsChanged = true;
1151 }
1152 }
1153
Brian Osman8a9de3d2017-03-01 14:59:05 -05001154 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001155 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001156 auto prButton = [&](GpuPathRenderers x) {
1157 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001158 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1159 params.fGrContextOptions.fGpuPathRenderers = x;
1160 paramsChanged = true;
1161 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001162 }
1163 };
1164
1165 if (!ctx) {
1166 ImGui::RadioButton("Software", true);
1167 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001168 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001169 prButton(GpuPathRenderers::kAll);
1170 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1171 prButton(GpuPathRenderers::kStencilAndCover);
1172 }
1173 if (ctx->caps()->sampleShadingSupport()) {
1174 prButton(GpuPathRenderers::kMSAA);
1175 }
1176 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001177 prButton(GpuPathRenderers::kNone);
1178 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001179 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001180 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001181 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1182 prButton(GpuPathRenderers::kCoverageCounting);
1183 }
Jim Van Verth83010462017-03-16 08:45:39 -04001184 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001185 prButton(GpuPathRenderers::kTessellating);
1186 prButton(GpuPathRenderers::kNone);
1187 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001188 ImGui::TreePop();
1189 }
Brian Osman621491e2017-02-28 15:45:01 -05001190 }
1191
Brian Osman79086b92017-02-10 13:36:16 -05001192 if (ImGui::CollapsingHeader("Slide")) {
1193 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001194 static ImVector<const char*> filteredSlideNames;
1195 static ImVector<int> filteredSlideIndices;
1196
Brian Osman79086b92017-02-10 13:36:16 -05001197 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001198 filteredSlideNames.clear();
1199 filteredSlideIndices.clear();
1200 int filteredIndex = 0;
1201 for (int i = 0; i < fSlides.count(); ++i) {
1202 const char* slideName = fSlides[i]->getName().c_str();
1203 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1204 if (i == fCurrentSlide) {
1205 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001206 }
Brian Osmanf479e422017-11-08 13:11:36 -05001207 filteredSlideNames.push_back(slideName);
1208 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001209 }
Brian Osman79086b92017-02-10 13:36:16 -05001210 }
Brian Osmanf479e422017-11-08 13:11:36 -05001211
1212 int previousSlide = fCurrentSlide;
1213 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1214 filteredSlideNames.size(), 20)) {
1215 fCurrentSlide = filteredSlideIndices[filteredIndex];
1216 setupCurrentSlide(previousSlide);
Brian Osman79086b92017-02-10 13:36:16 -05001217 }
1218 }
Brian Osmana109e392017-02-24 09:49:14 -05001219
1220 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001221 ColorMode newMode = fColorMode;
1222 auto cmButton = [&](ColorMode mode, const char* label) {
1223 if (ImGui::RadioButton(label, mode == fColorMode)) {
1224 newMode = mode;
1225 }
1226 };
1227
1228 cmButton(ColorMode::kLegacy, "Legacy 8888");
1229 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1230 "Color Managed 8888 (Nonlinear blending)");
1231 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1232 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1233
1234 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001235 // It isn't safe to switch color mode now (in the middle of painting). We might
1236 // tear down the back-end, etc... Defer this change until the next onIdle.
1237 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001238 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001239 });
Brian Osmana109e392017-02-24 09:49:14 -05001240 }
1241
1242 // Pick from common gamuts:
1243 int primariesIdx = 4; // Default: Custom
1244 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1245 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1246 primariesIdx = i;
1247 break;
1248 }
1249 }
1250
Brian Osmanfdab5762017-11-09 10:27:55 -05001251 // When we're in xform canvas mode, we can alter the transfer function, too
1252 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1253 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1254 }
1255
Brian Osmana109e392017-02-24 09:49:14 -05001256 if (ImGui::Combo("Primaries", &primariesIdx,
1257 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1258 if (primariesIdx >= 0 && primariesIdx <= 3) {
1259 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1260 }
1261 }
1262
1263 // Allow direct editing of gamut
1264 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1265 }
Brian Osman79086b92017-02-10 13:36:16 -05001266 }
Brian Salomon99a33902017-03-07 15:16:34 -05001267 if (paramsChanged) {
1268 fDeferredActions.push_back([=]() {
1269 fWindow->setRequestedDisplayParams(params);
1270 fWindow->inval();
1271 this->updateTitle();
1272 });
1273 }
Brian Osman79086b92017-02-10 13:36:16 -05001274 ImGui::End();
1275 }
1276
Brian Osmanf6877092017-02-13 09:39:57 -05001277 SkPaint zoomImagePaint;
1278 if (fShowZoomWindow && fLastImage) {
1279 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
1280 static int zoomFactor = 4;
1281 ImGui::SliderInt("Scale", &zoomFactor, 1, 16);
1282
Mike Reed0acd7952017-04-28 11:12:19 -04001283 zoomImagePaint.setShader(fLastImage->makeShader());
Brian Osmanf6877092017-02-13 09:39:57 -05001284 zoomImagePaint.setColor(SK_ColorWHITE);
1285
1286 // Zoom by shrinking the corner UVs towards the mouse cursor
1287 ImVec2 mousePos = ImGui::GetMousePos();
1288 ImVec2 avail = ImGui::GetContentRegionAvail();
1289
1290 ImVec2 zoomHalfExtents = ImVec2((avail.x * 0.5f) / zoomFactor,
1291 (avail.y * 0.5f) / zoomFactor);
1292 ImGui::Image(&zoomImagePaint, avail,
1293 ImVec2(mousePos.x - zoomHalfExtents.x, mousePos.y - zoomHalfExtents.y),
1294 ImVec2(mousePos.x + zoomHalfExtents.x, mousePos.y + zoomHalfExtents.y));
1295 }
1296
1297 ImGui::End();
1298 }
1299
Brian Osman79086b92017-02-10 13:36:16 -05001300 // This causes ImGui to rebuild vertex/index data based on all immediate-mode commands
1301 // (widgets, etc...) that have been issued
1302 ImGui::Render();
1303
1304 // Then we fetch the most recent data, and convert it so we can render with Skia
1305 const ImDrawData* drawData = ImGui::GetDrawData();
1306 SkTDArray<SkPoint> pos;
1307 SkTDArray<SkPoint> uv;
1308 SkTDArray<SkColor> color;
Brian Osman79086b92017-02-10 13:36:16 -05001309
1310 for (int i = 0; i < drawData->CmdListsCount; ++i) {
1311 const ImDrawList* drawList = drawData->CmdLists[i];
1312
1313 // De-interleave all vertex data (sigh), convert to Skia types
1314 pos.rewind(); uv.rewind(); color.rewind();
1315 for (int i = 0; i < drawList->VtxBuffer.size(); ++i) {
1316 const ImDrawVert& vert = drawList->VtxBuffer[i];
1317 pos.push(SkPoint::Make(vert.pos.x, vert.pos.y));
1318 uv.push(SkPoint::Make(vert.uv.x, vert.uv.y));
1319 color.push(vert.col);
1320 }
1321 // ImGui colors are RGBA
1322 SkSwapRB(color.begin(), color.begin(), color.count());
1323
1324 int indexOffset = 0;
1325
1326 // Draw everything with canvas.drawVertices...
1327 for (int j = 0; j < drawList->CmdBuffer.size(); ++j) {
1328 const ImDrawCmd* drawCmd = &drawList->CmdBuffer[j];
1329
1330 // TODO: Find min/max index for each draw, so we know how many vertices (sigh)
1331 if (drawCmd->UserCallback) {
1332 drawCmd->UserCallback(drawList, drawCmd);
1333 } else {
Brian Osmanf6877092017-02-13 09:39:57 -05001334 SkPaint* paint = static_cast<SkPaint*>(drawCmd->TextureId);
1335 SkASSERT(paint);
1336
Brian Osman79086b92017-02-10 13:36:16 -05001337 canvas->save();
1338 canvas->clipRect(SkRect::MakeLTRB(drawCmd->ClipRect.x, drawCmd->ClipRect.y,
1339 drawCmd->ClipRect.z, drawCmd->ClipRect.w));
Mike Reed887cdf12017-04-03 11:11:09 -04001340 canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode,
1341 drawList->VtxBuffer.size(), pos.begin(),
1342 uv.begin(), color.begin(),
1343 drawCmd->ElemCount,
1344 drawList->IdxBuffer.begin() + indexOffset),
1345 SkBlendMode::kModulate, *paint);
Brian Osman79086b92017-02-10 13:36:16 -05001346 indexOffset += drawCmd->ElemCount;
1347 canvas->restore();
1348 }
1349 }
1350 }
1351}
1352
liyuqian2edb0f42016-07-06 14:11:32 -07001353void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001354 for (int i = 0; i < fDeferredActions.count(); ++i) {
1355 fDeferredActions[i]();
1356 }
1357 fDeferredActions.reset();
1358
Brian Osman1df161a2017-02-09 12:10:20 -05001359 double startTime = SkTime::GetMSecs();
jvanverthc265a922016-04-08 12:51:45 -07001360 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001361 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
1362 fAnimateTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
1363
Brian Osman79086b92017-02-10 13:36:16 -05001364 ImGuiIO& io = ImGui::GetIO();
1365 if (animateWantsInval || fDisplayStats || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001366 fWindow->inval();
1367 }
jvanverth9f372462016-04-06 06:08:59 -07001368}
liyuqiane5a6cd92016-05-27 08:52:52 -07001369
1370void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001371 if (!fWindow) {
1372 return;
1373 }
1374 if (fWindow->sampleCount() < 0) {
1375 return; // Surface hasn't been created yet.
1376 }
1377
liyuqianb73c24b2016-06-03 08:47:23 -07001378 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001379 Json::Value slideState(Json::objectValue);
1380 slideState[kName] = kSlideStateName;
1381 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001382 if (fAllSlideNames.size() == 0) {
1383 for(auto slide : fSlides) {
1384 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1385 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001386 }
liyuqian1f508fd2016-06-07 06:57:40 -07001387 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001388
liyuqianb73c24b2016-06-03 08:47:23 -07001389 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001390 Json::Value backendState(Json::objectValue);
1391 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001392 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001393 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001394 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001395 backendState[kOptions].append(Json::Value(str));
1396 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001397
csmartdalton578f0642017-02-24 16:04:47 -07001398 // MSAA state
1399 Json::Value msaaState(Json::objectValue);
1400 msaaState[kName] = kMSAAStateName;
1401 msaaState[kValue] = fWindow->sampleCount();
1402 msaaState[kOptions] = Json::Value(Json::arrayValue);
1403 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1404 msaaState[kOptions].append(Json::Value(0));
1405 } else {
1406 for (int msaa : {0, 4, 8, 16}) {
1407 msaaState[kOptions].append(Json::Value(msaa));
1408 }
1409 }
1410
csmartdalton61cd31a2017-02-27 17:00:53 -07001411 // Path renderer state
1412 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1413 Json::Value prState(Json::objectValue);
1414 prState[kName] = kPathRendererStateName;
1415 prState[kValue] = gPathRendererNames[pr];
1416 prState[kOptions] = Json::Value(Json::arrayValue);
1417 const GrContext* ctx = fWindow->getGrContext();
1418 if (!ctx) {
1419 prState[kOptions].append("Software");
1420 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001421 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001422 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1423 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1424 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1425 }
1426 if (ctx->caps()->sampleShadingSupport()) {
1427 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1428 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001429 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001430 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1431 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001432 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001433 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001434 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1435 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1436 }
Jim Van Verth83010462017-03-16 08:45:39 -04001437 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001438 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001439 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1440 }
1441
Brian Salomon99a33902017-03-07 15:16:34 -05001442 // Instanced rendering state
1443 Json::Value instState(Json::objectValue);
1444 instState[kName] = kInstancedRenderingStateName;
Yuqian Lid9020422017-07-25 15:01:53 -04001445 instState[kValue] = kOFF;
1446 instState[kOptions] = Json::Value(Json::arrayValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001447 if (ctx) {
1448 if (fWindow->getRequestedDisplayParams().fGrContextOptions.fEnableInstancedRendering) {
1449 instState[kValue] = kON;
Brian Salomon99a33902017-03-07 15:16:34 -05001450 }
Brian Salomon99a33902017-03-07 15:16:34 -05001451 instState[kOptions].append(kOFF);
1452 instState[kOptions].append(kON);
1453 }
1454
liyuqianb73c24b2016-06-03 08:47:23 -07001455 // Softkey state
1456 Json::Value softkeyState(Json::objectValue);
1457 softkeyState[kName] = kSoftkeyStateName;
1458 softkeyState[kValue] = kSoftkeyHint;
1459 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1460 softkeyState[kOptions].append(kSoftkeyHint);
1461 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1462 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1463 }
1464
liyuqian1f508fd2016-06-07 06:57:40 -07001465 // FPS state
1466 Json::Value fpsState(Json::objectValue);
1467 fpsState[kName] = kFpsStateName;
Brian Osman1df161a2017-02-09 12:10:20 -05001468 int idx = (fCurrentMeasurement + (kMeasurementCount - 1)) & (kMeasurementCount - 1);
1469 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
1470 fAnimateTimes[idx] + fPaintTimes[idx] + fFlushTimes[idx],
1471 fAnimateTimes[idx],
1472 fPaintTimes[idx],
1473 fFlushTimes[idx]).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001474 fpsState[kOptions] = Json::Value(Json::arrayValue);
1475
liyuqiane5a6cd92016-05-27 08:52:52 -07001476 Json::Value state(Json::arrayValue);
1477 state.append(slideState);
1478 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001479 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001480 state.append(prState);
Brian Salomon99a33902017-03-07 15:16:34 -05001481 state.append(instState);
liyuqianb73c24b2016-06-03 08:47:23 -07001482 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001483 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001484
1485 fWindow->setUIState(state);
1486}
1487
1488void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001489 // For those who will add more features to handle the state change in this function:
1490 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1491 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1492 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001493 if (stateName.equals(kSlideStateName)) {
1494 int previousSlide = fCurrentSlide;
1495 fCurrentSlide = 0;
1496 for(auto slide : fSlides) {
1497 if (slide->getName().equals(stateValue)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001498 this->setupCurrentSlide(previousSlide);
liyuqiane5a6cd92016-05-27 08:52:52 -07001499 break;
1500 }
1501 fCurrentSlide++;
1502 }
1503 if (fCurrentSlide >= fSlides.count()) {
1504 fCurrentSlide = previousSlide;
1505 SkDebugf("Slide not found: %s", stateValue.c_str());
1506 }
liyuqian6cb70252016-06-02 12:16:25 -07001507 } else if (stateName.equals(kBackendStateName)) {
1508 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1509 if (stateValue.equals(kBackendTypeStrings[i])) {
1510 if (fBackendType != i) {
1511 fBackendType = (sk_app::Window::BackendType)i;
1512 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001513 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001514 }
1515 break;
1516 }
1517 }
csmartdalton578f0642017-02-24 16:04:47 -07001518 } else if (stateName.equals(kMSAAStateName)) {
1519 DisplayParams params = fWindow->getRequestedDisplayParams();
1520 int sampleCount = atoi(stateValue.c_str());
1521 if (sampleCount != params.fMSAASampleCount) {
1522 params.fMSAASampleCount = sampleCount;
1523 fWindow->setRequestedDisplayParams(params);
1524 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001525 this->updateTitle();
1526 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001527 }
1528 } else if (stateName.equals(kPathRendererStateName)) {
1529 DisplayParams params = fWindow->getRequestedDisplayParams();
1530 for (const auto& pair : gPathRendererNames) {
1531 if (pair.second == stateValue.c_str()) {
1532 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1533 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1534 fWindow->setRequestedDisplayParams(params);
1535 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001536 this->updateTitle();
1537 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001538 }
1539 break;
1540 }
csmartdalton578f0642017-02-24 16:04:47 -07001541 }
Brian Salomon99a33902017-03-07 15:16:34 -05001542 } else if (stateName.equals(kInstancedRenderingStateName)) {
1543 DisplayParams params = fWindow->getRequestedDisplayParams();
1544 bool value = !strcmp(stateValue.c_str(), kON);
1545 if (params.fGrContextOptions.fEnableInstancedRendering != value) {
1546 params.fGrContextOptions.fEnableInstancedRendering = value;
1547 fWindow->setRequestedDisplayParams(params);
1548 fWindow->inval();
1549 this->updateTitle();
1550 this->updateUIState();
1551 }
liyuqianb73c24b2016-06-03 08:47:23 -07001552 } else if (stateName.equals(kSoftkeyStateName)) {
1553 if (!stateValue.equals(kSoftkeyHint)) {
1554 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001555 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001556 }
liyuqian2edb0f42016-07-06 14:11:32 -07001557 } else if (stateName.equals(kRefreshStateName)) {
1558 // This state is actually NOT in the UI state.
1559 // We use this to allow Android to quickly set bool fRefresh.
1560 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001561 } else {
1562 SkDebugf("Unknown stateName: %s", stateName.c_str());
1563 }
1564}
Brian Osman79086b92017-02-10 13:36:16 -05001565
1566bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
1567 return fCommands.onKey(key, state, modifiers);
1568}
1569
1570bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001571 if (fSlides[fCurrentSlide]->onChar(c)) {
1572 fWindow->inval();
1573 return true;
1574 }
1575
Brian Osman79086b92017-02-10 13:36:16 -05001576 return fCommands.onChar(c, modifiers);
1577}