Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "Camera3-Stream" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include <utils/Log.h> |
| 22 | #include <utils/Trace.h> |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 23 | #include "device3/Camera3Stream.h" |
| 24 | #include "device3/StatusTracker.h" |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 25 | |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
| 27 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 28 | namespace android { |
| 29 | |
| 30 | namespace camera3 { |
| 31 | |
| 32 | Camera3Stream::~Camera3Stream() { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 33 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 34 | if (statusTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) { |
| 35 | statusTracker->removeComponent(mStatusId); |
| 36 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | Camera3Stream* Camera3Stream::cast(camera3_stream *stream) { |
| 40 | return static_cast<Camera3Stream*>(stream); |
| 41 | } |
| 42 | |
| 43 | const Camera3Stream* Camera3Stream::cast(const camera3_stream *stream) { |
| 44 | return static_cast<const Camera3Stream*>(stream); |
| 45 | } |
| 46 | |
| 47 | Camera3Stream::Camera3Stream(int id, |
| 48 | camera3_stream_type type, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 49 | uint32_t width, uint32_t height, size_t maxSize, int format, |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 50 | android_dataspace dataSpace, camera3_stream_rotation_t rotation) : |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 51 | camera3_stream(), |
| 52 | mId(id), |
| 53 | mName(String8::format("Camera3Stream[%d]", id)), |
| 54 | mMaxSize(maxSize), |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 55 | mState(STATE_CONSTRUCTED), |
| 56 | mStatusId(StatusTracker::NO_STATUS_ID) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 57 | |
| 58 | camera3_stream::stream_type = type; |
| 59 | camera3_stream::width = width; |
| 60 | camera3_stream::height = height; |
| 61 | camera3_stream::format = format; |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 62 | camera3_stream::data_space = dataSpace; |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 63 | camera3_stream::rotation = rotation; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 64 | camera3_stream::usage = 0; |
| 65 | camera3_stream::max_buffers = 0; |
| 66 | camera3_stream::priv = NULL; |
| 67 | |
| 68 | if (format == HAL_PIXEL_FORMAT_BLOB && maxSize == 0) { |
| 69 | ALOGE("%s: BLOB format with size == 0", __FUNCTION__); |
| 70 | mState = STATE_ERROR; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | int Camera3Stream::getId() const { |
| 75 | return mId; |
| 76 | } |
| 77 | |
| 78 | uint32_t Camera3Stream::getWidth() const { |
| 79 | return camera3_stream::width; |
| 80 | } |
| 81 | |
| 82 | uint32_t Camera3Stream::getHeight() const { |
| 83 | return camera3_stream::height; |
| 84 | } |
| 85 | |
| 86 | int Camera3Stream::getFormat() const { |
| 87 | return camera3_stream::format; |
| 88 | } |
| 89 | |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 90 | android_dataspace Camera3Stream::getDataSpace() const { |
| 91 | return camera3_stream::data_space; |
| 92 | } |
| 93 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 94 | camera3_stream* Camera3Stream::startConfiguration() { |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 95 | ATRACE_CALL(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 96 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 97 | status_t res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 98 | |
| 99 | switch (mState) { |
| 100 | case STATE_ERROR: |
| 101 | ALOGE("%s: In error state", __FUNCTION__); |
| 102 | return NULL; |
| 103 | case STATE_CONSTRUCTED: |
| 104 | // OK |
| 105 | break; |
| 106 | case STATE_IN_CONFIG: |
| 107 | case STATE_IN_RECONFIG: |
| 108 | // Can start config again with no trouble; but don't redo |
| 109 | // oldUsage/oldMaxBuffers |
| 110 | return this; |
| 111 | case STATE_CONFIGURED: |
Chien-Yu Chen | 90746f4 | 2015-04-15 13:50:13 -0700 | [diff] [blame] | 112 | if (hasOutstandingBuffersLocked()) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 113 | ALOGE("%s: Cannot configure stream; has outstanding buffers", |
| 114 | __FUNCTION__); |
| 115 | return NULL; |
| 116 | } |
| 117 | break; |
| 118 | default: |
| 119 | ALOGE("%s: Unknown state %d", __FUNCTION__, mState); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 123 | oldUsage = camera3_stream::usage; |
| 124 | oldMaxBuffers = camera3_stream::max_buffers; |
| 125 | |
| 126 | res = getEndpointUsage(&(camera3_stream::usage)); |
| 127 | if (res != OK) { |
| 128 | ALOGE("%s: Cannot query consumer endpoint usage!", |
| 129 | __FUNCTION__); |
| 130 | return NULL; |
| 131 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 132 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 133 | // Stop tracking if currently doing so |
| 134 | if (mStatusId != StatusTracker::NO_STATUS_ID) { |
| 135 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 136 | if (statusTracker != 0) { |
| 137 | statusTracker->removeComponent(mStatusId); |
| 138 | } |
| 139 | mStatusId = StatusTracker::NO_STATUS_ID; |
| 140 | } |
| 141 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 142 | if (mState == STATE_CONSTRUCTED) { |
| 143 | mState = STATE_IN_CONFIG; |
| 144 | } else { // mState == STATE_CONFIGURED |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 145 | LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 146 | mState = STATE_IN_RECONFIG; |
| 147 | } |
| 148 | |
| 149 | return this; |
| 150 | } |
| 151 | |
| 152 | bool Camera3Stream::isConfiguring() const { |
| 153 | Mutex::Autolock l(mLock); |
| 154 | return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG); |
| 155 | } |
| 156 | |
| 157 | status_t Camera3Stream::finishConfiguration(camera3_device *hal3Device) { |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 158 | ATRACE_CALL(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 159 | Mutex::Autolock l(mLock); |
| 160 | switch (mState) { |
| 161 | case STATE_ERROR: |
| 162 | ALOGE("%s: In error state", __FUNCTION__); |
| 163 | return INVALID_OPERATION; |
| 164 | case STATE_IN_CONFIG: |
| 165 | case STATE_IN_RECONFIG: |
| 166 | // OK |
| 167 | break; |
| 168 | case STATE_CONSTRUCTED: |
| 169 | case STATE_CONFIGURED: |
| 170 | ALOGE("%s: Cannot finish configuration that hasn't been started", |
| 171 | __FUNCTION__); |
| 172 | return INVALID_OPERATION; |
| 173 | default: |
| 174 | ALOGE("%s: Unknown state", __FUNCTION__); |
| 175 | return INVALID_OPERATION; |
| 176 | } |
| 177 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 178 | // Register for idle tracking |
| 179 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 180 | if (statusTracker != 0) { |
| 181 | mStatusId = statusTracker->addComponent(); |
| 182 | } |
| 183 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 184 | // Check if the stream configuration is unchanged, and skip reallocation if |
| 185 | // so. As documented in hardware/camera3.h:configure_streams(). |
| 186 | if (mState == STATE_IN_RECONFIG && |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 187 | oldUsage == camera3_stream::usage && |
| 188 | oldMaxBuffers == camera3_stream::max_buffers) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 189 | mState = STATE_CONFIGURED; |
| 190 | return OK; |
| 191 | } |
| 192 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 193 | // Reset prepared state, since buffer config has changed, and existing |
| 194 | // allocations are no longer valid |
| 195 | mPrepared = false; |
| 196 | mStreamUnpreparable = false; |
| 197 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 198 | status_t res; |
| 199 | res = configureQueueLocked(); |
| 200 | if (res != OK) { |
| 201 | ALOGE("%s: Unable to configure stream %d queue: %s (%d)", |
| 202 | __FUNCTION__, mId, strerror(-res), res); |
| 203 | mState = STATE_ERROR; |
| 204 | return res; |
| 205 | } |
| 206 | |
| 207 | res = registerBuffersLocked(hal3Device); |
| 208 | if (res != OK) { |
| 209 | ALOGE("%s: Unable to register stream buffers with HAL: %s (%d)", |
| 210 | __FUNCTION__, strerror(-res), res); |
| 211 | mState = STATE_ERROR; |
| 212 | return res; |
| 213 | } |
| 214 | |
| 215 | mState = STATE_CONFIGURED; |
| 216 | |
| 217 | return res; |
| 218 | } |
| 219 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 220 | status_t Camera3Stream::cancelConfiguration() { |
| 221 | ATRACE_CALL(); |
| 222 | Mutex::Autolock l(mLock); |
| 223 | switch (mState) { |
| 224 | case STATE_ERROR: |
| 225 | ALOGE("%s: In error state", __FUNCTION__); |
| 226 | return INVALID_OPERATION; |
| 227 | case STATE_IN_CONFIG: |
| 228 | case STATE_IN_RECONFIG: |
| 229 | // OK |
| 230 | break; |
| 231 | case STATE_CONSTRUCTED: |
| 232 | case STATE_CONFIGURED: |
| 233 | ALOGE("%s: Cannot cancel configuration that hasn't been started", |
| 234 | __FUNCTION__); |
| 235 | return INVALID_OPERATION; |
| 236 | default: |
| 237 | ALOGE("%s: Unknown state", __FUNCTION__); |
| 238 | return INVALID_OPERATION; |
| 239 | } |
| 240 | |
| 241 | camera3_stream::usage = oldUsage; |
| 242 | camera3_stream::max_buffers = oldMaxBuffers; |
| 243 | |
Yin-Chia Yeh | 3ea3fcd | 2014-09-05 14:14:44 -0700 | [diff] [blame] | 244 | mState = (mState == STATE_IN_RECONFIG) ? STATE_CONFIGURED : STATE_CONSTRUCTED; |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 245 | return OK; |
| 246 | } |
| 247 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 248 | bool Camera3Stream::isUnpreparable() { |
| 249 | ATRACE_CALL(); |
| 250 | |
| 251 | Mutex::Autolock l(mLock); |
| 252 | return mStreamUnpreparable; |
| 253 | } |
| 254 | |
| 255 | status_t Camera3Stream::startPrepare() { |
| 256 | ATRACE_CALL(); |
| 257 | |
| 258 | Mutex::Autolock l(mLock); |
| 259 | status_t res = OK; |
| 260 | |
| 261 | // This function should be only called when the stream is configured already. |
| 262 | if (mState != STATE_CONFIGURED) { |
| 263 | ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED " |
| 264 | "state %d", __FUNCTION__, mId, mState); |
| 265 | return INVALID_OPERATION; |
| 266 | } |
| 267 | |
| 268 | // This function can't be called if the stream has already received filled |
| 269 | // buffers |
| 270 | if (mStreamUnpreparable) { |
| 271 | ALOGE("%s: Stream %d: Can't prepare stream that's already in use", |
| 272 | __FUNCTION__, mId); |
| 273 | return INVALID_OPERATION; |
| 274 | } |
| 275 | |
| 276 | if (getHandoutOutputBufferCountLocked() > 0) { |
| 277 | ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers", |
| 278 | __FUNCTION__, mId); |
| 279 | return INVALID_OPERATION; |
| 280 | } |
| 281 | |
| 282 | if (mPrepared) return OK; |
| 283 | |
| 284 | size_t bufferCount = getBufferCountLocked(); |
| 285 | |
| 286 | mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount); |
| 287 | mPreparedBufferIdx = 0; |
| 288 | |
| 289 | mState = STATE_PREPARING; |
| 290 | |
| 291 | return NOT_ENOUGH_DATA; |
| 292 | } |
| 293 | |
| 294 | bool Camera3Stream::isPreparing() const { |
| 295 | Mutex::Autolock l(mLock); |
| 296 | return mState == STATE_PREPARING; |
| 297 | } |
| 298 | |
| 299 | status_t Camera3Stream::prepareNextBuffer() { |
| 300 | ATRACE_CALL(); |
| 301 | |
| 302 | Mutex::Autolock l(mLock); |
| 303 | status_t res = OK; |
| 304 | |
| 305 | // This function should be only called when the stream is preparing |
| 306 | if (mState != STATE_PREPARING) { |
| 307 | ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING " |
| 308 | "state %d", __FUNCTION__, mId, mState); |
| 309 | return INVALID_OPERATION; |
| 310 | } |
| 311 | |
| 312 | // Get next buffer - this may allocate, and take a while for large buffers |
| 313 | res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) ); |
| 314 | if (res != OK) { |
| 315 | ALOGE("%s: Stream %d: Unable to allocate buffer %d during preparation", |
| 316 | __FUNCTION__, mId, mPreparedBufferIdx); |
| 317 | return NO_INIT; |
| 318 | } |
| 319 | |
| 320 | mPreparedBufferIdx++; |
| 321 | |
| 322 | // Check if we still have buffers left to allocate |
| 323 | if (mPreparedBufferIdx < mPreparedBuffers.size()) { |
| 324 | return NOT_ENOUGH_DATA; |
| 325 | } |
| 326 | |
| 327 | // Done with prepare - mark stream as such, and return all buffers |
| 328 | // via cancelPrepare |
| 329 | mPrepared = true; |
| 330 | |
| 331 | return cancelPrepareLocked(); |
| 332 | } |
| 333 | |
| 334 | status_t Camera3Stream::cancelPrepare() { |
| 335 | ATRACE_CALL(); |
| 336 | |
| 337 | Mutex::Autolock l(mLock); |
| 338 | |
| 339 | return cancelPrepareLocked(); |
| 340 | } |
| 341 | |
| 342 | status_t Camera3Stream::cancelPrepareLocked() { |
| 343 | status_t res = OK; |
| 344 | |
| 345 | // This function should be only called when the stream is mid-preparing. |
| 346 | if (mState != STATE_PREPARING) { |
| 347 | ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in " |
| 348 | "PREPARING state %d", __FUNCTION__, mId, mState); |
| 349 | return INVALID_OPERATION; |
| 350 | } |
| 351 | |
| 352 | // Return all valid buffers to stream, in ERROR state to indicate |
| 353 | // they weren't filled. |
| 354 | for (size_t i = 0; i < mPreparedBufferIdx; i++) { |
| 355 | mPreparedBuffers.editItemAt(i).release_fence = -1; |
| 356 | mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; |
| 357 | returnBufferLocked(mPreparedBuffers[i], 0); |
| 358 | } |
| 359 | mPreparedBuffers.clear(); |
| 360 | mPreparedBufferIdx = 0; |
| 361 | |
| 362 | mState = STATE_CONFIGURED; |
| 363 | |
| 364 | return res; |
| 365 | } |
| 366 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 367 | status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer) { |
| 368 | ATRACE_CALL(); |
| 369 | Mutex::Autolock l(mLock); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 370 | status_t res = OK; |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 371 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 372 | // This function should be only called when the stream is configured already. |
| 373 | if (mState != STATE_CONFIGURED) { |
| 374 | ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d", |
| 375 | __FUNCTION__, mId, mState); |
| 376 | return INVALID_OPERATION; |
| 377 | } |
| 378 | |
| 379 | // Wait for new buffer returned back if we are running into the limit. |
| 380 | if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) { |
| 381 | ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.", |
| 382 | __FUNCTION__, camera3_stream::max_buffers); |
| 383 | res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration); |
| 384 | if (res != OK) { |
| 385 | if (res == TIMED_OUT) { |
| 386 | ALOGE("%s: wait for output buffer return timed out after %lldms", __FUNCTION__, |
| 387 | kWaitForBufferDuration / 1000000LL); |
| 388 | } |
| 389 | return res; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | res = getBufferLocked(buffer); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 394 | if (res == OK) { |
| 395 | fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true); |
| 396 | } |
| 397 | |
| 398 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer, |
| 402 | nsecs_t timestamp) { |
| 403 | ATRACE_CALL(); |
| 404 | Mutex::Autolock l(mLock); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 405 | |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 406 | /** |
| 407 | * TODO: Check that the state is valid first. |
| 408 | * |
| 409 | * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED. |
| 410 | * >= HAL3.2 CONFIGURED only |
| 411 | * |
| 412 | * Do this for getBuffer as well. |
| 413 | */ |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 414 | status_t res = returnBufferLocked(buffer, timestamp); |
| 415 | if (res == OK) { |
| 416 | fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 417 | mOutputBufferReturnedSignal.signal(); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 423 | status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) { |
| 424 | ATRACE_CALL(); |
| 425 | Mutex::Autolock l(mLock); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 426 | status_t res = OK; |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 427 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 428 | // This function should be only called when the stream is configured already. |
| 429 | if (mState != STATE_CONFIGURED) { |
| 430 | ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d", |
| 431 | __FUNCTION__, mId, mState); |
| 432 | return INVALID_OPERATION; |
| 433 | } |
| 434 | |
| 435 | // Wait for new buffer returned back if we are running into the limit. |
| 436 | if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) { |
| 437 | ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.", |
| 438 | __FUNCTION__, camera3_stream::max_buffers); |
| 439 | res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration); |
| 440 | if (res != OK) { |
| 441 | if (res == TIMED_OUT) { |
| 442 | ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__, |
| 443 | kWaitForBufferDuration / 1000000LL); |
| 444 | } |
| 445 | return res; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | res = getInputBufferLocked(buffer); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 450 | if (res == OK) { |
| 451 | fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false); |
| 452 | } |
| 453 | |
| 454 | return res; |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) { |
| 458 | ATRACE_CALL(); |
| 459 | Mutex::Autolock l(mLock); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 460 | |
| 461 | status_t res = returnInputBufferLocked(buffer); |
| 462 | if (res == OK) { |
| 463 | fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 464 | mInputBufferReturnedSignal.signal(); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 465 | } |
| 466 | return res; |
| 467 | } |
| 468 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 469 | status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) { |
| 470 | ATRACE_CALL(); |
| 471 | Mutex::Autolock l(mLock); |
| 472 | |
| 473 | return getInputBufferProducerLocked(producer); |
| 474 | } |
| 475 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 476 | void Camera3Stream::fireBufferListenersLocked( |
| 477 | const camera3_stream_buffer& /*buffer*/, bool acquired, bool output) { |
| 478 | List<wp<Camera3StreamBufferListener> >::iterator it, end; |
| 479 | |
| 480 | // TODO: finish implementing |
| 481 | |
| 482 | Camera3StreamBufferListener::BufferInfo info = |
| 483 | Camera3StreamBufferListener::BufferInfo(); |
| 484 | info.mOutput = output; |
| 485 | // TODO: rest of fields |
| 486 | |
| 487 | for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); |
| 488 | it != end; |
| 489 | ++it) { |
| 490 | |
| 491 | sp<Camera3StreamBufferListener> listener = it->promote(); |
| 492 | if (listener != 0) { |
| 493 | if (acquired) { |
| 494 | listener->onBufferAcquired(info); |
| 495 | } else { |
| 496 | listener->onBufferReleased(info); |
| 497 | } |
| 498 | } |
| 499 | } |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 502 | bool Camera3Stream::hasOutstandingBuffers() const { |
| 503 | ATRACE_CALL(); |
| 504 | Mutex::Autolock l(mLock); |
| 505 | return hasOutstandingBuffersLocked(); |
| 506 | } |
| 507 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 508 | status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) { |
| 509 | Mutex::Autolock l(mLock); |
| 510 | sp<StatusTracker> oldTracker = mStatusTracker.promote(); |
| 511 | if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) { |
| 512 | oldTracker->removeComponent(mStatusId); |
| 513 | } |
| 514 | mStatusId = StatusTracker::NO_STATUS_ID; |
| 515 | mStatusTracker = statusTracker; |
| 516 | |
| 517 | return OK; |
| 518 | } |
| 519 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 520 | status_t Camera3Stream::disconnect() { |
| 521 | ATRACE_CALL(); |
| 522 | Mutex::Autolock l(mLock); |
Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 523 | ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId); |
| 524 | status_t res = disconnectLocked(); |
| 525 | |
| 526 | if (res == -ENOTCONN) { |
| 527 | // "Already disconnected" -- not an error |
| 528 | return OK; |
| 529 | } else { |
| 530 | return res; |
| 531 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) { |
| 535 | ATRACE_CALL(); |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 536 | |
| 537 | /** |
| 538 | * >= CAMERA_DEVICE_API_VERSION_3_2: |
| 539 | * |
| 540 | * camera3_device_t->ops->register_stream_buffers() is not called and must |
| 541 | * be NULL. |
| 542 | */ |
| 543 | if (hal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_2) { |
| 544 | ALOGV("%s: register_stream_buffers unused as of HAL3.2", __FUNCTION__); |
| 545 | |
Igor Murashkin | c758f22 | 2014-08-19 15:14:29 -0700 | [diff] [blame] | 546 | if (hal3Device->ops->register_stream_buffers != NULL) { |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 547 | ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; " |
| 548 | "must be set to NULL in camera3_device::ops", __FUNCTION__); |
| 549 | return INVALID_OPERATION; |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | return OK; |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 555 | ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__); |
| 556 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 557 | status_t res; |
| 558 | |
| 559 | size_t bufferCount = getBufferCountLocked(); |
| 560 | |
| 561 | Vector<buffer_handle_t*> buffers; |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 562 | buffers.insertAt(/*prototype_item*/NULL, /*index*/0, bufferCount); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 563 | |
| 564 | camera3_stream_buffer_set bufferSet = camera3_stream_buffer_set(); |
| 565 | bufferSet.stream = this; |
| 566 | bufferSet.num_buffers = bufferCount; |
| 567 | bufferSet.buffers = buffers.editArray(); |
| 568 | |
| 569 | Vector<camera3_stream_buffer_t> streamBuffers; |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 570 | streamBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 571 | |
| 572 | // Register all buffers with the HAL. This means getting all the buffers |
| 573 | // from the stream, providing them to the HAL with the |
| 574 | // register_stream_buffers() method, and then returning them back to the |
| 575 | // stream in the error state, since they won't have valid data. |
| 576 | // |
| 577 | // Only registered buffers can be sent to the HAL. |
| 578 | |
| 579 | uint32_t bufferIdx = 0; |
| 580 | for (; bufferIdx < bufferCount; bufferIdx++) { |
| 581 | res = getBufferLocked( &streamBuffers.editItemAt(bufferIdx) ); |
| 582 | if (res != OK) { |
| 583 | ALOGE("%s: Unable to get buffer %d for registration with HAL", |
| 584 | __FUNCTION__, bufferIdx); |
| 585 | // Skip registering, go straight to cleanup |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | sp<Fence> fence = new Fence(streamBuffers[bufferIdx].acquire_fence); |
Mathias Agopian | d764424 | 2013-05-16 18:07:35 -0700 | [diff] [blame] | 590 | fence->waitForever("Camera3Stream::registerBuffers"); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 591 | |
| 592 | buffers.editItemAt(bufferIdx) = streamBuffers[bufferIdx].buffer; |
| 593 | } |
| 594 | if (bufferIdx == bufferCount) { |
| 595 | // Got all buffers, register with HAL |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 596 | ALOGV("%s: Registering %zu buffers with camera HAL", |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 597 | __FUNCTION__, bufferCount); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 598 | ATRACE_BEGIN("camera3->register_stream_buffers"); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 599 | res = hal3Device->ops->register_stream_buffers(hal3Device, |
| 600 | &bufferSet); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 601 | ATRACE_END(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | // Return all valid buffers to stream, in ERROR state to indicate |
| 605 | // they weren't filled. |
| 606 | for (size_t i = 0; i < bufferIdx; i++) { |
| 607 | streamBuffers.editItemAt(i).release_fence = -1; |
| 608 | streamBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; |
| 609 | returnBufferLocked(streamBuffers[i], 0); |
| 610 | } |
| 611 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 612 | mPrepared = true; |
| 613 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 614 | return res; |
| 615 | } |
| 616 | |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 617 | status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *) { |
| 618 | ALOGE("%s: This type of stream does not support output", __FUNCTION__); |
| 619 | return INVALID_OPERATION; |
| 620 | } |
| 621 | status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &, |
| 622 | nsecs_t) { |
| 623 | ALOGE("%s: This type of stream does not support output", __FUNCTION__); |
| 624 | return INVALID_OPERATION; |
| 625 | } |
| 626 | status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) { |
| 627 | ALOGE("%s: This type of stream does not support input", __FUNCTION__); |
| 628 | return INVALID_OPERATION; |
| 629 | } |
| 630 | status_t Camera3Stream::returnInputBufferLocked( |
| 631 | const camera3_stream_buffer &) { |
| 632 | ALOGE("%s: This type of stream does not support input", __FUNCTION__); |
| 633 | return INVALID_OPERATION; |
| 634 | } |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 635 | status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer) { |
| 636 | ALOGE("%s: This type of stream does not support input", __FUNCTION__); |
| 637 | return INVALID_OPERATION; |
| 638 | } |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 639 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 640 | void Camera3Stream::addBufferListener( |
| 641 | wp<Camera3StreamBufferListener> listener) { |
| 642 | Mutex::Autolock l(mLock); |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 643 | |
| 644 | List<wp<Camera3StreamBufferListener> >::iterator it, end; |
| 645 | for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); |
| 646 | it != end; |
| 647 | ) { |
| 648 | if (*it == listener) { |
| 649 | ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__); |
| 650 | return; |
| 651 | } |
| 652 | it++; |
| 653 | } |
| 654 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 655 | mBufferListenerList.push_back(listener); |
| 656 | } |
| 657 | |
| 658 | void Camera3Stream::removeBufferListener( |
| 659 | const sp<Camera3StreamBufferListener>& listener) { |
| 660 | Mutex::Autolock l(mLock); |
| 661 | |
| 662 | bool erased = true; |
| 663 | List<wp<Camera3StreamBufferListener> >::iterator it, end; |
| 664 | for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); |
| 665 | it != end; |
| 666 | ) { |
| 667 | |
| 668 | if (*it == listener) { |
| 669 | it = mBufferListenerList.erase(it); |
| 670 | erased = true; |
| 671 | } else { |
| 672 | ++it; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | if (!erased) { |
| 677 | ALOGW("%s: Could not find listener to remove, already removed", |
| 678 | __FUNCTION__); |
| 679 | } |
| 680 | } |
| 681 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 682 | }; // namespace camera3 |
| 683 | |
| 684 | }; // namespace android |