Android: preparation bug 4142, reduce usage of global variable Android_Window

This commit is contained in:
Sylvain Becker 2019-01-03 13:14:16 +01:00
parent a95f91bcea
commit 5dc25fef3b
10 changed files with 194 additions and 205 deletions

View File

@ -558,7 +558,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
jint surfaceWidth, jint surfaceHeight, jint surfaceWidth, jint surfaceHeight,
jint deviceWidth, jint deviceHeight, jint format, jfloat rate) jint deviceWidth, jint deviceHeight, jint format, jfloat rate)
{ {
Android_SetScreenResolution(surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, format, rate); Android_SetScreenResolution(Android_Window, surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, format, rate);
} }
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
@ -650,15 +650,10 @@ JNIEXPORT jint JNICALL SDL_JAVA_CONTROLLER_INTERFACE(nativeRemoveHaptic)(
/* Surface Created */ /* Surface Created */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(JNIEnv *env, jclass jcls) JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(JNIEnv *env, jclass jcls)
{ {
SDL_WindowData *data; if (Android_Window && Android_Window->driverdata)
SDL_VideoDevice *_this; {
SDL_VideoDevice *_this = SDL_GetVideoDevice();
if (Android_Window == NULL || Android_Window->driverdata == NULL ) { SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
return;
}
_this = SDL_GetVideoDevice();
data = (SDL_WindowData *) Android_Window->driverdata;
/* If the surface has been previously destroyed by onNativeSurfaceDestroyed, recreate it here */ /* If the surface has been previously destroyed by onNativeSurfaceDestroyed, recreate it here */
if (data->egl_surface == EGL_NO_SURFACE) { if (data->egl_surface == EGL_NO_SURFACE) {
@ -670,25 +665,21 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(JNIEnv* env, j
} }
/* GL Context handling is done in the event loop because this function is run from the Java thread */ /* GL Context handling is done in the event loop because this function is run from the Java thread */
}
} }
/* Surface Destroyed */ /* Surface Destroyed */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceDestroyed)(JNIEnv *env, jclass jcls) JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceDestroyed)(JNIEnv *env, jclass jcls)
{ {
if (Android_Window && Android_Window->driverdata)
{
SDL_VideoDevice *_this = SDL_GetVideoDevice();
SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
/* We have to clear the current context and destroy the egl surface here /* We have to clear the current context and destroy the egl surface here
* Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume
* Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d
*/ */
SDL_WindowData *data;
SDL_VideoDevice *_this;
if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
return;
}
_this = SDL_GetVideoDevice();
data = (SDL_WindowData *) Android_Window->driverdata;
if (data->egl_surface != EGL_NO_SURFACE) { if (data->egl_surface != EGL_NO_SURFACE) {
SDL_EGL_MakeCurrent(_this, NULL, NULL); SDL_EGL_MakeCurrent(_this, NULL, NULL);
@ -697,7 +688,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceDestroyed)(JNIEnv* env,
} }
/* GL Context handling is done in the event loop because this function is run from the Java thread */ /* GL Context handling is done in the event loop because this function is run from the Java thread */
}
} }
/* Keydown */ /* Keydown */
@ -731,7 +722,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeTouch)(
jint touch_device_id_in, jint pointer_finger_id_in, jint touch_device_id_in, jint pointer_finger_id_in,
jint action, jfloat x, jfloat y, jfloat p) jint action, jfloat x, jfloat y, jfloat p)
{ {
Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p); Android_OnTouch(Android_Window, touch_device_id_in, pointer_finger_id_in, action, x, y, p);
} }
/* Mouse */ /* Mouse */
@ -739,7 +730,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
JNIEnv *env, jclass jcls, JNIEnv *env, jclass jcls,
jint button, jint action, jfloat x, jfloat y, jboolean relative) jint button, jint action, jfloat x, jfloat y, jboolean relative)
{ {
Android_OnMouse(button, action, x, y, relative); Android_OnMouse(Android_Window, button, action, x, y, relative);
} }
/* Accelerometer */ /* Accelerometer */

View File

