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