6

Is there a good way to draw standard Android View objects on top of a GLSurfaceView and synchronize the movement between the two layers?

My layout looks like

<FrameLayout>
     <RelativeLayout>
     <MyCustomGLSurfaceView>
</FrameLayout>

The MyCustomGLSurfaceView implements a camera and this camera is moved when the user touches the screen appropriately.

At the end of the onDrawFrame() callback in the renderer I invoke a callback to tell the RelativeLayout that the camera has moved and that it should update the location of any subviews as necessary.

To try and synchronize the two layers I am doing the following:

  • Set renderMode = RENDERMODE_WHEN_DIRTY within MyCustomGLSurfaceView
  • Having MyCustomGLSurfaceView implement Choreographer.FrameCallback and in the doFrame(frameTimeNanos: Long) method calling requestRender()
  • onDrawFrame invokes a callback to indicate the camera has moved and the RelativeLayout updates the position of it’s subviews as necessary.
  • Delaying the position of the camera 1 frame within the MyCustomGLSurfaceView since it appears that the Android View objects aren't updated on the screen until the following frame whereas the MyCustomGLSurfaceView was updated immediately.

The theory is that by listening for Choreographer callbacks, the rendering of the MyCustomGLSurfaceView is happening at the same time as when the application is re-drawing the standard UI elements.

This seems to work reasonably well but there are noticeable stutters within the Android View object layer. Also positioning the elements in the Android View layer has some odd behaviour when doing it via setting margins (views get destroyed when they're off screen and don't always reappear and sometimes stop being updated at all). I have tried positioning them using translationX / translationY but then the synchronization between the two layers isn't as good.

Is there a good approach for achieving this synchronization?

I have thought that it might be better to use a TextureView as opposed to a GLSurfaceView since the TextureView is composited into the view hierarchy rather than being a separate surface, so I thought maybe synchronization will fall out naturally from that.

Zach Dean
  • 113
  • 5
  • Are you setting up margins using something similar to the method in [this post](https://stackoverflow.com/questions/3294590/set-the-absolute-position-of-a-view)? Also recommend looking at [Grafika](https://github.com/google/grafika) (especially Record GL App use of Choreographer). This [question](https://stackoverflow.com/questions/26317132/minimize-android-glsurfaceview-lag) may be helpful. Also, [architecture](https://source.android.com/docs/core/graphics/architecture) docs. Comment cause it doesn't feel like enough for an answer. – G. Putnam Jan 23 '23 at 22:12

0 Answers0