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 #define LOG_TAG "[email protected]"
18 #include <hardware/camera.h>
19 #include <hardware/gralloc1.h>
20 #include <hidlmemory/mapping.h>
21 #include <log/log.h>
22 #include <utils/Trace.h>
23 
24 #include <media/hardware/HardwareAPI.h> // For VideoNativeHandleMetadata
25 #include "CameraDevice_1_0.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace camera {
30 namespace device {
31 namespace V1_0 {
32 namespace implementation {
33 
34 using ::android::hardware::graphics::common::V1_0::BufferUsage;
35 using ::android::hardware::graphics::common::V1_0::PixelFormat;
36 
37 HandleImporter CameraDevice::sHandleImporter;
38 
getHidlStatus(const int & status)39 Status CameraDevice::getHidlStatus(const int& status) {
40     switch (status) {
41         case 0: return Status::OK;
42         case -ENOSYS: return Status::OPERATION_NOT_SUPPORTED;
43         case -EBUSY : return Status::CAMERA_IN_USE;
44         case -EUSERS: return Status::MAX_CAMERAS_IN_USE;
45         case -ENODEV: return Status::INTERNAL_ERROR;
46         case -EINVAL: return Status::ILLEGAL_ARGUMENT;
47         default:
48             ALOGE("%s: unknown HAL status code %d", __FUNCTION__, status);
49             return Status::INTERNAL_ERROR;
50     }
51 }
52 
getStatusT(const Status & s)53 status_t CameraDevice::getStatusT(const Status& s)  {
54     switch(s) {
55         case Status::OK:
56             return OK;
57         case Status::ILLEGAL_ARGUMENT:
58             return BAD_VALUE;
59         case Status::CAMERA_IN_USE:
60             return -EBUSY;
61         case Status::MAX_CAMERAS_IN_USE:
62             return -EUSERS;
63         case Status::METHOD_NOT_SUPPORTED:
64             return UNKNOWN_TRANSACTION;
65         case Status::OPERATION_NOT_SUPPORTED:
66             return INVALID_OPERATION;
67         case Status::CAMERA_DISCONNECTED:
68             return DEAD_OBJECT;
69         case Status::INTERNAL_ERROR:
70             return INVALID_OPERATION;
71     }
72     ALOGW("Unexpected HAL status code %d", s);
73     return INVALID_OPERATION;
74 }
75 
initStatus() const76 Status CameraDevice::initStatus() const {
77     Mutex::Autolock _l(mLock);
78     Status status = Status::OK;
79     if (mInitFail) {
80         status = Status::INTERNAL_ERROR;
81     } else if (mDisconnected) {
82         status = Status::CAMERA_DISCONNECTED;
83     }
84     return status;
85 }
86 
CameraDevice(sp<CameraModule> module,const std::string & cameraId,const SortedVector<std::pair<std::string,std::string>> & cameraDeviceNames)87 CameraDevice::CameraDevice(
88     sp<CameraModule> module, const std::string& cameraId,
89     const SortedVector<std::pair<std::string, std::string>>& cameraDeviceNames) :
90         mModule(module),
91         mCameraId(cameraId),
92         mDisconnected(false),
93         mCameraDeviceNames(cameraDeviceNames) {
94     mCameraIdInt = atoi(mCameraId.c_str());
95     // Should not reach here as provider also validate ID
96     if (mCameraIdInt < 0 || mCameraIdInt >= module->getNumberOfCameras()) {
97         ALOGE("%s: Invalid camera id: %s", __FUNCTION__, mCameraId.c_str());
98         mInitFail = true;
99     }
100 
101     mDeviceVersion = mModule->getDeviceVersion(mCameraIdInt);
102     if (mDeviceVersion != CAMERA_DEVICE_API_VERSION_1_0 && !mModule->isOpenLegacyDefined()) {
103         ALOGI("%s: Camera id %s does not support HAL1.0",
104                 __FUNCTION__, mCameraId.c_str());
105         mInitFail = true;
106     }
107 
108     mAshmemAllocator = IAllocator::getService("ashmem");
109     if (mAshmemAllocator == nullptr) {
110         ALOGI("%s: cannot get ashmemAllocator", __FUNCTION__);
111         mInitFail = true;
112     }
113 }
114 
~CameraDevice()115 CameraDevice::~CameraDevice() {
116     Mutex::Autolock _l(mLock);
117     if (mDevice != nullptr) {
118         ALOGW("%s: camera %s is deleted while open", __FUNCTION__, mCameraId.c_str());
119         closeLocked();
120     }
121     mHalPreviewWindow.cleanUpCirculatingBuffers();
122 }
123 
124 
setConnectionStatus(bool connected)125 void CameraDevice::setConnectionStatus(bool connected) {
126     Mutex::Autolock _l(mLock);
127     mDisconnected = !connected;
128     if (mDevice == nullptr) {
129         return;
130     }
131     if (!connected) {
132         ALOGW("%s: camera %s is disconneted. Closing", __FUNCTION__, mCameraId.c_str());
133         closeLocked();
134     }
135     return;
136 }
137 
cleanUpCirculatingBuffers()138 void CameraDevice::CameraPreviewWindow::cleanUpCirculatingBuffers() {
139     Mutex::Autolock _l(mLock);
140     for (auto pair : mCirculatingBuffers) {
141         sHandleImporter.freeBuffer(pair.second);
142     }
143     mCirculatingBuffers.clear();
144     mBufferIdMap.clear();
145 }
146 
sDequeueBuffer(struct preview_stream_ops * w,buffer_handle_t ** buffer,int * stride)147 int CameraDevice::sDequeueBuffer(struct preview_stream_ops* w,
148                                    buffer_handle_t** buffer, int *stride) {
149     CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
150     if (object->mPreviewCallback == nullptr) {
151         ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
152         return INVALID_OPERATION;
153     }
154 
155     if (buffer == nullptr || stride == nullptr) {
156         ALOGE("%s: buffer (%p) and stride (%p) must not be null!", __FUNCTION__, buffer, stride);
157         return BAD_VALUE;
158     }
159 
160     Status s;
161     object->mPreviewCallback->dequeueBuffer(
162         [&](auto status, uint64_t bufferId, const auto& buf, uint32_t strd) {
163             s = status;
164             if (s == Status::OK) {
165                 Mutex::Autolock _l(object->mLock);
166                 if (object->mCirculatingBuffers.count(bufferId) == 0) {
167                     buffer_handle_t importedBuf = buf.getNativeHandle();
168                     sHandleImporter.importBuffer(importedBuf);
169                     if (importedBuf == nullptr) {
170                         ALOGE("%s: preview buffer import failed!", __FUNCTION__);
171                         s = Status::INTERNAL_ERROR;
172                         return;
173                     } else {
174                         object->mCirculatingBuffers[bufferId] = importedBuf;
175                         object->mBufferIdMap[&(object->mCirculatingBuffers[bufferId])] = bufferId;
176                     }
177                 }
178                 *buffer = &(object->mCirculatingBuffers[bufferId]);
179                 *stride = strd;
180             }
181         });
182     return getStatusT(s);
183 }
184 
sLockBuffer(struct preview_stream_ops *,buffer_handle_t *)185 int CameraDevice::sLockBuffer(struct preview_stream_ops*, buffer_handle_t*) {
186     return 0;
187 }
188 
sEnqueueBuffer(struct preview_stream_ops * w,buffer_handle_t * buffer)189 int CameraDevice::sEnqueueBuffer(struct preview_stream_ops* w, buffer_handle_t* buffer) {
190     CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
191     if (object->mPreviewCallback == nullptr) {
192         ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
193         return INVALID_OPERATION;
194     }
195     uint64_t bufferId = object->mBufferIdMap.at(buffer);
196     return getStatusT(object->mPreviewCallback->enqueueBuffer(bufferId));
197 }
198 
sCancelBuffer(struct preview_stream_ops * w,buffer_handle_t * buffer)199 int CameraDevice::sCancelBuffer(struct preview_stream_ops* w, buffer_handle_t* buffer) {
200     CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
201     if (object->mPreviewCallback == nullptr) {
202         ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
203         return INVALID_OPERATION;
204     }
205     uint64_t bufferId = object->mBufferIdMap.at(buffer);
206     return getStatusT(object->mPreviewCallback->cancelBuffer(bufferId));
207 }
208 
sSetBufferCount(struct preview_stream_ops * w,int count)209 int CameraDevice::sSetBufferCount(struct preview_stream_ops* w, int count) {
210     CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
211     if (object->mPreviewCallback == nullptr) {
212         ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
213         return INVALID_OPERATION;
214     }
215 
216     object->cleanUpCirculatingBuffers();
217     return getStatusT(object->mPreviewCallback->setBufferCount(count));
218 }
219 
sSetBuffersGeometry(struct preview_stream_ops * w,int width,int height,int format)220 int CameraDevice::sSetBuffersGeometry(struct preview_stream_ops* w,
221                                          int width, int height, int format) {
222     CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
223     if (object->mPreviewCallback == nullptr) {
224         ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
225         return INVALID_OPERATION;
226     }
227 
228     object->cleanUpCirculatingBuffers();
229     return getStatusT(
230             object->mPreviewCallback->setBuffersGeometry(width, height, (PixelFormat) format));
231 }
232 
sSetCrop(struct preview_stream_ops * w,int left,int top,int right,int bottom)233 int CameraDevice::sSetCrop(struct preview_stream_ops *w,
234                              int left, int top, int right, int bottom) {
235     CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
236     if (object->mPreviewCallback == nullptr) {
237         ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
238         return INVALID_OPERATION;
239     }
240 
241     return getStatusT(object->mPreviewCallback->setCrop(left, top, right, bottom));
242 }
243 
sSetTimestamp(struct preview_stream_ops * w,int64_t timestamp)244 int CameraDevice::sSetTimestamp(struct preview_stream_ops *w, int64_t timestamp) {
245     CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
246     if (object->mPreviewCallback == nullptr) {
247         ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
248         return INVALID_OPERATION;
249     }
250 
251     return getStatusT(object->mPreviewCallback->setTimestamp(timestamp));
252 }
253 
sSetUsage(struct preview_stream_ops * w,int usage)254 int CameraDevice::sSetUsage(struct preview_stream_ops* w, int usage) {
255     CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
256     if (object->mPreviewCallback == nullptr) {
257         ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
258         return INVALID_OPERATION;
259     }
260 
261     object->cleanUpCirculatingBuffers();
262     return getStatusT(object->mPreviewCallback->setUsage((BufferUsage)usage));
263 }
264 
sSetSwapInterval(struct preview_stream_ops * w,int interval)265 int CameraDevice::sSetSwapInterval(struct preview_stream_ops *w, int interval) {
266     CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
267     if (object->mPreviewCallback == nullptr) {
268         ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
269         return INVALID_OPERATION;
270     }
271 
272     return getStatusT(object->mPreviewCallback->setSwapInterval(interval));
273 }
274 
sGetMinUndequeuedBufferCount(const struct preview_stream_ops * w,int * count)275 int CameraDevice::sGetMinUndequeuedBufferCount(
276                   const struct preview_stream_ops *w,
277                   int *count) {
278     const CameraPreviewWindow* object =  static_cast<const CameraPreviewWindow*>(w);
279     if (object->mPreviewCallback == nullptr) {
280         ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
281         return INVALID_OPERATION;
282     }
283     if (count == nullptr) {
284         ALOGE("%s: count is null!", __FUNCTION__);
285         return BAD_VALUE;
286     }
287 
288     Status s;
289     object->mPreviewCallback->getMinUndequeuedBufferCount(
290         [&](auto status, uint32_t cnt) {
291             s = status;
292             if (s == Status::OK) {
293                 *count = cnt;
294             }
295         });
296     return getStatusT(s);
297 }
298 
CameraHeapMemory(int fd,size_t buf_size,uint_t num_buffers)299 CameraDevice::CameraHeapMemory::CameraHeapMemory(
300     int fd, size_t buf_size, uint_t num_buffers) :
301         mBufSize(buf_size),
302         mNumBufs(num_buffers) {
303     mHidlHandle = native_handle_create(1,0);
304     mHidlHandle->data[0] = fcntl(fd, F_DUPFD_CLOEXEC, 0);
305     const size_t pagesize = getpagesize();
306     size_t size = ((buf_size * num_buffers + pagesize-1) & ~(pagesize-1));
307     mHidlHeap = hidl_memory("ashmem", mHidlHandle, size);
308     commonInitialization();
309 }
310 
CameraHeapMemory(sp<IAllocator> ashmemAllocator,size_t buf_size,uint_t num_buffers)311 CameraDevice::CameraHeapMemory::CameraHeapMemory(
312     sp<IAllocator> ashmemAllocator,
313     size_t buf_size, uint_t num_buffers) :
314         mBufSize(buf_size),
315         mNumBufs(num_buffers) {
316     const size_t pagesize = getpagesize();
317     size_t size = ((buf_size * num_buffers + pagesize-1) & ~(pagesize-1));
318     ashmemAllocator->allocate(size,
319         [&](bool success, const hidl_memory& mem) {
320             if (!success) {
321                 ALOGE("%s: allocating ashmem of %zu bytes failed!",
322                         __FUNCTION__, buf_size * num_buffers);
323                 return;
324             }
325             mHidlHandle = native_handle_clone(mem.handle());
326             mHidlHeap = hidl_memory("ashmem", mHidlHandle, size);
327         });
328 
329     commonInitialization();
330 }
331 
commonInitialization()332 void CameraDevice::CameraHeapMemory::commonInitialization() {
333     mHidlHeapMemory = mapMemory(mHidlHeap);
334     if (mHidlHeapMemory == nullptr) {
335         ALOGE("%s: memory map failed!", __FUNCTION__);
336         native_handle_close(mHidlHandle); // close FD for the shared memory
337         native_handle_delete(mHidlHandle);
338         mHidlHeap = hidl_memory();
339         mHidlHandle = nullptr;
340         return;
341     }
342     mHidlHeapMemData = mHidlHeapMemory->getPointer();
343     handle.data = mHidlHeapMemData;
344     handle.size = mBufSize * mNumBufs;
345     handle.handle = this;
346     handle.release = sPutMemory;
347 }
348 
~CameraHeapMemory()349 CameraDevice::CameraHeapMemory::~CameraHeapMemory() {
350     if (mHidlHeapMemory != nullptr) {
351         mHidlHeapMemData = nullptr;
352         mHidlHeapMemory.clear(); // The destructor will trigger munmap
353     }
354 
355     if (mHidlHandle) {
356         native_handle_close(mHidlHandle); // close FD for the shared memory
357         native_handle_delete(mHidlHandle);
358     }
359 }
360 
361 // shared memory methods
sGetMemory(int fd,size_t buf_size,uint_t num_bufs,void * user)362 camera_memory_t* CameraDevice::sGetMemory(int fd, size_t buf_size, uint_t num_bufs, void *user) {
363     ALOGV("%s", __FUNCTION__);
364     CameraDevice* object = static_cast<CameraDevice*>(user);
365     if (object->mDeviceCallback == nullptr) {
366         ALOGE("%s: camera HAL request memory while camera is not opened!", __FUNCTION__);
367         return nullptr;
368     }
369 
370     CameraHeapMemory* mem;
371     if (fd < 0) {
372         mem = new CameraHeapMemory(object->mAshmemAllocator, buf_size, num_bufs);
373     } else {
374         mem = new CameraHeapMemory(fd, buf_size, num_bufs);
375     }
376     mem->incStrong(mem);
377     hidl_handle hidlHandle = mem->mHidlHandle;
378     MemoryId id = object->mDeviceCallback->registerMemory(hidlHandle, buf_size, num_bufs);
379     mem->handle.mId = id;
380 
381     {
382         Mutex::Autolock _l(object->mMemoryMapLock);
383         if (object->mMemoryMap.count(id) != 0) {
384             ALOGE("%s: duplicate MemoryId %d returned by client!", __FUNCTION__, id);
385         }
386         object->mMemoryMap[id] = mem;
387     }
388     mem->handle.mDevice = object;
389     return &mem->handle;
390 }
391 
sPutMemory(camera_memory_t * data)392 void CameraDevice::sPutMemory(camera_memory_t *data) {
393     if (!data)
394         return;
395 
396     CameraHeapMemory* mem = static_cast<CameraHeapMemory *>(data->handle);
397     CameraDevice* device = mem->handle.mDevice;
398     if (device == nullptr) {
399         ALOGE("%s: camera HAL return memory for a null device!", __FUNCTION__);
400         return;
401     }
402     if (device->mDeviceCallback == nullptr) {
403         ALOGE("%s: camera HAL return memory while camera is not opened!", __FUNCTION__);
404         return;
405     }
406     device->mDeviceCallback->unregisterMemory(mem->handle.mId);
407     {
408         Mutex::Autolock _l(device->mMemoryMapLock);
409         device->mMemoryMap.erase(mem->handle.mId);
410     }
411     mem->decStrong(mem);
412 }
413 
414 // Callback forwarding methods
sNotifyCb(int32_t msg_type,int32_t ext1,int32_t ext2,void * user)415 void CameraDevice::sNotifyCb(int32_t msg_type, int32_t ext1, int32_t ext2, void *user) {
416     ALOGV("%s", __FUNCTION__);
417     CameraDevice* object = static_cast<CameraDevice*>(user);
418     if (object->mDeviceCallback != nullptr) {
419         object->mDeviceCallback->notifyCallback((NotifyCallbackMsg) msg_type, ext1, ext2);
420     }
421 }
422 
sDataCb(int32_t msg_type,const camera_memory_t * data,unsigned int index,camera_frame_metadata_t * metadata,void * user)423 void CameraDevice::sDataCb(int32_t msg_type, const camera_memory_t *data, unsigned int index,
424         camera_frame_metadata_t *metadata, void *user) {
425     ALOGV("%s", __FUNCTION__);
426     CameraDevice* object = static_cast<CameraDevice*>(user);
427     sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory*>(data->handle));
428     if (index >= mem->mNumBufs) {
429         ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
430              index, mem->mNumBufs);
431         return;
432     }
433     if (object->mDeviceCallback != nullptr) {
434         CameraFrameMetadata hidlMetadata;
435         if (metadata) {
436             hidlMetadata.faces.resize(metadata->number_of_faces);
437             for (size_t i = 0; i < hidlMetadata.faces.size(); i++) {
438                 hidlMetadata.faces[i].score = metadata->faces[i].score;
439                 hidlMetadata.faces[i].id = metadata->faces[i].id;
440                 for (int k = 0; k < 4; k++) {
441                     hidlMetadata.faces[i].rect[k] = metadata->faces[i].rect[k];
442                 }
443                 for (int k = 0; k < 2; k++) {
444                     hidlMetadata.faces[i].leftEye[k] = metadata->faces[i].left_eye[k];
445                 }
446                 for (int k = 0; k < 2; k++) {
447                     hidlMetadata.faces[i].rightEye[k] = metadata->faces[i].right_eye[k];
448                 }
449                 for (int k = 0; k < 2; k++) {
450                     hidlMetadata.faces[i].mouth[k] = metadata->faces[i].mouth[k];
451                 }
452             }
453         }
454         CameraHeapMemory* mem = static_cast<CameraHeapMemory *>(data->handle);
455         object->mDeviceCallback->dataCallback(
456                 (DataCallbackMsg) msg_type, mem->handle.mId, index, hidlMetadata);
457     }
458 }
459 
handleCallbackTimestamp(nsecs_t timestamp,int32_t msg_type,MemoryId memId,unsigned index,native_handle_t * handle)460 void CameraDevice::handleCallbackTimestamp(
461         nsecs_t timestamp, int32_t msg_type,
462         MemoryId memId , unsigned index, native_handle_t* handle) {
463     uint32_t batchSize = 0;
464     {
465         Mutex::Autolock _l(mBatchLock);
466         batchSize = mBatchSize;
467     }
468 
469     if (batchSize == 0) { // non-batch mode
470         mDeviceCallback->handleCallbackTimestamp(
471                 (DataCallbackMsg) msg_type, handle, memId, index, timestamp);
472     } else { // batch mode
473         Mutex::Autolock _l(mBatchLock);
474         size_t inflightSize = mInflightBatch.size();
475         if (inflightSize == 0) {
476             mBatchMsgType = msg_type;
477         } else if (mBatchMsgType != msg_type) {
478             ALOGE("%s: msg_type change (from %d to %d) is not supported!",
479                     __FUNCTION__, mBatchMsgType, msg_type);
480             return;
481         }
482         mInflightBatch.push_back({handle, memId, index, timestamp});
483 
484         // Send batched frames to camera framework
485         if (mInflightBatch.size() >= batchSize) {
486             mDeviceCallback->handleCallbackTimestampBatch(
487                     (DataCallbackMsg) mBatchMsgType, mInflightBatch);
488             mInflightBatch.clear();
489         }
490     }
491 }
492 
sDataCbTimestamp(nsecs_t timestamp,int32_t msg_type,const camera_memory_t * data,unsigned index,void * user)493 void CameraDevice::sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type,
494         const camera_memory_t *data, unsigned index, void *user) {
495     ALOGV("%s", __FUNCTION__);
496     CameraDevice* object = static_cast<CameraDevice*>(user);
497     // Start refcounting the heap object from here on.  When the clients
498     // drop all references, it will be destroyed (as well as the enclosed
499     // MemoryHeapBase.
500     sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory*>(data->handle));
501     if (index >= mem->mNumBufs) {
502         ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
503              index, mem->mNumBufs);
504         return;
505     }
506 
507     native_handle_t* handle = nullptr;
508     if (object->mMetadataMode) {
509         if (mem->mBufSize == sizeof(VideoNativeHandleMetadata)) {
510             VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*)
511                     ((uint8_t*) mem->mHidlHeapMemData + index * mem->mBufSize);
512             if (md->eType == kMetadataBufferTypeNativeHandleSource) {
513                 handle = md->pHandle;
514             }
515         }
516     }
517 
518     if (object->mDeviceCallback != nullptr) {
519         if (handle == nullptr) {
520             object->mDeviceCallback->dataCallbackTimestamp(
521                     (DataCallbackMsg) msg_type, mem->handle.mId, index, timestamp);
522         } else {
523             object->handleCallbackTimestamp(timestamp, msg_type, mem->handle.mId, index, handle);
524         }
525     }
526 }
527 
initHalPreviewWindow()528 void CameraDevice::initHalPreviewWindow()
529 {
530     mHalPreviewWindow.cancel_buffer = sCancelBuffer;
531     mHalPreviewWindow.lock_buffer = sLockBuffer;
532     mHalPreviewWindow.dequeue_buffer = sDequeueBuffer;
533     mHalPreviewWindow.enqueue_buffer = sEnqueueBuffer;
534     mHalPreviewWindow.set_buffer_count = sSetBufferCount;
535     mHalPreviewWindow.set_buffers_geometry = sSetBuffersGeometry;
536     mHalPreviewWindow.set_crop = sSetCrop;
537     mHalPreviewWindow.set_timestamp = sSetTimestamp;
538     mHalPreviewWindow.set_usage = sSetUsage;
539     mHalPreviewWindow.set_swap_interval = sSetSwapInterval;
540 
541     mHalPreviewWindow.get_min_undequeued_buffer_count =
542             sGetMinUndequeuedBufferCount;
543 }
544 
545 // Methods from ::android::hardware::camera::device::V1_0::ICameraDevice follow.
getResourceCost(getResourceCost_cb _hidl_cb)546 Return<void> CameraDevice::getResourceCost(getResourceCost_cb _hidl_cb) {
547     Status status = initStatus();
548     CameraResourceCost resCost;
549     if (status == Status::OK) {
550         int cost = 100;
551         std::vector<std::string> conflicting_devices;
552         struct camera_info info;
553 
554         // If using post-2.4 module version, query the cost + conflicting devices from the HAL
555         if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4) {
556             int ret = mModule->getCameraInfo(mCameraIdInt, &info);
557             if (ret == OK) {
558                 cost = info.resource_cost;
559                 for (size_t i = 0; i < info.conflicting_devices_length; i++) {
560                     std::string cameraId(info.conflicting_devices[i]);
561                     for (const auto& pair : mCameraDeviceNames) {
562                         if (cameraId == pair.first) {
563                             conflicting_devices.push_back(pair.second);
564                         }
565                     }
566                 }
567             } else {
568                 status = Status::INTERNAL_ERROR;
569             }
570         }
571 
572         if (status == Status::OK) {
573             resCost.resourceCost = cost;
574             resCost.conflictingDevices.resize(conflicting_devices.size());
575             for (size_t i = 0; i < conflicting_devices.size(); i++) {
576                 resCost.conflictingDevices[i] = conflicting_devices[i];
577                 ALOGV("CamDevice %s is conflicting with camDevice %s",
578                         mCameraId.c_str(), resCost.conflictingDevices[i].c_str());
579             }
580         }
581     }
582     _hidl_cb(status, resCost);
583     return Void();
584 }
585 
getCameraInfo(getCameraInfo_cb _hidl_cb)586 Return<void> CameraDevice::getCameraInfo(getCameraInfo_cb _hidl_cb) {
587     Status status = initStatus();
588     CameraInfo cameraInfo;
589     if (status == Status::OK) {
590         struct camera_info info;
591         int ret = mModule->getCameraInfo(mCameraIdInt, &info);
592         if (ret == OK) {
593             cameraInfo.facing = (CameraFacing) info.facing;
594             // Device 1.0 does not support external camera facing.
595             // The closest approximation would be front camera.
596             if (cameraInfo.facing == CameraFacing::EXTERNAL) {
597                 cameraInfo.facing = CameraFacing::FRONT;
598             }
599             cameraInfo.orientation = info.orientation;
600         } else {
601             ALOGE("%s: get camera info failed!", __FUNCTION__);
602             status = Status::INTERNAL_ERROR;
603         }
604     }
605     _hidl_cb(status, cameraInfo);
606     return Void();
607 }
608 
setTorchMode(TorchMode mode)609 Return<Status> CameraDevice::setTorchMode(TorchMode mode) {
610     if (!mModule->isSetTorchModeSupported()) {
611         return Status::METHOD_NOT_SUPPORTED;
612     }
613 
614     Status status = initStatus();
615     if (status == Status::OK) {
616         bool enable = (mode == TorchMode::ON) ? true : false;
617         status = getHidlStatus(mModule->setTorchMode(mCameraId.c_str(), enable));
618     }
619     return status;
620 }
621 
dumpState(const hidl_handle & handle)622 Return<Status> CameraDevice::dumpState(const hidl_handle& handle) {
623     Mutex::Autolock _l(mLock);
624     if (handle.getNativeHandle() == nullptr) {
625         ALOGE("%s: handle must not be null", __FUNCTION__);
626         return Status::ILLEGAL_ARGUMENT;
627     }
628     if (handle->numFds != 1 || handle->numInts != 0) {
629         ALOGE("%s: handle must contain 1 FD and 0 integers! Got %d FDs and %d ints",
630                 __FUNCTION__, handle->numFds, handle->numInts);
631         return Status::ILLEGAL_ARGUMENT;
632     }
633     int fd = handle->data[0];
634 
635     if (mDevice != nullptr) {
636         if (mDevice->ops->dump) { // It's fine if the HAL doesn't implement dump()
637             return getHidlStatus(mDevice->ops->dump(mDevice, fd));
638         }
639     }
640     return Status::OK;
641 }
642 
open(const sp<ICameraDeviceCallback> & callback)643 Return<Status> CameraDevice::open(const sp<ICameraDeviceCallback>& callback) {
644     ALOGI("Opening camera %s", mCameraId.c_str());
645     Mutex::Autolock _l(mLock);
646 
647     camera_info info;
648     status_t res = mModule->getCameraInfo(mCameraIdInt, &info);
649     if (res != OK) {
650         ALOGE("Could not get camera info: %s: %d", mCameraId.c_str(), res);
651         return getHidlStatus(res);
652     }
653 
654     int rc = OK;
655     if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 &&
656         info.device_version > CAMERA_DEVICE_API_VERSION_1_0) {
657         // Open higher version camera device as HAL1.0 device.
658         rc = mModule->openLegacy(mCameraId.c_str(),
659                                  CAMERA_DEVICE_API_VERSION_1_0,
660                                  (hw_device_t **)&mDevice);
661     } else {
662         rc = mModule->open(mCameraId.c_str(), (hw_device_t **)&mDevice);
663     }
664     if (rc != OK) {
665         mDevice = nullptr;
666         ALOGE("Could not open camera %s: %d", mCameraId.c_str(), rc);
667         return getHidlStatus(rc);
668     }
669 
670     initHalPreviewWindow();
671     mDeviceCallback = callback;
672 
673     if (mDevice->ops->set_callbacks) {
674         mDevice->ops->set_callbacks(mDevice,
675                 sNotifyCb, sDataCb, sDataCbTimestamp, sGetMemory, this);
676     }
677 
678     return getHidlStatus(rc);
679 }
680 
setPreviewWindow(const sp<ICameraDevicePreviewCallback> & window)681 Return<Status> CameraDevice::setPreviewWindow(const sp<ICameraDevicePreviewCallback>& window) {
682     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
683     Mutex::Autolock _l(mLock);
684     if (!mDevice) {
685         ALOGE("%s called while camera is not opened", __FUNCTION__);
686         return Status::OPERATION_NOT_SUPPORTED;
687     }
688 
689     mHalPreviewWindow.mPreviewCallback = window;
690     if (mDevice->ops->set_preview_window) {
691         return getHidlStatus(mDevice->ops->set_preview_window(mDevice,
692                 (window == nullptr) ? nullptr : &mHalPreviewWindow));
693     }
694     return Status::INTERNAL_ERROR; // HAL should provide set_preview_window
695 }
696 
enableMsgType(uint32_t msgType)697 Return<void> CameraDevice::enableMsgType(uint32_t msgType) {
698     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
699     Mutex::Autolock _l(mLock);
700     if (!mDevice) {
701         ALOGE("%s called while camera is not opened", __FUNCTION__);
702         return Void();
703     }
704     if (mDevice->ops->enable_msg_type) {
705         mDevice->ops->enable_msg_type(mDevice, msgType);
706     }
707     return Void();
708 }
709 
disableMsgType(uint32_t msgType)710 Return<void> CameraDevice::disableMsgType(uint32_t msgType) {
711     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
712     Mutex::Autolock _l(mLock);
713     if (!mDevice) {
714         ALOGE("%s called while camera is not opened", __FUNCTION__);
715         return Void();
716     }
717     if (mDevice->ops->disable_msg_type) {
718         mDevice->ops->disable_msg_type(mDevice, msgType);
719     }
720     return Void();
721 }
722 
msgTypeEnabled(uint32_t msgType)723 Return<bool> CameraDevice::msgTypeEnabled(uint32_t msgType) {
724     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
725     Mutex::Autolock _l(mLock);
726     if (!mDevice) {
727         ALOGE("%s called while camera is not opened", __FUNCTION__);
728         return false;
729     }
730     if (mDevice->ops->msg_type_enabled) {
731         return mDevice->ops->msg_type_enabled(mDevice, msgType);
732     }
733     return false;
734 }
735 
startPreview()736 Return<Status> CameraDevice::startPreview() {
737     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
738     Mutex::Autolock _l(mLock);
739     if (!mDevice) {
740         ALOGE("%s called while camera is not opened", __FUNCTION__);
741         return Status::OPERATION_NOT_SUPPORTED;
742     }
743     if (mDevice->ops->start_preview) {
744         return getHidlStatus(mDevice->ops->start_preview(mDevice));
745     }
746     return Status::INTERNAL_ERROR; // HAL should provide start_preview
747 }
748 
stopPreview()749 Return<void> CameraDevice::stopPreview() {
750     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
751     Mutex::Autolock _l(mLock);
752     if (!mDevice) {
753         ALOGE("%s called while camera is not opened", __FUNCTION__);
754         return Void();
755     }
756     if (mDevice->ops->stop_preview) {
757         mDevice->ops->stop_preview(mDevice);
758     }
759     return Void();
760 }
761 
previewEnabled()762 Return<bool> CameraDevice::previewEnabled() {
763     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
764     Mutex::Autolock _l(mLock);
765     if (!mDevice) {
766         ALOGE("%s called while camera is not opened", __FUNCTION__);
767         return false;
768     }
769     if (mDevice->ops->preview_enabled) {
770         return mDevice->ops->preview_enabled(mDevice);
771     }
772     return false;
773 }
774 
storeMetaDataInBuffers(bool enable)775 Return<Status> CameraDevice::storeMetaDataInBuffers(bool enable) {
776     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
777     Mutex::Autolock _l(mLock);
778     if (!mDevice) {
779         ALOGE("%s called while camera is not opened", __FUNCTION__);
780         return Status::OPERATION_NOT_SUPPORTED;
781     }
782     if (mDevice->ops->store_meta_data_in_buffers) {
783         status_t s = mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
784         if (s == OK && enable) {
785             mMetadataMode = true;
786         }
787         return getHidlStatus(s);
788     }
789     return enable ? Status::ILLEGAL_ARGUMENT : Status::OK;
790 }
791 
startRecording()792 Return<Status> CameraDevice::startRecording() {
793     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
794     Mutex::Autolock _l(mLock);
795     if (!mDevice) {
796         ALOGE("%s called while camera is not opened", __FUNCTION__);
797         return Status::OPERATION_NOT_SUPPORTED;
798     }
799     if (mDevice->ops->start_recording) {
800         return getHidlStatus(mDevice->ops->start_recording(mDevice));
801     }
802     return Status::ILLEGAL_ARGUMENT;
803 }
804 
stopRecording()805 Return<void> CameraDevice::stopRecording() {
806     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
807     Mutex::Autolock _l(mLock);
808     if (!mDevice) {
809         ALOGE("%s called while camera is not opened", __FUNCTION__);
810         return Void();
811     }
812     if (mDevice->ops->stop_recording) {
813         mDevice->ops->stop_recording(mDevice);
814     }
815     return Void();
816 }
817 
recordingEnabled()818 Return<bool> CameraDevice::recordingEnabled() {
819     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
820     Mutex::Autolock _l(mLock);
821     if (!mDevice) {
822         ALOGE("%s called while camera is not opened", __FUNCTION__);
823         return false;
824     }
825     if (mDevice->ops->recording_enabled) {
826         return mDevice->ops->recording_enabled(mDevice);
827     }
828     return false;
829 }
830 
releaseRecordingFrameLocked(uint32_t memId,uint32_t bufferIndex,const native_handle_t * handle)831 void CameraDevice::releaseRecordingFrameLocked(
832         uint32_t memId, uint32_t bufferIndex, const native_handle_t* handle) {
833     if (!mDevice) {
834         ALOGE("%s called while camera is not opened", __FUNCTION__);
835         return;
836     }
837     if (mDevice->ops->release_recording_frame) {
838         CameraHeapMemory* camMemory;
839         {
840             Mutex::Autolock _l(mMemoryMapLock);
841             auto it = mMemoryMap.find(memId);
842             if (it == mMemoryMap.end() || it->second == nullptr) {
843                 ALOGE("%s unknown memoryId %d", __FUNCTION__, memId);
844                 return;
845             }
846             camMemory = it->second;
847         }
848         if (bufferIndex >= camMemory->mNumBufs) {
849             ALOGE("%s: bufferIndex %d exceeds number of buffers %d",
850                     __FUNCTION__, bufferIndex, camMemory->mNumBufs);
851             return;
852         }
853         void *data = ((uint8_t *) camMemory->mHidlHeapMemData) + bufferIndex * camMemory->mBufSize;
854         if (handle) {
855             VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*) data;
856             if (md->eType == kMetadataBufferTypeNativeHandleSource) {
857                 // Input handle will be closed by HIDL transport later, so clone it
858                 // HAL implementation is responsible to close/delete the clone
859                 native_handle_t* clone = native_handle_clone(handle);
860                 if (!clone) {
861                     ALOGE("%s: failed to clone buffer %p", __FUNCTION__, handle);
862                     return;
863                 }
864                 md->pHandle = clone;
865             } else {
866                 ALOGE("%s:Malform VideoNativeHandleMetadata at memId %d, bufferId %d",
867                         __FUNCTION__, memId, bufferIndex);
868                 return;
869             }
870         }
871         mDevice->ops->release_recording_frame(mDevice, data);
872     }
873 }
874 
releaseRecordingFrame(uint32_t memId,uint32_t bufferIndex)875 Return<void> CameraDevice::releaseRecordingFrame(uint32_t memId, uint32_t bufferIndex) {
876     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
877     Mutex::Autolock _l(mLock);
878     releaseRecordingFrameLocked(memId, bufferIndex, nullptr);
879     return Void();
880 }
881 
releaseRecordingFrameHandle(uint32_t memId,uint32_t bufferIndex,const hidl_handle & frame)882 Return<void> CameraDevice::releaseRecordingFrameHandle(
883         uint32_t memId, uint32_t bufferIndex, const hidl_handle& frame) {
884     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
885     Mutex::Autolock _l(mLock);
886     releaseRecordingFrameLocked(
887             memId, bufferIndex, frame.getNativeHandle());
888     return Void();
889 }
890 
releaseRecordingFrameHandleBatch(const hidl_vec<VideoFrameMessage> & msgs)891 Return<void> CameraDevice::releaseRecordingFrameHandleBatch(
892         const hidl_vec<VideoFrameMessage>& msgs) {
893     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
894     Mutex::Autolock _l(mLock);
895     for (auto& msg : msgs) {
896         releaseRecordingFrameLocked(
897                 msg.data, msg.bufferIndex, msg.frameData.getNativeHandle());
898     }
899     return Void();
900 }
901 
autoFocus()902 Return<Status> CameraDevice::autoFocus() {
903     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
904     Mutex::Autolock _l(mLock);
905     if (!mDevice) {
906         ALOGE("%s called while camera is not opened", __FUNCTION__);
907         return Status::OPERATION_NOT_SUPPORTED;
908     }
909     if (mDevice->ops->auto_focus) {
910         return getHidlStatus(mDevice->ops->auto_focus(mDevice));
911     }
912     return Status::ILLEGAL_ARGUMENT;
913 }
914 
cancelAutoFocus()915 Return<Status> CameraDevice::cancelAutoFocus() {
916     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
917     Mutex::Autolock _l(mLock);
918     if (!mDevice) {
919         ALOGE("%s called while camera is not opened", __FUNCTION__);
920         return Status::OPERATION_NOT_SUPPORTED;
921     }
922     if (mDevice->ops->cancel_auto_focus) {
923         return getHidlStatus(mDevice->ops->cancel_auto_focus(mDevice));
924     }
925     return Status::ILLEGAL_ARGUMENT;
926 }
927 
takePicture()928 Return<Status> CameraDevice::takePicture() {
929     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
930     Mutex::Autolock _l(mLock);
931     if (!mDevice) {
932         ALOGE("%s called while camera is not opened", __FUNCTION__);
933         return Status::OPERATION_NOT_SUPPORTED;
934     }
935     if (mDevice->ops->take_picture) {
936         return getHidlStatus(mDevice->ops->take_picture(mDevice));
937     }
938     return Status::ILLEGAL_ARGUMENT;
939 }
940 
cancelPicture()941 Return<Status> CameraDevice::cancelPicture() {
942     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
943     Mutex::Autolock _l(mLock);
944     if (!mDevice) {
945         ALOGE("%s called while camera is not opened", __FUNCTION__);
946         return Status::OPERATION_NOT_SUPPORTED;
947     }
948     if (mDevice->ops->cancel_picture) {
949         return getHidlStatus(mDevice->ops->cancel_picture(mDevice));
950     }
951     return Status::ILLEGAL_ARGUMENT;
952 }
953 
setParameters(const hidl_string & params)954 Return<Status> CameraDevice::setParameters(const hidl_string& params) {
955     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
956     Mutex::Autolock _l(mLock);
957     if (!mDevice) {
958         ALOGE("%s called while camera is not opened", __FUNCTION__);
959         return Status::OPERATION_NOT_SUPPORTED;
960     }
961     if (mDevice->ops->set_parameters) {
962         return getHidlStatus(mDevice->ops->set_parameters(mDevice, params.c_str()));
963     }
964     return Status::ILLEGAL_ARGUMENT;
965 }
966 
getParameters(getParameters_cb _hidl_cb)967 Return<void> CameraDevice::getParameters(getParameters_cb _hidl_cb) {
968     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
969     Mutex::Autolock _l(mLock);
970     hidl_string outStr;
971     if (!mDevice) {
972         ALOGE("%s called while camera is not opened", __FUNCTION__);
973         _hidl_cb(outStr);
974         return Void();
975     }
976     if (mDevice->ops->get_parameters) {
977         char *temp = mDevice->ops->get_parameters(mDevice);
978         outStr = temp;
979         if (mDevice->ops->put_parameters) {
980             mDevice->ops->put_parameters(mDevice, temp);
981         } else {
982             free(temp);
983         }
984     }
985     _hidl_cb(outStr);
986     return Void();
987 }
988 
sendCommand(CommandType cmd,int32_t arg1,int32_t arg2)989 Return<Status> CameraDevice::sendCommand(CommandType cmd, int32_t arg1, int32_t arg2) {
990     ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
991     Mutex::Autolock _l(mLock);
992     if (!mDevice) {
993         ALOGE("%s called while camera is not opened", __FUNCTION__);
994         return Status::OPERATION_NOT_SUPPORTED;
995     }
996     if (mDevice->ops->send_command) {
997         return getHidlStatus(mDevice->ops->send_command(mDevice, (int32_t) cmd, arg1, arg2));
998     }
999     return Status::ILLEGAL_ARGUMENT;
1000 }
1001 
close()1002 Return<void> CameraDevice::close() {
1003     Mutex::Autolock _l(mLock);
1004     closeLocked();
1005     return Void();
1006 }
1007 
closeLocked()1008 void CameraDevice::closeLocked() {
1009     ALOGI("Closing camera %s", mCameraId.c_str());
1010     if(mDevice) {
1011         int rc = mDevice->common.close(&mDevice->common);
1012         if (rc != OK) {
1013             ALOGE("Could not close camera %s: %d", mCameraId.c_str(), rc);
1014         }
1015         mDevice = nullptr;
1016     }
1017 }
1018 
1019 }  // namespace implementation
1020 }  // namespace V1_0
1021 }  // namespace device
1022 }  // namespace camera
1023 }  // namespace hardware
1024 }  // namespace android
1025