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