Friday, 31 May 2019

Android - GlSurfaceView in Fragment is running twice at the same time

I have an android app that uses a GlSurfaceView to render a 3D fullscreen scene inside a fragment. I have noticed in the profiler, that the GlSurfaceView is actually running twice (in two threads), hogging resources and tanking the FPS. I have confirmed the issue by rendering the same OpenGL scene (using the same Renderer implementation) to a live wallpaper and profiling it, which only runs it once.

Am I doing anything wrong here?


The code is as follows:

MySurfaceView

class MySurfaceView(ctx: Context): GLSurfaceView(ctx)
{
    init
    {
        setEGLContextClientVersion(3)
        preserveEGLContextOnPause = true
        setRenderer( /* instantiating the renderer class */ )
    }
}

OpenGLFragment

class OpenGLFragment: Fragment()
{
    private lateinit var glView: GLSurfaceView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View?
    {
        this.glView = MySurfaceView(this.activity)
        return this.glView
    }
}

MainActivity

class MainActivity : FragmentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val fm = supportFragmentManager
        for (i in 0 until fm.getBackStackEntryCount()) {
            fm.popBackStack()
        }

        supportFragmentManager.beginTransaction().add(R.id.main_container, OpenGLFragment())
                    .addToBackStack(null).commit()
    }

}



from Android - GlSurfaceView in Fragment is running twice at the same time

No comments:

Post a Comment