blob: fc46e0dc70a5e9f34ba0a122d45a6ddb974ac4c4 [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"
liyuqiane5a6cd92016-05-27 08:52:52 -070015#include "SkJSONCPP.h"
jvanverth9fab59d2016-04-06 12:08:51 -070016
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
28 virtual ~Window() {};
29
30 virtual void setTitle(const char*) = 0;
31 virtual void show() = 0;
liyuqiane5a6cd92016-05-27 08:52:52 -070032 virtual void setUIState(const Json::Value& state) {} // do nothing in default
liyuqian566c8e42016-05-23 10:52:34 -070033
34 // Shedules an invalidation event for window if one is not currently pending.
35 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
36 // the the inval event. They unset fIsContentInvalided which allow future onInval.
37 void inval();
jvanverth9f372462016-04-06 06:08:59 -070038
djsollen12d62a72016-04-21 07:59:44 -070039 virtual bool scaleContentToFit() const { return false; }
40 virtual bool supportsContentRect() const { return false; }
41 virtual SkRect getContentRect() { return SkRect::MakeEmpty(); }
42
jvanverthaf236b52016-05-20 06:01:06 -070043 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070044 kNativeGL_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070045#ifdef SK_VULKAN
jvanverthaf236b52016-05-20 06:01:06 -070046 kVulkan_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070047#endif
liyuqiand94ad582016-06-07 14:22:37 -070048 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070049
liyuqiand94ad582016-06-07 14:22:37 -070050 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070051 };
52 enum {
53 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070054 };
55
jvanverthaf236b52016-05-20 06:01:06 -070056 virtual bool attach(BackendType attachType, const DisplayParams& params) = 0;
jvanverth9f372462016-04-06 06:08:59 -070057 void detach();
58
59 // input handling
brianosman622c8d52016-05-10 06:50:49 -070060 enum class Key {
61 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070062
brianosman622c8d52016-05-10 06:50:49 -070063 kLeftSoftKey,
64 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070065
brianosman622c8d52016-05-10 06:50:49 -070066 kHome, //!< the home key - added to match android
67 kBack, //!< (CLR)
68 kSend, //!< the green (talk) key
69 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070070
brianosman622c8d52016-05-10 06:50:49 -070071 k0,
72 k1,
73 k2,
74 k3,
75 k4,
76 k5,
77 k6,
78 k7,
79 k8,
80 k9,
81 kStar, //!< the * key
82 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070083
brianosman622c8d52016-05-10 06:50:49 -070084 kUp,
85 kDown,
86 kLeft,
87 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -070088
brianosman622c8d52016-05-10 06:50:49 -070089 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -070090
brianosman622c8d52016-05-10 06:50:49 -070091 kVolUp, //!< volume up - match android
92 kVolDown, //!< volume down - same
93 kPower, //!< power button - same
94 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -070095
brianosman622c8d52016-05-10 06:50:49 -070096 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -070097 };
brianosman622c8d52016-05-10 06:50:49 -070098 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -070099
100 enum ModifierKeys {
101 kShift_ModifierKey = 1 << 0,
102 kControl_ModifierKey = 1 << 1,
103 kOption_ModifierKey = 1 << 2, // same as ALT
104 kCommand_ModifierKey = 1 << 3,
105 kFirstPress_ModifierKey = 1 << 4,
106 };
107
108 enum InputState {
109 kDown_InputState,
110 kUp_InputState,
111 kMove_InputState // only valid for mouse
112 };
113
114 // return value of 'true' means 'I have handled this event'
115 typedef bool(*OnCharFunc)(SkUnichar c, uint32_t modifiers, void* userData);
116 typedef bool(*OnKeyFunc)(Key key, InputState state, uint32_t modifiers, void* userData);
117 typedef bool(*OnMouseFunc)(int x, int y, InputState state, uint32_t modifiers, void* userData);
jvanverth814e38d2016-06-06 08:48:47 -0700118 typedef bool(*OnTouchFunc)(intptr_t owner, InputState state, float x, float y, void* userData);
liyuqiane5a6cd92016-05-27 08:52:52 -0700119 typedef void(*OnUIStateChangedFunc)(
120 const SkString& stateName, const SkString& stateValue, void* userData);
jvanverth9f372462016-04-06 06:08:59 -0700121 typedef void(*OnPaintFunc)(SkCanvas*, void* userData);
122
jvanverth9fab59d2016-04-06 12:08:51 -0700123 void registerCharFunc(OnCharFunc func, void* userData) {
124 fCharFunc = func;
125 fCharUserData = userData;
126 }
127
jvanverth9f372462016-04-06 06:08:59 -0700128 void registerKeyFunc(OnKeyFunc func, void* userData) {
129 fKeyFunc = func;
130 fKeyUserData = userData;
131 }
132
133 void registerMouseFunc(OnMouseFunc func, void* userData) {
134 fMouseFunc = func;
135 fMouseUserData = userData;
136 }
137
138 void registerPaintFunc(OnPaintFunc func, void* userData) {
139 fPaintFunc = func;
140 fPaintUserData = userData;
141 }
142
liyuqiand3cdbca2016-05-17 12:44:20 -0700143 void registerTouchFunc(OnTouchFunc func, void* userData) {
144 fTouchFunc = func;
145 fTouchUserData = userData;
146 }
147
liyuqiane5a6cd92016-05-27 08:52:52 -0700148 void registerUIStateChangedFunc(OnUIStateChangedFunc func, void* userData) {
149 fUIStateChangedFunc = func;
150 fUIStateChangedUserData = userData;
151 }
152
jvanverth9fab59d2016-04-06 12:08:51 -0700153 bool onChar(SkUnichar c, uint32_t modifiers);
154 bool onKey(Key key, InputState state, uint32_t modifiers);
155 bool onMouse(int x, int y, InputState state, uint32_t modifiers);
jvanverth814e38d2016-06-06 08:48:47 -0700156 bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
liyuqiane5a6cd92016-05-27 08:52:52 -0700157 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700158 void onPaint();
bsalomonccde4ab2016-07-27 08:50:12 -0700159 void onResize(int width, int height);
jvanverth9f372462016-04-06 06:08:59 -0700160
bsalomoneeb4c382016-07-26 08:50:26 -0700161 int width() { return fWidth; }
162 int height() { return fHeight; }
jvanverthc265a922016-04-08 12:51:45 -0700163
liyuqian40d21de2016-05-12 09:17:04 -0700164 virtual const DisplayParams& getDisplayParams();
brianosman05de2162016-05-06 13:28:57 -0700165 void setDisplayParams(const DisplayParams& params);
166
liyuqian74959a12016-06-16 14:10:34 -0700167 // This is just for the sRGB split screen
168 sk_sp<SkSurface> getOffscreenSurface(bool forceSRGB);
169
jvanverth9f372462016-04-06 06:08:59 -0700170protected:
171 Window();
172
bsalomoneeb4c382016-07-26 08:50:26 -0700173 int fWidth;
174 int fHeight;
jvanverthc265a922016-04-08 12:51:45 -0700175
jvanverth9fab59d2016-04-06 12:08:51 -0700176 OnCharFunc fCharFunc;
177 void* fCharUserData;
jvanverth9f372462016-04-06 06:08:59 -0700178 OnKeyFunc fKeyFunc;
179 void* fKeyUserData;
180 OnMouseFunc fMouseFunc;
181 void* fMouseUserData;
liyuqiand3cdbca2016-05-17 12:44:20 -0700182 OnTouchFunc fTouchFunc;
183 void* fTouchUserData;
liyuqiane5a6cd92016-05-27 08:52:52 -0700184 OnUIStateChangedFunc
185 fUIStateChangedFunc;
186 void* fUIStateChangedUserData;
jvanverth9f372462016-04-06 06:08:59 -0700187 OnPaintFunc fPaintFunc;
188 void* fPaintUserData;
189
liyuqiand3cdbca2016-05-17 12:44:20 -0700190 WindowContext* fWindowContext = nullptr;
liyuqian566c8e42016-05-23 10:52:34 -0700191
192 virtual void onInval() = 0;
193
194 // Uncheck fIsContentInvalided to allow future inval/onInval.
195 void markInvalProcessed();
196
197 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
jvanverth9f372462016-04-06 06:08:59 -0700198};
199
jvanverth34524262016-05-04 13:49:13 -0700200} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700201#endif