Camera: Insert default RAW boost if needed
am: a13ac99f34

Change-Id: Iee5e983c5853ece9b6f059a141c309e6a1a0f4f4
diff --git a/camera/device/3.2/default/CameraDeviceSession.cpp b/camera/device/3.2/default/CameraDeviceSession.cpp
index e9f78ff..b718723 100644
--- a/camera/device/3.2/default/CameraDeviceSession.cpp
+++ b/camera/device/3.2/default/CameraDeviceSession.cpp
@@ -46,6 +46,7 @@
         mDevice(device),
         mDeviceVersion(device->common.version),
         mIsAELockAvailable(false),
+        mDerivePostRawSensKey(false),
         mNumPartialResults(1),
         mResultBatcher(callback) {
 
@@ -64,6 +65,13 @@
                 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
     }
 
+    // Determine whether we need to derive sensitivity boost values for older devices.
+    // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
+    // be listed (as the default value 100)
+    if (mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
+        mDerivePostRawSensKey = true;
+    }
+
     mInitFail = initialize();
 }
 
@@ -692,7 +700,22 @@
                   __FUNCTION__, type);
             status = Status::ILLEGAL_ARGUMENT;
         } else {
-            convertToHidl(rawRequest, &outMetadata);
+            mOverridenRequest.clear();
+            mOverridenRequest.append(rawRequest);
+            // Derive some new keys for backward compatibility
+            if (mDerivePostRawSensKey && !mOverridenRequest.exists(
+                    ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
+                int32_t defaultBoost[1] = {100};
+                mOverridenRequest.update(
+                        ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
+                        defaultBoost, 1);
+                const camera_metadata_t *metaBuffer =
+                        mOverridenRequest.getAndLock();
+                convertToHidl(metaBuffer, &outMetadata);
+                mOverridenRequest.unlock(metaBuffer);
+            } else {
+                convertToHidl(rawRequest, &outMetadata);
+            }
         }
     }
     _hidl_cb(status, outMetadata);
@@ -752,6 +775,14 @@
         return Void();
     }
 
+    if (!mInflightRawBoostPresent.empty()) {
+        ALOGE("%s: trying to configureStreams while there are still %zu inflight"
+                " boost overrides!", __FUNCTION__,
+                mInflightRawBoostPresent.size());
+        _hidl_cb(Status::INTERNAL_ERROR, outStreams);
+        return Void();
+    }
+
     if (status != Status::OK) {
         _hidl_cb(status, outStreams);
         return Void();
@@ -1051,6 +1082,11 @@
                         "trigger overrides!", __FUNCTION__,
                         mInflightAETriggerOverrides.size());
             }
+            if (!mInflightRawBoostPresent.empty()) {
+                ALOGE("%s: trying to close while there are still %zu inflight "
+                        " RAW boost overrides!", __FUNCTION__,
+                        mInflightRawBoostPresent.size());
+            }
 
         }
 
@@ -1116,20 +1152,59 @@
     result.partialResult = hal_result->partial_result;
     convertToHidl(hal_result->result, &result.result);
     if (nullptr != hal_result->result) {
+        bool resultOverriden = false;
         Mutex::Autolock _l(d->mInflightLock);
+
+        // Derive some new keys for backward compatibility
+        if (d->mDerivePostRawSensKey) {
+            camera_metadata_ro_entry entry;
+            if (find_camera_metadata_ro_entry(hal_result->result,
+                    ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST, &entry) == 0) {
+                d->mInflightRawBoostPresent[frameNumber] = true;
+            } else {
+                auto entry = d->mInflightRawBoostPresent.find(frameNumber);
+                if (d->mInflightRawBoostPresent.end() == entry) {
+                    d->mInflightRawBoostPresent[frameNumber] = false;
+                }
+            }
+
+            if ((hal_result->partial_result == d->mNumPartialResults)) {
+                if (!d->mInflightRawBoostPresent[frameNumber]) {
+                    if (!resultOverriden) {
+                        d->mOverridenResult.clear();
+                        d->mOverridenResult.append(hal_result->result);
+                        resultOverriden = true;
+                    }
+                    int32_t defaultBoost[1] = {100};
+                    d->mOverridenResult.update(
+                            ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
+                            defaultBoost, 1);
+                }
+
+                d->mInflightRawBoostPresent.erase(frameNumber);
+            }
+        }
+
         auto entry = d->mInflightAETriggerOverrides.find(frameNumber);
         if (d->mInflightAETriggerOverrides.end() != entry) {
-            d->mOverridenResult.clear();
-            d->mOverridenResult.append(hal_result->result);
+            if (!resultOverriden) {
+                d->mOverridenResult.clear();
+                d->mOverridenResult.append(hal_result->result);
+                resultOverriden = true;
+            }
             d->overrideResultForPrecaptureCancelLocked(entry->second,
                     &d->mOverridenResult);
-            const camera_metadata_t *metaBuffer = d->mOverridenResult.getAndLock();
-            convertToHidl(metaBuffer, &result.result);
-            d->mOverridenResult.unlock(metaBuffer);
             if (hal_result->partial_result == d->mNumPartialResults) {
                 d->mInflightAETriggerOverrides.erase(frameNumber);
             }
         }
+
+        if (resultOverriden) {
+            const camera_metadata_t *metaBuffer =
+                    d->mOverridenResult.getAndLock();
+            convertToHidl(metaBuffer, &result.result);
+            d->mOverridenResult.unlock(metaBuffer);
+        }
     }
     if (hasInputBuf) {
         result.inputBuffer.streamId =
@@ -1219,6 +1294,14 @@
                     d->mInflightAETriggerOverrides.erase(
                             hidlMsg.msg.error.frameNumber);
                 }
+
+                auto boostEntry = d->mInflightRawBoostPresent.find(
+                        hidlMsg.msg.error.frameNumber);
+                if (d->mInflightRawBoostPresent.end() != boostEntry) {
+                    d->mInflightRawBoostPresent.erase(
+                            hidlMsg.msg.error.frameNumber);
+                }
+
             }
                 break;
             case ErrorCode::ERROR_BUFFER:
diff --git a/camera/device/3.2/default/CameraDeviceSession.h b/camera/device/3.2/default/CameraDeviceSession.h
index c08ed90..bbf39e6 100644
--- a/camera/device/3.2/default/CameraDeviceSession.h
+++ b/camera/device/3.2/default/CameraDeviceSession.h
@@ -121,6 +121,7 @@
     camera3_device_t* mDevice;
     uint32_t mDeviceVersion;
     bool mIsAELockAvailable;
+    bool mDerivePostRawSensKey;
     uint32_t mNumPartialResults;
     // Stream ID -> Camera3Stream cache
     std::map<int, Camera3Stream> mStreamMap;
@@ -132,6 +133,8 @@
     // (frameNumber, AETriggerOverride) -> inflight request AETriggerOverrides
     std::map<uint32_t, AETriggerCancelOverride> mInflightAETriggerOverrides;
     ::android::hardware::camera::common::V1_0::helper::CameraMetadata mOverridenResult;
+    std::map<uint32_t, bool> mInflightRawBoostPresent;
+    ::android::hardware::camera::common::V1_0::helper::CameraMetadata mOverridenRequest;
 
     // buffers currently ciculating between HAL and camera service
     // key: bufferId sent via HIDL interface