blob: 8a71f3566f648715b654a05aab267024afda3456 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
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
brianosman05de2162016-05-06 13:28:57 -070011#include "DisplayParams.h"
djsollen12d62a72016-04-21 07:59:44 -070012#include "SkRect.h"
liyuqiand3cdbca2016-05-17 12:44:20 -070013#include "SkTouchGesture.h"
14#include "SkTypes.h"
jvanverth9fab59d2016-04-06 12:08:51 -070015
csmartdalton61cd31a2017-02-27 17:00:53 -070016class GrContext;
jvanverth9f372462016-04-06 06:08:59 -070017class SkCanvas;
liyuqian74959a12016-06-16 14:10:34 -070018class SkSurface;
Ben Wagner37c54032018-04-13 14:30:23 -040019class SkSurfaceProps;
jvanverth9f372462016-04-06 06:08:59 -070020
jvanverth34524262016-05-04 13:49:13 -070021namespace sk_app {
22
jvanvertha8d0d6c2016-05-05 12:32:03 -070023class WindowContext;
24
jvanverth9f372462016-04-06 06:08:59 -070025class Window {
26public:
27 static Window* CreateNativeWindow(void* platformData);
28
Brian Osman2dd96932016-10-18 15:33:53 -040029 virtual ~Window() { this->detach(); }
jvanverth9f372462016-04-06 06:08:59 -070030
31 virtual void setTitle(const char*) = 0;
32 virtual void show() = 0;
Brian Osmaneff04b52017-11-21 13:18:02 -050033
34 // JSON-formatted UI state for Android. Do nothing by default
35 virtual void setUIState(const char*) {}
liyuqian566c8e42016-05-23 10:52:34 -070036
37 // Shedules an invalidation event for window if one is not currently pending.
38 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
39 // the the inval event. They unset fIsContentInvalided which allow future onInval.
40 void inval();
jvanverth9f372462016-04-06 06:08:59 -070041
djsollen12d62a72016-04-21 07:59:44 -070042 virtual bool scaleContentToFit() const { return false; }
djsollen12d62a72016-04-21 07:59:44 -070043
jvanverthaf236b52016-05-20 06:01:06 -070044 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070045 kNativeGL_BackendType,
Brian Salomon194db172017-08-17 14:37:06 -040046#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
47 kANGLE_BackendType,
48#endif
jvanverth063ece72016-06-17 09:29:14 -070049#ifdef SK_VULKAN
jvanverthaf236b52016-05-20 06:01:06 -070050 kVulkan_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070051#endif
liyuqiand94ad582016-06-07 14:22:37 -070052 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070053
liyuqiand94ad582016-06-07 14:22:37 -070054 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070055 };
56 enum {
57 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070058 };
59
csmartdalton578f0642017-02-24 16:04:47 -070060 virtual bool attach(BackendType) = 0;
jvanverth9f372462016-04-06 06:08:59 -070061 void detach();
62
63 // input handling
brianosman622c8d52016-05-10 06:50:49 -070064 enum class Key {
65 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070066
brianosman622c8d52016-05-10 06:50:49 -070067 kLeftSoftKey,
68 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070069
brianosman622c8d52016-05-10 06:50:49 -070070 kHome, //!< the home key - added to match android
71 kBack, //!< (CLR)
72 kSend, //!< the green (talk) key
73 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070074
brianosman622c8d52016-05-10 06:50:49 -070075 k0,
76 k1,
77 k2,
78 k3,
79 k4,
80 k5,
81 k6,
82 k7,
83 k8,
84 k9,
85 kStar, //!< the * key
86 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070087
brianosman622c8d52016-05-10 06:50:49 -070088 kUp,
89 kDown,
90 kLeft,
91 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -070092
Brian Osman79086b92017-02-10 13:36:16 -050093 // Keys needed by ImGui
94 kTab,
95 kPageUp,
96 kPageDown,
97 kDelete,
98 kEscape,
99 kShift,
100 kCtrl,
101 kOption, // AKA Alt
102 kA,
103 kC,
104 kV,
105 kX,
106 kY,
107 kZ,
108
brianosman622c8d52016-05-10 06:50:49 -0700109 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -0700110
brianosman622c8d52016-05-10 06:50:49 -0700111 kVolUp, //!< volume up - match android
112 kVolDown, //!< volume down - same
113 kPower, //!< power button - same
114 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -0700115
brianosman622c8d52016-05-10 06:50:49 -0700116 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -0700117 };
brianosman622c8d52016-05-10 06:50:49 -0700118 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -0700119
120 enum ModifierKeys {
121 kShift_ModifierKey = 1 << 0,
122 kControl_ModifierKey = 1 << 1,
123 kOption_ModifierKey = 1 << 2, // same as ALT
124 kCommand_ModifierKey = 1 << 3,
125 kFirstPress_ModifierKey = 1 << 4,
126 };
127
128 enum InputState {
129 kDown_InputState,
130 kUp_InputState,
131 kMove_InputState // only valid for mouse
132 };
133
Brian Osman80fc07e2017-12-08 16:45:43 -0500134 class Layer {
135 public:
Brian Osman56a24812017-12-19 11:15:16 -0500136 Layer() : fActive(true) {}
Brian Osman80fc07e2017-12-08 16:45:43 -0500137 virtual ~Layer() = default;
jvanverth9f372462016-04-06 06:08:59 -0700138
Brian Osman56a24812017-12-19 11:15:16 -0500139 bool getActive() { return fActive; }
140 void setActive(bool active) { fActive = active; }
141
Brian Osman80fc07e2017-12-08 16:45:43 -0500142 // return value of 'true' means 'I have handled this event'
143 virtual void onBackendCreated() {}
Brian Osmand67e5182017-12-08 16:46:09 -0500144 virtual void onAttach(Window* window) {}
Brian Osman80fc07e2017-12-08 16:45:43 -0500145 virtual bool onChar(SkUnichar c, uint32_t modifiers) { return false; }
146 virtual bool onKey(Key key, InputState state, uint32_t modifiers) { return false; }
147 virtual bool onMouse(int x, int y, InputState state, uint32_t modifiers) { return false; }
148 virtual bool onMouseWheel(float delta, uint32_t modifiers) { return false; }
149 virtual bool onTouch(intptr_t owner, InputState state, float x, float y) { return false; }
150 virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
Brian Osmand67e5182017-12-08 16:46:09 -0500151 virtual void onPrePaint() {}
Brian Osman80fc07e2017-12-08 16:45:43 -0500152 virtual void onPaint(SkCanvas*) {}
Brian Osman56a24812017-12-19 11:15:16 -0500153
154 private:
155 friend class Window;
156 bool fActive;
Brian Osman80fc07e2017-12-08 16:45:43 -0500157 };
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700158
Brian Osman80fc07e2017-12-08 16:45:43 -0500159 void pushLayer(Layer* layer) {
Brian Osmand67e5182017-12-08 16:46:09 -0500160 layer->onAttach(this);
Brian Osman80fc07e2017-12-08 16:45:43 -0500161 fLayers.push(layer);
liyuqiane5a6cd92016-05-27 08:52:52 -0700162 }
163
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700164 void onBackendCreated();
jvanverth9fab59d2016-04-06 12:08:51 -0700165 bool onChar(SkUnichar c, uint32_t modifiers);
166 bool onKey(Key key, InputState state, uint32_t modifiers);
167 bool onMouse(int x, int y, InputState state, uint32_t modifiers);
Brian Osman79086b92017-02-10 13:36:16 -0500168 bool onMouseWheel(float delta, uint32_t modifiers);
jvanverth814e38d2016-06-06 08:48:47 -0700169 bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
liyuqiane5a6cd92016-05-27 08:52:52 -0700170 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700171 void onPaint();
bsalomonccde4ab2016-07-27 08:50:12 -0700172 void onResize(int width, int height);
jvanverth9f372462016-04-06 06:08:59 -0700173
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700174 int width();
175 int height();
jvanverthc265a922016-04-08 12:51:45 -0700176
csmartdalton578f0642017-02-24 16:04:47 -0700177 virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
Brian Osman2ac96dc2017-06-23 13:32:29 -0400178 virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
csmartdalton578f0642017-02-24 16:04:47 -0700179
180 // Actual parameters in effect, obtained from the native window.
181 int sampleCount() const;
182 int stencilBits() const;
brianosman05de2162016-05-06 13:28:57 -0700183
csmartdalton61cd31a2017-02-27 17:00:53 -0700184 // Returns null if there is not a GPU backend or if the backend is not yet created.
185 const GrContext* getGrContext() const;
186
jvanverth9f372462016-04-06 06:08:59 -0700187protected:
188 Window();
189
Brian Osman80fc07e2017-12-08 16:45:43 -0500190 SkTDArray<Layer*> fLayers;
csmartdalton578f0642017-02-24 16:04:47 -0700191 DisplayParams fRequestedDisplayParams;
jvanverth9f372462016-04-06 06:08:59 -0700192
liyuqiand3cdbca2016-05-17 12:44:20 -0700193 WindowContext* fWindowContext = nullptr;
liyuqian566c8e42016-05-23 10:52:34 -0700194
195 virtual void onInval() = 0;
196
197 // Uncheck fIsContentInvalided to allow future inval/onInval.
198 void markInvalProcessed();
199
200 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
Brian Osman56a24812017-12-19 11:15:16 -0500201
202 void visitLayers(std::function<void(Layer*)> visitor);
203 bool signalLayers(std::function<bool(Layer*)> visitor);
jvanverth9f372462016-04-06 06:08:59 -0700204};
205
jvanverth34524262016-05-04 13:49:13 -0700206} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700207#endif