Camera2: Skeleton for output frame processing, plus face detect

- Plumbing for processing output metadata frames from the HAL
- Support for passing face detection metadata from said frames
  to the application.
- Switch calls on ICameraClient interface to use separate mutex to
  avoid deadlock scenarios with messages being communicated from the
  HAL to the camera user while calls from the user to the service are
  active.

Bug: 6243944

Change-Id: Id4cf821d9c5c3c0069be4c0f669874b6ff0d1ecd
diff --git a/services/camera/libcameraservice/Camera2Client.h b/services/camera/libcameraservice/Camera2Client.h
index dffd4ab..ca2edfab 100644
--- a/services/camera/libcameraservice/Camera2Client.h
+++ b/services/camera/libcameraservice/Camera2Client.h
@@ -31,8 +31,10 @@
  * Implements the android.hardware.camera API on top of
  * camera device HAL version 2.
  */
-class Camera2Client : public CameraService::Client,
-                      public Camera2Device::NotificationListener
+class Camera2Client :
+        public CameraService::Client,
+        public Camera2Device::NotificationListener,
+        public Camera2Device::FrameListener
 {
 public:
     // ICamera interface (see ICamera for details)
@@ -81,6 +83,7 @@
     virtual void notifyAutoExposure(uint8_t newState, int triggerId);
     virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId);
 
+    virtual void onNewFrameAvailable();
 private:
     enum State {
         DISCONNECTED,
@@ -102,6 +105,11 @@
     // they're called
     mutable Mutex mICameraLock;
 
+    // Mutex that must be locked by methods accessing the base Client's
+    // mCameraClient ICameraClient interface member, for sending notifications
+    // up to the camera user
+    mutable Mutex mICameraClientLock;
+
     status_t setPreviewWindowL(const sp<IBinder>& binder,
             sp<ANativeWindow> window);
 
@@ -200,13 +208,16 @@
         // listed in Camera.Parameters
         bool storeMetadataInBuffers;
         bool playShutterSound;
-        bool enableFocusMoveMessages;
+        bool enableFaceDetect;
 
+        bool enableFocusMoveMessages;
         int afTriggerCounter;
         int currentAfTriggerId;
         bool afInMotion;
     };
 
+    // This class encapsulates the Parameters class so that it can only be accessed
+    // by constructing a Key object, which locks the LockedParameter's mutex.
     class LockedParameters {
       public:
         class Key {
@@ -258,15 +269,32 @@
 
     } mParameters;
 
+    // Static device information; this is a subset of the information
+    // available through the staticInfo() method, used for frequently-accessed
+    // values or values that have to be calculated from the static information.
+    struct DeviceInfo {
+        int32_t arrayWidth;
+        int32_t arrayHeight;
+        uint8_t bestFaceDetectMode;
+        int32_t maxFaces;
+    };
+    const DeviceInfo *mDeviceInfo;
+
     /** Camera device-related private members */
 
     class Camera2Heap;
 
+    status_t updateRequests(const Parameters &params);
+
     // Number of zoom steps to simulate
     static const unsigned int NUM_ZOOM_STEPS = 10;
     // Used with stream IDs
     static const int NO_STREAM = -1;
 
+    /* Output frame metadata processing methods */
+
+    status_t processFrameFaceDetect(camera_metadata_t *frame);
+
     /* Preview related members */
 
     int mPreviewStreamId;
@@ -373,6 +401,8 @@
     camera_metadata_entry_t staticInfo(uint32_t tag,
             size_t minCount=0, size_t maxCount=0);
 
+    // Extract frequently-used camera static information into mDeviceInfo
+    status_t buildDeviceInfo();
     // Convert static camera info from a camera2 device to the
     // old API parameter map.
     status_t buildDefaultParameters();
@@ -380,6 +410,12 @@
     // Update parameters all requests use, based on mParameters
     status_t updateRequestCommon(camera_metadata_t *request, const Parameters &params);
 
+    // Map from sensor active array pixel coordinates to normalized camera parameter coordinates
+    // The former are (0,0)-(array width - 1, array height - 1), the latter from
+    // (-1000,-1000)-(1000,1000)
+    int arrayXToNormalized(int width) const;
+    int arrayYToNormalized(int height) const;
+
     // Update specific metadata entry with new values. Adds entry if it does not
     // exist, which will invalidate sorting
     static status_t updateEntry(camera_metadata_t *buffer,