Skip to content

Commit

Permalink
Merge pull request #459 from liaxim/textviewso
Browse files Browse the repository at this point in the history
Fix leaking GVRViewSceneObjects
  • Loading branch information
thomasflynn committed Mar 11, 2016
2 parents e5f1206 + 3e419e1 commit c200909
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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()
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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>(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<SurfaceTexture> mSurfaceTextureRef;
private final GVRContext mGvrContext;
}
}

0 comments on commit c200909

Please sign in to comment.