blob: 927d8a96779979378e3912b0b23da00fa3022583 [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
brianosman05de2162016-05-06 13:28:57 -070011#include "DisplayParams.h"
djsollen12d62a72016-04-21 07:59:44 -070012#include "SkRect.h"
liyuqiand3cdbca2016-05-17 12:44:20 -070013#include "SkTouchGesture.h"
14#include "SkTypes.h"
jvanverth9fab59d2016-04-06 12:08:51 -070015
csmartdalton61cd31a2017-02-27 17:00:53 -070016class GrContext;
jvanverth9f372462016-04-06 06:08:59 -070017class SkCanvas;
liyuqian74959a12016-06-16 14:10:34 -070018class SkSurface;
jvanverth9f372462016-04-06 06:08:59 -070019
jvanverth34524262016-05-04 13:49:13 -070020namespace sk_app {
21
jvanvertha8d0d6c2016-05-05 12:32:03 -070022class WindowContext;
23
jvanverth9f372462016-04-06 06:08:59 -070024class Window {
25public:
26 static Window* CreateNativeWindow(void* platformData);
27
Brian Osman2dd96932016-10-18 15:33:53 -040028 virtual ~Window() { this->detach(); }
jvanverth9f372462016-04-06 06:08:59 -070029
30 virtual void setTitle(const char*) = 0;
31 virtual void show() = 0;
Brian Osmaneff04b52017-11-21 13:18:02 -050032
33 // JSON-formatted UI state for Android. Do nothing by default
34 virtual void setUIState(const char*) {}
liyuqian566c8e42016-05-23 10:52:34 -070035
36 // Shedules an invalidation event for window if one is not currently pending.
37 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
38 // the the inval event. They unset fIsContentInvalided which allow future onInval.
39 void inval();
jvanverth9f372462016-04-06 06:08:59 -070040
djsollen12d62a72016-04-21 07:59:44 -070041 virtual bool scaleContentToFit() const { return false; }
djsollen12d62a72016-04-21 07:59:44 -070042
jvanverthaf236b52016-05-20 06:01:06 -070043 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070044 kNativeGL_BackendType,
Brian Salomon194db172017-08-17 14:37:06 -040045#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
46 kANGLE_BackendType,
47#endif
jvanverth063ece72016-06-17 09:29:14 -070048#ifdef SK_VULKAN
jvanverthaf236b52016-05-20 06:01:06 -070049 kVulkan_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070050#endif
liyuqiand94ad582016-06-07 14:22:37 -070051 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070052
liyuqiand94ad582016-06-07 14:22:37 -070053 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070054 };
55 enum {
56 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070057 };
58
csmartdalton578f0642017-02-24 16:04:47 -070059 virtual bool attach(BackendType) = 0;
jvanverth9f372462016-04-06 06:08:59 -070060 void detach();
61
62 // input handling
brianosman622c8d52016-05-10 06:50:49 -070063 enum class Key {
64 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070065
brianosman622c8d52016-05-10 06:50:49 -070066 kLeftSoftKey,
67 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070068
brianosman622c8d52016-05-10 06:50:49 -070069 kHome, //!< the home key - added to match android
70 kBack, //!< (CLR)
71 kSend, //!< the green (talk) key
72 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070073
brianosman622c8d52016-05-10 06:50:49 -070074 k0,
75 k1,
76 k2,
77 k3,
78 k4,
79 k5,
80 k6,
81 k7,
82 k8,
83 k9,
84 kStar, //!< the * key
85 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070086
brianosman622c8d52016-05-10 06:50:49 -070087 kUp,
88 kDown,
89 kLeft,
90 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -070091
Brian Osman79086b92017-02-10 13:36:16 -050092 // Keys needed by ImGui
93 kTab,
94 kPageUp,
95 kPageDown,
96 kDelete,
97 kEscape,
98 kShift,
99 kCtrl,
100 kOption, // AKA Alt
101 kA,
102 kC,
103 kV,
104 kX,
105 kY,
106 kZ,
107
brianosman622c8d52016-05-10 06:50:49 -0700108 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -0700109
brianosman622c8d52016-05-10 06:50:49 -0700110 kVolUp, //!< volume up - match android
111 kVolDown, //!< volume down - same
112 kPower, //!< power button - same
113 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -0700114
brianosman622c8d52016-05-10 06:50:49 -0700115 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -0700116 };
brianosman622c8d52016-05-10 06:50:49 -0700117 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -0700118
119 enum ModifierKeys {
120 kShift_ModifierKey = 1 << 0,
121 kControl_ModifierKey = 1 << 1,
122 kOption_ModifierKey = 1 << 2, // same as ALT
123 kCommand_ModifierKey = 1 << 3,
124 kFirstPress_ModifierKey = 1 << 4,
125 };
126
127 enum InputState {
128 kDown_InputState,
129 kUp_InputState,
130 kMove_InputState // only valid for mouse
131 };
132
Brian Osman80fc07e2017-12-08 16:45:43 -0500133 class Layer {
134 public:
Brian Osman56a24812017-12-19 11:15:16 -0500135 Layer() : fActive(true) {}
Brian Osman80fc07e2017-12-08 16:45:43 -0500136 virtual ~Layer() = default;
jvanverth9f372462016-04-06 06:08:59 -0700137
Brian Osman56a24812017-12-19 11:15:16 -0500138 bool getActive() { return fActive; }
139 void setActive(bool active) { fActive = active; }
140
Brian Osman80fc07e2017-12-08 16:45:43 -0500141 // return value of 'true' means 'I have handled this event'
142 virtual void onBackendCreated() {}
Brian Osmand67e5182017-12-08 16:46:09 -0500143 virtual void onAttach(Window* window) {}
Brian Osman80fc07e2017-12-08 16:45:43 -0500144 virtual bool onChar(SkUnichar c, uint32_t modifiers) { return false; }
145 virtual bool onKey(Key key, InputState state, uint32_t modifiers) { return false; }
146 virtual bool onMouse(int x, int y, InputState state, uint32_t modifiers) { return false; }
147 virtual bool onMouseWheel(float delta, uint32_t modifiers) { return false; }
148 virtual bool onTouch(intptr_t owner, InputState state, float x, float y) { return false; }
149 virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
Brian Osmand67e5182017-12-08 16:46:09 -0500150 virtual void onPrePaint() {}
Brian Osman80fc07e2017-12-08 16:45:43 -0500151 virtual void onPaint(SkCanvas*) {}
Brian Osman56a24812017-12-19 11:15:16 -0500152
153 private:
154 friend class Window;
155 bool fActive;
Brian Osman80fc07e2017-12-08 16:45:43 -0500156 };
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700157
Brian Osman80fc07e2017-12-08 16:45:43 -0500158 void pushLayer(Layer* layer) {
Brian Osmand67e5182017-12-08 16:46:09 -0500159 layer->onAttach(this);
Brian Osman80fc07e2017-12-08 16:45:43 -0500160 fLayers.push(layer);
liyuqiane5a6cd92016-05-27 08:52:52 -0700161 }
162
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700163 void onBackendCreated();
jvanverth9fab59d2016-04-06 12:08:51 -0700164 bool onChar(SkUnichar c, uint32_t modifiers);
165 bool onKey(Key key, InputState state, uint32_t modifiers);
166 bool onMouse(int x, int y, InputState state, uint32_t modifiers);
Brian Osman79086b92017-02-10 13:36:16 -0500167 bool onMouseWheel(float delta, uint32_t modifiers);
jvanverth814e38d2016-06-06 08:48:47 -0700168 bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
liyuqiane5a6cd92016-05-27 08:52:52 -0700169 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700170 void onPaint();
bsalomonccde4ab2016-07-27 08:50:12 -0700171 void onResize(int width, int height);
jvanverth9f372462016-04-06 06:08:59 -0700172
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700173 int width();
174 int height();
jvanverthc265a922016-04-08 12:51:45 -0700175
csmartdalton578f0642017-02-24 16:04:47 -0700176 virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
Brian Osman2ac96dc2017-06-23 13:32:29 -0400177 virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
csmartdalton578f0642017-02-24 16:04:47 -0700178
179 // Actual parameters in effect, obtained from the native window.
180 int sampleCount() const;
181 int stencilBits() const;
brianosman05de2162016-05-06 13:28:57 -0700182
csmartdalton61cd31a2017-02-27 17:00:53 -0700183 // Returns null if there is not a GPU backend or if the backend is not yet created.
184 const GrContext* getGrContext() const;
185
jvanverth9f372462016-04-06 06:08:59 -0700186protected:
187 Window();
188
Brian Osman80fc07e2017-12-08 16:45:43 -0500189 SkTDArray<Layer*> fLayers;
csmartdalton578f0642017-02-24 16:04:47 -0700190 DisplayParams fRequestedDisplayParams;
jvanverth9f372462016-04-06 06:08:59 -0700191
liyuqiand3cdbca2016-05-17 12:44:20 -0700192 WindowContext* fWindowContext = nullptr;
liyuqian566c8e42016-05-23 10:52:34 -0700193
194 virtual void onInval() = 0;
195
196 // Uncheck fIsContentInvalided to allow future inval/onInval.
197 void markInvalProcessed();
198
199 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
Brian Osman56a24812017-12-19 11:15:16 -0500200
201 void visitLayers(std::function<void(Layer*)> visitor);
202 bool signalLayers(std::function<bool(Layer*)> visitor);
jvanverth9f372462016-04-06 06:08:59 -0700203};
204
jvanverth34524262016-05-04 13:49:13 -0700205} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700206#endif