blob: 8d34a8730928e860463df5890b6d2d8a07764052 [file] [log] [blame]
Haoxiang Li35d2a702020-04-10 01:19:32 +00001/*
2 * Copyright 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
Haoxiang Lib88cd3e2020-06-04 10:27:31 -070019#include "CoreLibSetupHelper.h"
20
21#include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
22#include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h>
23#include <android/hardware/automotive/evs/1.1/IEvsEnumerator.h>
Haoxiang Li35d2a702020-04-10 01:19:32 +000024#include <android/hardware/automotive/sv/1.0/types.h>
25#include <android/hardware/automotive/sv/1.0/ISurroundViewStream.h>
26#include <android/hardware/automotive/sv/1.0/ISurroundView2dSession.h>
Haoxiang Lib88cd3e2020-06-04 10:27:31 -070027
Haoxiang Li35d2a702020-04-10 01:19:32 +000028#include <hidl/MQDescriptor.h>
29#include <hidl/Status.h>
30
Haoxiang Li35d2a702020-04-10 01:19:32 +000031#include <ui/GraphicBuffer.h>
32
Haoxiang Lib88cd3e2020-06-04 10:27:31 -070033#include <thread>
34
35using namespace ::android::hardware::automotive::evs::V1_1;
Haoxiang Li35d2a702020-04-10 01:19:32 +000036using namespace ::android::hardware::automotive::sv::V1_0;
Haoxiang Lib88cd3e2020-06-04 10:27:31 -070037using namespace ::android_auto::surround_view;
38
Haoxiang Li35d2a702020-04-10 01:19:32 +000039using ::android::hardware::Return;
40using ::android::hardware::hidl_vec;
41using ::android::sp;
Haoxiang Lica7719a2020-06-03 20:51:11 -070042using ::std::condition_variable;
Haoxiang Li35d2a702020-04-10 01:19:32 +000043
Haoxiang Lib88cd3e2020-06-04 10:27:31 -070044using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc;
45using BufferDesc_1_1 = ::android::hardware::automotive::evs::V1_1::BufferDesc;
Haoxiang Li35d2a702020-04-10 01:19:32 +000046
47namespace android {
48namespace hardware {
49namespace automotive {
50namespace sv {
51namespace V1_0 {
52namespace implementation {
53
54class SurroundView2dSession : public ISurroundView2dSession {
Haoxiang Lib88cd3e2020-06-04 10:27:31 -070055
56 /*
57 * FramesHandler:
58 * This class can be used to receive camera imagery from an IEvsCamera implementation. It will
59 * hold onto the most recent image buffer, returning older ones.
60 * Note that the video frames are delivered on a background thread, while the control interface
61 * is actuated from the applications foreground thread.
62 */
63 class FramesHandler : public IEvsCameraStream {
64 public:
65 FramesHandler(sp<IEvsCamera> pCamera, sp<SurroundView2dSession> pSession);
66
67 private:
68 // Implementation for ::android::hardware::automotive::evs::V1_0::IEvsCameraStream
69 Return<void> deliverFrame(const BufferDesc_1_0& buffer) override;
70
71 // Implementation for ::android::hardware::automotive::evs::V1_1::IEvsCameraStream
72 Return<void> deliverFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffer) override;
73 Return<void> notify(const EvsEventDesc& event) override;
74
75 // Values initialized as startup
76 sp <IEvsCamera> mCamera;
77
78 sp<SurroundView2dSession> mSession;
79 };
80
Haoxiang Li35d2a702020-04-10 01:19:32 +000081public:
Haoxiang Lib88cd3e2020-06-04 10:27:31 -070082 SurroundView2dSession(sp<IEvsEnumerator> pEvs);
83 ~SurroundView2dSession();
84 bool initialize();
Haoxiang Li35d2a702020-04-10 01:19:32 +000085
86 // Methods from ::android::hardware::automotive::sv::V1_0::ISurroundViewSession.
87 Return<SvResult> startStream(
88 const sp<ISurroundViewStream>& stream) override;
89 Return<void> stopStream() override;
90 Return<void> doneWithFrames(const SvFramesDesc& svFramesDesc) override;
91
92 // Methods from ISurroundView2dSession follow.
93 Return<void> get2dMappingInfo(get2dMappingInfo_cb _hidl_cb) override;
94 Return<SvResult> set2dConfig(const Sv2dConfig& sv2dConfig) override;
95 Return<void> get2dConfig(get2dConfig_cb _hidl_cb) override;
96 Return<void> projectCameraPoints(
97 const hidl_vec<Point2dInt>& points2dCamera,
98 const hidl_string& cameraId,
99 projectCameraPoints_cb _hidl_cb) override;
100
101private:
Haoxiang Lica7719a2020-06-03 20:51:11 -0700102 void processFrames();
103
Haoxiang Lib88cd3e2020-06-04 10:27:31 -0700104 // Set up and open the Evs camera(s), triggered when session is created.
105 bool setupEvs();
106
107 // Start Evs camera video stream, triggered when SV stream is started.
108 bool startEvs();
109
Haoxiang Lica7719a2020-06-03 20:51:11 -0700110 bool handleFrames(int sequenceId);
111
Haoxiang Lia091e4a2020-06-06 20:38:46 -0700112 bool copyFromBufferToPointers(BufferDesc_1_1 buffer,
113 SurroundViewInputBufferPointers pointers);
114
Haoxiang Li35d2a702020-04-10 01:19:32 +0000115 enum StreamStateValues {
116 STOPPED,
117 RUNNING,
118 STOPPING,
119 DEAD,
120 };
Haoxiang Li35d2a702020-04-10 01:19:32 +0000121
Haoxiang Lib88cd3e2020-06-04 10:27:31 -0700122 // EVS Enumerator to control the start/stop of the Evs Stream
123 sp<IEvsEnumerator> mEvs;
124
125 // Instance and metadata for the opened Evs Camera
126 sp<IEvsCamera> mCamera;
127 CameraDesc mCameraDesc;
Haoxiang Lia9d23d12020-06-13 18:09:13 -0700128 vector<SurroundViewCameraParams> mCameraParams;
Haoxiang Lib88cd3e2020-06-04 10:27:31 -0700129
Haoxiang Li35d2a702020-04-10 01:19:32 +0000130 // Stream subscribed for the session.
131 sp<ISurroundViewStream> mStream GUARDED_BY(mAccessLock);
Haoxiang Lica7719a2020-06-03 20:51:11 -0700132 StreamStateValues mStreamState GUARDED_BY(mAccessLock);
Haoxiang Li35d2a702020-04-10 01:19:32 +0000133
Haoxiang Lica7719a2020-06-03 20:51:11 -0700134 thread mProcessThread; // The thread we'll use to process frames
Haoxiang Li35d2a702020-04-10 01:19:32 +0000135
Haoxiang Lib88cd3e2020-06-04 10:27:31 -0700136 // Reference to the inner class, to handle the incoming Evs frames
137 sp<FramesHandler> mFramesHandler;
138
Haoxiang Lica7719a2020-06-03 20:51:11 -0700139 // Used to signal a set of frames is ready
Haoxiang Lib88cd3e2020-06-04 10:27:31 -0700140 condition_variable mFramesSignal GUARDED_BY(mAccessLock);
Haoxiang Lie3011cc2020-06-10 21:33:26 -0700141 bool mProcessingEvsFrames GUARDED_BY(mAccessLock);
Haoxiang Lica7719a2020-06-03 20:51:11 -0700142
143 int mSequenceId;
Haoxiang Li35d2a702020-04-10 01:19:32 +0000144
145 struct FramesRecord {
146 SvFramesDesc frames;
147 bool inUse = false;
148 };
149
Haoxiang Lie3011cc2020-06-10 21:33:26 -0700150 FramesRecord mFramesRecord GUARDED_BY(mAccessLock);
Haoxiang Li35d2a702020-04-10 01:19:32 +0000151
152 // Synchronization necessary to deconflict mCaptureThread from the main
153 // service thread
154 mutex mAccessLock;
155
156 vector<string> mEvsCameraIds GUARDED_BY(mAccessLock);
157
158 unique_ptr<SurroundView> mSurroundView GUARDED_BY(mAccessLock);
159
160 vector<SurroundViewInputBufferPointers>
161 mInputPointers GUARDED_BY(mAccessLock);
162 SurroundViewResultPointer mOutputPointer GUARDED_BY(mAccessLock);
Haoxiang Lica7719a2020-06-03 20:51:11 -0700163
164 Sv2dConfig mConfig GUARDED_BY(mAccessLock);
165 int mHeight GUARDED_BY(mAccessLock);
166
167 // TODO(b/158479099): Rename it to mMappingInfo
168 Sv2dMappingInfo mInfo GUARDED_BY(mAccessLock);
Haoxiang Li35d2a702020-04-10 01:19:32 +0000169 int mOutputWidth, mOutputHeight GUARDED_BY(mAccessLock);
170
171 sp<GraphicBuffer> mSvTexture GUARDED_BY(mAccessLock);
172
173 bool mIsInitialized GUARDED_BY(mAccessLock) = false;
174};
175
176} // namespace implementation
177} // namespace V1_0
178} // namespace sv
179} // namespace automotive
180} // namespace hardware
181} // namespace android
182