Camera2: Add basic concurrency guards.

- Serialize access through ICamera interface
- Serialize access to internal parameter structure

Bug: 6243944
Change-Id: I82c9811c1d059b2bca5cca7e3d73890120dbbe59
diff --git a/services/camera/libcameraservice/Camera2Client.cpp b/services/camera/libcameraservice/Camera2Client.cpp
index 6f79aef..97dbade 100644
--- a/services/camera/libcameraservice/Camera2Client.cpp
+++ b/services/camera/libcameraservice/Camera2Client.cpp
@@ -274,9 +274,11 @@
 
 void Camera2Client::disconnect() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
+
     if (mDevice == 0) return;
 
-    stopPreview();
+    stopPreviewLocked();
 
     if (mPreviewStreamId != NO_PREVIEW_STREAM) {
         mDevice->deleteStream(mPreviewStreamId);
@@ -288,23 +290,31 @@
 
 status_t Camera2Client::connect(const sp<ICameraClient>& client) {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
+
     return BAD_VALUE;
 }
 
 status_t Camera2Client::lock() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
+
     return BAD_VALUE;
 }
 
 status_t Camera2Client::unlock() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
+
     return BAD_VALUE;
 }
 
 status_t Camera2Client::setPreviewDisplay(
         const sp<Surface>& surface) {
     ATRACE_CALL();
-    if (mState == PREVIEW) return INVALID_OPERATION;
+    Mutex::Autolock icl(mICameraLock);
+
+    if (mState >= PREVIEW) return INVALID_OPERATION;
 
     sp<IBinder> binder;
     sp<ANativeWindow> window;
@@ -319,7 +329,9 @@
 status_t Camera2Client::setPreviewTexture(
         const sp<ISurfaceTexture>& surfaceTexture) {
     ATRACE_CALL();
-    if (mState == PREVIEW) return INVALID_OPERATION;
+    Mutex::Autolock icl(mICameraLock);
+
+    if (mState >= PREVIEW) return INVALID_OPERATION;
 
     sp<IBinder> binder;
     sp<ANativeWindow> window;
@@ -362,10 +374,13 @@
 
 void Camera2Client::setPreviewCallbackFlag(int flag) {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
 }
 
 status_t Camera2Client::startPreview() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
+
     status_t res;
     if (mState == PREVIEW) return INVALID_OPERATION;
 
@@ -414,6 +429,12 @@
 
 void Camera2Client::stopPreview() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
+    stopPreviewLocked();
+}
+
+void Camera2Client::stopPreviewLocked() {
+    ATRACE_CALL();
     if (mState != PREVIEW) return;
 
     mDevice->setStreamingRequest(NULL);
@@ -422,60 +443,75 @@
 
 bool Camera2Client::previewEnabled() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
     return mState == PREVIEW;
 }
 
 status_t Camera2Client::storeMetaDataInBuffers(bool enabled) {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
     return BAD_VALUE;
 }
 
 status_t Camera2Client::startRecording() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
     return BAD_VALUE;
 }
 
 void Camera2Client::stopRecording() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
 }
 
 bool Camera2Client::recordingEnabled() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
     return BAD_VALUE;
 }
 
 void Camera2Client::releaseRecordingFrame(const sp<IMemory>& mem) {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
 }
 
 status_t Camera2Client::autoFocus() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
     return OK;
 }
 
 status_t Camera2Client::cancelAutoFocus() {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
     return OK;
 }
 
 status_t Camera2Client::takePicture(int msgType) {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
     return BAD_VALUE;
 }
 
 status_t Camera2Client::setParameters(const String8& params) {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
     return OK;
 }
 
 String8 Camera2Client::getParameters() const {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
+
+    Mutex::Autolock pl(mParamsLock);
+
     // TODO: Deal with focus distances
     return mParamsFlattened;
 }
 
 status_t Camera2Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
     ATRACE_CALL();
+    Mutex::Autolock icl(mICameraLock);
     return OK;
 }
 
@@ -520,6 +556,8 @@
 
 status_t Camera2Client::buildDefaultParameters() {
     ATRACE_CALL();
+    Mutex::Autolock pl(mParamsLock);
+
     status_t res;
     CameraParameters params;
 
diff --git a/services/camera/libcameraservice/Camera2Client.h b/services/camera/libcameraservice/Camera2Client.h
index 2476f35..2bdf7d4 100644
--- a/services/camera/libcameraservice/Camera2Client.h
+++ b/services/camera/libcameraservice/Camera2Client.h
@@ -78,8 +78,16 @@
 
     /** ICamera interface-related private members */
 
+    // Mutex that must be locked by methods implementing the ICamera interface.
+    // Ensures serialization between incoming ICamera calls
+    mutable Mutex mICameraLock;
+
     status_t setPreviewWindow(const sp<IBinder>& binder,
             const sp<ANativeWindow>& window);
+    void stopPreviewLocked();
+
+    // Mutex that must be locked before accessing mParams, mParamsFlattened
+    mutable Mutex mParamsLock;
     String8 mParamsFlattened;
     // Current camera state; this is the contents of the CameraParameters object
     // in a more-efficient format. The enum values are mostly based off the