Merge "Fix various ANRs in camera caused by GLThread owning texture ids." into gb-ub-photos-bryce
diff --git a/src/com/android/camera/CameraScreenNail.java b/src/com/android/camera/CameraScreenNail.java
index d2c62fd..5d3c5c0 100644
--- a/src/com/android/camera/CameraScreenNail.java
+++ b/src/com/android/camera/CameraScreenNail.java
@@ -231,7 +231,9 @@
                 mAcquireTexture = false;
                 mLock.notifyAll();
             } else {
-                super.releaseSurfaceTexture();
+                if (super.getSurfaceTexture() != null) {
+                    super.releaseSurfaceTexture();
+                }
                 mAnimState = ANIM_NONE; // stop the animation
             }
         }
diff --git a/src/com/android/camera/PanoramaModule.java b/src/com/android/camera/PanoramaModule.java
index 8425451..7a45124 100644
--- a/src/com/android/camera/PanoramaModule.java
+++ b/src/com/android/camera/PanoramaModule.java
@@ -452,34 +452,38 @@
                 mMosaicPreviewRenderer.release();
             }
             mMosaicPreviewRenderer = null;
-            if (screenNail.getSurfaceTexture() == null) {
-                screenNail.acquireSurfaceTexture();
-            } else {
-                screenNail.releaseSurfaceTexture();
-                screenNail.acquireSurfaceTexture();
-                mActivity.notifyScreenNailChanged();
-            }
-            final boolean isLandscape = (mActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
-            new Thread(new Runnable() {
-                @Override
-                public void run() {
-                    CameraScreenNail screenNail = (CameraScreenNail) mActivity.mCameraScreenNail;
-                    MosaicPreviewRenderer renderer = new MosaicPreviewRenderer(
-                            screenNail.getSurfaceTexture(), w, h, isLandscape);
+            screenNail.releaseSurfaceTexture();
+            screenNail.acquireSurfaceTexture();
+        }
+        mActivity.notifyScreenNailChanged();
+        final boolean isLandscape = (mActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                CameraScreenNail screenNail = (CameraScreenNail) mActivity.mCameraScreenNail;
+                SurfaceTexture surfaceTexture = screenNail.getSurfaceTexture();
+                if (surfaceTexture == null) {
                     synchronized (mRendererLock) {
-                        mMosaicPreviewRenderer = renderer;
-                        mIsConfigPending = false;
-                        mCameraTexture = mMosaicPreviewRenderer.getInputSurfaceTexture();
-
-                        if (!mPaused && !mThreadRunning && mWaitProcessorTask == null) {
-                            mMainHandler.sendEmptyMessage(MSG_RESET_TO_PREVIEW);
-                        }
+                        mIsConfigPending = true; // try config again later.
                         mIsCreatingRenderer = false;
                         mRendererLock.notifyAll();
+                        return;
                     }
                 }
-            }).start();
-        }
+                MosaicPreviewRenderer renderer = new MosaicPreviewRenderer(
+                        screenNail.getSurfaceTexture(), w, h, isLandscape);
+                synchronized (mRendererLock) {
+                    mMosaicPreviewRenderer = renderer;
+                    mCameraTexture = mMosaicPreviewRenderer.getInputSurfaceTexture();
+
+                    if (!mPaused && !mThreadRunning && mWaitProcessorTask == null) {
+                        mMainHandler.sendEmptyMessage(MSG_RESET_TO_PREVIEW);
+                    }
+                    mIsCreatingRenderer = false;
+                    mRendererLock.notifyAll();
+                }
+            }
+        }).start();
     }
 
     // Receives the layout change event from the preview area. So we can set
@@ -1012,9 +1016,7 @@
             mSoundPlayer = null;
         }
         CameraScreenNail screenNail = (CameraScreenNail) mActivity.mCameraScreenNail;
-        if (screenNail.getSurfaceTexture() != null) {
-            screenNail.releaseSurfaceTexture();
-        }
+        screenNail.releaseSurfaceTexture();
         System.gc();
     }
 
