简体   繁体   English

为什么GLSurfaceView.Renderer中没有onSurfaceDestroyed方法?

[英]Why is there no onSurfaceDestroyed method in GLSurfaceView.Renderer?

I am working on an Android app that performs OpenCL/OpenGL interops on the camera perview. 我正在开发一款在相机视图上执行OpenCL / OpenGL互操作的Android应用。 I am using GLSurfaceView.Renderer. 我正在使用GLSurfaceView.Renderer。 Naturally the code to create and initialize the OpenCL running environment (from OpenGL) is called from onSurfaceCreated, and the actual processing of each preview frame happens in onDrawFrame. 当然,从onSurfaceCreated调用创建和初始化OpenCL运行环境(来自OpenGL)的代码,并且每个预览框架的实际处理都在onDrawFrame中进行。

All work well, except when I am finished, I want to clean up the OpenCL stuff. 一切顺利,除非我完成,我想清理OpenCL的东西。 Ideally an onSurfaceDestroyed method would be the perfect place to clean up, but there is no such method in GLSurfaceView.Renderer . 理想情况下,onSurfaceDestroyed方法是清理的最佳位置,但GLSurfaceView.Renderer中没有这样的方法 So the cleanup code has nowhere to go, and there is probably memory leak in my app. 所以清理代码无处可去,我的应用程序可能存在内存泄漏。

Here are my questions: 这是我的问题:

  1. Why is there no onSurfaceDestroyed method in GLSurfaceView.Renderer? 为什么GLSurfaceView.Renderer中没有onSurfaceDestroyed方法? There are onSurfaceCreated and onSurfaceChanged. 有onSurfaceCreated和onSurfaceChanged。 One would expect onSurfaceDestroyed to be there. 人们会期望onSurfaceDestroyed在那里。

  2. Given the fact that no onSurfaceDestroyed exists in GLSurfaceView.Renderer, where should my cleanup code go, and why? 鉴于GLSurfaceView.Renderer中不存在onSurfaceDestroyed,我的清理代码应该去哪里,为什么?

GLSurfaceView is a collection of helper code that simplifies the use of OpenGL ES with a SurfaceView. GLSurfaceView是一个辅助代码集合,简化了OpenGL ES与SurfaceView的使用。 You're not required to use it to use GLES, and if you've got a bunch of other stuff going on at the same time I recommend that you don't. 您不需要使用它来使用GLES,如果您同时还有其他一些东西,我建议您不要这样做。

If you compare the complexity of Grafika's " show + capture camera ", which uses GLSurfaceView, to " continuous capture ", which uses plain SurfaceView, you can see that the latter requires a bunch of extra code to manage EGL and the renderer thread, but it also has fewer hoops to jump through because it doesn't have to fight with GLSurfaceView's EGL and thread management. 如果你将使用GLSurfaceView的Grafika“ show + capture camera ”的复杂性与使用普通SurfaceView的“ 连续捕获 ”进行比较,你可以看到后者需要一堆额外的代码来管理EGL和渲染器线程,但是它也有更少的跳跃,因为它不必与GLSurfaceView的EGL和线程管理斗争。 (Just read the comments at the top of the CameraCaptureActivity class.) (只需阅读CameraCaptureActivity类顶部的注释。)

As one of the commenters noted, I suspect there's no "on destroyed" callback because the class aggressively destroys its EGL context, so there's no GLES cleanup needed. 正如其中一位评论者指出的那样,我怀疑没有“被破坏”的回调,因为该类积极破坏其EGL上下文,因此不需要GLES清理。 It would certainly have been useful for the renderer thread to have an opportunity to clean up non-GLES resources, but it doesn't, so you have to handle that through the Activity lifecycle callbacks. 渲染器线程有可能有机会清理非GLES资源,但事实并非如此,因此您必须通过Activity生命周期回调来处理它。 (At one point in development, CameraCaptureActivity handled the camera on the renderer thread, but the lack of a reliable shutdown callback made that difficult.) (在开发的某个阶段,CameraCaptureActivity在渲染器线程上处理了摄像头,但缺乏可靠的关闭回调使得这很困难。)

Your cleanup code should probably be based on the Activity lifecycle callbacks. 您的清理代码应该基于Activity生命周期回调。 Note these are somewhat dissociated from the SurfaceView callbacks. 请注意,这些与SurfaceView回调有些不同。 A full explanation can be found in an architecture doc appendix . 可以在架构文档附录中找到完整的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM