jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 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 | |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 11 | #include "DisplayParams.h" |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 12 | #include "SkRect.h" |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 13 | #include "SkTouchGesture.h" |
| 14 | #include "SkTypes.h" |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 15 | #include "SkJSONCPP.h" |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 16 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 17 | class GrContext; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 18 | class SkCanvas; |
liyuqian | 74959a1 | 2016-06-16 14:10:34 -0700 | [diff] [blame] | 19 | class SkSurface; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 20 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 21 | namespace sk_app { |
| 22 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 23 | class WindowContext; |
| 24 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 25 | class Window { |
| 26 | public: |
| 27 | static Window* CreateNativeWindow(void* platformData); |
| 28 | |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 29 | virtual ~Window() { this->detach(); } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 30 | |
| 31 | virtual void setTitle(const char*) = 0; |
| 32 | virtual void show() = 0; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 33 | virtual void setUIState(const Json::Value& state) {} // do nothing in default |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 34 | |
| 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(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 39 | |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 40 | virtual bool scaleContentToFit() const { return false; } |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 41 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 42 | enum BackendType { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 43 | kNativeGL_BackendType, |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 44 | #if SK_ANGLE && defined(SK_BUILD_FOR_WIN) |
| 45 | kANGLE_BackendType, |
| 46 | #endif |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 47 | #ifdef SK_VULKAN |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 48 | kVulkan_BackendType, |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 49 | #endif |
liyuqian | d94ad58 | 2016-06-07 14:22:37 -0700 | [diff] [blame] | 50 | kRaster_BackendType, |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 51 | |
liyuqian | d94ad58 | 2016-06-07 14:22:37 -0700 | [diff] [blame] | 52 | kLast_BackendType = kRaster_BackendType |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 53 | }; |
| 54 | enum { |
| 55 | kBackendTypeCount = kLast_BackendType + 1 |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 58 | virtual bool attach(BackendType) = 0; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 59 | void detach(); |
| 60 | |
| 61 | // input handling |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 62 | enum class Key { |
| 63 | kNONE, //corresponds to android's UNKNOWN |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 64 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 65 | kLeftSoftKey, |
| 66 | kRightSoftKey, |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 67 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 68 | kHome, //!< the home key - added to match android |
| 69 | kBack, //!< (CLR) |
| 70 | kSend, //!< the green (talk) key |
| 71 | kEnd, //!< the red key |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 72 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 73 | 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 |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 85 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 86 | kUp, |
| 87 | kDown, |
| 88 | kLeft, |
| 89 | kRight, |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 90 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 91 | // 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 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 107 | kOK, //!< the center key |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 108 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 109 | kVolUp, //!< volume up - match android |
| 110 | kVolDown, //!< volume down - same |
| 111 | kPower, //!< power button - same |
| 112 | kCamera, //!< camera - same |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 113 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 114 | kLast = kCamera |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 115 | }; |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 116 | static const int kKeyCount = static_cast<int>(Key::kLast) + 1; |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 117 | |
| 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 Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 133 | typedef void(*OnBackendCreatedFunc)(void* userData); |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 134 | 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 Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 137 | typedef bool(*OnMouseWheelFunc)(float delta, uint32_t modifiers, void* userData); |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 138 | typedef bool(*OnTouchFunc)(intptr_t owner, InputState state, float x, float y, void* userData); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 139 | typedef void(*OnUIStateChangedFunc)( |
| 140 | const SkString& stateName, const SkString& stateValue, void* userData); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 141 | typedef void(*OnPaintFunc)(SkCanvas*, void* userData); |
| 142 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 143 | void registerBackendCreatedFunc(OnBackendCreatedFunc func, void* userData) { |
| 144 | fBackendCreatedFunc = func; |
| 145 | fBackendCreatedUserData = userData; |
| 146 | } |
| 147 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 148 | void registerCharFunc(OnCharFunc func, void* userData) { |
| 149 | fCharFunc = func; |
| 150 | fCharUserData = userData; |
| 151 | } |
| 152 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 153 | 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 Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 163 | void registerMouseWheelFunc(OnMouseWheelFunc func, void* userData) { |
| 164 | fMouseWheelFunc = func; |
| 165 | fMouseWheelUserData = userData; |
| 166 | } |
| 167 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 168 | void registerPaintFunc(OnPaintFunc func, void* userData) { |
| 169 | fPaintFunc = func; |
| 170 | fPaintUserData = userData; |
| 171 | } |
| 172 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 173 | void registerTouchFunc(OnTouchFunc func, void* userData) { |
| 174 | fTouchFunc = func; |
| 175 | fTouchUserData = userData; |
| 176 | } |
| 177 | |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 178 | void registerUIStateChangedFunc(OnUIStateChangedFunc func, void* userData) { |
| 179 | fUIStateChangedFunc = func; |
| 180 | fUIStateChangedUserData = userData; |
| 181 | } |
| 182 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 183 | void onBackendCreated(); |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 184 | 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 Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 187 | bool onMouseWheel(float delta, uint32_t modifiers); |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 188 | bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 189 | void onUIStateChanged(const SkString& stateName, const SkString& stateValue); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 190 | void onPaint(); |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 191 | void onResize(int width, int height); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 192 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 193 | int width(); |
| 194 | int height(); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 195 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 196 | virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; } |
Brian Osman | 2ac96dc | 2017-06-23 13:32:29 -0400 | [diff] [blame] | 197 | virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 198 | |
| 199 | // Actual parameters in effect, obtained from the native window. |
| 200 | int sampleCount() const; |
| 201 | int stencilBits() const; |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 202 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 203 | // Returns null if there is not a GPU backend or if the backend is not yet created. |
| 204 | const GrContext* getGrContext() const; |
| 205 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 206 | protected: |
| 207 | Window(); |
| 208 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 209 | 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; |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 225 | DisplayParams fRequestedDisplayParams; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 226 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 227 | WindowContext* fWindowContext = nullptr; |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 228 | |
| 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 |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 237 | } // namespace sk_app |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 238 | #endif |