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 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 17 | class SkCanvas; |
liyuqian | 74959a1 | 2016-06-16 14:10:34 -0700 | [diff] [blame] | 18 | class SkSurface; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 19 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 20 | namespace sk_app { |
| 21 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 22 | class WindowContext; |
| 23 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 24 | class Window { |
| 25 | public: |
| 26 | static Window* CreateNativeWindow(void* platformData); |
| 27 | |
| 28 | virtual ~Window() {}; |
| 29 | |
| 30 | virtual void setTitle(const char*) = 0; |
| 31 | virtual void show() = 0; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 32 | virtual void setUIState(const Json::Value& state) {} // do nothing in default |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 33 | |
| 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(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 38 | |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 39 | virtual bool scaleContentToFit() const { return false; } |
| 40 | virtual bool supportsContentRect() const { return false; } |
| 41 | virtual SkRect getContentRect() { return SkRect::MakeEmpty(); } |
| 42 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 43 | enum BackendType { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 44 | kNativeGL_BackendType, |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 45 | #ifdef SK_VULKAN |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 46 | kVulkan_BackendType, |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 47 | #endif |
liyuqian | d94ad58 | 2016-06-07 14:22:37 -0700 | [diff] [blame] | 48 | kRaster_BackendType, |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 49 | |
liyuqian | d94ad58 | 2016-06-07 14:22:37 -0700 | [diff] [blame] | 50 | kLast_BackendType = kRaster_BackendType |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 51 | }; |
| 52 | enum { |
| 53 | kBackendTypeCount = kLast_BackendType + 1 |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 56 | virtual bool attach(BackendType attachType, const DisplayParams& params) = 0; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 57 | void detach(); |
| 58 | |
| 59 | // input handling |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 60 | enum class Key { |
| 61 | kNONE, //corresponds to android's UNKNOWN |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 62 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 63 | kLeftSoftKey, |
| 64 | kRightSoftKey, |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 65 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 66 | kHome, //!< the home key - added to match android |
| 67 | kBack, //!< (CLR) |
| 68 | kSend, //!< the green (talk) key |
| 69 | kEnd, //!< the red key |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 70 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 71 | k0, |
| 72 | k1, |
| 73 | k2, |
| 74 | k3, |
| 75 | k4, |
| 76 | k5, |
| 77 | k6, |
| 78 | k7, |
| 79 | k8, |
| 80 | k9, |
| 81 | kStar, //!< the * key |
| 82 | kHash, //!< the # key |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 83 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 84 | kUp, |
| 85 | kDown, |
| 86 | kLeft, |
| 87 | kRight, |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 88 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 89 | kOK, //!< the center key |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 90 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 91 | kVolUp, //!< volume up - match android |
| 92 | kVolDown, //!< volume down - same |
| 93 | kPower, //!< power button - same |
| 94 | kCamera, //!< camera - same |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 95 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 96 | kLast = kCamera |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 97 | }; |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 98 | static const int kKeyCount = static_cast<int>(Key::kLast) + 1; |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 99 | |
| 100 | enum ModifierKeys { |
| 101 | kShift_ModifierKey = 1 << 0, |
| 102 | kControl_ModifierKey = 1 << 1, |
| 103 | kOption_ModifierKey = 1 << 2, // same as ALT |
| 104 | kCommand_ModifierKey = 1 << 3, |
| 105 | kFirstPress_ModifierKey = 1 << 4, |
| 106 | }; |
| 107 | |
| 108 | enum InputState { |
| 109 | kDown_InputState, |
| 110 | kUp_InputState, |
| 111 | kMove_InputState // only valid for mouse |
| 112 | }; |
| 113 | |
| 114 | // return value of 'true' means 'I have handled this event' |
| 115 | typedef bool(*OnCharFunc)(SkUnichar c, uint32_t modifiers, void* userData); |
| 116 | typedef bool(*OnKeyFunc)(Key key, InputState state, uint32_t modifiers, void* userData); |
| 117 | typedef bool(*OnMouseFunc)(int x, int y, InputState state, uint32_t modifiers, void* userData); |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 118 | 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] | 119 | typedef void(*OnUIStateChangedFunc)( |
| 120 | const SkString& stateName, const SkString& stateValue, void* userData); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 121 | typedef void(*OnPaintFunc)(SkCanvas*, void* userData); |
| 122 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 123 | void registerCharFunc(OnCharFunc func, void* userData) { |
| 124 | fCharFunc = func; |
| 125 | fCharUserData = userData; |
| 126 | } |
| 127 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 128 | void registerKeyFunc(OnKeyFunc func, void* userData) { |
| 129 | fKeyFunc = func; |
| 130 | fKeyUserData = userData; |
| 131 | } |
| 132 | |
| 133 | void registerMouseFunc(OnMouseFunc func, void* userData) { |
| 134 | fMouseFunc = func; |
| 135 | fMouseUserData = userData; |
| 136 | } |
| 137 | |
| 138 | void registerPaintFunc(OnPaintFunc func, void* userData) { |
| 139 | fPaintFunc = func; |
| 140 | fPaintUserData = userData; |
| 141 | } |
| 142 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 143 | void registerTouchFunc(OnTouchFunc func, void* userData) { |
| 144 | fTouchFunc = func; |
| 145 | fTouchUserData = userData; |
| 146 | } |
| 147 | |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 148 | void registerUIStateChangedFunc(OnUIStateChangedFunc func, void* userData) { |
| 149 | fUIStateChangedFunc = func; |
| 150 | fUIStateChangedUserData = userData; |
| 151 | } |
| 152 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 153 | bool onChar(SkUnichar c, uint32_t modifiers); |
| 154 | bool onKey(Key key, InputState state, uint32_t modifiers); |
| 155 | bool onMouse(int x, int y, InputState state, uint32_t modifiers); |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 156 | 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] | 157 | void onUIStateChanged(const SkString& stateName, const SkString& stateValue); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 158 | void onPaint(); |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame^] | 159 | void onResize(int width, int height); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 160 | |
bsalomon | eeb4c38 | 2016-07-26 08:50:26 -0700 | [diff] [blame] | 161 | int width() { return fWidth; } |
| 162 | int height() { return fHeight; } |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 163 | |
liyuqian | 40d21de | 2016-05-12 09:17:04 -0700 | [diff] [blame] | 164 | virtual const DisplayParams& getDisplayParams(); |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 165 | void setDisplayParams(const DisplayParams& params); |
| 166 | |
liyuqian | 74959a1 | 2016-06-16 14:10:34 -0700 | [diff] [blame] | 167 | // This is just for the sRGB split screen |
| 168 | sk_sp<SkSurface> getOffscreenSurface(bool forceSRGB); |
| 169 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 170 | protected: |
| 171 | Window(); |
| 172 | |
bsalomon | eeb4c38 | 2016-07-26 08:50:26 -0700 | [diff] [blame] | 173 | int fWidth; |
| 174 | int fHeight; |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 175 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 176 | OnCharFunc fCharFunc; |
| 177 | void* fCharUserData; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 178 | OnKeyFunc fKeyFunc; |
| 179 | void* fKeyUserData; |
| 180 | OnMouseFunc fMouseFunc; |
| 181 | void* fMouseUserData; |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 182 | OnTouchFunc fTouchFunc; |
| 183 | void* fTouchUserData; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 184 | OnUIStateChangedFunc |
| 185 | fUIStateChangedFunc; |
| 186 | void* fUIStateChangedUserData; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 187 | OnPaintFunc fPaintFunc; |
| 188 | void* fPaintUserData; |
| 189 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 190 | WindowContext* fWindowContext = nullptr; |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 191 | |
| 192 | virtual void onInval() = 0; |
| 193 | |
| 194 | // Uncheck fIsContentInvalided to allow future inval/onInval. |
| 195 | void markInvalProcessed(); |
| 196 | |
| 197 | bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 200 | } // namespace sk_app |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 201 | #endif |