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 "SkJSONCPP.h"
19#include "SkTouchGesture.h"
20#include "Slide.h"
21#include "StatsLayer.h"
22
23class SkCanvas;
24
25class Viewer : public sk_app::Application, sk_app::Window::Layer {
26public:
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    bool onTouch(intptr_t owner, sk_app::Window::InputState state, float x, float y) override;
35    bool onMouse(int x, int y, sk_app::Window::InputState state, uint32_t modifiers) override;
36    void onUIStateChanged(const SkString& stateName, const SkString& stateValue) override;
37    bool onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers) override;
38    bool onChar(SkUnichar c, uint32_t modifiers) override;
39
40    struct SkPaintFields {
41        bool fTypeface = false;
42        bool fPathEffect = false;
43        bool fShader = false;
44        bool fMaskFilter = false;
45        bool fColorFilter = false;
46        bool fDrawLooper = false;
47        bool fImageFilter = false;
48
49        bool fTextSize = false;
50        bool fTextScaleX = false;
51        bool fTextSkewX = false;
52        bool fColor = false;
53        bool fWidth = false;
54        bool fMiterLimit = false;
55        bool fBlendMode = false;
56
57        uint32_t fFlags = 0;
58        enum class AntiAliasState {
59            Alias,
60            Normal,
61            AnalyticAAEnabled,
62            AnalyticAAForced,
63            DeltaAAEnabled,
64            DeltaAAForced,
65        } fAntiAlias = AntiAliasState::Alias;
66        bool fOriginalSkUseAnalyticAA = false;
67        bool fOriginalSkForceAnalyticAA = false;
68        bool fOriginalSkUseDeltaAA = false;
69        bool fOriginalSkForceDeltaAA = false;
70
71        bool fTextAlign = false;
72        bool fCapType = false;
73        bool fJoinType = false;
74        bool fStyle = false;
75        bool fTextEncoding = false;
76        bool fHinting = false;
77        bool fFilterQuality = false;
78    };
79private:
80    enum class ColorMode {
81        kLegacy,                                 // N32, no color management
82        kColorManagedSRGB8888_NonLinearBlending, // N32, sRGB transfer function, nonlinear blending
83        kColorManagedSRGB8888,                   // N32, sRGB transfer function, linear blending
84        kColorManagedLinearF16,                  // F16, linear transfer function, linear blending
85    };
86
87    void initSlides();
88    void updateTitle();
89    void setBackend(sk_app::Window::BackendType);
90    void setColorMode(ColorMode);
91    int startupSlide() const;
92    void setCurrentSlide(int);
93    void setupCurrentSlide();
94    void listNames() const;
95
96    void updateUIState();
97
98    void drawSlide(SkCanvas* canvs);
99    void drawImGui();
100
101    void changeZoomLevel(float delta);
102    SkMatrix computeMatrix();
103    SkPoint mapEvent(float x, float y);
104
105    void resetExecutor() {
106        fExecutor = SkExecutor::MakeFIFOThreadPool(fThreadCnt == 0 ? fTileCnt : fThreadCnt);
107    }
108
109    sk_app::Window*        fWindow;
110
111    StatsLayer             fStatsLayer;
112    StatsLayer::Timer      fPaintTimer;
113    StatsLayer::Timer      fFlushTimer;
114    StatsLayer::Timer      fAnimateTimer;
115
116    SkAnimTimer            fAnimTimer;
117    SkTArray<sk_sp<Slide>> fSlides;
118    int                    fCurrentSlide;
119
120    bool                   fRefresh; // whether to continuously refresh for measuring render time
121
122    bool                   fSaveToSKP;
123
124    ImGuiLayer             fImGuiLayer;
125    SkPaint                fImGuiGamutPaint;
126    bool                   fShowImGuiDebugWindow;
127    bool                   fShowSlidePicker;
128    bool                   fShowImGuiTestWindow;
129
130    bool                   fShowZoomWindow;
131    sk_sp<SkImage>         fLastImage;
132
133    sk_app::Window::BackendType fBackendType;
134
135    // Color properties for slide rendering
136    ColorMode              fColorMode;
137    SkColorSpacePrimaries  fColorSpacePrimaries;
138    SkColorSpaceTransferFn fColorSpaceTransferFn;
139
140    // transform data
141    SkScalar               fZoomLevel;
142
143    sk_app::CommandSet     fCommands;
144
145    enum class GestureDevice {
146        kNone,
147        kTouch,
148        kMouse,
149    };
150
151    SkTouchGesture         fGesture;
152    GestureDevice          fGestureDevice;
153
154    // identity unless the window initially scales the content to fit the screen.
155    SkMatrix               fDefaultMatrix;
156
157    SkTArray<std::function<void(void)>> fDeferredActions;
158
159    Json::Value            fAllSlideNames; // cache all slide names for fast updateUIState
160
161    int fTileCnt;
162    int fThreadCnt;
163    std::unique_ptr<SkExecutor> fExecutor;
164
165    SkPaint fPaint;
166    SkPaintFields fPaintOverrides;
167};
168
169
170#endif
171