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