blob: 5ca96549716a010f908518fb4a30dc080e101a0e [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"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040013#include "SkTDArray.h"
liyuqiand3cdbca2016-05-17 12:44:20 -070014#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;
Ben Wagner37c54032018-04-13 14:30:23 -040019class SkSurfaceProps;
jvanverth9f372462016-04-06 06:08:59 -070020
jvanverth34524262016-05-04 13:49:13 -070021namespace sk_app {
22
jvanvertha8d0d6c2016-05-05 12:32:03 -070023class WindowContext;
24
jvanverth9f372462016-04-06 06:08:59 -070025class Window {
26public:
27 static Window* CreateNativeWindow(void* platformData);
28
Brian Osman2dd96932016-10-18 15:33:53 -040029 virtual ~Window() { this->detach(); }
jvanverth9f372462016-04-06 06:08:59 -070030
31 virtual void setTitle(const char*) = 0;
32 virtual void show() = 0;
Brian Osmaneff04b52017-11-21 13:18:02 -050033
34 // JSON-formatted UI state for Android. Do nothing by default
35 virtual void setUIState(const char*) {}
liyuqian566c8e42016-05-23 10:52:34 -070036
37 // Shedules an invalidation event for window if one is not currently pending.
38 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
39 // the the inval event. They unset fIsContentInvalided which allow future onInval.
40 void inval();
jvanverth9f372462016-04-06 06:08:59 -070041
djsollen12d62a72016-04-21 07:59:44 -070042 virtual bool scaleContentToFit() const { return false; }
djsollen12d62a72016-04-21 07:59:44 -070043
jvanverthaf236b52016-05-20 06:01:06 -070044 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070045 kNativeGL_BackendType,
Brian Salomon194db172017-08-17 14:37:06 -040046#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
47 kANGLE_BackendType,
48#endif
jvanverth063ece72016-06-17 09:29:14 -070049#ifdef SK_VULKAN
jvanverthaf236b52016-05-20 06:01:06 -070050 kVulkan_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070051#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -050052#if SK_METAL && defined(SK_BUILD_FOR_MAC)
53 kMetal_BackendType,
54#endif
liyuqiand94ad582016-06-07 14:22:37 -070055 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070056
liyuqiand94ad582016-06-07 14:22:37 -070057 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070058 };
59 enum {
60 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070061 };
62
csmartdalton578f0642017-02-24 16:04:47 -070063 virtual bool attach(BackendType) = 0;
jvanverth9f372462016-04-06 06:08:59 -070064 void detach();
65
66 // input handling
brianosman622c8d52016-05-10 06:50:49 -070067 enum class Key {
68 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070069
brianosman622c8d52016-05-10 06:50:49 -070070 kLeftSoftKey,
71 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070072
brianosman622c8d52016-05-10 06:50:49 -070073 kHome, //!< the home key - added to match android
74 kBack, //!< (CLR)
75 kSend, //!< the green (talk) key
76 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070077
brianosman622c8d52016-05-10 06:50:49 -070078 k0,
79 k1,
80 k2,
81 k3,
82 k4,
83 k5,
84 k6,
85 k7,
86 k8,
87 k9,
88 kStar, //!< the * key
89 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070090
brianosman622c8d52016-05-10 06:50:49 -070091 kUp,
92 kDown,
93 kLeft,
94 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -070095
Brian Osman79086b92017-02-10 13:36:16 -050096 // Keys needed by ImGui
97 kTab,
98 kPageUp,
99 kPageDown,
100 kDelete,
101 kEscape,
102 kShift,
103 kCtrl,
104 kOption, // AKA Alt
105 kA,
106 kC,
107 kV,
108 kX,
109 kY,
110 kZ,
111
brianosman622c8d52016-05-10 06:50:49 -0700112 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -0700113
brianosman622c8d52016-05-10 06:50:49 -0700114 kVolUp, //!< volume up - match android
115 kVolDown, //!< volume down - same
116 kPower, //!< power button - same
117 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -0700118
brianosman622c8d52016-05-10 06:50:49 -0700119 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -0700120 };
brianosman622c8d52016-05-10 06:50:49 -0700121 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -0700122
123 enum ModifierKeys {
124 kShift_ModifierKey = 1 << 0,
125 kControl_ModifierKey = 1 << 1,
126 kOption_ModifierKey = 1 << 2, // same as ALT
127 kCommand_ModifierKey = 1 << 3,
128 kFirstPress_ModifierKey = 1 << 4,
129 };
130
131 enum InputState {
132 kDown_InputState,
133 kUp_InputState,
134 kMove_InputState // only valid for mouse
135 };
136
Brian Osman80fc07e2017-12-08 16:45:43 -0500137 class Layer {
138 public:
Brian Osman56a24812017-12-19 11:15:16 -0500139 Layer() : fActive(true) {}
Brian Osman80fc07e2017-12-08 16:45:43 -0500140 virtual ~Layer() = default;
jvanverth9f372462016-04-06 06:08:59 -0700141
Brian Osman56a24812017-12-19 11:15:16 -0500142 bool getActive() { return fActive; }
143 void setActive(bool active) { fActive = active; }
144
Brian Osman80fc07e2017-12-08 16:45:43 -0500145 // return value of 'true' means 'I have handled this event'
146 virtual void onBackendCreated() {}
Brian Osmand67e5182017-12-08 16:46:09 -0500147 virtual void onAttach(Window* window) {}
Brian Osman80fc07e2017-12-08 16:45:43 -0500148 virtual bool onChar(SkUnichar c, uint32_t modifiers) { return false; }
149 virtual bool onKey(Key key, InputState state, uint32_t modifiers) { return false; }
150 virtual bool onMouse(int x, int y, InputState state, uint32_t modifiers) { return false; }
151 virtual bool onMouseWheel(float delta, uint32_t modifiers) { return false; }
152 virtual bool onTouch(intptr_t owner, InputState state, float x, float y) { return false; }
153 virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
Brian Osmand67e5182017-12-08 16:46:09 -0500154 virtual void onPrePaint() {}
Robert Phillips9882dae2019-03-04 11:00:10 -0500155 virtual void onPaint(SkSurface*) {}
Ben Wagnera1915972018-08-09 15:06:19 -0400156 virtual void onResize(int width, int height) {}
Brian Osman56a24812017-12-19 11:15:16 -0500157
158 private:
159 friend class Window;
160 bool fActive;
Brian Osman80fc07e2017-12-08 16:45:43 -0500161 };
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700162
Brian Osman80fc07e2017-12-08 16:45:43 -0500163 void pushLayer(Layer* layer) {
Brian Osmand67e5182017-12-08 16:46:09 -0500164 layer->onAttach(this);
Mike Reed5edcd312018-08-08 11:23:41 -0400165 fLayers.push_back(layer);
liyuqiane5a6cd92016-05-27 08:52:52 -0700166 }
167
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700168 void onBackendCreated();
jvanverth9fab59d2016-04-06 12:08:51 -0700169 bool onChar(SkUnichar c, uint32_t modifiers);
170 bool onKey(Key key, InputState state, uint32_t modifiers);
171 bool onMouse(int x, int y, InputState state, uint32_t modifiers);
Brian Osman79086b92017-02-10 13:36:16 -0500172 bool onMouseWheel(float delta, uint32_t modifiers);
jvanverth814e38d2016-06-06 08:48:47 -0700173 bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
liyuqiane5a6cd92016-05-27 08:52:52 -0700174 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700175 void onPaint();
bsalomonccde4ab2016-07-27 08:50:12 -0700176 void onResize(int width, int height);
jvanverth9f372462016-04-06 06:08:59 -0700177
Jim Van Verth98385ba2019-02-06 11:23:34 -0500178 int width() const;
179 int height() const;
jvanverthc265a922016-04-08 12:51:45 -0700180
csmartdalton578f0642017-02-24 16:04:47 -0700181 virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
Brian Osman2ac96dc2017-06-23 13:32:29 -0400182 virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
csmartdalton578f0642017-02-24 16:04:47 -0700183
184 // Actual parameters in effect, obtained from the native window.
185 int sampleCount() const;
186 int stencilBits() const;
brianosman05de2162016-05-06 13:28:57 -0700187
csmartdalton61cd31a2017-02-27 17:00:53 -0700188 // Returns null if there is not a GPU backend or if the backend is not yet created.
Chris Dalton89305752018-11-01 10:52:34 -0600189 GrContext* getGrContext() const;
csmartdalton61cd31a2017-02-27 17:00:53 -0700190
jvanverth9f372462016-04-06 06:08:59 -0700191protected:
192 Window();
193
Brian Osman80fc07e2017-12-08 16:45:43 -0500194 SkTDArray<Layer*> fLayers;
csmartdalton578f0642017-02-24 16:04:47 -0700195 DisplayParams fRequestedDisplayParams;
jvanverth9f372462016-04-06 06:08:59 -0700196
liyuqiand3cdbca2016-05-17 12:44:20 -0700197 WindowContext* fWindowContext = nullptr;
liyuqian566c8e42016-05-23 10:52:34 -0700198
199 virtual void onInval() = 0;
200
201 // Uncheck fIsContentInvalided to allow future inval/onInval.
202 void markInvalProcessed();
203
204 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
Brian Osman56a24812017-12-19 11:15:16 -0500205
206 void visitLayers(std::function<void(Layer*)> visitor);
207 bool signalLayers(std::function<bool(Layer*)> visitor);
jvanverth9f372462016-04-06 06:08:59 -0700208};
209
jvanverth34524262016-05-04 13:49:13 -0700210} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700211#endif