blob: f4d75cbe73b627c7617eb293ba507770e4ec5e34 [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
jvanverth9f372462016-04-06 06:08:59 -070017class SkCanvas;
liyuqian74959a12016-06-16 14:10:34 -070018class SkSurface;
jvanverth9f372462016-04-06 06:08:59 -070019
jvanverth34524262016-05-04 13:49:13 -070020namespace sk_app {
21
jvanvertha8d0d6c2016-05-05 12:32:03 -070022class WindowContext;
23
jvanverth9f372462016-04-06 06:08:59 -070024class Window {
25public:
26 static Window* CreateNativeWindow(void* platformData);
27
28 virtual ~Window() {};
29
30 virtual void setTitle(const char*) = 0;
31 virtual void show() = 0;
liyuqiane5a6cd92016-05-27 08:52:52 -070032 virtual void setUIState(const Json::Value& state) {} // do nothing in default
liyuqian566c8e42016-05-23 10:52:34 -070033
34 // Shedules an invalidation event for window if one is not currently pending.
35 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
36 // the the inval event. They unset fIsContentInvalided which allow future onInval.
37 void inval();
jvanverth9f372462016-04-06 06:08:59 -070038
djsollen12d62a72016-04-21 07:59:44 -070039 virtual bool scaleContentToFit() const { return false; }
40 virtual bool supportsContentRect() const { return false; }
41 virtual SkRect getContentRect() { return SkRect::MakeEmpty(); }
42
jvanverthaf236b52016-05-20 06:01:06 -070043 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070044 kNativeGL_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070045 kVulkan_BackendType,
liyuqiand94ad582016-06-07 14:22:37 -070046 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070047
liyuqiand94ad582016-06-07 14:22:37 -070048 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070049 };
50 enum {
51 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070052 };
53
jvanverthaf236b52016-05-20 06:01:06 -070054 virtual bool attach(BackendType attachType, const DisplayParams& params) = 0;
jvanverth9f372462016-04-06 06:08:59 -070055 void detach();
56
57 // input handling
brianosman622c8d52016-05-10 06:50:49 -070058 enum class Key {
59 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070060
brianosman622c8d52016-05-10 06:50:49 -070061 kLeftSoftKey,
62 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070063
brianosman622c8d52016-05-10 06:50:49 -070064 kHome, //!< the home key - added to match android
65 kBack, //!< (CLR)
66 kSend, //!< the green (talk) key
67 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070068
brianosman622c8d52016-05-10 06:50:49 -070069 k0,
70 k1,
71 k2,
72 k3,
73 k4,
74 k5,
75 k6,
76 k7,
77 k8,
78 k9,
79 kStar, //!< the * key
80 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070081
brianosman622c8d52016-05-10 06:50:49 -070082 kUp,
83 kDown,
84 kLeft,
85 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -070086
brianosman622c8d52016-05-10 06:50:49 -070087 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -070088
brianosman622c8d52016-05-10 06:50:49 -070089 kVolUp, //!< volume up - match android
90 kVolDown, //!< volume down - same
91 kPower, //!< power button - same
92 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -070093
brianosman622c8d52016-05-10 06:50:49 -070094 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -070095 };
brianosman622c8d52016-05-10 06:50:49 -070096 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -070097
98 enum ModifierKeys {
99 kShift_ModifierKey = 1 << 0,
100 kControl_ModifierKey = 1 << 1,
101 kOption_ModifierKey = 1 << 2, // same as ALT
102 kCommand_ModifierKey = 1 << 3,
103 kFirstPress_ModifierKey = 1 << 4,
104 };
105
106 enum InputState {
107 kDown_InputState,
108 kUp_InputState,
109 kMove_InputState // only valid for mouse
110 };
111
112 // return value of 'true' means 'I have handled this event'
113 typedef bool(*OnCharFunc)(SkUnichar c, uint32_t modifiers, void* userData);
114 typedef bool(*OnKeyFunc)(Key key, InputState state, uint32_t modifiers, void* userData);
115 typedef bool(*OnMouseFunc)(int x, int y, InputState state, uint32_t modifiers, void* userData);
jvanverth814e38d2016-06-06 08:48:47 -0700116 typedef bool(*OnTouchFunc)(intptr_t owner, InputState state, float x, float y, void* userData);
liyuqiane5a6cd92016-05-27 08:52:52 -0700117 typedef void(*OnUIStateChangedFunc)(
118 const SkString& stateName, const SkString& stateValue, void* userData);
jvanverth9f372462016-04-06 06:08:59 -0700119 typedef void(*OnPaintFunc)(SkCanvas*, void* userData);
120
jvanverth9fab59d2016-04-06 12:08:51 -0700121 void registerCharFunc(OnCharFunc func, void* userData) {
122 fCharFunc = func;
123 fCharUserData = userData;
124 }
125
jvanverth9f372462016-04-06 06:08:59 -0700126 void registerKeyFunc(OnKeyFunc func, void* userData) {
127 fKeyFunc = func;
128 fKeyUserData = userData;
129 }
130
131 void registerMouseFunc(OnMouseFunc func, void* userData) {
132 fMouseFunc = func;
133 fMouseUserData = userData;
134 }
135
136 void registerPaintFunc(OnPaintFunc func, void* userData) {
137 fPaintFunc = func;
138 fPaintUserData = userData;
139 }
140
liyuqiand3cdbca2016-05-17 12:44:20 -0700141 void registerTouchFunc(OnTouchFunc func, void* userData) {
142 fTouchFunc = func;
143 fTouchUserData = userData;
144 }
145
liyuqiane5a6cd92016-05-27 08:52:52 -0700146 void registerUIStateChangedFunc(OnUIStateChangedFunc func, void* userData) {
147 fUIStateChangedFunc = func;
148 fUIStateChangedUserData = userData;
149 }
150
jvanverth9fab59d2016-04-06 12:08:51 -0700151 bool onChar(SkUnichar c, uint32_t modifiers);
152 bool onKey(Key key, InputState state, uint32_t modifiers);
153 bool onMouse(int x, int y, InputState state, uint32_t modifiers);
jvanverth814e38d2016-06-06 08:48:47 -0700154 bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
liyuqiane5a6cd92016-05-27 08:52:52 -0700155 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700156 void onPaint();
jvanverth9fab59d2016-04-06 12:08:51 -0700157 void onResize(uint32_t width, uint32_t height);
jvanverth9f372462016-04-06 06:08:59 -0700158
jvanverthc265a922016-04-08 12:51:45 -0700159 uint32_t width() { return fWidth; }
160 uint32_t height() { return fHeight; }
161
liyuqian40d21de2016-05-12 09:17:04 -0700162 virtual const DisplayParams& getDisplayParams();
brianosman05de2162016-05-06 13:28:57 -0700163 void setDisplayParams(const DisplayParams& params);
164
liyuqian74959a12016-06-16 14:10:34 -0700165 // This is just for the sRGB split screen
166 sk_sp<SkSurface> getOffscreenSurface(bool forceSRGB);
167
jvanverth9f372462016-04-06 06:08:59 -0700168protected:
169 Window();
170
jvanverthc265a922016-04-08 12:51:45 -0700171 uint32_t fWidth;
172 uint32_t fHeight;
173
jvanverth9fab59d2016-04-06 12:08:51 -0700174 OnCharFunc fCharFunc;
175 void* fCharUserData;
jvanverth9f372462016-04-06 06:08:59 -0700176 OnKeyFunc fKeyFunc;
177 void* fKeyUserData;
178 OnMouseFunc fMouseFunc;
179 void* fMouseUserData;
liyuqiand3cdbca2016-05-17 12:44:20 -0700180 OnTouchFunc fTouchFunc;
181 void* fTouchUserData;
liyuqiane5a6cd92016-05-27 08:52:52 -0700182 OnUIStateChangedFunc
183 fUIStateChangedFunc;
184 void* fUIStateChangedUserData;
jvanverth9f372462016-04-06 06:08:59 -0700185 OnPaintFunc fPaintFunc;
186 void* fPaintUserData;
187
liyuqiand3cdbca2016-05-17 12:44:20 -0700188 WindowContext* fWindowContext = nullptr;
liyuqian566c8e42016-05-23 10:52:34 -0700189
190 virtual void onInval() = 0;
191
192 // Uncheck fIsContentInvalided to allow future inval/onInval.
193 void markInvalProcessed();
194
195 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
jvanverth9f372462016-04-06 06:08:59 -0700196};
197
jvanverth34524262016-05-04 13:49:13 -0700198} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700199#endif