1 /* 2 * Copyright (C) 2016 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 #ifndef ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSV4LCAMERA_H 18 #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSV4LCAMERA_H 19 20 #include "VideoCapture.h" 21 #include "ConfigManager.h" 22 23 #include <functional> 24 #include <thread> 25 26 #include <android/hardware/automotive/evs/1.1/types.h> 27 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h> 28 #include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h> 29 #include <android/hardware/automotive/evs/1.1/IEvsDisplay.h> 30 #include <android/hardware/camera/device/3.2/ICameraDevice.h> 31 #include <android-base/result.h> 32 #include <ui/GraphicBuffer.h> 33 34 using ::android::hardware::hidl_string; 35 using ::android::hardware::camera::device::V3_2::Stream; 36 using ::android::hardware::automotive::evs::V1_0::EvsResult; 37 using ::android::hardware::automotive::evs::V1_0::CameraDesc; 38 using IEvsDisplay_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsDisplay; 39 using IEvsDisplay_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsDisplay; 40 using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc; 41 using BufferDesc_1_1 = ::android::hardware::automotive::evs::V1_1::BufferDesc; 42 using IEvsCameraStream_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsCameraStream; 43 using IEvsCameraStream_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCameraStream; 44 45 namespace android { 46 namespace hardware { 47 namespace automotive { 48 namespace evs { 49 namespace V1_1 { 50 namespace implementation { 51 52 53 // From EvsEnumerator.h 54 class EvsEnumerator; 55 56 57 class EvsV4lCamera : public IEvsCamera { 58 public: 59 // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow. 60 Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb) override; 61 Return<EvsResult> setMaxFramesInFlight(uint32_t bufferCount) override; 62 Return<EvsResult> startVideoStream(const ::android::sp<IEvsCameraStream_1_0>& stream) override; 63 Return<void> doneWithFrame(const BufferDesc_1_0& buffer) override; 64 Return<void> stopVideoStream() override; 65 Return<int32_t> getExtendedInfo(uint32_t opaqueIdentifier) override; 66 Return<EvsResult> setExtendedInfo(uint32_t opaqueIdentifier, int32_t opaqueValue) override; 67 68 // Methods from ::android::hardware::automotive::evs::V1_1::IEvsCamera follow. 69 Return<void> getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb) override; 70 Return<void> getPhysicalCameraInfo(const hidl_string& deviceId, 71 getPhysicalCameraInfo_cb _hidl_cb) override; 72 Return<EvsResult> pauseVideoStream() override; 73 Return<EvsResult> resumeVideoStream() override; 74 Return<EvsResult> doneWithFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffer) override; 75 Return<EvsResult> setMaster() override; 76 Return<EvsResult> forceMaster(const sp<IEvsDisplay_1_0>&) override; 77 Return<EvsResult> unsetMaster() override; 78 Return<void> getParameterList(getParameterList_cb _hidl_cb) override; 79 Return<void> getIntParameterRange(CameraParam id, 80 getIntParameterRange_cb _hidl_cb) override; 81 Return<void> setIntParameter(CameraParam id, int32_t value, 82 setIntParameter_cb _hidl_cb) override; 83 Return<void> getIntParameter(CameraParam id, 84 getIntParameter_cb _hidl_cb) override; 85 Return<EvsResult> setExtendedInfo_1_1(uint32_t opaqueIdentifier, 86 const hidl_vec<uint8_t>& opaqueValue) override; 87 Return<void> getExtendedInfo_1_1(uint32_t opaqueIdentifier, 88 getExtendedInfo_1_1_cb _hidl_cb) override; 89 Return<void> importExternalBuffers(const hidl_vec<BufferDesc_1_1>& buffers, 90 importExternalBuffers_cb _hidl_cb) override; 91 92 static sp<EvsV4lCamera> Create(const char *deviceName); 93 static sp<EvsV4lCamera> Create(const char *deviceName, 94 unique_ptr<ConfigManager::CameraInfo> &camInfo, 95 const Stream *streamCfg = nullptr); 96 EvsV4lCamera(const EvsV4lCamera&) = delete; 97 EvsV4lCamera& operator=(const EvsV4lCamera&) = delete; 98 99 virtual ~EvsV4lCamera() override; 100 void shutdown(); 101 getDesc()102 const CameraDesc& getDesc() { return mDescription; }; 103 104 // Dump captured frames to the filesystem 105 android::base::Result<void> startDumpFrames(const std::string& path); 106 android::base::Result<void> stopDumpFrames(); 107 108 private: 109 // Constructors 110 EvsV4lCamera(const char *deviceName, 111 unique_ptr<ConfigManager::CameraInfo> &camInfo); 112 113 // These three functions are expected to be called while mAccessLock is held 114 bool setAvailableFrames_Locked(unsigned bufferCount); 115 unsigned increaseAvailableFrames_Locked(unsigned numToAdd); 116 unsigned decreaseAvailableFrames_Locked(unsigned numToRemove); 117 118 void forwardFrame(imageBuffer* tgt, void* data); 119 inline bool convertToV4l2CID(CameraParam id, uint32_t& v4l2cid); 120 121 sp <IEvsCameraStream_1_0> mStream = nullptr; // The callback used to deliver each frame 122 sp <IEvsCameraStream_1_1> mStream_1_1 = nullptr; // The callback used to deliver each frame 123 124 VideoCapture mVideo; // Interface to the v4l device 125 CameraDesc mDescription = {}; // The properties of this camera 126 127 uint32_t mFormat = 0; // Values from android_pixel_format_t 128 uint32_t mUsage = 0; // Values from from Gralloc.h 129 uint32_t mStride = 0; // Pixels per row (may be greater than image width) 130 131 struct BufferRecord { 132 buffer_handle_t handle; 133 bool inUse; 134 BufferRecordBufferRecord135 explicit BufferRecord(buffer_handle_t h) : handle(h), inUse(false) {}; 136 }; 137 138 std::vector <BufferRecord> mBuffers; // Graphics buffers to transfer images 139 unsigned mFramesAllowed; // How many buffers are we currently using 140 unsigned mFramesInUse; // How many buffers are currently outstanding 141 142 std::set<uint32_t> mCameraControls; // Available camera controls 143 144 // Which format specific function we need to use to move camera imagery into our output buffers 145 void(*mFillBufferFromVideo)(const BufferDesc& tgtBuff, uint8_t* tgt, 146 void* imgData, void* buf, unsigned imgStride); 147 148 149 EvsResult doneWithFrame_impl(const uint32_t id, const buffer_handle_t handle); 150 151 // Synchronization necessary to deconflict the capture thread from the main service thread 152 // Note that the service interface remains single threaded (ie: not reentrant) 153 std::mutex mAccessLock; 154 155 // Static camera module information 156 unique_ptr<ConfigManager::CameraInfo> &mCameraInfo; 157 158 // Extended information 159 std::unordered_map<uint32_t, std::vector<uint8_t>> mExtInfo; 160 161 // Dump captured frames 162 std::atomic<bool> mDumpFrame = false; 163 164 // Path to store captured frames 165 std::string mDumpPath; 166 167 // Frame counter 168 uint64_t mFrameCounter = 0; 169 170 // A buffer to hold an intermediate color conversion data 171 std::vector<uint8_t> mColorSpaceConversionBuffer; 172 }; 173 174 } // namespace implementation 175 } // namespace V1_1 176 } // namespace evs 177 } // namespace automotive 178 } // namespace hardware 179 } // namespace android 180 181 #endif // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSV4LCAMERA_H 182