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