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 <android/hardware/automotive/evs/1.1/types.h>
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/IEvsDisplay.h>
24 #include <android/hardware/camera/device/3.2/ICameraDevice.h>
25 #include <ui/GraphicBuffer.h>
26 
27 #include <functional>
28 #include <thread>
29 #include <set>
30 
31 #include "VideoCapture.h"
32 #include "ConfigManager.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 private:
105     // Constructors
106     EvsV4lCamera(const char *deviceName,
107                  unique_ptr<ConfigManager::CameraInfo> &camInfo);
108 
109     // These three functions are expected to be called while mAccessLock is held
110     bool setAvailableFrames_Locked(unsigned bufferCount);
111     unsigned increaseAvailableFrames_Locked(unsigned numToAdd);
112     unsigned decreaseAvailableFrames_Locked(unsigned numToRemove);
113 
114     void forwardFrame(imageBuffer* tgt, void* data);
115     inline bool convertToV4l2CID(CameraParam id, uint32_t& v4l2cid);
116 
117     sp <IEvsCameraStream_1_0> mStream     = nullptr;  // The callback used to deliver each frame
118     sp <IEvsCameraStream_1_1> mStream_1_1 = nullptr;  // The callback used to deliver each frame
119 
120     VideoCapture              mVideo;                 // Interface to the v4l device
121     CameraDesc                mDescription = {};      // The properties of this camera
122 
123     uint32_t mFormat = 0;           // Values from android_pixel_format_t
124     uint32_t mUsage  = 0;           // Values from from Gralloc.h
125     uint32_t mStride = 0;           // Pixels per row (may be greater than image width)
126 
127     struct BufferRecord {
128         buffer_handle_t handle;
129         bool inUse;
130 
BufferRecordBufferRecord131         explicit BufferRecord(buffer_handle_t h) : handle(h), inUse(false) {};
132     };
133 
134     std::vector <BufferRecord> mBuffers;    // Graphics buffers to transfer images
135     unsigned mFramesAllowed;                // How many buffers are we currently using
136     unsigned mFramesInUse;                  // How many buffers are currently outstanding
137 
138     std::set<uint32_t> mCameraControls;     // Available camera controls
139 
140     // Which format specific function we need to use to move camera imagery into our output buffers
141     void(*mFillBufferFromVideo)(const BufferDesc& tgtBuff, uint8_t* tgt,
142                                 void* imgData, unsigned imgStride);
143 
144 
145     EvsResult doneWithFrame_impl(const uint32_t id, const buffer_handle_t handle);
146 
147     // Synchronization necessary to deconflict the capture thread from the main service thread
148     // Note that the service interface remains single threaded (ie: not reentrant)
149     std::mutex mAccessLock;
150 
151     // Static camera module information
152     unique_ptr<ConfigManager::CameraInfo> &mCameraInfo;
153 
154     // Extended information
155     std::unordered_map<uint32_t, std::vector<uint8_t>> mExtInfo;
156 };
157 
158 } // namespace implementation
159 } // namespace V1_1
160 } // namespace evs
161 } // namespace automotive
162 } // namespace hardware
163 } // namespace android
164 
165 #endif  // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSV4LCAMERA_H
166