Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 1 | /* |
| 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 Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 19 | #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 Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 24 | #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 Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 27 | |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 28 | #include <hidl/MQDescriptor.h> |
| 29 | #include <hidl/Status.h> |
| 30 | |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 31 | #include <ui/GraphicBuffer.h> |
| 32 | |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 33 | #include <thread> |
| 34 | |
| 35 | using namespace ::android::hardware::automotive::evs::V1_1; |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 36 | using namespace ::android::hardware::automotive::sv::V1_0; |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 37 | using namespace ::android_auto::surround_view; |
| 38 | |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 39 | using ::android::hardware::Return; |
| 40 | using ::android::hardware::hidl_vec; |
| 41 | using ::android::sp; |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 42 | using ::std::condition_variable; |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 43 | |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 44 | using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc; |
| 45 | using BufferDesc_1_1 = ::android::hardware::automotive::evs::V1_1::BufferDesc; |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 46 | |
| 47 | namespace android { |
| 48 | namespace hardware { |
| 49 | namespace automotive { |
| 50 | namespace sv { |
| 51 | namespace V1_0 { |
| 52 | namespace implementation { |
| 53 | |
| 54 | class SurroundView2dSession : public ISurroundView2dSession { |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 55 | |
| 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 Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 81 | public: |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 82 | SurroundView2dSession(sp<IEvsEnumerator> pEvs); |
| 83 | ~SurroundView2dSession(); |
| 84 | bool initialize(); |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 85 | |
| 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 | |
| 101 | private: |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 102 | void processFrames(); |
| 103 | |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 104 | // 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 Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 110 | bool handleFrames(int sequenceId); |
| 111 | |
Haoxiang Li | a091e4a | 2020-06-06 20:38:46 -0700 | [diff] [blame] | 112 | bool copyFromBufferToPointers(BufferDesc_1_1 buffer, |
| 113 | SurroundViewInputBufferPointers pointers); |
| 114 | |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 115 | enum StreamStateValues { |
| 116 | STOPPED, |
| 117 | RUNNING, |
| 118 | STOPPING, |
| 119 | DEAD, |
| 120 | }; |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 121 | |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 122 | // 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; |
| 128 | |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 129 | // Stream subscribed for the session. |
| 130 | sp<ISurroundViewStream> mStream GUARDED_BY(mAccessLock); |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 131 | StreamStateValues mStreamState GUARDED_BY(mAccessLock); |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 132 | |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 133 | thread mProcessThread; // The thread we'll use to process frames |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 134 | |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 135 | // Reference to the inner class, to handle the incoming Evs frames |
| 136 | sp<FramesHandler> mFramesHandler; |
| 137 | |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 138 | // Used to signal a set of frames is ready |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 139 | condition_variable mFramesSignal GUARDED_BY(mAccessLock); |
Haoxiang Li | e3011cc | 2020-06-10 21:33:26 -0700 | [diff] [blame^] | 140 | bool mProcessingEvsFrames GUARDED_BY(mAccessLock); |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 141 | |
| 142 | int mSequenceId; |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 143 | |
| 144 | struct FramesRecord { |
| 145 | SvFramesDesc frames; |
| 146 | bool inUse = false; |
| 147 | }; |
| 148 | |
Haoxiang Li | e3011cc | 2020-06-10 21:33:26 -0700 | [diff] [blame^] | 149 | FramesRecord mFramesRecord GUARDED_BY(mAccessLock); |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 150 | |
| 151 | // Synchronization necessary to deconflict mCaptureThread from the main |
| 152 | // service thread |
| 153 | mutex mAccessLock; |
| 154 | |
| 155 | vector<string> mEvsCameraIds GUARDED_BY(mAccessLock); |
| 156 | |
| 157 | unique_ptr<SurroundView> mSurroundView GUARDED_BY(mAccessLock); |
| 158 | |
| 159 | vector<SurroundViewInputBufferPointers> |
| 160 | mInputPointers GUARDED_BY(mAccessLock); |
| 161 | SurroundViewResultPointer mOutputPointer GUARDED_BY(mAccessLock); |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 162 | |
| 163 | Sv2dConfig mConfig GUARDED_BY(mAccessLock); |
| 164 | int mHeight GUARDED_BY(mAccessLock); |
| 165 | |
| 166 | // TODO(b/158479099): Rename it to mMappingInfo |
| 167 | Sv2dMappingInfo mInfo GUARDED_BY(mAccessLock); |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 168 | int mOutputWidth, mOutputHeight GUARDED_BY(mAccessLock); |
| 169 | |
| 170 | sp<GraphicBuffer> mSvTexture GUARDED_BY(mAccessLock); |
| 171 | |
| 172 | bool mIsInitialized GUARDED_BY(mAccessLock) = false; |
| 173 | }; |
| 174 | |
| 175 | } // namespace implementation |
| 176 | } // namespace V1_0 |
| 177 | } // namespace sv |
| 178 | } // namespace automotive |
| 179 | } // namespace hardware |
| 180 | } // namespace android |
| 181 | |