Camera2: When focus is already locked in CAF mode, do not trigger HAL.

In HAL2 CAF modes, once focus is locked by an AF trigger, additional
triggers will not cause AF notifications, since the state will not
change again until a cancelAutofocus call.

Since the old API still expects to see a notification, short-circuit
this at the service and send an immediate success notification.

Bug: 7318298
Change-Id: Ib209a24eaf2a35a247d06aea671efe80a33d751e
diff --git a/services/camera/libcameraservice/Camera2Client.cpp b/services/camera/libcameraservice/Camera2Client.cpp
index 7290663..c5ea3ed 100644
--- a/services/camera/libcameraservice/Camera2Client.cpp
+++ b/services/camera/libcameraservice/Camera2Client.cpp
@@ -267,6 +267,17 @@
         default: result.append("UNKNOWN\n");
     }
 
+    result.append("   Focus state: ");
+    switch (p.focusState) {
+        CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_INACTIVE)
+        CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN)
+        CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED)
+        CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN)
+        CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED)
+        CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED)
+        default: result.append("UNKNOWN\n");
+    }
+
     result.append("    Focusing areas:\n");
     for (size_t i = 0; i < p.focusingAreas.size(); i++) {
         result.appendFormat("      [ (%d, %d, %d, %d), weight %d ]\n",
@@ -952,6 +963,8 @@
     if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
 
     int triggerId;
+    bool notifyImmediately = false;
+    bool notifySuccess = false;
     {
         SharedParameters::Lock l(mParameters);
         if (l.mParameters.state < Parameters::PREVIEW) {
@@ -964,15 +977,34 @@
           * with a fake value of success set to true.
           */
         if (l.mParameters.focusMode == Parameters::FOCUS_MODE_FIXED) {
+            notifyImmediately = true;
+            notifySuccess = true;
+        }
+        /**
+         * If we're in CAF mode, and AF has already been locked, just fire back
+         * the callback right away; the HAL would not send a notification since
+         * no state change would happen on a AF trigger.
+         */
+        if ( (l.mParameters.focusMode == Parameters::FOCUS_MODE_CONTINUOUS_PICTURE ||
+                l.mParameters.focusMode == Parameters::FOCUS_MODE_CONTINUOUS_VIDEO) &&
+                l.mParameters.focusState == ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED ) {
+            notifyImmediately = true;
+            notifySuccess = true;
+        }
+        /**
+         * Send immediate notification back to client
+         */
+        if (notifyImmediately) {
             SharedCameraClient::Lock l(mSharedCameraClient);
             if (l.mCameraClient != 0) {
                 l.mCameraClient->notifyCallback(CAMERA_MSG_FOCUS,
-                    /*success*/1, 0);
+                        notifySuccess ? 1 : 0, 0);
             }
-
             return OK;
         }
-
+        /**
+         * Handle quirk mode for AF in scene modes
+         */
         if (l.mParameters.quirks.triggerAfWithAuto &&
                 l.mParameters.sceneMode != ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED &&
                 l.mParameters.focusMode != Parameters::FOCUS_MODE_AUTO) {
@@ -1303,6 +1335,7 @@
     bool afInMotion = false;
     {
         SharedParameters::Lock l(mParameters);
+        l.mParameters.focusState = newState;
         switch (l.mParameters.focusMode) {
             case Parameters::FOCUS_MODE_AUTO:
             case Parameters::FOCUS_MODE_MACRO:
diff --git a/services/camera/libcameraservice/camera2/Parameters.cpp b/services/camera/libcameraservice/camera2/Parameters.cpp
index 3c679da..9a0083a 100644
--- a/services/camera/libcameraservice/camera2/Parameters.cpp
+++ b/services/camera/libcameraservice/camera2/Parameters.cpp
@@ -633,6 +633,7 @@
         params.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES,
                 supportedFocusModes);
     }
+    focusState = ANDROID_CONTROL_AF_STATE_INACTIVE;
     shadowFocusMode = FOCUS_MODE_INVALID;
 
     camera_metadata_ro_entry_t max3aRegions =
@@ -1462,8 +1463,9 @@
                 }
             }
         }
+        validatedParams.focusState = ANDROID_CONTROL_AF_STATE_INACTIVE;
         // Always reset shadow focus mode to avoid reverting settings
-        shadowFocusMode = FOCUS_MODE_INVALID;
+        validatedParams.shadowFocusMode = FOCUS_MODE_INVALID;
         // Update in case of override
         newParams.set(CameraParameters::KEY_FOCUS_MODE,
                 focusModeEnumToString(validatedParams.focusMode));
diff --git a/services/camera/libcameraservice/camera2/Parameters.h b/services/camera/libcameraservice/camera2/Parameters.h
index fd02744..8a8645e 100644
--- a/services/camera/libcameraservice/camera2/Parameters.h
+++ b/services/camera/libcameraservice/camera2/Parameters.h
@@ -88,6 +88,8 @@
         FOCUS_MODE_INVALID = -1
     } focusMode;
 
+    uint8_t focusState; // Latest focus state from HAL
+
     // For use with triggerAfWithAuto quirk
     focusMode_t shadowFocusMode;