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