Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "ExtCamDevSsn@3.4" |
| 17 | //#define LOG_NDEBUG 0 |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 19 | #include <log/log.h> |
| 20 | |
| 21 | #include <inttypes.h> |
| 22 | #include "ExternalCameraDeviceSession.h" |
| 23 | |
| 24 | #include "android-base/macros.h" |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 25 | #include <utils/Timers.h> |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 26 | #include <utils/Trace.h> |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 27 | #include <linux/videodev2.h> |
| 28 | #include <sync/sync.h> |
| 29 | |
| 30 | #define HAVE_JPEG // required for libyuv.h to export MJPEG decode APIs |
| 31 | #include <libyuv.h> |
| 32 | |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 33 | #include <jpeglib.h> |
| 34 | |
| 35 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 36 | namespace android { |
| 37 | namespace hardware { |
| 38 | namespace camera { |
| 39 | namespace device { |
| 40 | namespace V3_4 { |
| 41 | namespace implementation { |
| 42 | |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 43 | namespace { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 44 | // Size of request/result metadata fast message queue. Change to 0 to always use hwbinder buffer. |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 45 | static constexpr size_t kMetadataMsgQueueSize = 1 << 18 /* 256kB */; |
| 46 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 47 | const int kBadFramesAfterStreamOn = 1; // drop x frames after streamOn to get rid of some initial |
| 48 | // bad frames. TODO: develop a better bad frame detection |
| 49 | // method |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 50 | constexpr int MAX_RETRY = 15; // Allow retry some ioctl failures a few times to account for some |
| 51 | // webcam showing temporarily ioctl failures. |
Yin-Chia Yeh | 2d61bfd | 2018-03-14 13:50:23 -0700 | [diff] [blame] | 52 | constexpr int IOCTL_RETRY_SLEEP_US = 33000; // 33ms * MAX_RETRY = 0.5 seconds |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 53 | |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 54 | // Constants for tryLock during dumpstate |
| 55 | static constexpr int kDumpLockRetries = 50; |
| 56 | static constexpr int kDumpLockSleep = 60000; |
| 57 | |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 58 | bool tryLock(Mutex& mutex) |
| 59 | { |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 60 | bool locked = false; |
| 61 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 62 | if (mutex.tryLock() == NO_ERROR) { |
| 63 | locked = true; |
| 64 | break; |
| 65 | } |
| 66 | usleep(kDumpLockSleep); |
| 67 | } |
| 68 | return locked; |
| 69 | } |
| 70 | |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 71 | bool tryLock(std::mutex& mutex) |
| 72 | { |
| 73 | bool locked = false; |
| 74 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 75 | if (mutex.try_lock()) { |
| 76 | locked = true; |
| 77 | break; |
| 78 | } |
| 79 | usleep(kDumpLockSleep); |
| 80 | } |
| 81 | return locked; |
| 82 | } |
| 83 | |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 84 | } // Anonymous namespace |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 85 | |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 86 | // Static instances |
| 87 | const int ExternalCameraDeviceSession::kMaxProcessedStream; |
| 88 | const int ExternalCameraDeviceSession::kMaxStallStream; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 89 | HandleImporter ExternalCameraDeviceSession::sHandleImporter; |
| 90 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 91 | ExternalCameraDeviceSession::ExternalCameraDeviceSession( |
| 92 | const sp<ICameraDeviceCallback>& callback, |
Yin-Chia Yeh | 1798249 | 2018-02-05 17:41:01 -0800 | [diff] [blame] | 93 | const ExternalCameraConfig& cfg, |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 94 | const std::vector<SupportedV4L2Format>& sortedFormats, |
| 95 | const CroppingType& croppingType, |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 96 | const common::V1_0::helper::CameraMetadata& chars, |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 97 | const std::string& cameraId, |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 98 | unique_fd v4l2Fd) : |
| 99 | mCallback(callback), |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 100 | mCfg(cfg), |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 101 | mCameraCharacteristics(chars), |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 102 | mSupportedFormats(sortedFormats), |
| 103 | mCroppingType(croppingType), |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 104 | mCameraId(cameraId), |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 105 | mV4l2Fd(std::move(v4l2Fd)), |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 106 | mOutputThread(new OutputThread(this, mCroppingType)), |
| 107 | mMaxThumbResolution(getMaxThumbResolution()), |
| 108 | mMaxJpegResolution(getMaxJpegResolution()) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 109 | mInitFail = initialize(); |
| 110 | } |
| 111 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 112 | bool ExternalCameraDeviceSession::initialize() { |
| 113 | if (mV4l2Fd.get() < 0) { |
| 114 | ALOGE("%s: invalid v4l2 device fd %d!", __FUNCTION__, mV4l2Fd.get()); |
| 115 | return true; |
| 116 | } |
| 117 | |
Yin-Chia Yeh | 2d61bfd | 2018-03-14 13:50:23 -0700 | [diff] [blame] | 118 | struct v4l2_capability capability; |
| 119 | int ret = ioctl(mV4l2Fd.get(), VIDIOC_QUERYCAP, &capability); |
| 120 | std::string make, model; |
| 121 | if (ret < 0) { |
| 122 | ALOGW("%s v4l2 QUERYCAP failed", __FUNCTION__); |
| 123 | make = "Generic UVC webcam"; |
| 124 | model = "Generic UVC webcam"; |
| 125 | } else { |
| 126 | // capability.card is UTF-8 encoded |
| 127 | char card[32]; |
| 128 | int j = 0; |
| 129 | for (int i = 0; i < 32; i++) { |
| 130 | if (capability.card[i] < 128) { |
| 131 | card[j++] = capability.card[i]; |
| 132 | } |
| 133 | if (capability.card[i] == '\0') { |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | if (j == 0 || card[j - 1] != '\0') { |
| 138 | make = "Generic UVC webcam"; |
| 139 | model = "Generic UVC webcam"; |
| 140 | } else { |
| 141 | make = card; |
| 142 | model = card; |
| 143 | } |
| 144 | } |
| 145 | mOutputThread->setExifMakeModel(make, model); |
| 146 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 147 | status_t status = initDefaultRequests(); |
| 148 | if (status != OK) { |
| 149 | ALOGE("%s: init default requests failed!", __FUNCTION__); |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | mRequestMetadataQueue = std::make_unique<RequestMetadataQueue>( |
| 154 | kMetadataMsgQueueSize, false /* non blocking */); |
| 155 | if (!mRequestMetadataQueue->isValid()) { |
| 156 | ALOGE("%s: invalid request fmq", __FUNCTION__); |
| 157 | return true; |
| 158 | } |
| 159 | mResultMetadataQueue = std::make_shared<RequestMetadataQueue>( |
| 160 | kMetadataMsgQueueSize, false /* non blocking */); |
| 161 | if (!mResultMetadataQueue->isValid()) { |
| 162 | ALOGE("%s: invalid result fmq", __FUNCTION__); |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | // TODO: check is PRIORITY_DISPLAY enough? |
| 167 | mOutputThread->run("ExtCamOut", PRIORITY_DISPLAY); |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | Status ExternalCameraDeviceSession::initStatus() const { |
| 172 | Mutex::Autolock _l(mLock); |
| 173 | Status status = Status::OK; |
| 174 | if (mInitFail || mClosed) { |
| 175 | ALOGI("%s: sesssion initFailed %d closed %d", __FUNCTION__, mInitFail, mClosed); |
| 176 | status = Status::INTERNAL_ERROR; |
| 177 | } |
| 178 | return status; |
| 179 | } |
| 180 | |
| 181 | ExternalCameraDeviceSession::~ExternalCameraDeviceSession() { |
| 182 | if (!isClosed()) { |
| 183 | ALOGE("ExternalCameraDeviceSession deleted before close!"); |
| 184 | close(); |
| 185 | } |
| 186 | } |
| 187 | |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 188 | |
| 189 | void ExternalCameraDeviceSession::dumpState(const native_handle_t* handle) { |
| 190 | if (handle->numFds != 1 || handle->numInts != 0) { |
| 191 | ALOGE("%s: handle must contain 1 FD and 0 integers! Got %d FDs and %d ints", |
| 192 | __FUNCTION__, handle->numFds, handle->numInts); |
| 193 | return; |
| 194 | } |
| 195 | int fd = handle->data[0]; |
| 196 | |
| 197 | bool intfLocked = tryLock(mInterfaceLock); |
| 198 | if (!intfLocked) { |
| 199 | dprintf(fd, "!! ExternalCameraDeviceSession interface may be deadlocked !!\n"); |
| 200 | } |
| 201 | |
| 202 | if (isClosed()) { |
| 203 | dprintf(fd, "External camera %s is closed\n", mCameraId.c_str()); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | bool streaming = false; |
| 208 | size_t v4L2BufferCount = 0; |
| 209 | SupportedV4L2Format streamingFmt; |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 210 | { |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 211 | bool sessionLocked = tryLock(mLock); |
| 212 | if (!sessionLocked) { |
| 213 | dprintf(fd, "!! ExternalCameraDeviceSession mLock may be deadlocked !!\n"); |
| 214 | } |
| 215 | streaming = mV4l2Streaming; |
| 216 | streamingFmt = mV4l2StreamingFmt; |
| 217 | v4L2BufferCount = mV4L2BufferCount; |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 218 | |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 219 | if (sessionLocked) { |
| 220 | mLock.unlock(); |
| 221 | } |
| 222 | } |
| 223 | |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 224 | std::unordered_set<uint32_t> inflightFrames; |
| 225 | { |
| 226 | bool iffLocked = tryLock(mInflightFramesLock); |
| 227 | if (!iffLocked) { |
| 228 | dprintf(fd, |
| 229 | "!! ExternalCameraDeviceSession mInflightFramesLock may be deadlocked !!\n"); |
| 230 | } |
| 231 | inflightFrames = mInflightFrames; |
| 232 | if (iffLocked) { |
| 233 | mInflightFramesLock.unlock(); |
| 234 | } |
| 235 | } |
| 236 | |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 237 | dprintf(fd, "External camera %s V4L2 FD %d, cropping type %s, %s\n", |
| 238 | mCameraId.c_str(), mV4l2Fd.get(), |
| 239 | (mCroppingType == VERTICAL) ? "vertical" : "horizontal", |
| 240 | streaming ? "streaming" : "not streaming"); |
| 241 | if (streaming) { |
| 242 | // TODO: dump fps later |
Yin-Chia Yeh | 3aa9ae9 | 2018-02-23 17:21:51 -0800 | [diff] [blame] | 243 | dprintf(fd, "Current V4L2 format %c%c%c%c %dx%d @ %ffps\n", |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 244 | streamingFmt.fourcc & 0xFF, |
| 245 | (streamingFmt.fourcc >> 8) & 0xFF, |
| 246 | (streamingFmt.fourcc >> 16) & 0xFF, |
| 247 | (streamingFmt.fourcc >> 24) & 0xFF, |
Yin-Chia Yeh | 3aa9ae9 | 2018-02-23 17:21:51 -0800 | [diff] [blame] | 248 | streamingFmt.width, streamingFmt.height, |
| 249 | mV4l2StreamingFps); |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 250 | |
| 251 | size_t numDequeuedV4l2Buffers = 0; |
| 252 | { |
| 253 | std::lock_guard<std::mutex> lk(mV4l2BufferLock); |
| 254 | numDequeuedV4l2Buffers = mNumDequeuedV4l2Buffers; |
| 255 | } |
| 256 | dprintf(fd, "V4L2 buffer queue size %zu, dequeued %zu\n", |
| 257 | v4L2BufferCount, numDequeuedV4l2Buffers); |
| 258 | } |
| 259 | |
| 260 | dprintf(fd, "In-flight frames (not sorted):"); |
| 261 | for (const auto& frameNumber : inflightFrames) { |
| 262 | dprintf(fd, "%d, ", frameNumber); |
| 263 | } |
| 264 | dprintf(fd, "\n"); |
| 265 | mOutputThread->dump(fd); |
| 266 | dprintf(fd, "\n"); |
| 267 | |
| 268 | if (intfLocked) { |
| 269 | mInterfaceLock.unlock(); |
| 270 | } |
| 271 | |
| 272 | return; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | Return<void> ExternalCameraDeviceSession::constructDefaultRequestSettings( |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 276 | V3_2::RequestTemplate type, |
| 277 | V3_2::ICameraDeviceSession::constructDefaultRequestSettings_cb _hidl_cb) { |
| 278 | V3_2::CameraMetadata outMetadata; |
| 279 | Status status = constructDefaultRequestSettingsRaw( |
| 280 | static_cast<RequestTemplate>(type), &outMetadata); |
| 281 | _hidl_cb(status, outMetadata); |
| 282 | return Void(); |
| 283 | } |
| 284 | |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 285 | Status ExternalCameraDeviceSession::constructDefaultRequestSettingsRaw(RequestTemplate type, |
| 286 | V3_2::CameraMetadata *outMetadata) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 287 | CameraMetadata emptyMd; |
| 288 | Status status = initStatus(); |
| 289 | if (status != Status::OK) { |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 290 | return status; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | switch (type) { |
| 294 | case RequestTemplate::PREVIEW: |
| 295 | case RequestTemplate::STILL_CAPTURE: |
| 296 | case RequestTemplate::VIDEO_RECORD: |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 297 | case RequestTemplate::VIDEO_SNAPSHOT: { |
| 298 | *outMetadata = mDefaultRequests[type]; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 299 | break; |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 300 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 301 | case RequestTemplate::MANUAL: |
| 302 | case RequestTemplate::ZERO_SHUTTER_LAG: |
Eino-Ville Talvala | 0a2a9fc | 2018-02-05 16:27:15 -0800 | [diff] [blame] | 303 | // Don't support MANUAL, ZSL templates |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 304 | status = Status::ILLEGAL_ARGUMENT; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 305 | break; |
| 306 | default: |
| 307 | ALOGE("%s: unknown request template type %d", __FUNCTION__, static_cast<int>(type)); |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 308 | status = Status::ILLEGAL_ARGUMENT; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 309 | break; |
| 310 | } |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 311 | return status; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | Return<void> ExternalCameraDeviceSession::configureStreams( |
| 315 | const V3_2::StreamConfiguration& streams, |
| 316 | ICameraDeviceSession::configureStreams_cb _hidl_cb) { |
| 317 | V3_2::HalStreamConfiguration outStreams; |
| 318 | V3_3::HalStreamConfiguration outStreams_v33; |
| 319 | Mutex::Autolock _il(mInterfaceLock); |
| 320 | |
| 321 | Status status = configureStreams(streams, &outStreams_v33); |
| 322 | size_t size = outStreams_v33.streams.size(); |
| 323 | outStreams.streams.resize(size); |
| 324 | for (size_t i = 0; i < size; i++) { |
| 325 | outStreams.streams[i] = outStreams_v33.streams[i].v3_2; |
| 326 | } |
| 327 | _hidl_cb(status, outStreams); |
| 328 | return Void(); |
| 329 | } |
| 330 | |
| 331 | Return<void> ExternalCameraDeviceSession::configureStreams_3_3( |
| 332 | const V3_2::StreamConfiguration& streams, |
| 333 | ICameraDeviceSession::configureStreams_3_3_cb _hidl_cb) { |
| 334 | V3_3::HalStreamConfiguration outStreams; |
| 335 | Mutex::Autolock _il(mInterfaceLock); |
| 336 | |
| 337 | Status status = configureStreams(streams, &outStreams); |
| 338 | _hidl_cb(status, outStreams); |
| 339 | return Void(); |
| 340 | } |
| 341 | |
| 342 | Return<void> ExternalCameraDeviceSession::configureStreams_3_4( |
| 343 | const V3_4::StreamConfiguration& requestedConfiguration, |
| 344 | ICameraDeviceSession::configureStreams_3_4_cb _hidl_cb) { |
| 345 | V3_2::StreamConfiguration config_v32; |
| 346 | V3_3::HalStreamConfiguration outStreams_v33; |
Yin-Chia Yeh | 4c64118 | 2018-08-06 15:01:20 -0700 | [diff] [blame] | 347 | V3_4::HalStreamConfiguration outStreams; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 348 | Mutex::Autolock _il(mInterfaceLock); |
| 349 | |
| 350 | config_v32.operationMode = requestedConfiguration.operationMode; |
| 351 | config_v32.streams.resize(requestedConfiguration.streams.size()); |
Yin-Chia Yeh | 4c64118 | 2018-08-06 15:01:20 -0700 | [diff] [blame] | 352 | uint32_t blobBufferSize = 0; |
| 353 | int numStallStream = 0; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 354 | for (size_t i = 0; i < config_v32.streams.size(); i++) { |
| 355 | config_v32.streams[i] = requestedConfiguration.streams[i].v3_2; |
Yin-Chia Yeh | 4c64118 | 2018-08-06 15:01:20 -0700 | [diff] [blame] | 356 | if (config_v32.streams[i].format == PixelFormat::BLOB) { |
| 357 | blobBufferSize = requestedConfiguration.streams[i].bufferSize; |
| 358 | numStallStream++; |
| 359 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Yin-Chia Yeh | 4c64118 | 2018-08-06 15:01:20 -0700 | [diff] [blame] | 362 | // Fail early if there are multiple BLOB streams |
| 363 | if (numStallStream > kMaxStallStream) { |
| 364 | ALOGE("%s: too many stall streams (expect <= %d, got %d)", __FUNCTION__, |
| 365 | kMaxStallStream, numStallStream); |
| 366 | _hidl_cb(Status::ILLEGAL_ARGUMENT, outStreams); |
| 367 | return Void(); |
| 368 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 369 | |
Yin-Chia Yeh | 4c64118 | 2018-08-06 15:01:20 -0700 | [diff] [blame] | 370 | Status status = configureStreams(config_v32, &outStreams_v33, blobBufferSize); |
| 371 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 372 | outStreams.streams.resize(outStreams_v33.streams.size()); |
| 373 | for (size_t i = 0; i < outStreams.streams.size(); i++) { |
| 374 | outStreams.streams[i].v3_3 = outStreams_v33.streams[i]; |
| 375 | } |
| 376 | _hidl_cb(status, outStreams); |
| 377 | return Void(); |
| 378 | } |
| 379 | |
| 380 | Return<void> ExternalCameraDeviceSession::getCaptureRequestMetadataQueue( |
| 381 | ICameraDeviceSession::getCaptureRequestMetadataQueue_cb _hidl_cb) { |
| 382 | Mutex::Autolock _il(mInterfaceLock); |
| 383 | _hidl_cb(*mRequestMetadataQueue->getDesc()); |
| 384 | return Void(); |
| 385 | } |
| 386 | |
| 387 | Return<void> ExternalCameraDeviceSession::getCaptureResultMetadataQueue( |
| 388 | ICameraDeviceSession::getCaptureResultMetadataQueue_cb _hidl_cb) { |
| 389 | Mutex::Autolock _il(mInterfaceLock); |
| 390 | _hidl_cb(*mResultMetadataQueue->getDesc()); |
| 391 | return Void(); |
| 392 | } |
| 393 | |
| 394 | Return<void> ExternalCameraDeviceSession::processCaptureRequest( |
| 395 | const hidl_vec<CaptureRequest>& requests, |
| 396 | const hidl_vec<BufferCache>& cachesToRemove, |
| 397 | ICameraDeviceSession::processCaptureRequest_cb _hidl_cb) { |
| 398 | Mutex::Autolock _il(mInterfaceLock); |
| 399 | updateBufferCaches(cachesToRemove); |
| 400 | |
| 401 | uint32_t numRequestProcessed = 0; |
| 402 | Status s = Status::OK; |
| 403 | for (size_t i = 0; i < requests.size(); i++, numRequestProcessed++) { |
| 404 | s = processOneCaptureRequest(requests[i]); |
| 405 | if (s != Status::OK) { |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | _hidl_cb(s, numRequestProcessed); |
| 411 | return Void(); |
| 412 | } |
| 413 | |
| 414 | Return<void> ExternalCameraDeviceSession::processCaptureRequest_3_4( |
| 415 | const hidl_vec<V3_4::CaptureRequest>& requests, |
| 416 | const hidl_vec<V3_2::BufferCache>& cachesToRemove, |
| 417 | ICameraDeviceSession::processCaptureRequest_3_4_cb _hidl_cb) { |
| 418 | Mutex::Autolock _il(mInterfaceLock); |
| 419 | updateBufferCaches(cachesToRemove); |
| 420 | |
| 421 | uint32_t numRequestProcessed = 0; |
| 422 | Status s = Status::OK; |
| 423 | for (size_t i = 0; i < requests.size(); i++, numRequestProcessed++) { |
| 424 | s = processOneCaptureRequest(requests[i].v3_2); |
| 425 | if (s != Status::OK) { |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | _hidl_cb(s, numRequestProcessed); |
| 431 | return Void(); |
| 432 | } |
| 433 | |
| 434 | Return<Status> ExternalCameraDeviceSession::flush() { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 435 | ATRACE_CALL(); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 436 | Mutex::Autolock _il(mInterfaceLock); |
| 437 | Status status = initStatus(); |
| 438 | if (status != Status::OK) { |
| 439 | return status; |
| 440 | } |
| 441 | mOutputThread->flush(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 442 | return Status::OK; |
| 443 | } |
| 444 | |
| 445 | Return<void> ExternalCameraDeviceSession::close() { |
| 446 | Mutex::Autolock _il(mInterfaceLock); |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 447 | bool closed = isClosed(); |
| 448 | if (!closed) { |
| 449 | mOutputThread->flush(); |
| 450 | mOutputThread->requestExit(); |
| 451 | mOutputThread->join(); |
| 452 | |
| 453 | Mutex::Autolock _l(mLock); |
| 454 | // free all buffers |
| 455 | for(auto pair : mStreamMap) { |
| 456 | cleanupBuffersLocked(/*Stream ID*/pair.first); |
| 457 | } |
| 458 | v4l2StreamOffLocked(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 459 | ALOGV("%s: closing V4L2 camera FD %d", __FUNCTION__, mV4l2Fd.get()); |
| 460 | mV4l2Fd.reset(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 461 | mClosed = true; |
| 462 | } |
| 463 | return Void(); |
| 464 | } |
| 465 | |
| 466 | Status ExternalCameraDeviceSession::importRequest( |
| 467 | const CaptureRequest& request, |
| 468 | hidl_vec<buffer_handle_t*>& allBufPtrs, |
| 469 | hidl_vec<int>& allFences) { |
| 470 | size_t numOutputBufs = request.outputBuffers.size(); |
| 471 | size_t numBufs = numOutputBufs; |
| 472 | // Validate all I/O buffers |
| 473 | hidl_vec<buffer_handle_t> allBufs; |
| 474 | hidl_vec<uint64_t> allBufIds; |
| 475 | allBufs.resize(numBufs); |
| 476 | allBufIds.resize(numBufs); |
| 477 | allBufPtrs.resize(numBufs); |
| 478 | allFences.resize(numBufs); |
| 479 | std::vector<int32_t> streamIds(numBufs); |
| 480 | |
| 481 | for (size_t i = 0; i < numOutputBufs; i++) { |
| 482 | allBufs[i] = request.outputBuffers[i].buffer.getNativeHandle(); |
| 483 | allBufIds[i] = request.outputBuffers[i].bufferId; |
| 484 | allBufPtrs[i] = &allBufs[i]; |
| 485 | streamIds[i] = request.outputBuffers[i].streamId; |
| 486 | } |
| 487 | |
| 488 | for (size_t i = 0; i < numBufs; i++) { |
| 489 | buffer_handle_t buf = allBufs[i]; |
| 490 | uint64_t bufId = allBufIds[i]; |
| 491 | CirculatingBuffers& cbs = mCirculatingBuffers[streamIds[i]]; |
| 492 | if (cbs.count(bufId) == 0) { |
| 493 | if (buf == nullptr) { |
| 494 | ALOGE("%s: bufferId %" PRIu64 " has null buffer handle!", __FUNCTION__, bufId); |
| 495 | return Status::ILLEGAL_ARGUMENT; |
| 496 | } |
| 497 | // Register a newly seen buffer |
| 498 | buffer_handle_t importedBuf = buf; |
| 499 | sHandleImporter.importBuffer(importedBuf); |
| 500 | if (importedBuf == nullptr) { |
| 501 | ALOGE("%s: output buffer %zu is invalid!", __FUNCTION__, i); |
| 502 | return Status::INTERNAL_ERROR; |
| 503 | } else { |
| 504 | cbs[bufId] = importedBuf; |
| 505 | } |
| 506 | } |
| 507 | allBufPtrs[i] = &cbs[bufId]; |
| 508 | } |
| 509 | |
| 510 | // All buffers are imported. Now validate output buffer acquire fences |
| 511 | for (size_t i = 0; i < numOutputBufs; i++) { |
| 512 | if (!sHandleImporter.importFence( |
| 513 | request.outputBuffers[i].acquireFence, allFences[i])) { |
| 514 | ALOGE("%s: output buffer %zu acquire fence is invalid", __FUNCTION__, i); |
| 515 | cleanupInflightFences(allFences, i); |
| 516 | return Status::INTERNAL_ERROR; |
| 517 | } |
| 518 | } |
| 519 | return Status::OK; |
| 520 | } |
| 521 | |
| 522 | void ExternalCameraDeviceSession::cleanupInflightFences( |
| 523 | hidl_vec<int>& allFences, size_t numFences) { |
| 524 | for (size_t j = 0; j < numFences; j++) { |
| 525 | sHandleImporter.closeFence(allFences[j]); |
| 526 | } |
| 527 | } |
| 528 | |
Yin-Chia Yeh | 3aa9ae9 | 2018-02-23 17:21:51 -0800 | [diff] [blame] | 529 | int ExternalCameraDeviceSession::waitForV4L2BufferReturnLocked(std::unique_lock<std::mutex>& lk) { |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 530 | ATRACE_CALL(); |
Yin-Chia Yeh | 3aa9ae9 | 2018-02-23 17:21:51 -0800 | [diff] [blame] | 531 | std::chrono::seconds timeout = std::chrono::seconds(kBufferWaitTimeoutSec); |
| 532 | mLock.unlock(); |
| 533 | auto st = mV4L2BufferReturned.wait_for(lk, timeout); |
| 534 | // Here we introduce a order where mV4l2BufferLock is acquired before mLock, while |
| 535 | // the normal lock acquisition order is reversed. This is fine because in most of |
| 536 | // cases we are protected by mInterfaceLock. The only thread that can cause deadlock |
| 537 | // is the OutputThread, where we do need to make sure we don't acquire mLock then |
| 538 | // mV4l2BufferLock |
| 539 | mLock.lock(); |
| 540 | if (st == std::cv_status::timeout) { |
| 541 | ALOGE("%s: wait for V4L2 buffer return timeout!", __FUNCTION__); |
| 542 | return -1; |
| 543 | } |
| 544 | return 0; |
| 545 | } |
| 546 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 547 | Status ExternalCameraDeviceSession::processOneCaptureRequest(const CaptureRequest& request) { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 548 | ATRACE_CALL(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 549 | Status status = initStatus(); |
| 550 | if (status != Status::OK) { |
| 551 | return status; |
| 552 | } |
| 553 | |
| 554 | if (request.inputBuffer.streamId != -1) { |
| 555 | ALOGE("%s: external camera does not support reprocessing!", __FUNCTION__); |
| 556 | return Status::ILLEGAL_ARGUMENT; |
| 557 | } |
| 558 | |
| 559 | Mutex::Autolock _l(mLock); |
| 560 | if (!mV4l2Streaming) { |
| 561 | ALOGE("%s: cannot process request in streamOff state!", __FUNCTION__); |
| 562 | return Status::INTERNAL_ERROR; |
| 563 | } |
| 564 | |
| 565 | const camera_metadata_t *rawSettings = nullptr; |
| 566 | bool converted = true; |
| 567 | CameraMetadata settingsFmq; // settings from FMQ |
| 568 | if (request.fmqSettingsSize > 0) { |
| 569 | // non-blocking read; client must write metadata before calling |
| 570 | // processOneCaptureRequest |
| 571 | settingsFmq.resize(request.fmqSettingsSize); |
| 572 | bool read = mRequestMetadataQueue->read(settingsFmq.data(), request.fmqSettingsSize); |
| 573 | if (read) { |
| 574 | converted = V3_2::implementation::convertFromHidl(settingsFmq, &rawSettings); |
| 575 | } else { |
| 576 | ALOGE("%s: capture request settings metadata couldn't be read from fmq!", __FUNCTION__); |
| 577 | converted = false; |
| 578 | } |
| 579 | } else { |
| 580 | converted = V3_2::implementation::convertFromHidl(request.settings, &rawSettings); |
| 581 | } |
| 582 | |
| 583 | if (converted && rawSettings != nullptr) { |
| 584 | mLatestReqSetting = rawSettings; |
| 585 | } |
| 586 | |
| 587 | if (!converted) { |
| 588 | ALOGE("%s: capture request settings metadata is corrupt!", __FUNCTION__); |
| 589 | return Status::ILLEGAL_ARGUMENT; |
| 590 | } |
| 591 | |
| 592 | if (mFirstRequest && rawSettings == nullptr) { |
| 593 | ALOGE("%s: capture request settings must not be null for first request!", |
| 594 | __FUNCTION__); |
| 595 | return Status::ILLEGAL_ARGUMENT; |
| 596 | } |
| 597 | |
| 598 | hidl_vec<buffer_handle_t*> allBufPtrs; |
| 599 | hidl_vec<int> allFences; |
| 600 | size_t numOutputBufs = request.outputBuffers.size(); |
| 601 | |
| 602 | if (numOutputBufs == 0) { |
| 603 | ALOGE("%s: capture request must have at least one output buffer!", __FUNCTION__); |
| 604 | return Status::ILLEGAL_ARGUMENT; |
| 605 | } |
| 606 | |
Yin-Chia Yeh | 3aa9ae9 | 2018-02-23 17:21:51 -0800 | [diff] [blame] | 607 | camera_metadata_entry fpsRange = mLatestReqSetting.find(ANDROID_CONTROL_AE_TARGET_FPS_RANGE); |
| 608 | if (fpsRange.count == 2) { |
| 609 | double requestFpsMax = fpsRange.data.i32[1]; |
| 610 | double closestFps = 0.0; |
| 611 | double fpsError = 1000.0; |
| 612 | bool fpsSupported = false; |
| 613 | for (const auto& fr : mV4l2StreamingFmt.frameRates) { |
| 614 | double f = fr.getDouble(); |
| 615 | if (std::fabs(requestFpsMax - f) < 1.0) { |
| 616 | fpsSupported = true; |
| 617 | break; |
| 618 | } |
| 619 | if (std::fabs(requestFpsMax - f) < fpsError) { |
| 620 | fpsError = std::fabs(requestFpsMax - f); |
| 621 | closestFps = f; |
| 622 | } |
| 623 | } |
| 624 | if (!fpsSupported) { |
| 625 | /* This can happen in a few scenarios: |
| 626 | * 1. The application is sending a FPS range not supported by the configured outputs. |
| 627 | * 2. The application is sending a valid FPS range for all cofigured outputs, but |
| 628 | * the selected V4L2 size can only run at slower speed. This should be very rare |
| 629 | * though: for this to happen a sensor needs to support at least 3 different aspect |
| 630 | * ratio outputs, and when (at least) two outputs are both not the main aspect ratio |
| 631 | * of the webcam, a third size that's larger might be picked and runs into this |
| 632 | * issue. |
| 633 | */ |
| 634 | ALOGW("%s: cannot reach fps %d! Will do %f instead", |
| 635 | __FUNCTION__, fpsRange.data.i32[1], closestFps); |
| 636 | requestFpsMax = closestFps; |
| 637 | } |
| 638 | |
| 639 | if (requestFpsMax != mV4l2StreamingFps) { |
| 640 | { |
| 641 | std::unique_lock<std::mutex> lk(mV4l2BufferLock); |
| 642 | while (mNumDequeuedV4l2Buffers != 0) { |
| 643 | // Wait until pipeline is idle before reconfigure stream |
| 644 | int waitRet = waitForV4L2BufferReturnLocked(lk); |
| 645 | if (waitRet != 0) { |
| 646 | ALOGE("%s: wait for pipeline idle failed!", __FUNCTION__); |
| 647 | return Status::INTERNAL_ERROR; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | configureV4l2StreamLocked(mV4l2StreamingFmt, requestFpsMax); |
| 652 | } |
| 653 | } |
| 654 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 655 | status = importRequest(request, allBufPtrs, allFences); |
| 656 | if (status != Status::OK) { |
| 657 | return status; |
| 658 | } |
| 659 | |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 660 | nsecs_t shutterTs = 0; |
| 661 | sp<V4L2Frame> frameIn = dequeueV4l2FrameLocked(&shutterTs); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 662 | if ( frameIn == nullptr) { |
| 663 | ALOGE("%s: V4L2 deque frame failed!", __FUNCTION__); |
| 664 | return Status::INTERNAL_ERROR; |
| 665 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 666 | |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 667 | std::shared_ptr<HalRequest> halReq = std::make_shared<HalRequest>(); |
| 668 | halReq->frameNumber = request.frameNumber; |
| 669 | halReq->setting = mLatestReqSetting; |
| 670 | halReq->frameIn = frameIn; |
| 671 | halReq->shutterTs = shutterTs; |
| 672 | halReq->buffers.resize(numOutputBufs); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 673 | for (size_t i = 0; i < numOutputBufs; i++) { |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 674 | HalStreamBuffer& halBuf = halReq->buffers[i]; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 675 | int streamId = halBuf.streamId = request.outputBuffers[i].streamId; |
| 676 | halBuf.bufferId = request.outputBuffers[i].bufferId; |
| 677 | const Stream& stream = mStreamMap[streamId]; |
| 678 | halBuf.width = stream.width; |
| 679 | halBuf.height = stream.height; |
| 680 | halBuf.format = stream.format; |
| 681 | halBuf.usage = stream.usage; |
| 682 | halBuf.bufPtr = allBufPtrs[i]; |
| 683 | halBuf.acquireFence = allFences[i]; |
| 684 | halBuf.fenceTimeout = false; |
| 685 | } |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 686 | { |
| 687 | std::lock_guard<std::mutex> lk(mInflightFramesLock); |
| 688 | mInflightFrames.insert(halReq->frameNumber); |
| 689 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 690 | // Send request to OutputThread for the rest of processing |
| 691 | mOutputThread->submitRequest(halReq); |
| 692 | mFirstRequest = false; |
| 693 | return Status::OK; |
| 694 | } |
| 695 | |
| 696 | void ExternalCameraDeviceSession::notifyShutter(uint32_t frameNumber, nsecs_t shutterTs) { |
| 697 | NotifyMsg msg; |
| 698 | msg.type = MsgType::SHUTTER; |
| 699 | msg.msg.shutter.frameNumber = frameNumber; |
| 700 | msg.msg.shutter.timestamp = shutterTs; |
| 701 | mCallback->notify({msg}); |
| 702 | } |
| 703 | |
| 704 | void ExternalCameraDeviceSession::notifyError( |
| 705 | uint32_t frameNumber, int32_t streamId, ErrorCode ec) { |
| 706 | NotifyMsg msg; |
| 707 | msg.type = MsgType::ERROR; |
| 708 | msg.msg.error.frameNumber = frameNumber; |
| 709 | msg.msg.error.errorStreamId = streamId; |
| 710 | msg.msg.error.errorCode = ec; |
| 711 | mCallback->notify({msg}); |
| 712 | } |
| 713 | |
| 714 | //TODO: refactor with processCaptureResult |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 715 | Status ExternalCameraDeviceSession::processCaptureRequestError( |
| 716 | const std::shared_ptr<HalRequest>& req) { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 717 | ATRACE_CALL(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 718 | // Return V4L2 buffer to V4L2 buffer queue |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 719 | enqueueV4l2Frame(req->frameIn); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 720 | |
| 721 | // NotifyShutter |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 722 | notifyShutter(req->frameNumber, req->shutterTs); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 723 | |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 724 | notifyError(/*frameNum*/req->frameNumber, /*stream*/-1, ErrorCode::ERROR_REQUEST); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 725 | |
| 726 | // Fill output buffers |
| 727 | hidl_vec<CaptureResult> results; |
| 728 | results.resize(1); |
| 729 | CaptureResult& result = results[0]; |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 730 | result.frameNumber = req->frameNumber; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 731 | result.partialResult = 1; |
| 732 | result.inputBuffer.streamId = -1; |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 733 | result.outputBuffers.resize(req->buffers.size()); |
| 734 | for (size_t i = 0; i < req->buffers.size(); i++) { |
| 735 | result.outputBuffers[i].streamId = req->buffers[i].streamId; |
| 736 | result.outputBuffers[i].bufferId = req->buffers[i].bufferId; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 737 | result.outputBuffers[i].status = BufferStatus::ERROR; |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 738 | if (req->buffers[i].acquireFence >= 0) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 739 | native_handle_t* handle = native_handle_create(/*numFds*/1, /*numInts*/0); |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 740 | handle->data[0] = req->buffers[i].acquireFence; |
Yin-Chia Yeh | 1e08966 | 2018-02-02 15:57:58 -0800 | [diff] [blame] | 741 | result.outputBuffers[i].releaseFence.setTo(handle, /*shouldOwn*/false); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
| 745 | // update inflight records |
| 746 | { |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 747 | std::lock_guard<std::mutex> lk(mInflightFramesLock); |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 748 | mInflightFrames.erase(req->frameNumber); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | // Callback into framework |
| 752 | invokeProcessCaptureResultCallback(results, /* tryWriteFmq */true); |
| 753 | freeReleaseFences(results); |
| 754 | return Status::OK; |
| 755 | } |
| 756 | |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 757 | Status ExternalCameraDeviceSession::processCaptureResult(std::shared_ptr<HalRequest>& req) { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 758 | ATRACE_CALL(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 759 | // Return V4L2 buffer to V4L2 buffer queue |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 760 | enqueueV4l2Frame(req->frameIn); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 761 | |
| 762 | // NotifyShutter |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 763 | notifyShutter(req->frameNumber, req->shutterTs); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 764 | |
| 765 | // Fill output buffers |
| 766 | hidl_vec<CaptureResult> results; |
| 767 | results.resize(1); |
| 768 | CaptureResult& result = results[0]; |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 769 | result.frameNumber = req->frameNumber; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 770 | result.partialResult = 1; |
| 771 | result.inputBuffer.streamId = -1; |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 772 | result.outputBuffers.resize(req->buffers.size()); |
| 773 | for (size_t i = 0; i < req->buffers.size(); i++) { |
| 774 | result.outputBuffers[i].streamId = req->buffers[i].streamId; |
| 775 | result.outputBuffers[i].bufferId = req->buffers[i].bufferId; |
| 776 | if (req->buffers[i].fenceTimeout) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 777 | result.outputBuffers[i].status = BufferStatus::ERROR; |
| 778 | native_handle_t* handle = native_handle_create(/*numFds*/1, /*numInts*/0); |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 779 | handle->data[0] = req->buffers[i].acquireFence; |
Yin-Chia Yeh | 1e08966 | 2018-02-02 15:57:58 -0800 | [diff] [blame] | 780 | result.outputBuffers[i].releaseFence.setTo(handle, /*shouldOwn*/false); |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 781 | notifyError(req->frameNumber, req->buffers[i].streamId, ErrorCode::ERROR_BUFFER); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 782 | } else { |
| 783 | result.outputBuffers[i].status = BufferStatus::OK; |
| 784 | // TODO: refactor |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 785 | if (req->buffers[i].acquireFence > 0) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 786 | native_handle_t* handle = native_handle_create(/*numFds*/1, /*numInts*/0); |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 787 | handle->data[0] = req->buffers[i].acquireFence; |
Yin-Chia Yeh | 1e08966 | 2018-02-02 15:57:58 -0800 | [diff] [blame] | 788 | result.outputBuffers[i].releaseFence.setTo(handle, /*shouldOwn*/false); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | // Fill capture result metadata |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 794 | fillCaptureResult(req->setting, req->shutterTs); |
| 795 | const camera_metadata_t *rawResult = req->setting.getAndLock(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 796 | V3_2::implementation::convertToHidl(rawResult, &result.result); |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 797 | req->setting.unlock(rawResult); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 798 | |
| 799 | // update inflight records |
| 800 | { |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 801 | std::lock_guard<std::mutex> lk(mInflightFramesLock); |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 802 | mInflightFrames.erase(req->frameNumber); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | // Callback into framework |
| 806 | invokeProcessCaptureResultCallback(results, /* tryWriteFmq */true); |
| 807 | freeReleaseFences(results); |
| 808 | return Status::OK; |
| 809 | } |
| 810 | |
| 811 | void ExternalCameraDeviceSession::invokeProcessCaptureResultCallback( |
| 812 | hidl_vec<CaptureResult> &results, bool tryWriteFmq) { |
| 813 | if (mProcessCaptureResultLock.tryLock() != OK) { |
| 814 | const nsecs_t NS_TO_SECOND = 1000000000; |
| 815 | ALOGV("%s: previous call is not finished! waiting 1s...", __FUNCTION__); |
| 816 | if (mProcessCaptureResultLock.timedLock(/* 1s */NS_TO_SECOND) != OK) { |
| 817 | ALOGE("%s: cannot acquire lock in 1s, cannot proceed", |
| 818 | __FUNCTION__); |
| 819 | return; |
| 820 | } |
| 821 | } |
| 822 | if (tryWriteFmq && mResultMetadataQueue->availableToWrite() > 0) { |
| 823 | for (CaptureResult &result : results) { |
| 824 | if (result.result.size() > 0) { |
| 825 | if (mResultMetadataQueue->write(result.result.data(), result.result.size())) { |
| 826 | result.fmqResultSize = result.result.size(); |
| 827 | result.result.resize(0); |
| 828 | } else { |
| 829 | ALOGW("%s: couldn't utilize fmq, fall back to hwbinder", __FUNCTION__); |
| 830 | result.fmqResultSize = 0; |
| 831 | } |
| 832 | } else { |
| 833 | result.fmqResultSize = 0; |
| 834 | } |
| 835 | } |
| 836 | } |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 837 | auto status = mCallback->processCaptureResult(results); |
| 838 | if (!status.isOk()) { |
| 839 | ALOGE("%s: processCaptureResult ERROR : %s", __FUNCTION__, |
| 840 | status.description().c_str()); |
| 841 | } |
| 842 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 843 | mProcessCaptureResultLock.unlock(); |
| 844 | } |
| 845 | |
| 846 | void ExternalCameraDeviceSession::freeReleaseFences(hidl_vec<CaptureResult>& results) { |
| 847 | for (auto& result : results) { |
| 848 | if (result.inputBuffer.releaseFence.getNativeHandle() != nullptr) { |
| 849 | native_handle_t* handle = const_cast<native_handle_t*>( |
| 850 | result.inputBuffer.releaseFence.getNativeHandle()); |
| 851 | native_handle_close(handle); |
| 852 | native_handle_delete(handle); |
| 853 | } |
| 854 | for (auto& buf : result.outputBuffers) { |
| 855 | if (buf.releaseFence.getNativeHandle() != nullptr) { |
| 856 | native_handle_t* handle = const_cast<native_handle_t*>( |
| 857 | buf.releaseFence.getNativeHandle()); |
| 858 | native_handle_close(handle); |
| 859 | native_handle_delete(handle); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | return; |
| 864 | } |
| 865 | |
| 866 | ExternalCameraDeviceSession::OutputThread::OutputThread( |
| 867 | wp<ExternalCameraDeviceSession> parent, |
| 868 | CroppingType ct) : mParent(parent), mCroppingType(ct) {} |
| 869 | |
| 870 | ExternalCameraDeviceSession::OutputThread::~OutputThread() {} |
| 871 | |
Yin-Chia Yeh | 2d61bfd | 2018-03-14 13:50:23 -0700 | [diff] [blame] | 872 | void ExternalCameraDeviceSession::OutputThread::setExifMakeModel( |
| 873 | const std::string& make, const std::string& model) { |
| 874 | mExifMake = make; |
| 875 | mExifModel = model; |
| 876 | } |
| 877 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 878 | uint32_t ExternalCameraDeviceSession::OutputThread::getFourCcFromLayout( |
| 879 | const YCbCrLayout& layout) { |
| 880 | intptr_t cb = reinterpret_cast<intptr_t>(layout.cb); |
| 881 | intptr_t cr = reinterpret_cast<intptr_t>(layout.cr); |
| 882 | if (std::abs(cb - cr) == 1 && layout.chromaStep == 2) { |
| 883 | // Interleaved format |
| 884 | if (layout.cb > layout.cr) { |
| 885 | return V4L2_PIX_FMT_NV21; |
| 886 | } else { |
| 887 | return V4L2_PIX_FMT_NV12; |
| 888 | } |
| 889 | } else if (layout.chromaStep == 1) { |
| 890 | // Planar format |
| 891 | if (layout.cb > layout.cr) { |
| 892 | return V4L2_PIX_FMT_YVU420; // YV12 |
| 893 | } else { |
| 894 | return V4L2_PIX_FMT_YUV420; // YU12 |
| 895 | } |
| 896 | } else { |
| 897 | return FLEX_YUV_GENERIC; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | int ExternalCameraDeviceSession::OutputThread::getCropRect( |
| 902 | CroppingType ct, const Size& inSize, const Size& outSize, IMapper::Rect* out) { |
| 903 | if (out == nullptr) { |
| 904 | ALOGE("%s: out is null", __FUNCTION__); |
| 905 | return -1; |
| 906 | } |
Yin-Chia Yeh | 4acd76e | 2018-01-23 15:29:14 -0800 | [diff] [blame] | 907 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 908 | uint32_t inW = inSize.width; |
| 909 | uint32_t inH = inSize.height; |
| 910 | uint32_t outW = outSize.width; |
| 911 | uint32_t outH = outSize.height; |
| 912 | |
Yin-Chia Yeh | 4acd76e | 2018-01-23 15:29:14 -0800 | [diff] [blame] | 913 | // Handle special case where aspect ratio is close to input but scaled |
| 914 | // dimension is slightly larger than input |
| 915 | float arIn = ASPECT_RATIO(inSize); |
| 916 | float arOut = ASPECT_RATIO(outSize); |
| 917 | if (isAspectRatioClose(arIn, arOut)) { |
| 918 | out->left = 0; |
| 919 | out->top = 0; |
| 920 | out->width = inW; |
| 921 | out->height = inH; |
| 922 | return 0; |
| 923 | } |
| 924 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 925 | if (ct == VERTICAL) { |
| 926 | uint64_t scaledOutH = static_cast<uint64_t>(outH) * inW / outW; |
| 927 | if (scaledOutH > inH) { |
| 928 | ALOGE("%s: Output size %dx%d cannot be vertically cropped from input size %dx%d", |
| 929 | __FUNCTION__, outW, outH, inW, inH); |
| 930 | return -1; |
| 931 | } |
| 932 | scaledOutH = scaledOutH & ~0x1; // make it multiple of 2 |
| 933 | |
| 934 | out->left = 0; |
| 935 | out->top = ((inH - scaledOutH) / 2) & ~0x1; |
| 936 | out->width = inW; |
| 937 | out->height = static_cast<int32_t>(scaledOutH); |
| 938 | ALOGV("%s: crop %dx%d to %dx%d: top %d, scaledH %d", |
| 939 | __FUNCTION__, inW, inH, outW, outH, out->top, static_cast<int32_t>(scaledOutH)); |
| 940 | } else { |
| 941 | uint64_t scaledOutW = static_cast<uint64_t>(outW) * inH / outH; |
| 942 | if (scaledOutW > inW) { |
| 943 | ALOGE("%s: Output size %dx%d cannot be horizontally cropped from input size %dx%d", |
| 944 | __FUNCTION__, outW, outH, inW, inH); |
| 945 | return -1; |
| 946 | } |
| 947 | scaledOutW = scaledOutW & ~0x1; // make it multiple of 2 |
| 948 | |
| 949 | out->left = ((inW - scaledOutW) / 2) & ~0x1; |
| 950 | out->top = 0; |
| 951 | out->width = static_cast<int32_t>(scaledOutW); |
| 952 | out->height = inH; |
| 953 | ALOGV("%s: crop %dx%d to %dx%d: top %d, scaledW %d", |
| 954 | __FUNCTION__, inW, inH, outW, outH, out->top, static_cast<int32_t>(scaledOutW)); |
| 955 | } |
| 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | int ExternalCameraDeviceSession::OutputThread::cropAndScaleLocked( |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 961 | sp<AllocatedFrame>& in, const Size& outSz, YCbCrLayout* out) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 962 | Size inSz = {in->mWidth, in->mHeight}; |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 963 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 964 | int ret; |
| 965 | if (inSz == outSz) { |
| 966 | ret = in->getLayout(out); |
| 967 | if (ret != 0) { |
| 968 | ALOGE("%s: failed to get input image layout", __FUNCTION__); |
| 969 | return ret; |
| 970 | } |
| 971 | return ret; |
| 972 | } |
| 973 | |
| 974 | // Cropping to output aspect ratio |
| 975 | IMapper::Rect inputCrop; |
| 976 | ret = getCropRect(mCroppingType, inSz, outSz, &inputCrop); |
| 977 | if (ret != 0) { |
| 978 | ALOGE("%s: failed to compute crop rect for output size %dx%d", |
| 979 | __FUNCTION__, outSz.width, outSz.height); |
| 980 | return ret; |
| 981 | } |
| 982 | |
| 983 | YCbCrLayout croppedLayout; |
| 984 | ret = in->getCroppedLayout(inputCrop, &croppedLayout); |
| 985 | if (ret != 0) { |
| 986 | ALOGE("%s: failed to crop input image %dx%d to output size %dx%d", |
| 987 | __FUNCTION__, inSz.width, inSz.height, outSz.width, outSz.height); |
| 988 | return ret; |
| 989 | } |
| 990 | |
| 991 | if ((mCroppingType == VERTICAL && inSz.width == outSz.width) || |
| 992 | (mCroppingType == HORIZONTAL && inSz.height == outSz.height)) { |
| 993 | // No scale is needed |
| 994 | *out = croppedLayout; |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | auto it = mScaledYu12Frames.find(outSz); |
| 999 | sp<AllocatedFrame> scaledYu12Buf; |
| 1000 | if (it != mScaledYu12Frames.end()) { |
| 1001 | scaledYu12Buf = it->second; |
| 1002 | } else { |
| 1003 | it = mIntermediateBuffers.find(outSz); |
| 1004 | if (it == mIntermediateBuffers.end()) { |
| 1005 | ALOGE("%s: failed to find intermediate buffer size %dx%d", |
| 1006 | __FUNCTION__, outSz.width, outSz.height); |
| 1007 | return -1; |
| 1008 | } |
| 1009 | scaledYu12Buf = it->second; |
| 1010 | } |
| 1011 | // Scale |
| 1012 | YCbCrLayout outLayout; |
| 1013 | ret = scaledYu12Buf->getLayout(&outLayout); |
| 1014 | if (ret != 0) { |
| 1015 | ALOGE("%s: failed to get output buffer layout", __FUNCTION__); |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
| 1019 | ret = libyuv::I420Scale( |
| 1020 | static_cast<uint8_t*>(croppedLayout.y), |
| 1021 | croppedLayout.yStride, |
| 1022 | static_cast<uint8_t*>(croppedLayout.cb), |
| 1023 | croppedLayout.cStride, |
| 1024 | static_cast<uint8_t*>(croppedLayout.cr), |
| 1025 | croppedLayout.cStride, |
| 1026 | inputCrop.width, |
| 1027 | inputCrop.height, |
| 1028 | static_cast<uint8_t*>(outLayout.y), |
| 1029 | outLayout.yStride, |
| 1030 | static_cast<uint8_t*>(outLayout.cb), |
| 1031 | outLayout.cStride, |
| 1032 | static_cast<uint8_t*>(outLayout.cr), |
| 1033 | outLayout.cStride, |
| 1034 | outSz.width, |
| 1035 | outSz.height, |
| 1036 | // TODO: b/72261744 see if we can use better filter without losing too much perf |
| 1037 | libyuv::FilterMode::kFilterNone); |
| 1038 | |
| 1039 | if (ret != 0) { |
| 1040 | ALOGE("%s: failed to scale buffer from %dx%d to %dx%d. Ret %d", |
| 1041 | __FUNCTION__, inputCrop.width, inputCrop.height, |
| 1042 | outSz.width, outSz.height, ret); |
| 1043 | return ret; |
| 1044 | } |
| 1045 | |
| 1046 | *out = outLayout; |
| 1047 | mScaledYu12Frames.insert({outSz, scaledYu12Buf}); |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1051 | |
| 1052 | int ExternalCameraDeviceSession::OutputThread::cropAndScaleThumbLocked( |
| 1053 | sp<AllocatedFrame>& in, const Size &outSz, YCbCrLayout* out) { |
| 1054 | Size inSz {in->mWidth, in->mHeight}; |
| 1055 | |
| 1056 | if ((outSz.width * outSz.height) > |
| 1057 | (mYu12ThumbFrame->mWidth * mYu12ThumbFrame->mHeight)) { |
| 1058 | ALOGE("%s: Requested thumbnail size too big (%d,%d) > (%d,%d)", |
| 1059 | __FUNCTION__, outSz.width, outSz.height, |
| 1060 | mYu12ThumbFrame->mWidth, mYu12ThumbFrame->mHeight); |
| 1061 | return -1; |
| 1062 | } |
| 1063 | |
| 1064 | int ret; |
| 1065 | |
| 1066 | /* This will crop-and-zoom the input YUV frame to the thumbnail size |
| 1067 | * Based on the following logic: |
| 1068 | * 1) Square pixels come in, square pixels come out, therefore single |
| 1069 | * scale factor is computed to either make input bigger or smaller |
| 1070 | * depending on if we are upscaling or downscaling |
| 1071 | * 2) That single scale factor would either make height too tall or width |
| 1072 | * too wide so we need to crop the input either horizontally or vertically |
| 1073 | * but not both |
| 1074 | */ |
| 1075 | |
| 1076 | /* Convert the input and output dimensions into floats for ease of math */ |
| 1077 | float fWin = static_cast<float>(inSz.width); |
| 1078 | float fHin = static_cast<float>(inSz.height); |
| 1079 | float fWout = static_cast<float>(outSz.width); |
| 1080 | float fHout = static_cast<float>(outSz.height); |
| 1081 | |
| 1082 | /* Compute the one scale factor from (1) above, it will be the smaller of |
| 1083 | * the two possibilities. */ |
| 1084 | float scaleFactor = std::min( fHin / fHout, fWin / fWout ); |
| 1085 | |
| 1086 | /* Since we are crop-and-zooming (as opposed to letter/pillar boxing) we can |
| 1087 | * simply multiply the output by our scaleFactor to get the cropped input |
| 1088 | * size. Note that at least one of {fWcrop, fHcrop} is going to wind up |
| 1089 | * being {fWin, fHin} respectively because fHout or fWout cancels out the |
| 1090 | * scaleFactor calculation above. |
| 1091 | * |
| 1092 | * Specifically: |
| 1093 | * if ( fHin / fHout ) < ( fWin / fWout ) we crop the sides off |
| 1094 | * input, in which case |
| 1095 | * scaleFactor = fHin / fHout |
| 1096 | * fWcrop = fHin / fHout * fWout |
| 1097 | * fHcrop = fHin |
| 1098 | * |
| 1099 | * Note that fWcrop <= fWin ( because ( fHin / fHout ) * fWout < fWin, which |
| 1100 | * is just the inequality above with both sides multiplied by fWout |
| 1101 | * |
| 1102 | * on the other hand if ( fWin / fWout ) < ( fHin / fHout) we crop the top |
| 1103 | * and the bottom off of input, and |
| 1104 | * scaleFactor = fWin / fWout |
| 1105 | * fWcrop = fWin |
| 1106 | * fHCrop = fWin / fWout * fHout |
| 1107 | */ |
| 1108 | float fWcrop = scaleFactor * fWout; |
| 1109 | float fHcrop = scaleFactor * fHout; |
| 1110 | |
| 1111 | /* Convert to integer and truncate to an even number */ |
| 1112 | Size cropSz = { 2*static_cast<uint32_t>(fWcrop/2.0f), |
| 1113 | 2*static_cast<uint32_t>(fHcrop/2.0f) }; |
| 1114 | |
| 1115 | /* Convert to a centered rectange with even top/left */ |
| 1116 | IMapper::Rect inputCrop { |
| 1117 | 2*static_cast<int32_t>((inSz.width - cropSz.width)/4), |
| 1118 | 2*static_cast<int32_t>((inSz.height - cropSz.height)/4), |
| 1119 | static_cast<int32_t>(cropSz.width), |
| 1120 | static_cast<int32_t>(cropSz.height) }; |
| 1121 | |
| 1122 | if ((inputCrop.top < 0) || |
| 1123 | (inputCrop.top >= static_cast<int32_t>(inSz.height)) || |
| 1124 | (inputCrop.left < 0) || |
| 1125 | (inputCrop.left >= static_cast<int32_t>(inSz.width)) || |
| 1126 | (inputCrop.width <= 0) || |
| 1127 | (inputCrop.width + inputCrop.left > static_cast<int32_t>(inSz.width)) || |
| 1128 | (inputCrop.height <= 0) || |
| 1129 | (inputCrop.height + inputCrop.top > static_cast<int32_t>(inSz.height))) |
| 1130 | { |
| 1131 | ALOGE("%s: came up with really wrong crop rectangle",__FUNCTION__); |
| 1132 | ALOGE("%s: input layout %dx%d to for output size %dx%d", |
| 1133 | __FUNCTION__, inSz.width, inSz.height, outSz.width, outSz.height); |
| 1134 | ALOGE("%s: computed input crop +%d,+%d %dx%d", |
| 1135 | __FUNCTION__, inputCrop.left, inputCrop.top, |
| 1136 | inputCrop.width, inputCrop.height); |
| 1137 | return -1; |
| 1138 | } |
| 1139 | |
| 1140 | YCbCrLayout inputLayout; |
| 1141 | ret = in->getCroppedLayout(inputCrop, &inputLayout); |
| 1142 | if (ret != 0) { |
| 1143 | ALOGE("%s: failed to crop input layout %dx%d to for output size %dx%d", |
| 1144 | __FUNCTION__, inSz.width, inSz.height, outSz.width, outSz.height); |
| 1145 | ALOGE("%s: computed input crop +%d,+%d %dx%d", |
| 1146 | __FUNCTION__, inputCrop.left, inputCrop.top, |
| 1147 | inputCrop.width, inputCrop.height); |
| 1148 | return ret; |
| 1149 | } |
| 1150 | ALOGV("%s: crop input layout %dx%d to for output size %dx%d", |
| 1151 | __FUNCTION__, inSz.width, inSz.height, outSz.width, outSz.height); |
| 1152 | ALOGV("%s: computed input crop +%d,+%d %dx%d", |
| 1153 | __FUNCTION__, inputCrop.left, inputCrop.top, |
| 1154 | inputCrop.width, inputCrop.height); |
| 1155 | |
| 1156 | |
| 1157 | // Scale |
| 1158 | YCbCrLayout outFullLayout; |
| 1159 | |
| 1160 | ret = mYu12ThumbFrame->getLayout(&outFullLayout); |
| 1161 | if (ret != 0) { |
| 1162 | ALOGE("%s: failed to get output buffer layout", __FUNCTION__); |
| 1163 | return ret; |
| 1164 | } |
| 1165 | |
| 1166 | |
| 1167 | ret = libyuv::I420Scale( |
| 1168 | static_cast<uint8_t*>(inputLayout.y), |
| 1169 | inputLayout.yStride, |
| 1170 | static_cast<uint8_t*>(inputLayout.cb), |
| 1171 | inputLayout.cStride, |
| 1172 | static_cast<uint8_t*>(inputLayout.cr), |
| 1173 | inputLayout.cStride, |
| 1174 | inputCrop.width, |
| 1175 | inputCrop.height, |
| 1176 | static_cast<uint8_t*>(outFullLayout.y), |
| 1177 | outFullLayout.yStride, |
| 1178 | static_cast<uint8_t*>(outFullLayout.cb), |
| 1179 | outFullLayout.cStride, |
| 1180 | static_cast<uint8_t*>(outFullLayout.cr), |
| 1181 | outFullLayout.cStride, |
| 1182 | outSz.width, |
| 1183 | outSz.height, |
| 1184 | libyuv::FilterMode::kFilterNone); |
| 1185 | |
| 1186 | if (ret != 0) { |
| 1187 | ALOGE("%s: failed to scale buffer from %dx%d to %dx%d. Ret %d", |
| 1188 | __FUNCTION__, inputCrop.width, inputCrop.height, |
| 1189 | outSz.width, outSz.height, ret); |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | *out = outFullLayout; |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1197 | int ExternalCameraDeviceSession::OutputThread::formatConvertLocked( |
| 1198 | const YCbCrLayout& in, const YCbCrLayout& out, Size sz, uint32_t format) { |
| 1199 | int ret = 0; |
| 1200 | switch (format) { |
| 1201 | case V4L2_PIX_FMT_NV21: |
| 1202 | ret = libyuv::I420ToNV21( |
| 1203 | static_cast<uint8_t*>(in.y), |
| 1204 | in.yStride, |
| 1205 | static_cast<uint8_t*>(in.cb), |
| 1206 | in.cStride, |
| 1207 | static_cast<uint8_t*>(in.cr), |
| 1208 | in.cStride, |
| 1209 | static_cast<uint8_t*>(out.y), |
| 1210 | out.yStride, |
| 1211 | static_cast<uint8_t*>(out.cr), |
| 1212 | out.cStride, |
| 1213 | sz.width, |
| 1214 | sz.height); |
| 1215 | if (ret != 0) { |
| 1216 | ALOGE("%s: convert to NV21 buffer failed! ret %d", |
| 1217 | __FUNCTION__, ret); |
| 1218 | return ret; |
| 1219 | } |
| 1220 | break; |
| 1221 | case V4L2_PIX_FMT_NV12: |
| 1222 | ret = libyuv::I420ToNV12( |
| 1223 | static_cast<uint8_t*>(in.y), |
| 1224 | in.yStride, |
| 1225 | static_cast<uint8_t*>(in.cb), |
| 1226 | in.cStride, |
| 1227 | static_cast<uint8_t*>(in.cr), |
| 1228 | in.cStride, |
| 1229 | static_cast<uint8_t*>(out.y), |
| 1230 | out.yStride, |
| 1231 | static_cast<uint8_t*>(out.cb), |
| 1232 | out.cStride, |
| 1233 | sz.width, |
| 1234 | sz.height); |
| 1235 | if (ret != 0) { |
| 1236 | ALOGE("%s: convert to NV12 buffer failed! ret %d", |
| 1237 | __FUNCTION__, ret); |
| 1238 | return ret; |
| 1239 | } |
| 1240 | break; |
| 1241 | case V4L2_PIX_FMT_YVU420: // YV12 |
| 1242 | case V4L2_PIX_FMT_YUV420: // YU12 |
| 1243 | // TODO: maybe we can speed up here by somehow save this copy? |
| 1244 | ret = libyuv::I420Copy( |
| 1245 | static_cast<uint8_t*>(in.y), |
| 1246 | in.yStride, |
| 1247 | static_cast<uint8_t*>(in.cb), |
| 1248 | in.cStride, |
| 1249 | static_cast<uint8_t*>(in.cr), |
| 1250 | in.cStride, |
| 1251 | static_cast<uint8_t*>(out.y), |
| 1252 | out.yStride, |
| 1253 | static_cast<uint8_t*>(out.cb), |
| 1254 | out.cStride, |
| 1255 | static_cast<uint8_t*>(out.cr), |
| 1256 | out.cStride, |
| 1257 | sz.width, |
| 1258 | sz.height); |
| 1259 | if (ret != 0) { |
| 1260 | ALOGE("%s: copy to YV12 or YU12 buffer failed! ret %d", |
| 1261 | __FUNCTION__, ret); |
| 1262 | return ret; |
| 1263 | } |
| 1264 | break; |
| 1265 | case FLEX_YUV_GENERIC: |
| 1266 | // TODO: b/72261744 write to arbitrary flexible YUV layout. Slow. |
| 1267 | ALOGE("%s: unsupported flexible yuv layout" |
| 1268 | " y %p cb %p cr %p y_str %d c_str %d c_step %d", |
| 1269 | __FUNCTION__, out.y, out.cb, out.cr, |
| 1270 | out.yStride, out.cStride, out.chromaStep); |
| 1271 | return -1; |
| 1272 | default: |
| 1273 | ALOGE("%s: unknown YUV format 0x%x!", __FUNCTION__, format); |
| 1274 | return -1; |
| 1275 | } |
| 1276 | return 0; |
| 1277 | } |
| 1278 | |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1279 | int ExternalCameraDeviceSession::OutputThread::encodeJpegYU12( |
| 1280 | const Size & inSz, const YCbCrLayout& inLayout, |
| 1281 | int jpegQuality, const void *app1Buffer, size_t app1Size, |
| 1282 | void *out, const size_t maxOutSize, size_t &actualCodeSize) |
| 1283 | { |
| 1284 | /* libjpeg is a C library so we use C-style "inheritance" by |
| 1285 | * putting libjpeg's jpeg_destination_mgr first in our custom |
| 1286 | * struct. This allows us to cast jpeg_destination_mgr* to |
| 1287 | * CustomJpegDestMgr* when we get it passed to us in a callback */ |
| 1288 | struct CustomJpegDestMgr { |
| 1289 | struct jpeg_destination_mgr mgr; |
| 1290 | JOCTET *mBuffer; |
| 1291 | size_t mBufferSize; |
| 1292 | size_t mEncodedSize; |
| 1293 | bool mSuccess; |
| 1294 | } dmgr; |
| 1295 | |
| 1296 | jpeg_compress_struct cinfo = {}; |
| 1297 | jpeg_error_mgr jerr; |
| 1298 | |
| 1299 | /* Initialize error handling with standard callbacks, but |
| 1300 | * then override output_message (to print to ALOG) and |
| 1301 | * error_exit to set a flag and print a message instead |
| 1302 | * of killing the whole process */ |
| 1303 | cinfo.err = jpeg_std_error(&jerr); |
| 1304 | |
| 1305 | cinfo.err->output_message = [](j_common_ptr cinfo) { |
| 1306 | char buffer[JMSG_LENGTH_MAX]; |
| 1307 | |
| 1308 | /* Create the message */ |
| 1309 | (*cinfo->err->format_message)(cinfo, buffer); |
| 1310 | ALOGE("libjpeg error: %s", buffer); |
| 1311 | }; |
| 1312 | cinfo.err->error_exit = [](j_common_ptr cinfo) { |
| 1313 | (*cinfo->err->output_message)(cinfo); |
| 1314 | if(cinfo->client_data) { |
| 1315 | auto & dmgr = |
| 1316 | *reinterpret_cast<CustomJpegDestMgr*>(cinfo->client_data); |
| 1317 | dmgr.mSuccess = false; |
| 1318 | } |
| 1319 | }; |
| 1320 | /* Now that we initialized some callbacks, let's create our compressor */ |
| 1321 | jpeg_create_compress(&cinfo); |
| 1322 | |
| 1323 | /* Initialize our destination manager */ |
| 1324 | dmgr.mBuffer = static_cast<JOCTET*>(out); |
| 1325 | dmgr.mBufferSize = maxOutSize; |
| 1326 | dmgr.mEncodedSize = 0; |
| 1327 | dmgr.mSuccess = true; |
| 1328 | cinfo.client_data = static_cast<void*>(&dmgr); |
| 1329 | |
| 1330 | /* These lambdas become C-style function pointers and as per C++11 spec |
| 1331 | * may not capture anything */ |
| 1332 | dmgr.mgr.init_destination = [](j_compress_ptr cinfo) { |
| 1333 | auto & dmgr = reinterpret_cast<CustomJpegDestMgr&>(*cinfo->dest); |
| 1334 | dmgr.mgr.next_output_byte = dmgr.mBuffer; |
| 1335 | dmgr.mgr.free_in_buffer = dmgr.mBufferSize; |
| 1336 | ALOGV("%s:%d jpeg start: %p [%zu]", |
| 1337 | __FUNCTION__, __LINE__, dmgr.mBuffer, dmgr.mBufferSize); |
| 1338 | }; |
| 1339 | |
| 1340 | dmgr.mgr.empty_output_buffer = [](j_compress_ptr cinfo __unused) { |
| 1341 | ALOGV("%s:%d Out of buffer", __FUNCTION__, __LINE__); |
| 1342 | return 0; |
| 1343 | }; |
| 1344 | |
| 1345 | dmgr.mgr.term_destination = [](j_compress_ptr cinfo) { |
| 1346 | auto & dmgr = reinterpret_cast<CustomJpegDestMgr&>(*cinfo->dest); |
| 1347 | dmgr.mEncodedSize = dmgr.mBufferSize - dmgr.mgr.free_in_buffer; |
| 1348 | ALOGV("%s:%d Done with jpeg: %zu", __FUNCTION__, __LINE__, dmgr.mEncodedSize); |
| 1349 | }; |
| 1350 | cinfo.dest = reinterpret_cast<struct jpeg_destination_mgr*>(&dmgr); |
| 1351 | |
| 1352 | /* We are going to be using JPEG in raw data mode, so we are passing |
| 1353 | * straight subsampled planar YCbCr and it will not touch our pixel |
| 1354 | * data or do any scaling or anything */ |
| 1355 | cinfo.image_width = inSz.width; |
| 1356 | cinfo.image_height = inSz.height; |
| 1357 | cinfo.input_components = 3; |
| 1358 | cinfo.in_color_space = JCS_YCbCr; |
| 1359 | |
| 1360 | /* Initialize defaults and then override what we want */ |
| 1361 | jpeg_set_defaults(&cinfo); |
| 1362 | |
| 1363 | jpeg_set_quality(&cinfo, jpegQuality, 1); |
| 1364 | jpeg_set_colorspace(&cinfo, JCS_YCbCr); |
| 1365 | cinfo.raw_data_in = 1; |
| 1366 | cinfo.dct_method = JDCT_IFAST; |
| 1367 | |
| 1368 | /* Configure sampling factors. The sampling factor is JPEG subsampling 420 |
| 1369 | * because the source format is YUV420. Note that libjpeg sampling factors |
| 1370 | * are... a little weird. Sampling of Y=2,U=1,V=1 means there is 1 U and |
| 1371 | * 1 V value for each 2 Y values */ |
| 1372 | cinfo.comp_info[0].h_samp_factor = 2; |
| 1373 | cinfo.comp_info[0].v_samp_factor = 2; |
| 1374 | cinfo.comp_info[1].h_samp_factor = 1; |
| 1375 | cinfo.comp_info[1].v_samp_factor = 1; |
| 1376 | cinfo.comp_info[2].h_samp_factor = 1; |
| 1377 | cinfo.comp_info[2].v_samp_factor = 1; |
| 1378 | |
| 1379 | /* Let's not hardcode YUV420 in 6 places... 5 was enough */ |
| 1380 | int maxVSampFactor = std::max( { |
| 1381 | cinfo.comp_info[0].v_samp_factor, |
| 1382 | cinfo.comp_info[1].v_samp_factor, |
| 1383 | cinfo.comp_info[2].v_samp_factor |
| 1384 | }); |
| 1385 | int cVSubSampling = cinfo.comp_info[0].v_samp_factor / |
| 1386 | cinfo.comp_info[1].v_samp_factor; |
| 1387 | |
| 1388 | /* Start the compressor */ |
| 1389 | jpeg_start_compress(&cinfo, TRUE); |
| 1390 | |
| 1391 | /* Compute our macroblock height, so we can pad our input to be vertically |
| 1392 | * macroblock aligned. |
| 1393 | * TODO: Does it need to be horizontally MCU aligned too? */ |
| 1394 | |
| 1395 | size_t mcuV = DCTSIZE*maxVSampFactor; |
| 1396 | size_t paddedHeight = mcuV * ((inSz.height + mcuV - 1) / mcuV); |
| 1397 | |
| 1398 | /* libjpeg uses arrays of row pointers, which makes it really easy to pad |
| 1399 | * data vertically (unfortunately doesn't help horizontally) */ |
| 1400 | std::vector<JSAMPROW> yLines (paddedHeight); |
| 1401 | std::vector<JSAMPROW> cbLines(paddedHeight/cVSubSampling); |
| 1402 | std::vector<JSAMPROW> crLines(paddedHeight/cVSubSampling); |
| 1403 | |
| 1404 | uint8_t *py = static_cast<uint8_t*>(inLayout.y); |
| 1405 | uint8_t *pcr = static_cast<uint8_t*>(inLayout.cr); |
| 1406 | uint8_t *pcb = static_cast<uint8_t*>(inLayout.cb); |
| 1407 | |
| 1408 | for(uint32_t i = 0; i < paddedHeight; i++) |
| 1409 | { |
| 1410 | /* Once we are in the padding territory we still point to the last line |
| 1411 | * effectively replicating it several times ~ CLAMP_TO_EDGE */ |
| 1412 | int li = std::min(i, inSz.height - 1); |
| 1413 | yLines[i] = static_cast<JSAMPROW>(py + li * inLayout.yStride); |
| 1414 | if(i < paddedHeight / cVSubSampling) |
| 1415 | { |
| 1416 | crLines[i] = static_cast<JSAMPROW>(pcr + li * inLayout.cStride); |
| 1417 | cbLines[i] = static_cast<JSAMPROW>(pcb + li * inLayout.cStride); |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | /* If APP1 data was passed in, use it */ |
| 1422 | if(app1Buffer && app1Size) |
| 1423 | { |
| 1424 | jpeg_write_marker(&cinfo, JPEG_APP0 + 1, |
| 1425 | static_cast<const JOCTET*>(app1Buffer), app1Size); |
| 1426 | } |
| 1427 | |
| 1428 | /* While we still have padded height left to go, keep giving it one |
| 1429 | * macroblock at a time. */ |
| 1430 | while (cinfo.next_scanline < cinfo.image_height) { |
| 1431 | const uint32_t batchSize = DCTSIZE * maxVSampFactor; |
| 1432 | const uint32_t nl = cinfo.next_scanline; |
| 1433 | JSAMPARRAY planes[3]{ &yLines[nl], |
| 1434 | &cbLines[nl/cVSubSampling], |
| 1435 | &crLines[nl/cVSubSampling] }; |
| 1436 | |
| 1437 | uint32_t done = jpeg_write_raw_data(&cinfo, planes, batchSize); |
| 1438 | |
| 1439 | if (done != batchSize) { |
| 1440 | ALOGE("%s: compressed %u lines, expected %u (total %u/%u)", |
| 1441 | __FUNCTION__, done, batchSize, cinfo.next_scanline, |
| 1442 | cinfo.image_height); |
| 1443 | return -1; |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | /* This will flush everything */ |
| 1448 | jpeg_finish_compress(&cinfo); |
| 1449 | |
| 1450 | /* Grab the actual code size and set it */ |
| 1451 | actualCodeSize = dmgr.mEncodedSize; |
| 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | /* |
| 1457 | * TODO: There needs to be a mechanism to discover allocated buffer size |
| 1458 | * in the HAL. |
| 1459 | * |
| 1460 | * This is very fragile because it is duplicated computation from: |
| 1461 | * frameworks/av/services/camera/libcameraservice/device3/Camera3Device.cpp |
| 1462 | * |
| 1463 | */ |
| 1464 | |
| 1465 | /* This assumes mSupportedFormats have all been declared as supporting |
| 1466 | * HAL_PIXEL_FORMAT_BLOB to the framework */ |
| 1467 | Size ExternalCameraDeviceSession::getMaxJpegResolution() const { |
| 1468 | Size ret { 0, 0 }; |
| 1469 | for(auto & fmt : mSupportedFormats) { |
| 1470 | if(fmt.width * fmt.height > ret.width * ret.height) { |
| 1471 | ret = Size { fmt.width, fmt.height }; |
| 1472 | } |
| 1473 | } |
| 1474 | return ret; |
| 1475 | } |
| 1476 | |
| 1477 | Size ExternalCameraDeviceSession::getMaxThumbResolution() const { |
| 1478 | Size thumbSize { 0, 0 }; |
| 1479 | camera_metadata_ro_entry entry = |
| 1480 | mCameraCharacteristics.find(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES); |
| 1481 | for(uint32_t i = 0; i < entry.count; i += 2) { |
| 1482 | Size sz { static_cast<uint32_t>(entry.data.i32[i]), |
| 1483 | static_cast<uint32_t>(entry.data.i32[i+1]) }; |
| 1484 | if(sz.width * sz.height > thumbSize.width * thumbSize.height) { |
| 1485 | thumbSize = sz; |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | if (thumbSize.width * thumbSize.height == 0) { |
| 1490 | ALOGW("%s: non-zero thumbnail size not available", __FUNCTION__); |
| 1491 | } |
| 1492 | |
| 1493 | return thumbSize; |
| 1494 | } |
| 1495 | |
| 1496 | |
| 1497 | ssize_t ExternalCameraDeviceSession::getJpegBufferSize( |
| 1498 | uint32_t width, uint32_t height) const { |
| 1499 | // Constant from camera3.h |
| 1500 | const ssize_t kMinJpegBufferSize = 256 * 1024 + sizeof(CameraBlob); |
| 1501 | // Get max jpeg size (area-wise). |
| 1502 | if (mMaxJpegResolution.width == 0) { |
| 1503 | ALOGE("%s: Do not have a single supported JPEG stream", |
| 1504 | __FUNCTION__); |
| 1505 | return BAD_VALUE; |
| 1506 | } |
| 1507 | |
| 1508 | // Get max jpeg buffer size |
| 1509 | ssize_t maxJpegBufferSize = 0; |
| 1510 | camera_metadata_ro_entry jpegBufMaxSize = |
| 1511 | mCameraCharacteristics.find(ANDROID_JPEG_MAX_SIZE); |
| 1512 | if (jpegBufMaxSize.count == 0) { |
| 1513 | ALOGE("%s: Can't find maximum JPEG size in static metadata!", |
| 1514 | __FUNCTION__); |
| 1515 | return BAD_VALUE; |
| 1516 | } |
| 1517 | maxJpegBufferSize = jpegBufMaxSize.data.i32[0]; |
| 1518 | |
| 1519 | if (maxJpegBufferSize <= kMinJpegBufferSize) { |
| 1520 | ALOGE("%s: ANDROID_JPEG_MAX_SIZE (%zd) <= kMinJpegBufferSize (%zd)", |
| 1521 | __FUNCTION__, maxJpegBufferSize, kMinJpegBufferSize); |
| 1522 | return BAD_VALUE; |
| 1523 | } |
| 1524 | |
| 1525 | // Calculate final jpeg buffer size for the given resolution. |
| 1526 | float scaleFactor = ((float) (width * height)) / |
| 1527 | (mMaxJpegResolution.width * mMaxJpegResolution.height); |
| 1528 | ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) + |
| 1529 | kMinJpegBufferSize; |
| 1530 | if (jpegBufferSize > maxJpegBufferSize) { |
| 1531 | jpegBufferSize = maxJpegBufferSize; |
| 1532 | } |
| 1533 | |
| 1534 | return jpegBufferSize; |
| 1535 | } |
| 1536 | |
| 1537 | int ExternalCameraDeviceSession::OutputThread::createJpegLocked( |
| 1538 | HalStreamBuffer &halBuf, |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1539 | const std::shared_ptr<HalRequest>& req) |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1540 | { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 1541 | ATRACE_CALL(); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1542 | int ret; |
| 1543 | auto lfail = [&](auto... args) { |
| 1544 | ALOGE(args...); |
| 1545 | |
| 1546 | return 1; |
| 1547 | }; |
| 1548 | auto parent = mParent.promote(); |
| 1549 | if (parent == nullptr) { |
| 1550 | ALOGE("%s: session has been disconnected!", __FUNCTION__); |
| 1551 | return 1; |
| 1552 | } |
| 1553 | |
| 1554 | ALOGV("%s: HAL buffer sid: %d bid: %" PRIu64 " w: %u h: %u", |
| 1555 | __FUNCTION__, halBuf.streamId, static_cast<uint64_t>(halBuf.bufferId), |
| 1556 | halBuf.width, halBuf.height); |
| 1557 | ALOGV("%s: HAL buffer fmt: %x usage: %" PRIx64 " ptr: %p", |
| 1558 | __FUNCTION__, halBuf.format, static_cast<uint64_t>(halBuf.usage), |
| 1559 | halBuf.bufPtr); |
| 1560 | ALOGV("%s: YV12 buffer %d x %d", |
| 1561 | __FUNCTION__, |
| 1562 | mYu12Frame->mWidth, mYu12Frame->mHeight); |
| 1563 | |
| 1564 | int jpegQuality, thumbQuality; |
| 1565 | Size thumbSize; |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 1566 | bool outputThumbnail = true; |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1567 | |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1568 | if (req->setting.exists(ANDROID_JPEG_QUALITY)) { |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1569 | camera_metadata_entry entry = |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1570 | req->setting.find(ANDROID_JPEG_QUALITY); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1571 | jpegQuality = entry.data.u8[0]; |
| 1572 | } else { |
| 1573 | return lfail("%s: ANDROID_JPEG_QUALITY not set",__FUNCTION__); |
| 1574 | } |
| 1575 | |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1576 | if (req->setting.exists(ANDROID_JPEG_THUMBNAIL_QUALITY)) { |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1577 | camera_metadata_entry entry = |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1578 | req->setting.find(ANDROID_JPEG_THUMBNAIL_QUALITY); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1579 | thumbQuality = entry.data.u8[0]; |
| 1580 | } else { |
| 1581 | return lfail( |
| 1582 | "%s: ANDROID_JPEG_THUMBNAIL_QUALITY not set", |
| 1583 | __FUNCTION__); |
| 1584 | } |
| 1585 | |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1586 | if (req->setting.exists(ANDROID_JPEG_THUMBNAIL_SIZE)) { |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1587 | camera_metadata_entry entry = |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1588 | req->setting.find(ANDROID_JPEG_THUMBNAIL_SIZE); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1589 | thumbSize = Size { static_cast<uint32_t>(entry.data.i32[0]), |
| 1590 | static_cast<uint32_t>(entry.data.i32[1]) |
| 1591 | }; |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 1592 | if (thumbSize.width == 0 && thumbSize.height == 0) { |
| 1593 | outputThumbnail = false; |
| 1594 | } |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1595 | } else { |
| 1596 | return lfail( |
| 1597 | "%s: ANDROID_JPEG_THUMBNAIL_SIZE not set", __FUNCTION__); |
| 1598 | } |
| 1599 | |
| 1600 | /* Cropped and scaled YU12 buffer for main and thumbnail */ |
| 1601 | YCbCrLayout yu12Main; |
| 1602 | Size jpegSize { halBuf.width, halBuf.height }; |
| 1603 | |
| 1604 | /* Compute temporary buffer sizes accounting for the following: |
| 1605 | * thumbnail can't exceed APP1 size of 64K |
| 1606 | * main image needs to hold APP1, headers, and at most a poorly |
| 1607 | * compressed image */ |
| 1608 | const ssize_t maxThumbCodeSize = 64 * 1024; |
Yin-Chia Yeh | 4c64118 | 2018-08-06 15:01:20 -0700 | [diff] [blame] | 1609 | const ssize_t maxJpegCodeSize = mBlobBufferSize == 0 ? |
| 1610 | parent->getJpegBufferSize(jpegSize.width, jpegSize.height) : |
| 1611 | mBlobBufferSize; |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1612 | |
| 1613 | /* Check that getJpegBufferSize did not return an error */ |
| 1614 | if (maxJpegCodeSize < 0) { |
| 1615 | return lfail( |
| 1616 | "%s: getJpegBufferSize returned %zd",__FUNCTION__,maxJpegCodeSize); |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | /* Hold actual thumbnail and main image code sizes */ |
| 1621 | size_t thumbCodeSize = 0, jpegCodeSize = 0; |
| 1622 | /* Temporary thumbnail code buffer */ |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 1623 | std::vector<uint8_t> thumbCode(outputThumbnail ? maxThumbCodeSize : 0); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1624 | |
| 1625 | YCbCrLayout yu12Thumb; |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 1626 | if (outputThumbnail) { |
| 1627 | ret = cropAndScaleThumbLocked(mYu12Frame, thumbSize, &yu12Thumb); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1628 | |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 1629 | if (ret != 0) { |
| 1630 | return lfail( |
| 1631 | "%s: crop and scale thumbnail failed!", __FUNCTION__); |
| 1632 | } |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | /* Scale and crop main jpeg */ |
| 1636 | ret = cropAndScaleLocked(mYu12Frame, jpegSize, &yu12Main); |
| 1637 | |
| 1638 | if (ret != 0) { |
| 1639 | return lfail("%s: crop and scale main failed!", __FUNCTION__); |
| 1640 | } |
| 1641 | |
| 1642 | /* Encode the thumbnail image */ |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 1643 | if (outputThumbnail) { |
| 1644 | ret = encodeJpegYU12(thumbSize, yu12Thumb, |
| 1645 | thumbQuality, 0, 0, |
| 1646 | &thumbCode[0], maxThumbCodeSize, thumbCodeSize); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1647 | |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 1648 | if (ret != 0) { |
| 1649 | return lfail("%s: thumbnail encodeJpegYU12 failed with %d",__FUNCTION__, ret); |
| 1650 | } |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* Combine camera characteristics with request settings to form EXIF |
| 1654 | * metadata */ |
| 1655 | common::V1_0::helper::CameraMetadata meta(parent->mCameraCharacteristics); |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1656 | meta.append(req->setting); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1657 | |
| 1658 | /* Generate EXIF object */ |
| 1659 | std::unique_ptr<ExifUtils> utils(ExifUtils::create()); |
| 1660 | /* Make sure it's initialized */ |
| 1661 | utils->initialize(); |
| 1662 | |
| 1663 | utils->setFromMetadata(meta, jpegSize.width, jpegSize.height); |
Yin-Chia Yeh | 2d61bfd | 2018-03-14 13:50:23 -0700 | [diff] [blame] | 1664 | utils->setMake(mExifMake); |
| 1665 | utils->setModel(mExifModel); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1666 | |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 1667 | ret = utils->generateApp1(outputThumbnail ? &thumbCode[0] : 0, thumbCodeSize); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1668 | |
| 1669 | if (!ret) { |
| 1670 | return lfail("%s: generating APP1 failed", __FUNCTION__); |
| 1671 | } |
| 1672 | |
| 1673 | /* Get internal buffer */ |
| 1674 | size_t exifDataSize = utils->getApp1Length(); |
| 1675 | const uint8_t* exifData = utils->getApp1Buffer(); |
| 1676 | |
| 1677 | /* Lock the HAL jpeg code buffer */ |
| 1678 | void *bufPtr = sHandleImporter.lock( |
| 1679 | *(halBuf.bufPtr), halBuf.usage, maxJpegCodeSize); |
| 1680 | |
| 1681 | if (!bufPtr) { |
| 1682 | return lfail("%s: could not lock %zu bytes", __FUNCTION__, maxJpegCodeSize); |
| 1683 | } |
| 1684 | |
| 1685 | /* Encode the main jpeg image */ |
| 1686 | ret = encodeJpegYU12(jpegSize, yu12Main, |
| 1687 | jpegQuality, exifData, exifDataSize, |
| 1688 | bufPtr, maxJpegCodeSize, jpegCodeSize); |
| 1689 | |
| 1690 | /* TODO: Not sure this belongs here, maybe better to pass jpegCodeSize out |
| 1691 | * and do this when returning buffer to parent */ |
| 1692 | CameraBlob blob { CameraBlobId::JPEG, static_cast<uint32_t>(jpegCodeSize) }; |
| 1693 | void *blobDst = |
| 1694 | reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(bufPtr) + |
| 1695 | maxJpegCodeSize - |
| 1696 | sizeof(CameraBlob)); |
| 1697 | memcpy(blobDst, &blob, sizeof(CameraBlob)); |
| 1698 | |
| 1699 | /* Unlock the HAL jpeg code buffer */ |
| 1700 | int relFence = sHandleImporter.unlock(*(halBuf.bufPtr)); |
| 1701 | if (relFence > 0) { |
| 1702 | halBuf.acquireFence = relFence; |
| 1703 | } |
| 1704 | |
| 1705 | /* Check if our JPEG actually succeeded */ |
| 1706 | if (ret != 0) { |
| 1707 | return lfail( |
| 1708 | "%s: encodeJpegYU12 failed with %d",__FUNCTION__, ret); |
| 1709 | } |
| 1710 | |
| 1711 | ALOGV("%s: encoded JPEG (ret:%d) with Q:%d max size: %zu", |
| 1712 | __FUNCTION__, ret, jpegQuality, maxJpegCodeSize); |
| 1713 | |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1717 | bool ExternalCameraDeviceSession::OutputThread::threadLoop() { |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1718 | std::shared_ptr<HalRequest> req; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1719 | auto parent = mParent.promote(); |
| 1720 | if (parent == nullptr) { |
| 1721 | ALOGE("%s: session has been disconnected!", __FUNCTION__); |
| 1722 | return false; |
| 1723 | } |
| 1724 | |
| 1725 | // TODO: maybe we need to setup a sensor thread to dq/enq v4l frames |
| 1726 | // regularly to prevent v4l buffer queue filled with stale buffers |
| 1727 | // when app doesn't program a preveiw request |
| 1728 | waitForNextRequest(&req); |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1729 | if (req == nullptr) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1730 | // No new request, wait again |
| 1731 | return true; |
| 1732 | } |
| 1733 | |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1734 | auto onDeviceError = [&](auto... args) { |
| 1735 | ALOGE(args...); |
| 1736 | parent->notifyError( |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1737 | req->frameNumber, /*stream*/-1, ErrorCode::ERROR_DEVICE); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1738 | signalRequestDone(); |
| 1739 | return false; |
| 1740 | }; |
| 1741 | |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1742 | if (req->frameIn->mFourcc != V4L2_PIX_FMT_MJPEG) { |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1743 | return onDeviceError("%s: do not support V4L2 format %c%c%c%c", __FUNCTION__, |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1744 | req->frameIn->mFourcc & 0xFF, |
| 1745 | (req->frameIn->mFourcc >> 8) & 0xFF, |
| 1746 | (req->frameIn->mFourcc >> 16) & 0xFF, |
| 1747 | (req->frameIn->mFourcc >> 24) & 0xFF); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1748 | } |
| 1749 | |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1750 | std::unique_lock<std::mutex> lk(mBufferLock); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1751 | // Convert input V4L2 frame to YU12 of the same size |
| 1752 | // TODO: see if we can save some computation by converting to YV12 here |
| 1753 | uint8_t* inData; |
| 1754 | size_t inDataSize; |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1755 | req->frameIn->map(&inData, &inDataSize); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1756 | // TODO: in some special case maybe we can decode jpg directly to gralloc output? |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 1757 | ATRACE_BEGIN("MJPGtoI420"); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1758 | int res = libyuv::MJPGToI420( |
| 1759 | inData, inDataSize, |
| 1760 | static_cast<uint8_t*>(mYu12FrameLayout.y), |
| 1761 | mYu12FrameLayout.yStride, |
| 1762 | static_cast<uint8_t*>(mYu12FrameLayout.cb), |
| 1763 | mYu12FrameLayout.cStride, |
| 1764 | static_cast<uint8_t*>(mYu12FrameLayout.cr), |
| 1765 | mYu12FrameLayout.cStride, |
| 1766 | mYu12Frame->mWidth, mYu12Frame->mHeight, |
| 1767 | mYu12Frame->mWidth, mYu12Frame->mHeight); |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 1768 | ATRACE_END(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1769 | |
| 1770 | if (res != 0) { |
| 1771 | // For some webcam, the first few V4L2 frames might be malformed... |
| 1772 | ALOGE("%s: Convert V4L2 frame to YU12 failed! res %d", __FUNCTION__, res); |
| 1773 | lk.unlock(); |
| 1774 | Status st = parent->processCaptureRequestError(req); |
| 1775 | if (st != Status::OK) { |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1776 | return onDeviceError("%s: failed to process capture request error!", __FUNCTION__); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1777 | } |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1778 | signalRequestDone(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1779 | return true; |
| 1780 | } |
| 1781 | |
| 1782 | ALOGV("%s processing new request", __FUNCTION__); |
| 1783 | const int kSyncWaitTimeoutMs = 500; |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1784 | for (auto& halBuf : req->buffers) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1785 | if (halBuf.acquireFence != -1) { |
| 1786 | int ret = sync_wait(halBuf.acquireFence, kSyncWaitTimeoutMs); |
| 1787 | if (ret) { |
| 1788 | halBuf.fenceTimeout = true; |
| 1789 | } else { |
| 1790 | ::close(halBuf.acquireFence); |
Yin-Chia Yeh | 1e08966 | 2018-02-02 15:57:58 -0800 | [diff] [blame] | 1791 | halBuf.acquireFence = -1; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | if (halBuf.fenceTimeout) { |
| 1796 | continue; |
| 1797 | } |
| 1798 | |
| 1799 | // Gralloc lockYCbCr the buffer |
| 1800 | switch (halBuf.format) { |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1801 | case PixelFormat::BLOB: { |
| 1802 | int ret = createJpegLocked(halBuf, req); |
| 1803 | |
| 1804 | if(ret != 0) { |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1805 | lk.unlock(); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1806 | return onDeviceError("%s: createJpegLocked failed with %d", |
| 1807 | __FUNCTION__, ret); |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1808 | } |
| 1809 | } break; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1810 | case PixelFormat::YCBCR_420_888: |
| 1811 | case PixelFormat::YV12: { |
| 1812 | IMapper::Rect outRect {0, 0, |
| 1813 | static_cast<int32_t>(halBuf.width), |
| 1814 | static_cast<int32_t>(halBuf.height)}; |
| 1815 | YCbCrLayout outLayout = sHandleImporter.lockYCbCr( |
| 1816 | *(halBuf.bufPtr), halBuf.usage, outRect); |
| 1817 | ALOGV("%s: outLayout y %p cb %p cr %p y_str %d c_str %d c_step %d", |
| 1818 | __FUNCTION__, outLayout.y, outLayout.cb, outLayout.cr, |
| 1819 | outLayout.yStride, outLayout.cStride, outLayout.chromaStep); |
| 1820 | |
| 1821 | // Convert to output buffer size/format |
| 1822 | uint32_t outputFourcc = getFourCcFromLayout(outLayout); |
| 1823 | ALOGV("%s: converting to format %c%c%c%c", __FUNCTION__, |
| 1824 | outputFourcc & 0xFF, |
| 1825 | (outputFourcc >> 8) & 0xFF, |
| 1826 | (outputFourcc >> 16) & 0xFF, |
| 1827 | (outputFourcc >> 24) & 0xFF); |
| 1828 | |
| 1829 | YCbCrLayout cropAndScaled; |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 1830 | ATRACE_BEGIN("cropAndScaleLocked"); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1831 | int ret = cropAndScaleLocked( |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1832 | mYu12Frame, |
| 1833 | Size { halBuf.width, halBuf.height }, |
| 1834 | &cropAndScaled); |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 1835 | ATRACE_END(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1836 | if (ret != 0) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1837 | lk.unlock(); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1838 | return onDeviceError("%s: crop and scale failed!", __FUNCTION__); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | Size sz {halBuf.width, halBuf.height}; |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 1842 | ATRACE_BEGIN("formatConvertLocked"); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1843 | ret = formatConvertLocked(cropAndScaled, outLayout, sz, outputFourcc); |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 1844 | ATRACE_END(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1845 | if (ret != 0) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1846 | lk.unlock(); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1847 | return onDeviceError("%s: format coversion failed!", __FUNCTION__); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1848 | } |
| 1849 | int relFence = sHandleImporter.unlock(*(halBuf.bufPtr)); |
| 1850 | if (relFence > 0) { |
| 1851 | halBuf.acquireFence = relFence; |
| 1852 | } |
| 1853 | } break; |
| 1854 | default: |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1855 | lk.unlock(); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1856 | return onDeviceError("%s: unknown output format %x", __FUNCTION__, halBuf.format); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1857 | } |
| 1858 | } // for each buffer |
| 1859 | mScaledYu12Frames.clear(); |
| 1860 | |
| 1861 | // Don't hold the lock while calling back to parent |
| 1862 | lk.unlock(); |
| 1863 | Status st = parent->processCaptureResult(req); |
| 1864 | if (st != Status::OK) { |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1865 | return onDeviceError("%s: failed to process capture result!", __FUNCTION__); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1866 | } |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1867 | signalRequestDone(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1868 | return true; |
| 1869 | } |
| 1870 | |
| 1871 | Status ExternalCameraDeviceSession::OutputThread::allocateIntermediateBuffers( |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1872 | const Size& v4lSize, const Size& thumbSize, |
Yin-Chia Yeh | 4c64118 | 2018-08-06 15:01:20 -0700 | [diff] [blame] | 1873 | const hidl_vec<Stream>& streams, |
| 1874 | uint32_t blobBufferSize) { |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1875 | std::lock_guard<std::mutex> lk(mBufferLock); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1876 | if (mScaledYu12Frames.size() != 0) { |
| 1877 | ALOGE("%s: intermediate buffer pool has %zu inflight buffers! (expect 0)", |
| 1878 | __FUNCTION__, mScaledYu12Frames.size()); |
| 1879 | return Status::INTERNAL_ERROR; |
| 1880 | } |
| 1881 | |
| 1882 | // Allocating intermediate YU12 frame |
| 1883 | if (mYu12Frame == nullptr || mYu12Frame->mWidth != v4lSize.width || |
| 1884 | mYu12Frame->mHeight != v4lSize.height) { |
| 1885 | mYu12Frame.clear(); |
| 1886 | mYu12Frame = new AllocatedFrame(v4lSize.width, v4lSize.height); |
| 1887 | int ret = mYu12Frame->allocate(&mYu12FrameLayout); |
| 1888 | if (ret != 0) { |
| 1889 | ALOGE("%s: allocating YU12 frame failed!", __FUNCTION__); |
| 1890 | return Status::INTERNAL_ERROR; |
| 1891 | } |
| 1892 | } |
| 1893 | |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 1894 | // Allocating intermediate YU12 thumbnail frame |
| 1895 | if (mYu12ThumbFrame == nullptr || |
| 1896 | mYu12ThumbFrame->mWidth != thumbSize.width || |
| 1897 | mYu12ThumbFrame->mHeight != thumbSize.height) { |
| 1898 | mYu12ThumbFrame.clear(); |
| 1899 | mYu12ThumbFrame = new AllocatedFrame(thumbSize.width, thumbSize.height); |
| 1900 | int ret = mYu12ThumbFrame->allocate(&mYu12ThumbFrameLayout); |
| 1901 | if (ret != 0) { |
| 1902 | ALOGE("%s: allocating YU12 thumb frame failed!", __FUNCTION__); |
| 1903 | return Status::INTERNAL_ERROR; |
| 1904 | } |
| 1905 | } |
| 1906 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1907 | // Allocating scaled buffers |
| 1908 | for (const auto& stream : streams) { |
| 1909 | Size sz = {stream.width, stream.height}; |
| 1910 | if (sz == v4lSize) { |
| 1911 | continue; // Don't need an intermediate buffer same size as v4lBuffer |
| 1912 | } |
| 1913 | if (mIntermediateBuffers.count(sz) == 0) { |
| 1914 | // Create new intermediate buffer |
| 1915 | sp<AllocatedFrame> buf = new AllocatedFrame(stream.width, stream.height); |
| 1916 | int ret = buf->allocate(); |
| 1917 | if (ret != 0) { |
| 1918 | ALOGE("%s: allocating intermediate YU12 frame %dx%d failed!", |
| 1919 | __FUNCTION__, stream.width, stream.height); |
| 1920 | return Status::INTERNAL_ERROR; |
| 1921 | } |
| 1922 | mIntermediateBuffers[sz] = buf; |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | // Remove unconfigured buffers |
| 1927 | auto it = mIntermediateBuffers.begin(); |
| 1928 | while (it != mIntermediateBuffers.end()) { |
| 1929 | bool configured = false; |
| 1930 | auto sz = it->first; |
| 1931 | for (const auto& stream : streams) { |
| 1932 | if (stream.width == sz.width && stream.height == sz.height) { |
| 1933 | configured = true; |
| 1934 | break; |
| 1935 | } |
| 1936 | } |
| 1937 | if (configured) { |
| 1938 | it++; |
| 1939 | } else { |
| 1940 | it = mIntermediateBuffers.erase(it); |
| 1941 | } |
| 1942 | } |
Yin-Chia Yeh | 4c64118 | 2018-08-06 15:01:20 -0700 | [diff] [blame] | 1943 | |
| 1944 | mBlobBufferSize = blobBufferSize; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1945 | return Status::OK; |
| 1946 | } |
| 1947 | |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1948 | Status ExternalCameraDeviceSession::OutputThread::submitRequest( |
| 1949 | const std::shared_ptr<HalRequest>& req) { |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1950 | std::unique_lock<std::mutex> lk(mRequestListLock); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1951 | mRequestList.push_back(req); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1952 | lk.unlock(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1953 | mRequestCond.notify_one(); |
| 1954 | return Status::OK; |
| 1955 | } |
| 1956 | |
| 1957 | void ExternalCameraDeviceSession::OutputThread::flush() { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 1958 | ATRACE_CALL(); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1959 | auto parent = mParent.promote(); |
| 1960 | if (parent == nullptr) { |
| 1961 | ALOGE("%s: session has been disconnected!", __FUNCTION__); |
| 1962 | return; |
| 1963 | } |
| 1964 | |
| 1965 | std::unique_lock<std::mutex> lk(mRequestListLock); |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1966 | std::list<std::shared_ptr<HalRequest>> reqs = std::move(mRequestList); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1967 | mRequestList.clear(); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1968 | if (mProcessingRequest) { |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 1969 | std::chrono::seconds timeout = std::chrono::seconds(kFlushWaitTimeoutSec); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1970 | auto st = mRequestDoneCond.wait_for(lk, timeout); |
| 1971 | if (st == std::cv_status::timeout) { |
| 1972 | ALOGE("%s: wait for inflight request finish timeout!", __FUNCTION__); |
| 1973 | } |
| 1974 | } |
| 1975 | |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 1976 | ALOGV("%s: flusing inflight requests", __FUNCTION__); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1977 | lk.unlock(); |
| 1978 | for (const auto& req : reqs) { |
| 1979 | parent->processCaptureRequestError(req); |
| 1980 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1981 | } |
| 1982 | |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 1983 | void ExternalCameraDeviceSession::OutputThread::waitForNextRequest( |
| 1984 | std::shared_ptr<HalRequest>* out) { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 1985 | ATRACE_CALL(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1986 | if (out == nullptr) { |
| 1987 | ALOGE("%s: out is null", __FUNCTION__); |
| 1988 | return; |
| 1989 | } |
| 1990 | |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 1991 | std::unique_lock<std::mutex> lk(mRequestListLock); |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 1992 | int waitTimes = 0; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1993 | while (mRequestList.empty()) { |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 1994 | if (exitPending()) { |
| 1995 | return; |
| 1996 | } |
| 1997 | std::chrono::milliseconds timeout = std::chrono::milliseconds(kReqWaitTimeoutMs); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 1998 | auto st = mRequestCond.wait_for(lk, timeout); |
| 1999 | if (st == std::cv_status::timeout) { |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 2000 | waitTimes++; |
| 2001 | if (waitTimes == kReqWaitTimesMax) { |
| 2002 | // no new request, return |
| 2003 | return; |
| 2004 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2005 | } |
| 2006 | } |
| 2007 | *out = mRequestList.front(); |
| 2008 | mRequestList.pop_front(); |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 2009 | mProcessingRequest = true; |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 2010 | mProcessingFrameNumer = (*out)->frameNumber; |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | void ExternalCameraDeviceSession::OutputThread::signalRequestDone() { |
| 2014 | std::unique_lock<std::mutex> lk(mRequestListLock); |
| 2015 | mProcessingRequest = false; |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 2016 | mProcessingFrameNumer = 0; |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 2017 | lk.unlock(); |
| 2018 | mRequestDoneCond.notify_one(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 2021 | void ExternalCameraDeviceSession::OutputThread::dump(int fd) { |
| 2022 | std::lock_guard<std::mutex> lk(mRequestListLock); |
| 2023 | if (mProcessingRequest) { |
| 2024 | dprintf(fd, "OutputThread processing frame %d\n", mProcessingFrameNumer); |
| 2025 | } else { |
| 2026 | dprintf(fd, "OutputThread not processing any frames\n"); |
| 2027 | } |
| 2028 | dprintf(fd, "OutputThread request list contains frame: "); |
| 2029 | for (const auto& req : mRequestList) { |
Yin-Chia Yeh | e086fb7 | 2018-02-16 14:54:04 -0800 | [diff] [blame] | 2030 | dprintf(fd, "%d, ", req->frameNumber); |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 2031 | } |
| 2032 | dprintf(fd, "\n"); |
| 2033 | } |
| 2034 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2035 | void ExternalCameraDeviceSession::cleanupBuffersLocked(int id) { |
| 2036 | for (auto& pair : mCirculatingBuffers.at(id)) { |
| 2037 | sHandleImporter.freeBuffer(pair.second); |
| 2038 | } |
| 2039 | mCirculatingBuffers[id].clear(); |
| 2040 | mCirculatingBuffers.erase(id); |
| 2041 | } |
| 2042 | |
| 2043 | void ExternalCameraDeviceSession::updateBufferCaches(const hidl_vec<BufferCache>& cachesToRemove) { |
| 2044 | Mutex::Autolock _l(mLock); |
| 2045 | for (auto& cache : cachesToRemove) { |
| 2046 | auto cbsIt = mCirculatingBuffers.find(cache.streamId); |
| 2047 | if (cbsIt == mCirculatingBuffers.end()) { |
| 2048 | // The stream could have been removed |
| 2049 | continue; |
| 2050 | } |
| 2051 | CirculatingBuffers& cbs = cbsIt->second; |
| 2052 | auto it = cbs.find(cache.bufferId); |
| 2053 | if (it != cbs.end()) { |
| 2054 | sHandleImporter.freeBuffer(it->second); |
| 2055 | cbs.erase(it); |
| 2056 | } else { |
| 2057 | ALOGE("%s: stream %d buffer %" PRIu64 " is not cached", |
| 2058 | __FUNCTION__, cache.streamId, cache.bufferId); |
| 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | bool ExternalCameraDeviceSession::isSupported(const Stream& stream) { |
| 2064 | int32_t ds = static_cast<int32_t>(stream.dataSpace); |
| 2065 | PixelFormat fmt = stream.format; |
| 2066 | uint32_t width = stream.width; |
| 2067 | uint32_t height = stream.height; |
| 2068 | // TODO: check usage flags |
| 2069 | |
| 2070 | if (stream.streamType != StreamType::OUTPUT) { |
| 2071 | ALOGE("%s: does not support non-output stream type", __FUNCTION__); |
| 2072 | return false; |
| 2073 | } |
| 2074 | |
| 2075 | if (stream.rotation != StreamRotation::ROTATION_0) { |
| 2076 | ALOGE("%s: does not support stream rotation", __FUNCTION__); |
| 2077 | return false; |
| 2078 | } |
| 2079 | |
| 2080 | if (ds & Dataspace::DEPTH) { |
| 2081 | ALOGI("%s: does not support depth output", __FUNCTION__); |
| 2082 | return false; |
| 2083 | } |
| 2084 | |
| 2085 | switch (fmt) { |
| 2086 | case PixelFormat::BLOB: |
| 2087 | if (ds != static_cast<int32_t>(Dataspace::V0_JFIF)) { |
| 2088 | ALOGI("%s: BLOB format does not support dataSpace %x", __FUNCTION__, ds); |
| 2089 | return false; |
| 2090 | } |
| 2091 | case PixelFormat::IMPLEMENTATION_DEFINED: |
| 2092 | case PixelFormat::YCBCR_420_888: |
| 2093 | case PixelFormat::YV12: |
| 2094 | // TODO: check what dataspace we can support here. |
| 2095 | // intentional no-ops. |
| 2096 | break; |
| 2097 | default: |
| 2098 | ALOGI("%s: does not support format %x", __FUNCTION__, fmt); |
| 2099 | return false; |
| 2100 | } |
| 2101 | |
| 2102 | // Assume we can convert any V4L2 format to any of supported output format for now, i.e, |
| 2103 | // ignoring v4l2Fmt.fourcc for now. Might need more subtle check if we support more v4l format |
| 2104 | // in the futrue. |
| 2105 | for (const auto& v4l2Fmt : mSupportedFormats) { |
| 2106 | if (width == v4l2Fmt.width && height == v4l2Fmt.height) { |
| 2107 | return true; |
| 2108 | } |
| 2109 | } |
| 2110 | ALOGI("%s: resolution %dx%d is not supported", __FUNCTION__, width, height); |
| 2111 | return false; |
| 2112 | } |
| 2113 | |
| 2114 | int ExternalCameraDeviceSession::v4l2StreamOffLocked() { |
| 2115 | if (!mV4l2Streaming) { |
| 2116 | return OK; |
| 2117 | } |
| 2118 | |
| 2119 | { |
| 2120 | std::lock_guard<std::mutex> lk(mV4l2BufferLock); |
| 2121 | if (mNumDequeuedV4l2Buffers != 0) { |
| 2122 | ALOGE("%s: there are %zu inflight V4L buffers", |
| 2123 | __FUNCTION__, mNumDequeuedV4l2Buffers); |
| 2124 | return -1; |
| 2125 | } |
| 2126 | } |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 2127 | mV4L2BufferCount = 0; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2128 | |
| 2129 | // VIDIOC_STREAMOFF |
| 2130 | v4l2_buf_type capture_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 2131 | if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_STREAMOFF, &capture_type)) < 0) { |
| 2132 | ALOGE("%s: STREAMOFF failed: %s", __FUNCTION__, strerror(errno)); |
| 2133 | return -errno; |
| 2134 | } |
| 2135 | |
| 2136 | // VIDIOC_REQBUFS: clear buffers |
| 2137 | v4l2_requestbuffers req_buffers{}; |
| 2138 | req_buffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 2139 | req_buffers.memory = V4L2_MEMORY_MMAP; |
| 2140 | req_buffers.count = 0; |
| 2141 | if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_REQBUFS, &req_buffers)) < 0) { |
| 2142 | ALOGE("%s: REQBUFS failed: %s", __FUNCTION__, strerror(errno)); |
| 2143 | return -errno; |
| 2144 | } |
| 2145 | |
| 2146 | mV4l2Streaming = false; |
| 2147 | return OK; |
| 2148 | } |
| 2149 | |
Yin-Chia Yeh | 3aa9ae9 | 2018-02-23 17:21:51 -0800 | [diff] [blame] | 2150 | int ExternalCameraDeviceSession::setV4l2FpsLocked(double fps) { |
| 2151 | // VIDIOC_G_PARM/VIDIOC_S_PARM: set fps |
| 2152 | v4l2_streamparm streamparm = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE }; |
| 2153 | // The following line checks that the driver knows about framerate get/set. |
| 2154 | int ret = TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_G_PARM, &streamparm)); |
| 2155 | if (ret != 0) { |
| 2156 | if (errno == -EINVAL) { |
| 2157 | ALOGW("%s: device does not support VIDIOC_G_PARM", __FUNCTION__); |
| 2158 | } |
| 2159 | return -errno; |
| 2160 | } |
| 2161 | // Now check if the device is able to accept a capture framerate set. |
| 2162 | if (!(streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME)) { |
| 2163 | ALOGW("%s: device does not support V4L2_CAP_TIMEPERFRAME", __FUNCTION__); |
| 2164 | return -EINVAL; |
| 2165 | } |
| 2166 | |
| 2167 | // fps is float, approximate by a fraction. |
| 2168 | const int kFrameRatePrecision = 10000; |
| 2169 | streamparm.parm.capture.timeperframe.numerator = kFrameRatePrecision; |
| 2170 | streamparm.parm.capture.timeperframe.denominator = |
| 2171 | (fps * kFrameRatePrecision); |
| 2172 | |
| 2173 | if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_S_PARM, &streamparm)) < 0) { |
| 2174 | ALOGE("%s: failed to set framerate to %f: %s", __FUNCTION__, fps, strerror(errno)); |
| 2175 | return -1; |
| 2176 | } |
| 2177 | |
| 2178 | double retFps = streamparm.parm.capture.timeperframe.denominator / |
| 2179 | static_cast<double>(streamparm.parm.capture.timeperframe.numerator); |
| 2180 | if (std::fabs(fps - retFps) > 1.0) { |
| 2181 | ALOGE("%s: expect fps %f, got %f instead", __FUNCTION__, fps, retFps); |
| 2182 | return -1; |
| 2183 | } |
| 2184 | mV4l2StreamingFps = fps; |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
| 2188 | int ExternalCameraDeviceSession::configureV4l2StreamLocked( |
| 2189 | const SupportedV4L2Format& v4l2Fmt, double requestFps) { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 2190 | ATRACE_CALL(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2191 | int ret = v4l2StreamOffLocked(); |
| 2192 | if (ret != OK) { |
| 2193 | ALOGE("%s: stop v4l2 streaming failed: ret %d", __FUNCTION__, ret); |
| 2194 | return ret; |
| 2195 | } |
| 2196 | |
| 2197 | // VIDIOC_S_FMT w/h/fmt |
| 2198 | v4l2_format fmt; |
| 2199 | fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 2200 | fmt.fmt.pix.width = v4l2Fmt.width; |
| 2201 | fmt.fmt.pix.height = v4l2Fmt.height; |
| 2202 | fmt.fmt.pix.pixelformat = v4l2Fmt.fourcc; |
| 2203 | ret = TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_S_FMT, &fmt)); |
| 2204 | if (ret < 0) { |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 2205 | int numAttempt = 0; |
| 2206 | while (ret < 0) { |
| 2207 | ALOGW("%s: VIDIOC_S_FMT failed, wait 33ms and try again", __FUNCTION__); |
Yin-Chia Yeh | 2d61bfd | 2018-03-14 13:50:23 -0700 | [diff] [blame] | 2208 | usleep(IOCTL_RETRY_SLEEP_US); // sleep and try again |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 2209 | ret = TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_S_FMT, &fmt)); |
| 2210 | if (numAttempt == MAX_RETRY) { |
| 2211 | break; |
| 2212 | } |
| 2213 | numAttempt++; |
| 2214 | } |
| 2215 | if (ret < 0) { |
| 2216 | ALOGE("%s: S_FMT ioctl failed: %s", __FUNCTION__, strerror(errno)); |
| 2217 | return -errno; |
| 2218 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | if (v4l2Fmt.width != fmt.fmt.pix.width || v4l2Fmt.height != fmt.fmt.pix.height || |
| 2222 | v4l2Fmt.fourcc != fmt.fmt.pix.pixelformat) { |
| 2223 | ALOGE("%s: S_FMT expect %c%c%c%c %dx%d, got %c%c%c%c %dx%d instead!", __FUNCTION__, |
| 2224 | v4l2Fmt.fourcc & 0xFF, |
| 2225 | (v4l2Fmt.fourcc >> 8) & 0xFF, |
| 2226 | (v4l2Fmt.fourcc >> 16) & 0xFF, |
| 2227 | (v4l2Fmt.fourcc >> 24) & 0xFF, |
| 2228 | v4l2Fmt.width, v4l2Fmt.height, |
| 2229 | fmt.fmt.pix.pixelformat & 0xFF, |
| 2230 | (fmt.fmt.pix.pixelformat >> 8) & 0xFF, |
| 2231 | (fmt.fmt.pix.pixelformat >> 16) & 0xFF, |
| 2232 | (fmt.fmt.pix.pixelformat >> 24) & 0xFF, |
| 2233 | fmt.fmt.pix.width, fmt.fmt.pix.height); |
| 2234 | return -EINVAL; |
| 2235 | } |
| 2236 | uint32_t bufferSize = fmt.fmt.pix.sizeimage; |
| 2237 | ALOGI("%s: V4L2 buffer size is %d", __FUNCTION__, bufferSize); |
Emilian Peev | bc0e165 | 2018-04-03 13:28:46 +0100 | [diff] [blame] | 2238 | uint32_t expectedMaxBufferSize = kMaxBytesPerPixel * fmt.fmt.pix.width * fmt.fmt.pix.height; |
| 2239 | if ((bufferSize == 0) || (bufferSize > expectedMaxBufferSize)) { |
| 2240 | ALOGE("%s: V4L2 buffer size: %u looks invalid. Expected maximum size: %u", __FUNCTION__, |
| 2241 | bufferSize, expectedMaxBufferSize); |
| 2242 | return -EINVAL; |
| 2243 | } |
| 2244 | mMaxV4L2BufferSize = bufferSize; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2245 | |
Yin-Chia Yeh | 3aa9ae9 | 2018-02-23 17:21:51 -0800 | [diff] [blame] | 2246 | const double kDefaultFps = 30.0; |
| 2247 | double fps = 1000.0; |
| 2248 | if (requestFps != 0.0) { |
| 2249 | fps = requestFps; |
| 2250 | } else { |
| 2251 | double maxFps = -1.0; |
| 2252 | // Try to pick the slowest fps that is at least 30 |
| 2253 | for (const auto& fr : v4l2Fmt.frameRates) { |
| 2254 | double f = fr.getDouble(); |
| 2255 | if (maxFps < f) { |
| 2256 | maxFps = f; |
| 2257 | } |
| 2258 | if (f >= kDefaultFps && f < fps) { |
| 2259 | fps = f; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2260 | } |
| 2261 | } |
Yin-Chia Yeh | 3aa9ae9 | 2018-02-23 17:21:51 -0800 | [diff] [blame] | 2262 | if (fps == 1000.0) { |
| 2263 | fps = maxFps; |
| 2264 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2265 | } |
Yin-Chia Yeh | 3aa9ae9 | 2018-02-23 17:21:51 -0800 | [diff] [blame] | 2266 | |
| 2267 | int fpsRet = setV4l2FpsLocked(fps); |
| 2268 | if (fpsRet != 0 && fpsRet != -EINVAL) { |
| 2269 | ALOGE("%s: set fps failed: %s", __FUNCTION__, strerror(fpsRet)); |
| 2270 | return fpsRet; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2271 | } |
| 2272 | |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 2273 | uint32_t v4lBufferCount = (fps >= kDefaultFps) ? |
| 2274 | mCfg.numVideoBuffers : mCfg.numStillBuffers; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2275 | // VIDIOC_REQBUFS: create buffers |
| 2276 | v4l2_requestbuffers req_buffers{}; |
| 2277 | req_buffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 2278 | req_buffers.memory = V4L2_MEMORY_MMAP; |
| 2279 | req_buffers.count = v4lBufferCount; |
| 2280 | if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_REQBUFS, &req_buffers)) < 0) { |
| 2281 | ALOGE("%s: VIDIOC_REQBUFS failed: %s", __FUNCTION__, strerror(errno)); |
| 2282 | return -errno; |
| 2283 | } |
| 2284 | |
| 2285 | // Driver can indeed return more buffer if it needs more to operate |
| 2286 | if (req_buffers.count < v4lBufferCount) { |
| 2287 | ALOGE("%s: VIDIOC_REQBUFS expected %d buffers, got %d instead", |
| 2288 | __FUNCTION__, v4lBufferCount, req_buffers.count); |
| 2289 | return NO_MEMORY; |
| 2290 | } |
| 2291 | |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 2292 | // VIDIOC_QUERYBUF: get buffer offset in the V4L2 fd |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2293 | // VIDIOC_QBUF: send buffer to driver |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 2294 | mV4L2BufferCount = req_buffers.count; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2295 | for (uint32_t i = 0; i < req_buffers.count; i++) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2296 | v4l2_buffer buffer = { |
| 2297 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 2298 | .index = i, |
| 2299 | .memory = V4L2_MEMORY_MMAP}; |
| 2300 | |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 2301 | if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_QUERYBUF, &buffer)) < 0) { |
| 2302 | ALOGE("%s: QUERYBUF %d failed: %s", __FUNCTION__, i, strerror(errno)); |
| 2303 | return -errno; |
| 2304 | } |
| 2305 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2306 | if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_QBUF, &buffer)) < 0) { |
| 2307 | ALOGE("%s: QBUF %d failed: %s", __FUNCTION__, i, strerror(errno)); |
| 2308 | return -errno; |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | // VIDIOC_STREAMON: start streaming |
| 2313 | v4l2_buf_type capture_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 2314 | ret = TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_STREAMON, &capture_type)); |
| 2315 | if (ret < 0) { |
| 2316 | int numAttempt = 0; |
| 2317 | while (ret < 0) { |
| 2318 | ALOGW("%s: VIDIOC_STREAMON failed, wait 33ms and try again", __FUNCTION__); |
| 2319 | usleep(IOCTL_RETRY_SLEEP_US); // sleep 100 ms and try again |
| 2320 | ret = TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_STREAMON, &capture_type)); |
| 2321 | if (numAttempt == MAX_RETRY) { |
| 2322 | break; |
| 2323 | } |
| 2324 | numAttempt++; |
| 2325 | } |
| 2326 | if (ret < 0) { |
| 2327 | ALOGE("%s: VIDIOC_STREAMON ioctl failed: %s", __FUNCTION__, strerror(errno)); |
| 2328 | return -errno; |
| 2329 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | // Swallow first few frames after streamOn to account for bad frames from some devices |
| 2333 | for (int i = 0; i < kBadFramesAfterStreamOn; i++) { |
| 2334 | v4l2_buffer buffer{}; |
| 2335 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 2336 | buffer.memory = V4L2_MEMORY_MMAP; |
| 2337 | if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_DQBUF, &buffer)) < 0) { |
| 2338 | ALOGE("%s: DQBUF fails: %s", __FUNCTION__, strerror(errno)); |
| 2339 | return -errno; |
| 2340 | } |
| 2341 | |
| 2342 | if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_QBUF, &buffer)) < 0) { |
| 2343 | ALOGE("%s: QBUF index %d fails: %s", __FUNCTION__, buffer.index, strerror(errno)); |
| 2344 | return -errno; |
| 2345 | } |
| 2346 | } |
| 2347 | |
Yin-Chia Yeh | 8b699aa | 2018-02-28 15:58:54 -0800 | [diff] [blame] | 2348 | ALOGI("%s: start V4L2 streaming %dx%d@%ffps", |
| 2349 | __FUNCTION__, v4l2Fmt.width, v4l2Fmt.height, fps); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2350 | mV4l2StreamingFmt = v4l2Fmt; |
| 2351 | mV4l2Streaming = true; |
| 2352 | return OK; |
| 2353 | } |
| 2354 | |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 2355 | sp<V4L2Frame> ExternalCameraDeviceSession::dequeueV4l2FrameLocked(/*out*/nsecs_t* shutterTs) { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 2356 | ATRACE_CALL(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2357 | sp<V4L2Frame> ret = nullptr; |
| 2358 | |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 2359 | if (shutterTs == nullptr) { |
| 2360 | ALOGE("%s: shutterTs must not be null!", __FUNCTION__); |
| 2361 | return ret; |
| 2362 | } |
| 2363 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2364 | { |
| 2365 | std::unique_lock<std::mutex> lk(mV4l2BufferLock); |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 2366 | if (mNumDequeuedV4l2Buffers == mV4L2BufferCount) { |
Yin-Chia Yeh | 3aa9ae9 | 2018-02-23 17:21:51 -0800 | [diff] [blame] | 2367 | int waitRet = waitForV4L2BufferReturnLocked(lk); |
| 2368 | if (waitRet != 0) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2369 | return ret; |
| 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 2374 | ATRACE_BEGIN("VIDIOC_DQBUF"); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2375 | v4l2_buffer buffer{}; |
| 2376 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 2377 | buffer.memory = V4L2_MEMORY_MMAP; |
| 2378 | if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_DQBUF, &buffer)) < 0) { |
| 2379 | ALOGE("%s: DQBUF fails: %s", __FUNCTION__, strerror(errno)); |
| 2380 | return ret; |
| 2381 | } |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 2382 | ATRACE_END(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2383 | |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 2384 | if (buffer.index >= mV4L2BufferCount) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2385 | ALOGE("%s: Invalid buffer id: %d", __FUNCTION__, buffer.index); |
| 2386 | return ret; |
| 2387 | } |
| 2388 | |
| 2389 | if (buffer.flags & V4L2_BUF_FLAG_ERROR) { |
| 2390 | ALOGE("%s: v4l2 buf error! buf flag 0x%x", __FUNCTION__, buffer.flags); |
| 2391 | // TODO: try to dequeue again |
| 2392 | } |
| 2393 | |
Emilian Peev | bc0e165 | 2018-04-03 13:28:46 +0100 | [diff] [blame] | 2394 | if (buffer.bytesused > mMaxV4L2BufferSize) { |
| 2395 | ALOGE("%s: v4l2 buffer bytes used: %u maximum %u", __FUNCTION__, buffer.bytesused, |
| 2396 | mMaxV4L2BufferSize); |
| 2397 | return ret; |
| 2398 | } |
| 2399 | |
Yin-Chia Yeh | 4a3393c | 2018-02-14 12:47:16 -0800 | [diff] [blame] | 2400 | if (buffer.flags & V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC) { |
| 2401 | // Ideally we should also check for V4L2_BUF_FLAG_TSTAMP_SRC_SOE, but |
| 2402 | // even V4L2_BUF_FLAG_TSTAMP_SRC_EOF is better than capture a timestamp now |
| 2403 | *shutterTs = static_cast<nsecs_t>(buffer.timestamp.tv_sec)*1000000000LL + |
| 2404 | buffer.timestamp.tv_usec * 1000LL; |
| 2405 | } else { |
| 2406 | *shutterTs = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2407 | } |
| 2408 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2409 | { |
| 2410 | std::lock_guard<std::mutex> lk(mV4l2BufferLock); |
| 2411 | mNumDequeuedV4l2Buffers++; |
| 2412 | } |
| 2413 | return new V4L2Frame( |
| 2414 | mV4l2StreamingFmt.width, mV4l2StreamingFmt.height, mV4l2StreamingFmt.fourcc, |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 2415 | buffer.index, mV4l2Fd.get(), buffer.bytesused, buffer.m.offset); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | void ExternalCameraDeviceSession::enqueueV4l2Frame(const sp<V4L2Frame>& frame) { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 2419 | ATRACE_CALL(); |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 2420 | frame->unmap(); |
| 2421 | ATRACE_BEGIN("VIDIOC_QBUF"); |
| 2422 | v4l2_buffer buffer{}; |
| 2423 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 2424 | buffer.memory = V4L2_MEMORY_MMAP; |
| 2425 | buffer.index = frame->mBufferIndex; |
| 2426 | if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_QBUF, &buffer)) < 0) { |
| 2427 | ALOGE("%s: QBUF index %d fails: %s", __FUNCTION__, |
| 2428 | frame->mBufferIndex, strerror(errno)); |
| 2429 | return; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2430 | } |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 2431 | ATRACE_END(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2432 | |
| 2433 | { |
| 2434 | std::lock_guard<std::mutex> lk(mV4l2BufferLock); |
| 2435 | mNumDequeuedV4l2Buffers--; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2436 | } |
Yin-Chia Yeh | 190e560 | 2018-02-13 17:43:13 -0800 | [diff] [blame] | 2437 | mV4L2BufferReturned.notify_one(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | Status ExternalCameraDeviceSession::configureStreams( |
Yin-Chia Yeh | 4c64118 | 2018-08-06 15:01:20 -0700 | [diff] [blame] | 2441 | const V3_2::StreamConfiguration& config, |
| 2442 | V3_3::HalStreamConfiguration* out, |
| 2443 | uint32_t blobBufferSize) { |
Yin-Chia Yeh | e99cf20 | 2018-02-28 11:30:38 -0800 | [diff] [blame] | 2444 | ATRACE_CALL(); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2445 | if (config.operationMode != StreamConfigurationMode::NORMAL_MODE) { |
| 2446 | ALOGE("%s: unsupported operation mode: %d", __FUNCTION__, config.operationMode); |
| 2447 | return Status::ILLEGAL_ARGUMENT; |
| 2448 | } |
| 2449 | |
| 2450 | if (config.streams.size() == 0) { |
| 2451 | ALOGE("%s: cannot configure zero stream", __FUNCTION__); |
| 2452 | return Status::ILLEGAL_ARGUMENT; |
| 2453 | } |
| 2454 | |
| 2455 | int numProcessedStream = 0; |
| 2456 | int numStallStream = 0; |
| 2457 | for (const auto& stream : config.streams) { |
| 2458 | // Check if the format/width/height combo is supported |
| 2459 | if (!isSupported(stream)) { |
| 2460 | return Status::ILLEGAL_ARGUMENT; |
| 2461 | } |
| 2462 | if (stream.format == PixelFormat::BLOB) { |
| 2463 | numStallStream++; |
| 2464 | } else { |
| 2465 | numProcessedStream++; |
| 2466 | } |
| 2467 | } |
| 2468 | |
| 2469 | if (numProcessedStream > kMaxProcessedStream) { |
| 2470 | ALOGE("%s: too many processed streams (expect <= %d, got %d)", __FUNCTION__, |
| 2471 | kMaxProcessedStream, numProcessedStream); |
| 2472 | return Status::ILLEGAL_ARGUMENT; |
| 2473 | } |
| 2474 | |
| 2475 | if (numStallStream > kMaxStallStream) { |
| 2476 | ALOGE("%s: too many stall streams (expect <= %d, got %d)", __FUNCTION__, |
| 2477 | kMaxStallStream, numStallStream); |
| 2478 | return Status::ILLEGAL_ARGUMENT; |
| 2479 | } |
| 2480 | |
| 2481 | Status status = initStatus(); |
| 2482 | if (status != Status::OK) { |
| 2483 | return status; |
| 2484 | } |
| 2485 | |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 2486 | |
| 2487 | { |
| 2488 | std::lock_guard<std::mutex> lk(mInflightFramesLock); |
| 2489 | if (!mInflightFrames.empty()) { |
| 2490 | ALOGE("%s: trying to configureStreams while there are still %zu inflight frames!", |
| 2491 | __FUNCTION__, mInflightFrames.size()); |
| 2492 | return Status::INTERNAL_ERROR; |
| 2493 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2494 | } |
| 2495 | |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 2496 | Mutex::Autolock _l(mLock); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2497 | // Add new streams |
| 2498 | for (const auto& stream : config.streams) { |
| 2499 | if (mStreamMap.count(stream.id) == 0) { |
| 2500 | mStreamMap[stream.id] = stream; |
| 2501 | mCirculatingBuffers.emplace(stream.id, CirculatingBuffers{}); |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | // Cleanup removed streams |
| 2506 | for(auto it = mStreamMap.begin(); it != mStreamMap.end();) { |
| 2507 | int id = it->first; |
| 2508 | bool found = false; |
| 2509 | for (const auto& stream : config.streams) { |
| 2510 | if (id == stream.id) { |
| 2511 | found = true; |
| 2512 | break; |
| 2513 | } |
| 2514 | } |
| 2515 | if (!found) { |
| 2516 | // Unmap all buffers of deleted stream |
| 2517 | cleanupBuffersLocked(id); |
| 2518 | it = mStreamMap.erase(it); |
| 2519 | } else { |
| 2520 | ++it; |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | // Now select a V4L2 format to produce all output streams |
| 2525 | float desiredAr = (mCroppingType == VERTICAL) ? kMaxAspectRatio : kMinAspectRatio; |
| 2526 | uint32_t maxDim = 0; |
| 2527 | for (const auto& stream : config.streams) { |
| 2528 | float aspectRatio = ASPECT_RATIO(stream); |
Yin-Chia Yeh | c15a1ca | 2018-03-02 14:16:52 -0800 | [diff] [blame] | 2529 | ALOGI("%s: request stream %dx%d", __FUNCTION__, stream.width, stream.height); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2530 | if ((mCroppingType == VERTICAL && aspectRatio < desiredAr) || |
| 2531 | (mCroppingType == HORIZONTAL && aspectRatio > desiredAr)) { |
| 2532 | desiredAr = aspectRatio; |
| 2533 | } |
| 2534 | |
| 2535 | // The dimension that's not cropped |
| 2536 | uint32_t dim = (mCroppingType == VERTICAL) ? stream.width : stream.height; |
| 2537 | if (dim > maxDim) { |
| 2538 | maxDim = dim; |
| 2539 | } |
| 2540 | } |
| 2541 | // Find the smallest format that matches the desired aspect ratio and is wide/high enough |
| 2542 | SupportedV4L2Format v4l2Fmt {.width = 0, .height = 0}; |
| 2543 | for (const auto& fmt : mSupportedFormats) { |
| 2544 | uint32_t dim = (mCroppingType == VERTICAL) ? fmt.width : fmt.height; |
| 2545 | if (dim >= maxDim) { |
| 2546 | float aspectRatio = ASPECT_RATIO(fmt); |
| 2547 | if (isAspectRatioClose(aspectRatio, desiredAr)) { |
| 2548 | v4l2Fmt = fmt; |
| 2549 | // since mSupportedFormats is sorted by width then height, the first matching fmt |
| 2550 | // will be the smallest one with matching aspect ratio |
| 2551 | break; |
| 2552 | } |
| 2553 | } |
| 2554 | } |
| 2555 | if (v4l2Fmt.width == 0) { |
| 2556 | // Cannot find exact good aspect ratio candidate, try to find a close one |
| 2557 | for (const auto& fmt : mSupportedFormats) { |
| 2558 | uint32_t dim = (mCroppingType == VERTICAL) ? fmt.width : fmt.height; |
| 2559 | if (dim >= maxDim) { |
| 2560 | float aspectRatio = ASPECT_RATIO(fmt); |
| 2561 | if ((mCroppingType == VERTICAL && aspectRatio < desiredAr) || |
| 2562 | (mCroppingType == HORIZONTAL && aspectRatio > desiredAr)) { |
| 2563 | v4l2Fmt = fmt; |
| 2564 | break; |
| 2565 | } |
| 2566 | } |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | if (v4l2Fmt.width == 0) { |
| 2571 | ALOGE("%s: unable to find a resolution matching (%s at least %d, aspect ratio %f)" |
| 2572 | , __FUNCTION__, (mCroppingType == VERTICAL) ? "width" : "height", |
| 2573 | maxDim, desiredAr); |
| 2574 | return Status::ILLEGAL_ARGUMENT; |
| 2575 | } |
| 2576 | |
| 2577 | if (configureV4l2StreamLocked(v4l2Fmt) != 0) { |
| 2578 | ALOGE("V4L configuration failed!, format:%c%c%c%c, w %d, h %d", |
| 2579 | v4l2Fmt.fourcc & 0xFF, |
| 2580 | (v4l2Fmt.fourcc >> 8) & 0xFF, |
| 2581 | (v4l2Fmt.fourcc >> 16) & 0xFF, |
| 2582 | (v4l2Fmt.fourcc >> 24) & 0xFF, |
| 2583 | v4l2Fmt.width, v4l2Fmt.height); |
| 2584 | return Status::INTERNAL_ERROR; |
| 2585 | } |
| 2586 | |
| 2587 | Size v4lSize = {v4l2Fmt.width, v4l2Fmt.height}; |
Yuriy Romanenko | e932f1b | 2018-01-19 16:12:00 -0800 | [diff] [blame] | 2588 | Size thumbSize { 0, 0 }; |
| 2589 | camera_metadata_ro_entry entry = |
| 2590 | mCameraCharacteristics.find(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES); |
| 2591 | for(uint32_t i = 0; i < entry.count; i += 2) { |
| 2592 | Size sz { static_cast<uint32_t>(entry.data.i32[i]), |
| 2593 | static_cast<uint32_t>(entry.data.i32[i+1]) }; |
| 2594 | if(sz.width * sz.height > thumbSize.width * thumbSize.height) { |
| 2595 | thumbSize = sz; |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | if (thumbSize.width * thumbSize.height == 0) { |
| 2600 | ALOGE("%s: non-zero thumbnail size not available", __FUNCTION__); |
| 2601 | return Status::INTERNAL_ERROR; |
| 2602 | } |
| 2603 | |
| 2604 | status = mOutputThread->allocateIntermediateBuffers(v4lSize, |
Yin-Chia Yeh | 4c64118 | 2018-08-06 15:01:20 -0700 | [diff] [blame] | 2605 | mMaxThumbResolution, config.streams, blobBufferSize); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2606 | if (status != Status::OK) { |
| 2607 | ALOGE("%s: allocating intermediate buffers failed!", __FUNCTION__); |
| 2608 | return status; |
| 2609 | } |
| 2610 | |
| 2611 | out->streams.resize(config.streams.size()); |
| 2612 | for (size_t i = 0; i < config.streams.size(); i++) { |
| 2613 | out->streams[i].overrideDataSpace = config.streams[i].dataSpace; |
| 2614 | out->streams[i].v3_2.id = config.streams[i].id; |
| 2615 | // TODO: double check should we add those CAMERA flags |
| 2616 | mStreamMap[config.streams[i].id].usage = |
| 2617 | out->streams[i].v3_2.producerUsage = config.streams[i].usage | |
| 2618 | BufferUsage::CPU_WRITE_OFTEN | |
| 2619 | BufferUsage::CAMERA_OUTPUT; |
| 2620 | out->streams[i].v3_2.consumerUsage = 0; |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 2621 | out->streams[i].v3_2.maxBuffers = mV4L2BufferCount; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2622 | |
| 2623 | switch (config.streams[i].format) { |
| 2624 | case PixelFormat::BLOB: |
| 2625 | case PixelFormat::YCBCR_420_888: |
Yin-Chia Yeh | fb1c154 | 2018-01-24 15:46:36 -0800 | [diff] [blame] | 2626 | case PixelFormat::YV12: // Used by SurfaceTexture |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2627 | // No override |
| 2628 | out->streams[i].v3_2.overrideFormat = config.streams[i].format; |
| 2629 | break; |
| 2630 | case PixelFormat::IMPLEMENTATION_DEFINED: |
| 2631 | // Override based on VIDEO or not |
| 2632 | out->streams[i].v3_2.overrideFormat = |
| 2633 | (config.streams[i].usage & BufferUsage::VIDEO_ENCODER) ? |
| 2634 | PixelFormat::YCBCR_420_888 : PixelFormat::YV12; |
| 2635 | // Save overridden formt in mStreamMap |
| 2636 | mStreamMap[config.streams[i].id].format = out->streams[i].v3_2.overrideFormat; |
| 2637 | break; |
| 2638 | default: |
Yin-Chia Yeh | fb1c154 | 2018-01-24 15:46:36 -0800 | [diff] [blame] | 2639 | ALOGE("%s: unsupported format 0x%x", __FUNCTION__, config.streams[i].format); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2640 | return Status::ILLEGAL_ARGUMENT; |
| 2641 | } |
| 2642 | } |
| 2643 | |
| 2644 | mFirstRequest = true; |
| 2645 | return Status::OK; |
| 2646 | } |
| 2647 | |
| 2648 | bool ExternalCameraDeviceSession::isClosed() { |
| 2649 | Mutex::Autolock _l(mLock); |
| 2650 | return mClosed; |
| 2651 | } |
| 2652 | |
| 2653 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 2654 | #define UPDATE(md, tag, data, size) \ |
| 2655 | do { \ |
| 2656 | if ((md).update((tag), (data), (size))) { \ |
| 2657 | ALOGE("Update " #tag " failed!"); \ |
| 2658 | return BAD_VALUE; \ |
| 2659 | } \ |
| 2660 | } while (0) |
| 2661 | |
| 2662 | status_t ExternalCameraDeviceSession::initDefaultRequests() { |
| 2663 | ::android::hardware::camera::common::V1_0::helper::CameraMetadata md; |
| 2664 | |
| 2665 | const uint8_t aberrationMode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF; |
| 2666 | UPDATE(md, ANDROID_COLOR_CORRECTION_ABERRATION_MODE, &aberrationMode, 1); |
| 2667 | |
| 2668 | const int32_t exposureCompensation = 0; |
| 2669 | UPDATE(md, ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, &exposureCompensation, 1); |
| 2670 | |
| 2671 | const uint8_t videoStabilizationMode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF; |
| 2672 | UPDATE(md, ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, &videoStabilizationMode, 1); |
| 2673 | |
| 2674 | const uint8_t awbMode = ANDROID_CONTROL_AWB_MODE_AUTO; |
| 2675 | UPDATE(md, ANDROID_CONTROL_AWB_MODE, &awbMode, 1); |
| 2676 | |
| 2677 | const uint8_t aeMode = ANDROID_CONTROL_AE_MODE_ON; |
| 2678 | UPDATE(md, ANDROID_CONTROL_AE_MODE, &aeMode, 1); |
| 2679 | |
| 2680 | const uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE; |
| 2681 | UPDATE(md, ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1); |
| 2682 | |
| 2683 | const uint8_t afMode = ANDROID_CONTROL_AF_MODE_AUTO; |
| 2684 | UPDATE(md, ANDROID_CONTROL_AF_MODE, &afMode, 1); |
| 2685 | |
| 2686 | const uint8_t afTrigger = ANDROID_CONTROL_AF_TRIGGER_IDLE; |
| 2687 | UPDATE(md, ANDROID_CONTROL_AF_TRIGGER, &afTrigger, 1); |
| 2688 | |
| 2689 | const uint8_t sceneMode = ANDROID_CONTROL_SCENE_MODE_DISABLED; |
| 2690 | UPDATE(md, ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1); |
| 2691 | |
| 2692 | const uint8_t effectMode = ANDROID_CONTROL_EFFECT_MODE_OFF; |
| 2693 | UPDATE(md, ANDROID_CONTROL_EFFECT_MODE, &effectMode, 1); |
| 2694 | |
| 2695 | const uint8_t flashMode = ANDROID_FLASH_MODE_OFF; |
| 2696 | UPDATE(md, ANDROID_FLASH_MODE, &flashMode, 1); |
| 2697 | |
| 2698 | const int32_t thumbnailSize[] = {240, 180}; |
| 2699 | UPDATE(md, ANDROID_JPEG_THUMBNAIL_SIZE, thumbnailSize, 2); |
| 2700 | |
| 2701 | const uint8_t jpegQuality = 90; |
| 2702 | UPDATE(md, ANDROID_JPEG_QUALITY, &jpegQuality, 1); |
| 2703 | UPDATE(md, ANDROID_JPEG_THUMBNAIL_QUALITY, &jpegQuality, 1); |
| 2704 | |
| 2705 | const int32_t jpegOrientation = 0; |
| 2706 | UPDATE(md, ANDROID_JPEG_ORIENTATION, &jpegOrientation, 1); |
| 2707 | |
| 2708 | const uint8_t oisMode = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF; |
| 2709 | UPDATE(md, ANDROID_LENS_OPTICAL_STABILIZATION_MODE, &oisMode, 1); |
| 2710 | |
| 2711 | const uint8_t nrMode = ANDROID_NOISE_REDUCTION_MODE_OFF; |
| 2712 | UPDATE(md, ANDROID_NOISE_REDUCTION_MODE, &nrMode, 1); |
| 2713 | |
Yin-Chia Yeh | c15a1ca | 2018-03-02 14:16:52 -0800 | [diff] [blame] | 2714 | const int32_t testPatternModes = ANDROID_SENSOR_TEST_PATTERN_MODE_OFF; |
| 2715 | UPDATE(md, ANDROID_SENSOR_TEST_PATTERN_MODE, &testPatternModes, 1); |
| 2716 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2717 | const uint8_t fdMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF; |
| 2718 | UPDATE(md, ANDROID_STATISTICS_FACE_DETECT_MODE, &fdMode, 1); |
| 2719 | |
| 2720 | const uint8_t hotpixelMode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF; |
| 2721 | UPDATE(md, ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE, &hotpixelMode, 1); |
| 2722 | |
| 2723 | bool support30Fps = false; |
| 2724 | int32_t maxFps = std::numeric_limits<int32_t>::min(); |
| 2725 | for (const auto& supportedFormat : mSupportedFormats) { |
Yin-Chia Yeh | 134093a | 2018-02-12 14:05:48 -0800 | [diff] [blame] | 2726 | for (const auto& fr : supportedFormat.frameRates) { |
| 2727 | int32_t framerateInt = static_cast<int32_t>(fr.getDouble()); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2728 | if (maxFps < framerateInt) { |
| 2729 | maxFps = framerateInt; |
| 2730 | } |
| 2731 | if (framerateInt == 30) { |
| 2732 | support30Fps = true; |
| 2733 | break; |
| 2734 | } |
| 2735 | } |
| 2736 | if (support30Fps) { |
| 2737 | break; |
| 2738 | } |
| 2739 | } |
| 2740 | int32_t defaultFramerate = support30Fps ? 30 : maxFps; |
Yin-Chia Yeh | c15a1ca | 2018-03-02 14:16:52 -0800 | [diff] [blame] | 2741 | int32_t defaultFpsRange[] = {defaultFramerate / 2, defaultFramerate}; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2742 | UPDATE(md, ANDROID_CONTROL_AE_TARGET_FPS_RANGE, defaultFpsRange, ARRAY_SIZE(defaultFpsRange)); |
| 2743 | |
| 2744 | uint8_t antibandingMode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO; |
| 2745 | UPDATE(md, ANDROID_CONTROL_AE_ANTIBANDING_MODE, &antibandingMode, 1); |
| 2746 | |
| 2747 | const uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO; |
| 2748 | UPDATE(md, ANDROID_CONTROL_MODE, &controlMode, 1); |
| 2749 | |
Steven Moreland | c90461c | 2018-05-01 16:54:09 -0700 | [diff] [blame] | 2750 | auto requestTemplates = hidl_enum_range<RequestTemplate>(); |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 2751 | for (RequestTemplate type : requestTemplates) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2752 | ::android::hardware::camera::common::V1_0::helper::CameraMetadata mdCopy = md; |
| 2753 | uint8_t intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
| 2754 | switch (type) { |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 2755 | case RequestTemplate::PREVIEW: |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2756 | intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
| 2757 | break; |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 2758 | case RequestTemplate::STILL_CAPTURE: |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2759 | intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE; |
| 2760 | break; |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 2761 | case RequestTemplate::VIDEO_RECORD: |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2762 | intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD; |
| 2763 | break; |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 2764 | case RequestTemplate::VIDEO_SNAPSHOT: |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2765 | intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT; |
| 2766 | break; |
| 2767 | default: |
Yuriy Romanenko | 083de0c | 2018-01-26 16:05:54 -0800 | [diff] [blame] | 2768 | ALOGV("%s: unsupported RequestTemplate type %d", __FUNCTION__, type); |
| 2769 | continue; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2770 | } |
| 2771 | UPDATE(mdCopy, ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1); |
| 2772 | |
| 2773 | camera_metadata_t* rawMd = mdCopy.release(); |
| 2774 | CameraMetadata hidlMd; |
| 2775 | hidlMd.setToExternal( |
| 2776 | (uint8_t*) rawMd, get_camera_metadata_size(rawMd)); |
| 2777 | mDefaultRequests[type] = hidlMd; |
| 2778 | free_camera_metadata(rawMd); |
| 2779 | } |
| 2780 | |
| 2781 | return OK; |
| 2782 | } |
| 2783 | |
| 2784 | status_t ExternalCameraDeviceSession::fillCaptureResult( |
| 2785 | common::V1_0::helper::CameraMetadata &md, nsecs_t timestamp) { |
| 2786 | // android.control |
| 2787 | // For USB camera, we don't know the AE state. Set the state to converged to |
| 2788 | // indicate the frame should be good to use. Then apps don't have to wait the |
| 2789 | // AE state. |
| 2790 | const uint8_t aeState = ANDROID_CONTROL_AE_STATE_CONVERGED; |
| 2791 | UPDATE(md, ANDROID_CONTROL_AE_STATE, &aeState, 1); |
| 2792 | |
| 2793 | const uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF; |
| 2794 | UPDATE(md, ANDROID_CONTROL_AE_LOCK, &ae_lock, 1); |
| 2795 | |
Yin-Chia Yeh | 94f52a3 | 2018-03-07 14:46:51 -0800 | [diff] [blame] | 2796 | bool afTrigger = false; |
| 2797 | { |
| 2798 | std::lock_guard<std::mutex> lk(mAfTriggerLock); |
| 2799 | afTrigger = mAfTrigger; |
| 2800 | if (md.exists(ANDROID_CONTROL_AF_TRIGGER)) { |
| 2801 | camera_metadata_entry entry = md.find(ANDROID_CONTROL_AF_TRIGGER); |
| 2802 | if (entry.data.u8[0] == ANDROID_CONTROL_AF_TRIGGER_START) { |
| 2803 | mAfTrigger = afTrigger = true; |
| 2804 | } else if (entry.data.u8[0] == ANDROID_CONTROL_AF_TRIGGER_CANCEL) { |
| 2805 | mAfTrigger = afTrigger = false; |
| 2806 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2807 | } |
| 2808 | } |
| 2809 | |
| 2810 | // For USB camera, the USB camera handles everything and we don't have control |
| 2811 | // over AF. We only simply fake the AF metadata based on the request |
| 2812 | // received here. |
| 2813 | uint8_t afState; |
| 2814 | if (afTrigger) { |
| 2815 | afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED; |
| 2816 | } else { |
| 2817 | afState = ANDROID_CONTROL_AF_STATE_INACTIVE; |
| 2818 | } |
| 2819 | UPDATE(md, ANDROID_CONTROL_AF_STATE, &afState, 1); |
| 2820 | |
| 2821 | // Set AWB state to converged to indicate the frame should be good to use. |
| 2822 | const uint8_t awbState = ANDROID_CONTROL_AWB_STATE_CONVERGED; |
| 2823 | UPDATE(md, ANDROID_CONTROL_AWB_STATE, &awbState, 1); |
| 2824 | |
| 2825 | const uint8_t awbLock = ANDROID_CONTROL_AWB_LOCK_OFF; |
| 2826 | UPDATE(md, ANDROID_CONTROL_AWB_LOCK, &awbLock, 1); |
| 2827 | |
| 2828 | camera_metadata_ro_entry active_array_size = |
| 2829 | mCameraCharacteristics.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE); |
| 2830 | |
| 2831 | if (active_array_size.count == 0) { |
| 2832 | ALOGE("%s: cannot find active array size!", __FUNCTION__); |
| 2833 | return -EINVAL; |
| 2834 | } |
| 2835 | |
Yin-Chia Yeh | fb1c154 | 2018-01-24 15:46:36 -0800 | [diff] [blame] | 2836 | const uint8_t flashState = ANDROID_FLASH_STATE_UNAVAILABLE; |
| 2837 | UPDATE(md, ANDROID_FLASH_STATE, &flashState, 1); |
| 2838 | |
Yin-Chia Yeh | c15a1ca | 2018-03-02 14:16:52 -0800 | [diff] [blame] | 2839 | // This means pipeline latency of X frame intervals. The maximum number is 4. |
| 2840 | const uint8_t requestPipelineMaxDepth = 4; |
| 2841 | UPDATE(md, ANDROID_REQUEST_PIPELINE_DEPTH, &requestPipelineMaxDepth, 1); |
| 2842 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2843 | // android.scaler |
| 2844 | const int32_t crop_region[] = { |
| 2845 | active_array_size.data.i32[0], active_array_size.data.i32[1], |
| 2846 | active_array_size.data.i32[2], active_array_size.data.i32[3], |
| 2847 | }; |
| 2848 | UPDATE(md, ANDROID_SCALER_CROP_REGION, crop_region, ARRAY_SIZE(crop_region)); |
| 2849 | |
| 2850 | // android.sensor |
| 2851 | UPDATE(md, ANDROID_SENSOR_TIMESTAMP, ×tamp, 1); |
| 2852 | |
| 2853 | // android.statistics |
| 2854 | const uint8_t lensShadingMapMode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF; |
| 2855 | UPDATE(md, ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, &lensShadingMapMode, 1); |
| 2856 | |
| 2857 | const uint8_t sceneFlicker = ANDROID_STATISTICS_SCENE_FLICKER_NONE; |
| 2858 | UPDATE(md, ANDROID_STATISTICS_SCENE_FLICKER, &sceneFlicker, 1); |
| 2859 | |
| 2860 | return OK; |
| 2861 | } |
| 2862 | |
| 2863 | #undef ARRAY_SIZE |
| 2864 | #undef UPDATE |
| 2865 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 2866 | } // namespace implementation |
| 2867 | } // namespace V3_4 |
| 2868 | } // namespace device |
| 2869 | } // namespace camera |
| 2870 | } // namespace hardware |
| 2871 | } // namespace android |