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