1 /*
2  * Copyright (C) 2019 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_EVSCAMERA_H
18 #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSCAMERA_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 <ui/GraphicBuffer.h>
25 
26 #include <thread>
27 
28 #include "ConfigManager.h"
29 
30 using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc;
31 using BufferDesc_1_1 = ::android::hardware::automotive::evs::V1_1::BufferDesc;
32 using IEvsCameraStream_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsCameraStream;
33 using IEvsCameraStream_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCameraStream;
34 using ::android::hardware::automotive::evs::V1_0::EvsResult;
35 using ::android::hardware::automotive::evs::V1_0::CameraDesc;
36 using IEvsDisplay_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsDisplay;
37 using IEvsDisplay_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsDisplay;
38 
39 
40 namespace android {
41 namespace hardware {
42 namespace automotive {
43 namespace evs {
44 namespace V1_1 {
45 namespace implementation {
46 
47 
48 // From EvsEnumerator.h
49 class EvsEnumerator;
50 
51 
52 class EvsCamera : public IEvsCamera {
53 public:
54     // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow.
55     Return<void>      getCameraInfo(getCameraInfo_cb _hidl_cb)  override;
56     Return<EvsResult> setMaxFramesInFlight(uint32_t bufferCount) override;
57     Return<EvsResult> startVideoStream(const ::android::sp<IEvsCameraStream_1_0>& stream) override;
58     Return<void>      stopVideoStream() override;
59     Return<void>      doneWithFrame(const BufferDesc_1_0& buffer) override;
60 
61     Return<int32_t>   getExtendedInfo(uint32_t opaqueIdentifier) override;
62     Return<EvsResult> setExtendedInfo(uint32_t opaqueIdentifier, int32_t opaqueValue) override;
63 
64     // Methods from ::android::hardware::automotive::evs::V1_1::IEvsCamera follow.
65     Return<void>      getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb)  override;
66     Return<void>      getPhysicalCameraInfo(const hidl_string& id,
67                                             getPhysicalCameraInfo_cb _hidl_cb)  override;
68     Return<EvsResult> pauseVideoStream() override;
69     Return<EvsResult> resumeVideoStream() override;
70     Return<EvsResult> doneWithFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffer) override;
71     Return<EvsResult> setMaster() override;
72     Return<EvsResult> forceMaster(const sp<IEvsDisplay_1_0>& display) override;
73     Return<EvsResult> unsetMaster() override;
74     Return<void>      getParameterList(getParameterList_cb _hidl_cb) override;
75     Return<void>      getIntParameterRange(CameraParam id,
76                                            getIntParameterRange_cb _hidl_cb) override;
77     Return<void>      setIntParameter(CameraParam id, int32_t value,
78                                       setIntParameter_cb _hidl_cb) override;
79     Return<void>      getIntParameter(CameraParam id,
80                                       getIntParameter_cb _hidl_cb) override;
81     Return<EvsResult> setExtendedInfo_1_1(uint32_t opaqueIdentifier,
82                                           const hidl_vec<uint8_t>& opaqueValue) override;
83     Return<void>      getExtendedInfo_1_1(uint32_t opaqueIdentifier,
84                                           getExtendedInfo_1_1_cb _hidl_cb) override;
85     Return<void>      importExternalBuffers(const hidl_vec<BufferDesc_1_1>& buffers,
86                                             importExternalBuffers_cb _hidl_cb) override;
87 
88     static sp<EvsCamera> Create(const char *deviceName);
89     static sp<EvsCamera> Create(const char *deviceName,
90                                 unique_ptr<ConfigManager::CameraInfo> &camInfo,
91                                 const Stream *streamCfg = nullptr);
92     EvsCamera(const EvsCamera&) = delete;
93     EvsCamera& operator=(const EvsCamera&) = delete;
94 
95     virtual ~EvsCamera() override;
96     void forceShutdown();   // This gets called if another caller "steals" ownership of the camera
97 
getDesc()98     const CameraDesc& getDesc() { return mDescription; };
99 
100     static const char kCameraName_Backup[];
101 
102 private:
103     EvsCamera(const char *id,
104               unique_ptr<ConfigManager::CameraInfo> &camInfo);
105     // These three functions are expected to be called while mAccessLock is held
106     //
107     bool setAvailableFrames_Locked(unsigned bufferCount);
108     unsigned increaseAvailableFrames_Locked(unsigned numToAdd);
109     unsigned decreaseAvailableFrames_Locked(unsigned numToRemove);
110 
111     void generateFrames();
112     void fillTestFrame(const BufferDesc_1_0& buff);
113     void fillTestFrame(const BufferDesc_1_1& buff);
114     void returnBuffer(const uint32_t bufferId, const buffer_handle_t memHandle);
115 
116     sp<EvsEnumerator> mEnumerator;  // The enumerator object that created this camera
117 
118     CameraDesc mDescription = {};   // The properties of this camera
119 
120     std::thread mCaptureThread;     // The thread we'll use to synthesize frames
121 
122     uint32_t mWidth  = 0;           // Horizontal pixel count in the buffers
123     uint32_t mHeight = 0;           // Vertical pixel count in the buffers
124     uint32_t mFormat = 0;           // Values from android_pixel_format_t
125     uint64_t mUsage  = 0;           // Values from from Gralloc.h
126     uint32_t mStride = 0;           // Bytes per line in the buffers
127 
128     sp<IEvsCameraStream_1_1> mStream = nullptr;  // The callback used to deliver each frame
129 
130     struct BufferRecord {
131         buffer_handle_t handle;
132         bool inUse;
133 
BufferRecordBufferRecord134         explicit BufferRecord(buffer_handle_t h) : handle(h), inUse(false) {};
135     };
136 
137     std::vector <BufferRecord> mBuffers;  // Graphics buffers to transfer images
138     unsigned mFramesAllowed;              // How many buffers are we currently using
139     unsigned mFramesInUse;                // How many buffers are currently outstanding
140 
141     enum StreamStateValues {
142         STOPPED,
143         RUNNING,
144         STOPPING,
145         DEAD,
146     };
147     StreamStateValues mStreamState;
148 
149     // Synchronization necessary to deconflict mCaptureThread from the main service thread
150     std::mutex mAccessLock;
151 
152     // Static camera module information
153     unique_ptr<ConfigManager::CameraInfo> &mCameraInfo;
154 };
155 
156 } // namespace implementation
157 } // namespace V1_1
158 } // namespace evs
159 } // namespace automotive
160 } // namespace hardware
161 } // namespace android
162 
163 #endif  // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSCAMERA_H
164