diff --git a/GVRf/Framework/src/org/gearvrf/scene_objects/GVRViewSceneObject.java b/GVRf/Framework/src/org/gearvrf/scene_objects/GVRViewSceneObject.java index 553e6243f..03da83eb5 100644 --- a/GVRf/Framework/src/org/gearvrf/scene_objects/GVRViewSceneObject.java +++ b/GVRf/Framework/src/org/gearvrf/scene_objects/GVRViewSceneObject.java @@ -15,6 +15,8 @@ package org.gearvrf.scene_objects; +import java.lang.ref.WeakReference; + import org.gearvrf.GVRContext; import org.gearvrf.GVRDrawFrameListener; import org.gearvrf.GVRExternalTexture; @@ -34,8 +36,7 @@ * into the scene with an arbitrarily complex geometry. * See {@link GVRView} */ -public class GVRViewSceneObject extends GVRSceneObject implements - GVRDrawFrameListener { +public class GVRViewSceneObject extends GVRSceneObject { private final Surface mSurface; private final SurfaceTexture mSurfaceTexture; @@ -54,7 +55,6 @@ public class GVRViewSceneObject extends GVRSceneObject implements public GVRViewSceneObject(GVRContext gvrContext, GVRView gvrView, GVRMesh mesh) { super(gvrContext, mesh); - gvrContext.registerDrawFrameListener(this); GVRTexture texture = new GVRExternalTexture(gvrContext); // TODO: Shader type maybe defined by some GVRView.getShaderType() @@ -68,6 +68,7 @@ public GVRViewSceneObject(GVRContext gvrContext, GVRView gvrView, GVRMesh mesh) mSurfaceTexture.setDefaultBufferSize(gvrView.getView().getWidth(), gvrView.getView().getHeight()); + gvrContext.registerDrawFrameListener(new GVRDrawFrameListenerImpl(gvrContext, mSurfaceTexture)); gvrView.setSceneObject(this); gvrView.getView().postInvalidate(); @@ -91,11 +92,6 @@ public GVRViewSceneObject(GVRContext gvrContext, GVRView gvrView, this(gvrContext, gvrView, gvrContext.createQuad(width, height)); } - @Override - public void onDrawFrame(float frameTime) { - mSurfaceTexture.updateTexImage(); - } - /** * Gets a Android {@link Canvas} for drawing into this {@link GVRViewSceneObject Scene object}. * After drawing into the provided Android {@link Canvas}, the caller must invoke {@linkplain @@ -116,4 +112,24 @@ public Canvas lockCanvas() { public void unlockCanvasAndPost(Canvas canvas) { mSurface.unlockCanvasAndPost(canvas); } + + private static final class GVRDrawFrameListenerImpl implements GVRDrawFrameListener { + GVRDrawFrameListenerImpl(final GVRContext gvrContext, final SurfaceTexture surfaceTexture) { + mSurfaceTextureRef = new WeakReference(surfaceTexture); + mGvrContext = gvrContext; + } + + @Override + public void onDrawFrame(float frameTime) { + final SurfaceTexture surfaceTexture = mSurfaceTextureRef.get(); + if (null != surfaceTexture) { + surfaceTexture.updateTexImage(); + } else { + mGvrContext.unregisterDrawFrameListener(this); + } + } + + private final WeakReference mSurfaceTextureRef; + private final GVRContext mGvrContext; + } }