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