• 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/sk_app/DisplayParams.h"
15 #include "tools/skui/InputState.h"
16 #include "tools/skui/Key.h"
17 #include "tools/skui/ModifierKey.h"
18 
19 #include <functional>
20 
21 class GrDirectContext;
22 class SkCanvas;
23 class SkSurface;
24 class SkSurfaceProps;
25 class SkString;
26 
27 namespace sk_app {
28 
29 class WindowContext;
30 
31 class Window {
32 public:
33     static Window* CreateNativeWindow(void* platformData);
34 
35     virtual ~Window();
36 
37     virtual void setTitle(const char*) = 0;
38     virtual void show() = 0;
39 
40     // JSON-formatted UI state for Android. Do nothing by default
setUIState(const char *)41     virtual void setUIState(const char*) {}
42 
43     // Interface to the system clipboard. Only implemented on UNIX.
getClipboardText()44     virtual const char* getClipboardText() { return nullptr; }
setClipboardText(const char *)45     virtual void        setClipboardText(const char*) {}
46 
47     // Schedules an invalidation event for window if one is not currently pending.
48     // Make sure that either onPaint or markInvalReceived is called when the client window consumes
49     // the the inval event. They unset fIsContentInvalided which allow future onInval.
50     void inval();
51 
scaleContentToFit()52     virtual bool scaleContentToFit() const { return false; }
53 
54     enum BackendType {
55 #ifdef SK_GL
56         kNativeGL_BackendType,
57 #endif
58 #if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
59         kANGLE_BackendType,
60 #endif
61 #ifdef SK_DAWN
62         kDawn_BackendType,
63 #endif
64 #ifdef SK_VULKAN
65         kVulkan_BackendType,
66 #endif
67 #ifdef SK_METAL
68         kMetal_BackendType,
69 #endif
70 #ifdef SK_DIRECT3D
71         kDirect3D_BackendType,
72 #endif
73         kRaster_BackendType,
74 
75         kLast_BackendType = kRaster_BackendType
76     };
77     enum {
78         kBackendTypeCount = kLast_BackendType + 1
79     };
80 
81     virtual bool attach(BackendType) = 0;
82     void detach();
83 
84     // input handling
85 
86     class Layer {
87     public:
Layer()88         Layer() : fActive(true) {}
89         virtual ~Layer() = default;
90 
getActive()91         bool getActive() { return fActive; }
setActive(bool active)92         void setActive(bool active) { fActive = active; }
93 
94         // return value of 'true' means 'I have handled this event'
onBackendCreated()95         virtual void onBackendCreated() {}
onAttach(Window * window)96         virtual void onAttach(Window* window) {}
onChar(SkUnichar c,skui::ModifierKey)97         virtual bool onChar(SkUnichar c, skui::ModifierKey) { return false; }
onKey(skui::Key,skui::InputState,skui::ModifierKey)98         virtual bool onKey(skui::Key, skui::InputState, skui::ModifierKey) { return false; }
onMouse(int x,int y,skui::InputState,skui::ModifierKey)99         virtual bool onMouse(int x, int y, skui::InputState, skui::ModifierKey) { return false; }
onMouseWheel(float delta,skui::ModifierKey)100         virtual bool onMouseWheel(float delta, skui::ModifierKey) { return false; }
onTouch(intptr_t owner,skui::InputState,float x,float y)101         virtual bool onTouch(intptr_t owner, skui::InputState, float x, float y) { return false; }
102         // Platform-detected gesture events
onFling(skui::InputState state)103         virtual bool onFling(skui::InputState state) { return false; }
onPinch(skui::InputState state,float scale,float x,float y)104         virtual bool onPinch(skui::InputState state, float scale, float x, float y) { return false; }
onUIStateChanged(const SkString & stateName,const SkString & stateValue)105         virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
onPrePaint()106         virtual void onPrePaint() {}
onPaint(SkSurface *)107         virtual void onPaint(SkSurface*) {}
onResize(int width,int height)108         virtual void onResize(int width, int height) {}
109 
110     private:
111         friend class Window;
112         bool fActive;
113     };
114 
pushLayer(Layer * layer)115     void pushLayer(Layer* layer) {
116         layer->onAttach(this);
117         fLayers.push_back(layer);
118     }
119 
120     void onBackendCreated();
121     bool onChar(SkUnichar c, skui::ModifierKey modifiers);
122     bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers);
123     bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers);
124     bool onMouseWheel(float delta, skui::ModifierKey modifiers);
125     bool onTouch(intptr_t owner, skui::InputState state, float x, float y);  // multi-owner = multi-touch
126     // Platform-detected gesture events
127     bool onFling(skui::InputState state);
128     bool onPinch(skui::InputState state, float scale, float x, float y);
129     void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
130     void onPaint();
131     void onResize(int width, int height);
132     void onActivate(bool isActive);
133 
134     int width() const;
135     int height() const;
scaleFactor()136     virtual float scaleFactor() const { return 1.0f; }
137 
getRequestedDisplayParams()138     virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
139     virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
140 
141     // Actual parameters in effect, obtained from the native window.
142     int sampleCount() const;
143     int stencilBits() const;
144 
145     // Returns null if there is not a GPU backend or if the backend is not yet created.
146     GrDirectContext* directContext() const;
147 
148 protected:
149     Window();
150 
151     SkTDArray<Layer*>      fLayers;
152     DisplayParams          fRequestedDisplayParams;
153     bool                   fIsActive = true;
154 
155     std::unique_ptr<WindowContext> fWindowContext;
156 
157     virtual void onInval() = 0;
158 
159     // Uncheck fIsContentInvalided to allow future inval/onInval.
160     void markInvalProcessed();
161 
162     bool fIsContentInvalidated = false;  // use this to avoid duplicate invalidate events
163 
164     void visitLayers(std::function<void(Layer*)> visitor);
165     bool signalLayers(std::function<bool(Layer*)> visitor);
166 };
167 
168 }   // namespace sk_app
169 #endif
170