Search code examples
androidsurfaceviewandroid-imageegltextureview

Is it possible to synchronize the content draw by OpenGL and the position of a View in Android?


There are 2 views in a FrameLayout:

  • SurfaceView
  • View

The content of SurfaceView is drawn using OpenGL in another thread:

  1. Send the Surface of SurfaceView from java to c++
  2. Create ANativeWindow: ANativeWindow_fromSurface(env, surface);
  3. Create surface: eglCreateWindowSurface()
  4. Other preparation stuff and draw to the surface
  5. setLayoutParams() for the View in the main thread.

I'm trying to make the position of View align with the content of SurfaceView. E.g if I draw a moving square, I want to set the position of View to match the square when it is moving.

But when I try it, the View moves before the content of SurfaceView.

I've tried to replace SurfaceView with a TextureView or a CustomView which uses ImageReader to provide surface to native, but no luck.

Does anyone have an idea how to make it possible?


Solution

  • I was able to do it by using a surface from ImageReader:

    • Before drawing, send the position and nano time from worker thread to main thread.
    • Draw to the Surface of ImageReader
    • Acquire the Image from ImageReader.OnImageAvailableListener
    • Get the Image.getTimestamp() value, this is nano time when the Image is created. Compare it with nano time from earlier to get the corresponding position. Update the position and draw the corresponding Image to the View.
    • Close the Image after it is drawn on screen by the Choreographer.FrameCallback after View.onDraw.