Skip to content

Android: add command mecanism to SDLActivity #14949

Closed
1bsyl wants to merge 3 commits into
mainfrom
br_android_RPC_cmd
Closed

Android: add command mecanism to SDLActivity #14949
1bsyl wants to merge 3 commits into
mainfrom
br_android_RPC_cmd

Conversation

@1bsyl

@1bsyl 1bsyl commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

Add some RPC commands so that SDLActivity defers most of SDL code internal execution it to the SDL main thread
see #14925

Most SDLActivity code is deferred to main SDL C thread with some command RPC.
except:

  • lifecycle event (which remains in main SDL C thread).
  • code to count recreation of activity because it is SDLActivity only.
  • code to initialize (nativeSetupJNI / nativeQuit), because it occurs before C Thread.
  • code which doesn't return void. (nativeGetHint()). because, value is needed immediately .
  • nativeInitMainThread() / nativeInitMainThread(), already in C thread.

(SDL_Window *)Android_Window global variable is removed.
Android ActivityMutex is removed.
Android_LifecycleMutex, is used to lock the SDLActivity state. (Android_WaitActiveAndLockActivity()).
since it prevents the SDLActivity to add new lifecylce message, it prevents it to change state.
factorisation of EGLSurface/native_window handling. (use in surfaceChanged/Created/Destroy, and SDLCreate/DestroyWindow()

It seem to work quite nicely from my side. I tested various stuff, but not fully.
(onNativeFileDialog() was the less easy to update, and I really have to tested).
other functions are always simple. I guess, the ways it's written with named struct, prevent lot of type mistake.

Still a draft. please give a try.
I'll try it also in production maybe in a while

maybe 2 todo:

  • I didn't add the HID native functions .. I am not familiar with them. but then didn't rely on SDL and use no locking.
    so maybe it makes not difference. (they could be added anyway afterwards).

  • we probably lose some event timestamp precision because event are sent from Main thread, and not SDLActivity thread. maybe it's not a big deal.
    but if we need it for some command, we could add. (just enabled the timestamp, and then send it with the event).
    maybe we wait to see, if this is really useful

@1bsyl 1bsyl linked an issue Feb 2, 2026 that may be closed by this pull request
@1bsyl
1bsyl marked this pull request as draft February 2, 2026 14:14
@1bsyl

1bsyl commented Feb 2, 2026

Copy link
Copy Markdown
Contributor Author

PR is still as draft because I need to test it in production,
but you can give a try as it is fully functional

@slouken slouken added this to the 3.6.0 milestone Feb 2, 2026
Comment thread src/events/SDL_events.c Outdated
// Android event processing is independent of the video subsystem
Android_PumpEvents(0);
SDL_VideoDevice *_this = SDL_GetVideoDevice();
SDL_Window *window = _this->windows;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crash if video isn't initialized (also below)

Comment thread src/joystick/android/SDL_sysjoystick.c Outdated
* FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
*/
static int keycode_to_SDL(int keycode)
int android_keycode_to_SDL(int keycode)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int android_keycode_to_SDL(int keycode)
int Android_Keycode_to_SDL(int keycode)

Comment thread src/video/android/SDL_androidwindow.h
@slouken

slouken commented Feb 2, 2026

Copy link
Copy Markdown
Collaborator

The joystick inputs are specifically designed to be thread-safe and low latency, so they should probably bypass the RPC queue.

@1bsyl

1bsyl commented Feb 2, 2026

Copy link
Copy Markdown
Contributor Author

Updated with suggestion.

maybe we can add hint define to have the 2 differents behaviors. handling with rpc shouldn't be that low

Comment thread src/events/SDL_events.c
Comment on lines +1517 to +1522
if (_this) {
SDL_Window *window = _this->windows;
if (window) {
Android_PumpEvents(window, 0);
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to pump events for lifecycle activity even if there's no window or video hasn't been initialized.

@slouken

slouken commented Feb 2, 2026

Copy link
Copy Markdown
Collaborator

Updated with suggestion.

maybe we can add hint define to have the 2 differents behaviors. handling with rpc shouldn't be that low

It still involves an extra thread context switch which could be delayed due to scheduling. We don't need a hint, we just need the gamepad events to be passed through immediately.

@1bsyl

1bsyl commented Feb 2, 2026

Copy link
Copy Markdown
Contributor Author

ok,
are all the following concerned ?

 onNativePadUp,
  onNativeJoy,
  onNativeHat,
  nativeAddJoystick,
  nativeRemoveJoystick,
  nativeAddHaptic,
  nativeRemoveHaptic

?

@slouken

slouken commented Feb 2, 2026

Copy link
Copy Markdown
Collaborator

ok, are all the following concerned ?

 onNativePadUp,
  onNativeJoy,
  onNativeHat,
  nativeAddJoystick,
  nativeRemoveJoystick,
  nativeAddHaptic,
  nativeRemoveHaptic

?

Yes, it would be all of the gamepad related functions. Really only the inputs are time sensitive, but we don't want input coming before nativeAddJoystick() has been processed, etc.

@1bsyl

1bsyl commented Feb 2, 2026

Copy link
Copy Markdown
Contributor Author

I've added a preprocessor define to toggle between gamepad under SDLActivity (now the default) or main C thread

@madebr

madebr commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

This branch was (accidentally) created on the libsdl-org/SDL repo.
Depending on how long this PR will remain alive, perhaps close and reopen from a personal fork?

@1bsyl

1bsyl commented Feb 2, 2026

Copy link
Copy Markdown
Contributor Author

oops, sorry I didn't notice that.
I'll fix that tomorrow :)
thanks!

@1bsyl

1bsyl commented Feb 3, 2026

Copy link
Copy Markdown
Contributor Author

ok new PR is there ! thanks for catching

#14962

@1bsyl 1bsyl closed this Feb 3, 2026
@sezero
sezero deleted the br_android_RPC_cmd branch February 3, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android: move any native SDL code from SDLActivity to C thread

3 participants