1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <android/hardware/automotive/sv/1.0/types.h>
20 #include <android/hardware/automotive/sv/1.0/ISurroundViewStream.h>
21 #include <android/hardware/automotive/sv/1.0/ISurroundView3dSession.h>
22 #include <hidl/MQDescriptor.h>
23 #include <hidl/Status.h>
24 
25 #include <thread>
26 
27 using namespace ::android::hardware::automotive::sv::V1_0;
28 using ::android::hardware::Return;
29 using ::android::hardware::Void;
30 using ::android::hardware::hidl_vec;
31 using ::android::sp;
32 using ::std::mutex;
33 
34 namespace android {
35 namespace hardware {
36 namespace automotive {
37 namespace sv {
38 namespace V1_0 {
39 namespace implementation {
40 
41 class SurroundView3dSession : public ISurroundView3dSession {
42 public:
43     SurroundView3dSession();
44 
45     // Methods from ::android::hardware::automotive::sv::V1_0::ISurroundViewSession.
46     Return<SvResult> startStream(
47         const sp<ISurroundViewStream>& stream) override;
48     Return<void> stopStream() override;
49     Return<void> doneWithFrames(const SvFramesDesc& svFramesDesc) override;
50 
51     // Methods from ISurroundView3dSession follow.
52     Return<SvResult> setViews(const hidl_vec<View3d>& views) override;
53     Return<SvResult> set3dConfig(const Sv3dConfig& sv3dConfig) override;
54     Return<void> get3dConfig(get3dConfig_cb _hidl_cb) override;
55     Return<SvResult>  updateOverlays(const OverlaysData& overlaysData);
56     Return<void> projectCameraPointsTo3dSurface(
57         const hidl_vec<Point2dInt>& cameraPoints,
58         const hidl_string& cameraId,
59         projectCameraPointsTo3dSurface_cb _hidl_cb);
60 
61     // Stream subscribed for the session.
62     // TODO(tanmayp): Make private and add set/get method.
63     sp<ISurroundViewStream> mStream;
64 
65 private:
66     void generateFrames();
67 
68     enum StreamStateValues {
69         STOPPED,
70         RUNNING,
71         STOPPING,
72         DEAD,
73     };
74     StreamStateValues mStreamState;
75 
76     std::thread mCaptureThread; // The thread we'll use to synthesize frames
77 
78     struct FramesRecord {
79         SvFramesDesc frames;
80         bool inUse = false;
81     };
82 
83     FramesRecord framesRecord;
84 
85     // Synchronization necessary to deconflict mCaptureThread from the main service thread
86     std::mutex mAccessLock;
87 
88     std::vector<View3d> mViews;
89 
90     Sv3dConfig mConfig;
91 
92     std::vector<std::string> mEvsCameraIds;
93 };
94 
95 }  // namespace implementation
96 }  // namespace V1_0
97 }  // namespace sv
98 }  // namespace automotive
99 }  // namespace hardware
100 }  // namespace android
101