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" |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 15 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 16 | class GrContext; |
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 | |
Brian Osman | 2dd9693 | 2016-10-18 15:33:53 -0400 | [diff] [blame] | 28 | virtual ~Window() { this->detach(); } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 29 | |
| 30 | virtual void setTitle(const char*) = 0; |
| 31 | virtual void show() = 0; |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 32 | |
| 33 | // JSON-formatted UI state for Android. Do nothing by default |
| 34 | virtual void setUIState(const char*) {} |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 35 | |
| 36 | // Shedules an invalidation event for window if one is not currently pending. |
| 37 | // Make sure that either onPaint or markInvalReceived is called when the client window consumes |
| 38 | // the the inval event. They unset fIsContentInvalided which allow future onInval. |
| 39 | void inval(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 40 | |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 41 | virtual bool scaleContentToFit() const { return false; } |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 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, |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 45 | #if SK_ANGLE && defined(SK_BUILD_FOR_WIN) |
| 46 | kANGLE_BackendType, |
| 47 | #endif |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 48 | #ifdef SK_VULKAN |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 49 | kVulkan_BackendType, |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 50 | #endif |
liyuqian | d94ad58 | 2016-06-07 14:22:37 -0700 | [diff] [blame] | 51 | kRaster_BackendType, |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 52 | |
liyuqian | d94ad58 | 2016-06-07 14:22:37 -0700 | [diff] [blame] | 53 | kLast_BackendType = kRaster_BackendType |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 54 | }; |
| 55 | enum { |
| 56 | kBackendTypeCount = kLast_BackendType + 1 |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 59 | virtual bool attach(BackendType) = 0; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 60 | void detach(); |
| 61 | |
| 62 | // input handling |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 63 | enum class Key { |
| 64 | kNONE, //corresponds to android's UNKNOWN |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 65 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 66 | kLeftSoftKey, |
| 67 | kRightSoftKey, |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 68 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 69 | kHome, //!< the home key - added to match android |
| 70 | kBack, //!< (CLR) |
| 71 | kSend, //!< the green (talk) key |
| 72 | kEnd, //!< the red key |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 73 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 74 | k0, |
| 75 | k1, |
| 76 | k2, |
| 77 | k3, |
| 78 | k4, |
| 79 | k5, |
| 80 | k6, |
| 81 | k7, |
| 82 | k8, |
| 83 | k9, |
| 84 | kStar, //!< the * key |
| 85 | kHash, //!< the # key |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 86 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 87 | kUp, |
| 88 | kDown, |
| 89 | kLeft, |
| 90 | kRight, |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 91 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 92 | // Keys needed by ImGui |
| 93 | kTab, |
| 94 | kPageUp, |
| 95 | kPageDown, |
| 96 | kDelete, |
| 97 | kEscape, |
| 98 | kShift, |
| 99 | kCtrl, |
| 100 | kOption, // AKA Alt |
| 101 | kA, |
| 102 | kC, |
| 103 | kV, |
| 104 | kX, |
| 105 | kY, |
| 106 | kZ, |
| 107 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 108 | kOK, //!< the center key |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 109 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 110 | kVolUp, //!< volume up - match android |
| 111 | kVolDown, //!< volume down - same |
| 112 | kPower, //!< power button - same |
| 113 | kCamera, //!< camera - same |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 114 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 115 | kLast = kCamera |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 116 | }; |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 117 | static const int kKeyCount = static_cast<int>(Key::kLast) + 1; |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 118 | |
| 119 | enum ModifierKeys { |
| 120 | kShift_ModifierKey = 1 << 0, |
| 121 | kControl_ModifierKey = 1 << 1, |
| 122 | kOption_ModifierKey = 1 << 2, // same as ALT |
| 123 | kCommand_ModifierKey = 1 << 3, |
| 124 | kFirstPress_ModifierKey = 1 << 4, |
| 125 | }; |
| 126 | |
| 127 | enum InputState { |
| 128 | kDown_InputState, |
| 129 | kUp_InputState, |
| 130 | kMove_InputState // only valid for mouse |
| 131 | }; |
| 132 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 133 | class Layer { |
| 134 | public: |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 135 | Layer() : fActive(true) {} |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 136 | virtual ~Layer() = default; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 137 | |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 138 | bool getActive() { return fActive; } |
| 139 | void setActive(bool active) { fActive = active; } |
| 140 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 141 | // return value of 'true' means 'I have handled this event' |
| 142 | virtual void onBackendCreated() {} |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 143 | virtual void onAttach(Window* window) {} |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 144 | virtual bool onChar(SkUnichar c, uint32_t modifiers) { return false; } |
| 145 | virtual bool onKey(Key key, InputState state, uint32_t modifiers) { return false; } |
| 146 | virtual bool onMouse(int x, int y, InputState state, uint32_t modifiers) { return false; } |
| 147 | virtual bool onMouseWheel(float delta, uint32_t modifiers) { return false; } |
| 148 | virtual bool onTouch(intptr_t owner, InputState state, float x, float y) { return false; } |
| 149 | virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {} |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 150 | virtual void onPrePaint() {} |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 151 | virtual void onPaint(SkCanvas*) {} |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 152 | |
| 153 | private: |
| 154 | friend class Window; |
| 155 | bool fActive; |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 156 | }; |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 157 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 158 | void pushLayer(Layer* layer) { |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 159 | layer->onAttach(this); |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 160 | fLayers.push(layer); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 163 | void onBackendCreated(); |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 164 | bool onChar(SkUnichar c, uint32_t modifiers); |
| 165 | bool onKey(Key key, InputState state, uint32_t modifiers); |
| 166 | bool onMouse(int x, int y, InputState state, uint32_t modifiers); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 167 | bool onMouseWheel(float delta, uint32_t modifiers); |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 168 | 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] | 169 | void onUIStateChanged(const SkString& stateName, const SkString& stateValue); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 170 | void onPaint(); |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 171 | void onResize(int width, int height); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 172 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 173 | int width(); |
| 174 | int height(); |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 175 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 176 | virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; } |
Brian Osman | 2ac96dc | 2017-06-23 13:32:29 -0400 | [diff] [blame] | 177 | virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 178 | |
| 179 | // Actual parameters in effect, obtained from the native window. |
| 180 | int sampleCount() const; |
| 181 | int stencilBits() const; |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 182 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 183 | // Returns null if there is not a GPU backend or if the backend is not yet created. |
| 184 | const GrContext* getGrContext() const; |
| 185 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 186 | protected: |
| 187 | Window(); |
| 188 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 189 | SkTDArray<Layer*> fLayers; |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 190 | DisplayParams fRequestedDisplayParams; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 191 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 192 | WindowContext* fWindowContext = nullptr; |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 193 | |
| 194 | virtual void onInval() = 0; |
| 195 | |
| 196 | // Uncheck fIsContentInvalided to allow future inval/onInval. |
| 197 | void markInvalProcessed(); |
| 198 | |
| 199 | bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 200 | |
| 201 | void visitLayers(std::function<void(Layer*)> visitor); |
| 202 | bool signalLayers(std::function<bool(Layer*)> visitor); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 205 | } // namespace sk_app |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 206 | #endif |