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 | |
Chris Dalton | 2d18f41 | 2018-02-20 13:23:32 -0700 | [diff] [blame] | 8 | #include "BisectSlide.h" |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 9 | #include "GMSlide.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | #include "GrContextPriv.h" |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 12 | #include "ImageSlide.h" |
Greg Daniel | 9fcc743 | 2016-11-29 16:35:19 -0500 | [diff] [blame] | 13 | #include "Resources.h" |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 14 | #include "SKPSlide.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 15 | #include "SampleSlide.h" |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 16 | #include "SkCanvas.h" |
Brian Osman | fdab576 | 2017-11-09 10:27:55 -0500 | [diff] [blame] | 17 | #include "SkColorSpacePriv.h" |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 18 | #include "SkColorSpaceXformCanvas.h" |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 19 | #include "SkCommandLineFlags.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 20 | #include "SkCommonFlags.h" |
Chris Dalton | 040238b | 2017-12-18 14:22:34 -0700 | [diff] [blame] | 21 | #include "SkCommonFlagsGpu.h" |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 22 | #include "SkEventTracingPriv.h" |
Ben Wagner | 483c772 | 2018-02-20 17:06:07 -0500 | [diff] [blame] | 23 | #include "SkFontMgrPriv.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" |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 26 | #include "SkJSONWriter.h" |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 27 | #include "SkMakeUnique.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" |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 30 | #include "SkPaintFilterCanvas.h" |
Brian Osman | 3ac99cf | 2017-12-01 11:23:53 -0500 | [diff] [blame] | 31 | #include "SkPictureRecorder.h" |
Yuqian Li | 399b3c2 | 2017-08-03 11:08:15 -0400 | [diff] [blame] | 32 | #include "SkScan.h" |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 33 | #include "SkStream.h" |
liyuqian | 74959a1 | 2016-06-16 14:10:34 -0700 | [diff] [blame] | 34 | #include "SkSurface.h" |
csmartdalton | 29d8715 | 2017-02-10 17:05:14 -0500 | [diff] [blame] | 35 | #include "SkTaskGroup.h" |
Ben Wagner | 483c772 | 2018-02-20 17:06:07 -0500 | [diff] [blame] | 36 | #include "SkTestFontMgr.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 37 | #include "SkTo.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 38 | #include "SvgSlide.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 39 | #include "Viewer.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 40 | #include "ccpr/GrCoverageCountingPathRenderer.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 41 | #include "imgui.h" |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 42 | |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 43 | #include <stdlib.h> |
| 44 | #include <map> |
| 45 | |
Florin Malita | 3b526b0 | 2018-05-25 12:43:51 -0400 | [diff] [blame] | 46 | #if defined(SK_HAS_SKSG) |
| 47 | #include "SlideDir.h" |
| 48 | #endif |
| 49 | |
Florin Malita | 87ccf33 | 2018-05-04 12:23:24 -0400 | [diff] [blame] | 50 | #if defined(SK_ENABLE_SKOTTIE) |
| 51 | #include "SkottieSlide.h" |
| 52 | #endif |
| 53 | |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 54 | #if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__)) |
| 55 | #include "NIMASlide.h" |
| 56 | #endif |
| 57 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 58 | using namespace sk_app; |
| 59 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 60 | static std::map<GpuPathRenderers, std::string> gPathRendererNames; |
| 61 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 62 | Application* Application::Create(int argc, char** argv, void* platformData) { |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 63 | return new Viewer(argc, argv, platformData); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Chris Dalton | 7a0ebfc | 2017-10-13 12:35:50 -0600 | [diff] [blame] | 66 | static DEFINE_string(slide, "", "Start on this sample."); |
| 67 | static DEFINE_bool(list, false, "List samples?"); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 68 | |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 69 | #ifdef SK_VULKAN |
jvanverth | b8794cc | 2016-07-27 14:29:18 -0700 | [diff] [blame] | 70 | # define BACKENDS_STR "\"sw\", \"gl\", and \"vk\"" |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 71 | #else |
| 72 | # define BACKENDS_STR "\"sw\" and \"gl\"" |
| 73 | #endif |
| 74 | |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 75 | 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] | 76 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 77 | static DEFINE_int32(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing."); |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 78 | |
Chris Dalton | 2d18f41 | 2018-02-20 13:23:32 -0700 | [diff] [blame] | 79 | DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect."); |
| 80 | |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 81 | DECLARE_int32(threads) |
Brian Salomon | 41eac79 | 2017-03-08 14:03:56 -0500 | [diff] [blame] | 82 | |
Florin Malita | 38792ce | 2018-05-08 10:36:18 -0400 | [diff] [blame] | 83 | DEFINE_string2(file, f, "", "Open a single file for viewing."); |
| 84 | |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 85 | const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 86 | "OpenGL", |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 87 | #if SK_ANGLE && defined(SK_BUILD_FOR_WIN) |
| 88 | "ANGLE", |
| 89 | #endif |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 90 | #ifdef SK_VULKAN |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 91 | "Vulkan", |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 92 | #endif |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 93 | "Raster" |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 96 | static sk_app::Window::BackendType get_backend_type(const char* str) { |
| 97 | #ifdef SK_VULKAN |
| 98 | if (0 == strcmp(str, "vk")) { |
| 99 | return sk_app::Window::kVulkan_BackendType; |
| 100 | } else |
| 101 | #endif |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 102 | #if SK_ANGLE && defined(SK_BUILD_FOR_WIN) |
| 103 | if (0 == strcmp(str, "angle")) { |
| 104 | return sk_app::Window::kANGLE_BackendType; |
| 105 | } else |
| 106 | #endif |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 107 | if (0 == strcmp(str, "gl")) { |
| 108 | return sk_app::Window::kNativeGL_BackendType; |
| 109 | } else if (0 == strcmp(str, "sw")) { |
| 110 | return sk_app::Window::kRaster_BackendType; |
| 111 | } else { |
| 112 | SkDebugf("Unknown backend type, %s, defaulting to sw.", str); |
| 113 | return sk_app::Window::kRaster_BackendType; |
| 114 | } |
| 115 | } |
| 116 | |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 117 | static SkColorSpacePrimaries gSrgbPrimaries = { |
| 118 | 0.64f, 0.33f, |
| 119 | 0.30f, 0.60f, |
| 120 | 0.15f, 0.06f, |
| 121 | 0.3127f, 0.3290f }; |
| 122 | |
| 123 | static SkColorSpacePrimaries gAdobePrimaries = { |
| 124 | 0.64f, 0.33f, |
| 125 | 0.21f, 0.71f, |
| 126 | 0.15f, 0.06f, |
| 127 | 0.3127f, 0.3290f }; |
| 128 | |
| 129 | static SkColorSpacePrimaries gP3Primaries = { |
| 130 | 0.680f, 0.320f, |
| 131 | 0.265f, 0.690f, |
| 132 | 0.150f, 0.060f, |
| 133 | 0.3127f, 0.3290f }; |
| 134 | |
| 135 | static SkColorSpacePrimaries gRec2020Primaries = { |
| 136 | 0.708f, 0.292f, |
| 137 | 0.170f, 0.797f, |
| 138 | 0.131f, 0.046f, |
| 139 | 0.3127f, 0.3290f }; |
| 140 | |
| 141 | struct NamedPrimaries { |
| 142 | const char* fName; |
| 143 | SkColorSpacePrimaries* fPrimaries; |
| 144 | } gNamedPrimaries[] = { |
| 145 | { "sRGB", &gSrgbPrimaries }, |
| 146 | { "AdobeRGB", &gAdobePrimaries }, |
| 147 | { "P3", &gP3Primaries }, |
| 148 | { "Rec. 2020", &gRec2020Primaries }, |
| 149 | }; |
| 150 | |
| 151 | static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) { |
| 152 | return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0; |
| 153 | } |
| 154 | |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 155 | static Window::BackendType backend_type_for_window(Window::BackendType backendType) { |
| 156 | // In raster mode, we still use GL for the window. |
| 157 | // This lets us render the GUI faster (and correct). |
| 158 | return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType; |
| 159 | } |
| 160 | |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 161 | const char* kName = "name"; |
| 162 | const char* kValue = "value"; |
| 163 | const char* kOptions = "options"; |
| 164 | const char* kSlideStateName = "Slide"; |
| 165 | const char* kBackendStateName = "Backend"; |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 166 | const char* kMSAAStateName = "MSAA"; |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 167 | const char* kPathRendererStateName = "Path renderer"; |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 168 | const char* kSoftkeyStateName = "Softkey"; |
| 169 | const char* kSoftkeyHint = "Please select a softkey"; |
liyuqian | 1f508fd | 2016-06-07 06:57:40 -0700 | [diff] [blame] | 170 | const char* kFpsStateName = "FPS"; |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 171 | const char* kON = "ON"; |
| 172 | const char* kOFF = "OFF"; |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 173 | const char* kRefreshStateName = "Refresh"; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 174 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 175 | Viewer::Viewer(int argc, char** argv, void* platformData) |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 176 | : fCurrentSlide(-1) |
| 177 | , fRefresh(false) |
Brian Osman | 3ac99cf | 2017-12-01 11:23:53 -0500 | [diff] [blame] | 178 | , fSaveToSKP(false) |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 179 | , fShowImGuiDebugWindow(false) |
Brian Osman | fce09c5 | 2017-11-14 15:32:20 -0500 | [diff] [blame] | 180 | , fShowSlidePicker(false) |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 181 | , fShowImGuiTestWindow(false) |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 182 | , fShowZoomWindow(false) |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 183 | , fZoomWindowFixed(false) |
| 184 | , fZoomWindowLocation{0.0f, 0.0f} |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 185 | , fLastImage(nullptr) |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 186 | , fBackendType(sk_app::Window::kNativeGL_BackendType) |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 187 | , fColorMode(ColorMode::kLegacy) |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 188 | , fColorSpacePrimaries(gSrgbPrimaries) |
Brian Osman | fdab576 | 2017-11-09 10:27:55 -0500 | [diff] [blame] | 189 | // Our UI can only tweak gamma (currently), so start out gamma-only |
| 190 | , fColorSpaceTransferFn(g2Dot2_TransferFn) |
egdaniel | 2a0bb0a | 2016-04-11 08:30:40 -0700 | [diff] [blame] | 191 | , fZoomLevel(0.0f) |
Ben Wagner | d02a74d | 2018-04-23 12:55:06 -0400 | [diff] [blame] | 192 | , fRotation(0.0f) |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 193 | , fOffset{0.0f, 0.0f} |
Brian Osman | b53f48c | 2017-06-07 10:00:30 -0400 | [diff] [blame] | 194 | , fGestureDevice(GestureDevice::kNone) |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 195 | , fPerspectiveMode(kPerspective_Off) |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 196 | { |
Greg Daniel | 285db44 | 2016-10-14 09:12:53 -0400 | [diff] [blame] | 197 | SkGraphics::Init(); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 198 | |
Brian Osman | f09e35e | 2017-12-15 14:48:09 -0500 | [diff] [blame] | 199 | gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers"; |
Brian Osman | f09e35e | 2017-12-15 14:48:09 -0500 | [diff] [blame] | 200 | gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering"; |
Brian Osman | f09e35e | 2017-12-15 14:48:09 -0500 | [diff] [blame] | 201 | gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)"; |
| 202 | gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting"; |
| 203 | gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating"; |
| 204 | gPathRendererNames[GpuPathRenderers::kNone] = "Software masks"; |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 205 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 206 | SkDebugf("Command line arguments: "); |
| 207 | for (int i = 1; i < argc; ++i) { |
| 208 | SkDebugf("%s ", argv[i]); |
| 209 | } |
| 210 | SkDebugf("\n"); |
| 211 | |
| 212 | SkCommandLineFlags::Parse(argc, argv); |
Greg Daniel | 9fcc743 | 2016-11-29 16:35:19 -0500 | [diff] [blame] | 213 | #ifdef SK_BUILD_FOR_ANDROID |
Brian Salomon | 96789b3 | 2017-05-26 12:06:21 -0400 | [diff] [blame] | 214 | SetResourcePath("/data/local/tmp/resources"); |
Greg Daniel | 9fcc743 | 2016-11-29 16:35:19 -0500 | [diff] [blame] | 215 | #endif |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 216 | |
Ben Wagner | 483c772 | 2018-02-20 17:06:07 -0500 | [diff] [blame] | 217 | if (!FLAGS_nativeFonts) { |
| 218 | gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr; |
| 219 | } |
| 220 | |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 221 | initializeEventTracingForTools(); |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 222 | static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads); |
Greg Daniel | 285db44 | 2016-10-14 09:12:53 -0400 | [diff] [blame] | 223 | |
bsalomon | 6c471f7 | 2016-07-26 12:56:32 -0700 | [diff] [blame] | 224 | fBackendType = get_backend_type(FLAGS_backend[0]); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 225 | fWindow = Window::CreateNativeWindow(platformData); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 226 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 227 | DisplayParams displayParams; |
| 228 | displayParams.fMSAASampleCount = FLAGS_msaa; |
Chris Dalton | 040238b | 2017-12-18 14:22:34 -0700 | [diff] [blame] | 229 | SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 230 | fWindow->setRequestedDisplayParams(displayParams); |
| 231 | |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 232 | // Configure timers |
| 233 | fStatsLayer.setActive(false); |
| 234 | fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff); |
| 235 | fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN); |
| 236 | fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666); |
| 237 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 238 | // register callbacks |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 239 | fCommands.attach(fWindow); |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 240 | fWindow->pushLayer(this); |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 241 | fWindow->pushLayer(&fStatsLayer); |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 242 | fWindow->pushLayer(&fImGuiLayer); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 243 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 244 | // add key-bindings |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 245 | fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() { |
| 246 | this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow; |
| 247 | fWindow->inval(); |
| 248 | }); |
Brian Osman | fce09c5 | 2017-11-14 15:32:20 -0500 | [diff] [blame] | 249 | // Command to jump directly to the slide picker and give it focus |
| 250 | fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() { |
| 251 | this->fShowImGuiDebugWindow = true; |
| 252 | this->fShowSlidePicker = true; |
| 253 | fWindow->inval(); |
| 254 | }); |
| 255 | // Alias that to Backspace, to match SampleApp |
| 256 | fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() { |
| 257 | this->fShowImGuiDebugWindow = true; |
| 258 | this->fShowSlidePicker = true; |
| 259 | fWindow->inval(); |
| 260 | }); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 261 | fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() { |
| 262 | this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow; |
| 263 | fWindow->inval(); |
| 264 | }); |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 265 | fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() { |
| 266 | this->fShowZoomWindow = !this->fShowZoomWindow; |
| 267 | fWindow->inval(); |
| 268 | }); |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 269 | fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() { |
| 270 | this->fZoomWindowFixed = !this->fZoomWindowFixed; |
| 271 | fWindow->inval(); |
| 272 | }); |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 273 | fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() { |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 274 | fStatsLayer.setActive(!fStatsLayer.getActive()); |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 275 | fWindow->inval(); |
| 276 | }); |
Jim Van Verth | 90dcce5 | 2017-11-03 13:36:07 -0400 | [diff] [blame] | 277 | fCommands.addCommand('0', "Overlays", "Reset stats", [this]() { |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 278 | fStatsLayer.resetMeasurements(); |
Jim Van Verth | 90dcce5 | 2017-11-03 13:36:07 -0400 | [diff] [blame] | 279 | this->updateTitle(); |
| 280 | fWindow->inval(); |
| 281 | }); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 282 | fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() { |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 283 | switch (fColorMode) { |
| 284 | case ColorMode::kLegacy: |
| 285 | this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending); |
| 286 | break; |
| 287 | case ColorMode::kColorManagedSRGB8888_NonLinearBlending: |
| 288 | this->setColorMode(ColorMode::kColorManagedSRGB8888); |
| 289 | break; |
| 290 | case ColorMode::kColorManagedSRGB8888: |
| 291 | this->setColorMode(ColorMode::kColorManagedLinearF16); |
| 292 | break; |
| 293 | case ColorMode::kColorManagedLinearF16: |
| 294 | this->setColorMode(ColorMode::kLegacy); |
| 295 | break; |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 296 | } |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 297 | }); |
| 298 | fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() { |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 299 | this->setCurrentSlide(fCurrentSlide < fSlides.count() - 1 ? fCurrentSlide + 1 : 0); |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 300 | }); |
| 301 | fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() { |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 302 | this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.count() - 1); |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 303 | }); |
| 304 | fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() { |
| 305 | this->changeZoomLevel(1.f / 32.f); |
| 306 | fWindow->inval(); |
| 307 | }); |
| 308 | fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() { |
| 309 | this->changeZoomLevel(-1.f / 32.f); |
| 310 | fWindow->inval(); |
| 311 | }); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 312 | fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() { |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 313 | sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)( |
| 314 | (fBackendType + 1) % sk_app::Window::kBackendTypeCount); |
Jim Van Verth | d63c102 | 2017-01-05 13:50:49 -0500 | [diff] [blame] | 315 | // Switching to and from Vulkan is problematic on Linux so disabled for now |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 316 | #if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN) |
| 317 | if (newBackend == sk_app::Window::kVulkan_BackendType) { |
| 318 | newBackend = (sk_app::Window::BackendType)((newBackend + 1) % |
| 319 | sk_app::Window::kBackendTypeCount); |
| 320 | } else if (fBackendType == sk_app::Window::kVulkan_BackendType) { |
| 321 | newBackend = sk_app::Window::kVulkan_BackendType; |
Jim Van Verth | d63c102 | 2017-01-05 13:50:49 -0500 | [diff] [blame] | 322 | } |
| 323 | #endif |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 324 | this->setBackend(newBackend); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 325 | }); |
Brian Osman | 3ac99cf | 2017-12-01 11:23:53 -0500 | [diff] [blame] | 326 | fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() { |
| 327 | fSaveToSKP = true; |
| 328 | fWindow->inval(); |
| 329 | }); |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 330 | fCommands.addCommand('G', "Modes", "Geometry", [this]() { |
| 331 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 332 | uint32_t flags = params.fSurfaceProps.flags(); |
| 333 | if (!fPixelGeometryOverrides) { |
| 334 | fPixelGeometryOverrides = true; |
| 335 | params.fSurfaceProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry); |
| 336 | } else { |
| 337 | switch (params.fSurfaceProps.pixelGeometry()) { |
| 338 | case kUnknown_SkPixelGeometry: |
| 339 | params.fSurfaceProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry); |
| 340 | break; |
| 341 | case kRGB_H_SkPixelGeometry: |
| 342 | params.fSurfaceProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry); |
| 343 | break; |
| 344 | case kBGR_H_SkPixelGeometry: |
| 345 | params.fSurfaceProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry); |
| 346 | break; |
| 347 | case kRGB_V_SkPixelGeometry: |
| 348 | params.fSurfaceProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry); |
| 349 | break; |
| 350 | case kBGR_V_SkPixelGeometry: |
| 351 | params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 352 | fPixelGeometryOverrides = false; |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | fWindow->setRequestedDisplayParams(params); |
| 357 | this->updateTitle(); |
| 358 | fWindow->inval(); |
| 359 | }); |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 360 | fCommands.addCommand('H', "Paint", "Hinting mode", [this]() { |
| 361 | if (!fPaintOverrides.fHinting) { |
| 362 | fPaintOverrides.fHinting = true; |
| 363 | fPaint.setHinting(SkPaint::kNo_Hinting); |
| 364 | } else { |
| 365 | switch (fPaint.getHinting()) { |
| 366 | case SkPaint::kNo_Hinting: |
| 367 | fPaint.setHinting(SkPaint::kSlight_Hinting); |
| 368 | break; |
| 369 | case SkPaint::kSlight_Hinting: |
| 370 | fPaint.setHinting(SkPaint::kNormal_Hinting); |
| 371 | break; |
| 372 | case SkPaint::kNormal_Hinting: |
| 373 | fPaint.setHinting(SkPaint::kFull_Hinting); |
| 374 | break; |
| 375 | case SkPaint::kFull_Hinting: |
| 376 | fPaint.setHinting(SkPaint::kNo_Hinting); |
| 377 | fPaintOverrides.fHinting = false; |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | this->updateTitle(); |
| 382 | fWindow->inval(); |
| 383 | }); |
| 384 | fCommands.addCommand('A', "Paint", "Antialias Mode", [this]() { |
| 385 | if (!(fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag)) { |
| 386 | fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias; |
| 387 | fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag; |
| 388 | fPaint.setAntiAlias(false); |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 389 | gSkUseAnalyticAA = gSkForceAnalyticAA = false; |
| 390 | gSkUseDeltaAA = gSkForceDeltaAA = false; |
| 391 | } else { |
| 392 | fPaint.setAntiAlias(true); |
| 393 | switch (fPaintOverrides.fAntiAlias) { |
| 394 | case SkPaintFields::AntiAliasState::Alias: |
| 395 | fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Normal; |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 396 | gSkUseAnalyticAA = gSkForceAnalyticAA = false; |
| 397 | gSkUseDeltaAA = gSkForceDeltaAA = false; |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 398 | break; |
| 399 | case SkPaintFields::AntiAliasState::Normal: |
| 400 | fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAEnabled; |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 401 | gSkUseAnalyticAA = true; |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 402 | gSkForceAnalyticAA = false; |
| 403 | gSkUseDeltaAA = gSkForceDeltaAA = false; |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 404 | break; |
| 405 | case SkPaintFields::AntiAliasState::AnalyticAAEnabled: |
| 406 | fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::AnalyticAAForced; |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 407 | gSkUseAnalyticAA = gSkForceAnalyticAA = true; |
| 408 | gSkUseDeltaAA = gSkForceDeltaAA = false; |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 409 | break; |
| 410 | case SkPaintFields::AntiAliasState::AnalyticAAForced: |
| 411 | fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAEnabled; |
| 412 | gSkUseAnalyticAA = gSkForceAnalyticAA = false; |
| 413 | gSkUseDeltaAA = true; |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 414 | gSkForceDeltaAA = false; |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 415 | break; |
| 416 | case SkPaintFields::AntiAliasState::DeltaAAEnabled: |
| 417 | fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::DeltaAAForced; |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 418 | gSkUseAnalyticAA = gSkForceAnalyticAA = false; |
| 419 | gSkUseDeltaAA = gSkForceDeltaAA = true; |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 420 | break; |
| 421 | case SkPaintFields::AntiAliasState::DeltaAAForced: |
| 422 | fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias; |
| 423 | fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag; |
| 424 | gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA; |
| 425 | gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA; |
| 426 | gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA; |
| 427 | gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA; |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | this->updateTitle(); |
| 432 | fWindow->inval(); |
| 433 | }); |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 434 | fCommands.addCommand('D', "Modes", "DFT", [this]() { |
| 435 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 436 | uint32_t flags = params.fSurfaceProps.flags(); |
| 437 | flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag; |
| 438 | params.fSurfaceProps = SkSurfaceProps(flags, params.fSurfaceProps.pixelGeometry()); |
| 439 | fWindow->setRequestedDisplayParams(params); |
| 440 | this->updateTitle(); |
| 441 | fWindow->inval(); |
| 442 | }); |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 443 | fCommands.addCommand('L', "Paint", "Subpixel Antialias Mode", [this]() { |
| 444 | if (!(fPaintOverrides.fFlags & SkPaint::kLCDRenderText_Flag)) { |
| 445 | fPaintOverrides.fFlags |= SkPaint::kLCDRenderText_Flag; |
| 446 | fPaint.setLCDRenderText(false); |
| 447 | } else { |
| 448 | if (!fPaint.isLCDRenderText()) { |
| 449 | fPaint.setLCDRenderText(true); |
| 450 | } else { |
| 451 | fPaintOverrides.fFlags &= ~SkPaint::kLCDRenderText_Flag; |
| 452 | } |
| 453 | } |
| 454 | this->updateTitle(); |
| 455 | fWindow->inval(); |
| 456 | }); |
| 457 | fCommands.addCommand('S', "Paint", "Subpixel Position Mode", [this]() { |
| 458 | if (!(fPaintOverrides.fFlags & SkPaint::kSubpixelText_Flag)) { |
| 459 | fPaintOverrides.fFlags |= SkPaint::kSubpixelText_Flag; |
| 460 | fPaint.setSubpixelText(false); |
| 461 | } else { |
| 462 | if (!fPaint.isSubpixelText()) { |
| 463 | fPaint.setSubpixelText(true); |
| 464 | } else { |
| 465 | fPaintOverrides.fFlags &= ~SkPaint::kSubpixelText_Flag; |
| 466 | } |
| 467 | } |
| 468 | this->updateTitle(); |
| 469 | fWindow->inval(); |
| 470 | }); |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 471 | fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() { |
| 472 | fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake |
| 473 | : kPerspective_Real; |
| 474 | this->updateTitle(); |
| 475 | fWindow->inval(); |
| 476 | }); |
| 477 | fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() { |
| 478 | fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real |
| 479 | : kPerspective_Off; |
| 480 | this->updateTitle(); |
| 481 | fWindow->inval(); |
| 482 | }); |
Yuqian Li | b2ba664 | 2017-11-22 12:07:41 -0500 | [diff] [blame] | 483 | |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 484 | // set up slides |
| 485 | this->initSlides(); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 486 | if (FLAGS_list) { |
| 487 | this->listNames(); |
| 488 | } |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 489 | |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 490 | fPerspectivePoints[0].set(0, 0); |
| 491 | fPerspectivePoints[1].set(1, 0); |
| 492 | fPerspectivePoints[2].set(0, 1); |
| 493 | fPerspectivePoints[3].set(1, 1); |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 494 | fAnimTimer.run(); |
| 495 | |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 496 | auto gamutImage = GetResourceAsImage("images/gamut.png"); |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 497 | if (gamutImage) { |
Mike Reed | 0acd795 | 2017-04-28 11:12:19 -0400 | [diff] [blame] | 498 | fImGuiGamutPaint.setShader(gamutImage->makeShader()); |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 499 | } |
| 500 | fImGuiGamutPaint.setColor(SK_ColorWHITE); |
| 501 | fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality); |
| 502 | |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 503 | fWindow->attach(backend_type_for_window(fBackendType)); |
Jim Van Verth | 0848fb0 | 2018-01-22 13:39:30 -0500 | [diff] [blame] | 504 | this->setCurrentSlide(this->startupSlide()); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 505 | } |
| 506 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 507 | void Viewer::initSlides() { |
Florin Malita | 0ffa322 | 2018-04-05 14:34:45 -0400 | [diff] [blame] | 508 | using SlideFactory = sk_sp<Slide>(*)(const SkString& name, const SkString& path); |
| 509 | static const struct { |
| 510 | const char* fExtension; |
| 511 | const char* fDirName; |
| 512 | const SkCommandLineFlags::StringArray& fFlags; |
| 513 | const SlideFactory fFactory; |
| 514 | } gExternalSlidesInfo[] = { |
| 515 | { ".skp", "skp-dir", FLAGS_skps, |
| 516 | [](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 517 | return sk_make_sp<SKPSlide>(name, path);} |
| 518 | }, |
| 519 | { ".jpg", "jpg-dir", FLAGS_jpgs, |
| 520 | [](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 521 | return sk_make_sp<ImageSlide>(name, path);} |
| 522 | }, |
Florin Malita | 87ccf33 | 2018-05-04 12:23:24 -0400 | [diff] [blame] | 523 | #if defined(SK_ENABLE_SKOTTIE) |
Eric Boren | 8c172ba | 2018-07-19 13:27:49 -0400 | [diff] [blame] | 524 | { ".json", "skottie-dir", FLAGS_lotties, |
Florin Malita | 0ffa322 | 2018-04-05 14:34:45 -0400 | [diff] [blame] | 525 | [](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 526 | return sk_make_sp<SkottieSlide>(name, path);} |
| 527 | }, |
Florin Malita | 87ccf33 | 2018-05-04 12:23:24 -0400 | [diff] [blame] | 528 | #endif |
Florin Malita | 0ffa322 | 2018-04-05 14:34:45 -0400 | [diff] [blame] | 529 | { ".svg", "svg-dir", FLAGS_svgs, |
| 530 | [](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 531 | return sk_make_sp<SvgSlide>(name, path);} |
| 532 | }, |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 533 | #if !(defined(SK_BUILD_FOR_WIN) && defined(__clang__)) |
| 534 | { ".nima", "nima-dir", FLAGS_nimas, |
| 535 | [](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 536 | return sk_make_sp<NIMASlide>(name, path);} |
| 537 | }, |
| 538 | #endif |
Florin Malita | 0ffa322 | 2018-04-05 14:34:45 -0400 | [diff] [blame] | 539 | }; |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 540 | |
Florin Malita | 0ffa322 | 2018-04-05 14:34:45 -0400 | [diff] [blame] | 541 | SkTArray<sk_sp<Slide>, true> dirSlides; |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 542 | |
Florin Malita | 0ffa322 | 2018-04-05 14:34:45 -0400 | [diff] [blame] | 543 | const auto addSlide = [&](const SkString& name, |
| 544 | const SkString& path, |
| 545 | const SlideFactory& fact) { |
| 546 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) { |
| 547 | return; |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 548 | } |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 549 | |
Florin Malita | 0ffa322 | 2018-04-05 14:34:45 -0400 | [diff] [blame] | 550 | if (auto slide = fact(name, path)) { |
Florin Malita | 76a076b | 2018-02-15 18:40:48 -0500 | [diff] [blame] | 551 | dirSlides.push_back(slide); |
| 552 | fSlides.push_back(std::move(slide)); |
| 553 | } |
Florin Malita | 0ffa322 | 2018-04-05 14:34:45 -0400 | [diff] [blame] | 554 | }; |
Florin Malita | 76a076b | 2018-02-15 18:40:48 -0500 | [diff] [blame] | 555 | |
Florin Malita | 38792ce | 2018-05-08 10:36:18 -0400 | [diff] [blame] | 556 | if (!FLAGS_file.isEmpty()) { |
| 557 | // single file mode |
| 558 | const SkString file(FLAGS_file[0]); |
| 559 | |
| 560 | if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) { |
| 561 | for (const auto& sinfo : gExternalSlidesInfo) { |
| 562 | if (file.endsWith(sinfo.fExtension)) { |
| 563 | addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory); |
| 564 | return; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str()); |
| 569 | } else { |
| 570 | fprintf(stderr, "Cannot read \"%s\"\n", file.c_str()); |
| 571 | } |
| 572 | |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | // Bisect slide. |
| 577 | if (!FLAGS_bisect.isEmpty()) { |
| 578 | sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]); |
| 579 | if (bisect && !SkCommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) { |
| 580 | if (FLAGS_bisect.count() >= 2) { |
| 581 | for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) { |
| 582 | bisect->onChar(*ch); |
| 583 | } |
| 584 | } |
| 585 | fSlides.push_back(std::move(bisect)); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | // GMs |
| 590 | int firstGM = fSlides.count(); |
| 591 | const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head()); |
| 592 | while (gms) { |
| 593 | std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr)); |
| 594 | |
| 595 | if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) { |
| 596 | sk_sp<Slide> slide(new GMSlide(gm.release())); |
| 597 | fSlides.push_back(std::move(slide)); |
| 598 | } |
| 599 | |
| 600 | gms = gms->next(); |
| 601 | } |
| 602 | // reverse gms |
| 603 | int numGMs = fSlides.count() - firstGM; |
| 604 | for (int i = 0; i < numGMs/2; ++i) { |
| 605 | std::swap(fSlides[firstGM + i], fSlides[fSlides.count() - i - 1]); |
| 606 | } |
| 607 | |
| 608 | // samples |
| 609 | const SkViewRegister* reg = SkViewRegister::Head(); |
| 610 | while (reg) { |
| 611 | sk_sp<Slide> slide(new SampleSlide(reg->factory())); |
| 612 | if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) { |
| 613 | fSlides.push_back(slide); |
| 614 | } |
| 615 | reg = reg->next(); |
| 616 | } |
| 617 | |
Florin Malita | 0ffa322 | 2018-04-05 14:34:45 -0400 | [diff] [blame] | 618 | for (const auto& info : gExternalSlidesInfo) { |
| 619 | for (const auto& flag : info.fFlags) { |
| 620 | if (SkStrEndsWith(flag.c_str(), info.fExtension)) { |
| 621 | // single file |
| 622 | addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory); |
| 623 | } else { |
| 624 | // directory |
| 625 | SkOSFile::Iter it(flag.c_str(), info.fExtension); |
| 626 | SkString name; |
| 627 | while (it.next(&name)) { |
| 628 | addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory); |
| 629 | } |
Florin Malita | c659c2c | 2018-04-05 11:57:21 -0400 | [diff] [blame] | 630 | } |
Florin Malita | 3b526b0 | 2018-05-25 12:43:51 -0400 | [diff] [blame] | 631 | #if defined(SK_HAS_SKSG) |
Florin Malita | 0ffa322 | 2018-04-05 14:34:45 -0400 | [diff] [blame] | 632 | if (!dirSlides.empty()) { |
| 633 | fSlides.push_back( |
| 634 | sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()), |
| 635 | std::move(dirSlides))); |
| 636 | dirSlides.reset(); |
| 637 | } |
Florin Malita | 3b526b0 | 2018-05-25 12:43:51 -0400 | [diff] [blame] | 638 | #endif |
Florin Malita | c659c2c | 2018-04-05 11:57:21 -0400 | [diff] [blame] | 639 | } |
| 640 | } |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 644 | Viewer::~Viewer() { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 645 | fWindow->detach(); |
| 646 | delete fWindow; |
| 647 | } |
| 648 | |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 649 | struct SkPaintTitleUpdater { |
| 650 | SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {} |
| 651 | void append(const char* s) { |
| 652 | if (fCount == 0) { |
| 653 | fTitle->append(" {"); |
| 654 | } else { |
| 655 | fTitle->append(", "); |
| 656 | } |
| 657 | fTitle->append(s); |
| 658 | ++fCount; |
| 659 | } |
| 660 | void done() { |
| 661 | if (fCount > 0) { |
| 662 | fTitle->append("}"); |
| 663 | } |
| 664 | } |
| 665 | SkString* fTitle; |
| 666 | int fCount; |
| 667 | }; |
| 668 | |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 669 | void Viewer::updateTitle() { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 670 | if (!fWindow) { |
| 671 | return; |
| 672 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 673 | if (fWindow->sampleCount() < 1) { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 674 | return; // Surface hasn't been created yet. |
| 675 | } |
| 676 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 677 | SkString title("Viewer: "); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 678 | title.append(fSlides[fCurrentSlide]->getName()); |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 679 | |
Yuqian Li | 399b3c2 | 2017-08-03 11:08:15 -0400 | [diff] [blame] | 680 | if (gSkUseDeltaAA) { |
| 681 | if (gSkForceDeltaAA) { |
| 682 | title.append(" <FDAA>"); |
| 683 | } else { |
| 684 | title.append(" <DAA>"); |
| 685 | } |
| 686 | } else if (gSkUseAnalyticAA) { |
| 687 | if (gSkForceAnalyticAA) { |
| 688 | title.append(" <FAAA>"); |
| 689 | } else { |
| 690 | title.append(" <AAA>"); |
| 691 | } |
| 692 | } |
| 693 | |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 694 | SkPaintTitleUpdater paintTitle(&title); |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame] | 695 | auto paintFlag = [this, &paintTitle](SkPaint::Flags flag, bool (SkPaint::* isFlag)() const, |
| 696 | const char* on, const char* off) |
| 697 | { |
| 698 | if (fPaintOverrides.fFlags & flag) { |
| 699 | paintTitle.append((fPaint.*isFlag)() ? on : off); |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 700 | } |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame] | 701 | }; |
| 702 | |
| 703 | paintFlag(SkPaint::kAntiAlias_Flag, &SkPaint::isAntiAlias, "Antialias", "Alias"); |
| 704 | paintFlag(SkPaint::kDither_Flag, &SkPaint::isDither, "DITHER", "No Dither"); |
| 705 | paintFlag(SkPaint::kFakeBoldText_Flag, &SkPaint::isFakeBoldText, "Fake Bold", "No Fake Bold"); |
| 706 | paintFlag(SkPaint::kLinearText_Flag, &SkPaint::isLinearText, "Linear Text", "Non-Linear Text"); |
| 707 | paintFlag(SkPaint::kSubpixelText_Flag, &SkPaint::isSubpixelText, "Subpixel Text", "Pixel Text"); |
| 708 | paintFlag(SkPaint::kLCDRenderText_Flag, &SkPaint::isLCDRenderText, "LCD", "lcd"); |
| 709 | paintFlag(SkPaint::kEmbeddedBitmapText_Flag, &SkPaint::isEmbeddedBitmapText, |
| 710 | "Bitmap Text", "No Bitmap Text"); |
| 711 | paintFlag(SkPaint::kAutoHinting_Flag, &SkPaint::isAutohinted, |
| 712 | "Force Autohint", "No Force Autohint"); |
| 713 | paintFlag(SkPaint::kVerticalText_Flag, &SkPaint::isVerticalText, |
| 714 | "Vertical Text", "No Vertical Text"); |
| 715 | |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 716 | if (fPaintOverrides.fHinting) { |
| 717 | switch (fPaint.getHinting()) { |
| 718 | case SkPaint::kNo_Hinting: |
| 719 | paintTitle.append("No Hinting"); |
| 720 | break; |
| 721 | case SkPaint::kSlight_Hinting: |
| 722 | paintTitle.append("Slight Hinting"); |
| 723 | break; |
| 724 | case SkPaint::kNormal_Hinting: |
| 725 | paintTitle.append("Normal Hinting"); |
| 726 | break; |
| 727 | case SkPaint::kFull_Hinting: |
| 728 | paintTitle.append("Full Hinting"); |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | paintTitle.done(); |
| 733 | |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 734 | switch (fColorMode) { |
| 735 | case ColorMode::kLegacy: |
| 736 | title.append(" Legacy 8888"); |
| 737 | break; |
| 738 | case ColorMode::kColorManagedSRGB8888_NonLinearBlending: |
| 739 | title.append(" ColorManaged 8888 (Nonlinear blending)"); |
| 740 | break; |
| 741 | case ColorMode::kColorManagedSRGB8888: |
| 742 | title.append(" ColorManaged 8888"); |
| 743 | break; |
| 744 | case ColorMode::kColorManagedLinearF16: |
| 745 | title.append(" ColorManaged F16"); |
| 746 | break; |
| 747 | } |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 748 | |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 749 | if (ColorMode::kLegacy != fColorMode) { |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 750 | int curPrimaries = -1; |
| 751 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) { |
| 752 | if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) { |
| 753 | curPrimaries = i; |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom"); |
Brian Osman | fdab576 | 2017-11-09 10:27:55 -0500 | [diff] [blame] | 758 | |
| 759 | if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) { |
| 760 | title.appendf(" Gamma %f", fColorSpaceTransferFn.fG); |
| 761 | } |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 762 | } |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 763 | |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 764 | const DisplayParams& params = fWindow->getRequestedDisplayParams(); |
| 765 | if (fPixelGeometryOverrides) { |
| 766 | switch (params.fSurfaceProps.pixelGeometry()) { |
| 767 | case kUnknown_SkPixelGeometry: |
| 768 | title.append( " Flat"); |
| 769 | break; |
| 770 | case kRGB_H_SkPixelGeometry: |
| 771 | title.append( " RGB"); |
| 772 | break; |
| 773 | case kBGR_H_SkPixelGeometry: |
| 774 | title.append( " BGR"); |
| 775 | break; |
| 776 | case kRGB_V_SkPixelGeometry: |
| 777 | title.append( " RGBV"); |
| 778 | break; |
| 779 | case kBGR_V_SkPixelGeometry: |
| 780 | title.append( " BGRV"); |
| 781 | break; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | if (params.fSurfaceProps.isUseDeviceIndependentFonts()) { |
| 786 | title.append(" DFT"); |
| 787 | } |
| 788 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 789 | title.append(" ["); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 790 | title.append(kBackendTypeStrings[fBackendType]); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 791 | int msaa = fWindow->sampleCount(); |
| 792 | if (msaa > 1) { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 793 | title.appendf(" MSAA: %i", msaa); |
| 794 | } |
| 795 | title.append("]"); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 796 | |
| 797 | GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers; |
Brian Osman | 1994f20 | 2018-07-10 11:41:22 -0400 | [diff] [blame] | 798 | if (GpuPathRenderers::kAll != pr) { |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 799 | title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str()); |
| 800 | } |
| 801 | |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 802 | if (kPerspective_Real == fPerspectiveMode) { |
| 803 | title.append(" Perpsective (Real)"); |
| 804 | } else if (kPerspective_Fake == fPerspectiveMode) { |
| 805 | title.append(" Perspective (Fake)"); |
| 806 | } |
| 807 | |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 808 | fWindow->setTitle(title.c_str()); |
| 809 | } |
| 810 | |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 811 | int Viewer::startupSlide() const { |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 812 | |
| 813 | if (!FLAGS_slide.isEmpty()) { |
| 814 | int count = fSlides.count(); |
| 815 | for (int i = 0; i < count; i++) { |
| 816 | if (fSlides[i]->getName().equals(FLAGS_slide[0])) { |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 817 | return i; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| 821 | fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]); |
| 822 | this->listNames(); |
| 823 | } |
| 824 | |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 825 | return 0; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 826 | } |
| 827 | |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 828 | void Viewer::listNames() const { |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 829 | SkDebugf("All Slides:\n"); |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 830 | for (const auto& slide : fSlides) { |
| 831 | SkDebugf(" %s\n", slide->getName().c_str()); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 835 | void Viewer::setCurrentSlide(int slide) { |
| 836 | SkASSERT(slide >= 0 && slide < fSlides.count()); |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 837 | |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 838 | if (slide == fCurrentSlide) { |
| 839 | return; |
| 840 | } |
| 841 | |
| 842 | if (fCurrentSlide >= 0) { |
| 843 | fSlides[fCurrentSlide]->unload(); |
| 844 | } |
| 845 | |
| 846 | fSlides[slide]->load(SkIntToScalar(fWindow->width()), |
| 847 | SkIntToScalar(fWindow->height())); |
| 848 | fCurrentSlide = slide; |
| 849 | this->setupCurrentSlide(); |
| 850 | } |
| 851 | |
| 852 | void Viewer::setupCurrentSlide() { |
Jim Van Verth | 0848fb0 | 2018-01-22 13:39:30 -0500 | [diff] [blame] | 853 | if (fCurrentSlide >= 0) { |
| 854 | // prepare dimensions for image slides |
| 855 | fGesture.resetTouchState(); |
| 856 | fDefaultMatrix.reset(); |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 857 | |
Jim Van Verth | 0848fb0 | 2018-01-22 13:39:30 -0500 | [diff] [blame] | 858 | const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); |
| 859 | const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height()); |
| 860 | const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height()); |
Brian Osman | 42bb6ac | 2017-06-05 08:46:04 -0400 | [diff] [blame] | 861 | |
Jim Van Verth | 0848fb0 | 2018-01-22 13:39:30 -0500 | [diff] [blame] | 862 | // Start with a matrix that scales the slide to the available screen space |
| 863 | if (fWindow->scaleContentToFit()) { |
| 864 | if (windowRect.width() > 0 && windowRect.height() > 0) { |
| 865 | fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit); |
| 866 | } |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 867 | } |
Jim Van Verth | 0848fb0 | 2018-01-22 13:39:30 -0500 | [diff] [blame] | 868 | |
| 869 | // Prevent the user from dragging content so far outside the window they can't find it again |
Yuqian Li | 755778c | 2018-03-28 16:23:31 -0400 | [diff] [blame] | 870 | fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix()); |
Jim Van Verth | 0848fb0 | 2018-01-22 13:39:30 -0500 | [diff] [blame] | 871 | |
| 872 | this->updateTitle(); |
| 873 | this->updateUIState(); |
| 874 | |
| 875 | fStatsLayer.resetMeasurements(); |
| 876 | |
| 877 | fWindow->inval(); |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 878 | } |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | #define MAX_ZOOM_LEVEL 8 |
| 882 | #define MIN_ZOOM_LEVEL -8 |
| 883 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 884 | void Viewer::changeZoomLevel(float delta) { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 885 | fZoomLevel += delta; |
Brian Osman | 42bb6ac | 2017-06-05 08:46:04 -0400 | [diff] [blame] | 886 | fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL); |
Ben Wagner | d02a74d | 2018-04-23 12:55:06 -0400 | [diff] [blame] | 887 | this->preTouchMatrixChanged(); |
| 888 | } |
Yuqian Li | 755778c | 2018-03-28 16:23:31 -0400 | [diff] [blame] | 889 | |
Ben Wagner | d02a74d | 2018-04-23 12:55:06 -0400 | [diff] [blame] | 890 | void Viewer::preTouchMatrixChanged() { |
| 891 | // Update the trans limit as the transform changes. |
Yuqian Li | 755778c | 2018-03-28 16:23:31 -0400 | [diff] [blame] | 892 | const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); |
| 893 | const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height()); |
| 894 | const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height()); |
| 895 | fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix()); |
| 896 | } |
| 897 | |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 898 | SkMatrix Viewer::computePerspectiveMatrix() { |
| 899 | SkScalar w = fWindow->width(), h = fWindow->height(); |
| 900 | SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } }; |
| 901 | SkPoint perspPts[4] = { |
| 902 | { fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h }, |
| 903 | { fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h }, |
| 904 | { fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h }, |
| 905 | { fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h } |
| 906 | }; |
| 907 | SkMatrix m; |
| 908 | m.setPolyToPoly(orthoPts, perspPts, 4); |
| 909 | return m; |
| 910 | } |
| 911 | |
Yuqian Li | 755778c | 2018-03-28 16:23:31 -0400 | [diff] [blame] | 912 | SkMatrix Viewer::computePreTouchMatrix() { |
| 913 | SkMatrix m = fDefaultMatrix; |
| 914 | SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel) |
| 915 | : SK_Scalar1 + fZoomLevel; |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 916 | m.preTranslate(fOffset.x(), fOffset.y()); |
Yuqian Li | 755778c | 2018-03-28 16:23:31 -0400 | [diff] [blame] | 917 | m.preScale(zoomScale, zoomScale); |
Brian Osman | bdaf97b | 2018-04-26 16:22:42 -0400 | [diff] [blame] | 918 | |
| 919 | const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); |
| 920 | m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f); |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 921 | |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 922 | if (kPerspective_Real == fPerspectiveMode) { |
| 923 | SkMatrix persp = this->computePerspectiveMatrix(); |
Brian Osman | bdaf97b | 2018-04-26 16:22:42 -0400 | [diff] [blame] | 924 | m.postConcat(persp); |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 925 | } |
| 926 | |
Yuqian Li | 755778c | 2018-03-28 16:23:31 -0400 | [diff] [blame] | 927 | return m; |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 928 | } |
| 929 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 930 | SkMatrix Viewer::computeMatrix() { |
Yuqian Li | 755778c | 2018-03-28 16:23:31 -0400 | [diff] [blame] | 931 | SkMatrix m = fGesture.localM(); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 932 | m.preConcat(fGesture.globalM()); |
Yuqian Li | 755778c | 2018-03-28 16:23:31 -0400 | [diff] [blame] | 933 | m.preConcat(this->computePreTouchMatrix()); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 934 | return m; |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 937 | void Viewer::setBackend(sk_app::Window::BackendType backendType) { |
| 938 | fBackendType = backendType; |
| 939 | |
| 940 | fWindow->detach(); |
| 941 | |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 942 | #if defined(SK_BUILD_FOR_WIN) |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 943 | // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point |
| 944 | // on Windows, so we just delete the window and recreate it. |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 945 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 946 | delete fWindow; |
| 947 | fWindow = Window::CreateNativeWindow(nullptr); |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 948 | |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 949 | // re-register callbacks |
| 950 | fCommands.attach(fWindow); |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 951 | fWindow->pushLayer(this); |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 952 | fWindow->pushLayer(&fStatsLayer); |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 953 | fWindow->pushLayer(&fImGuiLayer); |
| 954 | |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 955 | // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above |
| 956 | // will still include our correct sample count. But the re-created fWindow will lose that |
| 957 | // information. On Windows, we need to re-create the window when changing sample count, |
| 958 | // so we'll incorrectly detect that situation, then re-initialize the window in GL mode, |
| 959 | // rendering this tear-down step pointless (and causing the Vulkan window context to fail |
| 960 | // as if we had never changed windows at all). |
| 961 | fWindow->setRequestedDisplayParams(params, false); |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 962 | #endif |
| 963 | |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 964 | fWindow->attach(backend_type_for_window(fBackendType)); |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 965 | } |
| 966 | |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 967 | void Viewer::setColorMode(ColorMode colorMode) { |
| 968 | fColorMode = colorMode; |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 969 | |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 970 | // 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] | 971 | // or out of legacy/nonlinear mode, we need to update our window configuration. |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 972 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 973 | bool wasInLegacy = !SkToBool(params.fColorSpace); |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 974 | bool wantLegacy = (ColorMode::kLegacy == fColorMode) || |
| 975 | (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode); |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 976 | if (wasInLegacy != wantLegacy) { |
| 977 | params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB(); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 978 | fWindow->setRequestedDisplayParams(params); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | this->updateTitle(); |
| 982 | fWindow->inval(); |
| 983 | } |
| 984 | |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 985 | class OveridePaintFilterCanvas : public SkPaintFilterCanvas { |
| 986 | public: |
| 987 | OveridePaintFilterCanvas(SkCanvas* canvas, SkPaint* paint, Viewer::SkPaintFields* fields) |
| 988 | : SkPaintFilterCanvas(canvas), fPaint(paint), fPaintOverrides(fields) |
| 989 | { } |
| 990 | bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override { |
Ben Wagner | aa5da73 | 2018-03-28 13:36:02 -0400 | [diff] [blame] | 991 | if (*paint == nullptr) { |
| 992 | return true; |
| 993 | } |
Ben Wagner | d2ae4df | 2018-06-07 17:54:07 -0400 | [diff] [blame] | 994 | if (fPaintOverrides->fTextSize) { |
| 995 | paint->writable()->setTextSize(fPaint->getTextSize()); |
| 996 | } |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 997 | if (fPaintOverrides->fHinting) { |
| 998 | paint->writable()->setHinting(fPaint->getHinting()); |
| 999 | } |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame] | 1000 | |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 1001 | if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) { |
| 1002 | paint->writable()->setAntiAlias(fPaint->isAntiAlias()); |
| 1003 | } |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame] | 1004 | if (fPaintOverrides->fFlags & SkPaint::kDither_Flag) { |
| 1005 | paint->writable()->setDither(fPaint->isDither()); |
| 1006 | } |
| 1007 | if (fPaintOverrides->fFlags & SkPaint::kFakeBoldText_Flag) { |
| 1008 | paint->writable()->setFakeBoldText(fPaint->isFakeBoldText()); |
| 1009 | } |
| 1010 | if (fPaintOverrides->fFlags & SkPaint::kLinearText_Flag) { |
| 1011 | paint->writable()->setLinearText(fPaint->isLinearText()); |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 1012 | } |
| 1013 | if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) { |
| 1014 | paint->writable()->setSubpixelText(fPaint->isSubpixelText()); |
| 1015 | } |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame] | 1016 | if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) { |
| 1017 | paint->writable()->setLCDRenderText(fPaint->isLCDRenderText()); |
| 1018 | } |
| 1019 | if (fPaintOverrides->fFlags & SkPaint::kEmbeddedBitmapText_Flag) { |
| 1020 | paint->writable()->setEmbeddedBitmapText(fPaint->isEmbeddedBitmapText()); |
| 1021 | } |
| 1022 | if (fPaintOverrides->fFlags & SkPaint::kAutoHinting_Flag) { |
| 1023 | paint->writable()->setAutohinted(fPaint->isAutohinted()); |
| 1024 | } |
| 1025 | if (fPaintOverrides->fFlags & SkPaint::kVerticalText_Flag) { |
| 1026 | paint->writable()->setVerticalText(fPaint->isVerticalText()); |
| 1027 | } |
| 1028 | |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 1029 | return true; |
| 1030 | } |
| 1031 | SkPaint* fPaint; |
| 1032 | Viewer::SkPaintFields* fPaintOverrides; |
| 1033 | }; |
| 1034 | |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1035 | void Viewer::drawSlide(SkCanvas* canvas) { |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 1036 | SkAutoCanvasRestore autorestore(canvas, false); |
| 1037 | |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1038 | // By default, we render directly into the window's surface/canvas |
| 1039 | SkCanvas* slideCanvas = canvas; |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1040 | fLastImage.reset(); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 1041 | |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1042 | // If we're in any of the color managed modes, construct the color space we're going to use |
| 1043 | sk_sp<SkColorSpace> cs = nullptr; |
| 1044 | if (ColorMode::kLegacy != fColorMode) { |
| 1045 | auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode) |
| 1046 | ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma; |
Mike Klein | c722f79 | 2017-07-31 11:57:21 -0400 | [diff] [blame] | 1047 | SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor); |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1048 | SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ)); |
Brian Osman | fdab576 | 2017-11-09 10:27:55 -0500 | [diff] [blame] | 1049 | if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) { |
| 1050 | cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ); |
| 1051 | } else { |
| 1052 | cs = SkColorSpace::MakeRGB(transferFn, toXYZ); |
| 1053 | } |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
Brian Osman | 3ac99cf | 2017-12-01 11:23:53 -0500 | [diff] [blame] | 1056 | if (fSaveToSKP) { |
| 1057 | SkPictureRecorder recorder; |
| 1058 | SkCanvas* recorderCanvas = recorder.beginRecording( |
| 1059 | SkRect::Make(fSlides[fCurrentSlide]->getDimensions())); |
| 1060 | // In xform-canvas mode, record the transformed output |
| 1061 | std::unique_ptr<SkCanvas> xformCanvas = nullptr; |
| 1062 | if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) { |
| 1063 | xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs); |
| 1064 | recorderCanvas = xformCanvas.get(); |
| 1065 | } |
| 1066 | fSlides[fCurrentSlide]->draw(recorderCanvas); |
| 1067 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
| 1068 | SkFILEWStream stream("sample_app.skp"); |
| 1069 | picture->serialize(&stream); |
| 1070 | fSaveToSKP = false; |
| 1071 | } |
| 1072 | |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1073 | // If we're in F16, or we're zooming, or we're in color correct 8888 and the gamut isn't sRGB, |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 1074 | // we need to render offscreen. We also need to render offscreen if we're in any raster mode, |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 1075 | // because the window surface is actually GL, or we're doing fake perspective. |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1076 | sk_sp<SkSurface> offscreenSurface = nullptr; |
Yuqian Li | b2ba664 | 2017-11-22 12:07:41 -0500 | [diff] [blame] | 1077 | std::unique_ptr<SkCanvas> threadedCanvas; |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 1078 | if (Window::kRaster_BackendType == fBackendType || |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 1079 | kPerspective_Fake == fPerspectiveMode || |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 1080 | ColorMode::kColorManagedLinearF16 == fColorMode || |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1081 | fShowZoomWindow || |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1082 | (ColorMode::kColorManagedSRGB8888 == fColorMode && |
| 1083 | !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) { |
| 1084 | |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1085 | SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode) |
| 1086 | ? kRGBA_F16_SkColorType : kN32_SkColorType; |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1087 | // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it |
| 1088 | // with a special canvas (below) that has the color space attached |
| 1089 | sk_sp<SkColorSpace> offscreenColorSpace = |
| 1090 | (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs; |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1091 | SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType, |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1092 | kPremul_SkAlphaType, std::move(offscreenColorSpace)); |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 1093 | SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |
| 1094 | canvas->getProps(&props); |
| 1095 | offscreenSurface = Window::kRaster_BackendType == fBackendType |
| 1096 | ? SkSurface::MakeRaster(info, &props) |
| 1097 | : canvas->makeSurface(info); |
Yuqian Li | b2ba664 | 2017-11-22 12:07:41 -0500 | [diff] [blame] | 1098 | SkPixmap offscreenPixmap; |
Mike Klein | 48b6490 | 2018-07-25 13:28:44 -0400 | [diff] [blame] | 1099 | slideCanvas = offscreenSurface->getCanvas(); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1100 | } |
| 1101 | |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1102 | std::unique_ptr<SkCanvas> xformCanvas = nullptr; |
| 1103 | if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) { |
| 1104 | xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs); |
| 1105 | slideCanvas = xformCanvas.get(); |
| 1106 | } |
| 1107 | |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 1108 | int count = slideCanvas->save(); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1109 | slideCanvas->clear(SK_ColorWHITE); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1110 | slideCanvas->concat(computeMatrix()); |
Brian Osman | e19e23f | 2018-05-22 16:56:40 -0400 | [diff] [blame] | 1111 | if (kPerspective_Real == fPerspectiveMode) { |
| 1112 | slideCanvas->clipRect(SkRect::MakeWH(fWindow->width(), fWindow->height())); |
| 1113 | } |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1114 | // Time the painting logic of the slide |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1115 | fStatsLayer.beginTiming(fPaintTimer); |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 1116 | OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides); |
| 1117 | fSlides[fCurrentSlide]->draw(&filterCanvas); |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1118 | fStatsLayer.endTiming(fPaintTimer); |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 1119 | slideCanvas->restoreToCount(count); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1120 | |
| 1121 | // Force a flush so we can time that, too |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1122 | fStatsLayer.beginTiming(fFlushTimer); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1123 | slideCanvas->flush(); |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1124 | fStatsLayer.endTiming(fFlushTimer); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1125 | |
| 1126 | // If we rendered offscreen, snap an image and push the results to the window's canvas |
| 1127 | if (offscreenSurface) { |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1128 | fLastImage = offscreenSurface->makeImageSnapshot(); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1129 | |
| 1130 | // 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] | 1131 | sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode) |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1132 | ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB(); |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1133 | auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get()); |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 1134 | SkPaint paint; |
| 1135 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 1136 | int prePerspectiveCount = canvas->save(); |
| 1137 | if (kPerspective_Fake == fPerspectiveMode) { |
| 1138 | paint.setFilterQuality(kHigh_SkFilterQuality); |
| 1139 | canvas->clear(SK_ColorWHITE); |
| 1140 | canvas->concat(this->computePerspectiveMatrix()); |
| 1141 | } |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 1142 | canvas->drawImage(retaggedImage, 0, 0, &paint); |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 1143 | canvas->restoreToCount(prePerspectiveCount); |
liyuqian | 74959a1 | 2016-06-16 14:10:34 -0700 | [diff] [blame] | 1144 | } |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 1147 | void Viewer::onBackendCreated() { |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 1148 | this->setupCurrentSlide(); |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 1149 | fWindow->show(); |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 1150 | } |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 1151 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 1152 | void Viewer::onPaint(SkCanvas* canvas) { |
Jim Van Verth | 90dcce5 | 2017-11-03 13:36:07 -0400 | [diff] [blame] | 1153 | this->drawSlide(canvas); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 1154 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 1155 | fCommands.drawHelp(canvas); |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 1156 | |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 1157 | this->drawImGui(); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Florin Malita | cefc1b9 | 2018-02-19 21:43:47 -0500 | [diff] [blame] | 1160 | SkPoint Viewer::mapEvent(float x, float y) { |
| 1161 | const auto m = this->computeMatrix(); |
| 1162 | SkMatrix inv; |
| 1163 | |
| 1164 | SkAssertResult(m.invert(&inv)); |
| 1165 | |
| 1166 | return inv.mapXY(x, y); |
| 1167 | } |
| 1168 | |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 1169 | bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) { |
Brian Osman | b53f48c | 2017-06-07 10:00:30 -0400 | [diff] [blame] | 1170 | if (GestureDevice::kMouse == fGestureDevice) { |
| 1171 | return false; |
| 1172 | } |
Florin Malita | cefc1b9 | 2018-02-19 21:43:47 -0500 | [diff] [blame] | 1173 | |
| 1174 | const auto slidePt = this->mapEvent(x, y); |
| 1175 | if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) { |
| 1176 | fWindow->inval(); |
| 1177 | return true; |
| 1178 | } |
| 1179 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 1180 | void* castedOwner = reinterpret_cast<void*>(owner); |
| 1181 | switch (state) { |
| 1182 | case Window::kUp_InputState: { |
| 1183 | fGesture.touchEnd(castedOwner); |
| 1184 | break; |
| 1185 | } |
| 1186 | case Window::kDown_InputState: { |
Brian Osman | 42bb6ac | 2017-06-05 08:46:04 -0400 | [diff] [blame] | 1187 | fGesture.touchBegin(castedOwner, x, y); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 1188 | break; |
| 1189 | } |
| 1190 | case Window::kMove_InputState: { |
Brian Osman | 42bb6ac | 2017-06-05 08:46:04 -0400 | [diff] [blame] | 1191 | fGesture.touchMoved(castedOwner, x, y); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 1192 | break; |
| 1193 | } |
| 1194 | } |
Brian Osman | b53f48c | 2017-06-07 10:00:30 -0400 | [diff] [blame] | 1195 | fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone; |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 1196 | fWindow->inval(); |
| 1197 | return true; |
| 1198 | } |
| 1199 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 1200 | bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) { |
Brian Osman | 16c81a1 | 2017-12-20 11:58:34 -0500 | [diff] [blame] | 1201 | if (GestureDevice::kTouch == fGestureDevice) { |
| 1202 | return false; |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 1203 | } |
Brian Osman | 16c81a1 | 2017-12-20 11:58:34 -0500 | [diff] [blame] | 1204 | |
Florin Malita | cefc1b9 | 2018-02-19 21:43:47 -0500 | [diff] [blame] | 1205 | const auto slidePt = this->mapEvent(x, y); |
| 1206 | if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) { |
| 1207 | fWindow->inval(); |
| 1208 | return true; |
Brian Osman | 16c81a1 | 2017-12-20 11:58:34 -0500 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | switch (state) { |
| 1212 | case Window::kUp_InputState: { |
| 1213 | fGesture.touchEnd(nullptr); |
| 1214 | break; |
| 1215 | } |
| 1216 | case Window::kDown_InputState: { |
| 1217 | fGesture.touchBegin(nullptr, x, y); |
| 1218 | break; |
| 1219 | } |
| 1220 | case Window::kMove_InputState: { |
| 1221 | fGesture.touchMoved(nullptr, x, y); |
| 1222 | break; |
| 1223 | } |
| 1224 | } |
| 1225 | fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone; |
| 1226 | |
| 1227 | if (state != Window::kMove_InputState || fGesture.isBeingTouched()) { |
| 1228 | fWindow->inval(); |
| 1229 | } |
Jim Van Verth | e770578 | 2017-05-04 14:00:59 -0400 | [diff] [blame] | 1230 | return true; |
| 1231 | } |
| 1232 | |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1233 | static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y, |
| 1234 | const ImVec2& pos, const ImVec2& size) { |
| 1235 | // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip) |
| 1236 | ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y); |
| 1237 | |
| 1238 | // Invisible 10x10 button |
| 1239 | ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5)); |
| 1240 | ImGui::InvisibleButton(label, ImVec2(10, 10)); |
| 1241 | |
| 1242 | if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) { |
| 1243 | ImGuiIO& io = ImGui::GetIO(); |
| 1244 | // Normalized mouse position, relative to our gamut box |
| 1245 | ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y); |
| 1246 | // Clamp to edge of box, convert back to primary scale |
| 1247 | *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f; |
| 1248 | *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f; |
| 1249 | } |
| 1250 | |
| 1251 | if (ImGui::IsItemHovered()) { |
| 1252 | ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y); |
| 1253 | } |
| 1254 | |
| 1255 | // Return screen coordinates for the caller. We could just return center here, but we'd have |
| 1256 | // one frame of lag during drag. |
| 1257 | return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y); |
| 1258 | } |
| 1259 | |
| 1260 | static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) { |
| 1261 | ImDrawList* drawList = ImGui::GetWindowDrawList(); |
| 1262 | |
| 1263 | // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available |
| 1264 | // width, and scale the height to maintain aspect ratio. |
| 1265 | float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f); |
| 1266 | ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f)); |
| 1267 | ImVec2 pos = ImGui::GetCursorScreenPos(); |
| 1268 | |
| 1269 | // Background image. Only draw a subset of the image, to avoid the regions less than zero. |
| 1270 | // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area. |
| 1271 | // Magic numbers are pixel locations of the origin and upper-right corner. |
| 1272 | drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y), |
| 1273 | ImVec2(242, 61), ImVec2(1897, 1922)); |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1274 | |
| 1275 | // Primary markers |
| 1276 | ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size); |
| 1277 | ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size); |
| 1278 | ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size); |
| 1279 | ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size); |
| 1280 | |
| 1281 | // Gamut triangle |
| 1282 | drawList->AddCircle(r, 5.0f, 0xFF000040); |
| 1283 | drawList->AddCircle(g, 5.0f, 0xFF004000); |
| 1284 | drawList->AddCircle(b, 5.0f, 0xFF400000); |
| 1285 | drawList->AddCircle(w, 5.0f, 0xFFFFFFFF); |
| 1286 | drawList->AddTriangle(r, g, b, 0xFFFFFFFF); |
| 1287 | |
| 1288 | // Re-position cursor immediate after the diagram for subsequent controls |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 1289 | ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y)); |
| 1290 | } |
| 1291 | |
| 1292 | static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p, |
| 1293 | const ImVec2& pos, const ImVec2& size, bool* dragging) { |
| 1294 | // Transform points ([0, 0] - [1.0, 1.0]) to screen coords |
| 1295 | ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y); |
| 1296 | |
| 1297 | // Invisible 10x10 button |
| 1298 | ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5)); |
| 1299 | ImGui::InvisibleButton(label, ImVec2(10, 10)); |
| 1300 | |
| 1301 | if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) { |
| 1302 | ImGuiIO& io = ImGui::GetIO(); |
| 1303 | // Normalized mouse position, relative to our gamut box |
| 1304 | ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y); |
| 1305 | // Clamp to edge of box |
| 1306 | p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f); |
| 1307 | p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f); |
| 1308 | *dragging = true; |
| 1309 | } |
| 1310 | |
| 1311 | // Return screen coordinates for the caller. We could just return center here, but we'd have |
| 1312 | // one frame of lag during drag. |
| 1313 | return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y); |
| 1314 | } |
| 1315 | |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 1316 | static bool ImGui_DragLocation(SkPoint* pt) { |
| 1317 | ImDrawList* drawList = ImGui::GetWindowDrawList(); |
| 1318 | |
| 1319 | // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio. |
| 1320 | float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f); |
| 1321 | ImVec2 size = ImVec2(canvasWidth, canvasWidth); |
| 1322 | ImVec2 pos = ImGui::GetCursorScreenPos(); |
| 1323 | |
| 1324 | // Background rectangle |
| 1325 | drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128)); |
| 1326 | |
| 1327 | // Location marker |
| 1328 | bool dragging = false; |
| 1329 | ImVec2 tl = ImGui_DragPoint("SL", pt + 0, pos, size, &dragging); |
| 1330 | drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF); |
| 1331 | |
| 1332 | ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y)); |
| 1333 | ImGui::Spacing(); |
| 1334 | |
| 1335 | return dragging; |
| 1336 | } |
| 1337 | |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 1338 | static bool ImGui_DragQuad(SkPoint* pts) { |
| 1339 | ImDrawList* drawList = ImGui::GetWindowDrawList(); |
| 1340 | |
| 1341 | // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio. |
| 1342 | float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f); |
| 1343 | ImVec2 size = ImVec2(canvasWidth, canvasWidth); |
| 1344 | ImVec2 pos = ImGui::GetCursorScreenPos(); |
| 1345 | |
| 1346 | // Background rectangle |
| 1347 | drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128)); |
| 1348 | |
| 1349 | // Corner markers |
| 1350 | bool dragging = false; |
| 1351 | ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging); |
| 1352 | ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging); |
| 1353 | ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging); |
| 1354 | ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging); |
| 1355 | |
| 1356 | // Draw markers and quad |
| 1357 | drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF); |
| 1358 | drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF); |
| 1359 | drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF); |
| 1360 | drawList->AddCircle(br, 5.0f, 0xFFFFFFFF); |
| 1361 | drawList->AddLine(tl, tr, 0xFFFFFFFF); |
| 1362 | drawList->AddLine(tr, br, 0xFFFFFFFF); |
| 1363 | drawList->AddLine(br, bl, 0xFFFFFFFF); |
| 1364 | drawList->AddLine(bl, tl, 0xFFFFFFFF); |
| 1365 | |
| 1366 | ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y)); |
| 1367 | ImGui::Spacing(); |
| 1368 | |
| 1369 | return dragging; |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1370 | } |
| 1371 | |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 1372 | void Viewer::drawImGui() { |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1373 | // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible |
| 1374 | if (fShowImGuiTestWindow) { |
Brian Osman | 7197e05 | 2018-06-29 14:30:48 -0400 | [diff] [blame] | 1375 | ImGui::ShowDemoWindow(&fShowImGuiTestWindow); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | if (fShowImGuiDebugWindow) { |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1379 | // We have some dynamic content that sizes to fill available size. If the scroll bar isn't |
| 1380 | // always visible, we can end up in a layout feedback loop. |
Brian Osman | 7197e05 | 2018-06-29 14:30:48 -0400 | [diff] [blame] | 1381 | ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1382 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 1383 | bool paramsChanged = false; |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1384 | if (ImGui::Begin("Tools", &fShowImGuiDebugWindow, |
| 1385 | ImGuiWindowFlags_AlwaysVerticalScrollbar)) { |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 1386 | if (ImGui::CollapsingHeader("Backend")) { |
| 1387 | int newBackend = static_cast<int>(fBackendType); |
| 1388 | ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType); |
| 1389 | ImGui::SameLine(); |
| 1390 | ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType); |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 1391 | #if SK_ANGLE && defined(SK_BUILD_FOR_WIN) |
| 1392 | ImGui::SameLine(); |
| 1393 | ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType); |
| 1394 | #endif |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 1395 | #if defined(SK_VULKAN) |
| 1396 | ImGui::SameLine(); |
| 1397 | ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType); |
| 1398 | #endif |
| 1399 | if (newBackend != fBackendType) { |
| 1400 | fDeferredActions.push_back([=]() { |
| 1401 | this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend)); |
| 1402 | }); |
| 1403 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1404 | |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1405 | const GrContext* ctx = fWindow->getGrContext(); |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 1406 | bool* wire = ¶ms.fGrContextOptions.fWireframeMode; |
| 1407 | if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) { |
| 1408 | paramsChanged = true; |
| 1409 | } |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1410 | |
Brian Osman | 28b1252 | 2017-03-08 17:10:24 -0500 | [diff] [blame] | 1411 | if (ctx) { |
| 1412 | int sampleCount = fWindow->sampleCount(); |
| 1413 | ImGui::Text("MSAA: "); ImGui::SameLine(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1414 | ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine(); |
Brian Osman | 28b1252 | 2017-03-08 17:10:24 -0500 | [diff] [blame] | 1415 | ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine(); |
| 1416 | ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine(); |
| 1417 | ImGui::RadioButton("16", &sampleCount, 16); |
| 1418 | |
| 1419 | if (sampleCount != params.fMSAASampleCount) { |
| 1420 | params.fMSAASampleCount = sampleCount; |
| 1421 | paramsChanged = true; |
| 1422 | } |
| 1423 | } |
| 1424 | |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 1425 | int pixelGeometryIdx = 0; |
| 1426 | if (fPixelGeometryOverrides) { |
| 1427 | pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1; |
| 1428 | } |
| 1429 | if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx, |
| 1430 | "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0")) |
| 1431 | { |
| 1432 | uint32_t flags = params.fSurfaceProps.flags(); |
| 1433 | if (pixelGeometryIdx == 0) { |
| 1434 | fPixelGeometryOverrides = false; |
| 1435 | params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 1436 | } else { |
| 1437 | fPixelGeometryOverrides = true; |
| 1438 | SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1); |
| 1439 | params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry); |
| 1440 | } |
| 1441 | paramsChanged = true; |
| 1442 | } |
| 1443 | |
| 1444 | bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts(); |
| 1445 | if (ImGui::Checkbox("DFT", &useDFT)) { |
| 1446 | uint32_t flags = params.fSurfaceProps.flags(); |
| 1447 | if (useDFT) { |
| 1448 | flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag; |
| 1449 | } else { |
| 1450 | flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag; |
| 1451 | } |
| 1452 | SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry(); |
| 1453 | params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry); |
| 1454 | paramsChanged = true; |
| 1455 | } |
| 1456 | |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1457 | if (ImGui::TreeNode("Path Renderers")) { |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1458 | GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers; |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1459 | auto prButton = [&](GpuPathRenderers x) { |
| 1460 | if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) { |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1461 | if (x != params.fGrContextOptions.fGpuPathRenderers) { |
| 1462 | params.fGrContextOptions.fGpuPathRenderers = x; |
| 1463 | paramsChanged = true; |
| 1464 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1465 | } |
| 1466 | }; |
| 1467 | |
| 1468 | if (!ctx) { |
| 1469 | ImGui::RadioButton("Software", true); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1470 | } else if (fWindow->sampleCount() > 1) { |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1471 | prButton(GpuPathRenderers::kAll); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 1472 | if (ctx->contextPriv().caps()->shaderCaps()->pathRenderingSupport()) { |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1473 | prButton(GpuPathRenderers::kStencilAndCover); |
| 1474 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1475 | prButton(GpuPathRenderers::kTessellating); |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1476 | prButton(GpuPathRenderers::kNone); |
| 1477 | } else { |
| 1478 | prButton(GpuPathRenderers::kAll); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 1479 | if (GrCoverageCountingPathRenderer::IsSupported( |
| 1480 | *ctx->contextPriv().caps())) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1481 | prButton(GpuPathRenderers::kCoverageCounting); |
| 1482 | } |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 1483 | prButton(GpuPathRenderers::kSmall); |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1484 | prButton(GpuPathRenderers::kTessellating); |
| 1485 | prButton(GpuPathRenderers::kNone); |
| 1486 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1487 | ImGui::TreePop(); |
| 1488 | } |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 1489 | } |
| 1490 | |
Ben Wagner | d02a74d | 2018-04-23 12:55:06 -0400 | [diff] [blame] | 1491 | if (ImGui::CollapsingHeader("Transform")) { |
| 1492 | float zoom = fZoomLevel; |
| 1493 | if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) { |
| 1494 | fZoomLevel = zoom; |
| 1495 | this->preTouchMatrixChanged(); |
| 1496 | paramsChanged = true; |
| 1497 | } |
| 1498 | float deg = fRotation; |
Ben Wagner | cb13935 | 2018-05-04 10:33:04 -0400 | [diff] [blame] | 1499 | if (ImGui::SliderFloat("Rotate", °, -30, 360, "%.3f deg")) { |
Ben Wagner | d02a74d | 2018-04-23 12:55:06 -0400 | [diff] [blame] | 1500 | fRotation = deg; |
| 1501 | this->preTouchMatrixChanged(); |
| 1502 | paramsChanged = true; |
| 1503 | } |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 1504 | if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) { |
| 1505 | if (ImGui_DragLocation(&fOffset)) { |
| 1506 | this->preTouchMatrixChanged(); |
| 1507 | paramsChanged = true; |
| 1508 | } |
| 1509 | } |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 1510 | int perspectiveMode = static_cast<int>(fPerspectiveMode); |
| 1511 | if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) { |
| 1512 | fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode); |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 1513 | this->preTouchMatrixChanged(); |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 1514 | paramsChanged = true; |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 1515 | } |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 1516 | if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) { |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 1517 | this->preTouchMatrixChanged(); |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 1518 | paramsChanged = true; |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 1519 | } |
Ben Wagner | d02a74d | 2018-04-23 12:55:06 -0400 | [diff] [blame] | 1520 | } |
| 1521 | |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 1522 | if (ImGui::CollapsingHeader("Paint")) { |
| 1523 | int hintingIdx = 0; |
| 1524 | if (fPaintOverrides.fHinting) { |
| 1525 | hintingIdx = fPaint.getHinting() + 1; |
| 1526 | } |
| 1527 | if (ImGui::Combo("Hinting", &hintingIdx, |
| 1528 | "Default\0None\0Slight\0Normal\0Full\0\0")) |
| 1529 | { |
| 1530 | if (hintingIdx == 0) { |
| 1531 | fPaintOverrides.fHinting = false; |
| 1532 | fPaint.setHinting(SkPaint::kNo_Hinting); |
| 1533 | } else { |
| 1534 | fPaintOverrides.fHinting = true; |
| 1535 | SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1); |
| 1536 | fPaint.setHinting(hinting); |
| 1537 | } |
| 1538 | paramsChanged = true; |
| 1539 | } |
| 1540 | |
| 1541 | int aliasIdx = 0; |
| 1542 | if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) { |
| 1543 | aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1; |
| 1544 | } |
| 1545 | if (ImGui::Combo("Anti-Alias", &aliasIdx, |
| 1546 | "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0" |
| 1547 | "DeltaAAEnabled\0DeltaAAForced\0\0")) |
| 1548 | { |
| 1549 | gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA; |
| 1550 | gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA; |
| 1551 | gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA; |
| 1552 | gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA; |
| 1553 | if (aliasIdx == 0) { |
| 1554 | fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias; |
| 1555 | fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag; |
| 1556 | } else { |
| 1557 | fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag; |
| 1558 | fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1); |
| 1559 | fPaint.setAntiAlias(aliasIdx > 1); |
| 1560 | switch (fPaintOverrides.fAntiAlias) { |
| 1561 | case SkPaintFields::AntiAliasState::Alias: |
| 1562 | break; |
| 1563 | case SkPaintFields::AntiAliasState::Normal: |
| 1564 | break; |
| 1565 | case SkPaintFields::AntiAliasState::AnalyticAAEnabled: |
| 1566 | gSkUseAnalyticAA = true; |
| 1567 | gSkForceAnalyticAA = false; |
| 1568 | gSkUseDeltaAA = gSkForceDeltaAA = false; |
| 1569 | break; |
| 1570 | case SkPaintFields::AntiAliasState::AnalyticAAForced: |
| 1571 | gSkUseAnalyticAA = gSkForceAnalyticAA = true; |
| 1572 | gSkUseDeltaAA = gSkForceDeltaAA = false; |
| 1573 | break; |
| 1574 | case SkPaintFields::AntiAliasState::DeltaAAEnabled: |
| 1575 | gSkUseAnalyticAA = gSkForceAnalyticAA = false; |
| 1576 | gSkUseDeltaAA = true; |
| 1577 | gSkForceDeltaAA = false; |
| 1578 | break; |
| 1579 | case SkPaintFields::AntiAliasState::DeltaAAForced: |
| 1580 | gSkUseAnalyticAA = gSkForceAnalyticAA = false; |
| 1581 | gSkUseDeltaAA = gSkForceDeltaAA = true; |
| 1582 | break; |
| 1583 | } |
| 1584 | } |
| 1585 | paramsChanged = true; |
| 1586 | } |
| 1587 | |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame] | 1588 | auto paintFlag = [this, ¶msChanged](const char* label, const char* items, |
| 1589 | SkPaint::Flags flag, |
| 1590 | bool (SkPaint::* isFlag)() const, |
| 1591 | void (SkPaint::* setFlag)(bool) ) |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 1592 | { |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame] | 1593 | int itemIndex = 0; |
| 1594 | if (fPaintOverrides.fFlags & flag) { |
| 1595 | itemIndex = (fPaint.*isFlag)() ? 2 : 1; |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 1596 | } |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame] | 1597 | if (ImGui::Combo(label, &itemIndex, items)) { |
| 1598 | if (itemIndex == 0) { |
| 1599 | fPaintOverrides.fFlags &= ~flag; |
| 1600 | } else { |
| 1601 | fPaintOverrides.fFlags |= flag; |
| 1602 | (fPaint.*setFlag)(itemIndex == 2); |
| 1603 | } |
| 1604 | paramsChanged = true; |
| 1605 | } |
| 1606 | }; |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 1607 | |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame] | 1608 | paintFlag("Dither", |
| 1609 | "Default\0No Dither\0Dither\0\0", |
| 1610 | SkPaint::kDither_Flag, |
| 1611 | &SkPaint::isDither, &SkPaint::setDither); |
| 1612 | |
| 1613 | paintFlag("Fake Bold Glyphs", |
| 1614 | "Default\0No Fake Bold\0Fake Bold\0\0", |
| 1615 | SkPaint::kFakeBoldText_Flag, |
| 1616 | &SkPaint::isFakeBoldText, &SkPaint::setFakeBoldText); |
| 1617 | |
| 1618 | paintFlag("Linear Text", |
| 1619 | "Default\0No Linear Text\0Linear Text\0\0", |
| 1620 | SkPaint::kLinearText_Flag, |
| 1621 | &SkPaint::isLinearText, &SkPaint::setLinearText); |
| 1622 | |
| 1623 | paintFlag("Subpixel Position Glyphs", |
| 1624 | "Default\0Pixel Text\0Subpixel Text\0\0", |
| 1625 | SkPaint::kSubpixelText_Flag, |
| 1626 | &SkPaint::isSubpixelText, &SkPaint::setSubpixelText); |
| 1627 | |
| 1628 | paintFlag("Subpixel Anti-Alias", |
| 1629 | "Default\0lcd\0LCD\0\0", |
| 1630 | SkPaint::kLCDRenderText_Flag, |
| 1631 | &SkPaint::isLCDRenderText, &SkPaint::setLCDRenderText); |
| 1632 | |
| 1633 | paintFlag("Embedded Bitmap Text", |
| 1634 | "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0", |
| 1635 | SkPaint::kEmbeddedBitmapText_Flag, |
| 1636 | &SkPaint::isEmbeddedBitmapText, &SkPaint::setEmbeddedBitmapText); |
| 1637 | |
| 1638 | paintFlag("Force Auto-Hinting", |
| 1639 | "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0", |
| 1640 | SkPaint::kAutoHinting_Flag, |
| 1641 | &SkPaint::isAutohinted, &SkPaint::setAutohinted); |
| 1642 | |
| 1643 | paintFlag("Vertical Text", |
| 1644 | "Default\0No Vertical Text\0Vertical Text\0\0", |
| 1645 | SkPaint::kVerticalText_Flag, |
| 1646 | &SkPaint::isVerticalText, &SkPaint::setVerticalText); |
Ben Wagner | d2ae4df | 2018-06-07 17:54:07 -0400 | [diff] [blame] | 1647 | |
| 1648 | ImGui::Checkbox("Override TextSize", &fPaintOverrides.fTextSize); |
| 1649 | if (fPaintOverrides.fTextSize) { |
| 1650 | ImGui::DragFloat2("TextRange", fPaintOverrides.fTextSizeRange, |
| 1651 | 0.001f, -10.0f, 300.0f, "%.6f", 2.0f); |
| 1652 | float textSize = fPaint.getTextSize(); |
| 1653 | if (ImGui::DragFloat("TextSize", &textSize, 0.001f, |
| 1654 | fPaintOverrides.fTextSizeRange[0], |
| 1655 | fPaintOverrides.fTextSizeRange[1], |
| 1656 | "%.6f", 2.0f)) |
| 1657 | { |
| 1658 | fPaint.setTextSize(textSize); |
| 1659 | this->preTouchMatrixChanged(); |
| 1660 | paramsChanged = true; |
| 1661 | } |
| 1662 | } |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 1663 | } |
| 1664 | |
Mike Reed | 81f60ec | 2018-05-15 10:09:52 -0400 | [diff] [blame] | 1665 | { |
| 1666 | SkMetaData controls; |
| 1667 | if (fSlides[fCurrentSlide]->onGetControls(&controls)) { |
| 1668 | if (ImGui::CollapsingHeader("Current Slide")) { |
| 1669 | SkMetaData::Iter iter(controls); |
| 1670 | const char* name; |
| 1671 | SkMetaData::Type type; |
| 1672 | int count; |
| 1673 | bool found = false; |
| 1674 | while ((name = iter.next(&type, &count)) != nullptr && found == false) { |
| 1675 | if (type == SkMetaData::kScalar_Type) { |
| 1676 | float val[3]; |
| 1677 | SkASSERT(count == 3); |
| 1678 | controls.findScalars(name, &count, val); |
| 1679 | if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) { |
| 1680 | controls.setScalars(name, 3, val); |
| 1681 | fSlides[fCurrentSlide]->onSetControls(controls); |
| 1682 | found = paramsChanged = true; |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | |
Ben Wagner | 7a3c674 | 2018-04-23 10:01:07 -0400 | [diff] [blame] | 1690 | if (fShowSlidePicker) { |
| 1691 | ImGui::SetNextTreeNodeOpen(true); |
| 1692 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1693 | if (ImGui::CollapsingHeader("Slide")) { |
| 1694 | static ImGuiTextFilter filter; |
Brian Osman | f479e42 | 2017-11-08 13:11:36 -0500 | [diff] [blame] | 1695 | static ImVector<const char*> filteredSlideNames; |
| 1696 | static ImVector<int> filteredSlideIndices; |
| 1697 | |
Brian Osman | fce09c5 | 2017-11-14 15:32:20 -0500 | [diff] [blame] | 1698 | if (fShowSlidePicker) { |
| 1699 | ImGui::SetKeyboardFocusHere(); |
| 1700 | fShowSlidePicker = false; |
| 1701 | } |
| 1702 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1703 | filter.Draw(); |
Brian Osman | f479e42 | 2017-11-08 13:11:36 -0500 | [diff] [blame] | 1704 | filteredSlideNames.clear(); |
| 1705 | filteredSlideIndices.clear(); |
| 1706 | int filteredIndex = 0; |
| 1707 | for (int i = 0; i < fSlides.count(); ++i) { |
| 1708 | const char* slideName = fSlides[i]->getName().c_str(); |
| 1709 | if (filter.PassFilter(slideName) || i == fCurrentSlide) { |
| 1710 | if (i == fCurrentSlide) { |
| 1711 | filteredIndex = filteredSlideIndices.size(); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1712 | } |
Brian Osman | f479e42 | 2017-11-08 13:11:36 -0500 | [diff] [blame] | 1713 | filteredSlideNames.push_back(slideName); |
| 1714 | filteredSlideIndices.push_back(i); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1715 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1716 | } |
Brian Osman | f479e42 | 2017-11-08 13:11:36 -0500 | [diff] [blame] | 1717 | |
Brian Osman | f479e42 | 2017-11-08 13:11:36 -0500 | [diff] [blame] | 1718 | if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(), |
| 1719 | filteredSlideNames.size(), 20)) { |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 1720 | this->setCurrentSlide(filteredSlideIndices[filteredIndex]); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1721 | } |
| 1722 | } |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1723 | |
| 1724 | if (ImGui::CollapsingHeader("Color Mode")) { |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1725 | ColorMode newMode = fColorMode; |
| 1726 | auto cmButton = [&](ColorMode mode, const char* label) { |
| 1727 | if (ImGui::RadioButton(label, mode == fColorMode)) { |
| 1728 | newMode = mode; |
| 1729 | } |
| 1730 | }; |
| 1731 | |
| 1732 | cmButton(ColorMode::kLegacy, "Legacy 8888"); |
| 1733 | cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending, |
| 1734 | "Color Managed 8888 (Nonlinear blending)"); |
| 1735 | cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888"); |
| 1736 | cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16"); |
| 1737 | |
| 1738 | if (newMode != fColorMode) { |
Brian Osman | fd8f4d5 | 2017-02-24 11:57:23 -0500 | [diff] [blame] | 1739 | // It isn't safe to switch color mode now (in the middle of painting). We might |
| 1740 | // tear down the back-end, etc... Defer this change until the next onIdle. |
| 1741 | fDeferredActions.push_back([=]() { |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1742 | this->setColorMode(newMode); |
Brian Osman | fd8f4d5 | 2017-02-24 11:57:23 -0500 | [diff] [blame] | 1743 | }); |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | // Pick from common gamuts: |
| 1747 | int primariesIdx = 4; // Default: Custom |
| 1748 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) { |
| 1749 | if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) { |
| 1750 | primariesIdx = i; |
| 1751 | break; |
| 1752 | } |
| 1753 | } |
| 1754 | |
Brian Osman | fdab576 | 2017-11-09 10:27:55 -0500 | [diff] [blame] | 1755 | // When we're in xform canvas mode, we can alter the transfer function, too |
| 1756 | if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) { |
| 1757 | ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f); |
| 1758 | } |
| 1759 | |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1760 | if (ImGui::Combo("Primaries", &primariesIdx, |
| 1761 | "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) { |
| 1762 | if (primariesIdx >= 0 && primariesIdx <= 3) { |
| 1763 | fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | // Allow direct editing of gamut |
| 1768 | ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint); |
| 1769 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1770 | } |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1771 | if (paramsChanged) { |
| 1772 | fDeferredActions.push_back([=]() { |
| 1773 | fWindow->setRequestedDisplayParams(params); |
| 1774 | fWindow->inval(); |
| 1775 | this->updateTitle(); |
| 1776 | }); |
| 1777 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1778 | ImGui::End(); |
| 1779 | } |
| 1780 | |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1781 | if (fShowZoomWindow && fLastImage) { |
Brian Osman | 7197e05 | 2018-06-29 14:30:48 -0400 | [diff] [blame] | 1782 | ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver); |
| 1783 | if (ImGui::Begin("Zoom", &fShowZoomWindow)) { |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1784 | static int zoomFactor = 8; |
| 1785 | if (ImGui::Button("<<")) { |
| 1786 | zoomFactor = SkTMax(zoomFactor / 2, 4); |
| 1787 | } |
| 1788 | ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine(); |
| 1789 | if (ImGui::Button(">>")) { |
| 1790 | zoomFactor = SkTMin(zoomFactor * 2, 32); |
| 1791 | } |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1792 | |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 1793 | if (!fZoomWindowFixed) { |
| 1794 | ImVec2 mousePos = ImGui::GetMousePos(); |
| 1795 | fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y); |
| 1796 | } |
| 1797 | SkScalar x = fZoomWindowLocation.x(); |
| 1798 | SkScalar y = fZoomWindowLocation.y(); |
| 1799 | int xInt = SkScalarRoundToInt(x); |
| 1800 | int yInt = SkScalarRoundToInt(y); |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1801 | ImVec2 avail = ImGui::GetContentRegionAvail(); |
| 1802 | |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1803 | uint32_t pixel = 0; |
| 1804 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 1805 | if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), xInt, yInt)) { |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1806 | ImGui::SameLine(); |
Brian Osman | 07b56b2 | 2017-11-21 14:59:31 -0500 | [diff] [blame] | 1807 | ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x", |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 1808 | xInt, yInt, |
Brian Osman | 07b56b2 | 2017-11-21 14:59:31 -0500 | [diff] [blame] | 1809 | SkGetPackedR32(pixel), SkGetPackedG32(pixel), |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1810 | SkGetPackedB32(pixel), SkGetPackedA32(pixel)); |
| 1811 | } |
| 1812 | |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 1813 | fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) { |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1814 | // Translate so the region of the image that's under the mouse cursor is centered |
| 1815 | // in the zoom canvas: |
| 1816 | c->scale(zoomFactor, zoomFactor); |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 1817 | c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f, |
| 1818 | avail.y * 0.5f / zoomFactor - y - 0.5f); |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1819 | c->drawImage(this->fLastImage, 0, 0); |
| 1820 | |
| 1821 | SkPaint outline; |
| 1822 | outline.setStyle(SkPaint::kStroke_Style); |
Ben Wagner | 3627d2e | 2018-06-26 14:23:20 -0400 | [diff] [blame] | 1823 | c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline); |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1824 | }); |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | ImGui::End(); |
| 1828 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1829 | } |
| 1830 | |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 1831 | void Viewer::onIdle() { |
Brian Osman | fd8f4d5 | 2017-02-24 11:57:23 -0500 | [diff] [blame] | 1832 | for (int i = 0; i < fDeferredActions.count(); ++i) { |
| 1833 | fDeferredActions[i](); |
| 1834 | } |
| 1835 | fDeferredActions.reset(); |
| 1836 | |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1837 | fStatsLayer.beginTiming(fAnimateTimer); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 1838 | fAnimTimer.updateTime(); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1839 | bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer); |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1840 | fStatsLayer.endTiming(fAnimateTimer); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1841 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1842 | ImGuiIO& io = ImGui::GetIO(); |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1843 | if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 1844 | fWindow->inval(); |
| 1845 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 1846 | } |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1847 | |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 1848 | template <typename OptionsFunc> |
| 1849 | static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value, |
| 1850 | OptionsFunc&& optionsFunc) { |
| 1851 | writer.beginObject(); |
| 1852 | { |
| 1853 | writer.appendString(kName , name); |
| 1854 | writer.appendString(kValue, value); |
| 1855 | |
| 1856 | writer.beginArray(kOptions); |
| 1857 | { |
| 1858 | optionsFunc(writer); |
| 1859 | } |
| 1860 | writer.endArray(); |
| 1861 | } |
| 1862 | writer.endObject(); |
| 1863 | } |
| 1864 | |
| 1865 | |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1866 | void Viewer::updateUIState() { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1867 | if (!fWindow) { |
| 1868 | return; |
| 1869 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1870 | if (fWindow->sampleCount() < 1) { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1871 | return; // Surface hasn't been created yet. |
| 1872 | } |
| 1873 | |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 1874 | SkDynamicMemoryWStream memStream; |
| 1875 | SkJSONWriter writer(&memStream); |
| 1876 | writer.beginArray(); |
| 1877 | |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1878 | // Slide state |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 1879 | WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(), |
| 1880 | [this](SkJSONWriter& writer) { |
| 1881 | for(const auto& slide : fSlides) { |
| 1882 | writer.appendString(slide->getName().c_str()); |
| 1883 | } |
| 1884 | }); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1885 | |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1886 | // Backend state |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 1887 | WriteStateObject(writer, kBackendStateName, kBackendTypeStrings[fBackendType], |
| 1888 | [](SkJSONWriter& writer) { |
| 1889 | for (const auto& str : kBackendTypeStrings) { |
| 1890 | writer.appendString(str); |
| 1891 | } |
| 1892 | }); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1893 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1894 | // MSAA state |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 1895 | const auto countString = SkStringPrintf("%d", fWindow->sampleCount()); |
| 1896 | WriteStateObject(writer, kMSAAStateName, countString.c_str(), |
| 1897 | [this](SkJSONWriter& writer) { |
| 1898 | writer.appendS32(0); |
| 1899 | |
| 1900 | if (sk_app::Window::kRaster_BackendType == fBackendType) { |
| 1901 | return; |
| 1902 | } |
| 1903 | |
| 1904 | for (int msaa : {4, 8, 16}) { |
| 1905 | writer.appendS32(msaa); |
| 1906 | } |
| 1907 | }); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1908 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1909 | // Path renderer state |
| 1910 | GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers; |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 1911 | WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(), |
| 1912 | [this](SkJSONWriter& writer) { |
| 1913 | const GrContext* ctx = fWindow->getGrContext(); |
| 1914 | if (!ctx) { |
| 1915 | writer.appendString("Software"); |
| 1916 | } else { |
| 1917 | const auto* caps = ctx->contextPriv().caps(); |
| 1918 | |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 1919 | writer.appendString(gPathRendererNames[GpuPathRenderers::kAll].c_str()); |
| 1920 | if (fWindow->sampleCount() > 1) { |
| 1921 | if (caps->shaderCaps()->pathRenderingSupport()) { |
| 1922 | writer.appendString( |
| 1923 | gPathRendererNames[GpuPathRenderers::kStencilAndCover].c_str()); |
| 1924 | } |
| 1925 | } else { |
| 1926 | if(GrCoverageCountingPathRenderer::IsSupported(*caps)) { |
| 1927 | writer.appendString( |
| 1928 | gPathRendererNames[GpuPathRenderers::kCoverageCounting].c_str()); |
| 1929 | } |
| 1930 | writer.appendString(gPathRendererNames[GpuPathRenderers::kSmall].c_str()); |
| 1931 | } |
| 1932 | writer.appendString( |
| 1933 | gPathRendererNames[GpuPathRenderers::kTessellating].c_str()); |
| 1934 | writer.appendString(gPathRendererNames[GpuPathRenderers::kNone].c_str()); |
| 1935 | } |
| 1936 | }); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1937 | |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1938 | // Softkey state |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 1939 | WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint, |
| 1940 | [this](SkJSONWriter& writer) { |
| 1941 | writer.appendString(kSoftkeyHint); |
| 1942 | for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) { |
| 1943 | writer.appendString(softkey.c_str()); |
| 1944 | } |
| 1945 | }); |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1946 | |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 1947 | writer.endArray(); |
| 1948 | writer.flush(); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1949 | |
Florin Malita | b632df7 | 2018-06-18 21:23:06 -0400 | [diff] [blame] | 1950 | auto data = memStream.detachAsData(); |
| 1951 | |
| 1952 | // TODO: would be cool to avoid this copy |
| 1953 | const SkString cstring(static_cast<const char*>(data->data()), data->size()); |
| 1954 | |
| 1955 | fWindow->setUIState(cstring.c_str()); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) { |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1959 | // For those who will add more features to handle the state change in this function: |
| 1960 | // After the change, please call updateUIState no notify the frontend (e.g., Android app). |
| 1961 | // For example, after slide change, updateUIState is called inside setupCurrentSlide; |
| 1962 | // after backend change, updateUIState is called in this function. |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1963 | if (stateName.equals(kSlideStateName)) { |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 1964 | for (int i = 0; i < fSlides.count(); ++i) { |
| 1965 | if (fSlides[i]->getName().equals(stateValue)) { |
| 1966 | this->setCurrentSlide(i); |
| 1967 | return; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1968 | } |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1969 | } |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 1970 | |
| 1971 | SkDebugf("Slide not found: %s", stateValue.c_str()); |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1972 | } else if (stateName.equals(kBackendStateName)) { |
| 1973 | for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) { |
| 1974 | if (stateValue.equals(kBackendTypeStrings[i])) { |
| 1975 | if (fBackendType != i) { |
| 1976 | fBackendType = (sk_app::Window::BackendType)i; |
| 1977 | fWindow->detach(); |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 1978 | fWindow->attach(backend_type_for_window(fBackendType)); |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1979 | } |
| 1980 | break; |
| 1981 | } |
| 1982 | } |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1983 | } else if (stateName.equals(kMSAAStateName)) { |
| 1984 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 1985 | int sampleCount = atoi(stateValue.c_str()); |
| 1986 | if (sampleCount != params.fMSAASampleCount) { |
| 1987 | params.fMSAASampleCount = sampleCount; |
| 1988 | fWindow->setRequestedDisplayParams(params); |
| 1989 | fWindow->inval(); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1990 | this->updateTitle(); |
| 1991 | this->updateUIState(); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1992 | } |
| 1993 | } else if (stateName.equals(kPathRendererStateName)) { |
| 1994 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 1995 | for (const auto& pair : gPathRendererNames) { |
| 1996 | if (pair.second == stateValue.c_str()) { |
| 1997 | if (params.fGrContextOptions.fGpuPathRenderers != pair.first) { |
| 1998 | params.fGrContextOptions.fGpuPathRenderers = pair.first; |
| 1999 | fWindow->setRequestedDisplayParams(params); |
| 2000 | fWindow->inval(); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 2001 | this->updateTitle(); |
| 2002 | this->updateUIState(); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 2003 | } |
| 2004 | break; |
| 2005 | } |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 2006 | } |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 2007 | } else if (stateName.equals(kSoftkeyStateName)) { |
| 2008 | if (!stateValue.equals(kSoftkeyHint)) { |
| 2009 | fCommands.onSoftkey(stateValue); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 2010 | this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 2011 | } |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 2012 | } else if (stateName.equals(kRefreshStateName)) { |
| 2013 | // This state is actually NOT in the UI state. |
| 2014 | // We use this to allow Android to quickly set bool fRefresh. |
| 2015 | fRefresh = stateValue.equals(kON); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 2016 | } else { |
| 2017 | SkDebugf("Unknown stateName: %s", stateName.c_str()); |
| 2018 | } |
| 2019 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 2020 | |
| 2021 | bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) { |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 2022 | return fCommands.onKey(key, state, modifiers); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | bool Viewer::onChar(SkUnichar c, uint32_t modifiers) { |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 2026 | if (fSlides[fCurrentSlide]->onChar(c)) { |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 2027 | fWindow->inval(); |
| 2028 | return true; |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 2029 | } else { |
| 2030 | return fCommands.onChar(c, modifiers); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 2031 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 2032 | } |