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 | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 112 | enum StreamStateValues { |
| 113 | STOPPED, |
| 114 | RUNNING, |
| 115 | STOPPING, |
| 116 | DEAD, |
| 117 | }; |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 118 | |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame^] | 119 | // 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 Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 126 | // Stream subscribed for the session. |
| 127 | sp<ISurroundViewStream> mStream GUARDED_BY(mAccessLock); |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 128 | StreamStateValues mStreamState GUARDED_BY(mAccessLock); |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 129 | |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 130 | thread mProcessThread; // The thread we'll use to process frames |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 131 | |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame^] | 132 | // Reference to the inner class, to handle the incoming Evs frames |
| 133 | sp<FramesHandler> mFramesHandler; |
| 134 | |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 135 | // Used to signal a set of frames is ready |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame^] | 136 | condition_variable mFramesSignal GUARDED_BY(mAccessLock); |
Haoxiang Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 137 | bool mFramesAvailable GUARDED_BY(mAccessLock); |
| 138 | |
| 139 | int mSequenceId; |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 140 | |
| 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 Li | ca7719a | 2020-06-03 20:51:11 -0700 | [diff] [blame] | 159 | |
| 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 Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 165 | 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 | |