@ -40,27 +40,27 @@ static void ANDROIDAUDIO_PauseDevices(void) {}
#endif #endif
static void static void
android_egl_context_restore() android_egl_context_restore(SDL_Window *window)
{ {
SDL_Event event; SDL_Event event;
SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata; SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
if (SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context) < 0) { if (SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context) < 0) {
/* The context is no longer valid, create a new one */ /* The context is no longer valid, create a new one */
data->egl_context = (EGLContext) SDL_GL_CreateContext(Android_Window); data->egl_context = (EGLContext) SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context); SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context);
event.type = SDL_RENDER_DEVICE_RESET; event.type = SDL_RENDER_DEVICE_RESET;
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }
} }
static void static void
android_egl_context_backup() android_egl_context_backup(SDL_Window *window)
{ {
/* Keep a copy of the EGL Context so we can try to restore it when we resume */ /* Keep a copy of the EGL Context so we can try to restore it when we resume */
SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata; SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
data->egl_context = SDL_GL_GetCurrentContext(); data->egl_context = SDL_GL_GetCurrentContext();
/* We need to do this so the EGLSurface can be freed */ /* We need to do this so the EGLSurface can be freed */
SDL_GL_MakeCurrent(Android_Window, NULL); SDL_GL_MakeCurrent(window, NULL);
} }
void void
@ -80,7 +80,7 @@ Android_PumpEvents(_THIS)
#if SDL_ANDROID_BLOCK_ON_PAUSE #if SDL_ANDROID_BLOCK_ON_PAUSE
if (isPaused && !isPausing) { if (isPaused && !isPausing) {
/* Make sure this is the last thing we do before pausing */ /* Make sure this is the last thing we do before pausing */
android_egl_context_backup(); android_egl_context_backup(Android_Window);
ANDROIDAUDIO_PauseDevices(); ANDROIDAUDIO_PauseDevices();
if (SDL_SemWait(Android_ResumeSem) == 0) { if (SDL_SemWait(Android_ResumeSem) == 0) {
#else #else
@ -91,7 +91,7 @@ Android_PumpEvents(_THIS)
ANDROIDAUDIO_ResumeDevices(); ANDROIDAUDIO_ResumeDevices();
/* Restore the GL Context from here, as this operation is thread dependent */ /* Restore the GL Context from here, as this operation is thread dependent */
if (!SDL_HasEvent(SDL_QUIT)) { if (!SDL_HasEvent(SDL_QUIT)) {
android_egl_context_restore(); android_egl_context_restore(Android_Window);
} }
} }
} }
@ -110,7 +110,7 @@ Android_PumpEvents(_THIS)
} }
#else #else
if (SDL_SemTryWait(Android_PauseSem) == 0) { if (SDL_SemTryWait(Android_PauseSem) == 0) {
android_egl_context_backup(); android_egl_context_backup(Android_Window);
ANDROIDAUDIO_PauseDevices(); ANDROIDAUDIO_PauseDevices();
isPaused = 1; isPaused = 1;
} }

View File

