Snap for 4829593 from 29d1cc0fe6c2cc5f97e9d77e21f47df58544340c to pi-release

Change-Id: I14980b0e8e9a93c336b8be0abc5bca7cd2c31f9e
diff --git a/services/camera/libcameraservice/device3/DistortionMapper.cpp b/services/camera/libcameraservice/device3/DistortionMapper.cpp
index 9229079..eef6658 100644
--- a/services/camera/libcameraservice/device3/DistortionMapper.cpp
+++ b/services/camera/libcameraservice/device3/DistortionMapper.cpp
@@ -18,6 +18,7 @@
 #define ATRACE_TAG ATRACE_TAG_CAMERA
 //#define LOG_NDEBUG 0
 
+#include <algorithm>
 #include <cmath>
 
 #include "device3/DistortionMapper.h"
@@ -43,13 +44,13 @@
 };
 
 // Only for capture result
-constexpr std::array<uint32_t, 2> DistortionMapper::kResultRectsToCorrect = {
+constexpr std::array<uint32_t, 1> DistortionMapper::kResultRectsToCorrect = {
     ANDROID_SCALER_CROP_REGION,
-    ANDROID_STATISTICS_FACE_RECTANGLES
 };
 
 // Only for capture result
-constexpr std::array<uint32_t, 1> DistortionMapper::kResultPointsToCorrect = {
+constexpr std::array<uint32_t, 2> DistortionMapper::kResultPointsToCorrect = {
+    ANDROID_STATISTICS_FACE_RECTANGLES, // Says rectangles, is really points
     ANDROID_STATISTICS_FACE_LANDMARKS,
 };
 
@@ -81,6 +82,10 @@
     mArrayWidth = array.data.i32[2];
     mArrayHeight = array.data.i32[3];
 
+    array = deviceInfo.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
+    mActiveWidth = array.data.i32[2];
+    mActiveHeight = array.data.i32[3];
+
     return updateCalibration(deviceInfo);
 }
 
@@ -102,8 +107,21 @@
         for (auto region : kMeteringRegionsToCorrect) {
             e = request->find(region);
             for (size_t j = 0; j < e.count; j += 5) {
+                int32_t weight = e.data.i32[j + 4];
+                if (weight == 0) {
+                    continue;
+                }
                 res = mapCorrectedToRaw(e.data.i32 + j, 2);
                 if (res != OK) return res;
+                for (size_t k = 0; k < 4; k+=2) {
+                    int32_t& x = e.data.i32[j + k];
+                    int32_t& y = e.data.i32[j + k + 1];
+                    // Clamp to within active array
+                    x = std::max(0, x);
+                    x = std::min(mActiveWidth - 1, x);
+                    y = std::max(0, y);
+                    y = std::min(mActiveHeight - 1, y);
+                }
             }
         }
         for (auto rect : kRequestRectsToCorrect) {
@@ -134,8 +152,21 @@
         for (auto region : kMeteringRegionsToCorrect) {
             e = result->find(region);
             for (size_t j = 0; j < e.count; j += 5) {
+                int32_t weight = e.data.i32[j + 4];
+                if (weight == 0) {
+                    continue;
+                }
                 res = mapRawToCorrected(e.data.i32 + j, 2);
                 if (res != OK) return res;
+                for (size_t k = 0; k < 4; k+=2) {
+                    int32_t& x = e.data.i32[j + k];
+                    int32_t& y = e.data.i32[j + k + 1];
+                    // Clamp to within active array
+                    x = std::max(0, x);
+                    x = std::min(mActiveWidth - 1, x);
+                    y = std::max(0, y);
+                    y = std::min(mActiveHeight - 1, y);
+                }
             }
         }
         for (auto rect : kResultRectsToCorrect) {
@@ -212,7 +243,8 @@
     for (int i = 0; i < coordCount * 2; i += 2) {
         const GridQuad *quad = findEnclosingQuad(coordPairs + i, mDistortedGrid);
         if (quad == nullptr) {
-            ALOGE("Raw to corrected mapping failure: No quad found");
+            ALOGE("Raw to corrected mapping failure: No quad found for (%d, %d)",
+                    *(coordPairs + i), *(coordPairs + i + 1));
             return INVALID_OPERATION;
         }
         ALOGV("src xy: %d, %d, enclosing quad: (%f, %f), (%f, %f), (%f, %f), (%f, %f)",
diff --git a/services/camera/libcameraservice/device3/DistortionMapper.h b/services/camera/libcameraservice/device3/DistortionMapper.h
index c6d715b..00cbd32 100644
--- a/services/camera/libcameraservice/device3/DistortionMapper.h
+++ b/services/camera/libcameraservice/device3/DistortionMapper.h
@@ -148,10 +148,10 @@
     static const std::array<uint32_t, 1> kRequestRectsToCorrect;
 
     // Only capture result
-    static const std::array<uint32_t, 2> kResultRectsToCorrect;
+    static const std::array<uint32_t, 1> kResultRectsToCorrect;
 
     // Only for capture results
-    static const std::array<uint32_t, 1> kResultPointsToCorrect;
+    static const std::array<uint32_t, 2> kResultPointsToCorrect;
 
     // Utility to create reverse mapping grids
     status_t buildGrids();
@@ -169,6 +169,8 @@
 
     // pre-correction active array dimensions
     int mArrayWidth, mArrayHeight;
+    // active array dimensions
+    int mActiveWidth, mActiveHeight;
 
     std::vector<GridQuad> mCorrectedGrid;
     std::vector<GridQuad> mDistortedGrid;