Fixes setViews to be set from sv_app

- Fixes Sv Service which was hardcoding the views
- Adds a list of quat and translations view in the sv_app to set.
- Behaviour should be the same as before.

Bug: 160028656
Bug: 162599435
Test: mm -j
Change-Id: I85d7a21aa3a956b143c5f55f181d3f304815d04c
Merged-In: I85d7a21aa3a956b143c5f55f181d3f304815d04c
diff --git a/surround_view/app/sv_app.cpp b/surround_view/app/sv_app.cpp
index 2fe0c86..d697c1e 100644
--- a/surround_view/app/sv_app.cpp
+++ b/surround_view/app/sv_app.cpp
@@ -50,6 +50,53 @@
     DEMO_3D,
 };
 
+const float kHorizontalFov = 90;
+
+// Number of views to generate.
+const uint32_t kPoseCount = 16;
+
+// Set of pose rotations expressed in quaternions.
+// Views are generated about a circle at a height about the car, point towards the center.
+const float kPoseRot[kPoseCount][4] = {
+    {-0.251292, -0.251292, -0.660948, 0.660948},
+    {0.197439, 0.295488, 0.777193, -0.519304},
+    {0.135998, 0.328329, 0.86357, -0.357702},
+    {0.0693313, 0.348552, 0.916761, -0.182355},
+    {-7.76709e-09, 0.355381, 0.934722, 2.0429e-08},
+    {-0.0693313, 0.348552, 0.916761, 0.182355},
+    {-0.135998, 0.328329, 0.86357, 0.357702},
+    {-0.197439, 0.295488, 0.777193, 0.519304},
+    {-0.251292, 0.251292, 0.660948, 0.660948},
+    {-0.295488, 0.197439, 0.519304, 0.777193},
+    {-0.328329, 0.135998, 0.357702, 0.86357},
+    {-0.348552, 0.0693313, 0.182355, 0.916761},
+    {-0.355381, -2.11894e-09, -5.57322e-09, 0.934722},
+    {-0.348552, -0.0693313, -0.182355, 0.916761},
+    {-0.328329, -0.135998, -0.357702, 0.86357},
+    {-0.295488, -0.197439, -0.519304, 0.777193}
+};
+
+// Set of pose translations i.e. positions of the views.
+// Views are generated about a circle at a height about the car, point towards the center.
+const float kPoseTrans[kPoseCount][4] = {
+    {4, 0, 2.5},
+    {3.69552, 1.53073, 2.5},
+    {2.82843, 2.82843, 2.5},
+    {1.53073, 3.69552, 2.5},
+    {-1.74846e-07, 4, 2.5},
+    {-1.53073, 3.69552, 2.5},
+    {-2.82843, 2.82843, 2.5},
+    {-3.69552, 1.53073, 2.5},
+    {-4, -3.49691e-07, 2.5},
+    {-3.69552, -1.53073, 2.5},
+    {-2.82843, -2.82843, 2.5},
+    {-1.53073, -3.69552, 2.5},
+    {4.76995e-08, -4, 2.5},
+    {1.53073, -3.69552, 2.5},
+    {2.82843, -2.82843, 2.5},
+    {3.69552, -1.53073, 2.5}
+};
+
 bool run2dSurroundView(sp<ISurroundViewService> pSurroundViewService,
                        sp<IEvsDisplay> pDisplay) {
     LOG(INFO) << "Run 2d Surround View demo";
@@ -108,6 +155,28 @@
     return true;
 };
 
+// Given a valid sv 3d session and pose, viewid and hfov parameters, sets the view.
+bool setView(sp<ISurroundView3dSession> surroundView3dSession, uint32_t viewId,
+        uint32_t poseIndex, float hfov)
+{
+    const View3d view3d = {
+        .viewId = viewId,
+        .pose = {
+            .rotation = {.x=kPoseRot[poseIndex][0], .y=kPoseRot[poseIndex][1],
+                    .z=kPoseRot[poseIndex][2], .w=kPoseRot[poseIndex][3]},
+            .translation = {.x=kPoseTrans[poseIndex][0], .y=kPoseTrans[poseIndex][1],
+                    .z=kPoseTrans[poseIndex][2]},
+        },
+        .horizontalFov = hfov,
+    };
+
+    const std::vector<View3d> views = {view3d};
+    if (surroundView3dSession->setViews(views) != SvResult::OK) {
+        return false;
+    }
+    return true;
+}
+
 bool run3dSurroundView(sp<ISurroundViewService> pSurroundViewService,
                        sp<IEvsDisplay> pDisplay) {
     LOG(INFO) << "Run 3d Surround View demo";
@@ -130,16 +199,6 @@
         LOG(INFO) << "start3dSession succeeded";
     }
 
