Apply EVS multiple camera logic to SV 2d session

Apply EVS multiple camera logic into Surround View 2d Session, and
remove the mCaptureThread that was used to set up frame delay
explicitly.

Bug: 157498592
Test: Manually launched SV service and app on Osprey
Change-Id: Ia414fecd8f09ec08e70ebf1b8793b54c99aef560
diff --git a/surround_view/service-impl/SurroundView2dSession.h b/surround_view/service-impl/SurroundView2dSession.h
index b0dcec7..c23cee4 100644
--- a/surround_view/service-impl/SurroundView2dSession.h
+++ b/surround_view/service-impl/SurroundView2dSession.h
@@ -16,24 +16,33 @@
 
 #pragma once
 
+#include "CoreLibSetupHelper.h"
+
+#include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
+#include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h>
+#include <android/hardware/automotive/evs/1.1/IEvsEnumerator.h>
 #include <android/hardware/automotive/sv/1.0/types.h>
 #include <android/hardware/automotive/sv/1.0/ISurroundViewStream.h>
 #include <android/hardware/automotive/sv/1.0/ISurroundView2dSession.h>
+
 #include <hidl/MQDescriptor.h>
 #include <hidl/Status.h>
 
-#include "CoreLibSetupHelper.h"
-#include <thread>
-
 #include <ui/GraphicBuffer.h>
 
+#include <thread>
+
+using namespace ::android::hardware::automotive::evs::V1_1;
 using namespace ::android::hardware::automotive::sv::V1_0;
+using namespace ::android_auto::surround_view;
+
 using ::android::hardware::Return;
 using ::android::hardware::hidl_vec;
 using ::android::sp;
 using ::std::condition_variable;
 
-using namespace android_auto::surround_view;
+using BufferDesc_1_0  = ::android::hardware::automotive::evs::V1_0::BufferDesc;
+using BufferDesc_1_1  = ::android::hardware::automotive::evs::V1_1::BufferDesc;
 
 namespace android {
 namespace hardware {
@@ -43,8 +52,36 @@
 namespace implementation {
 
 class SurroundView2dSession : public ISurroundView2dSession {
+
+    /*
+     * FramesHandler:
+     * This class can be used to receive camera imagery from an IEvsCamera implementation.  It will
+     * hold onto the most recent image buffer, returning older ones.
+     * Note that the video frames are delivered on a background thread, while the control interface
+     * is actuated from the applications foreground thread.
+     */
+    class FramesHandler : public IEvsCameraStream {
+    public:
+        FramesHandler(sp<IEvsCamera> pCamera, sp<SurroundView2dSession> pSession);
+
+    private:
+        // Implementation for ::android::hardware::automotive::evs::V1_0::IEvsCameraStream
+        Return<void> deliverFrame(const BufferDesc_1_0& buffer) override;
+
+        // Implementation for ::android::hardware::automotive::evs::V1_1::IEvsCameraStream
+        Return<void> deliverFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffer) override;
+        Return<void> notify(const EvsEventDesc& event) override;
+
+        // Values initialized as startup
+        sp <IEvsCamera> mCamera;
+
+        sp<SurroundView2dSession> mSession;
+    };
+
 public:
-    SurroundView2dSession();
+    SurroundView2dSession(sp<IEvsEnumerator> pEvs);
+    ~SurroundView2dSession();
+    bool initialize();
 
     // Methods from ::android::hardware::automotive::sv::V1_0::ISurroundViewSession.
     Return<SvResult> startStream(
@@ -62,11 +99,14 @@
         projectCameraPoints_cb _hidl_cb) override;
 
 private:
-    bool initialize();
-
-    void generateFrames();
     void processFrames();
 
+    // Set up and open the Evs camera(s), triggered when session is created.
+    bool setupEvs();
+
+    // Start Evs camera video stream, triggered when SV stream is started.
+    bool startEvs();
+
     bool handleFrames(int sequenceId);
 
     enum StreamStateValues {
@@ -76,15 +116,24 @@
         DEAD,
     };
 
+    // EVS Enumerator to control the start/stop of the Evs Stream
+    sp<IEvsEnumerator> mEvs;
+
+    // Instance and metadata for the opened Evs Camera
+    sp<IEvsCamera> mCamera;
+    CameraDesc mCameraDesc;
+
     // Stream subscribed for the session.
     sp<ISurroundViewStream> mStream GUARDED_BY(mAccessLock);
     StreamStateValues mStreamState GUARDED_BY(mAccessLock);
 
-    thread mCaptureThread; // The thread we'll use to synthesize frames
     thread mProcessThread; // The thread we'll use to process frames
 
+    // Reference to the inner class, to handle the incoming Evs frames
+    sp<FramesHandler> mFramesHandler;
+
     // Used to signal a set of frames is ready
-    condition_variable mSignal GUARDED_BY(mAccessLock);
+    condition_variable mFramesSignal GUARDED_BY(mAccessLock);
     bool mFramesAvailable GUARDED_BY(mAccessLock);
 
     int mSequenceId;