diff options
author | Angus Kong <shkong@google.com> | 2011-08-16 23:06:46 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2011-08-16 23:06:46 -0700 |
commit | 97965c13c44accd66621362873f8bbac8bc104d4 (patch) | |
tree | f8fe13b46bb8d0bf4789afbb99d18068b9df78ea /src | |
parent | 1254e04152ce9efc0b54d6d97b5873d7a9acdb2f (diff) | |
parent | 36839102e03107e3fb121197036365a58ec2686e (diff) | |
download | LegacyCamera-97965c13c44accd66621362873f8bbac8bc104d4.zip LegacyCamera-97965c13c44accd66621362873f8bbac8bc104d4.tar.gz LegacyCamera-97965c13c44accd66621362873f8bbac8bc104d4.tar.bz2 |
Merge "Fix NPE at PanoramaActivity.stopCapture()"
Diffstat (limited to 'src')
3 files changed, 75 insertions, 113 deletions
diff --git a/src/com/android/camera/panorama/MosaicRendererSurfaceView.java b/src/com/android/camera/panorama/MosaicRendererSurfaceView.java index 6767c87..d107e27 100644 --- a/src/com/android/camera/panorama/MosaicRendererSurfaceView.java +++ b/src/com/android/camera/panorama/MosaicRendererSurfaceView.java @@ -16,22 +16,20 @@ package com.android.camera.panorama; -import javax.microedition.khronos.egl.EGL10; -import javax.microedition.khronos.egl.EGLConfig; -import javax.microedition.khronos.egl.EGLContext; -import javax.microedition.khronos.egl.EGLDisplay; - -import android.app.Activity; import android.content.Context; import android.graphics.PixelFormat; -import android.graphics.SurfaceTexture; import android.opengl.GLSurfaceView; import android.os.ConditionVariable; import android.util.AttributeSet; import android.util.Log; +import javax.microedition.khronos.egl.EGL10; +import javax.microedition.khronos.egl.EGLConfig; +import javax.microedition.khronos.egl.EGLContext; +import javax.microedition.khronos.egl.EGLDisplay; + public class MosaicRendererSurfaceView extends GLSurfaceView { - private static String TAG = "MosaicRendererSurfaceView"; + private static final String TAG = "MosaicRendererSurfaceView"; private static final boolean DEBUG = false; private MosaicRendererSurfaceViewRenderer mRenderer; private ConditionVariable mPreviewFrameReadyForProcessing; @@ -75,9 +73,9 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { * custom config chooser. See ConfigChooser class definition * below. */ - setEGLConfigChooser( translucent ? - new ConfigChooser(8, 8, 8, 8, depth, stencil) : - new ConfigChooser(5, 6, 5, 0, depth, stencil) ); + setEGLConfigChooser( + translucent ? new ConfigChooser(8, 8, 8, 8, depth, stencil) : + new ConfigChooser(5, 6, 5, 0, depth, stencil)); /* Set the renderer responsible for frame rendering */ mRenderer = new MosaicRendererSurfaceViewRenderer(); @@ -91,8 +89,9 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) { Log.w(TAG, "creating OpenGL ES 2.0 context"); checkEglError("Before eglCreateContext", egl); - int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; - EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list); + int[] attribList = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; + EGLContext context = egl.eglCreateContext( + display, eglConfig, EGL10.EGL_NO_CONTEXT, attribList); checkEglError("After eglCreateContext", egl); return context; } @@ -138,10 +137,10 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { /* Get the number of minimally matching EGL configurations */ - int[] num_config = new int[1]; - egl.eglChooseConfig(display, CONFIG_ATTRIBUTES, null, 0, num_config); + int[] numConfig = new int[1]; + egl.eglChooseConfig(display, CONFIG_ATTRIBUTES, null, 0, numConfig); - int numConfigs = num_config[0]; + int numConfigs = numConfig[0]; if (numConfigs <= 0) { throw new IllegalArgumentException("No configs match configSpec"); @@ -150,7 +149,7 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { /* Allocate then read the array of minimally matching EGL configs */ EGLConfig[] configs = new EGLConfig[numConfigs]; - egl.eglChooseConfig(display, CONFIG_ATTRIBUTES, configs, numConfigs, num_config); + egl.eglChooseConfig(display, CONFIG_ATTRIBUTES, configs, numConfigs, numConfig); if (DEBUG) { printConfigs(egl, display, configs); @@ -162,7 +161,7 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs) { - for(EGLConfig config : configs) { + for (EGLConfig config : configs) { int d = findConfigAttrib(egl, display, config, EGL10.EGL_DEPTH_SIZE, 0); int s = findConfigAttrib(egl, display, config, @@ -283,7 +282,7 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { for (int i = 0; i < attributes.length; i++) { int attribute = attributes[i]; String name = names[i]; - if ( egl.eglGetConfigAttrib(display, config, attribute, value)) { + if (egl.eglGetConfigAttrib(display, config, attribute, value)) { Log.w(TAG, String.format(" %s: %d\n", name, value[0])); } else { // Log.w(TAG, String.format(" %s: failed\n", name)); @@ -302,23 +301,19 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { private int[] mValue = new int[1]; } - public void lockPreviewReadyFlag() - { + public void lockPreviewReadyFlag() { mPreviewFrameReadyForProcessing.close(); } - private void unlockPreviewReadyFlag() - { + private void unlockPreviewReadyFlag() { mPreviewFrameReadyForProcessing.open(); } - public void waitUntilPreviewReady() - { + public void waitUntilPreviewReady() { mPreviewFrameReadyForProcessing.block(); } - public void setReady() - { + public void setReady() { queueEvent(new Runnable() { @Override @@ -328,8 +323,7 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { }); } - public void preprocess() - { + public void preprocess() { queueEvent(new Runnable() { @Override @@ -339,8 +333,7 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { }); } - public void transferGPUtoCPU() - { + public void transferGPUtoCPU() { queueEvent(new Runnable() { @Override @@ -351,19 +344,7 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { }); } - public void setUIObject(final Activity activity) - { - queueEvent(new Runnable() { - - @Override - public void run() { - mRenderer.setUIObject(activity); - } - }); - } - - public void setWarping(final boolean flag) - { + public void setWarping(final boolean flag) { queueEvent(new Runnable() { @Override @@ -373,14 +354,6 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { }); } - public int getTextureID() { - return mRenderer.getTextureID(); - } - - public void setSurfaceTexture(SurfaceTexture surface) { - mRenderer.setSurfaceTexture(surface); - } - public void updateSurfaceTexture() { queueEvent(new Runnable() { @@ -391,4 +364,8 @@ public class MosaicRendererSurfaceView extends GLSurfaceView { }); } + public MosaicRendererSurfaceViewRenderer getRenderer() { + return mRenderer; + } + } diff --git a/src/com/android/camera/panorama/MosaicRendererSurfaceViewRenderer.java b/src/com/android/camera/panorama/MosaicRendererSurfaceViewRenderer.java index d29765a..be504b3 100644 --- a/src/com/android/camera/panorama/MosaicRendererSurfaceViewRenderer.java +++ b/src/com/android/camera/panorama/MosaicRendererSurfaceViewRenderer.java @@ -16,16 +16,26 @@ package com.android.camera.panorama; -import javax.microedition.khronos.egl.EGLConfig; -import javax.microedition.khronos.opengles.GL10; - -import android.app.Activity; import android.graphics.SurfaceTexture; import android.opengl.GLSurfaceView; import android.util.Log; +import javax.microedition.khronos.egl.EGLConfig; +import javax.microedition.khronos.opengles.GL10; + public class MosaicRendererSurfaceViewRenderer implements GLSurfaceView.Renderer { + private static final String TAG = "MosaicRendererSurfaceViewRenderer"; + + private float[] mSTMatrix = new float[16]; + private SurfaceTexture mSurface; + private MosaicSurfaceCreateListener mSurfaceCreateListener; + + /** A callback to be called when the surface is created */ + public interface MosaicSurfaceCreateListener { + public void onMosaicSurfaceCreated(final SurfaceTexture surface); + } + @Override public void onDrawFrame(GL10 gl) { MosaicRenderer.step(); @@ -33,23 +43,21 @@ public class MosaicRendererSurfaceViewRenderer implements GLSurfaceView.Renderer @Override public void onSurfaceChanged(GL10 gl, int width, int height) { - Log.i(TAG, "Renderer: onSurfaceChanged"); MosaicRenderer.reset(width, height); Log.i(TAG, "Renderer: onSurfaceChanged"); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { - mTextureID = MosaicRenderer.init(); + mSurface = new SurfaceTexture(MosaicRenderer.init()); - mActivity.runOnUiThread(new Runnable() { + if (mSurfaceCreateListener != null) { + mSurfaceCreateListener.onMosaicSurfaceCreated(mSurface); + } + } - @Override - public void run() { - mActivity.createSurfaceTextureAndStartPreview(mTextureID); - setSurfaceTexture(mActivity.getSurfaceTexture()); - } - }); + public void setMosaicSurfaceCreateListener(MosaicSurfaceCreateListener listener) { + mSurfaceCreateListener = listener; } public void setReady() { @@ -72,25 +80,4 @@ public class MosaicRendererSurfaceViewRenderer implements GLSurfaceView.Renderer mSurface.updateTexImage(); mSurface.getTransformMatrix(mSTMatrix); } - - public void setUIObject(Activity activity) { - mActivity = (PanoramaActivity)activity; - } - - public int getTextureID() { - return mTextureID; - } - - public void setSurfaceTexture(SurfaceTexture surface) { - mSurface = surface; - } - - private float[] mSTMatrix = new float[16]; - private int mTextureID; - - private PanoramaActivity mActivity; - - private static String TAG = "MosaicRendererSurfaceViewRenderer"; - - private SurfaceTexture mSurface; } diff --git a/src/com/android/camera/panorama/PanoramaActivity.java b/src/com/android/camera/panorama/PanoramaActivity.java index 88406d6..146ac89 100644 --- a/src/com/android/camera/panorama/PanoramaActivity.java +++ b/src/com/android/camera/panorama/PanoramaActivity.java @@ -35,17 +35,16 @@ import android.graphics.Rect; import android.graphics.SurfaceTexture; import android.graphics.YuvImage; import android.hardware.Camera; +import android.hardware.Camera.Parameters; +import android.hardware.Camera.Size; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; -import android.hardware.Camera.Parameters; -import android.hardware.Camera.Size; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; -import android.view.SurfaceView; import android.view.View; import android.view.WindowManager; import android.widget.Button; @@ -59,7 +58,8 @@ import java.util.List; */ public class PanoramaActivity extends Activity implements ModePicker.OnModeChangeListener, - SurfaceTexture.OnFrameAvailableListener { + SurfaceTexture.OnFrameAvailableListener, + MosaicRendererSurfaceViewRenderer.MosaicSurfaceCreateListener { public static final int DEFAULT_SWEEP_ANGLE = 160; public static final int DEFAULT_BLEND_MODE = Mosaic.BLENDTYPE_HORIZONTAL; public static final int DEFAULT_CAPTURE_PIXELS = 960 * 720; @@ -134,20 +134,6 @@ public class PanoramaActivity extends Activity implements }; } - public void createSurfaceTextureAndStartPreview(int textureID) { - /* - * Create the SurfaceTexture that will feed this textureID, and pass it to the camera - */ - mSurface = new SurfaceTexture(textureID); - mSurface.setOnFrameAvailableListener(this); - startPreview(); - Log.i(TAG, "Created Surface Texture"); - } - - public SurfaceTexture getSurfaceTexture() { - return mSurface; - } - private void setupCamera() { openCamera(); Parameters parameters = mCameraDevice.getParameters(); @@ -256,6 +242,17 @@ public class PanoramaActivity extends Activity implements } } + public void onMosaicSurfaceCreated(final SurfaceTexture surface) { + runOnUiThread(new Runnable() { + @Override + public void run() { + mSurface = surface; + mSurface.setOnFrameAvailableListener(PanoramaActivity.this); + startCameraPreview(); + } + }); + } + public void runViewFinder() { mRealTimeMosaicView.setWarping(false); @@ -324,7 +321,7 @@ public class PanoramaActivity extends Activity implements mCaptureState = CAPTURE_VIEWFINDER; mMosaicFrameProcessor.setProgressListener(null); - stopPreview(); + stopCameraPreview(); mSurface.setOnFrameAvailableListener(null); @@ -379,7 +376,7 @@ public class PanoramaActivity extends Activity implements mReview = (ImageView) findViewById(R.id.pano_reviewarea); mRealTimeMosaicView = (MosaicRendererSurfaceView) findViewById(R.id.pano_renderer); - mRealTimeMosaicView.setUIObject(this); + mRealTimeMosaicView.getRenderer().setMosaicSurfaceCreateListener(this); mPanoControlLayout = (View) findViewById(R.id.pano_control_layout); @@ -393,19 +390,20 @@ public class PanoramaActivity extends Activity implements @OnClickAttr public void onStartButtonClicked(View v) { - if (mPausing || mThreadRunning) return; + // If mSurface == null then GL setup is not finished yet. All buttons cannot be pressed. + if (mPausing || mThreadRunning || mSurface == null) return; startCapture(); } @OnClickAttr public void onStopButtonClicked(View v) { - if (mPausing || mThreadRunning) return; + if (mPausing || mThreadRunning || mSurface == null) return; stopCapture(); } @OnClickAttr public void onOkButtonClicked(View v) { - if (mPausing || mThreadRunning) return; + if (mPausing || mThreadRunning || mSurface == null) return; mThreadRunning = true; Thread t = new Thread() { @Override @@ -435,7 +433,7 @@ public class PanoramaActivity extends Activity implements mSurface.setOnFrameAvailableListener(this); - if (!mPausing) startPreview(); + if (!mPausing) startCameraPreview(); mRealTimeMosaicView.setVisibility(View.VISIBLE); } @@ -576,10 +574,10 @@ public class PanoramaActivity extends Activity implements } } - private void startPreview() { + private void startCameraPreview() { // If we're previewing already, stop the preview first (this will blank // the screen). - if (mCameraState != PREVIEW_STOPPED) stopPreview(); + if (mCameraState != PREVIEW_STOPPED) stopCameraPreview(); setPreviewTexture(mSurface); @@ -593,7 +591,7 @@ public class PanoramaActivity extends Activity implements mCameraState = PREVIEW_ACTIVE; } - private void stopPreview() { + private void stopCameraPreview() { if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) { Log.v(TAG, "stopPreview"); mCameraDevice.stopPreview(); |