blob: 735786051260028431522a8d96dea9da5acee1c8 [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"
liyuqiane5a6cd92016-05-27 08:52:52 -070015#include "SkJSONCPP.h"
jvanverth9fab59d2016-04-06 12:08:51 -070016
csmartdalton61cd31a2017-02-27 17:00:53 -070017class GrContext;
jvanverth9f372462016-04-06 06:08:59 -070018class SkCanvas;
liyuqian74959a12016-06-16 14:10:34 -070019class SkSurface;
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;
liyuqiane5a6cd92016-05-27 08:52:52 -070033 virtual void setUIState(const Json::Value& state) {} // do nothing in default
liyuqian566c8e42016-05-23 10:52:34 -070034
35 // Shedules an invalidation event for window if one is not currently pending.
36 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
37 // the the inval event. They unset fIsContentInvalided which allow future onInval.
38 void inval();
jvanverth9f372462016-04-06 06:08:59 -070039
djsollen12d62a72016-04-21 07:59:44 -070040 virtual bool scaleContentToFit() const { return false; }
djsollen12d62a72016-04-21 07:59:44 -070041
jvanverthaf236b52016-05-20 06:01:06 -070042 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070043 kNativeGL_BackendType,
Brian Salomon194db172017-08-17 14:37:06 -040044#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
45 kANGLE_BackendType,
46#endif
jvanverth063ece72016-06-17 09:29:14 -070047#ifdef SK_VULKAN
jvanverthaf236b52016-05-20 06:01:06 -070048 kVulkan_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070049#endif
liyuqiand94ad582016-06-07 14:22:37 -070050 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070051
liyuqiand94ad582016-06-07 14:22:37 -070052 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070053 };
54 enum {
55 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070056 };
57
csmartdalton578f0642017-02-24 16:04:47 -070058 virtual bool attach(BackendType) = 0;
jvanverth9f372462016-04-06 06:08:59 -070059 void detach();
60
61 // input handling
brianosman622c8d52016-05-10 06:50:49 -070062 enum class Key {
63 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070064
brianosman622c8d52016-05-10 06:50:49 -070065 kLeftSoftKey,
66 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070067
brianosman622c8d52016-05-10 06:50:49 -070068 kHome, //!< the home key - added to match android
69 kBack, //!< (CLR)
70 kSend, //!< the green (talk) key
71 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070072
brianosman622c8d52016-05-10 06:50:49 -070073 k0,
74 k1,
75 k2,
76 k3,
77 k4,
78 k5,
79 k6,
80 k7,
81 k8,
82 k9,
83 kStar, //!< the * key
84 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070085
brianosman622c8d52016-05-10 06:50:49 -070086 kUp,
87 kDown,
88 kLeft,
89 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -070090
Brian Osman79086b92017-02-10 13:36:16 -050091 // Keys needed by ImGui
92 kTab,
93 kPageUp,
94 kPageDown,
95 kDelete,
96 kEscape,
97 kShift,
98 kCtrl,
99 kOption, // AKA Alt
100 kA,
101 kC,
102 kV,
103 kX,
104 kY,
105 kZ,
106
brianosman622c8d52016-05-10 06:50:49 -0700107 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -0700108
brianosman622c8d52016-05-10 06:50:49 -0700109 kVolUp, //!< volume up - match android
110 kVolDown, //!< volume down - same
111 kPower, //!< power button - same
112 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -0700113
brianosman622c8d52016-05-10 06:50:49 -0700114 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -0700115 };
brianosman622c8d52016-05-10 06:50:49 -0700116 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -0700117
118 enum ModifierKeys {
119 kShift_ModifierKey = 1 << 0,
120 kControl_ModifierKey = 1 << 1,
121 kOption_ModifierKey = 1 << 2, // same as ALT
122 kCommand_ModifierKey = 1 << 3,
123 kFirstPress_ModifierKey = 1 << 4,
124 };
125
126 enum InputState {
127 kDown_InputState,
128 kUp_InputState,
129 kMove_InputState // only valid for mouse
130 };
131
132 // return value of 'true' means 'I have handled this event'
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700133 typedef void(*OnBackendCreatedFunc)(void* userData);
jvanverth9fab59d2016-04-06 12:08:51 -0700134 typedef bool(*OnCharFunc)(SkUnichar c, uint32_t modifiers, void* userData);
135 typedef bool(*OnKeyFunc)(Key key, InputState state, uint32_t modifiers, void* userData);
136 typedef bool(*OnMouseFunc)(int x, int y, InputState state, uint32_t modifiers, void* userData);
Brian Osman79086b92017-02-10 13:36:16 -0500137 typedef bool(*OnMouseWheelFunc)(float delta, uint32_t modifiers, void* userData);
jvanverth814e38d2016-06-06 08:48:47 -0700138 typedef bool(*OnTouchFunc)(intptr_t owner, InputState state, float x, float y, void* userData);
liyuqiane5a6cd92016-05-27 08:52:52 -0700139 typedef void(*OnUIStateChangedFunc)(
140 const SkString& stateName, const SkString& stateValue, void* userData);
jvanverth9f372462016-04-06 06:08:59 -0700141 typedef void(*OnPaintFunc)(SkCanvas*, void* userData);
142
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700143 void registerBackendCreatedFunc(OnBackendCreatedFunc func, void* userData) {
144 fBackendCreatedFunc = func;
145 fBackendCreatedUserData = userData;
146 }
147
jvanverth9fab59d2016-04-06 12:08:51 -0700148 void registerCharFunc(OnCharFunc func, void* userData) {
149 fCharFunc = func;
150 fCharUserData = userData;
151 }
152
jvanverth9f372462016-04-06 06:08:59 -0700153 void registerKeyFunc(OnKeyFunc func, void* userData) {
154 fKeyFunc = func;
155 fKeyUserData = userData;
156 }
157
158 void registerMouseFunc(OnMouseFunc func, void* userData) {
159 fMouseFunc = func;
160 fMouseUserData = userData;
161 }
162
Brian Osman79086b92017-02-10 13:36:16 -0500163 void registerMouseWheelFunc(OnMouseWheelFunc func, void* userData) {
164 fMouseWheelFunc = func;
165 fMouseWheelUserData = userData;
166 }
167
jvanverth9f372462016-04-06 06:08:59 -0700168 void registerPaintFunc(OnPaintFunc func, void* userData) {
169 fPaintFunc = func;
170 fPaintUserData = userData;
171 }
172
liyuqiand3cdbca2016-05-17 12:44:20 -0700173 void registerTouchFunc(OnTouchFunc func, void* userData) {
174 fTouchFunc = func;
175 fTouchUserData = userData;
176 }
177
liyuqiane5a6cd92016-05-27 08:52:52 -0700178 void registerUIStateChangedFunc(OnUIStateChangedFunc func, void* userData) {
179 fUIStateChangedFunc = func;
180 fUIStateChangedUserData = userData;
181 }
182
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700183 void onBackendCreated();
jvanverth9fab59d2016-04-06 12:08:51 -0700184 bool onChar(SkUnichar c, uint32_t modifiers);
185 bool onKey(Key key, InputState state, uint32_t modifiers);
186 bool onMouse(int x, int y, InputState state, uint32_t modifiers);
Brian Osman79086b92017-02-10 13:36:16 -0500187 bool onMouseWheel(float delta, uint32_t modifiers);
jvanverth814e38d2016-06-06 08:48:47 -0700188 bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
liyuqiane5a6cd92016-05-27 08:52:52 -0700189 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700190 void onPaint();
bsalomonccde4ab2016-07-27 08:50:12 -0700191 void onResize(int width, int height);
jvanverth9f372462016-04-06 06:08:59 -0700192
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700193 int width();
194 int height();
jvanverthc265a922016-04-08 12:51:45 -0700195
csmartdalton578f0642017-02-24 16:04:47 -0700196 virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
Brian Osman2ac96dc2017-06-23 13:32:29 -0400197 virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
csmartdalton578f0642017-02-24 16:04:47 -0700198
199 // Actual parameters in effect, obtained from the native window.
200 int sampleCount() const;
201 int stencilBits() const;
brianosman05de2162016-05-06 13:28:57 -0700202
csmartdalton61cd31a2017-02-27 17:00:53 -0700203 // Returns null if there is not a GPU backend or if the backend is not yet created.
204 const GrContext* getGrContext() const;
205
jvanverth9f372462016-04-06 06:08:59 -0700206protected:
207 Window();
208
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700209 OnBackendCreatedFunc fBackendCreatedFunc;
210 void* fBackendCreatedUserData;
211 OnCharFunc fCharFunc;
212 void* fCharUserData;
213 OnKeyFunc fKeyFunc;
214 void* fKeyUserData;
215 OnMouseFunc fMouseFunc;
216 void* fMouseUserData;
217 OnMouseWheelFunc fMouseWheelFunc;
218 void* fMouseWheelUserData;
219 OnTouchFunc fTouchFunc;
220 void* fTouchUserData;
221 OnUIStateChangedFunc fUIStateChangedFunc;
222 void* fUIStateChangedUserData;
223 OnPaintFunc fPaintFunc;
224 void* fPaintUserData;
csmartdalton578f0642017-02-24 16:04:47 -0700225 DisplayParams fRequestedDisplayParams;
jvanverth9f372462016-04-06 06:08:59 -0700226
liyuqiand3cdbca2016-05-17 12:44:20 -0700227 WindowContext* fWindowContext = nullptr;
liyuqian566c8e42016-05-23 10:52:34 -0700228
229 virtual void onInval() = 0;
230
231 // Uncheck fIsContentInvalided to allow future inval/onInval.
232 void markInvalProcessed();
233
234 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
jvanverth9f372462016-04-06 06:08:59 -0700235};
236
jvanverth34524262016-05-04 13:49:13 -0700237} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700238#endif