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