diff --git a/src/com/android/camera/PhotoModule.java b/src/com/android/camera/PhotoModule.java
index 1183344..6f88b6a 100644
--- a/src/com/android/camera/PhotoModule.java
+++ b/src/com/android/camera/PhotoModule.java
@@ -58,7 +58,6 @@
 import android.widget.FrameLayout;
 import android.widget.FrameLayout.LayoutParams;
 import android.widget.ImageView;
-import android.widget.TextView;
 import android.widget.Toast;
 
 import com.android.camera.CameraManager.CameraProxy;
@@ -74,8 +73,8 @@
 import com.android.camera.ui.TwoStateImageView;
 import com.android.camera.ui.ZoomRenderer;
 import com.android.gallery3d.common.ApiHelper;
-import com.android.gallery3d.filtershow.FilterShowActivity;
 import com.android.gallery3d.filtershow.CropExtras;
+import com.android.gallery3d.filtershow.FilterShowActivity;
 
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -310,6 +309,7 @@
 
         public void cancel() {
             mCancelled = true;
+            interrupt();
         }
 
         @Override
@@ -2121,7 +2121,9 @@
                 mSurfaceTexture = screenNail.getSurfaceTexture();
             }
             mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation);
-            mCameraDevice.setPreviewTextureAsync((SurfaceTexture) mSurfaceTexture);
+            if (mSurfaceTexture != null) {
+                mCameraDevice.setPreviewTextureAsync((SurfaceTexture) mSurfaceTexture);
+            }
         } else {
             mCameraDevice.setDisplayOrientation(mDisplayOrientation);
             mCameraDevice.setPreviewDisplayAsync(mCameraSurfaceHolder);
@@ -2580,6 +2582,7 @@
         if (mFocusManager != null) mFocusManager.setPreviewSize(width, height);
     }
 
+    @Override
     public void onCountDownFinished() {
         mSnapshotOnIdle = false;
         mFocusManager.doSnap();
diff --git a/src/com/android/camera/VideoModule.java b/src/com/android/camera/VideoModule.java
index 0fecca6..d32234a 100644
--- a/src/com/android/camera/VideoModule.java
+++ b/src/com/android/camera/VideoModule.java
@@ -28,6 +28,7 @@
 import android.content.SharedPreferences.Editor;
 import android.content.res.Configuration;
 import android.graphics.Bitmap;
+import android.graphics.SurfaceTexture;
 import android.hardware.Camera.CameraInfo;
 import android.hardware.Camera.Parameters;
 import android.hardware.Camera.PictureCallback;
@@ -891,8 +892,12 @@
         try {
             if (!effectsActive()) {
                 if (ApiHelper.HAS_SURFACE_TEXTURE) {
-                    mActivity.mCameraDevice.setPreviewTextureAsync(
-                            ((CameraScreenNail) mActivity.mCameraScreenNail).getSurfaceTexture());
+                    SurfaceTexture surfaceTexture = ((CameraScreenNail) mActivity.mCameraScreenNail)
+                            .getSurfaceTexture();
+                    if (surfaceTexture == null) {
+                        return; // The texture has been destroyed (pause, etc)
+                    }
+                    mActivity.mCameraDevice.setPreviewTextureAsync(surfaceTexture);
                 } else {
                     mActivity.mCameraDevice.setPreviewDisplayAsync(mPreviewSurfaceView.getHolder());
                 }
@@ -977,9 +982,7 @@
     private void releasePreviewResources() {
         if (ApiHelper.HAS_SURFACE_TEXTURE) {
             CameraScreenNail screenNail = (CameraScreenNail) mActivity.mCameraScreenNail;
-            if (screenNail.getSurfaceTexture() != null) {
-                screenNail.releaseSurfaceTexture();
-            }
+            screenNail.releaseSurfaceTexture();
             if (!ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) {
                 mHandler.removeMessages(HIDE_SURFACE_VIEW);
                 mPreviewSurfaceView.setVisibility(View.GONE);