blob: 38a1cc0c233ce05c62a468933a7eeb36e1d719cb [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"
Hal Canaryff2e8fe2019-07-16 09:58:43 -040014#include "tools/InputState.h"
Hal Canary3a85ed12019-07-08 16:07:57 -040015#include "tools/ModifierKey.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "tools/sk_app/DisplayParams.h"
jvanverth9fab59d2016-04-06 12:08:51 -070017
csmartdalton61cd31a2017-02-27 17:00:53 -070018class GrContext;
jvanverth9f372462016-04-06 06:08:59 -070019class SkCanvas;
liyuqian74959a12016-06-16 14:10:34 -070020class SkSurface;
Ben Wagner37c54032018-04-13 14:30:23 -040021class SkSurfaceProps;
jvanverth9f372462016-04-06 06:08:59 -070022
jvanverth34524262016-05-04 13:49:13 -070023namespace sk_app {
24
jvanvertha8d0d6c2016-05-05 12:32:03 -070025class WindowContext;
26
jvanverth9f372462016-04-06 06:08:59 -070027class Window {
28public:
29 static Window* CreateNativeWindow(void* platformData);
30
Hal Canary149f3f12019-08-01 16:21:49 -040031 virtual ~Window();
jvanverth9f372462016-04-06 06:08:59 -070032
33 virtual void setTitle(const char*) = 0;
34 virtual void show() = 0;
Brian Osmaneff04b52017-11-21 13:18:02 -050035
36 // JSON-formatted UI state for Android. Do nothing by default
37 virtual void setUIState(const char*) {}
liyuqian566c8e42016-05-23 10:52:34 -070038
39 // Shedules an invalidation event for window if one is not currently pending.
40 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
41 // the the inval event. They unset fIsContentInvalided which allow future onInval.
42 void inval();
jvanverth9f372462016-04-06 06:08:59 -070043
djsollen12d62a72016-04-21 07:59:44 -070044 virtual bool scaleContentToFit() const { return false; }
djsollen12d62a72016-04-21 07:59:44 -070045
jvanverthaf236b52016-05-20 06:01:06 -070046 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070047 kNativeGL_BackendType,
Brian Salomon194db172017-08-17 14:37:06 -040048#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
49 kANGLE_BackendType,
50#endif
Stephen Whitea800ec92019-08-02 15:04:52 -040051#ifdef SK_DAWN
52 kDawn_BackendType,
53#endif
jvanverth063ece72016-06-17 09:29:14 -070054#ifdef SK_VULKAN
jvanverthaf236b52016-05-20 06:01:06 -070055 kVulkan_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070056#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -050057#if SK_METAL && defined(SK_BUILD_FOR_MAC)
58 kMetal_BackendType,
59#endif
liyuqiand94ad582016-06-07 14:22:37 -070060 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070061
liyuqiand94ad582016-06-07 14:22:37 -070062 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070063 };
64 enum {
65 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070066 };
67
csmartdalton578f0642017-02-24 16:04:47 -070068 virtual bool attach(BackendType) = 0;
jvanverth9f372462016-04-06 06:08:59 -070069 void detach();
70
71 // input handling
brianosman622c8d52016-05-10 06:50:49 -070072 enum class Key {
73 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070074
brianosman622c8d52016-05-10 06:50:49 -070075 kLeftSoftKey,
76 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070077
brianosman622c8d52016-05-10 06:50:49 -070078 kHome, //!< the home key - added to match android
79 kBack, //!< (CLR)
80 kSend, //!< the green (talk) key
81 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070082
brianosman622c8d52016-05-10 06:50:49 -070083 k0,
84 k1,
85 k2,
86 k3,
87 k4,
88 k5,
89 k6,
90 k7,
91 k8,
92 k9,
93 kStar, //!< the * key
94 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070095
brianosman622c8d52016-05-10 06:50:49 -070096 kUp,
97 kDown,
98 kLeft,
99 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -0700100
Brian Osman79086b92017-02-10 13:36:16 -0500101 // Keys needed by ImGui
102 kTab,
103 kPageUp,
104 kPageDown,
105 kDelete,
106 kEscape,
107 kShift,
108 kCtrl,
109 kOption, // AKA Alt
110 kA,
111 kC,
112 kV,
113 kX,
114 kY,
115 kZ,
116
brianosman622c8d52016-05-10 06:50:49 -0700117 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -0700118
brianosman622c8d52016-05-10 06:50:49 -0700119 kVolUp, //!< volume up - match android
120 kVolDown, //!< volume down - same
121 kPower, //!< power button - same
122 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -0700123
brianosman622c8d52016-05-10 06:50:49 -0700124 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -0700125 };
brianosman622c8d52016-05-10 06:50:49 -0700126 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -0700127
Brian Osman80fc07e2017-12-08 16:45:43 -0500128 class Layer {
129 public:
Brian Osman56a24812017-12-19 11:15:16 -0500130 Layer() : fActive(true) {}
Brian Osman80fc07e2017-12-08 16:45:43 -0500131 virtual ~Layer() = default;
jvanverth9f372462016-04-06 06:08:59 -0700132
Brian Osman56a24812017-12-19 11:15:16 -0500133 bool getActive() { return fActive; }
134 void setActive(bool active) { fActive = active; }
135
Brian Osman80fc07e2017-12-08 16:45:43 -0500136 // return value of 'true' means 'I have handled this event'
137 virtual void onBackendCreated() {}
Brian Osmand67e5182017-12-08 16:46:09 -0500138 virtual void onAttach(Window* window) {}
Hal Canary3a85ed12019-07-08 16:07:57 -0400139 virtual bool onChar(SkUnichar c, ModifierKey modifiers) { return false; }
140 virtual bool onKey(Key key, InputState state, ModifierKey modifiers) { return false; }
141 virtual bool onMouse(int x, int y, InputState state, ModifierKey modifiers) { return false; }
142 virtual bool onMouseWheel(float delta, ModifierKey modifiers) { return false; }
Brian Osman80fc07e2017-12-08 16:45:43 -0500143 virtual bool onTouch(intptr_t owner, InputState state, float x, float y) { return false; }
144 virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
Brian Osmand67e5182017-12-08 16:46:09 -0500145 virtual void onPrePaint() {}
Robert Phillips9882dae2019-03-04 11:00:10 -0500146 virtual void onPaint(SkSurface*) {}
Ben Wagnera1915972018-08-09 15:06:19 -0400147 virtual void onResize(int width, int height) {}
Brian Osman56a24812017-12-19 11:15:16 -0500148
149 private:
150 friend class Window;
151 bool fActive;
Brian Osman80fc07e2017-12-08 16:45:43 -0500152 };
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700153
Brian Osman80fc07e2017-12-08 16:45:43 -0500154 void pushLayer(Layer* layer) {
Brian Osmand67e5182017-12-08 16:46:09 -0500155 layer->onAttach(this);
Mike Reed5edcd312018-08-08 11:23:41 -0400156 fLayers.push_back(layer);
liyuqiane5a6cd92016-05-27 08:52:52 -0700157 }
158
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700159 void onBackendCreated();
Hal Canary3a85ed12019-07-08 16:07:57 -0400160 bool onChar(SkUnichar c, ModifierKey modifiers);
161 bool onKey(Key key, InputState state, ModifierKey modifiers);
162 bool onMouse(int x, int y, InputState state, ModifierKey modifiers);
163 bool onMouseWheel(float delta, ModifierKey modifiers);
jvanverth814e38d2016-06-06 08:48:47 -0700164 bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
liyuqiane5a6cd92016-05-27 08:52:52 -0700165 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700166 void onPaint();
bsalomonccde4ab2016-07-27 08:50:12 -0700167 void onResize(int width, int height);
jvanverth9f372462016-04-06 06:08:59 -0700168
Jim Van Verth98385ba2019-02-06 11:23:34 -0500169 int width() const;
170 int height() const;
jvanverthc265a922016-04-08 12:51:45 -0700171
csmartdalton578f0642017-02-24 16:04:47 -0700172 virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
Brian Osman2ac96dc2017-06-23 13:32:29 -0400173 virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
csmartdalton578f0642017-02-24 16:04:47 -0700174
175 // Actual parameters in effect, obtained from the native window.
176 int sampleCount() const;
177 int stencilBits() const;
brianosman05de2162016-05-06 13:28:57 -0700178
csmartdalton61cd31a2017-02-27 17:00:53 -0700179 // Returns null if there is not a GPU backend or if the backend is not yet created.
Chris Dalton89305752018-11-01 10:52:34 -0600180 GrContext* getGrContext() const;
csmartdalton61cd31a2017-02-27 17:00:53 -0700181
jvanverth9f372462016-04-06 06:08:59 -0700182protected:
183 Window();
184
Brian Osman80fc07e2017-12-08 16:45:43 -0500185 SkTDArray<Layer*> fLayers;
csmartdalton578f0642017-02-24 16:04:47 -0700186 DisplayParams fRequestedDisplayParams;
jvanverth9f372462016-04-06 06:08:59 -0700187
Hal Canary149f3f12019-08-01 16:21:49 -0400188 std::unique_ptr<WindowContext> fWindowContext;
liyuqian566c8e42016-05-23 10:52:34 -0700189
190 virtual void onInval() = 0;
191
192 // Uncheck fIsContentInvalided to allow future inval/onInval.
193 void markInvalProcessed();
194
195 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
Brian Osman56a24812017-12-19 11:15:16 -0500196
197 void visitLayers(std::function<void(Layer*)> visitor);
198 bool signalLayers(std::function<bool(Layer*)> visitor);
jvanverth9f372462016-04-06 06:08:59 -0700199};
200
jvanverth34524262016-05-04 13:49:13 -0700201} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700202#endif