在Android中,是否可能同步OpenGL绘制的内容和View的位置?

huangapple go评论81阅读模式
英文:

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

问题

在FrameLayout中有两个视图:

  • SurfaceView
  • View

SurfaceView的内容是在另一个线程中使用OpenGL绘制的:

  1. 从Java将SurfaceView的Surface发送到C++
  2. 创建ANativeWindow:ANativeWindow_fromSurface(env, surface);
  3. 创建surface:eglCreateWindowSurface()
  4. 其他准备工作并绘制到surface上
  5. 在主线程中为View设置LayoutParams()

我试图使View的位置与SurfaceView的内容对齐。例如,如果我绘制一个移动的正方形,我希望在正方形移动时将View的位置设置为与正方形匹配。

但是当我尝试时,View在SurfaceView的内容之前移动。

我尝试用TextureView或使用ImageReader提供surface给本地的CustomView替换SurfaceView,但没有成功。

有人有办法实现吗?

英文:

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?

答案1

得分: 0

我通过使用ImageReaderSurface来完成了这个操作:

  • 在绘制之前,将位置和纳秒时间从工作线程发送到主线程。
  • 绘制到ImageReaderSurface上。
  • 通过ImageReader.OnImageAvailableListener获取Image
  • 获取Image.getTimestamp()的值,这是创建Image时的纳秒时间。将其与之前的纳秒时间进行比较,以获取相应的位置。更新位置并将相应的Image绘制到View上。
  • View.onDraw之后,由Choreographer.FrameCallback在屏幕上绘制Image后关闭Image
英文:

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.

huangapple
  • 本文由 发表于 2023年7月27日 16:07:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定