How to draw using framebuffer on Android?
Asked Answered
C

1

9

I would like to write an application for Android which displays stuff on screen using the framebuffer. This will run only on a specific rooted device, so permissions etc is not a problem. Same application (simple test version anyway) is already running okay on PC/Linux.

The questions:

  1. How to avoid the Android OS from accessing the framebuffer? I would like, while my application is running, to have the OS never touch the framebuffer, no writes and no ioctls. What do I need to do to get exclusive use of the framebuffer, and then (when my application quits) give it back to the OS?

  2. Are there any differences between Android framebuffer and Linux framebuffer to watch out for?

P.S. I would like to start my application as a regular Android application (with some native code), it just has no visible UI except for framebuffer draws which take over the whole screen. It would be nice to still be able to get events from the OS.

See also: http://www.kandroid.org/online-pdk/guide/display_drivers.html

Chimkent answered 25/1, 2014 at 9:13 Comment(0)
R
6

Hi Alex Not sure why / how to stop android OS from writing to framebuffer. As long as your android application is visible and on top you have the control as what you want to display.

Your application should have an activity with a SurfaceView ( you may want your application to hide notification bar call this function in oncreate of your activity) requestWindowFeature(Window.FEATURE_NO_TITLE); )

your activity should have SurfaceHolder.Callback implementation to handle callbacks as when the surface is ready to filled with framebuffer. Get the surface holder object as SurfaceView.getHolder() incase you want set pixel formats of the view etc.

Once "surfaceCreated" callback is called you can safely pass your surfaceview object(passing width and height maybe a good idea too) to the native so that you can fill it framebuffer using "ANativeWindow" class.

Check NDK sample code to see how to use the class NDK documentation

SurfaceHolder.Callback documentation

SurfaceHolder documentation

essentially you need to these (ON JB /Kitkat)

  1. get the native window (ANativeWindow) associated with the surfaceview by ANativeWindow_fromSurface.

  2. Acquire a lock on the ANativeWindow by ANativeWindow_acquire .

  3. Set geometry parameters(window,width,height,pf) for the nativewindow by ANativeWindow_setBuffersGeometry

  4. Load the nativewindow with the frambuffer stored (apply dirty rectangle if any here) by ANativeWindow_lock

  5. Final step to unlock and post the changes for rendering by ANativeWindow_unlockAndPost

Go through the ndk sample examples in case you need sample code.NDK documentation

Rowena answered 18/2, 2014 at 12:39 Comment(4)
Thank you, but your answer does not actually say anything about framebuffer :( I know about ANativeWindow. Problem is that the buffers that you can get from it are still handled by the compositor, they are double/triple buffered and (I believe) may sometimes also be composited using OpenGL draws. In any case there is substantial delay between that and actually seeing pixels on screen (several frames). I want to go one layer down, to whatever Android is using as the last step in the compositor before displaying stuff. I especially want non-double-buffered draws. Thoughts?Chimkent
you are correct these writes are still double or triple buffered (JB onwards) . I dont see a way to avoid that without changing a lot of surface flinger code. Also it may lead to buffer corruption at times when other system apps / notification tries to write to buffer if such a buffer circumvented.Rowena
yes, that's it. I want not to change surface flinger, but just turn it off, and use the fb device directly. and I also want to disable system apps/notification/anything else that may try to draw (although, usually I suppose if they draw it would only stay around for one frame).Chimkent
not sure how to turn off surface flinger or bypass it .. sorry.Rowena

© 2022 - 2024 — McMap. All rights reserved.