Skip to content

android: populate dpi_scale from DisplayMetrics.density#647

Open
benface wants to merge 1 commit into
not-fl3:masterfrom
benface:pr-android-density
Open

android: populate dpi_scale from DisplayMetrics.density#647
benface wants to merge 1 commit into
not-fl3:masterfrom
benface:pr-android-density

Conversation

@benface

@benface benface commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Upstream leaves NativeDisplayData::dpi_scale at 1.0 on Android. macroquad's screen_width() / mouse_position() divide by dpi_scale to return density-independent units, so the same code that returns dp on a Retina Mac returns physical pixels on Android — UI laid out in dp lands at the wrong screen position, and apps have to special-case Android to compensate.

This populates dpi_scale from Resources.getDisplayMetrics().density (mdpi = 1.0, xhdpi = 2.0, etc.) via JNI:

  • in init_with_activity when NativeDisplayData is first constructed
  • on every SurfaceChanged (covers device density changes the OS may push at runtime)

query_display_density() is fail-soft — every nullable JNI lookup falls back to 1.0, preserving upstream behaviour for apps that don't expect a non-1 dpi_scale on Android.

Test plan

  • Pixel 6 (density = 2.625): screen_width() now reports ≈ 411 dp instead of ≈ 1080 px; touch coords land on dp-positioned UI without per-platform conversion.
  • cargo check --target aarch64-linux-android clean (no new warnings).

@benface benface force-pushed the pr-android-density branch from e8bae76 to ff92ef2 Compare June 14, 2026 05:52
Upstream miniquad leaves dpi_scale at its 1.0 default on Android. As a
result, macroquad's screen_width() / mouse_position() (which divide by
dpi_scale to return density-independent units) return physical-pixel
values instead of dp — so the same code that lays out UI correctly on
macOS Retina has to special-case Android.

This change queries the activity's Resources.getDisplayMetrics().density
via JNI when init_with_activity sets up NativeDisplayData and again on
every SurfaceChanged. With dpi_scale set, callers get consistent
dp units across iOS / Android / macOS / Windows.

query_display_density() is fail-soft — every nullable JNI lookup falls
back to 1.0, matching upstream's behaviour for apps that don't yet
expect a non-1 dpi_scale on Android.
@benface benface force-pushed the pr-android-density branch from ff92ef2 to bccf70b Compare June 14, 2026 05:53
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.

1 participant