Avichal Rakesh | e1857f8 | 2022-06-08 17:47:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 "ExtCamOfflnSsn" |
| 18 | #include <android/log.h> |
| 19 | |
| 20 | #include "ExternalCameraOfflineSession.h" |
| 21 | |
| 22 | #include <aidl/android/hardware/camera/device/BufferStatus.h> |
| 23 | #include <aidl/android/hardware/camera/device/ErrorMsg.h> |
| 24 | #include <aidl/android/hardware/camera/device/ShutterMsg.h> |
| 25 | #include <aidl/android/hardware/camera/device/StreamBuffer.h> |
| 26 | #include <aidlcommonsupport/NativeHandle.h> |
| 27 | #include <convert.h> |
| 28 | #include <linux/videodev2.h> |
| 29 | #include <sync/sync.h> |
| 30 | #include <utils/Trace.h> |
| 31 | |
| 32 | #define HAVE_JPEG // required for libyuv.h to export MJPEG decode APIs |
| 33 | #include <libyuv.h> |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | // Size of request/result metadata fast message queue. Change to 0 to always use hwbinder buffer. |
| 38 | constexpr size_t kMetadataMsgQueueSize = 1 << 18 /* 256kB */; |
| 39 | |
| 40 | } // anonymous namespace |
| 41 | |
| 42 | namespace android { |
| 43 | namespace hardware { |
| 44 | namespace camera { |
| 45 | namespace device { |
| 46 | namespace implementation { |
| 47 | |
| 48 | using ::aidl::android::hardware::camera::device::BufferStatus; |
| 49 | using ::aidl::android::hardware::camera::device::ErrorMsg; |
| 50 | using ::aidl::android::hardware::camera::device::ShutterMsg; |
| 51 | using ::aidl::android::hardware::camera::device::StreamBuffer; |
| 52 | |
| 53 | // Static instance |
| 54 | HandleImporter ExternalCameraOfflineSession::sHandleImporter; |
| 55 | |
| 56 | ExternalCameraOfflineSession::ExternalCameraOfflineSession( |
| 57 | const CroppingType& croppingType, const common::V1_0::helper::CameraMetadata& chars, |
| 58 | const std::string& cameraId, const std::string& exifMake, const std::string& exifModel, |
| 59 | uint32_t blobBufferSize, bool afTrigger, const std::vector<Stream>& offlineStreams, |
| 60 | std::deque<std::shared_ptr<HalRequest>>& offlineReqs, |
| 61 | const std::map<int, CirculatingBuffers>& circulatingBuffers) |
| 62 | : mCroppingType(croppingType), |
| 63 | mChars(chars), |
| 64 | mCameraId(cameraId), |
| 65 | mExifMake(exifMake), |
| 66 | mExifModel(exifModel), |
| 67 | mBlobBufferSize(blobBufferSize), |
| 68 | mAfTrigger(afTrigger), |
| 69 | mOfflineStreams(offlineStreams), |
| 70 | mOfflineReqs(offlineReqs), |
| 71 | mCirculatingBuffers(circulatingBuffers) {} |
| 72 | |
| 73 | ExternalCameraOfflineSession::~ExternalCameraOfflineSession() { |
| 74 | close(); |
| 75 | } |
| 76 | |
| 77 | bool ExternalCameraOfflineSession::initialize() { |
| 78 | mResultMetadataQueue = |
| 79 | std::make_shared<ResultMetadataQueue>(kMetadataMsgQueueSize, false /* non blocking */); |
| 80 | if (!mResultMetadataQueue->isValid()) { |
| 81 | ALOGE("%s: invalid result fmq", __FUNCTION__); |
| 82 | return true; |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | Status ExternalCameraOfflineSession::importBuffer(int32_t streamId, uint64_t bufId, |
| 88 | buffer_handle_t buf, |
| 89 | buffer_handle_t** outBufPtr) { |
| 90 | Mutex::Autolock _l(mCbsLock); |
| 91 | return importBufferImpl(mCirculatingBuffers, sHandleImporter, streamId, bufId, buf, outBufPtr); |
| 92 | } |
| 93 | |
| 94 | Status ExternalCameraOfflineSession::processCaptureResult(std::shared_ptr<HalRequest>& req) { |
| 95 | ATRACE_CALL(); |
| 96 | // Fill output buffers |
| 97 | std::vector<CaptureResult> results; |
| 98 | results.resize(1); |
| 99 | CaptureResult& result = results[0]; |
| 100 | result.frameNumber = req->frameNumber; |
| 101 | result.partialResult = 1; |
| 102 | result.inputBuffer.streamId = -1; |
| 103 | result.outputBuffers.resize(req->buffers.size()); |
| 104 | for (size_t i = 0; i < req->buffers.size(); i++) { |
| 105 | StreamBuffer& outputBuffer = result.outputBuffers[i]; |
| 106 | outputBuffer.streamId = req->buffers[i].streamId; |
| 107 | outputBuffer.bufferId = req->buffers[i].bufferId; |
| 108 | if (req->buffers[i].fenceTimeout) { |
| 109 | outputBuffer.status = BufferStatus::ERROR; |
| 110 | if (req->buffers[i].acquireFence >= 0) { |
| 111 | native_handle_t* handle = native_handle_create(/*numFds*/ 1, /*numInts*/ 0); |
| 112 | handle->data[0] = req->buffers[i].acquireFence; |
| 113 | result.outputBuffers[i].releaseFence = android::makeToAidl(handle); |
| 114 | } |
| 115 | notifyError(req->frameNumber, req->buffers[i].streamId, ErrorCode::ERROR_BUFFER); |
| 116 | } else { |
| 117 | result.outputBuffers[i].status = BufferStatus::OK; |
| 118 | // TODO: refactor |
| 119 | if (req->buffers[i].acquireFence >= 0) { |
| 120 | native_handle_t* handle = native_handle_create(/*numFds*/ 1, /*numInts*/ 0); |
| 121 | handle->data[0] = req->buffers[i].acquireFence; |
| 122 | outputBuffer.releaseFence = android::makeToAidl(handle); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Fill capture result metadata |
| 128 | fillCaptureResult(req->setting, req->shutterTs); |
| 129 | const camera_metadata_t* rawResult = req->setting.getAndLock(); |
| 130 | convertToAidl(rawResult, &result.result); |
| 131 | req->setting.unlock(rawResult); |
| 132 | |
| 133 | // Callback into framework |
| 134 | invokeProcessCaptureResultCallback(results, /* tryWriteFmq */ true); |
| 135 | freeReleaseFences(results); |
| 136 | return Status::OK; |
| 137 | } |
| 138 | |
| 139 | #define UPDATE(md, tag, data, size) \ |
| 140 | do { \ |
| 141 | if ((md).update((tag), (data), (size))) { \ |
| 142 | ALOGE("Update " #tag " failed!"); \ |
| 143 | return BAD_VALUE; \ |
| 144 | } \ |
| 145 | } while (0) |
| 146 | |
| 147 | status_t ExternalCameraOfflineSession::fillCaptureResult(common::V1_0::helper::CameraMetadata md, |
| 148 | nsecs_t timestamp) { |
| 149 | bool afTrigger = false; |
| 150 | { |
| 151 | std::lock_guard<std::mutex> lk(mAfTriggerLock); |
| 152 | afTrigger = mAfTrigger; |
| 153 | if (md.exists(ANDROID_CONTROL_AF_TRIGGER)) { |
| 154 | camera_metadata_entry entry = md.find(ANDROID_CONTROL_AF_TRIGGER); |
| 155 | if (entry.data.u8[0] == ANDROID_CONTROL_AF_TRIGGER_START) { |
| 156 | mAfTrigger = afTrigger = true; |
| 157 | } else if (entry.data.u8[0] == ANDROID_CONTROL_AF_TRIGGER_CANCEL) { |
| 158 | mAfTrigger = afTrigger = false; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // For USB camera, the USB camera handles everything and we don't have control |
| 164 | // over AF. We only simply fake the AF metadata based on the request |
| 165 | // received here. |
| 166 | uint8_t afState; |
| 167 | if (afTrigger) { |
| 168 | afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED; |
| 169 | } else { |
| 170 | afState = ANDROID_CONTROL_AF_STATE_INACTIVE; |
| 171 | } |
| 172 | UPDATE(md, ANDROID_CONTROL_AF_STATE, &afState, 1); |
| 173 | |
| 174 | camera_metadata_ro_entry activeArraySize = mChars.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE); |
| 175 | |
| 176 | return fillCaptureResultCommon(md, timestamp, activeArraySize); |
| 177 | } |
| 178 | void ExternalCameraOfflineSession::invokeProcessCaptureResultCallback( |
| 179 | std::vector<CaptureResult>& results, bool tryWriteFmq) { |
| 180 | if (mProcessCaptureResultLock.tryLock() != OK) { |
| 181 | const nsecs_t NS_TO_SECOND = 1E9; |
| 182 | ALOGV("%s: previous call is not finished! waiting 1s...", __FUNCTION__); |
| 183 | if (mProcessCaptureResultLock.timedLock(/* 1s */ NS_TO_SECOND) != OK) { |
| 184 | ALOGE("%s: cannot acquire lock in 1s, cannot proceed", __FUNCTION__); |
| 185 | return; |
| 186 | } |
| 187 | } |
| 188 | if (tryWriteFmq && mResultMetadataQueue->availableToWrite() > 0) { |
| 189 | for (CaptureResult& result : results) { |
| 190 | if (!result.result.metadata.empty()) { |
| 191 | if (mResultMetadataQueue->write( |
| 192 | reinterpret_cast<int8_t*>(result.result.metadata.data()), |
| 193 | result.result.metadata.size())) { |
| 194 | result.fmqResultSize = result.result.metadata.size(); |
| 195 | result.result.metadata.clear(); |
| 196 | } else { |
| 197 | ALOGW("%s: couldn't utilize fmq, fall back to hwbinder", __FUNCTION__); |
| 198 | result.fmqResultSize = 0; |
| 199 | } |
| 200 | } else { |
| 201 | result.fmqResultSize = 0; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | auto status = mCallback->processCaptureResult(results); |
| 206 | if (!status.isOk()) { |
| 207 | ALOGE("%s: processCaptureResult ERROR : %d:%d", __FUNCTION__, status.getExceptionCode(), |
| 208 | status.getServiceSpecificError()); |
| 209 | } |
| 210 | |
| 211 | mProcessCaptureResultLock.unlock(); |
| 212 | } |
| 213 | |
| 214 | Status ExternalCameraOfflineSession::processCaptureRequestError( |
| 215 | const std::shared_ptr<HalRequest>& req, std::vector<NotifyMsg>* outMsgs, |
| 216 | std::vector<CaptureResult>* outResults) { |
| 217 | ATRACE_CALL(); |
| 218 | |
| 219 | if (outMsgs == nullptr) { |
| 220 | notifyError(/*frameNum*/ req->frameNumber, /*stream*/ -1, ErrorCode::ERROR_REQUEST); |
| 221 | } else { |
| 222 | NotifyMsg shutter; |
| 223 | shutter.set<NotifyMsg::Tag::shutter>(ShutterMsg{ |
| 224 | .frameNumber = req->frameNumber, |
| 225 | .timestamp = req->shutterTs, |
| 226 | }); |
| 227 | |
| 228 | NotifyMsg error; |
| 229 | error.set<NotifyMsg::Tag::error>(ErrorMsg{.frameNumber = req->frameNumber, |
| 230 | .errorStreamId = -1, |
| 231 | .errorCode = ErrorCode::ERROR_REQUEST}); |
| 232 | outMsgs->push_back(shutter); |
| 233 | outMsgs->push_back(error); |
| 234 | } |
| 235 | |
| 236 | // Fill output buffers |
| 237 | CaptureResult result; |
| 238 | result.frameNumber = req->frameNumber; |
| 239 | result.partialResult = 1; |
| 240 | result.inputBuffer.streamId = -1; |
| 241 | result.outputBuffers.resize(req->buffers.size()); |
| 242 | for (size_t i = 0; i < req->buffers.size(); i++) { |
| 243 | StreamBuffer& outputBuffer = result.outputBuffers[i]; |
| 244 | outputBuffer.streamId = req->buffers[i].streamId; |
| 245 | outputBuffer.bufferId = req->buffers[i].bufferId; |
| 246 | outputBuffer.status = BufferStatus::ERROR; |
| 247 | if (req->buffers[i].acquireFence >= 0) { |
| 248 | native_handle_t* handle = native_handle_create(/*numFds*/ 1, /*numInts*/ 0); |
| 249 | handle->data[0] = req->buffers[i].acquireFence; |
| 250 | outputBuffer.releaseFence = makeToAidl(handle); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (outResults == nullptr) { |
| 255 | // Callback into framework |
| 256 | std::vector<CaptureResult> results(1); |
| 257 | results[0] = std::move(result); |
| 258 | invokeProcessCaptureResultCallback(results, /* tryWriteFmq */ true); |
| 259 | freeReleaseFences(results); |
| 260 | } else { |
| 261 | outResults->push_back(std::move(result)); |
| 262 | } |
| 263 | return Status::OK; |
| 264 | } |
| 265 | |
| 266 | ssize_t ExternalCameraOfflineSession::getJpegBufferSize(int32_t, int32_t) const { |
| 267 | // Empty implementation here as the jpeg buffer size is passed in by ctor |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | void ExternalCameraOfflineSession::notifyError(int32_t frameNumber, int32_t streamId, |
| 272 | ErrorCode ec) { |
| 273 | NotifyMsg msg; |
| 274 | msg.set<NotifyMsg::Tag::error>( |
| 275 | ErrorMsg{.frameNumber = frameNumber, .errorStreamId = streamId, .errorCode = ec}); |
| 276 | mCallback->notify({msg}); |
| 277 | } |
| 278 | |
| 279 | ScopedAStatus ExternalCameraOfflineSession::setCallback( |
| 280 | const std::shared_ptr<ICameraDeviceCallback>& in_cb) { |
| 281 | Mutex::Autolock _il(mInterfaceLock); |
| 282 | if (mCallback != nullptr && in_cb != nullptr) { |
| 283 | ALOGE("%s: callback must not be set twice!", __FUNCTION__); |
| 284 | return fromStatus(Status::OK); |
| 285 | } |
| 286 | mCallback = in_cb; |
| 287 | |
| 288 | initOutputThread(); |
| 289 | |
| 290 | if (mOutputThread == nullptr) { |
| 291 | ALOGE("%s: init OutputThread failed!", __FUNCTION__); |
| 292 | } |
| 293 | return fromStatus(Status::OK); |
| 294 | } |
| 295 | void ExternalCameraOfflineSession::initOutputThread() { |
| 296 | if (mOutputThread != nullptr) { |
| 297 | ALOGE("%s: OutputThread already exist!", __FUNCTION__); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | // Grab a shared_ptr to 'this' from ndk::SharedRefBase::ref() |
| 302 | std::shared_ptr<ExternalCameraOfflineSession> thiz = ref<ExternalCameraOfflineSession>(); |
| 303 | |
| 304 | mBufferRequestThread = std::make_shared<ExternalCameraDeviceSession::BufferRequestThread>( |
| 305 | /*parent=*/thiz, mCallback); |
| 306 | mBufferRequestThread->run(); |
| 307 | |
| 308 | mOutputThread = std::make_shared<OutputThread>(/*parent=*/thiz, mCroppingType, mChars, |
| 309 | mBufferRequestThread, mOfflineReqs); |
| 310 | |
| 311 | mOutputThread->setExifMakeModel(mExifMake, mExifModel); |
| 312 | |
| 313 | Size inputSize = {mOfflineReqs[0]->frameIn->mWidth, mOfflineReqs[0]->frameIn->mHeight}; |
| 314 | Size maxThumbSize = getMaxThumbnailResolution(mChars); |
| 315 | mOutputThread->allocateIntermediateBuffers(inputSize, maxThumbSize, mOfflineStreams, |
| 316 | mBlobBufferSize); |
| 317 | |
| 318 | mOutputThread->run(); |
| 319 | } |
| 320 | |
| 321 | ScopedAStatus ExternalCameraOfflineSession::getCaptureResultMetadataQueue( |
| 322 | MQDescriptor<int8_t, SynchronizedReadWrite>* _aidl_return) { |
| 323 | Mutex::Autolock _il(mInterfaceLock); |
| 324 | *_aidl_return = mResultMetadataQueue->dupeDesc(); |
| 325 | return fromStatus(Status::OK); |
| 326 | } |
| 327 | |
| 328 | ScopedAStatus ExternalCameraOfflineSession::close() { |
| 329 | Mutex::Autolock _il(mInterfaceLock); |
| 330 | { |
| 331 | Mutex::Autolock _l(mLock); |
| 332 | if (mClosed) { |
| 333 | ALOGW("%s: offline session already closed!", __FUNCTION__); |
| 334 | return fromStatus(Status::OK); |
| 335 | } |
| 336 | } |
| 337 | if (mBufferRequestThread != nullptr) { |
| 338 | mBufferRequestThread->requestExitAndWait(); |
| 339 | mBufferRequestThread.reset(); |
| 340 | } |
| 341 | if (mOutputThread) { |
| 342 | mOutputThread->flush(); |
| 343 | mOutputThread->requestExitAndWait(); |
| 344 | mOutputThread.reset(); |
| 345 | } |
| 346 | |
| 347 | Mutex::Autolock _l(mLock); |
| 348 | // free all buffers |
| 349 | { |
| 350 | Mutex::Autolock _cbl(mCbsLock); |
| 351 | for (auto& stream : mOfflineStreams) { |
| 352 | cleanupBuffersLocked(stream.id); |
| 353 | } |
| 354 | } |
| 355 | mCallback.reset(); |
| 356 | mClosed = true; |
| 357 | return fromStatus(Status::OK); |
| 358 | } |
| 359 | void ExternalCameraOfflineSession::cleanupBuffersLocked(int32_t id) { |
| 360 | for (auto& pair : mCirculatingBuffers.at(id)) { |
| 361 | sHandleImporter.freeBuffer(pair.second); |
| 362 | } |
| 363 | mCirculatingBuffers[id].clear(); |
| 364 | mCirculatingBuffers.erase(id); |
| 365 | } |
| 366 | |
| 367 | bool ExternalCameraOfflineSession::OutputThread::threadLoop() { |
| 368 | auto parent = mParent.lock(); |
| 369 | if (parent == nullptr) { |
| 370 | ALOGE("%s: session has been disconnected!", __FUNCTION__); |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | if (mOfflineReqs.empty()) { |
| 375 | ALOGI("%s: all offline requests are processed. Stopping.", __FUNCTION__); |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | std::shared_ptr<HalRequest> req = mOfflineReqs.front(); |
| 380 | mOfflineReqs.pop_front(); |
| 381 | |
| 382 | auto onDeviceError = [&](auto... args) { |
| 383 | ALOGE(args...); |
| 384 | parent->notifyError(req->frameNumber, /*stream*/ -1, ErrorCode::ERROR_DEVICE); |
| 385 | signalRequestDone(); |
| 386 | return false; |
| 387 | }; |
| 388 | |
| 389 | if (req->frameIn->mFourcc != V4L2_PIX_FMT_MJPEG && req->frameIn->mFourcc != V4L2_PIX_FMT_Z16) { |
| 390 | return onDeviceError("%s: do not support V4L2 format %c%c%c%c", __FUNCTION__, |
| 391 | req->frameIn->mFourcc & 0xFF, (req->frameIn->mFourcc >> 8) & 0xFF, |
| 392 | (req->frameIn->mFourcc >> 16) & 0xFF, |
| 393 | (req->frameIn->mFourcc >> 24) & 0xFF); |
| 394 | } |
| 395 | |
| 396 | int res = requestBufferStart(req->buffers); |
| 397 | if (res != 0) { |
| 398 | ALOGE("%s: send BufferRequest failed! res %d", __FUNCTION__, res); |
| 399 | return onDeviceError("%s: failed to send buffer request!", __FUNCTION__); |
| 400 | } |
| 401 | |
| 402 | std::unique_lock<std::mutex> lk(mBufferLock); |
| 403 | // Convert input V4L2 frame to YU12 of the same size |
| 404 | // TODO: see if we can save some computation by converting to YV12 here |
| 405 | uint8_t* inData; |
| 406 | size_t inDataSize; |
| 407 | if (req->frameIn->getData(&inData, &inDataSize) != 0) { |
| 408 | lk.unlock(); |
| 409 | return onDeviceError("%s: V4L2 buffer map failed", __FUNCTION__); |
| 410 | } |
| 411 | |
| 412 | // TODO: in some special case maybe we can decode jpg directly to gralloc output? |
| 413 | if (req->frameIn->mFourcc == V4L2_PIX_FMT_MJPEG) { |
| 414 | ATRACE_BEGIN("MJPGtoI420"); |
| 415 | int convRes = libyuv::MJPGToI420( |
| 416 | inData, inDataSize, static_cast<uint8_t*>(mYu12FrameLayout.y), |
| 417 | mYu12FrameLayout.yStride, static_cast<uint8_t*>(mYu12FrameLayout.cb), |
| 418 | mYu12FrameLayout.cStride, static_cast<uint8_t*>(mYu12FrameLayout.cr), |
| 419 | mYu12FrameLayout.cStride, mYu12Frame->mWidth, mYu12Frame->mHeight, |
| 420 | mYu12Frame->mWidth, mYu12Frame->mHeight); |
| 421 | ATRACE_END(); |
| 422 | |
| 423 | if (convRes != 0) { |
| 424 | // For some webcam, the first few V4L2 frames might be malformed... |
| 425 | ALOGE("%s: Convert V4L2 frame to YU12 failed! res %d", __FUNCTION__, convRes); |
| 426 | lk.unlock(); |
| 427 | Status st = parent->processCaptureRequestError(req); |
| 428 | if (st != Status::OK) { |
| 429 | return onDeviceError("%s: failed to process capture request error!", __FUNCTION__); |
| 430 | } |
| 431 | signalRequestDone(); |
| 432 | return true; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | ATRACE_BEGIN("Wait for BufferRequest done"); |
| 437 | res = waitForBufferRequestDone(&req->buffers); |
| 438 | ATRACE_END(); |
| 439 | |
| 440 | if (res != 0) { |
| 441 | ALOGE("%s: wait for BufferRequest done failed! res %d", __FUNCTION__, res); |
| 442 | lk.unlock(); |
| 443 | return onDeviceError("%s: failed to process buffer request error!", __FUNCTION__); |
| 444 | } |
| 445 | |
| 446 | ALOGV("%s processing new request", __FUNCTION__); |
| 447 | const int kSyncWaitTimeoutMs = 500; |
| 448 | for (auto& halBuf : req->buffers) { |
| 449 | if (*(halBuf.bufPtr) == nullptr) { |
| 450 | ALOGW("%s: buffer for stream %d missing", __FUNCTION__, halBuf.streamId); |
| 451 | halBuf.fenceTimeout = true; |
| 452 | } else if (halBuf.acquireFence >= 0) { |
| 453 | int ret = sync_wait(halBuf.acquireFence, kSyncWaitTimeoutMs); |
| 454 | if (ret) { |
| 455 | halBuf.fenceTimeout = true; |
| 456 | } else { |
| 457 | ::close(halBuf.acquireFence); |
| 458 | halBuf.acquireFence = -1; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (halBuf.fenceTimeout) { |
| 463 | continue; |
| 464 | } |
| 465 | |
| 466 | // Gralloc lockYCbCr the buffer |
| 467 | switch (halBuf.format) { |
| 468 | case PixelFormat::BLOB: { |
| 469 | int ret = createJpegLocked(halBuf, req->setting); |
| 470 | |
| 471 | if (ret != 0) { |
| 472 | lk.unlock(); |
| 473 | return onDeviceError("%s: createJpegLocked failed with %d", __FUNCTION__, ret); |
| 474 | } |
| 475 | } break; |
| 476 | case PixelFormat::Y16: { |
| 477 | void* outLayout = sHandleImporter.lock( |
| 478 | *(halBuf.bufPtr), static_cast<uint64_t>(halBuf.usage), inDataSize); |
| 479 | |
| 480 | std::memcpy(outLayout, inData, inDataSize); |
| 481 | |
| 482 | int relFence = sHandleImporter.unlock(*(halBuf.bufPtr)); |
| 483 | if (relFence >= 0) { |
| 484 | halBuf.acquireFence = relFence; |
| 485 | } |
| 486 | } break; |
| 487 | case PixelFormat::YCBCR_420_888: |
| 488 | case PixelFormat::YV12: { |
Devin Moore | 5e15409 | 2023-09-13 16:18:30 +0000 | [diff] [blame^] | 489 | android::Rect outRect{0, 0, static_cast<int32_t>(halBuf.width), |
Avichal Rakesh | e1857f8 | 2022-06-08 17:47:23 -0700 | [diff] [blame] | 490 | static_cast<int32_t>(halBuf.height)}; |
Devin Moore | 5e15409 | 2023-09-13 16:18:30 +0000 | [diff] [blame^] | 491 | android_ycbcr result = sHandleImporter.lockYCbCr( |
Avichal Rakesh | e1857f8 | 2022-06-08 17:47:23 -0700 | [diff] [blame] | 492 | *(halBuf.bufPtr), static_cast<uint64_t>(halBuf.usage), outRect); |
Devin Moore | 5e15409 | 2023-09-13 16:18:30 +0000 | [diff] [blame^] | 493 | ALOGV("%s: outLayout y %p cb %p cr %p y_str %zu c_str %zu c_step %zu", __FUNCTION__, |
| 494 | result.y, result.cb, result.cr, result.ystride, result.cstride, |
| 495 | result.chroma_step); |
| 496 | if (result.ystride > UINT32_MAX || result.cstride > UINT32_MAX || |
| 497 | result.chroma_step > UINT32_MAX) { |
| 498 | return onDeviceError("%s: lockYCbCr failed. Unexpected values!", __FUNCTION__); |
| 499 | } |
| 500 | YCbCrLayout outLayout = {.y = result.y, |
| 501 | .cb = result.cb, |
| 502 | .cr = result.cr, |
| 503 | .yStride = static_cast<uint32_t>(result.ystride), |
| 504 | .cStride = static_cast<uint32_t>(result.cstride), |
| 505 | .chromaStep = static_cast<uint32_t>(result.chroma_step)}; |
Avichal Rakesh | e1857f8 | 2022-06-08 17:47:23 -0700 | [diff] [blame] | 506 | |
| 507 | // Convert to output buffer size/format |
| 508 | uint32_t outputFourcc = getFourCcFromLayout(outLayout); |
| 509 | ALOGV("%s: converting to format %c%c%c%c", __FUNCTION__, outputFourcc & 0xFF, |
| 510 | (outputFourcc >> 8) & 0xFF, (outputFourcc >> 16) & 0xFF, |
| 511 | (outputFourcc >> 24) & 0xFF); |
| 512 | |
| 513 | YCbCrLayout cropAndScaled; |
| 514 | ATRACE_BEGIN("cropAndScaleLocked"); |
| 515 | int ret = cropAndScaleLocked(mYu12Frame, Size{halBuf.width, halBuf.height}, |
| 516 | &cropAndScaled); |
| 517 | ATRACE_END(); |
| 518 | if (ret != 0) { |
| 519 | lk.unlock(); |
| 520 | return onDeviceError("%s: crop and scale failed!", __FUNCTION__); |
| 521 | } |
| 522 | |
| 523 | Size sz{halBuf.width, halBuf.height}; |
| 524 | ATRACE_BEGIN("formatConvert"); |
| 525 | ret = formatConvert(cropAndScaled, outLayout, sz, outputFourcc); |
| 526 | ATRACE_END(); |
| 527 | if (ret != 0) { |
| 528 | lk.unlock(); |
| 529 | return onDeviceError("%s: format coversion failed!", __FUNCTION__); |
| 530 | } |
| 531 | int relFence = sHandleImporter.unlock(*(halBuf.bufPtr)); |
| 532 | if (relFence >= 0) { |
| 533 | halBuf.acquireFence = relFence; |
| 534 | } |
| 535 | } break; |
| 536 | default: |
| 537 | lk.unlock(); |
| 538 | return onDeviceError("%s: unknown output format %x", __FUNCTION__, halBuf.format); |
| 539 | } |
| 540 | } // for each buffer |
| 541 | mScaledYu12Frames.clear(); |
| 542 | |
| 543 | // Don't hold the lock while calling back to parent |
| 544 | lk.unlock(); |
| 545 | Status st = parent->processCaptureResult(req); |
| 546 | if (st != Status::OK) { |
| 547 | return onDeviceError("%s: failed to process capture result!", __FUNCTION__); |
| 548 | } |
| 549 | signalRequestDone(); |
| 550 | return true; |
| 551 | } |
| 552 | |
| 553 | } // namespace implementation |
| 554 | } // namespace device |
| 555 | } // namespace camera |
| 556 | } // namespace hardware |
Devin Moore | 5e15409 | 2023-09-13 16:18:30 +0000 | [diff] [blame^] | 557 | } // namespace android |