| 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 |
|
| 8 |
#include "tools/viewer/Viewer.h" |
| 9 |
|
| 10 |
#include "bench/GpuTools.h" |
| 11 |
#include "gm/gm.h" |
| 12 |
#include "include/codec/SkCodec.h" |
| 13 |
#include "include/core/SkAlphaType.h" |
| 14 |
#include "include/core/SkBitmap.h" |
| 15 |
#include "include/core/SkBlendMode.h" |
| 16 |
#include "include/core/SkCanvas.h" |
| 17 |
#include "include/core/SkColor.h" |
| 18 |
#include "include/core/SkColorType.h" |
| 19 |
#include "include/core/SkData.h" |
| 20 |
#include "include/core/SkFontTypes.h" |
| 21 |
#include "include/core/SkGraphics.h" |
| 22 |
#include "include/core/SkImage.h" |
| 23 |
#include "include/core/SkImageInfo.h" |
| 24 |
#include "include/core/SkPicture.h" |
| 25 |
#include "include/core/SkPictureRecorder.h" |
| 26 |
#include "include/core/SkRect.h" |
| 27 |
#include "include/core/SkSamplingOptions.h" |
| 28 |
#include "include/core/SkSerialProcs.h" |
| 29 |
#include "include/core/SkStream.h" |
| 30 |
#include "include/core/SkSurface.h" |
| 31 |
#include "include/core/SkSurfaceProps.h" |
| 32 |
#include "include/core/SkTextBlob.h" |
| 33 |
#include "include/private/base/SkDebug.h" |
| 34 |
#include "include/private/base/SkTPin.h" |
| 35 |
#include "include/private/base/SkTo.h" |
| 36 |
#include "include/utils/SkPaintFilterCanvas.h" |
| 37 |
#include "src/base/SkBase64.h" |
| 38 |
#include "src/base/SkTLazy.h" |
| 39 |
#include "src/base/SkTSort.h" |
| 40 |
#include "src/base/SkUTF.h" |
| 41 |
#include "src/core/SkAutoPixmapStorage.h" |
| 42 |
#include "src/core/SkColorPriv.h" |
| 43 |
#include "src/core/SkLRUCache.h" |
| 44 |
#include "src/core/SkMD5.h" |
| 45 |
#include "src/core/SkOSFile.h" |
| 46 |
#include "src/core/SkReadBuffer.h" |
| 47 |
#include "src/core/SkScan.h" |
| 48 |
#include "src/core/SkStringUtils.h" |
| 49 |
#include "src/core/SkTaskGroup.h" |
| 50 |
#include "src/core/SkTextBlobPriv.h" |
| 51 |
#include "src/image/SkImage_Base.h" |
| 52 |
#include "src/sksl/SkSLCompiler.h" |
| 53 |
#include "src/sksl/SkSLString.h" |
| 54 |
#include "src/text/GlyphRun.h" |
| 55 |
#include "src/utils/SkJSONWriter.h" |
| 56 |
#include "src/utils/SkOSPath.h" |
| 57 |
#include "src/utils/SkShaderUtils.h" |
| 58 |
#include "tools/CodecUtils.h" |
| 59 |
#include "tools/DecodeUtils.h" |
| 60 |
#include "tools/DeserialProcsUtils.h" |
| 61 |
#include "tools/Resources.h" |
| 62 |
#include "tools/RuntimeBlendUtils.h" |
| 63 |
#include "tools/SkMetaData.h" |
| 64 |
#include "tools/flags/CommandLineFlags.h" |
| 65 |
#include "tools/flags/CommonFlags.h" |
| 66 |
#include "tools/flags/CommonFlagsGanesh.h" |
| 67 |
#include "tools/flags/CommonFlagsGraphite.h" |
| 68 |
#include "tools/fonts/FontToolUtils.h" |
| 69 |
#include "tools/skui/InputState.h" |
| 70 |
#include "tools/skui/Key.h" |
| 71 |
#include "tools/skui/ModifierKey.h" |
| 72 |
#include "tools/trace/EventTracingPriv.h" |
| 73 |
#include "tools/viewer/AnimatedImageSlide.h" |
| 74 |
#include "tools/viewer/BisectSlide.h" |
| 75 |
#include "tools/viewer/CaptureSlide.h" |
| 76 |
#include "tools/viewer/GMSlide.h" |
| 77 |
#include "tools/viewer/ImageSlide.h" |
| 78 |
#include "tools/viewer/MSKPSlide.h" |
| 79 |
#include "tools/viewer/SKPSlide.h" |
| 80 |
#include "tools/viewer/SkSLDebuggerSlide.h" |
| 81 |
#include "tools/viewer/SkSLSlide.h" |
| 82 |
#include "tools/viewer/Slide.h" |
| 83 |
#include "tools/viewer/SlideDir.h" |
| 84 |
#include "tools/window/DisplayParams.h" |
| 85 |
|
| 86 |
#include <algorithm> |
| 87 |
#include <cfloat> |
| 88 |
#include <chrono> |
| 89 |
#include <cmath> |
| 90 |
#include <cstdint> |
| 91 |
#include <cstdio> |
| 92 |
#include <cstdlib> |
| 93 |
#include <cstring> |
| 94 |
#include <initializer_list> |
| 95 |
#include <map> |
| 96 |
#include <memory> |
| 97 |
#include <optional> |
| 98 |
#include <ratio> |
| 99 |
#include <regex> |
| 100 |
#include <tuple> |
| 101 |
#include <utility> |
| 102 |
#include <vector> |
| 103 |
|
| 104 |
#if defined(SK_GANESH) |
| 105 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 106 |
#include "src/gpu/ganesh/GrCaps.h" |
| 107 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 108 |
#include "src/gpu/ganesh/GrGpu.h" |
| 109 |
#include "src/gpu/ganesh/GrPersistentCacheUtils.h" |
| 110 |
#include "src/gpu/ganesh/GrShaderCaps.h" |
| 111 |
#include "src/gpu/ganesh/ops/AtlasPathRenderer.h" |
| 112 |
#include "src/gpu/ganesh/ops/TessellationPathRenderer.h" |
| 113 |
#endif |
| 114 |
|
| 115 |
#if defined(SK_GRAPHITE) |
| 116 |
#include "include/gpu/graphite/Context.h" |
| 117 |
#include "include/gpu/graphite/Recorder.h" |
| 118 |
#include "src/gpu/graphite/ContextPriv.h" |
| 119 |
#include "src/gpu/graphite/GlobalCache.h" |
| 120 |
#include "src/gpu/graphite/GraphicsPipeline.h" |
| 121 |
#include "src/gpu/graphite/RendererProvider.h" |
| 122 |
#include "tools/window/GraphiteDisplayParams.h" |
| 123 |
#endif |
| 124 |
|
| 125 |
#include "imgui.h" |
| 126 |
#include "misc/cpp/imgui_stdlib.h" // For ImGui support of std::string |
| 127 |
|
| 128 |
#if defined(SK_VULKAN) |
| 129 |
#include "spirv-tools/libspirv.hpp" |
| 130 |
#endif |
| 131 |
|
| 132 |
#if defined(SK_ENABLE_SKOTTIE) |
| 133 |
#include "tools/viewer/SkottieSlide.h" |
| 134 |
#endif |
| 135 |
|
| 136 |
#if defined(SK_ENABLE_SVG) |
| 137 |
#include "modules/svg/include/SkSVGOpenTypeSVGDecoder.h" |
| 138 |
#include "tools/viewer/SvgSlide.h" |
| 139 |
#endif |
| 140 |
|
| 141 |
#if defined(SK_CODEC_DECODES_PNG_WITH_RUST) |
| 142 |
#include "include/codec/SkPngRustDecoder.h" |
| 143 |
#else |
| 144 |
#include "include/codec/SkPngDecoder.h" |
| 145 |
#endif |
| 146 |
|
| 147 |
#if defined(SK_CODEC_ENCODES_PNG_WITH_RUST) |
| 148 |
#include "include/encode/SkPngRustEncoder.h" |
| 149 |
#else |
| 150 |
#include "include/encode/SkPngEncoder.h" |
| 151 |
#endif |
| 152 |
|
| 153 |
using namespace skia_private; |
| 154 |
using skwindow::DisplayParams; |
| 155 |
|
| 156 |
#if defined(SK_GANESH) |
| 157 |
class CapturingShaderErrorHandler : public GrContextOptions::ShaderErrorHandler { |
| 158 |
public: |
| 159 |
void compileError(const char* shader, const char* errors) override { |
| 160 |
fShaders.push_back(SkString(shader)); |
| 161 |
fErrors.push_back(SkString(errors)); |
| 162 |
} |
| 163 |
|
| 164 |
void reset() { |
| 165 |
fShaders.clear(); |
| 166 |
fErrors.clear(); |
| 167 |
} |
| 168 |
|
| 169 |
TArray<SkString> fShaders; |
| 170 |
TArray<SkString> fErrors; |
| 171 |
}; |
| 172 |
|
| 173 |
static CapturingShaderErrorHandler gShaderErrorHandler; |
| 174 |
|
| 175 |
GrContextOptions::ShaderErrorHandler* Viewer::ShaderErrorHandler() { return &gShaderErrorHandler; } |
| 176 |
|
| 177 |
static std::map<GpuPathRenderers, std::string> gGaneshPathRendererNames; |
| 178 |
#endif |
| 179 |
|
| 180 |
using namespace sk_app; |
| 181 |
using SkSL::Compiler; |
| 182 |
using OverrideFlag = SkSL::Compiler::OverrideFlag; |
| 183 |
|
| 184 |
|
| 185 |
Application* Application::Create(int argc, char** argv, void* platformData) { |
| 186 |
return new Viewer(argc, argv, platformData); |
| 187 |
} |
| 188 |
|
| 189 |
static DEFINE_string(slide, "", "Start on this sample."); |
| 190 |
static DEFINE_bool(list, false, "List samples?"); |
| 191 |
|
| 192 |
#ifdef SK_GL |
| 193 |
#define GL_BACKEND_STR ", \"gl\"" |
| 194 |
#else |
| 195 |
#define GL_BACKEND_STR |
| 196 |
#endif |
| 197 |
#ifdef SK_VULKAN |
| 198 |
#define VK_BACKEND_STR ", \"vk\"" |
| 199 |
#else |
| 200 |
#define VK_BACKEND_STR |
| 201 |
#endif |
| 202 |
#ifdef SK_METAL |
| 203 |
#define MTL_BACKEND_STR ", \"mtl\"" |
| 204 |
#else |
| 205 |
#define MTL_BACKEND_STR |
| 206 |
#endif |
| 207 |
#ifdef SK_DIRECT3D |
| 208 |
#define D3D_BACKEND_STR ", \"d3d\"" |
| 209 |
#else |
| 210 |
#define D3D_BACKEND_STR |
| 211 |
#endif |
| 212 |
#ifdef SK_DAWN |
| 213 |
#define DAWN_BACKEND_STR ", \"dawn\"" |
| 214 |
#else |
| 215 |
#define DAWN_BACKEND_STR |
| 216 |
#endif |
| 217 |
#define BACKENDS_STR_EVALUATOR(sw, gl, vk, mtl, d3d, dawn) sw gl vk mtl d3d dawn |
| 218 |
#define BACKENDS_STR BACKENDS_STR_EVALUATOR( \ |
| 219 |
"\"sw\"", GL_BACKEND_STR, VK_BACKEND_STR, MTL_BACKEND_STR, D3D_BACKEND_STR, DAWN_BACKEND_STR) |
| 220 |
|
| 221 |
static DEFINE_string2(backend, b, "sw", "Backend to use. Allowed values are " BACKENDS_STR "."); |
| 222 |
|
| 223 |
static DEFINE_int(msaa, 1, "Number of subpixel samples. 0 for no HW antialiasing."); |
| 224 |
static DEFINE_bool(dmsaa, false, "Use internal MSAA to render to non-MSAA surfaces?"); |
| 225 |
|
| 226 |
static DEFINE_string(bisect, "", "Path to a .skp or .svg file to bisect."); |
| 227 |
|
| 228 |
static DEFINE_string2(file, f, "", "Open a single file for viewing."); |
| 229 |
|
| 230 |
static DEFINE_string2(match, m, nullptr, |
| 231 |
"[~][^]substring[$] [...] of name to run.\n" |
| 232 |
"Multiple matches may be separated by spaces.\n" |
| 233 |
"~ causes a matching name to always be skipped\n" |
| 234 |
"^ requires the start of the name to match\n" |
| 235 |
"$ requires the end of the name to match\n" |
| 236 |
"^ and $ requires an exact match\n" |
| 237 |
"If a name does not match any list entry,\n" |
| 238 |
"it is skipped unless some list entry starts with ~"); |
| 239 |
|
| 240 |
#if defined(SK_GRAPHITE) |
| 241 |
#ifdef SK_ENABLE_VELLO_SHADERS |
| 242 |
#define COMPUTE_ANALYTIC_PATHSTRATEGY_STR ", \"compute-analytic\"" |
| 243 |
#define COMPUTE_MSAA16_PATHSTRATEGY_STR ", \"compute-msaa16\"" |
| 244 |
#define COMPUTE_MSAA8_PATHSTRATEGY_STR ", \"compute-msaa8\"" |
| 245 |
#else |
| 246 |
#define COMPUTE_ANALYTIC_PATHSTRATEGY_STR |
| 247 |
#define COMPUTE_MSAA16_PATHSTRATEGY_STR |
| 248 |
#define COMPUTE_MSAA8_PATHSTRATEGY_STR |
| 249 |
#endif |
| 250 |
#define PATHSTRATEGY_STR_EVALUATOR( \ |
| 251 |
default, raster, compute_analytic, compute_msaa16, compute_msaa8, tess) \ |
| 252 |
default raster compute_analytic compute_msaa16 tess |
| 253 |
#define PATHSTRATEGY_STR \ |
| 254 |
PATHSTRATEGY_STR_EVALUATOR("\"default\"", \ |
| 255 |
"\"raster\"", \ |
| 256 |
COMPUTE_ANALYTIC_PATHSTRATEGY_STR, \ |
| 257 |
COMPUTE_MSAA16_PATHSTRATEGY_STR, \ |
| 258 |
COMPUTE_MSAA8_PATHSTRATEGY_STR, \ |
| 259 |
"\"tessellation\"") |
| 260 |
|
| 261 |
static DEFINE_string(pathstrategy, "default", |
| 262 |
"Path renderer strategy to use. Allowed values are " PATHSTRATEGY_STR "."); |
| 263 |
#endif |
| 264 |
|
| 265 |
#if defined(SK_BUILD_FOR_ANDROID) |
| 266 |
# define PATH_PREFIX "/data/local/tmp/" |
| 267 |
#else |
| 268 |
# define PATH_PREFIX "" |
| 269 |
#endif |
| 270 |
|
| 271 |
static DEFINE_string(jpgs , PATH_PREFIX "jpgs" , "Directory to read jpgs from."); |
| 272 |
static DEFINE_string(jxls , PATH_PREFIX "jxls" , "Directory to read jxls from."); |
| 273 |
static DEFINE_string(skps , PATH_PREFIX "skps" , "Directory to read skps from."); |
| 274 |
static DEFINE_string(mskps , PATH_PREFIX "mskps" , "Directory to read mskps from."); |
| 275 |
static DEFINE_string(lotties, PATH_PREFIX "lotties", "Directory to read (Bodymovin) jsons from."); |
| 276 |
static DEFINE_string(captures, PATH_PREFIX "captures", "Directory to read SkCaptures from."); |
| 277 |
#undef PATH_PREFIX |
| 278 |
|
| 279 |
static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file."); |
| 280 |
|
| 281 |
static DEFINE_int_2(threads, j, -1, |
| 282 |
"Run threadsafe tests on a threadpool with this many extra threads, " |
| 283 |
"defaulting to one extra thread per core."); |
| 284 |
|
| 285 |
static DEFINE_bool(redraw, false, "Toggle continuous redraw."); |
| 286 |
|
| 287 |
static DEFINE_bool(offscreen, false, "Force rendering to an offscreen surface."); |
| 288 |
static DEFINE_bool(stats, false, "Display stats overlay on startup."); |
| 289 |
static DEFINE_bool(createProtected, false, "Create a protected native backend (e.g., in EGL)."); |
| 290 |
|
| 291 |
static bool is_graphite_backend_type(sk_app::Window::BackendType type) { |
| 292 |
#if defined(SK_GRAPHITE) |
| 293 |
switch (type) { |
| 294 |
#if defined(SK_DAWN) |
| 295 |
case sk_app::Window::BackendType::kGraphiteDawnD3D11: |
| 296 |
case sk_app::Window::BackendType::kGraphiteDawnD3D12: |
| 297 |
case sk_app::Window::BackendType::kGraphiteDawnMetal: |
| 298 |
case sk_app::Window::BackendType::kGraphiteDawnOpenGLES: |
| 299 |
case sk_app::Window::BackendType::kGraphiteDawnVulkan: |
| 300 |
#endif |
| 301 |
#if defined(SK_METAL) |
| 302 |
case sk_app::Window::BackendType::kGraphiteMetal: |
| 303 |
#endif |
| 304 |
#if defined(SK_VULKAN) |
| 305 |
case sk_app::Window::BackendType::kGraphiteVulkan: |
| 306 |
#endif |
| 307 |
return true; |
| 308 |
default: |
| 309 |
break; |
| 310 |
} |
| 311 |
#endif |
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
#if defined(SK_GRAPHITE) |
| 316 |
static const char* get_path_renderer_strategy_string( |
| 317 |
std::optional<skgpu::graphite::PathRendererStrategy> strategy) { |
| 318 |
using Strategy = skgpu::graphite::PathRendererStrategy; |
| 319 |
if (!strategy.has_value()) { |
| 320 |
return "Default"; |
| 321 |
} |
| 322 |
|
| 323 |
switch (*strategy) { |
| 324 |
case Strategy::kComputeAnalyticAA: |
| 325 |
return "GPU Compute AA (Analytic)"; |
| 326 |
case Strategy::kComputeMSAA16: |
| 327 |
return "GPU Compute AA (16xMSAA)"; |
| 328 |
case Strategy::kComputeMSAA8: |
| 329 |
return "GPU Compute AA (8xMSAA)"; |
| 330 |
case Strategy::kRasterAtlas: |
| 331 |
return "CPU Raster Atlas"; |
| 332 |
case Strategy::kTessellation: |
| 333 |
return "Tessellation"; |
| 334 |
case Strategy::kTessellationAndSmallAtlas: |
| 335 |
return "Tessellation w/ Small Path Atlas"; |
| 336 |
} |
| 337 |
|
| 338 |
SkUNREACHABLE; |
| 339 |
} |
| 340 |
|
| 341 |
static std::optional<skgpu::graphite::PathRendererStrategy> get_path_renderer_strategy_type( |
| 342 |
const char* str) { |
| 343 |
using Strategy = skgpu::graphite::PathRendererStrategy; |
| 344 |
if (0 == strcmp(str, "default")) { |
| 345 |
return {}; |
| 346 |
} else if (0 == strcmp(str, "raster")) { |
| 347 |
return Strategy::kRasterAtlas; |
| 348 |
#ifdef SK_ENABLE_VELLO_SHADERS |
| 349 |
} else if (0 == strcmp(str, "compute-analytic")) { |
| 350 |
return Strategy::kComputeAnalyticAA; |
| 351 |
} else if (0 == strcmp(str, "compute-msaa16")) { |
| 352 |
return Strategy::kComputeMSAA16; |
| 353 |
} else if (0 == strcmp(str, "compute-msaa8")) { |
| 354 |
return Strategy::kComputeMSAA8; |
| 355 |
#endif |
| 356 |
} else if (0 == strcmp(str, "tessellation")) { |
| 357 |
return Strategy::kTessellation; |
| 358 |
} else if (0 == strcmp(str, "tessellation+atlas")) { |
| 359 |
return Strategy::kTessellationAndSmallAtlas; |
| 360 |
} else { |
| 361 |
SkDebugf("Unknown path renderer strategy type, %s, defaulting to default.", str); |
| 362 |
return {}; |
| 363 |
} |
| 364 |
} |
| 365 |
#endif |
| 366 |
|
| 367 |
const char* get_backend_string(sk_app::Window::BackendType type) { |
| 368 |
switch (type) { |
| 369 |
case sk_app::Window::BackendType::kNativeGL: |
| 370 |
return "OpenGL"; |
| 371 |
case sk_app::Window::BackendType::kANGLE: |
| 372 |
return "ANGLE"; |
| 373 |
case sk_app::Window::BackendType::kGraphiteDawnD3D11: |
| 374 |
return "Dawn D3D11 (Graphite)"; |
| 375 |
case sk_app::Window::BackendType::kGraphiteDawnD3D12: |
| 376 |
return "Dawn D3D12 (Graphite)"; |
| 377 |
case sk_app::Window::BackendType::kGraphiteDawnMetal: |
| 378 |
return "Dawn Metal (Graphite)"; |
| 379 |
case sk_app::Window::BackendType::kGraphiteDawnOpenGLES: |
| 380 |
return "Dawn OpenGLES (Graphite)"; |
| 381 |
case sk_app::Window::BackendType::kGraphiteDawnVulkan: |
| 382 |
return "Dawn Vulkan (Graphite)"; |
| 383 |
case sk_app::Window::BackendType::kVulkan: |
| 384 |
return "Vulkan"; |
| 385 |
case sk_app::Window::BackendType::kGraphiteVulkan: |
| 386 |
return "Vulkan (Graphite)"; |
| 387 |
case sk_app::Window::BackendType::kMetal: |
| 388 |
return "Metal"; |
| 389 |
case sk_app::Window::BackendType::kGraphiteMetal: |
| 390 |
return "Metal (Graphite)"; |
| 391 |
case sk_app::Window::BackendType::kDirect3D: |
| 392 |
return "Direct3D"; |
| 393 |
case sk_app::Window::BackendType::kRaster: |
| 394 |
return "Raster"; |
| 395 |
default: |
| 396 |
SK_ABORT("unsupported backend type"); |
| 397 |
} |
| 398 |
return nullptr; |
| 399 |
} |
| 400 |
|
| 401 |
static sk_app::Window::BackendType get_backend_type(const char* str) { |
| 402 |
if (0 == strcmp(str, "sw")) { |
| 403 |
return sk_app::Window::BackendType::kRaster; |
| 404 |
} |
| 405 |
#if defined(SK_DAWN) && defined(SK_GRAPHITE) |
| 406 |
if (0 == strcmp(str, "grdawn_d3d11")) { |
| 407 |
return sk_app::Window::BackendType::kGraphiteDawnD3D11; |
| 408 |
} else if (0 == strcmp(str, "grdawn_d3d12")) { |
| 409 |
return sk_app::Window::BackendType::kGraphiteDawnD3D12; |
| 410 |
} else if (0 == strcmp(str, "grdawn_metal")) { |
| 411 |
return sk_app::Window::BackendType::kGraphiteDawnMetal; |
| 412 |
} else if (0 == strcmp(str, "grdawn_gles")) { |
| 413 |
return sk_app::Window::BackendType::kGraphiteDawnOpenGLES; |
| 414 |
} else if (0 == strcmp(str, "grdawn_vk")) { |
| 415 |
return sk_app::Window::BackendType::kGraphiteDawnVulkan; |
| 416 |
} else |
| 417 |
#endif |
| 418 |
|
| 419 |
#if defined(SK_VULKAN) |
| 420 |
# if defined(SK_GANESH) |
| 421 |
if (0 == strcmp(str, "vk")) { |
| 422 |
return sk_app::Window::BackendType::kVulkan; |
| 423 |
} else |
| 424 |
# endif |
| 425 |
# if defined(SK_GRAPHITE) |
| 426 |
if (0 == strcmp(str, "grvk")) { |
| 427 |
return sk_app::Window::BackendType::kGraphiteVulkan; |
| 428 |
} else |
| 429 |
# endif |
| 430 |
#endif |
| 431 |
|
| 432 |
#if defined(SK_METAL) |
| 433 |
# if defined(SK_GANESH) |
| 434 |
if (0 == strcmp(str, "mtl")) { |
| 435 |
return sk_app::Window::BackendType::kMetal; |
| 436 |
} else |
| 437 |
# endif |
| 438 |
# if defined(SK_GRAPHITE) |
| 439 |
if (0 == strcmp(str, "grmtl")) { |
| 440 |
return sk_app::Window::BackendType::kGraphiteMetal; |
| 441 |
} else |
| 442 |
# endif |
| 443 |
#endif |
| 444 |
|
| 445 |
#if defined(SK_DIRECT3D) && defined(SK_GANESH) |
| 446 |
if (0 == strcmp(str, "d3d")) { |
| 447 |
return sk_app::Window::BackendType::kDirect3D; |
| 448 |
} else |
| 449 |
#endif |
| 450 |
|
| 451 |
#if defined(SK_GL) && defined(SK_GANESH) |
| 452 |
if (0 == strcmp(str, "gl")) { |
| 453 |
return sk_app::Window::BackendType::kNativeGL; |
| 454 |
} else if (0 == strcmp(str, "sw")) { |
| 455 |
return sk_app::Window::BackendType::kRaster; |
| 456 |
} else |
| 457 |
#if defined(SK_ANGLE) && (defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)) |
| 458 |
if (0 == strcmp(str, "angle")) { |
| 459 |
return sk_app::Window::BackendType::kANGLE; |
| 460 |
} else |
| 461 |
#endif |
| 462 |
#endif |
| 463 |
SkDebugf("Unknown backend type, %s, defaulting to sw.", str); |
| 464 |
return sk_app::Window::BackendType::kRaster; |
| 465 |
} |
| 466 |
|
| 467 |
static SkColorSpacePrimaries gSrgbPrimaries = { |
| 468 |
0.64f, 0.33f, |
| 469 |
0.30f, 0.60f, |
| 470 |
0.15f, 0.06f, |
| 471 |
0.3127f, 0.3290f }; |
| 472 |
|
| 473 |
static SkColorSpacePrimaries gAdobePrimaries = { |
| 474 |
0.64f, 0.33f, |
| 475 |
0.21f, 0.71f, |
| 476 |
0.15f, 0.06f, |
| 477 |
0.3127f, 0.3290f }; |
| 478 |
|
| 479 |
static SkColorSpacePrimaries gP3Primaries = { |
| 480 |
0.680f, 0.320f, |
| 481 |
0.265f, 0.690f, |
| 482 |
0.150f, 0.060f, |
| 483 |
0.3127f, 0.3290f }; |
| 484 |
|
| 485 |
static SkColorSpacePrimaries gRec2020Primaries = { |
| 486 |
0.708f, 0.292f, |
| 487 |
0.170f, 0.797f, |
| 488 |
0.131f, 0.046f, |
| 489 |
0.3127f, 0.3290f }; |
| 490 |
|
| 491 |
struct NamedPrimaries { |
| 492 |
const char* fName; |
| 493 |
SkColorSpacePrimaries* fPrimaries; |
| 494 |
} gNamedPrimaries[] = { |
| 495 |
{ "sRGB", &gSrgbPrimaries }, |
| 496 |
{ "AdobeRGB", &gAdobePrimaries }, |
| 497 |
{ "P3", &gP3Primaries }, |
| 498 |
{ "Rec. 2020", &gRec2020Primaries }, |
| 499 |
}; |
| 500 |
|
| 501 |
static bool primaries_equal(const SkColorSpacePrimaries& a, const SkColorSpacePrimaries& b) { |
| 502 |
return memcmp(&a, &b, sizeof(SkColorSpacePrimaries)) == 0; |
| 503 |
} |
| 504 |
|
| 505 |
class NullSlide : public Slide { |
| 506 |
void draw(SkCanvas* canvas) override { |
| 507 |
canvas->clear(0xffff11ff); |
| 508 |
} |
| 509 |
}; |
| 510 |
|
| 511 |
static const char kName[] = "name"; |
| 512 |
static const char kValue[] = "value"; |
| 513 |
static const char kOptions[] = "options"; |
| 514 |
static const char kSlideStateName[] = "Slide"; |
| 515 |
static const char kBackendStateName[] = "Backend"; |
| 516 |
static const char kMSAAStateName[] = "MSAA"; |
| 517 |
static const char kSoftkeyStateName[] = "Softkey"; |
| 518 |
static const char kSoftkeyHint[] = "Please select a softkey"; |
| 519 |
static const char kON[] = "ON"; |
| 520 |
static const char kRefreshStateName[] = "Refresh"; |
| 521 |
|
| 522 |
#if defined(SK_GANESH) |
| 523 |
static const char kPathRendererStateName[] = "Path renderer"; |
| 524 |
#endif |
| 525 |
|
| 526 |
static const Window::BackendType kSupportedBackends[] = { |
| 527 |
#if defined(SK_GL) && defined(SK_GANESH) |
| 528 |
sk_app::Window::BackendType::kNativeGL, |
| 529 |
#if defined(SK_ANGLE) && (defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)) |
| 530 |
sk_app::Window::BackendType::kANGLE, |
| 531 |
#endif |
| 532 |
#endif |
| 533 |
|
| 534 |
#if defined(SK_DAWN) && defined(SK_GRAPHITE) |
| 535 |
#if defined(SK_BUILD_FOR_WIN) |
| 536 |
sk_app::Window::BackendType::kGraphiteDawnD3D11, |
| 537 |
sk_app::Window::BackendType::kGraphiteDawnD3D12, |
| 538 |
#endif |
| 539 |
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 540 |
sk_app::Window::BackendType::kGraphiteDawnMetal, |
| 541 |
#endif |
| 542 |
#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) |
| 543 |
sk_app::Window::BackendType::kGraphiteDawnOpenGLES, |
| 544 |
sk_app::Window::BackendType::kGraphiteDawnVulkan, |
| 545 |
#endif |
| 546 |
#endif |
| 547 |
|
| 548 |
#if defined(SK_VULKAN) |
| 549 |
# if defined(SK_GANESH) |
| 550 |
sk_app::Window::BackendType::kVulkan, |
| 551 |
# endif |
| 552 |
# if defined(SK_GRAPHITE) |
| 553 |
sk_app::Window::BackendType::kGraphiteVulkan, |
| 554 |
# endif |
| 555 |
#endif |
| 556 |
|
| 557 |
#if defined(SK_METAL) |
| 558 |
# if defined(SK_GANESH) |
| 559 |
sk_app::Window::BackendType::kMetal, |
| 560 |
# endif |
| 561 |
# if defined(SK_GRAPHITE) |
| 562 |
sk_app::Window::BackendType::kGraphiteMetal, |
| 563 |
# endif |
| 564 |
#endif |
| 565 |
|
| 566 |
#if defined(SK_DIRECT3D) && defined(SK_GANESH) |
| 567 |
sk_app::Window::BackendType::kDirect3D, |
| 568 |
#endif |
| 569 |
|
| 570 |
sk_app::Window::BackendType::kRaster, |
| 571 |
}; |
| 572 |
|
| 573 |
constexpr size_t kSupportedBackendTypeCount = std::size(kSupportedBackends); |
| 574 |
|
| 575 |
static Window::BackendType backend_type_for_window(Window::BackendType backendType) { |
| 576 |
// In raster mode, we still use GL for the window. |
| 577 |
// This lets us render the GUI faster (and correct). |
| 578 |
#if defined(SK_GANESH) && defined(SK_GL) |
| 579 |
Window::BackendType windowTypeForRaster = Window::BackendType::kNativeGL; |
| 580 |
#else |
| 581 |
// kSupportedBackends will always have at least one entry. Picking the first should |
| 582 |
// usually result in an adequate GPU-backend. |
| 583 |
Window::BackendType windowTypeForRaster = kSupportedBackends[0]; |
| 584 |
#endif |
| 585 |
return Window::BackendType::kRaster == backendType ? windowTypeForRaster : backendType; |
| 586 |
} |
| 587 |
|
| 588 |
#if defined(SK_GRAPHITE) |
| 589 |
static skwindow::GraphiteDisplayParamsBuilder make_display_params_builder( |
| 590 |
const DisplayParams* other = nullptr) { |
| 591 |
if (!other) { |
| 592 |
return skwindow::GraphiteDisplayParamsBuilder(); |
| 593 |
} |
| 594 |
return skwindow::GraphiteDisplayParamsBuilder(other); |
| 595 |
} |
| 596 |
#else |
| 597 |
static skwindow::DisplayParamsBuilder make_display_params_builder( |
| 598 |
const DisplayParams* other = nullptr) { |
| 599 |
if (!other) { |
| 600 |
return skwindow::DisplayParamsBuilder(); |
| 601 |
} |
| 602 |
return skwindow::DisplayParamsBuilder(other); |
| 603 |
} |
| 604 |
#endif |
| 605 |
|
| 606 |
Viewer::Viewer(int argc, char** argv, void* platformData) |
| 607 |
: fCurrentSlide(-1) |
| 608 |
, fRefresh(false) |
| 609 |
, fSaveToSKP(false) |
| 610 |
, fShowSlideDimensions(false) |
| 611 |
, fShowImGuiDebugWindow(false) |
| 612 |
, fShowSlidePicker(false) |
| 613 |
, fShowImGuiTestWindow(false) |
| 614 |
, fShowHistogramWindow(false) |
| 615 |
, fShowZoomWindow(false) |
| 616 |
, fZoomWindowFixed(false) |
| 617 |
, fZoomWindowLocation{0.0f, 0.0f} |
| 618 |
, fLastImage(nullptr) |
| 619 |
, fZoomUI(false) |
| 620 |
, fBackendType(sk_app::Window::BackendType::kNativeGL) |
| 621 |
, fColorMode(ColorMode::kLegacy) |
| 622 |
, fColorSpacePrimaries(gSrgbPrimaries) |
| 623 |
// Our UI can only tweak gamma (currently), so start out gamma-only |
| 624 |
, fColorSpaceTransferFn(SkNamedTransferFn::k2Dot2) |
| 625 |
, fApplyBackingScale(true) |
| 626 |
, fZoomLevel(0.0f) |
| 627 |
, fRotation(0.0f) |
| 628 |
, fOffset{0.5f, 0.5f} |
| 629 |
, fGestureDevice(GestureDevice::kNone) |
| 630 |
, fTiled(false) |
| 631 |
, fDrawTileBoundaries(false) |
| 632 |
, fTileScale{0.25f, 0.25f} |
| 633 |
, fPerspectiveMode(kPerspective_Off) { |
| 634 |
SkGraphics::Init(); |
| 635 |
#if defined(SK_ENABLE_SVG) |
| 636 |
SkGraphics::SetOpenTypeSVGDecoderFactory(SkSVGOpenTypeSVGDecoder::Make); |
| 637 |
#endif |
| 638 |
CodecUtils::RegisterAllAvailable(); |
| 639 |
ToolUtils::RegisterAvailableTypefaceFactories(); |
| 640 |
|
| 641 |
SkDebugf("Command line arguments: "); |
| 642 |
for (int i = 1; i < argc; ++i) { |
| 643 |
SkDebugf("%s ", argv[i]); |
| 644 |
} |
| 645 |
SkDebugf("\n"); |
| 646 |
|
| 647 |
CommandLineFlags::Parse(argc, argv); |
| 648 |
#ifdef SK_BUILD_FOR_ANDROID |
| 649 |
SetResourcePath("/data/local/tmp/resources"); |
| 650 |
#endif |
| 651 |
|
| 652 |
initializeEventTracingForTools(); |
| 653 |
static SkTaskGroup::Enabler kTaskGroupEnabler(FLAGS_threads); |
| 654 |
|
| 655 |
fBackendType = get_backend_type(FLAGS_backend[0]); |
| 656 |
fWindow = Windows::CreateNativeWindow(platformData); |
| 657 |
|
| 658 |
auto paramsBuilder = make_display_params_builder(); |
| 659 |
paramsBuilder.msaaSampleCount(FLAGS_msaa); |
| 660 |
paramsBuilder.createProtectedNativeBackend(FLAGS_createProtected); |
| 661 |
|
| 662 |
#if defined(SK_GANESH) |
| 663 |
gGaneshPathRendererNames[GpuPathRenderers::kDefault] = "Default Path Renderers"; |
| 664 |
gGaneshPathRendererNames[GpuPathRenderers::kAtlas] = "Atlas (tessellation)"; |
| 665 |
gGaneshPathRendererNames[GpuPathRenderers::kTessellation] = "Tessellation"; |
| 666 |
gGaneshPathRendererNames[GpuPathRenderers::kSmall] = "Small paths (cached sdf or alpha masks)"; |
| 667 |
gGaneshPathRendererNames[GpuPathRenderers::kTriangulating] = "Triangulating"; |
| 668 |
gGaneshPathRendererNames[GpuPathRenderers::kNone] = "Software masks"; |
| 669 |
|
| 670 |
GrContextOptions grctxOpts; |
| 671 |
CommonFlags::SetCtxOptions(&grctxOpts); |
| 672 |
grctxOpts.fPersistentCache = &fPersistentCache; |
| 673 |
grctxOpts.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL; |
| 674 |
// Make sure this agrees with GrContextOptions. |
| 675 |
SkASSERT(fOptLevel == kShaderOptLevel_Source); |
| 676 |
grctxOpts.fShaderErrorHandler = &gShaderErrorHandler; |
| 677 |
grctxOpts.fSuppressPrints = true; |
| 678 |
grctxOpts.fSupportBilerpFromGlyphAtlas = true; |
| 679 |
paramsBuilder.grContextOptions(grctxOpts); |
| 680 |
if (FLAGS_dmsaa) { |
| 681 |
paramsBuilder.surfaceProps( |
| 682 |
SkSurfaceProps(SkSurfaceProps::kDefault_Flag | SkSurfaceProps::kDynamicMSAA_Flag, |
| 683 |
kRGB_H_SkPixelGeometry, |
| 684 |
SK_GAMMA_CONTRAST, |
| 685 |
SK_GAMMA_EXPONENT)); |
| 686 |
} |
| 687 |
#endif |
| 688 |
|
| 689 |
#if defined(SK_GRAPHITE) |
| 690 |
skwindow::GraphiteTestOptions gto; |
| 691 |
CommonFlags::SetTestOptions(>o.fTestOptions); |
| 692 |
gto.fPriv.fPathRendererStrategy = get_path_renderer_strategy_type(FLAGS_pathstrategy[0]); |
| 693 |
if (FLAGS_msaa <= 0) { |
| 694 |
gto.fTestOptions.fContextOptions.fInternalMultisampleCount = |
| 695 |
skgpu::graphite::SampleCount::k1; |
| 696 |
} |
| 697 |
paramsBuilder.graphiteTestOptions(gto); |
| 698 |
#endif |
| 699 |
fDisplay = paramsBuilder.detach(); |
| 700 |
fWindow->setRequestedDisplayParams(fDisplay->clone()); |
| 701 |
fRefresh = FLAGS_redraw; |
| 702 |
|
| 703 |
fImGuiLayer.setScaleFactor(fWindow->scaleFactor()); |
| 704 |
fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor()); |
| 705 |
|
| 706 |
// Configure timers |
| 707 |
fStatsLayer.setActive(FLAGS_stats); |
| 708 |
fAnimateTimer = fStatsLayer.addTimer("Animate", SK_ColorMAGENTA, 0xffff66ff); |
| 709 |
fPaintTimer = fStatsLayer.addTimer("Paint", SK_ColorGREEN); |
| 710 |
fFlushTimer = fStatsLayer.addTimer("Flush", SK_ColorRED, 0xffff6666); |
| 711 |
|
| 712 |
// register callbacks |
| 713 |
fCommands.attach(fWindow); |
| 714 |
fWindow->pushLayer(this); |
| 715 |
fWindow->pushLayer(&fStatsLayer); |
| 716 |
fWindow->pushLayer(&fImGuiLayer); |
| 717 |
|
| 718 |
// add key-bindings |
| 719 |
fCommands.addCommand(' ', "GUI", "Toggle Debug GUI", [this]() { |
| 720 |
this->fShowImGuiDebugWindow = !this->fShowImGuiDebugWindow; |
| 721 |
fWindow->inval(); |
| 722 |
}); |
| 723 |
// Command to jump directly to the slide picker and give it focus |
| 724 |
fCommands.addCommand('/', "GUI", "Jump to slide picker", [this]() { |
| 725 |
this->fShowImGuiDebugWindow = true; |
| 726 |
this->fShowSlidePicker = true; |
| 727 |
fWindow->inval(); |
| 728 |
}); |
| 729 |
// Alias that to Backspace, to match SampleApp |
| 730 |
fCommands.addCommand(skui::Key::kBack, "Backspace", "GUI", "Jump to slide picker", [this]() { |
| 731 |
this->fShowImGuiDebugWindow = true; |
| 732 |
this->fShowSlidePicker = true; |
| 733 |
fWindow->inval(); |
| 734 |
}); |
| 735 |
fCommands.addCommand('g', "GUI", "Toggle GUI Demo", [this]() { |
| 736 |
this->fShowImGuiTestWindow = !this->fShowImGuiTestWindow; |
| 737 |
fWindow->inval(); |
| 738 |
}); |
| 739 |
fCommands.addCommand('z', "GUI", "Toggle zoom window", [this]() { |
| 740 |
this->fShowZoomWindow = !this->fShowZoomWindow; |
| 741 |
fWindow->inval(); |
| 742 |
}); |
| 743 |
fCommands.addCommand('Z', "GUI", "Toggle zoom window state", [this]() { |
| 744 |
this->fZoomWindowFixed = !this->fZoomWindowFixed; |
| 745 |
fWindow->inval(); |
| 746 |
}); |
| 747 |
fCommands.addCommand('v', "Swapchain", "Toggle vsync on/off", [this]() { |
| 748 |
auto params = fWindow->getRequestedDisplayParams(); |
| 749 |
auto paramsBuilder = make_display_params_builder(params); |
| 750 |
paramsBuilder.disableVsync(!params->disableVsync()); |
| 751 |
fWindow->setRequestedDisplayParams(paramsBuilder.detach()); |
| 752 |
this->updateTitle(); |
| 753 |
fWindow->inval(); |
| 754 |
}); |
| 755 |
fCommands.addCommand('V', "Swapchain", "Toggle delayed acquire on/off (Metal only)", [this]() { |
| 756 |
auto params = fWindow->getRequestedDisplayParams(); |
| 757 |
auto paramsBuilder = make_display_params_builder(params); |
| 758 |
paramsBuilder.delayDrawableAcquisition(!params->delayDrawableAcquisition()); |
| 759 |
fWindow->setRequestedDisplayParams(paramsBuilder.detach()); |
| 760 |
this->updateTitle(); |
| 761 |
fWindow->inval(); |
| 762 |
}); |
| 763 |
fCommands.addCommand('r', "Redraw", "Toggle redraw", [this]() { |
| 764 |
fRefresh = !fRefresh; |
| 765 |
fWindow->inval(); |
| 766 |
}); |
| 767 |
fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() { |
| 768 |
fStatsLayer.setActive(!fStatsLayer.getActive()); |
| 769 |
fWindow->inval(); |
| 770 |
}); |
| 771 |
fCommands.addCommand('0', "Overlays", "Reset stats", [this]() { |
| 772 |
fStatsLayer.resetMeasurements(); |
| 773 |
this->updateTitle(); |
| 774 |
fWindow->inval(); |
| 775 |
}); |
| 776 |
fCommands.addCommand('C', "GUI", "Toggle color histogram", [this]() { |
| 777 |
this->fShowHistogramWindow = !this->fShowHistogramWindow; |
| 778 |
fWindow->inval(); |
| 779 |
}); |
| 780 |
fCommands.addCommand('c', "Modes", "Cycle color mode", [this]() { |
| 781 |
switch (fColorMode) { |
| 782 |
case ColorMode::kLegacy: |
| 783 |
this->setColorMode(ColorMode::kColorManaged8888); |
| 784 |
break; |
| 785 |
case ColorMode::kColorManaged8888: |
| 786 |
this->setColorMode(ColorMode::kColorManagedF16); |
| 787 |
break; |
| 788 |
case ColorMode::kColorManagedF16: |
| 789 |
this->setColorMode(ColorMode::kColorManagedF16Norm); |
| 790 |
break; |
| 791 |
case ColorMode::kColorManagedF16Norm: |
| 792 |
this->setColorMode(ColorMode::kLegacy); |
| 793 |
break; |
| 794 |
} |
| 795 |
}); |
| 796 |
#if defined(SK_GANESH) |
| 797 |
fCommands.addCommand('w', "Modes", "Toggle wireframe", [this]() { |
| 798 |
auto params = fWindow->getRequestedDisplayParams(); |
| 799 |
auto paramsBuilder = make_display_params_builder(params); |
| 800 |
GrContextOptions grOpts = params->grContextOptions(); |
| 801 |
grOpts.fWireframeMode = !grOpts.fWireframeMode; |
| 802 |
paramsBuilder.grContextOptions(grOpts); |
| 803 |
fWindow->setRequestedDisplayParams(paramsBuilder.detach()); |
| 804 |
fWindow->inval(); |
| 805 |
}); |
| 806 |
fCommands.addCommand('w', "Modes", "Toggle reduced shaders", [this]() { |
| 807 |
auto params = fWindow->getRequestedDisplayParams(); |
| 808 |
auto paramsBuilder = make_display_params_builder(params); |
| 809 |
GrContextOptions grOpts = params->grContextOptions(); |
| 810 |
grOpts.fReducedShaderVariations = !grOpts.fReducedShaderVariations; |
| 811 |
paramsBuilder.grContextOptions(grOpts); |
| 812 |
fWindow->setRequestedDisplayParams(paramsBuilder.detach()); |
| 813 |
fWindow->inval(); |
| 814 |
}); |
| 815 |
#endif |
| 816 |
fCommands.addCommand(skui::Key::kRight, "Right", "Navigation", "Next slide", [this]() { |
| 817 |
this->setCurrentSlide(fCurrentSlide < fSlides.size() - 1 ? fCurrentSlide + 1 : 0); |
| 818 |
}); |
| 819 |
fCommands.addCommand(skui::Key::kLeft, "Left", "Navigation", "Previous slide", [this]() { |
| 820 |
this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.size() - 1); |
| 821 |
}); |
| 822 |
fCommands.addCommand(skui::Key::kUp, "Up", "Transform", "Zoom in", [this]() { |
| 823 |
this->changeZoomLevel(1.f / 32.f); |
| 824 |
fWindow->inval(); |
| 825 |
}); |
| 826 |
fCommands.addCommand(skui::Key::kDown, "Down", "Transform", "Zoom out", [this]() { |
| 827 |
this->changeZoomLevel(-1.f / 32.f); |
| 828 |
fWindow->inval(); |
| 829 |
}); |
| 830 |
|
| 831 |
fCommands.addCommand('d', "Modes", "Change rendering backend", [this]() { |
| 832 |
int currIdx = -1; |
| 833 |
for (size_t i = 0; i < kSupportedBackendTypeCount; i++) { |
| 834 |
if (kSupportedBackends[i] == fBackendType) { |
| 835 |
currIdx = int(i); |
| 836 |
break; |
| 837 |
} |
| 838 |
} |
| 839 |
SkASSERT(currIdx >= 0); |
| 840 |
auto newBackend = kSupportedBackends[(currIdx + 1) % kSupportedBackendTypeCount]; |
| 841 |
this->setBackend(newBackend); |
| 842 |
}); |
| 843 |
fCommands.addCommand('K', "IO", "Save slide to SKP", [this]() { |
| 844 |
fSaveToSKP = true; |
| 845 |
fWindow->inval(); |
| 846 |
}); |
| 847 |
fCommands.addCommand('&', "Overlays", "Show slide dimensions", [this]() { |
| 848 |
fShowSlideDimensions = !fShowSlideDimensions; |
| 849 |
fWindow->inval(); |
| 850 |
}); |
| 851 |
fCommands.addCommand('G', "Modes", "Geometry", [this]() { |
| 852 |
auto params = fWindow->getRequestedDisplayParams(); |
| 853 |
auto paramsBuilder = make_display_params_builder(params); |
| 854 |
SkSurfaceProps newProps; |
| 855 |
|
| 856 |
uint32_t flags = params->surfaceProps().flags(); |
| 857 |
SkPixelGeometry defaultPixelGeometry = fDisplay->surfaceProps().pixelGeometry(); |
| 858 |
SkSurfaceProps defaultSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry); |
| 859 |
fSlides[fCurrentSlide]->setSurfaceProps(&defaultSurfaceProps); |
| 860 |
if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) { |
| 861 |
fDisplayOverrides.fSurfaceProps.fPixelGeometry = true; |
| 862 |
newProps = SkSurfaceProps(flags, kUnknown_SkPixelGeometry); |
| 863 |
} else { |
| 864 |
switch (params->surfaceProps().pixelGeometry()) { |
| 865 |
case kUnknown_SkPixelGeometry: |
| 866 |
newProps = SkSurfaceProps(flags, kRGB_H_SkPixelGeometry); |
| 867 |
break; |
| 868 |
case kRGB_H_SkPixelGeometry: |
| 869 |
newProps = SkSurfaceProps(flags, kBGR_H_SkPixelGeometry); |
| 870 |
break; |
| 871 |
case kBGR_H_SkPixelGeometry: |
| 872 |
newProps = SkSurfaceProps(flags, kRGB_V_SkPixelGeometry); |
| 873 |
break; |
| 874 |
case kRGB_V_SkPixelGeometry: |
| 875 |
newProps = SkSurfaceProps(flags, kBGR_V_SkPixelGeometry); |
| 876 |
break; |
| 877 |
case kBGR_V_SkPixelGeometry: |
| 878 |
newProps = defaultSurfaceProps; |
| 879 |
fDisplayOverrides.fSurfaceProps.fPixelGeometry = false; |
| 880 |
break; |
| 881 |
} |
| 882 |
} |
| 883 |
paramsBuilder.surfaceProps(newProps); |
| 884 |
fWindow->setRequestedDisplayParams(paramsBuilder.detach()); |
| 885 |
this->updateTitle(); |
| 886 |
fWindow->inval(); |
| 887 |
}); |
| 888 |
fCommands.addCommand('H', "Font", "Hinting mode", [this]() { |
| 889 |
if (!fFontOverrides.fHinting) { |
| 890 |
fFontOverrides.fHinting = true; |
| 891 |
fFont.setHinting(SkFontHinting::kNone); |
| 892 |
} else { |
| 893 |
switch (fFont.getHinting()) { |
| 894 |
case SkFontHinting::kNone: |
| 895 |
fFont.setHinting(SkFontHinting::kSlight); |
| 896 |
break; |
| 897 |
case SkFontHinting::kSlight: |
| 898 |
fFont.setHinting(SkFontHinting::kNormal); |
| 899 |
break; |
| 900 |
case SkFontHinting::kNormal: |
| 901 |
fFont.setHinting(SkFontHinting::kFull); |
| 902 |
break; |
| 903 |
case SkFontHinting::kFull: |
| 904 |
fFont.setHinting(SkFontHinting::kNone); |
| 905 |
fFontOverrides.fHinting = false; |
| 906 |
break; |
| 907 |
} |
| 908 |
} |
| 909 |
this->updateTitle(); |
| 910 |
fWindow->inval(); |
| 911 |
}); |
| 912 |
fCommands.addCommand('D', "Modes", "DFT", [this]() { |
| 913 |
auto params = fWindow->getRequestedDisplayParams(); |
| 914 |
auto paramsBuilder = make_display_params_builder(params); |
| 915 |
uint32_t flags = params->surfaceProps().flags(); |
| 916 |
flags ^= SkSurfaceProps::kUseDeviceIndependentFonts_Flag; |
| 917 |
SkSurfaceProps newProps = SkSurfaceProps(flags, params->surfaceProps().pixelGeometry()); |
| 918 |
|
| 919 |
paramsBuilder.surfaceProps(newProps); |
| 920 |
fWindow->setRequestedDisplayParams(paramsBuilder.detach()); |
| 921 |
this->updateTitle(); |
| 922 |
fWindow->inval(); |
| 923 |
}); |
| 924 |
fCommands.addCommand('L', "Font", "Subpixel Antialias Mode", [this]() { |
| 925 |
if (!fFontOverrides.fEdging) { |
| 926 |
fFontOverrides.fEdging = true; |
| 927 |
fFont.setEdging(SkFont::Edging::kAlias); |
| 928 |
} else { |
| 929 |
switch (fFont.getEdging()) { |
| 930 |
case SkFont::Edging::kAlias: |
| 931 |
fFont.setEdging(SkFont::Edging::kAntiAlias); |
| 932 |
break; |
| 933 |
case SkFont::Edging::kAntiAlias: |
| 934 |
fFont.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 935 |
break; |
| 936 |
case SkFont::Edging::kSubpixelAntiAlias: |
| 937 |
fFont.setEdging(SkFont::Edging::kAlias); |
| 938 |
fFontOverrides.fEdging = false; |
| 939 |
break; |
| 940 |
} |
| 941 |
} |
| 942 |
this->updateTitle(); |
| 943 |
fWindow->inval(); |
| 944 |
}); |
| 945 |
fCommands.addCommand('S', "Font", "Subpixel Position Mode", [this]() { |
| 946 |
if (!fFontOverrides.fSubpixel) { |
| 947 |
fFontOverrides.fSubpixel = true; |
| 948 |
fFont.setSubpixel(false); |
| 949 |
} else { |
| 950 |
if (!fFont.isSubpixel()) { |
| 951 |
fFont.setSubpixel(true); |
| 952 |
} else { |
| 953 |
fFontOverrides.fSubpixel = false; |
| 954 |
} |
| 955 |
} |
| 956 |
this->updateTitle(); |
| 957 |
fWindow->inval(); |
| 958 |
}); |
| 959 |
fCommands.addCommand('B', "Font", "Baseline Snapping", [this]() { |
| 960 |
if (!fFontOverrides.fBaselineSnap) { |
| 961 |
fFontOverrides.fBaselineSnap = true; |
| 962 |
fFont.setBaselineSnap(false); |
| 963 |
} else { |
| 964 |
if (!fFont.isBaselineSnap()) { |
| 965 |
fFont.setBaselineSnap(true); |
| 966 |
} else { |
| 967 |
fFontOverrides.fBaselineSnap = false; |
| 968 |
} |
| 969 |
} |
| 970 |
this->updateTitle(); |
| 971 |
fWindow->inval(); |
| 972 |
}); |
| 973 |
fCommands.addCommand('p', "Transform", "Toggle Perspective Mode", [this]() { |
| 974 |
fPerspectiveMode = (kPerspective_Real == fPerspectiveMode) ? kPerspective_Fake |
| 975 |
: kPerspective_Real; |
| 976 |
this->updateTitle(); |
| 977 |
fWindow->inval(); |
| 978 |
}); |
| 979 |
fCommands.addCommand('P', "Transform", "Toggle Perspective", [this]() { |
| 980 |
fPerspectiveMode = (kPerspective_Off == fPerspectiveMode) ? kPerspective_Real |
| 981 |
: kPerspective_Off; |
| 982 |
this->updateTitle(); |
| 983 |
fWindow->inval(); |
| 984 |
}); |
| 985 |
fCommands.addCommand('a', "Transform", "Toggle Animation", [this]() { |
| 986 |
fAnimTimer.togglePauseResume(); |
| 987 |
}); |
| 988 |
fCommands.addCommand('u', "GUI", "Zoom UI", [this]() { |
| 989 |
fZoomUI = !fZoomUI; |
| 990 |
fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor()); |
| 991 |
fWindow->inval(); |
| 992 |
}); |
| 993 |
fCommands.addCommand('=', "Transform", "Apply Backing Scale", [this]() { |
| 994 |
fApplyBackingScale = !fApplyBackingScale; |
| 995 |
fWindow->inval(); |
| 996 |
}); |
| 997 |
fCommands.addCommand('$', "ViaSerialize", "Toggle ViaSerialize", [this]() { |
| 998 |
fDrawViaSerialize = !fDrawViaSerialize; |
| 999 |
this->updateTitle(); |
| 1000 |
fWindow->inval(); |
| 1001 |
}); |
| 1002 |
|
| 1003 |
// set up slides |
| 1004 |
this->initSlides(); |
| 1005 |
if (FLAGS_list) { |
| 1006 |
this->listNames(); |
| 1007 |
} |
| 1008 |
|
| 1009 |
fPerspectivePoints[0].set(0, 0); |
| 1010 |
fPerspectivePoints[1].set(1, 0); |
| 1011 |
fPerspectivePoints[2].set(0, 1); |
| 1012 |
fPerspectivePoints[3].set(1, 1); |
| 1013 |
fAnimTimer.run(); |
| 1014 |
|
| 1015 |
auto gamutImage = ToolUtils::GetResourceAsImage("images/gamut.png"); |
| 1016 |
if (gamutImage) { |
| 1017 |
fImGuiGamutPaint.setShader(gamutImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear))); |
| 1018 |
} |
| 1019 |
fImGuiGamutPaint.setColor(SK_ColorWHITE); |
| 1020 |
|
| 1021 |
fWindow->attach(backend_type_for_window(fBackendType)); |
| 1022 |
this->initGpuTimer(); |
| 1023 |
this->setCurrentSlide(this->startupSlide()); |
| 1024 |
} |
| 1025 |
|
| 1026 |
static sk_sp<SkData> data_from_file(FILE* fp) { |
| 1027 |
SkDynamicMemoryWStream stream; |
| 1028 |
char buf[4096]; |
| 1029 |
while (size_t bytesRead = fread(buf, 1, 4096, fp)) { |
| 1030 |
stream.write(buf, bytesRead); |
| 1031 |
} |
| 1032 |
return stream.detachAsData(); |
| 1033 |
} |
| 1034 |
|
| 1035 |
static sk_sp<SkData> base64_string_to_data(const std::string& s) { |
| 1036 |
size_t dataLen; |
| 1037 |
if (SkBase64::Decode(s.c_str(), s.size(), nullptr, &dataLen) != SkBase64::kNoError) { |
| 1038 |
return nullptr; |
| 1039 |
} |
| 1040 |
|
| 1041 |
sk_sp<SkData> decodedData = SkData::MakeUninitialized(dataLen); |
| 1042 |
void* rawData = decodedData->writable_data(); |
| 1043 |
if (SkBase64::Decode(s.c_str(), s.size(), rawData, &dataLen) != SkBase64::kNoError) { |
| 1044 |
return nullptr; |
| 1045 |
} |
| 1046 |
|
| 1047 |
return decodedData; |
| 1048 |
} |
| 1049 |
|
| 1050 |
static std::vector<sk_sp<SkImage>> find_data_uri_images(sk_sp<SkData> data) { |
| 1051 |
std::string str(reinterpret_cast<const char*>(data->data()), data->size()); |
| 1052 |
std::regex re("data:image/png;base64,([a-zA-Z0-9+/=]+)"); |
| 1053 |
std::sregex_iterator images_begin(str.begin(), str.end(), re); |
| 1054 |
std::sregex_iterator images_end; |
| 1055 |
std::vector<sk_sp<SkImage>> images; |
| 1056 |
|
| 1057 |
for (auto iter = images_begin; iter != images_end; ++iter) { |
| 1058 |
const std::smatch& match = *iter; |
| 1059 |
auto raw = base64_string_to_data(match[1].str()); |
| 1060 |
if (!raw) { |
| 1061 |
continue; |
| 1062 |
} |
| 1063 |
auto image = SkImages::DeferredFromEncodedData(std::move(raw)); |
| 1064 |
if (image) { |
| 1065 |
images.push_back(std::move(image)); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
return images; |
| 1070 |
} |
| 1071 |
|
| 1072 |
void Viewer::initSlides() { |
| 1073 |
using SlideMaker = sk_sp<Slide> (*)(const SkString& name, const SkString& path); |
| 1074 |
static const struct { |
| 1075 |
const char* fExtension; |
| 1076 |
const char* fDirName; |
| 1077 |
const CommandLineFlags::StringArray& fFlags; |
| 1078 |
const SlideMaker fFactory; |
| 1079 |
} gExternalSlidesInfo[] = { |
| 1080 |
#if defined(SK_CODEC_DECODES_PNG_WITH_LIBPNG) |
| 1081 |
{".mskp", |
| 1082 |
"mskp-dir", |
| 1083 |
FLAGS_mskps, |
| 1084 |
[](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 1085 |
return sk_make_sp<MSKPSlide>(name, path); |
| 1086 |
}}, |
| 1087 |
#endif |
| 1088 |
{".skp", |
| 1089 |
"skp-dir", |
| 1090 |
FLAGS_skps, |
| 1091 |
[](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 1092 |
return sk_make_sp<SKPSlide>(name, path); |
| 1093 |
}}, |
| 1094 |
{".gif", |
| 1095 |
"gif-dir", |
| 1096 |
FLAGS_jpgs, |
| 1097 |
[](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 1098 |
return sk_make_sp<AnimatedImageSlide>(name, path); |
| 1099 |
}}, |
| 1100 |
{".jpg", |
| 1101 |
"jpg-dir", |
| 1102 |
FLAGS_jpgs, |
| 1103 |
[](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 1104 |
return sk_make_sp<ImageSlide>(name, path); |
| 1105 |
}}, |
| 1106 |
{".jxl", |
| 1107 |
"jxl-dir", |
| 1108 |
FLAGS_jxls, |
| 1109 |
[](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 1110 |
return sk_make_sp<ImageSlide>(name, path); |
| 1111 |
}}, |
| 1112 |
{".webp", |
| 1113 |
"webp-dir", |
| 1114 |
FLAGS_jpgs, |
| 1115 |
[](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 1116 |
return sk_make_sp<AnimatedImageSlide>(name, path); |
| 1117 |
}}, |
| 1118 |
#if defined(SK_ENABLE_SKOTTIE) |
| 1119 |
{".json", |
| 1120 |
"skottie-dir", |
| 1121 |
FLAGS_lotties, |
| 1122 |
[](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 1123 |
return sk_make_sp<SkottieSlide>(name, path); |
| 1124 |
}}, |
| 1125 |
#endif |
| 1126 |
#if defined(SK_ENABLE_SVG) |
| 1127 |
{".svg", |
| 1128 |
"svg-dir", |
| 1129 |
FLAGS_svgs, |
| 1130 |
[](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 1131 |
return sk_make_sp<SvgSlide>(name, path); |
| 1132 |
}}, |
| 1133 |
#endif |
| 1134 |
{".capt", |
| 1135 |
"capture-dir", |
| 1136 |
FLAGS_captures, |
| 1137 |
[](const SkString& name, const SkString& path) -> sk_sp<Slide> { |
| 1138 |
return sk_make_sp<CaptureSlide>(name, path); |
| 1139 |
}}, |
| 1140 |
}; |
| 1141 |
|
| 1142 |
TArray<sk_sp<Slide>> dirSlides; |
| 1143 |
|
| 1144 |
const auto addSlide = [&](const SkString& name, const SkString& path, const SlideMaker& fact) { |
| 1145 |
if (CommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) { |
| 1146 |
return; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (auto slide = fact(name, path)) { |
| 1150 |
dirSlides.push_back(slide); |
| 1151 |
fSlides.push_back(std::move(slide)); |
| 1152 |
} |
| 1153 |
}; |
| 1154 |
|
| 1155 |
if (!FLAGS_file.isEmpty()) { |
| 1156 |
// single file mode |
| 1157 |
const SkString file(FLAGS_file[0]); |
| 1158 |
|
| 1159 |
// `--file stdin` parses stdin, looking for data URIs that encode images |
| 1160 |
if (file.equals("stdin")) { |
| 1161 |
sk_sp<SkData> data = data_from_file(stdin); |
| 1162 |
std::vector<sk_sp<SkImage>> images = find_data_uri_images(std::move(data)); |
| 1163 |
// TODO: If there is an even number of images, create diff images from consecutive pairs |
| 1164 |
// (Maybe do this optionally? Or add a dedicated diff-slide that can show diff stats?) |
| 1165 |
for (auto image : images) { |
| 1166 |
char imageID = 'A' + fSlides.size(); |
| 1167 |
fSlides.push_back(sk_make_sp<ImageSlide>(SkStringPrintf("Image %c", imageID), |
| 1168 |
std::move(image))); |
| 1169 |
} |
| 1170 |
if (!fSlides.empty()) { |
| 1171 |
fShowZoomWindow = true; |
| 1172 |
return; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
if (sk_exists(file.c_str(), kRead_SkFILE_Flag)) { |
| 1177 |
for (const auto& sinfo : gExternalSlidesInfo) { |
| 1178 |
if (file.endsWith(sinfo.fExtension)) { |
| 1179 |
addSlide(SkOSPath::Basename(file.c_str()), file, sinfo.fFactory); |
| 1180 |
return; |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
fprintf(stderr, "Unsupported file type \"%s\"\n", file.c_str()); |
| 1185 |
} else { |
| 1186 |
fprintf(stderr, "Cannot read \"%s\"\n", file.c_str()); |
| 1187 |
} |
| 1188 |
|
| 1189 |
return; |
| 1190 |
} |
| 1191 |
|
| 1192 |
// Bisect slide. |
| 1193 |
if (!FLAGS_bisect.isEmpty()) { |
| 1194 |
sk_sp<BisectSlide> bisect = BisectSlide::Create(FLAGS_bisect[0]); |
| 1195 |
if (bisect && !CommandLineFlags::ShouldSkip(FLAGS_match, bisect->getName().c_str())) { |
| 1196 |
if (FLAGS_bisect.size() >= 2) { |
| 1197 |
for (const char* ch = FLAGS_bisect[1]; *ch; ++ch) { |
| 1198 |
bisect->onChar(*ch); |
| 1199 |
} |
| 1200 |
} |
| 1201 |
fSlides.push_back(std::move(bisect)); |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
// GMs |
| 1206 |
int firstGM = fSlides.size(); |
| 1207 |
for (const skiagm::GMFactory& gmFactory : skiagm::GMRegistry::Range()) { |
| 1208 |
std::unique_ptr<skiagm::GM> gm = gmFactory(); |
| 1209 |
if (!CommandLineFlags::ShouldSkip(FLAGS_match, gm->getName().c_str())) { |
| 1210 |
auto slide = sk_make_sp<GMSlide>(std::move(gm)); |
| 1211 |
fSlides.push_back(std::move(slide)); |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
auto orderBySlideName = [](sk_sp<Slide> a, sk_sp<Slide> b) { |
| 1216 |
return SK_strcasecmp(a->getName().c_str(), b->getName().c_str()) < 0; |
| 1217 |
}; |
| 1218 |
std::sort(fSlides.begin() + firstGM, fSlides.end(), orderBySlideName); |
| 1219 |
|
| 1220 |
int firstRegisteredSlide = fSlides.size(); |
| 1221 |
|
| 1222 |
// Registered slides are replacing Samples. |
| 1223 |
for (const SlideFactory& factory : SlideRegistry::Range()) { |
| 1224 |
auto slide = sk_sp<Slide>(factory()); |
| 1225 |
if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) { |
| 1226 |
fSlides.push_back(slide); |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
std::sort(fSlides.begin() + firstRegisteredSlide, fSlides.end(), orderBySlideName); |
| 1231 |
|
| 1232 |
// Runtime shader editor |
| 1233 |
{ |
| 1234 |
auto slide = sk_make_sp<SkSLSlide>(); |
| 1235 |
if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) { |
| 1236 |
fSlides.push_back(std::move(slide)); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
// Runtime shader debugger |
| 1241 |
{ |
| 1242 |
auto slide = sk_make_sp<SkSLDebuggerSlide>(); |
| 1243 |
if (!CommandLineFlags::ShouldSkip(FLAGS_match, slide->getName().c_str())) { |
| 1244 |
fSlides.push_back(std::move(slide)); |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
for (const auto& info : gExternalSlidesInfo) { |
| 1249 |
for (const auto& flag : info.fFlags) { |
| 1250 |
if (SkStrEndsWith(flag.c_str(), info.fExtension)) { |
| 1251 |
// single file |
| 1252 |
addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory); |
| 1253 |
} else { |
| 1254 |
// directory |
| 1255 |
SkString name; |
| 1256 |
TArray<SkString> sortedFilenames; |
| 1257 |
SkOSFile::Iter it(flag.c_str(), info.fExtension); |
| 1258 |
while (it.next(&name)) { |
| 1259 |
sortedFilenames.push_back(name); |
| 1260 |
} |
| 1261 |
if (sortedFilenames.size()) { |
| 1262 |
SkTQSort(sortedFilenames.begin(), sortedFilenames.end(), |
| 1263 |
[](const SkString& a, const SkString& b) { |
| 1264 |
return strcmp(a.c_str(), b.c_str()) < 0; |
| 1265 |
}); |
| 1266 |
} |
| 1267 |
for (const SkString& filename : sortedFilenames) { |
| 1268 |
addSlide(filename, SkOSPath::Join(flag.c_str(), filename.c_str()), |
| 1269 |
info.fFactory); |
| 1270 |
} |
| 1271 |
} |
| 1272 |
if (!dirSlides.empty()) { |
| 1273 |
fSlides.push_back( |
| 1274 |
sk_make_sp<SlideDir>(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()), |
| 1275 |
std::move(dirSlides))); |
| 1276 |
dirSlides.clear(); // NOLINT(bugprone-use-after-move) |
| 1277 |
} |
| 1278 |
} |
| 1279 |
} |
| 1280 |
|
| 1281 |
if (fSlides.empty()) { |
| 1282 |
auto slide = sk_make_sp<NullSlide>(); |
| 1283 |
fSlides.push_back(std::move(slide)); |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
Viewer::~Viewer() { |
| 1289 |
for(auto& slide : fSlides) { |
| 1290 |
slide->gpuTeardown(); |
| 1291 |
} |
| 1292 |
|
| 1293 |
fWindow->detach(); |
| 1294 |
delete fWindow; |
| 1295 |
} |
| 1296 |
|
| 1297 |
struct SkPaintTitleUpdater { |
| 1298 |
SkPaintTitleUpdater(SkString* title) : fTitle(title), fCount(0) {} |
| 1299 |
void append(const char* s) { |
| 1300 |
if (fCount == 0) { |
| 1301 |
fTitle->append(" {"); |
| 1302 |
} else { |
| 1303 |
fTitle->append(", "); |
| 1304 |
} |
| 1305 |
fTitle->append(s); |
| 1306 |
++fCount; |
| 1307 |
} |
| 1308 |
void done() { |
| 1309 |
if (fCount > 0) { |
| 1310 |
fTitle->append("}"); |
| 1311 |
} |
| 1312 |
} |
| 1313 |
SkString* fTitle; |
| 1314 |
int fCount; |
| 1315 |
}; |
| 1316 |
|
| 1317 |
void Viewer::updateTitle() { |
| 1318 |
if (!fWindow) { |
| 1319 |
return; |
| 1320 |
} |
| 1321 |
if (fWindow->sampleCount() < 1) { |
| 1322 |
return; // Surface hasn't been created yet. |
| 1323 |
} |
| 1324 |
|
| 1325 |
SkString title("Viewer: "); |
| 1326 |
title.append(fSlides[fCurrentSlide]->getName()); |
| 1327 |
|
| 1328 |
if (fDrawViaSerialize) { |
| 1329 |
title.append(" <serialize>"); |
| 1330 |
} |
| 1331 |
|
| 1332 |
SkPaintTitleUpdater paintTitle(&title); |
| 1333 |
auto paintFlag = [this, &paintTitle](bool SkPaintFields::* flag, |
| 1334 |
bool (SkPaint::* isFlag)() const, |
| 1335 |
const char* on, const char* off) |
| 1336 |
{ |
| 1337 |
if (fPaintOverrides.*flag) { |
| 1338 |
paintTitle.append((fPaint.*isFlag)() ? on : off); |
| 1339 |
} |
| 1340 |
}; |
| 1341 |
|
| 1342 |
auto fontFlag = [this, &paintTitle](bool SkFontFields::* flag, bool (SkFont::* isFlag)() const, |
| 1343 |
const char* on, const char* off) |
| 1344 |
{ |
| 1345 |
if (fFontOverrides.*flag) { |
| 1346 |
paintTitle.append((fFont.*isFlag)() ? on : off); |
| 1347 |
} |
| 1348 |
}; |
| 1349 |
|
| 1350 |
paintFlag(&SkPaintFields::fAntiAlias, &SkPaint::isAntiAlias, "Antialias", "Alias"); |
| 1351 |
paintFlag(&SkPaintFields::fDither, &SkPaint::isDither, "DITHER", "No Dither"); |
| 1352 |
|
| 1353 |
fontFlag(&SkFontFields::fForceAutoHinting, &SkFont::isForceAutoHinting, |
| 1354 |
"Force Autohint", "No Force Autohint"); |
| 1355 |
fontFlag(&SkFontFields::fEmbolden, &SkFont::isEmbolden, "Fake Bold", "No Fake Bold"); |
| 1356 |
fontFlag(&SkFontFields::fBaselineSnap, &SkFont::isBaselineSnap, "BaseSnap", "No BaseSnap"); |
| 1357 |
fontFlag(&SkFontFields::fLinearMetrics, &SkFont::isLinearMetrics, |
| 1358 |
"Linear Metrics", "Non-Linear Metrics"); |
| 1359 |
fontFlag(&SkFontFields::fEmbeddedBitmaps, &SkFont::isEmbeddedBitmaps, |
| 1360 |
"Bitmap Text", "No Bitmap Text"); |
| 1361 |
fontFlag(&SkFontFields::fSubpixel, &SkFont::isSubpixel, "Subpixel Text", "Pixel Text"); |
| 1362 |
|
| 1363 |
if (fFontOverrides.fEdging) { |
| 1364 |
switch (fFont.getEdging()) { |
| 1365 |
case SkFont::Edging::kAlias: |
| 1366 |
paintTitle.append("Alias Text"); |
| 1367 |
break; |
| 1368 |
case SkFont::Edging::kAntiAlias: |
| 1369 |
paintTitle.append("Antialias Text"); |
| 1370 |
break; |
| 1371 |
case SkFont::Edging::kSubpixelAntiAlias: |
| 1372 |
paintTitle.append("Subpixel Antialias Text"); |
| 1373 |
break; |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
if (fFontOverrides.fHinting) { |
| 1378 |
switch (fFont.getHinting()) { |
| 1379 |
case SkFontHinting::kNone: |
| 1380 |
paintTitle.append("No Hinting"); |
| 1381 |
break; |
| 1382 |
case SkFontHinting::kSlight: |
| 1383 |
paintTitle.append("Slight Hinting"); |
| 1384 |
break; |
| 1385 |
case SkFontHinting::kNormal: |
| 1386 |
paintTitle.append("Normal Hinting"); |
| 1387 |
break; |
| 1388 |
case SkFontHinting::kFull: |
| 1389 |
paintTitle.append("Full Hinting"); |
| 1390 |
break; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
paintTitle.done(); |
| 1394 |
|
| 1395 |
switch (fColorMode) { |
| 1396 |
case ColorMode::kLegacy: |
| 1397 |
title.append(" Legacy 8888"); |
| 1398 |
break; |
| 1399 |
case ColorMode::kColorManaged8888: |
| 1400 |
title.append(" ColorManaged 8888"); |
| 1401 |
break; |
| 1402 |
case ColorMode::kColorManagedF16: |
| 1403 |
title.append(" ColorManaged F16"); |
| 1404 |
break; |
| 1405 |
case ColorMode::kColorManagedF16Norm: |
| 1406 |
title.append(" ColorManaged F16 Norm"); |
| 1407 |
break; |
| 1408 |
} |
| 1409 |
|
| 1410 |
if (ColorMode::kLegacy != fColorMode) { |
| 1411 |
int curPrimaries = -1; |
| 1412 |
for (size_t i = 0; i < std::size(gNamedPrimaries); ++i) { |
| 1413 |
if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) { |
| 1414 |
curPrimaries = i; |
| 1415 |
break; |
| 1416 |
} |
| 1417 |
} |
| 1418 |
title.appendf(" %s Gamma %f", |
| 1419 |
curPrimaries >= 0 ? gNamedPrimaries[curPrimaries].fName : "Custom", |
| 1420 |
fColorSpaceTransferFn.g); |
| 1421 |
} |
| 1422 |
|
| 1423 |
auto params = fWindow->getRequestedDisplayParams(); |
| 1424 |
if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) { |
| 1425 |
switch (params->surfaceProps().pixelGeometry()) { |
| 1426 |
case kUnknown_SkPixelGeometry: |
| 1427 |
title.append( " Flat"); |
| 1428 |
break; |
| 1429 |
case kRGB_H_SkPixelGeometry: |
| 1430 |
title.append( " RGB"); |
| 1431 |
break; |
| 1432 |
case kBGR_H_SkPixelGeometry: |
| 1433 |
title.append( " BGR"); |
| 1434 |
break; |
| 1435 |
case kRGB_V_SkPixelGeometry: |
| 1436 |
title.append( " RGBV"); |
| 1437 |
break; |
| 1438 |
case kBGR_V_SkPixelGeometry: |
| 1439 |
title.append( " BGRV"); |
| 1440 |
break; |
| 1441 |
} |
| 1442 |
} |
| 1443 |
|
| 1444 |
if (params->surfaceProps().isUseDeviceIndependentFonts()) { |
| 1445 |
title.append(" DFT"); |
| 1446 |
} |
| 1447 |
|
| 1448 |
title.append(" ["); |
| 1449 |
title.append(get_backend_string(fBackendType)); |
| 1450 |
int msaa = fWindow->sampleCount(); |
| 1451 |
if (msaa > 1) { |
| 1452 |
title.appendf(" MSAA: %i", msaa); |
| 1453 |
} |
| 1454 |
title.append("]"); |
| 1455 |
|
| 1456 |
if (is_graphite_backend_type(fBackendType)) { |
| 1457 |
#if defined(SK_GRAPHITE) |
| 1458 |
auto graphiteOptions = fWindow->getRequestedDisplayParams()->graphiteTestOptions(); |
| 1459 |
SkASSERT(graphiteOptions); |
| 1460 |
auto strategy = graphiteOptions->fPriv.fPathRendererStrategy; |
| 1461 |
if (strategy.has_value()) { |
| 1462 |
title.appendf(" [Path renderer strategy: %s]", |
| 1463 |
get_path_renderer_strategy_string(strategy)); |
| 1464 |
} |
| 1465 |
#endif |
| 1466 |
} else { |
| 1467 |
#if defined(SK_GANESH) |
| 1468 |
GpuPathRenderers pr = |
| 1469 |
fWindow->getRequestedDisplayParams()->grContextOptions().fGpuPathRenderers; |
| 1470 |
if (GpuPathRenderers::kDefault != pr) { |
| 1471 |
title.appendf(" [Path renderer: %s]", gGaneshPathRendererNames[pr].c_str()); |
| 1472 |
} |
| 1473 |
#endif |
| 1474 |
} |
| 1475 |
|
| 1476 |
if (kPerspective_Real == fPerspectiveMode) { |
| 1477 |
title.append(" Perspective (Real)"); |
| 1478 |
} else if (kPerspective_Fake == fPerspectiveMode) { |
| 1479 |
title.append(" Perspective (Fake)"); |
| 1480 |
} |
| 1481 |
|
| 1482 |
fWindow->setTitle(title.c_str()); |
| 1483 |
} |
| 1484 |
|
| 1485 |
int Viewer::startupSlide() const { |
| 1486 |
|
| 1487 |
if (!FLAGS_slide.isEmpty()) { |
| 1488 |
int count = fSlides.size(); |
| 1489 |
for (int i = 0; i < count; i++) { |
| 1490 |
if (fSlides[i]->getName().equals(FLAGS_slide[0])) { |
| 1491 |
return i; |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
fprintf(stderr, "Unknown slide \"%s\"\n", FLAGS_slide[0]); |
| 1496 |
this->listNames(); |
| 1497 |
} |
| 1498 |
|
| 1499 |
return 0; |
| 1500 |
} |
| 1501 |
|
| 1502 |
void Viewer::listNames() const { |
| 1503 |
SkDebugf("All Slides:\n"); |
| 1504 |
for (const auto& slide : fSlides) { |
| 1505 |
SkDebugf(" %s\n", slide->getName().c_str()); |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
void Viewer::setCurrentSlide(int slide) { |
| 1510 |
SkASSERT(slide >= 0 && slide < fSlides.size()); |
| 1511 |
|
| 1512 |
if (slide == fCurrentSlide) { |
| 1513 |
return; |
| 1514 |
} |
| 1515 |
|
| 1516 |
if (fCurrentSlide >= 0) { |
| 1517 |
fSlides[fCurrentSlide]->unload(); |
| 1518 |
} |
| 1519 |
|
| 1520 |
SkScalar scaleFactor = 1.0; |
| 1521 |
if (fApplyBackingScale) { |
| 1522 |
scaleFactor = fWindow->scaleFactor(); |
| 1523 |
} |
| 1524 |
fSlides[slide]->load(SkIntToScalar(fWindow->width()) / scaleFactor, |
| 1525 |
SkIntToScalar(fWindow->height()) / scaleFactor); |
| 1526 |
fCurrentSlide = slide; |
| 1527 |
this->setupCurrentSlide(); |
| 1528 |
} |
| 1529 |
|
| 1530 |
SkISize Viewer::currentSlideSize() const { |
| 1531 |
if (auto size = fSlides[fCurrentSlide]->getDimensions(); !size.isEmpty()) { |
| 1532 |
return size; |
| 1533 |
} |
| 1534 |
return {fWindow->width(), fWindow->height()}; |
| 1535 |
} |
| 1536 |
|
| 1537 |
void Viewer::setupCurrentSlide() { |
| 1538 |
if (fCurrentSlide >= 0) { |
| 1539 |
// prepare dimensions for image slides |
| 1540 |
fGesture.resetTouchState(); |
| 1541 |
fDefaultMatrix.reset(); |
| 1542 |
|
| 1543 |
const SkRect slideBounds = SkRect::Make(this->currentSlideSize()); |
| 1544 |
const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height()); |
| 1545 |
|
| 1546 |
// Start with a matrix that scales the slide to the available screen space |
| 1547 |
if (fWindow->scaleContentToFit()) { |
| 1548 |
if (windowRect.width() > 0 && windowRect.height() > 0) { |
| 1549 |
fDefaultMatrix = SkMatrix::RectToRectOrIdentity(slideBounds, windowRect, |
| 1550 |
SkMatrix::kStart_ScaleToFit); |
| 1551 |
} |
| 1552 |
} |
| 1553 |
|
| 1554 |
// Prevent the user from dragging content so far outside the window they can't find it again |
| 1555 |
fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix()); |
| 1556 |
|
| 1557 |
if (!fDisplayOverrides.fSurfaceProps.fPixelGeometry) { |
| 1558 |
const skwindow::DisplayParams* params = fWindow->getRequestedDisplayParams(); |
| 1559 |
uint32_t flags = params->surfaceProps().flags(); |
| 1560 |
SkPixelGeometry defaultPixelGeometry = fDisplay->surfaceProps().pixelGeometry(); |
| 1561 |
SkSurfaceProps defaultSurfaceProps = SkSurfaceProps(flags, defaultPixelGeometry); |
| 1562 |
fSlides[fCurrentSlide]->setSurfaceProps(&defaultSurfaceProps); |
| 1563 |
if (defaultSurfaceProps != params->surfaceProps()) { |
| 1564 |
auto paramsBuilder = make_display_params_builder(params); |
| 1565 |
paramsBuilder.surfaceProps(defaultSurfaceProps); |
| 1566 |
const skwindow::DisplayParams* newParams = paramsBuilder.detach().release(); |
| 1567 |
// Defer to avoid asserts when the GPU flushes the current draw. |
| 1568 |
fDeferredActions.emplace_back([newParams, this]() { |
| 1569 |
fWindow->setRequestedDisplayParams( |
| 1570 |
std::unique_ptr<const skwindow::DisplayParams>(newParams)); |
| 1571 |
fWindow->inval(); |
| 1572 |
}); |
| 1573 |
} |
| 1574 |
} |
| 1575 |
|
| 1576 |
this->updateTitle(); |
| 1577 |
this->updateUIState(); |
| 1578 |
|
| 1579 |
fStatsLayer.resetMeasurements(); |
| 1580 |
|
| 1581 |
fWindow->inval(); |
| 1582 |
} |
| 1583 |
} |
| 1584 |
|
| 1585 |
#define MAX_ZOOM_LEVEL 8.0f |
| 1586 |
#define MIN_ZOOM_LEVEL -8.0f |
| 1587 |
|
| 1588 |
void Viewer::changeZoomLevel(float delta) { |
| 1589 |
fZoomLevel += delta; |
| 1590 |
fZoomLevel = SkTPin(fZoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL); |
| 1591 |
this->updateGestureTransLimit(); |
| 1592 |
} |
| 1593 |
|
| 1594 |
void Viewer::updateGestureTransLimit() { |
| 1595 |
// Update the trans limit as the transform changes. |
| 1596 |
const SkRect slideBounds = SkRect::Make(this->currentSlideSize()); |
| 1597 |
const SkRect windowRect = SkRect::MakeIWH(fWindow->width(), fWindow->height()); |
| 1598 |
fGesture.setTransLimit(slideBounds, windowRect, this->computePreTouchMatrix()); |
| 1599 |
} |
| 1600 |
|
| 1601 |
SkMatrix Viewer::computePerspectiveMatrix() { |
| 1602 |
SkScalar w = fWindow->width(), h = fWindow->height(); |
| 1603 |
SkPoint orthoPts[4] = { { 0, 0 }, { w, 0 }, { 0, h }, { w, h } }; |
| 1604 |
SkPoint perspPts[4] = { |
| 1605 |
{ fPerspectivePoints[0].fX * w, fPerspectivePoints[0].fY * h }, |
| 1606 |
{ fPerspectivePoints[1].fX * w, fPerspectivePoints[1].fY * h }, |
| 1607 |
{ fPerspectivePoints[2].fX * w, fPerspectivePoints[2].fY * h }, |
| 1608 |
{ fPerspectivePoints[3].fX * w, fPerspectivePoints[3].fY * h } |
| 1609 |
}; |
| 1610 |
return SkMatrix::PolyToPoly(orthoPts, perspPts).value_or(SkMatrix::I()); |
| 1611 |
} |
| 1612 |
|
| 1613 |
SkMatrix Viewer::computePreTouchMatrix() { |
| 1614 |
SkMatrix m = fDefaultMatrix; |
| 1615 |
|
| 1616 |
SkScalar zoomScale = exp(fZoomLevel); |
| 1617 |
if (fApplyBackingScale) { |
| 1618 |
zoomScale *= fWindow->scaleFactor(); |
| 1619 |
} |
| 1620 |
m.preTranslate((fOffset.x() - 0.5f) * 2.0f, (fOffset.y() - 0.5f) * 2.0f); |
| 1621 |
m.preScale(zoomScale, zoomScale); |
| 1622 |
|
| 1623 |
const SkISize slideSize = this->currentSlideSize(); |
| 1624 |
m.preRotate(fRotation, slideSize.width() * 0.5f, slideSize.height() * 0.5f); |
| 1625 |
|
| 1626 |
if (kPerspective_Real == fPerspectiveMode) { |
| 1627 |
SkMatrix persp = this->computePerspectiveMatrix(); |
| 1628 |
m.postConcat(persp); |
| 1629 |
} |
| 1630 |
|
| 1631 |
return m; |
| 1632 |
} |
| 1633 |
|
| 1634 |
SkMatrix Viewer::computeMatrix() { |
| 1635 |
SkMatrix m = fGesture.localM(); |
| 1636 |
m.preConcat(fGesture.globalM()); |
| 1637 |
m.preConcat(this->computePreTouchMatrix()); |
| 1638 |
return m; |
| 1639 |
} |
| 1640 |
|
| 1641 |
void Viewer::setBackend(sk_app::Window::BackendType backendType) { |
| 1642 |
#if defined(SK_GANESH) |
| 1643 |
fPersistentCache.reset(); |
| 1644 |
#endif |
| 1645 |
fCachedShaders.clear(); |
| 1646 |
fBackendType = backendType; |
| 1647 |
|
| 1648 |
// The active context is going away in 'detach' |
| 1649 |
for(auto& slide : fSlides) { |
| 1650 |
slide->gpuTeardown(); |
| 1651 |
} |
| 1652 |
|
| 1653 |
fWindow->detach(); |
| 1654 |
|
| 1655 |
#if defined(SK_BUILD_FOR_WIN) |
| 1656 |
// Switching between OpenGL, Vulkan, and ANGLE in the same window is problematic at this point |
| 1657 |
// on Windows, so we just delete the window and recreate it with the same params. |
| 1658 |
std::unique_ptr<DisplayParams> params = fWindow->getRequestedDisplayParams()->clone(); |
| 1659 |
delete fWindow; |
| 1660 |
fWindow = Windows::CreateNativeWindow(nullptr); |
| 1661 |
|
| 1662 |
// re-register callbacks |
| 1663 |
fCommands.attach(fWindow); |
| 1664 |
fWindow->pushLayer(this); |
| 1665 |
fWindow->pushLayer(&fStatsLayer); |
| 1666 |
fWindow->pushLayer(&fImGuiLayer); |
| 1667 |
|
| 1668 |
// Don't allow the window to re-attach. If we're in MSAA mode, the params we grabbed above |
| 1669 |
// will still include our correct sample count. But the re-created fWindow will lose that |
| 1670 |
// information. On Windows, we need to re-create the window when changing sample count, |
| 1671 |
// so we'll incorrectly detect that situation, then re-initialize the window in GL mode, |
| 1672 |
// rendering this tear-down step pointless (and causing the Vulkan window context to fail |
| 1673 |
// as if we had never changed windows at all). |
| 1674 |
fWindow->setRequestedDisplayParams(std::move(params), false); |
| 1675 |
#endif |
| 1676 |
|
| 1677 |
fWindow->attach(backend_type_for_window(fBackendType)); |
| 1678 |
this->initGpuTimer(); |
| 1679 |
} |
| 1680 |
|
| 1681 |
void Viewer::initGpuTimer() { |
| 1682 |
// The explicit raster backend check is here because raster may be presented via a GPU window |
| 1683 |
// context which does support GPU timers. |
| 1684 |
if (fBackendType == Window::BackendType::kRaster || !fWindow->supportsGpuTimer()) { |
| 1685 |
fStatsLayer.disableGpuTimer(); |
| 1686 |
return; |
| 1687 |
} |
| 1688 |
fStatsLayer.enableGpuTimer(SK_ColorYELLOW); |
| 1689 |
} |
| 1690 |
|
| 1691 |
void Viewer::setColorMode(ColorMode colorMode) { |
| 1692 |
fColorMode = colorMode; |
| 1693 |
this->updateTitle(); |
| 1694 |
fWindow->inval(); |
| 1695 |
} |
| 1696 |
|
| 1697 |
class OveridePaintFilterCanvas : public SkPaintFilterCanvas { |
| 1698 |
public: |
| 1699 |
OveridePaintFilterCanvas(SkCanvas* canvas, |
| 1700 |
SkPaint* paint, Viewer::SkPaintFields* pfields, |
| 1701 |
SkFont* font, Viewer::SkFontFields* ffields) |
| 1702 |
: SkPaintFilterCanvas(canvas) |
| 1703 |
, fPaint(paint) |
| 1704 |
, fPaintOverrides(pfields) |
| 1705 |
, fFont(font) |
| 1706 |
, fFontOverrides(ffields) { |
| 1707 |
} |
| 1708 |
|
| 1709 |
const SkTextBlob* filterTextBlob(const SkPaint& paint, |
| 1710 |
const SkTextBlob* blob, |
| 1711 |
sk_sp<SkTextBlob>* cache) { |
| 1712 |
bool blobWillChange = false; |
| 1713 |
for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) { |
| 1714 |
SkTCopyOnFirstWrite<SkFont> filteredFont(it.font()); |
| 1715 |
bool shouldDraw = this->filterFont(&filteredFont); |
| 1716 |
if (it.font() != *filteredFont || !shouldDraw) { |
| 1717 |
blobWillChange = true; |
| 1718 |
break; |
| 1719 |
} |
| 1720 |
} |
| 1721 |
if (!blobWillChange) { |
| 1722 |
return blob; |
| 1723 |
} |
| 1724 |
|
| 1725 |
SkTextBlobBuilder builder; |
| 1726 |
for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) { |
| 1727 |
SkTCopyOnFirstWrite<SkFont> filteredFont(it.font()); |
| 1728 |
bool shouldDraw = this->filterFont(&filteredFont); |
| 1729 |
if (!shouldDraw) { |
| 1730 |
continue; |
| 1731 |
} |
| 1732 |
|
| 1733 |
SkFont font = *filteredFont; |
| 1734 |
|
| 1735 |
const SkTextBlobBuilder::RunBuffer& runBuffer |
| 1736 |
= it.positioning() == SkTextBlobRunIterator::kDefault_Positioning |
| 1737 |
? builder.allocRunText(font, it.glyphCount(), it.offset().x(),it.offset().y(), |
| 1738 |
it.textSize()) |
| 1739 |
: it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning |
| 1740 |
? builder.allocRunTextPosH(font, it.glyphCount(), it.offset().y(), |
| 1741 |
it.textSize()) |
| 1742 |
: it.positioning() == SkTextBlobRunIterator::kFull_Positioning |
| 1743 |
? builder.allocRunTextPos(font, it.glyphCount(), it.textSize()) |
| 1744 |
: it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning |
| 1745 |
? builder.allocRunTextRSXform(font, it.glyphCount(), it.textSize()) |
| 1746 |
: (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer()); |
| 1747 |
uint32_t glyphCount = it.glyphCount(); |
| 1748 |
if (it.glyphs()) { |
| 1749 |
size_t glyphSize = sizeof(decltype(*it.glyphs())); |
| 1750 |
memcpy(runBuffer.glyphs, it.glyphs(), glyphCount * glyphSize); |
| 1751 |
} |
| 1752 |
if (it.pos()) { |
| 1753 |
size_t posSize = sizeof(decltype(*it.pos())); |
| 1754 |
unsigned posPerGlyph = it.scalarsPerGlyph(); |
| 1755 |
memcpy(runBuffer.pos, it.pos(), glyphCount * posPerGlyph * posSize); |
| 1756 |
} |
| 1757 |
if (it.text()) { |
| 1758 |
size_t textSize = sizeof(decltype(*it.text())); |
| 1759 |
uint32_t textCount = it.textSize(); |
| 1760 |
memcpy(runBuffer.utf8text, it.text(), textCount * textSize); |
| 1761 |
} |
| 1762 |
if (it.clusters()) { |
| 1763 |
size_t clusterSize = sizeof(decltype(*it.clusters())); |
| 1764 |
memcpy(runBuffer.clusters, it.clusters(), glyphCount * clusterSize); |
| 1765 |
} |
| 1766 |
} |
| 1767 |
*cache = builder.make(); |
| 1768 |
return cache->get(); |
| 1769 |
} |
| 1770 |
void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 1771 |
const SkPaint& paint) override { |
| 1772 |
sk_sp<SkTextBlob> cache; |
| 1773 |
this->SkPaintFilterCanvas::onDrawTextBlob( |
| 1774 |
this->filterTextBlob(paint, blob, &cache), x, y, paint); |
| 1775 |
} |
| 1776 |
|
| 1777 |
void onDrawGlyphRunList( |
| 1778 |
const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) override { |
| 1779 |
sk_sp<SkTextBlob> cache; |
| 1780 |
sk_sp<SkTextBlob> blob = glyphRunList.makeBlob(); |
| 1781 |
this->filterTextBlob(paint, blob.get(), &cache); |
| 1782 |
if (!cache) { |
| 1783 |
this->SkPaintFilterCanvas::onDrawGlyphRunList(glyphRunList, paint); |
| 1784 |
return; |
| 1785 |
} |
| 1786 |
sktext::GlyphRunBuilder builder; |
| 1787 |
const sktext::GlyphRunList& filtered = |
| 1788 |
builder.blobToGlyphRunList(*cache, glyphRunList.origin()); |
| 1789 |
this->SkPaintFilterCanvas::onDrawGlyphRunList(filtered, paint); |
| 1790 |
} |
| 1791 |
|
| 1792 |
bool filterFont(SkTCopyOnFirstWrite<SkFont>* font) const { |
| 1793 |
if (fFontOverrides->fTypeface) { |
| 1794 |
font->writable()->setTypeface(fFont->refTypeface()); |
| 1795 |
} |
| 1796 |
if (fFontOverrides->fSize) { |
| 1797 |
font->writable()->setSize(fFont->getSize()); |
| 1798 |
} |
| 1799 |
if (fFontOverrides->fScaleX) { |
| 1800 |
font->writable()->setScaleX(fFont->getScaleX()); |
| 1801 |
} |
| 1802 |
if (fFontOverrides->fSkewX) { |
| 1803 |
font->writable()->setSkewX(fFont->getSkewX()); |
| 1804 |
} |
| 1805 |
if (fFontOverrides->fHinting) { |
| 1806 |
font->writable()->setHinting(fFont->getHinting()); |
| 1807 |
} |
| 1808 |
if (fFontOverrides->fEdging) { |
| 1809 |
font->writable()->setEdging(fFont->getEdging()); |
| 1810 |
} |
| 1811 |
if (fFontOverrides->fSubpixel) { |
| 1812 |
font->writable()->setSubpixel(fFont->isSubpixel()); |
| 1813 |
} |
| 1814 |
if (fFontOverrides->fForceAutoHinting) { |
| 1815 |
font->writable()->setForceAutoHinting(fFont->isForceAutoHinting()); |
| 1816 |
} |
| 1817 |
if (fFontOverrides->fEmbeddedBitmaps) { |
| 1818 |
font->writable()->setEmbeddedBitmaps(fFont->isEmbeddedBitmaps()); |
| 1819 |
} |
| 1820 |
if (fFontOverrides->fLinearMetrics) { |
| 1821 |
font->writable()->setLinearMetrics(fFont->isLinearMetrics()); |
| 1822 |
} |
| 1823 |
if (fFontOverrides->fEmbolden) { |
| 1824 |
font->writable()->setEmbolden(fFont->isEmbolden()); |
| 1825 |
} |
| 1826 |
if (fFontOverrides->fBaselineSnap) { |
| 1827 |
font->writable()->setBaselineSnap(fFont->isBaselineSnap()); |
| 1828 |
} |
| 1829 |
|
| 1830 |
return true; // we, currently, never elide a draw |
| 1831 |
} |
| 1832 |
|
| 1833 |
bool onFilter(SkPaint& paint) const override { |
| 1834 |
if (fPaintOverrides->fPathEffect) { |
| 1835 |
paint.setPathEffect(fPaint->refPathEffect()); |
| 1836 |
} |
| 1837 |
if (fPaintOverrides->fShader) { |
| 1838 |
paint.setShader(fPaint->refShader()); |
| 1839 |
} |
| 1840 |
if (fPaintOverrides->fMaskFilter) { |
| 1841 |
paint.setMaskFilter(fPaint->refMaskFilter()); |
| 1842 |
} |
| 1843 |
if (fPaintOverrides->fColorFilter) { |
| 1844 |
paint.setColorFilter(fPaint->refColorFilter()); |
| 1845 |
} |
| 1846 |
if (fPaintOverrides->fImageFilter) { |
| 1847 |
paint.setImageFilter(fPaint->refImageFilter()); |
| 1848 |
} |
| 1849 |
if (fPaintOverrides->fColor) { |
| 1850 |
paint.setColor4f(fPaint->getColor4f()); |
| 1851 |
} |
| 1852 |
if (fPaintOverrides->fStrokeWidth) { |
| 1853 |
paint.setStrokeWidth(fPaint->getStrokeWidth()); |
| 1854 |
} |
| 1855 |
if (fPaintOverrides->fMiterLimit) { |
| 1856 |
paint.setStrokeMiter(fPaint->getStrokeMiter()); |
| 1857 |
} |
| 1858 |
if (fPaintOverrides->fBlendMode) { |
| 1859 |
paint.setBlendMode(fPaint->getBlendMode_or(SkBlendMode::kSrc)); |
| 1860 |
} |
| 1861 |
if (fPaintOverrides->fAntiAlias) { |
| 1862 |
paint.setAntiAlias(fPaint->isAntiAlias()); |
| 1863 |
} |
| 1864 |
if (fPaintOverrides->fDither) { |
| 1865 |
paint.setDither(fPaint->isDither()); |
| 1866 |
} |
| 1867 |
if (fPaintOverrides->fForceRuntimeBlend) { |
| 1868 |
if (std::optional<SkBlendMode> mode = paint.asBlendMode()) { |
| 1869 |
paint.setBlender(GetRuntimeBlendForBlendMode(*mode)); |
| 1870 |
} |
| 1871 |
} |
| 1872 |
if (fPaintOverrides->fCapType) { |
| 1873 |
paint.setStrokeCap(fPaint->getStrokeCap()); |
| 1874 |
} |
| 1875 |
if (fPaintOverrides->fJoinType) { |
| 1876 |
paint.setStrokeJoin(fPaint->getStrokeJoin()); |
| 1877 |
} |
| 1878 |
if (fPaintOverrides->fStyle) { |
| 1879 |
paint.setStyle(fPaint->getStyle()); |
| 1880 |
} |
| 1881 |
return true; // we, currently, never elide a draw |
| 1882 |
} |
| 1883 |
SkPaint* fPaint; |
| 1884 |
Viewer::SkPaintFields* fPaintOverrides; |
| 1885 |
SkFont* fFont; |
| 1886 |
Viewer::SkFontFields* fFontOverrides; |
| 1887 |
}; |
| 1888 |
|
| 1889 |
static SkSerialProcs serial_procs_using_png() { |
| 1890 |
SkSerialProcs sProcs; |
| 1891 |
sProcs.fImageProc = [](SkImage* img, void*) -> SkSerialReturnType { |
| 1892 |
#if defined(SK_CODEC_ENCODES_PNG_WITH_RUST) |
| 1893 |
return SkPngRustEncoder::Encode( |
| 1894 |
as_IB(img)->directContext(), img, SkPngRustEncoder::Options{}); |
| 1895 |
#else |
| 1896 |
return SkPngEncoder::Encode(as_IB(img)->directContext(), img, SkPngEncoder::Options{}); |
| 1897 |
#endif |
| 1898 |
}; |
| 1899 |
return sProcs; |
| 1900 |
} |
| 1901 |
|
| 1902 |
void Viewer::drawSlide(SkSurface* surface) { |
| 1903 |
if (fCurrentSlide < 0) { |
| 1904 |
return; |
| 1905 |
} |
| 1906 |
|
| 1907 |
SkAutoCanvasRestore autorestore(surface->getCanvas(), false); |
| 1908 |
|
| 1909 |
// By default, we render directly into the window's surface/canvas |
| 1910 |
[[maybe_unused]] SkSurface* slideSurface = surface; |
| 1911 |
SkCanvas* slideCanvas = surface->getCanvas(); |
| 1912 |
fLastImage.reset(); |
| 1913 |
|
| 1914 |
// If we're in any of the color managed modes, construct the color space we're going to use |
| 1915 |
sk_sp<SkColorSpace> colorSpace = nullptr; |
| 1916 |
if (ColorMode::kLegacy != fColorMode) { |
| 1917 |
skcms_Matrix3x3 toXYZ; |
| 1918 |
SkAssertResult(fColorSpacePrimaries.toXYZD50(&toXYZ)); |
| 1919 |
colorSpace = SkColorSpace::MakeRGB(fColorSpaceTransferFn, toXYZ); |
| 1920 |
} |
| 1921 |
|
| 1922 |
if (fSaveToSKP) { |
| 1923 |
SkPictureRecorder recorder; |
| 1924 |
SkCanvas* recorderCanvas = recorder.beginRecording(SkRect::Make(this->currentSlideSize())); |
| 1925 |
fSlides[fCurrentSlide]->draw(recorderCanvas); |
| 1926 |
sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
| 1927 |
SkFILEWStream stream("sample_app.skp"); |
| 1928 |
SkSerialProcs sProcs = serial_procs_using_png(); |
| 1929 |
picture->serialize(&stream, &sProcs); |
| 1930 |
fSaveToSKP = false; |
| 1931 |
} |
| 1932 |
|
| 1933 |
// Grab some things we'll need to make surfaces (for tiling or general offscreen rendering) |
| 1934 |
SkColorType colorType; |
| 1935 |
switch (fColorMode) { |
| 1936 |
case ColorMode::kLegacy: |
| 1937 |
case ColorMode::kColorManaged8888: |
| 1938 |
colorType = kN32_SkColorType; |
| 1939 |
break; |
| 1940 |
case ColorMode::kColorManagedF16: |
| 1941 |
colorType = kRGBA_F16_SkColorType; |
| 1942 |
break; |
| 1943 |
case ColorMode::kColorManagedF16Norm: |
| 1944 |
colorType = kRGBA_F16Norm_SkColorType; |
| 1945 |
break; |
| 1946 |
} |
| 1947 |
|
| 1948 |
// We need to render offscreen if we're... |
| 1949 |
// ... in fake perspective or zooming (so we have a snapped copy of the results) |
| 1950 |
// ... in any raster mode, because the window surface is actually GL |
| 1951 |
// ... in any color managed mode, because we always make the window surface with no color space |
| 1952 |
// ... or if the user explicitly requested offscreen rendering |
| 1953 |
sk_sp<SkSurface> offscreenSurface = nullptr; |
| 1954 |
if (kPerspective_Fake == fPerspectiveMode || fShowZoomWindow || fShowHistogramWindow || |
| 1955 |
Window::BackendType::kRaster == fBackendType || colorSpace != nullptr || FLAGS_offscreen) { |
| 1956 |
SkSurfaceProps props; |
| 1957 |
if (!slideCanvas->getProps(&props)) { |
| 1958 |
props = fWindow->getRequestedDisplayParams()->surfaceProps(); |
| 1959 |
} |
| 1960 |
|
| 1961 |
SkImageInfo info = SkImageInfo::Make( |
| 1962 |
fWindow->width(), fWindow->height(), colorType, kPremul_SkAlphaType, colorSpace); |
| 1963 |
offscreenSurface = Window::BackendType::kRaster == this->fBackendType |
| 1964 |
? SkSurfaces::Raster(info, &props) |
| 1965 |
: slideCanvas->makeSurface(info, &props); |
| 1966 |
|
| 1967 |
slideSurface = offscreenSurface.get(); |
| 1968 |
slideCanvas = offscreenSurface->getCanvas(); |
| 1969 |
} |
| 1970 |
|
| 1971 |
SkPictureRecorder recorder; |
| 1972 |
SkCanvas* recorderRestoreCanvas = nullptr; |
| 1973 |
if (fDrawViaSerialize) { |
| 1974 |
recorderRestoreCanvas = slideCanvas; |
| 1975 |
slideCanvas = recorder.beginRecording(SkRect::Make(this->currentSlideSize())); |
| 1976 |
} |
| 1977 |
|
| 1978 |
int count = slideCanvas->save(); |
| 1979 |
slideCanvas->clear(SK_ColorWHITE); |
| 1980 |
// Time the painting logic of the slide |
| 1981 |
fStatsLayer.beginTiming(fPaintTimer); |
| 1982 |
if (fTiled) { |
| 1983 |
int tileW = SkScalarCeilToInt(fWindow->width() * fTileScale.width()); |
| 1984 |
int tileH = SkScalarCeilToInt(fWindow->height() * fTileScale.height()); |
| 1985 |
for (int y = 0; y < fWindow->height(); y += tileH) { |
| 1986 |
for (int x = 0; x < fWindow->width(); x += tileW) { |
| 1987 |
SkAutoCanvasRestore acr(slideCanvas, true); |
| 1988 |
|
| 1989 |
SkSurfaceProps props; |
| 1990 |
if (!slideCanvas->getProps(&props)) { |
| 1991 |
props = fWindow->getRequestedDisplayParams()->surfaceProps(); |
| 1992 |
} |
| 1993 |
|
| 1994 |
SkImageInfo info = SkImageInfo::Make( |
| 1995 |
tileW, tileH, colorType, kPremul_SkAlphaType, colorSpace); |
| 1996 |
sk_sp<SkSurface> tileSurface = Window::BackendType::kRaster == this->fBackendType |
| 1997 |
? SkSurfaces::Raster(info, &props) |
| 1998 |
: slideCanvas->makeSurface(info, &props); |
| 1999 |
SkCanvas* tileCanvas = tileSurface->getCanvas(); |
| 2000 |
tileCanvas->setMatrix(slideCanvas->getLocalToDevice()); |
| 2001 |
tileCanvas->translate(-x, -y); |
| 2002 |
fSlides[fCurrentSlide]->draw(tileCanvas); |
| 2003 |
|
| 2004 |
sk_sp<SkImage> tileImage = tileSurface->makeImageSnapshot(); |
| 2005 |
SkPaint paint; |
| 2006 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 2007 |
SkSamplingOptions sampling; |
| 2008 |
slideCanvas->drawImage(tileImage, x, y, sampling, &paint); |
| 2009 |
} |
| 2010 |
} |
| 2011 |
|
| 2012 |
// Draw borders between tiles |
| 2013 |
if (fDrawTileBoundaries) { |
| 2014 |
SkPaint border; |
| 2015 |
border.setColor(0x60FF00FF); |
| 2016 |
border.setStyle(SkPaint::kStroke_Style); |
| 2017 |
for (int y = 0; y < fWindow->height(); y += tileH) { |
| 2018 |
for (int x = 0; x < fWindow->width(); x += tileW) { |
| 2019 |
slideCanvas->drawRect(SkRect::MakeXYWH(x, y, tileW, tileH), border); |
| 2020 |
} |
| 2021 |
} |
| 2022 |
} |
| 2023 |
} else { |
| 2024 |
slideCanvas->concat(this->computeMatrix()); |
| 2025 |
if (fPaintOverrides.overridesSomething() || fFontOverrides.overridesSomething()) { |
| 2026 |
OveridePaintFilterCanvas filterCanvas(slideCanvas, |
| 2027 |
&fPaint, &fPaintOverrides, |
| 2028 |
&fFont, &fFontOverrides); |
| 2029 |
fSlides[fCurrentSlide]->draw(&filterCanvas); |
| 2030 |
} else { |
| 2031 |
fSlides[fCurrentSlide]->draw(slideCanvas); |
| 2032 |
} |
| 2033 |
} |
| 2034 |
#if defined(SK_GRAPHITE) |
| 2035 |
// Finish flushing Tasks to Recorder |
| 2036 |
skgpu::graphite::Flush(slideSurface); |
| 2037 |
#endif |
| 2038 |
fStatsLayer.endTiming(fPaintTimer); |
| 2039 |
slideCanvas->restoreToCount(count); |
| 2040 |
|
| 2041 |
if (recorderRestoreCanvas) { |
| 2042 |
sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
| 2043 |
SkSerialProcs sProcs = serial_procs_using_png(); |
| 2044 |
auto data = picture->serialize(&sProcs); |
| 2045 |
slideCanvas = recorderRestoreCanvas; |
| 2046 |
SkDeserialProcs dProcs = ToolUtils::get_default_skp_deserial_procs(); |
| 2047 |
slideCanvas->drawPicture(SkPicture::MakeFromData(data.get(), &dProcs)); |
| 2048 |
} |
| 2049 |
|
| 2050 |
// Force a flush so we can time that and add a gpu timer. |
| 2051 |
fStatsLayer.beginTiming(fFlushTimer); |
| 2052 |
fWindow->submitToGpu(fStatsLayer.issueGpuTimer()); |
| 2053 |
fStatsLayer.endTiming(fFlushTimer); |
| 2054 |
|
| 2055 |
// If we rendered offscreen, snap an image and push the results to the window's canvas |
| 2056 |
if (offscreenSurface) { |
| 2057 |
fLastImage = offscreenSurface->makeImageSnapshot(); |
| 2058 |
|
| 2059 |
SkCanvas* canvas = surface->getCanvas(); |
| 2060 |
SkPaint paint; |
| 2061 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 2062 |
SkSamplingOptions sampling; |
| 2063 |
int prePerspectiveCount = canvas->save(); |
| 2064 |
if (kPerspective_Fake == fPerspectiveMode) { |
| 2065 |
sampling = SkSamplingOptions({1.0f/3, 1.0f/3}); |
| 2066 |
canvas->clear(SK_ColorWHITE); |
| 2067 |
canvas->concat(this->computePerspectiveMatrix()); |
| 2068 |
} |
| 2069 |
canvas->drawImage(fLastImage, 0, 0, sampling, &paint); |
| 2070 |
canvas->restoreToCount(prePerspectiveCount); |
| 2071 |
} |
| 2072 |
|
| 2073 |
if (fShowSlideDimensions) { |
| 2074 |
SkCanvas* canvas = surface->getCanvas(); |
| 2075 |
SkAutoCanvasRestore acr(canvas, true); |
| 2076 |
canvas->concat(this->computeMatrix()); |
| 2077 |
SkRect r = SkRect::Make(this->currentSlideSize()); |
| 2078 |
SkPaint paint; |
| 2079 |
paint.setColor(0x40FFFF00); |
| 2080 |
canvas->drawRect(r, paint); |
| 2081 |
} |
| 2082 |
|
| 2083 |
// Allow drawing to update the slide bounds. |
| 2084 |
this->updateGestureTransLimit(); |
| 2085 |
} |
| 2086 |
|
| 2087 |
void Viewer::onBackendCreated() { |
| 2088 |
this->setupCurrentSlide(); |
| 2089 |
fWindow->show(); |
| 2090 |
} |
| 2091 |
|
| 2092 |
void Viewer::onPaint(SkSurface* surface) { |
| 2093 |
this->drawSlide(surface); |
| 2094 |
|
| 2095 |
fCommands.drawHelp(surface->getCanvas()); |
| 2096 |
|
| 2097 |
this->drawImGui(); |
| 2098 |
|
| 2099 |
fLastImage.reset(); |
| 2100 |
|
| 2101 |
#if defined(SK_GANESH) |
| 2102 |
if (auto direct = fWindow->directContext()) { |
| 2103 |
// Clean out cache items that haven't been used in more than 10 seconds. |
| 2104 |
direct->performDeferredCleanup(std::chrono::seconds(10)); |
| 2105 |
} |
| 2106 |
#endif |
| 2107 |
} |
| 2108 |
|
| 2109 |
void Viewer::resizeCurrentSlide(int width, int height) { |
| 2110 |
if (fCurrentSlide >= 0) { |
| 2111 |
// Resizing can reset the context on some backends so just tear it all down. |
| 2112 |
// We'll rebuild these resources on the next draw. |
| 2113 |
fSlides[fCurrentSlide]->gpuTeardown(); |
| 2114 |
|
| 2115 |
SkScalar scaleFactor = 1.0; |
| 2116 |
if (fApplyBackingScale) { |
| 2117 |
scaleFactor = fWindow->scaleFactor(); |
| 2118 |
} |
| 2119 |
fSlides[fCurrentSlide]->resize(width / scaleFactor, height / scaleFactor); |
| 2120 |
} |
| 2121 |
} |
| 2122 |
|
| 2123 |
void Viewer::onResize(int width, int height) { |
| 2124 |
this->resizeCurrentSlide(width, height); |
| 2125 |
fImGuiLayer.setScaleFactor(fWindow->scaleFactor()); |
| 2126 |
fStatsLayer.setDisplayScale((fZoomUI ? 2.0f : 1.0f) * fWindow->scaleFactor()); |
| 2127 |
} |
| 2128 |
|
| 2129 |
SkPoint Viewer::mapEvent(float x, float y) { |
| 2130 |
const auto m = this->computeMatrix(); |
| 2131 |
SkMatrix inv; |
| 2132 |
|
| 2133 |
SkAssertResult(m.invert(&inv)); |
| 2134 |
|
| 2135 |
return inv.mapPoint({x, y}); |
| 2136 |
} |
| 2137 |
|
| 2138 |
bool Viewer::onTouch(intptr_t owner, skui::InputState state, float x, float y) { |
| 2139 |
if (GestureDevice::kMouse == fGestureDevice) { |
| 2140 |
return false; |
| 2141 |
} |
| 2142 |
|
| 2143 |
const auto slidePt = this->mapEvent(x, y); |
| 2144 |
if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, skui::ModifierKey::kNone)) { |
| 2145 |
fWindow->inval(); |
| 2146 |
return true; |
| 2147 |
} |
| 2148 |
|
| 2149 |
void* castedOwner = reinterpret_cast<void*>(owner); |
| 2150 |
switch (state) { |
| 2151 |
case skui::InputState::kUp: { |
| 2152 |
fGesture.touchEnd(castedOwner); |
| 2153 |
#if defined(SK_BUILD_FOR_IOS) |
| 2154 |
// TODO: move IOS swipe detection higher up into the platform code |
| 2155 |
SkPoint dir; |
| 2156 |
if (fGesture.isFling(&dir)) { |
| 2157 |
// swiping left or right |
| 2158 |
if (SkTAbs(dir.fX) > SkTAbs(dir.fY)) { |
| 2159 |
if (dir.fX < 0) { |
| 2160 |
this->setCurrentSlide(fCurrentSlide < fSlides.size() - 1 ? |
| 2161 |
fCurrentSlide + 1 : 0); |
| 2162 |
} else { |
| 2163 |
this->setCurrentSlide(fCurrentSlide > 0 ? |
| 2164 |
fCurrentSlide - 1 : fSlides.size() - 1); |
| 2165 |
} |
| 2166 |
} |
| 2167 |
fGesture.reset(); |
| 2168 |
} |
| 2169 |
#endif |
| 2170 |
break; |
| 2171 |
} |
| 2172 |
case skui::InputState::kDown: { |
| 2173 |
fGesture.touchBegin(castedOwner, x, y); |
| 2174 |
break; |
| 2175 |
} |
| 2176 |
case skui::InputState::kMove: { |
| 2177 |
fGesture.touchMoved(castedOwner, x, y); |
| 2178 |
break; |
| 2179 |
} |
| 2180 |
default: { |
| 2181 |
// kLeft and kRight are only for swipes |
| 2182 |
SkASSERT(false); |
| 2183 |
break; |
| 2184 |
} |
| 2185 |
} |
| 2186 |
fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kTouch : GestureDevice::kNone; |
| 2187 |
fWindow->inval(); |
| 2188 |
return true; |
| 2189 |
} |
| 2190 |
|
| 2191 |
bool Viewer::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) { |
| 2192 |
if (GestureDevice::kTouch == fGestureDevice) { |
| 2193 |
return false; |
| 2194 |
} |
| 2195 |
|
| 2196 |
const auto slidePt = this->mapEvent(x, y); |
| 2197 |
if (fSlides[fCurrentSlide]->onMouse(slidePt.x(), slidePt.y(), state, modifiers)) { |
| 2198 |
fWindow->inval(); |
| 2199 |
return true; |
| 2200 |
} |
| 2201 |
|
| 2202 |
switch (state) { |
| 2203 |
case skui::InputState::kUp: { |
| 2204 |
fGesture.touchEnd(nullptr); |
| 2205 |
break; |
| 2206 |
} |
| 2207 |
case skui::InputState::kDown: { |
| 2208 |
fGesture.touchBegin(nullptr, x, y); |
| 2209 |
break; |
| 2210 |
} |
| 2211 |
case skui::InputState::kMove: { |
| 2212 |
fGesture.touchMoved(nullptr, x, y); |
| 2213 |
break; |
| 2214 |
} |
| 2215 |
default: { |
| 2216 |
SkASSERT(false); // shouldn't see kRight or kLeft here |
| 2217 |
break; |
| 2218 |
} |
| 2219 |
} |
| 2220 |
fGestureDevice = fGesture.isBeingTouched() ? GestureDevice::kMouse : GestureDevice::kNone; |
| 2221 |
|
| 2222 |
if (state != skui::InputState::kMove || fGesture.isBeingTouched()) { |
| 2223 |
fWindow->inval(); |
| 2224 |
} |
| 2225 |
return true; |
| 2226 |
} |
| 2227 |
|
| 2228 |
bool Viewer::onMouseWheel(float delta, int x, int y, skui::ModifierKey) { |
| 2229 |
// Rather than updating the fixed zoom level, treat a mouse wheel event as a gesture, which |
| 2230 |
// applies a pre- and post-translation to the transform, resulting in a zoom effect centered at |
| 2231 |
// the mouse cursor position. |
| 2232 |
SkScalar scale = exp(delta * 0.001); |
| 2233 |
fGesture.startZoom(); |
| 2234 |
fGesture.updateZoom(scale, x, y, x, y); |
| 2235 |
fGesture.endZoom(); |
| 2236 |
fWindow->inval(); |
| 2237 |
return true; |
| 2238 |
} |
| 2239 |
|
| 2240 |
bool Viewer::onFling(skui::InputState state) { |
| 2241 |
if (skui::InputState::kRight == state) { |
| 2242 |
this->setCurrentSlide(fCurrentSlide > 0 ? fCurrentSlide - 1 : fSlides.size() - 1); |
| 2243 |
return true; |
| 2244 |
} else if (skui::InputState::kLeft == state) { |
| 2245 |
this->setCurrentSlide(fCurrentSlide < fSlides.size() - 1 ? fCurrentSlide + 1 : 0); |
| 2246 |
return true; |
| 2247 |
} |
| 2248 |
return false; |
| 2249 |
} |
| 2250 |
|
| 2251 |
bool Viewer::onPinch(skui::InputState state, float scale, float x, float y) { |
| 2252 |
switch (state) { |
| 2253 |
case skui::InputState::kDown: |
| 2254 |
fGesture.startZoom(); |
| 2255 |
return true; |
| 2256 |
case skui::InputState::kMove: |
| 2257 |
fGesture.updateZoom(scale, x, y, x, y); |
| 2258 |
return true; |
| 2259 |
case skui::InputState::kUp: |
| 2260 |
fGesture.endZoom(); |
| 2261 |
return true; |
| 2262 |
default: |
| 2263 |
SkASSERT(false); |
| 2264 |
break; |
| 2265 |
} |
| 2266 |
|
| 2267 |
return false; |
| 2268 |
} |
| 2269 |
|
| 2270 |
static void ImGui_Primaries(SkColorSpacePrimaries* primaries, SkPaint* gamutPaint) { |
| 2271 |
// The gamut image covers a (0.8 x 0.9) shaped region |
| 2272 |
ImGui::DragCanvas dc(primaries, { 0.0f, 0.9f }, { 0.8f, 0.0f }); |
| 2273 |
|
| 2274 |
// Background image. Only draw a subset of the image, to avoid the regions less than zero. |
| 2275 |
// Simplifes re-mapping math, clipping behavior, and increases resolution in the useful area. |
| 2276 |
// Magic numbers are pixel locations of the origin and upper-right corner. |
| 2277 |
dc.fDrawList->AddImage(gamutPaint, dc.fPos, |
| 2278 |
ImVec2(dc.fPos.x + dc.fSize.x, dc.fPos.y + dc.fSize.y), |
| 2279 |
ImVec2(242, 61), ImVec2(1897, 1922)); |
| 2280 |
|
| 2281 |
dc.dragPoint((SkPoint*)(&primaries->fRX), true, 0xFF000040); |
| 2282 |
dc.dragPoint((SkPoint*)(&primaries->fGX), true, 0xFF004000); |
| 2283 |
dc.dragPoint((SkPoint*)(&primaries->fBX), true, 0xFF400000); |
| 2284 |
dc.dragPoint((SkPoint*)(&primaries->fWX), true); |
| 2285 |
dc.fDrawList->AddPolyline(dc.fScreenPoints.begin(), 3, 0xFFFFFFFF, true, 1.5f); |
| 2286 |
} |
| 2287 |
|
| 2288 |
static bool ImGui_DragLocation(SkPoint* pt) { |
| 2289 |
ImGui::DragCanvas dc(pt); |
| 2290 |
dc.fillColor(IM_COL32(0, 0, 0, 128)); |
| 2291 |
dc.dragPoint(pt); |
| 2292 |
return dc.fDragging; |
| 2293 |
} |
| 2294 |
|
| 2295 |
static bool ImGui_DragQuad(SkPoint* pts) { |
| 2296 |
ImGui::DragCanvas dc(pts); |
| 2297 |
dc.fillColor(IM_COL32(0, 0, 0, 128)); |
| 2298 |
|
| 2299 |
for (int i = 0; i < 4; ++i) { |
| 2300 |
dc.dragPoint(pts + i); |
| 2301 |
} |
| 2302 |
|
| 2303 |
dc.fDrawList->AddLine(dc.fScreenPoints[0], dc.fScreenPoints[1], 0xFFFFFFFF); |
| 2304 |
dc.fDrawList->AddLine(dc.fScreenPoints[1], dc.fScreenPoints[3], 0xFFFFFFFF); |
| 2305 |
dc.fDrawList->AddLine(dc.fScreenPoints[3], dc.fScreenPoints[2], 0xFFFFFFFF); |
| 2306 |
dc.fDrawList->AddLine(dc.fScreenPoints[2], dc.fScreenPoints[0], 0xFFFFFFFF); |
| 2307 |
|
| 2308 |
return dc.fDragging; |
| 2309 |
} |
| 2310 |
|
| 2311 |
#if defined(SK_GANESH) |
| 2312 |
|
| 2313 |
static std::string build_sksl_highlight_shader() { |
| 2314 |
return std::string("void main() { sk_FragColor = half4(1, 0, 1, 0.5); }"); |
| 2315 |
} |
| 2316 |
|
| 2317 |
static std::string build_metal_highlight_shader(const std::string& inShader) { |
| 2318 |
// Metal fragment shaders need a lot of non-trivial boilerplate that we don't want to recompute |
| 2319 |
// here. So keep all shader code, but right before `return *_out;`, swap out the sk_FragColor. |
| 2320 |
size_t pos = inShader.rfind("return _out;\n"); |
| 2321 |
if (pos == std::string::npos) { |
| 2322 |
return inShader; |
| 2323 |
} |
| 2324 |
|
| 2325 |
std::string replacementShader = inShader; |
| 2326 |
replacementShader.insert(pos, "_out.sk_FragColor = float4(1.0, 0.0, 1.0, 0.5); "); |
| 2327 |
return replacementShader; |
| 2328 |
} |
| 2329 |
|
| 2330 |
static std::string build_glsl_highlight_shader(const GrShaderCaps& shaderCaps) { |
| 2331 |
const char* versionDecl = shaderCaps.fVersionDeclString; |
| 2332 |
std::string highlight = versionDecl ? versionDecl : ""; |
| 2333 |
if (shaderCaps.fUsesPrecisionModifiers) { |
| 2334 |
highlight.append("precision mediump float;\n"); |
| 2335 |
} |
| 2336 |
SkSL::String::appendf(&highlight, "out vec4 sk_FragColor;\n" |
| 2337 |
"void main() { sk_FragColor = vec4(1, 0, 1, 0.5); }"); |
| 2338 |
return highlight; |
| 2339 |
} |
| 2340 |
#endif |
| 2341 |
|
| 2342 |
void Viewer::drawImGui() { |
| 2343 |
// Support drawing the ImGui demo window. Superfluous, but gives a good idea of what's possible |
| 2344 |
if (fShowImGuiTestWindow) { |
| 2345 |
ImGui::ShowDemoWindow(&fShowImGuiTestWindow); |
| 2346 |
} |
| 2347 |
|
| 2348 |
if (fShowImGuiDebugWindow) { |
| 2349 |
// We have some dynamic content that sizes to fill available size. If the scroll bar isn't |
| 2350 |
// always visible, we can end up in a layout feedback loop. |
| 2351 |
ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver); |
| 2352 |
const DisplayParams* params = fWindow->getRequestedDisplayParams(); |
| 2353 |
auto newParamsBuilder = make_display_params_builder(params); |
| 2354 |
bool displayParamsChanged = false; // heavy-weight, might recreate entire context |
| 2355 |
bool uiParamsChanged = false; // light weight, just triggers window invalidation |
| 2356 |
GrDirectContext* ctx = fWindow->directContext(); |
| 2357 |
|
| 2358 |
if (ImGui::Begin("Tools", &fShowImGuiDebugWindow, |
| 2359 |
ImGuiWindowFlags_AlwaysVerticalScrollbar)) { |
| 2360 |
if (ImGui::CollapsingHeader("Backend")) { |
| 2361 |
int newBackend = static_cast<int>(fBackendType); |
| 2362 |
ImGui::RadioButton("Raster", |
| 2363 |
&newBackend, |
| 2364 |
static_cast<int>(sk_app::Window::BackendType::kRaster)); |
| 2365 |
ImGui::SameLine(); |
| 2366 |
ImGui::RadioButton("OpenGL", |
| 2367 |
&newBackend, |
| 2368 |
static_cast<int>(sk_app::Window::BackendType::kNativeGL)); |
| 2369 |
#if defined(SK_ANGLE) && (defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)) |
| 2370 |
ImGui::SameLine(); |
| 2371 |
ImGui::RadioButton("ANGLE", |
| 2372 |
&newBackend, |
| 2373 |
static_cast<int>(sk_app::Window::BackendType::kANGLE)); |
| 2374 |
#endif |
| 2375 |
|
| 2376 |
#if defined(SK_DAWN) && defined(SK_GRAPHITE) |
| 2377 |
#if defined(SK_BUILD_FOR_WIN) |
| 2378 |
ImGui::SameLine(); |
| 2379 |
ImGui::RadioButton( |
| 2380 |
"Dawn D3D12 (Graphite)", |
| 2381 |
&newBackend, |
| 2382 |
static_cast<int>(sk_app::Window::BackendType::kGraphiteDawnD3D12)); |
| 2383 |
#endif |
| 2384 |
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 2385 |
ImGui::SameLine(); |
| 2386 |
ImGui::RadioButton( |
| 2387 |
"Dawn Metal (Graphite)", |
| 2388 |
&newBackend, |
| 2389 |
static_cast<int>(sk_app::Window::BackendType::kGraphiteDawnMetal)); |
| 2390 |
#endif |
| 2391 |
#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) |
| 2392 |
ImGui::SameLine(); |
| 2393 |
ImGui::RadioButton( |
| 2394 |
"Dawn OpenGLES (Graphite)", |
| 2395 |
&newBackend, |
| 2396 |
static_cast<int>(sk_app::Window::BackendType::kGraphiteDawnOpenGLES)); |
| 2397 |
ImGui::SameLine(); |
| 2398 |
ImGui::RadioButton( |
| 2399 |
"Dawn Vulkan (Graphite)", |
| 2400 |
&newBackend, |
| 2401 |
static_cast<int>(sk_app::Window::BackendType::kGraphiteDawnVulkan)); |
| 2402 |
#endif |
| 2403 |
#endif |
| 2404 |
|
| 2405 |
|
| 2406 |
#if defined(SK_VULKAN) && !defined(SK_BUILD_FOR_MAC) |
| 2407 |
# if defined(SK_GANESH) |
| 2408 |
ImGui::SameLine(); |
| 2409 |
ImGui::RadioButton("Vulkan", |
| 2410 |
&newBackend, |
| 2411 |
static_cast<int>(sk_app::Window::BackendType::kVulkan)); |
| 2412 |
# endif |
| 2413 |
# if defined(SK_GRAPHITE) |
| 2414 |
ImGui::SameLine(); |
| 2415 |
ImGui::RadioButton("Vulkan (Graphite)", |
| 2416 |
&newBackend, |
| 2417 |
static_cast<int>(sk_app::Window::BackendType::kGraphiteVulkan)); |
| 2418 |
# endif |
| 2419 |
#endif |
| 2420 |
|
| 2421 |
#if defined(SK_METAL) |
| 2422 |
# if defined(SK_GANESH) |
| 2423 |
ImGui::SameLine(); |
| 2424 |
ImGui::RadioButton("Metal", |
| 2425 |
&newBackend, |
| 2426 |
static_cast<int>(sk_app::Window::BackendType::kMetal)); |
| 2427 |
# endif |
| 2428 |
# if defined(SK_GRAPHITE) |
| 2429 |
ImGui::SameLine(); |
| 2430 |
ImGui::RadioButton("Metal (Graphite)", |
| 2431 |
&newBackend, |
| 2432 |
static_cast<int>(sk_app::Window::BackendType::kGraphiteMetal)); |
| 2433 |
# endif |
| 2434 |
#endif |
| 2435 |
|
| 2436 |
#if defined(SK_DIRECT3D) && defined(SK_GANESH) |
| 2437 |
ImGui::SameLine(); |
| 2438 |
ImGui::RadioButton("Direct3D", |
| 2439 |
&newBackend, |
| 2440 |
static_cast<int>(sk_app::Window::BackendType::kDirect3D)); |
| 2441 |
#endif |
| 2442 |
|
| 2443 |
if (newBackend != static_cast<int>(fBackendType)) { |
| 2444 |
fDeferredActions.push_back([newBackend, this]() { |
| 2445 |
this->setBackend(static_cast<sk_app::Window::BackendType>(newBackend)); |
| 2446 |
}); |
| 2447 |
} |
| 2448 |
|
| 2449 |
#if defined(SK_GANESH) |
| 2450 |
if (ctx) { |
| 2451 |
GrContextOptions grOpts = params->grContextOptions(); |
| 2452 |
if (ImGui::Checkbox("Wireframe Mode", &grOpts.fWireframeMode)) { |
| 2453 |
displayParamsChanged = true; |
| 2454 |
newParamsBuilder.grContextOptions(grOpts); |
| 2455 |
} |
| 2456 |
|
| 2457 |
if (ImGui::Checkbox("Reduced shaders", &grOpts.fReducedShaderVariations)) { |
| 2458 |
displayParamsChanged = true; |
| 2459 |
newParamsBuilder.grContextOptions(grOpts); |
| 2460 |
} |
| 2461 |
|
| 2462 |
// Determine the context's max sample count for MSAA radio buttons. |
| 2463 |
int sampleCount = fWindow->sampleCount(); |
| 2464 |
int maxMSAA = |
| 2465 |
(fBackendType != sk_app::Window::BackendType::kRaster) |
| 2466 |
? ctx->maxSurfaceSampleCountForColorType(kRGBA_8888_SkColorType) |
| 2467 |
: 1; |
| 2468 |
|
| 2469 |
// Only display the MSAA radio buttons when there are options above 1x MSAA. |
| 2470 |
if (maxMSAA >= 4) { |
| 2471 |
ImGui::Text("MSAA: "); |
| 2472 |
|
| 2473 |
for (int curMSAA = 1; curMSAA <= maxMSAA; curMSAA *= 2) { |
| 2474 |
// 2x MSAA works, but doesn't offer much of a visual improvement, so we |
| 2475 |
// don't show it in the list. |
| 2476 |
if (curMSAA == 2) { |
| 2477 |
continue; |
| 2478 |
} |
| 2479 |
ImGui::SameLine(); |
| 2480 |
ImGui::RadioButton(SkStringPrintf("%d", curMSAA).c_str(), |
| 2481 |
&sampleCount, curMSAA); |
| 2482 |
} |
| 2483 |
} |
| 2484 |
|
| 2485 |
if (sampleCount != params->msaaSampleCount()) { |
| 2486 |
displayParamsChanged = true; |
| 2487 |
newParamsBuilder.msaaSampleCount(sampleCount); |
| 2488 |
} |
| 2489 |
} |
| 2490 |
#endif |
| 2491 |
|
| 2492 |
int pixelGeometryIdx = 0; |
| 2493 |
if (fDisplayOverrides.fSurfaceProps.fPixelGeometry) { |
| 2494 |
pixelGeometryIdx = params->surfaceProps().pixelGeometry() + 1; |
| 2495 |
} |
| 2496 |
if (ImGui::Combo("Pixel Geometry", &pixelGeometryIdx, |
| 2497 |
"Default\0Flat\0RGB\0BGR\0RGBV\0BGRV\0\0")) |
| 2498 |
{ |
| 2499 |
uint32_t flags = params->surfaceProps().flags(); |
| 2500 |
if (pixelGeometryIdx == 0) { |
| 2501 |
fDisplayOverrides.fSurfaceProps.fPixelGeometry = false; |
| 2502 |
SkPixelGeometry pixelGeometry = fDisplay->surfaceProps().pixelGeometry(); |
| 2503 |
newParamsBuilder.surfaceProps(SkSurfaceProps(flags, pixelGeometry)); |
| 2504 |
} else { |
| 2505 |
fDisplayOverrides.fSurfaceProps.fPixelGeometry = true; |
| 2506 |
SkPixelGeometry pixelGeometry = SkTo<SkPixelGeometry>(pixelGeometryIdx - 1); |
| 2507 |
newParamsBuilder.surfaceProps(SkSurfaceProps(flags, pixelGeometry)); |
| 2508 |
} |
| 2509 |
displayParamsChanged = true; |
| 2510 |
} |
| 2511 |
|
| 2512 |
bool useDFT = params->surfaceProps().isUseDeviceIndependentFonts(); |
| 2513 |
if (ImGui::Checkbox("DFT", &useDFT)) { |
| 2514 |
uint32_t flags = params->surfaceProps().flags(); |
| 2515 |
if (useDFT) { |
| 2516 |
flags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag; |
| 2517 |
} else { |
| 2518 |
flags &= ~SkSurfaceProps::kUseDeviceIndependentFonts_Flag; |
| 2519 |
} |
| 2520 |
SkPixelGeometry pixelGeometry = params->surfaceProps().pixelGeometry(); |
| 2521 |
newParamsBuilder.surfaceProps(SkSurfaceProps(flags, pixelGeometry)); |
| 2522 |
displayParamsChanged = true; |
| 2523 |
} |
| 2524 |
|
| 2525 |
if (ImGui::TreeNode("Path Renderers")) { |
| 2526 |
#if defined(SK_GRAPHITE) |
| 2527 |
skgpu::graphite::Context* gctx = fWindow->graphiteContext(); |
| 2528 |
if (is_graphite_backend_type(fBackendType) && gctx) { |
| 2529 |
using skgpu::graphite::PathRendererStrategy; |
| 2530 |
SkASSERT(params->graphiteTestOptions()); |
| 2531 |
skwindow::GraphiteTestOptions opts = *params->graphiteTestOptions(); |
| 2532 |
auto prevPrs = opts.fPriv.fPathRendererStrategy; |
| 2533 |
auto prsButton = |
| 2534 |
[&](std::optional<skgpu::graphite::PathRendererStrategy> s) { |
| 2535 |
if (ImGui::RadioButton(get_path_renderer_strategy_string(s), |
| 2536 |
prevPrs == s)) { |
| 2537 |
if (s != opts.fPriv.fPathRendererStrategy) { |
| 2538 |
opts.fPriv.fPathRendererStrategy = s; |
| 2539 |
newParamsBuilder.graphiteTestOptions(opts); |
| 2540 |
displayParamsChanged = true; |
| 2541 |
} |
| 2542 |
} |
| 2543 |
}; |
| 2544 |
|
| 2545 |
prsButton({}); // "Default" |
| 2546 |
|
| 2547 |
PathRendererStrategy strategies[] = { |
| 2548 |
PathRendererStrategy::kComputeAnalyticAA, |
| 2549 |
PathRendererStrategy::kComputeMSAA16, |
| 2550 |
PathRendererStrategy::kComputeMSAA8, |
| 2551 |
PathRendererStrategy::kRasterAtlas, |
| 2552 |
PathRendererStrategy::kTessellation, |
| 2553 |
PathRendererStrategy::kTessellationAndSmallAtlas, |
| 2554 |
}; |
| 2555 |
for (size_t i = 0; i < std::size(strategies); ++i) { |
| 2556 |
if (skgpu::graphite::RendererProvider::IsSupported( |
| 2557 |
strategies[i], gctx->priv().caps())) { |
| 2558 |
prsButton(strategies[i]); |
| 2559 |
} |
| 2560 |
} |
| 2561 |
} else |
| 2562 |
#endif |
| 2563 |
#if defined(SK_GANESH) |
| 2564 |
if (fBackendType != Window::BackendType::kRaster && ctx) { |
| 2565 |
GrContextOptions grOpts = params->grContextOptions(); |
| 2566 |
auto prButton = [&](GpuPathRenderers x) { |
| 2567 |
if (ImGui::RadioButton(gGaneshPathRendererNames[x].c_str(), |
| 2568 |
grOpts.fGpuPathRenderers == x)) { |
| 2569 |
if (x != grOpts.fGpuPathRenderers) { |
| 2570 |
grOpts.fGpuPathRenderers = x; |
| 2571 |
displayParamsChanged = true; |
| 2572 |
newParamsBuilder.grContextOptions(grOpts); |
| 2573 |
} |
| 2574 |
} |
| 2575 |
}; |
| 2576 |
|
| 2577 |
prButton(GpuPathRenderers::kDefault); |
| 2578 |
if (fWindow->sampleCount() > 1 || FLAGS_dmsaa) { |
| 2579 |
const auto* caps = ctx->priv().caps(); |
| 2580 |
if (skgpu::ganesh::AtlasPathRenderer::IsSupported(ctx)) { |
| 2581 |
prButton(GpuPathRenderers::kAtlas); |
| 2582 |
} |
| 2583 |
if (skgpu::ganesh::TessellationPathRenderer::IsSupported(*caps)) { |
| 2584 |
prButton(GpuPathRenderers::kTessellation); |
| 2585 |
} |
| 2586 |
} |
| 2587 |
if (1 == fWindow->sampleCount()) { |
| 2588 |
prButton(GpuPathRenderers::kSmall); |
| 2589 |
} |
| 2590 |
prButton(GpuPathRenderers::kTriangulating); |
| 2591 |
prButton(GpuPathRenderers::kNone); |
| 2592 |
} else |
| 2593 |
#endif |
| 2594 |
{ |
| 2595 |
ImGui::RadioButton("Software", true); |
| 2596 |
} |
| 2597 |
ImGui::TreePop(); |
| 2598 |
} |
| 2599 |
} |
| 2600 |
|
| 2601 |
if (ImGui::CollapsingHeader("Tiling")) { |
| 2602 |
ImGui::Checkbox("Enable", &fTiled); |
| 2603 |
ImGui::Checkbox("Draw Boundaries", &fDrawTileBoundaries); |
| 2604 |
ImGui::SliderFloat("Horizontal", &fTileScale.fWidth, 0.1f, 1.0f); |
| 2605 |
ImGui::SliderFloat("Vertical", &fTileScale.fHeight, 0.1f, 1.0f); |
| 2606 |
} |
| 2607 |
|
| 2608 |
if (ImGui::CollapsingHeader("Transform")) { |
| 2609 |
if (ImGui::Checkbox("Apply Backing Scale", &fApplyBackingScale)) { |
| 2610 |
this->updateGestureTransLimit(); |
| 2611 |
// NOTE: Do not call onResize() as we are in the middle of ImgGui processing, |
| 2612 |
// and onResize() modifies ImgGui state that is otherwise locked. The backing |
| 2613 |
// scale factor only affects the slide itself, so adjust that directly. |
| 2614 |
this->resizeCurrentSlide(fWindow->width(), fWindow->height()); |
| 2615 |
// This changes how we manipulate the canvas transform, it's not changing the |
| 2616 |
// window's actual parameters. |
| 2617 |
uiParamsChanged = true; |
| 2618 |
} |
| 2619 |
|
| 2620 |
float zoom = fZoomLevel; |
| 2621 |
if (ImGui::SliderFloat("Zoom", &zoom, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) { |
| 2622 |
fZoomLevel = zoom; |
| 2623 |
this->updateGestureTransLimit(); |
| 2624 |
uiParamsChanged = true; |
| 2625 |
} |
| 2626 |
float deg = fRotation; |
| 2627 |
if (ImGui::SliderFloat("Rotate", °, -30, 360, "%.3f deg")) { |
| 2628 |
fRotation = deg; |
| 2629 |
this->updateGestureTransLimit(); |
| 2630 |
uiParamsChanged = true; |
| 2631 |
} |
| 2632 |
if (ImGui::CollapsingHeader("Subpixel offset", ImGuiTreeNodeFlags_NoTreePushOnOpen)) { |
| 2633 |
if (ImGui_DragLocation(&fOffset)) { |
| 2634 |
this->updateGestureTransLimit(); |
| 2635 |
uiParamsChanged = true; |
| 2636 |
} |
| 2637 |
} else if (fOffset != SkVector{0.5f, 0.5f}) { |
| 2638 |
this->updateGestureTransLimit(); |
| 2639 |
uiParamsChanged = true; |
| 2640 |
fOffset = {0.5f, 0.5f}; |
| 2641 |
} |
| 2642 |
int perspectiveMode = static_cast<int>(fPerspectiveMode); |
| 2643 |
if (ImGui::Combo("Perspective", &perspectiveMode, "Off\0Real\0Fake\0\0")) { |
| 2644 |
fPerspectiveMode = static_cast<PerspectiveMode>(perspectiveMode); |
| 2645 |
this->updateGestureTransLimit(); |
| 2646 |
uiParamsChanged = true; |
| 2647 |
} |
| 2648 |
if (perspectiveMode != kPerspective_Off && ImGui_DragQuad(fPerspectivePoints)) { |
| 2649 |
this->updateGestureTransLimit(); |
| 2650 |
uiParamsChanged = true; |
| 2651 |
} |
| 2652 |
} |
| 2653 |
|
| 2654 |
if (ImGui::CollapsingHeader("Paint")) { |
| 2655 |
auto paintFlag = [this, &uiParamsChanged](const char* label, const char* items, |
| 2656 |
bool SkPaintFields::* flag, |
| 2657 |
bool (SkPaint::* isFlag)() const, |
| 2658 |
void (SkPaint::* setFlag)(bool) ) |
| 2659 |
{ |
| 2660 |
int itemIndex = 0; |
| 2661 |
if (fPaintOverrides.*flag) { |
| 2662 |
itemIndex = (fPaint.*isFlag)() ? 2 : 1; |
| 2663 |
} |
| 2664 |
if (ImGui::Combo(label, &itemIndex, items)) { |
| 2665 |
if (itemIndex == 0) { |
| 2666 |
fPaintOverrides.*flag = false; |
| 2667 |
} else { |
| 2668 |
fPaintOverrides.*flag = true; |
| 2669 |
(fPaint.*setFlag)(itemIndex == 2); |
| 2670 |
} |
| 2671 |
uiParamsChanged = true; |
| 2672 |
} |
| 2673 |
}; |
| 2674 |
|
| 2675 |
paintFlag("Antialias", |
| 2676 |
"Default\0No AA\0AA\0\0", |
| 2677 |
&SkPaintFields::fAntiAlias, |
| 2678 |
&SkPaint::isAntiAlias, &SkPaint::setAntiAlias); |
| 2679 |
|
| 2680 |
paintFlag("Dither", |
| 2681 |
"Default\0No Dither\0Dither\0\0", |
| 2682 |
&SkPaintFields::fDither, |
| 2683 |
&SkPaint::isDither, &SkPaint::setDither); |
| 2684 |
|
| 2685 |
int styleIdx = 0; |
| 2686 |
if (fPaintOverrides.fStyle) { |
| 2687 |
styleIdx = SkTo<int>(fPaint.getStyle()) + 1; |
| 2688 |
} |
| 2689 |
if (ImGui::Combo("Style", &styleIdx, |
| 2690 |
"Default\0Fill\0Stroke\0Stroke and Fill\0\0")) |
| 2691 |
{ |
| 2692 |
if (styleIdx == 0) { |
| 2693 |
fPaintOverrides.fStyle = false; |
| 2694 |
fPaint.setStyle(SkPaint::kFill_Style); |
| 2695 |
} else { |
| 2696 |
fPaint.setStyle(SkTo<SkPaint::Style>(styleIdx - 1)); |
| 2697 |
fPaintOverrides.fStyle = true; |
| 2698 |
} |
| 2699 |
uiParamsChanged = true; |
| 2700 |
} |
| 2701 |
|
| 2702 |
ImGui::Checkbox("Force Runtime Blends", &fPaintOverrides.fForceRuntimeBlend); |
| 2703 |
|
| 2704 |
ImGui::Checkbox("Override Stroke Width", &fPaintOverrides.fStrokeWidth); |
| 2705 |
if (fPaintOverrides.fStrokeWidth) { |
| 2706 |
float width = fPaint.getStrokeWidth(); |
| 2707 |
if (ImGui::SliderFloat("Stroke Width", &width, 0, 20)) { |
| 2708 |
fPaint.setStrokeWidth(width); |
| 2709 |
uiParamsChanged = true; |
| 2710 |
} |
| 2711 |
} |
| 2712 |
|
| 2713 |
ImGui::Checkbox("Override Miter Limit", &fPaintOverrides.fMiterLimit); |
| 2714 |
if (fPaintOverrides.fMiterLimit) { |
| 2715 |
float miterLimit = fPaint.getStrokeMiter(); |
| 2716 |
if (ImGui::SliderFloat("Miter Limit", &miterLimit, 0, 20)) { |
| 2717 |
fPaint.setStrokeMiter(miterLimit); |
| 2718 |
uiParamsChanged = true; |
| 2719 |
} |
| 2720 |
} |
| 2721 |
|
| 2722 |
int capIdx = 0; |
| 2723 |
if (fPaintOverrides.fCapType) { |
| 2724 |
capIdx = SkTo<int>(fPaint.getStrokeCap()) + 1; |
| 2725 |
} |
| 2726 |
if (ImGui::Combo("Cap Type", &capIdx, |
| 2727 |
"Default\0Butt\0Round\0Square\0\0")) |
| 2728 |
{ |
| 2729 |
if (capIdx == 0) { |
| 2730 |
fPaintOverrides.fCapType = false; |
| 2731 |
fPaint.setStrokeCap(SkPaint::kDefault_Cap); |
| 2732 |
} else { |
| 2733 |
fPaint.setStrokeCap(SkTo<SkPaint::Cap>(capIdx - 1)); |
| 2734 |
fPaintOverrides.fCapType = true; |
| 2735 |
} |
| 2736 |
uiParamsChanged = true; |
| 2737 |
} |
| 2738 |
|
| 2739 |
int joinIdx = 0; |
| 2740 |
if (fPaintOverrides.fJoinType) { |
| 2741 |
joinIdx = SkTo<int>(fPaint.getStrokeJoin()) + 1; |
| 2742 |
} |
| 2743 |
if (ImGui::Combo("Join Type", &joinIdx, |
| 2744 |
"Default\0Miter\0Round\0Bevel\0\0")) |
| 2745 |
{ |
| 2746 |
if (joinIdx == 0) { |
| 2747 |
fPaintOverrides.fJoinType = false; |
| 2748 |
fPaint.setStrokeJoin(SkPaint::kDefault_Join); |
| 2749 |
} else { |
| 2750 |
fPaint.setStrokeJoin(SkTo<SkPaint::Join>(joinIdx - 1)); |
| 2751 |
fPaintOverrides.fJoinType = true; |
| 2752 |
} |
| 2753 |
uiParamsChanged = true; |
| 2754 |
} |
| 2755 |
} |
| 2756 |
|
| 2757 |
if (ImGui::CollapsingHeader("Font")) { |
| 2758 |
int hintingIdx = 0; |
| 2759 |
if (fFontOverrides.fHinting) { |
| 2760 |
hintingIdx = SkTo<int>(fFont.getHinting()) + 1; |
| 2761 |
} |
| 2762 |
if (ImGui::Combo("Hinting", &hintingIdx, |
| 2763 |
"Default\0None\0Slight\0Normal\0Full\0\0")) |
| 2764 |
{ |
| 2765 |
if (hintingIdx == 0) { |
| 2766 |
fFontOverrides.fHinting = false; |
| 2767 |
fFont.setHinting(SkFontHinting::kNone); |
| 2768 |
} else { |
| 2769 |
fFont.setHinting(SkTo<SkFontHinting>(hintingIdx - 1)); |
| 2770 |
fFontOverrides.fHinting = true; |
| 2771 |
} |
| 2772 |
uiParamsChanged = true; |
| 2773 |
} |
| 2774 |
|
| 2775 |
auto fontFlag = [this, &uiParamsChanged](const char* label, const char* items, |
| 2776 |
bool SkFontFields::* flag, |
| 2777 |
bool (SkFont::* isFlag)() const, |
| 2778 |
void (SkFont::* setFlag)(bool) ) |
| 2779 |
{ |
| 2780 |
int itemIndex = 0; |
| 2781 |
if (fFontOverrides.*flag) { |
| 2782 |
itemIndex = (fFont.*isFlag)() ? 2 : 1; |
| 2783 |
} |
| 2784 |
if (ImGui::Combo(label, &itemIndex, items)) { |
| 2785 |
if (itemIndex == 0) { |
| 2786 |
fFontOverrides.*flag = false; |
| 2787 |
} else { |
| 2788 |
fFontOverrides.*flag = true; |
| 2789 |
(fFont.*setFlag)(itemIndex == 2); |
| 2790 |
} |
| 2791 |
uiParamsChanged = true; |
| 2792 |
} |
| 2793 |
}; |
| 2794 |
|
| 2795 |
fontFlag("Fake Bold Glyphs", |
| 2796 |
"Default\0No Fake Bold\0Fake Bold\0\0", |
| 2797 |
&SkFontFields::fEmbolden, |
| 2798 |
&SkFont::isEmbolden, &SkFont::setEmbolden); |
| 2799 |
|
| 2800 |
fontFlag("Baseline Snapping", |
| 2801 |
"Default\0No Baseline Snapping\0Baseline Snapping\0\0", |
| 2802 |
&SkFontFields::fBaselineSnap, |
| 2803 |
&SkFont::isBaselineSnap, &SkFont::setBaselineSnap); |
| 2804 |
|
| 2805 |
fontFlag("Linear Text", |
| 2806 |
"Default\0No Linear Text\0Linear Text\0\0", |
| 2807 |
&SkFontFields::fLinearMetrics, |
| 2808 |
&SkFont::isLinearMetrics, &SkFont::setLinearMetrics); |
| 2809 |
|
| 2810 |
fontFlag("Subpixel Position Glyphs", |
| 2811 |
"Default\0Pixel Text\0Subpixel Text\0\0", |
| 2812 |
&SkFontFields::fSubpixel, |
| 2813 |
&SkFont::isSubpixel, &SkFont::setSubpixel); |
| 2814 |
|
| 2815 |
fontFlag("Embedded Bitmap Text", |
| 2816 |
"Default\0No Embedded Bitmaps\0Embedded Bitmaps\0\0", |
| 2817 |
&SkFontFields::fEmbeddedBitmaps, |
| 2818 |
&SkFont::isEmbeddedBitmaps, &SkFont::setEmbeddedBitmaps); |
| 2819 |
|
| 2820 |
fontFlag("Force Auto-Hinting", |
| 2821 |
"Default\0No Force Auto-Hinting\0Force Auto-Hinting\0\0", |
| 2822 |
&SkFontFields::fForceAutoHinting, |
| 2823 |
&SkFont::isForceAutoHinting, &SkFont::setForceAutoHinting); |
| 2824 |
|
| 2825 |
int edgingIdx = 0; |
| 2826 |
if (fFontOverrides.fEdging) { |
| 2827 |
edgingIdx = SkTo<int>(fFont.getEdging()) + 1; |
| 2828 |
} |
| 2829 |
if (ImGui::Combo("Edging", &edgingIdx, |
| 2830 |
"Default\0Alias\0Antialias\0Subpixel Antialias\0\0")) |
| 2831 |
{ |
| 2832 |
if (edgingIdx == 0) { |
| 2833 |
fFontOverrides.fEdging = false; |
| 2834 |
fFont.setEdging(SkFont::Edging::kAlias); |
| 2835 |
} else { |
| 2836 |
fFont.setEdging(SkTo<SkFont::Edging>(edgingIdx-1)); |
| 2837 |
fFontOverrides.fEdging = true; |
| 2838 |
} |
| 2839 |
uiParamsChanged = true; |
| 2840 |
} |
| 2841 |
|
| 2842 |
ImGui::Checkbox("Override Size", &fFontOverrides.fSize); |
| 2843 |
if (fFontOverrides.fSize) { |
| 2844 |
ImGui::DragFloat2("TextRange", fFontOverrides.fSizeRange, |
| 2845 |
0.001f, -10.0f, 300.0f, "%.6f", ImGuiSliderFlags_Logarithmic); |
| 2846 |
float textSize = fFont.getSize(); |
| 2847 |
if (ImGui::DragFloat("TextSize", &textSize, 0.001f, |
| 2848 |
fFontOverrides.fSizeRange[0], |
| 2849 |
fFontOverrides.fSizeRange[1], |
| 2850 |
"%.6f", ImGuiSliderFlags_Logarithmic)) |
| 2851 |
{ |
| 2852 |
fFont.setSize(textSize); |
| 2853 |
uiParamsChanged = true; |
| 2854 |
} |
| 2855 |
} |
| 2856 |
|
| 2857 |
ImGui::Checkbox("Override ScaleX", &fFontOverrides.fScaleX); |
| 2858 |
if (fFontOverrides.fScaleX) { |
| 2859 |
float scaleX = fFont.getScaleX(); |
| 2860 |
if (ImGui::SliderFloat("ScaleX", &scaleX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) { |
| 2861 |
fFont.setScaleX(scaleX); |
| 2862 |
uiParamsChanged = true; |
| 2863 |
} |
| 2864 |
} |
| 2865 |
|
| 2866 |
ImGui::Checkbox("Override SkewX", &fFontOverrides.fSkewX); |
| 2867 |
if (fFontOverrides.fSkewX) { |
| 2868 |
float skewX = fFont.getSkewX(); |
| 2869 |
if (ImGui::SliderFloat("SkewX", &skewX, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL)) { |
| 2870 |
fFont.setSkewX(skewX); |
| 2871 |
uiParamsChanged = true; |
| 2872 |
} |
| 2873 |
} |
| 2874 |
} |
| 2875 |
|
| 2876 |
{ |
| 2877 |
SkMetaData controls; |
| 2878 |
if (fSlides[fCurrentSlide]->onGetControls(&controls)) { |
| 2879 |
if (ImGui::CollapsingHeader("Current Slide")) { |
| 2880 |
SkMetaData::Iter iter(controls); |
| 2881 |
const char* name; |
| 2882 |
SkMetaData::Type type; |
| 2883 |
int count; |
| 2884 |
while ((name = iter.next(&type, &count)) != nullptr) { |
| 2885 |
if (type == SkMetaData::kScalar_Type) { |
| 2886 |
float val[3]; |
| 2887 |
SkASSERT(count == 3); |
| 2888 |
controls.findScalars(name, &count, val); |
| 2889 |
if (ImGui::SliderFloat(name, &val[0], val[1], val[2])) { |
| 2890 |
controls.setScalars(name, 3, val); |
| 2891 |
} |
| 2892 |
} else if (type == SkMetaData::kBool_Type) { |
| 2893 |
bool val; |
| 2894 |
SkASSERT(count == 1); |
| 2895 |
controls.findBool(name, &val); |
| 2896 |
if (ImGui::Checkbox(name, &val)) { |
| 2897 |
controls.setBool(name, val); |
| 2898 |
} |
| 2899 |
} |
| 2900 |
} |
| 2901 |
fSlides[fCurrentSlide]->onSetControls(controls); |
| 2902 |
} |
| 2903 |
} |
| 2904 |
} |
| 2905 |
|
| 2906 |
if (fShowSlidePicker) { |
| 2907 |
ImGui::SetNextTreeNodeOpen(true); |
| 2908 |
} |
| 2909 |
if (ImGui::CollapsingHeader("Slide")) { |
| 2910 |
static ImGuiTextFilter filter; |
| 2911 |
static ImVector<const char*> filteredSlideNames; |
| 2912 |
static ImVector<int> filteredSlideIndices; |
| 2913 |
|
| 2914 |
if (fShowSlidePicker) { |
| 2915 |
ImGui::SetKeyboardFocusHere(); |
| 2916 |
fShowSlidePicker = false; |
| 2917 |
} |
| 2918 |
|
| 2919 |
filter.Draw(); |
| 2920 |
filteredSlideNames.clear(); |
| 2921 |
filteredSlideIndices.clear(); |
| 2922 |
int filteredIndex = 0; |
| 2923 |
for (int i = 0; i < fSlides.size(); ++i) { |
| 2924 |
const char* slideName = fSlides[i]->getName().c_str(); |
| 2925 |
if (filter.PassFilter(slideName) || i == fCurrentSlide) { |
| 2926 |
if (i == fCurrentSlide) { |
| 2927 |
filteredIndex = filteredSlideIndices.size(); |
| 2928 |
} |
| 2929 |
filteredSlideNames.push_back(slideName); |
| 2930 |
filteredSlideIndices.push_back(i); |
| 2931 |
} |
| 2932 |
} |
| 2933 |
|
| 2934 |
if (ImGui::ListBox("", &filteredIndex, filteredSlideNames.begin(), |
| 2935 |
filteredSlideNames.size(), 20)) { |
| 2936 |
this->setCurrentSlide(filteredSlideIndices[filteredIndex]); |
| 2937 |
} |
| 2938 |
} |
| 2939 |
|
| 2940 |
if (ImGui::CollapsingHeader("Color Mode")) { |
| 2941 |
ColorMode newMode = fColorMode; |
| 2942 |
auto cmButton = [&](ColorMode mode, const char* label) { |
| 2943 |
if (ImGui::RadioButton(label, mode == fColorMode)) { |
| 2944 |
newMode = mode; |
| 2945 |
} |
| 2946 |
}; |
| 2947 |
|
| 2948 |
cmButton(ColorMode::kLegacy, "Legacy 8888"); |
| 2949 |
cmButton(ColorMode::kColorManaged8888, "Color Managed 8888"); |
| 2950 |
cmButton(ColorMode::kColorManagedF16, "Color Managed F16"); |
| 2951 |
cmButton(ColorMode::kColorManagedF16Norm, "Color Managed F16 Norm"); |
| 2952 |
|
| 2953 |
if (newMode != fColorMode) { |
| 2954 |
this->setColorMode(newMode); |
| 2955 |
} |
| 2956 |
|
| 2957 |
// Pick from common gamuts: |
| 2958 |
int primariesIdx = 4; // Default: Custom |
| 2959 |
for (size_t i = 0; i < std::size(gNamedPrimaries); ++i) { |
| 2960 |
if (primaries_equal(*gNamedPrimaries[i].fPrimaries, fColorSpacePrimaries)) { |
| 2961 |
primariesIdx = i; |
| 2962 |
break; |
| 2963 |
} |
| 2964 |
} |
| 2965 |
|
| 2966 |
// Let user adjust the gamma |
| 2967 |
ImGui::SliderFloat("Gamma", &fColorSpaceTransferFn.g, 0.5f, 3.5f); |
| 2968 |
|
| 2969 |
if (ImGui::Combo("Primaries", &primariesIdx, |
| 2970 |
"sRGB\0AdobeRGB\0P3\0Rec. 2020\0Custom\0\0")) { |
| 2971 |
if (primariesIdx >= 0 && primariesIdx <= 3) { |
| 2972 |
fColorSpacePrimaries = *gNamedPrimaries[primariesIdx].fPrimaries; |
| 2973 |
} |
| 2974 |
} |
| 2975 |
|
| 2976 |
if (ImGui::Button("Spin")) { |
| 2977 |
float rx = fColorSpacePrimaries.fRX, |
| 2978 |
ry = fColorSpacePrimaries.fRY; |
| 2979 |
fColorSpacePrimaries.fRX = fColorSpacePrimaries.fGX; |
| 2980 |
fColorSpacePrimaries.fRY = fColorSpacePrimaries.fGY; |
| 2981 |
fColorSpacePrimaries.fGX = fColorSpacePrimaries.fBX; |
| 2982 |
fColorSpacePrimaries.fGY = fColorSpacePrimaries.fBY; |
| 2983 |
fColorSpacePrimaries.fBX = rx; |
| 2984 |
fColorSpacePrimaries.fBY = ry; |
| 2985 |
} |
| 2986 |
|
| 2987 |
// Allow direct editing of gamut |
| 2988 |
ImGui_Primaries(&fColorSpacePrimaries, &fImGuiGamutPaint); |
| 2989 |
} |
| 2990 |
|
| 2991 |
if (ImGui::CollapsingHeader("Animation")) { |
| 2992 |
bool isPaused = AnimTimer::kPaused_State == fAnimTimer.state(); |
| 2993 |
if (ImGui::Checkbox("Pause", &isPaused)) { |
| 2994 |
fAnimTimer.togglePauseResume(); |
| 2995 |
} |
| 2996 |
|
| 2997 |
float speed = fAnimTimer.getSpeed(); |
| 2998 |
if (ImGui::DragFloat("Speed", &speed, 0.1f)) { |
| 2999 |
fAnimTimer.setSpeed(speed); |
| 3000 |
} |
| 3001 |
} |
| 3002 |
|
| 3003 |
if (ImGui::CollapsingHeader("Shaders")) { |
| 3004 |
bool sksl = true; |
| 3005 |
#if defined(SK_GANESH) |
| 3006 |
if (ctx) { |
| 3007 |
sksl = params->grContextOptions().fShaderCacheStrategy == |
| 3008 |
GrContextOptions::ShaderCacheStrategy::kSkSL; |
| 3009 |
} |
| 3010 |
#endif |
| 3011 |
|
| 3012 |
const bool isVulkan = fBackendType == sk_app::Window::BackendType::kVulkan; |
| 3013 |
|
| 3014 |
// To re-load shaders from the currently active programs, we flush all |
| 3015 |
// caches on one frame, then set a flag to poll the cache on the next frame. |
| 3016 |
static bool gLoadPending = false; |
| 3017 |
if (gLoadPending) { |
| 3018 |
fCachedShaders.clear(); |
| 3019 |
#if defined(SK_GANESH) |
| 3020 |
if (ctx) { |
| 3021 |
fPersistentCache.foreach([this](sk_sp<const SkData> key, |
| 3022 |
sk_sp<SkData> data, |
| 3023 |
const SkString& description, |
| 3024 |
int hitCount) { |
| 3025 |
CachedShader& entry(fCachedShaders.push_back()); |
| 3026 |
entry.fKey = key; |
| 3027 |
SkMD5 hash; |
| 3028 |
hash.write(key->bytes(), key->size()); |
| 3029 |
entry.fKeyString = hash.finish().toHexString(); |
| 3030 |
entry.fKeyDescription = description; |
| 3031 |
|
| 3032 |
SkReadBuffer reader(data->data(), data->size()); |
| 3033 |
entry.fShaderType = GrPersistentCacheUtils::GetType(&reader); |
| 3034 |
const bool isBinaryShader = |
| 3035 |
entry.fShaderType == SkSetFourByteTag('S', 'P', 'R', 'V'); |
| 3036 |
GrPersistentCacheUtils::UnpackCachedShaders(&reader, |
| 3037 |
entry.fShader, |
| 3038 |
isBinaryShader, |
| 3039 |
entry.fInterfaces, |
| 3040 |
kGrShaderTypeCount); |
| 3041 |
}); |
| 3042 |
} |
| 3043 |
#endif |
| 3044 |
#if defined(SK_GRAPHITE) |
| 3045 |
if (skgpu::graphite::Context* gctx = fWindow->graphiteContext()) { |
| 3046 |
int index = 1; |
| 3047 |
auto callback = [&](const skgpu::UniqueKey& key, |
| 3048 |
const skgpu::graphite::GraphicsPipeline* pipeline) { |
| 3049 |
// Retrieve the shaders from the pipeline. |
| 3050 |
const skgpu::graphite::GraphicsPipeline::PipelineInfo& pipelineInfo = |
| 3051 |
pipeline->getPipelineInfo(); |
| 3052 |
|
| 3053 |
CachedShader& entry(fCachedShaders.push_back()); |
| 3054 |
entry.fKey = nullptr; |
| 3055 |
entry.fKeyString = SkStringPrintf("#%-3d %s", |
| 3056 |
index++, pipeline->getLabel()); |
| 3057 |
|
| 3058 |
if (sksl) { |
| 3059 |
entry.fShader[CachedShader::kVertexIndex].fText = |
| 3060 |
pipelineInfo.fSkSLVertexShader; |
| 3061 |
entry.fShader[CachedShader::kFragmentIndex].fText = |
| 3062 |
pipelineInfo.fSkSLFragmentShader; |
| 3063 |
entry.fShaderType = SkSetFourByteTag('S', 'K', 'S', 'L'); |
| 3064 |
} else { |
| 3065 |
entry.fShader[CachedShader::kVertexIndex].fText = |
| 3066 |
pipelineInfo.fNativeVertexShader; |
| 3067 |
entry.fShader[CachedShader::kFragmentIndex].fText = |
| 3068 |
pipelineInfo.fNativeFragmentShader; |
| 3069 |
// We could derive the shader type from the GraphicsPipeline's type |
| 3070 |
// if there is ever a need to. |
| 3071 |
entry.fShaderType = SkSetFourByteTag('?', '?', '?', '?'); |
| 3072 |
} |
| 3073 |
}; |
| 3074 |
gctx->priv().globalCache()->forEachGraphicsPipeline(callback); |
| 3075 |
} |
| 3076 |
#endif |
| 3077 |
|
| 3078 |
gLoadPending = false; |
| 3079 |
|
| 3080 |
#if defined(SK_VULKAN) |
| 3081 |
if (isVulkan && !sksl) { |
| 3082 |
// Disassemble the SPIR-V into its textual form. |
| 3083 |
spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
| 3084 |
uint32_t options = spvtools::SpirvTools::kDefaultDisassembleOption; |
| 3085 |
options |= SPV_BINARY_TO_TEXT_OPTION_COMMENT | |
| 3086 |
SPV_BINARY_TO_TEXT_OPTION_INDENT | |
| 3087 |
SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT; |
| 3088 |
for (auto& entry : fCachedShaders) { |
| 3089 |
for (int i = 0; i < CachedShader::kShaderTypeCount; ++i) { |
| 3090 |
std::string disasm; |
| 3091 |
tools.Disassemble(entry.fShader[i].fBinary.data(), |
| 3092 |
entry.fShader[i].fBinary.size(), |
| 3093 |
&disasm, |
| 3094 |
options); |
| 3095 |
entry.fShader[i].fText.assign(disasm); |
| 3096 |
} |
| 3097 |
} |
| 3098 |
} else |
| 3099 |
#endif |
| 3100 |
{ |
| 3101 |
// Reformat the SkSL with proper indentation. |
| 3102 |
for (auto& entry : fCachedShaders) { |
| 3103 |
for (int i = 0; i < CachedShader::kShaderTypeCount; ++i) { |
| 3104 |
entry.fShader[i].fText = |
| 3105 |
SkShaderUtils::PrettyPrint(entry.fShader[i].fText); |
| 3106 |
} |
| 3107 |
} |
| 3108 |
} |
| 3109 |
} |
| 3110 |
|
| 3111 |
// Defer actually doing the View/Apply logic so that we can trigger an Apply when we |
| 3112 |
// start or finish hovering on a tree node in the list below: |
| 3113 |
bool doView = ImGui::Button("View"); ImGui::SameLine(); |
| 3114 |
bool doApply [[maybe_unused]] = false; |
| 3115 |
bool doDump = false; |
| 3116 |
if (ctx) { |
| 3117 |
// TODO(skbug.com/40045492): we only have Ganesh implementations of Apply/Dump |
| 3118 |
doApply = ImGui::Button("Apply Changes"); ImGui::SameLine(); |
| 3119 |
doDump = ImGui::Button("Dump SkSL to resources/sksl/"); |
| 3120 |
} |
| 3121 |
int newOptLevel = fOptLevel; |
| 3122 |
ImGui::RadioButton("SkSL", &newOptLevel, kShaderOptLevel_Source); |
| 3123 |
ImGui::SameLine(); |
| 3124 |
ImGui::RadioButton("Compile", &newOptLevel, kShaderOptLevel_Compile); |
| 3125 |
ImGui::SameLine(); |
| 3126 |
ImGui::RadioButton("Optimize", &newOptLevel, kShaderOptLevel_Optimize); |
| 3127 |
ImGui::SameLine(); |
| 3128 |
ImGui::RadioButton("Inline", &newOptLevel, kShaderOptLevel_Inline); |
| 3129 |
|
| 3130 |
// If we are changing the compile mode, we want to reset the cache and redo |
| 3131 |
// everything. |
| 3132 |
static bool sDoDeferredView = false; |
| 3133 |
if (doView || doDump || newOptLevel != fOptLevel) { |
| 3134 |
sksl = doDump || (newOptLevel == kShaderOptLevel_Source); |
| 3135 |
fOptLevel = (ShaderOptLevel)newOptLevel; |
| 3136 |
switch (fOptLevel) { |
| 3137 |
case kShaderOptLevel_Source: |
| 3138 |
Compiler::EnableOptimizer(OverrideFlag::kOff); |
| 3139 |
Compiler::EnableInliner(OverrideFlag::kOff); |
| 3140 |
break; |
| 3141 |
case kShaderOptLevel_Compile: |
| 3142 |
Compiler::EnableOptimizer(OverrideFlag::kOff); |
| 3143 |
Compiler::EnableInliner(OverrideFlag::kOff); |
| 3144 |
break; |
| 3145 |
case kShaderOptLevel_Optimize: |
| 3146 |
Compiler::EnableOptimizer(OverrideFlag::kOn); |
| 3147 |
Compiler::EnableInliner(OverrideFlag::kOff); |
| 3148 |
break; |
| 3149 |
case kShaderOptLevel_Inline: |
| 3150 |
Compiler::EnableOptimizer(OverrideFlag::kOn); |
| 3151 |
Compiler::EnableInliner(OverrideFlag::kOn); |
| 3152 |
break; |
| 3153 |
} |
| 3154 |
|
| 3155 |
#if defined(SK_GANESH) |
| 3156 |
GrContextOptions grOpts = params->grContextOptions(); |
| 3157 |
grOpts.fShaderCacheStrategy = |
| 3158 |
sksl ? GrContextOptions::ShaderCacheStrategy::kSkSL |
| 3159 |
: GrContextOptions::ShaderCacheStrategy::kBackendSource; |
| 3160 |
displayParamsChanged = true; |
| 3161 |
newParamsBuilder.grContextOptions(grOpts); |
| 3162 |
#endif |
| 3163 |
|
| 3164 |
fDeferredActions.push_back([doDump, this]() { |
| 3165 |
#if defined(SK_GANESH) |
| 3166 |
// Reset the cache. |
| 3167 |
fPersistentCache.reset(); |
| 3168 |
#endif |
| 3169 |
|
| 3170 |
sDoDeferredView = true; |
| 3171 |
|
| 3172 |
// Dump the cache once we have drawn a frame with it. |
| 3173 |
if (doDump) { |
| 3174 |
fDeferredActions.push_back([this]() { |
| 3175 |
this->dumpShadersToResources(); |
| 3176 |
}); |
| 3177 |
} |
| 3178 |
}); |
| 3179 |
} |
| 3180 |
|
| 3181 |
ImGui::BeginChild("##ScrollingRegion"); |
| 3182 |
for (auto& entry : fCachedShaders) { |
| 3183 |
bool inTreeNode = ImGui::TreeNode(entry.fKeyString.c_str()); |
| 3184 |
bool hovered = ImGui::IsItemHovered(); |
| 3185 |
if (hovered != entry.fHovered) { |
| 3186 |
// Force an Apply to patch the highlight shader in/out |
| 3187 |
entry.fHovered = hovered; |
| 3188 |
doApply = true; |
| 3189 |
} |
| 3190 |
if (inTreeNode) { |
| 3191 |
auto stringBox = [](const char* label, std::string* str) { |
| 3192 |
// Full width, and not too much space for each shader |
| 3193 |
int lines = std::count(str->begin(), str->end(), '\n') + 2; |
| 3194 |
ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * std::min(lines, 30)); |
| 3195 |
ImGui::InputTextMultiline(label, str, boxSize); |
| 3196 |
}; |
| 3197 |
if (ImGui::TreeNode("Key")) { |
| 3198 |
ImGui::TextWrapped("%s", entry.fKeyDescription.c_str()); |
| 3199 |
ImGui::TreePop(); |
| 3200 |
} |
| 3201 |
stringBox("##VP", &entry.fShader[CachedShader::kVertexIndex].fText); |
| 3202 |
stringBox("##FP", &entry.fShader[CachedShader::kFragmentIndex].fText); |
| 3203 |
ImGui::TreePop(); |
| 3204 |
} |
| 3205 |
} |
| 3206 |
ImGui::EndChild(); |
| 3207 |
|
| 3208 |
if (doView || sDoDeferredView) { |
| 3209 |
#if defined(SK_GANESH) |
| 3210 |
fPersistentCache.reset(); |
| 3211 |
if (ctx) { |
| 3212 |
ctx->priv().getGpu()->resetShaderCacheForTesting(); |
| 3213 |
} |
| 3214 |
#endif |
| 3215 |
#if defined(SK_GRAPHITE) |
| 3216 |
if (skgpu::graphite::Context* gctx = fWindow->graphiteContext()) { |
| 3217 |
gctx->priv().globalCache()->deleteResources(); |
| 3218 |
} |
| 3219 |
#endif |
| 3220 |
gLoadPending = true; |
| 3221 |
sDoDeferredView = false; |
| 3222 |
} |
| 3223 |
|
| 3224 |
// We don't support updating SPIRV shaders. We could re-assemble them (with edits), |
| 3225 |
// but I'm not sure anyone wants to do that. |
| 3226 |
if (isVulkan && !sksl) { |
| 3227 |
doApply = false; |
| 3228 |
} |
| 3229 |
#if defined(SK_GANESH) |
| 3230 |
if (ctx && doApply) { |
| 3231 |
fPersistentCache.reset(); |
| 3232 |
ctx->priv().getGpu()->resetShaderCacheForTesting(); |
| 3233 |
for (auto& entry : fCachedShaders) { |
| 3234 |
std::string backup = entry.fShader[kFragment_GrShaderType].fText; |
| 3235 |
if (entry.fHovered) { |
| 3236 |
// The hovered item (if any) gets a special shader to make it |
| 3237 |
// identifiable. |
| 3238 |
std::string& fragShader = entry.fShader[kFragment_GrShaderType].fText; |
| 3239 |
switch (entry.fShaderType) { |
| 3240 |
case SkSetFourByteTag('S', 'K', 'S', 'L'): { |
| 3241 |
fragShader = build_sksl_highlight_shader(); |
| 3242 |
break; |
| 3243 |
} |
| 3244 |
case SkSetFourByteTag('G', 'L', 'S', 'L'): { |
| 3245 |
fragShader = build_glsl_highlight_shader( |
| 3246 |
*ctx->priv().caps()->shaderCaps()); |
| 3247 |
break; |
| 3248 |
} |
| 3249 |
case SkSetFourByteTag('M', 'S', 'L', ' '): { |
| 3250 |
fragShader = build_metal_highlight_shader(fragShader); |
| 3251 |
break; |
| 3252 |
} |
| 3253 |
} |
| 3254 |
} |
| 3255 |
|
| 3256 |
SkASSERT(!entry.fShader[0].isBinary()); |
| 3257 |
auto data = GrPersistentCacheUtils::PackCachedShaders(entry.fShaderType, |
| 3258 |
entry.fShader, |
| 3259 |
entry.fInterfaces, |
| 3260 |
kGrShaderTypeCount); |
| 3261 |
fPersistentCache.store(*entry.fKey, *data, entry.fKeyDescription); |
| 3262 |
|
| 3263 |
entry.fShader[kFragment_GrShaderType].fText = backup; |
| 3264 |
} |
| 3265 |
} |
| 3266 |
#endif |
| 3267 |
} |
| 3268 |
} |
| 3269 |
if (displayParamsChanged || uiParamsChanged) { |
| 3270 |
// The unique_ptr cannot be captured, so release it and capture the raw pointer value in |
| 3271 |
// the lambda. The lambda is responsible for deleting it, which will only be called once |
| 3272 |
skwindow::DisplayParams* newParams = newParamsBuilder.detach().release(); |
| 3273 |
fDeferredActions.emplace_back([displayParamsChanged, newParams, this]() { |
| 3274 |
if (displayParamsChanged) { |
| 3275 |
fWindow->setRequestedDisplayParams( |
| 3276 |
std::unique_ptr<skwindow::DisplayParams>(newParams)); |
| 3277 |
} |
| 3278 |
fWindow->inval(); |
| 3279 |
this->updateTitle(); |
| 3280 |
}); |
| 3281 |
} |
| 3282 |
ImGui::End(); |
| 3283 |
} |
| 3284 |
|
| 3285 |
#if defined(SK_GANESH) |
| 3286 |
if (gShaderErrorHandler.fErrors.size()) { |
| 3287 |
ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver); |
| 3288 |
ImGui::Begin("Shader Errors", nullptr, ImGuiWindowFlags_NoFocusOnAppearing); |
| 3289 |
for (int i = 0; i < gShaderErrorHandler.fErrors.size(); ++i) { |
| 3290 |
ImGui::TextWrapped("%s", gShaderErrorHandler.fErrors[i].c_str()); |
| 3291 |
std::string sksl(gShaderErrorHandler.fShaders[i].c_str()); |
| 3292 |
SkShaderUtils::VisitLineByLine(sksl, [](int lineNumber, const char* lineText) { |
| 3293 |
ImGui::TextWrapped("%4i\t%s\n", lineNumber, lineText); |
| 3294 |
}); |
| 3295 |
} |
| 3296 |
ImGui::End(); |
| 3297 |
gShaderErrorHandler.reset(); |
| 3298 |
} |
| 3299 |
#endif |
| 3300 |
|
| 3301 |
if (fShowZoomWindow && fLastImage) { |
| 3302 |
ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver); |
| 3303 |
if (ImGui::Begin("Zoom", &fShowZoomWindow)) { |
| 3304 |
static int zoomFactor = 8; |
| 3305 |
if (ImGui::Button("<<")) { |
| 3306 |
zoomFactor = std::max(zoomFactor / 2, 4); |
| 3307 |
} |
| 3308 |
ImGui::SameLine(); ImGui::Text("%2d", zoomFactor); ImGui::SameLine(); |
| 3309 |
if (ImGui::Button(">>")) { |
| 3310 |
zoomFactor = std::min(zoomFactor * 2, 32); |
| 3311 |
} |
| 3312 |
|
| 3313 |
if (!fZoomWindowFixed) { |
| 3314 |
ImVec2 mousePos = ImGui::GetMousePos(); |
| 3315 |
fZoomWindowLocation = SkPoint::Make(mousePos.x, mousePos.y); |
| 3316 |
} |
| 3317 |
SkScalar x = fZoomWindowLocation.x(); |
| 3318 |
SkScalar y = fZoomWindowLocation.y(); |
| 3319 |
int xInt = SkScalarRoundToInt(x); |
| 3320 |
int yInt = SkScalarRoundToInt(y); |
| 3321 |
ImVec2 avail = ImGui::GetContentRegionAvail(); |
| 3322 |
|
| 3323 |
uint32_t pixel = 0; |
| 3324 |
SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 3325 |
bool didGraphiteRead = false; |
| 3326 |
if (is_graphite_backend_type(fBackendType)) { |
| 3327 |
#if defined(GPU_TEST_UTILS) |
| 3328 |
SkBitmap bitmap; |
| 3329 |
bitmap.allocPixels(info); |
| 3330 |
SkPixmap pixels; |
| 3331 |
SkAssertResult(bitmap.peekPixels(&pixels)); |
| 3332 |
didGraphiteRead = |
| 3333 |
as_IB(fLastImage) |
| 3334 |
->readPixelsGraphite(fWindow->baseRecorder(), pixels, xInt, yInt); |
| 3335 |
pixel = *pixels.addr32(); |
| 3336 |
ImGui::SameLine(); |
| 3337 |
ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X", |
| 3338 |
xInt, yInt, |
| 3339 |
SkGetPackedR32(pixel), SkGetPackedG32(pixel), |
| 3340 |
SkGetPackedB32(pixel), SkGetPackedA32(pixel)); |
| 3341 |
#endif |
| 3342 |
} |
| 3343 |
auto dContext = fWindow->directContext(); |
| 3344 |
if (fLastImage->readPixels(dContext, |
| 3345 |
info, |
| 3346 |
&pixel, |
| 3347 |
info.minRowBytes(), |
| 3348 |
xInt, |
| 3349 |
yInt)) { |
| 3350 |
ImGui::SameLine(); |
| 3351 |
ImGui::Text("(X, Y): %d, %d RGBA: %X %X %X %X", |
| 3352 |
xInt, yInt, |
| 3353 |
SkGetPackedR32(pixel), SkGetPackedG32(pixel), |
| 3354 |
SkGetPackedB32(pixel), SkGetPackedA32(pixel)); |
| 3355 |
} else { |
| 3356 |
if (!didGraphiteRead) { |
| 3357 |
ImGui::SameLine(); |
| 3358 |
ImGui::Text("Failed to readPixels"); |
| 3359 |
} |
| 3360 |
} |
| 3361 |
|
| 3362 |
fImGuiLayer.skiaWidget(avail, [=, lastImage = fLastImage](SkCanvas* c) { |
| 3363 |
// Translate so the region of the image that's under the mouse cursor is centered |
| 3364 |
// in the zoom canvas: |
| 3365 |
c->scale(zoomFactor, zoomFactor); |
| 3366 |
c->translate(avail.x * 0.5f / zoomFactor - x - 0.5f, |
| 3367 |
avail.y * 0.5f / zoomFactor - y - 0.5f); |
| 3368 |
c->drawImage(lastImage, 0, 0); |
| 3369 |
|
| 3370 |
// Draw a pixel outline around the pixel whose color and coordinate are displayed |
| 3371 |
// in the text of the widget. The paint is configured to ensure contrast on any |
| 3372 |
// background color. |
| 3373 |
SkPaint outline; |
| 3374 |
outline.setColor(SK_ColorWHITE); |
| 3375 |
outline.setStyle(SkPaint::kStroke_Style); |
| 3376 |
outline.setBlendMode(SkBlendMode::kExclusion); |
| 3377 |
c->drawRect(SkRect::MakeXYWH(x, y, 1, 1), outline); |
| 3378 |
}); |
| 3379 |
} |
| 3380 |
|
| 3381 |
ImGui::End(); |
| 3382 |
} |
| 3383 |
|
| 3384 |
if (fShowHistogramWindow && fLastImage) { |
| 3385 |
ImGui::SetNextWindowSize(ImVec2(450, 500)); |
| 3386 |
ImGui::SetNextWindowBgAlpha(0.5f); |
| 3387 |
if (ImGui::Begin("Color Histogram (R,G,B)", &fShowHistogramWindow)) { |
| 3388 |
const auto info = SkImageInfo::MakeN32Premul(fWindow->width(), fWindow->height()); |
| 3389 |
SkAutoPixmapStorage pixmap; |
| 3390 |
pixmap.alloc(info); |
| 3391 |
|
| 3392 |
if (fLastImage->readPixels(fWindow->directContext(), info, pixmap.writable_addr(), |
| 3393 |
info.minRowBytes(), 0, 0)) { |
| 3394 |
std::vector<float> r(256), g(256), b(256); |
| 3395 |
for (int y = 0; y < info.height(); ++y) { |
| 3396 |
for (int x = 0; x < info.width(); ++x) { |
| 3397 |
const auto pmc = *pixmap.addr32(x, y); |
| 3398 |
r[SkGetPackedR32(pmc)]++; |
| 3399 |
g[SkGetPackedG32(pmc)]++; |
| 3400 |
b[SkGetPackedB32(pmc)]++; |
| 3401 |
} |
| 3402 |
} |
| 3403 |
|
| 3404 |
ImGui::PushItemWidth(-1); |
| 3405 |
ImGui::PlotHistogram("R", r.data(), r.size(), 0, nullptr, |
| 3406 |
FLT_MAX, FLT_MAX, ImVec2(0, 150)); |
| 3407 |
ImGui::PlotHistogram("G", g.data(), g.size(), 0, nullptr, |
| 3408 |
FLT_MAX, FLT_MAX, ImVec2(0, 150)); |
| 3409 |
ImGui::PlotHistogram("B", b.data(), b.size(), 0, nullptr, |
| 3410 |
FLT_MAX, FLT_MAX, ImVec2(0, 150)); |
| 3411 |
ImGui::PopItemWidth(); |
| 3412 |
} |
| 3413 |
} |
| 3414 |
|
| 3415 |
ImGui::End(); |
| 3416 |
} |
| 3417 |
} |
| 3418 |
|
| 3419 |
void Viewer::dumpShadersToResources() { |
| 3420 |
// Sort the list of cached shaders so we can maintain some minimal level of consistency. |
| 3421 |
// It doesn't really matter, but it will keep files from switching places unpredictably. |
| 3422 |
std::vector<const CachedShader*> shaders; |
| 3423 |
shaders.reserve(fCachedShaders.size()); |
| 3424 |
for (const CachedShader& shader : fCachedShaders) { |
| 3425 |
shaders.push_back(&shader); |
| 3426 |
} |
| 3427 |
|
| 3428 |
std::sort(shaders.begin(), shaders.end(), [](const CachedShader* a, const CachedShader* b) { |
| 3429 |
return std::tie(a->fShader[CachedShader::kFragmentIndex].fText, |
| 3430 |
a->fShader[CachedShader::kVertexIndex].fText) < |
| 3431 |
std::tie(b->fShader[CachedShader::kFragmentIndex].fText, |
| 3432 |
b->fShader[CachedShader::kVertexIndex].fText); |
| 3433 |
}); |
| 3434 |
|
| 3435 |
// Make the resources/sksl/SlideName/ directory. |
| 3436 |
SkString directory = SkStringPrintf("%ssksl/%s", |
| 3437 |
GetResourcePath().c_str(), |
| 3438 |
fSlides[fCurrentSlide]->getName().c_str()); |
| 3439 |
if (!sk_mkdir(directory.c_str())) { |
| 3440 |
SkDEBUGFAILF("Unable to create directory '%s'", directory.c_str()); |
| 3441 |
return; |
| 3442 |
} |
| 3443 |
|
| 3444 |
int index = 0; |
| 3445 |
for (const auto& entry : shaders) { |
| 3446 |
SkString vertPath = SkStringPrintf("%s/Vertex_%02d.vert", directory.c_str(), index); |
| 3447 |
FILE* vertFile = sk_fopen(vertPath.c_str(), kWrite_SkFILE_Flag); |
| 3448 |
if (vertFile) { |
| 3449 |
const std::string& vertText = entry->fShader[CachedShader::kVertexIndex].fText; |
| 3450 |
SkAssertResult(sk_fwrite(vertText.c_str(), vertText.size(), vertFile)); |
| 3451 |
sk_fclose(vertFile); |
| 3452 |
} else { |
| 3453 |
SkDEBUGFAILF("Unable to write shader to path '%s'", vertPath.c_str()); |
| 3454 |
} |
| 3455 |
|
| 3456 |
SkString fragPath = SkStringPrintf("%s/Fragment_%02d.frag", directory.c_str(), index); |
| 3457 |
FILE* fragFile = sk_fopen(fragPath.c_str(), kWrite_SkFILE_Flag); |
| 3458 |
if (fragFile) { |
| 3459 |
const std::string& fragText = entry->fShader[CachedShader::kFragmentIndex].fText; |
| 3460 |
SkAssertResult(sk_fwrite(fragText.c_str(), fragText.size(), fragFile)); |
| 3461 |
sk_fclose(fragFile); |
| 3462 |
} else { |
| 3463 |
SkDEBUGFAILF("Unable to write shader to path '%s'", fragPath.c_str()); |
| 3464 |
} |
| 3465 |
|
| 3466 |
++index; |
| 3467 |
} |
| 3468 |
} |
| 3469 |
|
| 3470 |
void Viewer::onIdle() { |
| 3471 |
TArray<std::function<void()>> actionsToRun; |
| 3472 |
actionsToRun.swap(fDeferredActions); |
| 3473 |
|
| 3474 |
for (const auto& fn : actionsToRun) { |
| 3475 |
fn(); |
| 3476 |
} |
| 3477 |
|
| 3478 |
fStatsLayer.beginTiming(fAnimateTimer); |
| 3479 |
fAnimTimer.updateTime(); |
| 3480 |
bool animateWantsInval = fSlides[fCurrentSlide]->animate(fAnimTimer.nanos()); |
| 3481 |
fStatsLayer.endTiming(fAnimateTimer); |
| 3482 |
|
| 3483 |
ImGuiIO& io = ImGui::GetIO(); |
| 3484 |
// ImGui always has at least one "active" window, which is the default "Debug" window. It may |
| 3485 |
// not be visible, though. So we need to redraw if there is at least one visible window, or |
| 3486 |
// more than one active window. Newly created windows are active but not visible for one frame |
| 3487 |
// while they determine their layout and sizing. |
| 3488 |
if (animateWantsInval || fStatsLayer.getActive() || fRefresh || |
| 3489 |
io.MetricsActiveWindows > 1 || io.MetricsRenderWindows > 0) { |
| 3490 |
fWindow->inval(); |
| 3491 |
} |
| 3492 |
} |
| 3493 |
|
| 3494 |
template <typename OptionsFunc> |
| 3495 |
static void WriteStateObject(SkJSONWriter& writer, const char* name, const char* value, |
| 3496 |
OptionsFunc&& optionsFunc) { |
| 3497 |
writer.beginObject(); |
| 3498 |
{ |
| 3499 |
writer.appendCString(kName , name); |
| 3500 |
writer.appendCString(kValue, value); |
| 3501 |
|
| 3502 |
writer.beginArray(kOptions); |
| 3503 |
{ |
| 3504 |
optionsFunc(writer); |
| 3505 |
} |
| 3506 |
writer.endArray(); |
| 3507 |
} |
| 3508 |
writer.endObject(); |
| 3509 |
} |
| 3510 |
|
| 3511 |
|
| 3512 |
void Viewer::updateUIState() { |
| 3513 |
if (!fWindow) { |
| 3514 |
return; |
| 3515 |
} |
| 3516 |
if (fWindow->sampleCount() < 1) { |
| 3517 |
return; // Surface hasn't been created yet. |
| 3518 |
} |
| 3519 |
|
| 3520 |
SkDynamicMemoryWStream memStream; |
| 3521 |
SkJSONWriter writer(&memStream); |
| 3522 |
writer.beginArray(); |
| 3523 |
|
| 3524 |
// Slide state |
| 3525 |
WriteStateObject(writer, kSlideStateName, fSlides[fCurrentSlide]->getName().c_str(), |
| 3526 |
[this](SkJSONWriter& writer) { |
| 3527 |
for(const auto& slide : fSlides) { |
| 3528 |
writer.appendString(slide->getName()); |
| 3529 |
} |
| 3530 |
}); |
| 3531 |
|
| 3532 |
// Backend state |
| 3533 |
WriteStateObject(writer, kBackendStateName, get_backend_string(fBackendType), |
| 3534 |
[](SkJSONWriter& writer) { |
| 3535 |
for (size_t i = 0; i < kSupportedBackendTypeCount; ++i) { |
| 3536 |
auto backendType = kSupportedBackends[i]; |
| 3537 |
writer.appendCString(get_backend_string(backendType)); |
| 3538 |
} |
| 3539 |
}); |
| 3540 |
|
| 3541 |
// MSAA state |
| 3542 |
const auto countString = SkStringPrintf("%d", fWindow->sampleCount()); |
| 3543 |
WriteStateObject(writer, kMSAAStateName, countString.c_str(), |
| 3544 |
[this](SkJSONWriter& writer) { |
| 3545 |
writer.appendS32(0); |
| 3546 |
|
| 3547 |
if (sk_app::Window::BackendType::kRaster == fBackendType) { |
| 3548 |
return; |
| 3549 |
} |
| 3550 |
|
| 3551 |
for (int msaa : {4, 8, 16}) { |
| 3552 |
writer.appendS32(msaa); |
| 3553 |
} |
| 3554 |
}); |
| 3555 |
|
| 3556 |
// TODO: Store Graphite path renderer strategy |
| 3557 |
// Path renderer state |
| 3558 |
#if defined(SK_GANESH) |
| 3559 |
GpuPathRenderers pr = |
| 3560 |
fWindow->getRequestedDisplayParams()->grContextOptions().fGpuPathRenderers; |
| 3561 |
WriteStateObject(writer, kPathRendererStateName, gGaneshPathRendererNames[pr].c_str(), |
| 3562 |
[this](SkJSONWriter& writer) { |
| 3563 |
auto ctx = fWindow->directContext(); |
| 3564 |
if (!ctx) { |
| 3565 |
writer.appendNString("Software"); |
| 3566 |
} else { |
| 3567 |
writer.appendString(gGaneshPathRendererNames[GpuPathRenderers::kDefault]); |
| 3568 |
if (fWindow->sampleCount() > 1 || FLAGS_dmsaa) { |
| 3569 |
const auto* caps = ctx->priv().caps(); |
| 3570 |
if (skgpu::ganesh::AtlasPathRenderer::IsSupported(ctx)) { |
| 3571 |
writer.appendString(gGaneshPathRendererNames[GpuPathRenderers::kAtlas]); |
| 3572 |
} |
| 3573 |
if (skgpu::ganesh::TessellationPathRenderer::IsSupported(*caps)) { |
| 3574 |
writer.appendString(gGaneshPathRendererNames[GpuPathRenderers::kTessellation]); |
| 3575 |
} |
| 3576 |
} |
| 3577 |
if (1 == fWindow->sampleCount()) { |
| 3578 |
writer.appendString(gGaneshPathRendererNames[GpuPathRenderers::kSmall]); |
| 3579 |
} |
| 3580 |
writer.appendString(gGaneshPathRendererNames[GpuPathRenderers::kTriangulating]); |
| 3581 |
writer.appendString(gGaneshPathRendererNames[GpuPathRenderers::kNone]); |
| 3582 |
} |
| 3583 |
}); |
| 3584 |
#endif |
| 3585 |
|
| 3586 |
// Softkey state |
| 3587 |
WriteStateObject(writer, kSoftkeyStateName, kSoftkeyHint, |
| 3588 |
[this](SkJSONWriter& writer) { |
| 3589 |
writer.appendNString(kSoftkeyHint); |
| 3590 |
for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) { |
| 3591 |
writer.appendString(softkey); |
| 3592 |
} |
| 3593 |
}); |
| 3594 |
|
| 3595 |
writer.endArray(); |
| 3596 |
writer.flush(); |
| 3597 |
|
| 3598 |
auto data = memStream.detachAsData(); |
| 3599 |
|
| 3600 |
// TODO: would be cool to avoid this copy |
| 3601 |
const SkString cstring(static_cast<const char*>(data->data()), data->size()); |
| 3602 |
|
| 3603 |
fWindow->setUIState(cstring.c_str()); |
| 3604 |
} |
| 3605 |
|
| 3606 |
void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) { |
| 3607 |
// For those who will add more features to handle the state change in this function: |
| 3608 |
// After the change, please call updateUIState no notify the frontend (e.g., Android app). |
| 3609 |
// For example, after slide change, updateUIState is called inside setupCurrentSlide; |
| 3610 |
// after backend change, updateUIState is called in this function. |
| 3611 |
if (stateName.equals(kSlideStateName)) { |
| 3612 |
for (int i = 0; i < fSlides.size(); ++i) { |
| 3613 |
if (fSlides[i]->getName().equals(stateValue)) { |
| 3614 |
this->setCurrentSlide(i); |
| 3615 |
return; |
| 3616 |
} |
| 3617 |
} |
| 3618 |
|
| 3619 |
SkDebugf("Slide not found: %s", stateValue.c_str()); |
| 3620 |
} else if (stateName.equals(kBackendStateName)) { |
| 3621 |
for (size_t i = 0; i < kSupportedBackendTypeCount; i++) { |
| 3622 |
auto backendType = kSupportedBackends[i]; |
| 3623 |
if (stateValue.equals(get_backend_string(backendType))) { |
| 3624 |
if (fBackendType != backendType) { |
| 3625 |
this->setBackend(backendType); |
| 3626 |
} |
| 3627 |
break; |
| 3628 |
} |
| 3629 |
} |
| 3630 |
} else if (stateName.equals(kMSAAStateName)) { |
| 3631 |
auto params = fWindow->getRequestedDisplayParams(); |
| 3632 |
int sampleCount = atoi(stateValue.c_str()); |
| 3633 |
if (sampleCount != params->msaaSampleCount()) { |
| 3634 |
auto newParamsBuilder = make_display_params_builder(params); |
| 3635 |
newParamsBuilder.msaaSampleCount(sampleCount); |
| 3636 |
fWindow->setRequestedDisplayParams(newParamsBuilder.detach()); |
| 3637 |
fWindow->inval(); |
| 3638 |
this->updateTitle(); |
| 3639 |
this->updateUIState(); |
| 3640 |
} |
| 3641 |
#if defined(SK_GANESH) |
| 3642 |
} else if (stateName.equals(kPathRendererStateName)) { |
| 3643 |
auto params = fWindow->getRequestedDisplayParams(); |
| 3644 |
for (const auto& pair : gGaneshPathRendererNames) { |
| 3645 |
if (pair.second == stateValue.c_str()) { |
| 3646 |
if (params->grContextOptions().fGpuPathRenderers != pair.first) { |
| 3647 |
auto newParamsBuilder = make_display_params_builder(params); |
| 3648 |
auto newOpts = params->grContextOptions(); |
| 3649 |
newOpts.fGpuPathRenderers = pair.first; |
| 3650 |
newParamsBuilder.grContextOptions(newOpts); |
| 3651 |
fWindow->setRequestedDisplayParams(newParamsBuilder.detach()); |
| 3652 |
fWindow->inval(); |
| 3653 |
this->updateTitle(); |
| 3654 |
this->updateUIState(); |
| 3655 |
} |
| 3656 |
break; |
| 3657 |
} |
| 3658 |
} |
| 3659 |
#endif |
| 3660 |
} else if (stateName.equals(kSoftkeyStateName)) { |
| 3661 |
if (!stateValue.equals(kSoftkeyHint)) { |
| 3662 |
fCommands.onSoftkey(stateValue); |
| 3663 |
this->updateUIState(); // This is still needed to reset the value to kSoftkeyHint |
| 3664 |
} |
| 3665 |
} else if (stateName.equals(kRefreshStateName)) { |
| 3666 |
// This state is actually NOT in the UI state. |
| 3667 |
// We use this to allow Android to quickly set bool fRefresh. |
| 3668 |
fRefresh = stateValue.equals(kON); |
| 3669 |
} else { |
| 3670 |
SkDebugf("Unknown stateName: %s", stateName.c_str()); |
| 3671 |
} |
| 3672 |
} |
| 3673 |
|
| 3674 |
bool Viewer::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) { |
| 3675 |
return fCommands.onKey(key, state, modifiers); |
| 3676 |
} |
| 3677 |
|
| 3678 |
bool Viewer::onChar(SkUnichar c, skui::ModifierKey modifiers) { |
| 3679 |
if (fSlides[fCurrentSlide]->onChar(c)) { |
| 3680 |
fWindow->inval(); |
| 3681 |
return true; |
| 3682 |
} else { |
| 3683 |
return fCommands.onChar(c, modifiers); |
| 3684 |
} |
| 3685 |
} |