An issue with SDL_GetDisplayDPI on android

I’m trying to get the DPI of the display by calling SDL_GetDisplayDPI, and it doesn’t work both on my Nook Color (with CyanogenMod) and Galaxy S5 (with the default version of Android). SDL_GetError says “That operaion is not supported”. Is there a robust way to find out DPI on Android with SDL? Or am I doing something wrong?

It looks like GetDisplayDPI is only implemented on Windows, macOS, and X11 currently.

Looking at the Android docs a bit, 160 * getResources().getDisplayMetrics().density; would give a DPI based on the UI scale factor.

Side notes:
There are also xdpi and ydpi fields of DisplayMetrics which are supposed to be the real physical DPI of the screen. I’m not sure how useful these are through.

It might make sense to add a separate API to SDL for physical DPI and scale factor… although you can get the scale factor by dividing SDL_GL_GetDrawableSize by SDL_GetWindowSize (only on macOS/iOS currently).

DPI is not a great way to express scale factor because every platform uses a different “base” DPI; on Windows this is 96 and macOS it’s 72 and Android it’s 160.

How did you find that it’s not implemented? Is there some documentation that says which function is in which file?
Also, would you recommend to modify the GetDisplayDPI function in my copy of SDL (that I use to build the apk) or to call java functions from my game’s C code?

Thank you.

1 Like

I’m not sure if that is documented, but what I did was a Find in Files for “GetDisplayDPI” in the SDL source. SDL_GetDisplayDPI calls a GetDisplayDPI function pointer that will be one of the backends’s implementations.

I haven’t worked with SDL’s android code but it looks like putting it in SDL_android.c would be easiest.

Looks like an Android implementation was just committed to hg:

1 Like

That’s great, but I can’t build the latest version. Visual Studio gives me around 50 errors. Does that happen often or am I doing something wrong? Should I wait until the next release?

I should note the implementation I contributed just grabs the Android DisplayMetrics values (xdpi, ydpi, etc.); if the manufacturer enters weird values, then the values will be weird. :slight_smile: