camera2: Add tracing to AutoFocusStateMachine (lens-locking actions only)

Bug: 11071158
Change-Id: I6a1d0130b6ca36c853f539ef3f1afb363a70df24
diff --git a/camera2/public/src/com/android/ex/camera2/pos/AutoFocusStateMachine.java b/camera2/public/src/com/android/ex/camera2/pos/AutoFocusStateMachine.java
index 5b11c2b..ce66285 100644
--- a/camera2/public/src/com/android/ex/camera2/pos/AutoFocusStateMachine.java
+++ b/camera2/public/src/com/android/ex/camera2/pos/AutoFocusStateMachine.java
@@ -21,6 +21,8 @@
 import android.hardware.camera2.CaptureResult;
 import android.util.Log;
 
+import com.android.ex.camera2.utils.SysTrace;
+
 /**
  * Manage the auto focus state machine for CameraDevice.
  *
@@ -72,6 +74,10 @@
     private int mCurrentAfMode = AF_UNINITIALIZED;
     private int mCurrentAfTrigger = AF_UNINITIALIZED;
 
+    private int mCurrentAfCookie = AF_UNINITIALIZED;
+    private String mCurrentAfTrace = "";
+    private int mLastAfCookie = 0;
+
     public AutoFocusStateMachine(AutoFocusStateListener listener) {
         if (listener == null) {
             throw new IllegalArgumentException("listener should not be null");
@@ -146,9 +152,11 @@
         switch (afState) {
             case CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED:
                 mListener.onAutoFocusSuccess(result, /*locked*/true);
+                endTraceAsync();
                 break;
             case CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
                 mListener.onAutoFocusFail(result, /*locked*/true);
+                endTraceAsync();
                 break;
             case CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED:
                 mListener.onAutoFocusSuccess(result, /*locked*/false);
@@ -195,6 +203,8 @@
             throw new IllegalStateException("AF mode was not enabled");
         }
 
+        beginTraceAsync("AFSM_lockAutoFocus");
+
         mCurrentAfTrigger = CaptureRequest.CONTROL_AF_TRIGGER_START;
 
         repeatingBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
@@ -275,6 +285,8 @@
             CaptureRequest.Builder requestBuilder) {
         if (VERBOSE_LOGGING) Log.v(TAG, "setActiveAutoFocus");
 
+        beginTraceAsync("AFSM_setActiveAutoFocus");
+
         mCurrentAfMode = CaptureRequest.CONTROL_AF_MODE_AUTO;
 
         repeatingBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
@@ -311,4 +323,27 @@
 
         repeatingBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
     }
+
+    private synchronized void beginTraceAsync(String sectionName) {
+        if (mCurrentAfCookie != AF_UNINITIALIZED) {
+            // Terminate any currently active async sections before beginning another section
+            SysTrace.endSectionAsync(mCurrentAfTrace, mCurrentAfCookie);
+        }
+
+        mLastAfCookie++;
+        mCurrentAfCookie = mLastAfCookie;
+        mCurrentAfTrace = sectionName;
+
+        SysTrace.beginSectionAsync(sectionName, mCurrentAfCookie);
+    }
+
+    private synchronized void endTraceAsync() {
+        if (mCurrentAfCookie == AF_UNINITIALIZED) {
+            Log.w(TAG, "endTraceAsync - no current trace active");
+            return;
+        }
+
+        SysTrace.endSectionAsync(mCurrentAfTrace, mCurrentAfCookie);
+        mCurrentAfCookie = AF_UNINITIALIZED;
+    }
 }