blob: c23cee457d30be497e02f3fec44c6281e99c42fa [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 Li35d2a702020-04-10 01:19:32 +0000112 enum StreamStateValues {
113 STOPPED,
114 RUNNING,
115 STOPPING,
116 DEAD,
117 };
Haoxiang Li35d2a702020-04-10 01:19:32 +0000118
Haoxiang Lib88cd3e2020-06-04 10:27:31 -0700119 // EVS Enumerator to control the start/stop of the Evs Stream
120 sp<IEvsEnumerator> mEvs;
121
122 // Instance and metadata for the opened Evs Camera
123 sp<IEvsCamera> mCamera;
124 CameraDesc mCameraDesc;
125
Haoxiang Li35d2a702020-04-10 01:19:32 +0000126 // Stream subscribed for the session.
127 sp<ISurroundViewStream> mStream GUARDED_BY(mAccessLock);
Haoxiang Lica7719a2020-06-03 20:51:11 -0700128 StreamStateValues mStreamState GUARDED_BY(mAccessLock);
Haoxiang Li35d2a702020-04-10 01:19:32 +0000129
Haoxiang Lica7719a2020-06-03 20:51:11 -0700130 thread mProcessThread; // The thread we'll use to process frames
Haoxiang Li35d2a702020-04-10 01:19:32 +0000131
Haoxiang Lib88cd3e2020-06-04 10:27:31 -0700132 // Reference to the inner class, to handle the incoming Evs frames
133 sp<FramesHandler> mFramesHandler;
134
Haoxiang Lica7719a2020-06-03 20:51:11 -0700135 // Used to signal a set of frames is ready
Haoxiang Lib88cd3e2020-06-04 10:27:31 -0700136 condition_variable mFramesSignal GUARDED_BY(mAccessLock);
Haoxiang Lica7719a2020-06-03 20:51:11 -0700137 bool mFramesAvailable GUARDED_BY(mAccessLock);
138
139 int mSequenceId;
Haoxiang Li35d2a702020-04-10 01:19:32 +0000140
141 struct FramesRecord {
142 SvFramesDesc frames;
143 bool inUse = false;
144 };
145
146 FramesRecord framesRecord GUARDED_BY(mAccessLock);
147
148 // Synchronization necessary to deconflict mCaptureThread from the main
149 // service thread
150 mutex mAccessLock;
151
152 vector<string> mEvsCameraIds GUARDED_BY(mAccessLock);
153
154 unique_ptr<SurroundView> mSurroundView GUARDED_BY(mAccessLock);
155
156 vector<SurroundViewInputBufferPointers>
157 mInputPointers GUARDED_BY(mAccessLock);
158 SurroundViewResultPointer mOutputPointer GUARDED_BY(mAccessLock);
Haoxiang Lica7719a2020-06-03 20:51:11 -0700159
160 Sv2dConfig mConfig GUARDED_BY(mAccessLock);
161 int mHeight GUARDED_BY(mAccessLock);
162
163 // TODO(b/158479099): Rename it to mMappingInfo
164 Sv2dMappingInfo mInfo GUARDED_BY(mAccessLock);
Haoxiang Li35d2a702020-04-10 01:19:32 +0000165 int mOutputWidth, mOutputHeight GUARDED_BY(mAccessLock);
166
167 sp<GraphicBuffer> mSvTexture GUARDED_BY(mAccessLock);
168
169 bool mIsInitialized GUARDED_BY(mAccessLock) = false;
170};
171
172} // namespace implementation
173} // namespace V1_0
174} // namespace sv
175} // namespace automotive
176} // namespace hardware
177} // namespace android
178