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