• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void listNames() const;
156     void dumpShadersToResources();
157 
158     void updateUIState();
159 
160     void drawSlide(SkSurface* surface);
161     void drawImGui();
162 
163     void changeZoomLevel(float delta);
164     void preTouchMatrixChanged();
165     SkMatrix computePreTouchMatrix();
166     SkMatrix computePerspectiveMatrix();
167     SkMatrix computeMatrix();
168     SkPoint mapEvent(float x, float y);
169 
170     sk_app::Window*        fWindow;
171 
172     StatsLayer             fStatsLayer;
173     StatsLayer::Timer      fPaintTimer;
174     StatsLayer::Timer      fFlushTimer;
175     StatsLayer::Timer      fAnimateTimer;
176 
177     AnimTimer              fAnimTimer;
178     SkTArray<sk_sp<Slide>> fSlides;
179     int                    fCurrentSlide;
180 
181     bool                   fRefresh; // whether to continuously refresh for measuring render time
182 
183     bool                   fSaveToSKP;
184     bool                   fShowSlideDimensions;
185 
186     ImGuiLayer             fImGuiLayer;
187     SkPaint                fImGuiGamutPaint;
188     bool                   fShowImGuiDebugWindow;
189     bool                   fShowSlidePicker;
190     bool                   fShowImGuiTestWindow;
191 
192     bool                   fShowZoomWindow;
193     bool                   fZoomWindowFixed;
194     SkPoint                fZoomWindowLocation;
195     sk_sp<SkImage>         fLastImage;
196     bool                   fZoomUI;
197 
198     sk_app::Window::BackendType fBackendType;
199 
200     // Color properties for slide rendering
201     ColorMode              fColorMode;
202     SkColorSpacePrimaries  fColorSpacePrimaries;
203     skcms_TransferFunction fColorSpaceTransferFn;
204 
205     // transform data
206     bool                   fApplyBackingScale;
207     SkScalar               fZoomLevel;
208     SkScalar               fRotation;
209     SkVector               fOffset;
210 
211     sk_app::CommandSet     fCommands;
212 
213     enum class GestureDevice {
214         kNone,
215         kTouch,
216         kMouse,
217     };
218 
219     TouchGesture           fGesture;
220     GestureDevice          fGestureDevice;
221 
222     // identity unless the window initially scales the content to fit the screen.
223     SkMatrix               fDefaultMatrix;
224 
225     bool                   fTiled;
226     bool                   fDrawTileBoundaries;
227     SkSize                 fTileScale;
228     bool                   fDrawViaSerialize = false;
229 
230     enum PerspectiveMode {
231         kPerspective_Off,
232         kPerspective_Real,
233         kPerspective_Fake,
234     };
235     PerspectiveMode        fPerspectiveMode;
236     SkPoint                fPerspectivePoints[4];
237 
238     SkTArray<std::function<void()>> fDeferredActions;
239 
240     // fPaint contains override values, fPaintOverrides controls if overrides are applied.
241     SkPaint fPaint;
242     SkPaintFields fPaintOverrides;
243 
244     // fFont contains override values, fFontOverrides controls if overrides are applied.
245     SkFont fFont;
246     SkFontFields fFontOverrides;
247 
248     // fDisplay contains default values (fWindow.fRequestedDisplayParams contains the overrides),
249     // fDisplayOverrides controls if overrides are applied.
250     sk_app::DisplayParams fDisplay;
251     DisplayFields fDisplayOverrides;
252 
253     struct CachedShader {
254         bool                fHovered = false;
255 
256         sk_sp<const SkData> fKey;
257         SkString            fKeyString;
258         SkString            fKeyDescription;
259 
260         SkFourByteTag         fShaderType;
261         SkSL::String          fShader[kGrShaderTypeCount];
262         SkSL::Program::Inputs fInputs[kGrShaderTypeCount];
263     };
264 
265     sk_gpu_test::MemoryCache fPersistentCache;
266     SkTArray<CachedShader>   fCachedShaders;
267 
268     enum ShaderOptLevel : int {
269         kShaderOptLevel_Source,
270         kShaderOptLevel_Compile,
271         kShaderOptLevel_Optimize,
272         kShaderOptLevel_Inline,
273     };
274     ShaderOptLevel fOptLevel = kShaderOptLevel_Source;
275 
276     SkVMBlitter::Key fHoveredKey;
277     skvm::Program    fHoveredProgram;
278 
279     SkTHashMap<SkVMBlitter::Key, std::string> fDisassemblyCache;
280 };
281 
282 #endif
283