• 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 Window_DEFINED
9 #define Window_DEFINED
10 
11 #include "include/core/SkRect.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/SkTDArray.h"
14 #include "tools/InputState.h"
15 #include "tools/ModifierKey.h"
16 #include "tools/sk_app/DisplayParams.h"
17 
18 class GrContext;
19 class SkCanvas;
20 class SkSurface;
21 class SkSurfaceProps;
22 
23 namespace sk_app {
24 
25 class WindowContext;
26 
27 class Window {
28 public:
29     static Window* CreateNativeWindow(void* platformData);
30 
31     virtual ~Window();
32 
33     virtual void setTitle(const char*) = 0;
34     virtual void show() = 0;
35 
36     // JSON-formatted UI state for Android. Do nothing by default
setUIState(const char *)37     virtual void setUIState(const char*) {}
38 
39     // Shedules an invalidation event for window if one is not currently pending.
40     // Make sure that either onPaint or markInvalReceived is called when the client window consumes
41     // the the inval event. They unset fIsContentInvalided which allow future onInval.
42     void inval();
43 
scaleContentToFit()44     virtual bool scaleContentToFit() const { return false; }
45 
46     enum BackendType {
47         kNativeGL_BackendType,
48 #if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
49         kANGLE_BackendType,
50 #endif
51 #ifdef SK_DAWN
52         kDawn_BackendType,
53 #endif
54 #ifdef SK_VULKAN
55         kVulkan_BackendType,
56 #endif
57 #if SK_METAL && defined(SK_BUILD_FOR_MAC)
58         kMetal_BackendType,
59 #endif
60         kRaster_BackendType,
61 
62         kLast_BackendType = kRaster_BackendType
63     };
64     enum {
65         kBackendTypeCount = kLast_BackendType + 1
66     };
67 
68     virtual bool attach(BackendType) = 0;
69     void detach();
70 
71     // input handling
72     enum class Key {
73         kNONE,    //corresponds to android's UNKNOWN
74 
75         kLeftSoftKey,
76         kRightSoftKey,
77 
78         kHome,    //!< the home key - added to match android
79         kBack,    //!< (CLR)
80         kSend,    //!< the green (talk) key
81         kEnd,     //!< the red key
82 
83         k0,
84         k1,
85         k2,
86         k3,
87         k4,
88         k5,
89         k6,
90         k7,
91         k8,
92         k9,
93         kStar,    //!< the * key
94         kHash,    //!< the # key
95 
96         kUp,
97         kDown,
98         kLeft,
99         kRight,
100 
101         // Keys needed by ImGui
102         kTab,
103         kPageUp,
104         kPageDown,
105         kDelete,
106         kEscape,
107         kShift,
108         kCtrl,
109         kOption, // AKA Alt
110         kA,
111         kC,
112         kV,
113         kX,
114         kY,
115         kZ,
116 
117         kOK,      //!< the center key
118 
119         kVolUp,   //!< volume up    - match android
120         kVolDown, //!< volume down  - same
121         kPower,   //!< power button - same
122         kCamera,  //!< camera       - same
123 
124         kLast = kCamera
125     };
126     static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
127 
128     class Layer {
129     public:
Layer()130         Layer() : fActive(true) {}
131         virtual ~Layer() = default;
132 
getActive()133         bool getActive() { return fActive; }
setActive(bool active)134         void setActive(bool active) { fActive = active; }
135 
136         // return value of 'true' means 'I have handled this event'
onBackendCreated()137         virtual void onBackendCreated() {}
onAttach(Window * window)138         virtual void onAttach(Window* window) {}
onChar(SkUnichar c,ModifierKey modifiers)139         virtual bool onChar(SkUnichar c, ModifierKey modifiers) { return false; }
onKey(Key key,InputState state,ModifierKey modifiers)140         virtual bool onKey(Key key, InputState state, ModifierKey modifiers) { return false; }
onMouse(int x,int y,InputState state,ModifierKey modifiers)141         virtual bool onMouse(int x, int y, InputState state, ModifierKey modifiers) { return false; }
onMouseWheel(float delta,ModifierKey modifiers)142         virtual bool onMouseWheel(float delta, ModifierKey modifiers) { return false; }
onTouch(intptr_t owner,InputState state,float x,float y)143         virtual bool onTouch(intptr_t owner, InputState state, float x, float y) { return false; }
onUIStateChanged(const SkString & stateName,const SkString & stateValue)144         virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
onPrePaint()145         virtual void onPrePaint() {}
onPaint(SkSurface *)146         virtual void onPaint(SkSurface*) {}
onResize(int width,int height)147         virtual void onResize(int width, int height) {}
148 
149     private:
150         friend class Window;
151         bool fActive;
152     };
153 
pushLayer(Layer * layer)154     void pushLayer(Layer* layer) {
155         layer->onAttach(this);
156         fLayers.push_back(layer);
157     }
158 
159     void onBackendCreated();
160     bool onChar(SkUnichar c, ModifierKey modifiers);
161     bool onKey(Key key, InputState state, ModifierKey modifiers);
162     bool onMouse(int x, int y, InputState state, ModifierKey modifiers);
163     bool onMouseWheel(float delta, ModifierKey modifiers);
164     bool onTouch(intptr_t owner, InputState state, float x, float y);  // multi-owner = multi-touch
165     void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
166     void onPaint();
167     void onResize(int width, int height);
168 
169     int width() const;
170     int height() const;
171 
getRequestedDisplayParams()172     virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
173     virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
174 
175     // Actual parameters in effect, obtained from the native window.
176     int sampleCount() const;
177     int stencilBits() const;
178 
179     // Returns null if there is not a GPU backend or if the backend is not yet created.
180     GrContext* getGrContext() const;
181 
182 protected:
183     Window();
184 
185     SkTDArray<Layer*>      fLayers;
186     DisplayParams          fRequestedDisplayParams;
187 
188     std::unique_ptr<WindowContext> fWindowContext;
189 
190     virtual void onInval() = 0;
191 
192     // Uncheck fIsContentInvalided to allow future inval/onInval.
193     void markInvalProcessed();
194 
195     bool fIsContentInvalidated = false;  // use this to avoid duplicate invalidate events
196 
197     void visitLayers(std::function<void(Layer*)> visitor);
198     bool signalLayers(std::function<bool(Layer*)> visitor);
199 };
200 
201 }   // namespace sk_app
202 #endif
203