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