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