| 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 |
#ifndef Viewer_DEFINED |
| 9 |
#define Viewer_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkData.h" |
| 13 |
#include "include/core/SkFont.h" |
| 14 |
#include "include/private/base/SkTArray.h" |
| 15 |
#include "modules/skcms/skcms.h" |
| 16 |
#include "src/sksl/codegen/SkSLNativeShader.h" |
| 17 |
#include "src/sksl/ir/SkSLProgram.h" |
| 18 |
#include "tools/sk_app/Application.h" |
| 19 |
#include "tools/sk_app/CommandSet.h" |
| 20 |
#include "tools/sk_app/Window.h" |
| 21 |
#include "tools/viewer/AnimTimer.h" |
| 22 |
#include "tools/viewer/ImGuiLayer.h" |
| 23 |
#include "tools/viewer/StatsLayer.h" |
| 24 |
#include "tools/viewer/TouchGesture.h" |
| 25 |
#include "tools/window/DisplayParams.h" |
| 26 |
|
| 27 |
#if defined(SK_GANESH) |
| 28 |
#include "include/gpu/ganesh/GrContextOptions.h" |
| 29 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 30 |
#include "tools/ganesh/MemoryCache.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <atomic> |
| 34 |
#include <cstdint> |
| 35 |
#include <functional> |
| 36 |
#include <string> |
| 37 |
|
| 38 |
class SkImage; |
| 39 |
class SkSurface; |
| 40 |
class Slide; |
| 41 |
namespace skui { |
| 42 |
enum class InputState; |
| 43 |
enum class Key; |
| 44 |
enum class ModifierKey; |
| 45 |
} // namespace skui |
| 46 |
|
| 47 |
class Viewer : public sk_app::Application, sk_app::Window::Layer { |
| 48 |
public: |
| 49 |
Viewer(int argc, char** argv, void* platformData); |
| 50 |
~Viewer() override; |
| 51 |
|
| 52 |
void onIdle() override; |
| 53 |
|
| 54 |
void onBackendCreated() override; |
| 55 |
void onPaint(SkSurface*) override; |
| 56 |
void onResize(int width, int height) override; |
| 57 |
bool onTouch(intptr_t owner, skui::InputState state, float x, float y) override; |
| 58 |
bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) override; |
| 59 |
bool onMouseWheel(float delta, int x, int y, skui::ModifierKey) override; |
| 60 |
void onUIStateChanged(const SkString& stateName, const SkString& stateValue) override; |
| 61 |
bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) override; |
| 62 |
bool onChar(SkUnichar c, skui::ModifierKey modifiers) override; |
| 63 |
bool onPinch(skui::InputState state, float scale, float x, float y) override; |
| 64 |
bool onFling(skui::InputState state) override; |
| 65 |
|
| 66 |
#if defined(SK_GANESH) |
| 67 |
static GrContextOptions::ShaderErrorHandler* ShaderErrorHandler(); |
| 68 |
#endif |
| 69 |
|
| 70 |
struct SkFontFields { |
| 71 |
bool overridesSomething() const { |
| 72 |
return fTypeface || |
| 73 |
fSize || |
| 74 |
fScaleX || |
| 75 |
fSkewX || |
| 76 |
fHinting || |
| 77 |
fEdging || |
| 78 |
fSubpixel || |
| 79 |
fForceAutoHinting || |
| 80 |
fEmbeddedBitmaps || |
| 81 |
fLinearMetrics || |
| 82 |
fEmbolden || |
| 83 |
fBaselineSnap; |
| 84 |
} |
| 85 |
|
| 86 |
bool fTypeface = false; |
| 87 |
bool fSize = false; |
| 88 |
SkScalar fSizeRange[2] = { 0, 20 }; |
| 89 |
bool fScaleX = false; |
| 90 |
bool fSkewX = false; |
| 91 |
bool fHinting = false; |
| 92 |
bool fEdging = false; |
| 93 |
bool fSubpixel = false; |
| 94 |
bool fForceAutoHinting = false; |
| 95 |
bool fEmbeddedBitmaps = false; |
| 96 |
bool fLinearMetrics = false; |
| 97 |
bool fEmbolden = false; |
| 98 |
bool fBaselineSnap = false; |
| 99 |
}; |
| 100 |
struct SkPaintFields { |
| 101 |
bool overridesSomething() const { |
| 102 |
return fPathEffect || |
| 103 |
fShader || |
| 104 |
fMaskFilter || |
| 105 |
fColorFilter || |
| 106 |
fImageFilter || |
| 107 |
fColor || |
| 108 |
fStrokeWidth || |
| 109 |
fMiterLimit || |
| 110 |
fBlendMode || |
| 111 |
fAntiAlias || |
| 112 |
fDither || |
| 113 |
fForceRuntimeBlend || |
| 114 |
fCapType || |
| 115 |
fJoinType || |
| 116 |
fStyle; |
| 117 |
} |
| 118 |
|
| 119 |
bool fPathEffect = false; |
| 120 |
bool fShader = false; |
| 121 |
bool fMaskFilter = false; |
| 122 |
bool fColorFilter = false; |
| 123 |
bool fImageFilter = false; |
| 124 |
|
| 125 |
bool fColor = false; |
| 126 |
bool fStrokeWidth = false; |
| 127 |
bool fMiterLimit = false; |
| 128 |
bool fBlendMode = false; |
| 129 |
|
| 130 |
bool fAntiAlias = false; |
| 131 |
bool fDither = false; |
| 132 |
bool fForceRuntimeBlend = false; |
| 133 |
|
| 134 |
bool fCapType = false; |
| 135 |
bool fJoinType = false; |
| 136 |
bool fStyle = false; |
| 137 |
}; |
| 138 |
struct SkSurfacePropsFields { |
| 139 |
bool fFlags = false; |
| 140 |
bool fPixelGeometry = false; |
| 141 |
}; |
| 142 |
struct DisplayFields { |
| 143 |
bool fColorType = false; |
| 144 |
bool fColorSpace = false; |
| 145 |
bool fMSAASampleCount = false; |
| 146 |
bool fGrContextOptions = false; |
| 147 |
SkSurfacePropsFields fSurfaceProps; |
| 148 |
bool fDisableVsync = false; |
| 149 |
}; |
| 150 |
private: |
| 151 |
enum class ColorMode { |
| 152 |
kLegacy, // 8888, no color management |
| 153 |
kColorManaged8888, // 8888 with color management |
| 154 |
kColorManagedF16, // F16 with color management |
| 155 |
kColorManagedF16Norm, // Normalized F16 with color management |
| 156 |
}; |
| 157 |
|
| 158 |
void initSlides(); |
| 159 |
void updateTitle(); |
| 160 |
void setBackend(sk_app::Window::BackendType); |
| 161 |
void initGpuTimer(); |
| 162 |
void setColorMode(ColorMode); |
| 163 |
int startupSlide() const; |
| 164 |
void setCurrentSlide(int); |
| 165 |
void setupCurrentSlide(); |
| 166 |
void resizeCurrentSlide(int width, int height); |
| 167 |
SkISize currentSlideSize() const; |
| 168 |
void listNames() const; |
| 169 |
void dumpShadersToResources(); |
| 170 |
|
| 171 |
void updateUIState(); |
| 172 |
|
| 173 |
void drawSlide(SkSurface* surface); |
| 174 |
void drawImGui(); |
| 175 |
|
| 176 |
void changeZoomLevel(float delta); |
| 177 |
void updateGestureTransLimit(); |
| 178 |
SkMatrix computePreTouchMatrix(); |
| 179 |
SkMatrix computePerspectiveMatrix(); |
| 180 |
SkMatrix computeMatrix(); |
| 181 |
SkPoint mapEvent(float x, float y); |
| 182 |
|
| 183 |
sk_app::Window* fWindow; |
| 184 |
|
| 185 |
StatsLayer fStatsLayer; |
| 186 |
StatsLayer::Timer fPaintTimer; |
| 187 |
StatsLayer::Timer fFlushTimer; |
| 188 |
StatsLayer::Timer fAnimateTimer; |
| 189 |
|
| 190 |
AnimTimer fAnimTimer; |
| 191 |
skia_private::TArray<sk_sp<Slide>> fSlides; |
| 192 |
int fCurrentSlide; |
| 193 |
|
| 194 |
bool fRefresh; // whether to continuously refresh for measuring render time |
| 195 |
|
| 196 |
bool fSaveToSKP; |
| 197 |
bool fShowSlideDimensions; |
| 198 |
|
| 199 |
ImGuiLayer fImGuiLayer; |
| 200 |
SkPaint fImGuiGamutPaint; |
| 201 |
bool fShowImGuiDebugWindow; |
| 202 |
bool fShowSlidePicker; |
| 203 |
bool fShowImGuiTestWindow; |
| 204 |
bool fShowHistogramWindow; |
| 205 |
|
| 206 |
bool fShowZoomWindow; |
| 207 |
bool fZoomWindowFixed; |
| 208 |
SkPoint fZoomWindowLocation; |
| 209 |
sk_sp<SkImage> fLastImage; |
| 210 |
bool fZoomUI; |
| 211 |
|
| 212 |
sk_app::Window::BackendType fBackendType; |
| 213 |
|
| 214 |
// Color properties for slide rendering |
| 215 |
ColorMode fColorMode; |
| 216 |
SkColorSpacePrimaries fColorSpacePrimaries; |
| 217 |
skcms_TransferFunction fColorSpaceTransferFn; |
| 218 |
|
| 219 |
// transform data |
| 220 |
bool fApplyBackingScale; |
| 221 |
SkScalar fZoomLevel; |
| 222 |
SkScalar fRotation; |
| 223 |
SkVector fOffset; |
| 224 |
|
| 225 |
sk_app::CommandSet fCommands; |
| 226 |
|
| 227 |
enum class GestureDevice { |
| 228 |
kNone, |
| 229 |
kTouch, |
| 230 |
kMouse, |
| 231 |
}; |
| 232 |
|
| 233 |
TouchGesture fGesture; |
| 234 |
GestureDevice fGestureDevice; |
| 235 |
|
| 236 |
// identity unless the window initially scales the content to fit the screen. |
| 237 |
SkMatrix fDefaultMatrix; |
| 238 |
|
| 239 |
bool fTiled; |
| 240 |
bool fDrawTileBoundaries; |
| 241 |
SkSize fTileScale; |
| 242 |
bool fDrawViaSerialize = false; |
| 243 |
|
| 244 |
enum PerspectiveMode { |
| 245 |
kPerspective_Off, |
| 246 |
kPerspective_Real, |
| 247 |
kPerspective_Fake, |
| 248 |
}; |
| 249 |
PerspectiveMode fPerspectiveMode; |
| 250 |
SkPoint fPerspectivePoints[4]; |
| 251 |
|
| 252 |
skia_private::TArray<std::function<void()>> fDeferredActions; |
| 253 |
|
| 254 |
// fPaint contains override values, fPaintOverrides controls if overrides are applied. |
| 255 |
SkPaint fPaint; |
| 256 |
SkPaintFields fPaintOverrides; |
| 257 |
|
| 258 |
// fFont contains override values, fFontOverrides controls if overrides are applied. |
| 259 |
SkFont fFont; |
| 260 |
SkFontFields fFontOverrides; |
| 261 |
|
| 262 |
// fDisplay contains default values (fWindow.fRequestedDisplayParams contains the overrides), |
| 263 |
// fDisplayOverrides controls if overrides are applied. |
| 264 |
std::unique_ptr<skwindow::DisplayParams> fDisplay; |
| 265 |
DisplayFields fDisplayOverrides; |
| 266 |
|
| 267 |
struct CachedShader { |
| 268 |
bool fHovered = false; |
| 269 |
|
| 270 |
sk_sp<const SkData> fKey; |
| 271 |
SkString fKeyString; |
| 272 |
SkString fKeyDescription; |
| 273 |
|
| 274 |
SkFourByteTag fShaderType; |
| 275 |
|
| 276 |
static constexpr int kVertexIndex = 0; |
| 277 |
static constexpr int kFragmentIndex = 1; |
| 278 |
static constexpr int kShaderTypeCount = 2; |
| 279 |
|
| 280 |
SkSL::NativeShader fShader[kShaderTypeCount]; |
| 281 |
SkSL::Program::Interface fInterfaces[kShaderTypeCount]; |
| 282 |
}; |
| 283 |
|
| 284 |
#if defined(SK_GANESH) |
| 285 |
sk_gpu_test::MemoryCache fPersistentCache; |
| 286 |
#endif |
| 287 |
skia_private::TArray<CachedShader> fCachedShaders; |
| 288 |
|
| 289 |
enum ShaderOptLevel : int { |
| 290 |
kShaderOptLevel_Source, |
| 291 |
kShaderOptLevel_Compile, |
| 292 |
kShaderOptLevel_Optimize, |
| 293 |
kShaderOptLevel_Inline, |
| 294 |
}; |
| 295 |
ShaderOptLevel fOptLevel = kShaderOptLevel_Source; |
| 296 |
}; |
| 297 |
|
| 298 |
#endif |