blob: ab22838ae50076ac688dabbe15f4be537dc3de66 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#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 Canaryb1f411a2019-08-29 10:39:22 -040015#include "tools/skui/InputState.h"
16#include "tools/skui/Key.h"
17#include "tools/skui/ModifierKey.h"
jvanverth9fab59d2016-04-06 12:08:51 -070018
Brian Osman45236912020-11-25 12:50:19 -050019#include <functional>
20
Robert Phillipsed653392020-07-10 13:55:21 -040021class GrDirectContext;
jvanverth9f372462016-04-06 06:08:59 -070022class SkCanvas;
liyuqian74959a12016-06-16 14:10:34 -070023class SkSurface;
Ben Wagner37c54032018-04-13 14:30:23 -040024class SkSurfaceProps;
Brian Osman01e6d172020-03-30 15:57:14 -040025class SkString;
jvanverth9f372462016-04-06 06:08:59 -070026
Jim Van Verth7bb0ff02021-09-30 16:27:30 -040027namespace skgpu {
28class Context;
29}
30
jvanverth34524262016-05-04 13:49:13 -070031namespace sk_app {
32
jvanvertha8d0d6c2016-05-05 12:32:03 -070033class WindowContext;
34
jvanverth9f372462016-04-06 06:08:59 -070035class Window {
36public:
37 static Window* CreateNativeWindow(void* platformData);
38
Hal Canary149f3f12019-08-01 16:21:49 -040039 virtual ~Window();
jvanverth9f372462016-04-06 06:08:59 -070040
41 virtual void setTitle(const char*) = 0;
42 virtual void show() = 0;
Brian Osmaneff04b52017-11-21 13:18:02 -050043
44 // JSON-formatted UI state for Android. Do nothing by default
45 virtual void setUIState(const char*) {}
liyuqian566c8e42016-05-23 10:52:34 -070046
Brian Osmanba3e8f92020-11-10 13:10:29 -050047 // 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.
liyuqian566c8e42016-05-23 10:52:34 -070052 // 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();
jvanverth9f372462016-04-06 06:08:59 -070055
djsollen12d62a72016-04-21 07:59:44 -070056 virtual bool scaleContentToFit() const { return false; }
djsollen12d62a72016-04-21 07:59:44 -070057
jvanverthaf236b52016-05-20 06:01:06 -070058 enum BackendType {
Brian Salomonf4ba4ec2020-03-19 15:54:28 -040059#ifdef SK_GL
jvanverth9f372462016-04-06 06:08:59 -070060 kNativeGL_BackendType,
Brian Salomonf4ba4ec2020-03-19 15:54:28 -040061#endif
Brian Salomon194db172017-08-17 14:37:06 -040062#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
63 kANGLE_BackendType,
64#endif
Stephen Whitea800ec92019-08-02 15:04:52 -040065#ifdef SK_DAWN
66 kDawn_BackendType,
67#endif
jvanverth063ece72016-06-17 09:29:14 -070068#ifdef SK_VULKAN
jvanverthaf236b52016-05-20 06:01:06 -070069 kVulkan_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070070#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -040071#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -050072 kMetal_BackendType,
Jim Van Verth7bb0ff02021-09-30 16:27:30 -040073#ifdef SK_GRAPHITE_ENABLED
74 kGraphiteMetal_BackendType,
75#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -050076#endif
Jim Van Verth682a2f42020-05-13 16:54:09 -040077#ifdef SK_DIRECT3D
78 kDirect3D_BackendType,
79#endif
liyuqiand94ad582016-06-07 14:22:37 -070080 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070081
liyuqiand94ad582016-06-07 14:22:37 -070082 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070083 };
84 enum {
85 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070086 };
87
csmartdalton578f0642017-02-24 16:04:47 -070088 virtual bool attach(BackendType) = 0;
jvanverth9f372462016-04-06 06:08:59 -070089 void detach();
90
91 // input handling
jvanverth9fab59d2016-04-06 12:08:51 -070092
Brian Osman80fc07e2017-12-08 16:45:43 -050093 class Layer {
94 public:
Brian Osman56a24812017-12-19 11:15:16 -050095 Layer() : fActive(true) {}
Brian Osman80fc07e2017-12-08 16:45:43 -050096 virtual ~Layer() = default;
jvanverth9f372462016-04-06 06:08:59 -070097
Brian Osman56a24812017-12-19 11:15:16 -050098 bool getActive() { return fActive; }
99 void setActive(bool active) { fActive = active; }
100
Brian Osman80fc07e2017-12-08 16:45:43 -0500101 // return value of 'true' means 'I have handled this event'
102 virtual void onBackendCreated() {}
Brian Osmand67e5182017-12-08 16:46:09 -0500103 virtual void onAttach(Window* window) {}
Hal Canaryb1f411a2019-08-29 10:39:22 -0400104 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 Verthd0cf5da2019-09-09 16:53:39 -0400109 // 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 Osman80fc07e2017-12-08 16:45:43 -0500112 virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
Brian Osmand67e5182017-12-08 16:46:09 -0500113 virtual void onPrePaint() {}
Robert Phillips9882dae2019-03-04 11:00:10 -0500114 virtual void onPaint(SkSurface*) {}
Ben Wagnera1915972018-08-09 15:06:19 -0400115 virtual void onResize(int width, int height) {}
Brian Osman56a24812017-12-19 11:15:16 -0500116
117 private:
118 friend class Window;
119 bool fActive;
Brian Osman80fc07e2017-12-08 16:45:43 -0500120 };
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700121
Brian Osman80fc07e2017-12-08 16:45:43 -0500122 void pushLayer(Layer* layer) {
Brian Osmand67e5182017-12-08 16:46:09 -0500123 layer->onAttach(this);
Mike Reed5edcd312018-08-08 11:23:41 -0400124 fLayers.push_back(layer);
liyuqiane5a6cd92016-05-27 08:52:52 -0700125 }
126
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700127 void onBackendCreated();
Hal Canaryb1f411a2019-08-29 10:39:22 -0400128 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 Verthd0cf5da2019-09-09 16:53:39 -0400133 // Platform-detected gesture events
134 bool onFling(skui::InputState state);
135 bool onPinch(skui::InputState state, float scale, float x, float y);
liyuqiane5a6cd92016-05-27 08:52:52 -0700136 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700137 void onPaint();
bsalomonccde4ab2016-07-27 08:50:12 -0700138 void onResize(int width, int height);
Jim Van Verth5a89ed52020-11-16 11:06:58 -0500139 void onActivate(bool isActive);
jvanverth9f372462016-04-06 06:08:59 -0700140
Jim Van Verth98385ba2019-02-06 11:23:34 -0500141 int width() const;
142 int height() const;
Ben Wagnerfa8b5e42021-01-28 14:30:59 -0500143 virtual float scaleFactor() const { return 1.0f; }
jvanverthc265a922016-04-08 12:51:45 -0700144
csmartdalton578f0642017-02-24 16:04:47 -0700145 virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
Brian Osman2ac96dc2017-06-23 13:32:29 -0400146 virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
csmartdalton578f0642017-02-24 16:04:47 -0700147
148 // Actual parameters in effect, obtained from the native window.
149 int sampleCount() const;
150 int stencilBits() const;
brianosman05de2162016-05-06 13:28:57 -0700151
csmartdalton61cd31a2017-02-27 17:00:53 -0700152 // Returns null if there is not a GPU backend or if the backend is not yet created.
Robert Phillipsed653392020-07-10 13:55:21 -0400153 GrDirectContext* directContext() const;
Jim Van Verth7bb0ff02021-09-30 16:27:30 -0400154 skgpu::Context* graphiteContext() const;
csmartdalton61cd31a2017-02-27 17:00:53 -0700155
jvanverth9f372462016-04-06 06:08:59 -0700156protected:
157 Window();
158
Brian Osman80fc07e2017-12-08 16:45:43 -0500159 SkTDArray<Layer*> fLayers;
csmartdalton578f0642017-02-24 16:04:47 -0700160 DisplayParams fRequestedDisplayParams;
Jim Van Verth5a89ed52020-11-16 11:06:58 -0500161 bool fIsActive = true;
jvanverth9f372462016-04-06 06:08:59 -0700162
Hal Canary149f3f12019-08-01 16:21:49 -0400163 std::unique_ptr<WindowContext> fWindowContext;
liyuqian566c8e42016-05-23 10:52:34 -0700164
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 Osman56a24812017-12-19 11:15:16 -0500171
172 void visitLayers(std::function<void(Layer*)> visitor);
173 bool signalLayers(std::function<bool(Layer*)> visitor);
jvanverth9f372462016-04-06 06:08:59 -0700174};
175
jvanverth34524262016-05-04 13:49:13 -0700176} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700177#endif