• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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_CAMERA_DEVICE_V1_0_CAMERADEVICE_H
18 #define ANDROID_HARDWARE_CAMERA_DEVICE_V1_0_CAMERADEVICE_H
19 
20 #include <unordered_map>
21 #include "utils/Mutex.h"
22 #include "utils/SortedVector.h"
23 #include "CameraModule.h"
24 #include "HandleImporter.h"
25 
26 #include <android/hardware/camera/device/1.0/ICameraDevice.h>
27 #include <android/hidl/allocator/1.0/IAllocator.h>
28 #include <android/hidl/memory/1.0/IMemory.h>
29 #include <hidl/MQDescriptor.h>
30 #include <hidl/Status.h>
31 
32 namespace android {
33 namespace hardware {
34 namespace camera {
35 namespace device {
36 namespace V1_0 {
37 namespace implementation {
38 
39 using ::android::hardware::camera::common::V1_0::CameraResourceCost;
40 using ::android::hardware::camera::common::V1_0::Status;
41 using ::android::hardware::camera::common::V1_0::TorchMode;
42 using ::android::hardware::camera::common::V1_0::helper::CameraModule;
43 using ::android::hardware::camera::common::V1_0::helper::HandleImporter;
44 using ::android::hardware::camera::device::V1_0::CameraInfo;
45 using ::android::hardware::camera::device::V1_0::CommandType;
46 using ::android::hardware::camera::device::V1_0::ICameraDevice;
47 using ::android::hardware::camera::device::V1_0::ICameraDeviceCallback;
48 using ::android::hardware::camera::device::V1_0::ICameraDevicePreviewCallback;
49 using ::android::hardware::camera::device::V1_0::MemoryId;
50 using ::android::hidl::allocator::V1_0::IAllocator;
51 using ::android::hidl::base::V1_0::IBase;
52 using ::android::hidl::memory::V1_0::IMemory;
53 using ::android::hardware::hidl_array;
54 using ::android::hardware::hidl_memory;
55 using ::android::hardware::hidl_string;
56 using ::android::hardware::hidl_vec;
57 using ::android::hardware::Return;
58 using ::android::hardware::Void;
59 using ::android::sp;
60 
61 struct CameraDevice : public ICameraDevice {
62 
63     // Called by provider HAL. Provider HAL must ensure the uniqueness of
64     // CameraDevice object per cameraId, or there could be multiple CameraDevice
65     // trying to access the same physical camera.
66     // Also, provider will have to keep track of all CameraDevice objects in
67     // order to notify CameraDevice when the underlying camera is detached
68     CameraDevice(sp<CameraModule> module,
69                  const std::string& cameraId,
70                  const SortedVector<std::pair<std::string, std::string>>& cameraDeviceNames);
71     ~CameraDevice();
72 
73     // Caller must use this method to check if CameraDevice ctor failed
isInitFailedCameraDevice74     bool isInitFailed() { return mInitFail; }
75     // Used by provider HAL to signal external camera disconnected
76     void setConnectionStatus(bool connected);
77 
78     // Methods from ::android::hardware::camera::device::V1_0::ICameraDevice follow.
79     Return<void> getResourceCost(getResourceCost_cb _hidl_cb) override;
80     Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb) override;
81     Return<Status> setTorchMode(TorchMode mode) override;
82     Return<Status> dumpState(const hidl_handle& fd) override;
83     Return<Status> open(const sp<ICameraDeviceCallback>& callback) override;
84     Return<Status> setPreviewWindow(const sp<ICameraDevicePreviewCallback>& window) override;
85     Return<void> enableMsgType(uint32_t msgType) override;
86     Return<void> disableMsgType(uint32_t msgType) override;
87     Return<bool> msgTypeEnabled(uint32_t msgType) override;
88     Return<Status> startPreview() override;
89     Return<void> stopPreview() override;
90     Return<bool> previewEnabled() override;
91     Return<Status> storeMetaDataInBuffers(bool enable) override;
92     Return<Status> startRecording() override;
93     Return<void> stopRecording() override;
94     Return<bool> recordingEnabled() override;
95     Return<void> releaseRecordingFrame(uint32_t memId, uint32_t bufferIndex) override;
96     Return<void> releaseRecordingFrameHandle(
97             uint32_t memId, uint32_t bufferIndex, const hidl_handle& frame) override;
98     Return<void> releaseRecordingFrameHandleBatch(
99             const hidl_vec<VideoFrameMessage>&) override;
100     Return<Status> autoFocus() override;
101     Return<Status> cancelAutoFocus() override;
102     Return<Status> takePicture() override;
103     Return<Status> cancelPicture() override;
104     Return<Status> setParameters(const hidl_string& params) override;
105     Return<void> getParameters(getParameters_cb _hidl_cb) override;
106     Return<Status> sendCommand(CommandType cmd, int32_t arg1, int32_t arg2) override;
107     Return<void> close() override;
108 
109 private:
110     struct CameraMemory : public camera_memory_t {
111         MemoryId mId;
112         CameraDevice* mDevice;
113     };
114 
115     class CameraHeapMemory : public RefBase {
116     public:
117         CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1);
118         explicit CameraHeapMemory(
119             sp<IAllocator> ashmemAllocator, size_t buf_size, uint_t num_buffers = 1);
120         void commonInitialization();
121         virtual ~CameraHeapMemory();
122 
123         size_t mBufSize;
124         uint_t mNumBufs;
125 
126         // Shared memory related members
127         hidl_memory      mHidlHeap;
128         native_handle_t* mHidlHandle; // contains one shared memory FD
129         void*            mHidlHeapMemData;
130         sp<IMemory>      mHidlHeapMemory; // munmap happens in ~IMemory()
131 
132         CameraMemory handle;
133     };
134     sp<IAllocator> mAshmemAllocator;
135 
136     const sp<CameraModule> mModule;
137     const std::string mCameraId;
138     // const after ctor
139     int   mCameraIdInt;
140     int   mDeviceVersion;
141 
142     camera_device_t* mDevice = nullptr;
143 
144     void initHalPreviewWindow();
145     struct CameraPreviewWindow : public preview_stream_ops {
146         // Called when we expect buffer will be re-allocated
147         void cleanUpCirculatingBuffers();
148 
149         Mutex mLock;
150         sp<ICameraDevicePreviewCallback> mPreviewCallback = nullptr;
151         std::unordered_map<uint64_t, buffer_handle_t> mCirculatingBuffers;
152         std::unordered_map<buffer_handle_t*, uint64_t> mBufferIdMap;
153     } mHalPreviewWindow;
154 
155     // gating access to mDevice, mInitFail, mDisconnected
156     mutable Mutex mLock;
157 
158     bool  mInitFail = false;
159     // Set by provider (when external camera is connected/disconnected)
160     bool  mDisconnected;
161 
162     static HandleImporter sHandleImporter;
163 
164     const SortedVector<std::pair<std::string, std::string>>& mCameraDeviceNames;
165 
166     sp<ICameraDeviceCallback> mDeviceCallback = nullptr;
167 
168     std::unordered_map<MemoryId, CameraHeapMemory*> mMemoryMap;
169 
170     bool mMetadataMode = false;
171 
172     mutable Mutex mBatchLock;
173     // Start of protection scope for mBatchLock
174     uint32_t mBatchSize = 0; // 0 for non-batch mode, set to other value to start batching
175     int32_t mBatchMsgType;   // Maybe only allow DataCallbackMsg::VIDEO_FRAME?
176     std::vector<HandleTimestampMessage> mInflightBatch;
177     // End of protection scope for mBatchLock
178 
179     void handleCallbackTimestamp(
180             nsecs_t timestamp, int32_t msg_type,
181             MemoryId memId , unsigned index, native_handle_t* handle);
182     void releaseRecordingFrameLocked(uint32_t memId, uint32_t bufferIndex, const native_handle_t*);
183 
184     // shared memory methods
185     static camera_memory_t* sGetMemory(int fd, size_t buf_size, uint_t num_bufs, void *user);
186     static void sPutMemory(camera_memory_t *data);
187 
188     // Device callback forwarding methods
189     static void sNotifyCb(int32_t msg_type, int32_t ext1, int32_t ext2, void *user);
190     static void sDataCb(int32_t msg_type, const camera_memory_t *data, unsigned int index,
191                         camera_frame_metadata_t *metadata, void *user);
192     static void sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type,
193                                     const camera_memory_t *data, unsigned index, void *user);
194 
195     // Preview window callback forwarding methods
196     static int sDequeueBuffer(struct preview_stream_ops* w,
197                               buffer_handle_t** buffer, int *stride);
198 
199     static int sLockBuffer(struct preview_stream_ops* w, buffer_handle_t* buffer);
200 
201     static int sEnqueueBuffer(struct preview_stream_ops* w, buffer_handle_t* buffer);
202 
203     static int sCancelBuffer(struct preview_stream_ops* w, buffer_handle_t* buffer);
204 
205     static int sSetBufferCount(struct preview_stream_ops* w, int count);
206 
207     static int sSetBuffersGeometry(struct preview_stream_ops* w,
208                                    int width, int height, int format);
209 
210     static int sSetCrop(struct preview_stream_ops *w, int left, int top, int right, int bottom);
211 
212     static int sSetTimestamp(struct preview_stream_ops *w, int64_t timestamp);
213 
214     static int sSetUsage(struct preview_stream_ops* w, int usage);
215 
216     static int sSetSwapInterval(struct preview_stream_ops *w, int interval);
217 
218     static int sGetMinUndequeuedBufferCount(const struct preview_stream_ops *w, int *count);
219 
220     // convert conventional HAL status to HIDL Status
221     static Status getHidlStatus(const int&);
222     static status_t getStatusT(const Status& s);
223 
224     Status initStatus() const;
225     void closeLocked();
226 };
227 
228 }  // namespace implementation
229 }  // namespace V1_0
230 }  // namespace device
231 }  // namespace camera
232 }  // namespace hardware
233 }  // namespace android
234 
235 #endif  // ANDROID_HARDWARE_CAMERA_DEVICE_V1_0_CAMERADEVICE_H
236