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); |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame^] | 716 | auto paintFlag = [this, &paintTitle](SkPaint::Flags flag, bool (SkPaint::* isFlag)() const, |
| 717 | const char* on, const char* off) |
| 718 | { |
| 719 | if (fPaintOverrides.fFlags & flag) { |
| 720 | paintTitle.append((fPaint.*isFlag)() ? on : off); |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 721 | } |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame^] | 722 | }; |
| 723 | |
| 724 | paintFlag(SkPaint::kAntiAlias_Flag, &SkPaint::isAntiAlias, "Antialias", "Alias"); |
| 725 | paintFlag(SkPaint::kDither_Flag, &SkPaint::isDither, "DITHER", "No Dither"); |
| 726 | paintFlag(SkPaint::kFakeBoldText_Flag, &SkPaint::isFakeBoldText, "Fake Bold", "No Fake Bold"); |
| 727 | paintFlag(SkPaint::kLinearText_Flag, &SkPaint::isLinearText, "Linear Text", "Non-Linear Text"); |
| 728 | paintFlag(SkPaint::kSubpixelText_Flag, &SkPaint::isSubpixelText, "Subpixel Text", "Pixel Text"); |
| 729 | paintFlag(SkPaint::kLCDRenderText_Flag, &SkPaint::isLCDRenderText, "LCD", "lcd"); |
| 730 | paintFlag(SkPaint::kEmbeddedBitmapText_Flag, &SkPaint::isEmbeddedBitmapText, |
| 731 | "Bitmap Text", "No Bitmap Text"); |
| 732 | paintFlag(SkPaint::kAutoHinting_Flag, &SkPaint::isAutohinted, |
| 733 | "Force Autohint", "No Force Autohint"); |
| 734 | paintFlag(SkPaint::kVerticalText_Flag, &SkPaint::isVerticalText, |
| 735 | "Vertical Text", "No Vertical Text"); |
| 736 | |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 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 | } |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame^] | 1024 | |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 1025 | if (fPaintOverrides->fFlags & SkPaint::kAntiAlias_Flag) { |
| 1026 | paint->writable()->setAntiAlias(fPaint->isAntiAlias()); |
| 1027 | } |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame^] | 1028 | if (fPaintOverrides->fFlags & SkPaint::kDither_Flag) { |
| 1029 | paint->writable()->setDither(fPaint->isDither()); |
| 1030 | } |
| 1031 | if (fPaintOverrides->fFlags & SkPaint::kFakeBoldText_Flag) { |
| 1032 | paint->writable()->setFakeBoldText(fPaint->isFakeBoldText()); |
| 1033 | } |
| 1034 | if (fPaintOverrides->fFlags & SkPaint::kLinearText_Flag) { |
| 1035 | paint->writable()->setLinearText(fPaint->isLinearText()); |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 1036 | } |
| 1037 | if (fPaintOverrides->fFlags & SkPaint::kSubpixelText_Flag) { |
| 1038 | paint->writable()->setSubpixelText(fPaint->isSubpixelText()); |
| 1039 | } |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame^] | 1040 | if (fPaintOverrides->fFlags & SkPaint::kLCDRenderText_Flag) { |
| 1041 | paint->writable()->setLCDRenderText(fPaint->isLCDRenderText()); |
| 1042 | } |
| 1043 | if (fPaintOverrides->fFlags & SkPaint::kEmbeddedBitmapText_Flag) { |
| 1044 | paint->writable()->setEmbeddedBitmapText(fPaint->isEmbeddedBitmapText()); |
| 1045 | } |
| 1046 | if (fPaintOverrides->fFlags & SkPaint::kAutoHinting_Flag) { |
| 1047 | paint->writable()->setAutohinted(fPaint->isAutohinted()); |
| 1048 | } |
| 1049 | if (fPaintOverrides->fFlags & SkPaint::kVerticalText_Flag) { |
| 1050 | paint->writable()->setVerticalText(fPaint->isVerticalText()); |
| 1051 | } |
| 1052 | |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 1053 | return true; |
| 1054 | } |
| 1055 | SkPaint* fPaint; |
| 1056 | Viewer::SkPaintFields* fPaintOverrides; |
| 1057 | }; |
| 1058 | |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1059 | void Viewer::drawSlide(SkCanvas* canvas) { |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 1060 | SkAutoCanvasRestore autorestore(canvas, false); |
| 1061 | |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1062 | // By default, we render directly into the window's surface/canvas |
| 1063 | SkCanvas* slideCanvas = canvas; |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1064 | fLastImage.reset(); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 1065 | |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1066 | // If we're in any of the color managed modes, construct the color space we're going to use |
| 1067 | sk_sp<SkColorSpace> cs = nullptr; |
| 1068 | if (ColorMode::kLegacy != fColorMode) { |
| 1069 | auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode) |
| 1070 | ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma; |
Mike Klein | c722f79 | 2017-07-31 11:57:21 -0400 | [diff] [blame] | 1071 | SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor); |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1072 | SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ)); |
Brian Osman | fdab576 | 2017-11-09 10:27:55 -0500 | [diff] [blame] | 1073 | if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) { |
| 1074 | cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ); |
| 1075 | } else { |
| 1076 | cs = SkColorSpace::MakeRGB(transferFn, toXYZ); |
| 1077 | } |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1078 | } |
| 1079 | |
Brian Osman | 3ac99cf | 2017-12-01 11:23:53 -0500 | [diff] [blame] | 1080 | if (fSaveToSKP) { |
| 1081 | SkPictureRecorder recorder; |
| 1082 | SkCanvas* recorderCanvas = recorder.beginRecording( |
| 1083 | SkRect::Make(fSlides[fCurrentSlide]->getDimensions())); |
| 1084 | // In xform-canvas mode, record the transformed output |
| 1085 | std::unique_ptr<SkCanvas> xformCanvas = nullptr; |
| 1086 | if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) { |
| 1087 | xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs); |
| 1088 | recorderCanvas = xformCanvas.get(); |
| 1089 | } |
| 1090 | fSlides[fCurrentSlide]->draw(recorderCanvas); |
| 1091 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
| 1092 | SkFILEWStream stream("sample_app.skp"); |
| 1093 | picture->serialize(&stream); |
| 1094 | fSaveToSKP = false; |
| 1095 | } |
| 1096 | |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1097 | // 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] | 1098 | // 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] | 1099 | // 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] | 1100 | sk_sp<SkSurface> offscreenSurface = nullptr; |
Yuqian Li | b2ba664 | 2017-11-22 12:07:41 -0500 | [diff] [blame] | 1101 | std::unique_ptr<SkCanvas> threadedCanvas; |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 1102 | if (Window::kRaster_BackendType == fBackendType || |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 1103 | kPerspective_Fake == fPerspectiveMode || |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 1104 | ColorMode::kColorManagedLinearF16 == fColorMode || |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1105 | fShowZoomWindow || |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1106 | (ColorMode::kColorManagedSRGB8888 == fColorMode && |
| 1107 | !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) { |
| 1108 | |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1109 | SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode) |
| 1110 | ? kRGBA_F16_SkColorType : kN32_SkColorType; |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1111 | // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it |
| 1112 | // with a special canvas (below) that has the color space attached |
| 1113 | sk_sp<SkColorSpace> offscreenColorSpace = |
| 1114 | (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs; |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1115 | SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType, |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1116 | kPremul_SkAlphaType, std::move(offscreenColorSpace)); |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 1117 | SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |
| 1118 | canvas->getProps(&props); |
| 1119 | offscreenSurface = Window::kRaster_BackendType == fBackendType |
| 1120 | ? SkSurface::MakeRaster(info, &props) |
| 1121 | : canvas->makeSurface(info); |
Yuqian Li | b2ba664 | 2017-11-22 12:07:41 -0500 | [diff] [blame] | 1122 | SkPixmap offscreenPixmap; |
| 1123 | if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) { |
| 1124 | SkBitmap offscreenBitmap; |
| 1125 | offscreenBitmap.installPixels(offscreenPixmap); |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 1126 | threadedCanvas = |
| 1127 | skstd::make_unique<SkCanvas>( |
| 1128 | sk_make_sp<SkThreadedBMPDevice>( |
| 1129 | offscreenBitmap, fTileCnt, fThreadCnt, fExecutor.get())); |
Yuqian Li | b2ba664 | 2017-11-22 12:07:41 -0500 | [diff] [blame] | 1130 | slideCanvas = threadedCanvas.get(); |
| 1131 | } else { |
| 1132 | slideCanvas = offscreenSurface->getCanvas(); |
| 1133 | } |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1134 | } |
| 1135 | |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1136 | std::unique_ptr<SkCanvas> xformCanvas = nullptr; |
| 1137 | if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) { |
| 1138 | xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs); |
| 1139 | slideCanvas = xformCanvas.get(); |
| 1140 | } |
| 1141 | |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 1142 | int count = slideCanvas->save(); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1143 | slideCanvas->clear(SK_ColorWHITE); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1144 | slideCanvas->concat(computeMatrix()); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1145 | // Time the painting logic of the slide |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1146 | fStatsLayer.beginTiming(fPaintTimer); |
Ben Wagner | abdcc5f | 2018-02-12 16:37:28 -0500 | [diff] [blame] | 1147 | OveridePaintFilterCanvas filterCanvas(slideCanvas, &fPaint, &fPaintOverrides); |
| 1148 | fSlides[fCurrentSlide]->draw(&filterCanvas); |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1149 | fStatsLayer.endTiming(fPaintTimer); |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 1150 | slideCanvas->restoreToCount(count); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1151 | |
| 1152 | // Force a flush so we can time that, too |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1153 | fStatsLayer.beginTiming(fFlushTimer); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1154 | slideCanvas->flush(); |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1155 | fStatsLayer.endTiming(fFlushTimer); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1156 | |
| 1157 | // If we rendered offscreen, snap an image and push the results to the window's canvas |
| 1158 | if (offscreenSurface) { |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1159 | fLastImage = offscreenSurface->makeImageSnapshot(); |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1160 | |
| 1161 | // 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] | 1162 | sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode) |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 1163 | ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB(); |
Brian Osman | e0d4fba | 2017-03-15 10:24:55 -0400 | [diff] [blame] | 1164 | auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get()); |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 1165 | SkPaint paint; |
| 1166 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 1167 | int prePerspectiveCount = canvas->save(); |
| 1168 | if (kPerspective_Fake == fPerspectiveMode) { |
| 1169 | paint.setFilterQuality(kHigh_SkFilterQuality); |
| 1170 | canvas->clear(SK_ColorWHITE); |
| 1171 | canvas->concat(this->computePerspectiveMatrix()); |
| 1172 | } |
Brian Salomon | bf52e3d | 2017-02-22 15:21:11 -0500 | [diff] [blame] | 1173 | canvas->drawImage(retaggedImage, 0, 0, &paint); |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 1174 | canvas->restoreToCount(prePerspectiveCount); |
liyuqian | 74959a1 | 2016-06-16 14:10:34 -0700 | [diff] [blame] | 1175 | } |
liyuqian | 6f163d2 | 2016-06-13 12:26:45 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 1178 | void Viewer::onBackendCreated() { |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 1179 | this->setupCurrentSlide(); |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 1180 | fWindow->show(); |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 1181 | } |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 1182 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 1183 | void Viewer::onPaint(SkCanvas* canvas) { |
Jim Van Verth | 90dcce5 | 2017-11-03 13:36:07 -0400 | [diff] [blame] | 1184 | this->drawSlide(canvas); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 1185 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 1186 | fCommands.drawHelp(canvas); |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 1187 | |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 1188 | this->drawImGui(); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
Florin Malita | cefc1b9 | 2018-02-19 21:43:47 -0500 | [diff] [blame] | 1191 | SkPoint Viewer::mapEvent(float x, float y) { |
| 1192 | const auto m = this->computeMatrix(); |
| 1193 | SkMatrix inv; |
| 1194 | |
| 1195 | SkAssertResult(m.invert(&inv)); |
| 1196 | |
| 1197 | return inv.mapXY(x, y); |
| 1198 | } |
| 1199 | |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 1200 | 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] | 1201 | if (GestureDevice::kMouse == fGestureDevice) { |
| 1202 | return false; |
| 1203 | } |
Florin Malita | cefc1b9 | 2018-02-19 21:43:47 -0500 | [diff] [blame] | 1204 | |
| 1205 | const auto slidePt = this->mapEvent(x, y); |
| 1206 | if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, 0)) { |
| 1207 | fWindow->inval(); |
| 1208 | return true; |
| 1209 | } |
| 1210 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 1211 | void* castedOwner = reinterpret_cast<void*>(owner); |
| 1212 | switch (state) { |
| 1213 | case Window::kUp_InputState: { |
| 1214 | fGesture.touchEnd(castedOwner); |
| 1215 | break; |
| 1216 | } |
| 1217 | case Window::kDown_InputState: { |
Brian Osman | 42bb6ac | 2017-06-05 08:46:04 -0400 | [diff] [blame] | 1218 | fGesture.touchBegin(castedOwner, x, y); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 1219 | break; |
| 1220 | } |
| 1221 | case Window::kMove_InputState: { |
Brian Osman | 42bb6ac | 2017-06-05 08:46:04 -0400 | [diff] [blame] | 1222 | fGesture.touchMoved(castedOwner, x, y); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 1223 | break; |
| 1224 | } |
| 1225 | } |
Brian Osman | b53f48c | 2017-06-07 10:00:30 -0400 | [diff] [blame] | 1226 | fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone; |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 1227 | fWindow->inval(); |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 1231 | 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] | 1232 | if (GestureDevice::kTouch == fGestureDevice) { |
| 1233 | return false; |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 1234 | } |
Brian Osman | 16c81a1 | 2017-12-20 11:58:34 -0500 | [diff] [blame] | 1235 | |
Florin Malita | cefc1b9 | 2018-02-19 21:43:47 -0500 | [diff] [blame] | 1236 | const auto slidePt = this->mapEvent(x, y); |
| 1237 | if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) { |
| 1238 | fWindow->inval(); |
| 1239 | return true; |
Brian Osman | 16c81a1 | 2017-12-20 11:58:34 -0500 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | switch (state) { |
| 1243 | case Window::kUp_InputState: { |
| 1244 | fGesture.touchEnd(nullptr); |
| 1245 | break; |
| 1246 | } |
| 1247 | case Window::kDown_InputState: { |
| 1248 | fGesture.touchBegin(nullptr, x, y); |
| 1249 | break; |
| 1250 | } |
| 1251 | case Window::kMove_InputState: { |
| 1252 | fGesture.touchMoved(nullptr, x, y); |
| 1253 | break; |
| 1254 | } |
| 1255 | } |
| 1256 | fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone; |
| 1257 | |
| 1258 | if (state != Window::kMove_InputState || fGesture.isBeingTouched()) { |
| 1259 | fWindow->inval(); |
| 1260 | } |
Jim Van Verth | e770578 | 2017-05-04 14:00:59 -0400 | [diff] [blame] | 1261 | return true; |
| 1262 | } |
| 1263 | |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1264 | static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y, |
| 1265 | const ImVec2& pos, const ImVec2& size) { |
| 1266 | // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip) |
| 1267 | ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y); |
| 1268 | |
| 1269 | // Invisible 10x10 button |
| 1270 | ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5)); |
| 1271 | ImGui::InvisibleButton(label, ImVec2(10, 10)); |
| 1272 | |
| 1273 | if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) { |
| 1274 | ImGuiIO& io = ImGui::GetIO(); |
| 1275 | // Normalized mouse position, relative to our gamut box |
| 1276 | ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y); |
| 1277 | // Clamp to edge of box, convert back to primary scale |
| 1278 | *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f; |
| 1279 | *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f; |
| 1280 | } |
| 1281 | |
| 1282 | if (ImGui::IsItemHovered()) { |
| 1283 | ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y); |
| 1284 | } |
| 1285 | |
| 1286 | // Return screen coordinates for the caller. We could just return center here, but we'd have |
| 1287 | // one frame of lag during drag. |
| 1288 | return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y); |
| 1289 | } |
| 1290 | |
| 1291 | static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) { |
| 1292 | ImDrawList* drawList = ImGui::GetWindowDrawList(); |
| 1293 | |
| 1294 | // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available |
| 1295 | // width, and scale the height to maintain aspect ratio. |
| 1296 | float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f); |
| 1297 | ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f)); |
| 1298 | ImVec2 pos = ImGui::GetCursorScreenPos(); |
| 1299 | |
| 1300 | // Background image. Only draw a subset of the image, to avoid the regions less than zero. |
| 1301 | // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area. |
| 1302 | // Magic numbers are pixel locations of the origin and upper-right corner. |
| 1303 | drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y), |
| 1304 | ImVec2(242, 61), ImVec2(1897, 1922)); |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1305 | |
| 1306 | // Primary markers |
| 1307 | ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size); |
| 1308 | ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size); |
| 1309 | ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size); |
| 1310 | ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size); |
| 1311 | |
| 1312 | // Gamut triangle |
| 1313 | drawList->AddCircle(r, 5.0f, 0xFF000040); |
| 1314 | drawList->AddCircle(g, 5.0f, 0xFF004000); |
| 1315 | drawList->AddCircle(b, 5.0f, 0xFF400000); |
| 1316 | drawList->AddCircle(w, 5.0f, 0xFFFFFFFF); |
| 1317 | drawList->AddTriangle(r, g, b, 0xFFFFFFFF); |
| 1318 | |
| 1319 | // Re-position cursor immediate after the diagram for subsequent controls |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 1320 | ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y)); |
| 1321 | } |
| 1322 | |
| 1323 | static ImVec2 ImGui_DragPoint(const char* label, SkPoint* p, |
| 1324 | const ImVec2& pos, const ImVec2& size, bool* dragging) { |
| 1325 | // Transform points ([0, 0] - [1.0, 1.0]) to screen coords |
| 1326 | ImVec2 center(pos.x + p->fX * size.x, pos.y + p->fY * size.y); |
| 1327 | |
| 1328 | // Invisible 10x10 button |
| 1329 | ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5)); |
| 1330 | ImGui::InvisibleButton(label, ImVec2(10, 10)); |
| 1331 | |
| 1332 | if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) { |
| 1333 | ImGuiIO& io = ImGui::GetIO(); |
| 1334 | // Normalized mouse position, relative to our gamut box |
| 1335 | ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y); |
| 1336 | // Clamp to edge of box |
| 1337 | p->fX = SkTPin(mousePosXY.x, 0.0f, 1.0f); |
| 1338 | p->fY = SkTPin(mousePosXY.y, 0.0f, 1.0f); |
| 1339 | *dragging = true; |
| 1340 | } |
| 1341 | |
| 1342 | // Return screen coordinates for the caller. We could just return center here, but we'd have |
| 1343 | // one frame of lag during drag. |
| 1344 | return ImVec2(pos.x + p->fX * size.x, pos.y + p->fY * size.y); |
| 1345 | } |
| 1346 | |
| 1347 | static bool ImGui_DragQuad(SkPoint* pts) { |
| 1348 | ImDrawList* drawList = ImGui::GetWindowDrawList(); |
| 1349 | |
| 1350 | // Fit our image/canvas to the available width, and scale the height to maintain aspect ratio. |
| 1351 | float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f); |
| 1352 | ImVec2 size = ImVec2(canvasWidth, canvasWidth); |
| 1353 | ImVec2 pos = ImGui::GetCursorScreenPos(); |
| 1354 | |
| 1355 | // Background rectangle |
| 1356 | drawList->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(0, 0, 0, 128)); |
| 1357 | |
| 1358 | // Corner markers |
| 1359 | bool dragging = false; |
| 1360 | ImVec2 tl = ImGui_DragPoint("TL", pts + 0, pos, size, &dragging); |
| 1361 | ImVec2 tr = ImGui_DragPoint("TR", pts + 1, pos, size, &dragging); |
| 1362 | ImVec2 bl = ImGui_DragPoint("BL", pts + 2, pos, size, &dragging); |
| 1363 | ImVec2 br = ImGui_DragPoint("BR", pts + 3, pos, size, &dragging); |
| 1364 | |
| 1365 | // Draw markers and quad |
| 1366 | drawList->AddCircle(tl, 5.0f, 0xFFFFFFFF); |
| 1367 | drawList->AddCircle(tr, 5.0f, 0xFFFFFFFF); |
| 1368 | drawList->AddCircle(bl, 5.0f, 0xFFFFFFFF); |
| 1369 | drawList->AddCircle(br, 5.0f, 0xFFFFFFFF); |
| 1370 | drawList->AddLine(tl, tr, 0xFFFFFFFF); |
| 1371 | drawList->AddLine(tr, br, 0xFFFFFFFF); |
| 1372 | drawList->AddLine(br, bl, 0xFFFFFFFF); |
| 1373 | drawList->AddLine(bl, tl, 0xFFFFFFFF); |
| 1374 | |
| 1375 | ImGui::SetCursorScreenPos(ImVec2(pos.x, pos.y + size.y)); |
| 1376 | ImGui::Spacing(); |
| 1377 | |
| 1378 | return dragging; |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1379 | } |
| 1380 | |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 1381 | void Viewer::drawImGui() { |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1382 | // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible |
| 1383 | if (fShowImGuiTestWindow) { |
| 1384 | ImGui::ShowTestWindow(&fShowImGuiTestWindow); |
| 1385 | } |
| 1386 | |
| 1387 | if (fShowImGuiDebugWindow) { |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1388 | // We have some dynamic content that sizes to fill available size. If the scroll bar isn't |
| 1389 | // always visible, we can end up in a layout feedback loop. |
| 1390 | ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1391 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 1392 | bool paramsChanged = false; |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1393 | if (ImGui::Begin("Tools", &fShowImGuiDebugWindow, |
| 1394 | ImGuiWindowFlags_AlwaysVerticalScrollbar)) { |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 1395 | if (ImGui::CollapsingHeader("Backend")) { |
| 1396 | int newBackend = static_cast<int>(fBackendType); |
| 1397 | ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType); |
| 1398 | ImGui::SameLine(); |
| 1399 | ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType); |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 1400 | #if SK_ANGLE && defined(SK_BUILD_FOR_WIN) |
| 1401 | ImGui::SameLine(); |
| 1402 | ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType); |
| 1403 | #endif |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 1404 | #if defined(SK_VULKAN) |
| 1405 | ImGui::SameLine(); |
| 1406 | ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType); |
| 1407 | #endif |
| 1408 | if (newBackend != fBackendType) { |
| 1409 | fDeferredActions.push_back([=]() { |
| 1410 | this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend)); |
| 1411 | }); |
| 1412 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1413 | |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1414 | const GrContext* ctx = fWindow->getGrContext(); |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 1415 | bool* wire = ¶ms.fGrContextOptions.fWireframeMode; |
| 1416 | if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) { |
| 1417 | paramsChanged = true; |
| 1418 | } |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1419 | |
Brian Osman | 28b1252 | 2017-03-08 17:10:24 -0500 | [diff] [blame] | 1420 | if (ctx) { |
| 1421 | int sampleCount = fWindow->sampleCount(); |
| 1422 | ImGui::Text("MSAA: "); ImGui::SameLine(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1423 | ImGui::RadioButton("1", &sampleCount, 1); ImGui::SameLine(); |
Brian Osman | 28b1252 | 2017-03-08 17:10:24 -0500 | [diff] [blame] | 1424 | ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine(); |
| 1425 | ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine(); |
| 1426 | ImGui::RadioButton("16", &sampleCount, 16); |
| 1427 | |
| 1428 | if (sampleCount != params.fMSAASampleCount) { |
| 1429 | params.fMSAASampleCount = sampleCount; |
| 1430 | paramsChanged = true; |
| 1431 | } |
| 1432 | } |
| 1433 | |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 1434 | int pixelGeometryIdx = 0; |
| 1435 | if (fPixelGeometryOverrides) { |
| 1436 | pixelGeometryIdx = params.fSurfaceProps.pixelGeometry() + 1; |
| 1437 | } |
| 1438 | if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx, |
| 1439 | "Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0")) |
| 1440 | { |
| 1441 | uint32_t flags = params.fSurfaceProps.flags(); |
| 1442 | if (pixelGeometryIdx == 0) { |
| 1443 | fPixelGeometryOverrides = false; |
| 1444 | params.fSurfaceProps = SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 1445 | } else { |
| 1446 | fPixelGeometryOverrides = true; |
| 1447 | SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1); |
| 1448 | params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry); |
| 1449 | } |
| 1450 | paramsChanged = true; |
| 1451 | } |
| 1452 | |
| 1453 | bool useDFT = params.fSurfaceProps.isUseDeviceIndependentFonts(); |
| 1454 | if (ImGui::Checkbox("DFT", &useDFT)) { |
| 1455 | uint32_t flags = params.fSurfaceProps.flags(); |
| 1456 | if (useDFT) { |
| 1457 | flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag; |
| 1458 | } else { |
| 1459 | flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag; |
| 1460 | } |
| 1461 | SkPixelGeometry pixelGeometry = params.fSurfaceProps.pixelGeometry(); |
| 1462 | params.fSurfaceProps = SkSurfaceProps(flags, pixelGeometry); |
| 1463 | paramsChanged = true; |
| 1464 | } |
| 1465 | |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1466 | if (ImGui::TreeNode("Path Renderers")) { |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1467 | GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers; |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1468 | auto prButton = [&](GpuPathRenderers x) { |
| 1469 | if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) { |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1470 | if (x != params.fGrContextOptions.fGpuPathRenderers) { |
| 1471 | params.fGrContextOptions.fGpuPathRenderers = x; |
| 1472 | paramsChanged = true; |
| 1473 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1474 | } |
| 1475 | }; |
| 1476 | |
| 1477 | if (!ctx) { |
| 1478 | ImGui::RadioButton("Software", true); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1479 | } else if (fWindow->sampleCount() > 1) { |
Brian Osman | 8b0f265 | 2017-08-29 15:18:34 -0400 | [diff] [blame] | 1480 | prButton(GpuPathRenderers::kDefault); |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1481 | prButton(GpuPathRenderers::kAll); |
| 1482 | if (ctx->caps()->shaderCaps()->pathRenderingSupport()) { |
| 1483 | prButton(GpuPathRenderers::kStencilAndCover); |
| 1484 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1485 | prButton(GpuPathRenderers::kTessellating); |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1486 | prButton(GpuPathRenderers::kNone); |
| 1487 | } else { |
Brian Osman | 8b0f265 | 2017-08-29 15:18:34 -0400 | [diff] [blame] | 1488 | prButton(GpuPathRenderers::kDefault); |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1489 | prButton(GpuPathRenderers::kAll); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1490 | if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) { |
| 1491 | prButton(GpuPathRenderers::kCoverageCounting); |
| 1492 | } |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 1493 | prButton(GpuPathRenderers::kSmall); |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1494 | prButton(GpuPathRenderers::kTessellating); |
| 1495 | prButton(GpuPathRenderers::kNone); |
| 1496 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1497 | ImGui::TreePop(); |
| 1498 | } |
Brian Osman | 621491e | 2017-02-28 15:45:01 -0500 | [diff] [blame] | 1499 | } |
| 1500 | |
Ben Wagner | d02a74d | 2018-04-23 12:55:06 -0400 | [diff] [blame] | 1501 | if (ImGui::CollapsingHeader("Transform")) { |
| 1502 | float zoom = fZoomLevel; |
| 1503 | if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) { |
| 1504 | fZoomLevel = zoom; |
| 1505 | this->preTouchMatrixChanged(); |
| 1506 | paramsChanged = true; |
| 1507 | } |
| 1508 | float deg = fRotation; |
Ben Wagner | cb13935 | 2018-05-04 10:33:04 -0400 | [diff] [blame] | 1509 | if (ImGui::SliderFloat("Rotate", °, -30, 360, "%.3f deg")) { |
Ben Wagner | d02a74d | 2018-04-23 12:55:06 -0400 | [diff] [blame] | 1510 | fRotation = deg; |
| 1511 | this->preTouchMatrixChanged(); |
| 1512 | paramsChanged = true; |
| 1513 | } |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 1514 | int perspectiveMode = static_cast<int>(fPerspectiveMode); |
| 1515 | if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) { |
| 1516 | fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode); |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 1517 | this->preTouchMatrixChanged(); |
Brian Osman | 805a727 | 2018-05-02 15:40:20 -0400 | [diff] [blame] | 1518 | this->updateTitle(); |
Brian Osman | 9bb47cf | 2018-04-26 15:55:00 -0400 | [diff] [blame] | 1519 | } |
| 1520 | if (ImGui_DragQuad(fPerspectivePoints)) { |
| 1521 | this->preTouchMatrixChanged(); |
| 1522 | } |
Ben Wagner | d02a74d | 2018-04-23 12:55:06 -0400 | [diff] [blame] | 1523 | } |
| 1524 | |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 1525 | if (ImGui::CollapsingHeader("Paint")) { |
| 1526 | int hintingIdx = 0; |
| 1527 | if (fPaintOverrides.fHinting) { |
| 1528 | hintingIdx = fPaint.getHinting() + 1; |
| 1529 | } |
| 1530 | if (ImGui::Combo("Hinting", &hintingIdx, |
| 1531 | "Default\0None\0Slight\0Normal\0Full\0\0")) |
| 1532 | { |
| 1533 | if (hintingIdx == 0) { |
| 1534 | fPaintOverrides.fHinting = false; |
| 1535 | fPaint.setHinting(SkPaint::kNo_Hinting); |
| 1536 | } else { |
| 1537 | fPaintOverrides.fHinting = true; |
| 1538 | SkPaint::Hinting hinting = SkTo<SkPaint::Hinting>(hintingIdx - 1); |
| 1539 | fPaint.setHinting(hinting); |
| 1540 | } |
| 1541 | paramsChanged = true; |
| 1542 | } |
| 1543 | |
| 1544 | int aliasIdx = 0; |
| 1545 | if (fPaintOverrides.fFlags & SkPaint::kAntiAlias_Flag) { |
| 1546 | aliasIdx = SkTo<int>(fPaintOverrides.fAntiAlias) + 1; |
| 1547 | } |
| 1548 | if (ImGui::Combo("Anti-Alias", &aliasIdx, |
| 1549 | "Default\0Alias\0Normal\0AnalyticAAEnabled\0AnalyticAAForced\0" |
| 1550 | "DeltaAAEnabled\0DeltaAAForced\0\0")) |
| 1551 | { |
| 1552 | gSkUseAnalyticAA = fPaintOverrides.fOriginalSkUseAnalyticAA; |
| 1553 | gSkForceAnalyticAA = fPaintOverrides.fOriginalSkForceAnalyticAA; |
| 1554 | gSkUseDeltaAA = fPaintOverrides.fOriginalSkUseDeltaAA; |
| 1555 | gSkForceDeltaAA = fPaintOverrides.fOriginalSkForceDeltaAA; |
| 1556 | if (aliasIdx == 0) { |
| 1557 | fPaintOverrides.fAntiAlias = SkPaintFields::AntiAliasState::Alias; |
| 1558 | fPaintOverrides.fFlags &= ~SkPaint::kAntiAlias_Flag; |
| 1559 | } else { |
| 1560 | fPaintOverrides.fFlags |= SkPaint::kAntiAlias_Flag; |
| 1561 | fPaintOverrides.fAntiAlias =SkTo<SkPaintFields::AntiAliasState>(aliasIdx-1); |
| 1562 | fPaint.setAntiAlias(aliasIdx > 1); |
| 1563 | switch (fPaintOverrides.fAntiAlias) { |
| 1564 | case SkPaintFields::AntiAliasState::Alias: |
| 1565 | break; |
| 1566 | case SkPaintFields::AntiAliasState::Normal: |
| 1567 | break; |
| 1568 | case SkPaintFields::AntiAliasState::AnalyticAAEnabled: |
| 1569 | gSkUseAnalyticAA = true; |
| 1570 | gSkForceAnalyticAA = false; |
| 1571 | gSkUseDeltaAA = gSkForceDeltaAA = false; |
| 1572 | break; |
| 1573 | case SkPaintFields::AntiAliasState::AnalyticAAForced: |
| 1574 | gSkUseAnalyticAA = gSkForceAnalyticAA = true; |
| 1575 | gSkUseDeltaAA = gSkForceDeltaAA = false; |
| 1576 | break; |
| 1577 | case SkPaintFields::AntiAliasState::DeltaAAEnabled: |
| 1578 | gSkUseAnalyticAA = gSkForceAnalyticAA = false; |
| 1579 | gSkUseDeltaAA = true; |
| 1580 | gSkForceDeltaAA = false; |
| 1581 | break; |
| 1582 | case SkPaintFields::AntiAliasState::DeltaAAForced: |
| 1583 | gSkUseAnalyticAA = gSkForceAnalyticAA = false; |
| 1584 | gSkUseDeltaAA = gSkForceDeltaAA = true; |
| 1585 | break; |
| 1586 | } |
| 1587 | } |
| 1588 | paramsChanged = true; |
| 1589 | } |
| 1590 | |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame^] | 1591 | auto paintFlag = [this, ¶msChanged](const char* label, const char* items, |
| 1592 | SkPaint::Flags flag, |
| 1593 | bool (SkPaint::* isFlag)() const, |
| 1594 | void (SkPaint::* setFlag)(bool) ) |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 1595 | { |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame^] | 1596 | int itemIndex = 0; |
| 1597 | if (fPaintOverrides.fFlags & flag) { |
| 1598 | itemIndex = (fPaint.*isFlag)() ? 2 : 1; |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 1599 | } |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame^] | 1600 | if (ImGui::Combo(label, &itemIndex, items)) { |
| 1601 | if (itemIndex == 0) { |
| 1602 | fPaintOverrides.fFlags &= ~flag; |
| 1603 | } else { |
| 1604 | fPaintOverrides.fFlags |= flag; |
| 1605 | (fPaint.*setFlag)(itemIndex == 2); |
| 1606 | } |
| 1607 | paramsChanged = true; |
| 1608 | } |
| 1609 | }; |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 1610 | |
Ben Wagner | 99a78dc | 2018-05-09 18:23:51 -0400 | [diff] [blame^] | 1611 | paintFlag("Dither", |
| 1612 | "Default\0No Dither\0Dither\0\0", |
| 1613 | SkPaint::kDither_Flag, |
| 1614 | &SkPaint::isDither, &SkPaint::setDither); |
| 1615 | |
| 1616 | paintFlag("Fake Bold Glyphs", |
| 1617 | "Default\0No Fake Bold\0Fake Bold\0\0", |
| 1618 | SkPaint::kFakeBoldText_Flag, |
| 1619 | &SkPaint::isFakeBoldText, &SkPaint::setFakeBoldText); |
| 1620 | |
| 1621 | paintFlag("Linear Text", |
| 1622 | "Default\0No Linear Text\0Linear Text\0\0", |
| 1623 | SkPaint::kLinearText_Flag, |
| 1624 | &SkPaint::isLinearText, &SkPaint::setLinearText); |
| 1625 | |
| 1626 | paintFlag("Subpixel Position Glyphs", |
| 1627 | "Default\0Pixel Text\0Subpixel Text\0\0", |
| 1628 | SkPaint::kSubpixelText_Flag, |
| 1629 | &SkPaint::isSubpixelText, &SkPaint::setSubpixelText); |
| 1630 | |
| 1631 | paintFlag("Subpixel Anti-Alias", |
| 1632 | "Default\0lcd\0LCD\0\0", |
| 1633 | SkPaint::kLCDRenderText_Flag, |
| 1634 | &SkPaint::isLCDRenderText, &SkPaint::setLCDRenderText); |
| 1635 | |
| 1636 | paintFlag("Embedded Bitmap Text", |
| 1637 | "Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0", |
| 1638 | SkPaint::kEmbeddedBitmapText_Flag, |
| 1639 | &SkPaint::isEmbeddedBitmapText, &SkPaint::setEmbeddedBitmapText); |
| 1640 | |
| 1641 | paintFlag("Force Auto-Hinting", |
| 1642 | "Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0", |
| 1643 | SkPaint::kAutoHinting_Flag, |
| 1644 | &SkPaint::isAutohinted, &SkPaint::setAutohinted); |
| 1645 | |
| 1646 | paintFlag("Vertical Text", |
| 1647 | "Default\0No Vertical Text\0Vertical Text\0\0", |
| 1648 | SkPaint::kVerticalText_Flag, |
| 1649 | &SkPaint::isVerticalText, &SkPaint::setVerticalText); |
Ben Wagner | a580fb3 | 2018-04-17 11:16:32 -0400 | [diff] [blame] | 1650 | } |
| 1651 | |
Ben Wagner | 7a3c674 | 2018-04-23 10:01:07 -0400 | [diff] [blame] | 1652 | if (fShowSlidePicker) { |
| 1653 | ImGui::SetNextTreeNodeOpen(true); |
| 1654 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1655 | if (ImGui::CollapsingHeader("Slide")) { |
| 1656 | static ImGuiTextFilter filter; |
Brian Osman | f479e42 | 2017-11-08 13:11:36 -0500 | [diff] [blame] | 1657 | static ImVector<const char*> filteredSlideNames; |
| 1658 | static ImVector<int> filteredSlideIndices; |
| 1659 | |
Brian Osman | fce09c5 | 2017-11-14 15:32:20 -0500 | [diff] [blame] | 1660 | if (fShowSlidePicker) { |
| 1661 | ImGui::SetKeyboardFocusHere(); |
| 1662 | fShowSlidePicker = false; |
| 1663 | } |
| 1664 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1665 | filter.Draw(); |
Brian Osman | f479e42 | 2017-11-08 13:11:36 -0500 | [diff] [blame] | 1666 | filteredSlideNames.clear(); |
| 1667 | filteredSlideIndices.clear(); |
| 1668 | int filteredIndex = 0; |
| 1669 | for (int i = 0; i < fSlides.count(); ++i) { |
| 1670 | const char* slideName = fSlides[i]->getName().c_str(); |
| 1671 | if (filter.PassFilter(slideName) || i == fCurrentSlide) { |
| 1672 | if (i == fCurrentSlide) { |
| 1673 | filteredIndex = filteredSlideIndices.size(); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1674 | } |
Brian Osman | f479e42 | 2017-11-08 13:11:36 -0500 | [diff] [blame] | 1675 | filteredSlideNames.push_back(slideName); |
| 1676 | filteredSlideIndices.push_back(i); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1677 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1678 | } |
Brian Osman | f479e42 | 2017-11-08 13:11:36 -0500 | [diff] [blame] | 1679 | |
Brian Osman | f479e42 | 2017-11-08 13:11:36 -0500 | [diff] [blame] | 1680 | if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(), |
| 1681 | filteredSlideNames.size(), 20)) { |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 1682 | this->setCurrentSlide(filteredSlideIndices[filteredIndex]); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1683 | } |
| 1684 | } |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1685 | |
| 1686 | if (ImGui::CollapsingHeader("Color Mode")) { |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1687 | ColorMode newMode = fColorMode; |
| 1688 | auto cmButton = [&](ColorMode mode, const char* label) { |
| 1689 | if (ImGui::RadioButton(label, mode == fColorMode)) { |
| 1690 | newMode = mode; |
| 1691 | } |
| 1692 | }; |
| 1693 | |
| 1694 | cmButton(ColorMode::kLegacy, "Legacy 8888"); |
| 1695 | cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending, |
| 1696 | "Color Managed 8888 (Nonlinear blending)"); |
| 1697 | cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888"); |
| 1698 | cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16"); |
| 1699 | |
| 1700 | if (newMode != fColorMode) { |
Brian Osman | fd8f4d5 | 2017-02-24 11:57:23 -0500 | [diff] [blame] | 1701 | // It isn't safe to switch color mode now (in the middle of painting). We might |
| 1702 | // tear down the back-end, etc... Defer this change until the next onIdle. |
| 1703 | fDeferredActions.push_back([=]() { |
Brian Osman | 9200480 | 2017-03-06 11:47:26 -0500 | [diff] [blame] | 1704 | this->setColorMode(newMode); |
Brian Osman | fd8f4d5 | 2017-02-24 11:57:23 -0500 | [diff] [blame] | 1705 | }); |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | // Pick from common gamuts: |
| 1709 | int primariesIdx = 4; // Default: Custom |
| 1710 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) { |
| 1711 | if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) { |
| 1712 | primariesIdx = i; |
| 1713 | break; |
| 1714 | } |
| 1715 | } |
| 1716 | |
Brian Osman | fdab576 | 2017-11-09 10:27:55 -0500 | [diff] [blame] | 1717 | // When we're in xform canvas mode, we can alter the transfer function, too |
| 1718 | if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) { |
| 1719 | ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f); |
| 1720 | } |
| 1721 | |
Brian Osman | a109e39 | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 1722 | if (ImGui::Combo("Primaries", &primariesIdx, |
| 1723 | "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) { |
| 1724 | if (primariesIdx >= 0 && primariesIdx <= 3) { |
| 1725 | fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries; |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | // Allow direct editing of gamut |
| 1730 | ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint); |
| 1731 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1732 | } |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1733 | if (paramsChanged) { |
| 1734 | fDeferredActions.push_back([=]() { |
| 1735 | fWindow->setRequestedDisplayParams(params); |
| 1736 | fWindow->inval(); |
| 1737 | this->updateTitle(); |
| 1738 | }); |
| 1739 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1740 | ImGui::End(); |
| 1741 | } |
| 1742 | |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1743 | if (fShowZoomWindow && fLastImage) { |
| 1744 | if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) { |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1745 | static int zoomFactor = 8; |
| 1746 | if (ImGui::Button("<<")) { |
| 1747 | zoomFactor = SkTMax(zoomFactor / 2, 4); |
| 1748 | } |
| 1749 | ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine(); |
| 1750 | if (ImGui::Button(">>")) { |
| 1751 | zoomFactor = SkTMin(zoomFactor * 2, 32); |
| 1752 | } |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1753 | |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1754 | ImVec2 mousePos = ImGui::GetMousePos(); |
| 1755 | ImVec2 avail = ImGui::GetContentRegionAvail(); |
| 1756 | |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1757 | uint32_t pixel = 0; |
| 1758 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 1759 | if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) { |
| 1760 | ImGui::SameLine(); |
Brian Osman | 07b56b2 | 2017-11-21 14:59:31 -0500 | [diff] [blame] | 1761 | ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x", |
| 1762 | sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y), |
| 1763 | SkGetPackedR32(pixel), SkGetPackedG32(pixel), |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1764 | SkGetPackedB32(pixel), SkGetPackedA32(pixel)); |
| 1765 | } |
| 1766 | |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 1767 | fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) { |
Brian Osman | ead517d | 2017-11-13 15:36:36 -0500 | [diff] [blame] | 1768 | // Translate so the region of the image that's under the mouse cursor is centered |
| 1769 | // in the zoom canvas: |
| 1770 | c->scale(zoomFactor, zoomFactor); |
| 1771 | c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f, |
| 1772 | avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f); |
| 1773 | c->drawImage(this->fLastImage, 0, 0); |
| 1774 | |
| 1775 | SkPaint outline; |
| 1776 | outline.setStyle(SkPaint::kStroke_Style); |
| 1777 | c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline); |
| 1778 | }); |
Brian Osman | f687709 | 2017-02-13 09:39:57 -0500 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | ImGui::End(); |
| 1782 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1783 | } |
| 1784 | |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 1785 | void Viewer::onIdle() { |
Brian Osman | fd8f4d5 | 2017-02-24 11:57:23 -0500 | [diff] [blame] | 1786 | for (int i = 0; i < fDeferredActions.count(); ++i) { |
| 1787 | fDeferredActions[i](); |
| 1788 | } |
| 1789 | fDeferredActions.reset(); |
| 1790 | |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1791 | fStatsLayer.beginTiming(fAnimateTimer); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 1792 | fAnimTimer.updateTime(); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1793 | bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer); |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1794 | fStatsLayer.endTiming(fAnimateTimer); |
Brian Osman | 1df161a | 2017-02-09 12:10:20 -0500 | [diff] [blame] | 1795 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1796 | ImGuiIO& io = ImGui::GetIO(); |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 1797 | if (animateWantsInval || fStatsLayer.getActive() || fRefresh || io.MetricsActiveWindows) { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 1798 | fWindow->inval(); |
| 1799 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 1800 | } |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1801 | |
| 1802 | void Viewer::updateUIState() { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1803 | if (!fWindow) { |
| 1804 | return; |
| 1805 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1806 | if (fWindow->sampleCount() < 1) { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1807 | return; // Surface hasn't been created yet. |
| 1808 | } |
| 1809 | |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1810 | // Slide state |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1811 | Json::Value slideState(Json::objectValue); |
| 1812 | slideState[kName] = kSlideStateName; |
| 1813 | slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str(); |
liyuqian | 1f508fd | 2016-06-07 06:57:40 -0700 | [diff] [blame] | 1814 | if (fAllSlideNames.size() == 0) { |
| 1815 | for(auto slide : fSlides) { |
| 1816 | fAllSlideNames.append(Json::Value(slide->getName().c_str())); |
| 1817 | } |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1818 | } |
liyuqian | 1f508fd | 2016-06-07 06:57:40 -0700 | [diff] [blame] | 1819 | slideState[kOptions] = fAllSlideNames; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1820 | |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1821 | // Backend state |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1822 | Json::Value backendState(Json::objectValue); |
| 1823 | backendState[kName] = kBackendStateName; |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1824 | backendState[kValue] = kBackendTypeStrings[fBackendType]; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1825 | backendState[kOptions] = Json::Value(Json::arrayValue); |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1826 | for (auto str : kBackendTypeStrings) { |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1827 | backendState[kOptions].append(Json::Value(str)); |
| 1828 | } |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1829 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1830 | // MSAA state |
| 1831 | Json::Value msaaState(Json::objectValue); |
| 1832 | msaaState[kName] = kMSAAStateName; |
| 1833 | msaaState[kValue] = fWindow->sampleCount(); |
| 1834 | msaaState[kOptions] = Json::Value(Json::arrayValue); |
| 1835 | if (sk_app::Window::kRaster_BackendType == fBackendType) { |
| 1836 | msaaState[kOptions].append(Json::Value(0)); |
| 1837 | } else { |
| 1838 | for (int msaa : {0, 4, 8, 16}) { |
| 1839 | msaaState[kOptions].append(Json::Value(msaa)); |
| 1840 | } |
| 1841 | } |
| 1842 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1843 | // Path renderer state |
| 1844 | GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers; |
| 1845 | Json::Value prState(Json::objectValue); |
| 1846 | prState[kName] = kPathRendererStateName; |
| 1847 | prState[kValue] = gPathRendererNames[pr]; |
| 1848 | prState[kOptions] = Json::Value(Json::arrayValue); |
| 1849 | const GrContext* ctx = fWindow->getGrContext(); |
| 1850 | if (!ctx) { |
| 1851 | prState[kOptions].append("Software"); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1852 | } else if (fWindow->sampleCount() > 1) { |
Brian Osman | 8b0f265 | 2017-08-29 15:18:34 -0400 | [diff] [blame] | 1853 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1854 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]); |
| 1855 | if (ctx->caps()->shaderCaps()->pathRenderingSupport()) { |
| 1856 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]); |
| 1857 | } |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1858 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1859 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]); |
| 1860 | } else { |
Brian Osman | 8b0f265 | 2017-08-29 15:18:34 -0400 | [diff] [blame] | 1861 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1862 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1863 | if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) { |
| 1864 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]); |
| 1865 | } |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 1866 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]); |
Brian Osman | 8a9de3d | 2017-03-01 14:59:05 -0500 | [diff] [blame] | 1867 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1868 | prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]); |
| 1869 | } |
| 1870 | |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1871 | // Softkey state |
| 1872 | Json::Value softkeyState(Json::objectValue); |
| 1873 | softkeyState[kName] = kSoftkeyStateName; |
| 1874 | softkeyState[kValue] = kSoftkeyHint; |
| 1875 | softkeyState[kOptions] = Json::Value(Json::arrayValue); |
| 1876 | softkeyState[kOptions].append(kSoftkeyHint); |
| 1877 | for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) { |
| 1878 | softkeyState[kOptions].append(Json::Value(softkey.c_str())); |
| 1879 | } |
| 1880 | |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1881 | Json::Value state(Json::arrayValue); |
| 1882 | state.append(slideState); |
| 1883 | state.append(backendState); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1884 | state.append(msaaState); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1885 | state.append(prState); |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1886 | state.append(softkeyState); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1887 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 1888 | fWindow->setUIState(state.toStyledString().c_str()); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) { |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1892 | // For those who will add more features to handle the state change in this function: |
| 1893 | // After the change, please call updateUIState no notify the frontend (e.g., Android app). |
| 1894 | // For example, after slide change, updateUIState is called inside setupCurrentSlide; |
| 1895 | // after backend change, updateUIState is called in this function. |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1896 | if (stateName.equals(kSlideStateName)) { |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 1897 | for (int i = 0; i < fSlides.count(); ++i) { |
| 1898 | if (fSlides[i]->getName().equals(stateValue)) { |
| 1899 | this->setCurrentSlide(i); |
| 1900 | return; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1901 | } |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1902 | } |
Florin Malita | ab99c34 | 2018-01-16 16:23:03 -0500 | [diff] [blame] | 1903 | |
| 1904 | SkDebugf("Slide not found: %s", stateValue.c_str()); |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1905 | } else if (stateName.equals(kBackendStateName)) { |
| 1906 | for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) { |
| 1907 | if (stateValue.equals(kBackendTypeStrings[i])) { |
| 1908 | if (fBackendType != i) { |
| 1909 | fBackendType = (sk_app::Window::BackendType)i; |
| 1910 | fWindow->detach(); |
Brian Osman | 70d2f43 | 2017-11-08 09:54:10 -0500 | [diff] [blame] | 1911 | fWindow->attach(backend_type_for_window(fBackendType)); |
liyuqian | 6cb7025 | 2016-06-02 12:16:25 -0700 | [diff] [blame] | 1912 | } |
| 1913 | break; |
| 1914 | } |
| 1915 | } |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1916 | } else if (stateName.equals(kMSAAStateName)) { |
| 1917 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 1918 | int sampleCount = atoi(stateValue.c_str()); |
| 1919 | if (sampleCount != params.fMSAASampleCount) { |
| 1920 | params.fMSAASampleCount = sampleCount; |
| 1921 | fWindow->setRequestedDisplayParams(params); |
| 1922 | fWindow->inval(); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1923 | this->updateTitle(); |
| 1924 | this->updateUIState(); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1925 | } |
| 1926 | } else if (stateName.equals(kPathRendererStateName)) { |
| 1927 | DisplayParams params = fWindow->getRequestedDisplayParams(); |
| 1928 | for (const auto& pair : gPathRendererNames) { |
| 1929 | if (pair.second == stateValue.c_str()) { |
| 1930 | if (params.fGrContextOptions.fGpuPathRenderers != pair.first) { |
| 1931 | params.fGrContextOptions.fGpuPathRenderers = pair.first; |
| 1932 | fWindow->setRequestedDisplayParams(params); |
| 1933 | fWindow->inval(); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1934 | this->updateTitle(); |
| 1935 | this->updateUIState(); |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 1936 | } |
| 1937 | break; |
| 1938 | } |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 1939 | } |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1940 | } else if (stateName.equals(kSoftkeyStateName)) { |
| 1941 | if (!stateValue.equals(kSoftkeyHint)) { |
| 1942 | fCommands.onSoftkey(stateValue); |
Brian Salomon | 99a3390 | 2017-03-07 15:16:34 -0500 | [diff] [blame] | 1943 | this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint |
liyuqian | b73c24b | 2016-06-03 08:47:23 -0700 | [diff] [blame] | 1944 | } |
liyuqian | 2edb0f4 | 2016-07-06 14:11:32 -0700 | [diff] [blame] | 1945 | } else if (stateName.equals(kRefreshStateName)) { |
| 1946 | // This state is actually NOT in the UI state. |
| 1947 | // We use this to allow Android to quickly set bool fRefresh. |
| 1948 | fRefresh = stateValue.equals(kON); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 1949 | } else { |
| 1950 | SkDebugf("Unknown stateName: %s", stateName.c_str()); |
| 1951 | } |
| 1952 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1953 | |
| 1954 | 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] | 1955 | return fCommands.onKey(key, state, modifiers); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | bool Viewer::onChar(SkUnichar c, uint32_t modifiers) { |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 1959 | if (fSlides[fCurrentSlide]->onChar(c)) { |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 1960 | fWindow->inval(); |
| 1961 | return true; |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 1962 | } else { |
| 1963 | return fCommands.onChar(c, modifiers); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 1964 | } |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 1965 | } |