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