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