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