@ -220,12 +220,12 @@ TranslateButton(int state)
} }
void void
Android_OnMouse(int state, int action, float x, float y, SDL_bool relative) Android_OnMouse(SDL_Window *window, int state, int action, float x, float y, SDL_bool relative)
{ {
int changes; int changes;
Uint8 button; Uint8 button;
if (!Android_Window) { if (!window) {
return; return;
} }
@ -234,25 +234,25 @@ Android_OnMouse(int state, int action, float x, float y, SDL_bool relative)
changes = state & ~last_state; changes = state & ~last_state;
button = TranslateButton(changes); button = TranslateButton(changes);
last_state = state; last_state = state;
SDL_SendMouseMotion(Android_Window, 0, relative, (int)x, (int)y); SDL_SendMouseMotion(window, 0, relative, (int)x, (int)y);
SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, button); SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
break; break;
case ACTION_UP: case ACTION_UP:
changes = last_state & ~state; changes = last_state & ~state;
button = TranslateButton(changes); button = TranslateButton(changes);
last_state = state; last_state = state;
SDL_SendMouseMotion(Android_Window, 0, relative, (int)x, (int)y); SDL_SendMouseMotion(window, 0, relative, (int)x, (int)y);
SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, button); SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
break; break;
case ACTION_MOVE: case ACTION_MOVE:
case ACTION_HOVER_MOVE: case ACTION_HOVER_MOVE:
SDL_SendMouseMotion(Android_Window, 0, relative, (int)x, (int)y); SDL_SendMouseMotion(window, 0, relative, (int)x, (int)y);
break; break;
case ACTION_SCROLL: case ACTION_SCROLL:
SDL_SendMouseWheel(Android_Window, 0, x, y, SDL_MOUSEWHEEL_NORMAL); SDL_SendMouseWheel(window, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
break; break;
default: default:

View File

@ -25,7 +25,7 @@
#include "SDL_androidvideo.h" #include "SDL_androidvideo.h"
extern void Android_InitMouse(void); extern void Android_InitMouse(void);
extern void Android_OnMouse(int button, int action, float x, float y, SDL_bool relative); extern void Android_OnMouse(SDL_Window *window, int button, int action, float x, float y, SDL_bool relative);
extern void Android_QuitMouse(void); extern void Android_QuitMouse(void);
#endif /* SDL_androidmouse_h_ */ #endif /* SDL_androidmouse_h_ */

View File

@ -40,12 +40,12 @@
#define ACTION_POINTER_DOWN 5 #define ACTION_POINTER_DOWN 5
#define ACTION_POINTER_UP 6 #define ACTION_POINTER_UP 6
static void Android_GetWindowCoordinates(float x, float y, static void Android_GetWindowCoordinates(SDL_Window *window, float x, float y,
int *window_x, int *window_y) int *window_x, int *window_y)
{ {
int window_w, window_h; int window_w, window_h;
SDL_GetWindowSize(Android_Window, &window_w, &window_h); SDL_GetWindowSize(window, &window_w, &window_h);
*window_x = (int)(x * window_w); *window_x = (int)(x * window_w);
*window_y = (int)(y * window_h); *window_y = (int)(y * window_h);
} }
@ -85,14 +85,14 @@ void Android_QuitTouch(void)
separate_mouse_and_touch = SDL_FALSE; separate_mouse_and_touch = SDL_FALSE;
} }
void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p) void Android_OnTouch(SDL_Window *window, int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
{ {
SDL_TouchID touchDeviceId = 0; SDL_TouchID touchDeviceId = 0;
SDL_FingerID fingerId = 0; SDL_FingerID fingerId = 0;
int window_x, window_y; int window_x, window_y;
static SDL_FingerID pointerFingerID = 0; static SDL_FingerID pointerFingerID = 0;
if (!Android_Window) { if (!window) {
return; return;
} }
@ -106,11 +106,11 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
case ACTION_DOWN: case ACTION_DOWN:
/* Primary pointer down */ /* Primary pointer down */
if (!separate_mouse_and_touch) { if (!separate_mouse_and_touch) {
Android_GetWindowCoordinates(x, y, &window_x, &window_y); Android_GetWindowCoordinates(window, x, y, &window_x, &window_y);
/* send moved event */ /* send moved event */
SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y); SDL_SendMouseMotion(window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
/* send mouse down event */ /* send mouse down event */
SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT); SDL_SendMouseButton(window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
} }
pointerFingerID = fingerId; pointerFingerID = fingerId;
case ACTION_POINTER_DOWN: case ACTION_POINTER_DOWN:
@ -121,9 +121,9 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
case ACTION_MOVE: case ACTION_MOVE:
if (!pointerFingerID) { if (!pointerFingerID) {
if (!separate_mouse_and_touch) { if (!separate_mouse_and_touch) {
Android_GetWindowCoordinates(x, y, &window_x, &window_y); Android_GetWindowCoordinates(window, x, y, &window_x, &window_y);
/* send moved event */ /* send moved event */
SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y); SDL_SendMouseMotion(window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
} }
} }
SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p); SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
@ -133,7 +133,7 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
/* Primary pointer up */ /* Primary pointer up */
if (!separate_mouse_and_touch) { if (!separate_mouse_and_touch) {
/* send mouse up */ /* send mouse up */
SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT); SDL_SendMouseButton(window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
} }
pointerFingerID = (SDL_FingerID) 0; pointerFingerID = (SDL_FingerID) 0;
case ACTION_POINTER_UP: case ACTION_POINTER_UP:

View File

@ -24,6 +24,6 @@
extern void Android_InitTouch(void); extern void Android_InitTouch(void);
extern void Android_QuitTouch(void); extern void Android_QuitTouch(void);
extern void Android_OnTouch( int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p); extern void Android_OnTouch(SDL_Window *window, int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p);
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -22,8 +22,7 @@
#if SDL_VIDEO_DRIVER_ANDROID #if SDL_VIDEO_DRIVER_ANDROID
/* Android SDL video driver implementation /* Android SDL video driver implementation */
*/
#include "SDL_video.h" #include "SDL_video.h"
#include "SDL_mouse.h" #include "SDL_mouse.h"
@ -69,9 +68,6 @@ static int Android_ScreenRate = 0;
SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL; SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
/* Currently only one window */
SDL_Window *Android_Window = NULL;
static int static int
Android_Available(void) Android_Available(void)
{ {
@ -212,7 +208,7 @@ Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * h
} }
void void
Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate) Android_SetScreenResolution(SDL_Window *window, int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
{ {
SDL_VideoDevice *device; SDL_VideoDevice *device;
SDL_VideoDisplay *display; SDL_VideoDisplay *display;
@ -239,10 +235,10 @@ Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth
display->desktop_mode.refresh_rate = Android_ScreenRate; display->desktop_mode.refresh_rate = Android_ScreenRate;
} }
if (Android_Window) { if (window) {
/* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
* will fall back to the old mode */ * will fall back to the old mode */
display = SDL_GetDisplayForWindow(Android_Window); display = SDL_GetDisplayForWindow(window);
display->display_modes[0].format = format; display->display_modes[0].format = format;
display->display_modes[0].w = Android_DeviceWidth; display->display_modes[0].w = Android_DeviceWidth;
@ -250,7 +246,7 @@ Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth
display->display_modes[0].refresh_rate = (int)rate; display->display_modes[0].refresh_rate = (int)rate;
display->current_mode = display->display_modes[0]; display->current_mode = display->display_modes[0];
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, surfaceWidth, surfaceHeight); SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, surfaceWidth, surfaceHeight);
} }
} }

