jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 8 | #include "Viewer.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 9 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 10 | #include "GMSlide.h" |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 11 | #include "ImageSlide.h" |
Greg Daniel | 9fcc743 | 2016-11-29 16:35:19 -0500 | [diff] [blame] | 12 | #include "Resources.h" |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 13 | #include "SampleSlide.h" |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 14 | #include "SKPSlide.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 15 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 16 | #include "GrContext.h" |
Greg Daniel | 285db44 | 2016-10-14 09:12:53 -0400 | [diff] [blame] | 17 | #include "SkATrace.h" |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 18 | #include "SkCanvas.h" |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 19 | #include "SkColorSpace_Base.h" |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 20 | #include "SkColorSpaceXformCanvas.h" |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 21 | #include "SkCommandLineFlags.h" |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 22 | #include "SkCommonFlagsPathRenderer.h" |
liyuqian | 74959a1 | 2016-06-16 14:10:34 -0700 | [diff] [blame] | 23 | #include "SkDashPathEffect.h" |
Greg Daniel | 285db44 | 2016-10-14 09:12:53 -0400 | [diff] [blame] | 24 | #include "SkGraphics.h" |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 25 | #include "SkImagePriv.h" |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 26 | #include "SkMetaData.h" |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 27 | #include "SkOnce.h" |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 28 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 29 | #include "SkOSPath.h" |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 30 | #include "SkRandom.h" |
| 31 | #include "SkStream.h" |
liyuqian | 74959a1 | 2016-06-16 14:10:34 -0700 | [diff] [blame] | 32 | #include "SkSurface.h" |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 33 | #include "SkSwizzle.h" |
csmartdalton | 29d8715 | 2017-02-10 17:05:14 -0500 | [diff] [blame] | 34 | #include "SkTaskGroup.h" |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 35 | #include "SkTime.h" |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 36 | #include "SkVertices.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 37 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 38 | #include "imgui.h" |
| 39 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 40 | #include <stdlib.h> |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 41 | #include <map> |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 42 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 43 | using namespace sk_app; |
| 44 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 45 | using GpuPathRenderers = GrContextOptions::GpuPathRenderers; |
| 46 | static std::map<GpuPathRenderers, std::string> gPathRendererNames; |
| 47 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 48 | Application* Application::Create(int argc, char** argv, void* platformData) { |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 49 | return new Viewer(argc, argv, platformData); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 52 | static void on_backend_created_func(void* userData) { |
| 53 | Viewer* vv = reinterpret_cast<Viewer*>(userData); |
| 54 | |
| 55 | return vv->onBackendCreated(); |
| 56 | } |
| 57 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 58 | static void on_paint_handler(SkCanvas* canvas, void* userData) { |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 59 | Viewer* vv = reinterpret_cast<Viewer*>(userData); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 60 | |
| 61 | return vv->onPaint(canvas); |
| 62 | } |
| 63 | |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 64 | static bool on_touch_handler(intptr_t owner, Window::InputState state, float x, float y, void* userData) |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 65 | { |
| 66 | Viewer* viewer = reinterpret_cast<Viewer*>(userData); |
| 67 | |
| 68 | return viewer->onTouch(owner, state, x, y); |
| 69 | } |
| 70 | |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 71 | static void on_ui_state_changed_handler(const SkString& stateName, const SkString& stateValue, void* userData) { |
| 72 | Viewer* viewer = reinterpret_cast<Viewer*>(userData); |
| 73 | |
| 74 | return viewer->onUIStateChanged(stateName, stateValue); |
| 75 | } |
| 76 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 77 | static bool on_mouse_handler(int x, int y, Window::InputState state, uint32_t modifiers, |
| 78 | void* userData) { |
| 79 | ImGuiIO& io = ImGui::GetIO(); |
| 80 | io.MousePos.x = static_cast<float>(x); |
| 81 | io.MousePos.y = static_cast<float>(y); |
| 82 | if (Window::kDown_InputState == state) { |
| 83 | io.MouseDown[0] = true; |
| 84 | } else if (Window::kUp_InputState == state) { |
| 85 | io.MouseDown[0] = false; |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | static bool on_mouse_wheel_handler(float delta, uint32_t modifiers, void* userData) { |
| 91 | ImGuiIO& io = ImGui::GetIO(); |
| 92 | io.MouseWheel += delta; |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | static bool on_key_handler(Window::Key key, Window::InputState state, uint32_t modifiers, |
| 97 | void* userData) { |
| 98 | ImGuiIO& io = ImGui::GetIO(); |
| 99 | io.KeysDown[static_cast<int>(key)] = (Window::kDown_InputState == state); |
| 100 | |
| 101 | if (io.WantCaptureKeyboard) { |
| 102 | return true; |
| 103 | } else { |
| 104 | Viewer* viewer = reinterpret_cast<Viewer*>(userData); |
| 105 | return viewer->onKey(key, state, modifiers); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static bool on_char_handler(SkUnichar c, uint32_t modifiers, void* userData) { |
| 110 | ImGuiIO& io = ImGui::GetIO(); |
| 111 | if (io.WantTextInput) { |
| 112 | if (c > 0 && c < 0x10000) { |
| 113 | io.AddInputCharacter(c); |
| 114 | } |
| 115 | return true; |
| 116 | } else { |
| 117 | Viewer* viewer = reinterpret_cast<Viewer*>(userData); |
| 118 | return viewer->onChar(c, modifiers); |
| 119 | } |
| 120 | } |
| 121 | |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 122 | static DEFINE_bool2(fullscreen, f, true, "Run fullscreen."); |
Brian Osman | 16adfa3 | 2016-10-18 14:42:44 -0400 | [diff] [blame] | 123 | |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 124 | static DEFINE_string2(match, m, nullptr, |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 125 | "[~][^]substring[$] [...] of bench name to run.\n" |
| 126 | "Multiple matches may be separated by spaces.\n" |
| 127 | "~ causes a matching bench to always be skipped\n" |
| 128 | "^ requires the start of the bench to match\n" |
| 129 | "$ requires the end of the bench to match\n" |
| 130 | "^ and $ requires an exact match\n" |
| 131 | "If a bench does not match any list entry,\n" |
| 132 | "it is skipped unless some list entry starts with ~"); |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 133 | |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 134 | DEFINE_string(slide, "", "Start on this sample."); |
| 135 | DEFINE_bool(list, false, "List samples?"); |
| 136 | |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 137 | #ifdef SK_VULKAN |
jvanverth | b8794cc | 2016-07-27 14:29:18 -0700 | [diff] [blame] | 138 | # define BACKENDS_STR "\"sw\", \"gl\", and \"vk\"" |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 139 | #else |
| 140 | # define BACKENDS_STR "\"sw\" and \"gl\"" |
| 141 | #endif |
| 142 | |
liyuqian | 71491dc | 2016-06-09 12:02:34 -0700 | [diff] [blame] | 143 | #ifdef SK_BUILD_FOR_ANDROID |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 144 | static DEFINE_string(skps, "/data/local/tmp/skia", "Directory to read skps from."); |
| 145 | static DEFINE_string(jpgs, "/data/local/tmp/skia", "Directory to read jpgs from."); |
liyuqian | 71491dc | 2016-06-09 12:02:34 -0700 | [diff] [blame] | 146 | #else |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 147 | static DEFINE_string(skps, "skps", "Directory to read skps from."); |
| 148 | static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from."); |
liyuqian | 71491dc | 2016-06-09 12:02:34 -0700 | [diff] [blame] | 149 | #endif |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 150 | |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 151 | static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR "."); |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 152 | |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 153 | static DEFINE_bool(atrace, false, "Enable support for using ATrace. ATrace is only supported on Android."); |
Greg Daniel | 285db44 | 2016-10-14 09:12:53 -0400 | [diff] [blame] | 154 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 155 | DEFINE_int32(msaa, 0, "Number of subpixel samples. 0 for no HW antialiasing."); |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 156 | DEFINE_pathrenderer_flag; |
| 157 | |
Brian Salomon | 41eac79 | 2017-03-08 14:03:56 -0500 | [diff] [blame] | 158 | DEFINE_bool(instancedRendering, false, "Enable instanced rendering on GPU backends."); |
| 159 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 160 | const char *kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 161 | "OpenGL", |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 162 | #ifdef SK_VULKAN |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 163 | "Vulkan", |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 164 | #endif |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 165 | "Raster" |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 168 | static sk_app::Window::BackendType get_backend_type(const char* str) { |
| 169 | #ifdef SK_VULKAN |
| 170 | if (0 == strcmp(str, "vk")) { |
| 171 | return sk_app::Window::kVulkan_BackendType; |
| 172 | } else |
| 173 | #endif |
| 174 | if (0 == strcmp(str, "gl")) { |
| 175 | return sk_app::Window::kNativeGL_BackendType; |
| 176 | } else if (0 == strcmp(str, "sw")) { |
| 177 | return sk_app::Window::kRaster_BackendType; |
| 178 | } else { |
| 179 | SkDebugf("Unknown backend type, %s, defaulting to sw.", str); |
| 180 | return sk_app::Window::kRaster_BackendType; |
| 181 | } |
| 182 | } |
| 183 | |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 184 | static SkColorSpacePrimaries gSrgbPrimaries = { |
| 185 | 0.64f, 0.33f, |
| 186 | 0.30f, 0.60f, |
| 187 | 0.15f, 0.06f, |
| 188 | 0.3127f, 0.3290f }; |
| 189 | |
| 190 | static SkColorSpacePrimaries gAdobePrimaries = { |
| 191 | 0.64f, 0.33f, |
| 192 | 0.21f, 0.71f, |
| 193 | 0.15f, 0.06f, |
| 194 | 0.3127f, 0.3290f }; |
| 195 | |
| 196 | static SkColorSpacePrimaries gP3Primaries = { |
| 197 | 0.680f, 0.320f, |
| 198 | 0.265f, 0.690f, |
| 199 | 0.150f, 0.060f, |
| 200 | 0.3127f, 0.3290f }; |
| 201 | |
| 202 | static SkColorSpacePrimaries gRec2020Primaries = { |
| 203 | 0.708f, 0.292f, |
| 204 | 0.170f, 0.797f, |
| 205 | 0.131f, 0.046f, |
| 206 | 0.3127f, 0.3290f }; |
| 207 | |
| 208 | struct NamedPrimaries { |
| 209 | const char* fName; |
| 210 | SkColorSpacePrimaries* fPrimaries; |
| 211 | } gNamedPrimaries[] = { |
| 212 | { "sRGB", &gSrgbPrimaries }, |
| 213 | { "AdobeRGB", &gAdobePrimaries }, |
| 214 | { "P3", &gP3Primaries }, |
| 215 | { "Rec. 2020", &gRec2020Primaries }, |
| 216 | }; |
| 217 | |
| 218 | static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) { |
| 219 | return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0; |
| 220 | } |
| 221 | |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 222 | const char* kName = "name"; |
| 223 | const char* kValue = "value"; |
| 224 | const char* kOptions = "options"; |
| 225 | const char* kSlideStateName = "Slide"; |
| 226 | const char* kBackendStateName = "Backend"; |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 227 | const char* kMSAAStateName = "MSAA"; |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 228 | const char* kPathRendererStateName = "Path renderer"; |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 229 | const char* kInstancedRenderingStateName = "Instanced rendering"; |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 230 | const char* kSoftkeyStateName = "Softkey"; |
| 231 | const char* kSoftkeyHint = "Please select a softkey"; |
liyuqian | 1f508fd | 2016-06-07 06:57:40 -0700 | [diff] [blame] | 232 | const char* kFpsStateName = "FPS"; |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 233 | const char* kON = "ON"; |
| 234 | const char* kOFF = "OFF"; |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 235 | const char* kRefreshStateName = "Refresh"; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 236 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 237 | Viewer::Viewer(int argc, char** argv, void* platformData) |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 238 | : fCurrentMeasurement(0) |
| 239 | , fDisplayStats(false) |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 240 | , fRefresh(false) |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 241 | , fShowImGuiDebugWindow(false) |
| 242 | , fShowImGuiTestWindow(false) |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 243 | , fShowZoomWindow(false) |
| 244 | , fLastImage(nullptr) |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 245 | , fBackendType(sk_app::Window::kNativeGL_BackendType) |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 246 | , fColorMode(ColorMode::kLegacy) |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 247 | , fColorSpacePrimaries(gSrgbPrimaries) |
egdaniel | 2a0bb0a | 2016-04-11 08:30:40 -0700 | [diff] [blame] | 248 | , fZoomCenterX(0.0f) |
| 249 | , fZoomCenterY(0.0f) |
| 250 | , fZoomLevel(0.0f) |
| 251 | , fZoomScale(SK_Scalar1) |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 252 | { |
csmartdalton | 29d8715 | 2017-02-10 17:05:14 -0500 | [diff] [blame] | 253 | static SkTaskGroup::Enabler kTaskGroupEnabler; |
Greg Daniel | 285db44 | 2016-10-14 09:12:53 -0400 | [diff] [blame] | 254 | SkGraphics::Init(); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 255 | |
| 256 | static SkOnce initPathRendererNames; |
| 257 | initPathRendererNames([]() { |
| 258 | gPathRendererNames[GpuPathRenderers::kAll] = "Default Ganesh Behavior (best path renderer)"; |
| 259 | gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering"; |
| 260 | gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading"; |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 261 | gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)"; |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 262 | gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating"; |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 263 | gPathRendererNames[GpuPathRenderers::kDefault] = "Original Ganesh path renderer"; |
| 264 | gPathRendererNames[GpuPathRenderers::kNone] = "Software masks"; |
| 265 | }); |
| 266 | |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 267 | memset(fPaintTimes, 0, sizeof(fPaintTimes)); |
| 268 | memset(fFlushTimes, 0, sizeof(fFlushTimes)); |
| 269 | memset(fAnimateTimes, 0, sizeof(fAnimateTimes)); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 270 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 271 | SkDebugf("Command line arguments: "); |
| 272 | for (int i = 1; i < argc; ++i) { |
| 273 | SkDebugf("%s ", argv[i]); |
| 274 | } |
| 275 | SkDebugf("\n"); |
| 276 | |
| 277 | SkCommandLineFlags::Parse(argc, argv); |
Greg Daniel | 9fcc743 | 2016-11-29 16:35:19 -0500 | [diff] [blame] | 278 | #ifdef SK_BUILD_FOR_ANDROID |
| 279 | SetResourcePath("/data/local/tmp/skia"); |
| 280 | #endif |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 281 | |
Greg Daniel | 285db44 | 2016-10-14 09:12:53 -0400 | [diff] [blame] | 282 | if (FLAGS_atrace) { |
| 283 | SkEventTracer::SetInstance(new SkATrace()); |
| 284 | } |
| 285 | |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 286 | fBackendType = get_backend_type(FLAGS_backend[0]); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 287 | fWindow = Window::CreateNativeWindow(platformData); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 288 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 289 | DisplayParams displayParams; |
| 290 | displayParams.fMSAASampleCount = FLAGS_msaa; |
Brian Salomon | 41eac79 | 2017-03-08 14:03:56 -0500 | [diff] [blame] | 291 | displayParams.fGrContextOptions.fEnableInstancedRendering = FLAGS_instancedRendering; |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 292 | displayParams.fGrContextOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags(); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 293 | fWindow->setRequestedDisplayParams(displayParams); |
| 294 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 295 | // register callbacks |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 296 | fCommands.attach(fWindow); |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 297 | fWindow->registerBackendCreatedFunc(on_backend_created_func, this); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 298 | fWindow->registerPaintFunc(on_paint_handler, this); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 299 | fWindow->registerTouchFunc(on_touch_handler, this); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 300 | fWindow->registerUIStateChangedFunc(on_ui_state_changed_handler, this); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 301 | fWindow->registerMouseFunc(on_mouse_handler, this); |
| 302 | fWindow->registerMouseWheelFunc(on_mouse_wheel_handler, this); |
| 303 | fWindow->registerKeyFunc(on_key_handler, this); |
| 304 | fWindow->registerCharFunc(on_char_handler, this); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 305 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 306 | // add key-bindings |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 307 | fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() { |
| 308 | this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow; |
| 309 | fWindow->inval(); |
| 310 | }); |
| 311 | fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() { |
| 312 | this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow; |
| 313 | fWindow->inval(); |
| 314 | }); |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 315 | fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() { |
| 316 | this->fShowZoomWindow = !this->fShowZoomWindow; |
| 317 | fWindow->inval(); |
| 318 | }); |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 319 | fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() { |
| 320 | this->fDisplayStats = !this->fDisplayStats; |
| 321 | fWindow->inval(); |
| 322 | }); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 323 | fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() { |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 324 | switch (fColorMode) { |
| 325 | case ColorMode::kLegacy: |
| 326 | this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending); |
| 327 | break; |
| 328 | case ColorMode::kColorManagedSRGB8888_NonLinearBlending: |
| 329 | this->setColorMode(ColorMode::kColorManagedSRGB8888); |
| 330 | break; |
| 331 | case ColorMode::kColorManagedSRGB8888: |
| 332 | this->setColorMode(ColorMode::kColorManagedLinearF16); |
| 333 | break; |
| 334 | case ColorMode::kColorManagedLinearF16: |
| 335 | this->setColorMode(ColorMode::kLegacy); |
| 336 | break; |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 337 | } |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 338 | }); |
| 339 | fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() { |
| 340 | int previousSlide = fCurrentSlide; |
| 341 | fCurrentSlide++; |
| 342 | if (fCurrentSlide >= fSlides.count()) { |
| 343 | fCurrentSlide = 0; |
| 344 | } |
| 345 | this->setupCurrentSlide(previousSlide); |
| 346 | }); |
| 347 | fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() { |
| 348 | int previousSlide = fCurrentSlide; |
| 349 | fCurrentSlide--; |
| 350 | if (fCurrentSlide < 0) { |
| 351 | fCurrentSlide = fSlides.count() - 1; |
| 352 | } |
| 353 | this->setupCurrentSlide(previousSlide); |
| 354 | }); |
| 355 | fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() { |
| 356 | this->changeZoomLevel(1.f / 32.f); |
| 357 | fWindow->inval(); |
| 358 | }); |
| 359 | fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() { |
| 360 | this->changeZoomLevel(-1.f / 32.f); |
| 361 | fWindow->inval(); |
| 362 | }); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 363 | fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() { |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 364 | sk_app::Window::BackendType newBackend = fBackendType; |
Jim Van Verth | d63c102 | 2017-01-05 13:50:49 -0500 | [diff] [blame] | 365 | #if defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC) |
jvanverth | b8794cc | 2016-07-27 14:29:18 -0700 | [diff] [blame] | 366 | if (sk_app::Window::kRaster_BackendType == fBackendType) { |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 367 | newBackend = sk_app::Window::kNativeGL_BackendType; |
jvanverth | b8794cc | 2016-07-27 14:29:18 -0700 | [diff] [blame] | 368 | #ifdef SK_VULKAN |
| 369 | } else if (sk_app::Window::kNativeGL_BackendType == fBackendType) { |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 370 | newBackend = sk_app::Window::kVulkan_BackendType; |
jvanverth | b8794cc | 2016-07-27 14:29:18 -0700 | [diff] [blame] | 371 | #endif |
| 372 | } else { |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 373 | newBackend = sk_app::Window::kRaster_BackendType; |
jvanverth | b8794cc | 2016-07-27 14:29:18 -0700 | [diff] [blame] | 374 | } |
Jim Van Verth | d63c102 | 2017-01-05 13:50:49 -0500 | [diff] [blame] | 375 | #elif defined(SK_BUILD_FOR_UNIX) |
| 376 | // Switching to and from Vulkan is problematic on Linux so disabled for now |
| 377 | if (sk_app::Window::kRaster_BackendType == fBackendType) { |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 378 | newBackend = sk_app::Window::kNativeGL_BackendType; |
Jim Van Verth | d63c102 | 2017-01-05 13:50:49 -0500 | [diff] [blame] | 379 | } else if (sk_app::Window::kNativeGL_BackendType == fBackendType) { |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 380 | newBackend = sk_app::Window::kRaster_BackendType; |
Jim Van Verth | d63c102 | 2017-01-05 13:50:49 -0500 | [diff] [blame] | 381 | } |
| 382 | #endif |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 383 | |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 384 | this->setBackend(newBackend); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 385 | }); |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 386 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 387 | // set up slides |
| 388 | this->initSlides(); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 389 | this->setStartupSlide(); |
| 390 | if (FLAGS_list) { |
| 391 | this->listNames(); |
| 392 | } |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 393 | |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 394 | fAnimTimer.run(); |
| 395 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 396 | // ImGui initialization: |
| 397 | ImGuiIO& io = ImGui::GetIO(); |
| 398 | io.DisplaySize.x = static_cast<float>(fWindow->width()); |
| 399 | io.DisplaySize.y = static_cast<float>(fWindow->height()); |
| 400 | |
| 401 | // Keymap... |
| 402 | io.KeyMap[ImGuiKey_Tab] = (int)Window::Key::kTab; |
| 403 | io.KeyMap[ImGuiKey_LeftArrow] = (int)Window::Key::kLeft; |
| 404 | io.KeyMap[ImGuiKey_RightArrow] = (int)Window::Key::kRight; |
| 405 | io.KeyMap[ImGuiKey_UpArrow] = (int)Window::Key::kUp; |
| 406 | io.KeyMap[ImGuiKey_DownArrow] = (int)Window::Key::kDown; |
| 407 | io.KeyMap[ImGuiKey_PageUp] = (int)Window::Key::kPageUp; |
| 408 | io.KeyMap[ImGuiKey_PageDown] = (int)Window::Key::kPageDown; |
| 409 | io.KeyMap[ImGuiKey_Home] = (int)Window::Key::kHome; |
| 410 | io.KeyMap[ImGuiKey_End] = (int)Window::Key::kEnd; |
| 411 | io.KeyMap[ImGuiKey_Delete] = (int)Window::Key::kDelete; |
| 412 | io.KeyMap[ImGuiKey_Backspace] = (int)Window::Key::kBack; |
| 413 | io.KeyMap[ImGuiKey_Enter] = (int)Window::Key::kOK; |
| 414 | io.KeyMap[ImGuiKey_Escape] = (int)Window::Key::kEscape; |
| 415 | io.KeyMap[ImGuiKey_A] = (int)Window::Key::kA; |
| 416 | io.KeyMap[ImGuiKey_C] = (int)Window::Key::kC; |
| 417 | io.KeyMap[ImGuiKey_V] = (int)Window::Key::kV; |
| 418 | io.KeyMap[ImGuiKey_X] = (int)Window::Key::kX; |
| 419 | io.KeyMap[ImGuiKey_Y] = (int)Window::Key::kY; |
| 420 | io.KeyMap[ImGuiKey_Z] = (int)Window::Key::kZ; |
| 421 | |
| 422 | int w, h; |
| 423 | unsigned char* pixels; |
| 424 | io.Fonts->GetTexDataAsAlpha8(&pixels, &w, &h); |
| 425 | SkImageInfo info = SkImageInfo::MakeA8(w, h); |
| 426 | SkPixmap pmap(info, pixels, info.minRowBytes()); |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 427 | SkMatrix localMatrix = SkMatrix::MakeScale(1.0f / w, 1.0f / h); |
| 428 | auto fontImage = SkImage::MakeFromRaster(pmap, nullptr, nullptr); |
Mike Reed | 0acd795 | 2017-04-28 11:12:19 -0400 | [diff] [blame] | 429 | auto fontShader = fontImage->makeShader(&localMatrix); |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 430 | fImGuiFontPaint.setShader(fontShader); |
| 431 | fImGuiFontPaint.setColor(SK_ColorWHITE); |
| 432 | fImGuiFontPaint.setFilterQuality(kLow_SkFilterQuality); |
| 433 | io.Fonts->TexID = &fImGuiFontPaint; |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 434 | |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 435 | auto gamutImage = GetResourceAsImage("gamut.png"); |
| 436 | if (gamutImage) { |
Mike Reed | 0acd795 | 2017-04-28 11:12:19 -0400 | [diff] [blame] | 437 | fImGuiGamutPaint.setShader(gamutImage->makeShader()); |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 438 | } |
| 439 | fImGuiGamutPaint.setColor(SK_ColorWHITE); |
| 440 | fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality); |
| 441 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 442 | fWindow->attach(fBackendType); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 443 | } |
| 444 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 445 | void Viewer::initSlides() { |
liyuqian | 1f508fd | 2016-06-07 06:57:40 -0700 | [diff] [blame] | 446 | fAllSlideNames = Json::Value(Json::arrayValue); |
| 447 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 448 | const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head()); |
| 449 | while (gms) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 450 | std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr)); |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 451 | |
| 452 | if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) { |
| 453 | sk_sp<Slide> slide(new GMSlide(gm.release())); |
| 454 | fSlides.push_back(slide); |
| 455 | } |
| 456 | |
| 457 | gms = gms->next(); |
| 458 | } |
| 459 | |
| 460 | // reverse array |
| 461 | for (int i = 0; i < fSlides.count()/2; ++i) { |
| 462 | sk_sp<Slide> temp = fSlides[i]; |
| 463 | fSlides[i] = fSlides[fSlides.count() - i - 1]; |
| 464 | fSlides[fSlides.count() - i - 1] = temp; |
| 465 | } |
| 466 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 467 | // samples |
| 468 | const SkViewRegister* reg = SkViewRegister::Head(); |
| 469 | while (reg) { |
| 470 | sk_sp<Slide> slide(new SampleSlide(reg->factory())); |
brianosman | e1d2007 | 2016-07-12 09:07:33 -0700 | [diff] [blame] | 471 | if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) { |
| 472 | fSlides.push_back(slide); |
| 473 | } |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 474 | reg = reg->next(); |
| 475 | } |
| 476 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 477 | // SKPs |
| 478 | for (int i = 0; i < FLAGS_skps.count(); i++) { |
| 479 | if (SkStrEndsWith(FLAGS_skps[i], ".skp")) { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 480 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) { |
| 481 | continue; |
| 482 | } |
| 483 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 484 | SkString path(FLAGS_skps[i]); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 485 | sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path)); |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 486 | if (slide) { |
| 487 | fSlides.push_back(slide); |
| 488 | } |
| 489 | } else { |
| 490 | SkOSFile::Iter it(FLAGS_skps[i], ".skp"); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 491 | SkString skpName; |
| 492 | while (it.next(&skpName)) { |
| 493 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) { |
| 494 | continue; |
| 495 | } |
| 496 | |
| 497 | SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str()); |
| 498 | sk_sp<SKPSlide> slide(new SKPSlide(skpName, path)); |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 499 | if (slide) { |
| 500 | fSlides.push_back(slide); |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | } |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 505 | |
| 506 | // JPGs |
| 507 | for (int i = 0; i < FLAGS_jpgs.count(); i++) { |
| 508 | SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg"); |
| 509 | SkString jpgName; |
| 510 | while (it.next(&jpgName)) { |
brianosman | e1d2007 | 2016-07-12 09:07:33 -0700 | [diff] [blame] | 511 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) { |
| 512 | continue; |
| 513 | } |
| 514 | |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 515 | SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str()); |
| 516 | sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path)); |
| 517 | if (slide) { |
| 518 | fSlides.push_back(slide); |
| 519 | } |
| 520 | } |
| 521 | } |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 525 | Viewer::~Viewer() { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 526 | fWindow->detach(); |
| 527 | delete fWindow; |
| 528 | } |
| 529 | |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 530 | void Viewer::updateTitle() { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 531 | if (!fWindow) { |
| 532 | return; |
| 533 | } |
| 534 | if (fWindow->sampleCount() < 0) { |
| 535 | return; // Surface hasn't been created yet. |
| 536 | } |
| 537 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 538 | SkString title("Viewer: "); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 539 | title.append(fSlides[fCurrentSlide]->getName()); |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 540 | |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 541 | switch (fColorMode) { |
| 542 | case ColorMode::kLegacy: |
| 543 | title.append(" Legacy 8888"); |
| 544 | break; |
| 545 | case ColorMode::kColorManagedSRGB8888_NonLinearBlending: |
| 546 | title.append(" ColorManaged 8888 (Nonlinear blending)"); |
| 547 | break; |
| 548 | case ColorMode::kColorManagedSRGB8888: |
| 549 | title.append(" ColorManaged 8888"); |
| 550 | break; |
| 551 | case ColorMode::kColorManagedLinearF16: |
| 552 | title.append(" ColorManaged F16"); |
| 553 | break; |
| 554 | } |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 555 | |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 556 | if (ColorMode::kLegacy != fColorMode) { |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 557 | int curPrimaries = -1; |
| 558 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) { |
| 559 | if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) { |
| 560 | curPrimaries = i; |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom"); |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 565 | } |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 566 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 567 | title.append(" ["); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 568 | title.append(kBackendTypeStrings[fBackendType]); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 569 | if (int msaa = fWindow->sampleCount()) { |
| 570 | title.appendf(" MSAA: %i", msaa); |
| 571 | } |
| 572 | title.append("]"); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 573 | |
| 574 | GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers; |
| 575 | if (GpuPathRenderers::kAll != pr) { |
| 576 | title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str()); |
| 577 | } |
| 578 | |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 579 | fWindow->setTitle(title.c_str()); |
| 580 | } |
| 581 | |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 582 | void Viewer::setStartupSlide() { |
| 583 | |
| 584 | if (!FLAGS_slide.isEmpty()) { |
| 585 | int count = fSlides.count(); |
| 586 | for (int i = 0; i < count; i++) { |
| 587 | if (fSlides[i]->getName().equals(FLAGS_slide[0])) { |
| 588 | fCurrentSlide = i; |
| 589 | return; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]); |
| 594 | this->listNames(); |
| 595 | } |
| 596 | |
| 597 | fCurrentSlide = 0; |
| 598 | } |
| 599 | |
| 600 | void Viewer::listNames() { |
| 601 | int count = fSlides.count(); |
| 602 | SkDebugf("All Slides:\n"); |
| 603 | for (int i = 0; i < count; i++) { |
| 604 | SkDebugf(" %s\n", fSlides[i]->getName().c_str()); |
| 605 | } |
| 606 | } |
| 607 | |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 608 | void Viewer::setupCurrentSlide(int previousSlide) { |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 609 | if (fCurrentSlide == previousSlide) { |
| 610 | return; // no change; do nothing |
| 611 | } |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 612 | // prepare dimensions for image slides |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 613 | fSlides[fCurrentSlide]->load(SkIntToScalar(fWindow->width()), SkIntToScalar(fWindow->height())); |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 614 | |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 615 | fGesture.reset(); |
| 616 | fDefaultMatrix.reset(); |
| 617 | fDefaultMatrixInv.reset(); |
| 618 | |
| 619 | if (fWindow->supportsContentRect() && fWindow->scaleContentToFit()) { |
| 620 | const SkRect contentRect = fWindow->getContentRect(); |
| 621 | const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); |
| 622 | const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height()); |
| 623 | if (contentRect.width() > 0 && contentRect.height() > 0) { |
| 624 | fDefaultMatrix.setRectToRect(slideBounds, contentRect, SkMatrix::kStart_ScaleToFit); |
liyuqian | beb1c67 | 2016-05-20 11:41:01 -0700 | [diff] [blame] | 625 | SkAssertResult(fDefaultMatrix.invert(&fDefaultMatrixInv)); |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | if (fWindow->supportsContentRect()) { |
| 630 | const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); |
| 631 | SkRect windowRect = fWindow->getContentRect(); |
| 632 | fDefaultMatrixInv.mapRect(&windowRect); |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 633 | fGesture.setTransLimit(SkRect::MakeWH(SkIntToScalar(slideSize.width()), |
| 634 | SkIntToScalar(slideSize.height())), |
| 635 | windowRect); |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 636 | } |
| 637 | |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 638 | this->updateTitle(); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 639 | this->updateUIState(); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 640 | if (previousSlide >= 0) { |
| 641 | fSlides[previousSlide]->unload(); |
| 642 | } |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 643 | fWindow->inval(); |
| 644 | } |
| 645 | |
| 646 | #define MAX_ZOOM_LEVEL 8 |
| 647 | #define MIN_ZOOM_LEVEL -8 |
| 648 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 649 | void Viewer::changeZoomLevel(float delta) { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 650 | fZoomLevel += delta; |
| 651 | if (fZoomLevel > 0) { |
| 652 | fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL); |
| 653 | fZoomScale = fZoomLevel + SK_Scalar1; |
| 654 | } else if (fZoomLevel < 0) { |
| 655 | fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL); |
| 656 | fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel); |
| 657 | } else { |
| 658 | fZoomScale = SK_Scalar1; |
| 659 | } |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 660 | } |
| 661 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 662 | SkMatrix Viewer::computeMatrix() { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 663 | SkMatrix m; |
| 664 | m.reset(); |
| 665 | |
| 666 | if (fZoomLevel) { |
| 667 | SkPoint center; |
| 668 | //m = this->getLocalMatrix();//.invert(&m); |
| 669 | m.mapXY(fZoomCenterX, fZoomCenterY, ¢er); |
| 670 | SkScalar cx = center.fX; |
| 671 | SkScalar cy = center.fY; |
| 672 | |
| 673 | m.setTranslate(-cx, -cy); |
| 674 | m.postScale(fZoomScale, fZoomScale); |
| 675 | m.postTranslate(cx, cy); |
| 676 | } |
| 677 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 678 | m.preConcat(fGesture.localM()); |
| 679 | m.preConcat(fGesture.globalM()); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 680 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 681 | return m; |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 684 | void Viewer::setBackend(sk_app::Window::BackendType backendType) { |
| 685 | fBackendType = backendType; |
| 686 | |
| 687 | fWindow->detach(); |
| 688 | |
| 689 | #if defined(SK_BUILD_FOR_WIN) && defined(SK_VULKAN) |
| 690 | // Switching from OpenGL to Vulkan in the same window is problematic at this point on |
| 691 | // Windows, so we just delete the window and recreate it. |
| 692 | if (sk_app::Window::kVulkan_BackendType == fBackendType) { |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame^] | 693 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 694 | delete fWindow; |
| 695 | fWindow = Window::CreateNativeWindow(nullptr); |
| 696 | |
| 697 | // re-register callbacks |
| 698 | fCommands.attach(fWindow); |
| 699 | fWindow->registerBackendCreatedFunc(on_backend_created_func, this); |
| 700 | fWindow->registerPaintFunc(on_paint_handler, this); |
| 701 | fWindow->registerTouchFunc(on_touch_handler, this); |
| 702 | fWindow->registerUIStateChangedFunc(on_ui_state_changed_handler, this); |
| 703 | fWindow->registerMouseFunc(on_mouse_handler, this); |
| 704 | fWindow->registerMouseWheelFunc(on_mouse_wheel_handler, this); |
| 705 | fWindow->registerKeyFunc(on_key_handler, this); |
| 706 | fWindow->registerCharFunc(on_char_handler, this); |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame^] | 707 | fWindow->setRequestedDisplayParams(params); |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 708 | } |
| 709 | #endif |
| 710 | |
| 711 | fWindow->attach(fBackendType); |
| 712 | } |
| 713 | |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 714 | void Viewer::setColorMode(ColorMode colorMode) { |
| 715 | fColorMode = colorMode; |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 716 | |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 717 | // When we're in color managed mode, we tag our window surface as sRGB. If we've switched into |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 718 | // or out of legacy/nonlinear mode, we need to update our window configuration. |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 719 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 720 | bool wasInLegacy = !SkToBool(params.fColorSpace); |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 721 | bool wantLegacy = (ColorMode::kLegacy == fColorMode) || |
| 722 | (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode); |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 723 | if (wasInLegacy != wantLegacy) { |
| 724 | params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB(); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 725 | fWindow->setRequestedDisplayParams(params); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | this->updateTitle(); |
| 729 | fWindow->inval(); |
| 730 | } |
| 731 | |
| 732 | void Viewer::drawSlide(SkCanvas* canvas) { |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 733 | SkAutoCanvasRestore autorestore(canvas, false); |
| 734 | |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 735 | if (fWindow->supportsContentRect()) { |
| 736 | SkRect contentRect = fWindow->getContentRect(); |
| 737 | canvas->clipRect(contentRect); |
| 738 | canvas->translate(contentRect.fLeft, contentRect.fTop); |
| 739 | } |
| 740 | |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 741 | // By default, we render directly into the window's surface/canvas |
| 742 | SkCanvas* slideCanvas = canvas; |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 743 | fLastImage.reset(); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 744 | |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 745 | // If we're in any of the color managed modes, construct the color space we're going to use |
| 746 | sk_sp<SkColorSpace> cs = nullptr; |
| 747 | if (ColorMode::kLegacy != fColorMode) { |
| 748 | auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode) |
| 749 | ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma; |
| 750 | SkMatrix44 toXYZ; |
| 751 | SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ)); |
| 752 | cs = SkColorSpace::MakeRGB(transferFn, toXYZ); |
| 753 | } |
| 754 | |
| 755 | // If we're in F16, or we're zooming, or we're in color correct 8888 and the gamut isn't sRGB, |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 756 | // we need to render offscreen |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 757 | sk_sp<SkSurface> offscreenSurface = nullptr; |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 758 | if (ColorMode::kColorManagedLinearF16 == fColorMode || |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 759 | fShowZoomWindow || |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 760 | (ColorMode::kColorManagedSRGB8888 == fColorMode && |
| 761 | !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) { |
| 762 | |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 763 | SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode) |
| 764 | ? kRGBA_F16_SkColorType : kN32_SkColorType; |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 765 | // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it |
| 766 | // with a special canvas (below) that has the color space attached |
| 767 | sk_sp<SkColorSpace> offscreenColorSpace = |
| 768 | (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs; |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 769 | SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType, |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 770 | kPremul_SkAlphaType, std::move(offscreenColorSpace)); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 771 | offscreenSurface = canvas->makeSurface(info); |
| 772 | slideCanvas = offscreenSurface->getCanvas(); |
| 773 | } |
| 774 | |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 775 | std::unique_ptr<SkCanvas> xformCanvas = nullptr; |
| 776 | if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) { |
| 777 | xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs); |
| 778 | slideCanvas = xformCanvas.get(); |
| 779 | } |
| 780 | |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 781 | int count = slideCanvas->save(); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 782 | slideCanvas->clear(SK_ColorWHITE); |
| 783 | slideCanvas->concat(fDefaultMatrix); |
| 784 | slideCanvas->concat(computeMatrix()); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 785 | // Time the painting logic of the slide |
| 786 | double startTime = SkTime::GetMSecs(); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 787 | fSlides[fCurrentSlide]->draw(slideCanvas); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 788 | fPaintTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime; |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 789 | slideCanvas->restoreToCount(count); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 790 | |
| 791 | // Force a flush so we can time that, too |
| 792 | startTime = SkTime::GetMSecs(); |
| 793 | slideCanvas->flush(); |
| 794 | fFlushTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime; |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 795 | |
| 796 | // If we rendered offscreen, snap an image and push the results to the window's canvas |
| 797 | if (offscreenSurface) { |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 798 | fLastImage = offscreenSurface->makeImageSnapshot(); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 799 | |
| 800 | // Tag the image with the sRGB gamut, so no further color space conversion happens |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 801 | sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode) |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 802 | ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB(); |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 803 | auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get()); |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 804 | SkPaint paint; |
| 805 | paint.setBlendMode(SkBlendMode::kSrc); |
| 806 | canvas->drawImage(retaggedImage, 0, 0, &paint); |
liyuqian | 74959a1 | 2016-06-16 14:10:34 -0700 | [diff] [blame] | 807 | } |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 810 | void Viewer::onBackendCreated() { |
| 811 | this->updateTitle(); |
| 812 | this->updateUIState(); |
| 813 | this->setupCurrentSlide(-1); |
| 814 | fWindow->show(); |
| 815 | fWindow->inval(); |
| 816 | } |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 817 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 818 | void Viewer::onPaint(SkCanvas* canvas) { |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 819 | // Update ImGui input |
| 820 | ImGuiIO& io = ImGui::GetIO(); |
| 821 | io.DeltaTime = 1.0f / 60.0f; |
| 822 | io.DisplaySize.x = static_cast<float>(fWindow->width()); |
| 823 | io.DisplaySize.y = static_cast<float>(fWindow->height()); |
| 824 | |
| 825 | io.KeyAlt = io.KeysDown[static_cast<int>(Window::Key::kOption)]; |
| 826 | io.KeyCtrl = io.KeysDown[static_cast<int>(Window::Key::kCtrl)]; |
| 827 | io.KeyShift = io.KeysDown[static_cast<int>(Window::Key::kShift)]; |
| 828 | |
| 829 | ImGui::NewFrame(); |
| 830 | |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 831 | drawSlide(canvas); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 832 | |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 833 | // Advance our timing bookkeeping |
| 834 | fCurrentMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1); |
| 835 | SkASSERT(fCurrentMeasurement < kMeasurementCount); |
| 836 | |
| 837 | // Draw any overlays or UI that we don't want timed |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 838 | if (fDisplayStats) { |
| 839 | drawStats(canvas); |
| 840 | } |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 841 | fCommands.drawHelp(canvas); |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 842 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 843 | drawImGui(canvas); |
| 844 | |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 845 | // Update the FPS |
| 846 | updateUIState(); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 847 | } |
| 848 | |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 849 | bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) { |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 850 | void* castedOwner = reinterpret_cast<void*>(owner); |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 851 | SkPoint touchPoint = fDefaultMatrixInv.mapXY(x, y); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 852 | switch (state) { |
| 853 | case Window::kUp_InputState: { |
| 854 | fGesture.touchEnd(castedOwner); |
| 855 | break; |
| 856 | } |
| 857 | case Window::kDown_InputState: { |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 858 | fGesture.touchBegin(castedOwner, touchPoint.fX, touchPoint.fY); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 859 | break; |
| 860 | } |
| 861 | case Window::kMove_InputState: { |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 862 | fGesture.touchMoved(castedOwner, touchPoint.fX, touchPoint.fY); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 863 | break; |
| 864 | } |
| 865 | } |
| 866 | fWindow->inval(); |
| 867 | return true; |
| 868 | } |
| 869 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 870 | void Viewer::drawStats(SkCanvas* canvas) { |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 871 | static const float kPixelPerMS = 2.0f; |
| 872 | static const int kDisplayWidth = 130; |
| 873 | static const int kDisplayHeight = 100; |
| 874 | static const int kDisplayPadding = 10; |
| 875 | static const int kGraphPadding = 3; |
| 876 | static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps |
| 877 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 878 | SkISize canvasSize = canvas->getBaseLayerSize(); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 879 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding), |
| 880 | SkIntToScalar(kDisplayPadding), |
| 881 | SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight)); |
| 882 | SkPaint paint; |
| 883 | canvas->save(); |
| 884 | |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 885 | if (fWindow->supportsContentRect()) { |
| 886 | SkRect contentRect = fWindow->getContentRect(); |
| 887 | canvas->clipRect(contentRect); |
| 888 | canvas->translate(contentRect.fLeft, contentRect.fTop); |
| 889 | } |
| 890 | |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 891 | canvas->clipRect(rect); |
| 892 | paint.setColor(SK_ColorBLACK); |
| 893 | canvas->drawRect(rect, paint); |
| 894 | // draw the 16ms line |
| 895 | paint.setColor(SK_ColorLTGRAY); |
| 896 | canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS, |
| 897 | rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint); |
| 898 | paint.setColor(SK_ColorRED); |
| 899 | paint.setStyle(SkPaint::kStroke_Style); |
| 900 | canvas->drawRect(rect, paint); |
| 901 | |
| 902 | int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding; |
| 903 | const int xStep = 2; |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 904 | int i = fCurrentMeasurement; |
| 905 | do { |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 906 | // Round to nearest values |
| 907 | int animateHeight = (int)(fAnimateTimes[i] * kPixelPerMS + 0.5); |
| 908 | int paintHeight = (int)(fPaintTimes[i] * kPixelPerMS + 0.5); |
| 909 | int flushHeight = (int)(fFlushTimes[i] * kPixelPerMS + 0.5); |
| 910 | int startY = SkScalarTruncToInt(rect.fBottom); |
| 911 | int endY = startY - flushHeight; |
| 912 | paint.setColor(SK_ColorRED); |
| 913 | canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY), |
| 914 | SkIntToScalar(x), SkIntToScalar(endY), paint); |
| 915 | startY = endY; |
| 916 | endY = startY - paintHeight; |
| 917 | paint.setColor(SK_ColorGREEN); |
| 918 | canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY), |
| 919 | SkIntToScalar(x), SkIntToScalar(endY), paint); |
| 920 | startY = endY; |
| 921 | endY = startY - animateHeight; |
| 922 | paint.setColor(SK_ColorMAGENTA); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 923 | canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY), |
| 924 | SkIntToScalar(x), SkIntToScalar(endY), paint); |
| 925 | i++; |
| 926 | i &= (kMeasurementCount - 1); // fast mod |
| 927 | x += xStep; |
| 928 | } while (i != fCurrentMeasurement); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 929 | |
| 930 | canvas->restore(); |
| 931 | } |
| 932 | |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 933 | static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y, |
| 934 | const ImVec2& pos, const ImVec2& size) { |
| 935 | // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip) |
| 936 | ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y); |
| 937 | |
| 938 | // Invisible 10x10 button |
| 939 | ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5)); |
| 940 | ImGui::InvisibleButton(label, ImVec2(10, 10)); |
| 941 | |
| 942 | if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) { |
| 943 | ImGuiIO& io = ImGui::GetIO(); |
| 944 | // Normalized mouse position, relative to our gamut box |
| 945 | ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y); |
| 946 | // Clamp to edge of box, convert back to primary scale |
| 947 | *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f; |
| 948 | *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f; |
| 949 | } |
| 950 | |
| 951 | if (ImGui::IsItemHovered()) { |
| 952 | ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y); |
| 953 | } |
| 954 | |
| 955 | // Return screen coordinates for the caller. We could just return center here, but we'd have |
| 956 | // one frame of lag during drag. |
| 957 | return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y); |
| 958 | } |
| 959 | |
| 960 | static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) { |
| 961 | ImDrawList* drawList = ImGui::GetWindowDrawList(); |
| 962 | |
| 963 | // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available |
| 964 | // width, and scale the height to maintain aspect ratio. |
| 965 | float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f); |
| 966 | ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f)); |
| 967 | ImVec2 pos = ImGui::GetCursorScreenPos(); |
| 968 | |
| 969 | // Background image. Only draw a subset of the image, to avoid the regions less than zero. |
| 970 | // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area. |
| 971 | // Magic numbers are pixel locations of the origin and upper-right corner. |
| 972 | drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y), |
| 973 | ImVec2(242, 61), ImVec2(1897, 1922)); |
| 974 | ImVec2 endPos = ImGui::GetCursorPos(); |
| 975 | |
| 976 | // Primary markers |
| 977 | ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size); |
| 978 | ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size); |
| 979 | ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size); |
| 980 | ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size); |
| 981 | |
| 982 | // Gamut triangle |
| 983 | drawList->AddCircle(r, 5.0f, 0xFF000040); |
| 984 | drawList->AddCircle(g, 5.0f, 0xFF004000); |
| 985 | drawList->AddCircle(b, 5.0f, 0xFF400000); |
| 986 | drawList->AddCircle(w, 5.0f, 0xFFFFFFFF); |
| 987 | drawList->AddTriangle(r, g, b, 0xFFFFFFFF); |
| 988 | |
| 989 | // Re-position cursor immediate after the diagram for subsequent controls |
| 990 | ImGui::SetCursorPos(endPos); |
| 991 | } |
| 992 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 993 | void Viewer::drawImGui(SkCanvas* canvas) { |
| 994 | // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible |
| 995 | if (fShowImGuiTestWindow) { |
| 996 | ImGui::ShowTestWindow(&fShowImGuiTestWindow); |
| 997 | } |
| 998 | |
| 999 | if (fShowImGuiDebugWindow) { |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1000 | // We have some dynamic content that sizes to fill available size. If the scroll bar isn't |
| 1001 | // always visible, we can end up in a layout feedback loop. |
| 1002 | ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1003 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 1004 | bool paramsChanged = false; |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1005 | if (ImGui::Begin("Tools", &fShowImGuiDebugWindow, |
| 1006 | ImGuiWindowFlags_AlwaysVerticalScrollbar)) { |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 1007 | if (ImGui::CollapsingHeader("Backend")) { |
| 1008 | int newBackend = static_cast<int>(fBackendType); |
| 1009 | ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType); |
| 1010 | ImGui::SameLine(); |
| 1011 | ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType); |
| 1012 | #if defined(SK_VULKAN) |
| 1013 | ImGui::SameLine(); |
| 1014 | ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType); |
| 1015 | #endif |
| 1016 | if (newBackend != fBackendType) { |
| 1017 | fDeferredActions.push_back([=]() { |
| 1018 | this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend)); |
| 1019 | }); |
| 1020 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1021 | |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1022 | const GrContext* ctx = fWindow->getGrContext(); |
| 1023 | bool* inst = ¶ms.fGrContextOptions.fEnableInstancedRendering; |
| 1024 | if (ctx && ImGui::Checkbox("Instanced Rendering", inst)) { |
| 1025 | paramsChanged = true; |
| 1026 | } |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame^] | 1027 | bool* wire = ¶ms.fGrContextOptions.fWireframeMode; |
| 1028 | if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) { |
| 1029 | paramsChanged = true; |
| 1030 | } |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1031 | |
Brian Osman | 28b1252 | 2017-03-08 17:10:24 -0500 | [diff] [blame] | 1032 | if (ctx) { |
| 1033 | int sampleCount = fWindow->sampleCount(); |
| 1034 | ImGui::Text("MSAA: "); ImGui::SameLine(); |
| 1035 | ImGui::RadioButton("0", &sampleCount, 0); ImGui::SameLine(); |
| 1036 | ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine(); |
| 1037 | ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine(); |
| 1038 | ImGui::RadioButton("16", &sampleCount, 16); |
| 1039 | |
| 1040 | if (sampleCount != params.fMSAASampleCount) { |
| 1041 | params.fMSAASampleCount = sampleCount; |
| 1042 | paramsChanged = true; |
| 1043 | } |
| 1044 | } |
| 1045 | |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1046 | if (ImGui::TreeNode("Path Renderers")) { |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1047 | GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers; |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1048 | auto prButton = [&](GpuPathRenderers x) { |
| 1049 | if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) { |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1050 | if (x != params.fGrContextOptions.fGpuPathRenderers) { |
| 1051 | params.fGrContextOptions.fGpuPathRenderers = x; |
| 1052 | paramsChanged = true; |
| 1053 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1054 | } |
| 1055 | }; |
| 1056 | |
| 1057 | if (!ctx) { |
| 1058 | ImGui::RadioButton("Software", true); |
| 1059 | } else if (fWindow->sampleCount()) { |
| 1060 | prButton(GpuPathRenderers::kAll); |
| 1061 | if (ctx->caps()->shaderCaps()->pathRenderingSupport()) { |
| 1062 | prButton(GpuPathRenderers::kStencilAndCover); |
| 1063 | } |
| 1064 | if (ctx->caps()->sampleShadingSupport()) { |
| 1065 | prButton(GpuPathRenderers::kMSAA); |
| 1066 | } |
| 1067 | prButton(GpuPathRenderers::kTessellating); |
| 1068 | prButton(GpuPathRenderers::kDefault); |
| 1069 | prButton(GpuPathRenderers::kNone); |
| 1070 | } else { |
| 1071 | prButton(GpuPathRenderers::kAll); |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 1072 | prButton(GpuPathRenderers::kSmall); |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1073 | prButton(GpuPathRenderers::kTessellating); |
| 1074 | prButton(GpuPathRenderers::kNone); |
| 1075 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1076 | ImGui::TreePop(); |
| 1077 | } |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 1078 | } |
| 1079 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1080 | if (ImGui::CollapsingHeader("Slide")) { |
| 1081 | static ImGuiTextFilter filter; |
| 1082 | filter.Draw(); |
| 1083 | int previousSlide = fCurrentSlide; |
| 1084 | fCurrentSlide = 0; |
| 1085 | for (auto slide : fSlides) { |
| 1086 | if (filter.PassFilter(slide->getName().c_str())) { |
| 1087 | ImGui::BulletText("%s", slide->getName().c_str()); |
| 1088 | if (ImGui::IsItemClicked()) { |
| 1089 | setupCurrentSlide(previousSlide); |
| 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | ++fCurrentSlide; |
| 1094 | } |
| 1095 | if (fCurrentSlide >= fSlides.count()) { |
| 1096 | fCurrentSlide = previousSlide; |
| 1097 | } |
| 1098 | } |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1099 | |
| 1100 | if (ImGui::CollapsingHeader("Color Mode")) { |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1101 | ColorMode newMode = fColorMode; |
| 1102 | auto cmButton = [&](ColorMode mode, const char* label) { |
| 1103 | if (ImGui::RadioButton(label, mode == fColorMode)) { |
| 1104 | newMode = mode; |
| 1105 | } |
| 1106 | }; |
| 1107 | |
| 1108 | cmButton(ColorMode::kLegacy, "Legacy 8888"); |
| 1109 | cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending, |
| 1110 | "Color Managed 8888 (Nonlinear blending)"); |
| 1111 | cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888"); |
| 1112 | cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16"); |
| 1113 | |
| 1114 | if (newMode != fColorMode) { |
Brian Osman | fd8f4d5 | 2017-02-24 11:57:23 -0500 | [diff] [blame] | 1115 | // It isn't safe to switch color mode now (in the middle of painting). We might |
| 1116 | // tear down the back-end, etc... Defer this change until the next onIdle. |
| 1117 | fDeferredActions.push_back([=]() { |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1118 | this->setColorMode(newMode); |
Brian Osman | fd8f4d5 | 2017-02-24 11:57:23 -0500 | [diff] [blame] | 1119 | }); |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | // Pick from common gamuts: |
| 1123 | int primariesIdx = 4; // Default: Custom |
| 1124 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) { |
| 1125 | if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) { |
| 1126 | primariesIdx = i; |
| 1127 | break; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | if (ImGui::Combo("Primaries", &primariesIdx, |
| 1132 | "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) { |
| 1133 | if (primariesIdx >= 0 && primariesIdx <= 3) { |
| 1134 | fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | // Allow direct editing of gamut |
| 1139 | ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint); |
| 1140 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1141 | } |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1142 | if (paramsChanged) { |
| 1143 | fDeferredActions.push_back([=]() { |
| 1144 | fWindow->setRequestedDisplayParams(params); |
| 1145 | fWindow->inval(); |
| 1146 | this->updateTitle(); |
| 1147 | }); |
| 1148 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1149 | ImGui::End(); |
| 1150 | } |
| 1151 | |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1152 | SkPaint zoomImagePaint; |
| 1153 | if (fShowZoomWindow && fLastImage) { |
| 1154 | if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) { |
| 1155 | static int zoomFactor = 4; |
| 1156 | ImGui::SliderInt("Scale", &zoomFactor, 1, 16); |
| 1157 | |
Mike Reed | 0acd795 | 2017-04-28 11:12:19 -0400 | [diff] [blame] | 1158 | zoomImagePaint.setShader(fLastImage->makeShader()); |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1159 | zoomImagePaint.setColor(SK_ColorWHITE); |
| 1160 | |
| 1161 | // Zoom by shrinking the corner UVs towards the mouse cursor |
| 1162 | ImVec2 mousePos = ImGui::GetMousePos(); |
| 1163 | ImVec2 avail = ImGui::GetContentRegionAvail(); |
| 1164 | |
| 1165 | ImVec2 zoomHalfExtents = ImVec2((avail.x * 0.5f) / zoomFactor, |
| 1166 | (avail.y * 0.5f) / zoomFactor); |
| 1167 | ImGui::Image(&zoomImagePaint, avail, |
| 1168 | ImVec2(mousePos.x - zoomHalfExtents.x, mousePos.y - zoomHalfExtents.y), |
| 1169 | ImVec2(mousePos.x + zoomHalfExtents.x, mousePos.y + zoomHalfExtents.y)); |
| 1170 | } |
| 1171 | |
| 1172 | ImGui::End(); |
| 1173 | } |
| 1174 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1175 | // This causes ImGui to rebuild vertex/index data based on all immediate-mode commands |
| 1176 | // (widgets, etc...) that have been issued |
| 1177 | ImGui::Render(); |
| 1178 | |
| 1179 | // Then we fetch the most recent data, and convert it so we can render with Skia |
| 1180 | const ImDrawData* drawData = ImGui::GetDrawData(); |
| 1181 | SkTDArray<SkPoint> pos; |
| 1182 | SkTDArray<SkPoint> uv; |
| 1183 | SkTDArray<SkColor> color; |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1184 | |
| 1185 | for (int i = 0; i < drawData->CmdListsCount; ++i) { |
| 1186 | const ImDrawList* drawList = drawData->CmdLists[i]; |
| 1187 | |
| 1188 | // De-interleave all vertex data (sigh), convert to Skia types |
| 1189 | pos.rewind(); uv.rewind(); color.rewind(); |
| 1190 | for (int i = 0; i < drawList->VtxBuffer.size(); ++i) { |
| 1191 | const ImDrawVert& vert = drawList->VtxBuffer[i]; |
| 1192 | pos.push(SkPoint::Make(vert.pos.x, vert.pos.y)); |
| 1193 | uv.push(SkPoint::Make(vert.uv.x, vert.uv.y)); |
| 1194 | color.push(vert.col); |
| 1195 | } |
| 1196 | // ImGui colors are RGBA |
| 1197 | SkSwapRB(color.begin(), color.begin(), color.count()); |
| 1198 | |
| 1199 | int indexOffset = 0; |
| 1200 | |
| 1201 | // Draw everything with canvas.drawVertices... |
| 1202 | for (int j = 0; j < drawList->CmdBuffer.size(); ++j) { |
| 1203 | const ImDrawCmd* drawCmd = &drawList->CmdBuffer[j]; |
| 1204 | |
| 1205 | // TODO: Find min/max index for each draw, so we know how many vertices (sigh) |
| 1206 | if (drawCmd->UserCallback) { |
| 1207 | drawCmd->UserCallback(drawList, drawCmd); |
| 1208 | } else { |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1209 | SkPaint* paint = static_cast<SkPaint*>(drawCmd->TextureId); |
| 1210 | SkASSERT(paint); |
| 1211 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1212 | canvas->save(); |
| 1213 | canvas->clipRect(SkRect::MakeLTRB(drawCmd->ClipRect.x, drawCmd->ClipRect.y, |
| 1214 | drawCmd->ClipRect.z, drawCmd->ClipRect.w)); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 1215 | canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, |
| 1216 | drawList->VtxBuffer.size(), pos.begin(), |
| 1217 | uv.begin(), color.begin(), |
| 1218 | drawCmd->ElemCount, |
| 1219 | drawList->IdxBuffer.begin() + indexOffset), |
| 1220 | SkBlendMode::kModulate, *paint); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1221 | indexOffset += drawCmd->ElemCount; |
| 1222 | canvas->restore(); |
| 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 1228 | void Viewer::onIdle() { |
Brian Osman | fd8f4d5 | 2017-02-24 11:57:23 -0500 | [diff] [blame] | 1229 | for (int i = 0; i < fDeferredActions.count(); ++i) { |
| 1230 | fDeferredActions[i](); |
| 1231 | } |
| 1232 | fDeferredActions.reset(); |
| 1233 | |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1234 | double startTime = SkTime::GetMSecs(); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 1235 | fAnimTimer.updateTime(); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1236 | bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer); |
| 1237 | fAnimateTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime; |
| 1238 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1239 | ImGuiIO& io = ImGui::GetIO(); |
| 1240 | if (animateWantsInval || fDisplayStats || fRefresh || io.MetricsActiveWindows) { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 1241 | fWindow->inval(); |
| 1242 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 1243 | } |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1244 | |
| 1245 | void Viewer::updateUIState() { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1246 | if (!fWindow) { |
| 1247 | return; |
| 1248 | } |
| 1249 | if (fWindow->sampleCount() < 0) { |
| 1250 | return; // Surface hasn't been created yet. |
| 1251 | } |
| 1252 | |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1253 | // Slide state |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1254 | Json::Value slideState(Json::objectValue); |
| 1255 | slideState[kName] = kSlideStateName; |
| 1256 | slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str(); |
liyuqian | 1f508fd | 2016-06-07 06:57:40 -0700 | [diff] [blame] | 1257 | if (fAllSlideNames.size() == 0) { |
| 1258 | for(auto slide : fSlides) { |
| 1259 | fAllSlideNames.append(Json::Value(slide->getName().c_str())); |
| 1260 | } |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1261 | } |
liyuqian | 1f508fd | 2016-06-07 06:57:40 -0700 | [diff] [blame] | 1262 | slideState[kOptions] = fAllSlideNames; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1263 | |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1264 | // Backend state |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1265 | Json::Value backendState(Json::objectValue); |
| 1266 | backendState[kName] = kBackendStateName; |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1267 | backendState[kValue] = kBackendTypeStrings[fBackendType]; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1268 | backendState[kOptions] = Json::Value(Json::arrayValue); |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1269 | for (auto str : kBackendTypeStrings) { |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1270 | backendState[kOptions].append(Json::Value(str)); |
| 1271 | } |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1272 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1273 | // MSAA state |
| 1274 | Json::Value msaaState(Json::objectValue); |
| 1275 | msaaState[kName] = kMSAAStateName; |
| 1276 | msaaState[kValue] = fWindow->sampleCount(); |
| 1277 | msaaState[kOptions] = Json::Value(Json::arrayValue); |
| 1278 | if (sk_app::Window::kRaster_BackendType == fBackendType) { |
| 1279 | msaaState[kOptions].append(Json::Value(0)); |
| 1280 | } else { |
| 1281 | for (int msaa : {0, 4, 8, 16}) { |
| 1282 | msaaState[kOptions].append(Json::Value(msaa)); |
| 1283 | } |
| 1284 | } |
| 1285 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1286 | // Path renderer state |
| 1287 | GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers; |
| 1288 | Json::Value prState(Json::objectValue); |
| 1289 | prState[kName] = kPathRendererStateName; |
| 1290 | prState[kValue] = gPathRendererNames[pr]; |
| 1291 | prState[kOptions] = Json::Value(Json::arrayValue); |
| 1292 | const GrContext* ctx = fWindow->getGrContext(); |
| 1293 | if (!ctx) { |
| 1294 | prState[kOptions].append("Software"); |
| 1295 | } else if (fWindow->sampleCount()) { |
| 1296 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]); |
| 1297 | if (ctx->caps()->shaderCaps()->pathRenderingSupport()) { |
| 1298 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]); |
| 1299 | } |
| 1300 | if (ctx->caps()->sampleShadingSupport()) { |
| 1301 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]); |
| 1302 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1303 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1304 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]); |
| 1305 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]); |
| 1306 | } else { |
| 1307 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]); |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 1308 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]); |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1309 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1310 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]); |
| 1311 | } |
| 1312 | |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1313 | // Instanced rendering state |
| 1314 | Json::Value instState(Json::objectValue); |
| 1315 | instState[kName] = kInstancedRenderingStateName; |
| 1316 | if (ctx) { |
| 1317 | if (fWindow->getRequestedDisplayParams().fGrContextOptions.fEnableInstancedRendering) { |
| 1318 | instState[kValue] = kON; |
| 1319 | } else { |
| 1320 | instState[kValue] = kOFF; |
| 1321 | } |
| 1322 | instState[kOptions] = Json::Value(Json::arrayValue); |
| 1323 | instState[kOptions].append(kOFF); |
| 1324 | instState[kOptions].append(kON); |
| 1325 | } |
| 1326 | |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1327 | // Softkey state |
| 1328 | Json::Value softkeyState(Json::objectValue); |
| 1329 | softkeyState[kName] = kSoftkeyStateName; |
| 1330 | softkeyState[kValue] = kSoftkeyHint; |
| 1331 | softkeyState[kOptions] = Json::Value(Json::arrayValue); |
| 1332 | softkeyState[kOptions].append(kSoftkeyHint); |
| 1333 | for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) { |
| 1334 | softkeyState[kOptions].append(Json::Value(softkey.c_str())); |
| 1335 | } |
| 1336 | |
liyuqian | 1f508fd | 2016-06-07 06:57:40 -0700 | [diff] [blame] | 1337 | // FPS state |
| 1338 | Json::Value fpsState(Json::objectValue); |
| 1339 | fpsState[kName] = kFpsStateName; |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1340 | int idx = (fCurrentMeasurement + (kMeasurementCount - 1)) & (kMeasurementCount - 1); |
| 1341 | fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf", |
| 1342 | fAnimateTimes[idx] + fPaintTimes[idx] + fFlushTimes[idx], |
| 1343 | fAnimateTimes[idx], |
| 1344 | fPaintTimes[idx], |
| 1345 | fFlushTimes[idx]).c_str(); |
liyuqian | 1f508fd | 2016-06-07 06:57:40 -0700 | [diff] [blame] | 1346 | fpsState[kOptions] = Json::Value(Json::arrayValue); |
| 1347 | |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1348 | Json::Value state(Json::arrayValue); |
| 1349 | state.append(slideState); |
| 1350 | state.append(backendState); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1351 | state.append(msaaState); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1352 | state.append(prState); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1353 | state.append(instState); |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1354 | state.append(softkeyState); |
liyuqian | 1f508fd | 2016-06-07 06:57:40 -0700 | [diff] [blame] | 1355 | state.append(fpsState); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1356 | |
| 1357 | fWindow->setUIState(state); |
| 1358 | } |
| 1359 | |
| 1360 | void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) { |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1361 | // For those who will add more features to handle the state change in this function: |
| 1362 | // After the change, please call updateUIState no notify the frontend (e.g., Android app). |
| 1363 | // For example, after slide change, updateUIState is called inside setupCurrentSlide; |
| 1364 | // after backend change, updateUIState is called in this function. |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1365 | if (stateName.equals(kSlideStateName)) { |
| 1366 | int previousSlide = fCurrentSlide; |
| 1367 | fCurrentSlide = 0; |
| 1368 | for(auto slide : fSlides) { |
| 1369 | if (slide->getName().equals(stateValue)) { |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1370 | this->setupCurrentSlide(previousSlide); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1371 | break; |
| 1372 | } |
| 1373 | fCurrentSlide++; |
| 1374 | } |
| 1375 | if (fCurrentSlide >= fSlides.count()) { |
| 1376 | fCurrentSlide = previousSlide; |
| 1377 | SkDebugf("Slide not found: %s", stateValue.c_str()); |
| 1378 | } |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1379 | } else if (stateName.equals(kBackendStateName)) { |
| 1380 | for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) { |
| 1381 | if (stateValue.equals(kBackendTypeStrings[i])) { |
| 1382 | if (fBackendType != i) { |
| 1383 | fBackendType = (sk_app::Window::BackendType)i; |
| 1384 | fWindow->detach(); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1385 | fWindow->attach(fBackendType); |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1386 | } |
| 1387 | break; |
| 1388 | } |
| 1389 | } |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1390 | } else if (stateName.equals(kMSAAStateName)) { |
| 1391 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 1392 | int sampleCount = atoi(stateValue.c_str()); |
| 1393 | if (sampleCount != params.fMSAASampleCount) { |
| 1394 | params.fMSAASampleCount = sampleCount; |
| 1395 | fWindow->setRequestedDisplayParams(params); |
| 1396 | fWindow->inval(); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1397 | this->updateTitle(); |
| 1398 | this->updateUIState(); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1399 | } |
| 1400 | } else if (stateName.equals(kPathRendererStateName)) { |
| 1401 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 1402 | for (const auto& pair : gPathRendererNames) { |
| 1403 | if (pair.second == stateValue.c_str()) { |
| 1404 | if (params.fGrContextOptions.fGpuPathRenderers != pair.first) { |
| 1405 | params.fGrContextOptions.fGpuPathRenderers = pair.first; |
| 1406 | fWindow->setRequestedDisplayParams(params); |
| 1407 | fWindow->inval(); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1408 | this->updateTitle(); |
| 1409 | this->updateUIState(); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1410 | } |
| 1411 | break; |
| 1412 | } |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1413 | } |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1414 | } else if (stateName.equals(kInstancedRenderingStateName)) { |
| 1415 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 1416 | bool value = !strcmp(stateValue.c_str(), kON); |
| 1417 | if (params.fGrContextOptions.fEnableInstancedRendering != value) { |
| 1418 | params.fGrContextOptions.fEnableInstancedRendering = value; |
| 1419 | fWindow->setRequestedDisplayParams(params); |
| 1420 | fWindow->inval(); |
| 1421 | this->updateTitle(); |
| 1422 | this->updateUIState(); |
| 1423 | } |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1424 | } else if (stateName.equals(kSoftkeyStateName)) { |
| 1425 | if (!stateValue.equals(kSoftkeyHint)) { |
| 1426 | fCommands.onSoftkey(stateValue); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1427 | this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1428 | } |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 1429 | } else if (stateName.equals(kRefreshStateName)) { |
| 1430 | // This state is actually NOT in the UI state. |
| 1431 | // We use this to allow Android to quickly set bool fRefresh. |
| 1432 | fRefresh = stateValue.equals(kON); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1433 | } else { |
| 1434 | SkDebugf("Unknown stateName: %s", stateName.c_str()); |
| 1435 | } |
| 1436 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1437 | |
| 1438 | bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) { |
| 1439 | return fCommands.onKey(key, state, modifiers); |
| 1440 | } |
| 1441 | |
| 1442 | bool Viewer::onChar(SkUnichar c, uint32_t modifiers) { |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 1443 | if (fSlides[fCurrentSlide]->onChar(c)) { |
| 1444 | fWindow->inval(); |
| 1445 | return true; |
| 1446 | } |
| 1447 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1448 | return fCommands.onChar(c, modifiers); |
| 1449 | } |