From 9bb6a8610a974d1952c11be45a7fce1b2f3592b9 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 21 Oct 2026 18:17:02 -0700
Subject: [PATCH] Added HDR support for Android 15 and newer
---
android-project/app/build.gradle | 4 +-
.../main/java/org/libsdl/app/SDLActivity.java | 23 +++
.../main/java/org/libsdl/app/SDLSurface.java | 192 +++++++++++++++++-
docs/README-android.md | 2 +-
src/core/android/SDL_android.c | 26 +++
src/core/android/SDL_android.h | 1 +
src/render/SDL_render.c | 30 +++
src/video/android/SDL_androidvideo.c | 17 ++
src/video/android/SDL_androidvideo.h | 1 +
9 files changed, 290 insertions(+), 6 deletions(-)
diff --git a/android-project/app/build.gradle b/android-project/app/build.gradle
index b9d75b1463c91..d0f31504e49c8 100644
--- a/android-project/app/build.gradle
+++ b/android-project/app/build.gradle
@@ -6,11 +6,11 @@ def buildWithCMake = project.hasProperty('BUILD_WITH_CMAKE');
android {
namespace = "org.libsdl.app"
- compileSdkVersion 35
+ compileSdkVersion 36
ndkVersion = "28.2.13676358"
defaultConfig {
minSdkVersion 21
- targetSdkVersion 35
+ targetSdkVersion 36
versionCode 1
versionName "1.0"
externalNativeBuild {
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
index 1e5065c563fa0..1af7af4ea182d 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
@@ -1204,6 +1204,7 @@ protected boolean sendCommand(int command, Object data) {
public static native void nativeFocusChanged(boolean hasFocus);
public static native void onNativeDropFile(String filename);
public static native void nativeSetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate);
+ public static native void nativeSetScreenHDRHeadroom(float headroom);
public static native void onNativeResize();
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
@@ -1336,6 +1337,28 @@ public void setOrientationBis(int w, int h, boolean resizable, String hint)
mSingleton.setRequestedOrientation(req);
}
+ /**
+ * This method is called by SDL using JNI.
+ */
+ public static void setDesiredHDRHeadroom(final float headroom) {
+ if (Build.VERSION.SDK_INT < 35 /* Android 15 (V) */) {
+ return;
+ }
+
+ if (mSingleton == null) {
+ return;
+ }
+
+ mSingleton.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ if (mSurface != null) {
+ mSurface.setDesiredHDRHeadroom(headroom);
+ }
+ }
+ });
+ }
+
/**
* This method is called by SDL using JNI.
*/
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java b/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
index 3e1332a0328d3..1d1def776ea81 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
@@ -1,6 +1,5 @@
package org.libsdl.app;
-
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Insets;
@@ -16,14 +15,14 @@
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.PointerIcon;
+import android.view.ScaleGestureDetector;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowInsets;
import android.view.WindowManager;
-
-import android.view.ScaleGestureDetector;
+import java.util.function.Consumer;
/**
SDLSurface. This is what we draw on, so we need to know when it's created
@@ -51,6 +50,36 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// Pinch events
private final ScaleGestureDetector scaleGestureDetector;
+ // HDR headroom changes
+ protected Consumer<Display> mHdrSdrRatioListener;
+ protected float mDesiredHDRHeadroom = -1.0f;
+ protected boolean mHDRActive;
+ protected boolean mHDRMeasured; // true if we've measured a stable HDR headroom value
+ protected boolean mHDRStable; // true if the HDR headroom value has settled
+ protected float mReportedHDRHeadroom = -1.0f;
+ protected final Runnable mHDRStableRunnable = new Runnable() {
+ @Override
+ public void run() {
+ try {
+ if (mDisplay.getHdrSdrRatio() <= 1.01f) {
+ // We're not in HDR mode yet
+ return;
+ }
+ } catch (Exception ignored) {
+ return;
+ }
+ mHDRStable = true;
+ mHDRMeasured = true;
+ updateHDRHeadroom();
+ }
+ };
+
+ // How long the HDR/SDR ratio has to stay the same before we consider the display done ramping up
+ protected static final long HDR_STABLE_DELAY_MS = 250;
+
+ // While the HDR/SDR ratio is ramping, only report changes in headroom bigger than this fraction
+ protected static final float HDR_RAMP_REPORT_THRESHOLD = 0.1f;
+
// Startup
protected SDLSurface(Context context) {
super(context);
@@ -103,6 +132,7 @@ protected Surface getNativeSurface() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.v("SDL", "surfaceCreated()");
+ enableHDRListener(true);
SDLActivity.onNativeSurfaceCreated();
}
@@ -117,6 +147,7 @@ public void surfaceDestroyed(SurfaceHolder holder) {
mIsSurfaceReady = false;
SDLActivity.onNativeSurfaceDestroyed();
+ enableHDRListener(false);
}
// Called when the surface is resized
@@ -153,6 +184,7 @@ public void surfaceChanged(SurfaceHolder holder,
Log.v("SDL", "Window size: " + width + "x" + height);
Log.v("SDL", "Device size: " + nDeviceWidth + "x" + nDeviceHeight);
SDLActivity.nativeSetScreenResolution(width, height, nDeviceWidth, nDeviceHeight, density, mDisplay.getRefreshRate());
+ updateHDRHeadroom();
SDLActivity.onNativeResize();
// Prevent a screen distortion glitch,
@@ -329,6 +361,160 @@ public boolean onTouch(View v, MotionEvent event) {
return true;
}
+ // HDR state
+ protected float getEstimatedHDRHeadroom() {
+ float headroom = 1.0f;
+
+ // Before Android 15 we can't request HDR headroom for the surface, and
+ // we don't know how HDR content is shown relative to the SDR white level,
+ // so applications can't render HDR content in a meaningful way, so leave
+ // HDR disabled in that case.
+ if (Build.VERSION.SDK_INT < 35 /* Android 15 (V) */) {
+ return headroom;
+ }
+
+ try {
+ Display.HdrCapabilities caps = mDisplay.getHdrCapabilities();
+ if (caps != null) {
+ for (int type : caps.getSupportedHdrTypes()) {
+ if (type == Display.HdrCapabilities.HDR_TYPE_HDR10) {
+ // Estimate the headroom from the display peak luminance and the
+ // BT.2408 reference white. We don't know the actual SDR white level,
+ // so this is only a rough guess until we've shown HDR content and
+ // seen how far the display goes above SDR white.
+ final float SDR_reference_white_nits = 203.0f;
+ float max_luminance = caps.getDesiredMaxLuminance();
+ if (max_luminance <= SDR_reference_white_nits) {
+ // The display didn't report a usable peak luminance
+ max_luminance = 1000.0f;
+ }
+ headroom = max_luminance / SDR_reference_white_nits;
+ break;
+ }
+ }
+ }
+
+ if (Build.VERSION.SDK_INT >= 36 /* Android 16 */ && mDisplay.isHdrSdrRatioAvailable()) {
+ float highest = mDisplay.getHighestHdrSdrRatio();
+ if (highest > 1.0f) {
+ if (headroom > 1.0f) {
+ // Use the minimum of our estimate and the highest possible headroom
+ headroom = Math.min(headroom, highest);
+ } else {
+ headroom = highest;
+ }
+ }
+ }
+ } catch (Exception ignored) {
+ }
+
+ return headroom;
+ }
+
+ protected void updateHDRHeadroom() {
+ float headroom = getEstimatedHDRHeadroom();
+
+ if (mHDRActive && headroom > 1.0f) {
+ try {
+ if (mDisplay.isHdrSdrRatioAvailable()) {
+ // We're requesting as much headroom as the display can do, so once the
+ // display has finished ramping up, the HDR/SDR ratio is the headroom that's
+ // actually available.
+ //
+ // Until then, keep reporting the estimate so applications don't see the
+ // headroom drop to nothing and then ramp back up.
+ //
+ // After that, report the ratio even if the display can't currently go above
+ // the SDR white level, e.g. at maximum brightness, but keep it slightly
+ // above 1.0 so HDR stays enabled.
+ float current = mDisplay.getHdrSdrRatio();
+ if (mHDRMeasured) {
+ headroom = Math.max(current, 1.01f);
+ } else {
+ headroom = Math.max(headroom, current);
+ }
+ }
+ } catch (Exception ignored) {
+ }
+ }
+
+ // The ratio changes every frame while the display ramps, so only report significant changes until it settles
+ if (mHDRActive && !mHDRStable && mReportedHDRHeadroom > 0.0f &&
+ Math.abs(headroom - mReportedHDRHeadroom) < HDR_RAMP_REPORT_THRESHOLD * mReportedHDRHeadroom) {
+ return;
+ }
+
+ mReportedHDRHeadroom = headroom;
+ SDLActivity.nativeSetScreenHDRHeadroom(headroom);
+ }
+
+ protected void onHDRSDRRatioChanged() {
+ if (mHDRActive) {
+ // Wait for the headroom value to stabilize again
+ mHDRStable = false;
+ removeCallbacks(mHDRStableRunnable);
+ postDelayed(mHDRStableRunnable, HDR_STABLE_DELAY_MS);
+ }
+ updateHDRHeadroom();
+ }
+
+ protected void setDesiredHDRHeadroom(float headroom) {
+ if (Build.VERSION.SDK_INT < 35 /* Android 15 (V) */) {
+ return;
+ }
+
+ // Android only shows content above the SDR white level when more than 1.01 headroom is requested
+ if (headroom > 1.0f && headroom <= 1.01f) {
+ headroom = 1.02f;
+ }
+
+ if (headroom == mDesiredHDRHeadroom) {
+ return;
+ }
+
+ mDesiredHDRHeadroom = headroom;
+ setDesiredHdrHeadroom(headroom);
+
+ boolean active = (headroom > 1.01f);
+ if (active != mHDRActive) {
+ mHDRActive = active;
+ mHDRMeasured = false;
+ mHDRStable = false;
+ removeCallbacks(mHDRStableRunnable);
+ if (active) {
+ postDelayed(mHDRStableRunnable, HDR_STABLE_DELAY_MS);
+ }
+ updateHDRHeadroom();
+ }
+ }
+
+ protected void enableHDRListener(boolean enabled) {
+ if (Build.VERSION.SDK_INT < 35 /* Android 15 (V) */) {
+ return;
+ }
+
+ try {
+ if (enabled) {
+ if (mHdrSdrRatioListener == null && mDisplay.isHdrSdrRatioAvailable()) {
+ mHdrSdrRatioListener = new Consumer<Display>() {
+ @Override
+ public void accept(Display display) {
+ onHDRSDRRatioChanged();
+ }
+ };
+ mDisplay.registerHdrSdrRatioChangedListener(getContext().getMainExecutor(), mHdrSdrRatioListener);
+ }
+ } else {
+ if (mHdrSdrRatioListener != null) {
+ mDisplay.unregisterHdrSdrRatioChangedListener(mHdrSdrRatioListener);
+ mHdrSdrRatioListener = null;
+ }
+ removeCallbacks(mHDRStableRunnable);
+ }
+ } catch (Exception ignored) {
+ }
+ }
+
// Sensor events
protected void enableSensor(int sensortype, boolean enabled) {
// TODO: This uses getDefaultSensor - what if we have >1 accels?
diff --git a/docs/README-android.md b/docs/README-android.md
index 3bd73de4966f6..fd1fa6cb891bd 100644
--- a/docs/README-android.md
+++ b/docs/README-android.md
@@ -10,7 +10,7 @@ The rest of this README covers the Android gradle style build process.
Requirements
================================================================================
-Android SDK (version 35 or later)
+Android SDK (version 36 or later)
https://developer.android.com/sdk/index.html
Android NDK r28c or later
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 61f5060c712d9..e8f88cbd0e545 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -115,6 +115,7 @@ static jmethodID midInitTouch;
static jmethodID midMinimizeWindow;
static jmethodID midSetActivityTitle;
static jmethodID midSetCustomCursor;
+static jmethodID midSetDesiredHDRHeadroom;
static jmethodID midSetOrientation;
static jmethodID midSetRelativeMouseEnabled;
static jmethodID midSetSystemCursor;
@@ -205,6 +206,10 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)(
jint surfaceWidth, jint surfaceHeight,
jint deviceWidth, jint deviceHeight, jfloat density, jfloat rate);
+JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenHDRHeadroom)(
+ JNIEnv *env, jclass jcls,
+ jfloat headroom);
+
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
JNIEnv *env, jclass cls);
@@ -376,6 +381,7 @@ static JNINativeMethod SDLActivity_tab[] = {
// Video/input methods, registered only when the video subsystem is enabled
{ "onNativeDropFile", "(Ljava/lang/String;)V", SDL_JAVA_INTERFACE(onNativeDropFile) },
{ "nativeSetScreenResolution", "(IIIIFF)V", SDL_JAVA_INTERFACE(nativeSetScreenResolution) },
+ { "nativeSetScreenHDRHeadroom", "(F)V", SDL_JAVA_INTERFACE(nativeSetScreenHDRHeadroom) },
{ "onNativeResize", "()V", SDL_JAVA_INTERFACE(onNativeResize) },
{ "nativeSetNaturalOrientation", "(I)V", SDL_JAVA_INTERFACE(nativeSetNaturalOrientation) },
{ "onNativeRotationChanged", "(I)V", SDL_JAVA_INTERFACE(onNativeRotationChanged) },
@@ -718,6 +724,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *env, jclass cl
midMinimizeWindow = (*env)->GetStaticMethodID(env, mActivityClass, "minimizeWindow", "()V");
midSetActivityTitle = (*env)->GetStaticMethodID(env, mActivityClass, "setActivityTitle", "(Ljava/lang/String;)Z");
midSetCustomCursor = (*env)->GetStaticMethodID(env, mActivityClass, "setCustomCursor", "(I)Z");
+ midSetDesiredHDRHeadroom = (*env)->GetStaticMethodID(env, mActivityClass, "setDesiredHDRHeadroom", "(F)V");
midSetOrientation = (*env)->GetStaticMethodID(env, mActivityClass, "setOrientation", "(IIZLjava/lang/String;)V");
midSetRelativeMouseEnabled = (*env)->GetStaticMethodID(env, mActivityClass, "setRelativeMouseEnabled", "(Z)Z");
midSetSystemCursor = (*env)->GetStaticMethodID(env, mActivityClass, "setSystemCursor", "(I)Z");
@@ -736,6 +743,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *env, jclass cl
!midMinimizeWindow ||
!midSetActivityTitle ||
!midSetCustomCursor ||
+ !midSetDesiredHDRHeadroom ||
!midSetOrientation ||
!midSetRelativeMouseEnabled ||
!midSetSystemCursor ||
@@ -1165,6 +1173,18 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)(
SDL_UnlockMutex(Android_ActivityMutex);
}
+// Set screen HDR headroom
+JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenHDRHeadroom)(
+ JNIEnv *env, jclass jcls,
+ jfloat headroom)
+{
+ SDL_LockMutex(Android_ActivityMutex);
+
+ Android_SetScreenHDRHeadroom(headroom);
+
+ SDL_UnlockMutex(Android_ActivityMutex);
+}
+
// Resize
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
JNIEnv *env, jclass jcls)
@@ -2850,6 +2870,12 @@ void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint)
(*env)->DeleteLocalRef(env, jhint);
}
+void Android_JNI_SetDesiredHDRHeadroom(float headroom)
+{
+ JNIEnv *env = Android_JNI_GetEnv();
+ (*env)->CallStaticVoidMethod(env, mActivityClass, midSetDesiredHDRHeadroom, headroom);
+}
+
void Android_JNI_MinimizeWindow(void)
{
JNIEnv *env = Android_JNI_GetEnv();
diff --git a/src/core/android/SDL_android.h b/src/core/android/SDL_android.h
index bb5f06eac1cf1..f9b0c98a46128 100644
--- a/src/core/android/SDL_android.h
+++ b/src/core/android/SDL_android.h
@@ -71,6 +71,7 @@ extern void Android_JNI_SetActivityTitle(const char *title);
extern void Android_JNI_SetWindowStyle(bool fullscreen);
extern void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint);
extern void Android_JNI_MinimizeWindow(void);
+extern void Android_JNI_SetDesiredHDRHeadroom(float headroom);
extern bool Android_JNI_ShouldMinimizeOnFocusLoss(void);
extern void Android_JNI_ShowScreenKeyboard(int input_type, SDL_Rect *inputRect);
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 4f9f5b97e22fe..7cc80392ebf4e 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -877,6 +877,14 @@ static void UpdateHDRProperties(SDL_Renderer *renderer)
const float SDR_reference_white_nits = 100.0f;
renderer->SDR_white_point = SDR_reference_white_nits / SCRGB_NITS;
} else
+#endif
+#ifdef SDL_PLATFORM_ANDROID
+ if (renderer->output_colorspace == SDL_COLORSPACE_HDR10) {
+ // Android composites PQ in absolute nits, and the SDR white level isn't
+ // available, so assume the BT.2408 reference white of 203 nits for now.
+ const float SDR_reference_white_nits = 203.0f;
+ renderer->SDR_white_point = SDR_reference_white_nits / SCRGB_NITS;
+ } else
#endif
if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0) {
// The Vulkan drivers don't set luminance for HDR10 swapchains, so the
@@ -894,6 +902,20 @@ static void UpdateHDRProperties(SDL_Renderer *renderer)
} else {
renderer->HDR_headroom = SDL_GetFloatProperty(window_props, SDL_PROP_WINDOW_HDR_HEADROOM_FLOAT, 1.0f);
}
+#ifdef SDL_PLATFORM_ANDROID
+ if (SDL_GetAndroidSDKVersion() < 35) {
+ if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
+ // Requesting HDR headroom needs Android 15, and without it Android clips
+ // extended range content to SDR white, so tone map to that instead.
+ renderer->HDR_headroom = 1.0f;
+ }
+ } else {
+ // Ask for as much headroom as the display can do, which lets us report the headroom
+ // that's actually available, and applications can tone map to that. Android only shows
+ // extended range content above SDR white if it requests more than 1.01 headroom.
+ Android_JNI_SetDesiredHDRHeadroom(10000.0f);
+ }
+#endif
} else {
renderer->SDR_white_point = 1.0f;
renderer->HDR_headroom = 1.0f;
@@ -5876,6 +5898,14 @@ void SDL_DestroyRendererWithoutFreeing(SDL_Renderer *renderer)
renderer->destroyed = true;
+#ifdef SDL_PLATFORM_ANDROID
+ if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR ||
+ renderer->output_colorspace == SDL_COLORSPACE_HDR10) {
+ // We're no longer showing HDR content, so stop requesting HDR headroom
+ Android_JNI_SetDesiredHDRHeadroom(0.0f);
+ }
+#endif
+
if (renderer->window) {
SDL_RemoveWindowEventWatch(SDL_WINDOW_EVENT_WATCH_NORMAL, SDL_RendererEventWatch, renderer);
SDL_RemoveWindowRenderer(renderer->window, renderer);
diff --git a/src/video/android/SDL_androidvideo.c b/src/video/android/SDL_androidvideo.c
index 2157d1168e013..474d3272f2a44 100644
--- a/src/video/android/SDL_androidvideo.c
+++ b/src/video/android/SDL_androidvideo.c
@@ -64,6 +64,7 @@ static int Android_DeviceHeight = 0;
static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_RGB565; // Default SurfaceView format, in case this is queried before being filled
float Android_ScreenDensity = 1.0f;
static float Android_ScreenRate = 0.0f;
+static float Android_ScreenHDRHeadroom = 1.0f;
static SDL_DisplayOrientation Android_ScreenOrientation = SDL_ORIENTATION_UNKNOWN;
int Android_SafeInsetLeft = 0;
int Android_SafeInsetRight = 0;
@@ -195,6 +196,7 @@ bool Android_VideoInit(SDL_VideoDevice *_this)
display->natural_orientation = Android_JNI_GetDisplayNaturalOrientation();
display->current_orientation = Android_JNI_GetDisplayCurrentOrientation();
display->content_scale = Android_ScreenDensity;
+ Android_SetScreenHDRHeadroom(Android_ScreenHDRHeadroom);
SDL_AddHintCallback(SDL_HINT_ANDROID_TRAP_BACK_BUTTON, Android_Video_InternalHintCallback, 0);
@@ -223,6 +225,21 @@ void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int device
Android_ScreenRate = rate;
}
+void Android_SetScreenHDRHeadroom(float headroom)
+{
+ Android_ScreenHDRHeadroom = headroom;
+
+ SDL_VideoDevice *device = SDL_GetVideoDevice();
+ if (device && device->num_displays > 0) {
+ SDL_VideoDisplay *display = device->displays[0];
+ SDL_HDROutputProperties HDR;
+
+ HDR.SDR_white_level = 1.0f;
+ HDR.HDR_headroom = headroom;
+ SDL_SetDisplayHDRProperties(display, &HDR);
+ }
+}
+
static Uint32 format_to_pixelFormat(int format)
{
Uint32 pf;
diff --git a/src/video/android/SDL_androidvideo.h b/src/video/android/SDL_androidvideo.h
index 03bfb0165e281..92f603d041a7e 100644
--- a/src/video/android/SDL_androidvideo.h
+++ b/src/video/android/SDL_androidvideo.h
@@ -27,6 +27,7 @@
// Called by the JNI layer when the screen changes size or format
extern void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate);
+extern void Android_SetScreenHDRHeadroom(float headroom);
extern void Android_SetFormat(int format_wanted, int format_got);
extern void Android_SetOrientation(SDL_DisplayOrientation orientation);
extern void Android_SendResize(SDL_Window *window);