-    // TODO(b/150412555): now we have the dummy view here since the views are
-    // set in service. This should be fixed.
-    std::vector<View3d> singleView(1);
-    surroundView3dSession->setViews(singleView);
-
-    if (surroundView3dSession->setViews(singleView) != SvResult::OK) {
-        LOG(ERROR) << "Failed to setViews";
-        return false;
-    }
-
     sp<SurroundViewServiceCallback> sv3dCallback
         = new SurroundViewServiceCallback(pDisplay, surroundView3dSession);
 
@@ -150,7 +209,14 @@
     }
 
     // Let the SV algorithm run for 10 seconds for HIGH_QUALITY
-    std::this_thread::sleep_for(std::chrono::seconds(10));
+    const int totalViewingTimeSecs = 10;
+    const std::chrono::milliseconds perPoseSleepTimeMs(totalViewingTimeSecs * 1000 / kPoseCount);
+    for(uint32_t i = 0; i < kPoseCount; i++) {
+        if (!setView(surroundView3dSession, i, i, kHorizontalFov)) {
+            LOG(WARNING) << "Failed to setView of pose index :" << i;
+        }
+        std::this_thread::sleep_for(perPoseSleepTimeMs);
+    }
 
     // Switch to low quality and lower resolution
     Sv3dConfig config;
@@ -163,7 +229,12 @@
     }
 
     // Let the SV algorithm run for 10 seconds for LOW_QUALITY
-    std::this_thread::sleep_for(std::chrono::seconds(10));
+    for(uint32_t i = 0; i < kPoseCount; i++) {
+        if(!setView(surroundView3dSession, i + kPoseCount, i, kHorizontalFov)) {
+            LOG(WARNING) << "Failed to setView of pose index :" << i;
+        }
+        std::this_thread::sleep_for(perPoseSleepTimeMs);
+    }
 
     // TODO(b/150412555): wait for the last frame
     // Stop the 3d stream and session
diff --git a/surround_view/service-impl/SurroundView3dSession.cpp b/surround_view/service-impl/SurroundView3dSession.cpp
index 2c7861d..3453190 100644
--- a/surround_view/service-impl/SurroundView3dSession.cpp
+++ b/surround_view/service-impl/SurroundView3dSession.cpp
@@ -417,6 +417,11 @@
     LOG(DEBUG) << __FUNCTION__;
     scoped_lock <mutex> lock(mAccessLock);
 
+    if (views.size() == 0) {
+        LOG(ERROR) << "Empty view argument, at-least one view is required.";
+        return SvResult::VIEW_NOT_SET;
+    }
+
     mViews.resize(views.size());
     for (int i=0; i<views.size(); i++) {
         mViews[i] = views[i];
@@ -676,18 +681,6 @@
         }
     }
 
-    // TODO(b/150412555): do not use the setViews for frames generation
-    // since there is a discrepancy between the HIDL APIs and core lib APIs.
-    array<array<float, 4>, 4> matrix;
-
-    // TODO(b/150412555): use hard-coded views for now. Change view every
-    // frame.
-    int recViewId = sequenceId % 16;
-    for (int i=0; i<4; i++)
-        for (int j=0; j<4; j++) {
-            matrix[i][j] = kRecViews[recViewId][i*4+j];
-    }
-
     // Get the latest VHal property values
     if (mVhalHandler != nullptr) {
         if (!mVhalHandler->getPropertyValues(&mPropertyValues)) {
@@ -710,8 +703,16 @@
         LOG(INFO) << "AnimationParams is empty. Ignored";
     }
 
+    // Get the view.
+    // TODO(161399517): Only single view is currently supported, add support for multiple views.
+    const View3d view3d = mViews[0];
+    const RotationQuat quat = view3d.pose.rotation;
+    const Translation trans = view3d.pose.translation;
+    const std::array<float, 4> viewQuaternion = {quat.x, quat.y, quat.z, quat.w};
+    const std::array<float, 3> viewTranslation = {trans.x, trans.y, trans.z};
+
     if (mSurroundView->Get3dSurroundView(
-        mInputPointers, matrix, &mOutputPointer)) {
+            mInputPointers, viewQuaternion, viewTranslation, &mOutputPointer)) {
         LOG(INFO) << "Get3dSurroundView succeeded";
     } else {
         LOG(ERROR) << "Get3dSurroundView failed. "
diff --git a/surround_view/service-impl/SurroundViewSessionTests.cpp b/surround_view/service-impl/SurroundViewSessionTests.cpp
index 2ad6d49..a6abd04 100644
--- a/surround_view/service-impl/SurroundViewSessionTests.cpp
+++ b/surround_view/service-impl/SurroundViewSessionTests.cpp
@@ -83,7 +83,14 @@
     sp<MockSurroundViewCallback> sv3dCallback =
             new MockSurroundViewCallback(sv3dSession);
 
-    View3d view = {};
+    View3d view = {
+        .viewId = 0,
+        .pose = {
+            .rotation = {.x=0, .y=0, .z=0, .w=1.0f},
+            .translation = {.x=0, .y=0, .z=0},
+        },
+        .horizontalFov = 90,
+    };
     vector<View3d> views;
     views.emplace_back(view);
     sv3dSession->setViews(views);