blob: 6490d6342f139eec4aaa9f2b71d0e95021e70e9b [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
csmartdalton578f0642017-02-24 16:04:47 -0700632 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700633 title.append(kBackendTypeStrings[fBackendType]);
csmartdalton578f0642017-02-24 16:04:47 -0700634 if (int msaa = fWindow->sampleCount()) {
635 title.appendf(" MSAA: %i", msaa);
636 }
637 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700638
639 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400640 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700641 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
642 }
643
brianosman05de2162016-05-06 13:28:57 -0700644 fWindow->setTitle(title.c_str());
645}
646
Jim Van Verth6f449692017-02-14 15:16:46 -0500647void Viewer::setStartupSlide() {
648
649 if (!FLAGS_slide.isEmpty()) {
650 int count = fSlides.count();
651 for (int i = 0; i < count; i++) {
652 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
653 fCurrentSlide = i;
654 return;
655 }
656 }
657
658 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
659 this->listNames();
660 }
661
662 fCurrentSlide = 0;
663}
664
665void Viewer::listNames() {
666 int count = fSlides.count();
667 SkDebugf("All Slides:\n");
668 for (int i = 0; i < count; i++) {
669 SkDebugf(" %s\n", fSlides[i]->getName().c_str());
670 }
671}
672
Jim Van Verth90dcce52017-11-03 13:36:07 -0400673void Viewer::resetMeasurements() {
674 memset(fPaintTimes, 0, sizeof(fPaintTimes));
675 memset(fFlushTimes, 0, sizeof(fFlushTimes));
676 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
677 fCurrentMeasurement = 0;
678 fCumulativeMeasurementTime = 0;
679 fCumulativeMeasurementCount = 0;
680}
681
brianosman05de2162016-05-06 13:28:57 -0700682void Viewer::setupCurrentSlide(int previousSlide) {
liyuqiane5a6cd92016-05-27 08:52:52 -0700683 if (fCurrentSlide == previousSlide) {
684 return; // no change; do nothing
685 }
liyuqian6f163d22016-06-13 12:26:45 -0700686 // prepare dimensions for image slides
jvanverthc7027ab2016-06-16 09:52:35 -0700687 fSlides[fCurrentSlide]->load(SkIntToScalar(fWindow->width()), SkIntToScalar(fWindow->height()));
liyuqian6f163d22016-06-13 12:26:45 -0700688
liyuqiane46e4f02016-05-20 07:32:19 -0700689 fGesture.reset();
690 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700691
Brian Osman42bb6ac2017-06-05 08:46:04 -0400692 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
693 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
694 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
695
696 // Start with a matrix that scales the slide to the available screen space
697 if (fWindow->scaleContentToFit()) {
698 if (windowRect.width() > 0 && windowRect.height() > 0) {
699 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
liyuqiane46e4f02016-05-20 07:32:19 -0700700 }
701 }
702
Brian Osman42bb6ac2017-06-05 08:46:04 -0400703 // Prevent the user from dragging content so far outside the window they can't find it again
704 fGesture.setTransLimit(slideBounds, windowRect, fDefaultMatrix);
liyuqiane46e4f02016-05-20 07:32:19 -0700705
brianosman05de2162016-05-06 13:28:57 -0700706 this->updateTitle();
liyuqiane5a6cd92016-05-27 08:52:52 -0700707 this->updateUIState();
jvanverthc265a922016-04-08 12:51:45 -0700708 if (previousSlide >= 0) {
709 fSlides[previousSlide]->unload();
710 }
Jim Van Verth90dcce52017-11-03 13:36:07 -0400711
712 this->resetMeasurements();
713
jvanverthc265a922016-04-08 12:51:45 -0700714 fWindow->inval();
715}
716
717#define MAX_ZOOM_LEVEL 8
718#define MIN_ZOOM_LEVEL -8
719
jvanverth34524262016-05-04 13:49:13 -0700720void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700721 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400722 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
jvanverthc265a922016-04-08 12:51:45 -0700723}
724
liyuqiand3cdbca2016-05-17 12:44:20 -0700725SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700726 SkMatrix m;
jvanverthc265a922016-04-08 12:51:45 -0700727
Brian Osman42bb6ac2017-06-05 08:46:04 -0400728 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
729 : SK_Scalar1 + fZoomLevel;
730 m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700731 m.preConcat(fGesture.globalM());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400732 m.preConcat(fDefaultMatrix);
733 m.preScale(zoomScale, zoomScale);
jvanverthc265a922016-04-08 12:51:45 -0700734
liyuqiand3cdbca2016-05-17 12:44:20 -0700735 return m;
jvanverthc265a922016-04-08 12:51:45 -0700736}
737
Brian Osman621491e2017-02-28 15:45:01 -0500738void Viewer::setBackend(sk_app::Window::BackendType backendType) {
739 fBackendType = backendType;
740
741 fWindow->detach();
742
Brian Osman70d2f432017-11-08 09:54:10 -0500743#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400744 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
745 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500746 DisplayParams params = fWindow->getRequestedDisplayParams();
747 delete fWindow;
748 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500749
Brian Osman70d2f432017-11-08 09:54:10 -0500750 // re-register callbacks
751 fCommands.attach(fWindow);
752 fWindow->registerBackendCreatedFunc(on_backend_created_func, this);
753 fWindow->registerPaintFunc(on_paint_handler, this);
754 fWindow->registerTouchFunc(on_touch_handler, this);
755 fWindow->registerUIStateChangedFunc(on_ui_state_changed_handler, this);
756 fWindow->registerMouseFunc(on_mouse_handler, this);
757 fWindow->registerMouseWheelFunc(on_mouse_wheel_handler, this);
758 fWindow->registerKeyFunc(on_key_handler, this);
759 fWindow->registerCharFunc(on_char_handler, this);
760 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
761 // will still include our correct sample count. But the re-created fWindow will lose that
762 // information. On Windows, we need to re-create the window when changing sample count,
763 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
764 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
765 // as if we had never changed windows at all).
766 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500767#endif
768
Brian Osman70d2f432017-11-08 09:54:10 -0500769 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500770}
771
Brian Osman92004802017-03-06 11:47:26 -0500772void Viewer::setColorMode(ColorMode colorMode) {
773 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700774
Brian Osmanf750fbc2017-02-08 10:47:28 -0500775 // 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 -0400776 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700777 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500778 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400779 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
780 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500781 if (wasInLegacy != wantLegacy) {
782 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700783 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500784 }
785
786 this->updateTitle();
787 fWindow->inval();
788}
789
790void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500791 SkAutoCanvasRestore autorestore(canvas, false);
792
Brian Osmanf750fbc2017-02-08 10:47:28 -0500793 // By default, we render directly into the window's surface/canvas
794 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500795 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700796
Brian Osmane0d4fba2017-03-15 10:24:55 -0400797 // If we're in any of the color managed modes, construct the color space we're going to use
798 sk_sp<SkColorSpace> cs = nullptr;
799 if (ColorMode::kLegacy != fColorMode) {
800 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
801 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400802 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400803 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500804 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
805 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
806 } else {
807 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
808 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400809 }
810
811 // 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 -0500812 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
813 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500814 sk_sp<SkSurface> offscreenSurface = nullptr;
Brian Osman70d2f432017-11-08 09:54:10 -0500815 if (Window::kRaster_BackendType == fBackendType ||
816 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500817 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400818 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
819 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
820
Brian Osman92004802017-03-06 11:47:26 -0500821 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
822 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400823 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
824 // with a special canvas (below) that has the color space attached
825 sk_sp<SkColorSpace> offscreenColorSpace =
826 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500827 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400828 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500829 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
830 : canvas->makeSurface(info);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500831 slideCanvas = offscreenSurface->getCanvas();
832 }
833
Brian Osmane0d4fba2017-03-15 10:24:55 -0400834 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
835 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
836 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
837 slideCanvas = xformCanvas.get();
838 }
839
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500840 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500841 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500842 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500843 // Time the painting logic of the slide
844 double startTime = SkTime::GetMSecs();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500845 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osman1df161a2017-02-09 12:10:20 -0500846 fPaintTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500847 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500848
849 // Force a flush so we can time that, too
850 startTime = SkTime::GetMSecs();
851 slideCanvas->flush();
852 fFlushTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500853
854 // If we rendered offscreen, snap an image and push the results to the window's canvas
855 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500856 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500857
858 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400859 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500860 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400861 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500862 SkPaint paint;
863 paint.setBlendMode(SkBlendMode::kSrc);
864 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -0700865 }
liyuqian6f163d22016-06-13 12:26:45 -0700866}
867
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700868void Viewer::onBackendCreated() {
869 this->updateTitle();
870 this->updateUIState();
871 this->setupCurrentSlide(-1);
Jim Van Verth90dcce52017-11-03 13:36:07 -0400872 this->resetMeasurements();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700873 fWindow->show();
874 fWindow->inval();
875}
Jim Van Verth6f449692017-02-14 15:16:46 -0500876
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700877void Viewer::onPaint(SkCanvas* canvas) {
Brian Osman79086b92017-02-10 13:36:16 -0500878 // Update ImGui input
879 ImGuiIO& io = ImGui::GetIO();
880 io.DeltaTime = 1.0f / 60.0f;
881 io.DisplaySize.x = static_cast<float>(fWindow->width());
882 io.DisplaySize.y = static_cast<float>(fWindow->height());
883
884 io.KeyAlt = io.KeysDown[static_cast<int>(Window::Key::kOption)];
885 io.KeyCtrl = io.KeysDown[static_cast<int>(Window::Key::kCtrl)];
886 io.KeyShift = io.KeysDown[static_cast<int>(Window::Key::kShift)];
887
888 ImGui::NewFrame();
889
Jim Van Verth90dcce52017-11-03 13:36:07 -0400890 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -0700891
Brian Osman1df161a2017-02-09 12:10:20 -0500892 // Advance our timing bookkeeping
Jim Van Verth90dcce52017-11-03 13:36:07 -0400893 fCumulativeMeasurementTime += fAnimateTimes[fCurrentMeasurement] +
894 fPaintTimes[fCurrentMeasurement] +
895 fFlushTimes[fCurrentMeasurement];
896 fCumulativeMeasurementCount++;
Brian Osman1df161a2017-02-09 12:10:20 -0500897 fCurrentMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1);
898 SkASSERT(fCurrentMeasurement < kMeasurementCount);
899
900 // Draw any overlays or UI that we don't want timed
jvanverthc265a922016-04-08 12:51:45 -0700901 if (fDisplayStats) {
902 drawStats(canvas);
903 }
brianosman622c8d52016-05-10 06:50:49 -0700904 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -0700905
Jim Van Verth90dcce52017-11-03 13:36:07 -0400906 this->drawImGui(canvas);
Brian Osman79086b92017-02-10 13:36:16 -0500907
Brian Osman1df161a2017-02-09 12:10:20 -0500908 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -0400909 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700910}
911
jvanverth814e38d2016-06-06 08:48:47 -0700912bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400913 if (GestureDevice::kMouse == fGestureDevice) {
914 return false;
915 }
liyuqiand3cdbca2016-05-17 12:44:20 -0700916 void* castedOwner = reinterpret_cast<void*>(owner);
917 switch (state) {
918 case Window::kUp_InputState: {
919 fGesture.touchEnd(castedOwner);
920 break;
921 }
922 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400923 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700924 break;
925 }
926 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400927 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700928 break;
929 }
930 }
Brian Osmanb53f48c2017-06-07 10:00:30 -0400931 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -0700932 fWindow->inval();
933 return true;
934}
935
Jim Van Verthe7705782017-05-04 14:00:59 -0400936bool Viewer::onMouse(float x, float y, Window::InputState state, uint32_t modifiers) {
Jim Van Verthc9e7f9c2017-11-02 09:46:49 -0400937 if (!fSlides[fCurrentSlide]->onMouse(x, y, state, modifiers)) {
938 if (GestureDevice::kTouch == fGestureDevice) {
939 return false;
940 }
941 switch (state) {
942 case Window::kUp_InputState: {
943 fGesture.touchEnd(nullptr);
944 break;
945 }
946 case Window::kDown_InputState: {
947 fGesture.touchBegin(nullptr, x, y);
948 break;
949 }
950 case Window::kMove_InputState: {
951 fGesture.touchMoved(nullptr, x, y);
952 break;
953 }
954 }
955 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
Brian Osmanb53f48c2017-06-07 10:00:30 -0400956 }
Jim Van Verthe7705782017-05-04 14:00:59 -0400957 fWindow->inval();
958 return true;
959}
960
jvanverth34524262016-05-04 13:49:13 -0700961void Viewer::drawStats(SkCanvas* canvas) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700962 static const float kPixelPerMS = 2.0f;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500963 static const int kDisplayWidth = 192;
964 static const int kGraphHeight = 100;
965 static const int kTextHeight = 60;
966 static const int kDisplayHeight = kGraphHeight + kTextHeight;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700967 static const int kDisplayPadding = 10;
968 static const int kGraphPadding = 3;
969 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
970
Mike Reed3661bc92017-02-22 13:21:42 -0500971 SkISize canvasSize = canvas->getBaseLayerSize();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700972 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
973 SkIntToScalar(kDisplayPadding),
974 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
975 SkPaint paint;
976 canvas->save();
977
jvanverth3d6ed3a2016-04-07 11:09:51 -0700978 paint.setColor(SK_ColorBLACK);
979 canvas->drawRect(rect, paint);
980 // draw the 16ms line
981 paint.setColor(SK_ColorLTGRAY);
982 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
983 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
984 paint.setColor(SK_ColorRED);
985 paint.setStyle(SkPaint::kStroke_Style);
986 canvas->drawRect(rect, paint);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500987 paint.setStyle(SkPaint::kFill_Style);
jvanverth3d6ed3a2016-04-07 11:09:51 -0700988
989 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500990 const int xStep = 3;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700991 int i = fCurrentMeasurement;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500992 double ms = 0;
993 double animateMS = 0;
994 double paintMS = 0;
995 double flushMS = 0;
996 int count = 0;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700997 do {
Brian Osman1df161a2017-02-09 12:10:20 -0500998 // Round to nearest values
999 int animateHeight = (int)(fAnimateTimes[i] * kPixelPerMS + 0.5);
1000 int paintHeight = (int)(fPaintTimes[i] * kPixelPerMS + 0.5);
1001 int flushHeight = (int)(fFlushTimes[i] * kPixelPerMS + 0.5);
1002 int startY = SkScalarTruncToInt(rect.fBottom);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001003 int endY = SkTMax(startY - flushHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -05001004 paint.setColor(SK_ColorRED);
1005 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
1006 SkIntToScalar(x), SkIntToScalar(endY), paint);
1007 startY = endY;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001008 endY = SkTMax(startY - paintHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -05001009 paint.setColor(SK_ColorGREEN);
1010 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
1011 SkIntToScalar(x), SkIntToScalar(endY), paint);
1012 startY = endY;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001013 endY = SkTMax(startY - animateHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -05001014 paint.setColor(SK_ColorMAGENTA);
jvanverth3d6ed3a2016-04-07 11:09:51 -07001015 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
1016 SkIntToScalar(x), SkIntToScalar(endY), paint);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001017
1018 double inc = fAnimateTimes[i] + fPaintTimes[i] + fFlushTimes[i];
1019 if (inc > 0) {
1020 ms += inc;
1021 animateMS += fAnimateTimes[i];
1022 paintMS += fPaintTimes[i];
1023 flushMS += fFlushTimes[i];
1024 ++count;
1025 }
1026
jvanverth3d6ed3a2016-04-07 11:09:51 -07001027 i++;
1028 i &= (kMeasurementCount - 1); // fast mod
1029 x += xStep;
1030 } while (i != fCurrentMeasurement);
jvanverth9f372462016-04-06 06:08:59 -07001031
Jim Van Verth98c5b8b2017-11-09 15:43:46 -05001032 paint.setTextSize(16);
1033 SkString mainString;
1034 mainString.appendf("%4.3f ms -> %4.3f ms", ms / SkTMax(1, count),
1035 fCumulativeMeasurementTime / SkTMax(1, fCumulativeMeasurementCount));
1036 paint.setColor(SK_ColorWHITE);
1037 canvas->drawString(mainString.c_str(), rect.fLeft+3, rect.fTop + 14, paint);
1038
1039 SkString animateString;
1040 animateString.appendf("Animate: %4.3f ms", animateMS / SkTMax(1, count));
1041 paint.setColor(0xffff66ff); // pure magenta is hard to read
1042 canvas->drawString(animateString.c_str(), rect.fLeft+3, rect.fTop + 28, paint);
1043
1044 SkString paintString;
1045 paintString.appendf("Paint: %4.3f ms", paintMS / SkTMax(1, count));
1046 paint.setColor(SK_ColorGREEN);
1047 canvas->drawString(paintString.c_str(), rect.fLeft+3, rect.fTop + 42, paint);
1048
1049 SkString flushString;
1050 flushString.appendf("Flush: %4.3f ms", flushMS / SkTMax(1, count));
1051 paint.setColor(0xffff6666); // pure red is hard to read
1052 canvas->drawString(flushString.c_str(), rect.fLeft+3, rect.fTop + 56, paint);
1053
jvanverth9f372462016-04-06 06:08:59 -07001054 canvas->restore();
1055}
1056
Brian Osmana109e392017-02-24 09:49:14 -05001057static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1058 const ImVec2& pos, const ImVec2& size) {
1059 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1060 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1061
1062 // Invisible 10x10 button
1063 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1064 ImGui::InvisibleButton(label, ImVec2(10, 10));
1065
1066 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1067 ImGuiIO& io = ImGui::GetIO();
1068 // Normalized mouse position, relative to our gamut box
1069 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1070 // Clamp to edge of box, convert back to primary scale
1071 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1072 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1073 }
1074
1075 if (ImGui::IsItemHovered()) {
1076 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1077 }
1078
1079 // Return screen coordinates for the caller. We could just return center here, but we'd have
1080 // one frame of lag during drag.
1081 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1082}
1083
1084static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1085 ImDrawList* drawList = ImGui::GetWindowDrawList();
1086
1087 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1088 // width, and scale the height to maintain aspect ratio.
1089 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1090 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1091 ImVec2 pos = ImGui::GetCursorScreenPos();
1092
1093 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1094 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1095 // Magic numbers are pixel locations of the origin and upper-right corner.
1096 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1097 ImVec2(242, 61), ImVec2(1897, 1922));
1098 ImVec2 endPos = ImGui::GetCursorPos();
1099
1100 // Primary markers
1101 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1102 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1103 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1104 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1105
1106 // Gamut triangle
1107 drawList->AddCircle(r, 5.0f, 0xFF000040);
1108 drawList->AddCircle(g, 5.0f, 0xFF004000);
1109 drawList->AddCircle(b, 5.0f, 0xFF400000);
1110 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1111 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1112
1113 // Re-position cursor immediate after the diagram for subsequent controls
1114 ImGui::SetCursorPos(endPos);
1115}
1116
Brian Osman79086b92017-02-10 13:36:16 -05001117void Viewer::drawImGui(SkCanvas* canvas) {
1118 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1119 if (fShowImGuiTestWindow) {
1120 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1121 }
1122
1123 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001124 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1125 // always visible, we can end up in a layout feedback loop.
1126 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001127 DisplayParams params = fWindow->getRequestedDisplayParams();
1128 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001129 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1130 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001131 if (ImGui::CollapsingHeader("Backend")) {
1132 int newBackend = static_cast<int>(fBackendType);
1133 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1134 ImGui::SameLine();
1135 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001136#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1137 ImGui::SameLine();
1138 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1139#endif
Brian Osman621491e2017-02-28 15:45:01 -05001140#if defined(SK_VULKAN)
1141 ImGui::SameLine();
1142 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1143#endif
1144 if (newBackend != fBackendType) {
1145 fDeferredActions.push_back([=]() {
1146 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1147 });
1148 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001149
Brian Salomon99a33902017-03-07 15:16:34 -05001150 const GrContext* ctx = fWindow->getGrContext();
1151 bool* inst = &params.fGrContextOptions.fEnableInstancedRendering;
1152 if (ctx && ImGui::Checkbox("Instanced Rendering", inst)) {
1153 paramsChanged = true;
1154 }
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001155 bool* wire = &params.fGrContextOptions.fWireframeMode;
1156 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1157 paramsChanged = true;
1158 }
Brian Salomon99a33902017-03-07 15:16:34 -05001159
Brian Osman28b12522017-03-08 17:10:24 -05001160 if (ctx) {
1161 int sampleCount = fWindow->sampleCount();
1162 ImGui::Text("MSAA: "); ImGui::SameLine();
1163 ImGui::RadioButton("0", &sampleCount, 0); ImGui::SameLine();
1164 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1165 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1166 ImGui::RadioButton("16", &sampleCount, 16);
1167
1168 if (sampleCount != params.fMSAASampleCount) {
1169 params.fMSAASampleCount = sampleCount;
1170 paramsChanged = true;
1171 }
1172 }
1173
Brian Osman8a9de3d2017-03-01 14:59:05 -05001174 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001175 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001176 auto prButton = [&](GpuPathRenderers x) {
1177 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001178 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1179 params.fGrContextOptions.fGpuPathRenderers = x;
1180 paramsChanged = true;
1181 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001182 }
1183 };
1184
1185 if (!ctx) {
1186 ImGui::RadioButton("Software", true);
1187 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001188 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001189 prButton(GpuPathRenderers::kAll);
1190 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1191 prButton(GpuPathRenderers::kStencilAndCover);
1192 }
1193 if (ctx->caps()->sampleShadingSupport()) {
1194 prButton(GpuPathRenderers::kMSAA);
1195 }
1196 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001197 prButton(GpuPathRenderers::kNone);
1198 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001199 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001200 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001201 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1202 prButton(GpuPathRenderers::kCoverageCounting);
1203 }
Jim Van Verth83010462017-03-16 08:45:39 -04001204 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001205 prButton(GpuPathRenderers::kTessellating);
1206 prButton(GpuPathRenderers::kNone);
1207 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001208 ImGui::TreePop();
1209 }
Brian Osman621491e2017-02-28 15:45:01 -05001210 }
1211
Brian Osman79086b92017-02-10 13:36:16 -05001212 if (ImGui::CollapsingHeader("Slide")) {
1213 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001214 static ImVector<const char*> filteredSlideNames;
1215 static ImVector<int> filteredSlideIndices;
1216
Brian Osman79086b92017-02-10 13:36:16 -05001217 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001218 filteredSlideNames.clear();
1219 filteredSlideIndices.clear();
1220 int filteredIndex = 0;
1221 for (int i = 0; i < fSlides.count(); ++i) {
1222 const char* slideName = fSlides[i]->getName().c_str();
1223 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1224 if (i == fCurrentSlide) {
1225 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001226 }
Brian Osmanf479e422017-11-08 13:11:36 -05001227 filteredSlideNames.push_back(slideName);
1228 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001229 }
Brian Osman79086b92017-02-10 13:36:16 -05001230 }
Brian Osmanf479e422017-11-08 13:11:36 -05001231
1232 int previousSlide = fCurrentSlide;
1233 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1234 filteredSlideNames.size(), 20)) {
1235 fCurrentSlide = filteredSlideIndices[filteredIndex];
1236 setupCurrentSlide(previousSlide);
Brian Osman79086b92017-02-10 13:36:16 -05001237 }
1238 }
Brian Osmana109e392017-02-24 09:49:14 -05001239
1240 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001241 ColorMode newMode = fColorMode;
1242 auto cmButton = [&](ColorMode mode, const char* label) {
1243 if (ImGui::RadioButton(label, mode == fColorMode)) {
1244 newMode = mode;
1245 }
1246 };
1247
1248 cmButton(ColorMode::kLegacy, "Legacy 8888");
1249 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1250 "Color Managed 8888 (Nonlinear blending)");
1251 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1252 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1253
1254 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001255 // It isn't safe to switch color mode now (in the middle of painting). We might
1256 // tear down the back-end, etc... Defer this change until the next onIdle.
1257 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001258 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001259 });
Brian Osmana109e392017-02-24 09:49:14 -05001260 }
1261
1262 // Pick from common gamuts:
1263 int primariesIdx = 4; // Default: Custom
1264 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1265 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1266 primariesIdx = i;
1267 break;
1268 }
1269 }
1270
Brian Osmanfdab5762017-11-09 10:27:55 -05001271 // When we're in xform canvas mode, we can alter the transfer function, too
1272 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1273 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1274 }
1275
Brian Osmana109e392017-02-24 09:49:14 -05001276 if (ImGui::Combo("Primaries", &primariesIdx,
1277 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1278 if (primariesIdx >= 0 && primariesIdx <= 3) {
1279 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1280 }
1281 }
1282
1283 // Allow direct editing of gamut
1284 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1285 }
Brian Osman79086b92017-02-10 13:36:16 -05001286 }
Brian Salomon99a33902017-03-07 15:16:34 -05001287 if (paramsChanged) {
1288 fDeferredActions.push_back([=]() {
1289 fWindow->setRequestedDisplayParams(params);
1290 fWindow->inval();
1291 this->updateTitle();
1292 });
1293 }
Brian Osman79086b92017-02-10 13:36:16 -05001294 ImGui::End();
1295 }
1296
Brian Osmanf6877092017-02-13 09:39:57 -05001297 SkPaint zoomImagePaint;
1298 if (fShowZoomWindow && fLastImage) {
1299 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
1300 static int zoomFactor = 4;
1301 ImGui::SliderInt("Scale", &zoomFactor, 1, 16);
1302
Mike Reed0acd7952017-04-28 11:12:19 -04001303 zoomImagePaint.setShader(fLastImage->makeShader());
Brian Osmanf6877092017-02-13 09:39:57 -05001304 zoomImagePaint.setColor(SK_ColorWHITE);
1305
1306 // Zoom by shrinking the corner UVs towards the mouse cursor
1307 ImVec2 mousePos = ImGui::GetMousePos();
1308 ImVec2 avail = ImGui::GetContentRegionAvail();
1309
1310 ImVec2 zoomHalfExtents = ImVec2((avail.x * 0.5f) / zoomFactor,
1311 (avail.y * 0.5f) / zoomFactor);
1312 ImGui::Image(&zoomImagePaint, avail,
1313 ImVec2(mousePos.x - zoomHalfExtents.x, mousePos.y - zoomHalfExtents.y),
1314 ImVec2(mousePos.x + zoomHalfExtents.x, mousePos.y + zoomHalfExtents.y));
1315 }
1316
1317 ImGui::End();
1318 }
1319
Brian Osman79086b92017-02-10 13:36:16 -05001320 // This causes ImGui to rebuild vertex/index data based on all immediate-mode commands
1321 // (widgets, etc...) that have been issued
1322 ImGui::Render();
1323
1324 // Then we fetch the most recent data, and convert it so we can render with Skia
1325 const ImDrawData* drawData = ImGui::GetDrawData();
1326 SkTDArray<SkPoint> pos;
1327 SkTDArray<SkPoint> uv;
1328 SkTDArray<SkColor> color;
Brian Osman79086b92017-02-10 13:36:16 -05001329
1330 for (int i = 0; i < drawData->CmdListsCount; ++i) {
1331 const ImDrawList* drawList = drawData->CmdLists[i];
1332
1333 // De-interleave all vertex data (sigh), convert to Skia types
1334 pos.rewind(); uv.rewind(); color.rewind();
1335 for (int i = 0; i < drawList->VtxBuffer.size(); ++i) {
1336 const ImDrawVert& vert = drawList->VtxBuffer[i];
1337 pos.push(SkPoint::Make(vert.pos.x, vert.pos.y));
1338 uv.push(SkPoint::Make(vert.uv.x, vert.uv.y));
1339 color.push(vert.col);
1340 }
1341 // ImGui colors are RGBA
1342 SkSwapRB(color.begin(), color.begin(), color.count());
1343
1344 int indexOffset = 0;
1345
1346 // Draw everything with canvas.drawVertices...
1347 for (int j = 0; j < drawList->CmdBuffer.size(); ++j) {
1348 const ImDrawCmd* drawCmd = &drawList->CmdBuffer[j];
1349
1350 // TODO: Find min/max index for each draw, so we know how many vertices (sigh)
1351 if (drawCmd->UserCallback) {
1352 drawCmd->UserCallback(drawList, drawCmd);
1353 } else {
Brian Osmanf6877092017-02-13 09:39:57 -05001354 SkPaint* paint = static_cast<SkPaint*>(drawCmd->TextureId);
1355 SkASSERT(paint);
1356
Brian Osman79086b92017-02-10 13:36:16 -05001357 canvas->save();
1358 canvas->clipRect(SkRect::MakeLTRB(drawCmd->ClipRect.x, drawCmd->ClipRect.y,
1359 drawCmd->ClipRect.z, drawCmd->ClipRect.w));
Mike Reed887cdf12017-04-03 11:11:09 -04001360 canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode,
1361 drawList->VtxBuffer.size(), pos.begin(),
1362 uv.begin(), color.begin(),
1363 drawCmd->ElemCount,
1364 drawList->IdxBuffer.begin() + indexOffset),
1365 SkBlendMode::kModulate, *paint);
Brian Osman79086b92017-02-10 13:36:16 -05001366 indexOffset += drawCmd->ElemCount;
1367 canvas->restore();
1368 }
1369 }
1370 }
1371}
1372
liyuqian2edb0f42016-07-06 14:11:32 -07001373void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001374 for (int i = 0; i < fDeferredActions.count(); ++i) {
1375 fDeferredActions[i]();
1376 }
1377 fDeferredActions.reset();
1378
Brian Osman1df161a2017-02-09 12:10:20 -05001379 double startTime = SkTime::GetMSecs();
jvanverthc265a922016-04-08 12:51:45 -07001380 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001381 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
1382 fAnimateTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
1383
Brian Osman79086b92017-02-10 13:36:16 -05001384 ImGuiIO& io = ImGui::GetIO();
1385 if (animateWantsInval || fDisplayStats || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001386 fWindow->inval();
1387 }
jvanverth9f372462016-04-06 06:08:59 -07001388}
liyuqiane5a6cd92016-05-27 08:52:52 -07001389
1390void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001391 if (!fWindow) {
1392 return;
1393 }
1394 if (fWindow->sampleCount() < 0) {
1395 return; // Surface hasn't been created yet.
1396 }
1397
liyuqianb73c24b2016-06-03 08:47:23 -07001398 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001399 Json::Value slideState(Json::objectValue);
1400 slideState[kName] = kSlideStateName;
1401 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001402 if (fAllSlideNames.size() == 0) {
1403 for(auto slide : fSlides) {
1404 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1405 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001406 }
liyuqian1f508fd2016-06-07 06:57:40 -07001407 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001408
liyuqianb73c24b2016-06-03 08:47:23 -07001409 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001410 Json::Value backendState(Json::objectValue);
1411 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001412 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001413 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001414 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001415 backendState[kOptions].append(Json::Value(str));
1416 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001417
csmartdalton578f0642017-02-24 16:04:47 -07001418 // MSAA state
1419 Json::Value msaaState(Json::objectValue);
1420 msaaState[kName] = kMSAAStateName;
1421 msaaState[kValue] = fWindow->sampleCount();
1422 msaaState[kOptions] = Json::Value(Json::arrayValue);
1423 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1424 msaaState[kOptions].append(Json::Value(0));
1425 } else {
1426 for (int msaa : {0, 4, 8, 16}) {
1427 msaaState[kOptions].append(Json::Value(msaa));
1428 }
1429 }
1430
csmartdalton61cd31a2017-02-27 17:00:53 -07001431 // Path renderer state
1432 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1433 Json::Value prState(Json::objectValue);
1434 prState[kName] = kPathRendererStateName;
1435 prState[kValue] = gPathRendererNames[pr];
1436 prState[kOptions] = Json::Value(Json::arrayValue);
1437 const GrContext* ctx = fWindow->getGrContext();
1438 if (!ctx) {
1439 prState[kOptions].append("Software");
1440 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001441 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001442 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1443 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1444 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1445 }
1446 if (ctx->caps()->sampleShadingSupport()) {
1447 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1448 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001449 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001450 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1451 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001452 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001453 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001454 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1455 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1456 }
Jim Van Verth83010462017-03-16 08:45:39 -04001457 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001458 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001459 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1460 }
1461
Brian Salomon99a33902017-03-07 15:16:34 -05001462 // Instanced rendering state
1463 Json::Value instState(Json::objectValue);
1464 instState[kName] = kInstancedRenderingStateName;
Yuqian Lid9020422017-07-25 15:01:53 -04001465 instState[kValue] = kOFF;
1466 instState[kOptions] = Json::Value(Json::arrayValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001467 if (ctx) {
1468 if (fWindow->getRequestedDisplayParams().fGrContextOptions.fEnableInstancedRendering) {
1469 instState[kValue] = kON;
Brian Salomon99a33902017-03-07 15:16:34 -05001470 }
Brian Salomon99a33902017-03-07 15:16:34 -05001471 instState[kOptions].append(kOFF);
1472 instState[kOptions].append(kON);
1473 }
1474
liyuqianb73c24b2016-06-03 08:47:23 -07001475 // Softkey state
1476 Json::Value softkeyState(Json::objectValue);
1477 softkeyState[kName] = kSoftkeyStateName;
1478 softkeyState[kValue] = kSoftkeyHint;
1479 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1480 softkeyState[kOptions].append(kSoftkeyHint);
1481 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1482 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1483 }
1484
liyuqian1f508fd2016-06-07 06:57:40 -07001485 // FPS state
1486 Json::Value fpsState(Json::objectValue);
1487 fpsState[kName] = kFpsStateName;
Brian Osman1df161a2017-02-09 12:10:20 -05001488 int idx = (fCurrentMeasurement + (kMeasurementCount - 1)) & (kMeasurementCount - 1);
1489 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
1490 fAnimateTimes[idx] + fPaintTimes[idx] + fFlushTimes[idx],
1491 fAnimateTimes[idx],
1492 fPaintTimes[idx],
1493 fFlushTimes[idx]).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001494 fpsState[kOptions] = Json::Value(Json::arrayValue);
1495
liyuqiane5a6cd92016-05-27 08:52:52 -07001496 Json::Value state(Json::arrayValue);
1497 state.append(slideState);
1498 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001499 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001500 state.append(prState);
Brian Salomon99a33902017-03-07 15:16:34 -05001501 state.append(instState);
liyuqianb73c24b2016-06-03 08:47:23 -07001502 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001503 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001504
1505 fWindow->setUIState(state);
1506}
1507
1508void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001509 // For those who will add more features to handle the state change in this function:
1510 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1511 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1512 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001513 if (stateName.equals(kSlideStateName)) {
1514 int previousSlide = fCurrentSlide;
1515 fCurrentSlide = 0;
1516 for(auto slide : fSlides) {
1517 if (slide->getName().equals(stateValue)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001518 this->setupCurrentSlide(previousSlide);
liyuqiane5a6cd92016-05-27 08:52:52 -07001519 break;
1520 }
1521 fCurrentSlide++;
1522 }
1523 if (fCurrentSlide >= fSlides.count()) {
1524 fCurrentSlide = previousSlide;
1525 SkDebugf("Slide not found: %s", stateValue.c_str());
1526 }
liyuqian6cb70252016-06-02 12:16:25 -07001527 } else if (stateName.equals(kBackendStateName)) {
1528 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1529 if (stateValue.equals(kBackendTypeStrings[i])) {
1530 if (fBackendType != i) {
1531 fBackendType = (sk_app::Window::BackendType)i;
1532 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001533 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001534 }
1535 break;
1536 }
1537 }
csmartdalton578f0642017-02-24 16:04:47 -07001538 } else if (stateName.equals(kMSAAStateName)) {
1539 DisplayParams params = fWindow->getRequestedDisplayParams();
1540 int sampleCount = atoi(stateValue.c_str());
1541 if (sampleCount != params.fMSAASampleCount) {
1542 params.fMSAASampleCount = sampleCount;
1543 fWindow->setRequestedDisplayParams(params);
1544 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001545 this->updateTitle();
1546 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001547 }
1548 } else if (stateName.equals(kPathRendererStateName)) {
1549 DisplayParams params = fWindow->getRequestedDisplayParams();
1550 for (const auto& pair : gPathRendererNames) {
1551 if (pair.second == stateValue.c_str()) {
1552 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1553 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1554 fWindow->setRequestedDisplayParams(params);
1555 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001556 this->updateTitle();
1557 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001558 }
1559 break;
1560 }
csmartdalton578f0642017-02-24 16:04:47 -07001561 }
Brian Salomon99a33902017-03-07 15:16:34 -05001562 } else if (stateName.equals(kInstancedRenderingStateName)) {
1563 DisplayParams params = fWindow->getRequestedDisplayParams();
1564 bool value = !strcmp(stateValue.c_str(), kON);
1565 if (params.fGrContextOptions.fEnableInstancedRendering != value) {
1566 params.fGrContextOptions.fEnableInstancedRendering = value;
1567 fWindow->setRequestedDisplayParams(params);
1568 fWindow->inval();
1569 this->updateTitle();
1570 this->updateUIState();
1571 }
liyuqianb73c24b2016-06-03 08:47:23 -07001572 } else if (stateName.equals(kSoftkeyStateName)) {
1573 if (!stateValue.equals(kSoftkeyHint)) {
1574 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001575 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001576 }
liyuqian2edb0f42016-07-06 14:11:32 -07001577 } else if (stateName.equals(kRefreshStateName)) {
1578 // This state is actually NOT in the UI state.
1579 // We use this to allow Android to quickly set bool fRefresh.
1580 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001581 } else {
1582 SkDebugf("Unknown stateName: %s", stateName.c_str());
1583 }
1584}
Brian Osman79086b92017-02-10 13:36:16 -05001585
1586bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
1587 return fCommands.onKey(key, state, modifiers);
1588}
1589
1590bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001591 if (fSlides[fCurrentSlide]->onChar(c)) {
1592 fWindow->inval();
1593 return true;
1594 }
1595
Brian Osman79086b92017-02-10 13:36:16 -05001596 return fCommands.onChar(c, modifiers);
1597}