blob: 8e2681b41c3c2f485fb5d0987351d49624941f8c [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
Brian Osman2dd96932016-10-18 15:33:53 -040031 virtual ~Window() { this->detach(); }
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
jvanverth063ece72016-06-17 09:29:14 -070051#ifdef SK_VULKAN
jvanverthaf236b52016-05-20 06:01:06 -070052 kVulkan_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070053#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -050054#if SK_METAL && defined(SK_BUILD_FOR_MAC)
55 kMetal_BackendType,
56#endif
liyuqiand94ad582016-06-07 14:22:37 -070057 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070058
liyuqiand94ad582016-06-07 14:22:37 -070059 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070060 };
61 enum {
62 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070063 };
64
csmartdalton578f0642017-02-24 16:04:47 -070065 virtual bool attach(BackendType) = 0;
jvanverth9f372462016-04-06 06:08:59 -070066 void detach();
67
68 // input handling
brianosman622c8d52016-05-10 06:50:49 -070069 enum class Key {
70 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070071
brianosman622c8d52016-05-10 06:50:49 -070072 kLeftSoftKey,
73 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070074
brianosman622c8d52016-05-10 06:50:49 -070075 kHome, //!< the home key - added to match android
76 kBack, //!< (CLR)
77 kSend, //!< the green (talk) key
78 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070079
brianosman622c8d52016-05-10 06:50:49 -070080 k0,
81 k1,
82 k2,
83 k3,
84 k4,
85 k5,
86 k6,
87 k7,
88 k8,
89 k9,
90 kStar, //!< the * key
91 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070092
brianosman622c8d52016-05-10 06:50:49 -070093 kUp,
94 kDown,
95 kLeft,
96 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -070097
Brian Osman79086b92017-02-10 13:36:16 -050098 // Keys needed by ImGui
99 kTab,
100 kPageUp,
101 kPageDown,
102 kDelete,
103 kEscape,
104 kShift,
105 kCtrl,
106 kOption, // AKA Alt
107 kA,
108 kC,
109 kV,
110 kX,
111 kY,
112 kZ,
113
brianosman622c8d52016-05-10 06:50:49 -0700114 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -0700115
brianosman622c8d52016-05-10 06:50:49 -0700116 kVolUp, //!< volume up - match android
117 kVolDown, //!< volume down - same
118 kPower, //!< power button - same
119 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -0700120
brianosman622c8d52016-05-10 06:50:49 -0700121 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -0700122 };
brianosman622c8d52016-05-10 06:50:49 -0700123 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -0700124
Brian Osman80fc07e2017-12-08 16:45:43 -0500125 class Layer {
126 public:
Brian Osman56a24812017-12-19 11:15:16 -0500127 Layer() : fActive(true) {}
Brian Osman80fc07e2017-12-08 16:45:43 -0500128 virtual ~Layer() = default;
jvanverth9f372462016-04-06 06:08:59 -0700129
Brian Osman56a24812017-12-19 11:15:16 -0500130 bool getActive() { return fActive; }
131 void setActive(bool active) { fActive = active; }
132
Brian Osman80fc07e2017-12-08 16:45:43 -0500133 // return value of 'true' means 'I have handled this event'
134 virtual void onBackendCreated() {}
Brian Osmand67e5182017-12-08 16:46:09 -0500135 virtual void onAttach(Window* window) {}
Hal Canary3a85ed12019-07-08 16:07:57 -0400136 virtual bool onChar(SkUnichar c, ModifierKey modifiers) { return false; }
137 virtual bool onKey(Key key, InputState state, ModifierKey modifiers) { return false; }
138 virtual bool onMouse(int x, int y, InputState state, ModifierKey modifiers) { return false; }
139 virtual bool onMouseWheel(float delta, ModifierKey modifiers) { return false; }
Brian Osman80fc07e2017-12-08 16:45:43 -0500140 virtual bool onTouch(intptr_t owner, InputState state, float x, float y) { return false; }
141 virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
Brian Osmand67e5182017-12-08 16:46:09 -0500142 virtual void onPrePaint() {}
Robert Phillips9882dae2019-03-04 11:00:10 -0500143 virtual void onPaint(SkSurface*) {}
Ben Wagnera1915972018-08-09 15:06:19 -0400144 virtual void onResize(int width, int height) {}
Brian Osman56a24812017-12-19 11:15:16 -0500145
146 private:
147 friend class Window;
148 bool fActive;
Brian Osman80fc07e2017-12-08 16:45:43 -0500149 };
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700150
Brian Osman80fc07e2017-12-08 16:45:43 -0500151 void pushLayer(Layer* layer) {
Brian Osmand67e5182017-12-08 16:46:09 -0500152 layer->onAttach(this);
Mike Reed5edcd312018-08-08 11:23:41 -0400153 fLayers.push_back(layer);
liyuqiane5a6cd92016-05-27 08:52:52 -0700154 }
155
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700156 void onBackendCreated();
Hal Canary3a85ed12019-07-08 16:07:57 -0400157 bool onChar(SkUnichar c, ModifierKey modifiers);
158 bool onKey(Key key, InputState state, ModifierKey modifiers);
159 bool onMouse(int x, int y, InputState state, ModifierKey modifiers);
160 bool onMouseWheel(float delta, ModifierKey modifiers);
jvanverth814e38d2016-06-06 08:48:47 -0700161 bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
liyuqiane5a6cd92016-05-27 08:52:52 -0700162 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700163 void onPaint();
bsalomonccde4ab2016-07-27 08:50:12 -0700164 void onResize(int width, int height);
jvanverth9f372462016-04-06 06:08:59 -0700165
Jim Van Verth98385ba2019-02-06 11:23:34 -0500166 int width() const;
167 int height() const;
jvanverthc265a922016-04-08 12:51:45 -0700168
csmartdalton578f0642017-02-24 16:04:47 -0700169 virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
Brian Osman2ac96dc2017-06-23 13:32:29 -0400170 virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
csmartdalton578f0642017-02-24 16:04:47 -0700171
172 // Actual parameters in effect, obtained from the native window.
173 int sampleCount() const;
174 int stencilBits() const;
brianosman05de2162016-05-06 13:28:57 -0700175
csmartdalton61cd31a2017-02-27 17:00:53 -0700176 // Returns null if there is not a GPU backend or if the backend is not yet created.
Chris Dalton89305752018-11-01 10:52:34 -0600177 GrContext* getGrContext() const;
csmartdalton61cd31a2017-02-27 17:00:53 -0700178
jvanverth9f372462016-04-06 06:08:59 -0700179protected:
180 Window();
181
Brian Osman80fc07e2017-12-08 16:45:43 -0500182 SkTDArray<Layer*> fLayers;
csmartdalton578f0642017-02-24 16:04:47 -0700183 DisplayParams fRequestedDisplayParams;
jvanverth9f372462016-04-06 06:08:59 -0700184
liyuqiand3cdbca2016-05-17 12:44:20 -0700185 WindowContext* fWindowContext = nullptr;
liyuqian566c8e42016-05-23 10:52:34 -0700186
187 virtual void onInval() = 0;
188
189 // Uncheck fIsContentInvalided to allow future inval/onInval.
190 void markInvalProcessed();
191
192 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
Brian Osman56a24812017-12-19 11:15:16 -0500193
194 void visitLayers(std::function<void(Layer*)> visitor);
195 bool signalLayers(std::function<bool(Layer*)> visitor);
jvanverth9f372462016-04-06 06:08:59 -0700196};
197
jvanverth34524262016-05-04 13:49:13 -0700198} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700199#endif