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