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