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