From df9daa56ab5f6ddc4579d4f32692435c647e8663 Mon Sep 17 00:00:00 2001
From: xianming wang <mingwax@codeaurora.org>
Date: Wed, 4 Apr 2018 16:36:01 +0800
Subject: [PATCH] SnapdragonCamera: Add the 60fps support for preview

Add the 60fps support for preview.

CRs-Fixed: 2210765
Change-Id: If16aff4e73721050e40364c1415c8a8dfe1faa14
---
 src/com/android/camera/CaptureModule.java   |  2 +
 src/com/android/camera/SettingsManager.java | 11 +++++
 src/com/android/camera/util/CameraUtil.java | 46 ++++++++++++++++++++-
 3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/src/com/android/camera/CaptureModule.java b/src/com/android/camera/CaptureModule.java
index d075ed10f..a225d727f 100755
--- a/src/com/android/camera/CaptureModule.java
+++ b/src/com/android/camera/CaptureModule.java
@@ -285,6 +285,8 @@ public class CaptureModule implements CameraModule, PhotoController,
             new CameraCharacteristics.Key<>("org.quic.camera2.customhfrfps.info.CustomHFRFpsTable", int[].class);
     public static final CameraCharacteristics.Key<int[]> sensorModeTable  =
             new CameraCharacteristics.Key<>("org.quic.camera2.sensormode.info.SensorModeTable", int[].class);
+    public static final CameraCharacteristics.Key<int[]> highSpeedVideoConfigs  =
+            new CameraCharacteristics.Key<>("android.control.availableHighSpeedVideoConfigurations", int[].class);
 
     public static final CaptureRequest.Key<Integer> sharpness_control = new CaptureRequest.Key<>(
             "org.codeaurora.qcamera3.sharpness.strength", Integer.class);
diff --git a/src/com/android/camera/SettingsManager.java b/src/com/android/camera/SettingsManager.java
index b9aca66fd..afb02eb89 100755
--- a/src/com/android/camera/SettingsManager.java
+++ b/src/com/android/camera/SettingsManager.java
@@ -504,6 +504,17 @@ public class SettingsManager implements ListMenu.SettingsListener {
         return sensorModeTable;
     }
 
+    public int[] getHighSpeedVideoConfigs(final int cameraId) {
+        int[] highSpeedVideoConfigs = null;
+        try {
+            highSpeedVideoConfigs = mCharacteristics.get(cameraId).get(
+                    CaptureModule.highSpeedVideoConfigs);
+        } catch (IllegalArgumentException exception) {
+            exception.printStackTrace();
+        }
+        return highSpeedVideoConfigs;
+    }
+
     public void registerListener(Listener listener) {
         mListeners.add(listener);
     }
diff --git a/src/com/android/camera/util/CameraUtil.java b/src/com/android/camera/util/CameraUtil.java
index 378d6633f..0704a90f9 100755
--- a/src/com/android/camera/util/CameraUtil.java
+++ b/src/com/android/camera/util/CameraUtil.java
@@ -1403,7 +1403,10 @@ public class CameraUtil {
         // Request list size: to limit the preview to 30fps, need use maxFps/30; to maximize
         // the preview frame rate, should use maxBatch size for that high speed stream
         // configuration. We choose the former for now.
-        int requestListSize = fpsRange.getUpper() / 30;
+        int requestListSize = getHighSpeedVideoConfigsLists((int)request.getTag());
+        if (requestListSize == -1) {
+            requestListSize = fpsRange.getUpper() / 30;
+        }
         List<CaptureRequest> requestList = new ArrayList<CaptureRequest>();
 
         // Prepare the Request builders: need carry over the request controls.
@@ -1471,6 +1474,47 @@ public class CameraUtil {
         return Collections.unmodifiableList(requestList);
     }
 
+    private static int getHighSpeedVideoConfigsLists(int cameraId) {
+        int optimalSizeIndex = -1;
+        SettingsManager settingsManager = SettingsManager.getInstance();
+        int[] table = settingsManager.getHighSpeedVideoConfigs(cameraId);
+        if (table == null) {
+            Log.w(TAG, " getHighSpeedVideoConfigsLists is  null");
+            return optimalSizeIndex;
+        }
+        String videoSizeString = settingsManager.getValue(SettingsManager.KEY_VIDEO_QUALITY);
+        if (videoSizeString == null) {
+            Log.w(TAG, " KEY_VIDEO_QUALITY is null");
+            return optimalSizeIndex;
+        }
+        android.util.Size videoSize = parsePictureSize(videoSizeString);
+        String rateValue = settingsManager.getValue(SettingsManager.KEY_VIDEO_HIGH_FRAME_RATE);
+        if (rateValue == null || rateValue.substring(0, 3).equals("off")) {
+            Log.w(TAG, " KEY_VIDEO_HIGH_FRAME_RATE is null");
+            return optimalSizeIndex;
+        }
+        int frameRate = Integer.parseInt(rateValue.substring(3));
+        for (int i = 0; i < table.length; i += 5) {
+            if (table[i] == videoSize.getWidth()
+                    && table[i + 1] == videoSize.getHeight()
+                    && (table[i + 2] == frameRate
+                    || table[i + 3] == frameRate)) {
+                if (i != table.length) {
+                    optimalSizeIndex = table[i + 4];
+                    return optimalSizeIndex;
+                }
+            }
+        }
+        return optimalSizeIndex;
+    }
+
+    private static android.util.Size parsePictureSize(String value) {
+        int indexX = value.indexOf('x');
+        int width = Integer.parseInt(value.substring(0, indexX));
+        int height = Integer.parseInt(value.substring(indexX + 1));
+        return new android.util.Size(width, height);
+    }
+
     private static CaptureRequest.Builder constructorCaptureRequestBuilder (
             CameraMetadataNative requestMetadata, boolean reprocess, int SESSION_ID_NONE,
             CaptureRequest request, Set<String> physicalCameraIdSet) {
-- 
GitLab