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