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