Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 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 | #define LOG_TAG "CameraHardwareInterface" |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | |
| 19 | #include <inttypes.h> |
| 20 | #include "CameraHardwareInterface.h" |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | using namespace hardware::camera::device::V1_0; |
| 25 | using namespace hardware::camera::common::V1_0; |
| 26 | using hardware::hidl_handle; |
| 27 | |
| 28 | CameraHardwareInterface::~CameraHardwareInterface() |
| 29 | { |
| 30 | ALOGI("Destroying camera %s", mName.string()); |
| 31 | if (mDevice) { |
| 32 | int rc = mDevice->common.close(&mDevice->common); |
| 33 | if (rc != OK) |
| 34 | ALOGE("Could not close camera %s: %d", mName.string(), rc); |
| 35 | } |
| 36 | if (mHidlDevice != nullptr) { |
| 37 | mHidlDevice->close(); |
| 38 | mHidlDevice.clear(); |
| 39 | cleanupCirculatingBuffers(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | status_t CameraHardwareInterface::initialize(CameraModule *module) |
| 44 | { |
| 45 | if (mHidlDevice != nullptr) { |
| 46 | ALOGE("%s: camera hardware interface has been initialized to HIDL path!", __FUNCTION__); |
| 47 | return INVALID_OPERATION; |
| 48 | } |
| 49 | ALOGI("Opening camera %s", mName.string()); |
| 50 | camera_info info; |
| 51 | status_t res = module->getCameraInfo(atoi(mName.string()), &info); |
| 52 | if (res != OK) { |
| 53 | return res; |
| 54 | } |
| 55 | |
| 56 | int rc = OK; |
| 57 | if (module->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 && |
| 58 | info.device_version > CAMERA_DEVICE_API_VERSION_1_0) { |
| 59 | // Open higher version camera device as HAL1.0 device. |
| 60 | rc = module->openLegacy(mName.string(), |
| 61 | CAMERA_DEVICE_API_VERSION_1_0, |
| 62 | (hw_device_t **)&mDevice); |
| 63 | } else { |
| 64 | rc = module->open(mName.string(), (hw_device_t **)&mDevice); |
| 65 | } |
| 66 | if (rc != OK) { |
| 67 | ALOGE("Could not open camera %s: %d", mName.string(), rc); |
| 68 | return rc; |
| 69 | } |
| 70 | initHalPreviewWindow(); |
| 71 | return rc; |
| 72 | } |
| 73 | |
| 74 | status_t CameraHardwareInterface::initialize(sp<CameraProviderManager> manager) { |
| 75 | if (mDevice) { |
| 76 | ALOGE("%s: camera hardware interface has been initialized to libhardware path!", |
| 77 | __FUNCTION__); |
| 78 | return INVALID_OPERATION; |
| 79 | } |
| 80 | |
| 81 | ALOGI("Opening camera %s", mName.string()); |
| 82 | |
Steven Moreland | 5ff9c91 | 2017-03-09 23:13:00 -0800 | [diff] [blame] | 83 | status_t ret = manager->openSession(mName.string(), this, &mHidlDevice); |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 84 | if (ret != OK) { |
| 85 | ALOGE("%s: openSession failed! %s (%d)", __FUNCTION__, strerror(-ret), ret); |
| 86 | } |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | status_t CameraHardwareInterface::setPreviewScalingMode(int scalingMode) |
| 91 | { |
| 92 | int rc = OK; |
| 93 | mPreviewScalingMode = scalingMode; |
| 94 | if (mPreviewWindow != nullptr) { |
| 95 | rc = native_window_set_scaling_mode(mPreviewWindow.get(), |
| 96 | scalingMode); |
| 97 | } |
| 98 | return rc; |
| 99 | } |
| 100 | |
| 101 | status_t CameraHardwareInterface::setPreviewTransform(int transform) { |
| 102 | int rc = OK; |
| 103 | mPreviewTransform = transform; |
| 104 | if (mPreviewWindow != nullptr) { |
| 105 | rc = native_window_set_buffers_transform(mPreviewWindow.get(), |
| 106 | mPreviewTransform); |
| 107 | } |
| 108 | return rc; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Implementation of android::hardware::camera::device::V1_0::ICameraDeviceCallback |
| 113 | */ |
| 114 | hardware::Return<void> CameraHardwareInterface::notifyCallback( |
| 115 | NotifyCallbackMsg msgType, int32_t ext1, int32_t ext2) { |
| 116 | sNotifyCb((int32_t) msgType, ext1, ext2, (void*) this); |
| 117 | return hardware::Void(); |
| 118 | } |
| 119 | |
| 120 | hardware::Return<uint32_t> CameraHardwareInterface::registerMemory( |
| 121 | const hardware::hidl_handle& descriptor, |
| 122 | uint32_t bufferSize, uint32_t bufferCount) { |
| 123 | if (descriptor->numFds != 1) { |
| 124 | ALOGE("%s: camera memory descriptor has numFds %d (expect 1)", |
| 125 | __FUNCTION__, descriptor->numFds); |
| 126 | return 0; |
| 127 | } |
| 128 | if (descriptor->data[0] < 0) { |
| 129 | ALOGE("%s: camera memory descriptor has FD %d (expect >= 0)", |
| 130 | __FUNCTION__, descriptor->data[0]); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | camera_memory_t* mem = sGetMemory(descriptor->data[0], bufferSize, bufferCount, this); |
| 135 | sp<CameraHeapMemory> camMem(static_cast<CameraHeapMemory *>(mem->handle)); |
| 136 | int memPoolId = camMem->mHeap->getHeapID(); |
| 137 | if (memPoolId < 0) { |
| 138 | ALOGE("%s: CameraHeapMemory has FD %d (expect >= 0)", __FUNCTION__, memPoolId); |
| 139 | return 0; |
| 140 | } |
| 141 | mHidlMemPoolMap.insert(std::make_pair(memPoolId, mem)); |
| 142 | return memPoolId; |
| 143 | } |
| 144 | |
| 145 | hardware::Return<void> CameraHardwareInterface::unregisterMemory(uint32_t memId) { |
| 146 | if (mHidlMemPoolMap.count(memId) == 0) { |
| 147 | ALOGE("%s: memory pool ID %d not found", __FUNCTION__, memId); |
| 148 | return hardware::Void(); |
| 149 | } |
| 150 | camera_memory_t* mem = mHidlMemPoolMap.at(memId); |
| 151 | sPutMemory(mem); |
| 152 | mHidlMemPoolMap.erase(memId); |
| 153 | return hardware::Void(); |
| 154 | } |
| 155 | |
| 156 | hardware::Return<void> CameraHardwareInterface::dataCallback( |
| 157 | DataCallbackMsg msgType, uint32_t data, uint32_t bufferIndex, |
| 158 | const hardware::camera::device::V1_0::CameraFrameMetadata& metadata) { |
| 159 | if (mHidlMemPoolMap.count(data) == 0) { |
| 160 | ALOGE("%s: memory pool ID %d not found", __FUNCTION__, data); |
| 161 | return hardware::Void(); |
| 162 | } |
| 163 | camera_frame_metadata_t md; |
| 164 | md.number_of_faces = metadata.faces.size(); |
| 165 | md.faces = (camera_face_t*) metadata.faces.data(); |
| 166 | sDataCb((int32_t) msgType, mHidlMemPoolMap.at(data), bufferIndex, &md, this); |
| 167 | return hardware::Void(); |
| 168 | } |
| 169 | |
| 170 | hardware::Return<void> CameraHardwareInterface::dataCallbackTimestamp( |
| 171 | DataCallbackMsg msgType, uint32_t data, |
| 172 | uint32_t bufferIndex, int64_t timestamp) { |
| 173 | if (mHidlMemPoolMap.count(data) == 0) { |
| 174 | ALOGE("%s: memory pool ID %d not found", __FUNCTION__, data); |
| 175 | return hardware::Void(); |
| 176 | } |
| 177 | sDataCbTimestamp(timestamp, (int32_t) msgType, mHidlMemPoolMap.at(data), bufferIndex, this); |
| 178 | return hardware::Void(); |
| 179 | } |
| 180 | |
| 181 | hardware::Return<void> CameraHardwareInterface::handleCallbackTimestamp( |
| 182 | DataCallbackMsg msgType, const hidl_handle& frameData, uint32_t data, |
| 183 | uint32_t bufferIndex, int64_t timestamp) { |
| 184 | if (mHidlMemPoolMap.count(data) == 0) { |
| 185 | ALOGE("%s: memory pool ID %d not found", __FUNCTION__, data); |
| 186 | return hardware::Void(); |
| 187 | } |
| 188 | sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(mHidlMemPoolMap.at(data)->handle)); |
| 189 | VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*) |
| 190 | mem->mBuffers[bufferIndex]->pointer(); |
| 191 | md->pHandle = const_cast<native_handle_t*>(frameData.getNativeHandle()); |
| 192 | sDataCbTimestamp(timestamp, (int32_t) msgType, mHidlMemPoolMap.at(data), bufferIndex, this); |
| 193 | return hardware::Void(); |
| 194 | } |
| 195 | |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 196 | hardware::Return<void> CameraHardwareInterface::handleCallbackTimestampBatch( |
| 197 | DataCallbackMsg msgType, |
| 198 | const hardware::hidl_vec<hardware::camera::device::V1_0::HandleTimestampMessage>& messages) { |
| 199 | std::vector<android::HandleTimestampMessage> msgs; |
| 200 | msgs.reserve(messages.size()); |
| 201 | |
| 202 | for (const auto& hidl_msg : messages) { |
| 203 | if (mHidlMemPoolMap.count(hidl_msg.data) == 0) { |
| 204 | ALOGE("%s: memory pool ID %d not found", __FUNCTION__, hidl_msg.data); |
| 205 | return hardware::Void(); |
| 206 | } |
| 207 | sp<CameraHeapMemory> mem( |
| 208 | static_cast<CameraHeapMemory *>(mHidlMemPoolMap.at(hidl_msg.data)->handle)); |
| 209 | |
| 210 | if (hidl_msg.bufferIndex >= mem->mNumBufs) { |
| 211 | ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__, |
| 212 | hidl_msg.bufferIndex, mem->mNumBufs); |
| 213 | return hardware::Void(); |
| 214 | } |
| 215 | VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*) |
| 216 | mem->mBuffers[hidl_msg.bufferIndex]->pointer(); |
| 217 | md->pHandle = const_cast<native_handle_t*>(hidl_msg.frameData.getNativeHandle()); |
| 218 | |
| 219 | msgs.push_back({hidl_msg.timestamp, mem->mBuffers[hidl_msg.bufferIndex]}); |
| 220 | } |
| 221 | |
| 222 | mDataCbTimestampBatch((int32_t) msgType, msgs, mCbUser); |
| 223 | return hardware::Void(); |
| 224 | } |
| 225 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 226 | std::pair<bool, uint64_t> CameraHardwareInterface::getBufferId( |
| 227 | ANativeWindowBuffer* anb) { |
| 228 | std::lock_guard<std::mutex> lock(mBufferIdMapLock); |
| 229 | |
| 230 | buffer_handle_t& buf = anb->handle; |
| 231 | auto it = mBufferIdMap.find(buf); |
| 232 | if (it == mBufferIdMap.end()) { |
| 233 | uint64_t bufId = mNextBufferId++; |
| 234 | mBufferIdMap[buf] = bufId; |
| 235 | mReversedBufMap[bufId] = anb; |
| 236 | return std::make_pair(true, bufId); |
| 237 | } else { |
| 238 | return std::make_pair(false, it->second); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void CameraHardwareInterface::cleanupCirculatingBuffers() { |
| 243 | std::lock_guard<std::mutex> lock(mBufferIdMapLock); |
| 244 | mBufferIdMap.clear(); |
| 245 | mReversedBufMap.clear(); |
| 246 | } |
| 247 | |
| 248 | hardware::Return<void> |
| 249 | CameraHardwareInterface::dequeueBuffer(dequeueBuffer_cb _hidl_cb) { |
| 250 | ANativeWindow *a = mPreviewWindow.get(); |
| 251 | if (a == nullptr) { |
| 252 | ALOGE("%s: preview window is null", __FUNCTION__); |
| 253 | return hardware::Void(); |
| 254 | } |
| 255 | ANativeWindowBuffer* anb; |
| 256 | int rc = native_window_dequeue_buffer_and_wait(a, &anb); |
| 257 | Status s = Status::INTERNAL_ERROR; |
| 258 | uint64_t bufferId = 0; |
| 259 | uint32_t stride = 0; |
| 260 | hidl_handle buf = nullptr; |
| 261 | if (rc == OK) { |
| 262 | s = Status::OK; |
| 263 | auto pair = getBufferId(anb); |
| 264 | buf = (pair.first) ? anb->handle : nullptr; |
| 265 | bufferId = pair.second; |
| 266 | stride = anb->stride; |
| 267 | } |
| 268 | |
| 269 | _hidl_cb(s, bufferId, buf, stride); |
| 270 | return hardware::Void(); |
| 271 | } |
| 272 | |
| 273 | hardware::Return<Status> |
| 274 | CameraHardwareInterface::enqueueBuffer(uint64_t bufferId) { |
| 275 | ANativeWindow *a = mPreviewWindow.get(); |
| 276 | if (a == nullptr) { |
| 277 | ALOGE("%s: preview window is null", __FUNCTION__); |
| 278 | return Status::INTERNAL_ERROR; |
| 279 | } |
| 280 | if (mReversedBufMap.count(bufferId) == 0) { |
| 281 | ALOGE("%s: bufferId %" PRIu64 " not found", __FUNCTION__, bufferId); |
| 282 | return Status::ILLEGAL_ARGUMENT; |
| 283 | } |
| 284 | int rc = a->queueBuffer(a, mReversedBufMap.at(bufferId), -1); |
| 285 | if (rc == 0) { |
| 286 | return Status::OK; |
| 287 | } |
| 288 | return Status::INTERNAL_ERROR; |
| 289 | } |
| 290 | |
| 291 | hardware::Return<Status> |
| 292 | CameraHardwareInterface::cancelBuffer(uint64_t bufferId) { |
| 293 | ANativeWindow *a = mPreviewWindow.get(); |
| 294 | if (a == nullptr) { |
| 295 | ALOGE("%s: preview window is null", __FUNCTION__); |
| 296 | return Status::INTERNAL_ERROR; |
| 297 | } |
| 298 | if (mReversedBufMap.count(bufferId) == 0) { |
| 299 | ALOGE("%s: bufferId %" PRIu64 " not found", __FUNCTION__, bufferId); |
| 300 | return Status::ILLEGAL_ARGUMENT; |
| 301 | } |
| 302 | int rc = a->cancelBuffer(a, mReversedBufMap.at(bufferId), -1); |
| 303 | if (rc == 0) { |
| 304 | return Status::OK; |
| 305 | } |
| 306 | return Status::INTERNAL_ERROR; |
| 307 | } |
| 308 | |
| 309 | hardware::Return<Status> |
| 310 | CameraHardwareInterface::setBufferCount(uint32_t count) { |
| 311 | ANativeWindow *a = mPreviewWindow.get(); |
| 312 | if (a != nullptr) { |
| 313 | // Workaround for b/27039775 |
| 314 | // Previously, setting the buffer count would reset the buffer |
| 315 | // queue's flag that allows for all buffers to be dequeued on the |
| 316 | // producer side, instead of just the producer's declared max count, |
| 317 | // if no filled buffers have yet been queued by the producer. This |
| 318 | // reset no longer happens, but some HALs depend on this behavior, |
| 319 | // so it needs to be maintained for HAL backwards compatibility. |
| 320 | // Simulate the prior behavior by disconnecting/reconnecting to the |
| 321 | // window and setting the values again. This has the drawback of |
| 322 | // actually causing memory reallocation, which may not have happened |
| 323 | // in the past. |
| 324 | native_window_api_disconnect(a, NATIVE_WINDOW_API_CAMERA); |
| 325 | native_window_api_connect(a, NATIVE_WINDOW_API_CAMERA); |
| 326 | if (mPreviewScalingMode != NOT_SET) { |
| 327 | native_window_set_scaling_mode(a, mPreviewScalingMode); |
| 328 | } |
| 329 | if (mPreviewTransform != NOT_SET) { |
| 330 | native_window_set_buffers_transform(a, mPreviewTransform); |
| 331 | } |
| 332 | if (mPreviewWidth != NOT_SET) { |
| 333 | native_window_set_buffers_dimensions(a, |
| 334 | mPreviewWidth, mPreviewHeight); |
| 335 | native_window_set_buffers_format(a, mPreviewFormat); |
| 336 | } |
| 337 | if (mPreviewUsage != 0) { |
| 338 | native_window_set_usage(a, mPreviewUsage); |
| 339 | } |
| 340 | if (mPreviewSwapInterval != NOT_SET) { |
| 341 | a->setSwapInterval(a, mPreviewSwapInterval); |
| 342 | } |
| 343 | if (mPreviewCrop.left != NOT_SET) { |
| 344 | native_window_set_crop(a, &(mPreviewCrop)); |
| 345 | } |
| 346 | } |
| 347 | int rc = native_window_set_buffer_count(a, count); |
| 348 | if (rc == OK) { |
| 349 | cleanupCirculatingBuffers(); |
| 350 | return Status::OK; |
| 351 | } |
| 352 | return Status::INTERNAL_ERROR; |
| 353 | } |
| 354 | |
| 355 | hardware::Return<Status> |
| 356 | CameraHardwareInterface::setBuffersGeometry( |
| 357 | uint32_t w, uint32_t h, hardware::graphics::common::V1_0::PixelFormat format) { |
| 358 | Status s = Status::INTERNAL_ERROR; |
| 359 | ANativeWindow *a = mPreviewWindow.get(); |
| 360 | if (a == nullptr) { |
| 361 | ALOGE("%s: preview window is null", __FUNCTION__); |
| 362 | return s; |
| 363 | } |
| 364 | mPreviewWidth = w; |
| 365 | mPreviewHeight = h; |
| 366 | mPreviewFormat = (int) format; |
| 367 | int rc = native_window_set_buffers_dimensions(a, w, h); |
| 368 | if (rc == OK) { |
| 369 | rc = native_window_set_buffers_format(a, mPreviewFormat); |
| 370 | } |
| 371 | if (rc == OK) { |
| 372 | cleanupCirculatingBuffers(); |
| 373 | s = Status::OK; |
| 374 | } |
| 375 | return s; |
| 376 | } |
| 377 | |
| 378 | hardware::Return<Status> |
| 379 | CameraHardwareInterface::setCrop(int32_t left, int32_t top, int32_t right, int32_t bottom) { |
| 380 | Status s = Status::INTERNAL_ERROR; |
| 381 | ANativeWindow *a = mPreviewWindow.get(); |
| 382 | if (a == nullptr) { |
| 383 | ALOGE("%s: preview window is null", __FUNCTION__); |
| 384 | return s; |
| 385 | } |
| 386 | mPreviewCrop.left = left; |
| 387 | mPreviewCrop.top = top; |
| 388 | mPreviewCrop.right = right; |
| 389 | mPreviewCrop.bottom = bottom; |
| 390 | int rc = native_window_set_crop(a, &mPreviewCrop); |
| 391 | if (rc == OK) { |
| 392 | s = Status::OK; |
| 393 | } |
| 394 | return s; |
| 395 | } |
| 396 | |
| 397 | hardware::Return<Status> |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 398 | CameraHardwareInterface::setUsage(hardware::graphics::allocator::V2_0::ProducerUsage usage) { |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 399 | Status s = Status::INTERNAL_ERROR; |
| 400 | ANativeWindow *a = mPreviewWindow.get(); |
| 401 | if (a == nullptr) { |
| 402 | ALOGE("%s: preview window is null", __FUNCTION__); |
| 403 | return s; |
| 404 | } |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 405 | mPreviewUsage = (int) usage; |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 406 | int rc = native_window_set_usage(a, mPreviewUsage); |
| 407 | if (rc == OK) { |
| 408 | cleanupCirculatingBuffers(); |
| 409 | s = Status::OK; |
| 410 | } |
| 411 | return s; |
| 412 | } |
| 413 | |
| 414 | hardware::Return<Status> |
| 415 | CameraHardwareInterface::setSwapInterval(int32_t interval) { |
| 416 | Status s = Status::INTERNAL_ERROR; |
| 417 | ANativeWindow *a = mPreviewWindow.get(); |
| 418 | if (a == nullptr) { |
| 419 | ALOGE("%s: preview window is null", __FUNCTION__); |
| 420 | return s; |
| 421 | } |
| 422 | mPreviewSwapInterval = interval; |
| 423 | int rc = a->setSwapInterval(a, interval); |
| 424 | if (rc == OK) { |
| 425 | s = Status::OK; |
| 426 | } |
| 427 | return s; |
| 428 | } |
| 429 | |
| 430 | hardware::Return<void> |
| 431 | CameraHardwareInterface::getMinUndequeuedBufferCount(getMinUndequeuedBufferCount_cb _hidl_cb) { |
| 432 | ANativeWindow *a = mPreviewWindow.get(); |
| 433 | if (a == nullptr) { |
| 434 | ALOGE("%s: preview window is null", __FUNCTION__); |
| 435 | return hardware::Void(); |
| 436 | } |
| 437 | int count = 0; |
| 438 | int rc = a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &count); |
| 439 | Status s = Status::INTERNAL_ERROR; |
| 440 | if (rc == OK) { |
| 441 | s = Status::OK; |
| 442 | } |
| 443 | _hidl_cb(s, count); |
| 444 | return hardware::Void(); |
| 445 | } |
| 446 | |
| 447 | hardware::Return<Status> |
| 448 | CameraHardwareInterface::setTimestamp(int64_t timestamp) { |
| 449 | Status s = Status::INTERNAL_ERROR; |
| 450 | ANativeWindow *a = mPreviewWindow.get(); |
| 451 | if (a == nullptr) { |
| 452 | ALOGE("%s: preview window is null", __FUNCTION__); |
| 453 | return s; |
| 454 | } |
| 455 | int rc = native_window_set_buffers_timestamp(a, timestamp); |
| 456 | if (rc == OK) { |
| 457 | s = Status::OK; |
| 458 | } |
| 459 | return s; |
| 460 | } |
| 461 | |
| 462 | status_t CameraHardwareInterface::setPreviewWindow(const sp<ANativeWindow>& buf) |
| 463 | { |
| 464 | ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get()); |
| 465 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 466 | mPreviewWindow = buf; |
| 467 | if (buf != nullptr) { |
| 468 | if (mPreviewScalingMode != NOT_SET) { |
| 469 | setPreviewScalingMode(mPreviewScalingMode); |
| 470 | } |
| 471 | if (mPreviewTransform != NOT_SET) { |
| 472 | setPreviewTransform(mPreviewTransform); |
| 473 | } |
| 474 | } |
| 475 | return CameraProviderManager::mapToStatusT( |
| 476 | mHidlDevice->setPreviewWindow(buf.get() ? this : nullptr)); |
| 477 | } else if (mDevice) { |
| 478 | if (mDevice->ops->set_preview_window) { |
| 479 | mPreviewWindow = buf; |
| 480 | if (buf != nullptr) { |
| 481 | if (mPreviewScalingMode != NOT_SET) { |
| 482 | setPreviewScalingMode(mPreviewScalingMode); |
| 483 | } |
| 484 | if (mPreviewTransform != NOT_SET) { |
| 485 | setPreviewTransform(mPreviewTransform); |
| 486 | } |
| 487 | } |
| 488 | mHalPreviewWindow.user = this; |
| 489 | ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p",__FUNCTION__, |
| 490 | &mHalPreviewWindow, mHalPreviewWindow.user); |
| 491 | return mDevice->ops->set_preview_window(mDevice, |
| 492 | buf.get() ? &mHalPreviewWindow.nw : 0); |
| 493 | } |
| 494 | } |
| 495 | return INVALID_OPERATION; |
| 496 | } |
| 497 | |
| 498 | void CameraHardwareInterface::setCallbacks(notify_callback notify_cb, |
| 499 | data_callback data_cb, |
| 500 | data_callback_timestamp data_cb_timestamp, |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 501 | data_callback_timestamp_batch data_cb_timestamp_batch, |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 502 | void* user) |
| 503 | { |
| 504 | mNotifyCb = notify_cb; |
| 505 | mDataCb = data_cb; |
| 506 | mDataCbTimestamp = data_cb_timestamp; |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 507 | mDataCbTimestampBatch = data_cb_timestamp_batch; |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 508 | mCbUser = user; |
| 509 | |
| 510 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 511 | |
| 512 | if (mDevice && mDevice->ops->set_callbacks) { |
| 513 | mDevice->ops->set_callbacks(mDevice, |
| 514 | sNotifyCb, |
| 515 | sDataCb, |
| 516 | sDataCbTimestamp, |
| 517 | sGetMemory, |
| 518 | this); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | void CameraHardwareInterface::enableMsgType(int32_t msgType) |
| 523 | { |
| 524 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 525 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 526 | mHidlDevice->enableMsgType(msgType); |
| 527 | } else if (mDevice && mDevice->ops->enable_msg_type) { |
| 528 | mDevice->ops->enable_msg_type(mDevice, msgType); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | void CameraHardwareInterface::disableMsgType(int32_t msgType) |
| 533 | { |
| 534 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 535 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 536 | mHidlDevice->disableMsgType(msgType); |
| 537 | } else if (mDevice && mDevice->ops->disable_msg_type) { |
| 538 | mDevice->ops->disable_msg_type(mDevice, msgType); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | int CameraHardwareInterface::msgTypeEnabled(int32_t msgType) |
| 543 | { |
| 544 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 545 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 546 | return mHidlDevice->msgTypeEnabled(msgType); |
| 547 | } else if (mDevice && mDevice->ops->msg_type_enabled) { |
| 548 | return mDevice->ops->msg_type_enabled(mDevice, msgType); |
| 549 | } |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | status_t CameraHardwareInterface::startPreview() |
| 554 | { |
| 555 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 556 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 557 | return CameraProviderManager::mapToStatusT( |
| 558 | mHidlDevice->startPreview()); |
| 559 | } else if (mDevice && mDevice->ops->start_preview) { |
| 560 | return mDevice->ops->start_preview(mDevice); |
| 561 | } |
| 562 | return INVALID_OPERATION; |
| 563 | } |
| 564 | |
| 565 | void CameraHardwareInterface::stopPreview() |
| 566 | { |
| 567 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 568 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 569 | mHidlDevice->stopPreview(); |
| 570 | } else if (mDevice && mDevice->ops->stop_preview) { |
| 571 | mDevice->ops->stop_preview(mDevice); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | int CameraHardwareInterface::previewEnabled() |
| 576 | { |
| 577 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 578 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 579 | return mHidlDevice->previewEnabled(); |
| 580 | } else if (mDevice && mDevice->ops->preview_enabled) { |
| 581 | return mDevice->ops->preview_enabled(mDevice); |
| 582 | } |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | status_t CameraHardwareInterface::storeMetaDataInBuffers(int enable) |
| 587 | { |
| 588 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 589 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 590 | return CameraProviderManager::mapToStatusT( |
| 591 | mHidlDevice->storeMetaDataInBuffers(enable)); |
| 592 | } else if (mDevice && mDevice->ops->store_meta_data_in_buffers) { |
| 593 | return mDevice->ops->store_meta_data_in_buffers(mDevice, enable); |
| 594 | } |
| 595 | return enable ? INVALID_OPERATION: OK; |
| 596 | } |
| 597 | |
| 598 | status_t CameraHardwareInterface::startRecording() |
| 599 | { |
| 600 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 601 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 602 | return CameraProviderManager::mapToStatusT( |
| 603 | mHidlDevice->startRecording()); |
| 604 | } else if (mDevice && mDevice->ops->start_recording) { |
| 605 | return mDevice->ops->start_recording(mDevice); |
| 606 | } |
| 607 | return INVALID_OPERATION; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Stop a previously started recording. |
| 612 | */ |
| 613 | void CameraHardwareInterface::stopRecording() |
| 614 | { |
| 615 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 616 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 617 | mHidlDevice->stopRecording(); |
| 618 | } else if (mDevice && mDevice->ops->stop_recording) { |
| 619 | mDevice->ops->stop_recording(mDevice); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Returns true if recording is enabled. |
| 625 | */ |
| 626 | int CameraHardwareInterface::recordingEnabled() |
| 627 | { |
| 628 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 629 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 630 | return mHidlDevice->recordingEnabled(); |
| 631 | } else if (mDevice && mDevice->ops->recording_enabled) { |
| 632 | return mDevice->ops->recording_enabled(mDevice); |
| 633 | } |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | void CameraHardwareInterface::releaseRecordingFrame(const sp<IMemory>& mem) |
| 638 | { |
| 639 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 640 | ssize_t offset; |
| 641 | size_t size; |
| 642 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 643 | int heapId = heap->getHeapID(); |
| 644 | int bufferIndex = offset / size; |
| 645 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 646 | if (size == sizeof(VideoNativeHandleMetadata)) { |
| 647 | VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*) mem->pointer(); |
| 648 | // Caching the handle here because md->pHandle will be subject to HAL's edit |
| 649 | native_handle_t* nh = md->pHandle; |
| 650 | hidl_handle frame = nh; |
| 651 | mHidlDevice->releaseRecordingFrameHandle(heapId, bufferIndex, frame); |
| 652 | native_handle_close(nh); |
| 653 | native_handle_delete(nh); |
| 654 | } else { |
| 655 | mHidlDevice->releaseRecordingFrame(heapId, bufferIndex); |
| 656 | } |
| 657 | } else if (mDevice && mDevice->ops->release_recording_frame) { |
| 658 | void *data = ((uint8_t *)heap->base()) + offset; |
| 659 | return mDevice->ops->release_recording_frame(mDevice, data); |
| 660 | } |
| 661 | } |
| 662 | |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 663 | void CameraHardwareInterface::releaseRecordingFrameBatch(const std::vector<sp<IMemory>>& frames) |
| 664 | { |
| 665 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 666 | size_t n = frames.size(); |
| 667 | std::vector<VideoFrameMessage> msgs; |
| 668 | msgs.reserve(n); |
| 669 | for (auto& mem : frames) { |
| 670 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 671 | ssize_t offset; |
| 672 | size_t size; |
| 673 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 674 | if (size == sizeof(VideoNativeHandleMetadata)) { |
| 675 | uint32_t heapId = heap->getHeapID(); |
| 676 | uint32_t bufferIndex = offset / size; |
| 677 | VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*) mem->pointer(); |
| 678 | // Caching the handle here because md->pHandle will be subject to HAL's edit |
| 679 | native_handle_t* nh = md->pHandle; |
| 680 | VideoFrameMessage msg; |
| 681 | msgs.push_back({nh, heapId, bufferIndex}); |
| 682 | } else { |
| 683 | ALOGE("%s only supports VideoNativeHandleMetadata mode", __FUNCTION__); |
| 684 | return; |
| 685 | } |
| 686 | } else { |
| 687 | ALOGE("Non HIDL mode do not support %s", __FUNCTION__); |
| 688 | return; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | mHidlDevice->releaseRecordingFrameHandleBatch(msgs); |
| 693 | |
| 694 | for (auto& msg : msgs) { |
| 695 | native_handle_t* nh = const_cast<native_handle_t*>(msg.frameData.getNativeHandle()); |
| 696 | native_handle_close(nh); |
| 697 | native_handle_delete(nh); |
| 698 | } |
| 699 | } |
| 700 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 701 | status_t CameraHardwareInterface::autoFocus() |
| 702 | { |
| 703 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 704 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 705 | return CameraProviderManager::mapToStatusT( |
| 706 | mHidlDevice->autoFocus()); |
| 707 | } else if (mDevice && mDevice->ops->auto_focus) { |
| 708 | return mDevice->ops->auto_focus(mDevice); |
| 709 | } |
| 710 | return INVALID_OPERATION; |
| 711 | } |
| 712 | |
| 713 | status_t CameraHardwareInterface::cancelAutoFocus() |
| 714 | { |
| 715 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 716 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 717 | return CameraProviderManager::mapToStatusT( |
| 718 | mHidlDevice->cancelAutoFocus()); |
| 719 | } else if (mDevice && mDevice->ops->cancel_auto_focus) { |
| 720 | return mDevice->ops->cancel_auto_focus(mDevice); |
| 721 | } |
| 722 | return INVALID_OPERATION; |
| 723 | } |
| 724 | |
| 725 | status_t CameraHardwareInterface::takePicture() |
| 726 | { |
| 727 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 728 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 729 | return CameraProviderManager::mapToStatusT( |
| 730 | mHidlDevice->takePicture()); |
| 731 | } else if (mDevice && mDevice->ops->take_picture) { |
| 732 | return mDevice->ops->take_picture(mDevice); |
| 733 | } |
| 734 | return INVALID_OPERATION; |
| 735 | } |
| 736 | |
| 737 | status_t CameraHardwareInterface::cancelPicture() |
| 738 | { |
| 739 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 740 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 741 | return CameraProviderManager::mapToStatusT( |
| 742 | mHidlDevice->cancelPicture()); |
| 743 | } else if (mDevice && mDevice->ops->cancel_picture) { |
| 744 | return mDevice->ops->cancel_picture(mDevice); |
| 745 | } |
| 746 | return INVALID_OPERATION; |
| 747 | } |
| 748 | |
| 749 | status_t CameraHardwareInterface::setParameters(const CameraParameters ¶ms) |
| 750 | { |
| 751 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 752 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 753 | return CameraProviderManager::mapToStatusT( |
| 754 | mHidlDevice->setParameters(params.flatten().string())); |
| 755 | } else if (mDevice && mDevice->ops->set_parameters) { |
| 756 | return mDevice->ops->set_parameters(mDevice, params.flatten().string()); |
| 757 | } |
| 758 | return INVALID_OPERATION; |
| 759 | } |
| 760 | |
| 761 | CameraParameters CameraHardwareInterface::getParameters() const |
| 762 | { |
| 763 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 764 | CameraParameters parms; |
| 765 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 766 | hardware::hidl_string outParam; |
| 767 | mHidlDevice->getParameters( |
| 768 | [&outParam](const auto& outStr) { |
| 769 | outParam = outStr; |
| 770 | }); |
| 771 | String8 tmp(outParam.c_str()); |
| 772 | parms.unflatten(tmp); |
| 773 | } else if (mDevice && mDevice->ops->get_parameters) { |
| 774 | char *temp = mDevice->ops->get_parameters(mDevice); |
| 775 | String8 str_parms(temp); |
| 776 | if (mDevice->ops->put_parameters) |
| 777 | mDevice->ops->put_parameters(mDevice, temp); |
| 778 | else |
| 779 | free(temp); |
| 780 | parms.unflatten(str_parms); |
| 781 | } |
| 782 | return parms; |
| 783 | } |
| 784 | |
| 785 | status_t CameraHardwareInterface::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) |
| 786 | { |
| 787 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 788 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 789 | return CameraProviderManager::mapToStatusT( |
| 790 | mHidlDevice->sendCommand((CommandType) cmd, arg1, arg2)); |
| 791 | } else if (mDevice && mDevice->ops->send_command) { |
| 792 | return mDevice->ops->send_command(mDevice, cmd, arg1, arg2); |
| 793 | } |
| 794 | return INVALID_OPERATION; |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * Release the hardware resources owned by this object. Note that this is |
| 799 | * *not* done in the destructor. |
| 800 | */ |
| 801 | void CameraHardwareInterface::release() { |
| 802 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 803 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 804 | mHidlDevice->close(); |
| 805 | mHidlDevice.clear(); |
| 806 | } else if (mDevice && mDevice->ops->release) { |
| 807 | mDevice->ops->release(mDevice); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Dump state of the camera hardware |
| 813 | */ |
| 814 | status_t CameraHardwareInterface::dump(int fd, const Vector<String16>& /*args*/) const |
| 815 | { |
| 816 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
| 817 | if (CC_LIKELY(mHidlDevice != nullptr)) { |
| 818 | native_handle_t* handle = native_handle_create(1,0); |
| 819 | handle->data[0] = fd; |
| 820 | Status s = mHidlDevice->dumpState(handle); |
| 821 | native_handle_delete(handle); |
| 822 | return CameraProviderManager::mapToStatusT(s); |
| 823 | } else if (mDevice && mDevice->ops->dump) { |
| 824 | return mDevice->ops->dump(mDevice, fd); |
| 825 | } |
| 826 | return OK; // It's fine if the HAL doesn't implement dump() |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Methods for legacy (non-HIDL) path follows |
| 831 | */ |
| 832 | void CameraHardwareInterface::sNotifyCb(int32_t msg_type, int32_t ext1, |
| 833 | int32_t ext2, void *user) |
| 834 | { |
| 835 | ALOGV("%s", __FUNCTION__); |
| 836 | CameraHardwareInterface *object = |
| 837 | static_cast<CameraHardwareInterface *>(user); |
| 838 | object->mNotifyCb(msg_type, ext1, ext2, object->mCbUser); |
| 839 | } |
| 840 | |
| 841 | void CameraHardwareInterface::sDataCb(int32_t msg_type, |
| 842 | const camera_memory_t *data, unsigned int index, |
| 843 | camera_frame_metadata_t *metadata, |
| 844 | void *user) |
| 845 | { |
| 846 | ALOGV("%s", __FUNCTION__); |
| 847 | CameraHardwareInterface *object = |
| 848 | static_cast<CameraHardwareInterface *>(user); |
| 849 | sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle)); |
| 850 | if (index >= mem->mNumBufs) { |
| 851 | ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__, |
| 852 | index, mem->mNumBufs); |
| 853 | return; |
| 854 | } |
| 855 | object->mDataCb(msg_type, mem->mBuffers[index], metadata, object->mCbUser); |
| 856 | } |
| 857 | |
| 858 | void CameraHardwareInterface::sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type, |
| 859 | const camera_memory_t *data, unsigned index, |
| 860 | void *user) |
| 861 | { |
| 862 | ALOGV("%s", __FUNCTION__); |
| 863 | CameraHardwareInterface *object = |
| 864 | static_cast<CameraHardwareInterface *>(user); |
| 865 | // Start refcounting the heap object from here on. When the clients |
| 866 | // drop all references, it will be destroyed (as well as the enclosed |
| 867 | // MemoryHeapBase. |
| 868 | sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle)); |
| 869 | if (index >= mem->mNumBufs) { |
| 870 | ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__, |
| 871 | index, mem->mNumBufs); |
| 872 | return; |
| 873 | } |
| 874 | object->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], object->mCbUser); |
| 875 | } |
| 876 | |
| 877 | camera_memory_t* CameraHardwareInterface::sGetMemory( |
| 878 | int fd, size_t buf_size, uint_t num_bufs, |
| 879 | void *user __attribute__((unused))) |
| 880 | { |
| 881 | CameraHeapMemory *mem; |
| 882 | if (fd < 0) { |
| 883 | mem = new CameraHeapMemory(buf_size, num_bufs); |
| 884 | } else { |
| 885 | mem = new CameraHeapMemory(fd, buf_size, num_bufs); |
| 886 | } |
| 887 | mem->incStrong(mem); |
| 888 | return &mem->handle; |
| 889 | } |
| 890 | |
| 891 | void CameraHardwareInterface::sPutMemory(camera_memory_t *data) |
| 892 | { |
| 893 | if (!data) { |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle); |
| 898 | mem->decStrong(mem); |
| 899 | } |
| 900 | |
| 901 | ANativeWindow* CameraHardwareInterface::sToAnw(void *user) |
| 902 | { |
| 903 | CameraHardwareInterface *object = |
| 904 | reinterpret_cast<CameraHardwareInterface *>(user); |
| 905 | return object->mPreviewWindow.get(); |
| 906 | } |
| 907 | #define anw(n) sToAnw(((struct camera_preview_window *)(n))->user) |
| 908 | #define hwi(n) reinterpret_cast<CameraHardwareInterface *>(\ |
| 909 | ((struct camera_preview_window *)(n))->user) |
| 910 | |
| 911 | int CameraHardwareInterface::sDequeueBuffer(struct preview_stream_ops* w, |
| 912 | buffer_handle_t** buffer, int *stride) |
| 913 | { |
| 914 | int rc; |
| 915 | ANativeWindow *a = anw(w); |
| 916 | ANativeWindowBuffer* anb; |
| 917 | rc = native_window_dequeue_buffer_and_wait(a, &anb); |
| 918 | if (rc == OK) { |
| 919 | *buffer = &anb->handle; |
| 920 | *stride = anb->stride; |
| 921 | } |
| 922 | return rc; |
| 923 | } |
| 924 | |
| 925 | #ifndef container_of |
| 926 | #define container_of(ptr, type, member) ({ \ |
| 927 | const __typeof__(((type *) 0)->member) *__mptr = (ptr); \ |
| 928 | (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); }) |
| 929 | #endif |
| 930 | |
| 931 | int CameraHardwareInterface::sLockBuffer(struct preview_stream_ops* w, |
| 932 | buffer_handle_t* /*buffer*/) |
| 933 | { |
| 934 | ANativeWindow *a = anw(w); |
| 935 | (void)a; |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | int CameraHardwareInterface::sEnqueueBuffer(struct preview_stream_ops* w, |
| 940 | buffer_handle_t* buffer) |
| 941 | { |
| 942 | ANativeWindow *a = anw(w); |
| 943 | return a->queueBuffer(a, |
| 944 | container_of(buffer, ANativeWindowBuffer, handle), -1); |
| 945 | } |
| 946 | |
| 947 | int CameraHardwareInterface::sCancelBuffer(struct preview_stream_ops* w, |
| 948 | buffer_handle_t* buffer) |
| 949 | { |
| 950 | ANativeWindow *a = anw(w); |
| 951 | return a->cancelBuffer(a, |
| 952 | container_of(buffer, ANativeWindowBuffer, handle), -1); |
| 953 | } |
| 954 | |
| 955 | int CameraHardwareInterface::sSetBufferCount(struct preview_stream_ops* w, int count) |
| 956 | { |
| 957 | ANativeWindow *a = anw(w); |
| 958 | |
| 959 | if (a != nullptr) { |
| 960 | // Workaround for b/27039775 |
| 961 | // Previously, setting the buffer count would reset the buffer |
| 962 | // queue's flag that allows for all buffers to be dequeued on the |
| 963 | // producer side, instead of just the producer's declared max count, |
| 964 | // if no filled buffers have yet been queued by the producer. This |
| 965 | // reset no longer happens, but some HALs depend on this behavior, |
| 966 | // so it needs to be maintained for HAL backwards compatibility. |
| 967 | // Simulate the prior behavior by disconnecting/reconnecting to the |
| 968 | // window and setting the values again. This has the drawback of |
| 969 | // actually causing memory reallocation, which may not have happened |
| 970 | // in the past. |
| 971 | CameraHardwareInterface *hw = hwi(w); |
| 972 | native_window_api_disconnect(a, NATIVE_WINDOW_API_CAMERA); |
| 973 | native_window_api_connect(a, NATIVE_WINDOW_API_CAMERA); |
| 974 | if (hw->mPreviewScalingMode != NOT_SET) { |
| 975 | native_window_set_scaling_mode(a, hw->mPreviewScalingMode); |
| 976 | } |
| 977 | if (hw->mPreviewTransform != NOT_SET) { |
| 978 | native_window_set_buffers_transform(a, hw->mPreviewTransform); |
| 979 | } |
| 980 | if (hw->mPreviewWidth != NOT_SET) { |
| 981 | native_window_set_buffers_dimensions(a, |
| 982 | hw->mPreviewWidth, hw->mPreviewHeight); |
| 983 | native_window_set_buffers_format(a, hw->mPreviewFormat); |
| 984 | } |
| 985 | if (hw->mPreviewUsage != 0) { |
| 986 | native_window_set_usage(a, hw->mPreviewUsage); |
| 987 | } |
| 988 | if (hw->mPreviewSwapInterval != NOT_SET) { |
| 989 | a->setSwapInterval(a, hw->mPreviewSwapInterval); |
| 990 | } |
| 991 | if (hw->mPreviewCrop.left != NOT_SET) { |
| 992 | native_window_set_crop(a, &(hw->mPreviewCrop)); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | return native_window_set_buffer_count(a, count); |
| 997 | } |
| 998 | |
| 999 | int CameraHardwareInterface::sSetBuffersGeometry(struct preview_stream_ops* w, |
| 1000 | int width, int height, int format) |
| 1001 | { |
| 1002 | int rc; |
| 1003 | ANativeWindow *a = anw(w); |
| 1004 | CameraHardwareInterface *hw = hwi(w); |
| 1005 | hw->mPreviewWidth = width; |
| 1006 | hw->mPreviewHeight = height; |
| 1007 | hw->mPreviewFormat = format; |
| 1008 | rc = native_window_set_buffers_dimensions(a, width, height); |
| 1009 | if (rc == OK) { |
| 1010 | rc = native_window_set_buffers_format(a, format); |
| 1011 | } |
| 1012 | return rc; |
| 1013 | } |
| 1014 | |
| 1015 | int CameraHardwareInterface::sSetCrop(struct preview_stream_ops *w, |
| 1016 | int left, int top, int right, int bottom) |
| 1017 | { |
| 1018 | ANativeWindow *a = anw(w); |
| 1019 | CameraHardwareInterface *hw = hwi(w); |
| 1020 | hw->mPreviewCrop.left = left; |
| 1021 | hw->mPreviewCrop.top = top; |
| 1022 | hw->mPreviewCrop.right = right; |
| 1023 | hw->mPreviewCrop.bottom = bottom; |
| 1024 | return native_window_set_crop(a, &(hw->mPreviewCrop)); |
| 1025 | } |
| 1026 | |
| 1027 | int CameraHardwareInterface::sSetTimestamp(struct preview_stream_ops *w, |
| 1028 | int64_t timestamp) { |
| 1029 | ANativeWindow *a = anw(w); |
| 1030 | return native_window_set_buffers_timestamp(a, timestamp); |
| 1031 | } |
| 1032 | |
| 1033 | int CameraHardwareInterface::sSetUsage(struct preview_stream_ops* w, int usage) |
| 1034 | { |
| 1035 | ANativeWindow *a = anw(w); |
| 1036 | CameraHardwareInterface *hw = hwi(w); |
| 1037 | hw->mPreviewUsage = usage; |
| 1038 | return native_window_set_usage(a, usage); |
| 1039 | } |
| 1040 | |
| 1041 | int CameraHardwareInterface::sSetSwapInterval(struct preview_stream_ops *w, int interval) |
| 1042 | { |
| 1043 | ANativeWindow *a = anw(w); |
| 1044 | CameraHardwareInterface *hw = hwi(w); |
| 1045 | hw->mPreviewSwapInterval = interval; |
| 1046 | return a->setSwapInterval(a, interval); |
| 1047 | } |
| 1048 | |
| 1049 | int CameraHardwareInterface::sGetMinUndequeuedBufferCount( |
| 1050 | const struct preview_stream_ops *w, |
| 1051 | int *count) |
| 1052 | { |
| 1053 | ANativeWindow *a = anw(w); |
| 1054 | return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count); |
| 1055 | } |
| 1056 | |
| 1057 | void CameraHardwareInterface::initHalPreviewWindow() |
| 1058 | { |
| 1059 | mHalPreviewWindow.nw.cancel_buffer = sCancelBuffer; |
| 1060 | mHalPreviewWindow.nw.lock_buffer = sLockBuffer; |
| 1061 | mHalPreviewWindow.nw.dequeue_buffer = sDequeueBuffer; |
| 1062 | mHalPreviewWindow.nw.enqueue_buffer = sEnqueueBuffer; |
| 1063 | mHalPreviewWindow.nw.set_buffer_count = sSetBufferCount; |
| 1064 | mHalPreviewWindow.nw.set_buffers_geometry = sSetBuffersGeometry; |
| 1065 | mHalPreviewWindow.nw.set_crop = sSetCrop; |
| 1066 | mHalPreviewWindow.nw.set_timestamp = sSetTimestamp; |
| 1067 | mHalPreviewWindow.nw.set_usage = sSetUsage; |
| 1068 | mHalPreviewWindow.nw.set_swap_interval = sSetSwapInterval; |
| 1069 | |
| 1070 | mHalPreviewWindow.nw.get_min_undequeued_buffer_count = |
| 1071 | sGetMinUndequeuedBufferCount; |
| 1072 | } |
| 1073 | |
| 1074 | }; // namespace android |