1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef _ANDROID_H_
24 #define _ANDROID_H_
25 
26 #if defined(__ANDROID__)
27 
28 #include "common/fs.h"
29 #include "common/archive.h"
30 #include "audio/mixer_intern.h"
31 #include "graphics/palette.h"
32 #include "graphics/surface.h"
33 #include "graphics/pixelbuffer.h"
34 #include "graphics/opengl/system_headers.h"
35 #include "graphics/opengl/framebuffer.h"
36 #include "backends/base-backend.h"
37 #include "backends/plugins/posix/posix-provider.h"
38 #include "backends/fs/posix/posix-fs-factory.h"
39 
40 #include "backends/platform/android/events.h"
41 #include "backends/platform/android/texture.h"
42 #include "backends/platform/android/touchcontrols.h"
43 
44 #include <pthread.h>
45 
46 #include <android/log.h>
47 
48 // toggles start
49 //#define ANDROID_DEBUG_ENTER
50 //#define ANDROID_DEBUG_GL
51 //#define ANDROID_DEBUG_GL_CALLS
52 // toggles end
53 
54 extern const char *android_log_tag;
55 
56 #define _ANDROID_LOG(prio, fmt, args...) __android_log_print(prio, android_log_tag, fmt, ## args)
57 #define LOGD(fmt, args...) _ANDROID_LOG(ANDROID_LOG_DEBUG, fmt, ##args)
58 #define LOGI(fmt, args...) _ANDROID_LOG(ANDROID_LOG_INFO, fmt, ##args)
59 #define LOGW(fmt, args...) _ANDROID_LOG(ANDROID_LOG_WARN, fmt, ##args)
60 #define LOGE(fmt, args...) _ANDROID_LOG(ANDROID_LOG_ERROR, fmt, ##args)
61 
62 #ifdef ANDROID_DEBUG_ENTER
63 #define ENTER(fmt, args...) LOGD("%s(" fmt ")", __FUNCTION__, ##args)
64 #else
65 #define ENTER(fmt, args...) do {  } while (false)
66 #endif
67 
68 #ifdef ANDROID_DEBUG_GL
69 extern void checkGlError(const char *expr, const char *file, int line);
70 
71 #ifdef ANDROID_DEBUG_GL_CALLS
72 #define GLCALLLOG(x, before) \
73 	do { \
74 		if (before) \
75 			LOGD("calling '%s' (%s:%d)", x, __FILE__, __LINE__); \
76 		else \
77 			LOGD("returned from '%s' (%s:%d)", x, __FILE__, __LINE__); \
78 	} while (false)
79 #else
80 #define GLCALLLOG(x, before) do {  } while (false)
81 #endif
82 
83 #define GLCALL(x) \
84 	do { \
85 		GLCALLLOG(#x, true); \
86 		(x); \
87 		GLCALLLOG(#x, false); \
88 		checkGlError(#x, __FILE__, __LINE__); \
89 	} while (false)
90 
91 #define GLTHREADCHECK \
92 	do { \
93 		assert(pthread_self() == _main_thread); \
94 	} while (false)
95 
96 #else
97 #define GLCALL(x) do { (x); } while (false)
98 #define GLTHREADCHECK do {  } while (false)
99 #endif
100 
101 class OSystem_Android : public EventsBaseBackend, public PaletteManager, public KeyReceiver {
102 private:
103 	// passed from the dark side
104 	int _audio_sample_rate;
105 	int _audio_buffer_size;
106 
107 	int _screen_changeid;
108 	int _egl_surface_width;
109 	int _egl_surface_height;
110 
111 	bool _force_redraw;
112 
113 	bool _opengl;
114 
115 	// Game layer
116 	GLESBaseTexture *_game_texture;
117 	Graphics::PixelBuffer _game_pbuf;
118 	OpenGL::FrameBuffer *_frame_buffer;
119 
120 	Common::Rect _focus_rect;
121 
122 	// Overlay layer
123 	GLES4444Texture *_overlay_texture;
124 	bool _show_overlay;
125 
126 	// Mouse layer
127 	GLESBaseTexture *_mouse_texture;
128 	GLESBaseTexture *_mouse_texture_palette;
129 	GLES5551Texture *_mouse_texture_rgb;
130 	Common::Point _mouse_hotspot;
131 	uint32 _mouse_keycolor;
132 	int _mouse_targetscale;
133 	bool _show_mouse;
134 	bool _use_mouse_palette;
135 
136 	bool _virtcontrols_on;
137 
138 	int _graphicsMode;
139 	bool _fullscreen;
140 	bool _ar_correction;
141 
142 	pthread_t _main_thread;
143 
144 	bool _timer_thread_exit;
145 	pthread_t _timer_thread;
146 	static void *timerThreadFunc(void *arg);
147 
148 	bool _audio_thread_exit;
149 	pthread_t _audio_thread;
150 	static void *audioThreadFunc(void *arg);
151 
152 	bool _enable_zoning;
153 	bool _virtkeybd_on;
154 
155 	Audio::MixerImpl *_mixer;
156 	timeval _startTime;
157 
158 	Common::String getSystemProperty(const char *name) const;
159 
160 	void initSurface();
161 	void deinitSurface();
162 	void initViewport();
163 
164 	void initOverlay();
165 
166 #ifdef USE_RGB_COLOR
167 	Common::String getPixelFormatName(const Graphics::PixelFormat &format) const;
168 	void initTexture(GLESBaseTexture **texture, uint width, uint height,
169 						const Graphics::PixelFormat *format);
170 #endif
171 
172 	void setupKeymapper();
173 	void setCursorPaletteInternal(const byte *colors, uint start, uint num);
174 
175 public:
176 	OSystem_Android(int audio_sample_rate, int audio_buffer_size);
177 	virtual ~OSystem_Android();
178 
179 	virtual void initBackend();
enableZoning(bool enable)180 	void enableZoning(bool enable) { _enable_zoning = enable; }
181 
182 	virtual bool hasFeature(Feature f);
183 	virtual void setFeatureState(Feature f, bool enable);
184 	virtual bool getFeatureState(Feature f);
185 
186 	virtual const GraphicsMode *getSupportedGraphicsModes() const;
187 	virtual int getDefaultGraphicsMode() const;
188 	virtual bool setGraphicsMode(int mode);
189 	virtual int getGraphicsMode() const;
190 
191 #ifdef USE_RGB_COLOR
192 	virtual Graphics::PixelFormat getScreenFormat() const;
193 	virtual Common::List<Graphics::PixelFormat> getSupportedFormats() const;
194 #endif
195 
196 	virtual void initSize(uint width, uint height,
197 							const Graphics::PixelFormat *format);
198 
199 	enum FixupType {
200 		kClear = 0,		// glClear
201 		kClearSwap,		// glClear + swapBuffers
202 		kClearUpdate	// glClear + updateScreen
203 	};
204 
205 	void clearScreen(FixupType type, byte count = 1);
206 
207 	void updateScreenRect();
208 	virtual int getScreenChangeID() const;
209 
210 	virtual int16 getHeight();
211 	virtual int16 getWidth();
212 
getPaletteManager()213 	virtual PaletteManager *getPaletteManager() {
214 		return this;
215 	}
216 
217 public:
218 	void pushEvent(int type, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6);
219 	void keyPress(const Common::KeyCode keycode, const KeyReceiver::KeyPressType type);
220 
221 private:
222 	Common::Queue<Common::Event> _event_queue;
223 	Common::Event _queuedEvent;
224 	uint32 _queuedEventTime;
225 	MutexRef _event_queue_lock;
226 
227 	Common::Point _touch_pt_down, _touch_pt_scroll, _touch_pt_dt;
228 	int _eventScaleX;
229 	int _eventScaleY;
230 	bool _touchpad_mode;
231 	int _touchpad_scale;
232 	int _trackball_scale;
233 	int _dpad_scale;
234 	int _fingersDown;
235 
236 	void clipMouse(Common::Point &p);
237 	void scaleMouse(Common::Point &p, int x, int y, bool deductDrawRect = true);
238 	void updateEventScale();
239 	void disableCursorPalette();
240 
241 	TouchControls _touchControls;
242 
243 	void drawVirtControls();
244 
245 protected:
246 	// PaletteManager API
247 	virtual void setPalette(const byte *colors, uint start, uint num);
248 	virtual void grabPalette(byte *colors, uint start, uint num);
249 
250 public:
251 	virtual void copyRectToScreen(const void *buf, int pitch, int x, int y,
252 									int w, int h);
253 	virtual void updateScreen();
254 	virtual Graphics::Surface *lockScreen();
255 	virtual void unlockScreen();
256 	virtual void setShakePos(int shakeOffset);
257 	virtual void fillScreen(uint32 col);
258 	virtual void setFocusRectangle(const Common::Rect& rect);
259 	virtual void clearFocusRectangle();
260 
261 	virtual void showOverlay();
262 	virtual void hideOverlay();
263 	virtual void clearOverlay();
264 	virtual void grabOverlay(void *buf, int pitch);
265 	virtual void copyRectToOverlay(const void *buf, int pitch,
266 									int x, int y, int w, int h);
267 	virtual int16 getOverlayHeight();
268 	virtual int16 getOverlayWidth();
269 	virtual Graphics::PixelFormat getOverlayFormat() const;
270 
271 	virtual bool showMouse(bool visible);
272 
273 	virtual void warpMouse(int x, int y);
274 	virtual void setMouseCursor(const void *buf, uint w, uint h, int hotspotX,
275 								int hotspotY, uint32 keycolor,
276 								bool dontScale,
277 								const Graphics::PixelFormat *format);
278 	virtual void setCursorPalette(const byte *colors, uint start, uint num);
279 
280 	virtual void pushEvent(const Common::Event &event);
281 	virtual bool pollEvent(Common::Event &event);
282 	virtual uint32 getMillis(bool skipRecord = false);
283 	virtual void delayMillis(uint msecs);
284 
285 	virtual MutexRef createMutex(void);
286 	virtual void lockMutex(MutexRef mutex);
287 	virtual void unlockMutex(MutexRef mutex);
288 	virtual void deleteMutex(MutexRef mutex);
289 
290 	virtual void quit();
291 
292 	virtual void setWindowCaption(const char *caption);
293 	virtual void displayMessageOnOSD(const char *msg);
294 	virtual void showVirtualKeyboard(bool enable);
295 
296 	virtual Audio::Mixer *getMixer();
297 	virtual void getTimeAndDate(TimeDate &t) const;
298 	virtual void logMessage(LogMessageType::Type type, const char *message);
299 	virtual void addSysArchivesToSearchSet(Common::SearchSet &s,
300 											int priority = 0);
301 	virtual bool openUrl(const Common::String &url);
302 	virtual Common::String getSystemLanguage() const;
303 
304 	// ResidualVM specific method
305 	virtual void launcherInitSize(uint w, uint h);
306 	bool lockMouse(bool lock);
setupScreen(uint screenW,uint screenH,bool fullscreen,bool accel3d)307 	void setupScreen(uint screenW, uint screenH, bool fullscreen, bool accel3d) {
308 		setupScreen(screenW, screenH, fullscreen, accel3d, true);
309 	}
310 	void setupScreen(uint screenW, uint screenH, bool fullscreen, bool accel3d, bool isGame);
311 	Graphics::PixelBuffer getScreenPixelBuffer();
312 };
313 
314 #endif
315 #endif
316