Skip to content

Android: decouple video/audio subsystems from JNI initialization#15199

Open
HTRamsey wants to merge 1 commit intolibsdl-org:mainfrom
HTRamsey:feature/android-subsystem-decoupling
Open

Android: decouple video/audio subsystems from JNI initialization#15199
HTRamsey wants to merge 1 commit intolibsdl-org:mainfrom
HTRamsey:feature/android-subsystem-decoupling

Conversation

@HTRamsey
Copy link
Copy Markdown

@HTRamsey HTRamsey commented Mar 12, 2026

Summary

Allows Android embedders to use SDL without the full video/audio Java layer by gating subsystem-specific code behind SDL_VIDEO_DISABLED and SDL_AUDIO_DISABLED preprocessor flags.

This enables applications that only need joystick/gamepad support (e.g. Qt-based apps like QGroundControl) to build SDL without shipping stub Java classes for unused subsystems. Currently, QGC maintains a full set of stubbed-out SDLActivity.java, SDLAudioManager.java, and SDLInputConnection.java files just to satisfy SDL's JNI requirements, even though it only uses the controller/HID subsystem.

Changes

C layer (SDL_android.c, SDL_android.h)

  • Split SDLActivity_tab JNI method table into core (20 entries: lifecycle, hints, permissions, file dialog) and video (24 entries: surface, input, clipboard, orientation)
  • JNI_OnLoad: conditionally register SDLActivity_video_tab, SDLInputConnection, and SDLAudioManager classes
  • checkJNIReady(): no longer requires mAudioManagerClass when SDL_AUDIO_DISABLED is defined
  • nativeSetupJNI(): method ID resolution split into core (always) vs video (guarded)
  • All audio JNI methods and function implementations: #ifndef SDL_AUDIO_DISABLED
  • All video JNI callbacks and function implementations: #ifndef SDL_VIDEO_DISABLED
  • Display orientation accessors (GetDisplayNaturalOrientation, GetDisplayCurrentOrientation) remain outside video guard, needed by camera subsystem
  • SDL_SendAndroidBackButton() compiles as no-op when video disabled (public API, must always link)
  • Video-specific includes (SDL_androidkeyboard.h, etc.) guarded

Java layer (SDL.java)

  • Added SDL_INIT_* flag constants matching include/SDL3/SDL_init.h values
  • New setupJNI(int subsystems) and initialize(int subsystems) methods for selective initialization
  • Zero-arg overloads preserved for full backwards compatibility

Build config (SDL_build_config_android.h)

  • Wrapped SDL_VIDEO_DRIVER_ANDROID, OpenGL ES, Vulkan, and GPU defines in #ifndef SDL_VIDEO_DISABLED, matching the existing pattern used for audio/joystick/haptic/sensor/camera

Usage

Embedders can now build SDL with -DSDL_VIDEO_DISABLED=1 -DSDL_AUDIO_DISABLED=1 and initialize from Java:

SDL.setupJNI(SDL.SDL_INIT_JOYSTICK | SDL.SDL_INIT_HAPTIC);
SDL.initialize(SDL.SDL_INIT_JOYSTICK | SDL.SDL_INIT_HAPTIC);
SDL.setContext(myActivity);

No SDLAudioManager.java, SDLInputConnection.java, SDLDummyEdit.java, or SDLSurface.java required. SDLActivity.java only needs core methods (getContext, isAndroidTV, isChromebook, isDeXMode, isTablet, sendMessage, etc.).

Test plan

  • Full SDL Android build with default flags (no behavioral change)
  • Build with -DSDL_VIDEO_DISABLED=1: video Java classes not required, joystick/gamepad still works
  • Build with -DSDL_AUDIO_DISABLED=1: audio Java class not required
  • Build with both disabled: minimal Java surface (SDLActivity core + SDLControllerManager + HIDDeviceManager)
  • Camera subsystem still compiles when video is disabled (uses unguarded orientation accessors)
  • SDL.setupJNI() zero-arg call still works identically to before

@slouken slouken added this to the 3.6.0 milestone Mar 12, 2026
@slouken slouken requested a review from 1bsyl March 12, 2026 15:39
@slouken
Copy link
Copy Markdown
Collaborator

slouken commented Mar 12, 2026

I'm marking this draft until CI passes and the test plan is complete.

@slouken slouken marked this pull request as draft March 12, 2026 15:40
@HTRamsey HTRamsey force-pushed the feature/android-subsystem-decoupling branch from 4053b0e to 7a16beb Compare March 12, 2026 15:55
Allow Android embedders to use SDL without the full video/audio Java
layer by gating subsystem-specific code behind SDL_VIDEO_DISABLED and
SDL_AUDIO_DISABLED preprocessor flags.

This enables applications that only need joystick/gamepad support
(e.g. Qt-based apps like QGroundControl) to build SDL without shipping
stub Java classes for unused subsystems.

Changes:
- Split SDLActivity JNI method table into core (lifecycle, hints,
  permissions) and video (surface, input, clipboard, orientation)
- Gate SDLAudioManager and SDLInputConnection JNI registration
- Make checkJNIReady() subsystem-aware: no longer requires
  mAudioManagerClass when SDL_AUDIO_DISABLED
- Group method ID resolution by subsystem in nativeSetupJNI()
- Guard all video/audio function implementations and declarations
- Keep display orientation accessors always available (needed by camera)
- Add subsystem-selective SDL.setupJNI(int)/initialize(int) to SDL.java
  with backwards-compatible zero-arg overloads
- Guard SDL_VIDEO_DRIVER_ANDROID and related defines in
  SDL_build_config_android.h
@HTRamsey HTRamsey force-pushed the feature/android-subsystem-decoupling branch from 7a16beb to 7e6af41 Compare March 12, 2026 16:45
@1bsyl
Copy link
Copy Markdown
Contributor

1bsyl commented Mar 12, 2026

There are going lots of merge conflicts with #14962
It's sound much easier to apply 14962 first and then disable things with ifdef.

@HTRamsey
Copy link
Copy Markdown
Author

Not really. Also most of the changes in this are trivial so rebase conflicts would be very easy to fix

@HTRamsey HTRamsey marked this pull request as ready for review March 12, 2026 18:16
@1bsyl
Copy link
Copy Markdown
Contributor

1bsyl commented Mar 13, 2026

it's a lot of idef indeed, but not so easy to test for someone who's not running SDL without video/audio

@AntTheAlchemist
Copy link
Copy Markdown
Contributor

I'm very interested in this and can test with audio disabled.

Would proguard see the final ints and strip out unused code from SDLAudioManager.java? Or can more compile conditions be put in?

@MoNTE48
Copy link
Copy Markdown

MoNTE48 commented Mar 21, 2026

I'm also interested in this because I use OpenAl-Soft directly and don't seem to be using SDL_VIDEO anywhere.

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.

5 participants