blob: c184eecadeec556f44134f681a3e12315aa4f9d2 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
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
jvanverth34524262016-05-04 13:49:13 -07008#include "Viewer.h"
jvanverth9f372462016-04-06 06:08:59 -07009
jvanverth2bb3b6d2016-04-08 07:24:09 -070010#include "GMSlide.h"
liyuqian6f163d22016-06-13 12:26:45 -070011#include "ImageSlide.h"
Greg Daniel9fcc7432016-11-29 16:35:19 -050012#include "Resources.h"
jvanverthc7027ab2016-06-16 09:52:35 -070013#include "SampleSlide.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070014#include "SKPSlide.h"
jvanverth9f372462016-04-06 06:08:59 -070015
csmartdalton61cd31a2017-02-27 17:00:53 -070016#include "GrContext.h"
Greg Daniel285db442016-10-14 09:12:53 -040017#include "SkATrace.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070018#include "SkCanvas.h"
Brian Osmana109e392017-02-24 09:49:14 -050019#include "SkColorSpace_Base.h"
Brian Osmanfdab5762017-11-09 10:27:55 -050020#include "SkColorSpacePriv.h"
Brian Osmane0d4fba2017-03-15 10:24:55 -040021#include "SkColorSpaceXformCanvas.h"
Brian Osman2dd96932016-10-18 15:33:53 -040022#include "SkCommandLineFlags.h"
Brian Osmanf9810662017-08-30 10:02:10 -040023#include "SkCommonFlagsGpuThreads.h"
csmartdalton008b9d82017-02-22 12:00:42 -070024#include "SkCommonFlagsPathRenderer.h"
liyuqian74959a12016-06-16 14:10:34 -070025#include "SkDashPathEffect.h"
Brian Osman53136aa2017-07-20 15:43:35 -040026#include "SkEventTracingPriv.h"
Greg Daniel285db442016-10-14 09:12:53 -040027#include "SkGraphics.h"
Brian Osmanf750fbc2017-02-08 10:47:28 -050028#include "SkImagePriv.h"
liyuqian6f163d22016-06-13 12:26:45 -070029#include "SkMetaData.h"
csmartdalton61cd31a2017-02-27 17:00:53 -070030#include "SkOnce.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070031#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050032#include "SkOSPath.h"
Brian Osman3ac99cf2017-12-01 11:23:53 -050033#include "SkPictureRecorder.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070034#include "SkRandom.h"
Yuqian Li399b3c22017-08-03 11:08:15 -040035#include "SkScan.h"
jvanverth2bb3b6d2016-04-08 07:24:09 -070036#include "SkStream.h"
liyuqian74959a12016-06-16 14:10:34 -070037#include "SkSurface.h"
Brian Osman79086b92017-02-10 13:36:16 -050038#include "SkSwizzle.h"
csmartdalton29d87152017-02-10 17:05:14 -050039#include "SkTaskGroup.h"
Yuqian Lib2ba6642017-11-22 12:07:41 -050040#include "SkThreadedBMPDevice.h"
liyuqian2edb0f42016-07-06 14:11:32 -070041#include "SkTime.h"
Mike Reed887cdf12017-04-03 11:11:09 -040042#include "SkVertices.h"
jvanverth9f372462016-04-06 06:08:59 -070043
Brian Osman79086b92017-02-10 13:36:16 -050044#include "imgui.h"
45
Chris Dalton1a325d22017-07-14 15:17:41 -060046#include "ccpr/GrCoverageCountingPathRenderer.h"
47
csmartdalton578f0642017-02-24 16:04:47 -070048#include <stdlib.h>
csmartdalton61cd31a2017-02-27 17:00:53 -070049#include <map>
csmartdalton578f0642017-02-24 16:04:47 -070050
jvanverth34524262016-05-04 13:49:13 -070051using namespace sk_app;
52
csmartdalton61cd31a2017-02-27 17:00:53 -070053static std::map<GpuPathRenderers, std::string> gPathRendererNames;
54
jvanverth9f372462016-04-06 06:08:59 -070055Application* Application::Create(int argc, char** argv, void* platformData) {
jvanverth34524262016-05-04 13:49:13 -070056 return new Viewer(argc, argv, platformData);
jvanverth9f372462016-04-06 06:08:59 -070057}
58
Brian Osman2dd96932016-10-18 15:33:53 -040059static DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
Brian Osman16adfa32016-10-18 14:42:44 -040060
Brian Osman2dd96932016-10-18 15:33:53 -040061static DEFINE_string2(match, m, nullptr,
jvanverth2bb3b6d2016-04-08 07:24:09 -070062 "[~][^]substring[$] [...] of bench name to run.\n"
63 "Multiple matches may be separated by spaces.\n"
64 "~ causes a matching bench to always be skipped\n"
65 "^ requires the start of the bench to match\n"
66 "$ requires the end of the bench to match\n"
67 "^ and $ requires an exact match\n"
68 "If a bench does not match any list entry,\n"
69 "it is skipped unless some list entry starts with ~");
bsalomon6c471f72016-07-26 12:56:32 -070070
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060071static DEFINE_string(slide, "", "Start on this sample.");
72static DEFINE_bool(list, false, "List samples?");
Jim Van Verth6f449692017-02-14 15:16:46 -050073
bsalomon6c471f72016-07-26 12:56:32 -070074#ifdef SK_VULKAN
jvanverthb8794cc2016-07-27 14:29:18 -070075# define BACKENDS_STR "\"sw\", \"gl\", and \"vk\""
bsalomon6c471f72016-07-26 12:56:32 -070076#else
77# define BACKENDS_STR "\"sw\" and \"gl\""
78#endif
79
liyuqian71491dc2016-06-09 12:02:34 -070080#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -040081static DEFINE_string(skps, "/data/local/tmp/skps", "Directory to read skps from.");
82static DEFINE_string(jpgs, "/data/local/tmp/resources", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070083#else
Brian Osman2dd96932016-10-18 15:33:53 -040084static DEFINE_string(skps, "skps", "Directory to read skps from.");
85static DEFINE_string(jpgs, "jpgs", "Directory to read jpgs from.");
liyuqian71491dc2016-06-09 12:02:34 -070086#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -070087
Brian Osman2dd96932016-10-18 15:33:53 -040088static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR ".");
bsalomon6c471f72016-07-26 12:56:32 -070089
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060090static DEFINE_int32(msaa, 0, "Number of subpixel samples. 0 for no HW antialiasing.");
91static DEFINE_bool(cachePathMasks, true, "Allows path mask textures to be cached in GPU configs.");
csmartdalton008b9d82017-02-22 12:00:42 -070092DEFINE_pathrenderer_flag;
93
Brian Osman53136aa2017-07-20 15:43:35 -040094DECLARE_int32(threads)
Brian Salomon41eac792017-03-08 14:03:56 -050095
Brian Salomon194db172017-08-17 14:37:06 -040096const char* kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
csmartdalton578f0642017-02-24 16:04:47 -070097 "OpenGL",
Brian Salomon194db172017-08-17 14:37:06 -040098#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
99 "ANGLE",
100#endif
jvanverth063ece72016-06-17 09:29:14 -0700101#ifdef SK_VULKAN
csmartdalton578f0642017-02-24 16:04:47 -0700102 "Vulkan",
jvanverth063ece72016-06-17 09:29:14 -0700103#endif
csmartdalton578f0642017-02-24 16:04:47 -0700104 "Raster"
jvanverthaf236b52016-05-20 06:01:06 -0700105};
106
bsalomon6c471f72016-07-26 12:56:32 -0700107static sk_app::Window::BackendType get_backend_type(const char* str) {
108#ifdef SK_VULKAN
109 if (0 == strcmp(str, "vk")) {
110 return sk_app::Window::kVulkan_BackendType;
111 } else
112#endif
Brian Salomon194db172017-08-17 14:37:06 -0400113#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
114 if (0 == strcmp(str, "angle")) {
115 return sk_app::Window::kANGLE_BackendType;
116 } else
117#endif
bsalomon6c471f72016-07-26 12:56:32 -0700118 if (0 == strcmp(str, "gl")) {
119 return sk_app::Window::kNativeGL_BackendType;
120 } else if (0 == strcmp(str, "sw")) {
121 return sk_app::Window::kRaster_BackendType;
122 } else {
123 SkDebugf("Unknown backend type, %s, defaulting to sw.", str);
124 return sk_app::Window::kRaster_BackendType;
125 }
126}
127
Brian Osmana109e392017-02-24 09:49:14 -0500128static SkColorSpacePrimaries gSrgbPrimaries = {
129 0.64f, 0.33f,
130 0.30f, 0.60f,
131 0.15f, 0.06f,
132 0.3127f, 0.3290f };
133
134static SkColorSpacePrimaries gAdobePrimaries = {
135 0.64f, 0.33f,
136 0.21f, 0.71f,
137 0.15f, 0.06f,
138 0.3127f, 0.3290f };
139
140static SkColorSpacePrimaries gP3Primaries = {
141 0.680f, 0.320f,
142 0.265f, 0.690f,
143 0.150f, 0.060f,
144 0.3127f, 0.3290f };
145
146static SkColorSpacePrimaries gRec2020Primaries = {
147 0.708f, 0.292f,
148 0.170f, 0.797f,
149 0.131f, 0.046f,
150 0.3127f, 0.3290f };
151
152struct NamedPrimaries {
153 const char* fName;
154 SkColorSpacePrimaries* fPrimaries;
155} gNamedPrimaries[] = {
156 { "sRGB", &gSrgbPrimaries },
157 { "AdobeRGB", &gAdobePrimaries },
158 { "P3", &gP3Primaries },
159 { "Rec. 2020", &gRec2020Primaries },
160};
161
162static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) {
163 return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0;
164}
165
Brian Osman70d2f432017-11-08 09:54:10 -0500166static Window::BackendType backend_type_for_window(Window::BackendType backendType) {
167 // In raster mode, we still use GL for the window.
168 // This lets us render the GUI faster (and correct).
169 return Window::kRaster_BackendType == backendType ? Window::kNativeGL_BackendType : backendType;
170}
171
liyuqiane5a6cd92016-05-27 08:52:52 -0700172const char* kName = "name";
173const char* kValue = "value";
174const char* kOptions = "options";
175const char* kSlideStateName = "Slide";
176const char* kBackendStateName = "Backend";
csmartdalton578f0642017-02-24 16:04:47 -0700177const char* kMSAAStateName = "MSAA";
csmartdalton61cd31a2017-02-27 17:00:53 -0700178const char* kPathRendererStateName = "Path renderer";
liyuqianb73c24b2016-06-03 08:47:23 -0700179const char* kSoftkeyStateName = "Softkey";
180const char* kSoftkeyHint = "Please select a softkey";
liyuqian1f508fd2016-06-07 06:57:40 -0700181const char* kFpsStateName = "FPS";
liyuqian6f163d22016-06-13 12:26:45 -0700182const char* kON = "ON";
183const char* kOFF = "OFF";
liyuqian2edb0f42016-07-06 14:11:32 -0700184const char* kRefreshStateName = "Refresh";
liyuqiane5a6cd92016-05-27 08:52:52 -0700185
jvanverth34524262016-05-04 13:49:13 -0700186Viewer::Viewer(int argc, char** argv, void* platformData)
jvanverthc265a922016-04-08 12:51:45 -0700187 : fCurrentMeasurement(0)
Jim Van Verth90dcce52017-11-03 13:36:07 -0400188 , fCumulativeMeasurementTime(0)
189 , fCumulativeMeasurementCount(0)
jvanverthc265a922016-04-08 12:51:45 -0700190 , fDisplayStats(false)
liyuqian2edb0f42016-07-06 14:11:32 -0700191 , fRefresh(false)
Brian Osman3ac99cf2017-12-01 11:23:53 -0500192 , fSaveToSKP(false)
Brian Osman79086b92017-02-10 13:36:16 -0500193 , fShowImGuiDebugWindow(false)
Brian Osmanfce09c52017-11-14 15:32:20 -0500194 , fShowSlidePicker(false)
Brian Osman79086b92017-02-10 13:36:16 -0500195 , fShowImGuiTestWindow(false)
Brian Osmanf6877092017-02-13 09:39:57 -0500196 , fShowZoomWindow(false)
197 , fLastImage(nullptr)
jvanverth063ece72016-06-17 09:29:14 -0700198 , fBackendType(sk_app::Window::kNativeGL_BackendType)
Brian Osman92004802017-03-06 11:47:26 -0500199 , fColorMode(ColorMode::kLegacy)
Brian Osmana109e392017-02-24 09:49:14 -0500200 , fColorSpacePrimaries(gSrgbPrimaries)
Brian Osmanfdab5762017-11-09 10:27:55 -0500201 // Our UI can only tweak gamma (currently), so start out gamma-only
202 , fColorSpaceTransferFn(g2Dot2_TransferFn)
egdaniel2a0bb0a2016-04-11 08:30:40 -0700203 , fZoomLevel(0.0f)
Brian Osmanb53f48c2017-06-07 10:00:30 -0400204 , fGestureDevice(GestureDevice::kNone)
Yuqian Lib2ba6642017-11-22 12:07:41 -0500205 , fTileCnt(0)
206 , fThreadCnt(0)
jvanverthc265a922016-04-08 12:51:45 -0700207{
Greg Daniel285db442016-10-14 09:12:53 -0400208 SkGraphics::Init();
csmartdalton61cd31a2017-02-27 17:00:53 -0700209
210 static SkOnce initPathRendererNames;
211 initPathRendererNames([]() {
Brian Osman8b0f2652017-08-29 15:18:34 -0400212 gPathRendererNames[GpuPathRenderers::kAll] = "All Path Renderers";
213 gPathRendererNames[GpuPathRenderers::kDefault] =
214 "Default Ganesh Behavior (best path renderer, not including CCPR)";
csmartdalton61cd31a2017-02-27 17:00:53 -0700215 gPathRendererNames[GpuPathRenderers::kStencilAndCover] = "NV_path_rendering";
216 gPathRendererNames[GpuPathRenderers::kMSAA] = "Sample shading";
Jim Van Verth83010462017-03-16 08:45:39 -0400217 gPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)";
Chris Dalton1a325d22017-07-14 15:17:41 -0600218 gPathRendererNames[GpuPathRenderers::kCoverageCounting] = "Coverage counting";
Brian Osman8a9de3d2017-03-01 14:59:05 -0500219 gPathRendererNames[GpuPathRenderers::kTessellating] = "Tessellating";
csmartdalton61cd31a2017-02-27 17:00:53 -0700220 gPathRendererNames[GpuPathRenderers::kNone] = "Software masks";
221 });
222
Brian Osman1df161a2017-02-09 12:10:20 -0500223 memset(fPaintTimes, 0, sizeof(fPaintTimes));
224 memset(fFlushTimes, 0, sizeof(fFlushTimes));
225 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
jvanverth9f372462016-04-06 06:08:59 -0700226
jvanverth2bb3b6d2016-04-08 07:24:09 -0700227 SkDebugf("Command line arguments: ");
228 for (int i = 1; i < argc; ++i) {
229 SkDebugf("%s ", argv[i]);
230 }
231 SkDebugf("\n");
232
233 SkCommandLineFlags::Parse(argc, argv);
Greg Daniel9fcc7432016-11-29 16:35:19 -0500234#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon96789b32017-05-26 12:06:21 -0400235 SetResourcePath("/data/local/tmp/resources");
Greg Daniel9fcc7432016-11-29 16:35:19 -0500236#endif
jvanverth2bb3b6d2016-04-08 07:24:09 -0700237
Brian Osmanbc8150f2017-07-24 11:38:01 -0400238 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -0400239 static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads);
Greg Daniel285db442016-10-14 09:12:53 -0400240
bsalomon6c471f72016-07-26 12:56:32 -0700241 fBackendType = get_backend_type(FLAGS_backend[0]);
jvanverth9f372462016-04-06 06:08:59 -0700242 fWindow = Window::CreateNativeWindow(platformData);
jvanverth9f372462016-04-06 06:08:59 -0700243
csmartdalton578f0642017-02-24 16:04:47 -0700244 DisplayParams displayParams;
245 displayParams.fMSAASampleCount = FLAGS_msaa;
csmartdalton61cd31a2017-02-27 17:00:53 -0700246 displayParams.fGrContextOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
Chris Dalton7a0ebfc2017-10-13 12:35:50 -0600247 displayParams.fGrContextOptions.fAllowPathMaskCaching = FLAGS_cachePathMasks;
Brian Osmanf9810662017-08-30 10:02:10 -0400248 displayParams.fGrContextOptions.fExecutor = GpuExecutorForTools();
csmartdalton578f0642017-02-24 16:04:47 -0700249 fWindow->setRequestedDisplayParams(displayParams);
250
jvanverth9f372462016-04-06 06:08:59 -0700251 // register callbacks
brianosman622c8d52016-05-10 06:50:49 -0700252 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500253 fWindow->pushLayer(this);
Brian Osmand67e5182017-12-08 16:46:09 -0500254 fWindow->pushLayer(&fImGuiLayer);
jvanverth9f372462016-04-06 06:08:59 -0700255
brianosman622c8d52016-05-10 06:50:49 -0700256 // add key-bindings
Brian Osman79086b92017-02-10 13:36:16 -0500257 fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() {
258 this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow;
259 fWindow->inval();
260 });
Brian Osmanfce09c52017-11-14 15:32:20 -0500261 // Command to jump directly to the slide picker and give it focus
262 fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() {
263 this->fShowImGuiDebugWindow = true;
264 this->fShowSlidePicker = true;
265 fWindow->inval();
266 });
267 // Alias that to Backspace, to match SampleApp
268 fCommands.addCommand(Window::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() {
269 this->fShowImGuiDebugWindow = true;
270 this->fShowSlidePicker = true;
271 fWindow->inval();
272 });
Brian Osman79086b92017-02-10 13:36:16 -0500273 fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() {
274 this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow;
275 fWindow->inval();
276 });
Brian Osmanf6877092017-02-13 09:39:57 -0500277 fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() {
278 this->fShowZoomWindow = !this->fShowZoomWindow;
279 fWindow->inval();
280 });
brianosman622c8d52016-05-10 06:50:49 -0700281 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
282 this->fDisplayStats = !this->fDisplayStats;
283 fWindow->inval();
284 });
Jim Van Verth90dcce52017-11-03 13:36:07 -0400285 fCommands.addCommand('0', "Overlays", "Reset stats", [this]() {
286 this->resetMeasurements();
287 this->updateTitle();
288 fWindow->inval();
289 });
Brian Osmanf750fbc2017-02-08 10:47:28 -0500290 fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() {
Brian Osman92004802017-03-06 11:47:26 -0500291 switch (fColorMode) {
292 case ColorMode::kLegacy:
293 this->setColorMode(ColorMode::kColorManagedSRGB8888_NonLinearBlending);
294 break;
295 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
296 this->setColorMode(ColorMode::kColorManagedSRGB8888);
297 break;
298 case ColorMode::kColorManagedSRGB8888:
299 this->setColorMode(ColorMode::kColorManagedLinearF16);
300 break;
301 case ColorMode::kColorManagedLinearF16:
302 this->setColorMode(ColorMode::kLegacy);
303 break;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500304 }
brianosman622c8d52016-05-10 06:50:49 -0700305 });
306 fCommands.addCommand(Window::Key::kRight, "Right", "Navigation", "Next slide", [this]() {
307 int previousSlide = fCurrentSlide;
308 fCurrentSlide++;
309 if (fCurrentSlide >= fSlides.count()) {
310 fCurrentSlide = 0;
311 }
312 this->setupCurrentSlide(previousSlide);
313 });
314 fCommands.addCommand(Window::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() {
315 int previousSlide = fCurrentSlide;
316 fCurrentSlide--;
317 if (fCurrentSlide < 0) {
318 fCurrentSlide = fSlides.count() - 1;
319 }
320 this->setupCurrentSlide(previousSlide);
321 });
322 fCommands.addCommand(Window::Key::kUp, "Up", "Transform", "Zoom in", [this]() {
323 this->changeZoomLevel(1.f / 32.f);
324 fWindow->inval();
325 });
326 fCommands.addCommand(Window::Key::kDown, "Down", "Transform", "Zoom out", [this]() {
327 this->changeZoomLevel(-1.f / 32.f);
328 fWindow->inval();
329 });
jvanverthaf236b52016-05-20 06:01:06 -0700330 fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() {
Brian Salomon194db172017-08-17 14:37:06 -0400331 sk_app::Window::BackendType newBackend = (sk_app::Window::BackendType)(
332 (fBackendType + 1) % sk_app::Window::kBackendTypeCount);
Jim Van Verthd63c1022017-01-05 13:50:49 -0500333 // Switching to and from Vulkan is problematic on Linux so disabled for now
Brian Salomon194db172017-08-17 14:37:06 -0400334#if defined(SK_BUILD_FOR_UNIX) && defined(SK_VULKAN)
335 if (newBackend == sk_app::Window::kVulkan_BackendType) {
336 newBackend = (sk_app::Window::BackendType)((newBackend + 1) %
337 sk_app::Window::kBackendTypeCount);
338 } else if (fBackendType == sk_app::Window::kVulkan_BackendType) {
339 newBackend = sk_app::Window::kVulkan_BackendType;
Jim Van Verthd63c1022017-01-05 13:50:49 -0500340 }
341#endif
Brian Osman621491e2017-02-28 15:45:01 -0500342 this->setBackend(newBackend);
jvanverthaf236b52016-05-20 06:01:06 -0700343 });
brianosman622c8d52016-05-10 06:50:49 -0700344
Yuqian Lib2ba6642017-11-22 12:07:41 -0500345 fCommands.addCommand('A', "AA", "Toggle analytic AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400346 if (!gSkUseAnalyticAA) {
347 gSkUseAnalyticAA = true;
348 } else if (!gSkForceAnalyticAA) {
349 gSkForceAnalyticAA = true;
350 } else {
351 gSkUseAnalyticAA = gSkForceAnalyticAA = false;
352 }
353 this->updateTitle();
354 fWindow->inval();
355 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500356 fCommands.addCommand('D', "AA", "Toggle delta AA", [this]() {
Yuqian Li399b3c22017-08-03 11:08:15 -0400357 if (!gSkUseDeltaAA) {
358 gSkUseDeltaAA = true;
359 } else if (!gSkForceDeltaAA) {
360 gSkForceDeltaAA = true;
361 } else {
362 gSkUseDeltaAA = gSkForceDeltaAA = false;
363 }
364 this->updateTitle();
365 fWindow->inval();
366 });
367
Yuqian Lib2ba6642017-11-22 12:07:41 -0500368 fCommands.addCommand('+', "Threaded Backend", "Increase tile count", [this]() {
369 fTileCnt++;
370 if (fThreadCnt == 0) {
371 this->resetExecutor();
372 }
373 this->updateTitle();
374 fWindow->inval();
375 });
376 fCommands.addCommand('-', "Threaded Backend", "Decrease tile count", [this]() {
377 fTileCnt = SkTMax(0, fTileCnt - 1);
378 if (fThreadCnt == 0) {
379 this->resetExecutor();
380 }
381 this->updateTitle();
382 fWindow->inval();
383 });
384 fCommands.addCommand('>', "Threaded Backend", "Increase thread count", [this]() {
385 if (fTileCnt == 0) {
386 return;
387 }
388 fThreadCnt = (fThreadCnt + 1) % fTileCnt;
389 this->resetExecutor();
390 this->updateTitle();
391 fWindow->inval();
392 });
393 fCommands.addCommand('<', "Threaded Backend", "Decrease thread count", [this]() {
394 if (fTileCnt == 0) {
395 return;
396 }
397 fThreadCnt = (fThreadCnt + fTileCnt - 1) % fTileCnt;
398 this->resetExecutor();
399 this->updateTitle();
400 fWindow->inval();
401 });
Brian Osman3ac99cf2017-12-01 11:23:53 -0500402 fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() {
403 fSaveToSKP = true;
404 fWindow->inval();
405 });
Yuqian Lib2ba6642017-11-22 12:07:41 -0500406
jvanverth2bb3b6d2016-04-08 07:24:09 -0700407 // set up slides
408 this->initSlides();
Jim Van Verth6f449692017-02-14 15:16:46 -0500409 this->setStartupSlide();
410 if (FLAGS_list) {
411 this->listNames();
412 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700413
djsollen12d62a72016-04-21 07:59:44 -0700414 fAnimTimer.run();
415
Hal Canaryc465d132017-12-08 10:21:31 -0500416 auto gamutImage = GetResourceAsImage("images/gamut.png");
Brian Osmana109e392017-02-24 09:49:14 -0500417 if (gamutImage) {
Mike Reed0acd7952017-04-28 11:12:19 -0400418 fImGuiGamutPaint.setShader(gamutImage->makeShader());
Brian Osmana109e392017-02-24 09:49:14 -0500419 }
420 fImGuiGamutPaint.setColor(SK_ColorWHITE);
421 fImGuiGamutPaint.setFilterQuality(kLow_SkFilterQuality);
422
Brian Osman70d2f432017-11-08 09:54:10 -0500423 fWindow->attach(backend_type_for_window(fBackendType));
jvanverth9f372462016-04-06 06:08:59 -0700424}
425
jvanverth34524262016-05-04 13:49:13 -0700426void Viewer::initSlides() {
liyuqian1f508fd2016-06-07 06:57:40 -0700427 fAllSlideNames = Json::Value(Json::arrayValue);
428
jvanverth2bb3b6d2016-04-08 07:24:09 -0700429 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
430 while (gms) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400431 std::unique_ptr<skiagm::GM> gm(gms->factory()(nullptr));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700432
433 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
434 sk_sp<Slide> slide(new GMSlide(gm.release()));
435 fSlides.push_back(slide);
436 }
437
438 gms = gms->next();
439 }
440
441 // reverse array
442 for (int i = 0; i < fSlides.count()/2; ++i) {
443 sk_sp<Slide> temp = fSlides[i];
444 fSlides[i] = fSlides[fSlides.count() - i - 1];
445 fSlides[fSlides.count() - i - 1] = temp;
446 }
447
jvanverthc7027ab2016-06-16 09:52:35 -0700448 // samples
449 const SkViewRegister* reg = SkViewRegister::Head();
450 while (reg) {
451 sk_sp<Slide> slide(new SampleSlide(reg->factory()));
brianosmane1d20072016-07-12 09:07:33 -0700452 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) {
453 fSlides.push_back(slide);
454 }
jvanverthc7027ab2016-06-16 09:52:35 -0700455 reg = reg->next();
456 }
457
jvanverth2bb3b6d2016-04-08 07:24:09 -0700458 // SKPs
459 for (int i = 0; i < FLAGS_skps.count(); i++) {
460 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
jvanverthc265a922016-04-08 12:51:45 -0700461 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
462 continue;
463 }
464
jvanverth2bb3b6d2016-04-08 07:24:09 -0700465 SkString path(FLAGS_skps[i]);
jvanverthc265a922016-04-08 12:51:45 -0700466 sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700467 if (slide) {
468 fSlides.push_back(slide);
469 }
470 } else {
471 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
jvanverthc265a922016-04-08 12:51:45 -0700472 SkString skpName;
473 while (it.next(&skpName)) {
474 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
475 continue;
476 }
477
478 SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
479 sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
jvanverth2bb3b6d2016-04-08 07:24:09 -0700480 if (slide) {
481 fSlides.push_back(slide);
482 }
483 }
484 }
485 }
liyuqian6f163d22016-06-13 12:26:45 -0700486
487 // JPGs
488 for (int i = 0; i < FLAGS_jpgs.count(); i++) {
489 SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg");
490 SkString jpgName;
491 while (it.next(&jpgName)) {
brianosmane1d20072016-07-12 09:07:33 -0700492 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) {
493 continue;
494 }
495
liyuqian6f163d22016-06-13 12:26:45 -0700496 SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str());
497 sk_sp<ImageSlide> slide(new ImageSlide(jpgName, path));
498 if (slide) {
499 fSlides.push_back(slide);
500 }
501 }
502 }
jvanverth2bb3b6d2016-04-08 07:24:09 -0700503}
504
505
jvanverth34524262016-05-04 13:49:13 -0700506Viewer::~Viewer() {
jvanverth9f372462016-04-06 06:08:59 -0700507 fWindow->detach();
508 delete fWindow;
509}
510
brianosman05de2162016-05-06 13:28:57 -0700511void Viewer::updateTitle() {
csmartdalton578f0642017-02-24 16:04:47 -0700512 if (!fWindow) {
513 return;
514 }
515 if (fWindow->sampleCount() < 0) {
516 return; // Surface hasn't been created yet.
517 }
518
jvanverth34524262016-05-04 13:49:13 -0700519 SkString title("Viewer: ");
jvanverthc265a922016-04-08 12:51:45 -0700520 title.append(fSlides[fCurrentSlide]->getName());
brianosmanb109b8c2016-06-16 13:03:24 -0700521
Yuqian Li399b3c22017-08-03 11:08:15 -0400522 if (gSkUseDeltaAA) {
523 if (gSkForceDeltaAA) {
524 title.append(" <FDAA>");
525 } else {
526 title.append(" <DAA>");
527 }
528 } else if (gSkUseAnalyticAA) {
529 if (gSkForceAnalyticAA) {
530 title.append(" <FAAA>");
531 } else {
532 title.append(" <AAA>");
533 }
534 }
535
Yuqian Lib2ba6642017-11-22 12:07:41 -0500536 if (fTileCnt > 0) {
537 title.appendf(" T%d", fTileCnt);
538 if (fThreadCnt > 0) {
539 title.appendf("/%d", fThreadCnt);
540 }
541 }
542
Brian Osman92004802017-03-06 11:47:26 -0500543 switch (fColorMode) {
544 case ColorMode::kLegacy:
545 title.append(" Legacy 8888");
546 break;
547 case ColorMode::kColorManagedSRGB8888_NonLinearBlending:
548 title.append(" ColorManaged 8888 (Nonlinear blending)");
549 break;
550 case ColorMode::kColorManagedSRGB8888:
551 title.append(" ColorManaged 8888");
552 break;
553 case ColorMode::kColorManagedLinearF16:
554 title.append(" ColorManaged F16");
555 break;
556 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500557
Brian Osman92004802017-03-06 11:47:26 -0500558 if (ColorMode::kLegacy != fColorMode) {
Brian Osmana109e392017-02-24 09:49:14 -0500559 int curPrimaries = -1;
560 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
561 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
562 curPrimaries = i;
563 break;
564 }
565 }
566 title.appendf(" %s", curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom");
Brian Osmanfdab5762017-11-09 10:27:55 -0500567
568 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
569 title.appendf(" Gamma %f", fColorSpaceTransferFn.fG);
570 }
brianosman05de2162016-05-06 13:28:57 -0700571 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500572
csmartdalton578f0642017-02-24 16:04:47 -0700573 title.append(" [");
jvanverthaf236b52016-05-20 06:01:06 -0700574 title.append(kBackendTypeStrings[fBackendType]);
csmartdalton578f0642017-02-24 16:04:47 -0700575 if (int msaa = fWindow->sampleCount()) {
576 title.appendf(" MSAA: %i", msaa);
577 }
578 title.append("]");
csmartdalton61cd31a2017-02-27 17:00:53 -0700579
580 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
Brian Osman8b0f2652017-08-29 15:18:34 -0400581 if (GpuPathRenderers::kDefault != pr) {
csmartdalton61cd31a2017-02-27 17:00:53 -0700582 title.appendf(" [Path renderer: %s]", gPathRendererNames[pr].c_str());
583 }
584
brianosman05de2162016-05-06 13:28:57 -0700585 fWindow->setTitle(title.c_str());
586}
587
Jim Van Verth6f449692017-02-14 15:16:46 -0500588void Viewer::setStartupSlide() {
589
590 if (!FLAGS_slide.isEmpty()) {
591 int count = fSlides.count();
592 for (int i = 0; i < count; i++) {
593 if (fSlides[i]->getName().equals(FLAGS_slide[0])) {
594 fCurrentSlide = i;
595 return;
596 }
597 }
598
599 fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]);
600 this->listNames();
601 }
602
603 fCurrentSlide = 0;
604}
605
606void Viewer::listNames() {
607 int count = fSlides.count();
608 SkDebugf("All Slides:\n");
609 for (int i = 0; i < count; i++) {
610 SkDebugf(" %s\n", fSlides[i]->getName().c_str());
611 }
612}
613
Jim Van Verth90dcce52017-11-03 13:36:07 -0400614void Viewer::resetMeasurements() {
615 memset(fPaintTimes, 0, sizeof(fPaintTimes));
616 memset(fFlushTimes, 0, sizeof(fFlushTimes));
617 memset(fAnimateTimes, 0, sizeof(fAnimateTimes));
618 fCurrentMeasurement = 0;
619 fCumulativeMeasurementTime = 0;
620 fCumulativeMeasurementCount = 0;
621}
622
brianosman05de2162016-05-06 13:28:57 -0700623void Viewer::setupCurrentSlide(int previousSlide) {
liyuqiane5a6cd92016-05-27 08:52:52 -0700624 if (fCurrentSlide == previousSlide) {
625 return; // no change; do nothing
626 }
liyuqian6f163d22016-06-13 12:26:45 -0700627 // prepare dimensions for image slides
jvanverthc7027ab2016-06-16 09:52:35 -0700628 fSlides[fCurrentSlide]->load(SkIntToScalar(fWindow->width()), SkIntToScalar(fWindow->height()));
liyuqian6f163d22016-06-13 12:26:45 -0700629
liyuqiane46e4f02016-05-20 07:32:19 -0700630 fGesture.reset();
631 fDefaultMatrix.reset();
liyuqiane46e4f02016-05-20 07:32:19 -0700632
Brian Osman42bb6ac2017-06-05 08:46:04 -0400633 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
634 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height());
635 const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height());
636
637 // Start with a matrix that scales the slide to the available screen space
638 if (fWindow->scaleContentToFit()) {
639 if (windowRect.width() > 0 && windowRect.height() > 0) {
640 fDefaultMatrix.setRectToRect(slideBounds, windowRect, SkMatrix::kStart_ScaleToFit);
liyuqiane46e4f02016-05-20 07:32:19 -0700641 }
642 }
643
Brian Osman42bb6ac2017-06-05 08:46:04 -0400644 // Prevent the user from dragging content so far outside the window they can't find it again
645 fGesture.setTransLimit(slideBounds, windowRect, fDefaultMatrix);
liyuqiane46e4f02016-05-20 07:32:19 -0700646
brianosman05de2162016-05-06 13:28:57 -0700647 this->updateTitle();
liyuqiane5a6cd92016-05-27 08:52:52 -0700648 this->updateUIState();
jvanverthc265a922016-04-08 12:51:45 -0700649 if (previousSlide >= 0) {
650 fSlides[previousSlide]->unload();
651 }
Jim Van Verth90dcce52017-11-03 13:36:07 -0400652
653 this->resetMeasurements();
654
jvanverthc265a922016-04-08 12:51:45 -0700655 fWindow->inval();
656}
657
658#define MAX_ZOOM_LEVEL 8
659#define MIN_ZOOM_LEVEL -8
660
jvanverth34524262016-05-04 13:49:13 -0700661void Viewer::changeZoomLevel(float delta) {
jvanverthc265a922016-04-08 12:51:45 -0700662 fZoomLevel += delta;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400663 fZoomLevel = SkScalarPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
jvanverthc265a922016-04-08 12:51:45 -0700664}
665
liyuqiand3cdbca2016-05-17 12:44:20 -0700666SkMatrix Viewer::computeMatrix() {
jvanverthc265a922016-04-08 12:51:45 -0700667 SkMatrix m;
jvanverthc265a922016-04-08 12:51:45 -0700668
Brian Osman42bb6ac2017-06-05 08:46:04 -0400669 SkScalar zoomScale = (fZoomLevel < 0) ? SK_Scalar1 / (SK_Scalar1 - fZoomLevel)
670 : SK_Scalar1 + fZoomLevel;
671 m = fGesture.localM();
liyuqiand3cdbca2016-05-17 12:44:20 -0700672 m.preConcat(fGesture.globalM());
Brian Osman42bb6ac2017-06-05 08:46:04 -0400673 m.preConcat(fDefaultMatrix);
674 m.preScale(zoomScale, zoomScale);
jvanverthc265a922016-04-08 12:51:45 -0700675
liyuqiand3cdbca2016-05-17 12:44:20 -0700676 return m;
jvanverthc265a922016-04-08 12:51:45 -0700677}
678
Brian Osman621491e2017-02-28 15:45:01 -0500679void Viewer::setBackend(sk_app::Window::BackendType backendType) {
680 fBackendType = backendType;
681
682 fWindow->detach();
683
Brian Osman70d2f432017-11-08 09:54:10 -0500684#if defined(SK_BUILD_FOR_WIN)
Brian Salomon194db172017-08-17 14:37:06 -0400685 // Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point
686 // on Windows, so we just delete the window and recreate it.
Brian Osman70d2f432017-11-08 09:54:10 -0500687 DisplayParams params = fWindow->getRequestedDisplayParams();
688 delete fWindow;
689 fWindow = Window::CreateNativeWindow(nullptr);
Brian Osman621491e2017-02-28 15:45:01 -0500690
Brian Osman70d2f432017-11-08 09:54:10 -0500691 // re-register callbacks
692 fCommands.attach(fWindow);
Brian Osman80fc07e2017-12-08 16:45:43 -0500693 fWindow->pushLayer(this);
Brian Osmand67e5182017-12-08 16:46:09 -0500694 fWindow->pushLayer(&fImGuiLayer);
695
Brian Osman70d2f432017-11-08 09:54:10 -0500696 // Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above
697 // will still include our correct sample count. But the re-created fWindow will lose that
698 // information. On Windows, we need to re-create the window when changing sample count,
699 // so we'll incorrectly detect that situation, then re-initialize the window in GL mode,
700 // rendering this tear-down step pointless (and causing the Vulkan window context to fail
701 // as if we had never changed windows at all).
702 fWindow->setRequestedDisplayParams(params, false);
Brian Osman621491e2017-02-28 15:45:01 -0500703#endif
704
Brian Osman70d2f432017-11-08 09:54:10 -0500705 fWindow->attach(backend_type_for_window(fBackendType));
Brian Osman621491e2017-02-28 15:45:01 -0500706}
707
Brian Osman92004802017-03-06 11:47:26 -0500708void Viewer::setColorMode(ColorMode colorMode) {
709 fColorMode = colorMode;
liyuqian6f163d22016-06-13 12:26:45 -0700710
Brian Osmanf750fbc2017-02-08 10:47:28 -0500711 // When we're in color managed mode, we tag our window surface as sRGB. If we've switched into
Brian Osmane0d4fba2017-03-15 10:24:55 -0400712 // or out of legacy/nonlinear mode, we need to update our window configuration.
csmartdalton578f0642017-02-24 16:04:47 -0700713 DisplayParams params = fWindow->getRequestedDisplayParams();
Brian Osman92004802017-03-06 11:47:26 -0500714 bool wasInLegacy = !SkToBool(params.fColorSpace);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400715 bool wantLegacy = (ColorMode::kLegacy == fColorMode) ||
716 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode);
Brian Osman92004802017-03-06 11:47:26 -0500717 if (wasInLegacy != wantLegacy) {
718 params.fColorSpace = wantLegacy ? nullptr : SkColorSpace::MakeSRGB();
csmartdalton578f0642017-02-24 16:04:47 -0700719 fWindow->setRequestedDisplayParams(params);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500720 }
721
722 this->updateTitle();
723 fWindow->inval();
724}
725
726void Viewer::drawSlide(SkCanvas* canvas) {
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500727 SkAutoCanvasRestore autorestore(canvas, false);
728
Brian Osmanf750fbc2017-02-08 10:47:28 -0500729 // By default, we render directly into the window's surface/canvas
730 SkCanvas* slideCanvas = canvas;
Brian Osmanf6877092017-02-13 09:39:57 -0500731 fLastImage.reset();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700732
Brian Osmane0d4fba2017-03-15 10:24:55 -0400733 // If we're in any of the color managed modes, construct the color space we're going to use
734 sk_sp<SkColorSpace> cs = nullptr;
735 if (ColorMode::kLegacy != fColorMode) {
736 auto transferFn = (ColorMode::kColorManagedLinearF16 == fColorMode)
737 ? SkColorSpace::kLinear_RenderTargetGamma : SkColorSpace::kSRGB_RenderTargetGamma;
Mike Kleinc722f792017-07-31 11:57:21 -0400738 SkMatrix44 toXYZ(SkMatrix44::kIdentity_Constructor);
Brian Osmane0d4fba2017-03-15 10:24:55 -0400739 SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ));
Brian Osmanfdab5762017-11-09 10:27:55 -0500740 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
741 cs = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ);
742 } else {
743 cs = SkColorSpace::MakeRGB(transferFn, toXYZ);
744 }
Brian Osmane0d4fba2017-03-15 10:24:55 -0400745 }
746
Brian Osman3ac99cf2017-12-01 11:23:53 -0500747 if (fSaveToSKP) {
748 SkPictureRecorder recorder;
749 SkCanvas* recorderCanvas = recorder.beginRecording(
750 SkRect::Make(fSlides[fCurrentSlide]->getDimensions()));
751 // In xform-canvas mode, record the transformed output
752 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
753 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
754 xformCanvas = SkCreateColorSpaceXformCanvas(recorderCanvas, cs);
755 recorderCanvas = xformCanvas.get();
756 }
757 fSlides[fCurrentSlide]->draw(recorderCanvas);
758 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
759 SkFILEWStream stream("sample_app.skp");
760 picture->serialize(&stream);
761 fSaveToSKP = false;
762 }
763
Brian Osmane0d4fba2017-03-15 10:24:55 -0400764 // If we're in F16, or we're zooming, or we're in color correct 8888 and the gamut isn't sRGB,
Brian Osman70d2f432017-11-08 09:54:10 -0500765 // we need to render offscreen. We also need to render offscreen if we're in any raster mode,
766 // because the window surface is actually GL.
Brian Osmanf750fbc2017-02-08 10:47:28 -0500767 sk_sp<SkSurface> offscreenSurface = nullptr;
Yuqian Lib2ba6642017-11-22 12:07:41 -0500768 std::unique_ptr<SkThreadedBMPDevice> threadedDevice;
769 std::unique_ptr<SkCanvas> threadedCanvas;
Brian Osman70d2f432017-11-08 09:54:10 -0500770 if (Window::kRaster_BackendType == fBackendType ||
771 ColorMode::kColorManagedLinearF16 == fColorMode ||
Brian Osman92004802017-03-06 11:47:26 -0500772 fShowZoomWindow ||
Brian Osmane0d4fba2017-03-15 10:24:55 -0400773 (ColorMode::kColorManagedSRGB8888 == fColorMode &&
774 !primaries_equal(fColorSpacePrimaries, gSrgbPrimaries))) {
775
Brian Osman92004802017-03-06 11:47:26 -0500776 SkColorType colorType = (ColorMode::kColorManagedLinearF16 == fColorMode)
777 ? kRGBA_F16_SkColorType : kN32_SkColorType;
Brian Osmane0d4fba2017-03-15 10:24:55 -0400778 // In nonlinear blending mode, we actually use a legacy off-screen canvas, and wrap it
779 // with a special canvas (below) that has the color space attached
780 sk_sp<SkColorSpace> offscreenColorSpace =
781 (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) ? nullptr : cs;
Brian Osman92004802017-03-06 11:47:26 -0500782 SkImageInfo info = SkImageInfo::Make(fWindow->width(), fWindow->height(), colorType,
Brian Osmane0d4fba2017-03-15 10:24:55 -0400783 kPremul_SkAlphaType, std::move(offscreenColorSpace));
Brian Osman70d2f432017-11-08 09:54:10 -0500784 offscreenSurface = Window::kRaster_BackendType == fBackendType ? SkSurface::MakeRaster(info)
785 : canvas->makeSurface(info);
Yuqian Lib2ba6642017-11-22 12:07:41 -0500786 SkPixmap offscreenPixmap;
787 if (fTileCnt > 0 && offscreenSurface->peekPixels(&offscreenPixmap)) {
788 SkBitmap offscreenBitmap;
789 offscreenBitmap.installPixels(offscreenPixmap);
790 threadedDevice.reset(new SkThreadedBMPDevice(offscreenBitmap, fTileCnt,
791 fThreadCnt, fExecutor.get()));
792 threadedCanvas.reset(new SkCanvas(threadedDevice.get()));
793 slideCanvas = threadedCanvas.get();
794 } else {
795 slideCanvas = offscreenSurface->getCanvas();
796 }
Brian Osmanf750fbc2017-02-08 10:47:28 -0500797 }
798
Brian Osmane0d4fba2017-03-15 10:24:55 -0400799 std::unique_ptr<SkCanvas> xformCanvas = nullptr;
800 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
801 xformCanvas = SkCreateColorSpaceXformCanvas(slideCanvas, cs);
802 slideCanvas = xformCanvas.get();
803 }
804
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500805 int count = slideCanvas->save();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500806 slideCanvas->clear(SK_ColorWHITE);
Brian Osmanf750fbc2017-02-08 10:47:28 -0500807 slideCanvas->concat(computeMatrix());
Brian Osman1df161a2017-02-09 12:10:20 -0500808 // Time the painting logic of the slide
809 double startTime = SkTime::GetMSecs();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500810 fSlides[fCurrentSlide]->draw(slideCanvas);
Brian Osman1df161a2017-02-09 12:10:20 -0500811 fPaintTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500812 slideCanvas->restoreToCount(count);
Brian Osman1df161a2017-02-09 12:10:20 -0500813
814 // Force a flush so we can time that, too
815 startTime = SkTime::GetMSecs();
816 slideCanvas->flush();
817 fFlushTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500818
819 // If we rendered offscreen, snap an image and push the results to the window's canvas
820 if (offscreenSurface) {
Brian Osmanf6877092017-02-13 09:39:57 -0500821 fLastImage = offscreenSurface->makeImageSnapshot();
Brian Osmanf750fbc2017-02-08 10:47:28 -0500822
823 // Tag the image with the sRGB gamut, so no further color space conversion happens
Brian Osmane0d4fba2017-03-15 10:24:55 -0400824 sk_sp<SkColorSpace> srgb = (ColorMode::kColorManagedLinearF16 == fColorMode)
Brian Osmanf750fbc2017-02-08 10:47:28 -0500825 ? SkColorSpace::MakeSRGBLinear() : SkColorSpace::MakeSRGB();
Brian Osmane0d4fba2017-03-15 10:24:55 -0400826 auto retaggedImage = SkImageMakeRasterCopyAndAssignColorSpace(fLastImage.get(), srgb.get());
Brian Salomonbf52e3d2017-02-22 15:21:11 -0500827 SkPaint paint;
828 paint.setBlendMode(SkBlendMode::kSrc);
829 canvas->drawImage(retaggedImage, 0, 0, &paint);
liyuqian74959a12016-06-16 14:10:34 -0700830 }
liyuqian6f163d22016-06-13 12:26:45 -0700831}
832
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700833void Viewer::onBackendCreated() {
834 this->updateTitle();
835 this->updateUIState();
836 this->setupCurrentSlide(-1);
Jim Van Verth90dcce52017-11-03 13:36:07 -0400837 this->resetMeasurements();
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700838 fWindow->show();
839 fWindow->inval();
840}
Jim Van Verth6f449692017-02-14 15:16:46 -0500841
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700842void Viewer::onPaint(SkCanvas* canvas) {
Jim Van Verth90dcce52017-11-03 13:36:07 -0400843 this->drawSlide(canvas);
jvanverthc265a922016-04-08 12:51:45 -0700844
Brian Osman1df161a2017-02-09 12:10:20 -0500845 // Advance our timing bookkeeping
Jim Van Verth90dcce52017-11-03 13:36:07 -0400846 fCumulativeMeasurementTime += fAnimateTimes[fCurrentMeasurement] +
847 fPaintTimes[fCurrentMeasurement] +
848 fFlushTimes[fCurrentMeasurement];
849 fCumulativeMeasurementCount++;
Brian Osman1df161a2017-02-09 12:10:20 -0500850 fCurrentMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1);
851 SkASSERT(fCurrentMeasurement < kMeasurementCount);
852
853 // Draw any overlays or UI that we don't want timed
jvanverthc265a922016-04-08 12:51:45 -0700854 if (fDisplayStats) {
855 drawStats(canvas);
856 }
brianosman622c8d52016-05-10 06:50:49 -0700857 fCommands.drawHelp(canvas);
liyuqian2edb0f42016-07-06 14:11:32 -0700858
Brian Osmand67e5182017-12-08 16:46:09 -0500859 this->drawImGui();
Brian Osman79086b92017-02-10 13:36:16 -0500860
Brian Osman1df161a2017-02-09 12:10:20 -0500861 // Update the FPS
Jim Van Verth90dcce52017-11-03 13:36:07 -0400862 this->updateUIState();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700863}
864
jvanverth814e38d2016-06-06 08:48:47 -0700865bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400866 if (GestureDevice::kMouse == fGestureDevice) {
867 return false;
868 }
liyuqiand3cdbca2016-05-17 12:44:20 -0700869 void* castedOwner = reinterpret_cast<void*>(owner);
870 switch (state) {
871 case Window::kUp_InputState: {
872 fGesture.touchEnd(castedOwner);
873 break;
874 }
875 case Window::kDown_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400876 fGesture.touchBegin(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700877 break;
878 }
879 case Window::kMove_InputState: {
Brian Osman42bb6ac2017-06-05 08:46:04 -0400880 fGesture.touchMoved(castedOwner, x, y);
liyuqiand3cdbca2016-05-17 12:44:20 -0700881 break;
882 }
883 }
Brian Osmanb53f48c2017-06-07 10:00:30 -0400884 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone;
liyuqiand3cdbca2016-05-17 12:44:20 -0700885 fWindow->inval();
886 return true;
887}
888
Brian Osman80fc07e2017-12-08 16:45:43 -0500889bool Viewer::onMouse(int x, int y, Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -0500890 if (!fSlides[fCurrentSlide]->onMouse(x, y, state, modifiers)) {
891 if (GestureDevice::kTouch == fGestureDevice) {
892 return false;
Brian Osman80fc07e2017-12-08 16:45:43 -0500893 }
Brian Osmand67e5182017-12-08 16:46:09 -0500894 switch (state) {
895 case Window::kUp_InputState: {
896 fGesture.touchEnd(nullptr);
897 break;
898 }
899 case Window::kDown_InputState: {
900 fGesture.touchBegin(nullptr, x, y);
901 break;
902 }
903 case Window::kMove_InputState: {
904 fGesture.touchMoved(nullptr, x, y);
905 break;
906 }
907 }
908 fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone;
Brian Osman80fc07e2017-12-08 16:45:43 -0500909 }
Brian Osmand67e5182017-12-08 16:46:09 -0500910 fWindow->inval();
Jim Van Verthe7705782017-05-04 14:00:59 -0400911 return true;
912}
913
jvanverth34524262016-05-04 13:49:13 -0700914void Viewer::drawStats(SkCanvas* canvas) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700915 static const float kPixelPerMS = 2.0f;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500916 static const int kDisplayWidth = 192;
917 static const int kGraphHeight = 100;
918 static const int kTextHeight = 60;
919 static const int kDisplayHeight = kGraphHeight + kTextHeight;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700920 static const int kDisplayPadding = 10;
921 static const int kGraphPadding = 3;
922 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
923
Mike Reed3661bc92017-02-22 13:21:42 -0500924 SkISize canvasSize = canvas->getBaseLayerSize();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700925 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
926 SkIntToScalar(kDisplayPadding),
927 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
928 SkPaint paint;
929 canvas->save();
930
jvanverth3d6ed3a2016-04-07 11:09:51 -0700931 paint.setColor(SK_ColorBLACK);
932 canvas->drawRect(rect, paint);
933 // draw the 16ms line
934 paint.setColor(SK_ColorLTGRAY);
935 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
936 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
937 paint.setColor(SK_ColorRED);
938 paint.setStyle(SkPaint::kStroke_Style);
939 canvas->drawRect(rect, paint);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500940 paint.setStyle(SkPaint::kFill_Style);
jvanverth3d6ed3a2016-04-07 11:09:51 -0700941
942 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500943 const int xStep = 3;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700944 int i = fCurrentMeasurement;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500945 double ms = 0;
946 double animateMS = 0;
947 double paintMS = 0;
948 double flushMS = 0;
949 int count = 0;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700950 do {
Brian Osman1df161a2017-02-09 12:10:20 -0500951 // Round to nearest values
952 int animateHeight = (int)(fAnimateTimes[i] * kPixelPerMS + 0.5);
953 int paintHeight = (int)(fPaintTimes[i] * kPixelPerMS + 0.5);
954 int flushHeight = (int)(fFlushTimes[i] * kPixelPerMS + 0.5);
955 int startY = SkScalarTruncToInt(rect.fBottom);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500956 int endY = SkTMax(startY - flushHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -0500957 paint.setColor(SK_ColorRED);
958 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
959 SkIntToScalar(x), SkIntToScalar(endY), paint);
960 startY = endY;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500961 endY = SkTMax(startY - paintHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -0500962 paint.setColor(SK_ColorGREEN);
963 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
964 SkIntToScalar(x), SkIntToScalar(endY), paint);
965 startY = endY;
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500966 endY = SkTMax(startY - animateHeight, kDisplayPadding + kTextHeight);
Brian Osman1df161a2017-02-09 12:10:20 -0500967 paint.setColor(SK_ColorMAGENTA);
jvanverth3d6ed3a2016-04-07 11:09:51 -0700968 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
969 SkIntToScalar(x), SkIntToScalar(endY), paint);
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500970
971 double inc = fAnimateTimes[i] + fPaintTimes[i] + fFlushTimes[i];
972 if (inc > 0) {
973 ms += inc;
974 animateMS += fAnimateTimes[i];
975 paintMS += fPaintTimes[i];
976 flushMS += fFlushTimes[i];
977 ++count;
978 }
979
jvanverth3d6ed3a2016-04-07 11:09:51 -0700980 i++;
981 i &= (kMeasurementCount - 1); // fast mod
982 x += xStep;
983 } while (i != fCurrentMeasurement);
jvanverth9f372462016-04-06 06:08:59 -0700984
Jim Van Verth98c5b8b2017-11-09 15:43:46 -0500985 paint.setTextSize(16);
986 SkString mainString;
987 mainString.appendf("%4.3f ms -> %4.3f ms", ms / SkTMax(1, count),
988 fCumulativeMeasurementTime / SkTMax(1, fCumulativeMeasurementCount));
989 paint.setColor(SK_ColorWHITE);
990 canvas->drawString(mainString.c_str(), rect.fLeft+3, rect.fTop + 14, paint);
991
992 SkString animateString;
993 animateString.appendf("Animate: %4.3f ms", animateMS / SkTMax(1, count));
994 paint.setColor(0xffff66ff); // pure magenta is hard to read
995 canvas->drawString(animateString.c_str(), rect.fLeft+3, rect.fTop + 28, paint);
996
997 SkString paintString;
998 paintString.appendf("Paint: %4.3f ms", paintMS / SkTMax(1, count));
999 paint.setColor(SK_ColorGREEN);
1000 canvas->drawString(paintString.c_str(), rect.fLeft+3, rect.fTop + 42, paint);
1001
1002 SkString flushString;
1003 flushString.appendf("Flush: %4.3f ms", flushMS / SkTMax(1, count));
1004 paint.setColor(0xffff6666); // pure red is hard to read
1005 canvas->drawString(flushString.c_str(), rect.fLeft+3, rect.fTop + 56, paint);
1006
jvanverth9f372462016-04-06 06:08:59 -07001007 canvas->restore();
1008}
1009
Brian Osmana109e392017-02-24 09:49:14 -05001010static ImVec2 ImGui_DragPrimary(const char* label, float* x, float* y,
1011 const ImVec2& pos, const ImVec2& size) {
1012 // Transform primaries ([0, 0] - [0.8, 0.9]) to screen coords (including Y-flip)
1013 ImVec2 center(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1014
1015 // Invisible 10x10 button
1016 ImGui::SetCursorScreenPos(ImVec2(center.x - 5, center.y - 5));
1017 ImGui::InvisibleButton(label, ImVec2(10, 10));
1018
1019 if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) {
1020 ImGuiIO& io = ImGui::GetIO();
1021 // Normalized mouse position, relative to our gamut box
1022 ImVec2 mousePosXY((io.MousePos.x - pos.x) / size.x, (io.MousePos.y - pos.y) / size.y);
1023 // Clamp to edge of box, convert back to primary scale
1024 *x = SkTPin(mousePosXY.x, 0.0f, 1.0f) * 0.8f;
1025 *y = SkTPin(1 - mousePosXY.y, 0.0f, 1.0f) * 0.9f;
1026 }
1027
1028 if (ImGui::IsItemHovered()) {
1029 ImGui::SetTooltip("x: %.3f\ny: %.3f", *x, *y);
1030 }
1031
1032 // Return screen coordinates for the caller. We could just return center here, but we'd have
1033 // one frame of lag during drag.
1034 return ImVec2(pos.x + (*x / 0.8f) * size.x, pos.y + (1.0f - (*y / 0.9f)) * size.y);
1035}
1036
1037static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) {
1038 ImDrawList* drawList = ImGui::GetWindowDrawList();
1039
1040 // The gamut image covers a (0.8 x 0.9) shaped region, so fit our image/canvas to the available
1041 // width, and scale the height to maintain aspect ratio.
1042 float canvasWidth = SkTMax(ImGui::GetContentRegionAvailWidth(), 50.0f);
1043 ImVec2 size = ImVec2(canvasWidth, canvasWidth * (0.9f / 0.8f));
1044 ImVec2 pos = ImGui::GetCursorScreenPos();
1045
1046 // Background image. Only draw a subset of the image, to avoid the regions less than zero.
1047 // Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area.
1048 // Magic numbers are pixel locations of the origin and upper-right corner.
1049 drawList->AddImage(gamutPaint, pos, ImVec2(pos.x + size.x, pos.y + size.y),
1050 ImVec2(242, 61), ImVec2(1897, 1922));
1051 ImVec2 endPos = ImGui::GetCursorPos();
1052
1053 // Primary markers
1054 ImVec2 r = ImGui_DragPrimary("R", &primaries->fRX, &primaries->fRY, pos, size);
1055 ImVec2 g = ImGui_DragPrimary("G", &primaries->fGX, &primaries->fGY, pos, size);
1056 ImVec2 b = ImGui_DragPrimary("B", &primaries->fBX, &primaries->fBY, pos, size);
1057 ImVec2 w = ImGui_DragPrimary("W", &primaries->fWX, &primaries->fWY, pos, size);
1058
1059 // Gamut triangle
1060 drawList->AddCircle(r, 5.0f, 0xFF000040);
1061 drawList->AddCircle(g, 5.0f, 0xFF004000);
1062 drawList->AddCircle(b, 5.0f, 0xFF400000);
1063 drawList->AddCircle(w, 5.0f, 0xFFFFFFFF);
1064 drawList->AddTriangle(r, g, b, 0xFFFFFFFF);
1065
1066 // Re-position cursor immediate after the diagram for subsequent controls
1067 ImGui::SetCursorPos(endPos);
1068}
1069
Brian Osmand67e5182017-12-08 16:46:09 -05001070void Viewer::drawImGui() {
Brian Osman79086b92017-02-10 13:36:16 -05001071 // Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible
1072 if (fShowImGuiTestWindow) {
1073 ImGui::ShowTestWindow(&fShowImGuiTestWindow);
1074 }
1075
1076 if (fShowImGuiDebugWindow) {
Brian Osmana109e392017-02-24 09:49:14 -05001077 // We have some dynamic content that sizes to fill available size. If the scroll bar isn't
1078 // always visible, we can end up in a layout feedback loop.
1079 ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiSetCond_FirstUseEver);
Brian Salomon99a33902017-03-07 15:16:34 -05001080 DisplayParams params = fWindow->getRequestedDisplayParams();
1081 bool paramsChanged = false;
Brian Osmana109e392017-02-24 09:49:14 -05001082 if (ImGui::Begin("Tools", &fShowImGuiDebugWindow,
1083 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
Brian Osman621491e2017-02-28 15:45:01 -05001084 if (ImGui::CollapsingHeader("Backend")) {
1085 int newBackend = static_cast<int>(fBackendType);
1086 ImGui::RadioButton("Raster", &newBackend, sk_app::Window::kRaster_BackendType);
1087 ImGui::SameLine();
1088 ImGui::RadioButton("OpenGL", &newBackend, sk_app::Window::kNativeGL_BackendType);
Brian Salomon194db172017-08-17 14:37:06 -04001089#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
1090 ImGui::SameLine();
1091 ImGui::RadioButton("ANGLE", &newBackend, sk_app::Window::kANGLE_BackendType);
1092#endif
Brian Osman621491e2017-02-28 15:45:01 -05001093#if defined(SK_VULKAN)
1094 ImGui::SameLine();
1095 ImGui::RadioButton("Vulkan", &newBackend, sk_app::Window::kVulkan_BackendType);
1096#endif
1097 if (newBackend != fBackendType) {
1098 fDeferredActions.push_back([=]() {
1099 this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend));
1100 });
1101 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001102
Brian Salomon99a33902017-03-07 15:16:34 -05001103 const GrContext* ctx = fWindow->getGrContext();
Jim Van Verthfbdc0802017-05-02 16:15:53 -04001104 bool* wire = &params.fGrContextOptions.fWireframeMode;
1105 if (ctx && ImGui::Checkbox("Wireframe Mode", wire)) {
1106 paramsChanged = true;
1107 }
Brian Salomon99a33902017-03-07 15:16:34 -05001108
Brian Osman28b12522017-03-08 17:10:24 -05001109 if (ctx) {
1110 int sampleCount = fWindow->sampleCount();
1111 ImGui::Text("MSAA: "); ImGui::SameLine();
1112 ImGui::RadioButton("0", &sampleCount, 0); ImGui::SameLine();
1113 ImGui::RadioButton("4", &sampleCount, 4); ImGui::SameLine();
1114 ImGui::RadioButton("8", &sampleCount, 8); ImGui::SameLine();
1115 ImGui::RadioButton("16", &sampleCount, 16);
1116
1117 if (sampleCount != params.fMSAASampleCount) {
1118 params.fMSAASampleCount = sampleCount;
1119 paramsChanged = true;
1120 }
1121 }
1122
Brian Osman8a9de3d2017-03-01 14:59:05 -05001123 if (ImGui::TreeNode("Path Renderers")) {
Brian Osman8a9de3d2017-03-01 14:59:05 -05001124 GpuPathRenderers prevPr = params.fGrContextOptions.fGpuPathRenderers;
Brian Osman8a9de3d2017-03-01 14:59:05 -05001125 auto prButton = [&](GpuPathRenderers x) {
1126 if (ImGui::RadioButton(gPathRendererNames[x].c_str(), prevPr == x)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001127 if (x != params.fGrContextOptions.fGpuPathRenderers) {
1128 params.fGrContextOptions.fGpuPathRenderers = x;
1129 paramsChanged = true;
1130 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001131 }
1132 };
1133
1134 if (!ctx) {
1135 ImGui::RadioButton("Software", true);
1136 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001137 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001138 prButton(GpuPathRenderers::kAll);
1139 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1140 prButton(GpuPathRenderers::kStencilAndCover);
1141 }
1142 if (ctx->caps()->sampleShadingSupport()) {
1143 prButton(GpuPathRenderers::kMSAA);
1144 }
1145 prButton(GpuPathRenderers::kTessellating);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001146 prButton(GpuPathRenderers::kNone);
1147 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001148 prButton(GpuPathRenderers::kDefault);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001149 prButton(GpuPathRenderers::kAll);
Chris Dalton1a325d22017-07-14 15:17:41 -06001150 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1151 prButton(GpuPathRenderers::kCoverageCounting);
1152 }
Jim Van Verth83010462017-03-16 08:45:39 -04001153 prButton(GpuPathRenderers::kSmall);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001154 prButton(GpuPathRenderers::kTessellating);
1155 prButton(GpuPathRenderers::kNone);
1156 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001157 ImGui::TreePop();
1158 }
Brian Osman621491e2017-02-28 15:45:01 -05001159 }
1160
Brian Osmanfce09c52017-11-14 15:32:20 -05001161 if (fShowSlidePicker) {
1162 ImGui::SetNextTreeNodeOpen(true);
1163 }
1164
Brian Osman79086b92017-02-10 13:36:16 -05001165 if (ImGui::CollapsingHeader("Slide")) {
1166 static ImGuiTextFilter filter;
Brian Osmanf479e422017-11-08 13:11:36 -05001167 static ImVector<const char*> filteredSlideNames;
1168 static ImVector<int> filteredSlideIndices;
1169
Brian Osmanfce09c52017-11-14 15:32:20 -05001170 if (fShowSlidePicker) {
1171 ImGui::SetKeyboardFocusHere();
1172 fShowSlidePicker = false;
1173 }
1174
Brian Osman79086b92017-02-10 13:36:16 -05001175 filter.Draw();
Brian Osmanf479e422017-11-08 13:11:36 -05001176 filteredSlideNames.clear();
1177 filteredSlideIndices.clear();
1178 int filteredIndex = 0;
1179 for (int i = 0; i < fSlides.count(); ++i) {
1180 const char* slideName = fSlides[i]->getName().c_str();
1181 if (filter.PassFilter(slideName) || i == fCurrentSlide) {
1182 if (i == fCurrentSlide) {
1183 filteredIndex = filteredSlideIndices.size();
Brian Osman79086b92017-02-10 13:36:16 -05001184 }
Brian Osmanf479e422017-11-08 13:11:36 -05001185 filteredSlideNames.push_back(slideName);
1186 filteredSlideIndices.push_back(i);
Brian Osman79086b92017-02-10 13:36:16 -05001187 }
Brian Osman79086b92017-02-10 13:36:16 -05001188 }
Brian Osmanf479e422017-11-08 13:11:36 -05001189
1190 int previousSlide = fCurrentSlide;
1191 if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(),
1192 filteredSlideNames.size(), 20)) {
1193 fCurrentSlide = filteredSlideIndices[filteredIndex];
1194 setupCurrentSlide(previousSlide);
Brian Osman79086b92017-02-10 13:36:16 -05001195 }
1196 }
Brian Osmana109e392017-02-24 09:49:14 -05001197
1198 if (ImGui::CollapsingHeader("Color Mode")) {
Brian Osman92004802017-03-06 11:47:26 -05001199 ColorMode newMode = fColorMode;
1200 auto cmButton = [&](ColorMode mode, const char* label) {
1201 if (ImGui::RadioButton(label, mode == fColorMode)) {
1202 newMode = mode;
1203 }
1204 };
1205
1206 cmButton(ColorMode::kLegacy, "Legacy 8888");
1207 cmButton(ColorMode::kColorManagedSRGB8888_NonLinearBlending,
1208 "Color Managed 8888 (Nonlinear blending)");
1209 cmButton(ColorMode::kColorManagedSRGB8888, "Color Managed 8888");
1210 cmButton(ColorMode::kColorManagedLinearF16, "Color Managed F16");
1211
1212 if (newMode != fColorMode) {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001213 // It isn't safe to switch color mode now (in the middle of painting). We might
1214 // tear down the back-end, etc... Defer this change until the next onIdle.
1215 fDeferredActions.push_back([=]() {
Brian Osman92004802017-03-06 11:47:26 -05001216 this->setColorMode(newMode);
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001217 });
Brian Osmana109e392017-02-24 09:49:14 -05001218 }
1219
1220 // Pick from common gamuts:
1221 int primariesIdx = 4; // Default: Custom
1222 for (size_t i = 0; i < SK_ARRAY_COUNT(gNamedPrimaries); ++i) {
1223 if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) {
1224 primariesIdx = i;
1225 break;
1226 }
1227 }
1228
Brian Osmanfdab5762017-11-09 10:27:55 -05001229 // When we're in xform canvas mode, we can alter the transfer function, too
1230 if (ColorMode::kColorManagedSRGB8888_NonLinearBlending == fColorMode) {
1231 ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.fG, 0.5f, 3.5f);
1232 }
1233
Brian Osmana109e392017-02-24 09:49:14 -05001234 if (ImGui::Combo("Primaries", &primariesIdx,
1235 "sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) {
1236 if (primariesIdx >= 0 && primariesIdx <= 3) {
1237 fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries;
1238 }
1239 }
1240
1241 // Allow direct editing of gamut
1242 ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint);
1243 }
Brian Osman79086b92017-02-10 13:36:16 -05001244 }
Brian Salomon99a33902017-03-07 15:16:34 -05001245 if (paramsChanged) {
1246 fDeferredActions.push_back([=]() {
1247 fWindow->setRequestedDisplayParams(params);
1248 fWindow->inval();
1249 this->updateTitle();
1250 });
1251 }
Brian Osman79086b92017-02-10 13:36:16 -05001252 ImGui::End();
1253 }
1254
Brian Osmanf6877092017-02-13 09:39:57 -05001255 if (fShowZoomWindow && fLastImage) {
1256 if (ImGui::Begin("Zoom", &fShowZoomWindow, ImVec2(200, 200))) {
Brian Osmanead517d2017-11-13 15:36:36 -05001257 static int zoomFactor = 8;
1258 if (ImGui::Button("<<")) {
1259 zoomFactor = SkTMax(zoomFactor / 2, 4);
1260 }
1261 ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine();
1262 if (ImGui::Button(">>")) {
1263 zoomFactor = SkTMin(zoomFactor * 2, 32);
1264 }
Brian Osmanf6877092017-02-13 09:39:57 -05001265
Brian Osmanf6877092017-02-13 09:39:57 -05001266 ImVec2 mousePos = ImGui::GetMousePos();
1267 ImVec2 avail = ImGui::GetContentRegionAvail();
1268
Brian Osmanead517d2017-11-13 15:36:36 -05001269 uint32_t pixel = 0;
1270 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1271 if (fLastImage->readPixels(info, &pixel, info.minRowBytes(), mousePos.x, mousePos.y)) {
1272 ImGui::SameLine();
Brian Osman07b56b22017-11-21 14:59:31 -05001273 ImGui::Text("(X, Y): %d, %d RGBA: %x %x %x %x",
1274 sk_float_round2int(mousePos.x), sk_float_round2int(mousePos.y),
1275 SkGetPackedR32(pixel), SkGetPackedG32(pixel),
Brian Osmanead517d2017-11-13 15:36:36 -05001276 SkGetPackedB32(pixel), SkGetPackedA32(pixel));
1277 }
1278
Brian Osmand67e5182017-12-08 16:46:09 -05001279 fImGuiLayer.skiaWidget(avail, [=](SkCanvas* c) {
Brian Osmanead517d2017-11-13 15:36:36 -05001280 // Translate so the region of the image that's under the mouse cursor is centered
1281 // in the zoom canvas:
1282 c->scale(zoomFactor, zoomFactor);
1283 c->translate(avail.x * 0.5f / zoomFactor - mousePos.x - 0.5f,
1284 avail.y * 0.5f / zoomFactor - mousePos.y - 0.5f);
1285 c->drawImage(this->fLastImage, 0, 0);
1286
1287 SkPaint outline;
1288 outline.setStyle(SkPaint::kStroke_Style);
1289 c->drawRect(SkRect::MakeXYWH(mousePos.x, mousePos.y, 1, 1), outline);
1290 });
Brian Osmanf6877092017-02-13 09:39:57 -05001291 }
1292
1293 ImGui::End();
1294 }
Brian Osman79086b92017-02-10 13:36:16 -05001295}
1296
liyuqian2edb0f42016-07-06 14:11:32 -07001297void Viewer::onIdle() {
Brian Osmanfd8f4d52017-02-24 11:57:23 -05001298 for (int i = 0; i < fDeferredActions.count(); ++i) {
1299 fDeferredActions[i]();
1300 }
1301 fDeferredActions.reset();
1302
Brian Osman1df161a2017-02-09 12:10:20 -05001303 double startTime = SkTime::GetMSecs();
jvanverthc265a922016-04-08 12:51:45 -07001304 fAnimTimer.updateTime();
Brian Osman1df161a2017-02-09 12:10:20 -05001305 bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer);
1306 fAnimateTimes[fCurrentMeasurement] = SkTime::GetMSecs() - startTime;
1307
Brian Osman79086b92017-02-10 13:36:16 -05001308 ImGuiIO& io = ImGui::GetIO();
1309 if (animateWantsInval || fDisplayStats || fRefresh || io.MetricsActiveWindows) {
jvanverthc265a922016-04-08 12:51:45 -07001310 fWindow->inval();
1311 }
jvanverth9f372462016-04-06 06:08:59 -07001312}
liyuqiane5a6cd92016-05-27 08:52:52 -07001313
1314void Viewer::updateUIState() {
csmartdalton578f0642017-02-24 16:04:47 -07001315 if (!fWindow) {
1316 return;
1317 }
1318 if (fWindow->sampleCount() < 0) {
1319 return; // Surface hasn't been created yet.
1320 }
1321
liyuqianb73c24b2016-06-03 08:47:23 -07001322 // Slide state
liyuqiane5a6cd92016-05-27 08:52:52 -07001323 Json::Value slideState(Json::objectValue);
1324 slideState[kName] = kSlideStateName;
1325 slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001326 if (fAllSlideNames.size() == 0) {
1327 for(auto slide : fSlides) {
1328 fAllSlideNames.append(Json::Value(slide->getName().c_str()));
1329 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001330 }
liyuqian1f508fd2016-06-07 06:57:40 -07001331 slideState[kOptions] = fAllSlideNames;
liyuqiane5a6cd92016-05-27 08:52:52 -07001332
liyuqianb73c24b2016-06-03 08:47:23 -07001333 // Backend state
liyuqiane5a6cd92016-05-27 08:52:52 -07001334 Json::Value backendState(Json::objectValue);
1335 backendState[kName] = kBackendStateName;
liyuqian6cb70252016-06-02 12:16:25 -07001336 backendState[kValue] = kBackendTypeStrings[fBackendType];
liyuqiane5a6cd92016-05-27 08:52:52 -07001337 backendState[kOptions] = Json::Value(Json::arrayValue);
liyuqianb73c24b2016-06-03 08:47:23 -07001338 for (auto str : kBackendTypeStrings) {
liyuqian6cb70252016-06-02 12:16:25 -07001339 backendState[kOptions].append(Json::Value(str));
1340 }
liyuqiane5a6cd92016-05-27 08:52:52 -07001341
csmartdalton578f0642017-02-24 16:04:47 -07001342 // MSAA state
1343 Json::Value msaaState(Json::objectValue);
1344 msaaState[kName] = kMSAAStateName;
1345 msaaState[kValue] = fWindow->sampleCount();
1346 msaaState[kOptions] = Json::Value(Json::arrayValue);
1347 if (sk_app::Window::kRaster_BackendType == fBackendType) {
1348 msaaState[kOptions].append(Json::Value(0));
1349 } else {
1350 for (int msaa : {0, 4, 8, 16}) {
1351 msaaState[kOptions].append(Json::Value(msaa));
1352 }
1353 }
1354
csmartdalton61cd31a2017-02-27 17:00:53 -07001355 // Path renderer state
1356 GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers;
1357 Json::Value prState(Json::objectValue);
1358 prState[kName] = kPathRendererStateName;
1359 prState[kValue] = gPathRendererNames[pr];
1360 prState[kOptions] = Json::Value(Json::arrayValue);
1361 const GrContext* ctx = fWindow->getGrContext();
1362 if (!ctx) {
1363 prState[kOptions].append("Software");
1364 } else if (fWindow->sampleCount()) {
Brian Osman8b0f2652017-08-29 15:18:34 -04001365 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001366 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
1367 if (ctx->caps()->shaderCaps()->pathRenderingSupport()) {
1368 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kStencilAndCover]);
1369 }
1370 if (ctx->caps()->sampleShadingSupport()) {
1371 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kMSAA]);
1372 }
Brian Osman8a9de3d2017-03-01 14:59:05 -05001373 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001374 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1375 } else {
Brian Osman8b0f2652017-08-29 15:18:34 -04001376 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kDefault]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001377 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kAll]);
Chris Dalton1a325d22017-07-14 15:17:41 -06001378 if (GrCoverageCountingPathRenderer::IsSupported(*ctx->caps())) {
1379 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kCoverageCounting]);
1380 }
Jim Van Verth83010462017-03-16 08:45:39 -04001381 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kSmall]);
Brian Osman8a9de3d2017-03-01 14:59:05 -05001382 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kTessellating]);
csmartdalton61cd31a2017-02-27 17:00:53 -07001383 prState[kOptions].append(gPathRendererNames[GpuPathRenderers::kNone]);
1384 }
1385
liyuqianb73c24b2016-06-03 08:47:23 -07001386 // Softkey state
1387 Json::Value softkeyState(Json::objectValue);
1388 softkeyState[kName] = kSoftkeyStateName;
1389 softkeyState[kValue] = kSoftkeyHint;
1390 softkeyState[kOptions] = Json::Value(Json::arrayValue);
1391 softkeyState[kOptions].append(kSoftkeyHint);
1392 for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) {
1393 softkeyState[kOptions].append(Json::Value(softkey.c_str()));
1394 }
1395
liyuqian1f508fd2016-06-07 06:57:40 -07001396 // FPS state
1397 Json::Value fpsState(Json::objectValue);
1398 fpsState[kName] = kFpsStateName;
Brian Osman1df161a2017-02-09 12:10:20 -05001399 int idx = (fCurrentMeasurement + (kMeasurementCount - 1)) & (kMeasurementCount - 1);
1400 fpsState[kValue] = SkStringPrintf("%8.3lf ms\n\nA %8.3lf\nP %8.3lf\nF%8.3lf",
1401 fAnimateTimes[idx] + fPaintTimes[idx] + fFlushTimes[idx],
1402 fAnimateTimes[idx],
1403 fPaintTimes[idx],
1404 fFlushTimes[idx]).c_str();
liyuqian1f508fd2016-06-07 06:57:40 -07001405 fpsState[kOptions] = Json::Value(Json::arrayValue);
1406
liyuqiane5a6cd92016-05-27 08:52:52 -07001407 Json::Value state(Json::arrayValue);
1408 state.append(slideState);
1409 state.append(backendState);
csmartdalton578f0642017-02-24 16:04:47 -07001410 state.append(msaaState);
csmartdalton61cd31a2017-02-27 17:00:53 -07001411 state.append(prState);
liyuqianb73c24b2016-06-03 08:47:23 -07001412 state.append(softkeyState);
liyuqian1f508fd2016-06-07 06:57:40 -07001413 state.append(fpsState);
liyuqiane5a6cd92016-05-27 08:52:52 -07001414
Brian Osmaneff04b52017-11-21 13:18:02 -05001415 fWindow->setUIState(state.toStyledString().c_str());
liyuqiane5a6cd92016-05-27 08:52:52 -07001416}
1417
1418void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
liyuqian6cb70252016-06-02 12:16:25 -07001419 // For those who will add more features to handle the state change in this function:
1420 // After the change, please call updateUIState no notify the frontend (e.g., Android app).
1421 // For example, after slide change, updateUIState is called inside setupCurrentSlide;
1422 // after backend change, updateUIState is called in this function.
liyuqiane5a6cd92016-05-27 08:52:52 -07001423 if (stateName.equals(kSlideStateName)) {
1424 int previousSlide = fCurrentSlide;
1425 fCurrentSlide = 0;
1426 for(auto slide : fSlides) {
1427 if (slide->getName().equals(stateValue)) {
Brian Salomon99a33902017-03-07 15:16:34 -05001428 this->setupCurrentSlide(previousSlide);
liyuqiane5a6cd92016-05-27 08:52:52 -07001429 break;
1430 }
1431 fCurrentSlide++;
1432 }
1433 if (fCurrentSlide >= fSlides.count()) {
1434 fCurrentSlide = previousSlide;
1435 SkDebugf("Slide not found: %s", stateValue.c_str());
1436 }
liyuqian6cb70252016-06-02 12:16:25 -07001437 } else if (stateName.equals(kBackendStateName)) {
1438 for (int i = 0; i < sk_app::Window::kBackendTypeCount; i++) {
1439 if (stateValue.equals(kBackendTypeStrings[i])) {
1440 if (fBackendType != i) {
1441 fBackendType = (sk_app::Window::BackendType)i;
1442 fWindow->detach();
Brian Osman70d2f432017-11-08 09:54:10 -05001443 fWindow->attach(backend_type_for_window(fBackendType));
liyuqian6cb70252016-06-02 12:16:25 -07001444 }
1445 break;
1446 }
1447 }
csmartdalton578f0642017-02-24 16:04:47 -07001448 } else if (stateName.equals(kMSAAStateName)) {
1449 DisplayParams params = fWindow->getRequestedDisplayParams();
1450 int sampleCount = atoi(stateValue.c_str());
1451 if (sampleCount != params.fMSAASampleCount) {
1452 params.fMSAASampleCount = sampleCount;
1453 fWindow->setRequestedDisplayParams(params);
1454 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001455 this->updateTitle();
1456 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001457 }
1458 } else if (stateName.equals(kPathRendererStateName)) {
1459 DisplayParams params = fWindow->getRequestedDisplayParams();
1460 for (const auto& pair : gPathRendererNames) {
1461 if (pair.second == stateValue.c_str()) {
1462 if (params.fGrContextOptions.fGpuPathRenderers != pair.first) {
1463 params.fGrContextOptions.fGpuPathRenderers = pair.first;
1464 fWindow->setRequestedDisplayParams(params);
1465 fWindow->inval();
Brian Salomon99a33902017-03-07 15:16:34 -05001466 this->updateTitle();
1467 this->updateUIState();
csmartdalton61cd31a2017-02-27 17:00:53 -07001468 }
1469 break;
1470 }
csmartdalton578f0642017-02-24 16:04:47 -07001471 }
liyuqianb73c24b2016-06-03 08:47:23 -07001472 } else if (stateName.equals(kSoftkeyStateName)) {
1473 if (!stateValue.equals(kSoftkeyHint)) {
1474 fCommands.onSoftkey(stateValue);
Brian Salomon99a33902017-03-07 15:16:34 -05001475 this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint
liyuqianb73c24b2016-06-03 08:47:23 -07001476 }
liyuqian2edb0f42016-07-06 14:11:32 -07001477 } else if (stateName.equals(kRefreshStateName)) {
1478 // This state is actually NOT in the UI state.
1479 // We use this to allow Android to quickly set bool fRefresh.
1480 fRefresh = stateValue.equals(kON);
liyuqiane5a6cd92016-05-27 08:52:52 -07001481 } else {
1482 SkDebugf("Unknown stateName: %s", stateName.c_str());
1483 }
1484}
Brian Osman79086b92017-02-10 13:36:16 -05001485
1486bool Viewer::onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001487 return fCommands.onKey(key, state, modifiers);
Brian Osman79086b92017-02-10 13:36:16 -05001488}
1489
1490bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osmand67e5182017-12-08 16:46:09 -05001491 if (fSlides[fCurrentSlide]->onChar(c)) {
Jim Van Verth6f449692017-02-14 15:16:46 -05001492 fWindow->inval();
1493 return true;
Brian Osman80fc07e2017-12-08 16:45:43 -05001494 } else {
1495 return fCommands.onChar(c, modifiers);
Jim Van Verth6f449692017-02-14 15:16:46 -05001496 }
Brian Osman79086b92017-02-10 13:36:16 -05001497}