Skip to content

Commit

Permalink
Fixed Camera2 crash on first camera start.
Browse files Browse the repository at this point in the history
When camera enters video preview mode, it uses current window
size to select the best matching video preview size to request
from the camera HAL.

However, if video preview mode is the initial mode in which the
camera starts, there's a race condition between preview size
selection and camera app layout completion. If preview size
selection code is run first, it will get zero window size and
subsequently will crash.

It's not clear what's really the "best" fix in this situation
(= waiting for layout to complete would introduce extra latency
in the camera startup process), so current work-around just checks
whether window size is already defined and if not - uses display
size instead. This makes sense because normally camera window
is running full-screen anyway, so window size will match display
size.

Change-Id: Ie8555025902ba8ae6f719d45371145126290bb7c
  • Loading branch information
luden-dev authored and andi34 committed Sep 8, 2016
1 parent 096a45a commit 10ce2b0
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/com/android/camera/VideoUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -246,7 +247,17 @@ public void showPassiveFocusIndicator() {
* @return The size of the available preview area.
*/
public Point getPreviewScreenSize() {
return new Point(mRootView.getMeasuredWidth(), mRootView.getMeasuredHeight());
if (mRootView.getMeasuredWidth() > 0 && mRootView.getMeasuredHeight() > 0) {
return new Point(mRootView.getMeasuredWidth(), mRootView.getMeasuredHeight());
} else {
// Measured width and height are not yet available - use screen size
// as a proxy, should be fine given camera typically is full screen anyhow.
DisplayMetrics metrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Log.i(TAG, "Measured width and height are not yet available, reverting " +
"to display size: " + metrics.widthPixels + ", " + metrics.heightPixels);
return new Point(metrics.widthPixels, metrics.heightPixels);
}
}

/**
Expand Down

0 comments on commit 10ce2b0

Please sign in to comment.