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