View File

@ -28,7 +28,7 @@
#include "../SDL_sysvideo.h" #include "../SDL_sysvideo.h"
/* Called by the JNI layer when the screen changes size or format */ /* Called by the JNI layer when the screen changes size or format */
extern void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate); extern void Android_SetScreenResolution(SDL_Window *window, int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate);
/* Private display data */ /* Private display data */
@ -42,8 +42,6 @@ extern int Android_SurfaceHeight;
extern int Android_DeviceWidth; extern int Android_DeviceWidth;
extern int Android_DeviceHeight; extern int Android_DeviceHeight;
extern SDL_sem *Android_PauseSem, *Android_ResumeSem; extern SDL_sem *Android_PauseSem, *Android_ResumeSem;
extern SDL_Window *Android_Window;
#endif /* SDL_androidvideo_h_ */ #endif /* SDL_androidvideo_h_ */

View File

@ -33,6 +33,9 @@
#include "SDL_androidwindow.h" #include "SDL_androidwindow.h"
#include "SDL_hints.h" #include "SDL_hints.h"
/* Currently only one window */
SDL_Window *Android_Window = NULL;
int int
Android_CreateWindow(_THIS, SDL_Window * window) Android_CreateWindow(_THIS, SDL_Window * window)
{ {

View File

@ -31,6 +31,7 @@ extern void Android_SetWindowTitle(_THIS, SDL_Window * window);
extern void Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen); extern void Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen);
extern void Android_DestroyWindow(_THIS, SDL_Window *window); extern void Android_DestroyWindow(_THIS, SDL_Window *window);
extern SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info); extern SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info);
extern SDL_Window *Android_Window;
typedef struct typedef struct
{ {