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