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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkRect.h" |
| 12 | #include "include/core/SkTypes.h" |
| 13 | #include "include/private/SkTDArray.h" |
| 14 | #include "tools/sk_app/DisplayParams.h" |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 15 | #include "tools/skui/InputState.h" |
| 16 | #include "tools/skui/Key.h" |
| 17 | #include "tools/skui/ModifierKey.h" |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 18 | |
Brian Osman | 4523691 | 2020-11-25 12:50:19 -0500 | [diff] [blame] | 19 | #include <functional> |
| 20 | |
Robert Phillips | ed65339 | 2020-07-10 13:55:21 -0400 | [diff] [blame] | 21 | class GrDirectContext; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 22 | class SkCanvas; |
liyuqian | 74959a1 | 2016-06-16 14:10:34 -0700 | [diff] [blame] | 23 | class SkSurface; |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 24 | class SkSurfaceProps; |
Brian Osman | 01e6d17 | 2020-03-30 15:57:14 -0400 | [diff] [blame] | 25 | class SkString; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 26 | |
Jim Van Verth | 7bb0ff0 | 2021-09-30 16:27:30 -0400 | [diff] [blame] | 27 | namespace skgpu { |
| 28 | class Context; |
| 29 | } |
| 30 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 31 | namespace sk_app { |
| 32 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 33 | class WindowContext; |
| 34 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 35 | class Window { |
| 36 | public: |
| 37 | static Window* CreateNativeWindow(void* platformData); |
| 38 | |
Hal Canary | 149f3f1 | 2019-08-01 16:21:49 -0400 | [diff] [blame] | 39 | virtual ~Window(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 40 | |
| 41 | virtual void setTitle(const char*) = 0; |
| 42 | virtual void show() = 0; |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 43 | |
| 44 | // JSON-formatted UI state for Android. Do nothing by default |
| 45 | virtual void setUIState(const char*) {} |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 46 | |
Brian Osman | ba3e8f9 | 2020-11-10 13:10:29 -0500 | [diff] [blame] | 47 | // Interface to the system clipboard. Only implemented on UNIX. |
| 48 | virtual const char* getClipboardText() { return nullptr; } |
| 49 | virtual void setClipboardText(const char*) {} |
| 50 | |
| 51 | // Schedules an invalidation event for window if one is not currently pending. |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 52 | // Make sure that either onPaint or markInvalReceived is called when the client window consumes |
| 53 | // the the inval event. They unset fIsContentInvalided which allow future onInval. |
| 54 | void inval(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 55 | |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 56 | virtual bool scaleContentToFit() const { return false; } |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 57 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 58 | enum BackendType { |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 59 | #ifdef SK_GL |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 60 | kNativeGL_BackendType, |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 61 | #endif |
Brian Salomon | 194db17 | 2017-08-17 14:37:06 -0400 | [diff] [blame] | 62 | #if SK_ANGLE && defined(SK_BUILD_FOR_WIN) |
| 63 | kANGLE_BackendType, |
| 64 | #endif |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 65 | #ifdef SK_DAWN |
| 66 | kDawn_BackendType, |
| 67 | #endif |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 68 | #ifdef SK_VULKAN |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 69 | kVulkan_BackendType, |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 70 | #endif |
Jim Van Verth | e58d532 | 2019-09-03 09:42:57 -0400 | [diff] [blame] | 71 | #ifdef SK_METAL |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 72 | kMetal_BackendType, |
Jim Van Verth | 7bb0ff0 | 2021-09-30 16:27:30 -0400 | [diff] [blame] | 73 | #ifdef SK_GRAPHITE_ENABLED |
| 74 | kGraphiteMetal_BackendType, |
| 75 | #endif |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 76 | #endif |
Jim Van Verth | 682a2f4 | 2020-05-13 16:54:09 -0400 | [diff] [blame] | 77 | #ifdef SK_DIRECT3D |
| 78 | kDirect3D_BackendType, |
| 79 | #endif |
liyuqian | d94ad58 | 2016-06-07 14:22:37 -0700 | [diff] [blame] | 80 | kRaster_BackendType, |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 81 | |
liyuqian | d94ad58 | 2016-06-07 14:22:37 -0700 | [diff] [blame] | 82 | kLast_BackendType = kRaster_BackendType |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 83 | }; |
| 84 | enum { |
| 85 | kBackendTypeCount = kLast_BackendType + 1 |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 88 | virtual bool attach(BackendType) = 0; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 89 | void detach(); |
| 90 | |
| 91 | // input handling |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 92 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 93 | class Layer { |
| 94 | public: |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 95 | Layer() : fActive(true) {} |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 96 | virtual ~Layer() = default; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 97 | |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 98 | bool getActive() { return fActive; } |
| 99 | void setActive(bool active) { fActive = active; } |
| 100 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 101 | // return value of 'true' means 'I have handled this event' |
| 102 | virtual void onBackendCreated() {} |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 103 | virtual void onAttach(Window* window) {} |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 104 | virtual bool onChar(SkUnichar c, skui::ModifierKey) { return false; } |
| 105 | virtual bool onKey(skui::Key, skui::InputState, skui::ModifierKey) { return false; } |
| 106 | virtual bool onMouse(int x, int y, skui::InputState, skui::ModifierKey) { return false; } |
| 107 | virtual bool onMouseWheel(float delta, skui::ModifierKey) { return false; } |
| 108 | virtual bool onTouch(intptr_t owner, skui::InputState, float x, float y) { return false; } |
Jim Van Verth | d0cf5da | 2019-09-09 16:53:39 -0400 | [diff] [blame] | 109 | // Platform-detected gesture events |
| 110 | virtual bool onFling(skui::InputState state) { return false; } |
| 111 | virtual bool onPinch(skui::InputState state, float scale, float x, float y) { return false; } |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 112 | virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {} |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 113 | virtual void onPrePaint() {} |
Robert Phillips | 9882dae | 2019-03-04 11:00:10 -0500 | [diff] [blame] | 114 | virtual void onPaint(SkSurface*) {} |
Ben Wagner | a191597 | 2018-08-09 15:06:19 -0400 | [diff] [blame] | 115 | virtual void onResize(int width, int height) {} |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 116 | |
| 117 | private: |
| 118 | friend class Window; |
| 119 | bool fActive; |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 120 | }; |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 121 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 122 | void pushLayer(Layer* layer) { |
Brian Osman | d67e518 | 2017-12-08 16:46:09 -0500 | [diff] [blame] | 123 | layer->onAttach(this); |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 124 | fLayers.push_back(layer); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 127 | void onBackendCreated(); |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 128 | bool onChar(SkUnichar c, skui::ModifierKey modifiers); |
| 129 | bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers); |
| 130 | bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers); |
| 131 | bool onMouseWheel(float delta, skui::ModifierKey modifiers); |
| 132 | bool onTouch(intptr_t owner, skui::InputState state, float x, float y); // multi-owner = multi-touch |
Jim Van Verth | d0cf5da | 2019-09-09 16:53:39 -0400 | [diff] [blame] | 133 | // Platform-detected gesture events |
| 134 | bool onFling(skui::InputState state); |
| 135 | bool onPinch(skui::InputState state, float scale, float x, float y); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 136 | void onUIStateChanged(const SkString& stateName, const SkString& stateValue); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 137 | void onPaint(); |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 138 | void onResize(int width, int height); |
Jim Van Verth | 5a89ed5 | 2020-11-16 11:06:58 -0500 | [diff] [blame] | 139 | void onActivate(bool isActive); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 140 | |
Jim Van Verth | 98385ba | 2019-02-06 11:23:34 -0500 | [diff] [blame] | 141 | int width() const; |
| 142 | int height() const; |
Ben Wagner | fa8b5e4 | 2021-01-28 14:30:59 -0500 | [diff] [blame] | 143 | virtual float scaleFactor() const { return 1.0f; } |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 144 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 145 | virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; } |
Brian Osman | 2ac96dc | 2017-06-23 13:32:29 -0400 | [diff] [blame] | 146 | virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true); |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 147 | |
| 148 | // Actual parameters in effect, obtained from the native window. |
| 149 | int sampleCount() const; |
| 150 | int stencilBits() const; |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 151 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 152 | // Returns null if there is not a GPU backend or if the backend is not yet created. |
Robert Phillips | ed65339 | 2020-07-10 13:55:21 -0400 | [diff] [blame] | 153 | GrDirectContext* directContext() const; |
Jim Van Verth | 7bb0ff0 | 2021-09-30 16:27:30 -0400 | [diff] [blame] | 154 | skgpu::Context* graphiteContext() const; |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 155 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 156 | protected: |
| 157 | Window(); |
| 158 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 159 | SkTDArray<Layer*> fLayers; |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 160 | DisplayParams fRequestedDisplayParams; |
Jim Van Verth | 5a89ed5 | 2020-11-16 11:06:58 -0500 | [diff] [blame] | 161 | bool fIsActive = true; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 162 | |
Hal Canary | 149f3f1 | 2019-08-01 16:21:49 -0400 | [diff] [blame] | 163 | std::unique_ptr<WindowContext> fWindowContext; |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 164 | |
| 165 | virtual void onInval() = 0; |
| 166 | |
| 167 | // Uncheck fIsContentInvalided to allow future inval/onInval. |
| 168 | void markInvalProcessed(); |
| 169 | |
| 170 | bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 171 | |
| 172 | void visitLayers(std::function<void(Layer*)> visitor); |
| 173 | bool signalLayers(std::function<bool(Layer*)> visitor); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 176 | } // namespace sk_app |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 177 | #endif |